USB - Updated code to use endpoint registration
[tpg/acess2.git] / KernelLand / Modules / USB / Core / usb_lowlevel.c
1 /*
2  * Acess 2 USB Stack
3  * - By John Hodge (thePowersGang)
4  * 
5  * usb_lowlevel.c
6  * - Low Level IO
7  */
8 #define DEBUG   0
9 #include <acess.h>
10 #include "usb.h"
11 #include "usb_proto.h"
12 #include "usb_lowlevel.h"
13 #include <timers.h>
14 #include <events.h>
15
16 // === PROTOTYPES ===
17 void    *USB_int_Request(tUSBDevice *Dev, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data);
18 void    USB_int_WakeThread(void *Thread, void *Data, size_t Length);
19  int    USB_int_SendSetupSetAddress(tUSBHost *Host, int Address);
20  int    USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest);
21 char    *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index);
22  int    _UTF16to8(Uint16 *Input, int InputLen, char *Dest);
23
24 // === CODE ===
25 void *USB_int_Request(tUSBDevice *Device, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data)
26 {
27         tUSBHost        *Host = Device->Host;
28         void    *hdl;
29         // TODO: Sanity check (and check that Type is valid)
30         struct sDeviceRequest   req;
31         tThread *thisthread = Proc_GetCurThread();
32         void *dest_hdl;
33
34         ENTER("pDevice xEndPt iType iReq iVal iIndx iLen pData",
35                 Device, EndPt, Type, Req, Val, Indx, Len, Data);
36         
37         if( EndPt < 0 || EndPt >= 16 ) {
38                 LEAVE('n');
39                 return NULL;
40         }
41
42         dest_hdl = Device->EndpointHandles[EndPt];
43         if( !dest_hdl ) {
44                 dest_hdl = Host->HostDef->InitControl(Host->Ptr, Device->Address*16 + EndPt);
45                 Device->EndpointHandles[EndPt] = dest_hdl;
46         }
47         
48         req.ReqType = Type;
49         req.Request = Req;
50         req.Value = LittleEndian16( Val );
51         req.Index = LittleEndian16( Indx );
52         req.Length = LittleEndian16( Len );
53
54         Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
55
56         LOG("Send");
57         if( Type & 0x80 ) {
58                 // Inbound data
59                 hdl = Host->HostDef->SendControl(Host->Ptr, dest_hdl, USB_int_WakeThread, thisthread, 0,
60                         &req, sizeof(req),
61                         NULL, 0,
62                         Data, Len
63                         );
64         }
65         else {
66                 // Outbound data
67                 hdl = Host->HostDef->SendControl(Host->Ptr, dest_hdl, USB_int_WakeThread, thisthread, 1,
68                         &req, sizeof(req),
69                         Data, Len,
70                         NULL, 0
71                         );
72         }
73         LOG("Wait...");
74         Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
75
76         LEAVE('p', hdl);
77         return hdl;
78 }
79
80 void USB_int_WakeThread(void *Thread, void *Data, size_t Length)
81 {
82         Threads_PostEvent(Thread, THREAD_EVENT_SHORTWAIT);
83 }
84
85 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
86 {
87         USB_int_Request(&Host->RootHubDev, 0, 0x00, 5, Address & 0x7F, 0, 0, NULL);
88         return 0;
89 }
90
91 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest)
92 {
93         struct sDeviceRequest   req;
94         void    *dest_hdl;
95         
96         dest_hdl = Dev->EndpointHandles[Endpoint];
97         if( !dest_hdl ) {
98                 dest_hdl = Dev->Host->HostDef->InitControl(Dev->Host->Ptr, Dev->Address*16 + Endpoint);
99                 Dev->EndpointHandles[Endpoint] = dest_hdl;
100         }
101
102         ENTER("pDev xEndpoint iType iIndex iLength pDest",
103                 Dev, Endpoint, Type, Index, Length, Dest);
104
105         req.ReqType = 0x80;
106         req.ReqType |= ((Type >> 8) & 0x3) << 5;        // Bits 5/6
107         req.ReqType |= (Type >> 12) & 3;        // Destination (Device, Interface, Endpoint, Other);
108
109         req.Request = 6;        // GET_DESCRIPTOR
110         req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
111         req.Index = LittleEndian16( 0 );        // TODO: Language ID / Interface
112         req.Length = LittleEndian16( Length );
113
114         Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
115         
116         LOG("Send");
117         Dev->Host->HostDef->SendControl(Dev->Host->Ptr, dest_hdl, USB_int_WakeThread, Proc_GetCurThread(), 0,
118                 &req, sizeof(req),
119                 NULL, 0,
120                 Dest, Length
121                 );
122
123         LOG("Waiting");
124         // TODO: Detect errors?
125         Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
126         
127         LEAVE('i', 0);
128         return 0;
129 }
130
131 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
132 {
133         struct sDescriptor_String       str;
134          int    src_len, new_len;
135         char    *ret;
136
137         if(Index == 0)  return strdup("");
138         
139         str.Length = 0;
140         USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
141         if(str.Length == 0)     return NULL;
142         if(str.Length < 2) {
143                 Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
144                         Dev->Host, Dev->Address, Endpoint, Index, str.Length);
145                 return NULL;
146         }
147 //      if(str.Length > sizeof(str)) {
148 //              // IMPOSSIBLE!
149 //              Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
150 //                      str.Length, sizeof(str)
151 //                      );
152 //      }
153         src_len = (str.Length - 2) / sizeof(str.Data[0]);
154
155         LOG("&str = %p, src_len = %i", &str, src_len);
156
157         new_len = _UTF16to8(str.Data, src_len, NULL);   
158         ret = malloc( new_len + 1 );
159         _UTF16to8(str.Data, src_len, ret);
160         ret[new_len] = 0;
161         return ret;
162 }
163
164 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
165 {
166          int    str_len, cp_len;
167         Uint32  saved_bits = 0;
168         str_len = 0;
169         for( int i = 0; i < InputLen; i ++)
170         {
171                 Uint32  cp;
172                 Uint16  val = Input[i];
173                 if( val >= 0xD800 && val <= 0xDBFF )
174                 {
175                         // Multibyte - Leading
176                         if(i + 1 > InputLen) {
177                                 cp = '?';
178                         }
179                         else {
180                                 saved_bits = (val - 0xD800) << 10;
181                                 saved_bits += 0x10000;
182                                 continue ;
183                         }
184                 }
185                 else if( val >= 0xDC00 && val <= 0xDFFF )
186                 {
187                         if( !saved_bits ) {
188                                 cp = '?';
189                         }
190                         else {
191                                 saved_bits |= (val - 0xDC00);
192                                 cp = saved_bits;
193                         }
194                 }
195                 else
196                         cp = val;
197
198                 cp_len = WriteUTF8((Uint8*)Dest, cp);
199                 if(Dest)
200                         Dest += cp_len;
201                 str_len += cp_len;
202
203                 saved_bits = 0;
204         }
205         
206         return str_len;
207 }
208

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