xref: /openbmc/linux/drivers/input/serio/q40kbd.c (revision 3213486f)
1 /*
2  *  Copyright (c) 2000-2001 Vojtech Pavlik
3  *
4  *  Based on the work of:
5  *	Richard Zidlicky <Richard.Zidlicky@stud.informatik.uni-erlangen.de>
6  */
7 
8 /*
9  * Q40 PS/2 keyboard controller driver for Linux/m68k
10  */
11 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  */
27 
28 #include <linux/module.h>
29 #include <linux/serio.h>
30 #include <linux/interrupt.h>
31 #include <linux/err.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/slab.h>
35 
36 #include <asm/io.h>
37 #include <linux/uaccess.h>
38 #include <asm/q40_master.h>
39 #include <asm/irq.h>
40 #include <asm/q40ints.h>
41 
42 #define DRV_NAME	"q40kbd"
43 
44 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45 MODULE_DESCRIPTION("Q40 PS/2 keyboard controller driver");
46 MODULE_LICENSE("GPL");
47 MODULE_ALIAS("platform:" DRV_NAME);
48 
49 struct q40kbd {
50 	struct serio *port;
51 	spinlock_t lock;
52 };
53 
54 static irqreturn_t q40kbd_interrupt(int irq, void *dev_id)
55 {
56 	struct q40kbd *q40kbd = dev_id;
57 	unsigned long flags;
58 
59 	spin_lock_irqsave(&q40kbd->lock, flags);
60 
61 	if (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG))
62 		serio_interrupt(q40kbd->port, master_inb(KEYCODE_REG), 0);
63 
64 	master_outb(-1, KEYBOARD_UNLOCK_REG);
65 
66 	spin_unlock_irqrestore(&q40kbd->lock, flags);
67 
68 	return IRQ_HANDLED;
69 }
70 
71 /*
72  * q40kbd_flush() flushes all data that may be in the keyboard buffers
73  */
74 
75 static void q40kbd_flush(struct q40kbd *q40kbd)
76 {
77 	int maxread = 100;
78 	unsigned long flags;
79 
80 	spin_lock_irqsave(&q40kbd->lock, flags);
81 
82 	while (maxread-- && (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG)))
83 		master_inb(KEYCODE_REG);
84 
85 	spin_unlock_irqrestore(&q40kbd->lock, flags);
86 }
87 
88 static void q40kbd_stop(void)
89 {
90 	master_outb(0, KEY_IRQ_ENABLE_REG);
91 	master_outb(-1, KEYBOARD_UNLOCK_REG);
92 }
93 
94 /*
95  * q40kbd_open() is called when a port is open by the higher layer.
96  * It allocates the interrupt and enables in in the chip.
97  */
98 
99 static int q40kbd_open(struct serio *port)
100 {
101 	struct q40kbd *q40kbd = port->port_data;
102 
103 	q40kbd_flush(q40kbd);
104 
105 	/* off we go */
106 	master_outb(-1, KEYBOARD_UNLOCK_REG);
107 	master_outb(1, KEY_IRQ_ENABLE_REG);
108 
109 	return 0;
110 }
111 
112 static void q40kbd_close(struct serio *port)
113 {
114 	struct q40kbd *q40kbd = port->port_data;
115 
116 	q40kbd_stop();
117 	q40kbd_flush(q40kbd);
118 }
119 
120 static int q40kbd_probe(struct platform_device *pdev)
121 {
122 	struct q40kbd *q40kbd;
123 	struct serio *port;
124 	int error;
125 
126 	q40kbd = kzalloc(sizeof(struct q40kbd), GFP_KERNEL);
127 	port = kzalloc(sizeof(struct serio), GFP_KERNEL);
128 	if (!q40kbd || !port) {
129 		error = -ENOMEM;
130 		goto err_free_mem;
131 	}
132 
133 	q40kbd->port = port;
134 	spin_lock_init(&q40kbd->lock);
135 
136 	port->id.type = SERIO_8042;
137 	port->open = q40kbd_open;
138 	port->close = q40kbd_close;
139 	port->port_data = q40kbd;
140 	port->dev.parent = &pdev->dev;
141 	strlcpy(port->name, "Q40 Kbd Port", sizeof(port->name));
142 	strlcpy(port->phys, "Q40", sizeof(port->phys));
143 
144 	q40kbd_stop();
145 
146 	error = request_irq(Q40_IRQ_KEYBOARD, q40kbd_interrupt, 0,
147 			    DRV_NAME, q40kbd);
148 	if (error) {
149 		dev_err(&pdev->dev, "Can't get irq %d.\n", Q40_IRQ_KEYBOARD);
150 		goto err_free_mem;
151 	}
152 
153 	serio_register_port(q40kbd->port);
154 
155 	platform_set_drvdata(pdev, q40kbd);
156 	printk(KERN_INFO "serio: Q40 kbd registered\n");
157 
158 	return 0;
159 
160 err_free_mem:
161 	kfree(port);
162 	kfree(q40kbd);
163 	return error;
164 }
165 
166 static int q40kbd_remove(struct platform_device *pdev)
167 {
168 	struct q40kbd *q40kbd = platform_get_drvdata(pdev);
169 
170 	/*
171 	 * q40kbd_close() will be called as part of unregistering
172 	 * and will ensure that IRQ is turned off, so it is safe
173 	 * to unregister port first and free IRQ later.
174 	 */
175 	serio_unregister_port(q40kbd->port);
176 	free_irq(Q40_IRQ_KEYBOARD, q40kbd);
177 	kfree(q40kbd);
178 
179 	return 0;
180 }
181 
182 static struct platform_driver q40kbd_driver = {
183 	.driver		= {
184 		.name	= "q40kbd",
185 	},
186 	.remove		= q40kbd_remove,
187 };
188 
189 module_platform_driver_probe(q40kbd_driver, q40kbd_probe);
190