aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/accessory/sec_keyboard.c
blob: cc89c0c30f9f5a96d529b95c731a2cbf019675fb (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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512

#include "sec_keyboard.h"

static void sec_keyboard_tx(struct sec_keyboard_drvdata *data, u8 cmd)
{
	if (data->pre_connected && data->tx_ready)
		serio_write(data->serio, cmd);
}

static void sec_keyboard_power(struct work_struct *work)
{
	struct sec_keyboard_drvdata *data = container_of(work,
			struct sec_keyboard_drvdata, power_dwork.work);

	if (UNKOWN_KEYLAYOUT == data->kl) {
		data->acc_power(1, false);
		data->pre_connected = false;

		if (data->check_uart_path)
			data->check_uart_path(false);
	}
}

static void forced_wakeup(struct sec_keyboard_drvdata *data)
{
	input_report_key(data->input_dev,
		KEY_WAKEUP, 1);
	input_report_key(data->input_dev,
		KEY_WAKEUP, 0);
	input_sync(data->input_dev);
}

static void sec_keyboard_remapkey(struct work_struct *work)
{
	unsigned int keycode = 0;
	struct sec_keyboard_drvdata *data = container_of(work,
			struct sec_keyboard_drvdata, remap_dwork.work);

	if (data->pressed[0x45] || data->pressed[0x48]) {
		keycode = data->keycode[data->remap_key];
		input_report_key(data->input_dev, keycode, 1);
		input_sync(data->input_dev);
	}
	data->remap_key = 0;
}

static void release_all_keys(struct sec_keyboard_drvdata *data)
{
	int i;
	printk(KERN_DEBUG "[Keyboard] Release the pressed keys.\n");
	for (i = 0; i < KEYBOARD_MAX; i++) {
		if (data->pressed[i]) {
			input_report_key(data->input_dev, data->keycode[i], 0);
			data->pressed[i] = false;
		}
		input_sync(data->input_dev);
	}
}

static void sec_keyboard_process_data(
	struct sec_keyboard_drvdata *data, u8 scan_code)
{
	bool press;
	unsigned int keycode;

	/* keyboard driver need the contry code*/
	if (data->kl == UNKOWN_KEYLAYOUT) {
		switch (scan_code) {
		case US_KEYBOARD:
			data->kl = US_KEYLAYOUT;
			data->keycode[49] = KEY_BACKSLASH;
			/* for the wakeup state*/
			data->pre_kl = data->kl;
			printk(KERN_DEBUG "[Keyboard] US keyboard is attacted.\n");
			break;

		case UK_KEYBOARD:
			data->kl = UK_KEYLAYOUT;
			data->keycode[49] = KEY_NUMERIC_POUND;
			/* for the wakeup state*/
			data->pre_kl = data->kl;
			printk(KERN_DEBUG "[Keyboard] UK keyboard is attacted.\n");
			break;

		default:
			printk(KERN_DEBUG "[Keyboard] Unkown layout : %x\n",
				scan_code);
			break;
		}
	} else {
		switch (scan_code) {
		case 0x0:
			release_all_keys(data);
			break;

		case 0xca: /* Caps lock on */
		case 0xcb: /* Caps lock off */
		case 0xeb: /* US keyboard */
		case 0xec: /* UK keyboard */
			break; /* Ignore */

		case 0x45:
		case 0x48:
			data->remap_key = scan_code;
			data->pressed[scan_code] = true;
			schedule_delayed_work(&data->remap_dwork, HZ/3);
			break;

		case 0xc5:
		case 0xc8:
			keycode = (scan_code & 0x7f);
			data->pressed[keycode] = false;
			if (0 == data->remap_key) {
				input_report_key(data->input_dev,
					data->keycode[keycode], 0);
				input_sync(data->input_dev);
			} else {
				cancel_delayed_work_sync(&data->remap_dwork);
				if (0x48 == keycode)
					keycode = KEY_NEXTSONG;
				else
					keycode = KEY_PREVIOUSSONG;

				input_report_key(data->input_dev,
					keycode, 1);
				input_report_key(data->input_dev,
					keycode, 0);
				input_sync(data->input_dev);
			}
			break;

		default:
			keycode = (scan_code & 0x7f);
			press = ((scan_code & 0x80) != 0x80);

			if (keycode >= KEYBOARD_MIN
				|| keycode <= KEYBOARD_MAX) {
				data->pressed[keycode] = press;
				input_report_key(data->input_dev,
					data->keycode[keycode], press);
				input_sync(data->input_dev);
			}
			break;
		}
	}
}

static int check_keyboard_dock(struct sec_keyboard_callbacks *cb, bool val)
{
	struct sec_keyboard_drvdata *data =
		container_of(cb, struct sec_keyboard_drvdata, callbacks);
	int try_cnt = 0;
	int max_cnt = 14;

	if (NULL == data->serio) {
		for (try_cnt = 0; try_cnt < max_cnt; try_cnt++) {
			msleep(50);
			if (data->tx_ready)
				break;

			if (gpio_get_value(data->acc_int_gpio)) {
				printk(KERN_DEBUG "[Keyboard] acc is disconnected.\n");
				return 0;
			}
		}
	}

	if (!val)
		data->dockconnected = false;
	else {
		cancel_delayed_work_sync(&data->power_dwork);
		/* wakeup by keyboard dock */
		if (data->pre_connected) {
			if (UNKOWN_KEYLAYOUT != data->pre_kl) {
				data->kl = data->pre_kl;
				data->acc_power(1, true);
				printk(KERN_DEBUG "[Keyboard] kl : %d\n",
					data->pre_kl);
				return 1;
			}
		} else
			data->pre_kl = UNKOWN_KEYLAYOUT;

		data->pre_connected = true;

		/* to prevent the over current issue */
		data->acc_power(0, false);

		if (data->check_uart_path)
			data->check_uart_path(true);

		msleep(200);
		data->acc_power(1, true);

		/* try to get handshake data */
		for (try_cnt = 0; try_cnt < max_cnt; try_cnt++) {
			msleep(50);
			if (data->kl != UNKOWN_KEYLAYOUT) {
				data->dockconnected = true;
				break;
			}
			if (gpio_get_value(data->acc_int_gpio)) {
				printk(KERN_DEBUG "[Keyboard] acc is disconnected.\n");
				break;
			}
		}
	}

	if (data->dockconnected) {
		return 1;
	} else {
		if (data->pre_connected) {
			data->dockconnected = false;
			schedule_delayed_work(&data->power_dwork, HZ/2);

			data->kl = UNKOWN_KEYLAYOUT;
			release_all_keys(data);
		}
		return 0;
	}
}

static int sec_keyboard_event(struct input_dev *dev,
			unsigned int type, unsigned int code, int value)
{
	struct sec_keyboard_drvdata *data = input_get_drvdata(dev);

	switch (type) {
	case EV_LED:
		if (value)
			sec_keyboard_tx(data, 0xca);
		else
			sec_keyboard_tx(data, 0xcb);

	printk(KERN_DEBUG "[Keyboard] %s, capslock on led value=%d\n",\
		 __func__, value);
		return 0;
	}
	return -1;
}

static ssize_t check_keyboard_connection(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct sec_keyboard_drvdata *ddata = dev_get_drvdata(dev);

	return sprintf(buf, "%u\n", ddata->dockconnected);
}

static DEVICE_ATTR(attached, S_IRUGO, check_keyboard_connection, NULL);

static struct attribute *sec_keyboard_attributes[] = {
	&dev_attr_attached.attr,
	NULL,
};

static struct attribute_group attr_group = {
	.attrs = sec_keyboard_attributes,
};

static irqreturn_t sec_keyboard_interrupt(struct serio *serio,
		unsigned char data, unsigned int flags)
{
	struct sec_keyboard_drvdata *ddata = serio_get_drvdata(serio);
	if (ddata->pre_connected)
		sec_keyboard_process_data(ddata, data);
	return IRQ_HANDLED;
}

static int sec_keyboard_connect(struct serio *serio, struct serio_driver *drv)
{
	struct sec_keyboard_drvdata *data = container_of(drv,
			struct sec_keyboard_drvdata, serio_driver);
	printk(KERN_DEBUG "[Keyboard] %s", __func__);
	data->serio = serio;
	serio_set_drvdata(serio, data);
	if (serio_open(serio, drv))
		printk(KERN_ERR "[Keyboard] failed to open serial port\n");
	else
		data->tx_ready = true;
	return 0;
}

static void sec_keyboard_disconnect(struct serio *serio)
{
	struct sec_keyboard_drvdata *data = serio_get_drvdata(serio);
	printk(KERN_DEBUG "[Keyboard] %s", __func__);
	data->tx_ready = false;
	serio_close(serio);
}

#ifdef CONFIG_HAS_EARLYSUSPEND
static void keyboard_early_suspend(struct early_suspend *early_sus)
{
	struct sec_keyboard_drvdata *data = container_of(early_sus,
		struct sec_keyboard_drvdata, early_suspend);

	if (data->kl != UNKOWN_KEYLAYOUT) {
		/*
		if the command of the caps lock off is needed,
		this command should be sent.
		sec_keyboard_tx(0xcb);
		msleep(20);
		*/
		release_all_keys(data);
		sec_keyboard_tx(data, 0x10);	/* the idle mode */
	}
}

static void keyboard_late_resume(struct early_suspend *early_sus)
{
	struct sec_keyboard_drvdata *data = container_of(early_sus,
		struct sec_keyboard_drvdata, early_suspend);

	if (data->kl != UNKOWN_KEYLAYOUT)
		printk(KERN_DEBUG "[Keyboard] %s\n", __func__);

}
#endif
static int __devinit sec_keyboard_probe(struct platform_device *pdev)
{
	struct sec_keyboard_platform_data *pdata = pdev->dev.platform_data;
	struct sec_keyboard_drvdata *ddata;
	struct input_dev *input;
	int i, error;

	if (pdata == NULL) {
		printk(KERN_ERR "%s: no pdata\n", __func__);
		return -ENODEV;
	}

	ddata = kzalloc(sizeof(struct sec_keyboard_drvdata), GFP_KERNEL);
	if (NULL == ddata) {
		error = -ENOMEM;
		goto err_free_mem;
	}

	input = input_allocate_device();
	if (NULL == input) {
		printk(KERN_ERR "[Keyboard] failed to allocate input device.\n");
		error = -ENOMEM;
		goto err_free_mem;
	}

	ddata->input_dev = input;
	ddata->acc_power = pdata->acc_power;
	ddata->check_uart_path = pdata->check_uart_path;
	ddata->acc_int_gpio = pdata->accessory_irq_gpio;
	ddata->led_on = false;
	ddata->dockconnected = false;
	ddata->pre_connected = false;
	ddata->remap_key = 0;
	ddata->kl = UNKOWN_KEYLAYOUT;
	ddata->callbacks.check_keyboard_dock = check_keyboard_dock;
	if (pdata->register_cb)
		pdata->register_cb(&ddata->callbacks);

	memcpy(ddata->keycode, sec_keycodes, sizeof(sec_keycodes));

	INIT_DELAYED_WORK(&ddata->remap_dwork, sec_keyboard_remapkey);
	INIT_DELAYED_WORK(&ddata->power_dwork, sec_keyboard_power);

	platform_set_drvdata(pdev, ddata);
	input_set_drvdata(input, ddata);

	input->name = pdev->name;
	input->dev.parent = &pdev->dev;
	input->id.bustype = BUS_RS232;
	input->event = sec_keyboard_event;

	__set_bit(EV_KEY, input->evbit);
	__set_bit(EV_LED, input->evbit);
	__set_bit(LED_CAPSL, input->ledbit);
	/* framework doesn't use repeat event */
	/* __set_bit(EV_REP, input->evbit); */

	for (i = 0; i < KEYBOARD_SIZE; i++) {
		if (KEY_RESERVED != ddata->keycode[i])
			input_set_capability(input, EV_KEY, ddata->keycode[i]);
	}

	/* for the UK keyboard */
	input_set_capability(input, EV_KEY, KEY_NUMERIC_POUND);

	/* for the remaped keys */
	input_set_capability(input, EV_KEY, KEY_NEXTSONG);
	input_set_capability(input, EV_KEY, KEY_PREVIOUSSONG);

	/* for the wakeup key */
	input_set_capability(input, EV_KEY, KEY_WAKEUP);

	error = input_register_device(input);
	if (error < 0) {
		printk(KERN_ERR "[Keyboard] failed to register input device.\n");
		error = -ENOMEM;
		goto err_input_allocate_device;
	}

	ddata->serio_driver.driver.name = pdev->name;
	ddata->serio_driver.id_table = sec_serio_ids;
	ddata->serio_driver.interrupt = sec_keyboard_interrupt,
	ddata->serio_driver.connect = sec_keyboard_connect,
	ddata->serio_driver.disconnect = sec_keyboard_disconnect,

	error = serio_register_driver(&ddata->serio_driver);
	if (error < 0) {
		printk(KERN_ERR "[Keyboard] failed to register serio\n");
		error = -ENOMEM;
		goto err_reg_serio;
	}

#ifdef CONFIG_HAS_EARLYSUSPEND
	ddata->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
	ddata->early_suspend.suspend = keyboard_early_suspend;
	ddata->early_suspend.resume = keyboard_late_resume;
	register_early_suspend(&ddata->early_suspend);
#endif	/* CONFIG_HAS_EARLYSUSPEND */

	ddata->keyboard_dev = device_create(sec_class, NULL, 0,
		ddata, "sec_keyboard");
	if (IS_ERR(ddata->keyboard_dev)) {
		printk(KERN_ERR "[Keyboard] failed to create device for the sysfs\n");
		error = -ENODEV;
		goto err_sysfs_create_group;
	}

	error = sysfs_create_group(&ddata->keyboard_dev->kobj, &attr_group);
	if (error) {
		printk(KERN_ERR "[Keyboard] failed to create sysfs group\n");
		goto err_sysfs_create_group;
	}

	return 0;

err_sysfs_create_group:
#ifdef CONFIG_HAS_EARLYSUSPEND
	unregister_early_suspend(&ddata->early_suspend);
#endif
	serio_unregister_driver(&ddata->serio_driver);
err_reg_serio:
err_input_allocate_device:
	input_free_device(input);
	del_timer_sync(&ddata->remap_dwork.timer);
	del_timer_sync(&ddata->power_dwork.timer);
err_free_mem:
	kfree(ddata);
	return error;

}

static int __devexit sec_keyboard_remove(struct platform_device *pdev)
{
	struct sec_keyboard_drvdata *data = platform_get_drvdata(pdev);
	input_unregister_device(data->input_dev);
	serio_unregister_driver(&data->serio_driver);
	return 0;
}

#ifndef CONFIG_HAS_EARLYSUSPEND
static int sec_keyboard_suspend(struct platform_device *pdev,
			pm_message_t state)
{
	struct sec_keyboard_drvdata *data = platform_get_drvdata(pdev);

	if (data->kl != UNKOWN_KEYLAYOUT)
		sec_keyboard_tx(data, 0x10);

	return 0;
}

static int sec_keyboard_resume(struct platform_device *pdev)
{
	struct sec_keyboard_platform_data *pdata = pdev->dev.platform_data;
	struct sec_keyboard_drvdata *data = platform_get_drvdata(pdev);
	if (pdata->wakeup_key) {
		if (KEY_WAKEUP == pdata->wakeup_key())
			forced_wakeup(data);
	}

	return 0;
}
#endif

static struct platform_driver sec_keyboard_driver = {
	.probe = sec_keyboard_probe,
	.remove = __devexit_p(sec_keyboard_remove),
#ifndef CONFIG_HAS_EARLYSUSPEND
	.suspend = sec_keyboard_suspend,
	.resume	= sec_keyboard_resume,
#endif
	.driver = {
		.name	= "sec_keyboard",
		.owner	= THIS_MODULE,
	}
};

static int __init sec_keyboard_init(void)
{
	return platform_driver_register(&sec_keyboard_driver);
}

static void __exit sec_keyboard_exit(void)
{
	platform_driver_unregister(&sec_keyboard_driver);
}

module_init(sec_keyboard_init);
module_exit(sec_keyboard_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("SEC Keyboard Dock driver");