summaryrefslogtreecommitdiff
path: root/drivers/input/misc/tegra_odm_scroll.c
blob: bbfbc8fed26d4ef339bbc13c92f7ba7db6e77623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * drivers/input/misc/tegra_odm_scroll.c
 *
 * Scrollwheel input device using NVIDIA Tegra ODM kit
 *
 * Copyright (c) 2009, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/module.h>
#include <linux/input.h>
#include <linux/platform_device.h>
#include <linux/kthread.h>

#include <mach/nvrm_linux.h>
#include <nvos.h>

#include <nvodm_scrollwheel.h>

struct tegra_scroll_dev {
	struct input_dev	*input_dev;
	struct task_struct	*task;
	NvOdmOsSemaphoreHandle	sem;
	NvOdmScrollWheelHandle	odm_dev;
};

static int scroll_thread(void *pdata)
{
	struct tegra_scroll_dev *scroll = pdata;
	NvOdmScrollWheelEvent Events;

	/* FIXME add a terminating condition */ 
	while(1) {
		NvOdmOsSemaphoreWait(scroll->sem);

		Events = NvOdmScrollWheelGetEvent(scroll->odm_dev);
		if (Events & NvOdmScrollWheelEvent_RotateAntiClockWise) {
			input_report_key(scroll->input_dev, KEY_UP, 1);
			input_report_key(scroll->input_dev, KEY_UP, 0);
		}
		if (Events & NvOdmScrollWheelEvent_RotateClockWise) {
			input_report_key(scroll->input_dev, KEY_DOWN, 1);
			input_report_key(scroll->input_dev, KEY_DOWN, 0);
		}

		if (Events & NvOdmScrollWheelEvent_Press)
			input_report_key(scroll->input_dev, KEY_ENTER, 1);
		else if (Events & NvOdmScrollWheelEvent_Release)
			input_report_key(scroll->input_dev, KEY_ENTER, 0);
	}
	return 0;
}
 

static int __init tegra_scroll_probe(struct platform_device *pdev)
{
	struct tegra_scroll_dev *scroll = NULL;
	struct input_dev *input_dev = NULL;
	NvOdmOsSemaphoreHandle sem = NULL;
	NvOdmScrollWheelHandle odm_dev = NULL;
	int err;
	NvOdmScrollWheelEvent events;

	sem = NvOdmOsSemaphoreCreate(0);
	if (!sem) {
		pr_err("tegra_scroll_probe: Semaphore creation failed\n");
		return -ENOMEM;
	}

	events = NvOdmScrollWheelEvent_Press |
		NvOdmScrollWheelEvent_Release  |
		NvOdmScrollWheelEvent_RotateAntiClockWise |
		NvOdmScrollWheelEvent_RotateClockWise;
 
	odm_dev = NvOdmScrollWheelOpen(sem, events);
	if (odm_dev == NULL) {
		pr_err("tegra_scroll_probe: scroll wheel not found\n");
		err = -ENXIO;
		goto err_scrollWheelnotfound;
	}

	scroll = kzalloc(sizeof(struct tegra_scroll_dev), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (input_dev == NULL || scroll == NULL) {
		err = -ENOMEM;
		pr_err("tegra_scroll_probe: Failed to allocate input device\n");
		goto err_alloc_failed;
	}

	scroll->input_dev = input_dev;
	scroll->sem = sem;
	scroll->odm_dev = odm_dev;
	scroll->task = kthread_create(scroll_thread, scroll, "tegra_scroll");
	if (scroll->task == NULL) {
		err = -1;
		goto err_kthread_create_failed;
	}
	wake_up_process( scroll->task );

	__set_bit(EV_KEY, input_dev->evbit);
	__set_bit(KEY_ENTER, input_dev->keybit);
	__set_bit(KEY_UP, input_dev->keybit);
	__set_bit(KEY_DOWN, input_dev->keybit);

	platform_set_drvdata(pdev, scroll);
	err = input_register_device(input_dev);
	if (err) {
		pr_err("tegra_scroll_probe: Unable to register input device\n");
		goto err_input_register_device_failed;
	}

	return 0;

err_input_register_device_failed:
	NvOdmScrollWheelClose(scroll->odm_dev);
err_kthread_create_failed:
	/* What to do? */
err_alloc_failed:
	kfree(scroll);
	input_free_device(input_dev);
	NvOdmScrollWheelClose(odm_dev);
err_scrollWheelnotfound:
	NvOdmOsSemaphoreDestroy(sem);
	return err;
}

static int tegra_scroll_remove(struct platform_device *pdev)
{
	struct tegra_scroll_dev *scroll = platform_get_drvdata(pdev);

	/* FIXME How to destroy the thread? Maybe we should use workqueues? */
	input_unregister_device(scroll->input_dev);
	NvOdmScrollWheelClose(scroll->odm_dev);
	NvOdmOsSemaphoreDestroy(scroll->sem);
	kfree(scroll);
	return 0;
}

static struct platform_driver tegra_ScrollWheel_driver = {
	.probe	= tegra_scroll_probe,
	.remove	= tegra_scroll_remove,
	.driver	= {
		.name = "tegra_scrollwheel",
	},
};

static int __devinit tegra_ScrollWheel_init(void)
{
	return platform_driver_register(&tegra_ScrollWheel_driver);
}

static void __exit tegra_ScrollWheel_exit(void)
{
	platform_driver_unregister(&tegra_ScrollWheel_driver);
}

module_init(tegra_ScrollWheel_init);
module_exit(tegra_ScrollWheel_exit);

MODULE_DESCRIPTION("Tegra Key board controller driver");