source: mainline/uspace/lib/usbdev/src/request.c@ 01eeaaf

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

libusbdev: Fix comments.

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