source: mainline/uspace/lib/usb/src/devpoll.c@ ba038f4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ba038f4 was 4ede178, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

No start/end pipe session calls in libusb

  • Property mode set to 100644
File size: 6.0 KB
Line 
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
29/** @addtogroup libusb
30 * @{
31 */
32/** @file
33 * USB device driver framework - automatic interrupt polling.
34 */
35#include <usb/devdrv.h>
36#include <usb/request.h>
37#include <usb/debug.h>
38#include <errno.h>
39#include <str_error.h>
40#include <assert.h>
41
42/** Maximum number of failed consecutive requests before announcing failure. */
43#define MAX_FAILED_ATTEMPTS 3
44
45/** Data needed for polling. */
46typedef struct {
47 usb_device_t *dev;
48 size_t pipe_index;
49 usb_polling_callback_t callback;
50 usb_polling_terminted_callback_t terminated_callback;
51 size_t request_size;
52 uint8_t *buffer;
53 void *custom_arg;
54} polling_data_t;
55
56/** Polling fibril.
57 *
58 * @param arg Pointer to polling_data_t.
59 * @return Always EOK.
60 */
61static int polling_fibril(void *arg)
62{
63 polling_data_t *polling_data = (polling_data_t *) arg;
64 assert(polling_data);
65
66 usb_pipe_t *pipe
67 = polling_data->dev->pipes[polling_data->pipe_index].pipe;
68
69 usb_log_debug("Pipe interface number: %d, protocol: %d, subclass: %d, max packet size: %d\n",
70 polling_data->dev->pipes[polling_data->pipe_index].interface_no,
71 polling_data->dev->pipes[polling_data->pipe_index].description->interface_protocol,
72 polling_data->dev->pipes[polling_data->pipe_index].description->interface_subclass,
73 pipe->max_packet_size);
74
75 size_t failed_attempts = 0;
76 while (failed_attempts < MAX_FAILED_ATTEMPTS) {
77 int rc;
78
79 size_t actual_size;
80 rc = usb_pipe_read(pipe, polling_data->buffer,
81 polling_data->request_size, &actual_size);
82
83
84// if (rc == ESTALL) {
85// usb_log_debug("Seding clear feature...\n");
86// usb_request_clear_feature(pipe, USB_REQUEST_TYPE_STANDARD,
87// USB_REQUEST_RECIPIENT_ENDPOINT, 0, pipe->endpoint_no);
88// continue;
89// }
90
91 if (rc != EOK) {
92 failed_attempts++;
93 continue;
94 }
95
96 /* We have the data, execute the callback now. */
97 bool carry_on = polling_data->callback(polling_data->dev,
98 polling_data->buffer, actual_size,
99 polling_data->custom_arg);
100
101 if (!carry_on) {
102 failed_attempts = 0;
103 break;
104 }
105
106 /* Reset as something might be only a temporary problem. */
107 failed_attempts = 0;
108 }
109
110 if (failed_attempts > 0) {
111 usb_log_error(
112 "Polling of device `%s' terminated: recurring failures.\n",
113 polling_data->dev->ddf_dev->name);
114 }
115
116 if (polling_data->terminated_callback != NULL) {
117 polling_data->terminated_callback(polling_data->dev,
118 failed_attempts > 0, polling_data->custom_arg);
119 }
120
121 /* Free the allocated memory. */
122 free(polling_data->buffer);
123 free(polling_data);
124
125 return EOK;
126}
127
128/** Start automatic device polling over interrupt in pipe.
129 *
130 * @warning It is up to the callback to produce delays between individual
131 * requests.
132 *
133 * @warning There is no guarantee when the request to the device
134 * will be sent for the first time (it is possible that this
135 * first request would be executed prior to return from this function).
136 *
137 * @param dev Device to be periodically polled.
138 * @param pipe_index Index of the endpoint pipe used for polling.
139 * @param callback Callback when data are available.
140 * @param request_size How many bytes to ask for in each request.
141 * @param terminated_callback Callback when polling is terminated.
142 * @param arg Custom argument (passed as is to the callbacks).
143 * @return Error code.
144 * @retval EOK New fibril polling the device was already started.
145 */
146int usb_device_auto_poll(usb_device_t *dev, size_t pipe_index,
147 usb_polling_callback_t callback, size_t request_size,
148 usb_polling_terminted_callback_t terminated_callback, void *arg)
149{
150 if ((dev == NULL) || (callback == NULL)) {
151 return EBADMEM;
152 }
153 if (request_size == 0) {
154 return EINVAL;
155 }
156 if ((dev->pipes[pipe_index].pipe->transfer_type != USB_TRANSFER_INTERRUPT)
157 || (dev->pipes[pipe_index].pipe->direction != USB_DIRECTION_IN)) {
158 return EINVAL;
159 }
160
161 polling_data_t *polling_data = malloc(sizeof(polling_data_t));
162 if (polling_data == NULL) {
163 return ENOMEM;
164 }
165
166 /* Allocate now to prevent immediate failure in the polling fibril. */
167 polling_data->buffer = malloc(request_size);
168 if (polling_data->buffer == NULL) {
169 free(polling_data);
170 return ENOMEM;
171 }
172 polling_data->dev = dev;
173 polling_data->pipe_index = pipe_index;
174 polling_data->callback = callback;
175 polling_data->terminated_callback = terminated_callback;
176 polling_data->request_size = request_size;
177 polling_data->custom_arg = arg;
178
179 fid_t fibril = fibril_create(polling_fibril, polling_data);
180 if (fibril == 0) {
181 free(polling_data->buffer);
182 free(polling_data);
183 /* FIXME: better error code. */
184 return ENOMEM;
185 }
186 fibril_add_ready(fibril);
187
188 /* The allocated buffer etc. will be freed by the fibril. */
189
190 return EOK;
191}
192
193/**
194 * @}
195 */
Note: See TracBrowser for help on using the repository browser.