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 | */
|
---|
52 | int 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 | */
|
---|
78 | int 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 | */
|
---|
94 | int 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 | */
|
---|
117 | bool 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 | */
|
---|
129 | int 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 | */
|
---|
154 | int 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 |
|
---|
174 | /** Get host controller handle by its class index.
|
---|
175 | *
|
---|
176 | * @param class_index Class index for the host controller.
|
---|
177 | * @param hc_handle Where to store the HC handle
|
---|
178 | * (can be NULL for existence test only).
|
---|
179 | * @return Error code.
|
---|
180 | */
|
---|
181 | int usb_ddf_get_hc_handle_by_class(size_t class_index,
|
---|
182 | devman_handle_t *hc_handle)
|
---|
183 | {
|
---|
184 | char *class_index_str;
|
---|
185 | devman_handle_t hc_handle_tmp;
|
---|
186 | int rc;
|
---|
187 |
|
---|
188 | rc = asprintf(&class_index_str, "%zu", class_index);
|
---|
189 | if (rc < 0) {
|
---|
190 | return ENOMEM;
|
---|
191 | }
|
---|
192 | rc = devman_device_get_handle_by_class("usbhc", class_index_str,
|
---|
193 | &hc_handle_tmp, 0);
|
---|
194 | free(class_index_str);
|
---|
195 | if (rc != EOK) {
|
---|
196 | return rc;
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (hc_handle != NULL) {
|
---|
200 | *hc_handle = hc_handle_tmp;
|
---|
201 | }
|
---|
202 |
|
---|
203 | return EOK;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * @}
|
---|
208 | */
|
---|