chiark / gitweb /
hid2hci: support to hid2hci for recovering Dell BT devices after S3
[elogind.git] / extras / hid2hci / hid2hci.c
1 /*
2  *
3  *  hid2hci : a tool for switching the radio on devices that support
4  *                      it from HID to HCI and back
5  *
6  *  Copyright (C) 2003-2009  Marcel Holtmann <marcel@holtmann.org>
7  *  Copyright (C) 2008-2009  Mario Limonciello <mario_limonciello@dell.com>
8  *
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.
13  *
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.
18  *
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
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <getopt.h>
35 #include <sys/ioctl.h>
36
37 #include <usb.h>
38
39 #ifdef NEED_USB_GET_BUSSES
40 static inline struct usb_bus *usb_get_busses(void)
41 {
42         return usb_busses;
43 }
44 #endif
45
46 #ifndef USB_DIR_OUT
47 #define USB_DIR_OUT     0x00
48 #endif
49
50 static char devpath[PATH_MAX + 1] = "/dev";
51
52 struct hiddev_devinfo {
53         unsigned int bustype;
54         unsigned int busnum;
55         unsigned int devnum;
56         unsigned int ifnum;
57         short vendor;
58         short product;
59         short version;
60         unsigned num_applications;
61 };
62
63 struct hiddev_report_info {
64         unsigned report_type;
65         unsigned report_id;
66         unsigned num_fields;
67 };
68
69 typedef __signed__ int __s32;
70
71 struct hiddev_usage_ref {
72         unsigned report_type;
73         unsigned report_id;
74         unsigned field_index;
75         unsigned usage_index;
76         unsigned usage_code;
77         __s32 value;
78 };
79
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)
84
85 #define HID_REPORT_TYPE_OUTPUT  2
86
87 #define HCI 0
88 #define HID 1
89
90 struct device_info {
91         struct usb_device *dev;
92         int mode;
93         uint16_t vendor;
94         uint16_t product;
95 };
96
97 static int switch_csr(struct device_info *devinfo)
98 {
99         struct usb_dev_handle *udev;
100         int err;
101
102         udev = usb_open(devinfo->dev);
103         if (!udev)
104                 return -errno;
105
106         err = usb_control_msg(udev, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
107                                 0, devinfo->mode, 0, NULL, 0, 10000);
108
109         if (err == 0) {
110                 err = -1;
111                 errno = EALREADY;
112         } else {
113                 if (errno == ETIMEDOUT)
114                         err = 0;
115         }
116
117         usb_close(udev);
118
119         return err;
120 }
121
122 static int send_report(int fd, const char *buf, size_t size)
123 {
124         struct hiddev_report_info rinfo;
125         struct hiddev_usage_ref uref;
126         unsigned int i;
127         int err;
128
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);
138                 if (err < 0)
139                         return err;
140         }
141
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);
147
148         return err;
149 }
150
151 static int switch_logitech(struct device_info *devinfo)
152 {
153         char devname[PATH_MAX + 1];
154         int i, fd, err = -1;
155
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 };
161
162                 sprintf(devname, "%s/hiddev%d", devpath, i);
163                 fd = open(devname, O_RDWR);
164                 if (fd < 0) {
165                         sprintf(devname, "%s/usb/hiddev%d", devpath, i);
166                         fd = open(devname, O_RDWR);
167                         if (fd < 0) {
168                                 sprintf(devname, "%s/usb/hid/hiddev%d", devpath, i);
169                                 fd = open(devname, O_RDWR);
170                                 if (fd < 0)
171                                         continue;
172                         }
173                 }
174
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)) {
179                         close(fd);
180                         continue;
181                 }
182
183                 err = ioctl(fd, HIDIOCINITREPORT, 0);
184                 if (err < 0) {
185                         close(fd);
186                         break;
187                 }
188
189                 err = send_report(fd, rep1, sizeof(rep1));
190                 if (err < 0) {
191                         close(fd);
192                         break;
193                 }
194
195                 err = send_report(fd, rep2, sizeof(rep2));
196                 if (err < 0) {
197                         close(fd);
198                         break;
199                 }
200
201                 err = send_report(fd, rep3, sizeof(rep3));
202                 close(fd);
203                 break;
204         }
205
206         return err;
207 }
208
209 static int switch_dell(struct device_info *devinfo)
210 {
211         char report[] = { 0x7f, 0x00, 0x00, 0x00 };
212
213         struct usb_dev_handle *handle;
214         int err;
215
216         switch (devinfo->mode) {
217         case HCI:
218                 report[1] = 0x13;
219                 break;
220         case HID:
221                 report[1] = 0x14;
222                 break;
223         }
224
225         handle = usb_open(devinfo->dev);
226         if (!handle)
227                 return -EIO;
228
229         /* Don't need to check return, as might not be in use */
230         usb_detach_kernel_driver_np(handle, 0);
231
232         if (usb_claim_interface(handle, 0) < 0) {
233                 usb_close(handle);
234                 return -EIO;
235         }
236
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);
241
242         if (err == 0) {
243                 err = -1;
244                 errno = EALREADY;
245         } else {
246                 if (errno == ETIMEDOUT)
247                         err = 0;
248         }
249
250         usb_close(handle);
251
252         return err;
253 }
254
255 static int find_device(struct device_info* devinfo)
256 {
257         struct usb_bus *bus;
258         struct usb_device *dev;
259
260         usb_find_busses();
261         usb_find_devices();
262
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) {
267                                 devinfo->dev=dev;
268                                 return 1;
269                         }
270                 }
271         return 0;
272 }
273
274 static int find_resuscitated_device(struct device_info* devinfo, uint8_t bInterfaceProtocol)
275 {
276         int i,j,k,l;
277         struct usb_device *dev, *child;
278         struct usb_config_descriptor config;
279         struct usb_interface interface;
280         struct usb_interface_descriptor altsetting;
281
282         /* Using the base device, attempt to find the child with the
283          * matching bInterfaceProtocol */
284         dev = devinfo->dev;
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;
295                                                 return 1;
296                                         }
297                                 }
298                         }
299                 }
300         }
301         return 0;
302 }
303
304 static void usage(char* error)
305 {
306         if (error)
307                 fprintf(stderr,"\n%s\n", error);
308         else
309                 printf("hid2hci - Bluetooth HID to HCI mode switching utility\n\n");
310
311         printf("Usage:\n"
312                 "\thid2hci [options]\n"
313                 "\n");
314
315         printf("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"
323                 "\n");
324         if (error)
325                 exit(1);
326 }
327
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' },
336         { 0, 0, 0, 0 }
337 };
338
339 int main(int argc, char *argv[])
340 {
341         struct device_info dev = { NULL, HCI, 0, 0 };
342         int opt, quiet = 0;
343         int (*method)(struct device_info *dev) = NULL;
344         uint8_t resuscitate = 0;
345
346         while ((opt = getopt_long(argc, argv, "+s:r:v:p:m:qh", main_options, NULL)) != -1) {
347                 switch (opt) {
348                 case 'r':
349                         if (optarg && !strcmp(optarg, "hid"))
350                                 dev.mode = HID;
351                         else if (optarg && !strcmp(optarg, "hci"))
352                                 dev.mode = HCI;
353                         else
354                                 usage("ERROR: Undefined radio mode\n");
355                         break;
356                 case 'v':
357                         sscanf(optarg, "%4hx", &dev.vendor);
358                         break;
359                 case 'p':
360                         sscanf(optarg, "%4hx", &dev.product);
361                         break;
362                 case 'm':
363                         if (optarg && !strcmp(optarg, "csr"))
364                                 method = switch_csr;
365                         else if (optarg && !strcmp(optarg, "logitech"))
366                                 method = switch_logitech;
367                         else if (optarg && !strcmp(optarg, "dell"))
368                                 method = switch_dell;
369                         else
370                                 usage("ERROR: Undefined switching method\n");
371                         break;
372                 case 'q':
373                         quiet = 1;
374                         break;
375                 case 's':
376                         sscanf(optarg, "%2hx", (short unsigned int*) &resuscitate);
377                         break;
378                 case 'h':
379                         usage(NULL);
380                 default:
381                         exit(0);
382                 }
383         }
384
385         if (!quiet && (!dev.vendor || !dev.product || !method))
386                 usage("ERROR: Vendor ID, Product ID, and Switching Method must all be defined.\n");
387
388         argc -= optind;
389         argv += optind;
390         optind = 0;
391
392         usb_init();
393
394         if (!find_device(&dev)) {
395                 if (!quiet)
396                         fprintf(stderr, "Device %04x:%04x not found on USB bus.\n",
397                                 dev.vendor, dev.product);
398                 exit(1);
399         }
400
401         if (resuscitate && !find_resuscitated_device(&dev, resuscitate)) {
402                 if (!quiet)
403                         fprintf(stderr, "Device %04x:%04x was unable to resucitate any child devices.\n",
404                                 dev.vendor,dev.product);
405                 exit(1);
406         }
407
408         if (!quiet)
409                 printf("Attempting to switch device %04x:%04x to %s mode ",
410                         dev.vendor, dev.product, dev.mode ? "HID" : "HCI");
411         fflush(stdout);
412
413         if (method(&dev) < 0 && !quiet)
414                 printf("failed (%s)\n", strerror(errno));
415         else if (!quiet)
416                 printf("was successful\n");
417
418         return errno;
419 }