| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#pragma message("!!!!!! usbio_bsd.c is INCOMPLETE and USB instruments will NOT WORK !!!!!!") |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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> |
| 49 |
#else |
| 50 |
# include <dev/usb/usb.h> |
| 51 |
#endif |
| 52 |
|
| 53 |
|
| 54 |
#if defined(FD_CLR) && !defined(POLLIN) |
| 55 |
#include "pollem.h" |
| 56 |
#define poll_x pollem |
| 57 |
#else |
| 58 |
#include <sys/poll.h> |
| 59 |
#define poll_x poll |
| 60 |
#endif |
| 61 |
|
| 62 |
|
| 63 |
|
| 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", |
| 71 |
"/dev/ugen[0-9]*", |
| 72 |
#else |
| 73 |
"/dev/ugen/[0-9]*.00", |
| 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 |
|
| 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 |
|
| 102 |
for (i = 0; i < g.gl_pathc; i++) { |
| 103 |
|
| 104 |
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
| 105 |
|
| 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 |
|
| 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 |
|
| 127 |
|
| 128 |
|
| 129 |
# pragma message("######### usbio_bsd.c missing USB code !!!!! ########") |
| 130 |
|
| 131 |
nconfig = 1; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 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 |
|
| 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 |
|
| 153 |
dpath = g.gl_pathv[i]; |
| 154 |
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
| 155 |
if (j == 0) { |
| 156 |
if ((cp = strrchr(dpath, '.')) != NULL |
| 157 |
&& cp[1] == '0' && cp[2] == '\000') |
| 158 |
*cp = '\000'; |
| 159 |
} |
| 160 |
#else |
| 161 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 206 |
|
| 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 |
|
| 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]; |
| 227 |
return ICOM_OK; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 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 |
|
| 242 |
void usb_del_usb(icoms *p) { |
| 243 |
|
| 244 |
usb_del_usb_idevice(p->usbd); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
void usb_close_port(icoms *p) { |
| 250 |
|
| 251 |
a1logd(p->log, 6, "usb_close_port: called\n"); |
| 252 |
|
| 253 |
#ifdef NEVER |
| 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 |
|
| 260 |
for (iface = 0; iface < p->nifce; iface++) |
| 261 |
ioctl(p->usbd->fd, USBDEVFS_RELEASEINTERFACE, &iface); |
| 262 |
|
| 263 |
|
| 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) { |
| 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 |
|
| 280 |
} |
| 281 |
} |
| 282 |
a1logd(p->log, 6, "usb_close_port: waiting for reaper thread\n"); |
| 283 |
pthread_join(p->usbd->thread, NULL); |
| 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 |
| 293 |
|
| 294 |
|
| 295 |
usb_delete_from_cleanup_list(p); |
| 296 |
} |
| 297 |
|
| 298 |
static void *urb_reaper(void *context); |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
static int usb_open_port( |
| 304 |
icoms *p, |
| 305 |
int config, |
| 306 |
int wr_ep, |
| 307 |
int rd_ep, |
| 308 |
icomuflags usbflags, |
| 309 |
int retries, |
| 310 |
char **pnames |
| 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 |
| 319 |
|
| 320 |
if (!p->is_open) { |
| 321 |
int rv, i, iface; |
| 322 |
kkill_nproc_ctx *kpc = NULL; |
| 323 |
|
| 324 |
if (config != 1) { |
| 325 |
|
| 326 |
a1loge(p->log, ICOM_NOTS, "usb_open_port: native driver cant handle config %d\n",config); |
| 327 |
return ICOM_NOTS; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 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 |
|
| 337 |
msec_sleep(77); |
| 338 |
} |
| 339 |
|
| 340 |
p->uflags = usbflags; |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 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 |
|
| 362 |
break; |
| 363 |
} |
| 364 |
|
| 365 |
if (kpc != NULL) |
| 366 |
kpc->del(kpc); |
| 367 |
|
| 368 |
|
| 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 |
|
| 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 |
|
| 398 |
|
| 399 |
|
| 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 |
|
| 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 |
|
| 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 |
| 435 |
|
| 436 |
usb_install_signal_handlers(p); |
| 437 |
|
| 438 |
return ICOM_OK; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
static int icoms_usb_transaction( |
| 444 |
icoms *p, |
| 445 |
usb_cancelt *cancelt, |
| 446 |
int *transferred, |
| 447 |
icom_usb_trantype ttype, |
| 448 |
unsigned char endpoint, |
| 449 |
unsigned char *buffer, |
| 450 |
int length, |
| 451 |
unsigned int timeout |
| 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 |
| 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 |
|
| 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 |
|
| 484 |
req.urbs = NULL; |
| 485 |
pthread_mutex_init(&req.lock, NULL); |
| 486 |
pthread_cond_init(&req.cond, NULL); |
| 487 |
|
| 488 |
|
| 489 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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; |
| 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); |
| 540 |
amutex_unlock(cancelt->cmtx); |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
pthread_mutex_lock(&req.lock); |
| 546 |
if (req.nourbs > 0) { |
| 547 |
struct timeval tv; |
| 548 |
struct timespec ts; |
| 549 |
|
| 550 |
|
| 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(;;) { |
| 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 |
|
| 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 |
|
| 575 |
goto done; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
for (;req.nourbs > 0;) { |
| 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 |
|
| 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; |
| 593 |
} |
| 594 |
} |
| 595 |
pthread_mutex_unlock(&req.lock); |
| 596 |
|
| 597 |
|
| 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) { |
| 603 |
reqrv = ICOM_SYS; |
| 604 |
} else if (reqrv == ICOM_OK && stat < 0 && stat != -ECONNRESET) { |
| 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) { |
| 610 |
reqrv = ICOM_CANC; |
| 611 |
} else if (reqrv == ICOM_OK |
| 612 |
&& req.urbs[i].urb.actual_length < req.urbs[i].urb.buffer_length) { |
| 613 |
|
| 614 |
break; |
| 615 |
} |
| 616 |
|
| 617 |
} |
| 618 |
|
| 619 |
if (ttype == icom_usb_trantype_command) |
| 620 |
xlength += IUSB_REQ_HEADER_SIZE; |
| 621 |
|
| 622 |
|
| 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 |
|
| 640 |
pthread_mutex_lock(&p->usbd->lock); |
| 641 |
preq = &p->usbd->reqs; |
| 642 |
while (*preq != &req && *preq != NULL) |
| 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 |
| 660 |
|
| 661 |
return reqrv; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 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 |
| 679 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 700 |
if (!dirw) |
| 701 |
memcpy(bytes, buf + IUSB_REQ_HEADER_SIZE, size); |
| 702 |
|
| 703 |
if (transferred != NULL) |
| 704 |
*transferred -= IUSB_REQ_HEADER_SIZE; |
| 705 |
|
| 706 |
free(buf); |
| 707 |
|
| 708 |
#endif |
| 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 |
|
| 715 |
|
| 716 |
#define USBIO_ERROR_TIMEOUT -ETIMEDOUT |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
int icoms_usb_cancel_io( |
| 722 |
icoms *p, |
| 723 |
usb_cancelt *cancelt |
| 724 |
) { |
| 725 |
int rv = ICOM_OK; |
| 726 |
#ifdef NEVER |
| 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) |
| 734 |
rv = ICOM_USBW; |
| 735 |
#endif |
| 736 |
|
| 737 |
return rv; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
int icoms_usb_resetep( |
| 743 |
icoms *p, |
| 744 |
int ep |
| 745 |
) { |
| 746 |
int rv = ICOM_OK; |
| 747 |
|
| 748 |
#ifdef NEVER |
| 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 |
| 754 |
return rv; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
int icoms_usb_clearhalt( |
| 760 |
icoms *p, |
| 761 |
int ep |
| 762 |
) { |
| 763 |
int rv = ICOM_OK; |
| 764 |
|
| 765 |
#ifdef NEVER |
| 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 |
| 771 |
return rv; |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|