blob: 0bc8dccd0f0ac968d8df6e2fca50be6bcd213361 [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
17 along with this program; if not, write to the
18 Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22/*
23 Module: rt2x00usb
24 Abstract: rt2x00 generic usb device routines.
25 */
26
Ivo van Doorn95ea3622007-09-25 17:57:13 -070027#include <linux/kernel.h>
28#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Ivo van Doorn95ea3622007-09-25 17:57:13 -070030#include <linux/usb.h>
Adam Baker3d823462007-10-27 13:43:29 +020031#include <linux/bug.h>
Ivo van Doorn95ea3622007-09-25 17:57:13 -070032
33#include "rt2x00.h"
34#include "rt2x00usb.h"
35
36/*
37 * Interfacing with the HW.
38 */
Adam Baker0e14f6d2007-10-27 13:41:25 +020039int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn95ea3622007-09-25 17:57:13 -070040 const u8 request, const u8 requesttype,
41 const u16 offset, const u16 value,
42 void *buffer, const u16 buffer_length,
Ivo van Doorne9136552007-09-25 20:54:20 +020043 const int timeout)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070044{
Ivo van Doornc1d35df2008-06-16 19:57:11 +020045 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070046 int status;
47 unsigned int i;
48 unsigned int pipe =
49 (requesttype == USB_VENDOR_REQUEST_IN) ?
50 usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
51
Sean Cross66f84d62009-11-05 20:22:03 +010052 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
53 return -ENODEV;
Adam Baker3d823462007-10-27 13:43:29 +020054
Ivo van Doorn95ea3622007-09-25 17:57:13 -070055 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
56 status = usb_control_msg(usb_dev, pipe, request, requesttype,
57 value, offset, buffer, buffer_length,
58 timeout);
59 if (status >= 0)
60 return 0;
61
62 /*
Ivo van Doorne9136552007-09-25 20:54:20 +020063 * Check for errors
Ivo van Doorn95ea3622007-09-25 17:57:13 -070064 * -ENODEV: Device has disappeared, no point continuing.
Ivo van Doorne9136552007-09-25 20:54:20 +020065 * All other errors: Try again.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070066 */
Sean Cross66f84d62009-11-05 20:22:03 +010067 else if (status == -ENODEV) {
68 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070069 break;
Sean Cross66f84d62009-11-05 20:22:03 +010070 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -070071 }
72
73 ERROR(rt2x00dev,
74 "Vendor Request 0x%02x failed for offset 0x%04x with error %d.\n",
75 request, offset, status);
76
77 return status;
78}
79EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
80
Adam Baker3d823462007-10-27 13:43:29 +020081int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
82 const u8 request, const u8 requesttype,
83 const u16 offset, void *buffer,
84 const u16 buffer_length, const int timeout)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070085{
86 int status;
87
Ivo van Doorn8ff48a82008-11-09 23:40:46 +010088 BUG_ON(!mutex_is_locked(&rt2x00dev->csr_mutex));
Adam Baker3d823462007-10-27 13:43:29 +020089
Ivo van Doorn95ea3622007-09-25 17:57:13 -070090 /*
91 * Check for Cache availability.
92 */
Ivo van Doorn21795092008-02-10 22:49:13 +010093 if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -070094 ERROR(rt2x00dev, "CSR cache not available.\n");
95 return -ENOMEM;
96 }
97
98 if (requesttype == USB_VENDOR_REQUEST_OUT)
Ivo van Doorn21795092008-02-10 22:49:13 +010099 memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700100
101 status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
Ivo van Doorn21795092008-02-10 22:49:13 +0100102 offset, 0, rt2x00dev->csr.cache,
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700103 buffer_length, timeout);
104
105 if (!status && requesttype == USB_VENDOR_REQUEST_IN)
Ivo van Doorn21795092008-02-10 22:49:13 +0100106 memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700107
108 return status;
109}
Adam Baker3d823462007-10-27 13:43:29 +0200110EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
111
112int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
113 const u8 request, const u8 requesttype,
114 const u16 offset, void *buffer,
115 const u16 buffer_length, const int timeout)
116{
Iwo Merglered0dbee2008-07-19 16:16:54 +0200117 int status = 0;
118 unsigned char *tb;
119 u16 off, len, bsize;
120
Ivo van Doorn8ff48a82008-11-09 23:40:46 +0100121 mutex_lock(&rt2x00dev->csr_mutex);
Iwo Merglered0dbee2008-07-19 16:16:54 +0200122
Ivo van Doorn82f97b82008-08-02 01:31:09 -0700123 tb = (char *)buffer;
Iwo Merglered0dbee2008-07-19 16:16:54 +0200124 off = offset;
125 len = buffer_length;
126 while (len && !status) {
127 bsize = min_t(u16, CSR_CACHE_SIZE, len);
128 status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
129 requesttype, off, tb,
130 bsize, timeout);
131
132 tb += bsize;
133 len -= bsize;
134 off += bsize;
135 }
136
Ivo van Doorn8ff48a82008-11-09 23:40:46 +0100137 mutex_unlock(&rt2x00dev->csr_mutex);
Iwo Merglered0dbee2008-07-19 16:16:54 +0200138
139 return status;
140}
Gertjan van Wingerde96b61ba2010-06-03 10:51:51 +0200141EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
Iwo Merglered0dbee2008-07-19 16:16:54 +0200142
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100143int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
144 const unsigned int offset,
Bartlomiej Zolnierkiewiczf255b922009-11-04 18:35:18 +0100145 const struct rt2x00_field32 field,
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100146 u32 *reg)
147{
148 unsigned int i;
149
Sean Cross66f84d62009-11-05 20:22:03 +0100150 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
151 return -ENODEV;
152
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100153 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
154 rt2x00usb_register_read_lock(rt2x00dev, offset, reg);
155 if (!rt2x00_get_field32(*reg, field))
156 return 1;
157 udelay(REGISTER_BUSY_DELAY);
158 }
159
160 ERROR(rt2x00dev, "Indirect register access failed: "
161 "offset=0x%.08x, value=0x%.08x\n", offset, *reg);
162 *reg = ~0;
163
164 return 0;
165}
166EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read);
167
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700168/*
169 * TX data handlers.
170 */
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200171static void rt2x00usb_work_txdone_entry(struct queue_entry *entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700172{
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700173 /*
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200174 * If the transfer to hardware succeeded, it does not mean the
Ivo van Doornfb55f4d12008-05-10 13:42:06 +0200175 * frame was send out correctly. It only means the frame
176 * was succesfully pushed to the hardware, we have no
177 * way to determine the transmission status right now.
178 * (Only indirectly by looking at the failed TX counters
179 * in the register).
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700180 */
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200181 if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
Ivo van Doorn3392bec2010-08-06 20:46:53 +0200182 rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200183 else
Ivo van Doorn3392bec2010-08-06 20:46:53 +0200184 rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700185}
186
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200187static void rt2x00usb_work_txdone(struct work_struct *work)
188{
189 struct rt2x00_dev *rt2x00dev =
190 container_of(work, struct rt2x00_dev, txdone_work);
191 struct data_queue *queue;
192 struct queue_entry *entry;
193
194 tx_queue_for_each(rt2x00dev, queue) {
195 while (!rt2x00queue_empty(queue)) {
196 entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
197
Ivo van Doorndba5dc12010-12-13 12:36:18 +0100198 if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
199 !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200200 break;
201
202 rt2x00usb_work_txdone_entry(entry);
203 }
204 }
205}
206
207static void rt2x00usb_interrupt_txdone(struct urb *urb)
208{
209 struct queue_entry *entry = (struct queue_entry *)urb->context;
210 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
211
Ivo van Doorna1d1eab2010-10-11 15:38:45 +0200212 if (!test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200213 return;
214
215 /*
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200216 * Report the frame as DMA done
217 */
218 rt2x00lib_dmadone(entry);
219
220 /*
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200221 * Check if the frame was correctly uploaded
222 */
223 if (urb->status)
Ivo van Doorna1d1eab2010-10-11 15:38:45 +0200224 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200225
226 /*
227 * Schedule the delayed work for reading the TX status
228 * from the device.
229 */
Ivo van Doorn0439f532011-01-30 13:24:05 +0100230 queue_work(rt2x00dev->workqueue, &rt2x00dev->txdone_work);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200231}
232
Helmut Schaa10e11562011-04-18 15:27:43 +0200233static bool rt2x00usb_kick_tx_entry(struct queue_entry *entry, void* data)
Ivo van Doornf019d512008-06-06 22:47:39 +0200234{
Gertjan van Wingerdefe725692010-06-29 21:40:34 +0200235 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
236 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
Ivo van Doornf019d512008-06-06 22:47:39 +0200237 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
Gertjan van Wingerdefe725692010-06-29 21:40:34 +0200238 u32 length;
Johannes Stezenbachd7bb5f82010-12-13 12:32:49 +0100239 int status;
Ivo van Doornf019d512008-06-06 22:47:39 +0200240
Ivo van Doorndba5dc12010-12-13 12:36:18 +0100241 if (!test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags) ||
242 test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
Helmut Schaa10e11562011-04-18 15:27:43 +0200243 return true;
Gertjan van Wingerdefe725692010-06-29 21:40:34 +0200244
Ivo van Doornee1e7552010-08-23 19:54:02 +0200245 /*
246 * USB devices cannot blindly pass the skb->len as the
247 * length of the data to usb_fill_bulk_urb. Pass the skb
248 * to the driver to determine what the length should be.
249 */
250 length = rt2x00dev->ops->lib->get_tx_data_len(entry);
Gertjan van Wingerdefe725692010-06-29 21:40:34 +0200251
Ivo van Doornee1e7552010-08-23 19:54:02 +0200252 usb_fill_bulk_urb(entry_priv->urb, usb_dev,
253 usb_sndbulkpipe(usb_dev, entry->queue->usb_endpoint),
254 entry->skb->data, length,
255 rt2x00usb_interrupt_txdone, entry);
256
Johannes Stezenbachd7bb5f82010-12-13 12:32:49 +0100257 status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
258 if (status) {
259 if (status == -ENODEV)
260 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
Ivo van Doorna13c8f32010-10-11 15:39:48 +0200261 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
262 rt2x00lib_dmadone(entry);
263 }
Helmut Schaa10e11562011-04-18 15:27:43 +0200264
265 return false;
Ivo van Doornf019d512008-06-06 22:47:39 +0200266}
267
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700268/*
269 * RX data handlers.
270 */
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200271static void rt2x00usb_work_rxdone(struct work_struct *work)
272{
273 struct rt2x00_dev *rt2x00dev =
274 container_of(work, struct rt2x00_dev, rxdone_work);
275 struct queue_entry *entry;
276 struct skb_frame_desc *skbdesc;
277 u8 rxd[32];
278
279 while (!rt2x00queue_empty(rt2x00dev->rx)) {
280 entry = rt2x00queue_get_entry(rt2x00dev->rx, Q_INDEX_DONE);
281
Ivo van Doorndba5dc12010-12-13 12:36:18 +0100282 if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
283 !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200284 break;
285
286 /*
287 * Fill in desc fields of the skb descriptor
288 */
289 skbdesc = get_skb_frame_desc(entry->skb);
290 skbdesc->desc = rxd;
291 skbdesc->desc_len = entry->queue->desc_size;
292
293 /*
294 * Send the frame to rt2x00lib for further processing.
295 */
Ivo van Doornfa695602010-10-11 15:37:25 +0200296 rt2x00lib_rxdone(entry);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200297 }
298}
299
Ivo van Doorn181d6902008-02-05 16:42:23 -0500300static void rt2x00usb_interrupt_rxdone(struct urb *urb)
301{
302 struct queue_entry *entry = (struct queue_entry *)urb->context;
303 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500304
Ivo van Doorna1d1eab2010-10-11 15:38:45 +0200305 if (!test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
Ivo van Doorn181d6902008-02-05 16:42:23 -0500306 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700307
308 /*
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200309 * Report the frame as DMA done
310 */
311 rt2x00lib_dmadone(entry);
312
313 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500314 * Check if the received data is simply too small
315 * to be actually valid, or if the urb is signaling
316 * a problem.
Ivo van Doorn08992f72008-01-24 01:56:25 -0800317 */
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200318 if (urb->actual_length < entry->queue->desc_size || urb->status)
Ivo van Doorna1d1eab2010-10-11 15:38:45 +0200319 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500320
321 /*
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200322 * Schedule the delayed work for reading the RX status
323 * from the device.
Ivo van Doorn181d6902008-02-05 16:42:23 -0500324 */
Ivo van Doorn0439f532011-01-30 13:24:05 +0100325 queue_work(rt2x00dev->workqueue, &rt2x00dev->rxdone_work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700326}
327
Helmut Schaa10e11562011-04-18 15:27:43 +0200328static bool rt2x00usb_kick_rx_entry(struct queue_entry *entry, void* data)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100329{
330 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
331 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
332 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
333 int status;
334
Ivo van Doorndba5dc12010-12-13 12:36:18 +0100335 if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
336 test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
Helmut Schaa10e11562011-04-18 15:27:43 +0200337 return true;
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100338
Ivo van Doorn64e7d722010-12-13 12:36:00 +0100339 rt2x00lib_dmastart(entry);
340
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100341 usb_fill_bulk_urb(entry_priv->urb, usb_dev,
342 usb_rcvbulkpipe(usb_dev, entry->queue->usb_endpoint),
343 entry->skb->data, entry->skb->len,
344 rt2x00usb_interrupt_rxdone, entry);
345
346 status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
347 if (status) {
348 if (status == -ENODEV)
349 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
350 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
351 rt2x00lib_dmadone(entry);
352 }
Helmut Schaa10e11562011-04-18 15:27:43 +0200353
354 return false;
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100355}
356
357void rt2x00usb_kick_queue(struct data_queue *queue)
358{
359 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100360 case QID_AC_VO:
361 case QID_AC_VI:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100362 case QID_AC_BE:
363 case QID_AC_BK:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100364 if (!rt2x00queue_empty(queue))
Helmut Schaa10e11562011-04-18 15:27:43 +0200365 rt2x00queue_for_each_entry(queue,
366 Q_INDEX_DONE,
367 Q_INDEX,
368 NULL,
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100369 rt2x00usb_kick_tx_entry);
370 break;
371 case QID_RX:
372 if (!rt2x00queue_full(queue))
Helmut Schaa10e11562011-04-18 15:27:43 +0200373 rt2x00queue_for_each_entry(queue,
374 Q_INDEX_DONE,
375 Q_INDEX,
376 NULL,
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100377 rt2x00usb_kick_rx_entry);
378 break;
379 default:
380 break;
381 }
382}
383EXPORT_SYMBOL_GPL(rt2x00usb_kick_queue);
384
Helmut Schaa10e11562011-04-18 15:27:43 +0200385static bool rt2x00usb_flush_entry(struct queue_entry *entry, void* data)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100386{
387 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
388 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
389 struct queue_entry_priv_usb_bcn *bcn_priv = entry->priv_data;
390
391 if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
Helmut Schaa10e11562011-04-18 15:27:43 +0200392 return true;
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100393
394 usb_kill_urb(entry_priv->urb);
395
396 /*
397 * Kill guardian urb (if required by driver).
398 */
399 if ((entry->queue->qid == QID_BEACON) &&
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200400 (test_bit(REQUIRE_BEACON_GUARD, &rt2x00dev->cap_flags)))
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100401 usb_kill_urb(bcn_priv->guardian_urb);
Helmut Schaa10e11562011-04-18 15:27:43 +0200402
403 return false;
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100404}
405
Ivo van Doorn5be65602010-12-13 12:35:40 +0100406void rt2x00usb_flush_queue(struct data_queue *queue)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100407{
Ivo van Doorn5be65602010-12-13 12:35:40 +0100408 struct work_struct *completion;
409 unsigned int i;
410
Helmut Schaa10e11562011-04-18 15:27:43 +0200411 rt2x00queue_for_each_entry(queue, Q_INDEX_DONE, Q_INDEX, NULL,
Ivo van Doorn5be65602010-12-13 12:35:40 +0100412 rt2x00usb_flush_entry);
413
414 /*
415 * Obtain the queue completion handler
416 */
417 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100418 case QID_AC_VO:
419 case QID_AC_VI:
Ivo van Doorn5be65602010-12-13 12:35:40 +0100420 case QID_AC_BE:
421 case QID_AC_BK:
Ivo van Doorn5be65602010-12-13 12:35:40 +0100422 completion = &queue->rt2x00dev->txdone_work;
423 break;
424 case QID_RX:
425 completion = &queue->rt2x00dev->rxdone_work;
426 break;
427 default:
428 return;
429 }
430
431 for (i = 0; i < 20; i++) {
432 /*
433 * Check if the driver is already done, otherwise we
434 * have to sleep a little while to give the driver/hw
435 * the oppurtunity to complete interrupt process itself.
436 */
437 if (rt2x00queue_empty(queue))
438 break;
439
440 /*
441 * Schedule the completion handler manually, when this
442 * worker function runs, it should cleanup the queue.
443 */
Ivo van Doorn0439f532011-01-30 13:24:05 +0100444 queue_work(queue->rt2x00dev->workqueue, completion);
Ivo van Doorn5be65602010-12-13 12:35:40 +0100445
446 /*
447 * Wait for a little while to give the driver
448 * the oppurtunity to recover itself.
449 */
450 msleep(10);
451 }
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100452}
Ivo van Doorn5be65602010-12-13 12:35:40 +0100453EXPORT_SYMBOL_GPL(rt2x00usb_flush_queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100454
455static void rt2x00usb_watchdog_tx_dma(struct data_queue *queue)
456{
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100457 WARNING(queue->rt2x00dev, "TX queue %d DMA timed out,"
458 " invoke forced forced reset\n", queue->qid);
459
Ivo van Doorn5be65602010-12-13 12:35:40 +0100460 rt2x00queue_flush_queue(queue, true);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100461}
462
463static void rt2x00usb_watchdog_tx_status(struct data_queue *queue)
464{
465 WARNING(queue->rt2x00dev, "TX queue %d status timed out,"
466 " invoke forced tx handler\n", queue->qid);
467
Ivo van Doorn0439f532011-01-30 13:24:05 +0100468 queue_work(queue->rt2x00dev->workqueue, &queue->rt2x00dev->txdone_work);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100469}
470
471void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev)
472{
473 struct data_queue *queue;
474
475 tx_queue_for_each(rt2x00dev, queue) {
476 if (!rt2x00queue_empty(queue)) {
477 if (rt2x00queue_dma_timeout(queue))
478 rt2x00usb_watchdog_tx_dma(queue);
479 if (rt2x00queue_status_timeout(queue))
480 rt2x00usb_watchdog_tx_status(queue);
481 }
482 }
483}
484EXPORT_SYMBOL_GPL(rt2x00usb_watchdog);
485
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700486/*
487 * Radio handlers
488 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700489void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
490{
Ivo van Doornbd394a72008-04-21 19:01:58 +0200491 rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700492 REGISTER_TIMEOUT);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700493}
494EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
495
496/*
497 * Device initialization handlers.
498 */
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100499void rt2x00usb_clear_entry(struct queue_entry *entry)
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100500{
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200501 entry->flags = 0;
502
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100503 if (entry->queue->qid == QID_RX)
Helmut Schaa10e11562011-04-18 15:27:43 +0200504 rt2x00usb_kick_rx_entry(entry, NULL);
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100505}
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100506EXPORT_SYMBOL_GPL(rt2x00usb_clear_entry);
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100507
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100508static void rt2x00usb_assign_endpoint(struct data_queue *queue,
509 struct usb_endpoint_descriptor *ep_desc)
510{
511 struct usb_device *usb_dev = to_usb_device_intf(queue->rt2x00dev->dev);
512 int pipe;
513
514 queue->usb_endpoint = usb_endpoint_num(ep_desc);
515
516 if (queue->qid == QID_RX) {
517 pipe = usb_rcvbulkpipe(usb_dev, queue->usb_endpoint);
518 queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 0);
519 } else {
520 pipe = usb_sndbulkpipe(usb_dev, queue->usb_endpoint);
521 queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 1);
522 }
523
524 if (!queue->usb_maxpacket)
525 queue->usb_maxpacket = 1;
526}
527
528static int rt2x00usb_find_endpoints(struct rt2x00_dev *rt2x00dev)
529{
530 struct usb_interface *intf = to_usb_interface(rt2x00dev->dev);
531 struct usb_host_interface *intf_desc = intf->cur_altsetting;
532 struct usb_endpoint_descriptor *ep_desc;
533 struct data_queue *queue = rt2x00dev->tx;
534 struct usb_endpoint_descriptor *tx_ep_desc = NULL;
535 unsigned int i;
536
537 /*
538 * Walk through all available endpoints to search for "bulk in"
539 * and "bulk out" endpoints. When we find such endpoints collect
540 * the information we need from the descriptor and assign it
541 * to the queue.
542 */
543 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
544 ep_desc = &intf_desc->endpoint[i].desc;
545
546 if (usb_endpoint_is_bulk_in(ep_desc)) {
547 rt2x00usb_assign_endpoint(rt2x00dev->rx, ep_desc);
Ivo van Doornd15cfc32008-12-20 11:00:23 +0100548 } else if (usb_endpoint_is_bulk_out(ep_desc) &&
549 (queue != queue_end(rt2x00dev))) {
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100550 rt2x00usb_assign_endpoint(queue, ep_desc);
Ivo van Doornd15cfc32008-12-20 11:00:23 +0100551 queue = queue_next(queue);
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100552
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100553 tx_ep_desc = ep_desc;
554 }
555 }
556
557 /*
558 * At least 1 endpoint for RX and 1 endpoint for TX must be available.
559 */
560 if (!rt2x00dev->rx->usb_endpoint || !rt2x00dev->tx->usb_endpoint) {
561 ERROR(rt2x00dev, "Bulk-in/Bulk-out endpoints not found\n");
562 return -EPIPE;
563 }
564
565 /*
566 * It might be possible not all queues have a dedicated endpoint.
567 * Loop through all TX queues and copy the endpoint information
568 * which we have gathered from already assigned endpoints.
569 */
570 txall_queue_for_each(rt2x00dev, queue) {
571 if (!queue->usb_endpoint)
572 rt2x00usb_assign_endpoint(queue, tx_ep_desc);
573 }
574
575 return 0;
576}
577
Ivo van Doornfa695602010-10-11 15:37:25 +0200578static int rt2x00usb_alloc_entries(struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700579{
Ivo van Doornfa695602010-10-11 15:37:25 +0200580 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200581 struct queue_entry_priv_usb *entry_priv;
582 struct queue_entry_priv_usb_bcn *bcn_priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700583 unsigned int i;
584
Ivo van Doorn181d6902008-02-05 16:42:23 -0500585 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200586 entry_priv = queue->entries[i].priv_data;
587 entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
588 if (!entry_priv->urb)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700589 return -ENOMEM;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200590 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500591
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200592 /*
593 * If this is not the beacon queue or
594 * no guardian byte was required for the beacon,
595 * then we are done.
596 */
Ivo van Doornfa695602010-10-11 15:37:25 +0200597 if (queue->qid != QID_BEACON ||
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200598 !test_bit(REQUIRE_BEACON_GUARD, &rt2x00dev->cap_flags))
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200599 return 0;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500600
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200601 for (i = 0; i < queue->limit; i++) {
602 bcn_priv = queue->entries[i].priv_data;
603 bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
604 if (!bcn_priv->guardian_urb)
605 return -ENOMEM;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700606 }
607
608 return 0;
609}
610
Ivo van Doornfa695602010-10-11 15:37:25 +0200611static void rt2x00usb_free_entries(struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700612{
Ivo van Doornfa695602010-10-11 15:37:25 +0200613 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200614 struct queue_entry_priv_usb *entry_priv;
615 struct queue_entry_priv_usb_bcn *bcn_priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700616 unsigned int i;
617
Ivo van Doorn181d6902008-02-05 16:42:23 -0500618 if (!queue->entries)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700619 return;
620
Ivo van Doorn181d6902008-02-05 16:42:23 -0500621 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200622 entry_priv = queue->entries[i].priv_data;
623 usb_kill_urb(entry_priv->urb);
624 usb_free_urb(entry_priv->urb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700625 }
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200626
627 /*
628 * If this is not the beacon queue or
629 * no guardian byte was required for the beacon,
630 * then we are done.
631 */
Ivo van Doornfa695602010-10-11 15:37:25 +0200632 if (queue->qid != QID_BEACON ||
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200633 !test_bit(REQUIRE_BEACON_GUARD, &rt2x00dev->cap_flags))
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200634 return;
635
636 for (i = 0; i < queue->limit; i++) {
637 bcn_priv = queue->entries[i].priv_data;
638 usb_kill_urb(bcn_priv->guardian_urb);
639 usb_free_urb(bcn_priv->guardian_urb);
640 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700641}
642
643int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
644{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500645 struct data_queue *queue;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200646 int status;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700647
648 /*
Ivo van Doornf1ca2162008-11-13 23:07:33 +0100649 * Find endpoints for each queue
650 */
651 status = rt2x00usb_find_endpoints(rt2x00dev);
652 if (status)
653 goto exit;
654
655 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700656 * Allocate DMA
657 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500658 queue_for_each(rt2x00dev, queue) {
Ivo van Doornfa695602010-10-11 15:37:25 +0200659 status = rt2x00usb_alloc_entries(queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700660 if (status)
661 goto exit;
662 }
663
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700664 return 0;
665
666exit:
667 rt2x00usb_uninitialize(rt2x00dev);
668
669 return status;
670}
671EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
672
673void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
674{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500675 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700676
Ivo van Doorn181d6902008-02-05 16:42:23 -0500677 queue_for_each(rt2x00dev, queue)
Ivo van Doornfa695602010-10-11 15:37:25 +0200678 rt2x00usb_free_entries(queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700679}
680EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
681
682/*
683 * USB driver handlers.
684 */
685static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
686{
687 kfree(rt2x00dev->rf);
688 rt2x00dev->rf = NULL;
689
690 kfree(rt2x00dev->eeprom);
691 rt2x00dev->eeprom = NULL;
692
Ivo van Doorn21795092008-02-10 22:49:13 +0100693 kfree(rt2x00dev->csr.cache);
694 rt2x00dev->csr.cache = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700695}
696
697static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
698{
Ivo van Doorn21795092008-02-10 22:49:13 +0100699 rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
700 if (!rt2x00dev->csr.cache)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700701 goto exit;
702
703 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
704 if (!rt2x00dev->eeprom)
705 goto exit;
706
707 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
708 if (!rt2x00dev->rf)
709 goto exit;
710
711 return 0;
712
713exit:
714 ERROR_PROBE("Failed to allocate registers.\n");
715
716 rt2x00usb_free_reg(rt2x00dev);
717
718 return -ENOMEM;
719}
720
721int rt2x00usb_probe(struct usb_interface *usb_intf,
722 const struct usb_device_id *id)
723{
724 struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
725 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_info;
726 struct ieee80211_hw *hw;
727 struct rt2x00_dev *rt2x00dev;
728 int retval;
729
730 usb_dev = usb_get_dev(usb_dev);
731
732 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
733 if (!hw) {
734 ERROR_PROBE("Failed to allocate hardware.\n");
735 retval = -ENOMEM;
736 goto exit_put_device;
737 }
738
739 usb_set_intfdata(usb_intf, hw);
740
741 rt2x00dev = hw->priv;
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200742 rt2x00dev->dev = &usb_intf->dev;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700743 rt2x00dev->ops = ops;
744 rt2x00dev->hw = hw;
745
Gertjan van Wingerde2015d192009-11-08 12:30:14 +0100746 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_USB);
747
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200748 INIT_WORK(&rt2x00dev->rxdone_work, rt2x00usb_work_rxdone);
749 INIT_WORK(&rt2x00dev->txdone_work, rt2x00usb_work_txdone);
750
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700751 retval = rt2x00usb_alloc_reg(rt2x00dev);
752 if (retval)
753 goto exit_free_device;
754
755 retval = rt2x00lib_probe_dev(rt2x00dev);
756 if (retval)
757 goto exit_free_reg;
758
759 return 0;
760
761exit_free_reg:
762 rt2x00usb_free_reg(rt2x00dev);
763
764exit_free_device:
765 ieee80211_free_hw(hw);
766
767exit_put_device:
768 usb_put_dev(usb_dev);
769
770 usb_set_intfdata(usb_intf, NULL);
771
772 return retval;
773}
774EXPORT_SYMBOL_GPL(rt2x00usb_probe);
775
776void rt2x00usb_disconnect(struct usb_interface *usb_intf)
777{
778 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
779 struct rt2x00_dev *rt2x00dev = hw->priv;
780
781 /*
782 * Free all allocated data.
783 */
784 rt2x00lib_remove_dev(rt2x00dev);
785 rt2x00usb_free_reg(rt2x00dev);
786 ieee80211_free_hw(hw);
787
788 /*
789 * Free the USB device data.
790 */
791 usb_set_intfdata(usb_intf, NULL);
792 usb_put_dev(interface_to_usbdev(usb_intf));
793}
794EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
795
796#ifdef CONFIG_PM
797int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
798{
799 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
800 struct rt2x00_dev *rt2x00dev = hw->priv;
801 int retval;
802
803 retval = rt2x00lib_suspend(rt2x00dev, state);
804 if (retval)
805 return retval;
806
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700807 /*
808 * Decrease usbdev refcount.
809 */
810 usb_put_dev(interface_to_usbdev(usb_intf));
811
812 return 0;
813}
814EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
815
816int rt2x00usb_resume(struct usb_interface *usb_intf)
817{
818 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
819 struct rt2x00_dev *rt2x00dev = hw->priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700820
821 usb_get_dev(interface_to_usbdev(usb_intf));
822
Ivo van Doorn499a2142009-03-28 20:51:58 +0100823 return rt2x00lib_resume(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700824}
825EXPORT_SYMBOL_GPL(rt2x00usb_resume);
826#endif /* CONFIG_PM */
827
828/*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500829 * rt2x00usb module information.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700830 */
831MODULE_AUTHOR(DRV_PROJECT);
832MODULE_VERSION(DRV_VERSION);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500833MODULE_DESCRIPTION("rt2x00 usb library");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700834MODULE_LICENSE("GPL");