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
[16];
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
, 0x10, serial
);
316 for(i
= 0; i
< 16; i
++) {
317 *p
++ = hex
[(serial
[i
] >> 4) & 0xF];
318 *p
++ = hex
[(serial
[i
] >> 0) & 0xF];
320 usb_string_iSerial
.bLength
= 68;
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
)
555 const void* ptr
= NULL
;
556 int length
= req
->wLength
;
557 int index
= req
->wValue
& 0xff;
559 switch(req
->wValue
>> 8) { /* type */
561 ptr
= &device_descriptor
;
562 size
= sizeof(struct usb_device_descriptor
);
565 case USB_DT_OTHER_SPEED_CONFIG
:
566 case USB_DT_CONFIG
: {
567 int i
, max_packet_size
;
569 if(req
->wValue
>>8==USB_DT_CONFIG
) {
570 max_packet_size
= (usb_drv_port_speed() ? 512 : 64);
571 config_descriptor
.bDescriptorType
= USB_DT_CONFIG
;
574 max_packet_size
=(usb_drv_port_speed() ? 64 : 512);
575 config_descriptor
.bDescriptorType
=
576 USB_DT_OTHER_SPEED_CONFIG
;
578 #ifdef HAVE_USB_CHARGING_ENABLE
579 if (usb_charging_mode
== USB_CHARGING_DISABLE
) {
580 config_descriptor
.bMaxPower
= (100+1)/2;
581 usb_charging_current_requested
= 100;
584 config_descriptor
.bMaxPower
= (500+1)/2;
585 usb_charging_current_requested
= 500;
588 size
= sizeof(struct usb_config_descriptor
);
590 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
591 if(drivers
[i
].enabled
&& drivers
[i
].get_config_descriptor
)
592 size
+= drivers
[i
].get_config_descriptor(
593 &response_data
[size
], max_packet_size
);
595 config_descriptor
.bNumInterfaces
= usb_core_num_interfaces
;
596 config_descriptor
.wTotalLength
= (uint16_t)size
;
597 memcpy(&response_data
[0], &config_descriptor
,
598 sizeof(struct usb_config_descriptor
));
605 logf("STRING %d", index
);
606 if((unsigned)index
< (sizeof(usb_strings
) /
607 sizeof(struct usb_string_descriptor
*))) {
608 size
= usb_strings
[index
]->bLength
;
609 ptr
= usb_strings
[index
];
612 logf("bad string id %d", index
);
613 usb_drv_stall(EP_CONTROL
, true, true);
617 case USB_DT_DEVICE_QUALIFIER
:
618 ptr
= &qualifier_descriptor
;
619 size
= sizeof(struct usb_qualifier_descriptor
);
625 control_request_handler_drivers(req
);
630 logf("data %d (%d)", size
, length
);
631 length
= MIN(size
, length
);
633 if (ptr
!= response_data
)
634 memcpy(response_data
, ptr
, length
);
636 usb_drv_recv(EP_CONTROL
, NULL
, 0);
637 usb_drv_send(EP_CONTROL
, response_data
, length
);
641 static void request_handler_device(struct usb_ctrlrequest
* req
)
645 switch(req
->bRequest
) {
646 case USB_REQ_GET_CONFIGURATION
: {
647 logf("usb_core: GET_CONFIG");
648 response_data
[0] = (usb_state
== ADDRESS
? 0 : 1);
649 usb_drv_recv(EP_CONTROL
, NULL
, 0);
650 usb_drv_send(EP_CONTROL
, response_data
, 1);
653 case USB_REQ_SET_CONFIGURATION
: {
654 logf("usb_core: SET_CONFIG");
655 usb_drv_cancel_all_transfers();
657 usb_state
= CONFIGURED
;
658 for(i
= 0; i
< USB_NUM_DRIVERS
; i
++)
659 if(drivers
[i
].enabled
&& drivers
[i
].init_connection
)
660 drivers
[i
].init_connection();
664 usb_drv_send(EP_CONTROL
, NULL
, 0);
665 #ifdef HAVE_USB_CHARGING_ENABLE
666 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
670 case USB_REQ_SET_ADDRESS
: {
671 unsigned char address
= req
->wValue
;
672 logf("usb_core: SET_ADR %d", address
);
673 usb_drv_send(EP_CONTROL
, NULL
, 0);
674 usb_drv_cancel_all_transfers();
675 usb_address
= address
;
676 usb_drv_set_address(usb_address
);
680 case USB_REQ_GET_DESCRIPTOR
:
681 logf("usb_core: GET_DESC %d", req
->wValue
>> 8);
682 request_handler_device_get_descriptor(req
);
684 case USB_REQ_CLEAR_FEATURE
:
686 case USB_REQ_SET_FEATURE
:
687 if(req
->wValue
==USB_DEVICE_TEST_MODE
) {
688 int mode
= req
->wIndex
>> 8;
689 usb_drv_send(EP_CONTROL
, NULL
, 0);
690 usb_drv_set_test_mode(mode
);
693 case USB_REQ_GET_STATUS
:
694 response_data
[0] = 0;
695 response_data
[1] = 0;
696 usb_drv_recv(EP_CONTROL
, NULL
, 0);
697 usb_drv_send(EP_CONTROL
, response_data
, 2);
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
:
749 static void request_handler_endoint_drivers(struct usb_ctrlrequest
* req
)
751 bool handled
= false;
752 control_handler_t control_handler
= NULL
;
754 if(EP_NUM(req
->wIndex
) < USB_NUM_ENDPOINTS
)
756 ep_data
[EP_NUM(req
->wIndex
)].control_handler
[EP_DIR(req
->wIndex
)];
759 handled
= control_handler(req
, response_data
);
762 /* nope. flag error */
763 logf("usb bad req %d", req
->bRequest
);
764 usb_drv_stall(EP_CONTROL
, true, true);
768 static void request_handler_endpoint_standard(struct usb_ctrlrequest
* req
)
770 switch (req
->bRequest
) {
771 case USB_REQ_CLEAR_FEATURE
:
772 if(req
->wValue
== USB_ENDPOINT_HALT
)
773 usb_drv_stall(EP_NUM(req
->wIndex
), false, EP_DIR(req
->wIndex
));
775 usb_drv_send(EP_CONTROL
, NULL
, 0);
777 case USB_REQ_SET_FEATURE
:
778 if(req
->wValue
== USB_ENDPOINT_HALT
)
779 usb_drv_stall(EP_NUM(req
->wIndex
), true, EP_DIR(req
->wIndex
));
781 usb_drv_send(EP_CONTROL
, NULL
, 0);
783 case USB_REQ_GET_STATUS
:
784 response_data
[0] = 0;
785 response_data
[1] = 0;
786 logf("usb_core: GET_STATUS");
788 response_data
[0] = usb_drv_stalled(EP_NUM(req
->wIndex
),
789 EP_DIR(req
->wIndex
));
791 usb_drv_recv(EP_CONTROL
, NULL
, 0);
792 usb_drv_send(EP_CONTROL
, response_data
, 2);
795 request_handler_endoint_drivers(req
);
800 static void request_handler_endpoint(struct usb_ctrlrequest
* req
)
802 switch(req
->bRequestType
& USB_TYPE_MASK
) {
803 case USB_TYPE_STANDARD
:
804 request_handler_endpoint_standard(req
);
807 request_handler_endoint_drivers(req
);
809 case USB_TYPE_VENDOR
:
815 /* Handling USB requests starts here */
816 static void usb_core_control_request_handler(struct usb_ctrlrequest
* req
)
818 #ifdef HAVE_USB_CHARGING_ENABLE
819 timeout_cancel(&usb_no_host_timeout
);
822 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
825 if(usb_state
== DEFAULT
) {
826 set_serial_descriptor();
827 usb_core_set_serial_function_id();
829 allocate_interfaces_and_endpoints();
832 switch(req
->bRequestType
& USB_RECIP_MASK
) {
833 case USB_RECIP_DEVICE
:
834 request_handler_device(req
);
836 case USB_RECIP_INTERFACE
:
837 request_handler_interface(req
);
839 case USB_RECIP_ENDPOINT
:
840 request_handler_endpoint(req
);
842 case USB_RECIP_OTHER
:
843 logf("unsupported recipient");
846 //logf("control handled");
849 /* called by usb_drv_int() */
850 void usb_core_bus_reset(void)
854 #ifdef HAVE_USB_CHARGING_ENABLE
855 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
859 /* called by usb_drv_transfer_completed() */
860 void usb_core_transfer_complete(int endpoint
, int dir
, int status
, int length
)
862 struct usb_transfer_completion_event_data
*completion_event
;
866 /* already handled */
870 completion_event
= &ep_data
[endpoint
].completion_event
[EP_DIR(dir
)];
872 completion_event
->endpoint
= endpoint
;
873 completion_event
->dir
= dir
;
874 completion_event
->data
= 0;
875 completion_event
->status
= status
;
876 completion_event
->length
= length
;
877 /* All other endoints. Let the thread deal with it */
878 usb_signal_transfer_completion(completion_event
);
883 /* called by usb_drv_int() */
884 void usb_core_control_request(struct usb_ctrlrequest
* req
)
886 struct usb_transfer_completion_event_data
* completion_event
=
887 &ep_data
[EP_CONTROL
].completion_event
[EP_DIR(USB_DIR_IN
)];
889 completion_event
->endpoint
= EP_CONTROL
;
890 completion_event
->dir
= 0;
891 completion_event
->data
= (void*)req
;
892 completion_event
->status
= 0;
893 completion_event
->length
= 0;
894 logf("ctrl received %ld", current_tick
);
895 usb_signal_transfer_completion(completion_event
);
898 #ifdef HAVE_USB_CHARGING_ENABLE
899 void usb_charging_enable(int state
)
901 usb_charging_mode
= state
;
902 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
905 int usb_charging_maxcurrent()
907 if (!initialized
|| usb_charging_mode
== USB_CHARGING_DISABLE
)
909 if (usb_state
== CONFIGURED
)
910 return usb_charging_current_requested
;
911 if (usb_charging_mode
== USB_CHARGING_FORCE
&& usb_no_host
)