hid-uclogic-core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * HID driver for UC-Logic devices not fully compliant with HID standard
  4. *
  5. * Copyright (c) 2010-2014 Nikolai Kondrashov
  6. * Copyright (c) 2013 Martin Rusko
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. */
  14. #include <linux/device.h>
  15. #include <linux/hid.h>
  16. #include <linux/module.h>
  17. #include <linux/timer.h>
  18. #include "usbhid/usbhid.h"
  19. #include "hid-uclogic-params.h"
  20. #include "hid-ids.h"
  21. /**
  22. * uclogic_inrange_timeout - handle pen in-range state timeout.
  23. * Emulate input events normally generated when pen goes out of range for
  24. * tablets which don't report that.
  25. *
  26. * @t: The timer the timeout handler is attached to, stored in a struct
  27. * uclogic_drvdata.
  28. */
  29. static void uclogic_inrange_timeout(struct timer_list *t)
  30. {
  31. struct uclogic_drvdata *drvdata = from_timer(drvdata, t,
  32. inrange_timer);
  33. struct input_dev *input = drvdata->pen_input;
  34. if (input == NULL)
  35. return;
  36. input_report_abs(input, ABS_PRESSURE, 0);
  37. /* If BTN_TOUCH state is changing */
  38. if (test_bit(BTN_TOUCH, input->key)) {
  39. input_event(input, EV_MSC, MSC_SCAN,
  40. /* Digitizer Tip Switch usage */
  41. 0xd0042);
  42. input_report_key(input, BTN_TOUCH, 0);
  43. }
  44. input_report_key(input, BTN_TOOL_PEN, 0);
  45. input_sync(input);
  46. }
  47. static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  48. unsigned int *rsize)
  49. {
  50. struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
  51. if (drvdata->desc_ptr != NULL) {
  52. rdesc = drvdata->desc_ptr;
  53. *rsize = drvdata->desc_size;
  54. }
  55. return rdesc;
  56. }
  57. static int uclogic_input_mapping(struct hid_device *hdev,
  58. struct hid_input *hi,
  59. struct hid_field *field,
  60. struct hid_usage *usage,
  61. unsigned long **bit,
  62. int *max)
  63. {
  64. struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
  65. struct uclogic_params *params = &drvdata->params;
  66. /* Discard invalid pen usages */
  67. if (params->pen.usage_invalid && (field->application == HID_DG_PEN))
  68. return -1;
  69. /* Let hid-core decide what to do */
  70. return 0;
  71. }
  72. static int uclogic_input_configured(struct hid_device *hdev,
  73. struct hid_input *hi)
  74. {
  75. struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
  76. struct uclogic_params *params = &drvdata->params;
  77. const char *suffix = NULL;
  78. struct hid_field *field;
  79. size_t i;
  80. const struct uclogic_params_frame *frame;
  81. /* no report associated (HID_QUIRK_MULTI_INPUT not set) */
  82. if (!hi->report)
  83. return 0;
  84. /*
  85. * If this is the input corresponding to the pen report
  86. * in need of tweaking.
  87. */
  88. if (hi->report->id == params->pen.id) {
  89. /* Remember the input device so we can simulate events */
  90. drvdata->pen_input = hi->input;
  91. }
  92. /* If it's one of the frame devices */
  93. for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
  94. frame = &params->frame_list[i];
  95. if (hi->report->id == frame->id) {
  96. /* Assign custom suffix, if any */
  97. suffix = frame->suffix;
  98. /*
  99. * Disable EV_MSC reports for touch ring interfaces to
  100. * make the Wacom driver pickup touch ring extents
  101. */
  102. if (frame->touch_byte > 0)
  103. __clear_bit(EV_MSC, hi->input->evbit);
  104. }
  105. }
  106. if (!suffix) {
  107. field = hi->report->field[0];
  108. switch (field->application) {
  109. case HID_GD_KEYBOARD:
  110. suffix = "Keyboard";
  111. break;
  112. case HID_GD_MOUSE:
  113. suffix = "Mouse";
  114. break;
  115. case HID_GD_KEYPAD:
  116. suffix = "Pad";
  117. break;
  118. case HID_DG_PEN:
  119. case HID_DG_DIGITIZER:
  120. suffix = "Pen";
  121. break;
  122. case HID_CP_CONSUMER_CONTROL:
  123. suffix = "Consumer Control";
  124. break;
  125. case HID_GD_SYSTEM_CONTROL:
  126. suffix = "System Control";
  127. break;
  128. }
  129. }
  130. if (suffix)
  131. hi->input->name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  132. "%s %s", hdev->name, suffix);
  133. return 0;
  134. }
  135. static int uclogic_probe(struct hid_device *hdev,
  136. const struct hid_device_id *id)
  137. {
  138. int rc;
  139. struct uclogic_drvdata *drvdata = NULL;
  140. bool params_initialized = false;
  141. if (!hid_is_usb(hdev))
  142. return -EINVAL;
  143. /*
  144. * libinput requires the pad interface to be on a different node
  145. * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
  146. */
  147. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  148. hdev->quirks |= HID_QUIRK_HIDINPUT_FORCE;
  149. /* Allocate and assign driver data */
  150. drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
  151. if (drvdata == NULL) {
  152. rc = -ENOMEM;
  153. goto failure;
  154. }
  155. timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
  156. drvdata->re_state = U8_MAX;
  157. drvdata->quirks = id->driver_data;
  158. hid_set_drvdata(hdev, drvdata);
  159. /* Initialize the device and retrieve interface parameters */
  160. rc = uclogic_params_init(&drvdata->params, hdev);
  161. if (rc != 0) {
  162. hid_err(hdev, "failed probing parameters: %d\n", rc);
  163. goto failure;
  164. }
  165. params_initialized = true;
  166. hid_dbg(hdev, "parameters:\n");
  167. uclogic_params_hid_dbg(hdev, &drvdata->params);
  168. if (drvdata->params.invalid) {
  169. hid_info(hdev, "interface is invalid, ignoring\n");
  170. rc = -ENODEV;
  171. goto failure;
  172. }
  173. /* Generate replacement report descriptor */
  174. rc = uclogic_params_get_desc(&drvdata->params,
  175. &drvdata->desc_ptr,
  176. &drvdata->desc_size);
  177. if (rc) {
  178. hid_err(hdev,
  179. "failed generating replacement report descriptor: %d\n",
  180. rc);
  181. goto failure;
  182. }
  183. rc = hid_parse(hdev);
  184. if (rc) {
  185. hid_err(hdev, "parse failed\n");
  186. goto failure;
  187. }
  188. rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  189. if (rc) {
  190. hid_err(hdev, "hw start failed\n");
  191. goto failure;
  192. }
  193. return 0;
  194. failure:
  195. /* Assume "remove" might not be called if "probe" failed */
  196. if (params_initialized)
  197. uclogic_params_cleanup(&drvdata->params);
  198. return rc;
  199. }
  200. #ifdef CONFIG_PM
  201. static int uclogic_resume(struct hid_device *hdev)
  202. {
  203. int rc;
  204. struct uclogic_params params;
  205. /* Re-initialize the device, but discard parameters */
  206. rc = uclogic_params_init(&params, hdev);
  207. if (rc != 0)
  208. hid_err(hdev, "failed to re-initialize the device\n");
  209. else
  210. uclogic_params_cleanup(&params);
  211. return rc;
  212. }
  213. #endif
  214. /**
  215. * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
  216. *
  217. * @drvdata: Driver data.
  218. * @data: Report data buffer, can be modified.
  219. * @size: Report data size, bytes.
  220. *
  221. * Returns:
  222. * Negative value on error (stops event delivery), zero for success.
  223. */
  224. static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
  225. u8 *data, int size)
  226. {
  227. struct uclogic_params_pen *pen = &drvdata->params.pen;
  228. WARN_ON(drvdata == NULL);
  229. WARN_ON(data == NULL && size != 0);
  230. /* If in-range reports are inverted */
  231. if (pen->inrange ==
  232. UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
  233. /* Invert the in-range bit */
  234. data[1] ^= 0x40;
  235. }
  236. /*
  237. * If report contains fragmented high-resolution pen
  238. * coordinates
  239. */
  240. if (size >= 10 && pen->fragmented_hires) {
  241. u8 pressure_low_byte;
  242. u8 pressure_high_byte;
  243. /* Lift pressure bytes */
  244. pressure_low_byte = data[6];
  245. pressure_high_byte = data[7];
  246. /*
  247. * Move Y coord to make space for high-order X
  248. * coord byte
  249. */
  250. data[6] = data[5];
  251. data[5] = data[4];
  252. /* Move high-order X coord byte */
  253. data[4] = data[8];
  254. /* Move high-order Y coord byte */
  255. data[7] = data[9];
  256. /* Place pressure bytes */
  257. data[8] = pressure_low_byte;
  258. data[9] = pressure_high_byte;
  259. }
  260. /* If we need to emulate in-range detection */
  261. if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
  262. /* Set in-range bit */
  263. data[1] |= 0x40;
  264. /* (Re-)start in-range timeout */
  265. mod_timer(&drvdata->inrange_timer,
  266. jiffies + msecs_to_jiffies(100));
  267. }
  268. /* If we report tilt and Y direction is flipped */
  269. if (size >= 12 && pen->tilt_y_flipped)
  270. data[11] = -data[11];
  271. return 0;
  272. }
  273. /**
  274. * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
  275. *
  276. * @drvdata: Driver data.
  277. * @frame: The parameters of the frame controls to handle.
  278. * @data: Report data buffer, can be modified.
  279. * @size: Report data size, bytes.
  280. *
  281. * Returns:
  282. * Negative value on error (stops event delivery), zero for success.
  283. */
  284. static int uclogic_raw_event_frame(
  285. struct uclogic_drvdata *drvdata,
  286. const struct uclogic_params_frame *frame,
  287. u8 *data, int size)
  288. {
  289. WARN_ON(drvdata == NULL);
  290. WARN_ON(data == NULL && size != 0);
  291. /* If need to, and can, set pad device ID for Wacom drivers */
  292. if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
  293. /* If we also have a touch ring and the finger left it */
  294. if (frame->touch_byte > 0 && frame->touch_byte < size &&
  295. data[frame->touch_byte] == 0) {
  296. data[frame->dev_id_byte] = 0;
  297. } else {
  298. data[frame->dev_id_byte] = 0xf;
  299. }
  300. }
  301. /* If need to, and can, read rotary encoder state change */
  302. if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
  303. unsigned int byte = frame->re_lsb / 8;
  304. unsigned int bit = frame->re_lsb % 8;
  305. u8 change;
  306. u8 prev_state = drvdata->re_state;
  307. /* Read Gray-coded state */
  308. u8 state = (data[byte] >> bit) & 0x3;
  309. /* Encode state change into 2-bit signed integer */
  310. if ((prev_state == 1 && state == 0) ||
  311. (prev_state == 2 && state == 3)) {
  312. change = 1;
  313. } else if ((prev_state == 2 && state == 0) ||
  314. (prev_state == 1 && state == 3)) {
  315. change = 3;
  316. } else {
  317. change = 0;
  318. }
  319. /* Write change */
  320. data[byte] = (data[byte] & ~((u8)3 << bit)) |
  321. (change << bit);
  322. /* Remember state */
  323. drvdata->re_state = state;
  324. }
  325. /* If need to, and can, transform the touch ring reports */
  326. if (frame->touch_byte > 0 && frame->touch_byte < size) {
  327. __s8 value = data[frame->touch_byte];
  328. if (value != 0) {
  329. if (frame->touch_flip_at != 0) {
  330. value = frame->touch_flip_at - value;
  331. if (value <= 0)
  332. value = frame->touch_max + value;
  333. }
  334. data[frame->touch_byte] = value - 1;
  335. }
  336. }
  337. /* If need to, and can, transform the bitmap dial reports */
  338. if (frame->bitmap_dial_byte > 0 && frame->bitmap_dial_byte < size) {
  339. if (data[frame->bitmap_dial_byte] == 2)
  340. data[frame->bitmap_dial_byte] = -1;
  341. }
  342. return 0;
  343. }
  344. static int uclogic_raw_event(struct hid_device *hdev,
  345. struct hid_report *report,
  346. u8 *data, int size)
  347. {
  348. unsigned int report_id = report->id;
  349. struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
  350. struct uclogic_params *params = &drvdata->params;
  351. struct uclogic_params_pen_subreport *subreport;
  352. struct uclogic_params_pen_subreport *subreport_list_end;
  353. size_t i;
  354. /* Do not handle anything but input reports */
  355. if (report->type != HID_INPUT_REPORT)
  356. return 0;
  357. while (true) {
  358. /* Tweak pen reports, if necessary */
  359. if ((report_id == params->pen.id) && (size >= 2)) {
  360. subreport_list_end =
  361. params->pen.subreport_list +
  362. ARRAY_SIZE(params->pen.subreport_list);
  363. /* Try to match a subreport */
  364. for (subreport = params->pen.subreport_list;
  365. subreport < subreport_list_end; subreport++) {
  366. if (subreport->value != 0 &&
  367. subreport->value == data[1]) {
  368. break;
  369. }
  370. }
  371. /* If a subreport matched */
  372. if (subreport < subreport_list_end) {
  373. /* Change to subreport ID, and restart */
  374. report_id = data[0] = subreport->id;
  375. continue;
  376. } else {
  377. return uclogic_raw_event_pen(drvdata, data, size);
  378. }
  379. }
  380. /* Tweak frame control reports, if necessary */
  381. for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
  382. if (report_id == params->frame_list[i].id) {
  383. return uclogic_raw_event_frame(
  384. drvdata, &params->frame_list[i],
  385. data, size);
  386. }
  387. }
  388. break;
  389. }
  390. return 0;
  391. }
  392. static void uclogic_remove(struct hid_device *hdev)
  393. {
  394. struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
  395. del_timer_sync(&drvdata->inrange_timer);
  396. hid_hw_stop(hdev);
  397. kfree(drvdata->desc_ptr);
  398. uclogic_params_cleanup(&drvdata->params);
  399. }
  400. static const struct hid_device_id uclogic_devices[] = {
  401. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  402. USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
  403. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  404. USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
  405. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  406. USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
  407. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  408. USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
  409. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  410. USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
  411. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  412. USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
  413. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  414. USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
  415. { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
  416. USB_DEVICE_ID_HUION_TABLET) },
  417. { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
  418. USB_DEVICE_ID_HUION_TABLET2) },
  419. { HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
  420. USB_DEVICE_ID_TRUST_PANORA_TABLET) },
  421. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  422. USB_DEVICE_ID_HUION_TABLET) },
  423. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  424. USB_DEVICE_ID_YIYNOVA_TABLET) },
  425. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  426. USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
  427. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  428. USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
  429. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  430. USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
  431. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  432. USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
  433. { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
  434. USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
  435. { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
  436. USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
  437. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  438. USB_DEVICE_ID_UGEE_PARBLO_A610_PRO) },
  439. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  440. USB_DEVICE_ID_UGEE_TABLET_G5) },
  441. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  442. USB_DEVICE_ID_UGEE_TABLET_EX07S) },
  443. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  444. USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
  445. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  446. USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
  447. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  448. USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
  449. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  450. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
  451. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  452. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01_V2) },
  453. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  454. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L) },
  455. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  456. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_MW),
  457. .driver_data = UCLOGIC_MOUSE_FRAME_QUIRK | UCLOGIC_BATTERY_QUIRK },
  458. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  459. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_S) },
  460. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  461. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_SW),
  462. .driver_data = UCLOGIC_MOUSE_FRAME_QUIRK | UCLOGIC_BATTERY_QUIRK },
  463. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  464. USB_DEVICE_ID_UGEE_XPPEN_TABLET_STAR06) },
  465. { }
  466. };
  467. MODULE_DEVICE_TABLE(hid, uclogic_devices);
  468. static struct hid_driver uclogic_driver = {
  469. .name = "uclogic",
  470. .id_table = uclogic_devices,
  471. .probe = uclogic_probe,
  472. .remove = uclogic_remove,
  473. .report_fixup = uclogic_report_fixup,
  474. .raw_event = uclogic_raw_event,
  475. .input_mapping = uclogic_input_mapping,
  476. .input_configured = uclogic_input_configured,
  477. #ifdef CONFIG_PM
  478. .resume = uclogic_resume,
  479. .reset_resume = uclogic_resume,
  480. #endif
  481. };
  482. module_hid_driver(uclogic_driver);
  483. MODULE_AUTHOR("Martin Rusko");
  484. MODULE_AUTHOR("Nikolai Kondrashov");
  485. MODULE_LICENSE("GPL");