ypr0: Correct .scrobbler.log path for YPR0
[maemo-rb.git] / firmware / usbstack / usb_core.c
blob801325b692ace5cbe58326d1cf031f9df7500f9f
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 request_handler_device(struct usb_ctrlrequest* req)
668 int i;
670 switch(req->bRequest) {
671 case USB_REQ_GET_CONFIGURATION: {
672 logf("usb_core: GET_CONFIG");
673 response_data[0] = (usb_state == ADDRESS ? 0 : 1);
674 usb_drv_recv(EP_CONTROL, NULL, 0);
675 usb_drv_send(EP_CONTROL, response_data, 1);
676 break;
678 case USB_REQ_SET_CONFIGURATION: {
679 logf("usb_core: SET_CONFIG");
680 usb_drv_cancel_all_transfers();
681 if(req->wValue) {
682 usb_state = CONFIGURED;
683 for(i = 0; i < USB_NUM_DRIVERS; i++)
684 if(drivers[i].enabled && drivers[i].init_connection)
685 drivers[i].init_connection();
687 else
688 usb_state = ADDRESS;
689 usb_drv_send(EP_CONTROL, NULL, 0);
690 #ifdef HAVE_USB_CHARGING_ENABLE
691 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
692 #endif
693 break;
695 case USB_REQ_SET_ADDRESS: {
696 unsigned char address = req->wValue;
697 logf("usb_core: SET_ADR %d", address);
698 usb_drv_send(EP_CONTROL, NULL, 0);
699 usb_drv_cancel_all_transfers();
700 usb_address = address;
701 usb_drv_set_address(usb_address);
702 usb_state = ADDRESS;
703 break;
705 case USB_REQ_GET_DESCRIPTOR:
706 logf("usb_core: GET_DESC %d", req->wValue >> 8);
707 request_handler_device_get_descriptor(req);
708 break;
709 case USB_REQ_CLEAR_FEATURE:
710 break;
711 case USB_REQ_SET_FEATURE:
712 if(req->wValue==USB_DEVICE_TEST_MODE) {
713 int mode = req->wIndex >> 8;
714 usb_drv_send(EP_CONTROL, NULL, 0);
715 usb_drv_set_test_mode(mode);
717 break;
718 case USB_REQ_GET_STATUS:
719 response_data[0] = 0;
720 response_data[1] = 0;
721 usb_drv_recv(EP_CONTROL, NULL, 0);
722 usb_drv_send(EP_CONTROL, response_data, 2);
723 break;
724 default:
725 logf("bad req:desc %d:%d", req->bRequest, req->wValue);
726 usb_drv_stall(EP_CONTROL, true, true);
727 break;
731 static void request_handler_interface_standard(struct usb_ctrlrequest* req)
733 switch (req->bRequest)
735 case USB_REQ_SET_INTERFACE:
736 logf("usb_core: SET_INTERFACE");
737 usb_drv_send(EP_CONTROL, NULL, 0);
738 break;
740 case USB_REQ_GET_INTERFACE:
741 logf("usb_core: GET_INTERFACE");
742 response_data[0] = 0;
743 usb_drv_recv(EP_CONTROL, NULL, 0);
744 usb_drv_send(EP_CONTROL, response_data, 1);
745 break;
746 case USB_REQ_CLEAR_FEATURE:
747 break;
748 case USB_REQ_SET_FEATURE:
749 break;
750 case USB_REQ_GET_STATUS:
751 response_data[0] = 0;
752 response_data[1] = 0;
753 usb_drv_recv(EP_CONTROL, NULL, 0);
754 usb_drv_send(EP_CONTROL, response_data, 2);
755 break;
756 default:
757 control_request_handler_drivers(req);
758 break;
762 static void request_handler_interface(struct usb_ctrlrequest* req)
764 switch(req->bRequestType & USB_TYPE_MASK) {
765 case USB_TYPE_STANDARD:
766 request_handler_interface_standard(req);
767 break;
768 case USB_TYPE_CLASS:
769 control_request_handler_drivers(req);
770 break;
771 case USB_TYPE_VENDOR:
772 default:
773 logf("bad req:desc %d", req->bRequest);
774 usb_drv_stall(EP_CONTROL, true, true);
775 break;
779 static void request_handler_endoint_drivers(struct usb_ctrlrequest* req)
781 bool handled = false;
782 control_handler_t control_handler = NULL;
784 if(EP_NUM(req->wIndex) < USB_NUM_ENDPOINTS)
785 control_handler =
786 ep_data[EP_NUM(req->wIndex)].control_handler[EP_DIR(req->wIndex)];
788 if(control_handler)
789 handled = control_handler(req, response_data);
791 if(!handled) {
792 /* nope. flag error */
793 logf("usb bad req %d", req->bRequest);
794 usb_drv_stall(EP_CONTROL, true, true);
798 static void request_handler_endpoint_standard(struct usb_ctrlrequest* req)
800 switch (req->bRequest) {
801 case USB_REQ_CLEAR_FEATURE:
802 if(req->wValue == USB_ENDPOINT_HALT)
803 usb_drv_stall(EP_NUM(req->wIndex), false, EP_DIR(req->wIndex));
805 usb_drv_send(EP_CONTROL, NULL, 0);
806 break;
807 case USB_REQ_SET_FEATURE:
808 if(req->wValue == USB_ENDPOINT_HALT)
809 usb_drv_stall(EP_NUM(req->wIndex), true, EP_DIR(req->wIndex));
811 usb_drv_send(EP_CONTROL, NULL, 0);
812 break;
813 case USB_REQ_GET_STATUS:
814 response_data[0] = 0;
815 response_data[1] = 0;
816 logf("usb_core: GET_STATUS");
817 if(req->wIndex > 0)
818 response_data[0] = usb_drv_stalled(EP_NUM(req->wIndex),
819 EP_DIR(req->wIndex));
821 usb_drv_recv(EP_CONTROL, NULL, 0);
822 usb_drv_send(EP_CONTROL, response_data, 2);
823 break;
824 default:
825 request_handler_endoint_drivers(req);
826 break;
830 static void request_handler_endpoint(struct usb_ctrlrequest* req)
832 switch(req->bRequestType & USB_TYPE_MASK) {
833 case USB_TYPE_STANDARD:
834 request_handler_endpoint_standard(req);
835 break;
836 case USB_TYPE_CLASS:
837 request_handler_endoint_drivers(req);
838 break;
839 case USB_TYPE_VENDOR:
840 default:
841 logf("bad req:desc %d", req->bRequest);
842 usb_drv_stall(EP_CONTROL, true, true);
843 break;
847 /* Handling USB requests starts here */
848 static void usb_core_control_request_handler(struct usb_ctrlrequest* req)
850 #ifdef HAVE_USB_CHARGING_ENABLE
851 timeout_cancel(&usb_no_host_timeout);
852 if(usb_no_host) {
853 usb_no_host = false;
854 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
856 #endif
857 if(usb_state == DEFAULT) {
858 set_serial_descriptor();
859 usb_core_set_serial_function_id();
861 allocate_interfaces_and_endpoints();
864 switch(req->bRequestType & USB_RECIP_MASK) {
865 case USB_RECIP_DEVICE:
866 request_handler_device(req);
867 break;
868 case USB_RECIP_INTERFACE:
869 request_handler_interface(req);
870 break;
871 case USB_RECIP_ENDPOINT:
872 request_handler_endpoint(req);
873 break;
874 case USB_RECIP_OTHER:
875 logf("unsupported recipient");
876 usb_drv_stall(EP_CONTROL, true, true);
877 break;
879 //logf("control handled");
882 /* called by usb_drv_int() */
883 void usb_core_bus_reset(void)
885 usb_address = 0;
886 usb_state = DEFAULT;
887 #ifdef HAVE_USB_CHARGING_ENABLE
888 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
889 #endif
892 /* called by usb_drv_transfer_completed() */
893 void usb_core_transfer_complete(int endpoint, int dir, int status, int length)
895 struct usb_transfer_completion_event_data *completion_event;
897 switch (endpoint) {
898 case EP_CONTROL:
899 /* already handled */
900 break;
902 default:
903 completion_event = &ep_data[endpoint].completion_event[EP_DIR(dir)];
905 completion_event->endpoint = endpoint;
906 completion_event->dir = dir;
907 completion_event->data = 0;
908 completion_event->status = status;
909 completion_event->length = length;
910 /* All other endpoints. Let the thread deal with it */
911 usb_signal_transfer_completion(completion_event);
912 break;
916 /* called by usb_drv_int() */
917 void usb_core_control_request(struct usb_ctrlrequest* req)
919 struct usb_transfer_completion_event_data* completion_event =
920 &ep_data[EP_CONTROL].completion_event[EP_DIR(USB_DIR_IN)];
922 completion_event->endpoint = EP_CONTROL;
923 completion_event->dir = 0;
924 completion_event->data = (void*)req;
925 completion_event->status = 0;
926 completion_event->length = 0;
927 logf("ctrl received %ld", current_tick);
928 usb_signal_transfer_completion(completion_event);
931 #ifdef HAVE_USB_CHARGING_ENABLE
932 void usb_charging_enable(int state)
934 usb_charging_mode = state;
935 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
938 int usb_charging_maxcurrent()
940 if (!initialized || usb_charging_mode == USB_CHARGING_DISABLE)
941 return 100;
942 if (usb_state == CONFIGURED)
943 return usb_charging_current_requested;
944 if (usb_charging_mode == USB_CHARGING_FORCE && usb_no_host)
945 return 500;
946 return 100;
948 #endif