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

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

Add special init for device on default address

  • Property mode set to 100644
File size: 4.9 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 <errno.h>
38#include <assert.h>
39#include <usb/usbdrv.h>
40
41/** Initialize connection to USB device.
42 *
43 * @param connection Connection structure to be initialized.
44 * @param device Generic device backing the USB device.
45 * @return Error code.
46 */
47int usb_device_connection_initialize_from_device(
48 usb_device_connection_t *connection, device_t *device)
49{
50 assert(connection);
51 assert(device);
52
53 int rc;
54 devman_handle_t hc_handle;
55 usb_address_t my_address;
56
57 rc = usb_drv_find_hc(device, &hc_handle);
58 if (rc != EOK) {
59 return rc;
60 }
61
62 int hc_phone = devman_device_connect(hc_handle, 0);
63 if (hc_phone < 0) {
64 return hc_phone;
65 }
66
67 my_address = usb_drv_get_my_address(hc_phone, device);
68 if (my_address < 0) {
69 rc = my_address;
70 goto leave;
71 }
72
73 rc = usb_device_connection_initialize(connection,
74 hc_handle, my_address);
75
76leave:
77 async_hangup(hc_phone);
78 return rc;
79}
80
81/** Initialize connection to USB device.
82 *
83 * @param connection Connection structure to be initialized.
84 * @param host_controller_handle Devman handle of host controller device is
85 * connected to.
86 * @param device_address Device USB address.
87 * @return Error code.
88 */
89int usb_device_connection_initialize(usb_device_connection_t *connection,
90 devman_handle_t host_controller_handle, usb_address_t device_address)
91{
92 assert(connection);
93
94 if ((device_address < 0) || (device_address >= USB11_ADDRESS_MAX)) {
95 return EINVAL;
96 }
97
98 connection->hc_handle = host_controller_handle;
99 connection->address = device_address;
100
101 return EOK;
102}
103
104/** Initialize connection to USB device on default address.
105 *
106 * @param dev_connection Device connection structure to be initialized.
107 * @param hc_connection Initialized connection to host controller.
108 * @return Error code.
109 */
110int usb_device_connection_initialize_on_default_address(
111 usb_device_connection_t *dev_connection,
112 usb_hc_connection_t *hc_connection)
113{
114 assert(dev_connection);
115
116 if (hc_connection == NULL) {
117 return EBADMEM;
118 }
119
120 return usb_device_connection_initialize(dev_connection,
121 hc_connection->hc_handle, (usb_address_t) 0);
122}
123
124
125/** Start a session on the endpoint pipe.
126 *
127 * A session is something inside what any communication occurs.
128 * It is expected that sessions would be started right before the transfer
129 * and ended - see usb_endpoint_pipe_end_session() - after the last
130 * transfer.
131 * The reason for this is that session actually opens some communication
132 * channel to the host controller (or to the physical hardware if you
133 * wish) and thus it involves acquiring kernel resources.
134 * Since they are limited, sessions shall not be longer than strictly
135 * necessary.
136 *
137 * @param pipe Endpoint pipe to start the session on.
138 * @return Error code.
139 */
140int usb_endpoint_pipe_start_session(usb_endpoint_pipe_t *pipe)
141{
142 assert(pipe);
143
144 if (pipe->hc_phone >= 0) {
145 return EBUSY;
146 }
147
148 int phone = devman_device_connect(pipe->wire->hc_handle, 0);
149 if (phone < 0) {
150 return phone;
151 }
152
153 pipe->hc_phone = phone;
154
155 return EOK;
156}
157
158
159/** Ends a session on the endpoint pipe.
160 *
161 * @see usb_endpoint_pipe_start_session
162 *
163 * @param pipe Endpoint pipe to end the session on.
164 * @return Error code.
165 */
166int usb_endpoint_pipe_end_session(usb_endpoint_pipe_t *pipe)
167{
168 assert(pipe);
169
170 if (pipe->hc_phone < 0) {
171 return ENOENT;
172 }
173
174 int rc = async_hangup(pipe->hc_phone);
175 if (rc != EOK) {
176 return rc;
177 }
178
179 pipe->hc_phone = -1;
180
181 return EOK;
182}
183
184/**
185 * @}
186 */
Note: See TracBrowser for help on using the repository browser.