wacom_sys.c 71 KB

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