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

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