wacom_sys.c 40 KB

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