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