Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Apple Motion Sensor driver (joystick emulation) |
| 4 | * |
| 5 | * Copyright (C) 2005 Stelian Pop (stelian@popies.net) |
| 6 | * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch) |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/delay.h> |
| 15 | |
| 16 | #include "ams.h" |
| 17 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 18 | static bool joystick; |
Dmitry Torokhov | 0a02002 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 19 | module_param(joystick, bool, S_IRUGO); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 20 | MODULE_PARM_DESC(joystick, "Enable the input class device on module load"); |
| 21 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 22 | static bool invert; |
Dmitry Torokhov | 0a02002 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 23 | module_param(invert, bool, S_IWUSR | S_IRUGO); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 24 | MODULE_PARM_DESC(invert, "Invert input data on X and Y axis"); |
| 25 | |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 26 | static DEFINE_MUTEX(ams_input_mutex); |
| 27 | |
Dmitry Torokhov | 0c444d9 | 2019-10-02 14:48:54 -0700 | [diff] [blame] | 28 | static void ams_idev_poll(struct input_dev *idev) |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 29 | { |
| 30 | s8 x, y, z; |
| 31 | |
Dmitry Torokhov | 3fdbc34 | 2007-09-26 00:01:41 -0400 | [diff] [blame] | 32 | mutex_lock(&ams_info.lock); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 33 | |
Dmitry Torokhov | 3fdbc34 | 2007-09-26 00:01:41 -0400 | [diff] [blame] | 34 | ams_sensors(&x, &y, &z); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 35 | |
Dmitry Torokhov | 3fdbc34 | 2007-09-26 00:01:41 -0400 | [diff] [blame] | 36 | x -= ams_info.xcalib; |
| 37 | y -= ams_info.ycalib; |
| 38 | z -= ams_info.zcalib; |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 39 | |
Dmitry Torokhov | 3fdbc34 | 2007-09-26 00:01:41 -0400 | [diff] [blame] | 40 | input_report_abs(idev, ABS_X, invert ? -x : x); |
| 41 | input_report_abs(idev, ABS_Y, invert ? -y : y); |
| 42 | input_report_abs(idev, ABS_Z, z); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 43 | |
Dmitry Torokhov | 3fdbc34 | 2007-09-26 00:01:41 -0400 | [diff] [blame] | 44 | input_sync(idev); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 45 | |
Dmitry Torokhov | 3fdbc34 | 2007-09-26 00:01:41 -0400 | [diff] [blame] | 46 | mutex_unlock(&ams_info.lock); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /* Call with ams_info.lock held! */ |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 50 | static int ams_input_enable(void) |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 51 | { |
Dmitry Torokhov | 3fdbc34 | 2007-09-26 00:01:41 -0400 | [diff] [blame] | 52 | struct input_dev *input; |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 53 | s8 x, y, z; |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 54 | int error; |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 55 | |
| 56 | ams_sensors(&x, &y, &z); |
| 57 | ams_info.xcalib = x; |
| 58 | ams_info.ycalib = y; |
| 59 | ams_info.zcalib = z; |
| 60 | |
Dmitry Torokhov | 0c444d9 | 2019-10-02 14:48:54 -0700 | [diff] [blame] | 61 | input = input_allocate_device(); |
| 62 | if (!input) |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 63 | return -ENOMEM; |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 64 | |
Dmitry Torokhov | 3fdbc34 | 2007-09-26 00:01:41 -0400 | [diff] [blame] | 65 | input->name = "Apple Motion Sensor"; |
| 66 | input->id.bustype = ams_info.bustype; |
| 67 | input->id.vendor = 0; |
| 68 | input->dev.parent = &ams_info.of_dev->dev; |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 69 | |
Dmitry Torokhov | 3fdbc34 | 2007-09-26 00:01:41 -0400 | [diff] [blame] | 70 | input_set_abs_params(input, ABS_X, -50, 50, 3, 0); |
| 71 | input_set_abs_params(input, ABS_Y, -50, 50, 3, 0); |
| 72 | input_set_abs_params(input, ABS_Z, -50, 50, 3, 0); |
Dmitry Torokhov | 0c444d9 | 2019-10-02 14:48:54 -0700 | [diff] [blame] | 73 | input_set_capability(input, EV_KEY, BTN_TOUCH); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 74 | |
Dmitry Torokhov | 0c444d9 | 2019-10-02 14:48:54 -0700 | [diff] [blame] | 75 | error = input_setup_polling(input, ams_idev_poll); |
| 76 | if (error) |
| 77 | goto err_free_input; |
Dmitry Torokhov | 3fdbc34 | 2007-09-26 00:01:41 -0400 | [diff] [blame] | 78 | |
Dmitry Torokhov | 0c444d9 | 2019-10-02 14:48:54 -0700 | [diff] [blame] | 79 | input_set_poll_interval(input, 25); |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 80 | |
Dmitry Torokhov | 0c444d9 | 2019-10-02 14:48:54 -0700 | [diff] [blame] | 81 | error = input_register_device(input); |
| 82 | if (error) |
| 83 | goto err_free_input; |
| 84 | |
| 85 | ams_info.idev = input; |
Gustavo A. R. Silva | 516c32f | 2018-01-23 19:48:46 -0600 | [diff] [blame] | 86 | joystick = true; |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 87 | |
| 88 | return 0; |
Dmitry Torokhov | 0c444d9 | 2019-10-02 14:48:54 -0700 | [diff] [blame] | 89 | |
| 90 | err_free_input: |
| 91 | input_free_device(input); |
| 92 | return error; |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 95 | static void ams_input_disable(void) |
| 96 | { |
| 97 | if (ams_info.idev) { |
Dmitry Torokhov | 0c444d9 | 2019-10-02 14:48:54 -0700 | [diff] [blame] | 98 | input_unregister_device(ams_info.idev); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 99 | ams_info.idev = NULL; |
| 100 | } |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 101 | |
Gustavo A. R. Silva | 516c32f | 2018-01-23 19:48:46 -0600 | [diff] [blame] | 102 | joystick = false; |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static ssize_t ams_input_show_joystick(struct device *dev, |
| 106 | struct device_attribute *attr, char *buf) |
| 107 | { |
| 108 | return sprintf(buf, "%d\n", joystick); |
| 109 | } |
| 110 | |
| 111 | static ssize_t ams_input_store_joystick(struct device *dev, |
| 112 | struct device_attribute *attr, const char *buf, size_t count) |
| 113 | { |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 114 | unsigned long enable; |
| 115 | int error = 0; |
Jingoo Han | 7033b64 | 2013-07-19 16:16:56 +0900 | [diff] [blame] | 116 | int ret; |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 117 | |
Jingoo Han | 7033b64 | 2013-07-19 16:16:56 +0900 | [diff] [blame] | 118 | ret = kstrtoul(buf, 0, &enable); |
| 119 | if (ret) |
| 120 | return ret; |
| 121 | if (enable > 1) |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 122 | return -EINVAL; |
| 123 | |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 124 | mutex_lock(&ams_input_mutex); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 125 | |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 126 | if (enable != joystick) { |
| 127 | if (enable) |
| 128 | error = ams_input_enable(); |
| 129 | else |
| 130 | ams_input_disable(); |
| 131 | } |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 132 | |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 133 | mutex_unlock(&ams_input_mutex); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 134 | |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 135 | return error ? error : count; |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR, |
| 139 | ams_input_show_joystick, ams_input_store_joystick); |
| 140 | |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 141 | int ams_input_init(void) |
| 142 | { |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 143 | if (joystick) |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 144 | ams_input_enable(); |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 145 | |
| 146 | return device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Al Viro | d7bde2f | 2007-02-09 16:38:50 +0000 | [diff] [blame] | 149 | void ams_input_exit(void) |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 150 | { |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 151 | device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick); |
Dmitry Torokhov | ee4cd32 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 152 | |
| 153 | mutex_lock(&ams_input_mutex); |
| 154 | ams_input_disable(); |
| 155 | mutex_unlock(&ams_input_mutex); |
Stelian Pop | dcb69dd | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 156 | } |