[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>
|
---|
[8d2dd7f2] | 52 | #include <stddef.h>
|
---|
| 53 | #include <stdint.h>
|
---|
[4bf94df] | 54 |
|
---|
| 55 | /** Maximum number of failed consecutive requests before announcing failure. */
|
---|
| 56 | #define MAX_FAILED_ATTEMPTS 3
|
---|
| 57 |
|
---|
| 58 | /** Data needed for polling. */
|
---|
| 59 | typedef struct {
|
---|
[6e3c005] | 60 | /** Parameters for automated polling. */
|
---|
[2703331b] | 61 | usb_device_auto_polling_t auto_polling;
|
---|
[8989f48f] | 62 |
|
---|
[6e3c005] | 63 | /** USB device to poll. */
|
---|
[4bf94df] | 64 | usb_device_t *dev;
|
---|
[bb70637] | 65 | /** Device enpoint mapping to use for polling. */
|
---|
| 66 | usb_endpoint_mapping_t *polling_mapping;
|
---|
[6e3c005] | 67 | /** Size of the recieved data. */
|
---|
[4bf94df] | 68 | size_t request_size;
|
---|
[6e3c005] | 69 | /** Data buffer. */
|
---|
[4bf94df] | 70 | uint8_t *buffer;
|
---|
| 71 | } polling_data_t;
|
---|
| 72 |
|
---|
[8989f48f] | 73 |
|
---|
[4bf94df] | 74 | /** Polling fibril.
|
---|
| 75 | *
|
---|
| 76 | * @param arg Pointer to polling_data_t.
|
---|
| 77 | * @return Always EOK.
|
---|
| 78 | */
|
---|
| 79 | static int polling_fibril(void *arg)
|
---|
| 80 | {
|
---|
[9dbfd288] | 81 | assert(arg);
|
---|
| 82 | const polling_data_t *data = arg;
|
---|
| 83 | /* Helper to reduce typing. */
|
---|
| 84 | const usb_device_auto_polling_t *params = &data->auto_polling;
|
---|
[4bf94df] | 85 |
|
---|
[bb70637] | 86 | usb_pipe_t *pipe = &data->polling_mapping->pipe;
|
---|
[9dbfd288] | 87 |
|
---|
| 88 | if (params->debug > 0) {
|
---|
[58563585] | 89 | const usb_endpoint_mapping_t *mapping =
|
---|
| 90 | data->polling_mapping;
|
---|
[b303275] | 91 | usb_log_debug("Poll (%p): started polling of `%s' - " \
|
---|
[5e168be1] | 92 | "interface %d (%s,%d,%d), %zuB/%zu.\n",
|
---|
[f6b2a76b] | 93 | data, usb_device_get_name(data->dev),
|
---|
[5e168be1] | 94 | (int) mapping->interface->interface_number,
|
---|
| 95 | usb_str_class(mapping->interface->interface_class),
|
---|
| 96 | (int) mapping->interface->interface_subclass,
|
---|
| 97 | (int) mapping->interface->interface_protocol,
|
---|
[9efad54] | 98 | data->request_size, pipe->desc.max_transfer_size);
|
---|
[5e168be1] | 99 | }
|
---|
[4bf94df] | 100 |
|
---|
| 101 | size_t failed_attempts = 0;
|
---|
[9dbfd288] | 102 | while (failed_attempts <= params->max_failures) {
|
---|
[4bf94df] | 103 | size_t actual_size;
|
---|
[9dbfd288] | 104 | const int rc = usb_pipe_read(pipe, data->buffer,
|
---|
| 105 | data->request_size, &actual_size);
|
---|
[4bf94df] | 106 |
|
---|
[51cc6cef] | 107 | if (rc == EOK) {
|
---|
| 108 | if (params->debug > 1) {
|
---|
[5e168be1] | 109 | usb_log_debug(
|
---|
[4125b7d] | 110 | "Poll%p: received: '%s' (%zuB).\n",
|
---|
[9dbfd288] | 111 | data,
|
---|
| 112 | usb_debug_str_buffer(data->buffer,
|
---|
[5e168be1] | 113 | actual_size, 16),
|
---|
| 114 | actual_size);
|
---|
[51cc6cef] | 115 | }
|
---|
| 116 | } else {
|
---|
[5e168be1] | 117 | usb_log_debug(
|
---|
[4125b7d] | 118 | "Poll%p: polling failed: %s.\n",
|
---|
[9dbfd288] | 119 | data, str_error(rc));
|
---|
[5e168be1] | 120 | }
|
---|
| 121 |
|
---|
[8989f48f] | 122 | /* If the pipe stalled, we can try to reset the stall. */
|
---|
[9dbfd288] | 123 | if ((rc == ESTALL) && (params->auto_clear_halt)) {
|
---|
[8989f48f] | 124 | /*
|
---|
| 125 | * We ignore error here as this is usually a futile
|
---|
| 126 | * attempt anyway.
|
---|
| 127 | */
|
---|
| 128 | usb_request_clear_endpoint_halt(
|
---|
[bb70637] | 129 | usb_device_get_default_pipe(data->dev),
|
---|
[816f5f4] | 130 | pipe->desc.endpoint_no);
|
---|
[8989f48f] | 131 | }
|
---|
[4bf94df] | 132 |
|
---|
| 133 | if (rc != EOK) {
|
---|
[9dbfd288] | 134 | ++failed_attempts;
|
---|
| 135 | const bool cont = (params->on_error == NULL) ? true :
|
---|
[a0487a2] | 136 | params->on_error(data->dev, rc, params->arg);
|
---|
[9dbfd288] | 137 | if (!cont) {
|
---|
[a4eb520f] | 138 | /* This is user requested abort, erases failures. */
|
---|
| 139 | failed_attempts = 0;
|
---|
| 140 | break;
|
---|
[8989f48f] | 141 | }
|
---|
[4bf94df] | 142 | continue;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /* We have the data, execute the callback now. */
|
---|
[9dbfd288] | 146 | assert(params->on_data);
|
---|
| 147 | const bool carry_on = params->on_data(
|
---|
[a0487a2] | 148 | data->dev, data->buffer, actual_size, params->arg);
|
---|
[4bf94df] | 149 |
|
---|
| 150 | if (!carry_on) {
|
---|
[9dbfd288] | 151 | /* This is user requested abort, erases failures. */
|
---|
[4bf94df] | 152 | failed_attempts = 0;
|
---|
| 153 | break;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | /* Reset as something might be only a temporary problem. */
|
---|
| 157 | failed_attempts = 0;
|
---|
[8989f48f] | 158 |
|
---|
| 159 | /* Take a rest before next request. */
|
---|
[816f5f4] | 160 |
|
---|
[58563585] | 161 | // FIXME TODO: This is broken, the time is in ms not us.
|
---|
[ff0258f] | 162 | // but first we need to fix drivers to actually stop using this,
|
---|
[58563585] | 163 | // since polling delay should be implemented in HC schedule
|
---|
[9dbfd288] | 164 | async_usleep(params->delay);
|
---|
[4bf94df] | 165 | }
|
---|
| 166 |
|
---|
[9dbfd288] | 167 | const bool failed = failed_attempts > 0;
|
---|
| 168 |
|
---|
| 169 | if (params->on_polling_end != NULL) {
|
---|
[a0487a2] | 170 | params->on_polling_end(data->dev, failed, params->arg);
|
---|
[4bf94df] | 171 | }
|
---|
| 172 |
|
---|
[9dbfd288] | 173 | if (params->debug > 0) {
|
---|
| 174 | if (failed) {
|
---|
| 175 | usb_log_error("Polling of device `%s' terminated: "
|
---|
[f6b2a76b] | 176 | "recurring failures.\n",
|
---|
| 177 | usb_device_get_name(data->dev));
|
---|
[5e168be1] | 178 | } else {
|
---|
[9dbfd288] | 179 | usb_log_debug("Polling of device `%s' terminated: "
|
---|
[f6b2a76b] | 180 | "driver request.\n",
|
---|
| 181 | usb_device_get_name(data->dev));
|
---|
[5e168be1] | 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[4bf94df] | 185 | /* Free the allocated memory. */
|
---|
[9dbfd288] | 186 | free(data->buffer);
|
---|
| 187 | free(data);
|
---|
[4bf94df] | 188 |
|
---|
| 189 | return EOK;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[8989f48f] | 192 |
|
---|
| 193 | /** Start automatic device polling over interrupt in pipe.
|
---|
| 194 | *
|
---|
| 195 | * The polling settings is copied thus it is okay to destroy the structure
|
---|
| 196 | * after this function returns.
|
---|
| 197 | *
|
---|
| 198 | * @warning There is no guarantee when the request to the device
|
---|
| 199 | * will be sent for the first time (it is possible that this
|
---|
| 200 | * first request would be executed prior to return from this function).
|
---|
| 201 | *
|
---|
| 202 | * @param dev Device to be periodically polled.
|
---|
[bb70637] | 203 | * @param epm Endpoint mapping to use.
|
---|
[8989f48f] | 204 | * @param polling Polling settings.
|
---|
[7dddd7b] | 205 | * @param req_size How many bytes to ask for in each request.
|
---|
[8989f48f] | 206 | * @return Error code.
|
---|
| 207 | * @retval EOK New fibril polling the device was already started.
|
---|
| 208 | */
|
---|
[7dddd7b] | 209 | int usb_device_auto_polling(usb_device_t *dev, usb_endpoint_mapping_t *epm,
|
---|
| 210 | const usb_device_auto_polling_t *polling, size_t req_size)
|
---|
[8989f48f] | 211 | {
|
---|
[7dddd7b] | 212 | int rc;
|
---|
| 213 | if (!dev || !polling || !polling->on_data)
|
---|
[8989f48f] | 214 | return EBADMEM;
|
---|
[b81410f] | 215 |
|
---|
[7dddd7b] | 216 | if (!req_size)
|
---|
[8989f48f] | 217 | return EINVAL;
|
---|
[816f5f4] | 218 |
|
---|
| 219 | if (!epm || (epm->pipe.desc.transfer_type != USB_TRANSFER_INTERRUPT) ||
|
---|
| 220 | (epm->pipe.desc.direction != USB_DIRECTION_IN))
|
---|
[8989f48f] | 221 | return EINVAL;
|
---|
[816f5f4] | 222 |
|
---|
[4bf94df] | 223 | polling_data_t *polling_data = malloc(sizeof(polling_data_t));
|
---|
[7dddd7b] | 224 | if (!polling_data)
|
---|
[4bf94df] | 225 | return ENOMEM;
|
---|
| 226 |
|
---|
[8989f48f] | 227 | /* Fill-in the data. */
|
---|
[7dddd7b] | 228 | polling_data->buffer = malloc(req_size);
|
---|
[4bf94df] | 229 | if (polling_data->buffer == NULL) {
|
---|
[7dddd7b] | 230 | rc = ENOMEM;
|
---|
| 231 | goto err_polling_data;
|
---|
[4bf94df] | 232 | }
|
---|
[7dddd7b] | 233 | polling_data->request_size = req_size;
|
---|
[4bf94df] | 234 | polling_data->dev = dev;
|
---|
[bb70637] | 235 | polling_data->polling_mapping = epm;
|
---|
[4bf94df] | 236 |
|
---|
[2703331b] | 237 | /* Copy provided settings. */
|
---|
| 238 | polling_data->auto_polling = *polling;
|
---|
[8989f48f] | 239 |
|
---|
[2703331b] | 240 | /* Negative value means use descriptor provided value. */
|
---|
| 241 | if (polling->delay < 0) {
|
---|
| 242 | polling_data->auto_polling.delay =
|
---|
[bb70637] | 243 | epm->descriptor->poll_interval;
|
---|
[2703331b] | 244 | }
|
---|
[8989f48f] | 245 |
|
---|
[4bf94df] | 246 | fid_t fibril = fibril_create(polling_fibril, polling_data);
|
---|
[7dddd7b] | 247 | if (!fibril) {
|
---|
| 248 | rc = ENOMEM;
|
---|
| 249 | goto err_buffer;
|
---|
[4bf94df] | 250 | }
|
---|
| 251 | fibril_add_ready(fibril);
|
---|
| 252 |
|
---|
[8989f48f] | 253 | /* Fibril launched. That fibril will free the allocated data. */
|
---|
[4bf94df] | 254 | return EOK;
|
---|
[bb70637] | 255 |
|
---|
[7dddd7b] | 256 | err_buffer:
|
---|
| 257 | free(polling_data->buffer);
|
---|
| 258 | err_polling_data:
|
---|
| 259 | free(polling_data);
|
---|
| 260 | return rc;
|
---|
[bb70637] | 261 | }
|
---|
[4bf94df] | 262 |
|
---|
| 263 | /**
|
---|
| 264 | * @}
|
---|
| 265 | */
|
---|