FS#11968 by Peter Lecky - Slovak language update
[maemo-rb.git] / firmware / usbstack / usb_core.c
blobaa96fff17f5af4e9ee2eda92bda562387b50e16f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "system.h"
22 #include "thread.h"
23 #include "kernel.h"
24 #include "string.h"
25 /*#define LOGF_ENABLE*/
26 #include "logf.h"
28 #include "usb.h"
29 #include "usb_ch9.h"
30 #include "usb_drv.h"
31 #include "usb_core.h"
32 #include "usb_class_driver.h"
34 #if defined(USB_ENABLE_STORAGE)
35 #include "usb_storage.h"
36 #endif
38 #if defined(USB_ENABLE_SERIAL)
39 #include "usb_serial.h"
40 #endif
42 #if defined(USB_ENABLE_CHARGING_ONLY)
43 #include "usb_charging_only.h"
44 #endif
46 #ifdef USB_ENABLE_HID
47 #include "usb_hid.h"
48 #endif
50 /* TODO: Move target-specific stuff somewhere else (serial number reading) */
52 #ifdef HAVE_AS3514
53 #include "ascodec.h"
54 #include "as3514.h"
55 #endif
57 #if !defined(HAVE_AS3514) && !defined(IPOD_ARCH) && (CONFIG_STORAGE & STORAGE_ATA)
58 #include "ata.h"
59 #endif
61 #ifndef USB_MAX_CURRENT
62 #define USB_MAX_CURRENT 500
63 #endif
65 /*-------------------------------------------------------------------------*/
66 /* USB protocol descriptors: */
68 static struct usb_device_descriptor __attribute__((aligned(2)))
69 device_descriptor=
71 .bLength = sizeof(struct usb_device_descriptor),
72 .bDescriptorType = USB_DT_DEVICE,
73 #ifndef USB_NO_HIGH_SPEED
74 .bcdUSB = 0x0200,
75 #else
76 .bcdUSB = 0x0110,
77 #endif
78 .bDeviceClass = USB_CLASS_PER_INTERFACE,
79 .bDeviceSubClass = 0,
80 .bDeviceProtocol = 0,
81 .bMaxPacketSize0 = 64,
82 .idVendor = USB_VENDOR_ID,
83 .idProduct = USB_PRODUCT_ID,
84 .bcdDevice = 0x0100,
85 .iManufacturer = 1,
86 .iProduct = 2,
87 .iSerialNumber = 3,
88 .bNumConfigurations = 1
89 } ;
91 static struct usb_config_descriptor __attribute__((aligned(2)))
92 config_descriptor =
94 .bLength = sizeof(struct usb_config_descriptor),
95 .bDescriptorType = USB_DT_CONFIG,
96 .wTotalLength = 0, /* will be filled in later */
97 .bNumInterfaces = 1,
98 .bConfigurationValue = 1,
99 .iConfiguration = 0,
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,
109 .bcdUSB = 0x0200,
110 .bDeviceClass = 0,
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 =
121 USB_DT_STRING,
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 =
129 USB_DT_STRING,
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)))
136 usb_string_iSerial =
139 USB_DT_STRING,
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)))
150 lang_descriptor =
153 USB_DT_STRING,
154 {0x0409} /* LANGID US English */
157 static const struct usb_string_descriptor* const usb_strings[] =
159 &lang_descriptor,
160 &usb_string_iManufacturer,
161 &usb_string_iProduct,
162 &usb_string_iSerial
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)
177 (void)tmo;
178 usb_no_host = true;
179 usb_charger_update();
180 return 0;
182 #endif
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);
190 static struct
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] = {
201 .enabled = false,
202 .needs_exclusive_storage = true,
203 .first_interface = 0,
204 .last_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,
213 #ifdef HAVE_HOTSWAP
214 .notify_hotswap = usb_storage_notify_hotswap,
215 #endif
217 #endif
218 #ifdef USB_ENABLE_SERIAL
219 [USB_DRIVER_SERIAL] = {
220 .enabled = false,
221 .needs_exclusive_storage = false,
222 .first_interface = 0,
223 .last_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,
232 #ifdef HAVE_HOTSWAP
233 .notify_hotswap = NULL,
234 #endif
236 #endif
237 #ifdef USB_ENABLE_CHARGING_ONLY
238 [USB_DRIVER_CHARGING_ONLY] = {
239 .enabled = false,
240 .needs_exclusive_storage = false,
241 .first_interface = 0,
242 .last_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,
247 .init = NULL,
248 .disconnect = NULL,
249 .transfer_complete = NULL,
250 .control_request = NULL,
251 #ifdef HAVE_HOTSWAP
252 .notify_hotswap = NULL,
253 #endif
255 #endif
256 #ifdef USB_ENABLE_HID
257 [USB_DRIVER_HID] = {
258 .enabled = false,
259 .needs_exclusive_storage = false,
260 .first_interface = 0,
261 .last_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,
270 #ifdef HAVE_HOTSWAP
271 .notify_hotswap = NULL,
272 #endif
274 #endif
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'};
283 #ifdef IPOD_ARCH
284 static void set_serial_descriptor(void)
286 #ifdef IPOD_VIDEO
287 uint32_t* serial = (uint32_t*)0x20004034;
288 #else
289 uint32_t* serial = (uint32_t*)0x20002034;
290 #endif
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];
295 uint32_t x;
296 int i, j;
298 for(i = 0; i < 2; i++) {
299 x = serial[i];
300 for(j = 0; j < 8; j++) {
301 *p-- = hex[x & 0xf];
302 x >>= 4;
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];
313 int i;
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
324 * from the disk */
325 static void set_serial_descriptor(void)
327 short* p = &usb_string_iSerial.wString[1];
328 unsigned short* identify = ata_get_identify();
329 unsigned short x;
330 int i;
332 for(i = 10; i < 20; i++) {
333 x = identify[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];
347 int i;
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;
354 #else
355 static void set_serial_descriptor(void)
357 device_descriptor.iSerialNumber = 0;
359 #endif
361 void usb_core_init(void)
363 int i;
364 if (initialized)
365 return;
367 usb_drv_init();
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)
374 drivers[i].init();
376 initialized = true;
377 usb_state = DEFAULT;
378 #ifdef HAVE_USB_CHARGING_ENABLE
379 usb_no_host = false;
380 timeout_register(&usb_no_host_timeout, usb_no_host_callback, HZ*10, 0);
381 #endif
382 logf("usb_core_init() finished");
385 void usb_core_exit(void)
387 int i;
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;
395 if(initialized) {
396 usb_drv_exit();
397 initialized = false;
399 usb_state = DEFAULT;
400 #ifdef HAVE_USB_CHARGING_ENABLE
401 usb_no_host = false;
402 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
403 #endif
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;
413 switch(ep) {
414 case EP_CONTROL:
415 logf("ctrl handled %ld",current_tick);
416 usb_core_control_request_handler(
417 (struct usb_ctrlrequest*)event->data);
418 break;
419 default:
420 handler = ep_data[ep].completion_handler[EP_DIR(event->dir)];
421 if(handler != NULL)
422 handler(ep, event->dir, event->status, event->length);
423 break;
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)
439 int i;
440 for(i = 0; i < USB_NUM_DRIVERS; i++)
441 if(drivers[i].enabled && drivers[i].needs_exclusive_storage)
442 return true;
444 return false;
447 #ifdef HAVE_HOTSWAP
448 void usb_core_hotswap_event(int volume, bool inserted)
450 int i;
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);
455 #endif
457 static void usb_core_set_serial_function_id(void)
459 int i, id = 0;
461 for(i = 0; i < USB_NUM_DRIVERS; i++)
462 if(drivers[i].enabled)
463 id |= 1 << i;
465 usb_string_iSerial.wString[0] = hex[id];
468 int usb_core_request_endpoint(int type, int dir, struct usb_class_driver* drv)
470 int ret, ep;
472 ret = usb_drv_request_endpoint(type, dir);
474 if(ret == -1)
475 return -1;
477 dir = EP_DIR(ret);
478 ep = EP_NUM(ret);
480 ep_data[ep].completion_handler[dir] = drv->transfer_complete;
481 ep_data[ep].control_handler[dir] = drv->control_request;
483 return ret;
486 void usb_core_release_endpoint(int ep)
488 int dir;
490 usb_drv_release_endpoint(ep);
492 dir = EP_DIR(ep);
493 ep = EP_NUM(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)
501 int i;
502 int interface = 0;
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;
517 continue;
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);
540 if(handled)
541 break;
544 if(!handled) {
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)
553 int size;
554 const void* ptr = NULL;
555 int length = req->wLength;
556 int index = req->wValue & 0xff;
558 switch(req->wValue >> 8) { /* type */
559 case USB_DT_DEVICE:
560 ptr = &device_descriptor;
561 size = sizeof(struct usb_device_descriptor);
562 #ifdef USB_DETECT_BY_CORE
563 /* Something requested a device descriptor; consider this a legit
564 connection */
565 usb_drv_usb_detect_event();
566 #endif
567 break;
569 case USB_DT_OTHER_SPEED_CONFIG:
570 case USB_DT_CONFIG: {
571 int i, max_packet_size;
573 if(req->wValue>>8==USB_DT_CONFIG) {
574 max_packet_size = (usb_drv_port_speed() ? 512 : 64);
575 config_descriptor.bDescriptorType = USB_DT_CONFIG;
577 else {
578 max_packet_size=(usb_drv_port_speed() ? 64 : 512);
579 config_descriptor.bDescriptorType =
580 USB_DT_OTHER_SPEED_CONFIG;
582 #ifdef HAVE_USB_CHARGING_ENABLE
583 if (usb_charging_mode == USB_CHARGING_DISABLE) {
584 config_descriptor.bMaxPower = (100+1)/2;
585 usb_charging_current_requested = 100;
587 else {
588 config_descriptor.bMaxPower = (500+1)/2;
589 usb_charging_current_requested = 500;
591 #endif
592 size = sizeof(struct usb_config_descriptor);
594 for(i = 0; i < USB_NUM_DRIVERS; i++)
595 if(drivers[i].enabled && drivers[i].get_config_descriptor)
596 size += drivers[i].get_config_descriptor(
597 &response_data[size], max_packet_size);
599 config_descriptor.bNumInterfaces = usb_core_num_interfaces;
600 config_descriptor.wTotalLength = (uint16_t)size;
601 memcpy(&response_data[0], &config_descriptor,
602 sizeof(struct usb_config_descriptor));
604 ptr = response_data;
605 break;
608 case USB_DT_STRING:
609 logf("STRING %d", index);
610 if((unsigned)index < (sizeof(usb_strings) /
611 sizeof(struct usb_string_descriptor*))) {
612 size = usb_strings[index]->bLength;
613 ptr = usb_strings[index];
615 else {
616 logf("bad string id %d", index);
617 usb_drv_stall(EP_CONTROL, true, true);
619 break;
621 case USB_DT_DEVICE_QUALIFIER:
622 ptr = &qualifier_descriptor;
623 size = sizeof(struct usb_qualifier_descriptor);
624 break;
626 default:
627 logf("ctrl desc.");
628 control_request_handler_drivers(req);
629 break;
632 if(ptr) {
633 logf("data %d (%d)", size, length);
634 length = MIN(size, length);
636 if (ptr != response_data)
637 memcpy(response_data, ptr, length);
639 usb_drv_recv(EP_CONTROL, NULL, 0);
640 usb_drv_send(EP_CONTROL, response_data, length);
644 static void request_handler_device(struct usb_ctrlrequest* req)
646 int i;
648 switch(req->bRequest) {
649 case USB_REQ_GET_CONFIGURATION: {
650 logf("usb_core: GET_CONFIG");
651 response_data[0] = (usb_state == ADDRESS ? 0 : 1);
652 usb_drv_recv(EP_CONTROL, NULL, 0);
653 usb_drv_send(EP_CONTROL, response_data, 1);
654 break;
656 case USB_REQ_SET_CONFIGURATION: {
657 logf("usb_core: SET_CONFIG");
658 usb_drv_cancel_all_transfers();
659 if(req->wValue) {
660 usb_state = CONFIGURED;
661 for(i = 0; i < USB_NUM_DRIVERS; i++)
662 if(drivers[i].enabled && drivers[i].init_connection)
663 drivers[i].init_connection();
665 else
666 usb_state = ADDRESS;
667 usb_drv_send(EP_CONTROL, NULL, 0);
668 #ifdef HAVE_USB_CHARGING_ENABLE
669 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
670 #endif
671 break;
673 case USB_REQ_SET_ADDRESS: {
674 unsigned char address = req->wValue;
675 logf("usb_core: SET_ADR %d", address);
676 usb_drv_send(EP_CONTROL, NULL, 0);
677 usb_drv_cancel_all_transfers();
678 usb_address = address;
679 usb_drv_set_address(usb_address);
680 usb_state = ADDRESS;
681 break;
683 case USB_REQ_GET_DESCRIPTOR:
684 logf("usb_core: GET_DESC %d", req->wValue >> 8);
685 request_handler_device_get_descriptor(req);
686 break;
687 case USB_REQ_CLEAR_FEATURE:
688 break;
689 case USB_REQ_SET_FEATURE:
690 if(req->wValue==USB_DEVICE_TEST_MODE) {
691 int mode = req->wIndex >> 8;
692 usb_drv_send(EP_CONTROL, NULL, 0);
693 usb_drv_set_test_mode(mode);
695 break;
696 case USB_REQ_GET_STATUS:
697 response_data[0] = 0;
698 response_data[1] = 0;
699 usb_drv_recv(EP_CONTROL, NULL, 0);
700 usb_drv_send(EP_CONTROL, response_data, 2);
701 break;
702 default:
703 logf("bad req:desc %d:%d", req->bRequest, req->wValue);
704 usb_drv_stall(EP_CONTROL, true, true);
705 break;
709 static void request_handler_interface_standard(struct usb_ctrlrequest* req)
711 switch (req->bRequest)
713 case USB_REQ_SET_INTERFACE:
714 logf("usb_core: SET_INTERFACE");
715 usb_drv_send(EP_CONTROL, NULL, 0);
716 break;
718 case USB_REQ_GET_INTERFACE:
719 logf("usb_core: GET_INTERFACE");
720 response_data[0] = 0;
721 usb_drv_recv(EP_CONTROL, NULL, 0);
722 usb_drv_send(EP_CONTROL, response_data, 1);
723 break;
724 case USB_REQ_CLEAR_FEATURE:
725 break;
726 case USB_REQ_SET_FEATURE:
727 break;
728 case USB_REQ_GET_STATUS:
729 response_data[0] = 0;
730 response_data[1] = 0;
731 usb_drv_recv(EP_CONTROL, NULL, 0);
732 usb_drv_send(EP_CONTROL, response_data, 2);
733 break;
734 default:
735 control_request_handler_drivers(req);
736 break;
740 static void request_handler_interface(struct usb_ctrlrequest* req)
742 switch(req->bRequestType & USB_TYPE_MASK) {
743 case USB_TYPE_STANDARD:
744 request_handler_interface_standard(req);
745 break;
746 case USB_TYPE_CLASS:
747 control_request_handler_drivers(req);
748 break;
749 case USB_TYPE_VENDOR:
750 default:
751 logf("bad req:desc %d", req->bRequest);
752 usb_drv_stall(EP_CONTROL, true, true);
753 break;
757 static void request_handler_endoint_drivers(struct usb_ctrlrequest* req)
759 bool handled = false;
760 control_handler_t control_handler = NULL;
762 if(EP_NUM(req->wIndex) < USB_NUM_ENDPOINTS)
763 control_handler =
764 ep_data[EP_NUM(req->wIndex)].control_handler[EP_DIR(req->wIndex)];
766 if(control_handler)
767 handled = control_handler(req, response_data);
769 if(!handled) {
770 /* nope. flag error */
771 logf("usb bad req %d", req->bRequest);
772 usb_drv_stall(EP_CONTROL, true, true);
776 static void request_handler_endpoint_standard(struct usb_ctrlrequest* req)
778 switch (req->bRequest) {
779 case USB_REQ_CLEAR_FEATURE:
780 if(req->wValue == USB_ENDPOINT_HALT)
781 usb_drv_stall(EP_NUM(req->wIndex), false, EP_DIR(req->wIndex));
783 usb_drv_send(EP_CONTROL, NULL, 0);
784 break;
785 case USB_REQ_SET_FEATURE:
786 if(req->wValue == USB_ENDPOINT_HALT)
787 usb_drv_stall(EP_NUM(req->wIndex), true, EP_DIR(req->wIndex));
789 usb_drv_send(EP_CONTROL, NULL, 0);
790 break;
791 case USB_REQ_GET_STATUS:
792 response_data[0] = 0;
793 response_data[1] = 0;
794 logf("usb_core: GET_STATUS");
795 if(req->wIndex > 0)
796 response_data[0] = usb_drv_stalled(EP_NUM(req->wIndex),
797 EP_DIR(req->wIndex));
799 usb_drv_recv(EP_CONTROL, NULL, 0);
800 usb_drv_send(EP_CONTROL, response_data, 2);
801 break;
802 default:
803 request_handler_endoint_drivers(req);
804 break;
808 static void request_handler_endpoint(struct usb_ctrlrequest* req)
810 switch(req->bRequestType & USB_TYPE_MASK) {
811 case USB_TYPE_STANDARD:
812 request_handler_endpoint_standard(req);
813 break;
814 case USB_TYPE_CLASS:
815 request_handler_endoint_drivers(req);
816 break;
817 case USB_TYPE_VENDOR:
818 default:
819 logf("bad req:desc %d", req->bRequest);
820 usb_drv_stall(EP_CONTROL, true, true);
821 break;
825 /* Handling USB requests starts here */
826 static void usb_core_control_request_handler(struct usb_ctrlrequest* req)
828 #ifdef HAVE_USB_CHARGING_ENABLE
829 timeout_cancel(&usb_no_host_timeout);
830 if(usb_no_host) {
831 usb_no_host = false;
832 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
834 #endif
835 if(usb_state == DEFAULT) {
836 set_serial_descriptor();
837 usb_core_set_serial_function_id();
839 allocate_interfaces_and_endpoints();
842 switch(req->bRequestType & USB_RECIP_MASK) {
843 case USB_RECIP_DEVICE:
844 request_handler_device(req);
845 break;
846 case USB_RECIP_INTERFACE:
847 request_handler_interface(req);
848 break;
849 case USB_RECIP_ENDPOINT:
850 request_handler_endpoint(req);
851 break;
852 case USB_RECIP_OTHER:
853 logf("unsupported recipient");
854 usb_drv_stall(EP_CONTROL, true, true);
855 break;
857 //logf("control handled");
860 /* called by usb_drv_int() */
861 void usb_core_bus_reset(void)
863 usb_address = 0;
864 usb_state = DEFAULT;
865 #ifdef HAVE_USB_CHARGING_ENABLE
866 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
867 #endif
870 /* called by usb_drv_transfer_completed() */
871 void usb_core_transfer_complete(int endpoint, int dir, int status, int length)
873 struct usb_transfer_completion_event_data *completion_event;
875 switch (endpoint) {
876 case EP_CONTROL:
877 /* already handled */
878 break;
880 default:
881 completion_event = &ep_data[endpoint].completion_event[EP_DIR(dir)];
883 completion_event->endpoint = endpoint;
884 completion_event->dir = dir;
885 completion_event->data = 0;
886 completion_event->status = status;
887 completion_event->length = length;
888 /* All other endoints. Let the thread deal with it */
889 usb_signal_transfer_completion(completion_event);
890 break;
894 /* called by usb_drv_int() */
895 void usb_core_control_request(struct usb_ctrlrequest* req)
897 struct usb_transfer_completion_event_data* completion_event =
898 &ep_data[EP_CONTROL].completion_event[EP_DIR(USB_DIR_IN)];
900 completion_event->endpoint = EP_CONTROL;
901 completion_event->dir = 0;
902 completion_event->data = (void*)req;
903 completion_event->status = 0;
904 completion_event->length = 0;
905 logf("ctrl received %ld", current_tick);
906 usb_signal_transfer_completion(completion_event);
909 #ifdef HAVE_USB_CHARGING_ENABLE
910 void usb_charging_enable(int state)
912 usb_charging_mode = state;
913 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
916 int usb_charging_maxcurrent()
918 if (!initialized || usb_charging_mode == USB_CHARGING_DISABLE)
919 return 100;
920 if (usb_state == CONFIGURED)
921 return usb_charging_current_requested;
922 if (usb_charging_mode == USB_CHARGING_FORCE && usb_no_host)
923 return 500;
924 return 100;
926 #endif