| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <usb.h> |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
#define USBHID_struct usb_dev_handle |
| 29 |
#include "usb_hid.h" |
| 30 |
|
| 31 |
static int timeout = 1000; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
USBHID OpenUSBHID( int index, int vendorId, int productId, int versionNumber, int flags ) |
| 42 |
{ |
| 43 |
struct usb_bus *bus; |
| 44 |
struct usb_device *dev; |
| 45 |
struct usb_dev_handle *usb = NULL; |
| 46 |
int err,i=0; |
| 47 |
|
| 48 |
usb_init(); |
| 49 |
|
| 50 |
usb_find_busses(); |
| 51 |
usb_find_devices(); |
| 52 |
|
| 53 |
for (bus = usb_get_busses(); bus != NULL; bus = bus->next) { |
| 54 |
for (dev = bus->devices; dev != NULL; dev = dev->next) { |
| 55 |
if (dev->descriptor.idVendor != vendorId || |
| 56 |
dev->descriptor.idProduct != productId) |
| 57 |
continue; |
| 58 |
|
| 59 |
if (i < index) |
| 60 |
continue; |
| 61 |
|
| 62 |
usb = usb_open(dev); |
| 63 |
if (usb == NULL) |
| 64 |
continue; |
| 65 |
|
| 66 |
err=usb_claim_interface(usb,1); |
| 67 |
if (err < 0) { |
| 68 |
fprintf(stderr,"Can't claim P5 glove interface: %s\n",strerror(errno)); |
| 69 |
usb_close(usb); |
| 70 |
return NULL; |
| 71 |
} |
| 72 |
|
| 73 |
break; |
| 74 |
} |
| 75 |
if (usb != NULL) |
| 76 |
break; |
| 77 |
} |
| 78 |
|
| 79 |
return usb; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
void CloseUSBHID( USBHID usb ) |
| 87 |
{ |
| 88 |
usb_release_interface(usb,1); |
| 89 |
usb_close(usb); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
#ifndef HAVE_USB_INTERRUPT_READ |
| 103 |
# define usb_read(usb,ep,data,len,tm) usb_bulk_read(usb,ep,data,len,tm) |
| 104 |
# define usb_clear_halt(usb,ep) usb_resetep(usb,ep) |
| 105 |
#else |
| 106 |
|
| 107 |
# define usb_read(usb,ep,data,len,tm) usb_interrupt_read(usb,ep,data,len,tm) |
| 108 |
#endif |
| 109 |
|
| 110 |
int ReadUSBHID( USBHID usb, void *dest, int count ) |
| 111 |
{ |
| 112 |
return usb_read(usb, 0x81, dest, count, timeout); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
int usb_get_input(USBHID udev, unsigned char *report, int ReportSize) |
| 117 |
{ |
| 118 |
int err; |
| 119 |
retry: |
| 120 |
err=usb_control_msg(udev, |
| 121 |
USB_ENDPOINT_IN + USB_TYPE_CLASS + USB_RECIP_INTERFACE, |
| 122 |
0x01, |
| 123 |
(report[0]&0xf) | (0x01<<8), |
| 124 |
1, report, ReportSize, timeout); |
| 125 |
if (err == -EPIPE) |
| 126 |
goto retry; |
| 127 |
return err; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
int usb_set_output(USBHID udev, unsigned char *report, int ReportSize ) |
| 132 |
{ |
| 133 |
return usb_control_msg(udev, |
| 134 |
USB_ENDPOINT_OUT + USB_TYPE_CLASS + USB_RECIP_INTERFACE, |
| 135 |
0x09, |
| 136 |
(report[0]&0xf) | (0x02<<8), |
| 137 |
1, report, ReportSize, timeout); |
| 138 |
} |
| 139 |
|
| 140 |
int GetUSBHIDFeature( USBHID udev, char *report, int count ) |
| 141 |
{ |
| 142 |
int err; |
| 143 |
retry: |
| 144 |
err=usb_control_msg(udev, |
| 145 |
USB_ENDPOINT_IN + USB_TYPE_CLASS + USB_RECIP_INTERFACE, |
| 146 |
0x01, |
| 147 |
(report[0]&0xf) | (0x03<<8), |
| 148 |
1, report, count, timeout); |
| 149 |
if (err == -EPIPE) |
| 150 |
goto retry; |
| 151 |
return err; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
int SetUSBHIDFeature( USBHID udev, char *report, int count ) |
| 156 |
{ |
| 157 |
return usb_control_msg(udev, |
| 158 |
USB_ENDPOINT_OUT + USB_TYPE_CLASS + USB_RECIP_INTERFACE, |
| 159 |
0x09, |
| 160 |
(report[0]&0xf) | (0x03<<8), |
| 161 |
1, report, count, timeout); |
| 162 |
} |