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. */
|
---|
46 | typedef 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 | */
|
---|
61 | static 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 | rc = usb_pipe_start_session(pipe);
|
---|
80 | if (rc != EOK) {
|
---|
81 | failed_attempts++;
|
---|
82 | continue;
|
---|
83 | }
|
---|
84 |
|
---|
85 | size_t actual_size;
|
---|
86 | rc = usb_pipe_read(pipe, polling_data->buffer,
|
---|
87 | polling_data->request_size, &actual_size);
|
---|
88 |
|
---|
89 | /* Quit the session regardless of errors. */
|
---|
90 | usb_pipe_end_session(pipe);
|
---|
91 |
|
---|
92 | // if (rc == ESTALL) {
|
---|
93 | // usb_log_debug("Seding clear feature...\n");
|
---|
94 | // usb_request_clear_feature(pipe, USB_REQUEST_TYPE_STANDARD,
|
---|
95 | // USB_REQUEST_RECIPIENT_ENDPOINT, 0, pipe->endpoint_no);
|
---|
96 | // continue;
|
---|
97 | // }
|
---|
98 |
|
---|
99 | if (rc != EOK) {
|
---|
100 | failed_attempts++;
|
---|
101 | continue;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /* We have the data, execute the callback now. */
|
---|
105 | bool carry_on = polling_data->callback(polling_data->dev,
|
---|
106 | polling_data->buffer, actual_size,
|
---|
107 | polling_data->custom_arg);
|
---|
108 |
|
---|
109 | if (!carry_on) {
|
---|
110 | failed_attempts = 0;
|
---|
111 | break;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* Reset as something might be only a temporary problem. */
|
---|
115 | failed_attempts = 0;
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (failed_attempts > 0) {
|
---|
119 | usb_log_error(
|
---|
120 | "Polling of device `%s' terminated: recurring failures.\n",
|
---|
121 | polling_data->dev->ddf_dev->name);
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (polling_data->terminated_callback != NULL) {
|
---|
125 | polling_data->terminated_callback(polling_data->dev,
|
---|
126 | failed_attempts > 0, polling_data->custom_arg);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /* Free the allocated memory. */
|
---|
130 | free(polling_data->buffer);
|
---|
131 | free(polling_data);
|
---|
132 |
|
---|
133 | return EOK;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /** Start automatic device polling over interrupt in pipe.
|
---|
137 | *
|
---|
138 | * @warning It is up to the callback to produce delays between individual
|
---|
139 | * requests.
|
---|
140 | *
|
---|
141 | * @warning There is no guarantee when the request to the device
|
---|
142 | * will be sent for the first time (it is possible that this
|
---|
143 | * first request would be executed prior to return from this function).
|
---|
144 | *
|
---|
145 | * @param dev Device to be periodically polled.
|
---|
146 | * @param pipe_index Index of the endpoint pipe used for polling.
|
---|
147 | * @param callback Callback when data are available.
|
---|
148 | * @param request_size How many bytes to ask for in each request.
|
---|
149 | * @param terminated_callback Callback when polling is terminated.
|
---|
150 | * @param arg Custom argument (passed as is to the callbacks).
|
---|
151 | * @return Error code.
|
---|
152 | * @retval EOK New fibril polling the device was already started.
|
---|
153 | */
|
---|
154 | int usb_device_auto_poll(usb_device_t *dev, size_t pipe_index,
|
---|
155 | usb_polling_callback_t callback, size_t request_size,
|
---|
156 | usb_polling_terminted_callback_t terminated_callback, void *arg)
|
---|
157 | {
|
---|
158 | if ((dev == NULL) || (callback == NULL)) {
|
---|
159 | return EBADMEM;
|
---|
160 | }
|
---|
161 | if (request_size == 0) {
|
---|
162 | return EINVAL;
|
---|
163 | }
|
---|
164 | if ((dev->pipes[pipe_index].pipe->transfer_type != USB_TRANSFER_INTERRUPT)
|
---|
165 | || (dev->pipes[pipe_index].pipe->direction != USB_DIRECTION_IN)) {
|
---|
166 | return EINVAL;
|
---|
167 | }
|
---|
168 |
|
---|
169 | polling_data_t *polling_data = malloc(sizeof(polling_data_t));
|
---|
170 | if (polling_data == NULL) {
|
---|
171 | return ENOMEM;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /* Allocate now to prevent immediate failure in the polling fibril. */
|
---|
175 | polling_data->buffer = malloc(request_size);
|
---|
176 | if (polling_data->buffer == NULL) {
|
---|
177 | free(polling_data);
|
---|
178 | return ENOMEM;
|
---|
179 | }
|
---|
180 | polling_data->dev = dev;
|
---|
181 | polling_data->pipe_index = pipe_index;
|
---|
182 | polling_data->callback = callback;
|
---|
183 | polling_data->terminated_callback = terminated_callback;
|
---|
184 | polling_data->request_size = request_size;
|
---|
185 | polling_data->custom_arg = arg;
|
---|
186 |
|
---|
187 | fid_t fibril = fibril_create(polling_fibril, polling_data);
|
---|
188 | if (fibril == 0) {
|
---|
189 | free(polling_data->buffer);
|
---|
190 | free(polling_data);
|
---|
191 | /* FIXME: better error code. */
|
---|
192 | return ENOMEM;
|
---|
193 | }
|
---|
194 | fibril_add_ready(fibril);
|
---|
195 |
|
---|
196 | /* The allocated buffer etc. will be freed by the fibril. */
|
---|
197 |
|
---|
198 | return EOK;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * @}
|
---|
203 | */
|
---|