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