3 * hid2hci : a tool for switching the radio on devices that support
4 * it from HID to HCI and back
6 * Copyright (C) 2003-2009 Marcel Holtmann <marcel@holtmann.org>
7 * Copyright (C) 2008-2009 Mario Limonciello <mario_limonciello@dell.com>
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
31 #include <sys/ioctl.h>
32 #include <linux/hiddev.h>
35 static char devpath[PATH_MAX + 1] = "/dev";
41 struct usb_device *dev;
47 static int switch_csr(struct device_info *devinfo)
49 struct usb_dev_handle *udev;
52 udev = usb_open(devinfo->dev);
56 err = usb_control_msg(udev,
57 USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
58 0, devinfo->mode, 0, NULL, 0, 10000);
64 if (errno == ETIMEDOUT)
73 static int send_report(int fd, const char *buf, size_t size)
75 struct hiddev_report_info rinfo;
76 struct hiddev_usage_ref uref;
80 for (i = 0; i < size; i++) {
81 memset(&uref, 0, sizeof(uref));
82 uref.report_type = HID_REPORT_TYPE_OUTPUT;
83 uref.report_id = 0x10;
86 uref.usage_code = 0xff000001;
87 uref.value = buf[i] & 0x000000ff;
88 err = ioctl(fd, HIDIOCSUSAGE, &uref);
93 memset(&rinfo, 0, sizeof(rinfo));
94 rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
95 rinfo.report_id = 0x10;
97 err = ioctl(fd, HIDIOCSREPORT, &rinfo);
102 static int switch_logitech(struct device_info *devinfo)
104 char devname[PATH_MAX + 1];
107 for (i = 0; i < 16; i++) {
108 struct hiddev_devinfo dinfo;
109 char rep1[] = { 0xff, 0x80, 0x80, 0x01, 0x00, 0x00 };
110 char rep2[] = { 0xff, 0x80, 0x00, 0x00, 0x30, 0x00 };
111 char rep3[] = { 0xff, 0x81, 0x80, 0x00, 0x00, 0x00 };
113 sprintf(devname, "%s/hiddev%d", devpath, i);
114 fd = open(devname, O_RDWR);
116 sprintf(devname, "%s/usb/hiddev%d", devpath, i);
117 fd = open(devname, O_RDWR);
119 sprintf(devname, "%s/usb/hid/hiddev%d", devpath, i);
120 fd = open(devname, O_RDWR);
126 memset(&dinfo, 0, sizeof(dinfo));
127 err = ioctl(fd, HIDIOCGDEVINFO, &dinfo);
128 if (err < 0 || (int) dinfo.busnum != atoi(devinfo->dev->bus->dirname) ||
129 (int) dinfo.devnum != atoi(devinfo->dev->filename)) {
134 err = ioctl(fd, HIDIOCINITREPORT, 0);
140 err = send_report(fd, rep1, sizeof(rep1));
146 err = send_report(fd, rep2, sizeof(rep2));
152 err = send_report(fd, rep3, sizeof(rep3));
160 static int switch_dell(struct device_info *devinfo)
162 char report[] = { 0x7f, 0x00, 0x00, 0x00 };
164 struct usb_dev_handle *handle;
167 switch (devinfo->mode) {
176 handle = usb_open(devinfo->dev);
180 /* Don't need to check return, as might not be in use */
181 usb_detach_kernel_driver_np(handle, 0);
183 if (usb_claim_interface(handle, 0) < 0) {
188 err = usb_control_msg(handle,
189 USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
190 USB_REQ_SET_CONFIGURATION, 0x7f | (0x03 << 8), 0,
191 report, sizeof(report), 5000);
197 if (errno == ETIMEDOUT)
206 static int find_device(struct device_info* devinfo)
209 struct usb_device *dev;
214 for (bus = usb_get_busses(); bus; bus = bus->next)
215 for (dev = bus->devices; dev; dev = dev->next) {
216 if (dev->descriptor.idVendor == devinfo->vendor &&
217 dev->descriptor.idProduct == devinfo->product) {
225 static int find_resuscitated_device(struct device_info* devinfo, uint8_t bInterfaceProtocol)
228 struct usb_device *dev, *child;
229 struct usb_config_descriptor config;
230 struct usb_interface interface;
231 struct usb_interface_descriptor altsetting;
233 /* Using the base device, attempt to find the child with the
234 * matching bInterfaceProtocol */
236 for (i = 0; i < dev->num_children; i++) {
237 child = dev->children[i];
238 for (j = 0; j < child->descriptor.bNumConfigurations; j++) {
239 config = child->config[j];
240 for (k = 0; k < config.bNumInterfaces; k++) {
241 interface = config.interface[k];
242 for (l = 0; l < interface.num_altsetting; l++) {
243 altsetting = interface.altsetting[l];
244 if (altsetting.bInterfaceProtocol == bInterfaceProtocol) {
245 devinfo->dev = child;
255 static void usage(char* error)
258 fprintf(stderr,"\n%s\n", error);
260 printf("hid2hci - Bluetooth HID to HCI mode switching utility\n\n");
263 "\thid2hci [options]\n"
267 "\t-h, --help Display help\n"
268 "\t-q, --quiet Don't display any messages\n"
269 "\t-r, --mode= Mode to switch to [hid, hci]\n"
270 "\t-v, --vendor= Vendor ID to act upon\n"
271 "\t-p, --product= Product ID to act upon\n"
272 "\t-m, --method= Method to use to switch [csr, logitech, dell]\n"
273 "\t-s, --resuscitate= Find the child device with this bInterfaceProtocol to run on \n"
279 int main(int argc, char *argv[])
281 static const struct option options[] = {
282 { "help", no_argument, NULL, 'h' },
283 { "quiet", no_argument, NULL, 'q' },
284 { "mode", required_argument, NULL, 'r' },
285 { "vendor", required_argument, NULL, 'v' },
286 { "product", required_argument, NULL, 'p' },
287 { "method", required_argument, NULL, 'm' },
288 { "resuscitate", required_argument, NULL, 's' },
291 struct device_info dev = { NULL, HCI, 0, 0 };
293 int (*method)(struct device_info *dev) = NULL;
294 uint8_t resuscitate = 0;
296 while ((opt = getopt_long(argc, argv, "+s:r:v:p:m:qh", options, NULL)) != -1) {
299 if (optarg && !strcmp(optarg, "hid"))
301 else if (optarg && !strcmp(optarg, "hci"))
304 usage("ERROR: Undefined radio mode\n");
307 sscanf(optarg, "%4hx", &dev.vendor);
310 sscanf(optarg, "%4hx", &dev.product);
313 if (optarg && !strcmp(optarg, "csr"))
315 else if (optarg && !strcmp(optarg, "logitech"))
316 method = switch_logitech;
317 else if (optarg && !strcmp(optarg, "dell"))
318 method = switch_dell;
320 usage("ERROR: Undefined switching method\n");
326 sscanf(optarg, "%2hx", (short unsigned int*) &resuscitate);
335 if (!quiet && (!dev.vendor || !dev.product || !method))
336 usage("ERROR: Vendor ID, Product ID, and Switching Method must all be defined.\n");
344 if (!find_device(&dev)) {
346 fprintf(stderr, "Device %04x:%04x not found on USB bus.\n",
347 dev.vendor, dev.product);
351 if (resuscitate && !find_resuscitated_device(&dev, resuscitate)) {
353 fprintf(stderr, "Device %04x:%04x was unable to resucitate any child devices.\n",
354 dev.vendor,dev.product);
359 printf("Attempting to switch device %04x:%04x to %s mode ",
360 dev.vendor, dev.product, dev.mode ? "HID" : "HCI");
363 if (method(&dev) < 0 && !quiet)
364 printf("failed (%s)\n", strerror(errno));
366 printf("was successful\n");