chiark / gitweb /
hid2hci: remove hid structures and include kernel header
[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 #include <stdio.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <getopt.h>
31 #include <sys/ioctl.h>
32 #include <linux/hiddev.h>
33 #include <usb.h>
34
35 static char devpath[PATH_MAX + 1] = "/dev";
36
37 #define HCI 0
38 #define HID 1
39
40 struct device_info {
41         struct usb_device *dev;
42         int mode;
43         uint16_t vendor;
44         uint16_t product;
45 };
46
47 static int switch_csr(struct device_info *devinfo)
48 {
49         struct usb_dev_handle *udev;
50         int err;
51
52         udev = usb_open(devinfo->dev);
53         if (!udev)
54                 return -errno;
55
56         err = usb_control_msg(udev,
57                               USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
58                               0, devinfo->mode, 0, NULL, 0, 10000);
59
60         if (err == 0) {
61                 err = -1;
62                 errno = EALREADY;
63         } else {
64                 if (errno == ETIMEDOUT)
65                         err = 0;
66         }
67
68         usb_close(udev);
69
70         return err;
71 }
72
73 static int send_report(int fd, const char *buf, size_t size)
74 {
75         struct hiddev_report_info rinfo;
76         struct hiddev_usage_ref uref;
77         unsigned int i;
78         int err;
79
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;
84                 uref.field_index = 0;
85                 uref.usage_index = i;
86                 uref.usage_code  = 0xff000001;
87                 uref.value       = buf[i] & 0x000000ff;
88                 err = ioctl(fd, HIDIOCSUSAGE, &uref);
89                 if (err < 0)
90                         return err;
91         }
92
93         memset(&rinfo, 0, sizeof(rinfo));
94         rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
95         rinfo.report_id   = 0x10;
96         rinfo.num_fields  = 1;
97         err = ioctl(fd, HIDIOCSREPORT, &rinfo);
98
99         return err;
100 }
101
102 static int switch_logitech(struct device_info *devinfo)
103 {
104         char devname[PATH_MAX + 1];
105         int i, fd, err = -1;
106
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 };
112
113                 sprintf(devname, "%s/hiddev%d", devpath, i);
114                 fd = open(devname, O_RDWR);
115                 if (fd < 0) {
116                         sprintf(devname, "%s/usb/hiddev%d", devpath, i);
117                         fd = open(devname, O_RDWR);
118                         if (fd < 0) {
119                                 sprintf(devname, "%s/usb/hid/hiddev%d", devpath, i);
120                                 fd = open(devname, O_RDWR);
121                                 if (fd < 0)
122                                         continue;
123                         }
124                 }
125
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)) {
130                         close(fd);
131                         continue;
132                 }
133
134                 err = ioctl(fd, HIDIOCINITREPORT, 0);
135                 if (err < 0) {
136                         close(fd);
137                         break;
138                 }
139
140                 err = send_report(fd, rep1, sizeof(rep1));
141                 if (err < 0) {
142                         close(fd);
143                         break;
144                 }
145
146                 err = send_report(fd, rep2, sizeof(rep2));
147                 if (err < 0) {
148                         close(fd);
149                         break;
150                 }
151
152                 err = send_report(fd, rep3, sizeof(rep3));
153                 close(fd);
154                 break;
155         }
156
157         return err;
158 }
159
160 static int switch_dell(struct device_info *devinfo)
161 {
162         char report[] = { 0x7f, 0x00, 0x00, 0x00 };
163
164         struct usb_dev_handle *handle;
165         int err;
166
167         switch (devinfo->mode) {
168         case HCI:
169                 report[1] = 0x13;
170                 break;
171         case HID:
172                 report[1] = 0x14;
173                 break;
174         }
175
176         handle = usb_open(devinfo->dev);
177         if (!handle)
178                 return -EIO;
179
180         /* Don't need to check return, as might not be in use */
181         usb_detach_kernel_driver_np(handle, 0);
182
183         if (usb_claim_interface(handle, 0) < 0) {
184                 usb_close(handle);
185                 return -EIO;
186         }
187
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);
192
193         if (err == 0) {
194                 err = -1;
195                 errno = EALREADY;
196         } else {
197                 if (errno == ETIMEDOUT)
198                         err = 0;
199         }
200
201         usb_close(handle);
202
203         return err;
204 }
205
206 static int find_device(struct device_info* devinfo)
207 {
208         struct usb_bus *bus;
209         struct usb_device *dev;
210
211         usb_find_busses();
212         usb_find_devices();
213
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) {
218                                 devinfo->dev=dev;
219                                 return 1;
220                         }
221                 }
222         return 0;
223 }
224
225 static int find_resuscitated_device(struct device_info* devinfo, uint8_t bInterfaceProtocol)
226 {
227         int i,j,k,l;
228         struct usb_device *dev, *child;
229         struct usb_config_descriptor config;
230         struct usb_interface interface;
231         struct usb_interface_descriptor altsetting;
232
233         /* Using the base device, attempt to find the child with the
234          * matching bInterfaceProtocol */
235         dev = devinfo->dev;
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;
246                                                 return 1;
247                                         }
248                                 }
249                         }
250                 }
251         }
252         return 0;
253 }
254
255 static void usage(char* error)
256 {
257         if (error)
258                 fprintf(stderr,"\n%s\n", error);
259         else
260                 printf("hid2hci - Bluetooth HID to HCI mode switching utility\n\n");
261
262         printf("Usage:\n"
263                 "\thid2hci [options]\n"
264                 "\n");
265
266         printf("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"
274                 "\n");
275         if (error)
276                 exit(1);
277 }
278
279 int main(int argc, char *argv[])
280 {
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' },
289                 { }
290         };
291         struct device_info dev = { NULL, HCI, 0, 0 };
292         int opt, quiet = 0;
293         int (*method)(struct device_info *dev) = NULL;
294         uint8_t resuscitate = 0;
295
296         while ((opt = getopt_long(argc, argv, "+s:r:v:p:m:qh", options, NULL)) != -1) {
297                 switch (opt) {
298                 case 'r':
299                         if (optarg && !strcmp(optarg, "hid"))
300                                 dev.mode = HID;
301                         else if (optarg && !strcmp(optarg, "hci"))
302                                 dev.mode = HCI;
303                         else
304                                 usage("ERROR: Undefined radio mode\n");
305                         break;
306                 case 'v':
307                         sscanf(optarg, "%4hx", &dev.vendor);
308                         break;
309                 case 'p':
310                         sscanf(optarg, "%4hx", &dev.product);
311                         break;
312                 case 'm':
313                         if (optarg && !strcmp(optarg, "csr"))
314                                 method = switch_csr;
315                         else if (optarg && !strcmp(optarg, "logitech"))
316                                 method = switch_logitech;
317                         else if (optarg && !strcmp(optarg, "dell"))
318                                 method = switch_dell;
319                         else
320                                 usage("ERROR: Undefined switching method\n");
321                         break;
322                 case 'q':
323                         quiet = 1;
324                         break;
325                 case 's':
326                         sscanf(optarg, "%2hx", (short unsigned int*) &resuscitate);
327                         break;
328                 case 'h':
329                         usage(NULL);
330                 default:
331                         exit(1);
332                 }
333         }
334
335         if (!quiet && (!dev.vendor || !dev.product || !method))
336                 usage("ERROR: Vendor ID, Product ID, and Switching Method must all be defined.\n");
337
338         argc -= optind;
339         argv += optind;
340         optind = 0;
341
342         usb_init();
343
344         if (!find_device(&dev)) {
345                 if (!quiet)
346                         fprintf(stderr, "Device %04x:%04x not found on USB bus.\n",
347                                 dev.vendor, dev.product);
348                 exit(1);
349         }
350
351         if (resuscitate && !find_resuscitated_device(&dev, resuscitate)) {
352                 if (!quiet)
353                         fprintf(stderr, "Device %04x:%04x was unable to resucitate any child devices.\n",
354                                 dev.vendor,dev.product);
355                 exit(1);
356         }
357
358         if (!quiet)
359                 printf("Attempting to switch device %04x:%04x to %s mode ",
360                         dev.vendor, dev.product, dev.mode ? "HID" : "HCI");
361         fflush(stdout);
362
363         if (method(&dev) < 0 && !quiet)
364                 printf("failed (%s)\n", strerror(errno));
365         else if (!quiet)
366                 printf("was successful\n");
367
368         return errno;
369 }