wacom_sys.c 74 KB

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