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 |
|
---|
36 | #include <usb/dev/device.h>
|
---|
37 | #include <usb/dev/pipes.h>
|
---|
38 | #include <usb/dev/poll.h>
|
---|
39 | #include <usb/dev/request.h>
|
---|
40 | #include <usb/classes/classes.h>
|
---|
41 | #include <usb/debug.h>
|
---|
42 | #include <usb/descriptor.h>
|
---|
43 | #include <usb/usb.h>
|
---|
44 |
|
---|
45 | #include <assert.h>
|
---|
46 | #include <async.h>
|
---|
47 | #include <errno.h>
|
---|
48 | #include <fibril.h>
|
---|
49 | #include <stdbool.h>
|
---|
50 | #include <stdlib.h>
|
---|
51 | #include <str_error.h>
|
---|
52 | #include <sys/types.h>
|
---|
53 |
|
---|
54 | /** Maximum number of failed consecutive requests before announcing failure. */
|
---|
55 | #define MAX_FAILED_ATTEMPTS 3
|
---|
56 |
|
---|
57 | /** Data needed for polling. */
|
---|
58 | typedef struct {
|
---|
59 | /** Parameters for automated polling. */
|
---|
60 | usb_device_auto_polling_t auto_polling;
|
---|
61 |
|
---|
62 | /** USB device to poll. */
|
---|
63 | usb_device_t *dev;
|
---|
64 | /** Device enpoint mapping to use for polling. */
|
---|
65 | usb_endpoint_mapping_t *polling_mapping;
|
---|
66 | /** Size of the recieved data. */
|
---|
67 | size_t request_size;
|
---|
68 | /** Data buffer. */
|
---|
69 | uint8_t *buffer;
|
---|
70 | } polling_data_t;
|
---|
71 |
|
---|
72 |
|
---|
73 | /** Polling fibril.
|
---|
74 | *
|
---|
75 | * @param arg Pointer to polling_data_t.
|
---|
76 | * @return Always EOK.
|
---|
77 | */
|
---|
78 | static int polling_fibril(void *arg)
|
---|
79 | {
|
---|
80 | assert(arg);
|
---|
81 | const polling_data_t *data = arg;
|
---|
82 | /* Helper to reduce typing. */
|
---|
83 | const usb_device_auto_polling_t *params = &data->auto_polling;
|
---|
84 |
|
---|
85 | usb_pipe_t *pipe = &data->polling_mapping->pipe;
|
---|
86 |
|
---|
87 | if (params->debug > 0) {
|
---|
88 | const usb_endpoint_mapping_t *mapping =
|
---|
89 | data->polling_mapping;
|
---|
90 | usb_log_debug("Poll (%p): started polling of `%s' - " \
|
---|
91 | "interface %d (%s,%d,%d), %zuB/%zu.\n",
|
---|
92 | data, usb_device_get_name(data->dev),
|
---|
93 | (int) mapping->interface->interface_number,
|
---|
94 | usb_str_class(mapping->interface->interface_class),
|
---|
95 | (int) mapping->interface->interface_subclass,
|
---|
96 | (int) mapping->interface->interface_protocol,
|
---|
97 | data->request_size, pipe->max_packet_size);
|
---|
98 | }
|
---|
99 |
|
---|
100 | size_t failed_attempts = 0;
|
---|
101 | while (failed_attempts <= params->max_failures) {
|
---|
102 | size_t actual_size;
|
---|
103 | const int rc = usb_pipe_read(pipe, data->buffer,
|
---|
104 | data->request_size, &actual_size);
|
---|
105 |
|
---|
106 | if (rc == EOK) {
|
---|
107 | if (params->debug > 1) {
|
---|
108 | usb_log_debug(
|
---|
109 | "Poll%p: received: '%s' (%zuB).\n",
|
---|
110 | data,
|
---|
111 | usb_debug_str_buffer(data->buffer,
|
---|
112 | actual_size, 16),
|
---|
113 | actual_size);
|
---|
114 | }
|
---|
115 | } else {
|
---|
116 | usb_log_debug(
|
---|
117 | "Poll%p: polling failed: %s.\n",
|
---|
118 | data, str_error(rc));
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* If the pipe stalled, we can try to reset the stall. */
|
---|
122 | if ((rc == ESTALL) && (params->auto_clear_halt)) {
|
---|
123 | /*
|
---|
124 | * We ignore error here as this is usually a futile
|
---|
125 | * attempt anyway.
|
---|
126 | */
|
---|
127 | usb_request_clear_endpoint_halt(
|
---|
128 | usb_device_get_default_pipe(data->dev),
|
---|
129 | pipe->endpoint_no);
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (rc != EOK) {
|
---|
133 | ++failed_attempts;
|
---|
134 | const bool cont = (params->on_error == NULL) ? true :
|
---|
135 | params->on_error(data->dev, rc, params->arg);
|
---|
136 | if (!cont) {
|
---|
137 | failed_attempts = params->max_failures;
|
---|
138 | }
|
---|
139 | continue;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /* We have the data, execute the callback now. */
|
---|
143 | assert(params->on_data);
|
---|
144 | const bool carry_on = params->on_data(
|
---|
145 | data->dev, data->buffer, actual_size, params->arg);
|
---|
146 |
|
---|
147 | if (!carry_on) {
|
---|
148 | /* This is user requested abort, erases failures. */
|
---|
149 | failed_attempts = 0;
|
---|
150 | break;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /* Reset as something might be only a temporary problem. */
|
---|
154 | failed_attempts = 0;
|
---|
155 |
|
---|
156 | /* Take a rest before next request. */
|
---|
157 |
|
---|
158 | // FIXME TODO: This is broken, the time is in ms not us.
|
---|
159 | // but first we need to fix drivers to actually stop using this,
|
---|
160 | // since polling delay should be implemented in HC schedule
|
---|
161 | async_usleep(params->delay);
|
---|
162 | }
|
---|
163 |
|
---|
164 | const bool failed = failed_attempts > 0;
|
---|
165 |
|
---|
166 | if (params->on_polling_end != NULL) {
|
---|
167 | params->on_polling_end(data->dev, failed, params->arg);
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (params->debug > 0) {
|
---|
171 | if (failed) {
|
---|
172 | usb_log_error("Polling of device `%s' terminated: "
|
---|
173 | "recurring failures.\n",
|
---|
174 | usb_device_get_name(data->dev));
|
---|
175 | } else {
|
---|
176 | usb_log_debug("Polling of device `%s' terminated: "
|
---|
177 | "driver request.\n",
|
---|
178 | usb_device_get_name(data->dev));
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | /* Free the allocated memory. */
|
---|
183 | free(data->buffer);
|
---|
184 | free(data);
|
---|
185 |
|
---|
186 | return EOK;
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | /** Start automatic device polling over interrupt in pipe.
|
---|
191 | *
|
---|
192 | * The polling settings is copied thus it is okay to destroy the structure
|
---|
193 | * after this function returns.
|
---|
194 | *
|
---|
195 | * @warning There is no guarantee when the request to the device
|
---|
196 | * will be sent for the first time (it is possible that this
|
---|
197 | * first request would be executed prior to return from this function).
|
---|
198 | *
|
---|
199 | * @param dev Device to be periodically polled.
|
---|
200 | * @param epm Endpoint mapping to use.
|
---|
201 | * @param polling Polling settings.
|
---|
202 | * @param request_size How many bytes to ask for in each request.
|
---|
203 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
204 | * @return Error code.
|
---|
205 | * @retval EOK New fibril polling the device was already started.
|
---|
206 | */
|
---|
207 | static int usb_device_auto_polling_internal(usb_device_t *dev,
|
---|
208 | usb_endpoint_mapping_t *epm, const usb_device_auto_polling_t *polling,
|
---|
209 | size_t request_size)
|
---|
210 | {
|
---|
211 | if ((dev == NULL) || (polling == NULL) || (polling->on_data == NULL)) {
|
---|
212 | return EBADMEM;
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (request_size == 0)
|
---|
216 | return EINVAL;
|
---|
217 |
|
---|
218 | if (!epm || (epm->pipe.transfer_type != USB_TRANSFER_INTERRUPT) ||
|
---|
219 | (epm->pipe.direction != USB_DIRECTION_IN))
|
---|
220 | return EINVAL;
|
---|
221 |
|
---|
222 |
|
---|
223 | polling_data_t *polling_data = malloc(sizeof(polling_data_t));
|
---|
224 | if (polling_data == NULL) {
|
---|
225 | return ENOMEM;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /* Fill-in the data. */
|
---|
229 | polling_data->buffer = malloc(sizeof(request_size));
|
---|
230 | if (polling_data->buffer == NULL) {
|
---|
231 | free(polling_data);
|
---|
232 | return ENOMEM;
|
---|
233 | }
|
---|
234 | polling_data->request_size = request_size;
|
---|
235 | polling_data->dev = dev;
|
---|
236 | polling_data->polling_mapping = epm;
|
---|
237 |
|
---|
238 | /* Copy provided settings. */
|
---|
239 | polling_data->auto_polling = *polling;
|
---|
240 |
|
---|
241 | /* Negative value means use descriptor provided value. */
|
---|
242 | if (polling->delay < 0) {
|
---|
243 | polling_data->auto_polling.delay =
|
---|
244 | epm->descriptor->poll_interval;
|
---|
245 | }
|
---|
246 |
|
---|
247 | fid_t fibril = fibril_create(polling_fibril, polling_data);
|
---|
248 | if (fibril == 0) {
|
---|
249 | free(polling_data->buffer);
|
---|
250 | free(polling_data);
|
---|
251 | return ENOMEM;
|
---|
252 | }
|
---|
253 | fibril_add_ready(fibril);
|
---|
254 |
|
---|
255 | /* Fibril launched. That fibril will free the allocated data. */
|
---|
256 |
|
---|
257 | return EOK;
|
---|
258 | }
|
---|
259 | /** Start automatic device polling over interrupt in pipe.
|
---|
260 | *
|
---|
261 | * The polling settings is copied thus it is okay to destroy the structure
|
---|
262 | * after this function returns.
|
---|
263 | *
|
---|
264 | * @warning There is no guarantee when the request to the device
|
---|
265 | * will be sent for the first time (it is possible that this
|
---|
266 | * first request would be executed prior to return from this function).
|
---|
267 | *
|
---|
268 | * @param dev Device to be periodically polled.
|
---|
269 | * @param pipe_index Index of the endpoint pipe used for polling.
|
---|
270 | * @param polling Polling settings.
|
---|
271 | * @param req_size How many bytes to ask for in each request.
|
---|
272 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
273 | * @return Error code.
|
---|
274 | * @retval EOK New fibril polling the device was already started.
|
---|
275 | */
|
---|
276 | int usb_device_auto_polling(usb_device_t *usb_dev, usb_endpoint_t ep,
|
---|
277 | const usb_device_auto_polling_t *polling, size_t req_size)
|
---|
278 | {
|
---|
279 | usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep(usb_dev, ep);
|
---|
280 | return usb_device_auto_polling_internal(usb_dev, epm, polling, req_size);
|
---|
281 | }
|
---|
282 |
|
---|
283 | /** Start automatic device polling over interrupt in pipe.
|
---|
284 | *
|
---|
285 | * @warning It is up to the callback to produce delays between individual
|
---|
286 | * requests.
|
---|
287 | *
|
---|
288 | * @warning There is no guarantee when the request to the device
|
---|
289 | * will be sent for the first time (it is possible that this
|
---|
290 | * first request would be executed prior to return from this function).
|
---|
291 | *
|
---|
292 | * @param dev Device to be periodically polled.
|
---|
293 | * @param ep Endpoint used for polling.
|
---|
294 | * @param callback Callback when data are available.
|
---|
295 | * @param request_size How many bytes to ask for in each request.
|
---|
296 | * @param delay NUmber of ms to wait between queries, -1 to use descriptor val.
|
---|
297 | * @param terminated_callback Callback when polling is terminated.
|
---|
298 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
299 | * @return Error code.
|
---|
300 | * @retval EOK New fibril polling the device was already started.
|
---|
301 | */
|
---|
302 | int usb_device_auto_poll(usb_device_t *dev, usb_endpoint_t ep,
|
---|
303 | usb_polling_callback_t callback, size_t request_size, int delay,
|
---|
304 | usb_polling_terminted_callback_t terminated_callback, void *arg)
|
---|
305 | {
|
---|
306 | const usb_device_auto_polling_t auto_polling = {
|
---|
307 | .debug = 1,
|
---|
308 | .auto_clear_halt = true,
|
---|
309 | .delay = delay,
|
---|
310 | .max_failures = MAX_FAILED_ATTEMPTS,
|
---|
311 | .on_data = callback,
|
---|
312 | .on_polling_end = terminated_callback,
|
---|
313 | .on_error = NULL,
|
---|
314 | .arg = arg,
|
---|
315 | };
|
---|
316 |
|
---|
317 | usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep(dev, ep);
|
---|
318 | return usb_device_auto_polling_internal(
|
---|
319 | dev, epm, &auto_polling, request_size);
|
---|
320 | }
|
---|
321 |
|
---|
322 | int usb_device_auto_polling_desc(usb_device_t *usb_dev,
|
---|
323 | const usb_endpoint_description_t *desc,
|
---|
324 | const usb_device_auto_polling_t *polling, size_t req_size)
|
---|
325 | {
|
---|
326 | usb_endpoint_mapping_t *epm =
|
---|
327 | usb_device_get_mapped_ep_desc(usb_dev, desc);
|
---|
328 | return usb_device_auto_polling_internal(usb_dev, epm, polling, req_size);
|
---|
329 | }
|
---|
330 |
|
---|
331 | int usb_device_auto_poll_desc(usb_device_t * usb_dev,
|
---|
332 | const usb_endpoint_description_t *desc, usb_polling_callback_t callback,
|
---|
333 | size_t req_size, int delay,
|
---|
334 | usb_polling_terminted_callback_t terminated_callback, void *arg)
|
---|
335 | {
|
---|
336 | const usb_device_auto_polling_t auto_polling = {
|
---|
337 | .debug = 1,
|
---|
338 | .auto_clear_halt = true,
|
---|
339 | .delay = delay,
|
---|
340 | .max_failures = MAX_FAILED_ATTEMPTS,
|
---|
341 | .on_data = callback,
|
---|
342 | .on_polling_end = terminated_callback,
|
---|
343 | .on_error = NULL,
|
---|
344 | .arg = arg,
|
---|
345 | };
|
---|
346 |
|
---|
347 | usb_endpoint_mapping_t *epm =
|
---|
348 | usb_device_get_mapped_ep_desc(usb_dev, desc);
|
---|
349 | return usb_device_auto_polling_internal(
|
---|
350 | usb_dev, epm, &auto_polling, req_size);
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * @}
|
---|
355 | */
|
---|