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