Skip to content

Commit

Permalink
Merge pull request #1 from nakj/if
Browse files Browse the repository at this point in the history
powerusb.c: add confirm device routine and detach kernel driver routine.
  • Loading branch information
nakj committed Mar 10, 2012
2 parents 3529310 + 647e58f commit f1c16b9
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion powerusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,70 @@ void send_cmd(struct libusb_device_handle *devh,int cmd)
int main(int argc, char **argv)
{
libusb_context *ctx = NULL;
libusb_device *dev;
libusb_device **devs;
struct libusb_device_handle *devh = NULL;
int r=1;
int i=0;
int cnt=0;


/* libusb initialize*/
if ((r = libusb_init(&ctx)) < 0) {
perror("libusb_init\n");
exit(1);
} else {
libusb_set_debug(ctx,3);
Dprintf("init done\n");
}

/* confirm powerusb device*/
if((libusb_get_device_list(ctx,&devs)) < 0) {
perror("no usb device found");
exit(1);
}
while((dev =devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
if (libusb_get_device_descriptor(dev,&desc) < 0) {
printf("failed to get device descriptor\n");
return 1;
}

if (desc.idVendor == USB_VENDOR_ID &&
desc.idProduct == USB_PRODUCT_ID) {
cnt++;
Dprintf("device found\n");
}
}
if (cnt == 0) {
fprintf(stderr,"device not connected\n");
exit(1);
}
if (cnt > 1) {
/* FIXME */
fprintf(stderr,"multi PowerUSB is not implemented yet\n");
exit(1);
}


/* open powerusb device */
if ((devh = libusb_open_device_with_vid_pid(ctx,USB_VENDOR_ID,USB_PRODUCT_ID)) < 0 ) {
perror("can't find PowerUSB device\n");
goto out;
} else {
Dprintf("device opened\n");
}

/* detach kernel driver if attached. */
r = libusb_kernel_driver_active(devh,0);
//Dprintf("%d\n",r);
if (r == 1) {
r = libusb_detach_kernel_driver(devh,0);
if (r != 0) {
perror("detaching kernel driver failed");
exit(1);
}
}

send_cmd(devh,CMD_GET_MODEL);
send_cmd(devh,CMD_GET_FIRM_VER);
send_cmd(devh,CMD_GET_STATE1);
Expand Down

0 comments on commit f1c16b9

Please sign in to comment.