3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2009 Bastien Nocera <hadess@hadess.net>
6 * Copyright (C) 2011 Antonio Ospite <ospite@studenti.unina.it>
7 * Copyright (C) 2013 Szymon Janc <szymon.janc@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include <sys/ioctl.h>
36 #include <linux/hidraw.h>
37 #include <linux/input.h>
41 #include "lib/bluetooth.h"
45 #include "src/adapter.h"
46 #include "src/device.h"
47 #include "src/plugin.h"
49 #include "src/shared/util.h"
59 .name = "PLAYSTATION(R)3 Controller",
66 .name = "Navigation Controller",
79 static void leds_data_destroy(struct leds_data *data)
81 free(data->syspath_prefix);
85 static struct udev *ctx = NULL;
86 static struct udev_monitor *monitor = NULL;
87 static guint watch_id = 0;
89 static int get_device_bdaddr(int fd, bdaddr_t *bdaddr)
94 memset(buf, 0, sizeof(buf));
98 ret = ioctl(fd, HIDIOCGFEATURE(sizeof(buf)), buf);
100 error("sixaxis: failed to read device address (%s)",
105 baswap(bdaddr, (bdaddr_t *) (buf + 4));
110 static int get_master_bdaddr(int fd, bdaddr_t *bdaddr)
115 memset(buf, 0, sizeof(buf));
119 ret = ioctl(fd, HIDIOCGFEATURE(sizeof(buf)), buf);
121 error("sixaxis: failed to read master address (%s)",
126 baswap(bdaddr, (bdaddr_t *) (buf + 2));
131 static int set_master_bdaddr(int fd, const bdaddr_t *bdaddr)
139 baswap((bdaddr_t *) (buf + 2), bdaddr);
141 ret = ioctl(fd, HIDIOCSFEATURE(sizeof(buf)), buf);
143 error("sixaxis: failed to write master address (%s)",
149 static uint8_t calc_leds_bitmap(int number)
153 /* TODO we could support up to 10 (1 + 2 + 3 + 4) */
162 bitmap |= 0x01 << number;
167 static void set_leds_hidraw(int fd, uint8_t leds_bitmap)
170 * the total time the led is active (0xff means forever)
171 * | duty_length: cycle time in deciseconds (0 - "blink very fast")
172 * | | ??? (Maybe a phase shift or duty_length multiplier?)
173 * | | | % of duty_length led is off (0xff means 100%)
174 * | | | | % of duty_length led is on (0xff means 100%)
176 * 0xff, 0x27, 0x10, 0x00, 0x32,
178 uint8_t leds_report[] = {
180 0x00, 0x00, 0x00, 0x00, 0x00, /* rumble values TBD */
181 0x00, 0x00, 0x00, 0x00, 0x00, /* LED_1=0x02, LED_2=0x04 ... */
182 0xff, 0x27, 0x10, 0x00, 0x32, /* LED_4 */
183 0xff, 0x27, 0x10, 0x00, 0x32, /* LED_3 */
184 0xff, 0x27, 0x10, 0x00, 0x32, /* LED_2 */
185 0xff, 0x27, 0x10, 0x00, 0x32, /* LED_1 */
186 0x00, 0x00, 0x00, 0x00, 0x00,
190 leds_report[10] = leds_bitmap;
192 ret = write(fd, leds_report, sizeof(leds_report));
193 if (ret == sizeof(leds_report))
197 error("sixaxis: failed to set LEDS (%s)", strerror(errno));
199 error("sixaxis: failed to set LEDS (%d bytes written)", ret);
202 static bool set_leds_sysfs(struct leds_data *data)
206 if (!data->syspath_prefix)
209 /* start from 1, LED0 is never used */
210 for (i = 1; i <= 4; i++) {
211 char path[PATH_MAX] = { 0 };
216 snprintf(path, PATH_MAX, "%s%d/brightness",
217 data->syspath_prefix, i);
219 fd = open(path, O_WRONLY);
221 error("sixaxis: cannot open %s (%s)", path,
226 buf[0] = '0' + !!(data->bitmap & (1 << i));
227 ret = write(fd, buf, sizeof(buf));
229 if (ret != sizeof(buf))
236 static gboolean setup_leds(GIOChannel *channel, GIOCondition cond,
239 struct leds_data *data = user_data;
244 if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
247 if(!set_leds_sysfs(data)) {
248 int fd = g_io_channel_unix_get_fd(channel);
249 set_leds_hidraw(fd, data->bitmap);
253 leds_data_destroy(data);
258 static bool setup_device(int fd, int index, struct btd_adapter *adapter)
260 char device_addr[18], master_addr[18], adapter_addr[18];
261 bdaddr_t device_bdaddr, master_bdaddr;
262 const bdaddr_t *adapter_bdaddr;
263 struct btd_device *device;
265 if (get_device_bdaddr(fd, &device_bdaddr) < 0)
268 if (get_master_bdaddr(fd, &master_bdaddr) < 0)
271 /* This can happen if controller was plugged while already connected
272 * eg. to charge up battery.
273 * Don't set LEDs in that case, hence return false */
274 device = btd_adapter_find_device(adapter, &device_bdaddr,
276 if (device && btd_device_is_connected(device))
279 adapter_bdaddr = btd_adapter_get_address(adapter);
281 if (bacmp(adapter_bdaddr, &master_bdaddr)) {
282 if (set_master_bdaddr(fd, adapter_bdaddr) < 0)
286 ba2str(&device_bdaddr, device_addr);
287 ba2str(&master_bdaddr, master_addr);
288 ba2str(adapter_bdaddr, adapter_addr);
289 DBG("remote %s old_master %s new_master %s",
290 device_addr, master_addr, adapter_addr);
292 device = btd_adapter_get_device(adapter, &device_bdaddr, BDADDR_BREDR);
294 if (g_slist_find_custom(btd_device_get_uuids(device), HID_UUID,
295 (GCompareFunc)strcasecmp)) {
296 DBG("device %s already known, skipping", device_addr);
300 info("sixaxis: setting up new device");
302 btd_device_device_set_name(device, devices[index].name);
303 btd_device_set_pnpid(device, devices[index].source, devices[index].vid,
304 devices[index].pid, devices[index].version);
305 btd_device_set_temporary(device, false);
310 static int get_js_number(struct udev_device *udevice)
312 struct udev_list_entry *devices, *dev_list_entry;
313 struct udev_enumerate *enumerate;
314 struct udev_device *hid_parent;
315 const char *hidraw_node;
319 hid_parent = udev_device_get_parent_with_subsystem_devtype(udevice,
323 * Look for HID_UNIQ first for the correct behavior via BT, if
324 * HID_UNIQ is not available it means the USB bus is being used and we
325 * can rely on HID_PHYS.
327 hid_id = udev_device_get_property_value(hid_parent, "HID_UNIQ");
329 hid_id = udev_device_get_property_value(hid_parent,
332 hidraw_node = udev_device_get_devnode(udevice);
333 if (!hid_id || !hidraw_node)
336 enumerate = udev_enumerate_new(udev_device_get_udev(udevice));
337 udev_enumerate_add_match_sysname(enumerate, "js*");
338 udev_enumerate_scan_devices(enumerate);
339 devices = udev_enumerate_get_list_entry(enumerate);
341 udev_list_entry_foreach(dev_list_entry, devices) {
342 struct udev_device *input_parent;
343 struct udev_device *js_dev;
344 const char *input_id;
347 devname = udev_list_entry_get_name(dev_list_entry);
348 js_dev = udev_device_new_from_syspath(
349 udev_device_get_udev(udevice),
352 input_parent = udev_device_get_parent_with_subsystem_devtype(
353 js_dev, "input", NULL);
357 /* check if this is the joystick relative to the hidraw device
359 input_id = udev_device_get_sysattr_value(input_parent, "uniq");
362 * A strlen() check is needed because input device over USB
363 * have the UNIQ attribute defined but with an empty value.
365 if (!input_id || strlen(input_id) == 0)
366 input_id = udev_device_get_sysattr_value(input_parent,
372 if (!strcmp(input_id, hid_id)) {
373 number = atoi(udev_device_get_sysnum(js_dev));
375 /* joystick numbers start from 0, leds from 1 */
378 udev_device_unref(js_dev);
382 udev_device_unref(js_dev);
385 udev_enumerate_unref(enumerate);
390 static char *get_leds_syspath_prefix(struct udev_device *udevice)
392 struct udev_list_entry *dev_list_entry;
393 struct udev_enumerate *enumerate;
394 struct udev_device *hid_parent;
396 char *syspath_prefix;
398 hid_parent = udev_device_get_parent_with_subsystem_devtype(udevice,
401 enumerate = udev_enumerate_new(udev_device_get_udev(udevice));
402 udev_enumerate_add_match_parent(enumerate, hid_parent);
403 udev_enumerate_add_match_subsystem(enumerate, "leds");
404 udev_enumerate_scan_devices(enumerate);
406 dev_list_entry = udev_enumerate_get_list_entry(enumerate);
407 if (!dev_list_entry) {
408 syspath_prefix = NULL;
412 syspath = udev_list_entry_get_name(dev_list_entry);
415 * All the sysfs paths of the LEDs have the same structure, just the
416 * number changes, so strip it and store only the common prefix.
418 * Subtracting 1 here means assuming that the LED number is a single
419 * digit, this is safe as the kernel driver only exposes 4 LEDs.
421 syspath_prefix = strndup(syspath, strlen(syspath) - 1);
424 udev_enumerate_unref(enumerate);
426 return syspath_prefix;
429 static struct leds_data *get_leds_data(struct udev_device *udevice)
431 struct leds_data *data;
434 number = get_js_number(udevice);
435 DBG("number %d", number);
437 data = malloc0(sizeof(*data));
441 data->bitmap = calc_leds_bitmap(number);
442 if (data->bitmap == 0) {
443 leds_data_destroy(data);
448 * It's OK if this fails, set_leds_hidraw() will be used in
449 * case data->syspath_prefix is NULL.
451 data->syspath_prefix = get_leds_syspath_prefix(udevice);
456 static int get_supported_device(struct udev_device *udevice, uint16_t *bus)
458 struct udev_device *hid_parent;
463 hid_parent = udev_device_get_parent_with_subsystem_devtype(udevice,
468 hid_id = udev_device_get_property_value(hid_parent, "HID_ID");
470 if (sscanf(hid_id, "%hx:%hx:%hx", bus, &vid, &pid) != 3)
473 for (i = 0; i < G_N_ELEMENTS(devices); i++) {
474 if (devices[i].vid == vid && devices[i].pid == pid)
481 static void device_added(struct udev_device *udevice)
483 struct btd_adapter *adapter;
489 adapter = btd_adapter_get_default();
493 index = get_supported_device(udevice, &bus);
497 info("sixaxis: compatible device connected: %s (%04X:%04X)",
498 devices[index].name, devices[index].vid,
501 fd = open(udev_device_get_devnode(udevice), O_RDWR);
505 io = g_io_channel_unix_new(fd);
509 if (!setup_device(fd, index, adapter))
514 /* wait for events before setting leds */
515 g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
516 setup_leds, get_leds_data(udevice));
520 DBG("uknown bus type (%u)", bus);
524 g_io_channel_set_close_on_unref(io, TRUE);
525 g_io_channel_unref(io);
528 static gboolean monitor_watch(GIOChannel *source, GIOCondition condition,
531 struct udev_device *udevice;
533 udevice = udev_monitor_receive_device(monitor);
537 if (!g_strcmp0(udev_device_get_action(udevice), "add"))
538 device_added(udevice);
540 udev_device_unref(udevice);
545 static int sixaxis_init(void)
555 monitor = udev_monitor_new_from_netlink(ctx, "udev");
563 /* Listen for newly connected hidraw interfaces */
564 udev_monitor_filter_add_match_subsystem_devtype(monitor, "hidraw",
566 udev_monitor_enable_receiving(monitor);
568 channel = g_io_channel_unix_new(udev_monitor_get_fd(monitor));
569 watch_id = g_io_add_watch(channel, G_IO_IN, monitor_watch, NULL);
570 g_io_channel_unref(channel);
575 static void sixaxis_exit(void)
579 g_source_remove(watch_id);
582 udev_monitor_unref(monitor);
589 BLUETOOTH_PLUGIN_DEFINE(sixaxis, VERSION, BLUETOOTH_PLUGIN_PRIORITY_LOW,
590 sixaxis_init, sixaxis_exit)