source: mainline/uspace/drv/nic/ar9271/wmi.c@ 118a872

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 118a872 was 8a64320e, checked in by Martin Decky <martin@…>, 10 years ago

pre-merge coding style cleanup and code review

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