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 #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',
141 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
142 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
143 '0', '0', '0', '0', '0', '0', '0', '0'}
146 /* Generic for all targets */
148 /* this is stringid #0: languages supported */
149 static const struct usb_string_descriptor
__attribute__((aligned(2)))
154 {0x0409} /* LANGID US English */
157 static const struct usb_string_descriptor
* const usb_strings
[] =
160 &usb_string_iManufacturer
,
161 &usb_string_iProduct
,
165 static int usb_address
= 0;
166 static bool initialized
= false;
167 static enum { DEFAULT
, ADDRESS
, CONFIGURED
} usb_state
;
169 #ifdef HAVE_USB_CHARGING_ENABLE
170 static int usb_charging_mode
= USB_CHARGING_DISABLE
;
171 static int usb_charging_current_requested
= 500;
172 static struct timeout usb_no_host_timeout
;
173 static bool usb_no_host
= false;
175 static int usb_no_host_callback(struct timeout
*tmo
)
179 usb_charger_update();
184 static int usb_core_num_interfaces
;
186 typedef void (*completion_handler_t
)(int ep
, int dir
, int status
, int length
);
187 typedef bool (*control_handler_t
)(struct usb_ctrlrequest
* req
,
188 unsigned char* dest
);
192 completion_handler_t completion_handler
[2];
193 control_handler_t control_handler
[2];
194 struct usb_transfer_completion_event_data completion_event
[2];
195 } ep_data
[USB_NUM_ENDPOINTS
];
197 static struct usb_class_driver drivers
[USB_NUM_DRIVERS
] =
199 #ifdef USB_ENABLE_STORAGE
200 [USB_DRIVER_MASS_STORAGE
] = {
202 .needs_exclusive_storage
= true,
203 .first_interface
= 0,
205 .request_endpoints
= usb_storage_request_endpoints
,
206 .set_first_interface
= usb_storage_set_first_interface
,
207 .get_config_descriptor
= usb_storage_get_config_descriptor
,
208 .init_connection
= usb_storage_init_connection
,
209 .init
= usb_storage_init
,
210 .disconnect
= usb_storage_disconnect
,
211 .transfer_complete
= usb_storage_transfer_complete
,
212 .control_request
= usb_storage_control_request
,
214 .notify_hotswap
= usb_storage_notify_hotswap
,
218 #ifdef USB_ENABLE_SERIAL
219 [USB_DRIVER_SERIAL
] = {
221 .needs_exclusive_storage
= false,
222 .first_interface
= 0,
224 .request_endpoints
= usb_serial_request_endpoints
,
225 .set_first_interface
= usb_serial_set_first_interface
,
226 .get_config_descriptor
= usb_serial_get_config_descriptor
,
227 .init_connection
= usb_serial_init_connection
,
228 .init
= usb_serial_init
,
229 .disconnect
= usb_serial_disconnect
,
230 .transfer_complete
= usb_serial_transfer_complete
,
231 .control_request
= usb_serial_control_request
,
233 .notify_hotswap
= NULL
,
237 #ifdef USB_ENABLE_CHARGING_ONLY
238 [USB_DRIVER_CHARGING_ONLY
] = {
240 .needs_exclusive_storage
= false,
241 .first_interface
= 0,
243 .request_endpoints
= usb_charging_only_request_endpoints
,
244 .set_first_interface
= usb_charging_only_set_first_interface
,
245 .get_config_descriptor
= usb_charging_only_get_config_descriptor
,
246 .init_connection
= NULL
,
249 .transfer_complete
= NULL
,
250 .control_request
= NULL
,
252 .notify_hotswap
= NULL
,
256 #ifdef USB_ENABLE_HID
259 .needs_exclusive_storage
= false,
260 .first_interface
= 0,
262 .request_endpoints
= usb_hid_request_endpoints
,
263 .set_first_interface
= usb_hid_set_first_interface
,
264 .get_config_descriptor
= usb_hid_get_config_descriptor
,
265 .init_connection
= usb_hid_init_connection
,
266 .init
= usb_hid_init
,
267 .disconnect
= usb_hid_disconnect
,
268 .transfer_complete
= usb_hid_transfer_complete
,
269 .control_request
= usb_hid_control_request
,
271 .notify_hotswap
= NULL
,
277 static void usb_core_control_request_handler(struct usb_ctrlrequest
* req
);
279 static unsigned char response_data
[256] USB_DEVBSS_ATTR
;
281 static short hex
[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
282 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
284 static void set_serial_descriptor(void)
287 uint32_t* serial
= (uint32_t*)0x20004034;
289 uint32_t* serial
= (uint32_t*)0x20002034;
292 /* We need to convert from a little-endian 64-bit int
293 into a utf-16 string of hex characters */
294 short* p
= &usb_string_iSerial
.wString
[24];
298 for(i
= 0; i
< 2; i
++) {
300 for(j
= 0; j
< 8; j
++) {
305 usb_string_iSerial
.bLength
= 52;
307 #elif defined(HAVE_AS3514)
308 static void set_serial_descriptor(void)
310 unsigned char serial
[AS3514_UID_LEN
];
311 /* Align 32 digits right in the 40-digit serial number */
312 short* p
= &usb_string_iSerial
.wString
[1];
315 ascodec_readbytes(AS3514_UID_0
, AS3514_UID_LEN
, serial
);
316 for(i
= 0; i
< AS3514_UID_LEN
; i
++) {
317 *p
++ = hex
[(serial
[i
] >> 4) & 0xF];
318 *p
++ = hex
[(serial
[i
] >> 0) & 0xF];
320 usb_string_iSerial
.bLength
= 36 + (2 * AS3514_UID_LEN
);
322 #elif (CONFIG_STORAGE & STORAGE_ATA)
323 /* If we don't know the device serial number, use the one
325 static void set_serial_descriptor(void)
327 short* p
= &usb_string_iSerial
.wString
[1];
328 unsigned short* identify
= ata_get_identify();
332 for(i
= 10; i
< 20; i
++) {
334 *p
++ = hex
[(x
>> 12) & 0xF];
335 *p
++ = hex
[(x
>> 8) & 0xF];
336 *p
++ = hex
[(x
>> 4) & 0xF];
337 *p
++ = hex
[(x
>> 0) & 0xF];
339 usb_string_iSerial
.bLength
= 84;
341 #elif (CONFIG_STORAGE & STORAGE_RAMDISK)
342 /* This "serial number" isn't unique, but it should never actually
343 appear in non-testing use */
344 static void set_serial_descriptor(void)
346 short* p
= &usb_string_iSerial
.wString
[1];
348 for(i
= 0; i
< 16; i
++) {
349 *p
++ = hex
[(2 * i
) & 0xF];
350 *p
++ = hex
[(2 * i
+ 1) & 0xF];
352 usb_string_iSerial
.bLength
= 68;
355 static void set_serial_descriptor(void)
357 device_descriptor
.iSerialNumber
= 0;
361 void usb_core_init(void)
369 /* class driver init functions should be safe to call even if the driver
370 * won't be used. This simplifies other logic (i.e. we don't need to know
371 * yet which drivers will be enabled */
372 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
373 if(drivers
[i
].init
!= NULL
)
378 #ifdef HAVE_USB_CHARGING_ENABLE
380 timeout_register(&usb_no_host_timeout
, usb_no_host_callback
, HZ
*10, 0);
382 logf("usb_core_init() finished");
385 void usb_core_exit(void)
388 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
389 if(drivers
[i
].enabled
&& drivers
[i
].disconnect
!= NULL
)
391 drivers
[i
].disconnect();
392 drivers
[i
].enabled
= false;
400 #ifdef HAVE_USB_CHARGING_ENABLE
402 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
404 logf("usb_core_exit() finished");
407 void usb_core_handle_transfer_completion(
408 struct usb_transfer_completion_event_data
* event
)
410 completion_handler_t handler
;
411 int ep
= event
->endpoint
;
415 logf("ctrl handled %ld",current_tick
);
416 usb_core_control_request_handler(
417 (struct usb_ctrlrequest
*)event
->data
);
420 handler
= ep_data
[ep
].completion_handler
[EP_DIR(event
->dir
)];
422 handler(ep
, event
->dir
, event
->status
, event
->length
);
427 void usb_core_enable_driver(int driver
, bool enabled
)
429 drivers
[driver
].enabled
= enabled
;
432 bool usb_core_driver_enabled(int driver
)
434 return drivers
[driver
].enabled
;
437 bool usb_core_any_exclusive_storage(void)
440 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
441 if(drivers
[i
].enabled
&& drivers
[i
].needs_exclusive_storage
)
448 void usb_core_hotswap_event(int volume
, bool inserted
)
451 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
452 if(drivers
[i
].enabled
&& drivers
[i
].notify_hotswap
!= NULL
)
453 drivers
[i
].notify_hotswap(volume
, inserted
);
457 static void usb_core_set_serial_function_id(void)
461 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
462 if(drivers
[i
].enabled
)
465 usb_string_iSerial
.wString
[0] = hex
[id
];
468 int usb_core_request_endpoint(int type
, int dir
, struct usb_class_driver
* drv
)
472 ret
= usb_drv_request_endpoint(type
, dir
);
480 ep_data
[ep
].completion_handler
[dir
] = drv
->transfer_complete
;
481 ep_data
[ep
].control_handler
[dir
] = drv
->control_request
;
486 void usb_core_release_endpoint(int ep
)
490 usb_drv_release_endpoint(ep
);
495 ep_data
[ep
].completion_handler
[dir
] = NULL
;
496 ep_data
[ep
].control_handler
[dir
] = NULL
;
499 static void allocate_interfaces_and_endpoints(void)
504 memset(ep_data
, 0, sizeof(ep_data
));
506 for(i
= 0; i
< USB_NUM_ENDPOINTS
; i
++) {
507 usb_drv_release_endpoint(i
| USB_DIR_OUT
);
508 usb_drv_release_endpoint(i
| USB_DIR_IN
);
511 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++) {
512 if(drivers
[i
].enabled
) {
513 drivers
[i
].first_interface
= interface
;
515 if(drivers
[i
].request_endpoints(&drivers
[i
])) {
516 drivers
[i
].enabled
= false;
520 interface
= drivers
[i
].set_first_interface(interface
);
521 drivers
[i
].last_interface
= interface
;
524 usb_core_num_interfaces
= interface
;
528 static void control_request_handler_drivers(struct usb_ctrlrequest
* req
)
530 int i
, interface
= req
->wIndex
& 0xff;
531 bool handled
= false;
533 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++) {
534 if(drivers
[i
].enabled
&&
535 drivers
[i
].control_request
&&
536 drivers
[i
].first_interface
<= interface
&&
537 drivers
[i
].last_interface
> interface
)
539 handled
= drivers
[i
].control_request(req
, response_data
);
545 /* nope. flag error */
546 logf("bad req:desc %d:%d", req
->bRequest
, req
->wValue
>> 8);
547 usb_drv_stall(EP_CONTROL
, true, true);
551 static void request_handler_device_get_descriptor(struct usb_ctrlrequest
* req
)
554 const void* ptr
= NULL
;
555 int length
= req
->wLength
;
556 int index
= req
->wValue
& 0xff;
558 switch(req
->wValue
>> 8) { /* type */
560 ptr
= &device_descriptor
;
561 size
= sizeof(struct usb_device_descriptor
);
564 case USB_DT_OTHER_SPEED_CONFIG
:
565 case USB_DT_CONFIG
: {
566 int i
, max_packet_size
;
568 if(req
->wValue
>>8==USB_DT_CONFIG
) {
569 max_packet_size
= (usb_drv_port_speed() ? 512 : 64);
570 config_descriptor
.bDescriptorType
= USB_DT_CONFIG
;
573 max_packet_size
=(usb_drv_port_speed() ? 64 : 512);
574 config_descriptor
.bDescriptorType
=
575 USB_DT_OTHER_SPEED_CONFIG
;
577 #ifdef HAVE_USB_CHARGING_ENABLE
578 if (usb_charging_mode
== USB_CHARGING_DISABLE
) {
579 config_descriptor
.bMaxPower
= (100+1)/2;
580 usb_charging_current_requested
= 100;
583 config_descriptor
.bMaxPower
= (500+1)/2;
584 usb_charging_current_requested
= 500;
587 size
= sizeof(struct usb_config_descriptor
);
589 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
590 if(drivers
[i
].enabled
&& drivers
[i
].get_config_descriptor
)
591 size
+= drivers
[i
].get_config_descriptor(
592 &response_data
[size
], max_packet_size
);
594 config_descriptor
.bNumInterfaces
= usb_core_num_interfaces
;
595 config_descriptor
.wTotalLength
= (uint16_t)size
;
596 memcpy(&response_data
[0], &config_descriptor
,
597 sizeof(struct usb_config_descriptor
));
604 logf("STRING %d", index
);
605 if((unsigned)index
< (sizeof(usb_strings
) /
606 sizeof(struct usb_string_descriptor
*))) {
607 size
= usb_strings
[index
]->bLength
;
608 ptr
= usb_strings
[index
];
611 logf("bad string id %d", index
);
612 usb_drv_stall(EP_CONTROL
, true, true);
616 case USB_DT_DEVICE_QUALIFIER
:
617 ptr
= &qualifier_descriptor
;
618 size
= sizeof(struct usb_qualifier_descriptor
);
623 control_request_handler_drivers(req
);
628 logf("data %d (%d)", size
, length
);
629 length
= MIN(size
, length
);
631 if (ptr
!= response_data
)
632 memcpy(response_data
, ptr
, length
);
634 usb_drv_recv(EP_CONTROL
, NULL
, 0);
635 usb_drv_send(EP_CONTROL
, response_data
, length
);
639 static void request_handler_device(struct usb_ctrlrequest
* req
)
643 switch(req
->bRequest
) {
644 case USB_REQ_GET_CONFIGURATION
: {
645 logf("usb_core: GET_CONFIG");
646 response_data
[0] = (usb_state
== ADDRESS
? 0 : 1);
647 usb_drv_recv(EP_CONTROL
, NULL
, 0);
648 usb_drv_send(EP_CONTROL
, response_data
, 1);
651 case USB_REQ_SET_CONFIGURATION
: {
652 logf("usb_core: SET_CONFIG");
653 usb_drv_cancel_all_transfers();
655 usb_state
= CONFIGURED
;
656 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
657 if(drivers
[i
].enabled
&& drivers
[i
].init_connection
)
658 drivers
[i
].init_connection();
662 usb_drv_send(EP_CONTROL
, NULL
, 0);
663 #ifdef HAVE_USB_CHARGING_ENABLE
664 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
668 case USB_REQ_SET_ADDRESS
: {
669 unsigned char address
= req
->wValue
;
670 logf("usb_core: SET_ADR %d", address
);
671 usb_drv_send(EP_CONTROL
, NULL
, 0);
672 usb_drv_cancel_all_transfers();
673 usb_address
= address
;
674 usb_drv_set_address(usb_address
);
678 case USB_REQ_GET_DESCRIPTOR
:
679 logf("usb_core: GET_DESC %d", req
->wValue
>> 8);
680 request_handler_device_get_descriptor(req
);
682 case USB_REQ_CLEAR_FEATURE
:
684 case USB_REQ_SET_FEATURE
:
685 if(req
->wValue
==USB_DEVICE_TEST_MODE
) {
686 int mode
= req
->wIndex
>> 8;
687 usb_drv_send(EP_CONTROL
, NULL
, 0);
688 usb_drv_set_test_mode(mode
);
691 case USB_REQ_GET_STATUS
:
692 response_data
[0] = 0;
693 response_data
[1] = 0;
694 usb_drv_recv(EP_CONTROL
, NULL
, 0);
695 usb_drv_send(EP_CONTROL
, response_data
, 2);
698 logf("bad req:desc %d:%d", req
->bRequest
, req
->wValue
);
699 usb_drv_stall(EP_CONTROL
, true, true);
704 static void request_handler_interface_standard(struct usb_ctrlrequest
* req
)
706 switch (req
->bRequest
)
708 case USB_REQ_SET_INTERFACE
:
709 logf("usb_core: SET_INTERFACE");
710 usb_drv_send(EP_CONTROL
, NULL
, 0);
713 case USB_REQ_GET_INTERFACE
:
714 logf("usb_core: GET_INTERFACE");
715 response_data
[0] = 0;
716 usb_drv_recv(EP_CONTROL
, NULL
, 0);
717 usb_drv_send(EP_CONTROL
, response_data
, 1);
719 case USB_REQ_CLEAR_FEATURE
:
721 case USB_REQ_SET_FEATURE
:
723 case USB_REQ_GET_STATUS
:
724 response_data
[0] = 0;
725 response_data
[1] = 0;
726 usb_drv_recv(EP_CONTROL
, NULL
, 0);
727 usb_drv_send(EP_CONTROL
, response_data
, 2);
730 control_request_handler_drivers(req
);
735 static void request_handler_interface(struct usb_ctrlrequest
* req
)
737 switch(req
->bRequestType
& USB_TYPE_MASK
) {
738 case USB_TYPE_STANDARD
:
739 request_handler_interface_standard(req
);
742 control_request_handler_drivers(req
);
744 case USB_TYPE_VENDOR
:
746 logf("bad req:desc %d", req
->bRequest
);
747 usb_drv_stall(EP_CONTROL
, true, true);
752 static void request_handler_endoint_drivers(struct usb_ctrlrequest
* req
)
754 bool handled
= false;
755 control_handler_t control_handler
= NULL
;
757 if(EP_NUM(req
->wIndex
) < USB_NUM_ENDPOINTS
)
759 ep_data
[EP_NUM(req
->wIndex
)].control_handler
[EP_DIR(req
->wIndex
)];
762 handled
= control_handler(req
, response_data
);
765 /* nope. flag error */
766 logf("usb bad req %d", req
->bRequest
);
767 usb_drv_stall(EP_CONTROL
, true, true);
771 static void request_handler_endpoint_standard(struct usb_ctrlrequest
* req
)
773 switch (req
->bRequest
) {
774 case USB_REQ_CLEAR_FEATURE
:
775 if(req
->wValue
== USB_ENDPOINT_HALT
)
776 usb_drv_stall(EP_NUM(req
->wIndex
), false, EP_DIR(req
->wIndex
));
778 usb_drv_send(EP_CONTROL
, NULL
, 0);
780 case USB_REQ_SET_FEATURE
:
781 if(req
->wValue
== USB_ENDPOINT_HALT
)
782 usb_drv_stall(EP_NUM(req
->wIndex
), true, EP_DIR(req
->wIndex
));
784 usb_drv_send(EP_CONTROL
, NULL
, 0);
786 case USB_REQ_GET_STATUS
:
787 response_data
[0] = 0;
788 response_data
[1] = 0;
789 logf("usb_core: GET_STATUS");
791 response_data
[0] = usb_drv_stalled(EP_NUM(req
->wIndex
),
792 EP_DIR(req
->wIndex
));
794 usb_drv_recv(EP_CONTROL
, NULL
, 0);
795 usb_drv_send(EP_CONTROL
, response_data
, 2);
798 request_handler_endoint_drivers(req
);
803 static void request_handler_endpoint(struct usb_ctrlrequest
* req
)
805 switch(req
->bRequestType
& USB_TYPE_MASK
) {
806 case USB_TYPE_STANDARD
:
807 request_handler_endpoint_standard(req
);
810 request_handler_endoint_drivers(req
);
812 case USB_TYPE_VENDOR
:
814 logf("bad req:desc %d", req
->bRequest
);
815 usb_drv_stall(EP_CONTROL
, true, true);
820 /* Handling USB requests starts here */
821 static void usb_core_control_request_handler(struct usb_ctrlrequest
* req
)
823 #ifdef HAVE_USB_CHARGING_ENABLE
824 timeout_cancel(&usb_no_host_timeout
);
827 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
830 if(usb_state
== DEFAULT
) {
831 set_serial_descriptor();
832 usb_core_set_serial_function_id();
834 allocate_interfaces_and_endpoints();
837 switch(req
->bRequestType
& USB_RECIP_MASK
) {
838 case USB_RECIP_DEVICE
:
839 request_handler_device(req
);
841 case USB_RECIP_INTERFACE
:
842 request_handler_interface(req
);
844 case USB_RECIP_ENDPOINT
:
845 request_handler_endpoint(req
);
847 case USB_RECIP_OTHER
:
848 logf("unsupported recipient");
849 usb_drv_stall(EP_CONTROL
, true, true);
852 //logf("control handled");
855 /* called by usb_drv_int() */
856 void usb_core_bus_reset(void)
860 #ifdef HAVE_USB_CHARGING_ENABLE
861 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
865 /* called by usb_drv_transfer_completed() */
866 void usb_core_transfer_complete(int endpoint
, int dir
, int status
, int length
)
868 struct usb_transfer_completion_event_data
*completion_event
;
872 /* already handled */
876 completion_event
= &ep_data
[endpoint
].completion_event
[EP_DIR(dir
)];
878 completion_event
->endpoint
= endpoint
;
879 completion_event
->dir
= dir
;
880 completion_event
->data
= 0;
881 completion_event
->status
= status
;
882 completion_event
->length
= length
;
883 /* All other endpoints. Let the thread deal with it */
884 usb_signal_transfer_completion(completion_event
);
889 /* called by usb_drv_int() */
890 void usb_core_control_request(struct usb_ctrlrequest
* req
)
892 struct usb_transfer_completion_event_data
* completion_event
=
893 &ep_data
[EP_CONTROL
].completion_event
[EP_DIR(USB_DIR_IN
)];
895 completion_event
->endpoint
= EP_CONTROL
;
896 completion_event
->dir
= 0;
897 completion_event
->data
= (void*)req
;
898 completion_event
->status
= 0;
899 completion_event
->length
= 0;
900 logf("ctrl received %ld", current_tick
);
901 usb_signal_transfer_completion(completion_event
);
904 #ifdef HAVE_USB_CHARGING_ENABLE
905 void usb_charging_enable(int state
)
907 usb_charging_mode
= state
;
908 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
911 int usb_charging_maxcurrent()
913 if (!initialized
|| usb_charging_mode
== USB_CHARGING_DISABLE
)
915 if (usb_state
== CONFIGURED
)
916 return usb_charging_current_requested
;
917 if (usb_charging_mode
== USB_CHARGING_FORCE
&& usb_no_host
)