blob: 552f0e94f800d321eeff96ae54bbdc4229ea43ab [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn811aa9c2008-02-03 15:42:53 +01002 Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
Ivo van Doorn95ea3622007-09-25 17:57:13 -07003 <http://rt2x00.serialmonkey.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the
17 Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21/*
22 Module: rt2x00usb
23 Abstract: rt2x00 generic usb device routines.
24 */
25
Ivo van Doorn95ea3622007-09-25 17:57:13 -070026#include <linux/kernel.h>
27#include <linux/module.h>
28#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{
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +020043 struct usb_device *usb_dev =
44 interface_to_usbdev(to_usb_interface(rt2x00dev->dev));
Ivo van Doorn95ea3622007-09-25 17:57:13 -070045 int status;
46 unsigned int i;
47 unsigned int pipe =
48 (requesttype == USB_VENDOR_REQUEST_IN) ?
49 usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
50
Adam Baker3d823462007-10-27 13:43:29 +020051
Ivo van Doorn95ea3622007-09-25 17:57:13 -070052 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
53 status = usb_control_msg(usb_dev, pipe, request, requesttype,
54 value, offset, buffer, buffer_length,
55 timeout);
56 if (status >= 0)
57 return 0;
58
59 /*
Ivo van Doorne9136552007-09-25 20:54:20 +020060 * Check for errors
Ivo van Doorn95ea3622007-09-25 17:57:13 -070061 * -ENODEV: Device has disappeared, no point continuing.
Ivo van Doorne9136552007-09-25 20:54:20 +020062 * All other errors: Try again.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070063 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -070064 else if (status == -ENODEV)
65 break;
66 }
67
68 ERROR(rt2x00dev,
69 "Vendor Request 0x%02x failed for offset 0x%04x with error %d.\n",
70 request, offset, status);
71
72 return status;
73}
74EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
75
Adam Baker3d823462007-10-27 13:43:29 +020076int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
77 const u8 request, const u8 requesttype,
78 const u16 offset, void *buffer,
79 const u16 buffer_length, const int timeout)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070080{
81 int status;
82
Adam Baker3d823462007-10-27 13:43:29 +020083 BUG_ON(!mutex_is_locked(&rt2x00dev->usb_cache_mutex));
84
Ivo van Doorn95ea3622007-09-25 17:57:13 -070085 /*
86 * Check for Cache availability.
87 */
Ivo van Doorn21795092008-02-10 22:49:13 +010088 if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -070089 ERROR(rt2x00dev, "CSR cache not available.\n");
90 return -ENOMEM;
91 }
92
93 if (requesttype == USB_VENDOR_REQUEST_OUT)
Ivo van Doorn21795092008-02-10 22:49:13 +010094 memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070095
96 status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
Ivo van Doorn21795092008-02-10 22:49:13 +010097 offset, 0, rt2x00dev->csr.cache,
Ivo van Doorn95ea3622007-09-25 17:57:13 -070098 buffer_length, timeout);
99
100 if (!status && requesttype == USB_VENDOR_REQUEST_IN)
Ivo van Doorn21795092008-02-10 22:49:13 +0100101 memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700102
103 return status;
104}
Adam Baker3d823462007-10-27 13:43:29 +0200105EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
106
107int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
108 const u8 request, const u8 requesttype,
109 const u16 offset, void *buffer,
110 const u16 buffer_length, const int timeout)
111{
112 int status;
113
114 mutex_lock(&rt2x00dev->usb_cache_mutex);
115
116 status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
117 requesttype, offset, buffer,
118 buffer_length, timeout);
119
120 mutex_unlock(&rt2x00dev->usb_cache_mutex);
121
122 return status;
123}
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700124EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
125
126/*
127 * TX data handlers.
128 */
129static void rt2x00usb_interrupt_txdone(struct urb *urb)
130{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500131 struct queue_entry *entry = (struct queue_entry *)urb->context;
132 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500133 struct txdone_entry_desc txdesc;
Johannes Berge039fa42008-05-15 12:55:29 +0200134 enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700135
136 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
Ivo van Doorn181d6902008-02-05 16:42:23 -0500137 !__test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700138 return;
139
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700140 /*
141 * Remove the descriptor data from the buffer.
142 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500143 skb_pull(entry->skb, entry->queue->desc_size);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700144
145 /*
146 * Obtain the status about this packet.
Ivo van Doornfb55f4d2008-05-10 13:42:06 +0200147 * Note that when the status is 0 it does not mean the
148 * frame was send out correctly. It only means the frame
149 * was succesfully pushed to the hardware, we have no
150 * way to determine the transmission status right now.
151 * (Only indirectly by looking at the failed TX counters
152 * in the register).
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700153 */
Ivo van Doornfb55f4d2008-05-10 13:42:06 +0200154 if (!urb->status)
155 __set_bit(TXDONE_UNKNOWN, &txdesc.flags);
156 else
157 __set_bit(TXDONE_FAILURE, &txdesc.flags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500158 txdesc.retry = 0;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700159
Ivo van Doorn181d6902008-02-05 16:42:23 -0500160 rt2x00lib_txdone(entry, &txdesc);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700161
162 /*
163 * Make this entry available for reuse.
164 */
165 entry->flags = 0;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500166 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700167
168 /*
Ivo van Doornb8697672008-06-06 22:53:14 +0200169 * If the data queue was below the threshold before the txdone
170 * handler we must make sure the packet queue in the mac80211 stack
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700171 * is reenabled when the txdone handler has finished.
172 */
Ivo van Doornb8697672008-06-06 22:53:14 +0200173 if (!rt2x00queue_threshold(entry->queue))
Johannes Berge039fa42008-05-15 12:55:29 +0200174 ieee80211_wake_queue(rt2x00dev->hw, qid);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700175}
176
Ivo van Doorn6db37862008-06-06 22:50:28 +0200177int rt2x00usb_write_tx_data(struct queue_entry *entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700178{
Ivo van Doorn6db37862008-06-06 22:50:28 +0200179 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200180 struct usb_device *usb_dev =
181 interface_to_usbdev(to_usb_interface(rt2x00dev->dev));
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200182 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500183 struct skb_frame_desc *skbdesc;
Ivo van Doorndd9fa2d2007-10-06 14:15:46 +0200184 u32 length;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700185
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200186 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700187 * Add the descriptor in front of the skb.
188 */
Ivo van Doorn6db37862008-06-06 22:50:28 +0200189 skb_push(entry->skb, entry->queue->desc_size);
190 memset(entry->skb->data, 0, entry->queue->desc_size);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700191
Ivo van Doorn08992f72008-01-24 01:56:25 -0800192 /*
193 * Fill in skb descriptor
194 */
Ivo van Doorn6db37862008-06-06 22:50:28 +0200195 skbdesc = get_skb_frame_desc(entry->skb);
Johannes Berge039fa42008-05-15 12:55:29 +0200196 memset(skbdesc, 0, sizeof(*skbdesc));
Ivo van Doorn6db37862008-06-06 22:50:28 +0200197 skbdesc->desc = entry->skb->data;
198 skbdesc->desc_len = entry->queue->desc_size;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500199 skbdesc->entry = entry;
Ivo van Doorn08992f72008-01-24 01:56:25 -0800200
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700201 /*
Ivo van Doorndd9fa2d2007-10-06 14:15:46 +0200202 * USB devices cannot blindly pass the skb->len as the
203 * length of the data to usb_fill_bulk_urb. Pass the skb
204 * to the driver to determine what the length should be.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700205 */
Ivo van Doorn6db37862008-06-06 22:50:28 +0200206 length = rt2x00dev->ops->lib->get_tx_data_len(rt2x00dev, entry->skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700207
Ivo van Doorn6db37862008-06-06 22:50:28 +0200208 usb_fill_bulk_urb(entry_priv->urb, usb_dev,
209 usb_sndbulkpipe(usb_dev, 1),
210 entry->skb->data, length,
211 rt2x00usb_interrupt_txdone, entry);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700212
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700213 return 0;
214}
215EXPORT_SYMBOL_GPL(rt2x00usb_write_tx_data);
216
Ivo van Doornf019d512008-06-06 22:47:39 +0200217static inline void rt2x00usb_kick_tx_entry(struct queue_entry *entry)
218{
219 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
220
221 if (__test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags))
222 usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
223}
224
225void rt2x00usb_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
226 const enum data_queue_qid qid)
227{
228 struct data_queue *queue = rt2x00queue_get_queue(rt2x00dev, qid);
229 unsigned long irqflags;
230 unsigned int index;
231 unsigned int index_done;
232 unsigned int i;
233
234 /*
235 * Only protect the range we are going to loop over,
236 * if during our loop a extra entry is set to pending
237 * it should not be kicked during this run, since it
238 * is part of another TX operation.
239 */
240 spin_lock_irqsave(&queue->lock, irqflags);
241 index = queue->index[Q_INDEX];
242 index_done = queue->index[Q_INDEX_DONE];
243 spin_unlock_irqrestore(&queue->lock, irqflags);
244
245 /*
246 * Start from the TX done pointer, this guarentees that we will
247 * send out all frames in the correct order.
248 */
249 if (index_done < index) {
250 for (i = index_done; i < index; i++)
251 rt2x00usb_kick_tx_entry(&queue->entries[i]);
252 } else {
253 for (i = index_done; i < queue->limit; i++)
254 rt2x00usb_kick_tx_entry(&queue->entries[i]);
255
256 for (i = 0; i < index; i++)
257 rt2x00usb_kick_tx_entry(&queue->entries[i]);
258 }
259}
260EXPORT_SYMBOL_GPL(rt2x00usb_kick_tx_queue);
261
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700262/*
263 * RX data handlers.
264 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500265static void rt2x00usb_interrupt_rxdone(struct urb *urb)
266{
267 struct queue_entry *entry = (struct queue_entry *)urb->context;
268 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200269 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Gertjan van Wingerdea26cbc62008-06-06 22:54:28 +0200270 u8 rxd[32];
Ivo van Doorn181d6902008-02-05 16:42:23 -0500271
272 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
273 !test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
274 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700275
276 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500277 * Check if the received data is simply too small
278 * to be actually valid, or if the urb is signaling
279 * a problem.
Ivo van Doorn08992f72008-01-24 01:56:25 -0800280 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500281 if (urb->actual_length < entry->queue->desc_size || urb->status)
282 goto skip_entry;
283
284 /*
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200285 * Fill in desc fields of the skb descriptor
Ivo van Doorn181d6902008-02-05 16:42:23 -0500286 */
Gertjan van Wingerdea26cbc62008-06-06 22:54:28 +0200287 skbdesc->desc = rxd;
288 skbdesc->desc_len = entry->queue->desc_size;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500289
Ivo van Doorn08992f72008-01-24 01:56:25 -0800290 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700291 * Send the frame to rt2x00lib for further processing.
292 */
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200293 rt2x00lib_rxdone(rt2x00dev, entry);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700294
295 /*
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200296 * Reinitialize the urb.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700297 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700298 urb->transfer_buffer = entry->skb->data;
299 urb->transfer_buffer_length = entry->skb->len;
300
301skip_entry:
Ivo van Doorn181d6902008-02-05 16:42:23 -0500302 if (test_bit(DEVICE_ENABLED_RADIO, &entry->queue->rt2x00dev->flags)) {
303 __set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700304 usb_submit_urb(urb, GFP_ATOMIC);
305 }
306
Ivo van Doorn181d6902008-02-05 16:42:23 -0500307 rt2x00queue_index_inc(entry->queue, Q_INDEX);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700308}
309
310/*
311 * Radio handlers
312 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700313void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
314{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200315 struct queue_entry_priv_usb *entry_priv;
316 struct queue_entry_priv_usb_bcn *bcn_priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700317 unsigned int i;
318
Ivo van Doornbd394a72008-04-21 19:01:58 +0200319 rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700320 REGISTER_TIMEOUT);
321
322 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500323 * Cancel all queues.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700324 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500325 for (i = 0; i < rt2x00dev->rx->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200326 entry_priv = rt2x00dev->rx->entries[i].priv_data;
327 usb_kill_urb(entry_priv->urb);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500328 }
329
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200330 /*
Ivo van Doornedfa78b2008-06-03 20:29:50 +0200331 * Kill guardian urb (if required by driver).
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200332 */
Ivo van Doornedfa78b2008-06-03 20:29:50 +0200333 if (!test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
334 return;
335
Ivo van Doorn330e3f92008-02-10 22:52:36 +0100336 for (i = 0; i < rt2x00dev->bcn->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200337 bcn_priv = rt2x00dev->bcn->entries[i].priv_data;
338 if (bcn_priv->guardian_urb)
339 usb_kill_urb(bcn_priv->guardian_urb);
Ivo van Doorn330e3f92008-02-10 22:52:36 +0100340 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700341}
342EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
343
344/*
345 * Device initialization handlers.
346 */
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100347void rt2x00usb_init_rxentry(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn181d6902008-02-05 16:42:23 -0500348 struct queue_entry *entry)
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100349{
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200350 struct usb_device *usb_dev =
351 interface_to_usbdev(to_usb_interface(rt2x00dev->dev));
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200352 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100353
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200354 usb_fill_bulk_urb(entry_priv->urb, usb_dev,
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100355 usb_rcvbulkpipe(usb_dev, 1),
356 entry->skb->data, entry->skb->len,
357 rt2x00usb_interrupt_rxdone, entry);
358
Ivo van Doorn181d6902008-02-05 16:42:23 -0500359 __set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200360 usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100361}
362EXPORT_SYMBOL_GPL(rt2x00usb_init_rxentry);
363
364void rt2x00usb_init_txentry(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn181d6902008-02-05 16:42:23 -0500365 struct queue_entry *entry)
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100366{
367 entry->flags = 0;
368}
369EXPORT_SYMBOL_GPL(rt2x00usb_init_txentry);
370
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700371static int rt2x00usb_alloc_urb(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn181d6902008-02-05 16:42:23 -0500372 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700373{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200374 struct queue_entry_priv_usb *entry_priv;
375 struct queue_entry_priv_usb_bcn *bcn_priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700376 unsigned int i;
377
Ivo van Doorn181d6902008-02-05 16:42:23 -0500378 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200379 entry_priv = queue->entries[i].priv_data;
380 entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
381 if (!entry_priv->urb)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700382 return -ENOMEM;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200383 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500384
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200385 /*
386 * If this is not the beacon queue or
387 * no guardian byte was required for the beacon,
388 * then we are done.
389 */
390 if (rt2x00dev->bcn != queue ||
391 !test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
392 return 0;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500393
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200394 for (i = 0; i < queue->limit; i++) {
395 bcn_priv = queue->entries[i].priv_data;
396 bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
397 if (!bcn_priv->guardian_urb)
398 return -ENOMEM;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700399 }
400
401 return 0;
402}
403
404static void rt2x00usb_free_urb(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn181d6902008-02-05 16:42:23 -0500405 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700406{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200407 struct queue_entry_priv_usb *entry_priv;
408 struct queue_entry_priv_usb_bcn *bcn_priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700409 unsigned int i;
410
Ivo van Doorn181d6902008-02-05 16:42:23 -0500411 if (!queue->entries)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700412 return;
413
Ivo van Doorn181d6902008-02-05 16:42:23 -0500414 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200415 entry_priv = queue->entries[i].priv_data;
416 usb_kill_urb(entry_priv->urb);
417 usb_free_urb(entry_priv->urb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700418 }
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200419
420 /*
421 * If this is not the beacon queue or
422 * no guardian byte was required for the beacon,
423 * then we are done.
424 */
425 if (rt2x00dev->bcn != queue ||
426 !test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
427 return;
428
429 for (i = 0; i < queue->limit; i++) {
430 bcn_priv = queue->entries[i].priv_data;
431 usb_kill_urb(bcn_priv->guardian_urb);
432 usb_free_urb(bcn_priv->guardian_urb);
433 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700434}
435
436int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
437{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500438 struct data_queue *queue;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200439 int status;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700440
441 /*
442 * Allocate DMA
443 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500444 queue_for_each(rt2x00dev, queue) {
445 status = rt2x00usb_alloc_urb(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700446 if (status)
447 goto exit;
448 }
449
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700450 return 0;
451
452exit:
453 rt2x00usb_uninitialize(rt2x00dev);
454
455 return status;
456}
457EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
458
459void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
460{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500461 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700462
Ivo van Doorn181d6902008-02-05 16:42:23 -0500463 queue_for_each(rt2x00dev, queue)
464 rt2x00usb_free_urb(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700465}
466EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
467
468/*
469 * USB driver handlers.
470 */
471static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
472{
473 kfree(rt2x00dev->rf);
474 rt2x00dev->rf = NULL;
475
476 kfree(rt2x00dev->eeprom);
477 rt2x00dev->eeprom = NULL;
478
Ivo van Doorn21795092008-02-10 22:49:13 +0100479 kfree(rt2x00dev->csr.cache);
480 rt2x00dev->csr.cache = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700481}
482
483static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
484{
Ivo van Doorn21795092008-02-10 22:49:13 +0100485 rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
486 if (!rt2x00dev->csr.cache)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700487 goto exit;
488
489 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
490 if (!rt2x00dev->eeprom)
491 goto exit;
492
493 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
494 if (!rt2x00dev->rf)
495 goto exit;
496
497 return 0;
498
499exit:
500 ERROR_PROBE("Failed to allocate registers.\n");
501
502 rt2x00usb_free_reg(rt2x00dev);
503
504 return -ENOMEM;
505}
506
507int rt2x00usb_probe(struct usb_interface *usb_intf,
508 const struct usb_device_id *id)
509{
510 struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
511 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_info;
512 struct ieee80211_hw *hw;
513 struct rt2x00_dev *rt2x00dev;
514 int retval;
515
516 usb_dev = usb_get_dev(usb_dev);
517
518 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
519 if (!hw) {
520 ERROR_PROBE("Failed to allocate hardware.\n");
521 retval = -ENOMEM;
522 goto exit_put_device;
523 }
524
525 usb_set_intfdata(usb_intf, hw);
526
527 rt2x00dev = hw->priv;
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200528 rt2x00dev->dev = &usb_intf->dev;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700529 rt2x00dev->ops = ops;
530 rt2x00dev->hw = hw;
Adam Baker3d823462007-10-27 13:43:29 +0200531 mutex_init(&rt2x00dev->usb_cache_mutex);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700532
Ivo van Doornb242e892007-11-15 23:41:31 +0100533 rt2x00dev->usb_maxpacket =
534 usb_maxpacket(usb_dev, usb_sndbulkpipe(usb_dev, 1), 1);
535 if (!rt2x00dev->usb_maxpacket)
536 rt2x00dev->usb_maxpacket = 1;
537
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700538 retval = rt2x00usb_alloc_reg(rt2x00dev);
539 if (retval)
540 goto exit_free_device;
541
542 retval = rt2x00lib_probe_dev(rt2x00dev);
543 if (retval)
544 goto exit_free_reg;
545
546 return 0;
547
548exit_free_reg:
549 rt2x00usb_free_reg(rt2x00dev);
550
551exit_free_device:
552 ieee80211_free_hw(hw);
553
554exit_put_device:
555 usb_put_dev(usb_dev);
556
557 usb_set_intfdata(usb_intf, NULL);
558
559 return retval;
560}
561EXPORT_SYMBOL_GPL(rt2x00usb_probe);
562
563void rt2x00usb_disconnect(struct usb_interface *usb_intf)
564{
565 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
566 struct rt2x00_dev *rt2x00dev = hw->priv;
567
568 /*
569 * Free all allocated data.
570 */
571 rt2x00lib_remove_dev(rt2x00dev);
572 rt2x00usb_free_reg(rt2x00dev);
573 ieee80211_free_hw(hw);
574
575 /*
576 * Free the USB device data.
577 */
578 usb_set_intfdata(usb_intf, NULL);
579 usb_put_dev(interface_to_usbdev(usb_intf));
580}
581EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
582
583#ifdef CONFIG_PM
584int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
585{
586 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
587 struct rt2x00_dev *rt2x00dev = hw->priv;
588 int retval;
589
590 retval = rt2x00lib_suspend(rt2x00dev, state);
591 if (retval)
592 return retval;
593
594 rt2x00usb_free_reg(rt2x00dev);
595
596 /*
597 * Decrease usbdev refcount.
598 */
599 usb_put_dev(interface_to_usbdev(usb_intf));
600
601 return 0;
602}
603EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
604
605int rt2x00usb_resume(struct usb_interface *usb_intf)
606{
607 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
608 struct rt2x00_dev *rt2x00dev = hw->priv;
609 int retval;
610
611 usb_get_dev(interface_to_usbdev(usb_intf));
612
613 retval = rt2x00usb_alloc_reg(rt2x00dev);
614 if (retval)
615 return retval;
616
617 retval = rt2x00lib_resume(rt2x00dev);
618 if (retval)
619 goto exit_free_reg;
620
621 return 0;
622
623exit_free_reg:
624 rt2x00usb_free_reg(rt2x00dev);
625
626 return retval;
627}
628EXPORT_SYMBOL_GPL(rt2x00usb_resume);
629#endif /* CONFIG_PM */
630
631/*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500632 * rt2x00usb module information.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700633 */
634MODULE_AUTHOR(DRV_PROJECT);
635MODULE_VERSION(DRV_VERSION);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500636MODULE_DESCRIPTION("rt2x00 usb library");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700637MODULE_LICENSE("GPL");