3 * - By John Hodge (thePowersGang)
12 #define POLL_ATOM 25 // 25ms atom
13 #define POLL_MAX 256 // Max period that can be nominated
14 #define POLL_SLOTS ((int)(POLL_MAX/POLL_ATOM))
17 void USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint);
20 tUSBEndpoint *gUSB_PollQueues[POLL_MAX/POLL_ATOM];
21 int giUSB_PollPosition; // Index into gUSB_PollQueues
24 void USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint)
29 if(Endpoint <= 0 || Endpoint >= Iface->nEndpoints) return ;
30 endpt = &Iface->Endpoints[Endpoint-1];
31 if(endpt->PollingPeriod > POLL_MAX || endpt->PollingPeriod <= 0)
34 // TODO: Check that this endpoint isn't already on the queue
36 // Determine polling period in atoms
37 endpt->PollingAtoms = (endpt->PollingPeriod + POLL_ATOM-1) / POLL_ATOM;
38 if(endpt->PollingAtoms > POLL_SLOTS) endpt->PollingAtoms = POLL_SLOTS;
40 endpt->Next = gUSB_PollQueues[endpt->PollingAtoms];
41 gUSB_PollQueues[endpt->PollingAtoms] = endpt;
45 * \brief USB polling thread
47 int USB_PollThread(void *unused)
51 tUSBEndpoint *ep, *prev;
53 // A little evil for neater code
54 prev = (void*)( (tVAddr)&gUSB_PollQueues[giUSB_PollPosition] - offsetof(tUSBEndpoint, Next) );
57 for( ep = gUSB_PollQueues[giUSB_PollPosition]; ep; prev = ep, ep = ep->Next )
59 int period_in_atoms = ep->PollingAtoms;
61 // Check for invalid entries
62 if(period_in_atoms < 0 || period_in_atoms > POLL_ATOM)
64 Log_Warning("USB", "Endpoint on polling queue with invalid period");
67 // Check for entries to delete
68 if(period_in_atoms == 0)
71 prev->Next = ep->Next;
72 ep->PollingAtoms = -1; // Mark as removed
73 ep = prev; // Make sure prev is kept valid
78 // TODO: Check the endpoint
79 // TODO: Async checking?
80 // - Send the read request on all of them then wait for the first to complete
84 if( period_in_atoms != POLL_SLOTS )
86 int newqueue_id = (giUSB_PollPosition + period_in_atoms) % POLL_SLOTS;
87 tUSBEndpoint **newqueue = &gUSB_PollQueues[newqueue_id];
89 prev->Next = ep->Next;
95 giUSB_PollPosition ++;
96 if(giUSB_PollPosition == POLL_SLOTS)
97 giUSB_PollPosition = 0;
98 // TODO: Check for a longer delay
99 Time_Delay(POLL_ATOM);