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
RevLine 
[c5d61ae6]1/*
2 * Copyright (c) 2011 Vojtech Horky
[6df14c5]3 * Copyright (c) 2011 Jan Vesely
[c5d61ae6]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 */
[77ad86c]29
[0edf7c7]30/** @addtogroup libusb
[c5d61ae6]31 * @{
32 */
33/** @file
[0edf7c7]34 * General communication with host controller driver (implementation).
[c5d61ae6]35 */
[77ad86c]36
[eb1a2f4]37#include <usb/debug.h>
[29e479f]38
[eb1a2f4]39#include <assert.h>
[29e479f]40#include <errno.h>
41#include <usbhc_iface.h>
42#include <usb/dev.h>
43#include <usb/hc.h>
[c5d61ae6]44
[6df14c5]45static int usb_hc_connection_add_ref(usb_hc_connection_t *connection)
46{
47 assert(connection);
[77ad86c]48
[6df14c5]49 fibril_mutex_lock(&connection->guard);
50 if (connection->ref_count == 0) {
51 assert(connection->hc_sess == NULL);
52 /* Parallel exchange for us */
[f9b2cb4c]53 connection->hc_sess = devman_device_connect(connection->hc_handle, 0);
[6df14c5]54 if (!connection->hc_sess) {
55 fibril_mutex_unlock(&connection->guard);
56 return ENOMEM;
57 }
58 }
[77ad86c]59
[6df14c5]60 ++connection->ref_count;
61 fibril_mutex_unlock(&connection->guard);
62 return EOK;
63}
[77ad86c]64
[6df14c5]65static int usb_hc_connection_del_ref(usb_hc_connection_t *connection)
66{
67 assert(connection);
[77ad86c]68
[6df14c5]69 fibril_mutex_lock(&connection->guard);
[30ec5ea]70 if (connection->ref_count == 0) {
71 /* Closing already closed connection... */
[6df2202]72 assert(connection->hc_sess == NULL);
[30ec5ea]73 fibril_mutex_unlock(&connection->guard);
74 return EOK;
75 }
[77ad86c]76
[6df14c5]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);
[7d5ef94]82 connection->hc_sess = NULL;
[6df14c5]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
[c5d61ae6]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,
[56fd7cf]116 ddf_dev_t *device)
[c5d61ae6]117{
[56fd7cf]118 if (device == NULL)
[c5d61ae6]119 return EBADMEM;
120
121 devman_handle_t hc_handle;
[56fd7cf]122 const int rc = usb_get_hc_by_handle(ddf_dev_get_handle(device), &hc_handle);
[6df14c5]123 if (rc == EOK) {
124 usb_hc_connection_initialize(connection, hc_handle);
[c5d61ae6]125 }
126
127 return rc;
128}
[77ad86c]129
[da2f1c9e]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}
[77ad86c]144
[c5d61ae6]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{
[6df14c5]152 return usb_hc_connection_add_ref(connection);
[c5d61ae6]153}
[77ad86c]154
[c5d61ae6]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{
[6df14c5]162 return usb_hc_connection_del_ref(connection);
163}
[77ad86c]164
[6df14c5]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);
[c5d61ae6]179
[6df14c5]180 usb_address_t address = preferred;
181 const int ret = usbhc_request_address(exch, &address, strict, speed);
[c5d61ae6]182
[6df14c5]183 EXCH_FINI(connection, exch);
184 return ret == EOK ? address : ret;
185}
[77ad86c]186
[6df14c5]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);
[c5d61ae6]192
[6df14c5]193 const int ret = usbhc_bind_address(exch, address, handle);
[c5d61ae6]194
[6df14c5]195 EXCH_FINI(connection, exch);
196 return ret;
[c5d61ae6]197}
[77ad86c]198
[0edf7c7]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{
[6df14c5]209 async_exch_t *exch;
210 EXCH_INIT(connection, exch);
[02fc5c4]211
212 const int ret = usbhc_get_handle(exch, address, handle);
[6df14c5]213
214 EXCH_FINI(connection, exch);
215 return ret;
216}
[77ad86c]217
[6df14c5]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);
[02fc5c4]227 return ret;
[0edf7c7]228}
[77ad86c]229
[6df14c5]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}
[77ad86c]243
[6df14c5]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}
[77ad86c]256
[30ec5ea]257int usb_hc_read(usb_hc_connection_t *connection, usb_address_t address,
[6df14c5]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}
[77ad86c]270
[30ec5ea]271int usb_hc_write(usb_hc_connection_t *connection, usb_address_t address,
[6df14c5]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}
[e8f826b]282
[c5d61ae6]283/**
284 * @}
285 */
Note: See TracBrowser for help on using the repository browser.