A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#ifndef _USB_H_
22#define _USB_H_
23
24#include "config.h"
25#include "kernel.h"
26#include "button.h"
27
28/** USB introduction
29 * Targets which do not have any hardware support for USB, and cannot even detect
30 * it must define USB_NONE. Otherwise, they must at least implement USB
31 * detection.
32 *
33 * USB architecture
34 * The USB code is split into several parts:
35 * - usb: manages the USB connection
36 * - usb_core: implements a software USB stack based on usb_drv
37 * - usb_drv: implements the USB protocol based on some hardware transceiver/core
38 * - usb_{hid,storage,...}: implement USB functionalities based on usb_core
39 * Note that not all those are compiled in, in particular in the case of a
40 * hardware USB stack, or when the driver doesn't support all types of transfers.
41 *
42 * Software versus hardware USB stack
43 * A very important thing to keep in mind is that there are two very different
44 * situations:
45 * - software USB stack: the device only provides a USB transceiver and the
46 * USB stack must be implemented entirely in software. In this case the target
47 * must define HAVE_USBSTACK, correctly set CONFIG_USBOTG and implement a driver
48 * for the transceiver.
49 * - hardware USB stack: the device has a dedicated chip which implements the
50 * USB stack in hardware. In this case the target must *NOT* define HAVE_USBSTACK
51 * but can still define CONFIG_USBOTG and implement a driver to enable/disable
52 * the USB hardware.
53 *
54 * USB ignore buttons
55 * In some cases, the user wants to prevent Rockbox from entering USB mode. It
56 * can do so by holding a button while inserting the cable. By default any button
57 * will prevent the USB mode from kicking-in, so targets can optionally define
58 * USBPOWER_BTN_IGNORE to a mask of buttons to ignore in this check.
59 *
60 * USB states
61 * It is important to understand that the usb code can be in one of three states:
62 * - extracted: no USB cable is plugged
63 * - powered-only: a USB cable is plugged but the USB mode will not be entered,
64 * either because no host was detected or because the user requested so.
65 * - inserted: a USB cable is plugged and the USB mode has been entered, either
66 * the software or hardware stack is running.
67 *
68 * USB exclusive mode
69 * Either in hardware or software stack, if the USB was configured to run in
70 * mass storage mode, it will require exclusive access to the disk and ask all
71 * threads to release any file handle and stop using the disks. It does so by
72 * broadcasting a SYS_USB_CONNECTED message, which threads must acknowledge using
73 * usb_acknowledge(SYS_USB_CONNECTED_ACK, ev.data). They must not access the disk
74 * until SYS_USB_DISCONNECTED is broadcast. To ease waiting, threads can call
75 * usb_wait_for_disconnect() or usb_wait_for_disconnect_w_tmo() on their waiting
76 * queue.
77 *
78 * USB detection
79 * Except when no usb code is compiled at all (USB_NONE), the usb thread keeps
80 * track of the USB insertion state, which can be either USB_INSERTED (meaning
81 * 5v is present) or USB_EXTRACTED. Each target must implement usb_detect()
82 * to report the insertion state.
83 * Targets which support insertion/extraction interrupts must define
84 * USB_STATUS_BY_EVENT and notify the thread on changes by calling
85 * usb_status_event() with the new state. Other targets must *not* define
86 * USB_STATUS_BY_EVENT and the usb thread by regularly poll the insertion state
87 * using usb_detect().
88 *
89 * USB powering & charging
90 * Device which can be powered from USB must define HAVE_USB_POWER. Note that
91 * powering doesn't imply charging (for example a AA-powered device can be
92 * powered from USB but not charged), charging sources are reported by the
93 * power subsystem (see power.h). The USB specification mandates the maximum
94 * current which can be drawn under which cirmcunstances. Device which cannot
95 * control the charge current should make sure it is always <100mA to meet the
96 * USB specification. Device with configurable charging current which support
97 * >=100mA must define HAVE_USB_CHARGING_ENABLE and implement
98 * usb_charging_maxcurrent_change() to let the usb thread control the maximum
99 * charging control.
100 * */
101
102#if defined(IPOD_COLOR) || defined(IPOD_4G) \
103 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
104#define USB_FIREWIRE_HANDLING
105#endif
106
107/* Messages from usb_tick and thread states */
108enum
109{
110 USB_SCREENDUMP = -1, /* State */
111 USB_EXTRACTED = 0, /* Event+State */
112 USB_INSERTED, /* Event+State */
113 USB_POWERED, /* State - transitional indicator if no host */
114#if (CONFIG_STORAGE & STORAGE_MMC)
115 USB_REENABLE, /* Event */
116#endif
117#ifdef HAVE_USBSTACK
118 USB_TRANSFER_COMPLETION, /* Event */
119 USB_NOTIFY_SET_ADDR, /* Event */
120 USB_NOTIFY_SET_CONFIG, /* Event */
121 USB_NOTIFY_BUS_RESET, /* Event */
122 USB_NOTIFY_CLASS_DRIVER, /* Event - notify_event() of specified class driver */
123#endif
124#ifdef USB_FIREWIRE_HANDLING
125 USB_REQUEST_REBOOT, /* Event */
126#endif
127 USB_QUIT, /* Event */
128#if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
129 USB_CHARGER_UPDATE, /* Event */
130#endif
131#ifdef HAVE_BOOTLOADER_USB_MODE
132 USB_HANDLED, /* Bootloader status code */
133#endif
134};
135
136/* Supported usb modes. */
137enum
138{
139 USB_MODE_MASS_STORAGE,
140 USB_MODE_CHARGE,
141 USB_MODE_ADB
142};
143
144#ifdef HAVE_USB_POWER
145/*allow people to define this in config-target.h if they need it*/
146#if !defined(USBPOWER_BTN_IGNORE)
147#define USBPOWER_BTN_IGNORE 0
148#endif
149
150#if defined(BOOTLOADER)
151#define USBMODE_DEFAULT USB_MODE_MASS_STORAGE
152#else
153#define USBMODE_DEFAULT USB_MODE_MASS_STORAGE
154#endif
155
156#endif
157
158#ifdef HAVE_USBSTACK
159/* USB class drivers */
160enum {
161#ifdef USB_ENABLE_STORAGE
162 USB_DRIVER_MASS_STORAGE,
163#endif
164#ifdef USB_ENABLE_SERIAL
165 USB_DRIVER_SERIAL,
166#endif
167#ifdef USB_ENABLE_CHARGING_ONLY
168 USB_DRIVER_CHARGING_ONLY,
169#endif
170#ifdef USB_ENABLE_HID
171 USB_DRIVER_HID,
172#endif
173#ifdef USB_ENABLE_AUDIO
174 USB_DRIVER_AUDIO,
175#endif
176 USB_NUM_DRIVERS
177};
178
179struct usb_transfer_completion_event_data
180{
181 unsigned char endpoint;
182 int dir;
183 int status;
184 int length;
185 void* data[2];
186};
187#endif /* HAVE_USBSTACK */
188
189/* initialise the usb code and thread */
190void usb_init(void) INIT_ATTR;
191/* target must implement this to enable/disable the usb transceiver/core */
192void usb_enable(bool on);
193/* called after host has been detected */
194void usb_attach(void);
195/* enable usb detection monitoring; before this function is called, all usb
196 * detection changes are ignored */
197void usb_start_monitoring(void) INIT_ATTR;
198void usb_close(void);
199/* acknowledge usb connection, typically with SYS_USB_CONNECTED_ACK */
200void usb_acknowledge(long id, intptr_t seqnum);
201/* block the current thread until SYS_USB_DISCONNECTED has been broadcast */
202void usb_wait_for_disconnect(struct event_queue *q);
203/* same as usb_wait_for_disconnect() but with a timeout, returns 1 on timeout */
204int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks);
205/* check whether USB is plugged, note that this is the official value which has
206 * been reported to the thread */
207bool usb_inserted(void);
208/* check whether USB is plugged, note that this is the raw hardware value */
209int usb_detect(void);
210#ifdef USB_STATUS_BY_EVENT
211/* Notify USB insertion state (USB_INSERTED or USB_EXTRACTED) */
212void usb_status_event(int current_status);
213#endif
214#ifdef HAVE_USB_POWER
215/* returns whether the USB is in powered-only state */
216bool usb_powered_only(void);
217#ifdef HAVE_USB_CHARGING_ENABLE
218enum {
219 USB_CHARGING_DISABLE, /* the USB code will never ask for more than 100mA */
220 USB_CHARGING_ENABLE, /* the code will ask for the maximum possible value */
221 USB_CHARGING_FORCE /* the code will always ask for 500mA */
222};
223/* select the USB charging mode, typically used by apps/ to reflect user setting,
224 * implemented by usb_core on targets with a software stack, and by target code
225 * on targets with a hardware stack */
226void usb_charging_enable(int state);
227#ifdef HAVE_USBSTACK
228/* update the USB charging value based on the current USB state */
229void usb_charger_update(void);
230#endif /* HAVE_USBSTACK */
231/* limit the maximum USB current the charger can draw */
232void usb_charging_maxcurrent_change(int maxcurrent);
233/* returns the maximum allowed USB current, based on USB charging mode and state */
234int usb_charging_maxcurrent(void);
235#endif /* HAVE_USB_CHARGING_ENABLE */
236void usb_set_mode(int mode);
237#endif /* HAVE_USB_POWER */
238#ifdef HAVE_USBSTACK
239/* USB driver call this function to notify that a transfer has completed */
240void usb_signal_transfer_completion(
241 struct usb_transfer_completion_event_data *event_data);
242
243/* Clear all signaled transfer completion events from event queue */
244void usb_clear_pending_transfer_completion_events(void);
245
246/* notify the USB code that some important event has occurred which influences the
247 * USB state (like USB_NOTIFY_SET_ADDR). USB drivers should call usb_core_notify_*
248 * functions and not this function.
249 * for USB_NOTIFY_CLASS_DRIVER, use usb_signal_class_notify() instead */
250void usb_signal_notify(long id, intptr_t data);
251
252/* wrapper for usb_signal_notify(USB_NOTIFY_CLASS_DRIVER)
253 * class_num: target driver. USB_DRIVER_*
254 * data: optional data. note that its upper 8 bits will be masked */
255static inline void usb_signal_class_notify(int8_t class_num, uint32_t data) {
256 usb_signal_notify(USB_NOTIFY_CLASS_DRIVER, class_num << 24 | (data & 0x00ffffff));
257}
258
259/* returns whether a USB_DRIVER_* is enabled (like HID, mass storage, ...) */
260bool usb_driver_enabled(int driver);
261/* returns whether exclusive storage is available for USB */
262bool usb_exclusive_storage(void);
263#endif /* HAVE_USBSTACK */
264
265/* broadcast usb insertion event to enable exclusive storage */
266void usb_request_exclusive_storage(void);
267/* finish exclusive storage access if enabled and mount volumes */
268void usb_release_exclusive_storage(void);
269
270#ifdef USB_FIREWIRE_HANDLING
271bool firewire_detect(void);
272void usb_firewire_connect_event(void);
273#endif
274
275#ifdef USB_ENABLE_HID
276/* enable or disable the HID driver */
277void usb_set_hid(bool enable);
278#endif
279
280#ifdef USB_ENABLE_AUDIO
281void usb_set_audio(int value);
282#endif
283
284#if defined(USB_ENABLE_STORAGE) && defined(HAVE_MULTIDRIVE)
285/* when the target has several drives, decide whether mass storage should
286 * skip the first drive. This is useful when the second drive is a SD card
287 * and the host only supports access to the first USB drive (this is very common
288 * in car tuners and USB speakers) */
289void usb_set_skip_first_drive(bool skip);
290#endif
291
292#if !defined(SIMULATOR) && !defined(USB_NONE)
293/* initialise the USB hardware, this is a one-time init and it should setup what
294 * is necessary to do proper USB detection, and it should call usb_drv_startup()
295 * to do the one-time initialisation of the USB driver */
296void usb_init_device(void);
297#endif
298
299#endif