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

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

libusb: Remove unused function, cleanup includes.

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