1/*
2 * arch/sh/boards/landisk/gio.c - driver for landisk
3 *
4 * This driver will also support the I-O DATA Device, Inc. LANDISK Board.
5 * LANDISK and USL-5P Button, LED and GIO driver drive function.
6 *
7 *   Copylight (C) 2006 kogiidena
8 *   Copylight (C) 2002 Atom Create Engineering Co., Ltd. *
9 *
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License.  See the file "COPYING" in the main directory of this archive
12 * for more details.
13 *
14 */
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/kdev_t.h>
18#include <linux/cdev.h>
19#include <linux/fs.h>
20#include <asm/io.h>
21#include <asm/uaccess.h>
22#include <asm/landisk/gio.h>
23#include <asm/landisk/iodata_landisk.h>
24
25#define DEVCOUNT                4
26#define GIO_MINOR	        2	/* GIO minor no. */
27
28static dev_t dev;
29static struct cdev *cdev_p;
30static int openCnt;
31
32static int gio_open(struct inode *inode, struct file *filp)
33{
34	int minor;
35
36	minor = MINOR(inode->i_rdev);
37	if (minor < DEVCOUNT) {
38		if (openCnt > 0) {
39			return -EALREADY;
40		} else {
41			openCnt++;
42			return 0;
43		}
44	}
45	return -ENOENT;
46}
47
48static int gio_close(struct inode *inode, struct file *filp)
49{
50	int minor;
51
52	minor = MINOR(inode->i_rdev);
53	if (minor < DEVCOUNT) {
54		openCnt--;
55	}
56	return 0;
57}
58
59static int gio_ioctl(struct inode *inode, struct file *filp,
60			     unsigned int cmd, unsigned long arg)
61{
62	unsigned int data;
63	static unsigned int addr = 0;
64
65	if (cmd & 0x01) {	/* write */
66		if (copy_from_user(&data, (int *)arg, sizeof(int))) {
67			return -EFAULT;
68		}
69	}
70
71	switch (cmd) {
72	case GIODRV_IOCSGIOSETADDR:	/* address set */
73		addr = data;
74		break;
75
76	case GIODRV_IOCSGIODATA1:	/* write byte */
77		ctrl_outb((unsigned char)(0x0ff & data), addr);
78		break;
79
80	case GIODRV_IOCSGIODATA2:	/* write word */
81		if (addr & 0x01) {
82			return -EFAULT;
83		}
84		ctrl_outw((unsigned short int)(0x0ffff & data), addr);
85		break;
86
87	case GIODRV_IOCSGIODATA4:	/* write long */
88		if (addr & 0x03) {
89			return -EFAULT;
90		}
91		ctrl_outl(data, addr);
92		break;
93
94	case GIODRV_IOCGGIODATA1:	/* read byte */
95		data = ctrl_inb(addr);
96		break;
97
98	case GIODRV_IOCGGIODATA2:	/* read word */
99		if (addr & 0x01) {
100			return -EFAULT;
101		}
102		data = ctrl_inw(addr);
103		break;
104
105	case GIODRV_IOCGGIODATA4:	/* read long */
106		if (addr & 0x03) {
107			return -EFAULT;
108		}
109		data = ctrl_inl(addr);
110		break;
111	default:
112		return -EFAULT;
113		break;
114	}
115
116	if ((cmd & 0x01) == 0) {	/* read */
117		if (copy_to_user((int *)arg, &data, sizeof(int))) {
118			return -EFAULT;
119		}
120	}
121	return 0;
122}
123
124static struct file_operations gio_fops = {
125	.owner = THIS_MODULE,
126	.open = gio_open,	/* open */
127	.release = gio_close,	/* release */
128	.ioctl = gio_ioctl,	/* ioctl */
129};
130
131static int __init gio_init(void)
132{
133	int error;
134
135	printk(KERN_INFO "gio: driver initialized\n");
136
137	openCnt = 0;
138
139	if ((error = alloc_chrdev_region(&dev, 0, DEVCOUNT, "gio")) < 0) {
140		printk(KERN_ERR
141		       "gio: Couldn't alloc_chrdev_region, error=%d\n",
142		       error);
143		return 1;
144	}
145
146	cdev_p = cdev_alloc();
147	cdev_p->ops = &gio_fops;
148	error = cdev_add(cdev_p, dev, DEVCOUNT);
149	if (error) {
150		printk(KERN_ERR
151		       "gio: Couldn't cdev_add, error=%d\n", error);
152		return 1;
153	}
154
155	return 0;
156}
157
158static void __exit gio_exit(void)
159{
160	cdev_del(cdev_p);
161	unregister_chrdev_region(dev, DEVCOUNT);
162}
163
164module_init(gio_init);
165module_exit(gio_exit);
166
167MODULE_LICENSE("GPL");
168