IPVS: fix netns if reading ip_vs_* procfs entries
[linux-2.6/linux-mips.git] / drivers / net / wireless / rt2x00 / rt2x00pci.c
blob4dd82b0b05201bf63888cbe4412f7fdcb3bbfc9b
1 /*
2 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
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>
30 #include <linux/slab.h>
32 #include "rt2x00.h"
33 #include "rt2x00pci.h"
36 * Register access.
38 int rt2x00pci_regbusy_read(struct rt2x00_dev *rt2x00dev,
39 const unsigned int offset,
40 const struct rt2x00_field32 field,
41 u32 *reg)
43 unsigned int i;
45 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
46 return 0;
48 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
49 rt2x00pci_register_read(rt2x00dev, offset, reg);
50 if (!rt2x00_get_field32(*reg, field))
51 return 1;
52 udelay(REGISTER_BUSY_DELAY);
55 ERROR(rt2x00dev, "Indirect register access failed: "
56 "offset=0x%.08x, value=0x%.08x\n", offset, *reg);
57 *reg = ~0;
59 return 0;
61 EXPORT_SYMBOL_GPL(rt2x00pci_regbusy_read);
63 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
65 struct data_queue *queue = rt2x00dev->rx;
66 struct queue_entry *entry;
67 struct queue_entry_priv_pci *entry_priv;
68 struct skb_frame_desc *skbdesc;
70 while (1) {
71 entry = rt2x00queue_get_entry(queue, Q_INDEX);
72 entry_priv = entry->priv_data;
74 if (rt2x00dev->ops->lib->get_entry_state(entry))
75 break;
78 * Fill in desc fields of the skb descriptor
80 skbdesc = get_skb_frame_desc(entry->skb);
81 skbdesc->desc = entry_priv->desc;
82 skbdesc->desc_len = entry->queue->desc_size;
85 * DMA is already done, notify rt2x00lib that
86 * it finished successfully.
88 rt2x00lib_dmastart(entry);
89 rt2x00lib_dmadone(entry);
92 * Send the frame to rt2x00lib for further processing.
94 rt2x00lib_rxdone(entry);
97 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
100 * Device initialization handlers.
102 static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
103 struct data_queue *queue)
105 struct queue_entry_priv_pci *entry_priv;
106 void *addr;
107 dma_addr_t dma;
108 unsigned int i;
111 * Allocate DMA memory for descriptor and buffer.
113 addr = dma_alloc_coherent(rt2x00dev->dev,
114 queue->limit * queue->desc_size,
115 &dma, GFP_KERNEL);
116 if (!addr)
117 return -ENOMEM;
119 memset(addr, 0, queue->limit * queue->desc_size);
122 * Initialize all queue entries to contain valid addresses.
124 for (i = 0; i < queue->limit; i++) {
125 entry_priv = queue->entries[i].priv_data;
126 entry_priv->desc = addr + i * queue->desc_size;
127 entry_priv->desc_dma = dma + i * queue->desc_size;
130 return 0;
133 static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
134 struct data_queue *queue)
136 struct queue_entry_priv_pci *entry_priv =
137 queue->entries[0].priv_data;
139 if (entry_priv->desc)
140 dma_free_coherent(rt2x00dev->dev,
141 queue->limit * queue->desc_size,
142 entry_priv->desc, entry_priv->desc_dma);
143 entry_priv->desc = NULL;
146 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
148 struct data_queue *queue;
149 int status;
152 * Allocate DMA
154 queue_for_each(rt2x00dev, queue) {
155 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
156 if (status)
157 goto exit;
161 * Register interrupt handler.
163 status = request_irq(rt2x00dev->irq,
164 rt2x00dev->ops->lib->irq_handler,
165 IRQF_SHARED, rt2x00dev->name, rt2x00dev);
166 if (status) {
167 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
168 rt2x00dev->irq, status);
169 goto exit;
172 return 0;
174 exit:
175 queue_for_each(rt2x00dev, queue)
176 rt2x00pci_free_queue_dma(rt2x00dev, queue);
178 return status;
180 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
182 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
184 struct data_queue *queue;
187 * Free irq line.
189 free_irq(rt2x00dev->irq, rt2x00dev);
192 * Free DMA
194 queue_for_each(rt2x00dev, queue)
195 rt2x00pci_free_queue_dma(rt2x00dev, queue);
197 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
200 * PCI driver handlers.
202 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
204 kfree(rt2x00dev->rf);
205 rt2x00dev->rf = NULL;
207 kfree(rt2x00dev->eeprom);
208 rt2x00dev->eeprom = NULL;
210 if (rt2x00dev->csr.base) {
211 iounmap(rt2x00dev->csr.base);
212 rt2x00dev->csr.base = NULL;
216 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
218 struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
220 rt2x00dev->csr.base = pci_ioremap_bar(pci_dev, 0);
221 if (!rt2x00dev->csr.base)
222 goto exit;
224 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
225 if (!rt2x00dev->eeprom)
226 goto exit;
228 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
229 if (!rt2x00dev->rf)
230 goto exit;
232 return 0;
234 exit:
235 ERROR_PROBE("Failed to allocate registers.\n");
237 rt2x00pci_free_reg(rt2x00dev);
239 return -ENOMEM;
242 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
244 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
245 struct ieee80211_hw *hw;
246 struct rt2x00_dev *rt2x00dev;
247 int retval;
249 retval = pci_enable_device(pci_dev);
250 if (retval) {
251 ERROR_PROBE("Enable device failed.\n");
252 return retval;
255 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
256 if (retval) {
257 ERROR_PROBE("PCI request regions failed.\n");
258 goto exit_disable_device;
261 pci_set_master(pci_dev);
263 if (pci_set_mwi(pci_dev))
264 ERROR_PROBE("MWI not available.\n");
266 if (dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32))) {
267 ERROR_PROBE("PCI DMA not supported.\n");
268 retval = -EIO;
269 goto exit_release_regions;
272 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
273 if (!hw) {
274 ERROR_PROBE("Failed to allocate hardware.\n");
275 retval = -ENOMEM;
276 goto exit_release_regions;
279 pci_set_drvdata(pci_dev, hw);
281 rt2x00dev = hw->priv;
282 rt2x00dev->dev = &pci_dev->dev;
283 rt2x00dev->ops = ops;
284 rt2x00dev->hw = hw;
285 rt2x00dev->irq = pci_dev->irq;
286 rt2x00dev->name = pci_name(pci_dev);
288 if (pci_is_pcie(pci_dev))
289 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCIE);
290 else
291 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCI);
293 retval = rt2x00pci_alloc_reg(rt2x00dev);
294 if (retval)
295 goto exit_free_device;
297 retval = rt2x00lib_probe_dev(rt2x00dev);
298 if (retval)
299 goto exit_free_reg;
301 return 0;
303 exit_free_reg:
304 rt2x00pci_free_reg(rt2x00dev);
306 exit_free_device:
307 ieee80211_free_hw(hw);
309 exit_release_regions:
310 pci_release_regions(pci_dev);
312 exit_disable_device:
313 pci_disable_device(pci_dev);
315 pci_set_drvdata(pci_dev, NULL);
317 return retval;
319 EXPORT_SYMBOL_GPL(rt2x00pci_probe);
321 void rt2x00pci_remove(struct pci_dev *pci_dev)
323 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
324 struct rt2x00_dev *rt2x00dev = hw->priv;
327 * Free all allocated data.
329 rt2x00lib_remove_dev(rt2x00dev);
330 rt2x00pci_free_reg(rt2x00dev);
331 ieee80211_free_hw(hw);
334 * Free the PCI device data.
336 pci_set_drvdata(pci_dev, NULL);
337 pci_disable_device(pci_dev);
338 pci_release_regions(pci_dev);
340 EXPORT_SYMBOL_GPL(rt2x00pci_remove);
342 #ifdef CONFIG_PM
343 int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
345 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
346 struct rt2x00_dev *rt2x00dev = hw->priv;
347 int retval;
349 retval = rt2x00lib_suspend(rt2x00dev, state);
350 if (retval)
351 return retval;
353 pci_save_state(pci_dev);
354 pci_disable_device(pci_dev);
355 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
357 EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
359 int rt2x00pci_resume(struct pci_dev *pci_dev)
361 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
362 struct rt2x00_dev *rt2x00dev = hw->priv;
364 if (pci_set_power_state(pci_dev, PCI_D0) ||
365 pci_enable_device(pci_dev)) {
366 ERROR(rt2x00dev, "Failed to resume device.\n");
367 return -EIO;
370 pci_restore_state(pci_dev);
371 return rt2x00lib_resume(rt2x00dev);
373 EXPORT_SYMBOL_GPL(rt2x00pci_resume);
374 #endif /* CONFIG_PM */
377 * rt2x00pci module information.
379 MODULE_AUTHOR(DRV_PROJECT);
380 MODULE_VERSION(DRV_VERSION);
381 MODULE_DESCRIPTION("rt2x00 pci library");
382 MODULE_LICENSE("GPL");