Upgrade to v0.92
[hackover2013-badge-firmware.git] / core / usbcdc / usbcore.c
1 /*----------------------------------------------------------------------------
2 * U S B - K e r n e l
3 *----------------------------------------------------------------------------
4 * Name: usbcore.c
5 * Purpose: USB Core Module
6 * Version: V1.20
7 *----------------------------------------------------------------------------
8 * This software is supplied "AS IS" without any warranties, express,
9 * implied or statutory, including but not limited to the implied
10 * warranties of fitness for purpose, satisfactory quality and
11 * noninfringement. Keil extends you a royalty-free right to reproduce
12 * and distribute executable files created using this software for use
13 * on NXP Semiconductors LPC microcontroller devices only. Nothing else
14 * gives you the right to use this software.
15 *
16 * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17 *----------------------------------------------------------------------------
18 * History:
19 * V1.20 Added vendor specific requests
20 * Changed string descriptor handling
21 * Reworked Endpoint0
22 * V1.00 Initial Version
23 *----------------------------------------------------------------------------*/
24 #include "projectconfig.h"
25
26 #include "usb.h"
27 #include "usbcfg.h"
28 #include "usbhw.h"
29 #include "usbcore.h"
30 #include "usbdesc.h"
31 #include "usbuser.h"
32
33 #if (USB_CLASS)
34
35 #if (USB_AUDIO)
36 #include "audio.h"
37 #include "adcuser.h"
38 #endif
39
40 #if (USB_HID)
41 #include "hid.h"
42 #include "hiduser.h"
43 #endif
44
45 #if (USB_MSC)
46 #include "msc.h"
47 #include "mscuser.h"
48 extern MSC_CSW CSW;
49 #endif
50
51 #if (USB_CDC)
52 #include "cdc.h"
53 #include "cdcuser.h"
54 #endif
55
56 #endif
57
58 #if (USB_VENDOR)
59 #include "vendor.h"
60 #endif
61
62 uint16_t USB_DeviceStatus;
63 uint8_t USB_DeviceAddress;
64 volatile uint8_t USB_Configuration;
65 uint32_t USB_EndPointMask;
66 uint32_t USB_EndPointHalt;
67 uint32_t USB_EndPointStall; /* EP must stay stalled */
68 uint8_t USB_NumInterfaces;
69 uint8_t USB_AltSetting[USB_IF_NUM];
70
71 uint8_t EP0Buf[USB_MAX_PACKET0];
72
73
74 USB_EP_DATA EP0Data;
75
76 USB_SETUP_PACKET SetupPacket;
77
78
79 /*
80 * Reset USB Core
81 * Parameters: None
82 * Return Value: None
83 */
84
85 void USB_ResetCore (void) {
86
87 USB_DeviceStatus = USB_POWER;
88 USB_DeviceAddress = 0;
89 USB_Configuration = 0;
90 USB_EndPointMask = 0x00010001;
91 USB_EndPointHalt = 0x00000000;
92 USB_EndPointStall = 0x00000000;
93 }
94
95
96 /*
97 * USB Request - Setup Stage
98 * Parameters: None (global SetupPacket)
99 * Return Value: None
100 */
101
102 void USB_SetupStage (void) {
103 USB_ReadEP(0x00, (uint8_t *)&SetupPacket);
104 }
105
106
107 /*
108 * USB Request - Data In Stage
109 * Parameters: None (global EP0Data)
110 * Return Value: None
111 */
112
113 void USB_DataInStage (void) {
114 uint32_t cnt;
115
116 if (EP0Data.Count > USB_MAX_PACKET0) {
117 cnt = USB_MAX_PACKET0;
118 } else {
119 cnt = EP0Data.Count;
120 }
121 cnt = USB_WriteEP(0x80, EP0Data.pData, cnt);
122 EP0Data.pData += cnt;
123 EP0Data.Count -= cnt;
124 }
125
126
127 /*
128 * USB Request - Data Out Stage
129 * Parameters: None (global EP0Data)
130 * Return Value: None
131 */
132
133 void USB_DataOutStage (void) {
134 uint32_t cnt;
135
136 cnt = USB_ReadEP(0x00, EP0Data.pData);
137 EP0Data.pData += cnt;
138 EP0Data.Count -= cnt;
139 }
140
141
142 /*
143 * USB Request - Status In Stage
144 * Parameters: None
145 * Return Value: None
146 */
147
148 void USB_StatusInStage (void) {
149 USB_WriteEP(0x80, NULL, 0);
150 }
151
152
153 /*
154 * USB Request - Status Out Stage
155 * Parameters: None
156 * Return Value: None
157 */
158
159 void USB_StatusOutStage (void) {
160 USB_ReadEP(0x00, EP0Buf);
161 }
162
163
164 /*
165 * Get Status USB Request
166 * Parameters: None (global SetupPacket)
167 * Return Value: TRUE - Success, FALSE - Error
168 */
169
170 static inline uint32_t USB_ReqGetStatus (void) {
171 uint32_t n, m;
172 uint16_t* ep0 = (uint16_t __attribute__((packed)) *)EP0Buf;
173
174 switch (SetupPacket.bmRequestType.BM.Recipient) {
175 case REQUEST_TO_DEVICE:
176 EP0Data.pData = (uint8_t *)&USB_DeviceStatus;
177 break;
178 case REQUEST_TO_INTERFACE:
179 if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
180 *ep0 = 0;
181 EP0Data.pData = EP0Buf;
182 } else {
183 return (FALSE);
184 }
185 break;
186 case REQUEST_TO_ENDPOINT:
187 n = SetupPacket.wIndex.WB.L & 0x8F;
188 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
189 if (((USB_Configuration != 0) || ((n & 0x0F) == 0)) && (USB_EndPointMask & m)) {
190 *ep0 = (USB_EndPointHalt & m) ? 1 : 0;
191 EP0Data.pData = EP0Buf;
192 } else {
193 return (FALSE);
194 }
195 break;
196 default:
197 return (FALSE);
198 }
199 return (TRUE);
200 }
201
202
203 /*
204 * Set/Clear Feature USB Request
205 * Parameters: sc: 0 - Clear, 1 - Set
206 * (global SetupPacket)
207 * Return Value: TRUE - Success, FALSE - Error
208 */
209
210 static inline uint32_t USB_ReqSetClrFeature (uint32_t sc) {
211 uint32_t n, m;
212
213 switch (SetupPacket.bmRequestType.BM.Recipient) {
214 case REQUEST_TO_DEVICE:
215 if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) {
216 if (sc) {
217 USB_WakeUpCfg(TRUE);
218 USB_DeviceStatus |= USB_GETSTATUS_REMOTE_WAKEUP;
219 } else {
220 USB_WakeUpCfg(FALSE);
221 USB_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP;
222 }
223 } else {
224 return (FALSE);
225 }
226 break;
227 case REQUEST_TO_INTERFACE:
228 return (FALSE);
229 case REQUEST_TO_ENDPOINT:
230 n = SetupPacket.wIndex.WB.L & 0x8F;
231 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
232 if ((USB_Configuration != 0) && ((n & 0x0F) != 0) && (USB_EndPointMask & m)) {
233 if (SetupPacket.wValue.W == USB_FEATURE_ENDPOINT_STALL) {
234 if (sc) {
235 USB_SetStallEP(n);
236 USB_EndPointHalt |= m;
237 } else {
238 if ((USB_EndPointStall & m) != 0) {
239 return (TRUE);
240 }
241 USB_ClrStallEP(n);
242 #if (USB_MSC)
243 if ((n == MSC_EP_IN) && ((USB_EndPointHalt & m) != 0)) {
244 /* Compliance Test: rewrite CSW after unstall */
245 if (CSW.dSignature == MSC_CSW_Signature) {
246 USB_WriteEP(MSC_EP_IN, (uint8_t *)&CSW, sizeof(CSW));
247 }
248 }
249 #endif
250 USB_EndPointHalt &= ~m;
251 }
252 } else {
253 return (FALSE);
254 }
255 } else {
256 return (FALSE);
257 }
258 break;
259 default:
260 return (FALSE);
261 }
262 return (TRUE);
263 }
264
265
266 /*
267 * Set Address USB Request
268 * Parameters: None (global SetupPacket)
269 * Return Value: TRUE - Success, FALSE - Error
270 */
271
272 static inline uint32_t USB_ReqSetAddress (void) {
273
274 switch (SetupPacket.bmRequestType.BM.Recipient) {
275 case REQUEST_TO_DEVICE:
276 USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
277 break;
278 default:
279 return (FALSE);
280 }
281 return (TRUE);
282 }
283
284
285 /*
286 * Get Descriptor USB Request
287 * Parameters: None (global SetupPacket)
288 * Return Value: TRUE - Success, FALSE - Error
289 */
290
291 static inline uint32_t USB_ReqGetDescriptor (void) {
292 uint8_t *pD;
293 uint32_t len, n;
294
295 switch (SetupPacket.bmRequestType.BM.Recipient) {
296 case REQUEST_TO_DEVICE:
297 switch (SetupPacket.wValue.WB.H) {
298 case USB_DEVICE_DESCRIPTOR_TYPE:
299 EP0Data.pData = (uint8_t *)USB_DeviceDescriptor;
300 len = USB_DEVICE_DESC_SIZE;
301 break;
302 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
303 pD = (uint8_t *)USB_ConfigDescriptor;
304 for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
305 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) {
306 pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
307 }
308 }
309 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) {
310 return (FALSE);
311 }
312 EP0Data.pData = pD;
313 len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
314 break;
315 case USB_STRING_DESCRIPTOR_TYPE:
316 pD = (uint8_t *)USB_StringDescriptor;
317 for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
318 if (((USB_STRING_DESCRIPTOR *)pD)->bLength != 0) {
319 pD += ((USB_STRING_DESCRIPTOR *)pD)->bLength;
320 }
321 }
322 if (((USB_STRING_DESCRIPTOR *)pD)->bLength == 0) {
323 return (FALSE);
324 }
325 EP0Data.pData = pD;
326 len = ((USB_STRING_DESCRIPTOR *)EP0Data.pData)->bLength;
327 break;
328 default:
329 return (FALSE);
330 }
331 break;
332 case REQUEST_TO_INTERFACE:
333 switch (SetupPacket.wValue.WB.H) {
334 #if USB_HID
335 case HID_HID_DESCRIPTOR_TYPE:
336 if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
337 return (FALSE); /* Only Single HID Interface is supported */
338 }
339 EP0Data.pData = (uint8_t *)USB_ConfigDescriptor + HID_DESC_OFFSET;
340 len = HID_DESC_SIZE;
341 break;
342 case HID_REPORT_DESCRIPTOR_TYPE:
343 if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
344 return (FALSE); /* Only Single HID Interface is supported */
345 }
346 EP0Data.pData = (uint8_t *)HID_ReportDescriptor;
347 len = HID_ReportDescSize;
348 break;
349 case HID_PHYSICAL_DESCRIPTOR_TYPE:
350 return (FALSE); /* HID Physical Descriptor is not supported */
351 #endif
352 default:
353 return (FALSE);
354 }
355 break;
356 default:
357 return (FALSE);
358 }
359
360 if (EP0Data.Count > len) {
361 EP0Data.Count = len;
362 }
363
364 return (TRUE);
365 }
366
367 /*
368 * Get Configuration USB Request
369 * Parameters: None (global SetupPacket)
370 * Return Value: TRUE - Success, FALSE - Error
371 */
372
373 static inline uint32_t USB_ReqGetConfiguration (void) {
374
375 switch (SetupPacket.bmRequestType.BM.Recipient) {
376 case REQUEST_TO_DEVICE:
377 // Added cast to avoid warnings due to USB_Configuration being volatile (KTownsend)
378 EP0Data.pData = (uint8_t *)&USB_Configuration;
379 break;
380 default:
381 return (FALSE);
382 }
383 return (TRUE);
384 }
385
386 /*
387 * Set Configuration USB Request
388 * Parameters: None (global SetupPacket)
389 * Return Value: TRUE - Success, FALSE - Error
390 */
391
392 static inline uint32_t USB_ReqSetConfiguration (void) {
393 USB_COMMON_DESCRIPTOR *pD;
394 uint32_t alt = 0;
395 uint32_t n, m;
396
397 switch (SetupPacket.bmRequestType.BM.Recipient) {
398 case REQUEST_TO_DEVICE:
399
400 if (SetupPacket.wValue.WB.L) {
401 pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
402 while (pD->bLength) {
403 switch (pD->bDescriptorType) {
404 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
405 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) {
406 USB_Configuration = SetupPacket.wValue.WB.L;
407 USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces;
408 for (n = 0; n < USB_IF_NUM; n++) {
409 USB_AltSetting[n] = 0;
410 }
411 for (n = 1; n < 16; n++) {
412 if (USB_EndPointMask & (1 << n)) {
413 USB_DisableEP(n);
414 }
415 if (USB_EndPointMask & ((1 << 16) << n)) {
416 USB_DisableEP(n | 0x80);
417 }
418 }
419 USB_EndPointMask = 0x00010001;
420 USB_EndPointHalt = 0x00000000;
421 USB_EndPointStall= 0x00000000;
422 USB_Configure(TRUE);
423 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) {
424 USB_DeviceStatus |= USB_GETSTATUS_SELF_POWERED;
425 } else {
426 USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED;
427 }
428 } else {
429 UsbAddPtr((void **)&pD, ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength);
430 continue;
431 }
432 break;
433 case USB_INTERFACE_DESCRIPTOR_TYPE:
434 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
435 break;
436 case USB_ENDPOINT_DESCRIPTOR_TYPE:
437 if (alt == 0) {
438 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
439 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
440 USB_EndPointMask |= m;
441 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
442 USB_EnableEP(n);
443 USB_ResetEP(n);
444 }
445 break;
446 }
447 UsbAddPtr((void **)&pD, pD->bLength);
448 }
449 }
450 else {
451 USB_Configuration = 0;
452 for (n = 1; n < 16; n++) {
453 if (USB_EndPointMask & (1 << n)) {
454 USB_DisableEP(n);
455 }
456 if (USB_EndPointMask & ((1 << 16) << n)) {
457 USB_DisableEP(n | 0x80);
458 }
459 }
460 USB_EndPointMask = 0x00010001;
461 USB_EndPointHalt = 0x00000000;
462 USB_EndPointStall = 0x00000000;
463 USB_Configure(FALSE);
464 }
465
466 if (USB_Configuration != SetupPacket.wValue.WB.L) {
467 return (FALSE);
468 }
469 break;
470 default:
471 return (FALSE);
472 }
473 return (TRUE);
474 }
475
476
477 /*
478 * Get Interface USB Request
479 * Parameters: None (global SetupPacket)
480 * Return Value: TRUE - Success, FALSE - Error
481 */
482
483 static inline uint32_t USB_ReqGetInterface (void) {
484
485 switch (SetupPacket.bmRequestType.BM.Recipient) {
486 case REQUEST_TO_INTERFACE:
487 if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
488 EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
489 } else {
490 return (FALSE);
491 }
492 break;
493 default:
494 return (FALSE);
495 }
496 return (TRUE);
497 }
498
499
500 /*
501 * Set Interface USB Request
502 * Parameters: None (global SetupPacket)
503 * Return Value: TRUE - Success, FALSE - Error
504 */
505
506 static inline uint32_t USB_ReqSetInterface (void) {
507 USB_COMMON_DESCRIPTOR *pD;
508 uint32_t ifn = 0, alt = 0, old = 0, msk = 0;
509 uint32_t n, m;
510 uint32_t set;
511
512 switch (SetupPacket.bmRequestType.BM.Recipient) {
513 case REQUEST_TO_INTERFACE:
514 if (USB_Configuration == 0) return (FALSE);
515 set = FALSE;
516 pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
517 while (pD->bLength) {
518 switch (pD->bDescriptorType) {
519 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
520 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue != USB_Configuration) {
521 UsbAddPtr((void **)&pD, ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength);
522 continue;
523 }
524 break;
525 case USB_INTERFACE_DESCRIPTOR_TYPE:
526 ifn = ((USB_INTERFACE_DESCRIPTOR *)pD)->bInterfaceNumber;
527 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
528 msk = 0;
529 if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) {
530 set = TRUE;
531 old = USB_AltSetting[ifn];
532 USB_AltSetting[ifn] = (uint8_t)alt;
533 }
534 break;
535 case USB_ENDPOINT_DESCRIPTOR_TYPE:
536 if (ifn == SetupPacket.wIndex.WB.L) {
537 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
538 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
539 if (alt == SetupPacket.wValue.WB.L) {
540 USB_EndPointMask |= m;
541 USB_EndPointHalt &= ~m;
542 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
543 USB_EnableEP(n);
544 USB_ResetEP(n);
545 msk |= m;
546 }
547 else if ((alt == old) && ((msk & m) == 0)) {
548 USB_EndPointMask &= ~m;
549 USB_EndPointHalt &= ~m;
550 USB_DisableEP(n);
551 }
552 }
553 break;
554 }
555 UsbAddPtr((void **)&pD, pD->bLength);
556 }
557 break;
558 default:
559 return (FALSE);
560 }
561
562 return (set);
563 }
564
565 /*
566 * USB Endpoint 0 Event Callback
567 * Parameters: event
568 * Return Value: none
569 */
570
571 void USB_EndPoint0 (uint32_t event) {
572
573 switch (event) {
574 case USB_EVT_SETUP:
575 USB_SetupStage();
576 USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
577 EP0Data.Count = SetupPacket.wLength; /* Number of bytes to transfer */
578 switch (SetupPacket.bmRequestType.BM.Type) {
579
580 case REQUEST_STANDARD:
581 switch (SetupPacket.bRequest) {
582 case USB_REQUEST_GET_STATUS:
583 if (!USB_ReqGetStatus()) {
584 goto stall_i;
585 }
586 USB_DataInStage();
587 break;
588
589 case USB_REQUEST_CLEAR_FEATURE:
590 if (!USB_ReqSetClrFeature(0)) {
591 goto stall_i;
592 }
593 USB_StatusInStage();
594 #if USB_FEATURE_EVENT
595 USB_Feature_Event();
596 #endif
597 break;
598
599 case USB_REQUEST_SET_FEATURE:
600 if (!USB_ReqSetClrFeature(1)) {
601 goto stall_i;
602 }
603 USB_StatusInStage();
604 #if USB_FEATURE_EVENT
605 USB_Feature_Event();
606 #endif
607 break;
608
609 case USB_REQUEST_SET_ADDRESS:
610 if (!USB_ReqSetAddress()) {
611 goto stall_i;
612 }
613 USB_StatusInStage();
614 break;
615
616 case USB_REQUEST_GET_DESCRIPTOR:
617 if (!USB_ReqGetDescriptor()) {
618 goto stall_i;
619 }
620 USB_DataInStage();
621 break;
622
623 case USB_REQUEST_SET_DESCRIPTOR:
624 /*stall_o:*/ USB_SetStallEP(0x00); /* not supported */
625 EP0Data.Count = 0;
626 break;
627
628 case USB_REQUEST_GET_CONFIGURATION:
629 if (!USB_ReqGetConfiguration()) {
630 goto stall_i;
631 }
632 USB_DataInStage();
633 break;
634
635 case USB_REQUEST_SET_CONFIGURATION:
636 if (!USB_ReqSetConfiguration()) {
637 goto stall_i;
638 }
639 USB_StatusInStage();
640 #if USB_CONFIGURE_EVENT
641 USB_Configure_Event();
642 #endif
643 break;
644
645 case USB_REQUEST_GET_INTERFACE:
646 if (!USB_ReqGetInterface()) {
647 goto stall_i;
648 }
649 USB_DataInStage();
650 break;
651
652 case USB_REQUEST_SET_INTERFACE:
653 if (!USB_ReqSetInterface()) {
654 goto stall_i;
655 }
656 USB_StatusInStage();
657 #if USB_INTERFACE_EVENT
658 USB_Interface_Event();
659 #endif
660 break;
661
662 default:
663 goto stall_i;
664 }
665 break; /* end case REQUEST_STANDARD */
666
667 #if USB_CLASS
668 case REQUEST_CLASS:
669 switch (SetupPacket.bmRequestType.BM.Recipient) {
670
671 case REQUEST_TO_DEVICE:
672 goto stall_i; /* not supported */
673
674 case REQUEST_TO_INTERFACE:
675 #if USB_HID
676 if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) { /* IF number correct? */
677 switch (SetupPacket.bRequest) {
678 case HID_REQUEST_GET_REPORT:
679 if (HID_GetReport()) {
680 EP0Data.pData = EP0Buf; /* point to data to be sent */
681 USB_DataInStage(); /* send requested data */
682 goto setup_class_ok;
683 }
684 break;
685 case HID_REQUEST_SET_REPORT:
686 EP0Data.pData = EP0Buf; /* data to be received */
687 goto setup_class_ok;
688 case HID_REQUEST_GET_IDLE:
689 if (HID_GetIdle()) {
690 EP0Data.pData = EP0Buf; /* point to data to be sent */
691 USB_DataInStage(); /* send requested data */
692 goto setup_class_ok;
693 }
694 break;
695 case HID_REQUEST_SET_IDLE:
696 if (HID_SetIdle()) {
697 USB_StatusInStage(); /* send Acknowledge */
698 goto setup_class_ok;
699 }
700 break;
701 case HID_REQUEST_GET_PROTOCOL:
702 if (HID_GetProtocol()) {
703 EP0Data.pData = EP0Buf; /* point to data to be sent */
704 USB_DataInStage(); /* send requested data */
705 goto setup_class_ok;
706 }
707 break;
708 case HID_REQUEST_SET_PROTOCOL:
709 if (HID_SetProtocol()) {
710 USB_StatusInStage(); /* send Acknowledge */
711 goto setup_class_ok;
712 }
713 break;
714 }
715 }
716 #endif /* USB_HID */
717 #if USB_MSC
718 if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) { /* IF number correct? */
719 switch (SetupPacket.bRequest) {
720 case MSC_REQUEST_RESET:
721 if ((SetupPacket.wValue.W == 0) && /* RESET with invalid parameters -> STALL */
722 (SetupPacket.wLength == 0)) {
723 if (MSC_Reset()) {
724 USB_StatusInStage();
725 goto setup_class_ok;
726 }
727 }
728 break;
729 case MSC_REQUEST_GET_MAX_LUN:
730 if ((SetupPacket.wValue.W == 0) && /* GET_MAX_LUN with invalid parameters -> STALL */
731 (SetupPacket.wLength == 1)) {
732 if (MSC_GetMaxLUN()) {
733 EP0Data.pData = EP0Buf;
734 USB_DataInStage();
735 goto setup_class_ok;
736 }
737 }
738 break;
739 }
740 }
741 #endif /* USB_MSC */
742 #if USB_AUDIO
743 if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) || /* IF number correct? */
744 (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
745 (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
746 switch (SetupPacket.bRequest) {
747 case AUDIO_REQUEST_GET_CUR:
748 case AUDIO_REQUEST_GET_MIN:
749 case AUDIO_REQUEST_GET_MAX:
750 case AUDIO_REQUEST_GET_RES:
751 if (ADC_IF_GetRequest()) {
752 EP0Data.pData = EP0Buf; /* point to data to be sent */
753 USB_DataInStage(); /* send requested data */
754 goto setup_class_ok;
755 }
756 break;
757 case AUDIO_REQUEST_SET_CUR:
758 // case AUDIO_REQUEST_SET_MIN:
759 // case AUDIO_REQUEST_SET_MAX:
760 // case AUDIO_REQUEST_SET_RES:
761 EP0Data.pData = EP0Buf; /* data to be received */
762 goto setup_class_ok;
763 }
764 }
765 #endif /* USB_AUDIO */
766 #if USB_CDC
767 if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */
768 (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
769 switch (SetupPacket.bRequest) {
770 case CDC_SEND_ENCAPSULATED_COMMAND:
771 EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
772 goto setup_class_ok;
773 case CDC_GET_ENCAPSULATED_RESPONSE:
774 if (CDC_GetEncapsulatedResponse()) {
775 EP0Data.pData = EP0Buf; /* point to data to be sent */
776 USB_DataInStage(); /* send requested data */
777 goto setup_class_ok;
778 }
779 break;
780 case CDC_SET_COMM_FEATURE:
781 EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
782 goto setup_class_ok;
783 case CDC_GET_COMM_FEATURE:
784 if (CDC_GetCommFeature(SetupPacket.wValue.W)) {
785 EP0Data.pData = EP0Buf; /* point to data to be sent */
786 USB_DataInStage(); /* send requested data */
787 goto setup_class_ok;
788 }
789 break;
790 case CDC_CLEAR_COMM_FEATURE:
791 if (CDC_ClearCommFeature(SetupPacket.wValue.W)) {
792 USB_StatusInStage(); /* send Acknowledge */
793 goto setup_class_ok;
794 }
795 break;
796 case CDC_SET_LINE_CODING:
797 EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
798 goto setup_class_ok;
799 case CDC_GET_LINE_CODING:
800 if (CDC_GetLineCoding()) {
801 EP0Data.pData = EP0Buf; /* point to data to be sent */
802 USB_DataInStage(); /* send requested data */
803 goto setup_class_ok;
804 }
805 break;
806 case CDC_SET_CONTROL_LINE_STATE:
807 if (CDC_SetControlLineState(SetupPacket.wValue.W)) {
808 USB_StatusInStage(); /* send Acknowledge */
809 goto setup_class_ok;
810 }
811 break;
812 case CDC_SEND_BREAK:
813 if (CDC_SendBreak(SetupPacket.wValue.W)) {
814 USB_StatusInStage(); /* send Acknowledge */
815 goto setup_class_ok;
816 }
817 break;
818 }
819 }
820 #endif /* USB_CDC */
821 goto stall_i; /* not supported */
822 /* end case REQUEST_TO_INTERFACE */
823
824 case REQUEST_TO_ENDPOINT:
825 #if USB_AUDIO
826 switch (SetupPacket.bRequest) {
827 case AUDIO_REQUEST_GET_CUR:
828 case AUDIO_REQUEST_GET_MIN:
829 case AUDIO_REQUEST_GET_MAX:
830 case AUDIO_REQUEST_GET_RES:
831 if (ADC_EP_GetRequest()) {
832 EP0Data.pData = EP0Buf; /* point to data to be sent */
833 USB_DataInStage(); /* send requested data */
834 goto setup_class_ok;
835 }
836 break;
837 case AUDIO_REQUEST_SET_CUR:
838 // case AUDIO_REQUEST_SET_MIN:
839 // case AUDIO_REQUEST_SET_MAX:
840 // case AUDIO_REQUEST_SET_RES:
841 EP0Data.pData = EP0Buf; /* data to be received */
842 goto setup_class_ok;
843 }
844 #endif /* USB_AUDIO */
845 goto stall_i;
846 /* end case REQUEST_TO_ENDPOINT */
847
848 default:
849 goto stall_i;
850 }
851 setup_class_ok: /* request finished successfully */
852 break; /* end case REQUEST_CLASS */
853 #endif /* USB_CLASS */
854
855 #if USB_VENDOR
856 case REQUEST_VENDOR:
857 switch (SetupPacket.bmRequestType.BM.Recipient) {
858
859 case REQUEST_TO_DEVICE:
860 if (!USB_ReqVendorDev(TRUE)) {
861 goto stall_i; /* not supported */
862 }
863 break;
864
865 case REQUEST_TO_INTERFACE:
866 if (!USB_ReqVendorIF(TRUE)) {
867 goto stall_i; /* not supported */
868 }
869 break;
870
871 case REQUEST_TO_ENDPOINT:
872 if (!USB_ReqVendorEP(TRUE)) {
873 goto stall_i; /* not supported */
874 }
875 break;
876
877 default:
878 goto stall_i;
879 }
880
881 if (SetupPacket.wLength) {
882 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
883 USB_DataInStage();
884 }
885 } else {
886 USB_StatusInStage();
887 }
888
889 break; /* end case REQUEST_VENDOR */
890 #endif /* USB_VENDOR */
891
892 default:
893 stall_i: USB_SetStallEP(0x80);
894 EP0Data.Count = 0;
895 break;
896 }
897 break; /* end case USB_EVT_SETUP */
898
899 case USB_EVT_OUT:
900 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_HOST_TO_DEVICE) {
901 if (EP0Data.Count) { /* still data to receive ? */
902 USB_DataOutStage(); /* receive data */
903 if (EP0Data.Count == 0) { /* data complete ? */
904 switch (SetupPacket.bmRequestType.BM.Type) {
905
906 case REQUEST_STANDARD:
907 goto stall_i; /* not supported */
908
909 #if (USB_CLASS)
910 case REQUEST_CLASS:
911 switch (SetupPacket.bmRequestType.BM.Recipient) {
912 case REQUEST_TO_DEVICE:
913 goto stall_i; /* not supported */
914
915 case REQUEST_TO_INTERFACE:
916 #if USB_HID
917 if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) { /* IF number correct? */
918 switch (SetupPacket.bRequest) {
919 case HID_REQUEST_SET_REPORT:
920 if (HID_SetReport()) {
921 USB_StatusInStage(); /* send Acknowledge */
922 goto out_class_ok;
923 }
924 break;
925 }
926 }
927 #endif /* USB_HID */
928 #if USB_AUDIO
929 if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) || /* IF number correct? */
930 (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
931 (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
932 switch (SetupPacket.bRequest) {
933 case AUDIO_REQUEST_SET_CUR:
934 // case AUDIO_REQUEST_SET_MIN:
935 // case AUDIO_REQUEST_SET_MAX:
936 // case AUDIO_REQUEST_SET_RES:
937 if (ADC_IF_SetRequest()) {
938 USB_StatusInStage(); /* send Acknowledge */
939 goto out_class_ok;
940 }
941 break;
942 }
943 }
944 #endif /* USB_AUDIO */
945 #if USB_CDC
946 if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */
947 (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
948 switch (SetupPacket.bRequest) {
949 case CDC_SEND_ENCAPSULATED_COMMAND:
950 if (CDC_SendEncapsulatedCommand()) {
951 USB_StatusInStage(); /* send Acknowledge */
952 goto out_class_ok;
953 }
954 break;
955 case CDC_SET_COMM_FEATURE:
956 if (CDC_SetCommFeature(SetupPacket.wValue.W)) {
957 USB_StatusInStage(); /* send Acknowledge */
958 goto out_class_ok;
959 }
960 break;
961 case CDC_SET_LINE_CODING:
962 if (CDC_SetLineCoding()) {
963 USB_StatusInStage(); /* send Acknowledge */
964 goto out_class_ok;
965 }
966 break;
967 }
968 }
969 #endif /* USB_CDC */
970 goto stall_i;
971 /* end case REQUEST_TO_INTERFACE */
972
973 case REQUEST_TO_ENDPOINT:
974 #if USB_AUDIO
975 switch (SetupPacket.bRequest) {
976 case AUDIO_REQUEST_SET_CUR:
977 // case AUDIO_REQUEST_SET_MIN:
978 // case AUDIO_REQUEST_SET_MAX:
979 // case AUDIO_REQUEST_SET_RES:
980 if (ADC_EP_SetRequest()) {
981 USB_StatusInStage(); /* send Acknowledge */
982 goto out_class_ok;
983 }
984 break;
985 }
986 #endif /* USB_AUDIO */
987 goto stall_i;
988 /* end case REQUEST_TO_ENDPOINT */
989
990 default:
991 goto stall_i;
992 }
993 out_class_ok: /* request finished successfully */
994 break; /* end case REQUEST_CLASS */
995 #endif /* USB_CLASS */
996
997 #if USB_VENDOR
998 case REQUEST_VENDOR:
999 switch (SetupPacket.bmRequestType.BM.Recipient) {
1000
1001 case REQUEST_TO_DEVICE:
1002 if (!USB_ReqVendorDev(FALSE)) {
1003 goto stall_i; /* not supported */
1004 }
1005 break;
1006
1007 case REQUEST_TO_INTERFACE:
1008 if (!USB_ReqVendorIF(FALSE)) {
1009 goto stall_i; /* not supported */
1010 }
1011 break;
1012
1013 case REQUEST_TO_ENDPOINT:
1014 if (!USB_ReqVendorEP(FALSE)) {
1015 goto stall_i; /* not supported */
1016 }
1017 break;
1018
1019 default:
1020 goto stall_i;
1021 }
1022
1023 USB_StatusInStage();
1024
1025 break; /* end case REQUEST_VENDOR */
1026 #endif /* USB_VENDOR */
1027
1028 default:
1029 goto stall_i;
1030 }
1031 }
1032 }
1033 } else {
1034 USB_StatusOutStage(); /* receive Acknowledge */
1035 }
1036 break; /* end case USB_EVT_OUT */
1037
1038 case USB_EVT_IN :
1039 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
1040 USB_DataInStage(); /* send data */
1041 } else {
1042 if (USB_DeviceAddress & 0x80) {
1043 USB_DeviceAddress &= 0x7F;
1044 USB_SetAddress(USB_DeviceAddress);
1045 }
1046 }
1047 break; /* end case USB_EVT_IN */
1048
1049 case USB_EVT_OUT_STALL:
1050 USB_ClrStallEP(0x00);
1051 break;
1052
1053 case USB_EVT_IN_STALL:
1054 USB_ClrStallEP(0x80);
1055 break;
1056
1057 }
1058 }
This page took 0.092707 seconds and 5 git commands to generate.