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

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