rt2x00: Add chipset version to chipset debugfs entry
[linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00pci.c
1 /*
2         Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
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
31 #include "rt2x00.h"
32 #include "rt2x00pci.h"
33
34 /*
35  * Beacon handlers.
36  */
37 int rt2x00pci_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
38                             struct ieee80211_tx_control *control)
39 {
40         struct rt2x00_dev *rt2x00dev = hw->priv;
41         struct data_ring *ring =
42             rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
43         struct data_entry *entry = rt2x00_get_data_entry(ring);
44
45         /*
46          * Just in case mac80211 doesn't set this correctly,
47          * but we need this queue set for the descriptor
48          * initialization.
49          */
50         control->queue = IEEE80211_TX_QUEUE_BEACON;
51
52         /*
53          * Update the beacon entry.
54          */
55         memcpy(entry->data_addr, skb->data, skb->len);
56         rt2x00lib_write_tx_desc(rt2x00dev, entry->priv,
57                                 (struct ieee80211_hdr *)skb->data,
58                                 skb->len, control);
59
60         /*
61          * Enable beacon generation.
62          */
63         rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
64
65         return 0;
66 }
67 EXPORT_SYMBOL_GPL(rt2x00pci_beacon_update);
68
69 /*
70  * TX data handlers.
71  */
72 int rt2x00pci_write_tx_data(struct rt2x00_dev *rt2x00dev,
73                             struct data_ring *ring, struct sk_buff *skb,
74                             struct ieee80211_tx_control *control)
75 {
76         struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
77         struct data_entry *entry = rt2x00_get_data_entry(ring);
78         __le32 *txd = entry->priv;
79         u32 word;
80
81         if (rt2x00_ring_full(ring)) {
82                 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
83                 return -EINVAL;
84         }
85
86         rt2x00_desc_read(txd, 0, &word);
87
88         if (rt2x00_get_field32(word, TXD_ENTRY_OWNER_NIC) ||
89             rt2x00_get_field32(word, TXD_ENTRY_VALID)) {
90                 ERROR(rt2x00dev,
91                       "Arrived at non-free entry in the non-full queue %d.\n"
92                       "Please file bug report to %s.\n",
93                       control->queue, DRV_PROJECT);
94                 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
95                 return -EINVAL;
96         }
97
98         entry->skb = skb;
99         memcpy(&entry->tx_status.control, control, sizeof(*control));
100         memcpy(entry->data_addr, skb->data, skb->len);
101         rt2x00lib_write_tx_desc(rt2x00dev, txd, ieee80211hdr,
102                                 skb->len, control);
103
104         rt2x00_ring_index_inc(ring);
105
106         if (rt2x00_ring_full(ring))
107                 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
108
109         return 0;
110 }
111 EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
112
113 /*
114  * TX/RX data handlers.
115  */
116 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
117 {
118         struct data_ring *ring = rt2x00dev->rx;
119         struct data_entry *entry;
120         struct sk_buff *skb;
121         struct ieee80211_hdr *hdr;
122         struct rxdata_entry_desc desc;
123         int header_size;
124         __le32 *rxd;
125         int align;
126         u32 word;
127
128         while (1) {
129                 entry = rt2x00_get_data_entry(ring);
130                 rxd = entry->priv;
131                 rt2x00_desc_read(rxd, 0, &word);
132
133                 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
134                         break;
135
136                 memset(&desc, 0x00, sizeof(desc));
137                 rt2x00dev->ops->lib->fill_rxdone(entry, &desc);
138
139                 hdr = (struct ieee80211_hdr *)entry->data_addr;
140                 header_size =
141                     ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
142
143                 /*
144                  * The data behind the ieee80211 header must be
145                  * aligned on a 4 byte boundary.
146                  */
147                 align = header_size % 4;
148
149                 /*
150                  * Allocate the sk_buffer, initialize it and copy
151                  * all data into it.
152                  */
153                 skb = dev_alloc_skb(desc.size + align);
154                 if (!skb)
155                         return;
156
157                 skb_reserve(skb, align);
158                 memcpy(skb_put(skb, desc.size), entry->data_addr, desc.size);
159
160                 /*
161                  * Send the frame to rt2x00lib for further processing.
162                  */
163                 rt2x00lib_rxdone(entry, skb, &desc);
164
165                 if (test_bit(DEVICE_ENABLED_RADIO, &ring->rt2x00dev->flags)) {
166                         rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
167                         rt2x00_desc_write(rxd, 0, word);
168                 }
169
170                 rt2x00_ring_index_inc(ring);
171         }
172 }
173 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
174
175 void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct data_entry *entry,
176                       const int tx_status, const int retry)
177 {
178         u32 word;
179
180         rt2x00lib_txdone(entry, tx_status, retry);
181
182         /*
183          * Make this entry available for reuse.
184          */
185         entry->flags = 0;
186
187         rt2x00_desc_read(entry->priv, 0, &word);
188         rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
189         rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
190         rt2x00_desc_write(entry->priv, 0, word);
191
192         rt2x00_ring_index_done_inc(entry->ring);
193
194         /*
195          * If the data ring was full before the txdone handler
196          * we must make sure the packet queue in the mac80211 stack
197          * is reenabled when the txdone handler has finished.
198          */
199         if (!rt2x00_ring_full(entry->ring))
200                 ieee80211_wake_queue(rt2x00dev->hw,
201                                      entry->tx_status.control.queue);
202
203 }
204 EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
205
206 /*
207  * Device initialization handlers.
208  */
209 #define priv_offset(__ring, __i)                                \
210 ({                                                              \
211         ring->data_addr + (i * ring->desc_size);                \
212 })
213
214 #define data_addr_offset(__ring, __i)                           \
215 ({                                                              \
216         (__ring)->data_addr +                                   \
217             ((__ring)->stats.limit * (__ring)->desc_size) +     \
218             ((__i) * (__ring)->data_size);                      \
219 })
220
221 #define data_dma_offset(__ring, __i)                            \
222 ({                                                              \
223         (__ring)->data_dma +                                    \
224             ((__ring)->stats.limit * (__ring)->desc_size) +     \
225             ((__i) * (__ring)->data_size);                      \
226 })
227
228 static int rt2x00pci_alloc_dma(struct rt2x00_dev *rt2x00dev,
229                                struct data_ring *ring)
230 {
231         unsigned int i;
232
233         /*
234          * Allocate DMA memory for descriptor and buffer.
235          */
236         ring->data_addr = pci_alloc_consistent(rt2x00dev_pci(rt2x00dev),
237                                                rt2x00_get_ring_size(ring),
238                                                &ring->data_dma);
239         if (!ring->data_addr)
240                 return -ENOMEM;
241
242         /*
243          * Initialize all ring entries to contain valid
244          * addresses.
245          */
246         for (i = 0; i < ring->stats.limit; i++) {
247                 ring->entry[i].priv = priv_offset(ring, i);
248                 ring->entry[i].data_addr = data_addr_offset(ring, i);
249                 ring->entry[i].data_dma = data_dma_offset(ring, i);
250         }
251
252         return 0;
253 }
254
255 static void rt2x00pci_free_dma(struct rt2x00_dev *rt2x00dev,
256                                struct data_ring *ring)
257 {
258         if (ring->data_addr)
259                 pci_free_consistent(rt2x00dev_pci(rt2x00dev),
260                                     rt2x00_get_ring_size(ring),
261                                     ring->data_addr, ring->data_dma);
262         ring->data_addr = NULL;
263 }
264
265 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
266 {
267         struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
268         struct data_ring *ring;
269         int status;
270
271         /*
272          * Allocate DMA
273          */
274         ring_for_each(rt2x00dev, ring) {
275                 status = rt2x00pci_alloc_dma(rt2x00dev, ring);
276                 if (status)
277                         goto exit;
278         }
279
280         /*
281          * Register interrupt handler.
282          */
283         status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
284                              IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
285         if (status) {
286                 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
287                       pci_dev->irq, status);
288                 return status;
289         }
290
291         return 0;
292
293 exit:
294         rt2x00pci_uninitialize(rt2x00dev);
295
296         return status;
297 }
298 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
299
300 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
301 {
302         struct data_ring *ring;
303
304         /*
305          * Free irq line.
306          */
307         free_irq(rt2x00dev_pci(rt2x00dev)->irq, rt2x00dev);
308
309         /*
310          * Free DMA
311          */
312         ring_for_each(rt2x00dev, ring)
313                 rt2x00pci_free_dma(rt2x00dev, ring);
314 }
315 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
316
317 /*
318  * PCI driver handlers.
319  */
320 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
321 {
322         kfree(rt2x00dev->rf);
323         rt2x00dev->rf = NULL;
324
325         kfree(rt2x00dev->eeprom);
326         rt2x00dev->eeprom = NULL;
327
328         if (rt2x00dev->csr_addr) {
329                 iounmap(rt2x00dev->csr_addr);
330                 rt2x00dev->csr_addr = NULL;
331         }
332 }
333
334 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
335 {
336         struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
337
338         rt2x00dev->csr_addr = ioremap(pci_resource_start(pci_dev, 0),
339                                       pci_resource_len(pci_dev, 0));
340         if (!rt2x00dev->csr_addr)
341                 goto exit;
342
343         rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
344         if (!rt2x00dev->eeprom)
345                 goto exit;
346
347         rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
348         if (!rt2x00dev->rf)
349                 goto exit;
350
351         return 0;
352
353 exit:
354         ERROR_PROBE("Failed to allocate registers.\n");
355
356         rt2x00pci_free_reg(rt2x00dev);
357
358         return -ENOMEM;
359 }
360
361 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
362 {
363         struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
364         struct ieee80211_hw *hw;
365         struct rt2x00_dev *rt2x00dev;
366         int retval;
367
368         retval = pci_request_regions(pci_dev, pci_name(pci_dev));
369         if (retval) {
370                 ERROR_PROBE("PCI request regions failed.\n");
371                 return retval;
372         }
373
374         retval = pci_enable_device(pci_dev);
375         if (retval) {
376                 ERROR_PROBE("Enable device failed.\n");
377                 goto exit_release_regions;
378         }
379
380         pci_set_master(pci_dev);
381
382         if (pci_set_mwi(pci_dev))
383                 ERROR_PROBE("MWI not available.\n");
384
385         if (pci_set_dma_mask(pci_dev, DMA_64BIT_MASK) &&
386             pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)) {
387                 ERROR_PROBE("PCI DMA not supported.\n");
388                 retval = -EIO;
389                 goto exit_disable_device;
390         }
391
392         hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
393         if (!hw) {
394                 ERROR_PROBE("Failed to allocate hardware.\n");
395                 retval = -ENOMEM;
396                 goto exit_disable_device;
397         }
398
399         pci_set_drvdata(pci_dev, hw);
400
401         rt2x00dev = hw->priv;
402         rt2x00dev->dev = pci_dev;
403         rt2x00dev->ops = ops;
404         rt2x00dev->hw = hw;
405
406         retval = rt2x00pci_alloc_reg(rt2x00dev);
407         if (retval)
408                 goto exit_free_device;
409
410         retval = rt2x00lib_probe_dev(rt2x00dev);
411         if (retval)
412                 goto exit_free_reg;
413
414         return 0;
415
416 exit_free_reg:
417         rt2x00pci_free_reg(rt2x00dev);
418
419 exit_free_device:
420         ieee80211_free_hw(hw);
421
422 exit_disable_device:
423         if (retval != -EBUSY)
424                 pci_disable_device(pci_dev);
425
426 exit_release_regions:
427         pci_release_regions(pci_dev);
428
429         pci_set_drvdata(pci_dev, NULL);
430
431         return retval;
432 }
433 EXPORT_SYMBOL_GPL(rt2x00pci_probe);
434
435 void rt2x00pci_remove(struct pci_dev *pci_dev)
436 {
437         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
438         struct rt2x00_dev *rt2x00dev = hw->priv;
439
440         /*
441          * Free all allocated data.
442          */
443         rt2x00lib_remove_dev(rt2x00dev);
444         rt2x00pci_free_reg(rt2x00dev);
445         ieee80211_free_hw(hw);
446
447         /*
448          * Free the PCI device data.
449          */
450         pci_set_drvdata(pci_dev, NULL);
451         pci_disable_device(pci_dev);
452         pci_release_regions(pci_dev);
453 }
454 EXPORT_SYMBOL_GPL(rt2x00pci_remove);
455
456 #ifdef CONFIG_PM
457 int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
458 {
459         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
460         struct rt2x00_dev *rt2x00dev = hw->priv;
461         int retval;
462
463         retval = rt2x00lib_suspend(rt2x00dev, state);
464         if (retval)
465                 return retval;
466
467         rt2x00pci_free_reg(rt2x00dev);
468
469         pci_save_state(pci_dev);
470         pci_disable_device(pci_dev);
471         return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
472 }
473 EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
474
475 int rt2x00pci_resume(struct pci_dev *pci_dev)
476 {
477         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
478         struct rt2x00_dev *rt2x00dev = hw->priv;
479         int retval;
480
481         if (pci_set_power_state(pci_dev, PCI_D0) ||
482             pci_enable_device(pci_dev) ||
483             pci_restore_state(pci_dev)) {
484                 ERROR(rt2x00dev, "Failed to resume device.\n");
485                 return -EIO;
486         }
487
488         retval = rt2x00pci_alloc_reg(rt2x00dev);
489         if (retval)
490                 return retval;
491
492         retval = rt2x00lib_resume(rt2x00dev);
493         if (retval)
494                 goto exit_free_reg;
495
496         return 0;
497
498 exit_free_reg:
499         rt2x00pci_free_reg(rt2x00dev);
500
501         return retval;
502 }
503 EXPORT_SYMBOL_GPL(rt2x00pci_resume);
504 #endif /* CONFIG_PM */
505
506 /*
507  * rt2x00pci module information.
508  */
509 MODULE_AUTHOR(DRV_PROJECT);
510 MODULE_VERSION(DRV_VERSION);
511 MODULE_DESCRIPTION("rt2x00 library");
512 MODULE_LICENSE("GPL");