Tool for using the Wii U GameCube Adapter on Linux
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

664 lines
18 KiB

  1. // See LICENSE for license
  2. #define _XOPEN_SOURCE 600
  3. #include <time.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <linux/input.h>
  9. #include <linux/uinput.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <signal.h>
  15. #include <errno.h>
  16. #include <libudev.h>
  17. #include <libusb.h>
  18. #include <pthread.h>
  19. #if (!defined(LIBUSBX_API_VERSION) || LIBUSBX_API_VERSION < 0x01000102) && (!defined(LIBUSB_API_VERSION) || LIBUSB_API_VERSION < 0x01000102)
  20. #error libusb(x) 1.0.16 or higher is required
  21. #endif
  22. #define EP_IN 0x81
  23. #define EP_OUT 0x02
  24. #define STATE_NORMAL 0x10
  25. #define STATE_WAVEBIRD 0x20
  26. #define MAX_FF_EVENTS 4
  27. //#define PAD_BTN
  28. #define XBOX360_EMULATION
  29. const int BUTTON_OFFSET_VALUES[16] = {
  30. BTN_START,
  31. BTN_TR,
  32. -1,
  33. -1,
  34. -1,
  35. -1,
  36. -1,
  37. -1,
  38. BTN_A,
  39. BTN_X,
  40. BTN_B,
  41. BTN_Y,
  42. BTN_DPAD_UP,
  43. BTN_DPAD_DOWN,
  44. BTN_DPAD_RIGHT,
  45. BTN_DPAD_LEFT,
  46. };
  47. const int AXIS_OFFSET_VALUES[6] = {
  48. ABS_X,
  49. ABS_Y,
  50. ABS_RX,
  51. ABS_RY,
  52. ABS_Z,
  53. ABS_RZ,
  54. };
  55. struct ff_event
  56. {
  57. bool in_use;
  58. bool forever;
  59. int duration;
  60. int delay;
  61. int repetitions;
  62. struct timespec start_time;
  63. struct timespec end_time;
  64. };
  65. struct ports
  66. {
  67. bool connected;
  68. bool extra_power;
  69. int uinput;
  70. unsigned char type;
  71. uint16_t buttons;
  72. uint8_t axis[6];
  73. struct ff_event ff_events[MAX_FF_EVENTS];
  74. };
  75. struct adapter
  76. {
  77. volatile bool quitting;
  78. struct libusb_device *device;
  79. struct libusb_device_handle *handle;
  80. pthread_t thread;
  81. unsigned char rumble[5];
  82. struct ports controllers[4];
  83. struct adapter *next;
  84. };
  85. static bool raw_mode;
  86. static volatile int quitting;
  87. static struct adapter adapters;
  88. static const char *uinput_path;
  89. static unsigned char connected_type(unsigned char status)
  90. {
  91. unsigned char type = status & (STATE_NORMAL | STATE_WAVEBIRD);
  92. switch (type)
  93. {
  94. case STATE_NORMAL:
  95. case STATE_WAVEBIRD:
  96. return type;
  97. default:
  98. return 0;
  99. }
  100. }
  101. static bool uinput_create(int i, struct ports *port, unsigned char type)
  102. {
  103. fprintf(stderr, "connecting on port %d\n", i);
  104. struct uinput_user_dev uinput_dev;
  105. memset(&uinput_dev, 0, sizeof(uinput_dev));
  106. port->uinput = open(uinput_path, O_RDWR | O_NONBLOCK);
  107. // buttons
  108. ioctl(port->uinput, UI_SET_EVBIT, EV_KEY);
  109. ioctl(port->uinput, UI_SET_KEYBIT, BTN_A);
  110. ioctl(port->uinput, UI_SET_KEYBIT, BTN_B);
  111. ioctl(port->uinput, UI_SET_KEYBIT, BTN_X);
  112. ioctl(port->uinput, UI_SET_KEYBIT, BTN_Y);
  113. ioctl(port->uinput, UI_SET_KEYBIT, BTN_TL);
  114. ioctl(port->uinput, UI_SET_KEYBIT, BTN_TR);
  115. ioctl(port->uinput, UI_SET_KEYBIT, BTN_SELECT);
  116. ioctl(port->uinput, UI_SET_KEYBIT, BTN_START);
  117. ioctl(port->uinput, UI_SET_KEYBIT, BTN_MODE);
  118. ioctl(port->uinput, UI_SET_KEYBIT, BTN_THUMBL);
  119. ioctl(port->uinput, UI_SET_KEYBIT, BTN_THUMBR);
  120. ioctl(port->uinput, UI_SET_KEYBIT, BTN_DPAD_UP);
  121. ioctl(port->uinput, UI_SET_KEYBIT, BTN_DPAD_DOWN);
  122. ioctl(port->uinput, UI_SET_KEYBIT, BTN_DPAD_LEFT);
  123. ioctl(port->uinput, UI_SET_KEYBIT, BTN_DPAD_RIGHT);
  124. // axis
  125. ioctl(port->uinput, UI_SET_EVBIT, EV_ABS);
  126. ioctl(port->uinput, UI_SET_ABSBIT, ABS_X);
  127. ioctl(port->uinput, UI_SET_ABSBIT, ABS_Y);
  128. ioctl(port->uinput, UI_SET_ABSBIT, ABS_Z);
  129. ioctl(port->uinput, UI_SET_ABSBIT, ABS_RX);
  130. ioctl(port->uinput, UI_SET_ABSBIT, ABS_RY);
  131. ioctl(port->uinput, UI_SET_ABSBIT, ABS_RZ);
  132. ioctl(port->uinput, UI_SET_ABSBIT, ABS_HAT0X);
  133. ioctl(port->uinput, UI_SET_ABSBIT, ABS_HAT0Y);
  134. if (raw_mode)
  135. {
  136. uinput_dev.absmin[ABS_X] = 0; uinput_dev.absmax[ABS_X] = 255;
  137. uinput_dev.absmin[ABS_Y] = 0; uinput_dev.absmax[ABS_Y] = 255;
  138. uinput_dev.absmin[ABS_Z] = -255; uinput_dev.absmax[ABS_Z] = 255;
  139. uinput_dev.absmin[ABS_RX] = 0; uinput_dev.absmax[ABS_RX] = 255;
  140. uinput_dev.absmin[ABS_RY] = 0; uinput_dev.absmax[ABS_RY] = 255;
  141. uinput_dev.absmin[ABS_RZ] = -255; uinput_dev.absmax[ABS_RZ] = 255;
  142. }
  143. else
  144. {
  145. uinput_dev.absmin[ABS_X] = 20; uinput_dev.absmax[ABS_X] = 235;
  146. uinput_dev.absmin[ABS_Y] = 20; uinput_dev.absmax[ABS_Y] = 235;
  147. uinput_dev.absmin[ABS_RY] = 30; uinput_dev.absmax[ABS_RY] = 225;
  148. uinput_dev.absmin[ABS_RX] = 30; uinput_dev.absmax[ABS_RX] = 225;
  149. uinput_dev.absmin[ABS_Z] = 0; uinput_dev.absmax[ABS_Z] = 225; uinput_dev.absflat[ABS_Z] = 30;
  150. uinput_dev.absmin[ABS_RZ] = 0; uinput_dev.absmax[ABS_RZ] = 225; uinput_dev.absflat[ABS_RZ] = 30;
  151. }
  152. uinput_dev.absmin[ABS_HAT0X] = -1; uinput_dev.absmax[ABS_HAT0X] = 1;
  153. uinput_dev.absmin[ABS_HAT0Y] = -1; uinput_dev.absmax[ABS_HAT0Y] = 1;
  154. // rumble
  155. ioctl(port->uinput, UI_SET_EVBIT, EV_FF);
  156. ioctl(port->uinput, UI_SET_FFBIT, FF_CONSTANT);
  157. ioctl(port->uinput, UI_SET_FFBIT, FF_RUMBLE);
  158. uinput_dev.ff_effects_max = MAX_FF_EVENTS;
  159. snprintf(uinput_dev.name, sizeof(uinput_dev.name), "Microsoft X-Box 360 pad");
  160. uinput_dev.name[sizeof(uinput_dev.name)-1] = 0;
  161. uinput_dev.id.bustype = BUS_USB;
  162. if (write(port->uinput, &uinput_dev, sizeof(uinput_dev)) != sizeof(uinput_dev))
  163. {
  164. fprintf(stderr, "error writing uinput device settings");
  165. close(port->uinput);
  166. return false;
  167. }
  168. if (ioctl(port->uinput, UI_DEV_CREATE) != 0)
  169. {
  170. fprintf(stderr, "error creating uinput device");
  171. close(port->uinput);
  172. return false;
  173. }
  174. port->type = type;
  175. port->connected = true;
  176. return true;
  177. }
  178. static void uinput_destroy(int i, struct ports *port)
  179. {
  180. fprintf(stderr, "disconnecting on port %d\n", i);
  181. ioctl(port->uinput, UI_DEV_DESTROY);
  182. close(port->uinput);
  183. port->connected = false;
  184. }
  185. static struct timespec ts_add(struct timespec *start, int milliseconds)
  186. {
  187. struct timespec ret = *start;
  188. int s = milliseconds / 1000;
  189. int ns = (milliseconds % 1000) * 1000000;
  190. ret.tv_sec += s ;
  191. ret.tv_nsec += ns ;
  192. if (ret.tv_nsec >= 1000000000L)
  193. {
  194. ret.tv_sec++;
  195. ret.tv_nsec -= 1000000000L;
  196. }
  197. return ret;
  198. }
  199. static bool ts_greaterthan(struct timespec *first, struct timespec *second)
  200. {
  201. return (first->tv_sec >= second->tv_sec || (first->tv_sec == second->tv_sec && first->tv_nsec >= second->tv_nsec));
  202. }
  203. static bool ts_lessthan(struct timespec *first, struct timespec *second)
  204. {
  205. return (first->tv_sec <= second->tv_sec || (first->tv_sec == second->tv_sec && first->tv_nsec <= second->tv_nsec));
  206. }
  207. static void update_ff_start_stop(struct ff_event *e, struct timespec *current_time)
  208. {
  209. e->repetitions--;
  210. if (e->repetitions < 0)
  211. {
  212. e->repetitions = 0;
  213. e->start_time.tv_sec = 0;
  214. e->start_time.tv_nsec = 0;
  215. e->end_time.tv_sec = 0;
  216. e->end_time.tv_nsec = 0;
  217. }
  218. else
  219. {
  220. e->start_time = ts_add(current_time, e->delay);
  221. if (e->duration == 0)
  222. {
  223. e->end_time.tv_sec = INT_MAX;
  224. e->end_time.tv_nsec = 999999999L;
  225. }
  226. else
  227. {
  228. e->end_time = ts_add(&e->start_time, e->duration);
  229. }
  230. }
  231. }
  232. static int create_ff_event(struct ports *port, struct uinput_ff_upload *upload)
  233. {
  234. if (upload->old.type != 0)
  235. {
  236. port->ff_events[upload->old.id].forever = (upload->effect.replay.length == 0);
  237. port->ff_events[upload->old.id].duration = upload->effect.replay.length;
  238. port->ff_events[upload->old.id].delay = upload->effect.replay.delay;
  239. port->ff_events[upload->old.id].repetitions = 0;
  240. return upload->old.id;
  241. }
  242. for (int i = 0; i < MAX_FF_EVENTS; i++)
  243. {
  244. if (!port->ff_events[i].in_use)
  245. {
  246. port->ff_events[i].in_use = true;
  247. port->ff_events[i].forever = (upload->effect.replay.length == 0);
  248. port->ff_events[i].duration = upload->effect.replay.length;
  249. port->ff_events[i].delay = upload->effect.replay.delay;
  250. port->ff_events[i].repetitions = 0;
  251. return i;
  252. }
  253. }
  254. return -1;
  255. }
  256. static void handle_payload(int i, struct ports *port, unsigned char *payload, struct timespec *current_time)
  257. {
  258. unsigned char status = payload[0];
  259. unsigned char type = connected_type(status);
  260. if (type != 0 && !port->connected)
  261. {
  262. uinput_create(i, port, type);
  263. }
  264. else if (type == 0 && port->connected)
  265. {
  266. uinput_destroy(i, port);
  267. }
  268. if (!port->connected)
  269. return;
  270. port->extra_power = ((status & 0x04) != 0);
  271. if (type != port->type)
  272. {
  273. fprintf(stderr, "controller on port %d changed controller type???", i+1);
  274. port->type = type;
  275. }
  276. struct input_event events[12+1+6+1]; // buttons + dpad + axis + syn event
  277. memset(&events, 0, sizeof(events));
  278. int e_count = 0;
  279. uint16_t btns = (uint16_t) payload[1] << 8 | (uint16_t) payload[2];
  280. for (int j = 0; j < 16; j++)
  281. {
  282. if (BUTTON_OFFSET_VALUES[j] == -1)
  283. continue;
  284. uint16_t mask = (1 << j);
  285. uint16_t pressed = btns & mask;
  286. if ((port->buttons & mask) != pressed)
  287. {
  288. switch (BUTTON_OFFSET_VALUES[j]) {
  289. case BTN_DPAD_RIGHT:
  290. case BTN_DPAD_LEFT:
  291. case BTN_DPAD_DOWN:
  292. case BTN_DPAD_UP:
  293. events[e_count].type = EV_ABS;
  294. events[e_count].code = (BUTTON_OFFSET_VALUES[j] == BTN_DPAD_UP || BUTTON_OFFSET_VALUES[j] == BTN_DPAD_DOWN)
  295. // axes are flipped for xbox compatibility
  296. ? ABS_HAT0X : ABS_HAT0Y;
  297. events[e_count].value = (BUTTON_OFFSET_VALUES[j] == BTN_DPAD_UP || BUTTON_OFFSET_VALUES[j] == BTN_DPAD_LEFT)
  298. ? (pressed == 0) ? 0 : -1 : (pressed == 0) ? 0 : 1;
  299. e_count++;
  300. break;
  301. default:
  302. break;
  303. }
  304. events[e_count].type = EV_KEY;
  305. events[e_count].code = BUTTON_OFFSET_VALUES[j];
  306. events[e_count].value = (pressed == 0) ? 0 : 1;
  307. e_count++;
  308. port->buttons &= ~mask;
  309. port->buttons |= pressed;
  310. }
  311. }
  312. for (int j = 0; j < 6; j++)
  313. {
  314. unsigned char value = payload[j+3];
  315. if (AXIS_OFFSET_VALUES[j] == ABS_Y || AXIS_OFFSET_VALUES[j] == ABS_RY)
  316. value ^= 0xFF; // flip from 0 - 255 to 255 - 0
  317. if (port->axis[j] != value)
  318. {
  319. events[e_count].type = EV_ABS;
  320. events[e_count].code = AXIS_OFFSET_VALUES[j];
  321. events[e_count].value = value;
  322. e_count++;
  323. port->axis[j] = value;
  324. }
  325. }
  326. if (e_count > 0)
  327. {
  328. events[e_count].type = EV_SYN;
  329. events[e_count].code = SYN_REPORT;
  330. e_count++;
  331. size_t to_write = sizeof(events[0]) * e_count;
  332. size_t written = 0;
  333. while (written < to_write)
  334. {
  335. ssize_t write_ret = write(port->uinput, (const char*)events + written, to_write - written);
  336. if (write_ret < 0)
  337. {
  338. char msg[128];
  339. strerror_r(errno, msg, sizeof(msg));
  340. fprintf(stderr, "Warning: writing input events failed: %s\n", msg);
  341. break;
  342. }
  343. written += write_ret;
  344. }
  345. }
  346. // check for rumble events
  347. struct input_event e;
  348. ssize_t ret = read(port->uinput, &e, sizeof(e));
  349. if (ret == sizeof(e))
  350. {
  351. if (e.type == EV_UINPUT)
  352. {
  353. switch (e.code)
  354. {
  355. case UI_FF_UPLOAD:
  356. {
  357. struct uinput_ff_upload upload = { 0 };
  358. upload.request_id = e.value;
  359. ioctl(port->uinput, UI_BEGIN_FF_UPLOAD, &upload);
  360. int id = create_ff_event(port, &upload);
  361. if (id < 0)
  362. {
  363. // TODO: what's the proper error code for this?
  364. upload.retval = -1;
  365. }
  366. else
  367. {
  368. upload.retval = 0;
  369. upload.effect.id = id;
  370. }
  371. ioctl(port->uinput, UI_END_FF_UPLOAD, &upload);
  372. break;
  373. }
  374. case UI_FF_ERASE:
  375. {
  376. struct uinput_ff_erase erase = { 0 };
  377. erase.request_id = e.value;
  378. ioctl(port->uinput, UI_BEGIN_FF_ERASE, &erase);
  379. if (erase.effect_id < MAX_FF_EVENTS)
  380. port->ff_events[erase.effect_id].in_use = false;
  381. ioctl(port->uinput, UI_END_FF_ERASE, &erase);
  382. }
  383. }
  384. }
  385. else if (e.type == EV_FF)
  386. {
  387. if (e.code < MAX_FF_EVENTS && port->ff_events[e.code].in_use)
  388. {
  389. port->ff_events[e.code].repetitions = e.value;
  390. update_ff_start_stop(&port->ff_events[e.code], current_time);
  391. }
  392. }
  393. }
  394. }
  395. static void *adapter_thread(void *data)
  396. {
  397. struct adapter *a = (struct adapter *)data;
  398. while (!a->quitting)
  399. {
  400. unsigned char payload[37];
  401. int size = 0;
  402. libusb_interrupt_transfer(a->handle, EP_IN, payload, sizeof(payload), &size, 100);
  403. if (size != 37 || payload[0] != 0x21)
  404. continue;
  405. unsigned char *controller = &payload[1];
  406. unsigned char rumble[5] = { 0x11, 0, 0, 0, 0 };
  407. struct timespec current_time = { 0 };
  408. clock_gettime(CLOCK_REALTIME, &current_time);
  409. for (int i = 0; i < 4; i++, controller += 9)
  410. {
  411. handle_payload(i, &a->controllers[i], controller, &current_time);
  412. rumble[i+1] = 0;
  413. if (a->controllers[i].extra_power && a->controllers[i].type == STATE_NORMAL)
  414. {
  415. for (int j = 0; j < MAX_FF_EVENTS; j++)
  416. {
  417. struct ff_event *e = &a->controllers[i].ff_events[j];
  418. if (e->in_use)
  419. {
  420. if (ts_lessthan(&e->start_time, &current_time) && ts_greaterthan(&e->end_time, &current_time))
  421. rumble[i+1] = 1;
  422. else
  423. update_ff_start_stop(e, &current_time);
  424. }
  425. }
  426. }
  427. }
  428. if (memcmp(rumble, a->rumble, sizeof(rumble)) != 0)
  429. {
  430. memcpy(a->rumble, rumble, sizeof(rumble));
  431. libusb_interrupt_transfer(a->handle, EP_OUT, a->rumble, sizeof(a->rumble), &size, 100);
  432. }
  433. }
  434. for (int i = 0; i < 4; i++)
  435. {
  436. if (a->controllers[i].connected)
  437. uinput_destroy(i, &a->controllers[i]);
  438. }
  439. return NULL;
  440. }
  441. static void add_adapter(struct libusb_device *dev)
  442. {
  443. struct adapter *a = (struct adapter *)calloc(1, sizeof(struct adapter));
  444. if (a == NULL)
  445. {
  446. fprintf(stderr, "FATAL: calloc() failed");
  447. exit(-1);
  448. }
  449. a->device = dev;
  450. if (libusb_open(a->device, &a->handle) != 0)
  451. {
  452. fprintf(stderr, "Error opening device %p\n", a->device);
  453. return;
  454. }
  455. if (libusb_kernel_driver_active(a->handle, 0) == 1 && libusb_detach_kernel_driver(a->handle, 0))
  456. {
  457. fprintf(stderr, "Error detaching handle %p from kernel\n", a->handle);
  458. return;
  459. }
  460. if (libusb_claim_interface(a->handle, 0) != 0)
  461. {
  462. fprintf(stderr, "Error claiming interface 0 on adapter 0x%p from kernel\n", a->handle);
  463. return;
  464. }
  465. int tmp;
  466. unsigned char payload[1] = { 0x13 };
  467. libusb_interrupt_transfer(a->handle, EP_OUT, payload, sizeof(payload), &tmp, 100);
  468. struct adapter *old_head = adapters.next;
  469. adapters.next = a;
  470. a->next = old_head;
  471. pthread_create(&a->thread, NULL, adapter_thread, a);
  472. fprintf(stderr, "adapter %p connected\n", a->device);
  473. }
  474. static void remove_adapter(struct libusb_device *dev)
  475. {
  476. struct adapter *a = &adapters;
  477. while (a->next != NULL)
  478. {
  479. if (a->next->device == dev)
  480. {
  481. a->next->quitting = true;
  482. libusb_release_interface(a->next->handle, 0);
  483. pthread_join(a->next->thread, NULL);
  484. fprintf(stderr, "adapter %p disconnected\n", a->next->device);
  485. libusb_close(a->next->handle);
  486. struct adapter *new_next = a->next->next;
  487. free(a->next);
  488. a->next = new_next;
  489. return;
  490. }
  491. a = a->next;
  492. }
  493. }
  494. static int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev, libusb_hotplug_event event, void *user_data)
  495. {
  496. (void)ctx;
  497. (void)user_data;
  498. if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)
  499. {
  500. add_adapter(dev);
  501. }
  502. else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
  503. {
  504. remove_adapter(dev);
  505. }
  506. return 0;
  507. }
  508. static void quitting_signal(int sig)
  509. {
  510. (void)sig;
  511. quitting = 1;
  512. }
  513. int main(int argc, char *argv[])
  514. {
  515. struct udev *udev;
  516. struct udev_device *uinput;
  517. struct sigaction sa;
  518. memset(&sa, 0, sizeof(sa));
  519. if (argc > 1 && (strcmp(argv[1], "-r") == 0 || strcmp(argv[1], "--raw") == 0))
  520. {
  521. fprintf(stderr, "raw mode enabled\n");
  522. raw_mode = true;
  523. }
  524. sa.sa_handler = quitting_signal;
  525. sa.sa_flags = SA_RESTART | SA_RESETHAND;
  526. sigemptyset(&sa.sa_mask);
  527. sigaction(SIGINT, &sa, NULL);
  528. sigaction(SIGTERM, &sa, NULL);
  529. udev = udev_new();
  530. if (udev == NULL) {
  531. fprintf(stderr, "udev init errors\n");
  532. return -1;
  533. }
  534. uinput = udev_device_new_from_subsystem_sysname(udev, "misc", "uinput");
  535. if (uinput == NULL)
  536. {
  537. fprintf(stderr, "uinput creation failed\n");
  538. return -1;
  539. }
  540. uinput_path = udev_device_get_devnode(uinput);
  541. if (uinput_path == NULL)
  542. {
  543. fprintf(stderr, "cannot find path to uinput\n");
  544. return -1;
  545. }
  546. libusb_init(NULL);
  547. struct libusb_device **devices;
  548. int count = libusb_get_device_list(NULL, &devices);
  549. for (int i = 0; i < count; i++)
  550. {
  551. struct libusb_device_descriptor desc;
  552. libusb_get_device_descriptor(devices[i], &desc);
  553. if (desc.idVendor == 0x057e && desc.idProduct == 0x0337)
  554. add_adapter(devices[i]);
  555. }
  556. if (count > 0)
  557. libusb_free_device_list(devices, 1);
  558. libusb_hotplug_callback_handle callback;
  559. int hotplug_ret = libusb_hotplug_register_callback(NULL, LIBUSB_HOTPLUG_MATCH_ANY, 0, 0x057e, 0x0337, LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL, &callback);
  560. if (hotplug_ret != 0)
  561. fprintf(stderr, "cannot register hotplug callback, hotplugging not enabled\n");
  562. // pump events until shutdown & all helper threads finish cleaning up
  563. while (!quitting)
  564. libusb_handle_events_completed(NULL, (int *)&quitting);
  565. while (adapters.next)
  566. remove_adapter(adapters.next->device);
  567. if (hotplug_ret == 0)
  568. libusb_hotplug_deregister_callback(NULL, callback);
  569. libusb_exit(NULL);
  570. udev_device_unref(uinput);
  571. udev_unref(udev);
  572. return 0;
  573. }