Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / input / serio / q40kbd.c
blobae55c4de092f950801b521eab2bdd4d2d110f629
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2000-2001 Vojtech Pavlik
5 * Based on the work of:
6 * Richard Zidlicky <Richard.Zidlicky@stud.informatik.uni-erlangen.de>
7 */
9 /*
10 * Q40 PS/2 keyboard controller driver for Linux/m68k
13 #include <linux/module.h>
14 #include <linux/serio.h>
15 #include <linux/interrupt.h>
16 #include <linux/err.h>
17 #include <linux/bitops.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
21 #include <asm/io.h>
22 #include <linux/uaccess.h>
23 #include <asm/q40_master.h>
24 #include <asm/irq.h>
25 #include <asm/q40ints.h>
27 #define DRV_NAME "q40kbd"
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
30 MODULE_DESCRIPTION("Q40 PS/2 keyboard controller driver");
31 MODULE_LICENSE("GPL");
32 MODULE_ALIAS("platform:" DRV_NAME);
34 struct q40kbd {
35 struct serio *port;
36 spinlock_t lock;
39 static irqreturn_t q40kbd_interrupt(int irq, void *dev_id)
41 struct q40kbd *q40kbd = dev_id;
43 guard(spinlock_irqsave)(&q40kbd->lock);
45 if (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG))
46 serio_interrupt(q40kbd->port, master_inb(KEYCODE_REG), 0);
48 master_outb(-1, KEYBOARD_UNLOCK_REG);
50 return IRQ_HANDLED;
54 * q40kbd_flush() flushes all data that may be in the keyboard buffers
57 static void q40kbd_flush(struct q40kbd *q40kbd)
59 int maxread = 100;
61 guard(spinlock_irqsave)(&q40kbd->lock);
63 while (maxread-- && (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG)))
64 master_inb(KEYCODE_REG);
67 static void q40kbd_stop(void)
69 master_outb(0, KEY_IRQ_ENABLE_REG);
70 master_outb(-1, KEYBOARD_UNLOCK_REG);
74 * q40kbd_open() is called when a port is open by the higher layer.
75 * It allocates the interrupt and enables in in the chip.
78 static int q40kbd_open(struct serio *port)
80 struct q40kbd *q40kbd = port->port_data;
82 q40kbd_flush(q40kbd);
84 /* off we go */
85 master_outb(-1, KEYBOARD_UNLOCK_REG);
86 master_outb(1, KEY_IRQ_ENABLE_REG);
88 return 0;
91 static void q40kbd_close(struct serio *port)
93 struct q40kbd *q40kbd = port->port_data;
95 q40kbd_stop();
96 q40kbd_flush(q40kbd);
99 static int q40kbd_probe(struct platform_device *pdev)
101 struct q40kbd *q40kbd;
102 struct serio *port;
103 int error;
105 q40kbd = kzalloc(sizeof(*q40kbd), GFP_KERNEL);
106 port = kzalloc(sizeof(*port), GFP_KERNEL);
107 if (!q40kbd || !port) {
108 error = -ENOMEM;
109 goto err_free_mem;
112 q40kbd->port = port;
113 spin_lock_init(&q40kbd->lock);
115 port->id.type = SERIO_8042;
116 port->open = q40kbd_open;
117 port->close = q40kbd_close;
118 port->port_data = q40kbd;
119 port->dev.parent = &pdev->dev;
120 strscpy(port->name, "Q40 Kbd Port", sizeof(port->name));
121 strscpy(port->phys, "Q40", sizeof(port->phys));
123 q40kbd_stop();
125 error = request_irq(Q40_IRQ_KEYBOARD, q40kbd_interrupt, 0,
126 DRV_NAME, q40kbd);
127 if (error) {
128 dev_err(&pdev->dev, "Can't get irq %d.\n", Q40_IRQ_KEYBOARD);
129 goto err_free_mem;
132 serio_register_port(q40kbd->port);
134 platform_set_drvdata(pdev, q40kbd);
135 printk(KERN_INFO "serio: Q40 kbd registered\n");
137 return 0;
139 err_free_mem:
140 kfree(port);
141 kfree(q40kbd);
142 return error;
145 static void q40kbd_remove(struct platform_device *pdev)
147 struct q40kbd *q40kbd = platform_get_drvdata(pdev);
150 * q40kbd_close() will be called as part of unregistering
151 * and will ensure that IRQ is turned off, so it is safe
152 * to unregister port first and free IRQ later.
154 serio_unregister_port(q40kbd->port);
155 free_irq(Q40_IRQ_KEYBOARD, q40kbd);
156 kfree(q40kbd);
159 static struct platform_driver q40kbd_driver = {
160 .driver = {
161 .name = "q40kbd",
163 .remove = q40kbd_remove,
166 module_platform_driver_probe(q40kbd_driver, q40kbd_probe);