wacom_sys.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  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 DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
  23. #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
  24. static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
  25. size_t size, unsigned int retries)
  26. {
  27. int retval;
  28. do {
  29. retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
  30. HID_REQ_GET_REPORT);
  31. } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
  32. if (retval < 0)
  33. hid_err(hdev, "wacom_get_report: ran out of retries "
  34. "(last error = %d)\n", retval);
  35. return retval;
  36. }
  37. static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
  38. size_t size, unsigned int retries)
  39. {
  40. int retval;
  41. do {
  42. retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
  43. HID_REQ_SET_REPORT);
  44. } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
  45. if (retval < 0)
  46. hid_err(hdev, "wacom_set_report: ran out of retries "
  47. "(last error = %d)\n", retval);
  48. return retval;
  49. }
  50. static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
  51. u8 *raw_data, int size)
  52. {
  53. struct wacom *wacom = hid_get_drvdata(hdev);
  54. if (size > WACOM_PKGLEN_MAX)
  55. return 1;
  56. memcpy(wacom->wacom_wac.data, raw_data, size);
  57. wacom_wac_irq(&wacom->wacom_wac, size);
  58. return 0;
  59. }
  60. static int wacom_open(struct input_dev *dev)
  61. {
  62. struct wacom *wacom = input_get_drvdata(dev);
  63. return hid_hw_open(wacom->hdev);
  64. }
  65. static void wacom_close(struct input_dev *dev)
  66. {
  67. struct wacom *wacom = input_get_drvdata(dev);
  68. hid_hw_close(wacom->hdev);
  69. }
  70. /*
  71. * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
  72. */
  73. static int wacom_calc_hid_res(int logical_extents, int physical_extents,
  74. unsigned unit, int exponent)
  75. {
  76. struct hid_field field = {
  77. .logical_maximum = logical_extents,
  78. .physical_maximum = physical_extents,
  79. .unit = unit,
  80. .unit_exponent = exponent,
  81. };
  82. return hidinput_calc_abs_res(&field, ABS_X);
  83. }
  84. static void wacom_feature_mapping(struct hid_device *hdev,
  85. struct hid_field *field, struct hid_usage *usage)
  86. {
  87. struct wacom *wacom = hid_get_drvdata(hdev);
  88. struct wacom_features *features = &wacom->wacom_wac.features;
  89. struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
  90. u8 *data;
  91. int ret;
  92. switch (usage->hid) {
  93. case HID_DG_CONTACTMAX:
  94. /* leave touch_max as is if predefined */
  95. if (!features->touch_max) {
  96. /* read manually */
  97. data = kzalloc(2, GFP_KERNEL);
  98. if (!data)
  99. break;
  100. data[0] = field->report->id;
  101. ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
  102. data, 2, WAC_CMD_RETRIES);
  103. if (ret == 2) {
  104. features->touch_max = data[1];
  105. } else {
  106. features->touch_max = 16;
  107. hid_warn(hdev, "wacom_feature_mapping: "
  108. "could not get HID_DG_CONTACTMAX, "
  109. "defaulting to %d\n",
  110. features->touch_max);
  111. }
  112. kfree(data);
  113. }
  114. break;
  115. case HID_DG_INPUTMODE:
  116. /* Ignore if value index is out of bounds. */
  117. if (usage->usage_index >= field->report_count) {
  118. dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
  119. break;
  120. }
  121. hid_data->inputmode = field->report->id;
  122. hid_data->inputmode_index = usage->usage_index;
  123. break;
  124. }
  125. }
  126. /*
  127. * Interface Descriptor of wacom devices can be incomplete and
  128. * inconsistent so wacom_features table is used to store stylus
  129. * device's packet lengths, various maximum values, and tablet
  130. * resolution based on product ID's.
  131. *
  132. * For devices that contain 2 interfaces, wacom_features table is
  133. * inaccurate for the touch interface. Since the Interface Descriptor
  134. * for touch interfaces has pretty complete data, this function exists
  135. * to query tablet for this missing information instead of hard coding in
  136. * an additional table.
  137. *
  138. * A typical Interface Descriptor for a stylus will contain a
  139. * boot mouse application collection that is not of interest and this
  140. * function will ignore it.
  141. *
  142. * It also contains a digitizer application collection that also is not
  143. * of interest since any information it contains would be duplicate
  144. * of what is in wacom_features. Usually it defines a report of an array
  145. * of bytes that could be used as max length of the stylus packet returned.
  146. * If it happens to define a Digitizer-Stylus Physical Collection then
  147. * the X and Y logical values contain valid data but it is ignored.
  148. *
  149. * A typical Interface Descriptor for a touch interface will contain a
  150. * Digitizer-Finger Physical Collection which will define both logical
  151. * X/Y maximum as well as the physical size of tablet. Since touch
  152. * interfaces haven't supported pressure or distance, this is enough
  153. * information to override invalid values in the wacom_features table.
  154. *
  155. * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
  156. * data. We deal with them after returning from this function.
  157. */
  158. static void wacom_usage_mapping(struct hid_device *hdev,
  159. struct hid_field *field, struct hid_usage *usage)
  160. {
  161. struct wacom *wacom = hid_get_drvdata(hdev);
  162. struct wacom_features *features = &wacom->wacom_wac.features;
  163. bool finger = WACOM_FINGER_FIELD(field);
  164. bool pen = WACOM_PEN_FIELD(field);
  165. /*
  166. * Requiring Stylus Usage will ignore boot mouse
  167. * X/Y values and some cases of invalid Digitizer X/Y
  168. * values commonly reported.
  169. */
  170. if (pen)
  171. features->device_type |= WACOM_DEVICETYPE_PEN;
  172. else if (finger)
  173. features->device_type |= WACOM_DEVICETYPE_TOUCH;
  174. else
  175. return;
  176. /*
  177. * Bamboo models do not support HID_DG_CONTACTMAX.
  178. * And, Bamboo Pen only descriptor contains touch.
  179. */
  180. if (features->type != BAMBOO_PT) {
  181. /* ISDv4 touch devices at least supports one touch point */
  182. if (finger && !features->touch_max)
  183. features->touch_max = 1;
  184. }
  185. switch (usage->hid) {
  186. case HID_GD_X:
  187. features->x_max = field->logical_maximum;
  188. if (finger) {
  189. features->x_phy = field->physical_maximum;
  190. if (features->type != BAMBOO_PT) {
  191. features->unit = field->unit;
  192. features->unitExpo = field->unit_exponent;
  193. }
  194. }
  195. break;
  196. case HID_GD_Y:
  197. features->y_max = field->logical_maximum;
  198. if (finger) {
  199. features->y_phy = field->physical_maximum;
  200. if (features->type != BAMBOO_PT) {
  201. features->unit = field->unit;
  202. features->unitExpo = field->unit_exponent;
  203. }
  204. }
  205. break;
  206. case HID_DG_TIPPRESSURE:
  207. if (pen)
  208. features->pressure_max = field->logical_maximum;
  209. break;
  210. }
  211. if (features->type == HID_GENERIC)
  212. wacom_wac_usage_mapping(hdev, field, usage);
  213. }
  214. static void wacom_post_parse_hid(struct hid_device *hdev,
  215. struct wacom_features *features)
  216. {
  217. struct wacom *wacom = hid_get_drvdata(hdev);
  218. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  219. if (features->type == HID_GENERIC) {
  220. /* Any last-minute generic device setup */
  221. if (features->touch_max > 1) {
  222. input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
  223. INPUT_MT_DIRECT);
  224. }
  225. }
  226. }
  227. static void wacom_parse_hid(struct hid_device *hdev,
  228. struct wacom_features *features)
  229. {
  230. struct hid_report_enum *rep_enum;
  231. struct hid_report *hreport;
  232. int i, j;
  233. /* check features first */
  234. rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
  235. list_for_each_entry(hreport, &rep_enum->report_list, list) {
  236. for (i = 0; i < hreport->maxfield; i++) {
  237. /* Ignore if report count is out of bounds. */
  238. if (hreport->field[i]->report_count < 1)
  239. continue;
  240. for (j = 0; j < hreport->field[i]->maxusage; j++) {
  241. wacom_feature_mapping(hdev, hreport->field[i],
  242. hreport->field[i]->usage + j);
  243. }
  244. }
  245. }
  246. /* now check the input usages */
  247. rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
  248. list_for_each_entry(hreport, &rep_enum->report_list, list) {
  249. if (!hreport->maxfield)
  250. continue;
  251. for (i = 0; i < hreport->maxfield; i++)
  252. for (j = 0; j < hreport->field[i]->maxusage; j++)
  253. wacom_usage_mapping(hdev, hreport->field[i],
  254. hreport->field[i]->usage + j);
  255. }
  256. wacom_post_parse_hid(hdev, features);
  257. }
  258. static int wacom_hid_set_device_mode(struct hid_device *hdev)
  259. {
  260. struct wacom *wacom = hid_get_drvdata(hdev);
  261. struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
  262. struct hid_report *r;
  263. struct hid_report_enum *re;
  264. if (hid_data->inputmode < 0)
  265. return 0;
  266. re = &(hdev->report_enum[HID_FEATURE_REPORT]);
  267. r = re->report_id_hash[hid_data->inputmode];
  268. if (r) {
  269. r->field[0]->value[hid_data->inputmode_index] = 2;
  270. hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
  271. }
  272. return 0;
  273. }
  274. static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
  275. int length, int mode)
  276. {
  277. unsigned char *rep_data;
  278. int error = -ENOMEM, limit = 0;
  279. rep_data = kzalloc(length, GFP_KERNEL);
  280. if (!rep_data)
  281. return error;
  282. do {
  283. rep_data[0] = report_id;
  284. rep_data[1] = mode;
  285. error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
  286. length, 1);
  287. if (error >= 0)
  288. error = wacom_get_report(hdev, HID_FEATURE_REPORT,
  289. rep_data, length, 1);
  290. } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
  291. kfree(rep_data);
  292. return error < 0 ? error : 0;
  293. }
  294. static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
  295. struct wacom_features *features)
  296. {
  297. struct wacom *wacom = hid_get_drvdata(hdev);
  298. int ret;
  299. u8 rep_data[2];
  300. switch (features->type) {
  301. case GRAPHIRE_BT:
  302. rep_data[0] = 0x03;
  303. rep_data[1] = 0x00;
  304. ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
  305. 3);
  306. if (ret >= 0) {
  307. rep_data[0] = speed == 0 ? 0x05 : 0x06;
  308. rep_data[1] = 0x00;
  309. ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
  310. rep_data, 2, 3);
  311. if (ret >= 0) {
  312. wacom->wacom_wac.bt_high_speed = speed;
  313. return 0;
  314. }
  315. }
  316. /*
  317. * Note that if the raw queries fail, it's not a hard failure
  318. * and it is safe to continue
  319. */
  320. hid_warn(hdev, "failed to poke device, command %d, err %d\n",
  321. rep_data[0], ret);
  322. break;
  323. case INTUOS4WL:
  324. if (speed == 1)
  325. wacom->wacom_wac.bt_features &= ~0x20;
  326. else
  327. wacom->wacom_wac.bt_features |= 0x20;
  328. rep_data[0] = 0x03;
  329. rep_data[1] = wacom->wacom_wac.bt_features;
  330. ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
  331. 1);
  332. if (ret >= 0)
  333. wacom->wacom_wac.bt_high_speed = speed;
  334. break;
  335. }
  336. return 0;
  337. }
  338. /*
  339. * Switch the tablet into its most-capable mode. Wacom tablets are
  340. * typically configured to power-up in a mode which sends mouse-like
  341. * reports to the OS. To get absolute position, pressure data, etc.
  342. * from the tablet, it is necessary to switch the tablet out of this
  343. * mode and into one which sends the full range of tablet data.
  344. */
  345. static int wacom_query_tablet_data(struct hid_device *hdev,
  346. struct wacom_features *features)
  347. {
  348. if (hdev->bus == BUS_BLUETOOTH)
  349. return wacom_bt_query_tablet_data(hdev, 1, features);
  350. if (features->type == HID_GENERIC)
  351. return wacom_hid_set_device_mode(hdev);
  352. if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
  353. if (features->type > TABLETPC) {
  354. /* MT Tablet PC touch */
  355. return wacom_set_device_mode(hdev, 3, 4, 4);
  356. }
  357. else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
  358. return wacom_set_device_mode(hdev, 18, 3, 2);
  359. }
  360. else if (features->type == WACOM_27QHDT) {
  361. return wacom_set_device_mode(hdev, 131, 3, 2);
  362. }
  363. else if (features->type == BAMBOO_PAD) {
  364. return wacom_set_device_mode(hdev, 2, 2, 2);
  365. }
  366. } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
  367. if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
  368. return wacom_set_device_mode(hdev, 2, 2, 2);
  369. }
  370. }
  371. return 0;
  372. }
  373. static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
  374. struct wacom_features *features)
  375. {
  376. struct wacom *wacom = hid_get_drvdata(hdev);
  377. struct usb_interface *intf = wacom->intf;
  378. /* default features */
  379. features->x_fuzz = 4;
  380. features->y_fuzz = 4;
  381. features->pressure_fuzz = 0;
  382. features->distance_fuzz = 0;
  383. /*
  384. * The wireless device HID is basic and layout conflicts with
  385. * other tablets (monitor and touch interface can look like pen).
  386. * Skip the query for this type and modify defaults based on
  387. * interface number.
  388. */
  389. if (features->type == WIRELESS) {
  390. if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
  391. features->device_type = WACOM_DEVICETYPE_NONE;
  392. } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
  393. features->device_type |= WACOM_DEVICETYPE_TOUCH;
  394. features->pktlen = WACOM_PKGLEN_BBTOUCH3;
  395. }
  396. }
  397. wacom_parse_hid(hdev, features);
  398. }
  399. struct wacom_hdev_data {
  400. struct list_head list;
  401. struct kref kref;
  402. struct hid_device *dev;
  403. struct wacom_shared shared;
  404. };
  405. static LIST_HEAD(wacom_udev_list);
  406. static DEFINE_MUTEX(wacom_udev_list_lock);
  407. static bool wacom_are_sibling(struct hid_device *hdev,
  408. struct hid_device *sibling)
  409. {
  410. struct wacom *wacom = hid_get_drvdata(hdev);
  411. struct wacom_features *features = &wacom->wacom_wac.features;
  412. int vid = features->oVid;
  413. int pid = features->oPid;
  414. int n1,n2;
  415. if (vid == 0 && pid == 0) {
  416. vid = hdev->vendor;
  417. pid = hdev->product;
  418. }
  419. if (vid != sibling->vendor || pid != sibling->product)
  420. return false;
  421. /* Compare the physical path. */
  422. n1 = strrchr(hdev->phys, '.') - hdev->phys;
  423. n2 = strrchr(sibling->phys, '.') - sibling->phys;
  424. if (n1 != n2 || n1 <= 0 || n2 <= 0)
  425. return false;
  426. return !strncmp(hdev->phys, sibling->phys, n1);
  427. }
  428. static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
  429. {
  430. struct wacom_hdev_data *data;
  431. list_for_each_entry(data, &wacom_udev_list, list) {
  432. if (wacom_are_sibling(hdev, data->dev)) {
  433. kref_get(&data->kref);
  434. return data;
  435. }
  436. }
  437. return NULL;
  438. }
  439. static int wacom_add_shared_data(struct hid_device *hdev)
  440. {
  441. struct wacom *wacom = hid_get_drvdata(hdev);
  442. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  443. struct wacom_hdev_data *data;
  444. int retval = 0;
  445. mutex_lock(&wacom_udev_list_lock);
  446. data = wacom_get_hdev_data(hdev);
  447. if (!data) {
  448. data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
  449. if (!data) {
  450. retval = -ENOMEM;
  451. goto out;
  452. }
  453. kref_init(&data->kref);
  454. data->dev = hdev;
  455. list_add_tail(&data->list, &wacom_udev_list);
  456. }
  457. wacom_wac->shared = &data->shared;
  458. if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
  459. wacom_wac->shared->touch = hdev;
  460. else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
  461. wacom_wac->shared->pen = hdev;
  462. out:
  463. mutex_unlock(&wacom_udev_list_lock);
  464. return retval;
  465. }
  466. static void wacom_release_shared_data(struct kref *kref)
  467. {
  468. struct wacom_hdev_data *data =
  469. container_of(kref, struct wacom_hdev_data, kref);
  470. mutex_lock(&wacom_udev_list_lock);
  471. list_del(&data->list);
  472. mutex_unlock(&wacom_udev_list_lock);
  473. kfree(data);
  474. }
  475. static void wacom_remove_shared_data(struct wacom *wacom)
  476. {
  477. struct wacom_hdev_data *data;
  478. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  479. if (wacom_wac->shared) {
  480. data = container_of(wacom_wac->shared, struct wacom_hdev_data,
  481. shared);
  482. if (wacom_wac->shared->touch == wacom->hdev)
  483. wacom_wac->shared->touch = NULL;
  484. else if (wacom_wac->shared->pen == wacom->hdev)
  485. wacom_wac->shared->pen = NULL;
  486. kref_put(&data->kref, wacom_release_shared_data);
  487. wacom_wac->shared = NULL;
  488. }
  489. }
  490. static int wacom_led_control(struct wacom *wacom)
  491. {
  492. unsigned char *buf;
  493. int retval;
  494. unsigned char report_id = WAC_CMD_LED_CONTROL;
  495. int buf_size = 9;
  496. if (wacom->wacom_wac.pid) { /* wireless connected */
  497. report_id = WAC_CMD_WL_LED_CONTROL;
  498. buf_size = 13;
  499. }
  500. buf = kzalloc(buf_size, GFP_KERNEL);
  501. if (!buf)
  502. return -ENOMEM;
  503. if (wacom->wacom_wac.features.type >= INTUOS5S &&
  504. wacom->wacom_wac.features.type <= INTUOSPL) {
  505. /*
  506. * Touch Ring and crop mark LED luminance may take on
  507. * one of four values:
  508. * 0 = Low; 1 = Medium; 2 = High; 3 = Off
  509. */
  510. int ring_led = wacom->led.select[0] & 0x03;
  511. int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
  512. int crop_lum = 0;
  513. unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
  514. buf[0] = report_id;
  515. if (wacom->wacom_wac.pid) {
  516. wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
  517. buf, buf_size, WAC_CMD_RETRIES);
  518. buf[0] = report_id;
  519. buf[4] = led_bits;
  520. } else
  521. buf[1] = led_bits;
  522. }
  523. else {
  524. int led = wacom->led.select[0] | 0x4;
  525. if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
  526. wacom->wacom_wac.features.type == WACOM_24HD)
  527. led |= (wacom->led.select[1] << 4) | 0x40;
  528. buf[0] = report_id;
  529. buf[1] = led;
  530. buf[2] = wacom->led.llv;
  531. buf[3] = wacom->led.hlv;
  532. buf[4] = wacom->led.img_lum;
  533. }
  534. retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
  535. WAC_CMD_RETRIES);
  536. kfree(buf);
  537. return retval;
  538. }
  539. static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
  540. const unsigned len, const void *img)
  541. {
  542. unsigned char *buf;
  543. int i, retval;
  544. const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
  545. buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
  546. if (!buf)
  547. return -ENOMEM;
  548. /* Send 'start' command */
  549. buf[0] = WAC_CMD_ICON_START;
  550. buf[1] = 1;
  551. retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
  552. WAC_CMD_RETRIES);
  553. if (retval < 0)
  554. goto out;
  555. buf[0] = xfer_id;
  556. buf[1] = button_id & 0x07;
  557. for (i = 0; i < 4; i++) {
  558. buf[2] = i;
  559. memcpy(buf + 3, img + i * chunk_len, chunk_len);
  560. retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
  561. buf, chunk_len + 3, WAC_CMD_RETRIES);
  562. if (retval < 0)
  563. break;
  564. }
  565. /* Send 'stop' */
  566. buf[0] = WAC_CMD_ICON_START;
  567. buf[1] = 0;
  568. wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
  569. WAC_CMD_RETRIES);
  570. out:
  571. kfree(buf);
  572. return retval;
  573. }
  574. static ssize_t wacom_led_select_store(struct device *dev, int set_id,
  575. const char *buf, size_t count)
  576. {
  577. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  578. struct wacom *wacom = hid_get_drvdata(hdev);
  579. unsigned int id;
  580. int err;
  581. err = kstrtouint(buf, 10, &id);
  582. if (err)
  583. return err;
  584. mutex_lock(&wacom->lock);
  585. wacom->led.select[set_id] = id & 0x3;
  586. err = wacom_led_control(wacom);
  587. mutex_unlock(&wacom->lock);
  588. return err < 0 ? err : count;
  589. }
  590. #define DEVICE_LED_SELECT_ATTR(SET_ID) \
  591. static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
  592. struct device_attribute *attr, const char *buf, size_t count) \
  593. { \
  594. return wacom_led_select_store(dev, SET_ID, buf, count); \
  595. } \
  596. static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
  597. struct device_attribute *attr, char *buf) \
  598. { \
  599. struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
  600. struct wacom *wacom = hid_get_drvdata(hdev); \
  601. return scnprintf(buf, PAGE_SIZE, "%d\n", \
  602. wacom->led.select[SET_ID]); \
  603. } \
  604. static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
  605. wacom_led##SET_ID##_select_show, \
  606. wacom_led##SET_ID##_select_store)
  607. DEVICE_LED_SELECT_ATTR(0);
  608. DEVICE_LED_SELECT_ATTR(1);
  609. static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
  610. const char *buf, size_t count)
  611. {
  612. unsigned int value;
  613. int err;
  614. err = kstrtouint(buf, 10, &value);
  615. if (err)
  616. return err;
  617. mutex_lock(&wacom->lock);
  618. *dest = value & 0x7f;
  619. err = wacom_led_control(wacom);
  620. mutex_unlock(&wacom->lock);
  621. return err < 0 ? err : count;
  622. }
  623. #define DEVICE_LUMINANCE_ATTR(name, field) \
  624. static ssize_t wacom_##name##_luminance_store(struct device *dev, \
  625. struct device_attribute *attr, const char *buf, size_t count) \
  626. { \
  627. struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
  628. struct wacom *wacom = hid_get_drvdata(hdev); \
  629. \
  630. return wacom_luminance_store(wacom, &wacom->led.field, \
  631. buf, count); \
  632. } \
  633. static ssize_t wacom_##name##_luminance_show(struct device *dev, \
  634. struct device_attribute *attr, char *buf) \
  635. { \
  636. struct wacom *wacom = dev_get_drvdata(dev); \
  637. return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
  638. } \
  639. static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
  640. wacom_##name##_luminance_show, \
  641. wacom_##name##_luminance_store)
  642. DEVICE_LUMINANCE_ATTR(status0, llv);
  643. DEVICE_LUMINANCE_ATTR(status1, hlv);
  644. DEVICE_LUMINANCE_ATTR(buttons, img_lum);
  645. static ssize_t wacom_button_image_store(struct device *dev, int button_id,
  646. const char *buf, size_t count)
  647. {
  648. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  649. struct wacom *wacom = hid_get_drvdata(hdev);
  650. int err;
  651. unsigned len;
  652. u8 xfer_id;
  653. if (hdev->bus == BUS_BLUETOOTH) {
  654. len = 256;
  655. xfer_id = WAC_CMD_ICON_BT_XFER;
  656. } else {
  657. len = 1024;
  658. xfer_id = WAC_CMD_ICON_XFER;
  659. }
  660. if (count != len)
  661. return -EINVAL;
  662. mutex_lock(&wacom->lock);
  663. err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
  664. mutex_unlock(&wacom->lock);
  665. return err < 0 ? err : count;
  666. }
  667. #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
  668. static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
  669. struct device_attribute *attr, const char *buf, size_t count) \
  670. { \
  671. return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
  672. } \
  673. static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
  674. NULL, wacom_btnimg##BUTTON_ID##_store)
  675. DEVICE_BTNIMG_ATTR(0);
  676. DEVICE_BTNIMG_ATTR(1);
  677. DEVICE_BTNIMG_ATTR(2);
  678. DEVICE_BTNIMG_ATTR(3);
  679. DEVICE_BTNIMG_ATTR(4);
  680. DEVICE_BTNIMG_ATTR(5);
  681. DEVICE_BTNIMG_ATTR(6);
  682. DEVICE_BTNIMG_ATTR(7);
  683. static struct attribute *cintiq_led_attrs[] = {
  684. &dev_attr_status_led0_select.attr,
  685. &dev_attr_status_led1_select.attr,
  686. NULL
  687. };
  688. static struct attribute_group cintiq_led_attr_group = {
  689. .name = "wacom_led",
  690. .attrs = cintiq_led_attrs,
  691. };
  692. static struct attribute *intuos4_led_attrs[] = {
  693. &dev_attr_status0_luminance.attr,
  694. &dev_attr_status1_luminance.attr,
  695. &dev_attr_status_led0_select.attr,
  696. &dev_attr_buttons_luminance.attr,
  697. &dev_attr_button0_rawimg.attr,
  698. &dev_attr_button1_rawimg.attr,
  699. &dev_attr_button2_rawimg.attr,
  700. &dev_attr_button3_rawimg.attr,
  701. &dev_attr_button4_rawimg.attr,
  702. &dev_attr_button5_rawimg.attr,
  703. &dev_attr_button6_rawimg.attr,
  704. &dev_attr_button7_rawimg.attr,
  705. NULL
  706. };
  707. static struct attribute_group intuos4_led_attr_group = {
  708. .name = "wacom_led",
  709. .attrs = intuos4_led_attrs,
  710. };
  711. static struct attribute *intuos5_led_attrs[] = {
  712. &dev_attr_status0_luminance.attr,
  713. &dev_attr_status_led0_select.attr,
  714. NULL
  715. };
  716. static struct attribute_group intuos5_led_attr_group = {
  717. .name = "wacom_led",
  718. .attrs = intuos5_led_attrs,
  719. };
  720. static int wacom_initialize_leds(struct wacom *wacom)
  721. {
  722. int error;
  723. if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
  724. return 0;
  725. /* Initialize default values */
  726. switch (wacom->wacom_wac.features.type) {
  727. case INTUOS4S:
  728. case INTUOS4:
  729. case INTUOS4WL:
  730. case INTUOS4L:
  731. wacom->led.select[0] = 0;
  732. wacom->led.select[1] = 0;
  733. wacom->led.llv = 10;
  734. wacom->led.hlv = 20;
  735. wacom->led.img_lum = 10;
  736. error = sysfs_create_group(&wacom->hdev->dev.kobj,
  737. &intuos4_led_attr_group);
  738. break;
  739. case WACOM_24HD:
  740. case WACOM_21UX2:
  741. wacom->led.select[0] = 0;
  742. wacom->led.select[1] = 0;
  743. wacom->led.llv = 0;
  744. wacom->led.hlv = 0;
  745. wacom->led.img_lum = 0;
  746. error = sysfs_create_group(&wacom->hdev->dev.kobj,
  747. &cintiq_led_attr_group);
  748. break;
  749. case INTUOS5S:
  750. case INTUOS5:
  751. case INTUOS5L:
  752. case INTUOSPS:
  753. case INTUOSPM:
  754. case INTUOSPL:
  755. wacom->led.select[0] = 0;
  756. wacom->led.select[1] = 0;
  757. wacom->led.llv = 32;
  758. wacom->led.hlv = 0;
  759. wacom->led.img_lum = 0;
  760. error = sysfs_create_group(&wacom->hdev->dev.kobj,
  761. &intuos5_led_attr_group);
  762. break;
  763. default:
  764. return 0;
  765. }
  766. if (error) {
  767. hid_err(wacom->hdev,
  768. "cannot create sysfs group err: %d\n", error);
  769. return error;
  770. }
  771. wacom_led_control(wacom);
  772. wacom->led_initialized = true;
  773. return 0;
  774. }
  775. static void wacom_destroy_leds(struct wacom *wacom)
  776. {
  777. if (!wacom->led_initialized)
  778. return;
  779. if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
  780. return;
  781. wacom->led_initialized = false;
  782. switch (wacom->wacom_wac.features.type) {
  783. case INTUOS4S:
  784. case INTUOS4:
  785. case INTUOS4WL:
  786. case INTUOS4L:
  787. sysfs_remove_group(&wacom->hdev->dev.kobj,
  788. &intuos4_led_attr_group);
  789. break;
  790. case WACOM_24HD:
  791. case WACOM_21UX2:
  792. sysfs_remove_group(&wacom->hdev->dev.kobj,
  793. &cintiq_led_attr_group);
  794. break;
  795. case INTUOS5S:
  796. case INTUOS5:
  797. case INTUOS5L:
  798. case INTUOSPS:
  799. case INTUOSPM:
  800. case INTUOSPL:
  801. sysfs_remove_group(&wacom->hdev->dev.kobj,
  802. &intuos5_led_attr_group);
  803. break;
  804. }
  805. }
  806. static enum power_supply_property wacom_battery_props[] = {
  807. POWER_SUPPLY_PROP_PRESENT,
  808. POWER_SUPPLY_PROP_STATUS,
  809. POWER_SUPPLY_PROP_SCOPE,
  810. POWER_SUPPLY_PROP_CAPACITY
  811. };
  812. static enum power_supply_property wacom_ac_props[] = {
  813. POWER_SUPPLY_PROP_PRESENT,
  814. POWER_SUPPLY_PROP_ONLINE,
  815. POWER_SUPPLY_PROP_SCOPE,
  816. };
  817. static int wacom_battery_get_property(struct power_supply *psy,
  818. enum power_supply_property psp,
  819. union power_supply_propval *val)
  820. {
  821. struct wacom *wacom = power_supply_get_drvdata(psy);
  822. int ret = 0;
  823. switch (psp) {
  824. case POWER_SUPPLY_PROP_PRESENT:
  825. val->intval = wacom->wacom_wac.bat_connected;
  826. break;
  827. case POWER_SUPPLY_PROP_SCOPE:
  828. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  829. break;
  830. case POWER_SUPPLY_PROP_CAPACITY:
  831. val->intval =
  832. wacom->wacom_wac.battery_capacity;
  833. break;
  834. case POWER_SUPPLY_PROP_STATUS:
  835. if (wacom->wacom_wac.bat_charging)
  836. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  837. else if (wacom->wacom_wac.battery_capacity == 100 &&
  838. wacom->wacom_wac.ps_connected)
  839. val->intval = POWER_SUPPLY_STATUS_FULL;
  840. else if (wacom->wacom_wac.ps_connected)
  841. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  842. else
  843. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  844. break;
  845. default:
  846. ret = -EINVAL;
  847. break;
  848. }
  849. return ret;
  850. }
  851. static int wacom_ac_get_property(struct power_supply *psy,
  852. enum power_supply_property psp,
  853. union power_supply_propval *val)
  854. {
  855. struct wacom *wacom = power_supply_get_drvdata(psy);
  856. int ret = 0;
  857. switch (psp) {
  858. case POWER_SUPPLY_PROP_PRESENT:
  859. /* fall through */
  860. case POWER_SUPPLY_PROP_ONLINE:
  861. val->intval = wacom->wacom_wac.ps_connected;
  862. break;
  863. case POWER_SUPPLY_PROP_SCOPE:
  864. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  865. break;
  866. default:
  867. ret = -EINVAL;
  868. break;
  869. }
  870. return ret;
  871. }
  872. static int wacom_initialize_battery(struct wacom *wacom)
  873. {
  874. static atomic_t battery_no = ATOMIC_INIT(0);
  875. struct power_supply_config psy_cfg = { .drv_data = wacom, };
  876. unsigned long n;
  877. if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
  878. struct power_supply_desc *bat_desc = &wacom->battery_desc;
  879. struct power_supply_desc *ac_desc = &wacom->ac_desc;
  880. n = atomic_inc_return(&battery_no) - 1;
  881. bat_desc->properties = wacom_battery_props;
  882. bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
  883. bat_desc->get_property = wacom_battery_get_property;
  884. sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
  885. bat_desc->name = wacom->wacom_wac.bat_name;
  886. bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  887. bat_desc->use_for_apm = 0;
  888. ac_desc->properties = wacom_ac_props;
  889. ac_desc->num_properties = ARRAY_SIZE(wacom_ac_props);
  890. ac_desc->get_property = wacom_ac_get_property;
  891. sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
  892. ac_desc->name = wacom->wacom_wac.ac_name;
  893. ac_desc->type = POWER_SUPPLY_TYPE_MAINS;
  894. ac_desc->use_for_apm = 0;
  895. wacom->battery = power_supply_register(&wacom->hdev->dev,
  896. &wacom->battery_desc, &psy_cfg);
  897. if (IS_ERR(wacom->battery))
  898. return PTR_ERR(wacom->battery);
  899. power_supply_powers(wacom->battery, &wacom->hdev->dev);
  900. wacom->ac = power_supply_register(&wacom->hdev->dev,
  901. &wacom->ac_desc,
  902. &psy_cfg);
  903. if (IS_ERR(wacom->ac)) {
  904. power_supply_unregister(wacom->battery);
  905. return PTR_ERR(wacom->ac);
  906. }
  907. power_supply_powers(wacom->ac, &wacom->hdev->dev);
  908. }
  909. return 0;
  910. }
  911. static void wacom_destroy_battery(struct wacom *wacom)
  912. {
  913. if (wacom->battery) {
  914. power_supply_unregister(wacom->battery);
  915. wacom->battery = NULL;
  916. power_supply_unregister(wacom->ac);
  917. wacom->ac = NULL;
  918. }
  919. }
  920. static ssize_t wacom_show_speed(struct device *dev,
  921. struct device_attribute
  922. *attr, char *buf)
  923. {
  924. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  925. struct wacom *wacom = hid_get_drvdata(hdev);
  926. return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
  927. }
  928. static ssize_t wacom_store_speed(struct device *dev,
  929. struct device_attribute *attr,
  930. const char *buf, size_t count)
  931. {
  932. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  933. struct wacom *wacom = hid_get_drvdata(hdev);
  934. u8 new_speed;
  935. if (kstrtou8(buf, 0, &new_speed))
  936. return -EINVAL;
  937. if (new_speed != 0 && new_speed != 1)
  938. return -EINVAL;
  939. wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
  940. return count;
  941. }
  942. static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
  943. wacom_show_speed, wacom_store_speed);
  944. static struct input_dev *wacom_allocate_input(struct wacom *wacom)
  945. {
  946. struct input_dev *input_dev;
  947. struct hid_device *hdev = wacom->hdev;
  948. struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
  949. input_dev = input_allocate_device();
  950. if (!input_dev)
  951. return NULL;
  952. input_dev->name = wacom_wac->pen_name;
  953. input_dev->phys = hdev->phys;
  954. input_dev->dev.parent = &hdev->dev;
  955. input_dev->open = wacom_open;
  956. input_dev->close = wacom_close;
  957. input_dev->uniq = hdev->uniq;
  958. input_dev->id.bustype = hdev->bus;
  959. input_dev->id.vendor = hdev->vendor;
  960. input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
  961. input_dev->id.version = hdev->version;
  962. input_set_drvdata(input_dev, wacom);
  963. return input_dev;
  964. }
  965. static void wacom_free_inputs(struct wacom *wacom)
  966. {
  967. struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
  968. if (wacom_wac->pen_input)
  969. input_free_device(wacom_wac->pen_input);
  970. if (wacom_wac->touch_input)
  971. input_free_device(wacom_wac->touch_input);
  972. if (wacom_wac->pad_input)
  973. input_free_device(wacom_wac->pad_input);
  974. wacom_wac->pen_input = NULL;
  975. wacom_wac->touch_input = NULL;
  976. wacom_wac->pad_input = NULL;
  977. }
  978. static int wacom_allocate_inputs(struct wacom *wacom)
  979. {
  980. struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
  981. struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
  982. pen_input_dev = wacom_allocate_input(wacom);
  983. touch_input_dev = wacom_allocate_input(wacom);
  984. pad_input_dev = wacom_allocate_input(wacom);
  985. if (!pen_input_dev || !touch_input_dev || !pad_input_dev) {
  986. wacom_free_inputs(wacom);
  987. return -ENOMEM;
  988. }
  989. wacom_wac->pen_input = pen_input_dev;
  990. wacom_wac->touch_input = touch_input_dev;
  991. wacom_wac->touch_input->name = wacom_wac->touch_name;
  992. wacom_wac->pad_input = pad_input_dev;
  993. wacom_wac->pad_input->name = wacom_wac->pad_name;
  994. return 0;
  995. }
  996. static void wacom_clean_inputs(struct wacom *wacom)
  997. {
  998. if (wacom->wacom_wac.pen_input) {
  999. if (wacom->wacom_wac.pen_registered)
  1000. input_unregister_device(wacom->wacom_wac.pen_input);
  1001. else
  1002. input_free_device(wacom->wacom_wac.pen_input);
  1003. }
  1004. if (wacom->wacom_wac.touch_input) {
  1005. if (wacom->wacom_wac.touch_registered)
  1006. input_unregister_device(wacom->wacom_wac.touch_input);
  1007. else
  1008. input_free_device(wacom->wacom_wac.touch_input);
  1009. }
  1010. if (wacom->wacom_wac.pad_input) {
  1011. if (wacom->wacom_wac.pad_registered)
  1012. input_unregister_device(wacom->wacom_wac.pad_input);
  1013. else
  1014. input_free_device(wacom->wacom_wac.pad_input);
  1015. }
  1016. wacom->wacom_wac.pen_input = NULL;
  1017. wacom->wacom_wac.touch_input = NULL;
  1018. wacom->wacom_wac.pad_input = NULL;
  1019. wacom_destroy_leds(wacom);
  1020. }
  1021. static int wacom_register_inputs(struct wacom *wacom)
  1022. {
  1023. struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
  1024. struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
  1025. int error = 0;
  1026. pen_input_dev = wacom_wac->pen_input;
  1027. touch_input_dev = wacom_wac->touch_input;
  1028. pad_input_dev = wacom_wac->pad_input;
  1029. if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
  1030. return -EINVAL;
  1031. error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
  1032. if (error) {
  1033. /* no pen in use on this interface */
  1034. input_free_device(pen_input_dev);
  1035. wacom_wac->pen_input = NULL;
  1036. pen_input_dev = NULL;
  1037. } else {
  1038. error = input_register_device(pen_input_dev);
  1039. if (error)
  1040. goto fail_register_pen_input;
  1041. wacom_wac->pen_registered = true;
  1042. }
  1043. error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
  1044. if (error) {
  1045. /* no touch in use on this interface */
  1046. input_free_device(touch_input_dev);
  1047. wacom_wac->touch_input = NULL;
  1048. touch_input_dev = NULL;
  1049. } else {
  1050. error = input_register_device(touch_input_dev);
  1051. if (error)
  1052. goto fail_register_touch_input;
  1053. wacom_wac->touch_registered = true;
  1054. }
  1055. error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
  1056. if (error) {
  1057. /* no pad in use on this interface */
  1058. input_free_device(pad_input_dev);
  1059. wacom_wac->pad_input = NULL;
  1060. pad_input_dev = NULL;
  1061. } else {
  1062. error = input_register_device(pad_input_dev);
  1063. if (error)
  1064. goto fail_register_pad_input;
  1065. wacom_wac->pad_registered = true;
  1066. error = wacom_initialize_leds(wacom);
  1067. if (error)
  1068. goto fail_leds;
  1069. }
  1070. return 0;
  1071. fail_leds:
  1072. input_unregister_device(pad_input_dev);
  1073. pad_input_dev = NULL;
  1074. wacom_wac->pad_registered = false;
  1075. fail_register_pad_input:
  1076. input_unregister_device(touch_input_dev);
  1077. wacom_wac->touch_input = NULL;
  1078. wacom_wac->touch_registered = false;
  1079. fail_register_touch_input:
  1080. input_unregister_device(pen_input_dev);
  1081. wacom_wac->pen_input = NULL;
  1082. wacom_wac->pen_registered = false;
  1083. fail_register_pen_input:
  1084. return error;
  1085. }
  1086. static void wacom_wireless_work(struct work_struct *work)
  1087. {
  1088. struct wacom *wacom = container_of(work, struct wacom, work);
  1089. struct usb_device *usbdev = wacom->usbdev;
  1090. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  1091. struct hid_device *hdev1, *hdev2;
  1092. struct wacom *wacom1, *wacom2;
  1093. struct wacom_wac *wacom_wac1, *wacom_wac2;
  1094. int error;
  1095. /*
  1096. * Regardless if this is a disconnect or a new tablet,
  1097. * remove any existing input and battery devices.
  1098. */
  1099. wacom_destroy_battery(wacom);
  1100. /* Stylus interface */
  1101. hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
  1102. wacom1 = hid_get_drvdata(hdev1);
  1103. wacom_wac1 = &(wacom1->wacom_wac);
  1104. wacom_clean_inputs(wacom1);
  1105. /* Touch interface */
  1106. hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
  1107. wacom2 = hid_get_drvdata(hdev2);
  1108. wacom_wac2 = &(wacom2->wacom_wac);
  1109. wacom_clean_inputs(wacom2);
  1110. if (wacom_wac->pid == 0) {
  1111. hid_info(wacom->hdev, "wireless tablet disconnected\n");
  1112. wacom_wac1->shared->type = 0;
  1113. } else {
  1114. const struct hid_device_id *id = wacom_ids;
  1115. hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
  1116. wacom_wac->pid);
  1117. while (id->bus) {
  1118. if (id->vendor == USB_VENDOR_ID_WACOM &&
  1119. id->product == wacom_wac->pid)
  1120. break;
  1121. id++;
  1122. }
  1123. if (!id->bus) {
  1124. hid_info(wacom->hdev, "ignoring unknown PID.\n");
  1125. return;
  1126. }
  1127. /* Stylus interface */
  1128. wacom_wac1->features =
  1129. *((struct wacom_features *)id->driver_data);
  1130. wacom_wac1->features.device_type |= WACOM_DEVICETYPE_PEN;
  1131. if (wacom_wac1->features.type != INTUOSHT &&
  1132. wacom_wac1->features.type != BAMBOO_PT)
  1133. wacom_wac1->features.device_type |= WACOM_DEVICETYPE_PAD;
  1134. snprintf(wacom_wac1->pen_name, WACOM_NAME_MAX, "%s (WL) Pen",
  1135. wacom_wac1->features.name);
  1136. snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
  1137. wacom_wac1->features.name);
  1138. wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
  1139. wacom_wac1->shared->type = wacom_wac1->features.type;
  1140. wacom_wac1->pid = wacom_wac->pid;
  1141. error = wacom_allocate_inputs(wacom1) ||
  1142. wacom_register_inputs(wacom1);
  1143. if (error)
  1144. goto fail;
  1145. /* Touch interface */
  1146. if (wacom_wac1->features.touch_max ||
  1147. wacom_wac1->features.type == INTUOSHT) {
  1148. wacom_wac2->features =
  1149. *((struct wacom_features *)id->driver_data);
  1150. wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
  1151. wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
  1152. snprintf(wacom_wac2->touch_name, WACOM_NAME_MAX,
  1153. "%s (WL) Finger",wacom_wac2->features.name);
  1154. snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
  1155. "%s (WL) Pad",wacom_wac2->features.name);
  1156. if (wacom_wac1->features.touch_max)
  1157. wacom_wac2->features.device_type |= WACOM_DEVICETYPE_TOUCH;
  1158. if (wacom_wac1->features.type == INTUOSHT ||
  1159. wacom_wac1->features.type == BAMBOO_PT)
  1160. wacom_wac2->features.device_type |= WACOM_DEVICETYPE_PAD;
  1161. wacom_wac2->pid = wacom_wac->pid;
  1162. error = wacom_allocate_inputs(wacom2) ||
  1163. wacom_register_inputs(wacom2);
  1164. if (error)
  1165. goto fail;
  1166. if (wacom_wac1->features.type == INTUOSHT &&
  1167. wacom_wac1->features.touch_max)
  1168. wacom_wac->shared->touch_input = wacom_wac2->touch_input;
  1169. }
  1170. error = wacom_initialize_battery(wacom);
  1171. if (error)
  1172. goto fail;
  1173. }
  1174. return;
  1175. fail:
  1176. wacom_clean_inputs(wacom1);
  1177. wacom_clean_inputs(wacom2);
  1178. return;
  1179. }
  1180. void wacom_battery_work(struct work_struct *work)
  1181. {
  1182. struct wacom *wacom = container_of(work, struct wacom, work);
  1183. if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
  1184. !wacom->battery) {
  1185. wacom_initialize_battery(wacom);
  1186. }
  1187. else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
  1188. wacom->battery) {
  1189. wacom_destroy_battery(wacom);
  1190. }
  1191. }
  1192. /*
  1193. * Not all devices report physical dimensions from HID.
  1194. * Compute the default from hardcoded logical dimension
  1195. * and resolution before driver overwrites them.
  1196. */
  1197. static void wacom_set_default_phy(struct wacom_features *features)
  1198. {
  1199. if (features->x_resolution) {
  1200. features->x_phy = (features->x_max * 100) /
  1201. features->x_resolution;
  1202. features->y_phy = (features->y_max * 100) /
  1203. features->y_resolution;
  1204. }
  1205. }
  1206. static void wacom_calculate_res(struct wacom_features *features)
  1207. {
  1208. /* set unit to "100th of a mm" for devices not reported by HID */
  1209. if (!features->unit) {
  1210. features->unit = 0x11;
  1211. features->unitExpo = -3;
  1212. }
  1213. features->x_resolution = wacom_calc_hid_res(features->x_max,
  1214. features->x_phy,
  1215. features->unit,
  1216. features->unitExpo);
  1217. features->y_resolution = wacom_calc_hid_res(features->y_max,
  1218. features->y_phy,
  1219. features->unit,
  1220. features->unitExpo);
  1221. }
  1222. static size_t wacom_compute_pktlen(struct hid_device *hdev)
  1223. {
  1224. struct hid_report_enum *report_enum;
  1225. struct hid_report *report;
  1226. size_t size = 0;
  1227. report_enum = hdev->report_enum + HID_INPUT_REPORT;
  1228. list_for_each_entry(report, &report_enum->report_list, list) {
  1229. size_t report_size = hid_report_len(report);
  1230. if (report_size > size)
  1231. size = report_size;
  1232. }
  1233. return size;
  1234. }
  1235. static void wacom_update_name(struct wacom *wacom)
  1236. {
  1237. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  1238. struct wacom_features *features = &wacom_wac->features;
  1239. char name[WACOM_NAME_MAX];
  1240. /* Generic devices name unspecified */
  1241. if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
  1242. if (strstr(wacom->hdev->name, "Wacom") ||
  1243. strstr(wacom->hdev->name, "wacom") ||
  1244. strstr(wacom->hdev->name, "WACOM")) {
  1245. /* name is in HID descriptor, use it */
  1246. strlcpy(name, wacom->hdev->name, sizeof(name));
  1247. /* strip out excess whitespaces */
  1248. while (1) {
  1249. char *gap = strstr(name, " ");
  1250. if (gap == NULL)
  1251. break;
  1252. /* shift everything including the terminator */
  1253. memmove(gap, gap+1, strlen(gap));
  1254. }
  1255. /* get rid of trailing whitespace */
  1256. if (name[strlen(name)-1] == ' ')
  1257. name[strlen(name)-1] = '\0';
  1258. } else {
  1259. /* no meaningful name retrieved. use product ID */
  1260. snprintf(name, sizeof(name),
  1261. "%s %X", features->name, wacom->hdev->product);
  1262. }
  1263. } else {
  1264. strlcpy(name, features->name, sizeof(name));
  1265. }
  1266. /* Append the device type to the name */
  1267. snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
  1268. "%s Pen", name);
  1269. snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
  1270. "%s Finger", name);
  1271. snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
  1272. "%s Pad", name);
  1273. }
  1274. static int wacom_probe(struct hid_device *hdev,
  1275. const struct hid_device_id *id)
  1276. {
  1277. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  1278. struct usb_device *dev = interface_to_usbdev(intf);
  1279. struct wacom *wacom;
  1280. struct wacom_wac *wacom_wac;
  1281. struct wacom_features *features;
  1282. int error;
  1283. unsigned int connect_mask = HID_CONNECT_HIDRAW;
  1284. if (!id->driver_data)
  1285. return -EINVAL;
  1286. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  1287. /* hid-core sets this quirk for the boot interface */
  1288. hdev->quirks &= ~HID_QUIRK_NOGET;
  1289. wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
  1290. if (!wacom)
  1291. return -ENOMEM;
  1292. hid_set_drvdata(hdev, wacom);
  1293. wacom->hdev = hdev;
  1294. /* ask for the report descriptor to be loaded by HID */
  1295. error = hid_parse(hdev);
  1296. if (error) {
  1297. hid_err(hdev, "parse failed\n");
  1298. goto fail_parse;
  1299. }
  1300. wacom_wac = &wacom->wacom_wac;
  1301. wacom_wac->features = *((struct wacom_features *)id->driver_data);
  1302. features = &wacom_wac->features;
  1303. features->pktlen = wacom_compute_pktlen(hdev);
  1304. if (features->pktlen > WACOM_PKGLEN_MAX) {
  1305. error = -EINVAL;
  1306. goto fail_pktlen;
  1307. }
  1308. if (features->check_for_hid_type && features->hid_type != hdev->type) {
  1309. error = -ENODEV;
  1310. goto fail_type;
  1311. }
  1312. wacom->usbdev = dev;
  1313. wacom->intf = intf;
  1314. mutex_init(&wacom->lock);
  1315. INIT_WORK(&wacom->work, wacom_wireless_work);
  1316. if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
  1317. error = wacom_allocate_inputs(wacom);
  1318. if (error)
  1319. goto fail_allocate_inputs;
  1320. }
  1321. /*
  1322. * Bamboo Pad has a generic hid handling for the Pen, and we switch it
  1323. * into debug mode for the touch part.
  1324. * We ignore the other interfaces.
  1325. */
  1326. if (features->type == BAMBOO_PAD) {
  1327. if (features->pktlen == WACOM_PKGLEN_PENABLED) {
  1328. features->type = HID_GENERIC;
  1329. } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
  1330. (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
  1331. error = -ENODEV;
  1332. goto fail_shared_data;
  1333. }
  1334. }
  1335. /* set the default size in case we do not get them from hid */
  1336. wacom_set_default_phy(features);
  1337. /* Retrieve the physical and logical size for touch devices */
  1338. wacom_retrieve_hid_descriptor(hdev, features);
  1339. wacom_setup_device_quirks(wacom);
  1340. if (features->device_type == WACOM_DEVICETYPE_NONE &&
  1341. features->type != WIRELESS) {
  1342. error = features->type == HID_GENERIC ? -ENODEV : 0;
  1343. dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
  1344. hdev->name,
  1345. error ? "Ignoring" : "Assuming pen");
  1346. if (error)
  1347. goto fail_shared_data;
  1348. features->device_type |= WACOM_DEVICETYPE_PEN;
  1349. }
  1350. wacom_calculate_res(features);
  1351. wacom_update_name(wacom);
  1352. error = wacom_add_shared_data(hdev);
  1353. if (error)
  1354. goto fail_shared_data;
  1355. if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
  1356. (features->quirks & WACOM_QUIRK_BATTERY)) {
  1357. error = wacom_initialize_battery(wacom);
  1358. if (error)
  1359. goto fail_battery;
  1360. }
  1361. if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
  1362. error = wacom_register_inputs(wacom);
  1363. if (error)
  1364. goto fail_register_inputs;
  1365. }
  1366. if (hdev->bus == BUS_BLUETOOTH) {
  1367. error = device_create_file(&hdev->dev, &dev_attr_speed);
  1368. if (error)
  1369. hid_warn(hdev,
  1370. "can't create sysfs speed attribute err: %d\n",
  1371. error);
  1372. }
  1373. if (features->type == HID_GENERIC)
  1374. connect_mask |= HID_CONNECT_DRIVER;
  1375. /* Regular HID work starts now */
  1376. error = hid_hw_start(hdev, connect_mask);
  1377. if (error) {
  1378. hid_err(hdev, "hw start failed\n");
  1379. goto fail_hw_start;
  1380. }
  1381. /* Note that if query fails it is not a hard failure */
  1382. wacom_query_tablet_data(hdev, features);
  1383. if (features->quirks & WACOM_QUIRK_MONITOR)
  1384. error = hid_hw_open(hdev);
  1385. if (wacom_wac->features.type == INTUOSHT &&
  1386. wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
  1387. wacom_wac->shared->touch_input = wacom_wac->touch_input;
  1388. }
  1389. return 0;
  1390. fail_hw_start:
  1391. if (hdev->bus == BUS_BLUETOOTH)
  1392. device_remove_file(&hdev->dev, &dev_attr_speed);
  1393. fail_register_inputs:
  1394. wacom_clean_inputs(wacom);
  1395. wacom_destroy_battery(wacom);
  1396. fail_battery:
  1397. wacom_remove_shared_data(wacom);
  1398. fail_shared_data:
  1399. wacom_clean_inputs(wacom);
  1400. fail_allocate_inputs:
  1401. fail_type:
  1402. fail_pktlen:
  1403. fail_parse:
  1404. kfree(wacom);
  1405. hid_set_drvdata(hdev, NULL);
  1406. return error;
  1407. }
  1408. static void wacom_remove(struct hid_device *hdev)
  1409. {
  1410. struct wacom *wacom = hid_get_drvdata(hdev);
  1411. hid_hw_stop(hdev);
  1412. cancel_work_sync(&wacom->work);
  1413. wacom_clean_inputs(wacom);
  1414. if (hdev->bus == BUS_BLUETOOTH)
  1415. device_remove_file(&hdev->dev, &dev_attr_speed);
  1416. wacom_destroy_battery(wacom);
  1417. wacom_remove_shared_data(wacom);
  1418. hid_set_drvdata(hdev, NULL);
  1419. kfree(wacom);
  1420. }
  1421. #ifdef CONFIG_PM
  1422. static int wacom_resume(struct hid_device *hdev)
  1423. {
  1424. struct wacom *wacom = hid_get_drvdata(hdev);
  1425. struct wacom_features *features = &wacom->wacom_wac.features;
  1426. mutex_lock(&wacom->lock);
  1427. /* switch to wacom mode first */
  1428. wacom_query_tablet_data(hdev, features);
  1429. wacom_led_control(wacom);
  1430. mutex_unlock(&wacom->lock);
  1431. return 0;
  1432. }
  1433. static int wacom_reset_resume(struct hid_device *hdev)
  1434. {
  1435. return wacom_resume(hdev);
  1436. }
  1437. #endif /* CONFIG_PM */
  1438. static struct hid_driver wacom_driver = {
  1439. .name = "wacom",
  1440. .id_table = wacom_ids,
  1441. .probe = wacom_probe,
  1442. .remove = wacom_remove,
  1443. .event = wacom_wac_event,
  1444. .report = wacom_wac_report,
  1445. #ifdef CONFIG_PM
  1446. .resume = wacom_resume,
  1447. .reset_resume = wacom_reset_resume,
  1448. #endif
  1449. .raw_event = wacom_raw_event,
  1450. };
  1451. module_hid_driver(wacom_driver);
  1452. MODULE_VERSION(DRIVER_VERSION);
  1453. MODULE_AUTHOR(DRIVER_AUTHOR);
  1454. MODULE_DESCRIPTION(DRIVER_DESC);
  1455. MODULE_LICENSE(DRIVER_LICENSE);