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

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