hid-uclogic-core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. /* Driver data */
  22. struct uclogic_drvdata {
  23. /* Interface parameters */
  24. struct uclogic_params params;
  25. /* Pointer to the replacement report descriptor. NULL if none. */
  26. __u8 *desc_ptr;
  27. /*
  28. * Size of the replacement report descriptor.
  29. * Only valid if desc_ptr is not NULL
  30. */
  31. unsigned int desc_size;
  32. /* Pen input device */
  33. struct input_dev *pen_input;
  34. /* In-range timer */
  35. struct timer_list inrange_timer;
  36. /* Last rotary encoder state, or U8_MAX for none */
  37. u8 re_state;
  38. };
  39. /**
  40. * uclogic_inrange_timeout - handle pen in-range state timeout.
  41. * Emulate input events normally generated when pen goes out of range for
  42. * tablets which don't report that.
  43. *
  44. * @t: The timer the timeout handler is attached to, stored in a struct
  45. * uclogic_drvdata.
  46. */
  47. static void uclogic_inrange_timeout(struct timer_list *t)
  48. {
  49. struct uclogic_drvdata *drvdata = from_timer(drvdata, t,
  50. inrange_timer);
  51. struct input_dev *input = drvdata->pen_input;
  52. if (input == NULL)
  53. return;
  54. input_report_abs(input, ABS_PRESSURE, 0);
  55. /* If BTN_TOUCH state is changing */
  56. if (test_bit(BTN_TOUCH, input->key)) {
  57. input_event(input, EV_MSC, MSC_SCAN,
  58. /* Digitizer Tip Switch usage */
  59. 0xd0042);
  60. input_report_key(input, BTN_TOUCH, 0);
  61. }
  62. input_report_key(input, BTN_TOOL_PEN, 0);
  63. input_sync(input);
  64. }
  65. static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  66. unsigned int *rsize)
  67. {
  68. struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
  69. if (drvdata->desc_ptr != NULL) {
  70. rdesc = drvdata->desc_ptr;
  71. *rsize = drvdata->desc_size;
  72. }
  73. return rdesc;
  74. }
  75. static int uclogic_input_mapping(struct hid_device *hdev,
  76. struct hid_input *hi,
  77. struct hid_field *field,
  78. struct hid_usage *usage,
  79. unsigned long **bit,
  80. int *max)
  81. {
  82. struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
  83. struct uclogic_params *params = &drvdata->params;
  84. /* discard the unused pen interface */
  85. if (params->pen_unused && (field->application == HID_DG_PEN))
  86. return -1;
  87. /* let hid-core decide what to do */
  88. return 0;
  89. }
  90. static int uclogic_input_configured(struct hid_device *hdev,
  91. struct hid_input *hi)
  92. {
  93. struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
  94. struct uclogic_params *params = &drvdata->params;
  95. char *name;
  96. const char *suffix = NULL;
  97. struct hid_field *field;
  98. size_t len;
  99. /* no report associated (HID_QUIRK_MULTI_INPUT not set) */
  100. if (!hi->report)
  101. return 0;
  102. /*
  103. * If this is the input corresponding to the pen report
  104. * in need of tweaking.
  105. */
  106. if (hi->report->id == params->pen.id) {
  107. /* Remember the input device so we can simulate events */
  108. drvdata->pen_input = hi->input;
  109. }
  110. field = hi->report->field[0];
  111. switch (field->application) {
  112. case HID_GD_KEYBOARD:
  113. suffix = "Keyboard";
  114. break;
  115. case HID_GD_MOUSE:
  116. suffix = "Mouse";
  117. break;
  118. case HID_GD_KEYPAD:
  119. suffix = "Pad";
  120. break;
  121. case HID_DG_PEN:
  122. suffix = "Pen";
  123. break;
  124. case HID_CP_CONSUMER_CONTROL:
  125. suffix = "Consumer Control";
  126. break;
  127. case HID_GD_SYSTEM_CONTROL:
  128. suffix = "System Control";
  129. break;
  130. }
  131. if (suffix) {
  132. len = strlen(hdev->name) + 2 + strlen(suffix);
  133. name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
  134. if (name) {
  135. snprintf(name, len, "%s %s", hdev->name, suffix);
  136. hi->input->name = name;
  137. }
  138. }
  139. return 0;
  140. }
  141. static int uclogic_probe(struct hid_device *hdev,
  142. const struct hid_device_id *id)
  143. {
  144. int rc;
  145. struct uclogic_drvdata *drvdata = NULL;
  146. bool params_initialized = false;
  147. if (!hid_is_usb(hdev))
  148. return -EINVAL;
  149. /*
  150. * libinput requires the pad interface to be on a different node
  151. * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
  152. */
  153. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  154. /* Allocate and assign driver data */
  155. drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
  156. if (drvdata == NULL) {
  157. rc = -ENOMEM;
  158. goto failure;
  159. }
  160. timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
  161. drvdata->re_state = U8_MAX;
  162. hid_set_drvdata(hdev, drvdata);
  163. /* Initialize the device and retrieve interface parameters */
  164. rc = uclogic_params_init(&drvdata->params, hdev);
  165. if (rc != 0) {
  166. hid_err(hdev, "failed probing parameters: %d\n", rc);
  167. goto failure;
  168. }
  169. params_initialized = true;
  170. hid_dbg(hdev, "parameters:\n" UCLOGIC_PARAMS_FMT_STR,
  171. UCLOGIC_PARAMS_FMT_ARGS(&drvdata->params));
  172. if (drvdata->params.invalid) {
  173. hid_info(hdev, "interface is invalid, ignoring\n");
  174. rc = -ENODEV;
  175. goto failure;
  176. }
  177. /* Generate replacement report descriptor */
  178. rc = uclogic_params_get_desc(&drvdata->params,
  179. &drvdata->desc_ptr,
  180. &drvdata->desc_size);
  181. if (rc) {
  182. hid_err(hdev,
  183. "failed generating replacement report descriptor: %d\n",
  184. rc);
  185. goto failure;
  186. }
  187. rc = hid_parse(hdev);
  188. if (rc) {
  189. hid_err(hdev, "parse failed\n");
  190. goto failure;
  191. }
  192. rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  193. if (rc) {
  194. hid_err(hdev, "hw start failed\n");
  195. goto failure;
  196. }
  197. return 0;
  198. failure:
  199. /* Assume "remove" might not be called if "probe" failed */
  200. if (params_initialized)
  201. uclogic_params_cleanup(&drvdata->params);
  202. return rc;
  203. }
  204. #ifdef CONFIG_PM
  205. static int uclogic_resume(struct hid_device *hdev)
  206. {
  207. int rc;
  208. struct uclogic_params params;
  209. /* Re-initialize the device, but discard parameters */
  210. rc = uclogic_params_init(&params, hdev);
  211. if (rc != 0)
  212. hid_err(hdev, "failed to re-initialize the device\n");
  213. else
  214. uclogic_params_cleanup(&params);
  215. return rc;
  216. }
  217. #endif
  218. static int uclogic_raw_event(struct hid_device *hdev,
  219. struct hid_report *report,
  220. u8 *data, int size)
  221. {
  222. struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
  223. struct uclogic_params *params = &drvdata->params;
  224. /* Tweak pen reports, if necessary */
  225. if (!params->pen_unused &&
  226. (report->type == HID_INPUT_REPORT) &&
  227. (report->id == params->pen.id) &&
  228. (size >= 2)) {
  229. /* If it's the "virtual" frame controls report */
  230. if (params->frame.id != 0 &&
  231. data[1] & params->pen_frame_flag) {
  232. /* Change to virtual frame controls report ID */
  233. data[0] = params->frame.id;
  234. return 0;
  235. }
  236. /* If in-range reports are inverted */
  237. if (params->pen.inrange ==
  238. UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
  239. /* Invert the in-range bit */
  240. data[1] ^= 0x40;
  241. }
  242. /*
  243. * If report contains fragmented high-resolution pen
  244. * coordinates
  245. */
  246. if (size >= 10 && params->pen.fragmented_hires) {
  247. u8 pressure_low_byte;
  248. u8 pressure_high_byte;
  249. /* Lift pressure bytes */
  250. pressure_low_byte = data[6];
  251. pressure_high_byte = data[7];
  252. /*
  253. * Move Y coord to make space for high-order X
  254. * coord byte
  255. */
  256. data[6] = data[5];
  257. data[5] = data[4];
  258. /* Move high-order X coord byte */
  259. data[4] = data[8];
  260. /* Move high-order Y coord byte */
  261. data[7] = data[9];
  262. /* Place pressure bytes */
  263. data[8] = pressure_low_byte;
  264. data[9] = pressure_high_byte;
  265. }
  266. /* If we need to emulate in-range detection */
  267. if (params->pen.inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
  268. /* Set in-range bit */
  269. data[1] |= 0x40;
  270. /* (Re-)start in-range timeout */
  271. mod_timer(&drvdata->inrange_timer,
  272. jiffies + msecs_to_jiffies(100));
  273. }
  274. }
  275. /* Tweak frame control reports, if necessary */
  276. if ((report->type == HID_INPUT_REPORT) &&
  277. (report->id == params->frame.id)) {
  278. /* If need to, and can, set pad device ID for Wacom drivers */
  279. if (params->frame.dev_id_byte > 0 &&
  280. params->frame.dev_id_byte < size) {
  281. data[params->frame.dev_id_byte] = 0xf;
  282. }
  283. /* If need to, and can, read rotary encoder state change */
  284. if (params->frame.re_lsb > 0 &&
  285. params->frame.re_lsb / 8 < size) {
  286. unsigned int byte = params->frame.re_lsb / 8;
  287. unsigned int bit = params->frame.re_lsb % 8;
  288. u8 change;
  289. u8 prev_state = drvdata->re_state;
  290. /* Read Gray-coded state */
  291. u8 state = (data[byte] >> bit) & 0x3;
  292. /* Encode state change into 2-bit signed integer */
  293. if ((prev_state == 1 && state == 0) ||
  294. (prev_state == 2 && state == 3)) {
  295. change = 1;
  296. } else if ((prev_state == 2 && state == 0) ||
  297. (prev_state == 1 && state == 3)) {
  298. change = 3;
  299. } else {
  300. change = 0;
  301. }
  302. /* Write change */
  303. data[byte] = (data[byte] & ~((u8)3 << bit)) |
  304. (change << bit);
  305. /* Remember state */
  306. drvdata->re_state = state;
  307. }
  308. }
  309. return 0;
  310. }
  311. static void uclogic_remove(struct hid_device *hdev)
  312. {
  313. struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
  314. del_timer_sync(&drvdata->inrange_timer);
  315. hid_hw_stop(hdev);
  316. kfree(drvdata->desc_ptr);
  317. uclogic_params_cleanup(&drvdata->params);
  318. }
  319. static const struct hid_device_id uclogic_devices[] = {
  320. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  321. USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
  322. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  323. USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
  324. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  325. USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
  326. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  327. USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
  328. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  329. USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
  330. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  331. USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
  332. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  333. USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
  334. { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
  335. USB_DEVICE_ID_HUION_TABLET) },
  336. { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
  337. USB_DEVICE_ID_HUION_HS64) },
  338. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  339. USB_DEVICE_ID_HUION_TABLET) },
  340. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  341. USB_DEVICE_ID_YIYNOVA_TABLET) },
  342. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  343. USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
  344. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  345. USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
  346. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  347. USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
  348. { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
  349. USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
  350. { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
  351. USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
  352. { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
  353. USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
  354. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  355. USB_DEVICE_ID_UGEE_TABLET_G5) },
  356. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  357. USB_DEVICE_ID_UGEE_TABLET_EX07S) },
  358. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  359. USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
  360. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  361. USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
  362. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  363. USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
  364. { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
  365. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
  366. { }
  367. };
  368. MODULE_DEVICE_TABLE(hid, uclogic_devices);
  369. static struct hid_driver uclogic_driver = {
  370. .name = "uclogic",
  371. .id_table = uclogic_devices,
  372. .probe = uclogic_probe,
  373. .remove = uclogic_remove,
  374. .report_fixup = uclogic_report_fixup,
  375. .raw_event = uclogic_raw_event,
  376. .input_mapping = uclogic_input_mapping,
  377. .input_configured = uclogic_input_configured,
  378. #ifdef CONFIG_PM
  379. .resume = uclogic_resume,
  380. .reset_resume = uclogic_resume,
  381. #endif
  382. };
  383. module_hid_driver(uclogic_driver);
  384. MODULE_AUTHOR("Martin Rusko");
  385. MODULE_AUTHOR("Nikolai Kondrashov");
  386. MODULE_LICENSE("GPL");