[PATCH] rt2x00: Add get_tx_data_len callback function
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / rt2x00 / rt2x00usb.c
blob73cc726c4046724f01f6e5b9e1b88b5eaf8dc0d2
1 /*
2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
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.
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.
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.
22 Module: rt2x00usb
23 Abstract: rt2x00 generic usb device routines.
27 * Set enviroment defines for rt2x00.h
29 #define DRV_NAME "rt2x00usb"
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/usb.h>
35 #include "rt2x00.h"
36 #include "rt2x00usb.h"
39 * Interfacing with the HW.
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)
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);
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;
63 * Check for errors
64 * -ENODEV: Device has disappeared, no point continuing.
65 * All other errors: Try again.
67 else if (status == -ENODEV)
68 break;
71 ERROR(rt2x00dev,
72 "Vendor Request 0x%02x failed for offset 0x%04x with error %d.\n",
73 request, offset, status);
75 return status;
77 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
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)
84 int status;
87 * Check for Cache availability.
89 if (unlikely(!rt2x00dev->csr_cache || buffer_length > CSR_CACHE_SIZE)) {
90 ERROR(rt2x00dev, "CSR cache not available.\n");
91 return -ENOMEM;
94 if (requesttype == USB_VENDOR_REQUEST_OUT)
95 memcpy(rt2x00dev->csr_cache, buffer, buffer_length);
97 status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
98 offset, 0, rt2x00dev->csr_cache,
99 buffer_length, timeout);
101 if (!status && requesttype == USB_VENDOR_REQUEST_IN)
102 memcpy(buffer, rt2x00dev->csr_cache, buffer_length);
104 return status;
106 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
109 * TX data handlers.
111 static void rt2x00usb_interrupt_txdone(struct urb *urb)
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;
120 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
121 !__test_and_clear_bit(ENTRY_OWNER_NIC, &entry->flags))
122 return;
124 rt2x00_desc_read(txd, 0, &word);
127 * Remove the descriptor data from the buffer.
129 skb_pull(entry->skb, ring->desc_size);
132 * Obtain the status about this packet.
134 tx_status = !urb->status ? TX_SUCCESS : TX_FAIL_RETRY;
136 rt2x00lib_txdone(entry, tx_status, 0);
139 * Make this entry available for reuse.
141 entry->flags = 0;
142 rt2x00_ring_index_done_inc(entry->ring);
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.
149 if (!rt2x00_ring_full(ring))
150 ieee80211_wake_queue(rt2x00dev->hw,
151 entry->tx_status.control.queue);
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)
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;
165 if (rt2x00_ring_full(ring)) {
166 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
167 return -EINVAL;
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;
180 * Add the descriptor in front of the skb.
182 skb_push(skb, ring->desc_size);
183 memset(skb->data, 0, ring->desc_size);
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;
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.
197 length = rt2x00dev->ops->lib->get_tx_data_len(rt2x00dev,
198 max_packet, skb);
201 * Initialize URB and send the frame to the device.
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);
208 rt2x00_ring_index_inc(ring);
210 if (rt2x00_ring_full(ring))
211 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
213 return 0;
215 EXPORT_SYMBOL_GPL(rt2x00usb_write_tx_data);
218 * RX data handlers.
220 static void rt2x00usb_interrupt_rxdone(struct urb *urb)
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;
229 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
230 !test_and_clear_bit(ENTRY_OWNER_NIC, &entry->flags))
231 return;
234 * Check if the received data is simply too small
235 * to be actually valid, or if the urb is signaling
236 * a problem.
238 if (urb->actual_length < entry->ring->desc_size || urb->status)
239 goto skip_entry;
241 memset(&desc, 0x00, sizeof(desc));
242 rt2x00dev->ops->lib->fill_rxdone(entry, &desc);
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.
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;
254 skb_reserve(skb, NET_IP_ALIGN);
255 skb_put(skb, frame_size);
258 * Trim the skb_buffer to only contain the valid
259 * frame data (so ignore the device's descriptor).
261 skb_trim(entry->skb, desc.size);
264 * Send the frame to rt2x00lib for further processing.
266 rt2x00lib_rxdone(entry, entry->skb, &desc);
269 * Replace current entry's skb with the newly allocated one,
270 * and reinitialize the urb.
272 entry->skb = skb;
273 urb->transfer_buffer = entry->skb->data;
274 urb->transfer_buffer_length = entry->skb->len;
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);
282 rt2x00_ring_index_inc(ring);
286 * Radio handlers
288 void rt2x00usb_enable_radio(struct rt2x00_dev *rt2x00dev)
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;
297 * Initialize the TX rings
299 txringall_for_each(rt2x00dev, ring) {
300 for (i = 0; i < ring->stats.limit; i++)
301 ring->entry[i].flags = 0;
303 rt2x00_ring_index_clear(ring);
307 * Initialize and start the RX ring.
309 rt2x00_ring_index_clear(rt2x00dev->rx);
311 for (i = 0; i < rt2x00dev->rx->stats.limit; i++) {
312 entry = &rt2x00dev->rx->entry[i];
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);
319 __set_bit(ENTRY_OWNER_NIC, &entry->flags);
320 usb_submit_urb(entry->priv, GFP_ATOMIC);
323 EXPORT_SYMBOL_GPL(rt2x00usb_enable_radio);
325 void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
327 struct data_ring *ring;
328 unsigned int i;
330 rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0x0000, 0x0000,
331 REGISTER_TIMEOUT);
334 * Cancel all rings.
336 ring_for_each(rt2x00dev, ring) {
337 for (i = 0; i < ring->stats.limit; i++)
338 usb_kill_urb(ring->entry[i].priv);
341 EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
344 * Device initialization handlers.
346 static int rt2x00usb_alloc_urb(struct rt2x00_dev *rt2x00dev,
347 struct data_ring *ring)
349 unsigned int i;
352 * Allocate the URB's
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;
360 return 0;
363 static void rt2x00usb_free_urb(struct rt2x00_dev *rt2x00dev,
364 struct data_ring *ring)
366 unsigned int i;
368 if (!ring->entry)
369 return;
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);
379 int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
381 struct data_ring *ring;
382 struct sk_buff *skb;
383 unsigned int entry_size;
384 unsigned int i;
385 int status;
388 * Allocate DMA
390 ring_for_each(rt2x00dev, ring) {
391 status = rt2x00usb_alloc_urb(rt2x00dev, ring);
392 if (status)
393 goto exit;
397 * For the RX ring, skb's should be allocated.
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;
405 skb_reserve(skb, NET_IP_ALIGN);
406 skb_put(skb, entry_size);
408 rt2x00dev->rx->entry[i].skb = skb;
411 return 0;
413 exit:
414 rt2x00usb_uninitialize(rt2x00dev);
416 return status;
418 EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
420 void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
422 struct data_ring *ring;
424 ring_for_each(rt2x00dev, ring)
425 rt2x00usb_free_urb(rt2x00dev, ring);
427 EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
430 * USB driver handlers.
432 static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
434 kfree(rt2x00dev->rf);
435 rt2x00dev->rf = NULL;
437 kfree(rt2x00dev->eeprom);
438 rt2x00dev->eeprom = NULL;
440 kfree(rt2x00dev->csr_cache);
441 rt2x00dev->csr_cache = NULL;
444 static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
446 rt2x00dev->csr_cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
447 if (!rt2x00dev->csr_cache)
448 goto exit;
450 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
451 if (!rt2x00dev->eeprom)
452 goto exit;
454 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
455 if (!rt2x00dev->rf)
456 goto exit;
458 return 0;
460 exit:
461 ERROR_PROBE("Failed to allocate registers.\n");
463 rt2x00usb_free_reg(rt2x00dev);
465 return -ENOMEM;
468 int rt2x00usb_probe(struct usb_interface *usb_intf,
469 const struct usb_device_id *id)
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;
477 usb_dev = usb_get_dev(usb_dev);
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;
486 usb_set_intfdata(usb_intf, hw);
488 rt2x00dev = hw->priv;
489 rt2x00dev->dev = usb_intf;
490 rt2x00dev->ops = ops;
491 rt2x00dev->hw = hw;
493 retval = rt2x00usb_alloc_reg(rt2x00dev);
494 if (retval)
495 goto exit_free_device;
497 retval = rt2x00lib_probe_dev(rt2x00dev);
498 if (retval)
499 goto exit_free_reg;
501 return 0;
503 exit_free_reg:
504 rt2x00usb_free_reg(rt2x00dev);
506 exit_free_device:
507 ieee80211_free_hw(hw);
509 exit_put_device:
510 usb_put_dev(usb_dev);
512 usb_set_intfdata(usb_intf, NULL);
514 return retval;
516 EXPORT_SYMBOL_GPL(rt2x00usb_probe);
518 void rt2x00usb_disconnect(struct usb_interface *usb_intf)
520 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
521 struct rt2x00_dev *rt2x00dev = hw->priv;
524 * Free all allocated data.
526 rt2x00lib_remove_dev(rt2x00dev);
527 rt2x00usb_free_reg(rt2x00dev);
528 ieee80211_free_hw(hw);
531 * Free the USB device data.
533 usb_set_intfdata(usb_intf, NULL);
534 usb_put_dev(interface_to_usbdev(usb_intf));
536 EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
538 #ifdef CONFIG_PM
539 int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
541 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
542 struct rt2x00_dev *rt2x00dev = hw->priv;
543 int retval;
545 retval = rt2x00lib_suspend(rt2x00dev, state);
546 if (retval)
547 return retval;
549 rt2x00usb_free_reg(rt2x00dev);
552 * Decrease usbdev refcount.
554 usb_put_dev(interface_to_usbdev(usb_intf));
556 return 0;
558 EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
560 int rt2x00usb_resume(struct usb_interface *usb_intf)
562 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
563 struct rt2x00_dev *rt2x00dev = hw->priv;
564 int retval;
566 usb_get_dev(interface_to_usbdev(usb_intf));
568 retval = rt2x00usb_alloc_reg(rt2x00dev);
569 if (retval)
570 return retval;
572 retval = rt2x00lib_resume(rt2x00dev);
573 if (retval)
574 goto exit_free_reg;
576 return 0;
578 exit_free_reg:
579 rt2x00usb_free_reg(rt2x00dev);
581 return retval;
583 EXPORT_SYMBOL_GPL(rt2x00usb_resume);
584 #endif /* CONFIG_PM */
587 * rt2x00pci module information.
589 MODULE_AUTHOR(DRV_PROJECT);
590 MODULE_VERSION(DRV_VERSION);
591 MODULE_DESCRIPTION("rt2x00 library");
592 MODULE_LICENSE("GPL");