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