v1.1.0
[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 //*((uint16_t __attribute__((packed)) *)EP0Buf) = 0;
181 *ep0 = 0;
182 EP0Data.pData = EP0Buf;
183 } else {
184 return (FALSE);
185 }
186 break;
187 case REQUEST_TO_ENDPOINT:
188 n = SetupPacket.wIndex.WB.L & 0x8F;
189 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
190 if (((USB_Configuration != 0) || ((n & 0x0F) == 0)) && (USB_EndPointMask & m)) {
191 // *((uint16_t __attribute__((packed)) *)EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0;
192 *ep0 = (USB_EndPointHalt & m) ? 1 : 0;
193 EP0Data.pData = EP0Buf;
194 } else {
195 return (FALSE);
196 }
197 break;
198 default:
199 return (FALSE);
200 }
201 return (TRUE);
202 }
203
204
205 /*
206 * Set/Clear Feature USB Request
207 * Parameters: sc: 0 - Clear, 1 - Set
208 * (global SetupPacket)
209 * Return Value: TRUE - Success, FALSE - Error
210 */
211
212 static inline uint32_t USB_ReqSetClrFeature (uint32_t sc) {
213 uint32_t n, m;
214
215 switch (SetupPacket.bmRequestType.BM.Recipient) {
216 case REQUEST_TO_DEVICE:
217 if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) {
218 if (sc) {
219 USB_WakeUpCfg(TRUE);
220 USB_DeviceStatus |= USB_GETSTATUS_REMOTE_WAKEUP;
221 } else {
222 USB_WakeUpCfg(FALSE);
223 USB_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP;
224 }
225 } else {
226 return (FALSE);
227 }
228 break;
229 case REQUEST_TO_INTERFACE:
230 return (FALSE);
231 case REQUEST_TO_ENDPOINT:
232 n = SetupPacket.wIndex.WB.L & 0x8F;
233 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
234 if ((USB_Configuration != 0) && ((n & 0x0F) != 0) && (USB_EndPointMask & m)) {
235 if (SetupPacket.wValue.W == USB_FEATURE_ENDPOINT_STALL) {
236 if (sc) {
237 USB_SetStallEP(n);
238 USB_EndPointHalt |= m;
239 } else {
240 if ((USB_EndPointStall & m) != 0) {
241 return (TRUE);
242 }
243 USB_ClrStallEP(n);
244 #if (USB_MSC)
245 if ((n == MSC_EP_IN) && ((USB_EndPointHalt & m) != 0)) {
246 /* Compliance Test: rewrite CSW after unstall */
247 if (CSW.dSignature == MSC_CSW_Signature) {
248 USB_WriteEP(MSC_EP_IN, (uint8_t *)&CSW, sizeof(CSW));
249 }
250 }
251 #endif
252 USB_EndPointHalt &= ~m;
253 }
254 } else {
255 return (FALSE);
256 }
257 } else {
258 return (FALSE);
259 }
260 break;
261 default:
262 return (FALSE);
263 }
264 return (TRUE);
265 }
266
267
268 /*
269 * Set Address USB Request
270 * Parameters: None (global SetupPacket)
271 * Return Value: TRUE - Success, FALSE - Error
272 */
273
274 static inline uint32_t USB_ReqSetAddress (void) {
275
276 switch (SetupPacket.bmRequestType.BM.Recipient) {
277 case REQUEST_TO_DEVICE:
278 USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
279 break;
280 default:
281 return (FALSE);
282 }
283 return (TRUE);
284 }
285
286
287 /*
288 * Get Descriptor USB Request
289 * Parameters: None (global SetupPacket)
290 * Return Value: TRUE - Success, FALSE - Error
291 */
292
293 static inline uint32_t USB_ReqGetDescriptor (void) {
294 uint8_t *pD;
295 uint32_t len, n;
296
297 switch (SetupPacket.bmRequestType.BM.Recipient) {
298 case REQUEST_TO_DEVICE:
299 switch (SetupPacket.wValue.WB.H) {
300 case USB_DEVICE_DESCRIPTOR_TYPE:
301 EP0Data.pData = (uint8_t *)USB_DeviceDescriptor;
302 len = USB_DEVICE_DESC_SIZE;
303 break;
304 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
305 pD = (uint8_t *)USB_ConfigDescriptor;
306 for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
307 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) {
308 pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
309 }
310 }
311 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) {
312 return (FALSE);
313 }
314 EP0Data.pData = pD;
315 len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
316 break;
317 case USB_STRING_DESCRIPTOR_TYPE:
318 pD = (uint8_t *)USB_StringDescriptor;
319 for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
320 if (((USB_STRING_DESCRIPTOR *)pD)->bLength != 0) {
321 pD += ((USB_STRING_DESCRIPTOR *)pD)->bLength;
322 }
323 }
324 if (((USB_STRING_DESCRIPTOR *)pD)->bLength == 0) {
325 return (FALSE);
326 }
327 EP0Data.pData = pD;
328 len = ((USB_STRING_DESCRIPTOR *)EP0Data.pData)->bLength;
329 break;
330 default:
331 return (FALSE);
332 }
333 break;
334 case REQUEST_TO_INTERFACE:
335 switch (SetupPacket.wValue.WB.H) {
336 #if USB_HID
337 case HID_HID_DESCRIPTOR_TYPE:
338 if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
339 return (FALSE); /* Only Single HID Interface is supported */
340 }
341 EP0Data.pData = (uint8_t *)USB_ConfigDescriptor + HID_DESC_OFFSET;
342 len = HID_DESC_SIZE;
343 break;
344 case HID_REPORT_DESCRIPTOR_TYPE:
345 if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
346 return (FALSE); /* Only Single HID Interface is supported */
347 }
348 EP0Data.pData = (uint8_t *)HID_ReportDescriptor;
349 len = HID_ReportDescSize;
350 break;
351 case HID_PHYSICAL_DESCRIPTOR_TYPE:
352 return (FALSE); /* HID Physical Descriptor is not supported */
353 #endif
354 default:
355 return (FALSE);
356 }
357 break;
358 default:
359 return (FALSE);
360 }
361
362 if (EP0Data.Count > len) {
363 EP0Data.Count = len;
364 }
365
366 return (TRUE);
367 }
368
369
370 /*
371 * Get Configuration USB Request
372 * Parameters: None (global SetupPacket)
373 * Return Value: TRUE - Success, FALSE - Error
374 */
375
376 static inline uint32_t USB_ReqGetConfiguration (void) {
377
378 switch (SetupPacket.bmRequestType.BM.Recipient) {
379 case REQUEST_TO_DEVICE:
380 // Added cast to avoid warnings due to USB_Configuration being volatile (KTownsend)
381 EP0Data.pData = (uint8_t *)&USB_Configuration;
382 //EP0Data.pData = &USB_Configuration;
383 break;
384 default:
385 return (FALSE);
386 }
387 return (TRUE);
388 }
389
390 /*
391 * Set Configuration USB Request
392 * Parameters: None (global SetupPacket)
393 * Return Value: TRUE - Success, FALSE - Error
394 */
395
396 static inline uint32_t USB_ReqSetConfiguration (void) {
397 USB_COMMON_DESCRIPTOR *pD;
398 uint32_t alt = 0;
399 uint32_t n, m;
400
401 switch (SetupPacket.bmRequestType.BM.Recipient) {
402 case REQUEST_TO_DEVICE:
403
404 if (SetupPacket.wValue.WB.L) {
405 pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
406 while (pD->bLength) {
407 switch (pD->bDescriptorType) {
408 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
409 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) {
410 USB_Configuration = SetupPacket.wValue.WB.L;
411 USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces;
412 for (n = 0; n < USB_IF_NUM; n++) {
413 USB_AltSetting[n] = 0;
414 }
415 for (n = 1; n < 16; n++) {
416 if (USB_EndPointMask & (1 << n)) {
417 USB_DisableEP(n);
418 }
419 if (USB_EndPointMask & ((1 << 16) << n)) {
420 USB_DisableEP(n | 0x80);
421 }
422 }
423 USB_EndPointMask = 0x00010001;
424 USB_EndPointHalt = 0x00000000;
425 USB_EndPointStall= 0x00000000;
426 USB_Configure(TRUE);
427 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) {
428 USB_DeviceStatus |= USB_GETSTATUS_SELF_POWERED;
429 } else {
430 USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED;
431 }
432 } else {
433 UsbAddPtr((void **)&pD, ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength);
434 continue;
435 }
436 break;
437 case USB_INTERFACE_DESCRIPTOR_TYPE:
438 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
439 break;
440 case USB_ENDPOINT_DESCRIPTOR_TYPE:
441 if (alt == 0) {
442 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
443 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
444 USB_EndPointMask |= m;
445 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
446 USB_EnableEP(n);
447 USB_ResetEP(n);
448 }
449 break;
450 }
451 UsbAddPtr((void **)&pD, pD->bLength);
452 }
453 }
454 else {
455 USB_Configuration = 0;
456 for (n = 1; n < 16; n++) {
457 if (USB_EndPointMask & (1 << n)) {
458 USB_DisableEP(n);
459 }
460 if (USB_EndPointMask & ((1 << 16) << n)) {
461 USB_DisableEP(n | 0x80);
462 }
463 }
464 USB_EndPointMask = 0x00010001;
465 USB_EndPointHalt = 0x00000000;
466 USB_EndPointStall = 0x00000000;
467 USB_Configure(FALSE);
468 }
469
470 if (USB_Configuration != SetupPacket.wValue.WB.L) {
471 return (FALSE);
472 }
473 break;
474 default:
475 return (FALSE);
476 }
477 return (TRUE);
478 }
479
480
481 /*
482 * Get Interface USB Request
483 * Parameters: None (global SetupPacket)
484 * Return Value: TRUE - Success, FALSE - Error
485 */
486
487 static inline uint32_t USB_ReqGetInterface (void) {
488
489 switch (SetupPacket.bmRequestType.BM.Recipient) {
490 case REQUEST_TO_INTERFACE:
491 if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
492 EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
493 } else {
494 return (FALSE);
495 }
496 break;
497 default:
498 return (FALSE);
499 }
500 return (TRUE);
501 }
502
503
504 /*
505 * Set Interface USB Request
506 * Parameters: None (global SetupPacket)
507 * Return Value: TRUE - Success, FALSE - Error
508 */
509
510 static inline uint32_t USB_ReqSetInterface (void) {
511 USB_COMMON_DESCRIPTOR *pD;
512 uint32_t ifn = 0, alt = 0, old = 0, msk = 0;
513 uint32_t n, m;
514 uint32_t set;
515
516 switch (SetupPacket.bmRequestType.BM.Recipient) {
517 case REQUEST_TO_INTERFACE:
518 if (USB_Configuration == 0) return (FALSE);
519 set = FALSE;
520 pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
521 while (pD->bLength) {
522 switch (pD->bDescriptorType) {
523 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
524 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue != USB_Configuration) {
525 UsbAddPtr((void **)&pD, ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength);
526 continue;
527 }
528 break;
529 case USB_INTERFACE_DESCRIPTOR_TYPE:
530 ifn = ((USB_INTERFACE_DESCRIPTOR *)pD)->bInterfaceNumber;
531 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
532 msk = 0;
533 if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) {
534 set = TRUE;
535 old = USB_AltSetting[ifn];
536 USB_AltSetting[ifn] = (uint8_t)alt;
537 }
538 break;
539 case USB_ENDPOINT_DESCRIPTOR_TYPE:
540 if (ifn == SetupPacket.wIndex.WB.L) {
541 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
542 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
543 if (alt == SetupPacket.wValue.WB.L) {
544 USB_EndPointMask |= m;
545 USB_EndPointHalt &= ~m;
546 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
547 USB_EnableEP(n);
548 USB_ResetEP(n);
549 msk |= m;
550 }
551 else if ((alt == old) && ((msk & m) == 0)) {
552 USB_EndPointMask &= ~m;
553 USB_EndPointHalt &= ~m;
554 USB_DisableEP(n);
555 }
556 }
557 break;
558 }
559 UsbAddPtr((void **)&pD, pD->bLength);
560 }
561 break;
562 default:
563 return (FALSE);
564 }
565
566 return (set);
567 }
568
569
570 /*
571 * USB Endpoint 0 Event Callback
572 * Parameters: event
573 * Return Value: none
574 */
575
576 void USB_EndPoint0 (uint32_t event) {
577
578 switch (event) {
579 case USB_EVT_SETUP:
580 USB_SetupStage();
581 USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
582 EP0Data.Count = SetupPacket.wLength; /* Number of bytes to transfer */
583 switch (SetupPacket.bmRequestType.BM.Type) {
584
585 case REQUEST_STANDARD:
586 switch (SetupPacket.bRequest) {
587 case USB_REQUEST_GET_STATUS:
588 if (!USB_ReqGetStatus()) {
589 goto stall_i;
590 }
591 USB_DataInStage();
592 break;
593
594 case USB_REQUEST_CLEAR_FEATURE:
595 if (!USB_ReqSetClrFeature(0)) {
596 goto stall_i;
597 }
598 USB_StatusInStage();
599 #if USB_FEATURE_EVENT
600 USB_Feature_Event();
601 #endif
602 break;
603
604 case USB_REQUEST_SET_FEATURE:
605 if (!USB_ReqSetClrFeature(1)) {
606 goto stall_i;
607 }
608 USB_StatusInStage();
609 #if USB_FEATURE_EVENT
610 USB_Feature_Event();
611 #endif
612 break;
613
614 case USB_REQUEST_SET_ADDRESS:
615 if (!USB_ReqSetAddress()) {
616 goto stall_i;
617 }
618 USB_StatusInStage();
619 break;
620
621 case USB_REQUEST_GET_DESCRIPTOR:
622 if (!USB_ReqGetDescriptor()) {
623 goto stall_i;
624 }
625 USB_DataInStage();
626 break;
627
628 case USB_REQUEST_SET_DESCRIPTOR:
629 /*stall_o:*/ USB_SetStallEP(0x00); /* not supported */
630 EP0Data.Count = 0;
631 break;
632
633 case USB_REQUEST_GET_CONFIGURATION:
634 if (!USB_ReqGetConfiguration()) {
635 goto stall_i;
636 }
637 USB_DataInStage();
638 break;
639
640 case USB_REQUEST_SET_CONFIGURATION:
641 if (!USB_ReqSetConfiguration()) {
642 goto stall_i;
643 }
644 USB_StatusInStage();
645 #if USB_CONFIGURE_EVENT
646 USB_Configure_Event();
647 #endif
648 break;
649
650 case USB_REQUEST_GET_INTERFACE:
651 if (!USB_ReqGetInterface()) {
652 goto stall_i;
653 }
654 USB_DataInStage();
655 break;
656
657 case USB_REQUEST_SET_INTERFACE:
658 if (!USB_ReqSetInterface()) {
659 goto stall_i;
660 }
661 USB_StatusInStage();
662 #if USB_INTERFACE_EVENT
663 USB_Interface_Event();
664 #endif
665 break;
666
667 default:
668 goto stall_i;
669 }
670 break; /* end case REQUEST_STANDARD */
671
672 #if USB_CLASS
673 case REQUEST_CLASS:
674 switch (SetupPacket.bmRequestType.BM.Recipient) {
675
676 case REQUEST_TO_DEVICE:
677 goto stall_i; /* not supported */
678
679 case REQUEST_TO_INTERFACE:
680 #if USB_HID
681 if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) { /* IF number correct? */
682 switch (SetupPacket.bRequest) {
683 case HID_REQUEST_GET_REPORT:
684 if (HID_GetReport()) {
685 EP0Data.pData = EP0Buf; /* point to data to be sent */
686 USB_DataInStage(); /* send requested data */
687 goto setup_class_ok;
688 }
689 break;
690 case HID_REQUEST_SET_REPORT:
691 EP0Data.pData = EP0Buf; /* data to be received */
692 goto setup_class_ok;
693 case HID_REQUEST_GET_IDLE:
694 if (HID_GetIdle()) {
695 EP0Data.pData = EP0Buf; /* point to data to be sent */
696 USB_DataInStage(); /* send requested data */
697 goto setup_class_ok;
698 }
699 break;
700 case HID_REQUEST_SET_IDLE:
701 if (HID_SetIdle()) {
702 USB_StatusInStage(); /* send Acknowledge */
703 goto setup_class_ok;
704 }
705 break;
706 case HID_REQUEST_GET_PROTOCOL:
707 if (HID_GetProtocol()) {
708 EP0Data.pData = EP0Buf; /* point to data to be sent */
709 USB_DataInStage(); /* send requested data */
710 goto setup_class_ok;
711 }
712 break;
713 case HID_REQUEST_SET_PROTOCOL:
714 if (HID_SetProtocol()) {
715 USB_StatusInStage(); /* send Acknowledge */
716 goto setup_class_ok;
717 }
718 break;
719 }
720 }
721 #endif /* USB_HID */
722 #if USB_MSC
723 if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) { /* IF number correct? */
724 switch (SetupPacket.bRequest) {
725 case MSC_REQUEST_RESET:
726 if ((SetupPacket.wValue.W == 0) && /* RESET with invalid parameters -> STALL */
727 (SetupPacket.wLength == 0)) {
728 if (MSC_Reset()) {
729 USB_StatusInStage();
730 goto setup_class_ok;
731 }
732 }
733 break;
734 case MSC_REQUEST_GET_MAX_LUN:
735 if ((SetupPacket.wValue.W == 0) && /* GET_MAX_LUN with invalid parameters -> STALL */
736 (SetupPacket.wLength == 1)) {
737 if (MSC_GetMaxLUN()) {
738 EP0Data.pData = EP0Buf;
739 USB_DataInStage();
740 goto setup_class_ok;
741 }
742 }
743 break;
744 }
745 }
746 #endif /* USB_MSC */
747 #if USB_AUDIO
748 if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) || /* IF number correct? */
749 (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
750 (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
751 switch (SetupPacket.bRequest) {
752 case AUDIO_REQUEST_GET_CUR:
753 case AUDIO_REQUEST_GET_MIN:
754 case AUDIO_REQUEST_GET_MAX:
755 case AUDIO_REQUEST_GET_RES:
756 if (ADC_IF_GetRequest()) {
757 EP0Data.pData = EP0Buf; /* point to data to be sent */
758 USB_DataInStage(); /* send requested data */
759 goto setup_class_ok;
760 }
761 break;
762 case AUDIO_REQUEST_SET_CUR:
763 // case AUDIO_REQUEST_SET_MIN:
764 // case AUDIO_REQUEST_SET_MAX:
765 // case AUDIO_REQUEST_SET_RES:
766 EP0Data.pData = EP0Buf; /* data to be received */
767 goto setup_class_ok;
768 }
769 }
770 #endif /* USB_AUDIO */
771 #if USB_CDC
772 if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */
773 (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
774 switch (SetupPacket.bRequest) {
775 case CDC_SEND_ENCAPSULATED_COMMAND:
776 EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
777 goto setup_class_ok;
778 case CDC_GET_ENCAPSULATED_RESPONSE:
779 if (CDC_GetEncapsulatedResponse()) {
780 EP0Data.pData = EP0Buf; /* point to data to be sent */
781 USB_DataInStage(); /* send requested data */
782 goto setup_class_ok;
783 }
784 break;
785 case CDC_SET_COMM_FEATURE:
786 EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
787 goto setup_class_ok;
788 case CDC_GET_COMM_FEATURE:
789 if (CDC_GetCommFeature(SetupPacket.wValue.W)) {
790 EP0Data.pData = EP0Buf; /* point to data to be sent */
791 USB_DataInStage(); /* send requested data */
792 goto setup_class_ok;
793 }
794 break;
795 case CDC_CLEAR_COMM_FEATURE:
796 if (CDC_ClearCommFeature(SetupPacket.wValue.W)) {
797 USB_StatusInStage(); /* send Acknowledge */
798 goto setup_class_ok;
799 }
800 break;
801 case CDC_SET_LINE_CODING:
802 EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
803 goto setup_class_ok;
804 case CDC_GET_LINE_CODING:
805 if (CDC_GetLineCoding()) {
806 EP0Data.pData = EP0Buf; /* point to data to be sent */
807 USB_DataInStage(); /* send requested data */
808 goto setup_class_ok;
809 }
810 break;
811 case CDC_SET_CONTROL_LINE_STATE:
812 if (CDC_SetControlLineState(SetupPacket.wValue.W)) {
813 USB_StatusInStage(); /* send Acknowledge */
814 goto setup_class_ok;
815 }
816 break;
817 case CDC_SEND_BREAK:
818 if (CDC_SendBreak(SetupPacket.wValue.W)) {
819 USB_StatusInStage(); /* send Acknowledge */
820 goto setup_class_ok;
821 }
822 break;
823 }
824 }
825 #endif /* USB_CDC */
826 goto stall_i; /* not supported */
827 /* end case REQUEST_TO_INTERFACE */
828
829 case REQUEST_TO_ENDPOINT:
830 #if USB_AUDIO
831 switch (SetupPacket.bRequest) {
832 case AUDIO_REQUEST_GET_CUR:
833 case AUDIO_REQUEST_GET_MIN:
834 case AUDIO_REQUEST_GET_MAX:
835 case AUDIO_REQUEST_GET_RES:
836 if (ADC_EP_GetRequest()) {
837 EP0Data.pData = EP0Buf; /* point to data to be sent */
838 USB_DataInStage(); /* send requested data */
839 goto setup_class_ok;
840 }
841 break;
842 case AUDIO_REQUEST_SET_CUR:
843 // case AUDIO_REQUEST_SET_MIN:
844 // case AUDIO_REQUEST_SET_MAX:
845 // case AUDIO_REQUEST_SET_RES:
846 EP0Data.pData = EP0Buf; /* data to be received */
847 goto setup_class_ok;
848 }
849 #endif /* USB_AUDIO */
850 goto stall_i;
851 /* end case REQUEST_TO_ENDPOINT */
852
853 default:
854 goto stall_i;
855 }
856 setup_class_ok: /* request finished successfully */
857 break; /* end case REQUEST_CLASS */
858 #endif /* USB_CLASS */
859
860 #if USB_VENDOR
861 case REQUEST_VENDOR:
862 switch (SetupPacket.bmRequestType.BM.Recipient) {
863
864 case REQUEST_TO_DEVICE:
865 if (!USB_ReqVendorDev(TRUE)) {
866 goto stall_i; /* not supported */
867 }
868 break;
869
870 case REQUEST_TO_INTERFACE:
871 if (!USB_ReqVendorIF(TRUE)) {
872 goto stall_i; /* not supported */
873 }
874 break;
875
876 case REQUEST_TO_ENDPOINT:
877 if (!USB_ReqVendorEP(TRUE)) {
878 goto stall_i; /* not supported */
879 }
880 break;
881
882 default:
883 goto stall_i;
884 }
885
886 if (SetupPacket.wLength) {
887 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
888 USB_DataInStage();
889 }
890 } else {
891 USB_StatusInStage();
892 }
893
894 break; /* end case REQUEST_VENDOR */
895 #endif /* USB_VENDOR */
896
897 default:
898 stall_i: USB_SetStallEP(0x80);
899 EP0Data.Count = 0;
900 break;
901 }
902 break; /* end case USB_EVT_SETUP */
903
904 case USB_EVT_OUT:
905 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_HOST_TO_DEVICE) {
906 if (EP0Data.Count) { /* still data to receive ? */
907 USB_DataOutStage(); /* receive data */
908 if (EP0Data.Count == 0) { /* data complete ? */
909 switch (SetupPacket.bmRequestType.BM.Type) {
910
911 case REQUEST_STANDARD:
912 goto stall_i; /* not supported */
913
914 #if (USB_CLASS)
915 case REQUEST_CLASS:
916 switch (SetupPacket.bmRequestType.BM.Recipient) {
917 case REQUEST_TO_DEVICE:
918 goto stall_i; /* not supported */
919
920 case REQUEST_TO_INTERFACE:
921 #if USB_HID
922 if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) { /* IF number correct? */
923 switch (SetupPacket.bRequest) {
924 case HID_REQUEST_SET_REPORT:
925 if (HID_SetReport()) {
926 USB_StatusInStage(); /* send Acknowledge */
927 goto out_class_ok;
928 }
929 break;
930 }
931 }
932 #endif /* USB_HID */
933 #if USB_AUDIO
934 if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) || /* IF number correct? */
935 (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
936 (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
937 switch (SetupPacket.bRequest) {
938 case AUDIO_REQUEST_SET_CUR:
939 // case AUDIO_REQUEST_SET_MIN:
940 // case AUDIO_REQUEST_SET_MAX:
941 // case AUDIO_REQUEST_SET_RES:
942 if (ADC_IF_SetRequest()) {
943 USB_StatusInStage(); /* send Acknowledge */
944 goto out_class_ok;
945 }
946 break;
947 }
948 }
949 #endif /* USB_AUDIO */
950 #if USB_CDC
951 if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */
952 (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
953 switch (SetupPacket.bRequest) {
954 case CDC_SEND_ENCAPSULATED_COMMAND:
955 if (CDC_SendEncapsulatedCommand()) {
956 USB_StatusInStage(); /* send Acknowledge */
957 goto out_class_ok;
958 }
959 break;
960 case CDC_SET_COMM_FEATURE:
961 if (CDC_SetCommFeature(SetupPacket.wValue.W)) {
962 USB_StatusInStage(); /* send Acknowledge */
963 goto out_class_ok;
964 }
965 break;
966 case CDC_SET_LINE_CODING:
967 if (CDC_SetLineCoding()) {
968 USB_StatusInStage(); /* send Acknowledge */
969 goto out_class_ok;
970 }
971 break;
972 }
973 }
974 #endif /* USB_CDC */
975 goto stall_i;
976 /* end case REQUEST_TO_INTERFACE */
977
978 case REQUEST_TO_ENDPOINT:
979 #if USB_AUDIO
980 switch (SetupPacket.bRequest) {
981 case AUDIO_REQUEST_SET_CUR:
982 // case AUDIO_REQUEST_SET_MIN:
983 // case AUDIO_REQUEST_SET_MAX:
984 // case AUDIO_REQUEST_SET_RES:
985 if (ADC_EP_SetRequest()) {
986 USB_StatusInStage(); /* send Acknowledge */
987 goto out_class_ok;
988 }
989 break;
990 }
991 #endif /* USB_AUDIO */
992 goto stall_i;
993 /* end case REQUEST_TO_ENDPOINT */
994
995 default:
996 goto stall_i;
997 }
998 out_class_ok: /* request finished successfully */
999 break; /* end case REQUEST_CLASS */
1000 #endif /* USB_CLASS */
1001
1002 #if USB_VENDOR
1003 case REQUEST_VENDOR:
1004 switch (SetupPacket.bmRequestType.BM.Recipient) {
1005
1006 case REQUEST_TO_DEVICE:
1007 if (!USB_ReqVendorDev(FALSE)) {
1008 goto stall_i; /* not supported */
1009 }
1010 break;
1011
1012 case REQUEST_TO_INTERFACE:
1013 if (!USB_ReqVendorIF(FALSE)) {
1014 goto stall_i; /* not supported */
1015 }
1016 break;
1017
1018 case REQUEST_TO_ENDPOINT:
1019 if (!USB_ReqVendorEP(FALSE)) {
1020 goto stall_i; /* not supported */
1021 }
1022 break;
1023
1024 default:
1025 goto stall_i;
1026 }
1027
1028 USB_StatusInStage();
1029
1030 break; /* end case REQUEST_VENDOR */
1031 #endif /* USB_VENDOR */
1032
1033 default:
1034 goto stall_i;
1035 }
1036 }
1037 }
1038 } else {
1039 USB_StatusOutStage(); /* receive Acknowledge */
1040 }
1041 break; /* end case USB_EVT_OUT */
1042
1043 case USB_EVT_IN :
1044 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
1045 USB_DataInStage(); /* send data */
1046 } else {
1047 if (USB_DeviceAddress & 0x80) {
1048 USB_DeviceAddress &= 0x7F;
1049 USB_SetAddress(USB_DeviceAddress);
1050 }
1051 }
1052 break; /* end case USB_EVT_IN */
1053
1054 case USB_EVT_OUT_STALL:
1055 USB_ClrStallEP(0x00);
1056 break;
1057
1058 case USB_EVT_IN_STALL:
1059 USB_ClrStallEP(0x80);
1060 break;
1061
1062 }
1063 }
This page took 0.098432 seconds and 5 git commands to generate.