Libraries/ld-acess - APPEND and TRUNCATE flags (unimplimented)
[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         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->InitInterrupt(
65                 Iface->Dev->Host->Ptr, Iface->Dev->Address * 16 + endpt->EndpointNum,
66                 0, endpt->PollingPeriod,
67                 USB_int_PollCallback, endpt,
68                 endpt->InputData, endpt->MaxPacketSize
69                 );
70         LEAVE('-');
71 }
72
73 /**
74  * \brief USB polling thread
75  */
76 int USB_PollThread(void *unused)
77 {
78         Threads_SetName("USB Polling Thread");
79         for(;;)
80         {
81                 // Check hosts
82                 for( tUSBHost *host = gUSB_Hosts; host; host = host->Next )
83                 {
84                         host->HostDef->CheckPorts(host->Ptr);
85                 }
86
87                 Time_Delay(100);
88         }
89 }
90

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