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 extern tUSBHost *gUSB_Hosts;
20 void USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint);
23 tUSBEndpoint *gUSB_PollQueues[POLL_MAX/POLL_ATOM];
24 int giUSB_PollPosition; // Index into gUSB_PollQueues
27 void USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint)
32 if(Endpoint <= 0 || Endpoint > Iface->nEndpoints) return ;
33 endpt = &Iface->Endpoints[Endpoint-1];
34 if(endpt->PollingPeriod > POLL_MAX || endpt->PollingPeriod <= 0)
37 // TODO: Check that this endpoint isn't already on the queue
39 endpt->InputData = malloc(endpt->MaxPacketSize);
41 // Determine polling period in atoms
42 endpt->PollingAtoms = (endpt->PollingPeriod + POLL_ATOM-1) / POLL_ATOM;
43 if(endpt->PollingAtoms > POLL_SLOTS) endpt->PollingAtoms = POLL_SLOTS;
47 int idx = giUSB_PollPosition + 1;
48 if(idx >= POLL_SLOTS) idx -= POLL_SLOTS;
49 endpt->Next = gUSB_PollQueues[idx];
50 gUSB_PollQueues[idx] = endpt;
55 * \brief USB polling thread
57 int USB_PollThread(void *unused)
59 Threads_SetName("USB Polling Thread");
62 tUSBEndpoint *ep, *prev;
64 if(giUSB_PollPosition == 0)
67 for( tUSBHost *host = gUSB_Hosts; host; host = host->Next )
69 host->HostDef->CheckPorts(host->Ptr);
73 // A little evil for neater code
74 prev = (void*)( (tVAddr)&gUSB_PollQueues[giUSB_PollPosition] - offsetof(tUSBEndpoint, Next) );
77 // LOG("giUSB_PollPosition = %i", giUSB_PollPosition);
78 for( ep = gUSB_PollQueues[giUSB_PollPosition]; ep; prev = ep, ep = ep->Next )
80 int period_in_atoms = ep->PollingAtoms;
81 // LOG("%i: ep = %p", giUSB_PollPosition, ep);
83 // Check for invalid entries
84 if(period_in_atoms < 0 || period_in_atoms > POLL_ATOM)
86 Log_Warning("USB", "Endpoint on polling queue with invalid period");
89 // Check for entries to delete
90 if(period_in_atoms == 0)
93 prev->Next = ep->Next;
94 ep->PollingAtoms = -1; // Mark as removed
95 ep = prev; // Make sure prev is kept valid
100 // TODO: Check the endpoint
101 // TODO: Async checking?
102 // - Send the read request on all of them then wait for the first to complete
104 ep->Interface, ep->EndpointIdx+1,
105 ep->MaxPacketSize, ep->InputData,
106 ep->Interface->Driver->Endpoints[ep->EndpointIdx].DataAvail
112 if( period_in_atoms != POLL_SLOTS )
114 int newqueue_id = (giUSB_PollPosition + period_in_atoms) % POLL_SLOTS;
115 tUSBEndpoint **newqueue = &gUSB_PollQueues[newqueue_id];
117 prev->Next = ep->Next;
119 ep->Next = *newqueue;
124 giUSB_PollPosition ++;
125 if(giUSB_PollPosition == POLL_SLOTS)
126 giUSB_PollPosition = 0;
127 // TODO: Check for a longer delay
128 Time_Delay(POLL_ATOM);