Modules/EHCI - Random debug
[tpg/acess2.git] / KernelLand / Modules / USB / EHCI / ehci.c
1 /*
2  * Acess2 EHCI Driver
3  * - By John Hodge (thePowersGang)
4  * 
5  * ehci.c
6  * - Driver Core
7  */
8 #define DEBUG   1
9 #define VERSION VER2(0,1)
10 #include <acess.h>
11 #include <modules.h>
12 #include <usb_host.h>
13 #include "ehci.h"
14 #include <drv_pci.h>
15 #include <limits.h>
16 #include <events.h>
17 #include <timers.h>
18
19 // === CONSTANTS ===
20 #define EHCI_MAX_CONTROLLERS    4
21 #define EHCI_THREADEVENT_IOC    THREAD_EVENT_USER1
22 #define EHCI_THREADEVENT_PORTSC THREAD_EVENT_USER2
23
24 // === PROTOTYPES ===
25  int    EHCI_Initialise(char **Arguments);
26  int    EHCI_Cleanup(void);
27  int    EHCI_InitController(tPAddr BaseAddress, Uint8 InterruptNum);
28 void    EHCI_InterruptHandler(int IRQ, void *Ptr);
29 // -- API ---
30 void    *EHCI_InitInterrupt(void *Ptr, int Endpoint, int bInput, int Period, tUSBHostCb Cb, void *CbData, void *Buf, size_t Length);
31 void    *EHCI_InitIsoch  (void *Ptr, int Endpoint, size_t MaxPacketSize);
32 void    *EHCI_InitControl(void *Ptr, int Endpoint, size_t MaxPacketSize);
33 void    *EHCI_InitBulk   (void *Ptr, int Endpoint, size_t MaxPacketSize);
34 void    EHCI_RemEndpoint(void *Ptr, void *Handle);
35 void    *EHCI_SendControl(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData,
36         int isOutbound,
37         const void *SetupData, size_t SetupLength,
38         const void *OutData, size_t OutLength,
39         void *InData, size_t InLength
40         );
41 void    *EHCI_SendBulk(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData, int Dir, void *Data, size_t Length);
42 void    EHCI_FreeOp(void *Ptr, void *Handle);
43 Uint32  EHCI_int_RootHub_FeatToMask(int Feat);
44 void    EHCI_RootHub_SetPortFeature(void *Ptr, int Port, int Feat);
45 void    EHCI_RootHub_ClearPortFeature(void *Ptr, int Port, int Feat);
46  int    EHCI_RootHub_GetPortStatus(void *Ptr, int Port, int Flag);
47 // --- Internals ---
48 tEHCI_qTD       *EHCI_int_AllocateTD(tEHCI_Controller *Cont, int PID, void *Data, size_t Length, tUSBHostCb Cb, void *CbData);
49 void    EHCI_int_DeallocateTD(tEHCI_Controller *Cont, tEHCI_qTD *TD);
50 void    EHCI_int_AppendTD(tEHCI_Controller *Cont, tEHCI_QH *QH, tEHCI_qTD *TD);
51 tEHCI_QH        *EHCI_int_AllocateQH(tEHCI_Controller *Cont, int Endpoint, size_t MaxPacketSize);
52 void    EHCI_int_DeallocateQH(tEHCI_Controller *Cont, tEHCI_QH *QH);
53 void    EHCI_int_InterruptThread(void *ControllerPtr);
54
55 // === GLOBALS ===
56 MODULE_DEFINE(0, VERSION, USB_EHCI, EHCI_Initialise, NULL, "USB_Core", NULL);
57 tEHCI_Controller        gaEHCI_Controllers[EHCI_MAX_CONTROLLERS];
58 tUSBHostDef     gEHCI_HostDef = {
59         .InitInterrupt = EHCI_InitInterrupt,
60         .InitIsoch     = EHCI_InitIsoch,
61         .InitControl   = EHCI_InitControl,
62         .InitBulk      = EHCI_InitBulk,
63         .RemEndpoint   = EHCI_RemEndpoint,
64         .SendIsoch   = NULL,
65         .SendControl = EHCI_SendControl,
66         .SendBulk    = EHCI_SendBulk,
67         .FreeOp      = EHCI_FreeOp,
68         
69         .CheckPorts = NULL,     // No need
70         .SetPortFeature   = EHCI_RootHub_SetPortFeature,
71         .ClearPortFeature = EHCI_RootHub_ClearPortFeature,
72         .GetPortStatus    = EHCI_RootHub_GetPortStatus,
73         };
74
75 // === CODE ===
76 int EHCI_Initialise(char **Arguments)
77 {
78         for( int id = -1; (id = PCI_GetDeviceByClass(0x0C0320, 0xFFFFFF, id)) >= 0;  )
79         {
80                 Uint32  addr = PCI_GetBAR(id, 0);
81                 if( addr == 0 ) {
82                         // Oops, PCI BIOS emulation time
83                 }
84                 Uint8   irq = PCI_GetIRQ(id);
85                 if( irq == 0 ) {
86                         // TODO: The same
87                 }
88
89                 Log_Log("ECHI", "Controller at PCI %i 0x%x IRQ 0x%x",
90                         id, addr, irq);
91
92                 if( EHCI_InitController(addr, irq) )
93                 {
94                         // TODO: Detect other forms of failure than "out of slots"
95                         break ;
96                 }
97
98                 // TODO: Register with the USB stack
99         }
100         return 0;
101 }
102
103 int EHCI_Cleanup(void)
104 {
105         return 0;
106 }
107
108 // --- Driver Init ---
109 int EHCI_InitController(tPAddr BaseAddress, Uint8 InterruptNum)
110 {
111         tEHCI_Controller        *cont = NULL;
112
113         for( int i = 0; i < EHCI_MAX_CONTROLLERS; i ++ )
114         {
115                 if( gaEHCI_Controllers[i].PhysBase == 0 ) {
116                         cont = &gaEHCI_Controllers[i];
117                         cont->PhysBase = BaseAddress;
118                         break;
119                 }
120         }
121         if(!cont) {
122                 Log_Notice("EHCI", "Too many controllers (EHCI_MAX_CONTROLLERS=%i)",
123                         EHCI_MAX_CONTROLLERS);
124                 return 1;
125         }
126
127         // - Nuke a couple of fields so error handling code doesn't derp
128         cont->CapRegs = NULL;
129         cont->PeriodicQueue = NULL;
130         cont->TDPool = NULL;
131
132         // -- Build up structure --
133         cont->CapRegs = (void*)MM_MapHWPages(BaseAddress, 1);
134         if( !cont->CapRegs ) {
135                 Log_Warning("EHCI", "Can't map 1 page at %P into kernel space", BaseAddress);
136                 goto _error;
137         }
138         // TODO: Error check
139         if( (cont->CapRegs->CapLength & 3) ) {
140                 Log_Warning("EHCI", "Controller at %P non-aligned op regs", BaseAddress);
141                 goto _error;
142         }
143         cont->OpRegs = (void*)( (Uint32*)cont->CapRegs + cont->CapRegs->CapLength / 4 );
144         // - Allocate periodic queue
145         tPAddr  unused;
146         cont->PeriodicQueue = (void*)MM_AllocDMA(1, 32, &unused);
147         if( !cont->PeriodicQueue ) {
148                 Log_Warning("ECHI", "Can't allocate 1 32-bit page for periodic queue");
149                 goto _error;
150         }
151         for( int i = 0; i < 1024; i ++ )
152                 cont->PeriodicQueue[i] = 1;
153         // TODO: Error check
154         //  > Populate queue
155
156         // - Allocate TD pool
157         cont->TDPool = (void*)MM_AllocDMA(1, 32, &unused);
158         if( !cont->TDPool ) {
159                 Log_Warning("ECHI", "Can't allocate 1 32-bit page for qTD pool");
160                 goto _error;
161         }
162         for( int i = 0; i < TD_POOL_SIZE; i ++ ) {
163                 cont->TDPool[i].Token = 3 << 8;
164         }
165
166         // Get port count
167         cont->nPorts = cont->CapRegs->HCSParams & 0xF;
168
169         // -- Bind IRQ --
170         IRQ_AddHandler(InterruptNum, EHCI_InterruptHandler, cont);
171         cont->InterruptThread = Proc_SpawnWorker(EHCI_int_InterruptThread, cont);
172         if( !cont->InterruptThread ) {
173                 Log_Warning("EHCI", "Can't spawn interrupt worker thread");
174                 goto _error;
175         }
176         LOG("cont->InterruptThread = %p", cont->InterruptThread);
177
178         // -- Initialisation procedure (from ehci-r10) --
179         // - Reset controller
180         cont->OpRegs->USBCmd = USBCMD_HCReset;
181         // - Set CTRLDSSEGMENT (TODO: 64-bit support)
182         // - Set USBINTR
183         cont->OpRegs->USBIntr = USBINTR_IOC|USBINTR_PortChange|USBINTR_FrameRollover;
184         // - Set PERIODICLIST BASE
185         cont->OpRegs->PeridocListBase = MM_GetPhysAddr( cont->PeriodicQueue );
186         // - Enable controller
187         cont->OpRegs->USBCmd = (0x40 << 16) | USBCMD_PeriodicEnable | USBCMD_Run;
188         // - Route all ports
189         cont->OpRegs->ConfigFlag = 1;
190
191         cont->DeadTD = EHCI_int_AllocateTD(cont, 0, NULL, 0, NULL, NULL);
192         cont->DeadTD->Link = 1;
193         cont->DeadTD->Link2 = 1;
194         cont->DeadTD->Token = 0;
195
196         // -- Register with USB Core --
197         cont->RootHub = USB_RegisterHost(&gEHCI_HostDef, cont, cont->nPorts);
198
199         return 0;
200 _error:
201         cont->PhysBase = 0;
202         if( cont->CapRegs )
203                 MM_Deallocate( (tVAddr)cont->CapRegs );
204         if( cont->PeriodicQueue )
205                 MM_Deallocate( (tVAddr)cont->PeriodicQueue );
206         if( cont->TDPool )
207                 MM_Deallocate( (tVAddr)cont->TDPool );
208         return 2;
209 }
210
211 void EHCI_InterruptHandler(int IRQ, void *Ptr)
212 {
213         tEHCI_Controller *Cont = Ptr;
214         Uint32  sts = Cont->OpRegs->USBSts;
215         
216         // Clear interrupts
217         Cont->OpRegs->USBSts = sts;     
218
219         if( sts & 0xFFFF0FC0 ) {
220                 LOG("Oops, reserved bits set (%08x), funny hardware?", sts);
221                 sts &= ~0xFFFF0FFC0;
222         }
223
224         // Unmask read-only bits
225         sts &= ~(0xF000);
226
227         if( sts & USBINTR_IOC ) {
228                 // IOC
229                 Threads_PostEvent(Cont->InterruptThread, EHCI_THREADEVENT_IOC);
230                 sts &= ~USBINTR_IOC;
231         }
232
233         if( sts & USBINTR_PortChange ) {
234                 // Port change, determine what port and poke helper thread
235                 LOG("Port status change");
236                 Threads_PostEvent(Cont->InterruptThread, EHCI_THREADEVENT_PORTSC);
237                 sts &= ~USBINTR_PortChange;
238         }
239         
240         if( sts & USBINTR_FrameRollover ) {
241                 // Frame rollover, used to aid timing (trigger per-second operations)
242                 LOG("Frame rollover");
243                 sts &= ~USBINTR_FrameRollover;
244         }
245
246         if( sts ) {
247                 // Unhandled interupt bits
248                 // TODO: Warn
249                 LOG("WARN - Bitmask %x unhandled", sts);
250         }
251
252
253 }
254
255 // --------------------------------------------------------------------
256 // USB API
257 // --------------------------------------------------------------------
258 void *EHCI_InitInterrupt(void *Ptr, int Endpoint, int bOutbound, int Period,
259         tUSBHostCb Cb, void *CbData, void *Buf, size_t Length)
260 {
261         tEHCI_Controller        *Cont = Ptr;
262          int    pow2period, period_pow;
263         
264         if( Endpoint >= 256*16 )
265                 return NULL;
266         if( Period <= 0 )
267                 return NULL;
268         if( Period > 256 )
269                 Period = 256;
270
271         LOG("Endpoint=%x, bOutbound=%i, Period=%i, Length=%i", Endpoint, bOutbound, Period, Length);
272
273         // Round the period to the closest power of two
274         pow2period = 1;
275         period_pow = 0;
276         // - Find the first power above the period
277         while( pow2period < Period )
278         {
279                 pow2period *= 2;
280                 period_pow ++;
281         }
282         // - Check which is closest
283         if( Period - pow2period / 2 > pow2period - Period )
284                 Period = pow2period;
285         else {
286                 Period = pow2period/2;
287                 period_pow --;
288         }
289         
290         // Allocate a QH
291         tEHCI_QH *qh = EHCI_int_AllocateQH(Cont, Endpoint, Length);
292         qh->Impl.IntPeriodPow = period_pow;
293
294         Mutex_Acquire(&Cont->PeriodicListLock);
295
296         // Choose an interrupt slot to use      
297         int minslot = 0, minslot_load = INT_MAX;
298         for( int slot = 0; slot < Period; slot ++ )
299         {
300                  int    load = 0;
301                 for( int i = 0; i < PERIODIC_SIZE; i += Period )
302                         load += Cont->InterruptLoad[i+slot];
303                 if( load == 0 ) break;
304                 if( load < minslot_load ) {
305                         minslot = slot;
306                         minslot_load = load;
307                 }
308         }
309         // Increase loading on the selected slot
310         for( int i = minslot; i < PERIODIC_SIZE; i += Period )
311                 Cont->InterruptLoad[i] += Length;
312         qh->Impl.IntOfs = minslot;
313
314         // Allocate TD for the data
315         tEHCI_qTD *td = EHCI_int_AllocateTD(Cont, (bOutbound ? PID_OUT : PID_IN), Buf, Length, Cb, CbData);
316         EHCI_int_AppendTD(Cont, qh, td);
317
318         // Insert into the periodic list
319         for( int i = 0; i < PERIODIC_SIZE; i += Period )
320         {
321                 // Walk list until
322                 // - the end is reached
323                 // - this QH is found
324                 // - A QH with a lower period is encountered
325                 tEHCI_QH        *pqh = NULL;
326                 tEHCI_QH        *nqh;
327                 for( nqh = Cont->PeriodicQueueV[i]; nqh; pqh = nqh, nqh = nqh->Impl.Next )
328                 {
329                         if( nqh == qh )
330                                 break;
331                         if( nqh->Impl.IntPeriodPow < period_pow )
332                                 break;
333                 }
334
335                 // Somehow, we've already been added to this queue.
336                 if( nqh && nqh == qh )
337                         continue ;
338
339                 if( qh->Impl.Next && qh->Impl.Next != nqh ) {
340                         Log_Warning("EHCI", "Suspected bookkeeping error on %p - int list %i+%i overlap",
341                                 Cont, period_pow, minslot);
342                         break;
343                 }
344
345                 if( nqh ) {
346                         qh->Impl.Next = nqh;
347                         qh->HLink = MM_GetPhysAddr(nqh) | 2;
348                 }
349                 else {
350                         qh->Impl.Next = NULL;
351                         qh->HLink = 2|1;        // QH, Terminate
352                 }
353
354                 if( pqh ) {
355                         pqh->Impl.Next = qh;
356                         pqh->HLink = MM_GetPhysAddr(qh) | 2;
357                 }
358                 else {
359                         Cont->PeriodicQueueV[i] = qh;
360                         Cont->PeriodicQueue[i] = MM_GetPhysAddr(qh) | 2;
361                 }
362         }
363         Mutex_Release(&Cont->PeriodicListLock);
364
365         return qh;
366 }
367
368 void *EHCI_InitIsoch(void *Ptr, int Endpoint, size_t MaxPacketSize)
369 {
370         return (void*)(tVAddr)(Endpoint + 1);
371 }
372 void *EHCI_InitControl(void *Ptr, int Endpoint, size_t MaxPacketSize)
373 {
374         tEHCI_Controller *Cont = Ptr;
375         
376         // Allocate a QH
377         tEHCI_QH *qh = EHCI_int_AllocateQH(Cont, Endpoint, MaxPacketSize);
378         qh->CurrentTD = MM_GetPhysAddr(Cont->DeadTD);
379
380         // Append to async list 
381         if( Cont->LastAsyncHead ) {
382                 Cont->LastAsyncHead->HLink = MM_GetPhysAddr(qh)|2;
383                 Cont->LastAsyncHead->Impl.Next = qh;
384                 LOG("- Placed after %p", Cont->LastAsyncHead);
385         }
386         else {
387                 Cont->OpRegs->AsyncListAddr = MM_GetPhysAddr(qh)|2;
388         }
389         qh->HLink = Cont->OpRegs->AsyncListAddr;
390         Cont->OpRegs->USBCmd |= USBCMD_AsyncEnable;
391         Cont->LastAsyncHead = qh;
392
393         LOG("Created %p for %p Ep 0x%x - %i bytes MPS", qh, Ptr, Endpoint, MaxPacketSize);
394
395         return qh;
396 }
397 void *EHCI_InitBulk(void *Ptr, int Endpoint, size_t MaxPacketSize)
398 {
399         return EHCI_InitControl(Ptr, Endpoint, MaxPacketSize);
400 }
401 void EHCI_RemEndpoint(void *Ptr, void *Handle)
402 {
403         if( Handle == NULL )
404                 return ;
405         else if( (tVAddr)Handle <= 256*16 )
406                 return ;        // Isoch
407         else {
408                 tEHCI_QH        *qh = Ptr;
409
410                 // Remove QH from list
411                 // - If it's a polling endpoint, need to remove from all periodic lists
412                 if( qh->Impl.IntPeriodPow != 0xFF) {
413                         // Poll
414                 }
415                 else {
416                         // GP
417                 }
418                 
419                 // Deallocate QH
420                 EHCI_int_DeallocateQH(Ptr, Handle);
421         }
422 }
423
424 void *EHCI_SendControl(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData,
425         int isOutbound,
426         const void *SetupData, size_t SetupLength,
427         const void *OutData, size_t OutLength,
428         void *InData, size_t InLength
429         )
430 {
431         tEHCI_Controller *Cont = Ptr;
432         tEHCI_qTD       *td_setup, *td_data, *td_status;
433
434         // Sanity checks
435         if( (tVAddr)Dest <= 256*16 )
436                 return NULL;
437
438         LOG("Dest=%p, isOutbound=%i, Lengths(Setup:%i,Out:%i,In:%i)", Dest, isOutbound, SetupLength, OutLength, InLength);
439
440         // Check size of SETUP and status
441         
442         // Allocate TDs
443         td_setup = EHCI_int_AllocateTD(Cont, PID_SETUP, (void*)SetupData, SetupLength, NULL, NULL);
444         if( isOutbound )
445         {
446                 td_data = OutData ? EHCI_int_AllocateTD(Cont, PID_OUT, (void*)OutData, OutLength, NULL, NULL) : NULL;
447                 td_status = EHCI_int_AllocateTD(Cont, PID_IN, InData, InLength, Cb, CbData);
448         }
449         else
450         {
451                 td_data = InData ? EHCI_int_AllocateTD(Cont, PID_IN, InData, InLength, NULL, NULL) : NULL;
452                 td_status = EHCI_int_AllocateTD(Cont, PID_OUT, (void*)OutData, OutLength, Cb, CbData);
453                 td_status->Token |= (1 << 15);  // IOC
454         }
455
456         // Append TDs
457         if( td_data ) {
458                 td_setup->Link = MM_GetPhysAddr(td_data);
459                 td_data->Link = MM_GetPhysAddr(td_status) | 1;
460                 td_data->Token |= (1 << 8);     // Active
461         }
462         else {
463                 td_setup->Link = MM_GetPhysAddr(td_status) | 1;
464         }
465         td_setup->Token |= (1 << 8);    // Active
466         td_status->Token |= (1 << 8);
467         EHCI_int_AppendTD(Cont, Dest, td_setup);
468
469         return td_status;
470 }
471
472 void *EHCI_SendBulk(void *Ptr, void *Dest, tUSBHostCb Cb, void *CbData, int Dir, void *Data, size_t Length)
473 {
474         tEHCI_Controller        *Cont = Ptr;
475         
476         // Sanity check the pointer
477         // - Can't be NULL or an isoch
478         if( (tVAddr)Dest <= 256*16 )
479                 return NULL;
480         
481         // Allocate single TD
482         tEHCI_qTD       *td = EHCI_int_AllocateTD(Cont, (Dir ? PID_OUT : PID_IN), Data, Length, Cb, CbData);
483         EHCI_int_AppendTD(Cont, Dest, td);      
484
485         return td;
486 }
487
488 void EHCI_FreeOp(void *Ptr, void *Handle)
489 {
490         tEHCI_Controller        *Cont = Ptr;
491
492         EHCI_int_DeallocateTD(Cont, Handle);
493 }
494
495 Uint32 EHCI_int_RootHub_FeatToMask(int Feat)
496 {
497         switch(Feat)
498         {
499         case PORT_RESET:        return PORTSC_PortReset;
500         case PORT_ENABLE:       return PORTSC_PortEnabled;
501         default:
502                 Log_Warning("EHCI", "Unknown root hub port feature %i", Feat);
503                 return 0;
504         }
505 }
506
507 void EHCI_RootHub_SetPortFeature(void *Ptr, int Port, int Feat)
508 {
509         tEHCI_Controller        *Cont = Ptr;
510         if(Port >= Cont->nPorts)        return;
511
512         Cont->OpRegs->PortSC[Port] |= EHCI_int_RootHub_FeatToMask(Feat);
513 }
514
515 void EHCI_RootHub_ClearPortFeature(void *Ptr, int Port, int Feat)
516 {
517         tEHCI_Controller        *Cont = Ptr;
518         if(Port >= Cont->nPorts)        return;
519
520         Cont->OpRegs->PortSC[Port] &= ~EHCI_int_RootHub_FeatToMask(Feat);
521 }
522
523 int EHCI_RootHub_GetPortStatus(void *Ptr, int Port, int Flag)
524 {
525         tEHCI_Controller        *Cont = Ptr;
526         if(Port >= Cont->nPorts)        return 0;
527
528         return !!(Cont->OpRegs->PortSC[Port] & EHCI_int_RootHub_FeatToMask(Flag));
529 }
530
531 // --------------------------------------------------------------------
532 // Internals
533 // --------------------------------------------------------------------
534 tEHCI_qTD *EHCI_int_GetTDFromPhys(tEHCI_Controller *Cont, Uint32 Addr)
535 {
536         if( Addr == 0 ) return NULL;
537         LOG("%p + (%x - %x)", Cont->TDPool, Addr, MM_GetPhysAddr(Cont->TDPool));
538         return Cont->TDPool + (Addr - MM_GetPhysAddr(Cont->TDPool))/sizeof(tEHCI_qTD);
539 }
540
541 tEHCI_qTD *EHCI_int_AllocateTD(tEHCI_Controller *Cont, int PID, void *Data, size_t Length, tUSBHostCb Cb, void *CbData)
542 {
543 //      Semaphore_Wait(&Cont->TDSemaphore, 1);
544         Mutex_Acquire(&Cont->TDPoolMutex);
545         for( int i = 0; i < TD_POOL_SIZE; i ++ )
546         {
547                 if( ((Cont->TDPool[i].Token >> 8) & 3) != 3 )
548                         continue ;
549                 Cont->TDPool[i].Token = (PID << 8) | (Length << 16);
550                 // NOTE: Assumes that `Length` is <= PAGE_SIZE
551                 Cont->TDPool[i].Pages[0] = MM_GetPhysAddr(Data);
552                 if( (Cont->TDPool[i].Pages[0] & (PAGE_SIZE-1)) + Length - 1 > PAGE_SIZE )
553                         Cont->TDPool[i].Pages[1] = MM_GetPhysAddr((char*)Data + Length - 1) & ~(PAGE_SIZE-1);
554                 Mutex_Release(&Cont->TDPoolMutex);
555                 LOG("Allocated %p for PID %i on %p", &Cont->TDPool[i], PID, Cont);
556                 return &Cont->TDPool[i];
557         }
558
559         Mutex_Release(&Cont->TDPoolMutex);
560         return NULL;
561 }
562
563 void EHCI_int_DeallocateTD(tEHCI_Controller *Cont, tEHCI_qTD *TD)
564 {
565         UNIMPLEMENTED();
566 }
567
568 void EHCI_int_AppendTD(tEHCI_Controller *Cont, tEHCI_QH *QH, tEHCI_qTD *TD)
569 {
570         tEHCI_qTD       *ptd = NULL;
571         Uint32  link = QH->CurrentTD;
572
573         // TODO: Need locking and validation here
574         while( link && !(link & 1) )
575         {
576                 ptd = EHCI_int_GetTDFromPhys(Cont, link);
577                 link = ptd->Link;
578         }
579         // TODO: Figure out how to follow this properly
580         if( !ptd ) {
581                 QH->CurrentTD = MM_GetPhysAddr(TD);
582                 LOG("Appended %p to beginning of %p", TD, QH);
583         }
584         else {
585                 ptd->Link = MM_GetPhysAddr(TD);
586                 LOG("Appended %p to end of %p", TD, QH);
587         }
588 }
589
590 tEHCI_QH *EHCI_int_AllocateQH(tEHCI_Controller *Cont, int Endpoint, size_t MaxPacketSize)
591 {
592         tEHCI_QH        *ret;
593         Mutex_Acquire(&Cont->QHPoolMutex);
594         for( int i = 0; i < QH_POOL_SIZE; i ++ )
595         {
596                 if( !MM_GetPhysAddr( Cont->QHPools[i/QH_POOL_NPERPAGE] ) ) {
597                         tPAddr  tmp;
598                         Cont->QHPools[i/QH_POOL_NPERPAGE] = (void*)MM_AllocDMA(1, 32, &tmp);
599                         memset(Cont->QHPools[i/QH_POOL_NPERPAGE], 0, PAGE_SIZE);
600                 }
601
602                 ret = &Cont->QHPools[i/QH_POOL_NPERPAGE][i%QH_POOL_NPERPAGE];
603                 if( ret->HLink == 0 ) {
604                         ret->HLink = 1;
605                         ret->Overlay.Link = 1;
606                         ret->Endpoint = (Endpoint >> 4) | 0x80 | ((Endpoint & 0xF) << 8)
607                                 | (MaxPacketSize << 16);
608                         // TODO: Endpoint speed (13:12) 0:Full, 1:Low, 2:High
609                         // TODO: Control Endpoint Flag (27) 0:*, 1:Full/Low Control
610                         Mutex_Release(&Cont->QHPoolMutex);
611                         return ret;
612                 }
613         }
614         Mutex_Release(&Cont->QHPoolMutex);
615         return NULL;
616 }
617
618 void EHCI_int_DeallocateQH(tEHCI_Controller *Cont, tEHCI_QH *QH)
619 {
620         // TODO: Ensure it's unused (somehow)
621         QH->HLink = 0;
622 }
623
624 void EHCI_int_HandlePortConnectChange(tEHCI_Controller *Cont, int Port)
625 {
626         // Connect Event
627         if( Cont->OpRegs->PortSC[Port] & PORTSC_CurrentConnectStatus )
628         {
629                 // Is the device low-speed?
630                 if( (Cont->OpRegs->PortSC[Port] & PORTSC_LineStatus_MASK) == PORTSC_LineStatus_Kstate )
631                 {
632                         LOG("Low speed device on %p Port %i, giving to companion", Cont, Port);
633                         Cont->OpRegs->PortSC[Port] |= PORTSC_PortOwner;
634                 }
635                 // not a low-speed device, EHCI reset
636                 else
637                 {
638                         LOG("Device connected on %p #%i", Cont, Port);
639                         // Reset procedure.
640                         USB_PortCtl_BeginReset(Cont->RootHub, Port);
641                 }
642         }
643         // Disconnect Event
644         else
645         {
646                 if( Cont->OpRegs->PortSC[Port] & PORTSC_PortOwner ) {
647                         LOG("Companion port %i disconnected, taking it back", Port);
648                         Cont->OpRegs->PortSC[Port] &= ~PORTSC_PortOwner;
649                 }
650                 else {
651                         LOG("Port %i disconnected", Port);
652                         USB_DeviceDisconnected(Cont->RootHub, Port);
653                 }
654         }
655 }
656
657 void EHCI_int_InterruptThread(void *ControllerPtr)
658 {
659         tEHCI_Controller        *Cont = ControllerPtr;
660         while(Cont->OpRegs)
661         {
662                 Uint32  events;
663                 
664                 LOG("sleeping for events");
665                 events = Threads_WaitEvents(EHCI_THREADEVENT_IOC|EHCI_THREADEVENT_PORTSC);
666                 if( !events ) {
667                         // TODO: Should this cause a termination?
668                 }
669                 LOG("events = 0x%x", events);
670
671                 if( events & EHCI_THREADEVENT_IOC )
672                 {
673                         // IOC, Do whatever it is you do
674                 }
675
676                 // Port status change interrupt
677                 if( events & EHCI_THREADEVENT_PORTSC )
678                 {
679                         // Check for port status changes
680                         for(int i = 0; i < Cont->nPorts; i ++ )
681                         {
682                                 Uint32  sts = Cont->OpRegs->PortSC[i];
683                                 LOG("Port %i: sts = %x", i, sts);
684                                 Cont->OpRegs->PortSC[i] = sts;
685                                 if( sts & PORTSC_ConnectStatusChange )
686                                         EHCI_int_HandlePortConnectChange(Cont, i);
687
688                                 if( sts & PORTSC_PortEnableChange )
689                                 {
690                                         // Handle enable/disable
691                                 }
692
693                                 if( sts & PORTSC_OvercurrentChange )
694                                 {
695                                         // Handle over-current change
696                                 }
697                         }
698                 }
699
700                 LOG("Going back to sleep");
701         }
702 }

UCC git Repository :: git.ucc.asn.au