1 | /*
|
---|
2 | * Copyright (c) 2014 Jan Kolarik
|
---|
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 | /** @file wmi.c
|
---|
30 | *
|
---|
31 | * Implementation of Atheros WMI communication.
|
---|
32 | *
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <usb/debug.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <mem.h>
|
---|
40 | #include <byteorder.h>
|
---|
41 | #include "wmi.h"
|
---|
42 |
|
---|
43 | /** WMI registry read.
|
---|
44 | *
|
---|
45 | * @param htc_device HTC device structure.
|
---|
46 | * @param reg_offset Registry offset (address) to be read.
|
---|
47 | * @param res Stored result.
|
---|
48 | *
|
---|
49 | * @return EOK if succeed, error code otherwise.
|
---|
50 | *
|
---|
51 | */
|
---|
52 | errno_t wmi_reg_read(htc_device_t *htc_device, uint32_t reg_offset, uint32_t *res)
|
---|
53 | {
|
---|
54 | uint32_t cmd_value = host2uint32_t_be(reg_offset);
|
---|
55 |
|
---|
56 | void *resp_buffer =
|
---|
57 | malloc(htc_device->ath_device->ctrl_response_length);
|
---|
58 |
|
---|
59 | errno_t rc = wmi_send_command(htc_device, WMI_REG_READ,
|
---|
60 | (uint8_t *) &cmd_value, sizeof(cmd_value), resp_buffer);
|
---|
61 |
|
---|
62 | if (rc != EOK) {
|
---|
63 | usb_log_error("Failed to read registry value.\n");
|
---|
64 | return rc;
|
---|
65 | }
|
---|
66 |
|
---|
67 | uint32_t *resp_value = (uint32_t *) ((void *) resp_buffer +
|
---|
68 | sizeof(htc_frame_header_t) + sizeof(wmi_command_header_t));
|
---|
69 |
|
---|
70 | *res = uint32_t_be2host(*resp_value);
|
---|
71 |
|
---|
72 | return rc;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** WMI registry write.
|
---|
76 | *
|
---|
77 | * @param htc_device HTC device structure.
|
---|
78 | * @param reg_offset Registry offset (address) to be written.
|
---|
79 | * @param val Value to be written
|
---|
80 | *
|
---|
81 | * @return EOK if succeed, error code otherwise.
|
---|
82 | *
|
---|
83 | */
|
---|
84 | errno_t wmi_reg_write(htc_device_t *htc_device, uint32_t reg_offset, uint32_t val)
|
---|
85 | {
|
---|
86 | uint32_t cmd_buffer[] = {
|
---|
87 | host2uint32_t_be(reg_offset),
|
---|
88 | host2uint32_t_be(val)
|
---|
89 | };
|
---|
90 |
|
---|
91 | void *resp_buffer =
|
---|
92 | malloc(htc_device->ath_device->ctrl_response_length);
|
---|
93 |
|
---|
94 | errno_t rc = wmi_send_command(htc_device, WMI_REG_WRITE,
|
---|
95 | (uint8_t *) &cmd_buffer, sizeof(cmd_buffer), resp_buffer);
|
---|
96 |
|
---|
97 | free(resp_buffer);
|
---|
98 |
|
---|
99 | if (rc != EOK) {
|
---|
100 | usb_log_error("Failed to write registry value.\n");
|
---|
101 | return rc;
|
---|
102 | }
|
---|
103 |
|
---|
104 | return rc;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** WMI registry set or clear specified bits.
|
---|
108 | *
|
---|
109 | * @param htc_device HTC device structure.
|
---|
110 | * @param reg_offset Registry offset (address) to be written.
|
---|
111 | * @param set_bit Bit to be set.
|
---|
112 | * @param clear_bit Bit to be cleared.
|
---|
113 | *
|
---|
114 | * @return EOK if succeed, error code otherwise.
|
---|
115 | *
|
---|
116 | */
|
---|
117 | errno_t wmi_reg_set_clear_bit(htc_device_t *htc_device, uint32_t reg_offset,
|
---|
118 | uint32_t set_bit, uint32_t clear_bit)
|
---|
119 | {
|
---|
120 | uint32_t value;
|
---|
121 |
|
---|
122 | errno_t rc = wmi_reg_read(htc_device, reg_offset, &value);
|
---|
123 | if (rc != EOK) {
|
---|
124 | usb_log_error("Failed to read registry value in RMW "
|
---|
125 | "function.\n");
|
---|
126 | return rc;
|
---|
127 | }
|
---|
128 |
|
---|
129 | value &= ~clear_bit;
|
---|
130 | value |= set_bit;
|
---|
131 |
|
---|
132 | rc = wmi_reg_write(htc_device, reg_offset, value);
|
---|
133 | if (rc != EOK) {
|
---|
134 | usb_log_error("Failed to write registry value in RMW "
|
---|
135 | "function.\n");
|
---|
136 | return rc;
|
---|
137 | }
|
---|
138 |
|
---|
139 | return rc;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** WMI registry set specified bit.
|
---|
143 | *
|
---|
144 | * @param htc_device HTC device structure.
|
---|
145 | * @param reg_offset Registry offset (address) to be written.
|
---|
146 | * @param set_bit Bit to be set.
|
---|
147 | *
|
---|
148 | * @return EOK if succeed, error code otherwise.
|
---|
149 | *
|
---|
150 | */
|
---|
151 | errno_t wmi_reg_set_bit(htc_device_t *htc_device, uint32_t reg_offset,
|
---|
152 | uint32_t set_bit)
|
---|
153 | {
|
---|
154 | return wmi_reg_set_clear_bit(htc_device, reg_offset, set_bit, 0);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** WMI registry clear specified bit.
|
---|
158 | *
|
---|
159 | * @param htc_device HTC device structure.
|
---|
160 | * @param reg_offset Registry offset (address) to be written.
|
---|
161 | * @param clear_bit Bit to be cleared.
|
---|
162 | *
|
---|
163 | * @return EOK if succeed, error code otherwise.
|
---|
164 | *
|
---|
165 | */
|
---|
166 | errno_t wmi_reg_clear_bit(htc_device_t *htc_device, uint32_t reg_offset,
|
---|
167 | uint32_t clear_bit)
|
---|
168 | {
|
---|
169 | return wmi_reg_set_clear_bit(htc_device, reg_offset, 0, clear_bit);
|
---|
170 | }
|
---|
171 |
|
---|
172 | /** WMI multi registry write.
|
---|
173 | *
|
---|
174 | * @param htc_device HTC device structure.
|
---|
175 | * @param reg_buffer Array of registry values to be written.
|
---|
176 | * @param elements Number of elements in array.
|
---|
177 | *
|
---|
178 | * @return EOK if succeed, error code otherwise.
|
---|
179 | *
|
---|
180 | */
|
---|
181 | errno_t wmi_reg_buffer_write(htc_device_t *htc_device, wmi_reg_t *reg_buffer,
|
---|
182 | size_t elements)
|
---|
183 | {
|
---|
184 | size_t buffer_size = sizeof(wmi_reg_t) * elements;
|
---|
185 | void *buffer = malloc(buffer_size);
|
---|
186 | void *resp_buffer =
|
---|
187 | malloc(htc_device->ath_device->ctrl_response_length);
|
---|
188 |
|
---|
189 | /* Convert values to correct endianness. */
|
---|
190 | for (size_t i = 0; i < elements; i++) {
|
---|
191 | wmi_reg_t *buffer_element = ®_buffer[i];
|
---|
192 | wmi_reg_t *buffer_it = (wmi_reg_t *)
|
---|
193 | ((void *) buffer + i * sizeof(wmi_reg_t));
|
---|
194 | buffer_it->offset =
|
---|
195 | host2uint32_t_be(buffer_element->offset);
|
---|
196 | buffer_it->value =
|
---|
197 | host2uint32_t_be(buffer_element->value);
|
---|
198 | }
|
---|
199 |
|
---|
200 | errno_t rc = wmi_send_command(htc_device, WMI_REG_WRITE,
|
---|
201 | (uint8_t *) buffer, buffer_size, resp_buffer);
|
---|
202 |
|
---|
203 | free(buffer);
|
---|
204 | free(resp_buffer);
|
---|
205 |
|
---|
206 | if (rc != EOK) {
|
---|
207 | usb_log_error("Failed to write multi registry value.\n");
|
---|
208 | return rc;
|
---|
209 | }
|
---|
210 |
|
---|
211 | return rc;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** Send WMI message to HTC device.
|
---|
215 | *
|
---|
216 | * @param htc_device HTC device structure.
|
---|
217 | * @param command_id Command identification.
|
---|
218 | * @param command_buffer Buffer with command data.
|
---|
219 | * @param command_length Length of command data.
|
---|
220 | * @param response_buffer Buffer with response data.
|
---|
221 | *
|
---|
222 | * @return EOK if succeed, error code otherwise.
|
---|
223 | *
|
---|
224 | */
|
---|
225 | errno_t wmi_send_command(htc_device_t *htc_device, wmi_command_t command_id,
|
---|
226 | uint8_t *command_buffer, uint32_t command_length, void *response_buffer)
|
---|
227 | {
|
---|
228 | size_t header_size = sizeof(wmi_command_header_t) +
|
---|
229 | sizeof(htc_frame_header_t);
|
---|
230 | size_t buffer_size = header_size + command_length;
|
---|
231 | void *buffer = malloc(buffer_size);
|
---|
232 | if (buffer == NULL) {
|
---|
233 | usb_log_error("Failed to allocate WMI message buffer (out of memory).\n");
|
---|
234 | return ENOMEM;
|
---|
235 | }
|
---|
236 |
|
---|
237 | if (command_buffer != NULL)
|
---|
238 | memcpy(buffer + header_size, command_buffer, command_length);
|
---|
239 |
|
---|
240 | /* Set up WMI header */
|
---|
241 | wmi_command_header_t *wmi_header = (wmi_command_header_t *)
|
---|
242 | ((void *) buffer + sizeof(htc_frame_header_t));
|
---|
243 | wmi_header->command_id = host2uint16_t_be(command_id);
|
---|
244 | wmi_header->sequence_number =
|
---|
245 | host2uint16_t_be(++htc_device->sequence_number);
|
---|
246 |
|
---|
247 | /* Send message (buffer will not be needed afterwards regardless of result). */
|
---|
248 | errno_t rc = htc_send_control_message(htc_device, buffer, buffer_size,
|
---|
249 | htc_device->endpoints.wmi_endpoint);
|
---|
250 | free(buffer);
|
---|
251 | if (rc != EOK) {
|
---|
252 | usb_log_error("Failed to send WMI message. Error: %s\n", str_error_name(rc));
|
---|
253 | return rc;
|
---|
254 | }
|
---|
255 |
|
---|
256 | bool clean_resp_buffer = false;
|
---|
257 | size_t response_buffer_size =
|
---|
258 | htc_device->ath_device->ctrl_response_length;
|
---|
259 | if (response_buffer == NULL) {
|
---|
260 | response_buffer = malloc(response_buffer_size);
|
---|
261 | clean_resp_buffer = true;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /* Read response. */
|
---|
265 | /* TODO: Ignoring WMI management RX messages ~ TX statuses etc. */
|
---|
266 | uint16_t cmd_id;
|
---|
267 | do {
|
---|
268 | rc = htc_read_control_message(htc_device, response_buffer,
|
---|
269 | response_buffer_size, NULL);
|
---|
270 | if (rc != EOK) {
|
---|
271 | usb_log_error("Failed to receive WMI message response. "
|
---|
272 | "Error: %s\n", str_error_name(rc));
|
---|
273 | return rc;
|
---|
274 | }
|
---|
275 |
|
---|
276 | if (response_buffer_size < sizeof(htc_frame_header_t) +
|
---|
277 | sizeof(wmi_command_header_t)) {
|
---|
278 | usb_log_error("Corrupted response received.\n");
|
---|
279 | return EINVAL;
|
---|
280 | }
|
---|
281 |
|
---|
282 | wmi_command_header_t *wmi_hdr = (wmi_command_header_t *)
|
---|
283 | ((void *) response_buffer + sizeof(htc_frame_header_t));
|
---|
284 | cmd_id = uint16_t_be2host(wmi_hdr->command_id);
|
---|
285 | } while (cmd_id & WMI_MGMT_CMD_MASK);
|
---|
286 |
|
---|
287 | if (clean_resp_buffer)
|
---|
288 | free(response_buffer);
|
---|
289 |
|
---|
290 | return rc;
|
---|
291 | }
|
---|