d8e1485af630e819fe2a9a55febc31b6d58b51bd
[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         req.ReqType = Type;
29         req.Request = Req;
30         req.Value = LittleEndian16( Val );
31         req.Index = LittleEndian16( Indx );
32         req.Length = LittleEndian16( Len );
33         
34         hdl = Host->HostDef->SendSETUP(Host->Ptr, Addr, EndPt, 0, NULL, NULL, &req, sizeof(req));
35
36         // TODO: Data toggle?
37         // TODO: Multi-packet transfers
38         if( Type & 0x80 )
39         {
40                 void    *hdl2;
41                 
42                 hdl = Host->HostDef->SendIN(Host->Ptr, Addr, EndPt, 0, NULL, NULL, Data, Len);
43
44                 hdl2 = Host->HostDef->SendOUT(Host->Ptr, Addr, EndPt, 0, NULL, NULL, NULL, 0);
45                 while( Host->HostDef->IsOpComplete(Host->Ptr, hdl2) == 0 )
46                         Time_Delay(1);
47         }
48         else
49         {
50                 void    *hdl2;
51                 
52                 if( Len > 0 )
53                         hdl = Host->HostDef->SendOUT(Host->Ptr, Addr, EndPt, 0, NULL, NULL, Data, Len);
54                 else
55                         hdl = NULL;
56                 
57                 // Status phase (DataToggle=1)
58                 hdl2 = Host->HostDef->SendIN(Host->Ptr, Addr, EndPt, 1, NULL, NULL, NULL, 0);
59                 while( Host->HostDef->IsOpComplete(Host->Ptr, hdl2) == 0 )
60                         Time_Delay(1);
61         }
62         return hdl;
63 }
64
65 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
66 {
67         USB_int_Request(Host, 0, 0, 0x00, 5, Address & 0x7F, 0, 0, NULL);
68         return 0;
69 }
70
71 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest)
72 {
73         const int       ciMaxPacketSize = 0x400;
74         struct sDeviceRequest   req;
75          int    bToggle = 0;
76         void    *final;
77
78         req.ReqType = 0x80;
79         switch( Type & 0xF00 )
80         {
81         case 0x000:     req.ReqType |= (0 << 5);        break;  // Standard
82         case 0x100:     req.ReqType |= (1 << 5);        break;  // Class
83         case 0x200:     req.ReqType |= (2 << 5);        break;  // Vendor
84         }
85
86         req.Request = 6;        // GET_DESCRIPTOR
87         req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
88         req.Index = LittleEndian16( 0 );        // TODO: Language ID
89         req.Length = LittleEndian16( Length );
90         
91         Dev->Host->HostDef->SendSETUP(
92                 Dev->Host->Ptr, Dev->Address, Endpoint,
93                 0, NULL, NULL,
94                 &req, sizeof(req)
95                 );
96         
97         bToggle = 1;
98         while( Length > ciMaxPacketSize )
99         {
100                 Dev->Host->HostDef->SendIN(
101                         Dev->Host->Ptr, Dev->Address, Endpoint,
102                         bToggle, NULL, NULL,
103                         Dest, ciMaxPacketSize
104                         );
105                 bToggle = !bToggle;
106                 Length -= ciMaxPacketSize;
107         }
108
109         final = Dev->Host->HostDef->SendIN(
110                 Dev->Host->Ptr, Dev->Address, Endpoint,
111                 bToggle, INVLPTR, NULL,
112                 Dest, Length
113                 );
114
115         while( Dev->Host->HostDef->IsOpComplete(Dev->Host->Ptr, final) == 0 )
116                 Threads_Yield();
117
118         return 0;
119 }
120
121 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
122 {
123         struct sDescriptor_String       str;
124          int    src_len, new_len;
125         char    *ret;
126
127         if(Index == 0)  return strdup("");
128         
129         USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
130         if(str.Length < 2) {
131                 Log_Error("USB", "String %p:%i:%i:%i descriptor is undersized (%i)",
132                         Dev->Host, Dev->Address, Endpoint, Index, str.Length);
133                 return NULL;
134         }
135 //      if(str.Length > sizeof(str)) {
136 //              // IMPOSSIBLE!
137 //              Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
138 //                      str.Length, sizeof(str)
139 //                      );
140 //      }
141         src_len = (str.Length - 2) / sizeof(str.Data[0]);
142
143         LOG("&str = %p, src_len = %i", &str, src_len);
144
145         new_len = _UTF16to8(str.Data, src_len, NULL);   
146         ret = malloc( new_len + 1 );
147         _UTF16to8(str.Data, src_len, ret);
148         ret[new_len] = 0;
149         return ret;
150 }
151
152 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
153 {
154          int    str_len, cp_len;
155         Uint32  saved_bits = 0;
156         str_len = 0;
157         for( int i = 0; i < InputLen; i ++)
158         {
159                 Uint32  cp;
160                 Uint16  val = Input[i];
161                 if( val >= 0xD800 && val <= 0xDBFF )
162                 {
163                         // Multibyte - Leading
164                         if(i + 1 > InputLen) {
165                                 cp = '?';
166                         }
167                         else {
168                                 saved_bits = (val - 0xD800) << 10;
169                                 saved_bits += 0x10000;
170                                 continue ;
171                         }
172                 }
173                 else if( val >= 0xDC00 && val <= 0xDFFF )
174                 {
175                         if( !saved_bits ) {
176                                 cp = '?';
177                         }
178                         else {
179                                 saved_bits |= (val - 0xDC00);
180                                 cp = saved_bits;
181                         }
182                 }
183                 else
184                         cp = val;
185
186                 cp_len = WriteUTF8((Uint8*)Dest, cp);
187                 if(Dest)
188                         Dest += cp_len;
189                 str_len += cp_len;
190
191                 saved_bits = 0;
192         }
193         
194         return str_len;
195 }
196

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