1 | /*
|
---|
2 | * Copyright (c) 2010 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 usb
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief USB driver - standard USB requests (implementation).
|
---|
34 | */
|
---|
35 | #include <usb/usbdrv.h>
|
---|
36 | #include <errno.h>
|
---|
37 |
|
---|
38 | /** Change address of connected device.
|
---|
39 | *
|
---|
40 | * @see usb_drv_reserve_default_address
|
---|
41 | * @see usb_drv_release_default_address
|
---|
42 | * @see usb_drv_request_address
|
---|
43 | * @see usb_drv_release_address
|
---|
44 | * @see usb_drv_bind_address
|
---|
45 | *
|
---|
46 | * @param phone Open phone to HC driver.
|
---|
47 | * @param old_address Current address.
|
---|
48 | * @param address Address to be set.
|
---|
49 | * @return Error code.
|
---|
50 | */
|
---|
51 | int usb_drv_req_set_address(int phone, usb_address_t old_address,
|
---|
52 | usb_address_t new_address)
|
---|
53 | {
|
---|
54 | /* Prepare the target. */
|
---|
55 | usb_target_t target = {
|
---|
56 | .address = old_address,
|
---|
57 | .endpoint = 0
|
---|
58 | };
|
---|
59 |
|
---|
60 | /* Prepare the setup packet. */
|
---|
61 | usb_device_request_setup_packet_t setup_packet = {
|
---|
62 | .request_type = 0,
|
---|
63 | .request = USB_DEVREQ_SET_ADDRESS,
|
---|
64 | .index = 0,
|
---|
65 | .length = 0,
|
---|
66 | };
|
---|
67 | setup_packet.value = new_address;
|
---|
68 |
|
---|
69 | usb_handle_t handle;
|
---|
70 | int rc;
|
---|
71 |
|
---|
72 | /* Start the control write transfer. */
|
---|
73 | rc = usb_drv_async_control_write_setup(phone, target,
|
---|
74 | &setup_packet, sizeof(setup_packet), &handle);
|
---|
75 | if (rc != EOK) {
|
---|
76 | return rc;
|
---|
77 | }
|
---|
78 | rc = usb_drv_async_wait_for(handle);
|
---|
79 | if (rc != EOK) {
|
---|
80 | return rc;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /* Finish the control write transfer. */
|
---|
84 | rc = usb_drv_async_control_write_status(phone, target, &handle);
|
---|
85 | if (rc != EOK) {
|
---|
86 | return rc;
|
---|
87 | }
|
---|
88 | rc = usb_drv_async_wait_for(handle);
|
---|
89 | if (rc != EOK) {
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 |
|
---|
93 | return EOK;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Retrieve device descriptor of connected USB device.
|
---|
97 | *
|
---|
98 | * @param[in] phone Open phone to HC driver.
|
---|
99 | * @param[in] address Device USB address.
|
---|
100 | * @param[out] descriptor Storage for the device descriptor.
|
---|
101 | * @return Error code.
|
---|
102 | * @retval EBADMEM @p descriptor is NULL.
|
---|
103 | */
|
---|
104 | int usb_drv_req_get_device_descriptor(int phone, usb_address_t address,
|
---|
105 | usb_standard_device_descriptor_t *descriptor)
|
---|
106 | {
|
---|
107 | if (descriptor == NULL) {
|
---|
108 | return EBADMEM;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /* Prepare the target. */
|
---|
112 | usb_target_t target = {
|
---|
113 | .address = address,
|
---|
114 | .endpoint = 0
|
---|
115 | };
|
---|
116 |
|
---|
117 | /* Prepare the setup packet. */
|
---|
118 | usb_device_request_setup_packet_t setup_packet = {
|
---|
119 | .request_type = 128,
|
---|
120 | .request = USB_DEVREQ_GET_DESCRIPTOR,
|
---|
121 | .index = 0,
|
---|
122 | .length = sizeof(usb_standard_device_descriptor_t)
|
---|
123 | };
|
---|
124 | setup_packet.value_high = USB_DESCTYPE_DEVICE;
|
---|
125 | setup_packet.value_low = 0;
|
---|
126 |
|
---|
127 | usb_handle_t handle;
|
---|
128 | int rc;
|
---|
129 |
|
---|
130 | /* Start the control read transfer. */
|
---|
131 | rc = usb_drv_async_control_read_setup(phone, target,
|
---|
132 | &setup_packet, sizeof(usb_device_request_setup_packet_t), &handle);
|
---|
133 | if (rc != EOK) {
|
---|
134 | return rc;
|
---|
135 | }
|
---|
136 | rc = usb_drv_async_wait_for(handle);
|
---|
137 | if (rc != EOK) {
|
---|
138 | return rc;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /* Retrieve the descriptor. */
|
---|
142 | size_t actually_transferred = 0;
|
---|
143 | usb_standard_device_descriptor_t descriptor_tmp;
|
---|
144 | rc = usb_drv_async_control_read_data(phone, target,
|
---|
145 | &descriptor_tmp, sizeof(usb_standard_device_descriptor_t),
|
---|
146 | &actually_transferred, &handle);
|
---|
147 | if (rc != EOK) {
|
---|
148 | return rc;
|
---|
149 | }
|
---|
150 | rc = usb_drv_async_wait_for(handle);
|
---|
151 | if (rc != EOK) {
|
---|
152 | return rc;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /* Finish the control read transfer. */
|
---|
156 | rc = usb_drv_async_control_read_status(phone, target, &handle);
|
---|
157 | if (rc != EOK) {
|
---|
158 | return rc;
|
---|
159 | }
|
---|
160 | rc = usb_drv_async_wait_for(handle);
|
---|
161 | if (rc != EOK) {
|
---|
162 | return rc;
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (actually_transferred < sizeof(usb_standard_device_descriptor_t)) {
|
---|
166 | return ELIMIT;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /*
|
---|
170 | * Everything is okay, copy the descriptor.
|
---|
171 | */
|
---|
172 | memcpy(descriptor, &descriptor_tmp,
|
---|
173 | sizeof(usb_standard_device_descriptor_t));
|
---|
174 |
|
---|
175 | return EOK;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * @}
|
---|
182 | */
|
---|