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