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 rt2x00_dev *rt2x00dev,
38 struct data_queue *queue, struct sk_buff *skb)
40 struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
41 struct queue_entry_priv_pci *entry_priv = entry->priv_data;
42 struct skb_frame_desc *skbdesc;
43 struct txentry_desc txdesc;
46 if (rt2x00queue_full(queue))
49 rt2x00_desc_read(entry_priv->desc, 0, &word);
51 if (rt2x00_get_field32(word, TXD_ENTRY_OWNER_NIC) ||
52 rt2x00_get_field32(word, TXD_ENTRY_VALID)) {
54 "Arrived at non-free entry in the non-full queue %d.\n"
55 "Please file bug report to %s.\n",
56 entry->queue->qid, DRV_PROJECT);
61 * Copy all TX descriptor information into txdesc,
62 * after that we are free to use the skb->cb array
63 * for our information.
66 rt2x00queue_create_tx_descriptor(entry, &txdesc);
69 * Fill in skb descriptor
71 skbdesc = get_skb_frame_desc(skb);
72 memset(skbdesc, 0, sizeof(*skbdesc));
73 skbdesc->data = skb->data;
74 skbdesc->data_len = skb->len;
75 skbdesc->desc = entry_priv->desc;
76 skbdesc->desc_len = queue->desc_size;
77 skbdesc->entry = entry;
79 memcpy(entry_priv->data, skb->data, skb->len);
81 rt2x00queue_write_tx_descriptor(entry, &txdesc);
82 rt2x00queue_index_inc(queue, Q_INDEX);
86 EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
89 * TX/RX data handlers.
91 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
93 struct data_queue *queue = rt2x00dev->rx;
94 struct queue_entry *entry;
95 struct queue_entry_priv_pci *entry_priv;
96 struct ieee80211_hdr *hdr;
97 struct skb_frame_desc *skbdesc;
98 struct rxdone_entry_desc rxdesc;
104 entry = rt2x00queue_get_entry(queue, Q_INDEX);
105 entry_priv = entry->priv_data;
106 rt2x00_desc_read(entry_priv->desc, 0, &word);
108 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
111 memset(&rxdesc, 0, sizeof(rxdesc));
112 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
114 hdr = (struct ieee80211_hdr *)entry_priv->data;
116 ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
119 * The data behind the ieee80211 header must be
120 * aligned on a 4 byte boundary.
122 align = header_size % 4;
125 * Allocate the sk_buffer, initialize it and copy
128 entry->skb = dev_alloc_skb(rxdesc.size + align);
132 skb_reserve(entry->skb, align);
133 memcpy(skb_put(entry->skb, rxdesc.size),
134 entry_priv->data, rxdesc.size);
137 * Fill in skb descriptor
139 skbdesc = get_skb_frame_desc(entry->skb);
140 memset(skbdesc, 0, sizeof(*skbdesc));
141 skbdesc->data = entry->skb->data;
142 skbdesc->data_len = entry->skb->len;
143 skbdesc->desc = entry_priv->desc;
144 skbdesc->desc_len = queue->desc_size;
145 skbdesc->entry = entry;
148 * Send the frame to rt2x00lib for further processing.
150 rt2x00lib_rxdone(entry, &rxdesc);
152 if (test_bit(DEVICE_ENABLED_RADIO, &queue->rt2x00dev->flags)) {
153 rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
154 rt2x00_desc_write(entry_priv->desc, 0, word);
157 rt2x00queue_index_inc(queue, Q_INDEX);
160 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
162 void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct queue_entry *entry,
163 struct txdone_entry_desc *txdesc)
165 struct queue_entry_priv_pci *entry_priv = entry->priv_data;
166 enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
169 rt2x00lib_txdone(entry, txdesc);
172 * Make this entry available for reuse.
176 rt2x00_desc_read(entry_priv->desc, 0, &word);
177 rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
178 rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
179 rt2x00_desc_write(entry_priv->desc, 0, word);
181 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
184 * If the data queue was full before the txdone handler
185 * we must make sure the packet queue in the mac80211 stack
186 * is reenabled when the txdone handler has finished.
188 if (!rt2x00queue_full(entry->queue))
189 ieee80211_wake_queue(rt2x00dev->hw, qid);
192 EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
195 * Device initialization handlers.
197 #define desc_size(__queue) \
199 ((__queue)->limit * (__queue)->desc_size);\
202 #define data_size(__queue) \
204 ((__queue)->limit * (__queue)->data_size);\
207 #define dma_size(__queue) \
209 data_size(__queue) + desc_size(__queue);\
212 #define desc_offset(__queue, __base, __i) \
214 (__base) + data_size(__queue) + \
215 ((__i) * (__queue)->desc_size); \
218 #define data_offset(__queue, __base, __i) \
221 ((__i) * (__queue)->data_size); \
224 static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
225 struct data_queue *queue)
227 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
228 struct queue_entry_priv_pci *entry_priv;
234 * Allocate DMA memory for descriptor and buffer.
236 addr = pci_alloc_consistent(pci_dev, dma_size(queue), &dma);
240 memset(addr, 0, dma_size(queue));
243 * Initialize all queue entries to contain valid addresses.
245 for (i = 0; i < queue->limit; i++) {
246 entry_priv = queue->entries[i].priv_data;
247 entry_priv->desc = desc_offset(queue, addr, i);
248 entry_priv->desc_dma = desc_offset(queue, dma, i);
249 entry_priv->data = data_offset(queue, addr, i);
250 entry_priv->data_dma = data_offset(queue, dma, i);
256 static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
257 struct data_queue *queue)
259 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
260 struct queue_entry_priv_pci *entry_priv =
261 queue->entries[0].priv_data;
263 if (entry_priv->data)
264 pci_free_consistent(pci_dev, dma_size(queue),
265 entry_priv->data, entry_priv->data_dma);
266 entry_priv->data = NULL;
269 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
271 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
272 struct data_queue *queue;
278 queue_for_each(rt2x00dev, queue) {
279 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
285 * Register interrupt handler.
287 status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
288 IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
290 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
291 pci_dev->irq, status);
298 queue_for_each(rt2x00dev, queue)
299 rt2x00pci_free_queue_dma(rt2x00dev, queue);
303 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
305 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
307 struct data_queue *queue;
312 free_irq(rt2x00dev_pci(rt2x00dev)->irq, rt2x00dev);
317 queue_for_each(rt2x00dev, queue)
318 rt2x00pci_free_queue_dma(rt2x00dev, queue);
320 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
323 * PCI driver handlers.
325 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
327 kfree(rt2x00dev->rf);
328 rt2x00dev->rf = NULL;
330 kfree(rt2x00dev->eeprom);
331 rt2x00dev->eeprom = NULL;
333 if (rt2x00dev->csr.base) {
334 iounmap(rt2x00dev->csr.base);
335 rt2x00dev->csr.base = NULL;
339 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
341 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
343 rt2x00dev->csr.base = ioremap(pci_resource_start(pci_dev, 0),
344 pci_resource_len(pci_dev, 0));
345 if (!rt2x00dev->csr.base)
348 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
349 if (!rt2x00dev->eeprom)
352 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
359 ERROR_PROBE("Failed to allocate registers.\n");
361 rt2x00pci_free_reg(rt2x00dev);
366 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
368 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
369 struct ieee80211_hw *hw;
370 struct rt2x00_dev *rt2x00dev;
373 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
375 ERROR_PROBE("PCI request regions failed.\n");
379 retval = pci_enable_device(pci_dev);
381 ERROR_PROBE("Enable device failed.\n");
382 goto exit_release_regions;
385 pci_set_master(pci_dev);
387 if (pci_set_mwi(pci_dev))
388 ERROR_PROBE("MWI not available.\n");
390 if (pci_set_dma_mask(pci_dev, DMA_64BIT_MASK) &&
391 pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)) {
392 ERROR_PROBE("PCI DMA not supported.\n");
394 goto exit_disable_device;
397 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
399 ERROR_PROBE("Failed to allocate hardware.\n");
401 goto exit_disable_device;
404 pci_set_drvdata(pci_dev, hw);
406 rt2x00dev = hw->priv;
407 rt2x00dev->dev = pci_dev;
408 rt2x00dev->ops = ops;
411 retval = rt2x00pci_alloc_reg(rt2x00dev);
413 goto exit_free_device;
415 retval = rt2x00lib_probe_dev(rt2x00dev);
422 rt2x00pci_free_reg(rt2x00dev);
425 ieee80211_free_hw(hw);
428 if (retval != -EBUSY)
429 pci_disable_device(pci_dev);
431 exit_release_regions:
432 pci_release_regions(pci_dev);
434 pci_set_drvdata(pci_dev, NULL);
438 EXPORT_SYMBOL_GPL(rt2x00pci_probe);
440 void rt2x00pci_remove(struct pci_dev *pci_dev)
442 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
443 struct rt2x00_dev *rt2x00dev = hw->priv;
446 * Free all allocated data.
448 rt2x00lib_remove_dev(rt2x00dev);
449 rt2x00pci_free_reg(rt2x00dev);
450 ieee80211_free_hw(hw);
453 * Free the PCI device data.
455 pci_set_drvdata(pci_dev, NULL);
456 pci_disable_device(pci_dev);
457 pci_release_regions(pci_dev);
459 EXPORT_SYMBOL_GPL(rt2x00pci_remove);
462 int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
464 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
465 struct rt2x00_dev *rt2x00dev = hw->priv;
468 retval = rt2x00lib_suspend(rt2x00dev, state);
472 rt2x00pci_free_reg(rt2x00dev);
474 pci_save_state(pci_dev);
475 pci_disable_device(pci_dev);
476 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
478 EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
480 int rt2x00pci_resume(struct pci_dev *pci_dev)
482 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
483 struct rt2x00_dev *rt2x00dev = hw->priv;
486 if (pci_set_power_state(pci_dev, PCI_D0) ||
487 pci_enable_device(pci_dev) ||
488 pci_restore_state(pci_dev)) {
489 ERROR(rt2x00dev, "Failed to resume device.\n");
493 retval = rt2x00pci_alloc_reg(rt2x00dev);
497 retval = rt2x00lib_resume(rt2x00dev);
504 rt2x00pci_free_reg(rt2x00dev);
508 EXPORT_SYMBOL_GPL(rt2x00pci_resume);
509 #endif /* CONFIG_PM */
512 * rt2x00pci module information.
514 MODULE_AUTHOR(DRV_PROJECT);
515 MODULE_VERSION(DRV_VERSION);
516 MODULE_DESCRIPTION("rt2x00 pci library");
517 MODULE_LICENSE("GPL");