[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 htc.c
|
---|
| 30 | *
|
---|
| 31 | * Implementation of Atheros HTC communication.
|
---|
| 32 | *
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <usb/debug.h>
|
---|
| 36 | #include <byteorder.h>
|
---|
| 37 | #include <errno.h>
|
---|
[dd8ab1c] | 38 | #include <str_error.h>
|
---|
[59fa7ab] | 39 | #include "wmi.h"
|
---|
| 40 | #include "htc.h"
|
---|
| 41 | #include "nic/nic.h"
|
---|
| 42 | #include "ar9271.h"
|
---|
| 43 |
|
---|
[8a64320e] | 44 | /** HTC download pipes mapping.
|
---|
| 45 | *
|
---|
[59fa7ab] | 46 | * @param service_id Identification of WMI service.
|
---|
[8a64320e] | 47 | *
|
---|
[59fa7ab] | 48 | * @return Number of pipe used for this service.
|
---|
[8a64320e] | 49 | *
|
---|
[59fa7ab] | 50 | */
|
---|
| 51 | static inline uint8_t wmi_service_to_download_pipe(wmi_services_t service_id)
|
---|
| 52 | {
|
---|
| 53 | return (service_id == WMI_CONTROL_SERVICE) ? 3 : 2;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[8a64320e] | 56 | /** HTC upload pipes mapping.
|
---|
| 57 | *
|
---|
[59fa7ab] | 58 | * @param service_id Identification of WMI service.
|
---|
[8a64320e] | 59 | *
|
---|
[59fa7ab] | 60 | * @return Number of pipe used for this service.
|
---|
[8a64320e] | 61 | *
|
---|
[59fa7ab] | 62 | */
|
---|
| 63 | static inline uint8_t wmi_service_to_upload_pipe(wmi_services_t service_id)
|
---|
| 64 | {
|
---|
| 65 | return (service_id == WMI_CONTROL_SERVICE) ? 4 : 1;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[b7fd2a0] | 68 | errno_t htc_init_new_vif(htc_device_t *htc_device)
|
---|
[59fa7ab] | 69 | {
|
---|
| 70 | htc_vif_msg_t vif_msg;
|
---|
| 71 | htc_sta_msg_t sta_msg;
|
---|
[a35b458] | 72 |
|
---|
[59fa7ab] | 73 | memset(&vif_msg, 0, sizeof(htc_vif_msg_t));
|
---|
| 74 | memset(&sta_msg, 0, sizeof(htc_sta_msg_t));
|
---|
[a35b458] | 75 |
|
---|
[59fa7ab] | 76 | nic_address_t addr;
|
---|
[8a64320e] | 77 | nic_t *nic =
|
---|
| 78 | nic_get_from_ddf_dev(ieee80211_get_ddf_dev(htc_device->ieee80211_dev));
|
---|
[59fa7ab] | 79 | nic_query_address(nic, &addr);
|
---|
[a35b458] | 80 |
|
---|
[59fa7ab] | 81 | memcpy(&vif_msg.addr, &addr.address, ETH_ADDR);
|
---|
| 82 | memcpy(&sta_msg.addr, &addr.address, ETH_ADDR);
|
---|
[a35b458] | 83 |
|
---|
[8a64320e] | 84 | ieee80211_operating_mode_t op_mode =
|
---|
| 85 | ieee80211_query_current_op_mode(htc_device->ieee80211_dev);
|
---|
[a35b458] | 86 |
|
---|
[8a64320e] | 87 | switch (op_mode) {
|
---|
| 88 | case IEEE80211_OPMODE_ADHOC:
|
---|
| 89 | vif_msg.op_mode = HTC_OPMODE_ADHOC;
|
---|
| 90 | break;
|
---|
| 91 | case IEEE80211_OPMODE_AP:
|
---|
| 92 | vif_msg.op_mode = HTC_OPMODE_AP;
|
---|
| 93 | break;
|
---|
| 94 | case IEEE80211_OPMODE_MESH:
|
---|
| 95 | vif_msg.op_mode = HTC_OPMODE_MESH;
|
---|
| 96 | break;
|
---|
| 97 | case IEEE80211_OPMODE_STATION:
|
---|
| 98 | vif_msg.op_mode = HTC_OPMODE_STATION;
|
---|
| 99 | break;
|
---|
[59fa7ab] | 100 | }
|
---|
[a35b458] | 101 |
|
---|
[59fa7ab] | 102 | vif_msg.index = 0;
|
---|
| 103 | vif_msg.rts_thres = host2uint16_t_be(HTC_RTS_THRESHOLD);
|
---|
[a35b458] | 104 |
|
---|
[8a64320e] | 105 | wmi_send_command(htc_device, WMI_VAP_CREATE, (uint8_t *) &vif_msg,
|
---|
| 106 | sizeof(vif_msg), NULL);
|
---|
[a35b458] | 107 |
|
---|
[59fa7ab] | 108 | sta_msg.is_vif_sta = 1;
|
---|
| 109 | sta_msg.max_ampdu = host2uint16_t_be(0xFFFF);
|
---|
| 110 | sta_msg.sta_index = 0;
|
---|
| 111 | sta_msg.vif_index = 0;
|
---|
[a35b458] | 112 |
|
---|
[8a64320e] | 113 | wmi_send_command(htc_device, WMI_NODE_CREATE, (uint8_t *) &sta_msg,
|
---|
| 114 | sizeof(sta_msg), NULL);
|
---|
[a35b458] | 115 |
|
---|
[59fa7ab] | 116 | /* Write first 4 bytes of MAC address. */
|
---|
| 117 | uint32_t id0;
|
---|
| 118 | memcpy(&id0, &addr.address, 4);
|
---|
| 119 | id0 = host2uint32_t_le(id0);
|
---|
| 120 | wmi_reg_write(htc_device, AR9271_STATION_ID0, id0);
|
---|
[a35b458] | 121 |
|
---|
[59fa7ab] | 122 | /* Write last 2 bytes of MAC address (and preserve existing data). */
|
---|
| 123 | uint32_t id1;
|
---|
| 124 | wmi_reg_read(htc_device, AR9271_STATION_ID1, &id1);
|
---|
[a35b458] | 125 |
|
---|
[59fa7ab] | 126 | uint16_t id1_addr;
|
---|
| 127 | memcpy(&id1_addr, &addr.address[4], 2);
|
---|
| 128 | id1 = (id1 & ~AR9271_STATION_ID1_MASK) | host2uint16_t_le(id1_addr);
|
---|
| 129 | wmi_reg_write(htc_device, AR9271_STATION_ID1, id1);
|
---|
[a35b458] | 130 |
|
---|
[59fa7ab] | 131 | return EOK;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[8a64320e] | 134 | static void htc_config_frame_header(htc_frame_header_t *header,
|
---|
| 135 | size_t buffer_size, uint8_t endpoint_id)
|
---|
[59fa7ab] | 136 | {
|
---|
| 137 | memset(header, 0, sizeof(htc_frame_header_t));
|
---|
[a35b458] | 138 |
|
---|
[59fa7ab] | 139 | header->endpoint_id = endpoint_id;
|
---|
[8a64320e] | 140 | header->payload_length =
|
---|
| 141 | host2uint16_t_be(buffer_size - sizeof(htc_frame_header_t));
|
---|
[59fa7ab] | 142 | }
|
---|
| 143 |
|
---|
[8a64320e] | 144 | /** Send control HTC message to USB device.
|
---|
| 145 | *
|
---|
| 146 | * @param htc_device HTC device structure.
|
---|
| 147 | * @param buffer Buffer with data to be sent to USB device
|
---|
| 148 | * (without HTC frame header).
|
---|
[59fa7ab] | 149 | * @param buffer_size Size of buffer (including HTC frame header).
|
---|
| 150 | * @param endpoint_id Destination endpoint.
|
---|
[8a64320e] | 151 | *
|
---|
[cde999a] | 152 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 153 | *
|
---|
[59fa7ab] | 154 | */
|
---|
[b7fd2a0] | 155 | errno_t htc_send_control_message(htc_device_t *htc_device, void *buffer,
|
---|
[8a64320e] | 156 | size_t buffer_size, uint8_t endpoint_id)
|
---|
[59fa7ab] | 157 | {
|
---|
| 158 | htc_config_frame_header((htc_frame_header_t *) buffer, buffer_size,
|
---|
[8a64320e] | 159 | endpoint_id);
|
---|
[a35b458] | 160 |
|
---|
[59fa7ab] | 161 | ath_t *ath_device = htc_device->ath_device;
|
---|
[a35b458] | 162 |
|
---|
[8a64320e] | 163 | return ath_device->ops->send_ctrl_message(ath_device, buffer,
|
---|
| 164 | buffer_size);
|
---|
[59fa7ab] | 165 | }
|
---|
| 166 |
|
---|
[8a64320e] | 167 | /** Send data HTC message to USB device.
|
---|
| 168 | *
|
---|
| 169 | * @param htc_device HTC device structure.
|
---|
| 170 | * @param buffer Buffer with data to be sent to USB device
|
---|
| 171 | * (without HTC frame header).
|
---|
[59fa7ab] | 172 | * @param buffer_size Size of buffer (including HTC frame header).
|
---|
| 173 | * @param endpoint_id Destination endpoint.
|
---|
[8a64320e] | 174 | *
|
---|
[cde999a] | 175 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 176 | *
|
---|
[59fa7ab] | 177 | */
|
---|
[b7fd2a0] | 178 | errno_t htc_send_data_message(htc_device_t *htc_device, void *buffer,
|
---|
[8a64320e] | 179 | size_t buffer_size, uint8_t endpoint_id)
|
---|
[59fa7ab] | 180 | {
|
---|
| 181 | htc_config_frame_header((htc_frame_header_t *) buffer, buffer_size,
|
---|
[8a64320e] | 182 | endpoint_id);
|
---|
[a35b458] | 183 |
|
---|
[59fa7ab] | 184 | ath_t *ath_device = htc_device->ath_device;
|
---|
[a35b458] | 185 |
|
---|
[8a64320e] | 186 | return ath_device->ops->send_data_message(ath_device, buffer,
|
---|
| 187 | buffer_size);
|
---|
[59fa7ab] | 188 | }
|
---|
| 189 |
|
---|
[8a64320e] | 190 | /** Read HTC data message from USB device.
|
---|
| 191 | *
|
---|
| 192 | * @param htc_device HTC device structure.
|
---|
| 193 | * @param buffer Buffer where data from USB device
|
---|
| 194 | * will be stored.
|
---|
| 195 | * @param buffer_size Size of buffer.
|
---|
[59fa7ab] | 196 | * @param transferred_size Real size of read data.
|
---|
[8a64320e] | 197 | *
|
---|
[cde999a] | 198 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 199 | *
|
---|
[59fa7ab] | 200 | */
|
---|
[b7fd2a0] | 201 | errno_t htc_read_data_message(htc_device_t *htc_device, void *buffer,
|
---|
[8a64320e] | 202 | size_t buffer_size, size_t *transferred_size)
|
---|
[59fa7ab] | 203 | {
|
---|
| 204 | ath_t *ath_device = htc_device->ath_device;
|
---|
[a35b458] | 205 |
|
---|
[8a64320e] | 206 | return ath_device->ops->read_data_message(ath_device, buffer,
|
---|
| 207 | buffer_size, transferred_size);
|
---|
[59fa7ab] | 208 | }
|
---|
| 209 |
|
---|
[8a64320e] | 210 | /** Read HTC control message from USB device.
|
---|
| 211 | *
|
---|
| 212 | * @param htc_device HTC device structure.
|
---|
| 213 | * @param buffer Buffer where data from USB device
|
---|
| 214 | * will be stored.
|
---|
| 215 | * @param buffer_size Size of buffer.
|
---|
[59fa7ab] | 216 | * @param transferred_size Real size of read data.
|
---|
[8a64320e] | 217 | *
|
---|
[cde999a] | 218 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 219 | *
|
---|
[59fa7ab] | 220 | */
|
---|
[b7fd2a0] | 221 | errno_t htc_read_control_message(htc_device_t *htc_device, void *buffer,
|
---|
[8a64320e] | 222 | size_t buffer_size, size_t *transferred_size)
|
---|
[59fa7ab] | 223 | {
|
---|
| 224 | ath_t *ath_device = htc_device->ath_device;
|
---|
[a35b458] | 225 |
|
---|
[8a64320e] | 226 | return ath_device->ops->read_ctrl_message(ath_device, buffer,
|
---|
| 227 | buffer_size, transferred_size);
|
---|
[59fa7ab] | 228 | }
|
---|
| 229 |
|
---|
[8a64320e] | 230 | /** Initialize HTC service.
|
---|
| 231 | *
|
---|
| 232 | * @param htc_device HTC device structure.
|
---|
| 233 | * @param service_id Identification of WMI service.
|
---|
| 234 | * @param response_endpoint_no HTC endpoint to be used for
|
---|
| 235 | * this service.
|
---|
| 236 | *
|
---|
| 237 | * @return EOK if succeed, EINVAL when failed to connect service,
|
---|
[cde999a] | 238 | * error code otherwise.
|
---|
[8a64320e] | 239 | *
|
---|
[59fa7ab] | 240 | */
|
---|
[b7fd2a0] | 241 | static errno_t htc_connect_service(htc_device_t *htc_device,
|
---|
[59fa7ab] | 242 | wmi_services_t service_id, int *response_endpoint_no)
|
---|
| 243 | {
|
---|
[8a64320e] | 244 | size_t buffer_size = sizeof(htc_frame_header_t) +
|
---|
| 245 | sizeof(htc_service_msg_t);
|
---|
[59fa7ab] | 246 | void *buffer = malloc(buffer_size);
|
---|
| 247 | memset(buffer, 0, buffer_size);
|
---|
[a35b458] | 248 |
|
---|
[59fa7ab] | 249 | /* Fill service message structure. */
|
---|
| 250 | htc_service_msg_t *service_message = (htc_service_msg_t *)
|
---|
[8a64320e] | 251 | ((void *) buffer + sizeof(htc_frame_header_t));
|
---|
| 252 | service_message->service_id =
|
---|
| 253 | host2uint16_t_be(service_id);
|
---|
| 254 | service_message->message_id =
|
---|
| 255 | host2uint16_t_be(HTC_MESSAGE_CONNECT_SERVICE);
|
---|
| 256 | service_message->download_pipe_id =
|
---|
| 257 | wmi_service_to_download_pipe(service_id);
|
---|
| 258 | service_message->upload_pipe_id =
|
---|
| 259 | wmi_service_to_upload_pipe(service_id);
|
---|
[59fa7ab] | 260 | service_message->connection_flags = 0;
|
---|
[a35b458] | 261 |
|
---|
[59fa7ab] | 262 | /* Send HTC message. */
|
---|
[b7fd2a0] | 263 | errno_t rc = htc_send_control_message(htc_device, buffer, buffer_size,
|
---|
[8a64320e] | 264 | htc_device->endpoints.ctrl_endpoint);
|
---|
| 265 | if (rc != EOK) {
|
---|
[59fa7ab] | 266 | free(buffer);
|
---|
[dd8ab1c] | 267 | usb_log_error("Failed to send HTC message. Error: %s\n", str_error_name(rc));
|
---|
[59fa7ab] | 268 | return rc;
|
---|
| 269 | }
|
---|
[a35b458] | 270 |
|
---|
[59fa7ab] | 271 | free(buffer);
|
---|
[a35b458] | 272 |
|
---|
[59fa7ab] | 273 | buffer_size = htc_device->ath_device->ctrl_response_length;
|
---|
| 274 | buffer = malloc(buffer_size);
|
---|
[a35b458] | 275 |
|
---|
[59fa7ab] | 276 | /* Read response from device. */
|
---|
| 277 | rc = htc_read_control_message(htc_device, buffer, buffer_size, NULL);
|
---|
[8a64320e] | 278 | if (rc != EOK) {
|
---|
[59fa7ab] | 279 | free(buffer);
|
---|
| 280 | usb_log_error("Failed to receive HTC service connect response. "
|
---|
[dd8ab1c] | 281 | "Error: %s\n", str_error_name(rc));
|
---|
[59fa7ab] | 282 | return rc;
|
---|
| 283 | }
|
---|
[a35b458] | 284 |
|
---|
[59fa7ab] | 285 | htc_service_resp_msg_t *response_message = (htc_service_resp_msg_t *)
|
---|
[8a64320e] | 286 | ((void *) buffer + sizeof(htc_frame_header_t));
|
---|
[a35b458] | 287 |
|
---|
[8a64320e] | 288 | /*
|
---|
| 289 | * If service was successfully connected,
|
---|
| 290 | * write down HTC endpoint number that will
|
---|
| 291 | * be used for communication.
|
---|
| 292 | */
|
---|
| 293 | if (response_message->status == HTC_SERVICE_SUCCESS) {
|
---|
[59fa7ab] | 294 | *response_endpoint_no = response_message->endpoint_id;
|
---|
| 295 | rc = EOK;
|
---|
| 296 | } else {
|
---|
| 297 | usb_log_error("Failed to connect HTC service. "
|
---|
[8a64320e] | 298 | "Message status: %d\n", response_message->status);
|
---|
[59fa7ab] | 299 | rc = EINVAL;
|
---|
[8a64320e] | 300 | }
|
---|
[a35b458] | 301 |
|
---|
[59fa7ab] | 302 | free(buffer);
|
---|
[a35b458] | 303 |
|
---|
[59fa7ab] | 304 | return rc;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[8a64320e] | 307 | /** HTC credits initialization message.
|
---|
| 308 | *
|
---|
[59fa7ab] | 309 | * @param htc_device HTC device structure.
|
---|
[8a64320e] | 310 | *
|
---|
[cde999a] | 311 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 312 | *
|
---|
[59fa7ab] | 313 | */
|
---|
[b7fd2a0] | 314 | static errno_t htc_config_credits(htc_device_t *htc_device)
|
---|
[59fa7ab] | 315 | {
|
---|
[8a64320e] | 316 | size_t buffer_size = sizeof(htc_frame_header_t) +
|
---|
| 317 | sizeof(htc_config_msg_t);
|
---|
[59fa7ab] | 318 | void *buffer = malloc(buffer_size);
|
---|
| 319 | htc_config_msg_t *config_message = (htc_config_msg_t *)
|
---|
[8a64320e] | 320 | ((void *) buffer + sizeof(htc_frame_header_t));
|
---|
[a35b458] | 321 |
|
---|
[1b20da0] | 322 | config_message->message_id =
|
---|
[8a64320e] | 323 | host2uint16_t_be(HTC_MESSAGE_CONFIG);
|
---|
[59fa7ab] | 324 | /* Magic number to initialize device. */
|
---|
| 325 | config_message->credits = 33;
|
---|
| 326 | config_message->pipe_id = 1;
|
---|
[a35b458] | 327 |
|
---|
[59fa7ab] | 328 | /* Send HTC message. */
|
---|
[b7fd2a0] | 329 | errno_t rc = htc_send_control_message(htc_device, buffer, buffer_size,
|
---|
[8a64320e] | 330 | htc_device->endpoints.ctrl_endpoint);
|
---|
| 331 | if (rc != EOK) {
|
---|
[59fa7ab] | 332 | free(buffer);
|
---|
| 333 | usb_log_error("Failed to send HTC config message. "
|
---|
[dd8ab1c] | 334 | "Error: %s\n", str_error_name(rc));
|
---|
[59fa7ab] | 335 | return rc;
|
---|
| 336 | }
|
---|
[a35b458] | 337 |
|
---|
[59fa7ab] | 338 | free(buffer);
|
---|
[a35b458] | 339 |
|
---|
[59fa7ab] | 340 | buffer_size = htc_device->ath_device->ctrl_response_length;
|
---|
| 341 | buffer = malloc(buffer_size);
|
---|
[a35b458] | 342 |
|
---|
[59fa7ab] | 343 | /* Check response from device. */
|
---|
| 344 | rc = htc_read_control_message(htc_device, buffer, buffer_size, NULL);
|
---|
[8a64320e] | 345 | if (rc != EOK) {
|
---|
[59fa7ab] | 346 | usb_log_error("Failed to receive HTC config response message. "
|
---|
[dd8ab1c] | 347 | "Error: %s\n", str_error_name(rc));
|
---|
[59fa7ab] | 348 | }
|
---|
[a35b458] | 349 |
|
---|
[59fa7ab] | 350 | free(buffer);
|
---|
[a35b458] | 351 |
|
---|
[59fa7ab] | 352 | return rc;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[8a64320e] | 355 | /** HTC setup complete confirmation message.
|
---|
| 356 | *
|
---|
[59fa7ab] | 357 | * @param htc_device HTC device structure.
|
---|
[8a64320e] | 358 | *
|
---|
[cde999a] | 359 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 360 | *
|
---|
[59fa7ab] | 361 | */
|
---|
[b7fd2a0] | 362 | static errno_t htc_complete_setup(htc_device_t *htc_device)
|
---|
[59fa7ab] | 363 | {
|
---|
[8a64320e] | 364 | size_t buffer_size = sizeof(htc_frame_header_t) +
|
---|
| 365 | sizeof(htc_setup_complete_msg_t);
|
---|
[59fa7ab] | 366 | void *buffer = malloc(buffer_size);
|
---|
[8a64320e] | 367 | htc_setup_complete_msg_t *complete_message =
|
---|
| 368 | (htc_setup_complete_msg_t *)
|
---|
| 369 | ((void *) buffer + sizeof(htc_frame_header_t));
|
---|
[a35b458] | 370 |
|
---|
[8a64320e] | 371 | complete_message->message_id =
|
---|
| 372 | host2uint16_t_be(HTC_MESSAGE_SETUP_COMPLETE);
|
---|
[a35b458] | 373 |
|
---|
[59fa7ab] | 374 | /* Send HTC message. */
|
---|
[b7fd2a0] | 375 | errno_t rc = htc_send_control_message(htc_device, buffer, buffer_size,
|
---|
[8a64320e] | 376 | htc_device->endpoints.ctrl_endpoint);
|
---|
| 377 | if (rc != EOK)
|
---|
[59fa7ab] | 378 | usb_log_error("Failed to send HTC setup complete message. "
|
---|
[dd8ab1c] | 379 | "Error: %s\n", str_error_name(rc));
|
---|
[a35b458] | 380 |
|
---|
[59fa7ab] | 381 | free(buffer);
|
---|
[a35b458] | 382 |
|
---|
[59fa7ab] | 383 | return rc;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
[8a64320e] | 386 | /** Try to fetch ready message from device.
|
---|
| 387 | *
|
---|
[59fa7ab] | 388 | * Checks that firmware was successfully loaded on device side.
|
---|
[8a64320e] | 389 | *
|
---|
[59fa7ab] | 390 | * @param htc_device HTC device structure.
|
---|
[8a64320e] | 391 | *
|
---|
| 392 | * @return EOK if succeed, EINVAL if response error,
|
---|
[cde999a] | 393 | * error code otherwise.
|
---|
[8a64320e] | 394 | *
|
---|
[59fa7ab] | 395 | */
|
---|
[b7fd2a0] | 396 | static errno_t htc_check_ready(htc_device_t *htc_device)
|
---|
[59fa7ab] | 397 | {
|
---|
| 398 | size_t buffer_size = htc_device->ath_device->ctrl_response_length;
|
---|
| 399 | void *buffer = malloc(buffer_size);
|
---|
[a35b458] | 400 |
|
---|
[59fa7ab] | 401 | /* Read response from device. */
|
---|
[b7fd2a0] | 402 | errno_t rc = htc_read_control_message(htc_device, buffer, buffer_size,
|
---|
[8a64320e] | 403 | NULL);
|
---|
| 404 | if (rc != EOK) {
|
---|
[59fa7ab] | 405 | free(buffer);
|
---|
| 406 | usb_log_error("Failed to receive HTC check ready message. "
|
---|
[dd8ab1c] | 407 | "Error: %s\n", str_error_name(rc));
|
---|
[59fa7ab] | 408 | return rc;
|
---|
| 409 | }
|
---|
[a35b458] | 410 |
|
---|
[8a64320e] | 411 | uint16_t *message_id = (uint16_t *) ((void *) buffer +
|
---|
| 412 | sizeof(htc_frame_header_t));
|
---|
| 413 | if (uint16_t_be2host(*message_id) == HTC_MESSAGE_READY)
|
---|
[59fa7ab] | 414 | rc = EOK;
|
---|
[8a64320e] | 415 | else
|
---|
[59fa7ab] | 416 | rc = EINVAL;
|
---|
[a35b458] | 417 |
|
---|
[59fa7ab] | 418 | free(buffer);
|
---|
[a35b458] | 419 |
|
---|
[59fa7ab] | 420 | return rc;
|
---|
| 421 | }
|
---|
| 422 |
|
---|
[8a64320e] | 423 | /** Initialize HTC device structure.
|
---|
| 424 | *
|
---|
| 425 | * @param ath_device Atheros WiFi device connected
|
---|
| 426 | * with this HTC device.
|
---|
[59fa7ab] | 427 | * @param htc_device HTC device structure to be initialized.
|
---|
[8a64320e] | 428 | *
|
---|
[cde999a] | 429 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 430 | *
|
---|
[59fa7ab] | 431 | */
|
---|
[b7fd2a0] | 432 | errno_t htc_device_init(ath_t *ath_device, ieee80211_dev_t *ieee80211_dev,
|
---|
[8a64320e] | 433 | htc_device_t *htc_device)
|
---|
[59fa7ab] | 434 | {
|
---|
| 435 | fibril_mutex_initialize(&htc_device->rx_lock);
|
---|
| 436 | fibril_mutex_initialize(&htc_device->tx_lock);
|
---|
[a35b458] | 437 |
|
---|
[59fa7ab] | 438 | htc_device->endpoints.ctrl_endpoint = 0;
|
---|
[a35b458] | 439 |
|
---|
[59fa7ab] | 440 | htc_device->ath_device = ath_device;
|
---|
| 441 | htc_device->ieee80211_dev = ieee80211_dev;
|
---|
[a35b458] | 442 |
|
---|
[59fa7ab] | 443 | return EOK;
|
---|
| 444 | }
|
---|
| 445 |
|
---|
[8a64320e] | 446 | /** HTC communication initalization.
|
---|
| 447 | *
|
---|
[59fa7ab] | 448 | * @param htc_device HTC device structure.
|
---|
[8a64320e] | 449 | *
|
---|
[cde999a] | 450 | * @return EOK if succeed, error code otherwise.
|
---|
[8a64320e] | 451 | *
|
---|
[59fa7ab] | 452 | */
|
---|
[b7fd2a0] | 453 | errno_t htc_init(htc_device_t *htc_device)
|
---|
[59fa7ab] | 454 | {
|
---|
| 455 | /* First check ready message in device. */
|
---|
[b7fd2a0] | 456 | errno_t rc = htc_check_ready(htc_device);
|
---|
[8a64320e] | 457 | if (rc != EOK) {
|
---|
[59fa7ab] | 458 | usb_log_error("Device is not in ready state after loading "
|
---|
[8a64320e] | 459 | "firmware.\n");
|
---|
[59fa7ab] | 460 | return rc;
|
---|
| 461 | }
|
---|
[a35b458] | 462 |
|
---|
[8a64320e] | 463 | /*
|
---|
[59fa7ab] | 464 | * HTC services initialization start.
|
---|
| 465 | */
|
---|
| 466 | rc = htc_connect_service(htc_device, WMI_CONTROL_SERVICE,
|
---|
| 467 | &htc_device->endpoints.wmi_endpoint);
|
---|
[8a64320e] | 468 | if (rc != EOK) {
|
---|
[59fa7ab] | 469 | usb_log_error("Error while initalizing WMI service.\n");
|
---|
| 470 | return rc;
|
---|
| 471 | }
|
---|
[a35b458] | 472 |
|
---|
[59fa7ab] | 473 | rc = htc_connect_service(htc_device, WMI_BEACON_SERVICE,
|
---|
| 474 | &htc_device->endpoints.beacon_endpoint);
|
---|
[8a64320e] | 475 | if (rc != EOK) {
|
---|
[59fa7ab] | 476 | usb_log_error("Error while initalizing beacon service.\n");
|
---|
| 477 | return rc;
|
---|
| 478 | }
|
---|
[a35b458] | 479 |
|
---|
[59fa7ab] | 480 | rc = htc_connect_service(htc_device, WMI_CAB_SERVICE,
|
---|
| 481 | &htc_device->endpoints.cab_endpoint);
|
---|
[8a64320e] | 482 | if (rc != EOK) {
|
---|
[59fa7ab] | 483 | usb_log_error("Error while initalizing CAB service.\n");
|
---|
| 484 | return rc;
|
---|
| 485 | }
|
---|
[a35b458] | 486 |
|
---|
[59fa7ab] | 487 | rc = htc_connect_service(htc_device, WMI_UAPSD_SERVICE,
|
---|
| 488 | &htc_device->endpoints.uapsd_endpoint);
|
---|
[8a64320e] | 489 | if (rc != EOK) {
|
---|
[59fa7ab] | 490 | usb_log_error("Error while initalizing UAPSD service.\n");
|
---|
| 491 | return rc;
|
---|
| 492 | }
|
---|
[a35b458] | 493 |
|
---|
[59fa7ab] | 494 | rc = htc_connect_service(htc_device, WMI_MGMT_SERVICE,
|
---|
| 495 | &htc_device->endpoints.mgmt_endpoint);
|
---|
[8a64320e] | 496 | if (rc != EOK) {
|
---|
[59fa7ab] | 497 | usb_log_error("Error while initalizing MGMT service.\n");
|
---|
| 498 | return rc;
|
---|
| 499 | }
|
---|
[a35b458] | 500 |
|
---|
[59fa7ab] | 501 | rc = htc_connect_service(htc_device, WMI_DATA_BE_SERVICE,
|
---|
| 502 | &htc_device->endpoints.data_be_endpoint);
|
---|
[8a64320e] | 503 | if (rc != EOK) {
|
---|
[59fa7ab] | 504 | usb_log_error("Error while initalizing data best effort "
|
---|
| 505 | "service.\n");
|
---|
| 506 | return rc;
|
---|
| 507 | }
|
---|
[a35b458] | 508 |
|
---|
[59fa7ab] | 509 | rc = htc_connect_service(htc_device, WMI_DATA_BK_SERVICE,
|
---|
| 510 | &htc_device->endpoints.data_bk_endpoint);
|
---|
[8a64320e] | 511 | if (rc != EOK) {
|
---|
[59fa7ab] | 512 | usb_log_error("Error while initalizing data background "
|
---|
| 513 | "service.\n");
|
---|
| 514 | return rc;
|
---|
| 515 | }
|
---|
[a35b458] | 516 |
|
---|
[59fa7ab] | 517 | rc = htc_connect_service(htc_device, WMI_DATA_VIDEO_SERVICE,
|
---|
| 518 | &htc_device->endpoints.data_video_endpoint);
|
---|
[8a64320e] | 519 | if (rc != EOK) {
|
---|
[59fa7ab] | 520 | usb_log_error("Error while initalizing data video service.\n");
|
---|
| 521 | return rc;
|
---|
| 522 | }
|
---|
[a35b458] | 523 |
|
---|
[59fa7ab] | 524 | rc = htc_connect_service(htc_device, WMI_DATA_VOICE_SERVICE,
|
---|
| 525 | &htc_device->endpoints.data_voice_endpoint);
|
---|
[8a64320e] | 526 | if (rc != EOK) {
|
---|
[59fa7ab] | 527 | usb_log_error("Error while initalizing data voice service.\n");
|
---|
| 528 | return rc;
|
---|
| 529 | }
|
---|
[a35b458] | 530 |
|
---|
[8a64320e] | 531 | /*
|
---|
[59fa7ab] | 532 | * HTC services initialization end.
|
---|
| 533 | */
|
---|
[a35b458] | 534 |
|
---|
[59fa7ab] | 535 | /* Credits initialization message. */
|
---|
| 536 | rc = htc_config_credits(htc_device);
|
---|
[8a64320e] | 537 | if (rc != EOK) {
|
---|
[59fa7ab] | 538 | usb_log_error("Failed to send HTC config message.\n");
|
---|
| 539 | return rc;
|
---|
| 540 | }
|
---|
[a35b458] | 541 |
|
---|
[59fa7ab] | 542 | /* HTC setup complete confirmation message. */
|
---|
| 543 | rc = htc_complete_setup(htc_device);
|
---|
[8a64320e] | 544 | if (rc != EOK) {
|
---|
[59fa7ab] | 545 | usb_log_error("Failed to send HTC complete setup message.\n");
|
---|
| 546 | return rc;
|
---|
| 547 | }
|
---|
[a35b458] | 548 |
|
---|
[59fa7ab] | 549 | usb_log_info("HTC services initialization finished successfully.\n");
|
---|
[a35b458] | 550 |
|
---|
[59fa7ab] | 551 | return EOK;
|
---|
[8a64320e] | 552 | }
|
---|