usb.c (15811B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2020 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #ifdef HAVE_CONFIG_H 23 #include "config.h" 24 #endif 25 26 #include <stdlib.h> 27 #include <string.h> 28 29 #ifdef HAVE_LIBUSB 30 #ifdef _WIN32 31 #define WIN32_LEAN_AND_MEAN 32 #define NOGDI 33 #endif 34 #include <libusb.h> 35 #endif 36 37 #include <libdivecomputer/usb.h> 38 39 #include "common-private.h" 40 #include "context-private.h" 41 #include "iostream-private.h" 42 #include "iterator-private.h" 43 #include "platform.h" 44 #include "array.h" 45 46 #define ISINSTANCE(device) dc_iostream_isinstance((device), &dc_usb_vtable) 47 48 typedef struct dc_usb_params_t { 49 unsigned int interface; 50 unsigned char endpoint_in; 51 unsigned char endpoint_out; 52 } dc_usb_params_t; 53 54 typedef struct dc_usb_config_t { 55 dc_usb_desc_t desc; 56 dc_usb_params_t params; 57 } dc_usb_config_t; 58 59 typedef struct dc_usb_session_t { 60 size_t refcount; 61 #ifdef HAVE_LIBUSB 62 libusb_context *handle; 63 #endif 64 } dc_usb_session_t; 65 66 struct dc_usb_device_t { 67 unsigned short vid, pid; 68 dc_usb_session_t *session; 69 #ifdef HAVE_LIBUSB 70 struct libusb_device *handle; 71 int interface; 72 unsigned char endpoint_in; 73 unsigned char endpoint_out; 74 #endif 75 }; 76 77 #ifdef HAVE_LIBUSB 78 static dc_status_t dc_usb_iterator_next (dc_iterator_t *iterator, void *item); 79 static dc_status_t dc_usb_iterator_free (dc_iterator_t *iterator); 80 81 static dc_status_t dc_usb_set_timeout (dc_iostream_t *iostream, int timeout); 82 static dc_status_t dc_usb_poll (dc_iostream_t *iostream, int timeout); 83 static dc_status_t dc_usb_read (dc_iostream_t *iostream, void *data, size_t size, size_t *actual); 84 static dc_status_t dc_usb_write (dc_iostream_t *iostream, const void *data, size_t size, size_t *actual); 85 static dc_status_t dc_usb_ioctl (dc_iostream_t *iostream, unsigned int request, void *data, size_t size); 86 static dc_status_t dc_usb_close (dc_iostream_t *iostream); 87 88 typedef struct dc_usb_iterator_t { 89 dc_iterator_t base; 90 dc_descriptor_t *descriptor; 91 dc_usb_session_t *session; 92 struct libusb_device **devices; 93 size_t count; 94 size_t current; 95 } dc_usb_iterator_t; 96 97 typedef struct dc_usb_t { 98 /* Base class. */ 99 dc_iostream_t base; 100 /* Internal state. */ 101 dc_usb_session_t *session; 102 libusb_device_handle *handle; 103 int interface; 104 unsigned char endpoint_in; 105 unsigned char endpoint_out; 106 unsigned int timeout; 107 } dc_usb_t; 108 109 static const dc_iterator_vtable_t dc_usb_iterator_vtable = { 110 sizeof(dc_usb_iterator_t), 111 dc_usb_iterator_next, 112 dc_usb_iterator_free, 113 }; 114 115 static const dc_iostream_vtable_t dc_usb_vtable = { 116 sizeof(dc_usb_t), 117 dc_usb_set_timeout, /* set_timeout */ 118 NULL, /* set_break */ 119 NULL, /* set_dtr */ 120 NULL, /* set_rts */ 121 NULL, /* get_lines */ 122 NULL, /* get_available */ 123 NULL, /* configure */ 124 dc_usb_poll, /* poll */ 125 dc_usb_read, /* read */ 126 dc_usb_write, /* write */ 127 dc_usb_ioctl, /* ioctl */ 128 NULL, /* flush */ 129 NULL, /* purge */ 130 NULL, /* sleep */ 131 dc_usb_close, /* close */ 132 }; 133 134 static const dc_usb_config_t g_usb_config[] = { 135 // Atomic Aquatics Cobalt 136 {{0x0471, 0x0888}, {0, 0x82, 0x02}}, 137 }; 138 139 static const dc_usb_params_t * 140 dc_usb_params_find (const dc_usb_desc_t *desc) 141 { 142 if (desc == NULL) 143 return NULL; 144 145 for (size_t i = 0; i < C_ARRAY_SIZE(g_usb_config); ++i) { 146 if (g_usb_config[i].desc.vid == desc->vid && 147 g_usb_config[i].desc.pid == desc->pid) 148 return &g_usb_config[i].params; 149 } 150 151 return NULL; 152 } 153 154 static dc_status_t 155 syserror(int errcode) 156 { 157 switch (errcode) { 158 case LIBUSB_ERROR_INVALID_PARAM: 159 return DC_STATUS_INVALIDARGS; 160 case LIBUSB_ERROR_NO_MEM: 161 return DC_STATUS_NOMEMORY; 162 case LIBUSB_ERROR_NO_DEVICE: 163 case LIBUSB_ERROR_NOT_FOUND: 164 return DC_STATUS_NODEVICE; 165 case LIBUSB_ERROR_ACCESS: 166 case LIBUSB_ERROR_BUSY: 167 return DC_STATUS_NOACCESS; 168 case LIBUSB_ERROR_TIMEOUT: 169 return DC_STATUS_TIMEOUT; 170 default: 171 return DC_STATUS_IO; 172 } 173 } 174 175 static dc_status_t 176 dc_usb_session_new (dc_usb_session_t **out, dc_context_t *context) 177 { 178 dc_status_t status = DC_STATUS_SUCCESS; 179 dc_usb_session_t *session = NULL; 180 181 if (out == NULL) 182 return DC_STATUS_INVALIDARGS; 183 184 session = (dc_usb_session_t *) malloc (sizeof(dc_usb_session_t)); 185 if (session == NULL) { 186 ERROR (context, "Failed to allocate memory."); 187 status = DC_STATUS_NOMEMORY; 188 goto error_unlock; 189 } 190 191 session->refcount = 1; 192 193 #if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x0100010A) 194 int rc = libusb_init_context (&session->handle, NULL, 0); 195 #else 196 int rc = libusb_init (&session->handle); 197 #endif 198 if (rc != LIBUSB_SUCCESS) { 199 ERROR (context, "Failed to initialize usb support (%s).", 200 libusb_error_name (rc)); 201 status = syserror (rc); 202 goto error_free; 203 } 204 205 *out = session; 206 207 return status; 208 209 error_free: 210 free (session); 211 error_unlock: 212 return status; 213 } 214 215 static dc_usb_session_t * 216 dc_usb_session_ref (dc_usb_session_t *session) 217 { 218 if (session == NULL) 219 return NULL; 220 221 session->refcount++; 222 223 return session; 224 } 225 226 static dc_status_t 227 dc_usb_session_unref (dc_usb_session_t *session) 228 { 229 if (session == NULL) 230 return DC_STATUS_SUCCESS; 231 232 if (--session->refcount == 0) { 233 libusb_exit (session->handle); 234 free (session); 235 } 236 237 return DC_STATUS_SUCCESS; 238 } 239 #endif 240 241 unsigned int 242 dc_usb_device_get_vid (dc_usb_device_t *device) 243 { 244 if (device == NULL) 245 return 0; 246 247 return device->vid; 248 } 249 250 unsigned int 251 dc_usb_device_get_pid (dc_usb_device_t *device) 252 { 253 if (device == NULL) 254 return 0; 255 256 return device->pid; 257 } 258 259 void 260 dc_usb_device_free(dc_usb_device_t *device) 261 { 262 if (device == NULL) 263 return; 264 265 #ifdef HAVE_LIBUSB 266 libusb_unref_device (device->handle); 267 dc_usb_session_unref (device->session); 268 #endif 269 free (device); 270 } 271 272 dc_status_t 273 dc_usb_iterator_new (dc_iterator_t **out, dc_context_t *context, dc_descriptor_t *descriptor) 274 { 275 #ifdef HAVE_LIBUSB 276 dc_status_t status = DC_STATUS_SUCCESS; 277 dc_usb_iterator_t *iterator = NULL; 278 279 if (out == NULL) 280 return DC_STATUS_INVALIDARGS; 281 282 iterator = (dc_usb_iterator_t *) dc_iterator_allocate (context, &dc_usb_iterator_vtable); 283 if (iterator == NULL) { 284 ERROR (context, "Failed to allocate memory."); 285 return DC_STATUS_NOMEMORY; 286 } 287 288 // Initialize the usb library. 289 status = dc_usb_session_new (&iterator->session, context); 290 if (status != DC_STATUS_SUCCESS) { 291 goto error_free; 292 } 293 294 // Enumerate the USB devices. 295 struct libusb_device **devices = NULL; 296 ssize_t ndevices = libusb_get_device_list (iterator->session->handle, &devices); 297 if (ndevices < 0) { 298 ERROR (context, "Failed to enumerate the usb devices (%s).", 299 libusb_error_name (ndevices)); 300 status = syserror (ndevices); 301 goto error_session_unref; 302 } 303 304 iterator->devices = devices; 305 iterator->count = ndevices; 306 iterator->current = 0; 307 iterator->descriptor = descriptor; 308 309 *out = (dc_iterator_t *) iterator; 310 311 return DC_STATUS_SUCCESS; 312 313 error_session_unref: 314 dc_usb_session_unref (iterator->session); 315 error_free: 316 dc_iterator_deallocate ((dc_iterator_t *) iterator); 317 return status; 318 #else 319 return DC_STATUS_UNSUPPORTED; 320 #endif 321 } 322 323 #ifdef HAVE_LIBUSB 324 static dc_status_t 325 dc_usb_iterator_next (dc_iterator_t *abstract, void *out) 326 { 327 dc_usb_iterator_t *iterator = (dc_usb_iterator_t *) abstract; 328 dc_usb_device_t *device = NULL; 329 330 while (iterator->current < iterator->count) { 331 struct libusb_device *current = iterator->devices[iterator->current++]; 332 333 // Get the device descriptor. 334 struct libusb_device_descriptor dev; 335 int rc = libusb_get_device_descriptor (current, &dev); 336 if (rc < 0) { 337 ERROR (abstract->context, "Failed to get the device descriptor (%s).", 338 libusb_error_name (rc)); 339 return syserror (rc); 340 } 341 342 dc_usb_desc_t usb = {dev.idVendor, dev.idProduct}; 343 if (!dc_descriptor_filter (iterator->descriptor, DC_TRANSPORT_USB, &usb)) { 344 continue; 345 } 346 347 // Check for known USB parameters. 348 const dc_usb_params_t *params = dc_usb_params_find (&usb); 349 350 // Get the active configuration descriptor. 351 struct libusb_config_descriptor *config = NULL; 352 rc = libusb_get_active_config_descriptor (current, &config); 353 if (rc != LIBUSB_SUCCESS) { 354 ERROR (abstract->context, "Failed to get the configuration descriptor (%s).", 355 libusb_error_name (rc)); 356 return syserror (rc); 357 } 358 359 // Find the first matching interface. 360 const struct libusb_interface_descriptor *interface = NULL; 361 for (unsigned int i = 0; i < config->bNumInterfaces; i++) { 362 const struct libusb_interface *iface = &config->interface[i]; 363 for (int j = 0; j < iface->num_altsetting; j++) { 364 const struct libusb_interface_descriptor *desc = &iface->altsetting[j]; 365 if (interface == NULL && 366 (params == NULL || params->interface == desc->bInterfaceNumber)) { 367 interface = desc; 368 } 369 } 370 } 371 372 if (interface == NULL) { 373 libusb_free_config_descriptor (config); 374 continue; 375 } 376 377 // Find the first matching input and output bulk endpoints. 378 const struct libusb_endpoint_descriptor *ep_in = NULL, *ep_out = NULL; 379 for (unsigned int i = 0; i < interface->bNumEndpoints; i++) { 380 const struct libusb_endpoint_descriptor *desc = &interface->endpoint[i]; 381 382 unsigned int type = desc->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK; 383 unsigned int direction = desc->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK; 384 385 if (type != LIBUSB_TRANSFER_TYPE_BULK) { 386 continue; 387 } 388 389 if (ep_in == NULL && direction == LIBUSB_ENDPOINT_IN && 390 (params == NULL || params->endpoint_in == desc->bEndpointAddress)) { 391 ep_in = desc; 392 } 393 394 if (ep_out == NULL && direction == LIBUSB_ENDPOINT_OUT && 395 (params == NULL || params->endpoint_out == desc->bEndpointAddress)) { 396 ep_out = desc; 397 } 398 } 399 400 if (ep_in == NULL || ep_out == NULL) { 401 libusb_free_config_descriptor (config); 402 continue; 403 } 404 405 device = (dc_usb_device_t *) malloc (sizeof(dc_usb_device_t)); 406 if (device == NULL) { 407 ERROR (abstract->context, "Failed to allocate memory."); 408 libusb_free_config_descriptor (config); 409 return DC_STATUS_NOMEMORY; 410 } 411 412 device->session = dc_usb_session_ref (iterator->session); 413 device->vid = dev.idVendor; 414 device->pid = dev.idProduct; 415 device->handle = libusb_ref_device (current); 416 device->interface = interface->bInterfaceNumber; 417 device->endpoint_in = ep_in->bEndpointAddress; 418 device->endpoint_out = ep_out->bEndpointAddress; 419 420 *(dc_usb_device_t **) out = device; 421 422 libusb_free_config_descriptor (config); 423 424 return DC_STATUS_SUCCESS; 425 } 426 427 return DC_STATUS_DONE; 428 } 429 430 static dc_status_t 431 dc_usb_iterator_free (dc_iterator_t *abstract) 432 { 433 dc_usb_iterator_t *iterator = (dc_usb_iterator_t *) abstract; 434 435 libusb_free_device_list (iterator->devices, 1); 436 dc_usb_session_unref (iterator->session); 437 438 return DC_STATUS_SUCCESS; 439 } 440 #endif 441 442 dc_status_t 443 dc_usb_open (dc_iostream_t **out, dc_context_t *context, dc_usb_device_t *device) 444 { 445 #ifdef HAVE_LIBUSB 446 dc_status_t status = DC_STATUS_SUCCESS; 447 dc_usb_t *usb = NULL; 448 449 if (out == NULL || device == NULL) 450 return DC_STATUS_INVALIDARGS; 451 452 INFO (context, "Open: vid=%04x, pid=%04x, interface=%u, endpoints=%02x,%02x", 453 device->vid, device->pid, device->interface, device->endpoint_in, device->endpoint_out); 454 455 // Allocate memory. 456 usb = (dc_usb_t *) dc_iostream_allocate (context, &dc_usb_vtable, DC_TRANSPORT_USB); 457 if (usb == NULL) { 458 ERROR (context, "Out of memory."); 459 return DC_STATUS_NOMEMORY; 460 } 461 462 // Initialize the usb library. 463 usb->session = dc_usb_session_ref (device->session); 464 if (usb->session == NULL) { 465 goto error_free; 466 } 467 468 // Open the USB device. 469 int rc = libusb_open (device->handle, &usb->handle); 470 if (rc != LIBUSB_SUCCESS) { 471 ERROR (context, "Failed to open the usb device (%s).", 472 libusb_error_name (rc)); 473 status = syserror (rc); 474 goto error_session_unref; 475 } 476 477 #if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000102) 478 libusb_set_auto_detach_kernel_driver (usb->handle, 1); 479 #endif 480 481 // Claim the interface. 482 rc = libusb_claim_interface (usb->handle, device->interface); 483 if (rc != LIBUSB_SUCCESS) { 484 ERROR (context, "Failed to claim the usb interface (%s).", 485 libusb_error_name (rc)); 486 status = syserror (rc); 487 goto error_usb_close; 488 } 489 490 usb->interface = device->interface; 491 usb->endpoint_in = device->endpoint_in; 492 usb->endpoint_out = device->endpoint_out; 493 usb->timeout = 0; 494 495 *out = (dc_iostream_t *) usb; 496 497 return DC_STATUS_SUCCESS; 498 499 error_usb_close: 500 libusb_close (usb->handle); 501 error_session_unref: 502 dc_usb_session_unref (usb->session); 503 error_free: 504 dc_iostream_deallocate ((dc_iostream_t *) usb); 505 return status; 506 #else 507 return DC_STATUS_UNSUPPORTED; 508 #endif 509 } 510 511 #ifdef HAVE_LIBUSB 512 static dc_status_t 513 dc_usb_close (dc_iostream_t *abstract) 514 { 515 dc_status_t status = DC_STATUS_SUCCESS; 516 dc_usb_t *usb = (dc_usb_t *) abstract; 517 518 libusb_release_interface (usb->handle, usb->interface); 519 libusb_close (usb->handle); 520 dc_usb_session_unref (usb->session); 521 522 return status; 523 } 524 525 static dc_status_t 526 dc_usb_set_timeout (dc_iostream_t *abstract, int timeout) 527 { 528 dc_usb_t *usb = (dc_usb_t *) abstract; 529 530 if (timeout < 0) { 531 usb->timeout = 0; 532 } else if (timeout == 0) { 533 return DC_STATUS_UNSUPPORTED; 534 } else { 535 usb->timeout = timeout; 536 } 537 538 return DC_STATUS_SUCCESS; 539 } 540 541 static dc_status_t 542 dc_usb_poll (dc_iostream_t *abstract, int timeout) 543 { 544 return DC_STATUS_UNSUPPORTED; 545 } 546 547 static dc_status_t 548 dc_usb_read (dc_iostream_t *abstract, void *data, size_t size, size_t *actual) 549 { 550 dc_status_t status = DC_STATUS_SUCCESS; 551 dc_usb_t *usb = (dc_usb_t *) abstract; 552 int nbytes = 0; 553 554 int rc = libusb_bulk_transfer (usb->handle, usb->endpoint_in, data, size, &nbytes, usb->timeout); 555 if (rc != LIBUSB_SUCCESS || nbytes < 0) { 556 ERROR (abstract->context, "Usb read bulk transfer failed (%s).", 557 libusb_error_name (rc)); 558 status = syserror (rc); 559 if (nbytes < 0) 560 nbytes = 0; 561 goto out; 562 } 563 564 out: 565 if (actual) 566 *actual = nbytes; 567 568 return status; 569 } 570 571 static dc_status_t 572 dc_usb_write (dc_iostream_t *abstract, const void *data, size_t size, size_t *actual) 573 { 574 dc_status_t status = DC_STATUS_SUCCESS; 575 dc_usb_t *usb = (dc_usb_t *) abstract; 576 int nbytes = 0; 577 578 int rc = libusb_bulk_transfer (usb->handle, usb->endpoint_out, (void *) data, size, &nbytes, 0); 579 if (rc != LIBUSB_SUCCESS || nbytes < 0) { 580 ERROR (abstract->context, "Usb write bulk transfer failed (%s).", 581 libusb_error_name (rc)); 582 status = syserror (rc); 583 if (nbytes < 0) 584 nbytes = 0; 585 goto out; 586 } 587 588 out: 589 if (actual) 590 *actual = nbytes; 591 592 return status; 593 } 594 595 static dc_status_t 596 dc_usb_ioctl_control (dc_iostream_t *abstract, void *data, size_t size) 597 { 598 dc_usb_t *usb = (dc_usb_t *) abstract; 599 const dc_usb_control_t *control = (const dc_usb_control_t *) data; 600 601 if (size < sizeof(control) || control->wLength > size - sizeof(control)) { 602 return DC_STATUS_INVALIDARGS; 603 } 604 605 int rc = libusb_control_transfer (usb->handle, 606 control->bmRequestType, control->bRequest, control->wValue, control->wIndex, 607 (unsigned char *) data + sizeof(control), control->wLength, usb->timeout); 608 if (rc != LIBUSB_SUCCESS) { 609 ERROR (abstract->context, "Usb control transfer failed (%s).", 610 libusb_error_name (rc)); 611 return syserror (rc); 612 } 613 614 return DC_STATUS_SUCCESS; 615 } 616 617 static dc_status_t 618 dc_usb_ioctl (dc_iostream_t *abstract, unsigned int request, void *data, size_t size) 619 { 620 switch (request) { 621 case DC_IOCTL_USB_CONTROL_READ: 622 case DC_IOCTL_USB_CONTROL_WRITE: 623 return dc_usb_ioctl_control (abstract, data, size); 624 default: 625 return DC_STATUS_UNSUPPORTED; 626 } 627 } 628 #endif