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 libusbdev
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * Standard USB requests (implementation).
|
---|
34 | */
|
---|
35 | #include <usb/dev/request.h>
|
---|
36 | #include <usb/request.h>
|
---|
37 | #include <usb/usb.h>
|
---|
38 |
|
---|
39 | #include <errno.h>
|
---|
40 | #include <mem.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <str.h>
|
---|
43 |
|
---|
44 | #define MAX_DATA_LENGTH ((size_t)(0xFFFF))
|
---|
45 |
|
---|
46 | /** Generic wrapper for SET requests using standard control request format.
|
---|
47 | *
|
---|
48 | * @see usb_pipe_control_write
|
---|
49 | *
|
---|
50 | * @param pipe Pipe used for the communication.
|
---|
51 | * @param request_type Request type (standard/class/vendor).
|
---|
52 | * @param recipient Request recipient (e.g. device or endpoint).
|
---|
53 | * @param request Actual request (e.g. GET_DESCRIPTOR).
|
---|
54 | * @param value Value of @c wValue field of setup packet
|
---|
55 | * (must be in USB endianness).
|
---|
56 | * @param index Value of @c wIndex field of setup packet
|
---|
57 | * (must be in USB endianness).
|
---|
58 | * @param data Data to be sent during DATA stage
|
---|
59 | * (expected to be in USB endianness).
|
---|
60 | * @param data_size Size of the @p data buffer (in native endianness).
|
---|
61 | * @return Error code.
|
---|
62 | * @retval EBADMEM @p pipe is NULL.
|
---|
63 | * @retval EBADMEM @p data is NULL and @p data_size is not zero.
|
---|
64 | * @retval ERANGE Data buffer too large.
|
---|
65 | */
|
---|
66 | int usb_control_request_set(usb_pipe_t *pipe,
|
---|
67 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
68 | uint8_t request, uint16_t value, uint16_t index,
|
---|
69 | const void *data, size_t data_size)
|
---|
70 | {
|
---|
71 | if (pipe == NULL) {
|
---|
72 | return EBADMEM;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (data_size > MAX_DATA_LENGTH) {
|
---|
76 | return ERANGE;
|
---|
77 | }
|
---|
78 |
|
---|
79 | if ((data_size > 0) && (data == NULL)) {
|
---|
80 | return EBADMEM;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * TODO: check that @p request_type and @p recipient are
|
---|
85 | * within ranges.
|
---|
86 | */
|
---|
87 |
|
---|
88 | const usb_device_request_setup_packet_t setup_packet = {
|
---|
89 | .request_type = (request_type << 5) | recipient,
|
---|
90 | .request = request,
|
---|
91 | .value = value,
|
---|
92 | .index = index,
|
---|
93 | .length = (uint16_t) data_size,
|
---|
94 | };
|
---|
95 |
|
---|
96 | return usb_pipe_control_write(pipe,
|
---|
97 | &setup_packet, sizeof(setup_packet), data, data_size);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Generic wrapper for GET requests using standard control request format.
|
---|
101 | *
|
---|
102 | * @see usb_pipe_control_read
|
---|
103 | *
|
---|
104 | * @param pipe Pipe used for the communication.
|
---|
105 | * @param request_type Request type (standard/class/vendor).
|
---|
106 | * @param recipient Request recipient (e.g. device or endpoint).
|
---|
107 | * @param request Actual request (e.g. GET_DESCRIPTOR).
|
---|
108 | * @param value Value of @c wValue field of setup packet
|
---|
109 | * (must be in USB endianness).
|
---|
110 | * @param index Value of @c wIndex field of setup packet
|
---|
111 | * (must be in USB endianness).
|
---|
112 | * @param data Buffer where to store data accepted during the DATA stage.
|
---|
113 | * (they will come in USB endianness).
|
---|
114 | * @param data_size Size of the @p data buffer
|
---|
115 | * (in native endianness).
|
---|
116 | * @param actual_data_size Actual size of transfered data
|
---|
117 | * (in native endianness).
|
---|
118 | * @return Error code.
|
---|
119 | * @retval EBADMEM @p pipe is NULL.
|
---|
120 | * @retval EBADMEM @p data is NULL and @p data_size is not zero.
|
---|
121 | * @retval ERANGE Data buffer too large.
|
---|
122 | */
|
---|
123 | int usb_control_request_get(usb_pipe_t *pipe,
|
---|
124 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
125 | uint8_t request, uint16_t value, uint16_t index,
|
---|
126 | void *data, size_t data_size, size_t *actual_data_size)
|
---|
127 | {
|
---|
128 | if (pipe == NULL) {
|
---|
129 | return EBADMEM;
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (data_size > MAX_DATA_LENGTH) {
|
---|
133 | return ERANGE;
|
---|
134 | }
|
---|
135 |
|
---|
136 | if ((data_size > 0) && (data == NULL)) {
|
---|
137 | return EBADMEM;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * TODO: check that @p request_type and @p recipient are
|
---|
142 | * within ranges.
|
---|
143 | */
|
---|
144 |
|
---|
145 | const usb_device_request_setup_packet_t setup_packet = {
|
---|
146 | .request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST
|
---|
147 | | (request_type << 5) | recipient,
|
---|
148 | .request = request,
|
---|
149 | .value = uint16_host2usb(value),
|
---|
150 | .index = uint16_host2usb(index),
|
---|
151 | .length = uint16_host2usb(data_size),
|
---|
152 | };
|
---|
153 |
|
---|
154 | return usb_pipe_control_read(pipe, &setup_packet, sizeof(setup_packet),
|
---|
155 | data, data_size, actual_data_size);
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Retrieve status of a USB device.
|
---|
159 | *
|
---|
160 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
161 | * @param[in] index Recipient index (in native endianness).
|
---|
162 | * @param[in] recipient Recipient of the GET_STATUS request.
|
---|
163 | * @param[out] status Recipient status (in native endianness).
|
---|
164 | * @return Error code.
|
---|
165 | */
|
---|
166 | int usb_request_get_status(usb_pipe_t *pipe,
|
---|
167 | usb_request_recipient_t recipient, uint16_t index,
|
---|
168 | uint16_t *status)
|
---|
169 | {
|
---|
170 | if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) {
|
---|
171 | return EINVAL;
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (status == NULL) {
|
---|
175 | return EBADMEM;
|
---|
176 | }
|
---|
177 |
|
---|
178 | uint16_t status_usb_endianess;
|
---|
179 | size_t data_transfered_size;
|
---|
180 | int rc = usb_control_request_get(pipe, USB_REQUEST_TYPE_STANDARD,
|
---|
181 | recipient, USB_DEVREQ_GET_STATUS, 0, uint16_host2usb(index),
|
---|
182 | &status_usb_endianess, 2, &data_transfered_size);
|
---|
183 | if (rc != EOK) {
|
---|
184 | return rc;
|
---|
185 | }
|
---|
186 | if (data_transfered_size != 2) {
|
---|
187 | return ELIMIT;
|
---|
188 | }
|
---|
189 |
|
---|
190 | *status = uint16_usb2host(status_usb_endianess);
|
---|
191 |
|
---|
192 | return EOK;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /** Clear or disable specific device feature.
|
---|
196 | *
|
---|
197 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
198 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
199 | * @param[in] recipient Recipient of the CLEAR_FEATURE request.
|
---|
200 | * @param[in] feature_selector Feature selector (in native endianness).
|
---|
201 | * @param[in] index Recipient index (in native endianness).
|
---|
202 | * @return Error code.
|
---|
203 | */
|
---|
204 | int usb_request_clear_feature(usb_pipe_t *pipe,
|
---|
205 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
206 | uint16_t feature_selector, uint16_t index)
|
---|
207 | {
|
---|
208 | if (request_type == USB_REQUEST_TYPE_STANDARD) {
|
---|
209 | if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0))
|
---|
210 | {
|
---|
211 | return EINVAL;
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | return usb_control_request_set(pipe,
|
---|
216 | request_type, recipient, USB_DEVREQ_CLEAR_FEATURE,
|
---|
217 | uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0);
|
---|
218 | }
|
---|
219 |
|
---|
220 | /** Set or enable specific device feature.
|
---|
221 | *
|
---|
222 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
223 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
224 | * @param[in] recipient Recipient of the SET_FEATURE request.
|
---|
225 | * @param[in] feature_selector Feature selector (in native endianness).
|
---|
226 | * @param[in] index Recipient index (in native endianness).
|
---|
227 | * @return Error code.
|
---|
228 | */
|
---|
229 | int usb_request_set_feature(usb_pipe_t *pipe,
|
---|
230 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
231 | uint16_t feature_selector, uint16_t index)
|
---|
232 | {
|
---|
233 | if (request_type == USB_REQUEST_TYPE_STANDARD) {
|
---|
234 | if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0))
|
---|
235 | {
|
---|
236 | return EINVAL;
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | return usb_control_request_set(pipe,
|
---|
241 | request_type, recipient, USB_DEVREQ_SET_FEATURE,
|
---|
242 | uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0);
|
---|
243 | }
|
---|
244 |
|
---|
245 | /** Retrieve USB descriptor of a USB device.
|
---|
246 | *
|
---|
247 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
248 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
249 | * @param[in] recipient Request recipient (device/interface/endpoint).
|
---|
250 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
251 | * @param[in] descriptor_index Descriptor index.
|
---|
252 | * @param[in] language Language index.
|
---|
253 | * @param[out] buffer Buffer where to store the retrieved descriptor.
|
---|
254 | * @param[in] size Size of the @p buffer.
|
---|
255 | * @param[out] actual_size Number of bytes actually transferred.
|
---|
256 | * @return Error code.
|
---|
257 | */
|
---|
258 | int usb_request_get_descriptor(usb_pipe_t *pipe,
|
---|
259 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
260 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
261 | uint16_t language,
|
---|
262 | void *buffer, size_t size, size_t *actual_size)
|
---|
263 | {
|
---|
264 | if (buffer == NULL) {
|
---|
265 | return EBADMEM;
|
---|
266 | }
|
---|
267 | if (size == 0) {
|
---|
268 | return EINVAL;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /* The wValue field specifies the descriptor type in the high byte
|
---|
272 | * and the descriptor index in the low byte. USB 1.1 spec p. 189
|
---|
273 | */
|
---|
274 | const uint16_t wValue = descriptor_index | (descriptor_type << 8);
|
---|
275 |
|
---|
276 | return usb_control_request_get(pipe,
|
---|
277 | request_type, recipient,
|
---|
278 | USB_DEVREQ_GET_DESCRIPTOR,
|
---|
279 | wValue, language,
|
---|
280 | buffer, size, actual_size);
|
---|
281 | }
|
---|
282 |
|
---|
283 | /** Retrieve USB descriptor, allocate space for it.
|
---|
284 | *
|
---|
285 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
286 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
287 | * @param[in] recipient Request recipient (device/interface/endpoint).
|
---|
288 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
289 | * @param[in] descriptor_index Descriptor index.
|
---|
290 | * @param[in] language Language index.
|
---|
291 | * @param[out] buffer_ptr Where to store pointer to allocated buffer.
|
---|
292 | * @param[out] buffer_size Where to store the size of the descriptor.
|
---|
293 | * @return
|
---|
294 | */
|
---|
295 | int usb_request_get_descriptor_alloc(usb_pipe_t * pipe,
|
---|
296 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
297 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
298 | uint16_t language,
|
---|
299 | void **buffer_ptr, size_t *buffer_size)
|
---|
300 | {
|
---|
301 | if (buffer_ptr == NULL) {
|
---|
302 | return EBADMEM;
|
---|
303 | }
|
---|
304 |
|
---|
305 | int rc;
|
---|
306 |
|
---|
307 | /*
|
---|
308 | * Get only first byte to retrieve descriptor length.
|
---|
309 | */
|
---|
310 | uint8_t tmp_buffer;
|
---|
311 | size_t bytes_transfered;
|
---|
312 | rc = usb_request_get_descriptor(pipe, request_type, recipient,
|
---|
313 | descriptor_type, descriptor_index, language,
|
---|
314 | &tmp_buffer, sizeof(tmp_buffer), &bytes_transfered);
|
---|
315 | if (rc != EOK) {
|
---|
316 | return rc;
|
---|
317 | }
|
---|
318 | if (bytes_transfered != 1) {
|
---|
319 | return ELIMIT;
|
---|
320 | }
|
---|
321 |
|
---|
322 | const size_t size = tmp_buffer;
|
---|
323 | if (size == 0) {
|
---|
324 | return ELIMIT;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /*
|
---|
328 | * Allocate buffer and get the descriptor again.
|
---|
329 | */
|
---|
330 | void *buffer = malloc(size);
|
---|
331 | if (buffer == NULL) {
|
---|
332 | return ENOMEM;
|
---|
333 | }
|
---|
334 |
|
---|
335 | rc = usb_request_get_descriptor(pipe, request_type, recipient,
|
---|
336 | descriptor_type, descriptor_index, language,
|
---|
337 | buffer, size, &bytes_transfered);
|
---|
338 | if (rc != EOK) {
|
---|
339 | free(buffer);
|
---|
340 | return rc;
|
---|
341 | }
|
---|
342 | if (bytes_transfered != size) {
|
---|
343 | free(buffer);
|
---|
344 | return ELIMIT;
|
---|
345 | }
|
---|
346 |
|
---|
347 | *buffer_ptr = buffer;
|
---|
348 | if (buffer_size != NULL) {
|
---|
349 | *buffer_size = size;
|
---|
350 | }
|
---|
351 |
|
---|
352 | return EOK;
|
---|
353 | }
|
---|
354 |
|
---|
355 | /** Retrieve standard device descriptor of a USB device.
|
---|
356 | *
|
---|
357 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
358 | * @param[out] descriptor Storage for the device descriptor.
|
---|
359 | * @return Error code.
|
---|
360 | */
|
---|
361 | int usb_request_get_device_descriptor(usb_pipe_t *pipe,
|
---|
362 | usb_standard_device_descriptor_t *descriptor)
|
---|
363 | {
|
---|
364 | if (descriptor == NULL) {
|
---|
365 | return EBADMEM;
|
---|
366 | }
|
---|
367 |
|
---|
368 | size_t actually_transferred = 0;
|
---|
369 | usb_standard_device_descriptor_t descriptor_tmp;
|
---|
370 | int rc = usb_request_get_descriptor(pipe,
|
---|
371 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
372 | USB_DESCTYPE_DEVICE, 0, 0, &descriptor_tmp, sizeof(descriptor_tmp),
|
---|
373 | &actually_transferred);
|
---|
374 |
|
---|
375 | if (rc != EOK) {
|
---|
376 | return rc;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /* Verify that all data has been transferred. */
|
---|
380 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
381 | return ELIMIT;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /* Everything is okay, copy the descriptor. */
|
---|
385 | memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
|
---|
386 |
|
---|
387 | return EOK;
|
---|
388 | }
|
---|
389 |
|
---|
390 | /** Retrieve configuration descriptor of a USB device.
|
---|
391 | *
|
---|
392 | * The function does not retrieve additional data binded with configuration
|
---|
393 | * descriptor (such as its interface and endpoint descriptors) - use
|
---|
394 | * usb_request_get_full_configuration_descriptor() instead.
|
---|
395 | *
|
---|
396 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
397 | * @param[in] index Descriptor index.
|
---|
398 | * @param[out] descriptor Storage for the device descriptor.
|
---|
399 | * @return Error code.
|
---|
400 | */
|
---|
401 | int usb_request_get_bare_configuration_descriptor(usb_pipe_t *pipe,
|
---|
402 | int index, usb_standard_configuration_descriptor_t *descriptor)
|
---|
403 | {
|
---|
404 | if (descriptor == NULL) {
|
---|
405 | return EBADMEM;
|
---|
406 | }
|
---|
407 |
|
---|
408 | if ((index < 0) || (index > 0xFF)) {
|
---|
409 | return ERANGE;
|
---|
410 | }
|
---|
411 |
|
---|
412 | size_t actually_transferred = 0;
|
---|
413 | usb_standard_configuration_descriptor_t descriptor_tmp;
|
---|
414 | const int rc = usb_request_get_descriptor(pipe,
|
---|
415 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
416 | USB_DESCTYPE_CONFIGURATION, index, 0,
|
---|
417 | &descriptor_tmp, sizeof(descriptor_tmp),
|
---|
418 | &actually_transferred);
|
---|
419 | if (rc != EOK) {
|
---|
420 | return rc;
|
---|
421 | }
|
---|
422 |
|
---|
423 | /* Verify that all data has been transferred. */
|
---|
424 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
425 | return ELIMIT;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /* Everything is okay, copy the descriptor. */
|
---|
429 | memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
|
---|
430 | return EOK;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /** Retrieve full configuration descriptor of a USB device.
|
---|
434 | *
|
---|
435 | * @warning The @p buffer might be touched (i.e. its contents changed)
|
---|
436 | * even when error occurs.
|
---|
437 | *
|
---|
438 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
439 | * @param[in] index Descriptor index.
|
---|
440 | * @param[out] descriptor Storage for the device descriptor.
|
---|
441 | * @param[in] descriptor_size Size of @p descriptor buffer.
|
---|
442 | * @param[out] actual_size Number of bytes actually transferred.
|
---|
443 | * @return Error code.
|
---|
444 | */
|
---|
445 | int usb_request_get_full_configuration_descriptor(usb_pipe_t *pipe,
|
---|
446 | int index, void *descriptor, size_t descriptor_size, size_t *actual_size)
|
---|
447 | {
|
---|
448 | if ((index < 0) || (index > 0xFF)) {
|
---|
449 | return ERANGE;
|
---|
450 | }
|
---|
451 |
|
---|
452 | return usb_request_get_descriptor(pipe,
|
---|
453 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
454 | USB_DESCTYPE_CONFIGURATION, index, 0,
|
---|
455 | descriptor, descriptor_size, actual_size);
|
---|
456 | }
|
---|
457 |
|
---|
458 | /** Retrieve full configuration descriptor, allocate space for it.
|
---|
459 | *
|
---|
460 | * The function takes care that full configuration descriptor is returned
|
---|
461 | * (i.e. the function will fail when less data then descriptor.totalLength
|
---|
462 | * is returned).
|
---|
463 | *
|
---|
464 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
465 | * @param[in] index Configuration index.
|
---|
466 | * @param[out] descriptor_ptr Where to store pointer to allocated buffer.
|
---|
467 | * @param[out] descriptor_size Where to store the size of the descriptor.
|
---|
468 | * @return Error code.
|
---|
469 | */
|
---|
470 | int usb_request_get_full_configuration_descriptor_alloc(
|
---|
471 | usb_pipe_t *pipe, int index,
|
---|
472 | const void **descriptor_ptr, size_t *descriptor_size)
|
---|
473 | {
|
---|
474 | int rc;
|
---|
475 |
|
---|
476 | if (descriptor_ptr == NULL) {
|
---|
477 | return EBADMEM;
|
---|
478 | }
|
---|
479 |
|
---|
480 | usb_standard_configuration_descriptor_t bare_config;
|
---|
481 | rc = usb_request_get_bare_configuration_descriptor(pipe, index,
|
---|
482 | &bare_config);
|
---|
483 | if (rc != EOK) {
|
---|
484 | return rc;
|
---|
485 | }
|
---|
486 | if (bare_config.descriptor_type != USB_DESCTYPE_CONFIGURATION) {
|
---|
487 | return ENOENT;
|
---|
488 | }
|
---|
489 |
|
---|
490 | const size_t total_length = uint16_usb2host(bare_config.total_length);
|
---|
491 | if (total_length < sizeof(bare_config)) {
|
---|
492 | return ELIMIT;
|
---|
493 | }
|
---|
494 |
|
---|
495 | void *buffer = malloc(total_length);
|
---|
496 | if (buffer == NULL) {
|
---|
497 | return ENOMEM;
|
---|
498 | }
|
---|
499 |
|
---|
500 | size_t transferred = 0;
|
---|
501 | rc = usb_request_get_full_configuration_descriptor(pipe, index,
|
---|
502 | buffer, total_length, &transferred);
|
---|
503 | if (rc != EOK) {
|
---|
504 | free(buffer);
|
---|
505 | return rc;
|
---|
506 | }
|
---|
507 |
|
---|
508 | if (transferred != total_length) {
|
---|
509 | free(buffer);
|
---|
510 | return ELIMIT;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /* Everything looks okay, copy the pointers. */
|
---|
514 |
|
---|
515 | *descriptor_ptr = buffer;
|
---|
516 |
|
---|
517 | if (descriptor_size != NULL) {
|
---|
518 | *descriptor_size = total_length;
|
---|
519 | }
|
---|
520 |
|
---|
521 | return EOK;
|
---|
522 | }
|
---|
523 |
|
---|
524 | /** Update existing or add new USB descriptor to a USB device.
|
---|
525 | *
|
---|
526 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
527 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
528 | * @param[in] recipient Request recipient (device/interface/endpoint).
|
---|
529 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
530 | * @param[in] descriptor_index Descriptor index.
|
---|
531 | * @param[in] language Language index (in native endianness).
|
---|
532 | * @param[in] buffer Buffer with the new descriptor (in USB endianness).
|
---|
533 | * @param[in] size Size of the @p buffer in bytes (in native endianness).
|
---|
534 | * @return Error code.
|
---|
535 | */
|
---|
536 | int usb_request_set_descriptor(usb_pipe_t *pipe,
|
---|
537 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
538 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
539 | uint16_t language, const void *buffer, size_t size)
|
---|
540 | {
|
---|
541 | if (buffer == NULL) {
|
---|
542 | return EBADMEM;
|
---|
543 | }
|
---|
544 | if (size == 0) {
|
---|
545 | return EINVAL;
|
---|
546 | }
|
---|
547 |
|
---|
548 | /* FIXME: proper endianness. */
|
---|
549 | uint16_t wValue = descriptor_index | (descriptor_type << 8);
|
---|
550 |
|
---|
551 | return usb_control_request_set(pipe,
|
---|
552 | request_type, recipient, USB_DEVREQ_SET_DESCRIPTOR,
|
---|
553 | wValue, language, buffer, size);
|
---|
554 | }
|
---|
555 |
|
---|
556 | /** Get current configuration value of USB device.
|
---|
557 | *
|
---|
558 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
559 | * @param[out] configuration_value Current configuration value.
|
---|
560 | * @return Error code.
|
---|
561 | */
|
---|
562 | int usb_request_get_configuration(usb_pipe_t *pipe,
|
---|
563 | uint8_t *configuration_value)
|
---|
564 | {
|
---|
565 | uint8_t value;
|
---|
566 | size_t actual_size;
|
---|
567 |
|
---|
568 | const int rc = usb_control_request_get(pipe,
|
---|
569 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
570 | USB_DEVREQ_GET_CONFIGURATION, 0, 0, &value, 1, &actual_size);
|
---|
571 |
|
---|
572 | if (rc != EOK) {
|
---|
573 | return rc;
|
---|
574 | }
|
---|
575 | if (actual_size != 1) {
|
---|
576 | return ELIMIT;
|
---|
577 | }
|
---|
578 |
|
---|
579 | if (configuration_value != NULL) {
|
---|
580 | *configuration_value = value;
|
---|
581 | }
|
---|
582 |
|
---|
583 | return EOK;
|
---|
584 | }
|
---|
585 |
|
---|
586 | /** Set configuration of USB device.
|
---|
587 | *
|
---|
588 | * @param pipe Control endpoint pipe (session must be already started).
|
---|
589 | * @param configuration_value New configuration value.
|
---|
590 | * @return Error code.
|
---|
591 | */
|
---|
592 | int usb_request_set_configuration(usb_pipe_t *pipe,
|
---|
593 | uint8_t configuration_value)
|
---|
594 | {
|
---|
595 | const uint16_t config_value
|
---|
596 | = uint16_host2usb((uint16_t) configuration_value);
|
---|
597 |
|
---|
598 | return usb_control_request_set(pipe,
|
---|
599 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
600 | USB_DEVREQ_SET_CONFIGURATION, config_value, 0,
|
---|
601 | NULL, 0);
|
---|
602 | }
|
---|
603 |
|
---|
604 | /** Get selected alternate setting for USB interface.
|
---|
605 | *
|
---|
606 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
607 | * @param[in] interface_index Interface index.
|
---|
608 | * @param[out] alternate_setting Alternate setting for the interface.
|
---|
609 | * @return Error code.
|
---|
610 | */
|
---|
611 | int usb_request_get_interface(usb_pipe_t *pipe,
|
---|
612 | uint8_t interface_index, uint8_t *alternate_setting)
|
---|
613 | {
|
---|
614 | uint8_t value;
|
---|
615 | size_t actual_size;
|
---|
616 |
|
---|
617 | const int rc = usb_control_request_get(pipe,
|
---|
618 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
|
---|
619 | USB_DEVREQ_GET_INTERFACE,
|
---|
620 | 0, uint16_host2usb((uint16_t) interface_index),
|
---|
621 | &value, sizeof(value), &actual_size);
|
---|
622 |
|
---|
623 | if (rc != EOK) {
|
---|
624 | return rc;
|
---|
625 | }
|
---|
626 | if (actual_size != 1) {
|
---|
627 | return ELIMIT;
|
---|
628 | }
|
---|
629 |
|
---|
630 | if (alternate_setting != NULL) {
|
---|
631 | *alternate_setting = value;
|
---|
632 | }
|
---|
633 |
|
---|
634 | return EOK;
|
---|
635 | }
|
---|
636 |
|
---|
637 | /** Select alternate setting for USB interface.
|
---|
638 | *
|
---|
639 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
640 | * @param[in] interface_index Interface index.
|
---|
641 | * @param[in] alternate_setting Alternate setting to select.
|
---|
642 | * @return Error code.
|
---|
643 | */
|
---|
644 | int usb_request_set_interface(usb_pipe_t *pipe,
|
---|
645 | uint8_t interface_index, uint8_t alternate_setting)
|
---|
646 | {
|
---|
647 | return usb_control_request_set(pipe,
|
---|
648 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
|
---|
649 | USB_DEVREQ_SET_INTERFACE,
|
---|
650 | uint16_host2usb((uint16_t) alternate_setting),
|
---|
651 | uint16_host2usb((uint16_t) interface_index),
|
---|
652 | NULL, 0);
|
---|
653 | }
|
---|
654 |
|
---|
655 | /** Get list of supported languages by USB device.
|
---|
656 | *
|
---|
657 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
658 | * @param[out] languages_ptr Where to store pointer to allocated array of
|
---|
659 | * supported languages.
|
---|
660 | * @param[out] languages_count Number of supported languages.
|
---|
661 | * @return Error code.
|
---|
662 | */
|
---|
663 | int usb_request_get_supported_languages(usb_pipe_t *pipe,
|
---|
664 | l18_win_locales_t **languages_ptr, size_t *languages_count)
|
---|
665 | {
|
---|
666 | if (languages_ptr == NULL || languages_count == NULL) {
|
---|
667 | return EBADMEM;
|
---|
668 | }
|
---|
669 |
|
---|
670 | uint8_t *string_descriptor = NULL;
|
---|
671 | size_t string_descriptor_size = 0;
|
---|
672 | const int rc = usb_request_get_descriptor_alloc(pipe,
|
---|
673 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
674 | USB_DESCTYPE_STRING, 0, 0,
|
---|
675 | (void **) &string_descriptor, &string_descriptor_size);
|
---|
676 | if (rc != EOK) {
|
---|
677 | return rc;
|
---|
678 | }
|
---|
679 | if (string_descriptor_size <= 2) {
|
---|
680 | free(string_descriptor);
|
---|
681 | return EEMPTY;
|
---|
682 | }
|
---|
683 | /* Subtract first 2 bytes (length and descriptor type). */
|
---|
684 | string_descriptor_size -= 2;
|
---|
685 |
|
---|
686 | /* Odd number of bytes - descriptor is broken? */
|
---|
687 | if ((string_descriptor_size % 2) != 0) {
|
---|
688 | /* FIXME: shall we return with error or silently ignore? */
|
---|
689 | free(string_descriptor);
|
---|
690 | return ESTALL;
|
---|
691 | }
|
---|
692 |
|
---|
693 | const size_t langs_count = string_descriptor_size / 2;
|
---|
694 | l18_win_locales_t *langs =
|
---|
695 | calloc(langs_count, sizeof(l18_win_locales_t));
|
---|
696 | if (langs == NULL) {
|
---|
697 | free(string_descriptor);
|
---|
698 | return ENOMEM;
|
---|
699 | }
|
---|
700 |
|
---|
701 | for (size_t i = 0; i < langs_count; i++) {
|
---|
702 | /* Language code from the descriptor is in USB endianness. */
|
---|
703 | /* FIXME: is this really correct? */
|
---|
704 | const uint16_t lang_code =
|
---|
705 | (string_descriptor[2 + 2 * i + 1] << 8)
|
---|
706 | + string_descriptor[2 + 2 * i];
|
---|
707 | langs[i] = uint16_usb2host(lang_code);
|
---|
708 | }
|
---|
709 |
|
---|
710 | free(string_descriptor);
|
---|
711 |
|
---|
712 | *languages_ptr = langs;
|
---|
713 | *languages_count =langs_count;
|
---|
714 |
|
---|
715 | return EOK;
|
---|
716 | }
|
---|
717 |
|
---|
718 | /** Get string (descriptor) from USB device.
|
---|
719 | *
|
---|
720 | * The string is returned in native encoding of the operating system.
|
---|
721 | * For HelenOS, that is UTF-8.
|
---|
722 | *
|
---|
723 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
724 | * @param[in] index String index (in native endianness),
|
---|
725 | * first index has number 1 (index from descriptors can be used directly).
|
---|
726 | * @param[in] lang String language (in native endianness).
|
---|
727 | * @param[out] string_ptr Where to store allocated string in native encoding.
|
---|
728 | * @return Error code.
|
---|
729 | */
|
---|
730 | int usb_request_get_string(usb_pipe_t *pipe,
|
---|
731 | size_t index, l18_win_locales_t lang, char **string_ptr)
|
---|
732 | {
|
---|
733 | if (string_ptr == NULL) {
|
---|
734 | return EBADMEM;
|
---|
735 | }
|
---|
736 | /*
|
---|
737 | * Index is actually one byte value and zero index is used
|
---|
738 | * to retrieve list of supported languages.
|
---|
739 | */
|
---|
740 | if ((index < 1) || (index > 0xFF)) {
|
---|
741 | return ERANGE;
|
---|
742 | }
|
---|
743 | /* Language is actually two byte value. */
|
---|
744 | if (lang > L18N_WIN_LOCALE_MAX) {
|
---|
745 | return ERANGE;
|
---|
746 | }
|
---|
747 |
|
---|
748 | int rc;
|
---|
749 |
|
---|
750 | /* Prepare dynamically allocated variables. */
|
---|
751 | uint8_t *string = NULL;
|
---|
752 | wchar_t *string_chars = NULL;
|
---|
753 |
|
---|
754 | /* Get the actual descriptor. */
|
---|
755 | size_t string_size;
|
---|
756 | rc = usb_request_get_descriptor_alloc(pipe,
|
---|
757 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
758 | USB_DESCTYPE_STRING, index, uint16_host2usb(lang),
|
---|
759 | (void **) &string, &string_size);
|
---|
760 | if (rc != EOK) {
|
---|
761 | goto leave;
|
---|
762 | }
|
---|
763 |
|
---|
764 | if (string_size <= 2) {
|
---|
765 | rc = EEMPTY;
|
---|
766 | goto leave;
|
---|
767 | }
|
---|
768 | /* Subtract first 2 bytes (length and descriptor type). */
|
---|
769 | string_size -= 2;
|
---|
770 |
|
---|
771 | /* Odd number of bytes - descriptor is broken? */
|
---|
772 | if ((string_size % 2) != 0) {
|
---|
773 | /* FIXME: shall we return with error or silently ignore? */
|
---|
774 | rc = ESTALL;
|
---|
775 | goto leave;
|
---|
776 | }
|
---|
777 |
|
---|
778 | const size_t string_char_count = string_size / 2;
|
---|
779 | string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1));
|
---|
780 | if (string_chars == NULL) {
|
---|
781 | rc = ENOMEM;
|
---|
782 | goto leave;
|
---|
783 | }
|
---|
784 |
|
---|
785 | /*
|
---|
786 | * Build a wide string.
|
---|
787 | * And do not forget to set NULL terminator (string descriptors
|
---|
788 | * do not have them).
|
---|
789 | */
|
---|
790 | for (size_t i = 0; i < string_char_count; i++) {
|
---|
791 | const uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
|
---|
792 | + string[2 + 2 * i];
|
---|
793 | string_chars[i] = uni_char;
|
---|
794 | }
|
---|
795 | string_chars[string_char_count] = 0;
|
---|
796 |
|
---|
797 |
|
---|
798 | /* Convert to normal string. */
|
---|
799 | char *str = wstr_to_astr(string_chars);
|
---|
800 | if (str == NULL) {
|
---|
801 | rc = ENOMEM;
|
---|
802 | goto leave;
|
---|
803 | }
|
---|
804 |
|
---|
805 | *string_ptr = str;
|
---|
806 | rc = EOK;
|
---|
807 |
|
---|
808 | leave:
|
---|
809 | free(string);
|
---|
810 | free(string_chars);
|
---|
811 |
|
---|
812 | return rc;
|
---|
813 | }
|
---|
814 |
|
---|
815 | /** Clear halt bit of an endpoint pipe (after pipe stall).
|
---|
816 | *
|
---|
817 | * @param pipe Control pipe.
|
---|
818 | * @param ep_index Endpoint index (in native endianness).
|
---|
819 | * @return Error code.
|
---|
820 | */
|
---|
821 | int usb_request_clear_endpoint_halt(usb_pipe_t *pipe, uint16_t ep_index)
|
---|
822 | {
|
---|
823 | return usb_request_clear_feature(pipe,
|
---|
824 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT,
|
---|
825 | uint16_host2usb(USB_FEATURE_SELECTOR_ENDPOINT_HALT),
|
---|
826 | uint16_host2usb(ep_index));
|
---|
827 | }
|
---|
828 |
|
---|
829 | /** Clear halt bit of an endpoint pipe (after pipe stall).
|
---|
830 | *
|
---|
831 | * @param ctrl_pipe Control pipe.
|
---|
832 | * @param target_pipe Which pipe is halted and shall be cleared.
|
---|
833 | * @return Error code.
|
---|
834 | */
|
---|
835 | int usb_pipe_clear_halt(usb_pipe_t *ctrl_pipe, usb_pipe_t *target_pipe)
|
---|
836 | {
|
---|
837 | if ((ctrl_pipe == NULL) || (target_pipe == NULL)) {
|
---|
838 | return EINVAL;
|
---|
839 | }
|
---|
840 | return usb_request_clear_endpoint_halt(ctrl_pipe,
|
---|
841 | target_pipe->endpoint_no);
|
---|
842 | }
|
---|
843 |
|
---|
844 | /** Get endpoint status.
|
---|
845 | *
|
---|
846 | * @param[in] ctrl_pipe Control pipe.
|
---|
847 | * @param[in] pipe Of which pipe the status shall be received.
|
---|
848 | * @param[out] status Where to store pipe status (in native endianness).
|
---|
849 | * @return Error code.
|
---|
850 | */
|
---|
851 | int usb_request_get_endpoint_status(usb_pipe_t *ctrl_pipe, usb_pipe_t *pipe,
|
---|
852 | uint16_t *status)
|
---|
853 | {
|
---|
854 | uint16_t status_tmp;
|
---|
855 | uint16_t pipe_index = (uint16_t) pipe->endpoint_no;
|
---|
856 | int rc = usb_request_get_status(ctrl_pipe,
|
---|
857 | USB_REQUEST_RECIPIENT_ENDPOINT, uint16_host2usb(pipe_index),
|
---|
858 | &status_tmp);
|
---|
859 | if (rc != EOK) {
|
---|
860 | return rc;
|
---|
861 | }
|
---|
862 |
|
---|
863 | if (status != NULL) {
|
---|
864 | *status = uint16_usb2host(status_tmp);
|
---|
865 | }
|
---|
866 |
|
---|
867 | return EOK;
|
---|
868 | }
|
---|
869 |
|
---|
870 | /**
|
---|
871 | * @}
|
---|
872 | */
|
---|