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