GNU Linux-libre 4.19.286-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 <net/rsi_91x.h>
22 #include "rsi_mgmt.h"
23 #include "rsi_common.h"
24 #include "rsi_coex.h"
25 #include "rsi_hal.h"
26 #include "rsi_usb.h"
27
28 u32 rsi_zone_enabled = /* INFO_ZONE |
29                         INIT_ZONE |
30                         MGMT_TX_ZONE |
31                         MGMT_RX_ZONE |
32                         DATA_TX_ZONE |
33                         DATA_RX_ZONE |
34                         FSM_ZONE |
35                         ISR_ZONE | */
36                         ERR_ZONE |
37                         0;
38 EXPORT_SYMBOL_GPL(rsi_zone_enabled);
39
40 #ifdef CONFIG_RSI_COEX
41 static struct rsi_proto_ops g_proto_ops = {
42         .coex_send_pkt = rsi_coex_send_pkt,
43         .get_host_intf = rsi_get_host_intf,
44         .set_bt_context = rsi_set_bt_context,
45 };
46 #endif
47
48 /**
49  * rsi_dbg() - This function outputs informational messages.
50  * @zone: Zone of interest for output message.
51  * @fmt: printf-style format for output message.
52  *
53  * Return: none
54  */
55 void rsi_dbg(u32 zone, const char *fmt, ...)
56 {
57         struct va_format vaf;
58         va_list args;
59
60         va_start(args, fmt);
61
62         vaf.fmt = fmt;
63         vaf.va = &args;
64
65         if (zone & rsi_zone_enabled)
66                 pr_info("%pV", &vaf);
67         va_end(args);
68 }
69 EXPORT_SYMBOL_GPL(rsi_dbg);
70
71 static char *opmode_str(int oper_mode)
72 {
73         switch (oper_mode) {
74         case DEV_OPMODE_WIFI_ALONE:
75                 return "Wi-Fi alone";
76         case DEV_OPMODE_BT_ALONE:
77                 return "BT EDR alone";
78         case DEV_OPMODE_BT_LE_ALONE:
79                 return "BT LE alone";
80         case DEV_OPMODE_BT_DUAL:
81                 return "BT Dual";
82         case DEV_OPMODE_STA_BT:
83                 return "Wi-Fi STA + BT EDR";
84         case DEV_OPMODE_STA_BT_LE:
85                 return "Wi-Fi STA + BT LE";
86         case DEV_OPMODE_STA_BT_DUAL:
87                 return "Wi-Fi STA + BT DUAL";
88         case DEV_OPMODE_AP_BT:
89                 return "Wi-Fi AP + BT EDR";
90         case DEV_OPMODE_AP_BT_DUAL:
91                 return "Wi-Fi AP + BT DUAL";
92         }
93
94         return "Unknown";
95 }
96
97 void rsi_print_version(struct rsi_common *common)
98 {
99         rsi_dbg(ERR_ZONE, "================================================\n");
100         rsi_dbg(ERR_ZONE, "================ RSI Version Info ==============\n");
101         rsi_dbg(ERR_ZONE, "================================================\n");
102         rsi_dbg(ERR_ZONE, "FW Version\t: %d.%d.%d\n",
103                 common->lmac_ver.major, common->lmac_ver.minor,
104                 common->lmac_ver.release_num);
105         rsi_dbg(ERR_ZONE, "Operating mode\t: %d [%s]",
106                 common->oper_mode, opmode_str(common->oper_mode));
107         rsi_dbg(ERR_ZONE, "Firmware file\t: %s", common->priv->fw_file_name);
108         rsi_dbg(ERR_ZONE, "================================================\n");
109 }
110
111 /**
112  * rsi_prepare_skb() - This function prepares the skb.
113  * @common: Pointer to the driver private structure.
114  * @buffer: Pointer to the packet data.
115  * @pkt_len: Length of the packet.
116  * @extended_desc: Extended descriptor.
117  *
118  * Return: Successfully skb.
119  */
120 static struct sk_buff *rsi_prepare_skb(struct rsi_common *common,
121                                        u8 *buffer,
122                                        u32 pkt_len,
123                                        u8 extended_desc)
124 {
125         struct ieee80211_tx_info *info;
126         struct sk_buff *skb = NULL;
127         u8 payload_offset;
128         struct ieee80211_vif *vif;
129         struct ieee80211_hdr *wh;
130
131         if (WARN(!pkt_len, "%s: Dummy pkt received", __func__))
132                 return NULL;
133
134         if (pkt_len > (RSI_RCV_BUFFER_LEN * 4)) {
135                 rsi_dbg(ERR_ZONE, "%s: Pkt size > max rx buf size %d\n",
136                         __func__, pkt_len);
137                 pkt_len = RSI_RCV_BUFFER_LEN * 4;
138         }
139
140         pkt_len -= extended_desc;
141         skb = dev_alloc_skb(pkt_len + FRAME_DESC_SZ);
142         if (skb == NULL)
143                 return NULL;
144
145         payload_offset = (extended_desc + FRAME_DESC_SZ);
146         skb_put(skb, pkt_len);
147         memcpy((skb->data), (buffer + payload_offset), skb->len);
148         wh = (struct ieee80211_hdr *)skb->data;
149         vif = rsi_get_vif(common->priv, wh->addr1);
150
151         info = IEEE80211_SKB_CB(skb);
152         return skb;
153 }
154
155 /**
156  * rsi_read_pkt() - This function reads frames from the card.
157  * @common: Pointer to the driver private structure.
158  * @rcv_pkt_len: Received pkt length. In case of USB it is 0.
159  *
160  * Return: 0 on success, -1 on failure.
161  */
162 int rsi_read_pkt(struct rsi_common *common, u8 *rx_pkt, s32 rcv_pkt_len)
163 {
164         u8 *frame_desc = NULL, extended_desc = 0;
165         u32 index, length = 0, queueno = 0;
166         u16 actual_length = 0, offset;
167         struct sk_buff *skb = NULL;
168 #ifdef CONFIG_RSI_COEX
169         u8 bt_pkt_type;
170 #endif
171
172         index = 0;
173         do {
174                 frame_desc = &rx_pkt[index];
175                 actual_length = *(u16 *)&frame_desc[0];
176                 offset = *(u16 *)&frame_desc[2];
177                 if (!rcv_pkt_len && offset >
178                         RSI_MAX_RX_USB_PKT_SIZE - FRAME_DESC_SZ)
179                         goto fail;
180
181                 queueno = rsi_get_queueno(frame_desc, offset);
182                 length = rsi_get_length(frame_desc, offset);
183
184                 /* Extended descriptor is valid for WLAN queues only */
185                 if (queueno == RSI_WIFI_DATA_Q || queueno == RSI_WIFI_MGMT_Q)
186                         extended_desc = rsi_get_extended_desc(frame_desc,
187                                                               offset);
188
189                 switch (queueno) {
190                 case RSI_COEX_Q:
191 #ifdef CONFIG_RSI_COEX
192                         if (common->coex_mode > 1)
193                                 rsi_coex_recv_pkt(common, frame_desc + offset);
194                         else
195 #endif
196                                 rsi_mgmt_pkt_recv(common,
197                                                   (frame_desc + offset));
198                         break;
199
200                 case RSI_WIFI_DATA_Q:
201                         skb = rsi_prepare_skb(common,
202                                               (frame_desc + offset),
203                                               length,
204                                               extended_desc);
205                         if (skb == NULL)
206                                 goto fail;
207
208                         rsi_indicate_pkt_to_os(common, skb);
209                         break;
210
211                 case RSI_WIFI_MGMT_Q:
212                         rsi_mgmt_pkt_recv(common, (frame_desc + offset));
213                         break;
214
215 #ifdef CONFIG_RSI_COEX
216                 case RSI_BT_MGMT_Q:
217                 case RSI_BT_DATA_Q:
218 #define BT_RX_PKT_TYPE_OFST     14
219 #define BT_CARD_READY_IND       0x89
220                         bt_pkt_type = frame_desc[offset + BT_RX_PKT_TYPE_OFST];
221                         if (bt_pkt_type == BT_CARD_READY_IND) {
222                                 rsi_dbg(INFO_ZONE, "BT Card ready recvd\n");
223                                 if (common->fsm_state == FSM_MAC_INIT_DONE)
224                                         rsi_attach_bt(common);
225                                 else
226                                         common->bt_defer_attach = true;
227                         } else {
228                                 if (common->bt_adapter)
229                                         rsi_bt_ops.recv_pkt(common->bt_adapter,
230                                                         frame_desc + offset);
231                         }
232                         break;
233 #endif
234
235                 default:
236                         rsi_dbg(ERR_ZONE, "%s: pkt from invalid queue: %d\n",
237                                 __func__,   queueno);
238                         goto fail;
239                 }
240
241                 index  += actual_length;
242                 rcv_pkt_len -= actual_length;
243         } while (rcv_pkt_len > 0);
244
245         return 0;
246 fail:
247         return -EINVAL;
248 }
249 EXPORT_SYMBOL_GPL(rsi_read_pkt);
250
251 /**
252  * rsi_tx_scheduler_thread() - This function is a kernel thread to send the
253  *                             packets to the device.
254  * @common: Pointer to the driver private structure.
255  *
256  * Return: None.
257  */
258 static void rsi_tx_scheduler_thread(struct rsi_common *common)
259 {
260         struct rsi_hw *adapter = common->priv;
261         u32 timeout = EVENT_WAIT_FOREVER;
262
263         do {
264                 if (adapter->determine_event_timeout)
265                         timeout = adapter->determine_event_timeout(adapter);
266                 rsi_wait_event(&common->tx_thread.event, timeout);
267                 rsi_reset_event(&common->tx_thread.event);
268
269                 if (common->init_done)
270                         rsi_core_qos_processor(common);
271         } while (atomic_read(&common->tx_thread.thread_done) == 0);
272         complete_and_exit(&common->tx_thread.completion, 0);
273 }
274
275 #ifdef CONFIG_RSI_COEX
276 enum rsi_host_intf rsi_get_host_intf(void *priv)
277 {
278         struct rsi_common *common = (struct rsi_common *)priv;
279
280         return common->priv->rsi_host_intf;
281 }
282
283 void rsi_set_bt_context(void *priv, void *bt_context)
284 {
285         struct rsi_common *common = (struct rsi_common *)priv;
286
287         common->bt_adapter = bt_context;
288 }
289 #endif
290
291 void rsi_attach_bt(struct rsi_common *common)
292 {
293 #ifdef CONFIG_RSI_COEX
294         if (rsi_bt_ops.attach(common, &g_proto_ops))
295                 rsi_dbg(ERR_ZONE,
296                         "Failed to attach BT module\n");
297 #endif
298 }
299
300 /**
301  * rsi_91x_init() - This function initializes os interface operations.
302  * @void: Void.
303  *
304  * Return: Pointer to the adapter structure on success, NULL on failure .
305  */
306 struct rsi_hw *rsi_91x_init(u16 oper_mode)
307 {
308         struct rsi_hw *adapter = NULL;
309         struct rsi_common *common = NULL;
310         u8 ii = 0;
311
312         adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
313         if (!adapter)
314                 return NULL;
315
316         adapter->priv = kzalloc(sizeof(*common), GFP_KERNEL);
317         if (adapter->priv == NULL) {
318                 rsi_dbg(ERR_ZONE, "%s: Failed in allocation of memory\n",
319                         __func__);
320                 kfree(adapter);
321                 return NULL;
322         } else {
323                 common = adapter->priv;
324                 common->priv = adapter;
325         }
326
327         for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
328                 skb_queue_head_init(&common->tx_queue[ii]);
329
330         rsi_init_event(&common->tx_thread.event);
331         mutex_init(&common->mutex);
332         mutex_init(&common->tx_lock);
333         mutex_init(&common->rx_lock);
334         mutex_init(&common->tx_bus_mutex);
335
336         if (rsi_create_kthread(common,
337                                &common->tx_thread,
338                                rsi_tx_scheduler_thread,
339                                "Tx-Thread")) {
340                 rsi_dbg(ERR_ZONE, "%s: Unable to init tx thrd\n", __func__);
341                 goto err;
342         }
343
344         rsi_default_ps_params(adapter);
345         spin_lock_init(&adapter->ps_lock);
346         timer_setup(&common->roc_timer, rsi_roc_timeout, 0);
347         init_completion(&common->wlan_init_completion);
348         adapter->device_model = RSI_DEV_9113;
349         common->oper_mode = oper_mode;
350
351         /* Determine coex mode */
352         switch (common->oper_mode) {
353         case DEV_OPMODE_STA_BT_DUAL:
354         case DEV_OPMODE_STA_BT:
355         case DEV_OPMODE_STA_BT_LE:
356         case DEV_OPMODE_BT_ALONE:
357         case DEV_OPMODE_BT_LE_ALONE:
358         case DEV_OPMODE_BT_DUAL:
359                 common->coex_mode = 2;
360                 break;
361         case DEV_OPMODE_AP_BT_DUAL:
362         case DEV_OPMODE_AP_BT:
363                 common->coex_mode = 4;
364                 break;
365         case DEV_OPMODE_WIFI_ALONE:
366                 common->coex_mode = 1;
367                 break;
368         default:
369                 common->oper_mode = 1;
370                 common->coex_mode = 1;
371         }
372         rsi_dbg(INFO_ZONE, "%s: oper_mode = %d, coex_mode = %d\n",
373                 __func__, common->oper_mode, common->coex_mode);
374
375         adapter->device_model = RSI_DEV_9113;
376 #ifdef CONFIG_RSI_COEX
377         if (common->coex_mode > 1) {
378                 if (rsi_coex_attach(common)) {
379                         rsi_dbg(ERR_ZONE, "Failed to init coex module\n");
380                         rsi_kill_thread(&common->tx_thread);
381                         goto err;
382                 }
383         }
384 #endif
385
386         common->init_done = true;
387         return adapter;
388
389 err:
390         kfree(common);
391         kfree(adapter);
392         return NULL;
393 }
394 EXPORT_SYMBOL_GPL(rsi_91x_init);
395
396 /**
397  * rsi_91x_deinit() - This function de-intializes os intf operations.
398  * @adapter: Pointer to the adapter structure.
399  *
400  * Return: None.
401  */
402 void rsi_91x_deinit(struct rsi_hw *adapter)
403 {
404         struct rsi_common *common = adapter->priv;
405         u8 ii;
406
407         rsi_dbg(INFO_ZONE, "%s: Performing deinit os ops\n", __func__);
408
409         rsi_kill_thread(&common->tx_thread);
410
411         for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
412                 skb_queue_purge(&common->tx_queue[ii]);
413
414 #ifdef CONFIG_RSI_COEX
415         if (common->coex_mode > 1) {
416                 if (common->bt_adapter) {
417                         rsi_bt_ops.detach(common->bt_adapter);
418                         common->bt_adapter = NULL;
419                 }
420                 rsi_coex_detach(common);
421         }
422 #endif
423
424         common->init_done = false;
425
426         kfree(common);
427         kfree(adapter->rsi_dev);
428         kfree(adapter);
429 }
430 EXPORT_SYMBOL_GPL(rsi_91x_deinit);
431
432 /**
433  * rsi_91x_hal_module_init() - This function is invoked when the module is
434  *                             loaded into the kernel.
435  *                             It registers the client driver.
436  * @void: Void.
437  *
438  * Return: 0 on success, -1 on failure.
439  */
440 static int rsi_91x_hal_module_init(void)
441 {
442         rsi_dbg(INIT_ZONE, "%s: Module init called\n", __func__);
443         return 0;
444 }
445
446 /**
447  * rsi_91x_hal_module_exit() - This function is called at the time of
448  *                             removing/unloading the module.
449  *                             It unregisters the client driver.
450  * @void: Void.
451  *
452  * Return: None.
453  */
454 static void rsi_91x_hal_module_exit(void)
455 {
456         rsi_dbg(INIT_ZONE, "%s: Module exit called\n", __func__);
457 }
458
459 module_init(rsi_91x_hal_module_init);
460 module_exit(rsi_91x_hal_module_exit);
461 MODULE_AUTHOR("Redpine Signals Inc");
462 MODULE_DESCRIPTION("Station driver for RSI 91x devices");
463 MODULE_SUPPORTED_DEVICE("RSI-91x");
464 MODULE_VERSION("0.1");
465 MODULE_LICENSE("Dual BSD/GPL");