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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b272c67a was f9b2cb4c, checked in by Martin Decky <martin@…>, 10 years ago

unify interface API

  • introduce new interfaces
  • unify location service clients to always expect service ID as the second argument
  • remove obsolete methods that take explicit exchange management arguments (first phase)
  • use interfaces in device drivers, devman, location service, logger, inet
  • Property mode set to 100644
File size: 7.8 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * Copyright (c) 2011 Jan Vesely
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libusb
31 * @{
32 */
33/** @file
34 * General communication with host controller driver (implementation).
35 */
36
37#include <usb/debug.h>
38
39#include <assert.h>
40#include <errno.h>
41#include <usbhc_iface.h>
42#include <usb/dev.h>
43#include <usb/hc.h>
44
45static int usb_hc_connection_add_ref(usb_hc_connection_t *connection)
46{
47 assert(connection);
48
49 fibril_mutex_lock(&connection->guard);
50 if (connection->ref_count == 0) {
51 assert(connection->hc_sess == NULL);
52 /* Parallel exchange for us */
53 connection->hc_sess = devman_device_connect(connection->hc_handle, 0);
54 if (!connection->hc_sess) {
55 fibril_mutex_unlock(&connection->guard);
56 return ENOMEM;
57 }
58 }
59
60 ++connection->ref_count;
61 fibril_mutex_unlock(&connection->guard);
62 return EOK;
63}
64
65static int usb_hc_connection_del_ref(usb_hc_connection_t *connection)
66{
67 assert(connection);
68
69 fibril_mutex_lock(&connection->guard);
70 if (connection->ref_count == 0) {
71 /* Closing already closed connection... */
72 assert(connection->hc_sess == NULL);
73 fibril_mutex_unlock(&connection->guard);
74 return EOK;
75 }
76
77 --connection->ref_count;
78 int ret = EOK;
79 if (connection->ref_count == 0) {
80 assert(connection->hc_sess);
81 ret = async_hangup(connection->hc_sess);
82 connection->hc_sess = NULL;
83 }
84 fibril_mutex_unlock(&connection->guard);
85 return ret;
86}
87
88#define EXCH_INIT(connection, exch) \
89do { \
90 exch = NULL; \
91 if (!connection) \
92 return EBADMEM; \
93 const int ret = usb_hc_connection_add_ref(connection); \
94 if (ret != EOK) \
95 return ret; \
96 exch = async_exchange_begin(connection->hc_sess); \
97 if (exch == NULL) { \
98 usb_hc_connection_del_ref(connection); \
99 return ENOMEM; \
100 } \
101} while (0)
102
103#define EXCH_FINI(connection, exch) \
104if (exch) { \
105 async_exchange_end(exch); \
106 usb_hc_connection_del_ref(connection); \
107} else (void)0
108
109/** Initialize connection to USB host controller.
110 *
111 * @param connection Connection to be initialized.
112 * @param device Device connecting to the host controller.
113 * @return Error code.
114 */
115int usb_hc_connection_initialize_from_device(usb_hc_connection_t *connection,
116 ddf_dev_t *device)
117{
118 if (device == NULL)
119 return EBADMEM;
120
121 devman_handle_t hc_handle;
122 const int rc = usb_get_hc_by_handle(ddf_dev_get_handle(device), &hc_handle);
123 if (rc == EOK) {
124 usb_hc_connection_initialize(connection, hc_handle);
125 }
126
127 return rc;
128}
129
130void usb_hc_connection_deinitialize(usb_hc_connection_t *connection)
131{
132 assert(connection);
133 fibril_mutex_lock(&connection->guard);
134 if (connection->ref_count != 0) {
135 usb_log_warning("%u stale reference(s) to HC connection.\n",
136 connection->ref_count);
137 assert(connection->hc_sess);
138 async_hangup(connection->hc_sess);
139 connection->hc_sess = NULL;
140 connection->ref_count = 0;
141 }
142 fibril_mutex_unlock(&connection->guard);
143}
144
145/** Open connection to host controller.
146 *
147 * @param connection Connection to the host controller.
148 * @return Error code.
149 */
150int usb_hc_connection_open(usb_hc_connection_t *connection)
151{
152 return usb_hc_connection_add_ref(connection);
153}
154
155/** Close connection to the host controller.
156 *
157 * @param connection Connection to the host controller.
158 * @return Error code.
159 */
160int usb_hc_connection_close(usb_hc_connection_t *connection)
161{
162 return usb_hc_connection_del_ref(connection);
163}
164
165/** Ask host controller for free address assignment.
166 *
167 * @param connection Opened connection to host controller.
168 * @param preferred Preferred SUB address.
169 * @param strict Fail if the preferred address is not avialable.
170 * @param speed Speed of the new device (device that will be assigned
171 * the returned address).
172 * @return Assigned USB address or negative error code.
173 */
174usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
175 usb_address_t preferred, bool strict, usb_speed_t speed)
176{
177 async_exch_t *exch;
178 EXCH_INIT(connection, exch);
179
180 usb_address_t address = preferred;
181 const int ret = usbhc_request_address(exch, &address, strict, speed);
182
183 EXCH_FINI(connection, exch);
184 return ret == EOK ? address : ret;
185}
186
187int usb_hc_bind_address(usb_hc_connection_t * connection,
188 usb_address_t address, devman_handle_t handle)
189{
190 async_exch_t *exch;
191 EXCH_INIT(connection, exch);
192
193 const int ret = usbhc_bind_address(exch, address, handle);
194
195 EXCH_FINI(connection, exch);
196 return ret;
197}
198
199/** Get handle of USB device with given address.
200 *
201 * @param[in] connection Opened connection to host controller.
202 * @param[in] address Address of device in question.
203 * @param[out] handle Where to write the device handle.
204 * @return Error code.
205 */
206int usb_hc_get_handle_by_address(usb_hc_connection_t *connection,
207 usb_address_t address, devman_handle_t *handle)
208{
209 async_exch_t *exch;
210 EXCH_INIT(connection, exch);
211
212 const int ret = usbhc_get_handle(exch, address, handle);
213
214 EXCH_FINI(connection, exch);
215 return ret;
216}
217
218int usb_hc_release_address(usb_hc_connection_t *connection,
219 usb_address_t address)
220{
221 async_exch_t *exch;
222 EXCH_INIT(connection, exch);
223
224 const int ret = usbhc_release_address(exch, address);
225
226 EXCH_FINI(connection, exch);
227 return ret;
228}
229
230int usb_hc_register_endpoint(usb_hc_connection_t *connection,
231 usb_address_t address, usb_endpoint_t endpoint, usb_transfer_type_t type,
232 usb_direction_t direction, size_t packet_size, unsigned interval)
233{
234 async_exch_t *exch;
235 EXCH_INIT(connection, exch);
236
237 const int ret = usbhc_register_endpoint(exch, address, endpoint,
238 type, direction, packet_size, interval);
239
240 EXCH_FINI(connection, exch);
241 return ret;
242}
243
244int usb_hc_unregister_endpoint(usb_hc_connection_t *connection,
245 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
246{
247 async_exch_t *exch;
248 EXCH_INIT(connection, exch);
249
250 const int ret =
251 usbhc_unregister_endpoint(exch, address, endpoint, direction);
252
253 EXCH_FINI(connection, exch);
254 return ret;
255}
256
257int usb_hc_read(usb_hc_connection_t *connection, usb_address_t address,
258 usb_endpoint_t endpoint, uint64_t setup, void *data, size_t size,
259 size_t *real_size)
260{
261 async_exch_t *exch;
262 EXCH_INIT(connection, exch);
263
264 const int ret =
265 usbhc_read(exch, address, endpoint, setup, data, size, real_size);
266
267 EXCH_FINI(connection, exch);
268 return ret;
269}
270
271int usb_hc_write(usb_hc_connection_t *connection, usb_address_t address,
272 usb_endpoint_t endpoint, uint64_t setup, const void *data, size_t size)
273{
274 async_exch_t *exch;
275 EXCH_INIT(connection, exch);
276
277 const int ret = usbhc_write(exch, address, endpoint, setup, data, size);
278
279 EXCH_FINI(connection, exch);
280 return ret;
281}
282
283/**
284 * @}
285 */
Note: See TracBrowser for help on using the repository browser.