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