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