| OLD | NEW |
| 1 /* | 1 /* |
| 2 * \file libusb1-glue.c | 2 * \file libusb1-glue.c |
| 3 * Low-level USB interface glue towards libusb. | 3 * Low-level USB interface glue towards libusb. |
| 4 * | 4 * |
| 5 * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com> | 5 * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com> |
| 6 * Copyright (C) 2005-2012 Linus Walleij <triad@df.lth.se> | 6 * Copyright (C) 2005-2012 Linus Walleij <triad@df.lth.se> |
| 7 * Copyright (C) 2006-2012 Marcus Meissner | 7 * Copyright (C) 2006-2012 Marcus Meissner |
| 8 * Copyright (C) 2007 Ted Bullock | 8 * Copyright (C) 2007 Ted Bullock |
| 9 * Copyright (C) 2008 Chris Bagwell <chris@cnpbagwell.com> | 9 * Copyright (C) 2008 Chris Bagwell <chris@cnpbagwell.com> |
| 10 * | 10 * |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 static const LIBMTP_device_entry_t mtp_device_table[] = { | 80 static const LIBMTP_device_entry_t mtp_device_table[] = { |
| 81 /* We include an .h file which is shared between us and libgphoto2 */ | 81 /* We include an .h file which is shared between us and libgphoto2 */ |
| 82 #include "music-players.h" | 82 #include "music-players.h" |
| 83 }; | 83 }; |
| 84 static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMT
P_device_entry_t); | 84 static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMT
P_device_entry_t); |
| 85 | 85 |
| 86 // Local functions | 86 // Local functions |
| 87 static LIBMTP_error_number_t init_usb(); | 87 static LIBMTP_error_number_t init_usb(); |
| 88 static void close_usb(PTP_USB* ptp_usb); | 88 static void close_usb(PTP_USB* ptp_usb); |
| 89 static int find_interface_and_endpoints(libusb_device *dev, | 89 static int find_interface_and_endpoints(libusb_device *dev, |
| 90 uint8_t *conf, |
| 90 uint8_t *interface, | 91 uint8_t *interface, |
| 92 uint8_t *altsetting, |
| 91 int* inep, | 93 int* inep, |
| 92 int* inep_maxpacket, | 94 int* inep_maxpacket, |
| 93 int* outep, | 95 int* outep, |
| 94 int* outep_maxpacket, | 96 int* outep_maxpacket, |
| 95 int* intep); | 97 int* intep); |
| 96 static void clear_stall(PTP_USB* ptp_usb); | 98 static void clear_stall(PTP_USB* ptp_usb); |
| 97 static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, libusb_device* dev
); | 99 static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, libusb_device* dev
); |
| 98 static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned l
ong*); | 100 static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned l
ong*); |
| 99 static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned lo
ng*,int); | 101 static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned lo
ng*,int); |
| 100 static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status); | 102 static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status); |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 } | 292 } |
| 291 | 293 |
| 292 /* | 294 /* |
| 293 * Check for Still Image Capture class with PIMA 15740 protocol, | 295 * Check for Still Image Capture class with PIMA 15740 protocol, |
| 294 * also known as PTP | 296 * also known as PTP |
| 295 */ | 297 */ |
| 296 if (intf->bInterfaceClass == LIBUSB_CLASS_PTP | 298 if (intf->bInterfaceClass == LIBUSB_CLASS_PTP |
| 297 && intf->bInterfaceSubClass == 0x01 | 299 && intf->bInterfaceSubClass == 0x01 |
| 298 && intf->bInterfaceProtocol == 0x01) { | 300 && intf->bInterfaceProtocol == 0x01) { |
| 299 if (dumpfile != NULL) { | 301 if (dumpfile != NULL) { |
| 302 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\
n", i, j, k); |
| 300 fprintf(dumpfile, " Found PTP device, check vendor " | 303 fprintf(dumpfile, " Found PTP device, check vendor " |
| 301 "extension...\n"); | 304 "extension...\n"); |
| 302 } | 305 } |
| 303 libusb_close(devh); | 306 libusb_close(devh); |
| 304 return 1; | 307 return 1; |
| 305 } | 308 } |
| 306 | 309 |
| 307 /* | 310 /* |
| 308 * Next we search for the MTP substring in the interface name. | 311 * Next we search for the MTP substring in the interface name. |
| 309 * For example : "RIM MS/MTP" should work. | 312 * For example : "RIM MS/MTP" should work. |
| 310 */ | 313 */ |
| 311 buf[0] = '\0'; | 314 buf[0] = '\0'; |
| 312 ret = libusb_get_string_descriptor_ascii(devh, | 315 ret = libusb_get_string_descriptor_ascii(devh, |
| 313 config->interface[j].altsetting[k].iInterf
ace, | 316 config->interface[j].altsetting[k].iInterf
ace, |
| 314 buf, | 317 buf, |
| 315 1024); | 318 1024); |
| 316 if (ret < 3) | 319 if (ret < 3) |
| 317 continue; | 320 continue; |
| 318 if (strstr((char *) buf, "MTP") != NULL) { | 321 if (strstr((char *) buf, "MTP") != NULL) { |
| 319 if (dumpfile != NULL) { | 322 if (dumpfile != NULL) { |
| 320 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\
n", i, j, k); | 323 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\
n", i, j, k); |
| 321 fprintf(dumpfile, " Interface description contains the string \"
MTP\"\n"); | 324 fprintf(dumpfile, " Interface description contains the string \"
MTP\"\n"); |
| 322 fprintf(dumpfile, " Device recognized as MTP, no further probing
.\n"); | 325 fprintf(dumpfile, " Device recognized as MTP, no further probing
.\n"); |
| 323 } | 326 } |
| 324 libusb_free_config_descriptor (config); | 327 libusb_free_config_descriptor(config); |
| 325 libusb_close(devh); | 328 libusb_close(devh); |
| 326 return 1; | 329 return 1; |
| 327 } | 330 } |
| 328 if (libusb_kernel_driver_active(devh, config->interface[j].altsetting[
k].iInterface)) | 331 if (libusb_kernel_driver_active(devh, config->interface[j].altsetting[
k].iInterface)) |
| 329 { | 332 { |
| 330 /* | 333 /* |
| 331 * Specifically avoid probing anything else than USB mass storage de
vices | 334 * Specifically avoid probing anything else than USB mass storage de
vices |
| 332 * and non-associated drivers in Linux. | 335 * and non-associated drivers in Linux. |
| 333 */ | 336 */ |
| 334 if (config->interface[j].altsetting[k].bInterfaceClass != | 337 if (config->interface[j].altsetting[k].bInterfaceClass != |
| 335 LIBUSB_CLASS_MASS_STORAGE) { | 338 LIBUSB_CLASS_MASS_STORAGE) { |
| 336 LIBMTP_INFO("avoid probing device using attached kernel interface\
n"); | 339 LIBMTP_INFO("avoid probing device using attached kernel interface\
n"); |
| 337 libusb_free_config_descriptor (config); | 340 libusb_free_config_descriptor(config); |
| 338 return 0; | 341 return 0; |
| 339 } | 342 } |
| 340 } | 343 } |
| 341 } | 344 } |
| 342 } | 345 } |
| 343 } | 346 libusb_free_config_descriptor(config); |
| 347 } |
| 344 | 348 |
| 345 /* | 349 /* |
| 346 * Only probe for OS descriptor if the device is vendor specific | 350 * Only probe for OS descriptor if the device is vendor specific |
| 347 * or one of the interfaces found is. | 351 * or one of the interfaces found is. |
| 348 */ | 352 */ |
| 349 if (desc.bDeviceClass == LIBUSB_CLASS_VENDOR_SPEC || | 353 if (desc.bDeviceClass == LIBUSB_CLASS_VENDOR_SPEC || |
| 350 found_vendor_spec_interface) { | 354 found_vendor_spec_interface) { |
| 351 | 355 |
| 352 /* Read the special descriptor */ | 356 /* Read the special descriptor */ |
| 353 ret = libusb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf)); | 357 ret = libusb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf)); |
| (...skipping 1262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1616 LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTER
FACE, | 1620 LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTER
FACE, |
| 1617 0x64, 0x0000, 0x0000, | 1621 0x64, 0x0000, 0x0000, |
| 1618 buffer, | 1622 buffer, |
| 1619 sizeof(buffer), | 1623 sizeof(buffer), |
| 1620 ptp_usb->timeout); | 1624 ptp_usb->timeout); |
| 1621 if (ret < sizeof(buffer)) | 1625 if (ret < sizeof(buffer)) |
| 1622 return PTP_ERROR_IO; | 1626 return PTP_ERROR_IO; |
| 1623 return PTP_RC_OK; | 1627 return PTP_RC_OK; |
| 1624 } | 1628 } |
| 1625 | 1629 |
| 1626 static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, libusb_device* dev
) | 1630 static int init_ptp_usb(PTPParams* params, PTP_USB* ptp_usb, libusb_device* dev) |
| 1627 { | 1631 { |
| 1628 libusb_device_handle *device_handle; | 1632 libusb_device_handle *device_handle; |
| 1629 unsigned char buf[255]; | 1633 unsigned char buf[255]; |
| 1630 int ret, usbresult; | 1634 int ret, usbresult; |
| 1635 struct libusb_config_descriptor *config; |
| 1631 | 1636 |
| 1632 params->sendreq_func=ptp_usb_sendreq; | 1637 params->sendreq_func=ptp_usb_sendreq; |
| 1633 params->senddata_func=ptp_usb_senddata; | 1638 params->senddata_func=ptp_usb_senddata; |
| 1634 params->getresp_func=ptp_usb_getresp; | 1639 params->getresp_func=ptp_usb_getresp; |
| 1635 params->getdata_func=ptp_usb_getdata; | 1640 params->getdata_func=ptp_usb_getdata; |
| 1636 params->cancelreq_func=ptp_usb_control_cancel_request; | 1641 params->cancelreq_func=ptp_usb_control_cancel_request; |
| 1637 params->data=ptp_usb; | 1642 params->data=ptp_usb; |
| 1638 params->transaction_id=0; | 1643 params->transaction_id=0; |
| 1639 /* | 1644 /* |
| 1640 * This is hardcoded here since we have no devices whatsoever that are BE. | 1645 * This is hardcoded here since we have no devices whatsoever that are BE. |
| 1641 * Change this the day we run into our first BE device (if ever). | 1646 * Change this the day we run into our first BE device (if ever). |
| 1642 */ | 1647 */ |
| 1643 params->byteorder = PTP_DL_LE; | 1648 params->byteorder = PTP_DL_LE; |
| 1644 | 1649 |
| 1645 ptp_usb->timeout = get_timeout(ptp_usb); | 1650 ptp_usb->timeout = get_timeout(ptp_usb); |
| 1646 | 1651 |
| 1647 ret = libusb_open(dev, &device_handle); | 1652 ret = libusb_open(dev, &device_handle); |
| 1648 if (ret != LIBUSB_SUCCESS) { | 1653 if (ret != LIBUSB_SUCCESS) { |
| 1649 perror("usb_open()"); | 1654 perror("libusb_open() failed!"); |
| 1650 return -1; | 1655 return -1; |
| 1651 } | 1656 } |
| 1652 ptp_usb->handle = device_handle; | 1657 ptp_usb->handle = device_handle; |
| 1658 |
| 1653 /* | 1659 /* |
| 1654 * If this device is known to be wrongfully claimed by other kernel | 1660 * If this device is known to be wrongfully claimed by other kernel |
| 1655 * drivers (such as mass storage), then try to unload it to make it | 1661 * drivers (such as mass storage), then try to unload it to make it |
| 1656 * accessible from user space. | 1662 * accessible from user space. |
| 1657 */ | 1663 */ |
| 1658 if (FLAG_UNLOAD_DRIVER(ptp_usb) && | 1664 if (FLAG_UNLOAD_DRIVER(ptp_usb) && |
| 1659 libusb_kernel_driver_active (device_handle, ptp_usb->interface) | 1665 libusb_kernel_driver_active(device_handle, ptp_usb->interface) |
| 1660 ) { | 1666 ) { |
| 1661 if (LIBUSB_SUCCESS != libusb_detach_kernel_driver (device_handle, ptp_usb-
>interface)) { | 1667 if (LIBUSB_SUCCESS != libusb_detach_kernel_driver(device_handle, ptp_usb->
interface)) { |
| 1662 return -1; | 1668 » perror("libusb_detach_kernel_driver() failed, continuing anyway..."); |
| 1663 } | 1669 } |
| 1664 } | 1670 } |
| 1665 #ifdef __WIN32__ | 1671 |
| 1666 // Only needed on Windows, and cause problems on other platforms. | 1672 /* |
| 1667 if (libusb_set_configuration(device_handle, dev->config->bConfigurationValue))
{ | 1673 * Check if the config is set to something else than what we want |
| 1668 perror("usb_set_configuration()"); | 1674 * to use. Only set the configuration if we absolutely have to. |
| 1675 * Also do not bail out if we fail. |
| 1676 */ |
| 1677 ret = libusb_get_active_config_descriptor(dev, &config); |
| 1678 if (ret != LIBUSB_SUCCESS) { |
| 1679 perror("libusb_get_active_config_descriptor(1) failed"); |
| 1669 return -1; | 1680 return -1; |
| 1670 } | 1681 } |
| 1671 #endif | 1682 if (config->bConfigurationValue != ptp_usb->config) { |
| 1672 // It seems like on kernel 2.6.31 if we already have it open on another | 1683 fprintf(stderr, "desired configuration different from current, trying to set
configuration\n"); |
| 1673 // pthread in our app, we'll get an error if we try to claim it again, | 1684 if (libusb_set_configuration(device_handle, ptp_usb->config)) { |
| 1674 // but that error is harmless because our process already claimed the interfac
e | 1685 perror("libusb_set_configuration() failed, continuing anyway..."); |
| 1686 } |
| 1687 /* Re-fetch the config descriptor if we changed */ |
| 1688 libusb_free_config_descriptor(config); |
| 1689 ret = libusb_get_active_config_descriptor(dev, &config); |
| 1690 if (ret != LIBUSB_SUCCESS) { |
| 1691 perror("libusb_get_active_config_descriptor(2) failed"); |
| 1692 return -1; |
| 1693 } |
| 1694 } |
| 1695 |
| 1696 /* |
| 1697 * It seems like on kernel 2.6.31 if we already have it open on another |
| 1698 * pthread in our app, we'll get an error if we try to claim it again, |
| 1699 * but that error is harmless because our process already claimed the interfac
e |
| 1700 */ |
| 1675 usbresult = libusb_claim_interface(device_handle, ptp_usb->interface); | 1701 usbresult = libusb_claim_interface(device_handle, ptp_usb->interface); |
| 1676 | 1702 |
| 1677 if (usbresult != 0) | 1703 if (usbresult != 0) |
| 1678 fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult); | 1704 fprintf(stderr, "ignoring libusb_claim_interface() = %d", usbresult); |
| 1679 | 1705 |
| 1680 // FIXME : Discovered in the Barry project | 1706 /* |
| 1681 // kernels >= 2.6.28 don't set the interface the same way as | 1707 * If the altsetting is set to something different than we want, switch |
| 1682 // previous versions did, and the Blackberry gets confused | 1708 * it. |
| 1683 // if it isn't explicitly set | 1709 * |
| 1684 // See above, same issue with pthreads means that if this fails it is not | 1710 * FIXME: this seems to cause trouble on the Mac:s so disable it. Retry |
| 1685 // fatal | 1711 * this on the Mac now that it only sets this when the altsetting differs. |
| 1686 // However, this causes problems on Macs so disable here | 1712 */ |
| 1687 #ifndef __APPLE__ | 1713 #ifndef __APPLE__ |
| 1688 usbresult = libusb_set_interface_alt_setting(device_handle, ptp_usb->interface
, 0); | 1714 #if 0 /* Disable this always, no idea on how to handle it */ |
| 1689 if (usbresult) | 1715 if (config->interface[].altsetting[].bAlternateSetting != |
| 1690 fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult); | 1716 ptp_usb->altsetting) { |
| 1717 fprintf(stderr, "desired altsetting different from current, trying to set al
tsetting\n"); |
| 1718 usbresult = libusb_set_interface_alt_setting(device_handle, |
| 1719 » » » » » » ptp_usb->interface, |
| 1720 » » » » » » ptp_usb->altsetting); |
| 1721 if (usbresult != 0) |
| 1722 fprintf(stderr, "ignoring libusb_set_interface_alt_setting() = %d\n", usbr
esult); |
| 1723 } |
| 1691 #endif | 1724 #endif |
| 1725 #endif |
| 1726 |
| 1727 libusb_free_config_descriptor(config); |
| 1692 | 1728 |
| 1693 if (FLAG_SWITCH_MODE_BLACKBERRY(ptp_usb)) { | 1729 if (FLAG_SWITCH_MODE_BLACKBERRY(ptp_usb)) { |
| 1694 int ret; | 1730 int ret; |
| 1695 | 1731 |
| 1696 // FIXME : Only for BlackBerry Storm | 1732 // FIXME : Only for BlackBerry Storm |
| 1697 // What does it mean? Maybe switch mode... | 1733 // What does it mean? Maybe switch mode... |
| 1698 // This first control message is absolutely necessary | 1734 // This first control message is absolutely necessary |
| 1699 usleep(1000); | 1735 usleep(1000); |
| 1700 ret = libusb_control_transfer(device_handle, | 1736 ret = libusb_control_transfer(device_handle, |
| 1701 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE |
LIBUSB_ENDPOINT_IN, | 1737 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE |
LIBUSB_ENDPOINT_IN, |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1816 */ | 1852 */ |
| 1817 libusb_reset_device (ptp_usb->handle); | 1853 libusb_reset_device (ptp_usb->handle); |
| 1818 } | 1854 } |
| 1819 libusb_close(ptp_usb->handle); | 1855 libusb_close(ptp_usb->handle); |
| 1820 } | 1856 } |
| 1821 | 1857 |
| 1822 /** | 1858 /** |
| 1823 * Self-explanatory? | 1859 * Self-explanatory? |
| 1824 */ | 1860 */ |
| 1825 static int find_interface_and_endpoints(libusb_device *dev, | 1861 static int find_interface_and_endpoints(libusb_device *dev, |
| 1862 uint8_t *conf, |
| 1826 uint8_t *interface, | 1863 uint8_t *interface, |
| 1864 uint8_t *altsetting, |
| 1827 int* inep, | 1865 int* inep, |
| 1828 int* inep_maxpacket, | 1866 int* inep_maxpacket, |
| 1829 int* outep, | 1867 int* outep, |
| 1830 int *outep_maxpacket, | 1868 int *outep_maxpacket, |
| 1831 int* intep) | 1869 int* intep) |
| 1832 { | 1870 { |
| 1833 int i, ret; | 1871 uint8_t i, ret; |
| 1834 struct libusb_device_descriptor desc; | 1872 struct libusb_device_descriptor desc; |
| 1835 | 1873 |
| 1836 ret = libusb_get_device_descriptor (dev, &desc); | 1874 ret = libusb_get_device_descriptor(dev, &desc); |
| 1837 if (ret != LIBUSB_SUCCESS) return -1; | 1875 if (ret != LIBUSB_SUCCESS) |
| 1876 return -1; |
| 1838 | 1877 |
| 1839 // Loop over the device configurations | 1878 // Loop over the device configurations |
| 1840 for (i = 0; i < desc.bNumConfigurations; i++) { | 1879 for (i = 0; i < desc.bNumConfigurations; i++) { |
| 1841 uint8_t j; | 1880 uint8_t j; |
| 1842 struct libusb_config_descriptor *config; | 1881 struct libusb_config_descriptor *config; |
| 1843 | 1882 |
| 1844 ret = libusb_get_config_descriptor (dev, i, &config); | 1883 ret = libusb_get_config_descriptor(dev, i, &config); |
| 1884 if (ret != 0) |
| 1885 continue; |
| 1886 |
| 1887 *conf = config->bConfigurationValue; |
| 1888 |
| 1845 if (ret != LIBUSB_SUCCESS) continue; | 1889 if (ret != LIBUSB_SUCCESS) continue; |
| 1846 // Loop over each configurations interfaces | 1890 // Loop over each configurations interfaces |
| 1847 for (j = 0; j < config->bNumInterfaces; j++) { | 1891 for (j = 0; j < config->bNumInterfaces; j++) { |
| 1848 uint8_t k; | 1892 uint8_t k, l; |
| 1849 uint8_t no_ep; | 1893 uint8_t no_ep; |
| 1850 int found_inep = 0; | 1894 int found_inep = 0; |
| 1851 int found_outep = 0; | 1895 int found_outep = 0; |
| 1852 int found_intep = 0; | 1896 int found_intep = 0; |
| 1853 const struct libusb_endpoint_descriptor *ep; | 1897 const struct libusb_endpoint_descriptor *ep; |
| 1854 | 1898 |
| 1855 // MTP devices shall have 3 endpoints, ignore those interfaces | 1899 // Inspect the altsettings of this interface... |
| 1856 // that haven't. | 1900 for (k = 0; k < config->interface[j].num_altsetting; k++) { |
| 1857 no_ep = config->interface[j].altsetting->bNumEndpoints; | |
| 1858 if (no_ep != 3) | |
| 1859 » continue; | |
| 1860 | 1901 |
| 1861 *interface = config->interface[j].altsetting->bInterfaceNumber; | 1902 » // MTP devices shall have 3 endpoints, ignore those interfaces |
| 1862 ep = config->interface[j].altsetting->endpoint; | 1903 » // that haven't. |
| 1904 » no_ep = config->interface[j].altsetting[k].bNumEndpoints; |
| 1905 » if (no_ep != 3) |
| 1906 » continue; |
| 1863 | 1907 |
| 1864 // Loop over the three endpoints to locate two bulk and | 1908 » *interface = config->interface[j].altsetting[k].bInterfaceNumber; |
| 1865 // one interrupt endpoint and FAIL if we cannot, and continue. | 1909 » *altsetting = config->interface[j].altsetting[k].bAlternateSetting; |
| 1866 for (k = 0; k < no_ep; k++) { | 1910 » ep = config->interface[j].altsetting[k].endpoint; |
| 1867 » if (ep[k].bmAttributes == LIBUSB_TRANSFER_TYPE_BULK) { | 1911 |
| 1868 » if ((ep[k].bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == | 1912 » // Loop over the three endpoints to locate two bulk and |
| 1869 » LIBUSB_ENDPOINT_DIR_MASK) { | 1913 » // one interrupt endpoint and FAIL if we cannot, and continue. |
| 1870 » *inep = ep[k].bEndpointAddress; | 1914 » for (l = 0; l < no_ep; l++) { |
| 1871 » *inep_maxpacket = ep[k].wMaxPacketSize; | 1915 » if (ep[l].bmAttributes == LIBUSB_TRANSFER_TYPE_BULK) { |
| 1872 » found_inep = 1; | 1916 » if ((ep[l].bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == |
| 1873 » } | 1917 » » LIBUSB_ENDPOINT_DIR_MASK) { |
| 1874 » if ((ep[k].bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == 0) { | 1918 » *inep = ep[l].bEndpointAddress; |
| 1875 » *outep = ep[k].bEndpointAddress; | 1919 » *inep_maxpacket = ep[l].wMaxPacketSize; |
| 1876 » *outep_maxpacket = ep[k].wMaxPacketSize; | 1920 » found_inep = 1; |
| 1877 » found_outep = 1; | 1921 » } |
| 1878 » } | 1922 » if ((ep[l].bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == 0) { |
| 1879 » } else if (ep[k].bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT) { | 1923 » *outep = ep[l].bEndpointAddress; |
| 1880 » if ((ep[k].bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == | 1924 » *outep_maxpacket = ep[l].wMaxPacketSize; |
| 1881 » LIBUSB_ENDPOINT_DIR_MASK) { | 1925 » found_outep = 1; |
| 1882 » *intep = ep[k].bEndpointAddress; | 1926 » } |
| 1883 » found_intep = 1; | 1927 » } else if (ep[l].bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT) { |
| 1928 » if ((ep[l].bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == |
| 1929 » » LIBUSB_ENDPOINT_DIR_MASK) { |
| 1930 » *intep = ep[l].bEndpointAddress; |
| 1931 » found_intep = 1; |
| 1932 » } |
| 1884 } | 1933 } |
| 1885 } | 1934 } |
| 1886 } | 1935 » if (found_inep && found_outep && found_intep) { |
| 1887 if (found_inep && found_outep && found_intep) { | 1936 » libusb_free_config_descriptor(config); |
| 1888 libusb_free_config_descriptor (config); | 1937 » // We assigned the endpoints so return here. |
| 1889 » // We assigned the endpoints so return here. | 1938 » return 0; |
| 1890 » return 0; | 1939 » } |
| 1891 } | 1940 } // Next altsetting |
| 1892 // Else loop to next interface/config | 1941 } // Next interface |
| 1893 } | 1942 libusb_free_config_descriptor(config); |
| 1894 libusb_free_config_descriptor (config); | 1943 } // Next config |
| 1895 } | |
| 1896 return -1; | 1944 return -1; |
| 1897 } | 1945 } |
| 1898 | 1946 |
| 1899 /** | 1947 /** |
| 1900 * This function assigns params and usbinfo given a raw device | 1948 * This function assigns params and usbinfo given a raw device |
| 1901 * as input. | 1949 * as input. |
| 1902 * @param device the device to be assigned. | 1950 * @param device the device to be assigned. |
| 1903 * @param usbinfo a pointer to the new usbinfo. | 1951 * @param usbinfo a pointer to the new usbinfo. |
| 1904 * @return an error code. | 1952 * @return an error code. |
| 1905 */ | 1953 */ |
| 1906 LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device, | 1954 LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device, |
| 1907 PTPParams *params, | 1955 PTPParams *params, |
| 1908 void **usbinfo) | 1956 void **usbinfo) |
| 1909 { | 1957 { |
| 1910 PTP_USB *ptp_usb; | 1958 PTP_USB *ptp_usb; |
| 1911 libusb_device *ldevice; | 1959 libusb_device *ldevice; |
| 1912 uint16_t ret = 0; | 1960 uint16_t ret = 0; |
| 1913 int err, found = 0, i; | 1961 int err, found = 0, i; |
| 1914 ssize_t nrofdevs; | 1962 ssize_t nrofdevs; |
| 1915 libusb_device **devs = NULL; | 1963 libusb_device **devs = NULL; |
| 1916 struct libusb_device_descriptor desc; | 1964 struct libusb_device_descriptor desc; |
| 1917 LIBMTP_error_number_t init_usb_ret; | 1965 LIBMTP_error_number_t init_usb_ret; |
| 1918 | 1966 |
| 1919 /* See if we can find this raw device again... */ | 1967 /* See if we can find this raw device again... */ |
| 1920 init_usb_ret = init_usb(); | 1968 init_usb_ret = init_usb(); |
| 1921 if (init_usb_ret != LIBMTP_ERROR_NONE) | 1969 if (init_usb_ret != LIBMTP_ERROR_NONE) |
| 1922 return init_usb_ret; | 1970 return init_usb_ret; |
| 1923 | 1971 |
| 1924 nrofdevs = libusb_get_device_list (NULL, &devs); | 1972 nrofdevs = libusb_get_device_list(NULL, &devs); |
| 1925 for (i = 0; i < nrofdevs ; i++) { | 1973 for (i = 0; i < nrofdevs ; i++) { |
| 1926 if (libusb_get_bus_number (devs[i]) != device->bus_location) | 1974 if (libusb_get_bus_number(devs[i]) != device->bus_location) |
| 1927 continue; | 1975 continue; |
| 1928 if (libusb_get_device_address (devs[i]) != device->devnum) | 1976 if (libusb_get_device_address(devs[i]) != device->devnum) |
| 1929 continue; | 1977 continue; |
| 1930 | 1978 |
| 1931 ret = libusb_get_device_descriptor (devs[i], &desc); | 1979 ret = libusb_get_device_descriptor(devs[i], &desc); |
| 1932 if (ret != LIBUSB_SUCCESS) continue; | 1980 if (ret != LIBUSB_SUCCESS) continue; |
| 1933 | 1981 |
| 1934 if(desc.idVendor == device->device_entry.vendor_id && | 1982 if(desc.idVendor == device->device_entry.vendor_id && |
| 1935 desc.idProduct == device->device_entry.product_id ) { | 1983 desc.idProduct == device->device_entry.product_id ) { |
| 1936 ldevice = devs[i]; | 1984 ldevice = devs[i]; |
| 1937 found = 1; | 1985 found = 1; |
| 1938 break; | 1986 break; |
| 1939 } | 1987 } |
| 1940 } | 1988 } |
| 1941 /* Device has gone since detecting raw devices! */ | 1989 /* Device has gone since detecting raw devices! */ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1960 * Some devices must have their "OS Descriptor" massaged in order | 2008 * Some devices must have their "OS Descriptor" massaged in order |
| 1961 * to work. | 2009 * to work. |
| 1962 */ | 2010 */ |
| 1963 if (FLAG_ALWAYS_PROBE_DESCRIPTOR(ptp_usb)) { | 2011 if (FLAG_ALWAYS_PROBE_DESCRIPTOR(ptp_usb)) { |
| 1964 // Massage the device descriptor | 2012 // Massage the device descriptor |
| 1965 (void) probe_device_descriptor(ldevice, NULL); | 2013 (void) probe_device_descriptor(ldevice, NULL); |
| 1966 } | 2014 } |
| 1967 | 2015 |
| 1968 /* Assign interface and endpoints to usbinfo... */ | 2016 /* Assign interface and endpoints to usbinfo... */ |
| 1969 err = find_interface_and_endpoints(ldevice, | 2017 err = find_interface_and_endpoints(ldevice, |
| 2018 &ptp_usb->config, |
| 1970 &ptp_usb->interface, | 2019 &ptp_usb->interface, |
| 2020 &ptp_usb->altsetting, |
| 1971 &ptp_usb->inep, | 2021 &ptp_usb->inep, |
| 1972 &ptp_usb->inep_maxpacket, | 2022 &ptp_usb->inep_maxpacket, |
| 1973 &ptp_usb->outep, | 2023 &ptp_usb->outep, |
| 1974 &ptp_usb->outep_maxpacket, | 2024 &ptp_usb->outep_maxpacket, |
| 1975 &ptp_usb->intep); | 2025 &ptp_usb->intep); |
| 1976 | 2026 |
| 1977 if (err) { | 2027 if (err) { |
| 1978 libusb_free_device_list (devs, 0); | 2028 libusb_free_device_list (devs, 0); |
| 1979 LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n
"); | 2029 LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n
"); |
| 1980 return LIBMTP_ERROR_CONNECTING; | 2030 return LIBMTP_ERROR_CONNECTING; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2082 { | 2132 { |
| 2083 return libusb_control_transfer(ptp_usb->handle, | 2133 return libusb_control_transfer(ptp_usb->handle, |
| 2084 LIBUSB_ENDPOINT_IN|LIBUSB_RECIPIENT_ENDPOINT, | 2134 LIBUSB_ENDPOINT_IN|LIBUSB_RECIPIENT_ENDPOINT, |
| 2085 LIBUSB_REQUEST_GET_STATUS, | 2135 LIBUSB_REQUEST_GET_STATUS, |
| 2086 USB_FEATURE_HALT, | 2136 USB_FEATURE_HALT, |
| 2087 ep, | 2137 ep, |
| 2088 (unsigned char *) status, | 2138 (unsigned char *) status, |
| 2089 2, | 2139 2, |
| 2090 ptp_usb->timeout); | 2140 ptp_usb->timeout); |
| 2091 } | 2141 } |
| OLD | NEW |