2449d785cf8d5b4d9b5260a0a2e4f79d0506fdd9
[linux-2.6.git] / drivers / net / wireless / rt2x00 / rt2x00pci.c
1 /*
2         Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
3         <http://rt2x00.serialmonkey.com>
4
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.
9
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.
14
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.
19  */
20
21 /*
22         Module: rt2x00pci
23         Abstract: rt2x00 generic pci device routines.
24  */
25
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>
31
32 #include "rt2x00.h"
33 #include "rt2x00pci.h"
34
35 /*
36  * Register access.
37  */
38 int rt2x00pci_regbusy_read(struct rt2x00_dev *rt2x00dev,
39                            const unsigned int offset,
40                            const struct rt2x00_field32 field,
41                            u32 *reg)
42 {
43         unsigned int i;
44
45         if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
46                 return 0;
47
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);
53         }
54
55         ERROR(rt2x00dev, "Indirect register access failed: "
56               "offset=0x%.08x, value=0x%.08x\n", offset, *reg);
57         *reg = ~0;
58
59         return 0;
60 }
61 EXPORT_SYMBOL_GPL(rt2x00pci_regbusy_read);
62
63 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
64 {
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;
69
70         while (1) {
71                 entry = rt2x00queue_get_entry(queue, Q_INDEX);
72                 entry_priv = entry->priv_data;
73
74                 if (rt2x00dev->ops->lib->get_entry_state(entry))
75                         break;
76
77                 /*
78                  * Fill in desc fields of the skb descriptor
79                  */
80                 skbdesc = get_skb_frame_desc(entry->skb);
81                 skbdesc->desc = entry_priv->desc;
82                 skbdesc->desc_len = entry->queue->desc_size;
83
84                 /*
85                  * Send the frame to rt2x00lib for further processing.
86                  */
87                 rt2x00lib_rxdone(entry);
88         }
89 }
90 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
91
92 /*
93  * Device initialization handlers.
94  */
95 static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
96                                      struct data_queue *queue)
97 {
98         struct queue_entry_priv_pci *entry_priv;
99         void *addr;
100         dma_addr_t dma;
101         unsigned int i;
102
103         /*
104          * Allocate DMA memory for descriptor and buffer.
105          */
106         addr = dma_alloc_coherent(rt2x00dev->dev,
107                                   queue->limit * queue->desc_size,
108                                   &dma, GFP_KERNEL | GFP_DMA);
109         if (!addr)
110                 return -ENOMEM;
111
112         memset(addr, 0, queue->limit * queue->desc_size);
113
114         /*
115          * Initialize all queue entries to contain valid addresses.
116          */
117         for (i = 0; i < queue->limit; i++) {
118                 entry_priv = queue->entries[i].priv_data;
119                 entry_priv->desc = addr + i * queue->desc_size;
120                 entry_priv->desc_dma = dma + i * queue->desc_size;
121         }
122
123         return 0;
124 }
125
126 static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
127                                      struct data_queue *queue)
128 {
129         struct queue_entry_priv_pci *entry_priv =
130             queue->entries[0].priv_data;
131
132         if (entry_priv->desc)
133                 dma_free_coherent(rt2x00dev->dev,
134                                   queue->limit * queue->desc_size,
135                                   entry_priv->desc, entry_priv->desc_dma);
136         entry_priv->desc = NULL;
137 }
138
139 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
140 {
141         struct data_queue *queue;
142         int status;
143
144         /*
145          * Allocate DMA
146          */
147         queue_for_each(rt2x00dev, queue) {
148                 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
149                 if (status)
150                         goto exit;
151         }
152
153         /*
154          * Register interrupt handler.
155          */
156         status = request_threaded_irq(rt2x00dev->irq,
157                                       rt2x00dev->ops->lib->irq_handler,
158                                       rt2x00dev->ops->lib->irq_handler_thread,
159                                       IRQF_SHARED, rt2x00dev->name, rt2x00dev);
160         if (status) {
161                 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
162                       rt2x00dev->irq, status);
163                 goto exit;
164         }
165
166         return 0;
167
168 exit:
169         queue_for_each(rt2x00dev, queue)
170                 rt2x00pci_free_queue_dma(rt2x00dev, queue);
171
172         return status;
173 }
174 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
175
176 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
177 {
178         struct data_queue *queue;
179
180         /*
181          * Free irq line.
182          */
183         free_irq(rt2x00dev->irq, rt2x00dev);
184
185         /*
186          * Free DMA
187          */
188         queue_for_each(rt2x00dev, queue)
189                 rt2x00pci_free_queue_dma(rt2x00dev, queue);
190 }
191 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
192
193 /*
194  * PCI driver handlers.
195  */
196 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
197 {
198         kfree(rt2x00dev->rf);
199         rt2x00dev->rf = NULL;
200
201         kfree(rt2x00dev->eeprom);
202         rt2x00dev->eeprom = NULL;
203
204         if (rt2x00dev->csr.base) {
205                 iounmap(rt2x00dev->csr.base);
206                 rt2x00dev->csr.base = NULL;
207         }
208 }
209
210 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
211 {
212         struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
213
214         rt2x00dev->csr.base = pci_ioremap_bar(pci_dev, 0);
215         if (!rt2x00dev->csr.base)
216                 goto exit;
217
218         rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
219         if (!rt2x00dev->eeprom)
220                 goto exit;
221
222         rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
223         if (!rt2x00dev->rf)
224                 goto exit;
225
226         return 0;
227
228 exit:
229         ERROR_PROBE("Failed to allocate registers.\n");
230
231         rt2x00pci_free_reg(rt2x00dev);
232
233         return -ENOMEM;
234 }
235
236 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
237 {
238         struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
239         struct ieee80211_hw *hw;
240         struct rt2x00_dev *rt2x00dev;
241         int retval;
242
243         retval = pci_enable_device(pci_dev);
244         if (retval) {
245                 ERROR_PROBE("Enable device failed.\n");
246                 return retval;
247         }
248
249         retval = pci_request_regions(pci_dev, pci_name(pci_dev));
250         if (retval) {
251                 ERROR_PROBE("PCI request regions failed.\n");
252                 goto exit_disable_device;
253         }
254
255         pci_set_master(pci_dev);
256
257         if (pci_set_mwi(pci_dev))
258                 ERROR_PROBE("MWI not available.\n");
259
260         if (dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32))) {
261                 ERROR_PROBE("PCI DMA not supported.\n");
262                 retval = -EIO;
263                 goto exit_release_regions;
264         }
265
266         hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
267         if (!hw) {
268                 ERROR_PROBE("Failed to allocate hardware.\n");
269                 retval = -ENOMEM;
270                 goto exit_release_regions;
271         }
272
273         pci_set_drvdata(pci_dev, hw);
274
275         rt2x00dev = hw->priv;
276         rt2x00dev->dev = &pci_dev->dev;
277         rt2x00dev->ops = ops;
278         rt2x00dev->hw = hw;
279         rt2x00dev->irq = pci_dev->irq;
280         rt2x00dev->name = pci_name(pci_dev);
281
282         if (pci_dev->is_pcie)
283                 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCIE);
284         else
285                 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCI);
286
287         retval = rt2x00pci_alloc_reg(rt2x00dev);
288         if (retval)
289                 goto exit_free_device;
290
291         retval = rt2x00lib_probe_dev(rt2x00dev);
292         if (retval)
293                 goto exit_free_reg;
294
295         return 0;
296
297 exit_free_reg:
298         rt2x00pci_free_reg(rt2x00dev);
299
300 exit_free_device:
301         ieee80211_free_hw(hw);
302
303 exit_release_regions:
304         pci_release_regions(pci_dev);
305
306 exit_disable_device:
307         pci_disable_device(pci_dev);
308
309         pci_set_drvdata(pci_dev, NULL);
310
311         return retval;
312 }
313 EXPORT_SYMBOL_GPL(rt2x00pci_probe);
314
315 void rt2x00pci_remove(struct pci_dev *pci_dev)
316 {
317         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
318         struct rt2x00_dev *rt2x00dev = hw->priv;
319
320         /*
321          * Free all allocated data.
322          */
323         rt2x00lib_remove_dev(rt2x00dev);
324         rt2x00pci_free_reg(rt2x00dev);
325         ieee80211_free_hw(hw);
326
327         /*
328          * Free the PCI device data.
329          */
330         pci_set_drvdata(pci_dev, NULL);
331         pci_disable_device(pci_dev);
332         pci_release_regions(pci_dev);
333 }
334 EXPORT_SYMBOL_GPL(rt2x00pci_remove);
335
336 #ifdef CONFIG_PM
337 int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
338 {
339         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
340         struct rt2x00_dev *rt2x00dev = hw->priv;
341         int retval;
342
343         retval = rt2x00lib_suspend(rt2x00dev, state);
344         if (retval)
345                 return retval;
346
347         pci_save_state(pci_dev);
348         pci_disable_device(pci_dev);
349         return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
350 }
351 EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
352
353 int rt2x00pci_resume(struct pci_dev *pci_dev)
354 {
355         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
356         struct rt2x00_dev *rt2x00dev = hw->priv;
357
358         if (pci_set_power_state(pci_dev, PCI_D0) ||
359             pci_enable_device(pci_dev) ||
360             pci_restore_state(pci_dev)) {
361                 ERROR(rt2x00dev, "Failed to resume device.\n");
362                 return -EIO;
363         }
364
365         return rt2x00lib_resume(rt2x00dev);
366 }
367 EXPORT_SYMBOL_GPL(rt2x00pci_resume);
368 #endif /* CONFIG_PM */
369
370 /*
371  * rt2x00pci module information.
372  */
373 MODULE_AUTHOR(DRV_PROJECT);
374 MODULE_VERSION(DRV_VERSION);
375 MODULE_DESCRIPTION("rt2x00 pci library");
376 MODULE_LICENSE("GPL");