Modules/USB - Slight correctness fixes, and fixed a race condition
[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   1
9 #include <acess.h>
10 #include "usb.h"
11 #include "usb_proto.h"
12 #include "usb_lowlevel.h"
13 #include <timers.h>
14
15 // === PROTOTYPES ===
16 void    *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data);
17  int    USB_int_SendSetupSetAddress(tUSBHost *Host, int Address);
18  int    USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest);
19 char    *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index);
20  int    _UTF16to8(Uint16 *Input, int InputLen, char *Dest);
21
22 // === CODE ===
23 void *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data)
24 {
25         void    *hdl;
26         // TODO: Sanity check (and check that Type is valid)
27         struct sDeviceRequest   req;
28          int    dest = Addr * 16 + EndPt;       // TODO: Validate
29         
30         ENTER("pHost xdest iType iReq iVal iIndx iLen pData",
31                 Host, dest, Type, Req, Val, Indx, Len, Data);
32         
33         req.ReqType = Type;
34         req.Request = Req;
35         req.Value = LittleEndian16( Val );
36         req.Index = LittleEndian16( Indx );
37         req.Length = LittleEndian16( Len );
38
39         LOG("SETUP");   
40         hdl = Host->HostDef->SendSETUP(Host->Ptr, dest, 0, NULL, NULL, &req, sizeof(req));
41
42         // TODO: Data toggle?
43         // TODO: Multi-packet transfers
44         if( Len > 0 )
45         {
46                 if( Type & 0x80 )
47                 {
48                         LOG("IN");
49                         hdl = Host->HostDef->SendIN(Host->Ptr, dest, 1, NULL, NULL, Data, Len);
50         
51                         LOG("OUT (Status)");
52                         hdl = Host->HostDef->SendOUT(Host->Ptr, dest, 1, INVLPTR, NULL, NULL, 0);
53                 }
54                 else
55                 {
56                         LOG("OUT");
57                         Host->HostDef->SendOUT(Host->Ptr, dest, 1, NULL, NULL, Data, Len);
58                         
59                         // Status phase (DataToggle=1)
60                         LOG("IN (Status)");
61                         hdl = Host->HostDef->SendIN(Host->Ptr, dest, 1, INVLPTR, NULL, NULL, 0);
62                 }
63         }
64         else
65         {
66                 // Zero length, IN status
67                 LOG("IN (Status)");
68                 hdl = Host->HostDef->SendIN(Host->Ptr, dest, 1, INVLPTR, NULL, NULL, 0);
69         }
70         LOG("Wait...");
71         while( Host->HostDef->IsOpComplete(Host->Ptr, hdl) == 0 )
72                 Time_Delay(1);
73         LEAVE('p', hdl);
74         return hdl;
75 }
76
77 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
78 {
79         USB_int_Request(Host, 0, 0, 0x00, 5, Address & 0x7F, 0, 0, NULL);
80         return 0;
81 }
82
83 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest)
84 {
85         const int       ciMaxPacketSize = 0x400;
86         struct sDeviceRequest   req;
87          int    bToggle = 0;
88         void    *final;
89          int    dest = Dev->Address*16 + Endpoint;
90
91         ENTER("pDev xdest iType iIndex iLength pDest",
92                 Dev, dest, Type, Index, Length, Dest);
93
94         req.ReqType = 0x80;
95         req.ReqType |= ((Type >> 8) & 0x3) << 5;        // Bits 5/6
96         req.ReqType |= (Type >> 12) & 3;        // Destination (Device, Interface, Endpoint, Other);
97
98         req.Request = 6;        // GET_DESCRIPTOR
99         req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
100         req.Index = LittleEndian16( 0 );        // TODO: Language ID / Interface
101         req.Length = LittleEndian16( Length );
102
103         LOG("SETUP");   
104         Dev->Host->HostDef->SendSETUP(
105                 Dev->Host->Ptr, dest,
106                 0, NULL, NULL,
107                 &req, sizeof(req)
108                 );
109         
110         bToggle = 1;
111         while( Length > ciMaxPacketSize )
112         {
113                 LOG("IN (%i rem)", Length - ciMaxPacketSize);
114                 Dev->Host->HostDef->SendIN(
115                         Dev->Host->Ptr, dest,
116                         bToggle, NULL, NULL,
117                         Dest, ciMaxPacketSize
118                         );
119                 bToggle = !bToggle;
120                 Length -= ciMaxPacketSize;
121         }
122
123         LOG("IN (final)");
124         Dev->Host->HostDef->SendIN(
125                 Dev->Host->Ptr, dest,
126                 bToggle, NULL, NULL,
127                 Dest, Length
128                 );
129
130         LOG("OUT (Status)");
131         final = Dev->Host->HostDef->SendOUT(Dev->Host->Ptr, dest, 1, INVLPTR, NULL, NULL, 0);
132
133         LOG("Waiting");
134         while( Dev->Host->HostDef->IsOpComplete(Dev->Host->Ptr, final) == 0 )
135                 Threads_Yield();        // BAD BAD BAD
136
137         LEAVE('i', 0);
138         return 0;
139 }
140
141 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
142 {
143         struct sDescriptor_String       str;
144          int    src_len, new_len;
145         char    *ret;
146
147         if(Index == 0)  return strdup("");
148         
149         USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
150         if(str.Length < 2) {
151                 Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
152                         Dev->Host, Dev->Address, Endpoint, Index, str.Length);
153                 return NULL;
154         }
155 //      if(str.Length > sizeof(str)) {
156 //              // IMPOSSIBLE!
157 //              Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
158 //                      str.Length, sizeof(str)
159 //                      );
160 //      }
161         src_len = (str.Length - 2) / sizeof(str.Data[0]);
162
163         LOG("&str = %p, src_len = %i", &str, src_len);
164
165         new_len = _UTF16to8(str.Data, src_len, NULL);   
166         ret = malloc( new_len + 1 );
167         _UTF16to8(str.Data, src_len, ret);
168         ret[new_len] = 0;
169         return ret;
170 }
171
172 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
173 {
174          int    str_len, cp_len;
175         Uint32  saved_bits = 0;
176         str_len = 0;
177         for( int i = 0; i < InputLen; i ++)
178         {
179                 Uint32  cp;
180                 Uint16  val = Input[i];
181                 if( val >= 0xD800 && val <= 0xDBFF )
182                 {
183                         // Multibyte - Leading
184                         if(i + 1 > InputLen) {
185                                 cp = '?';
186                         }
187                         else {
188                                 saved_bits = (val - 0xD800) << 10;
189                                 saved_bits += 0x10000;
190                                 continue ;
191                         }
192                 }
193                 else if( val >= 0xDC00 && val <= 0xDFFF )
194                 {
195                         if( !saved_bits ) {
196                                 cp = '?';
197                         }
198                         else {
199                                 saved_bits |= (val - 0xDC00);
200                                 cp = saved_bits;
201                         }
202                 }
203                 else
204                         cp = val;
205
206                 cp_len = WriteUTF8((Uint8*)Dest, cp);
207                 if(Dest)
208                         Dest += cp_len;
209                 str_len += cp_len;
210
211                 saved_bits = 0;
212         }
213         
214         return str_len;
215 }
216

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