2003-02-07 09:41:57 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Linus Nielsen Feltzing
|
|
|
|
*
|
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.
|
2003-02-07 09:41:57 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef _USB_H_
|
|
|
|
#define _USB_H_
|
|
|
|
|
2011-12-19 20:12:52 +00:00
|
|
|
#include "config.h"
|
2003-02-07 09:41:57 +00:00
|
|
|
#include "kernel.h"
|
2007-09-04 08:03:07 +00:00
|
|
|
#include "button.h"
|
|
|
|
|
2015-01-02 11:39:18 +00:00
|
|
|
/** USB introduction
|
|
|
|
* Targets which do not have any hardware support for USB, and cannot even detect
|
|
|
|
* it must define USB_NONE. Otherwise, they must at least implement USB
|
|
|
|
* detection.
|
|
|
|
*
|
|
|
|
* USB architecture
|
|
|
|
* The USB code is split into several parts:
|
|
|
|
* - usb: manages the USB connection
|
|
|
|
* - usb_core: implements a software USB stack based on usb_drv
|
|
|
|
* - usb_drv: implements the USB protocol based on some hardware transceiver/core
|
|
|
|
* - usb_{hid,storage,...}: implement USB functionalities based on usb_core
|
|
|
|
* Note that not all those are compiled in, in particular in the case of a
|
|
|
|
* hardware USB stack, or when the driver doesn't support all types of transfers.
|
|
|
|
*
|
|
|
|
* Software versus hardware USB stack
|
|
|
|
* A very important thing to keep in mind is that there are two very different
|
|
|
|
* situations:
|
|
|
|
* - software USB stack: the device only provides a USB transceiver and the
|
|
|
|
* USB stack must be implemented entirely in software. In this case the target
|
|
|
|
* must define HAVE_USBSTACK, correctly set CONFIG_USBOTG and implement a driver
|
|
|
|
* for the transceiver.
|
|
|
|
* - hardware USB stack: the device has a dedicated chip which implements the
|
|
|
|
* USB stack in hardware. In this case the target must *NOT* define HAVE_USBSTACK
|
|
|
|
* but can still define CONFIG_USBOTG and implement a driver to enable/disable
|
|
|
|
* the USB hardware.
|
|
|
|
*
|
|
|
|
* USB ignore buttons
|
|
|
|
* In some cases, the user wants to prevent Rockbox from entering USB mode. It
|
|
|
|
* can do so by holding a button while inserting the cable. By default any button
|
|
|
|
* will prevent the USB mode from kicking-in, so targets can optionally define
|
|
|
|
* USBPOWER_BTN_IGNORE to a mask of buttons to ignore in this check.
|
|
|
|
*
|
|
|
|
* USB states
|
|
|
|
* It is important to understand that the usb code can be in one of three states:
|
|
|
|
* - extracted: no USB cable is plugged
|
|
|
|
* - powered-only: a USB cable is plugged but the USB mode will not be entered,
|
|
|
|
* either because no host was detected or because the user requested so.
|
|
|
|
* - inserted: a USB cable is plugged and the USB mode has been entered, either
|
|
|
|
* the software or hardware stack is running.
|
|
|
|
*
|
|
|
|
* USB exclusive mode
|
|
|
|
* Either in hardware or software stack, if the USB was configured to run in
|
|
|
|
* mass storage mode, it will require exclusive access to the disk and ask all
|
|
|
|
* threads to release any file handle and stop using the disks. It does so by
|
|
|
|
* broadcasting a SYS_USB_CONNECTED message, which threads must acknowledge using
|
|
|
|
* usb_acknowledge(SYS_USB_CONNECTED_ACK). They must not access the disk until
|
|
|
|
* SYS_USB_DISCONNECTED is broadcast. To ease waiting, threads can call
|
|
|
|
* usb_wait_for_disconnect() or usb_wait_for_disconnect_w_tmo() on their waiting
|
|
|
|
* queue.
|
|
|
|
*
|
|
|
|
* USB detection
|
|
|
|
* Except when no usb code is compiled at all (USB_NONE), the usb thread keeps
|
|
|
|
* track of the USB insertion state, which can be either USB_INSERTED (meaning
|
|
|
|
* 5v is present) or USB_EXTRACTED. Each target must implement usb_detect()
|
|
|
|
* to report the insertion state.
|
|
|
|
* Targets which support insertion/extraction interrupts must define
|
|
|
|
* USB_STATUS_BY_EVENT and notify the thread on changes by calling
|
|
|
|
* usb_status_event() with the new state. Other targets must *not* define
|
|
|
|
* USB_STATUS_BY_EVENT and the usb thread by regularly poll the insertion state
|
|
|
|
* using usb_detect().
|
|
|
|
*
|
|
|
|
* USB powering & charging
|
|
|
|
* Device which can be powered from USB must define HAVE_USB_POWER. Note that
|
|
|
|
* powering doesn't imply charging (for example a AA-powered device can be
|
|
|
|
* powered from USB but not charged), charging sources are reported by the
|
|
|
|
* power subsystem (see power.h). The USB specification mandates the maximum
|
|
|
|
* current which can be drawn under which cirmcunstances. Device which cannot
|
|
|
|
* control the charge current should make sure it is always <100mA to meet the
|
|
|
|
* USB specification. Device with configurable charging current which support
|
|
|
|
* >=100mA must define HAVE_USB_CHARGING_ENABLE and implement
|
|
|
|
* usb_charging_maxcurrent_change() to let the usb thread control the maximum
|
|
|
|
* charging control.
|
|
|
|
* */
|
|
|
|
|
2009-01-13 16:27:35 +00:00
|
|
|
#if defined(IPOD_COLOR) || defined(IPOD_4G) \
|
|
|
|
|| defined(IPOD_MINI) || defined(IPOD_MINI2G)
|
|
|
|
#define USB_FIREWIRE_HANDLING
|
|
|
|
#endif
|
|
|
|
|
2007-09-04 08:03:07 +00:00
|
|
|
/* Messages from usb_tick and thread states */
|
2011-12-15 00:14:36 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
USB_SCREENDUMP = -1, /* State */
|
2009-01-22 22:05:04 +00:00
|
|
|
USB_EXTRACTED = 0, /* Event+State */
|
2009-01-13 16:27:35 +00:00
|
|
|
USB_INSERTED, /* Event+State */
|
2015-01-02 11:39:18 +00:00
|
|
|
USB_POWERED, /* State - transitional indicator if no host */
|
2009-01-13 16:27:35 +00:00
|
|
|
#if (CONFIG_STORAGE & STORAGE_MMC)
|
|
|
|
USB_REENABLE, /* Event */
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_USBSTACK
|
|
|
|
USB_TRANSFER_COMPLETION, /* Event */
|
2012-12-07 12:37:26 +00:00
|
|
|
USB_NOTIFY_SET_ADDR, /* Event */
|
|
|
|
USB_NOTIFY_SET_CONFIG, /* Event */
|
2009-01-13 16:27:35 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USB_FIREWIRE_HANDLING
|
|
|
|
USB_REQUEST_REBOOT, /* Event */
|
|
|
|
#endif
|
|
|
|
USB_QUIT, /* Event */
|
2010-06-05 20:43:30 +00:00
|
|
|
#if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
|
|
|
|
USB_CHARGER_UPDATE, /* Event */
|
|
|
|
#endif
|
2011-01-15 08:19:30 +00:00
|
|
|
#ifdef HAVE_BOOTLOADER_USB_MODE
|
|
|
|
USB_HANDLED, /* Bootloader status code */
|
|
|
|
#endif
|
2008-02-27 19:08:30 +00:00
|
|
|
};
|
2011-12-15 00:14:36 +00:00
|
|
|
|
2020-11-02 13:10:15 +00:00
|
|
|
/* Supported usb modes. */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
USB_MODE_MASS_STORAGE,
|
|
|
|
USB_MODE_CHARGE,
|
|
|
|
USB_MODE_ADB
|
|
|
|
};
|
|
|
|
|
2008-02-27 19:08:30 +00:00
|
|
|
#ifdef HAVE_USB_POWER
|
2011-05-16 21:10:31 +00:00
|
|
|
/*allow people to define this in config-target.h if they need it*/
|
2020-07-15 23:40:55 +00:00
|
|
|
#if !defined(USBPOWER_BTN_IGNORE)
|
2011-05-16 20:27:14 +00:00
|
|
|
#define USBPOWER_BTN_IGNORE 0
|
|
|
|
#endif
|
2020-11-16 16:32:12 +00:00
|
|
|
|
|
|
|
#if defined(BOOTLOADER)
|
|
|
|
#define USBMODE_DEFAULT USB_MODE_MASS_STORAGE
|
|
|
|
#else
|
|
|
|
#define USBMODE_DEFAULT USB_MODE_MASS_STORAGE
|
|
|
|
#endif
|
|
|
|
|
2007-09-04 08:03:07 +00:00
|
|
|
#endif
|
2003-02-07 09:41:57 +00:00
|
|
|
|
2008-02-25 23:43:11 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
|
|
|
/* USB class drivers */
|
|
|
|
enum {
|
2009-11-02 04:37:57 +00:00
|
|
|
#ifdef USB_ENABLE_STORAGE
|
2008-02-25 23:43:11 +00:00
|
|
|
USB_DRIVER_MASS_STORAGE,
|
2009-11-02 04:37:57 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USB_ENABLE_SERIAL
|
2008-02-25 23:43:11 +00:00
|
|
|
USB_DRIVER_SERIAL,
|
2009-11-02 04:37:57 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USB_ENABLE_CHARGING_ONLY
|
2008-03-06 21:25:09 +00:00
|
|
|
USB_DRIVER_CHARGING_ONLY,
|
2009-11-02 04:37:57 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USB_ENABLE_HID
|
2009-04-19 21:17:18 +00:00
|
|
|
USB_DRIVER_HID,
|
2009-11-02 04:37:57 +00:00
|
|
|
#endif
|
2008-03-06 21:25:09 +00:00
|
|
|
USB_NUM_DRIVERS
|
2008-02-25 23:43:11 +00:00
|
|
|
};
|
2009-04-18 21:32:41 +00:00
|
|
|
|
2008-02-27 19:08:30 +00:00
|
|
|
struct usb_transfer_completion_event_data
|
|
|
|
{
|
|
|
|
unsigned char endpoint;
|
2008-10-03 22:43:16 +00:00
|
|
|
int dir;
|
2008-02-27 19:08:30 +00:00
|
|
|
int status;
|
|
|
|
int length;
|
2021-09-19 23:58:33 +00:00
|
|
|
void* data[2];
|
2008-02-27 19:08:30 +00:00
|
|
|
};
|
2009-05-16 15:30:09 +00:00
|
|
|
#endif /* HAVE_USBSTACK */
|
2008-02-25 23:43:11 +00:00
|
|
|
|
2015-01-02 11:39:18 +00:00
|
|
|
/* initialise the usb code and thread */
|
2010-03-03 23:20:32 +00:00
|
|
|
void usb_init(void) INIT_ATTR;
|
2015-01-02 11:39:18 +00:00
|
|
|
/* target must implement this to enable/disable the usb transceiver/core */
|
2005-12-06 12:12:29 +00:00
|
|
|
void usb_enable(bool on);
|
2017-11-13 23:26:16 +00:00
|
|
|
/* when one or more driver requires exclusive mode, this is called after all threads have acknowledged
|
|
|
|
* exclusive mode and disk have been umounted; otherwise it is called immediately after host has
|
|
|
|
* been detected */
|
2009-01-19 13:41:25 +00:00
|
|
|
void usb_attach(void);
|
2015-01-02 11:39:18 +00:00
|
|
|
/* enable usb detection monitoring; before this function is called, all usb
|
|
|
|
* detection changes are ignored */
|
2011-12-19 20:12:52 +00:00
|
|
|
void usb_start_monitoring(void) INIT_ATTR;
|
2008-05-10 18:00:11 +00:00
|
|
|
void usb_close(void);
|
2015-01-02 11:39:18 +00:00
|
|
|
/* acknowledge usb connection, typically with SYS_USB_CONNECTED_ACK */
|
2005-01-30 15:16:58 +00:00
|
|
|
void usb_acknowledge(long id);
|
2015-01-02 11:39:18 +00:00
|
|
|
/* block the current thread until SYS_USB_DISCONNECTED has been broadcast */
|
2003-02-07 09:41:57 +00:00
|
|
|
void usb_wait_for_disconnect(struct event_queue *q);
|
2015-01-02 11:39:18 +00:00
|
|
|
/* same as usb_wait_for_disconnect() but with a timeout, returns 1 on timeout */
|
2003-02-07 09:41:57 +00:00
|
|
|
int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks);
|
2015-01-02 11:39:18 +00:00
|
|
|
/* check whether USB is plugged, note that this is the official value which has
|
|
|
|
* been reported to the thread */
|
|
|
|
bool usb_inserted(void);
|
|
|
|
/* check whether USB is plugged, note that this is the raw hardware value */
|
|
|
|
int usb_detect(void);
|
2015-01-02 11:38:34 +00:00
|
|
|
#ifdef USB_STATUS_BY_EVENT
|
|
|
|
/* Notify USB insertion state (USB_INSERTED or USB_EXTRACTED) */
|
2009-01-14 07:01:49 +00:00
|
|
|
void usb_status_event(int current_status);
|
2015-01-02 11:38:34 +00:00
|
|
|
#endif
|
2005-06-23 02:18:29 +00:00
|
|
|
#ifdef HAVE_USB_POWER
|
2015-01-02 21:15:31 +00:00
|
|
|
/* returns whether the USB is in powered-only state */
|
|
|
|
bool usb_powered_only(void);
|
2010-06-05 10:05:27 +00:00
|
|
|
#ifdef HAVE_USB_CHARGING_ENABLE
|
|
|
|
enum {
|
2015-01-02 11:39:18 +00:00
|
|
|
USB_CHARGING_DISABLE, /* the USB code will never ask for more than 100mA */
|
|
|
|
USB_CHARGING_ENABLE, /* the code will ask for the maximum possible value */
|
|
|
|
USB_CHARGING_FORCE /* the code will always ask for 500mA */
|
2010-06-05 10:05:27 +00:00
|
|
|
};
|
2015-01-02 11:39:18 +00:00
|
|
|
/* select the USB charging mode, typically used by apps/ to reflect user setting,
|
|
|
|
* implemented by usb_core on targets with a software stack, and by target code
|
|
|
|
* on targets with a hardware stack */
|
2010-06-05 10:05:27 +00:00
|
|
|
void usb_charging_enable(int state);
|
2010-06-05 20:43:30 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
2015-01-02 11:39:18 +00:00
|
|
|
/* update the USB charging value based on the current USB state */
|
2010-06-05 20:43:30 +00:00
|
|
|
void usb_charger_update(void);
|
|
|
|
#endif /* HAVE_USBSTACK */
|
2015-01-02 11:39:18 +00:00
|
|
|
/* limit the maximum USB current the charger can draw */
|
2015-01-01 23:26:28 +00:00
|
|
|
void usb_charging_maxcurrent_change(int maxcurrent);
|
2015-01-02 11:39:18 +00:00
|
|
|
/* returns the maximum allowed USB current, based on USB charging mode and state */
|
2015-01-01 23:26:28 +00:00
|
|
|
int usb_charging_maxcurrent(void);
|
2010-06-05 20:43:30 +00:00
|
|
|
#endif /* HAVE_USB_CHARGING_ENABLE */
|
2020-11-02 13:10:15 +00:00
|
|
|
void usb_set_mode(int mode);
|
2010-06-05 20:43:30 +00:00
|
|
|
#endif /* HAVE_USB_POWER */
|
2008-02-27 19:08:30 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
2015-01-02 11:39:18 +00:00
|
|
|
/* USB driver call this function to notify that a transfer has completed */
|
2009-04-18 18:17:29 +00:00
|
|
|
void usb_signal_transfer_completion(
|
|
|
|
struct usb_transfer_completion_event_data *event_data);
|
2015-01-02 11:39:18 +00:00
|
|
|
/* notify the USB code that some important event has occurred which influences the
|
|
|
|
* USB state (like USB_NOTIFY_SET_ADDR). USB drivers should call usb_core_notify_*
|
|
|
|
* functions and not this function. */
|
2012-12-07 12:37:26 +00:00
|
|
|
void usb_signal_notify(long id, intptr_t data);
|
2015-01-02 11:39:18 +00:00
|
|
|
/* returns whether a USB_DRIVER_* is enabled (like HID, mass storage, ...) */
|
2008-02-27 19:08:30 +00:00
|
|
|
bool usb_driver_enabled(int driver);
|
2015-01-02 11:39:18 +00:00
|
|
|
/* returns whether exclusive storage is available for USB */
|
|
|
|
bool usb_exclusive_storage(void);
|
|
|
|
#endif /* HAVE_USBSTACK */
|
2008-02-27 19:08:30 +00:00
|
|
|
|
2009-01-13 16:27:35 +00:00
|
|
|
#ifdef USB_FIREWIRE_HANDLING
|
2008-02-27 19:08:30 +00:00
|
|
|
bool firewire_detect(void);
|
2009-01-22 22:05:04 +00:00
|
|
|
void usb_firewire_connect_event(void);
|
2008-02-27 19:08:30 +00:00
|
|
|
#endif
|
2003-02-07 09:41:57 +00:00
|
|
|
|
2009-10-23 13:29:19 +00:00
|
|
|
#ifdef USB_ENABLE_HID
|
2015-01-02 11:39:18 +00:00
|
|
|
/* enable or disable the HID driver */
|
2009-10-23 13:29:19 +00:00
|
|
|
void usb_set_hid(bool enable);
|
|
|
|
#endif
|
|
|
|
|
2011-09-09 16:15:35 +00:00
|
|
|
#if defined(USB_ENABLE_STORAGE) && defined(HAVE_MULTIDRIVE)
|
2015-01-02 11:39:18 +00:00
|
|
|
/* when the target has several drives, decide whether mass storage should
|
|
|
|
* skip the first drive. This is useful when the second drive is a SD card
|
|
|
|
* and the host only supports access to the first USB drive (this is very common
|
|
|
|
* in car tuners and USB speakers) */
|
2011-09-09 16:15:35 +00:00
|
|
|
void usb_set_skip_first_drive(bool skip);
|
|
|
|
#endif
|
|
|
|
|
2011-12-31 18:43:41 +00:00
|
|
|
#if !defined(SIMULATOR) && !defined(USB_NONE)
|
2015-01-02 11:39:18 +00:00
|
|
|
/* initialise the USB hardware, this is a one-time init and it should setup what
|
|
|
|
* is necessary to do proper USB detection, and it should call usb_drv_startup()
|
|
|
|
* to do the one-time initialisation of the USB driver */
|
2011-12-31 18:43:41 +00:00
|
|
|
void usb_init_device(void);
|
|
|
|
#endif
|
|
|
|
|
2003-02-07 09:41:57 +00:00
|
|
|
#endif
|