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