2007-11-22 20:51:00 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2008-04-18 16:42:50 +00:00
|
|
|
* $Id$
|
2007-11-22 20:51:00 +00:00
|
|
|
*
|
2009-03-23 17:08:46 +00:00
|
|
|
* Copyright (C) 2007 by Björn Stenberg
|
2007-11-22 20:51:00 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-11-22 20:51:00 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "system.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "string.h"
|
2021-09-20 00:41:26 +00:00
|
|
|
#include "panic.h"
|
2009-11-03 16:25:03 +00:00
|
|
|
/*#define LOGF_ENABLE*/
|
2007-11-22 20:51:00 +00:00
|
|
|
#include "logf.h"
|
|
|
|
|
2008-02-25 23:43:11 +00:00
|
|
|
#include "usb.h"
|
2007-11-22 20:51:00 +00:00
|
|
|
#include "usb_ch9.h"
|
|
|
|
#include "usb_drv.h"
|
|
|
|
#include "usb_core.h"
|
2008-03-06 21:25:09 +00:00
|
|
|
#include "usb_class_driver.h"
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2009-05-23 14:30:20 +00:00
|
|
|
#if defined(USB_ENABLE_STORAGE)
|
2007-11-22 20:51:00 +00:00
|
|
|
#include "usb_storage.h"
|
2008-02-20 22:54:26 +00:00
|
|
|
#endif
|
|
|
|
|
2009-05-23 14:30:20 +00:00
|
|
|
#if defined(USB_ENABLE_SERIAL)
|
2007-11-30 00:13:00 +00:00
|
|
|
#include "usb_serial.h"
|
2008-02-20 22:54:26 +00:00
|
|
|
#endif
|
|
|
|
|
2009-05-23 14:30:20 +00:00
|
|
|
#if defined(USB_ENABLE_CHARGING_ONLY)
|
2008-04-26 19:02:16 +00:00
|
|
|
#include "usb_charging_only.h"
|
|
|
|
#endif
|
|
|
|
|
2009-09-30 10:06:04 +00:00
|
|
|
#ifdef USB_ENABLE_HID
|
2009-04-19 21:17:18 +00:00
|
|
|
#include "usb_hid.h"
|
|
|
|
#endif
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
/* TODO: Move target-specific stuff somewhere else (serial number reading) */
|
2008-02-20 22:54:26 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_AS3514
|
2008-10-31 00:16:42 +00:00
|
|
|
#include "ascodec.h"
|
2008-02-20 22:54:26 +00:00
|
|
|
#include "as3514.h"
|
|
|
|
#endif
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
#if !defined(HAVE_AS3514) && !defined(IPOD_ARCH) && (CONFIG_STORAGE & STORAGE_ATA)
|
2008-02-22 23:14:24 +00:00
|
|
|
#include "ata.h"
|
|
|
|
#endif
|
|
|
|
|
2012-05-31 11:55:35 +00:00
|
|
|
#if (CONFIG_CPU == IMX233)
|
|
|
|
#include "ocotp-imx233.h"
|
|
|
|
#endif
|
|
|
|
|
2021-07-10 06:56:32 +00:00
|
|
|
#ifdef SANSA_CONNECT
|
|
|
|
#include "cryptomem-sansaconnect.h"
|
|
|
|
#endif
|
|
|
|
|
2009-01-13 16:27:35 +00:00
|
|
|
#ifndef USB_MAX_CURRENT
|
|
|
|
#define USB_MAX_CURRENT 500
|
|
|
|
#endif
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
/* USB protocol descriptors: */
|
|
|
|
|
2009-04-20 20:24:23 +00:00
|
|
|
static struct usb_device_descriptor __attribute__((aligned(2)))
|
2008-03-06 21:25:09 +00:00
|
|
|
device_descriptor=
|
|
|
|
{
|
2007-11-22 20:51:00 +00:00
|
|
|
.bLength = sizeof(struct usb_device_descriptor),
|
|
|
|
.bDescriptorType = USB_DT_DEVICE,
|
2008-11-30 15:53:36 +00:00
|
|
|
#ifndef USB_NO_HIGH_SPEED
|
2008-02-20 22:54:26 +00:00
|
|
|
.bcdUSB = 0x0200,
|
2008-02-24 13:45:36 +00:00
|
|
|
#else
|
|
|
|
.bcdUSB = 0x0110,
|
|
|
|
#endif
|
2007-11-22 20:51:00 +00:00
|
|
|
.bDeviceClass = USB_CLASS_PER_INTERFACE,
|
|
|
|
.bDeviceSubClass = 0,
|
|
|
|
.bDeviceProtocol = 0,
|
|
|
|
.bMaxPacketSize0 = 64,
|
|
|
|
.idVendor = USB_VENDOR_ID,
|
|
|
|
.idProduct = USB_PRODUCT_ID,
|
|
|
|
.bcdDevice = 0x0100,
|
2021-10-06 21:40:54 +00:00
|
|
|
.iManufacturer = USB_STRING_INDEX_MANUFACTURER,
|
|
|
|
.iProduct = USB_STRING_INDEX_PRODUCT,
|
|
|
|
.iSerialNumber = USB_STRING_INDEX_SERIAL,
|
2007-11-22 20:51:00 +00:00
|
|
|
.bNumConfigurations = 1
|
2008-02-29 18:33:54 +00:00
|
|
|
} ;
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
static struct usb_config_descriptor __attribute__((aligned(2)))
|
2009-04-18 21:32:41 +00:00
|
|
|
config_descriptor =
|
2008-02-11 14:26:25 +00:00
|
|
|
{
|
2008-02-22 20:38:31 +00:00
|
|
|
.bLength = sizeof(struct usb_config_descriptor),
|
|
|
|
.bDescriptorType = USB_DT_CONFIG,
|
|
|
|
.wTotalLength = 0, /* will be filled in later */
|
|
|
|
.bNumInterfaces = 1,
|
|
|
|
.bConfigurationValue = 1,
|
|
|
|
.iConfiguration = 0,
|
|
|
|
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
|
2010-04-13 08:40:21 +00:00
|
|
|
.bMaxPower = (USB_MAX_CURRENT + 1) / 2, /* In 2mA units */
|
2008-02-22 20:38:31 +00:00
|
|
|
};
|
2008-02-11 14:26:25 +00:00
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
static const struct usb_qualifier_descriptor __attribute__((aligned(2)))
|
|
|
|
qualifier_descriptor =
|
2007-11-22 20:51:00 +00:00
|
|
|
{
|
|
|
|
.bLength = sizeof(struct usb_qualifier_descriptor),
|
|
|
|
.bDescriptorType = USB_DT_DEVICE_QUALIFIER,
|
|
|
|
.bcdUSB = 0x0200,
|
|
|
|
.bDeviceClass = 0,
|
|
|
|
.bDeviceSubClass = 0,
|
|
|
|
.bDeviceProtocol = 0,
|
|
|
|
.bMaxPacketSize0 = 64,
|
|
|
|
.bNumConfigurations = 1
|
|
|
|
};
|
|
|
|
|
2008-04-16 15:48:54 +00:00
|
|
|
static const struct usb_string_descriptor __attribute__((aligned(2)))
|
2008-03-06 21:25:09 +00:00
|
|
|
usb_string_iManufacturer =
|
2021-03-25 18:10:50 +00:00
|
|
|
USB_STRING_INITIALIZER(u"Rockbox.org");
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-04-16 15:48:54 +00:00
|
|
|
static const struct usb_string_descriptor __attribute__((aligned(2)))
|
2008-03-06 21:25:09 +00:00
|
|
|
usb_string_iProduct =
|
2021-03-25 18:10:50 +00:00
|
|
|
USB_STRING_INITIALIZER(u"Rockbox media player");
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
static struct usb_string_descriptor __attribute__((aligned(2)))
|
|
|
|
usb_string_iSerial =
|
2021-03-25 18:10:50 +00:00
|
|
|
USB_STRING_INITIALIZER(u"00000000000000000000000000000000000000000");
|
2008-02-11 14:26:25 +00:00
|
|
|
|
|
|
|
/* Generic for all targets */
|
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
/* this is stringid #0: languages supported */
|
2008-04-16 15:48:54 +00:00
|
|
|
static const struct usb_string_descriptor __attribute__((aligned(2)))
|
2008-03-06 21:25:09 +00:00
|
|
|
lang_descriptor =
|
2021-03-25 18:10:50 +00:00
|
|
|
USB_STRING_INITIALIZER(u"\x0409"); /* LANGID US English */
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2021-10-06 21:40:54 +00:00
|
|
|
static const struct usb_string_descriptor* const usb_strings[USB_STRING_INDEX_MAX] =
|
2008-02-11 14:26:25 +00:00
|
|
|
{
|
2021-10-06 21:40:54 +00:00
|
|
|
[USB_STRING_INDEX_LANGUAGE] = &lang_descriptor,
|
|
|
|
[USB_STRING_INDEX_MANUFACTURER] = &usb_string_iManufacturer,
|
|
|
|
[USB_STRING_INDEX_PRODUCT] = &usb_string_iProduct,
|
|
|
|
[USB_STRING_INDEX_SERIAL] = &usb_string_iSerial,
|
2008-02-11 14:26:25 +00:00
|
|
|
};
|
2007-11-22 20:51:00 +00:00
|
|
|
|
|
|
|
static int usb_address = 0;
|
|
|
|
static bool initialized = false;
|
2014-01-05 21:54:10 +00:00
|
|
|
static bool drivers_connected = false;
|
2007-11-24 22:37:41 +00:00
|
|
|
static enum { DEFAULT, ADDRESS, CONFIGURED } usb_state;
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2010-06-05 10:05:27 +00:00
|
|
|
#ifdef HAVE_USB_CHARGING_ENABLE
|
|
|
|
static int usb_charging_mode = USB_CHARGING_DISABLE;
|
|
|
|
static int usb_charging_current_requested = 500;
|
2010-06-05 20:43:30 +00:00
|
|
|
static struct timeout usb_no_host_timeout;
|
|
|
|
static bool usb_no_host = false;
|
|
|
|
|
|
|
|
static int usb_no_host_callback(struct timeout *tmo)
|
|
|
|
{
|
|
|
|
(void)tmo;
|
|
|
|
usb_no_host = true;
|
|
|
|
usb_charger_update();
|
|
|
|
return 0;
|
|
|
|
}
|
2010-06-05 10:05:27 +00:00
|
|
|
#endif
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
static int usb_core_num_interfaces;
|
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
typedef void (*completion_handler_t)(int ep, int dir, int status, int length);
|
2021-09-20 00:05:39 +00:00
|
|
|
typedef bool (*control_handler_t)(struct usb_ctrlrequest* req, void* reqdata,
|
2010-04-13 10:07:02 +00:00
|
|
|
unsigned char* dest);
|
2008-10-03 22:43:16 +00:00
|
|
|
|
2008-04-26 19:02:16 +00:00
|
|
|
static struct
|
|
|
|
{
|
2008-10-03 22:43:16 +00:00
|
|
|
completion_handler_t completion_handler[2];
|
|
|
|
control_handler_t control_handler[2];
|
2009-11-15 21:58:25 +00:00
|
|
|
struct usb_transfer_completion_event_data completion_event[2];
|
2009-01-19 13:41:25 +00:00
|
|
|
} ep_data[USB_NUM_ENDPOINTS];
|
2008-03-06 21:25:09 +00:00
|
|
|
|
2008-03-10 20:28:59 +00:00
|
|
|
static struct usb_class_driver drivers[USB_NUM_DRIVERS] =
|
2008-03-06 21:25:09 +00:00
|
|
|
{
|
2009-05-23 14:30:20 +00:00
|
|
|
#ifdef USB_ENABLE_STORAGE
|
2008-03-06 21:25:09 +00:00
|
|
|
[USB_DRIVER_MASS_STORAGE] = {
|
|
|
|
.enabled = false,
|
2009-01-13 16:27:35 +00:00
|
|
|
.needs_exclusive_storage = true,
|
2008-04-26 19:02:16 +00:00
|
|
|
.first_interface = 0,
|
|
|
|
.last_interface = 0,
|
2008-10-03 22:43:16 +00:00
|
|
|
.request_endpoints = usb_storage_request_endpoints,
|
2008-04-26 19:02:16 +00:00
|
|
|
.set_first_interface = usb_storage_set_first_interface,
|
2008-03-06 21:25:09 +00:00
|
|
|
.get_config_descriptor = usb_storage_get_config_descriptor,
|
|
|
|
.init_connection = usb_storage_init_connection,
|
|
|
|
.init = usb_storage_init,
|
2008-12-13 14:49:44 +00:00
|
|
|
.disconnect = usb_storage_disconnect,
|
2008-03-06 21:25:09 +00:00
|
|
|
.transfer_complete = usb_storage_transfer_complete,
|
2008-03-10 20:55:24 +00:00
|
|
|
.control_request = usb_storage_control_request,
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
.notify_hotswap = usb_storage_notify_hotswap,
|
|
|
|
#endif
|
2008-03-06 21:25:09 +00:00
|
|
|
},
|
|
|
|
#endif
|
2009-05-23 14:30:20 +00:00
|
|
|
#ifdef USB_ENABLE_SERIAL
|
2008-03-06 21:25:09 +00:00
|
|
|
[USB_DRIVER_SERIAL] = {
|
|
|
|
.enabled = false,
|
2009-01-13 16:27:35 +00:00
|
|
|
.needs_exclusive_storage = false,
|
2008-04-26 19:02:16 +00:00
|
|
|
.first_interface = 0,
|
|
|
|
.last_interface = 0,
|
2008-10-03 22:43:16 +00:00
|
|
|
.request_endpoints = usb_serial_request_endpoints,
|
2008-04-26 19:02:16 +00:00
|
|
|
.set_first_interface = usb_serial_set_first_interface,
|
2008-03-06 21:25:09 +00:00
|
|
|
.get_config_descriptor = usb_serial_get_config_descriptor,
|
|
|
|
.init_connection = usb_serial_init_connection,
|
|
|
|
.init = usb_serial_init,
|
|
|
|
.disconnect = usb_serial_disconnect,
|
|
|
|
.transfer_complete = usb_serial_transfer_complete,
|
2008-03-10 20:55:24 +00:00
|
|
|
.control_request = usb_serial_control_request,
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
.notify_hotswap = NULL,
|
|
|
|
#endif
|
2008-03-06 21:25:09 +00:00
|
|
|
},
|
2008-03-02 21:00:12 +00:00
|
|
|
#endif
|
2009-05-23 14:30:20 +00:00
|
|
|
#ifdef USB_ENABLE_CHARGING_ONLY
|
2008-03-06 21:25:09 +00:00
|
|
|
[USB_DRIVER_CHARGING_ONLY] = {
|
|
|
|
.enabled = false,
|
2009-01-13 16:27:35 +00:00
|
|
|
.needs_exclusive_storage = false,
|
2008-04-26 19:02:16 +00:00
|
|
|
.first_interface = 0,
|
|
|
|
.last_interface = 0,
|
2008-10-03 22:43:16 +00:00
|
|
|
.request_endpoints = usb_charging_only_request_endpoints,
|
2008-04-26 19:02:16 +00:00
|
|
|
.set_first_interface = usb_charging_only_set_first_interface,
|
|
|
|
.get_config_descriptor = usb_charging_only_get_config_descriptor,
|
2008-03-06 21:25:09 +00:00
|
|
|
.init_connection = NULL,
|
|
|
|
.init = NULL,
|
|
|
|
.disconnect = NULL,
|
|
|
|
.transfer_complete = NULL,
|
2008-03-10 20:55:24 +00:00
|
|
|
.control_request = NULL,
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
.notify_hotswap = NULL,
|
|
|
|
#endif
|
2008-03-06 21:25:09 +00:00
|
|
|
},
|
2009-05-23 12:41:46 +00:00
|
|
|
#endif
|
2009-05-23 14:30:20 +00:00
|
|
|
#ifdef USB_ENABLE_HID
|
2009-04-19 21:17:18 +00:00
|
|
|
[USB_DRIVER_HID] = {
|
|
|
|
.enabled = false,
|
|
|
|
.needs_exclusive_storage = false,
|
|
|
|
.first_interface = 0,
|
|
|
|
.last_interface = 0,
|
|
|
|
.request_endpoints = usb_hid_request_endpoints,
|
|
|
|
.set_first_interface = usb_hid_set_first_interface,
|
|
|
|
.get_config_descriptor = usb_hid_get_config_descriptor,
|
|
|
|
.init_connection = usb_hid_init_connection,
|
|
|
|
.init = usb_hid_init,
|
|
|
|
.disconnect = usb_hid_disconnect,
|
|
|
|
.transfer_complete = usb_hid_transfer_complete,
|
|
|
|
.control_request = usb_hid_control_request,
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
.notify_hotswap = NULL,
|
|
|
|
#endif
|
|
|
|
},
|
|
|
|
#endif
|
2008-03-06 21:25:09 +00:00
|
|
|
};
|
2008-02-25 23:43:11 +00:00
|
|
|
|
2021-09-20 00:41:26 +00:00
|
|
|
#ifdef USB_LEGACY_CONTROL_API
|
2021-11-08 21:12:24 +00:00
|
|
|
static struct usb_ctrlrequest* volatile active_request = NULL;
|
|
|
|
static void* volatile control_write_data = NULL;
|
|
|
|
static volatile bool control_write_data_done = false;
|
2021-09-20 00:41:26 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-20 00:05:39 +00:00
|
|
|
static void usb_core_control_request_handler(struct usb_ctrlrequest* req, void* reqdata);
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2009-01-19 13:41:25 +00:00
|
|
|
static unsigned char response_data[256] USB_DEVBSS_ATTR;
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2012-05-31 11:55:35 +00:00
|
|
|
/** NOTE Serial Number
|
|
|
|
* The serial number string is split into two parts:
|
|
|
|
* - the first character indicates the set of interfaces enabled
|
|
|
|
* - the other characters form a (hopefully) unique device-specific number
|
|
|
|
* The implementation of set_serial_descriptor should left the first character
|
|
|
|
* of usb_string_iSerial unused, ie never write to
|
|
|
|
* usb_string_iSerial.wString[0] but should take it into account when
|
|
|
|
* computing the length of the descriptor
|
|
|
|
*/
|
|
|
|
|
2012-05-07 05:35:57 +00:00
|
|
|
static const short hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
|
2010-04-13 08:40:21 +00:00
|
|
|
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
2008-02-11 14:26:25 +00:00
|
|
|
#ifdef IPOD_ARCH
|
2008-02-20 22:54:26 +00:00
|
|
|
static void set_serial_descriptor(void)
|
2008-02-11 14:26:25 +00:00
|
|
|
{
|
|
|
|
#ifdef IPOD_VIDEO
|
2010-04-13 08:40:21 +00:00
|
|
|
uint32_t* serial = (uint32_t*)0x20004034;
|
2008-02-11 14:26:25 +00:00
|
|
|
#else
|
2010-04-13 08:40:21 +00:00
|
|
|
uint32_t* serial = (uint32_t*)0x20002034;
|
2008-02-11 14:26:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We need to convert from a little-endian 64-bit int
|
|
|
|
into a utf-16 string of hex characters */
|
2008-03-04 12:08:00 +00:00
|
|
|
short* p = &usb_string_iSerial.wString[24];
|
2008-02-11 14:26:25 +00:00
|
|
|
uint32_t x;
|
2010-04-13 08:40:21 +00:00
|
|
|
int i, j;
|
2008-02-11 14:26:25 +00:00
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
for(i = 0; i < 2; i++) {
|
2008-02-11 14:26:25 +00:00
|
|
|
x = serial[i];
|
2010-04-13 08:40:21 +00:00
|
|
|
for(j = 0; j < 8; j++) {
|
2008-02-11 14:26:25 +00:00
|
|
|
*p-- = hex[x & 0xf];
|
|
|
|
x >>= 4;
|
|
|
|
}
|
|
|
|
}
|
2010-04-13 08:40:21 +00:00
|
|
|
usb_string_iSerial.bLength = 52;
|
2008-02-20 22:54:26 +00:00
|
|
|
}
|
|
|
|
#elif defined(HAVE_AS3514)
|
|
|
|
static void set_serial_descriptor(void)
|
|
|
|
{
|
2010-09-06 18:26:17 +00:00
|
|
|
unsigned char serial[AS3514_UID_LEN];
|
2008-02-22 23:14:24 +00:00
|
|
|
/* Align 32 digits right in the 40-digit serial number */
|
2008-03-04 12:08:00 +00:00
|
|
|
short* p = &usb_string_iSerial.wString[1];
|
2008-02-20 22:54:26 +00:00
|
|
|
int i;
|
|
|
|
|
2010-09-06 18:26:17 +00:00
|
|
|
ascodec_readbytes(AS3514_UID_0, AS3514_UID_LEN, serial);
|
|
|
|
for(i = 0; i < AS3514_UID_LEN; i++) {
|
2008-02-20 22:54:26 +00:00
|
|
|
*p++ = hex[(serial[i] >> 4) & 0xF];
|
|
|
|
*p++ = hex[(serial[i] >> 0) & 0xF];
|
|
|
|
}
|
2010-09-06 18:26:17 +00:00
|
|
|
usb_string_iSerial.bLength = 36 + (2 * AS3514_UID_LEN);
|
2008-02-22 23:14:24 +00:00
|
|
|
}
|
2013-06-23 16:59:13 +00:00
|
|
|
#elif (CONFIG_CPU == IMX233) && IMX233_SUBTARGET >= 3700
|
|
|
|
// FIXME where is the STMP3600 serial number stored ?
|
2012-05-31 11:55:35 +00:00
|
|
|
static void set_serial_descriptor(void)
|
|
|
|
{
|
|
|
|
short* p = &usb_string_iSerial.wString[1];
|
|
|
|
for(int i = 0; i < IMX233_NUM_OCOTP_OPS; i++) {
|
2013-06-16 14:46:12 +00:00
|
|
|
uint32_t ops = imx233_ocotp_read(&HW_OCOTP_OPSn(i));
|
2012-05-31 11:55:35 +00:00
|
|
|
for(int j = 0; j < 8; j++) {
|
|
|
|
*p++ = hex[(ops >> 28) & 0xF];
|
|
|
|
ops <<= 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usb_string_iSerial.bLength = 2 + 2 * (1 + IMX233_NUM_OCOTP_OPS * 8);
|
|
|
|
}
|
2021-07-10 06:56:32 +00:00
|
|
|
#elif defined(SANSA_CONNECT)
|
|
|
|
static void set_serial_descriptor(void)
|
|
|
|
{
|
|
|
|
char deviceid[32];
|
|
|
|
short* p = &usb_string_iSerial.wString[1];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if(!cryptomem_read_deviceid(deviceid)) {
|
|
|
|
for(i = 0; i < 32; i++) {
|
|
|
|
*p++ = deviceid[i];
|
|
|
|
}
|
|
|
|
usb_string_iSerial.bLength = 2 + 2 * (1 + 32);
|
|
|
|
} else {
|
|
|
|
device_descriptor.iSerialNumber = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-11-01 16:14:28 +00:00
|
|
|
#elif (CONFIG_STORAGE & STORAGE_ATA)
|
2008-02-22 23:14:24 +00:00
|
|
|
/* If we don't know the device serial number, use the one
|
|
|
|
* from the disk */
|
|
|
|
static void set_serial_descriptor(void)
|
|
|
|
{
|
2008-03-04 12:08:00 +00:00
|
|
|
short* p = &usb_string_iSerial.wString[1];
|
2008-02-22 23:14:24 +00:00
|
|
|
unsigned short* identify = ata_get_identify();
|
|
|
|
unsigned short x;
|
|
|
|
int i;
|
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
for(i = 10; i < 20; i++) {
|
2008-02-23 08:22:12 +00:00
|
|
|
x = identify[i];
|
2008-02-22 23:14:24 +00:00
|
|
|
*p++ = hex[(x >> 12) & 0xF];
|
|
|
|
*p++ = hex[(x >> 8) & 0xF];
|
2008-02-23 08:22:12 +00:00
|
|
|
*p++ = hex[(x >> 4) & 0xF];
|
|
|
|
*p++ = hex[(x >> 0) & 0xF];
|
2008-02-22 23:14:24 +00:00
|
|
|
}
|
2010-04-13 08:40:21 +00:00
|
|
|
usb_string_iSerial.bLength = 84;
|
2008-02-11 14:26:25 +00:00
|
|
|
}
|
2008-11-03 20:52:27 +00:00
|
|
|
#elif (CONFIG_STORAGE & STORAGE_RAMDISK)
|
|
|
|
/* This "serial number" isn't unique, but it should never actually
|
|
|
|
appear in non-testing use */
|
|
|
|
static void set_serial_descriptor(void)
|
|
|
|
{
|
|
|
|
short* p = &usb_string_iSerial.wString[1];
|
|
|
|
int i;
|
2010-04-13 08:40:21 +00:00
|
|
|
for(i = 0; i < 16; i++) {
|
|
|
|
*p++ = hex[(2 * i) & 0xF];
|
|
|
|
*p++ = hex[(2 * i + 1) & 0xF];
|
2008-11-03 20:52:27 +00:00
|
|
|
}
|
2010-04-13 08:40:21 +00:00
|
|
|
usb_string_iSerial.bLength = 68;
|
2008-11-03 20:52:27 +00:00
|
|
|
}
|
2008-11-01 16:14:28 +00:00
|
|
|
#else
|
2008-11-30 15:43:15 +00:00
|
|
|
static void set_serial_descriptor(void)
|
|
|
|
{
|
2009-04-20 20:24:23 +00:00
|
|
|
device_descriptor.iSerialNumber = 0;
|
2008-11-30 15:43:15 +00:00
|
|
|
}
|
2008-02-11 14:26:25 +00:00
|
|
|
#endif
|
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
void usb_core_init(void)
|
|
|
|
{
|
2008-03-06 21:25:09 +00:00
|
|
|
int i;
|
2007-11-23 15:02:26 +00:00
|
|
|
if (initialized)
|
|
|
|
return;
|
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
usb_drv_init();
|
2008-02-27 19:08:30 +00:00
|
|
|
|
|
|
|
/* class driver init functions should be safe to call even if the driver
|
|
|
|
* won't be used. This simplifies other logic (i.e. we don't need to know
|
|
|
|
* yet which drivers will be enabled */
|
2010-04-13 08:40:21 +00:00
|
|
|
for(i = 0; i < USB_NUM_DRIVERS; i++)
|
2009-05-24 20:46:57 +00:00
|
|
|
if(drivers[i].init != NULL)
|
2008-03-06 21:25:09 +00:00
|
|
|
drivers[i].init();
|
2007-11-30 00:13:00 +00:00
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
initialized = true;
|
2007-11-24 22:37:41 +00:00
|
|
|
usb_state = DEFAULT;
|
2010-06-05 20:43:30 +00:00
|
|
|
#ifdef HAVE_USB_CHARGING_ENABLE
|
|
|
|
usb_no_host = false;
|
|
|
|
timeout_register(&usb_no_host_timeout, usb_no_host_callback, HZ*10, 0);
|
|
|
|
#endif
|
2007-11-22 20:51:00 +00:00
|
|
|
logf("usb_core_init() finished");
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_core_exit(void)
|
|
|
|
{
|
2008-03-06 21:25:09 +00:00
|
|
|
int i;
|
2014-01-05 21:54:10 +00:00
|
|
|
if(drivers_connected)
|
|
|
|
{
|
|
|
|
for(i = 0; i < USB_NUM_DRIVERS; i++)
|
|
|
|
if(drivers[i].enabled && drivers[i].disconnect != NULL)
|
|
|
|
{
|
|
|
|
drivers[i].disconnect();
|
|
|
|
drivers[i].enabled = false;
|
|
|
|
}
|
|
|
|
drivers_connected = false;
|
|
|
|
}
|
2008-03-02 20:45:33 +00:00
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
if(initialized) {
|
2007-11-22 20:51:00 +00:00
|
|
|
usb_drv_exit();
|
2009-04-18 21:32:41 +00:00
|
|
|
initialized = false;
|
2007-11-22 20:51:00 +00:00
|
|
|
}
|
2009-01-13 16:27:35 +00:00
|
|
|
usb_state = DEFAULT;
|
2010-06-05 10:05:27 +00:00
|
|
|
#ifdef HAVE_USB_CHARGING_ENABLE
|
2010-06-05 20:43:30 +00:00
|
|
|
usb_no_host = false;
|
2010-06-05 10:05:27 +00:00
|
|
|
usb_charging_maxcurrent_change(usb_charging_maxcurrent());
|
|
|
|
#endif
|
2007-11-22 20:51:00 +00:00
|
|
|
logf("usb_core_exit() finished");
|
|
|
|
}
|
|
|
|
|
2010-04-13 10:07:02 +00:00
|
|
|
void usb_core_handle_transfer_completion(
|
|
|
|
struct usb_transfer_completion_event_data* event)
|
2007-11-22 20:51:00 +00:00
|
|
|
{
|
2009-04-18 21:32:41 +00:00
|
|
|
completion_handler_t handler;
|
2008-04-26 19:02:16 +00:00
|
|
|
int ep = event->endpoint;
|
2008-10-03 22:43:16 +00:00
|
|
|
|
2008-04-26 19:02:16 +00:00
|
|
|
switch(ep) {
|
2008-02-27 19:08:30 +00:00
|
|
|
case EP_CONTROL:
|
2013-07-24 20:40:12 +00:00
|
|
|
logf("ctrl handled %ld req=0x%x",
|
|
|
|
current_tick,
|
2021-09-19 23:58:33 +00:00
|
|
|
((struct usb_ctrlrequest*)event->data[0])->bRequest);
|
2013-07-24 20:40:12 +00:00
|
|
|
|
2010-04-13 10:07:02 +00:00
|
|
|
usb_core_control_request_handler(
|
2021-09-20 00:05:39 +00:00
|
|
|
(struct usb_ctrlrequest*)event->data[0], event->data[1]);
|
2008-02-27 19:08:30 +00:00
|
|
|
break;
|
2008-03-06 21:25:09 +00:00
|
|
|
default:
|
2009-05-16 15:30:09 +00:00
|
|
|
handler = ep_data[ep].completion_handler[EP_DIR(event->dir)];
|
2009-04-18 21:32:41 +00:00
|
|
|
if(handler != NULL)
|
2010-04-13 08:40:21 +00:00
|
|
|
handler(ep, event->dir, event->status, event->length);
|
2008-02-27 19:08:30 +00:00
|
|
|
break;
|
2007-11-22 20:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
void usb_core_enable_driver(int driver, bool enabled)
|
2007-11-22 20:51:00 +00:00
|
|
|
{
|
2008-03-06 21:25:09 +00:00
|
|
|
drivers[driver].enabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool usb_core_driver_enabled(int driver)
|
|
|
|
{
|
|
|
|
return drivers[driver].enabled;
|
|
|
|
}
|
|
|
|
|
2009-01-13 16:27:35 +00:00
|
|
|
bool usb_core_any_exclusive_storage(void)
|
|
|
|
{
|
|
|
|
int i;
|
2010-04-13 08:40:21 +00:00
|
|
|
for(i = 0; i < USB_NUM_DRIVERS; i++)
|
2009-04-18 21:32:41 +00:00
|
|
|
if(drivers[i].enabled && drivers[i].needs_exclusive_storage)
|
2009-01-13 16:27:35 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-10 20:55:24 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2010-04-13 08:40:21 +00:00
|
|
|
void usb_core_hotswap_event(int volume, bool inserted)
|
2008-03-10 20:55:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
2010-04-13 08:40:21 +00:00
|
|
|
for(i = 0; i < USB_NUM_DRIVERS; i++)
|
|
|
|
if(drivers[i].enabled && drivers[i].notify_hotswap != NULL)
|
|
|
|
drivers[i].notify_hotswap(volume, inserted);
|
2008-03-10 20:55:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
static void usb_core_set_serial_function_id(void)
|
|
|
|
{
|
2010-04-13 08:40:21 +00:00
|
|
|
int i, id = 0;
|
|
|
|
|
|
|
|
for(i = 0; i < USB_NUM_DRIVERS; i++)
|
2008-03-06 21:25:09 +00:00
|
|
|
if(drivers[i].enabled)
|
2010-04-13 08:40:21 +00:00
|
|
|
id |= 1 << i;
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
usb_string_iSerial.wString[0] = hex[id];
|
2008-02-27 19:08:30 +00:00
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2009-05-16 15:30:09 +00:00
|
|
|
int usb_core_request_endpoint(int type, int dir, struct usb_class_driver* drv)
|
2008-10-03 22:43:16 +00:00
|
|
|
{
|
|
|
|
int ret, ep;
|
|
|
|
|
2009-05-16 15:30:09 +00:00
|
|
|
ret = usb_drv_request_endpoint(type, dir);
|
2008-10-03 22:43:16 +00:00
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
if(ret == -1)
|
2008-10-03 22:43:16 +00:00
|
|
|
return -1;
|
|
|
|
|
2009-05-16 15:30:09 +00:00
|
|
|
dir = EP_DIR(ret);
|
|
|
|
ep = EP_NUM(ret);
|
2008-10-03 22:43:16 +00:00
|
|
|
|
|
|
|
ep_data[ep].completion_handler[dir] = drv->transfer_complete;
|
|
|
|
ep_data[ep].control_handler[dir] = drv->control_request;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_core_release_endpoint(int ep)
|
|
|
|
{
|
|
|
|
int dir;
|
|
|
|
|
|
|
|
usb_drv_release_endpoint(ep);
|
|
|
|
|
2009-05-16 15:30:09 +00:00
|
|
|
dir = EP_DIR(ep);
|
|
|
|
ep = EP_NUM(ep);
|
2008-10-03 22:43:16 +00:00
|
|
|
|
|
|
|
ep_data[ep].completion_handler[dir] = NULL;
|
|
|
|
ep_data[ep].control_handler[dir] = NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
static void allocate_interfaces_and_endpoints(void)
|
2008-02-27 19:08:30 +00:00
|
|
|
{
|
2008-10-03 22:43:16 +00:00
|
|
|
int i;
|
2010-04-13 08:40:21 +00:00
|
|
|
int interface = 0;
|
2008-04-26 19:02:16 +00:00
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
memset(ep_data, 0, sizeof(ep_data));
|
2008-04-26 19:02:16 +00:00
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
for(i = 0; i < USB_NUM_ENDPOINTS; i++) {
|
2008-10-03 22:43:16 +00:00
|
|
|
usb_drv_release_endpoint(i | USB_DIR_OUT);
|
|
|
|
usb_drv_release_endpoint(i | USB_DIR_IN);
|
|
|
|
}
|
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
for(i = 0; i < USB_NUM_DRIVERS; i++) {
|
2008-03-06 21:25:09 +00:00
|
|
|
if(drivers[i].enabled) {
|
2008-04-26 19:02:16 +00:00
|
|
|
drivers[i].first_interface = interface;
|
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
if(drivers[i].request_endpoints(&drivers[i])) {
|
2008-04-26 19:02:16 +00:00
|
|
|
drivers[i].enabled = false;
|
|
|
|
continue;
|
|
|
|
}
|
2008-10-03 22:43:16 +00:00
|
|
|
|
2008-04-26 19:02:16 +00:00
|
|
|
interface = drivers[i].set_first_interface(interface);
|
|
|
|
drivers[i].last_interface = interface;
|
2008-03-04 12:08:00 +00:00
|
|
|
}
|
2008-02-22 20:38:31 +00:00
|
|
|
}
|
2008-03-06 21:25:09 +00:00
|
|
|
usb_core_num_interfaces = interface;
|
|
|
|
}
|
2008-02-22 20:38:31 +00:00
|
|
|
|
2009-10-19 16:21:50 +00:00
|
|
|
|
2021-09-20 00:05:39 +00:00
|
|
|
static void control_request_handler_drivers(struct usb_ctrlrequest* req, void* reqdata)
|
2008-03-06 21:25:09 +00:00
|
|
|
{
|
2010-04-13 08:40:27 +00:00
|
|
|
int i, interface = req->wIndex & 0xff;
|
|
|
|
bool handled = false;
|
2009-05-16 15:30:09 +00:00
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
for(i = 0; i < USB_NUM_DRIVERS; i++) {
|
2009-04-19 19:53:32 +00:00
|
|
|
if(drivers[i].enabled &&
|
|
|
|
drivers[i].control_request &&
|
2009-05-16 15:30:09 +00:00
|
|
|
drivers[i].first_interface <= interface &&
|
|
|
|
drivers[i].last_interface > interface)
|
2009-04-19 19:53:32 +00:00
|
|
|
{
|
2021-09-20 00:05:39 +00:00
|
|
|
handled = drivers[i].control_request(req, reqdata, response_data);
|
2009-04-19 19:53:32 +00:00
|
|
|
if(handled)
|
|
|
|
break;
|
|
|
|
}
|
2008-03-06 21:25:09 +00:00
|
|
|
}
|
2009-04-19 19:53:32 +00:00
|
|
|
if(!handled) {
|
|
|
|
/* nope. flag error */
|
2010-04-13 08:40:21 +00:00
|
|
|
logf("bad req:desc %d:%d", req->bRequest, req->wValue >> 8);
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_STALL, NULL, 0);
|
2009-04-19 19:53:32 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-30 00:13:00 +00:00
|
|
|
|
2021-09-20 00:05:39 +00:00
|
|
|
static void request_handler_device_get_descriptor(struct usb_ctrlrequest* req, void* reqdata)
|
2009-04-19 19:53:32 +00:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
const void* ptr = NULL;
|
|
|
|
int length = req->wLength;
|
|
|
|
int index = req->wValue & 0xff;
|
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
switch(req->wValue >> 8) { /* type */
|
2009-04-19 19:53:32 +00:00
|
|
|
case USB_DT_DEVICE:
|
|
|
|
ptr = &device_descriptor;
|
|
|
|
size = sizeof(struct usb_device_descriptor);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_DT_OTHER_SPEED_CONFIG:
|
|
|
|
case USB_DT_CONFIG: {
|
|
|
|
int i, max_packet_size;
|
|
|
|
|
|
|
|
if(req->wValue>>8==USB_DT_CONFIG) {
|
2010-04-13 08:40:21 +00:00
|
|
|
max_packet_size = (usb_drv_port_speed() ? 512 : 64);
|
|
|
|
config_descriptor.bDescriptorType = USB_DT_CONFIG;
|
2008-03-06 21:25:09 +00:00
|
|
|
}
|
2009-04-19 19:53:32 +00:00
|
|
|
else {
|
|
|
|
max_packet_size=(usb_drv_port_speed() ? 64 : 512);
|
|
|
|
config_descriptor.bDescriptorType =
|
|
|
|
USB_DT_OTHER_SPEED_CONFIG;
|
2008-03-06 21:25:09 +00:00
|
|
|
}
|
2010-06-05 10:05:27 +00:00
|
|
|
#ifdef HAVE_USB_CHARGING_ENABLE
|
|
|
|
if (usb_charging_mode == USB_CHARGING_DISABLE) {
|
|
|
|
config_descriptor.bMaxPower = (100+1)/2;
|
|
|
|
usb_charging_current_requested = 100;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
config_descriptor.bMaxPower = (500+1)/2;
|
|
|
|
usb_charging_current_requested = 500;
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-19 19:53:32 +00:00
|
|
|
size = sizeof(struct usb_config_descriptor);
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
for(i = 0; i < USB_NUM_DRIVERS; i++)
|
|
|
|
if(drivers[i].enabled && drivers[i].get_config_descriptor)
|
|
|
|
size += drivers[i].get_config_descriptor(
|
|
|
|
&response_data[size], max_packet_size);
|
|
|
|
|
2009-04-19 19:53:32 +00:00
|
|
|
config_descriptor.bNumInterfaces = usb_core_num_interfaces;
|
2009-10-13 19:50:23 +00:00
|
|
|
config_descriptor.wTotalLength = (uint16_t)size;
|
2010-04-13 08:40:21 +00:00
|
|
|
memcpy(&response_data[0], &config_descriptor,
|
2009-04-19 19:53:32 +00:00
|
|
|
sizeof(struct usb_config_descriptor));
|
2008-04-18 16:42:50 +00:00
|
|
|
|
2009-04-19 19:53:32 +00:00
|
|
|
ptr = response_data;
|
|
|
|
break;
|
|
|
|
}
|
2008-04-18 16:42:50 +00:00
|
|
|
|
2009-04-19 19:53:32 +00:00
|
|
|
case USB_DT_STRING:
|
2010-04-13 08:40:21 +00:00
|
|
|
logf("STRING %d", index);
|
2021-10-06 21:40:54 +00:00
|
|
|
if((unsigned)index < USB_STRING_INDEX_MAX) {
|
2009-04-19 19:53:32 +00:00
|
|
|
size = usb_strings[index]->bLength;
|
|
|
|
ptr = usb_strings[index];
|
|
|
|
}
|
2013-11-26 21:03:20 +00:00
|
|
|
else if(index == 0xee) {
|
2014-02-10 06:46:13 +00:00
|
|
|
/* We don't have a real OS descriptor, and we don't handle
|
|
|
|
* STALL correctly on some devices, so we return any valid
|
|
|
|
* string (we arbitrarily pick the manufacturer name)
|
|
|
|
*/
|
2013-11-26 21:03:20 +00:00
|
|
|
size = usb_string_iManufacturer.bLength;
|
|
|
|
ptr = &usb_string_iManufacturer;
|
|
|
|
}
|
2009-04-19 19:53:32 +00:00
|
|
|
else {
|
2010-04-13 08:40:21 +00:00
|
|
|
logf("bad string id %d", index);
|
2021-09-20 00:59:52 +00:00
|
|
|
ptr = NULL;
|
2008-02-26 18:02:26 +00:00
|
|
|
}
|
2008-02-20 22:54:26 +00:00
|
|
|
break;
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2009-04-19 19:53:32 +00:00
|
|
|
case USB_DT_DEVICE_QUALIFIER:
|
|
|
|
ptr = &qualifier_descriptor;
|
|
|
|
size = sizeof(struct usb_qualifier_descriptor);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
logf("ctrl desc.");
|
2021-09-20 00:05:39 +00:00
|
|
|
control_request_handler_drivers(req, reqdata);
|
2021-09-20 00:59:52 +00:00
|
|
|
return;
|
2009-04-19 19:53:32 +00:00
|
|
|
}
|
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
if(ptr) {
|
|
|
|
logf("data %d (%d)", size, length);
|
|
|
|
length = MIN(size, length);
|
2009-04-19 19:53:32 +00:00
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
if (ptr != response_data)
|
|
|
|
memcpy(response_data, ptr, length);
|
2009-04-19 19:53:32 +00:00
|
|
|
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_ACK, response_data, length);
|
|
|
|
} else {
|
|
|
|
usb_drv_control_response(USB_CONTROL_STALL, NULL, 0);
|
2009-04-19 19:53:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 12:37:26 +00:00
|
|
|
static void usb_core_do_set_addr(uint8_t address)
|
2009-04-19 19:53:32 +00:00
|
|
|
{
|
2012-12-07 12:37:26 +00:00
|
|
|
logf("usb_core: SET_ADR %d", address);
|
|
|
|
usb_address = address;
|
|
|
|
usb_state = ADDRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_core_do_set_config(uint8_t config)
|
|
|
|
{
|
2014-01-05 21:54:10 +00:00
|
|
|
logf("usb_core: SET_CONFIG %d",config);
|
2012-12-07 12:37:26 +00:00
|
|
|
if(config) {
|
|
|
|
usb_state = CONFIGURED;
|
2014-01-05 21:54:10 +00:00
|
|
|
|
|
|
|
if(drivers_connected)
|
|
|
|
for(int i = 0; i < USB_NUM_DRIVERS; i++)
|
|
|
|
if(drivers[i].enabled && drivers[i].disconnect != NULL)
|
|
|
|
drivers[i].disconnect();
|
|
|
|
|
2012-12-07 12:37:26 +00:00
|
|
|
for(int i = 0; i < USB_NUM_DRIVERS; i++)
|
|
|
|
if(drivers[i].enabled && drivers[i].init_connection)
|
|
|
|
drivers[i].init_connection();
|
2014-01-05 21:54:10 +00:00
|
|
|
drivers_connected = true;
|
2012-12-07 12:37:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
usb_state = ADDRESS;
|
|
|
|
#ifdef HAVE_USB_CHARGING_ENABLE
|
|
|
|
usb_charging_maxcurrent_change(usb_charging_maxcurrent());
|
|
|
|
#endif
|
|
|
|
}
|
2009-04-19 19:53:32 +00:00
|
|
|
|
2013-07-24 20:29:44 +00:00
|
|
|
static void usb_core_do_clear_feature(int recip, int recip_nr, int feature)
|
|
|
|
{
|
|
|
|
logf("usb_core: CLEAR FEATURE (%d,%d,%d)", recip, recip_nr, feature);
|
|
|
|
if(recip == USB_RECIP_ENDPOINT)
|
|
|
|
{
|
|
|
|
if(feature == USB_ENDPOINT_HALT)
|
|
|
|
usb_drv_stall(EP_NUM(recip_nr), false, EP_DIR(recip_nr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 00:05:39 +00:00
|
|
|
static void request_handler_device(struct usb_ctrlrequest* req, void* reqdata)
|
2012-12-07 12:37:26 +00:00
|
|
|
{
|
2021-11-09 14:12:13 +00:00
|
|
|
unsigned address;
|
|
|
|
|
2009-04-19 19:53:32 +00:00
|
|
|
switch(req->bRequest) {
|
2021-09-20 00:59:52 +00:00
|
|
|
case USB_REQ_GET_CONFIGURATION:
|
|
|
|
logf("usb_core: GET_CONFIG");
|
|
|
|
response_data[0] = (usb_state == ADDRESS ? 0 : 1);
|
|
|
|
usb_drv_control_response(USB_CONTROL_ACK, response_data, 1);
|
|
|
|
break;
|
|
|
|
case USB_REQ_SET_CONFIGURATION:
|
|
|
|
usb_drv_cancel_all_transfers();
|
|
|
|
usb_core_do_set_config(req->wValue);
|
|
|
|
usb_drv_control_response(USB_CONTROL_ACK, NULL, 0);
|
|
|
|
break;
|
|
|
|
case USB_REQ_SET_ADDRESS:
|
|
|
|
/* NOTE: We really have no business handling this and drivers
|
|
|
|
* should just handle it themselves. We don't care beyond
|
|
|
|
* knowing if we've been assigned an address yet, or not. */
|
2021-11-09 14:12:13 +00:00
|
|
|
address = req->wValue;
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_ACK, NULL, 0);
|
|
|
|
usb_drv_cancel_all_transfers();
|
2021-11-09 14:12:13 +00:00
|
|
|
usb_drv_set_address(address);
|
|
|
|
usb_core_do_set_addr(address);
|
2021-09-20 00:59:52 +00:00
|
|
|
break;
|
2009-04-19 19:53:32 +00:00
|
|
|
case USB_REQ_GET_DESCRIPTOR:
|
2010-04-13 08:40:21 +00:00
|
|
|
logf("usb_core: GET_DESC %d", req->wValue >> 8);
|
2021-09-20 00:05:39 +00:00
|
|
|
request_handler_device_get_descriptor(req, reqdata);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
case USB_REQ_SET_FEATURE:
|
|
|
|
if(req->wValue==USB_DEVICE_TEST_MODE) {
|
2010-04-13 08:40:21 +00:00
|
|
|
int mode = req->wIndex >> 8;
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_ACK, NULL, 0);
|
2009-04-19 19:53:32 +00:00
|
|
|
usb_drv_set_test_mode(mode);
|
2021-09-20 00:59:52 +00:00
|
|
|
} else {
|
|
|
|
usb_drv_control_response(USB_CONTROL_STALL, NULL, 0);
|
2009-04-19 19:53:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case USB_REQ_GET_STATUS:
|
2010-04-13 08:40:21 +00:00
|
|
|
response_data[0] = 0;
|
|
|
|
response_data[1] = 0;
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_ACK, response_data, 2);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
2010-09-20 14:18:14 +00:00
|
|
|
logf("bad req:desc %d:%d", req->bRequest, req->wValue);
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_STALL, NULL, 0);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 00:05:39 +00:00
|
|
|
static void request_handler_interface_standard(struct usb_ctrlrequest* req, void* reqdata)
|
2009-04-19 19:53:32 +00:00
|
|
|
{
|
|
|
|
switch (req->bRequest)
|
|
|
|
{
|
|
|
|
case USB_REQ_SET_INTERFACE:
|
|
|
|
logf("usb_core: SET_INTERFACE");
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_ACK, NULL, 0);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_GET_INTERFACE:
|
|
|
|
logf("usb_core: GET_INTERFACE");
|
2010-04-13 08:40:21 +00:00
|
|
|
response_data[0] = 0;
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_ACK, response_data, 1);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
case USB_REQ_GET_STATUS:
|
2010-04-13 08:40:21 +00:00
|
|
|
response_data[0] = 0;
|
|
|
|
response_data[1] = 0;
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_ACK, response_data, 2);
|
|
|
|
break;
|
|
|
|
case USB_REQ_CLEAR_FEATURE:
|
|
|
|
case USB_REQ_SET_FEATURE:
|
|
|
|
/* TODO: These used to be ignored (erroneously).
|
|
|
|
* Should they be passed to the drivers instead? */
|
|
|
|
usb_drv_control_response(USB_CONTROL_STALL, NULL, 0);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-09-20 00:05:39 +00:00
|
|
|
control_request_handler_drivers(req, reqdata);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 00:05:39 +00:00
|
|
|
static void request_handler_interface(struct usb_ctrlrequest* req, void* reqdata)
|
2009-04-19 19:53:32 +00:00
|
|
|
{
|
|
|
|
switch(req->bRequestType & USB_TYPE_MASK) {
|
|
|
|
case USB_TYPE_STANDARD:
|
2021-09-20 00:05:39 +00:00
|
|
|
request_handler_interface_standard(req, reqdata);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
case USB_TYPE_CLASS:
|
2021-09-20 00:05:39 +00:00
|
|
|
control_request_handler_drivers(req, reqdata);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
case USB_TYPE_VENDOR:
|
2010-09-20 14:18:14 +00:00
|
|
|
default:
|
|
|
|
logf("bad req:desc %d", req->bRequest);
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_STALL, NULL, 0);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 00:59:52 +00:00
|
|
|
static void request_handler_endpoint_drivers(struct usb_ctrlrequest* req, void* reqdata)
|
2010-03-08 13:00:24 +00:00
|
|
|
{
|
|
|
|
bool handled = false;
|
|
|
|
control_handler_t control_handler = NULL;
|
|
|
|
|
|
|
|
if(EP_NUM(req->wIndex) < USB_NUM_ENDPOINTS)
|
2010-04-13 10:07:02 +00:00
|
|
|
control_handler =
|
|
|
|
ep_data[EP_NUM(req->wIndex)].control_handler[EP_DIR(req->wIndex)];
|
2010-03-08 13:00:24 +00:00
|
|
|
|
|
|
|
if(control_handler)
|
2021-09-20 00:05:39 +00:00
|
|
|
handled = control_handler(req, reqdata, response_data);
|
2010-03-08 13:00:24 +00:00
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
if(!handled) {
|
2010-03-08 13:00:24 +00:00
|
|
|
/* nope. flag error */
|
2010-04-13 08:40:21 +00:00
|
|
|
logf("usb bad req %d", req->bRequest);
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_STALL, NULL, 0);
|
2010-03-08 13:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 00:05:39 +00:00
|
|
|
static void request_handler_endpoint_standard(struct usb_ctrlrequest* req, void* reqdata)
|
2009-04-19 19:53:32 +00:00
|
|
|
{
|
|
|
|
switch (req->bRequest) {
|
|
|
|
case USB_REQ_CLEAR_FEATURE:
|
2013-07-24 20:29:44 +00:00
|
|
|
usb_core_do_clear_feature(USB_RECIP_ENDPOINT,
|
|
|
|
req->wIndex,
|
|
|
|
req->wValue);
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_ACK, NULL, 0);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
case USB_REQ_SET_FEATURE:
|
2013-07-24 20:40:12 +00:00
|
|
|
logf("usb_core: SET FEATURE (%d)", req->wValue);
|
2010-04-13 08:40:21 +00:00
|
|
|
if(req->wValue == USB_ENDPOINT_HALT)
|
2009-05-16 15:30:09 +00:00
|
|
|
usb_drv_stall(EP_NUM(req->wIndex), true, EP_DIR(req->wIndex));
|
2021-09-20 00:59:52 +00:00
|
|
|
|
|
|
|
usb_drv_control_response(USB_CONTROL_ACK, NULL, 0);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
case USB_REQ_GET_STATUS:
|
2010-04-13 08:40:21 +00:00
|
|
|
response_data[0] = 0;
|
|
|
|
response_data[1] = 0;
|
2009-04-19 19:53:32 +00:00
|
|
|
logf("usb_core: GET_STATUS");
|
2010-04-13 08:40:21 +00:00
|
|
|
if(req->wIndex > 0)
|
2010-04-13 10:07:02 +00:00
|
|
|
response_data[0] = usb_drv_stalled(EP_NUM(req->wIndex),
|
|
|
|
EP_DIR(req->wIndex));
|
2021-09-20 00:59:52 +00:00
|
|
|
|
|
|
|
usb_drv_control_response(USB_CONTROL_ACK, response_data, 2);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
2010-03-08 13:00:24 +00:00
|
|
|
default:
|
2021-09-20 00:59:52 +00:00
|
|
|
request_handler_endpoint_drivers(req, reqdata);
|
2010-03-08 13:00:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 00:05:39 +00:00
|
|
|
static void request_handler_endpoint(struct usb_ctrlrequest* req, void* reqdata)
|
2010-03-08 13:00:24 +00:00
|
|
|
{
|
|
|
|
switch(req->bRequestType & USB_TYPE_MASK) {
|
|
|
|
case USB_TYPE_STANDARD:
|
2021-09-20 00:05:39 +00:00
|
|
|
request_handler_endpoint_standard(req, reqdata);
|
2010-03-08 13:00:24 +00:00
|
|
|
break;
|
|
|
|
case USB_TYPE_CLASS:
|
2021-09-20 00:59:52 +00:00
|
|
|
request_handler_endpoint_drivers(req, reqdata);
|
2010-03-08 13:00:24 +00:00
|
|
|
break;
|
|
|
|
case USB_TYPE_VENDOR:
|
|
|
|
default:
|
2010-09-20 14:18:14 +00:00
|
|
|
logf("bad req:desc %d", req->bRequest);
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_STALL, NULL, 0);
|
2010-03-08 13:00:24 +00:00
|
|
|
break;
|
2007-11-22 20:51:00 +00:00
|
|
|
}
|
2009-04-19 19:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handling USB requests starts here */
|
2021-09-20 00:05:39 +00:00
|
|
|
static void usb_core_control_request_handler(struct usb_ctrlrequest* req, void* reqdata)
|
2009-04-19 19:53:32 +00:00
|
|
|
{
|
2010-06-05 20:43:30 +00:00
|
|
|
#ifdef HAVE_USB_CHARGING_ENABLE
|
|
|
|
timeout_cancel(&usb_no_host_timeout);
|
|
|
|
if(usb_no_host) {
|
2011-05-08 11:37:18 +00:00
|
|
|
usb_no_host = false;
|
|
|
|
usb_charging_maxcurrent_change(usb_charging_maxcurrent());
|
2010-06-05 20:43:30 +00:00
|
|
|
}
|
|
|
|
#endif
|
2010-04-13 08:40:21 +00:00
|
|
|
if(usb_state == DEFAULT) {
|
2009-04-19 19:53:32 +00:00
|
|
|
set_serial_descriptor();
|
|
|
|
usb_core_set_serial_function_id();
|
|
|
|
|
|
|
|
allocate_interfaces_and_endpoints();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(req->bRequestType & USB_RECIP_MASK) {
|
|
|
|
case USB_RECIP_DEVICE:
|
2021-09-20 00:05:39 +00:00
|
|
|
request_handler_device(req, reqdata);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
case USB_RECIP_INTERFACE:
|
2021-09-20 00:05:39 +00:00
|
|
|
request_handler_interface(req, reqdata);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
case USB_RECIP_ENDPOINT:
|
2021-09-20 00:05:39 +00:00
|
|
|
request_handler_endpoint(req, reqdata);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
2021-09-20 00:59:52 +00:00
|
|
|
default:
|
2009-04-19 19:53:32 +00:00
|
|
|
logf("unsupported recipient");
|
2021-09-20 00:59:52 +00:00
|
|
|
usb_drv_control_response(USB_CONTROL_STALL, NULL, 0);
|
2009-04-19 19:53:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called by usb_drv_int() */
|
|
|
|
void usb_core_bus_reset(void)
|
|
|
|
{
|
2014-01-05 21:54:10 +00:00
|
|
|
logf("usb_core: bus reset");
|
2010-04-13 08:40:21 +00:00
|
|
|
usb_address = 0;
|
|
|
|
usb_state = DEFAULT;
|
2010-06-05 10:05:27 +00:00
|
|
|
#ifdef HAVE_USB_CHARGING_ENABLE
|
2021-10-02 10:53:20 +00:00
|
|
|
#ifdef HAVE_USB_CHARGING_IN_THREAD
|
|
|
|
/* On some targets usb_charging_maxcurrent_change() cannot be called
|
|
|
|
* from an interrupt handler; get the USB thread to do it instead. */
|
|
|
|
usb_charger_update();
|
|
|
|
#else
|
2010-06-05 10:05:27 +00:00
|
|
|
usb_charging_maxcurrent_change(usb_charging_maxcurrent());
|
|
|
|
#endif
|
2021-10-02 10:53:20 +00:00
|
|
|
#endif
|
2007-11-22 20:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called by usb_drv_transfer_completed() */
|
2010-04-13 08:40:21 +00:00
|
|
|
void usb_core_transfer_complete(int endpoint, int dir, int status, int length)
|
2007-11-22 20:51:00 +00:00
|
|
|
{
|
2021-09-20 00:41:26 +00:00
|
|
|
struct usb_transfer_completion_event_data* completion_event =
|
|
|
|
&ep_data[endpoint].completion_event[EP_DIR(dir)];
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2021-11-13 17:16:28 +00:00
|
|
|
void* data0 = NULL;
|
|
|
|
void* data1 = NULL;
|
2021-09-20 00:41:26 +00:00
|
|
|
|
|
|
|
#ifdef USB_LEGACY_CONTROL_API
|
|
|
|
if(endpoint == EP_CONTROL) {
|
2021-11-08 21:12:24 +00:00
|
|
|
bool cwdd = control_write_data_done;
|
|
|
|
struct usb_ctrlrequest* req = active_request;
|
|
|
|
|
|
|
|
if(dir == USB_DIR_OUT && req && cwdd) {
|
2021-11-13 17:16:28 +00:00
|
|
|
data0 = req;
|
|
|
|
data1 = control_write_data;
|
2021-09-20 00:41:26 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
}
|
2021-09-20 00:41:26 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-13 17:16:28 +00:00
|
|
|
completion_event->endpoint = endpoint;
|
|
|
|
completion_event->dir = dir;
|
|
|
|
completion_event->data[0] = data0;
|
|
|
|
completion_event->data[1] = data1;
|
|
|
|
completion_event->status = status;
|
|
|
|
completion_event->length = length;
|
|
|
|
|
2021-09-20 00:41:26 +00:00
|
|
|
usb_signal_transfer_completion(completion_event);
|
2007-11-22 20:51:00 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 12:37:26 +00:00
|
|
|
void usb_core_handle_notify(long id, intptr_t data)
|
|
|
|
{
|
|
|
|
switch(id)
|
|
|
|
{
|
|
|
|
case USB_NOTIFY_SET_ADDR:
|
|
|
|
usb_core_do_set_addr(data);
|
|
|
|
break;
|
|
|
|
case USB_NOTIFY_SET_CONFIG:
|
|
|
|
usb_core_do_set_config(data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 00:41:26 +00:00
|
|
|
void usb_core_control_request(struct usb_ctrlrequest* req, void* reqdata)
|
2008-02-20 22:54:26 +00:00
|
|
|
{
|
2009-04-19 19:53:32 +00:00
|
|
|
struct usb_transfer_completion_event_data* completion_event =
|
2009-11-15 22:17:45 +00:00
|
|
|
&ep_data[EP_CONTROL].completion_event[EP_DIR(USB_DIR_IN)];
|
2009-04-18 21:32:41 +00:00
|
|
|
|
2010-04-13 08:40:21 +00:00
|
|
|
completion_event->endpoint = EP_CONTROL;
|
|
|
|
completion_event->dir = 0;
|
2021-09-19 23:58:33 +00:00
|
|
|
completion_event->data[0] = (void*)req;
|
2021-09-20 00:41:26 +00:00
|
|
|
completion_event->data[1] = reqdata;
|
2010-04-13 08:40:21 +00:00
|
|
|
completion_event->status = 0;
|
|
|
|
completion_event->length = 0;
|
2013-07-24 20:40:12 +00:00
|
|
|
logf("ctrl received %ld, req=0x%x", current_tick, req->bRequest);
|
2009-04-18 21:32:41 +00:00
|
|
|
usb_signal_transfer_completion(completion_event);
|
2008-02-20 22:54:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 00:41:26 +00:00
|
|
|
void usb_core_control_complete(int status)
|
|
|
|
{
|
|
|
|
/* We currently don't use this, it's here to make the API look good ;)
|
|
|
|
* It makes sense to #define it away on normal builds.
|
|
|
|
*/
|
|
|
|
(void)status;
|
|
|
|
logf("ctrl complete %ld, %d", current_tick, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USB_LEGACY_CONTROL_API
|
|
|
|
/* Only needed if the driver does not support the new API yet */
|
|
|
|
void usb_core_legacy_control_request(struct usb_ctrlrequest* req)
|
|
|
|
{
|
2021-11-14 14:19:44 +00:00
|
|
|
active_request = req;
|
2021-09-20 00:41:26 +00:00
|
|
|
control_write_data = NULL;
|
|
|
|
control_write_data_done = false;
|
|
|
|
|
|
|
|
usb_core_control_request(req, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_drv_control_response(enum usb_control_response resp,
|
|
|
|
void* data, int length)
|
|
|
|
{
|
2021-11-08 21:12:24 +00:00
|
|
|
struct usb_ctrlrequest* req = active_request;
|
|
|
|
|
|
|
|
if(!req)
|
2021-09-20 00:41:26 +00:00
|
|
|
panicf("null ctrl req");
|
|
|
|
|
2021-11-08 21:12:24 +00:00
|
|
|
if(req->wLength == 0)
|
2021-09-20 00:41:26 +00:00
|
|
|
{
|
|
|
|
active_request = NULL;
|
|
|
|
|
|
|
|
/* No-data request */
|
|
|
|
if(resp == USB_CONTROL_ACK)
|
|
|
|
usb_drv_send(EP_CONTROL, data, length);
|
|
|
|
else if(resp == USB_CONTROL_STALL)
|
|
|
|
usb_drv_stall(EP_CONTROL, true, true);
|
|
|
|
else
|
|
|
|
panicf("RECEIVE on non-data req");
|
|
|
|
}
|
2021-11-08 21:12:24 +00:00
|
|
|
else if(req->bRequestType & USB_DIR_IN)
|
2021-09-20 00:41:26 +00:00
|
|
|
{
|
|
|
|
/* Control read request */
|
|
|
|
if(resp == USB_CONTROL_ACK)
|
|
|
|
{
|
|
|
|
active_request = NULL;
|
|
|
|
usb_drv_recv_nonblocking(EP_CONTROL, NULL, 0);
|
|
|
|
usb_drv_send(EP_CONTROL, data, length);
|
|
|
|
}
|
|
|
|
else if(resp == USB_CONTROL_STALL)
|
|
|
|
{
|
|
|
|
active_request = NULL;
|
|
|
|
usb_drv_stall(EP_CONTROL, true, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
panicf("RECEIVE on ctrl read req");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!control_write_data_done)
|
|
|
|
{
|
|
|
|
/* Control write request, data phase */
|
|
|
|
if(resp == USB_CONTROL_RECEIVE)
|
|
|
|
{
|
|
|
|
control_write_data = data;
|
|
|
|
control_write_data_done = true;
|
|
|
|
usb_drv_recv_nonblocking(EP_CONTROL, data, length);
|
|
|
|
}
|
|
|
|
else if(resp == USB_CONTROL_STALL)
|
|
|
|
{
|
|
|
|
/* We should stall the OUT endpoint here, but the old code did
|
|
|
|
* not do so and some drivers may not handle it correctly. */
|
|
|
|
active_request = NULL;
|
|
|
|
usb_drv_stall(EP_CONTROL, true, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
panicf("ACK on ctrl write data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
active_request = NULL;
|
|
|
|
control_write_data = NULL;
|
|
|
|
control_write_data_done = false;
|
|
|
|
|
|
|
|
/* Control write request, status phase */
|
|
|
|
if(resp == USB_CONTROL_ACK)
|
|
|
|
usb_drv_send(EP_CONTROL, NULL, 0);
|
|
|
|
else if(resp == USB_CONTROL_STALL)
|
|
|
|
usb_drv_stall(EP_CONTROL, true, true);
|
|
|
|
else
|
|
|
|
panicf("RECEIVE on ctrl write status");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-07 12:37:26 +00:00
|
|
|
void usb_core_notify_set_address(uint8_t addr)
|
|
|
|
{
|
|
|
|
logf("notify set addr received %ld", current_tick);
|
|
|
|
usb_signal_notify(USB_NOTIFY_SET_ADDR, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_core_notify_set_config(uint8_t config)
|
|
|
|
{
|
|
|
|
logf("notify set config received %ld", current_tick);
|
|
|
|
usb_signal_notify(USB_NOTIFY_SET_CONFIG, config);
|
|
|
|
}
|
|
|
|
|
2010-06-05 10:05:27 +00:00
|
|
|
#ifdef HAVE_USB_CHARGING_ENABLE
|
|
|
|
void usb_charging_enable(int state)
|
|
|
|
{
|
|
|
|
usb_charging_mode = state;
|
|
|
|
usb_charging_maxcurrent_change(usb_charging_maxcurrent());
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_charging_maxcurrent()
|
2008-06-04 18:55:58 +00:00
|
|
|
{
|
2010-06-05 20:43:30 +00:00
|
|
|
if (!initialized || usb_charging_mode == USB_CHARGING_DISABLE)
|
2010-06-05 10:05:27 +00:00
|
|
|
return 100;
|
2010-06-05 20:43:30 +00:00
|
|
|
if (usb_state == CONFIGURED)
|
2011-05-08 11:37:18 +00:00
|
|
|
return usb_charging_current_requested;
|
2010-06-05 20:43:30 +00:00
|
|
|
if (usb_charging_mode == USB_CHARGING_FORCE && usb_no_host)
|
2011-05-08 11:37:18 +00:00
|
|
|
return 500;
|
2010-06-05 20:43:30 +00:00
|
|
|
return 100;
|
2008-06-04 18:55:58 +00:00
|
|
|
}
|
|
|
|
#endif
|