Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00usb.c
1 /*
2         Copyright (C) 2004 - 2008 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 = to_usb_device_intf(rt2x00dev->dev);
44         int status;
45         unsigned int i;
46         unsigned int pipe =
47             (requesttype == USB_VENDOR_REQUEST_IN) ?
48             usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
49
50
51         for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
52                 status = usb_control_msg(usb_dev, pipe, request, requesttype,
53                                          value, offset, buffer, buffer_length,
54                                          timeout);
55                 if (status >= 0)
56                         return 0;
57
58                 /*
59                  * Check for errors
60                  * -ENODEV: Device has disappeared, no point continuing.
61                  * All other errors: Try again.
62                  */
63                 else if (status == -ENODEV)
64                         break;
65         }
66
67         ERROR(rt2x00dev,
68               "Vendor Request 0x%02x failed for offset 0x%04x with error %d.\n",
69               request, offset, status);
70
71         return status;
72 }
73 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
74
75 int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
76                                    const u8 request, const u8 requesttype,
77                                    const u16 offset, void *buffer,
78                                    const u16 buffer_length, const int timeout)
79 {
80         int status;
81
82         BUG_ON(!mutex_is_locked(&rt2x00dev->usb_cache_mutex));
83
84         /*
85          * Check for Cache availability.
86          */
87         if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
88                 ERROR(rt2x00dev, "CSR cache not available.\n");
89                 return -ENOMEM;
90         }
91
92         if (requesttype == USB_VENDOR_REQUEST_OUT)
93                 memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
94
95         status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
96                                           offset, 0, rt2x00dev->csr.cache,
97                                           buffer_length, timeout);
98
99         if (!status && requesttype == USB_VENDOR_REQUEST_IN)
100                 memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
101
102         return status;
103 }
104 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
105
106 int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
107                                   const u8 request, const u8 requesttype,
108                                   const u16 offset, void *buffer,
109                                   const u16 buffer_length, const int timeout)
110 {
111         int status;
112
113         mutex_lock(&rt2x00dev->usb_cache_mutex);
114
115         status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
116                                                 requesttype, offset, buffer,
117                                                 buffer_length, timeout);
118
119         mutex_unlock(&rt2x00dev->usb_cache_mutex);
120
121         return status;
122 }
123 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
124
125 int rt2x00usb_vendor_request_large_buff(struct rt2x00_dev *rt2x00dev,
126                                         const u8 request, const u8 requesttype,
127                                         const u16 offset, const void *buffer,
128                                         const u16 buffer_length,
129                                         const int timeout)
130 {
131         int status = 0;
132         unsigned char *tb;
133         u16 off, len, bsize;
134
135         mutex_lock(&rt2x00dev->usb_cache_mutex);
136
137         tb  = (char *)buffer;
138         off = offset;
139         len = buffer_length;
140         while (len && !status) {
141                 bsize = min_t(u16, CSR_CACHE_SIZE, len);
142                 status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
143                                                         requesttype, off, tb,
144                                                         bsize, timeout);
145
146                 tb  += bsize;
147                 len -= bsize;
148                 off += bsize;
149         }
150
151         mutex_unlock(&rt2x00dev->usb_cache_mutex);
152
153         return status;
154 }
155 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_large_buff);
156
157 /*
158  * TX data handlers.
159  */
160 static void rt2x00usb_interrupt_txdone(struct urb *urb)
161 {
162         struct queue_entry *entry = (struct queue_entry *)urb->context;
163         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
164         struct txdone_entry_desc txdesc;
165
166         if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
167             !test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
168                 return;
169
170         /*
171          * Obtain the status about this packet.
172          * Note that when the status is 0 it does not mean the
173          * frame was send out correctly. It only means the frame
174          * was succesfully pushed to the hardware, we have no
175          * way to determine the transmission status right now.
176          * (Only indirectly by looking at the failed TX counters
177          * in the register).
178          */
179         txdesc.flags = 0;
180         if (!urb->status)
181                 __set_bit(TXDONE_UNKNOWN, &txdesc.flags);
182         else
183                 __set_bit(TXDONE_FAILURE, &txdesc.flags);
184         txdesc.retry = 0;
185
186         rt2x00lib_txdone(entry, &txdesc);
187 }
188
189 int rt2x00usb_write_tx_data(struct queue_entry *entry)
190 {
191         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
192         struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
193         struct queue_entry_priv_usb *entry_priv = entry->priv_data;
194         struct skb_frame_desc *skbdesc;
195         u32 length;
196
197         /*
198          * Add the descriptor in front of the skb.
199          */
200         skb_push(entry->skb, entry->queue->desc_size);
201         memset(entry->skb->data, 0, entry->queue->desc_size);
202
203         /*
204          * Fill in skb descriptor
205          */
206         skbdesc = get_skb_frame_desc(entry->skb);
207         skbdesc->desc = entry->skb->data;
208         skbdesc->desc_len = entry->queue->desc_size;
209
210         /*
211          * USB devices cannot blindly pass the skb->len as the
212          * length of the data to usb_fill_bulk_urb. Pass the skb
213          * to the driver to determine what the length should be.
214          */
215         length = rt2x00dev->ops->lib->get_tx_data_len(rt2x00dev, entry->skb);
216
217         usb_fill_bulk_urb(entry_priv->urb, usb_dev,
218                           usb_sndbulkpipe(usb_dev, 1),
219                           entry->skb->data, length,
220                           rt2x00usb_interrupt_txdone, entry);
221
222         /*
223          * Make sure the skb->data pointer points to the frame, not the
224          * descriptor.
225          */
226         skb_pull(entry->skb, entry->queue->desc_size);
227
228         return 0;
229 }
230 EXPORT_SYMBOL_GPL(rt2x00usb_write_tx_data);
231
232 static inline void rt2x00usb_kick_tx_entry(struct queue_entry *entry)
233 {
234         struct queue_entry_priv_usb *entry_priv = entry->priv_data;
235
236         if (test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags))
237                 usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
238 }
239
240 void rt2x00usb_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
241                              const enum data_queue_qid qid)
242 {
243         struct data_queue *queue = rt2x00queue_get_queue(rt2x00dev, qid);
244         unsigned long irqflags;
245         unsigned int index;
246         unsigned int index_done;
247         unsigned int i;
248
249         /*
250          * Only protect the range we are going to loop over,
251          * if during our loop a extra entry is set to pending
252          * it should not be kicked during this run, since it
253          * is part of another TX operation.
254          */
255         spin_lock_irqsave(&queue->lock, irqflags);
256         index = queue->index[Q_INDEX];
257         index_done = queue->index[Q_INDEX_DONE];
258         spin_unlock_irqrestore(&queue->lock, irqflags);
259
260         /*
261          * Start from the TX done pointer, this guarentees that we will
262          * send out all frames in the correct order.
263          */
264         if (index_done < index) {
265                 for (i = index_done; i < index; i++)
266                         rt2x00usb_kick_tx_entry(&queue->entries[i]);
267         } else {
268                 for (i = index_done; i < queue->limit; i++)
269                         rt2x00usb_kick_tx_entry(&queue->entries[i]);
270
271                 for (i = 0; i < index; i++)
272                         rt2x00usb_kick_tx_entry(&queue->entries[i]);
273         }
274 }
275 EXPORT_SYMBOL_GPL(rt2x00usb_kick_tx_queue);
276
277 /*
278  * RX data handlers.
279  */
280 static void rt2x00usb_interrupt_rxdone(struct urb *urb)
281 {
282         struct queue_entry *entry = (struct queue_entry *)urb->context;
283         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
284         struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
285         u8 rxd[32];
286
287         if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
288             !test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
289                 return;
290
291         /*
292          * Check if the received data is simply too small
293          * to be actually valid, or if the urb is signaling
294          * a problem.
295          */
296         if (urb->actual_length < entry->queue->desc_size || urb->status) {
297                 set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
298                 usb_submit_urb(urb, GFP_ATOMIC);
299                 return;
300         }
301
302         /*
303          * Fill in desc fields of the skb descriptor
304          */
305         skbdesc->desc = rxd;
306         skbdesc->desc_len = entry->queue->desc_size;
307
308         /*
309          * Send the frame to rt2x00lib for further processing.
310          */
311         rt2x00lib_rxdone(rt2x00dev, entry);
312 }
313
314 /*
315  * Radio handlers
316  */
317 void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
318 {
319         struct queue_entry_priv_usb *entry_priv;
320         struct queue_entry_priv_usb_bcn *bcn_priv;
321         struct data_queue *queue;
322         unsigned int i;
323
324         rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
325                                     REGISTER_TIMEOUT);
326
327         /*
328          * Cancel all queues.
329          */
330         queue_for_each(rt2x00dev, queue) {
331                 for (i = 0; i < queue->limit; i++) {
332                         entry_priv = queue->entries[i].priv_data;
333                         usb_kill_urb(entry_priv->urb);
334                 }
335         }
336
337         /*
338          * Kill guardian urb (if required by driver).
339          */
340         if (!test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
341                 return;
342
343         for (i = 0; i < rt2x00dev->bcn->limit; i++) {
344                 bcn_priv = rt2x00dev->bcn->entries[i].priv_data;
345                 if (bcn_priv->guardian_urb)
346                         usb_kill_urb(bcn_priv->guardian_urb);
347         }
348 }
349 EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
350
351 /*
352  * Device initialization handlers.
353  */
354 void rt2x00usb_init_rxentry(struct rt2x00_dev *rt2x00dev,
355                             struct queue_entry *entry)
356 {
357         struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
358         struct queue_entry_priv_usb *entry_priv = entry->priv_data;
359
360         usb_fill_bulk_urb(entry_priv->urb, usb_dev,
361                           usb_rcvbulkpipe(usb_dev, 1),
362                           entry->skb->data, entry->skb->len,
363                           rt2x00usb_interrupt_rxdone, entry);
364
365         set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
366         usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
367 }
368 EXPORT_SYMBOL_GPL(rt2x00usb_init_rxentry);
369
370 void rt2x00usb_init_txentry(struct rt2x00_dev *rt2x00dev,
371                             struct queue_entry *entry)
372 {
373         entry->flags = 0;
374 }
375 EXPORT_SYMBOL_GPL(rt2x00usb_init_txentry);
376
377 static int rt2x00usb_alloc_urb(struct rt2x00_dev *rt2x00dev,
378                                struct data_queue *queue)
379 {
380         struct queue_entry_priv_usb *entry_priv;
381         struct queue_entry_priv_usb_bcn *bcn_priv;
382         unsigned int i;
383
384         for (i = 0; i < queue->limit; i++) {
385                 entry_priv = queue->entries[i].priv_data;
386                 entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
387                 if (!entry_priv->urb)
388                         return -ENOMEM;
389         }
390
391         /*
392          * If this is not the beacon queue or
393          * no guardian byte was required for the beacon,
394          * then we are done.
395          */
396         if (rt2x00dev->bcn != queue ||
397             !test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
398                 return 0;
399
400         for (i = 0; i < queue->limit; i++) {
401                 bcn_priv = queue->entries[i].priv_data;
402                 bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
403                 if (!bcn_priv->guardian_urb)
404                         return -ENOMEM;
405         }
406
407         return 0;
408 }
409
410 static void rt2x00usb_free_urb(struct rt2x00_dev *rt2x00dev,
411                                struct data_queue *queue)
412 {
413         struct queue_entry_priv_usb *entry_priv;
414         struct queue_entry_priv_usb_bcn *bcn_priv;
415         unsigned int i;
416
417         if (!queue->entries)
418                 return;
419
420         for (i = 0; i < queue->limit; i++) {
421                 entry_priv = queue->entries[i].priv_data;
422                 usb_kill_urb(entry_priv->urb);
423                 usb_free_urb(entry_priv->urb);
424         }
425
426         /*
427          * If this is not the beacon queue or
428          * no guardian byte was required for the beacon,
429          * then we are done.
430          */
431         if (rt2x00dev->bcn != queue ||
432             !test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
433                 return;
434
435         for (i = 0; i < queue->limit; i++) {
436                 bcn_priv = queue->entries[i].priv_data;
437                 usb_kill_urb(bcn_priv->guardian_urb);
438                 usb_free_urb(bcn_priv->guardian_urb);
439         }
440 }
441
442 int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
443 {
444         struct data_queue *queue;
445         int status;
446
447         /*
448          * Allocate DMA
449          */
450         queue_for_each(rt2x00dev, queue) {
451                 status = rt2x00usb_alloc_urb(rt2x00dev, queue);
452                 if (status)
453                         goto exit;
454         }
455
456         return 0;
457
458 exit:
459         rt2x00usb_uninitialize(rt2x00dev);
460
461         return status;
462 }
463 EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
464
465 void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
466 {
467         struct data_queue *queue;
468
469         queue_for_each(rt2x00dev, queue)
470                 rt2x00usb_free_urb(rt2x00dev, queue);
471 }
472 EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
473
474 /*
475  * USB driver handlers.
476  */
477 static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
478 {
479         kfree(rt2x00dev->rf);
480         rt2x00dev->rf = NULL;
481
482         kfree(rt2x00dev->eeprom);
483         rt2x00dev->eeprom = NULL;
484
485         kfree(rt2x00dev->csr.cache);
486         rt2x00dev->csr.cache = NULL;
487 }
488
489 static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
490 {
491         rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
492         if (!rt2x00dev->csr.cache)
493                 goto exit;
494
495         rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
496         if (!rt2x00dev->eeprom)
497                 goto exit;
498
499         rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
500         if (!rt2x00dev->rf)
501                 goto exit;
502
503         return 0;
504
505 exit:
506         ERROR_PROBE("Failed to allocate registers.\n");
507
508         rt2x00usb_free_reg(rt2x00dev);
509
510         return -ENOMEM;
511 }
512
513 int rt2x00usb_probe(struct usb_interface *usb_intf,
514                     const struct usb_device_id *id)
515 {
516         struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
517         struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_info;
518         struct ieee80211_hw *hw;
519         struct rt2x00_dev *rt2x00dev;
520         int retval;
521
522         usb_dev = usb_get_dev(usb_dev);
523
524         hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
525         if (!hw) {
526                 ERROR_PROBE("Failed to allocate hardware.\n");
527                 retval = -ENOMEM;
528                 goto exit_put_device;
529         }
530
531         usb_set_intfdata(usb_intf, hw);
532
533         rt2x00dev = hw->priv;
534         rt2x00dev->dev = &usb_intf->dev;
535         rt2x00dev->ops = ops;
536         rt2x00dev->hw = hw;
537         mutex_init(&rt2x00dev->usb_cache_mutex);
538
539         rt2x00dev->usb_maxpacket =
540             usb_maxpacket(usb_dev, usb_sndbulkpipe(usb_dev, 1), 1);
541         if (!rt2x00dev->usb_maxpacket)
542                 rt2x00dev->usb_maxpacket = 1;
543
544         retval = rt2x00usb_alloc_reg(rt2x00dev);
545         if (retval)
546                 goto exit_free_device;
547
548         retval = rt2x00lib_probe_dev(rt2x00dev);
549         if (retval)
550                 goto exit_free_reg;
551
552         return 0;
553
554 exit_free_reg:
555         rt2x00usb_free_reg(rt2x00dev);
556
557 exit_free_device:
558         ieee80211_free_hw(hw);
559
560 exit_put_device:
561         usb_put_dev(usb_dev);
562
563         usb_set_intfdata(usb_intf, NULL);
564
565         return retval;
566 }
567 EXPORT_SYMBOL_GPL(rt2x00usb_probe);
568
569 void rt2x00usb_disconnect(struct usb_interface *usb_intf)
570 {
571         struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
572         struct rt2x00_dev *rt2x00dev = hw->priv;
573
574         /*
575          * Free all allocated data.
576          */
577         rt2x00lib_remove_dev(rt2x00dev);
578         rt2x00usb_free_reg(rt2x00dev);
579         ieee80211_free_hw(hw);
580
581         /*
582          * Free the USB device data.
583          */
584         usb_set_intfdata(usb_intf, NULL);
585         usb_put_dev(interface_to_usbdev(usb_intf));
586 }
587 EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
588
589 #ifdef CONFIG_PM
590 int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
591 {
592         struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
593         struct rt2x00_dev *rt2x00dev = hw->priv;
594         int retval;
595
596         retval = rt2x00lib_suspend(rt2x00dev, state);
597         if (retval)
598                 return retval;
599
600         rt2x00usb_free_reg(rt2x00dev);
601
602         /*
603          * Decrease usbdev refcount.
604          */
605         usb_put_dev(interface_to_usbdev(usb_intf));
606
607         return 0;
608 }
609 EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
610
611 int rt2x00usb_resume(struct usb_interface *usb_intf)
612 {
613         struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
614         struct rt2x00_dev *rt2x00dev = hw->priv;
615         int retval;
616
617         usb_get_dev(interface_to_usbdev(usb_intf));
618
619         retval = rt2x00usb_alloc_reg(rt2x00dev);
620         if (retval)
621                 return retval;
622
623         retval = rt2x00lib_resume(rt2x00dev);
624         if (retval)
625                 goto exit_free_reg;
626
627         return 0;
628
629 exit_free_reg:
630         rt2x00usb_free_reg(rt2x00dev);
631
632         return retval;
633 }
634 EXPORT_SYMBOL_GPL(rt2x00usb_resume);
635 #endif /* CONFIG_PM */
636
637 /*
638  * rt2x00usb module information.
639  */
640 MODULE_AUTHOR(DRV_PROJECT);
641 MODULE_VERSION(DRV_VERSION);
642 MODULE_DESCRIPTION("rt2x00 usb library");
643 MODULE_LICENSE("GPL");