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