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 | * USB endpoint pipes miscellaneous functions.
|
---|
34 | */
|
---|
35 | #include <usb/usb.h>
|
---|
36 | #include <usb/pipes.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <assert.h>
|
---|
39 | #include <usb/usbdrv.h>
|
---|
40 |
|
---|
41 | /** Initialize connection to USB device.
|
---|
42 | *
|
---|
43 | * @param connection Connection structure to be initialized.
|
---|
44 | * @param device Generic device backing the USB device.
|
---|
45 | * @return Error code.
|
---|
46 | */
|
---|
47 | int usb_device_connection_initialize_from_device(
|
---|
48 | usb_device_connection_t *connection, device_t *device)
|
---|
49 | {
|
---|
50 | assert(connection);
|
---|
51 | assert(device);
|
---|
52 |
|
---|
53 | int rc;
|
---|
54 | devman_handle_t hc_handle;
|
---|
55 | usb_address_t my_address;
|
---|
56 |
|
---|
57 | rc = usb_drv_find_hc(device, &hc_handle);
|
---|
58 | if (rc != EOK) {
|
---|
59 | return rc;
|
---|
60 | }
|
---|
61 |
|
---|
62 | int hc_phone = devman_device_connect(hc_handle, 0);
|
---|
63 | if (hc_phone < 0) {
|
---|
64 | return hc_phone;
|
---|
65 | }
|
---|
66 |
|
---|
67 | my_address = usb_drv_get_my_address(hc_phone, device);
|
---|
68 | if (my_address < 0) {
|
---|
69 | rc = my_address;
|
---|
70 | goto leave;
|
---|
71 | }
|
---|
72 |
|
---|
73 | rc = usb_device_connection_initialize(connection,
|
---|
74 | hc_handle, my_address);
|
---|
75 |
|
---|
76 | leave:
|
---|
77 | async_hangup(hc_phone);
|
---|
78 | return rc;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Initialize connection to USB device.
|
---|
82 | *
|
---|
83 | * @param connection Connection structure to be initialized.
|
---|
84 | * @param host_controller_handle Devman handle of host controller device is
|
---|
85 | * connected to.
|
---|
86 | * @param device_address Device USB address.
|
---|
87 | * @return Error code.
|
---|
88 | */
|
---|
89 | int usb_device_connection_initialize(usb_device_connection_t *connection,
|
---|
90 | devman_handle_t host_controller_handle, usb_address_t device_address)
|
---|
91 | {
|
---|
92 | assert(connection);
|
---|
93 |
|
---|
94 | if ((device_address < 0) || (device_address >= USB11_ADDRESS_MAX)) {
|
---|
95 | return EINVAL;
|
---|
96 | }
|
---|
97 |
|
---|
98 | connection->hc_handle = host_controller_handle;
|
---|
99 | connection->address = device_address;
|
---|
100 |
|
---|
101 | return EOK;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /** Start a session on the endpoint pipe.
|
---|
106 | *
|
---|
107 | * A session is something inside what any communication occurs.
|
---|
108 | * It is expected that sessions would be started right before the transfer
|
---|
109 | * and ended - see usb_endpoint_pipe_end_session() - after the last
|
---|
110 | * transfer.
|
---|
111 | * The reason for this is that session actually opens some communication
|
---|
112 | * channel to the host controller (or to the physical hardware if you
|
---|
113 | * wish) and thus it involves acquiring kernel resources.
|
---|
114 | * Since they are limited, sessions shall not be longer than strictly
|
---|
115 | * necessary.
|
---|
116 | *
|
---|
117 | * @param pipe Endpoint pipe to start the session on.
|
---|
118 | * @return Error code.
|
---|
119 | */
|
---|
120 | int usb_endpoint_pipe_start_session(usb_endpoint_pipe_t *pipe)
|
---|
121 | {
|
---|
122 | assert(pipe);
|
---|
123 |
|
---|
124 | if (pipe->hc_phone >= 0) {
|
---|
125 | return EBUSY;
|
---|
126 | }
|
---|
127 |
|
---|
128 | int phone = devman_device_connect(pipe->wire->hc_handle, 0);
|
---|
129 | if (phone < 0) {
|
---|
130 | return phone;
|
---|
131 | }
|
---|
132 |
|
---|
133 | pipe->hc_phone = phone;
|
---|
134 |
|
---|
135 | return EOK;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | /** Ends a session on the endpoint pipe.
|
---|
140 | *
|
---|
141 | * @see usb_endpoint_pipe_start_session
|
---|
142 | *
|
---|
143 | * @param pipe Endpoint pipe to end the session on.
|
---|
144 | * @return Error code.
|
---|
145 | */
|
---|
146 | int usb_endpoint_pipe_end_session(usb_endpoint_pipe_t *pipe)
|
---|
147 | {
|
---|
148 | assert(pipe);
|
---|
149 |
|
---|
150 | if (pipe->hc_phone < 0) {
|
---|
151 | return ENOENT;
|
---|
152 | }
|
---|
153 |
|
---|
154 | int rc = async_hangup(pipe->hc_phone);
|
---|
155 | if (rc != EOK) {
|
---|
156 | return rc;
|
---|
157 | }
|
---|
158 |
|
---|
159 | pipe->hc_phone = -1;
|
---|
160 |
|
---|
161 | return EOK;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * @}
|
---|
166 | */
|
---|