635966530653d36935f3e4f991fe7d20bf86ca80
[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 #include "usb_async.h"
13
14 // === IMPORTS ===
15 extern tUSBHost *gUSB_Hosts;
16
17 // === PROTOTYPES ===
18 void    USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint);
19
20 // === GLOBALS ===
21
22 // === CODE ===
23 void USB_int_PollCallback(void *Ptr, void *Data, size_t Length)
24 {
25         tAsyncOp *op;
26         tUSBEndpoint    *ep = Ptr;
27         
28         op = malloc(sizeof(*op));
29
30         op->Next = NULL;
31         op->Endpt = ep;
32         op->Length = Length;
33         op->Data = ep->InputData;
34
35         LOG("op %p, endpoint %p (0x%x)", op, ep,
36                 ep->Interface->Dev->Address * 16 + ep->EndpointNum);
37
38         Workqueue_AddWork(&gUSB_AsyncQueue, op);
39 }
40
41 void USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint)
42 {
43         tUSBEndpoint    *endpt;
44
45         ENTER("pIface iEndpoint", Iface, Endpoint);
46
47         // Some sanity checks
48         if(Endpoint <= 0 || Endpoint > Iface->nEndpoints) {
49                 LEAVE('-');
50                 return ;
51         }
52         endpt = &Iface->Endpoints[Endpoint-1];
53         LOG("endpt(%p)->PollingPeriod = %i", endpt, endpt->PollingPeriod);
54         if(endpt->PollingPeriod > 256 || endpt->PollingPeriod <= 0) {
55                 LOG("Invalid polling period");
56                 LEAVE('-');
57                 return ;
58         }
59
60         // TODO: Check that this endpoint isn't already on the queue
61
62         endpt->InputData = malloc(endpt->MaxPacketSize);
63         LOG("Polling 0x%x at %i ms", Iface->Dev->Address * 16 + endpt->EndpointNum, endpt->PollingPeriod);
64         Iface->Dev->Host->HostDef->InterruptIN(
65                 Iface->Dev->Host->Ptr,
66                 Iface->Dev->Address * 16 + endpt->EndpointNum,
67                 endpt->PollingPeriod,
68                 USB_int_PollCallback, endpt,
69                 endpt->InputData, endpt->MaxPacketSize
70                 );
71         LEAVE('-');
72 }
73
74 /**
75  * \brief USB polling thread
76  */
77 int USB_PollThread(void *unused)
78 {
79         Threads_SetName("USB Polling Thread");
80         for(;;)
81         {
82                 // Check hosts
83                 for( tUSBHost *host = gUSB_Hosts; host; host = host->Next )
84                 {
85                         host->HostDef->CheckPorts(host->Ptr);
86                 }
87
88                 Time_Delay(100);
89         }
90 }
91

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