| 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 |
|
|---|
| 29 | /** @addtogroup libusbdev
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * USB device driver framework - automatic interrupt polling.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <usb/dev/device.h>
|
|---|
| 37 | #include <usb/dev/pipes.h>
|
|---|
| 38 | #include <usb/dev/poll.h>
|
|---|
| 39 | #include <usb/dev/request.h>
|
|---|
| 40 | #include <usb/classes/classes.h>
|
|---|
| 41 | #include <usb/debug.h>
|
|---|
| 42 | #include <usb/descriptor.h>
|
|---|
| 43 | #include <usb/usb.h>
|
|---|
| 44 |
|
|---|
| 45 | #include <assert.h>
|
|---|
| 46 | #include <async.h>
|
|---|
| 47 | #include <errno.h>
|
|---|
| 48 | #include <fibril.h>
|
|---|
| 49 | #include <fibril_synch.h>
|
|---|
| 50 | #include <stdbool.h>
|
|---|
| 51 | #include <stdlib.h>
|
|---|
| 52 | #include <str_error.h>
|
|---|
| 53 | #include <stddef.h>
|
|---|
| 54 | #include <stdint.h>
|
|---|
| 55 |
|
|---|
| 56 | /** Private automated polling instance data. */
|
|---|
| 57 | struct usb_device_polling {
|
|---|
| 58 | /** Parameters for automated polling. */
|
|---|
| 59 | usb_device_polling_config_t config;
|
|---|
| 60 |
|
|---|
| 61 | /** USB device to poll. */
|
|---|
| 62 | usb_device_t *dev;
|
|---|
| 63 |
|
|---|
| 64 | /** Device enpoint mapping to use for polling. */
|
|---|
| 65 | usb_endpoint_mapping_t *ep_mapping;
|
|---|
| 66 |
|
|---|
| 67 | /** Size of the recieved data. */
|
|---|
| 68 | size_t request_size;
|
|---|
| 69 |
|
|---|
| 70 | /** Data buffer. */
|
|---|
| 71 | uint8_t *buffer;
|
|---|
| 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;
|
|---|
| 82 | };
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 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 |
|
|---|
| 93 | /** Polling fibril.
|
|---|
| 94 | *
|
|---|
| 95 | * @param arg Pointer to usb_device_polling_t.
|
|---|
| 96 | * @return Always EOK.
|
|---|
| 97 | */
|
|---|
| 98 | static int polling_fibril(void *arg)
|
|---|
| 99 | {
|
|---|
| 100 | assert(arg);
|
|---|
| 101 | usb_device_polling_t *data = arg;
|
|---|
| 102 | data->running = true;
|
|---|
| 103 |
|
|---|
| 104 | /* Helper to reduce typing. */
|
|---|
| 105 | const usb_device_polling_config_t *params = &data->config;
|
|---|
| 106 |
|
|---|
| 107 | usb_pipe_t *pipe = &data->ep_mapping->pipe;
|
|---|
| 108 |
|
|---|
| 109 | if (params->debug > 0) {
|
|---|
| 110 | const usb_endpoint_mapping_t *mapping =
|
|---|
| 111 | data->ep_mapping;
|
|---|
| 112 | usb_log_debug("Poll (%p): started polling of `%s' - " \
|
|---|
| 113 | "interface %d (%s,%d,%d), %zuB/%zu.\n",
|
|---|
| 114 | data, usb_device_get_name(data->dev),
|
|---|
| 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,
|
|---|
| 119 | data->request_size, pipe->desc.max_transfer_size);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | size_t failed_attempts = 0;
|
|---|
| 123 | while (failed_attempts <= params->max_failures) {
|
|---|
| 124 | size_t actual_size;
|
|---|
| 125 | const int rc = usb_pipe_read(pipe, data->buffer,
|
|---|
| 126 | data->request_size, &actual_size);
|
|---|
| 127 |
|
|---|
| 128 | if (rc == EOK) {
|
|---|
| 129 | if (params->debug > 1) {
|
|---|
| 130 | usb_log_debug(
|
|---|
| 131 | "Poll%p: received: '%s' (%zuB).\n",
|
|---|
| 132 | data,
|
|---|
| 133 | usb_debug_str_buffer(data->buffer,
|
|---|
| 134 | actual_size, 16),
|
|---|
| 135 | actual_size);
|
|---|
| 136 | }
|
|---|
| 137 | } else {
|
|---|
| 138 | usb_log_debug(
|
|---|
| 139 | "Poll%p: polling failed: %s.\n",
|
|---|
| 140 | data, str_error(rc));
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /* If the pipe stalled, we can try to reset the stall. */
|
|---|
| 144 | if ((rc == ESTALL) && (params->auto_clear_halt)) {
|
|---|
| 145 | /*
|
|---|
| 146 | * We ignore error here as this is usually a futile
|
|---|
| 147 | * attempt anyway.
|
|---|
| 148 | */
|
|---|
| 149 | usb_request_clear_endpoint_halt(
|
|---|
| 150 | usb_device_get_default_pipe(data->dev),
|
|---|
| 151 | pipe->desc.endpoint_no);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | if (rc != EOK) {
|
|---|
| 155 | ++failed_attempts;
|
|---|
| 156 | const bool cont = (params->on_error == NULL) ? true :
|
|---|
| 157 | params->on_error(data->dev, rc, params->arg);
|
|---|
| 158 | if (!cont || data->joining) {
|
|---|
| 159 | /* This is user requested abort, erases failures. */
|
|---|
| 160 | failed_attempts = 0;
|
|---|
| 161 | break;
|
|---|
| 162 | }
|
|---|
| 163 | continue;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /* We have the data, execute the callback now. */
|
|---|
| 167 | assert(params->on_data);
|
|---|
| 168 | const bool carry_on = params->on_data(
|
|---|
| 169 | data->dev, data->buffer, actual_size, params->arg);
|
|---|
| 170 |
|
|---|
| 171 | if (!carry_on) {
|
|---|
| 172 | /* This is user requested abort, erases failures. */
|
|---|
| 173 | failed_attempts = 0;
|
|---|
| 174 | break;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /* Reset as something might be only a temporary problem. */
|
|---|
| 178 | failed_attempts = 0;
|
|---|
| 179 |
|
|---|
| 180 | /* Take a rest before next request. */
|
|---|
| 181 |
|
|---|
| 182 | // FIXME TODO: This is broken, the time is in ms not us.
|
|---|
| 183 | // but first we need to fix drivers to actually stop using this,
|
|---|
| 184 | // since polling delay should be implemented in HC schedule
|
|---|
| 185 | async_usleep(params->delay);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | const bool failed = failed_attempts > 0;
|
|---|
| 189 |
|
|---|
| 190 | if (params->on_polling_end != NULL) {
|
|---|
| 191 | params->on_polling_end(data->dev, failed, params->arg);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | if (params->debug > 0) {
|
|---|
| 195 | if (failed) {
|
|---|
| 196 | usb_log_error("Polling of device `%s' terminated: "
|
|---|
| 197 | "recurring failures.\n",
|
|---|
| 198 | usb_device_get_name(data->dev));
|
|---|
| 199 | } else {
|
|---|
| 200 | usb_log_debug("Polling of device `%s' terminated: "
|
|---|
| 201 | "driver request.\n",
|
|---|
| 202 | usb_device_get_name(data->dev));
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 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 | }
|
|---|
| 216 |
|
|---|
| 217 | return EOK;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 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.
|
|---|
| 231 | * @param epm Endpoint mapping to use.
|
|---|
| 232 | * @param config Polling settings.
|
|---|
| 233 | * @param req_size How many bytes to ask for in each request.
|
|---|
| 234 | * @return Error code.
|
|---|
| 235 | * @retval EOK New fibril polling the device was already started.
|
|---|
| 236 | */
|
|---|
| 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)
|
|---|
| 240 | {
|
|---|
| 241 | int rc;
|
|---|
| 242 | if (!dev || !config || !config->on_data)
|
|---|
| 243 | return EBADMEM;
|
|---|
| 244 |
|
|---|
| 245 | if (!req_size)
|
|---|
| 246 | return EINVAL;
|
|---|
| 247 |
|
|---|
| 248 | if (!epm || (epm->pipe.desc.transfer_type != USB_TRANSFER_INTERRUPT) ||
|
|---|
| 249 | (epm->pipe.desc.direction != USB_DIRECTION_IN))
|
|---|
| 250 | return EINVAL;
|
|---|
| 251 |
|
|---|
| 252 | usb_device_polling_t *instance = malloc(sizeof(usb_device_polling_t));
|
|---|
| 253 | if (!instance)
|
|---|
| 254 | return ENOMEM;
|
|---|
| 255 |
|
|---|
| 256 | /* Fill-in the data. */
|
|---|
| 257 | instance->buffer = malloc(req_size);
|
|---|
| 258 | if (!instance->buffer) {
|
|---|
| 259 | rc = ENOMEM;
|
|---|
| 260 | goto err_instance;
|
|---|
| 261 | }
|
|---|
| 262 | instance->request_size = req_size;
|
|---|
| 263 | instance->dev = dev;
|
|---|
| 264 | instance->ep_mapping = epm;
|
|---|
| 265 | instance->joining = false;
|
|---|
| 266 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 267 | fibril_condvar_initialize(&instance->cv);
|
|---|
| 268 |
|
|---|
| 269 | /* Copy provided settings. */
|
|---|
| 270 | instance->config = *config;
|
|---|
| 271 |
|
|---|
| 272 | /* Negative value means use descriptor provided value. */
|
|---|
| 273 | if (config->delay < 0) {
|
|---|
| 274 | instance->config.delay = epm->descriptor->poll_interval;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | fid_t fibril = fibril_create(polling_fibril, instance);
|
|---|
| 278 | if (!fibril) {
|
|---|
| 279 | rc = ENOMEM;
|
|---|
| 280 | goto err_buffer;
|
|---|
| 281 | }
|
|---|
| 282 | fibril_add_ready(fibril);
|
|---|
| 283 |
|
|---|
| 284 | if (handle)
|
|---|
| 285 | *handle = instance;
|
|---|
| 286 |
|
|---|
| 287 | /* Fibril launched. That fibril will free the allocated data. */
|
|---|
| 288 | return EOK;
|
|---|
| 289 |
|
|---|
| 290 | err_buffer:
|
|---|
| 291 | free(instance->buffer);
|
|---|
| 292 | err_instance:
|
|---|
| 293 | free(instance);
|
|---|
| 294 | return rc;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 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 |
|
|---|
| 323 | /**
|
|---|
| 324 | * @}
|
|---|
| 325 | */
|
|---|