Path: blob/master/drivers/input/joystick/maplecontrol.c
15109 views
/*1* SEGA Dreamcast controller driver2* Based on drivers/usb/iforce.c3*4* Copyright Yaegashi Takeshi, 20015* Adrian McMenamin, 2008 - 20096*/78#include <linux/kernel.h>9#include <linux/slab.h>10#include <linux/input.h>11#include <linux/module.h>12#include <linux/init.h>13#include <linux/timer.h>14#include <linux/maple.h>1516MODULE_AUTHOR("Adrian McMenamin <[email protected]>");17MODULE_DESCRIPTION("SEGA Dreamcast controller driver");18MODULE_LICENSE("GPL");1920struct dc_pad {21struct input_dev *dev;22struct maple_device *mdev;23};2425static void dc_pad_callback(struct mapleq *mq)26{27unsigned short buttons;28struct maple_device *mapledev = mq->dev;29struct dc_pad *pad = maple_get_drvdata(mapledev);30struct input_dev *dev = pad->dev;31unsigned char *res = mq->recvbuf->buf;3233buttons = ~le16_to_cpup((__le16 *)(res + 8));3435input_report_abs(dev, ABS_HAT0Y,36(buttons & 0x0010 ? -1 : 0) + (buttons & 0x0020 ? 1 : 0));37input_report_abs(dev, ABS_HAT0X,38(buttons & 0x0040 ? -1 : 0) + (buttons & 0x0080 ? 1 : 0));39input_report_abs(dev, ABS_HAT1Y,40(buttons & 0x1000 ? -1 : 0) + (buttons & 0x2000 ? 1 : 0));41input_report_abs(dev, ABS_HAT1X,42(buttons & 0x4000 ? -1 : 0) + (buttons & 0x8000 ? 1 : 0));4344input_report_key(dev, BTN_C, buttons & 0x0001);45input_report_key(dev, BTN_B, buttons & 0x0002);46input_report_key(dev, BTN_A, buttons & 0x0004);47input_report_key(dev, BTN_START, buttons & 0x0008);48input_report_key(dev, BTN_Z, buttons & 0x0100);49input_report_key(dev, BTN_Y, buttons & 0x0200);50input_report_key(dev, BTN_X, buttons & 0x0400);51input_report_key(dev, BTN_SELECT, buttons & 0x0800);5253input_report_abs(dev, ABS_GAS, res[10]);54input_report_abs(dev, ABS_BRAKE, res[11]);55input_report_abs(dev, ABS_X, res[12]);56input_report_abs(dev, ABS_Y, res[13]);57input_report_abs(dev, ABS_RX, res[14]);58input_report_abs(dev, ABS_RY, res[15]);59}6061static int dc_pad_open(struct input_dev *dev)62{63struct dc_pad *pad = dev->dev.platform_data;6465maple_getcond_callback(pad->mdev, dc_pad_callback, HZ/20,66MAPLE_FUNC_CONTROLLER);6768return 0;69}7071static void dc_pad_close(struct input_dev *dev)72{73struct dc_pad *pad = dev->dev.platform_data;7475maple_getcond_callback(pad->mdev, dc_pad_callback, 0,76MAPLE_FUNC_CONTROLLER);77}7879/* allow the controller to be used */80static int __devinit probe_maple_controller(struct device *dev)81{82static const short btn_bit[32] = {83BTN_C, BTN_B, BTN_A, BTN_START, -1, -1, -1, -1,84BTN_Z, BTN_Y, BTN_X, BTN_SELECT, -1, -1, -1, -1,85-1, -1, -1, -1, -1, -1, -1, -1,86-1, -1, -1, -1, -1, -1, -1, -1,87};8889static const short abs_bit[32] = {90-1, -1, -1, -1, ABS_HAT0Y, ABS_HAT0Y, ABS_HAT0X, ABS_HAT0X,91-1, -1, -1, -1, ABS_HAT1Y, ABS_HAT1Y, ABS_HAT1X, ABS_HAT1X,92ABS_GAS, ABS_BRAKE, ABS_X, ABS_Y, ABS_RX, ABS_RY, -1, -1,93-1, -1, -1, -1, -1, -1, -1, -1,94};9596struct maple_device *mdev = to_maple_dev(dev);97struct maple_driver *mdrv = to_maple_driver(dev->driver);98int i, error;99struct dc_pad *pad;100struct input_dev *idev;101unsigned long data = be32_to_cpu(mdev->devinfo.function_data[0]);102103pad = kzalloc(sizeof(struct dc_pad), GFP_KERNEL);104idev = input_allocate_device();105if (!pad || !idev) {106error = -ENOMEM;107goto fail;108}109110pad->dev = idev;111pad->mdev = mdev;112113idev->open = dc_pad_open;114idev->close = dc_pad_close;115116for (i = 0; i < 32; i++) {117if (data & (1 << i)) {118if (btn_bit[i] >= 0)119__set_bit(btn_bit[i], idev->keybit);120else if (abs_bit[i] >= 0)121__set_bit(abs_bit[i], idev->absbit);122}123}124125if (idev->keybit[BIT_WORD(BTN_JOYSTICK)])126idev->evbit[0] |= BIT_MASK(EV_KEY);127128if (idev->absbit[0])129idev->evbit[0] |= BIT_MASK(EV_ABS);130131for (i = ABS_X; i <= ABS_BRAKE; i++)132input_set_abs_params(idev, i, 0, 255, 0, 0);133134for (i = ABS_HAT0X; i <= ABS_HAT3Y; i++)135input_set_abs_params(idev, i, 1, -1, 0, 0);136137idev->dev.platform_data = pad;138idev->dev.parent = &mdev->dev;139idev->name = mdev->product_name;140idev->id.bustype = BUS_HOST;141input_set_drvdata(idev, pad);142143error = input_register_device(idev);144if (error)145goto fail;146147mdev->driver = mdrv;148maple_set_drvdata(mdev, pad);149150return 0;151152fail:153input_free_device(idev);154kfree(pad);155maple_set_drvdata(mdev, NULL);156return error;157}158159static int __devexit remove_maple_controller(struct device *dev)160{161struct maple_device *mdev = to_maple_dev(dev);162struct dc_pad *pad = maple_get_drvdata(mdev);163164mdev->callback = NULL;165input_unregister_device(pad->dev);166maple_set_drvdata(mdev, NULL);167kfree(pad);168169return 0;170}171172static struct maple_driver dc_pad_driver = {173.function = MAPLE_FUNC_CONTROLLER,174.drv = {175.name = "Dreamcast_controller",176.probe = probe_maple_controller,177.remove = __devexit_p(remove_maple_controller),178},179};180181static int __init dc_pad_init(void)182{183return maple_driver_register(&dc_pad_driver);184}185186static void __exit dc_pad_exit(void)187{188maple_driver_unregister(&dc_pad_driver);189}190191module_init(dc_pad_init);192module_exit(dc_pad_exit);193194195