Home
Support

Knowledge Base

BSPs and drivers
Community resources
Product documentation
Questions?
Contact us

USB Insertion/Removal Notifications
 
/*
Compile with
qcc -w9 -otst tst.c -lusbdi
*/

#ifdef __USAGE
%C - listen for USB insertion / removal events

%C
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include <sys/usbdi.h>

/*
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).
*/

static void InsertCb( struct usbd_connection *conn, usbd_device_instance_t *inst ) {
printf( "--- Insertion ---\n" );
printf( " Path: %02x\n", inst->path );
printf( " Devno: %02x\n", inst->devno );
printf( " Generation: %04x\n", inst->generation );
printf( " Vendor: %08x\n", inst->ident.vendor );
printf( " Device: %08x\n", inst->ident.device );
printf( " Class: %08x\n", inst->ident.dclass );
printf( " Subclass: %08x\n", inst->ident.subclass );
printf( " Protocol: %08x\n", inst->ident.protocol );
printf( " Config: %08x\n", inst->config );
printf( " Iface: %08x\n", inst->iface );
printf( " Alternate: %08x\n\n", inst->alternate );
}

static void RemoveCb( struct usbd_connection *conn, usbd_device_instance_t *inst ) {
printf( "--- Removal ---\n" );
printf( " Path: %02x\n", inst->path );
printf( " Devno: %02x\n", inst->devno );
printf( " Generation: %04x\n", inst->generation );
printf( " Vendor: %08x\n", inst->ident.vendor );
printf( " Device: %08x\n", inst->ident.device );
printf( " Class: %08x\n", inst->ident.dclass );
printf( " Subclass: %08x\n", inst->ident.subclass );
printf( " Protocol: %08x\n", inst->ident.protocol );
printf( " Config: %08x\n", inst->config );
printf( " Iface: %08x\n", inst->iface );
printf( " Alternate: %08x\n\n", inst->alternate );
}

int main( int argc, char *argv[] ) {
struct usbd_connection *conn;
usbd_device_ident_t idents;
usbd_connect_parm_t parms;
usbd_funcs_t funcs;

idents.vendor = USBD_CONNECT_WILDCARD;
idents.device = USBD_CONNECT_WILDCARD;
idents.dclass = USBD_CONNECT_WILDCARD;
idents.subclass = USBD_CONNECT_WILDCARD;
idents.protocol = USBD_CONNECT_WILDCARD;

funcs.nentries = _USBDI_NFUNCS;
funcs.insertion = InsertCb;
funcs.removal = RemoveCb;
funcs.event = NULL;

parms.path = NULL;
parms.vusb = USB_VERSION;
parms.vusbd = USBD_VERSION;
parms.flags = 0;
parms.argc = argc;
parms.argv = argv;
parms.evtbufsz = 0;
parms.ident = &idents;
parms.funcs = &funcs;
parms.connect_wait = USBD_CONNECT_WAIT;

if ( EOK != ( errno = usbd_connect( &parms, &conn ) ) )
err( EXIT_FAILURE, "usbd_connect()" );

for ( ; ; )
sleep( 60 );

return EXIT_SUCCESS;
}


Related Attachments
 Attachment Name Size Last Modified
 usb_notify.c 3KB 10/30/2012 8:01 AM





Please contact us with your questions or concerns.