source: mainline/uspace/lib/usb/src/devpoll.c@ 8989f48f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8989f48f was 8989f48f, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Generalized automatic device polling

  • Property mode set to 100644
File size: 8.5 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
29/** @addtogroup libusb
30 * @{
31 */
32/** @file
33 * USB device driver framework - automatic interrupt polling.
34 */
[5e07e2b5]35#include <usb/devpoll.h>
[4bf94df]36#include <usb/request.h>
37#include <usb/debug.h>
38#include <errno.h>
39#include <str_error.h>
40#include <assert.h>
41
42/** Maximum number of failed consecutive requests before announcing failure. */
43#define MAX_FAILED_ATTEMPTS 3
44
45/** Data needed for polling. */
46typedef struct {
[8989f48f]47 size_t max_failures;
48 useconds_t delay;
49 bool auto_clear_halt;
50 bool (*on_data)(usb_device_t *, uint8_t *, size_t, void *);
51 void (*on_polling_end)(usb_device_t *, bool, void *);
52 bool (*on_error)(usb_device_t *, int, void *);
53
[4bf94df]54 usb_device_t *dev;
55 size_t pipe_index;
56 size_t request_size;
57 uint8_t *buffer;
58 void *custom_arg;
59} polling_data_t;
60
[8989f48f]61
[4bf94df]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 polling_data_t *polling_data = (polling_data_t *) arg;
70 assert(polling_data);
71
[a372663]72 usb_pipe_t *pipe
[4bf94df]73 = polling_data->dev->pipes[polling_data->pipe_index].pipe;
[7f2e33a]74
75 usb_log_debug("Pipe interface number: %d, protocol: %d, subclass: %d, max packet size: %d\n",
76 polling_data->dev->pipes[polling_data->pipe_index].interface_no,
77 polling_data->dev->pipes[polling_data->pipe_index].description->interface_protocol,
78 polling_data->dev->pipes[polling_data->pipe_index].description->interface_subclass,
79 pipe->max_packet_size);
[4bf94df]80
81 size_t failed_attempts = 0;
[8989f48f]82 while (failed_attempts <= polling_data->max_failures) {
[4bf94df]83 int rc;
84
85 size_t actual_size;
[3954a63b]86 rc = usb_pipe_read(pipe, polling_data->buffer,
[4bf94df]87 polling_data->request_size, &actual_size);
88
[8989f48f]89 /* If the pipe stalled, we can try to reset the stall. */
90 if ((rc == ESTALL) && (polling_data->auto_clear_halt)) {
91 /*
92 * We ignore error here as this is usually a futile
93 * attempt anyway.
94 */
95 usb_request_clear_endpoint_halt(
96 &polling_data->dev->ctrl_pipe,
97 pipe->endpoint_no);
98 }
[4bf94df]99
100 if (rc != EOK) {
[8989f48f]101 if (polling_data->on_error != NULL) {
102 bool cont = polling_data->on_error(
103 polling_data->dev, rc,
104 polling_data->custom_arg);
105 if (!cont) {
106 failed_attempts
107 = polling_data->max_failures;
108 }
109 }
[4bf94df]110 failed_attempts++;
111 continue;
112 }
113
114 /* We have the data, execute the callback now. */
[8989f48f]115 bool carry_on = polling_data->on_data(polling_data->dev,
[4bf94df]116 polling_data->buffer, actual_size,
117 polling_data->custom_arg);
118
119 if (!carry_on) {
120 failed_attempts = 0;
121 break;
122 }
123
124 /* Reset as something might be only a temporary problem. */
125 failed_attempts = 0;
[8989f48f]126
127 /* Take a rest before next request. */
128 async_usleep(polling_data->delay);
[4bf94df]129 }
130
131 if (failed_attempts > 0) {
132 usb_log_error(
133 "Polling of device `%s' terminated: recurring failures.\n",
134 polling_data->dev->ddf_dev->name);
135 }
136
[8989f48f]137 if (polling_data->on_polling_end != NULL) {
138 polling_data->on_polling_end(polling_data->dev,
[4bf94df]139 failed_attempts > 0, polling_data->custom_arg);
140 }
141
142 /* Free the allocated memory. */
143 free(polling_data->buffer);
144 free(polling_data);
145
146 return EOK;
147}
148
149/** Start automatic device polling over interrupt in pipe.
150 *
151 * @warning It is up to the callback to produce delays between individual
152 * requests.
153 *
154 * @warning There is no guarantee when the request to the device
155 * will be sent for the first time (it is possible that this
156 * first request would be executed prior to return from this function).
157 *
158 * @param dev Device to be periodically polled.
159 * @param pipe_index Index of the endpoint pipe used for polling.
160 * @param callback Callback when data are available.
161 * @param request_size How many bytes to ask for in each request.
162 * @param terminated_callback Callback when polling is terminated.
163 * @param arg Custom argument (passed as is to the callbacks).
164 * @return Error code.
165 * @retval EOK New fibril polling the device was already started.
166 */
167int usb_device_auto_poll(usb_device_t *dev, size_t pipe_index,
168 usb_polling_callback_t callback, size_t request_size,
169 usb_polling_terminted_callback_t terminated_callback, void *arg)
170{
171 if ((dev == NULL) || (callback == NULL)) {
172 return EBADMEM;
173 }
174 if (request_size == 0) {
175 return EINVAL;
176 }
177 if ((dev->pipes[pipe_index].pipe->transfer_type != USB_TRANSFER_INTERRUPT)
178 || (dev->pipes[pipe_index].pipe->direction != USB_DIRECTION_IN)) {
179 return EINVAL;
180 }
181
[8989f48f]182 usb_device_auto_polling_t *auto_polling
183 = malloc(sizeof(usb_device_auto_polling_t));
184 if (auto_polling == NULL) {
185 return ENOMEM;
186 }
187
188 auto_polling->auto_clear_halt = true;
189 auto_polling->delay = 0;
190 auto_polling->max_failures = MAX_FAILED_ATTEMPTS;
191 auto_polling->on_data = callback;
192 auto_polling->on_polling_end = terminated_callback;
193 auto_polling->on_error = NULL;
194
195 int rc = usb_device_auto_polling(dev, pipe_index, auto_polling,
196 request_size, arg);
197
198 free(auto_polling);
199
200 return rc;
201}
202
203/** Start automatic device polling over interrupt in pipe.
204 *
205 * The polling settings is copied thus it is okay to destroy the structure
206 * after this function returns.
207 *
208 * @warning There is no guarantee when the request to the device
209 * will be sent for the first time (it is possible that this
210 * first request would be executed prior to return from this function).
211 *
212 * @param dev Device to be periodically polled.
213 * @param pipe_index Index of the endpoint pipe used for polling.
214 * @param polling Polling settings.
215 * @param request_size How many bytes to ask for in each request.
216 * @param arg Custom argument (passed as is to the callbacks).
217 * @return Error code.
218 * @retval EOK New fibril polling the device was already started.
219 */
220int usb_device_auto_polling(usb_device_t *dev, size_t pipe_index,
221 usb_device_auto_polling_t *polling,
222 size_t request_size, void *arg)
223{
224 if (dev == NULL) {
225 return EBADMEM;
226 }
227 if (pipe_index >= dev->pipes_count) {
228 return EINVAL;
229 }
230 if ((dev->pipes[pipe_index].pipe->transfer_type != USB_TRANSFER_INTERRUPT)
231 || (dev->pipes[pipe_index].pipe->direction != USB_DIRECTION_IN)) {
232 return EINVAL;
233 }
234 if ((polling == NULL) || (polling->on_data == NULL)) {
235 return EBADMEM;
236 }
237
[4bf94df]238 polling_data_t *polling_data = malloc(sizeof(polling_data_t));
239 if (polling_data == NULL) {
240 return ENOMEM;
241 }
242
[8989f48f]243 /* Fill-in the data. */
244 polling_data->buffer = malloc(sizeof(request_size));
[4bf94df]245 if (polling_data->buffer == NULL) {
246 free(polling_data);
247 return ENOMEM;
248 }
[8989f48f]249 polling_data->request_size = request_size;
[4bf94df]250 polling_data->dev = dev;
251 polling_data->pipe_index = pipe_index;
252 polling_data->custom_arg = arg;
253
[8989f48f]254 polling_data->max_failures = polling->max_failures;
255 if (polling->delay >= 0) {
256 polling_data->delay = (useconds_t) polling->delay;
257 } else {
258 polling_data->delay = (useconds_t) dev->pipes[pipe_index]
259 .descriptor->poll_interval;
260 }
261 polling_data->auto_clear_halt = polling->auto_clear_halt;
262
263 polling_data->on_data = polling->on_data;
264 polling_data->on_polling_end = polling->on_polling_end;
265 polling_data->on_error = polling->on_error;
266
[4bf94df]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
[8989f48f]275 /* Fibril launched. That fibril will free the allocated data. */
[4bf94df]276
277 return EOK;
278}
279
280/**
281 * @}
282 */
Note: See TracBrowser for help on using the repository browser.