fbdev: imsttfb: Fix use after free bug in imsttfb_probe
[linux-block.git] / drivers / char / scx200_gpio.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
62c83cde 2/* linux/drivers/char/scx200_gpio.c
1da177e4
LT
3
4 National Semiconductor SCx200 GPIO driver. Allows a user space
5 process to play with the GPIO pins.
6
7 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> */
8
979b5ec3 9#include <linux/device.h>
1da177e4
LT
10#include <linux/fs.h>
11#include <linux/module.h>
12#include <linux/errno.h>
13#include <linux/kernel.h>
14#include <linux/init.h>
979b5ec3 15#include <linux/platform_device.h>
7c0f6ba6 16#include <linux/uaccess.h>
1da177e4
LT
17#include <asm/io.h>
18
7d7f2126
JC
19#include <linux/types.h>
20#include <linux/cdev.h>
21
1da177e4 22#include <linux/scx200_gpio.h>
fe3a168a 23#include <linux/nsc_gpio.h>
1da177e4 24
ae2d1f2f 25#define DRVNAME "scx200_gpio"
979b5ec3
JC
26
27static struct platform_device *pdev;
1da177e4
LT
28
29MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
ae2d1f2f 30MODULE_DESCRIPTION("NatSemi/AMD SCx200 GPIO Pin Driver");
1da177e4
LT
31MODULE_LICENSE("GPL");
32
33static int major = 0; /* default to dynamic major */
34module_param(major, int, 0);
35MODULE_PARM_DESC(major, "Major device number");
36
ae2d1f2f
JC
37#define MAX_PINS 32 /* 64 later, when known ok */
38
2e8f7a31 39struct nsc_gpio_ops scx200_gpio_ops = {
fe3a168a
JC
40 .owner = THIS_MODULE,
41 .gpio_config = scx200_gpio_configure,
0e41ef3c 42 .gpio_dump = nsc_gpio_dump,
fe3a168a
JC
43 .gpio_get = scx200_gpio_get,
44 .gpio_set = scx200_gpio_set,
fe3a168a
JC
45 .gpio_change = scx200_gpio_change,
46 .gpio_current = scx200_gpio_current
47};
58012cd7 48EXPORT_SYMBOL_GPL(scx200_gpio_ops);
fe3a168a 49
1da177e4
LT
50static int scx200_gpio_open(struct inode *inode, struct file *file)
51{
52 unsigned m = iminor(inode);
2e8f7a31 53 file->private_data = &scx200_gpio_ops;
c3dc8071 54
ae2d1f2f 55 if (m >= MAX_PINS)
1da177e4
LT
56 return -EINVAL;
57 return nonseekable_open(inode, file);
58}
59
60static int scx200_gpio_release(struct inode *inode, struct file *file)
61{
62 return 0;
63}
64
2e8f7a31 65static const struct file_operations scx200_gpio_fileops = {
1da177e4 66 .owner = THIS_MODULE,
1a66fdf0
JC
67 .write = nsc_gpio_write,
68 .read = nsc_gpio_read,
1da177e4
LT
69 .open = scx200_gpio_open,
70 .release = scx200_gpio_release,
6038f373 71 .llseek = no_llseek,
1da177e4
LT
72};
73
c8ad9681 74static struct cdev scx200_gpio_cdev; /* use 1 cdev for all pins */
7d7f2126 75
1da177e4
LT
76static int __init scx200_gpio_init(void)
77{
635adb6c 78 int rc;
ae2d1f2f 79 dev_t devid;
1da177e4 80
1da177e4 81 if (!scx200_gpio_present()) {
ae2d1f2f 82 printk(KERN_ERR DRVNAME ": no SCx200 gpio present\n");
1da177e4
LT
83 return -ENODEV;
84 }
979b5ec3
JC
85
86 /* support dev_dbg() with pdev->dev */
ae2d1f2f 87 pdev = platform_device_alloc(DRVNAME, 0);
979b5ec3
JC
88 if (!pdev)
89 return -ENOMEM;
90
91 rc = platform_device_add(pdev);
92 if (rc)
93 goto undo_malloc;
94
f31000e5 95 /* nsc_gpio uses dev_dbg(), so needs this */
2e8f7a31 96 scx200_gpio_ops.dev = &pdev->dev;
f31000e5 97
ae2d1f2f
JC
98 if (major) {
99 devid = MKDEV(major, 0);
100 rc = register_chrdev_region(devid, MAX_PINS, "scx200_gpio");
101 } else {
102 rc = alloc_chrdev_region(&devid, 0, MAX_PINS, "scx200_gpio");
103 major = MAJOR(devid);
1da177e4 104 }
7d7f2126 105 if (rc < 0) {
979b5ec3
JC
106 dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
107 goto undo_platform_device_add;
7d7f2126 108 }
635adb6c 109
2e8f7a31 110 cdev_init(&scx200_gpio_cdev, &scx200_gpio_fileops);
635adb6c 111 cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
1da177e4 112
979b5ec3 113 return 0; /* succeed */
7d7f2126 114
979b5ec3 115undo_platform_device_add:
1017f6af 116 platform_device_del(pdev);
979b5ec3 117undo_malloc:
1017f6af
IM
118 platform_device_put(pdev);
119
7d7f2126 120 return rc;
1da177e4
LT
121}
122
123static void __exit scx200_gpio_cleanup(void)
124{
635adb6c
JC
125 cdev_del(&scx200_gpio_cdev);
126 /* cdev_put(&scx200_gpio_cdev); */
127
ae2d1f2f 128 unregister_chrdev_region(MKDEV(major, 0), MAX_PINS);
979b5ec3 129 platform_device_unregister(pdev);
1da177e4
LT
130}
131
132module_init(scx200_gpio_init);
133module_exit(scx200_gpio_cleanup);