2 * hid2hci : switch the radio on devices that support
3 * it from HID to HCI and back
5 * Copyright (C) 2003-2010 Marcel Holtmann <marcel@holtmann.org>
6 * Copyright (C) 2008-2009 Mario Limonciello <mario_limonciello@dell.com>
7 * Copyright (C) 2009-2011 Kay Sievers <kay.sievers@vrfy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <sys/ioctl.h>
31 #include <linux/types.h>
32 #include <linux/hiddev.h>
42 static int usb_switch_csr(struct usb_dev_handle *dev, enum mode mode)
46 err = usb_control_msg(dev,
47 USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
48 0, mode, 0, NULL, 0, 10000);
53 if (errno == ETIMEDOUT)
59 static int hid_logitech_send_report(int fd, const char *buf, size_t size)
61 struct hiddev_report_info rinfo;
62 struct hiddev_usage_ref uref;
66 for (i = 0; i < size; i++) {
67 memset(&uref, 0, sizeof(uref));
68 uref.report_type = HID_REPORT_TYPE_OUTPUT;
69 uref.report_id = 0x10;
72 uref.usage_code = 0xff000001;
73 uref.value = buf[i] & 0x000000ff;
74 err = ioctl(fd, HIDIOCSUSAGE, &uref);
79 memset(&rinfo, 0, sizeof(rinfo));
80 rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
81 rinfo.report_id = 0x10;
83 err = ioctl(fd, HIDIOCSREPORT, &rinfo);
88 static int hid_switch_logitech(const char *filename)
90 char rep1[] = { 0xff, 0x80, 0x80, 0x01, 0x00, 0x00 };
91 char rep2[] = { 0xff, 0x80, 0x00, 0x00, 0x30, 0x00 };
92 char rep3[] = { 0xff, 0x81, 0x80, 0x00, 0x00, 0x00 };
96 fd = open(filename, O_RDWR);
100 err = ioctl(fd, HIDIOCINITREPORT, 0);
104 err = hid_logitech_send_report(fd, rep1, sizeof(rep1));
108 err = hid_logitech_send_report(fd, rep2, sizeof(rep2));
112 err = hid_logitech_send_report(fd, rep3, sizeof(rep3));
118 static int usb_switch_dell(struct usb_dev_handle *dev, enum mode mode)
120 char report[] = { 0x7f, 0x00, 0x00, 0x00 };
132 /* Don't need to check return, as might not be in use */
133 usb_detach_kernel_driver_np(dev, 0);
135 if (usb_claim_interface(dev, 0) < 0)
138 err = usb_control_msg(dev,
139 USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
140 USB_REQ_SET_CONFIGURATION, 0x7f | (0x03 << 8), 0,
141 report, sizeof(report), 5000);
147 if (errno == ETIMEDOUT)
154 * libusb needs to scan and open all devices, just to to find the
155 * device we already have. This should be fixed in libusb.
157 static struct usb_device *usb_device_open_from_udev(struct udev_device *usb_dev)
164 str = udev_device_get_sysattr_value(usb_dev, "busnum");
167 busnum = strtol(str, NULL, 0);
169 str = udev_device_get_sysattr_value(usb_dev, "devnum");
172 devnum = strtol(str, NULL, 0);
178 for (bus = usb_get_busses(); bus; bus = bus->next) {
179 struct usb_device *dev;
181 if (strtol(bus->dirname, NULL, 10) != busnum)
184 for (dev = bus->devices; dev; dev = dev->next) {
185 if (dev->devnum == devnum)
193 static struct usb_dev_handle *find_device(struct udev_device *udev_dev)
195 struct usb_device *dev;
197 dev = usb_device_open_from_udev(udev_dev);
200 return usb_open(dev);
203 static void usage(const char *error)
206 fprintf(stderr,"\n%s\n", error);
208 printf("hid2hci - Bluetooth HID to HCI mode switching utility\n\n");
210 printf("Usage: hid2hci [options]\n"
211 " --mode= mode to switch to [hid|hci] (default hci)\n"
212 " --devpath= sys device path\n"
213 " --method= method to use to switch [csr|logitech-hid|dell]\n"
217 int main(int argc, char *argv[])
219 static const struct option options[] = {
220 { "help", no_argument, NULL, 'h' },
221 { "mode", required_argument, NULL, 'm' },
222 { "devpath", required_argument, NULL, 'p' },
223 { "method", required_argument, NULL, 'M' },
231 } method = METHOD_UNDEF;
233 struct udev_device *udev_dev = NULL;
234 char syspath[PATH_MAX];
235 int (*usb_switch)(struct usb_dev_handle *dev, enum mode mode) = NULL;
236 enum mode mode = HCI;
237 const char *devpath = NULL;
244 option = getopt_long(argc, argv, "m:p:M:h", options, NULL);
250 if (!strcmp(optarg, "hid")) {
252 } else if (!strcmp(optarg, "hci")) {
255 usage("error: undefined radio mode\n");
263 if (!strcmp(optarg, "csr")) {
265 usb_switch = usb_switch_csr;
266 } else if (!strcmp(optarg, "logitech-hid")) {
267 method = METHOD_LOGITECH_HID;
268 } else if (!strcmp(optarg, "dell")) {
269 method = METHOD_DELL;
270 usb_switch = usb_switch_dell;
272 usage("error: undefined switching method\n");
281 if (!devpath || method == METHOD_UNDEF) {
282 usage("error: --devpath= and --method= must be defined\n");
290 snprintf(syspath, sizeof(syspath), "%s/%s", udev_get_sys_path(udev), devpath);
291 udev_dev = udev_device_new_from_syspath(udev, syspath);
292 if (udev_dev == NULL) {
293 fprintf(stderr, "error: could not find '%s'\n", devpath);
300 struct udev_device *dev;
301 struct usb_dev_handle *handle;
304 /* get the parent usb_device if needed */
306 type = udev_device_get_devtype(dev);
307 if (type == NULL || strcmp(type, "usb_device") != 0) {
308 dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
310 fprintf(stderr, "error: could not find usb_device for '%s'\n", devpath);
315 handle = find_device(dev);
316 if (handle == NULL) {
317 fprintf(stderr, "error: unable to handle '%s'\n",
318 udev_device_get_syspath(dev));
321 err = usb_switch(handle, mode);
324 case METHOD_LOGITECH_HID: {
327 device = udev_device_get_devnode(udev_dev);
328 if (device == NULL) {
329 fprintf(stderr, "error: could not find hiddev device node\n");
332 err = hid_switch_logitech(device);
340 fprintf(stderr, "error: switching device '%s' failed.\n",
341 udev_device_get_syspath(udev_dev));
343 udev_device_unref(udev_dev);