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