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 | * Standard USB requests (implementation).
|
---|
34 | */
|
---|
35 | #include <usb/request.h>
|
---|
36 | #include <usb/devreq.h>
|
---|
37 | #include <errno.h>
|
---|
38 |
|
---|
39 | #define MAX_DATA_LENGTH ((size_t)(0xFFFF))
|
---|
40 |
|
---|
41 | /** Generic wrapper for SET requests using standard control request format.
|
---|
42 | *
|
---|
43 | * @see usb_endpoint_pipe_control_write
|
---|
44 | *
|
---|
45 | * @param pipe Pipe used for the communication.
|
---|
46 | * @param request_type Request type (standard/class/vendor).
|
---|
47 | * @param recipient Request recipient (e.g. device or endpoint).
|
---|
48 | * @param request Actual request (e.g. GET_DESCRIPTOR).
|
---|
49 | * @param value Value of @c wValue field of setup packet
|
---|
50 | * (must be in USB endianness).
|
---|
51 | * @param index Value of @c wIndex field of setup packet
|
---|
52 | * (must be in USB endianness).
|
---|
53 | * @param data Data to be sent during DATA stage
|
---|
54 | * (expected to be in USB endianness).
|
---|
55 | * @param data_size Size of the @p data buffer (in native endianness).
|
---|
56 | * @return Error code.
|
---|
57 | * @retval EBADMEM @p pipe is NULL.
|
---|
58 | * @retval EBADMEM @p data is NULL and @p data_size is not zero.
|
---|
59 | * @retval ERANGE Data buffer too large.
|
---|
60 | */
|
---|
61 | int usb_control_request_set(usb_endpoint_pipe_t *pipe,
|
---|
62 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
63 | uint8_t request,
|
---|
64 | uint16_t value, uint16_t index,
|
---|
65 | void *data, size_t data_size)
|
---|
66 | {
|
---|
67 | if (pipe == NULL) {
|
---|
68 | return EBADMEM;
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (data_size > MAX_DATA_LENGTH) {
|
---|
72 | return ERANGE;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if ((data_size > 0) && (data == NULL)) {
|
---|
76 | return EBADMEM;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * TODO: check that @p request_type and @p recipient are
|
---|
81 | * within ranges.
|
---|
82 | */
|
---|
83 |
|
---|
84 | usb_device_request_setup_packet_t setup_packet;
|
---|
85 | setup_packet.request_type = (request_type << 5) | recipient;
|
---|
86 | setup_packet.request = request;
|
---|
87 | setup_packet.value = value;
|
---|
88 | setup_packet.index = index;
|
---|
89 | setup_packet.length = (uint16_t) data_size;
|
---|
90 |
|
---|
91 | int rc = usb_endpoint_pipe_control_write(pipe,
|
---|
92 | &setup_packet, sizeof(setup_packet),
|
---|
93 | data, data_size);
|
---|
94 |
|
---|
95 | return rc;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Generic wrapper for GET requests using standard control request format.
|
---|
99 | *
|
---|
100 | * @see usb_endpoint_pipe_control_read
|
---|
101 | *
|
---|
102 | * @param pipe Pipe used for the communication.
|
---|
103 | * @param request_type Request type (standard/class/vendor).
|
---|
104 | * @param recipient Request recipient (e.g. device or endpoint).
|
---|
105 | * @param request Actual request (e.g. GET_DESCRIPTOR).
|
---|
106 | * @param value Value of @c wValue field of setup packet
|
---|
107 | * (must be in USB endianness).
|
---|
108 | * @param index Value of @c wIndex field of setup packet
|
---|
109 | * (must be in USB endianness).
|
---|
110 | * @param data Buffer where to store data accepted during the DATA stage.
|
---|
111 | * (they will come in USB endianess).
|
---|
112 | * @param data_size Size of the @p data buffer
|
---|
113 | * (in native endianness).
|
---|
114 | * @param actual_data_size Actual size of transfered data
|
---|
115 | * (in native endianness).
|
---|
116 | * @return Error code.
|
---|
117 | * @retval EBADMEM @p pipe is NULL.
|
---|
118 | * @retval EBADMEM @p data is NULL and @p data_size is not zero.
|
---|
119 | * @retval ERANGE Data buffer too large.
|
---|
120 | */
|
---|
121 | int usb_control_request_get(usb_endpoint_pipe_t *pipe,
|
---|
122 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
123 | uint8_t request,
|
---|
124 | uint16_t value, uint16_t index,
|
---|
125 | void *data, size_t data_size, size_t *actual_data_size)
|
---|
126 | {
|
---|
127 | if (pipe == NULL) {
|
---|
128 | return EBADMEM;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (data_size > MAX_DATA_LENGTH) {
|
---|
132 | return ERANGE;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if ((data_size > 0) && (data == NULL)) {
|
---|
136 | return EBADMEM;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * TODO: check that @p request_type and @p recipient are
|
---|
141 | * within ranges.
|
---|
142 | */
|
---|
143 |
|
---|
144 | usb_device_request_setup_packet_t setup_packet;
|
---|
145 | setup_packet.request_type = 128 | (request_type << 5) | recipient;
|
---|
146 | setup_packet.request = request;
|
---|
147 | setup_packet.value = value;
|
---|
148 | setup_packet.index = index;
|
---|
149 | setup_packet.length = (uint16_t) data_size;
|
---|
150 |
|
---|
151 | int rc = usb_endpoint_pipe_control_read(pipe,
|
---|
152 | &setup_packet, sizeof(setup_packet),
|
---|
153 | data, data_size, actual_data_size);
|
---|
154 |
|
---|
155 | return rc;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Change address of connected device.
|
---|
159 | * This function automatically updates the backing connection to point to
|
---|
160 | * the new address.
|
---|
161 | *
|
---|
162 | * @see usb_drv_reserve_default_address
|
---|
163 | * @see usb_drv_release_default_address
|
---|
164 | * @see usb_drv_request_address
|
---|
165 | * @see usb_drv_release_address
|
---|
166 | * @see usb_drv_bind_address
|
---|
167 | *
|
---|
168 | * @param pipe Control endpoint pipe (session must be already started).
|
---|
169 | * @param new_address New USB address to be set (in native endianness).
|
---|
170 | * @return Error code.
|
---|
171 | */
|
---|
172 | int usb_request_set_address(usb_endpoint_pipe_t *pipe,
|
---|
173 | usb_address_t new_address)
|
---|
174 | {
|
---|
175 | if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
|
---|
176 | return EINVAL;
|
---|
177 | }
|
---|
178 |
|
---|
179 | uint16_t addr = uint16_host2usb((uint16_t)new_address);
|
---|
180 |
|
---|
181 | int rc = usb_control_request_set(pipe,
|
---|
182 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
183 | USB_DEVREQ_SET_ADDRESS,
|
---|
184 | addr, 0,
|
---|
185 | NULL, 0);
|
---|
186 |
|
---|
187 | if (rc != EOK) {
|
---|
188 | return rc;
|
---|
189 | }
|
---|
190 |
|
---|
191 | assert(pipe->wire != NULL);
|
---|
192 | /* TODO: prevent other from accessing wire now. */
|
---|
193 | pipe->wire->address = new_address;
|
---|
194 |
|
---|
195 | return EOK;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /** Retrieve USB descriptor of a USB device.
|
---|
199 | *
|
---|
200 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
201 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
202 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
203 | * @param[in] descriptor_index Descriptor index.
|
---|
204 | * @param[in] language Language index.
|
---|
205 | * @param[out] buffer Buffer where to store the retrieved descriptor.
|
---|
206 | * @param[in] size Size of the @p buffer.
|
---|
207 | * @param[out] actual_size Number of bytes actually transferred.
|
---|
208 | * @return Error code.
|
---|
209 | */
|
---|
210 | int usb_request_get_descriptor(usb_endpoint_pipe_t *pipe,
|
---|
211 | usb_request_type_t request_type,
|
---|
212 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
213 | uint16_t language,
|
---|
214 | void *buffer, size_t size, size_t *actual_size)
|
---|
215 | {
|
---|
216 | if (buffer == NULL) {
|
---|
217 | return EBADMEM;
|
---|
218 | }
|
---|
219 | if (size == 0) {
|
---|
220 | return EINVAL;
|
---|
221 | }
|
---|
222 |
|
---|
223 | uint16_t wValue = descriptor_index | (descriptor_type << 8);
|
---|
224 |
|
---|
225 | return usb_control_request_get(pipe,
|
---|
226 | request_type, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
227 | USB_DEVREQ_GET_DESCRIPTOR,
|
---|
228 | wValue, language,
|
---|
229 | buffer, size, actual_size);
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** Retrieve standard device descriptor of a USB device.
|
---|
233 | *
|
---|
234 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
235 | * @param[out] descriptor Storage for the device descriptor.
|
---|
236 | * @return Error code.
|
---|
237 | */
|
---|
238 | int usb_request_get_device_descriptor(usb_endpoint_pipe_t *pipe,
|
---|
239 | usb_standard_device_descriptor_t *descriptor)
|
---|
240 | {
|
---|
241 | if (descriptor == NULL) {
|
---|
242 | return EBADMEM;
|
---|
243 | }
|
---|
244 |
|
---|
245 | size_t actually_transferred = 0;
|
---|
246 | usb_standard_device_descriptor_t descriptor_tmp;
|
---|
247 | int rc = usb_request_get_descriptor(pipe,
|
---|
248 | USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_DEVICE,
|
---|
249 | 0, 0,
|
---|
250 | &descriptor_tmp, sizeof(descriptor_tmp),
|
---|
251 | &actually_transferred);
|
---|
252 |
|
---|
253 | if (rc != EOK) {
|
---|
254 | return rc;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /* Verify that all data has been transferred. */
|
---|
258 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
259 | return ELIMIT;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /* Everything is okay, copy the descriptor. */
|
---|
263 | memcpy(descriptor, &descriptor_tmp,
|
---|
264 | sizeof(descriptor_tmp));
|
---|
265 |
|
---|
266 | return EOK;
|
---|
267 | }
|
---|
268 |
|
---|
269 | /** Retrieve configuration descriptor of a USB device.
|
---|
270 | *
|
---|
271 | * The function does not retrieve additional data binded with configuration
|
---|
272 | * descriptor (such as its interface and endpoint descriptors) - use
|
---|
273 | * usb_request_get_full_configuration_descriptor() instead.
|
---|
274 | *
|
---|
275 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
276 | * @param[in] index Descriptor index.
|
---|
277 | * @param[out] descriptor Storage for the device descriptor.
|
---|
278 | * @return Error code.
|
---|
279 | */
|
---|
280 | int usb_request_get_bare_configuration_descriptor(usb_endpoint_pipe_t *pipe,
|
---|
281 | int index, usb_standard_configuration_descriptor_t *descriptor)
|
---|
282 | {
|
---|
283 | if (descriptor == NULL) {
|
---|
284 | return EBADMEM;
|
---|
285 | }
|
---|
286 |
|
---|
287 | if ((index < 0) || (index > 0xFF)) {
|
---|
288 | return ERANGE;
|
---|
289 | }
|
---|
290 |
|
---|
291 | size_t actually_transferred = 0;
|
---|
292 | usb_standard_configuration_descriptor_t descriptor_tmp;
|
---|
293 | int rc = usb_request_get_descriptor(pipe,
|
---|
294 | USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_CONFIGURATION,
|
---|
295 | index, 0,
|
---|
296 | &descriptor_tmp, sizeof(descriptor_tmp),
|
---|
297 | &actually_transferred);
|
---|
298 | if (rc != EOK) {
|
---|
299 | return rc;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /* Verify that all data has been transferred. */
|
---|
303 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
304 | return ELIMIT;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /* Everything is okay, copy the descriptor. */
|
---|
308 | memcpy(descriptor, &descriptor_tmp,
|
---|
309 | sizeof(descriptor_tmp));
|
---|
310 |
|
---|
311 | return EOK;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /** Retrieve full configuration descriptor of a USB device.
|
---|
315 | *
|
---|
316 | * @warning The @p buffer might be touched (i.e. its contents changed)
|
---|
317 | * even when error occurs.
|
---|
318 | *
|
---|
319 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
320 | * @param[in] index Descriptor index.
|
---|
321 | * @param[out] descriptor Storage for the device descriptor.
|
---|
322 | * @param[in] descriptor_size Size of @p descriptor buffer.
|
---|
323 | * @param[out] actual_size Number of bytes actually transferred.
|
---|
324 | * @return Error code.
|
---|
325 | */
|
---|
326 | int usb_request_get_full_configuration_descriptor(usb_endpoint_pipe_t *pipe,
|
---|
327 | int index, void *descriptor, size_t descriptor_size, size_t *actual_size)
|
---|
328 | {
|
---|
329 | if ((index < 0) || (index > 0xFF)) {
|
---|
330 | return ERANGE;
|
---|
331 | }
|
---|
332 |
|
---|
333 | return usb_request_get_descriptor(pipe,
|
---|
334 | USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_CONFIGURATION,
|
---|
335 | index, 0,
|
---|
336 | descriptor, descriptor_size, actual_size);
|
---|
337 | }
|
---|
338 |
|
---|
339 | /** Set configuration of USB device.
|
---|
340 | *
|
---|
341 | * @param pipe Control endpoint pipe (session must be already started).
|
---|
342 | * @param configuration_value New configuration value.
|
---|
343 | * @return Error code.
|
---|
344 | */
|
---|
345 | int usb_request_set_configuration(usb_endpoint_pipe_t *pipe,
|
---|
346 | uint8_t configuration_value)
|
---|
347 | {
|
---|
348 | uint16_t config_value
|
---|
349 | = uint16_host2usb((uint16_t) configuration_value);
|
---|
350 |
|
---|
351 | return usb_control_request_set(pipe,
|
---|
352 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
353 | USB_DEVREQ_SET_CONFIGURATION, config_value, 0,
|
---|
354 | NULL, 0);
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * @}
|
---|
359 | */
|
---|