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_ENABLE_STORAGE)
35 #include "usb_storage.h"
38 #if defined(USB_ENABLE_SERIAL)
39 #include "usb_serial.h"
42 #if defined(USB_ENABLE_CHARGING_ONLY)
43 #include "usb_charging_only.h"
50 /* TODO: Move target-specific stuff somewhere else (serial number reading) */
57 #if !defined(HAVE_AS3514) && !defined(IPOD_ARCH) && (CONFIG_STORAGE & STORAGE_ATA)
61 #ifndef USB_MAX_CURRENT
62 #define USB_MAX_CURRENT 500
65 /*-------------------------------------------------------------------------*/
66 /* USB protocol descriptors: */
68 static struct usb_device_descriptor
__attribute__((aligned(2)))
71 .bLength
= sizeof(struct usb_device_descriptor
),
72 .bDescriptorType
= USB_DT_DEVICE
,
73 #ifndef USB_NO_HIGH_SPEED
78 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
81 .bMaxPacketSize0
= 64,
82 .idVendor
= USB_VENDOR_ID
,
83 .idProduct
= USB_PRODUCT_ID
,
88 .bNumConfigurations
= 1
91 static struct usb_config_descriptor
__attribute__((aligned(2)))
94 .bLength
= sizeof(struct usb_config_descriptor
),
95 .bDescriptorType
= USB_DT_CONFIG
,
96 .wTotalLength
= 0, /* will be filled in later */
98 .bConfigurationValue
= 1,
100 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
101 .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
,unsigned char* dest
);
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
] =
182 #ifdef USB_ENABLE_STORAGE
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
,
201 #ifdef USB_ENABLE_SERIAL
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_ENABLE_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
,
239 #ifdef USB_ENABLE_HID
242 .needs_exclusive_storage
= false,
243 .first_interface
= 0,
245 .request_endpoints
= usb_hid_request_endpoints
,
246 .set_first_interface
= usb_hid_set_first_interface
,
247 .get_config_descriptor
= usb_hid_get_config_descriptor
,
248 .init_connection
= usb_hid_init_connection
,
249 .init
= usb_hid_init
,
250 .disconnect
= usb_hid_disconnect
,
251 .transfer_complete
= usb_hid_transfer_complete
,
252 .control_request
= usb_hid_control_request
,
254 .notify_hotswap
= NULL
,
260 static void usb_core_control_request_handler(struct usb_ctrlrequest
* req
);
262 static unsigned char response_data
[256] USB_DEVBSS_ATTR
;
264 static short hex
[16] = {'0','1','2','3','4','5','6','7',
265 '8','9','A','B','C','D','E','F'};
267 static void set_serial_descriptor(void)
270 uint32_t* serial
= (uint32_t*)(0x20004034);
272 uint32_t* serial
= (uint32_t*)(0x20002034);
275 /* We need to convert from a little-endian 64-bit int
276 into a utf-16 string of hex characters */
277 short* p
= &usb_string_iSerial
.wString
[24];
281 for (i
= 0; i
< 2; i
++) {
288 usb_string_iSerial
.bLength
=52;
290 #elif defined(HAVE_AS3514)
291 static void set_serial_descriptor(void)
293 unsigned char serial
[16];
294 /* Align 32 digits right in the 40-digit serial number */
295 short* p
= &usb_string_iSerial
.wString
[1];
298 ascodec_readbytes(AS3514_UID_0
, 0x10, serial
);
299 for (i
= 0; i
< 16; i
++) {
300 *p
++ = hex
[(serial
[i
] >> 4) & 0xF];
301 *p
++ = hex
[(serial
[i
] >> 0) & 0xF];
303 usb_string_iSerial
.bLength
=68;
305 #elif (CONFIG_STORAGE & STORAGE_ATA)
306 /* If we don't know the device serial number, use the one
308 static void set_serial_descriptor(void)
310 short* p
= &usb_string_iSerial
.wString
[1];
311 unsigned short* identify
= ata_get_identify();
315 for (i
= 10; i
< 20; i
++) {
317 *p
++ = hex
[(x
>> 12) & 0xF];
318 *p
++ = hex
[(x
>> 8) & 0xF];
319 *p
++ = hex
[(x
>> 4) & 0xF];
320 *p
++ = hex
[(x
>> 0) & 0xF];
322 usb_string_iSerial
.bLength
=84;
324 #elif (CONFIG_STORAGE & STORAGE_RAMDISK)
325 /* This "serial number" isn't unique, but it should never actually
326 appear in non-testing use */
327 static void set_serial_descriptor(void)
329 short* p
= &usb_string_iSerial
.wString
[1];
331 for (i
= 0; i
< 16; i
++) {
332 *p
++ = hex
[(2*i
)&0xF];
333 *p
++ = hex
[(2*i
+1)&0xF];
335 usb_string_iSerial
.bLength
=68;
338 static void set_serial_descriptor(void)
340 device_descriptor
.iSerialNumber
= 0;
344 void usb_core_init(void)
352 /* class driver init functions should be safe to call even if the driver
353 * won't be used. This simplifies other logic (i.e. we don't need to know
354 * yet which drivers will be enabled */
355 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
356 if(drivers
[i
].init
!= NULL
)
362 logf("usb_core_init() finished");
365 void usb_core_exit(void)
368 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
369 if(drivers
[i
].enabled
&& drivers
[i
].disconnect
!= NULL
)
371 drivers
[i
].disconnect();
372 drivers
[i
].enabled
= false;
381 logf("usb_core_exit() finished");
384 void usb_core_handle_transfer_completion(
385 struct usb_transfer_completion_event_data
* event
)
387 completion_handler_t handler
;
388 int ep
= event
->endpoint
;
392 logf("ctrl handled %ld",current_tick
);
393 usb_core_control_request_handler(
394 (struct usb_ctrlrequest
*)event
->data
);
397 handler
= ep_data
[ep
].completion_handler
[EP_DIR(event
->dir
)];
399 handler(ep
,event
->dir
,event
->status
,event
->length
);
404 void usb_core_enable_driver(int driver
,bool enabled
)
406 drivers
[driver
].enabled
= enabled
;
409 bool usb_core_driver_enabled(int driver
)
411 return drivers
[driver
].enabled
;
414 bool usb_core_any_exclusive_storage(void)
417 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
418 if(drivers
[i
].enabled
&& drivers
[i
].needs_exclusive_storage
)
428 void usb_core_hotswap_event(int volume
,bool inserted
)
431 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
432 if(drivers
[i
].enabled
&& drivers
[i
].notify_hotswap
!=NULL
)
434 drivers
[i
].notify_hotswap(volume
,inserted
);
440 static void usb_core_set_serial_function_id(void)
444 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
445 if(drivers
[i
].enabled
)
448 usb_string_iSerial
.wString
[0] = hex
[id
];
451 int usb_core_request_endpoint(int type
, int dir
, struct usb_class_driver
* drv
)
455 ret
= usb_drv_request_endpoint(type
, dir
);
463 ep_data
[ep
].completion_handler
[dir
] = drv
->transfer_complete
;
464 ep_data
[ep
].control_handler
[dir
] = drv
->control_request
;
469 void usb_core_release_endpoint(int ep
)
473 usb_drv_release_endpoint(ep
);
478 ep_data
[ep
].completion_handler
[dir
] = NULL
;
479 ep_data
[ep
].control_handler
[dir
] = NULL
;
482 static void allocate_interfaces_and_endpoints(void)
487 memset(ep_data
,0,sizeof(ep_data
));
489 for (i
= 0; i
< USB_NUM_ENDPOINTS
; i
++) {
490 usb_drv_release_endpoint(i
| USB_DIR_OUT
);
491 usb_drv_release_endpoint(i
| USB_DIR_IN
);
494 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
495 if(drivers
[i
].enabled
) {
496 drivers
[i
].first_interface
= interface
;
498 if (drivers
[i
].request_endpoints(&drivers
[i
])) {
499 drivers
[i
].enabled
= false;
503 interface
= drivers
[i
].set_first_interface(interface
);
504 drivers
[i
].last_interface
= interface
;
507 usb_core_num_interfaces
= interface
;
510 static int usb_core_ack_control(struct usb_ctrlrequest
* req
)
512 if (req
->bRequestType
& USB_DIR_IN
)
513 return usb_drv_recv(EP_CONTROL
,NULL
,0);
515 return usb_drv_send(EP_CONTROL
,NULL
,0);
519 static void control_request_handler_drivers(struct usb_ctrlrequest
* req
)
521 int i
, interface
= req
->wIndex
;
524 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
525 if(drivers
[i
].enabled
&&
526 drivers
[i
].control_request
&&
527 drivers
[i
].first_interface
<= interface
&&
528 drivers
[i
].last_interface
> interface
)
530 handled
= drivers
[i
].control_request(req
, response_data
);
536 /* nope. flag error */
537 logf("bad req:desc %d:%d", req
->bRequest
, req
->wValue
>>8);
538 usb_drv_stall(EP_CONTROL
, true, true);
539 usb_core_ack_control(req
);
543 static void request_handler_device_get_descriptor(struct usb_ctrlrequest
* req
)
547 const void* ptr
= NULL
;
548 int length
= req
->wLength
;
549 int index
= req
->wValue
& 0xff;
551 switch(req
->wValue
>>8) { /* type */
553 ptr
= &device_descriptor
;
554 size
= sizeof(struct usb_device_descriptor
);
557 case USB_DT_OTHER_SPEED_CONFIG
:
558 case USB_DT_CONFIG
: {
559 int i
, max_packet_size
;
561 if(req
->wValue
>>8==USB_DT_CONFIG
) {
562 max_packet_size
=(usb_drv_port_speed() ? 512 : 64);
563 config_descriptor
.bDescriptorType
=USB_DT_CONFIG
;
566 max_packet_size
=(usb_drv_port_speed() ? 64 : 512);
567 config_descriptor
.bDescriptorType
=
568 USB_DT_OTHER_SPEED_CONFIG
;
570 size
= sizeof(struct usb_config_descriptor
);
572 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
573 if(drivers
[i
].enabled
&& drivers
[i
].get_config_descriptor
) {
574 size
+=drivers
[i
].get_config_descriptor(
575 &response_data
[size
],max_packet_size
);
578 config_descriptor
.bNumInterfaces
= usb_core_num_interfaces
;
579 config_descriptor
.wTotalLength
= (uint16_t)size
;
580 memcpy(&response_data
[0],&config_descriptor
,
581 sizeof(struct usb_config_descriptor
));
588 logf("STRING %d",index
);
589 if ((unsigned)index
< (sizeof(usb_strings
)/
590 sizeof(struct usb_string_descriptor
*))) {
591 size
= usb_strings
[index
]->bLength
;
592 ptr
= usb_strings
[index
];
595 logf("bad string id %d",index
);
596 usb_drv_stall(EP_CONTROL
,true,true);
600 case USB_DT_DEVICE_QUALIFIER
:
601 ptr
= &qualifier_descriptor
;
602 size
= sizeof(struct usb_qualifier_descriptor
);
608 control_request_handler_drivers(req
);
613 logf("data %d (%d)",size
,length
);
614 length
= MIN(size
,length
);
616 if (ptr
!= response_data
) {
617 memcpy(response_data
,ptr
,length
);
620 usb_drv_recv(EP_CONTROL
,NULL
,0);
621 usb_drv_send(EP_CONTROL
,response_data
,length
);
625 static void request_handler_device(struct usb_ctrlrequest
* req
)
629 switch(req
->bRequest
) {
630 case USB_REQ_GET_CONFIGURATION
: {
631 logf("usb_core: GET_CONFIG");
632 response_data
[0] = (usb_state
== ADDRESS
? 0 : 1);
633 usb_drv_recv(EP_CONTROL
,NULL
,0);
634 usb_drv_send(EP_CONTROL
, response_data
, 1);
637 case USB_REQ_SET_CONFIGURATION
: {
638 logf("usb_core: SET_CONFIG");
639 usb_drv_cancel_all_transfers();
641 usb_state
= CONFIGURED
;
642 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
643 if(drivers
[i
].enabled
&& drivers
[i
].init_connection
)
644 drivers
[i
].init_connection();
650 usb_drv_send(EP_CONTROL
,NULL
,0);
653 case USB_REQ_SET_ADDRESS
: {
654 unsigned char address
= req
->wValue
;
655 logf("usb_core: SET_ADR %d", address
);
656 usb_drv_send(EP_CONTROL
,NULL
,0);
657 usb_drv_cancel_all_transfers();
658 usb_address
= address
;
659 usb_drv_set_address(usb_address
);
663 case USB_REQ_GET_DESCRIPTOR
:
664 logf("usb_core: GET_DESC %d", req
->wValue
>>8);
665 request_handler_device_get_descriptor(req
);
667 case USB_REQ_CLEAR_FEATURE
:
669 case USB_REQ_SET_FEATURE
:
670 if(req
->wValue
==USB_DEVICE_TEST_MODE
) {
671 int mode
=req
->wIndex
>>8;
672 usb_drv_send(EP_CONTROL
,NULL
,0);
673 usb_drv_set_test_mode(mode
);
676 case USB_REQ_GET_STATUS
:
679 usb_drv_recv(EP_CONTROL
,NULL
,0);
680 usb_drv_send(EP_CONTROL
, response_data
, 2);
687 static void request_handler_interface_standard(struct usb_ctrlrequest
* req
)
689 switch (req
->bRequest
)
691 case USB_REQ_SET_INTERFACE
:
692 logf("usb_core: SET_INTERFACE");
693 usb_drv_send(EP_CONTROL
,NULL
,0);
696 case USB_REQ_GET_INTERFACE
:
697 logf("usb_core: GET_INTERFACE");
699 usb_drv_recv(EP_CONTROL
,NULL
,0);
700 usb_drv_send(EP_CONTROL
,response_data
,1);
702 case USB_REQ_CLEAR_FEATURE
:
704 case USB_REQ_SET_FEATURE
:
706 case USB_REQ_GET_STATUS
:
709 usb_drv_recv(EP_CONTROL
,NULL
,0);
710 usb_drv_send(EP_CONTROL
, response_data
, 2);
713 control_request_handler_drivers(req
);
718 static void request_handler_interface(struct usb_ctrlrequest
* req
)
720 switch(req
->bRequestType
& USB_TYPE_MASK
) {
721 case USB_TYPE_STANDARD
:
722 request_handler_interface_standard(req
);
725 control_request_handler_drivers(req
);
727 case USB_TYPE_VENDOR
:
732 static void request_handler_endpoint(struct usb_ctrlrequest
* req
)
734 switch (req
->bRequest
) {
735 case USB_REQ_CLEAR_FEATURE
:
736 if (req
->wValue
==USB_ENDPOINT_HALT
) {
737 usb_drv_stall(EP_NUM(req
->wIndex
), false, EP_DIR(req
->wIndex
));
739 usb_drv_send(EP_CONTROL
,NULL
,0);
741 case USB_REQ_SET_FEATURE
:
742 if (req
->wValue
==USB_ENDPOINT_HALT
) {
743 usb_drv_stall(EP_NUM(req
->wIndex
), true, EP_DIR(req
->wIndex
));
745 usb_drv_send(EP_CONTROL
,NULL
,0);
747 case USB_REQ_GET_STATUS
:
750 logf("usb_core: GET_STATUS");
752 response_data
[0]=usb_drv_stalled(EP_NUM(req
->wIndex
),
753 EP_DIR(req
->wIndex
));
755 usb_drv_recv(EP_CONTROL
,NULL
,0);
756 usb_drv_send(EP_CONTROL
,response_data
,2);
760 control_handler_t control_handler
;
763 ep_data
[EP_NUM(req
->wIndex
)].control_handler
[EP_CONTROL
];
764 if (!control_handler
)
767 handled
=control_handler(req
, response_data
);
769 /* nope. flag error */
770 logf("usb bad req %d",req
->bRequest
);
771 usb_drv_stall(EP_CONTROL
,true,true);
772 usb_core_ack_control(req
);
779 /* Handling USB requests starts here */
780 static void usb_core_control_request_handler(struct usb_ctrlrequest
* req
)
782 if (usb_state
==DEFAULT
) {
783 set_serial_descriptor();
784 usb_core_set_serial_function_id();
786 allocate_interfaces_and_endpoints();
789 switch(req
->bRequestType
& USB_RECIP_MASK
) {
790 case USB_RECIP_DEVICE
:
791 request_handler_device(req
);
793 case USB_RECIP_INTERFACE
:
794 request_handler_interface(req
);
796 case USB_RECIP_ENDPOINT
:
797 request_handler_endpoint(req
);
799 case USB_RECIP_OTHER
:
800 logf("unsupported recipient");
803 //logf("control handled");
806 /* called by usb_drv_int() */
807 void usb_core_bus_reset(void)
813 /* called by usb_drv_transfer_completed() */
814 void usb_core_transfer_complete(int endpoint
,int dir
,int status
,int length
)
816 struct usb_transfer_completion_event_data
*completion_event
;
820 /* already handled */
824 completion_event
=&ep_data
[endpoint
].completion_event
;
826 completion_event
->endpoint
=endpoint
;
827 completion_event
->dir
=dir
;
828 completion_event
->data
=0;
829 completion_event
->status
=status
;
830 completion_event
->length
=length
;
831 /* All other endoints. Let the thread deal with it */
832 usb_signal_transfer_completion(completion_event
);
837 /* called by usb_drv_int() */
838 void usb_core_control_request(struct usb_ctrlrequest
* req
)
840 struct usb_transfer_completion_event_data
* completion_event
=
841 &ep_data
[EP_CONTROL
].completion_event
;
843 completion_event
->endpoint
=EP_CONTROL
;
844 completion_event
->dir
=0;
845 completion_event
->data
=(void*)req
;
846 completion_event
->status
=0;
847 completion_event
->length
=0;
848 logf("ctrl received %ld",current_tick
);
849 usb_signal_transfer_completion(completion_event
);
852 #ifdef HAVE_USB_POWER
853 unsigned short usb_allowed_current()
855 return (usb_state
==CONFIGURED
) ? MAX(USB_MAX_CURRENT
, 100) : 100;