1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libusbdev
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * USB device driver framework - automatic interrupt polling.
|
---|
34 | */
|
---|
35 | #include <usb/dev/poll.h>
|
---|
36 | #include <usb/dev/request.h>
|
---|
37 | #include <usb/debug.h>
|
---|
38 | #include <usb/classes/classes.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <str_error.h>
|
---|
41 | #include <assert.h>
|
---|
42 |
|
---|
43 | /** Maximum number of failed consecutive requests before announcing failure. */
|
---|
44 | #define MAX_FAILED_ATTEMPTS 3
|
---|
45 |
|
---|
46 | /** Data needed for polling. */
|
---|
47 | typedef struct {
|
---|
48 | usb_device_auto_polling_t auto_polling;
|
---|
49 |
|
---|
50 | usb_device_t *dev;
|
---|
51 | size_t pipe_index;
|
---|
52 | size_t request_size;
|
---|
53 | uint8_t *buffer;
|
---|
54 | void *custom_arg;
|
---|
55 | } polling_data_t;
|
---|
56 |
|
---|
57 |
|
---|
58 | /** Polling fibril.
|
---|
59 | *
|
---|
60 | * @param arg Pointer to polling_data_t.
|
---|
61 | * @return Always EOK.
|
---|
62 | */
|
---|
63 | static int polling_fibril(void *arg)
|
---|
64 | {
|
---|
65 | polling_data_t *polling_data = (polling_data_t *) arg;
|
---|
66 | assert(polling_data);
|
---|
67 |
|
---|
68 | usb_pipe_t *pipe
|
---|
69 | = &polling_data->dev->pipes[polling_data->pipe_index].pipe;
|
---|
70 |
|
---|
71 | if (polling_data->auto_polling.debug > 0) {
|
---|
72 | const usb_endpoint_mapping_t *mapping
|
---|
73 | = &polling_data->dev->pipes[polling_data->pipe_index];
|
---|
74 | usb_log_debug("Poll%p: started polling of `%s' - " \
|
---|
75 | "interface %d (%s,%d,%d), %zuB/%zu.\n",
|
---|
76 | polling_data,
|
---|
77 | polling_data->dev->ddf_dev->name,
|
---|
78 | (int) mapping->interface->interface_number,
|
---|
79 | usb_str_class(mapping->interface->interface_class),
|
---|
80 | (int) mapping->interface->interface_subclass,
|
---|
81 | (int) mapping->interface->interface_protocol,
|
---|
82 | polling_data->request_size, pipe->max_packet_size);
|
---|
83 | }
|
---|
84 |
|
---|
85 | size_t failed_attempts = 0;
|
---|
86 | while (failed_attempts <= polling_data->auto_polling.max_failures) {
|
---|
87 | size_t actual_size;
|
---|
88 | const int rc = usb_pipe_read(pipe, polling_data->buffer,
|
---|
89 | polling_data->request_size, &actual_size);
|
---|
90 |
|
---|
91 | if (polling_data->auto_polling.debug > 1) {
|
---|
92 | if (rc == EOK) {
|
---|
93 | usb_log_debug(
|
---|
94 | "Poll%p: received: '%s' (%zuB).\n",
|
---|
95 | polling_data,
|
---|
96 | usb_debug_str_buffer(polling_data->buffer,
|
---|
97 | actual_size, 16),
|
---|
98 | actual_size);
|
---|
99 | } else {
|
---|
100 | usb_log_debug(
|
---|
101 | "Poll%p: polling failed: %s.\n",
|
---|
102 | polling_data, str_error(rc));
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | /* If the pipe stalled, we can try to reset the stall. */
|
---|
107 | if ((rc == ESTALL) && (polling_data->auto_polling.auto_clear_halt)) {
|
---|
108 | /*
|
---|
109 | * We ignore error here as this is usually a futile
|
---|
110 | * attempt anyway.
|
---|
111 | */
|
---|
112 | usb_request_clear_endpoint_halt(
|
---|
113 | &polling_data->dev->ctrl_pipe, pipe->endpoint_no);
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (rc != EOK) {
|
---|
117 | if (polling_data->auto_polling.on_error != NULL) {
|
---|
118 | bool cont = polling_data->auto_polling.on_error(
|
---|
119 | polling_data->dev, rc,
|
---|
120 | polling_data->custom_arg);
|
---|
121 | if (!cont) {
|
---|
122 | failed_attempts
|
---|
123 | = polling_data->auto_polling.max_failures;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | failed_attempts++;
|
---|
127 | continue;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /* We have the data, execute the callback now. */
|
---|
131 | const bool carry_on = polling_data->auto_polling.on_data(
|
---|
132 | polling_data->dev, polling_data->buffer, actual_size,
|
---|
133 | polling_data->custom_arg);
|
---|
134 |
|
---|
135 | if (!carry_on) {
|
---|
136 | failed_attempts = 0;
|
---|
137 | break;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /* Reset as something might be only a temporary problem. */
|
---|
141 | failed_attempts = 0;
|
---|
142 |
|
---|
143 | /* Take a rest before next request. */
|
---|
144 | async_usleep(polling_data->auto_polling.delay);
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (polling_data->auto_polling.on_polling_end != NULL) {
|
---|
148 | polling_data->auto_polling.on_polling_end(polling_data->dev,
|
---|
149 | failed_attempts > 0, polling_data->custom_arg);
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (polling_data->auto_polling.debug > 0) {
|
---|
153 | if (failed_attempts > 0) {
|
---|
154 | usb_log_error(
|
---|
155 | "Polling of device `%s' terminated: %s.\n",
|
---|
156 | polling_data->dev->ddf_dev->name,
|
---|
157 | "recurring failures");
|
---|
158 | } else {
|
---|
159 | usb_log_debug(
|
---|
160 | "Polling of device `%s' terminated by user.\n",
|
---|
161 | polling_data->dev->ddf_dev->name
|
---|
162 | );
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | /* Free the allocated memory. */
|
---|
167 | free(polling_data->buffer);
|
---|
168 | free(polling_data);
|
---|
169 |
|
---|
170 | return EOK;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /** Start automatic device polling over interrupt in pipe.
|
---|
174 | *
|
---|
175 | * @warning It is up to the callback to produce delays between individual
|
---|
176 | * requests.
|
---|
177 | *
|
---|
178 | * @warning There is no guarantee when the request to the device
|
---|
179 | * will be sent for the first time (it is possible that this
|
---|
180 | * first request would be executed prior to return from this function).
|
---|
181 | *
|
---|
182 | * @param dev Device to be periodically polled.
|
---|
183 | * @param pipe_index Index of the endpoint pipe used for polling.
|
---|
184 | * @param callback Callback when data are available.
|
---|
185 | * @param request_size How many bytes to ask for in each request.
|
---|
186 | * @param terminated_callback Callback when polling is terminated.
|
---|
187 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
188 | * @return Error code.
|
---|
189 | * @retval EOK New fibril polling the device was already started.
|
---|
190 | */
|
---|
191 | int usb_device_auto_poll(usb_device_t *dev, size_t pipe_index,
|
---|
192 | usb_polling_callback_t callback, size_t request_size,
|
---|
193 | usb_polling_terminted_callback_t terminated_callback, void *arg)
|
---|
194 | {
|
---|
195 | const usb_device_auto_polling_t auto_polling = {
|
---|
196 | .debug = 1,
|
---|
197 | .auto_clear_halt = true,
|
---|
198 | .delay = 0,
|
---|
199 | .max_failures = MAX_FAILED_ATTEMPTS,
|
---|
200 | .on_data = callback,
|
---|
201 | .on_polling_end = terminated_callback,
|
---|
202 | .on_error = NULL,
|
---|
203 | };
|
---|
204 |
|
---|
205 | return usb_device_auto_polling(dev, pipe_index, &auto_polling,
|
---|
206 | request_size, arg);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /** Start automatic device polling over interrupt in pipe.
|
---|
210 | *
|
---|
211 | * The polling settings is copied thus it is okay to destroy the structure
|
---|
212 | * after this function returns.
|
---|
213 | *
|
---|
214 | * @warning There is no guarantee when the request to the device
|
---|
215 | * will be sent for the first time (it is possible that this
|
---|
216 | * first request would be executed prior to return from this function).
|
---|
217 | *
|
---|
218 | * @param dev Device to be periodically polled.
|
---|
219 | * @param pipe_index Index of the endpoint pipe used for polling.
|
---|
220 | * @param polling Polling settings.
|
---|
221 | * @param request_size How many bytes to ask for in each request.
|
---|
222 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
223 | * @return Error code.
|
---|
224 | * @retval EOK New fibril polling the device was already started.
|
---|
225 | */
|
---|
226 | int usb_device_auto_polling(usb_device_t *dev, size_t pipe_index,
|
---|
227 | const usb_device_auto_polling_t *polling,
|
---|
228 | size_t request_size, void *arg)
|
---|
229 | {
|
---|
230 | if ((dev == NULL) || (polling == NULL) || (polling->on_data == NULL)) {
|
---|
231 | return EBADMEM;
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (pipe_index >= dev->pipes_count || request_size == 0) {
|
---|
235 | return EINVAL;
|
---|
236 | }
|
---|
237 | if ((dev->pipes[pipe_index].pipe.transfer_type != USB_TRANSFER_INTERRUPT)
|
---|
238 | || (dev->pipes[pipe_index].pipe.direction != USB_DIRECTION_IN)) {
|
---|
239 | return EINVAL;
|
---|
240 | }
|
---|
241 |
|
---|
242 | polling_data_t *polling_data = malloc(sizeof(polling_data_t));
|
---|
243 | if (polling_data == NULL) {
|
---|
244 | return ENOMEM;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /* Fill-in the data. */
|
---|
248 | polling_data->buffer = malloc(sizeof(request_size));
|
---|
249 | if (polling_data->buffer == NULL) {
|
---|
250 | free(polling_data);
|
---|
251 | return ENOMEM;
|
---|
252 | }
|
---|
253 | polling_data->request_size = request_size;
|
---|
254 | polling_data->dev = dev;
|
---|
255 | polling_data->pipe_index = pipe_index;
|
---|
256 | polling_data->custom_arg = arg;
|
---|
257 |
|
---|
258 | /* Copy provided settings. */
|
---|
259 | polling_data->auto_polling = *polling;
|
---|
260 |
|
---|
261 | /* Negative value means use descriptor provided value. */
|
---|
262 | if (polling->delay < 0) {
|
---|
263 | polling_data->auto_polling.delay =
|
---|
264 | (int) dev->pipes[pipe_index].descriptor->poll_interval;
|
---|
265 | }
|
---|
266 |
|
---|
267 | fid_t fibril = fibril_create(polling_fibril, polling_data);
|
---|
268 | if (fibril == 0) {
|
---|
269 | free(polling_data->buffer);
|
---|
270 | free(polling_data);
|
---|
271 | return ENOMEM;
|
---|
272 | }
|
---|
273 | fibril_add_ready(fibril);
|
---|
274 |
|
---|
275 | /* Fibril launched. That fibril will free the allocated data. */
|
---|
276 |
|
---|
277 | return EOK;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * @}
|
---|
282 | */
|
---|