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 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2010-2015 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2011 Hans de Goede <hdegoede@redhat.com>
* Copyright (C) 2011 Debarshi Ray <debarshir@src.gnome.org>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/**
* SECTION:gusb-device
* @short_description: GLib device integration for libusb
*
* This object is a thin glib wrapper around a libusb_device
*/
#include "config.h"
#include <string.h>
#include <libusb.h>
#include "gusb-context.h"
#include "gusb-context-private.h"
#include "gusb-util.h"
#include "gusb-device.h"
#include "gusb-device-private.h"
#include "gusb-interface-private.h"
/**
* GUsbDevicePrivate:
*
* Private #GUsbDevice data
**/
struct _GUsbDevicePrivate
{
gchar *platform_id;
GUsbContext *context;
libusb_device *device;
libusb_device_handle *handle;
struct libusb_device_descriptor desc;
};
enum {
PROP_0,
PROP_LIBUSB_DEVICE,
PROP_CONTEXT,
PROP_PLATFORM_ID,
N_PROPERTIES
};
static GParamSpec *pspecs[N_PROPERTIES] = { NULL, };
static void g_usb_device_initable_iface_init (GInitableIface *iface);
G_DEFINE_TYPE_WITH_CODE (GUsbDevice, g_usb_device, G_TYPE_OBJECT,
G_ADD_PRIVATE (GUsbDevice)
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
g_usb_device_initable_iface_init))
/**
* g_usb_device_error_quark:
*
* Return value: Our personal error quark.
*
* Since: 0.1.0
**/
G_DEFINE_QUARK (g-usb-device-error-quark, g_usb_device_error)
static void
g_usb_device_finalize (GObject *object)
{
GUsbDevice *device = G_USB_DEVICE (object);
GUsbDevicePrivate *priv = device->priv;
g_free (priv->platform_id);
G_OBJECT_CLASS (g_usb_device_parent_class)->finalize (object);
}
static void
g_usb_device_dispose (GObject *object)
{
GUsbDevice *device = G_USB_DEVICE (object);
GUsbDevicePrivate *priv = device->priv;
g_clear_pointer (&priv->device, libusb_unref_device);
g_clear_object (&priv->context);
G_OBJECT_CLASS (g_usb_device_parent_class)->dispose (object);
}
static void
g_usb_device_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GUsbDevice *device = G_USB_DEVICE (object);
GUsbDevicePrivate *priv = device->priv;
switch (prop_id) {
case PROP_LIBUSB_DEVICE:
g_value_set_pointer (value, priv->device);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
g_usb_device_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GUsbDevice *device = G_USB_DEVICE (object);
GUsbDevicePrivate *priv = device->priv;
switch (prop_id) {
case PROP_LIBUSB_DEVICE:
priv->device = g_value_get_pointer (value);
break;
case PROP_CONTEXT:
priv->context = g_value_dup_object (value);
break;
case PROP_PLATFORM_ID:
priv->platform_id = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
g_usb_device_constructed (GObject *object)
{
GUsbDevice *device = G_USB_DEVICE (object);
GUsbDevicePrivate *priv;
gint rc;
priv = device->priv;
if (!priv->device)
g_error("constructed without a libusb_device");
libusb_ref_device(priv->device);
rc = libusb_get_device_descriptor (priv->device, &priv->desc);
if (rc != LIBUSB_SUCCESS)
g_warning ("Failed to get USB descriptor for device: %s",
g_usb_strerror (rc));
G_OBJECT_CLASS (g_usb_device_parent_class)->constructed (object);
}
static void
g_usb_device_class_init (GUsbDeviceClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = g_usb_device_finalize;
object_class->dispose = g_usb_device_dispose;
object_class->get_property = g_usb_device_get_property;
object_class->set_property = g_usb_device_set_property;
object_class->constructed = g_usb_device_constructed;
/**
* GUsbDevice:libusb_device:
*/
pspecs[PROP_LIBUSB_DEVICE] =
g_param_spec_pointer ("libusb-device", NULL, NULL,
G_PARAM_CONSTRUCT_ONLY|
G_PARAM_READWRITE);
/**
* GUsbDevice:context:
*/
pspecs[PROP_CONTEXT] =
g_param_spec_object ("context", NULL, NULL,
G_USB_TYPE_CONTEXT,
G_PARAM_CONSTRUCT_ONLY|
G_PARAM_WRITABLE);
/**
* GUsbDevice:platform-id:
*/
pspecs[PROP_PLATFORM_ID] =
g_param_spec_string ("platform-id", NULL, NULL,
NULL,
G_PARAM_CONSTRUCT_ONLY|
G_PARAM_WRITABLE);
g_object_class_install_properties (object_class, N_PROPERTIES, pspecs);
}
static void
g_usb_device_init (GUsbDevice *device)
{
device->priv = g_usb_device_get_instance_private (device);
}
static void
g_usb_device_build_platform_id_cb (GString *str, libusb_device *dev)
{
libusb_device *parent;
parent = libusb_get_parent (dev);
if (parent != NULL)
g_usb_device_build_platform_id_cb (str, parent);
if (str->len == 0) {
g_string_append_printf (str, "%02x:",
libusb_get_bus_number (dev));
}
g_string_append_printf (str, "%02x:",
libusb_get_port_number (dev));
}
static gchar *
g_usb_device_build_platform_id (struct libusb_device *dev)
{
GString *platform_id;
/* build a topology of the device */
platform_id = g_string_new ("usb:");
g_usb_device_build_platform_id_cb (platform_id, dev);
g_string_truncate (platform_id, platform_id->len - 1);
return g_string_free (platform_id, FALSE);
}
static gboolean
g_usb_device_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
GUsbDevice *device = G_USB_DEVICE (initable);
GUsbDevicePrivate *priv;
gint rc;
priv = device->priv;
if (!priv->device) {
g_set_error_literal (error, G_USB_DEVICE_ERROR, G_USB_DEVICE_ERROR_INTERNAL,
"Constructed without a libusb_device");
return FALSE;
}
libusb_ref_device (priv->device);
rc = libusb_get_device_descriptor (priv->device, &priv->desc);
if (rc != LIBUSB_SUCCESS) {
g_set_error (error, G_USB_DEVICE_ERROR, G_USB_DEVICE_ERROR_INTERNAL,
"Failed to get USB descriptor for device: %s",
g_usb_strerror (rc));
return FALSE;
}
/* this does not change on plug->unplug->plug */
priv->platform_id = g_usb_device_build_platform_id (priv->device);
return TRUE;
}
static void
g_usb_device_initable_iface_init (GInitableIface *iface)
{
iface->init = g_usb_device_initable_init;
}
/**
* _g_usb_device_new:
*
* Return value: a new #GUsbDevice object.
*
* Since: 0.1.0
**/
GUsbDevice *
_g_usb_device_new (GUsbContext *context,
libusb_device *device,
GError **error)
{
return g_initable_new (G_USB_TYPE_DEVICE,
NULL, error,
"context", context,
"libusb-device", device,
NULL);
}
/**
* _g_usb_device_get_device:
* @device: a #GUsbDevice instance
*
* Gets the low-level libusb_device
*
* Return value: The #libusb_device or %NULL. Do not unref this value.
**/
libusb_device *
_g_usb_device_get_device (GUsbDevice *device)
{
return device->priv->device;
}
static gboolean
g_usb_device_libusb_error_to_gerror (GUsbDevice *device,
gint rc,
GError **error)
{
gint error_code = G_USB_DEVICE_ERROR_INTERNAL;
/* Put the rc in libusb's error enum so that gcc warns us if we're
missing an error code */
enum libusb_error result = rc;
switch (result) {
case LIBUSB_SUCCESS:
return TRUE;
case LIBUSB_ERROR_INVALID_PARAM:
case LIBUSB_ERROR_NOT_FOUND:
case LIBUSB_ERROR_NO_MEM:
case LIBUSB_ERROR_OTHER:
case LIBUSB_ERROR_INTERRUPTED:
error_code = G_USB_DEVICE_ERROR_INTERNAL;
break;
case LIBUSB_ERROR_IO:
case LIBUSB_ERROR_OVERFLOW:
case LIBUSB_ERROR_PIPE:
error_code = G_USB_DEVICE_ERROR_IO;
break;
case LIBUSB_ERROR_TIMEOUT:
error_code = G_USB_DEVICE_ERROR_TIMED_OUT;
break;
case LIBUSB_ERROR_NOT_SUPPORTED:
error_code = G_USB_DEVICE_ERROR_NOT_SUPPORTED;
break;
case LIBUSB_ERROR_ACCESS:
error_code = G_USB_DEVICE_ERROR_PERMISSION_DENIED;
break;
case LIBUSB_ERROR_NO_DEVICE:
case LIBUSB_ERROR_BUSY:
error_code = G_USB_DEVICE_ERROR_NO_DEVICE;
break;
default:
break;
}
g_set_error (error, G_USB_DEVICE_ERROR, error_code,
"USB error on device %04x:%04x : %s [%i]",
g_usb_device_get_vid (device),
g_usb_device_get_pid (device),
g_usb_strerror (rc), rc);
return FALSE;
}
static gboolean
g_usb_device_not_open_error (GUsbDevice *device,
GError **error)
{
g_set_error (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_NOT_OPEN,
"Device %04x:%04x has not been opened",
g_usb_device_get_vid (device),
g_usb_device_get_pid (device));
return FALSE;
}
static void
g_usb_device_async_not_open_error (GUsbDevice *device,
GAsyncReadyCallback callback,
gpointer user_data,
gpointer source_tag)
{
g_task_report_new_error (device, callback, user_data, source_tag,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_NOT_OPEN,
"Device %04x:%04x has not been opened",
g_usb_device_get_vid (device),
g_usb_device_get_pid (device));
}
gboolean
_g_usb_device_open_internal (GUsbDevice *device, GError **error)
{
gint rc;
if (device->priv->handle != NULL) {
g_set_error (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_ALREADY_OPEN,
"Device %04x:%04x is already open",
g_usb_device_get_vid (device),
g_usb_device_get_pid (device));
return FALSE;
}
/* open device */
rc = libusb_open (device->priv->device, &device->priv->handle);
return g_usb_device_libusb_error_to_gerror (device, rc, error);
}
/**
* g_usb_device_open:
* @device: a #GUsbDevice
* @error: a #GError, or %NULL
*
* Opens the device for use.
*
* Warning: this function is synchronous.
*
* Return value: %TRUE on success
*
* Since: 0.1.0
**/
gboolean
g_usb_device_open (GUsbDevice *device, GError **error)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
/* ignore */
if (g_usb_context_get_flags (device->priv->context) & G_USB_CONTEXT_FLAGS_AUTO_OPEN_DEVICES)
return TRUE;
/* open */
return _g_usb_device_open_internal (device, error);
}
/**
* g_usb_device_get_custom_index:
* @device: a #GUsbDevice
* @class_id: a device class, e.g. 0xff for VENDOR
* @subclass_id: a device subclass
* @protocol_id: a protocol number
* @error: a #GError, or %NULL
*
* Gets the string index from the vendor class interface descriptor.
*
* Return value: a non-zero index, or 0x00 for failure
*
* Since: 0.2.5
**/
guint8
g_usb_device_get_custom_index (GUsbDevice *device,
guint8 class_id,
guint8 subclass_id,
guint8 protocol_id,
GError **error)
{
const struct libusb_interface_descriptor *ifp;
gint rc;
guint8 idx = 0x00;
guint i;
struct libusb_config_descriptor *config;
rc = libusb_get_active_config_descriptor (device->priv->device, &config);
if (!g_usb_device_libusb_error_to_gerror (device, rc, error))
return 0x00;
/* find the right data */
for (i = 0; i < config->bNumInterfaces; i++) {
ifp = &config->interface[i].altsetting[0];
if (ifp->bInterfaceClass != class_id)
continue;
if (ifp->bInterfaceSubClass != subclass_id)
continue;
if (ifp->bInterfaceProtocol != protocol_id)
continue;
idx = ifp->iInterface;
break;
}
/* nothing matched */
if (idx == 0x00) {
g_set_error (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_NOT_SUPPORTED,
"no vendor descriptor for class 0x%02x, "
"subclass 0x%02x and protocol 0x%02x",
class_id, subclass_id, protocol_id);
}
libusb_free_config_descriptor (config);
return idx;
}
/**
* g_usb_device_get_interface:
* @device: a #GUsbDevice
* @class_id: a device class, e.g. 0xff for VENDOR
* @subclass_id: a device subclass
* @protocol_id: a protocol number
* @error: a #GError, or %NULL
*
* Gets the first interface that matches the vendor class interface descriptor.
* If you want to find all the interfaces that match (there may be other
* 'alternate' interfaces you have to use g_usb_device_get_interfaces() and
* check each one manally.
*
* Return value: (transfer full): a #GUsbInterface or %NULL for not found
*
* Since: 0.2.8
**/
GUsbInterface *
g_usb_device_get_interface (GUsbDevice *device,
guint8 class_id,
guint8 subclass_id,
guint8 protocol_id,
GError **error)
{
const struct libusb_interface_descriptor *ifp;
gint rc;
GUsbInterface *interface = NULL;
guint i;
struct libusb_config_descriptor *config;
g_return_val_if_fail (G_USB_IS_DEVICE (device), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
rc = libusb_get_active_config_descriptor (device->priv->device, &config);
if (!g_usb_device_libusb_error_to_gerror (device, rc, error))
return NULL;
/* find the right data */
for (i = 0; i < config->bNumInterfaces; i++) {
ifp = &config->interface[i].altsetting[0];
if (ifp->bInterfaceClass != class_id)
continue;
if (ifp->bInterfaceSubClass != subclass_id)
continue;
if (ifp->bInterfaceProtocol != protocol_id)
continue;
interface = _g_usb_interface_new (ifp);
break;
}
/* nothing matched */
if (interface == NULL) {
g_set_error (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_NOT_SUPPORTED,
"no interface for class 0x%02x, "
"subclass 0x%02x and protocol 0x%02x",
class_id, subclass_id, protocol_id);
}
libusb_free_config_descriptor (config);
return interface;
}
/**
* g_usb_device_get_interfaces:
* @device: a #GUsbDevice
* @error: a #GError, or %NULL
*
* Gets all the interfaces exported by the device.
*
* Return value: (transfer container) (element-type GUsbInterface): an array of interfaces or %NULL for error
*
* Since: 0.2.8
**/
GPtrArray *
g_usb_device_get_interfaces (GUsbDevice *device, GError **error)
{
const struct libusb_interface_descriptor *ifp;
gint rc;
guint i;
guint j;
struct libusb_config_descriptor *config;
GPtrArray *array = NULL;
g_return_val_if_fail (G_USB_IS_DEVICE (device), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
rc = libusb_get_active_config_descriptor (device->priv->device, &config);
if (!g_usb_device_libusb_error_to_gerror (device, rc, error))
return NULL;
/* get all interfaces */
array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
for (i = 0; i < config->bNumInterfaces; i++) {
GUsbInterface *interface = NULL;
for (j = 0; j < (guint) config->interface[i].num_altsetting; j++) {
ifp = &config->interface[i].altsetting[j];
interface = _g_usb_interface_new (ifp);
g_ptr_array_add (array, interface);
}
}
libusb_free_config_descriptor (config);
return array;
}
/**
* g_usb_device_close:
* @device: a #GUsbDevice
* @error: a #GError, or %NULL
*
* Closes the device when it is no longer required.
*
* Return value: %TRUE on success
**/
gboolean
g_usb_device_close (GUsbDevice *device,
GError **error)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
/* ignore */
if (g_usb_context_get_flags (device->priv->context) & G_USB_CONTEXT_FLAGS_AUTO_OPEN_DEVICES)
return TRUE;
if (device->priv->handle == NULL)
return g_usb_device_not_open_error (device, error);
libusb_close (device->priv->handle);
device->priv->handle = NULL;
return TRUE;
}
/**
* g_usb_device_reset:
* @device: a #GUsbDevice
* @error: a #GError, or %NULL
*
* Perform a USB port reset to reinitialize a device.
*
* If the reset succeeds, the device will appear to disconnected and reconnected.
* This means the @device will no longer be valid and should be closed and
* rediscovered.
*
* This is a blocking function which usually incurs a noticeable delay.
*
* Return value: %TRUE on success
**/
gboolean
g_usb_device_reset (GUsbDevice *device,
GError **error)
{
gint rc;
g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (device->priv->handle == NULL)
return g_usb_device_not_open_error (device, error);
rc = libusb_reset_device (device->priv->handle);
if (rc == LIBUSB_ERROR_NOT_FOUND)
return TRUE;
return g_usb_device_libusb_error_to_gerror (device, rc, error);
}
/**
* g_usb_device_get_configuration:
* @device: a #GUsbDevice
* @error: a #GError, or %NULL
*
* Get the bConfigurationValue for the active configuration of the device.
*
* Warning: this function is synchronous.
*
* Return value: The bConfigurationValue of the active config, or -1 on error
*
* Since: 0.1.0
**/
gint
g_usb_device_get_configuration (GUsbDevice *device,
GError **error)
{
gint rc;
int config;
g_return_val_if_fail (G_USB_IS_DEVICE (device), -1);
g_return_val_if_fail (error == NULL || *error == NULL, -1);
if (device->priv->handle == NULL) {
g_usb_device_not_open_error (device, error);
return -1;
}
rc = libusb_get_configuration (device->priv->handle, &config);
if (rc != LIBUSB_SUCCESS) {
g_usb_device_libusb_error_to_gerror (device, rc, error);
return -1;
}
return config;
}
/**
* g_usb_device_set_configuration:
* @device: a #GUsbDevice
* @configuration: the configuration value to set
* @error: a #GError, or %NULL
*
* Set the active bConfigurationValue for the device.
*
* Warning: this function is synchronous.
*
* Return value: %TRUE on success
*
* Since: 0.1.0
**/
gboolean
g_usb_device_set_configuration (GUsbDevice *device,
gint configuration,
GError **error)
{
gint rc;
gint config_tmp = 0;
g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (device->priv->handle == NULL)
return g_usb_device_not_open_error (device, error);
/* verify we've not already set the same configuration */
rc = libusb_get_configuration (device->priv->handle,
&config_tmp);
if (rc != LIBUSB_SUCCESS) {
return g_usb_device_libusb_error_to_gerror (device,
rc,
error);
}
if (config_tmp == configuration)
return TRUE;
/* different, so change */
rc = libusb_set_configuration (device->priv->handle, configuration);
return g_usb_device_libusb_error_to_gerror (device, rc, error);
}
/**
* g_usb_device_claim_interface:
* @device: a #GUsbDevice
* @interface: bInterfaceNumber of the interface you wish to claim
* @flags: #GUsbDeviceClaimInterfaceFlags
* @error: a #GError, or %NULL
*
* Claim an interface of the device.
*
* Return value: %TRUE on success
*
* Since: 0.1.0
**/
gboolean
g_usb_device_claim_interface (GUsbDevice *device,
gint interface,
GUsbDeviceClaimInterfaceFlags flags,
GError **error)
{
gint rc;
g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (device->priv->handle == NULL)
return g_usb_device_not_open_error (device, error);
if (flags & G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER) {
rc = libusb_detach_kernel_driver (device->priv->handle,
interface);
if (rc != LIBUSB_SUCCESS &&
rc != LIBUSB_ERROR_NOT_FOUND && /* No driver attached */
rc != LIBUSB_ERROR_NOT_SUPPORTED && /* win32 */
rc != LIBUSB_ERROR_BUSY /* driver rebound already */)
return g_usb_device_libusb_error_to_gerror (device, rc,
error);
}
rc = libusb_claim_interface (device->priv->handle, interface);
return g_usb_device_libusb_error_to_gerror (device, rc, error);
}
/**
* g_usb_device_release_interface:
* @device: a #GUsbDevice
* @interface: bInterfaceNumber of the interface you wish to release
* @flags: #GUsbDeviceClaimInterfaceFlags
* @error: a #GError, or %NULL
*
* Release an interface of the device.
*
* Return value: %TRUE on success
*
* Since: 0.1.0
**/
gboolean
g_usb_device_release_interface (GUsbDevice *device,
gint interface,
GUsbDeviceClaimInterfaceFlags flags,
GError **error)
{
gint rc;
g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (device->priv->handle == NULL)
return g_usb_device_not_open_error (device, error);
rc = libusb_release_interface (device->priv->handle, interface);
if (rc != LIBUSB_SUCCESS)
return g_usb_device_libusb_error_to_gerror (device, rc, error);
if (flags & G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER) {
rc = libusb_attach_kernel_driver (device->priv->handle,
interface);
if (rc != LIBUSB_SUCCESS &&
rc != LIBUSB_ERROR_NOT_FOUND && /* No driver attached */
rc != LIBUSB_ERROR_NOT_SUPPORTED && /* win32 */
rc != LIBUSB_ERROR_BUSY /* driver rebound already */)
return g_usb_device_libusb_error_to_gerror (device, rc,
error);
}
return TRUE;
}
/**
* g_usb_device_set_interface_alt:
* @device: a #GUsbDevice
* @interface: bInterfaceNumber of the interface you wish to release
* @alt: alternative setting number
* @error: a #GError, or %NULL
*
* Sets an alternate setting on an interface.
*
* Return value: %TRUE on success
*
* Since: 0.2.8
**/
gboolean
g_usb_device_set_interface_alt (GUsbDevice *device, gint interface,
guint8 alt, GError **error)
{
gint rc;
g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (device->priv->handle == NULL)
return g_usb_device_not_open_error (device, error);
rc = libusb_set_interface_alt_setting (device->priv->handle, interface, (gint) alt);
if (rc != LIBUSB_SUCCESS)
return g_usb_device_libusb_error_to_gerror (device, rc, error);
return TRUE;
}
/**
* g_usb_device_get_string_descriptor:
* @desc_index: the index for the string descriptor to retreive
* @error: a #GError, or %NULL
*
* Get a string descriptor from the device. The returned string should be freed
* with g_free() when no longer needed.
*
* Return value: a newly-allocated string holding the descriptor, or NULL on error.
*
* Since: 0.1.0
**/
gchar *
g_usb_device_get_string_descriptor (GUsbDevice *device,
guint8 desc_index,
GError **error)
{
gint rc;
/* libusb_get_string_descriptor_ascii returns max 128 bytes */
unsigned char buf[128];
g_return_val_if_fail (G_USB_IS_DEVICE (device), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
if (device->priv->handle == NULL) {
g_usb_device_not_open_error (device, error);
return NULL;
}
rc = libusb_get_string_descriptor_ascii (device->priv->handle,
desc_index, buf, sizeof(buf));
if (rc < 0) {
g_usb_device_libusb_error_to_gerror (device, rc, error);
return NULL;
}
return g_strdup ((const gchar *)buf);
}
typedef gssize (GUsbDeviceTransferFinishFunc) (GUsbDevice *device, GAsyncResult *res, GError **error);
typedef struct {
GError **error;
GMainContext *context;
GMainLoop *loop;
GUsbDeviceTransferFinishFunc *finish_func;
gssize ret;
} GUsbSyncHelper;
static void
g_usb_device_sync_transfer_cb (GUsbDevice *device,
GAsyncResult *res,
GUsbSyncHelper *helper)
{
helper->ret = (*helper->finish_func) (device, res, helper->error);
g_main_loop_quit (helper->loop);
}
/**
* g_usb_device_control_transfer:
* @device: a #GUsbDevice
* @request_type: the request type field for the setup packet
* @request: the request field for the setup packet
* @value: the value field for the setup packet
* @idx: the index field for the setup packet
* @data: (array length=length): a suitably-sized data buffer for
* either input or output
* @length: the length field for the setup packet.
* @actual_length: the actual number of bytes sent, or %NULL
* @timeout: timeout timeout (in millseconds) that this function should wait
* before giving up due to no response being received. For an unlimited
* timeout, use 0.
* @cancellable: a #GCancellable, or %NULL
* @error: a #GError, or %NULL
*
* Perform a USB control transfer.
*
* Warning: this function is synchronous, and cannot be cancelled.
*
* Return value: %TRUE on success
*
* Since: 0.1.0
**/
gboolean
g_usb_device_control_transfer (GUsbDevice *device,
GUsbDeviceDirection direction,
GUsbDeviceRequestType request_type,
GUsbDeviceRecipient recipient,
guint8 request,
guint16 value,
guint16 idx,
guint8 *data,
gsize length,
gsize *actual_length,
guint timeout,
GCancellable *cancellable,
GError **error)
{
GUsbSyncHelper helper;
helper.ret = -1;
helper.context = g_usb_context_get_main_context (device->priv->context);
helper.loop = g_main_loop_new (helper.context, FALSE);
helper.error = error;
helper.finish_func = g_usb_device_control_transfer_finish;
g_usb_device_control_transfer_async (device,
direction,
request_type,
recipient,
request,
value,
idx,
data,
length,
timeout,
cancellable,
(GAsyncReadyCallback) g_usb_device_sync_transfer_cb,
&helper);
g_main_loop_run (helper.loop);
g_main_loop_unref (helper.loop);
if (actual_length != NULL)
*actual_length = (gsize) helper.ret;
return helper.ret != -1;
}
/**
* g_usb_device_bulk_transfer:
* @device: a #GUsbDevice
* @endpoint: the address of a valid endpoint to communicate with
* @data: (array length=length): a suitably-sized data buffer for
* either input or output
* @length: the length field for the setup packet.
* @actual_length: the actual number of bytes sent, or %NULL
* @timeout: timeout timeout (in millseconds) that this function should wait
* before giving up due to no response being received. For an unlimited
* timeout, use 0.
* @cancellable: a #GCancellable, or %NULL
* @error: a #GError, or %NULL
*
* Perform a USB bulk transfer.
*
* Warning: this function is synchronous, and cannot be cancelled.
*
* Return value: %TRUE on success
*
* Since: 0.1.0
**/
gboolean
g_usb_device_bulk_transfer (GUsbDevice *device,
guint8 endpoint,
guint8 *data,
gsize length,
gsize *actual_length,
guint timeout,
GCancellable *cancellable,
GError **error)
{
GUsbSyncHelper helper;
helper.ret = -1;
helper.context = g_usb_context_get_main_context (device->priv->context);
helper.loop = g_main_loop_new (helper.context, FALSE);
helper.error = error;
helper.finish_func = g_usb_device_bulk_transfer_finish;
g_usb_device_bulk_transfer_async (device,
endpoint,
data,
length,
timeout,
cancellable,
(GAsyncReadyCallback) g_usb_device_sync_transfer_cb,
&helper);
g_main_loop_run (helper.loop);
g_main_loop_unref (helper.loop);
if (actual_length != NULL)
*actual_length = (gsize) helper.ret;
return helper.ret != -1;
}
/**
* g_usb_device_interrupt_transfer:
* @device: a #GUsbDevice
* @endpoint: the address of a valid endpoint to communicate with
* @data: (array length=length): a suitably-sized data buffer for
* either input or output
* @length: the length field for the setup packet.
* @actual_length: the actual number of bytes sent, or %NULL
* @timeout: timeout timeout (in millseconds) that this function should wait
* before giving up due to no response being received. For an unlimited
* timeout, use 0.
* @cancellable: a #GCancellable, or %NULL
* @error: a #GError, or %NULL
*
* Perform a USB interrupt transfer.
*
* Warning: this function is synchronous, and cannot be cancelled.
*
* Return value: %TRUE on success
*
* Since: 0.1.0
**/
gboolean
g_usb_device_interrupt_transfer (GUsbDevice *device,
guint8 endpoint,
guint8 *data,
gsize length,
gsize *actual_length,
guint timeout,
GCancellable *cancellable,
GError **error)
{
GUsbSyncHelper helper;
helper.ret = -1;
helper.context = g_usb_context_get_main_context (device->priv->context);
helper.loop = g_main_loop_new (helper.context, FALSE);
helper.error = error;
helper.finish_func = g_usb_device_interrupt_transfer_finish;
g_usb_device_interrupt_transfer_async (device,
endpoint,
data,
length,
timeout,
cancellable,
(GAsyncReadyCallback) g_usb_device_sync_transfer_cb,
&helper);
g_main_loop_run (helper.loop);
g_main_loop_unref (helper.loop);
if (actual_length != NULL)
*actual_length = helper.ret;
return helper.ret != -1;
}
typedef struct {
GCancellable *cancellable;
gulong cancellable_id;
struct libusb_transfer *transfer;
guint8 *data; /* owned by the user */
guint8 *data_raw; /* owned by the task */
} GcmDeviceReq;
static void
g_usb_device_req_free (GcmDeviceReq *req)
{
g_free (req->data_raw);
if (req->cancellable_id > 0) {
g_cancellable_disconnect (req->cancellable,
req->cancellable_id);
g_object_unref (req->cancellable);
}
libusb_free_transfer (req->transfer);
g_slice_free (GcmDeviceReq, req);
}
static gboolean
g_usb_device_libusb_status_to_gerror (gint status,
GError **error)
{
gboolean ret = FALSE;
switch (status) {
case LIBUSB_TRANSFER_COMPLETED:
ret = TRUE;
break;
case LIBUSB_TRANSFER_ERROR:
g_set_error_literal (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_FAILED,
"transfer failed");
break;
case LIBUSB_TRANSFER_TIMED_OUT:
g_set_error_literal (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_TIMED_OUT,
"transfer timed out");
break;
case LIBUSB_TRANSFER_CANCELLED:
g_set_error_literal (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_CANCELLED,
"transfer cancelled");
break;
case LIBUSB_TRANSFER_STALL:
g_set_error_literal (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_NOT_SUPPORTED,
"endpoint stalled or request not supported");
break;
case LIBUSB_TRANSFER_NO_DEVICE:
g_set_error_literal (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_NO_DEVICE,
"device was disconnected");
break;
case LIBUSB_TRANSFER_OVERFLOW:
g_set_error_literal (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_INTERNAL,
"device sent more data than requested");
break;
default:
g_set_error (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_INTERNAL,
"unknown status [%i]", status);
}
return ret;
}
static void
g_usb_device_async_transfer_cb (struct libusb_transfer *transfer)
{
GTask *task = transfer->user_data;
gboolean ret;
GError *error = NULL;
/* did request fail? */
ret = g_usb_device_libusb_status_to_gerror (transfer->status, &error);
if (!ret) {
g_task_return_error (task, error);
} else {
g_task_return_int (task, transfer->actual_length);
}
g_object_unref (task);
}
static void
g_usb_device_cancelled_cb (GCancellable *cancellable,
GcmDeviceReq *req)
{
libusb_cancel_transfer (req->transfer);
}
static void
g_usb_device_control_transfer_cb (struct libusb_transfer *transfer)
{
GTask *task = transfer->user_data;
GcmDeviceReq *req = g_task_get_task_data (task);
gboolean ret;
GError *error = NULL;
/* did request fail? */
ret = g_usb_device_libusb_status_to_gerror (transfer->status,
&error);
if (!ret) {
g_task_return_error (task, error);
} else {
memmove (req->data,
transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE,
(gsize) transfer->actual_length);
g_task_return_int (task, transfer->actual_length);
}
g_object_unref (task);
}
/**
* g_usb_device_control_transfer_async:
* @device: a #GUsbDevice
* @data: (array length=length): a suitably-sized data buffer for
* either input or output
* @length: the length field for the setup packet.
* @timeout: timeout timeout (in millseconds) that this function should wait
* before giving up due to no response being received. For an unlimited
* timeout, use 0.
* @cancellable: a #GCancellable, or %NULL
* @callback: the function to run on completion
* @user_data: the data to pass to @callback
*
* Do an async control transfer
*
* Since: 0.1.0
**/
void
g_usb_device_control_transfer_async (GUsbDevice *device,
GUsbDeviceDirection direction,
GUsbDeviceRequestType request_type,
GUsbDeviceRecipient recipient,
guint8 request,
guint16 value,
guint16 idx,
guint8 *data,
gsize length,
guint timeout,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
GcmDeviceReq *req;
gint rc;
guint8 request_type_raw = 0;
GError *error = NULL;
g_return_if_fail (G_USB_IS_DEVICE (device));
if (device->priv->handle == NULL) {
g_usb_device_async_not_open_error (device, callback, user_data,
g_usb_device_control_transfer_async);
return;
}
req = g_slice_new0 (GcmDeviceReq);
req->transfer = libusb_alloc_transfer (0);
req->data = data;
/* setup cancellation */
if (cancellable != NULL) {
req->cancellable = g_object_ref (cancellable);
req->cancellable_id = g_cancellable_connect (req->cancellable,
G_CALLBACK (g_usb_device_cancelled_cb),
req,
NULL);
}
task = g_task_new (device, cancellable, callback, user_data);
g_task_set_task_data (task, req, (GDestroyNotify)g_usb_device_req_free);
/* munge back to flags */
if (direction == G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST)
request_type_raw |= 0x80;
request_type_raw |= (request_type << 5);
request_type_raw |= recipient;
req->data_raw = g_malloc0 (length + LIBUSB_CONTROL_SETUP_SIZE);
memmove (req->data_raw + LIBUSB_CONTROL_SETUP_SIZE, data, length);
/* fill in setup packet */
libusb_fill_control_setup (req->data_raw, request_type_raw,
request, value, idx, length);
/* fill in transfer details */
libusb_fill_control_transfer (req->transfer,
device->priv->handle,
req->data_raw,
g_usb_device_control_transfer_cb,
task,
timeout);
/* submit transfer */
rc = libusb_submit_transfer (req->transfer);
if (rc < 0) {
g_usb_device_libusb_error_to_gerror (device, rc, &error);
g_task_return_error (task, error);
g_object_unref (task);
}
}
/**
* g_usb_device_control_transfer_finish:
* @device: a #GUsbDevice instance.
* @res: the #GAsyncResult
* @error: A #GError or %NULL
*
* Gets the result from the asynchronous function.
*
* Return value: the actual number of bytes sent, or -1 on error.
*
* Since: 0.1.0
**/
gssize
g_usb_device_control_transfer_finish (GUsbDevice *device,
GAsyncResult *res,
GError **error)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), -1);
g_return_val_if_fail (g_task_is_valid (res, device), -1);
g_return_val_if_fail (error == NULL || *error == NULL, -1);
return g_task_propagate_int (G_TASK (res), error);
}
/**
* g_usb_device_bulk_transfer_async:
* @device: a #GUsbDevice instance.
* @endpoint: the address of a valid endpoint to communicate with
* @data: (array length=length): a suitably-sized data buffer for
* either input or output
* @length: the length field for the setup packet.
* @timeout: timeout timeout (in millseconds) that this function should wait
* before giving up due to no response being received. For an unlimited
* timeout, use 0.
* @cancellable: a #GCancellable, or %NULL
* @callback: the function to run on completion
* @user_data: the data to pass to @callback
*
* Do an async bulk transfer
*
* Since: 0.1.0
**/
void
g_usb_device_bulk_transfer_async (GUsbDevice *device,
guint8 endpoint,
guint8 *data,
gsize length,
guint timeout,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
GcmDeviceReq *req;
gint rc;
GError *error = NULL;
g_return_if_fail (G_USB_IS_DEVICE (device));
if (device->priv->handle == NULL) {
g_usb_device_async_not_open_error (device, callback, user_data,
g_usb_device_bulk_transfer_async);
return;
}
req = g_slice_new0 (GcmDeviceReq);
req->transfer = libusb_alloc_transfer (0);
/* setup cancellation */
if (cancellable != NULL) {
req->cancellable = g_object_ref (cancellable);
req->cancellable_id = g_cancellable_connect (req->cancellable,
G_CALLBACK (g_usb_device_cancelled_cb),
req,
NULL);
}
task = g_task_new (device, cancellable, callback, user_data);
g_task_set_task_data (task, req, (GDestroyNotify)g_usb_device_req_free);
/* fill in transfer details */
libusb_fill_bulk_transfer (req->transfer,
device->priv->handle,
endpoint,
data,
length,
g_usb_device_async_transfer_cb,
task,
timeout);
/* submit transfer */
rc = libusb_submit_transfer (req->transfer);
if (rc < 0) {
g_usb_device_libusb_error_to_gerror (device, rc, &error);
g_task_return_error (task, error);
g_object_unref (task);
}
}
/**
* g_usb_device_bulk_transfer_finish:
* @device: a #GUsbDevice instance.
* @res: the #GAsyncResult
* @error: A #GError or %NULL
*
* Gets the result from the asynchronous function.
*
* Return value: the actual number of bytes sent, or -1 on error.
*
* Since: 0.1.0
**/
gssize
g_usb_device_bulk_transfer_finish (GUsbDevice *device,
GAsyncResult *res,
GError **error)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), -1);
g_return_val_if_fail (g_task_is_valid (res, device), -1);
g_return_val_if_fail (error == NULL || *error == NULL, -1);
return g_task_propagate_int (G_TASK (res), error);
}
/**
* g_usb_device_interrupt_transfer_async:
* @device: a #GUsbDevice instance.
* @endpoint: the address of a valid endpoint to communicate with
* @data: (array length=length): a suitably-sized data buffer for
* either input or output
* @length: the length field for the setup packet.
* @timeout: timeout timeout (in millseconds) that this function should wait
* before giving up due to no response being received. For an unlimited
* timeout, use 0.
* @cancellable: a #GCancellable, or %NULL
* @callback: the function to run on completion
* @user_data: the data to pass to @callback
*
* Do an async interrupt transfer
*
* Since: 0.1.0
**/
void
g_usb_device_interrupt_transfer_async (GUsbDevice *device,
guint8 endpoint,
guint8 *data,
gsize length,
guint timeout,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
GcmDeviceReq *req;
GError *error = NULL;
gint rc;
g_return_if_fail (G_USB_IS_DEVICE (device));
if (device->priv->handle == NULL) {
g_usb_device_async_not_open_error (device, callback, user_data,
g_usb_device_interrupt_transfer_async);
return;
}
req = g_slice_new0 (GcmDeviceReq);
req->transfer = libusb_alloc_transfer (0);
/* setup cancellation */
if (cancellable != NULL) {
req->cancellable = g_object_ref (cancellable);
req->cancellable_id = g_cancellable_connect (req->cancellable,
G_CALLBACK (g_usb_device_cancelled_cb),
req,
NULL);
}
task = g_task_new (device, cancellable, callback, user_data);
g_task_set_task_data (task, req, (GDestroyNotify)g_usb_device_req_free);
/* fill in transfer details */
libusb_fill_interrupt_transfer (req->transfer,
device->priv->handle,
endpoint,
data,
length,
g_usb_device_async_transfer_cb,
task,
timeout);
/* submit transfer */
rc = libusb_submit_transfer (req->transfer);
if (rc < 0) {
g_usb_device_libusb_error_to_gerror (device, rc, &error);
g_task_return_error (task, error);
g_object_unref (task);
}
}
/**
* g_usb_device_interrupt_transfer_finish:
* @device: a #GUsbDevice instance.
* @res: the #GAsyncResult
* @error: A #GError or %NULL
*
* Gets the result from the asynchronous function.
*
* Return value: the actual number of bytes sent, or -1 on error.
*
* Since: 0.1.0
**/
gssize
g_usb_device_interrupt_transfer_finish (GUsbDevice *device,
GAsyncResult *res,
GError **error)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), -1);
g_return_val_if_fail (g_task_is_valid (res, device), -1);
g_return_val_if_fail (error == NULL || *error == NULL, -1);
return g_task_propagate_int (G_TASK (res), error);
}
/**
* g_usb_device_get_platform_id:
* @device: a #GUsbDevice
*
* Gets the platform identifier for the device.
* On Linux, this is the full sysfs path of the device
*
* When the device is removed and then replugged, this value is not expected to
* be different.
*
* Return value: The platform ID, e.g. "usb:02:00:03:01"
*
* Since: 0.1.1
**/
const gchar *
g_usb_device_get_platform_id (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), NULL);
return device->priv->platform_id;
}
/**
* g_usb_device_get_parent:
* @device: a #GUsbDevice instance
*
* Gets the device parent if one exists.
*
* Return value: (transfer full): #GUsbDevice or %NULL
*
* Since: 0.2.4
**/
GUsbDevice *
g_usb_device_get_parent (GUsbDevice *device)
{
GUsbDevicePrivate *priv = device->priv;
libusb_device *parent;
parent = libusb_get_parent (priv->device);
if (parent == NULL)
return NULL;
return g_usb_context_find_by_bus_address (priv->context,
libusb_get_bus_number (parent),
libusb_get_device_address (parent),
NULL);
}
/**
* g_usb_device_get_children:
* @device: a #GUsbDevice instance
*
* Gets the device children if any exist.
*
* Return value: (transfer full) (element-type GUsbDevice): an array of #GUsbDevice
*
* Since: 0.2.4
**/
GPtrArray *
g_usb_device_get_children (GUsbDevice *device)
{
GPtrArray *children;
GUsbDevice *device_tmp;
GUsbDevicePrivate *priv = device->priv;
guint i;
GPtrArray *devices = NULL;
/* find any devices that have @device as a parent */
children = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
devices = g_usb_context_get_devices (priv->context);
for (i = 0; i < devices->len; i++) {
device_tmp = g_ptr_array_index (devices, i);
if (priv->device == libusb_get_parent (device_tmp->priv->device))
g_ptr_array_add (children, g_object_ref (device_tmp));
}
g_ptr_array_unref (devices);
return children;
}
/**
* g_usb_device_get_bus:
* @device: a #GUsbDevice
*
* Gets the USB bus number for the device.
*
* Return value: The 8-bit bus number
*
* Since: 0.1.0
**/
guint8
g_usb_device_get_bus (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), 0);
return libusb_get_bus_number (device->priv->device);
}
/**
* g_usb_device_get_address:
* @device: a #GUsbDevice
*
* Gets the USB address for the device.
*
* Return value: The 8-bit address
*
* Since: 0.1.0
**/
guint8
g_usb_device_get_address (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), 0);
return libusb_get_device_address (device->priv->device);
}
/**
* g_usb_device_get_port_number:
* @device: a #GUsbDevice
*
* Gets the USB port number for the device.
*
* Return value: The 8-bit port number
*
* Since: 0.2.4
**/
guint8
g_usb_device_get_port_number (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), 0);
return libusb_get_port_number (device->priv->device);
}
/**
* g_usb_device_get_vid:
* @device: a #GUsbDevice
*
* Gets the vendor ID for the device.
*
* Return value: an ID.
*
* Since: 0.1.0
**/
guint16
g_usb_device_get_vid (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), 0);
return device->priv->desc.idVendor;
}
/**
* g_usb_device_get_pid:
* @device: a #GUsbDevice
*
* Gets the product ID for the device.
*
* Return value: an ID.
*
* Since: 0.1.0
**/
guint16
g_usb_device_get_pid (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), 0);
return device->priv->desc.idProduct;
}
/**
* g_usb_device_get_release:
* @device: a #GUsbDevice
*
* Gets the BCD firmware version number for the device.
*
* Return value: a version number in BCD format.
*
* Since: 0.2.8
**/
guint16
g_usb_device_get_release (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), 0);
return device->priv->desc.bcdDevice;
}
/**
* g_usb_device_get_vid_as_str:
* @device: a #GUsbDevice
*
* Gets the vendor ID for the device as a string.
*
* Return value: an string ID, or %NULL if not available.
*
* Since: 0.2.4
**/
const gchar *
g_usb_device_get_vid_as_str (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), NULL);
return _g_usb_context_lookup_vendor (device->priv->context,
device->priv->desc.idVendor,
NULL);
}
/**
* g_usb_device_get_pid_as_str:
* @device: a #GUsbDevice
*
* Gets the product ID for the device as a string.
*
* Return value: an string ID, or %NULL if not available.
*
* Since: 0.2.4
**/
const gchar *
g_usb_device_get_pid_as_str (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), NULL);
return _g_usb_context_lookup_product (device->priv->context,
device->priv->desc.idVendor,
device->priv->desc.idProduct,
NULL);
}
/**
* g_usb_device_get_manufacturer_index:
* @device: a #GUsbDevice
*
* Gets the index for the Manufacturer string descriptor.
*
* Return value: a string descriptor index.
*
* Since: 0.1.0
**/
guint8
g_usb_device_get_manufacturer_index (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), 0);
return device->priv->desc.iManufacturer;
}
/**
* g_usb_device_get_device_class:
* @device: a #GUsbDevice
*
* Gets the device class, typically a #GUsbDeviceClassCode.
*
* Return value: a device class number, e.g. 0x09 is a USB hub.
*
* Since: 0.1.7
**/
guint8
g_usb_device_get_device_class (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), 0);
return device->priv->desc.bDeviceClass;
}
/**
* g_usb_device_get_device_subclass:
* @device: a #GUsbDevice
*
* Gets the device subclass qualified by the class number.
* See g_usb_device_get_device_class().
*
* Return value: a device subclass number.
*
* Since: 0.2.4
**/
guint8
g_usb_device_get_device_subclass (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), 0);
return device->priv->desc.bDeviceSubClass;
}
/**
* g_usb_device_get_device_protocol:
* @device: a #GUsbDevice
*
* Gets the device protocol qualified by the class and subclass numbers.
* See g_usb_device_get_device_class() and g_usb_device_get_device_subclass().
*
* Return value: a device protocol number.
*
* Since: 0.2.4
**/
guint8
g_usb_device_get_device_protocol (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), 0);
return device->priv->desc.bDeviceProtocol;
}
/**
* g_usb_device_get_product_index:
* @device: a #GUsbDevice
*
* Gets the index for the Product string descriptor.
*
* Return value: a string descriptor index.
*
* Since: 0.1.0
**/
guint8
g_usb_device_get_product_index (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), 0);
return device->priv->desc.iProduct;
}
/**
* g_usb_device_get_serial_number_index:
* @device: a #GUsbDevice
*
* Gets the index for the Serial Number string descriptor.
*
* Return value: a string descriptor index.
*
* Since: 0.1.0
**/
guint8
g_usb_device_get_serial_number_index (GUsbDevice *device)
{
g_return_val_if_fail (G_USB_IS_DEVICE (device), 0);
return device->priv->desc.iSerialNumber;
}
|