chiark / gitweb /
559c6f77b19d08c3e05e2497487b11acb7f324f4
[elogind.git] / extras / hid2hci / hid2hci.c
1 /*
2  * hid2hci : switch the radio on devices that support
3  *           it from HID to HCI and back
4  *
5  * Copyright (C) 2003-2010  Marcel Holtmann <marcel@holtmann.org>
6  * Copyright (C) 2008-2009  Mario Limonciello <mario_limonciello@dell.com>
7  * Copyright (C) 2009-2011 Kay Sievers <kay.sievers@vrfy.org>
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 #include <stdio.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdint.h>
28 #include <string.h>
29 #include <getopt.h>
30 #include <sys/ioctl.h>
31 #include <linux/types.h>
32 #include <linux/hiddev.h>
33 #include <usb.h>
34
35 #include "libudev.h"
36
37 enum mode {
38         HCI = 0,
39         HID = 1,
40 };
41
42 static int usb_switch_csr(struct usb_dev_handle *dev, enum mode mode)
43 {
44         int err;
45
46         err = usb_control_msg(dev,
47                               USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
48                               0, mode, 0, NULL, 0, 10000);
49         if (err == 0) {
50                 err = -1;
51                 errno = EALREADY;
52         } else {
53                 if (errno == ETIMEDOUT)
54                         err = 0;
55         }
56         return err;
57 }
58
59 static int hid_logitech_send_report(int fd, const char *buf, size_t size)
60 {
61         struct hiddev_report_info rinfo;
62         struct hiddev_usage_ref uref;
63         unsigned int i;
64         int err;
65
66         for (i = 0; i < size; i++) {
67                 memset(&uref, 0, sizeof(uref));
68                 uref.report_type = HID_REPORT_TYPE_OUTPUT;
69                 uref.report_id   = 0x10;
70                 uref.field_index = 0;
71                 uref.usage_index = i;
72                 uref.usage_code  = 0xff000001;
73                 uref.value       = buf[i] & 0x000000ff;
74                 err = ioctl(fd, HIDIOCSUSAGE, &uref);
75                 if (err < 0)
76                         return err;
77         }
78
79         memset(&rinfo, 0, sizeof(rinfo));
80         rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
81         rinfo.report_id   = 0x10;
82         rinfo.num_fields  = 1;
83         err = ioctl(fd, HIDIOCSREPORT, &rinfo);
84
85         return err;
86 }
87
88 static int hid_switch_logitech(const char *filename)
89 {
90         char rep1[] = { 0xff, 0x80, 0x80, 0x01, 0x00, 0x00 };
91         char rep2[] = { 0xff, 0x80, 0x00, 0x00, 0x30, 0x00 };
92         char rep3[] = { 0xff, 0x81, 0x80, 0x00, 0x00, 0x00 };
93         int fd;
94         int err = -1;
95
96         fd = open(filename, O_RDWR);
97         if (fd < 0)
98                 return err;
99
100         err = ioctl(fd, HIDIOCINITREPORT, 0);
101         if (err < 0)
102                 goto out;
103
104         err = hid_logitech_send_report(fd, rep1, sizeof(rep1));
105         if (err < 0)
106                 goto out;
107
108         err = hid_logitech_send_report(fd, rep2, sizeof(rep2));
109         if (err < 0)
110                 goto out;
111
112         err = hid_logitech_send_report(fd, rep3, sizeof(rep3));
113 out:
114         close(fd);
115         return err;
116 }
117
118 static int usb_switch_dell(struct usb_dev_handle *dev, enum mode mode)
119 {
120         char report[] = { 0x7f, 0x00, 0x00, 0x00 };
121         int err;
122
123         switch (mode) {
124         case HCI:
125                 report[1] = 0x13;
126                 break;
127         case HID:
128                 report[1] = 0x14;
129                 break;
130         }
131
132         /* Don't need to check return, as might not be in use */
133         usb_detach_kernel_driver_np(dev, 0);
134
135         if (usb_claim_interface(dev, 0) < 0)
136                 return -EIO;
137
138         err = usb_control_msg(dev,
139                         USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
140                         USB_REQ_SET_CONFIGURATION, 0x7f | (0x03 << 8), 0,
141                         report, sizeof(report), 5000);
142
143         if (err == 0) {
144                 err = -1;
145                 errno = EALREADY;
146         } else {
147                 if (errno == ETIMEDOUT)
148                         err = 0;
149         }
150         return err;
151 }
152
153 /*
154  * libusb needs to scan and open all devices, just to to find the
155  * device we already have. This should be fixed in libusb.
156  */
157 static struct usb_device *usb_device_open_from_udev(struct udev_device *usb_dev)
158 {
159         struct usb_bus *bus;
160         const char *str;
161         int busnum;
162         int devnum;
163
164         str = udev_device_get_sysattr_value(usb_dev, "busnum");
165         if (str == NULL)
166                 return NULL;
167         busnum = strtol(str, NULL, 0);
168
169         str = udev_device_get_sysattr_value(usb_dev, "devnum");
170         if (str == NULL)
171                 return NULL;
172         devnum = strtol(str, NULL, 0);
173
174         usb_init();
175         usb_find_busses();
176         usb_find_devices();
177
178         for (bus = usb_get_busses(); bus; bus = bus->next) {
179                 struct usb_device *dev;
180
181                 if (strtol(bus->dirname, NULL, 10) != busnum)
182                         continue;
183
184                 for (dev = bus->devices; dev; dev = dev->next) {
185                         if (dev->devnum == devnum)
186                                 return dev;
187                 }
188         }
189
190         return NULL;
191 }
192
193 static struct usb_dev_handle *find_device(struct udev_device *udev_dev)
194 {
195         struct usb_device *dev;
196
197         dev = usb_device_open_from_udev(udev_dev);
198         if (dev == NULL)
199                 return NULL;
200         return usb_open(dev);
201 }
202
203 static void usage(const char *error)
204 {
205         if (error)
206                 fprintf(stderr,"\n%s\n", error);
207         else
208                 printf("hid2hci - Bluetooth HID to HCI mode switching utility\n\n");
209
210         printf("Usage: hid2hci [options]\n"
211                 "  --mode=               mode to switch to [hid|hci] (default hci)\n"
212                 "  --devpath=            sys device path\n"
213                 "  --method=             method to use to switch [csr|logitech-hid|dell]\n"
214                 "  --help\n\n");
215 }
216
217 int main(int argc, char *argv[])
218 {
219         static const struct option options[] = {
220                 { "help", no_argument, NULL, 'h' },
221                 { "mode", required_argument, NULL, 'm' },
222                 { "devpath", required_argument, NULL, 'p' },
223                 { "method", required_argument, NULL, 'M' },
224                 { }
225         };
226         enum method {
227                 METHOD_UNDEF,
228                 METHOD_CSR,
229                 METHOD_LOGITECH_HID,
230                 METHOD_DELL,
231         } method = METHOD_UNDEF;
232         struct udev *udev;
233         struct udev_device *udev_dev = NULL;
234         char syspath[PATH_MAX];
235         int (*usb_switch)(struct usb_dev_handle *dev, enum mode mode) = NULL;
236         enum mode mode = HCI;
237         const char *devpath = NULL;
238         int err = -1;
239         int rc = 1;
240
241         for (;;) {
242                 int option;
243
244                 option = getopt_long(argc, argv, "m:p:M:h", options, NULL);
245                 if (option == -1)
246                         break;
247
248                 switch (option) {
249                 case 'm':
250                         if (!strcmp(optarg, "hid")) {
251                                 mode = HID;
252                         } else if (!strcmp(optarg, "hci")) {
253                                 mode = HCI;
254                         } else {
255                                 usage("error: undefined radio mode\n");
256                                 exit(1);
257                         }
258                         break;
259                 case 'p':
260                         devpath = optarg;
261                         break;
262                 case 'M':
263                         if (!strcmp(optarg, "csr")) {
264                                 method = METHOD_CSR;
265                                 usb_switch = usb_switch_csr;
266                         } else if (!strcmp(optarg, "logitech-hid")) {
267                                 method = METHOD_LOGITECH_HID;
268                         } else if (!strcmp(optarg, "dell")) {
269                                 method = METHOD_DELL;
270                                 usb_switch = usb_switch_dell;
271                         } else {
272                                 usage("error: undefined switching method\n");
273                                 exit(1);
274                         }
275                         break;
276                 case 'h':
277                         usage(NULL);
278                 }
279         }
280
281         if (!devpath || method == METHOD_UNDEF) {
282                 usage("error: --devpath= and --method= must be defined\n");
283                 exit(1);
284         }
285
286         udev = udev_new();
287         if (udev == NULL)
288                 goto exit;
289
290         snprintf(syspath, sizeof(syspath), "%s/%s", udev_get_sys_path(udev), devpath);
291         udev_dev = udev_device_new_from_syspath(udev, syspath);
292         if (udev_dev == NULL) {
293                 fprintf(stderr, "error: could not find '%s'\n", devpath);
294                 goto exit;
295         }
296
297         switch (method) {
298         case METHOD_CSR:
299         case METHOD_DELL: {
300                 struct udev_device *dev;
301                 struct usb_dev_handle *handle;
302                 const char *type;
303
304                 /* get the parent usb_device if needed */
305                 dev = udev_dev;
306                 type = udev_device_get_devtype(dev);
307                 if (type == NULL || strcmp(type, "usb_device") != 0) {
308                         dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
309                         if (dev == NULL) {
310                                 fprintf(stderr, "error: could not find usb_device for '%s'\n", devpath);
311                                 goto exit;
312                         }
313                 }
314
315                 handle = find_device(dev);
316                 if (handle == NULL) {
317                         fprintf(stderr, "error: unable to handle '%s'\n",
318                                 udev_device_get_syspath(dev));
319                         goto exit;
320                 }
321                 err = usb_switch(handle, mode);
322                 break;
323         }
324         case METHOD_LOGITECH_HID: {
325                 const char *device;
326
327                 device = udev_device_get_devnode(udev_dev);
328                 if (device == NULL) {
329                         fprintf(stderr, "error: could not find hiddev device node\n");
330                         goto exit;
331                 }
332                 err = hid_switch_logitech(device);
333                 break;
334         }
335         default:
336                 break;
337         }
338
339         if (err < 0)
340                 fprintf(stderr, "error: switching device '%s' failed.\n",
341                         udev_device_get_syspath(udev_dev));
342 exit:
343         udev_device_unref(udev_dev);
344         udev_unref(udev);
345         return rc;
346 }