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

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

libusbdev: Make usb_request_set_address private.

This function causes to much trouble to be a part of the library interface.
Anyone crazy enough to change device address should add a separate wrapper
and solve all the problems it causes.

usb_request_set_address takes care of endpoint registration now.

  • 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 usb_device_request_setup_packet_t setup_packet;
146 setup_packet.request_type = 128 | (request_type << 5) | recipient;
147 setup_packet.request = request;
148 setup_packet.value = value;
149 setup_packet.index = index;
150 setup_packet.length = (uint16_t) data_size;
151
152 int rc = usb_pipe_control_read(pipe,
153 &setup_packet, sizeof(setup_packet),
154 data, data_size, actual_data_size);
155
156 return rc;
157}
158
159/** Retrieve status of a USB device.
160 *
161 * @param[in] pipe Control endpoint pipe (session must be already started).
162 * @param[in] index Recipient index (in native endianness).
163 * @param[in] recipient Recipient of the GET_STATUS request.
164 * @param[out] status Recipient status (in native endianness).
165 * @return Error code.
166 */
167int usb_request_get_status(usb_pipe_t *pipe,
168 usb_request_recipient_t recipient, uint16_t index,
169 uint16_t *status)
170{
171 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) {
172 return EINVAL;
173 }
174
175 if (status == NULL) {
176 return EBADMEM;
177 }
178
179 uint16_t status_usb_endianess;
180 size_t data_transfered_size;
181 int rc = usb_control_request_get(pipe, USB_REQUEST_TYPE_STANDARD,
182 recipient, USB_DEVREQ_GET_STATUS, 0, uint16_host2usb(index),
183 &status_usb_endianess, 2, &data_transfered_size);
184 if (rc != EOK) {
185 return rc;
186 }
187 if (data_transfered_size != 2) {
188 return ELIMIT;
189 }
190
191 *status = uint16_usb2host(status_usb_endianess);
192
193 return EOK;
194}
195
196/** Clear or disable specific device feature.
197 *
198 * @param[in] pipe Control endpoint pipe (session must be already started).
199 * @param[in] request_type Request type (standard/class/vendor).
200 * @param[in] recipient Recipient of the CLEAR_FEATURE request.
201 * @param[in] feature_selector Feature selector (in native endianness).
202 * @param[in] index Recipient index (in native endianness).
203 * @return Error code.
204 */
205int usb_request_clear_feature(usb_pipe_t *pipe,
206 usb_request_type_t request_type, usb_request_recipient_t recipient,
207 uint16_t feature_selector, uint16_t index)
208{
209 if (request_type == USB_REQUEST_TYPE_STANDARD) {
210 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE)
211 && (index != 0)) {
212 return EINVAL;
213 }
214 }
215
216 int rc = usb_control_request_set(pipe, request_type, recipient,
217 USB_DEVREQ_CLEAR_FEATURE,
218 uint16_host2usb(feature_selector), uint16_host2usb(index),
219 NULL, 0);
220
221 return rc;
222}
223
224/** Set or enable specific device feature.
225 *
226 * @param[in] pipe Control endpoint pipe (session must be already started).
227 * @param[in] request_type Request type (standard/class/vendor).
228 * @param[in] recipient Recipient of the SET_FEATURE request.
229 * @param[in] feature_selector Feature selector (in native endianness).
230 * @param[in] index Recipient index (in native endianness).
231 * @return Error code.
232 */
233int usb_request_set_feature(usb_pipe_t *pipe,
234 usb_request_type_t request_type, usb_request_recipient_t recipient,
235 uint16_t feature_selector, uint16_t index)
236{
237 if (request_type == USB_REQUEST_TYPE_STANDARD) {
238 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE)
239 && (index != 0)) {
240 return EINVAL;
241 }
242 }
243
244 int rc = usb_control_request_set(pipe, request_type, recipient,
245 USB_DEVREQ_SET_FEATURE,
246 uint16_host2usb(feature_selector), uint16_host2usb(index),
247 NULL, 0);
248
249 return rc;
250}
251
252/** Retrieve USB descriptor of a USB device.
253 *
254 * @param[in] pipe Control endpoint pipe (session must be already started).
255 * @param[in] request_type Request type (standard/class/vendor).
256 * @param[in] recipient Request recipient (device/interface/endpoint).
257 * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
258 * @param[in] descriptor_index Descriptor index.
259 * @param[in] language Language index.
260 * @param[out] buffer Buffer where to store the retrieved descriptor.
261 * @param[in] size Size of the @p buffer.
262 * @param[out] actual_size Number of bytes actually transferred.
263 * @return Error code.
264 */
265int usb_request_get_descriptor(usb_pipe_t *pipe,
266 usb_request_type_t request_type, usb_request_recipient_t recipient,
267 uint8_t descriptor_type, uint8_t descriptor_index,
268 uint16_t language,
269 void *buffer, size_t size, size_t *actual_size)
270{
271 if (buffer == NULL) {
272 return EBADMEM;
273 }
274 if (size == 0) {
275 return EINVAL;
276 }
277
278 uint16_t wValue = descriptor_index | (descriptor_type << 8);
279
280 return usb_control_request_get(pipe,
281 request_type, recipient,
282 USB_DEVREQ_GET_DESCRIPTOR,
283 wValue, language,
284 buffer, size, actual_size);
285}
286
287/** Retrieve USB descriptor, allocate space for it.
288 *
289 * @param[in] pipe Control endpoint pipe (session must be already started).
290 * @param[in] request_type Request type (standard/class/vendor).
291 * @param[in] recipient Request recipient (device/interface/endpoint).
292 * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
293 * @param[in] descriptor_index Descriptor index.
294 * @param[in] language Language index.
295 * @param[out] buffer_ptr Where to store pointer to allocated buffer.
296 * @param[out] buffer_size Where to store the size of the descriptor.
297 * @return
298 */
299int usb_request_get_descriptor_alloc(usb_pipe_t * pipe,
300 usb_request_type_t request_type, usb_request_recipient_t recipient,
301 uint8_t descriptor_type, uint8_t descriptor_index,
302 uint16_t language,
303 void **buffer_ptr, size_t *buffer_size)
304{
305 if (buffer_ptr == NULL) {
306 return EBADMEM;
307 }
308
309 int rc;
310
311 /*
312 * Get only first byte to retrieve descriptor length.
313 */
314 uint8_t tmp_buffer[1];
315 size_t bytes_transfered;
316 rc = usb_request_get_descriptor(pipe, request_type, recipient,
317 descriptor_type, descriptor_index, language,
318 &tmp_buffer, 1, &bytes_transfered);
319 if (rc != EOK) {
320 return rc;
321 }
322 if (bytes_transfered != 1) {
323 /* FIXME: some better error code? */
324 return ESTALL;
325 }
326
327 size_t size = tmp_buffer[0];
328 if (size == 0) {
329 /* FIXME: some better error code? */
330 return ESTALL;
331 }
332
333 /*
334 * Allocate buffer and get the descriptor again.
335 */
336 void *buffer = malloc(size);
337 if (buffer == NULL) {
338 return ENOMEM;
339 }
340
341 rc = usb_request_get_descriptor(pipe, request_type, recipient,
342 descriptor_type, descriptor_index, language,
343 buffer, size, &bytes_transfered);
344 if (rc != EOK) {
345 free(buffer);
346 return rc;
347 }
348 if (bytes_transfered != size) {
349 free(buffer);
350 /* FIXME: some better error code? */
351 return ESTALL;
352 }
353
354 *buffer_ptr = buffer;
355 if (buffer_size != NULL) {
356 *buffer_size = size;
357 }
358
359 return EOK;
360}
361
362/** Retrieve standard device descriptor of a USB device.
363 *
364 * @param[in] pipe Control endpoint pipe (session must be already started).
365 * @param[out] descriptor Storage for the device descriptor.
366 * @return Error code.
367 */
368int usb_request_get_device_descriptor(usb_pipe_t *pipe,
369 usb_standard_device_descriptor_t *descriptor)
370{
371 if (descriptor == NULL) {
372 return EBADMEM;
373 }
374
375 size_t actually_transferred = 0;
376 usb_standard_device_descriptor_t descriptor_tmp;
377 int rc = usb_request_get_descriptor(pipe,
378 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
379 USB_DESCTYPE_DEVICE, 0, 0,
380 &descriptor_tmp, sizeof(descriptor_tmp),
381 &actually_transferred);
382
383 if (rc != EOK) {
384 return rc;
385 }
386
387 /* Verify that all data has been transferred. */
388 if (actually_transferred < sizeof(descriptor_tmp)) {
389 return ELIMIT;
390 }
391
392 /* Everything is okay, copy the descriptor. */
393 memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
394
395 return EOK;
396}
397
398/** Retrieve configuration descriptor of a USB device.
399 *
400 * The function does not retrieve additional data binded with configuration
401 * descriptor (such as its interface and endpoint descriptors) - use
402 * usb_request_get_full_configuration_descriptor() instead.
403 *
404 * @param[in] pipe Control endpoint pipe (session must be already started).
405 * @param[in] index Descriptor index.
406 * @param[out] descriptor Storage for the device descriptor.
407 * @return Error code.
408 */
409int usb_request_get_bare_configuration_descriptor(usb_pipe_t *pipe,
410 int index, usb_standard_configuration_descriptor_t *descriptor)
411{
412 if (descriptor == NULL) {
413 return EBADMEM;
414 }
415
416 if ((index < 0) || (index > 0xFF)) {
417 return ERANGE;
418 }
419
420 size_t actually_transferred = 0;
421 usb_standard_configuration_descriptor_t descriptor_tmp;
422 int rc = usb_request_get_descriptor(pipe,
423 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
424 USB_DESCTYPE_CONFIGURATION, index, 0,
425 &descriptor_tmp, sizeof(descriptor_tmp),
426 &actually_transferred);
427 if (rc != EOK) {
428 return rc;
429 }
430
431 /* Verify that all data has been transferred. */
432 if (actually_transferred < sizeof(descriptor_tmp)) {
433 return ELIMIT;
434 }
435
436 /* Everything is okay, copy the descriptor. */
437 memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
438
439 return EOK;
440}
441
442/** Retrieve full configuration descriptor of a USB device.
443 *
444 * @warning The @p buffer might be touched (i.e. its contents changed)
445 * even when error occurs.
446 *
447 * @param[in] pipe Control endpoint pipe (session must be already started).
448 * @param[in] index Descriptor index.
449 * @param[out] descriptor Storage for the device descriptor.
450 * @param[in] descriptor_size Size of @p descriptor buffer.
451 * @param[out] actual_size Number of bytes actually transferred.
452 * @return Error code.
453 */
454int usb_request_get_full_configuration_descriptor(usb_pipe_t *pipe,
455 int index, void *descriptor, size_t descriptor_size, size_t *actual_size)
456{
457 if ((index < 0) || (index > 0xFF)) {
458 return ERANGE;
459 }
460
461 return usb_request_get_descriptor(pipe,
462 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
463 USB_DESCTYPE_CONFIGURATION, index, 0,
464 descriptor, descriptor_size, actual_size);
465}
466
467/** Retrieve full configuration descriptor, allocate space for it.
468 *
469 * The function takes care that full configuration descriptor is returned
470 * (i.e. the function will fail when less data then descriptor.totalLength
471 * is returned).
472 *
473 * @param[in] pipe Control endpoint pipe (session must be already started).
474 * @param[in] index Configuration index.
475 * @param[out] descriptor_ptr Where to store pointer to allocated buffer.
476 * @param[out] descriptor_size Where to store the size of the descriptor.
477 * @return Error code.
478 */
479int usb_request_get_full_configuration_descriptor_alloc(
480 usb_pipe_t *pipe, int index,
481 void **descriptor_ptr, size_t *descriptor_size)
482{
483 int rc;
484
485 if (descriptor_ptr == NULL) {
486 return EBADMEM;
487 }
488
489 usb_standard_configuration_descriptor_t bare_config;
490 rc = usb_request_get_bare_configuration_descriptor(pipe, index,
491 &bare_config);
492 if (rc != EOK) {
493 return rc;
494 }
495 if (bare_config.descriptor_type != USB_DESCTYPE_CONFIGURATION) {
496 return ENOENT;
497 }
498 if (bare_config.total_length < sizeof(bare_config)) {
499 return ELIMIT;
500 }
501
502 void *buffer = malloc(bare_config.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, bare_config.total_length, &transferred);
510 if (rc != EOK) {
511 free(buffer);
512 return rc;
513 }
514
515 if (transferred != bare_config.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 = bare_config.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.