[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 |
|
---|
| 29 | /** @addtogroup libusb
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * USB device driver framework - automatic interrupt polling.
|
---|
| 34 | */
|
---|
[5e07e2b5] | 35 | #include <usb/devpoll.h>
|
---|
[4bf94df] | 36 | #include <usb/request.h>
|
---|
| 37 | #include <usb/debug.h>
|
---|
[5e168be1] | 38 | #include <usb/classes/classes.h>
|
---|
[4bf94df] | 39 | #include <errno.h>
|
---|
| 40 | #include <str_error.h>
|
---|
| 41 | #include <assert.h>
|
---|
| 42 |
|
---|
| 43 | /** Maximum number of failed consecutive requests before announcing failure. */
|
---|
| 44 | #define MAX_FAILED_ATTEMPTS 3
|
---|
| 45 |
|
---|
| 46 | /** Data needed for polling. */
|
---|
| 47 | typedef struct {
|
---|
[5e168be1] | 48 | int debug;
|
---|
[8989f48f] | 49 | size_t max_failures;
|
---|
| 50 | useconds_t delay;
|
---|
| 51 | bool auto_clear_halt;
|
---|
| 52 | bool (*on_data)(usb_device_t *, uint8_t *, size_t, void *);
|
---|
| 53 | void (*on_polling_end)(usb_device_t *, bool, void *);
|
---|
| 54 | bool (*on_error)(usb_device_t *, int, void *);
|
---|
| 55 |
|
---|
[4bf94df] | 56 | usb_device_t *dev;
|
---|
| 57 | size_t pipe_index;
|
---|
| 58 | size_t request_size;
|
---|
| 59 | uint8_t *buffer;
|
---|
| 60 | void *custom_arg;
|
---|
| 61 | } polling_data_t;
|
---|
| 62 |
|
---|
[8989f48f] | 63 |
|
---|
[4bf94df] | 64 | /** Polling fibril.
|
---|
| 65 | *
|
---|
| 66 | * @param arg Pointer to polling_data_t.
|
---|
| 67 | * @return Always EOK.
|
---|
| 68 | */
|
---|
| 69 | static int polling_fibril(void *arg)
|
---|
| 70 | {
|
---|
| 71 | polling_data_t *polling_data = (polling_data_t *) arg;
|
---|
| 72 | assert(polling_data);
|
---|
| 73 |
|
---|
[a372663] | 74 | usb_pipe_t *pipe
|
---|
[4bf94df] | 75 | = polling_data->dev->pipes[polling_data->pipe_index].pipe;
|
---|
[7f2e33a] | 76 |
|
---|
[5e168be1] | 77 | if (polling_data->debug > 0) {
|
---|
| 78 | usb_endpoint_mapping_t *mapping
|
---|
| 79 | = &polling_data->dev->pipes[polling_data->pipe_index];
|
---|
| 80 | usb_log_debug("Poll0x%x: started polling of `%s' - " \
|
---|
| 81 | "interface %d (%s,%d,%d), %zuB/%zu.\n",
|
---|
| 82 | polling_data,
|
---|
| 83 | polling_data->dev->ddf_dev->name,
|
---|
| 84 | (int) mapping->interface->interface_number,
|
---|
| 85 | usb_str_class(mapping->interface->interface_class),
|
---|
| 86 | (int) mapping->interface->interface_subclass,
|
---|
| 87 | (int) mapping->interface->interface_protocol,
|
---|
| 88 | polling_data->request_size, pipe->max_packet_size);
|
---|
| 89 | }
|
---|
[4bf94df] | 90 |
|
---|
| 91 | size_t failed_attempts = 0;
|
---|
[8989f48f] | 92 | while (failed_attempts <= polling_data->max_failures) {
|
---|
[4bf94df] | 93 | int rc;
|
---|
| 94 |
|
---|
| 95 | size_t actual_size;
|
---|
[3954a63b] | 96 | rc = usb_pipe_read(pipe, polling_data->buffer,
|
---|
[4bf94df] | 97 | polling_data->request_size, &actual_size);
|
---|
| 98 |
|
---|
[5e168be1] | 99 | if (polling_data->debug > 1) {
|
---|
| 100 | if (rc == EOK) {
|
---|
| 101 | usb_log_debug(
|
---|
| 102 | "Poll0x%x: received: '%s' (%zuB).\n",
|
---|
| 103 | polling_data,
|
---|
| 104 | usb_debug_str_buffer(polling_data->buffer,
|
---|
| 105 | actual_size, 16),
|
---|
| 106 | actual_size);
|
---|
| 107 | } else {
|
---|
| 108 | usb_log_debug(
|
---|
| 109 | "Poll0x%x: polling failed: %s.\n",
|
---|
| 110 | polling_data, str_error(rc));
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[8989f48f] | 114 | /* If the pipe stalled, we can try to reset the stall. */
|
---|
| 115 | if ((rc == ESTALL) && (polling_data->auto_clear_halt)) {
|
---|
| 116 | /*
|
---|
| 117 | * We ignore error here as this is usually a futile
|
---|
| 118 | * attempt anyway.
|
---|
| 119 | */
|
---|
| 120 | usb_request_clear_endpoint_halt(
|
---|
| 121 | &polling_data->dev->ctrl_pipe,
|
---|
| 122 | pipe->endpoint_no);
|
---|
| 123 | }
|
---|
[4bf94df] | 124 |
|
---|
| 125 | if (rc != EOK) {
|
---|
[8989f48f] | 126 | if (polling_data->on_error != NULL) {
|
---|
| 127 | bool cont = polling_data->on_error(
|
---|
| 128 | polling_data->dev, rc,
|
---|
| 129 | polling_data->custom_arg);
|
---|
| 130 | if (!cont) {
|
---|
| 131 | failed_attempts
|
---|
| 132 | = polling_data->max_failures;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
[4bf94df] | 135 | failed_attempts++;
|
---|
| 136 | continue;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /* We have the data, execute the callback now. */
|
---|
[8989f48f] | 140 | bool carry_on = polling_data->on_data(polling_data->dev,
|
---|
[4bf94df] | 141 | polling_data->buffer, actual_size,
|
---|
| 142 | polling_data->custom_arg);
|
---|
| 143 |
|
---|
| 144 | if (!carry_on) {
|
---|
| 145 | failed_attempts = 0;
|
---|
| 146 | break;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /* Reset as something might be only a temporary problem. */
|
---|
| 150 | failed_attempts = 0;
|
---|
[8989f48f] | 151 |
|
---|
| 152 | /* Take a rest before next request. */
|
---|
| 153 | async_usleep(polling_data->delay);
|
---|
[4bf94df] | 154 | }
|
---|
| 155 |
|
---|
[8989f48f] | 156 | if (polling_data->on_polling_end != NULL) {
|
---|
| 157 | polling_data->on_polling_end(polling_data->dev,
|
---|
[4bf94df] | 158 | failed_attempts > 0, polling_data->custom_arg);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[5e168be1] | 161 | if (polling_data->debug > 0) {
|
---|
| 162 | if (failed_attempts > 0) {
|
---|
| 163 | usb_log_error(
|
---|
| 164 | "Polling of device `%s' terminated: %s.\n",
|
---|
| 165 | polling_data->dev->ddf_dev->name,
|
---|
| 166 | "recurring failures");
|
---|
| 167 | } else {
|
---|
| 168 | usb_log_debug(
|
---|
| 169 | "Polling of device `%s' terminated by user.\n",
|
---|
| 170 | polling_data->dev->ddf_dev->name
|
---|
| 171 | );
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[4bf94df] | 175 | /* Free the allocated memory. */
|
---|
| 176 | free(polling_data->buffer);
|
---|
| 177 | free(polling_data);
|
---|
| 178 |
|
---|
| 179 | return EOK;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /** Start automatic device polling over interrupt in pipe.
|
---|
| 183 | *
|
---|
| 184 | * @warning It is up to the callback to produce delays between individual
|
---|
| 185 | * requests.
|
---|
| 186 | *
|
---|
| 187 | * @warning There is no guarantee when the request to the device
|
---|
| 188 | * will be sent for the first time (it is possible that this
|
---|
| 189 | * first request would be executed prior to return from this function).
|
---|
| 190 | *
|
---|
| 191 | * @param dev Device to be periodically polled.
|
---|
| 192 | * @param pipe_index Index of the endpoint pipe used for polling.
|
---|
| 193 | * @param callback Callback when data are available.
|
---|
| 194 | * @param request_size How many bytes to ask for in each request.
|
---|
| 195 | * @param terminated_callback Callback when polling is terminated.
|
---|
| 196 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
| 197 | * @return Error code.
|
---|
| 198 | * @retval EOK New fibril polling the device was already started.
|
---|
| 199 | */
|
---|
| 200 | int usb_device_auto_poll(usb_device_t *dev, size_t pipe_index,
|
---|
| 201 | usb_polling_callback_t callback, size_t request_size,
|
---|
| 202 | usb_polling_terminted_callback_t terminated_callback, void *arg)
|
---|
| 203 | {
|
---|
| 204 | if ((dev == NULL) || (callback == NULL)) {
|
---|
| 205 | return EBADMEM;
|
---|
| 206 | }
|
---|
| 207 | if (request_size == 0) {
|
---|
| 208 | return EINVAL;
|
---|
| 209 | }
|
---|
| 210 | if ((dev->pipes[pipe_index].pipe->transfer_type != USB_TRANSFER_INTERRUPT)
|
---|
| 211 | || (dev->pipes[pipe_index].pipe->direction != USB_DIRECTION_IN)) {
|
---|
| 212 | return EINVAL;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[8989f48f] | 215 | usb_device_auto_polling_t *auto_polling
|
---|
| 216 | = malloc(sizeof(usb_device_auto_polling_t));
|
---|
| 217 | if (auto_polling == NULL) {
|
---|
| 218 | return ENOMEM;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[5e168be1] | 221 | auto_polling->debug = 1;
|
---|
[8989f48f] | 222 | auto_polling->auto_clear_halt = true;
|
---|
| 223 | auto_polling->delay = 0;
|
---|
| 224 | auto_polling->max_failures = MAX_FAILED_ATTEMPTS;
|
---|
| 225 | auto_polling->on_data = callback;
|
---|
| 226 | auto_polling->on_polling_end = terminated_callback;
|
---|
| 227 | auto_polling->on_error = NULL;
|
---|
| 228 |
|
---|
| 229 | int rc = usb_device_auto_polling(dev, pipe_index, auto_polling,
|
---|
| 230 | request_size, arg);
|
---|
| 231 |
|
---|
| 232 | free(auto_polling);
|
---|
| 233 |
|
---|
| 234 | return rc;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | /** Start automatic device polling over interrupt in pipe.
|
---|
| 238 | *
|
---|
| 239 | * The polling settings is copied thus it is okay to destroy the structure
|
---|
| 240 | * after this function returns.
|
---|
| 241 | *
|
---|
| 242 | * @warning There is no guarantee when the request to the device
|
---|
| 243 | * will be sent for the first time (it is possible that this
|
---|
| 244 | * first request would be executed prior to return from this function).
|
---|
| 245 | *
|
---|
| 246 | * @param dev Device to be periodically polled.
|
---|
| 247 | * @param pipe_index Index of the endpoint pipe used for polling.
|
---|
| 248 | * @param polling Polling settings.
|
---|
| 249 | * @param request_size How many bytes to ask for in each request.
|
---|
| 250 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
| 251 | * @return Error code.
|
---|
| 252 | * @retval EOK New fibril polling the device was already started.
|
---|
| 253 | */
|
---|
| 254 | int usb_device_auto_polling(usb_device_t *dev, size_t pipe_index,
|
---|
| 255 | usb_device_auto_polling_t *polling,
|
---|
| 256 | size_t request_size, void *arg)
|
---|
| 257 | {
|
---|
| 258 | if (dev == NULL) {
|
---|
| 259 | return EBADMEM;
|
---|
| 260 | }
|
---|
| 261 | if (pipe_index >= dev->pipes_count) {
|
---|
| 262 | return EINVAL;
|
---|
| 263 | }
|
---|
| 264 | if ((dev->pipes[pipe_index].pipe->transfer_type != USB_TRANSFER_INTERRUPT)
|
---|
| 265 | || (dev->pipes[pipe_index].pipe->direction != USB_DIRECTION_IN)) {
|
---|
| 266 | return EINVAL;
|
---|
| 267 | }
|
---|
| 268 | if ((polling == NULL) || (polling->on_data == NULL)) {
|
---|
| 269 | return EBADMEM;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[4bf94df] | 272 | polling_data_t *polling_data = malloc(sizeof(polling_data_t));
|
---|
| 273 | if (polling_data == NULL) {
|
---|
| 274 | return ENOMEM;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[8989f48f] | 277 | /* Fill-in the data. */
|
---|
| 278 | polling_data->buffer = malloc(sizeof(request_size));
|
---|
[4bf94df] | 279 | if (polling_data->buffer == NULL) {
|
---|
| 280 | free(polling_data);
|
---|
| 281 | return ENOMEM;
|
---|
| 282 | }
|
---|
[8989f48f] | 283 | polling_data->request_size = request_size;
|
---|
[4bf94df] | 284 | polling_data->dev = dev;
|
---|
| 285 | polling_data->pipe_index = pipe_index;
|
---|
| 286 | polling_data->custom_arg = arg;
|
---|
| 287 |
|
---|
[5e168be1] | 288 | polling_data->debug = polling->debug;
|
---|
[8989f48f] | 289 | polling_data->max_failures = polling->max_failures;
|
---|
| 290 | if (polling->delay >= 0) {
|
---|
| 291 | polling_data->delay = (useconds_t) polling->delay;
|
---|
| 292 | } else {
|
---|
| 293 | polling_data->delay = (useconds_t) dev->pipes[pipe_index]
|
---|
| 294 | .descriptor->poll_interval;
|
---|
| 295 | }
|
---|
| 296 | polling_data->auto_clear_halt = polling->auto_clear_halt;
|
---|
| 297 |
|
---|
| 298 | polling_data->on_data = polling->on_data;
|
---|
| 299 | polling_data->on_polling_end = polling->on_polling_end;
|
---|
| 300 | polling_data->on_error = polling->on_error;
|
---|
| 301 |
|
---|
[4bf94df] | 302 | fid_t fibril = fibril_create(polling_fibril, polling_data);
|
---|
| 303 | if (fibril == 0) {
|
---|
| 304 | free(polling_data->buffer);
|
---|
| 305 | free(polling_data);
|
---|
| 306 | return ENOMEM;
|
---|
| 307 | }
|
---|
| 308 | fibril_add_ready(fibril);
|
---|
| 309 |
|
---|
[8989f48f] | 310 | /* Fibril launched. That fibril will free the allocated data. */
|
---|
[4bf94df] | 311 |
|
---|
| 312 | return EOK;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | /**
|
---|
| 316 | * @}
|
---|
| 317 | */
|
---|