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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since edc51615 was edc51615, checked in by Petr Manek <petr.manek@…>, 7 years ago

usbdev: small fixes

  • Property mode set to 100644
File size: 8.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
36#include <usb/dev/device.h>
37#include <usb/dev/pipes.h>
38#include <usb/dev/poll.h>
39#include <usb/dev/request.h>
40#include <usb/classes/classes.h>
41#include <usb/debug.h>
42#include <usb/descriptor.h>
43#include <usb/usb.h>
44
45#include <assert.h>
46#include <async.h>
47#include <errno.h>
48#include <fibril.h>
49#include <fibril_synch.h>
50#include <stdbool.h>
51#include <stdlib.h>
52#include <str_error.h>
53#include <stddef.h>
54#include <stdint.h>
55
56/** Private automated polling instance data. */
57struct usb_device_polling {
58 /** Parameters for automated polling. */
59 usb_device_polling_config_t config;
60
61 /** USB device to poll. */
62 usb_device_t *dev;
63
64 /** Device enpoint mapping to use for polling. */
65 usb_endpoint_mapping_t *ep_mapping;
66
67 /** Size of the recieved data. */
68 size_t request_size;
69
70 /** Data buffer. */
71 uint8_t *buffer;
72
73 /** True if polling is currently in operation. */
74 volatile bool running;
75
76 /** True if polling should terminate as soon as possible. */
77 volatile bool joining;
78
79 /** Synchronization primitives for joining polling end. */
80 fibril_mutex_t guard;
81 fibril_condvar_t cv;
82};
83
84
85static void polling_fini(usb_device_polling_t *polling)
86{
87 /* Free the allocated memory. */
88 free(polling->buffer);
89 free(polling);
90}
91
92
93/** Polling fibril.
94 *
95 * @param arg Pointer to usb_device_polling_t.
96 * @return Always EOK.
97 */
98static int polling_fibril(void *arg)
99{
100 assert(arg);
101 usb_device_polling_t *data = arg;
102 data->running = true;
103
104 /* Helper to reduce typing. */
105 const usb_device_polling_config_t *params = &data->config;
106
107 usb_pipe_t *pipe = &data->ep_mapping->pipe;
108
109 if (params->debug > 0) {
110 const usb_endpoint_mapping_t *mapping =
111 data->ep_mapping;
112 usb_log_debug("Poll (%p): started polling of `%s' - " \
113 "interface %d (%s,%d,%d), %zuB/%zu.\n",
114 data, usb_device_get_name(data->dev),
115 (int) mapping->interface->interface_number,
116 usb_str_class(mapping->interface->interface_class),
117 (int) mapping->interface->interface_subclass,
118 (int) mapping->interface->interface_protocol,
119 data->request_size, pipe->desc.max_transfer_size);
120 }
121
122 size_t failed_attempts = 0;
123 while (failed_attempts <= params->max_failures) {
124 size_t actual_size;
125 const int rc = usb_pipe_read(pipe, data->buffer,
126 data->request_size, &actual_size);
127
128 if (rc == EOK) {
129 if (params->debug > 1) {
130 usb_log_debug(
131 "Poll%p: received: '%s' (%zuB).\n",
132 data,
133 usb_debug_str_buffer(data->buffer,
134 actual_size, 16),
135 actual_size);
136 }
137 } else {
138 usb_log_debug(
139 "Poll%p: polling failed: %s.\n",
140 data, str_error(rc));
141 }
142
143 /* If the pipe stalled, we can try to reset the stall. */
144 if ((rc == ESTALL) && (params->auto_clear_halt)) {
145 /*
146 * We ignore error here as this is usually a futile
147 * attempt anyway.
148 */
149 usb_request_clear_endpoint_halt(
150 usb_device_get_default_pipe(data->dev),
151 pipe->desc.endpoint_no);
152 }
153
154 if (rc != EOK) {
155 ++failed_attempts;
156 const bool cont = (params->on_error == NULL) ? true :
157 params->on_error(data->dev, rc, params->arg);
158 if (!cont || data->joining) {
159 /* This is user requested abort, erases failures. */
160 failed_attempts = 0;
161 break;
162 }
163 continue;
164 }
165
166 /* We have the data, execute the callback now. */
167 assert(params->on_data);
168 const bool carry_on = params->on_data(
169 data->dev, data->buffer, actual_size, params->arg);
170
171 if (!carry_on) {
172 /* This is user requested abort, erases failures. */
173 failed_attempts = 0;
174 break;
175 }
176
177 /* Reset as something might be only a temporary problem. */
178 failed_attempts = 0;
179
180 /* Take a rest before next request. */
181
182 // FIXME TODO: This is broken, the time is in ms not us.
183 // but first we need to fix drivers to actually stop using this,
184 // since polling delay should be implemented in HC schedule
185 async_usleep(params->delay);
186 }
187
188 const bool failed = failed_attempts > 0;
189
190 if (params->on_polling_end != NULL) {
191 params->on_polling_end(data->dev, failed, params->arg);
192 }
193
194 if (params->debug > 0) {
195 if (failed) {
196 usb_log_error("Polling of device `%s' terminated: "
197 "recurring failures.\n",
198 usb_device_get_name(data->dev));
199 } else {
200 usb_log_debug("Polling of device `%s' terminated: "
201 "driver request.\n",
202 usb_device_get_name(data->dev));
203 }
204 }
205
206 data->running = false;
207
208 /* Notify joiners, if any. */
209 fibril_condvar_broadcast(&data->cv);
210
211 /* Free allocated memory. */
212 if (!data->joining) {
213 polling_fini(data);
214 }
215
216 return EOK;
217}
218
219
220/** Start automatic device polling over interrupt in pipe.
221 *
222 * The polling settings is copied thus it is okay to destroy the structure
223 * after this function returns.
224 *
225 * @warning There is no guarantee when the request to the device
226 * will be sent for the first time (it is possible that this
227 * first request would be executed prior to return from this function).
228 *
229 * @param dev Device to be periodically polled.
230 * @param epm Endpoint mapping to use.
231 * @param config Polling settings.
232 * @param req_size How many bytes to ask for in each request.
233 * @return Error code.
234 * @retval EOK New fibril polling the device was already started.
235 */
236int usb_device_poll(usb_device_t *dev, usb_endpoint_mapping_t *epm,
237 const usb_device_polling_config_t *config, size_t req_size,
238 usb_device_polling_t **handle)
239{
240 int rc;
241 if (!dev || !config || !config->on_data)
242 return EBADMEM;
243
244 if (!req_size)
245 return EINVAL;
246
247 if (!epm || (epm->pipe.desc.transfer_type != USB_TRANSFER_INTERRUPT) ||
248 (epm->pipe.desc.direction != USB_DIRECTION_IN))
249 return EINVAL;
250
251 usb_device_polling_t *instance = malloc(sizeof(usb_device_polling_t));
252 if (!instance)
253 return ENOMEM;
254
255 /* Fill-in the data. */
256 instance->buffer = malloc(req_size);
257 if (!instance->buffer) {
258 rc = ENOMEM;
259 goto err_instance;
260 }
261 instance->request_size = req_size;
262 instance->dev = dev;
263 instance->ep_mapping = epm;
264 instance->joining = false;
265 fibril_mutex_initialize(&instance->guard);
266 fibril_condvar_initialize(&instance->cv);
267
268 /* Copy provided settings. */
269 instance->config = *config;
270
271 /* Negative value means use descriptor provided value. */
272 if (config->delay < 0) {
273 instance->config.delay = epm->descriptor->poll_interval;
274 }
275
276 fid_t fibril = fibril_create(polling_fibril, instance);
277 if (!fibril) {
278 rc = ENOMEM;
279 goto err_buffer;
280 }
281 fibril_add_ready(fibril);
282
283 if (handle)
284 *handle = instance;
285
286 /* Fibril launched. That fibril will free the allocated data. */
287 return EOK;
288
289err_buffer:
290 free(instance->buffer);
291err_instance:
292 free(instance);
293 return rc;
294}
295
296int usb_device_poll_join(usb_device_polling_t *polling)
297{
298 int rc;
299 if (!polling)
300 return EBADMEM;
301
302 /* Set the flag */
303 polling->joining = true;
304
305 /* Unregister the pipe. */
306 if ((rc = usb_device_unmap_ep(polling->ep_mapping))) {
307 return rc;
308 }
309
310 /* Wait for the fibril to terminate. */
311 fibril_mutex_lock(&polling->guard);
312 while (polling->running)
313 fibril_condvar_wait(&polling->cv, &polling->guard);
314 fibril_mutex_unlock(&polling->guard);
315
316 /* Free the instance. */
317 polling_fini(polling);
318
319 return EOK;
320}
321
322/**
323 * @}
324 */
Note: See TracBrowser for help on using the repository browser.