| 1 | /* Hey EMACS -*- linux-c -*- */ |
| 2 | /* $Id$ */ |
| 3 | |
| 4 | /* libticables2 - link cable library, a part of the TiLP project |
| 5 | * Copyright (c) 1999-2006 Romain Lievin |
| 6 | * Copyright (c) 2001 Julien Blache (original author) |
| 7 | * Copyright (c) 2007 Romain Lievin (libusb-win32 support) |
| 8 | * Copyright (c) 2007, 2011 Kevin Kofler (slv_check support) |
| 9 | * Copyright (c) 2011 Jon Sturm (libusb-1.0 support) |
| 10 | * Copyright (c) 2011 Lionel Debroux (style fixes, corner case fixes) |
| 11 | * |
| 12 | * Portions lifted from libusb (LGPL): |
| 13 | * Copyright (C) 2007-2008 Daniel Drake <[email protected]> |
| 14 | * Copyright (C) 2001 Johannes Erdfelt <[email protected]> |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2 of the License, or |
| 19 | * (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA. |
| 29 | */ |
| 30 | |
| 31 | /* TI-GRAPH LINK USB and direct USB cable support (libusb 1.0.x) */ |
| 32 | |
| 33 | #ifdef HAVE_CONFIG_H |
| 34 | #include <config.h> |
| 35 | #endif |
| 36 | |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <fcntl.h> |
| 41 | #include <errno.h> |
| 42 | #if defined(__BSD__) || defined(__MACOSX__) |
| 43 | #include <libusb.h> |
| 44 | #else |
| 45 | #include <libusb-1.0/libusb.h> |
| 46 | #endif |
| 47 | |
| 48 | #ifdef __WIN32__ |
| 49 | #ifndef _WINSOCKAPI_ |
| 50 | #include <winsock2.h> /* struct timeval */ |
| 51 | #endif |
| 52 | #else |
| 53 | #include <unistd.h> |
| 54 | #include <sys/time.h> |
| 55 | #include <sys/types.h> |
| 56 | #include <sys/stat.h> |
| 57 | #include <sys/ioctl.h> |
| 58 | #endif |
| 59 | |
| 60 | |
| 61 | #include "../ticables.h" |
| 62 | #include "../logging.h" |
| 63 | #include "../error.h" |
| 64 | #include "../gettext.h" |
| 65 | #include "../internal.h" |
| 66 | #if defined(__WIN32__) |
| 67 | #include "../win32/detect.h" |
| 68 | #elif defined(__MACOSX__) |
| 69 | #include "../macos/detect.h" |
| 70 | #elif defined(__BSD__) |
| 71 | #include "../bsd/detect.h" |
| 72 | #else |
| 73 | #include "detect.h" |
| 74 | #endif |
| 75 | #include "../timeout.h" |
| 76 | |
| 77 | /* Constants */ |
| 78 | |
| 79 | #define MAX_CABLES 4 |
| 80 | |
| 81 | #define VID_TI 0x0451 /* Texas Instruments, Inc. */ |
| 82 | |
| 83 | #define to (100 * h->timeout) // in ms |
| 84 | |
| 85 | /* Types */ |
| 86 | |
| 87 | // device infos |
| 88 | typedef struct |
| 89 | { |
| 90 | uint16_t vid; |
| 91 | uint16_t pid; |
| 92 | const char* str; |
| 93 | |
| 94 | struct libusb_device *dev; |
| 95 | } usb_infos; |
| 96 | |
| 97 | // list of known devices |
| 98 | static usb_infos tigl_infos[] = |
| 99 | { |
| 100 | {VID_TI, PID_TIGLUSB, "TI-GRAPH LINK USB", NULL}, |
| 101 | {VID_TI, PID_TI84P, "TI-84 Plus Hand-Held", NULL}, |
| 102 | {VID_TI, PID_TI89TM, "TI-89 Titanium Hand-Held", NULL}, |
| 103 | {VID_TI, PID_TI84P_SE, "TI-84 Plus Silver Hand-Held", NULL}, |
| 104 | {VID_TI, PID_NSPIRE, "TI-Nspire Hand-Held", NULL}, |
| 105 | {0, 0, NULL, NULL} |
| 106 | }; |
| 107 | |
| 108 | // list of devices found |
| 109 | static USBCableInfo tigl_devices[MAX_CABLES+1]; |
| 110 | static int tigl_n_devices; |
| 111 | |
| 112 | // internal structure for holding data |
| 113 | typedef struct |
| 114 | { |
| 115 | struct libusb_device *device; |
| 116 | struct libusb_device_handle *handle; |
| 117 | |
| 118 | USBCableInfo cable_info; |
| 119 | int nBytesRead; |
| 120 | uint8_t rBuf[64]; |
| 121 | uint8_t* rBufPtr; |
| 122 | int in_endpoint; |
| 123 | int out_endpoint; |
| 124 | int max_ps; |
| 125 | int was_max_ps; |
| 126 | } usb_struct; |
| 127 | |
| 128 | // variables for slv_check and slv_bulk_read |
| 129 | static int io_pending = 0; |
| 130 | static struct libusb_transfer *transfer; |
| 131 | static int completed = 0; |
| 132 | |
| 133 | // convenient macros |
| 134 | #define uDev (((usb_struct *)(h->priv2))->device) |
| 135 | #define uHdl (((usb_struct *)(h->priv2))->handle) |
| 136 | #define cable_info (((usb_struct *)(h->priv2))->cable_info) |
| 137 | #define max_ps (((usb_struct *)(h->priv2))->max_ps) |
| 138 | #define was_max_ps (((usb_struct *)(h->priv2))->was_max_ps) |
| 139 | #define nBytesRead (((usb_struct *)(h->priv2))->nBytesRead) |
| 140 | #define rBuf (((usb_struct *)(h->priv2))->rBuf) |
| 141 | #define rBufPtr (((usb_struct *)(h->priv2))->rBufPtr) |
| 142 | #define uInEnd (((usb_struct *)(h->priv2))->in_endpoint) |
| 143 | #define uOutEnd (((usb_struct *)(h->priv2))->out_endpoint) |
| 144 | |
| 145 | #if !HAVE_LIBUSB10_STRERROR |
| 146 | #error Please use a version of libusb 1.0 which provides libusb_strerror() (>= 1.0.16). |
| 147 | #endif |
| 148 | |
| 149 | static void tigl_get_product(char * string, size_t maxlen, struct libusb_device *dev) |
| 150 | { |
| 151 | libusb_device_handle *han; |
| 152 | libusb_error ret; |
| 153 | struct libusb_device_descriptor desc; |
| 154 | int r = libusb_get_device_descriptor(dev, &desc); |
| 155 | |
| 156 | string[0] = 0; |
| 157 | |
| 158 | if (r < 0) |
| 159 | { |
| 160 | ticables_critical("failed to get device descriptor"); |
| 161 | } |
| 162 | |
| 163 | if (desc.iProduct) |
| 164 | { |
| 165 | if (!libusb_open(dev, &han)) |
| 166 | { |
| 167 | ret = (libusb_error)libusb_get_string_descriptor_ascii(han, desc.iProduct, (unsigned char *) string, maxlen); |
| 168 | libusb_close(han); |
| 169 | if (ret <= 0) |
| 170 | { |
| 171 | ticables_warning("libusb_get_string_descriptor_ascii (%s).\n", libusb_strerror(ret)); |
| 172 | } |
| 173 | } |
| 174 | // else do nothing. |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static int tigl_find(void) |
| 179 | { |
| 180 | // discover devices |
| 181 | libusb_device **list; |
| 182 | ssize_t cnt = libusb_get_device_list(NULL, &list); |
| 183 | ssize_t i = 0; |
| 184 | int j = 0; |
| 185 | int k; |
| 186 | |
| 187 | if (cnt <= 0) |
| 188 | { |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | memset(tigl_devices, 0, sizeof(tigl_devices)); |
| 193 | tigl_n_devices = 0; |
| 194 | |
| 195 | for (i = 0; i < cnt; i++) |
| 196 | { |
| 197 | libusb_device *device = list[i]; |
| 198 | struct libusb_device_descriptor desc; |
| 199 | int r = libusb_get_device_descriptor(device, &desc); |
| 200 | if (r < 0) |
| 201 | { |
| 202 | fprintf(stderr, "failed to get device descriptor"); |
| 203 | return r; |
| 204 | } |
| 205 | if (desc.idVendor == VID_TI) |
| 206 | { |
| 207 | for(k = 0; k < (int)(sizeof(tigl_infos) / sizeof(tigl_infos[0])); k++) |
| 208 | { |
| 209 | if (desc.idProduct == tigl_infos[k].pid) |
| 210 | { |
| 211 | tigl_devices[j].vid = desc.idVendor; |
| 212 | tigl_devices[j].pid = desc.idProduct; |
| 213 | tigl_devices[j].version = desc.bcdDevice; |
| 214 | tigl_get_product(tigl_devices[j].product_str, sizeof(tigl_devices[j].product_str), device); |
| 215 | ticables_info(_(" found %s on #%i, version <%x.%02x>\n"), |
| 216 | tigl_devices[j].product_str, j+1, |
| 217 | desc.bcdDevice >> 8, |
| 218 | desc.bcdDevice & 0xff); |
| 219 | |
| 220 | tigl_devices[j++].dev = device; |
| 221 | tigl_n_devices = j; |
| 222 | |
| 223 | if (j >= MAX_CABLES) |
| 224 | { |
| 225 | return j; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | return j; |
| 232 | } |
| 233 | |
| 234 | static int tigl_enum(void) |
| 235 | { |
| 236 | int ret = 0; |
| 237 | |
| 238 | /* find all TI products on all ports */ |
| 239 | ret = tigl_find(); |
| 240 | if (ret == 0) |
| 241 | { |
| 242 | ticables_warning("%s", _("no devices found!\n")); |
| 243 | return ERR_LIBUSB_OPEN; |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int tigl_open(int id, libusb_device_handle ** udh) |
| 250 | { |
| 251 | int ret; |
| 252 | |
| 253 | tigl_enum(); |
| 254 | |
| 255 | if (tigl_devices[id].dev == NULL) |
| 256 | { |
| 257 | return ERR_LIBUSB_OPEN; |
| 258 | } |
| 259 | |
| 260 | if (!libusb_open((libusb_device *)(tigl_devices[id].dev), udh)) |
| 261 | { |
| 262 | /* only one configuration: #1 */ |
| 263 | ret = libusb_set_configuration(*udh, 1); |
| 264 | if (ret) |
| 265 | { |
| 266 | ticables_warning("libusb_set_configuration (%s).\n", libusb_strerror((libusb_error)ret)); |
| 267 | } |
| 268 | |
| 269 | /* configuration #1, interface #0 */ |
| 270 | ret = libusb_claim_interface(*udh, 0); |
| 271 | if (ret) |
| 272 | { |
| 273 | ticables_warning("libusb_claim_interface (%s).\n", libusb_strerror((libusb_error)ret)); |
| 274 | return ERR_LIBUSB_CLAIM; |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | return ERR_LIBUSB_OPEN; |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int tigl_close(libusb_device_handle **udh) |
| 288 | { |
| 289 | // cancel any pending transfers to prevent a segfault in libusb |
| 290 | if (io_pending) |
| 291 | { |
| 292 | io_pending = FALSE; |
| 293 | if (!completed) |
| 294 | { |
| 295 | libusb_cancel_transfer(transfer); |
| 296 | while (!completed) |
| 297 | { |
| 298 | if (libusb_handle_events(NULL) < 0) |
| 299 | { |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | libusb_free_transfer(transfer); |
| 305 | } |
| 306 | |
| 307 | // NOTE: slv_close() has already checked for *udh != NULL . |
| 308 | libusb_release_interface(*udh, 0); |
| 309 | libusb_close(*udh); |
| 310 | *udh = NULL; |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static int tigl_reset(CableHandle *h) |
| 316 | { |
| 317 | // Reset out pipe |
| 318 | if (NULL != uHdl) |
| 319 | { |
| 320 | int ret = libusb_clear_halt(uHdl, uOutEnd); |
| 321 | if (ret) |
| 322 | { |
| 323 | ticables_warning("libusb_clear_halt (%s).\n", libusb_strerror((libusb_error)ret)); |
| 324 | } |
| 325 | |
| 326 | // Reset in pipe |
| 327 | ret = libusb_clear_halt(uHdl, uInEnd); |
| 328 | if (ret) |
| 329 | { |
| 330 | ticables_warning("libusb_clear_halt (%s).\n", libusb_strerror((libusb_error)ret)); |
| 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | return ERR_LIBUSB_RESET; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /* API */ |
| 342 | |
| 343 | static int slv_prepare(CableHandle *h) |
| 344 | { |
| 345 | int ret; |
| 346 | char str[64]; |
| 347 | |
| 348 | #if defined(__WIN32__) |
| 349 | ret = win32_check_libusb(); |
| 350 | #elif defined(__MACOSX__) |
| 351 | ret = macosx_check_libusb(); |
| 352 | #elif defined(__BSD__) |
| 353 | ret = bsd_check_libusb(); |
| 354 | #else |
| 355 | ret = linux_check_libusb(); |
| 356 | #endif |
| 357 | if (!ret) |
| 358 | { |
| 359 | if (h->port >= MAX_CABLES) |
| 360 | { |
| 361 | return ERR_ILLEGAL_ARG; |
| 362 | } |
| 363 | |
| 364 | h->address = h->port-1; |
| 365 | sprintf(str, "TiglUsb #%i", h->port); |
| 366 | h->device = strdup(str); |
| 367 | h->priv2 = (usb_struct *)calloc(1, sizeof(usb_struct)); |
| 368 | } |
| 369 | |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | static int slv_open(CableHandle *h) |
| 374 | { |
| 375 | int ret; |
| 376 | int i; |
| 377 | struct libusb_config_descriptor *config; |
| 378 | const struct libusb_interface *interface_; |
| 379 | const struct libusb_interface_descriptor *interface; |
| 380 | const struct libusb_endpoint_descriptor *endpoint; |
| 381 | |
| 382 | // open device |
| 383 | ret = tigl_open(h->address, &uHdl); |
| 384 | if (ret) |
| 385 | { |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | cable_info = tigl_devices[h->address]; |
| 390 | uDev = (libusb_device *)(tigl_devices[h->address].dev); |
| 391 | uInEnd = 0x81; |
| 392 | uOutEnd = 0x02; |
| 393 | |
| 394 | // get max packet size |
| 395 | libusb_get_active_config_descriptor(uDev, &config); |
| 396 | interface_ = &(config->interface[0]); |
| 397 | interface = &(interface_->altsetting[0]); |
| 398 | endpoint = &(interface->endpoint[0]); |
| 399 | max_ps = endpoint->wMaxPacketSize; |
| 400 | |
| 401 | // Enumerate endpoints. |
| 402 | for (i = 0; i < interface->bNumEndpoints; i++) |
| 403 | { |
| 404 | endpoint = &(interface->endpoint[i]); |
| 405 | if ((endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_BULK) == LIBUSB_TRANSFER_TYPE_BULK) |
| 406 | { |
| 407 | if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) |
| 408 | { |
| 409 | if (endpoint->bEndpointAddress != 0x83) // Some Nspire OS use that seemingly bogus endpoint. |
| 410 | { |
| 411 | uInEnd = endpoint->bEndpointAddress; |
| 412 | ticables_info("found bulk in endpoint 0x%02X\n", uInEnd); |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | ticables_info("XXX: swallowing bulk in endpoint 0x83, advertised by Nspire (CAS and non-CAS) 1.x but seemingly not working\n"); |
| 417 | } |
| 418 | } |
| 419 | else |
| 420 | { |
| 421 | uOutEnd = endpoint->bEndpointAddress; |
| 422 | ticables_info("found bulk out endpoint 0x%02X\n", uOutEnd); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | nBytesRead = 0; |
| 427 | was_max_ps = 0; |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static int slv_close(CableHandle *h) |
| 433 | { |
| 434 | if (uHdl != NULL) |
| 435 | { |
| 436 | tigl_close(&uHdl); |
| 437 | } |
| 438 | |
| 439 | uDev = NULL; |
| 440 | |
| 441 | free(h->priv2); |
| 442 | h->priv2 = NULL; |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static int slv_get_device_info(CableHandle *h, CableDeviceInfo *info) |
| 448 | { |
| 449 | translate_usb_device_info(info, &cable_info); |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static int slv_reset(CableHandle *h) |
| 454 | { |
| 455 | int ret; |
| 456 | |
| 457 | /* Reset both endpoints (send an URB_FUNCTION_RESET_PIPE) */ |
| 458 | ret = tigl_reset(h); |
| 459 | if (!ret) |
| 460 | { |
| 461 | /* Reset USB port (send an IOCTL_INTERNAL_USB_RESET_PORT) */ |
| 462 | /* NOTE: tigl_reset() has already checked for uHdl != NULL */ |
| 463 | ret = libusb_reset_device(uHdl); |
| 464 | if (ret != 0) |
| 465 | { |
| 466 | ticables_warning("libusb_device_reset (%s).\n", libusb_strerror((libusb_error)ret)); |
| 467 | /* On Mac OS X, reenumeration isn't automatic, so let's not return here. */ |
| 468 | #ifndef __MACOSX__ |
| 469 | ret = ERR_LIBUSB_RESET; |
| 470 | #else |
| 471 | ret = 0; |
| 472 | #endif |
| 473 | } |
| 474 | |
| 475 | if (!ret) |
| 476 | { |
| 477 | // lib-usb doc: after calling usb_reset, the device will need to re-enumerate |
| 478 | // and therefore, requires you to find the new device and open a new handle. |
| 479 | // The handle used to call usb_reset will no longer work. |
| 480 | #ifdef __WIN32__ |
| 481 | Sleep(500); |
| 482 | #else |
| 483 | usleep(500000); |
| 484 | #endif |
| 485 | ret = slv_close(h); |
| 486 | if (!ret) |
| 487 | { |
| 488 | h->priv2 = (usb_struct *)calloc(1, sizeof(usb_struct)); |
| 489 | ret = slv_open(h); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return ret; |
| 495 | } |
| 496 | |
| 497 | // convenient function which send one or more bytes |
| 498 | static int send_block(CableHandle *h, uint8_t *data, int length) |
| 499 | { |
| 500 | int ret, tmp; |
| 501 | |
| 502 | if (NULL == uHdl) |
| 503 | { |
| 504 | return ERR_WRITE_ERROR; |
| 505 | } |
| 506 | |
| 507 | ret = libusb_bulk_transfer(uHdl, uOutEnd, (unsigned char*)data, length, &tmp, to); |
| 508 | |
| 509 | if (ret == LIBUSB_ERROR_TIMEOUT) |
| 510 | { |
| 511 | ticables_warning("libusb_bulk_transfer (%s).\n", libusb_strerror((libusb_error)ret)); |
| 512 | return ERR_WRITE_TIMEOUT; |
| 513 | } |
| 514 | else if (ret < 0) |
| 515 | { |
| 516 | ticables_warning("libusb_bulk_transfer (%s).\n", libusb_strerror((libusb_error)ret)); |
| 517 | return ERR_WRITE_ERROR; |
| 518 | } |
| 519 | |
| 520 | if (tigl_devices[h->address].pid == PID_NSPIRE && length % max_ps == 0) |
| 521 | { |
| 522 | ticables_info("XXX triggering an extra bulk write"); |
| 523 | ret = libusb_bulk_transfer(uHdl, uOutEnd, (unsigned char*)data, 0, &tmp, to); |
| 524 | |
| 525 | if (ret == LIBUSB_ERROR_TIMEOUT) |
| 526 | { |
| 527 | ticables_warning("libusb_bulk_transfer (%s).\n", libusb_strerror((libusb_error)ret)); |
| 528 | return ERR_WRITE_TIMEOUT; |
| 529 | } |
| 530 | else if (ret < 0) |
| 531 | { |
| 532 | ticables_warning("libusb_bulk_transfer (%s).\n", libusb_strerror((libusb_error)ret)); |
| 533 | return ERR_WRITE_ERROR; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static int slv_put(CableHandle* h, uint8_t *data, uint32_t len) |
| 541 | { |
| 542 | return send_block(h, data, len); |
| 543 | } |
| 544 | |
| 545 | static void LIBUSB_CALL bulk_transfer_cb(struct libusb_transfer *transfer2) |
| 546 | { |
| 547 | // This comes from libusb. |
| 548 | int *completed2 = (int *)(transfer2->user_data); |
| 549 | *completed2 = 1; |
| 550 | /* caller interprets results and frees transfer */ |
| 551 | } |
| 552 | |
| 553 | static int slv_bulk_read(struct libusb_device_handle *dev_handle, |
| 554 | unsigned char endpoint, unsigned char *buffer, int length, |
| 555 | int *transferred, unsigned int timeout) |
| 556 | { |
| 557 | // This is a variant of libusb_bulk_transfer in libusb, edited to take |
| 558 | // the io_pending variable set in slv_check into account. |
| 559 | int r; |
| 560 | |
| 561 | if (io_pending) |
| 562 | { |
| 563 | io_pending = FALSE; |
| 564 | } |
| 565 | else |
| 566 | { |
| 567 | completed = 0; |
| 568 | transfer = libusb_alloc_transfer(0); |
| 569 | if (!transfer) |
| 570 | { |
| 571 | return LIBUSB_ERROR_NO_MEM; |
| 572 | } |
| 573 | |
| 574 | libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, |
| 575 | buffer, length, bulk_transfer_cb, |
| 576 | &completed, timeout); |
| 577 | |
| 578 | r = libusb_submit_transfer(transfer); |
| 579 | if (r < 0) |
| 580 | { |
| 581 | libusb_free_transfer(transfer); |
| 582 | return r; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | while (!completed) |
| 587 | { |
| 588 | r = libusb_handle_events(NULL); |
| 589 | if (r < 0) |
| 590 | { |
| 591 | if (r == LIBUSB_ERROR_INTERRUPTED) |
| 592 | { |
| 593 | continue; |
| 594 | } |
| 595 | libusb_cancel_transfer(transfer); |
| 596 | while (!completed) |
| 597 | { |
| 598 | if (libusb_handle_events(NULL) < 0) |
| 599 | { |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | libusb_free_transfer(transfer); |
| 604 | return r; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | *transferred = transfer->actual_length; |
| 609 | switch (transfer->status) |
| 610 | { |
| 611 | case LIBUSB_TRANSFER_COMPLETED: |
| 612 | r = 0; |
| 613 | break; |
| 614 | case LIBUSB_TRANSFER_TIMED_OUT: |
| 615 | r = LIBUSB_ERROR_TIMEOUT; |
| 616 | break; |
| 617 | case LIBUSB_TRANSFER_STALL: |
| 618 | r = LIBUSB_ERROR_PIPE; |
| 619 | break; |
| 620 | case LIBUSB_TRANSFER_OVERFLOW: |
| 621 | r = LIBUSB_ERROR_OVERFLOW; |
| 622 | break; |
| 623 | case LIBUSB_TRANSFER_NO_DEVICE: |
| 624 | r = LIBUSB_ERROR_NO_DEVICE; |
| 625 | break; |
| 626 | default: |
| 627 | ticables_warning("slv_bulk_read: unrecognized status code %d", transfer->status); |
| 628 | r = LIBUSB_ERROR_OTHER; |
| 629 | } |
| 630 | |
| 631 | libusb_free_transfer(transfer); |
| 632 | return r; |
| 633 | } |
| 634 | |
| 635 | static int slv_get_(CableHandle *h, uint8_t *data) |
| 636 | { |
| 637 | int ret = 0; |
| 638 | int len = 0; |
| 639 | tiTIME clk; |
| 640 | |
| 641 | /* Read up to 32/64 bytes and store them in a buffer for subsequent accesses */ |
| 642 | if (nBytesRead <= 0) |
| 643 | { |
| 644 | TO_START(clk); |
| 645 | do |
| 646 | { |
| 647 | // NOTE: slv_get() has already checked for uHdl != NULL . |
| 648 | ret = slv_bulk_read(uHdl, uInEnd, (unsigned char*)rBuf, max_ps, &len, to); |
| 649 | } |
| 650 | while(!len && !ret); |
| 651 | |
| 652 | if (len == max_ps) |
| 653 | { |
| 654 | was_max_ps = 1; |
| 655 | } |
| 656 | else |
| 657 | { |
| 658 | was_max_ps = 0; |
| 659 | } |
| 660 | |
| 661 | if (ret == LIBUSB_ERROR_TIMEOUT) |
| 662 | { |
| 663 | ticables_warning("slv_bulk_read (%s).\n", libusb_strerror((libusb_error)ret)); |
| 664 | nBytesRead = 0; |
| 665 | return ERR_READ_TIMEOUT; |
| 666 | } |
| 667 | else if (ret != 0) |
| 668 | { |
| 669 | ticables_warning("slv_bulk_read (%s).\n", libusb_strerror((libusb_error)ret)); |
| 670 | nBytesRead = 0; |
| 671 | return ERR_READ_ERROR; |
| 672 | } |
| 673 | |
| 674 | nBytesRead = len; |
| 675 | rBufPtr = rBuf; |
| 676 | } |
| 677 | |
| 678 | *data = *rBufPtr++; |
| 679 | nBytesRead--; |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | static int slv_get(CableHandle* h, uint8_t *data, uint32_t len) |
| 685 | { |
| 686 | int i=0; |
| 687 | int ret = 0; |
| 688 | int tmp; |
| 689 | |
| 690 | if (NULL == uHdl) |
| 691 | { |
| 692 | return ERR_READ_ERROR; |
| 693 | } |
| 694 | |
| 695 | /* we can't do that in any other way because slv_get_ can returns |
| 696 | * 1, 2, ..., len bytes. |
| 697 | * |
| 698 | * XXX But we know how much was actually recived can't we just try |
| 699 | * again if its less than we expected rather than this mess, whatever |
| 700 | * the point of it was? |
| 701 | */ |
| 702 | for(i = 0; i < (int)len; i++) |
| 703 | { |
| 704 | ret = slv_get_(h, data+i); |
| 705 | if (ret != 0) |
| 706 | { |
| 707 | break; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | if (!ret && was_max_ps != 0 && nBytesRead == 0) |
| 712 | { |
| 713 | if ( tigl_devices[h->address].pid == PID_NSPIRE |
| 714 | || len == 0 && ( tigl_devices[h->address].pid == PID_TI89TM |
| 715 | || tigl_devices[h->address].pid == PID_TI84P |
| 716 | || tigl_devices[h->address].pid == PID_TI84P_SE |
| 717 | ) |
| 718 | ) |
| 719 | { |
| 720 | ticables_info("XXX triggering an extra bulk read"); |
| 721 | ret = slv_bulk_read(uHdl, uInEnd, (unsigned char*)data, max_ps, &tmp, to); |
| 722 | |
| 723 | if (ret == LIBUSB_ERROR_TIMEOUT) |
| 724 | { |
| 725 | ticables_warning("slv_bulk_read (%s).\n", libusb_strerror((libusb_error)ret)); |
| 726 | nBytesRead = 0; |
| 727 | return ERR_READ_TIMEOUT; |
| 728 | } |
| 729 | else if (ret != 0) |
| 730 | { |
| 731 | ticables_warning("slv_bulk_read (%s).\n", libusb_strerror((libusb_error)ret)); |
| 732 | nBytesRead = 0; |
| 733 | return ERR_READ_ERROR; |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | return ret; |
| 739 | } |
| 740 | |
| 741 | static int slv_probe(CableHandle *h) |
| 742 | { |
| 743 | int ret; |
| 744 | int i; |
| 745 | |
| 746 | ret = tigl_enum(); |
| 747 | if (ret) |
| 748 | { |
| 749 | return ret; |
| 750 | } |
| 751 | |
| 752 | for (i = 0; i < MAX_CABLES; i++) |
| 753 | { |
| 754 | if (tigl_devices[h->address].pid == PID_TIGLUSB) |
| 755 | { |
| 756 | return 0; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | return ERR_PROBE_FAILED; |
| 761 | } |
| 762 | |
| 763 | static int raw_probe(CableHandle *h) |
| 764 | { |
| 765 | int ret; |
| 766 | int i; |
| 767 | |
| 768 | ret = tigl_enum(); |
| 769 | if (ret) |
| 770 | { |
| 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | for(i = 0; i < MAX_CABLES; i++) |
| 775 | { |
| 776 | if (tigl_devices[h->address].pid == PID_TI89TM || |
| 777 | tigl_devices[h->address].pid == PID_TI84P || |
| 778 | tigl_devices[h->address].pid == PID_TI84P_SE || |
| 779 | tigl_devices[h->address].pid == PID_NSPIRE) |
| 780 | { |
| 781 | return 0; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | return ERR_PROBE_FAILED; |
| 786 | } |
| 787 | |
| 788 | static int slv_check(CableHandle *h, int *status) |
| 789 | { |
| 790 | // This really should be in libusb, but alas it isn't, so their code was |
| 791 | // adapted by Kevin Kofler for use here. It's required to get TiEmu 3 to |
| 792 | // work with the SilverLink. |
| 793 | |
| 794 | int r; |
| 795 | struct timeval tv; |
| 796 | |
| 797 | if (nBytesRead > 0) |
| 798 | { |
| 799 | *status = TRUE; |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | if (NULL == uHdl) |
| 804 | { |
| 805 | return ERR_READ_ERROR; |
| 806 | } |
| 807 | |
| 808 | if (!io_pending) |
| 809 | { |
| 810 | completed = 0; |
| 811 | transfer = libusb_alloc_transfer(0); |
| 812 | if (!transfer) |
| 813 | { |
| 814 | return ERR_READ_ERROR; |
| 815 | } |
| 816 | |
| 817 | libusb_fill_bulk_transfer(transfer, uHdl, uInEnd, rBuf, |
| 818 | max_ps, bulk_transfer_cb, |
| 819 | &completed, to); |
| 820 | transfer->type = LIBUSB_TRANSFER_TYPE_BULK; |
| 821 | |
| 822 | r = libusb_submit_transfer(transfer); |
| 823 | if (r < 0) |
| 824 | { |
| 825 | libusb_free_transfer(transfer); |
| 826 | return ERR_READ_ERROR; |
| 827 | } |
| 828 | |
| 829 | io_pending = TRUE; |
| 830 | } |
| 831 | |
| 832 | tv.tv_sec = 0; |
| 833 | tv.tv_usec = 0; |
| 834 | r = libusb_handle_events_timeout(NULL, &tv); |
| 835 | if (r < 0) |
| 836 | { |
| 837 | if (r == LIBUSB_ERROR_INTERRUPTED) |
| 838 | { |
| 839 | return 0; |
| 840 | } |
| 841 | libusb_cancel_transfer(transfer); |
| 842 | while (!completed) |
| 843 | { |
| 844 | if (libusb_handle_events(NULL) < 0) |
| 845 | { |
| 846 | break; |
| 847 | } |
| 848 | } |
| 849 | libusb_free_transfer(transfer); |
| 850 | io_pending = FALSE; |
| 851 | return ERR_READ_ERROR; |
| 852 | } |
| 853 | |
| 854 | if (completed && transfer->status != LIBUSB_TRANSFER_COMPLETED |
| 855 | && transfer->status != LIBUSB_TRANSFER_TIMED_OUT) |
| 856 | { |
| 857 | libusb_free_transfer(transfer); |
| 858 | io_pending = FALSE; |
| 859 | return ERR_READ_ERROR; |
| 860 | } |
| 861 | |
| 862 | if (transfer->actual_length > 0) |
| 863 | { |
| 864 | nBytesRead = transfer->actual_length; |
| 865 | rBufPtr = rBuf; |
| 866 | *status = STATUS_RX; // data available |
| 867 | } |
| 868 | if (completed) |
| 869 | { |
| 870 | io_pending = FALSE; |
| 871 | libusb_free_transfer(transfer); |
| 872 | } |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | extern const CableFncts cable_slv = |
| 877 | { |
| 878 | CABLE_SLV, |
| 879 | "SLV", |
| 880 | N_("SilverLink"), |
| 881 | N_("SilverLink (TI-GRAPH LINK USB) cable"), |
| 882 | 0, |
| 883 | &slv_prepare, |
| 884 | &slv_open, &slv_close, &slv_reset, &slv_probe, NULL, |
| 885 | &slv_put, &slv_get, &slv_check, |
| 886 | &noop_set_red_wire, &noop_set_white_wire, |
| 887 | &noop_get_red_wire, &noop_get_white_wire, |
| 888 | NULL, NULL, |
| 889 | &noop_set_device, |
| 890 | &slv_get_device_info |
| 891 | }; |
| 892 | |
| 893 | extern const CableFncts cable_raw = |
| 894 | { |
| 895 | CABLE_USB, |
| 896 | "USB", |
| 897 | N_("DirectLink"), |
| 898 | N_("DirectLink (DIRECT USB) cable"), |
| 899 | 0, |
| 900 | &slv_prepare, |
| 901 | &slv_open, &slv_close, &slv_reset, &raw_probe, NULL, |
| 902 | &slv_put, &slv_get, &slv_check, |
| 903 | &noop_set_red_wire, &noop_set_white_wire, |
| 904 | &noop_get_red_wire, &noop_get_white_wire, |
| 905 | NULL, NULL, |
| 906 | &noop_set_device, |
| 907 | &slv_get_device_info |
| 908 | }; |
| 909 | |
| 910 | //======================= |
| 911 | |
| 912 | // returns list of detected devices |
| 913 | int usb_probe_device_info(const USBCableInfo **list, int *count) |
| 914 | { |
| 915 | int ret; |
| 916 | if (!(ret = tigl_enum())) |
| 917 | { |
| 918 | *list = tigl_devices; |
| 919 | *count = tigl_n_devices; |
| 920 | } |
| 921 | else |
| 922 | { |
| 923 | *list = NULL; |
| 924 | *count = 0; |
| 925 | } |
| 926 | return ret; |
| 927 | } |