source: mainline/uspace/lib/usb/src/request.c@ bbc7d83e

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

Add GET_DESCRIPTOR request wrapper using pipes

  • Property mode set to 100644
File size: 4.3 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 libusb
30 * @{
31 */
32/** @file
33 * Standard USB requests (implementation).
34 */
35#include <usb/request.h>
36#include <usb/devreq.h>
37#include <errno.h>
38
39/** Prepare setup packet.
40 *
41 * @param name Variable name with the setup packet.
42 * @param p_direction Data transfer direction.
43 * @param p_type Request type (standard/class/vendor)
44 * @param p_recipient Recipient of the request.
45 * @param p_request Request.
46 * @param p_value wValue field of setup packet.
47 * @param p_index wIndex field of setup packet.
48 * @param p_length Length of extra data.
49 */
50#define PREPARE_SETUP_PACKET(name, p_direction, p_type, p_recipient, \
51 p_request, p_value, p_index, p_length) \
52 usb_device_request_setup_packet_t name = { \
53 .request_type = \
54 ((p_direction) == USB_DIRECTION_IN ? 128 : 0) \
55 | ((p_type) << 5) \
56 | (p_recipient), \
57 .request = (p_request), \
58 { .value = (p_value) }, \
59 .index = (p_index), \
60 .length = (p_length) \
61 }
62
63/** Prepare setup packet.
64 *
65 * @param name Variable name with the setup packet.
66 * @param p_direction Data transfer direction.
67 * @param p_type Request type (standard/class/vendor)
68 * @param p_recipient Recipient of the request.
69 * @param p_request Request.
70 * @param p_value_low wValue field of setup packet (low byte).
71 * @param p_value_high wValue field of setup packet (high byte).
72 * @param p_index wIndex field of setup packet.
73 * @param p_length Length of extra data.
74 */
75#define PREPARE_SETUP_PACKET_LOHI(name, p_direction, p_type, p_recipient, \
76 p_request, p_value_low, p_value_high, p_index, p_length) \
77 PREPARE_SETUP_PACKET(name, p_direction, p_type, p_recipient, \
78 p_request, (p_value_low) | ((p_value_high) << 8), \
79 p_index, p_length)
80
81
82/** Retrieve USB descriptor of a USB device.
83 *
84 * @param[in] pipe Control endpoint pipe (session must be already started).
85 * @param[in] request_type Request type (standard/class/vendor).
86 * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
87 * @param[in] descriptor_index Descriptor index.
88 * @param[in] language Language index.
89 * @param[out] buffer Buffer where to store the retrieved descriptor.
90 * @param[in] size Size of the @p buffer.
91 * @param[out] actual_size Number of bytes actually transferred.
92 * @return Error code.
93 */
94int usb_request_get_descriptor(usb_endpoint_pipe_t *pipe,
95 usb_request_type_t request_type,
96 uint8_t descriptor_type, uint8_t descriptor_index,
97 uint16_t language,
98 void *buffer, size_t size, size_t *actual_size)
99{
100 if (buffer == NULL) {
101 return EBADMEM;
102 }
103 if (size == 0) {
104 return EINVAL;
105 }
106
107 PREPARE_SETUP_PACKET_LOHI(setup_packet, USB_DIRECTION_IN,
108 request_type, USB_REQUEST_RECIPIENT_DEVICE,
109 USB_DEVREQ_GET_DESCRIPTOR, descriptor_index, descriptor_type,
110 language, size);
111
112 int rc = usb_endpoint_pipe_control_read(pipe,
113 &setup_packet, sizeof(setup_packet),
114 buffer, size, actual_size);
115
116 return rc;
117}
118
119/**
120 * @}
121 */
Note: See TracBrowser for help on using the repository browser.