wacom_sys.c 72 KB

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