Debian Codegraph @

spectro/usbio_w0.c

raw symbols SCIP
1
2 /* General USB I/O support, MSWin native libusb0.sys implementation. */
3 /* This file is conditionaly #included into usbio.c */
4
5 /*
6 * Argyll Color Management System
7 *
8 * Author: Graeme W. Gill
9 * Date: 2006/22/4
10 *
11 * Copyright 2006 - 2013 Graeme W. Gill
12 * All rights reserved.
13 *
14 * This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
15 * see the License2.txt file for licencing details.
16 */
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <windows.h>
24 #include <winioctl.h>
25 #include <setupapi.h>
26 #include "driver_api.h"
27
28 #undef DEBUG /* Turn on debug messages */
29
30 #define LIBUSBW1_MAX_DEVICES 255
31 #define LIBUSBW1_PATH_MAX 512
32 #define LIBUSBW1_DEFAULT_TIMEOUT 5000
33
34 #ifndef PATH_MAX
35 # define PATH_MAX 4096
36 #endif
37
38 static int icoms_usb_control_msg_w0(icoms *p, int *transferred, int requesttype, int request,
39 int value, int index, unsigned char *bytes, int size, int timeout);
40
41 /* Do a synchronous request. Return an ICOM error */
42 static int do_sync_io(
43 HANDLE handle,
44 unsigned int ioctl,
45 void *out, int outsz,
46 void *in, int insz,
47 int *retsz) {
48 OVERLAPPED olaps;
49 DWORD xlength;
50
51 memset(&olaps, 0, sizeof(OVERLAPPED));
52 if (retsz != NULL)
53 *retsz = 0;
54
55 if ((olaps.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL)
56 return ICOM_SYS;
57
58 if (!DeviceIoControl(handle, ioctl, out, outsz, in, insz, &xlength, &olaps)) {
59 if (GetLastError() != ERROR_IO_PENDING) {
60 CloseHandle(olaps.hEvent);
61 return ICOM_USBW;
62 }
63 if (!GetOverlappedResult(handle, &olaps, &xlength, TRUE)) {
64 CloseHandle(olaps.hEvent);
65 return ICOM_USBR;
66 }
67 }
68 CloseHandle(olaps.hEvent);
69 if (retsz != NULL)
70 *retsz = (int)xlength;
71
72 return ICOM_OK;
73 }
74
75 /* Add paths to USB connected device */
76 /* Return an icom error */
77 int usb_get_paths_w0(
78 icompaths *p
79 ) {
80 unsigned int vid, pid, nep10 = 0xffff;
81 unsigned int configix, nconfig, nifce;
82 devType itype;
83 struct usb_idevice *usbd = NULL;
84 int rv, retsz, i;
85 int upto;
86
87 upto = p->get_cur_count(p);
88
89 for (i = 0; i < LIBUSBW1_MAX_DEVICES; i++) {
90 libusb_request req;
91 char dpath[LIBUSBW1_PATH_MAX];
92 HANDLE handle;
93 unsigned char buf[IUSB_DESC_TYPE_STRING_MAX_SIZE];
94
95 _snprintf(dpath, LIBUSBW1_PATH_MAX - 1,"\\\\.\\libusb0-%04d", i+1);
96 if (i < 16) /* Suppress messages on unlikely ports */
97 a1logd(p->log, 6, "usb_get_paths_w0 opening device '%s'\n",dpath);
98
99 if ((handle = CreateFile(dpath, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED,
100 NULL)) == INVALID_HANDLE_VALUE) {
101 #ifdef DEBUG
102 a1logd(p->log, 8, "usb_get_paths_w0 failed to open device '%s'\n",dpath);
103 #endif
104 continue;
105 }
106
107 /* Set kernel message debug */
108 if (p->log->debug >= 6) {
109 req.debug.level = LIBUSB_DEBUG_MAX;
110 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
111 if (do_sync_io(handle, LIBUSB_IOCTL_SET_DEBUG_LEVEL,
112 &req, sizeof(libusb_request), NULL, 0, NULL)) {
113 a1logd(p->log, 1, "usb_get_paths_w0 failed to set driver log leve\n");
114 } else {
115 a1logd(p->log, 1, "usb_get_paths_w0 turned on kernel debug messages\n");
116 }
117 }
118
119 /* Read the device descriptor */
120 req.descriptor.type = IUSB_DESC_TYPE_DEVICE;
121 req.descriptor.recipient = IUSB_REQ_RECIP_DEVICE;
122 req.descriptor.index = 0;
123 req.descriptor.language_id = 0;
124 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
125
126 if (do_sync_io(handle, LIBUSB_IOCTL_GET_DESCRIPTOR,
127 &req, sizeof(libusb_request),
128 buf, IUSB_DESC_TYPE_DEVICE_SIZE, &retsz) != ICOM_OK
129 || retsz != IUSB_DESC_TYPE_DEVICE_SIZE) {
130 a1logd(p->log, 1, "usb_get_paths_w0 failed to read device descriptor '%s'\n",dpath);
131 CloseHandle(handle);
132 continue;
133 }
134
135 /* Extract the vid and pid */
136 vid = usb2ushort(buf + 8);
137 pid = usb2ushort(buf + 10);
138 nconfig = buf[17];
139 /*
140 Indexes of DESCRIPTOR strings:
141 iManufacturer buf[14]
142 iProduct buf[15]
143 iSerialNumber buf[16]
144 */
145
146 a1logd(p->log, 6, "usb_get_paths_w0 checking vid 0x%04x, pid 0x%04x\n",vid,pid);
147
148 /* Do a preliminary match */
149 if ((itype = inst_usb_match(vid, pid, 0)) == instUnknown) {
150 a1logd(p->log, 6 , "usb_get_paths_w0 instrument not reconized\n");
151 CloseHandle(handle);
152 continue;
153 }
154
155 /* Allocate an idevice so that we can fill in the end point information */
156 if ((usbd = (struct usb_idevice *) calloc(sizeof(struct usb_idevice), 1)) == NULL) {
157 a1loge(p->log, ICOM_SYS, "usb_get_paths_w0 calloc failed!\n");
158 CloseHandle(handle);
159 return ICOM_SYS;
160 }
161
162 usbd->iserialno = buf[16];
163 usbd->nconfig = nconfig;
164
165 /* Read the configuration descriptors looking for the first configuration, first interface, */
166 /* and extract the number of end points for each configuration */
167 for (configix = 0; configix < nconfig; configix++) {
168 int configno, totlen;
169 unsigned char *buf2, *bp, *zp;
170 unsigned int ninfaces, inface, nep;
171
172 /* Read the configuration descriptor */
173 req.descriptor.type = IUSB_DESC_TYPE_CONFIG;
174 req.descriptor.recipient = IUSB_REQ_RECIP_DEVICE;
175 req.descriptor.index = configix;
176 req.descriptor.language_id = 0;
177 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
178
179 /* Read just the first part of the config descriptor to get its size and no. */
180 if ((rv = do_sync_io(handle, LIBUSB_IOCTL_GET_DESCRIPTOR,
181 &req, sizeof(libusb_request),
182 buf, IUSB_DESC_TYPE_CONFIG_SIZE, &retsz)) != ICOM_OK
183 || retsz != IUSB_DESC_TYPE_CONFIG_SIZE) {
184 a1logd(p->log, 1, "usb_get_paths_w0 failed to read configix %d descriptor\n",configix);
185 free(usbd);
186 CloseHandle(handle);
187 break;
188 }
189 nifce = buf[4]; /* number of interfaces */
190 configno = buf[5]; /* Configuration number */
191
192 if (configno != 1)
193 continue;
194
195 if ((totlen = usb2ushort(buf + 2)) < 6) {
196 a1logd(p->log, 1, "usb_get_paths_w0 '%s' config desc size strange\n",dpath);
197 free(usbd);
198 CloseHandle(handle);
199 break;
200 }
201 if ((buf2 = calloc(1, totlen)) == NULL) {
202 a1loge(p->log, ICOM_SYS, "usb_get_paths_w0 calloc of descriptor failed!\n");
203 free(usbd);
204 CloseHandle(handle);
205 return ICOM_SYS;
206 }
207
208 /* Read the whole configuration descriptor */
209 req.descriptor.type = IUSB_DESC_TYPE_CONFIG;
210 req.descriptor.recipient = IUSB_REQ_RECIP_DEVICE;
211 req.descriptor.index = configix;
212 req.descriptor.language_id = 0;
213 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
214
215 if (do_sync_io(handle, LIBUSB_IOCTL_GET_DESCRIPTOR,
216 &req, sizeof(libusb_request),
217 buf2, totlen, &retsz) != ICOM_OK
218 || retsz != totlen) {
219 a1logd(p->log, 1, "usb_get_paths_w0 failed to read all configix %d descriptor\n",configix);
220 free(buf2);
221 free(usbd);
222 CloseHandle(handle);
223 break;
224 }
225
226 bp = buf2 + buf2[0]; /* Skip coniguration tag */
227 zp = buf2 + totlen; /* Past last bytes */
228
229 /* We are at the first configuration. */
230 /* Just read tags and keep track of where we are */
231 ninfaces = 0;
232 nep = 0;
233 usbd->nifce = buf2[4]; /* number of interfaces */
234 usbd->config = configno = buf2[5]; /* this configuration */
235 for (;bp < zp; bp += bp[0]) {
236 int ifaceno;
237 if ((bp + 1) >= zp)
238 break; /* Hmm - bodgy, give up */
239 if (bp[1] == IUSB_DESC_TYPE_INTERFACE) {
240 ninfaces++;
241 if ((bp + 2) >= zp)
242 break; /* Hmm - bodgy, give up */
243 ifaceno = bp[2]; /* Get bInterfaceNumber */
244 } else if (bp[1] == IUSB_DESC_TYPE_ENDPOINT) {
245 nep++;
246 if ((bp + 5) >= zp)
247 break; /* Hmm - bodgy */
248 /* At first config - */
249 /* record current nep and end point details */
250 if (configno == 1) { /* (if isn't actually needed...) */
251 int ad = bp[2];
252 nep10 = nep;
253 usbd->EPINFO(ad).valid = 1;
254 usbd->EPINFO(ad).addr = ad;
255 usbd->EPINFO(ad).packetsize = usb2ushort(bp + 4);
256 usbd->EPINFO(ad).type = bp[3] & IUSB_ENDPOINT_TYPE_MASK;
257 usbd->EPINFO(ad).ifaceno = ifaceno;
258 a1logd(p->log, 6, "set ep ad 0x%x packetsize %d type %d\n",ad,usbd->EPINFO(ad).packetsize,usbd->EPINFO(ad).type);
259 }
260 }
261 /* Ignore other tags */
262 }
263 free(buf2);
264
265 /* We only get here on the first config, so we're done. */
266 break;
267
268 } /* Next configuration */
269 if (nep10 == 0xffff) { /* Hmm. Failed to find number of end points */
270 a1logd(p->log, 1, "usb_get_paths_w0 failed to find number of end points\n");
271 free(usbd);
272 CloseHandle(handle);
273 continue;
274 }
275
276 /* Check that we have an up to date kernel driver */
277 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
278 if (do_sync_io(handle, LIBUSB_IOCTL_GET_VERSION,
279 &req, sizeof(libusb_request),
280 &req, sizeof(libusb_request), &retsz) != ICOM_OK
281 || retsz != sizeof(libusb_request)) {
282 a1logd(p->log, 1, "usb_get_paths_w0 failed to read driver version info\n");
283 free(usbd);
284 CloseHandle(handle);
285 continue;
286 }
287 if (req.version.major < 1
288 || req.version.major == 1 && (req.version.minor < 2
289 || req.version.minor == 2 && req.version.micro < 6)) {
290 a1loge(p->log, ICOM_VER, "usb_get_paths_w0 Must update %s System Driver to latest version!\n",inst_name(itype));
291 free(usbd);
292 CloseHandle(handle);
293 return ICOM_VER;
294 }
295 CloseHandle(handle);
296
297 /* Found a known instrument, and not found by a previous driver ? */
298 if ((itype = inst_usb_match(vid, pid, nep10)) != instUnknown
299 && !p->check_usb_upto(p, upto, itype)) {
300 char pname[400];
301
302 a1logd(p->log, 1, "usb_get_paths_w0 found instrument vid 0x%04x, pid 0x%04x\n",vid,pid);
303
304 /* Create a path/identification */
305 sprintf(pname,"%s (%s)", dpath + 4, inst_name(itype));
306
307 if ((usbd->dpath = strdup(dpath)) == NULL) {
308 a1loge(p->log, ICOM_SYS, "usb_get_paths_w0 strdup path failed!\n");
309 free(usbd);
310 return ICOM_SYS;
311 }
312
313 /* Add the path and ep info to the list */
314 if ((rv = p->add_usb(p, pname, vid, pid, nep10, usbd, itype)) != ICOM_OK)
315 return rv;
316
317
318 } else {
319 free(usbd);
320 }
321 }
322
323 return ICOM_OK;
324 }
325
326
327 /* Copy usb_idevice contents from icompaths to icom */
328 /* return icom error */
329 int usb_copy_usb_idevice_w0(icoms *d, icompath *s) {
330 int i;
331 if (s->usbd == NULL) {
332 d->usbd = NULL;
333 return ICOM_OK;
334 }
335 if ((d->usbd = calloc(sizeof(struct usb_idevice), 1)) == NULL) {
336 a1loge(d->log, ICOM_SYS, "usb_copy_usb_idevice_w0: malloc\n");
337 return ICOM_SYS;
338 }
339 if ((d->usbd->dpath = strdup(s->usbd->dpath)) == NULL) {
340 a1loge(d->log, ICOM_SYS, "usb_copy_usb_idevice_w0: malloc\n");
341 return ICOM_SYS;
342 }
343 d->usbd->iserialno = s->usbd->iserialno;
344 if (s->usbd->SerialNumber != NULL
345 && (d->usbd->SerialNumber = strdup(s->usbd->SerialNumber)) == NULL) {
346 a1loge(d->log, ICOM_SYS, "usb_copy_usb_idevice: malloc\n");
347 return ICOM_SYS;
348 }
349
350 /* Copy the current state & ep info */
351 d->nconfig = s->usbd->nconfig;
352 d->config = s->usbd->config;
353 d->nifce = s->usbd->nifce;
354 for (i = 0; i < 32; i++)
355 d->ep[i] = s->usbd->ep[i]; /* Struct copy */
356 return ICOM_OK;
357 }
358
359 /* Cleanup and then free a usb dev entry */
360 void usb_del_usb_idevice_w0(struct usb_idevice *usbd) {
361
362 if (usbd == NULL)
363 return;
364
365 if (usbd->dpath != NULL)
366 free(usbd->dpath);
367 if (usbd->SerialNumber != NULL)
368 free(usbd->SerialNumber);
369 free(usbd);
370 }
371
372 /* Cleanup any USB specific icoms state */
373 void usb_del_usb_w0(icoms *p) {
374
375 usb_del_usb_idevice(p->usbd);
376 }
377
378 /* Close an open USB port */
379 /* If we don't do this, the port and/or the device may be left in an unusable state. */
380 void usb_close_port_w0(icoms *p) {
381
382 a1logd(p->log, 6, "usb_close_port_w0: called\n");
383
384 if (p->is_open && p->usbd != NULL) {
385 int iface, rv;
386
387 /* Release all the interfaces */
388 for (iface = 0; iface < p->nifce; iface++) {
389 libusb_request req;
390
391 memset(&req, 0, sizeof(req));
392 req.intf.interface_number = iface;
393 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
394
395 do_sync_io(p->usbd->handle, LIBUSB_IOCTL_RELEASE_INTERFACE,
396 &req, sizeof(libusb_request), NULL, 0, NULL);
397 }
398
399 /* Workaround for some bugs - reset device on close */
400 if (p->uflags & icomuf_reset_before_close) {
401 libusb_request req;
402
403 a1logd(p->log, 6, "usb_close_port_w0: icomuf_reset_before_close\n");
404
405 memset(&req, 0, sizeof(req));
406 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
407
408 if ((rv = do_sync_io(p->usbd->handle, LIBUSB_IOCTL_RESET_DEVICE,
409 &req, sizeof(libusb_request), NULL, 0, NULL)) != ICOM_OK) {
410 a1logd(p->log, 1, "usb_close_port_w0: reset returned %d\n",rv);
411 }
412 msec_sleep(500); /* Things foul up unless we wait for the reset... */
413 }
414 CloseHandle(p->usbd->handle);
415
416 a1logd(p->log, 6, "usb_close_port_w0: usb port has been released and closed\n");
417 }
418 p->is_open = 0;
419
420 /* Find it and delete it from our static cleanup list */
421 usb_delete_from_cleanup_list(p);
422 }
423
424 static void *urb_reaper(void *context); /* Declare */
425
426 /* Open a USB port for all our uses. */
427 /* This always re-opens the port */
428 /* return icom error */
429 static int usb_open_port_w0(
430 icoms *p,
431 int config, /* Configuration number */
432 int wr_ep, /* Write end point */
433 int rd_ep, /* Read end point */
434 icomuflags usbflags,/* Any special handling flags */
435 int retries, /* > 0 if we should retry set_configuration (100msec) */
436 char **pnames /* List of process names to try and kill before opening */
437 ) {
438 int rv, tries = 0;
439 a1logd(p->log, 8, "usb_open_port_w0: Make sure USB port is open, tries %d\n",retries);
440
441 if (p->is_open)
442 p->close_port(p);
443
444 /* Make sure the port is open */
445 if (!p->is_open) {
446 int rv, i, iface;
447 kkill_nproc_ctx *kpc = NULL;
448 OSVERSIONINFO osver;
449
450 if (config != 1) {
451 /* NT defaults to config 1, and nothing currently needs any other config, */
452 /* so we haven't implemented selecting a config yet... */
453 a1loge(p->log, ICOM_NOTS, "usb_open_port_w0: native driver cant handle config %d\n",config);
454 return ICOM_NOTS;
455
456 msec_sleep(50); /* Allow device some time to configure */
457 }
458
459 /* Do open retries */
460 for (tries = 0; retries >= 0; retries--, tries++) {
461
462 a1logd(p->log, 8, "usb_open_port_w0: About to open USB port '%s'\n",p->usbd->dpath);
463
464 if (tries > 0) {
465 //msec_sleep(i_rand(50,100));
466 msec_sleep(77);
467 }
468
469 if ((p->usbd->handle = CreateFile(p->usbd->dpath, 0, 0, NULL, OPEN_EXISTING,
470 FILE_FLAG_OVERLAPPED, NULL)) == INVALID_HANDLE_VALUE) {
471 a1logd(p->log, 8, "usb_open_port_w0: open '%s' config %d failed (%d) (Device being used ?)\n",p->usbd->dpath,config,GetLastError());
472 if (retries <= 0) {
473 if (kpc != NULL) {
474 if (kpc->th->result < 0)
475 a1logw(p->log, "usb_open_port_w0: killing competing processes failed\n");
476 kpc->del(kpc);
477 }
478 a1logw(p->log, "usb_open_port_w0: open '%s' config %d failed (%d) (Device being used ?)\n",p->usbd->dpath,config,GetLastError());
479 return ICOM_SYS;
480 }
481 continue;
482 } else if (p->debug)
483 a1logd(p->log, 2, "usb_open_port_w0: open port '%s' succeeded\n",p->usbd->dpath);
484
485 p->uflags = usbflags;
486
487 /* We're done */
488 break;
489 }
490
491 if (kpc != NULL)
492 kpc->del(kpc);
493
494 /* We should only do a set configuration if the device has more than one */
495 /* possible configuration and it is currently not the desired configuration, */
496 /* but we should avoid doing a set configuration if the OS has already */
497 /* selected the configuration we want, since two set configs seem to */
498 /* mess up the Spyder2, BUT we can't do a get config because this */
499 /* messes up the i1pro-D. */
500
501 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
502 osver.dwMajorVersion = 5;
503 GetVersionEx(&osver);
504 if (osver.dwMajorVersion >= 6 && osver.dwMinorVersion >= 2) {
505 p->cconfig = 0; /* Need to do set_congfig(1) on Win8 */
506 } else {
507 p->cconfig = 1; /* Set by default to config 1 */
508 }
509
510 if (p->cconfig != config) {
511 libusb_request req;
512
513 memset(&req, 0, sizeof(libusb_request));
514 req.configuration.configuration = config;
515 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
516
517 if ((rv = do_sync_io(p->usbd->handle, LIBUSB_IOCTL_SET_CONFIGURATION,
518 &req, sizeof(libusb_request), NULL, 0, NULL)) != ICOM_OK) {
519
520 a1loge(p->log, rv, "usb_open_port_w0: Setting port '%s' to config %d failed with %d\n",p->usbd->dpath,config,rv);
521 return ICOM_SYS;
522 }
523 p->cconfig = config;
524 a1logd(p->log, 6, "usb_open_port_w0: set config %d OK\n",config);
525 }
526
527 /* Claim all the interfaces */
528 for (iface = 0; iface < p->nifce; iface++) {
529 libusb_request req;
530
531 memset(&req, 0, sizeof(libusb_request));
532 req.intf.interface_number = iface;
533 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
534
535 if ((rv = do_sync_io(p->usbd->handle, LIBUSB_IOCTL_CLAIM_INTERFACE,
536 &req, sizeof(libusb_request), NULL, 0, NULL)) != ICOM_OK) {
537
538 a1loge(p->log, rv, "usb_open_port_w0: Claiming USB port '%s' interface %d failed with %d\n",p->usbd->dpath,iface,rv);
539 return ICOM_SYS;
540 }
541 }
542
543 /* Clear any errors */
544 /* (Some I/F seem to hang if we do this, some seem to hang if we don't !) */
545 if (!(p->uflags & icomuf_no_open_clear)) {
546 for (i = 0; i < 32; i++) {
547 if (!p->ep[i].valid)
548 continue;
549 p->usb_clearhalt(p, p->ep[i].addr);
550 }
551 }
552
553 /* Set "serial" coms values */
554 p->wr_ep = wr_ep;
555 p->rd_ep = rd_ep;
556 p->rd_qa = p->EPINFO(rd_ep).packetsize;
557 if (p->rd_qa == 0)
558 p->rd_qa = 8;
559 a1logd(p->log, 8, "usb_open_port_w0: 'serial' read quanta = packet size = %d\n",p->rd_qa);
560
561 p->is_open = 1;
562 a1logd(p->log, 8, "usb_open_port_w0: USB port is now open\n");
563
564 /* If possible, read the device serial no */
565 if (p->usbd->iserialno != 0) {
566 unsigned char buf[IUSB_DESC_TYPE_STRING_MAX_SIZE];
567 int se = 0;
568 int retsz = 0;
569
570 if (((se = icoms_usb_control_msg_w0(p, &retsz,
571 IUSB_REQ_DEV_TO_HOST | IUSB_REQ_TYPE_STANDARD | IUSB_REQ_RECIP_DEVICE,
572 IUSB_REQ_GET_DESCRIPTOR,
573 (IUSB_DESC_TYPE_STRING << 8) | p->usbd->iserialno, 0,
574 buf, IUSB_DESC_TYPE_STRING_MAX_SIZE, 200)) != ICOM_OK && se != ICOM_SHORT)
575 || retsz < IUSB_DESC_TYPE_STRING_MIN_SIZE) {
576 a1logd(p->log, 1, "usb_open_port failed to read device serial no with ICOM err 0x%x and retsz %d\n",se,retsz);
577 } else {
578 unsigned int slen = buf[0];
579
580 if (slen > 2 && (slen & 1) == 0) {
581 icmUTF16 buf2[IUSB_DESC_TYPE_STRING_MAX_SIZE/2];
582 unsigned int j;
583 size_t s8len;
584
585 /* Convert to 16 bit native endian and add nul terminator */
586 slen = (slen-2)/2;
587 for (j = 0; j < slen; j++) {
588 buf2[j] = usb2ushort(buf + 2 + j * 2);
589 }
590 buf2[j] = 0;
591
592 s8len =icmUTF16toUTF8(NULL, NULL, buf2); /* Compute UTF8 size */
593
594 if ((p->usbd->SerialNumber = (char *) calloc(s8len, 1)) == NULL) {
595 a1loge(p->log, ICOM_SYS, "usb_get_paths_w0 calloc failed!\n");
596 usb_close_port(p);
597 return ICOM_SYS;
598 }
599 icmUTF16toUTF8(NULL, (unsigned char *)p->usbd->SerialNumber, buf2); /* Convert to UTF8 */
600 a1logd(p->log, 6, "usb_open_port got USB serial no '%s'\n",p->usbd->SerialNumber);
601 }
602 }
603 }
604 }
605
606 /* Install the cleanup signal handlers, and add to our cleanup list */
607 usb_install_signal_handlers(p);
608
609 return ICOM_OK;
610 }
611
612 /* -------------------------------------------------------------- */
613
614 /* Our universal (non-control pipe) USB transfer function, used for rd/wr. */
615 /* It appears that we may return a timeout with valid characters. */
616 static int icoms_usb_transaction_w0(
617 icoms *p,
618 usb_cancelt *cancelt, /* if !NULL, allows signalling another thread that op has started */
619 int *transferred,
620 icom_usb_trantype ttype, /* transfer type */
621 unsigned char endpoint, /* 0x80 for control write, 0x00 for control read */
622 unsigned char *buffer,
623 int length, /* Length to write/expected length to read */
624 unsigned int timeout /* In msec */
625 ) {
626 int rv = ICOM_OK;
627 int dirw = (endpoint & IUSB_ENDPOINT_DIR_MASK) == IUSB_ENDPOINT_OUT ? 1 : 0;
628 libusb_request req;
629 OVERLAPPED olaps;
630 DWORD xlength = 0; /* Transferred length */
631
632 in_usb_rw++;
633
634 a1logd(p->log, 8, "icoms_usb_transaction_w0: req type 0x%x ep 0x%x size %d\n",ttype,endpoint,length);
635
636 if (transferred != NULL)
637 *transferred = 0;
638
639 if (ttype != icom_usb_trantype_interrutpt
640 && ttype != icom_usb_trantype_bulk) {
641 /* We only handle interrupt & bulk, not control */
642 return ICOM_SYS;
643 }
644
645 memset(&olaps, 0, sizeof(olaps));
646
647 if ((olaps.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL)
648 return ICOM_SYS;
649
650 memset(&req, 0, sizeof(libusb_request));
651 req.endpoint.endpoint = endpoint;
652
653 if (!DeviceIoControl(p->usbd->handle,
654 dirw ? LIBUSB_IOCTL_INTERRUPT_OR_BULK_WRITE
655 : LIBUSB_IOCTL_INTERRUPT_OR_BULK_READ,
656 &req, sizeof(libusb_request),
657 buffer,
658 length, &xlength, &olaps)) {
659 if (GetLastError() != ERROR_IO_PENDING) {
660 rv = dirw ? ICOM_USBW : ICOM_USBR;
661 goto done;
662 }
663
664 if (cancelt != NULL) {
665 amutex_lock(cancelt->cmtx);
666 cancelt->hcancel = (void *)&endpoint;
667 cancelt->state = 1;
668 amutex_unlock(cancelt->condx); /* Signal any thread waiting for IO start */
669 amutex_unlock(cancelt->cmtx);
670 }
671
672 if (WaitForSingleObject(olaps.hEvent, timeout) == WAIT_TIMEOUT) {
673
674 /* Cancel the operation, because it timed out */
675 memset(&req, 0, sizeof(libusb_request));
676 req.endpoint.endpoint = endpoint;
677 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
678 do_sync_io(p->usbd->handle, LIBUSB_IOCTL_ABORT_ENDPOINT,
679 &req, sizeof(libusb_request), NULL, 0, NULL);
680 rv = ICOM_TO;
681 }
682
683 if (!GetOverlappedResult(p->usbd->handle, &olaps, &xlength, TRUE)) {
684 if (rv == ICOM_OK) {
685 if (GetLastError() == ERROR_OPERATION_ABORTED)
686 rv = ICOM_CANC;
687 else
688 rv = dirw ? ICOM_USBW : ICOM_USBR;
689 }
690 }
691 }
692 done:;
693 if (cancelt != NULL) {
694 amutex_lock(cancelt->cmtx);
695 cancelt->hcancel = (void *)NULL;
696 if (cancelt->state == 0) {
697 amutex_unlock(cancelt->condx); /* Make sure this gets unlocked */
698 }
699 cancelt->state = 2;
700 amutex_unlock(cancelt->cmtx);
701 }
702
703 CloseHandle(olaps.hEvent);
704
705 if (transferred != NULL)
706 *transferred = (int)xlength;
707
708 /* The requested size wasn't transferred */
709 if (rv == ICOM_OK && xlength != length)
710 rv |= ICOM_SHORT;
711
712 if (in_usb_rw < 0)
713 exit(0);
714
715 in_usb_rw--;
716
717 a1logd(p->log, 8, "coms_usb_transaction: returning err 0x%x and %d bytes\n",rv, xlength);
718
719 return rv;
720 }
721
722
723 /* Our control message routine */
724 /* Return error icom error code */
725 static int icoms_usb_control_msg_w0(
726 icoms *p,
727 int *transferred,
728 int requesttype,
729 int request,
730 int value,
731 int index,
732 unsigned char *bytes,
733 int size,
734 int timeout
735 ) {
736 int rv = ICOM_OK;
737 int dirw = (requesttype & IUSB_REQ_DIR_MASK) == IUSB_REQ_HOST_TO_DEV ? 1 : 0;
738 libusb_request req;
739 unsigned char *obuf = (unsigned char *)&req;
740 int osize = sizeof(libusb_request);
741 unsigned char *ibuf = bytes;
742 int isize = size;
743 int ioctl = 0;
744 int retsz = 0;
745
746 a1logd(p->log, 8, "icoms_usb_control_msg_w0: type 0x%x req 0x%x size %d\n",requesttype,request,size);
747
748 if (transferred != NULL)
749 *transferred = 0;
750
751 memset(&req, 0, sizeof(libusb_request));
752 req.timeout = timeout;
753
754 /* We need to treat each request type as a different IOCTL */
755 switch (requesttype & IUSB_REQ_TYPE_MASK) {
756
757 case IUSB_REQ_TYPE_STANDARD:
758
759 switch (request) {
760 case IUSB_REQ_GET_STATUS:
761 req.status.recipient = requesttype & IUSB_REQ_RECIP_MASK;
762 req.status.index = index;
763 ioctl = LIBUSB_IOCTL_GET_STATUS;
764 break;
765
766 case IUSB_REQ_CLEAR_FEATURE:
767 req.feature.recipient = requesttype & IUSB_REQ_RECIP_MASK;
768 req.feature.feature = value;
769 req.feature.index = index;
770 ioctl = LIBUSB_IOCTL_CLEAR_FEATURE;
771 break;
772
773 case IUSB_REQ_SET_FEATURE:
774 req.feature.recipient = requesttype & IUSB_REQ_RECIP_MASK;
775 req.feature.feature = value;
776 req.feature.index = index;
777 ioctl = LIBUSB_IOCTL_SET_FEATURE;
778 break;
779
780 case IUSB_REQ_GET_DESCRIPTOR:
781 req.descriptor.recipient = requesttype & IUSB_REQ_RECIP_MASK;
782 req.descriptor.type = (value >> 8) & 0xFF;
783 req.descriptor.index = value & 0xFF;
784 req.descriptor.language_id = index;
785 ioctl = LIBUSB_IOCTL_GET_DESCRIPTOR;
786 break;
787
788 case IUSB_REQ_SET_DESCRIPTOR:
789 req.descriptor.recipient = requesttype & IUSB_REQ_RECIP_MASK;
790 req.descriptor.type = (value >> 8) & 0xFF;
791 req.descriptor.index = value & 0xFF;
792 req.descriptor.language_id = index;
793 ioctl = LIBUSB_IOCTL_SET_DESCRIPTOR;
794 break;
795
796 case IUSB_REQ_GET_CONFIGURATION:
797 ioctl = LIBUSB_IOCTL_GET_CONFIGURATION;
798 break;
799
800 case IUSB_REQ_SET_CONFIGURATION:
801 req.configuration.configuration = value;
802 ioctl = LIBUSB_IOCTL_SET_CONFIGURATION;
803 break;
804
805 case IUSB_REQ_GET_INTERFACE:
806 req.intf.interface_number = index;
807 ioctl = LIBUSB_IOCTL_GET_INTERFACE;
808 break;
809
810 case IUSB_REQ_SET_INTERFACE:
811 req.intf.interface_number = index;
812 req.intf.altsetting_number = value;
813 ioctl = LIBUSB_IOCTL_SET_INTERFACE;
814 break;
815
816 default:
817 return ICOM_SYS;
818 }
819 break;
820
821 case IUSB_REQ_TYPE_VENDOR:
822 case IUSB_REQ_TYPE_CLASS:
823
824 req.vendor.type = (requesttype & IUSB_REQ_TYPE_MASK) >> IUSB_REQ_TYPE_SHIFT;
825 req.vendor.recipient = requesttype & IUSB_REQ_RECIP_MASK;
826 req.vendor.request = request;
827 req.vendor.value = value;
828 req.vendor.index = index;
829
830 if (dirw)
831 ioctl = LIBUSB_IOCTL_VENDOR_WRITE;
832 else
833 ioctl = LIBUSB_IOCTL_VENDOR_READ;
834 break;
835
836 case IUSB_REQ_TYPE_RESERVED:
837 default:
838 return ICOM_SYS;
839 }
840
841 /* If we're writing the data, append it to the req */
842 if (dirw) {
843 osize = sizeof(libusb_request) + size;
844 if ((obuf = calloc(1, osize)) == NULL) {
845 a1loge(p->log, ICOM_SYS, "icoms_usb_control_msg_w0: calloc failed\n");
846 return ICOM_SYS;
847 }
848
849 memcpy(obuf, &req, sizeof(libusb_request));
850 memcpy(obuf + sizeof(libusb_request), bytes, size);
851 ibuf = NULL;
852 isize = 0;
853 }
854
855 if ((rv = do_sync_io(p->usbd->handle, ioctl, obuf, osize, ibuf, isize, &retsz))
856 != ICOM_OK) {
857 if (dirw)
858 free(obuf);
859 return rv;
860 }
861
862 if (dirw) {
863 free(obuf);
864 retsz = size;
865 }
866
867 if (transferred != NULL) /* Adjust for header size requested */
868 *transferred = retsz;
869
870 /* The requested size wasn't transferred */
871 if (rv == ICOM_OK && retsz != size)
872 rv |= ICOM_SHORT;
873
874 a1logd(p->log, 8, "icoms_usb_control_msg_w0: returning err 0x%x and %d bytes\n",rv, *transferred);
875 return rv;
876 }
877
878 /* Cancel i/o operation in another thread. */
879 /* Only Vista has CancelIoEx that can cancel a single operation, */
880 /* so we cancel the io to the end point, which will */
881 /* achieve what we want. */
882 int icoms_usb_cancel_io_w0(
883 icoms *p,
884 usb_cancelt *cancelt
885 ) {
886 int rv = ICOM_OK;
887 amutex_lock(cancelt->cmtx);
888 if (cancelt->hcancel != NULL) {
889 libusb_request req;
890
891 memset(&req, 0, sizeof(libusb_request));
892 req.endpoint.endpoint = *((unsigned char *)cancelt->hcancel);
893 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
894
895 if ((rv = do_sync_io(p->usbd->handle, LIBUSB_IOCTL_ABORT_ENDPOINT,
896 &req, sizeof(libusb_request), NULL, 0, NULL)) != ICOM_OK) {
897 a1logd(p->log, 1, "icoms_usb_cancel_io_w0: failed with 0x%x\n",rv);
898 }
899 }
900 amutex_unlock(cancelt->cmtx);
901
902 return rv;
903 }
904
905 /* - - - - - - - - - - - - - - - - - - - - - - - - - */
906 /* Reset and end point data toggle to 0 */
907 int icoms_usb_resetep_w0(
908 icoms *p,
909 int ep /* End point address */
910 ) {
911 libusb_request req;
912 int rv = ICOM_OK;
913
914 memset(&req, 0, sizeof(libusb_request));
915 req.endpoint.endpoint = ep;
916 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
917
918 if ((rv = do_sync_io(p->usbd->handle, LIBUSB_IOCTL_RESET_ENDPOINT, &req,
919 sizeof(libusb_request), NULL, 0, NULL)) != ICOM_OK) {
920 a1logd(p->log, 1, "icoms_usb_resetep_w0 failed with %d\n",rv);
921 return rv;
922 }
923 return rv;
924 }
925
926 /* - - - - - - - - - - - - - - - - - - - - - - - - - */
927 /* Clear a halt on an end point */
928 /* (Actually does a resetep) */
929 int icoms_usb_clearhalt_w0(
930 icoms *p,
931 int ep /* End point address */
932 ) {
933 libusb_request req;
934 int rv = ICOM_OK;
935
936 memset(&req, 0, sizeof(libusb_request));
937 req.endpoint.endpoint = ep;
938 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
939
940 if ((rv = do_sync_io(p->usbd->handle, LIBUSB_IOCTL_RESET_ENDPOINT, &req,
941 sizeof(libusb_request), NULL, 0, NULL)) != ICOM_OK) {
942 a1logd(p->log, 1, "icoms_resetpipe/clearhalt_w0 failed with %d\n",rv);
943 return rv;
944 }
945 return rv;
946 }
947
948 /* - - - - - - - - - - - - - - - - - - - - - - - - - */
949
950 #ifdef NEVER
951 libusb_request req;
952 unsigned char buf[1] = { 0xff };
953
954 memset(&req, 0, sizeof(libusb_request));
955 req.timeout = LIBUSBW1_DEFAULT_TIMEOUT;
956
957 if ((rv = do_sync_io(p->usbd->handle, LIBUSB_IOCTL_GET_CONFIGURATION,
958 &req, sizeof(libusb_request), buf, 1, NULL)) != ICOM_OK) {
959
960 a1logd(p->log, 1, "usb_open_port_w0: Getting port '%s' configuration failed with %d\n",p->usbd->dpath,rv);
961 /* Ignore error */
962 } else {
963 a1logd(p->log, 1, "usb_open_port_w0: current config = %d\n",(int)buf[0]);
964 }
965 config = buf[0];
966 #endif // NEVER
967