p54pci.c 17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

/*
 * Linux device driver for PCI based Prism54
 *
 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
 *
 * Based on the islsm (softmac prism54) driver, which is:
 * Copyright 2004-2006 Jean-Baptiste Note <jean-baptiste.note@m4x.org>, et al.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/init.h>
#include <linux/pci.h>
#include <linux/firmware.h>
#include <linux/etherdevice.h>
#include <linux/delay.h>
#include <linux/completion.h>
#include <net/mac80211.h>

#include "p54.h"
#include "p54pci.h"

MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
MODULE_DESCRIPTION("Prism54 PCI wireless driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("prism54pci");

static struct pci_device_id p54p_table[] __devinitdata = {
	/* Intersil PRISM Duette/Prism GT Wireless LAN adapter */
	{ PCI_DEVICE(0x1260, 0x3890) },
	/* 3COM 3CRWE154G72 Wireless LAN adapter */
	{ PCI_DEVICE(0x10b7, 0x6001) },
	/* Intersil PRISM Indigo Wireless LAN adapter */
	{ PCI_DEVICE(0x1260, 0x3877) },
	/* Intersil PRISM Javelin/Xbow Wireless LAN adapter */
	{ PCI_DEVICE(0x1260, 0x3886) },
Andrew Morton's avatar
Andrew Morton committed
40
	{ },
41
42
43
44
45
46
47
48
49
50
};

MODULE_DEVICE_TABLE(pci, p54p_table);

static int p54p_upload_firmware(struct ieee80211_hw *dev)
{
	struct p54p_priv *priv = dev->priv;
	const struct firmware *fw_entry = NULL;
	__le32 reg;
	int err;
51
	__le32 *data;
52
53
	u32 remains, left, device_addr;

54
	P54P_WRITE(int_enable, cpu_to_le32(0));
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
	P54P_READ(int_enable);
	udelay(10);

	reg = P54P_READ(ctrl_stat);
	reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
	reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RAMBOOT);
	P54P_WRITE(ctrl_stat, reg);
	P54P_READ(ctrl_stat);
	udelay(10);

	reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
	P54P_WRITE(ctrl_stat, reg);
	wmb();
	udelay(10);

	reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
	P54P_WRITE(ctrl_stat, reg);
	wmb();

	mdelay(50);

	err = request_firmware(&fw_entry, "isl3886", &priv->pdev->dev);
	if (err) {
		printk(KERN_ERR "%s (prism54pci): cannot find firmware "
		       "(isl3886)\n", pci_name(priv->pdev));
		return err;
	}

	p54_parse_firmware(dev, fw_entry);

85
	data = (__le32 *) fw_entry->data;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	remains = fw_entry->size;
	device_addr = ISL38XX_DEV_FIRMWARE_ADDR;
	while (remains) {
		u32 i = 0;
		left = min((u32)0x1000, remains);
		P54P_WRITE(direct_mem_base, cpu_to_le32(device_addr));
		P54P_READ(int_enable);

		device_addr += 0x1000;
		while (i < left) {
			P54P_WRITE(direct_mem_win[i], *data++);
			i += sizeof(u32);
		}

		remains -= left;
		P54P_READ(int_enable);
	}

	release_firmware(fw_entry);

	reg = P54P_READ(ctrl_stat);
	reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_CLKRUN);
	reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
	reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RAMBOOT);
	P54P_WRITE(ctrl_stat, reg);
	P54P_READ(ctrl_stat);
	udelay(10);

	reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
	P54P_WRITE(ctrl_stat, reg);
	wmb();
	udelay(10);

	reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
	P54P_WRITE(ctrl_stat, reg);
	wmb();
	udelay(10);

	return 0;
}

static irqreturn_t p54p_simple_interrupt(int irq, void *dev_id)
{
	struct p54p_priv *priv = (struct p54p_priv *) dev_id;
	__le32 reg;

	reg = P54P_READ(int_ident);
	P54P_WRITE(int_ack, reg);

	if (reg & P54P_READ(int_enable))
		complete(&priv->boot_comp);

	return IRQ_HANDLED;
}

static int p54p_read_eeprom(struct ieee80211_hw *dev)
{
	struct p54p_priv *priv = dev->priv;
144
	struct p54p_ring_control *ring_control = priv->ring_control;
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
	int err;
	struct p54_control_hdr *hdr;
	void *eeprom;
	dma_addr_t rx_mapping, tx_mapping;
	u16 alen;

	init_completion(&priv->boot_comp);
	err = request_irq(priv->pdev->irq, &p54p_simple_interrupt,
			  IRQF_SHARED, "prism54pci", priv);
	if (err) {
		printk(KERN_ERR "%s (prism54pci): failed to register IRQ handler\n",
		       pci_name(priv->pdev));
		return err;
	}

	eeprom = kmalloc(0x2010 + EEPROM_READBACK_LEN, GFP_KERNEL);
	if (!eeprom) {
		printk(KERN_ERR "%s (prism54pci): no memory for eeprom!\n",
		       pci_name(priv->pdev));
		err = -ENOMEM;
		goto out;
	}

168
	memset(ring_control, 0, sizeof(*ring_control));
169
	P54P_WRITE(ring_control_base, cpu_to_le32(priv->ring_control_dma));
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
	P54P_READ(ring_control_base);
	udelay(10);

	P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_INIT));
	P54P_READ(int_enable);
	udelay(10);

	P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));

	if (!wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ)) {
		printk(KERN_ERR "%s (prism54pci): Cannot boot firmware!\n",
		       pci_name(priv->pdev));
		err = -EINVAL;
		goto out;
	}

	P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_UPDATE));
	P54P_READ(int_enable);

	hdr = eeprom + 0x2010;
	p54_fill_eeprom_readback(hdr);
	hdr->req_id = cpu_to_le32(priv->common.rx_start);

	rx_mapping = pci_map_single(priv->pdev, eeprom,
				    0x2010, PCI_DMA_FROMDEVICE);
	tx_mapping = pci_map_single(priv->pdev, (void *)hdr,
				    EEPROM_READBACK_LEN, PCI_DMA_TODEVICE);

198
199
200
201
202
	ring_control->rx_mgmt[0].host_addr = cpu_to_le32(rx_mapping);
	ring_control->rx_mgmt[0].len = cpu_to_le16(0x2010);
	ring_control->tx_data[0].host_addr = cpu_to_le32(tx_mapping);
	ring_control->tx_data[0].device_addr = hdr->req_id;
	ring_control->tx_data[0].len = cpu_to_le16(EEPROM_READBACK_LEN);
203

204
205
	ring_control->host_idx[2] = cpu_to_le32(1);
	ring_control->host_idx[1] = cpu_to_le32(1);
206
207
208
209
210
211
212
213
214
215
216
217
218

	wmb();
	mdelay(100);
	P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));

	wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ);
	wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ);

	pci_unmap_single(priv->pdev, tx_mapping,
			 EEPROM_READBACK_LEN, PCI_DMA_TODEVICE);
	pci_unmap_single(priv->pdev, rx_mapping,
			 0x2010, PCI_DMA_FROMDEVICE);

219
220
	alen = le16_to_cpu(ring_control->rx_mgmt[0].len);
	if (le32_to_cpu(ring_control->device_idx[2]) != 1 ||
221
222
223
224
225
226
227
228
229
230
231
	    alen < 0x10) {
		printk(KERN_ERR "%s (prism54pci): Cannot read eeprom!\n",
		       pci_name(priv->pdev));
		err = -EINVAL;
		goto out;
	}

	p54_parse_eeprom(dev, (u8 *)eeprom + 0x10, alen - 0x10);

 out:
	kfree(eeprom);
232
	P54P_WRITE(int_enable, cpu_to_le32(0));
233
234
235
236
237
238
239
240
241
242
	P54P_READ(int_enable);
	udelay(10);
	free_irq(priv->pdev->irq, priv);
	P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));
	return err;
}

static void p54p_refill_rx_ring(struct ieee80211_hw *dev)
{
	struct p54p_priv *priv = dev->priv;
243
	struct p54p_ring_control *ring_control = priv->ring_control;
244
245
	u32 limit, host_idx, idx;

246
	host_idx = le32_to_cpu(ring_control->host_idx[0]);
247
	limit = host_idx;
248
249
	limit -= le32_to_cpu(ring_control->device_idx[0]);
	limit = ARRAY_SIZE(ring_control->rx_data) - limit;
250

251
	idx = host_idx % ARRAY_SIZE(ring_control->rx_data);
252
	while (limit-- > 1) {
253
		struct p54p_desc *desc = &ring_control->rx_data[idx];
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

		if (!desc->host_addr) {
			struct sk_buff *skb;
			dma_addr_t mapping;
			skb = dev_alloc_skb(MAX_RX_SIZE);
			if (!skb)
				break;

			mapping = pci_map_single(priv->pdev,
						 skb_tail_pointer(skb),
						 MAX_RX_SIZE,
						 PCI_DMA_FROMDEVICE);
			desc->host_addr = cpu_to_le32(mapping);
			desc->device_addr = 0;	// FIXME: necessary?
			desc->len = cpu_to_le16(MAX_RX_SIZE);
			desc->flags = 0;
			priv->rx_buf[idx] = skb;
		}

		idx++;
		host_idx++;
275
		idx %= ARRAY_SIZE(ring_control->rx_data);
276
277
278
	}

	wmb();
279
	ring_control->host_idx[0] = cpu_to_le32(host_idx);
280
281
282
283
284
285
}

static irqreturn_t p54p_interrupt(int irq, void *dev_id)
{
	struct ieee80211_hw *dev = dev_id;
	struct p54p_priv *priv = dev->priv;
286
	struct p54p_ring_control *ring_control = priv->ring_control;
287
288
289
290
	__le32 reg;

	spin_lock(&priv->lock);
	reg = P54P_READ(int_ident);
291
	if (unlikely(reg == cpu_to_le32(0xFFFFFFFF))) {
292
293
294
295
296
297
298
299
300
301
302
303
		spin_unlock(&priv->lock);
		return IRQ_HANDLED;
	}

	P54P_WRITE(int_ack, reg);

	reg &= P54P_READ(int_enable);

	if (reg & cpu_to_le32(ISL38XX_INT_IDENT_UPDATE)) {
		struct p54p_desc *desc;
		u32 idx, i;
		i = priv->tx_idx;
304
305
306
		i %= ARRAY_SIZE(ring_control->tx_data);
		priv->tx_idx = idx = le32_to_cpu(ring_control->device_idx[1]);
		idx %= ARRAY_SIZE(ring_control->tx_data);
307
308

		while (i != idx) {
309
			desc = &ring_control->tx_data[i];
310
311
312
313
314
315
316
317
318
319
320
321
322
323
			if (priv->tx_buf[i]) {
				kfree(priv->tx_buf[i]);
				priv->tx_buf[i] = NULL;
			}

			pci_unmap_single(priv->pdev, le32_to_cpu(desc->host_addr),
					 le16_to_cpu(desc->len), PCI_DMA_TODEVICE);

			desc->host_addr = 0;
			desc->device_addr = 0;
			desc->len = 0;
			desc->flags = 0;

			i++;
324
			i %= ARRAY_SIZE(ring_control->tx_data);
325
326
327
		}

		i = priv->rx_idx;
328
329
330
		i %= ARRAY_SIZE(ring_control->rx_data);
		priv->rx_idx = idx = le32_to_cpu(ring_control->device_idx[0]);
		idx %= ARRAY_SIZE(ring_control->rx_data);
331
332
333
		while (i != idx) {
			u16 len;
			struct sk_buff *skb;
334
			desc = &ring_control->rx_data[i];
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
			len = le16_to_cpu(desc->len);
			skb = priv->rx_buf[i];

			skb_put(skb, len);

			if (p54_rx(dev, skb)) {
				pci_unmap_single(priv->pdev,
						 le32_to_cpu(desc->host_addr),
						 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);

				priv->rx_buf[i] = NULL;
				desc->host_addr = 0;
			} else {
				skb_trim(skb, 0);
				desc->len = cpu_to_le16(MAX_RX_SIZE);
			}

			i++;
353
			i %= ARRAY_SIZE(ring_control->rx_data);
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
		}

		p54p_refill_rx_ring(dev);

		wmb();
		P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
	} else if (reg & cpu_to_le32(ISL38XX_INT_IDENT_INIT))
		complete(&priv->boot_comp);

	spin_unlock(&priv->lock);

	return reg ? IRQ_HANDLED : IRQ_NONE;
}

static void p54p_tx(struct ieee80211_hw *dev, struct p54_control_hdr *data,
		    size_t len, int free_on_tx)
{
	struct p54p_priv *priv = dev->priv;
372
	struct p54p_ring_control *ring_control = priv->ring_control;
373
374
375
376
377
378
379
	unsigned long flags;
	struct p54p_desc *desc;
	dma_addr_t mapping;
	u32 device_idx, idx, i;

	spin_lock_irqsave(&priv->lock, flags);

380
381
382
	device_idx = le32_to_cpu(ring_control->device_idx[1]);
	idx = le32_to_cpu(ring_control->host_idx[1]);
	i = idx % ARRAY_SIZE(ring_control->tx_data);
383
384

	mapping = pci_map_single(priv->pdev, data, len, PCI_DMA_TODEVICE);
385
	desc = &ring_control->tx_data[i];
386
387
388
389
390
391
	desc->host_addr = cpu_to_le32(mapping);
	desc->device_addr = data->req_id;
	desc->len = cpu_to_le16(len);
	desc->flags = 0;

	wmb();
392
	ring_control->host_idx[1] = cpu_to_le32(idx + 1);
393
394
395
396
397
398
399
400
401
402
403

	if (free_on_tx)
		priv->tx_buf[i] = data;

	spin_unlock_irqrestore(&priv->lock, flags);

	P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
	P54P_READ(dev_int);

	/* FIXME: unlikely to happen because the device usually runs out of
	   memory before we fill the ring up, but we can make it impossible */
404
	if (idx - device_idx > ARRAY_SIZE(ring_control->tx_data) - 2)
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
		printk(KERN_INFO "%s: tx overflow.\n", wiphy_name(dev->wiphy));
}

static int p54p_open(struct ieee80211_hw *dev)
{
	struct p54p_priv *priv = dev->priv;
	int err;

	init_completion(&priv->boot_comp);
	err = request_irq(priv->pdev->irq, &p54p_interrupt,
			  IRQF_SHARED, "prism54pci", dev);
	if (err) {
		printk(KERN_ERR "%s: failed to register IRQ handler\n",
		       wiphy_name(dev->wiphy));
		return err;
	}

	memset(priv->ring_control, 0, sizeof(*priv->ring_control));
	priv->rx_idx = priv->tx_idx = 0;
	p54p_refill_rx_ring(dev);

	p54p_upload_firmware(dev);

428
	P54P_WRITE(ring_control_base, cpu_to_le32(priv->ring_control_dma));
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
	P54P_READ(ring_control_base);
	wmb();
	udelay(10);

	P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_INIT));
	P54P_READ(int_enable);
	wmb();
	udelay(10);

	P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));
	P54P_READ(dev_int);

	if (!wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ)) {
		printk(KERN_ERR "%s: Cannot boot firmware!\n",
		       wiphy_name(dev->wiphy));
		free_irq(priv->pdev->irq, dev);
		return -ETIMEDOUT;
	}

	P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_UPDATE));
	P54P_READ(int_enable);
	wmb();
	udelay(10);

	P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
	P54P_READ(dev_int);
	wmb();
	udelay(10);

	return 0;
}

static void p54p_stop(struct ieee80211_hw *dev)
{
	struct p54p_priv *priv = dev->priv;
464
	struct p54p_ring_control *ring_control = priv->ring_control;
465
466
467
	unsigned int i;
	struct p54p_desc *desc;

468
	P54P_WRITE(int_enable, cpu_to_le32(0));
469
470
471
472
473
474
475
476
	P54P_READ(int_enable);
	udelay(10);

	free_irq(priv->pdev->irq, dev);

	P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));

	for (i = 0; i < ARRAY_SIZE(priv->rx_buf); i++) {
477
		desc = &ring_control->rx_data[i];
478
479
480
481
482
483
484
485
		if (desc->host_addr)
			pci_unmap_single(priv->pdev, le32_to_cpu(desc->host_addr),
					 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
		kfree_skb(priv->rx_buf[i]);
		priv->rx_buf[i] = NULL;
	}

	for (i = 0; i < ARRAY_SIZE(priv->tx_buf); i++) {
486
		desc = &ring_control->tx_data[i];
487
488
489
490
491
492
493
494
		if (desc->host_addr)
			pci_unmap_single(priv->pdev, le32_to_cpu(desc->host_addr),
					 le16_to_cpu(desc->len), PCI_DMA_TODEVICE);

		kfree(priv->tx_buf[i]);
		priv->tx_buf[i] = NULL;
	}

495
	memset(ring_control, 0, sizeof(ring_control));
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
}

static int __devinit p54p_probe(struct pci_dev *pdev,
				const struct pci_device_id *id)
{
	struct p54p_priv *priv;
	struct ieee80211_hw *dev;
	unsigned long mem_addr, mem_len;
	int err;
	DECLARE_MAC_BUF(mac);

	err = pci_enable_device(pdev);
	if (err) {
		printk(KERN_ERR "%s (prism54pci): Cannot enable new PCI device\n",
		       pci_name(pdev));
		return err;
	}

	mem_addr = pci_resource_start(pdev, 0);
	mem_len = pci_resource_len(pdev, 0);
	if (mem_len < sizeof(struct p54p_csr)) {
		printk(KERN_ERR "%s (prism54pci): Too short PCI resources\n",
		       pci_name(pdev));
		pci_disable_device(pdev);
		return err;
	}

	err = pci_request_regions(pdev, "prism54pci");
	if (err) {
		printk(KERN_ERR "%s (prism54pci): Cannot obtain PCI resources\n",
		       pci_name(pdev));
		return err;
	}

	if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) ||
	    pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)) {
		printk(KERN_ERR "%s (prism54pci): No suitable DMA available\n",
		       pci_name(pdev));
		goto err_free_reg;
	}

	pci_set_master(pdev);
	pci_try_set_mwi(pdev);

	pci_write_config_byte(pdev, 0x40, 0);
	pci_write_config_byte(pdev, 0x41, 0);

	dev = p54_init_common(sizeof(*priv));
	if (!dev) {
		printk(KERN_ERR "%s (prism54pci): ieee80211 alloc failed\n",
		       pci_name(pdev));
		err = -ENOMEM;
		goto err_free_reg;
	}

	priv = dev->priv;
	priv->pdev = pdev;

	SET_IEEE80211_DEV(dev, &pdev->dev);
	pci_set_drvdata(pdev, dev);

	priv->map = ioremap(mem_addr, mem_len);
	if (!priv->map) {
		printk(KERN_ERR "%s (prism54pci): Cannot map device memory\n",
		       pci_name(pdev));
		err = -EINVAL;	// TODO: use a better error code?
		goto err_free_dev;
	}

	priv->ring_control = pci_alloc_consistent(pdev, sizeof(*priv->ring_control),
						  &priv->ring_control_dma);
	if (!priv->ring_control) {
		printk(KERN_ERR "%s (prism54pci): Cannot allocate rings\n",
		       pci_name(pdev));
		err = -ENOMEM;
		goto err_iounmap;
	}
	memset(priv->ring_control, 0, sizeof(*priv->ring_control));

	err = p54p_upload_firmware(dev);
	if (err)
		goto err_free_desc;

	err = p54p_read_eeprom(dev);
	if (err)
		goto err_free_desc;

	priv->common.open = p54p_open;
	priv->common.stop = p54p_stop;
	priv->common.tx = p54p_tx;

	spin_lock_init(&priv->lock);

	err = ieee80211_register_hw(dev);
	if (err) {
		printk(KERN_ERR "%s (prism54pci): Cannot register netdevice\n",
		       pci_name(pdev));
		goto err_free_common;
	}

	printk(KERN_INFO "%s: hwaddr %s, isl38%02x\n",
	       wiphy_name(dev->wiphy),
	       print_mac(mac, dev->wiphy->perm_addr),
	       priv->common.version);

	return 0;

 err_free_common:
	p54_free_common(dev);

 err_free_desc:
	pci_free_consistent(pdev, sizeof(*priv->ring_control),
			    priv->ring_control, priv->ring_control_dma);

 err_iounmap:
	iounmap(priv->map);

 err_free_dev:
	pci_set_drvdata(pdev, NULL);
	ieee80211_free_hw(dev);

 err_free_reg:
	pci_release_regions(pdev);
	pci_disable_device(pdev);
	return err;
}

static void __devexit p54p_remove(struct pci_dev *pdev)
{
	struct ieee80211_hw *dev = pci_get_drvdata(pdev);
	struct p54p_priv *priv;

	if (!dev)
		return;

	ieee80211_unregister_hw(dev);
	priv = dev->priv;
	pci_free_consistent(pdev, sizeof(*priv->ring_control),
			    priv->ring_control, priv->ring_control_dma);
	p54_free_common(dev);
	iounmap(priv->map);
	pci_release_regions(pdev);
	pci_disable_device(pdev);
	ieee80211_free_hw(dev);
}

#ifdef CONFIG_PM
static int p54p_suspend(struct pci_dev *pdev, pm_message_t state)
{
	struct ieee80211_hw *dev = pci_get_drvdata(pdev);
	struct p54p_priv *priv = dev->priv;

648
	if (priv->common.mode != IEEE80211_IF_TYPE_INVALID) {
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
		ieee80211_stop_queues(dev);
		p54p_stop(dev);
	}

	pci_save_state(pdev);
	pci_set_power_state(pdev, pci_choose_state(pdev, state));
	return 0;
}

static int p54p_resume(struct pci_dev *pdev)
{
	struct ieee80211_hw *dev = pci_get_drvdata(pdev);
	struct p54p_priv *priv = dev->priv;

	pci_set_power_state(pdev, PCI_D0);
	pci_restore_state(pdev);

666
	if (priv->common.mode != IEEE80211_IF_TYPE_INVALID) {
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
		p54p_open(dev);
		ieee80211_start_queues(dev);
	}

	return 0;
}
#endif /* CONFIG_PM */

static struct pci_driver p54p_driver = {
	.name		= "prism54pci",
	.id_table	= p54p_table,
	.probe		= p54p_probe,
	.remove		= __devexit_p(p54p_remove),
#ifdef CONFIG_PM
	.suspend	= p54p_suspend,
	.resume		= p54p_resume,
#endif /* CONFIG_PM */
};

static int __init p54p_init(void)
{
	return pci_register_driver(&p54p_driver);
}

static void __exit p54p_exit(void)
{
	pci_unregister_driver(&p54p_driver);
}

module_init(p54p_init);
module_exit(p54p_exit);