rt2x00: Add TX/RX frame dumping facility
[cascardo/linux.git] / drivers / net / wireless / rt2x00 / 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  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/usb.h>
29 #include <linux/bug.h>
30
31 #include "rt2x00.h"
32 #include "rt2x00usb.h"
33
34 /*
35  * Interfacing with the HW.
36  */
37 int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
38                              const u8 request, const u8 requesttype,
39                              const u16 offset, const u16 value,
40                              void *buffer, const u16 buffer_length,
41                              const int timeout)
42 {
43         struct usb_device *usb_dev =
44             interface_to_usbdev(rt2x00dev_usb(rt2x00dev));
45         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
51
52         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                 /*
60                  * Check for errors
61                  * -ENODEV: Device has disappeared, no point continuing.
62                  * All other errors: Try again.
63                  */
64                 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 }
74 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
75
76 int 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)
80 {
81         int status;
82
83         BUG_ON(!mutex_is_locked(&rt2x00dev->usb_cache_mutex));
84
85         /*
86          * Check for Cache availability.
87          */
88         if (unlikely(!rt2x00dev->csr_cache || buffer_length > CSR_CACHE_SIZE)) {
89                 ERROR(rt2x00dev, "CSR cache not available.\n");
90                 return -ENOMEM;
91         }
92
93         if (requesttype == USB_VENDOR_REQUEST_OUT)
94                 memcpy(rt2x00dev->csr_cache, buffer, buffer_length);
95
96         status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
97                                           offset, 0, rt2x00dev->csr_cache,
98                                           buffer_length, timeout);
99
100         if (!status && requesttype == USB_VENDOR_REQUEST_IN)
101                 memcpy(buffer, rt2x00dev->csr_cache, buffer_length);
102
103         return status;
104 }
105 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
106
107 int 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 }
124 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
125
126 /*
127  * TX data handlers.
128  */
129 static void rt2x00usb_interrupt_txdone(struct urb *urb)
130 {
131         struct data_entry *entry = (struct data_entry *)urb->context;
132         struct data_ring *ring = entry->ring;
133         struct rt2x00_dev *rt2x00dev = ring->rt2x00dev;
134         __le32 *txd = (__le32 *)entry->skb->data;
135         u32 word;
136         int tx_status;
137
138         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
139             !__test_and_clear_bit(ENTRY_OWNER_NIC, &entry->flags))
140                 return;
141
142         rt2x00_desc_read(txd, 0, &word);
143
144         /*
145          * Remove the descriptor data from the buffer.
146          */
147         skb_pull(entry->skb, ring->desc_size);
148
149         /*
150          * Obtain the status about this packet.
151          */
152         tx_status = !urb->status ? TX_SUCCESS : TX_FAIL_RETRY;
153
154         rt2x00lib_txdone(entry, tx_status, 0);
155
156         /*
157          * Make this entry available for reuse.
158          */
159         entry->flags = 0;
160         rt2x00_ring_index_done_inc(entry->ring);
161
162         /*
163          * If the data ring was full before the txdone handler
164          * we must make sure the packet queue in the mac80211 stack
165          * is reenabled when the txdone handler has finished.
166          */
167         if (!rt2x00_ring_full(ring))
168                 ieee80211_wake_queue(rt2x00dev->hw,
169                                      entry->tx_status.control.queue);
170 }
171
172 int rt2x00usb_write_tx_data(struct rt2x00_dev *rt2x00dev,
173                             struct data_ring *ring, struct sk_buff *skb,
174                             struct ieee80211_tx_control *control)
175 {
176         struct usb_device *usb_dev =
177             interface_to_usbdev(rt2x00dev_usb(rt2x00dev));
178         struct data_entry *entry = rt2x00_get_data_entry(ring);
179         struct skb_desc *desc;
180         u32 length;
181
182         if (rt2x00_ring_full(ring)) {
183                 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
184                 return -EINVAL;
185         }
186
187         if (test_bit(ENTRY_OWNER_NIC, &entry->flags)) {
188                 ERROR(rt2x00dev,
189                       "Arrived at non-free entry in the non-full queue %d.\n"
190                       "Please file bug report to %s.\n",
191                       control->queue, DRV_PROJECT);
192                 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
193                 return -EINVAL;
194         }
195
196         /*
197          * Add the descriptor in front of the skb.
198          */
199         skb_push(skb, ring->desc_size);
200         memset(skb->data, 0, ring->desc_size);
201
202         /*
203          * Fill in skb descriptor
204          */
205         desc = get_skb_desc(skb);
206         desc->desc_len = ring->desc_size;
207         desc->data_len = skb->len - ring->desc_size;
208         desc->desc = skb->data;
209         desc->data = skb->data + ring->desc_size;
210         desc->ring = ring;
211         desc->entry = entry;
212
213         rt2x00lib_write_tx_desc(rt2x00dev, skb, control);
214
215         /*
216          * USB devices cannot blindly pass the skb->len as the
217          * length of the data to usb_fill_bulk_urb. Pass the skb
218          * to the driver to determine what the length should be.
219          */
220         length = rt2x00dev->ops->lib->get_tx_data_len(rt2x00dev, skb);
221
222         /*
223          * Initialize URB and send the frame to the device.
224          */
225         __set_bit(ENTRY_OWNER_NIC, &entry->flags);
226         usb_fill_bulk_urb(entry->priv, usb_dev, usb_sndbulkpipe(usb_dev, 1),
227                           skb->data, length, rt2x00usb_interrupt_txdone, entry);
228         usb_submit_urb(entry->priv, GFP_ATOMIC);
229
230         rt2x00_ring_index_inc(ring);
231
232         if (rt2x00_ring_full(ring))
233                 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
234
235         return 0;
236 }
237 EXPORT_SYMBOL_GPL(rt2x00usb_write_tx_data);
238
239 /*
240  * RX data handlers.
241  */
242 static void rt2x00usb_interrupt_rxdone(struct urb *urb)
243 {
244         struct data_entry *entry = (struct data_entry *)urb->context;
245         struct data_ring *ring = entry->ring;
246         struct rt2x00_dev *rt2x00dev = ring->rt2x00dev;
247         struct sk_buff *skb;
248         struct ieee80211_hdr *hdr;
249         struct skb_desc *skbdesc;
250         struct rxdata_entry_desc desc;
251         int header_size;
252         int frame_size;
253
254         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
255             !test_and_clear_bit(ENTRY_OWNER_NIC, &entry->flags))
256                 return;
257
258         /*
259          * Check if the received data is simply too small
260          * to be actually valid, or if the urb is signaling
261          * a problem.
262          */
263         if (urb->actual_length < entry->ring->desc_size || urb->status)
264                 goto skip_entry;
265
266         memset(&desc, 0, sizeof(desc));
267         rt2x00dev->ops->lib->fill_rxdone(entry, &desc);
268
269         /*
270          * Allocate a new sk buffer to replace the current one.
271          * If allocation fails, we should drop the current frame
272          * so we can recycle the existing sk buffer for the new frame.
273          * As alignment we use 2 and not NET_IP_ALIGN because we need
274          * to be sure we have 2 bytes room in the head. (NET_IP_ALIGN
275          * can be 0 on some hardware). We use these 2 bytes for frame
276          * alignment later, we assume that the chance that
277          * header_size % 4 == 2 is bigger then header_size % 2 == 0
278          * and thus optimize alignment by reserving the 2 bytes in
279          * advance.
280          */
281         frame_size = entry->ring->data_size + entry->ring->desc_size;
282         skb = dev_alloc_skb(frame_size + 2);
283         if (!skb)
284                 goto skip_entry;
285
286         skb_reserve(skb, 2);
287         skb_put(skb, frame_size);
288
289         /*
290          * The data behind the ieee80211 header must be
291          * aligned on a 4 byte boundary.
292          * After that trim the entire buffer down to only
293          * contain the valid frame data excluding the device
294          * descriptor.
295          */
296         hdr = (struct ieee80211_hdr *)entry->skb->data;
297         header_size =
298             ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
299
300         if (header_size % 4 == 0) {
301                 skb_push(entry->skb, 2);
302                 memmove(entry->skb->data, entry->skb->data + 2, skb->len - 2);
303         }
304         skb_trim(entry->skb, desc.size);
305
306         /*
307          * Fill in skb descriptor
308          */
309         skbdesc = get_skb_desc(entry->skb);
310         skbdesc->desc_len = entry->ring->desc_size;
311         skbdesc->data_len = entry->skb->len;
312         skbdesc->desc = entry->skb->data - skbdesc->desc_len;
313         skbdesc->data = entry->skb->data;
314         skbdesc->ring = ring;
315         skbdesc->entry = entry;
316
317         /*
318          * Send the frame to rt2x00lib for further processing.
319          */
320         rt2x00lib_rxdone(entry, entry->skb, &desc);
321
322         /*
323          * Replace current entry's skb with the newly allocated one,
324          * and reinitialize the urb.
325          */
326         entry->skb = skb;
327         urb->transfer_buffer = entry->skb->data;
328         urb->transfer_buffer_length = entry->skb->len;
329
330 skip_entry:
331         if (test_bit(DEVICE_ENABLED_RADIO, &ring->rt2x00dev->flags)) {
332                 __set_bit(ENTRY_OWNER_NIC, &entry->flags);
333                 usb_submit_urb(urb, GFP_ATOMIC);
334         }
335
336         rt2x00_ring_index_inc(ring);
337 }
338
339 /*
340  * Radio handlers
341  */
342 void rt2x00usb_enable_radio(struct rt2x00_dev *rt2x00dev)
343 {
344         struct usb_device *usb_dev =
345             interface_to_usbdev(rt2x00dev_usb(rt2x00dev));
346         struct data_ring *ring;
347         struct data_entry *entry;
348         unsigned int i;
349
350         /*
351          * Initialize the TX rings
352          */
353         txringall_for_each(rt2x00dev, ring) {
354                 for (i = 0; i < ring->stats.limit; i++)
355                         ring->entry[i].flags = 0;
356
357                 rt2x00_ring_index_clear(ring);
358         }
359
360         /*
361          * Initialize and start the RX ring.
362          */
363         rt2x00_ring_index_clear(rt2x00dev->rx);
364
365         for (i = 0; i < rt2x00dev->rx->stats.limit; i++) {
366                 entry = &rt2x00dev->rx->entry[i];
367
368                 usb_fill_bulk_urb(entry->priv, usb_dev,
369                                   usb_rcvbulkpipe(usb_dev, 1),
370                                   entry->skb->data, entry->skb->len,
371                                   rt2x00usb_interrupt_rxdone, entry);
372
373                 __set_bit(ENTRY_OWNER_NIC, &entry->flags);
374                 usb_submit_urb(entry->priv, GFP_ATOMIC);
375         }
376 }
377 EXPORT_SYMBOL_GPL(rt2x00usb_enable_radio);
378
379 void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
380 {
381         struct data_ring *ring;
382         unsigned int i;
383
384         rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0x0000, 0x0000,
385                                     REGISTER_TIMEOUT);
386
387         /*
388          * Cancel all rings.
389          */
390         ring_for_each(rt2x00dev, ring) {
391                 for (i = 0; i < ring->stats.limit; i++)
392                         usb_kill_urb(ring->entry[i].priv);
393         }
394 }
395 EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
396
397 /*
398  * Device initialization handlers.
399  */
400 static int rt2x00usb_alloc_urb(struct rt2x00_dev *rt2x00dev,
401                                struct data_ring *ring)
402 {
403         unsigned int i;
404
405         /*
406          * Allocate the URB's
407          */
408         for (i = 0; i < ring->stats.limit; i++) {
409                 ring->entry[i].priv = usb_alloc_urb(0, GFP_KERNEL);
410                 if (!ring->entry[i].priv)
411                         return -ENOMEM;
412         }
413
414         return 0;
415 }
416
417 static void rt2x00usb_free_urb(struct rt2x00_dev *rt2x00dev,
418                                struct data_ring *ring)
419 {
420         unsigned int i;
421
422         if (!ring->entry)
423                 return;
424
425         for (i = 0; i < ring->stats.limit; i++) {
426                 usb_kill_urb(ring->entry[i].priv);
427                 usb_free_urb(ring->entry[i].priv);
428                 if (ring->entry[i].skb)
429                         kfree_skb(ring->entry[i].skb);
430         }
431 }
432
433 int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
434 {
435         struct data_ring *ring;
436         struct sk_buff *skb;
437         unsigned int entry_size;
438         unsigned int i;
439         int status;
440
441         /*
442          * Allocate DMA
443          */
444         ring_for_each(rt2x00dev, ring) {
445                 status = rt2x00usb_alloc_urb(rt2x00dev, ring);
446                 if (status)
447                         goto exit;
448         }
449
450         /*
451          * For the RX ring, skb's should be allocated.
452          */
453         entry_size = rt2x00dev->rx->data_size + rt2x00dev->rx->desc_size;
454         for (i = 0; i < rt2x00dev->rx->stats.limit; i++) {
455                 skb = dev_alloc_skb(NET_IP_ALIGN + entry_size);
456                 if (!skb)
457                         goto exit;
458
459                 skb_reserve(skb, NET_IP_ALIGN);
460                 skb_put(skb, entry_size);
461
462                 rt2x00dev->rx->entry[i].skb = skb;
463         }
464
465         return 0;
466
467 exit:
468         rt2x00usb_uninitialize(rt2x00dev);
469
470         return status;
471 }
472 EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
473
474 void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
475 {
476         struct data_ring *ring;
477
478         ring_for_each(rt2x00dev, ring)
479                 rt2x00usb_free_urb(rt2x00dev, ring);
480 }
481 EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
482
483 /*
484  * USB driver handlers.
485  */
486 static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
487 {
488         kfree(rt2x00dev->rf);
489         rt2x00dev->rf = NULL;
490
491         kfree(rt2x00dev->eeprom);
492         rt2x00dev->eeprom = NULL;
493
494         kfree(rt2x00dev->csr_cache);
495         rt2x00dev->csr_cache = NULL;
496 }
497
498 static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
499 {
500         rt2x00dev->csr_cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
501         if (!rt2x00dev->csr_cache)
502                 goto exit;
503
504         rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
505         if (!rt2x00dev->eeprom)
506                 goto exit;
507
508         rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
509         if (!rt2x00dev->rf)
510                 goto exit;
511
512         return 0;
513
514 exit:
515         ERROR_PROBE("Failed to allocate registers.\n");
516
517         rt2x00usb_free_reg(rt2x00dev);
518
519         return -ENOMEM;
520 }
521
522 int rt2x00usb_probe(struct usb_interface *usb_intf,
523                     const struct usb_device_id *id)
524 {
525         struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
526         struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_info;
527         struct ieee80211_hw *hw;
528         struct rt2x00_dev *rt2x00dev;
529         int retval;
530
531         usb_dev = usb_get_dev(usb_dev);
532
533         hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
534         if (!hw) {
535                 ERROR_PROBE("Failed to allocate hardware.\n");
536                 retval = -ENOMEM;
537                 goto exit_put_device;
538         }
539
540         usb_set_intfdata(usb_intf, hw);
541
542         rt2x00dev = hw->priv;
543         rt2x00dev->dev = usb_intf;
544         rt2x00dev->ops = ops;
545         rt2x00dev->hw = hw;
546         mutex_init(&rt2x00dev->usb_cache_mutex);
547
548         rt2x00dev->usb_maxpacket =
549             usb_maxpacket(usb_dev, usb_sndbulkpipe(usb_dev, 1), 1);
550         if (!rt2x00dev->usb_maxpacket)
551                 rt2x00dev->usb_maxpacket = 1;
552
553         retval = rt2x00usb_alloc_reg(rt2x00dev);
554         if (retval)
555                 goto exit_free_device;
556
557         retval = rt2x00lib_probe_dev(rt2x00dev);
558         if (retval)
559                 goto exit_free_reg;
560
561         return 0;
562
563 exit_free_reg:
564         rt2x00usb_free_reg(rt2x00dev);
565
566 exit_free_device:
567         ieee80211_free_hw(hw);
568
569 exit_put_device:
570         usb_put_dev(usb_dev);
571
572         usb_set_intfdata(usb_intf, NULL);
573
574         return retval;
575 }
576 EXPORT_SYMBOL_GPL(rt2x00usb_probe);
577
578 void rt2x00usb_disconnect(struct usb_interface *usb_intf)
579 {
580         struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
581         struct rt2x00_dev *rt2x00dev = hw->priv;
582
583         /*
584          * Free all allocated data.
585          */
586         rt2x00lib_remove_dev(rt2x00dev);
587         rt2x00usb_free_reg(rt2x00dev);
588         ieee80211_free_hw(hw);
589
590         /*
591          * Free the USB device data.
592          */
593         usb_set_intfdata(usb_intf, NULL);
594         usb_put_dev(interface_to_usbdev(usb_intf));
595 }
596 EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
597
598 #ifdef CONFIG_PM
599 int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
600 {
601         struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
602         struct rt2x00_dev *rt2x00dev = hw->priv;
603         int retval;
604
605         retval = rt2x00lib_suspend(rt2x00dev, state);
606         if (retval)
607                 return retval;
608
609         rt2x00usb_free_reg(rt2x00dev);
610
611         /*
612          * Decrease usbdev refcount.
613          */
614         usb_put_dev(interface_to_usbdev(usb_intf));
615
616         return 0;
617 }
618 EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
619
620 int rt2x00usb_resume(struct usb_interface *usb_intf)
621 {
622         struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
623         struct rt2x00_dev *rt2x00dev = hw->priv;
624         int retval;
625
626         usb_get_dev(interface_to_usbdev(usb_intf));
627
628         retval = rt2x00usb_alloc_reg(rt2x00dev);
629         if (retval)
630                 return retval;
631
632         retval = rt2x00lib_resume(rt2x00dev);
633         if (retval)
634                 goto exit_free_reg;
635
636         return 0;
637
638 exit_free_reg:
639         rt2x00usb_free_reg(rt2x00dev);
640
641         return retval;
642 }
643 EXPORT_SYMBOL_GPL(rt2x00usb_resume);
644 #endif /* CONFIG_PM */
645
646 /*
647  * rt2x00pci module information.
648  */
649 MODULE_AUTHOR(DRV_PROJECT);
650 MODULE_VERSION(DRV_VERSION);
651 MODULE_DESCRIPTION("rt2x00 library");
652 MODULE_LICENSE("GPL");