Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *  Copyright (c) 1999-2001 Vojtech Pavlik
  4 */
  5
  6/*
  7 *  82C710 C&T mouse port chip driver for Linux
  8 */
  9
 10/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 11 */
 12
 13#include <linux/delay.h>
 14#include <linux/module.h>
 15#include <linux/ioport.h>
 16#include <linux/init.h>
 17#include <linux/interrupt.h>
 18#include <linux/serio.h>
 19#include <linux/errno.h>
 20#include <linux/err.h>
 21#include <linux/platform_device.h>
 22#include <linux/slab.h>
 23
 24#include <asm/io.h>
 25
 26MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 27MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
 28MODULE_LICENSE("GPL");
 29
 30/*
 31 * ct82c710 interface
 32 */
 33
 34#define CT82C710_DEV_IDLE     0x01		/* Device Idle */
 35#define CT82C710_RX_FULL      0x02		/* Device Char received */
 36#define CT82C710_TX_IDLE      0x04		/* Device XMIT Idle */
 37#define CT82C710_RESET        0x08		/* Device Reset */
 38#define CT82C710_INTS_ON      0x10		/* Device Interrupt On */
 39#define CT82C710_ERROR_FLAG   0x20		/* Device Error */
 40#define CT82C710_CLEAR        0x40		/* Device Clear */
 41#define CT82C710_ENABLE       0x80		/* Device Enable */
 42
 43#define CT82C710_IRQ          12
 44
 45#define CT82C710_DATA         ct82c710_iores.start
 46#define CT82C710_STATUS       (ct82c710_iores.start + 1)
 47
 48static struct serio *ct82c710_port;
 49static struct platform_device *ct82c710_device;
 50static struct resource ct82c710_iores;
 51
 52/*
 53 * Interrupt handler for the 82C710 mouse port. A character
 54 * is waiting in the 82C710.
 55 */
 56
 57static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id)
 58{
 59	return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0);
 60}
 61
 62/*
 63 * Wait for device to send output char and flush any input char.
 64 */
 65
 66static int ct82c170_wait(void)
 67{
 68	int timeout = 60000;
 69
 70	while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
 71		       != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
 72
 73		if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA);
 74
 75		udelay(1);
 76		timeout--;
 77	}
 78
 79	return !timeout;
 80}
 81
 82static void ct82c710_close(struct serio *serio)
 83{
 84	if (ct82c170_wait())
 85		printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
 86
 87	outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS);
 88
 89	if (ct82c170_wait())
 90		printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
 91
 92	free_irq(CT82C710_IRQ, NULL);
 93}
 94
 95static int ct82c710_open(struct serio *serio)
 96{
 97	unsigned char status;
 98	int err;
 99
100	err = request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL);
101	if (err)
102		return err;
103
104	status = inb_p(CT82C710_STATUS);
105
106	status |= (CT82C710_ENABLE | CT82C710_RESET);
107	outb_p(status, CT82C710_STATUS);
108
109	status &= ~(CT82C710_RESET);
110	outb_p(status, CT82C710_STATUS);
111
112	status |= CT82C710_INTS_ON;
113	outb_p(status, CT82C710_STATUS);	/* Enable interrupts */
114
115	while (ct82c170_wait()) {
116		printk(KERN_ERR "ct82c710: Device busy in open()\n");
117		status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
118		outb_p(status, CT82C710_STATUS);
119		free_irq(CT82C710_IRQ, NULL);
120		return -EBUSY;
121	}
122
123	return 0;
124}
125
126/*
127 * Write to the 82C710 mouse device.
128 */
129
130static int ct82c710_write(struct serio *port, unsigned char c)
131{
132	if (ct82c170_wait()) return -1;
133	outb_p(c, CT82C710_DATA);
134	return 0;
135}
136
137/*
138 * See if we can find a 82C710 device. Read mouse address.
139 */
140
141static int __init ct82c710_detect(void)
142{
143	outb_p(0x55, 0x2fa);				/* Any value except 9, ff or 36 */
144	outb_p(0xaa, 0x3fa);				/* Inverse of 55 */
145	outb_p(0x36, 0x3fa);				/* Address the chip */
146	outb_p(0xe4, 0x3fa);				/* 390/4; 390 = config address */
147	outb_p(0x1b, 0x2fa);				/* Inverse of e4 */
148	outb_p(0x0f, 0x390);				/* Write index */
149	if (inb_p(0x391) != 0xe4)			/* Config address found? */
150		return -ENODEV;				/* No: no 82C710 here */
151
152	outb_p(0x0d, 0x390);				/* Write index */
153	ct82c710_iores.start = inb_p(0x391) << 2;	/* Get mouse I/O address */
154	ct82c710_iores.end = ct82c710_iores.start + 1;
155	ct82c710_iores.flags = IORESOURCE_IO;
156	outb_p(0x0f, 0x390);
157	outb_p(0x0f, 0x391);				/* Close config mode */
158
159	return 0;
160}
161
162static int ct82c710_probe(struct platform_device *dev)
163{
164	ct82c710_port = kzalloc(sizeof(struct serio), GFP_KERNEL);
165	if (!ct82c710_port)
166		return -ENOMEM;
167
168	ct82c710_port->id.type = SERIO_8042;
169	ct82c710_port->dev.parent = &dev->dev;
170	ct82c710_port->open = ct82c710_open;
171	ct82c710_port->close = ct82c710_close;
172	ct82c710_port->write = ct82c710_write;
173	strlcpy(ct82c710_port->name, "C&T 82c710 mouse port",
174		sizeof(ct82c710_port->name));
175	snprintf(ct82c710_port->phys, sizeof(ct82c710_port->phys),
176		 "isa%16llx/serio0", (unsigned long long)CT82C710_DATA);
177
178	serio_register_port(ct82c710_port);
179
180	printk(KERN_INFO "serio: C&T 82c710 mouse port at %#llx irq %d\n",
181		(unsigned long long)CT82C710_DATA, CT82C710_IRQ);
182
183	return 0;
184}
185
186static int ct82c710_remove(struct platform_device *dev)
187{
188	serio_unregister_port(ct82c710_port);
189
190	return 0;
191}
192
193static struct platform_driver ct82c710_driver = {
194	.driver		= {
195		.name	= "ct82c710",
196	},
197	.probe		= ct82c710_probe,
198	.remove		= ct82c710_remove,
199};
200
201
202static int __init ct82c710_init(void)
203{
204	int error;
205
206	error = ct82c710_detect();
207	if (error)
208		return error;
209
210	error = platform_driver_register(&ct82c710_driver);
211	if (error)
212		return error;
213
214	ct82c710_device = platform_device_alloc("ct82c710", -1);
215	if (!ct82c710_device) {
216		error = -ENOMEM;
217		goto err_unregister_driver;
218	}
219
220	error = platform_device_add_resources(ct82c710_device, &ct82c710_iores, 1);
221	if (error)
222		goto err_free_device;
223
224	error = platform_device_add(ct82c710_device);
225	if (error)
226		goto err_free_device;
227
228	return 0;
229
230 err_free_device:
231	platform_device_put(ct82c710_device);
232 err_unregister_driver:
233	platform_driver_unregister(&ct82c710_driver);
234	return error;
235}
236
237static void __exit ct82c710_exit(void)
238{
239	platform_device_unregister(ct82c710_device);
240	platform_driver_unregister(&ct82c710_driver);
241}
242
243module_init(ct82c710_init);
244module_exit(ct82c710_exit);
v4.6
 
  1/*
  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>
 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>
 37#include <linux/platform_device.h>
 38#include <linux/slab.h>
 39
 40#include <asm/io.h>
 41
 42MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 43MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
 44MODULE_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
 64static struct serio *ct82c710_port;
 65static struct platform_device *ct82c710_device;
 66static struct resource ct82c710_iores;
 67
 68/*
 69 * Interrupt handler for the 82C710 mouse port. A character
 70 * is waiting in the 82C710.
 71 */
 72
 73static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id)
 74{
 75	return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0);
 76}
 77
 78/*
 79 * Wait for device to send output char and flush any input char.
 80 */
 81
 82static 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
 98static 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
111static int ct82c710_open(struct serio *serio)
112{
113	unsigned char status;
114	int err;
115
116	err = request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL);
117	if (err)
118		return err;
119
120	status = inb_p(CT82C710_STATUS);
121
122	status |= (CT82C710_ENABLE | CT82C710_RESET);
123	outb_p(status, CT82C710_STATUS);
124
125	status &= ~(CT82C710_RESET);
126	outb_p(status, CT82C710_STATUS);
127
128	status |= CT82C710_INTS_ON;
129	outb_p(status, CT82C710_STATUS);	/* Enable interrupts */
130
131	while (ct82c170_wait()) {
132		printk(KERN_ERR "ct82c710: Device busy in open()\n");
133		status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
134		outb_p(status, CT82C710_STATUS);
135		free_irq(CT82C710_IRQ, NULL);
136		return -EBUSY;
137	}
138
139	return 0;
140}
141
142/*
143 * Write to the 82C710 mouse device.
144 */
145
146static int ct82c710_write(struct serio *port, unsigned char c)
147{
148	if (ct82c170_wait()) return -1;
149	outb_p(c, CT82C710_DATA);
150	return 0;
151}
152
153/*
154 * See if we can find a 82C710 device. Read mouse address.
155 */
156
157static int __init ct82c710_detect(void)
158{
159	outb_p(0x55, 0x2fa);				/* Any value except 9, ff or 36 */
160	outb_p(0xaa, 0x3fa);				/* Inverse of 55 */
161	outb_p(0x36, 0x3fa);				/* Address the chip */
162	outb_p(0xe4, 0x3fa);				/* 390/4; 390 = config address */
163	outb_p(0x1b, 0x2fa);				/* Inverse of e4 */
164	outb_p(0x0f, 0x390);				/* Write index */
165	if (inb_p(0x391) != 0xe4)			/* Config address found? */
166		return -ENODEV;				/* No: no 82C710 here */
167
168	outb_p(0x0d, 0x390);				/* Write index */
169	ct82c710_iores.start = inb_p(0x391) << 2;	/* Get mouse I/O address */
170	ct82c710_iores.end = ct82c710_iores.start + 1;
171	ct82c710_iores.flags = IORESOURCE_IO;
172	outb_p(0x0f, 0x390);
173	outb_p(0x0f, 0x391);				/* Close config mode */
174
175	return 0;
176}
177
178static int ct82c710_probe(struct platform_device *dev)
179{
180	ct82c710_port = kzalloc(sizeof(struct serio), GFP_KERNEL);
181	if (!ct82c710_port)
182		return -ENOMEM;
183
184	ct82c710_port->id.type = SERIO_8042;
185	ct82c710_port->dev.parent = &dev->dev;
186	ct82c710_port->open = ct82c710_open;
187	ct82c710_port->close = ct82c710_close;
188	ct82c710_port->write = ct82c710_write;
189	strlcpy(ct82c710_port->name, "C&T 82c710 mouse port",
190		sizeof(ct82c710_port->name));
191	snprintf(ct82c710_port->phys, sizeof(ct82c710_port->phys),
192		 "isa%16llx/serio0", (unsigned long long)CT82C710_DATA);
193
194	serio_register_port(ct82c710_port);
195
196	printk(KERN_INFO "serio: C&T 82c710 mouse port at %#llx irq %d\n",
197		(unsigned long long)CT82C710_DATA, CT82C710_IRQ);
198
199	return 0;
200}
201
202static int ct82c710_remove(struct platform_device *dev)
203{
204	serio_unregister_port(ct82c710_port);
205
206	return 0;
207}
208
209static struct platform_driver ct82c710_driver = {
210	.driver		= {
211		.name	= "ct82c710",
212	},
213	.probe		= ct82c710_probe,
214	.remove		= ct82c710_remove,
215};
216
217
218static int __init ct82c710_init(void)
219{
220	int error;
221
222	error = ct82c710_detect();
223	if (error)
224		return error;
225
226	error = platform_driver_register(&ct82c710_driver);
227	if (error)
228		return error;
229
230	ct82c710_device = platform_device_alloc("ct82c710", -1);
231	if (!ct82c710_device) {
232		error = -ENOMEM;
233		goto err_unregister_driver;
234	}
235
236	error = platform_device_add_resources(ct82c710_device, &ct82c710_iores, 1);
237	if (error)
238		goto err_free_device;
239
240	error = platform_device_add(ct82c710_device);
241	if (error)
242		goto err_free_device;
243
244	return 0;
245
246 err_free_device:
247	platform_device_put(ct82c710_device);
248 err_unregister_driver:
249	platform_driver_unregister(&ct82c710_driver);
250	return error;
251}
252
253static void __exit ct82c710_exit(void)
254{
255	platform_device_unregister(ct82c710_device);
256	platform_driver_unregister(&ct82c710_driver);
257}
258
259module_init(ct82c710_init);
260module_exit(ct82c710_exit);