[PATCH] scx200_gpio: 1 cdev for N minors: cleanup, prep
[deliverable/linux.git] / drivers / char / scx200_gpio.c
CommitLineData
62c83cde 1/* linux/drivers/char/scx200_gpio.c
1da177e4
LT
2
3 National Semiconductor SCx200 GPIO driver. Allows a user space
4 process to play with the GPIO pins.
5
6 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> */
7
979b5ec3 8#include <linux/device.h>
1da177e4
LT
9#include <linux/fs.h>
10#include <linux/module.h>
11#include <linux/errno.h>
12#include <linux/kernel.h>
13#include <linux/init.h>
979b5ec3 14#include <linux/platform_device.h>
1da177e4
LT
15#include <asm/uaccess.h>
16#include <asm/io.h>
17
7d7f2126
JC
18#include <linux/types.h>
19#include <linux/cdev.h>
20
1da177e4 21#include <linux/scx200_gpio.h>
fe3a168a 22#include <linux/nsc_gpio.h>
1da177e4 23
ae2d1f2f 24#define DRVNAME "scx200_gpio"
979b5ec3
JC
25
26static struct platform_device *pdev;
1da177e4
LT
27
28MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
ae2d1f2f 29MODULE_DESCRIPTION("NatSemi/AMD SCx200 GPIO Pin Driver");
1da177e4
LT
30MODULE_LICENSE("GPL");
31
32static int major = 0; /* default to dynamic major */
33module_param(major, int, 0);
34MODULE_PARM_DESC(major, "Major device number");
35
ae2d1f2f
JC
36#define MAX_PINS 32 /* 64 later, when known ok */
37
fe3a168a
JC
38struct nsc_gpio_ops scx200_access = {
39 .owner = THIS_MODULE,
40 .gpio_config = scx200_gpio_configure,
0e41ef3c 41 .gpio_dump = nsc_gpio_dump,
fe3a168a
JC
42 .gpio_get = scx200_gpio_get,
43 .gpio_set = scx200_gpio_set,
44 .gpio_set_high = scx200_gpio_set_high,
45 .gpio_set_low = scx200_gpio_set_low,
46 .gpio_change = scx200_gpio_change,
47 .gpio_current = scx200_gpio_current
48};
ae2d1f2f 49EXPORT_SYMBOL(scx200_access);
fe3a168a 50
1da177e4
LT
51static int scx200_gpio_open(struct inode *inode, struct file *file)
52{
53 unsigned m = iminor(inode);
c3dc8071
JC
54 file->private_data = &scx200_access;
55
ae2d1f2f 56 if (m >= MAX_PINS)
1da177e4
LT
57 return -EINVAL;
58 return nonseekable_open(inode, file);
59}
60
61static int scx200_gpio_release(struct inode *inode, struct file *file)
62{
63 return 0;
64}
65
66
62322d25 67static const struct file_operations scx200_gpio_fops = {
1da177e4 68 .owner = THIS_MODULE,
1a66fdf0
JC
69 .write = nsc_gpio_write,
70 .read = nsc_gpio_read,
1da177e4
LT
71 .open = scx200_gpio_open,
72 .release = scx200_gpio_release,
73};
74
7d7f2126 75struct cdev *scx200_devices;
7d7f2126 76
1da177e4
LT
77static int __init scx200_gpio_init(void)
78{
7d7f2126 79 int rc, i;
ae2d1f2f 80 dev_t devid;
1da177e4 81
1da177e4 82 if (!scx200_gpio_present()) {
ae2d1f2f 83 printk(KERN_ERR DRVNAME ": no SCx200 gpio present\n");
1da177e4
LT
84 return -ENODEV;
85 }
979b5ec3
JC
86
87 /* support dev_dbg() with pdev->dev */
ae2d1f2f 88 pdev = platform_device_alloc(DRVNAME, 0);
979b5ec3
JC
89 if (!pdev)
90 return -ENOMEM;
91
92 rc = platform_device_add(pdev);
93 if (rc)
94 goto undo_malloc;
95
f31000e5
JC
96 /* nsc_gpio uses dev_dbg(), so needs this */
97 scx200_access.dev = &pdev->dev;
98
ae2d1f2f
JC
99 if (major) {
100 devid = MKDEV(major, 0);
101 rc = register_chrdev_region(devid, MAX_PINS, "scx200_gpio");
102 } else {
103 rc = alloc_chrdev_region(&devid, 0, MAX_PINS, "scx200_gpio");
104 major = MAJOR(devid);
1da177e4 105 }
7d7f2126 106 if (rc < 0) {
979b5ec3
JC
107 dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
108 goto undo_platform_device_add;
7d7f2126 109 }
ae2d1f2f 110 scx200_devices = kzalloc(MAX_PINS * sizeof(struct cdev), GFP_KERNEL);
7d7f2126
JC
111 if (!scx200_devices) {
112 rc = -ENOMEM;
979b5ec3 113 goto undo_chrdev_region;
7d7f2126 114 }
ae2d1f2f 115 for (i = 0; i < MAX_PINS; i++) {
7d7f2126
JC
116 struct cdev *cdev = &scx200_devices[i];
117 cdev_init(cdev, &scx200_gpio_fops);
118 cdev->owner = THIS_MODULE;
7d7f2126 119 rc = cdev_add(cdev, MKDEV(major, i), 1);
979b5ec3 120 /* tolerate 'minor' errors */
7d7f2126 121 if (rc)
979b5ec3 122 dev_err(&pdev->dev, "Error %d on minor %d", rc, i);
1da177e4
LT
123 }
124
979b5ec3 125 return 0; /* succeed */
7d7f2126 126
979b5ec3 127undo_chrdev_region:
ae2d1f2f 128 unregister_chrdev_region(devid, MAX_PINS);
979b5ec3 129undo_platform_device_add:
1017f6af 130 platform_device_del(pdev);
979b5ec3 131undo_malloc:
1017f6af
IM
132 platform_device_put(pdev);
133
7d7f2126 134 return rc;
1da177e4
LT
135}
136
137static void __exit scx200_gpio_cleanup(void)
138{
7d7f2126 139 kfree(scx200_devices);
ae2d1f2f 140 unregister_chrdev_region(MKDEV(major, 0), MAX_PINS);
979b5ec3 141 platform_device_unregister(pdev);
1da177e4
LT
142}
143
144module_init(scx200_gpio_init);
145module_exit(scx200_gpio_cleanup);
This page took 0.159828 seconds and 5 git commands to generate.