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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ba654f2 was 3f44312, checked in by Ondřej Hlavatý <aearsis@…>, 7 years ago

usbdev: ignore also EHANGUP when joining polling fibril

  • 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
[8989f48f]57
[8b71f3e]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;
[71f211f]69
[8b71f3e]70 /* Zero out everything */
71 memset(polling, 0, sizeof(usb_polling_t));
[8a0c52a]72
[8b71f3e]73 /* Internal initializers. */
74 fibril_mutex_initialize(&polling->guard);
75 fibril_condvar_initialize(&polling->cv);
[8a0c52a]76
[8b71f3e]77 /* Default configuration. */
78 polling->auto_clear_halt = true;
79 polling->delay = -1;
80 polling->max_failures = 3;
[8a0c52a]81
[8b71f3e]82 return EOK;
83}
[4bf94df]84
[8989f48f]85
[8b71f3e]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)
[8a0c52a]93{
[8b71f3e]94 /* Nothing done at the moment. */
95 assert(polling);
96 assert(!polling->running);
[8a0c52a]97}
98
99
[4bf94df]100/** Polling fibril.
101 *
[8b71f3e]102 * @param arg Pointer to usb_polling_t.
[4bf94df]103 * @return Always EOK.
104 */
105static int polling_fibril(void *arg)
106{
[9dbfd288]107 assert(arg);
[8b71f3e]108 usb_polling_t *polling = arg;
[4e44f5d]109
110 fibril_mutex_lock(&polling->guard);
[8b71f3e]111 polling->running = true;
[4e44f5d]112 fibril_mutex_unlock(&polling->guard);
[71f211f]113
[8b71f3e]114 usb_pipe_t *pipe = &polling->ep_mapping->pipe;
[4bf94df]115
[8b71f3e]116 if (polling->debug > 0) {
[58563585]117 const usb_endpoint_mapping_t *mapping =
[8b71f3e]118 polling->ep_mapping;
[b303275]119 usb_log_debug("Poll (%p): started polling of `%s' - " \
[5e168be1]120 "interface %d (%s,%d,%d), %zuB/%zu.\n",
[8b71f3e]121 polling, usb_device_get_name(polling->device),
[5e168be1]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,
[8b71f3e]126 polling->request_size, pipe->desc.max_transfer_size);
[5e168be1]127 }
[4bf94df]128
129 size_t failed_attempts = 0;
[8b71f3e]130 while (failed_attempts <= polling->max_failures) {
[4bf94df]131 size_t actual_size;
[8b71f3e]132 const int rc = usb_pipe_read(pipe, polling->buffer,
133 polling->request_size, &actual_size);
[4bf94df]134
[51cc6cef]135 if (rc == EOK) {
[8b71f3e]136 if (polling->debug > 1) {
[5e168be1]137 usb_log_debug(
[4125b7d]138 "Poll%p: received: '%s' (%zuB).\n",
[8b71f3e]139 polling,
140 usb_debug_str_buffer(polling->buffer,
[5e168be1]141 actual_size, 16),
142 actual_size);
[51cc6cef]143 }
144 } else {
[5e168be1]145 usb_log_debug(
[4125b7d]146 "Poll%p: polling failed: %s.\n",
[8b71f3e]147 polling, str_error(rc));
[5e168be1]148 }
149
[8989f48f]150 /* If the pipe stalled, we can try to reset the stall. */
[8b71f3e]151 if (rc == ESTALL && polling->auto_clear_halt) {
[8989f48f]152 /*
153 * We ignore error here as this is usually a futile
154 * attempt anyway.
155 */
156 usb_request_clear_endpoint_halt(
[8b71f3e]157 usb_device_get_default_pipe(polling->device),
[816f5f4]158 pipe->desc.endpoint_no);
[8989f48f]159 }
[4bf94df]160
161 if (rc != EOK) {
[9dbfd288]162 ++failed_attempts;
[8b71f3e]163 const bool carry_on = !polling->on_error ? true :
164 polling->on_error(polling->device, rc, polling->arg);
165
166 if (!carry_on || polling->joining) {
[a4eb520f]167 /* This is user requested abort, erases failures. */
168 failed_attempts = 0;
169 break;
[8989f48f]170 }
[4bf94df]171 continue;
172 }
173
174 /* We have the data, execute the callback now. */
[8b71f3e]175 assert(polling->on_data);
176 const bool carry_on = polling->on_data(polling->device,
177 polling->buffer, actual_size, polling->arg);
[4bf94df]178
179 if (!carry_on) {
[9dbfd288]180 /* This is user requested abort, erases failures. */
[4bf94df]181 failed_attempts = 0;
182 break;
183 }
184
185 /* Reset as something might be only a temporary problem. */
186 failed_attempts = 0;
[8989f48f]187
188 /* Take a rest before next request. */
[816f5f4]189
[58563585]190 // FIXME TODO: This is broken, the time is in ms not us.
[ff0258f]191 // but first we need to fix drivers to actually stop using this,
[58563585]192 // since polling delay should be implemented in HC schedule
[8b71f3e]193 async_usleep(polling->delay);
[4bf94df]194 }
195
[9dbfd288]196 const bool failed = failed_attempts > 0;
197
[8b71f3e]198 if (polling->on_polling_end)
199 polling->on_polling_end(polling->device, failed, polling->arg);
[4bf94df]200
[8b71f3e]201 if (polling->debug > 0) {
[9dbfd288]202 if (failed) {
203 usb_log_error("Polling of device `%s' terminated: "
[f6b2a76b]204 "recurring failures.\n",
[8b71f3e]205 usb_device_get_name(polling->device));
[5e168be1]206 } else {
[9dbfd288]207 usb_log_debug("Polling of device `%s' terminated: "
[f6b2a76b]208 "driver request.\n",
[8b71f3e]209 usb_device_get_name(polling->device));
[5e168be1]210 }
211 }
212
[4e44f5d]213 fibril_mutex_lock(&polling->guard);
[8b71f3e]214 polling->running = false;
[4e44f5d]215 fibril_mutex_unlock(&polling->guard);
[8a0c52a]216
[edc51615]217 /* Notify joiners, if any. */
[8b71f3e]218 fibril_condvar_broadcast(&polling->cv);
[4bf94df]219 return EOK;
220}
221
[8989f48f]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 *
[8b71f3e]232 * @param polling Polling data structure.
[8989f48f]233 * @return Error code.
234 * @retval EOK New fibril polling the device was already started.
235 */
[8b71f3e]236int usb_polling_start(usb_polling_t *polling)
[8989f48f]237{
[8b71f3e]238 if (!polling || !polling->device || !polling->ep_mapping || !polling->on_data)
[8989f48f]239 return EBADMEM;
[b81410f]240
[8b71f3e]241 if (!polling->request_size)
[8989f48f]242 return EINVAL;
[816f5f4]243
[8b71f3e]244 if (!polling->ep_mapping || (polling->ep_mapping->pipe.desc.transfer_type != USB_TRANSFER_INTERRUPT)
245 || (polling->ep_mapping->pipe.desc.direction != USB_DIRECTION_IN))
[8989f48f]246 return EINVAL;
[816f5f4]247
[2703331b]248 /* Negative value means use descriptor provided value. */
[8b71f3e]249 if (polling->delay < 0)
250 polling->delay = polling->ep_mapping->descriptor->poll_interval;
[8989f48f]251
[8b71f3e]252 polling->fibril = fibril_create(polling_fibril, polling);
253 if (!polling->fibril)
254 return ENOMEM;
[4bf94df]255
[8b71f3e]256 fibril_add_ready(polling->fibril);
[71f211f]257
[8989f48f]258 /* Fibril launched. That fibril will free the allocated data. */
[4bf94df]259 return EOK;
[bb70637]260}
[4bf94df]261
[8b71f3e]262/** Close the polling pipe permanently and synchronously wait
263 * until the automatic polling fibril terminates.
264 *
265 * It is safe to deallocate the polling data structure (and its
266 * data buffer) only after a successful call to this function.
267 *
268 * @warning Call to this function will trigger execution of the
269 * on_error() callback with EINTR error code.
270 *
271 * @parram polling Polling data structure.
272 * @return Error code.
273 * @retval EOK Polling fibril has been successfully terminated.
274 */
275int usb_polling_join(usb_polling_t *polling)
[8a0c52a]276{
277 int rc;
278 if (!polling)
279 return EBADMEM;
280
[8b71f3e]281 /* Check if the fibril already terminated. */
282 if (!polling->running)
283 return EOK;
284
[8a0c52a]285 /* Set the flag */
286 polling->joining = true;
287
288 /* Unregister the pipe. */
[338729c]289 rc = usb_device_unmap_ep(polling->ep_mapping);
[3f44312]290 if (rc != EOK && rc != ENOENT && rc != EHANGUP)
[8a0c52a]291 return rc;
292
293 /* Wait for the fibril to terminate. */
294 fibril_mutex_lock(&polling->guard);
295 while (polling->running)
296 fibril_condvar_wait(&polling->cv, &polling->guard);
297 fibril_mutex_unlock(&polling->guard);
298
299 return EOK;
300}
301
[4bf94df]302/**
303 * @}
304 */
Note: See TracBrowser for help on using the repository browser.