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