/
endpoint0.xc
393 lines (330 loc) · 14.7 KB
/
endpoint0.xc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright (c) 2011, XMOS Ltd, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#include <xs1.h>
#include <print.h>
#include "xud.h"
#include "usb.h"
#include "hid.h"
#include "DescriptorRequests.h"
// This devices Device Descriptor:
static unsigned char hiSpdDesc[] = {
0x12, /* 0 bLength */
0x01, /* 1 bdescriptorType */
0x00, /* 2 bcdUSB */
0x02, /* 3 bcdUSB */
0x02, /* 4 bDeviceClass */
0x00, /* 5 bDeviceSubClass */ // ?? 6
0x00, /* 6 bDeviceProtocol */
0x40, /* 7 bMaxPacketSize */
0xb1, /* 8 idVendor */
0x20, /* 9 idVendor */
0x01, /* 10 idProduct */
0x04, /* 11 idProduct */
0x00, /* 12 bcdDevice */
0x02, /* 13 bcdDevice */
0x01, /* 14 iManufacturer */
0x02, /* 15 iProduct */
0x00, /* 16 iSerialNumber */
0x01 /* 17 bNumConfigurations */
};
unsigned char fullSpdDesc[] =
{
0x0a, /* 0 bLength */
DEVICE_QUALIFIER, /* 1 bDescriptorType */
0x00, /* 2 bcdUSB */
0x02, /* 3 bcdUSB */
0x02, /* 4 bDeviceClass */
0x0C, /* 5 bDeviceSubClass */
0x00, /* 6 bDeviceProtocol */
0x40, /* 7 bMaxPacketSize */
0x01, /* 8 bNumConfigurations */
0x00 /* 9 bReserved */
};
static unsigned char hiSpdConfDesc[] = {
0x09, /* 0 bLength */
0x02, /* 1 bDescriptortype */
0x20, 0x00, /* 2 wTotalLength */
0x01, /* 4 bNumInterfaces */
0x01, /* 5 bConfigurationValue */
0x04, /* 6 iConfiguration */
0x80, /* 7 bmAttributes */
0xC8, /* 8 bMaxPower */
0x09, /* 0 bLength */
0x04, /* 1 bDescriptorType */
0x00, /* 2 bInterfacecNumber */
0x00, /* 3 bAlternateSetting */
0x02, /* 4: bNumEndpoints */
0x02, /* 5: bInterfaceClass */
0x0C, /* 6: bInterfaceSubClass */
0x07, /* 7: bInterfaceProtocol*/
0x00, /* 8 iInterface */
0x07, /* 0 bLength */
0x05, /* 1 bDescriptorType */
0x81, /* 2 bEndpointAddress */
0x03, /* 3 bmAttributes */
0x00, /* 4 wMaxPacketSize */
0x02, /* 5 wMaxPacketSize */
0x01, /* 6 bInterval */
0x07, /* 0 bLength */
0x05, /* 1 bDescriptorType */
0x82, /* 2 bEndpointAddress */
0x02, /* 3 bmAttributes */
0x00, /* 4 wMaxPacketSize */
0x02, /* 5 wMaxPacketSize */
0x01 /* 6 bInterval */
};
unsigned char fullSpdConfDesc[] =
{
0x09, /* 0 bLength */
OTHER_SPEED_CONFIGURATION, /* 1 bDescriptorType */
0x12, /* 2 wTotalLength */
0x00, /* 3 wTotalLength */
0x01, /* 4 bNumInterface: Number of interfaces*/
0x00, /* 5 bConfigurationValue */
0x00, /* 6 iConfiguration */
0x80, /* 7 bmAttributes */
0xC8, /* 8 bMaxPower */
0x09, /* 0 bLength */
0x04, /* 1 bDescriptorType */
0x00, /* 2 bInterfaceNumber */
0x00, /* 3 bAlternateSetting */
0x00, /* 4 bNumEndpoints */
0x00, /* 5 bInterfaceClass */
0x00, /* 6 bInterfaceSubclass */
0x00, /* 7 bInterfaceProtocol */
0x00, /* 8 iInterface */
};
static unsigned char stringDescriptors[][40] = {
"\009\004", // Language string
"XMOS", // iManufacturer
"XMEth", // iProduct
"002030112233", // MAC address
"Config" // iConfiguration
};
extern int min(int a, int b);
int ControlInterfaceClassRequests(XUD_ep c_ep0_out, XUD_ep c_ep0_in, SetupPacket sp)
{
unsigned char buffer[64];
// Recipient: Interface
// NOTE: CLASS SPECIFIC REQUESTS
switch(sp.bRequest )
{
case 0x43:
// TODO
return XUD_SetBuffer_ResetPid(c_ep0_in, buffer, 0, PIDn_DATA1);
break;
default:
/* Error case */
break;
}
return 0;
}
int g_epStatusOut[2];
int g_epStatusIn[2];
void SetEndpointStatus(unsigned epNum, unsigned status)
{
/* Inspect for IN bit */
if( epNum & 0x80 )
{
epNum &= 0x7f;
/* Range check */
if(epNum < 2)
{
g_epStatusIn[ epNum & 0x7F ] = status;
}
}
else
{
if(epNum < 2)
{
g_epStatusOut[ epNum ] = status;
}
}
}
void Endpoint0( chanend chan_ep0_out, chanend chan_ep0_in)
{
unsigned char buffer[1024];
SetupPacket sp;
unsigned int current_config = 0;
XUD_ep c_ep0_out = XUD_Init_Ep(chan_ep0_out);
XUD_ep c_ep0_in = XUD_Init_Ep(chan_ep0_in);
while(1)
{
/* Do standard enumeration requests */
int retVal = 0;
retVal = DescriptorRequests(c_ep0_out, c_ep0_in, hiSpdDesc, sizeof(hiSpdDesc),
hiSpdConfDesc, sizeof(hiSpdConfDesc), fullSpdDesc, sizeof(fullSpdDesc),
fullSpdConfDesc, sizeof(fullSpdConfDesc), stringDescriptors, sp);
if (retVal)
{
#if 0
if (sp.bRequest != 5 && sp.bRequest != 9) {
printintln(sp.bmRequestType.Type);
printintln(sp.bmRequestType.Recipient);
printintln(sp.bRequest);
while(1) ;
}
#endif
/* Request not covered by XUD_DoEnumReqs() so decode ourselves */
switch(sp.bmRequestType.Type)
{
/* Class request */
case BM_REQTYPE_TYPE_CLASS:
switch(sp.bmRequestType.Recipient)
{
case BM_REQTYPE_RECIP_INTER:
/* Inspect for Control interface num */
if(sp.wIndex == 0)
{
ControlInterfaceClassRequests(c_ep0_out, c_ep0_in, sp);
}
break;
}
break;
case BM_REQTYPE_TYPE_STANDARD:
switch(sp.bmRequestType.Recipient)
{
case BM_REQTYPE_RECIP_EP:
/* Standard endpoint requests */
switch ( sp.bRequest )
{
/* ClearFeature */
case CLEAR_FEATURE:
switch ( sp.wValue )
{
case ENDPOINT_HALT:
/* Mark the endpoint status */
SetEndpointStatus(sp.wIndex, 0);
/* No data stage for this request, just do status stage */
retVal = XUD_DoSetRequestStatus(c_ep0_in, 0);
break;
default:
XUD_Error( "Unknown request in Endpoint ClearFeature" );
break;
}
break; /* B_REQ_CLRFEAR */
/* SetFeature */
case SET_FEATURE:
switch( sp.wValue )
{
case ENDPOINT_HALT:
/* Check request is in range */
SetEndpointStatus(sp.wIndex, 1);
break;
default:
XUD_Error("Unknown feature in SetFeature Request");
break;
}
retVal = XUD_DoSetRequestStatus(c_ep0_in, 0);
break;
/* Endpoint GetStatus Request */
case GET_STATUS:
buffer[0] = 0;
buffer[1] = 0;
if( sp.wIndex & 0x80 )
{
/* IN Endpoint */
if((sp.wIndex&0x7f) < 2) {
buffer[0] = ( g_epStatusIn[ sp.wIndex & 0x7F ] & 0xff );
buffer[1] = ( g_epStatusIn[ sp.wIndex & 0x7F ] >> 8 );
}
}
else
{
/* OUT Endpoint */
if(sp.wIndex < 2)
{
buffer[0] = ( g_epStatusOut[ sp.wIndex ] & 0xff );
buffer[1] = ( g_epStatusOut[ sp.wIndex ] >> 8 );
}
}
retVal = XUD_DoGetRequest(c_ep0_out, c_ep0_in, buffer, 2, sp.wLength);
break;
default:
//printstrln("Unknown Standard Endpoint Request");
break;
}
break;
case BM_REQTYPE_RECIP_INTER:
switch(sp.bRequest)
{
/* Set Interface */
case SET_INTERFACE:
/* TODO: Set the interface */
/* No data stage for this request, just do data stage */
XUD_DoSetRequestStatus(c_ep0_in, 0);
break;
case GET_INTERFACE:
buffer[0] = 0;
XUD_DoGetRequest(c_ep0_out, c_ep0_in, buffer,1, sp.wLength );
break;
case GET_STATUS:
buffer[0] = 0;
buffer[1] = 0;
XUD_DoGetRequest(c_ep0_out, c_ep0_in, buffer, 2, sp.wLength);
break;
case GET_DESCRIPTOR:
break;
}
break;
/* Recipient: Device */
case BM_REQTYPE_RECIP_DEV:
/* Standard Device requests (8) */
switch( sp.bRequest )
{
/* TODO We could check direction to be double safe */
/* Standard request: SetConfiguration */
case SET_CONFIGURATION:
/* Set the config */
current_config = sp.wValue;
/* No data stage for this request, just do status stage */
XUD_DoSetRequestStatus(c_ep0_in, 0);
break;
case GET_CONFIGURATION:
buffer[0] = (char)current_config;
XUD_DoGetRequest(c_ep0_out, c_ep0_in, buffer, 1, sp.wLength);
break;
case GET_STATUS:
buffer[0] = 0;
buffer[1] = 0;
if (hiSpdConfDesc[7] & 0x40)
buffer[0] = 0x1;
XUD_DoGetRequest(c_ep0_out, c_ep0_in, buffer, 2, sp.wLength);
break;
case SET_ADDRESS:
/* Status stage: Send a zero length packet */
retVal = XUD_SetBuffer_ResetPid(c_ep0_in, buffer, 0, PIDn_DATA1);
/* We should wait until ACK is received for status stage before changing address */
{
timer t;
unsigned time;
t :> time;
t when timerafter(time+50000) :> void;
}
/* Set device address in XUD */
XUD_SetDevAddr(sp.wValue);
break;
default:
//XUD_Error("Unknown device request");
break;
}
break;
default:
/* Got a request to a recipient we didn't recognise... */
//XUD_Error("Unknown Recipient");
break;
}
break;
default:
/* Error */
break;
}
} /* if XUD_DoEnumReqs() */
if (retVal == -1)
{
XUD_ResetEndpoint(c_ep0_out, c_ep0_in);
}
}
}