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

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