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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cc74cb5 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[4bf94df]1/*
2 * Copyright (c) 2011 Vojtech Horky
[338729c]3 * Copyright (c) 2017 Petr Manek
[4bf94df]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
[160b75e]30/** @addtogroup libusbdev
[4bf94df]31 * @{
32 */
33/** @file
34 * USB device driver framework - automatic interrupt polling.
35 */
[58563585]36
[c01987c]37#include <usb/dev/device.h>
38#include <usb/dev/pipes.h>
[7d521e24]39#include <usb/dev/poll.h>
40#include <usb/dev/request.h>
[5e168be1]41#include <usb/classes/classes.h>
[c01987c]42#include <usb/debug.h>
43#include <usb/descriptor.h>
44#include <usb/usb.h>
45
46#include <assert.h>
47#include <async.h>
[4bf94df]48#include <errno.h>
[c01987c]49#include <fibril.h>
[8a0c52a]50#include <fibril_synch.h>
[c01987c]51#include <stdbool.h>
52#include <stdlib.h>
[4bf94df]53#include <str_error.h>
[8d2dd7f2]54#include <stddef.h>
55#include <stdint.h>
[4bf94df]56
[8b71f3e]57/** Initialize the polling data structure, its internals and configuration
58 * with default values.
59 *
60 * @param polling Valid polling data structure.
61 * @return Error code.
62 * @retval EOK Polling data structure is ready to be used.
63 */
64int usb_polling_init(usb_polling_t *polling)
65{
66 if (!polling)
67 return EBADMEM;
[71f211f]68
[8b71f3e]69 /* Zero out everything */
70 memset(polling, 0, sizeof(usb_polling_t));
[8a0c52a]71
[8b71f3e]72 /* Internal initializers. */
73 fibril_mutex_initialize(&polling->guard);
74 fibril_condvar_initialize(&polling->cv);
[8a0c52a]75
[8b71f3e]76 /* Default configuration. */
77 polling->auto_clear_halt = true;
78 polling->delay = -1;
79 polling->max_failures = 3;
[8a0c52a]80
[8b71f3e]81 return EOK;
82}
[4bf94df]83
[8b71f3e]84/** Destroy the polling data structure.
85 * This function does nothing but a safety check whether the polling
86 * was joined successfully.
87 *
88 * @param polling Polling data structure.
89 */
90void usb_polling_fini(usb_polling_t *polling)
[8a0c52a]91{
[8b71f3e]92 /* Nothing done at the moment. */
93 assert(polling);
94 assert(!polling->running);
[8a0c52a]95}
96
[4bf94df]97/** Polling fibril.
98 *
[8b71f3e]99 * @param arg Pointer to usb_polling_t.
[4bf94df]100 * @return Always EOK.
101 */
[5a6cc679]102static errno_t polling_fibril(void *arg)
[4bf94df]103{
[9dbfd288]104 assert(arg);
[8b71f3e]105 usb_polling_t *polling = arg;
[4e44f5d]106
107 fibril_mutex_lock(&polling->guard);
[8b71f3e]108 polling->running = true;
[4e44f5d]109 fibril_mutex_unlock(&polling->guard);
[71f211f]110
[8b71f3e]111 usb_pipe_t *pipe = &polling->ep_mapping->pipe;
[4bf94df]112
[8b71f3e]113 if (polling->debug > 0) {
[58563585]114 const usb_endpoint_mapping_t *mapping =
[8b71f3e]115 polling->ep_mapping;
[5ef16903]116 usb_log_debug("Poll (%p): started polling of `%s' - "
[5e168be1]117 "interface %d (%s,%d,%d), %zuB/%zu.\n",
[8b71f3e]118 polling, usb_device_get_name(polling->device),
[5e168be1]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,
[8b71f3e]123 polling->request_size, pipe->desc.max_transfer_size);
[5e168be1]124 }
[4bf94df]125
126 size_t failed_attempts = 0;
[8b71f3e]127 while (failed_attempts <= polling->max_failures) {
[4bf94df]128 size_t actual_size;
[5a6cc679]129 const errno_t rc = usb_pipe_read(pipe, polling->buffer,
[8b71f3e]130 polling->request_size, &actual_size);
[4bf94df]131
[51cc6cef]132 if (rc == EOK) {
[8b71f3e]133 if (polling->debug > 1) {
[5e168be1]134 usb_log_debug(
[4125b7d]135 "Poll%p: received: '%s' (%zuB).\n",
[8b71f3e]136 polling,
137 usb_debug_str_buffer(polling->buffer,
[3bacee1]138 actual_size, 16),
[5e168be1]139 actual_size);
[51cc6cef]140 }
141 } else {
[3bacee1]142 usb_log_debug(
143 "Poll%p: polling failed: %s.\n",
144 polling, str_error(rc));
[5e168be1]145 }
146
[8989f48f]147 /* If the pipe stalled, we can try to reset the stall. */
[8b71f3e]148 if (rc == ESTALL && polling->auto_clear_halt) {
[8989f48f]149 /*
150 * We ignore error here as this is usually a futile
151 * attempt anyway.
152 */
[d08aa42d]153 usb_pipe_clear_halt(
154 usb_device_get_default_pipe(polling->device), pipe);
[8989f48f]155 }
[4bf94df]156
157 if (rc != EOK) {
[9dbfd288]158 ++failed_attempts;
[8b71f3e]159 const bool carry_on = !polling->on_error ? true :
160 polling->on_error(polling->device, rc, polling->arg);
161
162 if (!carry_on || polling->joining) {
[a4eb520f]163 /* This is user requested abort, erases failures. */
164 failed_attempts = 0;
165 break;
[8989f48f]166 }
[4bf94df]167 continue;
168 }
169
170 /* We have the data, execute the callback now. */
[8b71f3e]171 assert(polling->on_data);
172 const bool carry_on = polling->on_data(polling->device,
173 polling->buffer, actual_size, polling->arg);
[4bf94df]174
175 if (!carry_on) {
[9dbfd288]176 /* This is user requested abort, erases failures. */
[4bf94df]177 failed_attempts = 0;
178 break;
179 }
180
181 /* Reset as something might be only a temporary problem. */
182 failed_attempts = 0;
[8989f48f]183
184 /* Take a rest before next request. */
[816f5f4]185
[58563585]186 // FIXME TODO: This is broken, the time is in ms not us.
[ff0258f]187 // but first we need to fix drivers to actually stop using this,
[58563585]188 // since polling delay should be implemented in HC schedule
[5f97ef44]189 fibril_usleep(polling->delay);
[4bf94df]190 }
191
[9dbfd288]192 const bool failed = failed_attempts > 0;
193
[8b71f3e]194 if (polling->on_polling_end)
195 polling->on_polling_end(polling->device, failed, polling->arg);
[4bf94df]196
[8b71f3e]197 if (polling->debug > 0) {
[9dbfd288]198 if (failed) {
199 usb_log_error("Polling of device `%s' terminated: "
[f6b2a76b]200 "recurring failures.\n",
[8b71f3e]201 usb_device_get_name(polling->device));
[5e168be1]202 } else {
[9dbfd288]203 usb_log_debug("Polling of device `%s' terminated: "
[f6b2a76b]204 "driver request.\n",
[8b71f3e]205 usb_device_get_name(polling->device));
[5e168be1]206 }
207 }
208
[4e44f5d]209 fibril_mutex_lock(&polling->guard);
[8b71f3e]210 polling->running = false;
[4e44f5d]211 fibril_mutex_unlock(&polling->guard);
[8a0c52a]212
[edc51615]213 /* Notify joiners, if any. */
[8b71f3e]214 fibril_condvar_broadcast(&polling->cv);
[4bf94df]215 return EOK;
216}
217
[8989f48f]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 *
[8b71f3e]227 * @param polling Polling data structure.
[8989f48f]228 * @return Error code.
229 * @retval EOK New fibril polling the device was already started.
230 */
[5a6cc679]231errno_t usb_polling_start(usb_polling_t *polling)
[8989f48f]232{
[8b71f3e]233 if (!polling || !polling->device || !polling->ep_mapping || !polling->on_data)
[8989f48f]234 return EBADMEM;
[b81410f]235
[8b71f3e]236 if (!polling->request_size)
[8989f48f]237 return EINVAL;
[816f5f4]238
[3bacee1]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))
[8989f48f]241 return EINVAL;
[816f5f4]242
[2703331b]243 /* Negative value means use descriptor provided value. */
[8b71f3e]244 if (polling->delay < 0)
245 polling->delay = polling->ep_mapping->descriptor->poll_interval;
[8989f48f]246
[8b71f3e]247 polling->fibril = fibril_create(polling_fibril, polling);
248 if (!polling->fibril)
249 return ENOMEM;
[4bf94df]250
[8b71f3e]251 fibril_add_ready(polling->fibril);
[71f211f]252
[8989f48f]253 /* Fibril launched. That fibril will free the allocated data. */
[4bf94df]254 return EOK;
[bb70637]255}
[4bf94df]256
[8b71f3e]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 */
[5a6cc679]270errno_t usb_polling_join(usb_polling_t *polling)
[8a0c52a]271{
[5a6cc679]272 errno_t rc;
[8a0c52a]273 if (!polling)
274 return EBADMEM;
275
[8b71f3e]276 /* Check if the fibril already terminated. */
277 if (!polling->running)
278 return EOK;
279
[8a0c52a]280 /* Set the flag */
281 polling->joining = true;
282
283 /* Unregister the pipe. */
[338729c]284 rc = usb_device_unmap_ep(polling->ep_mapping);
[3f44312]285 if (rc != EOK && rc != ENOENT && rc != EHANGUP)
[8a0c52a]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
[4bf94df]297/**
298 * @}
299 */
Note: See TracBrowser for help on using the repository browser.