Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  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)
 
 
 
 
 
  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
 18static bool joystick;
 19module_param(joystick, bool, S_IRUGO);
 20MODULE_PARM_DESC(joystick, "Enable the input class device on module load");
 21
 22static bool invert;
 23module_param(invert, bool, S_IWUSR | S_IRUGO);
 24MODULE_PARM_DESC(invert, "Invert input data on X and Y axis");
 25
 26static DEFINE_MUTEX(ams_input_mutex);
 27
 28static void ams_idev_poll(struct input_dev *idev)
 29{
 
 30	s8 x, y, z;
 31
 32	mutex_lock(&ams_info.lock);
 33
 34	ams_sensors(&x, &y, &z);
 35
 36	x -= ams_info.xcalib;
 37	y -= ams_info.ycalib;
 38	z -= ams_info.zcalib;
 39
 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);
 43
 44	input_sync(idev);
 45
 46	mutex_unlock(&ams_info.lock);
 47}
 48
 49/* Call with ams_info.lock held! */
 50static int ams_input_enable(void)
 51{
 52	struct input_dev *input;
 53	s8 x, y, z;
 54	int error;
 55
 56	ams_sensors(&x, &y, &z);
 57	ams_info.xcalib = x;
 58	ams_info.ycalib = y;
 59	ams_info.zcalib = z;
 60
 61	input = input_allocate_device();
 62	if (!input)
 63		return -ENOMEM;
 64
 
 
 
 
 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;
 69
 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);
 73	input_set_capability(input, EV_KEY, BTN_TOUCH);
 74
 75	error = input_setup_polling(input, ams_idev_poll);
 76	if (error)
 77		goto err_free_input;
 78
 79	input_set_poll_interval(input, 25);
 80
 81	error = input_register_device(input);
 82	if (error)
 83		goto err_free_input;
 
 
 
 
 
 
 
 84
 85	ams_info.idev = input;
 86	joystick = true;
 87
 88	return 0;
 89
 90err_free_input:
 91	input_free_device(input);
 92	return error;
 93}
 94
 95static void ams_input_disable(void)
 96{
 97	if (ams_info.idev) {
 98		input_unregister_device(ams_info.idev);
 
 99		ams_info.idev = NULL;
100	}
101
102	joystick = false;
103}
104
105static 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
111static ssize_t ams_input_store_joystick(struct device *dev,
112	struct device_attribute *attr, const char *buf, size_t count)
113{
114	unsigned long enable;
115	int error = 0;
116	int ret;
117
118	ret = kstrtoul(buf, 0, &enable);
119	if (ret)
120		return ret;
121	if (enable > 1)
122		return -EINVAL;
123
124	mutex_lock(&ams_input_mutex);
125
126	if (enable != joystick) {
127		if (enable)
128			error = ams_input_enable();
129		else
130			ams_input_disable();
131	}
132
133	mutex_unlock(&ams_input_mutex);
134
135	return error ? error : count;
136}
137
138static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR,
139	ams_input_show_joystick, ams_input_store_joystick);
140
141int ams_input_init(void)
142{
143	if (joystick)
144		ams_input_enable();
145
146	return device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
147}
148
149void ams_input_exit(void)
150{
151	device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick);
152
153	mutex_lock(&ams_input_mutex);
154	ams_input_disable();
155	mutex_unlock(&ams_input_mutex);
156}
v3.1
 
  1/*
  2 * Apple Motion Sensor driver (joystick emulation)
  3 *
  4 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
  5 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License as published by
  9 * the Free Software Foundation; either version 2 of the License, or
 10 * (at your option) any later version.
 11 */
 12
 13#include <linux/module.h>
 14
 15#include <linux/types.h>
 16#include <linux/errno.h>
 17#include <linux/init.h>
 18#include <linux/delay.h>
 19
 20#include "ams.h"
 21
 22static unsigned int joystick;
 23module_param(joystick, bool, S_IRUGO);
 24MODULE_PARM_DESC(joystick, "Enable the input class device on module load");
 25
 26static unsigned int invert;
 27module_param(invert, bool, S_IWUSR | S_IRUGO);
 28MODULE_PARM_DESC(invert, "Invert input data on X and Y axis");
 29
 30static DEFINE_MUTEX(ams_input_mutex);
 31
 32static void ams_idev_poll(struct input_polled_dev *dev)
 33{
 34	struct input_dev *idev = dev->input;
 35	s8 x, y, z;
 36
 37	mutex_lock(&ams_info.lock);
 38
 39	ams_sensors(&x, &y, &z);
 40
 41	x -= ams_info.xcalib;
 42	y -= ams_info.ycalib;
 43	z -= ams_info.zcalib;
 44
 45	input_report_abs(idev, ABS_X, invert ? -x : x);
 46	input_report_abs(idev, ABS_Y, invert ? -y : y);
 47	input_report_abs(idev, ABS_Z, z);
 48
 49	input_sync(idev);
 50
 51	mutex_unlock(&ams_info.lock);
 52}
 53
 54/* Call with ams_info.lock held! */
 55static int ams_input_enable(void)
 56{
 57	struct input_dev *input;
 58	s8 x, y, z;
 59	int error;
 60
 61	ams_sensors(&x, &y, &z);
 62	ams_info.xcalib = x;
 63	ams_info.ycalib = y;
 64	ams_info.zcalib = z;
 65
 66	ams_info.idev = input_allocate_polled_device();
 67	if (!ams_info.idev)
 68		return -ENOMEM;
 69
 70	ams_info.idev->poll = ams_idev_poll;
 71	ams_info.idev->poll_interval = 25;
 72
 73	input = ams_info.idev->input;
 74	input->name = "Apple Motion Sensor";
 75	input->id.bustype = ams_info.bustype;
 76	input->id.vendor = 0;
 77	input->dev.parent = &ams_info.of_dev->dev;
 78
 79	input_set_abs_params(input, ABS_X, -50, 50, 3, 0);
 80	input_set_abs_params(input, ABS_Y, -50, 50, 3, 0);
 81	input_set_abs_params(input, ABS_Z, -50, 50, 3, 0);
 
 
 
 
 
 
 
 82
 83	set_bit(EV_ABS, input->evbit);
 84	set_bit(EV_KEY, input->evbit);
 85	set_bit(BTN_TOUCH, input->keybit);
 86
 87	error = input_register_polled_device(ams_info.idev);
 88	if (error) {
 89		input_free_polled_device(ams_info.idev);
 90		ams_info.idev = NULL;
 91		return error;
 92	}
 93
 94	joystick = 1;
 
 95
 96	return 0;
 
 
 
 
 97}
 98
 99static void ams_input_disable(void)
100{
101	if (ams_info.idev) {
102		input_unregister_polled_device(ams_info.idev);
103		input_free_polled_device(ams_info.idev);
104		ams_info.idev = NULL;
105	}
106
107	joystick = 0;
108}
109
110static ssize_t ams_input_show_joystick(struct device *dev,
111	struct device_attribute *attr, char *buf)
112{
113	return sprintf(buf, "%d\n", joystick);
114}
115
116static ssize_t ams_input_store_joystick(struct device *dev,
117	struct device_attribute *attr, const char *buf, size_t count)
118{
119	unsigned long enable;
120	int error = 0;
 
121
122	if (strict_strtoul(buf, 0, &enable) || enable > 1)
 
 
 
123		return -EINVAL;
124
125	mutex_lock(&ams_input_mutex);
126
127	if (enable != joystick) {
128		if (enable)
129			error = ams_input_enable();
130		else
131			ams_input_disable();
132	}
133
134	mutex_unlock(&ams_input_mutex);
135
136	return error ? error : count;
137}
138
139static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR,
140	ams_input_show_joystick, ams_input_store_joystick);
141
142int ams_input_init(void)
143{
144	if (joystick)
145		ams_input_enable();
146
147	return device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
148}
149
150void ams_input_exit(void)
151{
152	device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick);
153
154	mutex_lock(&ams_input_mutex);
155	ams_input_disable();
156	mutex_unlock(&ams_input_mutex);
157}