Connect a client driver to the USB stack
#include <sys/usbdi.h> int usbd_connect( usbd_connect_parm_t *parm, struct usbd_connection **connection );
libusbdi
You use the usbd_connect() function to connect to a USB device and to provide insertion/removal callbacks (in the usbd_connect_parm_t data structure).
typedef struct usbd_connect_parm { const char *path; uint16_t vusb; uint16_t vusbd; uint32_t flags; int argc; char **argv; uint32_t evtbufsz; usbd_device_ident_t *ident; usbd_funcs_t *funcs; uint16_t connect_wait } usbd_connect_parm_t;
typedef struct usbd_device_ident { uint32_t vendor; uint32_t device; uint32_t dclass; uint32_t subclass; uint32_t protocol; } usbd_device_ident_t;
You can set the fields to USBD_CONNECT_WILDCARD or to an explicit value. You would typically make the usbd_device_ident_t structure be a filter for devices you support from this specific class driver.
typedef struct usbd_funcs { uint32_t nentries; void (*insertion)(struct usbd_connection *, usbd_device_instance_t *instance); void (*removal)(struct usbd_connection *, usbd_device_instance_t *instance); void (*event)(struct usbd_connection *, usbd_device_instance_t *instance, uint16_t type); } usbd_funcs_t;
The usbd_funcs_t structure includes the following members:
By passing NULL as the usbd_funcs, you're saying that you're not interested in receiving dynamic insertion/removal notifications, which means that you won't be a fully operational class driver. No asynchronous I/O will be allowed, no event thread, etc. This approach is taken, for example, by the usb display utility. |
A class driver (in its main(), probably) for a 3COM Ethernet card might connect like this:
usbd_device_ident_t interest = { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250, USBD_CONNECT_WILDCARD, USBD_CONNECT_WILDCARD, USBD_CONNECT_WILDCARD, }; usbd_funcs_t funcs = { _USBDI_NFUNCS, insertion, removal, NULL }; usbd_connect_parm_t cparms = { NULL, USB_VERSION, USBD_VERSION, 0, argc, argv, 0, &interest, &funcs }; struct usbd_connection *connection; int error; error = usbd_connect(&cparms, &connection);
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
The usbd_connect() function creates a thread on your behalf that's used by the library to monitor the USB stack for device insertion or removal. Since your insertion and removal callback functions are called by this new thread, you must ensure that any common resources used between that thread and any other thread(s) in your class driver are properly protected (e.g. via a mutex).
usbd_args_lookup(), usbd_attach(), usbd_detach(), usbd_disconnect()