6285a11efc6183af577c8c4c2bcdc78e492abaca
[tpg/acess2.git] / KernelLand / 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   0
9 #include <usb_core.h>
10 #include "usb.h"
11 #include <timers.h>
12
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))
16
17 // === IMPORTS ===
18 extern tUSBHost *gUSB_Hosts;
19
20 // === PROTOTYPES ===
21 void    USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint);
22
23 // === GLOBALS ===
24 tUSBEndpoint    *gUSB_PollQueues[POLL_MAX/POLL_ATOM];
25  int    giUSB_PollPosition;     // Index into gUSB_PollQueues
26
27 // === CODE ===
28 void USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint)
29 {
30         tUSBEndpoint    *endpt;
31
32         ENTER("pIface iEndpoint", Iface, Endpoint);
33         LEAVE('-');
34
35         // Some sanity checks
36         if(Endpoint <= 0 || Endpoint > Iface->nEndpoints)
37                 return ;
38         endpt = &Iface->Endpoints[Endpoint-1];
39         LOG("endpt(%p)->PollingPeriod = %i", endpt, endpt->PollingPeriod);
40         if(endpt->PollingPeriod > POLL_MAX || endpt->PollingPeriod <= 0)
41                 return ;
42
43         // TODO: Check that this endpoint isn't already on the queue
44
45         endpt->InputData = malloc(endpt->MaxPacketSize);
46
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);
51         // Add to poll queue
52         // TODO: Locking
53         {
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;
58         }
59 }
60
61 /**
62  * \brief USB polling thread
63  */
64 int USB_PollThread(void *unused)
65 {
66         Threads_SetName("USB Polling Thread");
67         for(;;)
68         {
69                 tUSBEndpoint    *ep, *prev;
70
71                 if(giUSB_PollPosition == 0)
72                 {
73                         // Check hosts
74                         for( tUSBHost *host = gUSB_Hosts; host; host = host->Next )
75                         {
76                                 host->HostDef->CheckPorts(host->Ptr);
77                         }
78                 }
79
80 //              Log_Debug("USBPoll", "giUSB_PollPosition = %i", giUSB_PollPosition);
81
82                 // A little evil for neater code
83                 prev = (void*)( (tVAddr)&gUSB_PollQueues[giUSB_PollPosition] - offsetof(tUSBEndpoint, Next) );
84
85                 // Process queue
86 //              LOG("giUSB_PollPosition = %i", giUSB_PollPosition);
87                 for( ep = gUSB_PollQueues[giUSB_PollPosition]; ep; prev = ep, ep = ep->Next )
88                 {
89                          int    period_in_atoms = ep->PollingAtoms;
90                         LOG("%i: ep = %p", giUSB_PollPosition, ep);
91
92                         // Check for invalid entries
93                         if(period_in_atoms < 0 || period_in_atoms > POLL_ATOM)
94                         {
95                                 Log_Warning("USB", "Endpoint on polling queue with invalid period");
96                                 continue ;
97                         }
98                         // Check for entries to delete
99                         if(period_in_atoms == 0)
100                         {
101                                 // Remove
102                                 prev->Next = ep->Next;
103                                 ep->PollingAtoms = -1;  // Mark as removed
104                                 ep = prev;      // Make sure prev is kept valid
105                                 continue ;
106                         }
107
108                         // Read data
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
112                         USB_RecvDataA(
113                                 ep->Interface, ep->EndpointIdx+1,
114                                 ep->MaxPacketSize, ep->InputData,
115                                 ep->Interface->Driver->Endpoints[ep->EndpointIdx].DataAvail
116                                 );
117                                 
118                         // Call callback
119
120                         // Reschedule
121                         if( period_in_atoms != POLL_SLOTS )
122                         {
123                                  int    newqueue_id = (giUSB_PollPosition + period_in_atoms) % POLL_SLOTS;
124                                 tUSBEndpoint    **newqueue = &gUSB_PollQueues[newqueue_id];
125                                 
126                                 prev->Next = ep->Next;
127                                 
128                                 ep->Next = *newqueue;
129                                 *newqueue = ep;
130                                 ep = prev;
131                         }
132                 }
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);
138         }
139 }
140

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