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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bd575647 was 2c202c5, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

libusb: Add device_connection_t.

  • Property mode set to 100644
File size: 9.0 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#include <devman.h>
37#include <async.h>
38#include <dev_iface.h>
39#include <usbhc_iface.h>
40#include <usb/hc.h>
41#include <usb/debug.h>
42#include <usb/dev.h>
43#include <errno.h>
44#include <assert.h>
45
46static int usb_hc_connection_add_ref(usb_hc_connection_t *connection)
47{
48 assert(connection);
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(EXCHANGE_PARALLEL,
54 connection->hc_handle, 0);
55 if (!connection->hc_sess) {
56 fibril_mutex_unlock(&connection->guard);
57 return ENOMEM;
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 fibril_mutex_lock(&connection->guard);
69 --connection->ref_count;
70 int ret = EOK;
71 if (connection->ref_count == 0) {
72 assert(connection->hc_sess);
73 ret = async_hangup(connection->hc_sess);
74 }
75 fibril_mutex_unlock(&connection->guard);
76 return ret;
77}
78
79#define EXCH_INIT(connection, exch) \
80do { \
81 exch = NULL; \
82 if (!connection) \
83 return EBADMEM; \
84 const int ret = usb_hc_connection_add_ref(connection); \
85 if (ret != EOK) \
86 return ret; \
87 exch = async_exchange_begin(connection->hc_sess); \
88 if (exch == NULL) { \
89 usb_hc_connection_del_ref(connection); \
90 return ENOMEM; \
91 } \
92} while (0)
93
94#define EXCH_FINI(connection, exch) \
95if (exch) { \
96 async_exchange_end(exch); \
97 usb_hc_connection_del_ref(connection); \
98} else (void)0
99
100/** Initialize connection to USB host controller.
101 *
102 * @param connection Connection to be initialized.
103 * @param device Device connecting to the host controller.
104 * @return Error code.
105 */
106int usb_hc_connection_initialize_from_device(usb_hc_connection_t *connection,
107 const ddf_dev_t *device)
108{
109 assert(connection);
110
111 if (device == NULL) {
112 return EBADMEM;
113 }
114
115 devman_handle_t hc_handle;
116 const int rc = usb_get_hc_by_handle(device->handle, &hc_handle);
117 if (rc == EOK) {
118 usb_hc_connection_initialize(connection, hc_handle);
119 }
120
121 return rc;
122}
123/*----------------------------------------------------------------------------*/
124/** Open connection to host controller.
125 *
126 * @param connection Connection to the host controller.
127 * @return Error code.
128 */
129int usb_hc_connection_open(usb_hc_connection_t *connection)
130{
131 return usb_hc_connection_add_ref(connection);
132}
133/*----------------------------------------------------------------------------*/
134/** Tells whether connection to host controller is opened.
135 *
136 * @param connection Connection to the host controller.
137 * @return Whether connection is opened.
138 */
139bool usb_hc_connection_is_open(const usb_hc_connection_t *connection)
140{
141 assert(connection);
142 return (connection->hc_sess != NULL);
143}
144/*----------------------------------------------------------------------------*/
145/** Close connection to the host controller.
146 *
147 * @param connection Connection to the host controller.
148 * @return Error code.
149 */
150int usb_hc_connection_close(usb_hc_connection_t *connection)
151{
152 return usb_hc_connection_del_ref(connection);
153}
154/*----------------------------------------------------------------------------*/
155/** Ask host controller for free address assignment.
156 *
157 * @param connection Opened connection to host controller.
158 * @param preferred Preferred SUB address.
159 * @param strict Fail if the preferred address is not avialable.
160 * @param speed Speed of the new device (device that will be assigned
161 * the returned address).
162 * @return Assigned USB address or negative error code.
163 */
164usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
165 usb_address_t preferred, bool strict, usb_speed_t speed)
166{
167 async_exch_t *exch;
168 EXCH_INIT(connection, exch);
169
170 usb_address_t address = preferred;
171 const int ret = usbhc_request_address(exch, &address, strict, speed);
172
173 EXCH_FINI(connection, exch);
174 return ret == EOK ? address : ret;
175}
176/*----------------------------------------------------------------------------*/
177int usb_hc_bind_address(usb_hc_connection_t * connection,
178 usb_address_t address, devman_handle_t handle)
179{
180 async_exch_t *exch;
181 EXCH_INIT(connection, exch);
182
183 const int ret = usbhc_bind_address(exch, address, handle);
184
185 EXCH_FINI(connection, exch);
186 return ret;
187}
188/*----------------------------------------------------------------------------*/
189/** Get handle of USB device with given address.
190 *
191 * @param[in] connection Opened connection to host controller.
192 * @param[in] address Address of device in question.
193 * @param[out] handle Where to write the device handle.
194 * @return Error code.
195 */
196int usb_hc_get_handle_by_address(usb_hc_connection_t *connection,
197 usb_address_t address, devman_handle_t *handle)
198{
199 async_exch_t *exch;
200 EXCH_INIT(connection, exch);
201
202 const int ret = usbhc_get_handle(exch, address, handle);
203
204 EXCH_FINI(connection, exch);
205 return ret;
206}
207/*----------------------------------------------------------------------------*/
208int usb_hc_release_address(usb_hc_connection_t *connection,
209 usb_address_t address)
210{
211 async_exch_t *exch;
212 EXCH_INIT(connection, exch);
213
214 const int ret = usbhc_release_address(exch, address);
215
216 EXCH_FINI(connection, exch);
217 return ret;
218}
219/*----------------------------------------------------------------------------*/
220int usb_hc_register_endpoint(usb_hc_connection_t *connection,
221 usb_address_t address, usb_endpoint_t endpoint, usb_transfer_type_t type,
222 usb_direction_t direction, size_t packet_size, unsigned interval)
223{
224 async_exch_t *exch;
225 EXCH_INIT(connection, exch);
226
227 const int ret = usbhc_register_endpoint(exch, address, endpoint,
228 type, direction, packet_size, interval);
229
230 EXCH_FINI(connection, exch);
231 return ret;
232}
233/*----------------------------------------------------------------------------*/
234int usb_hc_unregister_endpoint(usb_hc_connection_t *connection,
235 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
236{
237 async_exch_t *exch;
238 EXCH_INIT(connection, exch);
239
240 const int ret =
241 usbhc_unregister_endpoint(exch, address, endpoint, direction);
242
243 EXCH_FINI(connection, exch);
244 return ret;
245}
246/*----------------------------------------------------------------------------*/
247int usb_hc_control_read(usb_hc_connection_t *connection, usb_address_t address,
248 usb_endpoint_t endpoint, uint64_t setup, void *data, size_t size,
249 size_t *real_size)
250{
251 async_exch_t *exch;
252 EXCH_INIT(connection, exch);
253
254 const int ret =
255 usbhc_read(exch, address, endpoint, setup, data, size, real_size);
256
257 EXCH_FINI(connection, exch);
258 return ret;
259}
260/*----------------------------------------------------------------------------*/
261int usb_hc_control_write(usb_hc_connection_t *connection, usb_address_t address,
262 usb_endpoint_t endpoint, uint64_t setup, const void *data, size_t size)
263{
264 async_exch_t *exch;
265 EXCH_INIT(connection, exch);
266
267 const int ret = usbhc_write(exch, address, endpoint, setup, data, size);
268
269 EXCH_FINI(connection, exch);
270 return ret;
271}
272/*----------------------------------------------------------------------------*/
273/** Get host controller handle by its class index.
274 *
275 * @param sid Service ID of the HC function.
276 * @param hc_handle Where to store the HC handle
277 * (can be NULL for existence test only).
278 * @return Error code.
279 */
280int usb_ddf_get_hc_handle_by_sid(service_id_t sid, devman_handle_t *hc_handle)
281{
282 devman_handle_t handle;
283
284 const int ret = devman_fun_sid_to_handle(sid, &handle);
285 if (ret == EOK && hc_handle != NULL)
286 *hc_handle = handle;
287
288 return ret;
289}
290
291/**
292 * @}
293 */
Note: See TracBrowser for help on using the repository browser.