^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * SEGA Dreamcast mouse driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Based on drivers/usb/usbmouse.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) Yaegashi Takeshi, 2001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) Adrian McMenamin, 2008 - 2009
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/maple.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) MODULE_DESCRIPTION("SEGA Dreamcast mouse driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct dc_mouse {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct maple_device *mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static void dc_mouse_callback(struct mapleq *mq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int buttons, relx, rely, relz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct maple_device *mapledev = mq->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct dc_mouse *mse = maple_get_drvdata(mapledev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct input_dev *dev = mse->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned char *res = mq->recvbuf->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) buttons = ~res[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) relx = *(unsigned short *)(res + 12) - 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) rely = *(unsigned short *)(res + 14) - 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) relz = *(unsigned short *)(res + 16) - 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) input_report_key(dev, BTN_LEFT, buttons & 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) input_report_key(dev, BTN_MIDDLE, buttons & 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) input_report_key(dev, BTN_RIGHT, buttons & 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) input_report_rel(dev, REL_X, relx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) input_report_rel(dev, REL_Y, rely);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) input_report_rel(dev, REL_WHEEL, relz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int dc_mouse_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct dc_mouse *mse = maple_get_drvdata(to_maple_dev(&dev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) maple_getcond_callback(mse->mdev, dc_mouse_callback, HZ/50,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) MAPLE_FUNC_MOUSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static void dc_mouse_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct dc_mouse *mse = maple_get_drvdata(to_maple_dev(&dev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) maple_getcond_callback(mse->mdev, dc_mouse_callback, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) MAPLE_FUNC_MOUSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /* allow the mouse to be used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static int probe_maple_mouse(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct maple_device *mdev = to_maple_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct maple_driver *mdrv = to_maple_driver(dev->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct dc_mouse *mse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) mse = kzalloc(sizeof(struct dc_mouse), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (!mse) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) goto fail_nomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) mse->dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) mse->mdev = mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) BIT_MASK(REL_WHEEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) input_dev->open = dc_mouse_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) input_dev->close = dc_mouse_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) input_dev->name = mdev->product_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) input_dev->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) error = input_register_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) goto fail_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) mdev->driver = mdrv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) maple_set_drvdata(mdev, mse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) fail_register:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) fail_nomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) kfree(mse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int remove_maple_mouse(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct maple_device *mdev = to_maple_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct dc_mouse *mse = maple_get_drvdata(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) mdev->callback = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) input_unregister_device(mse->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) maple_set_drvdata(mdev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) kfree(mse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static struct maple_driver dc_mouse_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .function = MAPLE_FUNC_MOUSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .name = "Dreamcast_mouse",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .probe = probe_maple_mouse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .remove = remove_maple_mouse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int __init dc_mouse_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return maple_driver_register(&dc_mouse_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static void __exit dc_mouse_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) maple_driver_unregister(&dc_mouse_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) module_init(dc_mouse_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) module_exit(dc_mouse_exit);