1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
|
/* $FreeBSD$ */
/*-
* Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
* Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef LIBUSB_GLOBAL_INCLUDE_FILE
#include LIBUSB_GLOBAL_INCLUDE_FILE
#else
#include <assert.h>
#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <sys/endian.h>
#endif
/* Set the clock selected for the conditon variable attribute ATTR. */
extern int pthread_condattr_setclock (pthread_condattr_t *__attr,
__clockid_t __clock_id)
__THROW __nonnull ((1));
#define libusb_device_handle libusb20_device
#include "libusb20.h"
#include "libusb20_desc.h"
#include "libusb20_int.h"
#include "libusb.h"
#include "libusb10.h"
static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
struct libusb_context *usbi_default_context = NULL;
/* Prototypes */
static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
static int libusb10_convert_error(uint8_t status);
static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
static void libusb10_isoc_proxy(struct libusb20_transfer *);
static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
static void libusb10_ctrl_proxy(struct libusb20_transfer *);
static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
/* Library initialisation / deinitialisation */
void
libusb_set_debug(libusb_context *ctx, int level)
{
ctx = GET_CONTEXT(ctx);
if (ctx)
ctx->debug = level;
}
static void
libusb_set_nonblocking(int f)
{
int flags;
/*
* We ignore any failures in this function, hence the
* non-blocking flag is not critical to the operation of
* libUSB. We use F_GETFL and F_SETFL to be compatible with
* Linux.
*/
flags = fcntl(f, F_GETFL, NULL);
if (flags == -1)
return;
flags |= O_NONBLOCK;
fcntl(f, F_SETFL, flags);
}
int
libusb_init(libusb_context **context)
{
struct libusb_context *ctx;
pthread_condattr_t attr;
char *debug;
int ret;
ctx = malloc(sizeof(*ctx));
if (!ctx)
return (LIBUSB_ERROR_INVALID_PARAM);
memset(ctx, 0, sizeof(*ctx));
debug = getenv("LIBUSB_DEBUG");
if (debug != NULL) {
ctx->debug = atoi(debug);
if (ctx->debug != 0)
ctx->debug_fixed = 1;
}
TAILQ_INIT(&ctx->pollfds);
TAILQ_INIT(&ctx->tr_done);
if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
free(ctx);
return (LIBUSB_ERROR_NO_MEM);
}
if (pthread_condattr_init(&attr) != 0) {
pthread_mutex_destroy(&ctx->ctx_lock);
free(ctx);
return (LIBUSB_ERROR_NO_MEM);
}
if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
pthread_mutex_destroy(&ctx->ctx_lock);
pthread_condattr_destroy(&attr);
free(ctx);
return (LIBUSB_ERROR_OTHER);
}
if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
pthread_mutex_destroy(&ctx->ctx_lock);
pthread_condattr_destroy(&attr);
free(ctx);
return (LIBUSB_ERROR_NO_MEM);
}
pthread_condattr_destroy(&attr);
ctx->ctx_handler = NO_THREAD;
ret = pipe(ctx->ctrl_pipe);
if (ret < 0) {
pthread_mutex_destroy(&ctx->ctx_lock);
pthread_cond_destroy(&ctx->ctx_cond);
free(ctx);
return (LIBUSB_ERROR_OTHER);
}
/* set non-blocking mode on the control pipe to avoid deadlock */
libusb_set_nonblocking(ctx->ctrl_pipe[0]);
libusb_set_nonblocking(ctx->ctrl_pipe[1]);
libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
pthread_mutex_lock(&default_context_lock);
if (usbi_default_context == NULL) {
usbi_default_context = ctx;
}
pthread_mutex_unlock(&default_context_lock);
if (context)
*context = ctx;
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
return (0);
}
void
libusb_exit(libusb_context *ctx)
{
ctx = GET_CONTEXT(ctx);
if (ctx == NULL)
return;
/* XXX cleanup devices */
libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
close(ctx->ctrl_pipe[0]);
close(ctx->ctrl_pipe[1]);
pthread_mutex_destroy(&ctx->ctx_lock);
pthread_cond_destroy(&ctx->ctx_cond);
pthread_mutex_lock(&default_context_lock);
if (ctx == usbi_default_context) {
usbi_default_context = NULL;
}
pthread_mutex_unlock(&default_context_lock);
free(ctx);
}
/* Device handling and initialisation. */
ssize_t
libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
{
struct libusb20_backend *usb_backend;
struct libusb20_device *pdev;
struct libusb_device *dev;
int i;
ctx = GET_CONTEXT(ctx);
if (ctx == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
if (list == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
usb_backend = libusb20_be_alloc_default();
if (usb_backend == NULL)
return (LIBUSB_ERROR_NO_MEM);
/* figure out how many USB devices are present */
pdev = NULL;
i = 0;
while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
i++;
/* allocate device pointer list */
*list = malloc((i + 1) * sizeof(void *));
if (*list == NULL) {
libusb20_be_free(usb_backend);
return (LIBUSB_ERROR_NO_MEM);
}
/* create libusb v1.0 compliant devices */
i = 0;
while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
dev = malloc(sizeof(*dev));
if (dev == NULL) {
while (i != 0) {
libusb_unref_device((*list)[i - 1]);
i--;
}
free(*list);
*list = NULL;
libusb20_be_free(usb_backend);
return (LIBUSB_ERROR_NO_MEM);
}
/* get device into libUSB v1.0 list */
libusb20_be_dequeue_device(usb_backend, pdev);
memset(dev, 0, sizeof(*dev));
/* init transfer queues */
TAILQ_INIT(&dev->tr_head);
/* set context we belong to */
dev->ctx = ctx;
/* link together the two structures */
dev->os_priv = pdev;
pdev->privLuData = dev;
(*list)[i] = libusb_ref_device(dev);
i++;
}
(*list)[i] = NULL;
libusb20_be_free(usb_backend);
return (i);
}
void
libusb_free_device_list(libusb_device **list, int unref_devices)
{
int i;
if (list == NULL)
return; /* be NULL safe */
if (unref_devices) {
for (i = 0; list[i] != NULL; i++)
libusb_unref_device(list[i]);
}
free(list);
}
uint8_t
libusb_get_bus_number(libusb_device *dev)
{
if (dev == NULL)
return (0); /* should not happen */
return (libusb20_dev_get_bus_number(dev->os_priv));
}
int
libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
{
return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
}
int
libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
uint8_t bufsize)
{
return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
}
uint8_t
libusb_get_device_address(libusb_device *dev)
{
if (dev == NULL)
return (0); /* should not happen */
return (libusb20_dev_get_address(dev->os_priv));
}
enum libusb_speed
libusb_get_device_speed(libusb_device *dev)
{
if (dev == NULL)
return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
switch (libusb20_dev_get_speed(dev->os_priv)) {
case LIBUSB20_SPEED_LOW:
return (LIBUSB_SPEED_LOW);
case LIBUSB20_SPEED_FULL:
return (LIBUSB_SPEED_FULL);
case LIBUSB20_SPEED_HIGH:
return (LIBUSB_SPEED_HIGH);
case LIBUSB20_SPEED_SUPER:
return (LIBUSB_SPEED_SUPER);
default:
break;
}
return (LIBUSB_SPEED_UNKNOWN);
}
int
libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
{
struct libusb_config_descriptor *pdconf;
struct libusb_interface *pinf;
struct libusb_interface_descriptor *pdinf;
struct libusb_endpoint_descriptor *pdend;
int i;
int j;
int k;
int ret;
if (dev == NULL)
return (LIBUSB_ERROR_NO_DEVICE);
ret = libusb_get_active_config_descriptor(dev, &pdconf);
if (ret < 0)
return (ret);
ret = LIBUSB_ERROR_NOT_FOUND;
for (i = 0; i < pdconf->bNumInterfaces; i++) {
pinf = &pdconf->interface[i];
for (j = 0; j < pinf->num_altsetting; j++) {
pdinf = &pinf->altsetting[j];
for (k = 0; k < pdinf->bNumEndpoints; k++) {
pdend = &pdinf->endpoint[k];
if (pdend->bEndpointAddress == endpoint) {
ret = pdend->wMaxPacketSize;
goto out;
}
}
}
}
out:
libusb_free_config_descriptor(pdconf);
return (ret);
}
int
libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
{
int multiplier;
int ret;
ret = libusb_get_max_packet_size(dev, endpoint);
switch (libusb20_dev_get_speed(dev->os_priv)) {
case LIBUSB20_SPEED_LOW:
case LIBUSB20_SPEED_FULL:
break;
default:
if (ret > -1) {
multiplier = (1 + ((ret >> 11) & 3));
if (multiplier > 3)
multiplier = 3;
ret = (ret & 0x7FF) * multiplier;
}
break;
}
return (ret);
}
libusb_device *
libusb_ref_device(libusb_device *dev)
{
if (dev == NULL)
return (NULL); /* be NULL safe */
CTX_LOCK(dev->ctx);
dev->refcnt++;
CTX_UNLOCK(dev->ctx);
return (dev);
}
void
libusb_unref_device(libusb_device *dev)
{
if (dev == NULL)
return; /* be NULL safe */
CTX_LOCK(dev->ctx);
dev->refcnt--;
CTX_UNLOCK(dev->ctx);
if (dev->refcnt == 0) {
libusb20_dev_free(dev->os_priv);
free(dev);
}
}
int
libusb_open(libusb_device *dev, libusb_device_handle **devh)
{
libusb_context *ctx = dev->ctx;
struct libusb20_device *pdev = dev->os_priv;
uint8_t dummy;
int err;
if (devh == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
/* set default device handle value */
*devh = NULL;
dev = libusb_ref_device(dev);
if (dev == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
if (err) {
libusb_unref_device(dev);
return (LIBUSB_ERROR_NO_MEM);
}
libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
POLLOUT | POLLRDNORM | POLLWRNORM);
/* make sure our event loop detects the new device */
dummy = 0;
err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
if (err < (int)sizeof(dummy)) {
/* ignore error, if any */
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
}
*devh = pdev;
return (0);
}
libusb_device_handle *
libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
uint16_t product_id)
{
struct libusb_device **devs;
struct libusb20_device *pdev;
struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
int i;
int j;
ctx = GET_CONTEXT(ctx);
if (ctx == NULL)
return (NULL); /* be NULL safe */
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
if ((i = libusb_get_device_list(ctx, &devs)) < 0)
return (NULL);
pdev = NULL;
for (j = 0; j < i; j++) {
struct libusb20_device *tdev;
tdev = devs[j]->os_priv;
pdesc = libusb20_dev_get_device_desc(tdev);
/*
* NOTE: The USB library will automatically swap the
* fields in the device descriptor to be of host
* endian type!
*/
if (pdesc->idVendor == vendor_id &&
pdesc->idProduct == product_id) {
libusb_open(devs[j], &pdev);
break;
}
}
libusb_free_device_list(devs, 1);
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
return (pdev);
}
void
libusb_close(struct libusb20_device *pdev)
{
libusb_context *ctx;
struct libusb_device *dev;
uint8_t dummy;
int err;
if (pdev == NULL)
return; /* be NULL safe */
dev = libusb_get_device(pdev);
ctx = dev->ctx;
libusb10_remove_pollfd(ctx, &dev->dev_poll);
libusb20_dev_close(pdev);
/* unref will free the "pdev" when the refcount reaches zero */
libusb_unref_device(dev);
/* make sure our event loop detects the closed device */
dummy = 0;
err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
if (err < (int)sizeof(dummy)) {
/* ignore error, if any */
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
}
}
libusb_device *
libusb_get_device(struct libusb20_device *pdev)
{
if (pdev == NULL)
return (NULL);
return ((libusb_device *)pdev->privLuData);
}
int
libusb_get_configuration(struct libusb20_device *pdev, int *config)
{
struct libusb20_config *pconf;
if (pdev == NULL || config == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
if (pconf == NULL)
return (LIBUSB_ERROR_NO_MEM);
*config = pconf->desc.bConfigurationValue;
free(pconf);
return (0);
}
int
libusb_set_configuration(struct libusb20_device *pdev, int configuration)
{
struct libusb20_config *pconf;
struct libusb_device *dev;
int err;
uint8_t i;
dev = libusb_get_device(pdev);
if (dev == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
if (configuration < 1) {
/* unconfigure */
i = 255;
} else {
for (i = 0; i != 255; i++) {
uint8_t found;
pconf = libusb20_dev_alloc_config(pdev, i);
if (pconf == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
found = (pconf->desc.bConfigurationValue
== configuration);
free(pconf);
if (found)
goto set_config;
}
return (LIBUSB_ERROR_INVALID_PARAM);
}
set_config:
libusb10_cancel_all_transfer(dev);
libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
err = libusb20_dev_set_config_index(pdev, i);
libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
POLLOUT | POLLRDNORM | POLLWRNORM);
return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
}
int
libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
{
libusb_device *dev;
dev = libusb_get_device(pdev);
if (dev == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
if (interface_number < 0 || interface_number > 31)
return (LIBUSB_ERROR_INVALID_PARAM);
CTX_LOCK(dev->ctx);
dev->claimed_interfaces |= (1 << interface_number);
CTX_UNLOCK(dev->ctx);
return (0);
}
int
libusb_release_interface(struct libusb20_device *pdev, int interface_number)
{
libusb_device *dev;
int err = 0;
dev = libusb_get_device(pdev);
if (dev == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
if (interface_number < 0 || interface_number > 31)
return (LIBUSB_ERROR_INVALID_PARAM);
CTX_LOCK(dev->ctx);
if (!(dev->claimed_interfaces & (1 << interface_number)))
err = LIBUSB_ERROR_NOT_FOUND;
if (!err)
dev->claimed_interfaces &= ~(1 << interface_number);
CTX_UNLOCK(dev->ctx);
return (err);
}
int
libusb_set_interface_alt_setting(struct libusb20_device *pdev,
int interface_number, int alternate_setting)
{
libusb_device *dev;
int err = 0;
dev = libusb_get_device(pdev);
if (dev == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
if (interface_number < 0 || interface_number > 31)
return (LIBUSB_ERROR_INVALID_PARAM);
CTX_LOCK(dev->ctx);
if (!(dev->claimed_interfaces & (1 << interface_number)))
err = LIBUSB_ERROR_NOT_FOUND;
CTX_UNLOCK(dev->ctx);
if (err)
return (err);
libusb10_cancel_all_transfer(dev);
libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
err = libusb20_dev_set_alt_index(pdev,
interface_number, alternate_setting);
libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
pdev, libusb20_dev_get_fd(pdev),
POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
return (err ? LIBUSB_ERROR_OTHER : 0);
}
static struct libusb20_transfer *
libusb10_get_transfer(struct libusb20_device *pdev,
uint8_t endpoint, uint8_t xfer_index)
{
xfer_index &= 1; /* double buffering */
xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
/* this is an IN endpoint */
xfer_index |= 2;
}
return (libusb20_tr_get_pointer(pdev, xfer_index));
}
int
libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
{
struct libusb20_transfer *xfer;
struct libusb_device *dev;
int err;
xfer = libusb10_get_transfer(pdev, endpoint, 0);
if (xfer == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
dev = libusb_get_device(pdev);
if (dev == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
CTX_LOCK(dev->ctx);
err = libusb20_tr_open(xfer, 0, 1, endpoint);
CTX_UNLOCK(dev->ctx);
if (err != 0 && err != LIBUSB20_ERROR_BUSY)
return (LIBUSB_ERROR_OTHER);
libusb20_tr_clear_stall_sync(xfer);
/* check if we opened the transfer */
if (err == 0) {
CTX_LOCK(dev->ctx);
libusb20_tr_close(xfer);
CTX_UNLOCK(dev->ctx);
}
return (0); /* success */
}
int
libusb_reset_device(struct libusb20_device *pdev)
{
libusb_device *dev;
int err;
dev = libusb_get_device(pdev);
if (dev == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
libusb10_cancel_all_transfer(dev);
libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
err = libusb20_dev_reset(pdev);
libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
pdev, libusb20_dev_get_fd(pdev),
POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
return (err ? LIBUSB_ERROR_OTHER : 0);
}
int
libusb_check_connected(struct libusb20_device *pdev)
{
libusb_device *dev;
int err;
dev = libusb_get_device(pdev);
if (dev == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
err = libusb20_dev_check_connected(pdev);
return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
}
int
libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
{
if (pdev == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
if (libusb20_dev_kernel_driver_active(pdev, interface))
return (0); /* no kernel driver is active */
else
return (1); /* kernel driver is active */
}
int
libusb_get_driver_np(struct libusb20_device *pdev, int interface,
char *name, int namelen)
{
return (libusb_get_driver(pdev, interface, name, namelen));
}
int
libusb_get_driver(struct libusb20_device *pdev, int interface,
char *name, int namelen)
{
char *ptr;
int err;
if (pdev == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
if (namelen < 1)
return (LIBUSB_ERROR_INVALID_PARAM);
if (namelen > 255)
namelen = 255;
err = libusb20_dev_get_iface_desc(
pdev, interface, name, namelen);
if (err != 0)
return (LIBUSB_ERROR_OTHER);
/* we only want the driver name */
ptr = strstr(name, ":");
if (ptr != NULL)
*ptr = 0;
return (0);
}
int
libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
{
return (libusb_detach_kernel_driver(pdev, interface));
}
int
libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
{
int err;
if (pdev == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
err = libusb20_dev_detach_kernel_driver(
pdev, interface);
return (err ? LIBUSB_ERROR_OTHER : 0);
}
int
libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
{
if (pdev == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
/* stub - currently not supported by libusb20 */
return (0);
}
/* Asynchronous device I/O */
struct libusb_transfer *
libusb_alloc_transfer(int iso_packets)
{
struct libusb_transfer *uxfer;
struct libusb_super_transfer *sxfer;
int len;
len = sizeof(struct libusb_transfer) +
sizeof(struct libusb_super_transfer) +
(iso_packets * sizeof(libusb_iso_packet_descriptor));
sxfer = malloc(len);
if (sxfer == NULL)
return (NULL);
memset(sxfer, 0, len);
uxfer = (struct libusb_transfer *)(
((uint8_t *)sxfer) + sizeof(*sxfer));
/* set default value */
uxfer->num_iso_packets = iso_packets;
return (uxfer);
}
void
libusb_free_transfer(struct libusb_transfer *uxfer)
{
struct libusb_super_transfer *sxfer;
if (uxfer == NULL)
return; /* be NULL safe */
/* check if we should free the transfer buffer */
if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
free(uxfer->buffer);
sxfer = (struct libusb_super_transfer *)(
(uint8_t *)uxfer - sizeof(*sxfer));
free(sxfer);
}
static uint32_t
libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
{
uint32_t ret;
switch (xfer->type) {
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
break;
case LIBUSB_TRANSFER_TYPE_CONTROL:
ret = 2;
break;
default:
ret = 1;
break;
}
return (ret);
}
static int
libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
{
int ret;
int usb_speed;
usb_speed = libusb20_dev_get_speed(pdev);
switch (xfer->type) {
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
ret = 0; /* kernel will auto-select */
break;
case LIBUSB_TRANSFER_TYPE_CONTROL:
ret = 1024;
break;
default:
switch (usb_speed) {
case LIBUSB20_SPEED_LOW:
ret = 256;
break;
case LIBUSB20_SPEED_FULL:
ret = 4096;
break;
case LIBUSB20_SPEED_SUPER:
ret = 65536;
break;
default:
ret = 16384;
break;
}
break;
}
return (ret);
}
static int
libusb10_convert_error(uint8_t status)
{
; /* indent fix */
switch (status) {
case LIBUSB20_TRANSFER_START:
case LIBUSB20_TRANSFER_COMPLETED:
return (LIBUSB_TRANSFER_COMPLETED);
case LIBUSB20_TRANSFER_OVERFLOW:
return (LIBUSB_TRANSFER_OVERFLOW);
case LIBUSB20_TRANSFER_NO_DEVICE:
return (LIBUSB_TRANSFER_NO_DEVICE);
case LIBUSB20_TRANSFER_STALL:
return (LIBUSB_TRANSFER_STALL);
case LIBUSB20_TRANSFER_CANCELLED:
return (LIBUSB_TRANSFER_CANCELLED);
case LIBUSB20_TRANSFER_TIMED_OUT:
return (LIBUSB_TRANSFER_TIMED_OUT);
default:
return (LIBUSB_TRANSFER_ERROR);
}
}
/* This function must be called locked */
static void
libusb10_complete_transfer(struct libusb20_transfer *pxfer,
struct libusb_super_transfer *sxfer, int status)
{
struct libusb_transfer *uxfer;
struct libusb_device *dev;
uxfer = (struct libusb_transfer *)(
((uint8_t *)sxfer) + sizeof(*sxfer));
if (pxfer != NULL)
libusb20_tr_set_priv_sc1(pxfer, NULL);
/* set transfer status */
uxfer->status = status;
/* update super transfer state */
sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
dev = libusb_get_device(uxfer->dev_handle);
TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
}
/* This function must be called locked */
static void
libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
{
struct libusb_super_transfer *sxfer;
struct libusb_transfer *uxfer;
uint32_t actlen;
uint16_t iso_packets;
uint16_t i;
uint8_t status;
uint8_t flags;
status = libusb20_tr_get_status(pxfer);
sxfer = libusb20_tr_get_priv_sc1(pxfer);
actlen = libusb20_tr_get_actual_length(pxfer);
iso_packets = libusb20_tr_get_max_frames(pxfer);
if (sxfer == NULL)
return; /* cancelled - nothing to do */
uxfer = (struct libusb_transfer *)(
((uint8_t *)sxfer) + sizeof(*sxfer));
if (iso_packets > uxfer->num_iso_packets)
iso_packets = uxfer->num_iso_packets;
if (iso_packets == 0)
return; /* nothing to do */
/* make sure that the number of ISOCHRONOUS packets is valid */
uxfer->num_iso_packets = iso_packets;
flags = uxfer->flags;
switch (status) {
case LIBUSB20_TRANSFER_COMPLETED:
/* update actual length */
uxfer->actual_length = actlen;
for (i = 0; i != iso_packets; i++) {
uxfer->iso_packet_desc[i].actual_length =
libusb20_tr_get_length(pxfer, i);
}
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
break;
case LIBUSB20_TRANSFER_START:
/* setup length(s) */
actlen = 0;
for (i = 0; i != iso_packets; i++) {
libusb20_tr_setup_isoc(pxfer,
&uxfer->buffer[actlen],
uxfer->iso_packet_desc[i].length, i);
actlen += uxfer->iso_packet_desc[i].length;
}
/* no remainder */
sxfer->rem_len = 0;
libusb20_tr_set_total_frames(pxfer, iso_packets);
libusb20_tr_submit(pxfer);
/* fork another USB transfer, if any */
libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
break;
default:
libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
break;
}
}
/* This function must be called locked */
static void
libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
{
struct libusb_super_transfer *sxfer;
struct libusb_transfer *uxfer;
uint32_t max_bulk;
uint32_t actlen;
uint8_t status;
uint8_t flags;
status = libusb20_tr_get_status(pxfer);
sxfer = libusb20_tr_get_priv_sc1(pxfer);
max_bulk = libusb20_tr_get_max_total_length(pxfer);
actlen = libusb20_tr_get_actual_length(pxfer);
if (sxfer == NULL)
return; /* cancelled - nothing to do */
uxfer = (struct libusb_transfer *)(
((uint8_t *)sxfer) + sizeof(*sxfer));
flags = uxfer->flags;
switch (status) {
case LIBUSB20_TRANSFER_COMPLETED:
uxfer->actual_length += actlen;
/* check for short packet */
if (sxfer->last_len != actlen) {
if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
} else {
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
}
break;
}
/* check for end of data */
if (sxfer->rem_len == 0) {
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
break;
}
/* FALLTHROUGH */
case LIBUSB20_TRANSFER_START:
if (max_bulk > sxfer->rem_len) {
max_bulk = sxfer->rem_len;
}
/* setup new BULK or INTERRUPT transaction */
libusb20_tr_setup_bulk(pxfer,
sxfer->curr_data, max_bulk, uxfer->timeout);
/* update counters */
sxfer->last_len = max_bulk;
sxfer->curr_data += max_bulk;
sxfer->rem_len -= max_bulk;
libusb20_tr_submit(pxfer);
/* check if we can fork another USB transfer */
if (sxfer->rem_len == 0)
libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
break;
default:
libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
break;
}
}
/* This function must be called locked */
static void
libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
{
struct libusb_super_transfer *sxfer;
struct libusb_transfer *uxfer;
uint32_t max_bulk;
uint32_t actlen;
uint8_t status;
uint8_t flags;
status = libusb20_tr_get_status(pxfer);
sxfer = libusb20_tr_get_priv_sc1(pxfer);
max_bulk = libusb20_tr_get_max_total_length(pxfer);
actlen = libusb20_tr_get_actual_length(pxfer);
if (sxfer == NULL)
return; /* cancelled - nothing to do */
uxfer = (struct libusb_transfer *)(
((uint8_t *)sxfer) + sizeof(*sxfer));
flags = uxfer->flags;
switch (status) {
case LIBUSB20_TRANSFER_COMPLETED:
uxfer->actual_length += actlen;
/* subtract length of SETUP packet, if any */
actlen -= libusb20_tr_get_length(pxfer, 0);
/* check for short packet */
if (sxfer->last_len != actlen) {
if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
} else {
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
}
break;
}
/* check for end of data */
if (sxfer->rem_len == 0) {
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
break;
}
/* FALLTHROUGH */
case LIBUSB20_TRANSFER_START:
if (max_bulk > sxfer->rem_len) {
max_bulk = sxfer->rem_len;
}
/* setup new CONTROL transaction */
if (status == LIBUSB20_TRANSFER_COMPLETED) {
/* next fragment - don't send SETUP packet */
libusb20_tr_set_length(pxfer, 0, 0);
} else {
/* first fragment - send SETUP packet */
libusb20_tr_set_length(pxfer, 8, 0);
libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
}
if (max_bulk != 0) {
libusb20_tr_set_length(pxfer, max_bulk, 1);
libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
libusb20_tr_set_total_frames(pxfer, 2);
} else {
libusb20_tr_set_total_frames(pxfer, 1);
}
/* update counters */
sxfer->last_len = max_bulk;
sxfer->curr_data += max_bulk;
sxfer->rem_len -= max_bulk;
libusb20_tr_submit(pxfer);
/* check if we can fork another USB transfer */
if (sxfer->rem_len == 0)
libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
break;
default:
libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
break;
}
}
/* The following function must be called locked */
static void
libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
{
struct libusb20_transfer *pxfer0;
struct libusb20_transfer *pxfer1;
struct libusb_super_transfer *sxfer;
struct libusb_transfer *uxfer;
struct libusb_device *dev;
int err;
int buffsize;
int maxframe;
int temp;
uint8_t dummy;
dev = libusb_get_device(pdev);
pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
if (pxfer0 == NULL || pxfer1 == NULL)
return; /* shouldn't happen */
temp = 0;
if (libusb20_tr_pending(pxfer0))
temp |= 1;
if (libusb20_tr_pending(pxfer1))
temp |= 2;
switch (temp) {
case 3:
/* wait till one of the transfers complete */
return;
case 2:
sxfer = libusb20_tr_get_priv_sc1(pxfer1);
if (sxfer == NULL)
return; /* cancelling */
if (sxfer->rem_len)
return; /* cannot queue another one */
/* swap transfers */
pxfer1 = pxfer0;
break;
case 1:
sxfer = libusb20_tr_get_priv_sc1(pxfer0);
if (sxfer == NULL)
return; /* cancelling */
if (sxfer->rem_len)
return; /* cannot queue another one */
/* swap transfers */
pxfer0 = pxfer1;
break;
default:
break;
}
/* find next transfer on same endpoint */
TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
uxfer = (struct libusb_transfer *)(
((uint8_t *)sxfer) + sizeof(*sxfer));
if (uxfer->endpoint == endpoint) {
TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
sxfer->entry.tqe_prev = NULL;
goto found;
}
}
return; /* success */
found:
libusb20_tr_set_priv_sc0(pxfer0, pdev);
libusb20_tr_set_priv_sc1(pxfer0, sxfer);
/* reset super transfer state */
sxfer->rem_len = uxfer->length;
sxfer->curr_data = uxfer->buffer;
uxfer->actual_length = 0;
switch (uxfer->type) {
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
break;
case LIBUSB_TRANSFER_TYPE_BULK:
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
break;
case LIBUSB_TRANSFER_TYPE_CONTROL:
libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
if (sxfer->rem_len < 8)
goto failure;
/* remove SETUP packet from data */
sxfer->rem_len -= 8;
sxfer->curr_data += 8;
break;
default:
goto failure;
}
buffsize = libusb10_get_buffsize(pdev, uxfer);
maxframe = libusb10_get_maxframe(pdev, uxfer);
/* make sure the transfer is opened */
err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
if (err && (err != LIBUSB20_ERROR_BUSY)) {
goto failure;
}
libusb20_tr_start(pxfer0);
return;
failure:
libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
/* make sure our event loop spins the done handler */
dummy = 0;
err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
}
/* The following function must be called unlocked */
int
libusb_submit_transfer(struct libusb_transfer *uxfer)
{
struct libusb20_transfer *pxfer0;
struct libusb20_transfer *pxfer1;
struct libusb_super_transfer *sxfer;
struct libusb_device *dev;
uint8_t endpoint;
int err;
if (uxfer == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
if (uxfer->dev_handle == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
endpoint = uxfer->endpoint;
dev = libusb_get_device(uxfer->dev_handle);
DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
sxfer = (struct libusb_super_transfer *)(
(uint8_t *)uxfer - sizeof(*sxfer));
CTX_LOCK(dev->ctx);
pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
if (pxfer0 == NULL || pxfer1 == NULL) {
err = LIBUSB_ERROR_OTHER;
} else if ((sxfer->entry.tqe_prev != NULL) ||
(libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
(libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
err = LIBUSB_ERROR_BUSY;
} else {
/* set pending state */
sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
/* insert transfer into transfer head list */
TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
/* start work transfers */
libusb10_submit_transfer_sub(
uxfer->dev_handle, endpoint);
err = 0; /* success */
}
CTX_UNLOCK(dev->ctx);
DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
return (err);
}
/* Asynchronous transfer cancel */
int
libusb_cancel_transfer(struct libusb_transfer *uxfer)
{
struct libusb20_transfer *pxfer0;
struct libusb20_transfer *pxfer1;
struct libusb_super_transfer *sxfer;
struct libusb_device *dev;
uint8_t endpoint;
int retval;
if (uxfer == NULL)
return (LIBUSB_ERROR_INVALID_PARAM);
/* check if not initialised */
if (uxfer->dev_handle == NULL)
return (LIBUSB_ERROR_NOT_FOUND);
endpoint = uxfer->endpoint;
dev = libusb_get_device(uxfer->dev_handle);
DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
sxfer = (struct libusb_super_transfer *)(
(uint8_t *)uxfer - sizeof(*sxfer));
retval = 0;
CTX_LOCK(dev->ctx);
pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
/* only update the transfer status */
uxfer->status = LIBUSB_TRANSFER_CANCELLED;
retval = LIBUSB_ERROR_NOT_FOUND;
} else if (sxfer->entry.tqe_prev != NULL) {
/* we are lucky - transfer is on a queue */
TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
sxfer->entry.tqe_prev = NULL;
libusb10_complete_transfer(NULL,
sxfer, LIBUSB_TRANSFER_CANCELLED);
} else if (pxfer0 == NULL || pxfer1 == NULL) {
/* not started */
retval = LIBUSB_ERROR_NOT_FOUND;
} else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
libusb10_complete_transfer(pxfer0,
sxfer, LIBUSB_TRANSFER_CANCELLED);
libusb20_tr_stop(pxfer0);
/* make sure the queue doesn't stall */
libusb10_submit_transfer_sub(
uxfer->dev_handle, endpoint);
} else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
libusb10_complete_transfer(pxfer1,
sxfer, LIBUSB_TRANSFER_CANCELLED);
libusb20_tr_stop(pxfer1);
/* make sure the queue doesn't stall */
libusb10_submit_transfer_sub(
uxfer->dev_handle, endpoint);
} else {
/* not started */
retval = LIBUSB_ERROR_NOT_FOUND;
}
CTX_UNLOCK(dev->ctx);
DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
return (retval);
}
UNEXPORTED void
libusb10_cancel_all_transfer(libusb_device *dev)
{
/* TODO */
}
uint16_t
libusb_cpu_to_le16(uint16_t x)
{
return (htole16(x));
}
uint16_t
libusb_le16_to_cpu(uint16_t x)
{
return (le16toh(x));
}
const char *
libusb_strerror(int code)
{
switch (code) {
case LIBUSB_SUCCESS:
return ("Success");
case LIBUSB_ERROR_IO:
return ("I/O error");
case LIBUSB_ERROR_INVALID_PARAM:
return ("Invalid parameter");
case LIBUSB_ERROR_ACCESS:
return ("Permissions error");
case LIBUSB_ERROR_NO_DEVICE:
return ("No device");
case LIBUSB_ERROR_NOT_FOUND:
return ("Not found");
case LIBUSB_ERROR_BUSY:
return ("Device busy");
case LIBUSB_ERROR_TIMEOUT:
return ("Timeout");
case LIBUSB_ERROR_OVERFLOW:
return ("Overflow");
case LIBUSB_ERROR_PIPE:
return ("Pipe error");
case LIBUSB_ERROR_INTERRUPTED:
return ("Interrupted");
case LIBUSB_ERROR_NO_MEM:
return ("Out of memory");
case LIBUSB_ERROR_NOT_SUPPORTED:
return ("Not supported");
case LIBUSB_ERROR_OTHER:
return ("Other error");
default:
return ("Unknown error");
}
}
const char *
libusb_error_name(int code)
{
switch (code) {
case LIBUSB_SUCCESS:
return ("LIBUSB_SUCCESS");
case LIBUSB_ERROR_IO:
return ("LIBUSB_ERROR_IO");
case LIBUSB_ERROR_INVALID_PARAM:
return ("LIBUSB_ERROR_INVALID_PARAM");
case LIBUSB_ERROR_ACCESS:
return ("LIBUSB_ERROR_ACCESS");
case LIBUSB_ERROR_NO_DEVICE:
return ("LIBUSB_ERROR_NO_DEVICE");
case LIBUSB_ERROR_NOT_FOUND:
return ("LIBUSB_ERROR_NOT_FOUND");
case LIBUSB_ERROR_BUSY:
return ("LIBUSB_ERROR_BUSY");
case LIBUSB_ERROR_TIMEOUT:
return ("LIBUSB_ERROR_TIMEOUT");
case LIBUSB_ERROR_OVERFLOW:
return ("LIBUSB_ERROR_OVERFLOW");
case LIBUSB_ERROR_PIPE:
return ("LIBUSB_ERROR_PIPE");
case LIBUSB_ERROR_INTERRUPTED:
return ("LIBUSB_ERROR_INTERRUPTED");
case LIBUSB_ERROR_NO_MEM:
return ("LIBUSB_ERROR_NO_MEM");
case LIBUSB_ERROR_NOT_SUPPORTED:
return ("LIBUSB_ERROR_NOT_SUPPORTED");
case LIBUSB_ERROR_OTHER:
return ("LIBUSB_ERROR_OTHER");
default:
return ("LIBUSB_ERROR_UNKNOWN");
}
}
|