3 * - By John Hodge (thePowersGang)
13 #define POLL_ATOM 25 // 25ms atom
14 #define POLL_MAX 256 // Max period that can be nominated
15 #define POLL_SLOTS ((int)(POLL_MAX/POLL_ATOM))
18 extern tUSBHost *gUSB_Hosts;
21 void USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint);
24 tUSBEndpoint *gUSB_PollQueues[POLL_MAX/POLL_ATOM];
25 int giUSB_PollPosition; // Index into gUSB_PollQueues
28 void USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint)
33 if(Endpoint <= 0 || Endpoint > Iface->nEndpoints) return ;
34 endpt = &Iface->Endpoints[Endpoint-1];
35 if(endpt->PollingPeriod > POLL_MAX || endpt->PollingPeriod <= 0)
38 // TODO: Check that this endpoint isn't already on the queue
40 endpt->InputData = malloc(endpt->MaxPacketSize);
42 // Determine polling period in atoms
43 endpt->PollingAtoms = (endpt->PollingPeriod + POLL_ATOM-1) / POLL_ATOM;
44 if(endpt->PollingAtoms > POLL_SLOTS) endpt->PollingAtoms = POLL_SLOTS;
48 int idx = giUSB_PollPosition + 1;
49 if(idx >= POLL_SLOTS) idx -= POLL_SLOTS;
50 endpt->Next = gUSB_PollQueues[idx];
51 gUSB_PollQueues[idx] = endpt;
56 * \brief USB polling thread
58 int USB_PollThread(void *unused)
60 Threads_SetName("USB Polling Thread");
63 tUSBEndpoint *ep, *prev;
65 if(giUSB_PollPosition == 0)
68 for( tUSBHost *host = gUSB_Hosts; host; host = host->Next )
70 host->HostDef->CheckPorts(host->Ptr);
74 // Log_Debug("USBPoll", "giUSB_PollPosition = %i", giUSB_PollPosition);
76 // A little evil for neater code
77 prev = (void*)( (tVAddr)&gUSB_PollQueues[giUSB_PollPosition] - offsetof(tUSBEndpoint, Next) );
80 // LOG("giUSB_PollPosition = %i", giUSB_PollPosition);
81 for( ep = gUSB_PollQueues[giUSB_PollPosition]; ep; prev = ep, ep = ep->Next )
83 int period_in_atoms = ep->PollingAtoms;
84 // LOG("%i: ep = %p", giUSB_PollPosition, ep);
86 // Check for invalid entries
87 if(period_in_atoms < 0 || period_in_atoms > POLL_ATOM)
89 Log_Warning("USB", "Endpoint on polling queue with invalid period");
92 // Check for entries to delete
93 if(period_in_atoms == 0)
96 prev->Next = ep->Next;
97 ep->PollingAtoms = -1; // Mark as removed
98 ep = prev; // Make sure prev is kept valid
103 // TODO: Check the endpoint
104 // TODO: Async checking?
105 // - Send the read request on all of them then wait for the first to complete
107 ep->Interface, ep->EndpointIdx+1,
108 ep->MaxPacketSize, ep->InputData,
109 ep->Interface->Driver->Endpoints[ep->EndpointIdx].DataAvail
115 if( period_in_atoms != POLL_SLOTS )
117 int newqueue_id = (giUSB_PollPosition + period_in_atoms) % POLL_SLOTS;
118 tUSBEndpoint **newqueue = &gUSB_PollQueues[newqueue_id];
120 prev->Next = ep->Next;
122 ep->Next = *newqueue;
127 giUSB_PollPosition ++;
128 if(giUSB_PollPosition == POLL_SLOTS)
129 giUSB_PollPosition = 0;
130 // TODO: Check for a longer delay
131 Time_Delay(POLL_ATOM);