usbutils.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2012 Andrew Smith
  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. #ifndef USBUTILS_H
  10. #define USBUTILS_H
  11. #include <libusb.h>
  12. // Use the device defined timeout
  13. #define DEVTIMEOUT 0
  14. // For endpoints defined in usb_find_devices.eps,
  15. // the first two must be the default IN and OUT
  16. #define DEFAULT_EP_IN 0
  17. #define DEFAULT_EP_OUT 1
  18. struct usb_endpoints {
  19. uint8_t att;
  20. uint16_t size;
  21. unsigned char ep;
  22. bool found;
  23. };
  24. struct usb_find_devices {
  25. int drv;
  26. const char *name;
  27. uint16_t idVendor;
  28. uint16_t idProduct;
  29. int config;
  30. int interface;
  31. unsigned int timeout;
  32. int epcount;
  33. struct usb_endpoints *eps;
  34. };
  35. struct cg_usb_device {
  36. struct usb_find_devices *found;
  37. libusb_device_handle *handle;
  38. pthread_mutex_t *mutex;
  39. struct libusb_device_descriptor *descriptor;
  40. uint8_t bus_number;
  41. uint8_t device_address;
  42. uint16_t usbver;
  43. int speed;
  44. char *prod_string;
  45. char *manuf_string;
  46. char *serial_string;
  47. unsigned char fwVersion; // ??
  48. unsigned char interfaceVersion; // ??
  49. };
  50. enum usb_cmds {
  51. C_PING = 0,
  52. C_CLEAR,
  53. C_REQUESTVERSION,
  54. C_GETVERSION,
  55. C_REQUESTFPGACOUNT,
  56. C_GETFPGACOUNT,
  57. C_STARTPROGRAM,
  58. C_STARTPROGRAMSTATUS,
  59. C_PROGRAM,
  60. C_PROGRAMSTATUS,
  61. C_PROGRAMSTATUS2,
  62. C_FINALPROGRAMSTATUS,
  63. C_SETCLOCK,
  64. C_REPLYSETCLOCK,
  65. C_REQUESTUSERCODE,
  66. C_GETUSERCODE,
  67. C_REQUESTTEMPERATURE,
  68. C_GETTEMPERATURE,
  69. C_SENDWORK,
  70. C_SENDWORKSTATUS,
  71. C_REQUESTWORKSTATUS,
  72. C_GETWORKSTATUS,
  73. C_MAX
  74. };
  75. struct device_api;
  76. struct cgpu_info;
  77. void usb_uninit(struct cgpu_info *cgpu);
  78. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found);
  79. void usb_detect(struct device_api *api, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
  80. struct api_data *api_usb_stats(int *count);
  81. void update_usb_stats(struct cgpu_info *cgpu);
  82. int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, int eol, enum usb_cmds);
  83. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
  84. void usb_cleanup();
  85. #define usb_read(cgpu, buf, bufsiz, read, cmd) \
  86. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, -1, cmd)
  87. #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
  88. _usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, -1, cmd)
  89. #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  90. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, -1, cmd)
  91. #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
  92. _usb_read(cgpu, ep, buf, bufsiz, read, timeout, -1, cmd)
  93. #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
  94. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  95. #define usb_write_ep(cgpu, ep, buf, bufsiz, wrote, cmd) \
  96. _usb_write(cgpu, ep, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  97. #define usb_write_timeout(cgpu, buf, bufsiz, wrote, timeout, cmd) \
  98. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, timeout, cmd)
  99. #define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
  100. _usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
  101. #endif