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 libusbdev
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * USB device driver framework - automatic interrupt polling.
|
---|
34 | */
|
---|
35 | #include <usb/dev/poll.h>
|
---|
36 | #include <usb/dev/request.h>
|
---|
37 | #include <usb/debug.h>
|
---|
38 | #include <usb/classes/classes.h>
|
---|
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 {
|
---|
48 | usb_device_auto_polling_t auto_polling;
|
---|
49 |
|
---|
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 |
|
---|
57 |
|
---|
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 | {
|
---|
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;
|
---|
69 |
|
---|
70 | usb_pipe_t *pipe
|
---|
71 | = &data->dev->pipes[data->pipe_index].pipe;
|
---|
72 |
|
---|
73 | if (params->debug > 0) {
|
---|
74 | const usb_endpoint_mapping_t *mapping
|
---|
75 | = &data->dev->pipes[data->pipe_index];
|
---|
76 | usb_log_debug("Poll%p: started polling of `%s' - " \
|
---|
77 | "interface %d (%s,%d,%d), %zuB/%zu.\n",
|
---|
78 | data, data->dev->ddf_dev->name,
|
---|
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,
|
---|
83 | data->request_size, pipe->max_packet_size);
|
---|
84 | }
|
---|
85 |
|
---|
86 | usb_pipe_start_long_transfer(pipe);
|
---|
87 | size_t failed_attempts = 0;
|
---|
88 | while (failed_attempts <= params->max_failures) {
|
---|
89 | size_t actual_size;
|
---|
90 | const int rc = usb_pipe_read(pipe, data->buffer,
|
---|
91 | data->request_size, &actual_size);
|
---|
92 |
|
---|
93 | if (params->debug > 1) {
|
---|
94 | if (rc == EOK) {
|
---|
95 | usb_log_debug(
|
---|
96 | "Poll%p: received: '%s' (%zuB).\n",
|
---|
97 | data,
|
---|
98 | usb_debug_str_buffer(data->buffer,
|
---|
99 | actual_size, 16),
|
---|
100 | actual_size);
|
---|
101 | } else {
|
---|
102 | usb_log_debug(
|
---|
103 | "Poll%p: polling failed: %s.\n",
|
---|
104 | data, str_error(rc));
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* If the pipe stalled, we can try to reset the stall. */
|
---|
109 | if ((rc == ESTALL) && (params->auto_clear_halt)) {
|
---|
110 | /*
|
---|
111 | * We ignore error here as this is usually a futile
|
---|
112 | * attempt anyway.
|
---|
113 | */
|
---|
114 | usb_request_clear_endpoint_halt(
|
---|
115 | &data->dev->ctrl_pipe, pipe->endpoint_no);
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (rc != EOK) {
|
---|
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;
|
---|
124 | }
|
---|
125 | continue;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /* We have the data, execute the callback now. */
|
---|
129 | assert(params->on_data);
|
---|
130 | const bool carry_on = params->on_data(
|
---|
131 | data->dev, data->buffer, actual_size, data->custom_arg);
|
---|
132 |
|
---|
133 | if (!carry_on) {
|
---|
134 | /* This is user requested abort, erases failures. */
|
---|
135 | failed_attempts = 0;
|
---|
136 | break;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /* Reset as something might be only a temporary problem. */
|
---|
140 | failed_attempts = 0;
|
---|
141 |
|
---|
142 | /* Take a rest before next request. */
|
---|
143 | async_usleep(params->delay);
|
---|
144 | }
|
---|
145 |
|
---|
146 | usb_pipe_end_long_transfer(pipe);
|
---|
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);
|
---|
152 | }
|
---|
153 |
|
---|
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);
|
---|
158 | } else {
|
---|
159 | usb_log_debug("Polling of device `%s' terminated: "
|
---|
160 | "driver request.\n", data->dev->ddf_dev->name);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /* Free the allocated memory. */
|
---|
165 | free(data->buffer);
|
---|
166 | free(data);
|
---|
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 | {
|
---|
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 | };
|
---|
202 |
|
---|
203 | return usb_device_auto_polling(dev, pipe_index, &auto_polling,
|
---|
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,
|
---|
225 | const usb_device_auto_polling_t *polling,
|
---|
226 | size_t request_size, void *arg)
|
---|
227 | {
|
---|
228 | if ((dev == NULL) || (polling == NULL) || (polling->on_data == NULL)) {
|
---|
229 | return EBADMEM;
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (pipe_index >= dev->pipes_count || request_size == 0) {
|
---|
233 | return EINVAL;
|
---|
234 | }
|
---|
235 | if ((dev->pipes[pipe_index].pipe.transfer_type != USB_TRANSFER_INTERRUPT)
|
---|
236 | || (dev->pipes[pipe_index].pipe.direction != USB_DIRECTION_IN)) {
|
---|
237 | return EINVAL;
|
---|
238 | }
|
---|
239 |
|
---|
240 | polling_data_t *polling_data = malloc(sizeof(polling_data_t));
|
---|
241 | if (polling_data == NULL) {
|
---|
242 | return ENOMEM;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /* Fill-in the data. */
|
---|
246 | polling_data->buffer = malloc(sizeof(request_size));
|
---|
247 | if (polling_data->buffer == NULL) {
|
---|
248 | free(polling_data);
|
---|
249 | return ENOMEM;
|
---|
250 | }
|
---|
251 | polling_data->request_size = request_size;
|
---|
252 | polling_data->dev = dev;
|
---|
253 | polling_data->pipe_index = pipe_index;
|
---|
254 | polling_data->custom_arg = arg;
|
---|
255 |
|
---|
256 | /* Copy provided settings. */
|
---|
257 | polling_data->auto_polling = *polling;
|
---|
258 |
|
---|
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 | }
|
---|
264 |
|
---|
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 |
|
---|
273 | /* Fibril launched. That fibril will free the allocated data. */
|
---|
274 |
|
---|
275 | return EOK;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * @}
|
---|
280 | */
|
---|