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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
/**
**************************************************************************
* @file usbd_sdr.c
* @brief usb standard device request
**************************************************************************
* Copyright notice & Disclaimer
*
* The software Board Support Package (BSP) that is made available to
* download from Artery official website is the copyrighted work of Artery.
* Artery authorizes customers to use, copy, and distribute the BSP
* software and its related documentation for the purpose of design and
* development in conjunction with Artery microcontrollers. Use of the
* software is governed by this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
*
**************************************************************************
*/
#include "usbd_sdr.h"
/** @defgroup USBD_drivers_standard_request
* @brief usb device standard_request
* @{
*/
/** @defgroup USBD_sdr_private_functions
* @{
*/
static usb_sts_type usbd_get_descriptor(usbd_core_type *udev);
static usb_sts_type usbd_set_address(usbd_core_type *udev);
static usb_sts_type usbd_get_status(usbd_core_type *udev);
static usb_sts_type usbd_clear_feature(usbd_core_type *udev);
static usb_sts_type usbd_set_feature(usbd_core_type *udev);
static usb_sts_type usbd_get_configuration(usbd_core_type *udev);
static usb_sts_type usbd_set_configuration(usbd_core_type *udev);
/**
* @brief usb parse standard setup request
* @param setup: setup structure
* @param buf: setup buffer
* @retval none
*/
void usbd_setup_request_parse(usb_setup_type *setup, uint8_t *buf)
{
setup->bmRequestType = *(uint8_t *) buf;
setup->bRequest = *(uint8_t *) (buf + 1);
setup->wValue = SWAPBYTE(buf + 2);
setup->wIndex = SWAPBYTE(buf + 4);
setup->wLength = SWAPBYTE(buf + 6);
}
/**
* @brief get usb standard device description request
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_get_descriptor(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
uint16_t len = 0;
uint8_t desc_type = udev->setup.wValue >> 8;
usbd_desc_t *desc = NULL, usbd_desc =
{
.length = 0xA,
.descriptor = NULL,
};
switch(desc_type)
{
case USB_DESCIPTOR_TYPE_DEVICE:
case USB_DESCIPTOR_TYPE_CONFIGURATION:
desc = &usbd_desc;
break;
case USB_DESCIPTOR_TYPE_STRING:
{
uint8_t str_desc = (uint8_t)udev->setup.wValue;
switch(str_desc)
{
case USB_LANGID_STRING:
case USB_MFC_STRING:
case USB_PRODUCT_STRING:
case USB_SERIAL_STRING:
case USB_CONFIG_STRING:
case USB_INTERFACE_STRING:
desc = &usbd_desc;
break;
default:
usbd_ctrl_send(udev, desc->descriptor, len);
return ret;
}
break;
}
case USB_DESCIPTOR_TYPE_DEVICE_QUALIFIER:
usbd_ctrl_unsupport(udev);
break;
case USB_DESCIPTOR_TYPE_OTHER_SPEED:
usbd_ctrl_unsupport(udev);
return ret;
default:
usbd_ctrl_unsupport(udev);
return ret;
}
if(desc != NULL)
{
if((desc->length != 0) && (udev->setup.wLength != 0))
{
len = MIN(desc->length , udev->setup.wLength);
usbd_ctrl_send(udev, desc->descriptor, len);
}
}
return ret;
}
/**
* @brief this request sets the device address
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_set_address(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
uint8_t dev_addr;
/* if wIndex or wLength are non-zero, then the behavior of
the device is not specified
*/
if(setup->wIndex == 0 && setup->wLength == 0)
{
dev_addr = (uint8_t)(setup->wValue) & 0x7f;
/* device behavior when this request is received
while the device is in the configured state is not specified.*/
if(udev->conn_state == USB_CONN_STATE_CONFIGURED )
{
usbd_ctrl_unsupport(udev);
}
else
{
udev->device_addr = dev_addr;
if(dev_addr != 0)
{
udev->conn_state = USB_CONN_STATE_ADDRESSED;
}
else
{
udev->conn_state = USB_CONN_STATE_DEFAULT;
}
usbd_ctrl_send_status(udev);
}
}
else
{
usbd_ctrl_unsupport(udev);
}
return ret;
}
/**
* @brief get usb status request
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_get_status(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
case USB_CONN_STATE_CONFIGURED:
if(udev->remote_wakup)
{
udev->config_status |= USB_CONF_REMOTE_WAKEUP;
}
usbd_ctrl_send(udev, (uint8_t *)(&udev->config_status), 2);
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
return ret;
}
/**
* @brief clear usb feature request
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_clear_feature(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
case USB_CONN_STATE_CONFIGURED:
if(setup->wValue == USB_FEATURE_REMOTE_WAKEUP)
{
udev->remote_wakup = 0;
udev->config_status &= ~USB_CONF_REMOTE_WAKEUP;
usbd_ctrl_send_status(udev);
}
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
return ret;
}
/**
* @brief set usb feature request
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_set_feature(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
if(setup->wValue == USB_FEATURE_REMOTE_WAKEUP)
{
udev->remote_wakup = 1;
usbd_ctrl_send_status(udev);
}
return ret;
}
/**
* @brief get usb configuration request
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_get_configuration(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
if(setup->wLength != 1)
{
usbd_ctrl_unsupport(udev);
}
else
{
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
udev->default_config = 0;
usbd_ctrl_send(udev, (uint8_t *)(&udev->default_config), 1);
break;
case USB_CONN_STATE_CONFIGURED:
usbd_ctrl_send(udev, (uint8_t *)(&udev->dev_config), 1);
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
}
return ret;
}
/**
* @brief sets the usb device configuration request
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_set_configuration(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
static uint8_t config_value;
usb_setup_type *setup = &udev->setup;
config_value = (uint8_t)setup->wValue;
if(setup->wIndex == 0 && setup->wLength == 0)
{
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
if(config_value)
{
udev->dev_config = config_value;
udev->conn_state = USB_CONN_STATE_CONFIGURED;
usbd_ctrl_send_status(udev);
}
else
{
usbd_ctrl_send_status(udev);
}
break;
case USB_CONN_STATE_CONFIGURED:
if(config_value == 0)
{
udev->conn_state = USB_CONN_STATE_ADDRESSED;
udev->dev_config = config_value;
usbd_ctrl_send_status(udev);
}
else if(config_value == udev->dev_config)
{
udev->dev_config = config_value;
usbd_ctrl_send_status(udev);
}
else
{
usbd_ctrl_send_status(udev);
}
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
}
else
{
usbd_ctrl_unsupport(udev);
}
return ret;
}
/**
* @brief standard usb device requests
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type usbd_device_request(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
if((setup->bmRequestType & USB_REQ_TYPE_RESERVED) != USB_REQ_TYPE_STANDARD)
{
udev->class_handler->setup_handler(udev, &udev->setup);
return ret;
}
switch(udev->setup.bRequest)
{
case USB_STD_REQ_GET_STATUS:
usbd_get_status(udev);
break;
case USB_STD_REQ_CLEAR_FEATURE:
usbd_clear_feature(udev);
break;
case USB_STD_REQ_SET_FEATURE:
usbd_set_feature(udev);
break;
case USB_STD_REQ_SET_ADDRESS:
usbd_set_address(udev);
break;
case USB_STD_REQ_GET_DESCRIPTOR:
usbd_get_descriptor(udev);
break;
case USB_STD_REQ_GET_CONFIGURATION:
usbd_get_configuration(udev);
break;
case USB_STD_REQ_SET_CONFIGURATION:
usbd_set_configuration(udev);
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
return ret;
}
/**
* @brief standard usb interface requests
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type usbd_interface_request(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
switch(udev->conn_state)
{
case USB_CONN_STATE_CONFIGURED:
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
case USB_REQ_TYPE_CLASS:
if(setup->wLength)
{
if(setup->bmRequestType & USB_REQ_DIR_DTH)
{
USB_SET_RXSTS((udev->ept_in[0].ept_address & 0x7F), USB_RX_VALID);
}
else
{
usbd_ept_send(udev, 0, 0, 0);
USB_SET_TXSTS((udev->ept_in[0].ept_address & 0x7F), USB_TX_VALID);
}
}
default:
break;
}
if(setup->wLength == 0)
{
usbd_ctrl_send_status(udev);
}
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
return ret;
}
/**
* @brief standard usb endpoint requests
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type usbd_endpoint_request(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
uint8_t ept_addr = LBYTE(setup->wIndex);
usb_ept_info *ept_info;
if((setup->bmRequestType & USB_REQ_TYPE_RESERVED) == USB_REQ_TYPE_CLASS)
{
udev->class_handler->setup_handler(udev, &udev->setup);
}
switch(setup->bRequest)
{
case USB_STD_REQ_GET_STATUS:
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
if((ept_addr & 0x7F) != 0)
{
usbd_set_stall(udev, ept_addr);
}
break;
case USB_CONN_STATE_CONFIGURED:
{
if((ept_addr & 0x80) != 0)
{
ept_info = &udev->ept_in[ept_addr & 0x7F];
}
else
{
ept_info = &udev->ept_out[ept_addr & 0x7F];
}
if(ept_info->stall == 1)
{
ept_info->status = 0x0001;
}
else
{
ept_info->status = 0x0000;
}
usbd_ctrl_send(udev, (uint8_t *)(&ept_info->status), 2);
}
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
break;
case USB_STD_REQ_CLEAR_FEATURE:
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
if((ept_addr != 0x00) && (ept_addr != 0x80))
{
usbd_set_stall(udev, ept_addr);
}
break;
case USB_CONN_STATE_CONFIGURED:
if(setup->wValue == USB_FEATURE_EPT_HALT)
{
if((ept_addr & 0x7F) != 0x00 )
{
usbd_clear_stall(udev, ept_addr);
udev->class_handler->setup_handler(udev, &udev->setup);
}
usbd_ctrl_send_status(udev);
}
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
break;
case USB_STD_REQ_SET_FEATURE:
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
if((ept_addr != 0x00) && (ept_addr != 0x80))
{
usbd_set_stall(udev, ept_addr);
}
break;
case USB_CONN_STATE_CONFIGURED:
if(setup->wValue == USB_FEATURE_EPT_HALT)
{
if((ept_addr != 0x00) && (ept_addr != 0x80))
{
usbd_set_stall(udev, ept_addr);
}
}
udev->class_handler->setup_handler(udev, &udev->setup);
usbd_ctrl_send_status(udev);
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
return ret;
}
/**
* @}
*/
/**
* @}
*/
|