source: mainline/uspace/lib/usbdev/src/pipes.c@ ab27e01

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ab27e01 was 7fc260ff, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

libusbdev: Merge init_wire_and_ctrl_pipe to the only place it was called.

Add usb_device_release_descriptors function that properly clears the descriptor
structure.

  • Property mode set to 100644
File size: 6.4 KB
Line 
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 endpoint pipes miscellaneous functions.
34 */
35#include <usb/usb.h>
36#include <usb/dev/pipes.h>
37#include <usb/debug.h>
38#include <usb/hc.h>
39#include <usbhc_iface.h>
40#include <usb_iface.h>
41#include <devman.h>
42#include <errno.h>
43#include <assert.h>
44#include "pipepriv.h"
45
46#define IPC_AGAIN_DELAY (1000 * 2) /* 2ms */
47
48/** Tell USB address assigned to given device.
49 *
50 * @param sess Session to parent device.
51 * @param dev Device in question.
52 * @return USB address or error code.
53 */
54static usb_address_t get_my_address(async_sess_t *sess, const ddf_dev_t *dev)
55{
56 async_exch_t *exch = async_exchange_begin(sess);
57
58 sysarg_t address;
59 int rc = async_req_1_1(exch, DEV_IFACE_ID(USB_DEV_IFACE),
60 IPC_M_USB_GET_MY_ADDRESS, &address);
61
62 async_exchange_end(exch);
63
64 if (rc != EOK)
65 return rc;
66
67 return (usb_address_t) address;
68}
69
70/** Tell USB interface assigned to given device.
71 *
72 * @param device Device in question.
73 * @return Interface number (negative code means any).
74 */
75int usb_device_get_assigned_interface(const ddf_dev_t *device)
76{
77 assert(device);
78 async_sess_t *parent_sess =
79 devman_parent_device_connect(EXCHANGE_ATOMIC, device->handle,
80 IPC_FLAG_BLOCKING);
81 if (!parent_sess)
82 return -1;
83
84 async_exch_t *exch = async_exchange_begin(parent_sess);
85
86 sysarg_t iface_no;
87 int rc = async_req_2_1(exch, DEV_IFACE_ID(USB_DEV_IFACE),
88 IPC_M_USB_GET_INTERFACE, device->handle, &iface_no);
89
90 async_exchange_end(exch);
91 async_hangup(parent_sess);
92
93 if (rc != EOK)
94 return -1;
95
96 return (int) iface_no;
97}
98
99/** Initialize connection to USB device.
100 *
101 * @param connection Connection structure to be initialized.
102 * @param dev Generic device backing the USB device.
103 * @return Error code.
104 */
105int usb_device_connection_initialize_from_device(
106 usb_device_connection_t *connection, const ddf_dev_t *dev)
107{
108 assert(connection);
109 assert(dev);
110
111 int rc;
112 devman_handle_t hc_handle;
113 usb_address_t my_address;
114
115 rc = usb_hc_find(dev->handle, &hc_handle);
116 if (rc != EOK)
117 return rc;
118
119 async_sess_t *parent_sess =
120 devman_parent_device_connect(EXCHANGE_ATOMIC, dev->handle,
121 IPC_FLAG_BLOCKING);
122 if (!parent_sess)
123 return ENOMEM;
124
125 /*
126 * Asking for "my" address may require several attempts.
127 * That is because following scenario may happen:
128 * - parent driver (i.e. driver of parent device) announces new device
129 * and devman launches current driver
130 * - parent driver is preempted and thus does not send address-handle
131 * binding to HC driver
132 * - this driver gets here and wants the binding
133 * - the HC does not know the binding yet and thus it answers ENOENT
134 * So, we need to wait for the HC to learn the binding.
135 */
136
137 do {
138 my_address = get_my_address(parent_sess, dev);
139
140 if (my_address == ENOENT) {
141 /* Be nice, let other fibrils run and try again. */
142 async_usleep(IPC_AGAIN_DELAY);
143 } else if (my_address < 0) {
144 /* Some other problem, no sense trying again. */
145 rc = my_address;
146 goto leave;
147 }
148
149 } while (my_address < 0);
150
151 rc = usb_device_connection_initialize(connection,
152 hc_handle, my_address);
153
154leave:
155 async_hangup(parent_sess);
156 return rc;
157}
158
159/** Initialize connection to USB device.
160 *
161 * @param connection Connection structure to be initialized.
162 * @param host_controller_handle Devman handle of host controller device is
163 * connected to.
164 * @param device_address Device USB address.
165 * @return Error code.
166 */
167int usb_device_connection_initialize(usb_device_connection_t *connection,
168 devman_handle_t host_controller_handle, usb_address_t device_address)
169{
170 assert(connection);
171
172 if ((device_address < 0) || (device_address >= USB11_ADDRESS_MAX)) {
173 return EINVAL;
174 }
175
176 connection->hc_handle = host_controller_handle;
177 connection->address = device_address;
178
179 return EOK;
180}
181
182/** Initialize connection to USB device on default address.
183 *
184 * @param dev_connection Device connection structure to be initialized.
185 * @param hc_connection Initialized connection to host controller.
186 * @return Error code.
187 */
188int usb_device_connection_initialize_on_default_address(
189 usb_device_connection_t *dev_connection,
190 usb_hc_connection_t *hc_connection)
191{
192 assert(dev_connection);
193
194 if (hc_connection == NULL) {
195 return EBADMEM;
196 }
197
198 return usb_device_connection_initialize(dev_connection,
199 hc_connection->hc_handle, (usb_address_t) 0);
200}
201
202/** Prepare pipe for a long transfer.
203 *
204 * By a long transfer is mean transfer consisting of several
205 * requests to the HC.
206 * Calling such function is optional and it has positive effect of
207 * improved performance because IPC session is initiated only once.
208 *
209 * @param pipe Pipe over which the transfer will happen.
210 * @return Error code.
211 */
212void usb_pipe_start_long_transfer(usb_pipe_t *pipe)
213{
214 (void) pipe_add_ref(pipe, true);
215}
216
217/** Terminate a long transfer on a pipe.
218 *
219 * @see usb_pipe_start_long_transfer
220 *
221 * @param pipe Pipe where to end the long transfer.
222 */
223void usb_pipe_end_long_transfer(usb_pipe_t *pipe)
224{
225 pipe_drop_ref(pipe);
226}
227
228/**
229 * @}
230 */
Note: See TracBrowser for help on using the repository browser.