ARM: shmobile: Add DT bindings for Renesas memory controllers
[linux/fpc-iii.git] / tools / usb / ffs-aio-example / simple / host_app / test.c
blobacd6332811f3237d80561e2667d9c1f33b0f0c5d
1 /*
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
7 * means.
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/>
28 #include <libusb.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
33 #define VENDOR 0x1d6b
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)
41 #define BUF_LEN 8192
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
53 struct test_state {
54 libusb_device *found;
55 libusb_context *ctx;
56 libusb_device_handle *handle;
57 int attached;
61 * test_init - initialize test program
64 int test_init(struct test_state *state)
66 int i, ret;
67 ssize_t cnt;
68 libusb_device **list;
70 state->found = NULL;
71 state->ctx = NULL;
72 state->handle = NULL;
73 state->attached = 0;
75 ret = libusb_init(&state->ctx);
76 if (ret) {
77 printf("cannot init libusb: %s\n", libusb_error_name(ret));
78 return 1;
81 cnt = libusb_get_device_list(state->ctx, &list);
82 if (cnt <= 0) {
83 printf("no devices found\n");
84 goto error1;
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;
96 if (desc.idVendor == VENDOR && desc.idProduct == PRODUCT) {
97 state->found = dev;
98 break;
102 if (!state->found) {
103 printf("no devices found\n");
104 goto error2;
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;
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;
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;
129 return 0;
131 error4:
132 if (state->attached == 1)
133 libusb_attach_kernel_driver(state->handle, 0);
135 error3:
136 libusb_close(state->handle);
138 error2:
139 libusb_free_device_list(list, 1);
141 error1:
142 libusb_exit(state->ctx);
143 return 1;
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);
159 int main(void)
161 struct test_state state;
163 if (test_init(&state))
164 return 1;
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);
174 test_exit(&state);