wacom_sys.c 75 KB

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