-
Notifications
You must be signed in to change notification settings - Fork 1
/
pmd.c
205 lines (179 loc) · 5.77 KB
/
pmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
*
* Copyright (c) 2004-2005 Warren Jasper <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "pmd.h"
// Driver Functions
bool match_product(struct usb_dev_handle* dev_h, void* custom, unsigned int len)
{
// There is currently a bug in libhid in that it masks the product id instead
// of matching on it.
HIDInterfaceMatcher *match = (HIDInterfaceMatcher*) custom;
struct usb_device const* dev = usb_device(dev_h);
if (((dev->descriptor.idProduct) == match->product_id) &&
((dev->descriptor.idVendor) == match->vendor_id)) {
return 1;
} else {
return 0;
}
}
bool match_serial_number(struct usb_dev_handle* usbdev, void* custom, unsigned int len)
{
bool ret;
char* buffer = (char*)malloc(len);
usb_get_string_simple(usbdev, usb_device(usbdev)->descriptor.iSerialNumber, buffer, len);
ret = strncmp(buffer, (char*)custom, len) == 0;
free(buffer);
return ret;
}
int PMD_Find_Interface(HIDInterface** hid, int interface, int product_id)
{
hid_return ret;
HIDInterfaceMatcher matcher = { MCC_VID, 0x0, NULL, NULL, 0 };
matcher.product_id = product_id;
matcher.custom_data = (void *) &matcher;
matcher.custom_data_length = sizeof(matcher);
matcher.matcher_fn = (matcher_fn_t) match_product;
/**********************************************
// Examples of other matcher functions to fix bugs in libhid. Some have been fixed.
HIDInterfaceMatcher matcher = { MCC_VID, 0x0, match_serial_number, "12345678", 9 };
*******************************************/
*hid = hid_new_HIDInterface();
if (*hid == 0) {
fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
return -1;
}
ret = hid_force_open(*hid, interface, &matcher, 3);
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
return -1;
} else {
return interface;
}
}
char * PMD_GetSerialNumber(HIDInterface* hid)
{
struct usb_dev_handle* udev = hid->dev_handle;
static char serial[13];
int ret;
if (usb_device(udev)->descriptor.iSerialNumber) {
ret = usb_get_string_simple(udev, usb_device(udev)->descriptor.iSerialNumber, serial, sizeof(serial));
if (ret > 0) {
serial[8] = '\0';
return serial;
} else {
strcpy(serial, "Error "); // error
return serial;
}
}
return 0;
}
int PMD_SendOutputReport(HIDInterface* hid, __u8 reportID, __u8* vals, int num_vals, int delay)
{
int ret;
if (reportID == 0) { // use interrupt endpoint 1
ret = usb_interrupt_write(hid->dev_handle, USB_ENDPOINT_OUT | 1, (char *) vals, num_vals, delay);
if (ret != num_vals) { // try one more time:
ret = usb_interrupt_write(hid->dev_handle, USB_ENDPOINT_OUT | 1, (char *) vals, num_vals, delay);
}
} else { // use the control endpoint (Some FS devices use this)
ret = usb_control_msg(hid->dev_handle,
(USB_TYPE_CLASS| USB_RECIP_INTERFACE),
SET_REPORT,
(OUTPUT_REPORT | reportID),
0,
(char *) vals,
num_vals,
delay);
}
return ret;
}
int PMD_GetInputReport(HIDInterface* hid, __u8 reportID, __u8 *vals, int nbytes, int delay)
{
char data[33];
int ret;
if ( reportID == 0 ) { // it's an LS device, use endpoint 1
ret = usb_interrupt_read(hid->dev_handle, USB_ENDPOINT_IN | 1, data, nbytes, delay);
if ( ret < 0 ) {
perror("PMD_GetInputReport");
}
memcpy(vals, data, nbytes);
return ret;
}
return -1;
}
int PMD_GetFeatureReport(HIDInterface* hid, __u8 reportID, __u8 *vals, int num_vals, int delay)
{
int ret = 0;
ret = usb_control_msg(hid->dev_handle,
0xa1,
GET_REPORT,
FEATURE_REPORT,
0,
(char *) vals,
num_vals,
delay);
return ret;
}
usb_dev_handle* usb_device_find_USB_MCC( int productId )
{
struct usb_bus *bus = NULL;
struct usb_device *dev = NULL;
usb_dev_handle *udev = NULL;
int ret;
int vendorId = MCC_VID;
char name[80];
usb_init();
// usb_set_debug(3);
usb_find_busses();
usb_find_devices();
for (bus = usb_get_busses(); bus; bus = bus->next) { // loop through all the busses
for (dev = bus->devices; dev; dev = dev->next) { // loop through all the devices
if ( (dev->descriptor.idVendor == vendorId) && // If this is our device ...
(dev->descriptor.idProduct == productId)) {
if ((udev = usb_open(dev))) { // open the device
printf("Vendor ID = %#x Product ID = %#x\n", dev->descriptor.idVendor, dev->descriptor.idProduct);
if (usb_get_driver_np(udev, 0, name, sizeof(name)) == 0) {
printf("USB device already bound to driver: %s\n", name);
usb_close(udev);
continue;
}
/* set the configuration */
if ((ret = usb_set_configuration(udev, 1))) {
perror("Error setting configuration\n");
}
/* claim interface */
if ((ret = usb_claim_interface(udev, 0))) {
perror("Error claiming usb interface 0\n");
}
return udev;
}
}
}
}
return 0;
}