PageRenderTime 19ms CodeModel.GetById 10ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/input/mouse/maplemouse.c

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