blob: 7627af6098eb5896420ef99bbfcc5c022f116d39 [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;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070045 unsigned int pipe =
46 (requesttype == USB_VENDOR_REQUEST_IN) ?
47 usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
Stanislaw Gruszkaad92bc92014-11-26 15:29:13 +010048 unsigned long expire = jiffies + msecs_to_jiffies(timeout);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070049
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
Stanislaw Gruszkaad92bc92014-11-26 15:29:13 +010053 do {
Ivo van Doorn95ea3622007-09-25 17:57:13 -070054 status = usb_control_msg(usb_dev, pipe, request, requesttype,
55 value, offset, buffer, buffer_length,
Stanislaw Gruszkaad92bc92014-11-26 15:29:13 +010056 timeout / 2);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070057 if (status >= 0)
58 return 0;
59
Stanislaw Gruszkaad92bc92014-11-26 15:29:13 +010060 if (status == -ENODEV) {
61 /* Device has disappeared. */
Sean Cross66f84d62009-11-05 20:22:03 +010062 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070063 break;
Sean Cross66f84d62009-11-05 20:22:03 +010064 }
Stanislaw Gruszkaad92bc92014-11-26 15:29:13 +010065 } while (time_before(jiffies, expire));
Ivo van Doorn95ea3622007-09-25 17:57:13 -070066
Joe Perchesec9c4982013-04-19 08:33:40 -070067 rt2x00_err(rt2x00dev,
68 "Vendor Request 0x%02x failed for offset 0x%04x with error %d\n",
69 request, offset, status);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070070
71 return status;
72}
73EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
74
Adam Baker3d823462007-10-27 13:43:29 +020075int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
76 const u8 request, const u8 requesttype,
77 const u16 offset, void *buffer,
78 const u16 buffer_length, const int timeout)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070079{
80 int status;
81
Ivo van Doorn8ff48a82008-11-09 23:40:46 +010082 BUG_ON(!mutex_is_locked(&rt2x00dev->csr_mutex));
Adam Baker3d823462007-10-27 13:43:29 +020083
Ivo van Doorn95ea3622007-09-25 17:57:13 -070084 /*
85 * Check for Cache availability.
86 */
Ivo van Doorn21795092008-02-10 22:49:13 +010087 if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
Joe Perchesec9c4982013-04-19 08:33:40 -070088 rt2x00_err(rt2x00dev, "CSR cache not available\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -070089 return -ENOMEM;
90 }
91
92 if (requesttype == USB_VENDOR_REQUEST_OUT)
Ivo van Doorn21795092008-02-10 22:49:13 +010093 memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070094
95 status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
Ivo van Doorn21795092008-02-10 22:49:13 +010096 offset, 0, rt2x00dev->csr.cache,
Ivo van Doorn95ea3622007-09-25 17:57:13 -070097 buffer_length, timeout);
98
99 if (!status && requesttype == USB_VENDOR_REQUEST_IN)
Ivo van Doorn21795092008-02-10 22:49:13 +0100100 memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700101
102 return status;
103}
Adam Baker3d823462007-10-27 13:43:29 +0200104EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
105
106int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
107 const u8 request, const u8 requesttype,
108 const u16 offset, void *buffer,
Stanislaw Gruszkae9dc51a2014-10-08 14:16:37 +0200109 const u16 buffer_length)
Adam Baker3d823462007-10-27 13:43:29 +0200110{
Iwo Merglered0dbee2008-07-19 16:16:54 +0200111 int status = 0;
112 unsigned char *tb;
113 u16 off, len, bsize;
114
Ivo van Doorn8ff48a82008-11-09 23:40:46 +0100115 mutex_lock(&rt2x00dev->csr_mutex);
Iwo Merglered0dbee2008-07-19 16:16:54 +0200116
Ivo van Doorn82f97b82008-08-02 01:31:09 -0700117 tb = (char *)buffer;
Iwo Merglered0dbee2008-07-19 16:16:54 +0200118 off = offset;
119 len = buffer_length;
120 while (len && !status) {
121 bsize = min_t(u16, CSR_CACHE_SIZE, len);
122 status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
123 requesttype, off, tb,
Stanislaw Gruszkae9dc51a2014-10-08 14:16:37 +0200124 bsize, REGISTER_TIMEOUT);
Iwo Merglered0dbee2008-07-19 16:16:54 +0200125
126 tb += bsize;
127 len -= bsize;
128 off += bsize;
129 }
130
Ivo van Doorn8ff48a82008-11-09 23:40:46 +0100131 mutex_unlock(&rt2x00dev->csr_mutex);
Iwo Merglered0dbee2008-07-19 16:16:54 +0200132
133 return status;
134}
Gertjan van Wingerde96b61ba2010-06-03 10:51:51 +0200135EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
Iwo Merglered0dbee2008-07-19 16:16:54 +0200136
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100137int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
138 const unsigned int offset,
Bartlomiej Zolnierkiewiczf255b922009-11-04 18:35:18 +0100139 const struct rt2x00_field32 field,
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100140 u32 *reg)
141{
142 unsigned int i;
143
Sean Cross66f84d62009-11-05 20:22:03 +0100144 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
145 return -ENODEV;
146
Stanislaw Gruszka7a5a7352014-11-26 15:29:14 +0100147 for (i = 0; i < REGISTER_USB_BUSY_COUNT; i++) {
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100148 rt2x00usb_register_read_lock(rt2x00dev, offset, reg);
149 if (!rt2x00_get_field32(*reg, field))
150 return 1;
151 udelay(REGISTER_BUSY_DELAY);
152 }
153
Joe Perchesec9c4982013-04-19 08:33:40 -0700154 rt2x00_err(rt2x00dev, "Indirect register access failed: offset=0x%.08x, value=0x%.08x\n",
155 offset, *reg);
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100156 *reg = ~0;
157
158 return 0;
159}
160EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read);
161
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200162
163struct rt2x00_async_read_data {
164 __le32 reg;
165 struct usb_ctrlrequest cr;
166 struct rt2x00_dev *rt2x00dev;
Ivo van Doorna073fde2011-04-30 17:14:23 +0200167 bool (*callback)(struct rt2x00_dev *, int, u32);
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200168};
169
170static void rt2x00usb_register_read_async_cb(struct urb *urb)
171{
172 struct rt2x00_async_read_data *rd = urb->context;
Ivo van Doorna073fde2011-04-30 17:14:23 +0200173 if (rd->callback(rd->rt2x00dev, urb->status, le32_to_cpu(rd->reg))) {
174 if (usb_submit_urb(urb, GFP_ATOMIC) < 0)
175 kfree(rd);
176 } else
177 kfree(rd);
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200178}
179
180void rt2x00usb_register_read_async(struct rt2x00_dev *rt2x00dev,
181 const unsigned int offset,
Ivo van Doorna073fde2011-04-30 17:14:23 +0200182 bool (*callback)(struct rt2x00_dev*, int, u32))
Johannes Stezenbach0e0d39e2011-04-18 15:29:12 +0200183{
184 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
185 struct urb *urb;
186 struct rt2x00_async_read_data *rd;
187
188 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
189 if (!rd)
190 return;
191
192 urb = usb_alloc_urb(0, GFP_ATOMIC);
193 if (!urb) {
194 kfree(rd);
195 return;
196 }
197
198 rd->rt2x00dev = rt2x00dev;
199 rd->callback = callback;
200 rd->cr.bRequestType = USB_VENDOR_REQUEST_IN;
201 rd->cr.bRequest = USB_MULTI_READ;
202 rd->cr.wValue = 0;
203 rd->cr.wIndex = cpu_to_le16(offset);
204 rd->cr.wLength = cpu_to_le16(sizeof(u32));
205
206 usb_fill_control_urb(urb, usb_dev, usb_rcvctrlpipe(usb_dev, 0),
207 (unsigned char *)(&rd->cr), &rd->reg, sizeof(rd->reg),
208 rt2x00usb_register_read_async_cb, rd);
209 if (usb_submit_urb(urb, GFP_ATOMIC) < 0)
210 kfree(rd);
211 usb_free_urb(urb);
212}
213EXPORT_SYMBOL_GPL(rt2x00usb_register_read_async);
214
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700215/*
216 * TX data handlers.
217 */
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200218static void rt2x00usb_work_txdone_entry(struct queue_entry *entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700219{
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700220 /*
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200221 * If the transfer to hardware succeeded, it does not mean the
Ivo van Doornfb55f4d12008-05-10 13:42:06 +0200222 * frame was send out correctly. It only means the frame
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300223 * was successfully pushed to the hardware, we have no
Ivo van Doornfb55f4d12008-05-10 13:42:06 +0200224 * way to determine the transmission status right now.
225 * (Only indirectly by looking at the failed TX counters
226 * in the register).
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700227 */
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200228 if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
Ivo van Doorn3392bec2010-08-06 20:46:53 +0200229 rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200230 else
Ivo van Doorn3392bec2010-08-06 20:46:53 +0200231 rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700232}
233
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200234static void rt2x00usb_work_txdone(struct work_struct *work)
235{
236 struct rt2x00_dev *rt2x00dev =
237 container_of(work, struct rt2x00_dev, txdone_work);
238 struct data_queue *queue;
239 struct queue_entry *entry;
240
241 tx_queue_for_each(rt2x00dev, queue) {
242 while (!rt2x00queue_empty(queue)) {
243 entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
244
Ivo van Doorndba5dc12010-12-13 12:36:18 +0100245 if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
246 !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200247 break;
248
249 rt2x00usb_work_txdone_entry(entry);
250 }
251 }
252}
253
254static void rt2x00usb_interrupt_txdone(struct urb *urb)
255{
256 struct queue_entry *entry = (struct queue_entry *)urb->context;
257 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
258
Stanislaw Gruszkadf71c9c2011-08-10 15:32:23 +0200259 if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200260 return;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200261 /*
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200262 * Check if the frame was correctly uploaded
263 */
264 if (urb->status)
Ivo van Doorna1d1eab2010-10-11 15:38:45 +0200265 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
Stanislaw Gruszkadf71c9c2011-08-10 15:32:23 +0200266 /*
267 * Report the frame as DMA done
268 */
269 rt2x00lib_dmadone(entry);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200270
Stanislaw Gruszkadf71c9c2011-08-10 15:32:23 +0200271 if (rt2x00dev->ops->lib->tx_dma_done)
272 rt2x00dev->ops->lib->tx_dma_done(entry);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200273 /*
274 * Schedule the delayed work for reading the TX status
275 * from the device.
276 */
Fred Choub9d305c2014-12-26 16:19:18 +0800277 if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_TXSTATUS_FIFO) ||
Johannes Stezenbachf0187a12011-04-18 15:30:36 +0200278 !kfifo_is_empty(&rt2x00dev->txstatus_fifo))
279 queue_work(rt2x00dev->workqueue, &rt2x00dev->txdone_work);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200280}
281
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100282static bool rt2x00usb_kick_tx_entry(struct queue_entry *entry, void *data)
Ivo van Doornf019d512008-06-06 22:47:39 +0200283{
Gertjan van Wingerdefe725692010-06-29 21:40:34 +0200284 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
285 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
Ivo van Doornf019d512008-06-06 22:47:39 +0200286 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
Gertjan van Wingerdefe725692010-06-29 21:40:34 +0200287 u32 length;
Johannes Stezenbachd7bb5f82010-12-13 12:32:49 +0100288 int status;
Ivo van Doornf019d512008-06-06 22:47:39 +0200289
Ivo van Doorndba5dc12010-12-13 12:36:18 +0100290 if (!test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags) ||
291 test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
Ivo van Doorn4268d8e2011-05-04 21:42:05 +0200292 return false;
Gertjan van Wingerdefe725692010-06-29 21:40:34 +0200293
Ivo van Doornee1e7552010-08-23 19:54:02 +0200294 /*
Jakub Kicińskid823a502011-12-28 01:53:22 +0100295 * USB devices require certain padding at the end of each frame
296 * and urb. Those paddings are not included in skbs. Pass entry
297 * to the driver to determine what the overall length should be.
Ivo van Doornee1e7552010-08-23 19:54:02 +0200298 */
299 length = rt2x00dev->ops->lib->get_tx_data_len(entry);
Gertjan van Wingerdefe725692010-06-29 21:40:34 +0200300
Jakub Kicińskid823a502011-12-28 01:53:22 +0100301 status = skb_padto(entry->skb, length);
302 if (unlikely(status)) {
303 /* TODO: report something more appropriate than IO_FAILED. */
Joe Perchesec9c4982013-04-19 08:33:40 -0700304 rt2x00_warn(rt2x00dev, "TX SKB padding error, out of memory\n");
Jakub Kicińskid823a502011-12-28 01:53:22 +0100305 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
306 rt2x00lib_dmadone(entry);
307
308 return false;
309 }
310
Ivo van Doornee1e7552010-08-23 19:54:02 +0200311 usb_fill_bulk_urb(entry_priv->urb, usb_dev,
312 usb_sndbulkpipe(usb_dev, entry->queue->usb_endpoint),
313 entry->skb->data, length,
314 rt2x00usb_interrupt_txdone, entry);
315
Johannes Stezenbachd7bb5f82010-12-13 12:32:49 +0100316 status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
317 if (status) {
318 if (status == -ENODEV)
319 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
Ivo van Doorna13c8f32010-10-11 15:39:48 +0200320 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
321 rt2x00lib_dmadone(entry);
322 }
Helmut Schaa10e11562011-04-18 15:27:43 +0200323
324 return false;
Ivo van Doornf019d512008-06-06 22:47:39 +0200325}
326
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700327/*
328 * RX data handlers.
329 */
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200330static void rt2x00usb_work_rxdone(struct work_struct *work)
331{
332 struct rt2x00_dev *rt2x00dev =
333 container_of(work, struct rt2x00_dev, rxdone_work);
334 struct queue_entry *entry;
335 struct skb_frame_desc *skbdesc;
336 u8 rxd[32];
337
338 while (!rt2x00queue_empty(rt2x00dev->rx)) {
339 entry = rt2x00queue_get_entry(rt2x00dev->rx, Q_INDEX_DONE);
340
Ivo van Doorndba5dc12010-12-13 12:36:18 +0100341 if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
342 !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200343 break;
344
345 /*
346 * Fill in desc fields of the skb descriptor
347 */
348 skbdesc = get_skb_frame_desc(entry->skb);
349 skbdesc->desc = rxd;
350 skbdesc->desc_len = entry->queue->desc_size;
351
352 /*
353 * Send the frame to rt2x00lib for further processing.
354 */
Helmut Schaa88211022012-04-19 13:24:10 +0200355 rt2x00lib_rxdone(entry, GFP_KERNEL);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200356 }
357}
358
Ivo van Doorn181d6902008-02-05 16:42:23 -0500359static void rt2x00usb_interrupt_rxdone(struct urb *urb)
360{
361 struct queue_entry *entry = (struct queue_entry *)urb->context;
362 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500363
Ivo van Doorna1d1eab2010-10-11 15:38:45 +0200364 if (!test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
Ivo van Doorn181d6902008-02-05 16:42:23 -0500365 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700366
367 /*
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200368 * Report the frame as DMA done
369 */
370 rt2x00lib_dmadone(entry);
371
372 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500373 * Check if the received data is simply too small
374 * to be actually valid, or if the urb is signaling
375 * a problem.
Ivo van Doorn08992f72008-01-24 01:56:25 -0800376 */
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200377 if (urb->actual_length < entry->queue->desc_size || urb->status)
Ivo van Doorna1d1eab2010-10-11 15:38:45 +0200378 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500379
380 /*
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200381 * Schedule the delayed work for reading the RX status
382 * from the device.
Ivo van Doorn181d6902008-02-05 16:42:23 -0500383 */
Ivo van Doorn0439f532011-01-30 13:24:05 +0100384 queue_work(rt2x00dev->workqueue, &rt2x00dev->rxdone_work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700385}
386
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100387static bool rt2x00usb_kick_rx_entry(struct queue_entry *entry, void *data)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100388{
389 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
390 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
391 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
392 int status;
393
Ivo van Doorndba5dc12010-12-13 12:36:18 +0100394 if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
395 test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
Ivo van Doorn4268d8e2011-05-04 21:42:05 +0200396 return false;
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100397
Ivo van Doorn64e7d722010-12-13 12:36:00 +0100398 rt2x00lib_dmastart(entry);
399
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100400 usb_fill_bulk_urb(entry_priv->urb, usb_dev,
401 usb_rcvbulkpipe(usb_dev, entry->queue->usb_endpoint),
402 entry->skb->data, entry->skb->len,
403 rt2x00usb_interrupt_rxdone, entry);
404
405 status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
406 if (status) {
407 if (status == -ENODEV)
408 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
409 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
410 rt2x00lib_dmadone(entry);
411 }
Helmut Schaa10e11562011-04-18 15:27:43 +0200412
413 return false;
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100414}
415
416void rt2x00usb_kick_queue(struct data_queue *queue)
417{
418 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100419 case QID_AC_VO:
420 case QID_AC_VI:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100421 case QID_AC_BE:
422 case QID_AC_BK:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100423 if (!rt2x00queue_empty(queue))
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100424 rt2x00queue_for_each_entry(queue,
425 Q_INDEX_DONE,
426 Q_INDEX,
427 NULL,
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100428 rt2x00usb_kick_tx_entry);
429 break;
430 case QID_RX:
431 if (!rt2x00queue_full(queue))
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100432 rt2x00queue_for_each_entry(queue,
433 Q_INDEX,
434 Q_INDEX_DONE,
435 NULL,
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100436 rt2x00usb_kick_rx_entry);
437 break;
438 default:
439 break;
440 }
441}
442EXPORT_SYMBOL_GPL(rt2x00usb_kick_queue);
443
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100444static bool rt2x00usb_flush_entry(struct queue_entry *entry, void *data)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100445{
446 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
447 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
448 struct queue_entry_priv_usb_bcn *bcn_priv = entry->priv_data;
449
450 if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
Ivo van Doorn4268d8e2011-05-04 21:42:05 +0200451 return false;
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100452
453 usb_kill_urb(entry_priv->urb);
454
455 /*
456 * Kill guardian urb (if required by driver).
457 */
458 if ((entry->queue->qid == QID_BEACON) &&
Fred Choub9d305c2014-12-26 16:19:18 +0800459 (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD)))
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100460 usb_kill_urb(bcn_priv->guardian_urb);
Helmut Schaa10e11562011-04-18 15:27:43 +0200461
462 return false;
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100463}
464
Ivo van Doorn152a5992011-04-18 15:31:02 +0200465void rt2x00usb_flush_queue(struct data_queue *queue, bool drop)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100466{
Ivo van Doorn5be65602010-12-13 12:35:40 +0100467 struct work_struct *completion;
468 unsigned int i;
469
Ivo van Doorn152a5992011-04-18 15:31:02 +0200470 if (drop)
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100471 rt2x00queue_for_each_entry(queue, Q_INDEX_DONE, Q_INDEX, NULL,
Ivo van Doorn152a5992011-04-18 15:31:02 +0200472 rt2x00usb_flush_entry);
Ivo van Doorn5be65602010-12-13 12:35:40 +0100473
474 /*
475 * Obtain the queue completion handler
476 */
477 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100478 case QID_AC_VO:
479 case QID_AC_VI:
Ivo van Doorn5be65602010-12-13 12:35:40 +0100480 case QID_AC_BE:
481 case QID_AC_BK:
Ivo van Doorn5be65602010-12-13 12:35:40 +0100482 completion = &queue->rt2x00dev->txdone_work;
483 break;
484 case QID_RX:
485 completion = &queue->rt2x00dev->rxdone_work;
486 break;
487 default:
488 return;
489 }
490
Ivo van Doorn152a5992011-04-18 15:31:02 +0200491 for (i = 0; i < 10; i++) {
Ivo van Doorn5be65602010-12-13 12:35:40 +0100492 /*
493 * Check if the driver is already done, otherwise we
494 * have to sleep a little while to give the driver/hw
495 * the oppurtunity to complete interrupt process itself.
496 */
497 if (rt2x00queue_empty(queue))
498 break;
499
500 /*
501 * Schedule the completion handler manually, when this
502 * worker function runs, it should cleanup the queue.
503 */
Ivo van Doorn0439f532011-01-30 13:24:05 +0100504 queue_work(queue->rt2x00dev->workqueue, completion);
Ivo van Doorn5be65602010-12-13 12:35:40 +0100505
506 /*
507 * Wait for a little while to give the driver
508 * the oppurtunity to recover itself.
509 */
510 msleep(10);
511 }
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100512}
Ivo van Doorn5be65602010-12-13 12:35:40 +0100513EXPORT_SYMBOL_GPL(rt2x00usb_flush_queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100514
515static void rt2x00usb_watchdog_tx_dma(struct data_queue *queue)
516{
Joe Perchesec9c4982013-04-19 08:33:40 -0700517 rt2x00_warn(queue->rt2x00dev, "TX queue %d DMA timed out, invoke forced forced reset\n",
518 queue->qid);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100519
Stanislaw Gruszkafdbdd252013-10-05 18:15:33 +0200520 rt2x00queue_stop_queue(queue);
Ivo van Doorn5be65602010-12-13 12:35:40 +0100521 rt2x00queue_flush_queue(queue, true);
Stanislaw Gruszkafdbdd252013-10-05 18:15:33 +0200522 rt2x00queue_start_queue(queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100523}
524
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200525static int rt2x00usb_dma_timeout(struct data_queue *queue)
526{
527 struct queue_entry *entry;
528
529 entry = rt2x00queue_get_entry(queue, Q_INDEX_DMA_DONE);
530 return rt2x00queue_dma_timeout(entry);
531}
532
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100533void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev)
534{
535 struct data_queue *queue;
536
537 tx_queue_for_each(rt2x00dev, queue) {
538 if (!rt2x00queue_empty(queue)) {
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200539 if (rt2x00usb_dma_timeout(queue))
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100540 rt2x00usb_watchdog_tx_dma(queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100541 }
542 }
543}
544EXPORT_SYMBOL_GPL(rt2x00usb_watchdog);
545
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700546/*
547 * Radio handlers
548 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700549void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
550{
Ivo van Doornbd394a72008-04-21 19:01:58 +0200551 rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700552 REGISTER_TIMEOUT);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700553}
554EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
555
556/*
557 * Device initialization handlers.
558 */
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100559void rt2x00usb_clear_entry(struct queue_entry *entry)
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100560{
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200561 entry->flags = 0;
562
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100563 if (entry->queue->qid == QID_RX)
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100564 rt2x00usb_kick_rx_entry(entry, NULL);
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100565}
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100566EXPORT_SYMBOL_GPL(rt2x00usb_clear_entry);
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100567
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100568static void rt2x00usb_assign_endpoint(struct data_queue *queue,
569 struct usb_endpoint_descriptor *ep_desc)
570{
571 struct usb_device *usb_dev = to_usb_device_intf(queue->rt2x00dev->dev);
572 int pipe;
573
574 queue->usb_endpoint = usb_endpoint_num(ep_desc);
575
576 if (queue->qid == QID_RX) {
577 pipe = usb_rcvbulkpipe(usb_dev, queue->usb_endpoint);
578 queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 0);
579 } else {
580 pipe = usb_sndbulkpipe(usb_dev, queue->usb_endpoint);
581 queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 1);
582 }
583
584 if (!queue->usb_maxpacket)
585 queue->usb_maxpacket = 1;
586}
587
588static int rt2x00usb_find_endpoints(struct rt2x00_dev *rt2x00dev)
589{
590 struct usb_interface *intf = to_usb_interface(rt2x00dev->dev);
591 struct usb_host_interface *intf_desc = intf->cur_altsetting;
592 struct usb_endpoint_descriptor *ep_desc;
593 struct data_queue *queue = rt2x00dev->tx;
594 struct usb_endpoint_descriptor *tx_ep_desc = NULL;
595 unsigned int i;
596
597 /*
598 * Walk through all available endpoints to search for "bulk in"
599 * and "bulk out" endpoints. When we find such endpoints collect
600 * the information we need from the descriptor and assign it
601 * to the queue.
602 */
603 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
604 ep_desc = &intf_desc->endpoint[i].desc;
605
606 if (usb_endpoint_is_bulk_in(ep_desc)) {
607 rt2x00usb_assign_endpoint(rt2x00dev->rx, ep_desc);
Ivo van Doornd15cfc32008-12-20 11:00:23 +0100608 } else if (usb_endpoint_is_bulk_out(ep_desc) &&
609 (queue != queue_end(rt2x00dev))) {
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100610 rt2x00usb_assign_endpoint(queue, ep_desc);
Ivo van Doornd15cfc32008-12-20 11:00:23 +0100611 queue = queue_next(queue);
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100612
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100613 tx_ep_desc = ep_desc;
614 }
615 }
616
617 /*
618 * At least 1 endpoint for RX and 1 endpoint for TX must be available.
619 */
620 if (!rt2x00dev->rx->usb_endpoint || !rt2x00dev->tx->usb_endpoint) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700621 rt2x00_err(rt2x00dev, "Bulk-in/Bulk-out endpoints not found\n");
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100622 return -EPIPE;
623 }
624
625 /*
626 * It might be possible not all queues have a dedicated endpoint.
627 * Loop through all TX queues and copy the endpoint information
628 * which we have gathered from already assigned endpoints.
629 */
630 txall_queue_for_each(rt2x00dev, queue) {
631 if (!queue->usb_endpoint)
632 rt2x00usb_assign_endpoint(queue, tx_ep_desc);
633 }
634
635 return 0;
636}
637
Ivo van Doornfa695602010-10-11 15:37:25 +0200638static int rt2x00usb_alloc_entries(struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700639{
Ivo van Doornfa695602010-10-11 15:37:25 +0200640 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200641 struct queue_entry_priv_usb *entry_priv;
642 struct queue_entry_priv_usb_bcn *bcn_priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700643 unsigned int i;
644
Ivo van Doorn181d6902008-02-05 16:42:23 -0500645 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200646 entry_priv = queue->entries[i].priv_data;
647 entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
648 if (!entry_priv->urb)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700649 return -ENOMEM;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200650 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500651
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200652 /*
653 * If this is not the beacon queue or
654 * no guardian byte was required for the beacon,
655 * then we are done.
656 */
Ivo van Doornfa695602010-10-11 15:37:25 +0200657 if (queue->qid != QID_BEACON ||
Fred Choub9d305c2014-12-26 16:19:18 +0800658 !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200659 return 0;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500660
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200661 for (i = 0; i < queue->limit; i++) {
662 bcn_priv = queue->entries[i].priv_data;
663 bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
664 if (!bcn_priv->guardian_urb)
665 return -ENOMEM;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700666 }
667
668 return 0;
669}
670
Ivo van Doornfa695602010-10-11 15:37:25 +0200671static void rt2x00usb_free_entries(struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700672{
Ivo van Doornfa695602010-10-11 15:37:25 +0200673 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200674 struct queue_entry_priv_usb *entry_priv;
675 struct queue_entry_priv_usb_bcn *bcn_priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700676 unsigned int i;
677
Ivo van Doorn181d6902008-02-05 16:42:23 -0500678 if (!queue->entries)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700679 return;
680
Ivo van Doorn181d6902008-02-05 16:42:23 -0500681 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200682 entry_priv = queue->entries[i].priv_data;
683 usb_kill_urb(entry_priv->urb);
684 usb_free_urb(entry_priv->urb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700685 }
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200686
687 /*
688 * If this is not the beacon queue or
689 * no guardian byte was required for the beacon,
690 * then we are done.
691 */
Ivo van Doornfa695602010-10-11 15:37:25 +0200692 if (queue->qid != QID_BEACON ||
Fred Choub9d305c2014-12-26 16:19:18 +0800693 !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200694 return;
695
696 for (i = 0; i < queue->limit; i++) {
697 bcn_priv = queue->entries[i].priv_data;
698 usb_kill_urb(bcn_priv->guardian_urb);
699 usb_free_urb(bcn_priv->guardian_urb);
700 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700701}
702
703int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
704{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500705 struct data_queue *queue;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200706 int status;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700707
708 /*
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100709 * Find endpoints for each queue
710 */
711 status = rt2x00usb_find_endpoints(rt2x00dev);
712 if (status)
713 goto exit;
714
715 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700716 * Allocate DMA
717 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500718 queue_for_each(rt2x00dev, queue) {
Ivo van Doornfa695602010-10-11 15:37:25 +0200719 status = rt2x00usb_alloc_entries(queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700720 if (status)
721 goto exit;
722 }
723
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700724 return 0;
725
726exit:
727 rt2x00usb_uninitialize(rt2x00dev);
728
729 return status;
730}
731EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
732
733void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
734{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500735 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700736
Ivo van Doorn181d6902008-02-05 16:42:23 -0500737 queue_for_each(rt2x00dev, queue)
Ivo van Doornfa695602010-10-11 15:37:25 +0200738 rt2x00usb_free_entries(queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700739}
740EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
741
742/*
743 * USB driver handlers.
744 */
745static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
746{
747 kfree(rt2x00dev->rf);
748 rt2x00dev->rf = NULL;
749
750 kfree(rt2x00dev->eeprom);
751 rt2x00dev->eeprom = NULL;
752
Ivo van Doorn21795092008-02-10 22:49:13 +0100753 kfree(rt2x00dev->csr.cache);
754 rt2x00dev->csr.cache = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700755}
756
757static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
758{
Ivo van Doorn21795092008-02-10 22:49:13 +0100759 rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
760 if (!rt2x00dev->csr.cache)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700761 goto exit;
762
763 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
764 if (!rt2x00dev->eeprom)
765 goto exit;
766
767 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
768 if (!rt2x00dev->rf)
769 goto exit;
770
771 return 0;
772
773exit:
Joe Perchesec9c4982013-04-19 08:33:40 -0700774 rt2x00_probe_err("Failed to allocate registers\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700775
776 rt2x00usb_free_reg(rt2x00dev);
777
778 return -ENOMEM;
779}
780
781int rt2x00usb_probe(struct usb_interface *usb_intf,
Gertjan van Wingerdee01ae272011-04-18 15:32:13 +0200782 const struct rt2x00_ops *ops)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700783{
784 struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700785 struct ieee80211_hw *hw;
786 struct rt2x00_dev *rt2x00dev;
787 int retval;
788
789 usb_dev = usb_get_dev(usb_dev);
Stanislaw Gruszkabf4c02d2011-06-19 19:47:39 +0200790 usb_reset_device(usb_dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700791
792 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
793 if (!hw) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700794 rt2x00_probe_err("Failed to allocate hardware\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700795 retval = -ENOMEM;
796 goto exit_put_device;
797 }
798
799 usb_set_intfdata(usb_intf, hw);
800
801 rt2x00dev = hw->priv;
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200802 rt2x00dev->dev = &usb_intf->dev;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700803 rt2x00dev->ops = ops;
804 rt2x00dev->hw = hw;
805
Gertjan van Wingerde2015d192009-11-08 12:30:14 +0100806 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_USB);
807
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200808 INIT_WORK(&rt2x00dev->rxdone_work, rt2x00usb_work_rxdone);
809 INIT_WORK(&rt2x00dev->txdone_work, rt2x00usb_work_txdone);
Stanislaw Gruszkaf4211112012-03-14 11:16:19 +0100810 hrtimer_init(&rt2x00dev->txstatus_timer, CLOCK_MONOTONIC,
811 HRTIMER_MODE_REL);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200812
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700813 retval = rt2x00usb_alloc_reg(rt2x00dev);
814 if (retval)
815 goto exit_free_device;
816
817 retval = rt2x00lib_probe_dev(rt2x00dev);
818 if (retval)
819 goto exit_free_reg;
820
821 return 0;
822
823exit_free_reg:
824 rt2x00usb_free_reg(rt2x00dev);
825
826exit_free_device:
827 ieee80211_free_hw(hw);
828
829exit_put_device:
830 usb_put_dev(usb_dev);
831
832 usb_set_intfdata(usb_intf, NULL);
833
834 return retval;
835}
836EXPORT_SYMBOL_GPL(rt2x00usb_probe);
837
838void rt2x00usb_disconnect(struct usb_interface *usb_intf)
839{
840 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
841 struct rt2x00_dev *rt2x00dev = hw->priv;
842
843 /*
844 * Free all allocated data.
845 */
846 rt2x00lib_remove_dev(rt2x00dev);
847 rt2x00usb_free_reg(rt2x00dev);
848 ieee80211_free_hw(hw);
849
850 /*
851 * Free the USB device data.
852 */
853 usb_set_intfdata(usb_intf, NULL);
854 usb_put_dev(interface_to_usbdev(usb_intf));
855}
856EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
857
858#ifdef CONFIG_PM
859int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
860{
861 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
862 struct rt2x00_dev *rt2x00dev = hw->priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700863
Stanislaw Gruszka543cc382011-08-12 14:02:04 +0200864 return rt2x00lib_suspend(rt2x00dev, state);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700865}
866EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
867
868int rt2x00usb_resume(struct usb_interface *usb_intf)
869{
870 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
871 struct rt2x00_dev *rt2x00dev = hw->priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700872
Ivo van Doorn499a2142009-03-28 20:51:58 +0100873 return rt2x00lib_resume(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700874}
875EXPORT_SYMBOL_GPL(rt2x00usb_resume);
876#endif /* CONFIG_PM */
877
878/*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500879 * rt2x00usb module information.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700880 */
881MODULE_AUTHOR(DRV_PROJECT);
882MODULE_VERSION(DRV_VERSION);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500883MODULE_DESCRIPTION("rt2x00 usb library");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700884MODULE_LICENSE("GPL");