hid-uclogic-params.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * HID driver for UC-Logic devices not fully compliant with HID standard
  4. * - tablet initialization and parameter retrieval
  5. *
  6. * Copyright (c) 2018 Nikolai Kondrashov
  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 "hid-uclogic-params.h"
  15. #include "hid-uclogic-rdesc.h"
  16. #include "usbhid/usbhid.h"
  17. #include "hid-ids.h"
  18. #include <linux/ctype.h>
  19. #include <linux/string.h>
  20. #include <linux/unaligned.h>
  21. /**
  22. * uclogic_params_pen_inrange_to_str() - Convert a pen in-range reporting type
  23. * to a string.
  24. * @inrange: The in-range reporting type to convert.
  25. *
  26. * Return:
  27. * * The string representing the type, or
  28. * * %NULL if the type is unknown.
  29. */
  30. static const char *uclogic_params_pen_inrange_to_str(
  31. enum uclogic_params_pen_inrange inrange)
  32. {
  33. switch (inrange) {
  34. case UCLOGIC_PARAMS_PEN_INRANGE_NORMAL:
  35. return "normal";
  36. case UCLOGIC_PARAMS_PEN_INRANGE_INVERTED:
  37. return "inverted";
  38. case UCLOGIC_PARAMS_PEN_INRANGE_NONE:
  39. return "none";
  40. default:
  41. return NULL;
  42. }
  43. }
  44. /**
  45. * uclogic_params_pen_hid_dbg() - Dump tablet interface pen parameters
  46. * @hdev: The HID device the pen parameters describe.
  47. * @pen: The pen parameters to dump.
  48. *
  49. * Dump tablet interface pen parameters with hid_dbg(). The dump is indented
  50. * with a tab.
  51. */
  52. static void uclogic_params_pen_hid_dbg(const struct hid_device *hdev,
  53. const struct uclogic_params_pen *pen)
  54. {
  55. size_t i;
  56. hid_dbg(hdev, "\t.usage_invalid = %s\n",
  57. (pen->usage_invalid ? "true" : "false"));
  58. hid_dbg(hdev, "\t.desc_ptr = %p\n", pen->desc_ptr);
  59. hid_dbg(hdev, "\t.desc_size = %u\n", pen->desc_size);
  60. hid_dbg(hdev, "\t.id = %u\n", pen->id);
  61. hid_dbg(hdev, "\t.subreport_list = {\n");
  62. for (i = 0; i < ARRAY_SIZE(pen->subreport_list); i++) {
  63. hid_dbg(hdev, "\t\t{0x%02hhx, %hhu}%s\n",
  64. pen->subreport_list[i].value,
  65. pen->subreport_list[i].id,
  66. i < (ARRAY_SIZE(pen->subreport_list) - 1) ? "," : "");
  67. }
  68. hid_dbg(hdev, "\t}\n");
  69. hid_dbg(hdev, "\t.inrange = %s\n",
  70. uclogic_params_pen_inrange_to_str(pen->inrange));
  71. hid_dbg(hdev, "\t.fragmented_hires = %s\n",
  72. (pen->fragmented_hires ? "true" : "false"));
  73. hid_dbg(hdev, "\t.tilt_y_flipped = %s\n",
  74. (pen->tilt_y_flipped ? "true" : "false"));
  75. }
  76. /**
  77. * uclogic_params_frame_hid_dbg() - Dump tablet interface frame parameters
  78. * @hdev: The HID device the pen parameters describe.
  79. * @frame: The frame parameters to dump.
  80. *
  81. * Dump tablet interface frame parameters with hid_dbg(). The dump is
  82. * indented with two tabs.
  83. */
  84. static void uclogic_params_frame_hid_dbg(
  85. const struct hid_device *hdev,
  86. const struct uclogic_params_frame *frame)
  87. {
  88. hid_dbg(hdev, "\t\t.desc_ptr = %p\n", frame->desc_ptr);
  89. hid_dbg(hdev, "\t\t.desc_size = %u\n", frame->desc_size);
  90. hid_dbg(hdev, "\t\t.id = %u\n", frame->id);
  91. hid_dbg(hdev, "\t\t.suffix = %s\n", frame->suffix);
  92. hid_dbg(hdev, "\t\t.re_lsb = %u\n", frame->re_lsb);
  93. hid_dbg(hdev, "\t\t.dev_id_byte = %u\n", frame->dev_id_byte);
  94. hid_dbg(hdev, "\t\t.touch_byte = %u\n", frame->touch_byte);
  95. hid_dbg(hdev, "\t\t.touch_max = %hhd\n", frame->touch_max);
  96. hid_dbg(hdev, "\t\t.touch_flip_at = %hhd\n",
  97. frame->touch_flip_at);
  98. hid_dbg(hdev, "\t\t.bitmap_dial_byte = %u\n",
  99. frame->bitmap_dial_byte);
  100. }
  101. /**
  102. * uclogic_params_hid_dbg() - Dump tablet interface parameters
  103. * @hdev: The HID device the parameters describe.
  104. * @params: The parameters to dump.
  105. *
  106. * Dump tablet interface parameters with hid_dbg().
  107. */
  108. void uclogic_params_hid_dbg(const struct hid_device *hdev,
  109. const struct uclogic_params *params)
  110. {
  111. size_t i;
  112. hid_dbg(hdev, ".invalid = %s\n",
  113. params->invalid ? "true" : "false");
  114. hid_dbg(hdev, ".desc_ptr = %p\n", params->desc_ptr);
  115. hid_dbg(hdev, ".desc_size = %u\n", params->desc_size);
  116. hid_dbg(hdev, ".pen = {\n");
  117. uclogic_params_pen_hid_dbg(hdev, &params->pen);
  118. hid_dbg(hdev, "\t}\n");
  119. hid_dbg(hdev, ".frame_list = {\n");
  120. for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
  121. hid_dbg(hdev, "\t{\n");
  122. uclogic_params_frame_hid_dbg(hdev, &params->frame_list[i]);
  123. hid_dbg(hdev, "\t}%s\n",
  124. i < (ARRAY_SIZE(params->frame_list) - 1) ? "," : "");
  125. }
  126. hid_dbg(hdev, "}\n");
  127. }
  128. /**
  129. * uclogic_params_get_str_desc - retrieve a string descriptor from a HID
  130. * device interface, putting it into a kmalloc-allocated buffer as is, without
  131. * character encoding conversion.
  132. *
  133. * @pbuf: Location for the kmalloc-allocated buffer pointer containing
  134. * the retrieved descriptor. Not modified in case of error.
  135. * Can be NULL to have retrieved descriptor discarded.
  136. * @hdev: The HID device of the tablet interface to retrieve the string
  137. * descriptor from. Cannot be NULL.
  138. * @idx: Index of the string descriptor to request from the device.
  139. * @len: Length of the buffer to allocate and the data to retrieve.
  140. *
  141. * Returns:
  142. * number of bytes retrieved (<= len),
  143. * -EPIPE, if the descriptor was not found, or
  144. * another negative errno code in case of other error.
  145. */
  146. static int uclogic_params_get_str_desc(__u8 **pbuf, struct hid_device *hdev,
  147. __u8 idx, size_t len)
  148. {
  149. int rc;
  150. struct usb_device *udev;
  151. __u8 *buf = NULL;
  152. /* Check arguments */
  153. if (hdev == NULL) {
  154. rc = -EINVAL;
  155. goto cleanup;
  156. }
  157. udev = hid_to_usb_dev(hdev);
  158. buf = kmalloc(len, GFP_KERNEL);
  159. if (buf == NULL) {
  160. rc = -ENOMEM;
  161. goto cleanup;
  162. }
  163. rc = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  164. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  165. (USB_DT_STRING << 8) + idx,
  166. 0x0409, buf, len,
  167. USB_CTRL_GET_TIMEOUT);
  168. if (rc == -EPIPE) {
  169. hid_dbg(hdev, "string descriptor #%hhu not found\n", idx);
  170. goto cleanup;
  171. } else if (rc < 0) {
  172. hid_err(hdev,
  173. "failed retrieving string descriptor #%u: %d\n",
  174. idx, rc);
  175. goto cleanup;
  176. }
  177. if (pbuf != NULL) {
  178. *pbuf = buf;
  179. buf = NULL;
  180. }
  181. cleanup:
  182. kfree(buf);
  183. return rc;
  184. }
  185. /**
  186. * uclogic_params_pen_cleanup - free resources used by struct
  187. * uclogic_params_pen (tablet interface's pen input parameters).
  188. * Can be called repeatedly.
  189. *
  190. * @pen: Pen input parameters to cleanup. Cannot be NULL.
  191. */
  192. static void uclogic_params_pen_cleanup(struct uclogic_params_pen *pen)
  193. {
  194. kfree(pen->desc_ptr);
  195. memset(pen, 0, sizeof(*pen));
  196. }
  197. /**
  198. * uclogic_params_pen_init_v1() - initialize tablet interface pen
  199. * input and retrieve its parameters from the device, using v1 protocol.
  200. *
  201. * @pen: Pointer to the pen parameters to initialize (to be
  202. * cleaned up with uclogic_params_pen_cleanup()). Not modified in
  203. * case of error, or if parameters are not found. Cannot be NULL.
  204. * @pfound: Location for a flag which is set to true if the parameters
  205. * were found, and to false if not (e.g. device was
  206. * incompatible). Not modified in case of error. Cannot be NULL.
  207. * @hdev: The HID device of the tablet interface to initialize and get
  208. * parameters from. Cannot be NULL.
  209. *
  210. * Returns:
  211. * Zero, if successful. A negative errno code on error.
  212. */
  213. static int uclogic_params_pen_init_v1(struct uclogic_params_pen *pen,
  214. bool *pfound,
  215. struct hid_device *hdev)
  216. {
  217. int rc;
  218. bool found = false;
  219. /* Buffer for (part of) the string descriptor */
  220. __u8 *buf = NULL;
  221. /* Minimum descriptor length required, maximum seen so far is 18 */
  222. const int len = 12;
  223. s32 resolution;
  224. /* Pen report descriptor template parameters */
  225. s32 desc_params[UCLOGIC_RDESC_PH_ID_NUM];
  226. __u8 *desc_ptr = NULL;
  227. /* Check arguments */
  228. if (pen == NULL || pfound == NULL || hdev == NULL) {
  229. rc = -EINVAL;
  230. goto cleanup;
  231. }
  232. /*
  233. * Read string descriptor containing pen input parameters.
  234. * The specific string descriptor and data were discovered by sniffing
  235. * the Windows driver traffic.
  236. * NOTE: This enables fully-functional tablet mode.
  237. */
  238. rc = uclogic_params_get_str_desc(&buf, hdev, 100, len);
  239. if (rc == -EPIPE) {
  240. hid_dbg(hdev,
  241. "string descriptor with pen parameters not found, assuming not compatible\n");
  242. goto finish;
  243. } else if (rc < 0) {
  244. hid_err(hdev, "failed retrieving pen parameters: %d\n", rc);
  245. goto cleanup;
  246. } else if (rc != len) {
  247. hid_dbg(hdev,
  248. "string descriptor with pen parameters has invalid length (got %d, expected %d), assuming not compatible\n",
  249. rc, len);
  250. goto finish;
  251. }
  252. /*
  253. * Fill report descriptor parameters from the string descriptor
  254. */
  255. desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_LM] =
  256. get_unaligned_le16(buf + 2);
  257. desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_LM] =
  258. get_unaligned_le16(buf + 4);
  259. desc_params[UCLOGIC_RDESC_PEN_PH_ID_PRESSURE_LM] =
  260. get_unaligned_le16(buf + 8);
  261. resolution = get_unaligned_le16(buf + 10);
  262. if (resolution == 0) {
  263. desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_PM] = 0;
  264. desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_PM] = 0;
  265. } else {
  266. desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_PM] =
  267. desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_LM] * 1000 /
  268. resolution;
  269. desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_PM] =
  270. desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_LM] * 1000 /
  271. resolution;
  272. }
  273. kfree(buf);
  274. buf = NULL;
  275. /*
  276. * Generate pen report descriptor
  277. */
  278. desc_ptr = uclogic_rdesc_template_apply(
  279. uclogic_rdesc_v1_pen_template_arr,
  280. uclogic_rdesc_v1_pen_template_size,
  281. desc_params, ARRAY_SIZE(desc_params));
  282. if (desc_ptr == NULL) {
  283. rc = -ENOMEM;
  284. goto cleanup;
  285. }
  286. /*
  287. * Fill-in the parameters
  288. */
  289. memset(pen, 0, sizeof(*pen));
  290. pen->desc_ptr = desc_ptr;
  291. desc_ptr = NULL;
  292. pen->desc_size = uclogic_rdesc_v1_pen_template_size;
  293. pen->id = UCLOGIC_RDESC_V1_PEN_ID;
  294. pen->inrange = UCLOGIC_PARAMS_PEN_INRANGE_INVERTED;
  295. found = true;
  296. finish:
  297. *pfound = found;
  298. rc = 0;
  299. cleanup:
  300. kfree(desc_ptr);
  301. kfree(buf);
  302. return rc;
  303. }
  304. /**
  305. * uclogic_params_get_le24() - get a 24-bit little-endian number from a
  306. * buffer.
  307. *
  308. * @p: The pointer to the number buffer.
  309. *
  310. * Returns:
  311. * The retrieved number
  312. */
  313. static s32 uclogic_params_get_le24(const void *p)
  314. {
  315. const __u8 *b = p;
  316. return b[0] | (b[1] << 8UL) | (b[2] << 16UL);
  317. }
  318. /**
  319. * uclogic_params_pen_init_v2() - initialize tablet interface pen
  320. * input and retrieve its parameters from the device, using v2 protocol.
  321. *
  322. * @pen: Pointer to the pen parameters to initialize (to be
  323. * cleaned up with uclogic_params_pen_cleanup()). Not
  324. * modified in case of error, or if parameters are not
  325. * found. Cannot be NULL.
  326. * @pfound: Location for a flag which is set to true if the
  327. * parameters were found, and to false if not (e.g.
  328. * device was incompatible). Not modified in case of
  329. * error. Cannot be NULL.
  330. * @pparams_ptr: Location for a kmalloc'ed pointer to the retrieved raw
  331. * parameters, which could be used to identify the tablet
  332. * to some extent. Should be freed with kfree after use.
  333. * NULL, if not needed. Not modified in case of error.
  334. * Only set if *pfound is set to true.
  335. * @pparams_len: Location for the length of the retrieved raw
  336. * parameters. NULL, if not needed. Not modified in case
  337. * of error. Only set if *pfound is set to true.
  338. * @hdev: The HID device of the tablet interface to initialize
  339. * and get parameters from. Cannot be NULL.
  340. *
  341. * Returns:
  342. * Zero, if successful. A negative errno code on error.
  343. */
  344. static int uclogic_params_pen_init_v2(struct uclogic_params_pen *pen,
  345. bool *pfound,
  346. __u8 **pparams_ptr,
  347. size_t *pparams_len,
  348. struct hid_device *hdev)
  349. {
  350. int rc;
  351. bool found = false;
  352. /* Buffer for (part of) the parameter string descriptor */
  353. __u8 *buf = NULL;
  354. /* Parameter string descriptor required length */
  355. const int params_len_min = 18;
  356. /* Parameter string descriptor accepted length */
  357. const int params_len_max = 32;
  358. /* Parameter string descriptor received length */
  359. int params_len;
  360. size_t i;
  361. s32 resolution;
  362. /* Pen report descriptor template parameters */
  363. s32 desc_params[UCLOGIC_RDESC_PH_ID_NUM];
  364. __u8 *desc_ptr = NULL;
  365. /* Check arguments */
  366. if (pen == NULL || pfound == NULL || hdev == NULL) {
  367. rc = -EINVAL;
  368. goto cleanup;
  369. }
  370. /*
  371. * Read string descriptor containing pen input parameters.
  372. * The specific string descriptor and data were discovered by sniffing
  373. * the Windows driver traffic.
  374. * NOTE: This enables fully-functional tablet mode.
  375. */
  376. rc = uclogic_params_get_str_desc(&buf, hdev, 200, params_len_max);
  377. if (rc == -EPIPE) {
  378. hid_dbg(hdev,
  379. "string descriptor with pen parameters not found, assuming not compatible\n");
  380. goto finish;
  381. } else if (rc < 0) {
  382. hid_err(hdev, "failed retrieving pen parameters: %d\n", rc);
  383. goto cleanup;
  384. } else if (rc < params_len_min) {
  385. hid_dbg(hdev,
  386. "string descriptor with pen parameters is too short (got %d, expected at least %d), assuming not compatible\n",
  387. rc, params_len_min);
  388. goto finish;
  389. }
  390. params_len = rc;
  391. /*
  392. * Check it's not just a catch-all UTF-16LE-encoded ASCII
  393. * string (such as the model name) some tablets put into all
  394. * unknown string descriptors.
  395. */
  396. for (i = 2;
  397. i < params_len &&
  398. (buf[i] >= 0x20 && buf[i] < 0x7f && buf[i + 1] == 0);
  399. i += 2);
  400. if (i >= params_len) {
  401. hid_dbg(hdev,
  402. "string descriptor with pen parameters seems to contain only text, assuming not compatible\n");
  403. goto finish;
  404. }
  405. /*
  406. * Fill report descriptor parameters from the string descriptor
  407. */
  408. desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_LM] =
  409. uclogic_params_get_le24(buf + 2);
  410. desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_LM] =
  411. uclogic_params_get_le24(buf + 5);
  412. desc_params[UCLOGIC_RDESC_PEN_PH_ID_PRESSURE_LM] =
  413. get_unaligned_le16(buf + 8);
  414. resolution = get_unaligned_le16(buf + 10);
  415. if (resolution == 0) {
  416. desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_PM] = 0;
  417. desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_PM] = 0;
  418. } else {
  419. desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_PM] =
  420. desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_LM] * 1000 /
  421. resolution;
  422. desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_PM] =
  423. desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_LM] * 1000 /
  424. resolution;
  425. }
  426. /*
  427. * Generate pen report descriptor
  428. */
  429. desc_ptr = uclogic_rdesc_template_apply(
  430. uclogic_rdesc_v2_pen_template_arr,
  431. uclogic_rdesc_v2_pen_template_size,
  432. desc_params, ARRAY_SIZE(desc_params));
  433. if (desc_ptr == NULL) {
  434. rc = -ENOMEM;
  435. goto cleanup;
  436. }
  437. /*
  438. * Fill-in the parameters
  439. */
  440. memset(pen, 0, sizeof(*pen));
  441. pen->desc_ptr = desc_ptr;
  442. desc_ptr = NULL;
  443. pen->desc_size = uclogic_rdesc_v2_pen_template_size;
  444. pen->id = UCLOGIC_RDESC_V2_PEN_ID;
  445. pen->inrange = UCLOGIC_PARAMS_PEN_INRANGE_NONE;
  446. pen->fragmented_hires = true;
  447. pen->tilt_y_flipped = true;
  448. found = true;
  449. if (pparams_ptr != NULL) {
  450. *pparams_ptr = buf;
  451. buf = NULL;
  452. }
  453. if (pparams_len != NULL)
  454. *pparams_len = params_len;
  455. finish:
  456. *pfound = found;
  457. rc = 0;
  458. cleanup:
  459. kfree(desc_ptr);
  460. kfree(buf);
  461. return rc;
  462. }
  463. /**
  464. * uclogic_params_frame_cleanup - free resources used by struct
  465. * uclogic_params_frame (tablet interface's frame controls input parameters).
  466. * Can be called repeatedly.
  467. *
  468. * @frame: Frame controls input parameters to cleanup. Cannot be NULL.
  469. */
  470. static void uclogic_params_frame_cleanup(struct uclogic_params_frame *frame)
  471. {
  472. kfree(frame->desc_ptr);
  473. memset(frame, 0, sizeof(*frame));
  474. }
  475. /**
  476. * uclogic_params_frame_init_with_desc() - initialize tablet's frame control
  477. * parameters with a static report descriptor.
  478. *
  479. * @frame: Pointer to the frame parameters to initialize (to be cleaned
  480. * up with uclogic_params_frame_cleanup()). Not modified in case
  481. * of error. Cannot be NULL.
  482. * @desc_ptr: Report descriptor pointer. Can be NULL, if desc_size is zero.
  483. * @desc_size: Report descriptor size.
  484. * @id: Report ID used for frame reports, if they should be tweaked,
  485. * zero if not.
  486. *
  487. * Returns:
  488. * Zero, if successful. A negative errno code on error.
  489. */
  490. static int uclogic_params_frame_init_with_desc(
  491. struct uclogic_params_frame *frame,
  492. const __u8 *desc_ptr,
  493. size_t desc_size,
  494. unsigned int id)
  495. {
  496. __u8 *copy_desc_ptr;
  497. if (frame == NULL || (desc_ptr == NULL && desc_size != 0))
  498. return -EINVAL;
  499. copy_desc_ptr = kmemdup(desc_ptr, desc_size, GFP_KERNEL);
  500. if (copy_desc_ptr == NULL)
  501. return -ENOMEM;
  502. memset(frame, 0, sizeof(*frame));
  503. frame->desc_ptr = copy_desc_ptr;
  504. frame->desc_size = desc_size;
  505. frame->id = id;
  506. return 0;
  507. }
  508. /**
  509. * uclogic_params_frame_init_v1() - initialize v1 tablet interface frame
  510. * controls.
  511. *
  512. * @frame: Pointer to the frame parameters to initialize (to be cleaned
  513. * up with uclogic_params_frame_cleanup()). Not modified in case
  514. * of error, or if parameters are not found. Cannot be NULL.
  515. * @pfound: Location for a flag which is set to true if the parameters
  516. * were found, and to false if not (e.g. device was
  517. * incompatible). Not modified in case of error. Cannot be NULL.
  518. * @hdev: The HID device of the tablet interface to initialize and get
  519. * parameters from. Cannot be NULL.
  520. *
  521. * Returns:
  522. * Zero, if successful. A negative errno code on error.
  523. */
  524. static int uclogic_params_frame_init_v1(struct uclogic_params_frame *frame,
  525. bool *pfound,
  526. struct hid_device *hdev)
  527. {
  528. int rc;
  529. bool found = false;
  530. struct usb_device *usb_dev;
  531. char *str_buf = NULL;
  532. const size_t str_len = 16;
  533. /* Check arguments */
  534. if (frame == NULL || pfound == NULL || hdev == NULL) {
  535. rc = -EINVAL;
  536. goto cleanup;
  537. }
  538. usb_dev = hid_to_usb_dev(hdev);
  539. /*
  540. * Enable generic button mode
  541. */
  542. str_buf = kzalloc(str_len, GFP_KERNEL);
  543. if (str_buf == NULL) {
  544. rc = -ENOMEM;
  545. goto cleanup;
  546. }
  547. rc = usb_string(usb_dev, 123, str_buf, str_len);
  548. if (rc == -EPIPE) {
  549. hid_dbg(hdev,
  550. "generic button -enabling string descriptor not found\n");
  551. } else if (rc < 0) {
  552. goto cleanup;
  553. } else if (strncmp(str_buf, "HK On", rc) != 0) {
  554. hid_dbg(hdev,
  555. "invalid response to enabling generic buttons: \"%s\"\n",
  556. str_buf);
  557. } else {
  558. hid_dbg(hdev, "generic buttons enabled\n");
  559. rc = uclogic_params_frame_init_with_desc(
  560. frame,
  561. uclogic_rdesc_v1_frame_arr,
  562. uclogic_rdesc_v1_frame_size,
  563. UCLOGIC_RDESC_V1_FRAME_ID);
  564. if (rc != 0)
  565. goto cleanup;
  566. found = true;
  567. }
  568. *pfound = found;
  569. rc = 0;
  570. cleanup:
  571. kfree(str_buf);
  572. return rc;
  573. }
  574. /**
  575. * uclogic_params_cleanup_event_hooks - free resources used by the list of raw
  576. * event hooks.
  577. * Can be called repeatedly.
  578. *
  579. * @params: Input parameters to cleanup. Cannot be NULL.
  580. */
  581. static void uclogic_params_cleanup_event_hooks(struct uclogic_params *params)
  582. {
  583. struct uclogic_raw_event_hook *curr, *n;
  584. if (!params || !params->event_hooks)
  585. return;
  586. list_for_each_entry_safe(curr, n, &params->event_hooks->list, list) {
  587. cancel_work_sync(&curr->work);
  588. list_del(&curr->list);
  589. kfree(curr->event);
  590. kfree(curr);
  591. }
  592. kfree(params->event_hooks);
  593. params->event_hooks = NULL;
  594. }
  595. /**
  596. * uclogic_params_cleanup - free resources used by struct uclogic_params
  597. * (tablet interface's parameters).
  598. * Can be called repeatedly.
  599. *
  600. * @params: Input parameters to cleanup. Cannot be NULL.
  601. */
  602. void uclogic_params_cleanup(struct uclogic_params *params)
  603. {
  604. if (!params->invalid) {
  605. size_t i;
  606. kfree(params->desc_ptr);
  607. uclogic_params_pen_cleanup(&params->pen);
  608. for (i = 0; i < ARRAY_SIZE(params->frame_list); i++)
  609. uclogic_params_frame_cleanup(&params->frame_list[i]);
  610. uclogic_params_cleanup_event_hooks(params);
  611. memset(params, 0, sizeof(*params));
  612. }
  613. }
  614. /**
  615. * uclogic_params_get_desc() - Get a replacement report descriptor for a
  616. * tablet's interface.
  617. *
  618. * @params: The parameters of a tablet interface to get report
  619. * descriptor for. Cannot be NULL.
  620. * @pdesc: Location for the resulting, kmalloc-allocated report
  621. * descriptor pointer, or for NULL, if there's no replacement
  622. * report descriptor. Not modified in case of error. Cannot be
  623. * NULL.
  624. * @psize: Location for the resulting report descriptor size, not set if
  625. * there's no replacement report descriptor. Not modified in case
  626. * of error. Cannot be NULL.
  627. *
  628. * Returns:
  629. * Zero, if successful.
  630. * -EINVAL, if invalid arguments are supplied.
  631. * -ENOMEM, if failed to allocate memory.
  632. */
  633. int uclogic_params_get_desc(const struct uclogic_params *params,
  634. const __u8 **pdesc,
  635. unsigned int *psize)
  636. {
  637. int rc = -ENOMEM;
  638. bool present = false;
  639. unsigned int size = 0;
  640. __u8 *desc = NULL;
  641. size_t i;
  642. /* Check arguments */
  643. if (params == NULL || pdesc == NULL || psize == NULL)
  644. return -EINVAL;
  645. /* Concatenate descriptors */
  646. #define ADD_DESC(_desc_ptr, _desc_size) \
  647. do { \
  648. unsigned int new_size; \
  649. __u8 *new_desc; \
  650. if ((_desc_ptr) == NULL) { \
  651. break; \
  652. } \
  653. new_size = size + (_desc_size); \
  654. new_desc = krealloc(desc, new_size, GFP_KERNEL); \
  655. if (new_desc == NULL) { \
  656. goto cleanup; \
  657. } \
  658. memcpy(new_desc + size, (_desc_ptr), (_desc_size)); \
  659. desc = new_desc; \
  660. size = new_size; \
  661. present = true; \
  662. } while (0)
  663. ADD_DESC(params->desc_ptr, params->desc_size);
  664. ADD_DESC(params->pen.desc_ptr, params->pen.desc_size);
  665. for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
  666. ADD_DESC(params->frame_list[i].desc_ptr,
  667. params->frame_list[i].desc_size);
  668. }
  669. #undef ADD_DESC
  670. if (present) {
  671. *pdesc = desc;
  672. *psize = size;
  673. desc = NULL;
  674. }
  675. rc = 0;
  676. cleanup:
  677. kfree(desc);
  678. return rc;
  679. }
  680. /**
  681. * uclogic_params_init_invalid() - initialize tablet interface parameters,
  682. * specifying the interface is invalid.
  683. *
  684. * @params: Parameters to initialize (to be cleaned with
  685. * uclogic_params_cleanup()). Cannot be NULL.
  686. */
  687. static void uclogic_params_init_invalid(struct uclogic_params *params)
  688. {
  689. params->invalid = true;
  690. }
  691. /**
  692. * uclogic_params_init_with_opt_desc() - initialize tablet interface
  693. * parameters with an optional replacement report descriptor. Only modify
  694. * report descriptor, if the original report descriptor matches the expected
  695. * size.
  696. *
  697. * @params: Parameters to initialize (to be cleaned with
  698. * uclogic_params_cleanup()). Not modified in case of
  699. * error. Cannot be NULL.
  700. * @hdev: The HID device of the tablet interface create the
  701. * parameters for. Cannot be NULL.
  702. * @orig_desc_size: Expected size of the original report descriptor to
  703. * be replaced.
  704. * @desc_ptr: Pointer to the replacement report descriptor.
  705. * Can be NULL, if desc_size is zero.
  706. * @desc_size: Size of the replacement report descriptor.
  707. *
  708. * Returns:
  709. * Zero, if successful. -EINVAL if an invalid argument was passed.
  710. * -ENOMEM, if failed to allocate memory.
  711. */
  712. static int uclogic_params_init_with_opt_desc(struct uclogic_params *params,
  713. struct hid_device *hdev,
  714. unsigned int orig_desc_size,
  715. const __u8 *desc_ptr,
  716. unsigned int desc_size)
  717. {
  718. __u8 *desc_copy_ptr = NULL;
  719. unsigned int desc_copy_size;
  720. int rc;
  721. /* Check arguments */
  722. if (params == NULL || hdev == NULL ||
  723. (desc_ptr == NULL && desc_size != 0)) {
  724. rc = -EINVAL;
  725. goto cleanup;
  726. }
  727. /* Replace report descriptor, if it matches */
  728. if (hdev->dev_rsize == orig_desc_size) {
  729. hid_dbg(hdev,
  730. "device report descriptor matches the expected size, replacing\n");
  731. desc_copy_ptr = kmemdup(desc_ptr, desc_size, GFP_KERNEL);
  732. if (desc_copy_ptr == NULL) {
  733. rc = -ENOMEM;
  734. goto cleanup;
  735. }
  736. desc_copy_size = desc_size;
  737. } else {
  738. hid_dbg(hdev,
  739. "device report descriptor doesn't match the expected size (%u != %u), preserving\n",
  740. hdev->dev_rsize, orig_desc_size);
  741. desc_copy_ptr = NULL;
  742. desc_copy_size = 0;
  743. }
  744. /* Output parameters */
  745. memset(params, 0, sizeof(*params));
  746. params->desc_ptr = desc_copy_ptr;
  747. desc_copy_ptr = NULL;
  748. params->desc_size = desc_copy_size;
  749. rc = 0;
  750. cleanup:
  751. kfree(desc_copy_ptr);
  752. return rc;
  753. }
  754. /**
  755. * uclogic_params_huion_init() - initialize a Huion tablet interface and discover
  756. * its parameters.
  757. *
  758. * @params: Parameters to fill in (to be cleaned with
  759. * uclogic_params_cleanup()). Not modified in case of error.
  760. * Cannot be NULL.
  761. * @hdev: The HID device of the tablet interface to initialize and get
  762. * parameters from. Cannot be NULL.
  763. *
  764. * Returns:
  765. * Zero, if successful. A negative errno code on error.
  766. */
  767. static int uclogic_params_huion_init(struct uclogic_params *params,
  768. struct hid_device *hdev)
  769. {
  770. int rc;
  771. struct usb_device *udev;
  772. struct usb_interface *iface;
  773. __u8 bInterfaceNumber;
  774. bool found;
  775. /* The resulting parameters (noop) */
  776. struct uclogic_params p = {0, };
  777. static const char transition_ver[] = "HUION_T153_160607";
  778. char *ver_ptr = NULL;
  779. const size_t ver_len = sizeof(transition_ver) + 1;
  780. __u8 *params_ptr = NULL;
  781. size_t params_len = 0;
  782. /* Parameters string descriptor of a model with touch ring (HS610) */
  783. const __u8 touch_ring_model_params_buf[] = {
  784. 0x13, 0x03, 0x70, 0xC6, 0x00, 0x06, 0x7C, 0x00,
  785. 0xFF, 0x1F, 0xD8, 0x13, 0x03, 0x0D, 0x10, 0x01,
  786. 0x04, 0x3C, 0x3E
  787. };
  788. /* Check arguments */
  789. if (params == NULL || hdev == NULL) {
  790. rc = -EINVAL;
  791. goto cleanup;
  792. }
  793. udev = hid_to_usb_dev(hdev);
  794. iface = to_usb_interface(hdev->dev.parent);
  795. bInterfaceNumber = iface->cur_altsetting->desc.bInterfaceNumber;
  796. /* If it's a custom keyboard interface */
  797. if (bInterfaceNumber == 1) {
  798. /* Keep everything intact, but mark pen usage invalid */
  799. p.pen.usage_invalid = true;
  800. goto output;
  801. /* Else, if it's not a pen interface */
  802. } else if (bInterfaceNumber != 0) {
  803. uclogic_params_init_invalid(&p);
  804. goto output;
  805. }
  806. /* Try to get firmware version */
  807. ver_ptr = kzalloc(ver_len, GFP_KERNEL);
  808. if (ver_ptr == NULL) {
  809. rc = -ENOMEM;
  810. goto cleanup;
  811. }
  812. rc = usb_string(udev, 201, ver_ptr, ver_len);
  813. if (rc == -EPIPE) {
  814. *ver_ptr = '\0';
  815. } else if (rc < 0) {
  816. hid_err(hdev,
  817. "failed retrieving Huion firmware version: %d\n", rc);
  818. goto cleanup;
  819. }
  820. /* The firmware is used in userspace as unique identifier */
  821. strscpy(hdev->uniq, ver_ptr, sizeof(hdev->uniq));
  822. /* If this is a transition firmware */
  823. if (strcmp(ver_ptr, transition_ver) == 0) {
  824. hid_dbg(hdev,
  825. "transition firmware detected, not probing pen v2 parameters\n");
  826. } else {
  827. /* Try to probe v2 pen parameters */
  828. rc = uclogic_params_pen_init_v2(&p.pen, &found,
  829. &params_ptr, &params_len,
  830. hdev);
  831. if (rc != 0) {
  832. hid_err(hdev,
  833. "failed probing pen v2 parameters: %d\n", rc);
  834. goto cleanup;
  835. } else if (found) {
  836. hid_dbg(hdev, "pen v2 parameters found\n");
  837. /* Create v2 frame button parameters */
  838. rc = uclogic_params_frame_init_with_desc(
  839. &p.frame_list[0],
  840. uclogic_rdesc_v2_frame_buttons_arr,
  841. uclogic_rdesc_v2_frame_buttons_size,
  842. UCLOGIC_RDESC_V2_FRAME_BUTTONS_ID);
  843. if (rc != 0) {
  844. hid_err(hdev,
  845. "failed creating v2 frame button parameters: %d\n",
  846. rc);
  847. goto cleanup;
  848. }
  849. /* Link from pen sub-report */
  850. p.pen.subreport_list[0].value = 0xe0;
  851. p.pen.subreport_list[0].id =
  852. UCLOGIC_RDESC_V2_FRAME_BUTTONS_ID;
  853. /* If this is the model with touch ring */
  854. if (params_ptr != NULL &&
  855. params_len == sizeof(touch_ring_model_params_buf) &&
  856. memcmp(params_ptr, touch_ring_model_params_buf,
  857. params_len) == 0) {
  858. /* Create touch ring parameters */
  859. rc = uclogic_params_frame_init_with_desc(
  860. &p.frame_list[1],
  861. uclogic_rdesc_v2_frame_touch_ring_arr,
  862. uclogic_rdesc_v2_frame_touch_ring_size,
  863. UCLOGIC_RDESC_V2_FRAME_TOUCH_ID);
  864. if (rc != 0) {
  865. hid_err(hdev,
  866. "failed creating v2 frame touch ring parameters: %d\n",
  867. rc);
  868. goto cleanup;
  869. }
  870. p.frame_list[1].suffix = "Touch Ring";
  871. p.frame_list[1].dev_id_byte =
  872. UCLOGIC_RDESC_V2_FRAME_TOUCH_DEV_ID_BYTE;
  873. p.frame_list[1].touch_byte = 5;
  874. p.frame_list[1].touch_max = 12;
  875. p.frame_list[1].touch_flip_at = 7;
  876. } else {
  877. /* Create touch strip parameters */
  878. rc = uclogic_params_frame_init_with_desc(
  879. &p.frame_list[1],
  880. uclogic_rdesc_v2_frame_touch_strip_arr,
  881. uclogic_rdesc_v2_frame_touch_strip_size,
  882. UCLOGIC_RDESC_V2_FRAME_TOUCH_ID);
  883. if (rc != 0) {
  884. hid_err(hdev,
  885. "failed creating v2 frame touch strip parameters: %d\n",
  886. rc);
  887. goto cleanup;
  888. }
  889. p.frame_list[1].suffix = "Touch Strip";
  890. p.frame_list[1].dev_id_byte =
  891. UCLOGIC_RDESC_V2_FRAME_TOUCH_DEV_ID_BYTE;
  892. p.frame_list[1].touch_byte = 5;
  893. p.frame_list[1].touch_max = 8;
  894. }
  895. /* Link from pen sub-report */
  896. p.pen.subreport_list[1].value = 0xf0;
  897. p.pen.subreport_list[1].id =
  898. UCLOGIC_RDESC_V2_FRAME_TOUCH_ID;
  899. /* Create v2 frame dial parameters */
  900. rc = uclogic_params_frame_init_with_desc(
  901. &p.frame_list[2],
  902. uclogic_rdesc_v2_frame_dial_arr,
  903. uclogic_rdesc_v2_frame_dial_size,
  904. UCLOGIC_RDESC_V2_FRAME_DIAL_ID);
  905. if (rc != 0) {
  906. hid_err(hdev,
  907. "failed creating v2 frame dial parameters: %d\n",
  908. rc);
  909. goto cleanup;
  910. }
  911. p.frame_list[2].suffix = "Dial";
  912. p.frame_list[2].dev_id_byte =
  913. UCLOGIC_RDESC_V2_FRAME_DIAL_DEV_ID_BYTE;
  914. p.frame_list[2].bitmap_dial_byte = 5;
  915. /* Link from pen sub-report */
  916. p.pen.subreport_list[2].value = 0xf1;
  917. p.pen.subreport_list[2].id =
  918. UCLOGIC_RDESC_V2_FRAME_DIAL_ID;
  919. goto output;
  920. }
  921. hid_dbg(hdev, "pen v2 parameters not found\n");
  922. }
  923. /* Try to probe v1 pen parameters */
  924. rc = uclogic_params_pen_init_v1(&p.pen, &found, hdev);
  925. if (rc != 0) {
  926. hid_err(hdev,
  927. "failed probing pen v1 parameters: %d\n", rc);
  928. goto cleanup;
  929. } else if (found) {
  930. hid_dbg(hdev, "pen v1 parameters found\n");
  931. /* Try to probe v1 frame */
  932. rc = uclogic_params_frame_init_v1(&p.frame_list[0],
  933. &found, hdev);
  934. if (rc != 0) {
  935. hid_err(hdev, "v1 frame probing failed: %d\n", rc);
  936. goto cleanup;
  937. }
  938. hid_dbg(hdev, "frame v1 parameters%s found\n",
  939. (found ? "" : " not"));
  940. if (found) {
  941. /* Link frame button subreports from pen reports */
  942. p.pen.subreport_list[0].value = 0xe0;
  943. p.pen.subreport_list[0].id =
  944. UCLOGIC_RDESC_V1_FRAME_ID;
  945. }
  946. goto output;
  947. }
  948. hid_dbg(hdev, "pen v1 parameters not found\n");
  949. uclogic_params_init_invalid(&p);
  950. output:
  951. /* Output parameters */
  952. memcpy(params, &p, sizeof(*params));
  953. memset(&p, 0, sizeof(p));
  954. rc = 0;
  955. cleanup:
  956. kfree(params_ptr);
  957. kfree(ver_ptr);
  958. uclogic_params_cleanup(&p);
  959. return rc;
  960. }
  961. /**
  962. * uclogic_probe_interface() - some tablets, like the Parblo A610 PLUS V2 or
  963. * the XP-PEN Deco Mini 7, need to be initialized by sending them magic data.
  964. *
  965. * @hdev: The HID device of the tablet interface to initialize and get
  966. * parameters from. Cannot be NULL.
  967. * @magic_arr: The magic data that should be sent to probe the interface.
  968. * Cannot be NULL.
  969. * @magic_size: Size of the magic data.
  970. * @endpoint: Endpoint where the magic data should be sent.
  971. *
  972. * Returns:
  973. * Zero, if successful. A negative errno code on error.
  974. */
  975. static int uclogic_probe_interface(struct hid_device *hdev, const u8 *magic_arr,
  976. size_t magic_size, int endpoint)
  977. {
  978. struct usb_device *udev;
  979. unsigned int pipe = 0;
  980. int sent;
  981. u8 *buf = NULL;
  982. int rc = 0;
  983. if (!hdev || !magic_arr) {
  984. rc = -EINVAL;
  985. goto cleanup;
  986. }
  987. buf = kmemdup(magic_arr, magic_size, GFP_KERNEL);
  988. if (!buf) {
  989. rc = -ENOMEM;
  990. goto cleanup;
  991. }
  992. udev = hid_to_usb_dev(hdev);
  993. pipe = usb_sndintpipe(udev, endpoint);
  994. rc = usb_interrupt_msg(udev, pipe, buf, magic_size, &sent, 1000);
  995. if (rc || sent != magic_size) {
  996. hid_err(hdev, "Interface probing failed: %d\n", rc);
  997. rc = -1;
  998. goto cleanup;
  999. }
  1000. rc = 0;
  1001. cleanup:
  1002. kfree(buf);
  1003. return rc;
  1004. }
  1005. /**
  1006. * uclogic_params_parse_ugee_v2_desc - parse the string descriptor containing
  1007. * pen and frame parameters returned by UGEE v2 devices.
  1008. *
  1009. * @str_desc: String descriptor, cannot be NULL.
  1010. * @str_desc_size: Size of the string descriptor.
  1011. * @desc_params: Output description params list.
  1012. * @desc_params_size: Size of the output description params list.
  1013. * @frame_type: Output frame type.
  1014. *
  1015. * Returns:
  1016. * Zero, if successful. A negative errno code on error.
  1017. */
  1018. static int uclogic_params_parse_ugee_v2_desc(const __u8 *str_desc,
  1019. size_t str_desc_size,
  1020. s32 *desc_params,
  1021. size_t desc_params_size,
  1022. enum uclogic_params_frame_type *frame_type)
  1023. {
  1024. s32 pen_x_lm, pen_y_lm;
  1025. s32 pen_x_pm, pen_y_pm;
  1026. s32 pen_pressure_lm;
  1027. s32 frame_num_buttons;
  1028. s32 resolution;
  1029. /* Minimum descriptor length required, maximum seen so far is 14 */
  1030. const int min_str_desc_size = 12;
  1031. if (!str_desc || str_desc_size < min_str_desc_size)
  1032. return -EINVAL;
  1033. if (desc_params_size != UCLOGIC_RDESC_PH_ID_NUM)
  1034. return -EINVAL;
  1035. pen_x_lm = get_unaligned_le16(str_desc + 2);
  1036. pen_y_lm = get_unaligned_le16(str_desc + 4);
  1037. frame_num_buttons = str_desc[6];
  1038. *frame_type = str_desc[7];
  1039. pen_pressure_lm = get_unaligned_le16(str_desc + 8);
  1040. resolution = get_unaligned_le16(str_desc + 10);
  1041. if (resolution == 0) {
  1042. pen_x_pm = 0;
  1043. pen_y_pm = 0;
  1044. } else {
  1045. pen_x_pm = pen_x_lm * 1000 / resolution;
  1046. pen_y_pm = pen_y_lm * 1000 / resolution;
  1047. }
  1048. desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_LM] = pen_x_lm;
  1049. desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_PM] = pen_x_pm;
  1050. desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_LM] = pen_y_lm;
  1051. desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_PM] = pen_y_pm;
  1052. desc_params[UCLOGIC_RDESC_PEN_PH_ID_PRESSURE_LM] = pen_pressure_lm;
  1053. desc_params[UCLOGIC_RDESC_FRAME_PH_ID_UM] = frame_num_buttons;
  1054. return 0;
  1055. }
  1056. /**
  1057. * uclogic_params_ugee_v2_init_frame_buttons() - initialize a UGEE v2 frame with
  1058. * buttons.
  1059. * @p: Parameters to fill in, cannot be NULL.
  1060. * @desc_params: Device description params list.
  1061. * @desc_params_size: Size of the description params list.
  1062. *
  1063. * Returns:
  1064. * Zero, if successful. A negative errno code on error.
  1065. */
  1066. static int uclogic_params_ugee_v2_init_frame_buttons(struct uclogic_params *p,
  1067. const s32 *desc_params,
  1068. size_t desc_params_size)
  1069. {
  1070. __u8 *rdesc_frame = NULL;
  1071. int rc = 0;
  1072. if (!p || desc_params_size != UCLOGIC_RDESC_PH_ID_NUM)
  1073. return -EINVAL;
  1074. rdesc_frame = uclogic_rdesc_template_apply(
  1075. uclogic_rdesc_ugee_v2_frame_btn_template_arr,
  1076. uclogic_rdesc_ugee_v2_frame_btn_template_size,
  1077. desc_params, UCLOGIC_RDESC_PH_ID_NUM);
  1078. if (!rdesc_frame)
  1079. return -ENOMEM;
  1080. rc = uclogic_params_frame_init_with_desc(&p->frame_list[0],
  1081. rdesc_frame,
  1082. uclogic_rdesc_ugee_v2_frame_btn_template_size,
  1083. UCLOGIC_RDESC_V1_FRAME_ID);
  1084. kfree(rdesc_frame);
  1085. return rc;
  1086. }
  1087. /**
  1088. * uclogic_params_ugee_v2_init_frame_dial() - initialize a UGEE v2 frame with a
  1089. * bitmap dial.
  1090. * @p: Parameters to fill in, cannot be NULL.
  1091. * @desc_params: Device description params list.
  1092. * @desc_params_size: Size of the description params list.
  1093. *
  1094. * Returns:
  1095. * Zero, if successful. A negative errno code on error.
  1096. */
  1097. static int uclogic_params_ugee_v2_init_frame_dial(struct uclogic_params *p,
  1098. const s32 *desc_params,
  1099. size_t desc_params_size)
  1100. {
  1101. __u8 *rdesc_frame = NULL;
  1102. int rc = 0;
  1103. if (!p || desc_params_size != UCLOGIC_RDESC_PH_ID_NUM)
  1104. return -EINVAL;
  1105. rdesc_frame = uclogic_rdesc_template_apply(
  1106. uclogic_rdesc_ugee_v2_frame_dial_template_arr,
  1107. uclogic_rdesc_ugee_v2_frame_dial_template_size,
  1108. desc_params, UCLOGIC_RDESC_PH_ID_NUM);
  1109. if (!rdesc_frame)
  1110. return -ENOMEM;
  1111. rc = uclogic_params_frame_init_with_desc(&p->frame_list[0],
  1112. rdesc_frame,
  1113. uclogic_rdesc_ugee_v2_frame_dial_template_size,
  1114. UCLOGIC_RDESC_V1_FRAME_ID);
  1115. kfree(rdesc_frame);
  1116. if (rc)
  1117. return rc;
  1118. p->frame_list[0].bitmap_dial_byte = 7;
  1119. return 0;
  1120. }
  1121. /**
  1122. * uclogic_params_ugee_v2_init_frame_mouse() - initialize a UGEE v2 frame with a
  1123. * mouse.
  1124. * @p: Parameters to fill in, cannot be NULL.
  1125. *
  1126. * Returns:
  1127. * Zero, if successful. A negative errno code on error.
  1128. */
  1129. static int uclogic_params_ugee_v2_init_frame_mouse(struct uclogic_params *p)
  1130. {
  1131. int rc = 0;
  1132. if (!p)
  1133. return -EINVAL;
  1134. rc = uclogic_params_frame_init_with_desc(&p->frame_list[1],
  1135. uclogic_rdesc_ugee_v2_frame_mouse_template_arr,
  1136. uclogic_rdesc_ugee_v2_frame_mouse_template_size,
  1137. UCLOGIC_RDESC_V1_FRAME_ID);
  1138. return rc;
  1139. }
  1140. /**
  1141. * uclogic_params_ugee_v2_has_battery() - check whether a UGEE v2 device has
  1142. * battery or not.
  1143. * @hdev: The HID device of the tablet interface.
  1144. *
  1145. * Returns:
  1146. * True if the device has battery, false otherwise.
  1147. */
  1148. static bool uclogic_params_ugee_v2_has_battery(struct hid_device *hdev)
  1149. {
  1150. struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
  1151. if (drvdata->quirks & UCLOGIC_BATTERY_QUIRK)
  1152. return true;
  1153. /* The XP-PEN Deco LW vendor, product and version are identical to the
  1154. * Deco L. The only difference reported by their firmware is the product
  1155. * name. Add a quirk to support battery reporting on the wireless
  1156. * version.
  1157. */
  1158. if (hdev->vendor == USB_VENDOR_ID_UGEE &&
  1159. hdev->product == USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L) {
  1160. struct usb_device *udev = hid_to_usb_dev(hdev);
  1161. if (strstarts(udev->product, "Deco LW"))
  1162. return true;
  1163. }
  1164. return false;
  1165. }
  1166. /**
  1167. * uclogic_params_ugee_v2_init_battery() - initialize UGEE v2 battery reporting.
  1168. * @hdev: The HID device of the tablet interface, cannot be NULL.
  1169. * @p: Parameters to fill in, cannot be NULL.
  1170. *
  1171. * Returns:
  1172. * Zero, if successful. A negative errno code on error.
  1173. */
  1174. static int uclogic_params_ugee_v2_init_battery(struct hid_device *hdev,
  1175. struct uclogic_params *p)
  1176. {
  1177. int rc = 0;
  1178. if (!hdev || !p)
  1179. return -EINVAL;
  1180. /* Some tablets contain invalid characters in hdev->uniq, throwing a
  1181. * "hwmon: '<name>' is not a valid name attribute, please fix" error.
  1182. * Use the device vendor and product IDs instead.
  1183. */
  1184. snprintf(hdev->uniq, sizeof(hdev->uniq), "%x-%x", hdev->vendor,
  1185. hdev->product);
  1186. rc = uclogic_params_frame_init_with_desc(&p->frame_list[1],
  1187. uclogic_rdesc_ugee_v2_battery_template_arr,
  1188. uclogic_rdesc_ugee_v2_battery_template_size,
  1189. UCLOGIC_RDESC_UGEE_V2_BATTERY_ID);
  1190. if (rc)
  1191. return rc;
  1192. p->frame_list[1].suffix = "Battery";
  1193. p->pen.subreport_list[1].value = 0xf2;
  1194. p->pen.subreport_list[1].id = UCLOGIC_RDESC_UGEE_V2_BATTERY_ID;
  1195. return rc;
  1196. }
  1197. /**
  1198. * uclogic_params_ugee_v2_reconnect_work() - When a wireless tablet looses
  1199. * connection to the USB dongle and reconnects, either because of its physical
  1200. * distance or because it was switches off and on using the frame's switch,
  1201. * uclogic_probe_interface() needs to be called again to enable the tablet.
  1202. *
  1203. * @work: The work that triggered this function.
  1204. */
  1205. static void uclogic_params_ugee_v2_reconnect_work(struct work_struct *work)
  1206. {
  1207. struct uclogic_raw_event_hook *event_hook;
  1208. event_hook = container_of(work, struct uclogic_raw_event_hook, work);
  1209. uclogic_probe_interface(event_hook->hdev, uclogic_ugee_v2_probe_arr,
  1210. uclogic_ugee_v2_probe_size,
  1211. uclogic_ugee_v2_probe_endpoint);
  1212. }
  1213. /**
  1214. * uclogic_params_ugee_v2_init_event_hooks() - initialize the list of events
  1215. * to be hooked for UGEE v2 devices.
  1216. * @hdev: The HID device of the tablet interface to initialize and get
  1217. * parameters from.
  1218. * @p: Parameters to fill in, cannot be NULL.
  1219. *
  1220. * Returns:
  1221. * Zero, if successful. A negative errno code on error.
  1222. */
  1223. static int uclogic_params_ugee_v2_init_event_hooks(struct hid_device *hdev,
  1224. struct uclogic_params *p)
  1225. {
  1226. struct uclogic_raw_event_hook *event_hook;
  1227. __u8 reconnect_event[] = {
  1228. /* Event received on wireless tablet reconnection */
  1229. 0x02, 0xF8, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1230. };
  1231. if (!p)
  1232. return -EINVAL;
  1233. /* The reconnection event is only received if the tablet has battery */
  1234. if (!uclogic_params_ugee_v2_has_battery(hdev))
  1235. return 0;
  1236. p->event_hooks = kzalloc(sizeof(*p->event_hooks), GFP_KERNEL);
  1237. if (!p->event_hooks)
  1238. return -ENOMEM;
  1239. INIT_LIST_HEAD(&p->event_hooks->list);
  1240. event_hook = kzalloc(sizeof(*event_hook), GFP_KERNEL);
  1241. if (!event_hook)
  1242. return -ENOMEM;
  1243. INIT_WORK(&event_hook->work, uclogic_params_ugee_v2_reconnect_work);
  1244. event_hook->hdev = hdev;
  1245. event_hook->size = ARRAY_SIZE(reconnect_event);
  1246. event_hook->event = kmemdup(reconnect_event, event_hook->size, GFP_KERNEL);
  1247. if (!event_hook->event)
  1248. return -ENOMEM;
  1249. list_add_tail(&event_hook->list, &p->event_hooks->list);
  1250. return 0;
  1251. }
  1252. /**
  1253. * uclogic_params_ugee_v2_init() - initialize a UGEE graphics tablets by
  1254. * discovering their parameters.
  1255. *
  1256. * These tables, internally designed as v2 to differentiate them from older
  1257. * models, expect a payload of magic data in orther to be switched to the fully
  1258. * functional mode and expose their parameters in a similar way to the
  1259. * information present in uclogic_params_pen_init_v1() but with some
  1260. * differences.
  1261. *
  1262. * @params: Parameters to fill in (to be cleaned with
  1263. * uclogic_params_cleanup()). Not modified in case of error.
  1264. * Cannot be NULL.
  1265. * @hdev: The HID device of the tablet interface to initialize and get
  1266. * parameters from. Cannot be NULL.
  1267. *
  1268. * Returns:
  1269. * Zero, if successful. A negative errno code on error.
  1270. */
  1271. static int uclogic_params_ugee_v2_init(struct uclogic_params *params,
  1272. struct hid_device *hdev)
  1273. {
  1274. int rc = 0;
  1275. struct uclogic_drvdata *drvdata;
  1276. struct usb_interface *iface;
  1277. __u8 bInterfaceNumber;
  1278. const int str_desc_len = 12;
  1279. __u8 *str_desc = NULL;
  1280. __u8 *rdesc_pen = NULL;
  1281. s32 desc_params[UCLOGIC_RDESC_PH_ID_NUM];
  1282. enum uclogic_params_frame_type frame_type;
  1283. /* The resulting parameters (noop) */
  1284. struct uclogic_params p = {0, };
  1285. if (!params || !hdev) {
  1286. rc = -EINVAL;
  1287. goto cleanup;
  1288. }
  1289. drvdata = hid_get_drvdata(hdev);
  1290. iface = to_usb_interface(hdev->dev.parent);
  1291. bInterfaceNumber = iface->cur_altsetting->desc.bInterfaceNumber;
  1292. if (bInterfaceNumber == 0) {
  1293. rc = uclogic_params_ugee_v2_init_frame_mouse(&p);
  1294. if (rc)
  1295. goto cleanup;
  1296. goto output;
  1297. }
  1298. if (bInterfaceNumber != 2) {
  1299. uclogic_params_init_invalid(&p);
  1300. goto output;
  1301. }
  1302. /*
  1303. * Initialize the interface by sending magic data.
  1304. * The specific data was discovered by sniffing the Windows driver
  1305. * traffic.
  1306. */
  1307. rc = uclogic_probe_interface(hdev, uclogic_ugee_v2_probe_arr,
  1308. uclogic_ugee_v2_probe_size,
  1309. uclogic_ugee_v2_probe_endpoint);
  1310. if (rc) {
  1311. uclogic_params_init_invalid(&p);
  1312. goto output;
  1313. }
  1314. /*
  1315. * Read the string descriptor containing pen and frame parameters.
  1316. * The specific string descriptor and data were discovered by sniffing
  1317. * the Windows driver traffic.
  1318. */
  1319. rc = uclogic_params_get_str_desc(&str_desc, hdev, 100, str_desc_len);
  1320. if (rc != str_desc_len) {
  1321. hid_err(hdev, "failed retrieving pen and frame parameters: %d\n", rc);
  1322. uclogic_params_init_invalid(&p);
  1323. goto output;
  1324. }
  1325. rc = uclogic_params_parse_ugee_v2_desc(str_desc, str_desc_len,
  1326. desc_params,
  1327. ARRAY_SIZE(desc_params),
  1328. &frame_type);
  1329. if (rc)
  1330. goto cleanup;
  1331. kfree(str_desc);
  1332. str_desc = NULL;
  1333. /* Initialize the pen interface */
  1334. rdesc_pen = uclogic_rdesc_template_apply(
  1335. uclogic_rdesc_ugee_v2_pen_template_arr,
  1336. uclogic_rdesc_ugee_v2_pen_template_size,
  1337. desc_params, ARRAY_SIZE(desc_params));
  1338. if (!rdesc_pen) {
  1339. rc = -ENOMEM;
  1340. goto cleanup;
  1341. }
  1342. p.pen.desc_ptr = rdesc_pen;
  1343. p.pen.desc_size = uclogic_rdesc_ugee_v2_pen_template_size;
  1344. p.pen.id = 0x02;
  1345. p.pen.subreport_list[0].value = 0xf0;
  1346. p.pen.subreport_list[0].id = UCLOGIC_RDESC_V1_FRAME_ID;
  1347. /* Initialize the frame interface */
  1348. if (drvdata->quirks & UCLOGIC_MOUSE_FRAME_QUIRK)
  1349. frame_type = UCLOGIC_PARAMS_FRAME_MOUSE;
  1350. switch (frame_type) {
  1351. case UCLOGIC_PARAMS_FRAME_DIAL:
  1352. case UCLOGIC_PARAMS_FRAME_MOUSE:
  1353. rc = uclogic_params_ugee_v2_init_frame_dial(&p, desc_params,
  1354. ARRAY_SIZE(desc_params));
  1355. break;
  1356. case UCLOGIC_PARAMS_FRAME_BUTTONS:
  1357. default:
  1358. rc = uclogic_params_ugee_v2_init_frame_buttons(&p, desc_params,
  1359. ARRAY_SIZE(desc_params));
  1360. break;
  1361. }
  1362. if (rc)
  1363. goto cleanup;
  1364. /* Initialize the battery interface*/
  1365. if (uclogic_params_ugee_v2_has_battery(hdev)) {
  1366. rc = uclogic_params_ugee_v2_init_battery(hdev, &p);
  1367. if (rc) {
  1368. hid_err(hdev, "error initializing battery: %d\n", rc);
  1369. goto cleanup;
  1370. }
  1371. }
  1372. /* Create a list of raw events to be ignored */
  1373. rc = uclogic_params_ugee_v2_init_event_hooks(hdev, &p);
  1374. if (rc) {
  1375. hid_err(hdev, "error initializing event hook list: %d\n", rc);
  1376. goto cleanup;
  1377. }
  1378. output:
  1379. /* Output parameters */
  1380. memcpy(params, &p, sizeof(*params));
  1381. memset(&p, 0, sizeof(p));
  1382. rc = 0;
  1383. cleanup:
  1384. kfree(str_desc);
  1385. uclogic_params_cleanup(&p);
  1386. return rc;
  1387. }
  1388. /**
  1389. * uclogic_params_init() - initialize a tablet interface and discover its
  1390. * parameters.
  1391. *
  1392. * @params: Parameters to fill in (to be cleaned with
  1393. * uclogic_params_cleanup()). Not modified in case of error.
  1394. * Cannot be NULL.
  1395. * @hdev: The HID device of the tablet interface to initialize and get
  1396. * parameters from. Cannot be NULL. Must be using the USB low-level
  1397. * driver, i.e. be an actual USB tablet.
  1398. *
  1399. * Returns:
  1400. * Zero, if successful. A negative errno code on error.
  1401. */
  1402. int uclogic_params_init(struct uclogic_params *params,
  1403. struct hid_device *hdev)
  1404. {
  1405. int rc;
  1406. struct usb_device *udev;
  1407. __u8 bNumInterfaces;
  1408. struct usb_interface *iface;
  1409. __u8 bInterfaceNumber;
  1410. bool found;
  1411. /* The resulting parameters (noop) */
  1412. struct uclogic_params p = {0, };
  1413. /* Check arguments */
  1414. if (params == NULL || hdev == NULL || !hid_is_usb(hdev)) {
  1415. rc = -EINVAL;
  1416. goto cleanup;
  1417. }
  1418. udev = hid_to_usb_dev(hdev);
  1419. bNumInterfaces = udev->config->desc.bNumInterfaces;
  1420. iface = to_usb_interface(hdev->dev.parent);
  1421. bInterfaceNumber = iface->cur_altsetting->desc.bInterfaceNumber;
  1422. /*
  1423. * Set replacement report descriptor if the original matches the
  1424. * specified size. Otherwise keep interface unchanged.
  1425. */
  1426. #define WITH_OPT_DESC(_orig_desc_token, _new_desc_token) \
  1427. uclogic_params_init_with_opt_desc( \
  1428. &p, hdev, \
  1429. UCLOGIC_RDESC_##_orig_desc_token##_SIZE, \
  1430. uclogic_rdesc_##_new_desc_token##_arr, \
  1431. uclogic_rdesc_##_new_desc_token##_size)
  1432. #define VID_PID(_vid, _pid) \
  1433. (((__u32)(_vid) << 16) | ((__u32)(_pid) & U16_MAX))
  1434. /*
  1435. * Handle specific interfaces for specific tablets.
  1436. *
  1437. * Observe the following logic:
  1438. *
  1439. * If the interface is recognized as producing certain useful input:
  1440. * Mark interface as valid.
  1441. * Output interface parameters.
  1442. * Else, if the interface is recognized as *not* producing any useful
  1443. * input:
  1444. * Mark interface as invalid.
  1445. * Else:
  1446. * Mark interface as valid.
  1447. * Output noop parameters.
  1448. *
  1449. * Rule of thumb: it is better to disable a broken interface than let
  1450. * it spew garbage input.
  1451. */
  1452. switch (VID_PID(hdev->vendor, hdev->product)) {
  1453. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1454. USB_DEVICE_ID_UCLOGIC_TABLET_PF1209):
  1455. rc = WITH_OPT_DESC(PF1209_ORIG, pf1209_fixed);
  1456. if (rc != 0)
  1457. goto cleanup;
  1458. break;
  1459. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1460. USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U):
  1461. rc = WITH_OPT_DESC(WPXXXXU_ORIG, wp4030u_fixed);
  1462. if (rc != 0)
  1463. goto cleanup;
  1464. break;
  1465. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1466. USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U):
  1467. if (hdev->dev_rsize == UCLOGIC_RDESC_WP5540U_V2_ORIG_SIZE) {
  1468. if (bInterfaceNumber == 0) {
  1469. /* Try to probe v1 pen parameters */
  1470. rc = uclogic_params_pen_init_v1(&p.pen,
  1471. &found, hdev);
  1472. if (rc != 0) {
  1473. hid_err(hdev,
  1474. "pen probing failed: %d\n",
  1475. rc);
  1476. goto cleanup;
  1477. }
  1478. if (!found) {
  1479. hid_warn(hdev,
  1480. "pen parameters not found");
  1481. }
  1482. } else {
  1483. uclogic_params_init_invalid(&p);
  1484. }
  1485. } else {
  1486. rc = WITH_OPT_DESC(WPXXXXU_ORIG, wp5540u_fixed);
  1487. if (rc != 0)
  1488. goto cleanup;
  1489. }
  1490. break;
  1491. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1492. USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U):
  1493. rc = WITH_OPT_DESC(WPXXXXU_ORIG, wp8060u_fixed);
  1494. if (rc != 0)
  1495. goto cleanup;
  1496. break;
  1497. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1498. USB_DEVICE_ID_UCLOGIC_TABLET_WP1062):
  1499. rc = WITH_OPT_DESC(WP1062_ORIG, wp1062_fixed);
  1500. if (rc != 0)
  1501. goto cleanup;
  1502. break;
  1503. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1504. USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850):
  1505. switch (bInterfaceNumber) {
  1506. case 0:
  1507. rc = WITH_OPT_DESC(TWHL850_ORIG0, twhl850_fixed0);
  1508. if (rc != 0)
  1509. goto cleanup;
  1510. break;
  1511. case 1:
  1512. rc = WITH_OPT_DESC(TWHL850_ORIG1, twhl850_fixed1);
  1513. if (rc != 0)
  1514. goto cleanup;
  1515. break;
  1516. case 2:
  1517. rc = WITH_OPT_DESC(TWHL850_ORIG2, twhl850_fixed2);
  1518. if (rc != 0)
  1519. goto cleanup;
  1520. break;
  1521. }
  1522. break;
  1523. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1524. USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60):
  1525. /*
  1526. * If it is not a three-interface version, which is known to
  1527. * respond to initialization.
  1528. */
  1529. if (bNumInterfaces != 3) {
  1530. switch (bInterfaceNumber) {
  1531. case 0:
  1532. rc = WITH_OPT_DESC(TWHA60_ORIG0,
  1533. twha60_fixed0);
  1534. if (rc != 0)
  1535. goto cleanup;
  1536. break;
  1537. case 1:
  1538. rc = WITH_OPT_DESC(TWHA60_ORIG1,
  1539. twha60_fixed1);
  1540. if (rc != 0)
  1541. goto cleanup;
  1542. break;
  1543. }
  1544. break;
  1545. }
  1546. fallthrough;
  1547. case VID_PID(USB_VENDOR_ID_HUION,
  1548. USB_DEVICE_ID_HUION_TABLET):
  1549. case VID_PID(USB_VENDOR_ID_HUION,
  1550. USB_DEVICE_ID_HUION_TABLET2):
  1551. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1552. USB_DEVICE_ID_HUION_TABLET):
  1553. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1554. USB_DEVICE_ID_YIYNOVA_TABLET):
  1555. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1556. USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81):
  1557. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1558. USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3):
  1559. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1560. USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45):
  1561. case VID_PID(USB_VENDOR_ID_UCLOGIC,
  1562. USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47):
  1563. rc = uclogic_params_huion_init(&p, hdev);
  1564. if (rc != 0)
  1565. goto cleanup;
  1566. break;
  1567. case VID_PID(USB_VENDOR_ID_UGTIZER,
  1568. USB_DEVICE_ID_UGTIZER_TABLET_GP0610):
  1569. case VID_PID(USB_VENDOR_ID_UGTIZER,
  1570. USB_DEVICE_ID_UGTIZER_TABLET_GT5040):
  1571. case VID_PID(USB_VENDOR_ID_UGEE,
  1572. USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540):
  1573. case VID_PID(USB_VENDOR_ID_UGEE,
  1574. USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640):
  1575. case VID_PID(USB_VENDOR_ID_UGEE,
  1576. USB_DEVICE_ID_UGEE_XPPEN_TABLET_STAR06):
  1577. case VID_PID(USB_VENDOR_ID_UGEE,
  1578. USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720):
  1579. /* If this is the pen interface */
  1580. if (bInterfaceNumber == 1) {
  1581. /* Probe v1 pen parameters */
  1582. rc = uclogic_params_pen_init_v1(&p.pen, &found, hdev);
  1583. if (rc != 0) {
  1584. hid_err(hdev, "pen probing failed: %d\n", rc);
  1585. goto cleanup;
  1586. }
  1587. if (!found) {
  1588. hid_warn(hdev, "pen parameters not found");
  1589. uclogic_params_init_invalid(&p);
  1590. }
  1591. } else {
  1592. uclogic_params_init_invalid(&p);
  1593. }
  1594. break;
  1595. case VID_PID(USB_VENDOR_ID_UGEE,
  1596. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01):
  1597. /* If this is the pen and frame interface */
  1598. if (bInterfaceNumber == 1) {
  1599. /* Probe v1 pen parameters */
  1600. rc = uclogic_params_pen_init_v1(&p.pen, &found, hdev);
  1601. if (rc != 0) {
  1602. hid_err(hdev, "pen probing failed: %d\n", rc);
  1603. goto cleanup;
  1604. }
  1605. /* Initialize frame parameters */
  1606. rc = uclogic_params_frame_init_with_desc(
  1607. &p.frame_list[0],
  1608. uclogic_rdesc_xppen_deco01_frame_arr,
  1609. uclogic_rdesc_xppen_deco01_frame_size,
  1610. 0);
  1611. if (rc != 0)
  1612. goto cleanup;
  1613. } else {
  1614. uclogic_params_init_invalid(&p);
  1615. }
  1616. break;
  1617. case VID_PID(USB_VENDOR_ID_UGEE,
  1618. USB_DEVICE_ID_UGEE_PARBLO_A610_PRO):
  1619. case VID_PID(USB_VENDOR_ID_UGEE,
  1620. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01_V2):
  1621. case VID_PID(USB_VENDOR_ID_UGEE,
  1622. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L):
  1623. case VID_PID(USB_VENDOR_ID_UGEE,
  1624. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_MW):
  1625. case VID_PID(USB_VENDOR_ID_UGEE,
  1626. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_S):
  1627. case VID_PID(USB_VENDOR_ID_UGEE,
  1628. USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_PRO_SW):
  1629. rc = uclogic_params_ugee_v2_init(&p, hdev);
  1630. if (rc != 0)
  1631. goto cleanup;
  1632. break;
  1633. case VID_PID(USB_VENDOR_ID_TRUST,
  1634. USB_DEVICE_ID_TRUST_PANORA_TABLET):
  1635. case VID_PID(USB_VENDOR_ID_UGEE,
  1636. USB_DEVICE_ID_UGEE_TABLET_G5):
  1637. /* Ignore non-pen interfaces */
  1638. if (bInterfaceNumber != 1) {
  1639. uclogic_params_init_invalid(&p);
  1640. break;
  1641. }
  1642. rc = uclogic_params_pen_init_v1(&p.pen, &found, hdev);
  1643. if (rc != 0) {
  1644. hid_err(hdev, "pen probing failed: %d\n", rc);
  1645. goto cleanup;
  1646. } else if (found) {
  1647. rc = uclogic_params_frame_init_with_desc(
  1648. &p.frame_list[0],
  1649. uclogic_rdesc_ugee_g5_frame_arr,
  1650. uclogic_rdesc_ugee_g5_frame_size,
  1651. UCLOGIC_RDESC_UGEE_G5_FRAME_ID);
  1652. if (rc != 0) {
  1653. hid_err(hdev,
  1654. "failed creating frame parameters: %d\n",
  1655. rc);
  1656. goto cleanup;
  1657. }
  1658. p.frame_list[0].re_lsb =
  1659. UCLOGIC_RDESC_UGEE_G5_FRAME_RE_LSB;
  1660. p.frame_list[0].dev_id_byte =
  1661. UCLOGIC_RDESC_UGEE_G5_FRAME_DEV_ID_BYTE;
  1662. } else {
  1663. hid_warn(hdev, "pen parameters not found");
  1664. uclogic_params_init_invalid(&p);
  1665. }
  1666. break;
  1667. case VID_PID(USB_VENDOR_ID_UGEE,
  1668. USB_DEVICE_ID_UGEE_TABLET_EX07S):
  1669. /* Ignore non-pen interfaces */
  1670. if (bInterfaceNumber != 1) {
  1671. uclogic_params_init_invalid(&p);
  1672. break;
  1673. }
  1674. rc = uclogic_params_pen_init_v1(&p.pen, &found, hdev);
  1675. if (rc != 0) {
  1676. hid_err(hdev, "pen probing failed: %d\n", rc);
  1677. goto cleanup;
  1678. } else if (found) {
  1679. rc = uclogic_params_frame_init_with_desc(
  1680. &p.frame_list[0],
  1681. uclogic_rdesc_ugee_ex07_frame_arr,
  1682. uclogic_rdesc_ugee_ex07_frame_size,
  1683. 0);
  1684. if (rc != 0) {
  1685. hid_err(hdev,
  1686. "failed creating frame parameters: %d\n",
  1687. rc);
  1688. goto cleanup;
  1689. }
  1690. } else {
  1691. hid_warn(hdev, "pen parameters not found");
  1692. uclogic_params_init_invalid(&p);
  1693. }
  1694. break;
  1695. }
  1696. #undef VID_PID
  1697. #undef WITH_OPT_DESC
  1698. /* Output parameters */
  1699. memcpy(params, &p, sizeof(*params));
  1700. memset(&p, 0, sizeof(p));
  1701. rc = 0;
  1702. cleanup:
  1703. uclogic_params_cleanup(&p);
  1704. return rc;
  1705. }
  1706. #ifdef CONFIG_HID_KUNIT_TEST
  1707. #include "hid-uclogic-params-test.c"
  1708. #endif