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

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

libusbdev: Nest usb_device_auto_polling_t instead of duplicating its members.

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