wacom_sys.c 62 KB

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