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