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

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

Merge mainline changes

  • Property mode set to 100644
File size: 6.5 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 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/** Tell USB address assigned to given device.
45 *
46 * @param phone Phone to parent device.
47 * @param dev Device in question.
48 * @return USB address or error code.
49 */
50static usb_address_t get_my_address(int phone, ddf_dev_t *dev)
51{
52 sysarg_t address;
53
54
55 /*
56 * We are sending special value as a handle - zero - to get
57 * handle of the parent function (that handle was used
58 * when registering our device @p dev.
59 */
60 int rc = async_req_2_1(phone, DEV_IFACE_ID(USB_DEV_IFACE),
61 IPC_M_USB_GET_ADDRESS,
62 0, &address);
63
64 if (rc != EOK) {
65 return rc;
66 }
67
68 return (usb_address_t) address;
69}
70
71/** Tell USB interface assigned to given device.
72 *
73 * @param device Device in question.
74 * @return Interface number (negative code means any).
75 */
76int usb_device_get_assigned_interface(ddf_dev_t *device)
77{
78 int parent_phone = devman_parent_device_connect(device->handle,
79 IPC_FLAG_BLOCKING);
80 if (parent_phone < 0) {
81 return -1;
82 }
83
84 sysarg_t iface_no;
85 int rc = async_req_2_1(parent_phone, DEV_IFACE_ID(USB_DEV_IFACE),
86 IPC_M_USB_GET_INTERFACE,
87 device->handle, &iface_no);
88
89 async_hangup(parent_phone);
90
91 if (rc != EOK) {
92 return -1;
93 }
94
95 return (int) iface_no;
96}
97
98/** Initialize connection to USB device.
99 *
100 * @param connection Connection structure to be initialized.
101 * @param device Generic device backing the USB device.
102 * @return Error code.
103 */
104int usb_device_connection_initialize_from_device(
105 usb_device_connection_t *connection, ddf_dev_t *dev)
106{
107 assert(connection);
108 assert(dev);
109
110 int rc;
111 devman_handle_t hc_handle;
112 usb_address_t my_address;
113
114 rc = usb_hc_find(dev->handle, &hc_handle);
115 if (rc != EOK) {
116 return rc;
117 }
118
119 int parent_phone = devman_parent_device_connect(dev->handle,
120 IPC_FLAG_BLOCKING);
121 if (parent_phone < 0) {
122 return parent_phone;
123 }
124
125 my_address = get_my_address(parent_phone, dev);
126 if (my_address < 0) {
127 rc = my_address;
128 goto leave;
129 }
130
131 rc = usb_device_connection_initialize(connection,
132 hc_handle, my_address);
133
134leave:
135 async_hangup(parent_phone);
136 return rc;
137}
138
139/** Initialize connection to USB device.
140 *
141 * @param connection Connection structure to be initialized.
142 * @param host_controller_handle Devman handle of host controller device is
143 * connected to.
144 * @param device_address Device USB address.
145 * @return Error code.
146 */
147int usb_device_connection_initialize(usb_device_connection_t *connection,
148 devman_handle_t host_controller_handle, usb_address_t device_address)
149{
150 assert(connection);
151
152 if ((device_address < 0) || (device_address >= USB11_ADDRESS_MAX)) {
153 return EINVAL;
154 }
155
156 connection->hc_handle = host_controller_handle;
157 connection->address = device_address;
158
159 return EOK;
160}
161
162/** Initialize connection to USB device on default address.
163 *
164 * @param dev_connection Device connection structure to be initialized.
165 * @param hc_connection Initialized connection to host controller.
166 * @return Error code.
167 */
168int usb_device_connection_initialize_on_default_address(
169 usb_device_connection_t *dev_connection,
170 usb_hc_connection_t *hc_connection)
171{
172 assert(dev_connection);
173
174 if (hc_connection == NULL) {
175 return EBADMEM;
176 }
177
178 return usb_device_connection_initialize(dev_connection,
179 hc_connection->hc_handle, (usb_address_t) 0);
180}
181
182
183/** Start a session on the endpoint pipe.
184 *
185 * A session is something inside what any communication occurs.
186 * It is expected that sessions would be started right before the transfer
187 * and ended - see usb_endpoint_pipe_end_session() - after the last
188 * transfer.
189 * The reason for this is that session actually opens some communication
190 * channel to the host controller (or to the physical hardware if you
191 * wish) and thus it involves acquiring kernel resources.
192 * Since they are limited, sessions shall not be longer than strictly
193 * necessary.
194 *
195 * @param pipe Endpoint pipe to start the session on.
196 * @return Error code.
197 */
198int usb_endpoint_pipe_start_session(usb_endpoint_pipe_t *pipe)
199{
200 assert(pipe);
201
202 if (usb_endpoint_pipe_is_session_started(pipe)) {
203 return EBUSY;
204 }
205
206 int phone = devman_device_connect(pipe->wire->hc_handle, 0);
207 if (phone < 0) {
208 return phone;
209 }
210
211 pipe->hc_phone = phone;
212
213 return EOK;
214}
215
216
217/** Ends a session on the endpoint pipe.
218 *
219 * @see usb_endpoint_pipe_start_session
220 *
221 * @param pipe Endpoint pipe to end the session on.
222 * @return Error code.
223 */
224int usb_endpoint_pipe_end_session(usb_endpoint_pipe_t *pipe)
225{
226 assert(pipe);
227
228 if (!usb_endpoint_pipe_is_session_started(pipe)) {
229 return ENOENT;
230 }
231
232 int rc = async_hangup(pipe->hc_phone);
233 if (rc != EOK) {
234 return rc;
235 }
236
237 pipe->hc_phone = -1;
238
239 return EOK;
240}
241
242/** Tell whether a session is started (open) on the endpoint pipe.
243 *
244 * The expected usage of this function is in assertions for some
245 * nested functions.
246 *
247 * @param pipe Endpoint pipe in question.
248 * @return Whether @p pipe has opened a session.
249 */
250bool usb_endpoint_pipe_is_session_started(usb_endpoint_pipe_t *pipe)
251{
252 return (pipe->hc_phone >= 0);
253}
254
255/**
256 * @}
257 */
Note: See TracBrowser for help on using the repository browser.