9 /* endpoints indexes */
11 #define EP_BULK_IN (1 | LIBUSB_ENDPOINT_IN)
12 #define EP_BULK_OUT (2 | LIBUSB_ENDPOINT_OUT)
17 * struct test_state - describes test program state
18 * @list: list of devices returned by libusb_get_device_list function
19 * @found: pointer to struct describing tested device
20 * @ctx: context, set to NULL
21 * @handle: handle of tested device
22 * @attached: indicates that device was attached to kernel, and has to be
23 * reattached at the end of test program
29 libusb_device_handle
*handle
;
34 * test_init - initialize test program
37 int test_init(struct test_state
*state
)
48 ret
= libusb_init(&state
->ctx
);
50 printf("cannot init libusb: %s\n", libusb_error_name(ret
));
54 cnt
= libusb_get_device_list(state
->ctx
, &list
);
56 printf("no devices found\n");
60 for (i
= 0; i
< cnt
; ++i
) {
61 libusb_device
*dev
= list
[i
];
62 struct libusb_device_descriptor desc
;
63 ret
= libusb_get_device_descriptor(dev
, &desc
);
65 printf("unable to get device descriptor: %s\n",
66 libusb_error_name(ret
));
69 if (desc
.idVendor
== VENDOR
&& desc
.idProduct
== PRODUCT
) {
76 printf("no devices found\n");
80 ret
= libusb_open(state
->found
, &state
->handle
);
82 printf("cannot open device: %s\n", libusb_error_name(ret
));
86 if (libusb_claim_interface(state
->handle
, 0)) {
87 ret
= libusb_detach_kernel_driver(state
->handle
, 0);
89 printf("unable to detach kernel driver: %s\n",
90 libusb_error_name(ret
));
94 ret
= libusb_claim_interface(state
->handle
, 0);
96 printf("cannot claim interface: %s\n",
97 libusb_error_name(ret
));
105 if (state
->attached
== 1)
106 libusb_attach_kernel_driver(state
->handle
, 0);
109 libusb_close(state
->handle
);
112 libusb_free_device_list(list
, 1);
115 libusb_exit(state
->ctx
);
120 * test_exit - cleanup test program
123 void test_exit(struct test_state
*state
)
125 libusb_release_interface(state
->handle
, 0);
126 if (state
->attached
== 1)
127 libusb_attach_kernel_driver(state
->handle
, 0);
128 libusb_close(state
->handle
);
129 libusb_exit(state
->ctx
);
134 struct test_state state
;
136 if (test_init(&state
))
140 static unsigned char buffer
[BUF_LEN
];
142 libusb_bulk_transfer(state
.handle
, EP_BULK_IN
, buffer
, BUF_LEN
,
144 libusb_bulk_transfer(state
.handle
, EP_BULK_OUT
, buffer
, BUF_LEN
,