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 "kernel_defines.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
41 extern "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 #if !IS_ACTIVE(KCONFIG_MODULE_USBUS)
72 #define CONFIG_USBUS_AUTO_ATTACH 1
73 #endif
74 #endif
75 
83 #if IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_8)
84 #define CONFIG_USBUS_EP0_SIZE 8
85 #elif IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_16)
86 #define CONFIG_USBUS_EP0_SIZE 16
87 #elif IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_32)
88 #define CONFIG_USBUS_EP0_SIZE 32
89 #elif IS_ACTIVE(CONFIG_USBUS_EP0_SIZE_64)
90 #define CONFIG_USBUS_EP0_SIZE 64
91 #endif
92 
93 #ifndef CONFIG_USBUS_EP0_SIZE
94 #define CONFIG_USBUS_EP0_SIZE 64
95 #endif
96 
101 #define USBUS_TNAME "usbus"
102 
110 #define USBUS_THREAD_FLAG_USBDEV (0x02)
111 #define USBUS_THREAD_FLAG_USBDEV_EP (0x04)
120 #define USBUS_HANDLER_FLAG_RESET (0x0001)
121 #define USBUS_HANDLER_FLAG_SOF (0x0002)
122 #define USBUS_HANDLER_FLAG_SUSPEND (0x0004)
123 #define USBUS_HANDLER_FLAG_RESUME (0x0008)
124 #define USBUS_HANDLER_FLAG_TR_FAIL (0x0010)
125 #define USBUS_HANDLER_FLAG_TR_STALL (0x0020)
131 typedef enum {
137 
141 typedef enum {
146 
150 typedef enum {
156 } usbus_state_t;
157 
161 typedef enum {
171 
175 typedef struct usbus_string {
176  struct usbus_string *next;
177  const char *str;
178  uint16_t idx;
180 
184 typedef struct usbus usbus_t;
185 
190 
194 typedef enum {
198 
202 typedef struct {
213  size_t (*fmt_pre_descriptor)(usbus_t *usbus, void *arg);
214 
225  size_t (*fmt_post_descriptor)(usbus_t *usbus, void *arg);
226  union {
240  size_t (*get_descriptor_len)(usbus_t *usbus, void *arg);
249  size_t fixed_len;
250  } len;
253 
263 typedef struct usbus_descr_gen {
266  void *arg;
269 
273 typedef struct usbus_endpoint {
280  uint16_t maxpacketsize;
281  uint8_t interval;
282  bool active;
285 
291 typedef struct usbus_interface_alt {
298 
302 typedef struct usbus_interface {
312  uint16_t idx;
314  uint8_t class;
315  uint8_t subclass;
316  uint8_t protocol;
318 
322 typedef struct usbus_handler_driver {
323 
333  void (*init)(usbus_t * usbus, struct usbus_handler *handler);
334 
344  void (*event_handler)(usbus_t * usbus, struct usbus_handler *handler,
346 
357  void (*transfer_handler)(usbus_t * usbus, struct usbus_handler *handler,
359 
375  int (*control_handler)(usbus_t * usbus, struct usbus_handler *handler,
377  usb_setup_t *request);
379 
386  struct usbus_handler *next;
391  uint32_t flags;
392 };
393 
397 struct usbus {
411  uint32_t ep_events;
413  uint16_t str_idx;
416  uint8_t addr;
417 };
418 
426 {
428 }
429 
440  const char *str);
441 
451 
467  usb_ep_type_t type,
468  usb_ep_dir_t dir);
469 
486  usb_ep_type_t type, usb_ep_dir_t dir,
487  size_t len);
488 
497 
509 
517 
527 void usbus_create(char *stack, int stacksize, char priority,
528  const char *name, usbus_t *usbus);
529 
538 {
539  ep->active = true;
540 }
541 
550 {
551  ep->active = false;
552 }
553 
560 static inline void usbus_handler_set_flag(usbus_handler_t *handler,
561  uint32_t flag)
562 {
563  handler->flags |= flag;
564 }
565 
572 static inline void usbus_handler_remove_flag(usbus_handler_t *handler,
573  uint32_t flag)
574 {
575  handler->flags &= ~flag;
576 }
577 
586 static inline bool usbus_handler_isset_flag(usbus_handler_t *handler,
587  uint32_t flag)
588 {
589  return handler->flags & flag;
590 }
591 
592 
593 #ifdef __cplusplus
594 }
595 #endif
596 #endif /* USB_USBUS_H */
597 
usb_ep_dir_t
usb_ep_dir_t
USB endpoint directions.
Definition: usb.h:204
usbus_interface_alt::next
struct usbus_interface_alt * next
Next alternative setting.
Definition: usbus.h:292
kernel_pid_t
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:125
usbus_event_post
static void usbus_event_post(usbus_t *usbus, event_t *event)
Submit an event to the usbus thread.
Definition: usbus.h:425
usbus_endpoint::active
bool active
If the endpoint should be activated after reset.
Definition: usbus.h:282
usbus::ep_out
usbus_endpoint_t ep_out[USBDEV_NUM_ENDPOINTS]
USBUS OUT endpoints
Definition: usbus.h:401
USBDEV_NUM_ENDPOINTS
#define USBDEV_NUM_ENDPOINTS
Number of USB IN and OUT endpoints allocated.
Definition: usbdev.h:113
usbus_interface_alt::ep
usbus_endpoint_t * ep
List of associated endpoints for this alternative setting.
Definition: usbus.h:295
usbus_interface::subclass
uint8_t subclass
USB interface subclass
Definition: usbus.h:315
usbus_interface_alt_t
struct usbus_interface_alt usbus_interface_alt_t
USBUS interface alternative setting.
usbus::control
usbus_handler_t * control
Ptr to the control endpoint handler
Definition: usbus.h:405
usbus_event_transfer_t
usbus_event_transfer_t
USB endpoint transfer status events.
Definition: usbus.h:141
kernel_defines.h
Common macros and compiler attributes/pragmas configuration.
USBUS_DESCR_LEN_FUNC
@ USBUS_DESCR_LEN_FUNC
Descriptor length is calculated by a function.
Definition: usbus.h:196
usbus_add_interface
uint16_t usbus_add_interface(usbus_t *usbus, usbus_interface_t *iface)
Add an interface to the USBUS thread context.
usbus_event_usb_t
usbus_event_usb_t
USB handler events.
Definition: usbus.h:131
usbus_add_string_descriptor
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.
USBUS_CONTROL_REQUEST_STATE_INACK
@ USBUS_CONTROL_REQUEST_STATE_INACK
Expecting a zero-length ack IN request from the host.
Definition: usbus.h:168
usbus_interface_alt
USBUS interface alternative setting.
Definition: usbus.h:291
usbus_interface_t
struct usbus_interface usbus_interface_t
USBUS interface.
usbus_handler::driver
const usbus_handler_driver_t * driver
driver for this handler
Definition: usbus.h:388
usbus_handler_driver::transfer_handler
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:357
USBUS_EVENT_TRANSFER_FAIL
@ USBUS_EVENT_TRANSFER_FAIL
Transfer nack replied by peripheral.
Definition: usbus.h:143
USBUS_EVENT_USB_SOF
@ USBUS_EVENT_USB_SOF
USB start of frame received
Definition: usbus.h:133
usbdev.h
Definitions low-level USB driver interface.
USBUS_EVENT_USB_RESUME
@ USBUS_EVENT_USB_RESUME
USB resume condition detected
Definition: usbus.h:135
usb_ep_type_t
usb_ep_type_t
USB endpoint types.
Definition: usb.h:193
event
event structure
Definition: event.h:142
usbus_descr_gen::next
struct usbus_descr_gen * next
ptr to the next descriptor generator
Definition: usbus.h:264
usbus_interface::alts
struct usbus_interface_alt * alts
List of alt settings
Definition: usbus.h:309
usbus_handler_remove_flag
static void usbus_handler_remove_flag(usbus_handler_t *handler, uint32_t flag)
disable a specific handler flag
Definition: usbus.h:572
usbus::product
usbus_string_t product
Product string
Definition: usbus.h:399
usbus::ep_in
usbus_endpoint_t ep_in[USBDEV_NUM_ENDPOINTS]
USBUS IN endpoints
Definition: usbus.h:402
event_queue_t
event queue structure
Definition: event.h:150
msg.h
Messaging API for inter process communication.
usbus_disable_endpoint
static void usbus_disable_endpoint(usbus_endpoint_t *ep)
Disable an endpoint.
Definition: usbus.h:549
USBUS_CONTROL_REQUEST_STATE_READY
@ USBUS_CONTROL_REQUEST_STATE_READY
Ready for new control request.
Definition: usbus.h:162
sched.h
Scheduler API definition.
usbus_enable_endpoint
static void usbus_enable_endpoint(usbus_endpoint_t *ep)
Enable an endpoint.
Definition: usbus.h:537
usbdev
usbdev device descriptor
Definition: usbdev.h:217
usbus_interface::descr_gen
usbus_descr_gen_t * descr_gen
Linked list of optional additional descriptor generators.
Definition: usbus.h:305
usbus::manuf
usbus_string_t manuf
Manufacturer string
Definition: usbus.h:398
usbus_interface::descr
usbus_string_t * descr
Descriptor string
Definition: usbus.h:311
usbus_add_conf_descriptor
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::addr
uint8_t addr
Address of the USB peripheral
Definition: usbus.h:416
USBUS_STATE_SUSPEND
@ USBUS_STATE_SUSPEND
Peripheral is suspended by the host.
Definition: usbus.h:155
usbus_endpoint::next
struct usbus_endpoint * next
Next endpoint in the usbus_interface_t list of endpoints.
Definition: usbus.h:274
usbus_descr_gen_funcs_t
USBUS descriptor generator function pointers.
Definition: usbus.h:202
USBUS_CONTROL_REQUEST_STATE_OUTDATA
@ USBUS_CONTROL_REQUEST_STATE_OUTDATA
Data OUT expected.
Definition: usbus.h:167
usbus_string::next
struct usbus_string * next
Ptr to the next registered string.
Definition: usbus.h:176
usb_setup_t
USB setup packet (USB 2.0 spec table 9-2)
Definition: descriptor.h:198
usbus_control_request_state_t
usbus_control_request_state_t
USBUS control request state machine.
Definition: usbus.h:161
usbus_interface_find_endpoint
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.
usbus::config
usbus_string_t config
Configuration string
Definition: usbus.h:400
usbus_register_event_handler
void usbus_register_event_handler(usbus_t *usbus, usbus_handler_t *handler)
Add an event handler to the USBUS context.
usbus_interface::protocol
uint8_t protocol
USB interface protocol
Definition: usbus.h:316
usbus_string::idx
uint16_t idx
USB string index
Definition: usbus.h:178
usbus_handler::flags
uint32_t flags
Report flags.
Definition: usbus.h:391
usbus_handler_set_flag
static void usbus_handler_set_flag(usbus_handler_t *handler, uint32_t flag)
enable a specific handler flag
Definition: usbus.h:560
usbus_endpoint::descr_gen
usbus_descr_gen_t * descr_gen
Linked list of optional additional descriptor generators.
Definition: usbus.h:277
usbus::iface
usbus_interface_t * iface
List of USB interfaces
Definition: usbus.h:409
USBUS_EVENT_USB_RESET
@ USBUS_EVENT_USB_RESET
USB reset event
Definition: usbus.h:132
usbus_endpoint_t
struct usbus_endpoint usbus_endpoint_t
USBUS endpoint context.
usbus_add_endpoint
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.
USBUS_EVENT_TRANSFER_STALL
@ USBUS_EVENT_TRANSFER_STALL
Transfer stall replied by peripheral.
Definition: usbus.h:144
usbus_handler_driver::event_handler
void(* event_handler)(usbus_t *usbus, struct usbus_handler *handler, usbus_event_usb_t event)
event handler function
Definition: usbus.h:344
USBUS_CONTROL_REQUEST_STATE_INDATA
@ USBUS_CONTROL_REQUEST_STATE_INDATA
Request received with expected DATA IN stage.
Definition: usbus.h:163
usbus_handler::next
struct usbus_handler * next
List of handlers (to be used by usbus_t)
Definition: usbus.h:386
usbdev_ep
usbdev endpoint descriptor
Definition: usbdev.h:229
usbus_descr_gen_funcs_t::len_type
usbus_descr_len_type_t len_type
Either USBUS_DESCR_LEN_FIXED or USBUS_DESCR_LEN_FUNC.
Definition: usbus.h:251
usbus_interface::ep
usbus_endpoint_t * ep
Linked list of endpoints belonging to this interface
Definition: usbus.h:307
usbus_handler::iface
usbus_interface_t * iface
Interface this handler belongs to.
Definition: usbus.h:389
USBUS_EVENT_USB_SUSPEND
@ USBUS_EVENT_USB_SUSPEND
USB suspend condition detected.
Definition: usbus.h:134
usbus_endpoint::ep
usbdev_ep_t * ep
ptr to the matching usbdev endpoint
Definition: usbus.h:279
usb.h
Definition of global compile time configuration options.
usbus_handler
USBUS handler struct.
Definition: usbus.h:385
usbus_handler_driver::init
void(* init)(usbus_t *usbus, struct usbus_handler *handler)
Initialize the event handler.
Definition: usbus.h:333
usbus::ep_events
uint32_t ep_events
bitflags with endpoint event state
Definition: usbus.h:411
clist.h
Circular linked list.
event_post
void event_post(event_queue_t *queue, event_t *event)
Queue an event.
usbus_endpoint::maxpacketsize
uint16_t maxpacketsize
Max packet size of this endpoint.
Definition: usbus.h:280
usbus_endpoint::interval
uint8_t interval
Poll interval for interrupt endpoints.
Definition: usbus.h:281
usbus::dev
usbdev_t * dev
usb phy device of the usb manager
Definition: usbus.h:404
usbus_interface_alt::descr_gen
usbus_descr_gen_t * descr_gen
Linked list of optional additional descriptor generators.
Definition: usbus.h:293
usbus::state
usbus_state_t state
Current state
Definition: usbus.h:414
USBUS_STATE_DISCONNECT
@ USBUS_STATE_DISCONNECT
Device is disconnected from the host.
Definition: usbus.h:151
usbus_handler_driver_t
struct usbus_handler_driver usbus_handler_driver_t
USBUS event handler function pointers.
usbus_descr_gen
USBUS descriptor generator.
Definition: usbus.h:263
usbus_interface::idx
uint16_t idx
Interface index, (set by USBUS during registration
Definition: usbus.h:312
USBUS_STATE_ADDR
@ USBUS_STATE_ADDR
Address configured.
Definition: usbus.h:153
usbus_string
USBUS string type.
Definition: usbus.h:175
usbus::pid
kernel_pid_t pid
PID of the usb manager's thread
Definition: usbus.h:412
usbus_descr_gen_funcs_t::fixed_len
size_t fixed_len
Fixed total length of the generated descriptors.
Definition: usbus.h:249
usbus::descr_gen
usbus_descr_gen_t * descr_gen
Linked list of top level descriptor generators
Definition: usbus.h:406
usbus_string::str
const char * str
C string to use as content
Definition: usbus.h:177
usbus::strings
usbus_string_t * strings
List of descriptor strings
Definition: usbus.h:408
usbus_descr_gen::funcs
const usbus_descr_gen_funcs_t * funcs
Function pointers.
Definition: usbus.h:265
usbus::str_idx
uint16_t str_idx
Number of strings registered
Definition: usbus.h:413
usbus_interface
USBUS interface.
Definition: usbus.h:302
usbus_descr_gen_t
struct usbus_descr_gen usbus_descr_gen_t
USBUS descriptor generator.
usbus_descr_gen::arg
void * arg
Extra context argument for the descriptor functions.
Definition: usbus.h:266
usbus_init
void usbus_init(usbus_t *usbus, usbdev_t *usbdev)
Initialize an USBUS context.
usbus_descr_len_type_t
usbus_descr_len_type_t
descriptor length types for USB descriptor generators
Definition: usbus.h:194
usbus_handler_driver
USBUS event handler function pointers.
Definition: usbus.h:322
usbus_interface::handler
usbus_handler_t * handler
Handlers for this interface
Definition: usbus.h:310
usbus::handlers
usbus_handler_t * handlers
List of event callback handlers
Definition: usbus.h:410
USBUS_STATE_CONFIGURED
@ USBUS_STATE_CONFIGURED
Peripheral is configured.
Definition: usbus.h:154
usbus::queue
event_queue_t queue
Event queue
Definition: usbus.h:403
usbus::pstate
usbus_state_t pstate
state to recover to from suspend
Definition: usbus.h:415
USBUS_EVENT_TRANSFER_COMPLETE
@ USBUS_EVENT_TRANSFER_COMPLETE
Transfer successfully completed.
Definition: usbus.h:142
usbus_endpoint
USBUS endpoint context.
Definition: usbus.h:273
USBUS_CONTROL_REQUEST_STATE_OUTACK
@ USBUS_CONTROL_REQUEST_STATE_OUTACK
Expecting a zero-length ack OUT request from the host.
Definition: usbus.h:165
usbus_string_t
struct usbus_string usbus_string_t
USBUS string type.
USBUS_DESCR_LEN_FIXED
@ USBUS_DESCR_LEN_FIXED
Descriptor always generates a fixed length.
Definition: usbus.h:195
usbus_state_t
usbus_state_t
state machine states for the global USBUS thread
Definition: usbus.h:150
usbus_handler_driver::control_handler
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:375
USBUS_STATE_RESET
@ USBUS_STATE_RESET
Reset condition received.
Definition: usbus.h:152
usbus_interface::next
struct usbus_interface * next
Next interface (set by USBUS during registration)
Definition: usbus.h:303
usbus_handler_isset_flag
static bool usbus_handler_isset_flag(usbus_handler_t *handler, uint32_t flag)
check if a specific handler flag is set
Definition: usbus.h:586
descriptor.h
Definitions for USB protocol messages.
usbus
USBUS context struct.
Definition: usbus.h:397
usbus_create
void usbus_create(char *stack, int stacksize, char priority, const char *name, usbus_t *usbus)
Create and start the USBUS thread.