wacom_sys.c 41 KB

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