-
-
Notifications
You must be signed in to change notification settings - Fork 337
/
ohci.cpp
2575 lines (2099 loc) · 71.7 KB
/
ohci.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2005-2013, Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Jan-Rixt Van Hoye
* Salvatore Benedetto <salvatore.benedetto@gmail.com>
* Michael Lotz <mmlr@mlotz.ch>
* Siarzhuk Zharski <imker@gmx.li>
*/
#include <module.h>
#include <PCI.h>
#include <PCI_x86.h>
#include <USB3.h>
#include <KernelExport.h>
#include <util/AutoLock.h>
#include "ohci.h"
#define USB_MODULE_NAME "ohci"
pci_module_info *OHCI::sPCIModule = NULL;
pci_x86_module_info *OHCI::sPCIx86Module = NULL;
static int32
ohci_std_ops(int32 op, ...)
{
switch (op) {
case B_MODULE_INIT:
TRACE_MODULE("init module\n");
return B_OK;
case B_MODULE_UNINIT:
TRACE_MODULE("uninit module\n");
return B_OK;
}
return EINVAL;
}
usb_host_controller_info ohci_module = {
{
"busses/usb/ohci",
0,
ohci_std_ops
},
NULL,
OHCI::AddTo
};
module_info *modules[] = {
(module_info *)&ohci_module,
NULL
};
OHCI::OHCI(pci_info *info, Stack *stack)
: BusManager(stack),
fPCIInfo(info),
fStack(stack),
fOperationalRegisters(NULL),
fRegisterArea(-1),
fHccaArea(-1),
fHcca(NULL),
fInterruptEndpoints(NULL),
fDummyControl(NULL),
fDummyBulk(NULL),
fDummyIsochronous(NULL),
fFirstTransfer(NULL),
fLastTransfer(NULL),
fFinishTransfersSem(-1),
fFinishThread(-1),
fStopFinishThread(false),
fProcessingPipe(NULL),
fFrameBandwidth(NULL),
fRootHub(NULL),
fRootHubAddress(0),
fPortCount(0),
fIRQ(0),
fUseMSI(false)
{
if (!fInitOK) {
TRACE_ERROR("bus manager failed to init\n");
return;
}
TRACE("constructing new OHCI host controller driver\n");
fInitOK = false;
mutex_init(&fEndpointLock, "ohci endpoint lock");
// enable busmaster and memory mapped access
uint16 command = sPCIModule->read_pci_config(fPCIInfo->bus,
fPCIInfo->device, fPCIInfo->function, PCI_command, 2);
command &= ~PCI_command_io;
command |= PCI_command_master | PCI_command_memory;
sPCIModule->write_pci_config(fPCIInfo->bus, fPCIInfo->device,
fPCIInfo->function, PCI_command, 2, command);
// map the registers
uint32 offset = sPCIModule->read_pci_config(fPCIInfo->bus,
fPCIInfo->device, fPCIInfo->function, PCI_base_registers, 4);
offset &= PCI_address_memory_32_mask;
TRACE_ALWAYS("iospace offset: 0x%" B_PRIx32 "\n", offset);
fRegisterArea = map_physical_memory("OHCI memory mapped registers",
offset, B_PAGE_SIZE, B_ANY_KERNEL_BLOCK_ADDRESS,
B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA | B_READ_AREA | B_WRITE_AREA,
(void **)&fOperationalRegisters);
if (fRegisterArea < B_OK) {
TRACE_ERROR("failed to map register memory\n");
return;
}
TRACE("mapped operational registers: %p\n", fOperationalRegisters);
// Check the revision of the controller, which should be 10h
uint32 revision = _ReadReg(OHCI_REVISION) & 0xff;
TRACE("version %" B_PRId32 ".%" B_PRId32 "%s\n",
OHCI_REVISION_HIGH(revision), OHCI_REVISION_LOW(revision),
OHCI_REVISION_LEGACY(revision) ? ", legacy support" : "");
if (OHCI_REVISION_HIGH(revision) != 1 || OHCI_REVISION_LOW(revision) != 0) {
TRACE_ERROR("unsupported OHCI revision\n");
return;
}
phys_addr_t hccaPhysicalAddress;
fHccaArea = fStack->AllocateArea((void **)&fHcca, &hccaPhysicalAddress,
sizeof(ohci_hcca), "USB OHCI Host Controller Communication Area");
if (fHccaArea < B_OK) {
TRACE_ERROR("unable to create the HCCA block area\n");
return;
}
memset(fHcca, 0, sizeof(ohci_hcca));
// Set Up Host controller
// Dummy endpoints
fDummyControl = _AllocateEndpoint();
if (!fDummyControl)
return;
fDummyBulk = _AllocateEndpoint();
if (!fDummyBulk) {
_FreeEndpoint(fDummyControl);
return;
}
fDummyIsochronous = _AllocateEndpoint();
if (!fDummyIsochronous) {
_FreeEndpoint(fDummyControl);
_FreeEndpoint(fDummyBulk);
return;
}
// Static endpoints that get linked in the HCCA
fInterruptEndpoints = new(std::nothrow)
ohci_endpoint_descriptor *[OHCI_STATIC_ENDPOINT_COUNT];
if (!fInterruptEndpoints) {
TRACE_ERROR("failed to allocate memory for interrupt endpoints\n");
_FreeEndpoint(fDummyControl);
_FreeEndpoint(fDummyBulk);
_FreeEndpoint(fDummyIsochronous);
return;
}
for (int32 i = 0; i < OHCI_STATIC_ENDPOINT_COUNT; i++) {
fInterruptEndpoints[i] = _AllocateEndpoint();
if (!fInterruptEndpoints[i]) {
TRACE_ERROR("failed to allocate interrupt endpoint %" B_PRId32 "\n",
i);
while (--i >= 0)
_FreeEndpoint(fInterruptEndpoints[i]);
_FreeEndpoint(fDummyBulk);
_FreeEndpoint(fDummyControl);
_FreeEndpoint(fDummyIsochronous);
return;
}
}
// build flat tree so that at each of the static interrupt endpoints
// fInterruptEndpoints[i] == interrupt endpoint for interval 2^i
uint32 interval = OHCI_BIGGEST_INTERVAL;
uint32 intervalIndex = OHCI_STATIC_ENDPOINT_COUNT - 1;
while (interval > 1) {
uint32 insertIndex = interval / 2;
while (insertIndex < OHCI_BIGGEST_INTERVAL) {
fHcca->interrupt_table[insertIndex]
= fInterruptEndpoints[intervalIndex]->physical_address;
insertIndex += interval;
}
intervalIndex--;
interval /= 2;
}
// setup the empty slot in the list and linking of all -> first
fHcca->interrupt_table[0] = fInterruptEndpoints[0]->physical_address;
for (int32 i = 1; i < OHCI_STATIC_ENDPOINT_COUNT; i++) {
fInterruptEndpoints[i]->next_physical_endpoint
= fInterruptEndpoints[0]->physical_address;
fInterruptEndpoints[i]->next_logical_endpoint
= fInterruptEndpoints[0];
}
// Now link the first endpoint to the isochronous endpoint
fInterruptEndpoints[0]->next_physical_endpoint
= fDummyIsochronous->physical_address;
// When the handover from SMM takes place, all interrupts are routed to the
// OS. As we don't yet have an interrupt handler installed at this point,
// this may cause interrupt storms if the firmware does not disable the
// interrupts during handover. Therefore we disable interrupts before
// requesting ownership. We have to keep the ownership change interrupt
// enabled though, as otherwise the SMM will not be notified of the
// ownership change request we trigger below.
_WriteReg(OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTERRUPTS &
~OHCI_OWNERSHIP_CHANGE) ;
// Determine in what context we are running (Kindly copied from FreeBSD)
uint32 control = _ReadReg(OHCI_CONTROL);
if (control & OHCI_INTERRUPT_ROUTING) {
TRACE_ALWAYS("smm is in control of the host controller\n");
uint32 status = _ReadReg(OHCI_COMMAND_STATUS);
_WriteReg(OHCI_COMMAND_STATUS, status | OHCI_OWNERSHIP_CHANGE_REQUEST);
for (uint32 i = 0; i < 100 && (control & OHCI_INTERRUPT_ROUTING); i++) {
snooze(1000);
control = _ReadReg(OHCI_CONTROL);
}
if ((control & OHCI_INTERRUPT_ROUTING) != 0) {
TRACE_ERROR("smm does not respond.\n");
// TODO: Enable this reset as soon as the non-specified
// reset a few lines later is replaced by a better solution.
//_WriteReg(OHCI_CONTROL, OHCI_HC_FUNCTIONAL_STATE_RESET);
//snooze(USB_DELAY_BUS_RESET);
} else
TRACE_ALWAYS("ownership change successful\n");
} else {
TRACE("cold started\n");
snooze(USB_DELAY_BUS_RESET);
}
// TODO: This reset delays system boot time. It should not be necessary
// according to the OHCI spec, but without it some controllers don't start.
_WriteReg(OHCI_CONTROL, OHCI_HC_FUNCTIONAL_STATE_RESET);
snooze(USB_DELAY_BUS_RESET);
// We now own the host controller and the bus has been reset
uint32 frameInterval = _ReadReg(OHCI_FRAME_INTERVAL);
uint32 intervalValue = OHCI_GET_INTERVAL_VALUE(frameInterval);
_WriteReg(OHCI_COMMAND_STATUS, OHCI_HOST_CONTROLLER_RESET);
// Nominal time for a reset is 10 us
uint32 reset = 0;
for (uint32 i = 0; i < 10; i++) {
spin(10);
reset = _ReadReg(OHCI_COMMAND_STATUS) & OHCI_HOST_CONTROLLER_RESET;
if (reset == 0)
break;
}
if (reset) {
TRACE_ERROR("error resetting the host controller (timeout)\n");
return;
}
// The controller is now in SUSPEND state, we have 2ms to go OPERATIONAL.
// Set up host controller register
_WriteReg(OHCI_HCCA, (uint32)hccaPhysicalAddress);
_WriteReg(OHCI_CONTROL_HEAD_ED, (uint32)fDummyControl->physical_address);
_WriteReg(OHCI_BULK_HEAD_ED, (uint32)fDummyBulk->physical_address);
// Switch on desired functional features
control = _ReadReg(OHCI_CONTROL);
control &= ~(OHCI_CONTROL_BULK_SERVICE_RATIO_MASK | OHCI_ENABLE_LIST
| OHCI_HC_FUNCTIONAL_STATE_MASK | OHCI_INTERRUPT_ROUTING);
control |= OHCI_ENABLE_LIST | OHCI_CONTROL_BULK_RATIO_1_4
| OHCI_HC_FUNCTIONAL_STATE_OPERATIONAL;
// And finally start the controller
_WriteReg(OHCI_CONTROL, control);
// The controller is now OPERATIONAL.
frameInterval = (_ReadReg(OHCI_FRAME_INTERVAL) & OHCI_FRAME_INTERVAL_TOGGLE)
^ OHCI_FRAME_INTERVAL_TOGGLE;
frameInterval |= OHCI_FSMPS(intervalValue) | intervalValue;
_WriteReg(OHCI_FRAME_INTERVAL, frameInterval);
// 90% periodic
uint32 periodic = OHCI_PERIODIC(intervalValue);
_WriteReg(OHCI_PERIODIC_START, periodic);
// Fiddle the No Over Current Protection bit to avoid chip bug
uint32 desca = _ReadReg(OHCI_RH_DESCRIPTOR_A);
_WriteReg(OHCI_RH_DESCRIPTOR_A, desca | OHCI_RH_NO_OVER_CURRENT_PROTECTION);
_WriteReg(OHCI_RH_STATUS, OHCI_RH_LOCAL_POWER_STATUS_CHANGE);
snooze(OHCI_ENABLE_POWER_DELAY);
_WriteReg(OHCI_RH_DESCRIPTOR_A, desca);
// The AMD756 requires a delay before re-reading the register,
// otherwise it will occasionally report 0 ports.
uint32 numberOfPorts = 0;
for (uint32 i = 0; i < 10 && numberOfPorts == 0; i++) {
snooze(OHCI_READ_DESC_DELAY);
uint32 descriptor = _ReadReg(OHCI_RH_DESCRIPTOR_A);
numberOfPorts = OHCI_RH_GET_PORT_COUNT(descriptor);
}
if (numberOfPorts > OHCI_MAX_PORT_COUNT)
numberOfPorts = OHCI_MAX_PORT_COUNT;
fPortCount = numberOfPorts;
TRACE("port count is %d\n", fPortCount);
// Create the array that will keep bandwidth information
fFrameBandwidth = new(std::nothrow) uint16[NUMBER_OF_FRAMES];
for (int32 i = 0; i < NUMBER_OF_FRAMES; i++)
fFrameBandwidth[i] = MAX_AVAILABLE_BANDWIDTH;
// Create semaphore the finisher thread will wait for
fFinishTransfersSem = create_sem(0, "OHCI Finish Transfers");
if (fFinishTransfersSem < B_OK) {
TRACE_ERROR("failed to create semaphore\n");
return;
}
// Create the finisher service thread
fFinishThread = spawn_kernel_thread(_FinishThread, "ohci finish thread",
B_URGENT_DISPLAY_PRIORITY, (void *)this);
resume_thread(fFinishThread);
// Find the right interrupt vector, using MSIs if available.
fIRQ = fPCIInfo->u.h0.interrupt_line;
if (sPCIx86Module != NULL && sPCIx86Module->get_msi_count(fPCIInfo->bus,
fPCIInfo->device, fPCIInfo->function) >= 1) {
uint8 msiVector = 0;
if (sPCIx86Module->configure_msi(fPCIInfo->bus, fPCIInfo->device,
fPCIInfo->function, 1, &msiVector) == B_OK
&& sPCIx86Module->enable_msi(fPCIInfo->bus, fPCIInfo->device,
fPCIInfo->function) == B_OK) {
TRACE_ALWAYS("using message signaled interrupts\n");
fIRQ = msiVector;
fUseMSI = true;
}
}
// Install the interrupt handler
TRACE("installing interrupt handler\n");
install_io_interrupt_handler(fIRQ, _InterruptHandler, (void *)this, 0);
// Enable interesting interrupts now that the handler is in place
_WriteReg(OHCI_INTERRUPT_ENABLE, OHCI_NORMAL_INTERRUPTS
| OHCI_MASTER_INTERRUPT_ENABLE);
TRACE("OHCI host controller driver constructed\n");
fInitOK = true;
}
OHCI::~OHCI()
{
int32 result = 0;
fStopFinishThread = true;
delete_sem(fFinishTransfersSem);
wait_for_thread(fFinishThread, &result);
remove_io_interrupt_handler(fIRQ, _InterruptHandler, (void *)this);
_LockEndpoints();
mutex_destroy(&fEndpointLock);
if (fHccaArea >= B_OK)
delete_area(fHccaArea);
if (fRegisterArea >= B_OK)
delete_area(fRegisterArea);
_FreeEndpoint(fDummyControl);
_FreeEndpoint(fDummyBulk);
_FreeEndpoint(fDummyIsochronous);
if (fInterruptEndpoints != NULL) {
for (int i = 0; i < OHCI_STATIC_ENDPOINT_COUNT; i++)
_FreeEndpoint(fInterruptEndpoints[i]);
}
delete [] fFrameBandwidth;
delete [] fInterruptEndpoints;
delete fRootHub;
if (fUseMSI && sPCIx86Module != NULL) {
sPCIx86Module->disable_msi(fPCIInfo->bus,
fPCIInfo->device, fPCIInfo->function);
sPCIx86Module->unconfigure_msi(fPCIInfo->bus,
fPCIInfo->device, fPCIInfo->function);
}
put_module(B_PCI_MODULE_NAME);
if (sPCIx86Module != NULL) {
sPCIx86Module = NULL;
put_module(B_PCI_X86_MODULE_NAME);
}
}
status_t
OHCI::Start()
{
TRACE("starting OHCI host controller\n");
uint32 control = _ReadReg(OHCI_CONTROL);
if ((control & OHCI_HC_FUNCTIONAL_STATE_MASK)
!= OHCI_HC_FUNCTIONAL_STATE_OPERATIONAL) {
TRACE_ERROR("controller not started (0x%08" B_PRIx32 ")!\n", control);
return B_ERROR;
} else
TRACE("controller is operational!\n");
fRootHubAddress = AllocateAddress();
fRootHub = new(std::nothrow) OHCIRootHub(RootObject(), fRootHubAddress);
if (!fRootHub) {
TRACE_ERROR("no memory to allocate root hub\n");
return B_NO_MEMORY;
}
if (fRootHub->InitCheck() < B_OK) {
TRACE_ERROR("root hub failed init check\n");
return B_ERROR;
}
SetRootHub(fRootHub);
TRACE_ALWAYS("successfully started the controller\n");
return BusManager::Start();
}
status_t
OHCI::SubmitTransfer(Transfer *transfer)
{
// short circuit the root hub
if (transfer->TransferPipe()->DeviceAddress() == fRootHubAddress)
return fRootHub->ProcessTransfer(this, transfer);
uint32 type = transfer->TransferPipe()->Type();
if (type & USB_OBJECT_CONTROL_PIPE) {
TRACE("submitting request\n");
return _SubmitRequest(transfer);
}
if ((type & USB_OBJECT_BULK_PIPE) || (type & USB_OBJECT_INTERRUPT_PIPE)) {
TRACE("submitting %s transfer\n",
(type & USB_OBJECT_BULK_PIPE) ? "bulk" : "interrupt");
return _SubmitTransfer(transfer);
}
if (type & USB_OBJECT_ISO_PIPE) {
TRACE("submitting isochronous transfer\n");
return _SubmitIsochronousTransfer(transfer);
}
TRACE_ERROR("tried to submit transfer for unknown pipe type %" B_PRIu32 "\n",
type);
return B_ERROR;
}
status_t
OHCI::CancelQueuedTransfers(Pipe *pipe, bool force)
{
if (!Lock())
return B_ERROR;
struct transfer_entry {
Transfer * transfer;
transfer_entry * next;
};
transfer_entry *list = NULL;
transfer_data *current = fFirstTransfer;
while (current) {
if (current->transfer && current->transfer->TransferPipe() == pipe) {
// Check if the skip bit is already set
if (!(current->endpoint->flags & OHCI_ENDPOINT_SKIP)) {
current->endpoint->flags |= OHCI_ENDPOINT_SKIP;
// In case the controller is processing
// this endpoint, wait for it to finish
snooze(1000);
}
// Clear the endpoint
current->endpoint->head_physical_descriptor
= current->endpoint->tail_physical_descriptor;
if (!force) {
if (pipe->Type() & USB_OBJECT_ISO_PIPE) {
ohci_isochronous_td *descriptor
= (ohci_isochronous_td *)current->first_descriptor;
while (descriptor) {
uint16 frame = OHCI_ITD_GET_STARTING_FRAME(
descriptor->flags);
_ReleaseIsochronousBandwidth(frame,
OHCI_ITD_GET_FRAME_COUNT(descriptor->flags));
if (descriptor
== (ohci_isochronous_td*)current->last_descriptor)
// this is the last ITD of the transfer
break;
descriptor
= (ohci_isochronous_td *)
descriptor->next_done_descriptor;
}
}
// If the transfer is canceled by force, the one causing the
// cancel is probably not the one who initiated the transfer
// and the callback is likely not safe anymore
transfer_entry *entry
= (transfer_entry *)malloc(sizeof(transfer_entry));
if (entry != NULL) {
entry->transfer = current->transfer;
current->transfer = NULL;
entry->next = list;
list = entry;
}
}
current->canceled = true;
}
current = current->link;
}
Unlock();
while (list != NULL) {
transfer_entry *next = list->next;
list->transfer->Finished(B_CANCELED, 0);
delete list->transfer;
free(list);
list = next;
}
// wait for any transfers that might have made it before canceling
while (fProcessingPipe == pipe)
snooze(1000);
// notify the finisher so it can clean up the canceled transfers
release_sem_etc(fFinishTransfersSem, 1, B_DO_NOT_RESCHEDULE);
return B_OK;
}
status_t
OHCI::NotifyPipeChange(Pipe *pipe, usb_change change)
{
TRACE("pipe change %d for pipe %p\n", change, pipe);
if (pipe->DeviceAddress() == fRootHubAddress) {
// no need to insert/remove endpoint descriptors for the root hub
return B_OK;
}
switch (change) {
case USB_CHANGE_CREATED:
return _InsertEndpointForPipe(pipe);
case USB_CHANGE_DESTROYED:
return _RemoveEndpointForPipe(pipe);
case USB_CHANGE_PIPE_POLICY_CHANGED:
TRACE("pipe policy changing unhandled!\n");
break;
default:
TRACE_ERROR("unknown pipe change!\n");
return B_ERROR;
}
return B_OK;
}
status_t
OHCI::AddTo(Stack *stack)
{
#ifdef TRACE_USB
set_dprintf_enabled(true);
#ifndef HAIKU_TARGET_PLATFORM_HAIKU
load_driver_symbols("ohci");
#endif
#endif
if (!sPCIModule) {
status_t status = get_module(B_PCI_MODULE_NAME, (module_info **)&sPCIModule);
if (status < B_OK) {
TRACE_MODULE_ERROR("getting pci module failed! 0x%08" B_PRIx32 "\n",
status);
return status;
}
}
TRACE_MODULE("searching devices\n");
bool found = false;
pci_info *item = new(std::nothrow) pci_info;
if (!item) {
sPCIModule = NULL;
put_module(B_PCI_MODULE_NAME);
return B_NO_MEMORY;
}
// Try to get the PCI x86 module as well so we can enable possible MSIs.
if (sPCIx86Module == NULL && get_module(B_PCI_X86_MODULE_NAME,
(module_info **)&sPCIx86Module) != B_OK) {
// If it isn't there, that's not critical though.
TRACE_MODULE_ERROR("failed to get pci x86 module\n");
sPCIx86Module = NULL;
}
for (uint32 i = 0 ; sPCIModule->get_nth_pci_info(i, item) >= B_OK; i++) {
if (item->class_base == PCI_serial_bus && item->class_sub == PCI_usb
&& item->class_api == PCI_usb_ohci) {
if (item->u.h0.interrupt_line == 0
|| item->u.h0.interrupt_line == 0xFF) {
TRACE_MODULE_ERROR("found device with invalid IRQ -"
" check IRQ assignement\n");
continue;
}
TRACE_MODULE("found device at IRQ %u\n",
item->u.h0.interrupt_line);
OHCI *bus = new(std::nothrow) OHCI(item, stack);
if (!bus) {
delete item;
sPCIModule = NULL;
put_module(B_PCI_MODULE_NAME);
if (sPCIx86Module != NULL) {
sPCIx86Module = NULL;
put_module(B_PCI_X86_MODULE_NAME);
}
return B_NO_MEMORY;
}
if (bus->InitCheck() < B_OK) {
TRACE_MODULE_ERROR("bus failed init check\n");
delete bus;
continue;
}
// the bus took it away
item = new(std::nothrow) pci_info;
bus->Start();
stack->AddBusManager(bus);
found = true;
}
}
if (!found) {
TRACE_MODULE_ERROR("no devices found\n");
delete item;
sPCIModule = NULL;
put_module(B_PCI_MODULE_NAME);
if (sPCIx86Module != NULL) {
sPCIx86Module = NULL;
put_module(B_PCI_X86_MODULE_NAME);
}
return ENODEV;
}
delete item;
return B_OK;
}
status_t
OHCI::GetPortStatus(uint8 index, usb_port_status *status)
{
if (index >= fPortCount) {
TRACE_ERROR("get port status for invalid port %u\n", index);
return B_BAD_INDEX;
}
status->status = status->change = 0;
uint32 portStatus = _ReadReg(OHCI_RH_PORT_STATUS(index));
// status
if (portStatus & OHCI_RH_PORTSTATUS_CCS)
status->status |= PORT_STATUS_CONNECTION;
if (portStatus & OHCI_RH_PORTSTATUS_PES)
status->status |= PORT_STATUS_ENABLE;
if (portStatus & OHCI_RH_PORTSTATUS_PSS)
status->status |= PORT_STATUS_SUSPEND;
if (portStatus & OHCI_RH_PORTSTATUS_POCI)
status->status |= PORT_STATUS_OVER_CURRENT;
if (portStatus & OHCI_RH_PORTSTATUS_PRS)
status->status |= PORT_STATUS_RESET;
if (portStatus & OHCI_RH_PORTSTATUS_PPS)
status->status |= PORT_STATUS_POWER;
if (portStatus & OHCI_RH_PORTSTATUS_LSDA)
status->status |= PORT_STATUS_LOW_SPEED;
// change
if (portStatus & OHCI_RH_PORTSTATUS_CSC)
status->change |= PORT_STATUS_CONNECTION;
if (portStatus & OHCI_RH_PORTSTATUS_PESC)
status->change |= PORT_STATUS_ENABLE;
if (portStatus & OHCI_RH_PORTSTATUS_PSSC)
status->change |= PORT_STATUS_SUSPEND;
if (portStatus & OHCI_RH_PORTSTATUS_OCIC)
status->change |= PORT_STATUS_OVER_CURRENT;
if (portStatus & OHCI_RH_PORTSTATUS_PRSC)
status->change |= PORT_STATUS_RESET;
TRACE("port %u status 0x%04x change 0x%04x\n", index,
status->status, status->change);
return B_OK;
}
status_t
OHCI::SetPortFeature(uint8 index, uint16 feature)
{
TRACE("set port feature index %u feature %u\n", index, feature);
if (index > fPortCount)
return B_BAD_INDEX;
switch (feature) {
case PORT_ENABLE:
_WriteReg(OHCI_RH_PORT_STATUS(index), OHCI_RH_PORTSTATUS_PES);
return B_OK;
case PORT_SUSPEND:
_WriteReg(OHCI_RH_PORT_STATUS(index), OHCI_RH_PORTSTATUS_PSS);
return B_OK;
case PORT_RESET:
_WriteReg(OHCI_RH_PORT_STATUS(index), OHCI_RH_PORTSTATUS_PRS);
return B_OK;
case PORT_POWER:
_WriteReg(OHCI_RH_PORT_STATUS(index), OHCI_RH_PORTSTATUS_PPS);
return B_OK;
}
return B_BAD_VALUE;
}
status_t
OHCI::ClearPortFeature(uint8 index, uint16 feature)
{
TRACE("clear port feature index %u feature %u\n", index, feature);
if (index > fPortCount)
return B_BAD_INDEX;
switch (feature) {
case PORT_ENABLE:
_WriteReg(OHCI_RH_PORT_STATUS(index), OHCI_RH_PORTSTATUS_CCS);
return B_OK;
case PORT_SUSPEND:
_WriteReg(OHCI_RH_PORT_STATUS(index), OHCI_RH_PORTSTATUS_POCI);
return B_OK;
case PORT_POWER:
_WriteReg(OHCI_RH_PORT_STATUS(index), OHCI_RH_PORTSTATUS_LSDA);
return B_OK;
case C_PORT_CONNECTION:
_WriteReg(OHCI_RH_PORT_STATUS(index), OHCI_RH_PORTSTATUS_CSC);
return B_OK;
case C_PORT_ENABLE:
_WriteReg(OHCI_RH_PORT_STATUS(index), OHCI_RH_PORTSTATUS_PESC);
return B_OK;
case C_PORT_SUSPEND:
_WriteReg(OHCI_RH_PORT_STATUS(index), OHCI_RH_PORTSTATUS_PSSC);
return B_OK;
case C_PORT_OVER_CURRENT:
_WriteReg(OHCI_RH_PORT_STATUS(index), OHCI_RH_PORTSTATUS_OCIC);
return B_OK;
case C_PORT_RESET:
_WriteReg(OHCI_RH_PORT_STATUS(index), OHCI_RH_PORTSTATUS_PRSC);
return B_OK;
}
return B_BAD_VALUE;
}
int32
OHCI::_InterruptHandler(void *data)
{
return ((OHCI *)data)->_Interrupt();
}
int32
OHCI::_Interrupt()
{
static spinlock lock = B_SPINLOCK_INITIALIZER;
acquire_spinlock(&lock);
uint32 status = 0;
uint32 acknowledge = 0;
bool finishTransfers = false;
int32 result = B_HANDLED_INTERRUPT;
// The LSb of done_head is used to inform the HCD that an interrupt
// condition exists for both the done list and for another event recorded in
// the HcInterruptStatus register. If done_head is 0, then the interrupt
// was caused by other than the HccaDoneHead update and the
// HcInterruptStatus register needs to be accessed to determine that exact
// interrupt cause. If HccDoneHead is nonzero, then a done list update
// interrupt is indicated and if the LSb of the Dword is nonzero, then an
// additional interrupt event is indicated and HcInterruptStatus should be
// checked to determine its cause.
uint32 doneHead = fHcca->done_head;
if (doneHead != 0) {
status = OHCI_WRITEBACK_DONE_HEAD;
if (doneHead & OHCI_DONE_INTERRUPTS)
status |= _ReadReg(OHCI_INTERRUPT_STATUS)
& _ReadReg(OHCI_INTERRUPT_ENABLE);
} else {
status = _ReadReg(OHCI_INTERRUPT_STATUS) & _ReadReg(OHCI_INTERRUPT_ENABLE)
& ~OHCI_WRITEBACK_DONE_HEAD;
if (status == 0) {
// Nothing to be done (PCI shared interrupt)
release_spinlock(&lock);
return B_UNHANDLED_INTERRUPT;
}
}
if (status & OHCI_SCHEDULING_OVERRUN) {
TRACE_MODULE("scheduling overrun occured\n");
acknowledge |= OHCI_SCHEDULING_OVERRUN;
}
if (status & OHCI_WRITEBACK_DONE_HEAD) {
TRACE_MODULE("transfer descriptors processed\n");
fHcca->done_head = 0;
acknowledge |= OHCI_WRITEBACK_DONE_HEAD;
result = B_INVOKE_SCHEDULER;
finishTransfers = true;
}
if (status & OHCI_RESUME_DETECTED) {
TRACE_MODULE("resume detected\n");
acknowledge |= OHCI_RESUME_DETECTED;
}
if (status & OHCI_UNRECOVERABLE_ERROR) {
TRACE_MODULE_ERROR("unrecoverable error - controller halted\n");
_WriteReg(OHCI_CONTROL, OHCI_HC_FUNCTIONAL_STATE_RESET);
// TODO: clear all pending transfers, reset and resetup the controller
}
if (status & OHCI_ROOT_HUB_STATUS_CHANGE) {
TRACE_MODULE("root hub status change\n");
// Disable the interrupt as it will otherwise be retriggered until the
// port has been reset and the change is cleared explicitly.
// TODO: renable it once we use status changes instead of polling
_WriteReg(OHCI_INTERRUPT_DISABLE, OHCI_ROOT_HUB_STATUS_CHANGE);
acknowledge |= OHCI_ROOT_HUB_STATUS_CHANGE;
}
if (acknowledge != 0)
_WriteReg(OHCI_INTERRUPT_STATUS, acknowledge);
release_spinlock(&lock);
if (finishTransfers)
release_sem_etc(fFinishTransfersSem, 1, B_DO_NOT_RESCHEDULE);
return result;
}
status_t
OHCI::_AddPendingTransfer(Transfer *transfer,
ohci_endpoint_descriptor *endpoint, ohci_general_td *firstDescriptor,
ohci_general_td *dataDescriptor, ohci_general_td *lastDescriptor,
bool directionIn)
{
if (!transfer || !endpoint || !lastDescriptor)
return B_BAD_VALUE;
transfer_data *data = new(std::nothrow) transfer_data;
if (!data)
return B_NO_MEMORY;
status_t result = transfer->InitKernelAccess();
if (result < B_OK) {
delete data;
return result;
}
data->transfer = transfer;
data->endpoint = endpoint;
data->incoming = directionIn;
data->canceled = false;
data->link = NULL;
// the current tail will become the first descriptor
data->first_descriptor = (ohci_general_td *)endpoint->tail_logical_descriptor;
// the data and first descriptors might be the same
if (dataDescriptor == firstDescriptor)
data->data_descriptor = data->first_descriptor;
else
data->data_descriptor = dataDescriptor;
// even the last and the first descriptor might be the same
if (lastDescriptor == firstDescriptor)
data->last_descriptor = data->first_descriptor;
else
data->last_descriptor = lastDescriptor;
if (!Lock()) {
delete data;
return B_ERROR;
}
if (fLastTransfer)
fLastTransfer->link = data;
else
fFirstTransfer = data;
fLastTransfer = data;
Unlock();
return B_OK;
}
status_t
OHCI::_AddPendingIsochronousTransfer(Transfer *transfer,
ohci_endpoint_descriptor *endpoint, ohci_isochronous_td *firstDescriptor,
ohci_isochronous_td *lastDescriptor, bool directionIn)
{
if (!transfer || !endpoint || !lastDescriptor)
return B_BAD_VALUE;
transfer_data *data = new(std::nothrow) transfer_data;
if (!data)
return B_NO_MEMORY;
status_t result = transfer->InitKernelAccess();
if (result < B_OK) {
delete data;
return result;
}
data->transfer = transfer;
data->endpoint = endpoint;
data->incoming = directionIn;
data->canceled = false;
data->link = NULL;
// the current tail will become the first descriptor
data->first_descriptor = (ohci_general_td*)endpoint->tail_logical_descriptor;
// the data and first descriptors are the same
data->data_descriptor = data->first_descriptor;
// the last and the first descriptor might be the same
if (lastDescriptor == firstDescriptor)
data->last_descriptor = data->first_descriptor;
else
data->last_descriptor = (ohci_general_td*)lastDescriptor;
if (!Lock()) {
delete data;
return B_ERROR;
}
if (fLastTransfer)
fLastTransfer->link = data;
else
fFirstTransfer = data;
fLastTransfer = data;
Unlock();
return B_OK;
}
int32
OHCI::_FinishThread(void *data)
{
((OHCI *)data)->_FinishTransfers();
return B_OK;
}