hid2hci.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * hid2hci : switch the radio on devices that support
  4. * it from HID to HCI and back
  5. *
  6. * Copyright (C) 2003-2010 Marcel Holtmann <marcel@holtmann.org>
  7. * Copyright (C) 2008-2009 Mario Limonciello <mario_limonciello@dell.com>
  8. * Copyright (C) 2009-2011 Kay Sievers <kay.sievers@vrfy.org>
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #define _GNU_SOURCE
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <unistd.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <dirent.h>
  23. #include <getopt.h>
  24. #include <limits.h>
  25. #include <sys/ioctl.h>
  26. #include <linux/types.h>
  27. #include <linux/hiddev.h>
  28. #include "libudev.h"
  29. #define USB_REQ_SET_CONFIGURATION 0x09
  30. #define USB_TYPE_CLASS (0x01 << 5)
  31. #define USB_TYPE_VENDOR (0x02 << 5)
  32. #define USB_RECIP_DEVICE 0x00
  33. #define USB_RECIP_INTERFACE 0x01
  34. #define USB_ENDPOINT_OUT 0x00
  35. struct usbfs_ctrltransfer {
  36. uint8_t bmRequestType;
  37. uint8_t bRequest;
  38. uint16_t wValue;
  39. uint16_t wIndex;
  40. uint16_t wLength;
  41. uint32_t timeout; /* in milliseconds */
  42. const void *data; /* pointer to data */
  43. };
  44. #define USBFS_DISCONNECT_IF_DRIVER 0x01
  45. #define USBFS_DISCONNECT_EXCEPT_DRIVER 0x02
  46. struct usbfs_disconnect{
  47. unsigned int interface;
  48. unsigned int flags;
  49. char driver[256];
  50. };
  51. #define USBFS_IOCTL_CONTROL _IOWR('U', 0, struct usbfs_ctrltransfer)
  52. #define USBFS_IOCTL_DISCONNECT _IOR('U', 27, struct usbfs_disconnect)
  53. static int control_message(int fd, int requesttype, int request,
  54. int value, int index,
  55. const uint8_t *bytes, int size, int timeout)
  56. {
  57. struct usbfs_ctrltransfer transfer;
  58. transfer.bmRequestType = requesttype;
  59. transfer.bRequest = request;
  60. transfer.wValue = value;
  61. transfer.wIndex = index;
  62. transfer.wLength = size,
  63. transfer.timeout = timeout;
  64. transfer.data = bytes;
  65. if (ioctl(fd, USBFS_IOCTL_CONTROL, &transfer) < 0) {
  66. fprintf(stderr, "Control transfer failed: %s (%d)\n",
  67. strerror(errno), errno);
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. enum mode {
  73. HCI = 0,
  74. HID = 1,
  75. };
  76. static int usb_switch_csr(int fd, enum mode mode)
  77. {
  78. int err;
  79. err = control_message(fd, USB_ENDPOINT_OUT | USB_TYPE_VENDOR |
  80. USB_RECIP_DEVICE,
  81. 0, mode, 0, NULL, 0, 10000);
  82. if (err == 0) {
  83. err = -1;
  84. errno = EALREADY;
  85. } else if (errno == ETIMEDOUT)
  86. err = 0;
  87. return err;
  88. }
  89. static int usb_switch_csr2(int fd, enum mode mode)
  90. {
  91. int err = 0;
  92. struct usbfs_disconnect disconnect;
  93. const uint8_t report[] = {
  94. 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  95. };
  96. switch (mode) {
  97. case HCI:
  98. /* send report as is */
  99. disconnect.interface = 0;
  100. disconnect.flags = USBFS_DISCONNECT_EXCEPT_DRIVER;
  101. strcpy(disconnect.driver, "usbfs");
  102. if (ioctl(fd, USBFS_IOCTL_DISCONNECT, &disconnect) < 0) {
  103. fprintf(stderr, "Can't claim interface: %s (%d)\n",
  104. strerror(errno), errno);
  105. return -1;
  106. }
  107. /* Set_report request with
  108. * report id: 0x01, report type: feature (0x03)
  109. * on interface 0
  110. */
  111. err = control_message(fd,
  112. USB_ENDPOINT_OUT | USB_TYPE_CLASS |
  113. USB_RECIP_INTERFACE,
  114. USB_REQ_SET_CONFIGURATION,
  115. 0x01 | (0x03 << 8),
  116. 0, report, sizeof(report), 5000);
  117. /* unable to detect whether the previous state
  118. * already was HCI (EALREADY)
  119. */
  120. break;
  121. case HID:
  122. /* currently unknown how to switch to HID */
  123. fprintf(stderr,
  124. "csr2: Switching to hid mode is not implemented\n");
  125. err = -1;
  126. break;
  127. }
  128. return err;
  129. }
  130. static int hid_logitech_send_report(int fd, const char *buf, size_t size)
  131. {
  132. struct hiddev_report_info rinfo;
  133. struct hiddev_usage_ref uref;
  134. unsigned int i;
  135. int err;
  136. for (i = 0; i < size; i++) {
  137. memset(&uref, 0, sizeof(uref));
  138. uref.report_type = HID_REPORT_TYPE_OUTPUT;
  139. uref.report_id = 0x10;
  140. uref.field_index = 0;
  141. uref.usage_index = i;
  142. uref.usage_code = 0xff000001;
  143. uref.value = buf[i] & 0x000000ff;
  144. err = ioctl(fd, HIDIOCSUSAGE, &uref);
  145. if (err < 0)
  146. return err;
  147. }
  148. memset(&rinfo, 0, sizeof(rinfo));
  149. rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
  150. rinfo.report_id = 0x10;
  151. rinfo.num_fields = 1;
  152. err = ioctl(fd, HIDIOCSREPORT, &rinfo);
  153. return err;
  154. }
  155. static int hid_switch_logitech(const char *filename)
  156. {
  157. char rep1[] = { 0xff, 0x80, 0x80, 0x01, 0x00, 0x00 };
  158. char rep2[] = { 0xff, 0x80, 0x00, 0x00, 0x30, 0x00 };
  159. char rep3[] = { 0xff, 0x81, 0x80, 0x00, 0x00, 0x00 };
  160. int fd;
  161. int err = -1;
  162. fd = open(filename, O_RDWR);
  163. if (fd < 0)
  164. return err;
  165. err = ioctl(fd, HIDIOCINITREPORT, 0);
  166. if (err < 0)
  167. goto out;
  168. err = hid_logitech_send_report(fd, rep1, sizeof(rep1));
  169. if (err < 0)
  170. goto out;
  171. err = hid_logitech_send_report(fd, rep2, sizeof(rep2));
  172. if (err < 0)
  173. goto out;
  174. err = hid_logitech_send_report(fd, rep3, sizeof(rep3));
  175. out:
  176. close(fd);
  177. return err;
  178. }
  179. static int usb_switch_dell(int fd, enum mode mode)
  180. {
  181. uint8_t report[] = { 0x7f, 0x00, 0x00, 0x00 };
  182. struct usbfs_disconnect disconnect;
  183. int err;
  184. switch (mode) {
  185. case HCI:
  186. report[1] = 0x13;
  187. break;
  188. case HID:
  189. report[1] = 0x14;
  190. break;
  191. }
  192. disconnect.interface = 0;
  193. disconnect.flags = USBFS_DISCONNECT_EXCEPT_DRIVER;
  194. strcpy(disconnect.driver, "usbfs");
  195. if (ioctl(fd, USBFS_IOCTL_DISCONNECT, &disconnect) < 0) {
  196. fprintf(stderr, "Can't claim interface: %s (%d)\n",
  197. strerror(errno), errno);
  198. return -1;
  199. }
  200. err = control_message(fd, USB_ENDPOINT_OUT | USB_TYPE_CLASS |
  201. USB_RECIP_INTERFACE,
  202. USB_REQ_SET_CONFIGURATION,
  203. 0x7f | (0x03 << 8), 0,
  204. report, sizeof(report), 5000);
  205. if (err == 0) {
  206. err = -1;
  207. errno = EALREADY;
  208. } else {
  209. if (errno == ETIMEDOUT)
  210. err = 0;
  211. }
  212. return err;
  213. }
  214. static int find_device(struct udev_device *udev_dev)
  215. {
  216. char path[PATH_MAX];
  217. const char *busnum_str, *devnum_str;
  218. int busnum, devnum;
  219. int fd;
  220. busnum_str = udev_device_get_sysattr_value(udev_dev, "busnum");
  221. if (!busnum_str)
  222. return -1;
  223. busnum = strtol(busnum_str, NULL, 10);
  224. devnum_str = udev_device_get_sysattr_value(udev_dev, "devnum");
  225. if (!devnum_str)
  226. return -1;
  227. devnum = strtol(devnum_str, NULL, 10);
  228. snprintf(path, sizeof(path), "/dev/bus/usb/%03d/%03d", busnum, devnum);
  229. fd = open(path, O_RDWR, O_CLOEXEC);
  230. if (fd < 0) {
  231. fprintf(stderr, "Can't open device: %s (%d)\n",
  232. strerror(errno), errno);
  233. return -1;
  234. }
  235. return fd;
  236. }
  237. static void usage(const char *error)
  238. {
  239. if (error)
  240. fprintf(stderr,"\n%s\n", error);
  241. else
  242. printf("hid2hci - Bluetooth HID to HCI mode switching utility\n\n");
  243. printf("Usage: hid2hci [options]\n"
  244. " --mode= mode to switch to [hid|hci] (default hci)\n"
  245. " --devpath= sys device path\n"
  246. " --method= method to use to switch [csr|csr2|logitech-hid|dell]\n"
  247. " --help\n\n");
  248. }
  249. int main(int argc, char *argv[])
  250. {
  251. static const struct option options[] = {
  252. { "help", no_argument, NULL, 'h' },
  253. { "mode", required_argument, NULL, 'm' },
  254. { "devpath", required_argument, NULL, 'p' },
  255. { "method", required_argument, NULL, 'M' },
  256. { }
  257. };
  258. enum method {
  259. METHOD_UNDEF,
  260. METHOD_CSR,
  261. METHOD_LOGITECH_HID,
  262. METHOD_DELL,
  263. } method = METHOD_UNDEF;
  264. struct udev *udev;
  265. struct udev_device *udev_dev = NULL;
  266. char syspath[PATH_MAX];
  267. int (*usb_switch)(int fd, enum mode mode) = NULL;
  268. enum mode mode = HCI;
  269. const char *devpath = NULL;
  270. int err = -1;
  271. int rc = 1;
  272. for (;;) {
  273. int option;
  274. option = getopt_long(argc, argv, "m:p:M:h", options, NULL);
  275. if (option == -1)
  276. break;
  277. switch (option) {
  278. case 'm':
  279. if (!strcmp(optarg, "hid")) {
  280. mode = HID;
  281. } else if (!strcmp(optarg, "hci")) {
  282. mode = HCI;
  283. } else {
  284. usage("error: undefined radio mode\n");
  285. exit(1);
  286. }
  287. break;
  288. case 'p':
  289. devpath = optarg;
  290. break;
  291. case 'M':
  292. if (!strcmp(optarg, "csr")) {
  293. method = METHOD_CSR;
  294. usb_switch = usb_switch_csr;
  295. } else if (!strcmp(optarg, "csr2")) {
  296. method = METHOD_CSR;
  297. usb_switch = usb_switch_csr2;
  298. } else if (!strcmp(optarg, "logitech-hid")) {
  299. method = METHOD_LOGITECH_HID;
  300. } else if (!strcmp(optarg, "dell")) {
  301. method = METHOD_DELL;
  302. usb_switch = usb_switch_dell;
  303. } else {
  304. usage("error: undefined switching method\n");
  305. exit(1);
  306. }
  307. break;
  308. case 'h':
  309. usage(NULL);
  310. }
  311. }
  312. if (!devpath || method == METHOD_UNDEF) {
  313. usage("error: --devpath= and --method= must be defined\n");
  314. exit(1);
  315. }
  316. udev = udev_new();
  317. if (udev == NULL)
  318. goto exit;
  319. snprintf(syspath, sizeof(syspath), "/sys/%s", devpath);
  320. udev_dev = udev_device_new_from_syspath(udev, syspath);
  321. if (udev_dev == NULL) {
  322. fprintf(stderr, "error: could not find '%s'\n", devpath);
  323. goto exit;
  324. }
  325. switch (method) {
  326. case METHOD_CSR:
  327. case METHOD_DELL: {
  328. struct udev_device *dev;
  329. int handle;
  330. const char *type;
  331. /* get the parent usb_device if needed */
  332. dev = udev_dev;
  333. type = udev_device_get_devtype(dev);
  334. if (type == NULL || strcmp(type, "usb_device") != 0) {
  335. dev = udev_device_get_parent_with_subsystem_devtype(dev,
  336. "usb", "usb_device");
  337. if (dev == NULL) {
  338. fprintf(stderr, "error: could not find usb_device for '%s'\n", devpath);
  339. goto exit;
  340. }
  341. }
  342. handle = find_device(dev);
  343. if (handle < 0) {
  344. fprintf(stderr, "error: unable to handle '%s'\n",
  345. udev_device_get_syspath(dev));
  346. goto exit;
  347. }
  348. err = usb_switch(handle, mode);
  349. close(handle);
  350. break;
  351. }
  352. case METHOD_LOGITECH_HID: {
  353. const char *device;
  354. device = udev_device_get_devnode(udev_dev);
  355. if (device == NULL) {
  356. fprintf(stderr, "error: could not find hiddev device node\n");
  357. goto exit;
  358. }
  359. err = hid_switch_logitech(device);
  360. break;
  361. }
  362. case METHOD_UNDEF:
  363. default:
  364. break;
  365. }
  366. if (err < 0)
  367. fprintf(stderr, "error: switching device '%s' failed.\n",
  368. udev_device_get_syspath(udev_dev));
  369. exit:
  370. udev_device_unref(udev_dev);
  371. udev_unref(udev);
  372. return rc;
  373. }