1 /*
2  * FireDTV driver (formerly known as FireSAT)
3  *
4  * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
5  *
6  *	This program is free software; you can redistribute it and/or
7  *	modify it under the terms of the GNU General Public License as
8  *	published by the Free Software Foundation; either version 2 of
9  *	the License, or (at your option) any later version.
10  */
11 
12 #include <linux/bitops.h>
13 #include <linux/input.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
18 #include <linux/workqueue.h>
19 
20 #include "firedtv.h"
21 
22 /* fixed table with older keycodes, geared towards MythTV */
23 static const u16 oldtable[] = {
24 
25 	/* code from device: 0x4501...0x451f */
26 
27 	KEY_ESC,
28 	KEY_F9,
29 	KEY_1,
30 	KEY_2,
31 	KEY_3,
32 	KEY_4,
33 	KEY_5,
34 	KEY_6,
35 	KEY_7,
36 	KEY_8,
37 	KEY_9,
38 	KEY_I,
39 	KEY_0,
40 	KEY_ENTER,
41 	KEY_RED,
42 	KEY_UP,
43 	KEY_GREEN,
44 	KEY_F10,
45 	KEY_SPACE,
46 	KEY_F11,
47 	KEY_YELLOW,
48 	KEY_DOWN,
49 	KEY_BLUE,
50 	KEY_Z,
51 	KEY_P,
52 	KEY_PAGEDOWN,
53 	KEY_LEFT,
54 	KEY_W,
55 	KEY_RIGHT,
56 	KEY_P,
57 	KEY_M,
58 
59 	/* code from device: 0x4540...0x4542 */
60 
61 	KEY_R,
62 	KEY_V,
63 	KEY_C,
64 };
65 
66 /* user-modifiable table for a remote as sold in 2008 */
67 static const u16 keytable[] = {
68 
69 	/* code from device: 0x0300...0x031f */
70 
71 	[0x00] = KEY_POWER,
72 	[0x01] = KEY_SLEEP,
73 	[0x02] = KEY_STOP,
74 	[0x03] = KEY_OK,
75 	[0x04] = KEY_RIGHT,
76 	[0x05] = KEY_1,
77 	[0x06] = KEY_2,
78 	[0x07] = KEY_3,
79 	[0x08] = KEY_LEFT,
80 	[0x09] = KEY_4,
81 	[0x0a] = KEY_5,
82 	[0x0b] = KEY_6,
83 	[0x0c] = KEY_UP,
84 	[0x0d] = KEY_7,
85 	[0x0e] = KEY_8,
86 	[0x0f] = KEY_9,
87 	[0x10] = KEY_DOWN,
88 	[0x11] = KEY_TITLE,	/* "OSD" - fixme */
89 	[0x12] = KEY_0,
90 	[0x13] = KEY_F20,	/* "16:9" - fixme */
91 	[0x14] = KEY_SCREEN,	/* "FULL" - fixme */
92 	[0x15] = KEY_MUTE,
93 	[0x16] = KEY_SUBTITLE,
94 	[0x17] = KEY_RECORD,
95 	[0x18] = KEY_TEXT,
96 	[0x19] = KEY_AUDIO,
97 	[0x1a] = KEY_RED,
98 	[0x1b] = KEY_PREVIOUS,
99 	[0x1c] = KEY_REWIND,
100 	[0x1d] = KEY_PLAYPAUSE,
101 	[0x1e] = KEY_NEXT,
102 	[0x1f] = KEY_VOLUMEUP,
103 
104 	/* code from device: 0x0340...0x0354 */
105 
106 	[0x20] = KEY_CHANNELUP,
107 	[0x21] = KEY_F21,	/* "4:3" - fixme */
108 	[0x22] = KEY_TV,
109 	[0x23] = KEY_DVD,
110 	[0x24] = KEY_VCR,
111 	[0x25] = KEY_AUX,
112 	[0x26] = KEY_GREEN,
113 	[0x27] = KEY_YELLOW,
114 	[0x28] = KEY_BLUE,
115 	[0x29] = KEY_CHANNEL,	/* "CH.LIST" */
116 	[0x2a] = KEY_VENDOR,	/* "CI" - fixme */
117 	[0x2b] = KEY_VOLUMEDOWN,
118 	[0x2c] = KEY_CHANNELDOWN,
119 	[0x2d] = KEY_LAST,
120 	[0x2e] = KEY_INFO,
121 	[0x2f] = KEY_FORWARD,
122 	[0x30] = KEY_LIST,
123 	[0x31] = KEY_FAVORITES,
124 	[0x32] = KEY_MENU,
125 	[0x33] = KEY_EPG,
126 	[0x34] = KEY_EXIT,
127 };
128 
129 int fdtv_register_rc(struct firedtv *fdtv, struct device *dev)
130 {
131 	struct input_dev *idev;
132 	int i, err;
133 
134 	idev = input_allocate_device();
135 	if (!idev)
136 		return -ENOMEM;
137 
138 	fdtv->remote_ctrl_dev = idev;
139 	idev->name = "FireDTV remote control";
140 	idev->dev.parent = dev;
141 	idev->evbit[0] = BIT_MASK(EV_KEY);
142 	idev->keycode = kmemdup(keytable, sizeof(keytable), GFP_KERNEL);
143 	if (!idev->keycode) {
144 		err = -ENOMEM;
145 		goto fail;
146 	}
147 	idev->keycodesize = sizeof(keytable[0]);
148 	idev->keycodemax = ARRAY_SIZE(keytable);
149 
150 	for (i = 0; i < ARRAY_SIZE(keytable); i++)
151 		set_bit(keytable[i], idev->keybit);
152 
153 	err = input_register_device(idev);
154 	if (err)
155 		goto fail_free_keymap;
156 
157 	return 0;
158 
159 fail_free_keymap:
160 	kfree(idev->keycode);
161 fail:
162 	input_free_device(idev);
163 	return err;
164 }
165 
166 void fdtv_unregister_rc(struct firedtv *fdtv)
167 {
168 	cancel_work_sync(&fdtv->remote_ctrl_work);
169 	kfree(fdtv->remote_ctrl_dev->keycode);
170 	input_unregister_device(fdtv->remote_ctrl_dev);
171 }
172 
173 void fdtv_handle_rc(struct firedtv *fdtv, unsigned int code)
174 {
175 	struct input_dev *idev = fdtv->remote_ctrl_dev;
176 	u16 *keycode = idev->keycode;
177 
178 	if (code >= 0x0300 && code <= 0x031f)
179 		code = keycode[code - 0x0300];
180 	else if (code >= 0x0340 && code <= 0x0354)
181 		code = keycode[code - 0x0320];
182 	else if (code >= 0x4501 && code <= 0x451f)
183 		code = oldtable[code - 0x4501];
184 	else if (code >= 0x4540 && code <= 0x4542)
185 		code = oldtable[code - 0x4521];
186 	else {
187 		printk(KERN_DEBUG "firedtv: invalid key code 0x%04x "
188 		       "from remote control\n", code);
189 		return;
190 	}
191 
192 	input_report_key(idev, code, 1);
193 	input_sync(idev);
194 	input_report_key(idev, code, 0);
195 	input_sync(idev);
196 }
197