chiark / gitweb /
make: build internal tools against libudev-private.la
[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-2009  Marcel Holtmann <marcel@holtmann.org>
6  * Copyright (C) 2008-2009  Mario Limonciello <mario_limonciello@dell.com>
7  * Copyright (C) 2009 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/hiddev.h>
32 #include <usb.h>
33
34 #include "libudev.h"
35 #include "libudev-private.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  * The braindead libusb needs to scan and open all devices, just to
155  * to find the device we already have. This needs to be fixed in libusb
156  * or it will be ripped out and we carry our own code.
157  */
158 static struct usb_device *usb_device_open_from_udev(struct udev_device *usb_dev)
159 {
160         struct usb_bus *bus;
161         const char *str;
162         int busnum;
163         int devnum;
164
165         str = udev_device_get_sysattr_value(usb_dev, "busnum");
166         if (str == NULL)
167                 return NULL;
168         busnum = strtol(str, NULL, 0);
169
170         str = udev_device_get_sysattr_value(usb_dev, "devnum");
171         if (str == NULL)
172                 return NULL;
173         devnum = strtol(str, NULL, 0);
174
175         usb_init();
176         usb_find_busses();
177         usb_find_devices();
178
179         for (bus = usb_get_busses(); bus; bus = bus->next) {
180                 struct usb_device *dev;
181
182                 if (strtol(bus->dirname, NULL, 10) != busnum)
183                         continue;
184
185                 for (dev = bus->devices; dev; dev = dev->next) {
186                         if (dev->devnum == devnum)
187                                 return dev;
188                 }
189         }
190
191         return NULL;
192 }
193
194 static struct usb_dev_handle *find_device(struct udev_device *udev_dev, const char *sibling_intf)
195 {
196         struct udev *udev = udev_device_get_udev(udev_dev);
197         struct usb_device *dev;
198         struct udev_device *udev_parent;
199         char str[UTIL_NAME_SIZE];
200         struct udev_enumerate *enumerate;
201         struct udev_list_entry *entry;
202         struct usb_dev_handle *handle = NULL;
203
204         if (sibling_intf == NULL) {
205                 dev = usb_device_open_from_udev(udev_dev);
206                 if (dev == NULL)
207                         return NULL;
208                 return usb_open(dev);
209         }
210
211         /* find matching sibling of the current usb_device, they share the same hub */
212         udev_parent = udev_device_get_parent_with_subsystem_devtype(udev_dev, "usb", "usb_device");
213         if (udev_parent == NULL)
214                 return NULL;
215
216         enumerate = udev_enumerate_new(udev);
217         if (enumerate == NULL)
218                 return NULL;
219
220         udev_enumerate_add_match_subsystem(enumerate, "usb");
221
222         /* match all childs of the parent */
223         util_strscpyl(str, sizeof(str), udev_device_get_sysname(udev_parent), "*", NULL);
224         udev_enumerate_add_match_sysname(enumerate, str);
225
226         /* match the specified interface */
227         util_strscpy(str, sizeof(str), sibling_intf);
228         str[2] = '\0';
229         str[5] = '\0';
230         str[8] = '\0';
231         if (strcmp(str, "-1") != 0)
232                 udev_enumerate_add_match_sysattr(enumerate, "bInterfaceClass", str);
233         if (strcmp(&str[3], "-1") != 0)
234                 udev_enumerate_add_match_sysattr(enumerate, "bInterfaceSubClass", &str[3]);
235         if (strcmp(&str[6], "-1") != 0)
236                 udev_enumerate_add_match_sysattr(enumerate, "bInterfaceProtocol", &str[6]);
237
238         udev_enumerate_scan_devices(enumerate);
239         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(enumerate)) {
240                 struct udev_device *udev_device;
241
242                 udev_device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(entry));
243                 if (udev_device != NULL) {
244                         /* get the usb_device of the usb_interface we matched */
245                         udev_parent = udev_device_get_parent_with_subsystem_devtype(udev_device, "usb", "usb_device");
246                         if (udev_parent == NULL)
247                                 continue;
248                         /* only look at the first matching device */
249                         dev = usb_device_open_from_udev(udev_parent);
250                         if (dev != NULL)
251                                 handle = usb_open(dev);
252                         udev_device_unref(udev_device);
253                         break;
254                 }
255 }
256         udev_enumerate_unref(enumerate);
257         return handle;
258 }
259
260 static void usage(const char *error)
261 {
262         if (error)
263                 fprintf(stderr,"\n%s\n", error);
264         else
265                 printf("hid2hci - Bluetooth HID to HCI mode switching utility\n\n");
266
267         printf("Usage: hid2hci [options]\n"
268                 "  --mode=               mode to switch to [hid|hci] (default hci)\n"
269                 "  --devpath=            sys device path\n"
270                 "  --method=             method to use to switch [csr|logitech-hid|dell]\n"
271                 "  --find-sibling-intf=  find the sibling device with 00:00:00 (class:subclass:prot)\n"
272                 "  --help\n\n");
273 }
274
275 int main(int argc, char *argv[])
276 {
277         static const struct option options[] = {
278                 { "help", no_argument, NULL, 'h' },
279                 { "mode", required_argument, NULL, 'm' },
280                 { "devpath", required_argument, NULL, 'p' },
281                 { "method", required_argument, NULL, 'M' },
282                 { "find-sibling-intf", required_argument, NULL, 'I' },
283                 { }
284         };
285         enum method {
286                 METHOD_UNDEF,
287                 METHOD_CSR,
288                 METHOD_LOGITECH_HID,
289                 METHOD_DELL,
290         } method = METHOD_UNDEF;
291         struct udev *udev;
292         struct udev_device *udev_dev = NULL;
293         char syspath[UTIL_PATH_SIZE];
294         int (*usb_switch)(struct usb_dev_handle *dev, enum mode mode) = NULL;
295         enum mode mode = HCI;
296         const char *devpath = NULL;
297         const char *sibling_intf = NULL;
298         int err = -1;
299         int rc = 1;
300
301         for (;;) {
302                 int option;
303
304                 option = getopt_long(argc, argv, "m:p:M:I:qh", options, NULL);
305                 if (option == -1)
306                         break;
307
308                 switch (option) {
309                 case 'm':
310                         if (!strcmp(optarg, "hid")) {
311                                 mode = HID;
312                         } else if (!strcmp(optarg, "hci")) {
313                                 mode = HCI;
314                         } else {
315                                 usage("error: undefined radio mode\n");
316                                 exit(1);
317                         }
318                         break;
319                 case 'p':
320                         devpath = optarg;
321                         break;
322                 case 'M':
323                         if (!strcmp(optarg, "csr")) {
324                                 method = METHOD_CSR;
325                                 usb_switch = usb_switch_csr;
326                         } else if (!strcmp(optarg, "logitech-hid")) {
327                                 method = METHOD_LOGITECH_HID;
328                         } else if (!strcmp(optarg, "dell")) {
329                                 method = METHOD_DELL;
330                                 usb_switch = usb_switch_dell;
331                         } else {
332                                 usage("error: undefined switching method\n");
333                                 exit(1);
334                         }
335                         break;
336                 case 'I':
337                         sibling_intf = optarg;
338                         break;
339                 case 'h':
340                         usage(NULL);
341                 default:
342                         exit(1);
343                 }
344         }
345
346         if (!devpath || method == METHOD_UNDEF) {
347                 usage("error: --devpath= and --method= must be defined\n");
348                 exit(1);
349         }
350
351         udev = udev_new();
352         if (udev == NULL)
353                 goto exit;
354
355         util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
356         udev_dev = udev_device_new_from_syspath(udev, syspath);
357         if (udev_dev == NULL) {
358                 fprintf(stderr, "error: could not find '%s'\n", devpath);
359                 goto exit;
360         }
361
362         switch (method) {
363         case METHOD_CSR:
364         case METHOD_DELL: {
365                 struct udev_device *dev;
366                 struct usb_dev_handle *handle;
367                 const char *type;
368
369                 /* get the parent usb_device if needed */
370                 dev = udev_dev;
371                 type = udev_device_get_devtype(dev);
372                 if (type == NULL || strcmp(type, "usb_device") != 0) {
373                         dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
374                         if (dev == NULL) {
375                                 fprintf(stderr, "error: could not find usb_device for '%s'\n", devpath);
376                                 goto exit;
377                         }
378                 }
379
380                 handle = find_device(dev, sibling_intf);
381                 if (handle == NULL) {
382                         fprintf(stderr, "error: unable to handle '%s' (intf=%s)\n",
383                                 udev_device_get_syspath(dev), sibling_intf);
384                         goto exit;
385                 }
386                 err = usb_switch(handle, mode);
387                 break;
388         }
389         case METHOD_LOGITECH_HID: {
390                 const char *device;
391
392                 device = udev_device_get_devnode(udev_dev);
393                 if (device == NULL) {
394                         fprintf(stderr, "error: could not find hiddev device node\n");
395                         goto exit;
396                 }
397                 err = hid_switch_logitech(device);
398                 break;
399         }
400         default:
401                 break;
402         }
403
404         if (err < 0)
405                 fprintf(stderr, "error: switching device '%s' (intf=%s) failed.\n",
406                         udev_device_get_syspath(udev_dev), sibling_intf);
407 exit:
408         udev_device_unref(udev_dev);
409         udev_unref(udev);
410         return rc;
411 }