wacom_sys.c 68 KB

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