HelenOS sources
This source file includes following definitions.
- wmi_service_to_download_pipe
- wmi_service_to_upload_pipe
- htc_init_new_vif
- htc_config_frame_header
- htc_send_control_message
- htc_send_data_message
- htc_read_data_message
- htc_read_control_message
- htc_connect_service
- htc_config_credits
- htc_complete_setup
- htc_check_ready
- htc_device_init
- htc_init
#include <usb/debug.h>
#include <byteorder.h>
#include <errno.h>
#include <str_error.h>
#include "wmi.h"
#include "htc.h"
#include "nic/nic.h"
#include "ar9271.h"
static inline uint8_t wmi_service_to_download_pipe(wmi_services_t service_id)
{
return (service_id == WMI_CONTROL_SERVICE) ? 3 : 2;
}
static inline uint8_t wmi_service_to_upload_pipe(wmi_services_t service_id)
{
return (service_id == WMI_CONTROL_SERVICE) ? 4 : 1;
}
errno_t htc_init_new_vif(htc_device_t *htc_device)
{
htc_vif_msg_t vif_msg;
htc_sta_msg_t sta_msg;
memset(&vif_msg, 0, sizeof(htc_vif_msg_t));
memset(&sta_msg, 0, sizeof(htc_sta_msg_t));
nic_address_t addr;
nic_t *nic =
nic_get_from_ddf_dev(ieee80211_get_ddf_dev(htc_device->ieee80211_dev));
nic_query_address(nic, &addr);
memcpy(&vif_msg.addr, &addr.address, ETH_ADDR);
memcpy(&sta_msg.addr, &addr.address, ETH_ADDR);
ieee80211_operating_mode_t op_mode =
ieee80211_query_current_op_mode(htc_device->ieee80211_dev);
switch (op_mode) {
case IEEE80211_OPMODE_ADHOC:
vif_msg.op_mode = HTC_OPMODE_ADHOC;
break;
case IEEE80211_OPMODE_AP:
vif_msg.op_mode = HTC_OPMODE_AP;
break;
case IEEE80211_OPMODE_MESH:
vif_msg.op_mode = HTC_OPMODE_MESH;
break;
case IEEE80211_OPMODE_STATION:
vif_msg.op_mode = HTC_OPMODE_STATION;
break;
}
vif_msg.index = 0;
vif_msg.rts_thres = host2uint16_t_be(HTC_RTS_THRESHOLD);
wmi_send_command(htc_device, WMI_VAP_CREATE, (uint8_t *) &vif_msg,
sizeof(vif_msg), NULL);
sta_msg.is_vif_sta = 1;
sta_msg.max_ampdu = host2uint16_t_be(0xFFFF);
sta_msg.sta_index = 0;
sta_msg.vif_index = 0;
wmi_send_command(htc_device, WMI_NODE_CREATE, (uint8_t *) &sta_msg,
sizeof(sta_msg), NULL);
uint32_t id0;
memcpy(&id0, &addr.address, 4);
id0 = host2uint32_t_le(id0);
wmi_reg_write(htc_device, AR9271_STATION_ID0, id0);
uint32_t id1;
wmi_reg_read(htc_device, AR9271_STATION_ID1, &id1);
uint16_t id1_addr;
memcpy(&id1_addr, &addr.address[4], 2);
id1 = (id1 & ~AR9271_STATION_ID1_MASK) | host2uint16_t_le(id1_addr);
wmi_reg_write(htc_device, AR9271_STATION_ID1, id1);
return EOK;
}
static void htc_config_frame_header(htc_frame_header_t *header,
size_t buffer_size, uint8_t endpoint_id)
{
memset(header, 0, sizeof(htc_frame_header_t));
header->endpoint_id = endpoint_id;
header->payload_length =
host2uint16_t_be(buffer_size - sizeof(htc_frame_header_t));
}
errno_t htc_send_control_message(htc_device_t *htc_device, void *buffer,
size_t buffer_size, uint8_t endpoint_id)
{
htc_config_frame_header((htc_frame_header_t *) buffer, buffer_size,
endpoint_id);
ath_t *ath_device = htc_device->ath_device;
return ath_device->ops->send_ctrl_message(ath_device, buffer,
buffer_size);
}
errno_t htc_send_data_message(htc_device_t *htc_device, void *buffer,
size_t buffer_size, uint8_t endpoint_id)
{
htc_config_frame_header((htc_frame_header_t *) buffer, buffer_size,
endpoint_id);
ath_t *ath_device = htc_device->ath_device;
return ath_device->ops->send_data_message(ath_device, buffer,
buffer_size);
}
errno_t htc_read_data_message(htc_device_t *htc_device, void *buffer,
size_t buffer_size, size_t *transferred_size)
{
ath_t *ath_device = htc_device->ath_device;
return ath_device->ops->read_data_message(ath_device, buffer,
buffer_size, transferred_size);
}
errno_t htc_read_control_message(htc_device_t *htc_device, void *buffer,
size_t buffer_size, size_t *transferred_size)
{
ath_t *ath_device = htc_device->ath_device;
return ath_device->ops->read_ctrl_message(ath_device, buffer,
buffer_size, transferred_size);
}
static errno_t htc_connect_service(htc_device_t *htc_device,
wmi_services_t service_id, int *response_endpoint_no)
{
size_t buffer_size = sizeof(htc_frame_header_t) +
sizeof(htc_service_msg_t);
void *buffer = malloc(buffer_size);
memset(buffer, 0, buffer_size);
htc_service_msg_t *service_message = (htc_service_msg_t *)
((void *) buffer + sizeof(htc_frame_header_t));
service_message->service_id =
host2uint16_t_be(service_id);
service_message->message_id =
host2uint16_t_be(HTC_MESSAGE_CONNECT_SERVICE);
service_message->download_pipe_id =
wmi_service_to_download_pipe(service_id);
service_message->upload_pipe_id =
wmi_service_to_upload_pipe(service_id);
service_message->connection_flags = 0;
errno_t rc = htc_send_control_message(htc_device, buffer, buffer_size,
htc_device->endpoints.ctrl_endpoint);
if (rc != EOK) {
free(buffer);
usb_log_error("Failed to send HTC message. Error: %s\n", str_error_name(rc));
return rc;
}
free(buffer);
buffer_size = htc_device->ath_device->ctrl_response_length;
buffer = malloc(buffer_size);
rc = htc_read_control_message(htc_device, buffer, buffer_size, NULL);
if (rc != EOK) {
free(buffer);
usb_log_error("Failed to receive HTC service connect response. "
"Error: %s\n", str_error_name(rc));
return rc;
}
htc_service_resp_msg_t *response_message = (htc_service_resp_msg_t *)
((void *) buffer + sizeof(htc_frame_header_t));
if (response_message->status == HTC_SERVICE_SUCCESS) {
*response_endpoint_no = response_message->endpoint_id;
rc = EOK;
} else {
usb_log_error("Failed to connect HTC service. "
"Message status: %d\n", response_message->status);
rc = EINVAL;
}
free(buffer);
return rc;
}
static errno_t htc_config_credits(htc_device_t *htc_device)
{
size_t buffer_size = sizeof(htc_frame_header_t) +
sizeof(htc_config_msg_t);
void *buffer = malloc(buffer_size);
htc_config_msg_t *config_message = (htc_config_msg_t *)
((void *) buffer + sizeof(htc_frame_header_t));
config_message->message_id =
host2uint16_t_be(HTC_MESSAGE_CONFIG);
config_message->credits = 33;
config_message->pipe_id = 1;
errno_t rc = htc_send_control_message(htc_device, buffer, buffer_size,
htc_device->endpoints.ctrl_endpoint);
if (rc != EOK) {
free(buffer);
usb_log_error("Failed to send HTC config message. "
"Error: %s\n", str_error_name(rc));
return rc;
}
free(buffer);
buffer_size = htc_device->ath_device->ctrl_response_length;
buffer = malloc(buffer_size);
rc = htc_read_control_message(htc_device, buffer, buffer_size, NULL);
if (rc != EOK) {
usb_log_error("Failed to receive HTC config response message. "
"Error: %s\n", str_error_name(rc));
}
free(buffer);
return rc;
}
static errno_t htc_complete_setup(htc_device_t *htc_device)
{
size_t buffer_size = sizeof(htc_frame_header_t) +
sizeof(htc_setup_complete_msg_t);
void *buffer = malloc(buffer_size);
htc_setup_complete_msg_t *complete_message =
(htc_setup_complete_msg_t *)
((void *) buffer + sizeof(htc_frame_header_t));
complete_message->message_id =
host2uint16_t_be(HTC_MESSAGE_SETUP_COMPLETE);
errno_t rc = htc_send_control_message(htc_device, buffer, buffer_size,
htc_device->endpoints.ctrl_endpoint);
if (rc != EOK)
usb_log_error("Failed to send HTC setup complete message. "
"Error: %s\n", str_error_name(rc));
free(buffer);
return rc;
}
static errno_t htc_check_ready(htc_device_t *htc_device)
{
size_t buffer_size = htc_device->ath_device->ctrl_response_length;
void *buffer = malloc(buffer_size);
errno_t rc = htc_read_control_message(htc_device, buffer, buffer_size,
NULL);
if (rc != EOK) {
free(buffer);
usb_log_error("Failed to receive HTC check ready message. "
"Error: %s\n", str_error_name(rc));
return rc;
}
uint16_t *message_id = (uint16_t *) ((void *) buffer +
sizeof(htc_frame_header_t));
if (uint16_t_be2host(*message_id) == HTC_MESSAGE_READY)
rc = EOK;
else
rc = EINVAL;
free(buffer);
return rc;
}
errno_t htc_device_init(ath_t *ath_device, ieee80211_dev_t *ieee80211_dev,
htc_device_t *htc_device)
{
fibril_mutex_initialize(&htc_device->rx_lock);
fibril_mutex_initialize(&htc_device->tx_lock);
htc_device->endpoints.ctrl_endpoint = 0;
htc_device->ath_device = ath_device;
htc_device->ieee80211_dev = ieee80211_dev;
return EOK;
}
errno_t htc_init(htc_device_t *htc_device)
{
errno_t rc = htc_check_ready(htc_device);
if (rc != EOK) {
usb_log_error("Device is not in ready state after loading "
"firmware.\n");
return rc;
}
rc = htc_connect_service(htc_device, WMI_CONTROL_SERVICE,
&htc_device->endpoints.wmi_endpoint);
if (rc != EOK) {
usb_log_error("Error while initalizing WMI service.\n");
return rc;
}
rc = htc_connect_service(htc_device, WMI_BEACON_SERVICE,
&htc_device->endpoints.beacon_endpoint);
if (rc != EOK) {
usb_log_error("Error while initalizing beacon service.\n");
return rc;
}
rc = htc_connect_service(htc_device, WMI_CAB_SERVICE,
&htc_device->endpoints.cab_endpoint);
if (rc != EOK) {
usb_log_error("Error while initalizing CAB service.\n");
return rc;
}
rc = htc_connect_service(htc_device, WMI_UAPSD_SERVICE,
&htc_device->endpoints.uapsd_endpoint);
if (rc != EOK) {
usb_log_error("Error while initalizing UAPSD service.\n");
return rc;
}
rc = htc_connect_service(htc_device, WMI_MGMT_SERVICE,
&htc_device->endpoints.mgmt_endpoint);
if (rc != EOK) {
usb_log_error("Error while initalizing MGMT service.\n");
return rc;
}
rc = htc_connect_service(htc_device, WMI_DATA_BE_SERVICE,
&htc_device->endpoints.data_be_endpoint);
if (rc != EOK) {
usb_log_error("Error while initalizing data best effort "
"service.\n");
return rc;
}
rc = htc_connect_service(htc_device, WMI_DATA_BK_SERVICE,
&htc_device->endpoints.data_bk_endpoint);
if (rc != EOK) {
usb_log_error("Error while initalizing data background "
"service.\n");
return rc;
}
rc = htc_connect_service(htc_device, WMI_DATA_VIDEO_SERVICE,
&htc_device->endpoints.data_video_endpoint);
if (rc != EOK) {
usb_log_error("Error while initalizing data video service.\n");
return rc;
}
rc = htc_connect_service(htc_device, WMI_DATA_VOICE_SERVICE,
&htc_device->endpoints.data_voice_endpoint);
if (rc != EOK) {
usb_log_error("Error while initalizing data voice service.\n");
return rc;
}
rc = htc_config_credits(htc_device);
if (rc != EOK) {
usb_log_error("Failed to send HTC config message.\n");
return rc;
}
rc = htc_complete_setup(htc_device);
if (rc != EOK) {
usb_log_error("Failed to send HTC complete setup message.\n");
return rc;
}
usb_log_info("HTC services initialization finished successfully.\n");
return EOK;
}
HelenOS homepage, sources at GitHub