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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 32ec5671 was 160b75e, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Fix Doxygen groups of USB libraries

  • 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,
428 sizeof(descriptor_tmp));
429
430 return EOK;
431}
432
433/** Retrieve configuration descriptor of a USB device.
434 *
435 * The function does not retrieve additional data binded with configuration
436 * descriptor (such as its interface and endpoint descriptors) - use
437 * usb_request_get_full_configuration_descriptor() instead.
438 *
439 * @param[in] pipe Control endpoint pipe (session must be already started).
440 * @param[in] index Descriptor index.
441 * @param[out] descriptor Storage for the device descriptor.
442 * @return Error code.
443 */
444int usb_request_get_bare_configuration_descriptor(usb_pipe_t *pipe,
445 int index, usb_standard_configuration_descriptor_t *descriptor)
446{
447 if (descriptor == NULL) {
448 return EBADMEM;
449 }
450
451 if ((index < 0) || (index > 0xFF)) {
452 return ERANGE;
453 }
454
455 size_t actually_transferred = 0;
456 usb_standard_configuration_descriptor_t descriptor_tmp;
457 int rc = usb_request_get_descriptor(pipe,
458 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
459 USB_DESCTYPE_CONFIGURATION, index, 0,
460 &descriptor_tmp, sizeof(descriptor_tmp),
461 &actually_transferred);
462 if (rc != EOK) {
463 return rc;
464 }
465
466 /* Verify that all data has been transferred. */
467 if (actually_transferred < sizeof(descriptor_tmp)) {
468 return ELIMIT;
469 }
470
471 /* Everything is okay, copy the descriptor. */
472 memcpy(descriptor, &descriptor_tmp,
473 sizeof(descriptor_tmp));
474
475 return EOK;
476}
477
478/** Retrieve full configuration descriptor of a USB device.
479 *
480 * @warning The @p buffer might be touched (i.e. its contents changed)
481 * even when error occurs.
482 *
483 * @param[in] pipe Control endpoint pipe (session must be already started).
484 * @param[in] index Descriptor index.
485 * @param[out] descriptor Storage for the device descriptor.
486 * @param[in] descriptor_size Size of @p descriptor buffer.
487 * @param[out] actual_size Number of bytes actually transferred.
488 * @return Error code.
489 */
490int usb_request_get_full_configuration_descriptor(usb_pipe_t *pipe,
491 int index, void *descriptor, size_t descriptor_size, size_t *actual_size)
492{
493 if ((index < 0) || (index > 0xFF)) {
494 return ERANGE;
495 }
496
497 return usb_request_get_descriptor(pipe,
498 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
499 USB_DESCTYPE_CONFIGURATION, index, 0,
500 descriptor, descriptor_size, actual_size);
501}
502
503/** Retrieve full configuration descriptor, allocate space for it.
504 *
505 * The function takes care that full configuration descriptor is returned
506 * (i.e. the function will fail when less data then descriptor.totalLength
507 * is returned).
508 *
509 * @param[in] pipe Control endpoint pipe (session must be already started).
510 * @param[in] index Configuration index.
511 * @param[out] descriptor_ptr Where to store pointer to allocated buffer.
512 * @param[out] descriptor_size Where to store the size of the descriptor.
513 * @return Error code.
514 */
515int usb_request_get_full_configuration_descriptor_alloc(
516 usb_pipe_t *pipe, int index,
517 void **descriptor_ptr, size_t *descriptor_size)
518{
519 int rc;
520
521 if (descriptor_ptr == NULL) {
522 return EBADMEM;
523 }
524
525 usb_standard_configuration_descriptor_t bare_config;
526 rc = usb_request_get_bare_configuration_descriptor(pipe, index,
527 &bare_config);
528 if (rc != EOK) {
529 return rc;
530 }
531 if (bare_config.descriptor_type != USB_DESCTYPE_CONFIGURATION) {
532 return ENOENT;
533 }
534 if (bare_config.total_length < sizeof(bare_config)) {
535 return ELIMIT;
536 }
537
538 void *buffer = malloc(bare_config.total_length);
539 if (buffer == NULL) {
540 return ENOMEM;
541 }
542
543 size_t transferred = 0;
544 rc = usb_request_get_full_configuration_descriptor(pipe, index,
545 buffer, bare_config.total_length, &transferred);
546 if (rc != EOK) {
547 free(buffer);
548 return rc;
549 }
550
551 if (transferred != bare_config.total_length) {
552 free(buffer);
553 return ELIMIT;
554 }
555
556 /* Everything looks okay, copy the pointers. */
557
558 *descriptor_ptr = buffer;
559
560 if (descriptor_size != NULL) {
561 *descriptor_size = bare_config.total_length;
562 }
563
564 return EOK;
565}
566
567/** Update existing or add new USB descriptor to a USB device.
568 *
569 * @param[in] pipe Control endpoint pipe (session must be already started).
570 * @param[in] request_type Request type (standard/class/vendor).
571 * @param[in] recipient Request recipient (device/interface/endpoint).
572 * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
573 * @param[in] descriptor_index Descriptor index.
574 * @param[in] language Language index (in native endianness).
575 * @param[in] buffer Buffer with the new descriptor (in USB endianness).
576 * @param[in] size Size of the @p buffer in bytes (in native endianness).
577 * @return Error code.
578 */
579int usb_request_set_descriptor(usb_pipe_t *pipe,
580 usb_request_type_t request_type, usb_request_recipient_t recipient,
581 uint8_t descriptor_type, uint8_t descriptor_index,
582 uint16_t language,
583 void *buffer, size_t size)
584{
585 if (buffer == NULL) {
586 return EBADMEM;
587 }
588 if (size == 0) {
589 return EINVAL;
590 }
591
592 /* FIXME: proper endianness. */
593 uint16_t wValue = descriptor_index | (descriptor_type << 8);
594
595 return usb_control_request_set(pipe,
596 request_type, recipient,
597 USB_DEVREQ_SET_DESCRIPTOR,
598 wValue, language,
599 buffer, size);
600}
601
602/** Get current configuration value of USB device.
603 *
604 * @param[in] pipe Control endpoint pipe (session must be already started).
605 * @param[out] configuration_value Current configuration value.
606 * @return Error code.
607 */
608int usb_request_get_configuration(usb_pipe_t *pipe,
609 uint8_t *configuration_value)
610{
611 uint8_t value;
612 size_t actual_size;
613
614 int rc = usb_control_request_get(pipe,
615 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
616 USB_DEVREQ_GET_CONFIGURATION,
617 0, 0,
618 &value, 1, &actual_size);
619
620 if (rc != EOK) {
621 return rc;
622 }
623 if (actual_size != 1) {
624 return ELIMIT;
625 }
626
627 if (configuration_value != NULL) {
628 *configuration_value = value;
629 }
630
631 return EOK;
632}
633
634/** Set configuration of USB device.
635 *
636 * @param pipe Control endpoint pipe (session must be already started).
637 * @param configuration_value New configuration value.
638 * @return Error code.
639 */
640int usb_request_set_configuration(usb_pipe_t *pipe,
641 uint8_t configuration_value)
642{
643 uint16_t config_value
644 = uint16_host2usb((uint16_t) configuration_value);
645
646 return usb_control_request_set(pipe,
647 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
648 USB_DEVREQ_SET_CONFIGURATION, config_value, 0,
649 NULL, 0);
650}
651
652/** Get selected 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[out] alternate_setting Alternate setting for the interface.
657 * @return Error code.
658 */
659int usb_request_get_interface(usb_pipe_t *pipe,
660 uint8_t interface_index, uint8_t *alternate_setting)
661{
662 uint8_t value;
663 size_t actual_size;
664
665 int rc = usb_control_request_get(pipe,
666 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
667 USB_DEVREQ_GET_INTERFACE,
668 0, uint16_host2usb((uint16_t) interface_index),
669 &value, 1, &actual_size);
670
671 if (rc != EOK) {
672 return rc;
673 }
674 if (actual_size != 1) {
675 return ELIMIT;
676 }
677
678 if (alternate_setting != NULL) {
679 *alternate_setting = value;
680 }
681
682 return EOK;
683}
684
685/** Select alternate setting for USB interface.
686 *
687 * @param[in] pipe Control endpoint pipe (session must be already started).
688 * @param[in] interface_index Interface index.
689 * @param[in] alternate_setting Alternate setting to select.
690 * @return Error code.
691 */
692int usb_request_set_interface(usb_pipe_t *pipe,
693 uint8_t interface_index, uint8_t alternate_setting)
694{
695 return usb_control_request_set(pipe,
696 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
697 USB_DEVREQ_SET_INTERFACE,
698 uint16_host2usb((uint16_t) alternate_setting),
699 uint16_host2usb((uint16_t) interface_index),
700 NULL, 0);
701}
702
703/** Get list of supported languages by USB device.
704 *
705 * @param[in] pipe Control endpoint pipe (session must be already started).
706 * @param[out] languages_ptr Where to store pointer to allocated array of
707 * supported languages.
708 * @param[out] languages_count Number of supported languages.
709 * @return Error code.
710 */
711int usb_request_get_supported_languages(usb_pipe_t *pipe,
712 l18_win_locales_t **languages_ptr, size_t *languages_count)
713{
714 int rc;
715
716 if (languages_ptr == NULL) {
717 return EBADMEM;
718 }
719 if (languages_count == NULL) {
720 return EBADMEM;
721 }
722
723 uint8_t *string_descriptor = NULL;
724 size_t string_descriptor_size = 0;
725 rc = usb_request_get_descriptor_alloc(pipe,
726 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
727 USB_DESCTYPE_STRING, 0, 0,
728 (void **) &string_descriptor, &string_descriptor_size);
729 if (rc != EOK) {
730 return rc;
731 }
732 if (string_descriptor_size <= 2) {
733 free(string_descriptor);
734 return EEMPTY;
735 }
736 /* Subtract first 2 bytes (length and descriptor type). */
737 string_descriptor_size -= 2;
738
739 /* Odd number of bytes - descriptor is broken? */
740 if ((string_descriptor_size % 2) != 0) {
741 /* FIXME: shall we return with error or silently ignore? */
742 free(string_descriptor);
743 return ESTALL;
744 }
745
746 size_t langs_count = string_descriptor_size / 2;
747 l18_win_locales_t *langs
748 = malloc(sizeof(l18_win_locales_t) * langs_count);
749 if (langs == NULL) {
750 free(string_descriptor);
751 return ENOMEM;
752 }
753
754 size_t i;
755 for (i = 0; i < langs_count; i++) {
756 /* Language code from the descriptor is in USB endianness. */
757 /* FIXME: is this really correct? */
758 uint16_t lang_code = (string_descriptor[2 + 2 * i + 1] << 8)
759 + string_descriptor[2 + 2 * i];
760 langs[i] = uint16_usb2host(lang_code);
761 }
762
763 free(string_descriptor);
764
765 *languages_ptr = langs;
766 *languages_count =langs_count;
767
768 return EOK;
769}
770
771/** Get string (descriptor) from USB device.
772 *
773 * The string is returned in native encoding of the operating system.
774 * For HelenOS, that is UTF-8.
775 *
776 * @param[in] pipe Control endpoint pipe (session must be already started).
777 * @param[in] index String index (in native endianness),
778 * first index has number 1 (index from descriptors can be used directly).
779 * @param[in] lang String language (in native endianness).
780 * @param[out] string_ptr Where to store allocated string in native encoding.
781 * @return Error code.
782 */
783int usb_request_get_string(usb_pipe_t *pipe,
784 size_t index, l18_win_locales_t lang, char **string_ptr)
785{
786 if (string_ptr == NULL) {
787 return EBADMEM;
788 }
789 /*
790 * Index is actually one byte value and zero index is used
791 * to retrieve list of supported languages.
792 */
793 if ((index < 1) || (index > 0xFF)) {
794 return ERANGE;
795 }
796 /* Language is actually two byte value. */
797 if (lang > 0xFFFF) {
798 return ERANGE;
799 }
800
801 int rc;
802
803 /* Prepare dynamically allocated variables. */
804 uint8_t *string = NULL;
805 wchar_t *string_chars = NULL;
806
807 /* Get the actual descriptor. */
808 size_t string_size;
809 rc = usb_request_get_descriptor_alloc(pipe,
810 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
811 USB_DESCTYPE_STRING, index, uint16_host2usb(lang),
812 (void **) &string, &string_size);
813 if (rc != EOK) {
814 goto leave;
815 }
816
817 if (string_size <= 2) {
818 rc = EEMPTY;
819 goto leave;
820 }
821 /* Subtract first 2 bytes (length and descriptor type). */
822 string_size -= 2;
823
824 /* Odd number of bytes - descriptor is broken? */
825 if ((string_size % 2) != 0) {
826 /* FIXME: shall we return with error or silently ignore? */
827 rc = ESTALL;
828 goto leave;
829 }
830
831 size_t string_char_count = string_size / 2;
832 string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1));
833 if (string_chars == NULL) {
834 rc = ENOMEM;
835 goto leave;
836 }
837
838 /*
839 * Build a wide string.
840 * And do not forget to set NULL terminator (string descriptors
841 * do not have them).
842 */
843 size_t i;
844 for (i = 0; i < string_char_count; i++) {
845 uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
846 + string[2 + 2 * i];
847 string_chars[i] = uni_char;
848 }
849 string_chars[string_char_count] = 0;
850
851
852 /* Convert to normal string. */
853 char *str = wstr_to_astr(string_chars);
854 if (str == NULL) {
855 rc = ENOMEM;
856 goto leave;
857 }
858
859 *string_ptr = str;
860 rc = EOK;
861
862leave:
863 if (string != NULL) {
864 free(string);
865 }
866 if (string_chars != NULL) {
867 free(string_chars);
868 }
869
870 return rc;
871}
872
873/** Clear halt bit of an endpoint pipe (after pipe stall).
874 *
875 * @param pipe Control pipe.
876 * @param ep_index Endpoint index (in native endianness).
877 * @return Error code.
878 */
879int usb_request_clear_endpoint_halt(usb_pipe_t *pipe, uint16_t ep_index)
880{
881 return usb_request_clear_feature(pipe,
882 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT,
883 uint16_host2usb(USB_FEATURE_SELECTOR_ENDPOINT_HALT),
884 uint16_host2usb(ep_index));
885}
886
887/** Clear halt bit of an endpoint pipe (after pipe stall).
888 *
889 * @param ctrl_pipe Control pipe.
890 * @param target_pipe Which pipe is halted and shall be cleared.
891 * @return Error code.
892 */
893int usb_pipe_clear_halt(usb_pipe_t *ctrl_pipe, usb_pipe_t *target_pipe)
894{
895 if ((ctrl_pipe == NULL) || (target_pipe == NULL)) {
896 return EINVAL;
897 }
898 return usb_request_clear_endpoint_halt(ctrl_pipe,
899 target_pipe->endpoint_no);
900}
901
902/** Get endpoint status.
903 *
904 * @param[in] ctrl_pipe Control pipe.
905 * @param[in] pipe Of which pipe the status shall be received.
906 * @param[out] status Where to store pipe status (in native endianness).
907 * @return Error code.
908 */
909int usb_request_get_endpoint_status(usb_pipe_t *ctrl_pipe, usb_pipe_t *pipe,
910 uint16_t *status)
911{
912 uint16_t status_tmp;
913 uint16_t pipe_index = (uint16_t) pipe->endpoint_no;
914 int rc = usb_request_get_status(ctrl_pipe,
915 USB_REQUEST_RECIPIENT_ENDPOINT, uint16_host2usb(pipe_index),
916 &status_tmp);
917 if (rc != EOK) {
918 return rc;
919 }
920
921 if (status != NULL) {
922 *status = uint16_usb2host(status_tmp);
923 }
924
925 return EOK;
926}
927
928/**
929 * @}
930 */
Note: See TracBrowser for help on using the repository browser.