Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1 | /* |
Ivo van Doorn | 811aa9c | 2008-02-03 15:42:53 +0100 | [diff] [blame] | 2 | Copyright (C) 2004 - 2008 rt2x00 SourceForge Project |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 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 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 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 | /* |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 35 | * TX data handlers. |
| 36 | */ |
Ivo van Doorn | 6db3786 | 2008-06-06 22:50:28 +0200 | [diff] [blame] | 37 | int rt2x00pci_write_tx_data(struct queue_entry *entry) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 38 | { |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 39 | struct queue_entry_priv_pci *entry_priv = entry->priv_data; |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 40 | struct skb_frame_desc *skbdesc; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 41 | u32 word; |
| 42 | |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 43 | rt2x00_desc_read(entry_priv->desc, 0, &word); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 44 | |
Ivo van Doorn | 6db3786 | 2008-06-06 22:50:28 +0200 | [diff] [blame] | 45 | /* |
| 46 | * This should not happen, we already checked the entry |
| 47 | * was ours. When the hardware disagrees there has been |
| 48 | * a queue corruption! |
| 49 | */ |
| 50 | if (unlikely(rt2x00_get_field32(word, TXD_ENTRY_OWNER_NIC) || |
| 51 | rt2x00_get_field32(word, TXD_ENTRY_VALID))) { |
| 52 | ERROR(entry->queue->rt2x00dev, |
| 53 | "Corrupt queue %d, accessing entry which is not ours.\n" |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 54 | "Please file bug report to %s.\n", |
Ivo van Doorn | e58c6ac | 2008-04-21 19:00:47 +0200 | [diff] [blame] | 55 | entry->queue->qid, DRV_PROJECT); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 56 | return -EINVAL; |
| 57 | } |
| 58 | |
Ivo van Doorn | 08992f7 | 2008-01-24 01:56:25 -0800 | [diff] [blame] | 59 | /* |
| 60 | * Fill in skb descriptor |
| 61 | */ |
Ivo van Doorn | 6db3786 | 2008-06-06 22:50:28 +0200 | [diff] [blame] | 62 | skbdesc = get_skb_frame_desc(entry->skb); |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 63 | memset(skbdesc, 0, sizeof(*skbdesc)); |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 64 | skbdesc->desc = entry_priv->desc; |
Ivo van Doorn | 6db3786 | 2008-06-06 22:50:28 +0200 | [diff] [blame] | 65 | skbdesc->desc_len = entry->queue->desc_size; |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 66 | skbdesc->entry = entry; |
Ivo van Doorn | 08992f7 | 2008-01-24 01:56:25 -0800 | [diff] [blame] | 67 | |
Ivo van Doorn | 6db3786 | 2008-06-06 22:50:28 +0200 | [diff] [blame] | 68 | memcpy(entry_priv->data, entry->skb->data, entry->skb->len); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 69 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data); |
| 73 | |
| 74 | /* |
Ivo van Doorn | 3957ccb | 2007-11-12 15:02:40 +0100 | [diff] [blame] | 75 | * TX/RX data handlers. |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 76 | */ |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame^] | 77 | static void rt2x00pci_rxdone_entry(struct rt2x00_dev *rt2x00dev, |
| 78 | struct queue_entry *entry) |
| 79 | { |
| 80 | struct sk_buff *skb; |
| 81 | struct skb_frame_desc *skbdesc; |
| 82 | struct rxdone_entry_desc rxdesc; |
| 83 | struct queue_entry_priv_pci *entry_priv = entry->priv_data; |
| 84 | |
| 85 | /* |
| 86 | * Allocate a new sk_buffer. If no new buffer available, drop the |
| 87 | * received frame and reuse the existing buffer. |
| 88 | */ |
| 89 | skb = rt2x00queue_alloc_skb(entry->queue); |
| 90 | if (!skb) |
| 91 | return; |
| 92 | |
| 93 | /* |
| 94 | * Extract the RXD details. |
| 95 | */ |
| 96 | memset(&rxdesc, 0, sizeof(rxdesc)); |
| 97 | rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc); |
| 98 | |
| 99 | /* |
| 100 | * Copy the received data to the entries' skb. |
| 101 | */ |
| 102 | memcpy(entry->skb->data, entry_priv->data, rxdesc.size); |
| 103 | skb_trim(entry->skb, rxdesc.size); |
| 104 | |
| 105 | /* |
| 106 | * Fill in skb descriptor |
| 107 | */ |
| 108 | skbdesc = get_skb_frame_desc(entry->skb); |
| 109 | memset(skbdesc, 0, sizeof(*skbdesc)); |
| 110 | skbdesc->desc = entry_priv->desc; |
| 111 | skbdesc->desc_len = entry->queue->desc_size; |
| 112 | skbdesc->entry = entry; |
| 113 | |
| 114 | /* |
| 115 | * Send the frame to rt2x00lib for further processing. |
| 116 | */ |
| 117 | rt2x00lib_rxdone(entry, &rxdesc); |
| 118 | |
| 119 | /* |
| 120 | * Replace the entries' skb with the newly allocated one. |
| 121 | */ |
| 122 | entry->skb = skb; |
| 123 | } |
| 124 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 125 | void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev) |
| 126 | { |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 127 | struct data_queue *queue = rt2x00dev->rx; |
| 128 | struct queue_entry *entry; |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 129 | struct queue_entry_priv_pci *entry_priv; |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 130 | u32 word; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 131 | |
| 132 | while (1) { |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 133 | entry = rt2x00queue_get_entry(queue, Q_INDEX); |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 134 | entry_priv = entry->priv_data; |
| 135 | rt2x00_desc_read(entry_priv->desc, 0, &word); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 136 | |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 137 | if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC)) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 138 | break; |
| 139 | |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame^] | 140 | rt2x00pci_rxdone_entry(rt2x00dev, entry); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 141 | |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 142 | if (test_bit(DEVICE_ENABLED_RADIO, &queue->rt2x00dev->flags)) { |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 143 | rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1); |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 144 | rt2x00_desc_write(entry_priv->desc, 0, word); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 147 | rt2x00queue_index_inc(queue, Q_INDEX); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | EXPORT_SYMBOL_GPL(rt2x00pci_rxdone); |
| 151 | |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 152 | void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct queue_entry *entry, |
| 153 | struct txdone_entry_desc *txdesc) |
Ivo van Doorn | 3957ccb | 2007-11-12 15:02:40 +0100 | [diff] [blame] | 154 | { |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 155 | struct queue_entry_priv_pci *entry_priv = entry->priv_data; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 156 | enum data_queue_qid qid = skb_get_queue_mapping(entry->skb); |
Ivo van Doorn | 3957ccb | 2007-11-12 15:02:40 +0100 | [diff] [blame] | 157 | u32 word; |
| 158 | |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 159 | rt2x00lib_txdone(entry, txdesc); |
Ivo van Doorn | 3957ccb | 2007-11-12 15:02:40 +0100 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * Make this entry available for reuse. |
| 163 | */ |
| 164 | entry->flags = 0; |
| 165 | |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 166 | rt2x00_desc_read(entry_priv->desc, 0, &word); |
Ivo van Doorn | 3957ccb | 2007-11-12 15:02:40 +0100 | [diff] [blame] | 167 | rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0); |
| 168 | rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0); |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 169 | rt2x00_desc_write(entry_priv->desc, 0, word); |
Ivo van Doorn | 3957ccb | 2007-11-12 15:02:40 +0100 | [diff] [blame] | 170 | |
Ivo van Doorn | 6db3786 | 2008-06-06 22:50:28 +0200 | [diff] [blame] | 171 | __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 172 | rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE); |
Ivo van Doorn | 3957ccb | 2007-11-12 15:02:40 +0100 | [diff] [blame] | 173 | |
| 174 | /* |
Ivo van Doorn | b869767 | 2008-06-06 22:53:14 +0200 | [diff] [blame] | 175 | * If the data queue was below the threshold before the txdone |
| 176 | * handler we must make sure the packet queue in the mac80211 stack |
Ivo van Doorn | 3957ccb | 2007-11-12 15:02:40 +0100 | [diff] [blame] | 177 | * is reenabled when the txdone handler has finished. |
| 178 | */ |
Ivo van Doorn | b869767 | 2008-06-06 22:53:14 +0200 | [diff] [blame] | 179 | if (!rt2x00queue_threshold(entry->queue)) |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 180 | ieee80211_wake_queue(rt2x00dev->hw, qid); |
Ivo van Doorn | 3957ccb | 2007-11-12 15:02:40 +0100 | [diff] [blame] | 181 | |
| 182 | } |
| 183 | EXPORT_SYMBOL_GPL(rt2x00pci_txdone); |
| 184 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 185 | /* |
| 186 | * Device initialization handlers. |
| 187 | */ |
Ivo van Doorn | 9c9dd2c | 2008-02-10 22:46:52 +0100 | [diff] [blame] | 188 | #define desc_size(__queue) \ |
| 189 | ({ \ |
| 190 | ((__queue)->limit * (__queue)->desc_size);\ |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 191 | }) |
| 192 | |
Ivo van Doorn | 9c9dd2c | 2008-02-10 22:46:52 +0100 | [diff] [blame] | 193 | #define data_size(__queue) \ |
| 194 | ({ \ |
| 195 | ((__queue)->limit * (__queue)->data_size);\ |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 196 | }) |
| 197 | |
Ivo van Doorn | 9c9dd2c | 2008-02-10 22:46:52 +0100 | [diff] [blame] | 198 | #define dma_size(__queue) \ |
| 199 | ({ \ |
| 200 | data_size(__queue) + desc_size(__queue);\ |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 201 | }) |
| 202 | |
Ivo van Doorn | 9c9dd2c | 2008-02-10 22:46:52 +0100 | [diff] [blame] | 203 | #define desc_offset(__queue, __base, __i) \ |
| 204 | ({ \ |
| 205 | (__base) + data_size(__queue) + \ |
| 206 | ((__i) * (__queue)->desc_size); \ |
| 207 | }) |
| 208 | |
| 209 | #define data_offset(__queue, __base, __i) \ |
| 210 | ({ \ |
| 211 | (__base) + \ |
| 212 | ((__i) * (__queue)->data_size); \ |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 213 | }) |
| 214 | |
| 215 | static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev, |
| 216 | struct data_queue *queue) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 217 | { |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 218 | struct queue_entry_priv_pci *entry_priv; |
Ivo van Doorn | 30b3a23 | 2008-02-17 17:33:24 +0100 | [diff] [blame] | 219 | void *addr; |
Ivo van Doorn | 9c9dd2c | 2008-02-10 22:46:52 +0100 | [diff] [blame] | 220 | dma_addr_t dma; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 221 | unsigned int i; |
| 222 | |
| 223 | /* |
| 224 | * Allocate DMA memory for descriptor and buffer. |
| 225 | */ |
Gertjan van Wingerde | 14a3bf8 | 2008-06-16 19:55:43 +0200 | [diff] [blame] | 226 | addr = dma_alloc_coherent(rt2x00dev->dev, dma_size(queue), &dma, |
| 227 | GFP_KERNEL | GFP_DMA); |
Ivo van Doorn | 30b3a23 | 2008-02-17 17:33:24 +0100 | [diff] [blame] | 228 | if (!addr) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 229 | return -ENOMEM; |
| 230 | |
Ivo van Doorn | 30b3a23 | 2008-02-17 17:33:24 +0100 | [diff] [blame] | 231 | memset(addr, 0, dma_size(queue)); |
Ivo van Doorn | 9c9dd2c | 2008-02-10 22:46:52 +0100 | [diff] [blame] | 232 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 233 | /* |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 234 | * Initialize all queue entries to contain valid addresses. |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 235 | */ |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 236 | for (i = 0; i < queue->limit; i++) { |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 237 | entry_priv = queue->entries[i].priv_data; |
| 238 | entry_priv->desc = desc_offset(queue, addr, i); |
| 239 | entry_priv->desc_dma = desc_offset(queue, dma, i); |
| 240 | entry_priv->data = data_offset(queue, addr, i); |
| 241 | entry_priv->data_dma = data_offset(queue, dma, i); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 247 | static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev, |
| 248 | struct data_queue *queue) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 249 | { |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 250 | struct queue_entry_priv_pci *entry_priv = |
| 251 | queue->entries[0].priv_data; |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 252 | |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 253 | if (entry_priv->data) |
Gertjan van Wingerde | 14a3bf8 | 2008-06-16 19:55:43 +0200 | [diff] [blame] | 254 | dma_free_coherent(rt2x00dev->dev, dma_size(queue), |
| 255 | entry_priv->data, entry_priv->data_dma); |
Ivo van Doorn | b8be63f | 2008-05-10 13:46:03 +0200 | [diff] [blame] | 256 | entry_priv->data = NULL; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev) |
| 260 | { |
Gertjan van Wingerde | 14a3bf8 | 2008-06-16 19:55:43 +0200 | [diff] [blame] | 261 | struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 262 | struct data_queue *queue; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 263 | int status; |
| 264 | |
| 265 | /* |
| 266 | * Allocate DMA |
| 267 | */ |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 268 | queue_for_each(rt2x00dev, queue) { |
| 269 | status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 270 | if (status) |
| 271 | goto exit; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Register interrupt handler. |
| 276 | */ |
| 277 | status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler, |
| 278 | IRQF_SHARED, pci_name(pci_dev), rt2x00dev); |
| 279 | if (status) { |
| 280 | ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n", |
| 281 | pci_dev->irq, status); |
Ivo van Doorn | b30cdfc | 2008-05-05 17:24:03 +0200 | [diff] [blame] | 282 | goto exit; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | return 0; |
| 286 | |
| 287 | exit: |
Ivo van Doorn | b30cdfc | 2008-05-05 17:24:03 +0200 | [diff] [blame] | 288 | queue_for_each(rt2x00dev, queue) |
| 289 | rt2x00pci_free_queue_dma(rt2x00dev, queue); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 290 | |
| 291 | return status; |
| 292 | } |
| 293 | EXPORT_SYMBOL_GPL(rt2x00pci_initialize); |
| 294 | |
| 295 | void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev) |
| 296 | { |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 297 | struct data_queue *queue; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 298 | |
| 299 | /* |
| 300 | * Free irq line. |
| 301 | */ |
Gertjan van Wingerde | 14a3bf8 | 2008-06-16 19:55:43 +0200 | [diff] [blame] | 302 | free_irq(to_pci_dev(rt2x00dev->dev)->irq, rt2x00dev); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 303 | |
| 304 | /* |
| 305 | * Free DMA |
| 306 | */ |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 307 | queue_for_each(rt2x00dev, queue) |
| 308 | rt2x00pci_free_queue_dma(rt2x00dev, queue); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 309 | } |
| 310 | EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize); |
| 311 | |
| 312 | /* |
| 313 | * PCI driver handlers. |
| 314 | */ |
| 315 | static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev) |
| 316 | { |
| 317 | kfree(rt2x00dev->rf); |
| 318 | rt2x00dev->rf = NULL; |
| 319 | |
| 320 | kfree(rt2x00dev->eeprom); |
| 321 | rt2x00dev->eeprom = NULL; |
| 322 | |
Ivo van Doorn | 2179509 | 2008-02-10 22:49:13 +0100 | [diff] [blame] | 323 | if (rt2x00dev->csr.base) { |
| 324 | iounmap(rt2x00dev->csr.base); |
| 325 | rt2x00dev->csr.base = NULL; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev) |
| 330 | { |
Gertjan van Wingerde | 14a3bf8 | 2008-06-16 19:55:43 +0200 | [diff] [blame] | 331 | struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 332 | |
Ivo van Doorn | 2179509 | 2008-02-10 22:49:13 +0100 | [diff] [blame] | 333 | rt2x00dev->csr.base = ioremap(pci_resource_start(pci_dev, 0), |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 334 | pci_resource_len(pci_dev, 0)); |
Ivo van Doorn | 2179509 | 2008-02-10 22:49:13 +0100 | [diff] [blame] | 335 | if (!rt2x00dev->csr.base) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 336 | goto exit; |
| 337 | |
| 338 | rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL); |
| 339 | if (!rt2x00dev->eeprom) |
| 340 | goto exit; |
| 341 | |
| 342 | rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL); |
| 343 | if (!rt2x00dev->rf) |
| 344 | goto exit; |
| 345 | |
| 346 | return 0; |
| 347 | |
| 348 | exit: |
| 349 | ERROR_PROBE("Failed to allocate registers.\n"); |
| 350 | |
| 351 | rt2x00pci_free_reg(rt2x00dev); |
| 352 | |
| 353 | return -ENOMEM; |
| 354 | } |
| 355 | |
| 356 | int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id) |
| 357 | { |
| 358 | struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data; |
| 359 | struct ieee80211_hw *hw; |
| 360 | struct rt2x00_dev *rt2x00dev; |
| 361 | int retval; |
| 362 | |
| 363 | retval = pci_request_regions(pci_dev, pci_name(pci_dev)); |
| 364 | if (retval) { |
| 365 | ERROR_PROBE("PCI request regions failed.\n"); |
| 366 | return retval; |
| 367 | } |
| 368 | |
| 369 | retval = pci_enable_device(pci_dev); |
| 370 | if (retval) { |
| 371 | ERROR_PROBE("Enable device failed.\n"); |
| 372 | goto exit_release_regions; |
| 373 | } |
| 374 | |
| 375 | pci_set_master(pci_dev); |
| 376 | |
| 377 | if (pci_set_mwi(pci_dev)) |
| 378 | ERROR_PROBE("MWI not available.\n"); |
| 379 | |
Gertjan van Wingerde | 14a3bf8 | 2008-06-16 19:55:43 +0200 | [diff] [blame] | 380 | if (dma_set_mask(&pci_dev->dev, DMA_32BIT_MASK)) { |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 381 | ERROR_PROBE("PCI DMA not supported.\n"); |
| 382 | retval = -EIO; |
| 383 | goto exit_disable_device; |
| 384 | } |
| 385 | |
| 386 | hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw); |
| 387 | if (!hw) { |
| 388 | ERROR_PROBE("Failed to allocate hardware.\n"); |
| 389 | retval = -ENOMEM; |
| 390 | goto exit_disable_device; |
| 391 | } |
| 392 | |
| 393 | pci_set_drvdata(pci_dev, hw); |
| 394 | |
| 395 | rt2x00dev = hw->priv; |
Gertjan van Wingerde | 14a3bf8 | 2008-06-16 19:55:43 +0200 | [diff] [blame] | 396 | rt2x00dev->dev = &pci_dev->dev; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 397 | rt2x00dev->ops = ops; |
| 398 | rt2x00dev->hw = hw; |
| 399 | |
| 400 | retval = rt2x00pci_alloc_reg(rt2x00dev); |
| 401 | if (retval) |
| 402 | goto exit_free_device; |
| 403 | |
| 404 | retval = rt2x00lib_probe_dev(rt2x00dev); |
| 405 | if (retval) |
| 406 | goto exit_free_reg; |
| 407 | |
| 408 | return 0; |
| 409 | |
| 410 | exit_free_reg: |
| 411 | rt2x00pci_free_reg(rt2x00dev); |
| 412 | |
| 413 | exit_free_device: |
| 414 | ieee80211_free_hw(hw); |
| 415 | |
| 416 | exit_disable_device: |
| 417 | if (retval != -EBUSY) |
| 418 | pci_disable_device(pci_dev); |
| 419 | |
| 420 | exit_release_regions: |
| 421 | pci_release_regions(pci_dev); |
| 422 | |
| 423 | pci_set_drvdata(pci_dev, NULL); |
| 424 | |
| 425 | return retval; |
| 426 | } |
| 427 | EXPORT_SYMBOL_GPL(rt2x00pci_probe); |
| 428 | |
| 429 | void rt2x00pci_remove(struct pci_dev *pci_dev) |
| 430 | { |
| 431 | struct ieee80211_hw *hw = pci_get_drvdata(pci_dev); |
| 432 | struct rt2x00_dev *rt2x00dev = hw->priv; |
| 433 | |
| 434 | /* |
| 435 | * Free all allocated data. |
| 436 | */ |
| 437 | rt2x00lib_remove_dev(rt2x00dev); |
| 438 | rt2x00pci_free_reg(rt2x00dev); |
| 439 | ieee80211_free_hw(hw); |
| 440 | |
| 441 | /* |
| 442 | * Free the PCI device data. |
| 443 | */ |
| 444 | pci_set_drvdata(pci_dev, NULL); |
| 445 | pci_disable_device(pci_dev); |
| 446 | pci_release_regions(pci_dev); |
| 447 | } |
| 448 | EXPORT_SYMBOL_GPL(rt2x00pci_remove); |
| 449 | |
| 450 | #ifdef CONFIG_PM |
| 451 | int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state) |
| 452 | { |
| 453 | struct ieee80211_hw *hw = pci_get_drvdata(pci_dev); |
| 454 | struct rt2x00_dev *rt2x00dev = hw->priv; |
| 455 | int retval; |
| 456 | |
| 457 | retval = rt2x00lib_suspend(rt2x00dev, state); |
| 458 | if (retval) |
| 459 | return retval; |
| 460 | |
| 461 | rt2x00pci_free_reg(rt2x00dev); |
| 462 | |
| 463 | pci_save_state(pci_dev); |
| 464 | pci_disable_device(pci_dev); |
| 465 | return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state)); |
| 466 | } |
| 467 | EXPORT_SYMBOL_GPL(rt2x00pci_suspend); |
| 468 | |
| 469 | int rt2x00pci_resume(struct pci_dev *pci_dev) |
| 470 | { |
| 471 | struct ieee80211_hw *hw = pci_get_drvdata(pci_dev); |
| 472 | struct rt2x00_dev *rt2x00dev = hw->priv; |
| 473 | int retval; |
| 474 | |
| 475 | if (pci_set_power_state(pci_dev, PCI_D0) || |
| 476 | pci_enable_device(pci_dev) || |
| 477 | pci_restore_state(pci_dev)) { |
| 478 | ERROR(rt2x00dev, "Failed to resume device.\n"); |
| 479 | return -EIO; |
| 480 | } |
| 481 | |
| 482 | retval = rt2x00pci_alloc_reg(rt2x00dev); |
| 483 | if (retval) |
| 484 | return retval; |
| 485 | |
| 486 | retval = rt2x00lib_resume(rt2x00dev); |
| 487 | if (retval) |
| 488 | goto exit_free_reg; |
| 489 | |
| 490 | return 0; |
| 491 | |
| 492 | exit_free_reg: |
| 493 | rt2x00pci_free_reg(rt2x00dev); |
| 494 | |
| 495 | return retval; |
| 496 | } |
| 497 | EXPORT_SYMBOL_GPL(rt2x00pci_resume); |
| 498 | #endif /* CONFIG_PM */ |
| 499 | |
| 500 | /* |
| 501 | * rt2x00pci module information. |
| 502 | */ |
| 503 | MODULE_AUTHOR(DRV_PROJECT); |
| 504 | MODULE_VERSION(DRV_VERSION); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 505 | MODULE_DESCRIPTION("rt2x00 pci library"); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 506 | MODULE_LICENSE("GPL"); |