summaryrefslogtreecommitdiff
path: root/kernel-devel/module-basics/vain_pci/vain_pci_orig.c
blob: f632f262ec0a4c7436096d81d995e9dc4ee40084 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
85
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
#include <linux/init.h>
#include <linux/module.h>
#include <linux/pci.h>

#define PCI_VENDOR_ID_ILLEGAL_VENDOR 0xffff
#define PCI_DEVICE_ID_ILLEGAL_VENDOR_DEVICE 0x2342

struct vain_pci_info {
	void __iomem *base;
	struct pci_dev *pdev;
	spinlock_t lock;
};

static struct pci_device_id vain_pci_ids[] __devinitdata = {
	{PCI_VENDOR_ID_ILLEGAL_VENDOR, PCI_DEVICE_ID_ILLEGAL_VENDOR_DEVICE,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
	{0, }
};
MODULE_DEVICE_TABLE(pci, vain_pci_ids);

static int __devinit vain_pci_probe(struct pci_dev *pdev,
				    const struct pci_device_id *ent)
{
	struct vain_pci_info *info;
	int ret;

	dev_dbg(&pdev->dev, "Init, new card found\n");

	info = kzalloc(sizeof(struct vain_pci_info), GFP_KERNEL);
	if (unlikely(!info)) {
		dev_err(&pdev->dev, "Could not allocate memory\n");
		ret = -ENOMEM;
		goto err_free;
	}

	info->pdev = pdev;

	ret = pci_enable_device(pdev);
	if (ret) {
		dev_err(&pdev->dev, "Failed to enable PCI Device\n");
		goto err_free;
	}

	ret = pci_request_regions(pdev, "vain_pci");
	if (ret) {
		dev_err(&pdev->dev, "I/O address 0x%04x already in use\n");
		goto err_disable_device;
	}

	info->base = pci_ioremap_bar(pdev, 0);
	if (!info->base) {
		dev_err(&pdev->dev, "Unable to map card MMIO\n");
		ret = -ENODEV;
		goto err_rel_regs;
	}

	pci_set_drvdata(pdev, info);

	return 0;

err_rel_regs:
	pci_release_regions(pdev);
err_disable_device:
	pci_disable_device(pdev);
err_free:
	kfree(info);

	return ret;
}

static void __devexit vain_pci_remove(struct pci_dev *pdev)
{
	struct vain_pci_info *info = pci_get_drvdata(pdev);

	iounmap(info->base);
	pci_release_regions(pdev);
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);

	kfree (info);
}

static struct pci_driver vain_pci_driver = {
	.name =		"vain_pci",
	.id_table =	vain_pci_ids,
	.probe =	vain_pci_probe,
	.remove =	__devexit_p(vain_pci_remove),
};

static int vain_pci_init(void)
{
	int err;

	err = pci_register_driver(&vain_pci_driver);

	if (!err)
		printk(KERN_INFO "vain_pci_init: done\n");
	return err;
}

static void vain_pci_exit(void)
{
	pci_unregister_driver(&vain_pci_driver);
	printk(KERN_INFO "vain_pci_exit: done\n");
}

module_init(vain_pci_init);
module_exit(vain_pci_exit);

MODULE_AUTHOR("Benedikt Spranger <b.spranger@linutronix.de>");
MODULE_DESCRIPTION("a more or less useless PCI module");
MODULE_LICENSE("GPL v2");
MODULE_VERSION("0815");