Loading...
Searching...
No Matches
usbus.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018 Koen Zandberg
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
23#ifndef USB_USBUS_H
24#define USB_USBUS_H
25
26#include <stdint.h>
27#include <stdlib.h>
28
29#include "clist.h"
30#include "event.h"
31#include "sched.h"
32#include "modules.h"
33#include "msg.h"
34#include "thread.h"
35
36#include "usb.h"
37#include "periph/usbdev.h"
38#include "usb/descriptor.h"
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
52#ifndef USBUS_STACKSIZE
53#define USBUS_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
54#endif
55
59#ifndef USBUS_PRIO
60#define USBUS_PRIO (THREAD_PRIORITY_MAIN - 6)
61#endif
62
70#ifndef CONFIG_USBUS_AUTO_ATTACH
71/* Check for Kconfig usage */
72#if !IS_ACTIVE(CONFIG_MODULE_USBUS)
73#define CONFIG_USBUS_AUTO_ATTACH 1
74#endif
75#endif
76
83#ifndef CONFIG_USBUS_MSC_AUTO_MTD
84#define CONFIG_USBUS_MSC_AUTO_MTD 1
85#endif
86
94#if IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_8)
95#define CONFIG_USBUS_EP0_SIZE 8
96#elif IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_16)
97#define CONFIG_USBUS_EP0_SIZE 16
98#elif IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_32)
99#define CONFIG_USBUS_EP0_SIZE 32
100#elif IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_64)
101#define CONFIG_USBUS_EP0_SIZE 64
102#endif
103
104#ifndef CONFIG_USBUS_EP0_SIZE
105#define CONFIG_USBUS_EP0_SIZE 64
106#endif
112#define USBUS_TNAME "usbus"
113
121#define USBUS_THREAD_FLAG_USBDEV (0x02)
122#define USBUS_THREAD_FLAG_USBDEV_EP (0x04)
131#define USBUS_HANDLER_FLAG_RESET (0x0001)
132#define USBUS_HANDLER_FLAG_SOF (0x0002)
133#define USBUS_HANDLER_FLAG_SUSPEND (0x0004)
134#define USBUS_HANDLER_FLAG_RESUME (0x0008)
135#define USBUS_HANDLER_FLAG_TR_STALL (0x0020)
148#define USBUS_URB_FLAG_AUTO_ZLP (0x0001)
149
154#define USBUS_URB_FLAG_NEEDS_ZLP (0x1000)
155
160#define USBUS_URB_FLAG_CANCELLED (0x2000)
172
181
192
206
210typedef struct usbus_string {
212 const char *str;
213 uint16_t idx;
215
219typedef struct usbus usbus_t;
220
225
233
237typedef struct {
248 size_t (*fmt_pre_descriptor)(usbus_t *usbus, void *arg);
249
260 size_t (*fmt_post_descriptor)(usbus_t *usbus, void *arg);
261 union {
275 size_t (*get_descriptor_len)(usbus_t *usbus, void *arg);
284 size_t fixed_len;
285 } len;
288
304
308typedef struct usbus_endpoint {
315#ifdef MODULE_USBUS_URB
316 clist_node_t urb_list;
317#endif
318 uint16_t maxpacketsize;
319 uint8_t interval;
320 bool active;
322 bool halted;
324
328typedef struct usbus_urb {
330 uint32_t flags;
331 uint8_t *buf;
332 size_t len;
333 size_t transferred;
335
349
369
373typedef struct usbus_handler_driver {
374
384 void (*init)(usbus_t * usbus, struct usbus_handler *handler);
385
395 void (*event_handler)(usbus_t * usbus, struct usbus_handler *handler,
397
408 void (*transfer_handler)(usbus_t * usbus, struct usbus_handler *handler,
410
426 int (*control_handler)(usbus_t * usbus, struct usbus_handler *handler,
428 usb_setup_t *request);
430
444
477
485{
487}
488
499 const char *str);
500
510
519
535 usb_ep_type_t type,
536 usb_ep_dir_t dir);
537
554 usb_ep_type_t type, usb_ep_dir_t dir,
555 size_t len);
556
565
577
585
593
601
611void usbus_create(char *stack, int stacksize, char priority,
612 const char *name, usbus_t *usbus);
613
629static inline void usbus_urb_init(usbus_urb_t *urb,
630 uint8_t *buf,
631 size_t len,
632 uint32_t flags)
633{
634 urb->buf = buf;
635 urb->len = len;
636 urb->flags = flags;
637 urb->transferred = 0;
638}
639
650
672
680
688
697{
698 ep->active = true;
699}
700
709{
710 ep->active = false;
711}
712
719static inline void usbus_handler_set_flag(usbus_handler_t *handler,
720 uint32_t flag)
721{
722 handler->flags |= flag;
723}
724
731static inline void usbus_handler_remove_flag(usbus_handler_t *handler,
732 uint32_t flag)
733{
734 handler->flags &= ~flag;
735}
736
745static inline bool usbus_handler_isset_flag(usbus_handler_t *handler,
746 uint32_t flag)
747{
748 return handler->flags & flag;
749}
750
757static inline void usbus_urb_set_flag(usbus_urb_t *urb,
758 uint32_t flag)
759{
760 urb->flags |= flag;
761}
762
769static inline void usbus_urb_remove_flag(usbus_urb_t *urb,
770 uint32_t flag)
771{
772 urb->flags &= ~flag;
773}
774
783static inline bool usbus_urb_isset_flag(usbus_urb_t *urb,
784 uint32_t flag)
785{
786 return urb->flags & flag;
787}
788
789#ifdef __cplusplus
790}
791#endif
792#endif /* USB_USBUS_H */
Circular linked list.
Definitions for USB protocol messages.
#define USBDEV_NUM_ENDPOINTS
Number of USB OTG FS endpoints including EP0.
Definition periph_cpu.h:709
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:139
void event_post(event_queue_t *queue, event_t *event)
Queue an event.
static void usbus_urb_remove_flag(usbus_urb_t *urb, uint32_t flag)
disable an URB flag
Definition usbus.h:769
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:731
struct usbus_interface_alt usbus_interface_alt_t
USBUS interface alternative setting.
usbus_event_transfer_t
USB endpoint transfer status events.
Definition usbus.h:176
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:229
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:629
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:745
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:757
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:166
static void usbus_event_post(usbus_t *usbus, event_t *event)
Submit an event to the usbus thread.
Definition usbus.h:484
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:719
usbus_control_request_state_t
USBUS control request state machine.
Definition usbus.h:196
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.
void usbus_endpoint_halt(usbus_endpoint_t *ep)
Set the halt condition on an endpoint.
static bool usbus_urb_isset_flag(usbus_urb_t *urb, uint32_t flag)
check if an URB flag is set
Definition usbus.h:783
static void usbus_enable_endpoint(usbus_endpoint_t *ep)
Enable an endpoint.
Definition usbus.h:696
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:185
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:708
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:177
@ USBUS_EVENT_TRANSFER_STALL
Transfer stall replied by peripheral.
Definition usbus.h:179
@ USBUS_EVENT_TRANSFER_FAIL
Transfer nack replied by peripheral.
Definition usbus.h:178
@ USBUS_DESCR_LEN_FIXED
Descriptor always generates a fixed length.
Definition usbus.h:230
@ USBUS_DESCR_LEN_FUNC
Descriptor length is calculated by a function.
Definition usbus.h:231
@ USBUS_EVENT_USB_SOF
USB start of frame received
Definition usbus.h:168
@ USBUS_EVENT_USB_RESUME
USB resume condition detected
Definition usbus.h:170
@ USBUS_EVENT_USB_RESET
USB reset event
Definition usbus.h:167
@ USBUS_EVENT_USB_SUSPEND
USB suspend condition detected.
Definition usbus.h:169
@ USBUS_CONTROL_REQUEST_STATE_INACK
Expecting a zero-length ack IN request from the host.
Definition usbus.h:203
@ USBUS_CONTROL_REQUEST_STATE_OUTACK
Expecting a zero-length ack OUT request from the host.
Definition usbus.h:200
@ USBUS_CONTROL_REQUEST_STATE_OUTDATA
Data OUT expected.
Definition usbus.h:202
@ USBUS_CONTROL_REQUEST_STATE_INDATA
Request received with expected DATA IN stage.
Definition usbus.h:198
@ USBUS_CONTROL_REQUEST_STATE_READY
Ready for new control request.
Definition usbus.h:197
@ USBUS_STATE_RESET
Reset condition received.
Definition usbus.h:187
@ USBUS_STATE_CONFIGURED
Peripheral is configured.
Definition usbus.h:189
@ USBUS_STATE_ADDR
Address configured.
Definition usbus.h:188
@ USBUS_STATE_DISCONNECT
Device is disconnected from the host.
Definition usbus.h:186
@ USBUS_STATE_SUSPEND
Peripheral is suspended by the host.
Definition usbus.h:190
usb_ep_dir_t
USB endpoint directions.
Definition usb.h:245
usb_ep_type_t
USB endpoint types.
Definition usb.h:234
Common macros and compiler attributes/pragmas configuration.
Scheduler API definition.
event queue structure
Definition event.h:156
event structure
Definition event.h:148
List node structure.
Definition list.h:40
USB setup packet (USB 2.0 spec table 9-2)
Definition descriptor.h:207
usbdev endpoint descriptor
Definition usbdev.h:259
usbdev device descriptor
Definition usbdev.h:247
USBUS descriptor generator function pointers.
Definition usbus.h:237
usbus_descr_len_type_t len_type
Either USBUS_DESCR_LEN_FIXED or USBUS_DESCR_LEN_FUNC.
Definition usbus.h:286
size_t fixed_len
Fixed total length of the generated descriptors.
Definition usbus.h:284
USBUS descriptor generator.
Definition usbus.h:298
void * arg
Extra context argument for the descriptor functions.
Definition usbus.h:301
struct usbus_descr_gen * next
ptr to the next descriptor generator
Definition usbus.h:299
const usbus_descr_gen_funcs_t * funcs
Function pointers.
Definition usbus.h:300
USBUS endpoint context.
Definition usbus.h:308
uint16_t maxpacketsize
Max packet size of this endpoint.
Definition usbus.h:318
bool active
If the endpoint should be activated after reset.
Definition usbus.h:320
usbdev_ep_t * ep
ptr to the matching usbdev endpoint
Definition usbus.h:314
uint8_t interval
Poll interval for interrupt endpoints.
Definition usbus.h:319
struct usbus_endpoint * next
Next endpoint in the usbus_interface_t list of endpoints.
Definition usbus.h:309
usbus_descr_gen_t * descr_gen
Linked list of optional additional descriptor generators.
Definition usbus.h:312
bool halted
Endpoint is halted.
Definition usbus.h:322
USBUS event handler function pointers.
Definition usbus.h:373
void(* init)(usbus_t *usbus, struct usbus_handler *handler)
Initialize the event handler.
Definition usbus.h:384
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:408
void(* event_handler)(usbus_t *usbus, struct usbus_handler *handler, usbus_event_usb_t event)
event handler function
Definition usbus.h:395
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:426
USBUS handler struct.
Definition usbus.h:436
struct usbus_handler * next
List of handlers (to be used by usbus_t)
Definition usbus.h:437
uint32_t flags
Report flags.
Definition usbus.h:442
const usbus_handler_driver_t * driver
driver for this handler
Definition usbus.h:439
usbus_interface_t * iface
Interface this handler belongs to.
Definition usbus.h:440
USBUS interface alternative setting.
Definition usbus.h:341
usbus_descr_gen_t * descr_gen
Linked list of optional additional descriptor generators.
Definition usbus.h:343
struct usbus_interface_alt * next
Next alternative setting.
Definition usbus.h:342
usbus_endpoint_t * ep
List of associated endpoints for this alternative setting.
Definition usbus.h:345
usbus_string_t * descr
Descriptor string.
Definition usbus.h:347
USBUS interface.
Definition usbus.h:353
uint8_t protocol
USB interface protocol
Definition usbus.h:367
struct usbus_interface_alt * alts
List of alt settings
Definition usbus.h:360
uint8_t subclass
USB interface subclass
Definition usbus.h:366
usbus_endpoint_t * ep
Linked list of endpoints belonging to this interface
Definition usbus.h:358
struct usbus_interface * next
Next interface (set by USBUS during registration)
Definition usbus.h:354
usbus_descr_gen_t * descr_gen
Linked list of optional additional descriptor generators.
Definition usbus.h:356
usbus_handler_t * handler
Handlers for this interface
Definition usbus.h:361
uint16_t idx
Interface index, (set by USBUS during registration
Definition usbus.h:363
usbus_string_t * descr
Descriptor string
Definition usbus.h:362
USBUS string type.
Definition usbus.h:210
const char * str
C string to use as content
Definition usbus.h:212
struct usbus_string * next
Ptr to the next registered string.
Definition usbus.h:211
uint16_t idx
USB string index
Definition usbus.h:213
USBUS USB request/response block.
Definition usbus.h:328
uint8_t * buf
Pointer to the (aligned) buffer.
Definition usbus.h:331
clist_node_t list
clist block in the queue
Definition usbus.h:329
size_t len
Length of the data.
Definition usbus.h:332
uint32_t flags
Transfer flags.
Definition usbus.h:330
size_t transferred
amount transferred, only valid for OUT
Definition usbus.h:333
USBUS context struct.
Definition usbus.h:448
usbus_state_t state
Current state
Definition usbus.h:466
usbus_state_t pstate
state to recover to from suspend
Definition usbus.h:467
usbus_endpoint_t ep_out[USBDEV_NUM_ENDPOINTS]
USBUS OUT endpoints
Definition usbus.h:453
usbus_handler_t * handlers
List of event callback handlers
Definition usbus.h:462
uint8_t addr
Address of the USB peripheral
Definition usbus.h:468
bool wakeup_enabled
Remote wakeup device feature status
Definition usbus.h:469
char serial_str[2 *CONFIG_USB_SERIAL_BYTE_LENGTH+1]
Hex representation of the device serial number.
Definition usbus.h:474
kernel_pid_t pid
PID of the usb manager's thread
Definition usbus.h:464
usbus_endpoint_t ep_in[USBDEV_NUM_ENDPOINTS]
USBUS IN endpoints
Definition usbus.h:454
usbus_descr_gen_t * descr_gen
Linked list of top level descriptor generators
Definition usbus.h:458
usbus_handler_t * control
Ptr to the control endpoint handler
Definition usbus.h:457
usbus_string_t serial
serial string
Definition usbus.h:452
usbus_string_t product
Product string
Definition usbus.h:450
usbus_string_t manuf
Manufacturer string
Definition usbus.h:449
usbus_string_t * strings
List of descriptor strings
Definition usbus.h:460
usbdev_t * dev
usb phy device of the usb manager
Definition usbus.h:456
uint16_t str_idx
Number of strings registered
Definition usbus.h:465
usbus_interface_t * iface
List of USB interfaces
Definition usbus.h:461
event_queue_t queue
Event queue
Definition usbus.h:455
uint32_t ep_events
bitflags with endpoint event state
Definition usbus.h:463
usbus_string_t config
Configuration string
Definition usbus.h:451
Definition of global compile time configuration options.
Definitions low-level USB driver interface.