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