Loading...
Searching...
No Matches
usbus.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2018 Koen Zandberg
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
21
22#include <stdint.h>
23#include <stdlib.h>
24
25#include "clist.h"
26#include "event.h"
27#include "sched.h"
28#include "modules.h"
29#include "msg.h"
30#include "thread.h"
31
32#include "usb.h"
33#include "periph/usbdev.h"
34#include "usb/descriptor.h"
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
48#ifndef USBUS_STACKSIZE
49#define USBUS_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
50#endif
51
55#ifndef USBUS_PRIO
56#define USBUS_PRIO (THREAD_PRIORITY_MAIN - 6)
57#endif
58
66#ifndef CONFIG_USBUS_AUTO_ATTACH
67/* Check for Kconfig usage */
68#if !IS_ACTIVE(CONFIG_MODULE_USBUS)
69#define CONFIG_USBUS_AUTO_ATTACH 1
70#endif
71#endif
72
79#ifndef CONFIG_USBUS_MSC_AUTO_MTD
80#define CONFIG_USBUS_MSC_AUTO_MTD 1
81#endif
82
90#if IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_8)
91#define CONFIG_USBUS_EP0_SIZE 8
92#elif IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_16)
93#define CONFIG_USBUS_EP0_SIZE 16
94#elif IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_32)
95#define CONFIG_USBUS_EP0_SIZE 32
96#elif IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_64)
97#define CONFIG_USBUS_EP0_SIZE 64
98#endif
99
100#ifndef CONFIG_USBUS_EP0_SIZE
101#define CONFIG_USBUS_EP0_SIZE 64
102#endif
104
108#define USBUS_TNAME "usbus"
109
117#define USBUS_THREAD_FLAG_USBDEV (0x02)
118#define USBUS_THREAD_FLAG_USBDEV_EP (0x04)
121
127#define USBUS_HANDLER_FLAG_RESET (0x0001)
128#define USBUS_HANDLER_FLAG_SOF (0x0002)
129#define USBUS_HANDLER_FLAG_SUSPEND (0x0004)
130#define USBUS_HANDLER_FLAG_RESUME (0x0008)
131#define USBUS_HANDLER_FLAG_TR_STALL (0x0020)
133
139
144#define USBUS_URB_FLAG_AUTO_ZLP (0x0001)
145
150#define USBUS_URB_FLAG_NEEDS_ZLP (0x1000)
151
156#define USBUS_URB_FLAG_CANCELLED (0x2000)
158
168
177
188
202
206typedef struct usbus_string {
208 const char *str;
209 uint16_t idx;
211
215typedef struct usbus usbus_t;
216
221
229
233typedef struct {
244 size_t (*fmt_pre_descriptor)(usbus_t *usbus, void *arg);
245
256 size_t (*fmt_post_descriptor)(usbus_t *usbus, void *arg);
257 union {
271 size_t (*get_descriptor_len)(usbus_t *usbus, void *arg);
280 size_t fixed_len;
281 } len;
284
300
304typedef struct usbus_endpoint {
311#ifdef MODULE_USBUS_URB
312 clist_node_t urb_list;
313#endif
314 uint16_t maxpacketsize;
315 uint8_t interval;
316 bool active;
318 bool halted;
320
324typedef struct usbus_urb {
326 uint32_t flags;
327 uint8_t *buf;
328 size_t len;
329 size_t transferred;
331
345
365
369typedef struct usbus_handler_driver {
370
380 void (*init)(usbus_t * usbus, struct usbus_handler *handler);
381
391 void (*event_handler)(usbus_t * usbus, struct usbus_handler *handler,
393
404 void (*transfer_handler)(usbus_t * usbus, struct usbus_handler *handler,
406
422 int (*control_handler)(usbus_t * usbus, struct usbus_handler *handler,
424 usb_setup_t *request);
426
440
473
481{
483}
484
495 const char *str);
496
506
515
531 usb_ep_type_t type,
532 usb_ep_dir_t dir);
533
550 usb_ep_type_t type, usb_ep_dir_t dir,
551 size_t len);
552
561
573
581
589
597
607void usbus_create(char *stack, int stacksize, char priority,
608 const char *name, usbus_t *usbus);
609
625static inline void usbus_urb_init(usbus_urb_t *urb,
626 uint8_t *buf,
627 size_t len,
628 uint32_t flags)
629{
630 urb->buf = buf;
631 urb->len = len;
632 urb->flags = flags;
633 urb->transferred = 0;
634}
635
646
668
676
684
693{
694 ep->active = true;
695}
696
705{
706 ep->active = false;
707}
708
715static inline void usbus_handler_set_flag(usbus_handler_t *handler,
716 uint32_t flag)
717{
718 handler->flags |= flag;
719}
720
727static inline void usbus_handler_remove_flag(usbus_handler_t *handler,
728 uint32_t flag)
729{
730 handler->flags &= ~flag;
731}
732
741static inline bool usbus_handler_isset_flag(usbus_handler_t *handler,
742 uint32_t flag)
743{
744 return handler->flags & flag;
745}
746
753static inline void usbus_urb_set_flag(usbus_urb_t *urb,
754 uint32_t flag)
755{
756 urb->flags |= flag;
757}
758
765static inline void usbus_urb_remove_flag(usbus_urb_t *urb,
766 uint32_t flag)
767{
768 urb->flags &= ~flag;
769}
770
779static inline bool usbus_urb_isset_flag(usbus_urb_t *urb,
780 uint32_t flag)
781{
782 return urb->flags & flag;
783}
784
785#ifdef __cplusplus
786}
787#endif
Circular linked list.
list_node_t clist_node_t
List node structure.
Definition clist.h:104
Definitions for USB protocol messages.
#define USBDEV_NUM_ENDPOINTS
Number of USB OTG FS endpoints including EP0.
Definition periph_cpu.h:699
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:134
struct usbdev_ep usbdev_ep_t
usbdev_ep_t forward declaration
Definition usbdev.h:96
struct usbdev usbdev_t
usbdev_t forward declaration
Definition usbdev.h:91
void event_post(event_queue_t *queue, event_t *event)
Queue an event.
struct PTRTAG event_queue_t
event queue structure
struct event event_t
event structure forward declaration
Definition event.h:132
static void usbus_urb_remove_flag(usbus_urb_t *urb, uint32_t flag)
disable an URB flag
Definition usbus.h:765
void usbus_add_interface_alt(usbus_interface_t *iface, usbus_interface_alt_t *alt)
Add alternate settings to a given interface.
static void usbus_handler_remove_flag(usbus_handler_t *handler, uint32_t flag)
disable a specific handler flag
Definition usbus.h:727
struct usbus_interface_alt usbus_interface_alt_t
USBUS interface alternative setting.
usbus_event_transfer_t
USB endpoint transfer status events.
Definition usbus.h:172
struct usbus_handler_driver usbus_handler_driver_t
USBUS event handler function pointers.
usbus_descr_len_type_t
descriptor length types for USB descriptor generators
Definition usbus.h:225
static void usbus_urb_init(usbus_urb_t *urb, uint8_t *buf, size_t len, uint32_t flags)
Initialize a new URB.
Definition usbus.h:625
struct usbus_string usbus_string_t
USBUS string type.
struct usbus_urb usbus_urb_t
USBUS USB request/response block.
static bool usbus_handler_isset_flag(usbus_handler_t *handler, uint32_t flag)
check if a specific handler flag is set
Definition usbus.h:741
void usbus_init(usbus_t *usbus, usbdev_t *usbdev)
Initialize an USBUS context.
void usbus_endpoint_clear_halt(usbus_endpoint_t *ep)
Clear the halt condition on an endpoint.
int usbus_urb_cancel(usbus_t *usbus, usbus_endpoint_t *endpoint, usbus_urb_t *urb)
Cancel and already queued URB.
static void usbus_urb_set_flag(usbus_urb_t *urb, uint32_t flag)
enable an URB flag
Definition usbus.h:753
struct usbus_endpoint usbus_endpoint_t
USBUS endpoint context.
size_t usbus_max_interrupt_endpoint_size(usbus_t *usbus)
Get the maximum supported interrupt endpoint transfer size based on the enumeration speed.
uint16_t usbus_add_interface(usbus_t *usbus, usbus_interface_t *iface)
Add an interface to the USBUS thread context.
usbus_event_usb_t
USB handler events.
Definition usbus.h:162
static void usbus_event_post(usbus_t *usbus, event_t *event)
Submit an event to the usbus thread.
Definition usbus.h:480
size_t usbus_max_bulk_endpoint_size(usbus_t *usbus)
Get the maximum supported bulk endpoint transfer size based on the enumeration speed.
usbus_endpoint_t * usbus_interface_find_endpoint(usbus_interface_t *interface, usb_ep_type_t type, usb_ep_dir_t dir)
Find an endpoint from an interface based on the endpoint properties.
static void usbus_handler_set_flag(usbus_handler_t *handler, uint32_t flag)
enable a specific handler flag
Definition usbus.h:715
usbus_control_request_state_t
USBUS control request state machine.
Definition usbus.h:192
struct usbus_descr_gen usbus_descr_gen_t
USBUS descriptor generator.
usbus_endpoint_t * usbus_add_endpoint(usbus_t *usbus, usbus_interface_t *iface, usb_ep_type_t type, usb_ep_dir_t dir, size_t len)
Add an endpoint to the specified interface.
void usbus_create(char *stack, int stacksize, char priority, const char *name, usbus_t *usbus)
Create and start the USBUS thread.
uint16_t usbus_add_string_descriptor(usbus_t *usbus, usbus_string_t *desc, const char *str)
Add a string descriptor to the USBUS thread context.
struct usbus_interface usbus_interface_t
USBUS interface.
struct usbus_handler usbus_handler_t
USBUS event handler forward declaration.
Definition usbus.h:220
void usbus_endpoint_halt(usbus_endpoint_t *ep)
Set the halt condition on an endpoint.
struct usbus usbus_t
USBUS context forward declaration.
Definition usbus.h:215
static bool usbus_urb_isset_flag(usbus_urb_t *urb, uint32_t flag)
check if an URB flag is set
Definition usbus.h:779
static void usbus_enable_endpoint(usbus_endpoint_t *ep)
Enable an endpoint.
Definition usbus.h:692
void usbus_add_conf_descriptor(usbus_t *usbus, usbus_descr_gen_t *descr_gen)
Add a generator for generating additional top level USB descriptor content.
usbus_state_t
state machine states for the global USBUS thread
Definition usbus.h:181
void usbus_urb_submit(usbus_t *usbus, usbus_endpoint_t *endpoint, usbus_urb_t *urb)
Submit an URB to an endpoint.
static void usbus_disable_endpoint(usbus_endpoint_t *ep)
Disable an endpoint.
Definition usbus.h:704
void usbus_register_event_handler(usbus_t *usbus, usbus_handler_t *handler)
Add an event handler to the USBUS context.
@ USBUS_EVENT_TRANSFER_COMPLETE
Transfer successfully completed.
Definition usbus.h:173
@ USBUS_EVENT_TRANSFER_STALL
Transfer stall replied by peripheral.
Definition usbus.h:175
@ USBUS_EVENT_TRANSFER_FAIL
Transfer nack replied by peripheral.
Definition usbus.h:174
@ USBUS_DESCR_LEN_FIXED
Descriptor always generates a fixed length.
Definition usbus.h:226
@ USBUS_DESCR_LEN_FUNC
Descriptor length is calculated by a function.
Definition usbus.h:227
@ USBUS_EVENT_USB_SOF
USB start of frame received.
Definition usbus.h:164
@ USBUS_EVENT_USB_RESUME
USB resume condition detected.
Definition usbus.h:166
@ USBUS_EVENT_USB_RESET
USB reset event.
Definition usbus.h:163
@ USBUS_EVENT_USB_SUSPEND
USB suspend condition detected.
Definition usbus.h:165
@ USBUS_CONTROL_REQUEST_STATE_INACK
Expecting a zero-length ack IN request from the host.
Definition usbus.h:199
@ USBUS_CONTROL_REQUEST_STATE_OUTACK
Expecting a zero-length ack OUT request from the host.
Definition usbus.h:196
@ USBUS_CONTROL_REQUEST_STATE_OUTDATA
Data OUT expected.
Definition usbus.h:198
@ USBUS_CONTROL_REQUEST_STATE_INDATA
Request received with expected DATA IN stage.
Definition usbus.h:194
@ USBUS_CONTROL_REQUEST_STATE_READY
Ready for new control request.
Definition usbus.h:193
@ USBUS_STATE_RESET
Reset condition received.
Definition usbus.h:183
@ USBUS_STATE_CONFIGURED
Peripheral is configured.
Definition usbus.h:185
@ USBUS_STATE_ADDR
Address configured.
Definition usbus.h:184
@ USBUS_STATE_DISCONNECT
Device is disconnected from the host.
Definition usbus.h:182
@ USBUS_STATE_SUSPEND
Peripheral is suspended by the host.
Definition usbus.h:186
usb_ep_dir_t
USB endpoint directions.
Definition usb.h:242
usb_ep_type_t
USB endpoint types.
Definition usb.h:231
Common macros and compiler attributes/pragmas configuration.
event structure
Definition event.h:142
USB setup packet (USB 2.0 spec table 9-2)
Definition descriptor.h:205
usbdev device descriptor
Definition usbdev.h:244
USBUS descriptor generator function pointers.
Definition usbus.h:233
size_t(* get_descriptor_len)(usbus_t *usbus, void *arg)
USBUS generic descriptor generator generated length.
Definition usbus.h:271
size_t(* fmt_post_descriptor)(usbus_t *usbus, void *arg)
function pointer to format the descriptor content of this descriptor generator.
Definition usbus.h:256
usbus_descr_len_type_t len_type
Either USBUS_DESCR_LEN_FIXED or USBUS_DESCR_LEN_FUNC.
Definition usbus.h:282
size_t(* fmt_pre_descriptor)(usbus_t *usbus, void *arg)
function pointer to format the descriptor content of this descriptor generator.
Definition usbus.h:244
size_t fixed_len
Fixed total length of the generated descriptors.
Definition usbus.h:280
USBUS descriptor generator.
Definition usbus.h:294
void * arg
Extra context argument for the descriptor functions.
Definition usbus.h:297
struct usbus_descr_gen * next
ptr to the next descriptor generator
Definition usbus.h:295
const usbus_descr_gen_funcs_t * funcs
Function pointers.
Definition usbus.h:296
USBUS endpoint context.
Definition usbus.h:304
uint16_t maxpacketsize
Max packet size of this endpoint.
Definition usbus.h:314
bool active
If the endpoint should be activated after reset.
Definition usbus.h:316
usbdev_ep_t * ep
ptr to the matching usbdev endpoint
Definition usbus.h:310
uint8_t interval
Poll interval for interrupt endpoints.
Definition usbus.h:315
struct usbus_endpoint * next
Next endpoint in the usbus_interface_t list of endpoints.
Definition usbus.h:305
usbus_descr_gen_t * descr_gen
Linked list of optional additional descriptor generators.
Definition usbus.h:308
bool halted
Endpoint is halted.
Definition usbus.h:318
USBUS event handler function pointers.
Definition usbus.h:369
void(* init)(usbus_t *usbus, struct usbus_handler *handler)
Initialize the event handler.
Definition usbus.h:380
void(* transfer_handler)(usbus_t *usbus, struct usbus_handler *handler, usbdev_ep_t *ep, usbus_event_transfer_t event)
transfer handler function
Definition usbus.h:404
void(* event_handler)(usbus_t *usbus, struct usbus_handler *handler, usbus_event_usb_t event)
event handler function
Definition usbus.h:391
int(* control_handler)(usbus_t *usbus, struct usbus_handler *handler, usbus_control_request_state_t state, usb_setup_t *request)
control request handler function
Definition usbus.h:422
USBUS handler struct.
Definition usbus.h:432
struct usbus_handler * next
List of handlers (to be used by usbus_t)
Definition usbus.h:433
uint32_t flags
Report flags.
Definition usbus.h:438
const usbus_handler_driver_t * driver
driver for this handler
Definition usbus.h:435
usbus_interface_t * iface
Interface this handler belongs to.
Definition usbus.h:436
USBUS interface alternative setting.
Definition usbus.h:337
usbus_descr_gen_t * descr_gen
Linked list of optional additional descriptor generators.
Definition usbus.h:339
struct usbus_interface_alt * next
Next alternative setting.
Definition usbus.h:338
usbus_endpoint_t * ep
List of associated endpoints for this alternative setting.
Definition usbus.h:341
usbus_string_t * descr
Descriptor string.
Definition usbus.h:343
USBUS interface.
Definition usbus.h:349
uint8_t protocol
USB interface protocol.
Definition usbus.h:363
struct usbus_interface_alt * alts
List of alt settings.
Definition usbus.h:356
uint8_t subclass
USB interface subclass.
Definition usbus.h:362
usbus_endpoint_t * ep
Linked list of endpoints belonging to this interface.
Definition usbus.h:354
struct usbus_interface * next
Next interface (set by USBUS during registration)
Definition usbus.h:350
usbus_descr_gen_t * descr_gen
Linked list of optional additional descriptor generators.
Definition usbus.h:352
usbus_handler_t * handler
Handlers for this interface.
Definition usbus.h:357
uint16_t idx
Interface index, (set by USBUS during registration.
Definition usbus.h:359
usbus_string_t * descr
Descriptor string.
Definition usbus.h:358
USBUS string type.
Definition usbus.h:206
const char * str
C string to use as content.
Definition usbus.h:208
struct usbus_string * next
Ptr to the next registered string.
Definition usbus.h:207
uint16_t idx
USB string index.
Definition usbus.h:209
USBUS USB request/response block.
Definition usbus.h:324
uint8_t * buf
Pointer to the (aligned) buffer.
Definition usbus.h:327
clist_node_t list
clist block in the queue
Definition usbus.h:325
size_t len
Length of the data.
Definition usbus.h:328
uint32_t flags
Transfer flags.
Definition usbus.h:326
size_t transferred
amount transferred, only valid for OUT
Definition usbus.h:329
USBUS context struct.
Definition usbus.h:444
usbus_state_t state
Current state.
Definition usbus.h:462
usbus_state_t pstate
state to recover to from suspend
Definition usbus.h:463
usbus_endpoint_t ep_out[USBDEV_NUM_ENDPOINTS]
USBUS OUT endpoints.
Definition usbus.h:449
usbus_handler_t * handlers
List of event callback handlers.
Definition usbus.h:458
uint8_t addr
Address of the USB peripheral.
Definition usbus.h:464
bool wakeup_enabled
Remote wakeup device feature status.
Definition usbus.h:465
char serial_str[2 *CONFIG_USB_SERIAL_BYTE_LENGTH+1]
Hex representation of the device serial number.
Definition usbus.h:470
kernel_pid_t pid
PID of the usb manager's thread.
Definition usbus.h:460
usbus_endpoint_t ep_in[USBDEV_NUM_ENDPOINTS]
USBUS IN endpoints.
Definition usbus.h:450
usbus_descr_gen_t * descr_gen
Linked list of top level descriptor generators.
Definition usbus.h:454
usbus_handler_t * control
Ptr to the control endpoint handler.
Definition usbus.h:453
usbus_string_t serial
serial string
Definition usbus.h:448
usbus_string_t product
Product string.
Definition usbus.h:446
usbus_string_t manuf
Manufacturer string.
Definition usbus.h:445
usbus_string_t * strings
List of descriptor strings.
Definition usbus.h:456
usbdev_t * dev
usb phy device of the usb manager
Definition usbus.h:452
uint16_t str_idx
Number of strings registered.
Definition usbus.h:461
usbus_interface_t * iface
List of USB interfaces.
Definition usbus.h:457
event_queue_t queue
Event queue.
Definition usbus.h:451
uint32_t ep_events
bitflags with endpoint event state
Definition usbus.h:459
usbus_string_t config
Configuration string.
Definition usbus.h:447
Definition of global compile time configuration options.
Definitions low-level USB driver interface.