wacom_sys.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900
  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. if (!read_only) {
  1154. led->trigger.name = name;
  1155. error = devm_led_trigger_register(dev, &led->trigger);
  1156. if (error) {
  1157. hid_err(wacom->hdev,
  1158. "failed to register LED trigger %s: %d\n",
  1159. led->cdev.name, error);
  1160. return error;
  1161. }
  1162. }
  1163. led->group = group;
  1164. led->id = id;
  1165. led->wacom = wacom;
  1166. led->llv = wacom->led.llv;
  1167. led->hlv = wacom->led.hlv;
  1168. led->cdev.name = name;
  1169. led->cdev.max_brightness = LED_FULL;
  1170. led->cdev.flags = LED_HW_PLUGGABLE;
  1171. led->cdev.brightness_get = __wacom_led_brightness_get;
  1172. if (!read_only) {
  1173. led->cdev.brightness_set_blocking = wacom_led_brightness_set;
  1174. led->cdev.default_trigger = led->cdev.name;
  1175. } else {
  1176. led->cdev.brightness_set = wacom_led_readonly_brightness_set;
  1177. }
  1178. error = devm_led_classdev_register(dev, &led->cdev);
  1179. if (error) {
  1180. hid_err(wacom->hdev,
  1181. "failed to register LED %s: %d\n",
  1182. led->cdev.name, error);
  1183. led->cdev.name = NULL;
  1184. return error;
  1185. }
  1186. return 0;
  1187. }
  1188. static void wacom_led_groups_release_one(void *data)
  1189. {
  1190. struct wacom_group_leds *group = data;
  1191. devres_release_group(group->dev, group);
  1192. }
  1193. static int wacom_led_groups_alloc_and_register_one(struct device *dev,
  1194. struct wacom *wacom,
  1195. int group_id, int count,
  1196. bool read_only)
  1197. {
  1198. struct wacom_led *leds;
  1199. int i, error;
  1200. if (group_id >= wacom->led.count || count <= 0)
  1201. return -EINVAL;
  1202. if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
  1203. return -ENOMEM;
  1204. leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
  1205. if (!leds) {
  1206. error = -ENOMEM;
  1207. goto err;
  1208. }
  1209. wacom->led.groups[group_id].leds = leds;
  1210. wacom->led.groups[group_id].count = count;
  1211. for (i = 0; i < count; i++) {
  1212. error = wacom_led_register_one(dev, wacom, &leds[i],
  1213. group_id, i, read_only);
  1214. if (error)
  1215. goto err;
  1216. }
  1217. wacom->led.groups[group_id].dev = dev;
  1218. devres_close_group(dev, &wacom->led.groups[group_id]);
  1219. /*
  1220. * There is a bug (?) in devm_led_classdev_register() in which its
  1221. * increments the refcount of the parent. If the parent is an input
  1222. * device, that means the ref count never reaches 0 when
  1223. * devm_input_device_release() gets called.
  1224. * This means that the LEDs are still there after disconnect.
  1225. * Manually force the release of the group so that the leds are released
  1226. * once we are done using them.
  1227. */
  1228. error = devm_add_action_or_reset(&wacom->hdev->dev,
  1229. wacom_led_groups_release_one,
  1230. &wacom->led.groups[group_id]);
  1231. if (error)
  1232. return error;
  1233. return 0;
  1234. err:
  1235. devres_release_group(dev, &wacom->led.groups[group_id]);
  1236. return error;
  1237. }
  1238. struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
  1239. unsigned int id)
  1240. {
  1241. struct wacom_group_leds *group;
  1242. if (group_id >= wacom->led.count)
  1243. return NULL;
  1244. group = &wacom->led.groups[group_id];
  1245. if (!group->leds)
  1246. return NULL;
  1247. id %= group->count;
  1248. return &group->leds[id];
  1249. }
  1250. /*
  1251. * wacom_led_next: gives the next available led with a wacom trigger.
  1252. *
  1253. * returns the next available struct wacom_led which has its default trigger
  1254. * or the current one if none is available.
  1255. */
  1256. struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
  1257. {
  1258. struct wacom_led *next_led;
  1259. int group, next;
  1260. if (!wacom || !cur)
  1261. return NULL;
  1262. group = cur->group;
  1263. next = cur->id;
  1264. do {
  1265. next_led = wacom_led_find(wacom, group, ++next);
  1266. if (!next_led || next_led == cur)
  1267. return next_led;
  1268. } while (next_led->cdev.trigger != &next_led->trigger);
  1269. return next_led;
  1270. }
  1271. static void wacom_led_groups_release(void *data)
  1272. {
  1273. struct wacom *wacom = data;
  1274. wacom->led.groups = NULL;
  1275. wacom->led.count = 0;
  1276. }
  1277. static int wacom_led_groups_allocate(struct wacom *wacom, int count)
  1278. {
  1279. struct device *dev = &wacom->hdev->dev;
  1280. struct wacom_group_leds *groups;
  1281. int error;
  1282. groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
  1283. GFP_KERNEL);
  1284. if (!groups)
  1285. return -ENOMEM;
  1286. error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
  1287. if (error)
  1288. return error;
  1289. wacom->led.groups = groups;
  1290. wacom->led.count = count;
  1291. return 0;
  1292. }
  1293. static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
  1294. int led_per_group, bool read_only)
  1295. {
  1296. struct device *dev;
  1297. int i, error;
  1298. if (!wacom->wacom_wac.pad_input)
  1299. return -EINVAL;
  1300. dev = &wacom->wacom_wac.pad_input->dev;
  1301. error = wacom_led_groups_allocate(wacom, group_count);
  1302. if (error)
  1303. return error;
  1304. for (i = 0; i < group_count; i++) {
  1305. error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
  1306. led_per_group,
  1307. read_only);
  1308. if (error)
  1309. return error;
  1310. }
  1311. return 0;
  1312. }
  1313. int wacom_initialize_leds(struct wacom *wacom)
  1314. {
  1315. int error;
  1316. if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
  1317. return 0;
  1318. /* Initialize default values */
  1319. switch (wacom->wacom_wac.features.type) {
  1320. case HID_GENERIC:
  1321. if (!wacom->generic_has_leds)
  1322. return 0;
  1323. wacom->led.llv = 100;
  1324. wacom->led.max_llv = 100;
  1325. error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
  1326. if (error) {
  1327. hid_err(wacom->hdev,
  1328. "cannot create leds err: %d\n", error);
  1329. return error;
  1330. }
  1331. error = wacom_devm_sysfs_create_group(wacom,
  1332. &generic_led_attr_group);
  1333. break;
  1334. case INTUOS4S:
  1335. case INTUOS4:
  1336. case INTUOS4WL:
  1337. case INTUOS4L:
  1338. wacom->led.llv = 10;
  1339. wacom->led.hlv = 20;
  1340. wacom->led.max_llv = 127;
  1341. wacom->led.max_hlv = 127;
  1342. wacom->led.img_lum = 10;
  1343. error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
  1344. if (error) {
  1345. hid_err(wacom->hdev,
  1346. "cannot create leds err: %d\n", error);
  1347. return error;
  1348. }
  1349. error = wacom_devm_sysfs_create_group(wacom,
  1350. &intuos4_led_attr_group);
  1351. break;
  1352. case WACOM_24HD:
  1353. case WACOM_21UX2:
  1354. wacom->led.llv = 0;
  1355. wacom->led.hlv = 0;
  1356. wacom->led.img_lum = 0;
  1357. error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
  1358. if (error) {
  1359. hid_err(wacom->hdev,
  1360. "cannot create leds err: %d\n", error);
  1361. return error;
  1362. }
  1363. error = wacom_devm_sysfs_create_group(wacom,
  1364. &cintiq_led_attr_group);
  1365. break;
  1366. case INTUOS5S:
  1367. case INTUOS5:
  1368. case INTUOS5L:
  1369. case INTUOSPS:
  1370. case INTUOSPM:
  1371. case INTUOSPL:
  1372. wacom->led.llv = 32;
  1373. wacom->led.max_llv = 96;
  1374. error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
  1375. if (error) {
  1376. hid_err(wacom->hdev,
  1377. "cannot create leds err: %d\n", error);
  1378. return error;
  1379. }
  1380. error = wacom_devm_sysfs_create_group(wacom,
  1381. &intuos5_led_attr_group);
  1382. break;
  1383. case INTUOSP2_BT:
  1384. wacom->led.llv = 50;
  1385. wacom->led.max_llv = 100;
  1386. error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
  1387. if (error) {
  1388. hid_err(wacom->hdev,
  1389. "cannot create leds err: %d\n", error);
  1390. return error;
  1391. }
  1392. return 0;
  1393. case REMOTE:
  1394. wacom->led.llv = 255;
  1395. wacom->led.max_llv = 255;
  1396. error = wacom_led_groups_allocate(wacom, 5);
  1397. if (error) {
  1398. hid_err(wacom->hdev,
  1399. "cannot create leds err: %d\n", error);
  1400. return error;
  1401. }
  1402. return 0;
  1403. default:
  1404. return 0;
  1405. }
  1406. if (error) {
  1407. hid_err(wacom->hdev,
  1408. "cannot create sysfs group err: %d\n", error);
  1409. return error;
  1410. }
  1411. return 0;
  1412. }
  1413. static void wacom_init_work(struct work_struct *work)
  1414. {
  1415. struct wacom *wacom = container_of(work, struct wacom, init_work.work);
  1416. _wacom_query_tablet_data(wacom);
  1417. wacom_led_control(wacom);
  1418. }
  1419. static void wacom_query_tablet_data(struct wacom *wacom)
  1420. {
  1421. schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
  1422. }
  1423. static enum power_supply_property wacom_battery_props[] = {
  1424. POWER_SUPPLY_PROP_MODEL_NAME,
  1425. POWER_SUPPLY_PROP_PRESENT,
  1426. POWER_SUPPLY_PROP_STATUS,
  1427. POWER_SUPPLY_PROP_SCOPE,
  1428. POWER_SUPPLY_PROP_CAPACITY
  1429. };
  1430. static int wacom_battery_get_property(struct power_supply *psy,
  1431. enum power_supply_property psp,
  1432. union power_supply_propval *val)
  1433. {
  1434. struct wacom_battery *battery = power_supply_get_drvdata(psy);
  1435. int ret = 0;
  1436. switch (psp) {
  1437. case POWER_SUPPLY_PROP_MODEL_NAME:
  1438. val->strval = battery->wacom->wacom_wac.name;
  1439. break;
  1440. case POWER_SUPPLY_PROP_PRESENT:
  1441. val->intval = battery->bat_connected;
  1442. break;
  1443. case POWER_SUPPLY_PROP_SCOPE:
  1444. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  1445. break;
  1446. case POWER_SUPPLY_PROP_CAPACITY:
  1447. val->intval = battery->battery_capacity;
  1448. break;
  1449. case POWER_SUPPLY_PROP_STATUS:
  1450. if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
  1451. val->intval = battery->bat_status;
  1452. else if (battery->bat_charging)
  1453. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  1454. else if (battery->battery_capacity == 100 &&
  1455. battery->ps_connected)
  1456. val->intval = POWER_SUPPLY_STATUS_FULL;
  1457. else if (battery->ps_connected)
  1458. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  1459. else
  1460. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  1461. break;
  1462. default:
  1463. ret = -EINVAL;
  1464. break;
  1465. }
  1466. return ret;
  1467. }
  1468. static int __wacom_initialize_battery(struct wacom *wacom,
  1469. struct wacom_battery *battery)
  1470. {
  1471. static atomic_t battery_no = ATOMIC_INIT(0);
  1472. struct device *dev = &wacom->hdev->dev;
  1473. struct power_supply_config psy_cfg = { .drv_data = battery, };
  1474. struct power_supply *ps_bat;
  1475. struct power_supply_desc *bat_desc = &battery->bat_desc;
  1476. unsigned long n;
  1477. int error;
  1478. if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
  1479. return -ENOMEM;
  1480. battery->wacom = wacom;
  1481. n = atomic_inc_return(&battery_no) - 1;
  1482. bat_desc->properties = wacom_battery_props;
  1483. bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
  1484. bat_desc->get_property = wacom_battery_get_property;
  1485. sprintf(battery->bat_name, "wacom_battery_%ld", n);
  1486. bat_desc->name = battery->bat_name;
  1487. bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  1488. bat_desc->use_for_apm = 0;
  1489. ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
  1490. if (IS_ERR(ps_bat)) {
  1491. error = PTR_ERR(ps_bat);
  1492. goto err;
  1493. }
  1494. power_supply_powers(ps_bat, &wacom->hdev->dev);
  1495. battery->battery = ps_bat;
  1496. devres_close_group(dev, bat_desc);
  1497. return 0;
  1498. err:
  1499. devres_release_group(dev, bat_desc);
  1500. return error;
  1501. }
  1502. static int wacom_initialize_battery(struct wacom *wacom)
  1503. {
  1504. if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
  1505. return __wacom_initialize_battery(wacom, &wacom->battery);
  1506. return 0;
  1507. }
  1508. static void wacom_destroy_battery(struct wacom *wacom)
  1509. {
  1510. if (wacom->battery.battery) {
  1511. devres_release_group(&wacom->hdev->dev,
  1512. &wacom->battery.bat_desc);
  1513. wacom->battery.battery = NULL;
  1514. }
  1515. }
  1516. static ssize_t wacom_show_speed(struct device *dev,
  1517. struct device_attribute
  1518. *attr, char *buf)
  1519. {
  1520. struct hid_device *hdev = to_hid_device(dev);
  1521. struct wacom *wacom = hid_get_drvdata(hdev);
  1522. return sysfs_emit(buf, "%i\n", wacom->wacom_wac.bt_high_speed);
  1523. }
  1524. static ssize_t wacom_store_speed(struct device *dev,
  1525. struct device_attribute *attr,
  1526. const char *buf, size_t count)
  1527. {
  1528. struct hid_device *hdev = to_hid_device(dev);
  1529. struct wacom *wacom = hid_get_drvdata(hdev);
  1530. u8 new_speed;
  1531. if (kstrtou8(buf, 0, &new_speed))
  1532. return -EINVAL;
  1533. if (new_speed != 0 && new_speed != 1)
  1534. return -EINVAL;
  1535. wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
  1536. return count;
  1537. }
  1538. static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
  1539. wacom_show_speed, wacom_store_speed);
  1540. static ssize_t wacom_show_remote_mode(struct kobject *kobj,
  1541. struct kobj_attribute *kattr,
  1542. char *buf, int index)
  1543. {
  1544. struct device *dev = kobj_to_dev(kobj->parent);
  1545. struct hid_device *hdev = to_hid_device(dev);
  1546. struct wacom *wacom = hid_get_drvdata(hdev);
  1547. u8 mode;
  1548. mode = wacom->led.groups[index].select;
  1549. return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
  1550. }
  1551. #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
  1552. static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
  1553. struct kobj_attribute *kattr, char *buf) \
  1554. { \
  1555. return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
  1556. } \
  1557. static struct kobj_attribute remote##SET_ID##_mode_attr = { \
  1558. .attr = {.name = "remote_mode", \
  1559. .mode = DEV_ATTR_RO_PERM}, \
  1560. .show = wacom_show_remote##SET_ID##_mode, \
  1561. }; \
  1562. static struct attribute *remote##SET_ID##_serial_attrs[] = { \
  1563. &remote##SET_ID##_mode_attr.attr, \
  1564. NULL \
  1565. }; \
  1566. static const struct attribute_group remote##SET_ID##_serial_group = { \
  1567. .name = NULL, \
  1568. .attrs = remote##SET_ID##_serial_attrs, \
  1569. }
  1570. DEVICE_EKR_ATTR_GROUP(0);
  1571. DEVICE_EKR_ATTR_GROUP(1);
  1572. DEVICE_EKR_ATTR_GROUP(2);
  1573. DEVICE_EKR_ATTR_GROUP(3);
  1574. DEVICE_EKR_ATTR_GROUP(4);
  1575. static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
  1576. int index)
  1577. {
  1578. int error = 0;
  1579. struct wacom_remote *remote = wacom->remote;
  1580. remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
  1581. GFP_KERNEL,
  1582. "%d", serial);
  1583. if (!remote->remotes[index].group.name)
  1584. return -ENOMEM;
  1585. error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
  1586. &remote->remotes[index].group);
  1587. if (error) {
  1588. remote->remotes[index].group.name = NULL;
  1589. hid_err(wacom->hdev,
  1590. "cannot create sysfs group err: %d\n", error);
  1591. return error;
  1592. }
  1593. return 0;
  1594. }
  1595. static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
  1596. {
  1597. const size_t buf_size = 2;
  1598. unsigned char *buf;
  1599. int retval;
  1600. buf = kzalloc(buf_size, GFP_KERNEL);
  1601. if (!buf)
  1602. return -ENOMEM;
  1603. buf[0] = WAC_CMD_DELETE_PAIRING;
  1604. buf[1] = selector;
  1605. retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
  1606. buf_size, WAC_CMD_RETRIES);
  1607. kfree(buf);
  1608. return retval;
  1609. }
  1610. static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
  1611. struct kobj_attribute *attr,
  1612. const char *buf, size_t count)
  1613. {
  1614. unsigned char selector = 0;
  1615. struct device *dev = kobj_to_dev(kobj->parent);
  1616. struct hid_device *hdev = to_hid_device(dev);
  1617. struct wacom *wacom = hid_get_drvdata(hdev);
  1618. int err;
  1619. if (!strncmp(buf, "*\n", 2)) {
  1620. selector = WAC_CMD_UNPAIR_ALL;
  1621. } else {
  1622. hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
  1623. buf);
  1624. return -1;
  1625. }
  1626. mutex_lock(&wacom->lock);
  1627. err = wacom_cmd_unpair_remote(wacom, selector);
  1628. mutex_unlock(&wacom->lock);
  1629. return err < 0 ? err : count;
  1630. }
  1631. static struct kobj_attribute unpair_remote_attr = {
  1632. .attr = {.name = "unpair_remote", .mode = 0200},
  1633. .store = wacom_store_unpair_remote,
  1634. };
  1635. static const struct attribute *remote_unpair_attrs[] = {
  1636. &unpair_remote_attr.attr,
  1637. NULL
  1638. };
  1639. static void wacom_remotes_destroy(void *data)
  1640. {
  1641. struct wacom *wacom = data;
  1642. struct wacom_remote *remote = wacom->remote;
  1643. if (!remote)
  1644. return;
  1645. kobject_put(remote->remote_dir);
  1646. kfifo_free(&remote->remote_fifo);
  1647. wacom->remote = NULL;
  1648. }
  1649. static int wacom_initialize_remotes(struct wacom *wacom)
  1650. {
  1651. int error = 0;
  1652. struct wacom_remote *remote;
  1653. int i;
  1654. if (wacom->wacom_wac.features.type != REMOTE)
  1655. return 0;
  1656. remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
  1657. GFP_KERNEL);
  1658. if (!remote)
  1659. return -ENOMEM;
  1660. wacom->remote = remote;
  1661. spin_lock_init(&remote->remote_lock);
  1662. error = kfifo_alloc(&remote->remote_fifo,
  1663. 5 * sizeof(struct wacom_remote_data),
  1664. GFP_KERNEL);
  1665. if (error) {
  1666. hid_err(wacom->hdev, "failed allocating remote_fifo\n");
  1667. return -ENOMEM;
  1668. }
  1669. remote->remotes[0].group = remote0_serial_group;
  1670. remote->remotes[1].group = remote1_serial_group;
  1671. remote->remotes[2].group = remote2_serial_group;
  1672. remote->remotes[3].group = remote3_serial_group;
  1673. remote->remotes[4].group = remote4_serial_group;
  1674. remote->remote_dir = kobject_create_and_add("wacom_remote",
  1675. &wacom->hdev->dev.kobj);
  1676. if (!remote->remote_dir)
  1677. return -ENOMEM;
  1678. error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
  1679. if (error) {
  1680. hid_err(wacom->hdev,
  1681. "cannot create sysfs group err: %d\n", error);
  1682. return error;
  1683. }
  1684. for (i = 0; i < WACOM_MAX_REMOTES; i++) {
  1685. wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
  1686. remote->remotes[i].serial = 0;
  1687. }
  1688. error = devm_add_action_or_reset(&wacom->hdev->dev,
  1689. wacom_remotes_destroy, wacom);
  1690. if (error)
  1691. return error;
  1692. return 0;
  1693. }
  1694. static struct input_dev *wacom_allocate_input(struct wacom *wacom)
  1695. {
  1696. struct input_dev *input_dev;
  1697. struct hid_device *hdev = wacom->hdev;
  1698. struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
  1699. input_dev = devm_input_allocate_device(&hdev->dev);
  1700. if (!input_dev)
  1701. return NULL;
  1702. input_dev->name = wacom_wac->features.name;
  1703. input_dev->phys = hdev->phys;
  1704. input_dev->dev.parent = &hdev->dev;
  1705. input_dev->open = wacom_open;
  1706. input_dev->close = wacom_close;
  1707. input_dev->uniq = hdev->uniq;
  1708. input_dev->id.bustype = hdev->bus;
  1709. input_dev->id.vendor = hdev->vendor;
  1710. input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
  1711. input_dev->id.version = hdev->version;
  1712. input_set_drvdata(input_dev, wacom);
  1713. return input_dev;
  1714. }
  1715. static int wacom_allocate_inputs(struct wacom *wacom)
  1716. {
  1717. struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
  1718. wacom_wac->pen_input = wacom_allocate_input(wacom);
  1719. wacom_wac->touch_input = wacom_allocate_input(wacom);
  1720. wacom_wac->pad_input = wacom_allocate_input(wacom);
  1721. if (!wacom_wac->pen_input ||
  1722. !wacom_wac->touch_input ||
  1723. !wacom_wac->pad_input)
  1724. return -ENOMEM;
  1725. wacom_wac->pen_input->name = wacom_wac->pen_name;
  1726. wacom_wac->touch_input->name = wacom_wac->touch_name;
  1727. wacom_wac->pad_input->name = wacom_wac->pad_name;
  1728. return 0;
  1729. }
  1730. static int wacom_register_inputs(struct wacom *wacom)
  1731. {
  1732. struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
  1733. struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
  1734. int error = 0;
  1735. pen_input_dev = wacom_wac->pen_input;
  1736. touch_input_dev = wacom_wac->touch_input;
  1737. pad_input_dev = wacom_wac->pad_input;
  1738. if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
  1739. return -EINVAL;
  1740. error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
  1741. if (error) {
  1742. /* no pen in use on this interface */
  1743. input_free_device(pen_input_dev);
  1744. wacom_wac->pen_input = NULL;
  1745. pen_input_dev = NULL;
  1746. } else {
  1747. error = input_register_device(pen_input_dev);
  1748. if (error)
  1749. goto fail;
  1750. }
  1751. error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
  1752. if (error) {
  1753. /* no touch in use on this interface */
  1754. input_free_device(touch_input_dev);
  1755. wacom_wac->touch_input = NULL;
  1756. touch_input_dev = NULL;
  1757. } else {
  1758. error = input_register_device(touch_input_dev);
  1759. if (error)
  1760. goto fail;
  1761. }
  1762. error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
  1763. if (error) {
  1764. /* no pad events using this interface */
  1765. input_free_device(pad_input_dev);
  1766. wacom_wac->pad_input = NULL;
  1767. pad_input_dev = NULL;
  1768. } else {
  1769. error = input_register_device(pad_input_dev);
  1770. if (error)
  1771. goto fail;
  1772. }
  1773. return 0;
  1774. fail:
  1775. wacom_wac->pad_input = NULL;
  1776. wacom_wac->touch_input = NULL;
  1777. wacom_wac->pen_input = NULL;
  1778. return error;
  1779. }
  1780. /*
  1781. * Not all devices report physical dimensions from HID.
  1782. * Compute the default from hardcoded logical dimension
  1783. * and resolution before driver overwrites them.
  1784. */
  1785. static void wacom_set_default_phy(struct wacom_features *features)
  1786. {
  1787. if (features->x_resolution) {
  1788. features->x_phy = (features->x_max * 100) /
  1789. features->x_resolution;
  1790. features->y_phy = (features->y_max * 100) /
  1791. features->y_resolution;
  1792. }
  1793. }
  1794. static void wacom_calculate_res(struct wacom_features *features)
  1795. {
  1796. /* set unit to "100th of a mm" for devices not reported by HID */
  1797. if (!features->unit) {
  1798. features->unit = 0x11;
  1799. features->unitExpo = -3;
  1800. }
  1801. features->x_resolution = wacom_calc_hid_res(features->x_max,
  1802. features->x_phy,
  1803. features->unit,
  1804. features->unitExpo);
  1805. features->y_resolution = wacom_calc_hid_res(features->y_max,
  1806. features->y_phy,
  1807. features->unit,
  1808. features->unitExpo);
  1809. }
  1810. void wacom_battery_work(struct work_struct *work)
  1811. {
  1812. struct wacom *wacom = container_of(work, struct wacom, battery_work);
  1813. if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
  1814. !wacom->battery.battery) {
  1815. wacom_initialize_battery(wacom);
  1816. }
  1817. else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
  1818. wacom->battery.battery) {
  1819. wacom_destroy_battery(wacom);
  1820. }
  1821. }
  1822. static size_t wacom_compute_pktlen(struct hid_device *hdev)
  1823. {
  1824. struct hid_report_enum *report_enum;
  1825. struct hid_report *report;
  1826. size_t size = 0;
  1827. report_enum = hdev->report_enum + HID_INPUT_REPORT;
  1828. list_for_each_entry(report, &report_enum->report_list, list) {
  1829. size_t report_size = hid_report_len(report);
  1830. if (report_size > size)
  1831. size = report_size;
  1832. }
  1833. return size;
  1834. }
  1835. static void wacom_update_name(struct wacom *wacom, const char *suffix)
  1836. {
  1837. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  1838. struct wacom_features *features = &wacom_wac->features;
  1839. char name[WACOM_NAME_MAX - 20]; /* Leave some room for suffixes */
  1840. /* Generic devices name unspecified */
  1841. if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
  1842. char *product_name = wacom->hdev->name;
  1843. if (hid_is_usb(wacom->hdev)) {
  1844. struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
  1845. struct usb_device *dev = interface_to_usbdev(intf);
  1846. product_name = dev->product;
  1847. }
  1848. if (wacom->hdev->bus == BUS_I2C) {
  1849. snprintf(name, sizeof(name), "%s %X",
  1850. features->name, wacom->hdev->product);
  1851. } else if (strstr(product_name, "Wacom") ||
  1852. strstr(product_name, "wacom") ||
  1853. strstr(product_name, "WACOM")) {
  1854. strscpy(name, product_name, sizeof(name));
  1855. } else {
  1856. snprintf(name, sizeof(name), "Wacom %s", product_name);
  1857. }
  1858. /* strip out excess whitespaces */
  1859. while (1) {
  1860. char *gap = strstr(name, " ");
  1861. if (gap == NULL)
  1862. break;
  1863. /* shift everything including the terminator */
  1864. memmove(gap, gap+1, strlen(gap));
  1865. }
  1866. /* get rid of trailing whitespace */
  1867. if (name[strlen(name)-1] == ' ')
  1868. name[strlen(name)-1] = '\0';
  1869. } else {
  1870. strscpy(name, features->name, sizeof(name));
  1871. }
  1872. snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
  1873. name, suffix);
  1874. /* Append the device type to the name */
  1875. snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
  1876. "%s%s Pen", name, suffix);
  1877. snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
  1878. "%s%s Finger", name, suffix);
  1879. snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
  1880. "%s%s Pad", name, suffix);
  1881. }
  1882. static void wacom_release_resources(struct wacom *wacom)
  1883. {
  1884. struct hid_device *hdev = wacom->hdev;
  1885. if (!wacom->resources)
  1886. return;
  1887. devres_release_group(&hdev->dev, wacom);
  1888. wacom->resources = false;
  1889. wacom->wacom_wac.pen_input = NULL;
  1890. wacom->wacom_wac.touch_input = NULL;
  1891. wacom->wacom_wac.pad_input = NULL;
  1892. }
  1893. static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
  1894. {
  1895. if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
  1896. wacom_wac->shared->type = wacom_wac->features.type;
  1897. wacom_wac->shared->touch_input = wacom_wac->touch_input;
  1898. }
  1899. if (wacom_wac->has_mute_touch_switch) {
  1900. wacom_wac->shared->has_mute_touch_switch = true;
  1901. /* Hardware touch switch may be off. Wait until
  1902. * we know the switch state to decide is_touch_on.
  1903. * Softkey state should be initialized to "on" to
  1904. * match historic default.
  1905. */
  1906. if (wacom_wac->is_soft_touch_switch)
  1907. wacom_wac->shared->is_touch_on = true;
  1908. }
  1909. if (wacom_wac->shared->has_mute_touch_switch &&
  1910. wacom_wac->shared->touch_input) {
  1911. set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
  1912. input_set_capability(wacom_wac->shared->touch_input, EV_SW,
  1913. SW_MUTE_DEVICE);
  1914. }
  1915. }
  1916. static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
  1917. {
  1918. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  1919. struct wacom_features *features = &wacom_wac->features;
  1920. struct hid_device *hdev = wacom->hdev;
  1921. int error;
  1922. unsigned int connect_mask = HID_CONNECT_HIDRAW;
  1923. features->pktlen = wacom_compute_pktlen(hdev);
  1924. if (features->pktlen > WACOM_PKGLEN_MAX)
  1925. return -EINVAL;
  1926. if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
  1927. return -ENOMEM;
  1928. wacom->resources = true;
  1929. error = wacom_allocate_inputs(wacom);
  1930. if (error)
  1931. goto fail;
  1932. /*
  1933. * Bamboo Pad has a generic hid handling for the Pen, and we switch it
  1934. * into debug mode for the touch part.
  1935. * We ignore the other interfaces.
  1936. */
  1937. if (features->type == BAMBOO_PAD) {
  1938. if (features->pktlen == WACOM_PKGLEN_PENABLED) {
  1939. features->type = HID_GENERIC;
  1940. } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
  1941. (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
  1942. error = -ENODEV;
  1943. goto fail;
  1944. }
  1945. }
  1946. /* set the default size in case we do not get them from hid */
  1947. wacom_set_default_phy(features);
  1948. /* Retrieve the physical and logical size for touch devices */
  1949. wacom_retrieve_hid_descriptor(hdev, features);
  1950. wacom_setup_device_quirks(wacom);
  1951. if (features->device_type == WACOM_DEVICETYPE_NONE &&
  1952. features->type != WIRELESS) {
  1953. error = features->type == HID_GENERIC ? -ENODEV : 0;
  1954. dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
  1955. hdev->name,
  1956. error ? "Ignoring" : "Assuming pen");
  1957. if (error)
  1958. goto fail;
  1959. features->device_type |= WACOM_DEVICETYPE_PEN;
  1960. }
  1961. wacom_calculate_res(features);
  1962. wacom_update_name(wacom, wireless ? " (WL)" : "");
  1963. /* pen only Bamboo neither support touch nor pad */
  1964. if ((features->type == BAMBOO_PEN) &&
  1965. ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
  1966. (features->device_type & WACOM_DEVICETYPE_PAD))) {
  1967. error = -ENODEV;
  1968. goto fail;
  1969. }
  1970. error = wacom_add_shared_data(hdev);
  1971. if (error)
  1972. goto fail;
  1973. if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
  1974. (features->quirks & WACOM_QUIRK_BATTERY)) {
  1975. error = wacom_initialize_battery(wacom);
  1976. if (error)
  1977. goto fail;
  1978. }
  1979. error = wacom_register_inputs(wacom);
  1980. if (error)
  1981. goto fail;
  1982. if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
  1983. error = wacom_initialize_leds(wacom);
  1984. if (error)
  1985. goto fail;
  1986. error = wacom_initialize_remotes(wacom);
  1987. if (error)
  1988. goto fail;
  1989. }
  1990. if (features->type == HID_GENERIC)
  1991. connect_mask |= HID_CONNECT_DRIVER;
  1992. /* Regular HID work starts now */
  1993. error = hid_hw_start(hdev, connect_mask);
  1994. if (error) {
  1995. hid_err(hdev, "hw start failed\n");
  1996. goto fail;
  1997. }
  1998. if (!wireless) {
  1999. /* Note that if query fails it is not a hard failure */
  2000. wacom_query_tablet_data(wacom);
  2001. }
  2002. /* touch only Bamboo doesn't support pen */
  2003. if ((features->type == BAMBOO_TOUCH) &&
  2004. (features->device_type & WACOM_DEVICETYPE_PEN)) {
  2005. cancel_delayed_work_sync(&wacom->init_work);
  2006. _wacom_query_tablet_data(wacom);
  2007. error = -ENODEV;
  2008. goto fail_quirks;
  2009. }
  2010. if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR) {
  2011. error = hid_hw_open(hdev);
  2012. if (error) {
  2013. hid_err(hdev, "hw open failed\n");
  2014. goto fail_quirks;
  2015. }
  2016. }
  2017. wacom_set_shared_values(wacom_wac);
  2018. devres_close_group(&hdev->dev, wacom);
  2019. return 0;
  2020. fail_quirks:
  2021. hid_hw_stop(hdev);
  2022. fail:
  2023. wacom_release_resources(wacom);
  2024. return error;
  2025. }
  2026. static void wacom_wireless_work(struct work_struct *work)
  2027. {
  2028. struct wacom *wacom = container_of(work, struct wacom, wireless_work);
  2029. struct usb_device *usbdev = wacom->usbdev;
  2030. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  2031. struct hid_device *hdev1, *hdev2;
  2032. struct wacom *wacom1, *wacom2;
  2033. struct wacom_wac *wacom_wac1, *wacom_wac2;
  2034. int error;
  2035. /*
  2036. * Regardless if this is a disconnect or a new tablet,
  2037. * remove any existing input and battery devices.
  2038. */
  2039. wacom_destroy_battery(wacom);
  2040. if (!usbdev)
  2041. return;
  2042. /* Stylus interface */
  2043. hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
  2044. wacom1 = hid_get_drvdata(hdev1);
  2045. wacom_wac1 = &(wacom1->wacom_wac);
  2046. wacom_release_resources(wacom1);
  2047. /* Touch interface */
  2048. hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
  2049. wacom2 = hid_get_drvdata(hdev2);
  2050. wacom_wac2 = &(wacom2->wacom_wac);
  2051. wacom_release_resources(wacom2);
  2052. if (wacom_wac->pid == 0) {
  2053. hid_info(wacom->hdev, "wireless tablet disconnected\n");
  2054. } else {
  2055. const struct hid_device_id *id = wacom_ids;
  2056. hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
  2057. wacom_wac->pid);
  2058. while (id->bus) {
  2059. if (id->vendor == USB_VENDOR_ID_WACOM &&
  2060. id->product == wacom_wac->pid)
  2061. break;
  2062. id++;
  2063. }
  2064. if (!id->bus) {
  2065. hid_info(wacom->hdev, "ignoring unknown PID.\n");
  2066. return;
  2067. }
  2068. /* Stylus interface */
  2069. wacom_wac1->features =
  2070. *((struct wacom_features *)id->driver_data);
  2071. wacom_wac1->pid = wacom_wac->pid;
  2072. hid_hw_stop(hdev1);
  2073. error = wacom_parse_and_register(wacom1, true);
  2074. if (error)
  2075. goto fail;
  2076. /* Touch interface */
  2077. if (wacom_wac1->features.touch_max ||
  2078. (wacom_wac1->features.type >= INTUOSHT &&
  2079. wacom_wac1->features.type <= BAMBOO_PT)) {
  2080. wacom_wac2->features =
  2081. *((struct wacom_features *)id->driver_data);
  2082. wacom_wac2->pid = wacom_wac->pid;
  2083. hid_hw_stop(hdev2);
  2084. error = wacom_parse_and_register(wacom2, true);
  2085. if (error)
  2086. goto fail;
  2087. }
  2088. strscpy(wacom_wac->name, wacom_wac1->name,
  2089. sizeof(wacom_wac->name));
  2090. error = wacom_initialize_battery(wacom);
  2091. if (error)
  2092. goto fail;
  2093. }
  2094. return;
  2095. fail:
  2096. wacom_release_resources(wacom1);
  2097. wacom_release_resources(wacom2);
  2098. return;
  2099. }
  2100. static void wacom_remote_destroy_battery(struct wacom *wacom, int index)
  2101. {
  2102. struct wacom_remote *remote = wacom->remote;
  2103. if (remote->remotes[index].battery.battery) {
  2104. devres_release_group(&wacom->hdev->dev,
  2105. &remote->remotes[index].battery.bat_desc);
  2106. remote->remotes[index].battery.battery = NULL;
  2107. remote->remotes[index].active_time = 0;
  2108. }
  2109. }
  2110. static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
  2111. {
  2112. struct wacom_remote *remote = wacom->remote;
  2113. u32 serial = remote->remotes[index].serial;
  2114. int i;
  2115. unsigned long flags;
  2116. for (i = 0; i < WACOM_MAX_REMOTES; i++) {
  2117. if (remote->remotes[i].serial == serial) {
  2118. spin_lock_irqsave(&remote->remote_lock, flags);
  2119. remote->remotes[i].registered = false;
  2120. spin_unlock_irqrestore(&remote->remote_lock, flags);
  2121. wacom_remote_destroy_battery(wacom, i);
  2122. if (remote->remotes[i].group.name)
  2123. devres_release_group(&wacom->hdev->dev,
  2124. &remote->remotes[i]);
  2125. remote->remotes[i].serial = 0;
  2126. remote->remotes[i].group.name = NULL;
  2127. wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
  2128. }
  2129. }
  2130. }
  2131. static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
  2132. unsigned int index)
  2133. {
  2134. struct wacom_remote *remote = wacom->remote;
  2135. struct device *dev = &wacom->hdev->dev;
  2136. int error, k;
  2137. /* A remote can pair more than once with an EKR,
  2138. * check to make sure this serial isn't already paired.
  2139. */
  2140. for (k = 0; k < WACOM_MAX_REMOTES; k++) {
  2141. if (remote->remotes[k].serial == serial)
  2142. break;
  2143. }
  2144. if (k < WACOM_MAX_REMOTES) {
  2145. remote->remotes[index].serial = serial;
  2146. return 0;
  2147. }
  2148. if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
  2149. return -ENOMEM;
  2150. error = wacom_remote_create_attr_group(wacom, serial, index);
  2151. if (error)
  2152. goto fail;
  2153. remote->remotes[index].input = wacom_allocate_input(wacom);
  2154. if (!remote->remotes[index].input) {
  2155. error = -ENOMEM;
  2156. goto fail;
  2157. }
  2158. remote->remotes[index].input->uniq = remote->remotes[index].group.name;
  2159. remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
  2160. if (!remote->remotes[index].input->name) {
  2161. error = -EINVAL;
  2162. goto fail;
  2163. }
  2164. error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
  2165. &wacom->wacom_wac);
  2166. if (error)
  2167. goto fail;
  2168. remote->remotes[index].serial = serial;
  2169. error = input_register_device(remote->remotes[index].input);
  2170. if (error)
  2171. goto fail;
  2172. error = wacom_led_groups_alloc_and_register_one(
  2173. &remote->remotes[index].input->dev,
  2174. wacom, index, 3, true);
  2175. if (error)
  2176. goto fail;
  2177. remote->remotes[index].registered = true;
  2178. devres_close_group(dev, &remote->remotes[index]);
  2179. return 0;
  2180. fail:
  2181. devres_release_group(dev, &remote->remotes[index]);
  2182. remote->remotes[index].serial = 0;
  2183. return error;
  2184. }
  2185. static int wacom_remote_attach_battery(struct wacom *wacom, int index)
  2186. {
  2187. struct wacom_remote *remote = wacom->remote;
  2188. int error;
  2189. if (!remote->remotes[index].registered)
  2190. return 0;
  2191. if (remote->remotes[index].battery.battery)
  2192. return 0;
  2193. if (!remote->remotes[index].active_time)
  2194. return 0;
  2195. if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
  2196. return 0;
  2197. error = __wacom_initialize_battery(wacom,
  2198. &wacom->remote->remotes[index].battery);
  2199. if (error)
  2200. return error;
  2201. return 0;
  2202. }
  2203. static void wacom_remote_work(struct work_struct *work)
  2204. {
  2205. struct wacom *wacom = container_of(work, struct wacom, remote_work);
  2206. struct wacom_remote *remote = wacom->remote;
  2207. ktime_t kt = ktime_get();
  2208. struct wacom_remote_data data;
  2209. unsigned long flags;
  2210. unsigned int count;
  2211. u32 serial;
  2212. int i;
  2213. spin_lock_irqsave(&remote->remote_lock, flags);
  2214. count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
  2215. if (count != sizeof(data)) {
  2216. hid_err(wacom->hdev,
  2217. "workitem triggered without status available\n");
  2218. spin_unlock_irqrestore(&remote->remote_lock, flags);
  2219. return;
  2220. }
  2221. if (!kfifo_is_empty(&remote->remote_fifo))
  2222. wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
  2223. spin_unlock_irqrestore(&remote->remote_lock, flags);
  2224. for (i = 0; i < WACOM_MAX_REMOTES; i++) {
  2225. serial = data.remote[i].serial;
  2226. if (data.remote[i].connected) {
  2227. if (kt - remote->remotes[i].active_time > WACOM_REMOTE_BATTERY_TIMEOUT
  2228. && remote->remotes[i].active_time != 0)
  2229. wacom_remote_destroy_battery(wacom, i);
  2230. if (remote->remotes[i].serial == serial) {
  2231. wacom_remote_attach_battery(wacom, i);
  2232. continue;
  2233. }
  2234. if (remote->remotes[i].serial)
  2235. wacom_remote_destroy_one(wacom, i);
  2236. wacom_remote_create_one(wacom, serial, i);
  2237. } else if (remote->remotes[i].serial) {
  2238. wacom_remote_destroy_one(wacom, i);
  2239. }
  2240. }
  2241. }
  2242. static void wacom_mode_change_work(struct work_struct *work)
  2243. {
  2244. struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
  2245. struct wacom_shared *shared = wacom->wacom_wac.shared;
  2246. struct wacom *wacom1 = NULL;
  2247. struct wacom *wacom2 = NULL;
  2248. bool is_direct = wacom->wacom_wac.is_direct_mode;
  2249. int error = 0;
  2250. if (shared->pen) {
  2251. wacom1 = hid_get_drvdata(shared->pen);
  2252. wacom_release_resources(wacom1);
  2253. hid_hw_stop(wacom1->hdev);
  2254. wacom1->wacom_wac.has_mode_change = true;
  2255. wacom1->wacom_wac.is_direct_mode = is_direct;
  2256. }
  2257. if (shared->touch) {
  2258. wacom2 = hid_get_drvdata(shared->touch);
  2259. wacom_release_resources(wacom2);
  2260. hid_hw_stop(wacom2->hdev);
  2261. wacom2->wacom_wac.has_mode_change = true;
  2262. wacom2->wacom_wac.is_direct_mode = is_direct;
  2263. }
  2264. if (wacom1) {
  2265. error = wacom_parse_and_register(wacom1, false);
  2266. if (error)
  2267. return;
  2268. }
  2269. if (wacom2) {
  2270. error = wacom_parse_and_register(wacom2, false);
  2271. if (error)
  2272. return;
  2273. }
  2274. return;
  2275. }
  2276. static int wacom_probe(struct hid_device *hdev,
  2277. const struct hid_device_id *id)
  2278. {
  2279. struct wacom *wacom;
  2280. struct wacom_wac *wacom_wac;
  2281. struct wacom_features *features;
  2282. int error;
  2283. if (!id->driver_data)
  2284. return -EINVAL;
  2285. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  2286. /* hid-core sets this quirk for the boot interface */
  2287. hdev->quirks &= ~HID_QUIRK_NOGET;
  2288. wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
  2289. if (!wacom)
  2290. return -ENOMEM;
  2291. hid_set_drvdata(hdev, wacom);
  2292. wacom->hdev = hdev;
  2293. wacom_wac = &wacom->wacom_wac;
  2294. wacom_wac->features = *((struct wacom_features *)id->driver_data);
  2295. features = &wacom_wac->features;
  2296. if (features->check_for_hid_type && features->hid_type != hdev->type)
  2297. return -ENODEV;
  2298. error = wacom_devm_kfifo_alloc(wacom);
  2299. if (error)
  2300. return error;
  2301. wacom_wac->hid_data.inputmode = -1;
  2302. wacom_wac->mode_report = -1;
  2303. if (hid_is_usb(hdev)) {
  2304. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  2305. struct usb_device *dev = interface_to_usbdev(intf);
  2306. wacom->usbdev = dev;
  2307. wacom->intf = intf;
  2308. }
  2309. mutex_init(&wacom->lock);
  2310. INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
  2311. INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
  2312. INIT_WORK(&wacom->battery_work, wacom_battery_work);
  2313. INIT_WORK(&wacom->remote_work, wacom_remote_work);
  2314. INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
  2315. timer_setup(&wacom->idleprox_timer, &wacom_idleprox_timeout, TIMER_DEFERRABLE);
  2316. /* ask for the report descriptor to be loaded by HID */
  2317. error = hid_parse(hdev);
  2318. if (error) {
  2319. hid_err(hdev, "parse failed\n");
  2320. return error;
  2321. }
  2322. if (features->type == BOOTLOADER) {
  2323. hid_warn(hdev, "Using device in hidraw-only mode");
  2324. return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  2325. }
  2326. error = wacom_parse_and_register(wacom, false);
  2327. if (error)
  2328. return error;
  2329. if (hdev->bus == BUS_BLUETOOTH) {
  2330. error = device_create_file(&hdev->dev, &dev_attr_speed);
  2331. if (error)
  2332. hid_warn(hdev,
  2333. "can't create sysfs speed attribute err: %d\n",
  2334. error);
  2335. }
  2336. wacom_wac->probe_complete = true;
  2337. return 0;
  2338. }
  2339. static void wacom_remove(struct hid_device *hdev)
  2340. {
  2341. struct wacom *wacom = hid_get_drvdata(hdev);
  2342. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  2343. struct wacom_features *features = &wacom_wac->features;
  2344. if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
  2345. hid_hw_close(hdev);
  2346. hid_hw_stop(hdev);
  2347. cancel_delayed_work_sync(&wacom->init_work);
  2348. cancel_work_sync(&wacom->wireless_work);
  2349. cancel_work_sync(&wacom->battery_work);
  2350. cancel_work_sync(&wacom->remote_work);
  2351. cancel_work_sync(&wacom->mode_change_work);
  2352. del_timer_sync(&wacom->idleprox_timer);
  2353. if (hdev->bus == BUS_BLUETOOTH)
  2354. device_remove_file(&hdev->dev, &dev_attr_speed);
  2355. /* make sure we don't trigger the LEDs */
  2356. wacom_led_groups_release(wacom);
  2357. if (wacom->wacom_wac.features.type != REMOTE)
  2358. wacom_release_resources(wacom);
  2359. }
  2360. #ifdef CONFIG_PM
  2361. static int wacom_resume(struct hid_device *hdev)
  2362. {
  2363. struct wacom *wacom = hid_get_drvdata(hdev);
  2364. mutex_lock(&wacom->lock);
  2365. /* switch to wacom mode first */
  2366. _wacom_query_tablet_data(wacom);
  2367. wacom_led_control(wacom);
  2368. mutex_unlock(&wacom->lock);
  2369. return 0;
  2370. }
  2371. static int wacom_reset_resume(struct hid_device *hdev)
  2372. {
  2373. return wacom_resume(hdev);
  2374. }
  2375. #endif /* CONFIG_PM */
  2376. static struct hid_driver wacom_driver = {
  2377. .name = "wacom",
  2378. .id_table = wacom_ids,
  2379. .probe = wacom_probe,
  2380. .remove = wacom_remove,
  2381. .report = wacom_wac_report,
  2382. #ifdef CONFIG_PM
  2383. .resume = wacom_resume,
  2384. .reset_resume = wacom_reset_resume,
  2385. #endif
  2386. .raw_event = wacom_raw_event,
  2387. };
  2388. module_hid_driver(wacom_driver);
  2389. MODULE_VERSION(DRIVER_VERSION);
  2390. MODULE_AUTHOR(DRIVER_AUTHOR);
  2391. MODULE_DESCRIPTION(DRIVER_DESC);
  2392. MODULE_LICENSE("GPL");