Call skinlist_set_cfg() unconditionally, i.e. also if do_refresh==false
[maemo-rb.git] / firmware / usbstack / usb_core.c
blobeb17073e5191597fb3e65c108645573e85122a7b
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 break;
564 case USB_DT_OTHER_SPEED_CONFIG:
565 case USB_DT_CONFIG: {
566 int i, max_packet_size;
568 if(req->wValue>>8==USB_DT_CONFIG) {
569 max_packet_size = (usb_drv_port_speed() ? 512 : 64);
570 config_descriptor.bDescriptorType = USB_DT_CONFIG;
572 else {
573 max_packet_size=(usb_drv_port_speed() ? 64 : 512);
574 config_descriptor.bDescriptorType =
575 USB_DT_OTHER_SPEED_CONFIG;
577 #ifdef HAVE_USB_CHARGING_ENABLE
578 if (usb_charging_mode == USB_CHARGING_DISABLE) {
579 config_descriptor.bMaxPower = (100+1)/2;
580 usb_charging_current_requested = 100;
582 else {
583 config_descriptor.bMaxPower = (500+1)/2;
584 usb_charging_current_requested = 500;
586 #endif
587 size = sizeof(struct usb_config_descriptor);
589 for(i = 0; i < USB_NUM_DRIVERS; i++)
590 if(drivers[i].enabled && drivers[i].get_config_descriptor)
591 size += drivers[i].get_config_descriptor(
592 &response_data[size], max_packet_size);
594 config_descriptor.bNumInterfaces = usb_core_num_interfaces;
595 config_descriptor.wTotalLength = (uint16_t)size;
596 memcpy(&response_data[0], &config_descriptor,
597 sizeof(struct usb_config_descriptor));
599 ptr = response_data;
600 break;
603 case USB_DT_STRING:
604 logf("STRING %d", index);
605 if((unsigned)index < (sizeof(usb_strings) /
606 sizeof(struct usb_string_descriptor*))) {
607 size = usb_strings[index]->bLength;
608 ptr = usb_strings[index];
610 else {
611 logf("bad string id %d", index);
612 usb_drv_stall(EP_CONTROL, true, true);
614 break;
616 case USB_DT_DEVICE_QUALIFIER:
617 ptr = &qualifier_descriptor;
618 size = sizeof(struct usb_qualifier_descriptor);
619 break;
621 default:
622 logf("ctrl desc.");
623 control_request_handler_drivers(req);
624 break;
627 if(ptr) {
628 logf("data %d (%d)", size, length);
629 length = MIN(size, length);
631 if (ptr != response_data)
632 memcpy(response_data, ptr, length);
634 usb_drv_recv(EP_CONTROL, NULL, 0);
635 usb_drv_send(EP_CONTROL, response_data, length);
639 static void request_handler_device(struct usb_ctrlrequest* req)
641 int i;
643 switch(req->bRequest) {
644 case USB_REQ_GET_CONFIGURATION: {
645 logf("usb_core: GET_CONFIG");
646 response_data[0] = (usb_state == ADDRESS ? 0 : 1);
647 usb_drv_recv(EP_CONTROL, NULL, 0);
648 usb_drv_send(EP_CONTROL, response_data, 1);
649 break;
651 case USB_REQ_SET_CONFIGURATION: {
652 logf("usb_core: SET_CONFIG");
653 usb_drv_cancel_all_transfers();
654 if(req->wValue) {
655 usb_state = CONFIGURED;
656 for(i = 0; i < USB_NUM_DRIVERS; i++)
657 if(drivers[i].enabled && drivers[i].init_connection)
658 drivers[i].init_connection();
660 else
661 usb_state = ADDRESS;
662 usb_drv_send(EP_CONTROL, NULL, 0);
663 #ifdef HAVE_USB_CHARGING_ENABLE
664 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
665 #endif
666 break;
668 case USB_REQ_SET_ADDRESS: {
669 unsigned char address = req->wValue;
670 logf("usb_core: SET_ADR %d", address);
671 usb_drv_send(EP_CONTROL, NULL, 0);
672 usb_drv_cancel_all_transfers();
673 usb_address = address;
674 usb_drv_set_address(usb_address);
675 usb_state = ADDRESS;
676 break;
678 case USB_REQ_GET_DESCRIPTOR:
679 logf("usb_core: GET_DESC %d", req->wValue >> 8);
680 request_handler_device_get_descriptor(req);
681 break;
682 case USB_REQ_CLEAR_FEATURE:
683 break;
684 case USB_REQ_SET_FEATURE:
685 if(req->wValue==USB_DEVICE_TEST_MODE) {
686 int mode = req->wIndex >> 8;
687 usb_drv_send(EP_CONTROL, NULL, 0);
688 usb_drv_set_test_mode(mode);
690 break;
691 case USB_REQ_GET_STATUS:
692 response_data[0] = 0;
693 response_data[1] = 0;
694 usb_drv_recv(EP_CONTROL, NULL, 0);
695 usb_drv_send(EP_CONTROL, response_data, 2);
696 break;
697 default:
698 logf("bad req:desc %d:%d", req->bRequest, req->wValue);
699 usb_drv_stall(EP_CONTROL, true, true);
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 default:
746 logf("bad req:desc %d", req->bRequest);
747 usb_drv_stall(EP_CONTROL, true, true);
748 break;
752 static void request_handler_endoint_drivers(struct usb_ctrlrequest* req)
754 bool handled = false;
755 control_handler_t control_handler = NULL;
757 if(EP_NUM(req->wIndex) < USB_NUM_ENDPOINTS)
758 control_handler =
759 ep_data[EP_NUM(req->wIndex)].control_handler[EP_DIR(req->wIndex)];
761 if(control_handler)
762 handled = control_handler(req, response_data);
764 if(!handled) {
765 /* nope. flag error */
766 logf("usb bad req %d", req->bRequest);
767 usb_drv_stall(EP_CONTROL, true, true);
771 static void request_handler_endpoint_standard(struct usb_ctrlrequest* req)
773 switch (req->bRequest) {
774 case USB_REQ_CLEAR_FEATURE:
775 if(req->wValue == USB_ENDPOINT_HALT)
776 usb_drv_stall(EP_NUM(req->wIndex), false, EP_DIR(req->wIndex));
778 usb_drv_send(EP_CONTROL, NULL, 0);
779 break;
780 case USB_REQ_SET_FEATURE:
781 if(req->wValue == USB_ENDPOINT_HALT)
782 usb_drv_stall(EP_NUM(req->wIndex), true, EP_DIR(req->wIndex));
784 usb_drv_send(EP_CONTROL, NULL, 0);
785 break;
786 case USB_REQ_GET_STATUS:
787 response_data[0] = 0;
788 response_data[1] = 0;
789 logf("usb_core: GET_STATUS");
790 if(req->wIndex > 0)
791 response_data[0] = usb_drv_stalled(EP_NUM(req->wIndex),
792 EP_DIR(req->wIndex));
794 usb_drv_recv(EP_CONTROL, NULL, 0);
795 usb_drv_send(EP_CONTROL, response_data, 2);
796 break;
797 default:
798 request_handler_endoint_drivers(req);
799 break;
803 static void request_handler_endpoint(struct usb_ctrlrequest* req)
805 switch(req->bRequestType & USB_TYPE_MASK) {
806 case USB_TYPE_STANDARD:
807 request_handler_endpoint_standard(req);
808 break;
809 case USB_TYPE_CLASS:
810 request_handler_endoint_drivers(req);
811 break;
812 case USB_TYPE_VENDOR:
813 default:
814 logf("bad req:desc %d", req->bRequest);
815 usb_drv_stall(EP_CONTROL, true, true);
816 break;
820 /* Handling USB requests starts here */
821 static void usb_core_control_request_handler(struct usb_ctrlrequest* req)
823 #ifdef HAVE_USB_CHARGING_ENABLE
824 timeout_cancel(&usb_no_host_timeout);
825 if(usb_no_host) {
826 usb_no_host = false;
827 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
829 #endif
830 if(usb_state == DEFAULT) {
831 set_serial_descriptor();
832 usb_core_set_serial_function_id();
834 allocate_interfaces_and_endpoints();
837 switch(req->bRequestType & USB_RECIP_MASK) {
838 case USB_RECIP_DEVICE:
839 request_handler_device(req);
840 break;
841 case USB_RECIP_INTERFACE:
842 request_handler_interface(req);
843 break;
844 case USB_RECIP_ENDPOINT:
845 request_handler_endpoint(req);
846 break;
847 case USB_RECIP_OTHER:
848 logf("unsupported recipient");
849 usb_drv_stall(EP_CONTROL, true, true);
850 break;
852 //logf("control handled");
855 /* called by usb_drv_int() */
856 void usb_core_bus_reset(void)
858 usb_address = 0;
859 usb_state = DEFAULT;
860 #ifdef HAVE_USB_CHARGING_ENABLE
861 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
862 #endif
865 /* called by usb_drv_transfer_completed() */
866 void usb_core_transfer_complete(int endpoint, int dir, int status, int length)
868 struct usb_transfer_completion_event_data *completion_event;
870 switch (endpoint) {
871 case EP_CONTROL:
872 /* already handled */
873 break;
875 default:
876 completion_event = &ep_data[endpoint].completion_event[EP_DIR(dir)];
878 completion_event->endpoint = endpoint;
879 completion_event->dir = dir;
880 completion_event->data = 0;
881 completion_event->status = status;
882 completion_event->length = length;
883 /* All other endpoints. Let the thread deal with it */
884 usb_signal_transfer_completion(completion_event);
885 break;
889 /* called by usb_drv_int() */
890 void usb_core_control_request(struct usb_ctrlrequest* req)
892 struct usb_transfer_completion_event_data* completion_event =
893 &ep_data[EP_CONTROL].completion_event[EP_DIR(USB_DIR_IN)];
895 completion_event->endpoint = EP_CONTROL;
896 completion_event->dir = 0;
897 completion_event->data = (void*)req;
898 completion_event->status = 0;
899 completion_event->length = 0;
900 logf("ctrl received %ld", current_tick);
901 usb_signal_transfer_completion(completion_event);
904 #ifdef HAVE_USB_CHARGING_ENABLE
905 void usb_charging_enable(int state)
907 usb_charging_mode = state;
908 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
911 int usb_charging_maxcurrent()
913 if (!initialized || usb_charging_mode == USB_CHARGING_DISABLE)
914 return 100;
915 if (usb_state == CONFIGURED)
916 return usb_charging_current_requested;
917 if (usb_charging_mode == USB_CHARGING_FORCE && usb_no_host)
918 return 500;
919 return 100;
921 #endif