wacom_sys.c 40 KB

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