165
165
if (SetupDiGetDeviceInterfaceDetail(hdinfo, &did, pdidd, DIDD_BUFSIZE, NULL, &dinfod)
167
167
error("SetupDiGetDeviceInterfaceDetail failed");
168
//printf("~1 found HID device '%s', inst %d\n",pdidd->DevicePath, dinfod.DevInst);
170
/* Retriev its vid and pid by querying the device */
171
if ((fh = CreateFile(pdidd->DevicePath, 0, 0, NULL, OPEN_EXISTING,
172
0, NULL)) != INVALID_HANDLE_VALUE) {
175
if ((*pHidD_GetAttributes)(fh, &attr) == 0) {
176
DBG((stderr,"HidD_GetAttributes failed for device %d",i))
169
/* Extract the vid and pid from the device path */
175
if ((cp = strchr(pdidd->DevicePath, 'v')) == NULL)
179
if (cp[1] != 'i' || cp[2] != 'd' || cp[3] != '_')
181
memcpy(buf, cp + 4, 4);
183
if (sscanf(buf, "%hx", &VendorID) != 1)
185
if ((cp = strchr(pdidd->DevicePath, 'p')) == NULL)
189
if (cp[1] != 'i' || cp[2] != 'd' || cp[3] != '_')
191
memcpy(buf, cp + 4, 4);
193
if (sscanf(buf, "%hx", &ProductID) != 1)
199
if (p->debug) fprintf(stderr,"found HID device '%s', inst %d but unable get PID and VID\n",pdidd->DevicePath, dinfod.DevInst);
181
/* If it's a device we're looking for */
182
if ((itype = inst_usb_match(attr.VendorID, attr.ProductID)) != instUnknown) {
185
/* Create human readable path/identification */
186
sprintf(pname,"hid:/%d (%s)", dinfod.DevInst, inst_name(itype));
188
/* Add the path to the list */
189
if (p->paths == NULL) {
190
if ((p->paths = (icompath **)calloc(sizeof(icompath *), 1 + 1)) == NULL)
191
error("icoms: calloc failed!");
193
if ((p->paths = (icompath **)realloc(p->paths,
194
sizeof(icompath *) * (p->npaths + 2))) == NULL)
195
error("icoms: realloc failed!");
196
p->paths[p->npaths+1] = NULL;
198
if ((p->paths[p->npaths] = calloc(sizeof(icompath), 1)) == NULL)
199
error("icoms: calloc failed!");
200
p->paths[p->npaths]->vid = attr.VendorID;
201
p->paths[p->npaths]->pid = attr.ProductID;
202
p->paths[p->npaths]->dev = NULL; /* Make sure it's NULL */
203
if ((p->paths[p->npaths]->hev = (hid_device *)calloc(sizeof(hid_device), 1))
205
error("icoms: calloc failed!");
206
if ((p->paths[p->npaths]->hev->dpath = strdup(pdidd->DevicePath)) == NULL)
207
error("icoms: calloc failed!");
208
p->paths[p->npaths]->itype = itype;
209
if ((p->paths[p->npaths]->path = strdup(pname)) == NULL)
210
error("icoms: strdup failed!");
212
p->paths[p->npaths] = NULL;
204
/* If it's a device we're looking for */
205
if ((itype = inst_usb_match(VendorID, ProductID)) != instUnknown) {
208
if (p->debug) fprintf(stderr,"found HID device '%s', inst %d that we're looking for\n",pdidd->DevicePath, dinfod.DevInst);
210
/* Create human readable path/identification */
211
sprintf(pname,"hid:/%d (%s)", dinfod.DevInst, inst_name(itype));
213
/* Add the path to the list */
214
if (p->paths == NULL) {
215
if ((p->paths = (icompath **)calloc(sizeof(icompath *), 1 + 1)) == NULL)
216
error("icoms: calloc failed!");
218
if ((p->paths = (icompath **)realloc(p->paths,
219
sizeof(icompath *) * (p->npaths + 2))) == NULL)
220
error("icoms: realloc failed!");
221
p->paths[p->npaths+1] = NULL;
223
if ((p->paths[p->npaths] = calloc(sizeof(icompath), 1)) == NULL)
224
error("icoms: calloc failed!");
225
p->paths[p->npaths]->vid = VendorID;
226
p->paths[p->npaths]->pid = ProductID;
227
p->paths[p->npaths]->dev = NULL; /* Make sure it's NULL */
228
if ((p->paths[p->npaths]->hev = (hid_device *)calloc(sizeof(hid_device), 1))
230
error("icoms: calloc failed!");
231
if ((p->paths[p->npaths]->hev->dpath = strdup(pdidd->DevicePath)) == NULL)
232
error("icoms: calloc failed!");
233
p->paths[p->npaths]->itype = itype;
234
if ((p->paths[p->npaths]->path = strdup(pname)) == NULL)
235
error("icoms: strdup failed!");
237
p->paths[p->npaths] = NULL;
239
if (p->debug) fprintf(stderr,"found HID device '%s', inst %d but not one we're looking for\n",pdidd->DevicePath, dinfod.DevInst);
245
266
/* Find all the matching HID devices */
247
268
io_object_t ioob; /* HID object found */
248
CFMutableDictionaryRef hidprops = 0; /* HID Device properties */
249
CFNumberRef vref, pref;
269
CFNumberRef vref, pref; /* HID Vendor and Product ID propeties */
250
270
unsigned int vid = 0, pid = 0;
252
273
if ((ioob = IOIteratorNext(mit)) == 0)
255
/* Get the devices vid and pid */
256
if (IORegistryEntryCreateCFProperties(ioob, &hidprops,
257
kCFAllocatorDefault, kNilOptions) != KERN_SUCCESS || hidprops == 0)
260
if ((vref = CFDictionaryGetValue(hidprops, CFSTR(kIOHIDVendorIDKey))) != 0) {
276
/* Get the two properies we need. [ Doing IORegistryEntryCreateCFProperty() is much faster */
277
/* than IORegistryEntryCreateCFProperties() in some cases.] */
278
if ((vref = IORegistryEntryCreateCFProperty(ioob, CFSTR(kIOHIDVendorIDKey),
279
kCFAllocatorDefault,kNilOptions)) != 0) {
261
280
CFNumberGetValue(vref, kCFNumberIntType, &vid);
264
if ((pref = CFDictionaryGetValue(hidprops, CFSTR(kIOHIDProductIDKey))) != 0) {
283
if ((pref = IORegistryEntryCreateCFProperty(ioob, CFSTR(kIOHIDProductIDKey),
284
kCFAllocatorDefault,kNilOptions)) != 0) {
265
285
CFNumberGetValue(pref, kCFNumberIntType, &pid);
269
// CFRelease(hidprops); // Crashes !!
272
289
/* If it's a device we're looking for */
273
if (inst_usb_match(vid, pid) != instUnknown) {
275
for (i = 0; i < p->npaths; i++) { /* Locate it in the current list */
276
if (p->paths[i]->dev != NULL
277
&& p->paths[i]->vid == vid && p->paths[i]->pid == pid) {
279
if ((p->paths[i]->hev = (hid_device *)calloc(sizeof(hid_device), 1)) == NULL)
280
error("icoms: calloc failed!");
282
p->paths[i]->hev->ioob = ioob;
283
usb_del_usb_device(p->paths[i]->dev); /* Done with this */
284
p->paths[i]->dev = NULL; /* (Points to libusb allocation) */
290
if ((itype = inst_usb_match(vid, pid)) != instUnknown) {
292
if (p->debug) fprintf(stderr,"found HID device '%s' that we're looking for\n",inst_name(itype));
294
/* Create human readable path/identification */
295
/* (There seems to be no easy way of creating an interface no, without */
296
/* heroic efforts looking through the IO registry, so don't try.) */
297
sprintf(pname,"hid: (%s)", inst_name(itype));
299
/* Add the path to the list */
300
if (p->paths == NULL) {
301
if ((p->paths = (icompath **)calloc(sizeof(icompath *), 1 + 1)) == NULL)
302
error("icoms: calloc failed!");
304
if ((p->paths = (icompath **)realloc(p->paths,
305
sizeof(icompath *) * (p->npaths + 2))) == NULL)
306
error("icoms: realloc failed!");
307
p->paths[p->npaths+1] = NULL;
309
if ((p->paths[p->npaths] = calloc(sizeof(icompath), 1)) == NULL)
310
error("icoms: calloc failed!");
311
p->paths[p->npaths]->vid = vid;
312
p->paths[p->npaths]->pid = pid;
313
p->paths[p->npaths]->dev = NULL; /* Make sure it's NULL */
314
if ((p->paths[p->npaths]->hev = (hid_device *)calloc(sizeof(hid_device), 1))
316
error("icoms: calloc failed!");
317
p->paths[p->npaths]->hev->ioob = ioob;
318
ioob = 0; /* Don't release it */
319
p->paths[p->npaths]->itype = itype;
320
if ((p->paths[p->npaths]->path = strdup(pname)) == NULL)
321
error("icoms: strdup failed!");
323
p->paths[p->npaths] = NULL;
325
if (ioob != 0) /* If we haven't kept it */
292
326
IOObjectRelease(ioob); /* Release found object */
294
328
IOObjectRelease(mit); /* Release the itterator */
497
/* Open the device */
498
if ((p->hidd->fh = CreateFile(p->hidd->dpath, GENERIC_READ|GENERIC_WRITE,
499
0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL))
500
== INVALID_HANDLE_VALUE)
533
for (tries = 0; retries >= 0; retries--, tries++) {
534
/* Open the device */
535
if ((p->hidd->fh = CreateFile(p->hidd->dpath, GENERIC_READ|GENERIC_WRITE,
536
0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL))
537
!= INVALID_HANDLE_VALUE) {
538
memset(&p->hidd->ols,0,sizeof(OVERLAPPED));
539
if ((p->hidd->ols.hEvent = CreateEvent(NULL, 0, 0, NULL)) == NULL)
540
error("Failed to create HID Event'\n");
543
if (tries > 0 && pnames != NULL) {
544
/* Open failed. This could be the i1ProfileTray.exe */
545
kill_nprocess(pnames, p->debug);
549
if (p->hidd->fh == INVALID_HANDLE_VALUE)
501
550
error("Failed to open HID device '%s'\n",p->ppath->path);
503
memset(&p->hidd->ols,0,sizeof(OVERLAPPED));
504
if ((p->hidd->ols.hEvent = CreateEvent(NULL, 0, 0, NULL)) == NULL)
505
error("Failed to create HID Event'\n");
818
869
icoms_set_hid_port(
820
int port, /* HID com port, 1 - N, 0 for no change. */
821
icomuflags hidflags /* Any special handling flags */
871
int port, /* HID com port, 1 - N, 0 for no change. */
872
icomuflags hidflags, /* Any special handling flags */
873
int retries, /* > 0 if we should retry set_configuration (100msec) */
874
char **pnames /* List of process names to try and kill before opening */
823
876
if (p->debug) fprintf(stderr,"icoms: About to set hid port characteristics\n");
828
881
if (p->is_hid_portno(p, port) != instUnknown) {
830
hid_open_port(p, port, hidflags);
883
hid_open_port(p, port, hidflags, retries, pnames);