Merge branch 'master' into for-davem
[pandora-kernel.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 /*
64  * TX data handlers.
65  */
66 int rt2x00pci_write_tx_data(struct queue_entry *entry,
67                             struct txentry_desc *txdesc)
68 {
69         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
70         struct queue_entry_priv_pci *entry_priv = entry->priv_data;
71         struct skb_frame_desc *skbdesc;
72
73         /*
74          * This should not happen, we already checked the entry
75          * was ours. When the hardware disagrees there has been
76          * a queue corruption!
77          */
78         if (unlikely(rt2x00dev->ops->lib->get_entry_state(entry))) {
79                 ERROR(rt2x00dev,
80                       "Corrupt queue %d, accessing entry which is not ours.\n"
81                       "Please file bug report to %s.\n",
82                       entry->queue->qid, DRV_PROJECT);
83                 return -EINVAL;
84         }
85
86         /*
87          * Fill in skb descriptor
88          */
89         skbdesc = get_skb_frame_desc(entry->skb);
90         skbdesc->desc = entry_priv->desc;
91         skbdesc->desc_len = entry->queue->desc_size;
92
93         return 0;
94 }
95 EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
96
97 /*
98  * TX/RX data handlers.
99  */
100 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
101 {
102         struct data_queue *queue = rt2x00dev->rx;
103         struct queue_entry *entry;
104         struct queue_entry_priv_pci *entry_priv;
105         struct skb_frame_desc *skbdesc;
106
107         while (1) {
108                 entry = rt2x00queue_get_entry(queue, Q_INDEX);
109                 entry_priv = entry->priv_data;
110
111                 if (rt2x00dev->ops->lib->get_entry_state(entry))
112                         break;
113
114                 /*
115                  * Fill in desc fields of the skb descriptor
116                  */
117                 skbdesc = get_skb_frame_desc(entry->skb);
118                 skbdesc->desc = entry_priv->desc;
119                 skbdesc->desc_len = entry->queue->desc_size;
120
121                 /*
122                  * Send the frame to rt2x00lib for further processing.
123                  */
124                 rt2x00lib_rxdone(rt2x00dev, entry);
125         }
126 }
127 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
128
129 /*
130  * Device initialization handlers.
131  */
132 static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
133                                      struct data_queue *queue)
134 {
135         struct queue_entry_priv_pci *entry_priv;
136         void *addr;
137         dma_addr_t dma;
138         unsigned int i;
139
140         /*
141          * Allocate DMA memory for descriptor and buffer.
142          */
143         addr = dma_alloc_coherent(rt2x00dev->dev,
144                                   queue->limit * queue->desc_size,
145                                   &dma, GFP_KERNEL | GFP_DMA);
146         if (!addr)
147                 return -ENOMEM;
148
149         memset(addr, 0, queue->limit * queue->desc_size);
150
151         /*
152          * Initialize all queue entries to contain valid addresses.
153          */
154         for (i = 0; i < queue->limit; i++) {
155                 entry_priv = queue->entries[i].priv_data;
156                 entry_priv->desc = addr + i * queue->desc_size;
157                 entry_priv->desc_dma = dma + i * queue->desc_size;
158         }
159
160         return 0;
161 }
162
163 static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
164                                      struct data_queue *queue)
165 {
166         struct queue_entry_priv_pci *entry_priv =
167             queue->entries[0].priv_data;
168
169         if (entry_priv->desc)
170                 dma_free_coherent(rt2x00dev->dev,
171                                   queue->limit * queue->desc_size,
172                                   entry_priv->desc, entry_priv->desc_dma);
173         entry_priv->desc = NULL;
174 }
175
176 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
177 {
178         struct data_queue *queue;
179         int status;
180
181         /*
182          * Allocate DMA
183          */
184         queue_for_each(rt2x00dev, queue) {
185                 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
186                 if (status)
187                         goto exit;
188         }
189
190         /*
191          * Register interrupt handler.
192          */
193         status = request_irq(rt2x00dev->irq, rt2x00dev->ops->lib->irq_handler,
194                              IRQF_SHARED, rt2x00dev->name, rt2x00dev);
195         if (status) {
196                 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
197                       rt2x00dev->irq, status);
198                 goto exit;
199         }
200
201         return 0;
202
203 exit:
204         queue_for_each(rt2x00dev, queue)
205                 rt2x00pci_free_queue_dma(rt2x00dev, queue);
206
207         return status;
208 }
209 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
210
211 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
212 {
213         struct data_queue *queue;
214
215         /*
216          * Free irq line.
217          */
218         free_irq(to_pci_dev(rt2x00dev->dev)->irq, rt2x00dev);
219
220         /*
221          * Free DMA
222          */
223         queue_for_each(rt2x00dev, queue)
224                 rt2x00pci_free_queue_dma(rt2x00dev, queue);
225 }
226 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
227
228 /*
229  * PCI driver handlers.
230  */
231 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
232 {
233         kfree(rt2x00dev->rf);
234         rt2x00dev->rf = NULL;
235
236         kfree(rt2x00dev->eeprom);
237         rt2x00dev->eeprom = NULL;
238
239         if (rt2x00dev->csr.base) {
240                 iounmap(rt2x00dev->csr.base);
241                 rt2x00dev->csr.base = NULL;
242         }
243 }
244
245 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
246 {
247         struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
248
249         rt2x00dev->csr.base = pci_ioremap_bar(pci_dev, 0);
250         if (!rt2x00dev->csr.base)
251                 goto exit;
252
253         rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
254         if (!rt2x00dev->eeprom)
255                 goto exit;
256
257         rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
258         if (!rt2x00dev->rf)
259                 goto exit;
260
261         return 0;
262
263 exit:
264         ERROR_PROBE("Failed to allocate registers.\n");
265
266         rt2x00pci_free_reg(rt2x00dev);
267
268         return -ENOMEM;
269 }
270
271 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
272 {
273         struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
274         struct ieee80211_hw *hw;
275         struct rt2x00_dev *rt2x00dev;
276         int retval;
277
278         retval = pci_request_regions(pci_dev, pci_name(pci_dev));
279         if (retval) {
280                 ERROR_PROBE("PCI request regions failed.\n");
281                 return retval;
282         }
283
284         retval = pci_enable_device(pci_dev);
285         if (retval) {
286                 ERROR_PROBE("Enable device failed.\n");
287                 goto exit_release_regions;
288         }
289
290         pci_set_master(pci_dev);
291
292         if (pci_set_mwi(pci_dev))
293                 ERROR_PROBE("MWI not available.\n");
294
295         if (dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32))) {
296                 ERROR_PROBE("PCI DMA not supported.\n");
297                 retval = -EIO;
298                 goto exit_disable_device;
299         }
300
301         hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
302         if (!hw) {
303                 ERROR_PROBE("Failed to allocate hardware.\n");
304                 retval = -ENOMEM;
305                 goto exit_disable_device;
306         }
307
308         pci_set_drvdata(pci_dev, hw);
309
310         rt2x00dev = hw->priv;
311         rt2x00dev->dev = &pci_dev->dev;
312         rt2x00dev->ops = ops;
313         rt2x00dev->hw = hw;
314         rt2x00dev->irq = pci_dev->irq;
315         rt2x00dev->name = pci_name(pci_dev);
316
317         rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCI);
318
319         retval = rt2x00pci_alloc_reg(rt2x00dev);
320         if (retval)
321                 goto exit_free_device;
322
323         retval = rt2x00lib_probe_dev(rt2x00dev);
324         if (retval)
325                 goto exit_free_reg;
326
327         return 0;
328
329 exit_free_reg:
330         rt2x00pci_free_reg(rt2x00dev);
331
332 exit_free_device:
333         ieee80211_free_hw(hw);
334
335 exit_disable_device:
336         if (retval != -EBUSY)
337                 pci_disable_device(pci_dev);
338
339 exit_release_regions:
340         pci_release_regions(pci_dev);
341
342         pci_set_drvdata(pci_dev, NULL);
343
344         return retval;
345 }
346 EXPORT_SYMBOL_GPL(rt2x00pci_probe);
347
348 void rt2x00pci_remove(struct pci_dev *pci_dev)
349 {
350         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
351         struct rt2x00_dev *rt2x00dev = hw->priv;
352
353         /*
354          * Free all allocated data.
355          */
356         rt2x00lib_remove_dev(rt2x00dev);
357         rt2x00pci_free_reg(rt2x00dev);
358         ieee80211_free_hw(hw);
359
360         /*
361          * Free the PCI device data.
362          */
363         pci_set_drvdata(pci_dev, NULL);
364         pci_disable_device(pci_dev);
365         pci_release_regions(pci_dev);
366 }
367 EXPORT_SYMBOL_GPL(rt2x00pci_remove);
368
369 #ifdef CONFIG_PM
370 int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
371 {
372         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
373         struct rt2x00_dev *rt2x00dev = hw->priv;
374         int retval;
375
376         retval = rt2x00lib_suspend(rt2x00dev, state);
377         if (retval)
378                 return retval;
379
380         pci_save_state(pci_dev);
381         pci_disable_device(pci_dev);
382         return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
383 }
384 EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
385
386 int rt2x00pci_resume(struct pci_dev *pci_dev)
387 {
388         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
389         struct rt2x00_dev *rt2x00dev = hw->priv;
390
391         if (pci_set_power_state(pci_dev, PCI_D0) ||
392             pci_enable_device(pci_dev) ||
393             pci_restore_state(pci_dev)) {
394                 ERROR(rt2x00dev, "Failed to resume device.\n");
395                 return -EIO;
396         }
397
398         return rt2x00lib_resume(rt2x00dev);
399 }
400 EXPORT_SYMBOL_GPL(rt2x00pci_resume);
401 #endif /* CONFIG_PM */
402
403 /*
404  * rt2x00pci module information.
405  */
406 MODULE_AUTHOR(DRV_PROJECT);
407 MODULE_VERSION(DRV_VERSION);
408 MODULE_DESCRIPTION("rt2x00 pci library");
409 MODULE_LICENSE("GPL");