test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <libusb.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #define VENDOR 0x1d6b
  6. #define PRODUCT 0x0105
  7. /* endpoints indexes */
  8. #define EP_BULK_IN (1 | LIBUSB_ENDPOINT_IN)
  9. #define EP_BULK_OUT (2 | LIBUSB_ENDPOINT_OUT)
  10. #define BUF_LEN 8192
  11. /*
  12. * struct test_state - describes test program state
  13. * @list: list of devices returned by libusb_get_device_list function
  14. * @found: pointer to struct describing tested device
  15. * @ctx: context, set to NULL
  16. * @handle: handle of tested device
  17. * @attached: indicates that device was attached to kernel, and has to be
  18. * reattached at the end of test program
  19. */
  20. struct test_state {
  21. libusb_device *found;
  22. libusb_context *ctx;
  23. libusb_device_handle *handle;
  24. int attached;
  25. };
  26. /*
  27. * test_init - initialize test program
  28. */
  29. int test_init(struct test_state *state)
  30. {
  31. int i, ret;
  32. ssize_t cnt;
  33. libusb_device **list;
  34. state->found = NULL;
  35. state->ctx = NULL;
  36. state->handle = NULL;
  37. state->attached = 0;
  38. ret = libusb_init(&state->ctx);
  39. if (ret) {
  40. printf("cannot init libusb: %s\n", libusb_error_name(ret));
  41. return 1;
  42. }
  43. cnt = libusb_get_device_list(state->ctx, &list);
  44. if (cnt <= 0) {
  45. printf("no devices found\n");
  46. goto error1;
  47. }
  48. for (i = 0; i < cnt; ++i) {
  49. libusb_device *dev = list[i];
  50. struct libusb_device_descriptor desc;
  51. ret = libusb_get_device_descriptor(dev, &desc);
  52. if (ret) {
  53. printf("unable to get device descriptor: %s\n",
  54. libusb_error_name(ret));
  55. goto error2;
  56. }
  57. if (desc.idVendor == VENDOR && desc.idProduct == PRODUCT) {
  58. state->found = dev;
  59. break;
  60. }
  61. }
  62. if (!state->found) {
  63. printf("no devices found\n");
  64. goto error2;
  65. }
  66. ret = libusb_open(state->found, &state->handle);
  67. if (ret) {
  68. printf("cannot open device: %s\n", libusb_error_name(ret));
  69. goto error2;
  70. }
  71. if (libusb_claim_interface(state->handle, 0)) {
  72. ret = libusb_detach_kernel_driver(state->handle, 0);
  73. if (ret) {
  74. printf("unable to detach kernel driver: %s\n",
  75. libusb_error_name(ret));
  76. goto error3;
  77. }
  78. state->attached = 1;
  79. ret = libusb_claim_interface(state->handle, 0);
  80. if (ret) {
  81. printf("cannot claim interface: %s\n",
  82. libusb_error_name(ret));
  83. goto error4;
  84. }
  85. }
  86. return 0;
  87. error4:
  88. if (state->attached == 1)
  89. libusb_attach_kernel_driver(state->handle, 0);
  90. error3:
  91. libusb_close(state->handle);
  92. error2:
  93. libusb_free_device_list(list, 1);
  94. error1:
  95. libusb_exit(state->ctx);
  96. return 1;
  97. }
  98. /*
  99. * test_exit - cleanup test program
  100. */
  101. void test_exit(struct test_state *state)
  102. {
  103. libusb_release_interface(state->handle, 0);
  104. if (state->attached == 1)
  105. libusb_attach_kernel_driver(state->handle, 0);
  106. libusb_close(state->handle);
  107. libusb_exit(state->ctx);
  108. }
  109. int main(void)
  110. {
  111. struct test_state state;
  112. if (test_init(&state))
  113. return 1;
  114. while (1) {
  115. static unsigned char buffer[BUF_LEN];
  116. int bytes;
  117. libusb_bulk_transfer(state.handle, EP_BULK_IN, buffer, BUF_LEN,
  118. &bytes, 500);
  119. }
  120. test_exit(&state);
  121. }