Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  SEGA Dreamcast controller driver
0004  *  Based on drivers/usb/iforce.c
0005  *
0006  *  Copyright Yaegashi Takeshi, 2001
0007  *  Adrian McMenamin, 2008 - 2009
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/slab.h>
0012 #include <linux/input.h>
0013 #include <linux/module.h>
0014 #include <linux/init.h>
0015 #include <linux/timer.h>
0016 #include <linux/maple.h>
0017 
0018 MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
0019 MODULE_DESCRIPTION("SEGA Dreamcast controller driver");
0020 MODULE_LICENSE("GPL");
0021 
0022 struct dc_pad {
0023     struct input_dev *dev;
0024     struct maple_device *mdev;
0025 };
0026 
0027 static void dc_pad_callback(struct mapleq *mq)
0028 {
0029     unsigned short buttons;
0030     struct maple_device *mapledev = mq->dev;
0031     struct dc_pad *pad = maple_get_drvdata(mapledev);
0032     struct input_dev *dev = pad->dev;
0033     unsigned char *res = mq->recvbuf->buf;
0034 
0035     buttons = ~le16_to_cpup((__le16 *)(res + 8));
0036 
0037     input_report_abs(dev, ABS_HAT0Y,
0038         (buttons & 0x0010 ? -1 : 0) + (buttons & 0x0020 ? 1 : 0));
0039     input_report_abs(dev, ABS_HAT0X,
0040         (buttons & 0x0040 ? -1 : 0) + (buttons & 0x0080 ? 1 : 0));
0041     input_report_abs(dev, ABS_HAT1Y,
0042         (buttons & 0x1000 ? -1 : 0) + (buttons & 0x2000 ? 1 : 0));
0043     input_report_abs(dev, ABS_HAT1X,
0044         (buttons & 0x4000 ? -1 : 0) + (buttons & 0x8000 ? 1 : 0));
0045 
0046     input_report_key(dev, BTN_C,      buttons & 0x0001);
0047     input_report_key(dev, BTN_B,      buttons & 0x0002);
0048     input_report_key(dev, BTN_A,      buttons & 0x0004);
0049     input_report_key(dev, BTN_START,  buttons & 0x0008);
0050     input_report_key(dev, BTN_Z,      buttons & 0x0100);
0051     input_report_key(dev, BTN_Y,      buttons & 0x0200);
0052     input_report_key(dev, BTN_X,      buttons & 0x0400);
0053     input_report_key(dev, BTN_SELECT, buttons & 0x0800);
0054 
0055     input_report_abs(dev, ABS_GAS,    res[10]);
0056     input_report_abs(dev, ABS_BRAKE,  res[11]);
0057     input_report_abs(dev, ABS_X,      res[12]);
0058     input_report_abs(dev, ABS_Y,      res[13]);
0059     input_report_abs(dev, ABS_RX,     res[14]);
0060     input_report_abs(dev, ABS_RY,     res[15]);
0061 }
0062 
0063 static int dc_pad_open(struct input_dev *dev)
0064 {
0065     struct dc_pad *pad = dev_get_platdata(&dev->dev);
0066 
0067     maple_getcond_callback(pad->mdev, dc_pad_callback, HZ/20,
0068         MAPLE_FUNC_CONTROLLER);
0069 
0070     return 0;
0071 }
0072 
0073 static void dc_pad_close(struct input_dev *dev)
0074 {
0075     struct dc_pad *pad = dev_get_platdata(&dev->dev);
0076 
0077     maple_getcond_callback(pad->mdev, dc_pad_callback, 0,
0078         MAPLE_FUNC_CONTROLLER);
0079 }
0080 
0081 /* allow the controller to be used */
0082 static int probe_maple_controller(struct device *dev)
0083 {
0084     static const short btn_bit[32] = {
0085         BTN_C, BTN_B, BTN_A, BTN_START, -1, -1, -1, -1,
0086         BTN_Z, BTN_Y, BTN_X, BTN_SELECT, -1, -1, -1, -1,
0087         -1, -1, -1, -1, -1, -1, -1, -1,
0088         -1, -1, -1, -1, -1, -1, -1, -1,
0089     };
0090 
0091     static const short abs_bit[32] = {
0092         -1, -1, -1, -1, ABS_HAT0Y, ABS_HAT0Y, ABS_HAT0X, ABS_HAT0X,
0093         -1, -1, -1, -1, ABS_HAT1Y, ABS_HAT1Y, ABS_HAT1X, ABS_HAT1X,
0094         ABS_GAS, ABS_BRAKE, ABS_X, ABS_Y, ABS_RX, ABS_RY, -1, -1,
0095         -1, -1, -1, -1, -1, -1, -1, -1,
0096     };
0097 
0098     struct maple_device *mdev = to_maple_dev(dev);
0099     struct maple_driver *mdrv = to_maple_driver(dev->driver);
0100     int i, error;
0101     struct dc_pad *pad;
0102     struct input_dev *idev;
0103     unsigned long data = be32_to_cpu(mdev->devinfo.function_data[0]);
0104 
0105     pad = kzalloc(sizeof(struct dc_pad), GFP_KERNEL);
0106     idev = input_allocate_device();
0107     if (!pad || !idev) {
0108         error = -ENOMEM;
0109         goto fail;
0110     }
0111 
0112     pad->dev = idev;
0113     pad->mdev = mdev;
0114 
0115     idev->open = dc_pad_open;
0116     idev->close = dc_pad_close;
0117 
0118     for (i = 0; i < 32; i++) {
0119         if (data & (1 << i)) {
0120             if (btn_bit[i] >= 0)
0121                 __set_bit(btn_bit[i], idev->keybit);
0122             else if (abs_bit[i] >= 0)
0123                 __set_bit(abs_bit[i], idev->absbit);
0124         }
0125     }
0126 
0127     if (idev->keybit[BIT_WORD(BTN_JOYSTICK)])
0128         idev->evbit[0] |= BIT_MASK(EV_KEY);
0129 
0130     if (idev->absbit[0])
0131         idev->evbit[0] |= BIT_MASK(EV_ABS);
0132 
0133     for (i = ABS_X; i <= ABS_BRAKE; i++)
0134         input_set_abs_params(idev, i, 0, 255, 0, 0);
0135 
0136     for (i = ABS_HAT0X; i <= ABS_HAT3Y; i++)
0137         input_set_abs_params(idev, i, 1, -1, 0, 0);
0138 
0139     idev->dev.platform_data = pad;
0140     idev->dev.parent = &mdev->dev;
0141     idev->name = mdev->product_name;
0142     idev->id.bustype = BUS_HOST;
0143 
0144     error = input_register_device(idev);
0145     if (error)
0146         goto fail;
0147 
0148     mdev->driver = mdrv;
0149     maple_set_drvdata(mdev, pad);
0150 
0151     return 0;
0152 
0153 fail:
0154     input_free_device(idev);
0155     kfree(pad);
0156     maple_set_drvdata(mdev, NULL);
0157     return error;
0158 }
0159 
0160 static int remove_maple_controller(struct device *dev)
0161 {
0162     struct maple_device *mdev = to_maple_dev(dev);
0163     struct dc_pad *pad = maple_get_drvdata(mdev);
0164 
0165     mdev->callback = NULL;
0166     input_unregister_device(pad->dev);
0167     maple_set_drvdata(mdev, NULL);
0168     kfree(pad);
0169 
0170     return 0;
0171 }
0172 
0173 static struct maple_driver dc_pad_driver = {
0174     .function = MAPLE_FUNC_CONTROLLER,
0175     .drv = {
0176         .name   = "Dreamcast_controller",
0177         .probe  = probe_maple_controller,
0178         .remove = remove_maple_controller,
0179     },
0180 };
0181 
0182 static int __init dc_pad_init(void)
0183 {
0184     return maple_driver_register(&dc_pad_driver);
0185 }
0186 
0187 static void __exit dc_pad_exit(void)
0188 {
0189     maple_driver_unregister(&dc_pad_driver);
0190 }
0191 
0192 module_init(dc_pad_init);
0193 module_exit(dc_pad_exit);