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/dev/hc.h>
|
---|
39 | #include <usb/driver.h>
|
---|
40 | #include <usb/debug.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <assert.h>
|
---|
43 |
|
---|
44 | /** Initialize connection to USB host controller.
|
---|
45 | *
|
---|
46 | * @param connection Connection to be initialized.
|
---|
47 | * @param device Device connecting to the host controller.
|
---|
48 | * @return Error code.
|
---|
49 | */
|
---|
50 | int usb_hc_connection_initialize_from_device(usb_hc_connection_t *connection,
|
---|
51 | ddf_dev_t *device)
|
---|
52 | {
|
---|
53 | assert(connection);
|
---|
54 |
|
---|
55 | if (device == NULL) {
|
---|
56 | return EBADMEM;
|
---|
57 | }
|
---|
58 |
|
---|
59 | devman_handle_t hc_handle;
|
---|
60 | int rc = usb_hc_find(device->handle, &hc_handle);
|
---|
61 | if (rc != EOK) {
|
---|
62 | return rc;
|
---|
63 | }
|
---|
64 |
|
---|
65 | rc = usb_hc_connection_initialize(connection, hc_handle);
|
---|
66 |
|
---|
67 | return rc;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Manually initialize connection to USB host controller.
|
---|
71 | *
|
---|
72 | * @param connection Connection to be initialized.
|
---|
73 | * @param hc_handle Devman handle of the host controller.
|
---|
74 | * @return Error code.
|
---|
75 | */
|
---|
76 | int usb_hc_connection_initialize(usb_hc_connection_t *connection,
|
---|
77 | devman_handle_t hc_handle)
|
---|
78 | {
|
---|
79 | assert(connection);
|
---|
80 |
|
---|
81 | connection->hc_handle = hc_handle;
|
---|
82 | connection->hc_phone = -1;
|
---|
83 |
|
---|
84 | return EOK;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** Open connection to host controller.
|
---|
88 | *
|
---|
89 | * @param connection Connection to the host controller.
|
---|
90 | * @return Error code.
|
---|
91 | */
|
---|
92 | int usb_hc_connection_open(usb_hc_connection_t *connection)
|
---|
93 | {
|
---|
94 | assert(connection);
|
---|
95 |
|
---|
96 | if (usb_hc_connection_is_opened(connection)) {
|
---|
97 | return EBUSY;
|
---|
98 | }
|
---|
99 |
|
---|
100 | int phone = devman_device_connect(connection->hc_handle, 0);
|
---|
101 | if (phone < 0) {
|
---|
102 | return phone;
|
---|
103 | }
|
---|
104 |
|
---|
105 | connection->hc_phone = phone;
|
---|
106 |
|
---|
107 | return EOK;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** Tells whether connection to host controller is opened.
|
---|
111 | *
|
---|
112 | * @param connection Connection to the host controller.
|
---|
113 | * @return Whether connection is opened.
|
---|
114 | */
|
---|
115 | bool usb_hc_connection_is_opened(const usb_hc_connection_t *connection)
|
---|
116 | {
|
---|
117 | assert(connection);
|
---|
118 |
|
---|
119 | return (connection->hc_phone >= 0);
|
---|
120 | }
|
---|
121 |
|
---|
122 | /** Close connection to the host controller.
|
---|
123 | *
|
---|
124 | * @param connection Connection to the host controller.
|
---|
125 | * @return Error code.
|
---|
126 | */
|
---|
127 | int usb_hc_connection_close(usb_hc_connection_t *connection)
|
---|
128 | {
|
---|
129 | assert(connection);
|
---|
130 |
|
---|
131 | if (!usb_hc_connection_is_opened(connection)) {
|
---|
132 | return ENOENT;
|
---|
133 | }
|
---|
134 |
|
---|
135 | int rc = async_hangup(connection->hc_phone);
|
---|
136 | if (rc != EOK) {
|
---|
137 | return rc;
|
---|
138 | }
|
---|
139 |
|
---|
140 | connection->hc_phone = -1;
|
---|
141 |
|
---|
142 | return EOK;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * @}
|
---|
147 | */
|
---|