source: mainline/uspace/lib/usbdev/src/devpoll.c@ edc51615

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since edc51615 was edc51615, checked in by Petr Manek <petr.manek@…>, 7 years ago

usbdev: small fixes

  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[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 */
[58563585]35
[c01987c]36#include <usb/dev/device.h>
37#include <usb/dev/pipes.h>
[7d521e24]38#include <usb/dev/poll.h>
39#include <usb/dev/request.h>
[5e168be1]40#include <usb/classes/classes.h>
[c01987c]41#include <usb/debug.h>
42#include <usb/descriptor.h>
43#include <usb/usb.h>
44
45#include <assert.h>
46#include <async.h>
[4bf94df]47#include <errno.h>
[c01987c]48#include <fibril.h>
[8a0c52a]49#include <fibril_synch.h>
[c01987c]50#include <stdbool.h>
51#include <stdlib.h>
[4bf94df]52#include <str_error.h>
[8d2dd7f2]53#include <stddef.h>
54#include <stdint.h>
[4bf94df]55
[71f211f]56/** Private automated polling instance data. */
57struct usb_device_polling {
[6e3c005]58 /** Parameters for automated polling. */
[71f211f]59 usb_device_polling_config_t config;
[8989f48f]60
[6e3c005]61 /** USB device to poll. */
[4bf94df]62 usb_device_t *dev;
[71f211f]63
[bb70637]64 /** Device enpoint mapping to use for polling. */
[8a0c52a]65 usb_endpoint_mapping_t *ep_mapping;
[71f211f]66
[6e3c005]67 /** Size of the recieved data. */
[4bf94df]68 size_t request_size;
[71f211f]69
[6e3c005]70 /** Data buffer. */
[4bf94df]71 uint8_t *buffer;
[8a0c52a]72
73 /** True if polling is currently in operation. */
74 volatile bool running;
75
76 /** True if polling should terminate as soon as possible. */
77 volatile bool joining;
78
79 /** Synchronization primitives for joining polling end. */
80 fibril_mutex_t guard;
81 fibril_condvar_t cv;
[71f211f]82};
[4bf94df]83
[8989f48f]84
[8a0c52a]85static void polling_fini(usb_device_polling_t *polling)
86{
87 /* Free the allocated memory. */
88 free(polling->buffer);
89 free(polling);
90}
91
92
[4bf94df]93/** Polling fibril.
94 *
[71f211f]95 * @param arg Pointer to usb_device_polling_t.
[4bf94df]96 * @return Always EOK.
97 */
98static int polling_fibril(void *arg)
99{
[9dbfd288]100 assert(arg);
[8a0c52a]101 usb_device_polling_t *data = arg;
102 data->running = true;
[71f211f]103
[9dbfd288]104 /* Helper to reduce typing. */
[71f211f]105 const usb_device_polling_config_t *params = &data->config;
[4bf94df]106
[8a0c52a]107 usb_pipe_t *pipe = &data->ep_mapping->pipe;
[9dbfd288]108
109 if (params->debug > 0) {
[58563585]110 const usb_endpoint_mapping_t *mapping =
[8a0c52a]111 data->ep_mapping;
[b303275]112 usb_log_debug("Poll (%p): started polling of `%s' - " \
[5e168be1]113 "interface %d (%s,%d,%d), %zuB/%zu.\n",
[f6b2a76b]114 data, usb_device_get_name(data->dev),
[5e168be1]115 (int) mapping->interface->interface_number,
116 usb_str_class(mapping->interface->interface_class),
117 (int) mapping->interface->interface_subclass,
118 (int) mapping->interface->interface_protocol,
[9efad54]119 data->request_size, pipe->desc.max_transfer_size);
[5e168be1]120 }
[4bf94df]121
122 size_t failed_attempts = 0;
[9dbfd288]123 while (failed_attempts <= params->max_failures) {
[4bf94df]124 size_t actual_size;
[9dbfd288]125 const int rc = usb_pipe_read(pipe, data->buffer,
126 data->request_size, &actual_size);
[4bf94df]127
[51cc6cef]128 if (rc == EOK) {
129 if (params->debug > 1) {
[5e168be1]130 usb_log_debug(
[4125b7d]131 "Poll%p: received: '%s' (%zuB).\n",
[9dbfd288]132 data,
133 usb_debug_str_buffer(data->buffer,
[5e168be1]134 actual_size, 16),
135 actual_size);
[51cc6cef]136 }
137 } else {
[5e168be1]138 usb_log_debug(
[4125b7d]139 "Poll%p: polling failed: %s.\n",
[9dbfd288]140 data, str_error(rc));
[5e168be1]141 }
142
[8989f48f]143 /* If the pipe stalled, we can try to reset the stall. */
[9dbfd288]144 if ((rc == ESTALL) && (params->auto_clear_halt)) {
[8989f48f]145 /*
146 * We ignore error here as this is usually a futile
147 * attempt anyway.
148 */
149 usb_request_clear_endpoint_halt(
[bb70637]150 usb_device_get_default_pipe(data->dev),
[816f5f4]151 pipe->desc.endpoint_no);
[8989f48f]152 }
[4bf94df]153
154 if (rc != EOK) {
[9dbfd288]155 ++failed_attempts;
156 const bool cont = (params->on_error == NULL) ? true :
[a0487a2]157 params->on_error(data->dev, rc, params->arg);
[8a0c52a]158 if (!cont || data->joining) {
[a4eb520f]159 /* This is user requested abort, erases failures. */
160 failed_attempts = 0;
161 break;
[8989f48f]162 }
[4bf94df]163 continue;
164 }
165
166 /* We have the data, execute the callback now. */
[9dbfd288]167 assert(params->on_data);
168 const bool carry_on = params->on_data(
[a0487a2]169 data->dev, data->buffer, actual_size, params->arg);
[4bf94df]170
171 if (!carry_on) {
[9dbfd288]172 /* This is user requested abort, erases failures. */
[4bf94df]173 failed_attempts = 0;
174 break;
175 }
176
177 /* Reset as something might be only a temporary problem. */
178 failed_attempts = 0;
[8989f48f]179
180 /* Take a rest before next request. */
[816f5f4]181
[58563585]182 // FIXME TODO: This is broken, the time is in ms not us.
[ff0258f]183 // but first we need to fix drivers to actually stop using this,
[58563585]184 // since polling delay should be implemented in HC schedule
[9dbfd288]185 async_usleep(params->delay);
[4bf94df]186 }
187
[9dbfd288]188 const bool failed = failed_attempts > 0;
189
190 if (params->on_polling_end != NULL) {
[a0487a2]191 params->on_polling_end(data->dev, failed, params->arg);
[4bf94df]192 }
193
[9dbfd288]194 if (params->debug > 0) {
195 if (failed) {
196 usb_log_error("Polling of device `%s' terminated: "
[f6b2a76b]197 "recurring failures.\n",
198 usb_device_get_name(data->dev));
[5e168be1]199 } else {
[9dbfd288]200 usb_log_debug("Polling of device `%s' terminated: "
[f6b2a76b]201 "driver request.\n",
202 usb_device_get_name(data->dev));
[5e168be1]203 }
204 }
205
[8a0c52a]206 data->running = false;
207
[edc51615]208 /* Notify joiners, if any. */
209 fibril_condvar_broadcast(&data->cv);
210
211 /* Free allocated memory. */
212 if (!data->joining) {
[8a0c52a]213 polling_fini(data);
214 }
[4bf94df]215
216 return EOK;
217}
218
[8989f48f]219
220/** Start automatic device polling over interrupt in pipe.
221 *
222 * The polling settings is copied thus it is okay to destroy the structure
223 * after this function returns.
224 *
225 * @warning There is no guarantee when the request to the device
226 * will be sent for the first time (it is possible that this
227 * first request would be executed prior to return from this function).
228 *
229 * @param dev Device to be periodically polled.
[bb70637]230 * @param epm Endpoint mapping to use.
[71f211f]231 * @param config Polling settings.
[7dddd7b]232 * @param req_size How many bytes to ask for in each request.
[8989f48f]233 * @return Error code.
234 * @retval EOK New fibril polling the device was already started.
235 */
[71f211f]236int usb_device_poll(usb_device_t *dev, usb_endpoint_mapping_t *epm,
237 const usb_device_polling_config_t *config, size_t req_size,
238 usb_device_polling_t **handle)
[8989f48f]239{
[7dddd7b]240 int rc;
[71f211f]241 if (!dev || !config || !config->on_data)
[8989f48f]242 return EBADMEM;
[b81410f]243
[7dddd7b]244 if (!req_size)
[8989f48f]245 return EINVAL;
[816f5f4]246
247 if (!epm || (epm->pipe.desc.transfer_type != USB_TRANSFER_INTERRUPT) ||
248 (epm->pipe.desc.direction != USB_DIRECTION_IN))
[8989f48f]249 return EINVAL;
[816f5f4]250
[71f211f]251 usb_device_polling_t *instance = malloc(sizeof(usb_device_polling_t));
252 if (!instance)
[4bf94df]253 return ENOMEM;
254
[8989f48f]255 /* Fill-in the data. */
[71f211f]256 instance->buffer = malloc(req_size);
257 if (!instance->buffer) {
[7dddd7b]258 rc = ENOMEM;
[71f211f]259 goto err_instance;
[4bf94df]260 }
[71f211f]261 instance->request_size = req_size;
262 instance->dev = dev;
[8a0c52a]263 instance->ep_mapping = epm;
264 instance->joining = false;
265 fibril_mutex_initialize(&instance->guard);
266 fibril_condvar_initialize(&instance->cv);
[4bf94df]267
[2703331b]268 /* Copy provided settings. */
[71f211f]269 instance->config = *config;
[8989f48f]270
[2703331b]271 /* Negative value means use descriptor provided value. */
[71f211f]272 if (config->delay < 0) {
273 instance->config.delay = epm->descriptor->poll_interval;
[2703331b]274 }
[8989f48f]275
[71f211f]276 fid_t fibril = fibril_create(polling_fibril, instance);
[7dddd7b]277 if (!fibril) {
278 rc = ENOMEM;
279 goto err_buffer;
[4bf94df]280 }
281 fibril_add_ready(fibril);
282
[71f211f]283 if (handle)
284 *handle = instance;
285
[8989f48f]286 /* Fibril launched. That fibril will free the allocated data. */
[4bf94df]287 return EOK;
[bb70637]288
[7dddd7b]289err_buffer:
[71f211f]290 free(instance->buffer);
291err_instance:
292 free(instance);
[7dddd7b]293 return rc;
[bb70637]294}
[4bf94df]295
[8a0c52a]296int usb_device_poll_join(usb_device_polling_t *polling)
297{
298 int rc;
299 if (!polling)
300 return EBADMEM;
301
302 /* Set the flag */
303 polling->joining = true;
304
305 /* Unregister the pipe. */
306 if ((rc = usb_device_unmap_ep(polling->ep_mapping))) {
307 return rc;
308 }
309
310 /* Wait for the fibril to terminate. */
311 fibril_mutex_lock(&polling->guard);
312 while (polling->running)
313 fibril_condvar_wait(&polling->cv, &polling->guard);
314 fibril_mutex_unlock(&polling->guard);
315
316 /* Free the instance. */
317 polling_fini(polling);
318
319 return EOK;
320}
321
[4bf94df]322/**
323 * @}
324 */
Note: See TracBrowser for help on using the repository browser.