aboutsummaryrefslogtreecommitdiff
path: root/src/survive_usb.c
diff options
context:
space:
mode:
authorcnlohr <lohr85@gmail.com>2016-11-30 02:10:28 -0500
committercnlohr <lohr85@gmail.com>2016-11-30 02:10:28 -0500
commit1d0a3ef8b28e212eda4735da66f2f26e6c1a7cc5 (patch)
tree855a316ba979742543dd5122faaa80c7b50ba498 /src/survive_usb.c
parent8388ca81fbc6c0a840e81f6fd805b897d378556b (diff)
downloadlibsurvive-1d0a3ef8b28e212eda4735da66f2f26e6c1a7cc5.tar.gz
libsurvive-1d0a3ef8b28e212eda4735da66f2f26e6c1a7cc5.tar.bz2
I'm learning.
Diffstat (limited to 'src/survive_usb.c')
-rw-r--r--src/survive_usb.c67
1 files changed, 37 insertions, 30 deletions
diff --git a/src/survive_usb.c b/src/survive_usb.c
index 704b4b5..0958070 100644
--- a/src/survive_usb.c
+++ b/src/survive_usb.c
@@ -19,30 +19,31 @@ const char * devnames[] = {
static void handle_transfer(struct libusb_transfer* transfer) {
struct SurviveUSBInterface * iface = transfer->user_data;
+ struct SurviveContext * ctx = iface->ctx;
- if( transfer->status != LIBUSB_TRANSFER_COMPLETED )
+ if( transfer->status != LIBUSB_TRANSFER_COMPLETED )
{
SV_ERROR("Transfer problem %d with %s", transfer->status, iface->hname );
SV_KILL();
- return;
- }
+ return;
+ }
iface->actual_len = transfer->actual_length;
iface->cb( iface );
- if( libusb_submit_transfer(transfer) )
+ if( libusb_submit_transfer(transfer) )
{
- SV_ERROR( "Error resubmitting transfer for %s\n", iface->hname );
+ SV_ERROR( "Error resubmitting transfer for %s", iface->hname );
SV_KILL();
}
}
-static int AttachInterface( struct SurviveContext * t, int which_interface_am_i, libusb_device_handle * devh, int endpoint, usb_callback cb, const char * hname )
+static int AttachInterface( struct SurviveContext * ctx, int which_interface_am_i, libusb_device_handle * devh, int endpoint, usb_callback cb, const char * hname )
{
- struct SurviveUSBInterface * iface = &t->uiface[which_interface_am_i];
- iface->ctx = t;
+ struct SurviveUSBInterface * iface = &ctx->uiface[which_interface_am_i];
+ iface->ctx = ctx;
iface->which_interface_am_i = which_interface_am_i;
iface->hname = hname;
struct libusb_transfer * tx = iface->transfer = libusb_alloc_transfer(0);
@@ -51,7 +52,7 @@ static int AttachInterface( struct SurviveContext * t, int which_interface_am_i,
if (!iface->transfer)
{
- SV_ERROR( "Error: failed on libusb_alloc_transfer for %s\n", hname );
+ SV_ERROR( "Error: failed on libusb_alloc_transfer for %s", hname );
return 4;
}
@@ -60,15 +61,15 @@ static int AttachInterface( struct SurviveContext * t, int which_interface_am_i,
int rc = libusb_submit_transfer( tx );
if( rc )
{
- SV_ERROR( "Error: Could not submit transfer for %s (Code %d)\n", hname, rc );
+ SV_ERROR( "Error: Could not submit transfer for %s (Code %d)", hname, rc );
return 6;
}
return 0;
}
-
-void debug_cb( struct SurviveUSBInterface * si )
+/*
+static void debug_cb( struct SurviveUSBInterface * si )
{
int i;
int len = si->actual_len;
@@ -78,11 +79,11 @@ void debug_cb( struct SurviveUSBInterface * si )
printf( "%02x ", si->buffer[i] );
}
printf( "\n" );
-}
+}*/
-int survive_usb_init( struct SurviveContext * t )
+int survive_usb_init( struct SurviveContext * ctx )
{
- int r = libusb_init( &t->usbctx );
+ int r = libusb_init( &ctx->usbctx );
if( r )
{
SV_ERROR( "libusb fault %d\n", r );
@@ -91,11 +92,11 @@ int survive_usb_init( struct SurviveContext * t )
int i;
libusb_device** devs;
- int ret = libusb_get_device_list(t->usbctx, &devs);
+ int ret = libusb_get_device_list(ctx->usbctx, &devs);
if( ret < 0 )
{
- SV_ERROR( "Couldn't get list of USB devices %d\n", ret );
+ SV_ERROR( "Couldn't get list of USB devices %d", ret );
return ret;
}
@@ -125,39 +126,45 @@ int survive_usb_init( struct SurviveContext * t )
}
}
+ if( d == 0 )
+ {
+ SV_ERROR( "Couldn't find device %s (%04x:%04x.%d)", devnames[i], vid, pid, which );
+ return -99;
+ }
+
struct libusb_config_descriptor *conf;
ret = libusb_get_config_descriptor(d, 0, &conf);
if( ret )
continue;
- ret = libusb_open(d, &t->udev[i]);
+ ret = libusb_open(d, &ctx->udev[i]);
- if( !t->udev[i] || ret )
+ if( !ctx->udev[i] || ret )
{
- SV_ERROR( "Error: cannot open device \"%s\" with vid/pid %04x:%04x\n", devnames[i], vid, pid );
+ SV_ERROR( "Error: cannot open device \"%s\" with vid/pid %04x:%04x", devnames[i], vid, pid );
return -5;
}
- libusb_set_auto_detach_kernel_driver( t->udev[i], 1 );
+ libusb_set_auto_detach_kernel_driver( ctx->udev[i], 1 );
for (int j = 0; j < conf->bNumInterfaces; j++ )
{
- if( libusb_claim_interface(t->udev[i], j) )
+ if( libusb_claim_interface(ctx->udev[i], j) )
{
- SV_ERROR( "Could not claim interface %d of %s\n", j, devnames[i] );
+ SV_ERROR( "Could not claim interface %d of %s", j, devnames[i] );
return -9;
}
}
- SV_INFO( "Successfully enumerated %s (%d, %d)\n", devnames[i], did, conf->bNumInterfaces );
+ SV_INFO( "Successfully enumerated %s (%d, %d)", devnames[i], did, conf->bNumInterfaces );
}
libusb_free_device_list( devs, 1 );
- if( AttachInterface( t, USB_IF_HMD, t->udev[USB_DEV_HMD], 0x81, debug_cb, "Mainboard" ) ) { return -6; }
- if( AttachInterface( t, USB_IF_LIGHTHOUSE, t->udev[USB_DEV_LIGHTHOUSE], 0x81, debug_cb, "Lighthouse" ) ) { return -6; }
- if( AttachInterface( t, USB_IF_WATCHMAN1, t->udev[USB_DEV_WATCHMAN1], 0x81, debug_cb, "Watchman 1" ) ) { return -6; }
- if( AttachInterface( t, USB_IF_WATCHMAN2, t->udev[USB_DEV_WATCHMAN2], 0x81, debug_cb, "Watchman 2" ) ) { return -6; }
- if( AttachInterface( t, USB_IF_LIGHTCAP, t->udev[USB_DEV_LIGHTHOUSE], 0x82, debug_cb, "Lightcap" ) ) { return -6; }
+ if( AttachInterface( ctx, USB_IF_HMD, ctx->udev[USB_DEV_HMD], 0x81, survive_data_cb, "Mainboard" ) ) { return -6; }
+ if( AttachInterface( ctx, USB_IF_LIGHTHOUSE, ctx->udev[USB_DEV_LIGHTHOUSE], 0x81, survive_data_cb, "Lighthouse" ) ) { return -7; }
+ if( AttachInterface( ctx, USB_IF_WATCHMAN1, ctx->udev[USB_DEV_WATCHMAN1], 0x81, survive_data_cb, "Watchman 1" ) ) { return -8; }
+ if( AttachInterface( ctx, USB_IF_WATCHMAN2, ctx->udev[USB_DEV_WATCHMAN2], 0x81, survive_data_cb, "Watchman 2" ) ) { return -9; }
+ if( AttachInterface( ctx, USB_IF_LIGHTCAP, ctx->udev[USB_DEV_LIGHTHOUSE], 0x82, survive_data_cb, "Lightcap" ) ) { return -10; }
SV_INFO( "All devices attached.\n" );
@@ -182,7 +189,7 @@ int survive_usb_poll( struct SurviveContext * ctx )
int r = libusb_handle_events( ctx->usbctx );
if( r )
{
- SV_ERROR( "Libusb poll failed.\n" );
+ SV_ERROR( "Libusb poll failed." );
}
return r;
}