source: mainline/uspace/lib/usb/src/usbdevice.c@ 62066b4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 62066b4 was c5d61ae6, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Add wrapper for general connection to host controller

  • Property mode set to 100644
File size: 4.4 KB
Line 
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 between device drivers and host controller driver.
34 */
35#include <devman.h>
36#include <async.h>
37#include <usb_iface.h>
38#include <usb/usbdevice.h>
39#include <errno.h>
40
41/** Find host controller handle that is ancestor of given device.
42 *
43 * @param[in] device_handle Device devman handle.
44 * @param[out] hc_handle Where to store handle of host controller
45 * controlling device with @p device_handle handle.
46 * @return Error code.
47 */
48int usb_hc_find(devman_handle_t device_handle, devman_handle_t *hc_handle)
49{
50 int parent_phone = devman_parent_device_connect(device_handle,
51 IPC_FLAG_BLOCKING);
52 if (parent_phone < 0) {
53 return parent_phone;
54 }
55
56 devman_handle_t h;
57 int rc = async_req_1_1(parent_phone, DEV_IFACE_ID(USB_DEV_IFACE),
58 IPC_M_USB_GET_HOST_CONTROLLER_HANDLE, &h);
59
60 async_hangup(parent_phone);
61
62 if (rc != EOK) {
63 return rc;
64 }
65
66 if (hc_handle != NULL) {
67 *hc_handle = h;
68 }
69
70 return EOK;
71}
72
73/** Initialize connection to USB host controller.
74 *
75 * @param connection Connection to be initialized.
76 * @param device Device connecting to the host controller.
77 * @return Error code.
78 */
79int usb_hc_connection_initialize_from_device(usb_hc_connection_t *connection,
80 device_t *device)
81{
82 assert(connection);
83
84 if (device == NULL) {
85 return EBADMEM;
86 }
87
88 devman_handle_t hc_handle;
89 int rc = usb_hc_find(device->handle, &hc_handle);
90 if (rc != EOK) {
91 return rc;
92 }
93
94 rc = usb_hc_connection_initialize(connection, hc_handle);
95
96 return rc;
97}
98
99/** Manually initialize connection to USB host controller.
100 *
101 * @param connection Connection to be initialized.
102 * @param hc_handle Devman handle of the host controller.
103 * @return Error code.
104 */
105int usb_hc_connection_initialize(usb_hc_connection_t *connection,
106 devman_handle_t hc_handle)
107{
108 assert(connection);
109
110 connection->hc_handle = hc_handle;
111 connection->hc_phone = -1;
112
113 return EOK;
114}
115
116/** Open connection to host controller.
117 *
118 * @param connection Connection to the host controller.
119 * @return Error code.
120 */
121int usb_hc_connection_open(usb_hc_connection_t *connection)
122{
123 assert(connection);
124
125 if (usb_hc_connection_is_opened(connection)) {
126 return EBUSY;
127 }
128
129 int phone = devman_device_connect(connection->hc_handle, 0);
130 if (phone < 0) {
131 return phone;
132 }
133
134 connection->hc_phone = phone;
135
136 return EOK;
137}
138
139/** Tells whether connection to host controller is opened.
140 *
141 * @param connection Connection to the host controller.
142 * @return Whether connection is opened.
143 */
144bool usb_hc_connection_is_opened(const usb_hc_connection_t *connection)
145{
146 assert(connection);
147
148 return (connection->hc_phone >= 0);
149}
150
151/** Close connection to the host controller.
152 *
153 * @param connection Connection to the host controller.
154 * @return Error code.
155 */
156int usb_hc_connection_close(usb_hc_connection_t *connection)
157{
158 assert(connection);
159
160 if (!usb_hc_connection_is_opened(connection)) {
161 return ENOENT;
162 }
163
164 int rc = async_hangup(connection->hc_phone);
165 if (rc != EOK) {
166 return rc;
167 }
168
169 connection->hc_phone = -1;
170
171 return EOK;
172}
173
174/**
175 * @}
176 */
Note: See TracBrowser for help on using the repository browser.