[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;
|
---|
[bb70637] | 53 | /** Device enpoint mapping to use for polling. */
|
---|
| 54 | usb_endpoint_mapping_t *polling_mapping;
|
---|
[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 |
|
---|
[bb70637] | 74 | usb_pipe_t *pipe = &data->polling_mapping->pipe;
|
---|
[9dbfd288] | 75 |
|
---|
| 76 | if (params->debug > 0) {
|
---|
[b81410f] | 77 | const usb_endpoint_mapping_t *mapping
|
---|
[bb70637] | 78 | = data->polling_mapping;
|
---|
[4125b7d] | 79 | usb_log_debug("Poll%p: started polling of `%s' - " \
|
---|
[5e168be1] | 80 | "interface %d (%s,%d,%d), %zuB/%zu.\n",
|
---|
[f6b2a76b] | 81 | data, usb_device_get_name(data->dev),
|
---|
[5e168be1] | 82 | (int) mapping->interface->interface_number,
|
---|
| 83 | usb_str_class(mapping->interface->interface_class),
|
---|
| 84 | (int) mapping->interface->interface_subclass,
|
---|
| 85 | (int) mapping->interface->interface_protocol,
|
---|
[9dbfd288] | 86 | data->request_size, pipe->max_packet_size);
|
---|
[5e168be1] | 87 | }
|
---|
[4bf94df] | 88 |
|
---|
| 89 | size_t failed_attempts = 0;
|
---|
[9dbfd288] | 90 | while (failed_attempts <= params->max_failures) {
|
---|
[4bf94df] | 91 | size_t actual_size;
|
---|
[9dbfd288] | 92 | const int rc = usb_pipe_read(pipe, data->buffer,
|
---|
| 93 | data->request_size, &actual_size);
|
---|
[4bf94df] | 94 |
|
---|
[9dbfd288] | 95 | if (params->debug > 1) {
|
---|
[5e168be1] | 96 | if (rc == EOK) {
|
---|
| 97 | usb_log_debug(
|
---|
[4125b7d] | 98 | "Poll%p: received: '%s' (%zuB).\n",
|
---|
[9dbfd288] | 99 | data,
|
---|
| 100 | usb_debug_str_buffer(data->buffer,
|
---|
[5e168be1] | 101 | actual_size, 16),
|
---|
| 102 | actual_size);
|
---|
| 103 | } else {
|
---|
| 104 | usb_log_debug(
|
---|
[4125b7d] | 105 | "Poll%p: polling failed: %s.\n",
|
---|
[9dbfd288] | 106 | data, str_error(rc));
|
---|
[5e168be1] | 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[8989f48f] | 110 | /* If the pipe stalled, we can try to reset the stall. */
|
---|
[9dbfd288] | 111 | if ((rc == ESTALL) && (params->auto_clear_halt)) {
|
---|
[8989f48f] | 112 | /*
|
---|
| 113 | * We ignore error here as this is usually a futile
|
---|
| 114 | * attempt anyway.
|
---|
| 115 | */
|
---|
| 116 | usb_request_clear_endpoint_halt(
|
---|
[bb70637] | 117 | usb_device_get_default_pipe(data->dev),
|
---|
| 118 | pipe->endpoint_no);
|
---|
[8989f48f] | 119 | }
|
---|
[4bf94df] | 120 |
|
---|
| 121 | if (rc != EOK) {
|
---|
[9dbfd288] | 122 | ++failed_attempts;
|
---|
| 123 | const bool cont = (params->on_error == NULL) ? true :
|
---|
[a0487a2] | 124 | params->on_error(data->dev, rc, params->arg);
|
---|
[9dbfd288] | 125 | if (!cont) {
|
---|
| 126 | failed_attempts = params->max_failures;
|
---|
[8989f48f] | 127 | }
|
---|
[4bf94df] | 128 | continue;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /* We have the data, execute the callback now. */
|
---|
[9dbfd288] | 132 | assert(params->on_data);
|
---|
| 133 | const bool carry_on = params->on_data(
|
---|
[a0487a2] | 134 | data->dev, data->buffer, actual_size, params->arg);
|
---|
[4bf94df] | 135 |
|
---|
| 136 | if (!carry_on) {
|
---|
[9dbfd288] | 137 | /* This is user requested abort, erases failures. */
|
---|
[4bf94df] | 138 | failed_attempts = 0;
|
---|
| 139 | break;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /* Reset as something might be only a temporary problem. */
|
---|
| 143 | failed_attempts = 0;
|
---|
[8989f48f] | 144 |
|
---|
| 145 | /* Take a rest before next request. */
|
---|
[9dbfd288] | 146 | async_usleep(params->delay);
|
---|
[4bf94df] | 147 | }
|
---|
| 148 |
|
---|
[9dbfd288] | 149 | const bool failed = failed_attempts > 0;
|
---|
| 150 |
|
---|
| 151 | if (params->on_polling_end != NULL) {
|
---|
[a0487a2] | 152 | params->on_polling_end(data->dev, failed, params->arg);
|
---|
[4bf94df] | 153 | }
|
---|
| 154 |
|
---|
[9dbfd288] | 155 | if (params->debug > 0) {
|
---|
| 156 | if (failed) {
|
---|
| 157 | usb_log_error("Polling of device `%s' terminated: "
|
---|
[f6b2a76b] | 158 | "recurring failures.\n",
|
---|
| 159 | usb_device_get_name(data->dev));
|
---|
[5e168be1] | 160 | } else {
|
---|
[9dbfd288] | 161 | usb_log_debug("Polling of device `%s' terminated: "
|
---|
[f6b2a76b] | 162 | "driver request.\n",
|
---|
| 163 | usb_device_get_name(data->dev));
|
---|
[5e168be1] | 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[4bf94df] | 167 | /* Free the allocated memory. */
|
---|
[9dbfd288] | 168 | free(data->buffer);
|
---|
| 169 | free(data);
|
---|
[4bf94df] | 170 |
|
---|
| 171 | return EOK;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[8989f48f] | 174 |
|
---|
| 175 | /** Start automatic device polling over interrupt in pipe.
|
---|
| 176 | *
|
---|
| 177 | * The polling settings is copied thus it is okay to destroy the structure
|
---|
| 178 | * after this function returns.
|
---|
| 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.
|
---|
[bb70637] | 185 | * @param epm Endpoint mapping to use.
|
---|
[8989f48f] | 186 | * @param polling Polling settings.
|
---|
| 187 | * @param request_size How many bytes to ask for in each request.
|
---|
| 188 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
| 189 | * @return Error code.
|
---|
| 190 | * @retval EOK New fibril polling the device was already started.
|
---|
| 191 | */
|
---|
[bb70637] | 192 | static int usb_device_auto_polling_internal(usb_device_t *dev,
|
---|
| 193 | usb_endpoint_mapping_t *epm, const usb_device_auto_polling_t *polling,
|
---|
[a0487a2] | 194 | size_t request_size)
|
---|
[8989f48f] | 195 | {
|
---|
[b81410f] | 196 | if ((dev == NULL) || (polling == NULL) || (polling->on_data == NULL)) {
|
---|
[8989f48f] | 197 | return EBADMEM;
|
---|
| 198 | }
|
---|
[b81410f] | 199 |
|
---|
[bb70637] | 200 | if (request_size == 0)
|
---|
[8989f48f] | 201 | return EINVAL;
|
---|
[bb70637] | 202 |
|
---|
| 203 | if (!epm || (epm->pipe.transfer_type != USB_TRANSFER_INTERRUPT) ||
|
---|
| 204 | (epm->pipe.direction != USB_DIRECTION_IN))
|
---|
[8989f48f] | 205 | return EINVAL;
|
---|
[bb70637] | 206 |
|
---|
[8989f48f] | 207 |
|
---|
[4bf94df] | 208 | polling_data_t *polling_data = malloc(sizeof(polling_data_t));
|
---|
| 209 | if (polling_data == NULL) {
|
---|
| 210 | return ENOMEM;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[8989f48f] | 213 | /* Fill-in the data. */
|
---|
| 214 | polling_data->buffer = malloc(sizeof(request_size));
|
---|
[4bf94df] | 215 | if (polling_data->buffer == NULL) {
|
---|
| 216 | free(polling_data);
|
---|
| 217 | return ENOMEM;
|
---|
| 218 | }
|
---|
[8989f48f] | 219 | polling_data->request_size = request_size;
|
---|
[4bf94df] | 220 | polling_data->dev = dev;
|
---|
[bb70637] | 221 | polling_data->polling_mapping = epm;
|
---|
[4bf94df] | 222 |
|
---|
[2703331b] | 223 | /* Copy provided settings. */
|
---|
| 224 | polling_data->auto_polling = *polling;
|
---|
[8989f48f] | 225 |
|
---|
[2703331b] | 226 | /* Negative value means use descriptor provided value. */
|
---|
| 227 | if (polling->delay < 0) {
|
---|
| 228 | polling_data->auto_polling.delay =
|
---|
[bb70637] | 229 | epm->descriptor->poll_interval;
|
---|
[2703331b] | 230 | }
|
---|
[8989f48f] | 231 |
|
---|
[4bf94df] | 232 | fid_t fibril = fibril_create(polling_fibril, polling_data);
|
---|
| 233 | if (fibril == 0) {
|
---|
| 234 | free(polling_data->buffer);
|
---|
| 235 | free(polling_data);
|
---|
| 236 | return ENOMEM;
|
---|
| 237 | }
|
---|
| 238 | fibril_add_ready(fibril);
|
---|
| 239 |
|
---|
[8989f48f] | 240 | /* Fibril launched. That fibril will free the allocated data. */
|
---|
[4bf94df] | 241 |
|
---|
| 242 | return EOK;
|
---|
| 243 | }
|
---|
[bb70637] | 244 | /** Start automatic device polling over interrupt in pipe.
|
---|
| 245 | *
|
---|
| 246 | * The polling settings is copied thus it is okay to destroy the structure
|
---|
| 247 | * after this function returns.
|
---|
| 248 | *
|
---|
| 249 | * @warning There is no guarantee when the request to the device
|
---|
| 250 | * will be sent for the first time (it is possible that this
|
---|
| 251 | * first request would be executed prior to return from this function).
|
---|
| 252 | *
|
---|
| 253 | * @param dev Device to be periodically polled.
|
---|
| 254 | * @param pipe_index Index of the endpoint pipe used for polling.
|
---|
| 255 | * @param polling Polling settings.
|
---|
| 256 | * @param req_size How many bytes to ask for in each request.
|
---|
| 257 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
| 258 | * @return Error code.
|
---|
| 259 | * @retval EOK New fibril polling the device was already started.
|
---|
| 260 | */
|
---|
| 261 | int usb_device_auto_polling(usb_device_t *usb_dev, usb_endpoint_t ep,
|
---|
| 262 | const usb_device_auto_polling_t *polling, size_t req_size)
|
---|
| 263 | {
|
---|
| 264 | usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep(usb_dev, ep);
|
---|
| 265 | return usb_device_auto_polling_internal(usb_dev, epm, polling, req_size);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | /** Start automatic device polling over interrupt in pipe.
|
---|
| 269 | *
|
---|
| 270 | * @warning It is up to the callback to produce delays between individual
|
---|
| 271 | * requests.
|
---|
| 272 | *
|
---|
| 273 | * @warning There is no guarantee when the request to the device
|
---|
| 274 | * will be sent for the first time (it is possible that this
|
---|
| 275 | * first request would be executed prior to return from this function).
|
---|
| 276 | *
|
---|
| 277 | * @param dev Device to be periodically polled.
|
---|
| 278 | * @param ep Endpoint used for polling.
|
---|
| 279 | * @param callback Callback when data are available.
|
---|
| 280 | * @param request_size How many bytes to ask for in each request.
|
---|
| 281 | * @param delay NUmber of ms to wait between queries, -1 to use descriptor val.
|
---|
| 282 | * @param terminated_callback Callback when polling is terminated.
|
---|
| 283 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
| 284 | * @return Error code.
|
---|
| 285 | * @retval EOK New fibril polling the device was already started.
|
---|
| 286 | */
|
---|
| 287 | int usb_device_auto_poll(usb_device_t *dev, usb_endpoint_t ep,
|
---|
| 288 | usb_polling_callback_t callback, size_t request_size, int delay,
|
---|
| 289 | usb_polling_terminted_callback_t terminated_callback, void *arg)
|
---|
| 290 | {
|
---|
| 291 | const usb_device_auto_polling_t auto_polling = {
|
---|
| 292 | .debug = 1,
|
---|
| 293 | .auto_clear_halt = true,
|
---|
| 294 | .delay = delay,
|
---|
| 295 | .max_failures = MAX_FAILED_ATTEMPTS,
|
---|
| 296 | .on_data = callback,
|
---|
| 297 | .on_polling_end = terminated_callback,
|
---|
| 298 | .on_error = NULL,
|
---|
| 299 | .arg = arg,
|
---|
| 300 | };
|
---|
| 301 |
|
---|
| 302 | usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep(dev, ep);
|
---|
| 303 | return usb_device_auto_polling_internal(
|
---|
| 304 | dev, epm, &auto_polling, request_size);
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | int usb_device_auto_polling_desc(usb_device_t *usb_dev,
|
---|
| 308 | const usb_endpoint_description_t *desc,
|
---|
| 309 | const usb_device_auto_polling_t *polling, size_t req_size)
|
---|
| 310 | {
|
---|
| 311 | usb_endpoint_mapping_t *epm =
|
---|
| 312 | usb_device_get_mapped_ep_desc(usb_dev, desc);
|
---|
| 313 | return usb_device_auto_polling_internal(usb_dev, epm, polling, req_size);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | int usb_device_auto_poll_desc(usb_device_t * usb_dev,
|
---|
| 317 | const usb_endpoint_description_t *desc, usb_polling_callback_t callback,
|
---|
| 318 | size_t req_size, int delay,
|
---|
| 319 | usb_polling_terminted_callback_t terminated_callback, void *arg)
|
---|
| 320 | {
|
---|
| 321 | const usb_device_auto_polling_t auto_polling = {
|
---|
| 322 | .debug = 1,
|
---|
| 323 | .auto_clear_halt = true,
|
---|
| 324 | .delay = delay,
|
---|
| 325 | .max_failures = MAX_FAILED_ATTEMPTS,
|
---|
| 326 | .on_data = callback,
|
---|
| 327 | .on_polling_end = terminated_callback,
|
---|
| 328 | .on_error = NULL,
|
---|
| 329 | .arg = arg,
|
---|
| 330 | };
|
---|
| 331 |
|
---|
| 332 | usb_endpoint_mapping_t *epm =
|
---|
| 333 | usb_device_get_mapped_ep_desc(usb_dev, desc);
|
---|
| 334 | return usb_device_auto_polling_internal(
|
---|
| 335 | usb_dev, epm, &auto_polling, req_size);
|
---|
| 336 | }
|
---|
[4bf94df] | 337 |
|
---|
| 338 | /**
|
---|
| 339 | * @}
|
---|
| 340 | */
|
---|