Kernel/USB - Still broken, reworking host controller API to give driver more information
[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
25 // === CODE ===
26 void USB_int_PollCallback(void *Ptr, void *Data, size_t Length)
27 {
28         tUSBEndpoint    *ep = Ptr;
29         
30         ep->Interface->Driver->Endpoints[ep->EndpointIdx].DataAvail(ep->Interface, ep->EndpointIdx, Length, Data);
31 }
32
33 void USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint)
34 {
35         tUSBEndpoint    *endpt;
36
37         ENTER("pIface iEndpoint", Iface, Endpoint);
38         LEAVE('-');
39
40         // Some sanity checks
41         if(Endpoint <= 0 || Endpoint > Iface->nEndpoints)
42                 return ;
43         endpt = &Iface->Endpoints[Endpoint-1];
44         LOG("endpt(%p)->PollingPeriod = %i", endpt, endpt->PollingPeriod);
45         if(endpt->PollingPeriod > POLL_MAX || endpt->PollingPeriod <= 0)
46                 return ;
47
48         // TODO: Check that this endpoint isn't already on the queue
49
50         endpt->InputData = malloc(endpt->MaxPacketSize);
51
52         Iface->Dev->Host->HostDef->InterruptIN(
53                 Iface->Dev->Host->Ptr,
54                 Iface->Dev->Address * 16 + endpt->EndpointNum,
55                 endpt->PollingPeriod,
56                 USB_int_PollCallback, endpt,
57                 endpt->InputData, endpt->MaxPacketSize
58                 );
59         LEAVE('-');
60 }
61
62 /**
63  * \brief USB polling thread
64  */
65 int USB_PollThread(void *unused)
66 {
67         Threads_SetName("USB Polling Thread");
68         for(;;)
69         {
70                 // Check hosts
71                 for( tUSBHost *host = gUSB_Hosts; host; host = host->Next )
72                 {
73                         host->HostDef->CheckPorts(host->Ptr);
74                 }
75
76                 Time_Delay(POLL_ATOM);
77         }
78 }
79

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