Kernel - Slight reworks to timer code
[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 //              Log_Debug("USBPoll", "giUSB_PollPosition = %i", giUSB_PollPosition);
74
75                 // A little evil for neater code
76                 prev = (void*)( (tVAddr)&gUSB_PollQueues[giUSB_PollPosition] - offsetof(tUSBEndpoint, Next) );
77
78                 // Process queue
79 //              LOG("giUSB_PollPosition = %i", giUSB_PollPosition);
80                 for( ep = gUSB_PollQueues[giUSB_PollPosition]; ep; prev = ep, ep = ep->Next )
81                 {
82                          int    period_in_atoms = ep->PollingAtoms;
83 //                      LOG("%i: ep = %p", giUSB_PollPosition, ep);
84
85                         // Check for invalid entries
86                         if(period_in_atoms < 0 || period_in_atoms > POLL_ATOM)
87                         {
88                                 Log_Warning("USB", "Endpoint on polling queue with invalid period");
89                                 continue ;
90                         }
91                         // Check for entries to delete
92                         if(period_in_atoms == 0)
93                         {
94                                 // Remove
95                                 prev->Next = ep->Next;
96                                 ep->PollingAtoms = -1;  // Mark as removed
97                                 ep = prev;      // Make sure prev is kept valid
98                                 continue ;
99                         }
100
101                         // Read data
102                         // TODO: Check the endpoint
103                         // TODO: Async checking?
104                         // - Send the read request on all of them then wait for the first to complete
105                         USB_RecvDataA(
106                                 ep->Interface, ep->EndpointIdx+1,
107                                 ep->MaxPacketSize, ep->InputData,
108                                 ep->Interface->Driver->Endpoints[ep->EndpointIdx].DataAvail
109                                 );
110                                 
111                         // Call callback
112
113                         // Reschedule
114                         if( period_in_atoms != POLL_SLOTS )
115                         {
116                                  int    newqueue_id = (giUSB_PollPosition + period_in_atoms) % POLL_SLOTS;
117                                 tUSBEndpoint    **newqueue = &gUSB_PollQueues[newqueue_id];
118                                 
119                                 prev->Next = ep->Next;
120                                 
121                                 ep->Next = *newqueue;
122                                 *newqueue = ep;
123                                 ep = prev;
124                         }
125                 }
126                 giUSB_PollPosition ++;
127                 if(giUSB_PollPosition == POLL_SLOTS)
128                         giUSB_PollPosition = 0;
129                 // TODO: Check for a longer delay
130                 Time_Delay(POLL_ATOM);
131         }
132 }
133

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