added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / net / wireless / rt2x00 / rt2x00pci.c
blobd52b22b82d1fb4662184b653efe50f99b43fec13
1 /*
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.
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 * Register access.
37 int rt2x00pci_regbusy_read(struct rt2x00_dev *rt2x00dev,
38 const unsigned int offset,
39 const struct rt2x00_field32 field,
40 u32 *reg)
42 unsigned int i;
44 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
45 rt2x00pci_register_read(rt2x00dev, offset, reg);
46 if (!rt2x00_get_field32(*reg, field))
47 return 1;
48 udelay(REGISTER_BUSY_DELAY);
51 ERROR(rt2x00dev, "Indirect register access failed: "
52 "offset=0x%.08x, value=0x%.08x\n", offset, *reg);
53 *reg = ~0;
55 return 0;
57 EXPORT_SYMBOL_GPL(rt2x00pci_regbusy_read);
60 * TX data handlers.
62 int rt2x00pci_write_tx_data(struct queue_entry *entry)
64 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
65 struct queue_entry_priv_pci *entry_priv = entry->priv_data;
66 struct skb_frame_desc *skbdesc;
69 * This should not happen, we already checked the entry
70 * was ours. When the hardware disagrees there has been
71 * a queue corruption!
73 if (unlikely(rt2x00dev->ops->lib->get_entry_state(entry))) {
74 ERROR(rt2x00dev,
75 "Corrupt queue %d, accessing entry which is not ours.\n"
76 "Please file bug report to %s.\n",
77 entry->queue->qid, DRV_PROJECT);
78 return -EINVAL;
82 * Fill in skb descriptor
84 skbdesc = get_skb_frame_desc(entry->skb);
85 skbdesc->desc = entry_priv->desc;
86 skbdesc->desc_len = entry->queue->desc_size;
88 return 0;
90 EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
93 * TX/RX data handlers.
95 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
97 struct data_queue *queue = rt2x00dev->rx;
98 struct queue_entry *entry;
99 struct queue_entry_priv_pci *entry_priv;
100 struct skb_frame_desc *skbdesc;
102 while (1) {
103 entry = rt2x00queue_get_entry(queue, Q_INDEX);
104 entry_priv = entry->priv_data;
106 if (rt2x00dev->ops->lib->get_entry_state(entry))
107 break;
110 * Fill in desc fields of the skb descriptor
112 skbdesc = get_skb_frame_desc(entry->skb);
113 skbdesc->desc = entry_priv->desc;
114 skbdesc->desc_len = entry->queue->desc_size;
117 * Send the frame to rt2x00lib for further processing.
119 rt2x00lib_rxdone(rt2x00dev, entry);
122 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
125 * Device initialization handlers.
127 static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
128 struct data_queue *queue)
130 struct queue_entry_priv_pci *entry_priv;
131 void *addr;
132 dma_addr_t dma;
133 unsigned int i;
136 * Allocate DMA memory for descriptor and buffer.
138 addr = dma_alloc_coherent(rt2x00dev->dev,
139 queue->limit * queue->desc_size,
140 &dma, GFP_KERNEL | GFP_DMA);
141 if (!addr)
142 return -ENOMEM;
144 memset(addr, 0, queue->limit * queue->desc_size);
147 * Initialize all queue entries to contain valid addresses.
149 for (i = 0; i < queue->limit; i++) {
150 entry_priv = queue->entries[i].priv_data;
151 entry_priv->desc = addr + i * queue->desc_size;
152 entry_priv->desc_dma = dma + i * queue->desc_size;
155 return 0;
158 static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
159 struct data_queue *queue)
161 struct queue_entry_priv_pci *entry_priv =
162 queue->entries[0].priv_data;
164 if (entry_priv->desc)
165 dma_free_coherent(rt2x00dev->dev,
166 queue->limit * queue->desc_size,
167 entry_priv->desc, entry_priv->desc_dma);
168 entry_priv->desc = NULL;
171 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
173 struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
174 struct data_queue *queue;
175 int status;
178 * Allocate DMA
180 queue_for_each(rt2x00dev, queue) {
181 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
182 if (status)
183 goto exit;
187 * Register interrupt handler.
189 status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
190 IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
191 if (status) {
192 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
193 pci_dev->irq, status);
194 goto exit;
197 return 0;
199 exit:
200 queue_for_each(rt2x00dev, queue)
201 rt2x00pci_free_queue_dma(rt2x00dev, queue);
203 return status;
205 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
207 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
209 struct data_queue *queue;
212 * Free irq line.
214 free_irq(to_pci_dev(rt2x00dev->dev)->irq, rt2x00dev);
217 * Free DMA
219 queue_for_each(rt2x00dev, queue)
220 rt2x00pci_free_queue_dma(rt2x00dev, queue);
222 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
225 * PCI driver handlers.
227 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
229 kfree(rt2x00dev->rf);
230 rt2x00dev->rf = NULL;
232 kfree(rt2x00dev->eeprom);
233 rt2x00dev->eeprom = NULL;
235 if (rt2x00dev->csr.base) {
236 iounmap(rt2x00dev->csr.base);
237 rt2x00dev->csr.base = NULL;
241 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
243 struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
245 rt2x00dev->csr.base = pci_ioremap_bar(pci_dev, 0);
246 if (!rt2x00dev->csr.base)
247 goto exit;
249 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
250 if (!rt2x00dev->eeprom)
251 goto exit;
253 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
254 if (!rt2x00dev->rf)
255 goto exit;
257 return 0;
259 exit:
260 ERROR_PROBE("Failed to allocate registers.\n");
262 rt2x00pci_free_reg(rt2x00dev);
264 return -ENOMEM;
267 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
269 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
270 struct ieee80211_hw *hw;
271 struct rt2x00_dev *rt2x00dev;
272 int retval;
274 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
275 if (retval) {
276 ERROR_PROBE("PCI request regions failed.\n");
277 return retval;
280 retval = pci_enable_device(pci_dev);
281 if (retval) {
282 ERROR_PROBE("Enable device failed.\n");
283 goto exit_release_regions;
286 pci_set_master(pci_dev);
288 if (pci_set_mwi(pci_dev))
289 ERROR_PROBE("MWI not available.\n");
291 if (dma_set_mask(&pci_dev->dev, DMA_32BIT_MASK)) {
292 ERROR_PROBE("PCI DMA not supported.\n");
293 retval = -EIO;
294 goto exit_disable_device;
297 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
298 if (!hw) {
299 ERROR_PROBE("Failed to allocate hardware.\n");
300 retval = -ENOMEM;
301 goto exit_disable_device;
304 pci_set_drvdata(pci_dev, hw);
306 rt2x00dev = hw->priv;
307 rt2x00dev->dev = &pci_dev->dev;
308 rt2x00dev->ops = ops;
309 rt2x00dev->hw = hw;
311 retval = rt2x00pci_alloc_reg(rt2x00dev);
312 if (retval)
313 goto exit_free_device;
315 retval = rt2x00lib_probe_dev(rt2x00dev);
316 if (retval)
317 goto exit_free_reg;
319 return 0;
321 exit_free_reg:
322 rt2x00pci_free_reg(rt2x00dev);
324 exit_free_device:
325 ieee80211_free_hw(hw);
327 exit_disable_device:
328 if (retval != -EBUSY)
329 pci_disable_device(pci_dev);
331 exit_release_regions:
332 pci_release_regions(pci_dev);
334 pci_set_drvdata(pci_dev, NULL);
336 return retval;
338 EXPORT_SYMBOL_GPL(rt2x00pci_probe);
340 void rt2x00pci_remove(struct pci_dev *pci_dev)
342 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
343 struct rt2x00_dev *rt2x00dev = hw->priv;
346 * Free all allocated data.
348 rt2x00lib_remove_dev(rt2x00dev);
349 rt2x00pci_free_reg(rt2x00dev);
350 ieee80211_free_hw(hw);
353 * Free the PCI device data.
355 pci_set_drvdata(pci_dev, NULL);
356 pci_disable_device(pci_dev);
357 pci_release_regions(pci_dev);
359 EXPORT_SYMBOL_GPL(rt2x00pci_remove);
361 #ifdef CONFIG_PM
362 int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
364 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
365 struct rt2x00_dev *rt2x00dev = hw->priv;
366 int retval;
368 retval = rt2x00lib_suspend(rt2x00dev, state);
369 if (retval)
370 return retval;
372 rt2x00pci_free_reg(rt2x00dev);
374 pci_save_state(pci_dev);
375 pci_disable_device(pci_dev);
376 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
378 EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
380 int rt2x00pci_resume(struct pci_dev *pci_dev)
382 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
383 struct rt2x00_dev *rt2x00dev = hw->priv;
384 int retval;
386 if (pci_set_power_state(pci_dev, PCI_D0) ||
387 pci_enable_device(pci_dev) ||
388 pci_restore_state(pci_dev)) {
389 ERROR(rt2x00dev, "Failed to resume device.\n");
390 return -EIO;
393 retval = rt2x00pci_alloc_reg(rt2x00dev);
394 if (retval)
395 return retval;
397 retval = rt2x00lib_resume(rt2x00dev);
398 if (retval)
399 goto exit_free_reg;
401 return 0;
403 exit_free_reg:
404 rt2x00pci_free_reg(rt2x00dev);
406 return retval;
408 EXPORT_SYMBOL_GPL(rt2x00pci_resume);
409 #endif /* CONFIG_PM */
412 * rt2x00pci module information.
414 MODULE_AUTHOR(DRV_PROJECT);
415 MODULE_VERSION(DRV_VERSION);
416 MODULE_DESCRIPTION("rt2x00 pci library");
417 MODULE_LICENSE("GPL");