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.

libusb0_common.c 3.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "libusb0_common.h"
  26. static bool jtag_libusb_match(struct jtag_libusb_device *dev,
  27. const uint16_t vids[], const uint16_t pids[])
  28. {
  29. for (unsigned i = 0; vids[i] && pids[i]; i++) {
  30. if (dev->descriptor.idVendor == vids[i] &&
  31. dev->descriptor.idProduct == pids[i]) {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
  38. struct jtag_libusb_device_handle **out)
  39. {
  40. usb_init();
  41. usb_find_busses();
  42. usb_find_devices();
  43. struct usb_bus *busses = usb_get_busses();
  44. for (struct usb_bus *bus = busses; bus; bus = bus->next) {
  45. for (struct usb_device *dev = bus->devices;
  46. dev; dev = dev->next) {
  47. if (!jtag_libusb_match(dev, vids, pids))
  48. continue;
  49. *out = usb_open(dev);
  50. if (NULL == *out)
  51. return -errno;
  52. return 0;
  53. }
  54. }
  55. return -ENODEV;
  56. }
  57. void jtag_libusb_close(jtag_libusb_device_handle *dev)
  58. {
  59. /* Close device */
  60. jtag_libusb_close(dev);
  61. }
  62. int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes,
  63. int size, int timeout)
  64. {
  65. return usb_bulk_write(dev, ep, bytes, size, timeout);
  66. }
  67. int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes,
  68. int size, int timeout)
  69. {
  70. return usb_bulk_read(dev, ep, bytes, size, timeout);
  71. }
  72. int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
  73. int configuration)
  74. {
  75. struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
  76. return usb_set_configuration(devh,
  77. udev->config[configuration].bConfigurationValue);
  78. }
  79. int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev,
  80. unsigned int *usb_read_ep,
  81. unsigned int *usb_write_ep)
  82. {
  83. struct usb_interface *iface = udev->config->interface;
  84. struct usb_interface_descriptor *desc = iface->altsetting;
  85. for (int i = 0; i < desc->bNumEndpoints; i++) {
  86. uint8_t epnum = desc->endpoint[i].bEndpointAddress;
  87. bool is_input = epnum & 0x80;
  88. LOG_DEBUG("usb ep %s %02x", is_input ? "in" : "out", epnum);
  89. if (is_input)
  90. *usb_read_ep = epnum;
  91. else
  92. *usb_write_ep = epnum;
  93. }
  94. return 0;
  95. }