4c0fb73040ab249391249ff1fc3bc815648e5b4e
[tpg/acess2.git] / 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, &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, Data, Len);
42
43                 hdl2 = Host->HostDef->SendOUT(Host->Ptr, Addr, EndPt, 0, 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, 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, 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         req.Request = 6;        // GET_DESCRIPTOR
79         req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
80         req.Index = LittleEndian16( 0 );        // TODO: Language ID
81         req.Length = LittleEndian16( Length );
82         
83         Dev->Host->HostDef->SendSETUP(
84                 Dev->Host->Ptr, Dev->Address, Endpoint,
85                 0, NULL,
86                 &req, sizeof(req)
87                 );
88         
89         bToggle = 1;
90         while( Length > ciMaxPacketSize )
91         {
92                 Dev->Host->HostDef->SendIN(
93                         Dev->Host->Ptr, Dev->Address, Endpoint,
94                         bToggle, NULL,
95                         Dest, ciMaxPacketSize
96                         );
97                 bToggle = !bToggle;
98                 Length -= ciMaxPacketSize;
99         }
100
101         final = Dev->Host->HostDef->SendIN(
102                 Dev->Host->Ptr, Dev->Address, Endpoint,
103                 bToggle, INVLPTR,
104                 Dest, Length
105                 );
106
107         while( Dev->Host->HostDef->IsOpComplete(Dev->Host->Ptr, final) == 0 )
108                 Time_Delay(1);
109
110         return 0;
111 }
112
113 char *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index)
114 {
115         struct sDescriptor_String       str;
116          int    src_len, new_len;
117         char    *ret;
118         
119         USB_int_ReadDescriptor(Dev, Endpoint, 3, Index, sizeof(str), &str);
120 //      if(str.Length > sizeof(str)) {
121 //              // IMPOSSIBLE!
122 //              Log_Error("USB", "String is %i bytes, which is over prealloc size (%i)",
123 //                      str.Length, sizeof(str)
124 //                      );
125 //      }
126         src_len = (str.Length - 2) / sizeof(str.Data[0]);
127
128         new_len = _UTF16to8(str.Data, src_len, NULL);   
129         ret = malloc( new_len + 1 );
130         _UTF16to8(str.Data, src_len, ret);
131         ret[new_len] = 0;
132         return ret;
133 }
134
135 int _UTF16to8(Uint16 *Input, int InputLen, char *Dest)
136 {
137          int    str_len, cp_len;
138         Uint32  saved_bits = 0;
139         str_len = 0;
140         for( int i = 0; i < InputLen; i ++)
141         {
142                 Uint32  cp;
143                 Uint16  val = Input[i];
144                 if( val >= 0xD800 && val <= 0xDBFF )
145                 {
146                         // Multibyte - Leading
147                         if(i + 1 > InputLen) {
148                                 cp = '?';
149                         }
150                         else {
151                                 saved_bits = (val - 0xD800) << 10;
152                                 saved_bits += 0x10000;
153                                 continue ;
154                         }
155                 }
156                 else if( val >= 0xDC00 && val <= 0xDFFF )
157                 {
158                         if( !saved_bits ) {
159                                 cp = '?';
160                         }
161                         else {
162                                 saved_bits |= (val - 0xDC00);
163                                 cp = saved_bits;
164                         }
165                 }
166                 else
167                         cp = val;
168
169                 cp_len = WriteUTF8((Uint8*)Dest, cp);
170                 if(Dest)
171                         Dest += cp_len;
172                 str_len += cp_len;
173
174                 saved_bits = 0;
175         }
176         
177         return str_len;
178 }
179

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