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

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

usbdev: fix race condition when unmapping endpoints

  • Property mode set to 100644
File size: 8.3 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * Copyright (c) 2017 Petr Manek
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libusbdev
31 * @{
32 */
33/** @file
34 * USB device driver framework - automatic interrupt polling.
35 */
36
37#include <usb/dev/device.h>
38#include <usb/dev/pipes.h>
39#include <usb/dev/poll.h>
40#include <usb/dev/request.h>
41#include <usb/classes/classes.h>
42#include <usb/debug.h>
43#include <usb/descriptor.h>
44#include <usb/usb.h>
45
46#include <assert.h>
47#include <async.h>
48#include <errno.h>
49#include <fibril.h>
50#include <fibril_synch.h>
51#include <stdbool.h>
52#include <stdlib.h>
53#include <str_error.h>
54#include <stddef.h>
55#include <stdint.h>
56
57
58/** Initialize the polling data structure, its internals and configuration
59 * with default values.
60 *
61 * @param polling Valid polling data structure.
62 * @return Error code.
63 * @retval EOK Polling data structure is ready to be used.
64 */
65int usb_polling_init(usb_polling_t *polling)
66{
67 if (!polling)
68 return EBADMEM;
69
70 /* Zero out everything */
71 memset(polling, 0, sizeof(usb_polling_t));
72
73 /* Internal initializers. */
74 fibril_mutex_initialize(&polling->guard);
75 fibril_condvar_initialize(&polling->cv);
76
77 /* Default configuration. */
78 polling->auto_clear_halt = true;
79 polling->delay = -1;
80 polling->max_failures = 3;
81
82 return EOK;
83}
84
85
86/** Destroy the polling data structure.
87 * This function does nothing but a safety check whether the polling
88 * was joined successfully.
89 *
90 * @param polling Polling data structure.
91 */
92void usb_polling_fini(usb_polling_t *polling)
93{
94 /* Nothing done at the moment. */
95 assert(polling);
96 assert(!polling->running);
97}
98
99
100/** Polling fibril.
101 *
102 * @param arg Pointer to usb_polling_t.
103 * @return Always EOK.
104 */
105static int polling_fibril(void *arg)
106{
107 assert(arg);
108 usb_polling_t *polling = arg;
109 polling->running = true;
110
111 usb_pipe_t *pipe = &polling->ep_mapping->pipe;
112
113 if (polling->debug > 0) {
114 const usb_endpoint_mapping_t *mapping =
115 polling->ep_mapping;
116 usb_log_debug("Poll (%p): started polling of `%s' - " \
117 "interface %d (%s,%d,%d), %zuB/%zu.\n",
118 polling, usb_device_get_name(polling->device),
119 (int) mapping->interface->interface_number,
120 usb_str_class(mapping->interface->interface_class),
121 (int) mapping->interface->interface_subclass,
122 (int) mapping->interface->interface_protocol,
123 polling->request_size, pipe->desc.max_transfer_size);
124 }
125
126 size_t failed_attempts = 0;
127 while (failed_attempts <= polling->max_failures) {
128 size_t actual_size;
129 const int rc = usb_pipe_read(pipe, polling->buffer,
130 polling->request_size, &actual_size);
131
132 if (rc == EOK) {
133 if (polling->debug > 1) {
134 usb_log_debug(
135 "Poll%p: received: '%s' (%zuB).\n",
136 polling,
137 usb_debug_str_buffer(polling->buffer,
138 actual_size, 16),
139 actual_size);
140 }
141 } else {
142 usb_log_debug(
143 "Poll%p: polling failed: %s.\n",
144 polling, str_error(rc));
145 }
146
147 /* If the pipe stalled, we can try to reset the stall. */
148 if (rc == ESTALL && polling->auto_clear_halt) {
149 /*
150 * We ignore error here as this is usually a futile
151 * attempt anyway.
152 */
153 usb_request_clear_endpoint_halt(
154 usb_device_get_default_pipe(polling->device),
155 pipe->desc.endpoint_no);
156 }
157
158 if (rc != EOK) {
159 ++failed_attempts;
160 const bool carry_on = !polling->on_error ? true :
161 polling->on_error(polling->device, rc, polling->arg);
162
163 if (!carry_on || polling->joining) {
164 /* This is user requested abort, erases failures. */
165 failed_attempts = 0;
166 break;
167 }
168 continue;
169 }
170
171 /* We have the data, execute the callback now. */
172 assert(polling->on_data);
173 const bool carry_on = polling->on_data(polling->device,
174 polling->buffer, actual_size, polling->arg);
175
176 if (!carry_on) {
177 /* This is user requested abort, erases failures. */
178 failed_attempts = 0;
179 break;
180 }
181
182 /* Reset as something might be only a temporary problem. */
183 failed_attempts = 0;
184
185 /* Take a rest before next request. */
186
187 // FIXME TODO: This is broken, the time is in ms not us.
188 // but first we need to fix drivers to actually stop using this,
189 // since polling delay should be implemented in HC schedule
190 async_usleep(polling->delay);
191 }
192
193 const bool failed = failed_attempts > 0;
194
195 if (polling->on_polling_end)
196 polling->on_polling_end(polling->device, failed, polling->arg);
197
198 if (polling->debug > 0) {
199 if (failed) {
200 usb_log_error("Polling of device `%s' terminated: "
201 "recurring failures.\n",
202 usb_device_get_name(polling->device));
203 } else {
204 usb_log_debug("Polling of device `%s' terminated: "
205 "driver request.\n",
206 usb_device_get_name(polling->device));
207 }
208 }
209
210 polling->running = false;
211
212 /* Notify joiners, if any. */
213 fibril_condvar_broadcast(&polling->cv);
214 return EOK;
215}
216
217
218/** Start automatic device polling over interrupt in pipe.
219 *
220 * The polling settings is copied thus it is okay to destroy the structure
221 * after this function returns.
222 *
223 * @warning There is no guarantee when the request to the device
224 * will be sent for the first time (it is possible that this
225 * first request would be executed prior to return from this function).
226 *
227 * @param polling Polling data structure.
228 * @return Error code.
229 * @retval EOK New fibril polling the device was already started.
230 */
231int usb_polling_start(usb_polling_t *polling)
232{
233 if (!polling || !polling->device || !polling->ep_mapping || !polling->on_data)
234 return EBADMEM;
235
236 if (!polling->request_size)
237 return EINVAL;
238
239 if (!polling->ep_mapping || (polling->ep_mapping->pipe.desc.transfer_type != USB_TRANSFER_INTERRUPT)
240 || (polling->ep_mapping->pipe.desc.direction != USB_DIRECTION_IN))
241 return EINVAL;
242
243 /* Negative value means use descriptor provided value. */
244 if (polling->delay < 0)
245 polling->delay = polling->ep_mapping->descriptor->poll_interval;
246
247 polling->fibril = fibril_create(polling_fibril, polling);
248 if (!polling->fibril)
249 return ENOMEM;
250
251 fibril_add_ready(polling->fibril);
252
253 /* Fibril launched. That fibril will free the allocated data. */
254 return EOK;
255}
256
257/** Close the polling pipe permanently and synchronously wait
258 * until the automatic polling fibril terminates.
259 *
260 * It is safe to deallocate the polling data structure (and its
261 * data buffer) only after a successful call to this function.
262 *
263 * @warning Call to this function will trigger execution of the
264 * on_error() callback with EINTR error code.
265 *
266 * @parram polling Polling data structure.
267 * @return Error code.
268 * @retval EOK Polling fibril has been successfully terminated.
269 */
270int usb_polling_join(usb_polling_t *polling)
271{
272 int rc;
273 if (!polling)
274 return EBADMEM;
275
276 /* Check if the fibril already terminated. */
277 if (!polling->running)
278 return EOK;
279
280 /* Set the flag */
281 polling->joining = true;
282
283 /* Unregister the pipe. */
284 rc = usb_device_unmap_ep(polling->ep_mapping);
285 if (rc != EOK && rc != ENOENT)
286 return rc;
287
288 /* Wait for the fibril to terminate. */
289 fibril_mutex_lock(&polling->guard);
290 while (polling->running)
291 fibril_condvar_wait(&polling->cv, &polling->guard);
292 fibril_mutex_unlock(&polling->guard);
293
294 return EOK;
295}
296
297/**
298 * @}
299 */
Note: See TracBrowser for help on using the repository browser.