blob: 10572452cc21b76cc8fe4457298ea06bc3c875be [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn7e613e12010-08-06 20:45:38 +02002 Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
3 Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
Ivo van Doorn95ea3622007-09-25 17:57:13 -07004 <http://rt2x00.serialmonkey.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
Jeff Kirshera05b8c52013-12-06 03:32:11 -080017 along with this program; if not, see <http://www.gnu.org/licenses/>.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070018 */
19
20/*
21 Module: rt2x00usb
22 Abstract: rt2x00 generic usb device routines.
23 */
24
Ivo van Doorn95ea3622007-09-25 17:57:13 -070025#include <linux/kernel.h>
26#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Ivo van Doorn95ea3622007-09-25 17:57:13 -070028#include <linux/usb.h>
Adam Baker3d823462007-10-27 13:43:29 +020029#include <linux/bug.h>
Ivo van Doorn95ea3622007-09-25 17:57:13 -070030
31#include "rt2x00.h"
32#include "rt2x00usb.h"
33
34/*
35 * Interfacing with the HW.
36 */
Adam Baker0e14f6d2007-10-27 13:41:25 +020037int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn95ea3622007-09-25 17:57:13 -070038 const u8 request, const u8 requesttype,
39 const u16 offset, const u16 value,
40 void *buffer, const u16 buffer_length,
Ivo van Doorne9136552007-09-25 20:54:20 +020041 const int timeout)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070042{
Ivo van Doornc1d35df2008-06-16 19:57:11 +020043 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070044 int status;
45 unsigned int i;
46 unsigned int pipe =
47 (requesttype == USB_VENDOR_REQUEST_IN) ?
48 usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
49
Sean Cross66f84d62009-11-05 20:22:03 +010050 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
51 return -ENODEV;
Adam Baker3d823462007-10-27 13:43:29 +020052
Ivo van Doorn95ea3622007-09-25 17:57:13 -070053 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
54 status = usb_control_msg(usb_dev, pipe, request, requesttype,
55 value, offset, buffer, buffer_length,
56 timeout);
57 if (status >= 0)
58 return 0;
59
60 /*
Ivo van Doorne9136552007-09-25 20:54:20 +020061 * Check for errors
Ivo van Doorn95ea3622007-09-25 17:57:13 -070062 * -ENODEV: Device has disappeared, no point continuing.
Ivo van Doorne9136552007-09-25 20:54:20 +020063 * All other errors: Try again.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070064 */
Sean Cross66f84d62009-11-05 20:22:03 +010065 else if (status == -ENODEV) {
66 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070067 break;
Sean Cross66f84d62009-11-05 20:22:03 +010068 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -070069 }
70
Joe Perchesec9c4982013-04-19 08:33:40 -070071 rt2x00_err(rt2x00dev,
72 "Vendor Request 0x%02x failed for offset 0x%04x with error %d\n",
73 request, offset, status);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070074
75 return status;
76}
77EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
78
Adam Baker3d823462007-10-27 13:43:29 +020079int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
80 const u8 request, const u8 requesttype,
81 const u16 offset, void *buffer,
82 const u16 buffer_length, const int timeout)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070083{
84 int status;
85
Ivo van Doorn8ff48a82008-11-09 23:40:46 +010086 BUG_ON(!mutex_is_locked(&rt2x00dev->csr_mutex));
Adam Baker3d823462007-10-27 13:43:29 +020087
Ivo van Doorn95ea3622007-09-25 17:57:13 -070088 /*
89 * Check for Cache availability.
90 */
Ivo van Doorn21795092008-02-10 22:49:13 +010091 if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
Joe Perchesec9c4982013-04-19 08:33:40 -070092 rt2x00_err(rt2x00dev, "CSR cache not available\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -070093 return -ENOMEM;
94 }
95
96 if (requesttype == USB_VENDOR_REQUEST_OUT)
Ivo van Doorn21795092008-02-10 22:49:13 +010097 memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070098
99 status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
Ivo van Doorn21795092008-02-10 22:49:13 +0100100 offset, 0, rt2x00dev->csr.cache,
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700101 buffer_length, timeout);
102
103 if (!status && requesttype == USB_VENDOR_REQUEST_IN)
Ivo van Doorn21795092008-02-10 22:49:13 +0100104 memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700105
106 return status;
107}
Adam Baker3d823462007-10-27 13:43:29 +0200108EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
109
110int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
111 const u8 request, const u8 requesttype,
112 const u16 offset, void *buffer,
113 const u16 buffer_length, const int timeout)
114{
Iwo Merglered0dbee2008-07-19 16:16:54 +0200115 int status = 0;
116 unsigned char *tb;
117 u16 off, len, bsize;
118
Ivo van Doorn8ff48a82008-11-09 23:40:46 +0100119 mutex_lock(&rt2x00dev->csr_mutex);
Iwo Merglered0dbee2008-07-19 16:16:54 +0200120
Ivo van Doorn82f97b82008-08-02 01:31:09 -0700121 tb = (char *)buffer;
Iwo Merglered0dbee2008-07-19 16:16:54 +0200122 off = offset;
123 len = buffer_length;
124 while (len && !status) {
125 bsize = min_t(u16, CSR_CACHE_SIZE, len);
126 status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
127 requesttype, off, tb,
128 bsize, timeout);
129
130 tb += bsize;
131 len -= bsize;
132 off += bsize;
133 }
134
Ivo van Doorn8ff48a82008-11-09 23:40:46 +0100135 mutex_unlock(&rt2x00dev->csr_mutex);
Iwo Merglered0dbee2008-07-19 16:16:54 +0200136
137 return status;
138}
Gertjan van Wingerde96b61ba2010-06-03 10:51:51 +0200139EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
Iwo Merglered0dbee2008-07-19 16:16:54 +0200140
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100141int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
142 const unsigned int offset,
Bartlomiej Zolnierkiewiczf255b922009-11-04 18:35:18 +0100143 const struct rt2x00_field32 field,
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100144 u32 *reg)
145{
146 unsigned int i;
147
Sean Cross66f84d62009-11-05 20:22:03 +0100148 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
149 return -ENODEV;
150
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100151 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
152 rt2x00usb_register_read_lock(rt2x00dev, offset, reg);
153 if (!rt2x00_get_field32(*reg, field))
154 return 1;
155 udelay(REGISTER_BUSY_DELAY);
156 }
157
Joe Perchesec9c4982013-04-19 08:33:40 -0700158 rt2x00_err(rt2x00dev, "Indirect register access failed: offset=0x%.08x, value=0x%.08x\n",
159 offset, *reg);
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100160 *reg = ~0;
161
162 return 0;
163}
164EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read);
165
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200166
167struct rt2x00_async_read_data {
168 __le32 reg;
169 struct usb_ctrlrequest cr;
170 struct rt2x00_dev *rt2x00dev;
Ivo van Doorna073fde2011-04-30 17:14:23 +0200171 bool (*callback)(struct rt2x00_dev *, int, u32);
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200172};
173
174static void rt2x00usb_register_read_async_cb(struct urb *urb)
175{
176 struct rt2x00_async_read_data *rd = urb->context;
Ivo van Doorna073fde2011-04-30 17:14:23 +0200177 if (rd->callback(rd->rt2x00dev, urb->status, le32_to_cpu(rd->reg))) {
178 if (usb_submit_urb(urb, GFP_ATOMIC) < 0)
179 kfree(rd);
180 } else
181 kfree(rd);
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200182}
183
184void rt2x00usb_register_read_async(struct rt2x00_dev *rt2x00dev,
185 const unsigned int offset,
Ivo van Doorna073fde2011-04-30 17:14:23 +0200186 bool (*callback)(struct rt2x00_dev*, int, u32))
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200187{
188 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
189 struct urb *urb;
190 struct rt2x00_async_read_data *rd;
191
192 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
193 if (!rd)
194 return;
195
196 urb = usb_alloc_urb(0, GFP_ATOMIC);
197 if (!urb) {
198 kfree(rd);
199 return;
200 }
201
202 rd->rt2x00dev = rt2x00dev;
203 rd->callback = callback;
204 rd->cr.bRequestType = USB_VENDOR_REQUEST_IN;
205 rd->cr.bRequest = USB_MULTI_READ;
206 rd->cr.wValue = 0;
207 rd->cr.wIndex = cpu_to_le16(offset);
208 rd->cr.wLength = cpu_to_le16(sizeof(u32));
209
210 usb_fill_control_urb(urb, usb_dev, usb_rcvctrlpipe(usb_dev, 0),
211 (unsigned char *)(&rd->cr), &rd->reg, sizeof(rd->reg),
212 rt2x00usb_register_read_async_cb, rd);
213 if (usb_submit_urb(urb, GFP_ATOMIC) < 0)
214 kfree(rd);
215 usb_free_urb(urb);
216}
217EXPORT_SYMBOL_GPL(rt2x00usb_register_read_async);
218
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700219/*
220 * TX data handlers.
221 */
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200222static void rt2x00usb_work_txdone_entry(struct queue_entry *entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700223{
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700224 /*
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200225 * If the transfer to hardware succeeded, it does not mean the
Ivo van Doornfb55f4d12008-05-10 13:42:06 +0200226 * frame was send out correctly. It only means the frame
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300227 * was successfully pushed to the hardware, we have no
Ivo van Doornfb55f4d12008-05-10 13:42:06 +0200228 * way to determine the transmission status right now.
229 * (Only indirectly by looking at the failed TX counters
230 * in the register).
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700231 */
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200232 if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
Ivo van Doorn3392bec2010-08-06 20:46:53 +0200233 rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200234 else
Ivo van Doorn3392bec2010-08-06 20:46:53 +0200235 rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700236}
237
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200238static void rt2x00usb_work_txdone(struct work_struct *work)
239{
240 struct rt2x00_dev *rt2x00dev =
241 container_of(work, struct rt2x00_dev, txdone_work);
242 struct data_queue *queue;
243 struct queue_entry *entry;
244
245 tx_queue_for_each(rt2x00dev, queue) {
246 while (!rt2x00queue_empty(queue)) {
247 entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
248
Ivo van Doorndba5dc12010-12-13 12:36:18 +0100249 if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
250 !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200251 break;
252
253 rt2x00usb_work_txdone_entry(entry);
254 }
255 }
256}
257
258static void rt2x00usb_interrupt_txdone(struct urb *urb)
259{
260 struct queue_entry *entry = (struct queue_entry *)urb->context;
261 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
262
Stanislaw Gruszkadf71c9c2011-08-10 15:32:23 +0200263 if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200264 return;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200265 /*
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200266 * Check if the frame was correctly uploaded
267 */
268 if (urb->status)
Ivo van Doorna1d1eab2010-10-11 15:38:45 +0200269 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
Stanislaw Gruszkadf71c9c2011-08-10 15:32:23 +0200270 /*
271 * Report the frame as DMA done
272 */
273 rt2x00lib_dmadone(entry);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200274
Stanislaw Gruszkadf71c9c2011-08-10 15:32:23 +0200275 if (rt2x00dev->ops->lib->tx_dma_done)
276 rt2x00dev->ops->lib->tx_dma_done(entry);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200277 /*
278 * Schedule the delayed work for reading the TX status
279 * from the device.
280 */
Johannes Stezenbachf0187a12011-04-18 15:30:36 +0200281 if (!test_bit(REQUIRE_TXSTATUS_FIFO, &rt2x00dev->cap_flags) ||
282 !kfifo_is_empty(&rt2x00dev->txstatus_fifo))
283 queue_work(rt2x00dev->workqueue, &rt2x00dev->txdone_work);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200284}
285
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100286static bool rt2x00usb_kick_tx_entry(struct queue_entry *entry, void *data)
Ivo van Doornf019d512008-06-06 22:47:39 +0200287{
Gertjan van Wingerdefe725692010-06-29 21:40:34 +0200288 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
289 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
Ivo van Doornf019d512008-06-06 22:47:39 +0200290 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
Gertjan van Wingerdefe725692010-06-29 21:40:34 +0200291 u32 length;
Johannes Stezenbachd7bb5f82010-12-13 12:32:49 +0100292 int status;
Ivo van Doornf019d512008-06-06 22:47:39 +0200293
Ivo van Doorndba5dc12010-12-13 12:36:18 +0100294 if (!test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags) ||
295 test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
Ivo van Doorn4268d8e2011-05-04 21:42:05 +0200296 return false;
Gertjan van Wingerdefe725692010-06-29 21:40:34 +0200297
Ivo van Doornee1e7552010-08-23 19:54:02 +0200298 /*
Jakub Kicińskid823a502011-12-28 01:53:22 +0100299 * USB devices require certain padding at the end of each frame
300 * and urb. Those paddings are not included in skbs. Pass entry
301 * to the driver to determine what the overall length should be.
Ivo van Doornee1e7552010-08-23 19:54:02 +0200302 */
303 length = rt2x00dev->ops->lib->get_tx_data_len(entry);
Gertjan van Wingerdefe725692010-06-29 21:40:34 +0200304
Jakub Kicińskid823a502011-12-28 01:53:22 +0100305 status = skb_padto(entry->skb, length);
306 if (unlikely(status)) {
307 /* TODO: report something more appropriate than IO_FAILED. */
Joe Perchesec9c4982013-04-19 08:33:40 -0700308 rt2x00_warn(rt2x00dev, "TX SKB padding error, out of memory\n");
Jakub Kicińskid823a502011-12-28 01:53:22 +0100309 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
310 rt2x00lib_dmadone(entry);
311
312 return false;
313 }
314
Ivo van Doornee1e7552010-08-23 19:54:02 +0200315 usb_fill_bulk_urb(entry_priv->urb, usb_dev,
316 usb_sndbulkpipe(usb_dev, entry->queue->usb_endpoint),
317 entry->skb->data, length,
318 rt2x00usb_interrupt_txdone, entry);
319
Johannes Stezenbachd7bb5f82010-12-13 12:32:49 +0100320 status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
321 if (status) {
322 if (status == -ENODEV)
323 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
Ivo van Doorna13c8f32010-10-11 15:39:48 +0200324 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
325 rt2x00lib_dmadone(entry);
326 }
Helmut Schaa10e11562011-04-18 15:27:43 +0200327
328 return false;
Ivo van Doornf019d512008-06-06 22:47:39 +0200329}
330
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700331/*
332 * RX data handlers.
333 */
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200334static void rt2x00usb_work_rxdone(struct work_struct *work)
335{
336 struct rt2x00_dev *rt2x00dev =
337 container_of(work, struct rt2x00_dev, rxdone_work);
338 struct queue_entry *entry;
339 struct skb_frame_desc *skbdesc;
340 u8 rxd[32];
341
342 while (!rt2x00queue_empty(rt2x00dev->rx)) {
343 entry = rt2x00queue_get_entry(rt2x00dev->rx, Q_INDEX_DONE);
344
Ivo van Doorndba5dc12010-12-13 12:36:18 +0100345 if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
346 !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200347 break;
348
349 /*
350 * Fill in desc fields of the skb descriptor
351 */
352 skbdesc = get_skb_frame_desc(entry->skb);
353 skbdesc->desc = rxd;
354 skbdesc->desc_len = entry->queue->desc_size;
355
356 /*
357 * Send the frame to rt2x00lib for further processing.
358 */
Helmut Schaa88211022012-04-19 13:24:10 +0200359 rt2x00lib_rxdone(entry, GFP_KERNEL);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200360 }
361}
362
Ivo van Doorn181d6902008-02-05 16:42:23 -0500363static void rt2x00usb_interrupt_rxdone(struct urb *urb)
364{
365 struct queue_entry *entry = (struct queue_entry *)urb->context;
366 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500367
Ivo van Doorna1d1eab2010-10-11 15:38:45 +0200368 if (!test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
Ivo van Doorn181d6902008-02-05 16:42:23 -0500369 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700370
371 /*
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200372 * Report the frame as DMA done
373 */
374 rt2x00lib_dmadone(entry);
375
376 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500377 * Check if the received data is simply too small
378 * to be actually valid, or if the urb is signaling
379 * a problem.
Ivo van Doorn08992f72008-01-24 01:56:25 -0800380 */
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200381 if (urb->actual_length < entry->queue->desc_size || urb->status)
Ivo van Doorna1d1eab2010-10-11 15:38:45 +0200382 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500383
384 /*
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200385 * Schedule the delayed work for reading the RX status
386 * from the device.
Ivo van Doorn181d6902008-02-05 16:42:23 -0500387 */
Ivo van Doorn0439f532011-01-30 13:24:05 +0100388 queue_work(rt2x00dev->workqueue, &rt2x00dev->rxdone_work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700389}
390
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100391static bool rt2x00usb_kick_rx_entry(struct queue_entry *entry, void *data)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100392{
393 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
394 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
395 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
396 int status;
397
Ivo van Doorndba5dc12010-12-13 12:36:18 +0100398 if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
399 test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
Ivo van Doorn4268d8e2011-05-04 21:42:05 +0200400 return false;
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100401
Ivo van Doorn64e7d722010-12-13 12:36:00 +0100402 rt2x00lib_dmastart(entry);
403
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100404 usb_fill_bulk_urb(entry_priv->urb, usb_dev,
405 usb_rcvbulkpipe(usb_dev, entry->queue->usb_endpoint),
406 entry->skb->data, entry->skb->len,
407 rt2x00usb_interrupt_rxdone, entry);
408
409 status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
410 if (status) {
411 if (status == -ENODEV)
412 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
413 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
414 rt2x00lib_dmadone(entry);
415 }
Helmut Schaa10e11562011-04-18 15:27:43 +0200416
417 return false;
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100418}
419
420void rt2x00usb_kick_queue(struct data_queue *queue)
421{
422 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100423 case QID_AC_VO:
424 case QID_AC_VI:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100425 case QID_AC_BE:
426 case QID_AC_BK:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100427 if (!rt2x00queue_empty(queue))
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100428 rt2x00queue_for_each_entry(queue,
429 Q_INDEX_DONE,
430 Q_INDEX,
431 NULL,
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100432 rt2x00usb_kick_tx_entry);
433 break;
434 case QID_RX:
435 if (!rt2x00queue_full(queue))
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100436 rt2x00queue_for_each_entry(queue,
437 Q_INDEX,
438 Q_INDEX_DONE,
439 NULL,
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100440 rt2x00usb_kick_rx_entry);
441 break;
442 default:
443 break;
444 }
445}
446EXPORT_SYMBOL_GPL(rt2x00usb_kick_queue);
447
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100448static bool rt2x00usb_flush_entry(struct queue_entry *entry, void *data)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100449{
450 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
451 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
452 struct queue_entry_priv_usb_bcn *bcn_priv = entry->priv_data;
453
454 if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
Ivo van Doorn4268d8e2011-05-04 21:42:05 +0200455 return false;
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100456
457 usb_kill_urb(entry_priv->urb);
458
459 /*
460 * Kill guardian urb (if required by driver).
461 */
462 if ((entry->queue->qid == QID_BEACON) &&
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200463 (test_bit(REQUIRE_BEACON_GUARD, &rt2x00dev->cap_flags)))
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100464 usb_kill_urb(bcn_priv->guardian_urb);
Helmut Schaa10e11562011-04-18 15:27:43 +0200465
466 return false;
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100467}
468
Ivo van Doorn152a5992011-04-18 15:31:02 +0200469void rt2x00usb_flush_queue(struct data_queue *queue, bool drop)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100470{
Ivo van Doorn5be65602010-12-13 12:35:40 +0100471 struct work_struct *completion;
472 unsigned int i;
473
Ivo van Doorn152a5992011-04-18 15:31:02 +0200474 if (drop)
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100475 rt2x00queue_for_each_entry(queue, Q_INDEX_DONE, Q_INDEX, NULL,
Ivo van Doorn152a5992011-04-18 15:31:02 +0200476 rt2x00usb_flush_entry);
Ivo van Doorn5be65602010-12-13 12:35:40 +0100477
478 /*
479 * Obtain the queue completion handler
480 */
481 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100482 case QID_AC_VO:
483 case QID_AC_VI:
Ivo van Doorn5be65602010-12-13 12:35:40 +0100484 case QID_AC_BE:
485 case QID_AC_BK:
Ivo van Doorn5be65602010-12-13 12:35:40 +0100486 completion = &queue->rt2x00dev->txdone_work;
487 break;
488 case QID_RX:
489 completion = &queue->rt2x00dev->rxdone_work;
490 break;
491 default:
492 return;
493 }
494
Ivo van Doorn152a5992011-04-18 15:31:02 +0200495 for (i = 0; i < 10; i++) {
Ivo van Doorn5be65602010-12-13 12:35:40 +0100496 /*
497 * Check if the driver is already done, otherwise we
498 * have to sleep a little while to give the driver/hw
499 * the oppurtunity to complete interrupt process itself.
500 */
501 if (rt2x00queue_empty(queue))
502 break;
503
504 /*
505 * Schedule the completion handler manually, when this
506 * worker function runs, it should cleanup the queue.
507 */
Ivo van Doorn0439f532011-01-30 13:24:05 +0100508 queue_work(queue->rt2x00dev->workqueue, completion);
Ivo van Doorn5be65602010-12-13 12:35:40 +0100509
510 /*
511 * Wait for a little while to give the driver
512 * the oppurtunity to recover itself.
513 */
514 msleep(10);
515 }
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100516}
Ivo van Doorn5be65602010-12-13 12:35:40 +0100517EXPORT_SYMBOL_GPL(rt2x00usb_flush_queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100518
519static void rt2x00usb_watchdog_tx_dma(struct data_queue *queue)
520{
Joe Perchesec9c4982013-04-19 08:33:40 -0700521 rt2x00_warn(queue->rt2x00dev, "TX queue %d DMA timed out, invoke forced forced reset\n",
522 queue->qid);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100523
Stanislaw Gruszkafdbdd252013-10-05 18:15:33 +0200524 rt2x00queue_stop_queue(queue);
Ivo van Doorn5be65602010-12-13 12:35:40 +0100525 rt2x00queue_flush_queue(queue, true);
Stanislaw Gruszkafdbdd252013-10-05 18:15:33 +0200526 rt2x00queue_start_queue(queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100527}
528
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200529static int rt2x00usb_dma_timeout(struct data_queue *queue)
530{
531 struct queue_entry *entry;
532
533 entry = rt2x00queue_get_entry(queue, Q_INDEX_DMA_DONE);
534 return rt2x00queue_dma_timeout(entry);
535}
536
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100537void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev)
538{
539 struct data_queue *queue;
540
541 tx_queue_for_each(rt2x00dev, queue) {
542 if (!rt2x00queue_empty(queue)) {
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200543 if (rt2x00usb_dma_timeout(queue))
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100544 rt2x00usb_watchdog_tx_dma(queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100545 }
546 }
547}
548EXPORT_SYMBOL_GPL(rt2x00usb_watchdog);
549
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700550/*
551 * Radio handlers
552 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700553void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
554{
Ivo van Doornbd394a72008-04-21 19:01:58 +0200555 rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700556 REGISTER_TIMEOUT);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700557}
558EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
559
560/*
561 * Device initialization handlers.
562 */
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100563void rt2x00usb_clear_entry(struct queue_entry *entry)
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100564{
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200565 entry->flags = 0;
566
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100567 if (entry->queue->qid == QID_RX)
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100568 rt2x00usb_kick_rx_entry(entry, NULL);
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100569}
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100570EXPORT_SYMBOL_GPL(rt2x00usb_clear_entry);
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100571
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100572static void rt2x00usb_assign_endpoint(struct data_queue *queue,
573 struct usb_endpoint_descriptor *ep_desc)
574{
575 struct usb_device *usb_dev = to_usb_device_intf(queue->rt2x00dev->dev);
576 int pipe;
577
578 queue->usb_endpoint = usb_endpoint_num(ep_desc);
579
580 if (queue->qid == QID_RX) {
581 pipe = usb_rcvbulkpipe(usb_dev, queue->usb_endpoint);
582 queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 0);
583 } else {
584 pipe = usb_sndbulkpipe(usb_dev, queue->usb_endpoint);
585 queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 1);
586 }
587
588 if (!queue->usb_maxpacket)
589 queue->usb_maxpacket = 1;
590}
591
592static int rt2x00usb_find_endpoints(struct rt2x00_dev *rt2x00dev)
593{
594 struct usb_interface *intf = to_usb_interface(rt2x00dev->dev);
595 struct usb_host_interface *intf_desc = intf->cur_altsetting;
596 struct usb_endpoint_descriptor *ep_desc;
597 struct data_queue *queue = rt2x00dev->tx;
598 struct usb_endpoint_descriptor *tx_ep_desc = NULL;
599 unsigned int i;
600
601 /*
602 * Walk through all available endpoints to search for "bulk in"
603 * and "bulk out" endpoints. When we find such endpoints collect
604 * the information we need from the descriptor and assign it
605 * to the queue.
606 */
607 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
608 ep_desc = &intf_desc->endpoint[i].desc;
609
610 if (usb_endpoint_is_bulk_in(ep_desc)) {
611 rt2x00usb_assign_endpoint(rt2x00dev->rx, ep_desc);
Ivo van Doornd15cfc32008-12-20 11:00:23 +0100612 } else if (usb_endpoint_is_bulk_out(ep_desc) &&
613 (queue != queue_end(rt2x00dev))) {
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100614 rt2x00usb_assign_endpoint(queue, ep_desc);
Ivo van Doornd15cfc32008-12-20 11:00:23 +0100615 queue = queue_next(queue);
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100616
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100617 tx_ep_desc = ep_desc;
618 }
619 }
620
621 /*
622 * At least 1 endpoint for RX and 1 endpoint for TX must be available.
623 */
624 if (!rt2x00dev->rx->usb_endpoint || !rt2x00dev->tx->usb_endpoint) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700625 rt2x00_err(rt2x00dev, "Bulk-in/Bulk-out endpoints not found\n");
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100626 return -EPIPE;
627 }
628
629 /*
630 * It might be possible not all queues have a dedicated endpoint.
631 * Loop through all TX queues and copy the endpoint information
632 * which we have gathered from already assigned endpoints.
633 */
634 txall_queue_for_each(rt2x00dev, queue) {
635 if (!queue->usb_endpoint)
636 rt2x00usb_assign_endpoint(queue, tx_ep_desc);
637 }
638
639 return 0;
640}
641
Ivo van Doornfa695602010-10-11 15:37:25 +0200642static int rt2x00usb_alloc_entries(struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700643{
Ivo van Doornfa695602010-10-11 15:37:25 +0200644 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200645 struct queue_entry_priv_usb *entry_priv;
646 struct queue_entry_priv_usb_bcn *bcn_priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700647 unsigned int i;
648
Ivo van Doorn181d6902008-02-05 16:42:23 -0500649 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200650 entry_priv = queue->entries[i].priv_data;
651 entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
652 if (!entry_priv->urb)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700653 return -ENOMEM;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200654 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500655
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200656 /*
657 * If this is not the beacon queue or
658 * no guardian byte was required for the beacon,
659 * then we are done.
660 */
Ivo van Doornfa695602010-10-11 15:37:25 +0200661 if (queue->qid != QID_BEACON ||
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200662 !test_bit(REQUIRE_BEACON_GUARD, &rt2x00dev->cap_flags))
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200663 return 0;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500664
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200665 for (i = 0; i < queue->limit; i++) {
666 bcn_priv = queue->entries[i].priv_data;
667 bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
668 if (!bcn_priv->guardian_urb)
669 return -ENOMEM;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700670 }
671
672 return 0;
673}
674
Ivo van Doornfa695602010-10-11 15:37:25 +0200675static void rt2x00usb_free_entries(struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700676{
Ivo van Doornfa695602010-10-11 15:37:25 +0200677 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200678 struct queue_entry_priv_usb *entry_priv;
679 struct queue_entry_priv_usb_bcn *bcn_priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700680 unsigned int i;
681
Ivo van Doorn181d6902008-02-05 16:42:23 -0500682 if (!queue->entries)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700683 return;
684
Ivo van Doorn181d6902008-02-05 16:42:23 -0500685 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200686 entry_priv = queue->entries[i].priv_data;
687 usb_kill_urb(entry_priv->urb);
688 usb_free_urb(entry_priv->urb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700689 }
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200690
691 /*
692 * If this is not the beacon queue or
693 * no guardian byte was required for the beacon,
694 * then we are done.
695 */
Ivo van Doornfa695602010-10-11 15:37:25 +0200696 if (queue->qid != QID_BEACON ||
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200697 !test_bit(REQUIRE_BEACON_GUARD, &rt2x00dev->cap_flags))
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200698 return;
699
700 for (i = 0; i < queue->limit; i++) {
701 bcn_priv = queue->entries[i].priv_data;
702 usb_kill_urb(bcn_priv->guardian_urb);
703 usb_free_urb(bcn_priv->guardian_urb);
704 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700705}
706
707int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
708{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500709 struct data_queue *queue;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200710 int status;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700711
712 /*
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100713 * Find endpoints for each queue
714 */
715 status = rt2x00usb_find_endpoints(rt2x00dev);
716 if (status)
717 goto exit;
718
719 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700720 * Allocate DMA
721 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500722 queue_for_each(rt2x00dev, queue) {
Ivo van Doornfa695602010-10-11 15:37:25 +0200723 status = rt2x00usb_alloc_entries(queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700724 if (status)
725 goto exit;
726 }
727
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700728 return 0;
729
730exit:
731 rt2x00usb_uninitialize(rt2x00dev);
732
733 return status;
734}
735EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
736
737void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
738{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500739 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700740
Ivo van Doorn181d6902008-02-05 16:42:23 -0500741 queue_for_each(rt2x00dev, queue)
Ivo van Doornfa695602010-10-11 15:37:25 +0200742 rt2x00usb_free_entries(queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700743}
744EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
745
746/*
747 * USB driver handlers.
748 */
749static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
750{
751 kfree(rt2x00dev->rf);
752 rt2x00dev->rf = NULL;
753
754 kfree(rt2x00dev->eeprom);
755 rt2x00dev->eeprom = NULL;
756
Ivo van Doorn21795092008-02-10 22:49:13 +0100757 kfree(rt2x00dev->csr.cache);
758 rt2x00dev->csr.cache = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700759}
760
761static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
762{
Ivo van Doorn21795092008-02-10 22:49:13 +0100763 rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
764 if (!rt2x00dev->csr.cache)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700765 goto exit;
766
767 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
768 if (!rt2x00dev->eeprom)
769 goto exit;
770
771 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
772 if (!rt2x00dev->rf)
773 goto exit;
774
775 return 0;
776
777exit:
Joe Perchesec9c4982013-04-19 08:33:40 -0700778 rt2x00_probe_err("Failed to allocate registers\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700779
780 rt2x00usb_free_reg(rt2x00dev);
781
782 return -ENOMEM;
783}
784
785int rt2x00usb_probe(struct usb_interface *usb_intf,
Gertjan van Wingerdee01ae272011-04-18 15:32:13 +0200786 const struct rt2x00_ops *ops)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700787{
788 struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700789 struct ieee80211_hw *hw;
790 struct rt2x00_dev *rt2x00dev;
791 int retval;
792
793 usb_dev = usb_get_dev(usb_dev);
Stanislaw Gruszkabf4c02d2011-06-19 19:47:39 +0200794 usb_reset_device(usb_dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700795
796 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
797 if (!hw) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700798 rt2x00_probe_err("Failed to allocate hardware\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700799 retval = -ENOMEM;
800 goto exit_put_device;
801 }
802
803 usb_set_intfdata(usb_intf, hw);
804
805 rt2x00dev = hw->priv;
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200806 rt2x00dev->dev = &usb_intf->dev;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700807 rt2x00dev->ops = ops;
808 rt2x00dev->hw = hw;
809
Gertjan van Wingerde2015d192009-11-08 12:30:14 +0100810 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_USB);
811
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200812 INIT_WORK(&rt2x00dev->rxdone_work, rt2x00usb_work_rxdone);
813 INIT_WORK(&rt2x00dev->txdone_work, rt2x00usb_work_txdone);
Stanislaw Gruszkaf4211112012-03-14 11:16:19 +0100814 hrtimer_init(&rt2x00dev->txstatus_timer, CLOCK_MONOTONIC,
815 HRTIMER_MODE_REL);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200816
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700817 retval = rt2x00usb_alloc_reg(rt2x00dev);
818 if (retval)
819 goto exit_free_device;
820
821 retval = rt2x00lib_probe_dev(rt2x00dev);
822 if (retval)
823 goto exit_free_reg;
824
825 return 0;
826
827exit_free_reg:
828 rt2x00usb_free_reg(rt2x00dev);
829
830exit_free_device:
831 ieee80211_free_hw(hw);
832
833exit_put_device:
834 usb_put_dev(usb_dev);
835
836 usb_set_intfdata(usb_intf, NULL);
837
838 return retval;
839}
840EXPORT_SYMBOL_GPL(rt2x00usb_probe);
841
842void rt2x00usb_disconnect(struct usb_interface *usb_intf)
843{
844 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
845 struct rt2x00_dev *rt2x00dev = hw->priv;
846
847 /*
848 * Free all allocated data.
849 */
850 rt2x00lib_remove_dev(rt2x00dev);
851 rt2x00usb_free_reg(rt2x00dev);
852 ieee80211_free_hw(hw);
853
854 /*
855 * Free the USB device data.
856 */
857 usb_set_intfdata(usb_intf, NULL);
858 usb_put_dev(interface_to_usbdev(usb_intf));
859}
860EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
861
862#ifdef CONFIG_PM
863int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
864{
865 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
866 struct rt2x00_dev *rt2x00dev = hw->priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700867
Stanislaw Gruszka543cc382011-08-12 14:02:04 +0200868 return rt2x00lib_suspend(rt2x00dev, state);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700869}
870EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
871
872int rt2x00usb_resume(struct usb_interface *usb_intf)
873{
874 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
875 struct rt2x00_dev *rt2x00dev = hw->priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700876
Ivo van Doorn499a2142009-03-28 20:51:58 +0100877 return rt2x00lib_resume(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700878}
879EXPORT_SYMBOL_GPL(rt2x00usb_resume);
880#endif /* CONFIG_PM */
881
882/*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500883 * rt2x00usb module information.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700884 */
885MODULE_AUTHOR(DRV_PROJECT);
886MODULE_VERSION(DRV_VERSION);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500887MODULE_DESCRIPTION("rt2x00 usb library");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700888MODULE_LICENSE("GPL");