Debian Codegraph @

spectro/usbio_bsd.c

raw symbols SCIP
1
2 /* General USB I/O support, BSD native 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 #pragma message("!!!!!! usbio_bsd.c is INCOMPLETE and USB instruments will NOT WORK !!!!!!")
19
20 /*
21 !!!! This driver is incomplete and non-functional !!!!
22
23 ( Most of the below code is stubbed out, with the Linux
24 code as a placeholder. )
25
26 BSD uses fd per end point, so simplifies things (good).
27
28 No clear ep or abort i/o though, so we could try clear halt,
29 or close fd and see if that works in aborting transaction ?
30
31 Posix aio would probably work, but it's not loaded by default :-(
32
33 Could use libusb20 API, but not backwards or cross compatible,
34 and is very likely to be buggy ?
35
36 Not clear how to proceed.
37
38 */
39
40 #include <unistd.h>
41 #include <dirent.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <glob.h>
46 #include <sys/ioctl.h>
47 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
48 # include <dev/usb/usb_ioctl.h> /* Not sure what's going on with FreeBSD... */
49 #else
50 # include <dev/usb/usb.h> /* The usual include for BSD */
51 #endif
52
53 /* select() defined, but not poll(), so emulate poll() */
54 #if defined(FD_CLR) && !defined(POLLIN)
55 #include "pollem.h"
56 #define poll_x pollem
57 #else
58 #include <sys/poll.h> /* Else assume poll() is native */
59 #define poll_x poll
60 #endif
61
62 /* Add paths to USB connected device */
63 /* Return an icom error */
64 int usb_get_paths(
65 icompaths *p
66 ) {
67 int i, j;
68 char *paths[] = {
69 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
70 "/dev/usb/[0-9]*.*.0", /* FreeBSD >= 8 */
71 "/dev/ugen[0-9]*", /* FreeBSD < 8, but no .E */
72 #else
73 "/dev/ugen/[0-9]*.00", /* NetBSD, OpenBSD */
74 #endif
75 NULL
76 };
77 int vid, pid;
78 int nconfig = 0, nep = 0;
79 char *dpath;
80 devType itype;
81 struct usb_idevice *usbd = NULL;
82
83 a1logd(p->log, 6, "usb_get_paths: about to look through usb devices:\n");
84
85 /* See what device names to look for */
86 for (j = 0; ; j++) {
87 glob_t g;
88 int fd;
89 struct usb_device_info di;
90 int rv, found = 0;
91
92 if (paths[j] == NULL)
93 break;
94
95 memset(&g, 0, sizeof(g));
96
97 if (glob(paths[j], GLOB_NOSORT, NULL, &g) != 0) {
98 continue;
99 }
100
101 /* For all the nodes found by the glob */
102 for (i = 0; i < g.gl_pathc; i++) {
103
104 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
105 /* Skip anything with an end point number */
106 if (j == 1 && strchr(g.gl_pathv[i], '.') != NULL)
107 continue;
108 #endif
109 if ((fd = open(g.gl_pathv[i], O_RDONLY)) < 0)
110 continue;
111
112 if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0)
113 continue;
114
115 vid = di.udi_vendorNo;
116 pid = di.udi_productNo;
117
118 a1logd(p->log, 6, "usb_get_paths: checking vid 0x%04x, pid 0x%04x\n",vid,pid);
119
120 /* Do a preliminary match */
121 if ((itype = inst_usb_match(vid, pid, 0)) == instUnknown) {
122 a1logd(p->log, 6 , "usb_get_paths: instrument not reconized\n");
123 continue;
124 }
125
126 // ~~99 need to check number of end points ~~~
127 // ~~99 and number of configs
128 // ~~99 and read any USB serial numbert
129 # pragma message("######### usbio_bsd.c missing USB code !!!!! ########")
130
131 nconfig = 1;
132
133 //USB_GET_DEVICEINFO struct usb_device_info
134 //USB_GET_DEVICE_DESC struct usb_device_descriptor
135
136 /* Allocate an idevice so that we can fill in the end point information */
137 if ((usbd = (struct usb_idevice *) calloc(sizeof(struct usb_idevice), 1)) == NULL) {
138 a1loge(p->log, ICOM_SYS, "icoms: calloc failed!\n");
139 close(fd);
140 globfree(&g);
141 return ICOM_SYS;
142 }
143
144 usbd->nconfig = nconfig;
145
146 /* Found a known instrument ? */
147 if ((itype = inst_usb_match(vid, pid, nep)) != instUnknown) {
148 char pname[400], *cp;
149
150 a1logd(p->log, 2, "usb_get_paths: found instrument vid 0x%04x, pid 0x%04x\n",vid,pid);
151
152 /* Create the base device path */
153 dpath = g.gl_pathv[i];
154 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
155 if (j == 0) { /* Remove .0 */
156 if ((cp = strrchr(dpath, '.')) != NULL
157 && cp[1] == '0' && cp[2] == '\000')
158 *cp = '\000';
159 }
160 #else
161 /* Remove .00 */
162 if ((cp = strrchr(dpath, '.')) != NULL
163 && cp[1] == '0' && cp[2] == '0' && cp[3] == '\000')
164 *cp = '\000';
165 #endif
166 if ((usbd->dpath = strdup(dpath)) == NULL) {
167 a1loge(p->log, ICOM_SYS, "usb_get_paths: strdup path failed!\n");
168 free(usbd);
169 close(fd);
170 globfree(&g);
171 return ICOM_SYS;
172 }
173
174 /* Create a path/identification */
175 sprintf(pname,"%s (%s)", dpath, inst_name(itype));
176 if ((usbd->dpath = strdup(dpath)) == NULL) {
177 a1loge(p->log, ICOM_SYS, "usb_get_paths: strdup path failed!\n");
178 free(usbd);
179 close(fd);
180 globfree(&g);
181 return ICOM_SYS;
182 }
183
184 /* Add the path and ep info to the list */
185 if ((rv = p->add_usb(p, pname, vid, pid, nep, usbd, itype)) != ICOM_OK)
186 return rv;
187
188
189 } else {
190 free(usbd);
191 }
192
193 close(fd);
194 }
195 globfree(&g);
196
197 /* Only try one glob string */
198 break;
199 }
200
201 a1logd(p->log, 8, "usb_get_paths: returning %d paths and ICOM_OK\n",p->ndpaths[dtix_combined]);
202 return ICOM_OK;
203 }
204
205 /* Copy usb_idevice contents from icompaths to icom */
206 /* return icom error */
207 int usb_copy_usb_idevice(icoms *d, icompath *s) {
208 int i;
209 if (s->usbd == NULL) {
210 d->usbd = NULL;
211 return ICOM_OK;
212 }
213 if ((d->usbd = calloc(sizeof(struct usb_idevice), 1)) == NULL) {
214 a1loge(d->log, ICOM_SYS, "usb_copy_usb_idevice: malloc\n");
215 return ICOM_SYS;
216 }
217 if ((d->usbd->dpath = strdup(s->usbd->dpath)) == NULL) {
218 a1loge(d->log, ICOM_SYS, "usb_copy_usb_idevice: malloc\n");
219 return ICOM_SYS;
220 }
221 /* Copy the ep info */
222 d->nconfig = s->usbd->nconfig;
223 d->nifce = s->usbd->nifce;
224 d->config = s->usbd->config;
225 for (i = 0; i < 32; i++)
226 d->ep[i] = s->usbd->ep[i]; /* Struct copy */
227 return ICOM_OK;
228 }
229
230 /* Cleanup and then free a usb dev entry */
231 void usb_del_usb_idevice(struct usb_idevice *usbd) {
232
233 if (usbd == NULL)
234 return;
235
236 if (usbd->dpath != NULL)
237 free(usbd->dpath);
238 free(usbd);
239 }
240
241 /* Cleanup any USB specific icoms state */
242 void usb_del_usb(icoms *p) {
243
244 usb_del_usb_idevice(p->usbd);
245 }
246
247 /* Close an open USB port */
248 /* If we don't do this, the port and/or the device may be left in an unusable state. */
249 void usb_close_port(icoms *p) {
250
251 a1logd(p->log, 6, "usb_close_port: called\n");
252
253 #ifdef NEVER // ~~99
254 if (p->is_open && p->usbd != NULL) {
255 struct usbdevfs_urb urb;
256 unsigned char buf[8+IUSB_DESC_TYPE_DEVICE_SIZE];
257 int iface, rv;
258
259 /* Release all the interfaces */
260 for (iface = 0; iface < p->nifce; iface++)
261 ioctl(p->usbd->fd, USBDEVFS_RELEASEINTERFACE, &iface);
262
263 /* Workaround for some bugs - reset device on close */
264 if (p->uflags & icomuf_reset_before_close) {
265 a1logd(p->log, 6, "usb_close_port: icomuf_reset_before_close\n");
266 if ((rv = ioctl(p->usbd->fd, USBDEVFS_RESET, NULL)) != 0) {
267 a1logd(p->log, 1, "usb_close_port: reset returned %d\n",rv);
268 }
269 }
270
271 if (p->usbd->running) { /* If reaper is still running */
272 unsigned char buf[1] = { 0 };
273
274 a1logd(p->log, 6, "usb_close_port: waking reaper thread to trigger exit\n");
275 p->usbd->shutdown = 1;
276
277 if (write(p->usbd->sd_pipe[1], buf, 1) < 1) {
278 a1logd(p->log, 1, "usb_close_port: writing to sd_pipe failed with '%s'\n", strerror(errno));
279 /* Hmm. We could be in trouble ? */
280 }
281 }
282 a1logd(p->log, 6, "usb_close_port: waiting for reaper thread\n");
283 pthread_join(p->usbd->thread, NULL); /* Wait for urb reaper thread to exit */
284 close(p->usbd->fd);
285 pthread_mutex_destroy(&p->usbd->lock);
286 close(p->usbd->sd_pipe[0]);
287 close(p->usbd->sd_pipe[1]);
288
289 a1logd(p->log, 6, "usb_close_port: usb port has been released and closed\n");
290 }
291 p->is_open = 0;
292 #endif // ~~99
293
294 /* Find it and delete it from our static cleanup list */
295 usb_delete_from_cleanup_list(p);
296 }
297
298 static void *urb_reaper(void *context); /* Declare */
299
300 /* Open a USB port for all our uses. */
301 /* This always re-opens the port */
302 /* return icom error */
303 static int usb_open_port(
304 icoms *p,
305 int config, /* Configuration number */
306 int wr_ep, /* Write end point */
307 int rd_ep, /* Read end point */
308 icomuflags usbflags,/* Any special handling flags */
309 int retries, /* > 0 if we should retry set_configuration (100msec) */
310 char **pnames /* List of process names to try and kill before opening */
311 ) {
312 int rv, tries = 0;
313 a1logd(p->log, 8, "usb_open_port: Make sure USB port is open, tries %d\n",retries);
314
315 if (p->is_open)
316 p->close_port(p);
317
318 #ifdef NEVER // ~~99
319 /* Make sure the port is open */
320 if (!p->is_open) {
321 int rv, i, iface;
322 kkill_nproc_ctx *kpc = NULL;
323
324 if (config != 1) {
325 /* Nothing currently needs it, so we haven't implemented it yet... */
326 a1loge(p->log, ICOM_NOTS, "usb_open_port: native driver cant handle config %d\n",config);
327 return ICOM_NOTS;
328 }
329
330 /* Do open retries */
331 for (tries = 0; retries >= 0; retries--, tries++) {
332
333 a1logd(p->log, 8, "usb_open_port: About to open USB port '%s'\n",p->usbd->dpath);
334
335 if (tries > 0) {
336 //msec_sleep(i_rand(50,100));
337 msec_sleep(77);
338 }
339
340 p->uflags = usbflags;
341
342 /* We should only do a set configuration if the device has more than one */
343 /* possible configuration and it is currently not the desired configuration, */
344 /* but we should avoid doing a set configuration if the OS has already */
345 /* selected the configuration we want, since two set configs seem to */
346 /* mess up the Spyder2, BUT we can't do a get config because this */
347 /* messes up the i1pro-D. */
348
349 /* Linux set_configuration(1) by default, so we don't need to do anything */
350 p->cconfig = 1;
351
352 if (p->cconfig != config) {
353 if ((rv = ioctl(p->usbd->fd, USBDEVFS_SETCONFIGURATION, &config)) != 0) {
354 a1logd(p->log, 1, "icoms_usb_setconfig failed with %d\n",rv);
355 return ICOM_SYS;
356 }
357 p->cconfig = config;
358 a1logd(p->log, 6, "usb_open_port: set config %d OK\n",config);
359 }
360
361 /* We're done */
362 break;
363 }
364
365 if (kpc != NULL)
366 kpc->del(kpc);
367
368 /* Claim all the interfaces */
369 for (iface = 0; iface < p->nifce; iface++) {
370
371 if ((rv = ioctl(p->usbd->fd, USBDEVFS_CLAIMINTERFACE, &iface)) < 0) {
372 struct usbdevfs_getdriver getd;
373 getd.interface = iface;
374
375 a1logd(p->log, 1, "usb_open_port: Claiming USB port '%s' interface %d initally failed with %d\n",p->usbd->dpath,iface,rv);
376
377 /* Detatch the existing interface if kernel driver is active. */
378 if (p->uflags & icomuf_detach
379 && ioctl(p->usbd->fd, USBDEVFS_GETDRIVER, &getd) == 0) {
380 struct usbdevfs_ioctl cmd;
381 a1logd(p->log, 1, "usb_open_port: Attempting kernel detach\n");
382 cmd.ifno = iface;
383 cmd.ioctl_code = USBDEVFS_DISCONNECT;
384 cmd.data = NULL;
385 ioctl(p->usbd->fd, USBDEVFS_IOCTL, &cmd);
386 if ((rv = ioctl(p->usbd->fd, USBDEVFS_CLAIMINTERFACE, &iface)) < 0) {
387 a1loge(p->log, ICOM_SYS, "usb_open_port: Claiming USB port '%s' interface %d failed after detach with %d\n",p->usbd->dpath,iface,rv);
388 return ICOM_SYS;
389 }
390 } else {
391 a1loge(p->log, ICOM_SYS, "usb_open_port: Claiming USB port '%s' interface %d failed with %d\n",p->usbd->dpath,iface,rv);
392 return ICOM_SYS;
393 }
394 }
395 }
396
397 /* Clear any errors. */
398 /* (Some I/F seem to hang if we do this, some seem to hang if we don't !) */
399 /* The ColorMunki on Linux only starts every second time if we don't do this. */
400 if (!(p->uflags & icomuf_no_open_clear)) {
401 for (i = 0; i < 32; i++) {
402 if (!p->ep[i].valid)
403 continue;
404 p->usb_clearhalt(p, p->ep[i].addr);
405 }
406 }
407
408 /* Set "serial" coms values */
409 p->wr_ep = wr_ep;
410 p->rd_ep = rd_ep;
411 p->rd_qa = p->EPINFO(rd_ep).packetsize;
412 if (p->rd_qa == 0)
413 p->rd_qa = 8;
414 a1logd(p->log, 8, "usb_open_port: 'serial' read quanta = packet size = %d\n",p->rd_qa);
415
416 /* Start the reaper thread to handle URB completions */
417 if ((rv = pipe(p->usbd->sd_pipe)) < 0) {
418 a1loge(p->log, ICOM_SYS, "usb_open_port: creat pipe failed with %d\n",rv);
419 return ICOM_SYS;
420 }
421 pthread_mutex_init(&p->usbd->lock, NULL);
422
423 p->usbd->running = 1;
424 if ((rv = pthread_create(&p->usbd->thread, NULL, urb_reaper, (void*)p)) < 0) {
425 p->usbd->running = 0;
426 a1loge(p->log, ICOM_SYS, "usb_open_port: creating urb reaper thread failed with %s\n",rv);
427 return ICOM_SYS;
428 }
429
430 p->is_open = 1;
431 a1logd(p->log, 8, "usb_open_port: USB port is now open\n");
432 }
433
434 #endif // ~~99
435 /* Install the cleanup signal handlers, and add to our cleanup list */
436 usb_install_signal_handlers(p);
437
438 return ICOM_OK;
439 }
440
441 /* - - - - - - - - - - - - - - - - - - - - - */
442 /* Our universal USB transfer function */
443 static int icoms_usb_transaction(
444 icoms *p,
445 usb_cancelt *cancelt,
446 int *transferred,
447 icom_usb_trantype ttype, /* transfer type */
448 unsigned char endpoint, /* 0x80 for control write, 0x00 for control read */
449 unsigned char *buffer,
450 int length,
451 unsigned int timeout /* In msec */
452 ) {
453 int type;
454 int remlen;
455 unsigned char *bp;
456 int xlength = 0;
457 int i;
458 int reqrv = ICOM_OK;
459
460 #ifdef NEVER // ~~99
461 in_usb_rw++;
462 a1logd(p->log, 8, "icoms_usb_transaction: req type 0x%x ep 0x%x size %d\n",ttype,endpoint,length);
463
464 if (!p->usbd->running) {
465 in_usb_rw--;
466 a1logv(p->log, 1, "icoms_usb_transaction: reaper thread is not running\n");
467 return ICOM_SYS;
468 }
469
470 /* Translate icoms transfer type of Linux */
471 switch (ttype) {
472 case icom_usb_trantype_command:
473 type = USBDEVFS_URB_TYPE_CONTROL;
474 break;
475 case icom_usb_trantype_interrutpt:
476 type = USBDEVFS_URB_TYPE_INTERRUPT;
477 break;
478 case icom_usb_trantype_bulk:
479 type = USBDEVFS_URB_TYPE_BULK;
480 break;
481 }
482
483 /* Setup the icom req and urbs */
484 req.urbs = NULL;
485 pthread_mutex_init(&req.lock, NULL);
486 pthread_cond_init(&req.cond, NULL);
487
488 /* Linux historically only copes with 16384 length urbs, */
489 /* so break up longer requests into multiple urbs */
490
491 req.cancelled = 0;
492 req.nourbs = req.nurbs = (length + (1 << 14)-1) >> 14;
493 if ((req.urbs = (usbio_urb *)calloc(sizeof(usbio_urb), req.nourbs)) == NULL) {
494 in_usb_rw--;
495 a1loge(p->log, ICOM_SYS, "icoms_usb_transaction: control transfer too big! (%d)\n",length);
496 return ICOM_SYS;
497 }
498
499 bp = buffer;
500 remlen = length;
501 for (i = 0; i < req.nurbs; i++) {
502 req.urbs[i].req = &req;
503 req.urbs[i].urbno = i;
504 /* Setup Linux URB */
505 req.urbs[i].urb.usercontext = &req.urbs[i];
506 req.urbs[i].urb.type = type;
507 if (type != USBDEVFS_URB_TYPE_CONTROL)
508 req.urbs[i].urb.endpoint = endpoint;
509 if (remlen > 16384)
510 req.urbs[i].urb.buffer_length = 16384;
511 else
512 req.urbs[i].urb.buffer_length = remlen;
513 req.urbs[i].urb.buffer = (void *)bp;
514 remlen -= req.urbs[i].urb.buffer_length;
515 bp += req.urbs[i].urb.buffer_length;
516 req.urbs[i].urb.status = -EINPROGRESS;
517 }
518 a1logd(p->log, 8, "icoms_usb_transaction: reset req %p nourbs to %d\n",&req,req.nourbs);
519
520 /* Add our request to the req list so that it can be cancelled on reap failure */
521 pthread_mutex_lock(&p->usbd->lock);
522 req.next = p->usbd->reqs;
523 p->usbd->reqs = &req;
524 pthread_mutex_unlock(&p->usbd->lock);
525
526 /* submit the URBs */
527 for (i = 0; i < req.nurbs; i++) {
528 if ((rv = ioctl(p->usbd->fd, USBDEVFS_SUBMITURB, &req.urbs[i].urb)) < 0) {
529 a1logd(p->log, 1, "coms_usb_transaction: Submitting urb to fd %d failed with %d\n",p->usbd->fd, rv);
530 req.urbs[i].urb.status = ICOM_SYS; /* Mark it as failed to submit */
531 req.nourbs--;
532 }
533 }
534
535 if (cancelt != NULL) {
536 amutex_lock(cancelt->cmtx);
537 cancelt->hcancel = (void *)&req;
538 cancelt->state = 1;
539 amutex_unlock(cancelt->cond); /* Signal any thread waiting for IO start */
540 amutex_unlock(cancelt->cmtx);
541 }
542
543 /* Wait for the reaper to wake us, or for a timeout, */
544 /* or for the reaper to die. */
545 pthread_mutex_lock(&req.lock);
546 if (req.nourbs > 0) {
547 struct timeval tv;
548 struct timespec ts;
549
550 // this is unduly complicated...
551 gettimeofday(&tv, NULL);
552 ts.tv_sec = tv.tv_sec + timeout/1000;
553 ts.tv_nsec = (tv.tv_usec + (timeout % 1000) * 1000) * 1000L;
554 if (ts.tv_nsec > 1000000000L) {
555 ts.tv_nsec -= 1000000000L;
556 ts.tv_sec++;
557 }
558
559 for(;;) { /* Ignore spurious wakeups */
560 if ((rv = pthread_cond_timedwait(&req.cond, &req.lock, &ts)) != 0) {
561 if (rv != ETIMEDOUT) {
562 pthread_mutex_unlock(&req.lock);
563 a1logd(p->log, 1, "coms_usb_transaction: pthread_cond_timedwait failed with %d\n",rv);
564 rv = ICOM_SYS;
565 goto done;
566 }
567
568 /* Timed out - cancel the remaining URB's */
569 a1logd(p->log, 8, "coms_usb_transaction: time out - cancel remaining URB's\n");
570 reqrv = ICOM_TO;
571 if (!req.cancelled && (rv = cancel_req(p, &req, -1)) != ICOM_OK) {
572 pthread_mutex_unlock(&req.lock);
573 reqrv = ICOM_SYS;
574 /* Since cancelling failed, we can't wait for them to be reaped */
575 goto done;
576 }
577
578 /* Wait for the cancelled URB's to be reaped */
579 for (;req.nourbs > 0;) { /* Ignore spurious wakeups */
580 if ((rv = pthread_cond_wait(&req.cond, &req.lock)) != 0) {
581 pthread_mutex_unlock(&req.lock);
582 a1logd(p->log, 1, "coms_usb_transaction: pthread_cond_wait failed with %d\n",rv);
583 reqrv = ICOM_SYS;
584 /* Waiting for reap failed, so give up */
585 goto done;
586 }
587 }
588 } else {
589 a1logd(p->log, 8, "coms_usb_transaction: reap - %d left\n",req.nourbs);
590 }
591 if (req.nourbs <= 0)
592 break; /* All urbs's are done */
593 }
594 }
595 pthread_mutex_unlock(&req.lock);
596
597 /* Compute the overall result by going through the urbs. */
598 for (i = 0; i < req.nurbs; i++) {
599 int stat = req.urbs[i].urb.status;
600 xlength += req.urbs[i].urb.actual_length;
601
602 if (stat == ICOM_SYS) { /* Submit or cancel failed */
603 reqrv = ICOM_SYS;
604 } else if (reqrv == ICOM_OK && stat < 0 && stat != -ECONNRESET) { /* Error result */
605 if ((endpoint & IUSB_ENDPOINT_DIR_MASK) == IUSB_ENDPOINT_OUT)
606 reqrv = ICOM_USBW;
607 else
608 reqrv = ICOM_USBR;
609 } else if (reqrv == ICOM_OK && stat == -ECONNRESET) { /* Cancelled */
610 reqrv = ICOM_CANC;
611 } else if (reqrv == ICOM_OK
612 && req.urbs[i].urb.actual_length < req.urbs[i].urb.buffer_length) {
613 /* Disregard any following urb's status - they are probably cancelled */
614 break;
615 }
616 /* reqrv == ICOM_TO will ignore urb status */
617 }
618
619 if (ttype == icom_usb_trantype_command)
620 xlength += IUSB_REQ_HEADER_SIZE; /* Account for header - linux doesn't */
621
622 /* requested size wasn't transferred ? */
623 if (reqrv == ICOM_OK && xlength != length)
624 reqrv = ICOM_SHORT;
625
626 if (transferred != NULL)
627 *transferred = xlength;
628
629 done:;
630 if (cancelt != NULL) {
631 amutex_lock(cancelt->cmtx);
632 cancelt->hcancel = (void *)NULL;
633 if (cancelt->state == 0)
634 amutex_unlock(cancelt->cond);
635 cancelt->state = 2;
636 amutex_unlock(cancelt->cmtx);
637 }
638
639 /* Remove our request from the list */
640 pthread_mutex_lock(&p->usbd->lock);
641 preq = &p->usbd->reqs;
642 while (*preq != &req && *preq != NULL) /* Find it */
643 preq = &((*preq)->next);
644 if (*preq != NULL)
645 *preq = (*preq)->next;
646 pthread_mutex_unlock(&p->usbd->lock);
647
648 if (req.urbs != NULL)
649 free(req.urbs);
650 pthread_cond_destroy(&req.cond);
651 pthread_mutex_destroy(&req.lock);
652
653 if (in_usb_rw < 0)
654 exit(0);
655
656 in_usb_rw--;
657
658 a1logd(p->log, 8, "coms_usb_transaction: returning err 0x%x and %d bytes\n",reqrv, xlength);
659 #endif // ~~99
660
661 return reqrv;
662 }
663
664
665 /* Return error icom error code */
666 static int icoms_usb_control_msg(
667 icoms *p,
668 int *transferred,
669 int requesttype, int request,
670 int value, int index, unsigned char *bytes, int size,
671 int timeout) {
672 int reqrv = ICOM_OK;
673 int dirw = (requesttype & IUSB_REQ_DIR_MASK) == IUSB_REQ_HOST_TO_DEV ? 1 : 0;
674 unsigned char *buf;
675
676 a1logd(p->log, 8, "icoms_usb_control_msg: type 0x%x req 0x%x size %d\n",requesttype,request,size);
677
678 #ifdef NEVER // ~~99
679 /* Allocate a buffer for the ctrl header + payload */
680 if ((buf = calloc(1, IUSB_REQ_HEADER_SIZE + size)) == NULL) {
681 a1loge(p->log, ICOM_SYS, "icoms_usb_control_msg: calloc failed\n");
682 return ICOM_SYS;
683 }
684
685 /* Setup the control header */
686 buf[0] = requesttype;
687 buf[1] = request;
688 short2buf(buf + 2, value);
689 short2buf(buf + 4, index);
690 short2buf(buf + 6, size);
691
692 /* If it's a write, copy the write data into the buffer */
693 if (dirw)
694 memcpy(buf + IUSB_REQ_HEADER_SIZE, bytes, size);
695
696 reqrv = icoms_usb_transaction(p, NULL, transferred, icom_usb_trantype_command,
697 dirw ? 0x80 : 0x00, buf, IUSB_REQ_HEADER_SIZE + size, timeout);
698
699 /* If read, copy the data back */
700 if (!dirw)
701 memcpy(bytes, buf + IUSB_REQ_HEADER_SIZE, size);
702
703 if (transferred != NULL) /* Adjust for header size requested */
704 *transferred -= IUSB_REQ_HEADER_SIZE;
705
706 free(buf);
707
708 #endif // ~~99
709 a1logd(p->log, 8, "icoms_usb_control_msg: returning err 0x%x and %d bytes\n",reqrv, *transferred);
710 return reqrv;
711 }
712
713 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
714 /* Time out error return value */
715
716 #define USBIO_ERROR_TIMEOUT -ETIMEDOUT
717
718 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
719
720 /* Cancel i/o in another thread */
721 int icoms_usb_cancel_io(
722 icoms *p,
723 usb_cancelt *cancelt
724 ) {
725 int rv = ICOM_OK;
726 #ifdef NEVER // ~~99
727 a1logd(p->log, 8, "icoms_usb_cancel_io called\n");
728 usb_lock_cancel(cancelt);
729 if (cancelt->hcancel != NULL)
730 rv = cancel_req(p, (usbio_req *)cancelt->hcancel, -1);
731 usb_unlock_cancel(cancelt);
732
733 if (rv != ICOM_OK) /* Assume this could be because of faulty device */
734 rv = ICOM_USBW;
735 #endif // ~~99
736
737 return rv;
738 }
739
740 /* - - - - - - - - - - - - - - - - - - - - - - - - - */
741 /* Reset and end point data toggle to 0 */
742 int icoms_usb_resetep(
743 icoms *p,
744 int ep /* End point address */
745 ) {
746 int rv = ICOM_OK;
747
748 #ifdef NEVER // ~~99
749 if ((rv = ioctl(p->usbd->fd, USBDEVFS_RESETEP, &ep)) != 0) {
750 a1logd(p->log, 1, "icoms_usb_resetep failed with %d\n",rv);
751 rv = ICOM_USBW;
752 }
753 #endif // ~~99
754 return rv;
755 }
756
757 /* - - - - - - - - - - - - - - - - - - - - - - - - - */
758 /* Clear a halt on an end point */
759 int icoms_usb_clearhalt(
760 icoms *p,
761 int ep /* End point address */
762 ) {
763 int rv = ICOM_OK;
764
765 #ifdef NEVER // ~~99
766 if ((rv = ioctl(p->usbd->fd, USBDEVFS_CLEAR_HALT, &ep)) != 0) {
767 a1logd(p->log, 1, "icoms_usb_clearhalt failed with %d\n",rv);
768 rv = ICOM_USBW;
769 }
770 #endif // ~~99
771 return rv;
772 }
773
774 /* - - - - - - - - - - - - - - - - - - - - - - - - - */
775