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) 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 | * Should you need to contact me, the author, you can do so either by |
| 28 | * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: |
| 29 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic |
| 30 | */ |
| 31 | |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/serio.h> |
| 35 | #include <linux/interrupt.h> |
| 36 | #include <linux/err.h> |
| 37 | #include <linux/bitops.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 38 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | #include <asm/io.h> |
| 42 | #include <asm/uaccess.h> |
| 43 | #include <asm/q40_master.h> |
| 44 | #include <asm/irq.h> |
| 45 | #include <asm/q40ints.h> |
| 46 | |
| 47 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 48 | MODULE_DESCRIPTION("Q40 PS/2 keyboard controller driver"); |
| 49 | MODULE_LICENSE("GPL"); |
| 50 | |
Adrian Bunk | 4ad8890 | 2008-07-03 12:10:52 -0400 | [diff] [blame] | 51 | static DEFINE_SPINLOCK(q40kbd_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static struct serio *q40kbd_port; |
| 53 | static struct platform_device *q40kbd_device; |
| 54 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 55 | static irqreturn_t q40kbd_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | unsigned long flags; |
| 58 | |
| 59 | spin_lock_irqsave(&q40kbd_lock, flags); |
| 60 | |
| 61 | if (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG)) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 62 | serio_interrupt(q40kbd_port, master_inb(KEYCODE_REG), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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(void) |
| 76 | { |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 77 | int maxread = 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | unsigned long flags; |
| 79 | |
| 80 | spin_lock_irqsave(&q40kbd_lock, flags); |
| 81 | |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 82 | while (maxread-- && (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG))) |
| 83 | master_inb(KEYCODE_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | spin_unlock_irqrestore(&q40kbd_lock, flags); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * q40kbd_open() is called when a port is open by the higher layer. |
| 90 | * It allocates the interrupt and enables in in the chip. |
| 91 | */ |
| 92 | |
| 93 | static int q40kbd_open(struct serio *port) |
| 94 | { |
| 95 | q40kbd_flush(); |
| 96 | |
| 97 | if (request_irq(Q40_IRQ_KEYBOARD, q40kbd_interrupt, 0, "q40kbd", NULL)) { |
| 98 | printk(KERN_ERR "q40kbd.c: Can't get irq %d.\n", Q40_IRQ_KEYBOARD); |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 99 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 102 | /* off we go */ |
| 103 | master_outb(-1, KEYBOARD_UNLOCK_REG); |
| 104 | master_outb(1, KEY_IRQ_ENABLE_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 106 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void q40kbd_close(struct serio *port) |
| 110 | { |
| 111 | master_outb(0, KEY_IRQ_ENABLE_REG); |
| 112 | master_outb(-1, KEYBOARD_UNLOCK_REG); |
| 113 | free_irq(Q40_IRQ_KEYBOARD, NULL); |
| 114 | |
| 115 | q40kbd_flush(); |
| 116 | } |
| 117 | |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 118 | static int __devinit q40kbd_probe(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 120 | q40kbd_port = kzalloc(sizeof(struct serio), GFP_KERNEL); |
| 121 | if (!q40kbd_port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | return -ENOMEM; |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 123 | |
| 124 | q40kbd_port->id.type = SERIO_8042; |
| 125 | q40kbd_port->open = q40kbd_open; |
| 126 | q40kbd_port->close = q40kbd_close; |
| 127 | q40kbd_port->dev.parent = &dev->dev; |
| 128 | strlcpy(q40kbd_port->name, "Q40 Kbd Port", sizeof(q40kbd_port->name)); |
| 129 | strlcpy(q40kbd_port->phys, "Q40", sizeof(q40kbd_port->phys)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | serio_register_port(q40kbd_port); |
| 132 | printk(KERN_INFO "serio: Q40 kbd registered\n"); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 137 | static int __devexit q40kbd_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
| 139 | serio_unregister_port(q40kbd_port); |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static struct platform_driver q40kbd_driver = { |
| 145 | .driver = { |
| 146 | .name = "q40kbd", |
| 147 | .owner = THIS_MODULE, |
| 148 | }, |
| 149 | .probe = q40kbd_probe, |
| 150 | .remove = __devexit_p(q40kbd_remove), |
| 151 | }; |
| 152 | |
| 153 | static int __init q40kbd_init(void) |
| 154 | { |
| 155 | int error; |
| 156 | |
| 157 | if (!MACH_IS_Q40) |
Geert Uytterhoeven | fd5b462 | 2008-05-18 20:47:18 +0200 | [diff] [blame] | 158 | return -ENODEV; |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 159 | |
| 160 | error = platform_driver_register(&q40kbd_driver); |
| 161 | if (error) |
| 162 | return error; |
| 163 | |
| 164 | q40kbd_device = platform_device_alloc("q40kbd", -1); |
| 165 | if (!q40kbd_device) |
| 166 | goto err_unregister_driver; |
| 167 | |
| 168 | error = platform_device_add(q40kbd_device); |
| 169 | if (error) |
| 170 | goto err_free_device; |
| 171 | |
| 172 | return 0; |
| 173 | |
| 174 | err_free_device: |
| 175 | platform_device_put(q40kbd_device); |
| 176 | err_unregister_driver: |
| 177 | platform_driver_unregister(&q40kbd_driver); |
| 178 | return error; |
| 179 | } |
| 180 | |
| 181 | static void __exit q40kbd_exit(void) |
| 182 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | platform_device_unregister(q40kbd_device); |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 184 | platform_driver_unregister(&q40kbd_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | module_init(q40kbd_init); |
| 188 | module_exit(q40kbd_exit); |