Kobil_mIDentity_switch.c [plain text]
#include <stdio.h>
#include <string.h>
#include <usb.h>
#include <errno.h>
#include "config.h"
#define KOBIL_VENDOR_ID 0x0D46
#define MID_DEVICE_ID 0x4081
#define KOBIL_TIMEOUT 5000
#define VAL_STARTUP_4080 1
#define VAL_STARTUP_4000 2
#define VAL_STARTUP_4020 3
#define VAL_STARTUP_40A0 4
#define HIDCMD_SWITCH_DEVICE 0x0004
#define bmRequestType 0x22
#define bRequest 0x09
#define wValue 0x0200
#define wIndex 0x0002
static int kobil_midentity_control_msg(usb_dev_handle *usb)
{
int ret;
char switchCmd[10];
unsigned char Sleep = 1;
unsigned char hardDisk = 1;
unsigned char param = ((hardDisk)<<4) | (Sleep);
memset(switchCmd, 0x0, sizeof(switchCmd));
switchCmd[0] = HIDCMD_SWITCH_DEVICE>>8;
switchCmd[1] = HIDCMD_SWITCH_DEVICE;
switchCmd[5] = VAL_STARTUP_4000;
switchCmd[9] = param;
ret = usb_control_msg(usb, bmRequestType, bRequest, wValue, wIndex,
switchCmd, sizeof(switchCmd), KOBIL_TIMEOUT);
return(!(ret==sizeof(switchCmd)));
}
static int kobil_midentity_claim_interface(usb_dev_handle *usb, int ifnum)
{
int rv;
printf("claiming interface #%d ...\n", ifnum);
rv = usb_claim_interface(usb, ifnum);
if (rv == 0)
{
printf("success\n");
return(rv);
}
#ifdef HAVE_USB_DETACH_KERNEL_DRIVER_NP
printf("failed with error %d, trying to detach kernel driver ....\n", rv);
rv = usb_detach_kernel_driver_np(usb, ifnum);
if (rv == 0)
{
printf("success, claiming interface again ...");
rv = usb_claim_interface(usb, ifnum);
if (rv == 0)
{
printf("success\n");
return(rv);
}
}
#endif
printf("failed with error %d, giving up.\n", rv);
return(rv);
}
int main(int argc, char *argv[])
{
struct usb_bus *bus;
struct usb_device *dev = NULL;
struct usb_device *found_dev = NULL;
usb_dev_handle *usb = NULL;
int rv;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_busses; bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
printf("vendor/product: %04X %04X\n",
dev->descriptor.idVendor, dev->descriptor.idProduct);
if (dev->descriptor.idVendor == KOBIL_VENDOR_ID
&& dev->descriptor.idProduct ==MID_DEVICE_ID)
{
found_dev = dev;
}
}
}
if (found_dev == NULL)
{
printf("device not found. aborting.\n");
if (0 != geteuid())
printf("Try to rerun this program as root.\n");
exit(1);
}
printf("Device found, opening ... ");
usb = usb_open(found_dev);
if (!usb)
{
printf("failed, aborting.\n");
exit(2);
}
printf("success\n");
rv = kobil_midentity_claim_interface(usb, 0);
if (rv < 0)
{
usb_close(usb);
exit(3);
}
rv = kobil_midentity_claim_interface(usb, 1);
if (rv < 0)
{
usb_close(usb);
exit(3);
}
printf("Activating the CCID configuration .... ");
rv = kobil_midentity_control_msg(usb);
if (rv == 0)
printf("success\n");
else
printf("failed with error %d, giving up.\n", rv);
usb_close(usb);
return 0;
}