lowl-usb.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdbool.h>
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <libusb.h>
  16. #include "logging.h"
  17. #include "lowlevel.h"
  18. #include "lowl-usb.h"
  19. #include "miner.h"
  20. #include "util.h"
  21. static
  22. char *lowl_libusb_dup_string(libusb_device_handle * const handle, const uint8_t idx, const char * const idxname, const char * const fname)
  23. {
  24. if (!idx)
  25. return NULL;
  26. unsigned char buf[0x100];
  27. const int n = libusb_get_string_descriptor_ascii(handle, idx, buf, sizeof(buf)-1);
  28. if (unlikely(n < 0)) {
  29. // This could be LOG_ERR, but it's annoyingly common :/
  30. applog(LOG_DEBUG, "%s: Error getting USB string %d (%s): %s",
  31. fname, idx, idxname, bfg_strerror(n, BST_LIBUSB));
  32. return NULL;
  33. }
  34. if (n == 0)
  35. return NULL;
  36. buf[n] = '\0';
  37. return strdup((void*)buf);
  38. }
  39. static
  40. void usb_devinfo_free(struct lowlevel_device_info * const info)
  41. {
  42. libusb_device * const dev = info->lowl_data;
  43. if (dev)
  44. libusb_unref_device(dev);
  45. }
  46. static
  47. struct lowlevel_device_info *usb_devinfo_scan()
  48. {
  49. struct lowlevel_device_info *devinfo_list = NULL;
  50. ssize_t count, i;
  51. libusb_device **list;
  52. struct libusb_device_descriptor desc;
  53. libusb_device_handle *handle;
  54. struct lowlevel_device_info *info;
  55. int err;
  56. if (unlikely(!have_libusb))
  57. return NULL;
  58. count = libusb_get_device_list(NULL, &list);
  59. if (unlikely(count < 0)) {
  60. applog(LOG_ERR, "%s: Error getting USB device list: %s",
  61. __func__, bfg_strerror(count, BST_LIBUSB));
  62. return NULL;
  63. }
  64. for (i = 0; i < count; ++i) {
  65. err = libusb_get_device_descriptor(list[i], &desc);
  66. if (unlikely(err)) {
  67. applog(LOG_ERR, "%s: Error getting device descriptor: %s",
  68. __func__, bfg_strerror(err, BST_LIBUSB));
  69. continue;
  70. }
  71. info = malloc(sizeof(struct lowlevel_device_info));
  72. *info = (struct lowlevel_device_info){
  73. .lowl = &lowl_usb,
  74. .devid = bfg_make_devid_libusb(list[i]),
  75. .lowl_data = libusb_ref_device(list[i]),
  76. .vid = desc.idVendor,
  77. .pid = desc.idProduct,
  78. };
  79. err = libusb_open(list[i], &handle);
  80. if (unlikely(err))
  81. applog(LOG_DEBUG, "%s: Error opening device %s: %s",
  82. __func__, info->devid, bfg_strerror(err, BST_LIBUSB));
  83. else
  84. {
  85. info->manufacturer = lowl_libusb_dup_string(handle, desc.iManufacturer, "iManufacturer", __func__);
  86. info->product = lowl_libusb_dup_string(handle, desc.iProduct, "iProduct", __func__);
  87. info->serial = lowl_libusb_dup_string(handle, desc.iSerialNumber, "iSerialNumber", __func__);
  88. libusb_close(handle);
  89. }
  90. LL_PREPEND(devinfo_list, info);
  91. }
  92. libusb_free_device_list(list, 1);
  93. return devinfo_list;
  94. }
  95. bool lowl_usb_attach_kernel_driver(const struct lowlevel_device_info * const info)
  96. {
  97. libusb_device * const dev = info->lowl_data;
  98. libusb_device_handle *devh;
  99. bool rv = false;
  100. if (libusb_open(dev, &devh))
  101. return false;
  102. if (libusb_kernel_driver_active(devh, 0) == 0)
  103. if (!libusb_attach_kernel_driver(devh, 0))
  104. {
  105. applog(LOG_DEBUG, "Reattaching kernel driver for %s", info->devid);
  106. rv = true;
  107. }
  108. libusb_close(devh);
  109. return rv;
  110. }
  111. struct libusb_device_handle *lowl_usb_open(struct lowlevel_device_info * const info)
  112. {
  113. libusb_device * const dev = info->lowl_data;
  114. if (!dev)
  115. return NULL;
  116. libusb_device_handle *devh;
  117. if (libusb_open(dev, &devh)) {
  118. applog(LOG_ERR, "%s: Error opening device", __func__);
  119. return NULL;
  120. }
  121. return devh;
  122. }
  123. struct device_drv *bfg_claim_usb(struct device_drv * const api, const bool verbose, const uint8_t usbbus, const uint8_t usbaddr)
  124. {
  125. char * const devpath = bfg_make_devid_usb(usbbus, usbaddr);
  126. struct device_drv * const rv = bfg_claim_any(api, verbose ? "" : NULL, devpath);
  127. free(devpath);
  128. return rv;
  129. }
  130. #ifdef HAVE_LIBUSB
  131. void cgpu_copy_libusb_strings(struct cgpu_info *cgpu, libusb_device *usb)
  132. {
  133. unsigned char buf[0x20];
  134. libusb_device_handle *h;
  135. struct libusb_device_descriptor desc;
  136. if (LIBUSB_SUCCESS != libusb_open(usb, &h))
  137. return;
  138. if (libusb_get_device_descriptor(usb, &desc))
  139. {
  140. libusb_close(h);
  141. return;
  142. }
  143. if ((!cgpu->dev_manufacturer) && libusb_get_string_descriptor_ascii(h, desc.iManufacturer, buf, sizeof(buf)) >= 0)
  144. cgpu->dev_manufacturer = strdup((void *)buf);
  145. if ((!cgpu->dev_product) && libusb_get_string_descriptor_ascii(h, desc.iProduct, buf, sizeof(buf)) >= 0)
  146. cgpu->dev_product = strdup((void *)buf);
  147. if ((!cgpu->dev_serial) && libusb_get_string_descriptor_ascii(h, desc.iSerialNumber, buf, sizeof(buf)) >= 0)
  148. cgpu->dev_serial = strdup((void *)buf);
  149. libusb_close(h);
  150. }
  151. #endif
  152. struct lowl_usb_endpoint {
  153. struct libusb_device_handle *devh;
  154. unsigned char endpoint_r;
  155. int packetsz_r;
  156. bytes_t _buf_r;
  157. unsigned timeout_ms_r;
  158. unsigned char endpoint_w;
  159. int packetsz_w;
  160. unsigned timeout_ms_w;
  161. };
  162. struct lowl_usb_endpoint *usb_open_ep(struct libusb_device_handle * const devh, const uint8_t epid, const int pktsz)
  163. {
  164. struct lowl_usb_endpoint * const ep = malloc(sizeof(*ep));
  165. ep->devh = devh;
  166. if (epid & 0x80)
  167. {
  168. // Read endpoint
  169. ep->endpoint_r = epid;
  170. ep->packetsz_r = pktsz;
  171. bytes_init(&ep->_buf_r);
  172. }
  173. else
  174. {
  175. // Write endpoint
  176. ep->endpoint_w = epid;
  177. ep->packetsz_w = epid;
  178. ep->packetsz_r = -1;
  179. }
  180. return ep;
  181. };
  182. struct lowl_usb_endpoint *usb_open_ep_pair(struct libusb_device_handle * const devh, const uint8_t epid_r, const int pktsz_r, const uint8_t epid_w, const int pktsz_w)
  183. {
  184. struct lowl_usb_endpoint * const ep = malloc(sizeof(*ep));
  185. *ep = (struct lowl_usb_endpoint){
  186. .devh = devh,
  187. .endpoint_r = epid_r,
  188. .packetsz_r = pktsz_r,
  189. ._buf_r = BYTES_INIT,
  190. .endpoint_w = epid_w,
  191. .packetsz_w = pktsz_w,
  192. };
  193. return ep;
  194. }
  195. void usb_ep_set_timeouts_ms(struct lowl_usb_endpoint * const ep, const unsigned timeout_ms_r, const unsigned timeout_ms_w)
  196. {
  197. ep->timeout_ms_r = timeout_ms_r;
  198. ep->timeout_ms_w = timeout_ms_w;
  199. }
  200. ssize_t usb_read(struct lowl_usb_endpoint * const ep, void * const data, size_t datasz)
  201. {
  202. unsigned timeout;
  203. size_t xfer;
  204. if ( (xfer = bytes_len(&ep->_buf_r)) < datasz)
  205. {
  206. bytes_extend_buf(&ep->_buf_r, datasz + ep->packetsz_r - 1);
  207. unsigned char *p = &bytes_buf(&ep->_buf_r)[xfer];
  208. int pxfer;
  209. int rem = datasz - xfer, rsz;
  210. timeout = xfer ? 0 : ep->timeout_ms_r;
  211. while (rem > 0)
  212. {
  213. rsz = (rem / ep->packetsz_r) * ep->packetsz_r;
  214. if (rsz < rem)
  215. rsz += ep->packetsz_r;
  216. switch (libusb_bulk_transfer(ep->devh, ep->endpoint_r, p, rsz, &pxfer, timeout))
  217. {
  218. case 0:
  219. case LIBUSB_ERROR_TIMEOUT:
  220. if (!pxfer)
  221. // Behaviour is like tcsetattr-style timeout
  222. return 0;
  223. p += pxfer;
  224. rem -= pxfer;
  225. // NOTE: Need to maintain _buf_r length so data is saved in case of error
  226. xfer += pxfer;
  227. bytes_resize(&ep->_buf_r, xfer);
  228. break;
  229. case LIBUSB_ERROR_PIPE:
  230. case LIBUSB_ERROR_NO_DEVICE:
  231. errno = EPIPE;
  232. return -1;
  233. default:
  234. errno = EIO;
  235. return -1;
  236. }
  237. timeout = 0;
  238. }
  239. }
  240. memcpy(data, bytes_buf(&ep->_buf_r), datasz);
  241. bytes_shift(&ep->_buf_r, datasz);
  242. return datasz;
  243. }
  244. ssize_t usb_write(struct lowl_usb_endpoint * const ep, const void * const data, size_t datasz)
  245. {
  246. unsigned timeout = ep->timeout_ms_w;
  247. unsigned char *p = (void*)data;
  248. size_t rem = datasz;
  249. int pxfer;
  250. while (rem > 0)
  251. {
  252. switch (libusb_bulk_transfer(ep->devh, ep->endpoint_w, p, rem, &pxfer, timeout))
  253. {
  254. case 0:
  255. case LIBUSB_ERROR_TIMEOUT:
  256. p += pxfer;
  257. rem -= pxfer;
  258. break;
  259. case LIBUSB_ERROR_PIPE:
  260. case LIBUSB_ERROR_NO_DEVICE:
  261. errno = EPIPE;
  262. return (datasz - rem) ?: -1;
  263. default:
  264. errno = EIO;
  265. return (datasz - rem) ?: -1;
  266. }
  267. timeout = 0;
  268. }
  269. errno = 0;
  270. return datasz;
  271. }
  272. void usb_close_ep(struct lowl_usb_endpoint * const ep)
  273. {
  274. if (ep->packetsz_r != -1)
  275. bytes_free(&ep->_buf_r);
  276. free(ep);
  277. }
  278. void lowl_usb_close(struct libusb_device_handle * const devh)
  279. {
  280. libusb_close(devh);
  281. }
  282. struct lowlevel_driver lowl_usb = {
  283. .dname = "usb",
  284. .devinfo_scan = usb_devinfo_scan,
  285. .devinfo_free = usb_devinfo_free,
  286. };