| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 |  *  Copyright (c) 1999-2001 Vojtech Pavlik | 
 | 3 |  */ | 
 | 4 |  | 
 | 5 | /* | 
 | 6 |  *  82C710 C&T mouse port chip driver for Linux | 
 | 7 |  */ | 
 | 8 |  | 
 | 9 | /* | 
 | 10 |  * This program is free software; you can redistribute it and/or modify | 
 | 11 |  * it under the terms of the GNU General Public License as published by | 
 | 12 |  * the Free Software Foundation; either version 2 of the License, or | 
 | 13 |  * (at your option) any later version. | 
 | 14 |  * | 
 | 15 |  * This program is distributed in the hope that it will be useful, | 
 | 16 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 17 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 18 |  * GNU General Public License for more details. | 
 | 19 |  * | 
 | 20 |  * You should have received a copy of the GNU General Public License | 
 | 21 |  * along with this program; if not, write to the Free Software | 
 | 22 |  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 
 | 23 |  * | 
 | 24 |  * Should you need to contact me, the author, you can do so either by | 
 | 25 |  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: | 
 | 26 |  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic | 
 | 27 |  */ | 
 | 28 |  | 
 | 29 | #include <linux/delay.h> | 
 | 30 | #include <linux/module.h> | 
 | 31 | #include <linux/ioport.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/init.h> | 
 | 33 | #include <linux/interrupt.h> | 
 | 34 | #include <linux/serio.h> | 
 | 35 | #include <linux/errno.h> | 
 | 36 | #include <linux/err.h> | 
| Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 37 | #include <linux/platform_device.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 38 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 |  | 
 | 40 | #include <asm/io.h> | 
 | 41 |  | 
 | 42 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); | 
 | 43 | MODULE_DESCRIPTION("82C710 C&T mouse port chip driver"); | 
 | 44 | MODULE_LICENSE("GPL"); | 
 | 45 |  | 
 | 46 | /* | 
 | 47 |  * ct82c710 interface | 
 | 48 |  */ | 
 | 49 |  | 
 | 50 | #define CT82C710_DEV_IDLE     0x01		/* Device Idle */ | 
 | 51 | #define CT82C710_RX_FULL      0x02		/* Device Char received */ | 
 | 52 | #define CT82C710_TX_IDLE      0x04		/* Device XMIT Idle */ | 
 | 53 | #define CT82C710_RESET        0x08		/* Device Reset */ | 
 | 54 | #define CT82C710_INTS_ON      0x10		/* Device Interrupt On */ | 
 | 55 | #define CT82C710_ERROR_FLAG   0x20		/* Device Error */ | 
 | 56 | #define CT82C710_CLEAR        0x40		/* Device Clear */ | 
 | 57 | #define CT82C710_ENABLE       0x80		/* Device Enable */ | 
 | 58 |  | 
 | 59 | #define CT82C710_IRQ          12 | 
 | 60 |  | 
 | 61 | #define CT82C710_DATA         ct82c710_iores.start | 
 | 62 | #define CT82C710_STATUS       (ct82c710_iores.start + 1) | 
 | 63 |  | 
 | 64 | static struct serio *ct82c710_port; | 
 | 65 | static struct platform_device *ct82c710_device; | 
 | 66 | static struct resource ct82c710_iores; | 
 | 67 |  | 
 | 68 | /* | 
 | 69 |  * Interrupt handler for the 82C710 mouse port. A character | 
 | 70 |  * is waiting in the 82C710. | 
 | 71 |  */ | 
 | 72 |  | 
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 73 | static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { | 
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 75 | 	return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } | 
 | 77 |  | 
 | 78 | /* | 
 | 79 |  * Wait for device to send output char and flush any input char. | 
 | 80 |  */ | 
 | 81 |  | 
 | 82 | static int ct82c170_wait(void) | 
 | 83 | { | 
 | 84 | 	int timeout = 60000; | 
 | 85 |  | 
 | 86 | 	while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE)) | 
 | 87 | 		       != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) { | 
 | 88 |  | 
 | 89 | 		if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA); | 
 | 90 |  | 
 | 91 | 		udelay(1); | 
 | 92 | 		timeout--; | 
 | 93 | 	} | 
 | 94 |  | 
 | 95 | 	return !timeout; | 
 | 96 | } | 
 | 97 |  | 
 | 98 | static void ct82c710_close(struct serio *serio) | 
 | 99 | { | 
 | 100 | 	if (ct82c170_wait()) | 
 | 101 | 		printk(KERN_WARNING "ct82c710.c: Device busy in close()\n"); | 
 | 102 |  | 
 | 103 | 	outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS); | 
 | 104 |  | 
 | 105 | 	if (ct82c170_wait()) | 
 | 106 | 		printk(KERN_WARNING "ct82c710.c: Device busy in close()\n"); | 
 | 107 |  | 
 | 108 | 	free_irq(CT82C710_IRQ, NULL); | 
 | 109 | } | 
 | 110 |  | 
 | 111 | static int ct82c710_open(struct serio *serio) | 
 | 112 | { | 
 | 113 | 	unsigned char status; | 
 | 114 |  | 
 | 115 | 	if (request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL)) | 
 | 116 | 		return -1; | 
 | 117 |  | 
 | 118 | 	status = inb_p(CT82C710_STATUS); | 
 | 119 |  | 
 | 120 | 	status |= (CT82C710_ENABLE | CT82C710_RESET); | 
 | 121 | 	outb_p(status, CT82C710_STATUS); | 
 | 122 |  | 
 | 123 | 	status &= ~(CT82C710_RESET); | 
 | 124 | 	outb_p(status, CT82C710_STATUS); | 
 | 125 |  | 
 | 126 | 	status |= CT82C710_INTS_ON; | 
 | 127 | 	outb_p(status, CT82C710_STATUS);	/* Enable interrupts */ | 
 | 128 |  | 
 | 129 | 	while (ct82c170_wait()) { | 
 | 130 | 		printk(KERN_ERR "ct82c710: Device busy in open()\n"); | 
 | 131 | 		status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON); | 
 | 132 | 		outb_p(status, CT82C710_STATUS); | 
 | 133 | 		free_irq(CT82C710_IRQ, NULL); | 
 | 134 | 		return -1; | 
 | 135 | 	} | 
 | 136 |  | 
 | 137 | 	return 0; | 
 | 138 | } | 
 | 139 |  | 
 | 140 | /* | 
 | 141 |  * Write to the 82C710 mouse device. | 
 | 142 |  */ | 
 | 143 |  | 
 | 144 | static int ct82c710_write(struct serio *port, unsigned char c) | 
 | 145 | { | 
 | 146 | 	if (ct82c170_wait()) return -1; | 
 | 147 | 	outb_p(c, CT82C710_DATA); | 
 | 148 | 	return 0; | 
 | 149 | } | 
 | 150 |  | 
 | 151 | /* | 
 | 152 |  * See if we can find a 82C710 device. Read mouse address. | 
 | 153 |  */ | 
 | 154 |  | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 155 | static int __init ct82c710_detect(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { | 
 | 157 | 	outb_p(0x55, 0x2fa);				/* Any value except 9, ff or 36 */ | 
 | 158 | 	outb_p(0xaa, 0x3fa);				/* Inverse of 55 */ | 
 | 159 | 	outb_p(0x36, 0x3fa);				/* Address the chip */ | 
 | 160 | 	outb_p(0xe4, 0x3fa);				/* 390/4; 390 = config address */ | 
 | 161 | 	outb_p(0x1b, 0x2fa);				/* Inverse of e4 */ | 
 | 162 | 	outb_p(0x0f, 0x390);				/* Write index */ | 
 | 163 | 	if (inb_p(0x391) != 0xe4)			/* Config address found? */ | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 164 | 		return -ENODEV;				/* No: no 82C710 here */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 |  | 
 | 166 | 	outb_p(0x0d, 0x390);				/* Write index */ | 
 | 167 | 	ct82c710_iores.start = inb_p(0x391) << 2;	/* Get mouse I/O address */ | 
 | 168 | 	ct82c710_iores.end = ct82c710_iores.start + 1; | 
 | 169 | 	ct82c710_iores.flags = IORESOURCE_IO; | 
 | 170 | 	outb_p(0x0f, 0x390); | 
 | 171 | 	outb_p(0x0f, 0x391);				/* Close config mode */ | 
 | 172 |  | 
 | 173 | 	return 0; | 
 | 174 | } | 
 | 175 |  | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 176 | static int __devinit ct82c710_probe(struct platform_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 178 | 	ct82c710_port = kzalloc(sizeof(struct serio), GFP_KERNEL); | 
 | 179 | 	if (!ct82c710_port) | 
 | 180 | 		return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 |  | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 182 | 	ct82c710_port->id.type = SERIO_8042; | 
 | 183 | 	ct82c710_port->dev.parent = &dev->dev; | 
 | 184 | 	ct82c710_port->open = ct82c710_open; | 
 | 185 | 	ct82c710_port->close = ct82c710_close; | 
 | 186 | 	ct82c710_port->write = ct82c710_write; | 
 | 187 | 	strlcpy(ct82c710_port->name, "C&T 82c710 mouse port", | 
 | 188 | 		sizeof(ct82c710_port->name)); | 
 | 189 | 	snprintf(ct82c710_port->phys, sizeof(ct82c710_port->phys), | 
| Greg Kroah-Hartman | e29419f | 2006-06-12 15:20:16 -0700 | [diff] [blame] | 190 | 		 "isa%16llx/serio0", (unsigned long long)CT82C710_DATA); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 |  | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 192 | 	serio_register_port(ct82c710_port); | 
 | 193 |  | 
 | 194 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } | 
 | 196 |  | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 197 | static int __devexit ct82c710_remove(struct platform_device *dev) | 
 | 198 | { | 
 | 199 | 	serio_unregister_port(ct82c710_port); | 
 | 200 |  | 
 | 201 | 	return 0; | 
 | 202 | } | 
 | 203 |  | 
 | 204 | static struct platform_driver ct82c710_driver = { | 
 | 205 | 	.driver		= { | 
 | 206 | 		.name	= "ct82c710", | 
 | 207 | 		.owner	= THIS_MODULE, | 
 | 208 | 	}, | 
 | 209 | 	.probe		= ct82c710_probe, | 
 | 210 | 	.remove		= __devexit_p(ct82c710_remove), | 
 | 211 | }; | 
 | 212 |  | 
 | 213 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | static int __init ct82c710_init(void) | 
 | 215 | { | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 216 | 	int error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 |  | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 218 | 	error = ct82c710_detect(); | 
 | 219 | 	if (error) | 
 | 220 | 		return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 |  | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 222 | 	error = platform_driver_register(&ct82c710_driver); | 
 | 223 | 	if (error) | 
 | 224 | 		return error; | 
 | 225 |  | 
 | 226 | 	ct82c710_device = platform_device_alloc("ct82c710", -1); | 
 | 227 | 	if (!ct82c710_device) { | 
 | 228 | 		error = -ENOMEM; | 
 | 229 | 		goto err_unregister_driver; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | 	} | 
 | 231 |  | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 232 | 	error = platform_device_add_resources(ct82c710_device, &ct82c710_iores, 1); | 
 | 233 | 	if (error) | 
 | 234 | 		goto err_free_device; | 
 | 235 |  | 
 | 236 | 	error = platform_device_add(ct82c710_device); | 
 | 237 | 	if (error) | 
 | 238 | 		goto err_free_device; | 
 | 239 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | 	serio_register_port(ct82c710_port); | 
 | 241 |  | 
| Greg Kroah-Hartman | e29419f | 2006-06-12 15:20:16 -0700 | [diff] [blame] | 242 | 	printk(KERN_INFO "serio: C&T 82c710 mouse port at %#llx irq %d\n", | 
 | 243 | 		(unsigned long long)CT82C710_DATA, CT82C710_IRQ); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 |  | 
 | 245 | 	return 0; | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 246 |  | 
 | 247 |  err_free_device: | 
 | 248 | 	platform_device_put(ct82c710_device); | 
 | 249 |  err_unregister_driver: | 
 | 250 | 	platform_driver_unregister(&ct82c710_driver); | 
 | 251 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } | 
 | 253 |  | 
 | 254 | static void __exit ct82c710_exit(void) | 
 | 255 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | 	platform_device_unregister(ct82c710_device); | 
| Dmitry Torokhov | 916d83c | 2005-12-28 01:25:30 -0500 | [diff] [blame] | 257 | 	platform_driver_unregister(&ct82c710_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } | 
 | 259 |  | 
 | 260 | module_init(ct82c710_init); | 
 | 261 | module_exit(ct82c710_exit); |