wacom_sys.c 42 KB

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