source: mainline/uspace/lib/usbdev/src/request.c@ 7f9d97f3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7f9d97f3 was 12e689f, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

libusbdev: Do not modify retrieved descriptors, compute required value explicitly.

There might be more such values and we won't convert all.

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