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
35 #include <sys/ioctl.h>
39 #ifdef NEED_USB_GET_BUSSES
40 static inline struct usb_bus *usb_get_busses(void)
47 #define USB_DIR_OUT 0x00
50 static char devpath[PATH_MAX + 1] = "/dev";
52 struct hiddev_devinfo {
60 unsigned num_applications;
63 struct hiddev_report_info {
69 typedef __signed__ int __s32;
71 struct hiddev_usage_ref {
80 #define HIDIOCGDEVINFO _IOR('H', 0x03, struct hiddev_devinfo)
81 #define HIDIOCINITREPORT _IO('H', 0x05)
82 #define HIDIOCSREPORT _IOW('H', 0x08, struct hiddev_report_info)
83 #define HIDIOCSUSAGE _IOW('H', 0x0C, struct hiddev_usage_ref)
85 #define HID_REPORT_TYPE_OUTPUT 2
91 struct usb_device *dev;
97 static int switch_csr(struct device_info *devinfo)
99 struct usb_dev_handle *udev;
102 udev = usb_open(devinfo->dev);
106 err = usb_control_msg(udev, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
107 0, devinfo->mode, 0, NULL, 0, 10000);
113 if (errno == ETIMEDOUT)
122 static int send_report(int fd, const char *buf, size_t size)
124 struct hiddev_report_info rinfo;
125 struct hiddev_usage_ref uref;
129 for (i = 0; i < size; i++) {
130 memset(&uref, 0, sizeof(uref));
131 uref.report_type = HID_REPORT_TYPE_OUTPUT;
132 uref.report_id = 0x10;
133 uref.field_index = 0;
134 uref.usage_index = i;
135 uref.usage_code = 0xff000001;
136 uref.value = buf[i] & 0x000000ff;
137 err = ioctl(fd, HIDIOCSUSAGE, &uref);
142 memset(&rinfo, 0, sizeof(rinfo));
143 rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
144 rinfo.report_id = 0x10;
145 rinfo.num_fields = 1;
146 err = ioctl(fd, HIDIOCSREPORT, &rinfo);
151 static int switch_logitech(struct device_info *devinfo)
153 char devname[PATH_MAX + 1];
156 for (i = 0; i < 16; i++) {
157 struct hiddev_devinfo dinfo;
158 char rep1[] = { 0xff, 0x80, 0x80, 0x01, 0x00, 0x00 };
159 char rep2[] = { 0xff, 0x80, 0x00, 0x00, 0x30, 0x00 };
160 char rep3[] = { 0xff, 0x81, 0x80, 0x00, 0x00, 0x00 };
162 sprintf(devname, "%s/hiddev%d", devpath, i);
163 fd = open(devname, O_RDWR);
165 sprintf(devname, "%s/usb/hiddev%d", devpath, i);
166 fd = open(devname, O_RDWR);
168 sprintf(devname, "%s/usb/hid/hiddev%d", devpath, i);
169 fd = open(devname, O_RDWR);
175 memset(&dinfo, 0, sizeof(dinfo));
176 err = ioctl(fd, HIDIOCGDEVINFO, &dinfo);
177 if (err < 0 || (int) dinfo.busnum != atoi(devinfo->dev->bus->dirname) ||
178 (int) dinfo.devnum != atoi(devinfo->dev->filename)) {
183 err = ioctl(fd, HIDIOCINITREPORT, 0);
189 err = send_report(fd, rep1, sizeof(rep1));
195 err = send_report(fd, rep2, sizeof(rep2));
201 err = send_report(fd, rep3, sizeof(rep3));
209 static int switch_dell(struct device_info *devinfo)
211 char report[] = { 0x7f, 0x00, 0x00, 0x00 };
213 struct usb_dev_handle *handle;
216 switch (devinfo->mode) {
225 handle = usb_open(devinfo->dev);
229 /* Don't need to check return, as might not be in use */
230 usb_detach_kernel_driver_np(handle, 0);
232 if (usb_claim_interface(handle, 0) < 0) {
237 err = usb_control_msg(handle,
238 USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
239 USB_REQ_SET_CONFIGURATION, 0x7f | (0x03 << 8), 0,
240 report, sizeof(report), 5000);
246 if (errno == ETIMEDOUT)
255 static int find_device(struct device_info* devinfo)
258 struct usb_device *dev;
263 for (bus = usb_get_busses(); bus; bus = bus->next)
264 for (dev = bus->devices; dev; dev = dev->next) {
265 if (dev->descriptor.idVendor == devinfo->vendor &&
266 dev->descriptor.idProduct == devinfo->product) {
274 static int find_resuscitated_device(struct device_info* devinfo, uint8_t bInterfaceProtocol)
277 struct usb_device *dev, *child;
278 struct usb_config_descriptor config;
279 struct usb_interface interface;
280 struct usb_interface_descriptor altsetting;
282 /* Using the base device, attempt to find the child with the
283 * matching bInterfaceProtocol */
285 for (i = 0; i < dev->num_children; i++) {
286 child = dev->children[i];
287 for (j = 0; j < child->descriptor.bNumConfigurations; j++) {
288 config = child->config[j];
289 for (k = 0; k < config.bNumInterfaces; k++) {
290 interface = config.interface[k];
291 for (l = 0; l < interface.num_altsetting; l++) {
292 altsetting = interface.altsetting[l];
293 if (altsetting.bInterfaceProtocol == bInterfaceProtocol) {
294 devinfo->dev = child;
304 static void usage(char* error)
307 fprintf(stderr,"\n%s\n", error);
309 printf("hid2hci - Bluetooth HID to HCI mode switching utility\n\n");
312 "\thid2hci [options]\n"
316 "\t-h, --help Display help\n"
317 "\t-q, --quiet Don't display any messages\n"
318 "\t-r, --mode= Mode to switch to [hid, hci]\n"
319 "\t-v, --vendor= Vendor ID to act upon\n"
320 "\t-p, --product= Product ID to act upon\n"
321 "\t-m, --method= Method to use to switch [csr, logitech, dell]\n"
322 "\t-s, --resuscitate= Find the child device with this bInterfaceProtocol to run on \n"
328 static const struct option main_options[] = {
329 { "help", no_argument, 0, 'h' },
330 { "quiet", no_argument, 0, 'q' },
331 { "mode", required_argument, 0, 'r' },
332 { "vendor", required_argument, 0, 'v' },
333 { "product", required_argument, 0, 'p' },
334 { "method", required_argument, 0, 'm' },
335 { "resuscitate",required_argument, 0, 's' },
339 int main(int argc, char *argv[])
341 struct device_info dev = { NULL, HCI, 0, 0 };
343 int (*method)(struct device_info *dev) = NULL;
344 uint8_t resuscitate = 0;
346 while ((opt = getopt_long(argc, argv, "+s:r:v:p:m:qh", main_options, NULL)) != -1) {
349 if (optarg && !strcmp(optarg, "hid"))
351 else if (optarg && !strcmp(optarg, "hci"))
354 usage("ERROR: Undefined radio mode\n");
357 sscanf(optarg, "%4hx", &dev.vendor);
360 sscanf(optarg, "%4hx", &dev.product);
363 if (optarg && !strcmp(optarg, "csr"))
365 else if (optarg && !strcmp(optarg, "logitech"))
366 method = switch_logitech;
367 else if (optarg && !strcmp(optarg, "dell"))
368 method = switch_dell;
370 usage("ERROR: Undefined switching method\n");
376 sscanf(optarg, "%2hx", (short unsigned int*) &resuscitate);
385 if (!quiet && (!dev.vendor || !dev.product || !method))
386 usage("ERROR: Vendor ID, Product ID, and Switching Method must all be defined.\n");
394 if (!find_device(&dev)) {
396 fprintf(stderr, "Device %04x:%04x not found on USB bus.\n",
397 dev.vendor, dev.product);
401 if (resuscitate && !find_resuscitated_device(&dev, resuscitate)) {
403 fprintf(stderr, "Device %04x:%04x was unable to resucitate any child devices.\n",
404 dev.vendor,dev.product);
409 printf("Attempting to switch device %04x:%04x to %s mode ",
410 dev.vendor, dev.product, dev.mode ? "HID" : "HCI");
413 if (method(&dev) < 0 && !quiet)
414 printf("failed (%s)\n", strerror(errno));
416 printf("was successful\n");