USB - Cleaning a little
[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   1
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         // Some sanity checks
33         if(Endpoint <= 0 || Endpoint > Iface->nEndpoints)       return ;
34         endpt = &Iface->Endpoints[Endpoint-1];
35         if(endpt->PollingPeriod > POLL_MAX || endpt->PollingPeriod <= 0)
36                 return ;
37
38         // TODO: Check that this endpoint isn't already on the queue
39
40         endpt->InputData = malloc(endpt->MaxPacketSize);
41
42         // Determine polling period in atoms
43         endpt->PollingAtoms = (endpt->PollingPeriod + POLL_ATOM-1) / POLL_ATOM;
44         if(endpt->PollingAtoms > POLL_SLOTS)    endpt->PollingAtoms = POLL_SLOTS;
45         // Add to poll queue
46         // TODO: Locking
47         {
48                  int    idx = giUSB_PollPosition + 1;
49                 if(idx >= POLL_SLOTS)   idx -= POLL_SLOTS;
50                 endpt->Next = gUSB_PollQueues[idx];
51                 gUSB_PollQueues[idx] = endpt;
52         }
53 }
54
55 /**
56  * \brief USB polling thread
57  */
58 int USB_PollThread(void *unused)
59 {
60         Threads_SetName("USB Polling Thread");
61         for(;;)
62         {
63                 tUSBEndpoint    *ep, *prev;
64
65                 if(giUSB_PollPosition == 0)
66                 {
67                         // Check hosts
68                         for( tUSBHost *host = gUSB_Hosts; host; host = host->Next )
69                         {
70                                 host->HostDef->CheckPorts(host->Ptr);
71                         }
72                 }
73
74 //              Log_Debug("USBPoll", "giUSB_PollPosition = %i", giUSB_PollPosition);
75
76                 // A little evil for neater code
77                 prev = (void*)( (tVAddr)&gUSB_PollQueues[giUSB_PollPosition] - offsetof(tUSBEndpoint, Next) );
78
79                 // Process queue
80 //              LOG("giUSB_PollPosition = %i", giUSB_PollPosition);
81                 for( ep = gUSB_PollQueues[giUSB_PollPosition]; ep; prev = ep, ep = ep->Next )
82                 {
83                          int    period_in_atoms = ep->PollingAtoms;
84 //                      LOG("%i: ep = %p", giUSB_PollPosition, ep);
85
86                         // Check for invalid entries
87                         if(period_in_atoms < 0 || period_in_atoms > POLL_ATOM)
88                         {
89                                 Log_Warning("USB", "Endpoint on polling queue with invalid period");
90                                 continue ;
91                         }
92                         // Check for entries to delete
93                         if(period_in_atoms == 0)
94                         {
95                                 // Remove
96                                 prev->Next = ep->Next;
97                                 ep->PollingAtoms = -1;  // Mark as removed
98                                 ep = prev;      // Make sure prev is kept valid
99                                 continue ;
100                         }
101
102                         // Read data
103                         // TODO: Check the endpoint
104                         // TODO: Async checking?
105                         // - Send the read request on all of them then wait for the first to complete
106                         USB_RecvDataA(
107                                 ep->Interface, ep->EndpointIdx+1,
108                                 ep->MaxPacketSize, ep->InputData,
109                                 ep->Interface->Driver->Endpoints[ep->EndpointIdx].DataAvail
110                                 );
111                                 
112                         // Call callback
113
114                         // Reschedule
115                         if( period_in_atoms != POLL_SLOTS )
116                         {
117                                  int    newqueue_id = (giUSB_PollPosition + period_in_atoms) % POLL_SLOTS;
118                                 tUSBEndpoint    **newqueue = &gUSB_PollQueues[newqueue_id];
119                                 
120                                 prev->Next = ep->Next;
121                                 
122                                 ep->Next = *newqueue;
123                                 *newqueue = ep;
124                                 ep = prev;
125                         }
126                 }
127                 giUSB_PollPosition ++;
128                 if(giUSB_PollPosition == POLL_SLOTS)
129                         giUSB_PollPosition = 0;
130                 // TODO: Check for a longer delay
131                 Time_Delay(POLL_ATOM);
132         }
133 }
134

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