2 Copyright (C) 2004 - 2008 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.
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>
32 #include "rt2x00pci.h"
37 int rt2x00pci_write_tx_data(struct queue_entry *entry)
39 struct queue_entry_priv_pci *entry_priv = entry->priv_data;
40 struct skb_frame_desc *skbdesc;
43 rt2x00_desc_read(entry_priv->desc, 0, &word);
46 * This should not happen, we already checked the entry
47 * was ours. When the hardware disagrees there has been
50 if (unlikely(rt2x00_get_field32(word, TXD_ENTRY_OWNER_NIC) ||
51 rt2x00_get_field32(word, TXD_ENTRY_VALID))) {
52 ERROR(entry->queue->rt2x00dev,
53 "Corrupt queue %d, accessing entry which is not ours.\n"
54 "Please file bug report to %s.\n",
55 entry->queue->qid, DRV_PROJECT);
60 * Fill in skb descriptor
62 skbdesc = get_skb_frame_desc(entry->skb);
63 memset(skbdesc, 0, sizeof(*skbdesc));
64 skbdesc->desc = entry_priv->desc;
65 skbdesc->desc_len = entry->queue->desc_size;
66 skbdesc->entry = entry;
68 rt2x00queue_map_txskb(entry->queue->rt2x00dev, entry->skb);
72 EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
75 * TX/RX data handlers.
77 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
79 struct data_queue *queue = rt2x00dev->rx;
80 struct queue_entry *entry;
81 struct queue_entry_priv_pci *entry_priv;
82 struct skb_frame_desc *skbdesc;
86 entry = rt2x00queue_get_entry(queue, Q_INDEX);
87 entry_priv = entry->priv_data;
88 rt2x00_desc_read(entry_priv->desc, 0, &word);
90 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
94 * Fill in desc fields of the skb descriptor
96 skbdesc = get_skb_frame_desc(entry->skb);
97 skbdesc->desc = entry_priv->desc;
98 skbdesc->desc_len = entry->queue->desc_size;
101 * Send the frame to rt2x00lib for further processing.
103 rt2x00lib_rxdone(rt2x00dev, entry);
106 * Reset the RXD for this entry.
108 rt2x00dev->ops->lib->init_rxentry(rt2x00dev, entry);
110 rt2x00queue_index_inc(queue, Q_INDEX);
113 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
115 void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct queue_entry *entry,
116 struct txdone_entry_desc *txdesc)
118 struct queue_entry_priv_pci *entry_priv = entry->priv_data;
119 enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
125 rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
127 rt2x00lib_txdone(entry, txdesc);
130 * Make this entry available for reuse.
134 rt2x00_desc_read(entry_priv->desc, 0, &word);
135 rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
136 rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
137 rt2x00_desc_write(entry_priv->desc, 0, word);
139 __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
140 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
143 * If the data queue was below the threshold before the txdone
144 * handler we must make sure the packet queue in the mac80211 stack
145 * is reenabled when the txdone handler has finished.
147 if (!rt2x00queue_threshold(entry->queue))
148 ieee80211_wake_queue(rt2x00dev->hw, qid);
151 EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
154 * Device initialization handlers.
156 static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
157 struct data_queue *queue)
159 struct queue_entry_priv_pci *entry_priv;
165 * Allocate DMA memory for descriptor and buffer.
167 addr = dma_alloc_coherent(rt2x00dev->dev,
168 queue->limit * queue->desc_size,
169 &dma, GFP_KERNEL | GFP_DMA);
173 memset(addr, 0, queue->limit * queue->desc_size);
176 * Initialize all queue entries to contain valid addresses.
178 for (i = 0; i < queue->limit; i++) {
179 entry_priv = queue->entries[i].priv_data;
180 entry_priv->desc = addr + i * queue->desc_size;
181 entry_priv->desc_dma = dma + i * queue->desc_size;
187 static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
188 struct data_queue *queue)
190 struct queue_entry_priv_pci *entry_priv =
191 queue->entries[0].priv_data;
193 if (entry_priv->desc)
194 dma_free_coherent(rt2x00dev->dev,
195 queue->limit * queue->desc_size,
196 entry_priv->desc, entry_priv->desc_dma);
197 entry_priv->desc = NULL;
200 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
202 struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
203 struct data_queue *queue;
209 queue_for_each(rt2x00dev, queue) {
210 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
216 * Register interrupt handler.
218 status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
219 IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
221 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
222 pci_dev->irq, status);
229 queue_for_each(rt2x00dev, queue)
230 rt2x00pci_free_queue_dma(rt2x00dev, queue);
234 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
236 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
238 struct data_queue *queue;
243 free_irq(to_pci_dev(rt2x00dev->dev)->irq, rt2x00dev);
248 queue_for_each(rt2x00dev, queue)
249 rt2x00pci_free_queue_dma(rt2x00dev, queue);
251 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
254 * PCI driver handlers.
256 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
258 kfree(rt2x00dev->rf);
259 rt2x00dev->rf = NULL;
261 kfree(rt2x00dev->eeprom);
262 rt2x00dev->eeprom = NULL;
264 if (rt2x00dev->csr.base) {
265 iounmap(rt2x00dev->csr.base);
266 rt2x00dev->csr.base = NULL;
270 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
272 struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
274 rt2x00dev->csr.base = ioremap(pci_resource_start(pci_dev, 0),
275 pci_resource_len(pci_dev, 0));
276 if (!rt2x00dev->csr.base)
279 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
280 if (!rt2x00dev->eeprom)
283 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
290 ERROR_PROBE("Failed to allocate registers.\n");
292 rt2x00pci_free_reg(rt2x00dev);
297 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
299 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
300 struct ieee80211_hw *hw;
301 struct rt2x00_dev *rt2x00dev;
304 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
306 ERROR_PROBE("PCI request regions failed.\n");
310 retval = pci_enable_device(pci_dev);
312 ERROR_PROBE("Enable device failed.\n");
313 goto exit_release_regions;
316 pci_set_master(pci_dev);
318 if (pci_set_mwi(pci_dev))
319 ERROR_PROBE("MWI not available.\n");
321 if (dma_set_mask(&pci_dev->dev, DMA_32BIT_MASK)) {
322 ERROR_PROBE("PCI DMA not supported.\n");
324 goto exit_disable_device;
327 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
329 ERROR_PROBE("Failed to allocate hardware.\n");
331 goto exit_disable_device;
334 pci_set_drvdata(pci_dev, hw);
336 rt2x00dev = hw->priv;
337 rt2x00dev->dev = &pci_dev->dev;
338 rt2x00dev->ops = ops;
341 retval = rt2x00pci_alloc_reg(rt2x00dev);
343 goto exit_free_device;
345 retval = rt2x00lib_probe_dev(rt2x00dev);
352 rt2x00pci_free_reg(rt2x00dev);
355 ieee80211_free_hw(hw);
358 if (retval != -EBUSY)
359 pci_disable_device(pci_dev);
361 exit_release_regions:
362 pci_release_regions(pci_dev);
364 pci_set_drvdata(pci_dev, NULL);
368 EXPORT_SYMBOL_GPL(rt2x00pci_probe);
370 void rt2x00pci_remove(struct pci_dev *pci_dev)
372 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
373 struct rt2x00_dev *rt2x00dev = hw->priv;
376 * Free all allocated data.
378 rt2x00lib_remove_dev(rt2x00dev);
379 rt2x00pci_free_reg(rt2x00dev);
380 ieee80211_free_hw(hw);
383 * Free the PCI device data.
385 pci_set_drvdata(pci_dev, NULL);
386 pci_disable_device(pci_dev);
387 pci_release_regions(pci_dev);
389 EXPORT_SYMBOL_GPL(rt2x00pci_remove);
392 int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
394 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
395 struct rt2x00_dev *rt2x00dev = hw->priv;
398 retval = rt2x00lib_suspend(rt2x00dev, state);
402 rt2x00pci_free_reg(rt2x00dev);
404 pci_save_state(pci_dev);
405 pci_disable_device(pci_dev);
406 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
408 EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
410 int rt2x00pci_resume(struct pci_dev *pci_dev)
412 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
413 struct rt2x00_dev *rt2x00dev = hw->priv;
416 if (pci_set_power_state(pci_dev, PCI_D0) ||
417 pci_enable_device(pci_dev) ||
418 pci_restore_state(pci_dev)) {
419 ERROR(rt2x00dev, "Failed to resume device.\n");
423 retval = rt2x00pci_alloc_reg(rt2x00dev);
427 retval = rt2x00lib_resume(rt2x00dev);
434 rt2x00pci_free_reg(rt2x00dev);
438 EXPORT_SYMBOL_GPL(rt2x00pci_resume);
439 #endif /* CONFIG_PM */
442 * rt2x00pci module information.
444 MODULE_AUTHOR(DRV_PROJECT);
445 MODULE_VERSION(DRV_VERSION);
446 MODULE_DESCRIPTION("rt2x00 pci library");
447 MODULE_LICENSE("GPL");