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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d758fc was 5a6cc679, checked in by Jenda <jenda.jzqk73@…>, 7 years ago

Merge commit '50f19b7ee8e94570b5c63896736c4eb49cfa18db' into forwardport

Not all ints are converted to errno_t in xhci tree yet, however it compiles and works :)

  • Property mode set to 100644
File size: 8.5 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 errno_t polling_fibril(void *arg)
106{
107 assert(arg);
108 usb_polling_t *polling = arg;
109
110 fibril_mutex_lock(&polling->guard);
111 polling->running = true;
112 fibril_mutex_unlock(&polling->guard);
113
114 usb_pipe_t *pipe = &polling->ep_mapping->pipe;
115
116 if (polling->debug > 0) {
117 const usb_endpoint_mapping_t *mapping =
118 polling->ep_mapping;
119 usb_log_debug("Poll (%p): started polling of `%s' - " \
120 "interface %d (%s,%d,%d), %zuB/%zu.\n",
121 polling, usb_device_get_name(polling->device),
122 (int) mapping->interface->interface_number,
123 usb_str_class(mapping->interface->interface_class),
124 (int) mapping->interface->interface_subclass,
125 (int) mapping->interface->interface_protocol,
126 polling->request_size, pipe->desc.max_transfer_size);
127 }
128
129 size_t failed_attempts = 0;
130 while (failed_attempts <= polling->max_failures) {
131 size_t actual_size;
132 const errno_t rc = usb_pipe_read(pipe, polling->buffer,
133 polling->request_size, &actual_size);
134
135 if (rc == EOK) {
136 if (polling->debug > 1) {
137 usb_log_debug(
138 "Poll%p: received: '%s' (%zuB).\n",
139 polling,
140 usb_debug_str_buffer(polling->buffer,
141 actual_size, 16),
142 actual_size);
143 }
144 } else {
145 usb_log_debug(
146 "Poll%p: polling failed: %s.\n",
147 polling, str_error(rc));
148 }
149
150 /* If the pipe stalled, we can try to reset the stall. */
151 if (rc == ESTALL && polling->auto_clear_halt) {
152 /*
153 * We ignore error here as this is usually a futile
154 * attempt anyway.
155 */
156 usb_pipe_clear_halt(
157 usb_device_get_default_pipe(polling->device), pipe);
158 }
159
160 if (rc != EOK) {
161 ++failed_attempts;
162 const bool carry_on = !polling->on_error ? true :
163 polling->on_error(polling->device, rc, polling->arg);
164
165 if (!carry_on || polling->joining) {
166 /* This is user requested abort, erases failures. */
167 failed_attempts = 0;
168 break;
169 }
170 continue;
171 }
172
173 /* We have the data, execute the callback now. */
174 assert(polling->on_data);
175 const bool carry_on = polling->on_data(polling->device,
176 polling->buffer, actual_size, polling->arg);
177
178 if (!carry_on) {
179 /* This is user requested abort, erases failures. */
180 failed_attempts = 0;
181 break;
182 }
183
184 /* Reset as something might be only a temporary problem. */
185 failed_attempts = 0;
186
187 /* Take a rest before next request. */
188
189 // FIXME TODO: This is broken, the time is in ms not us.
190 // but first we need to fix drivers to actually stop using this,
191 // since polling delay should be implemented in HC schedule
192 async_usleep(polling->delay);
193 }
194
195 const bool failed = failed_attempts > 0;
196
197 if (polling->on_polling_end)
198 polling->on_polling_end(polling->device, failed, polling->arg);
199
200 if (polling->debug > 0) {
201 if (failed) {
202 usb_log_error("Polling of device `%s' terminated: "
203 "recurring failures.\n",
204 usb_device_get_name(polling->device));
205 } else {
206 usb_log_debug("Polling of device `%s' terminated: "
207 "driver request.\n",
208 usb_device_get_name(polling->device));
209 }
210 }
211
212 fibril_mutex_lock(&polling->guard);
213 polling->running = false;
214 fibril_mutex_unlock(&polling->guard);
215
216 /* Notify joiners, if any. */
217 fibril_condvar_broadcast(&polling->cv);
218 return EOK;
219}
220
221
222/** Start automatic device polling over interrupt in pipe.
223 *
224 * The polling settings is copied thus it is okay to destroy the structure
225 * after this function returns.
226 *
227 * @warning There is no guarantee when the request to the device
228 * will be sent for the first time (it is possible that this
229 * first request would be executed prior to return from this function).
230 *
231 * @param polling Polling data structure.
232 * @return Error code.
233 * @retval EOK New fibril polling the device was already started.
234 */
235errno_t usb_polling_start(usb_polling_t *polling)
236{
237 if (!polling || !polling->device || !polling->ep_mapping || !polling->on_data)
238 return EBADMEM;
239
240 if (!polling->request_size)
241 return EINVAL;
242
243 if (!polling->ep_mapping || (polling->ep_mapping->pipe.desc.transfer_type != USB_TRANSFER_INTERRUPT)
244 || (polling->ep_mapping->pipe.desc.direction != USB_DIRECTION_IN))
245 return EINVAL;
246
247 /* Negative value means use descriptor provided value. */
248 if (polling->delay < 0)
249 polling->delay = polling->ep_mapping->descriptor->poll_interval;
250
251 polling->fibril = fibril_create(polling_fibril, polling);
252 if (!polling->fibril)
253 return ENOMEM;
254
255 fibril_add_ready(polling->fibril);
256
257 /* Fibril launched. That fibril will free the allocated data. */
258 return EOK;
259}
260
261/** Close the polling pipe permanently and synchronously wait
262 * until the automatic polling fibril terminates.
263 *
264 * It is safe to deallocate the polling data structure (and its
265 * data buffer) only after a successful call to this function.
266 *
267 * @warning Call to this function will trigger execution of the
268 * on_error() callback with EINTR error code.
269 *
270 * @parram polling Polling data structure.
271 * @return Error code.
272 * @retval EOK Polling fibril has been successfully terminated.
273 */
274errno_t usb_polling_join(usb_polling_t *polling)
275{
276 errno_t rc;
277 if (!polling)
278 return EBADMEM;
279
280 /* Check if the fibril already terminated. */
281 if (!polling->running)
282 return EOK;
283
284 /* Set the flag */
285 polling->joining = true;
286
287 /* Unregister the pipe. */
288 rc = usb_device_unmap_ep(polling->ep_mapping);
289 if (rc != EOK && rc != ENOENT && rc != EHANGUP)
290 return rc;
291
292 /* Wait for the fibril to terminate. */
293 fibril_mutex_lock(&polling->guard);
294 while (polling->running)
295 fibril_condvar_wait(&polling->cv, &polling->guard);
296 fibril_mutex_unlock(&polling->guard);
297
298 return EOK;
299}
300
301/**
302 * @}
303 */
Note: See TracBrowser for help on using the repository browser.