source: mainline/uspace/lib/usbdev/src/devpoll.c@ c01987c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c01987c was c01987c, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libusbdev: Sanitize headers.

Include what you use.
https://code.google.com/p/include-what-you-use/

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