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