a4e8d44874077d78b78c9bf444e795972746c4a1
[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         // Determine polling period in atoms
37         endpt->PollingAtoms = (endpt->PollingPeriod + POLL_ATOM-1) / POLL_ATOM;
38         if(endpt->PollingAtoms > POLL_SLOTS)    endpt->PollingAtoms = POLL_SLOTS;
39         // Add to poll queue
40         endpt->Next = gUSB_PollQueues[endpt->PollingAtoms];
41         gUSB_PollQueues[endpt->PollingAtoms] = endpt;
42 }
43
44 /**
45  * \brief USB polling thread
46  */
47 int USB_PollThread(void *unused)
48 {
49         for(;;)
50         {
51                 tUSBEndpoint    *ep, *prev;
52
53                 // A little evil for neater code
54                 prev = (void*)( (tVAddr)&gUSB_PollQueues[giUSB_PollPosition] - offsetof(tUSBEndpoint, Next) );
55
56                 // Process queue
57                 for( ep = gUSB_PollQueues[giUSB_PollPosition]; ep; prev = ep, ep = ep->Next )
58                 {
59                          int    period_in_atoms = ep->PollingAtoms;
60
61                         // Check for invalid entries
62                         if(period_in_atoms < 0 || period_in_atoms > POLL_ATOM)
63                         {
64                                 Log_Warning("USB", "Endpoint on polling queue with invalid period");
65                                 continue ;
66                         }
67                         // Check for entries to delete
68                         if(period_in_atoms == 0)
69                         {
70                                 // Remove
71                                 prev->Next = ep->Next;
72                                 ep->PollingAtoms = -1;  // Mark as removed
73                                 ep = prev;      // Make sure prev is kept valid
74                                 continue ;
75                         }
76
77                         // Read data
78                         // TODO: Check the endpoint
79                         // TODO: Async checking?
80                         // - Send the read request on all of them then wait for the first to complete
81                         // Call callback
82
83                         // Reschedule
84                         if( period_in_atoms != POLL_SLOTS )
85                         {
86                                  int    newqueue_id = (giUSB_PollPosition + period_in_atoms) % POLL_SLOTS;
87                                 tUSBEndpoint    **newqueue = &gUSB_PollQueues[newqueue_id];
88                                 
89                                 prev->Next = ep->Next;
90                                 
91                                 ep->Next = *newqueue;
92                                 *newqueue = ep;
93                         }
94                 }
95                 giUSB_PollPosition ++;
96                 if(giUSB_PollPosition == POLL_SLOTS)
97                         giUSB_PollPosition = 0;
98                 // TODO: Check for a longer delay
99                 Time_Delay(POLL_ATOM);
100         }
101 }
102

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