1/*
2 *
3 *  BlueZ - Bluetooth protocol stack for Linux
4 *
5 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
6 *
7 *
8 *  This program is free software; you can redistribute it and/or modify
9 *  it under the terms of the GNU General Public License as published by
10 *  the Free Software Foundation; either version 2 of the License, or
11 *  (at your option) any later version.
12 *
13 *  This program is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *  GNU General Public License for more details.
17 *
18 *  You should have received a copy of the GNU General Public License
19 *  along with this program; if not, write to the Free Software
20 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 */
23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include <stdio.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <stdlib.h>
33#include <sys/types.h>
34
35#include <bluetooth/bluetooth.h>
36#include <bluetooth/l2cap.h>
37#include <bluetooth/hidp.h>
38#include <bluetooth/sdp.h>
39
40#include <glib.h>
41#include <dbus/dbus.h>
42
43#include "../src/adapter.h"
44#include "../src/device.h"
45
46#include "log.h"
47#include "device.h"
48#include "fakehid.h"
49#include "uinput.h"
50
51#define PS3_FLAGS_MASK 0xFFFFFF00
52
53enum ps3remote_special_keys {
54	PS3R_BIT_PS = 0,
55	PS3R_BIT_ENTER = 3,
56	PS3R_BIT_L2 = 8,
57	PS3R_BIT_R2 = 9,
58	PS3R_BIT_L1 = 10,
59	PS3R_BIT_R1 = 11,
60	PS3R_BIT_TRIANGLE = 12,
61	PS3R_BIT_CIRCLE = 13,
62	PS3R_BIT_CROSS = 14,
63	PS3R_BIT_SQUARE = 15,
64	PS3R_BIT_SELECT = 16,
65	PS3R_BIT_L3 = 17,
66	PS3R_BIT_R3 = 18,
67	PS3R_BIT_START = 19,
68	PS3R_BIT_UP = 20,
69	PS3R_BIT_RIGHT = 21,
70	PS3R_BIT_DOWN = 22,
71	PS3R_BIT_LEFT = 23,
72};
73
74static unsigned int ps3remote_bits[] = {
75	[PS3R_BIT_ENTER] = 0x0b,
76	[PS3R_BIT_PS] = 0x43,
77	[PS3R_BIT_SQUARE] = 0x5f,
78	[PS3R_BIT_CROSS] = 0x5e,
79	[PS3R_BIT_CIRCLE] = 0x5d,
80	[PS3R_BIT_TRIANGLE] = 0x5c,
81	[PS3R_BIT_R1] = 0x5b,
82	[PS3R_BIT_L1] = 0x5a,
83	[PS3R_BIT_R2] = 0x59,
84	[PS3R_BIT_L2] = 0x58,
85	[PS3R_BIT_LEFT] = 0x57,
86	[PS3R_BIT_DOWN] = 0x56,
87	[PS3R_BIT_RIGHT] = 0x55,
88	[PS3R_BIT_UP] = 0x54,
89	[PS3R_BIT_START] = 0x53,
90	[PS3R_BIT_R3] = 0x52,
91	[PS3R_BIT_L3] = 0x51,
92	[PS3R_BIT_SELECT] = 0x50,
93};
94
95static unsigned int ps3remote_keymap[] = {
96	[0x16] = KEY_EJECTCD,
97	[0x64] = KEY_AUDIO,
98	[0x65] = KEY_ANGLE,
99	[0x63] = KEY_SUBTITLE,
100	[0x0f] = KEY_CLEAR,
101	[0x28] = KEY_TIME,
102	[0x00] = KEY_1,
103	[0x01] = KEY_2,
104	[0x02] = KEY_3,
105	[0x03] = KEY_4,
106	[0x04] = KEY_5,
107	[0x05] = KEY_6,
108	[0x06] = KEY_7,
109	[0x07] = KEY_8,
110	[0x08] = KEY_9,
111	[0x09] = KEY_0,
112	[0x81] = KEY_RED,
113	[0x82] = KEY_GREEN,
114	[0x80] = KEY_BLUE,
115	[0x83] = KEY_YELLOW,
116	[0x70] = KEY_INFO,		/* display */
117	[0x1a] = KEY_MENU,		/* top menu */
118	[0x40] = KEY_CONTEXT_MENU,	/* pop up/menu */
119	[0x0e] = KEY_ESC,		/* return */
120	[0x5c] = KEY_OPTION,		/* options/triangle */
121	[0x5d] = KEY_BACK,		/* back/circle */
122	[0x5f] = KEY_SCREEN,		/* view/square */
123	[0x5e] = BTN_0,			/* cross */
124	[0x54] = KEY_UP,
125	[0x56] = KEY_DOWN,
126	[0x57] = KEY_LEFT,
127	[0x55] = KEY_RIGHT,
128	[0x0b] = KEY_ENTER,
129	[0x5a] = BTN_TL,		/* L1 */
130	[0x58] = BTN_TL2,		/* L2 */
131	[0x51] = BTN_THUMBL,		/* L3 */
132	[0x5b] = BTN_TR,		/* R1 */
133	[0x59] = BTN_TR2,		/* R2 */
134	[0x52] = BTN_THUMBR,		/* R3 */
135	[0x43] = KEY_HOMEPAGE,		/* PS button */
136	[0x50] = KEY_SELECT,
137	[0x53] = BTN_START,
138	[0x33] = KEY_REWIND,		/* scan back */
139	[0x32] = KEY_PLAY,
140	[0x34] = KEY_FORWARD,		/* scan forward */
141	[0x30] = KEY_PREVIOUS,
142	[0x38] = KEY_STOP,
143	[0x31] = KEY_NEXT,
144	[0x60] = KEY_FRAMEBACK,		/* slow/step back */
145	[0x39] = KEY_PAUSE,
146	[0x61] = KEY_FRAMEFORWARD,	/* slow/step forward */
147	[0xff] = KEY_MAX,
148};
149
150static int ps3remote_decode(char *buff, int size, unsigned int *value)
151{
152	static unsigned int lastkey = 0;
153	static unsigned int lastmask = 0;
154	unsigned int i, mask;
155	int retval;
156	guint8 key;
157
158	if (size < 12) {
159		error("Got a shorter packet! (size %i)\n", size);
160		return KEY_RESERVED;
161	}
162
163	mask = (buff[2] << 16) + (buff[3] << 8) + buff[4];
164	key = buff[5];
165
166	/* first, check flags */
167	for (i = 0; i < 24; i++) {
168		if ((lastmask & (1 << i)) == (mask & (1 << i)))
169			continue;
170		if (ps3remote_bits[i] == 0)
171			goto error;
172		retval = ps3remote_keymap[ps3remote_bits[i]];
173		if (mask & (1 << i))
174			/* key pressed */
175			*value = 1;
176		else
177			/* key released */
178			*value = 0;
179
180		goto out;
181	}
182
183	*value = buff[11];
184	if (buff[11] == 1) {
185		retval = ps3remote_keymap[key];
186	} else
187		retval = lastkey;
188
189	if (retval == KEY_RESERVED)
190		goto error;
191	if (retval == KEY_MAX)
192		return retval;
193
194	lastkey = retval;
195
196out:
197	fflush(stdout);
198
199	lastmask = mask;
200
201	return retval;
202
203error:
204	error("ps3remote: unrecognized sequence [%#x][%#x][%#x][%#x] [%#x],"
205			"last: [%#x][%#x][%#x][%#x]",
206			buff[2], buff[3], buff[4], buff[5], buff[11],
207				lastmask >> 16, lastmask >> 8 & 0xff,
208						lastmask & 0xff, lastkey);
209	return -1;
210}
211
212static gboolean ps3remote_event(GIOChannel *chan, GIOCondition cond,
213				gpointer data)
214{
215	struct fake_input *fake = data;
216	struct uinput_event event;
217	unsigned int key, value = 0;
218	gsize size;
219	char buff[50];
220
221	if (cond & G_IO_NVAL)
222		return FALSE;
223
224	if (cond & (G_IO_HUP | G_IO_ERR)) {
225		error("Hangup or error on rfcomm server socket");
226		goto failed;
227	}
228
229	memset(buff, 0, sizeof(buff));
230
231	if (g_io_channel_read(chan, buff, sizeof(buff), &size) !=
232							G_IO_ERROR_NONE) {
233		error("IO Channel read error");
234		goto failed;
235	}
236
237	key = ps3remote_decode(buff, size, &value);
238	if (key == KEY_RESERVED) {
239		error("Got invalid key from decode");
240		goto failed;
241	} else if (key == KEY_MAX)
242		return TRUE;
243
244	memset(&event, 0, sizeof(event));
245	gettimeofday(&event.time, NULL);
246	event.type = EV_KEY;
247	event.code = key;
248	event.value = value;
249	if (write(fake->uinput, &event, sizeof(event)) != sizeof(event)) {
250		error("Error writing to uinput device");
251		goto failed;
252	}
253
254	memset(&event, 0, sizeof(event));
255	gettimeofday(&event.time, NULL);
256	event.type = EV_SYN;
257	event.code = SYN_REPORT;
258	if (write(fake->uinput, &event, sizeof(event)) != sizeof(event)) {
259		error("Error writing to uinput device");
260		goto failed;
261	}
262
263	return TRUE;
264
265failed:
266	ioctl(fake->uinput, UI_DEV_DESTROY);
267	close(fake->uinput);
268	fake->uinput = -1;
269	g_io_channel_unref(fake->io);
270
271	return FALSE;
272}
273
274static int ps3remote_setup_uinput(struct fake_input *fake,
275				  struct fake_hid *fake_hid)
276{
277	struct uinput_dev dev;
278	int i;
279
280	fake->uinput = open("/dev/input/uinput", O_RDWR);
281	if (fake->uinput < 0) {
282		fake->uinput = open("/dev/uinput", O_RDWR);
283		if (fake->uinput < 0) {
284			fake->uinput = open("/dev/misc/uinput", O_RDWR);
285			if (fake->uinput < 0) {
286				error("Error opening uinput device file");
287				return 1;
288			}
289		}
290	}
291
292	memset(&dev, 0, sizeof(dev));
293	snprintf(dev.name, sizeof(dev.name), "%s", "PS3 Remote Controller");
294	dev.id.bustype = BUS_BLUETOOTH;
295	dev.id.vendor = fake_hid->vendor;
296	dev.id.product = fake_hid->product;
297
298	if (write(fake->uinput, &dev, sizeof(dev)) != sizeof(dev)) {
299		error("Error creating uinput device");
300		goto err;
301	}
302
303	/* enabling key events */
304	if (ioctl(fake->uinput, UI_SET_EVBIT, EV_KEY) < 0) {
305		error("Error enabling uinput device key events");
306		goto err;
307	}
308
309	/* enabling keys */
310	for (i = 0; i < 256; i++)
311		if (ps3remote_keymap[i] != KEY_RESERVED)
312			if (ioctl(fake->uinput, UI_SET_KEYBIT,
313						ps3remote_keymap[i]) < 0) {
314				error("Error enabling uinput key %i",
315							ps3remote_keymap[i]);
316				goto err;
317			}
318
319	/* creating the device */
320	if (ioctl(fake->uinput, UI_DEV_CREATE) < 0) {
321		error("Error creating uinput device");
322		goto err;
323	}
324
325	return 0;
326
327err:
328	close(fake->uinput);
329	return 1;
330}
331
332static gboolean fake_hid_common_connect(struct fake_input *fake, GError **err)
333{
334	return TRUE;
335}
336
337static int fake_hid_common_disconnect(struct fake_input *fake)
338{
339	return 0;
340}
341
342static struct fake_hid fake_hid_table[] = {
343	/* Sony PS3 remote device */
344	{
345		.vendor		= 0x054c,
346		.product	= 0x0306,
347		.connect	= fake_hid_common_connect,
348		.disconnect	= fake_hid_common_disconnect,
349		.event		= ps3remote_event,
350		.setup_uinput	= ps3remote_setup_uinput,
351	},
352
353	{ },
354};
355
356static inline int fake_hid_match_device(uint16_t vendor, uint16_t product,
357							struct fake_hid *fhid)
358{
359	return vendor == fhid->vendor && product == fhid->product;
360}
361
362struct fake_hid *get_fake_hid(uint16_t vendor, uint16_t product)
363{
364	int i;
365
366	for (i = 0; fake_hid_table[i].vendor != 0; i++)
367		if (fake_hid_match_device(vendor, product, &fake_hid_table[i]))
368			return &fake_hid_table[i];
369
370	return NULL;
371}
372
373int fake_hid_connadd(struct fake_input *fake, GIOChannel *intr_io,
374						struct fake_hid *fake_hid)
375{
376	if (fake_hid->setup_uinput(fake, fake_hid)) {
377		error("Error setting up uinput");
378		return ENOMEM;
379	}
380
381	fake->io = g_io_channel_ref(intr_io);
382	g_io_channel_set_close_on_unref(fake->io, TRUE);
383	g_io_add_watch(fake->io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
384					(GIOFunc) fake_hid->event, fake);
385
386	return 0;
387}
388