source: mainline/uspace/lib/usb/src/pipes.c@ c4e3b1f7

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

Add functions for explicit long transfers

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