1 /*
2  *  Copyright (C) 2008, cozybit Inc.
3  *  Copyright (C) 2003-2006, Marvell International Ltd.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or (at
8  *  your option) any later version.
9  */
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/slab.h>
13 
14 #include <linux/etherdevice.h>
15 #include "libertas_tf.h"
16 
17 #define DRIVER_RELEASE_VERSION "004.p0"
18 /* thinfirm version: 5.132.X.pX */
19 #define LBTF_FW_VER_MIN		0x05840300
20 #define LBTF_FW_VER_MAX		0x0584ffff
21 #define QOS_CONTROL_LEN		2
22 
23 /* Module parameters */
24 unsigned int lbtf_debug;
25 EXPORT_SYMBOL_GPL(lbtf_debug);
26 module_param_named(libertas_tf_debug, lbtf_debug, int, 0644);
27 
28 static const char lbtf_driver_version[] = "THINFIRM-USB8388-" DRIVER_RELEASE_VERSION
29 #ifdef DEBUG
30 	"-dbg"
31 #endif
32 	"";
33 
34 struct workqueue_struct *lbtf_wq;
35 
36 static const struct ieee80211_channel lbtf_channels[] = {
37 	{ .center_freq = 2412, .hw_value = 1 },
38 	{ .center_freq = 2417, .hw_value = 2 },
39 	{ .center_freq = 2422, .hw_value = 3 },
40 	{ .center_freq = 2427, .hw_value = 4 },
41 	{ .center_freq = 2432, .hw_value = 5 },
42 	{ .center_freq = 2437, .hw_value = 6 },
43 	{ .center_freq = 2442, .hw_value = 7 },
44 	{ .center_freq = 2447, .hw_value = 8 },
45 	{ .center_freq = 2452, .hw_value = 9 },
46 	{ .center_freq = 2457, .hw_value = 10 },
47 	{ .center_freq = 2462, .hw_value = 11 },
48 	{ .center_freq = 2467, .hw_value = 12 },
49 	{ .center_freq = 2472, .hw_value = 13 },
50 	{ .center_freq = 2484, .hw_value = 14 },
51 };
52 
53 /* This table contains the hardware specific values for the modulation rates. */
54 static const struct ieee80211_rate lbtf_rates[] = {
55 	{ .bitrate = 10,
56 	  .hw_value = 0, },
57 	{ .bitrate = 20,
58 	  .hw_value = 1,
59 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
60 	{ .bitrate = 55,
61 	  .hw_value = 2,
62 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
63 	{ .bitrate = 110,
64 	  .hw_value = 3,
65 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
66 	{ .bitrate = 60,
67 	  .hw_value = 5,
68 	  .flags = 0 },
69 	{ .bitrate = 90,
70 	  .hw_value = 6,
71 	  .flags = 0 },
72 	{ .bitrate = 120,
73 	  .hw_value = 7,
74 	  .flags = 0 },
75 	{ .bitrate = 180,
76 	  .hw_value = 8,
77 	  .flags = 0 },
78 	{ .bitrate = 240,
79 	  .hw_value = 9,
80 	  .flags = 0 },
81 	{ .bitrate = 360,
82 	  .hw_value = 10,
83 	  .flags = 0 },
84 	{ .bitrate = 480,
85 	  .hw_value = 11,
86 	  .flags = 0 },
87 	{ .bitrate = 540,
88 	  .hw_value = 12,
89 	  .flags = 0 },
90 };
91 
lbtf_cmd_work(struct work_struct * work)92 static void lbtf_cmd_work(struct work_struct *work)
93 {
94 	struct lbtf_private *priv = container_of(work, struct lbtf_private,
95 					 cmd_work);
96 
97 	lbtf_deb_enter(LBTF_DEB_CMD);
98 
99 	spin_lock_irq(&priv->driver_lock);
100 	/* command response? */
101 	if (priv->cmd_response_rxed) {
102 		priv->cmd_response_rxed = 0;
103 		spin_unlock_irq(&priv->driver_lock);
104 		lbtf_process_rx_command(priv);
105 		spin_lock_irq(&priv->driver_lock);
106 	}
107 
108 	if (priv->cmd_timed_out && priv->cur_cmd) {
109 		struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
110 
111 		if (++priv->nr_retries > 10) {
112 			lbtf_complete_command(priv, cmdnode,
113 					      -ETIMEDOUT);
114 			priv->nr_retries = 0;
115 		} else {
116 			priv->cur_cmd = NULL;
117 
118 			/* Stick it back at the _top_ of the pending
119 			 * queue for immediate resubmission */
120 			list_add(&cmdnode->list, &priv->cmdpendingq);
121 		}
122 	}
123 	priv->cmd_timed_out = 0;
124 	spin_unlock_irq(&priv->driver_lock);
125 
126 	if (!priv->fw_ready) {
127 		lbtf_deb_leave_args(LBTF_DEB_CMD, "fw not ready");
128 		return;
129 	}
130 
131 	/* Execute the next command */
132 	if (!priv->cur_cmd)
133 		lbtf_execute_next_command(priv);
134 
135 	lbtf_deb_leave(LBTF_DEB_CMD);
136 }
137 
138 /**
139  *  lbtf_setup_firmware: initialize firmware.
140  *
141  *  @priv    A pointer to struct lbtf_private structure
142  *
143  *  Returns: 0 on success.
144  */
lbtf_setup_firmware(struct lbtf_private * priv)145 static int lbtf_setup_firmware(struct lbtf_private *priv)
146 {
147 	int ret = -1;
148 
149 	lbtf_deb_enter(LBTF_DEB_FW);
150 	/*
151 	 * Read priv address from HW
152 	 */
153 	memset(priv->current_addr, 0xff, ETH_ALEN);
154 	ret = lbtf_update_hw_spec(priv);
155 	if (ret) {
156 		ret = -1;
157 		goto done;
158 	}
159 
160 	lbtf_set_mac_control(priv);
161 	lbtf_set_radio_control(priv);
162 
163 	ret = 0;
164 done:
165 	lbtf_deb_leave_args(LBTF_DEB_FW, "ret: %d", ret);
166 	return ret;
167 }
168 
169 /**
170  *  This function handles the timeout of command sending.
171  *  It will re-send the same command again.
172  */
command_timer_fn(unsigned long data)173 static void command_timer_fn(unsigned long data)
174 {
175 	struct lbtf_private *priv = (struct lbtf_private *)data;
176 	unsigned long flags;
177 	lbtf_deb_enter(LBTF_DEB_CMD);
178 
179 	spin_lock_irqsave(&priv->driver_lock, flags);
180 
181 	if (!priv->cur_cmd) {
182 		printk(KERN_DEBUG "libertastf: command timer expired; "
183 				  "no pending command\n");
184 		goto out;
185 	}
186 
187 	printk(KERN_DEBUG "libertas: command %x timed out\n",
188 		le16_to_cpu(priv->cur_cmd->cmdbuf->command));
189 
190 	priv->cmd_timed_out = 1;
191 	queue_work(lbtf_wq, &priv->cmd_work);
192 out:
193 	spin_unlock_irqrestore(&priv->driver_lock, flags);
194 	lbtf_deb_leave(LBTF_DEB_CMD);
195 }
196 
lbtf_init_adapter(struct lbtf_private * priv)197 static int lbtf_init_adapter(struct lbtf_private *priv)
198 {
199 	lbtf_deb_enter(LBTF_DEB_MAIN);
200 	memset(priv->current_addr, 0xff, ETH_ALEN);
201 	mutex_init(&priv->lock);
202 
203 	priv->vif = NULL;
204 	setup_timer(&priv->command_timer, command_timer_fn,
205 		(unsigned long)priv);
206 
207 	INIT_LIST_HEAD(&priv->cmdfreeq);
208 	INIT_LIST_HEAD(&priv->cmdpendingq);
209 
210 	spin_lock_init(&priv->driver_lock);
211 
212 	/* Allocate the command buffers */
213 	if (lbtf_allocate_cmd_buffer(priv))
214 		return -1;
215 
216 	lbtf_deb_leave(LBTF_DEB_MAIN);
217 	return 0;
218 }
219 
lbtf_free_adapter(struct lbtf_private * priv)220 static void lbtf_free_adapter(struct lbtf_private *priv)
221 {
222 	lbtf_deb_enter(LBTF_DEB_MAIN);
223 	lbtf_free_cmd_buffer(priv);
224 	del_timer(&priv->command_timer);
225 	lbtf_deb_leave(LBTF_DEB_MAIN);
226 }
227 
lbtf_op_tx(struct ieee80211_hw * hw,struct sk_buff * skb)228 static void lbtf_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
229 {
230 	struct lbtf_private *priv = hw->priv;
231 
232 	priv->skb_to_tx = skb;
233 	queue_work(lbtf_wq, &priv->tx_work);
234 	/*
235 	 * queue will be restarted when we receive transmission feedback if
236 	 * there are no buffered multicast frames to send
237 	 */
238 	ieee80211_stop_queues(priv->hw);
239 }
240 
lbtf_tx_work(struct work_struct * work)241 static void lbtf_tx_work(struct work_struct *work)
242 {
243 	struct lbtf_private *priv = container_of(work, struct lbtf_private,
244 					 tx_work);
245 	unsigned int len;
246 	struct ieee80211_tx_info *info;
247 	struct txpd *txpd;
248 	struct sk_buff *skb = NULL;
249 	int err;
250 
251 	lbtf_deb_enter(LBTF_DEB_MACOPS | LBTF_DEB_TX);
252 
253 	if ((priv->vif->type == NL80211_IFTYPE_AP) &&
254 	    (!skb_queue_empty(&priv->bc_ps_buf)))
255 		skb = skb_dequeue(&priv->bc_ps_buf);
256 	else if (priv->skb_to_tx) {
257 		skb = priv->skb_to_tx;
258 		priv->skb_to_tx = NULL;
259 	} else {
260 		lbtf_deb_leave(LBTF_DEB_MACOPS | LBTF_DEB_TX);
261 		return;
262 	}
263 
264 	len = skb->len;
265 	info  = IEEE80211_SKB_CB(skb);
266 	txpd = (struct txpd *)  skb_push(skb, sizeof(struct txpd));
267 
268 	if (priv->surpriseremoved) {
269 		dev_kfree_skb_any(skb);
270 		lbtf_deb_leave(LBTF_DEB_MACOPS | LBTF_DEB_TX);
271 		return;
272 	}
273 
274 	memset(txpd, 0, sizeof(struct txpd));
275 	/* Activate per-packet rate selection */
276 	txpd->tx_control |= cpu_to_le32(MRVL_PER_PACKET_RATE |
277 			     ieee80211_get_tx_rate(priv->hw, info)->hw_value);
278 
279 	/* copy destination address from 802.11 header */
280 	memcpy(txpd->tx_dest_addr_high, skb->data + sizeof(struct txpd) + 4,
281 		ETH_ALEN);
282 	txpd->tx_packet_length = cpu_to_le16(len);
283 	txpd->tx_packet_location = cpu_to_le32(sizeof(struct txpd));
284 	lbtf_deb_hex(LBTF_DEB_TX, "TX Data", skb->data, min_t(unsigned int, skb->len, 100));
285 	BUG_ON(priv->tx_skb);
286 	spin_lock_irq(&priv->driver_lock);
287 	priv->tx_skb = skb;
288 	err = priv->hw_host_to_card(priv, MVMS_DAT, skb->data, skb->len);
289 	spin_unlock_irq(&priv->driver_lock);
290 	if (err) {
291 		dev_kfree_skb_any(skb);
292 		priv->tx_skb = NULL;
293 		pr_err("TX error: %d", err);
294 	}
295 	lbtf_deb_leave(LBTF_DEB_MACOPS | LBTF_DEB_TX);
296 }
297 
lbtf_op_start(struct ieee80211_hw * hw)298 static int lbtf_op_start(struct ieee80211_hw *hw)
299 {
300 	struct lbtf_private *priv = hw->priv;
301 	void *card = priv->card;
302 	int ret = -1;
303 
304 	lbtf_deb_enter(LBTF_DEB_MACOPS);
305 
306 	if (!priv->fw_ready)
307 		/* Upload firmware */
308 		if (priv->hw_prog_firmware(card))
309 			goto err_prog_firmware;
310 
311 	/* poke the firmware */
312 	priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
313 	priv->radioon = RADIO_ON;
314 	priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
315 	ret = lbtf_setup_firmware(priv);
316 	if (ret)
317 		goto err_prog_firmware;
318 
319 	if ((priv->fwrelease < LBTF_FW_VER_MIN) ||
320 	    (priv->fwrelease > LBTF_FW_VER_MAX)) {
321 		ret = -1;
322 		goto err_prog_firmware;
323 	}
324 
325 	printk(KERN_INFO "libertastf: Marvell WLAN 802.11 thinfirm adapter\n");
326 	lbtf_deb_leave(LBTF_DEB_MACOPS);
327 	return 0;
328 
329 err_prog_firmware:
330 	priv->hw_reset_device(card);
331 	lbtf_deb_leave_args(LBTF_DEB_MACOPS, "error programing fw; ret=%d", ret);
332 	return ret;
333 }
334 
lbtf_op_stop(struct ieee80211_hw * hw)335 static void lbtf_op_stop(struct ieee80211_hw *hw)
336 {
337 	struct lbtf_private *priv = hw->priv;
338 	unsigned long flags;
339 	struct sk_buff *skb;
340 
341 	struct cmd_ctrl_node *cmdnode;
342 
343 	lbtf_deb_enter(LBTF_DEB_MACOPS);
344 
345 	/* Flush pending command nodes */
346 	spin_lock_irqsave(&priv->driver_lock, flags);
347 	list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
348 		cmdnode->result = -ENOENT;
349 		cmdnode->cmdwaitqwoken = 1;
350 		wake_up_interruptible(&cmdnode->cmdwait_q);
351 	}
352 
353 	spin_unlock_irqrestore(&priv->driver_lock, flags);
354 	cancel_work_sync(&priv->cmd_work);
355 	cancel_work_sync(&priv->tx_work);
356 	while ((skb = skb_dequeue(&priv->bc_ps_buf)))
357 		dev_kfree_skb_any(skb);
358 	priv->radioon = RADIO_OFF;
359 	lbtf_set_radio_control(priv);
360 
361 	lbtf_deb_leave(LBTF_DEB_MACOPS);
362 }
363 
lbtf_op_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)364 static int lbtf_op_add_interface(struct ieee80211_hw *hw,
365 			struct ieee80211_vif *vif)
366 {
367 	struct lbtf_private *priv = hw->priv;
368 	lbtf_deb_enter(LBTF_DEB_MACOPS);
369 	if (priv->vif != NULL)
370 		return -EOPNOTSUPP;
371 
372 	priv->vif = vif;
373 	switch (vif->type) {
374 	case NL80211_IFTYPE_MESH_POINT:
375 	case NL80211_IFTYPE_AP:
376 		lbtf_set_mode(priv, LBTF_AP_MODE);
377 		break;
378 	case NL80211_IFTYPE_STATION:
379 		lbtf_set_mode(priv, LBTF_STA_MODE);
380 		break;
381 	default:
382 		priv->vif = NULL;
383 		return -EOPNOTSUPP;
384 	}
385 	lbtf_set_mac_address(priv, (u8 *) vif->addr);
386 	lbtf_deb_leave(LBTF_DEB_MACOPS);
387 	return 0;
388 }
389 
lbtf_op_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)390 static void lbtf_op_remove_interface(struct ieee80211_hw *hw,
391 			struct ieee80211_vif *vif)
392 {
393 	struct lbtf_private *priv = hw->priv;
394 	lbtf_deb_enter(LBTF_DEB_MACOPS);
395 
396 	if (priv->vif->type == NL80211_IFTYPE_AP ||
397 	    priv->vif->type == NL80211_IFTYPE_MESH_POINT)
398 		lbtf_beacon_ctrl(priv, 0, 0);
399 	lbtf_set_mode(priv, LBTF_PASSIVE_MODE);
400 	lbtf_set_bssid(priv, 0, NULL);
401 	priv->vif = NULL;
402 	lbtf_deb_leave(LBTF_DEB_MACOPS);
403 }
404 
lbtf_op_config(struct ieee80211_hw * hw,u32 changed)405 static int lbtf_op_config(struct ieee80211_hw *hw, u32 changed)
406 {
407 	struct lbtf_private *priv = hw->priv;
408 	struct ieee80211_conf *conf = &hw->conf;
409 	lbtf_deb_enter(LBTF_DEB_MACOPS);
410 
411 	if (conf->channel->center_freq != priv->cur_freq) {
412 		priv->cur_freq = conf->channel->center_freq;
413 		lbtf_set_channel(priv, conf->channel->hw_value);
414 	}
415 	lbtf_deb_leave(LBTF_DEB_MACOPS);
416 	return 0;
417 }
418 
lbtf_op_prepare_multicast(struct ieee80211_hw * hw,struct netdev_hw_addr_list * mc_list)419 static u64 lbtf_op_prepare_multicast(struct ieee80211_hw *hw,
420 				     struct netdev_hw_addr_list *mc_list)
421 {
422 	struct lbtf_private *priv = hw->priv;
423 	int i;
424 	struct netdev_hw_addr *ha;
425 	int mc_count = netdev_hw_addr_list_count(mc_list);
426 
427 	if (!mc_count || mc_count > MRVDRV_MAX_MULTICAST_LIST_SIZE)
428 		return mc_count;
429 
430 	priv->nr_of_multicastmacaddr = mc_count;
431 	i = 0;
432 	netdev_hw_addr_list_for_each(ha, mc_list)
433 		memcpy(&priv->multicastlist[i++], ha->addr, ETH_ALEN);
434 
435 	return mc_count;
436 }
437 
438 #define SUPPORTED_FIF_FLAGS  (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)
lbtf_op_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * new_flags,u64 multicast)439 static void lbtf_op_configure_filter(struct ieee80211_hw *hw,
440 			unsigned int changed_flags,
441 			unsigned int *new_flags,
442 			u64 multicast)
443 {
444 	struct lbtf_private *priv = hw->priv;
445 	int old_mac_control = priv->mac_control;
446 
447 	lbtf_deb_enter(LBTF_DEB_MACOPS);
448 
449 	changed_flags &= SUPPORTED_FIF_FLAGS;
450 	*new_flags &= SUPPORTED_FIF_FLAGS;
451 
452 	if (!changed_flags) {
453 		lbtf_deb_leave(LBTF_DEB_MACOPS);
454 		return;
455 	}
456 
457 	if (*new_flags & (FIF_PROMISC_IN_BSS))
458 		priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
459 	else
460 		priv->mac_control &= ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
461 	if (*new_flags & (FIF_ALLMULTI) ||
462 	    multicast > MRVDRV_MAX_MULTICAST_LIST_SIZE) {
463 		priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
464 		priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
465 	} else if (multicast) {
466 		priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
467 		priv->mac_control &= ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
468 		lbtf_cmd_set_mac_multicast_addr(priv);
469 	} else {
470 		priv->mac_control &= ~(CMD_ACT_MAC_MULTICAST_ENABLE |
471 				       CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
472 		if (priv->nr_of_multicastmacaddr) {
473 			priv->nr_of_multicastmacaddr = 0;
474 			lbtf_cmd_set_mac_multicast_addr(priv);
475 		}
476 	}
477 
478 
479 	if (priv->mac_control != old_mac_control)
480 		lbtf_set_mac_control(priv);
481 
482 	lbtf_deb_leave(LBTF_DEB_MACOPS);
483 }
484 
lbtf_op_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u32 changes)485 static void lbtf_op_bss_info_changed(struct ieee80211_hw *hw,
486 			struct ieee80211_vif *vif,
487 			struct ieee80211_bss_conf *bss_conf,
488 			u32 changes)
489 {
490 	struct lbtf_private *priv = hw->priv;
491 	struct sk_buff *beacon;
492 	lbtf_deb_enter(LBTF_DEB_MACOPS);
493 
494 	if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_INT)) {
495 		switch (priv->vif->type) {
496 		case NL80211_IFTYPE_AP:
497 		case NL80211_IFTYPE_MESH_POINT:
498 			beacon = ieee80211_beacon_get(hw, vif);
499 			if (beacon) {
500 				lbtf_beacon_set(priv, beacon);
501 				kfree_skb(beacon);
502 				lbtf_beacon_ctrl(priv, 1,
503 						 bss_conf->beacon_int);
504 			}
505 			break;
506 		default:
507 			break;
508 		}
509 	}
510 
511 	if (changes & BSS_CHANGED_BSSID) {
512 		bool activate = !is_zero_ether_addr(bss_conf->bssid);
513 		lbtf_set_bssid(priv, activate, bss_conf->bssid);
514 	}
515 
516 	if (changes & BSS_CHANGED_ERP_PREAMBLE) {
517 		if (bss_conf->use_short_preamble)
518 			priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
519 		else
520 			priv->preamble = CMD_TYPE_LONG_PREAMBLE;
521 		lbtf_set_radio_control(priv);
522 	}
523 
524 	lbtf_deb_leave(LBTF_DEB_MACOPS);
525 }
526 
lbtf_op_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)527 static int lbtf_op_get_survey(struct ieee80211_hw *hw, int idx,
528 				struct survey_info *survey)
529 {
530 	struct lbtf_private *priv = hw->priv;
531 	struct ieee80211_conf *conf = &hw->conf;
532 
533 	if (idx != 0)
534 		return -ENOENT;
535 
536 	survey->channel = conf->channel;
537 	survey->filled = SURVEY_INFO_NOISE_DBM;
538 	survey->noise = priv->noise;
539 
540 	return 0;
541 }
542 
543 static const struct ieee80211_ops lbtf_ops = {
544 	.tx			= lbtf_op_tx,
545 	.start			= lbtf_op_start,
546 	.stop			= lbtf_op_stop,
547 	.add_interface		= lbtf_op_add_interface,
548 	.remove_interface	= lbtf_op_remove_interface,
549 	.config			= lbtf_op_config,
550 	.prepare_multicast	= lbtf_op_prepare_multicast,
551 	.configure_filter	= lbtf_op_configure_filter,
552 	.bss_info_changed	= lbtf_op_bss_info_changed,
553 	.get_survey		= lbtf_op_get_survey,
554 };
555 
lbtf_rx(struct lbtf_private * priv,struct sk_buff * skb)556 int lbtf_rx(struct lbtf_private *priv, struct sk_buff *skb)
557 {
558 	struct ieee80211_rx_status stats;
559 	struct rxpd *prxpd;
560 	int need_padding;
561 	unsigned int flags;
562 	struct ieee80211_hdr *hdr;
563 
564 	lbtf_deb_enter(LBTF_DEB_RX);
565 
566 	prxpd = (struct rxpd *) skb->data;
567 
568 	memset(&stats, 0, sizeof(stats));
569 	if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
570 		stats.flag |= RX_FLAG_FAILED_FCS_CRC;
571 	stats.freq = priv->cur_freq;
572 	stats.band = IEEE80211_BAND_2GHZ;
573 	stats.signal = prxpd->snr;
574 	priv->noise = prxpd->nf;
575 	/* Marvell rate index has a hole at value 4 */
576 	if (prxpd->rx_rate > 4)
577 		--prxpd->rx_rate;
578 	stats.rate_idx = prxpd->rx_rate;
579 	skb_pull(skb, sizeof(struct rxpd));
580 
581 	hdr = (struct ieee80211_hdr *)skb->data;
582 	flags = le32_to_cpu(*(__le32 *)(skb->data + 4));
583 
584 	need_padding = ieee80211_is_data_qos(hdr->frame_control);
585 	need_padding ^= ieee80211_has_a4(hdr->frame_control);
586 	need_padding ^= ieee80211_is_data_qos(hdr->frame_control) &&
587 			(*ieee80211_get_qos_ctl(hdr) &
588 			 IEEE80211_QOS_CONTROL_A_MSDU_PRESENT);
589 
590 	if (need_padding) {
591 		memmove(skb->data + 2, skb->data, skb->len);
592 		skb_reserve(skb, 2);
593 	}
594 
595 	memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
596 
597 	lbtf_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
598 	       skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
599 	lbtf_deb_hex(LBTF_DEB_RX, "RX Data", skb->data,
600 	             min_t(unsigned int, skb->len, 100));
601 
602 	ieee80211_rx_irqsafe(priv->hw, skb);
603 
604 	lbtf_deb_leave(LBTF_DEB_RX);
605 	return 0;
606 }
607 EXPORT_SYMBOL_GPL(lbtf_rx);
608 
609 /**
610  * lbtf_add_card: Add and initialize the card, no fw upload yet.
611  *
612  *  @card    A pointer to card
613  *
614  *  Returns: pointer to struct lbtf_priv.
615  */
lbtf_add_card(void * card,struct device * dmdev)616 struct lbtf_private *lbtf_add_card(void *card, struct device *dmdev)
617 {
618 	struct ieee80211_hw *hw;
619 	struct lbtf_private *priv = NULL;
620 
621 	lbtf_deb_enter(LBTF_DEB_MAIN);
622 
623 	hw = ieee80211_alloc_hw(sizeof(struct lbtf_private), &lbtf_ops);
624 	if (!hw)
625 		goto done;
626 
627 	priv = hw->priv;
628 	if (lbtf_init_adapter(priv))
629 		goto err_init_adapter;
630 
631 	priv->hw = hw;
632 	priv->card = card;
633 	priv->tx_skb = NULL;
634 
635 	hw->queues = 1;
636 	hw->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
637 	hw->extra_tx_headroom = sizeof(struct txpd);
638 	memcpy(priv->channels, lbtf_channels, sizeof(lbtf_channels));
639 	memcpy(priv->rates, lbtf_rates, sizeof(lbtf_rates));
640 	priv->band.n_bitrates = ARRAY_SIZE(lbtf_rates);
641 	priv->band.bitrates = priv->rates;
642 	priv->band.n_channels = ARRAY_SIZE(lbtf_channels);
643 	priv->band.channels = priv->channels;
644 	hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
645 	hw->wiphy->interface_modes =
646 		BIT(NL80211_IFTYPE_STATION) |
647 		BIT(NL80211_IFTYPE_ADHOC);
648 	skb_queue_head_init(&priv->bc_ps_buf);
649 
650 	SET_IEEE80211_DEV(hw, dmdev);
651 
652 	INIT_WORK(&priv->cmd_work, lbtf_cmd_work);
653 	INIT_WORK(&priv->tx_work, lbtf_tx_work);
654 	if (ieee80211_register_hw(hw))
655 		goto err_init_adapter;
656 
657 	goto done;
658 
659 err_init_adapter:
660 	lbtf_free_adapter(priv);
661 	ieee80211_free_hw(hw);
662 	priv = NULL;
663 
664 done:
665 	lbtf_deb_leave_args(LBTF_DEB_MAIN, "priv %p", priv);
666 	return priv;
667 }
668 EXPORT_SYMBOL_GPL(lbtf_add_card);
669 
670 
lbtf_remove_card(struct lbtf_private * priv)671 int lbtf_remove_card(struct lbtf_private *priv)
672 {
673 	struct ieee80211_hw *hw = priv->hw;
674 
675 	lbtf_deb_enter(LBTF_DEB_MAIN);
676 
677 	priv->surpriseremoved = 1;
678 	del_timer(&priv->command_timer);
679 	lbtf_free_adapter(priv);
680 	priv->hw = NULL;
681 	ieee80211_unregister_hw(hw);
682 	ieee80211_free_hw(hw);
683 
684     lbtf_deb_leave(LBTF_DEB_MAIN);
685 	return 0;
686 }
687 EXPORT_SYMBOL_GPL(lbtf_remove_card);
688 
lbtf_send_tx_feedback(struct lbtf_private * priv,u8 retrycnt,u8 fail)689 void lbtf_send_tx_feedback(struct lbtf_private *priv, u8 retrycnt, u8 fail)
690 {
691 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(priv->tx_skb);
692 
693 	ieee80211_tx_info_clear_status(info);
694 	/*
695 	 * Commented out, otherwise we never go beyond 1Mbit/s using mac80211
696 	 * default pid rc algorithm.
697 	 *
698 	 * info->status.retry_count = MRVL_DEFAULT_RETRIES - retrycnt;
699 	 */
700 	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) && !fail)
701 		info->flags |= IEEE80211_TX_STAT_ACK;
702 	skb_pull(priv->tx_skb, sizeof(struct txpd));
703 	ieee80211_tx_status_irqsafe(priv->hw, priv->tx_skb);
704 	priv->tx_skb = NULL;
705 	if (!priv->skb_to_tx && skb_queue_empty(&priv->bc_ps_buf))
706 		ieee80211_wake_queues(priv->hw);
707 	else
708 		queue_work(lbtf_wq, &priv->tx_work);
709 }
710 EXPORT_SYMBOL_GPL(lbtf_send_tx_feedback);
711 
lbtf_bcn_sent(struct lbtf_private * priv)712 void lbtf_bcn_sent(struct lbtf_private *priv)
713 {
714 	struct sk_buff *skb = NULL;
715 
716 	if (priv->vif->type != NL80211_IFTYPE_AP)
717 		return;
718 
719 	if (skb_queue_empty(&priv->bc_ps_buf)) {
720 		bool tx_buff_bc = 0;
721 
722 		while ((skb = ieee80211_get_buffered_bc(priv->hw, priv->vif))) {
723 			skb_queue_tail(&priv->bc_ps_buf, skb);
724 			tx_buff_bc = 1;
725 		}
726 		if (tx_buff_bc) {
727 			ieee80211_stop_queues(priv->hw);
728 			queue_work(lbtf_wq, &priv->tx_work);
729 		}
730 	}
731 
732 	skb = ieee80211_beacon_get(priv->hw, priv->vif);
733 
734 	if (skb) {
735 		lbtf_beacon_set(priv, skb);
736 		kfree_skb(skb);
737 	}
738 }
739 EXPORT_SYMBOL_GPL(lbtf_bcn_sent);
740 
lbtf_init_module(void)741 static int __init lbtf_init_module(void)
742 {
743 	lbtf_deb_enter(LBTF_DEB_MAIN);
744 	lbtf_wq = create_workqueue("libertastf");
745 	if (lbtf_wq == NULL) {
746 		printk(KERN_ERR "libertastf: couldn't create workqueue\n");
747 		return -ENOMEM;
748 	}
749 	lbtf_deb_leave(LBTF_DEB_MAIN);
750 	return 0;
751 }
752 
lbtf_exit_module(void)753 static void __exit lbtf_exit_module(void)
754 {
755 	lbtf_deb_enter(LBTF_DEB_MAIN);
756 	destroy_workqueue(lbtf_wq);
757 	lbtf_deb_leave(LBTF_DEB_MAIN);
758 }
759 
760 module_init(lbtf_init_module);
761 module_exit(lbtf_exit_module);
762 
763 MODULE_DESCRIPTION("Libertas WLAN Thinfirm Driver Library");
764 MODULE_AUTHOR("Cozybit Inc.");
765 MODULE_LICENSE("GPL");
766