18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Intel Wireless WiMAX Connection 2400m
38c2ecf20Sopenharmony_ci * Miscellaneous control functions for managing the device
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
98c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
108c2ecf20Sopenharmony_ci * are met:
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *   * Redistributions of source code must retain the above copyright
138c2ecf20Sopenharmony_ci *     notice, this list of conditions and the following disclaimer.
148c2ecf20Sopenharmony_ci *   * Redistributions in binary form must reproduce the above copyright
158c2ecf20Sopenharmony_ci *     notice, this list of conditions and the following disclaimer in
168c2ecf20Sopenharmony_ci *     the documentation and/or other materials provided with the
178c2ecf20Sopenharmony_ci *     distribution.
188c2ecf20Sopenharmony_ci *   * Neither the name of Intel Corporation nor the names of its
198c2ecf20Sopenharmony_ci *     contributors may be used to endorse or promote products derived
208c2ecf20Sopenharmony_ci *     from this software without specific prior written permission.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
238c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
248c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
258c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
268c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
278c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
288c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
298c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
308c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
318c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
328c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * Intel Corporation <linux-wimax@intel.com>
368c2ecf20Sopenharmony_ci * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
378c2ecf20Sopenharmony_ci *  - Initial implementation
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * This is a collection of functions used to control the device (plus
408c2ecf20Sopenharmony_ci * a few helpers).
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * There are utilities for handling TLV buffers, hooks on the device's
438c2ecf20Sopenharmony_ci * reports to act on device changes of state [i2400m_report_hook()],
448c2ecf20Sopenharmony_ci * on acks to commands [i2400m_msg_ack_hook()], a helper for sending
458c2ecf20Sopenharmony_ci * commands to the device and blocking until a reply arrives
468c2ecf20Sopenharmony_ci * [i2400m_msg_to_dev()], a few high level commands for manipulating
478c2ecf20Sopenharmony_ci * the device state, powersving mode and configuration plus the
488c2ecf20Sopenharmony_ci * routines to setup the device once communication is stablished with
498c2ecf20Sopenharmony_ci * it [i2400m_dev_initialize()].
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * ROADMAP
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * i2400m_dev_initialize()       Called by i2400m_dev_start()
548c2ecf20Sopenharmony_ci *   i2400m_set_init_config()
558c2ecf20Sopenharmony_ci *   i2400m_cmd_get_state()
568c2ecf20Sopenharmony_ci * i2400m_dev_shutdown()        Called by i2400m_dev_stop()
578c2ecf20Sopenharmony_ci *   i2400m_reset()
588c2ecf20Sopenharmony_ci *
598c2ecf20Sopenharmony_ci * i2400m_{cmd,get,set}_*()
608c2ecf20Sopenharmony_ci *   i2400m_msg_to_dev()
618c2ecf20Sopenharmony_ci *   i2400m_msg_check_status()
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci * i2400m_report_hook()         Called on reception of an event
648c2ecf20Sopenharmony_ci *   i2400m_report_state_hook()
658c2ecf20Sopenharmony_ci *     i2400m_tlv_buffer_walk()
668c2ecf20Sopenharmony_ci *     i2400m_tlv_match()
678c2ecf20Sopenharmony_ci *     i2400m_report_tlv_system_state()
688c2ecf20Sopenharmony_ci *     i2400m_report_tlv_rf_switches_status()
698c2ecf20Sopenharmony_ci *     i2400m_report_tlv_media_status()
708c2ecf20Sopenharmony_ci *   i2400m_cmd_enter_powersave()
718c2ecf20Sopenharmony_ci *
728c2ecf20Sopenharmony_ci * i2400m_msg_ack_hook()        Called on reception of a reply to a
738c2ecf20Sopenharmony_ci *                              command, get or set
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#include <stdarg.h>
778c2ecf20Sopenharmony_ci#include "i2400m.h"
788c2ecf20Sopenharmony_ci#include <linux/kernel.h>
798c2ecf20Sopenharmony_ci#include <linux/slab.h>
808c2ecf20Sopenharmony_ci#include <linux/wimax/i2400m.h>
818c2ecf20Sopenharmony_ci#include <linux/export.h>
828c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define D_SUBMODULE control
868c2ecf20Sopenharmony_ci#include "debug-levels.h"
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic int i2400m_idle_mode_disabled;/* 0 (idle mode enabled) by default */
898c2ecf20Sopenharmony_cimodule_param_named(idle_mode_disabled, i2400m_idle_mode_disabled, int, 0644);
908c2ecf20Sopenharmony_ciMODULE_PARM_DESC(idle_mode_disabled,
918c2ecf20Sopenharmony_ci		 "If true, the device will not enable idle mode negotiation "
928c2ecf20Sopenharmony_ci		 "with the base station (when connected) to save power.");
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/* 0 (power saving enabled) by default */
958c2ecf20Sopenharmony_cistatic int i2400m_power_save_disabled;
968c2ecf20Sopenharmony_cimodule_param_named(power_save_disabled, i2400m_power_save_disabled, int, 0644);
978c2ecf20Sopenharmony_ciMODULE_PARM_DESC(power_save_disabled,
988c2ecf20Sopenharmony_ci		 "If true, the driver will not tell the device to enter "
998c2ecf20Sopenharmony_ci		 "power saving mode when it reports it is ready for it. "
1008c2ecf20Sopenharmony_ci		 "False by default (so the device is told to do power "
1018c2ecf20Sopenharmony_ci		 "saving).");
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic int i2400m_passive_mode;	/* 0 (passive mode disabled) by default */
1048c2ecf20Sopenharmony_cimodule_param_named(passive_mode, i2400m_passive_mode, int, 0644);
1058c2ecf20Sopenharmony_ciMODULE_PARM_DESC(passive_mode,
1068c2ecf20Sopenharmony_ci		 "If true, the driver will not do any device setup "
1078c2ecf20Sopenharmony_ci		 "and leave it up to user space, who must be properly "
1088c2ecf20Sopenharmony_ci		 "setup.");
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/*
1128c2ecf20Sopenharmony_ci * Return if a TLV is of a give type and size
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * @tlv_hdr: pointer to the TLV
1158c2ecf20Sopenharmony_ci * @tlv_type: type of the TLV we are looking for
1168c2ecf20Sopenharmony_ci * @tlv_size: expected size of the TLV we are looking for (if -1,
1178c2ecf20Sopenharmony_ci *            don't check the size). This includes the header
1188c2ecf20Sopenharmony_ci * Returns: 0 if the TLV matches
1198c2ecf20Sopenharmony_ci *          < 0 if it doesn't match at all
1208c2ecf20Sopenharmony_ci *          > 0 total TLV + payload size, if the type matches, but not
1218c2ecf20Sopenharmony_ci *              the size
1228c2ecf20Sopenharmony_ci */
1238c2ecf20Sopenharmony_cistatic
1248c2ecf20Sopenharmony_cissize_t i2400m_tlv_match(const struct i2400m_tlv_hdr *tlv,
1258c2ecf20Sopenharmony_ci		     enum i2400m_tlv tlv_type, ssize_t tlv_size)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	if (le16_to_cpu(tlv->type) != tlv_type)	/* Not our type? skip */
1288c2ecf20Sopenharmony_ci		return -1;
1298c2ecf20Sopenharmony_ci	if (tlv_size != -1
1308c2ecf20Sopenharmony_ci	    && le16_to_cpu(tlv->length) + sizeof(*tlv) != tlv_size) {
1318c2ecf20Sopenharmony_ci		size_t size = le16_to_cpu(tlv->length) + sizeof(*tlv);
1328c2ecf20Sopenharmony_ci		printk(KERN_WARNING "W: tlv type 0x%x mismatched because of "
1338c2ecf20Sopenharmony_ci		       "size (got %zu vs %zd expected)\n",
1348c2ecf20Sopenharmony_ci		       tlv_type, size, tlv_size);
1358c2ecf20Sopenharmony_ci		return size;
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci	return 0;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci/*
1428c2ecf20Sopenharmony_ci * Given a buffer of TLVs, iterate over them
1438c2ecf20Sopenharmony_ci *
1448c2ecf20Sopenharmony_ci * @i2400m: device instance
1458c2ecf20Sopenharmony_ci * @tlv_buf: pointer to the beginning of the TLV buffer
1468c2ecf20Sopenharmony_ci * @buf_size: buffer size in bytes
1478c2ecf20Sopenharmony_ci * @tlv_pos: seek position; this is assumed to be a pointer returned
1488c2ecf20Sopenharmony_ci *           by i2400m_tlv_buffer_walk() [and thus, validated]. The
1498c2ecf20Sopenharmony_ci *           TLV returned will be the one following this one.
1508c2ecf20Sopenharmony_ci *
1518c2ecf20Sopenharmony_ci * Usage:
1528c2ecf20Sopenharmony_ci *
1538c2ecf20Sopenharmony_ci * tlv_itr = NULL;
1548c2ecf20Sopenharmony_ci * while (tlv_itr = i2400m_tlv_buffer_walk(i2400m, buf, size, tlv_itr))  {
1558c2ecf20Sopenharmony_ci *         ...
1568c2ecf20Sopenharmony_ci *         // Do stuff with tlv_itr, DON'T MODIFY IT
1578c2ecf20Sopenharmony_ci *         ...
1588c2ecf20Sopenharmony_ci * }
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_cistatic
1618c2ecf20Sopenharmony_ciconst struct i2400m_tlv_hdr *i2400m_tlv_buffer_walk(
1628c2ecf20Sopenharmony_ci	struct i2400m *i2400m,
1638c2ecf20Sopenharmony_ci	const void *tlv_buf, size_t buf_size,
1648c2ecf20Sopenharmony_ci	const struct i2400m_tlv_hdr *tlv_pos)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
1678c2ecf20Sopenharmony_ci	const struct i2400m_tlv_hdr *tlv_top = tlv_buf + buf_size;
1688c2ecf20Sopenharmony_ci	size_t offset, length, avail_size;
1698c2ecf20Sopenharmony_ci	unsigned type;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	if (tlv_pos == NULL)	/* Take the first one? */
1728c2ecf20Sopenharmony_ci		tlv_pos = tlv_buf;
1738c2ecf20Sopenharmony_ci	else			/* Nope, the next one */
1748c2ecf20Sopenharmony_ci		tlv_pos = (void *) tlv_pos
1758c2ecf20Sopenharmony_ci			+ le16_to_cpu(tlv_pos->length) + sizeof(*tlv_pos);
1768c2ecf20Sopenharmony_ci	if (tlv_pos == tlv_top) {	/* buffer done */
1778c2ecf20Sopenharmony_ci		tlv_pos = NULL;
1788c2ecf20Sopenharmony_ci		goto error_beyond_end;
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci	if (tlv_pos > tlv_top) {
1818c2ecf20Sopenharmony_ci		tlv_pos = NULL;
1828c2ecf20Sopenharmony_ci		WARN_ON(1);
1838c2ecf20Sopenharmony_ci		goto error_beyond_end;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci	offset = (void *) tlv_pos - (void *) tlv_buf;
1868c2ecf20Sopenharmony_ci	avail_size = buf_size - offset;
1878c2ecf20Sopenharmony_ci	if (avail_size < sizeof(*tlv_pos)) {
1888c2ecf20Sopenharmony_ci		dev_err(dev, "HW BUG? tlv_buf %p [%zu bytes], tlv @%zu: "
1898c2ecf20Sopenharmony_ci			"short header\n", tlv_buf, buf_size, offset);
1908c2ecf20Sopenharmony_ci		goto error_short_header;
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci	type = le16_to_cpu(tlv_pos->type);
1938c2ecf20Sopenharmony_ci	length = le16_to_cpu(tlv_pos->length);
1948c2ecf20Sopenharmony_ci	if (avail_size < sizeof(*tlv_pos) + length) {
1958c2ecf20Sopenharmony_ci		dev_err(dev, "HW BUG? tlv_buf %p [%zu bytes], "
1968c2ecf20Sopenharmony_ci			"tlv type 0x%04x @%zu: "
1978c2ecf20Sopenharmony_ci			"short data (%zu bytes vs %zu needed)\n",
1988c2ecf20Sopenharmony_ci			tlv_buf, buf_size, type, offset, avail_size,
1998c2ecf20Sopenharmony_ci			sizeof(*tlv_pos) + length);
2008c2ecf20Sopenharmony_ci		goto error_short_header;
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_cierror_short_header:
2038c2ecf20Sopenharmony_cierror_beyond_end:
2048c2ecf20Sopenharmony_ci	return tlv_pos;
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci/*
2098c2ecf20Sopenharmony_ci * Find a TLV in a buffer of sequential TLVs
2108c2ecf20Sopenharmony_ci *
2118c2ecf20Sopenharmony_ci * @i2400m: device descriptor
2128c2ecf20Sopenharmony_ci * @tlv_hdr: pointer to the first TLV in the sequence
2138c2ecf20Sopenharmony_ci * @size: size of the buffer in bytes; all TLVs are assumed to fit
2148c2ecf20Sopenharmony_ci *        fully in the buffer (otherwise we'll complain).
2158c2ecf20Sopenharmony_ci * @tlv_type: type of the TLV we are looking for
2168c2ecf20Sopenharmony_ci * @tlv_size: expected size of the TLV we are looking for (if -1,
2178c2ecf20Sopenharmony_ci *            don't check the size). This includes the header
2188c2ecf20Sopenharmony_ci *
2198c2ecf20Sopenharmony_ci * Returns: NULL if the TLV is not found, otherwise a pointer to
2208c2ecf20Sopenharmony_ci *          it. If the sizes don't match, an error is printed and NULL
2218c2ecf20Sopenharmony_ci *          returned.
2228c2ecf20Sopenharmony_ci */
2238c2ecf20Sopenharmony_cistatic
2248c2ecf20Sopenharmony_ciconst struct i2400m_tlv_hdr *i2400m_tlv_find(
2258c2ecf20Sopenharmony_ci	struct i2400m *i2400m,
2268c2ecf20Sopenharmony_ci	const struct i2400m_tlv_hdr *tlv_hdr, size_t size,
2278c2ecf20Sopenharmony_ci	enum i2400m_tlv tlv_type, ssize_t tlv_size)
2288c2ecf20Sopenharmony_ci{
2298c2ecf20Sopenharmony_ci	ssize_t match;
2308c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
2318c2ecf20Sopenharmony_ci	const struct i2400m_tlv_hdr *tlv = NULL;
2328c2ecf20Sopenharmony_ci	while ((tlv = i2400m_tlv_buffer_walk(i2400m, tlv_hdr, size, tlv))) {
2338c2ecf20Sopenharmony_ci		match = i2400m_tlv_match(tlv, tlv_type, tlv_size);
2348c2ecf20Sopenharmony_ci		if (match == 0)		/* found it :) */
2358c2ecf20Sopenharmony_ci			break;
2368c2ecf20Sopenharmony_ci		if (match > 0)
2378c2ecf20Sopenharmony_ci			dev_warn(dev, "TLV type 0x%04x found with size "
2388c2ecf20Sopenharmony_ci				 "mismatch (%zu vs %zd needed)\n",
2398c2ecf20Sopenharmony_ci				 tlv_type, match, tlv_size);
2408c2ecf20Sopenharmony_ci	}
2418c2ecf20Sopenharmony_ci	return tlv;
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic const struct
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	char *msg;
2488c2ecf20Sopenharmony_ci	int errno;
2498c2ecf20Sopenharmony_ci} ms_to_errno[I2400M_MS_MAX] = {
2508c2ecf20Sopenharmony_ci	[I2400M_MS_DONE_OK] = { "", 0 },
2518c2ecf20Sopenharmony_ci	[I2400M_MS_DONE_IN_PROGRESS] = { "", 0 },
2528c2ecf20Sopenharmony_ci	[I2400M_MS_INVALID_OP] = { "invalid opcode", -ENOSYS },
2538c2ecf20Sopenharmony_ci	[I2400M_MS_BAD_STATE] = { "invalid state", -EILSEQ },
2548c2ecf20Sopenharmony_ci	[I2400M_MS_ILLEGAL_VALUE] = { "illegal value", -EINVAL },
2558c2ecf20Sopenharmony_ci	[I2400M_MS_MISSING_PARAMS] = { "missing parameters", -ENOMSG },
2568c2ecf20Sopenharmony_ci	[I2400M_MS_VERSION_ERROR] = { "bad version", -EIO },
2578c2ecf20Sopenharmony_ci	[I2400M_MS_ACCESSIBILITY_ERROR] = { "accesibility error", -EIO },
2588c2ecf20Sopenharmony_ci	[I2400M_MS_BUSY] = { "busy", -EBUSY },
2598c2ecf20Sopenharmony_ci	[I2400M_MS_CORRUPTED_TLV] = { "corrupted TLV", -EILSEQ },
2608c2ecf20Sopenharmony_ci	[I2400M_MS_UNINITIALIZED] = { "uninitialized", -EILSEQ },
2618c2ecf20Sopenharmony_ci	[I2400M_MS_UNKNOWN_ERROR] = { "unknown error", -EIO },
2628c2ecf20Sopenharmony_ci	[I2400M_MS_PRODUCTION_ERROR] = { "production error", -EIO },
2638c2ecf20Sopenharmony_ci	[I2400M_MS_NO_RF] = { "no RF", -EIO },
2648c2ecf20Sopenharmony_ci	[I2400M_MS_NOT_READY_FOR_POWERSAVE] =
2658c2ecf20Sopenharmony_ci		{ "not ready for powersave", -EACCES },
2668c2ecf20Sopenharmony_ci	[I2400M_MS_THERMAL_CRITICAL] = { "thermal critical", -EL3HLT },
2678c2ecf20Sopenharmony_ci};
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci/*
2718c2ecf20Sopenharmony_ci * i2400m_msg_check_status - translate a message's status code
2728c2ecf20Sopenharmony_ci *
2738c2ecf20Sopenharmony_ci * @i2400m: device descriptor
2748c2ecf20Sopenharmony_ci * @l3l4_hdr: message header
2758c2ecf20Sopenharmony_ci * @strbuf: buffer to place a formatted error message (unless NULL).
2768c2ecf20Sopenharmony_ci * @strbuf_size: max amount of available space; larger messages will
2778c2ecf20Sopenharmony_ci * be truncated.
2788c2ecf20Sopenharmony_ci *
2798c2ecf20Sopenharmony_ci * Returns: errno code corresponding to the status code in @l3l4_hdr
2808c2ecf20Sopenharmony_ci *          and a message in @strbuf describing the error.
2818c2ecf20Sopenharmony_ci */
2828c2ecf20Sopenharmony_ciint i2400m_msg_check_status(const struct i2400m_l3l4_hdr *l3l4_hdr,
2838c2ecf20Sopenharmony_ci			    char *strbuf, size_t strbuf_size)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	int result;
2868c2ecf20Sopenharmony_ci	enum i2400m_ms status = le16_to_cpu(l3l4_hdr->status);
2878c2ecf20Sopenharmony_ci	const char *str;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	if (status == 0)
2908c2ecf20Sopenharmony_ci		return 0;
2918c2ecf20Sopenharmony_ci	if (status >= ARRAY_SIZE(ms_to_errno)) {
2928c2ecf20Sopenharmony_ci		str = "unknown status code";
2938c2ecf20Sopenharmony_ci		result = -EBADR;
2948c2ecf20Sopenharmony_ci	} else {
2958c2ecf20Sopenharmony_ci		str = ms_to_errno[status].msg;
2968c2ecf20Sopenharmony_ci		result = ms_to_errno[status].errno;
2978c2ecf20Sopenharmony_ci	}
2988c2ecf20Sopenharmony_ci	if (strbuf)
2998c2ecf20Sopenharmony_ci		snprintf(strbuf, strbuf_size, "%s (%d)", str, status);
3008c2ecf20Sopenharmony_ci	return result;
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci/*
3058c2ecf20Sopenharmony_ci * Act on a TLV System State reported by the device
3068c2ecf20Sopenharmony_ci *
3078c2ecf20Sopenharmony_ci * @i2400m: device descriptor
3088c2ecf20Sopenharmony_ci * @ss: validated System State TLV
3098c2ecf20Sopenharmony_ci */
3108c2ecf20Sopenharmony_cistatic
3118c2ecf20Sopenharmony_civoid i2400m_report_tlv_system_state(struct i2400m *i2400m,
3128c2ecf20Sopenharmony_ci				    const struct i2400m_tlv_system_state *ss)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
3158c2ecf20Sopenharmony_ci	struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
3168c2ecf20Sopenharmony_ci	enum i2400m_system_state i2400m_state = le32_to_cpu(ss->state);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	d_fnstart(3, dev, "(i2400m %p ss %p [%u])\n", i2400m, ss, i2400m_state);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	if (i2400m->state != i2400m_state) {
3218c2ecf20Sopenharmony_ci		i2400m->state = i2400m_state;
3228c2ecf20Sopenharmony_ci		wake_up_all(&i2400m->state_wq);
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci	switch (i2400m_state) {
3258c2ecf20Sopenharmony_ci	case I2400M_SS_UNINITIALIZED:
3268c2ecf20Sopenharmony_ci	case I2400M_SS_INIT:
3278c2ecf20Sopenharmony_ci	case I2400M_SS_CONFIG:
3288c2ecf20Sopenharmony_ci	case I2400M_SS_PRODUCTION:
3298c2ecf20Sopenharmony_ci		wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
3308c2ecf20Sopenharmony_ci		break;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	case I2400M_SS_RF_OFF:
3338c2ecf20Sopenharmony_ci	case I2400M_SS_RF_SHUTDOWN:
3348c2ecf20Sopenharmony_ci		wimax_state_change(wimax_dev, WIMAX_ST_RADIO_OFF);
3358c2ecf20Sopenharmony_ci		break;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	case I2400M_SS_READY:
3388c2ecf20Sopenharmony_ci	case I2400M_SS_STANDBY:
3398c2ecf20Sopenharmony_ci	case I2400M_SS_SLEEPACTIVE:
3408c2ecf20Sopenharmony_ci		wimax_state_change(wimax_dev, WIMAX_ST_READY);
3418c2ecf20Sopenharmony_ci		break;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	case I2400M_SS_CONNECTING:
3448c2ecf20Sopenharmony_ci	case I2400M_SS_WIMAX_CONNECTED:
3458c2ecf20Sopenharmony_ci		wimax_state_change(wimax_dev, WIMAX_ST_READY);
3468c2ecf20Sopenharmony_ci		break;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	case I2400M_SS_SCAN:
3498c2ecf20Sopenharmony_ci	case I2400M_SS_OUT_OF_ZONE:
3508c2ecf20Sopenharmony_ci		wimax_state_change(wimax_dev, WIMAX_ST_SCANNING);
3518c2ecf20Sopenharmony_ci		break;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	case I2400M_SS_IDLE:
3548c2ecf20Sopenharmony_ci		d_printf(1, dev, "entering BS-negotiated idle mode\n");
3558c2ecf20Sopenharmony_ci		fallthrough;
3568c2ecf20Sopenharmony_ci	case I2400M_SS_DISCONNECTING:
3578c2ecf20Sopenharmony_ci	case I2400M_SS_DATA_PATH_CONNECTED:
3588c2ecf20Sopenharmony_ci		wimax_state_change(wimax_dev, WIMAX_ST_CONNECTED);
3598c2ecf20Sopenharmony_ci		break;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	default:
3628c2ecf20Sopenharmony_ci		/* Huh? just in case, shut it down */
3638c2ecf20Sopenharmony_ci		dev_err(dev, "HW BUG? unknown state %u: shutting down\n",
3648c2ecf20Sopenharmony_ci			i2400m_state);
3658c2ecf20Sopenharmony_ci		i2400m_reset(i2400m, I2400M_RT_WARM);
3668c2ecf20Sopenharmony_ci		break;
3678c2ecf20Sopenharmony_ci	}
3688c2ecf20Sopenharmony_ci	d_fnend(3, dev, "(i2400m %p ss %p [%u]) = void\n",
3698c2ecf20Sopenharmony_ci		i2400m, ss, i2400m_state);
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci/*
3748c2ecf20Sopenharmony_ci * Parse and act on a TLV Media Status sent by the device
3758c2ecf20Sopenharmony_ci *
3768c2ecf20Sopenharmony_ci * @i2400m: device descriptor
3778c2ecf20Sopenharmony_ci * @ms: validated Media Status TLV
3788c2ecf20Sopenharmony_ci *
3798c2ecf20Sopenharmony_ci * This will set the carrier up on down based on the device's link
3808c2ecf20Sopenharmony_ci * report. This is done asides of what the WiMAX stack does based on
3818c2ecf20Sopenharmony_ci * the device's state as sometimes we need to do a link-renew (the BS
3828c2ecf20Sopenharmony_ci * wants us to renew a DHCP lease, for example).
3838c2ecf20Sopenharmony_ci *
3848c2ecf20Sopenharmony_ci * In fact, doc says that every time we get a link-up, we should do a
3858c2ecf20Sopenharmony_ci * DHCP negotiation...
3868c2ecf20Sopenharmony_ci */
3878c2ecf20Sopenharmony_cistatic
3888c2ecf20Sopenharmony_civoid i2400m_report_tlv_media_status(struct i2400m *i2400m,
3898c2ecf20Sopenharmony_ci				    const struct i2400m_tlv_media_status *ms)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
3928c2ecf20Sopenharmony_ci	struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
3938c2ecf20Sopenharmony_ci	struct net_device *net_dev = wimax_dev->net_dev;
3948c2ecf20Sopenharmony_ci	enum i2400m_media_status status = le32_to_cpu(ms->media_status);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	d_fnstart(3, dev, "(i2400m %p ms %p [%u])\n", i2400m, ms, status);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	switch (status) {
3998c2ecf20Sopenharmony_ci	case I2400M_MEDIA_STATUS_LINK_UP:
4008c2ecf20Sopenharmony_ci		netif_carrier_on(net_dev);
4018c2ecf20Sopenharmony_ci		break;
4028c2ecf20Sopenharmony_ci	case I2400M_MEDIA_STATUS_LINK_DOWN:
4038c2ecf20Sopenharmony_ci		netif_carrier_off(net_dev);
4048c2ecf20Sopenharmony_ci		break;
4058c2ecf20Sopenharmony_ci	/*
4068c2ecf20Sopenharmony_ci	 * This is the network telling us we need to retrain the DHCP
4078c2ecf20Sopenharmony_ci	 * lease -- so far, we are trusting the WiMAX Network Service
4088c2ecf20Sopenharmony_ci	 * in user space to pick this up and poke the DHCP client.
4098c2ecf20Sopenharmony_ci	 */
4108c2ecf20Sopenharmony_ci	case I2400M_MEDIA_STATUS_LINK_RENEW:
4118c2ecf20Sopenharmony_ci		netif_carrier_on(net_dev);
4128c2ecf20Sopenharmony_ci		break;
4138c2ecf20Sopenharmony_ci	default:
4148c2ecf20Sopenharmony_ci		dev_err(dev, "HW BUG? unknown media status %u\n",
4158c2ecf20Sopenharmony_ci			status);
4168c2ecf20Sopenharmony_ci	}
4178c2ecf20Sopenharmony_ci	d_fnend(3, dev, "(i2400m %p ms %p [%u]) = void\n",
4188c2ecf20Sopenharmony_ci		i2400m, ms, status);
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci/*
4238c2ecf20Sopenharmony_ci * Process a TLV from a 'state report'
4248c2ecf20Sopenharmony_ci *
4258c2ecf20Sopenharmony_ci * @i2400m: device descriptor
4268c2ecf20Sopenharmony_ci * @tlv: pointer to the TLV header; it has been already validated for
4278c2ecf20Sopenharmony_ci *     consistent size.
4288c2ecf20Sopenharmony_ci * @tag: for error messages
4298c2ecf20Sopenharmony_ci *
4308c2ecf20Sopenharmony_ci * Act on the TLVs from a 'state report'.
4318c2ecf20Sopenharmony_ci */
4328c2ecf20Sopenharmony_cistatic
4338c2ecf20Sopenharmony_civoid i2400m_report_state_parse_tlv(struct i2400m *i2400m,
4348c2ecf20Sopenharmony_ci				   const struct i2400m_tlv_hdr *tlv,
4358c2ecf20Sopenharmony_ci				   const char *tag)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
4388c2ecf20Sopenharmony_ci	const struct i2400m_tlv_media_status *ms;
4398c2ecf20Sopenharmony_ci	const struct i2400m_tlv_system_state *ss;
4408c2ecf20Sopenharmony_ci	const struct i2400m_tlv_rf_switches_status *rfss;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	if (0 == i2400m_tlv_match(tlv, I2400M_TLV_SYSTEM_STATE, sizeof(*ss))) {
4438c2ecf20Sopenharmony_ci		ss = container_of(tlv, typeof(*ss), hdr);
4448c2ecf20Sopenharmony_ci		d_printf(2, dev, "%s: system state TLV "
4458c2ecf20Sopenharmony_ci			 "found (0x%04x), state 0x%08x\n",
4468c2ecf20Sopenharmony_ci			 tag, I2400M_TLV_SYSTEM_STATE,
4478c2ecf20Sopenharmony_ci			 le32_to_cpu(ss->state));
4488c2ecf20Sopenharmony_ci		i2400m_report_tlv_system_state(i2400m, ss);
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci	if (0 == i2400m_tlv_match(tlv, I2400M_TLV_RF_STATUS, sizeof(*rfss))) {
4518c2ecf20Sopenharmony_ci		rfss = container_of(tlv, typeof(*rfss), hdr);
4528c2ecf20Sopenharmony_ci		d_printf(2, dev, "%s: RF status TLV "
4538c2ecf20Sopenharmony_ci			 "found (0x%04x), sw 0x%02x hw 0x%02x\n",
4548c2ecf20Sopenharmony_ci			 tag, I2400M_TLV_RF_STATUS,
4558c2ecf20Sopenharmony_ci			 le32_to_cpu(rfss->sw_rf_switch),
4568c2ecf20Sopenharmony_ci			 le32_to_cpu(rfss->hw_rf_switch));
4578c2ecf20Sopenharmony_ci		i2400m_report_tlv_rf_switches_status(i2400m, rfss);
4588c2ecf20Sopenharmony_ci	}
4598c2ecf20Sopenharmony_ci	if (0 == i2400m_tlv_match(tlv, I2400M_TLV_MEDIA_STATUS, sizeof(*ms))) {
4608c2ecf20Sopenharmony_ci		ms = container_of(tlv, typeof(*ms), hdr);
4618c2ecf20Sopenharmony_ci		d_printf(2, dev, "%s: Media Status TLV: %u\n",
4628c2ecf20Sopenharmony_ci			 tag, le32_to_cpu(ms->media_status));
4638c2ecf20Sopenharmony_ci		i2400m_report_tlv_media_status(i2400m, ms);
4648c2ecf20Sopenharmony_ci	}
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci/*
4698c2ecf20Sopenharmony_ci * Parse a 'state report' and extract information
4708c2ecf20Sopenharmony_ci *
4718c2ecf20Sopenharmony_ci * @i2400m: device descriptor
4728c2ecf20Sopenharmony_ci * @l3l4_hdr: pointer to message; it has been already validated for
4738c2ecf20Sopenharmony_ci *            consistent size.
4748c2ecf20Sopenharmony_ci * @size: size of the message (header + payload). The header length
4758c2ecf20Sopenharmony_ci *        declaration is assumed to be congruent with @size (as in
4768c2ecf20Sopenharmony_ci *        sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
4778c2ecf20Sopenharmony_ci *
4788c2ecf20Sopenharmony_ci * Walk over the TLVs in a report state and act on them.
4798c2ecf20Sopenharmony_ci */
4808c2ecf20Sopenharmony_cistatic
4818c2ecf20Sopenharmony_civoid i2400m_report_state_hook(struct i2400m *i2400m,
4828c2ecf20Sopenharmony_ci			      const struct i2400m_l3l4_hdr *l3l4_hdr,
4838c2ecf20Sopenharmony_ci			      size_t size, const char *tag)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
4868c2ecf20Sopenharmony_ci	const struct i2400m_tlv_hdr *tlv;
4878c2ecf20Sopenharmony_ci	size_t tlv_size = le16_to_cpu(l3l4_hdr->length);
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	d_fnstart(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s)\n",
4908c2ecf20Sopenharmony_ci		  i2400m, l3l4_hdr, size, tag);
4918c2ecf20Sopenharmony_ci	tlv = NULL;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	while ((tlv = i2400m_tlv_buffer_walk(i2400m, &l3l4_hdr->pl,
4948c2ecf20Sopenharmony_ci					     tlv_size, tlv)))
4958c2ecf20Sopenharmony_ci		i2400m_report_state_parse_tlv(i2400m, tlv, tag);
4968c2ecf20Sopenharmony_ci	d_fnend(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s) = void\n",
4978c2ecf20Sopenharmony_ci		i2400m, l3l4_hdr, size, tag);
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci/*
5028c2ecf20Sopenharmony_ci * i2400m_report_hook - (maybe) act on a report
5038c2ecf20Sopenharmony_ci *
5048c2ecf20Sopenharmony_ci * @i2400m: device descriptor
5058c2ecf20Sopenharmony_ci * @l3l4_hdr: pointer to message; it has been already validated for
5068c2ecf20Sopenharmony_ci *            consistent size.
5078c2ecf20Sopenharmony_ci * @size: size of the message (header + payload). The header length
5088c2ecf20Sopenharmony_ci *        declaration is assumed to be congruent with @size (as in
5098c2ecf20Sopenharmony_ci *        sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
5108c2ecf20Sopenharmony_ci *
5118c2ecf20Sopenharmony_ci * Extract information we might need (like carrien on/off) from a
5128c2ecf20Sopenharmony_ci * device report.
5138c2ecf20Sopenharmony_ci */
5148c2ecf20Sopenharmony_civoid i2400m_report_hook(struct i2400m *i2400m,
5158c2ecf20Sopenharmony_ci			const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
5188c2ecf20Sopenharmony_ci	unsigned msg_type;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	d_fnstart(3, dev, "(i2400m %p l3l4_hdr %p size %zu)\n",
5218c2ecf20Sopenharmony_ci		  i2400m, l3l4_hdr, size);
5228c2ecf20Sopenharmony_ci	/* Chew on the message, we might need some information from
5238c2ecf20Sopenharmony_ci	 * here */
5248c2ecf20Sopenharmony_ci	msg_type = le16_to_cpu(l3l4_hdr->type);
5258c2ecf20Sopenharmony_ci	switch (msg_type) {
5268c2ecf20Sopenharmony_ci	case I2400M_MT_REPORT_STATE:	/* carrier detection... */
5278c2ecf20Sopenharmony_ci		i2400m_report_state_hook(i2400m,
5288c2ecf20Sopenharmony_ci					 l3l4_hdr, size, "REPORT STATE");
5298c2ecf20Sopenharmony_ci		break;
5308c2ecf20Sopenharmony_ci	/* If the device is ready for power save, then ask it to do
5318c2ecf20Sopenharmony_ci	 * it. */
5328c2ecf20Sopenharmony_ci	case I2400M_MT_REPORT_POWERSAVE_READY:	/* zzzzz */
5338c2ecf20Sopenharmony_ci		if (l3l4_hdr->status == cpu_to_le16(I2400M_MS_DONE_OK)) {
5348c2ecf20Sopenharmony_ci			if (i2400m_power_save_disabled)
5358c2ecf20Sopenharmony_ci				d_printf(1, dev, "ready for powersave, "
5368c2ecf20Sopenharmony_ci					 "not requesting (disabled by module "
5378c2ecf20Sopenharmony_ci					 "parameter)\n");
5388c2ecf20Sopenharmony_ci			else {
5398c2ecf20Sopenharmony_ci				d_printf(1, dev, "ready for powersave, "
5408c2ecf20Sopenharmony_ci					 "requesting\n");
5418c2ecf20Sopenharmony_ci				i2400m_cmd_enter_powersave(i2400m);
5428c2ecf20Sopenharmony_ci			}
5438c2ecf20Sopenharmony_ci		}
5448c2ecf20Sopenharmony_ci		break;
5458c2ecf20Sopenharmony_ci	}
5468c2ecf20Sopenharmony_ci	d_fnend(3, dev, "(i2400m %p l3l4_hdr %p size %zu) = void\n",
5478c2ecf20Sopenharmony_ci		i2400m, l3l4_hdr, size);
5488c2ecf20Sopenharmony_ci}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci/*
5528c2ecf20Sopenharmony_ci * i2400m_msg_ack_hook - process cmd/set/get ack for internal status
5538c2ecf20Sopenharmony_ci *
5548c2ecf20Sopenharmony_ci * @i2400m: device descriptor
5558c2ecf20Sopenharmony_ci * @l3l4_hdr: pointer to message; it has been already validated for
5568c2ecf20Sopenharmony_ci *            consistent size.
5578c2ecf20Sopenharmony_ci * @size: size of the message
5588c2ecf20Sopenharmony_ci *
5598c2ecf20Sopenharmony_ci * Extract information we might need from acks to commands and act on
5608c2ecf20Sopenharmony_ci * it. This is akin to i2400m_report_hook(). Note most of this
5618c2ecf20Sopenharmony_ci * processing should be done in the function that calls the
5628c2ecf20Sopenharmony_ci * command. This is here for some cases where it can't happen...
5638c2ecf20Sopenharmony_ci */
5648c2ecf20Sopenharmony_cistatic void i2400m_msg_ack_hook(struct i2400m *i2400m,
5658c2ecf20Sopenharmony_ci				 const struct i2400m_l3l4_hdr *l3l4_hdr,
5668c2ecf20Sopenharmony_ci				 size_t size)
5678c2ecf20Sopenharmony_ci{
5688c2ecf20Sopenharmony_ci	int result;
5698c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
5708c2ecf20Sopenharmony_ci	unsigned int ack_type;
5718c2ecf20Sopenharmony_ci	char strerr[32];
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	/* Chew on the message, we might need some information from
5748c2ecf20Sopenharmony_ci	 * here */
5758c2ecf20Sopenharmony_ci	ack_type = le16_to_cpu(l3l4_hdr->type);
5768c2ecf20Sopenharmony_ci	switch (ack_type) {
5778c2ecf20Sopenharmony_ci	case I2400M_MT_CMD_ENTER_POWERSAVE:
5788c2ecf20Sopenharmony_ci		/* This is just left here for the sake of example, as
5798c2ecf20Sopenharmony_ci		 * the processing is done somewhere else. */
5808c2ecf20Sopenharmony_ci		if (0) {
5818c2ecf20Sopenharmony_ci			result = i2400m_msg_check_status(
5828c2ecf20Sopenharmony_ci				l3l4_hdr, strerr, sizeof(strerr));
5838c2ecf20Sopenharmony_ci			if (result >= 0)
5848c2ecf20Sopenharmony_ci				d_printf(1, dev, "ready for power save: %zd\n",
5858c2ecf20Sopenharmony_ci					 size);
5868c2ecf20Sopenharmony_ci		}
5878c2ecf20Sopenharmony_ci		break;
5888c2ecf20Sopenharmony_ci	}
5898c2ecf20Sopenharmony_ci}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci/*
5938c2ecf20Sopenharmony_ci * i2400m_msg_size_check() - verify message size and header are congruent
5948c2ecf20Sopenharmony_ci *
5958c2ecf20Sopenharmony_ci * It is ok if the total message size is larger than the expected
5968c2ecf20Sopenharmony_ci * size, as there can be padding.
5978c2ecf20Sopenharmony_ci */
5988c2ecf20Sopenharmony_ciint i2400m_msg_size_check(struct i2400m *i2400m,
5998c2ecf20Sopenharmony_ci			  const struct i2400m_l3l4_hdr *l3l4_hdr,
6008c2ecf20Sopenharmony_ci			  size_t msg_size)
6018c2ecf20Sopenharmony_ci{
6028c2ecf20Sopenharmony_ci	int result;
6038c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
6048c2ecf20Sopenharmony_ci	size_t expected_size;
6058c2ecf20Sopenharmony_ci	d_fnstart(4, dev, "(i2400m %p l3l4_hdr %p msg_size %zu)\n",
6068c2ecf20Sopenharmony_ci		  i2400m, l3l4_hdr, msg_size);
6078c2ecf20Sopenharmony_ci	if (msg_size < sizeof(*l3l4_hdr)) {
6088c2ecf20Sopenharmony_ci		dev_err(dev, "bad size for message header "
6098c2ecf20Sopenharmony_ci			"(expected at least %zu, got %zu)\n",
6108c2ecf20Sopenharmony_ci			(size_t) sizeof(*l3l4_hdr), msg_size);
6118c2ecf20Sopenharmony_ci		result = -EIO;
6128c2ecf20Sopenharmony_ci		goto error_hdr_size;
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci	expected_size = le16_to_cpu(l3l4_hdr->length) + sizeof(*l3l4_hdr);
6158c2ecf20Sopenharmony_ci	if (msg_size < expected_size) {
6168c2ecf20Sopenharmony_ci		dev_err(dev, "bad size for message code 0x%04x (expected %zu, "
6178c2ecf20Sopenharmony_ci			"got %zu)\n", le16_to_cpu(l3l4_hdr->type),
6188c2ecf20Sopenharmony_ci			expected_size, msg_size);
6198c2ecf20Sopenharmony_ci		result = -EIO;
6208c2ecf20Sopenharmony_ci	} else
6218c2ecf20Sopenharmony_ci		result = 0;
6228c2ecf20Sopenharmony_cierror_hdr_size:
6238c2ecf20Sopenharmony_ci	d_fnend(4, dev,
6248c2ecf20Sopenharmony_ci		"(i2400m %p l3l4_hdr %p msg_size %zu) = %d\n",
6258c2ecf20Sopenharmony_ci		i2400m, l3l4_hdr, msg_size, result);
6268c2ecf20Sopenharmony_ci	return result;
6278c2ecf20Sopenharmony_ci}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci/*
6328c2ecf20Sopenharmony_ci * Cancel a wait for a command ACK
6338c2ecf20Sopenharmony_ci *
6348c2ecf20Sopenharmony_ci * @i2400m: device descriptor
6358c2ecf20Sopenharmony_ci * @code: [negative] errno code to cancel with (don't use
6368c2ecf20Sopenharmony_ci *     -EINPROGRESS)
6378c2ecf20Sopenharmony_ci *
6388c2ecf20Sopenharmony_ci * If there is an ack already filled out, free it.
6398c2ecf20Sopenharmony_ci */
6408c2ecf20Sopenharmony_civoid i2400m_msg_to_dev_cancel_wait(struct i2400m *i2400m, int code)
6418c2ecf20Sopenharmony_ci{
6428c2ecf20Sopenharmony_ci	struct sk_buff *ack_skb;
6438c2ecf20Sopenharmony_ci	unsigned long flags;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	spin_lock_irqsave(&i2400m->rx_lock, flags);
6468c2ecf20Sopenharmony_ci	ack_skb = i2400m->ack_skb;
6478c2ecf20Sopenharmony_ci	if (ack_skb && !IS_ERR(ack_skb))
6488c2ecf20Sopenharmony_ci		kfree_skb(ack_skb);
6498c2ecf20Sopenharmony_ci	i2400m->ack_skb = ERR_PTR(code);
6508c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&i2400m->rx_lock, flags);
6518c2ecf20Sopenharmony_ci}
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci/**
6558c2ecf20Sopenharmony_ci * i2400m_msg_to_dev - Send a control message to the device and get a response
6568c2ecf20Sopenharmony_ci *
6578c2ecf20Sopenharmony_ci * @i2400m: device descriptor
6588c2ecf20Sopenharmony_ci *
6598c2ecf20Sopenharmony_ci * @buf: pointer to the buffer containing the message to be sent; it
6608c2ecf20Sopenharmony_ci *           has to start with a &struct i2400M_l3l4_hdr and then
6618c2ecf20Sopenharmony_ci *           followed by the payload. Once this function returns, the
6628c2ecf20Sopenharmony_ci *           buffer can be reused.
6638c2ecf20Sopenharmony_ci *
6648c2ecf20Sopenharmony_ci * @buf_len: buffer size
6658c2ecf20Sopenharmony_ci *
6668c2ecf20Sopenharmony_ci * Returns:
6678c2ecf20Sopenharmony_ci *
6688c2ecf20Sopenharmony_ci * Pointer to skb containing the ack message. You need to check the
6698c2ecf20Sopenharmony_ci * pointer with IS_ERR(), as it might be an error code. Error codes
6708c2ecf20Sopenharmony_ci * could happen because:
6718c2ecf20Sopenharmony_ci *
6728c2ecf20Sopenharmony_ci *  - the message wasn't formatted correctly
6738c2ecf20Sopenharmony_ci *  - couldn't send the message
6748c2ecf20Sopenharmony_ci *  - failed waiting for a response
6758c2ecf20Sopenharmony_ci *  - the ack message wasn't formatted correctly
6768c2ecf20Sopenharmony_ci *
6778c2ecf20Sopenharmony_ci * The returned skb has been allocated with wimax_msg_to_user_alloc(),
6788c2ecf20Sopenharmony_ci * it contains the response in a netlink attribute and is ready to be
6798c2ecf20Sopenharmony_ci * passed up to user space with wimax_msg_to_user_send(). To access
6808c2ecf20Sopenharmony_ci * the payload and its length, use wimax_msg_{data,len}() on the skb.
6818c2ecf20Sopenharmony_ci *
6828c2ecf20Sopenharmony_ci * The skb has to be freed with kfree_skb() once done.
6838c2ecf20Sopenharmony_ci *
6848c2ecf20Sopenharmony_ci * Description:
6858c2ecf20Sopenharmony_ci *
6868c2ecf20Sopenharmony_ci * This function delivers a message/command to the device and waits
6878c2ecf20Sopenharmony_ci * for an ack to be received. The format is described in
6888c2ecf20Sopenharmony_ci * linux/wimax/i2400m.h. In summary, a command/get/set is followed by an
6898c2ecf20Sopenharmony_ci * ack.
6908c2ecf20Sopenharmony_ci *
6918c2ecf20Sopenharmony_ci * This function will not check the ack status, that's left up to the
6928c2ecf20Sopenharmony_ci * caller.  Once done with the ack skb, it has to be kfree_skb()ed.
6938c2ecf20Sopenharmony_ci *
6948c2ecf20Sopenharmony_ci * The i2400m handles only one message at the same time, thus we need
6958c2ecf20Sopenharmony_ci * the mutex to exclude other players.
6968c2ecf20Sopenharmony_ci *
6978c2ecf20Sopenharmony_ci * We write the message and then wait for an answer to come back. The
6988c2ecf20Sopenharmony_ci * RX path intercepts control messages and handles them in
6998c2ecf20Sopenharmony_ci * i2400m_rx_ctl(). Reports (notifications) are (maybe) processed
7008c2ecf20Sopenharmony_ci * locally and then forwarded (as needed) to user space on the WiMAX
7018c2ecf20Sopenharmony_ci * stack message pipe. Acks are saved and passed back to us through an
7028c2ecf20Sopenharmony_ci * skb in i2400m->ack_skb which is ready to be given to generic
7038c2ecf20Sopenharmony_ci * netlink if need be.
7048c2ecf20Sopenharmony_ci */
7058c2ecf20Sopenharmony_cistruct sk_buff *i2400m_msg_to_dev(struct i2400m *i2400m,
7068c2ecf20Sopenharmony_ci				  const void *buf, size_t buf_len)
7078c2ecf20Sopenharmony_ci{
7088c2ecf20Sopenharmony_ci	int result;
7098c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
7108c2ecf20Sopenharmony_ci	const struct i2400m_l3l4_hdr *msg_l3l4_hdr;
7118c2ecf20Sopenharmony_ci	struct sk_buff *ack_skb;
7128c2ecf20Sopenharmony_ci	const struct i2400m_l3l4_hdr *ack_l3l4_hdr;
7138c2ecf20Sopenharmony_ci	size_t ack_len;
7148c2ecf20Sopenharmony_ci	int ack_timeout;
7158c2ecf20Sopenharmony_ci	unsigned msg_type;
7168c2ecf20Sopenharmony_ci	unsigned long flags;
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	d_fnstart(3, dev, "(i2400m %p buf %p len %zu)\n",
7198c2ecf20Sopenharmony_ci		  i2400m, buf, buf_len);
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	rmb();		/* Make sure we see what i2400m_dev_reset_handle() */
7228c2ecf20Sopenharmony_ci	if (i2400m->boot_mode)
7238c2ecf20Sopenharmony_ci		return ERR_PTR(-EL3RST);
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	msg_l3l4_hdr = buf;
7268c2ecf20Sopenharmony_ci	/* Check msg & payload consistency */
7278c2ecf20Sopenharmony_ci	result = i2400m_msg_size_check(i2400m, msg_l3l4_hdr, buf_len);
7288c2ecf20Sopenharmony_ci	if (result < 0)
7298c2ecf20Sopenharmony_ci		goto error_bad_msg;
7308c2ecf20Sopenharmony_ci	msg_type = le16_to_cpu(msg_l3l4_hdr->type);
7318c2ecf20Sopenharmony_ci	d_printf(1, dev, "CMD/GET/SET 0x%04x %zu bytes\n",
7328c2ecf20Sopenharmony_ci		 msg_type, buf_len);
7338c2ecf20Sopenharmony_ci	d_dump(2, dev, buf, buf_len);
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	/* Setup the completion, ack_skb ("we are waiting") and send
7368c2ecf20Sopenharmony_ci	 * the message to the device */
7378c2ecf20Sopenharmony_ci	mutex_lock(&i2400m->msg_mutex);
7388c2ecf20Sopenharmony_ci	spin_lock_irqsave(&i2400m->rx_lock, flags);
7398c2ecf20Sopenharmony_ci	i2400m->ack_skb = ERR_PTR(-EINPROGRESS);
7408c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&i2400m->rx_lock, flags);
7418c2ecf20Sopenharmony_ci	init_completion(&i2400m->msg_completion);
7428c2ecf20Sopenharmony_ci	result = i2400m_tx(i2400m, buf, buf_len, I2400M_PT_CTRL);
7438c2ecf20Sopenharmony_ci	if (result < 0) {
7448c2ecf20Sopenharmony_ci		dev_err(dev, "can't send message 0x%04x: %d\n",
7458c2ecf20Sopenharmony_ci			le16_to_cpu(msg_l3l4_hdr->type), result);
7468c2ecf20Sopenharmony_ci		goto error_tx;
7478c2ecf20Sopenharmony_ci	}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	/* Some commands take longer to execute because of crypto ops,
7508c2ecf20Sopenharmony_ci	 * so we give them some more leeway on timeout */
7518c2ecf20Sopenharmony_ci	switch (msg_type) {
7528c2ecf20Sopenharmony_ci	case I2400M_MT_GET_TLS_OPERATION_RESULT:
7538c2ecf20Sopenharmony_ci	case I2400M_MT_CMD_SEND_EAP_RESPONSE:
7548c2ecf20Sopenharmony_ci		ack_timeout = 5 * HZ;
7558c2ecf20Sopenharmony_ci		break;
7568c2ecf20Sopenharmony_ci	default:
7578c2ecf20Sopenharmony_ci		ack_timeout = HZ;
7588c2ecf20Sopenharmony_ci	}
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	if (unlikely(i2400m->trace_msg_from_user))
7618c2ecf20Sopenharmony_ci		wimax_msg(&i2400m->wimax_dev, "echo", buf, buf_len, GFP_KERNEL);
7628c2ecf20Sopenharmony_ci	/* The RX path in rx.c will put any response for this message
7638c2ecf20Sopenharmony_ci	 * in i2400m->ack_skb and wake us up. If we cancel the wait,
7648c2ecf20Sopenharmony_ci	 * we need to change the value of i2400m->ack_skb to something
7658c2ecf20Sopenharmony_ci	 * not -EINPROGRESS so RX knows there is no one waiting. */
7668c2ecf20Sopenharmony_ci	result = wait_for_completion_interruptible_timeout(
7678c2ecf20Sopenharmony_ci		&i2400m->msg_completion, ack_timeout);
7688c2ecf20Sopenharmony_ci	if (result == 0) {
7698c2ecf20Sopenharmony_ci		dev_err(dev, "timeout waiting for reply to message 0x%04x\n",
7708c2ecf20Sopenharmony_ci			msg_type);
7718c2ecf20Sopenharmony_ci		result = -ETIMEDOUT;
7728c2ecf20Sopenharmony_ci		i2400m_msg_to_dev_cancel_wait(i2400m, result);
7738c2ecf20Sopenharmony_ci		goto error_wait_for_completion;
7748c2ecf20Sopenharmony_ci	} else if (result < 0) {
7758c2ecf20Sopenharmony_ci		dev_err(dev, "error waiting for reply to message 0x%04x: %d\n",
7768c2ecf20Sopenharmony_ci			msg_type, result);
7778c2ecf20Sopenharmony_ci		i2400m_msg_to_dev_cancel_wait(i2400m, result);
7788c2ecf20Sopenharmony_ci		goto error_wait_for_completion;
7798c2ecf20Sopenharmony_ci	}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	/* Pull out the ack data from i2400m->ack_skb -- see if it is
7828c2ecf20Sopenharmony_ci	 * an error and act accordingly */
7838c2ecf20Sopenharmony_ci	spin_lock_irqsave(&i2400m->rx_lock, flags);
7848c2ecf20Sopenharmony_ci	ack_skb = i2400m->ack_skb;
7858c2ecf20Sopenharmony_ci	if (IS_ERR(ack_skb))
7868c2ecf20Sopenharmony_ci		result = PTR_ERR(ack_skb);
7878c2ecf20Sopenharmony_ci	else
7888c2ecf20Sopenharmony_ci		result = 0;
7898c2ecf20Sopenharmony_ci	i2400m->ack_skb = NULL;
7908c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&i2400m->rx_lock, flags);
7918c2ecf20Sopenharmony_ci	if (result < 0)
7928c2ecf20Sopenharmony_ci		goto error_ack_status;
7938c2ecf20Sopenharmony_ci	ack_l3l4_hdr = wimax_msg_data_len(ack_skb, &ack_len);
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	/* Check the ack and deliver it if it is ok */
7968c2ecf20Sopenharmony_ci	if (unlikely(i2400m->trace_msg_from_user))
7978c2ecf20Sopenharmony_ci		wimax_msg(&i2400m->wimax_dev, "echo",
7988c2ecf20Sopenharmony_ci			  ack_l3l4_hdr, ack_len, GFP_KERNEL);
7998c2ecf20Sopenharmony_ci	result = i2400m_msg_size_check(i2400m, ack_l3l4_hdr, ack_len);
8008c2ecf20Sopenharmony_ci	if (result < 0) {
8018c2ecf20Sopenharmony_ci		dev_err(dev, "HW BUG? reply to message 0x%04x: %d\n",
8028c2ecf20Sopenharmony_ci			msg_type, result);
8038c2ecf20Sopenharmony_ci		goto error_bad_ack_len;
8048c2ecf20Sopenharmony_ci	}
8058c2ecf20Sopenharmony_ci	if (msg_type != le16_to_cpu(ack_l3l4_hdr->type)) {
8068c2ecf20Sopenharmony_ci		dev_err(dev, "HW BUG? bad reply 0x%04x to message 0x%04x\n",
8078c2ecf20Sopenharmony_ci			le16_to_cpu(ack_l3l4_hdr->type), msg_type);
8088c2ecf20Sopenharmony_ci		result = -EIO;
8098c2ecf20Sopenharmony_ci		goto error_bad_ack_type;
8108c2ecf20Sopenharmony_ci	}
8118c2ecf20Sopenharmony_ci	i2400m_msg_ack_hook(i2400m, ack_l3l4_hdr, ack_len);
8128c2ecf20Sopenharmony_ci	mutex_unlock(&i2400m->msg_mutex);
8138c2ecf20Sopenharmony_ci	d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %p\n",
8148c2ecf20Sopenharmony_ci		i2400m, buf, buf_len, ack_skb);
8158c2ecf20Sopenharmony_ci	return ack_skb;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_cierror_bad_ack_type:
8188c2ecf20Sopenharmony_cierror_bad_ack_len:
8198c2ecf20Sopenharmony_ci	kfree_skb(ack_skb);
8208c2ecf20Sopenharmony_cierror_ack_status:
8218c2ecf20Sopenharmony_cierror_wait_for_completion:
8228c2ecf20Sopenharmony_cierror_tx:
8238c2ecf20Sopenharmony_ci	mutex_unlock(&i2400m->msg_mutex);
8248c2ecf20Sopenharmony_cierror_bad_msg:
8258c2ecf20Sopenharmony_ci	d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %d\n",
8268c2ecf20Sopenharmony_ci		i2400m, buf, buf_len, result);
8278c2ecf20Sopenharmony_ci	return ERR_PTR(result);
8288c2ecf20Sopenharmony_ci}
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci/*
8328c2ecf20Sopenharmony_ci * Definitions for the Enter Power Save command
8338c2ecf20Sopenharmony_ci *
8348c2ecf20Sopenharmony_ci * The Enter Power Save command requests the device to go into power
8358c2ecf20Sopenharmony_ci * saving mode. The device will ack or nak the command depending on it
8368c2ecf20Sopenharmony_ci * being ready for it. If it acks, we tell the USB subsystem to
8378c2ecf20Sopenharmony_ci *
8388c2ecf20Sopenharmony_ci * As well, the device might request to go into power saving mode by
8398c2ecf20Sopenharmony_ci * sending a report (REPORT_POWERSAVE_READY), in which case, we issue
8408c2ecf20Sopenharmony_ci * this command. The hookups in the RX coder allow
8418c2ecf20Sopenharmony_ci */
8428c2ecf20Sopenharmony_cienum {
8438c2ecf20Sopenharmony_ci	I2400M_WAKEUP_ENABLED  = 0x01,
8448c2ecf20Sopenharmony_ci	I2400M_WAKEUP_DISABLED = 0x02,
8458c2ecf20Sopenharmony_ci	I2400M_TLV_TYPE_WAKEUP_MODE = 144,
8468c2ecf20Sopenharmony_ci};
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_cistruct i2400m_cmd_enter_power_save {
8498c2ecf20Sopenharmony_ci	struct i2400m_l3l4_hdr hdr;
8508c2ecf20Sopenharmony_ci	struct i2400m_tlv_hdr tlv;
8518c2ecf20Sopenharmony_ci	__le32 val;
8528c2ecf20Sopenharmony_ci} __packed;
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci/*
8568c2ecf20Sopenharmony_ci * Request entering power save
8578c2ecf20Sopenharmony_ci *
8588c2ecf20Sopenharmony_ci * This command is (mainly) executed when the device indicates that it
8598c2ecf20Sopenharmony_ci * is ready to go into powersave mode via a REPORT_POWERSAVE_READY.
8608c2ecf20Sopenharmony_ci */
8618c2ecf20Sopenharmony_ciint i2400m_cmd_enter_powersave(struct i2400m *i2400m)
8628c2ecf20Sopenharmony_ci{
8638c2ecf20Sopenharmony_ci	int result;
8648c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
8658c2ecf20Sopenharmony_ci	struct sk_buff *ack_skb;
8668c2ecf20Sopenharmony_ci	struct i2400m_cmd_enter_power_save *cmd;
8678c2ecf20Sopenharmony_ci	char strerr[32];
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	result = -ENOMEM;
8708c2ecf20Sopenharmony_ci	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
8718c2ecf20Sopenharmony_ci	if (cmd == NULL)
8728c2ecf20Sopenharmony_ci		goto error_alloc;
8738c2ecf20Sopenharmony_ci	cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_ENTER_POWERSAVE);
8748c2ecf20Sopenharmony_ci	cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
8758c2ecf20Sopenharmony_ci	cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
8768c2ecf20Sopenharmony_ci	cmd->tlv.type = cpu_to_le16(I2400M_TLV_TYPE_WAKEUP_MODE);
8778c2ecf20Sopenharmony_ci	cmd->tlv.length = cpu_to_le16(sizeof(cmd->val));
8788c2ecf20Sopenharmony_ci	cmd->val = cpu_to_le32(I2400M_WAKEUP_ENABLED);
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
8818c2ecf20Sopenharmony_ci	result = PTR_ERR(ack_skb);
8828c2ecf20Sopenharmony_ci	if (IS_ERR(ack_skb)) {
8838c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to issue 'Enter power save' command: %d\n",
8848c2ecf20Sopenharmony_ci			result);
8858c2ecf20Sopenharmony_ci		goto error_msg_to_dev;
8868c2ecf20Sopenharmony_ci	}
8878c2ecf20Sopenharmony_ci	result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
8888c2ecf20Sopenharmony_ci					 strerr, sizeof(strerr));
8898c2ecf20Sopenharmony_ci	if (result == -EACCES)
8908c2ecf20Sopenharmony_ci		d_printf(1, dev, "Cannot enter power save mode\n");
8918c2ecf20Sopenharmony_ci	else if (result < 0)
8928c2ecf20Sopenharmony_ci		dev_err(dev, "'Enter power save' (0x%04x) command failed: "
8938c2ecf20Sopenharmony_ci			"%d - %s\n", I2400M_MT_CMD_ENTER_POWERSAVE,
8948c2ecf20Sopenharmony_ci			result, strerr);
8958c2ecf20Sopenharmony_ci	else
8968c2ecf20Sopenharmony_ci		d_printf(1, dev, "device ready to power save\n");
8978c2ecf20Sopenharmony_ci	kfree_skb(ack_skb);
8988c2ecf20Sopenharmony_cierror_msg_to_dev:
8998c2ecf20Sopenharmony_ci	kfree(cmd);
9008c2ecf20Sopenharmony_cierror_alloc:
9018c2ecf20Sopenharmony_ci	return result;
9028c2ecf20Sopenharmony_ci}
9038c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2400m_cmd_enter_powersave);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci/*
9078c2ecf20Sopenharmony_ci * Definitions for getting device information
9088c2ecf20Sopenharmony_ci */
9098c2ecf20Sopenharmony_cienum {
9108c2ecf20Sopenharmony_ci	I2400M_TLV_DETAILED_DEVICE_INFO = 140
9118c2ecf20Sopenharmony_ci};
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci/**
9148c2ecf20Sopenharmony_ci * i2400m_get_device_info - Query the device for detailed device information
9158c2ecf20Sopenharmony_ci *
9168c2ecf20Sopenharmony_ci * @i2400m: device descriptor
9178c2ecf20Sopenharmony_ci *
9188c2ecf20Sopenharmony_ci * Returns: an skb whose skb->data points to a 'struct
9198c2ecf20Sopenharmony_ci *    i2400m_tlv_detailed_device_info'. When done, kfree_skb() it. The
9208c2ecf20Sopenharmony_ci *    skb is *guaranteed* to contain the whole TLV data structure.
9218c2ecf20Sopenharmony_ci *
9228c2ecf20Sopenharmony_ci *    On error, IS_ERR(skb) is true and ERR_PTR(skb) is the error
9238c2ecf20Sopenharmony_ci *    code.
9248c2ecf20Sopenharmony_ci */
9258c2ecf20Sopenharmony_cistruct sk_buff *i2400m_get_device_info(struct i2400m *i2400m)
9268c2ecf20Sopenharmony_ci{
9278c2ecf20Sopenharmony_ci	int result;
9288c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
9298c2ecf20Sopenharmony_ci	struct sk_buff *ack_skb;
9308c2ecf20Sopenharmony_ci	struct i2400m_l3l4_hdr *cmd;
9318c2ecf20Sopenharmony_ci	const struct i2400m_l3l4_hdr *ack;
9328c2ecf20Sopenharmony_ci	size_t ack_len;
9338c2ecf20Sopenharmony_ci	const struct i2400m_tlv_hdr *tlv;
9348c2ecf20Sopenharmony_ci	const struct i2400m_tlv_detailed_device_info *ddi;
9358c2ecf20Sopenharmony_ci	char strerr[32];
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	ack_skb = ERR_PTR(-ENOMEM);
9388c2ecf20Sopenharmony_ci	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
9398c2ecf20Sopenharmony_ci	if (cmd == NULL)
9408c2ecf20Sopenharmony_ci		goto error_alloc;
9418c2ecf20Sopenharmony_ci	cmd->type = cpu_to_le16(I2400M_MT_GET_DEVICE_INFO);
9428c2ecf20Sopenharmony_ci	cmd->length = 0;
9438c2ecf20Sopenharmony_ci	cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
9468c2ecf20Sopenharmony_ci	if (IS_ERR(ack_skb)) {
9478c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to issue 'get device info' command: %ld\n",
9488c2ecf20Sopenharmony_ci			PTR_ERR(ack_skb));
9498c2ecf20Sopenharmony_ci		goto error_msg_to_dev;
9508c2ecf20Sopenharmony_ci	}
9518c2ecf20Sopenharmony_ci	ack = wimax_msg_data_len(ack_skb, &ack_len);
9528c2ecf20Sopenharmony_ci	result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
9538c2ecf20Sopenharmony_ci	if (result < 0) {
9548c2ecf20Sopenharmony_ci		dev_err(dev, "'get device info' (0x%04x) command failed: "
9558c2ecf20Sopenharmony_ci			"%d - %s\n", I2400M_MT_GET_DEVICE_INFO, result,
9568c2ecf20Sopenharmony_ci			strerr);
9578c2ecf20Sopenharmony_ci		goto error_cmd_failed;
9588c2ecf20Sopenharmony_ci	}
9598c2ecf20Sopenharmony_ci	tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
9608c2ecf20Sopenharmony_ci			      I2400M_TLV_DETAILED_DEVICE_INFO, sizeof(*ddi));
9618c2ecf20Sopenharmony_ci	if (tlv == NULL) {
9628c2ecf20Sopenharmony_ci		dev_err(dev, "GET DEVICE INFO: "
9638c2ecf20Sopenharmony_ci			"detailed device info TLV not found (0x%04x)\n",
9648c2ecf20Sopenharmony_ci			I2400M_TLV_DETAILED_DEVICE_INFO);
9658c2ecf20Sopenharmony_ci		result = -EIO;
9668c2ecf20Sopenharmony_ci		goto error_no_tlv;
9678c2ecf20Sopenharmony_ci	}
9688c2ecf20Sopenharmony_ci	skb_pull(ack_skb, (void *) tlv - (void *) ack_skb->data);
9698c2ecf20Sopenharmony_cierror_msg_to_dev:
9708c2ecf20Sopenharmony_ci	kfree(cmd);
9718c2ecf20Sopenharmony_cierror_alloc:
9728c2ecf20Sopenharmony_ci	return ack_skb;
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_cierror_no_tlv:
9758c2ecf20Sopenharmony_cierror_cmd_failed:
9768c2ecf20Sopenharmony_ci	kfree_skb(ack_skb);
9778c2ecf20Sopenharmony_ci	kfree(cmd);
9788c2ecf20Sopenharmony_ci	return ERR_PTR(result);
9798c2ecf20Sopenharmony_ci}
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci/* Firmware interface versions we support */
9838c2ecf20Sopenharmony_cienum {
9848c2ecf20Sopenharmony_ci	I2400M_HDIv_MAJOR = 9,
9858c2ecf20Sopenharmony_ci	I2400M_HDIv_MINOR = 1,
9868c2ecf20Sopenharmony_ci	I2400M_HDIv_MINOR_2 = 2,
9878c2ecf20Sopenharmony_ci};
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci/**
9918c2ecf20Sopenharmony_ci * i2400m_firmware_check - check firmware versions are compatible with
9928c2ecf20Sopenharmony_ci * the driver
9938c2ecf20Sopenharmony_ci *
9948c2ecf20Sopenharmony_ci * @i2400m: device descriptor
9958c2ecf20Sopenharmony_ci *
9968c2ecf20Sopenharmony_ci * Returns: 0 if ok, < 0 errno code an error and a message in the
9978c2ecf20Sopenharmony_ci *    kernel log.
9988c2ecf20Sopenharmony_ci *
9998c2ecf20Sopenharmony_ci * Long function, but quite simple; first chunk launches the command
10008c2ecf20Sopenharmony_ci * and double checks the reply for the right TLV. Then we process the
10018c2ecf20Sopenharmony_ci * TLV (where the meat is).
10028c2ecf20Sopenharmony_ci *
10038c2ecf20Sopenharmony_ci * Once we process the TLV that gives us the firmware's interface
10048c2ecf20Sopenharmony_ci * version, we encode it and save it in i2400m->fw_version for future
10058c2ecf20Sopenharmony_ci * reference.
10068c2ecf20Sopenharmony_ci */
10078c2ecf20Sopenharmony_ciint i2400m_firmware_check(struct i2400m *i2400m)
10088c2ecf20Sopenharmony_ci{
10098c2ecf20Sopenharmony_ci	int result;
10108c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
10118c2ecf20Sopenharmony_ci	struct sk_buff *ack_skb;
10128c2ecf20Sopenharmony_ci	struct i2400m_l3l4_hdr *cmd;
10138c2ecf20Sopenharmony_ci	const struct i2400m_l3l4_hdr *ack;
10148c2ecf20Sopenharmony_ci	size_t ack_len;
10158c2ecf20Sopenharmony_ci	const struct i2400m_tlv_hdr *tlv;
10168c2ecf20Sopenharmony_ci	const struct i2400m_tlv_l4_message_versions *l4mv;
10178c2ecf20Sopenharmony_ci	char strerr[32];
10188c2ecf20Sopenharmony_ci	unsigned major, minor, branch;
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	result = -ENOMEM;
10218c2ecf20Sopenharmony_ci	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
10228c2ecf20Sopenharmony_ci	if (cmd == NULL)
10238c2ecf20Sopenharmony_ci		goto error_alloc;
10248c2ecf20Sopenharmony_ci	cmd->type = cpu_to_le16(I2400M_MT_GET_LM_VERSION);
10258c2ecf20Sopenharmony_ci	cmd->length = 0;
10268c2ecf20Sopenharmony_ci	cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
10298c2ecf20Sopenharmony_ci	if (IS_ERR(ack_skb)) {
10308c2ecf20Sopenharmony_ci		result = PTR_ERR(ack_skb);
10318c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to issue 'get lm version' command: %-d\n",
10328c2ecf20Sopenharmony_ci			result);
10338c2ecf20Sopenharmony_ci		goto error_msg_to_dev;
10348c2ecf20Sopenharmony_ci	}
10358c2ecf20Sopenharmony_ci	ack = wimax_msg_data_len(ack_skb, &ack_len);
10368c2ecf20Sopenharmony_ci	result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
10378c2ecf20Sopenharmony_ci	if (result < 0) {
10388c2ecf20Sopenharmony_ci		dev_err(dev, "'get lm version' (0x%04x) command failed: "
10398c2ecf20Sopenharmony_ci			"%d - %s\n", I2400M_MT_GET_LM_VERSION, result,
10408c2ecf20Sopenharmony_ci			strerr);
10418c2ecf20Sopenharmony_ci		goto error_cmd_failed;
10428c2ecf20Sopenharmony_ci	}
10438c2ecf20Sopenharmony_ci	tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
10448c2ecf20Sopenharmony_ci			      I2400M_TLV_L4_MESSAGE_VERSIONS, sizeof(*l4mv));
10458c2ecf20Sopenharmony_ci	if (tlv == NULL) {
10468c2ecf20Sopenharmony_ci		dev_err(dev, "get lm version: TLV not found (0x%04x)\n",
10478c2ecf20Sopenharmony_ci			I2400M_TLV_L4_MESSAGE_VERSIONS);
10488c2ecf20Sopenharmony_ci		result = -EIO;
10498c2ecf20Sopenharmony_ci		goto error_no_tlv;
10508c2ecf20Sopenharmony_ci	}
10518c2ecf20Sopenharmony_ci	l4mv = container_of(tlv, typeof(*l4mv), hdr);
10528c2ecf20Sopenharmony_ci	major = le16_to_cpu(l4mv->major);
10538c2ecf20Sopenharmony_ci	minor = le16_to_cpu(l4mv->minor);
10548c2ecf20Sopenharmony_ci	branch = le16_to_cpu(l4mv->branch);
10558c2ecf20Sopenharmony_ci	result = -EINVAL;
10568c2ecf20Sopenharmony_ci	if (major != I2400M_HDIv_MAJOR) {
10578c2ecf20Sopenharmony_ci		dev_err(dev, "unsupported major fw version "
10588c2ecf20Sopenharmony_ci			"%u.%u.%u\n", major, minor, branch);
10598c2ecf20Sopenharmony_ci		goto error_bad_major;
10608c2ecf20Sopenharmony_ci	}
10618c2ecf20Sopenharmony_ci	result = 0;
10628c2ecf20Sopenharmony_ci	if (minor > I2400M_HDIv_MINOR_2 || minor < I2400M_HDIv_MINOR)
10638c2ecf20Sopenharmony_ci		dev_warn(dev, "untested minor fw version %u.%u.%u\n",
10648c2ecf20Sopenharmony_ci			 major, minor, branch);
10658c2ecf20Sopenharmony_ci	/* Yes, we ignore the branch -- we don't have to track it */
10668c2ecf20Sopenharmony_ci	i2400m->fw_version = major << 16 | minor;
10678c2ecf20Sopenharmony_ci	dev_info(dev, "firmware interface version %u.%u.%u\n",
10688c2ecf20Sopenharmony_ci		 major, minor, branch);
10698c2ecf20Sopenharmony_cierror_bad_major:
10708c2ecf20Sopenharmony_cierror_no_tlv:
10718c2ecf20Sopenharmony_cierror_cmd_failed:
10728c2ecf20Sopenharmony_ci	kfree_skb(ack_skb);
10738c2ecf20Sopenharmony_cierror_msg_to_dev:
10748c2ecf20Sopenharmony_ci	kfree(cmd);
10758c2ecf20Sopenharmony_cierror_alloc:
10768c2ecf20Sopenharmony_ci	return result;
10778c2ecf20Sopenharmony_ci}
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci/*
10818c2ecf20Sopenharmony_ci * Send an DoExitIdle command to the device to ask it to go out of
10828c2ecf20Sopenharmony_ci * basestation-idle mode.
10838c2ecf20Sopenharmony_ci *
10848c2ecf20Sopenharmony_ci * @i2400m: device descriptor
10858c2ecf20Sopenharmony_ci *
10868c2ecf20Sopenharmony_ci * This starts a renegotiation with the basestation that might involve
10878c2ecf20Sopenharmony_ci * another crypto handshake with user space.
10888c2ecf20Sopenharmony_ci *
10898c2ecf20Sopenharmony_ci * Returns: 0 if ok, < 0 errno code on error.
10908c2ecf20Sopenharmony_ci */
10918c2ecf20Sopenharmony_ciint i2400m_cmd_exit_idle(struct i2400m *i2400m)
10928c2ecf20Sopenharmony_ci{
10938c2ecf20Sopenharmony_ci	int result;
10948c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
10958c2ecf20Sopenharmony_ci	struct sk_buff *ack_skb;
10968c2ecf20Sopenharmony_ci	struct i2400m_l3l4_hdr *cmd;
10978c2ecf20Sopenharmony_ci	char strerr[32];
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	result = -ENOMEM;
11008c2ecf20Sopenharmony_ci	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
11018c2ecf20Sopenharmony_ci	if (cmd == NULL)
11028c2ecf20Sopenharmony_ci		goto error_alloc;
11038c2ecf20Sopenharmony_ci	cmd->type = cpu_to_le16(I2400M_MT_CMD_EXIT_IDLE);
11048c2ecf20Sopenharmony_ci	cmd->length = 0;
11058c2ecf20Sopenharmony_ci	cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci	ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
11088c2ecf20Sopenharmony_ci	result = PTR_ERR(ack_skb);
11098c2ecf20Sopenharmony_ci	if (IS_ERR(ack_skb)) {
11108c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to issue 'exit idle' command: %d\n",
11118c2ecf20Sopenharmony_ci			result);
11128c2ecf20Sopenharmony_ci		goto error_msg_to_dev;
11138c2ecf20Sopenharmony_ci	}
11148c2ecf20Sopenharmony_ci	result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
11158c2ecf20Sopenharmony_ci					 strerr, sizeof(strerr));
11168c2ecf20Sopenharmony_ci	kfree_skb(ack_skb);
11178c2ecf20Sopenharmony_cierror_msg_to_dev:
11188c2ecf20Sopenharmony_ci	kfree(cmd);
11198c2ecf20Sopenharmony_cierror_alloc:
11208c2ecf20Sopenharmony_ci	return result;
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci}
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci/*
11268c2ecf20Sopenharmony_ci * Query the device for its state, update the WiMAX stack's idea of it
11278c2ecf20Sopenharmony_ci *
11288c2ecf20Sopenharmony_ci * @i2400m: device descriptor
11298c2ecf20Sopenharmony_ci *
11308c2ecf20Sopenharmony_ci * Returns: 0 if ok, < 0 errno code on error.
11318c2ecf20Sopenharmony_ci *
11328c2ecf20Sopenharmony_ci * Executes a 'Get State' command and parses the returned
11338c2ecf20Sopenharmony_ci * TLVs.
11348c2ecf20Sopenharmony_ci *
11358c2ecf20Sopenharmony_ci * Because this is almost identical to a 'Report State', we use
11368c2ecf20Sopenharmony_ci * i2400m_report_state_hook() to parse the answer. This will set the
11378c2ecf20Sopenharmony_ci * carrier state, as well as the RF Kill switches state.
11388c2ecf20Sopenharmony_ci */
11398c2ecf20Sopenharmony_cistatic int i2400m_cmd_get_state(struct i2400m *i2400m)
11408c2ecf20Sopenharmony_ci{
11418c2ecf20Sopenharmony_ci	int result;
11428c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
11438c2ecf20Sopenharmony_ci	struct sk_buff *ack_skb;
11448c2ecf20Sopenharmony_ci	struct i2400m_l3l4_hdr *cmd;
11458c2ecf20Sopenharmony_ci	const struct i2400m_l3l4_hdr *ack;
11468c2ecf20Sopenharmony_ci	size_t ack_len;
11478c2ecf20Sopenharmony_ci	char strerr[32];
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci	result = -ENOMEM;
11508c2ecf20Sopenharmony_ci	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
11518c2ecf20Sopenharmony_ci	if (cmd == NULL)
11528c2ecf20Sopenharmony_ci		goto error_alloc;
11538c2ecf20Sopenharmony_ci	cmd->type = cpu_to_le16(I2400M_MT_GET_STATE);
11548c2ecf20Sopenharmony_ci	cmd->length = 0;
11558c2ecf20Sopenharmony_ci	cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
11588c2ecf20Sopenharmony_ci	if (IS_ERR(ack_skb)) {
11598c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to issue 'get state' command: %ld\n",
11608c2ecf20Sopenharmony_ci			PTR_ERR(ack_skb));
11618c2ecf20Sopenharmony_ci		result = PTR_ERR(ack_skb);
11628c2ecf20Sopenharmony_ci		goto error_msg_to_dev;
11638c2ecf20Sopenharmony_ci	}
11648c2ecf20Sopenharmony_ci	ack = wimax_msg_data_len(ack_skb, &ack_len);
11658c2ecf20Sopenharmony_ci	result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
11668c2ecf20Sopenharmony_ci	if (result < 0) {
11678c2ecf20Sopenharmony_ci		dev_err(dev, "'get state' (0x%04x) command failed: "
11688c2ecf20Sopenharmony_ci			"%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
11698c2ecf20Sopenharmony_ci		goto error_cmd_failed;
11708c2ecf20Sopenharmony_ci	}
11718c2ecf20Sopenharmony_ci	i2400m_report_state_hook(i2400m, ack, ack_len - sizeof(*ack),
11728c2ecf20Sopenharmony_ci				 "GET STATE");
11738c2ecf20Sopenharmony_ci	result = 0;
11748c2ecf20Sopenharmony_ci	kfree_skb(ack_skb);
11758c2ecf20Sopenharmony_cierror_cmd_failed:
11768c2ecf20Sopenharmony_cierror_msg_to_dev:
11778c2ecf20Sopenharmony_ci	kfree(cmd);
11788c2ecf20Sopenharmony_cierror_alloc:
11798c2ecf20Sopenharmony_ci	return result;
11808c2ecf20Sopenharmony_ci}
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci/**
11838c2ecf20Sopenharmony_ci * Set basic configuration settings
11848c2ecf20Sopenharmony_ci *
11858c2ecf20Sopenharmony_ci * @i2400m: device descriptor
11868c2ecf20Sopenharmony_ci * @args: array of pointers to the TLV headers to send for
11878c2ecf20Sopenharmony_ci *     configuration (each followed by its payload).
11888c2ecf20Sopenharmony_ci *     TLV headers and payloads must be properly initialized, with the
11898c2ecf20Sopenharmony_ci *     right endianess (LE).
11908c2ecf20Sopenharmony_ci * @arg_size: number of pointers in the @args array
11918c2ecf20Sopenharmony_ci */
11928c2ecf20Sopenharmony_cistatic int i2400m_set_init_config(struct i2400m *i2400m,
11938c2ecf20Sopenharmony_ci				  const struct i2400m_tlv_hdr **arg,
11948c2ecf20Sopenharmony_ci				  size_t args)
11958c2ecf20Sopenharmony_ci{
11968c2ecf20Sopenharmony_ci	int result;
11978c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
11988c2ecf20Sopenharmony_ci	struct sk_buff *ack_skb;
11998c2ecf20Sopenharmony_ci	struct i2400m_l3l4_hdr *cmd;
12008c2ecf20Sopenharmony_ci	char strerr[32];
12018c2ecf20Sopenharmony_ci	unsigned argc, argsize, tlv_size;
12028c2ecf20Sopenharmony_ci	const struct i2400m_tlv_hdr *tlv_hdr;
12038c2ecf20Sopenharmony_ci	void *buf, *itr;
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ci	d_fnstart(3, dev, "(i2400m %p arg %p args %zu)\n", i2400m, arg, args);
12068c2ecf20Sopenharmony_ci	result = 0;
12078c2ecf20Sopenharmony_ci	if (args == 0)
12088c2ecf20Sopenharmony_ci		goto none;
12098c2ecf20Sopenharmony_ci	/* Compute the size of all the TLVs, so we can alloc a
12108c2ecf20Sopenharmony_ci	 * contiguous command block to copy them. */
12118c2ecf20Sopenharmony_ci	argsize = 0;
12128c2ecf20Sopenharmony_ci	for (argc = 0; argc < args; argc++) {
12138c2ecf20Sopenharmony_ci		tlv_hdr = arg[argc];
12148c2ecf20Sopenharmony_ci		argsize += sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
12158c2ecf20Sopenharmony_ci	}
12168c2ecf20Sopenharmony_ci	WARN_ON(argc >= 9);	/* As per hw spec */
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	/* Alloc the space for the command and TLVs*/
12198c2ecf20Sopenharmony_ci	result = -ENOMEM;
12208c2ecf20Sopenharmony_ci	buf = kzalloc(sizeof(*cmd) + argsize, GFP_KERNEL);
12218c2ecf20Sopenharmony_ci	if (buf == NULL)
12228c2ecf20Sopenharmony_ci		goto error_alloc;
12238c2ecf20Sopenharmony_ci	cmd = buf;
12248c2ecf20Sopenharmony_ci	cmd->type = cpu_to_le16(I2400M_MT_SET_INIT_CONFIG);
12258c2ecf20Sopenharmony_ci	cmd->length = cpu_to_le16(argsize);
12268c2ecf20Sopenharmony_ci	cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	/* Copy the TLVs */
12298c2ecf20Sopenharmony_ci	itr = buf + sizeof(*cmd);
12308c2ecf20Sopenharmony_ci	for (argc = 0; argc < args; argc++) {
12318c2ecf20Sopenharmony_ci		tlv_hdr = arg[argc];
12328c2ecf20Sopenharmony_ci		tlv_size = sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
12338c2ecf20Sopenharmony_ci		memcpy(itr, tlv_hdr, tlv_size);
12348c2ecf20Sopenharmony_ci		itr += tlv_size;
12358c2ecf20Sopenharmony_ci	}
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci	/* Send the message! */
12388c2ecf20Sopenharmony_ci	ack_skb = i2400m_msg_to_dev(i2400m, buf, sizeof(*cmd) + argsize);
12398c2ecf20Sopenharmony_ci	result = PTR_ERR(ack_skb);
12408c2ecf20Sopenharmony_ci	if (IS_ERR(ack_skb)) {
12418c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to issue 'init config' command: %d\n",
12428c2ecf20Sopenharmony_ci			result);
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci		goto error_msg_to_dev;
12458c2ecf20Sopenharmony_ci	}
12468c2ecf20Sopenharmony_ci	result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
12478c2ecf20Sopenharmony_ci					 strerr, sizeof(strerr));
12488c2ecf20Sopenharmony_ci	if (result < 0)
12498c2ecf20Sopenharmony_ci		dev_err(dev, "'init config' (0x%04x) command failed: %d - %s\n",
12508c2ecf20Sopenharmony_ci			I2400M_MT_SET_INIT_CONFIG, result, strerr);
12518c2ecf20Sopenharmony_ci	kfree_skb(ack_skb);
12528c2ecf20Sopenharmony_cierror_msg_to_dev:
12538c2ecf20Sopenharmony_ci	kfree(buf);
12548c2ecf20Sopenharmony_cierror_alloc:
12558c2ecf20Sopenharmony_cinone:
12568c2ecf20Sopenharmony_ci	d_fnend(3, dev, "(i2400m %p arg %p args %zu) = %d\n",
12578c2ecf20Sopenharmony_ci		i2400m, arg, args, result);
12588c2ecf20Sopenharmony_ci	return result;
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci}
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci/**
12638c2ecf20Sopenharmony_ci * i2400m_set_idle_timeout - Set the device's idle mode timeout
12648c2ecf20Sopenharmony_ci *
12658c2ecf20Sopenharmony_ci * @i2400m: i2400m device descriptor
12668c2ecf20Sopenharmony_ci *
12678c2ecf20Sopenharmony_ci * @msecs: milliseconds for the timeout to enter idle mode. Between
12688c2ecf20Sopenharmony_ci *     100 to 300000 (5m); 0 to disable. In increments of 100.
12698c2ecf20Sopenharmony_ci *
12708c2ecf20Sopenharmony_ci * After this @msecs of the link being idle (no data being sent or
12718c2ecf20Sopenharmony_ci * received), the device will negotiate with the basestation entering
12728c2ecf20Sopenharmony_ci * idle mode for saving power. The connection is maintained, but
12738c2ecf20Sopenharmony_ci * getting out of it (done in tx.c) will require some negotiation,
12748c2ecf20Sopenharmony_ci * possible crypto re-handshake and a possible DHCP re-lease.
12758c2ecf20Sopenharmony_ci *
12768c2ecf20Sopenharmony_ci * Only available if fw_version >= 0x00090002.
12778c2ecf20Sopenharmony_ci *
12788c2ecf20Sopenharmony_ci * Returns: 0 if ok, < 0 errno code on error.
12798c2ecf20Sopenharmony_ci */
12808c2ecf20Sopenharmony_ciint i2400m_set_idle_timeout(struct i2400m *i2400m, unsigned msecs)
12818c2ecf20Sopenharmony_ci{
12828c2ecf20Sopenharmony_ci	int result;
12838c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
12848c2ecf20Sopenharmony_ci	struct sk_buff *ack_skb;
12858c2ecf20Sopenharmony_ci	struct {
12868c2ecf20Sopenharmony_ci		struct i2400m_l3l4_hdr hdr;
12878c2ecf20Sopenharmony_ci		struct i2400m_tlv_config_idle_timeout cit;
12888c2ecf20Sopenharmony_ci	} *cmd;
12898c2ecf20Sopenharmony_ci	const struct i2400m_l3l4_hdr *ack;
12908c2ecf20Sopenharmony_ci	size_t ack_len;
12918c2ecf20Sopenharmony_ci	char strerr[32];
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci	result = -ENOSYS;
12948c2ecf20Sopenharmony_ci	if (i2400m_le_v1_3(i2400m))
12958c2ecf20Sopenharmony_ci		goto error_alloc;
12968c2ecf20Sopenharmony_ci	result = -ENOMEM;
12978c2ecf20Sopenharmony_ci	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
12988c2ecf20Sopenharmony_ci	if (cmd == NULL)
12998c2ecf20Sopenharmony_ci		goto error_alloc;
13008c2ecf20Sopenharmony_ci	cmd->hdr.type = cpu_to_le16(I2400M_MT_GET_STATE);
13018c2ecf20Sopenharmony_ci	cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
13028c2ecf20Sopenharmony_ci	cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_ci	cmd->cit.hdr.type =
13058c2ecf20Sopenharmony_ci		cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
13068c2ecf20Sopenharmony_ci	cmd->cit.hdr.length = cpu_to_le16(sizeof(cmd->cit.timeout));
13078c2ecf20Sopenharmony_ci	cmd->cit.timeout = cpu_to_le32(msecs);
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
13108c2ecf20Sopenharmony_ci	if (IS_ERR(ack_skb)) {
13118c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to issue 'set idle timeout' command: "
13128c2ecf20Sopenharmony_ci			"%ld\n", PTR_ERR(ack_skb));
13138c2ecf20Sopenharmony_ci		result = PTR_ERR(ack_skb);
13148c2ecf20Sopenharmony_ci		goto error_msg_to_dev;
13158c2ecf20Sopenharmony_ci	}
13168c2ecf20Sopenharmony_ci	ack = wimax_msg_data_len(ack_skb, &ack_len);
13178c2ecf20Sopenharmony_ci	result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
13188c2ecf20Sopenharmony_ci	if (result < 0) {
13198c2ecf20Sopenharmony_ci		dev_err(dev, "'set idle timeout' (0x%04x) command failed: "
13208c2ecf20Sopenharmony_ci			"%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
13218c2ecf20Sopenharmony_ci		goto error_cmd_failed;
13228c2ecf20Sopenharmony_ci	}
13238c2ecf20Sopenharmony_ci	result = 0;
13248c2ecf20Sopenharmony_ci	kfree_skb(ack_skb);
13258c2ecf20Sopenharmony_cierror_cmd_failed:
13268c2ecf20Sopenharmony_cierror_msg_to_dev:
13278c2ecf20Sopenharmony_ci	kfree(cmd);
13288c2ecf20Sopenharmony_cierror_alloc:
13298c2ecf20Sopenharmony_ci	return result;
13308c2ecf20Sopenharmony_ci}
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci/**
13348c2ecf20Sopenharmony_ci * i2400m_dev_initialize - Initialize the device once communications are ready
13358c2ecf20Sopenharmony_ci *
13368c2ecf20Sopenharmony_ci * @i2400m: device descriptor
13378c2ecf20Sopenharmony_ci *
13388c2ecf20Sopenharmony_ci * Returns: 0 if ok, < 0 errno code on error.
13398c2ecf20Sopenharmony_ci *
13408c2ecf20Sopenharmony_ci * Configures the device to work the way we like it.
13418c2ecf20Sopenharmony_ci *
13428c2ecf20Sopenharmony_ci * At the point of this call, the device is registered with the WiMAX
13438c2ecf20Sopenharmony_ci * and netdev stacks, firmware is uploaded and we can talk to the
13448c2ecf20Sopenharmony_ci * device normally.
13458c2ecf20Sopenharmony_ci */
13468c2ecf20Sopenharmony_ciint i2400m_dev_initialize(struct i2400m *i2400m)
13478c2ecf20Sopenharmony_ci{
13488c2ecf20Sopenharmony_ci	int result;
13498c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
13508c2ecf20Sopenharmony_ci	struct i2400m_tlv_config_idle_parameters idle_params;
13518c2ecf20Sopenharmony_ci	struct i2400m_tlv_config_idle_timeout idle_timeout;
13528c2ecf20Sopenharmony_ci	struct i2400m_tlv_config_d2h_data_format df;
13538c2ecf20Sopenharmony_ci	struct i2400m_tlv_config_dl_host_reorder dlhr;
13548c2ecf20Sopenharmony_ci	const struct i2400m_tlv_hdr *args[9];
13558c2ecf20Sopenharmony_ci	unsigned argc = 0;
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_ci	d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
13588c2ecf20Sopenharmony_ci	if (i2400m_passive_mode)
13598c2ecf20Sopenharmony_ci		goto out_passive;
13608c2ecf20Sopenharmony_ci	/* Disable idle mode? (enabled by default) */
13618c2ecf20Sopenharmony_ci	if (i2400m_idle_mode_disabled) {
13628c2ecf20Sopenharmony_ci		if (i2400m_le_v1_3(i2400m)) {
13638c2ecf20Sopenharmony_ci			idle_params.hdr.type =
13648c2ecf20Sopenharmony_ci				cpu_to_le16(I2400M_TLV_CONFIG_IDLE_PARAMETERS);
13658c2ecf20Sopenharmony_ci			idle_params.hdr.length = cpu_to_le16(
13668c2ecf20Sopenharmony_ci				sizeof(idle_params) - sizeof(idle_params.hdr));
13678c2ecf20Sopenharmony_ci			idle_params.idle_timeout = 0;
13688c2ecf20Sopenharmony_ci			idle_params.idle_paging_interval = 0;
13698c2ecf20Sopenharmony_ci			args[argc++] = &idle_params.hdr;
13708c2ecf20Sopenharmony_ci		} else {
13718c2ecf20Sopenharmony_ci			idle_timeout.hdr.type =
13728c2ecf20Sopenharmony_ci				cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
13738c2ecf20Sopenharmony_ci			idle_timeout.hdr.length = cpu_to_le16(
13748c2ecf20Sopenharmony_ci				sizeof(idle_timeout) - sizeof(idle_timeout.hdr));
13758c2ecf20Sopenharmony_ci			idle_timeout.timeout = 0;
13768c2ecf20Sopenharmony_ci			args[argc++] = &idle_timeout.hdr;
13778c2ecf20Sopenharmony_ci		}
13788c2ecf20Sopenharmony_ci	}
13798c2ecf20Sopenharmony_ci	if (i2400m_ge_v1_4(i2400m)) {
13808c2ecf20Sopenharmony_ci		/* Enable extended RX data format? */
13818c2ecf20Sopenharmony_ci		df.hdr.type =
13828c2ecf20Sopenharmony_ci			cpu_to_le16(I2400M_TLV_CONFIG_D2H_DATA_FORMAT);
13838c2ecf20Sopenharmony_ci		df.hdr.length = cpu_to_le16(
13848c2ecf20Sopenharmony_ci			sizeof(df) - sizeof(df.hdr));
13858c2ecf20Sopenharmony_ci		df.format = 1;
13868c2ecf20Sopenharmony_ci		args[argc++] = &df.hdr;
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci		/* Enable RX data reordering?
13898c2ecf20Sopenharmony_ci		 * (switch flipped in rx.c:i2400m_rx_setup() after fw upload) */
13908c2ecf20Sopenharmony_ci		if (i2400m->rx_reorder) {
13918c2ecf20Sopenharmony_ci			dlhr.hdr.type =
13928c2ecf20Sopenharmony_ci				cpu_to_le16(I2400M_TLV_CONFIG_DL_HOST_REORDER);
13938c2ecf20Sopenharmony_ci			dlhr.hdr.length = cpu_to_le16(
13948c2ecf20Sopenharmony_ci				sizeof(dlhr) - sizeof(dlhr.hdr));
13958c2ecf20Sopenharmony_ci			dlhr.reorder = 1;
13968c2ecf20Sopenharmony_ci			args[argc++] = &dlhr.hdr;
13978c2ecf20Sopenharmony_ci		}
13988c2ecf20Sopenharmony_ci	}
13998c2ecf20Sopenharmony_ci	result = i2400m_set_init_config(i2400m, args, argc);
14008c2ecf20Sopenharmony_ci	if (result < 0)
14018c2ecf20Sopenharmony_ci		goto error;
14028c2ecf20Sopenharmony_ciout_passive:
14038c2ecf20Sopenharmony_ci	/*
14048c2ecf20Sopenharmony_ci	 * Update state: Here it just calls a get state; parsing the
14058c2ecf20Sopenharmony_ci	 * result (System State TLV and RF Status TLV [done in the rx
14068c2ecf20Sopenharmony_ci	 * path hooks]) will set the hardware and software RF-Kill
14078c2ecf20Sopenharmony_ci	 * status.
14088c2ecf20Sopenharmony_ci	 */
14098c2ecf20Sopenharmony_ci	result = i2400m_cmd_get_state(i2400m);
14108c2ecf20Sopenharmony_cierror:
14118c2ecf20Sopenharmony_ci	if (result < 0)
14128c2ecf20Sopenharmony_ci		dev_err(dev, "failed to initialize the device: %d\n", result);
14138c2ecf20Sopenharmony_ci	d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
14148c2ecf20Sopenharmony_ci	return result;
14158c2ecf20Sopenharmony_ci}
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci/**
14198c2ecf20Sopenharmony_ci * i2400m_dev_shutdown - Shutdown a running device
14208c2ecf20Sopenharmony_ci *
14218c2ecf20Sopenharmony_ci * @i2400m: device descriptor
14228c2ecf20Sopenharmony_ci *
14238c2ecf20Sopenharmony_ci * Release resources acquired during the running of the device; in
14248c2ecf20Sopenharmony_ci * theory, should also tell the device to go to sleep, switch off the
14258c2ecf20Sopenharmony_ci * radio, all that, but at this point, in most cases (driver
14268c2ecf20Sopenharmony_ci * disconnection, reset handling) we can't even talk to the device.
14278c2ecf20Sopenharmony_ci */
14288c2ecf20Sopenharmony_civoid i2400m_dev_shutdown(struct i2400m *i2400m)
14298c2ecf20Sopenharmony_ci{
14308c2ecf20Sopenharmony_ci	struct device *dev = i2400m_dev(i2400m);
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci	d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
14338c2ecf20Sopenharmony_ci	d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
14348c2ecf20Sopenharmony_ci}
1435