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

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

usb: Rework polling to accept either ep numbers or descriptions.

Switch usbhub and usbhid to new polling.

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