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"
46 #if defined(USB_ENABLE_HID)
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 #define USB_SC_SCSI 0x06 /* Transparent */
69 #define USB_PROT_BULK 0x50 /* bulk only */
71 static struct usb_device_descriptor
__attribute__((aligned(2)))
74 .bLength
= sizeof(struct usb_device_descriptor
),
75 .bDescriptorType
= USB_DT_DEVICE
,
76 #ifndef USB_NO_HIGH_SPEED
81 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
84 .bMaxPacketSize0
= 64,
85 .idVendor
= USB_VENDOR_ID
,
86 .idProduct
= USB_PRODUCT_ID
,
91 .bNumConfigurations
= 1
94 static struct usb_config_descriptor
__attribute__((aligned(2)))
97 .bLength
= sizeof(struct usb_config_descriptor
),
98 .bDescriptorType
= USB_DT_CONFIG
,
99 .wTotalLength
= 0, /* will be filled in later */
101 .bConfigurationValue
= 1,
103 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
104 .bMaxPower
= (USB_MAX_CURRENT
+1) / 2, /* In 2mA units */
107 static const struct usb_qualifier_descriptor
__attribute__((aligned(2)))
108 qualifier_descriptor
=
110 .bLength
= sizeof(struct usb_qualifier_descriptor
),
111 .bDescriptorType
= USB_DT_DEVICE_QUALIFIER
,
114 .bDeviceSubClass
= 0,
115 .bDeviceProtocol
= 0,
116 .bMaxPacketSize0
= 64,
117 .bNumConfigurations
= 1
120 static const struct usb_string_descriptor
__attribute__((aligned(2)))
121 usb_string_iManufacturer
=
125 {'R','o','c','k','b','o','x','.','o','r','g'}
128 static const struct usb_string_descriptor
__attribute__((aligned(2)))
129 usb_string_iProduct
=
133 {'R','o','c','k','b','o','x',' ',
134 'm','e','d','i','a',' ',
135 'p','l','a','y','e','r'}
138 static struct usb_string_descriptor
__attribute__((aligned(2)))
143 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0',
144 '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0',
145 '0','0','0','0','0','0','0','0','0'}
148 /* Generic for all targets */
150 /* this is stringid #0: languages supported */
151 static const struct usb_string_descriptor
__attribute__((aligned(2)))
156 {0x0409} /* LANGID US English */
159 static const struct usb_string_descriptor
* const usb_strings
[] =
162 &usb_string_iManufacturer
,
163 &usb_string_iProduct
,
167 static int usb_address
= 0;
168 static bool initialized
= false;
169 static enum { DEFAULT
, ADDRESS
, CONFIGURED
} usb_state
;
171 static int usb_core_num_interfaces
;
173 typedef void (*completion_handler_t
)(int ep
,int dir
,int status
,int length
);
174 typedef bool (*control_handler_t
)(struct usb_ctrlrequest
* req
,unsigned char* dest
);
178 completion_handler_t completion_handler
[2];
179 control_handler_t control_handler
[2];
180 struct usb_transfer_completion_event_data completion_event
;
181 } ep_data
[USB_NUM_ENDPOINTS
];
183 static struct usb_class_driver drivers
[USB_NUM_DRIVERS
] =
185 #ifdef USB_ENABLE_STORAGE
186 [USB_DRIVER_MASS_STORAGE
] = {
188 .needs_exclusive_storage
= true,
189 .first_interface
= 0,
191 .request_endpoints
= usb_storage_request_endpoints
,
192 .set_first_interface
= usb_storage_set_first_interface
,
193 .get_config_descriptor
= usb_storage_get_config_descriptor
,
194 .init_connection
= usb_storage_init_connection
,
195 .init
= usb_storage_init
,
196 .disconnect
= usb_storage_disconnect
,
197 .transfer_complete
= usb_storage_transfer_complete
,
198 .control_request
= usb_storage_control_request
,
200 .notify_hotswap
= usb_storage_notify_hotswap
,
204 #ifdef USB_ENABLE_SERIAL
205 [USB_DRIVER_SERIAL
] = {
207 .needs_exclusive_storage
= false,
208 .first_interface
= 0,
210 .request_endpoints
= usb_serial_request_endpoints
,
211 .set_first_interface
= usb_serial_set_first_interface
,
212 .get_config_descriptor
= usb_serial_get_config_descriptor
,
213 .init_connection
= usb_serial_init_connection
,
214 .init
= usb_serial_init
,
215 .disconnect
= usb_serial_disconnect
,
216 .transfer_complete
= usb_serial_transfer_complete
,
217 .control_request
= usb_serial_control_request
,
219 .notify_hotswap
= NULL
,
223 #ifdef USB_ENABLE_CHARGING_ONLY
224 [USB_DRIVER_CHARGING_ONLY
] = {
226 .needs_exclusive_storage
= false,
227 .first_interface
= 0,
229 .request_endpoints
= usb_charging_only_request_endpoints
,
230 .set_first_interface
= usb_charging_only_set_first_interface
,
231 .get_config_descriptor
= usb_charging_only_get_config_descriptor
,
232 .init_connection
= NULL
,
235 .transfer_complete
= NULL
,
236 .control_request
= NULL
,
238 .notify_hotswap
= NULL
,
242 #ifdef USB_ENABLE_HID
245 .needs_exclusive_storage
= false,
246 .first_interface
= 0,
248 .request_endpoints
= usb_hid_request_endpoints
,
249 .set_first_interface
= usb_hid_set_first_interface
,
250 .get_config_descriptor
= usb_hid_get_config_descriptor
,
251 .init_connection
= usb_hid_init_connection
,
252 .init
= usb_hid_init
,
253 .disconnect
= usb_hid_disconnect
,
254 .transfer_complete
= usb_hid_transfer_complete
,
255 .control_request
= usb_hid_control_request
,
257 .notify_hotswap
= NULL
,
263 static void usb_core_control_request_handler(struct usb_ctrlrequest
* req
);
265 static unsigned char response_data
[256] USB_DEVBSS_ATTR
;
267 static short hex
[16] = {'0','1','2','3','4','5','6','7',
268 '8','9','A','B','C','D','E','F'};
270 static void set_serial_descriptor(void)
273 uint32_t* serial
= (uint32_t*)(0x20004034);
275 uint32_t* serial
= (uint32_t*)(0x20002034);
278 /* We need to convert from a little-endian 64-bit int
279 into a utf-16 string of hex characters */
280 short* p
= &usb_string_iSerial
.wString
[24];
284 for (i
= 0; i
< 2; i
++) {
291 usb_string_iSerial
.bLength
=52;
293 #elif defined(HAVE_AS3514)
294 static void set_serial_descriptor(void)
296 unsigned char serial
[16];
297 /* Align 32 digits right in the 40-digit serial number */
298 short* p
= &usb_string_iSerial
.wString
[1];
301 ascodec_readbytes(AS3514_UID_0
, 0x10, serial
);
302 for (i
= 0; i
< 16; i
++) {
303 *p
++ = hex
[(serial
[i
] >> 4) & 0xF];
304 *p
++ = hex
[(serial
[i
] >> 0) & 0xF];
306 usb_string_iSerial
.bLength
=68;
308 #elif (CONFIG_STORAGE & STORAGE_ATA)
309 /* If we don't know the device serial number, use the one
311 static void set_serial_descriptor(void)
313 short* p
= &usb_string_iSerial
.wString
[1];
314 unsigned short* identify
= ata_get_identify();
318 for (i
= 10; i
< 20; i
++) {
320 *p
++ = hex
[(x
>> 12) & 0xF];
321 *p
++ = hex
[(x
>> 8) & 0xF];
322 *p
++ = hex
[(x
>> 4) & 0xF];
323 *p
++ = hex
[(x
>> 0) & 0xF];
325 usb_string_iSerial
.bLength
=84;
327 #elif (CONFIG_STORAGE & STORAGE_RAMDISK)
328 /* This "serial number" isn't unique, but it should never actually
329 appear in non-testing use */
330 static void set_serial_descriptor(void)
332 short* p
= &usb_string_iSerial
.wString
[1];
334 for (i
= 0; i
< 16; i
++) {
335 *p
++ = hex
[(2*i
)&0xF];
336 *p
++ = hex
[(2*i
+1)&0xF];
338 usb_string_iSerial
.bLength
=68;
341 static void set_serial_descriptor(void)
343 device_descriptor
.iSerialNumber
= 0;
347 void usb_core_init(void)
355 /* class driver init functions should be safe to call even if the driver
356 * won't be used. This simplifies other logic (i.e. we don't need to know
357 * yet which drivers will be enabled */
358 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
359 if(drivers
[i
].init
!= NULL
)
365 logf("usb_core_init() finished");
368 void usb_core_exit(void)
371 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
372 if(drivers
[i
].enabled
&& drivers
[i
].disconnect
!= NULL
)
374 drivers
[i
].disconnect();
375 drivers
[i
].enabled
= false;
384 logf("usb_core_exit() finished");
387 void usb_core_handle_transfer_completion(
388 struct usb_transfer_completion_event_data
* event
)
390 completion_handler_t handler
;
391 int ep
= event
->endpoint
;
395 logf("ctrl handled %ld",current_tick
);
396 usb_core_control_request_handler(
397 (struct usb_ctrlrequest
*)event
->data
);
400 handler
= ep_data
[ep
].completion_handler
[EP_DIR(event
->dir
)];
402 handler(ep
,event
->dir
,event
->status
,event
->length
);
407 void usb_core_enable_driver(int driver
,bool enabled
)
409 drivers
[driver
].enabled
= enabled
;
412 bool usb_core_driver_enabled(int driver
)
414 return drivers
[driver
].enabled
;
417 bool usb_core_any_exclusive_storage(void)
420 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
421 if(drivers
[i
].enabled
&& drivers
[i
].needs_exclusive_storage
)
431 void usb_core_hotswap_event(int volume
,bool inserted
)
434 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
435 if(drivers
[i
].enabled
&& drivers
[i
].notify_hotswap
!=NULL
)
437 drivers
[i
].notify_hotswap(volume
,inserted
);
443 static void usb_core_set_serial_function_id(void)
447 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
448 if(drivers
[i
].enabled
)
451 usb_string_iSerial
.wString
[0] = hex
[id
];
454 int usb_core_request_endpoint(int type
, int dir
, struct usb_class_driver
* drv
)
458 ret
= usb_drv_request_endpoint(type
, dir
);
466 ep_data
[ep
].completion_handler
[dir
] = drv
->transfer_complete
;
467 ep_data
[ep
].control_handler
[dir
] = drv
->control_request
;
472 void usb_core_release_endpoint(int ep
)
476 usb_drv_release_endpoint(ep
);
481 ep_data
[ep
].completion_handler
[dir
] = NULL
;
482 ep_data
[ep
].control_handler
[dir
] = NULL
;
485 static void allocate_interfaces_and_endpoints(void)
490 memset(ep_data
,0,sizeof(ep_data
));
492 for (i
= 0; i
< USB_NUM_ENDPOINTS
; i
++) {
493 usb_drv_release_endpoint(i
| USB_DIR_OUT
);
494 usb_drv_release_endpoint(i
| USB_DIR_IN
);
497 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
498 if(drivers
[i
].enabled
) {
499 drivers
[i
].first_interface
= interface
;
501 if (drivers
[i
].request_endpoints(&drivers
[i
])) {
502 drivers
[i
].enabled
= false;
506 interface
= drivers
[i
].set_first_interface(interface
);
507 drivers
[i
].last_interface
= interface
;
510 usb_core_num_interfaces
= interface
;
513 static void control_request_handler_drivers(struct usb_ctrlrequest
* req
)
515 int i
, interface
= req
->wIndex
;
518 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
519 if(drivers
[i
].enabled
&&
520 drivers
[i
].control_request
&&
521 drivers
[i
].first_interface
<= interface
&&
522 drivers
[i
].last_interface
> interface
)
524 handled
= drivers
[i
].control_request(req
, response_data
);
530 /* nope. flag error */
531 logf("bad req:desc %d:%d", req
->bRequest
, req
->wValue
>>8);
532 usb_drv_stall(EP_CONTROL
, true, true);
533 usb_core_ack_control(req
);
537 static void request_handler_device_get_descriptor(struct usb_ctrlrequest
* req
)
541 const void* ptr
= NULL
;
542 int length
= req
->wLength
;
543 int index
= req
->wValue
& 0xff;
545 switch(req
->wValue
>>8) { /* type */
547 ptr
= &device_descriptor
;
548 size
= sizeof(struct usb_device_descriptor
);
551 case USB_DT_OTHER_SPEED_CONFIG
:
552 case USB_DT_CONFIG
: {
553 int i
, max_packet_size
;
555 if(req
->wValue
>>8==USB_DT_CONFIG
) {
556 max_packet_size
=(usb_drv_port_speed() ? 512 : 64);
557 config_descriptor
.bDescriptorType
=USB_DT_CONFIG
;
560 max_packet_size
=(usb_drv_port_speed() ? 64 : 512);
561 config_descriptor
.bDescriptorType
=
562 USB_DT_OTHER_SPEED_CONFIG
;
564 size
= sizeof(struct usb_config_descriptor
);
566 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
567 if(drivers
[i
].enabled
&& drivers
[i
].get_config_descriptor
) {
568 size
+=drivers
[i
].get_config_descriptor(
569 &response_data
[size
],max_packet_size
);
572 config_descriptor
.bNumInterfaces
= usb_core_num_interfaces
;
573 config_descriptor
.wTotalLength
= size
;
574 memcpy(&response_data
[0],&config_descriptor
,
575 sizeof(struct usb_config_descriptor
));
582 logf("STRING %d",index
);
583 if ((unsigned)index
< (sizeof(usb_strings
)/
584 sizeof(struct usb_string_descriptor
*))) {
585 size
= usb_strings
[index
]->bLength
;
586 ptr
= usb_strings
[index
];
589 logf("bad string id %d",index
);
590 usb_drv_stall(EP_CONTROL
,true,true);
594 case USB_DT_DEVICE_QUALIFIER
:
595 ptr
= &qualifier_descriptor
;
596 size
= sizeof(struct usb_qualifier_descriptor
);
602 control_request_handler_drivers(req
);
607 logf("data %d (%d)",size
,length
);
608 length
= MIN(size
,length
);
610 if (ptr
!= response_data
) {
611 memcpy(response_data
,ptr
,length
);
614 if(usb_drv_send(EP_CONTROL
,response_data
,length
))
618 usb_core_ack_control(req
);
621 static void request_handler_device(struct usb_ctrlrequest
* req
)
625 switch(req
->bRequest
) {
626 case USB_REQ_GET_CONFIGURATION
: {
627 logf("usb_core: GET_CONFIG");
628 response_data
[0] = (usb_state
== ADDRESS
? 0 : 1);
629 if(!usb_drv_send(EP_CONTROL
, response_data
, 1))
630 usb_core_ack_control(req
);
633 case USB_REQ_SET_CONFIGURATION
: {
634 logf("usb_core: SET_CONFIG");
635 usb_drv_cancel_all_transfers();
637 usb_state
= CONFIGURED
;
638 for(i
=0;i
<USB_NUM_DRIVERS
;i
++) {
639 if(drivers
[i
].enabled
&& drivers
[i
].init_connection
)
640 drivers
[i
].init_connection();
646 usb_core_ack_control(req
);
649 case USB_REQ_SET_ADDRESS
: {
650 unsigned char address
= req
->wValue
;
651 logf("usb_core: SET_ADR %d", address
);
652 if(usb_core_ack_control(req
))
654 usb_drv_cancel_all_transfers();
655 usb_address
= address
;
656 usb_drv_set_address(usb_address
);
660 case USB_REQ_GET_DESCRIPTOR
:
661 logf("usb_core: GET_DESC %d", req
->wValue
>>8);
662 request_handler_device_get_descriptor(req
);
664 case USB_REQ_CLEAR_FEATURE
:
666 case USB_REQ_SET_FEATURE
:
667 if(req
->wValue
==USB_DEVICE_TEST_MODE
) {
668 int mode
=req
->wIndex
>>8;
669 usb_core_ack_control(req
);
670 usb_drv_set_test_mode(mode
);
673 case USB_REQ_GET_STATUS
:
676 if(!usb_drv_send(EP_CONTROL
, response_data
, 2))
677 usb_core_ack_control(req
);
684 static void request_handler_interface_standard(struct usb_ctrlrequest
* req
)
686 switch (req
->bRequest
)
688 case USB_REQ_SET_INTERFACE
:
689 logf("usb_core: SET_INTERFACE");
690 usb_core_ack_control(req
);
693 case USB_REQ_GET_INTERFACE
:
694 logf("usb_core: GET_INTERFACE");
696 if(!usb_drv_send(EP_CONTROL
,response_data
,1))
697 usb_core_ack_control(req
);
699 case USB_REQ_CLEAR_FEATURE
:
701 case USB_REQ_SET_FEATURE
:
703 case USB_REQ_GET_STATUS
:
706 if(!usb_drv_send(EP_CONTROL
, response_data
, 2))
707 usb_core_ack_control(req
);
710 control_request_handler_drivers(req
);
715 static void request_handler_interface(struct usb_ctrlrequest
* req
)
717 switch(req
->bRequestType
& USB_TYPE_MASK
) {
718 case USB_TYPE_STANDARD
:
719 request_handler_interface_standard(req
);
722 control_request_handler_drivers(req
);
724 case USB_TYPE_VENDOR
:
729 static void request_handler_endpoint(struct usb_ctrlrequest
* req
)
731 switch (req
->bRequest
) {
732 case USB_REQ_CLEAR_FEATURE
:
733 if (req
->wValue
==USB_ENDPOINT_HALT
) {
734 usb_drv_stall(EP_NUM(req
->wIndex
), false, EP_DIR(req
->wIndex
));
736 usb_core_ack_control(req
);
738 case USB_REQ_SET_FEATURE
:
739 if (req
->wValue
==USB_ENDPOINT_HALT
) {
740 usb_drv_stall(EP_NUM(req
->wIndex
), true, EP_DIR(req
->wIndex
));
742 usb_core_ack_control(req
);
744 case USB_REQ_GET_STATUS
:
747 logf("usb_core: GET_STATUS");
749 response_data
[0]=usb_drv_stalled(EP_NUM(req
->wIndex
),
750 EP_DIR(req
->wIndex
));
752 if(!usb_drv_send(EP_CONTROL
,response_data
,2))
753 usb_core_ack_control(req
);
757 control_handler_t control_handler
;
760 ep_data
[EP_NUM(req
->wIndex
)].control_handler
[EP_CONTROL
];
761 if (!control_handler
)
764 handled
=control_handler(req
, response_data
);
766 /* nope. flag error */
767 logf("usb bad req %d",req
->bRequest
);
768 usb_drv_stall(EP_CONTROL
,true,true);
769 usb_core_ack_control(req
);
776 /* Handling USB requests starts here */
777 static void usb_core_control_request_handler(struct usb_ctrlrequest
* req
)
779 if (usb_state
==DEFAULT
) {
780 set_serial_descriptor();
781 usb_core_set_serial_function_id();
783 allocate_interfaces_and_endpoints();
786 switch(req
->bRequestType
& USB_RECIP_MASK
) {
787 case USB_RECIP_DEVICE
:
788 request_handler_device(req
);
790 case USB_RECIP_INTERFACE
:
791 request_handler_interface(req
);
793 case USB_RECIP_ENDPOINT
:
794 request_handler_endpoint(req
);
796 case USB_RECIP_OTHER
:
797 logf("unsupported recipient");
800 //logf("control handled");
803 /* called by usb_drv_int() */
804 void usb_core_bus_reset(void)
810 /* called by usb_drv_transfer_completed() */
811 void usb_core_transfer_complete(int endpoint
,int dir
,int status
,int length
)
813 struct usb_transfer_completion_event_data
*completion_event
;
817 /* already handled */
821 completion_event
=&ep_data
[endpoint
].completion_event
;
823 completion_event
->endpoint
=endpoint
;
824 completion_event
->dir
=dir
;
825 completion_event
->data
=0;
826 completion_event
->status
=status
;
827 completion_event
->length
=length
;
828 /* All other endoints. Let the thread deal with it */
829 usb_signal_transfer_completion(completion_event
);
834 /* called by usb_drv_int() */
835 void usb_core_control_request(struct usb_ctrlrequest
* req
)
837 struct usb_transfer_completion_event_data
* completion_event
=
838 &ep_data
[EP_CONTROL
].completion_event
;
840 completion_event
->endpoint
=EP_CONTROL
;
841 completion_event
->dir
=0;
842 completion_event
->data
=(void*)req
;
843 completion_event
->status
=0;
844 completion_event
->length
=0;
845 logf("ctrl received %ld",current_tick
);
846 usb_signal_transfer_completion(completion_event
);
849 int usb_core_ack_control(struct usb_ctrlrequest
* req
)
851 if (req
->bRequestType
& USB_DIR_IN
)
852 return usb_drv_recv(EP_CONTROL
,NULL
,0);
854 return usb_drv_send(EP_CONTROL
,NULL
,0);
857 #ifdef HAVE_USB_POWER
858 unsigned short usb_allowed_current()
860 return (usb_state
==CONFIGURED
) ? MAX(USB_MAX_CURRENT
, 100) : 100;