1 | /*
|
---|
2 | * Copyright (c) 2010 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 usb
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief USB driver - standard USB requests (implementation).
|
---|
34 | */
|
---|
35 | #include <usb/usbdrv.h>
|
---|
36 | #include <errno.h>
|
---|
37 |
|
---|
38 | /** Change address of connected device.
|
---|
39 | *
|
---|
40 | * @see usb_drv_reserve_default_address
|
---|
41 | * @see usb_drv_release_default_address
|
---|
42 | * @see usb_drv_request_address
|
---|
43 | * @see usb_drv_release_address
|
---|
44 | * @see usb_drv_bind_address
|
---|
45 | *
|
---|
46 | * @param phone Open phone to HC driver.
|
---|
47 | * @param old_address Current address.
|
---|
48 | * @param address Address to be set.
|
---|
49 | * @return Error code.
|
---|
50 | */
|
---|
51 | int usb_drv_req_set_address(int phone, usb_address_t old_address,
|
---|
52 | usb_address_t new_address)
|
---|
53 | {
|
---|
54 | /* Prepare the target. */
|
---|
55 | usb_target_t target = {
|
---|
56 | .address = old_address,
|
---|
57 | .endpoint = 0
|
---|
58 | };
|
---|
59 |
|
---|
60 | /* Prepare the setup packet. */
|
---|
61 | usb_device_request_setup_packet_t setup_packet = {
|
---|
62 | .request_type = 0,
|
---|
63 | .request = USB_DEVREQ_SET_ADDRESS,
|
---|
64 | .index = 0,
|
---|
65 | .length = 0,
|
---|
66 | };
|
---|
67 | setup_packet.value = new_address;
|
---|
68 |
|
---|
69 | int rc = usb_drv_psync_control_write(phone, target,
|
---|
70 | &setup_packet, sizeof(setup_packet), NULL, 0);
|
---|
71 |
|
---|
72 | return rc;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** Retrieve device descriptor of connected USB device.
|
---|
76 | *
|
---|
77 | * @param[in] phone Open phone to HC driver.
|
---|
78 | * @param[in] address Device USB address.
|
---|
79 | * @param[out] descriptor Storage for the device descriptor.
|
---|
80 | * @return Error code.
|
---|
81 | * @retval EBADMEM @p descriptor is NULL.
|
---|
82 | */
|
---|
83 | int usb_drv_req_get_device_descriptor(int phone, usb_address_t address,
|
---|
84 | usb_standard_device_descriptor_t *descriptor)
|
---|
85 | {
|
---|
86 | if (descriptor == NULL) {
|
---|
87 | return EBADMEM;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /* Prepare the target. */
|
---|
91 | usb_target_t target = {
|
---|
92 | .address = address,
|
---|
93 | .endpoint = 0
|
---|
94 | };
|
---|
95 |
|
---|
96 | /* Prepare the setup packet. */
|
---|
97 | usb_device_request_setup_packet_t setup_packet = {
|
---|
98 | .request_type = 128,
|
---|
99 | .request = USB_DEVREQ_GET_DESCRIPTOR,
|
---|
100 | .index = 0,
|
---|
101 | .length = sizeof(usb_standard_device_descriptor_t)
|
---|
102 | };
|
---|
103 | setup_packet.value_high = USB_DESCTYPE_DEVICE;
|
---|
104 | setup_packet.value_low = 0;
|
---|
105 |
|
---|
106 | /* Prepare local descriptor. */
|
---|
107 | size_t actually_transferred = 0;
|
---|
108 | usb_standard_device_descriptor_t descriptor_tmp;
|
---|
109 |
|
---|
110 | /* Perform the control read transaction. */
|
---|
111 | int rc = usb_drv_psync_control_read(phone, target,
|
---|
112 | &setup_packet, sizeof(setup_packet),
|
---|
113 | &descriptor_tmp, sizeof(descriptor_tmp), &actually_transferred);
|
---|
114 |
|
---|
115 | if (rc != EOK) {
|
---|
116 | return rc;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /* Verify that all data has been transferred. */
|
---|
120 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
121 | return ELIMIT;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* Everything is okay, copy the descriptor. */
|
---|
125 | memcpy(descriptor, &descriptor_tmp,
|
---|
126 | sizeof(descriptor_tmp));
|
---|
127 |
|
---|
128 | return EOK;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /** Retrieve configuration descriptor of connected USB device.
|
---|
133 | *
|
---|
134 | * The function does not retrieve additional data binded with configuration
|
---|
135 | * descriptor (such as its interface and endpoint descriptors) - use
|
---|
136 | * usb_drv_req_get_full_configuration_descriptor() instead.
|
---|
137 | *
|
---|
138 | * @param[in] phone Open phone to HC driver.
|
---|
139 | * @param[in] address Device USB address.
|
---|
140 | * @param[in] index Configuration descriptor index.
|
---|
141 | * @param[out] descriptor Storage for the configuration descriptor.
|
---|
142 | * @return Error code.
|
---|
143 | * @retval EBADMEM @p descriptor is NULL.
|
---|
144 | */
|
---|
145 | int usb_drv_req_get_bare_configuration_descriptor(int phone,
|
---|
146 | usb_address_t address, int index,
|
---|
147 | usb_standard_configuration_descriptor_t *descriptor)
|
---|
148 | {
|
---|
149 | if (descriptor == NULL) {
|
---|
150 | return EBADMEM;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /* Prepare the target. */
|
---|
154 | usb_target_t target = {
|
---|
155 | .address = address,
|
---|
156 | .endpoint = 0
|
---|
157 | };
|
---|
158 |
|
---|
159 | /* Prepare the setup packet. */
|
---|
160 | usb_device_request_setup_packet_t setup_packet = {
|
---|
161 | .request_type = 128,
|
---|
162 | .request = USB_DEVREQ_GET_DESCRIPTOR,
|
---|
163 | .index = 0,
|
---|
164 | .length = sizeof(usb_standard_configuration_descriptor_t)
|
---|
165 | };
|
---|
166 | setup_packet.value_high = USB_DESCTYPE_CONFIGURATION;
|
---|
167 | setup_packet.value_low = index;
|
---|
168 |
|
---|
169 | /* Prepare local descriptor. */
|
---|
170 | size_t actually_transferred = 0;
|
---|
171 | usb_standard_configuration_descriptor_t descriptor_tmp;
|
---|
172 |
|
---|
173 | /* Perform the control read transaction. */
|
---|
174 | int rc = usb_drv_psync_control_read(phone, target,
|
---|
175 | &setup_packet, sizeof(setup_packet),
|
---|
176 | &descriptor_tmp, sizeof(descriptor_tmp), &actually_transferred);
|
---|
177 |
|
---|
178 | if (rc != EOK) {
|
---|
179 | return rc;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /* Verify that all data has been transferred. */
|
---|
183 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
184 | return ELIMIT;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /* Everything is okay, copy the descriptor. */
|
---|
188 | memcpy(descriptor, &descriptor_tmp,
|
---|
189 | sizeof(descriptor_tmp));
|
---|
190 |
|
---|
191 | return EOK;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /** Retrieve full configuration descriptor of connected USB device.
|
---|
195 | *
|
---|
196 | * @warning The @p buffer might be touched (i.e. its contents changed)
|
---|
197 | * even when error occurres.
|
---|
198 | *
|
---|
199 | * @param[in] phone Open phone to HC driver.
|
---|
200 | * @param[in] address Device USB address.
|
---|
201 | * @param[in] index Configuration descriptor index.
|
---|
202 | * @param[out] buffer Buffer for the whole configuration descriptor.
|
---|
203 | * @param[in] buffer_size Size of the prepared @p buffer.
|
---|
204 | * @param[out] actual_buffer_size Bytes actually transfered.
|
---|
205 | * @return Error code.
|
---|
206 | * @retval EBADMEM @p descriptor is NULL.
|
---|
207 | */
|
---|
208 | int usb_drv_req_get_full_configuration_descriptor(int phone,
|
---|
209 | usb_address_t address, int index,
|
---|
210 | void *buffer, size_t buffer_size, size_t *actual_buffer_size)
|
---|
211 | {
|
---|
212 | if (buffer == NULL) {
|
---|
213 | return EBADMEM;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /* Prepare the target. */
|
---|
217 | usb_target_t target = {
|
---|
218 | .address = address,
|
---|
219 | .endpoint = 0
|
---|
220 | };
|
---|
221 |
|
---|
222 | /* Prepare the setup packet. */
|
---|
223 | usb_device_request_setup_packet_t setup_packet = {
|
---|
224 | .request_type = 128,
|
---|
225 | .request = USB_DEVREQ_GET_DESCRIPTOR,
|
---|
226 | .index = 0,
|
---|
227 | .length = buffer_size
|
---|
228 | };
|
---|
229 | setup_packet.value_high = USB_DESCTYPE_CONFIGURATION;
|
---|
230 | setup_packet.value_low = index;
|
---|
231 |
|
---|
232 | /* Perform the control read transaction. */
|
---|
233 | int rc = usb_drv_psync_control_read(phone, target,
|
---|
234 | &setup_packet, sizeof(setup_packet),
|
---|
235 | buffer, buffer_size, actual_buffer_size);
|
---|
236 |
|
---|
237 | return rc;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * @}
|
---|
243 | */
|
---|