summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Chapman <dave@dchapman.com>2007-08-27 22:07:36 +0000
committerDave Chapman <dave@dchapman.com>2007-08-27 22:07:36 +0000
commit03745f4ed33f9dadc16bfc0e26472203e33b43cf (patch)
treec5ba1df703895f67f72eaca7776eaf65b191ce31
parenta6d52a8305ba355aeeef17a0648d40e6e4f8fa70 (diff)
downloadrockbox-03745f4ed33f9dadc16bfc0e26472203e33b43cf.tar.gz
rockbox-03745f4ed33f9dadc16bfc0e26472203e33b43cf.zip
Cosmetic changes only - tab, whitespace and brace policing
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@14484 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--firmware/drivers/usb/arcotg_dcd.c320
-rw-r--r--firmware/drivers/usb/arcotg_dcd.h4
-rw-r--r--firmware/target/arm/usb-fw-pp502x.c2
-rw-r--r--firmware/usbstack/config.h2
-rw-r--r--firmware/usbstack/core.h2
-rw-r--r--firmware/usbstack/core/config.c90
-rw-r--r--firmware/usbstack/core/core.c197
-rw-r--r--firmware/usbstack/core/epsetup.c98
-rw-r--r--firmware/usbstack/core/utils.c102
-rw-r--r--firmware/usbstack/device.h4
-rw-r--r--firmware/usbstack/drivers/device/usb_serial.c164
-rw-r--r--firmware/usbstack/drivers/device/usb_storage.c134
-rw-r--r--firmware/usbstack/host.h2
13 files changed, 562 insertions, 559 deletions
diff --git a/firmware/drivers/usb/arcotg_dcd.c b/firmware/drivers/usb/arcotg_dcd.c
index b545026a61..59ca93a072 100644
--- a/firmware/drivers/usb/arcotg_dcd.c
+++ b/firmware/drivers/usb/arcotg_dcd.c
@@ -118,14 +118,15 @@ timer_expired(struct timer * timer)
118/* gets called by usb_stack_init() to register 118/* gets called by usb_stack_init() to register
119 * this arcotg device conrtollder driver in the 119 * this arcotg device conrtollder driver in the
120 * stack. */ 120 * stack. */
121void usb_dcd_init(void) { 121void usb_dcd_init(void)
122{
122 usb_controller_register(&arcotg_dcd); 123 usb_controller_register(&arcotg_dcd);
123} 124}
124 125
125/*-------------------------------------------------------------------------*/ 126/*-------------------------------------------------------------------------*/
126 127
127void usb_arcotg_dcd_init(void) { 128void usb_arcotg_dcd_init(void)
128 129{
129 struct timer t; 130 struct timer t;
130 int i, ep_num = 0; 131 int i, ep_num = 0;
131 132
@@ -180,35 +181,36 @@ void usb_arcotg_dcd_init(void) {
180 /* put controller in device mode */ 181 /* put controller in device mode */
181 UDC_USBMODE |= USB_MODE_CTRL_MODE_DEVICE; 182 UDC_USBMODE |= USB_MODE_CTRL_MODE_DEVICE;
182 183
183 /* init queue heads */ 184 /* init queue heads */
184 qh_init(0, USB_RECV, USB_ENDPOINT_XFER_CONTROL, USB_MAX_CTRL_PAYLOAD, 0, 0); 185 qh_init(0, USB_RECV, USB_ENDPOINT_XFER_CONTROL, USB_MAX_CTRL_PAYLOAD, 0, 0);
185 qh_init(0, USB_SEND, USB_ENDPOINT_XFER_CONTROL, USB_MAX_CTRL_PAYLOAD, 0, 0); 186 qh_init(0, USB_SEND, USB_ENDPOINT_XFER_CONTROL, USB_MAX_CTRL_PAYLOAD, 0, 0);
186 187
187 UDC_ENDPOINTLISTADDR = (unsigned int)dev_qh; 188 UDC_ENDPOINTLISTADDR = (unsigned int)dev_qh;
188} 189}
189 190
190void usb_arcotg_dcd_shutdown(void) { 191void usb_arcotg_dcd_shutdown(void)
192{
191 193
192} 194}
193 195
194void usb_arcotg_dcd_start(void) { 196void usb_arcotg_dcd_start(void)
195 197{
196 logf("start"); 198 logf("start");
197 199
198 if (arcotg_dcd.device_driver != NULL) { 200 if (arcotg_dcd.device_driver != NULL) {
199 logf("YEEEEEEESSSSSSS"); 201 logf("YEEEEEEESSSSSSS");
200 } else { 202 } else {
201 logf("NOOOOOO"); 203 logf("NOOOOOO");
202 } 204 }
203 205
204 /* clear stopped bit */ 206 /* clear stopped bit */
205 dcd_controller.stopped = false; 207 dcd_controller.stopped = false;
206 208
207 UDC_USBCMD |= USB_CMD_RUN; 209 UDC_USBCMD |= USB_CMD_RUN;
208} 210}
209 211
210void usb_arcotg_dcd_stop(void) { 212void usb_arcotg_dcd_stop(void)
211 213{
212 logf("stop"); 214 logf("stop");
213 215
214 /* set stopped bit */ 216 /* set stopped bit */
@@ -217,8 +219,8 @@ void usb_arcotg_dcd_stop(void) {
217 UDC_USBCMD &= ~USB_CMD_RUN; 219 UDC_USBCMD &= ~USB_CMD_RUN;
218} 220}
219 221
220void usb_arcotg_dcd_irq(void) { 222void usb_arcotg_dcd_irq(void)
221 223{
222 if (dcd_controller.stopped == true) { 224 if (dcd_controller.stopped == true) {
223 return; 225 return;
224 } 226 }
@@ -241,10 +243,10 @@ void usb_arcotg_dcd_irq(void) {
241 } 243 }
242 244
243 if (UDC_ENDPTCOMPLETE) { 245 if (UDC_ENDPTCOMPLETE) {
244 UDC_ENDPTCOMPLETE = UDC_ENDPTCOMPLETE; 246 UDC_ENDPTCOMPLETE = UDC_ENDPTCOMPLETE;
245 } 247 }
246 } 248 }
247 249
248 if (UDC_USBSTS & USB_STS_PORT_CHANGE) { 250 if (UDC_USBSTS & USB_STS_PORT_CHANGE) {
249 port_change_int(); 251 port_change_int();
250 } 252 }
@@ -269,12 +271,12 @@ void usb_arcotg_dcd_irq(void) {
269/*-------------------------------------------------------------------------*/ 271/*-------------------------------------------------------------------------*/
270/* interrupt handlers */ 272/* interrupt handlers */
271 273
272static void setup_received_int(struct usb_ctrlrequest* request) { 274static void setup_received_int(struct usb_ctrlrequest* request)
273 275{
274 int error = 0; 276 int error = 0;
275 uint8_t address = 0; 277 uint8_t address = 0;
276 int handled = 0; /* set to zero if we do not handle the message, */ 278 int handled = 0; /* set to zero if we do not handle the message, */
277 /* and should pass it to the driver */ 279 /* and should pass it to the driver */
278 280
279 logf("setup_int"); 281 logf("setup_int");
280 into_usb_ctrlrequest(request); 282 into_usb_ctrlrequest(request);
@@ -324,16 +326,16 @@ static void setup_received_int(struct usb_ctrlrequest* request) {
324 } 326 }
325 327
326 if (handled == 0) { 328 if (handled == 0) {
327 handled = 1; /* dont pass it to driver */ 329 handled = 1; /* dont pass it to driver */
328 } 330 }
329 } 331 }
330#if 0 332#if 0
331 if (rc == 0) { 333 if (rc == 0) {
332 /* send status only if _arcotg_ep_set_halt success */ 334 /* send status only if _arcotg_ep_set_halt success */
333 if (ep0_prime_status(udc, EP_DIR_IN)) 335 if (ep0_prime_status(udc, EP_DIR_IN))
334 Ep0Stall(udc); 336 Ep0Stall(udc);
335 } 337 }
336#endif 338#endif
337 break; 339 break;
338 } 340 }
339 341
@@ -358,8 +360,8 @@ static void setup_received_int(struct usb_ctrlrequest* request) {
358 } 360 }
359} 361}
360 362
361static void port_change_int(void) { 363static void port_change_int(void)
362 364{
363 //logf("port_change_int"); 365 //logf("port_change_int");
364 uint32_t tmp; 366 uint32_t tmp;
365 enum usb_device_speed speed = USB_SPEED_UNKNOWN; 367 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
@@ -386,7 +388,7 @@ static void port_change_int(void) {
386 388
387 /* update speed */ 389 /* update speed */
388 arcotg_dcd.speed = speed; 390 arcotg_dcd.speed = speed;
389 391
390 /* update USB state */ 392 /* update USB state */
391 if (!dcd_controller.resume_state) { 393 if (!dcd_controller.resume_state) {
392 dcd_controller.usb_state = USB_STATE_DEFAULT; 394 dcd_controller.usb_state = USB_STATE_DEFAULT;
@@ -398,8 +400,8 @@ static void port_change_int(void) {
398 } 400 }
399} 401}
400 402
401static void suspend_int(void) { 403static void suspend_int(void)
402 404{
403 //logf("suspend_int"); 405 //logf("suspend_int");
404 dcd_controller.resume_state = dcd_controller.usb_state; 406 dcd_controller.resume_state = dcd_controller.usb_state;
405 dcd_controller.usb_state = USB_STATE_SUSPENDED; 407 dcd_controller.usb_state = USB_STATE_SUSPENDED;
@@ -410,8 +412,8 @@ static void suspend_int(void) {
410 } 412 }
411} 413}
412 414
413static void resume_int(void) { 415static void resume_int(void)
414 416{
415 //logf("resume_int"); 417 //logf("resume_int");
416 dcd_controller.usb_state = dcd_controller.resume_state; 418 dcd_controller.usb_state = dcd_controller.resume_state;
417 dcd_controller.resume_state = USB_STATE_NOTATTACHED; 419 dcd_controller.resume_state = USB_STATE_NOTATTACHED;
@@ -422,8 +424,8 @@ static void resume_int(void) {
422 } 424 }
423} 425}
424 426
425static void reset_int(void) { 427static void reset_int(void)
426 428{
427 //logf("reset_int"); 429 //logf("reset_int");
428 struct timer t; 430 struct timer t;
429 431
@@ -459,12 +461,12 @@ static void reset_int(void) {
459/*-------------------------------------------------------------------------*/ 461/*-------------------------------------------------------------------------*/
460/* usb controller ops */ 462/* usb controller ops */
461 463
462int usb_arcotg_dcd_enable(struct usb_ep* ep) { 464int usb_arcotg_dcd_enable(struct usb_ep* ep)
463 465{
464 unsigned short max = 0; 466 unsigned short max = 0;
465 unsigned char mult = 0, zlt = 0; 467 unsigned char mult = 0, zlt = 0;
466 int retval = 0; 468 int retval = 0;
467 char *val = NULL; /* for debug */ 469 char *val = NULL; /* for debug */
468 470
469 /* catch bogus parameter */ 471 /* catch bogus parameter */
470 if (!ep) { 472 if (!ep) {
@@ -496,105 +498,105 @@ int usb_arcotg_dcd_enable(struct usb_ep* ep) {
496 498
497#if 0 499#if 0
498 switch (ep->desc->bmAttributes & 0x03) { 500 switch (ep->desc->bmAttributes & 0x03) {
499 case USB_ENDPOINT_XFER_BULK: 501 case USB_ENDPOINT_XFER_BULK:
500 if (strstr(ep->ep.name, "-iso") || strstr(ep->ep.name, "-int")) { 502 if (strstr(ep->ep.name, "-iso") || strstr(ep->ep.name, "-int")) {
501 goto en_done; 503 goto en_done;
502 } 504 }
503 mult = 0; 505 mult = 0;
504 zlt = 1; 506 zlt = 1;
505 507
506 switch (arcotg_dcd.speed) { 508 switch (arcotg_dcd.speed) {
507 case USB_SPEED_HIGH: 509 case USB_SPEED_HIGH:
508 if ((max == 128) || (max == 256) || (max == 512)) { 510 if ((max == 128) || (max == 256) || (max == 512)) {
509 break; 511 break;
510 } 512 }
511 default: 513 default:
512 switch (max) { 514 switch (max) {
513 case 4: 515 case 4:
514 case 8: 516 case 8:
515 case 16: 517 case 16:
516 case 32: 518 case 32:
517 case 64: 519 case 64:
518 break; 520 break;
519 default: 521 default:
520 + case USB_SPEED_LOW: 522 + case USB_SPEED_LOW:
521 + goto en_done; 523 + goto en_done;
522 + } 524 + }
523 + } 525 + }
524 + break; 526 + break;
525 + case USB_ENDPOINT_XFER_INT: 527 + case USB_ENDPOINT_XFER_INT:
526 + if (strstr(ep->ep.name, "-iso")) /* bulk is ok */ 528 + if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
527 + goto en_done; 529 + goto en_done;
528 + mult = 0; 530 + mult = 0;
529 + zlt = 1; 531 + zlt = 1;
530 + switch (udc->gadget.speed) { 532 + switch (udc->gadget.speed) {
531 + case USB_SPEED_HIGH: 533 + case USB_SPEED_HIGH:
532 + if (max <= 1024) 534 + if (max <= 1024)
533 + break; 535 + break;
534 + case USB_SPEED_FULL: 536 + case USB_SPEED_FULL:
535 + if (max <= 64) 537 + if (max <= 64)
536 + break; 538 + break;
537 + default: 539 + default:
538 + if (max <= 8) 540 + if (max <= 8)
539 + break; 541 + break;
540 + goto en_done; 542 + goto en_done;
541 + } 543 + }
542 + break; 544 + break;
543 + case USB_ENDPOINT_XFER_ISOC: 545 + case USB_ENDPOINT_XFER_ISOC:
544 + if (strstr(ep->ep.name, "-bulk") || strstr(ep->ep.name, "-int")) 546 + if (strstr(ep->ep.name, "-bulk") || strstr(ep->ep.name, "-int"))
545 + goto en_done; 547 + goto en_done;
546 + mult = (unsigned char) 548 + mult = (unsigned char)
547 + (1 + ((le16_to_cpu(desc->wMaxPacketSize) >> 11) & 0x03)); 549 + (1 + ((le16_to_cpu(desc->wMaxPacketSize) >> 11) & 0x03));
548 + zlt = 0; 550 + zlt = 0;
549 + switch (udc->gadget.speed) { 551 + switch (udc->gadget.speed) {
550 + case USB_SPEED_HIGH: 552 + case USB_SPEED_HIGH:
551 + if (max <= 1024) 553 + if (max <= 1024)
552 + break; 554 + break;
553 + case USB_SPEED_FULL: 555 + case USB_SPEED_FULL:
554 + if (max <= 1023) 556 + if (max <= 1023)
555 + break; 557 + break;
556 + default: 558 + default:
557 + goto en_done; 559 + goto en_done;
558 + } 560 + }
559 + break; 561 + break;
560 + case USB_ENDPOINT_XFER_CONTROL: 562 + case USB_ENDPOINT_XFER_CONTROL:
561 + if (strstr(ep->ep.name, "-iso") || strstr(ep->ep.name, "-int")) 563 + if (strstr(ep->ep.name, "-iso") || strstr(ep->ep.name, "-int"))
562 + goto en_done; 564 + goto en_done;
563 + mult = 0; 565 + mult = 0;
564 + zlt = 1; 566 + zlt = 1;
565 + switch (udc->gadget.speed) { 567 + switch (udc->gadget.speed) {
566 + case USB_SPEED_HIGH: 568 + case USB_SPEED_HIGH:
567 + case USB_SPEED_FULL: 569 + case USB_SPEED_FULL:
568 + switch (max) { 570 + switch (max) {
569 + case 1: 571 + case 1:
570 + case 2: 572 + case 2:
571 + case 4: 573 + case 4:
572 + case 8: 574 + case 8:
573 + case 16: 575 + case 16:
574 + case 32: 576 + case 32:
575 + case 64: 577 + case 64:
576 + break; 578 + break;
577 + default: 579 + default:
578 + goto en_done; 580 + goto en_done;
579 + } 581 + }
580 + case USB_SPEED_LOW: 582 + case USB_SPEED_LOW:
581 + switch (max) { 583 + switch (max) {
582 + case 1: 584 + case 1:
583 + case 2: 585 + case 2:
584 + case 4: 586 + case 4:
585 + case 8: 587 + case 8:
586 + break; 588 + break;
587 + default: 589 + default:
588 + goto en_done; 590 + goto en_done;
589 + } 591 + }
590 + default: 592 + default:
591 + goto en_done; 593 + goto en_done;
592 + } 594 + }
593 + break; 595 + break;
594 + 596 +
595 + default: 597 + default:
596 + goto en_done; 598 + goto en_done;
597 + } 599 + }
598#endif 600#endif
599 601
600 /* here initialize variable of ep */ 602 /* here initialize variable of ep */
@@ -643,8 +645,8 @@ int usb_arcotg_dcd_enable(struct usb_ep* ep) {
643 return retval; 645 return retval;
644} 646}
645 647
646int usb_arcotg_dcd_set_halt(struct usb_ep* ep, bool halt) { 648int usb_arcotg_dcd_set_halt(struct usb_ep* ep, bool halt)
647 649{
648 int status = -EOPNOTSUPP; /* operation not supported */ 650 int status = -EOPNOTSUPP; /* operation not supported */
649 unsigned char dir = 0; 651 unsigned char dir = 0;
650 unsigned int tmp_epctrl = 0; 652 unsigned int tmp_epctrl = 0;
@@ -688,8 +690,8 @@ out:
688 return status; 690 return status;
689} 691}
690 692
691int usb_arcotg_dcd_send(struct usb_ep* ep, struct usb_response* res) { 693int usb_arcotg_dcd_send(struct usb_ep* ep, struct usb_response* res)
692 694{
693 char* ptr; 695 char* ptr;
694 int todo, error, size, done = 0; 696 int todo, error, size, done = 0;
695 int index = 1; /* use as default ep0 tx qh and td */ 697 int index = 1; /* use as default ep0 tx qh and td */
@@ -750,8 +752,8 @@ int usb_arcotg_dcd_send(struct usb_ep* ep, struct usb_response* res) {
750 return done; 752 return done;
751} 753}
752 754
753int usb_arcotg_dcd_receive(struct usb_ep* ep, struct usb_response* res) { 755int usb_arcotg_dcd_receive(struct usb_ep* ep, struct usb_response* res)
754 756{
755 char* ptr; 757 char* ptr;
756 int todo, error, size, done = 0; 758 int todo, error, size, done = 0;
757 int index = 0; /* use as default ep0 rx qh and td */ 759 int index = 0; /* use as default ep0 rx qh and td */
@@ -812,9 +814,9 @@ int usb_arcotg_dcd_receive(struct usb_ep* ep, struct usb_response* res) {
812/* lifecylce */ 814/* lifecylce */
813 815
814static void qh_init(unsigned char ep_num, unsigned char dir, unsigned char ep_type, 816static void qh_init(unsigned char ep_num, unsigned char dir, unsigned char ep_type,
815 unsigned int max_pkt_len, unsigned int zlt, unsigned char mult) { 817 unsigned int max_pkt_len, unsigned int zlt, unsigned char mult)
816 818{
817 struct dqh *qh = &dev_qh[2 * ep_num + dir]; 819 struct dqh *qh = &dev_qh[2 * ep_num + dir];
818 uint32_t tmp = 0; 820 uint32_t tmp = 0;
819 memset(qh, 0, sizeof(struct dqh)); 821 memset(qh, 0, sizeof(struct dqh));
820 822
@@ -856,8 +858,8 @@ static void qh_init(unsigned char ep_num, unsigned char dir, unsigned char ep_ty
856 logf("qh: init %d", (2 * ep_num + dir)); 858 logf("qh: init %d", (2 * ep_num + dir));
857} 859}
858 860
859static void td_init(struct dtd* td, void* buffer, uint32_t todo) { 861static void td_init(struct dtd* td, void* buffer, uint32_t todo)
860 862{
861 /* see 32.14.5.2 Building a Transfer Descriptor */ 863 /* see 32.14.5.2 Building a Transfer Descriptor */
862 864
863 /* init first 7 dwords with 0 */ 865 /* init first 7 dwords with 0 */
@@ -878,8 +880,8 @@ static void td_init(struct dtd* td, void* buffer, uint32_t todo) {
878 td->buf_ptr0 = (uint32_t)buffer; 880 td->buf_ptr0 = (uint32_t)buffer;
879} 881}
880 882
881static void ep_setup(unsigned char ep_num, unsigned char dir, unsigned char ep_type) { 883static void ep_setup(unsigned char ep_num, unsigned char dir, unsigned char ep_type)
882 884{
883 unsigned int tmp_epctrl = 0; 885 unsigned int tmp_epctrl = 0;
884 struct timer t; 886 struct timer t;
885 887
@@ -901,23 +903,23 @@ static void ep_setup(unsigned char ep_num, unsigned char dir, unsigned char ep_t
901 } 903 }
902 904
903 UDC_ENDPTCTRL(ep_num) = tmp_epctrl; 905 UDC_ENDPTCTRL(ep_num) = tmp_epctrl;
904 906
905 /* wait for the write reg to finish */ 907 /* wait for the write reg to finish */
906 908
907 timer_set(&t, SETUP_TIMER); 909 timer_set(&t, SETUP_TIMER);
908 while (!(UDC_ENDPTCTRL(ep_num) & (tmp_epctrl & (EPCTRL_TX_ENABLE | EPCTRL_RX_ENABLE)))) { 910 while (!(UDC_ENDPTCTRL(ep_num) & (tmp_epctrl & (EPCTRL_TX_ENABLE | EPCTRL_RX_ENABLE)))) {
909 if (timer_expired(&t)) { 911 if (timer_expired(&t)) {
910 logf("TIMEOUT: enable ep"); 912 logf("TIMEOUT: enable ep");
911 return; 913 return;
912 } 914 }
913 } 915 }
914} 916}
915 917
916/*-------------------------------------------------------------------------*/ 918/*-------------------------------------------------------------------------*/
917/* helpers for sending/receiving */ 919/* helpers for sending/receiving */
918 920
919static int td_enqueue(struct dtd* td, struct dqh* qh, unsigned int mask) { 921static int td_enqueue(struct dtd* td, struct dqh* qh, unsigned int mask)
920 922{
921 struct timer t; 923 struct timer t;
922 924
923 qh->dtd_ovrl.next_dtd = (unsigned int)td; 925 qh->dtd_ovrl.next_dtd = (unsigned int)td;
@@ -940,8 +942,8 @@ static int td_enqueue(struct dtd* td, struct dqh* qh, unsigned int mask) {
940 return 0; 942 return 0;
941} 943}
942 944
943static int td_wait(struct dtd* td, unsigned int mask) { 945static int td_wait(struct dtd* td, unsigned int mask)
944 946{
945 struct timer t; 947 struct timer t;
946 timer_set(&t, TRANSFER_TIMER); 948 timer_set(&t, TRANSFER_TIMER);
947 949
@@ -960,8 +962,8 @@ static int td_wait(struct dtd* td, unsigned int mask) {
960 } 962 }
961} 963}
962 964
963static int usb_ack(struct usb_ctrlrequest * s, int error) { 965static int usb_ack(struct usb_ctrlrequest * s, int error)
964 966{
965 if (error) { 967 if (error) {
966 logf("STALLing ep0"); 968 logf("STALLing ep0");
967 UDC_ENDPTCTRL0 |= 1 << 16; /* stall */ 969 UDC_ENDPTCTRL0 |= 1 << 16; /* stall */
diff --git a/firmware/drivers/usb/arcotg_dcd.h b/firmware/drivers/usb/arcotg_dcd.h
index 127ee43efa..68c2468d14 100644
--- a/firmware/drivers/usb/arcotg_dcd.h
+++ b/firmware/drivers/usb/arcotg_dcd.h
@@ -42,8 +42,8 @@
42 42
43#define ep_is_in(EP) (((EP)->desc->bEndpointAddress & USB_DIR_IN)==USB_DIR_IN) 43#define ep_is_in(EP) (((EP)->desc->bEndpointAddress & USB_DIR_IN)==USB_DIR_IN)
44 44
45#define EP_DIR_IN 1 45#define EP_DIR_IN 1
46#define EP_DIR_OUT 0 46#define EP_DIR_OUT 0
47 47
48/*-------------------------------------------------------------------------*/ 48/*-------------------------------------------------------------------------*/
49 49
diff --git a/firmware/target/arm/usb-fw-pp502x.c b/firmware/target/arm/usb-fw-pp502x.c
index 3c6b689d0f..e76c2509ad 100644
--- a/firmware/target/arm/usb-fw-pp502x.c
+++ b/firmware/target/arm/usb-fw-pp502x.c
@@ -150,7 +150,7 @@ bool usb_detect(void)
150 if ((usbstatus1 == true) && (prev_usbstatus1 == false)) { 150 if ((usbstatus1 == true) && (prev_usbstatus1 == false)) {
151 usb_stack_start(); 151 usb_stack_start();
152 } else if ((usbstatus1 == false) && (prev_usbstatus1 == true)) { 152 } else if ((usbstatus1 == false) && (prev_usbstatus1 == true)) {
153 usb_stack_stop(); 153 usb_stack_stop();
154 } 154 }
155#else 155#else
156 if ((usbstatus1 == true) && (prev_usbstatus1 == false)) { 156 if ((usbstatus1 == true) && (prev_usbstatus1 == false)) {
diff --git a/firmware/usbstack/config.h b/firmware/usbstack/config.h
index 95b00da1b3..e9cbe63970 100644
--- a/firmware/usbstack/config.h
+++ b/firmware/usbstack/config.h
@@ -25,7 +25,7 @@
25#define USBSTACK_CAPS 0 25#define USBSTACK_CAPS 0
26#endif 26#endif
27 27
28#define CONTROLLER_DEVICE (1 << 0) 28#define CONTROLLER_DEVICE (1 << 0)
29#define CONTROLLER_HOST (1 << 1) 29#define CONTROLLER_HOST (1 << 1)
30 30
31#endif /*_USBSTACK_CONFIG_H_*/ 31#endif /*_USBSTACK_CONFIG_H_*/
diff --git a/firmware/usbstack/core.h b/firmware/usbstack/core.h
index 7bda2934e7..33805ac42e 100644
--- a/firmware/usbstack/core.h
+++ b/firmware/usbstack/core.h
@@ -38,7 +38,7 @@ struct usb_response {
38struct usb_ep { 38struct usb_ep {
39 const char name[15]; 39 const char name[15];
40 uint8_t type; 40 uint8_t type;
41 uint32_t ep_num; /* which endpoint? */ 41 uint32_t ep_num; /* which endpoint? */
42 uint32_t pipe_num; /* which pipe? */ 42 uint32_t pipe_num; /* which pipe? */
43 uint32_t maxpacket; 43 uint32_t maxpacket;
44 bool claimed; 44 bool claimed;
diff --git a/firmware/usbstack/core/config.c b/firmware/usbstack/core/config.c
index a05a508bf9..277156d0d9 100644
--- a/firmware/usbstack/core/config.c
+++ b/firmware/usbstack/core/config.c
@@ -23,58 +23,58 @@
23#include <string.h> 23#include <string.h>
24#include "usbstack/core.h" 24#include "usbstack/core.h"
25 25
26static int usb_descriptor_fillbuf(void* buf, unsigned buflen, struct usb_descriptor_header** src) { 26static int usb_descriptor_fillbuf(void* buf, unsigned buflen, struct usb_descriptor_header** src)
27 27{
28 uint8_t* dest = buf; 28 uint8_t* dest = buf;
29 29
30 if (!src) { 30 if (!src) {
31 return -EINVAL; 31 return -EINVAL;
32 } 32 }
33 33
34 /* fill buffer from src[] until null descriptor ptr */ 34 /* fill buffer from src[] until null descriptor ptr */
35 for (; 0 != *src; src++) { 35 for (; 0 != *src; src++) {
36 unsigned len = (*src)->bLength; 36 unsigned len = (*src)->bLength;
37 37
38 logf("len: %d", len); 38 logf("len: %d", len);
39 39
40 if (len > buflen) 40 if (len > buflen)
41 return -EINVAL; 41 return -EINVAL;
42 memcpy(dest, *src, len); 42 memcpy(dest, *src, len);
43 buflen -= len; 43 buflen -= len;
44 dest += len; 44 dest += len;
45 } 45 }
46 return dest - (uint8_t *)buf; 46 return dest - (uint8_t *)buf;
47} 47}
48 48
49int usb_stack_configdesc(const struct usb_config_descriptor* config, void* buf, unsigned length, struct usb_descriptor_header** desc) { 49int usb_stack_configdesc(const struct usb_config_descriptor* config, void* buf, unsigned length, struct usb_descriptor_header** desc)
50 50{
51 struct usb_config_descriptor* cp = buf; 51 struct usb_config_descriptor* cp = buf;
52 int len; 52 int len;
53
54 if (length < USB_DT_CONFIG_SIZE || !desc) {
55 return -EINVAL;
56 }
57
58 /* config descriptor first */
59 *cp = *config;
60
61 /* then interface/endpoint/class/vendor/... */
62 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (uint8_t*)buf, length - USB_DT_CONFIG_SIZE, desc);
63
64 if (len < 0) {
65 return len;
66 }
53 67
54 if (length < USB_DT_CONFIG_SIZE || !desc) { 68 len += USB_DT_CONFIG_SIZE;
55 return -EINVAL; 69 if (len > 0xffff) {
56 } 70 return -EINVAL;
57 71 }
58 /* config descriptor first */
59 *cp = *config;
60 72
61 /* then interface/endpoint/class/vendor/... */ 73 /* patch up the config descriptor */
62 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (uint8_t*)buf, length - USB_DT_CONFIG_SIZE, desc); 74 cp->bLength = USB_DT_CONFIG_SIZE;
63 75 cp->bDescriptorType = USB_DT_CONFIG;
64 if (len < 0) { 76 cp->wTotalLength = len;
65 return len; 77 cp->bmAttributes |= USB_CONFIG_ATT_ONE;
66 }
67
68 len += USB_DT_CONFIG_SIZE;
69 if (len > 0xffff) {
70 return -EINVAL;
71 }
72 78
73 /* patch up the config descriptor */ 79 return len;
74 cp->bLength = USB_DT_CONFIG_SIZE;
75 cp->bDescriptorType = USB_DT_CONFIG;
76 cp->wTotalLength = len;
77 cp->bmAttributes |= USB_CONFIG_ATT_ONE;
78
79 return len;
80} 80}
diff --git a/firmware/usbstack/core/core.c b/firmware/usbstack/core/core.c
index 61b7f83636..94131ac3f1 100644
--- a/firmware/usbstack/core/core.c
+++ b/firmware/usbstack/core/core.c
@@ -39,26 +39,26 @@ static void bind_device_driver(struct usb_device_driver* driver);
39/** 39/**
40 * Initialize usb stack. 40 * Initialize usb stack.
41 */ 41 */
42void usb_stack_init(void) { 42void usb_stack_init(void)
43{
44 int i;
45 logf("usb_stack_init");
43 46
44 int i;
45 logf("usb_stack_init");
46
47 /* init datastructures */ 47 /* init datastructures */
48 usbcore.controller[0] = NULL; 48 usbcore.controller[0] = NULL;
49 usbcore.controller[1] = NULL; 49 usbcore.controller[1] = NULL;
50 usbcore.active_controller = NULL; 50 usbcore.active_controller = NULL;
51 usbcore.device_driver = NULL; 51 usbcore.device_driver = NULL;
52 usbcore.running = false; 52 usbcore.running = false;
53 53
54 memset(&device_driver_names, 0, USB_STACK_MAX_SETTINGS_NAME); 54 memset(&device_driver_names, 0, USB_STACK_MAX_SETTINGS_NAME);
55 55
56 /* init arrays */ 56 /* init arrays */
57 for (i = 0; i < NUM_DRIVERS; i++) { 57 for (i = 0; i < NUM_DRIVERS; i++) {
58 usbcore.device_drivers[i] = NULL; 58 usbcore.device_drivers[i] = NULL;
59 usbcore.host_drivers[i] = NULL; 59 usbcore.host_drivers[i] = NULL;
60 } 60 }
61 61
62 /* init controllers */ 62 /* init controllers */
63#if (USBSTACK_CAPS & CONTROLLER_DEVICE) 63#if (USBSTACK_CAPS & CONTROLLER_DEVICE)
64 usb_dcd_init(); 64 usb_dcd_init();
@@ -77,31 +77,31 @@ void usb_stack_init(void) {
77 * Start processing of usb stack. This function init 77 * Start processing of usb stack. This function init
78 * active usb controller. 78 * active usb controller.
79 */ 79 */
80void usb_stack_start(void) { 80void usb_stack_start(void)
81 81{
82 /* are we allready running? */ 82 /* are we allready running? */
83 if (usbcore.running) { 83 if (usbcore.running) {
84 logf("allready running!"); 84 logf("allready running!");
85 return; 85 return;
86 } 86 }
87 87
88 if (usbcore.active_controller == NULL) { 88 if (usbcore.active_controller == NULL) {
89 logf("no active controller!"); 89 logf("no active controller!");
90 return; 90 return;
91 } 91 }
92 92
93 /* forward to controller */ 93 /* forward to controller */
94 logf("starting controller"); 94 logf("starting controller");
95 usbcore.active_controller->start(); 95 usbcore.active_controller->start();
96 usbcore.running = true; 96 usbcore.running = true;
97 97
98 /* look if started controller is a device controller 98 /* look if started controller is a device controller
99 * and if it has a device driver bind to it */ 99 * and if it has a device driver bind to it */
100 logf("check for auto bind"); 100 logf("check for auto bind");
101 if (usbcore.active_controller->type == DEVICE) { 101 if (usbcore.active_controller->type == DEVICE) {
102 if (usbcore.active_controller->device_driver == NULL && usbcore.device_driver != NULL) { 102 if (usbcore.active_controller->device_driver == NULL && usbcore.device_driver != NULL) {
103 /* bind driver */ 103 /* bind driver */
104 logf("binding..."); 104 logf("binding...");
105 bind_device_driver(usbcore.device_driver); 105 bind_device_driver(usbcore.device_driver);
106 } 106 }
107 } 107 }
@@ -111,15 +111,15 @@ void usb_stack_start(void) {
111 * Stop processing of usb stack. This function shutsdown 111 * Stop processing of usb stack. This function shutsdown
112 * active usb controller. 112 * active usb controller.
113 */ 113 */
114void usb_stack_stop(void) { 114void usb_stack_stop(void)
115{
116 /* are we allready stopped? */
117 if (usbcore.running == false) {
118 return;
119 }
115 120
116 /* are we allready stopped? */
117 if (usbcore.running == false) {
118 return;
119 }
120
121 /* forward to controller */ 121 /* forward to controller */
122 usbcore.active_controller->stop(); 122 usbcore.active_controller->stop();
123 usbcore.running = false; 123 usbcore.running = false;
124} 124}
125 125
@@ -127,8 +127,8 @@ void usb_stack_stop(void) {
127 * Gets called by upper layers to indicate that there is 127 * Gets called by upper layers to indicate that there is
128 * an interrupt waiting for the controller. 128 * an interrupt waiting for the controller.
129 */ 129 */
130void usb_stack_irq(void) { 130void usb_stack_irq(void)
131 131{
132 /* simply notify usb controller */ 132 /* simply notify usb controller */
133 if (usbcore.active_controller != NULL && usbcore.active_controller->irq != NULL) { 133 if (usbcore.active_controller != NULL && usbcore.active_controller->irq != NULL) {
134 usbcore.active_controller->irq(); 134 usbcore.active_controller->irq();
@@ -140,7 +140,8 @@ void usb_stack_irq(void) {
140 * to call for maintanence. We need to check if a new device has connected, 140 * to call for maintanence. We need to check if a new device has connected,
141 * find suitable drivers for new devices. 141 * find suitable drivers for new devices.
142 */ 142 */
143void usb_stack_work(void) { 143void usb_stack_work(void)
144{
144 /* TODO will be used with host device controllers 145 /* TODO will be used with host device controllers
145 * and needs to be called in a loop (thread) */ 146 * and needs to be called in a loop (thread) */
146} 147}
@@ -153,8 +154,8 @@ void usb_stack_work(void) {
153 * @param ctrl pointer to controller to register. 154 * @param ctrl pointer to controller to register.
154 * @return 0 on success else a defined error code. 155 * @return 0 on success else a defined error code.
155 */ 156 */
156int usb_controller_register(struct usb_controller* ctrl) { 157int usb_controller_register(struct usb_controller* ctrl)
157 158{
158 if (ctrl == NULL) { 159 if (ctrl == NULL) {
159 return EINVAL; 160 return EINVAL;
160 } 161 }
@@ -220,21 +221,21 @@ int usb_controller_unregister(struct usb_controller* ctrl) {
220 * 221 *
221 * @param type of controller to activate. 222 * @param type of controller to activate.
222 */ 223 */
223void usb_controller_select(int type) { 224void usb_controller_select(int type)
224 225{
225 struct usb_controller* new = NULL; 226 struct usb_controller* new = NULL;
226 227
227 /* check if a controller of the wanted type is already loaded */ 228 /* check if a controller of the wanted type is already loaded */
228 if (usbcore.active_controller != NULL && (int)usbcore.active_controller->type == type) { 229 if (usbcore.active_controller != NULL && (int)usbcore.active_controller->type == type) {
229 logf("controller already set"); 230 logf("controller already set");
230 return; 231 return;
231 } 232 }
232 233
233 logf("usb_controller_select"); 234 logf("usb_controller_select");
234 logf(" -> type: %d", type); 235 logf(" -> type: %d", type);
236
237 usbcore.mode = type;
235 238
236 usbcore.mode = type;
237
238 switch (type) { 239 switch (type) {
239 case DEVICE: 240 case DEVICE:
240 new = usbcore.controller[0]; 241 new = usbcore.controller[0];
@@ -246,20 +247,20 @@ void usb_controller_select(int type) {
246 247
247 /* if there is only one controller, stop here */ 248 /* if there is only one controller, stop here */
248 if (new == NULL) { 249 if (new == NULL) {
249 logf("no suitable cntrl found"); 250 logf("no suitable cntrl found");
250 return; 251 return;
251 } 252 }
252 253
253 /* shutdown current used controller */ 254 /* shutdown current used controller */
254 if (usbcore.active_controller != NULL) { 255 if (usbcore.active_controller != NULL) {
255 logf("shuting down old one"); 256 logf("shuting down old one");
256 usbcore.active_controller->shutdown(); 257 usbcore.active_controller->shutdown();
257 } 258 }
258 259
259 /* set and init new controller */ 260 /* set and init new controller */
260 usbcore.active_controller = new; 261 usbcore.active_controller = new;
261 logf("init controller"); 262 logf("init controller");
262 usbcore.active_controller->init(); 263 usbcore.active_controller->init();
263} 264}
264 265
265int usb_stack_get_mode(void) { 266int usb_stack_get_mode(void) {
@@ -272,10 +273,10 @@ int usb_stack_get_mode(void) {
272 * @param driver pointer to an usb_device_driver struct. 273 * @param driver pointer to an usb_device_driver struct.
273 * @return 0 on success, else a defined error code. 274 * @return 0 on success, else a defined error code.
274 */ 275 */
275int usb_device_driver_register(struct usb_device_driver* driver) { 276int usb_device_driver_register(struct usb_device_driver* driver)
276 277{
277 int i; 278 int i;
278 279
279 if (driver == NULL) { 280 if (driver == NULL) {
280 return EINVAL; 281 return EINVAL;
281 } 282 }
@@ -284,109 +285,109 @@ int usb_device_driver_register(struct usb_device_driver* driver) {
284 logf("usb_stack: register usb driver"); 285 logf("usb_stack: register usb driver");
285 for (i = 0; i < NUM_DRIVERS; i++) { 286 for (i = 0; i < NUM_DRIVERS; i++) {
286 if (usbcore.device_drivers[i] == NULL) { 287 if (usbcore.device_drivers[i] == NULL) {
287 usbcore.device_drivers[i] = driver; 288 usbcore.device_drivers[i] = driver;
288 update_driver_names(device_driver_names); 289 update_driver_names(device_driver_names);
289 return 0; 290 return 0;
290 } 291 }
291 } 292 }
292 293
293 update_driver_names(device_driver_names); 294 update_driver_names(device_driver_names);
294 295
295 return 0; 296 return 0;
296} 297}
297 298
298int usb_device_driver_bind(const char* name) { 299int usb_device_driver_bind(const char* name) {
299 300
300 int i; 301 int i;
301 struct usb_device_driver *tmp = NULL; 302 struct usb_device_driver *tmp = NULL;
302 struct usb_device_driver *driver = NULL; 303 struct usb_device_driver *driver = NULL;
303 304
304 if (name == NULL) { 305 if (name == NULL) {
305 return EINVAL; 306 return EINVAL;
306 } 307 }
307 308
308 /* look for driver */ 309 /* look for driver */
309 logf("looking for driver %s", name); 310 logf("looking for driver %s", name);
310 for (i = 0; i < NUM_DRIVERS; i++) { 311 for (i = 0; i < NUM_DRIVERS; i++) {
311 tmp = usbcore.device_drivers[i]; 312 tmp = usbcore.device_drivers[i];
312 if (tmp != NULL && strcmp(name, tmp->name) == 0) { 313 if (tmp != NULL && strcmp(name, tmp->name) == 0) {
313 driver = tmp; 314 driver = tmp;
314 } 315 }
315 } 316 }
316 317
317 if (driver == NULL) { 318 if (driver == NULL) {
318 logf("no driver found"); 319 logf("no driver found");
319 return ENODRIVERFOUND; 320 return ENODRIVERFOUND;
320 } 321 }
321 322
322 /* look if there is an usb controller loaded */ 323 /* look if there is an usb controller loaded */
323 if (usbcore.active_controller == NULL) { 324 if (usbcore.active_controller == NULL) {
324 /* safe choosen driver and set it when controller starts */ 325 /* safe choosen driver and set it when controller starts */
325 usbcore.device_driver = driver; 326 usbcore.device_driver = driver;
326 327
327 } else { 328 } else {
328 329
329 /* we need to have an active dcd controller */ 330 /* we need to have an active dcd controller */
330 if (usbcore.active_controller->type != DEVICE) { 331 if (usbcore.active_controller->type != DEVICE) {
331 logf("wrong type"); 332 logf("wrong type");
332 return EWRONGCONTROLLERTYPE; 333 return EWRONGCONTROLLERTYPE;
333 } 334 }
334 335
335 /* bind driver to controller */ 336 /* bind driver to controller */
336 bind_device_driver(driver); 337 bind_device_driver(driver);
337 } 338 }
338 339
339 return 0; 340 return 0;
340} 341}
341 342
342void usb_device_driver_unbind(void) { 343void usb_device_driver_unbind(void) {
343 344
344 logf("usb_device_driver_unbind"); 345 logf("usb_device_driver_unbind");
345 if (usbcore.active_controller->device_driver != NULL) { 346 if (usbcore.active_controller->device_driver != NULL) {
346 usbcore.active_controller->device_driver->unbind(); 347 usbcore.active_controller->device_driver->unbind();
347 usbcore.active_controller->device_driver = NULL; 348 usbcore.active_controller->device_driver = NULL;
348 } 349 }
349 350
350 usbcore.device_driver = NULL; 351 usbcore.device_driver = NULL;
351} 352}
352 353
353static void update_driver_names(unsigned char* result) { 354static void update_driver_names(unsigned char* result) {
354 355
355 int i; 356 int i;
356 int pos = 0; 357 int pos = 0;
357 unsigned char terminator = ','; 358 unsigned char terminator = ',';
358 struct usb_device_driver* dd = NULL; 359 struct usb_device_driver* dd = NULL;
359 360
360 /* reset buffer, iterate through drivers and add to char array */ 361 /* reset buffer, iterate through drivers and add to char array */
361 memset(result, 0, USB_STACK_MAX_SETTINGS_NAME); 362 memset(result, 0, USB_STACK_MAX_SETTINGS_NAME);
362 for (i = 0; i < NUM_DRIVERS; i++) { 363 for (i = 0; i < NUM_DRIVERS; i++) {
363 int len; 364 int len;
364 dd = usbcore.device_drivers[i]; 365 dd = usbcore.device_drivers[i];
365 366
366 if (dd != NULL) { 367 if (dd != NULL) {
367 len = strlen(dd->name); 368 len = strlen(dd->name);
368 if (pos > 0) { 369 if (pos > 0) {
369 memcpy(result + pos, &terminator, 1); 370 memcpy(result + pos, &terminator, 1);
370 pos++; 371 pos++;
371 } 372 }
372 memcpy(result + pos, dd->name, len); 373 memcpy(result + pos, dd->name, len);
373 pos += len; 374 pos += len;
374 } 375 }
375 } 376 }
376} 377}
377 378
378static void bind_device_driver(struct usb_device_driver* driver) { 379static void bind_device_driver(struct usb_device_driver* driver) {
379 380
380 /* look if there is an old driver */ 381 /* look if there is an old driver */
381 if (usbcore.active_controller->device_driver != NULL) { 382 if (usbcore.active_controller->device_driver != NULL) {
382 usbcore.active_controller->device_driver->unbind(); 383 usbcore.active_controller->device_driver->unbind();
383 } 384 }
384 385
385 /* bind driver to controller */ 386 /* bind driver to controller */
386 usbcore.active_controller->device_driver = driver; 387 usbcore.active_controller->device_driver = driver;
387 388
388 /* init dirver */ 389 /* init dirver */
389 driver->bind(usbcore.active_controller->controller_ops); 390 driver->bind(usbcore.active_controller->controller_ops);
390} 391}
391 392
392 393
diff --git a/firmware/usbstack/core/epsetup.c b/firmware/usbstack/core/epsetup.c
index 6ae54fb9ac..36c24e6805 100644
--- a/firmware/usbstack/core/epsetup.c
+++ b/firmware/usbstack/core/epsetup.c
@@ -25,27 +25,27 @@
25 * 25 *
26 * Naming Convention for Endpoint Names 26 * Naming Convention for Endpoint Names
27 * 27 *
28 * - ep1, ep2, ... address is fixed, not direction or type 28 * - ep1, ep2, ... address is fixed, not direction or type
29 * - ep1in, ep2out, ... address and direction are fixed, not type 29 * - ep1in, ep2out, ... address and direction are fixed, not type
30 * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction 30 * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
31 * - ep1in-bulk, ep2out-iso, ... all three are fixed 31 * - ep1in-bulk, ep2out-iso, ... all three are fixed
32 * - ep-* ... no functionality restrictions 32 * - ep-* ... no functionality restrictions
33 * 33 *
34 * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal. 34 * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
35 * 35 *
36 */ 36 */
37static int ep_matches(struct usb_ep* ep, struct usb_endpoint_descriptor* desc); 37static int ep_matches(struct usb_ep* ep, struct usb_endpoint_descriptor* desc);
38 38
39void usb_ep_autoconfig_reset(void) { 39void usb_ep_autoconfig_reset(void)
40 40{
41 struct usb_ep* ep = NULL; 41 struct usb_ep* ep = NULL;
42 if (usbcore.active_controller == NULL) { 42 if (usbcore.active_controller == NULL) {
43 return; 43 return;
44 } 44 }
45 45
46 logf("resetting endpoints"); 46 logf("resetting endpoints");
47 list_for_each_entry(ep, &usbcore.active_controller->endpoints.list, list) { 47 list_for_each_entry(ep, &usbcore.active_controller->endpoints.list, list) {
48 logf("reset %s", ep->name); 48 logf("reset %s", ep->name);
49 ep->claimed = false; 49 ep->claimed = false;
50 } 50 }
51} 51}
@@ -55,48 +55,48 @@ void usb_ep_autoconfig_reset(void) {
55 * @param desc usb descritpro to use for seraching. 55 * @param desc usb descritpro to use for seraching.
56 * @return NULL or a valid endpoint. 56 * @return NULL or a valid endpoint.
57 */ 57 */
58struct usb_ep* usb_ep_autoconfig(struct usb_endpoint_descriptor* desc) { 58struct usb_ep* usb_ep_autoconfig(struct usb_endpoint_descriptor* desc)
59 59{
60 struct usb_ep* ep = NULL; 60 struct usb_ep* ep = NULL;
61 if (usbcore.active_controller == NULL) { 61 if (usbcore.active_controller == NULL) {
62 logf("active controller NULL"); 62 logf("active controller NULL");
63 return NULL; 63 return NULL;
64 } 64 }
65 65
66 list_for_each_entry(ep, &usbcore.active_controller->endpoints.list, list) { 66 list_for_each_entry(ep, &usbcore.active_controller->endpoints.list, list) {
67 if (ep_matches (ep, desc)) { 67 if (ep_matches (ep, desc)) {
68 return ep; 68 return ep;
69 } 69 }
70 } 70 }
71 71
72 return NULL; 72 return NULL;
73} 73}
74 74
75static int ep_matches(struct usb_ep* ep, struct usb_endpoint_descriptor* desc) { 75static int ep_matches(struct usb_ep* ep, struct usb_endpoint_descriptor* desc)
76 76{
77 uint8_t type; 77 uint8_t type;
78 const char* tmp; 78 const char* tmp;
79 uint16_t max; 79 uint16_t max;
80 80
81 /* endpoint already claimed? */ 81 /* endpoint already claimed? */
82 if (ep->claimed) { 82 if (ep->claimed) {
83 logf("!! claimed !!"); 83 logf("!! claimed !!");
84 return 0; 84 return 0;
85 } 85 }
86 86
87 /* only support ep0 for portable CONTROL traffic */ 87 /* only support ep0 for portable CONTROL traffic */
88 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; 88 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
89 if (type == USB_ENDPOINT_XFER_CONTROL) { 89 if (type == USB_ENDPOINT_XFER_CONTROL) {
90 logf("type == control"); 90 logf("type == control");
91 return 0; 91 return 0;
92 } 92 }
93 93
94 /* some other naming convention */ 94 /* some other naming convention */
95 if (ep->name[0] != 'e') { 95 if (ep->name[0] != 'e') {
96 logf("wrong name"); 96 logf("wrong name");
97 return 0; 97 return 0;
98 } 98 }
99 99
100 /* type-restriction: "-iso", "-bulk", or "-int". 100 /* type-restriction: "-iso", "-bulk", or "-int".
101 * direction-restriction: "in", "out". 101 * direction-restriction: "in", "out".
102 */ 102 */
@@ -111,20 +111,20 @@ static int ep_matches(struct usb_ep* ep, struct usb_endpoint_descriptor* desc) {
111 if (tmp[2] == 's') { // == "-iso" 111 if (tmp[2] == 's') { // == "-iso"
112 return 0; 112 return 0;
113 } 113 }
114 break; 114 break;
115 case USB_ENDPOINT_XFER_BULK: 115 case USB_ENDPOINT_XFER_BULK:
116 if (tmp[1] != 'b') { // != "-bulk" 116 if (tmp[1] != 'b') { // != "-bulk"
117 return 0; 117 return 0;
118 } 118 }
119 break; 119 break;
120 case USB_ENDPOINT_XFER_ISOC: 120 case USB_ENDPOINT_XFER_ISOC:
121 if (tmp[2] != 's') { // != "-iso" 121 if (tmp[2] != 's') { // != "-iso"
122 return 0; 122 return 0;
123 } 123 }
124 } 124 }
125 } else { 125 } else {
126 tmp = ep->name + strlen (ep->name); 126 tmp = ep->name + strlen (ep->name);
127 } 127 }
128 128
129 /* direction-restriction: "..in-..", "out-.." */ 129 /* direction-restriction: "..in-..", "out-.." */
130 tmp--; 130 tmp--;
@@ -132,7 +132,7 @@ static int ep_matches(struct usb_ep* ep, struct usb_endpoint_descriptor* desc) {
132 if (desc->bEndpointAddress & USB_DIR_IN) { 132 if (desc->bEndpointAddress & USB_DIR_IN) {
133 if ('n' != *tmp) { 133 if ('n' != *tmp) {
134 return 0; 134 return 0;
135 } 135 }
136 } else { 136 } else {
137 if ('t' != *tmp) { 137 if ('t' != *tmp) {
138 return 0; 138 return 0;
@@ -147,40 +147,40 @@ static int ep_matches(struct usb_ep* ep, struct usb_endpoint_descriptor* desc) {
147 * the usb spec fixes high speed bulk maxpacket at 512 bytes. 147 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
148 */ 148 */
149 max = 0x7ff & desc->wMaxPacketSize; 149 max = 0x7ff & desc->wMaxPacketSize;
150 150
151 switch (type) { 151 switch (type) {
152 case USB_ENDPOINT_XFER_INT: 152 case USB_ENDPOINT_XFER_INT:
153 /* INT: limit 64 bytes full speed, 1024 high speed */ 153 /* INT: limit 64 bytes full speed, 1024 high speed */
154 if ((usbcore.active_controller->speed != USB_SPEED_HIGH) && (max > 64)) { 154 if ((usbcore.active_controller->speed != USB_SPEED_HIGH) && (max > 64)) {
155 return 0; 155 return 0;
156 } 156 }
157 /* FALLTHROUGH */ 157 /* FALLTHROUGH */
158 158
159 case USB_ENDPOINT_XFER_ISOC: 159 case USB_ENDPOINT_XFER_ISOC:
160 if ((usbcore.active_controller->speed != USB_SPEED_HIGH) && (max > 1023)) { 160 if ((usbcore.active_controller->speed != USB_SPEED_HIGH) && (max > 1023)) {
161 return 0; 161 return 0;
162 } 162 }
163 break; 163 break;
164 } 164 }
165 165
166 /* MATCH!! */ 166 /* MATCH!! */
167 167
168 /* report address */ 168 /* report address */
169 desc->bEndpointAddress |= ep->ep_num; 169 desc->bEndpointAddress |= ep->ep_num;
170 170
171 /* report (variable) full speed bulk maxpacket */ 171 /* report (variable) full speed bulk maxpacket */
172 if (type == USB_ENDPOINT_XFER_BULK) { 172 if (type == USB_ENDPOINT_XFER_BULK) {
173 int size = max; 173 int size = max;
174 174
175 /* min() doesn't work on bitfields with gcc-3.5 */ 175 /* min() doesn't work on bitfields with gcc-3.5 */
176 if (size > 64) { 176 if (size > 64) {
177 size = 64; 177 size = 64;
178 } 178 }
179 desc->wMaxPacketSize = size; 179 desc->wMaxPacketSize = size;
180 } 180 }
181 181
182 /* save desc in endpoint */ 182 /* save desc in endpoint */
183 ep->desc = desc; 183 ep->desc = desc;
184 184
185 return 1; 185 return 1;
186} 186}
diff --git a/firmware/usbstack/core/utils.c b/firmware/usbstack/core/utils.c
index 2fb2695732..0ec4faa098 100644
--- a/firmware/usbstack/core/utils.c
+++ b/firmware/usbstack/core/utils.c
@@ -20,98 +20,98 @@
20#include <string.h> 20#include <string.h>
21#include "usbstack/core.h" 21#include "usbstack/core.h"
22 22
23void into_usb_ctrlrequest(struct usb_ctrlrequest* request) { 23void into_usb_ctrlrequest(struct usb_ctrlrequest* request)
24{
25 char* type = "";
26 char* req = "";
27 char* extra = 0;
24 28
25 char* type = ""; 29 logf("-usb request-");
26 char* req = "";
27 char* extra = 0;
28
29 logf("-usb request-");
30 /* check if packet is okay */ 30 /* check if packet is okay */
31 if (request->bRequestType == 0 && 31 if (request->bRequestType == 0 &&
32 request->bRequest == 0 && 32 request->bRequest == 0 &&
33 request->wValue == 0 && 33 request->wValue == 0 &&
34 request->wIndex == 0 && 34 request->wIndex == 0 &&
35 request->wLength == 0) { 35 request->wLength == 0) {
36 logf(" -> INVALID <-"); 36 logf(" -> INVALID <-");
37 return; 37 return;
38 } 38 }
39 39
40 switch (request->bRequestType & USB_TYPE_MASK) { 40 switch (request->bRequestType & USB_TYPE_MASK) {
41 case USB_TYPE_STANDARD: 41 case USB_TYPE_STANDARD:
42 type = "standard"; 42 type = "standard";
43 43
44 switch (request->bRequest) { 44 switch (request->bRequest) {
45 case USB_REQ_GET_STATUS: 45 case USB_REQ_GET_STATUS:
46 req = "get status"; 46 req = "get status";
47 break; 47 break;
48 case USB_REQ_CLEAR_FEATURE: 48 case USB_REQ_CLEAR_FEATURE:
49 req = "clear feature"; 49 req = "clear feature";
50 break; 50 break;
51 case USB_REQ_SET_FEATURE: 51 case USB_REQ_SET_FEATURE:
52 req = "set feature"; 52 req = "set feature";
53 break; 53 break;
54 case USB_REQ_SET_ADDRESS: 54 case USB_REQ_SET_ADDRESS:
55 req = "set address"; 55 req = "set address";
56 break; 56 break;
57 case USB_REQ_GET_DESCRIPTOR: 57 case USB_REQ_GET_DESCRIPTOR:
58 req = "get descriptor"; 58 req = "get descriptor";
59 59
60 switch (request->wValue >> 8) { 60 switch (request->wValue >> 8) {
61 case USB_DT_DEVICE: 61 case USB_DT_DEVICE:
62 extra = "get device descriptor"; 62 extra = "get device descriptor";
63 break; 63 break;
64 case USB_DT_DEVICE_QUALIFIER: 64 case USB_DT_DEVICE_QUALIFIER:
65 extra = "get device qualifier"; 65 extra = "get device qualifier";
66 break; 66 break;
67 case USB_DT_OTHER_SPEED_CONFIG: 67 case USB_DT_OTHER_SPEED_CONFIG:
68 extra = "get other-speed config descriptor"; 68 extra = "get other-speed config descriptor";
69 case USB_DT_CONFIG: 69 case USB_DT_CONFIG:
70 extra = "get configuration descriptor"; 70 extra = "get configuration descriptor";
71 break; 71 break;
72 case USB_DT_STRING: 72 case USB_DT_STRING:
73 extra = "get string descriptor"; 73 extra = "get string descriptor";
74 break; 74 break;
75 case USB_DT_DEBUG: 75 case USB_DT_DEBUG:
76 extra = "debug"; 76 extra = "debug";
77 break; 77 break;
78 } 78 }
79 break; 79 break;
80 80
81 break; 81 break;
82 case USB_REQ_SET_DESCRIPTOR: 82 case USB_REQ_SET_DESCRIPTOR:
83 req = "set descriptor"; 83 req = "set descriptor";
84 break; 84 break;
85 case USB_REQ_GET_CONFIGURATION: 85 case USB_REQ_GET_CONFIGURATION:
86 req = "get configuration"; 86 req = "get configuration";
87 break; 87 break;
88 case USB_REQ_SET_CONFIGURATION: 88 case USB_REQ_SET_CONFIGURATION:
89 req = "set configuration"; 89 req = "set configuration";
90 break; 90 break;
91 case USB_REQ_GET_INTERFACE: 91 case USB_REQ_GET_INTERFACE:
92 req = "get interface"; 92 req = "get interface";
93 break; 93 break;
94 case USB_REQ_SET_INTERFACE: 94 case USB_REQ_SET_INTERFACE:
95 req = "set interface"; 95 req = "set interface";
96 break; 96 break;
97 case USB_REQ_SYNCH_FRAME: 97 case USB_REQ_SYNCH_FRAME:
98 req = "sync frame"; 98 req = "sync frame";
99 break; 99 break;
100 default: 100 default:
101 req = "unkown"; 101 req = "unkown";
102 break; 102 break;
103 } 103 }
104 104
105 break; 105 break;
106 case USB_TYPE_CLASS: 106 case USB_TYPE_CLASS:
107 type = "class"; 107 type = "class";
108 break; 108 break;
109 109
110 case USB_TYPE_VENDOR: 110 case USB_TYPE_VENDOR:
111 type = "vendor"; 111 type = "vendor";
112 break; 112 break;
113 } 113 }
114 114
115 logf(" -b 0x%x", request->bRequestType); 115 logf(" -b 0x%x", request->bRequestType);
116 logf(" -b 0x%x", request->bRequest); 116 logf(" -b 0x%x", request->bRequest);
117 logf(" -b 0x%x", request->wValue); 117 logf(" -b 0x%x", request->wValue);
@@ -120,6 +120,6 @@ void into_usb_ctrlrequest(struct usb_ctrlrequest* request) {
120 logf(" -> t: %s", type); 120 logf(" -> t: %s", type);
121 logf(" -> r: %s", req); 121 logf(" -> r: %s", req);
122 if (extra != 0) { 122 if (extra != 0) {
123 logf(" -> e: %s", extra); 123 logf(" -> e: %s", extra);
124 } 124 }
125} 125}
diff --git a/firmware/usbstack/device.h b/firmware/usbstack/device.h
index 8993c9ba2f..cfdaa1ec97 100644
--- a/firmware/usbstack/device.h
+++ b/firmware/usbstack/device.h
@@ -41,7 +41,7 @@ struct usb_config_descriptor;
41struct usb_descriptor_header; 41struct usb_descriptor_header;
42 42
43int usb_stack_configdesc(const struct usb_config_descriptor* config, 43int usb_stack_configdesc(const struct usb_config_descriptor* config,
44 void* buf, unsigned length, 44 void* buf, unsigned length,
45 struct usb_descriptor_header** desc); 45 struct usb_descriptor_header** desc);
46 46
47#endif /*_USBSTACK_DEVICE_H_*/ 47#endif /*_USBSTACK_DEVICE_H_*/
diff --git a/firmware/usbstack/drivers/device/usb_serial.c b/firmware/usbstack/drivers/device/usb_serial.c
index fe1e52f25a..e44a76a936 100644
--- a/firmware/usbstack/drivers/device/usb_serial.c
+++ b/firmware/usbstack/drivers/device/usb_serial.c
@@ -128,69 +128,69 @@ static int set_config(int config);
128 128
129 129
130struct device { 130struct device {
131 struct usb_ep* in; 131 struct usb_ep* in;
132 struct usb_ep* out; 132 struct usb_ep* out;
133 uint32_t used_config; 133 uint32_t used_config;
134}; 134};
135 135
136static struct device dev; 136static struct device dev;
137 137
138/*-------------------------------------------------------------------------*/ 138/*-------------------------------------------------------------------------*/
139 139
140void usb_serial_driver_init(void) { 140void usb_serial_driver_init(void)
141 141{
142 logf("usb serial: register"); 142 logf("usb serial: register");
143 usb_device_driver_register(&usb_serial_driver); 143 usb_device_driver_register(&usb_serial_driver);
144} 144}
145 145
146/*-------------------------------------------------------------------------*/ 146/*-------------------------------------------------------------------------*/
147 147
148void usb_serial_driver_bind(void* controler_ops) { 148void usb_serial_driver_bind(void* controler_ops)
149 149{
150 logf("usb serial: bind"); 150 logf("usb serial: bind");
151 ops = controler_ops; 151 ops = controler_ops;
152 152
153 /* serach and asign endpoints */ 153 /* serach and asign endpoints */
154 usb_ep_autoconfig_reset(); 154 usb_ep_autoconfig_reset();
155 155
156 dev.in = usb_ep_autoconfig(&serial_fullspeed_in_desc); 156 dev.in = usb_ep_autoconfig(&serial_fullspeed_in_desc);
157 if (!dev.in) { 157 if (!dev.in) {
158 goto autoconf_fail; 158 goto autoconf_fail;
159 } 159 }
160 dev.in->claimed = true; 160 dev.in->claimed = true;
161 logf("usb serial: in: %s", dev.in->name); 161 logf("usb serial: in: %s", dev.in->name);
162 162
163 dev.out = usb_ep_autoconfig(&serial_fullspeed_out_desc); 163 dev.out = usb_ep_autoconfig(&serial_fullspeed_out_desc);
164 if (!dev.out) { 164 if (!dev.out) {
165 goto autoconf_fail; 165 goto autoconf_fail;
166 } 166 }
167 dev.out->claimed = true; 167 dev.out->claimed = true;
168 logf("usb serial: out: %s", dev.out->name); 168 logf("usb serial: out: %s", dev.out->name);
169 169
170 /* update device decsriptor */ 170 /* update device decsriptor */
171 serial_device_desc.bMaxPacketSize0 = ops->ep0->maxpacket; 171 serial_device_desc.bMaxPacketSize0 = ops->ep0->maxpacket;
172 172
173 /* update qualifie descriptor */ 173 /* update qualifie descriptor */
174 serial_qualifier_desc.bMaxPacketSize0 = ops->ep0->maxpacket; 174 serial_qualifier_desc.bMaxPacketSize0 = ops->ep0->maxpacket;
175 175
176 /* update debug descriptor */ 176 /* update debug descriptor */
177 serial_debug_desc.bDebugInEndpoint = dev.in->ep_num; 177 serial_debug_desc.bDebugInEndpoint = dev.in->ep_num;
178 serial_debug_desc.bDebugOutEndpoint = dev.out->ep_num; 178 serial_debug_desc.bDebugOutEndpoint = dev.out->ep_num;
179 179
180 return; 180 return;
181 181
182autoconf_fail: 182autoconf_fail:
183 logf("failed to find endpoiunts"); 183 logf("failed to find endpoiunts");
184} 184}
185 185
186int usb_serial_driver_request(struct usb_ctrlrequest* request) { 186int usb_serial_driver_request(struct usb_ctrlrequest* request)
187 187{
188 int ret = -EOPNOTSUPP; 188 int ret = -EOPNOTSUPP;
189 logf("usb serial: request"); 189 logf("usb serial: request");
190 190
191 res.length = 0; 191 res.length = 0;
192 res.buf = NULL; 192 res.buf = NULL;
193 193
194 switch (request->bRequestType & USB_TYPE_MASK) { 194 switch (request->bRequestType & USB_TYPE_MASK) {
195 case USB_TYPE_STANDARD: 195 case USB_TYPE_STANDARD:
196 196
@@ -205,26 +205,26 @@ int usb_serial_driver_request(struct usb_ctrlrequest* request) {
205 break; 205 break;
206 206
207 case USB_DT_DEVICE_QUALIFIER: 207 case USB_DT_DEVICE_QUALIFIER:
208 logf("usb serial: sending qualifier dec"); 208 logf("usb serial: sending qualifier dec");
209 ret = MIN(sizeof(struct usb_qualifier_descriptor), request->wLength); 209 ret = MIN(sizeof(struct usb_qualifier_descriptor), request->wLength);
210 res.buf = &serial_qualifier_desc; 210 res.buf = &serial_qualifier_desc;
211 211
212 case USB_DT_CONFIG: 212 case USB_DT_CONFIG:
213 logf("usb serial: sending config desc"); 213 logf("usb serial: sending config desc");
214 214
215 ret = config_buf(buf, request->wValue >> 8, request->wValue & 0xff); 215 ret = config_buf(buf, request->wValue >> 8, request->wValue & 0xff);
216 if (ret >= 0) { 216 if (ret >= 0) {
217 logf("%d, vs %d", request->wLength, ret); 217 logf("%d, vs %d", request->wLength, ret);
218 ret = MIN(request->wLength, (uint16_t)ret); 218 ret = MIN(request->wLength, (uint16_t)ret);
219 } 219 }
220 res.buf = buf; 220 res.buf = buf;
221 break; 221 break;
222 222
223 case USB_DT_DEBUG: 223 case USB_DT_DEBUG:
224 logf("usb serial: sending debug desc"); 224 logf("usb serial: sending debug desc");
225 ret = MIN(sizeof(struct usb_debug_descriptor), request->wLength); 225 ret = MIN(sizeof(struct usb_debug_descriptor), request->wLength);
226 res.buf = &serial_debug_desc; 226 res.buf = &serial_debug_desc;
227 break; 227 break;
228 } 228 }
229 break; 229 break;
230 230
@@ -232,9 +232,9 @@ int usb_serial_driver_request(struct usb_ctrlrequest* request) {
232 logf("usb serial: set configuration %d", request->wValue); 232 logf("usb serial: set configuration %d", request->wValue);
233 ret = set_config(request->wValue); 233 ret = set_config(request->wValue);
234 break; 234 break;
235 235
236 case USB_REQ_GET_CONFIGURATION: 236 case USB_REQ_GET_CONFIGURATION:
237 logf("usb serial: get configuration"); 237 logf("usb serial: get configuration");
238 ret = 1; 238 ret = 1;
239 res.buf = &dev.used_config; 239 res.buf = &dev.used_config;
240 break; 240 break;
@@ -245,56 +245,56 @@ int usb_serial_driver_request(struct usb_ctrlrequest* request) {
245 res.length = ret; 245 res.length = ret;
246 ret = ops->send(NULL, &res); 246 ret = ops->send(NULL, &res);
247 } 247 }
248 248
249 return ret; 249 return ret;
250} 250}
251 251
252void usb_serial_driver_speed(enum usb_device_speed speed) { 252void usb_serial_driver_speed(enum usb_device_speed speed)
253 253{
254 switch (speed) { 254 switch (speed) {
255 case USB_SPEED_LOW: 255 case USB_SPEED_LOW:
256 case USB_SPEED_FULL: 256 case USB_SPEED_FULL:
257 logf("usb serial: using fullspeed"); 257 logf("usb serial: using fullspeed");
258 break; 258 break;
259 case USB_SPEED_HIGH: 259 case USB_SPEED_HIGH:
260 logf("usb serial: using highspeed"); 260 logf("usb serial: using highspeed");
261 break; 261 break;
262 default: 262 default:
263 logf("speed: hmm"); 263 logf("speed: hmm");
264 break; 264 break;
265 } 265 }
266} 266}
267 267
268/*-------------------------------------------------------------------------*/ 268/*-------------------------------------------------------------------------*/
269/* helper functions */ 269/* helper functions */
270 270
271static int config_buf(uint8_t *buf, uint8_t type, unsigned index) { 271static int config_buf(uint8_t *buf, uint8_t type, unsigned index)
272 272{
273 int len; 273 int len;
274 274
275 /* TODO check index*/ 275 /* TODO check index*/
276 276
277 len = usb_stack_configdesc(&serial_bulk_config_desc, buf, BUFFER_SIZE, serial_bulk_fullspeed_function); 277 len = usb_stack_configdesc(&serial_bulk_config_desc, buf, BUFFER_SIZE, serial_bulk_fullspeed_function);
278 if (len < 0) { 278 if (len < 0) {
279 return len; 279 return len;
280 } 280 }
281 ((struct usb_config_descriptor *)buf)->bDescriptorType = type; 281 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
282 return len; 282 return len;
283} 283}
284 284
285static int set_config(int config) { 285static int set_config(int config)
286{
287 /* TODO check config*/
286 288
287 /* TODO check config*/
288
289 /* enable endpoints */ 289 /* enable endpoints */
290 logf("setup %s", dev.in->name); 290 logf("setup %s", dev.in->name);
291 ops->enable(dev.in); 291 ops->enable(dev.in);
292 logf("setup %s", dev.out->name); 292 logf("setup %s", dev.out->name);
293 ops->enable(dev.out); 293 ops->enable(dev.out);
294 294
295 /* store config */ 295 /* store config */
296 logf("using config %d", config); 296 logf("using config %d", config);
297 dev.used_config = config; 297 dev.used_config = config;
298 298
299 return 0; 299 return 0;
300} 300}
diff --git a/firmware/usbstack/drivers/device/usb_storage.c b/firmware/usbstack/drivers/device/usb_storage.c
index 0cb1f108b5..355fc4dfed 100644
--- a/firmware/usbstack/drivers/device/usb_storage.c
+++ b/firmware/usbstack/drivers/device/usb_storage.c
@@ -128,8 +128,8 @@ static int set_config(int config);
128 128
129/*-------------------------------------------------------------------------*/ 129/*-------------------------------------------------------------------------*/
130 130
131void usb_storage_driver_init(void) { 131void usb_storage_driver_init(void)
132 132{
133 logf("usb storage: register"); 133 logf("usb storage: register");
134 usb_device_driver_register(&usb_storage_driver); 134 usb_device_driver_register(&usb_storage_driver);
135} 135}
@@ -137,41 +137,41 @@ void usb_storage_driver_init(void) {
137/*-------------------------------------------------------------------------*/ 137/*-------------------------------------------------------------------------*/
138/* device driver ops */ 138/* device driver ops */
139 139
140void usb_storage_driver_bind(void* controler_ops) { 140void usb_storage_driver_bind(void* controler_ops)
141 141{
142 ops = controler_ops; 142 ops = controler_ops;
143 143
144 /* serach and asign endpoints */ 144 /* serach and asign endpoints */
145 usb_ep_autoconfig_reset(); 145 usb_ep_autoconfig_reset();
146 146
147 dev.in = usb_ep_autoconfig(&storage_bulk_in_desc); 147 dev.in = usb_ep_autoconfig(&storage_bulk_in_desc);
148 if (!dev.in) { 148 if (!dev.in) {
149 goto autoconf_fail; 149 goto autoconf_fail;
150 } 150 }
151 dev.in->claimed = true; 151 dev.in->claimed = true;
152 logf("usb storage: in: %s", dev.in->name); 152 logf("usb storage: in: %s", dev.in->name);
153 153
154 dev.out = usb_ep_autoconfig(&storage_bulk_out_desc); 154 dev.out = usb_ep_autoconfig(&storage_bulk_out_desc);
155 if (!dev.out) { 155 if (!dev.out) {
156 goto autoconf_fail; 156 goto autoconf_fail;
157 } 157 }
158 dev.out->claimed = true; 158 dev.out->claimed = true;
159 logf("usb storage: out: %s", dev.out->name); 159 logf("usb storage: out: %s", dev.out->name);
160 160
161 return; 161 return;
162 162
163autoconf_fail: 163autoconf_fail:
164 logf("failed to find endpoints"); 164 logf("failed to find endpoints");
165} 165}
166 166
167int usb_storage_driver_request(struct usb_ctrlrequest* request) { 167int usb_storage_driver_request(struct usb_ctrlrequest* request)
168 168{
169 int ret = -EOPNOTSUPP; 169 int ret = -EOPNOTSUPP;
170 logf("usb storage: request"); 170 logf("usb storage: request");
171 171
172 res.length = 0; 172 res.length = 0;
173 res.buf = NULL; 173 res.buf = NULL;
174 174
175 switch (request->bRequestType & USB_TYPE_MASK) { 175 switch (request->bRequestType & USB_TYPE_MASK) {
176 case USB_TYPE_STANDARD: 176 case USB_TYPE_STANDARD:
177 177
@@ -187,12 +187,12 @@ int usb_storage_driver_request(struct usb_ctrlrequest* request) {
187 187
188 case USB_DT_CONFIG: 188 case USB_DT_CONFIG:
189 logf("usb storage: sending config desc"); 189 logf("usb storage: sending config desc");
190 190
191 ret = config_buf(buf, request->wValue >> 8, request->wValue & 0xff); 191 ret = config_buf(buf, request->wValue >> 8, request->wValue & 0xff);
192 if (ret >= 0) { 192 if (ret >= 0) {
193 logf("%d, vs %d", request->wLength, ret); 193 logf("%d, vs %d", request->wLength, ret);
194 ret = MIN(request->wLength, (uint16_t)ret); 194 ret = MIN(request->wLength, (uint16_t)ret);
195 } 195 }
196 res.buf = buf; 196 res.buf = buf;
197 break; 197 break;
198 } 198 }
@@ -202,68 +202,68 @@ int usb_storage_driver_request(struct usb_ctrlrequest* request) {
202 logf("usb storage: set configuration %d", request->wValue); 202 logf("usb storage: set configuration %d", request->wValue);
203 ret = set_config(request->wValue); 203 ret = set_config(request->wValue);
204 break; 204 break;
205 205
206 case USB_REQ_SET_INTERFACE: 206 case USB_REQ_SET_INTERFACE:
207 logf("usb storage: set interface"); 207 logf("usb storage: set interface");
208 ret = 0; 208 ret = 0;
209 break; 209 break;
210 } 210 }
211 211
212 case USB_TYPE_CLASS: 212 case USB_TYPE_CLASS:
213 213
214 switch (request->bRequest) { 214 switch (request->bRequest) {
215 case USB_BULK_RESET_REQUEST: 215 case USB_BULK_RESET_REQUEST:
216 logf("usb storage: bulk reset"); 216 logf("usb storage: bulk reset");
217 break; 217 break;
218 218
219 case USB_BULK_GET_MAX_LUN_REQUEST: 219 case USB_BULK_GET_MAX_LUN_REQUEST:
220 logf("usb storage: get max lun"); 220 logf("usb storage: get max lun");
221 /* we support no LUNs (Logical Unit Number) */ 221 /* we support no LUNs (Logical Unit Number) */
222 buf[0] = 0; 222 buf[0] = 0;
223 ret = 1; 223 ret = 1;
224 break; 224 break;
225 } 225 }
226 break; 226 break;
227 } 227 }
228 228
229 if (ret >= 0) { 229 if (ret >= 0) {
230 res.length = ret; 230 res.length = ret;
231 ret = ops->send(NULL, &res); 231 ret = ops->send(NULL, &res);
232 } 232 }
233 233
234 return ret; 234 return ret;
235} 235}
236 236
237/*-------------------------------------------------------------------------*/ 237/*-------------------------------------------------------------------------*/
238/* S/GET CONFIGURATION helpers */ 238/* S/GET CONFIGURATION helpers */
239 239
240static int config_buf(uint8_t *buf, uint8_t type, unsigned index) { 240static int config_buf(uint8_t *buf, uint8_t type, unsigned index)
241 241{
242 int len; 242 int len;
243
244 /* only one configuration */
245 if (index != 0) {
246 return -EINVAL;
247 }
248
249 len = usb_stack_configdesc(&storage_config_desc, buf, BUFFER_SIZE, storage_fullspeed_function);
250 if (len < 0) {
251 return len;
252 }
253 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
254 return len;
255}
256 243
257static int set_config(int config) { 244 /* only one configuration */
245 if (index != 0) {
246 return -EINVAL;
247 }
258 248
249 len = usb_stack_configdesc(&storage_config_desc, buf, BUFFER_SIZE, storage_fullspeed_function);
250 if (len < 0) {
251 return len;
252 }
253 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
254 return len;
255}
256
257static int set_config(int config)
258{
259 /* enable endpoints */ 259 /* enable endpoints */
260 logf("setup %s", dev.in->name); 260 logf("setup %s", dev.in->name);
261 ops->enable(dev.in); 261 ops->enable(dev.in);
262 logf("setup %s", dev.out->name); 262 logf("setup %s", dev.out->name);
263 ops->enable(dev.out); 263 ops->enable(dev.out);
264 264
265 /* setup buffers */ 265 /* setup buffers */
266 266
267 return 0; 267 return 0;
268} 268}
269 269
diff --git a/firmware/usbstack/host.h b/firmware/usbstack/host.h
index cb2f550e46..862584d7cf 100644
--- a/firmware/usbstack/host.h
+++ b/firmware/usbstack/host.h
@@ -25,7 +25,7 @@
25 */ 25 */
26struct usb_host_driver { 26struct usb_host_driver {
27 const char* name; 27 const char* name;
28 void* data; /* used to store controller specific ops struct */ 28 void* data; /* used to store controller specific ops struct */
29}; 29};
30 30
31#endif /*_USBSTACK_HOST_H_*/ 31#endif /*_USBSTACK_HOST_H_*/