6b193d023c8e26879e07211467faa21c831dee1f
[openwrt/svn-archive/archive.git] / package / rt2x00 / src / rt2x00usb.c
1 /*
2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <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 Supported chipsets: rt2570, rt2571W & rt2671.
25 */
26
27 /*
28 * Set enviroment defines for rt2x00.h
29 */
30 #define DRV_NAME "rt2x00usb"
31
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/version.h>
35 #include <linux/init.h>
36 #include <linux/usb.h>
37
38 #include "rt2x00.h"
39 #include "rt2x00usb.h"
40
41 /*
42 * Interfacing with the HW.
43 */
44 int rt2x00usb_vendor_request(const struct rt2x00_dev *rt2x00dev,
45 const u8 request, const u8 type, const u16 offset,
46 u32 value, void *buffer, const u16 buffer_length, const u16 timeout)
47 {
48 struct usb_device *usb_dev = interface_to_usbdev(
49 rt2x00dev_usb(rt2x00dev));
50 int status;
51 unsigned int i;
52
53 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
54 status = usb_control_msg(
55 usb_dev,
56 (type == USB_VENDOR_REQUEST_IN) ?
57 usb_rcvctrlpipe(usb_dev, 0) :
58 usb_sndctrlpipe(usb_dev, 0),
59 request, type, value, offset, buffer, buffer_length,
60 timeout);
61 if (status >= 0)
62 return 0;
63 }
64
65 ERROR(rt2x00dev, "vendor request error. Request 0x%02x failed "
66 "for offset 0x%04x with error %d.\n", request, offset, status);
67
68 return status;
69 }
70 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
71
72 /*
73 * Radio handlers
74 */
75 void rt2x00usb_enable_radio(struct rt2x00_dev *rt2x00dev)
76 {
77 unsigned int i;
78
79 /*
80 * Start the RX ring.
81 */
82 for (i = 0; i < rt2x00dev->rx->stats.limit; i++) {
83 __set_bit(ENTRY_OWNER_NIC, &rt2x00dev->rx->entry[i].flags);
84 usb_submit_urb(rt2x00dev->rx->entry[i].priv, GFP_ATOMIC);
85 }
86 }
87 EXPORT_SYMBOL_GPL(rt2x00usb_enable_radio);
88
89 void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
90 {
91 struct data_ring *ring;
92 unsigned int i;
93
94 rt2x00usb_vendor_request(rt2x00dev, USB_RX_CONTROL,
95 USB_VENDOR_REQUEST_OUT, 0x00, 0x00, NULL, 0, REGISTER_TIMEOUT);
96
97 /*
98 * Cancel all rings.
99 */
100 ring_for_each(rt2x00dev, ring) {
101 for (i = 0; i < ring->stats.limit; i++)
102 usb_kill_urb(ring->entry[i].priv);
103 }
104 }
105 EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
106
107 /*
108 * Beacon handlers.
109 */
110 int rt2x00usb_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
111 struct ieee80211_tx_control *control)
112 {
113 struct rt2x00_dev *rt2x00dev = hw->priv;
114 struct usb_device *usb_dev =
115 interface_to_usbdev(rt2x00dev_usb(rt2x00dev));
116 struct data_ring *ring =
117 rt2x00_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
118 struct data_entry *beacon;
119 struct data_entry *guardian;
120 int length;
121
122 /*
123 * Just in case the ieee80211 doesn't set this,
124 * but we need this queue set for the descriptor
125 * initialization.
126 */
127 control->queue = IEEE80211_TX_QUEUE_BEACON;
128
129 /*
130 * Obtain 2 entries, one for the guardian byte,
131 * the second for the actual beacon.
132 */
133 guardian = rt2x00_get_data_entry(ring);
134 rt2x00_ring_index_inc(ring);
135 beacon = rt2x00_get_data_entry(ring);
136
137 /*
138 * First we create the beacon.
139 */
140 skb_push(skb, ring->desc_size);
141 rt2x00lib_write_tx_desc(rt2x00dev, beacon,
142 (struct data_desc*)skb->data,
143 (struct ieee80211_hdr*)(skb->data + ring->desc_size),
144 skb->len - ring->desc_size,
145 control);
146
147 /*
148 * Length passed to usb_fill_urb cannot be an odd number,
149 * so add 1 byte to make it even.
150 */
151 length = skb->len;
152 if (length % 2)
153 length++;
154
155 usb_fill_bulk_urb(
156 beacon->priv,
157 usb_dev,
158 usb_sndbulkpipe(usb_dev, 1),
159 skb->data,
160 length,
161 rt2x00usb_beacondone,
162 beacon);
163
164 beacon->skb = skb;
165
166 /*
167 * Second we need to create the guardian byte.
168 * We only need a single byte, so lets recycle
169 * the 'flags' field we are not using for beacons.
170 */
171 guardian->flags = 0;
172 usb_fill_bulk_urb(
173 guardian->priv,
174 usb_dev,
175 usb_sndbulkpipe(usb_dev, 1),
176 &guardian->flags,
177 1,
178 rt2x00usb_beacondone,
179 guardian);
180
181 /*
182 * Send out the guardian byte.
183 */
184 usb_submit_urb(guardian->priv, GFP_ATOMIC);
185
186 /*
187 * Enable beacon generation.
188 */
189 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
190
191 return 0;
192 }
193 EXPORT_SYMBOL_GPL(rt2x00usb_beacon_update);
194
195 void rt2x00usb_beacondone(struct urb *urb)
196 {
197 struct data_entry *entry = (struct data_entry*)urb->context;
198 struct data_ring *ring = entry->ring;
199
200 if (!test_bit(DEVICE_ENABLED_RADIO, &ring->rt2x00dev->flags))
201 return;
202
203 /*
204 * Check if this was the guardian beacon,
205 * if that was the case we need to send the real beacon now.
206 * Otherwise we should free the sk_buffer, the device
207 * should be doing the rest of the work now.
208 */
209 if (ring->index == 1) {
210 rt2x00_ring_index_done_inc(ring);
211 entry = rt2x00_get_data_entry(ring);
212 usb_submit_urb(entry->priv, GFP_ATOMIC);
213 rt2x00_ring_index_inc(ring);
214 } else if (ring->index_done == 1) {
215 entry = rt2x00_get_data_entry_done(ring);
216 if (entry->skb) {
217 dev_kfree_skb(entry->skb);
218 entry->skb = NULL;
219 }
220 rt2x00_ring_index_done_inc(ring);
221 }
222 }
223 EXPORT_SYMBOL_GPL(rt2x00usb_beacondone);
224
225 /*
226 * TX data handlers.
227 */
228 static void rt2x00usb_interrupt_txdone(struct urb *urb)
229 {
230 struct data_entry *entry = (struct data_entry*)urb->context;
231 struct data_ring *ring = entry->ring;
232 struct rt2x00_dev *rt2x00dev = ring->rt2x00dev;
233 struct data_desc *txd = (struct data_desc *)entry->skb->data;
234 u32 word;
235 int tx_status;
236
237 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
238 !__test_and_clear_bit(ENTRY_OWNER_NIC, &entry->flags))
239 return;
240
241 rt2x00_desc_read(txd, 0, &word);
242
243 /*
244 * Remove the descriptor data from the buffer.
245 */
246 skb_pull(entry->skb, ring->desc_size);
247
248 /*
249 * Obtain the status about this packet.
250 */
251 tx_status = !urb->status ? TX_SUCCESS : TX_FAIL_RETRY;
252
253 rt2x00lib_txdone(entry, tx_status, 0);
254
255 /*
256 * Make this entry available for reuse.
257 */
258 entry->flags = 0;
259 rt2x00_ring_index_done_inc(entry->ring);
260
261 /*
262 * If the data ring was full before the txdone handler
263 * we must make sure the packet queue in the mac80211 stack
264 * is reenabled when the txdone handler has finished.
265 */
266 if (!rt2x00_ring_full(ring))
267 ieee80211_wake_queue(rt2x00dev->hw,
268 entry->tx_status.control.queue);
269 }
270
271 int rt2x00usb_write_tx_data(struct rt2x00_dev *rt2x00dev,
272 struct data_ring *ring, struct sk_buff *skb,
273 struct ieee80211_tx_control *control)
274 {
275 struct usb_device *usb_dev =
276 interface_to_usbdev(rt2x00dev_usb(rt2x00dev));
277 struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr*)skb->data;
278 struct data_entry *entry = rt2x00_get_data_entry(ring);
279 struct data_desc *txd;
280 u32 length = skb->len;
281
282 if (rt2x00_ring_full(ring)) {
283 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
284 return -EINVAL;
285 }
286
287 if (test_bit(ENTRY_OWNER_NIC, &entry->flags)) {
288 ERROR(rt2x00dev,
289 "Arrived at non-free entry in the non-full queue %d.\n"
290 "Please file bug report to %s.\n",
291 control->queue, DRV_PROJECT);
292 ieee80211_stop_queue( rt2x00dev->hw, control->queue);
293 return -EINVAL;
294 }
295
296 skb_push(skb, rt2x00dev->hw->extra_tx_headroom);
297 txd = (struct data_desc*)skb->data;
298 rt2x00lib_write_tx_desc(rt2x00dev, entry, txd, ieee80211hdr,
299 length, control);
300 memcpy(&entry->tx_status.control, control, sizeof(*control));
301 entry->skb = skb;
302
303 /*
304 * Length passed to usb_fill_urb cannot be an odd number,
305 * so add 1 byte to make it even.
306 */
307 length += rt2x00dev->hw->extra_tx_headroom;
308 if (length % 2)
309 length++;
310
311 __set_bit(ENTRY_OWNER_NIC, &entry->flags);
312 usb_fill_bulk_urb(
313 entry->priv,
314 usb_dev,
315 usb_sndbulkpipe(usb_dev, 1),
316 skb->data,
317 length,
318 rt2x00usb_interrupt_txdone,
319 entry);
320 usb_submit_urb(entry->priv, GFP_ATOMIC);
321
322 rt2x00_ring_index_inc(ring);
323
324 if (rt2x00_ring_full(ring))
325 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
326
327 return 0;
328 }
329 EXPORT_SYMBOL_GPL(rt2x00usb_write_tx_data);
330
331 /*
332 * Device initialization handlers.
333 */
334 static int rt2x00usb_alloc_ring(struct rt2x00_dev *rt2x00dev,
335 struct data_ring *ring)
336 {
337 unsigned int i;
338
339 /*
340 * Allocate the URB's
341 */
342 for (i = 0; i < ring->stats.limit; i++) {
343 ring->entry[i].priv = usb_alloc_urb(0, GFP_KERNEL);
344 if (!ring->entry[i].priv)
345 return -ENOMEM;
346 }
347
348 return 0;
349 }
350
351 int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
352 {
353 struct data_ring *ring;
354 struct sk_buff *skb;
355 unsigned int entry_size;
356 unsigned int i;
357 int status;
358
359 /*
360 * Allocate DMA
361 */
362 ring_for_each(rt2x00dev, ring) {
363 status = rt2x00usb_alloc_ring(rt2x00dev, ring);
364 if (status)
365 goto exit;
366 }
367
368 /*
369 * For the RX ring, skb's should be allocated.
370 */
371 entry_size = ring->data_size + ring->desc_size;
372 for (i = 0; i < rt2x00dev->rx->stats.limit; i++) {
373 skb = dev_alloc_skb(NET_IP_ALIGN + entry_size);
374 if (!skb)
375 goto exit;
376
377 skb_reserve(skb, NET_IP_ALIGN);
378 skb_put(skb, entry_size);
379
380 rt2x00dev->rx->entry[i].skb = skb;
381 }
382
383 return 0;
384
385 exit:
386 rt2x00usb_uninitialize(rt2x00dev);
387
388 return status;
389 }
390 EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
391
392 void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
393 {
394 struct data_ring *ring;
395 unsigned int i;
396
397 ring_for_each(rt2x00dev, ring) {
398 if (!ring->entry)
399 continue;
400
401 for (i = 0; i < ring->stats.limit; i++) {
402 usb_kill_urb(ring->entry[i].priv);
403 usb_free_urb(ring->entry[i].priv);
404 if (ring->entry[i].skb)
405 kfree_skb(ring->entry[i].skb);
406 }
407 }
408 }
409 EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
410
411 /*
412 * USB driver handlers.
413 */
414 int rt2x00usb_probe(struct usb_interface *usb_intf,
415 const struct usb_device_id *id)
416 {
417 struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
418 struct rt2x00_ops *ops = (struct rt2x00_ops*)id->driver_info;
419 struct ieee80211_hw *hw;
420 struct rt2x00_dev *rt2x00dev;
421 int retval;
422
423 usb_dev = usb_get_dev(usb_dev);
424
425 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
426 if (!hw) {
427 ERROR_PROBE("Failed to allocate hardware.\n");
428 retval = -ENOMEM;
429 goto exit_put_device;
430 }
431
432 usb_set_intfdata(usb_intf, hw);
433
434 rt2x00dev = hw->priv;
435 rt2x00dev->dev = usb_intf;
436 rt2x00dev->device = &usb_intf->dev;
437 rt2x00dev->ops = ops;
438 rt2x00dev->hw = hw;
439
440 retval = rt2x00lib_probe_dev(rt2x00dev);
441 if (retval)
442 goto exit_free_device;
443
444 return 0;
445
446 exit_free_device:
447 ieee80211_free_hw(hw);
448
449 exit_put_device:
450 usb_put_dev(usb_dev);
451
452 usb_set_intfdata(usb_intf, NULL);
453
454 return retval;
455 }
456 EXPORT_SYMBOL_GPL(rt2x00usb_probe);
457
458 void rt2x00usb_disconnect(struct usb_interface *usb_intf)
459 {
460 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
461 struct rt2x00_dev *rt2x00dev = hw->priv;
462
463 /*
464 * Free all allocated data.
465 */
466 rt2x00lib_remove_dev(rt2x00dev);
467 ieee80211_free_hw(hw);
468
469 /*
470 * Free the USB device data.
471 */
472 usb_set_intfdata(usb_intf, NULL);
473 usb_put_dev(interface_to_usbdev(usb_intf));
474 }
475 EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
476
477 #ifdef CONFIG_PM
478 int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
479 {
480 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
481 struct rt2x00_dev *rt2x00dev = hw->priv;
482 int retval;
483
484 retval = rt2x00lib_suspend(rt2x00dev, state);
485 if (retval)
486 return retval;
487
488 /*
489 * Decrease usbdev refcount.
490 */
491 usb_put_dev(interface_to_usbdev(usb_intf));
492
493 return 0;
494 }
495 EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
496
497 int rt2x00usb_resume(struct usb_interface *usb_intf)
498 {
499 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
500 struct rt2x00_dev *rt2x00dev = hw->priv;
501
502 usb_get_dev(interface_to_usbdev(usb_intf));
503
504 return rt2x00lib_resume(rt2x00dev);
505 }
506 EXPORT_SYMBOL_GPL(rt2x00usb_resume);
507 #endif /* CONFIG_PM */
508
509 /*
510 * rt2x00pci module information.
511 */
512 MODULE_AUTHOR(DRV_PROJECT);
513 MODULE_VERSION(DRV_VERSION);
514 MODULE_DESCRIPTION("rt2x00 library");
515 MODULE_LICENSE("GPL");