Fix Theme Editor build.
[maemo-rb.git] / firmware / usbstack / usb_core.c
blob2a3cc1c525c4c4e37920d260cc4b4a761bfd1c7f
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 #if (CONFIG_CPU == IMX233)
62 #include "ocotp-imx233.h"
63 #endif
65 #ifndef USB_MAX_CURRENT
66 #define USB_MAX_CURRENT 500
67 #endif
69 /*-------------------------------------------------------------------------*/
70 /* USB protocol descriptors: */
72 static struct usb_device_descriptor __attribute__((aligned(2)))
73 device_descriptor=
75 .bLength = sizeof(struct usb_device_descriptor),
76 .bDescriptorType = USB_DT_DEVICE,
77 #ifndef USB_NO_HIGH_SPEED
78 .bcdUSB = 0x0200,
79 #else
80 .bcdUSB = 0x0110,
81 #endif
82 .bDeviceClass = USB_CLASS_PER_INTERFACE,
83 .bDeviceSubClass = 0,
84 .bDeviceProtocol = 0,
85 .bMaxPacketSize0 = 64,
86 .idVendor = USB_VENDOR_ID,
87 .idProduct = USB_PRODUCT_ID,
88 .bcdDevice = 0x0100,
89 .iManufacturer = 1,
90 .iProduct = 2,
91 .iSerialNumber = 3,
92 .bNumConfigurations = 1
93 } ;
95 static struct usb_config_descriptor __attribute__((aligned(2)))
96 config_descriptor =
98 .bLength = sizeof(struct usb_config_descriptor),
99 .bDescriptorType = USB_DT_CONFIG,
100 .wTotalLength = 0, /* will be filled in later */
101 .bNumInterfaces = 1,
102 .bConfigurationValue = 1,
103 .iConfiguration = 0,
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,
113 .bcdUSB = 0x0200,
114 .bDeviceClass = 0,
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 =
125 USB_DT_STRING,
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 =
133 USB_DT_STRING,
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)))
140 usb_string_iSerial =
143 USB_DT_STRING,
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)))
154 lang_descriptor =
157 USB_DT_STRING,
158 {0x0409} /* LANGID US English */
161 static const struct usb_string_descriptor* const usb_strings[] =
163 &lang_descriptor,
164 &usb_string_iManufacturer,
165 &usb_string_iProduct,
166 &usb_string_iSerial
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)
181 (void)tmo;
182 usb_no_host = true;
183 usb_charger_update();
184 return 0;
186 #endif
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);
194 static struct
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] = {
205 .enabled = false,
206 .needs_exclusive_storage = true,
207 .first_interface = 0,
208 .last_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,
217 #ifdef HAVE_HOTSWAP
218 .notify_hotswap = usb_storage_notify_hotswap,
219 #endif
221 #endif
222 #ifdef USB_ENABLE_SERIAL
223 [USB_DRIVER_SERIAL] = {
224 .enabled = false,
225 .needs_exclusive_storage = false,
226 .first_interface = 0,
227 .last_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,
236 #ifdef HAVE_HOTSWAP
237 .notify_hotswap = NULL,
238 #endif
240 #endif
241 #ifdef USB_ENABLE_CHARGING_ONLY
242 [USB_DRIVER_CHARGING_ONLY] = {
243 .enabled = false,
244 .needs_exclusive_storage = false,
245 .first_interface = 0,
246 .last_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,
251 .init = NULL,
252 .disconnect = NULL,
253 .transfer_complete = NULL,
254 .control_request = NULL,
255 #ifdef HAVE_HOTSWAP
256 .notify_hotswap = NULL,
257 #endif
259 #endif
260 #ifdef USB_ENABLE_HID
261 [USB_DRIVER_HID] = {
262 .enabled = false,
263 .needs_exclusive_storage = false,
264 .first_interface = 0,
265 .last_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,
274 #ifdef HAVE_HOTSWAP
275 .notify_hotswap = NULL,
276 #endif
278 #endif
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'};
297 #ifdef IPOD_ARCH
298 static void set_serial_descriptor(void)
300 #ifdef IPOD_VIDEO
301 uint32_t* serial = (uint32_t*)0x20004034;
302 #else
303 uint32_t* serial = (uint32_t*)0x20002034;
304 #endif
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];
309 uint32_t x;
310 int i, j;
312 for(i = 0; i < 2; i++) {
313 x = serial[i];
314 for(j = 0; j < 8; j++) {
315 *p-- = hex[x & 0xf];
316 x >>= 4;
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];
327 int i;
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];
344 ops <<= 4;
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
351 * from the disk */
352 static void set_serial_descriptor(void)
354 short* p = &usb_string_iSerial.wString[1];
355 unsigned short* identify = ata_get_identify();
356 unsigned short x;
357 int i;
359 for(i = 10; i < 20; i++) {
360 x = identify[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];
374 int i;
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;
381 #else
382 static void set_serial_descriptor(void)
384 device_descriptor.iSerialNumber = 0;
386 #endif
388 void usb_core_init(void)
390 int i;
391 if (initialized)
392 return;
394 usb_drv_init();
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)
401 drivers[i].init();
403 initialized = true;
404 usb_state = DEFAULT;
405 #ifdef HAVE_USB_CHARGING_ENABLE
406 usb_no_host = false;
407 timeout_register(&usb_no_host_timeout, usb_no_host_callback, HZ*10, 0);
408 #endif
409 logf("usb_core_init() finished");
412 void usb_core_exit(void)
414 int i;
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;
422 if(initialized) {
423 usb_drv_exit();
424 initialized = false;
426 usb_state = DEFAULT;
427 #ifdef HAVE_USB_CHARGING_ENABLE
428 usb_no_host = false;
429 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
430 #endif
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;
440 switch(ep) {
441 case EP_CONTROL:
442 logf("ctrl handled %ld",current_tick);
443 usb_core_control_request_handler(
444 (struct usb_ctrlrequest*)event->data);
445 break;
446 default:
447 handler = ep_data[ep].completion_handler[EP_DIR(event->dir)];
448 if(handler != NULL)
449 handler(ep, event->dir, event->status, event->length);
450 break;
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)
466 int i;
467 for(i = 0; i < USB_NUM_DRIVERS; i++)
468 if(drivers[i].enabled && drivers[i].needs_exclusive_storage)
469 return true;
471 return false;
474 #ifdef HAVE_HOTSWAP
475 void usb_core_hotswap_event(int volume, bool inserted)
477 int i;
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);
482 #endif
484 static void usb_core_set_serial_function_id(void)
486 int i, id = 0;
488 for(i = 0; i < USB_NUM_DRIVERS; i++)
489 if(drivers[i].enabled)
490 id |= 1 << i;
492 usb_string_iSerial.wString[0] = hex[id];
495 int usb_core_request_endpoint(int type, int dir, struct usb_class_driver* drv)
497 int ret, ep;
499 ret = usb_drv_request_endpoint(type, dir);
501 if(ret == -1)
502 return -1;
504 dir = EP_DIR(ret);
505 ep = EP_NUM(ret);
507 ep_data[ep].completion_handler[dir] = drv->transfer_complete;
508 ep_data[ep].control_handler[dir] = drv->control_request;
510 return ret;
513 void usb_core_release_endpoint(int ep)
515 int dir;
517 usb_drv_release_endpoint(ep);
519 dir = EP_DIR(ep);
520 ep = EP_NUM(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)
528 int i;
529 int interface = 0;
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;
544 continue;
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);
567 if(handled)
568 break;
571 if(!handled) {
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)
580 int size;
581 const void* ptr = NULL;
582 int length = req->wLength;
583 int index = req->wValue & 0xff;
585 switch(req->wValue >> 8) { /* type */
586 case USB_DT_DEVICE:
587 ptr = &device_descriptor;
588 size = sizeof(struct usb_device_descriptor);
589 break;
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;
599 else {
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;
609 else {
610 config_descriptor.bMaxPower = (500+1)/2;
611 usb_charging_current_requested = 500;
613 #endif
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));
626 ptr = response_data;
627 break;
630 case USB_DT_STRING:
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];
637 else {
638 logf("bad string id %d", index);
639 usb_drv_stall(EP_CONTROL, true, true);
641 break;
643 case USB_DT_DEVICE_QUALIFIER:
644 ptr = &qualifier_descriptor;
645 size = sizeof(struct usb_qualifier_descriptor);
646 break;
648 default:
649 logf("ctrl desc.");
650 control_request_handler_drivers(req);
651 break;
654 if(ptr) {
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;
670 usb_state = ADDRESS;
673 static void usb_core_do_set_config(uint8_t config)
675 logf("usb_core: SET_CONFIG");
676 if(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();
682 else
683 usb_state = ADDRESS;
684 #ifdef HAVE_USB_CHARGING_ENABLE
685 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
686 #endif
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);
697 break;
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);
703 break;
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);
712 break;
714 case USB_REQ_GET_DESCRIPTOR:
715 logf("usb_core: GET_DESC %d", req->wValue >> 8);
716 request_handler_device_get_descriptor(req);
717 break;
718 case USB_REQ_CLEAR_FEATURE:
719 break;
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);
726 break;
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);
732 break;
733 default:
734 logf("bad req:desc %d:%d", req->bRequest, req->wValue);
735 usb_drv_stall(EP_CONTROL, true, true);
736 break;
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);
747 break;
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);
754 break;
755 case USB_REQ_CLEAR_FEATURE:
756 break;
757 case USB_REQ_SET_FEATURE:
758 break;
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);
764 break;
765 default:
766 control_request_handler_drivers(req);
767 break;
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);
776 break;
777 case USB_TYPE_CLASS:
778 control_request_handler_drivers(req);
779 break;
780 case USB_TYPE_VENDOR:
781 default:
782 logf("bad req:desc %d", req->bRequest);
783 usb_drv_stall(EP_CONTROL, true, true);
784 break;
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)
794 control_handler =
795 ep_data[EP_NUM(req->wIndex)].control_handler[EP_DIR(req->wIndex)];
797 if(control_handler)
798 handled = control_handler(req, response_data);
800 if(!handled) {
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);
815 break;
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);
821 break;
822 case USB_REQ_GET_STATUS:
823 response_data[0] = 0;
824 response_data[1] = 0;
825 logf("usb_core: GET_STATUS");
826 if(req->wIndex > 0)
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);
832 break;
833 default:
834 request_handler_endoint_drivers(req);
835 break;
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);
844 break;
845 case USB_TYPE_CLASS:
846 request_handler_endoint_drivers(req);
847 break;
848 case USB_TYPE_VENDOR:
849 default:
850 logf("bad req:desc %d", req->bRequest);
851 usb_drv_stall(EP_CONTROL, true, true);
852 break;
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);
861 if(usb_no_host) {
862 usb_no_host = false;
863 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
865 #endif
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);
876 break;
877 case USB_RECIP_INTERFACE:
878 request_handler_interface(req);
879 break;
880 case USB_RECIP_ENDPOINT:
881 request_handler_endpoint(req);
882 break;
883 case USB_RECIP_OTHER:
884 logf("unsupported recipient");
885 usb_drv_stall(EP_CONTROL, true, true);
886 break;
888 //logf("control handled");
891 /* called by usb_drv_int() */
892 void usb_core_bus_reset(void)
894 usb_address = 0;
895 usb_state = DEFAULT;
896 #ifdef HAVE_USB_CHARGING_ENABLE
897 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
898 #endif
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;
906 switch (endpoint) {
907 case EP_CONTROL:
908 /* already handled */
909 break;
911 default:
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);
921 break;
925 void usb_core_handle_notify(long id, intptr_t data)
927 switch(id)
929 case USB_NOTIFY_SET_ADDR:
930 usb_core_do_set_addr(data);
931 break;
932 case USB_NOTIFY_SET_CONFIG:
933 usb_core_do_set_config(data);
934 break;
935 default:
936 break;
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)
977 return 100;
978 if (usb_state == CONFIGURED)
979 return usb_charging_current_requested;
980 if (usb_charging_mode == USB_CHARGING_FORCE && usb_no_host)
981 return 500;
982 return 100;
984 #endif