summaryrefslogtreecommitdiff
path: root/drivers/input/keyboard/tegra-nvec.c
blob: 586de4ffffdfbab40bde6add081fe1bf4584bbea (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
 * drivers/input/keyboard/tegra-nvec.c
 *
 * Keyboard class input driver for keyboards connected to an NvEc compliant
 * embedded controller
 *
 * 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 <linux/tegra_devices.h>

#include "nvos.h"
#include "nvec.h"
#include "nvodm_services.h"
#include "nvodm_keyboard.h"

#define DRIVER_DESC "NvEc keyboard driver"
#define DRIVER_LICENSE "GPL"

MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE(DRIVER_LICENSE);

#define NVEC_PAYLOAD    32

/* The total number of scan codes will be (first - last) */
#define EC_FIRST_CODE 0x00
#define EC_LAST_CODE 0x58
#define EC_TOTAL_CODES (EC_LAST_CODE - EC_FIRST_CODE + 1)

/**
 * @brief This is the actual Scan-code-to-VKey mapping table. For new layouts
 *        this is the only structure which needs to be modified to return the
 *        proper vkey depending on the scan code.
 */
NvU8 code_tab_102us[EC_TOTAL_CODES] = {
	KEY_GRAVE,	// 0x00
	KEY_BACK,
	KEY_1,
	KEY_2,
	KEY_3,
	KEY_4,
	KEY_5,
	KEY_6,
	KEY_7,
	KEY_8,
	KEY_9,
	KEY_0,
	KEY_MINUS,
	KEY_EQUAL,
	KEY_BACKSPACE,
	KEY_TAB,
	KEY_Q,		// 0x10
	KEY_W,
	KEY_E,
	KEY_R,
	KEY_T,
	KEY_Y,
	KEY_U,
	KEY_I,
	KEY_O,
	KEY_P,
	KEY_LEFTBRACE,
	KEY_RIGHTBRACE,
	KEY_ENTER,
	KEY_LEFTCTRL,
	KEY_A,
	KEY_S,
	KEY_D,		// 0x20
	KEY_F,
	KEY_G,
	KEY_H,
	KEY_J,
	KEY_K,
	KEY_L,
	KEY_SEMICOLON,
	KEY_APOSTROPHE,
	KEY_GRAVE,
	KEY_LEFTSHIFT,
	KEY_BACKSLASH,
	KEY_Z,
	KEY_X,
	KEY_C,
	KEY_V,
	KEY_B,		// 0x30
	KEY_N,
	KEY_M,
	KEY_COMMA,
	KEY_DOT,
	KEY_SLASH,
	KEY_RIGHTSHIFT,
	KEY_KPASTERISK,
	KEY_LEFTALT,
	KEY_SPACE,
	KEY_CAPSLOCK,
	KEY_F1,
	KEY_F2,
	KEY_F3,
	KEY_F4,
	KEY_F5,
	KEY_F6,		// 0x40
	KEY_F7,
	KEY_F8,
	KEY_F9,
	KEY_F10,
	KEY_NUMLOCK,
	0,		//VK_SCROLL
	KEY_KP7,
	KEY_KP8,
	KEY_KP9,
	KEY_KPMINUS,
	KEY_KP4,
	KEY_KP5,
	KEY_KP6,
	KEY_KPPLUS,
	KEY_KP1,
	KEY_KP2,	// 0x50
	KEY_KP3,
	KEY_KP0,
	KEY_KPDOT,
	0,		//VK_SNAPSHOT
	0,
	0,		//VK_OEM_102
	0,		//VK_F11
	0,		//VK_F12
};

/* The total number of scan codes will be (first - last) */
#define EC_EXT_CODE_FIRST 0xE010
#define EC_EXT_CODE_LAST 0xE06D
#define EC_EXT_TOTAL_CODES (EC_EXT_CODE_LAST - EC_EXT_CODE_FIRST + 1)

/**
 * @brief This table consists of the scan codes which were added after the
 *		original scan codes were designed.
 *        To avoid moving the already designed buttons to accomodate these
 *        new buttons, the new scan codes are preceded by 'E0'.
 */
NvU8 extcode_tab_us102[EC_EXT_TOTAL_CODES] = {
	0,		// 0xE0 0x10
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,		//VK_MEDIA_NEXT_TRACK,
	0,
	0,
	0,		//VK_RETURN,
	0,		//VK_RCONTROL,
	0,
	0,
	KEY_MUTE,	// 0xE0 0x20
	0,		//VK_LAUNCH_APP1
	0,		//VK_MEDIA_PLAY_PAUSE
	0,
	0,		//VK_MEDIA_STOP
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	KEY_VOLUMEDOWN,
	0,
	KEY_VOLUMEUP,	// 0xE0 0x30
	0,
	0,		//VK_BROWSER_HOME
	0,
	0,
	KEY_KPSLASH,	//VK_DIVIDE
	0,
	0,		//VK_SNAPSHOT
	0,		//VK_RMENU
	0,		//VK_OEM_NV_BACKLIGHT_UP
	0,		//VK_OEM_NV_BACKLIGHT_DN
	0,		//VK_OEM_NV_BACKLIGHT_AUTOTOGGLE
	0,		//VK_OEM_NV_POWER_INFO
	0,		//VK_OEM_NV_WIFI_TOGGLE
	0,		//VK_OEM_NV_DISPLAY_SELECT
	0,		//VK_OEM_NV_AIRPLANE_TOGGLE
	0,		//0xE0 0x40
	0,		//VK_OEM_NV_RESERVED
	0,		//VK_OEM_NV_RESERVED
	0,		//VK_OEM_NV_RESERVED
	0,		//VK_OEM_NV_RESERVED
	0,		//VK_OEM_NV_RESERVED
	KEY_CANCEL,
	KEY_HOME,
	KEY_UP,
	0,		//VK_PRIOR
	0,
	KEY_LEFT,
	0,
	KEY_RIGHT,
	0,
	KEY_END,
	KEY_DOWN,	// 0xE0 0x50
	0,		//VK_NEXT
	KEY_INSERT,
	KEY_DELETE,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	KEY_MENU,	//VK_LWIN
	0,		//VK_RWIN
	KEY_BACK,	//VK_APPS
	0,
	0,
	0,		// 0xE0 0x60
	0,
	0,
	0,
	0,
	0,		//VK_BROWSER_SEARCH
	0,		//VK_BROWSER_FAVORITES
	0,		//VK_BROWSER_REFRESH
	0,		//VK_BROWSER_STOP
	0,		//VK_BROWSER_FORWARD
	0,		//VK_BROWSER_BACK
	0,		//VK_LAUNCH_APP2
	0,		//VK_LAUNCH_MAIL
	0,		//VK_LAUNCH_MEDIA_SELECT
};

struct nvec_keyboard
{
	struct input_dev		*input_dev;
	struct task_struct		*task;
	char				name[128];
	unsigned short			keycode[512];
	NvEcHandle			hNvec;
	NvEcEventRegistrationHandle	hEvent;
};

static struct platform_device *nvec_keyboard_device;

static int nvec_keyboard_recv(void *arg)
{
	struct input_dev *input_dev = (struct input_dev *)arg;
	struct nvec_keyboard *keyboard = input_get_drvdata(input_dev);

	while (!kthread_should_stop()) {
		unsigned int pressed;
		NvU32 code;
		NvU8 flags;

		if (!NvOdmKeyboardGetKeyData(&code, &flags, 0)) {
			printk(KERN_INFO "nvec_keyboard: unhandled "
				"scancode %x\n", code);
			continue;
		}

		/* Check for the thread terminaiton request */
		if (kthread_should_stop())
			return 0;

		pressed = (flags & NV_ODM_SCAN_CODE_FLAG_MAKE);


		if ((code >= EC_FIRST_CODE) && (code <= EC_LAST_CODE)) {
			code -= EC_FIRST_CODE;
			code = code_tab_102us[code];
			input_report_key(keyboard->input_dev, code, pressed);
		}
		else if ((code >= EC_EXT_CODE_FIRST) &&
			(code <= EC_EXT_CODE_LAST)) {

			code -= EC_EXT_CODE_FIRST;
			code = extcode_tab_us102[code];
			input_report_key(keyboard->input_dev, code, pressed);
		}
	}

	return 0;
}

static int nvec_keyboard_open(struct input_dev *dev)
{
	return 0;
}

static void nvec_keyboard_close(struct input_dev *dev)
{
	return;
}


static int __devinit nvec_keyboard_probe(struct platform_device *pdev)
{
	int error;
	NvError nverr;
	struct nvec_keyboard *keyboard;
	struct input_dev *input_dev;
	int i;

	keyboard = kzalloc(sizeof(struct nvec_keyboard), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (!keyboard || !input_dev) {
		error = -ENOMEM;
		goto fail;
	}

	keyboard->input_dev = input_dev;
	input_set_drvdata(input_dev, keyboard);
	platform_set_drvdata(pdev, input_dev);

	if (!NvOdmKeyboardInit()) {
		error = -ENODEV;
		pr_err("tegra_keyboard_probe: no keyboard\n");
		goto fail_keyboard_init;
	}

	keyboard->task = kthread_create(nvec_keyboard_recv, input_dev,
		"nvec_keyboard_thread");
	if (keyboard->task == NULL) {
		error = -ENOMEM;
		goto fail_thread_create;
	}
	wake_up_process(keyboard->task);

	if (!strlen(keyboard->name))
		snprintf(keyboard->name, sizeof(keyboard->name),
			 "nvec keyboard");

	input_dev->name = keyboard->name;
	input_dev->open = nvec_keyboard_open;
	input_dev->close = nvec_keyboard_close;

	__set_bit(EV_KEY, input_dev->evbit);
	for (i=1; i<EC_TOTAL_CODES; i++) {
		__set_bit(code_tab_102us[i], input_dev->keybit);
	}
	for (i=1; i<EC_EXT_TOTAL_CODES; i++) {
		__set_bit(extcode_tab_us102[i], input_dev->keybit);
	}

	error = input_register_device(keyboard->input_dev);
	if (error)
		goto fail_input_register;

	return 0;

fail_input_register:
	(void)kthread_stop(keyboard->task);
fail_thread_create:
	NvOdmKeyboardDeInit();
fail_keyboard_init:
fail:
	input_free_device(input_dev);
	kfree(keyboard);

	return error;
}

static int __devexit nvec_keyboard_remove(struct platform_device *dev)
{
	struct input_dev *input_dev = platform_get_drvdata(dev);
	struct nvec_keyboard *keyboard = platform_get_drvdata(dev);

	(void)kthread_stop(keyboard->task);
	NvOdmKeyboardDeInit();
	input_free_device(input_dev);
	kfree(keyboard);

	return 0;
}

static struct platform_driver nvec_keyboard_driver = {
	.driver	 = {
		.name	= "nvec_keyboard",
		.owner	= THIS_MODULE,
	},
	.probe	= nvec_keyboard_probe,
	.remove	= __devexit_p(nvec_keyboard_remove),
};


static int __init nvec_keyboard_init(void)
{
	int err;

	err = platform_driver_register(&nvec_keyboard_driver);
	if (err) {
		goto error;
	}

	nvec_keyboard_device = platform_device_alloc("nvec_keyboard", -1);
	if (!nvec_keyboard_device) {
		err = -ENOMEM;
		goto error_unregister_driver;
	}

	err = platform_device_add(nvec_keyboard_device);
	if (err)
		goto error_free_device;

	return 0;

error_free_device:
	platform_device_put(nvec_keyboard_device);
error_unregister_driver:
	platform_driver_unregister(&nvec_keyboard_driver);
error:
	return err;
}

static void __exit nvec_keyboard_exit(void)
{
	platform_device_unregister(nvec_keyboard_device);
	platform_driver_unregister(&nvec_keyboard_driver);
}

module_init(nvec_keyboard_init);
module_exit(nvec_keyboard_exit);