Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-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, 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  = 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_ENABLED_RADIO, &rt2x00dev->flags) ||
167             !test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
168                 return;
169
170         /*
171          * Remove the descriptor data from the buffer.
172          */
173         skb_pull(entry->skb, entry->queue->desc_size);
174
175         /*
176          * Obtain the status about this packet.
177          * Note that when the status is 0 it does not mean the
178          * frame was send out correctly. It only means the frame
179          * was succesfully pushed to the hardware, we have no
180          * way to determine the transmission status right now.
181          * (Only indirectly by looking at the failed TX counters
182          * in the register).
183          */
184         if (!urb->status)
185                 __set_bit(TXDONE_UNKNOWN, &txdesc.flags);
186         else
187                 __set_bit(TXDONE_FAILURE, &txdesc.flags);
188         txdesc.retry = 0;
189
190         rt2x00lib_txdone(entry, &txdesc);
191 }
192
193 int rt2x00usb_write_tx_data(struct queue_entry *entry)
194 {
195         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
196         struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
197         struct queue_entry_priv_usb *entry_priv = entry->priv_data;
198         struct skb_frame_desc *skbdesc;
199         u32 length;
200
201         /*
202          * Add the descriptor in front of the skb.
203          */
204         skb_push(entry->skb, entry->queue->desc_size);
205         memset(entry->skb->data, 0, entry->queue->desc_size);
206
207         /*
208          * Fill in skb descriptor
209          */
210         skbdesc = get_skb_frame_desc(entry->skb);
211         skbdesc->desc = entry->skb->data;
212         skbdesc->desc_len = entry->queue->desc_size;
213
214         /*
215          * USB devices cannot blindly pass the skb->len as the
216          * length of the data to usb_fill_bulk_urb. Pass the skb
217          * to the driver to determine what the length should be.
218          */
219         length = rt2x00dev->ops->lib->get_tx_data_len(rt2x00dev, entry->skb);
220
221         usb_fill_bulk_urb(entry_priv->urb, usb_dev,
222                           usb_sndbulkpipe(usb_dev, 1),
223                           entry->skb->data, length,
224                           rt2x00usb_interrupt_txdone, entry);
225
226         return 0;
227 }
228 EXPORT_SYMBOL_GPL(rt2x00usb_write_tx_data);
229
230 static inline void rt2x00usb_kick_tx_entry(struct queue_entry *entry)
231 {
232         struct queue_entry_priv_usb *entry_priv = entry->priv_data;
233
234         if (__test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags))
235                 usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
236 }
237
238 void rt2x00usb_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
239                              const enum data_queue_qid qid)
240 {
241         struct data_queue *queue = rt2x00queue_get_queue(rt2x00dev, qid);
242         unsigned long irqflags;
243         unsigned int index;
244         unsigned int index_done;
245         unsigned int i;
246
247         /*
248          * Only protect the range we are going to loop over,
249          * if during our loop a extra entry is set to pending
250          * it should not be kicked during this run, since it
251          * is part of another TX operation.
252          */
253         spin_lock_irqsave(&queue->lock, irqflags);
254         index = queue->index[Q_INDEX];
255         index_done = queue->index[Q_INDEX_DONE];
256         spin_unlock_irqrestore(&queue->lock, irqflags);
257
258         /*
259          * Start from the TX done pointer, this guarentees that we will
260          * send out all frames in the correct order.
261          */
262         if (index_done < index) {
263                 for (i = index_done; i < index; i++)
264                         rt2x00usb_kick_tx_entry(&queue->entries[i]);
265         } else {
266                 for (i = index_done; i < queue->limit; i++)
267                         rt2x00usb_kick_tx_entry(&queue->entries[i]);
268
269                 for (i = 0; i < index; i++)
270                         rt2x00usb_kick_tx_entry(&queue->entries[i]);
271         }
272 }
273 EXPORT_SYMBOL_GPL(rt2x00usb_kick_tx_queue);
274
275 /*
276  * RX data handlers.
277  */
278 static void rt2x00usb_interrupt_rxdone(struct urb *urb)
279 {
280         struct queue_entry *entry = (struct queue_entry *)urb->context;
281         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
282         struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
283         u8 rxd[32];
284
285         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
286             !test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
287                 return;
288
289         /*
290          * Check if the received data is simply too small
291          * to be actually valid, or if the urb is signaling
292          * a problem.
293          */
294         if (urb->actual_length < entry->queue->desc_size || urb->status) {
295                 __set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
296                 usb_submit_urb(urb, GFP_ATOMIC);
297                 return;
298         }
299
300         /*
301          * Fill in desc fields of the skb descriptor
302          */
303         skbdesc->desc = rxd;
304         skbdesc->desc_len = entry->queue->desc_size;
305
306         /*
307          * Send the frame to rt2x00lib for further processing.
308          */
309         rt2x00lib_rxdone(rt2x00dev, entry);
310 }
311
312 /*
313  * Radio handlers
314  */
315 void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
316 {
317         struct queue_entry_priv_usb *entry_priv;
318         struct queue_entry_priv_usb_bcn *bcn_priv;
319         struct data_queue *queue;
320         unsigned int i;
321
322         rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
323                                     REGISTER_TIMEOUT);
324
325         /*
326          * Cancel all queues.
327          */
328         queue_for_each(rt2x00dev, queue) {
329                 for (i = 0; i < queue->limit; i++) {
330                         entry_priv = queue->entries[i].priv_data;
331                         usb_kill_urb(entry_priv->urb);
332                 }
333         }
334
335         /*
336          * Kill guardian urb (if required by driver).
337          */
338         if (!test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
339                 return;
340
341         for (i = 0; i < rt2x00dev->bcn->limit; i++) {
342                 bcn_priv = rt2x00dev->bcn->entries[i].priv_data;
343                 if (bcn_priv->guardian_urb)
344                         usb_kill_urb(bcn_priv->guardian_urb);
345         }
346 }
347 EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
348
349 /*
350  * Device initialization handlers.
351  */
352 void rt2x00usb_init_rxentry(struct rt2x00_dev *rt2x00dev,
353                             struct queue_entry *entry)
354 {
355         struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
356         struct queue_entry_priv_usb *entry_priv = entry->priv_data;
357
358         usb_fill_bulk_urb(entry_priv->urb, usb_dev,
359                           usb_rcvbulkpipe(usb_dev, 1),
360                           entry->skb->data, entry->skb->len,
361                           rt2x00usb_interrupt_rxdone, entry);
362
363         __set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
364         usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
365 }
366 EXPORT_SYMBOL_GPL(rt2x00usb_init_rxentry);
367
368 void rt2x00usb_init_txentry(struct rt2x00_dev *rt2x00dev,
369                             struct queue_entry *entry)
370 {
371         entry->flags = 0;
372 }
373 EXPORT_SYMBOL_GPL(rt2x00usb_init_txentry);
374
375 static int rt2x00usb_alloc_urb(struct rt2x00_dev *rt2x00dev,
376                                struct data_queue *queue)
377 {
378         struct queue_entry_priv_usb *entry_priv;
379         struct queue_entry_priv_usb_bcn *bcn_priv;
380         unsigned int i;
381
382         for (i = 0; i < queue->limit; i++) {
383                 entry_priv = queue->entries[i].priv_data;
384                 entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
385                 if (!entry_priv->urb)
386                         return -ENOMEM;
387         }
388
389         /*
390          * If this is not the beacon queue or
391          * no guardian byte was required for the beacon,
392          * then we are done.
393          */
394         if (rt2x00dev->bcn != queue ||
395             !test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
396                 return 0;
397
398         for (i = 0; i < queue->limit; i++) {
399                 bcn_priv = queue->entries[i].priv_data;
400                 bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
401                 if (!bcn_priv->guardian_urb)
402                         return -ENOMEM;
403         }
404
405         return 0;
406 }
407
408 static void rt2x00usb_free_urb(struct rt2x00_dev *rt2x00dev,
409                                struct data_queue *queue)
410 {
411         struct queue_entry_priv_usb *entry_priv;
412         struct queue_entry_priv_usb_bcn *bcn_priv;
413         unsigned int i;
414
415         if (!queue->entries)
416                 return;
417
418         for (i = 0; i < queue->limit; i++) {
419                 entry_priv = queue->entries[i].priv_data;
420                 usb_kill_urb(entry_priv->urb);
421                 usb_free_urb(entry_priv->urb);
422         }
423
424         /*
425          * If this is not the beacon queue or
426          * no guardian byte was required for the beacon,
427          * then we are done.
428          */
429         if (rt2x00dev->bcn != queue ||
430             !test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
431                 return;
432
433         for (i = 0; i < queue->limit; i++) {
434                 bcn_priv = queue->entries[i].priv_data;
435                 usb_kill_urb(bcn_priv->guardian_urb);
436                 usb_free_urb(bcn_priv->guardian_urb);
437         }
438 }
439
440 int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
441 {
442         struct data_queue *queue;
443         int status;
444
445         /*
446          * Allocate DMA
447          */
448         queue_for_each(rt2x00dev, queue) {
449                 status = rt2x00usb_alloc_urb(rt2x00dev, queue);
450                 if (status)
451                         goto exit;
452         }
453
454         return 0;
455
456 exit:
457         rt2x00usb_uninitialize(rt2x00dev);
458
459         return status;
460 }
461 EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
462
463 void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
464 {
465         struct data_queue *queue;
466
467         queue_for_each(rt2x00dev, queue)
468                 rt2x00usb_free_urb(rt2x00dev, queue);
469 }
470 EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
471
472 /*
473  * USB driver handlers.
474  */
475 static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
476 {
477         kfree(rt2x00dev->rf);
478         rt2x00dev->rf = NULL;
479
480         kfree(rt2x00dev->eeprom);
481         rt2x00dev->eeprom = NULL;
482
483         kfree(rt2x00dev->csr.cache);
484         rt2x00dev->csr.cache = NULL;
485 }
486
487 static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
488 {
489         rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
490         if (!rt2x00dev->csr.cache)
491                 goto exit;
492
493         rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
494         if (!rt2x00dev->eeprom)
495                 goto exit;
496
497         rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
498         if (!rt2x00dev->rf)
499                 goto exit;
500
501         return 0;
502
503 exit:
504         ERROR_PROBE("Failed to allocate registers.\n");
505
506         rt2x00usb_free_reg(rt2x00dev);
507
508         return -ENOMEM;
509 }
510
511 int rt2x00usb_probe(struct usb_interface *usb_intf,
512                     const struct usb_device_id *id)
513 {
514         struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
515         struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_info;
516         struct ieee80211_hw *hw;
517         struct rt2x00_dev *rt2x00dev;
518         int retval;
519
520         usb_dev = usb_get_dev(usb_dev);
521
522         hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
523         if (!hw) {
524                 ERROR_PROBE("Failed to allocate hardware.\n");
525                 retval = -ENOMEM;
526                 goto exit_put_device;
527         }
528
529         usb_set_intfdata(usb_intf, hw);
530
531         rt2x00dev = hw->priv;
532         rt2x00dev->dev = &usb_intf->dev;
533         rt2x00dev->ops = ops;
534         rt2x00dev->hw = hw;
535         mutex_init(&rt2x00dev->usb_cache_mutex);
536
537         rt2x00dev->usb_maxpacket =
538             usb_maxpacket(usb_dev, usb_sndbulkpipe(usb_dev, 1), 1);
539         if (!rt2x00dev->usb_maxpacket)
540                 rt2x00dev->usb_maxpacket = 1;
541
542         retval = rt2x00usb_alloc_reg(rt2x00dev);
543         if (retval)
544                 goto exit_free_device;
545
546         retval = rt2x00lib_probe_dev(rt2x00dev);
547         if (retval)
548                 goto exit_free_reg;
549
550         return 0;
551
552 exit_free_reg:
553         rt2x00usb_free_reg(rt2x00dev);
554
555 exit_free_device:
556         ieee80211_free_hw(hw);
557
558 exit_put_device:
559         usb_put_dev(usb_dev);
560
561         usb_set_intfdata(usb_intf, NULL);
562
563         return retval;
564 }
565 EXPORT_SYMBOL_GPL(rt2x00usb_probe);
566
567 void rt2x00usb_disconnect(struct usb_interface *usb_intf)
568 {
569         struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
570         struct rt2x00_dev *rt2x00dev = hw->priv;
571
572         /*
573          * Free all allocated data.
574          */
575         rt2x00lib_remove_dev(rt2x00dev);
576         rt2x00usb_free_reg(rt2x00dev);
577         ieee80211_free_hw(hw);
578
579         /*
580          * Free the USB device data.
581          */
582         usb_set_intfdata(usb_intf, NULL);
583         usb_put_dev(interface_to_usbdev(usb_intf));
584 }
585 EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
586
587 #ifdef CONFIG_PM
588 int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
589 {
590         struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
591         struct rt2x00_dev *rt2x00dev = hw->priv;
592         int retval;
593
594         retval = rt2x00lib_suspend(rt2x00dev, state);
595         if (retval)
596                 return retval;
597
598         rt2x00usb_free_reg(rt2x00dev);
599
600         /*
601          * Decrease usbdev refcount.
602          */
603         usb_put_dev(interface_to_usbdev(usb_intf));
604
605         return 0;
606 }
607 EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
608
609 int rt2x00usb_resume(struct usb_interface *usb_intf)
610 {
611         struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
612         struct rt2x00_dev *rt2x00dev = hw->priv;
613         int retval;
614
615         usb_get_dev(interface_to_usbdev(usb_intf));
616
617         retval = rt2x00usb_alloc_reg(rt2x00dev);
618         if (retval)
619                 return retval;
620
621         retval = rt2x00lib_resume(rt2x00dev);
622         if (retval)
623                 goto exit_free_reg;
624
625         return 0;
626
627 exit_free_reg:
628         rt2x00usb_free_reg(rt2x00dev);
629
630         return retval;
631 }
632 EXPORT_SYMBOL_GPL(rt2x00usb_resume);
633 #endif /* CONFIG_PM */
634
635 /*
636  * rt2x00usb module information.
637  */
638 MODULE_AUTHOR(DRV_PROJECT);
639 MODULE_VERSION(DRV_VERSION);
640 MODULE_DESCRIPTION("rt2x00 usb library");
641 MODULE_LICENSE("GPL");