GNU Linux-libre 4.14.313-gnu1
[releases.git] / drivers / net / wireless / rsi / rsi_91x_main.c
1 /**
2  * Copyright (c) 2014 Redpine Signals Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <linux/module.h>
20 #include <linux/firmware.h>
21 #include "rsi_mgmt.h"
22 #include "rsi_common.h"
23
24 u32 rsi_zone_enabled = /* INFO_ZONE |
25                         INIT_ZONE |
26                         MGMT_TX_ZONE |
27                         MGMT_RX_ZONE |
28                         DATA_TX_ZONE |
29                         DATA_RX_ZONE |
30                         FSM_ZONE |
31                         ISR_ZONE | */
32                         ERR_ZONE |
33                         0;
34 EXPORT_SYMBOL_GPL(rsi_zone_enabled);
35
36 /**
37  * rsi_dbg() - This function outputs informational messages.
38  * @zone: Zone of interest for output message.
39  * @fmt: printf-style format for output message.
40  *
41  * Return: none
42  */
43 void rsi_dbg(u32 zone, const char *fmt, ...)
44 {
45         struct va_format vaf;
46         va_list args;
47
48         va_start(args, fmt);
49
50         vaf.fmt = fmt;
51         vaf.va = &args;
52
53         if (zone & rsi_zone_enabled)
54                 pr_info("%pV", &vaf);
55         va_end(args);
56 }
57 EXPORT_SYMBOL_GPL(rsi_dbg);
58
59 /**
60  * rsi_prepare_skb() - This function prepares the skb.
61  * @common: Pointer to the driver private structure.
62  * @buffer: Pointer to the packet data.
63  * @pkt_len: Length of the packet.
64  * @extended_desc: Extended descriptor.
65  *
66  * Return: Successfully skb.
67  */
68 static struct sk_buff *rsi_prepare_skb(struct rsi_common *common,
69                                        u8 *buffer,
70                                        u32 pkt_len,
71                                        u8 extended_desc)
72 {
73         struct ieee80211_tx_info *info;
74         struct skb_info *rx_params;
75         struct sk_buff *skb = NULL;
76         u8 payload_offset;
77
78         if (WARN(!pkt_len, "%s: Dummy pkt received", __func__))
79                 return NULL;
80
81         if (pkt_len > (RSI_RCV_BUFFER_LEN * 4)) {
82                 rsi_dbg(ERR_ZONE, "%s: Pkt size > max rx buf size %d\n",
83                         __func__, pkt_len);
84                 pkt_len = RSI_RCV_BUFFER_LEN * 4;
85         }
86
87         pkt_len -= extended_desc;
88         skb = dev_alloc_skb(pkt_len + FRAME_DESC_SZ);
89         if (skb == NULL)
90                 return NULL;
91
92         payload_offset = (extended_desc + FRAME_DESC_SZ);
93         skb_put(skb, pkt_len);
94         memcpy((skb->data), (buffer + payload_offset), skb->len);
95
96         info = IEEE80211_SKB_CB(skb);
97         rx_params = (struct skb_info *)info->driver_data;
98         rx_params->rssi = rsi_get_rssi(buffer);
99         rx_params->channel = rsi_get_connected_channel(common->priv);
100
101         return skb;
102 }
103
104 /**
105  * rsi_read_pkt() - This function reads frames from the card.
106  * @common: Pointer to the driver private structure.
107  * @rcv_pkt_len: Received pkt length. In case of USB it is 0.
108  *
109  * Return: 0 on success, -1 on failure.
110  */
111 int rsi_read_pkt(struct rsi_common *common, s32 rcv_pkt_len)
112 {
113         u8 *frame_desc = NULL, extended_desc = 0;
114         u32 index, length = 0, queueno = 0;
115         u16 actual_length = 0, offset;
116         struct sk_buff *skb = NULL;
117
118         index = 0;
119         do {
120                 frame_desc = &common->rx_data_pkt[index];
121                 actual_length = *(u16 *)&frame_desc[0];
122                 offset = *(u16 *)&frame_desc[2];
123
124                 queueno = rsi_get_queueno(frame_desc, offset);
125                 length = rsi_get_length(frame_desc, offset);
126
127                 /* Extended descriptor is valid for WLAN queues only */
128                 if (queueno == RSI_WIFI_DATA_Q || queueno == RSI_WIFI_MGMT_Q)
129                         extended_desc = rsi_get_extended_desc(frame_desc,
130                                                               offset);
131
132                 switch (queueno) {
133                 case RSI_COEX_Q:
134                         rsi_mgmt_pkt_recv(common, (frame_desc + offset));
135                         break;
136                 case RSI_WIFI_DATA_Q:
137                         skb = rsi_prepare_skb(common,
138                                               (frame_desc + offset),
139                                               length,
140                                               extended_desc);
141                         if (skb == NULL)
142                                 goto fail;
143
144                         rsi_indicate_pkt_to_os(common, skb);
145                         break;
146
147                 case RSI_WIFI_MGMT_Q:
148                         rsi_mgmt_pkt_recv(common, (frame_desc + offset));
149                         break;
150
151                 default:
152                         rsi_dbg(ERR_ZONE, "%s: pkt from invalid queue: %d\n",
153                                 __func__,   queueno);
154                         goto fail;
155                 }
156
157                 index  += actual_length;
158                 rcv_pkt_len -= actual_length;
159         } while (rcv_pkt_len > 0);
160
161         return 0;
162 fail:
163         return -EINVAL;
164 }
165 EXPORT_SYMBOL_GPL(rsi_read_pkt);
166
167 /**
168  * rsi_tx_scheduler_thread() - This function is a kernel thread to send the
169  *                             packets to the device.
170  * @common: Pointer to the driver private structure.
171  *
172  * Return: None.
173  */
174 static void rsi_tx_scheduler_thread(struct rsi_common *common)
175 {
176         struct rsi_hw *adapter = common->priv;
177         u32 timeout = EVENT_WAIT_FOREVER;
178
179         do {
180                 if (adapter->determine_event_timeout)
181                         timeout = adapter->determine_event_timeout(adapter);
182                 rsi_wait_event(&common->tx_thread.event, timeout);
183                 rsi_reset_event(&common->tx_thread.event);
184
185                 if (common->init_done)
186                         rsi_core_qos_processor(common);
187         } while (atomic_read(&common->tx_thread.thread_done) == 0);
188         complete_and_exit(&common->tx_thread.completion, 0);
189 }
190
191 /**
192  * rsi_91x_init() - This function initializes os interface operations.
193  * @void: Void.
194  *
195  * Return: Pointer to the adapter structure on success, NULL on failure .
196  */
197 struct rsi_hw *rsi_91x_init(void)
198 {
199         struct rsi_hw *adapter = NULL;
200         struct rsi_common *common = NULL;
201         u8 ii = 0;
202
203         adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
204         if (!adapter)
205                 return NULL;
206
207         adapter->priv = kzalloc(sizeof(*common), GFP_KERNEL);
208         if (adapter->priv == NULL) {
209                 rsi_dbg(ERR_ZONE, "%s: Failed in allocation of memory\n",
210                         __func__);
211                 kfree(adapter);
212                 return NULL;
213         } else {
214                 common = adapter->priv;
215                 common->priv = adapter;
216         }
217
218         for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
219                 skb_queue_head_init(&common->tx_queue[ii]);
220
221         rsi_init_event(&common->tx_thread.event);
222         mutex_init(&common->mutex);
223         mutex_init(&common->tx_lock);
224         mutex_init(&common->rx_lock);
225
226         if (rsi_create_kthread(common,
227                                &common->tx_thread,
228                                rsi_tx_scheduler_thread,
229                                "Tx-Thread")) {
230                 rsi_dbg(ERR_ZONE, "%s: Unable to init tx thrd\n", __func__);
231                 goto err;
232         }
233
234         rsi_default_ps_params(adapter);
235         spin_lock_init(&adapter->ps_lock);
236         common->init_done = true;
237         return adapter;
238
239 err:
240         kfree(common);
241         kfree(adapter);
242         return NULL;
243 }
244 EXPORT_SYMBOL_GPL(rsi_91x_init);
245
246 /**
247  * rsi_91x_deinit() - This function de-intializes os intf operations.
248  * @adapter: Pointer to the adapter structure.
249  *
250  * Return: None.
251  */
252 void rsi_91x_deinit(struct rsi_hw *adapter)
253 {
254         struct rsi_common *common = adapter->priv;
255         u8 ii;
256
257         rsi_dbg(INFO_ZONE, "%s: Performing deinit os ops\n", __func__);
258
259         rsi_kill_thread(&common->tx_thread);
260
261         for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
262                 skb_queue_purge(&common->tx_queue[ii]);
263
264         common->init_done = false;
265
266         kfree(common);
267         kfree(adapter->rsi_dev);
268         kfree(adapter);
269 }
270 EXPORT_SYMBOL_GPL(rsi_91x_deinit);
271
272 /**
273  * rsi_91x_hal_module_init() - This function is invoked when the module is
274  *                             loaded into the kernel.
275  *                             It registers the client driver.
276  * @void: Void.
277  *
278  * Return: 0 on success, -1 on failure.
279  */
280 static int rsi_91x_hal_module_init(void)
281 {
282         rsi_dbg(INIT_ZONE, "%s: Module init called\n", __func__);
283         return 0;
284 }
285
286 /**
287  * rsi_91x_hal_module_exit() - This function is called at the time of
288  *                             removing/unloading the module.
289  *                             It unregisters the client driver.
290  * @void: Void.
291  *
292  * Return: None.
293  */
294 static void rsi_91x_hal_module_exit(void)
295 {
296         rsi_dbg(INIT_ZONE, "%s: Module exit called\n", __func__);
297 }
298
299 module_init(rsi_91x_hal_module_init);
300 module_exit(rsi_91x_hal_module_exit);
301 MODULE_AUTHOR("Redpine Signals Inc");
302 MODULE_DESCRIPTION("Station driver for RSI 91x devices");
303 MODULE_SUPPORTED_DEVICE("RSI-91x");
304 MODULE_VERSION("0.1");
305 MODULE_LICENSE("Dual BSD/GPL");