usbdev.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Koen Zandberg <koen@bergzand.net>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser General
5  * Public License v2.1. See the file LICENSE in the top level directory for
6  * more details.
7  */
71 #ifndef PERIPH_USBDEV_H
72 #define PERIPH_USBDEV_H
73 
74 #include <stdint.h>
75 #include <stddef.h>
76 
77 #include "assert.h"
78 #include "usb.h"
79 #include "usb/usbopt.h"
80 
81 #ifdef __cplusplus
82 extern "C" {
83 #endif
84 
88 typedef struct usbdev usbdev_t;
89 
93 typedef struct usbdev_ep usbdev_ep_t;
94 
102 #ifndef USBDEV_EP_BUF_SPACE
103 #define USBDEV_EP_BUF_SPACE 1024
104 #endif
105 
112 #ifndef USBDEV_NUM_ENDPOINTS
113 #define USBDEV_NUM_ENDPOINTS 8
114 #endif
115 
120 typedef enum {
125 
133 
141 
153 
160 
165 
170 
179 
187 
194  /* expand list if required */
196 
204 
211 typedef void (*usbdev_ep_event_cb_t)(usbdev_ep_t *ep,
213 
217 struct usbdev {
218  const struct usbdev_driver *driver;
223  void *context;
224 };
225 
229 struct usbdev_ep {
231  uint8_t *buf;
232  size_t len;
235  uint8_t num;
236 };
237 
244 typedef struct usbdev_driver {
245 
253  void (*init)(usbdev_t *usbdev);
254 
269  usbdev_ep_t *(*new_ep)(usbdev_t *dev, usb_ep_type_t type, usb_ep_dir_t dir, size_t buf_len);
270 
282  int (*get)(usbdev_t *usbdev, usbopt_t opt,
283  void *value, size_t max_len);
284 
296  int (*set)(usbdev_t *usbdev, usbopt_t opt,
297  const void *value, size_t value_len);
298 
307  void (*esr)(usbdev_t *dev);
308 
317  void (*ep_init)(usbdev_ep_t *ep);
318 
330  int (*ep_get)(usbdev_ep_t *ep, usbopt_ep_t opt,
331  void *value, size_t max_len);
332 
344  int (*ep_set)(usbdev_ep_t *ep, usbopt_ep_t opt,
345  const void *value, size_t value_len);
346 
355  void (*ep_esr)(usbdev_ep_t *ep);
356 
365  int (*ready)(usbdev_ep_t *ep, size_t len);
367 
373 void usbdev_init_lowlevel(void);
374 
382 usbdev_t *usbdev_get_ctx(unsigned num);
383 
393 static inline void usbdev_init(usbdev_t *dev)
394 {
395  assert(dev);
396  dev->driver->init(dev);
397 }
398 
414 static inline usbdev_ep_t * usbdev_new_ep(usbdev_t *dev, usb_ep_type_t type,
415  usb_ep_dir_t dir, size_t buf_len)
416 {
417  assert(dev);
418  return dev->driver->new_ep(dev, type, dir, buf_len);
419 }
420 
436 static inline int usbdev_get(usbdev_t *dev, usbopt_t opt,
437  void *value, size_t max_len)
438 {
439  assert(dev);
440  return dev->driver->get(dev, opt, value, max_len);
441 }
442 
458 static inline int usbdev_set(usbdev_t *dev, usbopt_t opt,
459  const void *value, size_t value_len)
460 {
461  assert(dev);
462  return dev->driver->set(dev, opt, value, value_len);
463 }
464 
474 static inline void usbdev_esr(usbdev_t *dev)
475 {
476  assert(dev);
477  dev->driver->esr(dev);
478 }
479 
490 static inline void usbdev_ep_init(usbdev_ep_t *ep)
491 {
492  assert(ep);
493  assert(ep->dev);
494  ep->dev->driver->ep_init(ep);
495 }
496 
513 static inline int usbdev_ep_get(usbdev_ep_t *ep, usbopt_ep_t opt,
514  void *value, size_t max_len)
515 {
516  assert(ep);
517  assert(ep->dev);
518  return ep->dev->driver->ep_get(ep, opt, value, max_len);
519 }
520 
537 static inline int usbdev_ep_set(usbdev_ep_t *ep, usbopt_ep_t opt,
538  const void *value, size_t value_len)
539 {
540  assert(ep);
541  assert(ep->dev);
542  return ep->dev->driver->ep_set(ep, opt, value, value_len);
543 }
544 
555 static inline void usbdev_ep_esr(usbdev_ep_t *ep)
556 {
557  assert(ep);
558  assert(ep->dev);
559  ep->dev->driver->ep_esr(ep);
560 }
561 
573 static inline int usbdev_ep_ready(usbdev_ep_t *ep, size_t len)
574 {
575  assert(ep);
576  assert(ep->dev);
577  return ep->dev->driver->ready(ep, len);
578 }
579 
580 #ifdef __cplusplus
581 }
582 #endif
583 
584 #endif /* PERIPH_USBDEV_H */
585 
usb_ep_dir_t
usb_ep_dir_t
USB endpoint directions.
Definition: usb.h:204
usbdev_ep::num
uint8_t num
Endpoint number
Definition: usbdev.h:235
usbdev_event_cb_t
void(* usbdev_event_cb_t)(usbdev_t *usbdev, usbdev_event_t event)
Event callback for signaling usbdev event to upper layers.
Definition: usbdev.h:203
assert
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
assert.h
POSIX.1-2008 compliant version of the assert macro.
usbdev::epcb
usbdev_ep_event_cb_t epcb
Endpoint event callback for upper layer
Definition: usbdev.h:221
usb_ep_type_t
usb_ep_type_t
USB endpoint types.
Definition: usb.h:193
usbdev_driver::ep_esr
void(* ep_esr)(usbdev_ep_t *ep)
an endpoint's user-space event handler
Definition: usbdev.h:355
usbdev_set
static int usbdev_set(usbdev_t *dev, usbopt_t opt, const void *value, size_t value_len)
Set an option value for a given usb device.
Definition: usbdev.h:458
event
event structure
Definition: event.h:142
USBDEV_EVENT_RESUME
@ USBDEV_EVENT_RESUME
USB suspend condition no longer active.
Definition: usbdev.h:169
usbdev_ep_ready
static int usbdev_ep_ready(usbdev_ep_t *ep, size_t len)
Signal data buffer ready for data transmission.
Definition: usbdev.h:573
usbdev_esr
static void usbdev_esr(usbdev_t *dev)
a driver's user-space event service handler
Definition: usbdev.h:474
usbdev_driver_t
struct usbdev_driver usbdev_driver_t
usbdev driver functions
usbdev
usbdev device descriptor
Definition: usbdev.h:217
usbdev_ep_set
static int usbdev_ep_set(usbdev_ep_t *ep, usbopt_ep_t opt, const void *value, size_t value_len)
Set an option value for a given usb device endpoint.
Definition: usbdev.h:537
usbdev_ep::len
size_t len
Size of the data buffer in bytes
Definition: usbdev.h:232
usbdev_init
static void usbdev_init(usbdev_t *dev)
Initialize the USB peripheral device.
Definition: usbdev.h:393
usbopt.h
Definition of global USB peripheral and USB peripheral endpoint configuration options.
USBDEV_EVENT_TR_COMPLETE
@ USBDEV_EVENT_TR_COMPLETE
Transaction completed event.
Definition: usbdev.h:178
usbdev_driver::init
void(* init)(usbdev_t *usbdev)
Initialize the USB peripheral device.
Definition: usbdev.h:253
usbdev::cb
usbdev_event_cb_t cb
Event callback supplied by upper layer
Definition: usbdev.h:219
usbdev_ep_esr
static void usbdev_ep_esr(usbdev_ep_t *ep)
an endpoint's user-space event handler
Definition: usbdev.h:555
USBDEV_EVENT_TR_FAIL
@ USBDEV_EVENT_TR_FAIL
Transaction fail event.
Definition: usbdev.h:193
USBDEV_EVENT_SUSPEND
@ USBDEV_EVENT_SUSPEND
USB suspend condition active.
Definition: usbdev.h:164
usbdev_ep
usbdev endpoint descriptor
Definition: usbdev.h:229
USBDEV_EVENT_RESET
@ USBDEV_EVENT_RESET
Line reset occurred.
Definition: usbdev.h:152
usbopt_t
usbopt_t
List of configuration settings for USB peripherals.
Definition: usbopt.h:37
USBDEV_EVENT_ESR
@ USBDEV_EVENT_ESR
Driver needs it's ESR (event service routine) handled.
Definition: usbdev.h:124
usbdev_ep::dev
usbdev_t * dev
USB device this endpoint belongs to
Definition: usbdev.h:230
usb.h
Definition of global compile time configuration options.
usbdev_driver
usbdev driver functions
Definition: usbdev.h:244
USBDEV_EVENT_TR_STALL
@ USBDEV_EVENT_TR_STALL
Transaction stall event.
Definition: usbdev.h:186
usbdev_driver::set
int(* set)(usbdev_t *usbdev, usbopt_t opt, const void *value, size_t value_len)
Set an option value for a given usb device.
Definition: usbdev.h:296
USBDEV_EVENT_HOST_CONNECT
@ USBDEV_EVENT_HOST_CONNECT
Host connection detected.
Definition: usbdev.h:132
usbdev_driver::ready
int(* ready)(usbdev_ep_t *ep, size_t len)
Signal data buffer ready for data transmission.
Definition: usbdev.h:365
usbdev_driver::ep_init
void(* ep_init)(usbdev_ep_t *ep)
Initialize the USB endpoint.
Definition: usbdev.h:317
usbdev_event_t
usbdev_event_t
List of event types that can be send from the device driver to the upper layer.
Definition: usbdev.h:120
usbdev_driver::ep_set
int(* ep_set)(usbdev_ep_t *ep, usbopt_ep_t opt, const void *value, size_t value_len)
Set an option value for a given usb device endpoint.
Definition: usbdev.h:344
usbdev_init_lowlevel
void usbdev_init_lowlevel(void)
Low level USB peripheral driver initialization.
usbdev_get_ctx
usbdev_t * usbdev_get_ctx(unsigned num)
Retrieve usbdev context from the peripheral.
usbdev_get
static int usbdev_get(usbdev_t *dev, usbopt_t opt, void *value, size_t max_len)
Get an option value from a given usb device.
Definition: usbdev.h:436
usbdev_ep_get
static int usbdev_ep_get(usbdev_ep_t *ep, usbopt_ep_t opt, void *value, size_t max_len)
Get an option value from a given usb device endpoint.
Definition: usbdev.h:513
usbdev_ep_init
static void usbdev_ep_init(usbdev_ep_t *ep)
Initialize the USB endpoint.
Definition: usbdev.h:490
usbdev_ep::dir
usb_ep_dir_t dir
Endpoint direction
Definition: usbdev.h:233
USBDEV_EVENT_SOF
@ USBDEV_EVENT_SOF
Start of Frame received.
Definition: usbdev.h:159
usbdev::driver
const struct usbdev_driver * driver
usbdev driver struct
Definition: usbdev.h:218
usbdev_driver::esr
void(* esr)(usbdev_t *dev)
a driver's user-space event service handler
Definition: usbdev.h:307
usbdev_driver::get
int(* get)(usbdev_t *usbdev, usbopt_t opt, void *value, size_t max_len)
Get an option value from a given usb device.
Definition: usbdev.h:282
usbopt_ep_t
usbopt_ep_t
List of configuration settings for USB peripheral endpoints.
Definition: usbopt.h:82
usbdev::context
void * context
Ptr to the thread context
Definition: usbdev.h:223
usbdev_ep::buf
uint8_t * buf
Ptr to the data buffer
Definition: usbdev.h:231
USBDEV_EVENT_HOST_DISCONNECT
@ USBDEV_EVENT_HOST_DISCONNECT
Host disconnected from the device.
Definition: usbdev.h:140
usbdev_ep_event_cb_t
void(* usbdev_ep_event_cb_t)(usbdev_ep_t *ep, usbdev_event_t event)
Event callback for signaling endpoint events to upper layers.
Definition: usbdev.h:211
usbdev_driver::new_ep
usbdev_ep_t *(* new_ep)(usbdev_t *dev, usb_ep_type_t type, usb_ep_dir_t dir, size_t buf_len)
Retrieve an USB endpoint of the specified type.
Definition: usbdev.h:269
usbdev_new_ep
static usbdev_ep_t * usbdev_new_ep(usbdev_t *dev, usb_ep_type_t type, usb_ep_dir_t dir, size_t buf_len)
Retrieve an USB endpoint of the specified type.
Definition: usbdev.h:414
usbdev_driver::ep_get
int(* ep_get)(usbdev_ep_t *ep, usbopt_ep_t opt, void *value, size_t max_len)
Get an option value from a given usb device endpoint.
Definition: usbdev.h:330
usbdev_ep::type
usb_ep_type_t type
Endpoint type
Definition: usbdev.h:234