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 ****************************************************************************/
25 /*#define LOGF_ENABLE*/
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 #if (CONFIG_CPU == IMX233)
62 #include "ocotp-imx233.h"
65 #ifndef USB_MAX_CURRENT
66 #define USB_MAX_CURRENT 500
69 /*-------------------------------------------------------------------------*/
70 /* USB protocol descriptors: */
72 static struct usb_device_descriptor
__attribute__((aligned(2)))
75 .bLength
= sizeof(struct usb_device_descriptor
),
76 .bDescriptorType
= USB_DT_DEVICE
,
77 #ifndef USB_NO_HIGH_SPEED
82 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
85 .bMaxPacketSize0
= 64,
86 .idVendor
= USB_VENDOR_ID
,
87 .idProduct
= USB_PRODUCT_ID
,
92 .bNumConfigurations
= 1
95 static struct usb_config_descriptor
__attribute__((aligned(2)))
98 .bLength
= sizeof(struct usb_config_descriptor
),
99 .bDescriptorType
= USB_DT_CONFIG
,
100 .wTotalLength
= 0, /* will be filled in later */
102 .bConfigurationValue
= 1,
104 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
105 .bMaxPower
= (USB_MAX_CURRENT
+ 1) / 2, /* In 2mA units */
108 static const struct usb_qualifier_descriptor
__attribute__((aligned(2)))
109 qualifier_descriptor
=
111 .bLength
= sizeof(struct usb_qualifier_descriptor
),
112 .bDescriptorType
= USB_DT_DEVICE_QUALIFIER
,
115 .bDeviceSubClass
= 0,
116 .bDeviceProtocol
= 0,
117 .bMaxPacketSize0
= 64,
118 .bNumConfigurations
= 1
121 static const struct usb_string_descriptor
__attribute__((aligned(2)))
122 usb_string_iManufacturer
=
126 {'R', 'o', 'c', 'k', 'b', 'o', 'x', '.', 'o', 'r', 'g'}
129 static const struct usb_string_descriptor
__attribute__((aligned(2)))
130 usb_string_iProduct
=
134 {'R', 'o', 'c', 'k', 'b', 'o', 'x', ' ',
135 'm', 'e', 'd', 'i', 'a', ' ',
136 'p', 'l', 'a', 'y', 'e', 'r'}
139 static struct usb_string_descriptor
__attribute__((aligned(2)))
144 {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
145 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
146 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
147 '0', '0', '0', '0', '0', '0', '0', '0'}
150 /* Generic for all targets */
152 /* this is stringid #0: languages supported */
153 static const struct usb_string_descriptor
__attribute__((aligned(2)))
158 {0x0409} /* LANGID US English */
161 static const struct usb_string_descriptor
* const usb_strings
[] =
164 &usb_string_iManufacturer
,
165 &usb_string_iProduct
,
169 static int usb_address
= 0;
170 static bool initialized
= false;
171 static enum { DEFAULT
, ADDRESS
, CONFIGURED
} usb_state
;
173 #ifdef HAVE_USB_CHARGING_ENABLE
174 static int usb_charging_mode
= USB_CHARGING_DISABLE
;
175 static int usb_charging_current_requested
= 500;
176 static struct timeout usb_no_host_timeout
;
177 static bool usb_no_host
= false;
179 static int usb_no_host_callback(struct timeout
*tmo
)
183 usb_charger_update();
188 static int usb_core_num_interfaces
;
190 typedef void (*completion_handler_t
)(int ep
, int dir
, int status
, int length
);
191 typedef bool (*control_handler_t
)(struct usb_ctrlrequest
* req
,
192 unsigned char* dest
);
196 completion_handler_t completion_handler
[2];
197 control_handler_t control_handler
[2];
198 struct usb_transfer_completion_event_data completion_event
[2];
199 } ep_data
[USB_NUM_ENDPOINTS
];
201 static struct usb_class_driver drivers
[USB_NUM_DRIVERS
] =
203 #ifdef USB_ENABLE_STORAGE
204 [USB_DRIVER_MASS_STORAGE
] = {
206 .needs_exclusive_storage
= true,
207 .first_interface
= 0,
209 .request_endpoints
= usb_storage_request_endpoints
,
210 .set_first_interface
= usb_storage_set_first_interface
,
211 .get_config_descriptor
= usb_storage_get_config_descriptor
,
212 .init_connection
= usb_storage_init_connection
,
213 .init
= usb_storage_init
,
214 .disconnect
= usb_storage_disconnect
,
215 .transfer_complete
= usb_storage_transfer_complete
,
216 .control_request
= usb_storage_control_request
,
218 .notify_hotswap
= usb_storage_notify_hotswap
,
222 #ifdef USB_ENABLE_SERIAL
223 [USB_DRIVER_SERIAL
] = {
225 .needs_exclusive_storage
= false,
226 .first_interface
= 0,
228 .request_endpoints
= usb_serial_request_endpoints
,
229 .set_first_interface
= usb_serial_set_first_interface
,
230 .get_config_descriptor
= usb_serial_get_config_descriptor
,
231 .init_connection
= usb_serial_init_connection
,
232 .init
= usb_serial_init
,
233 .disconnect
= usb_serial_disconnect
,
234 .transfer_complete
= usb_serial_transfer_complete
,
235 .control_request
= usb_serial_control_request
,
237 .notify_hotswap
= NULL
,
241 #ifdef USB_ENABLE_CHARGING_ONLY
242 [USB_DRIVER_CHARGING_ONLY
] = {
244 .needs_exclusive_storage
= false,
245 .first_interface
= 0,
247 .request_endpoints
= usb_charging_only_request_endpoints
,
248 .set_first_interface
= usb_charging_only_set_first_interface
,
249 .get_config_descriptor
= usb_charging_only_get_config_descriptor
,
250 .init_connection
= NULL
,
253 .transfer_complete
= NULL
,
254 .control_request
= NULL
,
256 .notify_hotswap
= NULL
,
260 #ifdef USB_ENABLE_HID
263 .needs_exclusive_storage
= false,
264 .first_interface
= 0,
266 .request_endpoints
= usb_hid_request_endpoints
,
267 .set_first_interface
= usb_hid_set_first_interface
,
268 .get_config_descriptor
= usb_hid_get_config_descriptor
,
269 .init_connection
= usb_hid_init_connection
,
270 .init
= usb_hid_init
,
271 .disconnect
= usb_hid_disconnect
,
272 .transfer_complete
= usb_hid_transfer_complete
,
273 .control_request
= usb_hid_control_request
,
275 .notify_hotswap
= NULL
,
281 static void usb_core_control_request_handler(struct usb_ctrlrequest
* req
);
283 static unsigned char response_data
[256] USB_DEVBSS_ATTR
;
285 /** NOTE Serial Number
286 * The serial number string is split into two parts:
287 * - the first character indicates the set of interfaces enabled
288 * - the other characters form a (hopefully) unique device-specific number
289 * The implementation of set_serial_descriptor should left the first character
290 * of usb_string_iSerial unused, ie never write to
291 * usb_string_iSerial.wString[0] but should take it into account when
292 * computing the length of the descriptor
295 static const short hex
[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
296 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
298 static void set_serial_descriptor(void)
301 uint32_t* serial
= (uint32_t*)0x20004034;
303 uint32_t* serial
= (uint32_t*)0x20002034;
306 /* We need to convert from a little-endian 64-bit int
307 into a utf-16 string of hex characters */
308 short* p
= &usb_string_iSerial
.wString
[24];
312 for(i
= 0; i
< 2; i
++) {
314 for(j
= 0; j
< 8; j
++) {
319 usb_string_iSerial
.bLength
= 52;
321 #elif defined(HAVE_AS3514)
322 static void set_serial_descriptor(void)
324 unsigned char serial
[AS3514_UID_LEN
];
325 /* Align 32 digits right in the 40-digit serial number */
326 short* p
= &usb_string_iSerial
.wString
[1];
329 ascodec_readbytes(AS3514_UID_0
, AS3514_UID_LEN
, serial
);
330 for(i
= 0; i
< AS3514_UID_LEN
; i
++) {
331 *p
++ = hex
[(serial
[i
] >> 4) & 0xF];
332 *p
++ = hex
[(serial
[i
] >> 0) & 0xF];
334 usb_string_iSerial
.bLength
= 36 + (2 * AS3514_UID_LEN
);
336 #elif (CONFIG_CPU == IMX233)
337 static void set_serial_descriptor(void)
339 short* p
= &usb_string_iSerial
.wString
[1];
340 for(int i
= 0; i
< IMX233_NUM_OCOTP_OPS
; i
++) {
341 uint32_t ops
= imx233_ocotp_read(&HW_OCOTP_OPSx(i
));
342 for(int j
= 0; j
< 8; j
++) {
343 *p
++ = hex
[(ops
>> 28) & 0xF];
347 usb_string_iSerial
.bLength
= 2 + 2 * (1 + IMX233_NUM_OCOTP_OPS
* 8);
349 #elif (CONFIG_STORAGE & STORAGE_ATA)
350 /* If we don't know the device serial number, use the one
352 static void set_serial_descriptor(void)
354 short* p
= &usb_string_iSerial
.wString
[1];
355 unsigned short* identify
= ata_get_identify();
359 for(i
= 10; i
< 20; i
++) {
361 *p
++ = hex
[(x
>> 12) & 0xF];
362 *p
++ = hex
[(x
>> 8) & 0xF];
363 *p
++ = hex
[(x
>> 4) & 0xF];
364 *p
++ = hex
[(x
>> 0) & 0xF];
366 usb_string_iSerial
.bLength
= 84;
368 #elif (CONFIG_STORAGE & STORAGE_RAMDISK)
369 /* This "serial number" isn't unique, but it should never actually
370 appear in non-testing use */
371 static void set_serial_descriptor(void)
373 short* p
= &usb_string_iSerial
.wString
[1];
375 for(i
= 0; i
< 16; i
++) {
376 *p
++ = hex
[(2 * i
) & 0xF];
377 *p
++ = hex
[(2 * i
+ 1) & 0xF];
379 usb_string_iSerial
.bLength
= 68;
382 static void set_serial_descriptor(void)
384 device_descriptor
.iSerialNumber
= 0;
388 void usb_core_init(void)
396 /* class driver init functions should be safe to call even if the driver
397 * won't be used. This simplifies other logic (i.e. we don't need to know
398 * yet which drivers will be enabled */
399 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
400 if(drivers
[i
].init
!= NULL
)
405 #ifdef HAVE_USB_CHARGING_ENABLE
407 timeout_register(&usb_no_host_timeout
, usb_no_host_callback
, HZ
*10, 0);
409 logf("usb_core_init() finished");
412 void usb_core_exit(void)
415 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
416 if(drivers
[i
].enabled
&& drivers
[i
].disconnect
!= NULL
)
418 drivers
[i
].disconnect();
419 drivers
[i
].enabled
= false;
427 #ifdef HAVE_USB_CHARGING_ENABLE
429 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
431 logf("usb_core_exit() finished");
434 void usb_core_handle_transfer_completion(
435 struct usb_transfer_completion_event_data
* event
)
437 completion_handler_t handler
;
438 int ep
= event
->endpoint
;
442 logf("ctrl handled %ld",current_tick
);
443 usb_core_control_request_handler(
444 (struct usb_ctrlrequest
*)event
->data
);
447 handler
= ep_data
[ep
].completion_handler
[EP_DIR(event
->dir
)];
449 handler(ep
, event
->dir
, event
->status
, event
->length
);
454 void usb_core_enable_driver(int driver
, bool enabled
)
456 drivers
[driver
].enabled
= enabled
;
459 bool usb_core_driver_enabled(int driver
)
461 return drivers
[driver
].enabled
;
464 bool usb_core_any_exclusive_storage(void)
467 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
468 if(drivers
[i
].enabled
&& drivers
[i
].needs_exclusive_storage
)
475 void usb_core_hotswap_event(int volume
, bool inserted
)
478 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
479 if(drivers
[i
].enabled
&& drivers
[i
].notify_hotswap
!= NULL
)
480 drivers
[i
].notify_hotswap(volume
, inserted
);
484 static void usb_core_set_serial_function_id(void)
488 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
489 if(drivers
[i
].enabled
)
492 usb_string_iSerial
.wString
[0] = hex
[id
];
495 int usb_core_request_endpoint(int type
, int dir
, struct usb_class_driver
* drv
)
499 ret
= usb_drv_request_endpoint(type
, dir
);
507 ep_data
[ep
].completion_handler
[dir
] = drv
->transfer_complete
;
508 ep_data
[ep
].control_handler
[dir
] = drv
->control_request
;
513 void usb_core_release_endpoint(int ep
)
517 usb_drv_release_endpoint(ep
);
522 ep_data
[ep
].completion_handler
[dir
] = NULL
;
523 ep_data
[ep
].control_handler
[dir
] = NULL
;
526 static void allocate_interfaces_and_endpoints(void)
531 memset(ep_data
, 0, sizeof(ep_data
));
533 for(i
= 0; i
< USB_NUM_ENDPOINTS
; i
++) {
534 usb_drv_release_endpoint(i
| USB_DIR_OUT
);
535 usb_drv_release_endpoint(i
| USB_DIR_IN
);
538 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++) {
539 if(drivers
[i
].enabled
) {
540 drivers
[i
].first_interface
= interface
;
542 if(drivers
[i
].request_endpoints(&drivers
[i
])) {
543 drivers
[i
].enabled
= false;
547 interface
= drivers
[i
].set_first_interface(interface
);
548 drivers
[i
].last_interface
= interface
;
551 usb_core_num_interfaces
= interface
;
555 static void control_request_handler_drivers(struct usb_ctrlrequest
* req
)
557 int i
, interface
= req
->wIndex
& 0xff;
558 bool handled
= false;
560 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++) {
561 if(drivers
[i
].enabled
&&
562 drivers
[i
].control_request
&&
563 drivers
[i
].first_interface
<= interface
&&
564 drivers
[i
].last_interface
> interface
)
566 handled
= drivers
[i
].control_request(req
, response_data
);
572 /* nope. flag error */
573 logf("bad req:desc %d:%d", req
->bRequest
, req
->wValue
>> 8);
574 usb_drv_stall(EP_CONTROL
, true, true);
578 static void request_handler_device_get_descriptor(struct usb_ctrlrequest
* req
)
581 const void* ptr
= NULL
;
582 int length
= req
->wLength
;
583 int index
= req
->wValue
& 0xff;
585 switch(req
->wValue
>> 8) { /* type */
587 ptr
= &device_descriptor
;
588 size
= sizeof(struct usb_device_descriptor
);
591 case USB_DT_OTHER_SPEED_CONFIG
:
592 case USB_DT_CONFIG
: {
593 int i
, max_packet_size
;
595 if(req
->wValue
>>8==USB_DT_CONFIG
) {
596 max_packet_size
= (usb_drv_port_speed() ? 512 : 64);
597 config_descriptor
.bDescriptorType
= USB_DT_CONFIG
;
600 max_packet_size
=(usb_drv_port_speed() ? 64 : 512);
601 config_descriptor
.bDescriptorType
=
602 USB_DT_OTHER_SPEED_CONFIG
;
604 #ifdef HAVE_USB_CHARGING_ENABLE
605 if (usb_charging_mode
== USB_CHARGING_DISABLE
) {
606 config_descriptor
.bMaxPower
= (100+1)/2;
607 usb_charging_current_requested
= 100;
610 config_descriptor
.bMaxPower
= (500+1)/2;
611 usb_charging_current_requested
= 500;
614 size
= sizeof(struct usb_config_descriptor
);
616 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
617 if(drivers
[i
].enabled
&& drivers
[i
].get_config_descriptor
)
618 size
+= drivers
[i
].get_config_descriptor(
619 &response_data
[size
], max_packet_size
);
621 config_descriptor
.bNumInterfaces
= usb_core_num_interfaces
;
622 config_descriptor
.wTotalLength
= (uint16_t)size
;
623 memcpy(&response_data
[0], &config_descriptor
,
624 sizeof(struct usb_config_descriptor
));
631 logf("STRING %d", index
);
632 if((unsigned)index
< (sizeof(usb_strings
) /
633 sizeof(struct usb_string_descriptor
*))) {
634 size
= usb_strings
[index
]->bLength
;
635 ptr
= usb_strings
[index
];
638 logf("bad string id %d", index
);
639 usb_drv_stall(EP_CONTROL
, true, true);
643 case USB_DT_DEVICE_QUALIFIER
:
644 ptr
= &qualifier_descriptor
;
645 size
= sizeof(struct usb_qualifier_descriptor
);
650 control_request_handler_drivers(req
);
655 logf("data %d (%d)", size
, length
);
656 length
= MIN(size
, length
);
658 if (ptr
!= response_data
)
659 memcpy(response_data
, ptr
, length
);
661 usb_drv_recv(EP_CONTROL
, NULL
, 0);
662 usb_drv_send(EP_CONTROL
, response_data
, length
);
666 static void usb_core_do_set_addr(uint8_t address
)
668 logf("usb_core: SET_ADR %d", address
);
669 usb_address
= address
;
673 static void usb_core_do_set_config(uint8_t config
)
675 logf("usb_core: SET_CONFIG");
677 usb_state
= CONFIGURED
;
678 for(int i
= 0; i
< USB_NUM_DRIVERS
; i
++)
679 if(drivers
[i
].enabled
&& drivers
[i
].init_connection
)
680 drivers
[i
].init_connection();
684 #ifdef HAVE_USB_CHARGING_ENABLE
685 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
689 static void request_handler_device(struct usb_ctrlrequest
* req
)
691 switch(req
->bRequest
) {
692 case USB_REQ_GET_CONFIGURATION
: {
693 logf("usb_core: GET_CONFIG");
694 response_data
[0] = (usb_state
== ADDRESS
? 0 : 1);
695 usb_drv_recv(EP_CONTROL
, NULL
, 0);
696 usb_drv_send(EP_CONTROL
, response_data
, 1);
699 case USB_REQ_SET_CONFIGURATION
: {
700 usb_drv_cancel_all_transfers();
701 usb_core_do_set_config(req
->wValue
);
702 usb_drv_send(EP_CONTROL
, NULL
, 0);
705 case USB_REQ_SET_ADDRESS
: {
706 unsigned char address
= req
->wValue
;
707 logf("usb_core: SET_ADR %d", address
);
708 usb_drv_send(EP_CONTROL
, NULL
, 0);
709 usb_drv_cancel_all_transfers();
710 usb_drv_set_address(address
);
711 usb_core_do_set_addr(address
);
714 case USB_REQ_GET_DESCRIPTOR
:
715 logf("usb_core: GET_DESC %d", req
->wValue
>> 8);
716 request_handler_device_get_descriptor(req
);
718 case USB_REQ_CLEAR_FEATURE
:
720 case USB_REQ_SET_FEATURE
:
721 if(req
->wValue
==USB_DEVICE_TEST_MODE
) {
722 int mode
= req
->wIndex
>> 8;
723 usb_drv_send(EP_CONTROL
, NULL
, 0);
724 usb_drv_set_test_mode(mode
);
727 case USB_REQ_GET_STATUS
:
728 response_data
[0] = 0;
729 response_data
[1] = 0;
730 usb_drv_recv(EP_CONTROL
, NULL
, 0);
731 usb_drv_send(EP_CONTROL
, response_data
, 2);
734 logf("bad req:desc %d:%d", req
->bRequest
, req
->wValue
);
735 usb_drv_stall(EP_CONTROL
, true, true);
740 static void request_handler_interface_standard(struct usb_ctrlrequest
* req
)
742 switch (req
->bRequest
)
744 case USB_REQ_SET_INTERFACE
:
745 logf("usb_core: SET_INTERFACE");
746 usb_drv_send(EP_CONTROL
, NULL
, 0);
749 case USB_REQ_GET_INTERFACE
:
750 logf("usb_core: GET_INTERFACE");
751 response_data
[0] = 0;
752 usb_drv_recv(EP_CONTROL
, NULL
, 0);
753 usb_drv_send(EP_CONTROL
, response_data
, 1);
755 case USB_REQ_CLEAR_FEATURE
:
757 case USB_REQ_SET_FEATURE
:
759 case USB_REQ_GET_STATUS
:
760 response_data
[0] = 0;
761 response_data
[1] = 0;
762 usb_drv_recv(EP_CONTROL
, NULL
, 0);
763 usb_drv_send(EP_CONTROL
, response_data
, 2);
766 control_request_handler_drivers(req
);
771 static void request_handler_interface(struct usb_ctrlrequest
* req
)
773 switch(req
->bRequestType
& USB_TYPE_MASK
) {
774 case USB_TYPE_STANDARD
:
775 request_handler_interface_standard(req
);
778 control_request_handler_drivers(req
);
780 case USB_TYPE_VENDOR
:
782 logf("bad req:desc %d", req
->bRequest
);
783 usb_drv_stall(EP_CONTROL
, true, true);
788 static void request_handler_endoint_drivers(struct usb_ctrlrequest
* req
)
790 bool handled
= false;
791 control_handler_t control_handler
= NULL
;
793 if(EP_NUM(req
->wIndex
) < USB_NUM_ENDPOINTS
)
795 ep_data
[EP_NUM(req
->wIndex
)].control_handler
[EP_DIR(req
->wIndex
)];
798 handled
= control_handler(req
, response_data
);
801 /* nope. flag error */
802 logf("usb bad req %d", req
->bRequest
);
803 usb_drv_stall(EP_CONTROL
, true, true);
807 static void request_handler_endpoint_standard(struct usb_ctrlrequest
* req
)
809 switch (req
->bRequest
) {
810 case USB_REQ_CLEAR_FEATURE
:
811 if(req
->wValue
== USB_ENDPOINT_HALT
)
812 usb_drv_stall(EP_NUM(req
->wIndex
), false, EP_DIR(req
->wIndex
));
814 usb_drv_send(EP_CONTROL
, NULL
, 0);
816 case USB_REQ_SET_FEATURE
:
817 if(req
->wValue
== USB_ENDPOINT_HALT
)
818 usb_drv_stall(EP_NUM(req
->wIndex
), true, EP_DIR(req
->wIndex
));
820 usb_drv_send(EP_CONTROL
, NULL
, 0);
822 case USB_REQ_GET_STATUS
:
823 response_data
[0] = 0;
824 response_data
[1] = 0;
825 logf("usb_core: GET_STATUS");
827 response_data
[0] = usb_drv_stalled(EP_NUM(req
->wIndex
),
828 EP_DIR(req
->wIndex
));
830 usb_drv_recv(EP_CONTROL
, NULL
, 0);
831 usb_drv_send(EP_CONTROL
, response_data
, 2);
834 request_handler_endoint_drivers(req
);
839 static void request_handler_endpoint(struct usb_ctrlrequest
* req
)
841 switch(req
->bRequestType
& USB_TYPE_MASK
) {
842 case USB_TYPE_STANDARD
:
843 request_handler_endpoint_standard(req
);
846 request_handler_endoint_drivers(req
);
848 case USB_TYPE_VENDOR
:
850 logf("bad req:desc %d", req
->bRequest
);
851 usb_drv_stall(EP_CONTROL
, true, true);
856 /* Handling USB requests starts here */
857 static void usb_core_control_request_handler(struct usb_ctrlrequest
* req
)
859 #ifdef HAVE_USB_CHARGING_ENABLE
860 timeout_cancel(&usb_no_host_timeout
);
863 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
866 if(usb_state
== DEFAULT
) {
867 set_serial_descriptor();
868 usb_core_set_serial_function_id();
870 allocate_interfaces_and_endpoints();
873 switch(req
->bRequestType
& USB_RECIP_MASK
) {
874 case USB_RECIP_DEVICE
:
875 request_handler_device(req
);
877 case USB_RECIP_INTERFACE
:
878 request_handler_interface(req
);
880 case USB_RECIP_ENDPOINT
:
881 request_handler_endpoint(req
);
883 case USB_RECIP_OTHER
:
884 logf("unsupported recipient");
885 usb_drv_stall(EP_CONTROL
, true, true);
888 //logf("control handled");
891 /* called by usb_drv_int() */
892 void usb_core_bus_reset(void)
896 #ifdef HAVE_USB_CHARGING_ENABLE
897 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
901 /* called by usb_drv_transfer_completed() */
902 void usb_core_transfer_complete(int endpoint
, int dir
, int status
, int length
)
904 struct usb_transfer_completion_event_data
*completion_event
;
908 /* already handled */
912 completion_event
= &ep_data
[endpoint
].completion_event
[EP_DIR(dir
)];
914 completion_event
->endpoint
= endpoint
;
915 completion_event
->dir
= dir
;
916 completion_event
->data
= 0;
917 completion_event
->status
= status
;
918 completion_event
->length
= length
;
919 /* All other endpoints. Let the thread deal with it */
920 usb_signal_transfer_completion(completion_event
);
925 void usb_core_handle_notify(long id
, intptr_t data
)
929 case USB_NOTIFY_SET_ADDR
:
930 usb_core_do_set_addr(data
);
932 case USB_NOTIFY_SET_CONFIG
:
933 usb_core_do_set_config(data
);
940 /* called by usb_drv_int() */
941 void usb_core_control_request(struct usb_ctrlrequest
* req
)
943 struct usb_transfer_completion_event_data
* completion_event
=
944 &ep_data
[EP_CONTROL
].completion_event
[EP_DIR(USB_DIR_IN
)];
946 completion_event
->endpoint
= EP_CONTROL
;
947 completion_event
->dir
= 0;
948 completion_event
->data
= (void*)req
;
949 completion_event
->status
= 0;
950 completion_event
->length
= 0;
951 logf("ctrl received %ld", current_tick
);
952 usb_signal_transfer_completion(completion_event
);
955 void usb_core_notify_set_address(uint8_t addr
)
957 logf("notify set addr received %ld", current_tick
);
958 usb_signal_notify(USB_NOTIFY_SET_ADDR
, addr
);
961 void usb_core_notify_set_config(uint8_t config
)
963 logf("notify set config received %ld", current_tick
);
964 usb_signal_notify(USB_NOTIFY_SET_CONFIG
, config
);
967 #ifdef HAVE_USB_CHARGING_ENABLE
968 void usb_charging_enable(int state
)
970 usb_charging_mode
= state
;
971 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
974 int usb_charging_maxcurrent()
976 if (!initialized
|| usb_charging_mode
== USB_CHARGING_DISABLE
)
978 if (usb_state
== CONFIGURED
)
979 return usb_charging_current_requested
;
980 if (usb_charging_mode
== USB_CHARGING_FORCE
&& usb_no_host
)