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 void usage(char* error)
277 fprintf(stderr,"\n%s\n", error);
279 printf("hid2hci - Bluetooth HID to HCI mode switching utility\n\n");
282 "\thid2hci [options]\n"
286 "\t-h, --help Display help\n"
287 "\t-q, --quiet Don't display any messages\n"
288 "\t-r, --mode= Mode to switch to [hid, hci]\n"
289 "\t-v, --vendor= Vendor ID to act upon\n"
290 "\t-p, --product= Product ID to act upon\n"
291 "\t-m, --method= Method to use to switch [csr, logitech, dell]\n"
297 static const struct option main_options[] = {
298 { "help", no_argument, 0, 'h' },
299 { "quiet", no_argument, 0, 'q' },
300 { "mode", required_argument, 0, 'r' },
301 { "vendor", required_argument, 0, 'v' },
302 { "product", required_argument, 0, 'p' },
303 { "method", required_argument, 0, 'm' },
307 int main(int argc, char *argv[])
309 struct device_info dev = { NULL, HCI, 0, 0 };
311 int (*method)(struct device_info *dev) = NULL;
313 while ((opt = getopt_long(argc, argv, "+r:v:p:m:qh", main_options, NULL)) != -1) {
316 if (optarg && !strcmp(optarg, "hid"))
318 else if (optarg && !strcmp(optarg, "hci"))
321 usage("ERROR: Undefined radio mode\n");
324 sscanf(optarg, "%4hx", &dev.vendor);
327 sscanf(optarg, "%4hx", &dev.product);
330 if (optarg && !strcmp(optarg, "csr"))
332 else if (optarg && !strcmp(optarg, "logitech"))
333 method = switch_logitech;
334 else if (optarg && !strcmp(optarg, "dell"))
335 method = switch_dell;
337 usage("ERROR: Undefined switching method\n");
349 if (!quiet && (!dev.vendor || !dev.product || !method))
350 usage("ERROR: Vendor ID, Product ID, and Switching Method must all be defined.\n");
358 if (!find_device(&dev)) {
360 fprintf(stderr, "Device %04x:%04x not found on USB bus.\n",
361 dev.vendor, dev.product);
366 printf("Attempting to switch device %04x:%04x to %s mode ",
367 dev.vendor, dev.product, dev.mode ? "HID" : "HCI");
370 if (method(&dev) < 0 && !quiet)
371 printf("failed (%s)\n", strerror(errno));
373 printf("was successful\n");