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