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 
203 typedef void (*usbdev_event_cb_t)(usbdev_t *usbdev, usbdev_event_t event);
204 
211 typedef void (*usbdev_ep_event_cb_t)(usbdev_ep_t *ep,
212  usbdev_event_t event);
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 
uint8_t num
Endpoint number.
Definition: usbdev.h:235
uint8_t * buf
Ptr to the data buffer.
Definition: usbdev.h:231
static void usbdev_esr(usbdev_t *dev)
a driver&#39;s user-space event service handler
Definition: usbdev.h:474
usb_ep_type_t
USB endpoint types.
Definition: usb.h:123
static int usbdev_ep_ready(usbdev_ep_t *ep, size_t len)
Signal data buffer ready for data transmission.
Definition: usbdev.h:573
usbdev_ep_event_cb_t epcb
Endpoint event callback for upper layer.
Definition: usbdev.h:221
static void usbdev_ep_esr(usbdev_ep_t *ep)
an endpoint&#39;s user-space event handler
Definition: usbdev.h:555
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
struct usbdev_driver usbdev_driver_t
usbdev driver functions
void(* esr)(usbdev_t *dev)
a driver&#39;s user-space event service handler
Definition: usbdev.h:307
void(* init)(usbdev_t *usbdev)
Initialize the USB peripheral device.
Definition: usbdev.h:253
void * context
Ptr to the thread context.
Definition: usbdev.h:223
static void usbdev_init(usbdev_t *dev)
Initialize the USB peripheral device.
Definition: usbdev.h:393
size_t len
Size of the data buffer in bytes.
Definition: usbdev.h:232
event structure
Definition: event.h:139
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
POSIX.1-2008 compliant version of the assert macro.
Transaction fail event.
Definition: usbdev.h:193
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
Driver needs it&#39;s ESR (event service routine) handled.
Definition: usbdev.h:124
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
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_event_cb_t cb
Event callback supplied by upper layer.
Definition: usbdev.h:219
static void usbdev_ep_init(usbdev_ep_t *ep)
Initialize the USB endpoint.
Definition: usbdev.h:490
USB suspend condition active.
Definition: usbdev.h:164
Start of Frame received.
Definition: usbdev.h:159
usbdev_t * dev
USB device this endpoint belongs to.
Definition: usbdev.h:230
usbdev_t * usbdev_get_ctx(unsigned num)
Retrieve usbdev context from the peripheral.
usb_ep_dir_t dir
Endpoint direction.
Definition: usbdev.h:233
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 driver functions
Definition: usbdev.h:244
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
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
Transaction stall event.
Definition: usbdev.h:186
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
void(* ep_init)(usbdev_ep_t *ep)
Initialize the USB endpoint.
Definition: usbdev.h:317
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
int(* ready)(usbdev_ep_t *ep, size_t len)
Signal data buffer ready for data transmission.
Definition: usbdev.h:365
Transaction completed event.
Definition: usbdev.h:178
void(* ep_esr)(usbdev_ep_t *ep)
an endpoint&#39;s user-space event handler
Definition: usbdev.h:355
usbdev_event_t
List of event types that can be send from the device driver to the upper layer.
Definition: usbdev.h:120
const struct usbdev_driver * driver
usbdev driver struct
Definition: usbdev.h:218
usb_ep_dir_t
USB endpoint directions.
Definition: usb.h:134
void usbdev_init_lowlevel(void)
Low level USB peripheral driver initialization.
Definition of global compile time configuration options.
Line reset occurred.
Definition: usbdev.h:152
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
usbopt_ep_t
List of configuration settings for USB peripheral endpoints.
Definition: usbopt.h:82
usbdev endpoint descriptor
Definition: usbdev.h:229
usbdev device descriptor
Definition: usbdev.h:217
Host disconnected from the device.
Definition: usbdev.h:140
Definition of global USB peripheral and USB peripheral endpoint configuration options.
Host connection detected.
Definition: usbdev.h:132
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
usb_ep_type_t type
Endpoint type.
Definition: usbdev.h:234
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
usbopt_t
List of configuration settings for USB peripherals.
Definition: usbopt.h:37
USB suspend condition no longer active.
Definition: usbdev.h:169