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 urb		*urb;
78 };
79 
80 struct ar5523_rx_data {
81 	struct	list_head	list;
82 	struct ar5523		*ar;
83 	struct urb		*urb;
84 	struct sk_buff		*skb;
85 };
86 
87 struct ar5523 {
88 	struct usb_device	*dev;
89 	struct ieee80211_hw	*hw;
90 
91 	unsigned long		flags;
92 	struct mutex		mutex;
93 	struct workqueue_struct *wq;
94 
95 	struct ar5523_tx_cmd	tx_cmd;
96 
97 	struct delayed_work	stat_work;
98 
99 	struct timer_list	tx_wd_timer;
100 	struct work_struct	tx_wd_work;
101 	struct work_struct	tx_work;
102 	struct list_head	tx_queue_pending;
103 	struct list_head	tx_queue_submitted;
104 	spinlock_t		tx_data_list_lock;
105 	wait_queue_head_t	tx_flush_waitq;
106 
107 	/* Queued + Submitted TX frames */
108 	atomic_t		tx_nr_total;
109 
110 	/* Submitted TX frames */
111 	atomic_t		tx_nr_pending;
112 
113 	void			*rx_cmd_buf;
114 	struct urb		*rx_cmd_urb;
115 
116 	struct ar5523_rx_data	rx_data[AR5523_RX_DATA_COUNT];
117 	spinlock_t		rx_data_list_lock;
118 	struct list_head	rx_data_free;
119 	struct list_head	rx_data_used;
120 	atomic_t		rx_data_free_cnt;
121 
122 	struct work_struct	rx_refill_work;
123 
124 	unsigned int		rxbufsz;
125 	u8			serial[16];
126 
127 	struct ieee80211_channel channels[14];
128 	struct ieee80211_rate	rates[12];
129 	struct ieee80211_supported_band band;
130 	struct ieee80211_vif	*vif;
131 };
132 
133 /* flags for sending firmware commands */
134 #define AR5523_CMD_FLAG_READ	(1 << 1)
135 #define AR5523_CMD_FLAG_MAGIC	(1 << 2)
136 
137 #define ar5523_dbg(ar, format, arg...) \
138 	dev_dbg(&(ar)->dev->dev, format, ## arg)
139 
140 /* On USB hot-unplug there can be a lot of URBs in flight and they'll all
141  * fail. Instead of dealing with them in every possible place just surpress
142  * any messages on USB disconnect.
143  */
144 #define ar5523_err(ar, format, arg...) \
145 do { \
146 	if (!test_bit(AR5523_USB_DISCONNECTED, &ar->flags)) { \
147 		dev_err(&(ar)->dev->dev, format, ## arg); \
148 	} \
149 } while (0)
150 #define ar5523_info(ar, format, arg...)	\
151 	dev_info(&(ar)->dev->dev, format, ## arg)
152