Skip to content

Commit 650ab53

Browse files
author
Josuah Demangeon
committed
usb: host: introduce wrappers to access the class function pointers
Add API wrappers around the function pointers in struct usbh_class_api, while also documenting the USB host class internal API. Signed-off-by: Josuah Demangeon <[email protected]>
1 parent 5d2d132 commit 650ab53

File tree

2 files changed

+162
-11
lines changed

2 files changed

+162
-11
lines changed

include/zephyr/usb/usbh.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,17 @@ struct usbh_class_data;
9090
* @brief USB host class instance API
9191
*/
9292
struct usbh_class_api {
93-
/** Initialization of the class implementation */
94-
int (*init)(struct usbh_class_data *const c_data);
93+
/** Host init handler, before any device is connected */
94+
int (*init)(struct usbh_class_data *const c_data,
95+
struct usbh_context *const uhs_ctx);
9596
/** Request completion event handler */
96-
int (*request)(struct usbh_class_data *const c_data,
97-
struct uhc_transfer *const xfer, int err);
98-
/** Device connected handler */
99-
int (*connected)(struct usbh_class_data *const c_data,
100-
void *const desc_start_addr,
101-
void *const desc_end_addr);
102-
/** Device removed handler */
97+
int (*completion_cb)(struct usbh_class_data *const c_data,
98+
struct uhc_transfer *const xfer);
99+
/** Device connection handler */
100+
int (*probe)(struct usbh_class_data *const c_data,
101+
struct usb_device *const udev,
102+
const uint8_t iface);
103+
/** Device removal handler */
103104
int (*removed)(struct usbh_class_data *const c_data);
104105
/** Bus suspended handler (optional) */
105106
int (*suspended)(struct usbh_class_data *const c_data);
@@ -115,8 +116,10 @@ struct usbh_class_data {
115116
const char *name;
116117
/** Pointer to USB host stack context structure */
117118
struct usbh_context *uhs_ctx;
118-
/** Class code supported by this instance */
119-
struct usbh_code_triple code;
119+
/** Pointer to USB device this class is used for */
120+
struct usb_device *udev;
121+
/** Interface number for which this class matched */
122+
uint8_t iface;
120123
/** Pointer to host support class API */
121124
struct usbh_class_api *api;
122125
/** Pointer to private data */

subsys/usb/host/usbh_class_api.h

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* Copyright Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @file
9+
* @brief USB host stack class instances API
10+
*
11+
* This file contains the USB host stack class instances API.
12+
*/
13+
14+
#ifndef ZEPHYR_INCLUDE_USBH_CLASS_API_H
15+
#define ZEPHYR_INCLUDE_USBH_CLASS_API_H
16+
17+
#include <zephyr/usb/usbh.h>
18+
19+
/**
20+
* @brief Initialization of the class implementation
21+
*
22+
* This is called for each instance during the initialization phase,
23+
* for every registered class.
24+
* It can be used to initialize underlying systems.
25+
*
26+
* @param[in] c_data Pointer to USB host class data
27+
* @param[in] uhs_ctx USB host context to assign to this class
28+
* @return 0 on success, negative error code on failure.
29+
*/
30+
static inline int usbh_class_init(struct usbh_class_data *const c_data,
31+
struct usbh_context *const uhs_ctx)
32+
{
33+
const struct usbh_class_api *api = c_data->api;
34+
35+
if (api->init != NULL) {
36+
return api->init(c_data, uhs_ctx);
37+
}
38+
39+
return -ENOTSUP;
40+
}
41+
42+
/**
43+
* @brief Request completion event handler
44+
*
45+
* Called upon completion of a request made by the host to this class.
46+
*
47+
* @param[in] c_data Pointer to USB host class data
48+
* @param[in] xfer Completed transfer
49+
* @return 0 on success, negative error code on failure.
50+
*/
51+
static inline int usbh_class_completion_cb(struct usbh_class_data *const c_data,
52+
struct uhc_transfer *const xfer)
53+
{
54+
const struct usbh_class_api *api = c_data->api;
55+
56+
if (api->completion_cb != NULL) {
57+
return api->completion_cb(c_data, xfer);
58+
}
59+
60+
return -ENOTSUP;
61+
}
62+
63+
/**
64+
* @brief Device initialization handler
65+
*
66+
* Called when a device is connected to the bus.
67+
* It is called once for every USB function of that device.
68+
*
69+
* @param[in] c_data Pointer to USB host class data
70+
* @param[in] udev USB device connected
71+
* @param[in] iface The @c bInterfaceNumber or @c bFirstInterface of this class
72+
* @return 0 on success, negative error code on failure.
73+
* @return -ENOTSUP if the class is not matching
74+
*/
75+
static inline int usbh_class_probe(struct usbh_class_data *const c_data,
76+
struct usb_device *const udev,
77+
const uint8_t iface)
78+
{
79+
const struct usbh_class_api *api = c_data->api;
80+
81+
if (api->probe != NULL) {
82+
return api->probe(c_data, udev, iface);
83+
}
84+
85+
return -ENOTSUP;
86+
}
87+
88+
/**
89+
* @brief Device removed handler
90+
*
91+
* Called when the device is removed from the bus
92+
* and it matches the class filters of this instance.
93+
*
94+
* @param[in] c_data Pointer to USB host class data
95+
* @return 0 on success, negative error code on failure.
96+
*/
97+
static inline int usbh_class_removed(struct usbh_class_data *const c_data)
98+
{
99+
const struct usbh_class_api *api = c_data->api;
100+
101+
if (api->removed != NULL) {
102+
return api->removed(c_data);
103+
}
104+
105+
return -ENOTSUP;
106+
}
107+
108+
/**
109+
* @brief Bus suspended handler
110+
*
111+
* Called when the host has suspended the bus.
112+
* It can be used to suspend underlying systems.
113+
*
114+
* @param[in] c_data Pointer to USB host class data
115+
* @return 0 on success, negative error code on failure.
116+
*/
117+
static inline int usbh_class_suspended(struct usbh_class_data *const c_data)
118+
{
119+
const struct usbh_class_api *api = c_data->api;
120+
121+
if (api->suspended != NULL) {
122+
return api->suspended(c_data);
123+
}
124+
125+
return 0;
126+
}
127+
128+
/**
129+
* @brief Bus resumed handler
130+
*
131+
* Called when the host resumes its activity on the bus.
132+
* It can be used to wake-up underlying systems.
133+
*
134+
* @param[in] c_data Pointer to USB host class data
135+
* @return 0 on success, negative error code on failure.
136+
*/
137+
static inline int usbh_class_resumed(struct usbh_class_data *const c_data)
138+
{
139+
const struct usbh_class_api *api = c_data->api;
140+
141+
if (api->resumed != NULL) {
142+
return api->resumed(c_data);
143+
}
144+
145+
return 0;
146+
}
147+
148+
#endif /* ZEPHYR_INCLUDE_USBD_CLASS_API_H */

0 commit comments

Comments
 (0)