rt2x00: Replace DRV_NAME with KBUILD_MODNAME
[linux-2.6/verdex.git] / drivers / net / wireless / rt2x00 / rt2x00pci.c
blobcf0bb5d9d847cc18e2aac9e431f7fc925ec3c939
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: rt2x00pci
23 Abstract: rt2x00 generic pci device routines.
26 #include <linux/dma-mapping.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
31 #include "rt2x00.h"
32 #include "rt2x00pci.h"
35 * Beacon handlers.
37 int rt2x00pci_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
38 struct ieee80211_tx_control *control)
40 struct rt2x00_dev *rt2x00dev = hw->priv;
41 struct data_ring *ring =
42 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
43 struct data_entry *entry = rt2x00_get_data_entry(ring);
46 * Just in case mac80211 doesn't set this correctly,
47 * but we need this queue set for the descriptor
48 * initialization.
50 control->queue = IEEE80211_TX_QUEUE_BEACON;
53 * Update the beacon entry.
55 memcpy(entry->data_addr, skb->data, skb->len);
56 rt2x00lib_write_tx_desc(rt2x00dev, entry->priv,
57 (struct ieee80211_hdr *)skb->data,
58 skb->len, control);
61 * Enable beacon generation.
63 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
65 return 0;
67 EXPORT_SYMBOL_GPL(rt2x00pci_beacon_update);
70 * TX data handlers.
72 int rt2x00pci_write_tx_data(struct rt2x00_dev *rt2x00dev,
73 struct data_ring *ring, struct sk_buff *skb,
74 struct ieee80211_tx_control *control)
76 struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
77 struct data_entry *entry = rt2x00_get_data_entry(ring);
78 __le32 *txd = entry->priv;
79 u32 word;
81 if (rt2x00_ring_full(ring)) {
82 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
83 return -EINVAL;
86 rt2x00_desc_read(txd, 0, &word);
88 if (rt2x00_get_field32(word, TXD_ENTRY_OWNER_NIC) ||
89 rt2x00_get_field32(word, TXD_ENTRY_VALID)) {
90 ERROR(rt2x00dev,
91 "Arrived at non-free entry in the non-full queue %d.\n"
92 "Please file bug report to %s.\n",
93 control->queue, DRV_PROJECT);
94 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
95 return -EINVAL;
98 entry->skb = skb;
99 memcpy(&entry->tx_status.control, control, sizeof(*control));
100 memcpy(entry->data_addr, skb->data, skb->len);
101 rt2x00lib_write_tx_desc(rt2x00dev, txd, ieee80211hdr,
102 skb->len, control);
104 rt2x00_ring_index_inc(ring);
106 if (rt2x00_ring_full(ring))
107 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
109 return 0;
111 EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
114 * TX/RX data handlers.
116 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
118 struct data_ring *ring = rt2x00dev->rx;
119 struct data_entry *entry;
120 struct sk_buff *skb;
121 struct ieee80211_hdr *hdr;
122 struct rxdata_entry_desc desc;
123 int header_size;
124 __le32 *rxd;
125 int align;
126 u32 word;
128 while (1) {
129 entry = rt2x00_get_data_entry(ring);
130 rxd = entry->priv;
131 rt2x00_desc_read(rxd, 0, &word);
133 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
134 break;
136 memset(&desc, 0x00, sizeof(desc));
137 rt2x00dev->ops->lib->fill_rxdone(entry, &desc);
139 hdr = (struct ieee80211_hdr *)entry->data_addr;
140 header_size =
141 ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
144 * The data behind the ieee80211 header must be
145 * aligned on a 4 byte boundary.
147 align = header_size % 4;
150 * Allocate the sk_buffer, initialize it and copy
151 * all data into it.
153 skb = dev_alloc_skb(desc.size + align);
154 if (!skb)
155 return;
157 skb_reserve(skb, align);
158 memcpy(skb_put(skb, desc.size), entry->data_addr, desc.size);
161 * Send the frame to rt2x00lib for further processing.
163 rt2x00lib_rxdone(entry, skb, &desc);
165 if (test_bit(DEVICE_ENABLED_RADIO, &ring->rt2x00dev->flags)) {
166 rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
167 rt2x00_desc_write(rxd, 0, word);
170 rt2x00_ring_index_inc(ring);
173 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
175 void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct data_entry *entry,
176 const int tx_status, const int retry)
178 u32 word;
180 rt2x00lib_txdone(entry, tx_status, retry);
183 * Make this entry available for reuse.
185 entry->flags = 0;
187 rt2x00_desc_read(entry->priv, 0, &word);
188 rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
189 rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
190 rt2x00_desc_write(entry->priv, 0, word);
192 rt2x00_ring_index_done_inc(entry->ring);
195 * If the data ring was full before the txdone handler
196 * we must make sure the packet queue in the mac80211 stack
197 * is reenabled when the txdone handler has finished.
199 if (!rt2x00_ring_full(entry->ring))
200 ieee80211_wake_queue(rt2x00dev->hw,
201 entry->tx_status.control.queue);
204 EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
207 * Device initialization handlers.
209 #define priv_offset(__ring, __i) \
210 ({ \
211 ring->data_addr + (i * ring->desc_size); \
214 #define data_addr_offset(__ring, __i) \
215 ({ \
216 (__ring)->data_addr + \
217 ((__ring)->stats.limit * (__ring)->desc_size) + \
218 ((__i) * (__ring)->data_size); \
221 #define data_dma_offset(__ring, __i) \
222 ({ \
223 (__ring)->data_dma + \
224 ((__ring)->stats.limit * (__ring)->desc_size) + \
225 ((__i) * (__ring)->data_size); \
228 static int rt2x00pci_alloc_dma(struct rt2x00_dev *rt2x00dev,
229 struct data_ring *ring)
231 unsigned int i;
234 * Allocate DMA memory for descriptor and buffer.
236 ring->data_addr = pci_alloc_consistent(rt2x00dev_pci(rt2x00dev),
237 rt2x00_get_ring_size(ring),
238 &ring->data_dma);
239 if (!ring->data_addr)
240 return -ENOMEM;
243 * Initialize all ring entries to contain valid
244 * addresses.
246 for (i = 0; i < ring->stats.limit; i++) {
247 ring->entry[i].priv = priv_offset(ring, i);
248 ring->entry[i].data_addr = data_addr_offset(ring, i);
249 ring->entry[i].data_dma = data_dma_offset(ring, i);
252 return 0;
255 static void rt2x00pci_free_dma(struct rt2x00_dev *rt2x00dev,
256 struct data_ring *ring)
258 if (ring->data_addr)
259 pci_free_consistent(rt2x00dev_pci(rt2x00dev),
260 rt2x00_get_ring_size(ring),
261 ring->data_addr, ring->data_dma);
262 ring->data_addr = NULL;
265 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
267 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
268 struct data_ring *ring;
269 int status;
272 * Allocate DMA
274 ring_for_each(rt2x00dev, ring) {
275 status = rt2x00pci_alloc_dma(rt2x00dev, ring);
276 if (status)
277 goto exit;
281 * Register interrupt handler.
283 status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
284 IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
285 if (status) {
286 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
287 pci_dev->irq, status);
288 return status;
291 return 0;
293 exit:
294 rt2x00pci_uninitialize(rt2x00dev);
296 return status;
298 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
300 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
302 struct data_ring *ring;
305 * Free irq line.
307 free_irq(rt2x00dev_pci(rt2x00dev)->irq, rt2x00dev);
310 * Free DMA
312 ring_for_each(rt2x00dev, ring)
313 rt2x00pci_free_dma(rt2x00dev, ring);
315 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
318 * PCI driver handlers.
320 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
322 kfree(rt2x00dev->rf);
323 rt2x00dev->rf = NULL;
325 kfree(rt2x00dev->eeprom);
326 rt2x00dev->eeprom = NULL;
328 if (rt2x00dev->csr_addr) {
329 iounmap(rt2x00dev->csr_addr);
330 rt2x00dev->csr_addr = NULL;
334 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
336 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
338 rt2x00dev->csr_addr = ioremap(pci_resource_start(pci_dev, 0),
339 pci_resource_len(pci_dev, 0));
340 if (!rt2x00dev->csr_addr)
341 goto exit;
343 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
344 if (!rt2x00dev->eeprom)
345 goto exit;
347 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
348 if (!rt2x00dev->rf)
349 goto exit;
351 return 0;
353 exit:
354 ERROR_PROBE("Failed to allocate registers.\n");
356 rt2x00pci_free_reg(rt2x00dev);
358 return -ENOMEM;
361 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
363 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
364 struct ieee80211_hw *hw;
365 struct rt2x00_dev *rt2x00dev;
366 int retval;
368 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
369 if (retval) {
370 ERROR_PROBE("PCI request regions failed.\n");
371 return retval;
374 retval = pci_enable_device(pci_dev);
375 if (retval) {
376 ERROR_PROBE("Enable device failed.\n");
377 goto exit_release_regions;
380 pci_set_master(pci_dev);
382 if (pci_set_mwi(pci_dev))
383 ERROR_PROBE("MWI not available.\n");
385 if (pci_set_dma_mask(pci_dev, DMA_64BIT_MASK) &&
386 pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)) {
387 ERROR_PROBE("PCI DMA not supported.\n");
388 retval = -EIO;
389 goto exit_disable_device;
392 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
393 if (!hw) {
394 ERROR_PROBE("Failed to allocate hardware.\n");
395 retval = -ENOMEM;
396 goto exit_disable_device;
399 pci_set_drvdata(pci_dev, hw);
401 rt2x00dev = hw->priv;
402 rt2x00dev->dev = pci_dev;
403 rt2x00dev->ops = ops;
404 rt2x00dev->hw = hw;
406 retval = rt2x00pci_alloc_reg(rt2x00dev);
407 if (retval)
408 goto exit_free_device;
410 retval = rt2x00lib_probe_dev(rt2x00dev);
411 if (retval)
412 goto exit_free_reg;
414 return 0;
416 exit_free_reg:
417 rt2x00pci_free_reg(rt2x00dev);
419 exit_free_device:
420 ieee80211_free_hw(hw);
422 exit_disable_device:
423 if (retval != -EBUSY)
424 pci_disable_device(pci_dev);
426 exit_release_regions:
427 pci_release_regions(pci_dev);
429 pci_set_drvdata(pci_dev, NULL);
431 return retval;
433 EXPORT_SYMBOL_GPL(rt2x00pci_probe);
435 void rt2x00pci_remove(struct pci_dev *pci_dev)
437 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
438 struct rt2x00_dev *rt2x00dev = hw->priv;
441 * Free all allocated data.
443 rt2x00lib_remove_dev(rt2x00dev);
444 rt2x00pci_free_reg(rt2x00dev);
445 ieee80211_free_hw(hw);
448 * Free the PCI device data.
450 pci_set_drvdata(pci_dev, NULL);
451 pci_disable_device(pci_dev);
452 pci_release_regions(pci_dev);
454 EXPORT_SYMBOL_GPL(rt2x00pci_remove);
456 #ifdef CONFIG_PM
457 int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
459 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
460 struct rt2x00_dev *rt2x00dev = hw->priv;
461 int retval;
463 retval = rt2x00lib_suspend(rt2x00dev, state);
464 if (retval)
465 return retval;
467 rt2x00pci_free_reg(rt2x00dev);
469 pci_save_state(pci_dev);
470 pci_disable_device(pci_dev);
471 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
473 EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
475 int rt2x00pci_resume(struct pci_dev *pci_dev)
477 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
478 struct rt2x00_dev *rt2x00dev = hw->priv;
479 int retval;
481 if (pci_set_power_state(pci_dev, PCI_D0) ||
482 pci_enable_device(pci_dev) ||
483 pci_restore_state(pci_dev)) {
484 ERROR(rt2x00dev, "Failed to resume device.\n");
485 return -EIO;
488 retval = rt2x00pci_alloc_reg(rt2x00dev);
489 if (retval)
490 return retval;
492 retval = rt2x00lib_resume(rt2x00dev);
493 if (retval)
494 goto exit_free_reg;
496 return 0;
498 exit_free_reg:
499 rt2x00pci_free_reg(rt2x00dev);
501 return retval;
503 EXPORT_SYMBOL_GPL(rt2x00pci_resume);
504 #endif /* CONFIG_PM */
507 * rt2x00pci module information.
509 MODULE_AUTHOR(DRV_PROJECT);
510 MODULE_VERSION(DRV_VERSION);
511 MODULE_DESCRIPTION("rt2x00 library");
512 MODULE_LICENSE("GPL");