blob: fa7de41be049c53ac21ddf664c8c7b03b204de5c [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn811aa9c2008-02-03 15:42:53 +01002 Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
Ivo van Doorn95ea3622007-09-25 17:57:13 -07003 <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 Doorn95ea3622007-09-25 17:57:13 -070026#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 Doorn95ea3622007-09-25 17:57:13 -070035 * TX data handlers.
36 */
37int rt2x00pci_write_tx_data(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn181d6902008-02-05 16:42:23 -050038 struct data_queue *queue, struct sk_buff *skb,
Ivo van Doorn95ea3622007-09-25 17:57:13 -070039 struct ieee80211_tx_control *control)
40{
Ivo van Doorn181d6902008-02-05 16:42:23 -050041 struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
Ivo van Doornb8be63f2008-05-10 13:46:03 +020042 struct queue_entry_priv_pci *entry_priv = entry->priv_data;
Ivo van Doorn181d6902008-02-05 16:42:23 -050043 struct skb_frame_desc *skbdesc;
Ivo van Doorn7050ec82008-05-10 13:46:13 +020044 struct txentry_desc txdesc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070045 u32 word;
46
Ivo van Doorn181d6902008-02-05 16:42:23 -050047 if (rt2x00queue_full(queue))
Ivo van Doorn95ea3622007-09-25 17:57:13 -070048 return -EINVAL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070049
Ivo van Doornb8be63f2008-05-10 13:46:03 +020050 rt2x00_desc_read(entry_priv->desc, 0, &word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070051
52 if (rt2x00_get_field32(word, TXD_ENTRY_OWNER_NIC) ||
53 rt2x00_get_field32(word, TXD_ENTRY_VALID)) {
54 ERROR(rt2x00dev,
55 "Arrived at non-free entry in the non-full queue %d.\n"
56 "Please file bug report to %s.\n",
Ivo van Doorne58c6ac2008-04-21 19:00:47 +020057 entry->queue->qid, DRV_PROJECT);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070058 return -EINVAL;
59 }
60
Ivo van Doorn08992f72008-01-24 01:56:25 -080061 /*
Ivo van Doorn7050ec82008-05-10 13:46:13 +020062 * Copy all TX descriptor information into txdesc,
63 * after that we are free to use the skb->cb array
64 * for our information.
65 */
66 entry->skb = skb;
67 rt2x00queue_create_tx_descriptor(entry, &txdesc, control);
68
69 /*
Ivo van Doorn08992f72008-01-24 01:56:25 -080070 * Fill in skb descriptor
71 */
Ivo van Doorn181d6902008-02-05 16:42:23 -050072 skbdesc = get_skb_frame_desc(skb);
Ivo van Doorn181d6902008-02-05 16:42:23 -050073 skbdesc->data = skb->data;
Ivo van Doorn647d0ca2008-02-10 22:51:21 +010074 skbdesc->data_len = skb->len;
Ivo van Doornb8be63f2008-05-10 13:46:03 +020075 skbdesc->desc = entry_priv->desc;
Ivo van Doorn181d6902008-02-05 16:42:23 -050076 skbdesc->desc_len = queue->desc_size;
77 skbdesc->entry = entry;
Ivo van Doorn08992f72008-01-24 01:56:25 -080078
Ivo van Doornb8be63f2008-05-10 13:46:03 +020079 memcpy(&entry_priv->control, control, sizeof(entry_priv->control));
80 memcpy(entry_priv->data, skb->data, skb->len);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070081
Ivo van Doorn7050ec82008-05-10 13:46:13 +020082 rt2x00queue_write_tx_descriptor(entry, &txdesc);
Ivo van Doorn181d6902008-02-05 16:42:23 -050083 rt2x00queue_index_inc(queue, Q_INDEX);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070084
Ivo van Doorn95ea3622007-09-25 17:57:13 -070085 return 0;
86}
87EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
88
89/*
Ivo van Doorn3957ccb2007-11-12 15:02:40 +010090 * TX/RX data handlers.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070091 */
92void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
93{
Ivo van Doorn181d6902008-02-05 16:42:23 -050094 struct data_queue *queue = rt2x00dev->rx;
95 struct queue_entry *entry;
Ivo van Doornb8be63f2008-05-10 13:46:03 +020096 struct queue_entry_priv_pci *entry_priv;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +010097 struct ieee80211_hdr *hdr;
Ivo van Doorn181d6902008-02-05 16:42:23 -050098 struct skb_frame_desc *skbdesc;
99 struct rxdone_entry_desc rxdesc;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100100 int header_size;
101 int align;
Johannes Berg4150c572007-09-17 01:29:23 -0400102 u32 word;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700103
104 while (1) {
Ivo van Doorn181d6902008-02-05 16:42:23 -0500105 entry = rt2x00queue_get_entry(queue, Q_INDEX);
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200106 entry_priv = entry->priv_data;
107 rt2x00_desc_read(entry_priv->desc, 0, &word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700108
Johannes Berg4150c572007-09-17 01:29:23 -0400109 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700110 break;
111
Ivo van Doorn181d6902008-02-05 16:42:23 -0500112 memset(&rxdesc, 0, sizeof(rxdesc));
113 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700114
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200115 hdr = (struct ieee80211_hdr *)entry_priv->data;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100116 header_size =
117 ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
118
119 /*
120 * The data behind the ieee80211 header must be
121 * aligned on a 4 byte boundary.
122 */
Ivo van Doornd101f642008-01-11 20:53:07 +0100123 align = header_size % 4;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100124
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700125 /*
126 * Allocate the sk_buffer, initialize it and copy
127 * all data into it.
128 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500129 entry->skb = dev_alloc_skb(rxdesc.size + align);
130 if (!entry->skb)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700131 return;
132
Ivo van Doorn181d6902008-02-05 16:42:23 -0500133 skb_reserve(entry->skb, align);
134 memcpy(skb_put(entry->skb, rxdesc.size),
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200135 entry_priv->data, rxdesc.size);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700136
137 /*
Ivo van Doorn08992f72008-01-24 01:56:25 -0800138 * Fill in skb descriptor
139 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500140 skbdesc = get_skb_frame_desc(entry->skb);
141 memset(skbdesc, 0, sizeof(*skbdesc));
142 skbdesc->data = entry->skb->data;
Ivo van Doorn647d0ca2008-02-10 22:51:21 +0100143 skbdesc->data_len = entry->skb->len;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200144 skbdesc->desc = entry_priv->desc;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500145 skbdesc->desc_len = queue->desc_size;
Ivo van Doorn08992f72008-01-24 01:56:25 -0800146 skbdesc->entry = entry;
147
148 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700149 * Send the frame to rt2x00lib for further processing.
150 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500151 rt2x00lib_rxdone(entry, &rxdesc);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700152
Ivo van Doorn181d6902008-02-05 16:42:23 -0500153 if (test_bit(DEVICE_ENABLED_RADIO, &queue->rt2x00dev->flags)) {
Johannes Berg4150c572007-09-17 01:29:23 -0400154 rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200155 rt2x00_desc_write(entry_priv->desc, 0, word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700156 }
157
Ivo van Doorn181d6902008-02-05 16:42:23 -0500158 rt2x00queue_index_inc(queue, Q_INDEX);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700159 }
160}
161EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
162
Ivo van Doorn181d6902008-02-05 16:42:23 -0500163void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct queue_entry *entry,
164 struct txdone_entry_desc *txdesc)
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100165{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200166 struct queue_entry_priv_pci *entry_priv = entry->priv_data;
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100167 u32 word;
168
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200169 txdesc->control = &entry_priv->control;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500170 rt2x00lib_txdone(entry, txdesc);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100171
172 /*
173 * Make this entry available for reuse.
174 */
175 entry->flags = 0;
176
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200177 rt2x00_desc_read(entry_priv->desc, 0, &word);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100178 rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
179 rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200180 rt2x00_desc_write(entry_priv->desc, 0, word);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100181
Ivo van Doorn181d6902008-02-05 16:42:23 -0500182 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100183
184 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500185 * If the data queue was full before the txdone handler
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100186 * we must make sure the packet queue in the mac80211 stack
187 * is reenabled when the txdone handler has finished.
188 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500189 if (!rt2x00queue_full(entry->queue))
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200190 ieee80211_wake_queue(rt2x00dev->hw, entry_priv->control.queue);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100191
192}
193EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
194
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700195/*
196 * Device initialization handlers.
197 */
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100198#define desc_size(__queue) \
199({ \
200 ((__queue)->limit * (__queue)->desc_size);\
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700201})
202
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100203#define data_size(__queue) \
204({ \
205 ((__queue)->limit * (__queue)->data_size);\
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700206})
207
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100208#define dma_size(__queue) \
209({ \
210 data_size(__queue) + desc_size(__queue);\
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700211})
212
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100213#define desc_offset(__queue, __base, __i) \
214({ \
215 (__base) + data_size(__queue) + \
216 ((__i) * (__queue)->desc_size); \
217})
218
219#define data_offset(__queue, __base, __i) \
220({ \
221 (__base) + \
222 ((__i) * (__queue)->data_size); \
Ivo van Doorn181d6902008-02-05 16:42:23 -0500223})
224
225static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
226 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700227{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500228 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200229 struct queue_entry_priv_pci *entry_priv;
Ivo van Doorn30b3a232008-02-17 17:33:24 +0100230 void *addr;
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100231 dma_addr_t dma;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700232 unsigned int i;
233
234 /*
235 * Allocate DMA memory for descriptor and buffer.
236 */
Ivo van Doorn30b3a232008-02-17 17:33:24 +0100237 addr = pci_alloc_consistent(pci_dev, dma_size(queue), &dma);
238 if (!addr)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700239 return -ENOMEM;
240
Ivo van Doorn30b3a232008-02-17 17:33:24 +0100241 memset(addr, 0, dma_size(queue));
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100242
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700243 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500244 * Initialize all queue entries to contain valid addresses.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700245 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500246 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200247 entry_priv = queue->entries[i].priv_data;
248 entry_priv->desc = desc_offset(queue, addr, i);
249 entry_priv->desc_dma = desc_offset(queue, dma, i);
250 entry_priv->data = data_offset(queue, addr, i);
251 entry_priv->data_dma = data_offset(queue, dma, i);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700252 }
253
254 return 0;
255}
256
Ivo van Doorn181d6902008-02-05 16:42:23 -0500257static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
258 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700259{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500260 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200261 struct queue_entry_priv_pci *entry_priv =
262 queue->entries[0].priv_data;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500263
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200264 if (entry_priv->data)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500265 pci_free_consistent(pci_dev, dma_size(queue),
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200266 entry_priv->data, entry_priv->data_dma);
267 entry_priv->data = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700268}
269
270int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
271{
272 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500273 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700274 int status;
275
276 /*
277 * Allocate DMA
278 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500279 queue_for_each(rt2x00dev, queue) {
280 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700281 if (status)
282 goto exit;
283 }
284
285 /*
286 * Register interrupt handler.
287 */
288 status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
289 IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
290 if (status) {
291 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
292 pci_dev->irq, status);
Ivo van Doornb30cdfc2008-05-05 17:24:03 +0200293 goto exit;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700294 }
295
296 return 0;
297
298exit:
Ivo van Doornb30cdfc2008-05-05 17:24:03 +0200299 queue_for_each(rt2x00dev, queue)
300 rt2x00pci_free_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700301
302 return status;
303}
304EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
305
306void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
307{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500308 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700309
310 /*
311 * Free irq line.
312 */
313 free_irq(rt2x00dev_pci(rt2x00dev)->irq, rt2x00dev);
314
315 /*
316 * Free DMA
317 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500318 queue_for_each(rt2x00dev, queue)
319 rt2x00pci_free_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700320}
321EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
322
323/*
324 * PCI driver handlers.
325 */
326static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
327{
328 kfree(rt2x00dev->rf);
329 rt2x00dev->rf = NULL;
330
331 kfree(rt2x00dev->eeprom);
332 rt2x00dev->eeprom = NULL;
333
Ivo van Doorn21795092008-02-10 22:49:13 +0100334 if (rt2x00dev->csr.base) {
335 iounmap(rt2x00dev->csr.base);
336 rt2x00dev->csr.base = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700337 }
338}
339
340static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
341{
342 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
343
Ivo van Doorn21795092008-02-10 22:49:13 +0100344 rt2x00dev->csr.base = ioremap(pci_resource_start(pci_dev, 0),
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700345 pci_resource_len(pci_dev, 0));
Ivo van Doorn21795092008-02-10 22:49:13 +0100346 if (!rt2x00dev->csr.base)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700347 goto exit;
348
349 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
350 if (!rt2x00dev->eeprom)
351 goto exit;
352
353 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
354 if (!rt2x00dev->rf)
355 goto exit;
356
357 return 0;
358
359exit:
360 ERROR_PROBE("Failed to allocate registers.\n");
361
362 rt2x00pci_free_reg(rt2x00dev);
363
364 return -ENOMEM;
365}
366
367int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
368{
369 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
370 struct ieee80211_hw *hw;
371 struct rt2x00_dev *rt2x00dev;
372 int retval;
373
374 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
375 if (retval) {
376 ERROR_PROBE("PCI request regions failed.\n");
377 return retval;
378 }
379
380 retval = pci_enable_device(pci_dev);
381 if (retval) {
382 ERROR_PROBE("Enable device failed.\n");
383 goto exit_release_regions;
384 }
385
386 pci_set_master(pci_dev);
387
388 if (pci_set_mwi(pci_dev))
389 ERROR_PROBE("MWI not available.\n");
390
391 if (pci_set_dma_mask(pci_dev, DMA_64BIT_MASK) &&
392 pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)) {
393 ERROR_PROBE("PCI DMA not supported.\n");
394 retval = -EIO;
395 goto exit_disable_device;
396 }
397
398 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
399 if (!hw) {
400 ERROR_PROBE("Failed to allocate hardware.\n");
401 retval = -ENOMEM;
402 goto exit_disable_device;
403 }
404
405 pci_set_drvdata(pci_dev, hw);
406
407 rt2x00dev = hw->priv;
408 rt2x00dev->dev = pci_dev;
409 rt2x00dev->ops = ops;
410 rt2x00dev->hw = hw;
411
412 retval = rt2x00pci_alloc_reg(rt2x00dev);
413 if (retval)
414 goto exit_free_device;
415
416 retval = rt2x00lib_probe_dev(rt2x00dev);
417 if (retval)
418 goto exit_free_reg;
419
420 return 0;
421
422exit_free_reg:
423 rt2x00pci_free_reg(rt2x00dev);
424
425exit_free_device:
426 ieee80211_free_hw(hw);
427
428exit_disable_device:
429 if (retval != -EBUSY)
430 pci_disable_device(pci_dev);
431
432exit_release_regions:
433 pci_release_regions(pci_dev);
434
435 pci_set_drvdata(pci_dev, NULL);
436
437 return retval;
438}
439EXPORT_SYMBOL_GPL(rt2x00pci_probe);
440
441void rt2x00pci_remove(struct pci_dev *pci_dev)
442{
443 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
444 struct rt2x00_dev *rt2x00dev = hw->priv;
445
446 /*
447 * Free all allocated data.
448 */
449 rt2x00lib_remove_dev(rt2x00dev);
450 rt2x00pci_free_reg(rt2x00dev);
451 ieee80211_free_hw(hw);
452
453 /*
454 * Free the PCI device data.
455 */
456 pci_set_drvdata(pci_dev, NULL);
457 pci_disable_device(pci_dev);
458 pci_release_regions(pci_dev);
459}
460EXPORT_SYMBOL_GPL(rt2x00pci_remove);
461
462#ifdef CONFIG_PM
463int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
464{
465 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
466 struct rt2x00_dev *rt2x00dev = hw->priv;
467 int retval;
468
469 retval = rt2x00lib_suspend(rt2x00dev, state);
470 if (retval)
471 return retval;
472
473 rt2x00pci_free_reg(rt2x00dev);
474
475 pci_save_state(pci_dev);
476 pci_disable_device(pci_dev);
477 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
478}
479EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
480
481int rt2x00pci_resume(struct pci_dev *pci_dev)
482{
483 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
484 struct rt2x00_dev *rt2x00dev = hw->priv;
485 int retval;
486
487 if (pci_set_power_state(pci_dev, PCI_D0) ||
488 pci_enable_device(pci_dev) ||
489 pci_restore_state(pci_dev)) {
490 ERROR(rt2x00dev, "Failed to resume device.\n");
491 return -EIO;
492 }
493
494 retval = rt2x00pci_alloc_reg(rt2x00dev);
495 if (retval)
496 return retval;
497
498 retval = rt2x00lib_resume(rt2x00dev);
499 if (retval)
500 goto exit_free_reg;
501
502 return 0;
503
504exit_free_reg:
505 rt2x00pci_free_reg(rt2x00dev);
506
507 return retval;
508}
509EXPORT_SYMBOL_GPL(rt2x00pci_resume);
510#endif /* CONFIG_PM */
511
512/*
513 * rt2x00pci module information.
514 */
515MODULE_AUTHOR(DRV_PROJECT);
516MODULE_VERSION(DRV_VERSION);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500517MODULE_DESCRIPTION("rt2x00 pci library");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700518MODULE_LICENSE("GPL");