Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * SEGA Dreamcast keyboard driver |
| 3 | * Based on drivers/usb/usbkbd.c |
Adrian McMenamin | b233b28 | 2009-02-27 16:07:32 +0900 | [diff] [blame] | 4 | * Copyright (c) YAEGASHI Takeshi, 2001 |
| 5 | * Porting to 2.6 Copyright (c) Adrian McMenamin, 2007 - 2009 |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, see the file COPYING, or write |
| 19 | * to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/input.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/timer.h> |
| 29 | #include <linux/maple.h> |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 30 | |
| 31 | /* Very simple mutex to ensure proper cleanup */ |
| 32 | static DEFINE_MUTEX(maple_keyb_mutex); |
| 33 | |
| 34 | #define NR_SCANCODES 256 |
| 35 | |
Adrian McMenamin | b233b28 | 2009-02-27 16:07:32 +0900 | [diff] [blame] | 36 | MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk"); |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 37 | MODULE_DESCRIPTION("SEGA Dreamcast keyboard driver"); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | |
| 40 | struct dc_kbd { |
| 41 | struct input_dev *dev; |
| 42 | unsigned short keycode[NR_SCANCODES]; |
| 43 | unsigned char new[8]; |
| 44 | unsigned char old[8]; |
| 45 | }; |
| 46 | |
| 47 | static const unsigned short dc_kbd_keycode[NR_SCANCODES] = { |
Adrian McMenamin | 86d9d32 | 2008-07-30 12:31:38 -0700 | [diff] [blame] | 48 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_A, KEY_B, |
| 49 | KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J, KEY_K, KEY_L, |
| 50 | KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T, KEY_U, KEY_V, |
| 51 | KEY_W, KEY_X, KEY_Y, KEY_Z, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, |
| 52 | KEY_7, KEY_8, KEY_9, KEY_0, KEY_ENTER, KEY_ESC, KEY_BACKSPACE, |
| 53 | KEY_TAB, KEY_SPACE, KEY_MINUS, KEY_EQUAL, KEY_LEFTBRACE, |
| 54 | KEY_RIGHTBRACE, KEY_BACKSLASH, KEY_BACKSLASH, KEY_SEMICOLON, |
| 55 | KEY_APOSTROPHE, KEY_GRAVE, KEY_COMMA, KEY_DOT, KEY_SLASH, |
| 56 | KEY_CAPSLOCK, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 57 | KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, KEY_SYSRQ, |
Adrian McMenamin | 86d9d32 | 2008-07-30 12:31:38 -0700 | [diff] [blame] | 58 | KEY_SCROLLLOCK, KEY_PAUSE, KEY_INSERT, KEY_HOME, KEY_PAGEUP, |
| 59 | KEY_DELETE, KEY_END, KEY_PAGEDOWN, KEY_RIGHT, KEY_LEFT, KEY_DOWN, |
| 60 | KEY_UP, KEY_NUMLOCK, KEY_KPSLASH, KEY_KPASTERISK, KEY_KPMINUS, |
| 61 | KEY_KPPLUS, KEY_KPENTER, KEY_KP1, KEY_KP2, KEY_KP3, KEY_KP4, KEY_KP5, |
| 62 | KEY_KP6, KEY_KP7, KEY_KP8, KEY_KP9, KEY_KP0, KEY_KPDOT, KEY_102ND, |
| 63 | KEY_COMPOSE, KEY_POWER, KEY_KPEQUAL, KEY_F13, KEY_F14, KEY_F15, |
| 64 | KEY_F16, KEY_F17, KEY_F18, KEY_F19, KEY_F20, KEY_F21, KEY_F22, |
| 65 | KEY_F23, KEY_F24, KEY_OPEN, KEY_HELP, KEY_PROPS, KEY_FRONT, KEY_STOP, |
| 66 | KEY_AGAIN, KEY_UNDO, KEY_CUT, KEY_COPY, KEY_PASTE, KEY_FIND, KEY_MUTE, |
| 67 | KEY_VOLUMEUP, KEY_VOLUMEDOWN, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 68 | KEY_KPCOMMA, KEY_RESERVED, KEY_RO, KEY_KATAKANAHIRAGANA , KEY_YEN, |
| 69 | KEY_HENKAN, KEY_MUHENKAN, KEY_KPJPCOMMA, KEY_RESERVED, KEY_RESERVED, |
| 70 | KEY_RESERVED, KEY_HANGEUL, KEY_HANJA, KEY_KATAKANA, KEY_HIRAGANA, |
| 71 | KEY_ZENKAKUHANKAKU, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 72 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 73 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 74 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 75 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 76 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 77 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 78 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 79 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 80 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 81 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 82 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 83 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 84 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 85 | KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, |
| 86 | KEY_RESERVED, KEY_RESERVED, KEY_LEFTCTRL, KEY_LEFTSHIFT, KEY_LEFTALT, |
| 87 | KEY_LEFTMETA, KEY_RIGHTCTRL, KEY_RIGHTSHIFT, KEY_RIGHTALT, |
| 88 | KEY_RIGHTMETA, KEY_PLAYPAUSE, KEY_STOPCD, KEY_PREVIOUSSONG, |
| 89 | KEY_NEXTSONG, KEY_EJECTCD, KEY_VOLUMEUP, KEY_VOLUMEDOWN, KEY_MUTE, |
| 90 | KEY_WWW, KEY_BACK, KEY_FORWARD, KEY_STOP, KEY_FIND, KEY_SCROLLUP, |
| 91 | KEY_SCROLLDOWN, KEY_EDIT, KEY_SLEEP, KEY_SCREENLOCK, KEY_REFRESH, |
| 92 | KEY_CALC, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | static void dc_scan_kbd(struct dc_kbd *kbd) |
| 96 | { |
| 97 | struct input_dev *dev = kbd->dev; |
| 98 | void *ptr; |
| 99 | int code, keycode; |
| 100 | int i; |
| 101 | |
| 102 | for (i = 0; i < 8; i++) { |
| 103 | code = i + 224; |
| 104 | keycode = kbd->keycode[code]; |
| 105 | input_event(dev, EV_MSC, MSC_SCAN, code); |
| 106 | input_report_key(dev, keycode, (kbd->new[0] >> i) & 1); |
| 107 | } |
| 108 | |
| 109 | for (i = 2; i < 8; i++) { |
| 110 | ptr = memchr(kbd->new + 2, kbd->old[i], 6); |
| 111 | code = kbd->old[i]; |
| 112 | if (code > 3 && ptr == NULL) { |
| 113 | keycode = kbd->keycode[code]; |
| 114 | if (keycode) { |
| 115 | input_event(dev, EV_MSC, MSC_SCAN, code); |
| 116 | input_report_key(dev, keycode, 0); |
| 117 | } else |
Adrian McMenamin | b233b28 | 2009-02-27 16:07:32 +0900 | [diff] [blame] | 118 | dev_dbg(&dev->dev, |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 119 | "Unknown key (scancode %#x) released.", |
| 120 | code); |
| 121 | } |
| 122 | ptr = memchr(kbd->old + 2, kbd->new[i], 6); |
| 123 | code = kbd->new[i]; |
| 124 | if (code > 3 && ptr) { |
| 125 | keycode = kbd->keycode[code]; |
| 126 | if (keycode) { |
| 127 | input_event(dev, EV_MSC, MSC_SCAN, code); |
| 128 | input_report_key(dev, keycode, 1); |
| 129 | } else |
Adrian McMenamin | b233b28 | 2009-02-27 16:07:32 +0900 | [diff] [blame] | 130 | dev_dbg(&dev->dev, |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 131 | "Unknown key (scancode %#x) pressed.", |
| 132 | code); |
| 133 | } |
| 134 | } |
| 135 | input_sync(dev); |
| 136 | memcpy(kbd->old, kbd->new, 8); |
| 137 | } |
| 138 | |
| 139 | static void dc_kbd_callback(struct mapleq *mq) |
| 140 | { |
| 141 | struct maple_device *mapledev = mq->dev; |
Paul Mundt | 6178706 | 2008-08-04 10:58:24 +0900 | [diff] [blame] | 142 | struct dc_kbd *kbd = maple_get_drvdata(mapledev); |
Adrian McMenamin | b233b28 | 2009-02-27 16:07:32 +0900 | [diff] [blame] | 143 | unsigned long *buf = (unsigned long *)(mq->recvbuf->buf); |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 144 | |
| 145 | /* |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 146 | * We should always get the lock because the only |
| 147 | * time it may be locked is if the driver is in the cleanup phase. |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 148 | */ |
| 149 | if (likely(mutex_trylock(&maple_keyb_mutex))) { |
| 150 | |
| 151 | if (buf[1] == mapledev->function) { |
| 152 | memcpy(kbd->new, buf + 2, 8); |
| 153 | dc_scan_kbd(kbd); |
| 154 | } |
| 155 | |
| 156 | mutex_unlock(&maple_keyb_mutex); |
| 157 | } |
| 158 | } |
| 159 | |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 160 | static int probe_maple_kbd(struct device *dev) |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 161 | { |
Adrian McMenamin | b233b28 | 2009-02-27 16:07:32 +0900 | [diff] [blame] | 162 | struct maple_device *mdev; |
| 163 | struct maple_driver *mdrv; |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 164 | int i, error; |
| 165 | struct dc_kbd *kbd; |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 166 | struct input_dev *idev; |
| 167 | |
Adrian McMenamin | b233b28 | 2009-02-27 16:07:32 +0900 | [diff] [blame] | 168 | mdev = to_maple_dev(dev); |
| 169 | mdrv = to_maple_driver(dev->driver); |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 170 | |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 171 | kbd = kzalloc(sizeof(struct dc_kbd), GFP_KERNEL); |
Adrian McMenamin | b233b28 | 2009-02-27 16:07:32 +0900 | [diff] [blame] | 172 | if (!kbd) { |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 173 | error = -ENOMEM; |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 174 | goto fail; |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Adrian McMenamin | b233b28 | 2009-02-27 16:07:32 +0900 | [diff] [blame] | 177 | idev = input_allocate_device(); |
| 178 | if (!idev) { |
| 179 | error = -ENOMEM; |
| 180 | goto fail_idev_alloc; |
| 181 | } |
| 182 | |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 183 | kbd->dev = idev; |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 184 | memcpy(kbd->keycode, dc_kbd_keycode, sizeof(kbd->keycode)); |
| 185 | |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 186 | idev->name = mdev->product_name; |
| 187 | idev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP); |
| 188 | idev->keycode = kbd->keycode; |
| 189 | idev->keycodesize = sizeof(unsigned short); |
| 190 | idev->keycodemax = ARRAY_SIZE(kbd->keycode); |
| 191 | idev->id.bustype = BUS_HOST; |
| 192 | idev->dev.parent = &mdev->dev; |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 193 | |
| 194 | for (i = 0; i < NR_SCANCODES; i++) |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 195 | __set_bit(dc_kbd_keycode[i], idev->keybit); |
| 196 | __clear_bit(KEY_RESERVED, idev->keybit); |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 197 | |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 198 | input_set_capability(idev, EV_MSC, MSC_SCAN); |
| 199 | input_set_drvdata(idev, kbd); |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 200 | |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 201 | error = input_register_device(idev); |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 202 | if (error) |
Adrian McMenamin | b233b28 | 2009-02-27 16:07:32 +0900 | [diff] [blame] | 203 | goto fail_register; |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 204 | |
| 205 | /* Maple polling is locked to VBLANK - which may be just 50/s */ |
Adrian McMenamin | 86d9d32 | 2008-07-30 12:31:38 -0700 | [diff] [blame] | 206 | maple_getcond_callback(mdev, dc_kbd_callback, HZ/50, |
| 207 | MAPLE_FUNC_KEYBOARD); |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 208 | |
| 209 | mdev->driver = mdrv; |
Paul Mundt | 6178706 | 2008-08-04 10:58:24 +0900 | [diff] [blame] | 210 | |
| 211 | maple_set_drvdata(mdev, kbd); |
| 212 | |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 213 | return error; |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 214 | |
Adrian McMenamin | b233b28 | 2009-02-27 16:07:32 +0900 | [diff] [blame] | 215 | fail_register: |
Paul Mundt | 6178706 | 2008-08-04 10:58:24 +0900 | [diff] [blame] | 216 | maple_set_drvdata(mdev, NULL); |
Adrian McMenamin | b233b28 | 2009-02-27 16:07:32 +0900 | [diff] [blame] | 217 | input_free_device(idev); |
| 218 | fail_idev_alloc: |
| 219 | kfree(kbd); |
| 220 | fail: |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 221 | return error; |
| 222 | } |
| 223 | |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 224 | static int remove_maple_kbd(struct device *dev) |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 225 | { |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 226 | struct maple_device *mdev = to_maple_dev(dev); |
Paul Mundt | 6178706 | 2008-08-04 10:58:24 +0900 | [diff] [blame] | 227 | struct dc_kbd *kbd = maple_get_drvdata(mdev); |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 228 | |
| 229 | mutex_lock(&maple_keyb_mutex); |
Adrian McMenamin | 459021f | 2008-08-04 10:09:03 +0900 | [diff] [blame] | 230 | |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 231 | input_unregister_device(kbd->dev); |
| 232 | kfree(kbd); |
| 233 | |
Paul Mundt | 6178706 | 2008-08-04 10:58:24 +0900 | [diff] [blame] | 234 | maple_set_drvdata(mdev, NULL); |
| 235 | |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 236 | mutex_unlock(&maple_keyb_mutex); |
Adrian McMenamin | 86d9d32 | 2008-07-30 12:31:38 -0700 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 240 | static struct maple_driver dc_kbd_driver = { |
| 241 | .function = MAPLE_FUNC_KEYBOARD, |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 242 | .drv = { |
| 243 | .name = "Dreamcast_keyboard", |
| 244 | .probe = probe_maple_kbd, |
Adrian McMenamin | 86d9d32 | 2008-07-30 12:31:38 -0700 | [diff] [blame] | 245 | .remove = remove_maple_kbd, |
Paul Mundt | 6387029 | 2008-08-04 10:39:46 +0900 | [diff] [blame] | 246 | }, |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | static int __init dc_kbd_init(void) |
| 250 | { |
Paul Mundt | 6387029 | 2008-08-04 10:39:46 +0900 | [diff] [blame] | 251 | return maple_driver_register(&dc_kbd_driver); |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static void __exit dc_kbd_exit(void) |
| 255 | { |
Paul Mundt | 6387029 | 2008-08-04 10:39:46 +0900 | [diff] [blame] | 256 | maple_driver_unregister(&dc_kbd_driver); |
Adrian McMenamin | b11d212 | 2007-10-12 20:36:13 -0400 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | module_init(dc_kbd_init); |
| 260 | module_exit(dc_kbd_exit); |