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

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

libusbdev: Add few const qualifiers.

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