test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /* endpoints indexes */
  34. #define EP_BULK_IN (1 | LIBUSB_ENDPOINT_IN)
  35. #define EP_BULK_OUT (2 | LIBUSB_ENDPOINT_OUT)
  36. #define BUF_LEN 8192
  37. /*
  38. * struct test_state - describes test program state
  39. * @list: list of devices returned by libusb_get_device_list function
  40. * @found: pointer to struct describing tested device
  41. * @ctx: context, set to NULL
  42. * @handle: handle of tested device
  43. * @attached: indicates that device was attached to kernel, and has to be
  44. * reattached at the end of test program
  45. */
  46. struct test_state {
  47. libusb_device *found;
  48. libusb_context *ctx;
  49. libusb_device_handle *handle;
  50. int attached;
  51. };
  52. /*
  53. * test_init - initialize test program
  54. */
  55. int test_init(struct test_state *state)
  56. {
  57. int i, ret;
  58. ssize_t cnt;
  59. libusb_device **list;
  60. state->found = NULL;
  61. state->ctx = NULL;
  62. state->handle = NULL;
  63. state->attached = 0;
  64. ret = libusb_init(&state->ctx);
  65. if (ret) {
  66. printf("cannot init libusb: %s\n", libusb_error_name(ret));
  67. return 1;
  68. }
  69. cnt = libusb_get_device_list(state->ctx, &list);
  70. if (cnt <= 0) {
  71. printf("no devices found\n");
  72. goto error1;
  73. }
  74. for (i = 0; i < cnt; ++i) {
  75. libusb_device *dev = list[i];
  76. struct libusb_device_descriptor desc;
  77. ret = libusb_get_device_descriptor(dev, &desc);
  78. if (ret) {
  79. printf("unable to get device descriptor: %s\n",
  80. libusb_error_name(ret));
  81. goto error2;
  82. }
  83. if (desc.idVendor == VENDOR && desc.idProduct == PRODUCT) {
  84. state->found = dev;
  85. break;
  86. }
  87. }
  88. if (!state->found) {
  89. printf("no devices found\n");
  90. goto error2;
  91. }
  92. ret = libusb_open(state->found, &state->handle);
  93. if (ret) {
  94. printf("cannot open device: %s\n", libusb_error_name(ret));
  95. goto error2;
  96. }
  97. if (libusb_claim_interface(state->handle, 0)) {
  98. ret = libusb_detach_kernel_driver(state->handle, 0);
  99. if (ret) {
  100. printf("unable to detach kernel driver: %s\n",
  101. libusb_error_name(ret));
  102. goto error3;
  103. }
  104. state->attached = 1;
  105. ret = libusb_claim_interface(state->handle, 0);
  106. if (ret) {
  107. printf("cannot claim interface: %s\n",
  108. libusb_error_name(ret));
  109. goto error4;
  110. }
  111. }
  112. return 0;
  113. error4:
  114. if (state->attached == 1)
  115. libusb_attach_kernel_driver(state->handle, 0);
  116. error3:
  117. libusb_close(state->handle);
  118. error2:
  119. libusb_free_device_list(list, 1);
  120. error1:
  121. libusb_exit(state->ctx);
  122. return 1;
  123. }
  124. /*
  125. * test_exit - cleanup test program
  126. */
  127. void test_exit(struct test_state *state)
  128. {
  129. libusb_release_interface(state->handle, 0);
  130. if (state->attached == 1)
  131. libusb_attach_kernel_driver(state->handle, 0);
  132. libusb_close(state->handle);
  133. libusb_exit(state->ctx);
  134. }
  135. int main(void)
  136. {
  137. struct test_state state;
  138. if (test_init(&state))
  139. return 1;
  140. while (1) {
  141. static unsigned char buffer[BUF_LEN];
  142. int bytes;
  143. libusb_bulk_transfer(state.handle, EP_BULK_IN, buffer, BUF_LEN,
  144. &bytes, 500);
  145. }
  146. test_exit(&state);
  147. }