[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>
|
---|
| 49 | #include <stdbool.h>
|
---|
| 50 | #include <stdlib.h>
|
---|
[4bf94df] | 51 | #include <str_error.h>
|
---|
[c01987c] | 52 | #include <sys/types.h>
|
---|
[4bf94df] | 53 |
|
---|
| 54 | /** Maximum number of failed consecutive requests before announcing failure. */
|
---|
| 55 | #define MAX_FAILED_ATTEMPTS 3
|
---|
| 56 |
|
---|
| 57 | /** Data needed for polling. */
|
---|
| 58 | typedef struct {
|
---|
[6e3c005] | 59 | /** Parameters for automated polling. */
|
---|
[2703331b] | 60 | usb_device_auto_polling_t auto_polling;
|
---|
[8989f48f] | 61 |
|
---|
[6e3c005] | 62 | /** USB device to poll. */
|
---|
[4bf94df] | 63 | usb_device_t *dev;
|
---|
[bb70637] | 64 | /** Device enpoint mapping to use for polling. */
|
---|
| 65 | usb_endpoint_mapping_t *polling_mapping;
|
---|
[6e3c005] | 66 | /** Size of the recieved data. */
|
---|
[4bf94df] | 67 | size_t request_size;
|
---|
[6e3c005] | 68 | /** Data buffer. */
|
---|
[4bf94df] | 69 | uint8_t *buffer;
|
---|
| 70 | } polling_data_t;
|
---|
| 71 |
|
---|
[8989f48f] | 72 |
|
---|
[4bf94df] | 73 | /** Polling fibril.
|
---|
| 74 | *
|
---|
| 75 | * @param arg Pointer to polling_data_t.
|
---|
| 76 | * @return Always EOK.
|
---|
| 77 | */
|
---|
| 78 | static int polling_fibril(void *arg)
|
---|
| 79 | {
|
---|
[9dbfd288] | 80 | assert(arg);
|
---|
| 81 | const polling_data_t *data = arg;
|
---|
| 82 | /* Helper to reduce typing. */
|
---|
| 83 | const usb_device_auto_polling_t *params = &data->auto_polling;
|
---|
[4bf94df] | 84 |
|
---|
[bb70637] | 85 | usb_pipe_t *pipe = &data->polling_mapping->pipe;
|
---|
[9dbfd288] | 86 |
|
---|
| 87 | if (params->debug > 0) {
|
---|
[58563585] | 88 | const usb_endpoint_mapping_t *mapping =
|
---|
| 89 | data->polling_mapping;
|
---|
[b303275] | 90 | usb_log_debug("Poll (%p): started polling of `%s' - " \
|
---|
[5e168be1] | 91 | "interface %d (%s,%d,%d), %zuB/%zu.\n",
|
---|
[f6b2a76b] | 92 | data, usb_device_get_name(data->dev),
|
---|
[5e168be1] | 93 | (int) mapping->interface->interface_number,
|
---|
| 94 | usb_str_class(mapping->interface->interface_class),
|
---|
| 95 | (int) mapping->interface->interface_subclass,
|
---|
| 96 | (int) mapping->interface->interface_protocol,
|
---|
[9dbfd288] | 97 | data->request_size, pipe->max_packet_size);
|
---|
[5e168be1] | 98 | }
|
---|
[4bf94df] | 99 |
|
---|
| 100 | size_t failed_attempts = 0;
|
---|
[9dbfd288] | 101 | while (failed_attempts <= params->max_failures) {
|
---|
[4bf94df] | 102 | size_t actual_size;
|
---|
[9dbfd288] | 103 | const int rc = usb_pipe_read(pipe, data->buffer,
|
---|
| 104 | data->request_size, &actual_size);
|
---|
[4bf94df] | 105 |
|
---|
[51cc6cef] | 106 | if (rc == EOK) {
|
---|
| 107 | if (params->debug > 1) {
|
---|
[5e168be1] | 108 | usb_log_debug(
|
---|
[4125b7d] | 109 | "Poll%p: received: '%s' (%zuB).\n",
|
---|
[9dbfd288] | 110 | data,
|
---|
| 111 | usb_debug_str_buffer(data->buffer,
|
---|
[5e168be1] | 112 | actual_size, 16),
|
---|
| 113 | actual_size);
|
---|
[51cc6cef] | 114 | }
|
---|
| 115 | } else {
|
---|
[5e168be1] | 116 | usb_log_debug(
|
---|
[4125b7d] | 117 | "Poll%p: polling failed: %s.\n",
|
---|
[9dbfd288] | 118 | data, str_error(rc));
|
---|
[5e168be1] | 119 | }
|
---|
| 120 |
|
---|
[8989f48f] | 121 | /* If the pipe stalled, we can try to reset the stall. */
|
---|
[9dbfd288] | 122 | if ((rc == ESTALL) && (params->auto_clear_halt)) {
|
---|
[8989f48f] | 123 | /*
|
---|
| 124 | * We ignore error here as this is usually a futile
|
---|
| 125 | * attempt anyway.
|
---|
| 126 | */
|
---|
| 127 | usb_request_clear_endpoint_halt(
|
---|
[bb70637] | 128 | usb_device_get_default_pipe(data->dev),
|
---|
| 129 | pipe->endpoint_no);
|
---|
[8989f48f] | 130 | }
|
---|
[4bf94df] | 131 |
|
---|
| 132 | if (rc != EOK) {
|
---|
[9dbfd288] | 133 | ++failed_attempts;
|
---|
| 134 | const bool cont = (params->on_error == NULL) ? true :
|
---|
[a0487a2] | 135 | params->on_error(data->dev, rc, params->arg);
|
---|
[9dbfd288] | 136 | if (!cont) {
|
---|
| 137 | failed_attempts = params->max_failures;
|
---|
[8989f48f] | 138 | }
|
---|
[4bf94df] | 139 | continue;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /* We have the data, execute the callback now. */
|
---|
[9dbfd288] | 143 | assert(params->on_data);
|
---|
| 144 | const bool carry_on = params->on_data(
|
---|
[a0487a2] | 145 | data->dev, data->buffer, actual_size, params->arg);
|
---|
[4bf94df] | 146 |
|
---|
| 147 | if (!carry_on) {
|
---|
[9dbfd288] | 148 | /* This is user requested abort, erases failures. */
|
---|
[4bf94df] | 149 | failed_attempts = 0;
|
---|
| 150 | break;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /* Reset as something might be only a temporary problem. */
|
---|
| 154 | failed_attempts = 0;
|
---|
[8989f48f] | 155 |
|
---|
| 156 | /* Take a rest before next request. */
|
---|
[58563585] | 157 |
|
---|
| 158 | // FIXME TODO: This is broken, the time is in ms not us.
|
---|
[ff0258f] | 159 | // but first we need to fix drivers to actually stop using this,
|
---|
[58563585] | 160 | // since polling delay should be implemented in HC schedule
|
---|
[9dbfd288] | 161 | async_usleep(params->delay);
|
---|
[4bf94df] | 162 | }
|
---|
| 163 |
|
---|
[9dbfd288] | 164 | const bool failed = failed_attempts > 0;
|
---|
| 165 |
|
---|
| 166 | if (params->on_polling_end != NULL) {
|
---|
[a0487a2] | 167 | params->on_polling_end(data->dev, failed, params->arg);
|
---|
[4bf94df] | 168 | }
|
---|
| 169 |
|
---|
[9dbfd288] | 170 | if (params->debug > 0) {
|
---|
| 171 | if (failed) {
|
---|
| 172 | usb_log_error("Polling of device `%s' terminated: "
|
---|
[f6b2a76b] | 173 | "recurring failures.\n",
|
---|
| 174 | usb_device_get_name(data->dev));
|
---|
[5e168be1] | 175 | } else {
|
---|
[9dbfd288] | 176 | usb_log_debug("Polling of device `%s' terminated: "
|
---|
[f6b2a76b] | 177 | "driver request.\n",
|
---|
| 178 | usb_device_get_name(data->dev));
|
---|
[5e168be1] | 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[4bf94df] | 182 | /* Free the allocated memory. */
|
---|
[9dbfd288] | 183 | free(data->buffer);
|
---|
| 184 | free(data);
|
---|
[4bf94df] | 185 |
|
---|
| 186 | return EOK;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[8989f48f] | 189 |
|
---|
| 190 | /** Start automatic device polling over interrupt in pipe.
|
---|
| 191 | *
|
---|
| 192 | * The polling settings is copied thus it is okay to destroy the structure
|
---|
| 193 | * after this function returns.
|
---|
| 194 | *
|
---|
| 195 | * @warning There is no guarantee when the request to the device
|
---|
| 196 | * will be sent for the first time (it is possible that this
|
---|
| 197 | * first request would be executed prior to return from this function).
|
---|
| 198 | *
|
---|
| 199 | * @param dev Device to be periodically polled.
|
---|
[bb70637] | 200 | * @param epm Endpoint mapping to use.
|
---|
[8989f48f] | 201 | * @param polling Polling settings.
|
---|
| 202 | * @param request_size How many bytes to ask for in each request.
|
---|
| 203 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
| 204 | * @return Error code.
|
---|
| 205 | * @retval EOK New fibril polling the device was already started.
|
---|
| 206 | */
|
---|
[bb70637] | 207 | static int usb_device_auto_polling_internal(usb_device_t *dev,
|
---|
| 208 | usb_endpoint_mapping_t *epm, const usb_device_auto_polling_t *polling,
|
---|
[a0487a2] | 209 | size_t request_size)
|
---|
[8989f48f] | 210 | {
|
---|
[b81410f] | 211 | if ((dev == NULL) || (polling == NULL) || (polling->on_data == NULL)) {
|
---|
[8989f48f] | 212 | return EBADMEM;
|
---|
| 213 | }
|
---|
[b81410f] | 214 |
|
---|
[bb70637] | 215 | if (request_size == 0)
|
---|
[8989f48f] | 216 | return EINVAL;
|
---|
[58563585] | 217 |
|
---|
[bb70637] | 218 | if (!epm || (epm->pipe.transfer_type != USB_TRANSFER_INTERRUPT) ||
|
---|
| 219 | (epm->pipe.direction != USB_DIRECTION_IN))
|
---|
[8989f48f] | 220 | return EINVAL;
|
---|
[58563585] | 221 |
|
---|
[8989f48f] | 222 |
|
---|
[4bf94df] | 223 | polling_data_t *polling_data = malloc(sizeof(polling_data_t));
|
---|
| 224 | if (polling_data == NULL) {
|
---|
| 225 | return ENOMEM;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[8989f48f] | 228 | /* Fill-in the data. */
|
---|
| 229 | polling_data->buffer = malloc(sizeof(request_size));
|
---|
[4bf94df] | 230 | if (polling_data->buffer == NULL) {
|
---|
| 231 | free(polling_data);
|
---|
| 232 | return ENOMEM;
|
---|
| 233 | }
|
---|
[8989f48f] | 234 | polling_data->request_size = request_size;
|
---|
[4bf94df] | 235 | polling_data->dev = dev;
|
---|
[bb70637] | 236 | polling_data->polling_mapping = epm;
|
---|
[4bf94df] | 237 |
|
---|
[2703331b] | 238 | /* Copy provided settings. */
|
---|
| 239 | polling_data->auto_polling = *polling;
|
---|
[8989f48f] | 240 |
|
---|
[2703331b] | 241 | /* Negative value means use descriptor provided value. */
|
---|
| 242 | if (polling->delay < 0) {
|
---|
| 243 | polling_data->auto_polling.delay =
|
---|
[bb70637] | 244 | epm->descriptor->poll_interval;
|
---|
[2703331b] | 245 | }
|
---|
[8989f48f] | 246 |
|
---|
[4bf94df] | 247 | fid_t fibril = fibril_create(polling_fibril, polling_data);
|
---|
| 248 | if (fibril == 0) {
|
---|
| 249 | free(polling_data->buffer);
|
---|
| 250 | free(polling_data);
|
---|
| 251 | return ENOMEM;
|
---|
| 252 | }
|
---|
| 253 | fibril_add_ready(fibril);
|
---|
| 254 |
|
---|
[8989f48f] | 255 | /* Fibril launched. That fibril will free the allocated data. */
|
---|
[4bf94df] | 256 |
|
---|
| 257 | return EOK;
|
---|
| 258 | }
|
---|
[bb70637] | 259 | /** Start automatic device polling over interrupt in pipe.
|
---|
| 260 | *
|
---|
| 261 | * The polling settings is copied thus it is okay to destroy the structure
|
---|
| 262 | * after this function returns.
|
---|
| 263 | *
|
---|
| 264 | * @warning There is no guarantee when the request to the device
|
---|
| 265 | * will be sent for the first time (it is possible that this
|
---|
| 266 | * first request would be executed prior to return from this function).
|
---|
| 267 | *
|
---|
| 268 | * @param dev Device to be periodically polled.
|
---|
| 269 | * @param pipe_index Index of the endpoint pipe used for polling.
|
---|
| 270 | * @param polling Polling settings.
|
---|
| 271 | * @param req_size How many bytes to ask for in each request.
|
---|
| 272 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
| 273 | * @return Error code.
|
---|
| 274 | * @retval EOK New fibril polling the device was already started.
|
---|
| 275 | */
|
---|
| 276 | int usb_device_auto_polling(usb_device_t *usb_dev, usb_endpoint_t ep,
|
---|
| 277 | const usb_device_auto_polling_t *polling, size_t req_size)
|
---|
| 278 | {
|
---|
| 279 | usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep(usb_dev, ep);
|
---|
| 280 | return usb_device_auto_polling_internal(usb_dev, epm, polling, req_size);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | /** Start automatic device polling over interrupt in pipe.
|
---|
| 284 | *
|
---|
| 285 | * @warning It is up to the callback to produce delays between individual
|
---|
| 286 | * requests.
|
---|
| 287 | *
|
---|
| 288 | * @warning There is no guarantee when the request to the device
|
---|
| 289 | * will be sent for the first time (it is possible that this
|
---|
| 290 | * first request would be executed prior to return from this function).
|
---|
| 291 | *
|
---|
| 292 | * @param dev Device to be periodically polled.
|
---|
| 293 | * @param ep Endpoint used for polling.
|
---|
| 294 | * @param callback Callback when data are available.
|
---|
| 295 | * @param request_size How many bytes to ask for in each request.
|
---|
| 296 | * @param delay NUmber of ms to wait between queries, -1 to use descriptor val.
|
---|
| 297 | * @param terminated_callback Callback when polling is terminated.
|
---|
| 298 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
| 299 | * @return Error code.
|
---|
| 300 | * @retval EOK New fibril polling the device was already started.
|
---|
| 301 | */
|
---|
| 302 | int usb_device_auto_poll(usb_device_t *dev, usb_endpoint_t ep,
|
---|
| 303 | usb_polling_callback_t callback, size_t request_size, int delay,
|
---|
| 304 | usb_polling_terminted_callback_t terminated_callback, void *arg)
|
---|
| 305 | {
|
---|
| 306 | const usb_device_auto_polling_t auto_polling = {
|
---|
| 307 | .debug = 1,
|
---|
| 308 | .auto_clear_halt = true,
|
---|
| 309 | .delay = delay,
|
---|
| 310 | .max_failures = MAX_FAILED_ATTEMPTS,
|
---|
| 311 | .on_data = callback,
|
---|
| 312 | .on_polling_end = terminated_callback,
|
---|
| 313 | .on_error = NULL,
|
---|
| 314 | .arg = arg,
|
---|
| 315 | };
|
---|
| 316 |
|
---|
| 317 | usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep(dev, ep);
|
---|
| 318 | return usb_device_auto_polling_internal(
|
---|
| 319 | dev, epm, &auto_polling, request_size);
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | int usb_device_auto_polling_desc(usb_device_t *usb_dev,
|
---|
| 323 | const usb_endpoint_description_t *desc,
|
---|
| 324 | const usb_device_auto_polling_t *polling, size_t req_size)
|
---|
| 325 | {
|
---|
| 326 | usb_endpoint_mapping_t *epm =
|
---|
| 327 | usb_device_get_mapped_ep_desc(usb_dev, desc);
|
---|
| 328 | return usb_device_auto_polling_internal(usb_dev, epm, polling, req_size);
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | int usb_device_auto_poll_desc(usb_device_t * usb_dev,
|
---|
| 332 | const usb_endpoint_description_t *desc, usb_polling_callback_t callback,
|
---|
| 333 | size_t req_size, int delay,
|
---|
| 334 | usb_polling_terminted_callback_t terminated_callback, void *arg)
|
---|
| 335 | {
|
---|
| 336 | const usb_device_auto_polling_t auto_polling = {
|
---|
| 337 | .debug = 1,
|
---|
| 338 | .auto_clear_halt = true,
|
---|
| 339 | .delay = delay,
|
---|
| 340 | .max_failures = MAX_FAILED_ATTEMPTS,
|
---|
| 341 | .on_data = callback,
|
---|
| 342 | .on_polling_end = terminated_callback,
|
---|
| 343 | .on_error = NULL,
|
---|
| 344 | .arg = arg,
|
---|
| 345 | };
|
---|
| 346 |
|
---|
| 347 | usb_endpoint_mapping_t *epm =
|
---|
| 348 | usb_device_get_mapped_ep_desc(usb_dev, desc);
|
---|
| 349 | return usb_device_auto_polling_internal(
|
---|
| 350 | usb_dev, epm, &auto_polling, req_size);
|
---|
| 351 | }
|
---|
[4bf94df] | 352 |
|
---|
| 353 | /**
|
---|
| 354 | * @}
|
---|
| 355 | */
|
---|