2007-09-04 23:34:41

by Adrian McMenamin

[permalink] [raw]
Subject: [PATCH] Add support for keyboard on SEGA Dreamcast

This patch will add support for the Dreamcast keyboard when used
alongside the maple bus patch (http://lkml.org/lkml/2007/9/4/165) and
the pvr2 patch.

Signed off by: Adrian McMenamin <[email protected]>

diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index c97d5eb..1689f73 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -253,4 +253,14 @@ config KEYBOARD_GPIO
To compile this driver as a module, choose M here: the
module will be called gpio-keys.

+
+config KEYBOARD_MAPLE
+ tristate "Maple bus keyboard"
+ depends on SH_DREAMCAST && MAPLE
+ help
+ Say Y here if you have a DreamCast console running Linux and have
+ a keyboard attached to its Maple bus.
+
+ To compile this driver as a module, choose M here: the
+ module will be called maple_keyb.
endif
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index 28d211b..3f775ed 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -21,4 +21,5 @@ obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o
obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keyboard.o
obj-$(CONFIG_KEYBOARD_AAED2000) += aaed2000_kbd.o
obj-$(CONFIG_KEYBOARD_GPIO) += gpio_keys.o
+obj-$(CONFIG_KEYBOARD_MAPLE) += maple_keyb.o

diff --git a/drivers/input/keyboard/maple_keyb.c
b/drivers/input/keyboard/maple_keyb.c
new file mode 100644
index 0000000..12e4692
--- /dev/null
+++ b/drivers/input/keyboard/maple_keyb.c
@@ -0,0 +1,222 @@
+/*
+ * SEGA Dreamcast keyboard driver
+ * Based on drivers/usb/usbkbd.c
+ * Copyright YAEGASHI Takeshi, 2001
+ * Porting to 2.6 Copyright Adrian McMenamin, 2007
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see the file COPYING, or write
+ * to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/input.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/timer.h>
+#include <linux/maple.h>
+
+MODULE_AUTHOR("YAEGASHI Takeshi, Adrian McMenamin");
+MODULE_DESCRIPTION("SEGA Dreamcast keyboard driver");
+MODULE_LICENSE("GPL");
+
+static unsigned char dc_kbd_keycode[256] = {
+ 0, 0, 0, 0, 30, 48, 46, 32,
+ 18, 33, 34, 35, 23, 36, 37, 38,
+ 50, 49, 24, 25, 16, 19, 31, 20,
+ 22, 47, 17, 45, 21, 44, 2, 3,
+ 4, 5, 6, 7, 8, 9, 10, 11, 28,
+ 1, 14, 15, 57, 12, 13, 26,
+ 27, 43, 43, 39, 40, 41, 51,
+ 52, 53, 58, 59, 60, 61, 62, 63, 64,
+ 65, 66, 67, 68, 87, 88, 99,
+ 70, 119, 110, 102, 104, 111,
+ 107, 109, 106, 105, 108, 103,
+ 69, 98, 55, 74, 78, 96, 79, 80,
+ 81, 75, 76, 77, 71, 72, 73, 82, 83,
+ 86, 127, 116, 117, 183, 184, 185,
+ 186, 187, 188, 189, 190,
+ 191, 192, 193, 194, 134, 138, 130, 132,
+ 128, 129, 131, 137, 133, 135, 136, 113,
+ 115, 114, 0, 0, 0, 121, 0, 89, 93, 124,
+ 92, 94, 95, 0, 0, 0,
+ 122, 123, 90, 91, 85, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 29, 42, 56, 125, 97, 54, 100, 126,
+ 164, 166, 165, 163, 161, 115, 114, 113,
+ 150, 158, 159, 128, 136, 177, 178, 176, 142,
+ 152, 173, 140
+};
+
+struct dc_kbd {
+ struct input_dev *dev;
+ unsigned char new[8];
+ unsigned char old[8];
+};
+
+static void dc_scan_kbd(struct dc_kbd *kbd)
+{
+ int i;
+ struct input_dev *dev = kbd->dev;
+ for (i = 0; i < 8; i++)
+ input_report_key(dev,
+ dc_kbd_keycode[i + 224],
+ (kbd->new[0] >> i) & 1);
+
+ for (i = 2; i < 8; i++) {
+ if (kbd->old[i] > 3
+ && memchr(kbd->new + 2, kbd->old[i], 6) == NULL) {
+ if (dc_kbd_keycode[kbd->old[i]])
+ input_report_key(dev,
+ dc_kbd_keycode[kbd->old[i]], 0);
+ else
+ printk
+ ("Unknown key (scancode %#x) released.",
+ kbd->old[i]);
+ }
+
+ if (kbd->new[i] > 3
+ && memchr(kbd->old + 2, kbd->new[i], 6) != NULL) {
+ if (dc_kbd_keycode[kbd->new[i]])
+ input_report_key(dev,
+ dc_kbd_keycode[kbd->new[i]], 1);
+ else
+ printk
+ ("Unknown key (scancode %#x) pressed.",
+ kbd->new[i]);
+ }
+ }
+ input_sync(dev);
+ memcpy(kbd->old, kbd->new, 8);
+}
+
+static void dc_kbd_callback(struct mapleq *mq)
+{
+ struct maple_device *mapledev = mq->dev;
+ struct dc_kbd *kbd = mapledev->private_data;
+ unsigned long *buf = mq->recvbuf;
+ if (buf[1] == mapledev->function) {
+ memcpy(kbd->new, buf + 2, 8);
+ dc_scan_kbd(kbd);
+ }
+}
+
+static int dc_kbd_connect(struct maple_device *dev)
+{
+ int i, retval;
+ unsigned long data = be32_to_cpu(dev->devinfo.function_data[0]);
+ struct dc_kbd *kbd;
+ if (dev->function != MAPLE_FUNC_KEYBOARD)
+ return -EINVAL;
+
+ kbd = kzalloc(sizeof(struct dc_kbd), GFP_KERNEL);
+ if (unlikely(!kbd))
+ return -ENOMEM;
+
+ dev->private_data = kbd;
+ kbd->dev = input_allocate_device();
+ if (unlikely(!kbd->dev))
+ goto cleanup;
+
+ kbd->dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
+
+ for (i = 0; i < 255; i++)
+ set_bit(dc_kbd_keycode[i], kbd->dev->keybit);
+
+ clear_bit(0, kbd->dev->keybit);
+
+ kbd->dev->private = kbd;
+
+ kbd->dev->name = dev->product_name;
+ kbd->dev->id.bustype = BUS_MAPLE;
+
+ retval = input_register_device(kbd->dev);
+ if (unlikely(retval))
+ goto cleanup;
+
+ maple_getcond_callback(dev, dc_kbd_callback, (25 * HZ) / 1000,
+ MAPLE_FUNC_KEYBOARD);
+
+ printk(KERN_INFO "input: keyboard(0x%lx): %s\n", data,
+ kbd->dev->name);
+
+ return 0;
+
+ cleanup:
+ kfree(kbd);
+ return -EINVAL;
+}
+
+static void dc_kbd_disconnect(struct maple_device *dev)
+{
+ struct dc_kbd *kbd = dev->private_data;
+
+ input_unregister_device(kbd->dev);
+ kfree(kbd);
+}
+
+/* allow the keyboard to be used */
+static int probe_maple_kbd(struct device *dev)
+{
+ struct maple_device *mdev;
+ struct maple_driver *mdrv;
+ int proberes;
+
+ mdev = to_maple_dev(dev);
+ mdrv = to_maple_driver(dev->driver);
+
+ proberes = dc_kbd_connect(mdev);
+ if (unlikely(proberes))
+ return proberes;
+ mdev->driver = mdrv;
+ mdev->registered = 1;
+ return 0;
+}
+
+
+static struct maple_driver dc_kbd_driver = {
+ .function = MAPLE_FUNC_KEYBOARD,
+ .connect = dc_kbd_connect,
+ .disconnect = dc_kbd_disconnect,
+ .drv = {
+ .name = "Dreamcast_keyboard",
+ .probe = probe_maple_kbd,
+ },
+};
+
+static int __init dc_kbd_init(void)
+{
+ int retval;
+ retval = maple_driver_register(&dc_kbd_driver.drv);
+ if (retval)
+ return retval;
+ return 0;
+}
+
+static void __exit dc_kbd_exit(void)
+{
+ driver_unregister(&dc_kbd_driver.drv);
+}
+
+module_init(dc_kbd_init);
+module_exit(dc_kbd_exit);
diff --git a/include/linux/input.h b/include/linux/input.h
index e02c6a6..2e65c88 100644
--- a/include/linux/input.h
+++ b/include/linux/input.h
@@ -688,6 +688,7 @@ struct input_absinfo {
#define BUS_HOST 0x19
#define BUS_GSC 0x1A
#define BUS_ATARI 0x1B
+#define BUS_MAPLE 0x1C

/*
* Values describing the status of a force-feedback effect


2007-09-05 03:14:51

by Dmitry Torokhov

[permalink] [raw]
Subject: Re: [PATCH] Add support for keyboard on SEGA Dreamcast

Hi Adrian,

On Tuesday 04 September 2007 19:34, Adrian McMenamin wrote:
> This patch will add support for the Dreamcast keyboard when used
> alongside the maple bus patch (http://lkml.org/lkml/2007/9/4/165) and
> the pvr2 patch.
>
> Signed off by: Adrian McMenamin <[email protected]>
>

Thnank you very much for your patch. Couple of comments:

> +
> +static unsigned char dc_kbd_keycode[256] = {

Const?

> +
> +static int dc_kbd_connect(struct maple_device *dev)
> +{
> + int i, retval;
> + unsigned long data = be32_to_cpu(dev->devinfo.function_data[0]);
> + struct dc_kbd *kbd;
> + if (dev->function != MAPLE_FUNC_KEYBOARD)
> + return -EINVAL;
> +
> + kbd = kzalloc(sizeof(struct dc_kbd), GFP_KERNEL);
> + if (unlikely(!kbd))
> + return -ENOMEM;
> +
> + dev->private_data = kbd;
> + kbd->dev = input_allocate_device();
> + if (unlikely(!kbd->dev))
> + goto cleanup;
> +
> + kbd->dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
> +
> + for (i = 0; i < 255; i++)
> + set_bit(dc_kbd_keycode[i], kbd->dev->keybit);
> +
> + clear_bit(0, kbd->dev->keybit);
> +
> + kbd->dev->private = kbd;
> +
> + kbd->dev->name = dev->product_name;
> + kbd->dev->id.bustype = BUS_MAPLE;

Do we really need a new bus type? Would not BUS_HOST suffice?

> +
> + retval = input_register_device(kbd->dev);
> + if (unlikely(retval))
> + goto cleanup;
> +
> + maple_getcond_callback(dev, dc_kbd_callback, (25 * HZ) / 1000,
> + MAPLE_FUNC_KEYBOARD);
> +
> + printk(KERN_INFO "input: keyboard(0x%lx): %s\n", data,
> + kbd->dev->name);

Input core already prints a line when a new input device is registered,
do we need another one?

> +
> + return 0;
> +
> + cleanup:
> + kfree(kbd);
> + return -EINVAL;

It loks like call to input_free_device() is missing here. You would leak
memory if input_register_device would fail.

I would also get rid of all likelys/unlikelys here - driver registration is
not a hot path...

> +}
> +
> +static void dc_kbd_disconnect(struct maple_device *dev)
> +{
> + struct dc_kbd *kbd = dev->private_data;
> +
> + input_unregister_device(kbd->dev);
> + kfree(kbd);

Are we guaranteed that the dc_kbd_callback is not running in a separate
thread?

Please also consider implementing support for changing keyma. Since
the keymap is pretty full I think the best way is to copy the vanilla
keymap into a per-device memory and set up keymap, keycodesize and
keycodemax in input device structure.

Thank you.

--
Dmitry

2007-09-05 04:36:11

by Mike Frysinger

[permalink] [raw]
Subject: Re: [PATCH] Add support for keyboard on SEGA Dreamcast

On Tuesday 04 September 2007, Adrian McMenamin wrote:
> --- a/drivers/input/keyboard/Kconfig
> +++ b/drivers/input/keyboard/Kconfig
> + Say Y here if you have a DreamCast console running Linux and have

funny caps in Dreamcast

> --- /dev/null
> +++ b/drivers/input/keyboard/maple_keyb.c
> +static void dc_scan_kbd(struct dc_kbd *kbd)

still some funny wrappings in this func ...

> + printk
> + ("Unknown key (scancode %#x) released.",
> + kbd->old[i]);
> ...
> + printk
> + ("Unknown key (scancode %#x) pressed.",
> + kbd->new[i]);

missing KERN log levels in those printk's

> +static int dc_kbd_connect(struct maple_device *dev)
> +{
> ...
> + struct dc_kbd *kbd;
> ...
> + kbd = kzalloc(sizeof(struct dc_kbd), GFP_KERNEL);

i find this more readable/managable myself:
kbd = kzalloc(*kbd, GFP_KERNEL);

> + kbd->dev = input_allocate_device();
> ...
> + retval = input_register_device(kbd->dev);
> + if (unlikely(retval))
> + goto cleanup;
> ...
> + cleanup:
> + kfree(kbd);
> + return -EINVAL;

i'm not familiar with the input layer, but do you need to deallocate that
input device if the register fails ? if so, i guess dc_kbd_disconnect()
would need tweaking too ...
-mike


Attachments:
(No filename) (1.20 kB)
signature.asc (827.00 B)
This is a digitally signed message part.
Download all attachments

2007-09-05 04:50:28

by Dmitry Torokhov

[permalink] [raw]
Subject: Re: [PATCH] Add support for keyboard on SEGA Dreamcast

Hi Mike,

On Wednesday 05 September 2007 00:34, Mike Frysinger wrote:
> > +     kbd->dev = input_allocate_device();
> > ...
> > +     retval = input_register_device(kbd->dev);
> > +     if (unlikely(retval))
> > +             goto cleanup;
> > ...
> > +      cleanup:
> > +     kfree(kbd);
> > +     return -EINVAL;
>
> i'm not familiar with the input layer, but do you need to deallocate that
> input device if the register fails ?  if so, i guess dc_kbd_disconnect()
> would need tweaking too ...

No, dc_kbd_disconnect() is fine - the structure is refcounted and so
input core will free it when the last reference drops. But you are
right, input_free_device() is still needed in error path.

--
Dmitry

2007-09-07 22:01:49

by Adrian McMenamin

[permalink] [raw]
Subject: Re: [PATCH] Add support for keyboard on SEGA Dreamcast

On 05/09/07, Dmitry Torokhov <[email protected]> wrote:

>
> Are we guaranteed that the dc_kbd_callback is not running in a separate
> thread?
>
> Please also consider implementing support for changing keyma. Since
> the keymap is pretty full I think the best way is to copy the vanilla
> keymap into a per-device memory and set up keymap, keycodesize and
> keycodemax in input device structure.
>
> Thank you.
>
> --
> Dmitry
>

Dmitry - have now worked out at least one alternative keymap - for
European keyboards. But could you explain the above point? I have to
admit I am not too familiar with the input layer - having really taken
an old, and bit rot infested 2.4 driver and brought it up to standard
for 2.6 - and so I don't follow this point too well!

Can I treat keymaps as firmware and load from userspace? Or is there a
way of making this completely user configurable at runtime?

Any help from you, or any other reader, much appreciated.

Adrian