wacom_sys.c 37 KB

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