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,
39 struct ieee80211_tx_control *control)
41 struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
42 struct queue_entry_priv_pci_tx *priv_tx = entry->priv_data;
43 struct skb_frame_desc *skbdesc;
46 if (rt2x00queue_full(queue))
49 rt2x00_desc_read(priv_tx->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 control->queue, DRV_PROJECT);
61 * Fill in skb descriptor
63 skbdesc = get_skb_frame_desc(skb);
64 skbdesc->data = skb->data;
65 skbdesc->data_len = skb->len;
66 skbdesc->desc = priv_tx->desc;
67 skbdesc->desc_len = queue->desc_size;
68 skbdesc->entry = entry;
70 memcpy(&priv_tx->control, control, sizeof(priv_tx->control));
71 memcpy(priv_tx->data, skb->data, skb->len);
72 rt2x00lib_write_tx_desc(rt2x00dev, skb, control);
74 rt2x00queue_index_inc(queue, Q_INDEX);
78 EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
81 * TX/RX data handlers.
83 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
85 struct data_queue *queue = rt2x00dev->rx;
86 struct queue_entry *entry;
87 struct queue_entry_priv_pci_rx *priv_rx;
88 struct ieee80211_hdr *hdr;
89 struct skb_frame_desc *skbdesc;
90 struct rxdone_entry_desc rxdesc;
96 entry = rt2x00queue_get_entry(queue, Q_INDEX);
97 priv_rx = entry->priv_data;
98 rt2x00_desc_read(priv_rx->desc, 0, &word);
100 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
103 memset(&rxdesc, 0, sizeof(rxdesc));
104 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
106 hdr = (struct ieee80211_hdr *)priv_rx->data;
108 ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
111 * The data behind the ieee80211 header must be
112 * aligned on a 4 byte boundary.
114 align = header_size % 4;
117 * Allocate the sk_buffer, initialize it and copy
120 entry->skb = dev_alloc_skb(rxdesc.size + align);
124 skb_reserve(entry->skb, align);
125 memcpy(skb_put(entry->skb, rxdesc.size),
126 priv_rx->data, rxdesc.size);
129 * Fill in skb descriptor
131 skbdesc = get_skb_frame_desc(entry->skb);
132 memset(skbdesc, 0, sizeof(*skbdesc));
133 skbdesc->data = entry->skb->data;
134 skbdesc->data_len = entry->skb->len;
135 skbdesc->desc = priv_rx->desc;
136 skbdesc->desc_len = queue->desc_size;
137 skbdesc->entry = entry;
140 * Send the frame to rt2x00lib for further processing.
142 rt2x00lib_rxdone(entry, &rxdesc);
144 if (test_bit(DEVICE_ENABLED_RADIO, &queue->rt2x00dev->flags)) {
145 rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
146 rt2x00_desc_write(priv_rx->desc, 0, word);
149 rt2x00queue_index_inc(queue, Q_INDEX);
152 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
154 void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct queue_entry *entry,
155 struct txdone_entry_desc *txdesc)
157 struct queue_entry_priv_pci_tx *priv_tx = entry->priv_data;
160 txdesc->control = &priv_tx->control;
161 rt2x00lib_txdone(entry, txdesc);
164 * Make this entry available for reuse.
168 rt2x00_desc_read(priv_tx->desc, 0, &word);
169 rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
170 rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
171 rt2x00_desc_write(priv_tx->desc, 0, word);
173 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
176 * If the data queue was full before the txdone handler
177 * we must make sure the packet queue in the mac80211 stack
178 * is reenabled when the txdone handler has finished.
180 if (!rt2x00queue_full(entry->queue))
181 ieee80211_wake_queue(rt2x00dev->hw, priv_tx->control.queue);
184 EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
187 * Device initialization handlers.
189 #define desc_size(__queue) \
191 ((__queue)->limit * (__queue)->desc_size);\
194 #define data_size(__queue) \
196 ((__queue)->limit * (__queue)->data_size);\
199 #define dma_size(__queue) \
201 data_size(__queue) + desc_size(__queue);\
204 #define desc_offset(__queue, __base, __i) \
206 (__base) + data_size(__queue) + \
207 ((__i) * (__queue)->desc_size); \
210 #define data_offset(__queue, __base, __i) \
213 ((__i) * (__queue)->data_size); \
216 static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
217 struct data_queue *queue)
219 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
220 struct queue_entry_priv_pci_rx *priv_rx;
221 struct queue_entry_priv_pci_tx *priv_tx;
231 * Allocate DMA memory for descriptor and buffer.
233 addr = pci_alloc_consistent(pci_dev, dma_size(queue), &dma);
237 memset(addr, 0, dma_size(queue));
240 * Initialize all queue entries to contain valid addresses.
242 for (i = 0; i < queue->limit; i++) {
243 desc_addr = desc_offset(queue, addr, i);
244 desc_dma = desc_offset(queue, dma, i);
245 data_addr = data_offset(queue, addr, i);
246 data_dma = data_offset(queue, dma, i);
248 if (queue->qid == QID_RX) {
249 priv_rx = queue->entries[i].priv_data;
250 priv_rx->desc = desc_addr;
251 priv_rx->desc_dma = desc_dma;
252 priv_rx->data = data_addr;
253 priv_rx->data_dma = data_dma;
255 priv_tx = queue->entries[i].priv_data;
256 priv_tx->desc = desc_addr;
257 priv_tx->desc_dma = desc_dma;
258 priv_tx->data = data_addr;
259 priv_tx->data_dma = data_dma;
266 static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
267 struct data_queue *queue)
269 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
270 struct queue_entry_priv_pci_rx *priv_rx;
271 struct queue_entry_priv_pci_tx *priv_tx;
275 if (queue->qid == QID_RX) {
276 priv_rx = queue->entries[0].priv_data;
277 data_addr = priv_rx->data;
278 data_dma = priv_rx->data_dma;
280 priv_rx->data = NULL;
282 priv_tx = queue->entries[0].priv_data;
283 data_addr = priv_tx->data;
284 data_dma = priv_tx->data_dma;
286 priv_tx->data = NULL;
290 pci_free_consistent(pci_dev, dma_size(queue),
291 data_addr, data_dma);
294 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
296 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
297 struct data_queue *queue;
303 queue_for_each(rt2x00dev, queue) {
304 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
310 * Register interrupt handler.
312 status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
313 IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
315 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
316 pci_dev->irq, status);
323 rt2x00pci_uninitialize(rt2x00dev);
327 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
329 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
331 struct data_queue *queue;
336 free_irq(rt2x00dev_pci(rt2x00dev)->irq, rt2x00dev);
341 queue_for_each(rt2x00dev, queue)
342 rt2x00pci_free_queue_dma(rt2x00dev, queue);
344 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
347 * PCI driver handlers.
349 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
351 kfree(rt2x00dev->rf);
352 rt2x00dev->rf = NULL;
354 kfree(rt2x00dev->eeprom);
355 rt2x00dev->eeprom = NULL;
357 if (rt2x00dev->csr.base) {
358 iounmap(rt2x00dev->csr.base);
359 rt2x00dev->csr.base = NULL;
363 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
365 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
367 rt2x00dev->csr.base = ioremap(pci_resource_start(pci_dev, 0),
368 pci_resource_len(pci_dev, 0));
369 if (!rt2x00dev->csr.base)
372 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
373 if (!rt2x00dev->eeprom)
376 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
383 ERROR_PROBE("Failed to allocate registers.\n");
385 rt2x00pci_free_reg(rt2x00dev);
390 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
392 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
393 struct ieee80211_hw *hw;
394 struct rt2x00_dev *rt2x00dev;
397 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
399 ERROR_PROBE("PCI request regions failed.\n");
403 retval = pci_enable_device(pci_dev);
405 ERROR_PROBE("Enable device failed.\n");
406 goto exit_release_regions;
409 pci_set_master(pci_dev);
411 if (pci_set_mwi(pci_dev))
412 ERROR_PROBE("MWI not available.\n");
414 if (pci_set_dma_mask(pci_dev, DMA_64BIT_MASK) &&
415 pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)) {
416 ERROR_PROBE("PCI DMA not supported.\n");
418 goto exit_disable_device;
421 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
423 ERROR_PROBE("Failed to allocate hardware.\n");
425 goto exit_disable_device;
428 pci_set_drvdata(pci_dev, hw);
430 rt2x00dev = hw->priv;
431 rt2x00dev->dev = pci_dev;
432 rt2x00dev->ops = ops;
435 retval = rt2x00pci_alloc_reg(rt2x00dev);
437 goto exit_free_device;
439 retval = rt2x00lib_probe_dev(rt2x00dev);
446 rt2x00pci_free_reg(rt2x00dev);
449 ieee80211_free_hw(hw);
452 if (retval != -EBUSY)
453 pci_disable_device(pci_dev);
455 exit_release_regions:
456 pci_release_regions(pci_dev);
458 pci_set_drvdata(pci_dev, NULL);
462 EXPORT_SYMBOL_GPL(rt2x00pci_probe);
464 void rt2x00pci_remove(struct pci_dev *pci_dev)
466 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
467 struct rt2x00_dev *rt2x00dev = hw->priv;
470 * Free all allocated data.
472 rt2x00lib_remove_dev(rt2x00dev);
473 rt2x00pci_free_reg(rt2x00dev);
474 ieee80211_free_hw(hw);
477 * Free the PCI device data.
479 pci_set_drvdata(pci_dev, NULL);
480 pci_disable_device(pci_dev);
481 pci_release_regions(pci_dev);
483 EXPORT_SYMBOL_GPL(rt2x00pci_remove);
486 int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
488 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
489 struct rt2x00_dev *rt2x00dev = hw->priv;
492 retval = rt2x00lib_suspend(rt2x00dev, state);
496 rt2x00pci_free_reg(rt2x00dev);
498 pci_save_state(pci_dev);
499 pci_disable_device(pci_dev);
500 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
502 EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
504 int rt2x00pci_resume(struct pci_dev *pci_dev)
506 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
507 struct rt2x00_dev *rt2x00dev = hw->priv;
510 if (pci_set_power_state(pci_dev, PCI_D0) ||
511 pci_enable_device(pci_dev) ||
512 pci_restore_state(pci_dev)) {
513 ERROR(rt2x00dev, "Failed to resume device.\n");
517 retval = rt2x00pci_alloc_reg(rt2x00dev);
521 retval = rt2x00lib_resume(rt2x00dev);
528 rt2x00pci_free_reg(rt2x00dev);
532 EXPORT_SYMBOL_GPL(rt2x00pci_resume);
533 #endif /* CONFIG_PM */
536 * rt2x00pci module information.
538 MODULE_AUTHOR(DRV_PROJECT);
539 MODULE_VERSION(DRV_VERSION);
540 MODULE_DESCRIPTION("rt2x00 pci library");
541 MODULE_LICENSE("GPL");