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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/serio.h> |
| 34 | #include <linux/interrupt.h> |
| 35 | #include <linux/err.h> |
| 36 | #include <linux/bitops.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 | #include <asm/uaccess.h> |
| 42 | #include <asm/q40_master.h> |
| 43 | #include <asm/irq.h> |
| 44 | #include <asm/q40ints.h> |
| 45 | |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 46 | #define DRV_NAME "q40kbd" |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 49 | MODULE_DESCRIPTION("Q40 PS/2 keyboard controller driver"); |
| 50 | MODULE_LICENSE("GPL"); |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 51 | MODULE_ALIAS("platform:" DRV_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 53 | struct q40kbd { |
| 54 | struct serio *port; |
| 55 | spinlock_t lock; |
| 56 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 58 | static irqreturn_t q40kbd_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 60 | struct q40kbd *q40kbd = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | unsigned long flags; |
| 62 | |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 63 | spin_lock_irqsave(&q40kbd->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | if (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG)) |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 66 | serio_interrupt(q40kbd->port, master_inb(KEYCODE_REG), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | master_outb(-1, KEYBOARD_UNLOCK_REG); |
| 69 | |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 70 | spin_unlock_irqrestore(&q40kbd->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | return IRQ_HANDLED; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * q40kbd_flush() flushes all data that may be in the keyboard buffers |
| 77 | */ |
| 78 | |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 79 | static void q40kbd_flush(struct q40kbd *q40kbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 81 | int maxread = 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | unsigned long flags; |
| 83 | |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 84 | spin_lock_irqsave(&q40kbd->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 86 | while (maxread-- && (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG))) |
| 87 | master_inb(KEYCODE_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 89 | spin_unlock_irqrestore(&q40kbd->lock, flags); |
| 90 | } |
| 91 | |
| 92 | static void q40kbd_stop(void) |
| 93 | { |
| 94 | master_outb(0, KEY_IRQ_ENABLE_REG); |
| 95 | master_outb(-1, KEYBOARD_UNLOCK_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* |
| 99 | * q40kbd_open() is called when a port is open by the higher layer. |
| 100 | * It allocates the interrupt and enables in in the chip. |
| 101 | */ |
| 102 | |
| 103 | static int q40kbd_open(struct serio *port) |
| 104 | { |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 105 | struct q40kbd *q40kbd = port->port_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 107 | q40kbd_flush(q40kbd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 109 | /* off we go */ |
| 110 | master_outb(-1, KEYBOARD_UNLOCK_REG); |
| 111 | master_outb(1, KEY_IRQ_ENABLE_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 113 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void q40kbd_close(struct serio *port) |
| 117 | { |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 118 | struct q40kbd *q40kbd = port->port_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 120 | q40kbd_stop(); |
| 121 | q40kbd_flush(q40kbd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 124 | static int q40kbd_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 126 | struct q40kbd *q40kbd; |
| 127 | struct serio *port; |
| 128 | int error; |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 129 | |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 130 | q40kbd = kzalloc(sizeof(struct q40kbd), GFP_KERNEL); |
| 131 | port = kzalloc(sizeof(struct serio), GFP_KERNEL); |
| 132 | if (!q40kbd || !port) { |
| 133 | error = -ENOMEM; |
| 134 | goto err_free_mem; |
| 135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 137 | q40kbd->port = port; |
| 138 | spin_lock_init(&q40kbd->lock); |
| 139 | |
| 140 | port->id.type = SERIO_8042; |
| 141 | port->open = q40kbd_open; |
| 142 | port->close = q40kbd_close; |
| 143 | port->port_data = q40kbd; |
| 144 | port->dev.parent = &pdev->dev; |
| 145 | strlcpy(port->name, "Q40 Kbd Port", sizeof(port->name)); |
| 146 | strlcpy(port->phys, "Q40", sizeof(port->phys)); |
| 147 | |
| 148 | q40kbd_stop(); |
| 149 | |
| 150 | error = request_irq(Q40_IRQ_KEYBOARD, q40kbd_interrupt, 0, |
| 151 | DRV_NAME, q40kbd); |
| 152 | if (error) { |
| 153 | dev_err(&pdev->dev, "Can't get irq %d.\n", Q40_IRQ_KEYBOARD); |
| 154 | goto err_free_mem; |
| 155 | } |
| 156 | |
| 157 | serio_register_port(q40kbd->port); |
| 158 | |
| 159 | platform_set_drvdata(pdev, q40kbd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | printk(KERN_INFO "serio: Q40 kbd registered\n"); |
| 161 | |
| 162 | return 0; |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 163 | |
| 164 | err_free_mem: |
| 165 | kfree(port); |
| 166 | kfree(q40kbd); |
| 167 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 170 | static int q40kbd_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 172 | struct q40kbd *q40kbd = platform_get_drvdata(pdev); |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 173 | |
Dmitry Torokhov | 409e154 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 174 | /* |
| 175 | * q40kbd_close() will be called as part of unregistering |
| 176 | * and will ensure that IRQ is turned off, so it is safe |
| 177 | * to unregister port first and free IRQ later. |
| 178 | */ |
| 179 | serio_unregister_port(q40kbd->port); |
| 180 | free_irq(Q40_IRQ_KEYBOARD, q40kbd); |
| 181 | kfree(q40kbd); |
| 182 | |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static struct platform_driver q40kbd_driver = { |
| 187 | .driver = { |
| 188 | .name = "q40kbd", |
| 189 | .owner = THIS_MODULE, |
| 190 | }, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 191 | .remove = q40kbd_remove, |
Dmitry Torokhov | 26421c7 | 2005-12-28 01:25:42 -0500 | [diff] [blame] | 192 | }; |
| 193 | |
Sachin Kamat | 07beda4 | 2013-03-17 21:28:28 -0700 | [diff] [blame] | 194 | module_platform_driver_probe(q40kbd_driver, q40kbd_probe); |