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
39 * struct test_state - describes test program state
40 * @list: list of devices returned by libusb_get_device_list function
41 * @found: pointer to struct describing tested device
42 * @ctx: context, set to NULL
43 * @handle: handle of tested device
44 * @attached: indicates that device was attached to kernel, and has to be
45 * reattached at the end of test program
51 libusb_device_handle
*handle
;
56 * test_init - initialize test program
59 int test_init(struct test_state
*state
)
70 ret
= libusb_init(&state
->ctx
);
72 printf("cannot init libusb: %s\n", libusb_error_name(ret
));
76 cnt
= libusb_get_device_list(state
->ctx
, &list
);
78 printf("no devices found\n");
82 for (i
= 0; i
< cnt
; ++i
) {
83 libusb_device
*dev
= list
[i
];
84 struct libusb_device_descriptor desc
;
85 ret
= libusb_get_device_descriptor(dev
, &desc
);
87 printf("unable to get device descriptor: %s\n",
88 libusb_error_name(ret
));
91 if (desc
.idVendor
== VENDOR
&& desc
.idProduct
== PRODUCT
) {
98 printf("no devices found\n");
102 ret
= libusb_open(state
->found
, &state
->handle
);
104 printf("cannot open device: %s\n", libusb_error_name(ret
));
108 if (libusb_claim_interface(state
->handle
, 0)) {
109 ret
= libusb_detach_kernel_driver(state
->handle
, 0);
111 printf("unable to detach kernel driver: %s\n",
112 libusb_error_name(ret
));
116 ret
= libusb_claim_interface(state
->handle
, 0);
118 printf("cannot claim interface: %s\n",
119 libusb_error_name(ret
));
127 if (state
->attached
== 1)
128 libusb_attach_kernel_driver(state
->handle
, 0);
131 libusb_close(state
->handle
);
134 libusb_free_device_list(list
, 1);
137 libusb_exit(state
->ctx
);
142 * test_exit - cleanup test program
145 void test_exit(struct test_state
*state
)
147 libusb_release_interface(state
->handle
, 0);
148 if (state
->attached
== 1)
149 libusb_attach_kernel_driver(state
->handle
, 0);
150 libusb_close(state
->handle
);
151 libusb_exit(state
->ctx
);
156 struct test_state state
;
157 struct libusb_config_descriptor
*conf
;
158 struct libusb_interface_descriptor
const *iface
;
161 if (test_init(&state
))
164 libusb_get_config_descriptor(state
.found
, 0, &conf
);
165 iface
= &conf
->interface
[0].altsetting
[0];
166 addr
= iface
->endpoint
[0].bEndpointAddress
;
169 static unsigned char buffer
[BUF_LEN
];
171 libusb_bulk_transfer(state
.handle
, addr
, buffer
, BUF_LEN
,