Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Apple Motion Sensor driver (joystick emulation)
0004  *
0005  * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
0006  * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
0007  */
0008 
0009 #include <linux/module.h>
0010 
0011 #include <linux/types.h>
0012 #include <linux/errno.h>
0013 #include <linux/init.h>
0014 #include <linux/delay.h>
0015 
0016 #include "ams.h"
0017 
0018 static bool joystick;
0019 module_param(joystick, bool, S_IRUGO);
0020 MODULE_PARM_DESC(joystick, "Enable the input class device on module load");
0021 
0022 static bool invert;
0023 module_param(invert, bool, S_IWUSR | S_IRUGO);
0024 MODULE_PARM_DESC(invert, "Invert input data on X and Y axis");
0025 
0026 static DEFINE_MUTEX(ams_input_mutex);
0027 
0028 static void ams_idev_poll(struct input_dev *idev)
0029 {
0030     s8 x, y, z;
0031 
0032     mutex_lock(&ams_info.lock);
0033 
0034     ams_sensors(&x, &y, &z);
0035 
0036     x -= ams_info.xcalib;
0037     y -= ams_info.ycalib;
0038     z -= ams_info.zcalib;
0039 
0040     input_report_abs(idev, ABS_X, invert ? -x : x);
0041     input_report_abs(idev, ABS_Y, invert ? -y : y);
0042     input_report_abs(idev, ABS_Z, z);
0043 
0044     input_sync(idev);
0045 
0046     mutex_unlock(&ams_info.lock);
0047 }
0048 
0049 /* Call with ams_info.lock held! */
0050 static int ams_input_enable(void)
0051 {
0052     struct input_dev *input;
0053     s8 x, y, z;
0054     int error;
0055 
0056     ams_sensors(&x, &y, &z);
0057     ams_info.xcalib = x;
0058     ams_info.ycalib = y;
0059     ams_info.zcalib = z;
0060 
0061     input = input_allocate_device();
0062     if (!input)
0063         return -ENOMEM;
0064 
0065     input->name = "Apple Motion Sensor";
0066     input->id.bustype = ams_info.bustype;
0067     input->id.vendor = 0;
0068     input->dev.parent = &ams_info.of_dev->dev;
0069 
0070     input_set_abs_params(input, ABS_X, -50, 50, 3, 0);
0071     input_set_abs_params(input, ABS_Y, -50, 50, 3, 0);
0072     input_set_abs_params(input, ABS_Z, -50, 50, 3, 0);
0073     input_set_capability(input, EV_KEY, BTN_TOUCH);
0074 
0075     error = input_setup_polling(input, ams_idev_poll);
0076     if (error)
0077         goto err_free_input;
0078 
0079     input_set_poll_interval(input, 25);
0080 
0081     error = input_register_device(input);
0082     if (error)
0083         goto err_free_input;
0084 
0085     ams_info.idev = input;
0086     joystick = true;
0087 
0088     return 0;
0089 
0090 err_free_input:
0091     input_free_device(input);
0092     return error;
0093 }
0094 
0095 static void ams_input_disable(void)
0096 {
0097     if (ams_info.idev) {
0098         input_unregister_device(ams_info.idev);
0099         ams_info.idev = NULL;
0100     }
0101 
0102     joystick = false;
0103 }
0104 
0105 static ssize_t ams_input_show_joystick(struct device *dev,
0106     struct device_attribute *attr, char *buf)
0107 {
0108     return sprintf(buf, "%d\n", joystick);
0109 }
0110 
0111 static ssize_t ams_input_store_joystick(struct device *dev,
0112     struct device_attribute *attr, const char *buf, size_t count)
0113 {
0114     unsigned long enable;
0115     int error = 0;
0116     int ret;
0117 
0118     ret = kstrtoul(buf, 0, &enable);
0119     if (ret)
0120         return ret;
0121     if (enable > 1)
0122         return -EINVAL;
0123 
0124     mutex_lock(&ams_input_mutex);
0125 
0126     if (enable != joystick) {
0127         if (enable)
0128             error = ams_input_enable();
0129         else
0130             ams_input_disable();
0131     }
0132 
0133     mutex_unlock(&ams_input_mutex);
0134 
0135     return error ? error : count;
0136 }
0137 
0138 static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR,
0139     ams_input_show_joystick, ams_input_store_joystick);
0140 
0141 int ams_input_init(void)
0142 {
0143     if (joystick)
0144         ams_input_enable();
0145 
0146     return device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
0147 }
0148 
0149 void ams_input_exit(void)
0150 {
0151     device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick);
0152 
0153     mutex_lock(&ams_input_mutex);
0154     ams_input_disable();
0155     mutex_unlock(&ams_input_mutex);
0156 }