summaryrefslogtreecommitdiff
path: root/kernel-devel/module-basics/vain_plat/vain_plat_orig.c
blob: 530407e51d2e9f534f7fe226b313ad2b31651f43 (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
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>

#include <asm/io.h>

struct vain_plat_info {
	void __iomem *base;
	struct platform_device *pdev;
	spinlock_t lock;
};

static int __devinit vain_plat_probe(struct platform_device *pdev)
{
	struct vain_plat_info *info;
	struct resource *res, *mem;
	int err;

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

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (unlikely(!res)) {
		dev_err(&pdev->dev, "I/O address already in use\n");
		err = -ENOENT;
		goto err_free;
	}

	mem = request_mem_region(res->start, resource_size(res), pdev->name);
	if (!mem) {
		err = -EBUSY;
		goto err_free;
	}

	info->base = ioremap(res->start, resource_size(res));
	if (!info->base) {
		dev_err(&pdev->dev, "Unable to map card MMIO\n");
		err = -ENODEV;
		goto err_ioremap;
	}

	platform_set_drvdata(pdev, info);

	return 0;

err_ioremap:
	release_mem_region(res->start, resource_size(res));
err_free:
	kfree(info);
out:
	return err;
}

static int __devexit vain_plat_remove(struct platform_device *pdev)
{
	struct vain_plat_info *info = platform_get_drvdata(pdev);
	struct resource *res;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	iounmap(info->base);
	release_mem_region(res->start, resource_size(res));
	platform_set_drvdata(pdev, NULL);

	kfree (info);

	return 0;
}

static struct platform_driver vain_plat_driver = {
	.driver		= {
		.name	= "vain_plat",
		.owner	= THIS_MODULE,
	},
	.probe		= vain_plat_probe,
	.remove		= __devexit_p(vain_plat_remove),
};

static int __init vain_plat_init(void)
{
	int err;

	err = platform_driver_register(&vain_plat_driver);

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

static void __exit vain_plat_exit(void)
{
	platform_driver_unregister(&vain_plat_driver);
	printk(KERN_INFO "vain_plat_exit: done\n");
}

module_init(vain_plat_init);
module_exit(vain_plat_exit);

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