[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 | */
|
---|
[7d521e24] | 35 | #include <usb/dev/poll.h>
|
---|
| 36 | #include <usb/dev/request.h>
|
---|
[4bf94df] | 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 {
|
---|
[6e3c005] | 48 | /** Parameters for automated polling. */
|
---|
[2703331b] | 49 | usb_device_auto_polling_t auto_polling;
|
---|
[8989f48f] | 50 |
|
---|
[6e3c005] | 51 | /** USB device to poll. */
|
---|
[4bf94df] | 52 | usb_device_t *dev;
|
---|
[6e3c005] | 53 | /** Device pipe to use for polling. */
|
---|
[4bf94df] | 54 | size_t pipe_index;
|
---|
[6e3c005] | 55 | /** Size of the recieved data. */
|
---|
[4bf94df] | 56 | size_t request_size;
|
---|
[6e3c005] | 57 | /** Data buffer. */
|
---|
[4bf94df] | 58 | uint8_t *buffer;
|
---|
[6e3c005] | 59 | /** Argument to pass to callbacks. */
|
---|
[4bf94df] | 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 | {
|
---|
[9dbfd288] | 71 | assert(arg);
|
---|
| 72 | const polling_data_t *data = arg;
|
---|
| 73 | /* Helper to reduce typing. */
|
---|
| 74 | const usb_device_auto_polling_t *params = &data->auto_polling;
|
---|
[4bf94df] | 75 |
|
---|
[a372663] | 76 | usb_pipe_t *pipe
|
---|
[9dbfd288] | 77 | = &data->dev->pipes[data->pipe_index].pipe;
|
---|
| 78 |
|
---|
| 79 | if (params->debug > 0) {
|
---|
[b81410f] | 80 | const usb_endpoint_mapping_t *mapping
|
---|
[9dbfd288] | 81 | = &data->dev->pipes[data->pipe_index];
|
---|
[4125b7d] | 82 | usb_log_debug("Poll%p: started polling of `%s' - " \
|
---|
[5e168be1] | 83 | "interface %d (%s,%d,%d), %zuB/%zu.\n",
|
---|
[9dbfd288] | 84 | data, data->dev->ddf_dev->name,
|
---|
[5e168be1] | 85 | (int) mapping->interface->interface_number,
|
---|
| 86 | usb_str_class(mapping->interface->interface_class),
|
---|
| 87 | (int) mapping->interface->interface_subclass,
|
---|
| 88 | (int) mapping->interface->interface_protocol,
|
---|
[9dbfd288] | 89 | data->request_size, pipe->max_packet_size);
|
---|
[5e168be1] | 90 | }
|
---|
[4bf94df] | 91 |
|
---|
[d085fbbe] | 92 | usb_pipe_start_long_transfer(pipe);
|
---|
[4bf94df] | 93 | size_t failed_attempts = 0;
|
---|
[9dbfd288] | 94 | while (failed_attempts <= params->max_failures) {
|
---|
[4bf94df] | 95 | size_t actual_size;
|
---|
[9dbfd288] | 96 | const int rc = usb_pipe_read(pipe, data->buffer,
|
---|
| 97 | data->request_size, &actual_size);
|
---|
[4bf94df] | 98 |
|
---|
[9dbfd288] | 99 | if (params->debug > 1) {
|
---|
[5e168be1] | 100 | if (rc == EOK) {
|
---|
| 101 | usb_log_debug(
|
---|
[4125b7d] | 102 | "Poll%p: received: '%s' (%zuB).\n",
|
---|
[9dbfd288] | 103 | data,
|
---|
| 104 | usb_debug_str_buffer(data->buffer,
|
---|
[5e168be1] | 105 | actual_size, 16),
|
---|
| 106 | actual_size);
|
---|
| 107 | } else {
|
---|
| 108 | usb_log_debug(
|
---|
[4125b7d] | 109 | "Poll%p: polling failed: %s.\n",
|
---|
[9dbfd288] | 110 | data, str_error(rc));
|
---|
[5e168be1] | 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[8989f48f] | 114 | /* If the pipe stalled, we can try to reset the stall. */
|
---|
[9dbfd288] | 115 | if ((rc == ESTALL) && (params->auto_clear_halt)) {
|
---|
[8989f48f] | 116 | /*
|
---|
| 117 | * We ignore error here as this is usually a futile
|
---|
| 118 | * attempt anyway.
|
---|
| 119 | */
|
---|
| 120 | usb_request_clear_endpoint_halt(
|
---|
[9dbfd288] | 121 | &data->dev->ctrl_pipe, pipe->endpoint_no);
|
---|
[8989f48f] | 122 | }
|
---|
[4bf94df] | 123 |
|
---|
| 124 | if (rc != EOK) {
|
---|
[9dbfd288] | 125 | ++failed_attempts;
|
---|
| 126 | const bool cont = (params->on_error == NULL) ? true :
|
---|
| 127 | params->on_error(data->dev, rc, data->custom_arg);
|
---|
| 128 | if (!cont) {
|
---|
| 129 | failed_attempts = params->max_failures;
|
---|
[8989f48f] | 130 | }
|
---|
[4bf94df] | 131 | continue;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /* We have the data, execute the callback now. */
|
---|
[9dbfd288] | 135 | assert(params->on_data);
|
---|
| 136 | const bool carry_on = params->on_data(
|
---|
| 137 | data->dev, data->buffer, actual_size, data->custom_arg);
|
---|
[4bf94df] | 138 |
|
---|
| 139 | if (!carry_on) {
|
---|
[9dbfd288] | 140 | /* This is user requested abort, erases failures. */
|
---|
[4bf94df] | 141 | failed_attempts = 0;
|
---|
| 142 | break;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /* Reset as something might be only a temporary problem. */
|
---|
| 146 | failed_attempts = 0;
|
---|
[8989f48f] | 147 |
|
---|
| 148 | /* Take a rest before next request. */
|
---|
[9dbfd288] | 149 | async_usleep(params->delay);
|
---|
[4bf94df] | 150 | }
|
---|
| 151 |
|
---|
[d085fbbe] | 152 | usb_pipe_end_long_transfer(pipe);
|
---|
[9dbfd288] | 153 |
|
---|
| 154 | const bool failed = failed_attempts > 0;
|
---|
| 155 |
|
---|
| 156 | if (params->on_polling_end != NULL) {
|
---|
| 157 | params->on_polling_end(data->dev, failed, data->custom_arg);
|
---|
[4bf94df] | 158 | }
|
---|
| 159 |
|
---|
[9dbfd288] | 160 | if (params->debug > 0) {
|
---|
| 161 | if (failed) {
|
---|
| 162 | usb_log_error("Polling of device `%s' terminated: "
|
---|
| 163 | "recurring failures.\n", data->dev->ddf_dev->name);
|
---|
[5e168be1] | 164 | } else {
|
---|
[9dbfd288] | 165 | usb_log_debug("Polling of device `%s' terminated: "
|
---|
| 166 | "driver request.\n", data->dev->ddf_dev->name);
|
---|
[5e168be1] | 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[4bf94df] | 170 | /* Free the allocated memory. */
|
---|
[9dbfd288] | 171 | free(data->buffer);
|
---|
| 172 | free(data);
|
---|
[4bf94df] | 173 |
|
---|
| 174 | return EOK;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /** Start automatic device polling over interrupt in pipe.
|
---|
| 178 | *
|
---|
| 179 | * @warning It is up to the callback to produce delays between individual
|
---|
| 180 | * requests.
|
---|
| 181 | *
|
---|
| 182 | * @warning There is no guarantee when the request to the device
|
---|
| 183 | * will be sent for the first time (it is possible that this
|
---|
| 184 | * first request would be executed prior to return from this function).
|
---|
| 185 | *
|
---|
| 186 | * @param dev Device to be periodically polled.
|
---|
| 187 | * @param pipe_index Index of the endpoint pipe used for polling.
|
---|
| 188 | * @param callback Callback when data are available.
|
---|
| 189 | * @param request_size How many bytes to ask for in each request.
|
---|
| 190 | * @param terminated_callback Callback when polling is terminated.
|
---|
| 191 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
| 192 | * @return Error code.
|
---|
| 193 | * @retval EOK New fibril polling the device was already started.
|
---|
| 194 | */
|
---|
| 195 | int usb_device_auto_poll(usb_device_t *dev, size_t pipe_index,
|
---|
| 196 | usb_polling_callback_t callback, size_t request_size,
|
---|
| 197 | usb_polling_terminted_callback_t terminated_callback, void *arg)
|
---|
| 198 | {
|
---|
[99a1a56] | 199 | const usb_device_auto_polling_t auto_polling = {
|
---|
| 200 | .debug = 1,
|
---|
| 201 | .auto_clear_halt = true,
|
---|
| 202 | .delay = 0,
|
---|
| 203 | .max_failures = MAX_FAILED_ATTEMPTS,
|
---|
| 204 | .on_data = callback,
|
---|
| 205 | .on_polling_end = terminated_callback,
|
---|
| 206 | .on_error = NULL,
|
---|
| 207 | };
|
---|
[8989f48f] | 208 |
|
---|
[99a1a56] | 209 | return usb_device_auto_polling(dev, pipe_index, &auto_polling,
|
---|
[8989f48f] | 210 | request_size, arg);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | /** Start automatic device polling over interrupt in pipe.
|
---|
| 214 | *
|
---|
| 215 | * The polling settings is copied thus it is okay to destroy the structure
|
---|
| 216 | * after this function returns.
|
---|
| 217 | *
|
---|
| 218 | * @warning There is no guarantee when the request to the device
|
---|
| 219 | * will be sent for the first time (it is possible that this
|
---|
| 220 | * first request would be executed prior to return from this function).
|
---|
| 221 | *
|
---|
| 222 | * @param dev Device to be periodically polled.
|
---|
| 223 | * @param pipe_index Index of the endpoint pipe used for polling.
|
---|
| 224 | * @param polling Polling settings.
|
---|
| 225 | * @param request_size How many bytes to ask for in each request.
|
---|
| 226 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
| 227 | * @return Error code.
|
---|
| 228 | * @retval EOK New fibril polling the device was already started.
|
---|
| 229 | */
|
---|
| 230 | int usb_device_auto_polling(usb_device_t *dev, size_t pipe_index,
|
---|
[99a1a56] | 231 | const usb_device_auto_polling_t *polling,
|
---|
[8989f48f] | 232 | size_t request_size, void *arg)
|
---|
| 233 | {
|
---|
[b81410f] | 234 | if ((dev == NULL) || (polling == NULL) || (polling->on_data == NULL)) {
|
---|
[8989f48f] | 235 | return EBADMEM;
|
---|
| 236 | }
|
---|
[b81410f] | 237 |
|
---|
| 238 | if (pipe_index >= dev->pipes_count || request_size == 0) {
|
---|
[8989f48f] | 239 | return EINVAL;
|
---|
| 240 | }
|
---|
[b77931d] | 241 | if ((dev->pipes[pipe_index].pipe.transfer_type != USB_TRANSFER_INTERRUPT)
|
---|
| 242 | || (dev->pipes[pipe_index].pipe.direction != USB_DIRECTION_IN)) {
|
---|
[8989f48f] | 243 | return EINVAL;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[4bf94df] | 246 | polling_data_t *polling_data = malloc(sizeof(polling_data_t));
|
---|
| 247 | if (polling_data == NULL) {
|
---|
| 248 | return ENOMEM;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[8989f48f] | 251 | /* Fill-in the data. */
|
---|
| 252 | polling_data->buffer = malloc(sizeof(request_size));
|
---|
[4bf94df] | 253 | if (polling_data->buffer == NULL) {
|
---|
| 254 | free(polling_data);
|
---|
| 255 | return ENOMEM;
|
---|
| 256 | }
|
---|
[8989f48f] | 257 | polling_data->request_size = request_size;
|
---|
[4bf94df] | 258 | polling_data->dev = dev;
|
---|
| 259 | polling_data->pipe_index = pipe_index;
|
---|
| 260 | polling_data->custom_arg = arg;
|
---|
| 261 |
|
---|
[2703331b] | 262 | /* Copy provided settings. */
|
---|
| 263 | polling_data->auto_polling = *polling;
|
---|
[8989f48f] | 264 |
|
---|
[2703331b] | 265 | /* Negative value means use descriptor provided value. */
|
---|
| 266 | if (polling->delay < 0) {
|
---|
| 267 | polling_data->auto_polling.delay =
|
---|
| 268 | (int) dev->pipes[pipe_index].descriptor->poll_interval;
|
---|
| 269 | }
|
---|
[8989f48f] | 270 |
|
---|
[4bf94df] | 271 | fid_t fibril = fibril_create(polling_fibril, polling_data);
|
---|
| 272 | if (fibril == 0) {
|
---|
| 273 | free(polling_data->buffer);
|
---|
| 274 | free(polling_data);
|
---|
| 275 | return ENOMEM;
|
---|
| 276 | }
|
---|
| 277 | fibril_add_ready(fibril);
|
---|
| 278 |
|
---|
[8989f48f] | 279 | /* Fibril launched. That fibril will free the allocated data. */
|
---|
[4bf94df] | 280 |
|
---|
| 281 | return EOK;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | /**
|
---|
| 285 | * @}
|
---|
| 286 | */
|
---|