2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
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.
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.
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.
23 Abstract: rt2x00 generic pci device routines.
24 Supported chipsets: rt2460, rt2560, rt2561, rt2561s & rt2661.
28 * Set enviroment defines for rt2x00.h
30 #define DRV_NAME "rt2x00pci"
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/version.h>
35 #include <linux/init.h>
36 #include <linux/pci.h>
39 #include "rt2x00lib.h"
40 #include "rt2x00pci.h"
45 int rt2x00pci_beacon_update(struct ieee80211_hw
*hw
, struct sk_buff
*skb
,
46 struct ieee80211_tx_control
*control
)
48 struct rt2x00_dev
*rt2x00dev
= hw
->priv
;
49 struct data_ring
*ring
=
50 rt2x00_get_ring(rt2x00dev
, IEEE80211_TX_QUEUE_BEACON
);
51 struct data_entry
*entry
= rt2x00_get_data_entry(ring
);
54 * Just in case the ieee80211 doesn't set this,
55 * but we need this queue set for the descriptor
58 control
->queue
= IEEE80211_TX_QUEUE_BEACON
;
61 * Update the beacon entry.
63 memcpy(entry
->data_addr
, skb
->data
, skb
->len
);
64 rt2x00lib_write_tx_desc(rt2x00dev
, entry
, entry
->priv
,
65 (struct ieee80211_hdr
*)skb
->data
, skb
->len
, control
);
68 * Enable beacon generation.
70 rt2x00dev
->ops
->lib
->kick_tx_queue(rt2x00dev
, control
->queue
);
74 EXPORT_SYMBOL_GPL(rt2x00pci_beacon_update
);
76 void rt2x00pci_beacondone(struct rt2x00_dev
*rt2x00dev
, const int queue
)
78 struct data_ring
*ring
= rt2x00_get_ring(rt2x00dev
, queue
);
79 struct data_entry
*entry
= rt2x00_get_data_entry(ring
);
82 skb
= ieee80211_beacon_get(rt2x00dev
->hw
,
83 rt2x00dev
->interface
.id
, &entry
->tx_status
.control
);
87 rt2x00dev
->ops
->hw
->beacon_update(rt2x00dev
->hw
, skb
,
88 &entry
->tx_status
.control
);
92 EXPORT_SYMBOL_GPL(rt2x00pci_beacondone
);
97 int rt2x00pci_write_tx_data(struct rt2x00_dev
*rt2x00dev
,
98 struct data_ring
*ring
, struct sk_buff
*skb
,
99 struct ieee80211_tx_control
*control
)
101 struct ieee80211_hdr
*ieee80211hdr
= (struct ieee80211_hdr
*)skb
->data
;
102 struct data_entry
*entry
= rt2x00_get_data_entry(ring
);
103 struct data_desc
*txd
= entry
->priv
;
106 if (rt2x00_ring_full(ring
)) {
107 ieee80211_stop_queue(rt2x00dev
->hw
, control
->queue
);
111 rt2x00_desc_read(txd
, 0, &word
);
113 if (rt2x00_get_field32(word
, TXD_ENTRY_OWNER_NIC
) ||
114 rt2x00_get_field32(word
, TXD_ENTRY_VALID
)) {
116 "Arrived at non-free entry in the non-full queue %d.\n"
117 "Please file bug report to %s.\n",
118 control
->queue
, DRV_PROJECT
);
119 ieee80211_stop_queue(rt2x00dev
->hw
, control
->queue
);
124 memcpy(&entry
->tx_status
.control
, control
, sizeof(*control
));
125 memcpy(entry
->data_addr
, skb
->data
, skb
->len
);
126 rt2x00lib_write_tx_desc(rt2x00dev
, entry
, txd
, ieee80211hdr
,
129 rt2x00_ring_index_inc(ring
);
131 if (rt2x00_ring_full(ring
))
132 ieee80211_stop_queue(rt2x00dev
->hw
, control
->queue
);
136 EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data
);
141 void rt2x00pci_rxdone(struct rt2x00_dev
*rt2x00dev
)
143 struct data_ring
*ring
= rt2x00dev
->rx
;
144 struct data_entry
*entry
;
145 struct data_desc
*rxd
;
153 entry
= rt2x00_get_data_entry(ring
);
155 rt2x00_desc_read(rxd
, 0, &desc
);
157 if (rt2x00_get_field32(desc
, RXD_ENTRY_OWNER_NIC
))
160 size
= rt2x00dev
->ops
->lib
->fill_rxdone(
161 entry
, &signal
, &rssi
, &ofdm
);
166 * Send the packet to upper layer.
168 rt2x00lib_rxdone(entry
, entry
->data_addr
, size
,
172 if (test_bit(DEVICE_ENABLED_RADIO
, &ring
->rt2x00dev
->flags
)) {
173 rt2x00_set_field32(&desc
, RXD_ENTRY_OWNER_NIC
, 1);
174 rt2x00_desc_write(rxd
, 0, desc
);
177 rt2x00_ring_index_inc(ring
);
180 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone
);
183 * Device initialization handlers.
185 #define priv_offset(__ring, __i) \
187 ring->data_addr + (i * ring->desc_size); \
190 #define data_addr_offset(__ring, __i) \
192 (__ring)->data_addr \
193 + ((__ring)->stats.limit * (__ring)->desc_size) \
194 + ((__i) * (__ring)->data_size); \
197 #define data_dma_offset(__ring, __i) \
200 + ((__ring)->stats.limit * (__ring)->desc_size) \
201 + ((__i) * (__ring)->data_size); \
204 static int rt2x00pci_alloc_ring(struct rt2x00_dev
*rt2x00dev
,
205 struct data_ring
*ring
)
210 * Allocate DMA memory for descriptor and buffer.
212 ring
->data_addr
= pci_alloc_consistent(rt2x00dev_pci(rt2x00dev
),
213 rt2x00_get_ring_size(ring
), &ring
->data_dma
);
214 if (!ring
->data_addr
)
218 * Initialize all ring entries to contain valid
221 for (i
= 0; i
< ring
->stats
.limit
; i
++) {
222 ring
->entry
[i
].priv
= priv_offset(ring
, i
);
223 ring
->entry
[i
].data_addr
= data_addr_offset(ring
, i
);
224 ring
->entry
[i
].data_dma
= data_dma_offset(ring
, i
);
230 int rt2x00pci_initialize(struct rt2x00_dev
*rt2x00dev
)
232 struct pci_dev
*pci_dev
= rt2x00dev_pci(rt2x00dev
);
233 struct data_ring
*ring
;
239 ring_for_each(rt2x00dev
, ring
) {
240 status
= rt2x00pci_alloc_ring(rt2x00dev
, ring
);
246 * Register interrupt handler.
248 status
= request_irq(pci_dev
->irq
, rt2x00dev
->ops
->lib
->irq_handler
,
249 IRQF_SHARED
, pci_dev
->driver
->name
, rt2x00dev
);
251 ERROR(rt2x00dev
, "IRQ %d allocation failed (error %d).\n",
252 pci_dev
->irq
, status
);
259 rt2x00pci_uninitialize(rt2x00dev
);
263 EXPORT_SYMBOL_GPL(rt2x00pci_initialize
);
265 void rt2x00pci_uninitialize(struct rt2x00_dev
*rt2x00dev
)
267 struct data_ring
*ring
;
272 free_irq(rt2x00dev_pci(rt2x00dev
)->irq
, rt2x00dev
);
277 ring_for_each(rt2x00dev
, ring
) {
279 pci_free_consistent(rt2x00dev_pci(rt2x00dev
),
280 rt2x00_get_ring_size(ring
),
281 ring
->data_addr
, ring
->data_dma
);
282 ring
->data_addr
= NULL
;
285 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize
);
288 * PCI driver handlers.
290 static int rt2x00pci_alloc_csr(struct rt2x00_dev
*rt2x00dev
)
292 rt2x00dev
->csr_addr
= ioremap(
293 pci_resource_start(rt2x00dev_pci(rt2x00dev
), 0),
294 pci_resource_len(rt2x00dev_pci(rt2x00dev
), 0));
295 if (!rt2x00dev
->csr_addr
) {
296 ERROR(rt2x00dev
, "Ioremap failed.\n");
303 static void rt2x00pci_free_csr(struct rt2x00_dev
*rt2x00dev
)
305 if (rt2x00dev
->csr_addr
) {
306 iounmap(rt2x00dev
->csr_addr
);
307 rt2x00dev
->csr_addr
= NULL
;
311 int rt2x00pci_probe(struct pci_dev
*pci_dev
, const struct pci_device_id
*id
)
313 struct rt2x00_ops
*ops
= (struct rt2x00_ops
*)id
->driver_data
;
314 struct ieee80211_hw
*hw
;
315 struct rt2x00_dev
*rt2x00dev
;
318 retval
= pci_request_regions(pci_dev
, pci_name(pci_dev
));
320 ERROR_PROBE("PCI request regions failed.\n");
324 retval
= pci_enable_device(pci_dev
);
326 ERROR_PROBE("Enable device failed.\n");
327 goto exit_release_regions
;
330 pci_set_master(pci_dev
);
332 if (pci_set_mwi(pci_dev
))
333 ERROR_PROBE("MWI not available.\n");
335 if (pci_set_dma_mask(pci_dev
, DMA_64BIT_MASK
) &&
336 pci_set_dma_mask(pci_dev
, DMA_32BIT_MASK
)) {
337 ERROR_PROBE("PCI DMA not supported.\n");
339 goto exit_disable_device
;
342 hw
= ieee80211_alloc_hw(sizeof(struct rt2x00_dev
), ops
->hw
);
344 ERROR_PROBE("Failed to allocate hardware.\n");
346 goto exit_disable_device
;
349 pci_set_drvdata(pci_dev
, hw
);
351 rt2x00dev
= hw
->priv
;
352 rt2x00dev
->dev
= pci_dev
;
353 rt2x00dev
->ops
= ops
;
356 retval
= rt2x00pci_alloc_csr(rt2x00dev
);
358 goto exit_free_device
;
360 retval
= rt2x00lib_probe_dev(rt2x00dev
);
367 rt2x00pci_free_csr(rt2x00dev
);
370 ieee80211_free_hw(hw
);
373 if (retval
!= -EBUSY
)
374 pci_disable_device(pci_dev
);
376 exit_release_regions
:
377 pci_release_regions(pci_dev
);
379 pci_set_drvdata(pci_dev
, NULL
);
383 EXPORT_SYMBOL_GPL(rt2x00pci_probe
);
385 void rt2x00pci_remove(struct pci_dev
*pci_dev
)
387 struct ieee80211_hw
*hw
= pci_get_drvdata(pci_dev
);
388 struct rt2x00_dev
*rt2x00dev
= hw
->priv
;
391 * Free all allocated data.
393 rt2x00lib_remove_dev(rt2x00dev
);
394 ieee80211_free_hw(hw
);
397 * Free the PCI device data.
399 pci_set_drvdata(pci_dev
, NULL
);
400 pci_disable_device(pci_dev
);
401 pci_release_regions(pci_dev
);
403 EXPORT_SYMBOL_GPL(rt2x00pci_remove
);
406 int rt2x00pci_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
408 struct ieee80211_hw
*hw
= pci_get_drvdata(pci_dev
);
409 struct rt2x00_dev
*rt2x00dev
= hw
->priv
;
412 retval
= rt2x00lib_suspend(rt2x00dev
, state
);
416 rt2x00pci_free_csr(rt2x00dev
);
418 pci_save_state(pci_dev
);
419 pci_disable_device(pci_dev
);
420 return pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
));
422 EXPORT_SYMBOL_GPL(rt2x00pci_suspend
);
424 int rt2x00pci_resume(struct pci_dev
*pci_dev
)
426 struct ieee80211_hw
*hw
= pci_get_drvdata(pci_dev
);
427 struct rt2x00_dev
*rt2x00dev
= hw
->priv
;
430 if (pci_set_power_state(pci_dev
, PCI_D0
) ||
431 pci_enable_device(pci_dev
) ||
432 pci_restore_state(pci_dev
)) {
433 ERROR(rt2x00dev
, "Failed to resume device.\n");
437 retval
= rt2x00pci_alloc_csr(rt2x00dev
);
441 return rt2x00lib_resume(rt2x00dev
);
443 EXPORT_SYMBOL_GPL(rt2x00pci_resume
);
444 #endif /* CONFIG_PM */
447 * rt2x00pci module information.
449 MODULE_AUTHOR(DRV_PROJECT
);
450 MODULE_VERSION(DRV_VERSION
);
451 MODULE_DESCRIPTION("rt2x00 library");
452 MODULE_LICENSE("GPL");