You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

153 lines
4.6 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2009 by Zachary T Welch <zw@superlucidity.net> *
  3. * *
  4. * Copyright (C) 2011 by Mauro Gamba <maurillo71@gmail.com> *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the *
  18. * Free Software Foundation, Inc., *
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  20. ***************************************************************************/
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include "log.h"
  25. #include "libusb1_common.h"
  26. static struct libusb_context *jtag_libusb_context; /**< Libusb context **/
  27. static libusb_device **devs; /**< The usb device list **/
  28. static bool jtag_libusb_match(struct jtag_libusb_device *dev,
  29. const uint16_t vids[], const uint16_t pids[])
  30. {
  31. struct libusb_device_descriptor dev_desc;
  32. for (unsigned i = 0; vids[i] && pids[i]; i++) {
  33. if (libusb_get_device_descriptor(dev, &dev_desc) == 0) {
  34. if (dev_desc.idVendor == vids[i] &&
  35. dev_desc.idProduct == pids[i])
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
  42. struct jtag_libusb_device_handle **out)
  43. {
  44. int cnt, idx, errCode;
  45. if (libusb_init(&jtag_libusb_context) < 0)
  46. return -ENODEV;
  47. libusb_set_debug(jtag_libusb_context, 3);
  48. cnt = libusb_get_device_list(jtag_libusb_context, &devs);
  49. for (idx = 0; idx < cnt; idx++) {
  50. if (!jtag_libusb_match(devs[idx], vids, pids))
  51. continue;
  52. errCode = libusb_open(devs[idx], out);
  53. /** Free the device list **/
  54. libusb_free_device_list(devs, 1);
  55. if (errCode < 0)
  56. return errCode;
  57. return 0;
  58. }
  59. return -ENODEV;
  60. }
  61. void jtag_libusb_close(jtag_libusb_device_handle *dev)
  62. {
  63. /* Close device */
  64. jtag_libusb_close(dev);
  65. libusb_exit(jtag_libusb_context);
  66. }
  67. int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes,
  68. int size, int timeout)
  69. {
  70. int transferred = 0;
  71. libusb_bulk_transfer(dev, ep, (unsigned char *)bytes, size,
  72. &transferred, timeout);
  73. return transferred;
  74. }
  75. int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes,
  76. int size, int timeout)
  77. {
  78. int transferred = 0;
  79. libusb_bulk_transfer(dev, ep, (unsigned char *)bytes, size,
  80. &transferred, timeout);
  81. return transferred;
  82. }
  83. int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
  84. int configuration)
  85. {
  86. struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
  87. int retCode = -99;
  88. struct libusb_config_descriptor *config;
  89. libusb_get_config_descriptor(udev, configuration, &config);
  90. retCode = libusb_set_configuration(devh, config->bConfigurationValue);
  91. libusb_free_config_descriptor(config);
  92. return retCode;
  93. }
  94. int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev,
  95. unsigned int *usb_read_ep,
  96. unsigned int *usb_write_ep)
  97. {
  98. const struct libusb_interface *inter;
  99. const struct libusb_interface_descriptor *interdesc;
  100. const struct libusb_endpoint_descriptor *epdesc;
  101. struct libusb_config_descriptor *config;
  102. libusb_get_config_descriptor(udev, 0, &config);
  103. for (int i = 0; i < (int)config->bNumInterfaces; i++) {
  104. inter = &config->interface[i];
  105. for (int j = 0; j < inter->num_altsetting; j++) {
  106. interdesc = &inter->altsetting[j];
  107. for (int k = 0;
  108. k < (int)interdesc->bNumEndpoints; k++) {
  109. epdesc = &interdesc->endpoint[k];
  110. uint8_t epnum = epdesc->bEndpointAddress;
  111. bool is_input = epnum & 0x80;
  112. LOG_DEBUG("usb ep %s %02x",
  113. is_input ? "in" : "out", epnum);
  114. if (is_input)
  115. *usb_read_ep = epnum;
  116. else
  117. *usb_write_ep = epnum;
  118. }
  119. }
  120. }
  121. libusb_free_config_descriptor(config);
  122. return 0;
  123. }