Usermode/libc - Fix strchr and strrchr behavior
[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                 LEAVE('n');
45                 return NULL;
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                 return -1;
99         }
100
101         ENTER("pDev xEndpoint iType iIndex iLength pDest",
102                 Dev, Endpoint, Type, Index, Length, Dest);
103
104         req.ReqType = 0x80;
105         req.ReqType |= ((Type >> 8) & 0x3) << 5;        // Bits 5/6
106         req.ReqType |= (Type >> 12) & 3;        // Destination (Device, Interface, Endpoint, Other);
107
108         req.Request = 6;        // GET_DESCRIPTOR
109         req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
110         req.Index = LittleEndian16( 0 );        // TODO: Language ID / Interface
111         req.Length = LittleEndian16( Length );
112
113         Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
114         
115         LOG("Send");
116         Dev->Host->HostDef->SendControl(Dev->Host->Ptr, dest_hdl, USB_int_WakeThread, Proc_GetCurThread(), 0,
117                 &req, sizeof(req),
118                 NULL, 0,
119                 Dest, Length
120                 );
121
122         LOG("Waiting");
123         // TODO: Detect errors?
124         Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
125         
126         LEAVE('i', 0);
127         return 0;
128 }
129
130 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
131 {
132         struct sDescriptor_String       str;
133          int    src_len, new_len;
134         char    *ret;
135
136         if(Index == 0)  return strdup("");
137         
138         str.Length = 0;
139         USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
140         if(str.Length == 0)     return NULL;
141         if(str.Length < 2) {
142                 Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
143                         Dev->Host, Dev->Address, Endpoint, Index, str.Length);
144                 return NULL;
145         }
146 //      if(str.Length > sizeof(str)) {
147 //              // IMPOSSIBLE!
148 //              Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
149 //                      str.Length, sizeof(str)
150 //                      );
151 //      }
152         src_len = (str.Length - 2) / sizeof(str.Data[0]);
153
154         LOG("&str = %p, src_len = %i", &str, src_len);
155
156         new_len = _UTF16to8(str.Data, src_len, NULL);   
157         ret = malloc( new_len + 1 );
158         _UTF16to8(str.Data, src_len, ret);
159         ret[new_len] = 0;
160         return ret;
161 }
162
163 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
164 {
165          int    str_len, cp_len;
166         Uint32  saved_bits = 0;
167         str_len = 0;
168         for( int i = 0; i < InputLen; i ++)
169         {
170                 Uint32  cp;
171                 Uint16  val = Input[i];
172                 if( val >= 0xD800 && val <= 0xDBFF )
173                 {
174                         // Multibyte - Leading
175                         if(i + 1 > InputLen) {
176                                 cp = '?';
177                         }
178                         else {
179                                 saved_bits = (val - 0xD800) << 10;
180                                 saved_bits += 0x10000;
181                                 continue ;
182                         }
183                 }
184                 else if( val >= 0xDC00 && val <= 0xDFFF )
185                 {
186                         if( !saved_bits ) {
187                                 cp = '?';
188                         }
189                         else {
190                                 saved_bits |= (val - 0xDC00);
191                                 cp = saved_bits;
192                         }
193                 }
194                 else
195                         cp = val;
196
197                 cp_len = WriteUTF8((Uint8*)Dest, cp);
198                 if(Dest)
199                         Dest += cp_len;
200                 str_len += cp_len;
201
202                 saved_bits = 0;
203         }
204         
205         return str_len;
206 }
207

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