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