test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * This is free and unencumbered software released into the public domain.
  3. *
  4. * Anyone is free to copy, modify, publish, use, compile, sell, or
  5. * distribute this software, either in source code form or as a compiled
  6. * binary, for any purpose, commercial or non-commercial, and by any
  7. * means.
  8. *
  9. * In jurisdictions that recognize copyright laws, the author or authors
  10. * of this software dedicate any and all copyright interest in the
  11. * software to the public domain. We make this dedication for the benefit
  12. * of the public at large and to the detriment of our heirs and
  13. * successors. We intend this dedication to be an overt act of
  14. * relinquishment in perpetuity of all present and future rights to this
  15. * software under copyright law.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * For more information, please refer to <http://unlicense.org/>
  26. */
  27. #include <libusb.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #define VENDOR 0x1d6b
  32. #define PRODUCT 0x0105
  33. #define BUF_LEN 8192
  34. /*
  35. * struct test_state - describes test program state
  36. * @list: list of devices returned by libusb_get_device_list function
  37. * @found: pointer to struct describing tested device
  38. * @ctx: context, set to NULL
  39. * @handle: handle of tested device
  40. * @attached: indicates that device was attached to kernel, and has to be
  41. * reattached at the end of test program
  42. */
  43. struct test_state {
  44. libusb_device *found;
  45. libusb_context *ctx;
  46. libusb_device_handle *handle;
  47. int attached;
  48. };
  49. /*
  50. * test_init - initialize test program
  51. */
  52. int test_init(struct test_state *state)
  53. {
  54. int i, ret;
  55. ssize_t cnt;
  56. libusb_device **list;
  57. state->found = NULL;
  58. state->ctx = NULL;
  59. state->handle = NULL;
  60. state->attached = 0;
  61. ret = libusb_init(&state->ctx);
  62. if (ret) {
  63. printf("cannot init libusb: %s\n", libusb_error_name(ret));
  64. return 1;
  65. }
  66. cnt = libusb_get_device_list(state->ctx, &list);
  67. if (cnt <= 0) {
  68. printf("no devices found\n");
  69. goto error1;
  70. }
  71. for (i = 0; i < cnt; ++i) {
  72. libusb_device *dev = list[i];
  73. struct libusb_device_descriptor desc;
  74. ret = libusb_get_device_descriptor(dev, &desc);
  75. if (ret) {
  76. printf("unable to get device descriptor: %s\n",
  77. libusb_error_name(ret));
  78. goto error2;
  79. }
  80. if (desc.idVendor == VENDOR && desc.idProduct == PRODUCT) {
  81. state->found = dev;
  82. break;
  83. }
  84. }
  85. if (!state->found) {
  86. printf("no devices found\n");
  87. goto error2;
  88. }
  89. ret = libusb_open(state->found, &state->handle);
  90. if (ret) {
  91. printf("cannot open device: %s\n", libusb_error_name(ret));
  92. goto error2;
  93. }
  94. if (libusb_claim_interface(state->handle, 0)) {
  95. ret = libusb_detach_kernel_driver(state->handle, 0);
  96. if (ret) {
  97. printf("unable to detach kernel driver: %s\n",
  98. libusb_error_name(ret));
  99. goto error3;
  100. }
  101. state->attached = 1;
  102. ret = libusb_claim_interface(state->handle, 0);
  103. if (ret) {
  104. printf("cannot claim interface: %s\n",
  105. libusb_error_name(ret));
  106. goto error4;
  107. }
  108. }
  109. return 0;
  110. error4:
  111. if (state->attached == 1)
  112. libusb_attach_kernel_driver(state->handle, 0);
  113. error3:
  114. libusb_close(state->handle);
  115. error2:
  116. libusb_free_device_list(list, 1);
  117. error1:
  118. libusb_exit(state->ctx);
  119. return 1;
  120. }
  121. /*
  122. * test_exit - cleanup test program
  123. */
  124. void test_exit(struct test_state *state)
  125. {
  126. libusb_release_interface(state->handle, 0);
  127. if (state->attached == 1)
  128. libusb_attach_kernel_driver(state->handle, 0);
  129. libusb_close(state->handle);
  130. libusb_exit(state->ctx);
  131. }
  132. int main(void)
  133. {
  134. struct test_state state;
  135. struct libusb_config_descriptor *conf;
  136. struct libusb_interface_descriptor const *iface;
  137. unsigned char in_addr, out_addr;
  138. if (test_init(&state))
  139. return 1;
  140. libusb_get_config_descriptor(state.found, 0, &conf);
  141. iface = &conf->interface[0].altsetting[0];
  142. in_addr = iface->endpoint[0].bEndpointAddress;
  143. out_addr = iface->endpoint[1].bEndpointAddress;
  144. while (1) {
  145. static unsigned char buffer[BUF_LEN];
  146. int bytes;
  147. libusb_bulk_transfer(state.handle, in_addr, buffer, BUF_LEN,
  148. &bytes, 500);
  149. libusb_bulk_transfer(state.handle, out_addr, buffer, BUF_LEN,
  150. &bytes, 500);
  151. }
  152. test_exit(&state);
  153. }