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 endpt->InputData = malloc(endpt->MaxPacketSize);
38 // Determine polling period in atoms
39 endpt->PollingAtoms = (endpt->PollingPeriod + POLL_ATOM-1) / POLL_ATOM;
40 if(endpt->PollingAtoms > POLL_SLOTS) endpt->PollingAtoms = POLL_SLOTS;
42 endpt->Next = gUSB_PollQueues[endpt->PollingAtoms];
43 gUSB_PollQueues[endpt->PollingAtoms] = endpt;
47 * \brief USB polling thread
49 int USB_PollThread(void *unused)
53 tUSBEndpoint *ep, *prev;
55 // A little evil for neater code
56 prev = (void*)( (tVAddr)&gUSB_PollQueues[giUSB_PollPosition] - offsetof(tUSBEndpoint, Next) );
59 for( ep = gUSB_PollQueues[giUSB_PollPosition]; ep; prev = ep, ep = ep->Next )
61 int period_in_atoms = ep->PollingAtoms;
63 // Check for invalid entries
64 if(period_in_atoms < 0 || period_in_atoms > POLL_ATOM)
66 Log_Warning("USB", "Endpoint on polling queue with invalid period");
69 // Check for entries to delete
70 if(period_in_atoms == 0)
73 prev->Next = ep->Next;
74 ep->PollingAtoms = -1; // Mark as removed
75 ep = prev; // Make sure prev is kept valid
80 // TODO: Check the endpoint
81 // TODO: Async checking?
82 // - Send the read request on all of them then wait for the first to complete
84 ep->Interface, ep->EndpointIdx,
85 ep->MaxPacketSize, ep->InputData,
86 ep->Interface->Driver->Endpoints[ep->EndpointIdx].DataAvail
92 if( period_in_atoms != POLL_SLOTS )
94 int newqueue_id = (giUSB_PollPosition + period_in_atoms) % POLL_SLOTS;
95 tUSBEndpoint **newqueue = &gUSB_PollQueues[newqueue_id];
97 prev->Next = ep->Next;
103 giUSB_PollPosition ++;
104 if(giUSB_PollPosition == POLL_SLOTS)
105 giUSB_PollPosition = 0;
106 // TODO: Check for a longer delay
107 Time_Delay(POLL_ATOM);