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
9#pragma once
10
24
25#include <stdint.h>
26#include <stdlib.h>
27
28#include "clist.h"
29#include "event.h"
30#include "sched.h"
31#include "modules.h"
32#include "msg.h"
33#include "thread.h"
34
35#include "usb.h"
36#include "periph/usbdev.h"
37#include "usb/descriptor.h"
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
51#ifndef USBUS_STACKSIZE
52#define USBUS_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
53#endif
54
58#ifndef USBUS_PRIO
59#define USBUS_PRIO (THREAD_PRIORITY_MAIN - 6)
60#endif
61
69#ifndef CONFIG_USBUS_AUTO_ATTACH
70/* Check for Kconfig usage */
71#if !IS_ACTIVE(CONFIG_MODULE_USBUS)
72#define CONFIG_USBUS_AUTO_ATTACH 1
73#endif
74#endif
75
82#ifndef CONFIG_USBUS_MSC_AUTO_MTD
83#define CONFIG_USBUS_MSC_AUTO_MTD 1
84#endif
85
93#if IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_8)
94#define CONFIG_USBUS_EP0_SIZE 8
95#elif IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_16)
96#define CONFIG_USBUS_EP0_SIZE 16
97#elif IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_32)
98#define CONFIG_USBUS_EP0_SIZE 32
99#elif IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_64)
100#define CONFIG_USBUS_EP0_SIZE 64
101#endif
102
103#ifndef CONFIG_USBUS_EP0_SIZE
104#define CONFIG_USBUS_EP0_SIZE 64
105#endif
107
111#define USBUS_TNAME "usbus"
112
120#define USBUS_THREAD_FLAG_USBDEV (0x02)
121#define USBUS_THREAD_FLAG_USBDEV_EP (0x04)
124
130#define USBUS_HANDLER_FLAG_RESET (0x0001)
131#define USBUS_HANDLER_FLAG_SOF (0x0002)
132#define USBUS_HANDLER_FLAG_SUSPEND (0x0004)
133#define USBUS_HANDLER_FLAG_RESUME (0x0008)
134#define USBUS_HANDLER_FLAG_TR_STALL (0x0020)
136
142
147#define USBUS_URB_FLAG_AUTO_ZLP (0x0001)
148
153#define USBUS_URB_FLAG_NEEDS_ZLP (0x1000)
154
159#define USBUS_URB_FLAG_CANCELLED (0x2000)
161
171
180
191
205
209typedef struct usbus_string {
211 const char *str;
212 uint16_t idx;
214
218typedef struct usbus usbus_t;
219
224
232
236typedef struct {
247 size_t (*fmt_pre_descriptor)(usbus_t *usbus, void *arg);
248
259 size_t (*fmt_post_descriptor)(usbus_t *usbus, void *arg);
260 union {
274 size_t (*get_descriptor_len)(usbus_t *usbus, void *arg);
283 size_t fixed_len;
284 } len;
287
303
307typedef struct usbus_endpoint {
314#ifdef MODULE_USBUS_URB
315 clist_node_t urb_list;
316#endif
317 uint16_t maxpacketsize;
318 uint8_t interval;
319 bool active;
321 bool halted;
323
327typedef struct usbus_urb {
329 uint32_t flags;
330 uint8_t *buf;
331 size_t len;
332 size_t transferred;
334
348
368
372typedef struct usbus_handler_driver {
373
383 void (*init)(usbus_t * usbus, struct usbus_handler *handler);
384
394 void (*event_handler)(usbus_t * usbus, struct usbus_handler *handler,
396
407 void (*transfer_handler)(usbus_t * usbus, struct usbus_handler *handler,
409
425 int (*control_handler)(usbus_t * usbus, struct usbus_handler *handler,
427 usb_setup_t *request);
429
443
476
484{
486}
487
498 const char *str);
499
509
518
534 usb_ep_type_t type,
535 usb_ep_dir_t dir);
536
553 usb_ep_type_t type, usb_ep_dir_t dir,
554 size_t len);
555
564
576
584
592
600
610void usbus_create(char *stack, int stacksize, char priority,
611 const char *name, usbus_t *usbus);
612
628static inline void usbus_urb_init(usbus_urb_t *urb,
629 uint8_t *buf,
630 size_t len,
631 uint32_t flags)
632{
633 urb->buf = buf;
634 urb->len = len;
635 urb->flags = flags;
636 urb->transferred = 0;
637}
638
649
671
679
687
696{
697 ep->active = true;
698}
699
708{
709 ep->active = false;
710}
711
718static inline void usbus_handler_set_flag(usbus_handler_t *handler,
719 uint32_t flag)
720{
721 handler->flags |= flag;
722}
723
730static inline void usbus_handler_remove_flag(usbus_handler_t *handler,
731 uint32_t flag)
732{
733 handler->flags &= ~flag;
734}
735
744static inline bool usbus_handler_isset_flag(usbus_handler_t *handler,
745 uint32_t flag)
746{
747 return handler->flags & flag;
748}
749
756static inline void usbus_urb_set_flag(usbus_urb_t *urb,
757 uint32_t flag)
758{
759 urb->flags |= flag;
760}
761
768static inline void usbus_urb_remove_flag(usbus_urb_t *urb,
769 uint32_t flag)
770{
771 urb->flags &= ~flag;
772}
773
782static inline bool usbus_urb_isset_flag(usbus_urb_t *urb,
783 uint32_t flag)
784{
785 return urb->flags & flag;
786}
787
788#ifdef __cplusplus
789}
790#endif
Circular linked list.
list_node_t clist_node_t
List node structure.
Definition clist.h:106
Definitions for USB protocol messages.
#define USBDEV_NUM_ENDPOINTS
Number of USB OTG FS endpoints including EP0.
Definition periph_cpu.h:707
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:138
struct usbdev_ep usbdev_ep_t
usbdev_ep_t forward declaration
Definition usbdev.h:99
struct usbdev usbdev_t
usbdev_t forward declaration
Definition usbdev.h:94
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:135
static void usbus_urb_remove_flag(usbus_urb_t *urb, uint32_t flag)
disable an URB flag
Definition usbus.h:768
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:730
struct usbus_interface_alt usbus_interface_alt_t
USBUS interface alternative setting.
usbus_event_transfer_t
USB endpoint transfer status events.
Definition usbus.h:175
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:228
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:628
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:744
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:756
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:165
static void usbus_event_post(usbus_t *usbus, event_t *event)
Submit an event to the usbus thread.
Definition usbus.h:483
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:718
usbus_control_request_state_t
USBUS control request state machine.
Definition usbus.h:195
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:223
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:218
static bool usbus_urb_isset_flag(usbus_urb_t *urb, uint32_t flag)
check if an URB flag is set
Definition usbus.h:782
static void usbus_enable_endpoint(usbus_endpoint_t *ep)
Enable an endpoint.
Definition usbus.h:695
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:184
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:707
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:176
@ USBUS_EVENT_TRANSFER_STALL
Transfer stall replied by peripheral.
Definition usbus.h:178
@ USBUS_EVENT_TRANSFER_FAIL
Transfer nack replied by peripheral.
Definition usbus.h:177
@ USBUS_DESCR_LEN_FIXED
Descriptor always generates a fixed length.
Definition usbus.h:229
@ USBUS_DESCR_LEN_FUNC
Descriptor length is calculated by a function.
Definition usbus.h:230
@ USBUS_EVENT_USB_SOF
USB start of frame received.
Definition usbus.h:167
@ USBUS_EVENT_USB_RESUME
USB resume condition detected.
Definition usbus.h:169
@ USBUS_EVENT_USB_RESET
USB reset event.
Definition usbus.h:166
@ USBUS_EVENT_USB_SUSPEND
USB suspend condition detected.
Definition usbus.h:168
@ USBUS_CONTROL_REQUEST_STATE_INACK
Expecting a zero-length ack IN request from the host.
Definition usbus.h:202
@ USBUS_CONTROL_REQUEST_STATE_OUTACK
Expecting a zero-length ack OUT request from the host.
Definition usbus.h:199
@ USBUS_CONTROL_REQUEST_STATE_OUTDATA
Data OUT expected.
Definition usbus.h:201
@ USBUS_CONTROL_REQUEST_STATE_INDATA
Request received with expected DATA IN stage.
Definition usbus.h:197
@ USBUS_CONTROL_REQUEST_STATE_READY
Ready for new control request.
Definition usbus.h:196
@ USBUS_STATE_RESET
Reset condition received.
Definition usbus.h:186
@ USBUS_STATE_CONFIGURED
Peripheral is configured.
Definition usbus.h:188
@ USBUS_STATE_ADDR
Address configured.
Definition usbus.h:187
@ USBUS_STATE_DISCONNECT
Device is disconnected from the host.
Definition usbus.h:185
@ USBUS_STATE_SUSPEND
Peripheral is suspended by the host.
Definition usbus.h:189
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 structure
Definition event.h:145
USB setup packet (USB 2.0 spec table 9-2)
Definition descriptor.h:208
usbdev device descriptor
Definition usbdev.h:247
USBUS descriptor generator function pointers.
Definition usbus.h:236
size_t(* get_descriptor_len)(usbus_t *usbus, void *arg)
USBUS generic descriptor generator generated length.
Definition usbus.h:274
size_t(* fmt_post_descriptor)(usbus_t *usbus, void *arg)
function pointer to format the descriptor content of this descriptor generator.
Definition usbus.h:259
usbus_descr_len_type_t len_type
Either USBUS_DESCR_LEN_FIXED or USBUS_DESCR_LEN_FUNC.
Definition usbus.h:285
size_t(* fmt_pre_descriptor)(usbus_t *usbus, void *arg)
function pointer to format the descriptor content of this descriptor generator.
Definition usbus.h:247
size_t fixed_len
Fixed total length of the generated descriptors.
Definition usbus.h:283
USBUS descriptor generator.
Definition usbus.h:297
void * arg
Extra context argument for the descriptor functions.
Definition usbus.h:300
struct usbus_descr_gen * next
ptr to the next descriptor generator
Definition usbus.h:298
const usbus_descr_gen_funcs_t * funcs
Function pointers.
Definition usbus.h:299
USBUS endpoint context.
Definition usbus.h:307
uint16_t maxpacketsize
Max packet size of this endpoint.
Definition usbus.h:317
bool active
If the endpoint should be activated after reset.
Definition usbus.h:319
usbdev_ep_t * ep
ptr to the matching usbdev endpoint
Definition usbus.h:313
uint8_t interval
Poll interval for interrupt endpoints.
Definition usbus.h:318
struct usbus_endpoint * next
Next endpoint in the usbus_interface_t list of endpoints.
Definition usbus.h:308
usbus_descr_gen_t * descr_gen
Linked list of optional additional descriptor generators.
Definition usbus.h:311
bool halted
Endpoint is halted.
Definition usbus.h:321
USBUS event handler function pointers.
Definition usbus.h:372
void(* init)(usbus_t *usbus, struct usbus_handler *handler)
Initialize the event handler.
Definition usbus.h:383
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:407
void(* event_handler)(usbus_t *usbus, struct usbus_handler *handler, usbus_event_usb_t event)
event handler function
Definition usbus.h:394
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:425
USBUS handler struct.
Definition usbus.h:435
struct usbus_handler * next
List of handlers (to be used by usbus_t)
Definition usbus.h:436
uint32_t flags
Report flags.
Definition usbus.h:441
const usbus_handler_driver_t * driver
driver for this handler
Definition usbus.h:438
usbus_interface_t * iface
Interface this handler belongs to.
Definition usbus.h:439
USBUS interface alternative setting.
Definition usbus.h:340
usbus_descr_gen_t * descr_gen
Linked list of optional additional descriptor generators.
Definition usbus.h:342
struct usbus_interface_alt * next
Next alternative setting.
Definition usbus.h:341
usbus_endpoint_t * ep
List of associated endpoints for this alternative setting.
Definition usbus.h:344
usbus_string_t * descr
Descriptor string.
Definition usbus.h:346
USBUS interface.
Definition usbus.h:352
uint8_t protocol
USB interface protocol.
Definition usbus.h:366
struct usbus_interface_alt * alts
List of alt settings.
Definition usbus.h:359
uint8_t subclass
USB interface subclass.
Definition usbus.h:365
usbus_endpoint_t * ep
Linked list of endpoints belonging to this interface.
Definition usbus.h:357
struct usbus_interface * next
Next interface (set by USBUS during registration)
Definition usbus.h:353
usbus_descr_gen_t * descr_gen
Linked list of optional additional descriptor generators.
Definition usbus.h:355
usbus_handler_t * handler
Handlers for this interface.
Definition usbus.h:360
uint16_t idx
Interface index, (set by USBUS during registration.
Definition usbus.h:362
usbus_string_t * descr
Descriptor string.
Definition usbus.h:361
USBUS string type.
Definition usbus.h:209
const char * str
C string to use as content.
Definition usbus.h:211
struct usbus_string * next
Ptr to the next registered string.
Definition usbus.h:210
uint16_t idx
USB string index.
Definition usbus.h:212
USBUS USB request/response block.
Definition usbus.h:327
uint8_t * buf
Pointer to the (aligned) buffer.
Definition usbus.h:330
clist_node_t list
clist block in the queue
Definition usbus.h:328
size_t len
Length of the data.
Definition usbus.h:331
uint32_t flags
Transfer flags.
Definition usbus.h:329
size_t transferred
amount transferred, only valid for OUT
Definition usbus.h:332
USBUS context struct.
Definition usbus.h:447
usbus_state_t state
Current state.
Definition usbus.h:465
usbus_state_t pstate
state to recover to from suspend
Definition usbus.h:466
usbus_endpoint_t ep_out[USBDEV_NUM_ENDPOINTS]
USBUS OUT endpoints.
Definition usbus.h:452
usbus_handler_t * handlers
List of event callback handlers.
Definition usbus.h:461
uint8_t addr
Address of the USB peripheral.
Definition usbus.h:467
bool wakeup_enabled
Remote wakeup device feature status.
Definition usbus.h:468
char serial_str[2 *CONFIG_USB_SERIAL_BYTE_LENGTH+1]
Hex representation of the device serial number.
Definition usbus.h:473
kernel_pid_t pid
PID of the usb manager's thread.
Definition usbus.h:463
usbus_endpoint_t ep_in[USBDEV_NUM_ENDPOINTS]
USBUS IN endpoints.
Definition usbus.h:453
usbus_descr_gen_t * descr_gen
Linked list of top level descriptor generators.
Definition usbus.h:457
usbus_handler_t * control
Ptr to the control endpoint handler.
Definition usbus.h:456
usbus_string_t serial
serial string
Definition usbus.h:451
usbus_string_t product
Product string.
Definition usbus.h:449
usbus_string_t manuf
Manufacturer string.
Definition usbus.h:448
usbus_string_t * strings
List of descriptor strings.
Definition usbus.h:459
usbdev_t * dev
usb phy device of the usb manager
Definition usbus.h:455
uint16_t str_idx
Number of strings registered.
Definition usbus.h:464
usbus_interface_t * iface
List of USB interfaces.
Definition usbus.h:460
event_queue_t queue
Event queue.
Definition usbus.h:454
uint32_t ep_events
bitflags with endpoint event state
Definition usbus.h:462
usbus_string_t config
Configuration string.
Definition usbus.h:450
Definition of global compile time configuration options.
Definitions low-level USB driver interface.