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

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

libusbdev: Add TODO

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