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;
44 int idx = giUSB_PollPosition + 1;
45 if(idx >= POLL_SLOTS) idx -= POLL_SLOTS;
47 endpt->Next = gUSB_PollQueues[idx];
48 gUSB_PollQueues[idx] = endpt;
53 * \brief USB polling thread
55 int USB_PollThread(void *unused)
57 Threads_SetName("USB Polling Thread");
60 tUSBEndpoint *ep, *prev;
62 // A little evil for neater code
63 prev = (void*)( (tVAddr)&gUSB_PollQueues[giUSB_PollPosition] - offsetof(tUSBEndpoint, Next) );
66 // LOG("giUSB_PollPosition = %i", giUSB_PollPosition);
67 for( ep = gUSB_PollQueues[giUSB_PollPosition]; ep; prev = ep, ep = ep->Next )
69 int period_in_atoms = ep->PollingAtoms;
70 LOG("%i: ep = %p", giUSB_PollPosition, ep);
72 // Check for invalid entries
73 if(period_in_atoms < 0 || period_in_atoms > POLL_ATOM)
75 Log_Warning("USB", "Endpoint on polling queue with invalid period");
78 // Check for entries to delete
79 if(period_in_atoms == 0)
82 prev->Next = ep->Next;
83 ep->PollingAtoms = -1; // Mark as removed
84 ep = prev; // Make sure prev is kept valid
89 // TODO: Check the endpoint
90 // TODO: Async checking?
91 // - Send the read request on all of them then wait for the first to complete
93 ep->Interface, ep->EndpointIdx+1,
94 ep->MaxPacketSize, ep->InputData,
95 ep->Interface->Driver->Endpoints[ep->EndpointIdx].DataAvail
101 if( period_in_atoms != POLL_SLOTS )
103 int newqueue_id = (giUSB_PollPosition + period_in_atoms) % POLL_SLOTS;
104 tUSBEndpoint **newqueue = &gUSB_PollQueues[newqueue_id];
106 prev->Next = ep->Next;
108 ep->Next = *newqueue;
113 giUSB_PollPosition ++;
114 if(giUSB_PollPosition == POLL_SLOTS)
115 giUSB_PollPosition = 0;
116 // TODO: Check for a longer delay
117 Time_Delay(POLL_ATOM);