Modules/USB - Interrupt endpoints work
[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 #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         Workqueue_AddWork(&gUSB_AsyncQueue, op);
36 }
37
38 void USB_StartPollingEndpoint(tUSBInterface *Iface, int Endpoint)
39 {
40         tUSBEndpoint    *endpt;
41
42         ENTER("pIface iEndpoint", Iface, Endpoint);
43
44         // Some sanity checks
45         if(Endpoint <= 0 || Endpoint > Iface->nEndpoints) {
46                 LEAVE('-');
47                 return ;
48         }
49         endpt = &Iface->Endpoints[Endpoint-1];
50         LOG("endpt(%p)->PollingPeriod = %i", endpt, endpt->PollingPeriod);
51         if(endpt->PollingPeriod > 256 || endpt->PollingPeriod <= 0) {
52                 LEAVE('-');
53                 return ;
54         }
55
56         // TODO: Check that this endpoint isn't already on the queue
57
58         endpt->InputData = malloc(endpt->MaxPacketSize);
59
60         Iface->Dev->Host->HostDef->InterruptIN(
61                 Iface->Dev->Host->Ptr,
62                 Iface->Dev->Address * 16 + endpt->EndpointNum,
63                 endpt->PollingPeriod,
64                 USB_int_PollCallback, endpt,
65                 endpt->InputData, endpt->MaxPacketSize
66                 );
67         LEAVE('-');
68 }
69
70 /**
71  * \brief USB polling thread
72  */
73 int USB_PollThread(void *unused)
74 {
75         Threads_SetName("USB Polling Thread");
76         for(;;)
77         {
78                 // Check hosts
79                 for( tUSBHost *host = gUSB_Hosts; host; host = host->Next )
80                 {
81                         host->HostDef->CheckPorts(host->Ptr);
82                 }
83
84                 Time_Delay(100);
85         }
86 }
87

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