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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b4b534ac was b4b534ac, checked in by Jakub Jermar <jakub@…>, 9 years ago

Merge from lp:~jan.vesely/helenos/usb

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