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 endpoint pipes miscellaneous functions.
|
---|
34 | */
|
---|
35 | #include <usb/usb.h>
|
---|
36 | #include <usb/pipes.h>
|
---|
37 | #include <usb/debug.h>
|
---|
38 | #include <usbhc_iface.h>
|
---|
39 | #include <usb_iface.h>
|
---|
40 | #include <devman.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <assert.h>
|
---|
43 |
|
---|
44 | #define IPC_AGAIN_DELAY (1000 * 2) /* 2ms */
|
---|
45 |
|
---|
46 | /** Tell USB address assigned to given device.
|
---|
47 | *
|
---|
48 | * @param phone Phone to parent device.
|
---|
49 | * @param dev Device in question.
|
---|
50 | * @return USB address or error code.
|
---|
51 | */
|
---|
52 | static usb_address_t get_my_address(int phone, ddf_dev_t *dev)
|
---|
53 | {
|
---|
54 | sysarg_t address;
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * We are sending special value as a handle - zero - to get
|
---|
58 | * handle of the parent function (that handle was used
|
---|
59 | * when registering our device @p dev.
|
---|
60 | */
|
---|
61 | int rc = async_req_2_1(phone, DEV_IFACE_ID(USB_DEV_IFACE),
|
---|
62 | IPC_M_USB_GET_ADDRESS,
|
---|
63 | 0, &address);
|
---|
64 |
|
---|
65 | if (rc != EOK) {
|
---|
66 | return rc;
|
---|
67 | }
|
---|
68 |
|
---|
69 | return (usb_address_t) address;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Tell USB interface assigned to given device.
|
---|
73 | *
|
---|
74 | * @param device Device in question.
|
---|
75 | * @return Interface number (negative code means any).
|
---|
76 | */
|
---|
77 | int usb_device_get_assigned_interface(ddf_dev_t *device)
|
---|
78 | {
|
---|
79 | int parent_phone = devman_parent_device_connect(device->handle,
|
---|
80 | IPC_FLAG_BLOCKING);
|
---|
81 | if (parent_phone < 0) {
|
---|
82 | return -1;
|
---|
83 | }
|
---|
84 |
|
---|
85 | sysarg_t iface_no;
|
---|
86 | int rc = async_req_2_1(parent_phone, DEV_IFACE_ID(USB_DEV_IFACE),
|
---|
87 | IPC_M_USB_GET_INTERFACE,
|
---|
88 | device->handle, &iface_no);
|
---|
89 |
|
---|
90 | async_hangup(parent_phone);
|
---|
91 |
|
---|
92 | if (rc != EOK) {
|
---|
93 | return -1;
|
---|
94 | }
|
---|
95 |
|
---|
96 | return (int) iface_no;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Tell USB address assigned to given device.
|
---|
100 | *
|
---|
101 | * @param dev_handle Devman handle of the USB device in question.
|
---|
102 | * @return USB address or negative error code.
|
---|
103 | */
|
---|
104 | usb_address_t usb_device_get_assigned_address(devman_handle_t dev_handle)
|
---|
105 | {
|
---|
106 | int parent_phone = devman_parent_device_connect(dev_handle,
|
---|
107 | IPC_FLAG_BLOCKING);
|
---|
108 | if (parent_phone < 0) {
|
---|
109 | return parent_phone;
|
---|
110 | }
|
---|
111 |
|
---|
112 | sysarg_t address;
|
---|
113 |
|
---|
114 | int rc = async_req_2_1(parent_phone, DEV_IFACE_ID(USB_DEV_IFACE),
|
---|
115 | IPC_M_USB_GET_ADDRESS,
|
---|
116 | dev_handle, &address);
|
---|
117 |
|
---|
118 | if (rc != EOK) {
|
---|
119 | return rc;
|
---|
120 | }
|
---|
121 |
|
---|
122 | async_hangup(parent_phone);
|
---|
123 |
|
---|
124 | return (usb_address_t) address;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** Initialize connection to USB device.
|
---|
128 | *
|
---|
129 | * @param connection Connection structure to be initialized.
|
---|
130 | * @param dev Generic device backing the USB device.
|
---|
131 | * @return Error code.
|
---|
132 | */
|
---|
133 | int usb_device_connection_initialize_from_device(
|
---|
134 | usb_device_connection_t *connection, ddf_dev_t *dev)
|
---|
135 | {
|
---|
136 | assert(connection);
|
---|
137 | assert(dev);
|
---|
138 |
|
---|
139 | int rc;
|
---|
140 | devman_handle_t hc_handle;
|
---|
141 | usb_address_t my_address;
|
---|
142 |
|
---|
143 | rc = usb_hc_find(dev->handle, &hc_handle);
|
---|
144 | if (rc != EOK) {
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 |
|
---|
148 | int parent_phone = devman_parent_device_connect(dev->handle,
|
---|
149 | IPC_FLAG_BLOCKING);
|
---|
150 | if (parent_phone < 0) {
|
---|
151 | return parent_phone;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * Asking for "my" address may require several attempts.
|
---|
156 | * That is because following scenario may happen:
|
---|
157 | * - parent driver (i.e. driver of parent device) announces new device
|
---|
158 | * and devman launches current driver
|
---|
159 | * - parent driver is preempted and thus does not send address-handle
|
---|
160 | * binding to HC driver
|
---|
161 | * - this driver gets here and wants the binding
|
---|
162 | * - the HC does not know the binding yet and thus it answers ENOENT
|
---|
163 | * So, we need to wait for the HC to learn the binding.
|
---|
164 | */
|
---|
165 | do {
|
---|
166 | my_address = get_my_address(parent_phone, dev);
|
---|
167 |
|
---|
168 | if (my_address == ENOENT) {
|
---|
169 | /* Be nice, let other fibrils run and try again. */
|
---|
170 | async_usleep(IPC_AGAIN_DELAY);
|
---|
171 | } else if (my_address < 0) {
|
---|
172 | /* Some other problem, no sense trying again. */
|
---|
173 | rc = my_address;
|
---|
174 | goto leave;
|
---|
175 | }
|
---|
176 |
|
---|
177 | } while (my_address < 0);
|
---|
178 |
|
---|
179 | rc = usb_device_connection_initialize(connection,
|
---|
180 | hc_handle, my_address);
|
---|
181 |
|
---|
182 | leave:
|
---|
183 | async_hangup(parent_phone);
|
---|
184 | return rc;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** Initialize connection to USB device.
|
---|
188 | *
|
---|
189 | * @param connection Connection structure to be initialized.
|
---|
190 | * @param host_controller_handle Devman handle of host controller device is
|
---|
191 | * connected to.
|
---|
192 | * @param device_address Device USB address.
|
---|
193 | * @return Error code.
|
---|
194 | */
|
---|
195 | int usb_device_connection_initialize(usb_device_connection_t *connection,
|
---|
196 | devman_handle_t host_controller_handle, usb_address_t device_address)
|
---|
197 | {
|
---|
198 | assert(connection);
|
---|
199 |
|
---|
200 | if ((device_address < 0) || (device_address >= USB11_ADDRESS_MAX)) {
|
---|
201 | return EINVAL;
|
---|
202 | }
|
---|
203 |
|
---|
204 | connection->hc_handle = host_controller_handle;
|
---|
205 | connection->address = device_address;
|
---|
206 |
|
---|
207 | return EOK;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /** Initialize connection to USB device on default address.
|
---|
211 | *
|
---|
212 | * @param dev_connection Device connection structure to be initialized.
|
---|
213 | * @param hc_connection Initialized connection to host controller.
|
---|
214 | * @return Error code.
|
---|
215 | */
|
---|
216 | int usb_device_connection_initialize_on_default_address(
|
---|
217 | usb_device_connection_t *dev_connection,
|
---|
218 | usb_hc_connection_t *hc_connection)
|
---|
219 | {
|
---|
220 | assert(dev_connection);
|
---|
221 |
|
---|
222 | if (hc_connection == NULL) {
|
---|
223 | return EBADMEM;
|
---|
224 | }
|
---|
225 |
|
---|
226 | return usb_device_connection_initialize(dev_connection,
|
---|
227 | hc_connection->hc_handle, (usb_address_t) 0);
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /** Start a session on the endpoint pipe.
|
---|
232 | *
|
---|
233 | * A session is something inside what any communication occurs.
|
---|
234 | * It is expected that sessions would be started right before the transfer
|
---|
235 | * and ended - see usb_pipe_end_session() - after the last
|
---|
236 | * transfer.
|
---|
237 | * The reason for this is that session actually opens some communication
|
---|
238 | * channel to the host controller (or to the physical hardware if you
|
---|
239 | * wish) and thus it involves acquiring kernel resources.
|
---|
240 | * Since they are limited, sessions shall not be longer than strictly
|
---|
241 | * necessary.
|
---|
242 | *
|
---|
243 | * @param pipe Endpoint pipe to start the session on.
|
---|
244 | * @return Error code.
|
---|
245 | */
|
---|
246 | int usb_pipe_start_session(usb_pipe_t *pipe)
|
---|
247 | {
|
---|
248 | assert(pipe);
|
---|
249 |
|
---|
250 | if (usb_pipe_is_session_started(pipe)) {
|
---|
251 | return EBUSY;
|
---|
252 | }
|
---|
253 |
|
---|
254 | int phone = devman_device_connect(pipe->wire->hc_handle, 0);
|
---|
255 | if (phone < 0) {
|
---|
256 | return phone;
|
---|
257 | }
|
---|
258 |
|
---|
259 | pipe->hc_phone = phone;
|
---|
260 |
|
---|
261 | return EOK;
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | /** Ends a session on the endpoint pipe.
|
---|
266 | *
|
---|
267 | * @see usb_pipe_start_session
|
---|
268 | *
|
---|
269 | * @param pipe Endpoint pipe to end the session on.
|
---|
270 | * @return Error code.
|
---|
271 | */
|
---|
272 | int usb_pipe_end_session(usb_pipe_t *pipe)
|
---|
273 | {
|
---|
274 | assert(pipe);
|
---|
275 |
|
---|
276 | if (!usb_pipe_is_session_started(pipe)) {
|
---|
277 | return ENOENT;
|
---|
278 | }
|
---|
279 |
|
---|
280 | int rc = async_hangup(pipe->hc_phone);
|
---|
281 | if (rc != EOK) {
|
---|
282 | return rc;
|
---|
283 | }
|
---|
284 |
|
---|
285 | pipe->hc_phone = -1;
|
---|
286 |
|
---|
287 | return EOK;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /** Tell whether a session is started (open) on the endpoint pipe.
|
---|
291 | *
|
---|
292 | * The expected usage of this function is in assertions for some
|
---|
293 | * nested functions.
|
---|
294 | *
|
---|
295 | * @param pipe Endpoint pipe in question.
|
---|
296 | * @return Whether @p pipe has opened a session.
|
---|
297 | */
|
---|
298 | bool usb_pipe_is_session_started(usb_pipe_t *pipe)
|
---|
299 | {
|
---|
300 | return (pipe->hc_phone >= 0);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * @}
|
---|
305 | */
|
---|