usb: api: Add user device status callback

By this commit user gets possibility to register USB
device satutus callback. This callback represents device state
and is added so user could know what happend to USB device.

Callback is registered by providing it to usb_enable()
USB api is extended by this callback handler.

Samples using using USB are by default provide no callback
and the usb_enable() is called with NULL parameter.

Status callback registered by hid class is deleted as now
USB device has global callback for all classes within device.

Signed-off-by: Emil Obalski <emil.obalski@nordicsemi.no>
This commit is contained in:
Emil Obalski 2019-11-05 15:24:45 +01:00 committed by Carles Cufí
commit 2128750138
18 changed files with 36 additions and 27 deletions

View file

@ -599,7 +599,7 @@ static int uart_console_init(struct device *arg)
#if defined(CONFIG_USB_UART_CONSOLE) && defined(CONFIG_USB_UART_DTR_WAIT) #if defined(CONFIG_USB_UART_CONSOLE) && defined(CONFIG_USB_UART_DTR_WAIT)
int ret; int ret;
ret = usb_enable(); ret = usb_enable(NULL);
if (ret != 0) { if (ret != 0) {
return ret; return ret;
} }

View file

@ -82,7 +82,6 @@ struct hid_ops {
#ifdef CONFIG_ENABLE_HID_INT_OUT_EP #ifdef CONFIG_ENABLE_HID_INT_OUT_EP
hid_int_ready_callback int_out_ready; hid_int_ready_callback int_out_ready;
#endif #endif
usb_dc_status_callback status_cb;
}; };
/* HID Report Definitions */ /* HID Report Definitions */

View file

@ -227,9 +227,12 @@ int usb_deconfig(void);
* before invoking this, so that any data or events on the bus are processed * before invoking this, so that any data or events on the bus are processed
* correctly by the associated class handling code. * correctly by the associated class handling code.
* *
* @param[in] status_cb Callback registered by user to notify
* about USB device controller state.
*
* @return 0 on success, negative errno code on fail. * @return 0 on success, negative errno code on fail.
*/ */
int usb_enable(void); int usb_enable(usb_dc_status_callback status_cb);
/** /**
* @brief Disable the USB device * @brief Disable the USB device

View file

@ -12,7 +12,7 @@ void main(void)
{ {
int ret; int ret;
ret = usb_enable(); ret = usb_enable(NULL);
if (ret != 0) { if (ret != 0) {
printk("Failed to enable USB"); printk("Failed to enable USB");
return; return;

View file

@ -98,7 +98,7 @@ int usb_transport_init(usb_transport_receive_callback_t callback)
receive_data_cb = callback; receive_data_cb = callback;
ret = usb_enable(); ret = usb_enable(NULL);
if (ret != 0) { if (ret != 0) {
LOG_ERR("Failed to enable USB"); LOG_ERR("Failed to enable USB");
return; return;

View file

@ -247,7 +247,7 @@ void main(void)
usb_hid_register_device(hid_dev, hid_report_desc, usb_hid_register_device(hid_dev, hid_report_desc,
sizeof(hid_report_desc), NULL); sizeof(hid_report_desc), NULL);
ret = usb_enable(); ret = usb_enable(NULL);
if (ret != 0) { if (ret != 0) {
LOG_ERR("Failed to enable USB"); LOG_ERR("Failed to enable USB");
return; return;

View file

@ -82,7 +82,7 @@ void main(void)
return; return;
} }
ret = usb_enable(); ret = usb_enable(NULL);
if (ret != 0) { if (ret != 0) {
LOG_ERR("Failed to enable USB"); LOG_ERR("Failed to enable USB");
return; return;

View file

@ -131,7 +131,7 @@ void main(void)
return; return;
} }
ret = usb_enable(); ret = usb_enable(NULL);
if (ret != 0) { if (ret != 0) {
LOG_ERR("Failed to enable USB"); LOG_ERR("Failed to enable USB");
return; return;

View file

@ -16,7 +16,7 @@ void main(void)
{ {
int ret; int ret;
ret = usb_enable(); ret = usb_enable(NULL);
if (ret != 0) { if (ret != 0) {
LOG_ERR("Failed to enable USB"); LOG_ERR("Failed to enable USB");
return; return;

View file

@ -551,6 +551,11 @@ int callbacks_configure(struct device *gpio, u32_t pin, int flags,
return 0; return 0;
} }
static void status_cb(enum usb_dc_status_code status, const u8_t *param)
{
LOG_INF("Status %d", status);
}
void main(void) void main(void)
{ {
int ret; int ret;
@ -626,7 +631,7 @@ void main(void)
usb_hid_init(hid0_dev); usb_hid_init(hid0_dev);
usb_hid_init(hid1_dev); usb_hid_init(hid1_dev);
ret = usb_enable(); ret = usb_enable(status_cb);
if (ret != 0) { if (ret != 0) {
LOG_ERR("Failed to enable USB"); LOG_ERR("Failed to enable USB");
return; return;

View file

@ -214,7 +214,6 @@ int callbacks_configure(struct device *gpio, u32_t pin, int flags,
void main(void) void main(void)
{ {
int ret; int ret;
u8_t report[4] = { 0x00 }; u8_t report[4] = { 0x00 };
u8_t toggle = 0U; u8_t toggle = 0U;
struct device *led_dev, *hid_dev; struct device *led_dev, *hid_dev;
@ -263,16 +262,13 @@ void main(void)
} }
#endif #endif
static const struct hid_ops ops = {
.status_cb = status_cb
};
usb_hid_register_device(hid_dev, usb_hid_register_device(hid_dev,
hid_report_desc, sizeof(hid_report_desc), hid_report_desc, sizeof(hid_report_desc),
&ops); NULL);
usb_hid_init(hid_dev); usb_hid_init(hid_dev);
ret = usb_enable(); ret = usb_enable(status_cb);
if (ret != 0) { if (ret != 0) {
LOG_ERR("Failed to enable USB"); LOG_ERR("Failed to enable USB");
return; return;

View file

@ -109,7 +109,6 @@ static void protocol_cb(u8_t protocol)
static const struct hid_ops ops = { static const struct hid_ops ops = {
.int_in_ready = in_ready_cb, .int_in_ready = in_ready_cb,
.status_cb = status_cb,
.on_idle = idle_cb, .on_idle = idle_cb,
.protocol_change = protocol_cb, .protocol_change = protocol_cb,
}; };
@ -120,7 +119,7 @@ void main(void)
LOG_DBG("Starting application"); LOG_DBG("Starting application");
ret = usb_enable(); ret = usb_enable(status_cb);
if (ret != 0) { if (ret != 0) {
LOG_ERR("Failed to enable USB"); LOG_ERR("Failed to enable USB");
return; return;

View file

@ -30,7 +30,7 @@ void main(void)
{ {
int ret; int ret;
ret = usb_enable(); ret = usb_enable(NULL);
if (ret != 0) { if (ret != 0) {
LOG_ERR("Failed to enable USB"); LOG_ERR("Failed to enable USB");
return; return;

View file

@ -13,7 +13,7 @@ void main(void)
{ {
int ret; int ret;
ret = usb_enable(); ret = usb_enable(NULL);
if (ret != 0) { if (ret != 0) {
LOG_ERR("Failed to enable USB"); LOG_ERR("Failed to enable USB");
return; return;

View file

@ -290,7 +290,7 @@ void main(void)
/* Set the custom and vendor request handlers */ /* Set the custom and vendor request handlers */
webusb_register_request_handlers(&req_handlers); webusb_register_request_handlers(&req_handlers);
ret = usb_enable(); ret = usb_enable(NULL);
if (ret != 0) { if (ret != 0) {
LOG_ERR("Failed to enable USB"); LOG_ERR("Failed to enable USB");
return; return;

View file

@ -376,9 +376,6 @@ static void hid_do_status_cb(struct hid_device_info *dev_data,
break; break;
} }
if (dev_data->ops && dev_data->ops->status_cb) {
dev_data->ops->status_cb(status, param);
}
} }
static void hid_status_cb(struct usb_cfg_data *cfg, static void hid_status_cb(struct usb_cfg_data *cfg,

View file

@ -131,7 +131,7 @@ static void netusb_init(struct net_if *iface)
LOG_DBG("netusb device initialization"); LOG_DBG("netusb device initialization");
ret = usb_enable(); ret = usb_enable(NULL);
if (ret != 0) { if (ret != 0) {
LOG_ERR("Failed to enable USB"); LOG_ERR("Failed to enable USB");
return; return;

View file

@ -150,8 +150,10 @@ static struct usb_dev_priv {
bool zlp_flag; bool zlp_flag;
/** Installed custom request handler */ /** Installed custom request handler */
usb_request_handler custom_req_handler; usb_request_handler custom_req_handler;
/** USB stack status clalback */ /** USB stack status callback */
usb_dc_status_callback status_callback; usb_dc_status_callback status_callback;
/** USB user status callback */
usb_dc_status_callback user_status_callback;
/** Pointer to registered descriptors */ /** Pointer to registered descriptors */
const u8_t *descriptors; const u8_t *descriptors;
/** Array of installed request handler callbacks */ /** Array of installed request handler callbacks */
@ -979,6 +981,10 @@ static void forward_status_cb(enum usb_dc_status_code status, const u8_t *param)
cfg->cb_usb_status(cfg, status, param); cfg->cb_usb_status(cfg, status, param);
} }
} }
if (usb_dev.user_status_callback) {
usb_dev.user_status_callback(status, param);
}
} }
/** /**
@ -1035,6 +1041,9 @@ int usb_deconfig(void)
/* unregister status callback */ /* unregister status callback */
usb_register_status_callback(NULL); usb_register_status_callback(NULL);
/* unregister user status callback */
usb_dev.user_status_callback = NULL;
/* Reset USB controller */ /* Reset USB controller */
usb_dc_reset(); usb_dc_reset();
@ -1533,7 +1542,7 @@ int usb_set_config(const u8_t *device_descriptor)
return 0; return 0;
} }
int usb_enable(void) int usb_enable(usb_dc_status_callback status_cb)
{ {
int ret; int ret;
u32_t i; u32_t i;
@ -1556,6 +1565,7 @@ int usb_enable(void)
goto out; goto out;
} }
usb_dev.user_status_callback = status_cb;
usb_register_status_callback(forward_status_cb); usb_register_status_callback(forward_status_cb);
usb_dc_set_status_callback(forward_status_cb); usb_dc_set_status_callback(forward_status_cb);