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