xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/ralink/rt2x00/rt2x00usb.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun 	Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
4*4882a593Smuzhiyun 	Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
5*4882a593Smuzhiyun 	<http://rt2x00.serialmonkey.com>
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun 	Module: rt2x00usb
11*4882a593Smuzhiyun 	Abstract: rt2x00 generic usb device routines.
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <linux/usb.h>
18*4882a593Smuzhiyun #include <linux/bug.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include "rt2x00.h"
21*4882a593Smuzhiyun #include "rt2x00usb.h"
22*4882a593Smuzhiyun 
rt2x00usb_check_usb_error(struct rt2x00_dev * rt2x00dev,int status)23*4882a593Smuzhiyun static bool rt2x00usb_check_usb_error(struct rt2x00_dev *rt2x00dev, int status)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	if (status == -ENODEV || status == -ENOENT)
26*4882a593Smuzhiyun 		return true;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	if (!test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
29*4882a593Smuzhiyun 		return false;
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	if (status == -EPROTO || status == -ETIMEDOUT)
32*4882a593Smuzhiyun 		rt2x00dev->num_proto_errs++;
33*4882a593Smuzhiyun 	else
34*4882a593Smuzhiyun 		rt2x00dev->num_proto_errs = 0;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	if (rt2x00dev->num_proto_errs > 3)
37*4882a593Smuzhiyun 		return true;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	return false;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun  * Interfacing with the HW.
44*4882a593Smuzhiyun  */
rt2x00usb_vendor_request(struct rt2x00_dev * rt2x00dev,const u8 request,const u8 requesttype,const u16 offset,const u16 value,void * buffer,const u16 buffer_length,const int timeout)45*4882a593Smuzhiyun int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
46*4882a593Smuzhiyun 			     const u8 request, const u8 requesttype,
47*4882a593Smuzhiyun 			     const u16 offset, const u16 value,
48*4882a593Smuzhiyun 			     void *buffer, const u16 buffer_length,
49*4882a593Smuzhiyun 			     const int timeout)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
52*4882a593Smuzhiyun 	int status;
53*4882a593Smuzhiyun 	unsigned int pipe =
54*4882a593Smuzhiyun 	    (requesttype == USB_VENDOR_REQUEST_IN) ?
55*4882a593Smuzhiyun 	    usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
56*4882a593Smuzhiyun 	unsigned long expire = jiffies + msecs_to_jiffies(timeout);
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
59*4882a593Smuzhiyun 		return -ENODEV;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	do {
62*4882a593Smuzhiyun 		status = usb_control_msg(usb_dev, pipe, request, requesttype,
63*4882a593Smuzhiyun 					 value, offset, buffer, buffer_length,
64*4882a593Smuzhiyun 					 timeout / 2);
65*4882a593Smuzhiyun 		if (status >= 0)
66*4882a593Smuzhiyun 			return 0;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 		if (rt2x00usb_check_usb_error(rt2x00dev, status)) {
69*4882a593Smuzhiyun 			/* Device has disappeared. */
70*4882a593Smuzhiyun 			clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
71*4882a593Smuzhiyun 			break;
72*4882a593Smuzhiyun 		}
73*4882a593Smuzhiyun 	} while (time_before(jiffies, expire));
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	rt2x00_err(rt2x00dev,
76*4882a593Smuzhiyun 		   "Vendor Request 0x%02x failed for offset 0x%04x with error %d\n",
77*4882a593Smuzhiyun 		   request, offset, status);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	return status;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
82*4882a593Smuzhiyun 
rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev * rt2x00dev,const u8 request,const u8 requesttype,const u16 offset,void * buffer,const u16 buffer_length,const int timeout)83*4882a593Smuzhiyun int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
84*4882a593Smuzhiyun 				   const u8 request, const u8 requesttype,
85*4882a593Smuzhiyun 				   const u16 offset, void *buffer,
86*4882a593Smuzhiyun 				   const u16 buffer_length, const int timeout)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	int status;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	BUG_ON(!mutex_is_locked(&rt2x00dev->csr_mutex));
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	/*
93*4882a593Smuzhiyun 	 * Check for Cache availability.
94*4882a593Smuzhiyun 	 */
95*4882a593Smuzhiyun 	if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
96*4882a593Smuzhiyun 		rt2x00_err(rt2x00dev, "CSR cache not available\n");
97*4882a593Smuzhiyun 		return -ENOMEM;
98*4882a593Smuzhiyun 	}
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	if (requesttype == USB_VENDOR_REQUEST_OUT)
101*4882a593Smuzhiyun 		memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
104*4882a593Smuzhiyun 					  offset, 0, rt2x00dev->csr.cache,
105*4882a593Smuzhiyun 					  buffer_length, timeout);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	if (!status && requesttype == USB_VENDOR_REQUEST_IN)
108*4882a593Smuzhiyun 		memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	return status;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
113*4882a593Smuzhiyun 
rt2x00usb_vendor_request_buff(struct rt2x00_dev * rt2x00dev,const u8 request,const u8 requesttype,const u16 offset,void * buffer,const u16 buffer_length)114*4882a593Smuzhiyun int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
115*4882a593Smuzhiyun 				  const u8 request, const u8 requesttype,
116*4882a593Smuzhiyun 				  const u16 offset, void *buffer,
117*4882a593Smuzhiyun 				  const u16 buffer_length)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	int status = 0;
120*4882a593Smuzhiyun 	unsigned char *tb;
121*4882a593Smuzhiyun 	u16 off, len, bsize;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	mutex_lock(&rt2x00dev->csr_mutex);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	tb  = (char *)buffer;
126*4882a593Smuzhiyun 	off = offset;
127*4882a593Smuzhiyun 	len = buffer_length;
128*4882a593Smuzhiyun 	while (len && !status) {
129*4882a593Smuzhiyun 		bsize = min_t(u16, CSR_CACHE_SIZE, len);
130*4882a593Smuzhiyun 		status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
131*4882a593Smuzhiyun 							requesttype, off, tb,
132*4882a593Smuzhiyun 							bsize, REGISTER_TIMEOUT);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 		tb  += bsize;
135*4882a593Smuzhiyun 		len -= bsize;
136*4882a593Smuzhiyun 		off += bsize;
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	mutex_unlock(&rt2x00dev->csr_mutex);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	return status;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
144*4882a593Smuzhiyun 
rt2x00usb_regbusy_read(struct rt2x00_dev * rt2x00dev,const unsigned int offset,const struct rt2x00_field32 field,u32 * reg)145*4882a593Smuzhiyun int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
146*4882a593Smuzhiyun 			   const unsigned int offset,
147*4882a593Smuzhiyun 			   const struct rt2x00_field32 field,
148*4882a593Smuzhiyun 			   u32 *reg)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	unsigned int i;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
153*4882a593Smuzhiyun 		return -ENODEV;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	for (i = 0; i < REGISTER_USB_BUSY_COUNT; i++) {
156*4882a593Smuzhiyun 		*reg = rt2x00usb_register_read_lock(rt2x00dev, offset);
157*4882a593Smuzhiyun 		if (!rt2x00_get_field32(*reg, field))
158*4882a593Smuzhiyun 			return 1;
159*4882a593Smuzhiyun 		udelay(REGISTER_BUSY_DELAY);
160*4882a593Smuzhiyun 	}
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	rt2x00_err(rt2x00dev, "Indirect register access failed: offset=0x%.08x, value=0x%.08x\n",
163*4882a593Smuzhiyun 		   offset, *reg);
164*4882a593Smuzhiyun 	*reg = ~0;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	return 0;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun struct rt2x00_async_read_data {
172*4882a593Smuzhiyun 	__le32 reg;
173*4882a593Smuzhiyun 	struct usb_ctrlrequest cr;
174*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev;
175*4882a593Smuzhiyun 	bool (*callback)(struct rt2x00_dev *, int, u32);
176*4882a593Smuzhiyun };
177*4882a593Smuzhiyun 
rt2x00usb_register_read_async_cb(struct urb * urb)178*4882a593Smuzhiyun static void rt2x00usb_register_read_async_cb(struct urb *urb)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	struct rt2x00_async_read_data *rd = urb->context;
181*4882a593Smuzhiyun 	if (rd->callback(rd->rt2x00dev, urb->status, le32_to_cpu(rd->reg))) {
182*4882a593Smuzhiyun 		usb_anchor_urb(urb, rd->rt2x00dev->anchor);
183*4882a593Smuzhiyun 		if (usb_submit_urb(urb, GFP_ATOMIC) < 0) {
184*4882a593Smuzhiyun 			usb_unanchor_urb(urb);
185*4882a593Smuzhiyun 			kfree(rd);
186*4882a593Smuzhiyun 		}
187*4882a593Smuzhiyun 	} else
188*4882a593Smuzhiyun 		kfree(rd);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
rt2x00usb_register_read_async(struct rt2x00_dev * rt2x00dev,const unsigned int offset,bool (* callback)(struct rt2x00_dev *,int,u32))191*4882a593Smuzhiyun void rt2x00usb_register_read_async(struct rt2x00_dev *rt2x00dev,
192*4882a593Smuzhiyun 				   const unsigned int offset,
193*4882a593Smuzhiyun 				   bool (*callback)(struct rt2x00_dev*, int, u32))
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
196*4882a593Smuzhiyun 	struct urb *urb;
197*4882a593Smuzhiyun 	struct rt2x00_async_read_data *rd;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
200*4882a593Smuzhiyun 	if (!rd)
201*4882a593Smuzhiyun 		return;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	urb = usb_alloc_urb(0, GFP_ATOMIC);
204*4882a593Smuzhiyun 	if (!urb) {
205*4882a593Smuzhiyun 		kfree(rd);
206*4882a593Smuzhiyun 		return;
207*4882a593Smuzhiyun 	}
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	rd->rt2x00dev = rt2x00dev;
210*4882a593Smuzhiyun 	rd->callback = callback;
211*4882a593Smuzhiyun 	rd->cr.bRequestType = USB_VENDOR_REQUEST_IN;
212*4882a593Smuzhiyun 	rd->cr.bRequest = USB_MULTI_READ;
213*4882a593Smuzhiyun 	rd->cr.wValue = 0;
214*4882a593Smuzhiyun 	rd->cr.wIndex = cpu_to_le16(offset);
215*4882a593Smuzhiyun 	rd->cr.wLength = cpu_to_le16(sizeof(u32));
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	usb_fill_control_urb(urb, usb_dev, usb_rcvctrlpipe(usb_dev, 0),
218*4882a593Smuzhiyun 			     (unsigned char *)(&rd->cr), &rd->reg, sizeof(rd->reg),
219*4882a593Smuzhiyun 			     rt2x00usb_register_read_async_cb, rd);
220*4882a593Smuzhiyun 	usb_anchor_urb(urb, rt2x00dev->anchor);
221*4882a593Smuzhiyun 	if (usb_submit_urb(urb, GFP_ATOMIC) < 0) {
222*4882a593Smuzhiyun 		usb_unanchor_urb(urb);
223*4882a593Smuzhiyun 		kfree(rd);
224*4882a593Smuzhiyun 	}
225*4882a593Smuzhiyun 	usb_free_urb(urb);
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_register_read_async);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun /*
230*4882a593Smuzhiyun  * TX data handlers.
231*4882a593Smuzhiyun  */
rt2x00usb_work_txdone_entry(struct queue_entry * entry)232*4882a593Smuzhiyun static void rt2x00usb_work_txdone_entry(struct queue_entry *entry)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun 	/*
235*4882a593Smuzhiyun 	 * If the transfer to hardware succeeded, it does not mean the
236*4882a593Smuzhiyun 	 * frame was send out correctly. It only means the frame
237*4882a593Smuzhiyun 	 * was successfully pushed to the hardware, we have no
238*4882a593Smuzhiyun 	 * way to determine the transmission status right now.
239*4882a593Smuzhiyun 	 * (Only indirectly by looking at the failed TX counters
240*4882a593Smuzhiyun 	 * in the register).
241*4882a593Smuzhiyun 	 */
242*4882a593Smuzhiyun 	if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
243*4882a593Smuzhiyun 		rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
244*4882a593Smuzhiyun 	else
245*4882a593Smuzhiyun 		rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun 
rt2x00usb_work_txdone(struct work_struct * work)248*4882a593Smuzhiyun static void rt2x00usb_work_txdone(struct work_struct *work)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev =
251*4882a593Smuzhiyun 	    container_of(work, struct rt2x00_dev, txdone_work);
252*4882a593Smuzhiyun 	struct data_queue *queue;
253*4882a593Smuzhiyun 	struct queue_entry *entry;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	tx_queue_for_each(rt2x00dev, queue) {
256*4882a593Smuzhiyun 		while (!rt2x00queue_empty(queue)) {
257*4882a593Smuzhiyun 			entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 			if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
260*4882a593Smuzhiyun 			    !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
261*4882a593Smuzhiyun 				break;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 			rt2x00usb_work_txdone_entry(entry);
264*4882a593Smuzhiyun 		}
265*4882a593Smuzhiyun 	}
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
rt2x00usb_interrupt_txdone(struct urb * urb)268*4882a593Smuzhiyun static void rt2x00usb_interrupt_txdone(struct urb *urb)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	struct queue_entry *entry = (struct queue_entry *)urb->context;
271*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
274*4882a593Smuzhiyun 		return;
275*4882a593Smuzhiyun 	/*
276*4882a593Smuzhiyun 	 * Check if the frame was correctly uploaded
277*4882a593Smuzhiyun 	 */
278*4882a593Smuzhiyun 	if (urb->status)
279*4882a593Smuzhiyun 		set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
280*4882a593Smuzhiyun 	/*
281*4882a593Smuzhiyun 	 * Report the frame as DMA done
282*4882a593Smuzhiyun 	 */
283*4882a593Smuzhiyun 	rt2x00lib_dmadone(entry);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	if (rt2x00dev->ops->lib->tx_dma_done)
286*4882a593Smuzhiyun 		rt2x00dev->ops->lib->tx_dma_done(entry);
287*4882a593Smuzhiyun 	/*
288*4882a593Smuzhiyun 	 * Schedule the delayed work for reading the TX status
289*4882a593Smuzhiyun 	 * from the device.
290*4882a593Smuzhiyun 	 */
291*4882a593Smuzhiyun 	if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_TXSTATUS_FIFO) ||
292*4882a593Smuzhiyun 	    !kfifo_is_empty(&rt2x00dev->txstatus_fifo))
293*4882a593Smuzhiyun 		queue_work(rt2x00dev->workqueue, &rt2x00dev->txdone_work);
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
rt2x00usb_kick_tx_entry(struct queue_entry * entry,void * data)296*4882a593Smuzhiyun static bool rt2x00usb_kick_tx_entry(struct queue_entry *entry, void *data)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
299*4882a593Smuzhiyun 	struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
300*4882a593Smuzhiyun 	struct queue_entry_priv_usb *entry_priv = entry->priv_data;
301*4882a593Smuzhiyun 	u32 length;
302*4882a593Smuzhiyun 	int status;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	if (!test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags) ||
305*4882a593Smuzhiyun 	    test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
306*4882a593Smuzhiyun 		return false;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	/*
309*4882a593Smuzhiyun 	 * USB devices require certain padding at the end of each frame
310*4882a593Smuzhiyun 	 * and urb. Those paddings are not included in skbs. Pass entry
311*4882a593Smuzhiyun 	 * to the driver to determine what the overall length should be.
312*4882a593Smuzhiyun 	 */
313*4882a593Smuzhiyun 	length = rt2x00dev->ops->lib->get_tx_data_len(entry);
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	status = skb_padto(entry->skb, length);
316*4882a593Smuzhiyun 	if (unlikely(status)) {
317*4882a593Smuzhiyun 		/* TODO: report something more appropriate than IO_FAILED. */
318*4882a593Smuzhiyun 		rt2x00_warn(rt2x00dev, "TX SKB padding error, out of memory\n");
319*4882a593Smuzhiyun 		set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
320*4882a593Smuzhiyun 		rt2x00lib_dmadone(entry);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 		return false;
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	usb_fill_bulk_urb(entry_priv->urb, usb_dev,
326*4882a593Smuzhiyun 			  usb_sndbulkpipe(usb_dev, entry->queue->usb_endpoint),
327*4882a593Smuzhiyun 			  entry->skb->data, length,
328*4882a593Smuzhiyun 			  rt2x00usb_interrupt_txdone, entry);
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
331*4882a593Smuzhiyun 	if (status) {
332*4882a593Smuzhiyun 		if (rt2x00usb_check_usb_error(rt2x00dev, status))
333*4882a593Smuzhiyun 			clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
334*4882a593Smuzhiyun 		set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
335*4882a593Smuzhiyun 		rt2x00lib_dmadone(entry);
336*4882a593Smuzhiyun 	}
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	return false;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun /*
342*4882a593Smuzhiyun  * RX data handlers.
343*4882a593Smuzhiyun  */
rt2x00usb_work_rxdone(struct work_struct * work)344*4882a593Smuzhiyun static void rt2x00usb_work_rxdone(struct work_struct *work)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev =
347*4882a593Smuzhiyun 	    container_of(work, struct rt2x00_dev, rxdone_work);
348*4882a593Smuzhiyun 	struct queue_entry *entry;
349*4882a593Smuzhiyun 	struct skb_frame_desc *skbdesc;
350*4882a593Smuzhiyun 	u8 rxd[32];
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	while (!rt2x00queue_empty(rt2x00dev->rx)) {
353*4882a593Smuzhiyun 		entry = rt2x00queue_get_entry(rt2x00dev->rx, Q_INDEX_DONE);
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 		if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
356*4882a593Smuzhiyun 			break;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 		/*
359*4882a593Smuzhiyun 		 * Fill in desc fields of the skb descriptor
360*4882a593Smuzhiyun 		 */
361*4882a593Smuzhiyun 		skbdesc = get_skb_frame_desc(entry->skb);
362*4882a593Smuzhiyun 		skbdesc->desc = rxd;
363*4882a593Smuzhiyun 		skbdesc->desc_len = entry->queue->desc_size;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 		/*
366*4882a593Smuzhiyun 		 * Send the frame to rt2x00lib for further processing.
367*4882a593Smuzhiyun 		 */
368*4882a593Smuzhiyun 		rt2x00lib_rxdone(entry, GFP_KERNEL);
369*4882a593Smuzhiyun 	}
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun 
rt2x00usb_interrupt_rxdone(struct urb * urb)372*4882a593Smuzhiyun static void rt2x00usb_interrupt_rxdone(struct urb *urb)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun 	struct queue_entry *entry = (struct queue_entry *)urb->context;
375*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
378*4882a593Smuzhiyun 		return;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	/*
381*4882a593Smuzhiyun 	 * Check if the received data is simply too small
382*4882a593Smuzhiyun 	 * to be actually valid, or if the urb is signaling
383*4882a593Smuzhiyun 	 * a problem.
384*4882a593Smuzhiyun 	 */
385*4882a593Smuzhiyun 	if (urb->actual_length < entry->queue->desc_size || urb->status)
386*4882a593Smuzhiyun 		set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	/*
389*4882a593Smuzhiyun 	 * Report the frame as DMA done
390*4882a593Smuzhiyun 	 */
391*4882a593Smuzhiyun 	rt2x00lib_dmadone(entry);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	/*
394*4882a593Smuzhiyun 	 * Schedule the delayed work for processing RX data
395*4882a593Smuzhiyun 	 */
396*4882a593Smuzhiyun 	queue_work(rt2x00dev->workqueue, &rt2x00dev->rxdone_work);
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
rt2x00usb_kick_rx_entry(struct queue_entry * entry,void * data)399*4882a593Smuzhiyun static bool rt2x00usb_kick_rx_entry(struct queue_entry *entry, void *data)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
402*4882a593Smuzhiyun 	struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
403*4882a593Smuzhiyun 	struct queue_entry_priv_usb *entry_priv = entry->priv_data;
404*4882a593Smuzhiyun 	int status;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
407*4882a593Smuzhiyun 		return false;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	rt2x00lib_dmastart(entry);
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	usb_fill_bulk_urb(entry_priv->urb, usb_dev,
412*4882a593Smuzhiyun 			  usb_rcvbulkpipe(usb_dev, entry->queue->usb_endpoint),
413*4882a593Smuzhiyun 			  entry->skb->data, entry->skb->len,
414*4882a593Smuzhiyun 			  rt2x00usb_interrupt_rxdone, entry);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
417*4882a593Smuzhiyun 	if (status) {
418*4882a593Smuzhiyun 		if (rt2x00usb_check_usb_error(rt2x00dev, status))
419*4882a593Smuzhiyun 			clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
420*4882a593Smuzhiyun 		set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
421*4882a593Smuzhiyun 		rt2x00lib_dmadone(entry);
422*4882a593Smuzhiyun 	}
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	return false;
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun 
rt2x00usb_kick_queue(struct data_queue * queue)427*4882a593Smuzhiyun void rt2x00usb_kick_queue(struct data_queue *queue)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun 	switch (queue->qid) {
430*4882a593Smuzhiyun 	case QID_AC_VO:
431*4882a593Smuzhiyun 	case QID_AC_VI:
432*4882a593Smuzhiyun 	case QID_AC_BE:
433*4882a593Smuzhiyun 	case QID_AC_BK:
434*4882a593Smuzhiyun 		if (!rt2x00queue_empty(queue))
435*4882a593Smuzhiyun 			rt2x00queue_for_each_entry(queue,
436*4882a593Smuzhiyun 						   Q_INDEX_DONE,
437*4882a593Smuzhiyun 						   Q_INDEX,
438*4882a593Smuzhiyun 						   NULL,
439*4882a593Smuzhiyun 						   rt2x00usb_kick_tx_entry);
440*4882a593Smuzhiyun 		break;
441*4882a593Smuzhiyun 	case QID_RX:
442*4882a593Smuzhiyun 		if (!rt2x00queue_full(queue))
443*4882a593Smuzhiyun 			rt2x00queue_for_each_entry(queue,
444*4882a593Smuzhiyun 						   Q_INDEX,
445*4882a593Smuzhiyun 						   Q_INDEX_DONE,
446*4882a593Smuzhiyun 						   NULL,
447*4882a593Smuzhiyun 						   rt2x00usb_kick_rx_entry);
448*4882a593Smuzhiyun 		break;
449*4882a593Smuzhiyun 	default:
450*4882a593Smuzhiyun 		break;
451*4882a593Smuzhiyun 	}
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_kick_queue);
454*4882a593Smuzhiyun 
rt2x00usb_flush_entry(struct queue_entry * entry,void * data)455*4882a593Smuzhiyun static bool rt2x00usb_flush_entry(struct queue_entry *entry, void *data)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
458*4882a593Smuzhiyun 	struct queue_entry_priv_usb *entry_priv = entry->priv_data;
459*4882a593Smuzhiyun 	struct queue_entry_priv_usb_bcn *bcn_priv = entry->priv_data;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
462*4882a593Smuzhiyun 		return false;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	usb_kill_urb(entry_priv->urb);
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	/*
467*4882a593Smuzhiyun 	 * Kill guardian urb (if required by driver).
468*4882a593Smuzhiyun 	 */
469*4882a593Smuzhiyun 	if ((entry->queue->qid == QID_BEACON) &&
470*4882a593Smuzhiyun 	    (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD)))
471*4882a593Smuzhiyun 		usb_kill_urb(bcn_priv->guardian_urb);
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	return false;
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun 
rt2x00usb_flush_queue(struct data_queue * queue,bool drop)476*4882a593Smuzhiyun void rt2x00usb_flush_queue(struct data_queue *queue, bool drop)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	struct work_struct *completion;
479*4882a593Smuzhiyun 	unsigned int i;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	if (drop)
482*4882a593Smuzhiyun 		rt2x00queue_for_each_entry(queue, Q_INDEX_DONE, Q_INDEX, NULL,
483*4882a593Smuzhiyun 					   rt2x00usb_flush_entry);
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	/*
486*4882a593Smuzhiyun 	 * Obtain the queue completion handler
487*4882a593Smuzhiyun 	 */
488*4882a593Smuzhiyun 	switch (queue->qid) {
489*4882a593Smuzhiyun 	case QID_AC_VO:
490*4882a593Smuzhiyun 	case QID_AC_VI:
491*4882a593Smuzhiyun 	case QID_AC_BE:
492*4882a593Smuzhiyun 	case QID_AC_BK:
493*4882a593Smuzhiyun 		completion = &queue->rt2x00dev->txdone_work;
494*4882a593Smuzhiyun 		break;
495*4882a593Smuzhiyun 	case QID_RX:
496*4882a593Smuzhiyun 		completion = &queue->rt2x00dev->rxdone_work;
497*4882a593Smuzhiyun 		break;
498*4882a593Smuzhiyun 	default:
499*4882a593Smuzhiyun 		return;
500*4882a593Smuzhiyun 	}
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	for (i = 0; i < 10; i++) {
503*4882a593Smuzhiyun 		/*
504*4882a593Smuzhiyun 		 * Check if the driver is already done, otherwise we
505*4882a593Smuzhiyun 		 * have to sleep a little while to give the driver/hw
506*4882a593Smuzhiyun 		 * the oppurtunity to complete interrupt process itself.
507*4882a593Smuzhiyun 		 */
508*4882a593Smuzhiyun 		if (rt2x00queue_empty(queue))
509*4882a593Smuzhiyun 			break;
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 		/*
512*4882a593Smuzhiyun 		 * Schedule the completion handler manually, when this
513*4882a593Smuzhiyun 		 * worker function runs, it should cleanup the queue.
514*4882a593Smuzhiyun 		 */
515*4882a593Smuzhiyun 		queue_work(queue->rt2x00dev->workqueue, completion);
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 		/*
518*4882a593Smuzhiyun 		 * Wait for a little while to give the driver
519*4882a593Smuzhiyun 		 * the oppurtunity to recover itself.
520*4882a593Smuzhiyun 		 */
521*4882a593Smuzhiyun 		msleep(50);
522*4882a593Smuzhiyun 	}
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_flush_queue);
525*4882a593Smuzhiyun 
rt2x00usb_watchdog_tx_dma(struct data_queue * queue)526*4882a593Smuzhiyun static void rt2x00usb_watchdog_tx_dma(struct data_queue *queue)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun 	rt2x00_warn(queue->rt2x00dev, "TX queue %d DMA timed out, invoke forced reset\n",
529*4882a593Smuzhiyun 		    queue->qid);
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 	rt2x00queue_stop_queue(queue);
532*4882a593Smuzhiyun 	rt2x00queue_flush_queue(queue, true);
533*4882a593Smuzhiyun 	rt2x00queue_start_queue(queue);
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun 
rt2x00usb_dma_timeout(struct data_queue * queue)536*4882a593Smuzhiyun static int rt2x00usb_dma_timeout(struct data_queue *queue)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun 	struct queue_entry *entry;
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	entry = rt2x00queue_get_entry(queue, Q_INDEX_DMA_DONE);
541*4882a593Smuzhiyun 	return rt2x00queue_dma_timeout(entry);
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun 
rt2x00usb_watchdog(struct rt2x00_dev * rt2x00dev)544*4882a593Smuzhiyun void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	struct data_queue *queue;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	tx_queue_for_each(rt2x00dev, queue) {
549*4882a593Smuzhiyun 		if (!rt2x00queue_empty(queue)) {
550*4882a593Smuzhiyun 			if (rt2x00usb_dma_timeout(queue))
551*4882a593Smuzhiyun 				rt2x00usb_watchdog_tx_dma(queue);
552*4882a593Smuzhiyun 		}
553*4882a593Smuzhiyun 	}
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_watchdog);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun /*
558*4882a593Smuzhiyun  * Radio handlers
559*4882a593Smuzhiyun  */
rt2x00usb_disable_radio(struct rt2x00_dev * rt2x00dev)560*4882a593Smuzhiyun void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
563*4882a593Smuzhiyun 				    REGISTER_TIMEOUT);
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun /*
568*4882a593Smuzhiyun  * Device initialization handlers.
569*4882a593Smuzhiyun  */
rt2x00usb_clear_entry(struct queue_entry * entry)570*4882a593Smuzhiyun void rt2x00usb_clear_entry(struct queue_entry *entry)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun 	entry->flags = 0;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	if (entry->queue->qid == QID_RX)
575*4882a593Smuzhiyun 		rt2x00usb_kick_rx_entry(entry, NULL);
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_clear_entry);
578*4882a593Smuzhiyun 
rt2x00usb_assign_endpoint(struct data_queue * queue,struct usb_endpoint_descriptor * ep_desc)579*4882a593Smuzhiyun static void rt2x00usb_assign_endpoint(struct data_queue *queue,
580*4882a593Smuzhiyun 				      struct usb_endpoint_descriptor *ep_desc)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun 	struct usb_device *usb_dev = to_usb_device_intf(queue->rt2x00dev->dev);
583*4882a593Smuzhiyun 	int pipe;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	queue->usb_endpoint = usb_endpoint_num(ep_desc);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	if (queue->qid == QID_RX) {
588*4882a593Smuzhiyun 		pipe = usb_rcvbulkpipe(usb_dev, queue->usb_endpoint);
589*4882a593Smuzhiyun 		queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 0);
590*4882a593Smuzhiyun 	} else {
591*4882a593Smuzhiyun 		pipe = usb_sndbulkpipe(usb_dev, queue->usb_endpoint);
592*4882a593Smuzhiyun 		queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 1);
593*4882a593Smuzhiyun 	}
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	if (!queue->usb_maxpacket)
596*4882a593Smuzhiyun 		queue->usb_maxpacket = 1;
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun 
rt2x00usb_find_endpoints(struct rt2x00_dev * rt2x00dev)599*4882a593Smuzhiyun static int rt2x00usb_find_endpoints(struct rt2x00_dev *rt2x00dev)
600*4882a593Smuzhiyun {
601*4882a593Smuzhiyun 	struct usb_interface *intf = to_usb_interface(rt2x00dev->dev);
602*4882a593Smuzhiyun 	struct usb_host_interface *intf_desc = intf->cur_altsetting;
603*4882a593Smuzhiyun 	struct usb_endpoint_descriptor *ep_desc;
604*4882a593Smuzhiyun 	struct data_queue *queue = rt2x00dev->tx;
605*4882a593Smuzhiyun 	struct usb_endpoint_descriptor *tx_ep_desc = NULL;
606*4882a593Smuzhiyun 	unsigned int i;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	/*
609*4882a593Smuzhiyun 	 * Walk through all available endpoints to search for "bulk in"
610*4882a593Smuzhiyun 	 * and "bulk out" endpoints. When we find such endpoints collect
611*4882a593Smuzhiyun 	 * the information we need from the descriptor and assign it
612*4882a593Smuzhiyun 	 * to the queue.
613*4882a593Smuzhiyun 	 */
614*4882a593Smuzhiyun 	for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
615*4882a593Smuzhiyun 		ep_desc = &intf_desc->endpoint[i].desc;
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 		if (usb_endpoint_is_bulk_in(ep_desc)) {
618*4882a593Smuzhiyun 			rt2x00usb_assign_endpoint(rt2x00dev->rx, ep_desc);
619*4882a593Smuzhiyun 		} else if (usb_endpoint_is_bulk_out(ep_desc) &&
620*4882a593Smuzhiyun 			   (queue != queue_end(rt2x00dev))) {
621*4882a593Smuzhiyun 			rt2x00usb_assign_endpoint(queue, ep_desc);
622*4882a593Smuzhiyun 			queue = queue_next(queue);
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 			tx_ep_desc = ep_desc;
625*4882a593Smuzhiyun 		}
626*4882a593Smuzhiyun 	}
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	/*
629*4882a593Smuzhiyun 	 * At least 1 endpoint for RX and 1 endpoint for TX must be available.
630*4882a593Smuzhiyun 	 */
631*4882a593Smuzhiyun 	if (!rt2x00dev->rx->usb_endpoint || !rt2x00dev->tx->usb_endpoint) {
632*4882a593Smuzhiyun 		rt2x00_err(rt2x00dev, "Bulk-in/Bulk-out endpoints not found\n");
633*4882a593Smuzhiyun 		return -EPIPE;
634*4882a593Smuzhiyun 	}
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	/*
637*4882a593Smuzhiyun 	 * It might be possible not all queues have a dedicated endpoint.
638*4882a593Smuzhiyun 	 * Loop through all TX queues and copy the endpoint information
639*4882a593Smuzhiyun 	 * which we have gathered from already assigned endpoints.
640*4882a593Smuzhiyun 	 */
641*4882a593Smuzhiyun 	txall_queue_for_each(rt2x00dev, queue) {
642*4882a593Smuzhiyun 		if (!queue->usb_endpoint)
643*4882a593Smuzhiyun 			rt2x00usb_assign_endpoint(queue, tx_ep_desc);
644*4882a593Smuzhiyun 	}
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	return 0;
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun 
rt2x00usb_alloc_entries(struct data_queue * queue)649*4882a593Smuzhiyun static int rt2x00usb_alloc_entries(struct data_queue *queue)
650*4882a593Smuzhiyun {
651*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
652*4882a593Smuzhiyun 	struct queue_entry_priv_usb *entry_priv;
653*4882a593Smuzhiyun 	struct queue_entry_priv_usb_bcn *bcn_priv;
654*4882a593Smuzhiyun 	unsigned int i;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	for (i = 0; i < queue->limit; i++) {
657*4882a593Smuzhiyun 		entry_priv = queue->entries[i].priv_data;
658*4882a593Smuzhiyun 		entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
659*4882a593Smuzhiyun 		if (!entry_priv->urb)
660*4882a593Smuzhiyun 			return -ENOMEM;
661*4882a593Smuzhiyun 	}
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	/*
664*4882a593Smuzhiyun 	 * If this is not the beacon queue or
665*4882a593Smuzhiyun 	 * no guardian byte was required for the beacon,
666*4882a593Smuzhiyun 	 * then we are done.
667*4882a593Smuzhiyun 	 */
668*4882a593Smuzhiyun 	if (queue->qid != QID_BEACON ||
669*4882a593Smuzhiyun 	    !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))
670*4882a593Smuzhiyun 		return 0;
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	for (i = 0; i < queue->limit; i++) {
673*4882a593Smuzhiyun 		bcn_priv = queue->entries[i].priv_data;
674*4882a593Smuzhiyun 		bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
675*4882a593Smuzhiyun 		if (!bcn_priv->guardian_urb)
676*4882a593Smuzhiyun 			return -ENOMEM;
677*4882a593Smuzhiyun 	}
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	return 0;
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun 
rt2x00usb_free_entries(struct data_queue * queue)682*4882a593Smuzhiyun static void rt2x00usb_free_entries(struct data_queue *queue)
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
685*4882a593Smuzhiyun 	struct queue_entry_priv_usb *entry_priv;
686*4882a593Smuzhiyun 	struct queue_entry_priv_usb_bcn *bcn_priv;
687*4882a593Smuzhiyun 	unsigned int i;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	if (!queue->entries)
690*4882a593Smuzhiyun 		return;
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun 	for (i = 0; i < queue->limit; i++) {
693*4882a593Smuzhiyun 		entry_priv = queue->entries[i].priv_data;
694*4882a593Smuzhiyun 		usb_kill_urb(entry_priv->urb);
695*4882a593Smuzhiyun 		usb_free_urb(entry_priv->urb);
696*4882a593Smuzhiyun 	}
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	/*
699*4882a593Smuzhiyun 	 * If this is not the beacon queue or
700*4882a593Smuzhiyun 	 * no guardian byte was required for the beacon,
701*4882a593Smuzhiyun 	 * then we are done.
702*4882a593Smuzhiyun 	 */
703*4882a593Smuzhiyun 	if (queue->qid != QID_BEACON ||
704*4882a593Smuzhiyun 	    !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))
705*4882a593Smuzhiyun 		return;
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	for (i = 0; i < queue->limit; i++) {
708*4882a593Smuzhiyun 		bcn_priv = queue->entries[i].priv_data;
709*4882a593Smuzhiyun 		usb_kill_urb(bcn_priv->guardian_urb);
710*4882a593Smuzhiyun 		usb_free_urb(bcn_priv->guardian_urb);
711*4882a593Smuzhiyun 	}
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun 
rt2x00usb_initialize(struct rt2x00_dev * rt2x00dev)714*4882a593Smuzhiyun int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun 	struct data_queue *queue;
717*4882a593Smuzhiyun 	int status;
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	/*
720*4882a593Smuzhiyun 	 * Find endpoints for each queue
721*4882a593Smuzhiyun 	 */
722*4882a593Smuzhiyun 	status = rt2x00usb_find_endpoints(rt2x00dev);
723*4882a593Smuzhiyun 	if (status)
724*4882a593Smuzhiyun 		goto exit;
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	/*
727*4882a593Smuzhiyun 	 * Allocate DMA
728*4882a593Smuzhiyun 	 */
729*4882a593Smuzhiyun 	queue_for_each(rt2x00dev, queue) {
730*4882a593Smuzhiyun 		status = rt2x00usb_alloc_entries(queue);
731*4882a593Smuzhiyun 		if (status)
732*4882a593Smuzhiyun 			goto exit;
733*4882a593Smuzhiyun 	}
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	return 0;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun exit:
738*4882a593Smuzhiyun 	rt2x00usb_uninitialize(rt2x00dev);
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	return status;
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
743*4882a593Smuzhiyun 
rt2x00usb_uninitialize(struct rt2x00_dev * rt2x00dev)744*4882a593Smuzhiyun void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
745*4882a593Smuzhiyun {
746*4882a593Smuzhiyun 	struct data_queue *queue;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	usb_kill_anchored_urbs(rt2x00dev->anchor);
749*4882a593Smuzhiyun 	hrtimer_cancel(&rt2x00dev->txstatus_timer);
750*4882a593Smuzhiyun 	cancel_work_sync(&rt2x00dev->rxdone_work);
751*4882a593Smuzhiyun 	cancel_work_sync(&rt2x00dev->txdone_work);
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	queue_for_each(rt2x00dev, queue)
754*4882a593Smuzhiyun 		rt2x00usb_free_entries(queue);
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun /*
759*4882a593Smuzhiyun  * USB driver handlers.
760*4882a593Smuzhiyun  */
rt2x00usb_free_reg(struct rt2x00_dev * rt2x00dev)761*4882a593Smuzhiyun static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
762*4882a593Smuzhiyun {
763*4882a593Smuzhiyun 	kfree(rt2x00dev->rf);
764*4882a593Smuzhiyun 	rt2x00dev->rf = NULL;
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 	kfree(rt2x00dev->eeprom);
767*4882a593Smuzhiyun 	rt2x00dev->eeprom = NULL;
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	kfree(rt2x00dev->csr.cache);
770*4882a593Smuzhiyun 	rt2x00dev->csr.cache = NULL;
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun 
rt2x00usb_alloc_reg(struct rt2x00_dev * rt2x00dev)773*4882a593Smuzhiyun static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
774*4882a593Smuzhiyun {
775*4882a593Smuzhiyun 	rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
776*4882a593Smuzhiyun 	if (!rt2x00dev->csr.cache)
777*4882a593Smuzhiyun 		goto exit;
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
780*4882a593Smuzhiyun 	if (!rt2x00dev->eeprom)
781*4882a593Smuzhiyun 		goto exit;
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
784*4882a593Smuzhiyun 	if (!rt2x00dev->rf)
785*4882a593Smuzhiyun 		goto exit;
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	return 0;
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun exit:
790*4882a593Smuzhiyun 	rt2x00_probe_err("Failed to allocate registers\n");
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	rt2x00usb_free_reg(rt2x00dev);
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	return -ENOMEM;
795*4882a593Smuzhiyun }
796*4882a593Smuzhiyun 
rt2x00usb_probe(struct usb_interface * usb_intf,const struct rt2x00_ops * ops)797*4882a593Smuzhiyun int rt2x00usb_probe(struct usb_interface *usb_intf,
798*4882a593Smuzhiyun 		    const struct rt2x00_ops *ops)
799*4882a593Smuzhiyun {
800*4882a593Smuzhiyun 	struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
801*4882a593Smuzhiyun 	struct ieee80211_hw *hw;
802*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev;
803*4882a593Smuzhiyun 	int retval;
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	usb_dev = usb_get_dev(usb_dev);
806*4882a593Smuzhiyun 	usb_reset_device(usb_dev);
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
809*4882a593Smuzhiyun 	if (!hw) {
810*4882a593Smuzhiyun 		rt2x00_probe_err("Failed to allocate hardware\n");
811*4882a593Smuzhiyun 		retval = -ENOMEM;
812*4882a593Smuzhiyun 		goto exit_put_device;
813*4882a593Smuzhiyun 	}
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	usb_set_intfdata(usb_intf, hw);
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	rt2x00dev = hw->priv;
818*4882a593Smuzhiyun 	rt2x00dev->dev = &usb_intf->dev;
819*4882a593Smuzhiyun 	rt2x00dev->ops = ops;
820*4882a593Smuzhiyun 	rt2x00dev->hw = hw;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_USB);
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	INIT_WORK(&rt2x00dev->rxdone_work, rt2x00usb_work_rxdone);
825*4882a593Smuzhiyun 	INIT_WORK(&rt2x00dev->txdone_work, rt2x00usb_work_txdone);
826*4882a593Smuzhiyun 	hrtimer_init(&rt2x00dev->txstatus_timer, CLOCK_MONOTONIC,
827*4882a593Smuzhiyun 		     HRTIMER_MODE_REL);
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	retval = rt2x00usb_alloc_reg(rt2x00dev);
830*4882a593Smuzhiyun 	if (retval)
831*4882a593Smuzhiyun 		goto exit_free_device;
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	rt2x00dev->anchor = devm_kmalloc(&usb_dev->dev,
834*4882a593Smuzhiyun 					sizeof(struct usb_anchor),
835*4882a593Smuzhiyun 					GFP_KERNEL);
836*4882a593Smuzhiyun 	if (!rt2x00dev->anchor) {
837*4882a593Smuzhiyun 		retval = -ENOMEM;
838*4882a593Smuzhiyun 		goto exit_free_reg;
839*4882a593Smuzhiyun 	}
840*4882a593Smuzhiyun 	init_usb_anchor(rt2x00dev->anchor);
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	retval = rt2x00lib_probe_dev(rt2x00dev);
843*4882a593Smuzhiyun 	if (retval)
844*4882a593Smuzhiyun 		goto exit_free_anchor;
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	return 0;
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun exit_free_anchor:
849*4882a593Smuzhiyun 	usb_kill_anchored_urbs(rt2x00dev->anchor);
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun exit_free_reg:
852*4882a593Smuzhiyun 	rt2x00usb_free_reg(rt2x00dev);
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun exit_free_device:
855*4882a593Smuzhiyun 	ieee80211_free_hw(hw);
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun exit_put_device:
858*4882a593Smuzhiyun 	usb_put_dev(usb_dev);
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	usb_set_intfdata(usb_intf, NULL);
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	return retval;
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_probe);
865*4882a593Smuzhiyun 
rt2x00usb_disconnect(struct usb_interface * usb_intf)866*4882a593Smuzhiyun void rt2x00usb_disconnect(struct usb_interface *usb_intf)
867*4882a593Smuzhiyun {
868*4882a593Smuzhiyun 	struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
869*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev = hw->priv;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	/*
872*4882a593Smuzhiyun 	 * Free all allocated data.
873*4882a593Smuzhiyun 	 */
874*4882a593Smuzhiyun 	rt2x00lib_remove_dev(rt2x00dev);
875*4882a593Smuzhiyun 	rt2x00usb_free_reg(rt2x00dev);
876*4882a593Smuzhiyun 	ieee80211_free_hw(hw);
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	/*
879*4882a593Smuzhiyun 	 * Free the USB device data.
880*4882a593Smuzhiyun 	 */
881*4882a593Smuzhiyun 	usb_set_intfdata(usb_intf, NULL);
882*4882a593Smuzhiyun 	usb_put_dev(interface_to_usbdev(usb_intf));
883*4882a593Smuzhiyun }
884*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun #ifdef CONFIG_PM
rt2x00usb_suspend(struct usb_interface * usb_intf,pm_message_t state)887*4882a593Smuzhiyun int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
888*4882a593Smuzhiyun {
889*4882a593Smuzhiyun 	struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
890*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev = hw->priv;
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	return rt2x00lib_suspend(rt2x00dev);
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
895*4882a593Smuzhiyun 
rt2x00usb_resume(struct usb_interface * usb_intf)896*4882a593Smuzhiyun int rt2x00usb_resume(struct usb_interface *usb_intf)
897*4882a593Smuzhiyun {
898*4882a593Smuzhiyun 	struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
899*4882a593Smuzhiyun 	struct rt2x00_dev *rt2x00dev = hw->priv;
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	return rt2x00lib_resume(rt2x00dev);
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rt2x00usb_resume);
904*4882a593Smuzhiyun #endif /* CONFIG_PM */
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun /*
907*4882a593Smuzhiyun  * rt2x00usb module information.
908*4882a593Smuzhiyun  */
909*4882a593Smuzhiyun MODULE_AUTHOR(DRV_PROJECT);
910*4882a593Smuzhiyun MODULE_VERSION(DRV_VERSION);
911*4882a593Smuzhiyun MODULE_DESCRIPTION("rt2x00 usb library");
912*4882a593Smuzhiyun MODULE_LICENSE("GPL");
913