1 /*
2  * Copyright (c) 2006 Damien Bergamini <damien.bergamini@free.fr>
3  * Copyright (c) 2006 Sam Leffler, Errno Consulting
4  * Copyright (c) 2007 Christoph Hellwig <hch@lst.de>
5  * Copyright (c) 2008-2009 Weongyo Jeong <weongyo@freebsd.org>
6  * Copyright (c) 2012 Pontus Fuchs <pontus.fuchs@gmail.com>
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #define AR5523_FLAG_PRE_FIRMWARE	(1 << 0)
22 #define AR5523_FLAG_ABG			(1 << 1)
23 
24 #define AR5523_FIRMWARE_FILE	"ar5523.bin"
25 
26 #define AR5523_CMD_TX_PIPE	0x01
27 #define	AR5523_DATA_TX_PIPE	0x02
28 #define	AR5523_CMD_RX_PIPE	0x81
29 #define	AR5523_DATA_RX_PIPE	0x82
30 
31 #define ar5523_cmd_tx_pipe(dev) \
32 	usb_sndbulkpipe((dev), AR5523_CMD_TX_PIPE)
33 #define ar5523_data_tx_pipe(dev) \
34 	usb_sndbulkpipe((dev), AR5523_DATA_TX_PIPE)
35 #define ar5523_cmd_rx_pipe(dev) \
36 	usb_rcvbulkpipe((dev), AR5523_CMD_RX_PIPE)
37 #define ar5523_data_rx_pipe(dev) \
38 	usb_rcvbulkpipe((dev), AR5523_DATA_RX_PIPE)
39 
40 #define	AR5523_DATA_TIMEOUT	10000
41 #define	AR5523_CMD_TIMEOUT	1000
42 
43 #define AR5523_TX_DATA_COUNT		8
44 #define AR5523_TX_DATA_RESTART_COUNT	2
45 #define AR5523_RX_DATA_COUNT		16
46 #define AR5523_RX_DATA_REFILL_COUNT	8
47 
48 #define AR5523_CMD_ID	1
49 #define AR5523_DATA_ID	2
50 
51 #define AR5523_TX_WD_TIMEOUT	(HZ * 2)
52 #define AR5523_FLUSH_TIMEOUT	(HZ * 3)
53 
54 enum AR5523_flags {
55 	AR5523_HW_UP,
56 	AR5523_USB_DISCONNECTED,
57 	AR5523_CONNECTED
58 };
59 
60 struct ar5523_tx_cmd {
61 	struct ar5523		*ar;
62 	struct urb		*urb_tx;
63 	void			*buf_tx;
64 	void			*odata;
65 	int			olen;
66 	int			flags;
67 	int			res;
68 	struct completion	done;
69 };
70 
71 /* This struct is placed in tx_info->driver_data. It must not be larger
72  *  than IEEE80211_TX_INFO_DRIVER_DATA_SIZE.
73  */
74 struct ar5523_tx_data {
75 	struct list_head	list;
76 	struct ar5523		*ar;
77 	struct sk_buff		*skb;
78 	struct urb		*urb;
79 };
80 
81 struct ar5523_rx_data {
82 	struct	list_head	list;
83 	struct ar5523		*ar;
84 	struct urb		*urb;
85 	struct sk_buff		*skb;
86 };
87 
88 struct ar5523 {
89 	struct usb_device	*dev;
90 	struct ieee80211_hw	*hw;
91 
92 	unsigned long		flags;
93 	struct mutex		mutex;
94 	struct workqueue_struct *wq;
95 
96 	struct ar5523_tx_cmd	tx_cmd;
97 
98 	struct delayed_work	stat_work;
99 
100 	struct timer_list	tx_wd_timer;
101 	struct work_struct	tx_wd_work;
102 	struct work_struct	tx_work;
103 	struct list_head	tx_queue_pending;
104 	struct list_head	tx_queue_submitted;
105 	spinlock_t		tx_data_list_lock;
106 	wait_queue_head_t	tx_flush_waitq;
107 
108 	/* Queued + Submitted TX frames */
109 	atomic_t		tx_nr_total;
110 
111 	/* Submitted TX frames */
112 	atomic_t		tx_nr_pending;
113 
114 	void			*rx_cmd_buf;
115 	struct urb		*rx_cmd_urb;
116 
117 	struct ar5523_rx_data	rx_data[AR5523_RX_DATA_COUNT];
118 	spinlock_t		rx_data_list_lock;
119 	struct list_head	rx_data_free;
120 	struct list_head	rx_data_used;
121 	atomic_t		rx_data_free_cnt;
122 
123 	struct work_struct	rx_refill_work;
124 
125 	int			rxbufsz;
126 	u8			serial[16];
127 
128 	struct ieee80211_channel channels[14];
129 	struct ieee80211_rate	rates[12];
130 	struct ieee80211_supported_band band;
131 	struct ieee80211_vif	*vif;
132 };
133 
134 /* flags for sending firmware commands */
135 #define AR5523_CMD_FLAG_READ	(1 << 1)
136 #define AR5523_CMD_FLAG_MAGIC	(1 << 2)
137 
138 #define ar5523_dbg(ar, format, arg...) \
139 	dev_dbg(&(ar)->dev->dev, format, ## arg)
140 
141 /* On USB hot-unplug there can be a lot of URBs in flight and they'll all
142  * fail. Instead of dealing with them in every possible place just surpress
143  * any messages on USB disconnect.
144  */
145 #define ar5523_err(ar, format, arg...) \
146 do { \
147 	if (!test_bit(AR5523_USB_DISCONNECTED, &ar->flags)) { \
148 		dev_err(&(ar)->dev->dev, format, ## arg); \
149 	} \
150 } while (0)
151 #define ar5523_info(ar, format, arg...)	\
152 	dev_info(&(ar)->dev->dev, format, ## arg)
153