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

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