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->data = entry->skb->data;
65 skbdesc->data_len = entry->skb->len;
66 skbdesc->desc = entry_priv->desc;
67 skbdesc->desc_len = entry->queue->desc_size;
68 skbdesc->entry = entry;
70 memcpy(entry_priv->data, entry->skb->data, entry->skb->len);
74 EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
77 * TX/RX data handlers.
79 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
81 struct data_queue *queue = rt2x00dev->rx;
82 struct queue_entry *entry;
83 struct queue_entry_priv_pci *entry_priv;
84 struct ieee80211_hdr *hdr;
85 struct skb_frame_desc *skbdesc;
86 struct rxdone_entry_desc rxdesc;
92 entry = rt2x00queue_get_entry(queue, Q_INDEX);
93 entry_priv = entry->priv_data;
94 rt2x00_desc_read(entry_priv->desc, 0, &word);
96 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
99 memset(&rxdesc, 0, sizeof(rxdesc));
100 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
102 hdr = (struct ieee80211_hdr *)entry_priv->data;
104 ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
107 * The data behind the ieee80211 header must be
108 * aligned on a 4 byte boundary.
110 align = header_size % 4;
113 * Allocate the sk_buffer, initialize it and copy
116 entry->skb = dev_alloc_skb(rxdesc.size + align);
120 skb_reserve(entry->skb, align);
121 memcpy(skb_put(entry->skb, rxdesc.size),
122 entry_priv->data, rxdesc.size);
125 * Fill in skb descriptor
127 skbdesc = get_skb_frame_desc(entry->skb);
128 memset(skbdesc, 0, sizeof(*skbdesc));
129 skbdesc->data = entry->skb->data;
130 skbdesc->data_len = entry->skb->len;
131 skbdesc->desc = entry_priv->desc;
132 skbdesc->desc_len = queue->desc_size;
133 skbdesc->entry = entry;
136 * Send the frame to rt2x00lib for further processing.
138 rt2x00lib_rxdone(entry, &rxdesc);
140 if (test_bit(DEVICE_ENABLED_RADIO, &queue->rt2x00dev->flags)) {
141 rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
142 rt2x00_desc_write(entry_priv->desc, 0, word);
145 rt2x00queue_index_inc(queue, Q_INDEX);
148 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
150 void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct queue_entry *entry,
151 struct txdone_entry_desc *txdesc)
153 struct queue_entry_priv_pci *entry_priv = entry->priv_data;
154 enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
157 rt2x00lib_txdone(entry, txdesc);
160 * Make this entry available for reuse.
164 rt2x00_desc_read(entry_priv->desc, 0, &word);
165 rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
166 rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
167 rt2x00_desc_write(entry_priv->desc, 0, word);
169 __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
170 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
173 * If the data queue was below the threshold before the txdone
174 * handler we must make sure the packet queue in the mac80211 stack
175 * is reenabled when the txdone handler has finished.
177 if (!rt2x00queue_threshold(entry->queue))
178 ieee80211_wake_queue(rt2x00dev->hw, qid);
181 EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
184 * Device initialization handlers.
186 #define desc_size(__queue) \
188 ((__queue)->limit * (__queue)->desc_size);\
191 #define data_size(__queue) \
193 ((__queue)->limit * (__queue)->data_size);\
196 #define dma_size(__queue) \
198 data_size(__queue) + desc_size(__queue);\
201 #define desc_offset(__queue, __base, __i) \
203 (__base) + data_size(__queue) + \
204 ((__i) * (__queue)->desc_size); \
207 #define data_offset(__queue, __base, __i) \
210 ((__i) * (__queue)->data_size); \
213 static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
214 struct data_queue *queue)
216 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
217 struct queue_entry_priv_pci *entry_priv;
223 * Allocate DMA memory for descriptor and buffer.
225 addr = pci_alloc_consistent(pci_dev, dma_size(queue), &dma);
229 memset(addr, 0, dma_size(queue));
232 * Initialize all queue entries to contain valid addresses.
234 for (i = 0; i < queue->limit; i++) {
235 entry_priv = queue->entries[i].priv_data;
236 entry_priv->desc = desc_offset(queue, addr, i);
237 entry_priv->desc_dma = desc_offset(queue, dma, i);
238 entry_priv->data = data_offset(queue, addr, i);
239 entry_priv->data_dma = data_offset(queue, dma, i);
245 static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
246 struct data_queue *queue)
248 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
249 struct queue_entry_priv_pci *entry_priv =
250 queue->entries[0].priv_data;
252 if (entry_priv->data)
253 pci_free_consistent(pci_dev, dma_size(queue),
254 entry_priv->data, entry_priv->data_dma);
255 entry_priv->data = NULL;
258 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
260 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
261 struct data_queue *queue;
267 queue_for_each(rt2x00dev, queue) {
268 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
274 * Register interrupt handler.
276 status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
277 IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
279 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
280 pci_dev->irq, status);
287 queue_for_each(rt2x00dev, queue)
288 rt2x00pci_free_queue_dma(rt2x00dev, queue);
292 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
294 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
296 struct data_queue *queue;
301 free_irq(rt2x00dev_pci(rt2x00dev)->irq, rt2x00dev);
306 queue_for_each(rt2x00dev, queue)
307 rt2x00pci_free_queue_dma(rt2x00dev, queue);
309 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
312 * PCI driver handlers.
314 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
316 kfree(rt2x00dev->rf);
317 rt2x00dev->rf = NULL;
319 kfree(rt2x00dev->eeprom);
320 rt2x00dev->eeprom = NULL;
322 if (rt2x00dev->csr.base) {
323 iounmap(rt2x00dev->csr.base);
324 rt2x00dev->csr.base = NULL;
328 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
330 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
332 rt2x00dev->csr.base = ioremap(pci_resource_start(pci_dev, 0),
333 pci_resource_len(pci_dev, 0));
334 if (!rt2x00dev->csr.base)
337 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
338 if (!rt2x00dev->eeprom)
341 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
348 ERROR_PROBE("Failed to allocate registers.\n");
350 rt2x00pci_free_reg(rt2x00dev);
355 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
357 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
358 struct ieee80211_hw *hw;
359 struct rt2x00_dev *rt2x00dev;
362 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
364 ERROR_PROBE("PCI request regions failed.\n");
368 retval = pci_enable_device(pci_dev);
370 ERROR_PROBE("Enable device failed.\n");
371 goto exit_release_regions;
374 pci_set_master(pci_dev);
376 if (pci_set_mwi(pci_dev))
377 ERROR_PROBE("MWI not available.\n");
379 if (pci_set_dma_mask(pci_dev, DMA_64BIT_MASK) &&
380 pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)) {
381 ERROR_PROBE("PCI DMA not supported.\n");
383 goto exit_disable_device;
386 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
388 ERROR_PROBE("Failed to allocate hardware.\n");
390 goto exit_disable_device;
393 pci_set_drvdata(pci_dev, hw);
395 rt2x00dev = hw->priv;
396 rt2x00dev->dev = pci_dev;
397 rt2x00dev->ops = ops;
400 retval = rt2x00pci_alloc_reg(rt2x00dev);
402 goto exit_free_device;
404 retval = rt2x00lib_probe_dev(rt2x00dev);
411 rt2x00pci_free_reg(rt2x00dev);
414 ieee80211_free_hw(hw);
417 if (retval != -EBUSY)
418 pci_disable_device(pci_dev);
420 exit_release_regions:
421 pci_release_regions(pci_dev);
423 pci_set_drvdata(pci_dev, NULL);
427 EXPORT_SYMBOL_GPL(rt2x00pci_probe);
429 void rt2x00pci_remove(struct pci_dev *pci_dev)
431 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
432 struct rt2x00_dev *rt2x00dev = hw->priv;
435 * Free all allocated data.
437 rt2x00lib_remove_dev(rt2x00dev);
438 rt2x00pci_free_reg(rt2x00dev);
439 ieee80211_free_hw(hw);
442 * Free the PCI device data.
444 pci_set_drvdata(pci_dev, NULL);
445 pci_disable_device(pci_dev);
446 pci_release_regions(pci_dev);
448 EXPORT_SYMBOL_GPL(rt2x00pci_remove);
451 int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
453 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
454 struct rt2x00_dev *rt2x00dev = hw->priv;
457 retval = rt2x00lib_suspend(rt2x00dev, state);
461 rt2x00pci_free_reg(rt2x00dev);
463 pci_save_state(pci_dev);
464 pci_disable_device(pci_dev);
465 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
467 EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
469 int rt2x00pci_resume(struct pci_dev *pci_dev)
471 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
472 struct rt2x00_dev *rt2x00dev = hw->priv;
475 if (pci_set_power_state(pci_dev, PCI_D0) ||
476 pci_enable_device(pci_dev) ||
477 pci_restore_state(pci_dev)) {
478 ERROR(rt2x00dev, "Failed to resume device.\n");
482 retval = rt2x00pci_alloc_reg(rt2x00dev);
486 retval = rt2x00lib_resume(rt2x00dev);
493 rt2x00pci_free_reg(rt2x00dev);
497 EXPORT_SYMBOL_GPL(rt2x00pci_resume);
498 #endif /* CONFIG_PM */
501 * rt2x00pci module information.
503 MODULE_AUTHOR(DRV_PROJECT);
504 MODULE_VERSION(DRV_VERSION);
505 MODULE_DESCRIPTION("rt2x00 pci library");
506 MODULE_LICENSE("GPL");