1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 by Bj�rn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
32 #include "usb_class_driver.h"
34 #if defined(USB_STORAGE)
35 #include "usb_storage.h"
38 #if defined(USB_SERIAL)
39 #include "usb_serial.h"
42 #if defined(USB_CHARGING_ONLY)
43 #include "usb_charging_only.h"
46 /* TODO: Move target-specific stuff somewhere else (serial number reading) */
53 #if !defined(HAVE_AS3514) && !defined(IPOD_ARCH) && (CONFIG_STORAGE & STORAGE_ATA)
57 #ifndef USB_MAX_CURRENT
58 #define USB_MAX_CURRENT 500
61 /*-------------------------------------------------------------------------*/
62 /* USB protocol descriptors: */
64 #define USB_SC_SCSI 0x06 /* Transparent */
65 #define USB_PROT_BULK 0x50 /* bulk only */
67 static const struct usb_device_descriptor
__attribute__((aligned(2)))
70 .bLength
= sizeof(struct usb_device_descriptor
),
71 .bDescriptorType
= USB_DT_DEVICE
,
72 #ifndef USB_NO_HIGH_SPEED
77 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
80 .bMaxPacketSize0
= 64,
81 .idVendor
= USB_VENDOR_ID
,
82 .idProduct
= USB_PRODUCT_ID
,
87 .bNumConfigurations
= 1
90 static struct usb_config_descriptor
__attribute__((aligned(2)))
93 .bLength
= sizeof(struct usb_config_descriptor
),
94 .bDescriptorType
= USB_DT_CONFIG
,
95 .wTotalLength
= 0, /* will be filled in later */
97 .bConfigurationValue
= 1,
99 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
100 .bMaxPower
= (USB_MAX_CURRENT
+1) / 2, /* In 2mA units */
104 static const struct usb_qualifier_descriptor
__attribute__((aligned(2)))
105 qualifier_descriptor
=
107 .bLength
= sizeof(struct usb_qualifier_descriptor
),
108 .bDescriptorType
= USB_DT_DEVICE_QUALIFIER
,
111 .bDeviceSubClass
= 0,
112 .bDeviceProtocol
= 0,
113 .bMaxPacketSize0
= 64,
114 .bNumConfigurations
= 1
117 static const struct usb_string_descriptor
__attribute__((aligned(2)))
118 usb_string_iManufacturer
=
122 {'R','o','c','k','b','o','x','.','o','r','g'}
125 static const struct usb_string_descriptor
__attribute__((aligned(2)))
126 usb_string_iProduct
=
130 {'R','o','c','k','b','o','x',' ',
131 'm','e','d','i','a',' ',
132 'p','l','a','y','e','r'}
135 static struct usb_string_descriptor
__attribute__((aligned(2)))
140 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0',
141 '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0',
142 '0','0','0','0','0','0','0','0','0'}
145 /* Generic for all targets */
147 /* this is stringid #0: languages supported */
148 static const struct usb_string_descriptor
__attribute__((aligned(2)))
153 {0x0409} /* LANGID US English */
156 static const struct usb_string_descriptor
* const usb_strings
[] =
159 &usb_string_iManufacturer
,
160 &usb_string_iProduct
,
164 static int usb_address
= 0;
165 static bool initialized
= false;
166 static enum { DEFAULT
, ADDRESS
, CONFIGURED
} usb_state
;
168 static int usb_core_num_interfaces
;
170 typedef void (*completion_handler_t
)(int ep
,int dir
, int status
, int length
);
171 typedef bool (*control_handler_t
)(struct usb_ctrlrequest
* req
);
175 completion_handler_t completion_handler
[2];
176 control_handler_t control_handler
[2];
177 struct usb_transfer_completion_event_data completion_event
;
178 } ep_data
[USB_NUM_ENDPOINTS
];
180 static struct usb_class_driver drivers
[USB_NUM_DRIVERS
] =
183 [USB_DRIVER_MASS_STORAGE
] = {
185 .needs_exclusive_storage
= true,
186 .first_interface
= 0,
188 .request_endpoints
= usb_storage_request_endpoints
,
189 .set_first_interface
= usb_storage_set_first_interface
,
190 .get_config_descriptor
= usb_storage_get_config_descriptor
,
191 .init_connection
= usb_storage_init_connection
,
192 .init
= usb_storage_init
,
193 .disconnect
= usb_storage_disconnect
,
194 .transfer_complete
= usb_storage_transfer_complete
,
195 .control_request
= usb_storage_control_request
,
197 .notify_hotswap
= usb_storage_notify_hotswap
,
202 [USB_DRIVER_SERIAL
] = {
204 .needs_exclusive_storage
= false,
205 .first_interface
= 0,
207 .request_endpoints
= usb_serial_request_endpoints
,
208 .set_first_interface
= usb_serial_set_first_interface
,
209 .get_config_descriptor
= usb_serial_get_config_descriptor
,
210 .init_connection
= usb_serial_init_connection
,
211 .init
= usb_serial_init
,
212 .disconnect
= usb_serial_disconnect
,
213 .transfer_complete
= usb_serial_transfer_complete
,
214 .control_request
= usb_serial_control_request
,
216 .notify_hotswap
= NULL
,
220 #ifdef USB_CHARGING_ONLY
221 [USB_DRIVER_CHARGING_ONLY
] = {
223 .needs_exclusive_storage
= false,
224 .first_interface
= 0,
226 .request_endpoints
= usb_charging_only_request_endpoints
,
227 .set_first_interface
= usb_charging_only_set_first_interface
,
228 .get_config_descriptor
= usb_charging_only_get_config_descriptor
,
229 .init_connection
= NULL
,
232 .transfer_complete
= NULL
,
233 .control_request
= NULL
,
235 .notify_hotswap
= NULL
,
241 static void usb_core_control_request_handler(struct usb_ctrlrequest
* req
);
243 static unsigned char response_data
[256] USB_DEVBSS_ATTR
;
246 static short hex
[16] = {'0','1','2','3','4','5','6','7',
247 '8','9','A','B','C','D','E','F'};
249 static void set_serial_descriptor(void)
252 uint32_t* serial
= (uint32_t*)(0x20004034);
254 uint32_t* serial
= (uint32_t*)(0x20002034);
257 /* We need to convert from a little-endian 64-bit int
258 into a utf-16 string of hex characters */
259 short* p
= &usb_string_iSerial
.wString
[24];
263 for (i
= 0; i
< 2; i
++) {
270 usb_string_iSerial
.bLength
=52;
272 #elif defined(HAVE_AS3514)
273 static void set_serial_descriptor(void)
275 unsigned char serial
[16];
276 /* Align 32 digits right in the 40-digit serial number */
277 short* p
= &usb_string_iSerial
.wString
[1];
280 ascodec_readbytes(AS3514_UID_0
, 0x10, serial
);
281 for (i
= 0; i
< 16; i
++) {
282 *p
++ = hex
[(serial
[i
] >> 4) & 0xF];
283 *p
++ = hex
[(serial
[i
] >> 0) & 0xF];
285 usb_string_iSerial
.bLength
=68;
287 #elif (CONFIG_STORAGE & STORAGE_ATA)
288 /* If we don't know the device serial number, use the one
290 static void set_serial_descriptor(void)
292 short* p
= &usb_string_iSerial
.wString
[1];
293 unsigned short* identify
= ata_get_identify();
297 for (i
= 10; i
< 20; i
++) {
299 *p
++ = hex
[(x
>> 12) & 0xF];
300 *p
++ = hex
[(x
>> 8) & 0xF];
301 *p
++ = hex
[(x
>> 4) & 0xF];
302 *p
++ = hex
[(x
>> 0) & 0xF];
304 usb_string_iSerial
.bLength
=84;
306 #elif (CONFIG_STORAGE & STORAGE_RAMDISK)
307 /* This "serial number" isn't unique, but it should never actually
308 appear in non-testing use */
309 static void set_serial_descriptor(void)
311 short* p
= &usb_string_iSerial
.wString
[1];
313 for (i
= 0; i
< 16; i
++) {
314 *p
++ = hex
[(2*i
)&0xF];
315 *p
++ = hex
[(2*i
+1)&0xF];
317 usb_string_iSerial
.bLength
=68;
320 #warning No proper set_serial_descriptor() implementation for this target
321 static void set_serial_descriptor(void)
323 short* p
= &usb_string_iSerial
.wString
[1];
325 for (i
= 0; i
< 16; i
++) {
326 *p
++ = hex
[(2*i
)&0xF];
327 *p
++ = hex
[(2*i
+1)&0xF];
329 usb_string_iSerial
.bLength
=68;
333 void usb_core_init(void)
341 /* class driver init functions should be safe to call even if the driver
342 * won't be used. This simplifies other logic (i.e. we don't need to know
343 * yet which drivers will be enabled */
344 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
345 if(drivers
[i
].enabled
&& drivers
[i
].init
!= NULL
)
351 logf("usb_core_init() finished");
354 void usb_core_exit(void)
357 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
358 if(drivers
[i
].enabled
&& drivers
[i
].disconnect
!= NULL
)
360 drivers
[i
].disconnect ();
361 drivers
[i
].enabled
= false;
370 logf("usb_core_exit() finished");
373 void usb_core_handle_transfer_completion(
374 struct usb_transfer_completion_event_data
* event
)
376 int ep
= event
->endpoint
;
380 logf("ctrl handled %ld",current_tick
);
381 usb_core_control_request_handler(
382 (struct usb_ctrlrequest
*)event
->data
);
385 if(ep_data
[ep
].completion_handler
[event
->dir
>>7] != NULL
)
386 ep_data
[ep
].completion_handler
[event
->dir
>>7](ep
,event
->dir
,
387 event
->status
,event
->length
);
392 void usb_core_enable_driver(int driver
,bool enabled
)
394 drivers
[driver
].enabled
= enabled
;
397 bool usb_core_driver_enabled(int driver
)
399 return drivers
[driver
].enabled
;
402 bool usb_core_any_exclusive_storage(void)
405 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
406 if(drivers
[i
].enabled
&&
407 drivers
[i
].needs_exclusive_storage
)
417 void usb_core_hotswap_event(int volume
,bool inserted
)
420 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
421 if(drivers
[i
].enabled
&&
422 drivers
[i
].notify_hotswap
!=NULL
)
424 drivers
[i
].notify_hotswap(volume
,inserted
);
430 static void usb_core_set_serial_function_id(void)
434 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
435 if(drivers
[i
].enabled
)
438 usb_string_iSerial
.wString
[0] = hex
[id
];
441 int usb_core_request_endpoint(int dir
, struct usb_class_driver
*drv
)
445 ret
= usb_drv_request_endpoint(dir
);
453 ep_data
[ep
].completion_handler
[dir
] = drv
->transfer_complete
;
454 ep_data
[ep
].control_handler
[dir
] = drv
->control_request
;
459 void usb_core_release_endpoint(int ep
)
463 usb_drv_release_endpoint(ep
);
468 ep_data
[ep
].completion_handler
[dir
] = NULL
;
469 ep_data
[ep
].control_handler
[dir
] = NULL
;
472 static void allocate_interfaces_and_endpoints(void)
477 memset(ep_data
,0,sizeof(ep_data
));
479 for (i
= 0; i
< USB_NUM_ENDPOINTS
; i
++) {
480 usb_drv_release_endpoint(i
| USB_DIR_OUT
);
481 usb_drv_release_endpoint(i
| USB_DIR_IN
);
484 for(i
=0; i
< USB_NUM_DRIVERS
; i
++) {
485 if(drivers
[i
].enabled
) {
486 drivers
[i
].first_interface
= interface
;
488 if (drivers
[i
].request_endpoints(&drivers
[i
])) {
489 drivers
[i
].enabled
= false;
493 interface
= drivers
[i
].set_first_interface(interface
);
494 drivers
[i
].last_interface
= interface
;
497 usb_core_num_interfaces
= interface
;
500 static void usb_core_control_request_handler(struct usb_ctrlrequest
* req
)
503 if(usb_state
== DEFAULT
) {
504 set_serial_descriptor();
505 usb_core_set_serial_function_id();
507 allocate_interfaces_and_endpoints();
510 switch(req
->bRequestType
& 0x1f) {
512 switch (req
->bRequest
) {
513 case USB_REQ_GET_CONFIGURATION
: {
514 logf("usb_core: GET_CONFIG");
515 if (usb_state
== ADDRESS
)
516 response_data
[0] = 0;
518 response_data
[0] = 1;
519 if(usb_drv_send(EP_CONTROL
, response_data
, 1)!= 0)
521 usb_core_ack_control(req
);
523 case USB_REQ_SET_CONFIGURATION
:
524 logf("usb_core: SET_CONFIG");
525 usb_drv_cancel_all_transfers();
527 usb_state
= CONFIGURED
;
528 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
529 if(drivers
[i
].enabled
&&
530 drivers
[i
].init_connection
!=NULL
)
532 drivers
[i
].init_connection();
539 usb_core_ack_control(req
);
542 case USB_REQ_SET_ADDRESS
: {
543 unsigned char address
= req
->wValue
;
544 logf("usb_core: SET_ADR %d", address
);
545 if(usb_core_ack_control(req
)!=0)
547 usb_drv_cancel_all_transfers();
548 usb_address
= address
;
549 usb_drv_set_address(usb_address
);
553 case USB_REQ_GET_DESCRIPTOR
: {
554 int index
= req
->wValue
& 0xff;
555 int length
= req
->wLength
;
557 const void* ptr
= NULL
;
558 logf("usb_core: GET_DESC %d", req
->wValue
>> 8);
560 switch (req
->wValue
>> 8) { /* type */
562 ptr
= &device_descriptor
;
563 size
= sizeof(struct usb_device_descriptor
);
566 case USB_DT_OTHER_SPEED_CONFIG
:
567 case USB_DT_CONFIG
: {
570 if(req
->wValue
>> 8 == USB_DT_CONFIG
) {
571 if(usb_drv_port_speed())
575 config_descriptor
.bDescriptorType
=USB_DT_CONFIG
;
578 if(usb_drv_port_speed())
582 config_descriptor
.bDescriptorType
=
583 USB_DT_OTHER_SPEED_CONFIG
;
585 size
= sizeof(struct usb_config_descriptor
);
587 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
588 if(drivers
[i
].enabled
&&
589 drivers
[i
].get_config_descriptor
)
591 size
+=drivers
[i
].get_config_descriptor(
592 &response_data
[size
],
596 config_descriptor
.bNumInterfaces
=
597 usb_core_num_interfaces
;
598 config_descriptor
.wTotalLength
= size
;
599 memcpy(&response_data
[0],&config_descriptor
,
600 sizeof(struct usb_config_descriptor
));
607 logf("STRING %d",index
);
608 if ((unsigned)index
< (sizeof(usb_strings
)/
609 sizeof(struct usb_string_descriptor
*)))
611 size
= usb_strings
[index
]->bLength
;
612 ptr
= usb_strings
[index
];
615 logf("bad string id %d", index
);
616 usb_drv_stall(EP_CONTROL
, true,true);
620 case USB_DT_DEVICE_QUALIFIER
:
621 ptr
= &qualifier_descriptor
;
622 size
= sizeof (struct usb_qualifier_descriptor
);
626 logf("bad desc %d", req
->wValue
>> 8);
627 usb_drv_stall(EP_CONTROL
, true,true);
632 length
= MIN(size
, length
);
634 if (ptr
!= response_data
) {
635 memcpy(response_data
, ptr
, length
);
638 if(usb_drv_send(EP_CONTROL
, response_data
, length
)!=0)
641 usb_core_ack_control(req
);
643 } /* USB_REQ_GET_DESCRIPTOR */
644 case USB_REQ_CLEAR_FEATURE
:
646 case USB_REQ_SET_FEATURE
:
647 if(req
->wValue
== 2) { /* TEST_MODE */
648 int mode
=req
->wIndex
>>8;
649 usb_core_ack_control(req
);
650 usb_drv_set_test_mode(mode
);
653 case USB_REQ_GET_STATUS
:
656 if(usb_drv_send(EP_CONTROL
, response_data
, 2)!=0)
658 usb_core_ack_control(req
);
664 case 1: /* Interface */
665 switch (req
->bRequest
) {
666 case USB_REQ_SET_INTERFACE
:
667 logf("usb_core: SET_INTERFACE");
668 usb_core_ack_control(req
);
671 case USB_REQ_GET_INTERFACE
:
672 logf("usb_core: GET_INTERFACE");
673 response_data
[0] = 0;
674 if(usb_drv_send(EP_CONTROL
, response_data
, 1)!=0)
676 usb_core_ack_control(req
);
678 case USB_REQ_CLEAR_FEATURE
:
680 case USB_REQ_SET_FEATURE
:
682 case USB_REQ_GET_STATUS
:
685 if(usb_drv_send(EP_CONTROL
, response_data
, 2)!=0)
687 usb_core_ack_control(req
);
691 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
692 if(drivers
[i
].enabled
&&
693 drivers
[i
].control_request
&&
694 drivers
[i
].first_interface
<= (req
->wIndex
) &&
695 drivers
[i
].last_interface
> (req
->wIndex
))
697 handled
= drivers
[i
].control_request(req
);
701 /* nope. flag error */
702 logf("usb bad req %d", req
->bRequest
);
703 usb_drv_stall(EP_CONTROL
, true,true);
704 usb_core_ack_control(req
);
710 case 2: /* Endpoint */
711 switch (req
->bRequest
) {
712 case USB_REQ_CLEAR_FEATURE
:
713 if (req
->wValue
== 0 ) /* ENDPOINT_HALT */
714 usb_drv_stall(req
->wIndex
& 0xf, false,
715 (req
->wIndex
& 0x80) !=0);
716 usb_core_ack_control(req
);
718 case USB_REQ_SET_FEATURE
:
719 if (req
->wValue
== 0 ) /* ENDPOINT_HALT */
720 usb_drv_stall(req
->wIndex
& 0xf, true,
721 (req
->wIndex
& 0x80) !=0);
722 usb_core_ack_control(req
);
724 case USB_REQ_GET_STATUS
:
727 logf("usb_core: GET_STATUS");
729 response_data
[0] = usb_drv_stalled(req
->wIndex
&0xf,
730 (req
->wIndex
&0x80)!=0);
731 if(usb_drv_send(EP_CONTROL
, response_data
, 2)!=0)
733 usb_core_ack_control(req
);
737 if(ep_data
[req
->wIndex
& 0xf].control_handler
[0] != NULL
)
738 handled
= ep_data
[req
->wIndex
& 0xf].control_handler
[0](req
);
740 /* nope. flag error */
741 logf("usb bad req %d", req
->bRequest
);
742 usb_drv_stall(EP_CONTROL
, true,true);
743 usb_core_ack_control(req
);
749 logf("control handled");
752 /* called by usb_drv_int() */
753 void usb_core_bus_reset(void)
759 /* called by usb_drv_transfer_completed() */
760 void usb_core_transfer_complete(int endpoint
, int dir
, int status
,int length
)
764 /* already handled */
768 ep_data
[endpoint
].completion_event
.endpoint
=endpoint
;
769 ep_data
[endpoint
].completion_event
.dir
=dir
;
770 ep_data
[endpoint
].completion_event
.data
=0;
771 ep_data
[endpoint
].completion_event
.status
=status
;
772 ep_data
[endpoint
].completion_event
.length
=length
;
773 /* All other endoints. Let the thread deal with it */
774 usb_signal_transfer_completion(&ep_data
[endpoint
].completion_event
);
779 /* called by usb_drv_int() */
780 void usb_core_control_request(struct usb_ctrlrequest
* req
)
782 ep_data
[0].completion_event
.endpoint
=0;
783 ep_data
[0].completion_event
.dir
=0;
784 ep_data
[0].completion_event
.data
=(void *)req
;
785 ep_data
[0].completion_event
.status
=0;
786 ep_data
[0].completion_event
.length
=0;
787 logf("ctrl received %ld",current_tick
);
788 usb_signal_transfer_completion(&ep_data
[0].completion_event
);
791 int usb_core_ack_control(struct usb_ctrlrequest
* req
)
793 if (req
->bRequestType
& 0x80)
794 return usb_drv_recv(EP_CONTROL
, NULL
, 0);
796 return usb_drv_send(EP_CONTROL
, NULL
, 0);
799 #ifdef HAVE_USB_POWER
800 unsigned short usb_allowed_current()
802 if (usb_state
== CONFIGURED
)
804 return MAX(USB_MAX_CURRENT
, 100);