source: mainline/uspace/lib/usb/src/hc.c@ 04028225

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

Getting address by handle also moved into libusb

  • Property mode set to 100644
File size: 5.8 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 * General communication with host controller driver (implementation).
34 */
35#include <devman.h>
36#include <async.h>
37#include <dev_iface.h>
38#include <usb_iface.h>
39#include <usbhc_iface.h>
40#include <usb/hc.h>
41#include <usb/driver.h>
42#include <usb/debug.h>
43#include <errno.h>
44#include <assert.h>
45
46/** Initialize connection to USB host controller.
47 *
48 * @param connection Connection to be initialized.
49 * @param device Device connecting to the host controller.
50 * @return Error code.
51 */
52int usb_hc_connection_initialize_from_device(usb_hc_connection_t *connection,
53 ddf_dev_t *device)
54{
55 assert(connection);
56
57 if (device == NULL) {
58 return EBADMEM;
59 }
60
61 devman_handle_t hc_handle;
62 int rc = usb_hc_find(device->handle, &hc_handle);
63 if (rc != EOK) {
64 return rc;
65 }
66
67 rc = usb_hc_connection_initialize(connection, hc_handle);
68
69 return rc;
70}
71
72/** Manually initialize connection to USB host controller.
73 *
74 * @param connection Connection to be initialized.
75 * @param hc_handle Devman handle of the host controller.
76 * @return Error code.
77 */
78int usb_hc_connection_initialize(usb_hc_connection_t *connection,
79 devman_handle_t hc_handle)
80{
81 assert(connection);
82
83 connection->hc_handle = hc_handle;
84 connection->hc_phone = -1;
85
86 return EOK;
87}
88
89/** Open connection to host controller.
90 *
91 * @param connection Connection to the host controller.
92 * @return Error code.
93 */
94int usb_hc_connection_open(usb_hc_connection_t *connection)
95{
96 assert(connection);
97
98 if (usb_hc_connection_is_opened(connection)) {
99 return EBUSY;
100 }
101
102 int phone = devman_device_connect(connection->hc_handle, 0);
103 if (phone < 0) {
104 return phone;
105 }
106
107 connection->hc_phone = phone;
108
109 return EOK;
110}
111
112/** Tells whether connection to host controller is opened.
113 *
114 * @param connection Connection to the host controller.
115 * @return Whether connection is opened.
116 */
117bool usb_hc_connection_is_opened(const usb_hc_connection_t *connection)
118{
119 assert(connection);
120
121 return (connection->hc_phone >= 0);
122}
123
124/** Close connection to the host controller.
125 *
126 * @param connection Connection to the host controller.
127 * @return Error code.
128 */
129int usb_hc_connection_close(usb_hc_connection_t *connection)
130{
131 assert(connection);
132
133 if (!usb_hc_connection_is_opened(connection)) {
134 return ENOENT;
135 }
136
137 int rc = async_hangup(connection->hc_phone);
138 if (rc != EOK) {
139 return rc;
140 }
141
142 connection->hc_phone = -1;
143
144 return EOK;
145}
146
147/** Get handle of USB device with given address.
148 *
149 * @param[in] connection Opened connection to host controller.
150 * @param[in] address Address of device in question.
151 * @param[out] handle Where to write the device handle.
152 * @return Error code.
153 */
154int usb_hc_get_handle_by_address(usb_hc_connection_t *connection,
155 usb_address_t address, devman_handle_t *handle)
156{
157 if (!usb_hc_connection_is_opened(connection)) {
158 return ENOENT;
159 }
160
161 sysarg_t tmp;
162 int rc = async_req_2_1(connection->hc_phone,
163 DEV_IFACE_ID(USBHC_DEV_IFACE),
164 IPC_M_USBHC_GET_HANDLE_BY_ADDRESS,
165 address, &tmp);
166 if ((rc == EOK) && (handle != NULL)) {
167 *handle = tmp;
168 }
169
170 return rc;
171}
172
173/** Tell USB address assigned to device with given handle.
174 *
175 * @param dev_handle Devman handle of the USB device in question.
176 * @return USB address or negative error code.
177 */
178usb_address_t usb_hc_get_address_by_handle(devman_handle_t dev_handle)
179{
180 int parent_phone = devman_parent_device_connect(dev_handle,
181 IPC_FLAG_BLOCKING);
182 if (parent_phone < 0) {
183 return parent_phone;
184 }
185
186 sysarg_t address;
187
188 int rc = async_req_2_1(parent_phone, DEV_IFACE_ID(USB_DEV_IFACE),
189 IPC_M_USB_GET_ADDRESS,
190 dev_handle, &address);
191
192 if (rc != EOK) {
193 return rc;
194 }
195
196 async_hangup(parent_phone);
197
198 return (usb_address_t) address;
199}
200
201
202/** Get host controller handle by its class index.
203 *
204 * @param class_index Class index for the host controller.
205 * @param hc_handle Where to store the HC handle
206 * (can be NULL for existence test only).
207 * @return Error code.
208 */
209int usb_ddf_get_hc_handle_by_class(size_t class_index,
210 devman_handle_t *hc_handle)
211{
212 char *class_index_str;
213 devman_handle_t hc_handle_tmp;
214 int rc;
215
216 rc = asprintf(&class_index_str, "%zu", class_index);
217 if (rc < 0) {
218 return ENOMEM;
219 }
220 rc = devman_device_get_handle_by_class("usbhc", class_index_str,
221 &hc_handle_tmp, 0);
222 free(class_index_str);
223 if (rc != EOK) {
224 return rc;
225 }
226
227 if (hc_handle != NULL) {
228 *hc_handle = hc_handle_tmp;
229 }
230
231 return EOK;
232}
233
234/**
235 * @}
236 */
Note: See TracBrowser for help on using the repository browser.