Debian Codegraph @

p5glove/libp5glove/usb_hid_libusb.c

raw symbols SCIP
1 /*
2 Copyright (c) 2004 Jason McMullan <[email protected]>
3
4 USB/HID I/O routines
5 */
6 /*
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 Open the indexth USBHID device which match the delection criteria. The
35 selection criteria may be any combination of vendor id, product id and
36 version number as specified by the flags parameter.
37
38 Returns an open handle on success, or INVALID_USBHIDHANDLE if an error
39 occurred.
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 Close a USB HID handle previously opened with OpenUSBHID()
85 */
86 void CloseUSBHID( USBHID usb )
87 {
88 usb_release_interface(usb,1);
89 usb_close(usb);
90 }
91
92
93 /*
94 Read count bytes from the USB HID into dest. Returns the number of bytes
95 read. This routine reads from the Kernel HID ring buffer, if it is no called
96 frequently enough the data returned may lag behind the latest HID reports
97 from the device.
98
99 HidD_GetInputReport will read the most recent report, but is only
100 implemented on Windows XP.
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 //# define usb_read(usb,ep,data,len,tm) usb_get_input(usb,1,data,len)
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, /* HID_REPORT_GET */
123 (report[0]&0xf) | (0x01<<8), /* HID_REPORT_TYPE_INPUT */
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, /* HID_REPORT_SET = 0x09*/
136 (report[0]&0xf) | (0x02<<8), /* HID_REPORT_TYPE_OUPUT */
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, /* HID_REPORT_GET */
147 (report[0]&0xf) | (0x03<<8), /* HID_REPORT_TYPE_FEATURE */
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, /* HID_REPORT_SET = 0x09*/
160 (report[0]&0xf) | (0x03<<8), /* HID_REPORT_TYPE_FEATURE */
161 1, report, count, timeout);
162 }