wacom_sys.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  1. /*
  2. * drivers/input/tablet/wacom_sys.c
  3. *
  4. * USB Wacom tablet support - system specific code
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include "wacom_wac.h"
  13. #include "wacom.h"
  14. #include <linux/input/mt.h>
  15. #define WAC_MSG_RETRIES 5
  16. #define WAC_CMD_WL_LED_CONTROL 0x03
  17. #define WAC_CMD_LED_CONTROL 0x20
  18. #define WAC_CMD_ICON_START 0x21
  19. #define WAC_CMD_ICON_XFER 0x23
  20. #define WAC_CMD_ICON_BT_XFER 0x26
  21. #define WAC_CMD_RETRIES 10
  22. #define WAC_CMD_DELETE_PAIRING 0x20
  23. #define WAC_CMD_UNPAIR_ALL 0xFF
  24. #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
  25. #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
  26. #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
  27. static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
  28. size_t size, unsigned int retries)
  29. {
  30. int retval;
  31. do {
  32. retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
  33. HID_REQ_GET_REPORT);
  34. } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
  35. if (retval < 0)
  36. hid_err(hdev, "wacom_get_report: ran out of retries "
  37. "(last error = %d)\n", retval);
  38. return retval;
  39. }
  40. static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
  41. size_t size, unsigned int retries)
  42. {
  43. int retval;
  44. do {
  45. retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
  46. HID_REQ_SET_REPORT);
  47. } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
  48. if (retval < 0)
  49. hid_err(hdev, "wacom_set_report: ran out of retries "
  50. "(last error = %d)\n", retval);
  51. return retval;
  52. }
  53. static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
  54. u8 *raw_data, int size)
  55. {
  56. struct wacom *wacom = hid_get_drvdata(hdev);
  57. if (size > WACOM_PKGLEN_MAX)
  58. return 1;
  59. memcpy(wacom->wacom_wac.data, raw_data, size);
  60. wacom_wac_irq(&wacom->wacom_wac, size);
  61. return 0;
  62. }
  63. static int wacom_open(struct input_dev *dev)
  64. {
  65. struct wacom *wacom = input_get_drvdata(dev);
  66. return hid_hw_open(wacom->hdev);
  67. }
  68. static void wacom_close(struct input_dev *dev)
  69. {
  70. struct wacom *wacom = input_get_drvdata(dev);
  71. /*
  72. * wacom->hdev should never be null, but surprisingly, I had the case
  73. * once while unplugging the Wacom Wireless Receiver.
  74. */
  75. if (wacom->hdev)
  76. hid_hw_close(wacom->hdev);
  77. }
  78. /*
  79. * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
  80. */
  81. static int wacom_calc_hid_res(int logical_extents, int physical_extents,
  82. unsigned unit, int exponent)
  83. {
  84. struct hid_field field = {
  85. .logical_maximum = logical_extents,
  86. .physical_maximum = physical_extents,
  87. .unit = unit,
  88. .unit_exponent = exponent,
  89. };
  90. return hidinput_calc_abs_res(&field, ABS_X);
  91. }
  92. static void wacom_feature_mapping(struct hid_device *hdev,
  93. struct hid_field *field, struct hid_usage *usage)
  94. {
  95. struct wacom *wacom = hid_get_drvdata(hdev);
  96. struct wacom_features *features = &wacom->wacom_wac.features;
  97. struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
  98. u8 *data;
  99. int ret;
  100. switch (usage->hid) {
  101. case HID_DG_CONTACTMAX:
  102. /* leave touch_max as is if predefined */
  103. if (!features->touch_max) {
  104. /* read manually */
  105. data = kzalloc(2, GFP_KERNEL);
  106. if (!data)
  107. break;
  108. data[0] = field->report->id;
  109. ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
  110. data, 2, WAC_CMD_RETRIES);
  111. if (ret == 2) {
  112. features->touch_max = data[1];
  113. } else {
  114. features->touch_max = 16;
  115. hid_warn(hdev, "wacom_feature_mapping: "
  116. "could not get HID_DG_CONTACTMAX, "
  117. "defaulting to %d\n",
  118. features->touch_max);
  119. }
  120. kfree(data);
  121. }
  122. break;
  123. case HID_DG_INPUTMODE:
  124. /* Ignore if value index is out of bounds. */
  125. if (usage->usage_index >= field->report_count) {
  126. dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
  127. break;
  128. }
  129. hid_data->inputmode = field->report->id;
  130. hid_data->inputmode_index = usage->usage_index;
  131. break;
  132. case HID_UP_DIGITIZER:
  133. if (field->report->id == 0x0B &&
  134. (field->application == WACOM_G9_DIGITIZER ||
  135. field->application == WACOM_G11_DIGITIZER)) {
  136. wacom->wacom_wac.mode_report = field->report->id;
  137. wacom->wacom_wac.mode_value = 0;
  138. }
  139. break;
  140. case WACOM_G9_PAGE:
  141. case WACOM_G11_PAGE:
  142. if (field->report->id == 0x03 &&
  143. (field->application == WACOM_G9_TOUCHSCREEN ||
  144. field->application == WACOM_G11_TOUCHSCREEN)) {
  145. wacom->wacom_wac.mode_report = field->report->id;
  146. wacom->wacom_wac.mode_value = 0;
  147. }
  148. break;
  149. }
  150. }
  151. /*
  152. * Interface Descriptor of wacom devices can be incomplete and
  153. * inconsistent so wacom_features table is used to store stylus
  154. * device's packet lengths, various maximum values, and tablet
  155. * resolution based on product ID's.
  156. *
  157. * For devices that contain 2 interfaces, wacom_features table is
  158. * inaccurate for the touch interface. Since the Interface Descriptor
  159. * for touch interfaces has pretty complete data, this function exists
  160. * to query tablet for this missing information instead of hard coding in
  161. * an additional table.
  162. *
  163. * A typical Interface Descriptor for a stylus will contain a
  164. * boot mouse application collection that is not of interest and this
  165. * function will ignore it.
  166. *
  167. * It also contains a digitizer application collection that also is not
  168. * of interest since any information it contains would be duplicate
  169. * of what is in wacom_features. Usually it defines a report of an array
  170. * of bytes that could be used as max length of the stylus packet returned.
  171. * If it happens to define a Digitizer-Stylus Physical Collection then
  172. * the X and Y logical values contain valid data but it is ignored.
  173. *
  174. * A typical Interface Descriptor for a touch interface will contain a
  175. * Digitizer-Finger Physical Collection which will define both logical
  176. * X/Y maximum as well as the physical size of tablet. Since touch
  177. * interfaces haven't supported pressure or distance, this is enough
  178. * information to override invalid values in the wacom_features table.
  179. *
  180. * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
  181. * data. We deal with them after returning from this function.
  182. */
  183. static void wacom_usage_mapping(struct hid_device *hdev,
  184. struct hid_field *field, struct hid_usage *usage)
  185. {
  186. struct wacom *wacom = hid_get_drvdata(hdev);
  187. struct wacom_features *features = &wacom->wacom_wac.features;
  188. bool finger = WACOM_FINGER_FIELD(field);
  189. bool pen = WACOM_PEN_FIELD(field);
  190. /*
  191. * Requiring Stylus Usage will ignore boot mouse
  192. * X/Y values and some cases of invalid Digitizer X/Y
  193. * values commonly reported.
  194. */
  195. if (pen)
  196. features->device_type |= WACOM_DEVICETYPE_PEN;
  197. else if (finger)
  198. features->device_type |= WACOM_DEVICETYPE_TOUCH;
  199. else
  200. return;
  201. /*
  202. * Bamboo models do not support HID_DG_CONTACTMAX.
  203. * And, Bamboo Pen only descriptor contains touch.
  204. */
  205. if (features->type > BAMBOO_PT) {
  206. /* ISDv4 touch devices at least supports one touch point */
  207. if (finger && !features->touch_max)
  208. features->touch_max = 1;
  209. }
  210. switch (usage->hid) {
  211. case HID_GD_X:
  212. features->x_max = field->logical_maximum;
  213. if (finger) {
  214. features->x_phy = field->physical_maximum;
  215. if ((features->type != BAMBOO_PT) &&
  216. (features->type != BAMBOO_TOUCH)) {
  217. features->unit = field->unit;
  218. features->unitExpo = field->unit_exponent;
  219. }
  220. }
  221. break;
  222. case HID_GD_Y:
  223. features->y_max = field->logical_maximum;
  224. if (finger) {
  225. features->y_phy = field->physical_maximum;
  226. if ((features->type != BAMBOO_PT) &&
  227. (features->type != BAMBOO_TOUCH)) {
  228. features->unit = field->unit;
  229. features->unitExpo = field->unit_exponent;
  230. }
  231. }
  232. break;
  233. case HID_DG_TIPPRESSURE:
  234. if (pen)
  235. features->pressure_max = field->logical_maximum;
  236. break;
  237. }
  238. if (features->type == HID_GENERIC)
  239. wacom_wac_usage_mapping(hdev, field, usage);
  240. }
  241. static void wacom_post_parse_hid(struct hid_device *hdev,
  242. struct wacom_features *features)
  243. {
  244. struct wacom *wacom = hid_get_drvdata(hdev);
  245. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  246. if (features->type == HID_GENERIC) {
  247. /* Any last-minute generic device setup */
  248. if (features->touch_max > 1) {
  249. input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
  250. INPUT_MT_DIRECT);
  251. }
  252. }
  253. }
  254. static void wacom_parse_hid(struct hid_device *hdev,
  255. struct wacom_features *features)
  256. {
  257. struct hid_report_enum *rep_enum;
  258. struct hid_report *hreport;
  259. int i, j;
  260. /* check features first */
  261. rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
  262. list_for_each_entry(hreport, &rep_enum->report_list, list) {
  263. for (i = 0; i < hreport->maxfield; i++) {
  264. /* Ignore if report count is out of bounds. */
  265. if (hreport->field[i]->report_count < 1)
  266. continue;
  267. for (j = 0; j < hreport->field[i]->maxusage; j++) {
  268. wacom_feature_mapping(hdev, hreport->field[i],
  269. hreport->field[i]->usage + j);
  270. }
  271. }
  272. }
  273. /* now check the input usages */
  274. rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
  275. list_for_each_entry(hreport, &rep_enum->report_list, list) {
  276. if (!hreport->maxfield)
  277. continue;
  278. for (i = 0; i < hreport->maxfield; i++)
  279. for (j = 0; j < hreport->field[i]->maxusage; j++)
  280. wacom_usage_mapping(hdev, hreport->field[i],
  281. hreport->field[i]->usage + j);
  282. }
  283. wacom_post_parse_hid(hdev, features);
  284. }
  285. static int wacom_hid_set_device_mode(struct hid_device *hdev)
  286. {
  287. struct wacom *wacom = hid_get_drvdata(hdev);
  288. struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
  289. struct hid_report *r;
  290. struct hid_report_enum *re;
  291. if (hid_data->inputmode < 0)
  292. return 0;
  293. re = &(hdev->report_enum[HID_FEATURE_REPORT]);
  294. r = re->report_id_hash[hid_data->inputmode];
  295. if (r) {
  296. r->field[0]->value[hid_data->inputmode_index] = 2;
  297. hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
  298. }
  299. return 0;
  300. }
  301. static int wacom_set_device_mode(struct hid_device *hdev,
  302. struct wacom_wac *wacom_wac)
  303. {
  304. u8 *rep_data;
  305. struct hid_report *r;
  306. struct hid_report_enum *re;
  307. int length;
  308. int error = -ENOMEM, limit = 0;
  309. if (wacom_wac->mode_report < 0)
  310. return 0;
  311. re = &(hdev->report_enum[HID_FEATURE_REPORT]);
  312. r = re->report_id_hash[wacom_wac->mode_report];
  313. if (!r)
  314. return -EINVAL;
  315. rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
  316. if (!rep_data)
  317. return -ENOMEM;
  318. length = hid_report_len(r);
  319. do {
  320. rep_data[0] = wacom_wac->mode_report;
  321. rep_data[1] = wacom_wac->mode_value;
  322. error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
  323. length, 1);
  324. if (error >= 0)
  325. error = wacom_get_report(hdev, HID_FEATURE_REPORT,
  326. rep_data, length, 1);
  327. } while (error >= 0 &&
  328. rep_data[1] != wacom_wac->mode_report &&
  329. limit++ < WAC_MSG_RETRIES);
  330. kfree(rep_data);
  331. return error < 0 ? error : 0;
  332. }
  333. static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
  334. struct wacom_features *features)
  335. {
  336. struct wacom *wacom = hid_get_drvdata(hdev);
  337. int ret;
  338. u8 rep_data[2];
  339. switch (features->type) {
  340. case GRAPHIRE_BT:
  341. rep_data[0] = 0x03;
  342. rep_data[1] = 0x00;
  343. ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
  344. 3);
  345. if (ret >= 0) {
  346. rep_data[0] = speed == 0 ? 0x05 : 0x06;
  347. rep_data[1] = 0x00;
  348. ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
  349. rep_data, 2, 3);
  350. if (ret >= 0) {
  351. wacom->wacom_wac.bt_high_speed = speed;
  352. return 0;
  353. }
  354. }
  355. /*
  356. * Note that if the raw queries fail, it's not a hard failure
  357. * and it is safe to continue
  358. */
  359. hid_warn(hdev, "failed to poke device, command %d, err %d\n",
  360. rep_data[0], ret);
  361. break;
  362. case INTUOS4WL:
  363. if (speed == 1)
  364. wacom->wacom_wac.bt_features &= ~0x20;
  365. else
  366. wacom->wacom_wac.bt_features |= 0x20;
  367. rep_data[0] = 0x03;
  368. rep_data[1] = wacom->wacom_wac.bt_features;
  369. ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
  370. 1);
  371. if (ret >= 0)
  372. wacom->wacom_wac.bt_high_speed = speed;
  373. break;
  374. }
  375. return 0;
  376. }
  377. /*
  378. * Switch the tablet into its most-capable mode. Wacom tablets are
  379. * typically configured to power-up in a mode which sends mouse-like
  380. * reports to the OS. To get absolute position, pressure data, etc.
  381. * from the tablet, it is necessary to switch the tablet out of this
  382. * mode and into one which sends the full range of tablet data.
  383. */
  384. static int wacom_query_tablet_data(struct hid_device *hdev,
  385. struct wacom_features *features)
  386. {
  387. struct wacom *wacom = hid_get_drvdata(hdev);
  388. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  389. if (hdev->bus == BUS_BLUETOOTH)
  390. return wacom_bt_query_tablet_data(hdev, 1, features);
  391. if (features->type != HID_GENERIC) {
  392. if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
  393. if (features->type > TABLETPC) {
  394. /* MT Tablet PC touch */
  395. wacom_wac->mode_report = 3;
  396. wacom_wac->mode_value = 4;
  397. } else if (features->type == WACOM_24HDT) {
  398. wacom_wac->mode_report = 18;
  399. wacom_wac->mode_value = 2;
  400. } else if (features->type == WACOM_27QHDT) {
  401. wacom_wac->mode_report = 131;
  402. wacom_wac->mode_value = 2;
  403. } else if (features->type == BAMBOO_PAD) {
  404. wacom_wac->mode_report = 2;
  405. wacom_wac->mode_value = 2;
  406. }
  407. } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
  408. if (features->type <= BAMBOO_PT) {
  409. wacom_wac->mode_report = 2;
  410. wacom_wac->mode_value = 2;
  411. }
  412. }
  413. }
  414. wacom_set_device_mode(hdev, wacom_wac);
  415. if (features->type == HID_GENERIC)
  416. return wacom_hid_set_device_mode(hdev);
  417. return 0;
  418. }
  419. static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
  420. struct wacom_features *features)
  421. {
  422. struct wacom *wacom = hid_get_drvdata(hdev);
  423. struct usb_interface *intf = wacom->intf;
  424. /* default features */
  425. features->x_fuzz = 4;
  426. features->y_fuzz = 4;
  427. features->pressure_fuzz = 0;
  428. features->distance_fuzz = 1;
  429. features->tilt_fuzz = 1;
  430. /*
  431. * The wireless device HID is basic and layout conflicts with
  432. * other tablets (monitor and touch interface can look like pen).
  433. * Skip the query for this type and modify defaults based on
  434. * interface number.
  435. */
  436. if (features->type == WIRELESS) {
  437. if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
  438. features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
  439. else
  440. features->device_type = WACOM_DEVICETYPE_NONE;
  441. return;
  442. }
  443. wacom_parse_hid(hdev, features);
  444. }
  445. struct wacom_hdev_data {
  446. struct list_head list;
  447. struct kref kref;
  448. struct hid_device *dev;
  449. struct wacom_shared shared;
  450. };
  451. static LIST_HEAD(wacom_udev_list);
  452. static DEFINE_MUTEX(wacom_udev_list_lock);
  453. static bool compare_device_paths(struct hid_device *hdev_a,
  454. struct hid_device *hdev_b, char separator)
  455. {
  456. int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
  457. int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
  458. if (n1 != n2 || n1 <= 0 || n2 <= 0)
  459. return false;
  460. return !strncmp(hdev_a->phys, hdev_b->phys, n1);
  461. }
  462. static bool wacom_are_sibling(struct hid_device *hdev,
  463. struct hid_device *sibling)
  464. {
  465. struct wacom *wacom = hid_get_drvdata(hdev);
  466. struct wacom_features *features = &wacom->wacom_wac.features;
  467. struct wacom *sibling_wacom = hid_get_drvdata(sibling);
  468. struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
  469. __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
  470. __u32 oPid = features->oPid ? features->oPid : hdev->product;
  471. /* The defined oVid/oPid must match that of the sibling */
  472. if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
  473. return false;
  474. if (features->oPid != HID_ANY_ID && sibling->product != oPid)
  475. return false;
  476. /*
  477. * Devices with the same VID/PID must share the same physical
  478. * device path, while those with different VID/PID must share
  479. * the same physical parent device path.
  480. */
  481. if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
  482. if (!compare_device_paths(hdev, sibling, '/'))
  483. return false;
  484. } else {
  485. if (!compare_device_paths(hdev, sibling, '.'))
  486. return false;
  487. }
  488. /* Skip the remaining heuristics unless you are a HID_GENERIC device */
  489. if (features->type != HID_GENERIC)
  490. return true;
  491. /*
  492. * Direct-input devices may not be siblings of indirect-input
  493. * devices.
  494. */
  495. if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
  496. !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
  497. return false;
  498. /*
  499. * Indirect-input devices may not be siblings of direct-input
  500. * devices.
  501. */
  502. if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
  503. (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
  504. return false;
  505. /* Pen devices may only be siblings of touch devices */
  506. if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
  507. !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
  508. return false;
  509. /* Touch devices may only be siblings of pen devices */
  510. if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
  511. !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
  512. return false;
  513. /*
  514. * No reason could be found for these two devices to NOT be
  515. * siblings, so there's a good chance they ARE siblings
  516. */
  517. return true;
  518. }
  519. static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
  520. {
  521. struct wacom_hdev_data *data;
  522. /* Try to find an already-probed interface from the same device */
  523. list_for_each_entry(data, &wacom_udev_list, list) {
  524. if (compare_device_paths(hdev, data->dev, '/')) {
  525. kref_get(&data->kref);
  526. return data;
  527. }
  528. }
  529. /* Fallback to finding devices that appear to be "siblings" */
  530. list_for_each_entry(data, &wacom_udev_list, list) {
  531. if (wacom_are_sibling(hdev, data->dev)) {
  532. kref_get(&data->kref);
  533. return data;
  534. }
  535. }
  536. return NULL;
  537. }
  538. static void wacom_release_shared_data(struct kref *kref)
  539. {
  540. struct wacom_hdev_data *data =
  541. container_of(kref, struct wacom_hdev_data, kref);
  542. mutex_lock(&wacom_udev_list_lock);
  543. list_del(&data->list);
  544. mutex_unlock(&wacom_udev_list_lock);
  545. kfree(data);
  546. }
  547. static void wacom_remove_shared_data(void *res)
  548. {
  549. struct wacom *wacom = res;
  550. struct wacom_hdev_data *data;
  551. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  552. if (wacom_wac->shared) {
  553. data = container_of(wacom_wac->shared, struct wacom_hdev_data,
  554. shared);
  555. if (wacom_wac->shared->touch == wacom->hdev)
  556. wacom_wac->shared->touch = NULL;
  557. else if (wacom_wac->shared->pen == wacom->hdev)
  558. wacom_wac->shared->pen = NULL;
  559. kref_put(&data->kref, wacom_release_shared_data);
  560. wacom_wac->shared = NULL;
  561. }
  562. }
  563. static int wacom_add_shared_data(struct hid_device *hdev)
  564. {
  565. struct wacom *wacom = hid_get_drvdata(hdev);
  566. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  567. struct wacom_hdev_data *data;
  568. int retval = 0;
  569. mutex_lock(&wacom_udev_list_lock);
  570. data = wacom_get_hdev_data(hdev);
  571. if (!data) {
  572. data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
  573. if (!data) {
  574. retval = -ENOMEM;
  575. goto out;
  576. }
  577. kref_init(&data->kref);
  578. data->dev = hdev;
  579. list_add_tail(&data->list, &wacom_udev_list);
  580. }
  581. wacom_wac->shared = &data->shared;
  582. retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
  583. if (retval) {
  584. mutex_unlock(&wacom_udev_list_lock);
  585. wacom_remove_shared_data(wacom);
  586. return retval;
  587. }
  588. if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
  589. wacom_wac->shared->touch = hdev;
  590. else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
  591. wacom_wac->shared->pen = hdev;
  592. out:
  593. mutex_unlock(&wacom_udev_list_lock);
  594. return retval;
  595. }
  596. static int wacom_led_control(struct wacom *wacom)
  597. {
  598. unsigned char *buf;
  599. int retval;
  600. unsigned char report_id = WAC_CMD_LED_CONTROL;
  601. int buf_size = 9;
  602. if (!hid_get_drvdata(wacom->hdev))
  603. return -ENODEV;
  604. if (!wacom->led.groups)
  605. return -ENOTSUPP;
  606. if (wacom->wacom_wac.features.type == REMOTE)
  607. return -ENOTSUPP;
  608. if (wacom->wacom_wac.pid) { /* wireless connected */
  609. report_id = WAC_CMD_WL_LED_CONTROL;
  610. buf_size = 13;
  611. }
  612. buf = kzalloc(buf_size, GFP_KERNEL);
  613. if (!buf)
  614. return -ENOMEM;
  615. if (wacom->wacom_wac.features.type >= INTUOS5S &&
  616. wacom->wacom_wac.features.type <= INTUOSPL) {
  617. /*
  618. * Touch Ring and crop mark LED luminance may take on
  619. * one of four values:
  620. * 0 = Low; 1 = Medium; 2 = High; 3 = Off
  621. */
  622. int ring_led = wacom->led.groups[0].select & 0x03;
  623. int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
  624. int crop_lum = 0;
  625. unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
  626. buf[0] = report_id;
  627. if (wacom->wacom_wac.pid) {
  628. wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
  629. buf, buf_size, WAC_CMD_RETRIES);
  630. buf[0] = report_id;
  631. buf[4] = led_bits;
  632. } else
  633. buf[1] = led_bits;
  634. }
  635. else {
  636. int led = wacom->led.groups[0].select | 0x4;
  637. if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
  638. wacom->wacom_wac.features.type == WACOM_24HD)
  639. led |= (wacom->led.groups[1].select << 4) | 0x40;
  640. buf[0] = report_id;
  641. buf[1] = led;
  642. buf[2] = wacom->led.llv;
  643. buf[3] = wacom->led.hlv;
  644. buf[4] = wacom->led.img_lum;
  645. }
  646. retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
  647. WAC_CMD_RETRIES);
  648. kfree(buf);
  649. return retval;
  650. }
  651. static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
  652. const unsigned len, const void *img)
  653. {
  654. unsigned char *buf;
  655. int i, retval;
  656. const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
  657. buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
  658. if (!buf)
  659. return -ENOMEM;
  660. /* Send 'start' command */
  661. buf[0] = WAC_CMD_ICON_START;
  662. buf[1] = 1;
  663. retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
  664. WAC_CMD_RETRIES);
  665. if (retval < 0)
  666. goto out;
  667. buf[0] = xfer_id;
  668. buf[1] = button_id & 0x07;
  669. for (i = 0; i < 4; i++) {
  670. buf[2] = i;
  671. memcpy(buf + 3, img + i * chunk_len, chunk_len);
  672. retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
  673. buf, chunk_len + 3, WAC_CMD_RETRIES);
  674. if (retval < 0)
  675. break;
  676. }
  677. /* Send 'stop' */
  678. buf[0] = WAC_CMD_ICON_START;
  679. buf[1] = 0;
  680. wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
  681. WAC_CMD_RETRIES);
  682. out:
  683. kfree(buf);
  684. return retval;
  685. }
  686. static ssize_t wacom_led_select_store(struct device *dev, int set_id,
  687. const char *buf, size_t count)
  688. {
  689. struct hid_device *hdev = to_hid_device(dev);
  690. struct wacom *wacom = hid_get_drvdata(hdev);
  691. unsigned int id;
  692. int err;
  693. err = kstrtouint(buf, 10, &id);
  694. if (err)
  695. return err;
  696. mutex_lock(&wacom->lock);
  697. wacom->led.groups[set_id].select = id & 0x3;
  698. err = wacom_led_control(wacom);
  699. mutex_unlock(&wacom->lock);
  700. return err < 0 ? err : count;
  701. }
  702. #define DEVICE_LED_SELECT_ATTR(SET_ID) \
  703. static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
  704. struct device_attribute *attr, const char *buf, size_t count) \
  705. { \
  706. return wacom_led_select_store(dev, SET_ID, buf, count); \
  707. } \
  708. static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
  709. struct device_attribute *attr, char *buf) \
  710. { \
  711. struct hid_device *hdev = to_hid_device(dev);\
  712. struct wacom *wacom = hid_get_drvdata(hdev); \
  713. return scnprintf(buf, PAGE_SIZE, "%d\n", \
  714. wacom->led.groups[SET_ID].select); \
  715. } \
  716. static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
  717. wacom_led##SET_ID##_select_show, \
  718. wacom_led##SET_ID##_select_store)
  719. DEVICE_LED_SELECT_ATTR(0);
  720. DEVICE_LED_SELECT_ATTR(1);
  721. static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
  722. const char *buf, size_t count)
  723. {
  724. unsigned int value;
  725. int err;
  726. err = kstrtouint(buf, 10, &value);
  727. if (err)
  728. return err;
  729. mutex_lock(&wacom->lock);
  730. *dest = value & 0x7f;
  731. err = wacom_led_control(wacom);
  732. mutex_unlock(&wacom->lock);
  733. return err < 0 ? err : count;
  734. }
  735. #define DEVICE_LUMINANCE_ATTR(name, field) \
  736. static ssize_t wacom_##name##_luminance_store(struct device *dev, \
  737. struct device_attribute *attr, const char *buf, size_t count) \
  738. { \
  739. struct hid_device *hdev = to_hid_device(dev);\
  740. struct wacom *wacom = hid_get_drvdata(hdev); \
  741. \
  742. return wacom_luminance_store(wacom, &wacom->led.field, \
  743. buf, count); \
  744. } \
  745. static ssize_t wacom_##name##_luminance_show(struct device *dev, \
  746. struct device_attribute *attr, char *buf) \
  747. { \
  748. struct wacom *wacom = dev_get_drvdata(dev); \
  749. return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
  750. } \
  751. static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
  752. wacom_##name##_luminance_show, \
  753. wacom_##name##_luminance_store)
  754. DEVICE_LUMINANCE_ATTR(status0, llv);
  755. DEVICE_LUMINANCE_ATTR(status1, hlv);
  756. DEVICE_LUMINANCE_ATTR(buttons, img_lum);
  757. static ssize_t wacom_button_image_store(struct device *dev, int button_id,
  758. const char *buf, size_t count)
  759. {
  760. struct hid_device *hdev = to_hid_device(dev);
  761. struct wacom *wacom = hid_get_drvdata(hdev);
  762. int err;
  763. unsigned len;
  764. u8 xfer_id;
  765. if (hdev->bus == BUS_BLUETOOTH) {
  766. len = 256;
  767. xfer_id = WAC_CMD_ICON_BT_XFER;
  768. } else {
  769. len = 1024;
  770. xfer_id = WAC_CMD_ICON_XFER;
  771. }
  772. if (count != len)
  773. return -EINVAL;
  774. mutex_lock(&wacom->lock);
  775. err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
  776. mutex_unlock(&wacom->lock);
  777. return err < 0 ? err : count;
  778. }
  779. #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
  780. static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
  781. struct device_attribute *attr, const char *buf, size_t count) \
  782. { \
  783. return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
  784. } \
  785. static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
  786. NULL, wacom_btnimg##BUTTON_ID##_store)
  787. DEVICE_BTNIMG_ATTR(0);
  788. DEVICE_BTNIMG_ATTR(1);
  789. DEVICE_BTNIMG_ATTR(2);
  790. DEVICE_BTNIMG_ATTR(3);
  791. DEVICE_BTNIMG_ATTR(4);
  792. DEVICE_BTNIMG_ATTR(5);
  793. DEVICE_BTNIMG_ATTR(6);
  794. DEVICE_BTNIMG_ATTR(7);
  795. static struct attribute *cintiq_led_attrs[] = {
  796. &dev_attr_status_led0_select.attr,
  797. &dev_attr_status_led1_select.attr,
  798. NULL
  799. };
  800. static struct attribute_group cintiq_led_attr_group = {
  801. .name = "wacom_led",
  802. .attrs = cintiq_led_attrs,
  803. };
  804. static struct attribute *intuos4_led_attrs[] = {
  805. &dev_attr_status0_luminance.attr,
  806. &dev_attr_status1_luminance.attr,
  807. &dev_attr_status_led0_select.attr,
  808. &dev_attr_buttons_luminance.attr,
  809. &dev_attr_button0_rawimg.attr,
  810. &dev_attr_button1_rawimg.attr,
  811. &dev_attr_button2_rawimg.attr,
  812. &dev_attr_button3_rawimg.attr,
  813. &dev_attr_button4_rawimg.attr,
  814. &dev_attr_button5_rawimg.attr,
  815. &dev_attr_button6_rawimg.attr,
  816. &dev_attr_button7_rawimg.attr,
  817. NULL
  818. };
  819. static struct attribute_group intuos4_led_attr_group = {
  820. .name = "wacom_led",
  821. .attrs = intuos4_led_attrs,
  822. };
  823. static struct attribute *intuos5_led_attrs[] = {
  824. &dev_attr_status0_luminance.attr,
  825. &dev_attr_status_led0_select.attr,
  826. NULL
  827. };
  828. static struct attribute_group intuos5_led_attr_group = {
  829. .name = "wacom_led",
  830. .attrs = intuos5_led_attrs,
  831. };
  832. struct wacom_sysfs_group_devres {
  833. struct attribute_group *group;
  834. struct kobject *root;
  835. };
  836. static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
  837. {
  838. struct wacom_sysfs_group_devres *devres = res;
  839. struct kobject *kobj = devres->root;
  840. dev_dbg(dev, "%s: dropping reference to %s\n",
  841. __func__, devres->group->name);
  842. sysfs_remove_group(kobj, devres->group);
  843. }
  844. static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
  845. struct kobject *root,
  846. struct attribute_group *group)
  847. {
  848. struct wacom_sysfs_group_devres *devres;
  849. int error;
  850. devres = devres_alloc(wacom_devm_sysfs_group_release,
  851. sizeof(struct wacom_sysfs_group_devres),
  852. GFP_KERNEL);
  853. if (!devres)
  854. return -ENOMEM;
  855. devres->group = group;
  856. devres->root = root;
  857. error = sysfs_create_group(devres->root, group);
  858. if (error)
  859. return error;
  860. devres_add(&wacom->hdev->dev, devres);
  861. return 0;
  862. }
  863. static int wacom_devm_sysfs_create_group(struct wacom *wacom,
  864. struct attribute_group *group)
  865. {
  866. return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
  867. group);
  868. }
  869. enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
  870. {
  871. struct wacom *wacom = led->wacom;
  872. if (wacom->led.max_hlv)
  873. return led->hlv * LED_FULL / wacom->led.max_hlv;
  874. if (wacom->led.max_llv)
  875. return led->llv * LED_FULL / wacom->led.max_llv;
  876. /* device doesn't support brightness tuning */
  877. return LED_FULL;
  878. }
  879. static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
  880. {
  881. struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
  882. struct wacom *wacom = led->wacom;
  883. if (wacom->led.groups[led->group].select != led->id)
  884. return LED_OFF;
  885. return wacom_leds_brightness_get(led);
  886. }
  887. static int wacom_led_brightness_set(struct led_classdev *cdev,
  888. enum led_brightness brightness)
  889. {
  890. struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
  891. struct wacom *wacom = led->wacom;
  892. int error;
  893. mutex_lock(&wacom->lock);
  894. if (!wacom->led.groups || (brightness == LED_OFF &&
  895. wacom->led.groups[led->group].select != led->id)) {
  896. error = 0;
  897. goto out;
  898. }
  899. led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
  900. led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
  901. wacom->led.groups[led->group].select = led->id;
  902. error = wacom_led_control(wacom);
  903. out:
  904. mutex_unlock(&wacom->lock);
  905. return error;
  906. }
  907. static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
  908. enum led_brightness brightness)
  909. {
  910. }
  911. static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
  912. struct wacom_led *led, unsigned int group,
  913. unsigned int id, bool read_only)
  914. {
  915. int error;
  916. char *name;
  917. name = devm_kasprintf(dev, GFP_KERNEL,
  918. "%s::wacom-%d.%d",
  919. dev_name(dev),
  920. group,
  921. id);
  922. if (!name)
  923. return -ENOMEM;
  924. if (!read_only) {
  925. led->trigger.name = name;
  926. error = devm_led_trigger_register(dev, &led->trigger);
  927. if (error) {
  928. hid_err(wacom->hdev,
  929. "failed to register LED trigger %s: %d\n",
  930. led->cdev.name, error);
  931. return error;
  932. }
  933. }
  934. led->group = group;
  935. led->id = id;
  936. led->wacom = wacom;
  937. led->llv = wacom->led.llv;
  938. led->hlv = wacom->led.hlv;
  939. led->cdev.name = name;
  940. led->cdev.max_brightness = LED_FULL;
  941. led->cdev.flags = LED_HW_PLUGGABLE;
  942. led->cdev.brightness_get = __wacom_led_brightness_get;
  943. if (!read_only) {
  944. led->cdev.brightness_set_blocking = wacom_led_brightness_set;
  945. led->cdev.default_trigger = led->cdev.name;
  946. } else {
  947. led->cdev.brightness_set = wacom_led_readonly_brightness_set;
  948. }
  949. error = devm_led_classdev_register(dev, &led->cdev);
  950. if (error) {
  951. hid_err(wacom->hdev,
  952. "failed to register LED %s: %d\n",
  953. led->cdev.name, error);
  954. led->cdev.name = NULL;
  955. return error;
  956. }
  957. return 0;
  958. }
  959. static void wacom_led_groups_release_one(void *data)
  960. {
  961. struct wacom_group_leds *group = data;
  962. devres_release_group(group->dev, group);
  963. }
  964. static int wacom_led_groups_alloc_and_register_one(struct device *dev,
  965. struct wacom *wacom,
  966. int group_id, int count,
  967. bool read_only)
  968. {
  969. struct wacom_led *leds;
  970. int i, error;
  971. if (group_id >= wacom->led.count || count <= 0)
  972. return -EINVAL;
  973. if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
  974. return -ENOMEM;
  975. leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
  976. if (!leds) {
  977. error = -ENOMEM;
  978. goto err;
  979. }
  980. wacom->led.groups[group_id].leds = leds;
  981. wacom->led.groups[group_id].count = count;
  982. for (i = 0; i < count; i++) {
  983. error = wacom_led_register_one(dev, wacom, &leds[i],
  984. group_id, i, read_only);
  985. if (error)
  986. goto err;
  987. }
  988. wacom->led.groups[group_id].dev = dev;
  989. devres_close_group(dev, &wacom->led.groups[group_id]);
  990. /*
  991. * There is a bug (?) in devm_led_classdev_register() in which its
  992. * increments the refcount of the parent. If the parent is an input
  993. * device, that means the ref count never reaches 0 when
  994. * devm_input_device_release() gets called.
  995. * This means that the LEDs are still there after disconnect.
  996. * Manually force the release of the group so that the leds are released
  997. * once we are done using them.
  998. */
  999. error = devm_add_action_or_reset(&wacom->hdev->dev,
  1000. wacom_led_groups_release_one,
  1001. &wacom->led.groups[group_id]);
  1002. if (error)
  1003. return error;
  1004. return 0;
  1005. err:
  1006. devres_release_group(dev, &wacom->led.groups[group_id]);
  1007. return error;
  1008. }
  1009. struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
  1010. unsigned int id)
  1011. {
  1012. struct wacom_group_leds *group;
  1013. if (group_id >= wacom->led.count)
  1014. return NULL;
  1015. group = &wacom->led.groups[group_id];
  1016. if (!group->leds)
  1017. return NULL;
  1018. id %= group->count;
  1019. return &group->leds[id];
  1020. }
  1021. /**
  1022. * wacom_led_next: gives the next available led with a wacom trigger.
  1023. *
  1024. * returns the next available struct wacom_led which has its default trigger
  1025. * or the current one if none is available.
  1026. */
  1027. struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
  1028. {
  1029. struct wacom_led *next_led;
  1030. int group, next;
  1031. if (!wacom || !cur)
  1032. return NULL;
  1033. group = cur->group;
  1034. next = cur->id;
  1035. do {
  1036. next_led = wacom_led_find(wacom, group, ++next);
  1037. if (!next_led || next_led == cur)
  1038. return next_led;
  1039. } while (next_led->cdev.trigger != &next_led->trigger);
  1040. return next_led;
  1041. }
  1042. static void wacom_led_groups_release(void *data)
  1043. {
  1044. struct wacom *wacom = data;
  1045. wacom->led.groups = NULL;
  1046. wacom->led.count = 0;
  1047. }
  1048. static int wacom_led_groups_allocate(struct wacom *wacom, int count)
  1049. {
  1050. struct device *dev = &wacom->hdev->dev;
  1051. struct wacom_group_leds *groups;
  1052. int error;
  1053. groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
  1054. GFP_KERNEL);
  1055. if (!groups)
  1056. return -ENOMEM;
  1057. error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
  1058. if (error)
  1059. return error;
  1060. wacom->led.groups = groups;
  1061. wacom->led.count = count;
  1062. return 0;
  1063. }
  1064. static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
  1065. int led_per_group, bool read_only)
  1066. {
  1067. struct device *dev;
  1068. int i, error;
  1069. if (!wacom->wacom_wac.pad_input)
  1070. return -EINVAL;
  1071. dev = &wacom->wacom_wac.pad_input->dev;
  1072. error = wacom_led_groups_allocate(wacom, group_count);
  1073. if (error)
  1074. return error;
  1075. for (i = 0; i < group_count; i++) {
  1076. error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
  1077. led_per_group,
  1078. read_only);
  1079. if (error)
  1080. return error;
  1081. }
  1082. return 0;
  1083. }
  1084. static int wacom_initialize_leds(struct wacom *wacom)
  1085. {
  1086. int error;
  1087. if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
  1088. return 0;
  1089. /* Initialize default values */
  1090. switch (wacom->wacom_wac.features.type) {
  1091. case INTUOS4S:
  1092. case INTUOS4:
  1093. case INTUOS4WL:
  1094. case INTUOS4L:
  1095. wacom->led.llv = 10;
  1096. wacom->led.hlv = 20;
  1097. wacom->led.max_llv = 127;
  1098. wacom->led.max_hlv = 127;
  1099. wacom->led.img_lum = 10;
  1100. error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
  1101. if (error) {
  1102. hid_err(wacom->hdev,
  1103. "cannot create leds err: %d\n", error);
  1104. return error;
  1105. }
  1106. error = wacom_devm_sysfs_create_group(wacom,
  1107. &intuos4_led_attr_group);
  1108. break;
  1109. case WACOM_24HD:
  1110. case WACOM_21UX2:
  1111. wacom->led.llv = 0;
  1112. wacom->led.hlv = 0;
  1113. wacom->led.img_lum = 0;
  1114. error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
  1115. if (error) {
  1116. hid_err(wacom->hdev,
  1117. "cannot create leds err: %d\n", error);
  1118. return error;
  1119. }
  1120. error = wacom_devm_sysfs_create_group(wacom,
  1121. &cintiq_led_attr_group);
  1122. break;
  1123. case INTUOS5S:
  1124. case INTUOS5:
  1125. case INTUOS5L:
  1126. case INTUOSPS:
  1127. case INTUOSPM:
  1128. case INTUOSPL:
  1129. wacom->led.llv = 32;
  1130. wacom->led.max_llv = 96;
  1131. error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
  1132. if (error) {
  1133. hid_err(wacom->hdev,
  1134. "cannot create leds err: %d\n", error);
  1135. return error;
  1136. }
  1137. error = wacom_devm_sysfs_create_group(wacom,
  1138. &intuos5_led_attr_group);
  1139. break;
  1140. case REMOTE:
  1141. wacom->led.llv = 255;
  1142. wacom->led.max_llv = 255;
  1143. error = wacom_led_groups_allocate(wacom, 5);
  1144. if (error) {
  1145. hid_err(wacom->hdev,
  1146. "cannot create leds err: %d\n", error);
  1147. return error;
  1148. }
  1149. return 0;
  1150. default:
  1151. return 0;
  1152. }
  1153. if (error) {
  1154. hid_err(wacom->hdev,
  1155. "cannot create sysfs group err: %d\n", error);
  1156. return error;
  1157. }
  1158. wacom_led_control(wacom);
  1159. return 0;
  1160. }
  1161. static enum power_supply_property wacom_battery_props[] = {
  1162. POWER_SUPPLY_PROP_MODEL_NAME,
  1163. POWER_SUPPLY_PROP_PRESENT,
  1164. POWER_SUPPLY_PROP_STATUS,
  1165. POWER_SUPPLY_PROP_SCOPE,
  1166. POWER_SUPPLY_PROP_CAPACITY
  1167. };
  1168. static int wacom_battery_get_property(struct power_supply *psy,
  1169. enum power_supply_property psp,
  1170. union power_supply_propval *val)
  1171. {
  1172. struct wacom_battery *battery = power_supply_get_drvdata(psy);
  1173. int ret = 0;
  1174. switch (psp) {
  1175. case POWER_SUPPLY_PROP_MODEL_NAME:
  1176. val->strval = battery->wacom->wacom_wac.name;
  1177. break;
  1178. case POWER_SUPPLY_PROP_PRESENT:
  1179. val->intval = battery->bat_connected;
  1180. break;
  1181. case POWER_SUPPLY_PROP_SCOPE:
  1182. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  1183. break;
  1184. case POWER_SUPPLY_PROP_CAPACITY:
  1185. val->intval = battery->battery_capacity;
  1186. break;
  1187. case POWER_SUPPLY_PROP_STATUS:
  1188. if (battery->bat_charging)
  1189. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  1190. else if (battery->battery_capacity == 100 &&
  1191. battery->ps_connected)
  1192. val->intval = POWER_SUPPLY_STATUS_FULL;
  1193. else if (battery->ps_connected)
  1194. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  1195. else
  1196. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  1197. break;
  1198. default:
  1199. ret = -EINVAL;
  1200. break;
  1201. }
  1202. return ret;
  1203. }
  1204. static int __wacom_initialize_battery(struct wacom *wacom,
  1205. struct wacom_battery *battery)
  1206. {
  1207. static atomic_t battery_no = ATOMIC_INIT(0);
  1208. struct device *dev = &wacom->hdev->dev;
  1209. struct power_supply_config psy_cfg = { .drv_data = battery, };
  1210. struct power_supply *ps_bat;
  1211. struct power_supply_desc *bat_desc = &battery->bat_desc;
  1212. unsigned long n;
  1213. int error;
  1214. if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
  1215. return -ENOMEM;
  1216. battery->wacom = wacom;
  1217. n = atomic_inc_return(&battery_no) - 1;
  1218. bat_desc->properties = wacom_battery_props;
  1219. bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
  1220. bat_desc->get_property = wacom_battery_get_property;
  1221. sprintf(battery->bat_name, "wacom_battery_%ld", n);
  1222. bat_desc->name = battery->bat_name;
  1223. bat_desc->type = POWER_SUPPLY_TYPE_USB;
  1224. bat_desc->use_for_apm = 0;
  1225. ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
  1226. if (IS_ERR(ps_bat)) {
  1227. error = PTR_ERR(ps_bat);
  1228. goto err;
  1229. }
  1230. power_supply_powers(ps_bat, &wacom->hdev->dev);
  1231. battery->battery = ps_bat;
  1232. devres_close_group(dev, bat_desc);
  1233. return 0;
  1234. err:
  1235. devres_release_group(dev, bat_desc);
  1236. return error;
  1237. }
  1238. static int wacom_initialize_battery(struct wacom *wacom)
  1239. {
  1240. if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
  1241. return __wacom_initialize_battery(wacom, &wacom->battery);
  1242. return 0;
  1243. }
  1244. static void wacom_destroy_battery(struct wacom *wacom)
  1245. {
  1246. if (wacom->battery.battery) {
  1247. devres_release_group(&wacom->hdev->dev,
  1248. &wacom->battery.bat_desc);
  1249. wacom->battery.battery = NULL;
  1250. }
  1251. }
  1252. static ssize_t wacom_show_speed(struct device *dev,
  1253. struct device_attribute
  1254. *attr, char *buf)
  1255. {
  1256. struct hid_device *hdev = to_hid_device(dev);
  1257. struct wacom *wacom = hid_get_drvdata(hdev);
  1258. return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
  1259. }
  1260. static ssize_t wacom_store_speed(struct device *dev,
  1261. struct device_attribute *attr,
  1262. const char *buf, size_t count)
  1263. {
  1264. struct hid_device *hdev = to_hid_device(dev);
  1265. struct wacom *wacom = hid_get_drvdata(hdev);
  1266. u8 new_speed;
  1267. if (kstrtou8(buf, 0, &new_speed))
  1268. return -EINVAL;
  1269. if (new_speed != 0 && new_speed != 1)
  1270. return -EINVAL;
  1271. wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
  1272. return count;
  1273. }
  1274. static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
  1275. wacom_show_speed, wacom_store_speed);
  1276. static ssize_t wacom_show_remote_mode(struct kobject *kobj,
  1277. struct kobj_attribute *kattr,
  1278. char *buf, int index)
  1279. {
  1280. struct device *dev = kobj_to_dev(kobj->parent);
  1281. struct hid_device *hdev = to_hid_device(dev);
  1282. struct wacom *wacom = hid_get_drvdata(hdev);
  1283. u8 mode;
  1284. mode = wacom->led.groups[index].select;
  1285. if (mode >= 0 && mode < 3)
  1286. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  1287. else
  1288. return snprintf(buf, PAGE_SIZE, "%d\n", -1);
  1289. }
  1290. #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
  1291. static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
  1292. struct kobj_attribute *kattr, char *buf) \
  1293. { \
  1294. return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
  1295. } \
  1296. static struct kobj_attribute remote##SET_ID##_mode_attr = { \
  1297. .attr = {.name = "remote_mode", \
  1298. .mode = DEV_ATTR_RO_PERM}, \
  1299. .show = wacom_show_remote##SET_ID##_mode, \
  1300. }; \
  1301. static struct attribute *remote##SET_ID##_serial_attrs[] = { \
  1302. &remote##SET_ID##_mode_attr.attr, \
  1303. NULL \
  1304. }; \
  1305. static struct attribute_group remote##SET_ID##_serial_group = { \
  1306. .name = NULL, \
  1307. .attrs = remote##SET_ID##_serial_attrs, \
  1308. }
  1309. DEVICE_EKR_ATTR_GROUP(0);
  1310. DEVICE_EKR_ATTR_GROUP(1);
  1311. DEVICE_EKR_ATTR_GROUP(2);
  1312. DEVICE_EKR_ATTR_GROUP(3);
  1313. DEVICE_EKR_ATTR_GROUP(4);
  1314. static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
  1315. int index)
  1316. {
  1317. int error = 0;
  1318. struct wacom_remote *remote = wacom->remote;
  1319. remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
  1320. GFP_KERNEL,
  1321. "%d", serial);
  1322. if (!remote->remotes[index].group.name)
  1323. return -ENOMEM;
  1324. error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
  1325. &remote->remotes[index].group);
  1326. if (error) {
  1327. remote->remotes[index].group.name = NULL;
  1328. hid_err(wacom->hdev,
  1329. "cannot create sysfs group err: %d\n", error);
  1330. return error;
  1331. }
  1332. return 0;
  1333. }
  1334. static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
  1335. {
  1336. const size_t buf_size = 2;
  1337. unsigned char *buf;
  1338. int retval;
  1339. buf = kzalloc(buf_size, GFP_KERNEL);
  1340. if (!buf)
  1341. return -ENOMEM;
  1342. buf[0] = WAC_CMD_DELETE_PAIRING;
  1343. buf[1] = selector;
  1344. retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
  1345. buf_size, WAC_CMD_RETRIES);
  1346. kfree(buf);
  1347. return retval;
  1348. }
  1349. static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
  1350. struct kobj_attribute *attr,
  1351. const char *buf, size_t count)
  1352. {
  1353. unsigned char selector = 0;
  1354. struct device *dev = kobj_to_dev(kobj->parent);
  1355. struct hid_device *hdev = to_hid_device(dev);
  1356. struct wacom *wacom = hid_get_drvdata(hdev);
  1357. int err;
  1358. if (!strncmp(buf, "*\n", 2)) {
  1359. selector = WAC_CMD_UNPAIR_ALL;
  1360. } else {
  1361. hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
  1362. buf);
  1363. return -1;
  1364. }
  1365. mutex_lock(&wacom->lock);
  1366. err = wacom_cmd_unpair_remote(wacom, selector);
  1367. mutex_unlock(&wacom->lock);
  1368. return err < 0 ? err : count;
  1369. }
  1370. static struct kobj_attribute unpair_remote_attr = {
  1371. .attr = {.name = "unpair_remote", .mode = 0200},
  1372. .store = wacom_store_unpair_remote,
  1373. };
  1374. static const struct attribute *remote_unpair_attrs[] = {
  1375. &unpair_remote_attr.attr,
  1376. NULL
  1377. };
  1378. static void wacom_remotes_destroy(void *data)
  1379. {
  1380. struct wacom *wacom = data;
  1381. struct wacom_remote *remote = wacom->remote;
  1382. if (!remote)
  1383. return;
  1384. kobject_put(remote->remote_dir);
  1385. kfifo_free(&remote->remote_fifo);
  1386. wacom->remote = NULL;
  1387. }
  1388. static int wacom_initialize_remotes(struct wacom *wacom)
  1389. {
  1390. int error = 0;
  1391. struct wacom_remote *remote;
  1392. int i;
  1393. if (wacom->wacom_wac.features.type != REMOTE)
  1394. return 0;
  1395. remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
  1396. GFP_KERNEL);
  1397. if (!remote)
  1398. return -ENOMEM;
  1399. wacom->remote = remote;
  1400. spin_lock_init(&remote->remote_lock);
  1401. error = kfifo_alloc(&remote->remote_fifo,
  1402. 5 * sizeof(struct wacom_remote_data),
  1403. GFP_KERNEL);
  1404. if (error) {
  1405. hid_err(wacom->hdev, "failed allocating remote_fifo\n");
  1406. return -ENOMEM;
  1407. }
  1408. remote->remotes[0].group = remote0_serial_group;
  1409. remote->remotes[1].group = remote1_serial_group;
  1410. remote->remotes[2].group = remote2_serial_group;
  1411. remote->remotes[3].group = remote3_serial_group;
  1412. remote->remotes[4].group = remote4_serial_group;
  1413. remote->remote_dir = kobject_create_and_add("wacom_remote",
  1414. &wacom->hdev->dev.kobj);
  1415. if (!remote->remote_dir)
  1416. return -ENOMEM;
  1417. error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
  1418. if (error) {
  1419. hid_err(wacom->hdev,
  1420. "cannot create sysfs group err: %d\n", error);
  1421. return error;
  1422. }
  1423. for (i = 0; i < WACOM_MAX_REMOTES; i++) {
  1424. wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
  1425. remote->remotes[i].serial = 0;
  1426. }
  1427. error = devm_add_action_or_reset(&wacom->hdev->dev,
  1428. wacom_remotes_destroy, wacom);
  1429. if (error)
  1430. return error;
  1431. return 0;
  1432. }
  1433. static struct input_dev *wacom_allocate_input(struct wacom *wacom)
  1434. {
  1435. struct input_dev *input_dev;
  1436. struct hid_device *hdev = wacom->hdev;
  1437. struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
  1438. input_dev = devm_input_allocate_device(&hdev->dev);
  1439. if (!input_dev)
  1440. return NULL;
  1441. input_dev->name = wacom_wac->features.name;
  1442. input_dev->phys = hdev->phys;
  1443. input_dev->dev.parent = &hdev->dev;
  1444. input_dev->open = wacom_open;
  1445. input_dev->close = wacom_close;
  1446. input_dev->uniq = hdev->uniq;
  1447. input_dev->id.bustype = hdev->bus;
  1448. input_dev->id.vendor = hdev->vendor;
  1449. input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
  1450. input_dev->id.version = hdev->version;
  1451. input_set_drvdata(input_dev, wacom);
  1452. return input_dev;
  1453. }
  1454. static int wacom_allocate_inputs(struct wacom *wacom)
  1455. {
  1456. struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
  1457. wacom_wac->pen_input = wacom_allocate_input(wacom);
  1458. wacom_wac->touch_input = wacom_allocate_input(wacom);
  1459. wacom_wac->pad_input = wacom_allocate_input(wacom);
  1460. if (!wacom_wac->pen_input ||
  1461. !wacom_wac->touch_input ||
  1462. !wacom_wac->pad_input)
  1463. return -ENOMEM;
  1464. wacom_wac->pen_input->name = wacom_wac->pen_name;
  1465. wacom_wac->touch_input->name = wacom_wac->touch_name;
  1466. wacom_wac->pad_input->name = wacom_wac->pad_name;
  1467. return 0;
  1468. }
  1469. static int wacom_register_inputs(struct wacom *wacom)
  1470. {
  1471. struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
  1472. struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
  1473. int error = 0;
  1474. pen_input_dev = wacom_wac->pen_input;
  1475. touch_input_dev = wacom_wac->touch_input;
  1476. pad_input_dev = wacom_wac->pad_input;
  1477. if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
  1478. return -EINVAL;
  1479. error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
  1480. if (error) {
  1481. /* no pen in use on this interface */
  1482. input_free_device(pen_input_dev);
  1483. wacom_wac->pen_input = NULL;
  1484. pen_input_dev = NULL;
  1485. } else {
  1486. error = input_register_device(pen_input_dev);
  1487. if (error)
  1488. goto fail;
  1489. }
  1490. error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
  1491. if (error) {
  1492. /* no touch in use on this interface */
  1493. input_free_device(touch_input_dev);
  1494. wacom_wac->touch_input = NULL;
  1495. touch_input_dev = NULL;
  1496. } else {
  1497. error = input_register_device(touch_input_dev);
  1498. if (error)
  1499. goto fail;
  1500. }
  1501. error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
  1502. if (error) {
  1503. /* no pad in use on this interface */
  1504. input_free_device(pad_input_dev);
  1505. wacom_wac->pad_input = NULL;
  1506. pad_input_dev = NULL;
  1507. } else {
  1508. error = input_register_device(pad_input_dev);
  1509. if (error)
  1510. goto fail;
  1511. }
  1512. return 0;
  1513. fail:
  1514. wacom_wac->pad_input = NULL;
  1515. wacom_wac->touch_input = NULL;
  1516. wacom_wac->pen_input = NULL;
  1517. return error;
  1518. }
  1519. /*
  1520. * Not all devices report physical dimensions from HID.
  1521. * Compute the default from hardcoded logical dimension
  1522. * and resolution before driver overwrites them.
  1523. */
  1524. static void wacom_set_default_phy(struct wacom_features *features)
  1525. {
  1526. if (features->x_resolution) {
  1527. features->x_phy = (features->x_max * 100) /
  1528. features->x_resolution;
  1529. features->y_phy = (features->y_max * 100) /
  1530. features->y_resolution;
  1531. }
  1532. }
  1533. static void wacom_calculate_res(struct wacom_features *features)
  1534. {
  1535. /* set unit to "100th of a mm" for devices not reported by HID */
  1536. if (!features->unit) {
  1537. features->unit = 0x11;
  1538. features->unitExpo = -3;
  1539. }
  1540. features->x_resolution = wacom_calc_hid_res(features->x_max,
  1541. features->x_phy,
  1542. features->unit,
  1543. features->unitExpo);
  1544. features->y_resolution = wacom_calc_hid_res(features->y_max,
  1545. features->y_phy,
  1546. features->unit,
  1547. features->unitExpo);
  1548. }
  1549. void wacom_battery_work(struct work_struct *work)
  1550. {
  1551. struct wacom *wacom = container_of(work, struct wacom, battery_work);
  1552. if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
  1553. !wacom->battery.battery) {
  1554. wacom_initialize_battery(wacom);
  1555. }
  1556. else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
  1557. wacom->battery.battery) {
  1558. wacom_destroy_battery(wacom);
  1559. }
  1560. }
  1561. static size_t wacom_compute_pktlen(struct hid_device *hdev)
  1562. {
  1563. struct hid_report_enum *report_enum;
  1564. struct hid_report *report;
  1565. size_t size = 0;
  1566. report_enum = hdev->report_enum + HID_INPUT_REPORT;
  1567. list_for_each_entry(report, &report_enum->report_list, list) {
  1568. size_t report_size = hid_report_len(report);
  1569. if (report_size > size)
  1570. size = report_size;
  1571. }
  1572. return size;
  1573. }
  1574. static void wacom_update_name(struct wacom *wacom, const char *suffix)
  1575. {
  1576. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  1577. struct wacom_features *features = &wacom_wac->features;
  1578. char name[WACOM_NAME_MAX];
  1579. /* Generic devices name unspecified */
  1580. if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
  1581. if (strstr(wacom->hdev->name, "Wacom") ||
  1582. strstr(wacom->hdev->name, "wacom") ||
  1583. strstr(wacom->hdev->name, "WACOM")) {
  1584. /* name is in HID descriptor, use it */
  1585. strlcpy(name, wacom->hdev->name, sizeof(name));
  1586. /* strip out excess whitespaces */
  1587. while (1) {
  1588. char *gap = strstr(name, " ");
  1589. if (gap == NULL)
  1590. break;
  1591. /* shift everything including the terminator */
  1592. memmove(gap, gap+1, strlen(gap));
  1593. }
  1594. /* get rid of trailing whitespace */
  1595. if (name[strlen(name)-1] == ' ')
  1596. name[strlen(name)-1] = '\0';
  1597. } else {
  1598. /* no meaningful name retrieved. use product ID */
  1599. snprintf(name, sizeof(name),
  1600. "%s %X", features->name, wacom->hdev->product);
  1601. }
  1602. } else {
  1603. strlcpy(name, features->name, sizeof(name));
  1604. }
  1605. snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
  1606. name, suffix);
  1607. /* Append the device type to the name */
  1608. snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
  1609. "%s%s Pen", name, suffix);
  1610. snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
  1611. "%s%s Finger", name, suffix);
  1612. snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
  1613. "%s%s Pad", name, suffix);
  1614. }
  1615. static void wacom_release_resources(struct wacom *wacom)
  1616. {
  1617. struct hid_device *hdev = wacom->hdev;
  1618. if (!wacom->resources)
  1619. return;
  1620. devres_release_group(&hdev->dev, wacom);
  1621. wacom->resources = false;
  1622. wacom->wacom_wac.pen_input = NULL;
  1623. wacom->wacom_wac.touch_input = NULL;
  1624. wacom->wacom_wac.pad_input = NULL;
  1625. }
  1626. static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
  1627. {
  1628. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  1629. struct wacom_features *features = &wacom_wac->features;
  1630. struct hid_device *hdev = wacom->hdev;
  1631. int error;
  1632. unsigned int connect_mask = HID_CONNECT_HIDRAW;
  1633. features->pktlen = wacom_compute_pktlen(hdev);
  1634. if (features->pktlen > WACOM_PKGLEN_MAX)
  1635. return -EINVAL;
  1636. if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
  1637. return -ENOMEM;
  1638. wacom->resources = true;
  1639. error = wacom_allocate_inputs(wacom);
  1640. if (error)
  1641. goto fail;
  1642. /*
  1643. * Bamboo Pad has a generic hid handling for the Pen, and we switch it
  1644. * into debug mode for the touch part.
  1645. * We ignore the other interfaces.
  1646. */
  1647. if (features->type == BAMBOO_PAD) {
  1648. if (features->pktlen == WACOM_PKGLEN_PENABLED) {
  1649. features->type = HID_GENERIC;
  1650. } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
  1651. (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
  1652. error = -ENODEV;
  1653. goto fail;
  1654. }
  1655. }
  1656. /* set the default size in case we do not get them from hid */
  1657. wacom_set_default_phy(features);
  1658. /* Retrieve the physical and logical size for touch devices */
  1659. wacom_retrieve_hid_descriptor(hdev, features);
  1660. wacom_setup_device_quirks(wacom);
  1661. if (features->device_type == WACOM_DEVICETYPE_NONE &&
  1662. features->type != WIRELESS) {
  1663. error = features->type == HID_GENERIC ? -ENODEV : 0;
  1664. dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
  1665. hdev->name,
  1666. error ? "Ignoring" : "Assuming pen");
  1667. if (error)
  1668. goto fail;
  1669. features->device_type |= WACOM_DEVICETYPE_PEN;
  1670. }
  1671. wacom_calculate_res(features);
  1672. wacom_update_name(wacom, wireless ? " (WL)" : "");
  1673. /* pen only Bamboo neither support touch nor pad */
  1674. if ((features->type == BAMBOO_PEN) &&
  1675. ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
  1676. (features->device_type & WACOM_DEVICETYPE_PAD))) {
  1677. error = -ENODEV;
  1678. goto fail;
  1679. }
  1680. error = wacom_add_shared_data(hdev);
  1681. if (error)
  1682. goto fail;
  1683. if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
  1684. (features->quirks & WACOM_QUIRK_BATTERY)) {
  1685. error = wacom_initialize_battery(wacom);
  1686. if (error)
  1687. goto fail;
  1688. }
  1689. error = wacom_register_inputs(wacom);
  1690. if (error)
  1691. goto fail;
  1692. if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
  1693. error = wacom_initialize_leds(wacom);
  1694. if (error)
  1695. goto fail;
  1696. error = wacom_initialize_remotes(wacom);
  1697. if (error)
  1698. goto fail;
  1699. }
  1700. if (features->type == HID_GENERIC)
  1701. connect_mask |= HID_CONNECT_DRIVER;
  1702. /* Regular HID work starts now */
  1703. error = hid_hw_start(hdev, connect_mask);
  1704. if (error) {
  1705. hid_err(hdev, "hw start failed\n");
  1706. goto fail;
  1707. }
  1708. if (!wireless) {
  1709. /* Note that if query fails it is not a hard failure */
  1710. wacom_query_tablet_data(hdev, features);
  1711. }
  1712. /* touch only Bamboo doesn't support pen */
  1713. if ((features->type == BAMBOO_TOUCH) &&
  1714. (features->device_type & WACOM_DEVICETYPE_PEN)) {
  1715. error = -ENODEV;
  1716. goto fail_quirks;
  1717. }
  1718. if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
  1719. error = hid_hw_open(hdev);
  1720. if ((wacom_wac->features.type == INTUOSHT ||
  1721. wacom_wac->features.type == INTUOSHT2) &&
  1722. (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
  1723. wacom_wac->shared->type = wacom_wac->features.type;
  1724. wacom_wac->shared->touch_input = wacom_wac->touch_input;
  1725. }
  1726. devres_close_group(&hdev->dev, wacom);
  1727. return 0;
  1728. fail_quirks:
  1729. hid_hw_stop(hdev);
  1730. fail:
  1731. wacom_release_resources(wacom);
  1732. return error;
  1733. }
  1734. static void wacom_wireless_work(struct work_struct *work)
  1735. {
  1736. struct wacom *wacom = container_of(work, struct wacom, wireless_work);
  1737. struct usb_device *usbdev = wacom->usbdev;
  1738. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  1739. struct hid_device *hdev1, *hdev2;
  1740. struct wacom *wacom1, *wacom2;
  1741. struct wacom_wac *wacom_wac1, *wacom_wac2;
  1742. int error;
  1743. /*
  1744. * Regardless if this is a disconnect or a new tablet,
  1745. * remove any existing input and battery devices.
  1746. */
  1747. wacom_destroy_battery(wacom);
  1748. /* Stylus interface */
  1749. hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
  1750. wacom1 = hid_get_drvdata(hdev1);
  1751. wacom_wac1 = &(wacom1->wacom_wac);
  1752. wacom_release_resources(wacom1);
  1753. /* Touch interface */
  1754. hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
  1755. wacom2 = hid_get_drvdata(hdev2);
  1756. wacom_wac2 = &(wacom2->wacom_wac);
  1757. wacom_release_resources(wacom2);
  1758. if (wacom_wac->pid == 0) {
  1759. hid_info(wacom->hdev, "wireless tablet disconnected\n");
  1760. } else {
  1761. const struct hid_device_id *id = wacom_ids;
  1762. hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
  1763. wacom_wac->pid);
  1764. while (id->bus) {
  1765. if (id->vendor == USB_VENDOR_ID_WACOM &&
  1766. id->product == wacom_wac->pid)
  1767. break;
  1768. id++;
  1769. }
  1770. if (!id->bus) {
  1771. hid_info(wacom->hdev, "ignoring unknown PID.\n");
  1772. return;
  1773. }
  1774. /* Stylus interface */
  1775. wacom_wac1->features =
  1776. *((struct wacom_features *)id->driver_data);
  1777. wacom_wac1->pid = wacom_wac->pid;
  1778. hid_hw_stop(hdev1);
  1779. error = wacom_parse_and_register(wacom1, true);
  1780. if (error)
  1781. goto fail;
  1782. /* Touch interface */
  1783. if (wacom_wac1->features.touch_max ||
  1784. (wacom_wac1->features.type >= INTUOSHT &&
  1785. wacom_wac1->features.type <= BAMBOO_PT)) {
  1786. wacom_wac2->features =
  1787. *((struct wacom_features *)id->driver_data);
  1788. wacom_wac2->pid = wacom_wac->pid;
  1789. hid_hw_stop(hdev2);
  1790. error = wacom_parse_and_register(wacom2, true);
  1791. if (error)
  1792. goto fail;
  1793. }
  1794. strlcpy(wacom_wac->name, wacom_wac1->name,
  1795. sizeof(wacom_wac->name));
  1796. error = wacom_initialize_battery(wacom);
  1797. if (error)
  1798. goto fail;
  1799. }
  1800. return;
  1801. fail:
  1802. wacom_release_resources(wacom1);
  1803. wacom_release_resources(wacom2);
  1804. return;
  1805. }
  1806. static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
  1807. {
  1808. struct wacom_remote *remote = wacom->remote;
  1809. u32 serial = remote->remotes[index].serial;
  1810. int i;
  1811. unsigned long flags;
  1812. spin_lock_irqsave(&remote->remote_lock, flags);
  1813. remote->remotes[index].registered = false;
  1814. spin_unlock_irqrestore(&remote->remote_lock, flags);
  1815. if (remote->remotes[index].battery.battery)
  1816. devres_release_group(&wacom->hdev->dev,
  1817. &remote->remotes[index].battery.bat_desc);
  1818. if (remote->remotes[index].group.name)
  1819. devres_release_group(&wacom->hdev->dev,
  1820. &remote->remotes[index]);
  1821. for (i = 0; i < WACOM_MAX_REMOTES; i++) {
  1822. if (remote->remotes[i].serial == serial) {
  1823. remote->remotes[i].serial = 0;
  1824. remote->remotes[i].group.name = NULL;
  1825. remote->remotes[i].registered = false;
  1826. remote->remotes[i].battery.battery = NULL;
  1827. wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
  1828. }
  1829. }
  1830. }
  1831. static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
  1832. unsigned int index)
  1833. {
  1834. struct wacom_remote *remote = wacom->remote;
  1835. struct device *dev = &wacom->hdev->dev;
  1836. int error, k;
  1837. /* A remote can pair more than once with an EKR,
  1838. * check to make sure this serial isn't already paired.
  1839. */
  1840. for (k = 0; k < WACOM_MAX_REMOTES; k++) {
  1841. if (remote->remotes[k].serial == serial)
  1842. break;
  1843. }
  1844. if (k < WACOM_MAX_REMOTES) {
  1845. remote->remotes[index].serial = serial;
  1846. return 0;
  1847. }
  1848. if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
  1849. return -ENOMEM;
  1850. error = wacom_remote_create_attr_group(wacom, serial, index);
  1851. if (error)
  1852. goto fail;
  1853. remote->remotes[index].input = wacom_allocate_input(wacom);
  1854. if (!remote->remotes[index].input) {
  1855. error = -ENOMEM;
  1856. goto fail;
  1857. }
  1858. remote->remotes[index].input->uniq = remote->remotes[index].group.name;
  1859. remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
  1860. if (!remote->remotes[index].input->name) {
  1861. error = -EINVAL;
  1862. goto fail;
  1863. }
  1864. error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
  1865. &wacom->wacom_wac);
  1866. if (error)
  1867. goto fail;
  1868. remote->remotes[index].serial = serial;
  1869. error = input_register_device(remote->remotes[index].input);
  1870. if (error)
  1871. goto fail;
  1872. error = wacom_led_groups_alloc_and_register_one(
  1873. &remote->remotes[index].input->dev,
  1874. wacom, index, 3, true);
  1875. if (error)
  1876. goto fail;
  1877. remote->remotes[index].registered = true;
  1878. devres_close_group(dev, &remote->remotes[index]);
  1879. return 0;
  1880. fail:
  1881. devres_release_group(dev, &remote->remotes[index]);
  1882. remote->remotes[index].serial = 0;
  1883. return error;
  1884. }
  1885. static int wacom_remote_attach_battery(struct wacom *wacom, int index)
  1886. {
  1887. struct wacom_remote *remote = wacom->remote;
  1888. int error;
  1889. if (!remote->remotes[index].registered)
  1890. return 0;
  1891. if (remote->remotes[index].battery.battery)
  1892. return 0;
  1893. if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
  1894. return 0;
  1895. error = __wacom_initialize_battery(wacom,
  1896. &wacom->remote->remotes[index].battery);
  1897. if (error)
  1898. return error;
  1899. return 0;
  1900. }
  1901. static void wacom_remote_work(struct work_struct *work)
  1902. {
  1903. struct wacom *wacom = container_of(work, struct wacom, remote_work);
  1904. struct wacom_remote *remote = wacom->remote;
  1905. struct wacom_remote_data data;
  1906. unsigned long flags;
  1907. unsigned int count;
  1908. u32 serial;
  1909. int i;
  1910. spin_lock_irqsave(&remote->remote_lock, flags);
  1911. count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
  1912. if (count != sizeof(data)) {
  1913. hid_err(wacom->hdev,
  1914. "workitem triggered without status available\n");
  1915. spin_unlock_irqrestore(&remote->remote_lock, flags);
  1916. return;
  1917. }
  1918. if (!kfifo_is_empty(&remote->remote_fifo))
  1919. wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
  1920. spin_unlock_irqrestore(&remote->remote_lock, flags);
  1921. for (i = 0; i < WACOM_MAX_REMOTES; i++) {
  1922. serial = data.remote[i].serial;
  1923. if (data.remote[i].connected) {
  1924. if (remote->remotes[i].serial == serial) {
  1925. wacom_remote_attach_battery(wacom, i);
  1926. continue;
  1927. }
  1928. if (remote->remotes[i].serial)
  1929. wacom_remote_destroy_one(wacom, i);
  1930. wacom_remote_create_one(wacom, serial, i);
  1931. } else if (remote->remotes[i].serial) {
  1932. wacom_remote_destroy_one(wacom, i);
  1933. }
  1934. }
  1935. }
  1936. static int wacom_probe(struct hid_device *hdev,
  1937. const struct hid_device_id *id)
  1938. {
  1939. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  1940. struct usb_device *dev = interface_to_usbdev(intf);
  1941. struct wacom *wacom;
  1942. struct wacom_wac *wacom_wac;
  1943. struct wacom_features *features;
  1944. int error;
  1945. if (!id->driver_data)
  1946. return -EINVAL;
  1947. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  1948. /* hid-core sets this quirk for the boot interface */
  1949. hdev->quirks &= ~HID_QUIRK_NOGET;
  1950. wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
  1951. if (!wacom)
  1952. return -ENOMEM;
  1953. hid_set_drvdata(hdev, wacom);
  1954. wacom->hdev = hdev;
  1955. wacom_wac = &wacom->wacom_wac;
  1956. wacom_wac->features = *((struct wacom_features *)id->driver_data);
  1957. features = &wacom_wac->features;
  1958. if (features->check_for_hid_type && features->hid_type != hdev->type) {
  1959. error = -ENODEV;
  1960. goto fail;
  1961. }
  1962. wacom_wac->hid_data.inputmode = -1;
  1963. wacom_wac->mode_report = -1;
  1964. wacom->usbdev = dev;
  1965. wacom->intf = intf;
  1966. mutex_init(&wacom->lock);
  1967. INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
  1968. INIT_WORK(&wacom->battery_work, wacom_battery_work);
  1969. INIT_WORK(&wacom->remote_work, wacom_remote_work);
  1970. /* ask for the report descriptor to be loaded by HID */
  1971. error = hid_parse(hdev);
  1972. if (error) {
  1973. hid_err(hdev, "parse failed\n");
  1974. goto fail;
  1975. }
  1976. error = wacom_parse_and_register(wacom, false);
  1977. if (error)
  1978. goto fail;
  1979. if (hdev->bus == BUS_BLUETOOTH) {
  1980. error = device_create_file(&hdev->dev, &dev_attr_speed);
  1981. if (error)
  1982. hid_warn(hdev,
  1983. "can't create sysfs speed attribute err: %d\n",
  1984. error);
  1985. }
  1986. return 0;
  1987. fail:
  1988. hid_set_drvdata(hdev, NULL);
  1989. return error;
  1990. }
  1991. static void wacom_remove(struct hid_device *hdev)
  1992. {
  1993. struct wacom *wacom = hid_get_drvdata(hdev);
  1994. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  1995. struct wacom_features *features = &wacom_wac->features;
  1996. if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
  1997. hid_hw_close(hdev);
  1998. hid_hw_stop(hdev);
  1999. cancel_work_sync(&wacom->wireless_work);
  2000. cancel_work_sync(&wacom->battery_work);
  2001. cancel_work_sync(&wacom->remote_work);
  2002. if (hdev->bus == BUS_BLUETOOTH)
  2003. device_remove_file(&hdev->dev, &dev_attr_speed);
  2004. wacom_release_resources(wacom);
  2005. hid_set_drvdata(hdev, NULL);
  2006. }
  2007. #ifdef CONFIG_PM
  2008. static int wacom_resume(struct hid_device *hdev)
  2009. {
  2010. struct wacom *wacom = hid_get_drvdata(hdev);
  2011. struct wacom_features *features = &wacom->wacom_wac.features;
  2012. mutex_lock(&wacom->lock);
  2013. /* switch to wacom mode first */
  2014. wacom_query_tablet_data(hdev, features);
  2015. wacom_led_control(wacom);
  2016. mutex_unlock(&wacom->lock);
  2017. return 0;
  2018. }
  2019. static int wacom_reset_resume(struct hid_device *hdev)
  2020. {
  2021. return wacom_resume(hdev);
  2022. }
  2023. #endif /* CONFIG_PM */
  2024. static struct hid_driver wacom_driver = {
  2025. .name = "wacom",
  2026. .id_table = wacom_ids,
  2027. .probe = wacom_probe,
  2028. .remove = wacom_remove,
  2029. .report = wacom_wac_report,
  2030. #ifdef CONFIG_PM
  2031. .resume = wacom_resume,
  2032. .reset_resume = wacom_reset_resume,
  2033. #endif
  2034. .raw_event = wacom_raw_event,
  2035. };
  2036. module_hid_driver(wacom_driver);
  2037. MODULE_VERSION(DRIVER_VERSION);
  2038. MODULE_AUTHOR(DRIVER_AUTHOR);
  2039. MODULE_DESCRIPTION(DRIVER_DESC);
  2040. MODULE_LICENSE(DRIVER_LICENSE);