92e0292292d9f148953139b49ad3216165db0c84
[tpg/acess2.git] / Modules / USB / Core / usb_poll.c
1 /*
2  * Acess2 USB Stack
3  * - By John Hodge (thePowersGang)
4  *
5  * usb_poll.c
6  * - Endpoint polling
7  */
8 #define DEBUG   1
9 #include <usb_core.h>
10 #include "usb.h"
11
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))
15
16 // === PROTOTYPES ===
17 void    USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint);
18
19 // === GLOBALS ===
20 tUSBEndpoint    *gUSB_PollQueues[POLL_MAX/POLL_ATOM];
21  int    giUSB_PollPosition;     // Index into gUSB_PollQueues
22
23 // === CODE ===
24 void USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint)
25 {
26         tUSBEndpoint    *endpt;
27
28         // Some sanity checks
29         if(Endpoint <= 0 || Endpoint >= Iface->nEndpoints)      return ;
30         endpt = &Iface->Endpoints[Endpoint-1];
31         if(endpt->PollingPeriod > POLL_MAX || endpt->PollingPeriod <= 0)
32                 return ;
33
34         // TODO: Check that this endpoint isn't already on the queue
35
36         endpt->InputData = malloc(endpt->MaxPacketSize);
37
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;
41         // Add to poll queue
42         endpt->Next = gUSB_PollQueues[endpt->PollingAtoms];
43         gUSB_PollQueues[endpt->PollingAtoms] = endpt;
44 }
45
46 /**
47  * \brief USB polling thread
48  */
49 int USB_PollThread(void *unused)
50 {
51         for(;;)
52         {
53                 tUSBEndpoint    *ep, *prev;
54
55                 // A little evil for neater code
56                 prev = (void*)( (tVAddr)&gUSB_PollQueues[giUSB_PollPosition] - offsetof(tUSBEndpoint, Next) );
57
58                 // Process queue
59                 for( ep = gUSB_PollQueues[giUSB_PollPosition]; ep; prev = ep, ep = ep->Next )
60                 {
61                          int    period_in_atoms = ep->PollingAtoms;
62
63                         // Check for invalid entries
64                         if(period_in_atoms < 0 || period_in_atoms > POLL_ATOM)
65                         {
66                                 Log_Warning("USB", "Endpoint on polling queue with invalid period");
67                                 continue ;
68                         }
69                         // Check for entries to delete
70                         if(period_in_atoms == 0)
71                         {
72                                 // Remove
73                                 prev->Next = ep->Next;
74                                 ep->PollingAtoms = -1;  // Mark as removed
75                                 ep = prev;      // Make sure prev is kept valid
76                                 continue ;
77                         }
78
79                         // Read data
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
83                         USB_RecvDataA(
84                                 ep->Interface, ep->EndpointIdx,
85                                 ep->MaxPacketSize, ep->InputData,
86                                 ep->Interface->Driver->Endpoints[ep->EndpointIdx].DataAvail
87                                 );
88                                 
89                         // Call callback
90
91                         // Reschedule
92                         if( period_in_atoms != POLL_SLOTS )
93                         {
94                                  int    newqueue_id = (giUSB_PollPosition + period_in_atoms) % POLL_SLOTS;
95                                 tUSBEndpoint    **newqueue = &gUSB_PollQueues[newqueue_id];
96                                 
97                                 prev->Next = ep->Next;
98                                 
99                                 ep->Next = *newqueue;
100                                 *newqueue = ep;
101                         }
102                 }
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);
108         }
109 }
110

UCC git Repository :: git.ucc.asn.au