Kernel/USB - Still broken, reworking host controller API to give driver more information
[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 #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         void    *final;
99          int    dest = Dev->Address*16 + Endpoint;
100
101         ENTER("pDev xdest iType iIndex iLength pDest",
102                 Dev, dest, 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         LOG("SETUP");   
114         Dev->Host->HostDef->ControlSETUP(Dev->Host->Ptr, dest, 0, &req, sizeof(req));
115         
116         bToggle = 1;
117         while( Length > ciMaxPacketSize )
118         {
119                 LOG("IN (%i rem)", Length - ciMaxPacketSize);
120                 Dev->Host->HostDef->ControlIN(
121                         Dev->Host->Ptr, dest,
122                         bToggle, NULL, NULL,
123                         Dest, ciMaxPacketSize
124                         );
125                 bToggle = !bToggle;
126                 Length -= ciMaxPacketSize;
127         }
128
129         LOG("IN (final)");
130         Dev->Host->HostDef->ControlIN( Dev->Host->Ptr, dest, bToggle, NULL, NULL, Dest, Length );
131
132         Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
133         LOG("OUT (Status)");
134         final = Dev->Host->HostDef->ControlOUT(
135                 Dev->Host->Ptr, dest, 1,
136                 USB_int_WakeThread, Proc_GetCurThread(),
137                 NULL, 0
138                 );
139
140         LOG("Waiting");
141         Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
142
143         LEAVE('i', 0);
144         return 0;
145 }
146
147 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
148 {
149         struct sDescriptor_String       str;
150          int    src_len, new_len;
151         char    *ret;
152
153         if(Index == 0)  return strdup("");
154         
155         USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
156         if(str.Length < 2) {
157                 Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
158                         Dev->Host, Dev->Address, Endpoint, Index, str.Length);
159                 return NULL;
160         }
161 //      if(str.Length > sizeof(str)) {
162 //              // IMPOSSIBLE!
163 //              Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
164 //                      str.Length, sizeof(str)
165 //                      );
166 //      }
167         src_len = (str.Length - 2) / sizeof(str.Data[0]);
168
169         LOG("&str = %p, src_len = %i", &str, src_len);
170
171         new_len = _UTF16to8(str.Data, src_len, NULL);   
172         ret = malloc( new_len + 1 );
173         _UTF16to8(str.Data, src_len, ret);
174         ret[new_len] = 0;
175         return ret;
176 }
177
178 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
179 {
180          int    str_len, cp_len;
181         Uint32  saved_bits = 0;
182         str_len = 0;
183         for( int i = 0; i < InputLen; i ++)
184         {
185                 Uint32  cp;
186                 Uint16  val = Input[i];
187                 if( val >= 0xD800 && val <= 0xDBFF )
188                 {
189                         // Multibyte - Leading
190                         if(i + 1 > InputLen) {
191                                 cp = '?';
192                         }
193                         else {
194                                 saved_bits = (val - 0xD800) << 10;
195                                 saved_bits += 0x10000;
196                                 continue ;
197                         }
198                 }
199                 else if( val >= 0xDC00 && val <= 0xDFFF )
200                 {
201                         if( !saved_bits ) {
202                                 cp = '?';
203                         }
204                         else {
205                                 saved_bits |= (val - 0xDC00);
206                                 cp = saved_bits;
207                         }
208                 }
209                 else
210                         cp = val;
211
212                 cp_len = WriteUTF8((Uint8*)Dest, cp);
213                 if(Dest)
214                         Dest += cp_len;
215                 str_len += cp_len;
216
217                 saved_bits = 0;
218         }
219         
220         return str_len;
221 }
222

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