Update the manual according to the changes in r26587 (PLA_EXIT and PLA_CANCEL on...
[kugel-rb.git] / firmware / usbstack / usb_core.c
blobd6c8c6e5cc42d6d373d7a56a907f9ec585eb852b
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[16];
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, 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
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 bool handled = true;
555 const void* ptr = NULL;
556 int length = req->wLength;
557 int index = req->wValue & 0xff;
559 switch(req->wValue >> 8) { /* type */
560 case USB_DT_DEVICE:
561 ptr = &device_descriptor;
562 size = sizeof(struct usb_device_descriptor);
563 break;
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;
573 else {
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;
583 else {
584 config_descriptor.bMaxPower = (500+1)/2;
585 usb_charging_current_requested = 500;
587 #endif
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));
600 ptr = response_data;
601 break;
604 case USB_DT_STRING:
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];
611 else {
612 logf("bad string id %d", index);
613 usb_drv_stall(EP_CONTROL, true, true);
615 break;
617 case USB_DT_DEVICE_QUALIFIER:
618 ptr = &qualifier_descriptor;
619 size = sizeof(struct usb_qualifier_descriptor);
620 break;
622 default:
623 logf("ctrl desc.");
624 handled = false;
625 control_request_handler_drivers(req);
626 break;
629 if(ptr) {
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)
643 int i;
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);
651 break;
653 case USB_REQ_SET_CONFIGURATION: {
654 logf("usb_core: SET_CONFIG");
655 usb_drv_cancel_all_transfers();
656 if(req->wValue) {
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();
662 else
663 usb_state = ADDRESS;
664 usb_drv_send(EP_CONTROL, NULL, 0);
665 #ifdef HAVE_USB_CHARGING_ENABLE
666 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
667 #endif
668 break;
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);
677 usb_state = ADDRESS;
678 break;
680 case USB_REQ_GET_DESCRIPTOR:
681 logf("usb_core: GET_DESC %d", req->wValue >> 8);
682 request_handler_device_get_descriptor(req);
683 break;
684 case USB_REQ_CLEAR_FEATURE:
685 break;
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);
692 break;
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);
698 break;
699 default:
700 break;
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);
711 break;
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);
718 break;
719 case USB_REQ_CLEAR_FEATURE:
720 break;
721 case USB_REQ_SET_FEATURE:
722 break;
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);
728 break;
729 default:
730 control_request_handler_drivers(req);
731 break;
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);
740 break;
741 case USB_TYPE_CLASS:
742 control_request_handler_drivers(req);
743 break;
744 case USB_TYPE_VENDOR:
745 break;
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)
755 control_handler =
756 ep_data[EP_NUM(req->wIndex)].control_handler[EP_DIR(req->wIndex)];
758 if(control_handler)
759 handled = control_handler(req, response_data);
761 if(!handled) {
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);
776 break;
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);
782 break;
783 case USB_REQ_GET_STATUS:
784 response_data[0] = 0;
785 response_data[1] = 0;
786 logf("usb_core: GET_STATUS");
787 if(req->wIndex > 0)
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);
793 break;
794 default:
795 request_handler_endoint_drivers(req);
796 break;
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);
805 break;
806 case USB_TYPE_CLASS:
807 request_handler_endoint_drivers(req);
808 break;
809 case USB_TYPE_VENDOR:
810 default:
811 break;
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);
820 if(usb_no_host) {
821 usb_no_host = false;
822 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
824 #endif
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);
835 break;
836 case USB_RECIP_INTERFACE:
837 request_handler_interface(req);
838 break;
839 case USB_RECIP_ENDPOINT:
840 request_handler_endpoint(req);
841 break;
842 case USB_RECIP_OTHER:
843 logf("unsupported recipient");
844 break;
846 //logf("control handled");
849 /* called by usb_drv_int() */
850 void usb_core_bus_reset(void)
852 usb_address = 0;
853 usb_state = DEFAULT;
854 #ifdef HAVE_USB_CHARGING_ENABLE
855 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
856 #endif
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;
864 switch (endpoint) {
865 case EP_CONTROL:
866 /* already handled */
867 break;
869 default:
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);
879 break;
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)
908 return 100;
909 if (usb_state == CONFIGURED)
910 return usb_charging_current_requested;
911 if (usb_charging_mode == USB_CHARGING_FORCE && usb_no_host)
912 return 500;
913 return 100;
915 #endif