2 * This is free and unencumbered software released into the public domain.
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
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.
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.
25 * For more information, please refer to <http://unlicense.org/>
34 #define PRODUCT 0x0105
36 /* endpoints indexes */
38 #define EP_BULK_IN (1 | LIBUSB_ENDPOINT_IN)
39 #define EP_BULK_OUT (2 | LIBUSB_ENDPOINT_OUT)
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
56 libusb_device_handle
*handle
;
61 * test_init - initialize test program
64 int test_init(struct test_state
*state
)
75 ret
= libusb_init(&state
->ctx
);
77 printf("cannot init libusb: %s\n", libusb_error_name(ret
));
81 cnt
= libusb_get_device_list(state
->ctx
, &list
);
83 printf("no devices found\n");
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
);
92 printf("unable to get device descriptor: %s\n",
93 libusb_error_name(ret
));
96 if (desc
.idVendor
== VENDOR
&& desc
.idProduct
== PRODUCT
) {
103 printf("no devices found\n");
107 ret
= libusb_open(state
->found
, &state
->handle
);
109 printf("cannot open device: %s\n", libusb_error_name(ret
));
113 if (libusb_claim_interface(state
->handle
, 0)) {
114 ret
= libusb_detach_kernel_driver(state
->handle
, 0);
116 printf("unable to detach kernel driver: %s\n",
117 libusb_error_name(ret
));
121 ret
= libusb_claim_interface(state
->handle
, 0);
123 printf("cannot claim interface: %s\n",
124 libusb_error_name(ret
));
132 if (state
->attached
== 1)
133 libusb_attach_kernel_driver(state
->handle
, 0);
136 libusb_close(state
->handle
);
139 libusb_free_device_list(list
, 1);
142 libusb_exit(state
->ctx
);
147 * test_exit - cleanup test program
150 void test_exit(struct test_state
*state
)
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
);
161 struct test_state state
;
163 if (test_init(&state
))
167 static unsigned char buffer
[BUF_LEN
];
169 libusb_bulk_transfer(state
.handle
, EP_BULK_IN
, buffer
, BUF_LEN
,
171 libusb_bulk_transfer(state
.handle
, EP_BULK_OUT
, buffer
, BUF_LEN
,