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)
32 ENTER("pIface iEndpoint", Iface, Endpoint);
36 if(Endpoint <= 0 || Endpoint > Iface->nEndpoints)
38 endpt = &Iface->Endpoints[Endpoint-1];
39 LOG("endpt(%p)->PollingPeriod = %i", endpt, endpt->PollingPeriod);
40 if(endpt->PollingPeriod > POLL_MAX || endpt->PollingPeriod <= 0)
43 // TODO: Check that this endpoint isn't already on the queue
45 endpt->InputData = malloc(endpt->MaxPacketSize);
47 // Determine polling period in atoms
48 endpt->PollingAtoms = (endpt->PollingPeriod + POLL_ATOM-1) / POLL_ATOM;
49 if(endpt->PollingAtoms > POLL_SLOTS) endpt->PollingAtoms = POLL_SLOTS;
50 LOG("endpt(%p)->PollingAtoms = %i", endpt, endpt->PollingAtoms);
54 int idx = giUSB_PollPosition + 1;
55 if(idx >= POLL_SLOTS) idx -= POLL_SLOTS;
56 endpt->Next = gUSB_PollQueues[idx];
57 gUSB_PollQueues[idx] = endpt;
62 * \brief USB polling thread
64 int USB_PollThread(void *unused)
66 Threads_SetName("USB Polling Thread");
69 tUSBEndpoint *ep, *prev;
71 if(giUSB_PollPosition == 0)
74 for( tUSBHost *host = gUSB_Hosts; host; host = host->Next )
76 host->HostDef->CheckPorts(host->Ptr);
80 // Log_Debug("USBPoll", "giUSB_PollPosition = %i", giUSB_PollPosition);
82 // A little evil for neater code
83 prev = (void*)( (tVAddr)&gUSB_PollQueues[giUSB_PollPosition] - offsetof(tUSBEndpoint, Next) );
86 // LOG("giUSB_PollPosition = %i", giUSB_PollPosition);
87 for( ep = gUSB_PollQueues[giUSB_PollPosition]; ep; prev = ep, ep = ep->Next )
89 int period_in_atoms = ep->PollingAtoms;
90 LOG("%i: ep = %p", giUSB_PollPosition, ep);
92 // Check for invalid entries
93 if(period_in_atoms < 0 || period_in_atoms > POLL_ATOM)
95 Log_Warning("USB", "Endpoint on polling queue with invalid period");
98 // Check for entries to delete
99 if(period_in_atoms == 0)
102 prev->Next = ep->Next;
103 ep->PollingAtoms = -1; // Mark as removed
104 ep = prev; // Make sure prev is kept valid
109 // TODO: Check the endpoint
110 // TODO: Async checking?
111 // - Send the read request on all of them then wait for the first to complete
113 ep->Interface, ep->EndpointIdx+1,
114 ep->MaxPacketSize, ep->InputData,
115 ep->Interface->Driver->Endpoints[ep->EndpointIdx].DataAvail
121 if( period_in_atoms != POLL_SLOTS )
123 int newqueue_id = (giUSB_PollPosition + period_in_atoms) % POLL_SLOTS;
124 tUSBEndpoint **newqueue = &gUSB_PollQueues[newqueue_id];
126 prev->Next = ep->Next;
128 ep->Next = *newqueue;
133 giUSB_PollPosition ++;
134 if(giUSB_PollPosition == POLL_SLOTS)
135 giUSB_PollPosition = 0;
136 // TODO: Check for a longer delay
137 Time_Delay(POLL_ATOM);