hid-rmi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * Copyright (c) 2013 Andrew Duggan <aduggan@synaptics.com>
  3. * Copyright (c) 2013 Synaptics Incorporated
  4. * Copyright (c) 2014 Benjamin Tissoires <benjamin.tissoires@gmail.com>
  5. * Copyright (c) 2014 Red Hat, Inc
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/hid.h>
  14. #include <linux/input.h>
  15. #include <linux/input/mt.h>
  16. #include <linux/module.h>
  17. #include <linux/pm.h>
  18. #include <linux/slab.h>
  19. #include <linux/wait.h>
  20. #include <linux/sched.h>
  21. #include "hid-ids.h"
  22. #define RMI_MOUSE_REPORT_ID 0x01 /* Mouse emulation Report */
  23. #define RMI_WRITE_REPORT_ID 0x09 /* Output Report */
  24. #define RMI_READ_ADDR_REPORT_ID 0x0a /* Output Report */
  25. #define RMI_READ_DATA_REPORT_ID 0x0b /* Input Report */
  26. #define RMI_ATTN_REPORT_ID 0x0c /* Input Report */
  27. #define RMI_SET_RMI_MODE_REPORT_ID 0x0f /* Feature Report */
  28. /* flags */
  29. #define RMI_READ_REQUEST_PENDING BIT(0)
  30. #define RMI_READ_DATA_PENDING BIT(1)
  31. #define RMI_STARTED BIT(2)
  32. enum rmi_mode_type {
  33. RMI_MODE_OFF = 0,
  34. RMI_MODE_ATTN_REPORTS = 1,
  35. RMI_MODE_NO_PACKED_ATTN_REPORTS = 2,
  36. };
  37. struct rmi_function {
  38. unsigned page; /* page of the function */
  39. u16 query_base_addr; /* base address for queries */
  40. u16 command_base_addr; /* base address for commands */
  41. u16 control_base_addr; /* base address for controls */
  42. u16 data_base_addr; /* base address for datas */
  43. unsigned int interrupt_base; /* cross-function interrupt number
  44. * (uniq in the device)*/
  45. unsigned int interrupt_count; /* number of interrupts */
  46. unsigned int report_size; /* size of a report */
  47. unsigned long irq_mask; /* mask of the interrupts
  48. * (to be applied against ATTN IRQ) */
  49. };
  50. /**
  51. * struct rmi_data - stores information for hid communication
  52. *
  53. * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
  54. * @page: Keeps track of the current virtual page
  55. *
  56. * @wait: Used for waiting for read data
  57. *
  58. * @writeReport: output buffer when writing RMI registers
  59. * @readReport: input buffer when reading RMI registers
  60. *
  61. * @input_report_size: size of an input report (advertised by HID)
  62. * @output_report_size: size of an output report (advertised by HID)
  63. *
  64. * @flags: flags for the current device (started, reading, etc...)
  65. *
  66. * @f11: placeholder of internal RMI function F11 description
  67. * @f30: placeholder of internal RMI function F30 description
  68. *
  69. * @max_fingers: maximum finger count reported by the device
  70. * @max_x: maximum x value reported by the device
  71. * @max_y: maximum y value reported by the device
  72. *
  73. * @gpio_led_count: count of GPIOs + LEDs reported by F30
  74. * @button_count: actual physical buttons count
  75. * @button_mask: button mask used to decode GPIO ATTN reports
  76. * @button_state_mask: pull state of the buttons
  77. *
  78. * @input: pointer to the kernel input device
  79. *
  80. * @reset_work: worker which will be called in case of a mouse report
  81. * @hdev: pointer to the struct hid_device
  82. */
  83. struct rmi_data {
  84. struct mutex page_mutex;
  85. int page;
  86. wait_queue_head_t wait;
  87. u8 *writeReport;
  88. u8 *readReport;
  89. int input_report_size;
  90. int output_report_size;
  91. unsigned long flags;
  92. struct rmi_function f11;
  93. struct rmi_function f30;
  94. unsigned int max_fingers;
  95. unsigned int max_x;
  96. unsigned int max_y;
  97. unsigned int x_size_mm;
  98. unsigned int y_size_mm;
  99. unsigned int gpio_led_count;
  100. unsigned int button_count;
  101. unsigned long button_mask;
  102. unsigned long button_state_mask;
  103. struct input_dev *input;
  104. struct work_struct reset_work;
  105. struct hid_device *hdev;
  106. };
  107. #define RMI_PAGE(addr) (((addr) >> 8) & 0xff)
  108. static int rmi_write_report(struct hid_device *hdev, u8 *report, int len);
  109. /**
  110. * rmi_set_page - Set RMI page
  111. * @hdev: The pointer to the hid_device struct
  112. * @page: The new page address.
  113. *
  114. * RMI devices have 16-bit addressing, but some of the physical
  115. * implementations (like SMBus) only have 8-bit addressing. So RMI implements
  116. * a page address at 0xff of every page so we can reliable page addresses
  117. * every 256 registers.
  118. *
  119. * The page_mutex lock must be held when this function is entered.
  120. *
  121. * Returns zero on success, non-zero on failure.
  122. */
  123. static int rmi_set_page(struct hid_device *hdev, u8 page)
  124. {
  125. struct rmi_data *data = hid_get_drvdata(hdev);
  126. int retval;
  127. data->writeReport[0] = RMI_WRITE_REPORT_ID;
  128. data->writeReport[1] = 1;
  129. data->writeReport[2] = 0xFF;
  130. data->writeReport[4] = page;
  131. retval = rmi_write_report(hdev, data->writeReport,
  132. data->output_report_size);
  133. if (retval != data->output_report_size) {
  134. dev_err(&hdev->dev,
  135. "%s: set page failed: %d.", __func__, retval);
  136. return retval;
  137. }
  138. data->page = page;
  139. return 0;
  140. }
  141. static int rmi_set_mode(struct hid_device *hdev, u8 mode)
  142. {
  143. int ret;
  144. u8 txbuf[2] = {RMI_SET_RMI_MODE_REPORT_ID, mode};
  145. ret = hid_hw_raw_request(hdev, RMI_SET_RMI_MODE_REPORT_ID, txbuf,
  146. sizeof(txbuf), HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  147. if (ret < 0) {
  148. dev_err(&hdev->dev, "unable to set rmi mode to %d (%d)\n", mode,
  149. ret);
  150. return ret;
  151. }
  152. return 0;
  153. }
  154. static int rmi_write_report(struct hid_device *hdev, u8 *report, int len)
  155. {
  156. int ret;
  157. ret = hid_hw_output_report(hdev, (void *)report, len);
  158. if (ret < 0) {
  159. dev_err(&hdev->dev, "failed to write hid report (%d)\n", ret);
  160. return ret;
  161. }
  162. return ret;
  163. }
  164. static int rmi_read_block(struct hid_device *hdev, u16 addr, void *buf,
  165. const int len)
  166. {
  167. struct rmi_data *data = hid_get_drvdata(hdev);
  168. int ret;
  169. int bytes_read;
  170. int bytes_needed;
  171. int retries;
  172. int read_input_count;
  173. mutex_lock(&data->page_mutex);
  174. if (RMI_PAGE(addr) != data->page) {
  175. ret = rmi_set_page(hdev, RMI_PAGE(addr));
  176. if (ret < 0)
  177. goto exit;
  178. }
  179. for (retries = 5; retries > 0; retries--) {
  180. data->writeReport[0] = RMI_READ_ADDR_REPORT_ID;
  181. data->writeReport[1] = 0; /* old 1 byte read count */
  182. data->writeReport[2] = addr & 0xFF;
  183. data->writeReport[3] = (addr >> 8) & 0xFF;
  184. data->writeReport[4] = len & 0xFF;
  185. data->writeReport[5] = (len >> 8) & 0xFF;
  186. set_bit(RMI_READ_REQUEST_PENDING, &data->flags);
  187. ret = rmi_write_report(hdev, data->writeReport,
  188. data->output_report_size);
  189. if (ret != data->output_report_size) {
  190. clear_bit(RMI_READ_REQUEST_PENDING, &data->flags);
  191. dev_err(&hdev->dev,
  192. "failed to write request output report (%d)\n",
  193. ret);
  194. goto exit;
  195. }
  196. bytes_read = 0;
  197. bytes_needed = len;
  198. while (bytes_read < len) {
  199. if (!wait_event_timeout(data->wait,
  200. test_bit(RMI_READ_DATA_PENDING, &data->flags),
  201. msecs_to_jiffies(1000))) {
  202. hid_warn(hdev, "%s: timeout elapsed\n",
  203. __func__);
  204. ret = -EAGAIN;
  205. break;
  206. }
  207. read_input_count = data->readReport[1];
  208. memcpy(buf + bytes_read, &data->readReport[2],
  209. read_input_count < bytes_needed ?
  210. read_input_count : bytes_needed);
  211. bytes_read += read_input_count;
  212. bytes_needed -= read_input_count;
  213. clear_bit(RMI_READ_DATA_PENDING, &data->flags);
  214. }
  215. if (ret >= 0) {
  216. ret = 0;
  217. break;
  218. }
  219. }
  220. exit:
  221. clear_bit(RMI_READ_REQUEST_PENDING, &data->flags);
  222. mutex_unlock(&data->page_mutex);
  223. return ret;
  224. }
  225. static inline int rmi_read(struct hid_device *hdev, u16 addr, void *buf)
  226. {
  227. return rmi_read_block(hdev, addr, buf, 1);
  228. }
  229. static void rmi_f11_process_touch(struct rmi_data *hdata, int slot,
  230. u8 finger_state, u8 *touch_data)
  231. {
  232. int x, y, wx, wy;
  233. int wide, major, minor;
  234. int z;
  235. input_mt_slot(hdata->input, slot);
  236. input_mt_report_slot_state(hdata->input, MT_TOOL_FINGER,
  237. finger_state == 0x01);
  238. if (finger_state == 0x01) {
  239. x = (touch_data[0] << 4) | (touch_data[2] & 0x0F);
  240. y = (touch_data[1] << 4) | (touch_data[2] >> 4);
  241. wx = touch_data[3] & 0x0F;
  242. wy = touch_data[3] >> 4;
  243. wide = (wx > wy);
  244. major = max(wx, wy);
  245. minor = min(wx, wy);
  246. z = touch_data[4];
  247. /* y is inverted */
  248. y = hdata->max_y - y;
  249. input_event(hdata->input, EV_ABS, ABS_MT_POSITION_X, x);
  250. input_event(hdata->input, EV_ABS, ABS_MT_POSITION_Y, y);
  251. input_event(hdata->input, EV_ABS, ABS_MT_ORIENTATION, wide);
  252. input_event(hdata->input, EV_ABS, ABS_MT_PRESSURE, z);
  253. input_event(hdata->input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
  254. input_event(hdata->input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
  255. }
  256. }
  257. static void rmi_reset_work(struct work_struct *work)
  258. {
  259. struct rmi_data *hdata = container_of(work, struct rmi_data,
  260. reset_work);
  261. /* switch the device to RMI if we receive a generic mouse report */
  262. rmi_set_mode(hdata->hdev, RMI_MODE_ATTN_REPORTS);
  263. }
  264. static inline int rmi_schedule_reset(struct hid_device *hdev)
  265. {
  266. struct rmi_data *hdata = hid_get_drvdata(hdev);
  267. return schedule_work(&hdata->reset_work);
  268. }
  269. static int rmi_f11_input_event(struct hid_device *hdev, u8 irq, u8 *data,
  270. int size)
  271. {
  272. struct rmi_data *hdata = hid_get_drvdata(hdev);
  273. int offset;
  274. int i;
  275. if (!(irq & hdata->f11.irq_mask) || size <= 0)
  276. return 0;
  277. offset = (hdata->max_fingers >> 2) + 1;
  278. for (i = 0; i < hdata->max_fingers; i++) {
  279. int fs_byte_position = i >> 2;
  280. int fs_bit_position = (i & 0x3) << 1;
  281. int finger_state = (data[fs_byte_position] >> fs_bit_position) &
  282. 0x03;
  283. int position = offset + 5 * i;
  284. if (position + 5 > size) {
  285. /* partial report, go on with what we received */
  286. printk_once(KERN_WARNING
  287. "%s %s: Detected incomplete finger report. Finger reports may occasionally get dropped on this platform.\n",
  288. dev_driver_string(&hdev->dev),
  289. dev_name(&hdev->dev));
  290. hid_dbg(hdev, "Incomplete finger report\n");
  291. break;
  292. }
  293. rmi_f11_process_touch(hdata, i, finger_state, &data[position]);
  294. }
  295. input_mt_sync_frame(hdata->input);
  296. input_sync(hdata->input);
  297. return hdata->f11.report_size;
  298. }
  299. static int rmi_f30_input_event(struct hid_device *hdev, u8 irq, u8 *data,
  300. int size)
  301. {
  302. struct rmi_data *hdata = hid_get_drvdata(hdev);
  303. int i;
  304. int button = 0;
  305. bool value;
  306. if (!(irq & hdata->f30.irq_mask))
  307. return 0;
  308. if (size < (int)hdata->f30.report_size) {
  309. hid_warn(hdev, "Click Button pressed, but the click data is missing\n");
  310. return 0;
  311. }
  312. for (i = 0; i < hdata->gpio_led_count; i++) {
  313. if (test_bit(i, &hdata->button_mask)) {
  314. value = (data[i / 8] >> (i & 0x07)) & BIT(0);
  315. if (test_bit(i, &hdata->button_state_mask))
  316. value = !value;
  317. input_event(hdata->input, EV_KEY, BTN_LEFT + button++,
  318. value);
  319. }
  320. }
  321. return hdata->f30.report_size;
  322. }
  323. static int rmi_input_event(struct hid_device *hdev, u8 *data, int size)
  324. {
  325. struct rmi_data *hdata = hid_get_drvdata(hdev);
  326. unsigned long irq_mask = 0;
  327. unsigned index = 2;
  328. if (!(test_bit(RMI_STARTED, &hdata->flags)))
  329. return 0;
  330. irq_mask |= hdata->f11.irq_mask;
  331. irq_mask |= hdata->f30.irq_mask;
  332. if (data[1] & ~irq_mask)
  333. hid_dbg(hdev, "unknown intr source:%02lx %s:%d\n",
  334. data[1] & ~irq_mask, __FILE__, __LINE__);
  335. if (hdata->f11.interrupt_base < hdata->f30.interrupt_base) {
  336. index += rmi_f11_input_event(hdev, data[1], &data[index],
  337. size - index);
  338. index += rmi_f30_input_event(hdev, data[1], &data[index],
  339. size - index);
  340. } else {
  341. index += rmi_f30_input_event(hdev, data[1], &data[index],
  342. size - index);
  343. index += rmi_f11_input_event(hdev, data[1], &data[index],
  344. size - index);
  345. }
  346. return 1;
  347. }
  348. static int rmi_read_data_event(struct hid_device *hdev, u8 *data, int size)
  349. {
  350. struct rmi_data *hdata = hid_get_drvdata(hdev);
  351. if (!test_bit(RMI_READ_REQUEST_PENDING, &hdata->flags)) {
  352. hid_dbg(hdev, "no read request pending\n");
  353. return 0;
  354. }
  355. memcpy(hdata->readReport, data, size < hdata->input_report_size ?
  356. size : hdata->input_report_size);
  357. set_bit(RMI_READ_DATA_PENDING, &hdata->flags);
  358. wake_up(&hdata->wait);
  359. return 1;
  360. }
  361. static int rmi_check_sanity(struct hid_device *hdev, u8 *data, int size)
  362. {
  363. int valid_size = size;
  364. /*
  365. * On the Dell XPS 13 9333, the bus sometimes get confused and fills
  366. * the report with a sentinel value "ff". Synaptics told us that such
  367. * behavior does not comes from the touchpad itself, so we filter out
  368. * such reports here.
  369. */
  370. while ((data[valid_size - 1] == 0xff) && valid_size > 0)
  371. valid_size--;
  372. return valid_size;
  373. }
  374. static int rmi_raw_event(struct hid_device *hdev,
  375. struct hid_report *report, u8 *data, int size)
  376. {
  377. size = rmi_check_sanity(hdev, data, size);
  378. if (size < 2)
  379. return 0;
  380. switch (data[0]) {
  381. case RMI_READ_DATA_REPORT_ID:
  382. return rmi_read_data_event(hdev, data, size);
  383. case RMI_ATTN_REPORT_ID:
  384. return rmi_input_event(hdev, data, size);
  385. case RMI_MOUSE_REPORT_ID:
  386. rmi_schedule_reset(hdev);
  387. break;
  388. }
  389. return 0;
  390. }
  391. #ifdef CONFIG_PM
  392. static int rmi_post_reset(struct hid_device *hdev)
  393. {
  394. return rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
  395. }
  396. static int rmi_post_resume(struct hid_device *hdev)
  397. {
  398. return rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
  399. }
  400. #endif /* CONFIG_PM */
  401. #define RMI4_MAX_PAGE 0xff
  402. #define RMI4_PAGE_SIZE 0x0100
  403. #define PDT_START_SCAN_LOCATION 0x00e9
  404. #define PDT_END_SCAN_LOCATION 0x0005
  405. #define RMI4_END_OF_PDT(id) ((id) == 0x00 || (id) == 0xff)
  406. struct pdt_entry {
  407. u8 query_base_addr:8;
  408. u8 command_base_addr:8;
  409. u8 control_base_addr:8;
  410. u8 data_base_addr:8;
  411. u8 interrupt_source_count:3;
  412. u8 bits3and4:2;
  413. u8 function_version:2;
  414. u8 bit7:1;
  415. u8 function_number:8;
  416. } __attribute__((__packed__));
  417. static inline unsigned long rmi_gen_mask(unsigned irq_base, unsigned irq_count)
  418. {
  419. return GENMASK(irq_count + irq_base - 1, irq_base);
  420. }
  421. static void rmi_register_function(struct rmi_data *data,
  422. struct pdt_entry *pdt_entry, int page, unsigned interrupt_count)
  423. {
  424. struct rmi_function *f = NULL;
  425. u16 page_base = page << 8;
  426. switch (pdt_entry->function_number) {
  427. case 0x11:
  428. f = &data->f11;
  429. break;
  430. case 0x30:
  431. f = &data->f30;
  432. break;
  433. }
  434. if (f) {
  435. f->page = page;
  436. f->query_base_addr = page_base | pdt_entry->query_base_addr;
  437. f->command_base_addr = page_base | pdt_entry->command_base_addr;
  438. f->control_base_addr = page_base | pdt_entry->control_base_addr;
  439. f->data_base_addr = page_base | pdt_entry->data_base_addr;
  440. f->interrupt_base = interrupt_count;
  441. f->interrupt_count = pdt_entry->interrupt_source_count;
  442. f->irq_mask = rmi_gen_mask(f->interrupt_base,
  443. f->interrupt_count);
  444. }
  445. }
  446. static int rmi_scan_pdt(struct hid_device *hdev)
  447. {
  448. struct rmi_data *data = hid_get_drvdata(hdev);
  449. struct pdt_entry entry;
  450. int page;
  451. bool page_has_function;
  452. int i;
  453. int retval;
  454. int interrupt = 0;
  455. u16 page_start, pdt_start , pdt_end;
  456. hid_info(hdev, "Scanning PDT...\n");
  457. for (page = 0; (page <= RMI4_MAX_PAGE); page++) {
  458. page_start = RMI4_PAGE_SIZE * page;
  459. pdt_start = page_start + PDT_START_SCAN_LOCATION;
  460. pdt_end = page_start + PDT_END_SCAN_LOCATION;
  461. page_has_function = false;
  462. for (i = pdt_start; i >= pdt_end; i -= sizeof(entry)) {
  463. retval = rmi_read_block(hdev, i, &entry, sizeof(entry));
  464. if (retval) {
  465. hid_err(hdev,
  466. "Read of PDT entry at %#06x failed.\n",
  467. i);
  468. goto error_exit;
  469. }
  470. if (RMI4_END_OF_PDT(entry.function_number))
  471. break;
  472. page_has_function = true;
  473. hid_info(hdev, "Found F%02X on page %#04x\n",
  474. entry.function_number, page);
  475. rmi_register_function(data, &entry, page, interrupt);
  476. interrupt += entry.interrupt_source_count;
  477. }
  478. if (!page_has_function)
  479. break;
  480. }
  481. hid_info(hdev, "%s: Done with PDT scan.\n", __func__);
  482. retval = 0;
  483. error_exit:
  484. return retval;
  485. }
  486. static int rmi_populate_f11(struct hid_device *hdev)
  487. {
  488. struct rmi_data *data = hid_get_drvdata(hdev);
  489. u8 buf[20];
  490. int ret;
  491. bool has_query9;
  492. bool has_query10 = false;
  493. bool has_query11;
  494. bool has_query12;
  495. bool has_query27;
  496. bool has_query28;
  497. bool has_query36 = false;
  498. bool has_physical_props;
  499. bool has_gestures;
  500. bool has_rel;
  501. bool has_data40 = false;
  502. unsigned x_size, y_size;
  503. u16 query_offset;
  504. if (!data->f11.query_base_addr) {
  505. hid_err(hdev, "No 2D sensor found, giving up.\n");
  506. return -ENODEV;
  507. }
  508. /* query 0 contains some useful information */
  509. ret = rmi_read(hdev, data->f11.query_base_addr, buf);
  510. if (ret) {
  511. hid_err(hdev, "can not get query 0: %d.\n", ret);
  512. return ret;
  513. }
  514. has_query9 = !!(buf[0] & BIT(3));
  515. has_query11 = !!(buf[0] & BIT(4));
  516. has_query12 = !!(buf[0] & BIT(5));
  517. has_query27 = !!(buf[0] & BIT(6));
  518. has_query28 = !!(buf[0] & BIT(7));
  519. /* query 1 to get the max number of fingers */
  520. ret = rmi_read(hdev, data->f11.query_base_addr + 1, buf);
  521. if (ret) {
  522. hid_err(hdev, "can not get NumberOfFingers: %d.\n", ret);
  523. return ret;
  524. }
  525. data->max_fingers = (buf[0] & 0x07) + 1;
  526. if (data->max_fingers > 5)
  527. data->max_fingers = 10;
  528. data->f11.report_size = data->max_fingers * 5 +
  529. DIV_ROUND_UP(data->max_fingers, 4);
  530. if (!(buf[0] & BIT(4))) {
  531. hid_err(hdev, "No absolute events, giving up.\n");
  532. return -ENODEV;
  533. }
  534. has_rel = !!(buf[0] & BIT(3));
  535. has_gestures = !!(buf[0] & BIT(5));
  536. /*
  537. * At least 4 queries are guaranteed to be present in F11
  538. * +1 for query 5 which is present since absolute events are
  539. * reported and +1 for query 12.
  540. */
  541. query_offset = 6;
  542. if (has_rel)
  543. ++query_offset; /* query 6 is present */
  544. if (has_gestures) {
  545. /* query 8 to find out if query 10 exists */
  546. ret = rmi_read(hdev,
  547. data->f11.query_base_addr + query_offset + 1, buf);
  548. if (ret) {
  549. hid_err(hdev, "can not read gesture information: %d.\n",
  550. ret);
  551. return ret;
  552. }
  553. has_query10 = !!(buf[0] & BIT(2));
  554. query_offset += 2; /* query 7 and 8 are present */
  555. }
  556. if (has_query9)
  557. ++query_offset;
  558. if (has_query10)
  559. ++query_offset;
  560. if (has_query11)
  561. ++query_offset;
  562. /* query 12 to know if the physical properties are reported */
  563. if (has_query12) {
  564. ret = rmi_read(hdev, data->f11.query_base_addr
  565. + query_offset, buf);
  566. if (ret) {
  567. hid_err(hdev, "can not get query 12: %d.\n", ret);
  568. return ret;
  569. }
  570. has_physical_props = !!(buf[0] & BIT(5));
  571. if (has_physical_props) {
  572. query_offset += 1;
  573. ret = rmi_read_block(hdev,
  574. data->f11.query_base_addr
  575. + query_offset, buf, 4);
  576. if (ret) {
  577. hid_err(hdev, "can not read query 15-18: %d.\n",
  578. ret);
  579. return ret;
  580. }
  581. x_size = buf[0] | (buf[1] << 8);
  582. y_size = buf[2] | (buf[3] << 8);
  583. data->x_size_mm = DIV_ROUND_CLOSEST(x_size, 10);
  584. data->y_size_mm = DIV_ROUND_CLOSEST(y_size, 10);
  585. hid_info(hdev, "%s: size in mm: %d x %d\n",
  586. __func__, data->x_size_mm, data->y_size_mm);
  587. /*
  588. * query 15 - 18 contain the size of the sensor
  589. * and query 19 - 26 contain bezel dimensions
  590. */
  591. query_offset += 12;
  592. }
  593. }
  594. if (has_query27)
  595. ++query_offset;
  596. if (has_query28) {
  597. ret = rmi_read(hdev, data->f11.query_base_addr
  598. + query_offset, buf);
  599. if (ret) {
  600. hid_err(hdev, "can not get query 28: %d.\n", ret);
  601. return ret;
  602. }
  603. has_query36 = !!(buf[0] & BIT(6));
  604. }
  605. if (has_query36) {
  606. query_offset += 2;
  607. ret = rmi_read(hdev, data->f11.query_base_addr
  608. + query_offset, buf);
  609. if (ret) {
  610. hid_err(hdev, "can not get query 36: %d.\n", ret);
  611. return ret;
  612. }
  613. has_data40 = !!(buf[0] & BIT(5));
  614. }
  615. if (has_data40)
  616. data->f11.report_size += data->max_fingers * 2;
  617. /*
  618. * retrieve the ctrl registers
  619. * the ctrl register has a size of 20 but a fw bug split it into 16 + 4,
  620. * and there is no way to know if the first 20 bytes are here or not.
  621. * We use only the first 10 bytes, so get only them.
  622. */
  623. ret = rmi_read_block(hdev, data->f11.control_base_addr, buf, 10);
  624. if (ret) {
  625. hid_err(hdev, "can not read ctrl block of size 10: %d.\n", ret);
  626. return ret;
  627. }
  628. data->max_x = buf[6] | (buf[7] << 8);
  629. data->max_y = buf[8] | (buf[9] << 8);
  630. return 0;
  631. }
  632. static int rmi_populate_f30(struct hid_device *hdev)
  633. {
  634. struct rmi_data *data = hid_get_drvdata(hdev);
  635. u8 buf[20];
  636. int ret;
  637. bool has_gpio, has_led;
  638. unsigned bytes_per_ctrl;
  639. u8 ctrl2_addr;
  640. int ctrl2_3_length;
  641. int i;
  642. /* function F30 is for physical buttons */
  643. if (!data->f30.query_base_addr) {
  644. hid_err(hdev, "No GPIO/LEDs found, giving up.\n");
  645. return -ENODEV;
  646. }
  647. ret = rmi_read_block(hdev, data->f30.query_base_addr, buf, 2);
  648. if (ret) {
  649. hid_err(hdev, "can not get F30 query registers: %d.\n", ret);
  650. return ret;
  651. }
  652. has_gpio = !!(buf[0] & BIT(3));
  653. has_led = !!(buf[0] & BIT(2));
  654. data->gpio_led_count = buf[1] & 0x1f;
  655. /* retrieve ctrl 2 & 3 registers */
  656. bytes_per_ctrl = (data->gpio_led_count + 7) / 8;
  657. /* Ctrl0 is present only if both has_gpio and has_led are set*/
  658. ctrl2_addr = (has_gpio && has_led) ? bytes_per_ctrl : 0;
  659. /* Ctrl1 is always be present */
  660. ctrl2_addr += bytes_per_ctrl;
  661. ctrl2_3_length = 2 * bytes_per_ctrl;
  662. data->f30.report_size = bytes_per_ctrl;
  663. ret = rmi_read_block(hdev, data->f30.control_base_addr + ctrl2_addr,
  664. buf, ctrl2_3_length);
  665. if (ret) {
  666. hid_err(hdev, "can not read ctrl 2&3 block of size %d: %d.\n",
  667. ctrl2_3_length, ret);
  668. return ret;
  669. }
  670. for (i = 0; i < data->gpio_led_count; i++) {
  671. int byte_position = i >> 3;
  672. int bit_position = i & 0x07;
  673. u8 dir_byte = buf[byte_position];
  674. u8 data_byte = buf[byte_position + bytes_per_ctrl];
  675. bool dir = (dir_byte >> bit_position) & BIT(0);
  676. bool dat = (data_byte >> bit_position) & BIT(0);
  677. if (dir == 0) {
  678. /* input mode */
  679. if (dat) {
  680. /* actual buttons have pull up resistor */
  681. data->button_count++;
  682. set_bit(i, &data->button_mask);
  683. set_bit(i, &data->button_state_mask);
  684. }
  685. }
  686. }
  687. return 0;
  688. }
  689. static int rmi_populate(struct hid_device *hdev)
  690. {
  691. int ret;
  692. ret = rmi_scan_pdt(hdev);
  693. if (ret) {
  694. hid_err(hdev, "PDT scan failed with code %d.\n", ret);
  695. return ret;
  696. }
  697. ret = rmi_populate_f11(hdev);
  698. if (ret) {
  699. hid_err(hdev, "Error while initializing F11 (%d).\n", ret);
  700. return ret;
  701. }
  702. ret = rmi_populate_f30(hdev);
  703. if (ret)
  704. hid_warn(hdev, "Error while initializing F30 (%d).\n", ret);
  705. return 0;
  706. }
  707. static void rmi_input_configured(struct hid_device *hdev, struct hid_input *hi)
  708. {
  709. struct rmi_data *data = hid_get_drvdata(hdev);
  710. struct input_dev *input = hi->input;
  711. int ret;
  712. int res_x, res_y, i;
  713. data->input = input;
  714. hid_dbg(hdev, "Opening low level driver\n");
  715. ret = hid_hw_open(hdev);
  716. if (ret)
  717. return;
  718. /* Allow incoming hid reports */
  719. hid_device_io_start(hdev);
  720. ret = rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
  721. if (ret < 0) {
  722. dev_err(&hdev->dev, "failed to set rmi mode\n");
  723. goto exit;
  724. }
  725. ret = rmi_set_page(hdev, 0);
  726. if (ret < 0) {
  727. dev_err(&hdev->dev, "failed to set page select to 0.\n");
  728. goto exit;
  729. }
  730. ret = rmi_populate(hdev);
  731. if (ret)
  732. goto exit;
  733. __set_bit(EV_ABS, input->evbit);
  734. input_set_abs_params(input, ABS_MT_POSITION_X, 1, data->max_x, 0, 0);
  735. input_set_abs_params(input, ABS_MT_POSITION_Y, 1, data->max_y, 0, 0);
  736. if (data->x_size_mm && data->y_size_mm) {
  737. res_x = (data->max_x - 1) / data->x_size_mm;
  738. res_y = (data->max_y - 1) / data->y_size_mm;
  739. input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
  740. input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
  741. }
  742. input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
  743. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xff, 0, 0);
  744. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 0x0f, 0, 0);
  745. input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 0x0f, 0, 0);
  746. input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
  747. if (data->button_count) {
  748. __set_bit(EV_KEY, input->evbit);
  749. for (i = 0; i < data->button_count; i++)
  750. __set_bit(BTN_LEFT + i, input->keybit);
  751. if (data->button_count == 1)
  752. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  753. }
  754. set_bit(RMI_STARTED, &data->flags);
  755. exit:
  756. hid_device_io_stop(hdev);
  757. hid_hw_close(hdev);
  758. }
  759. static int rmi_input_mapping(struct hid_device *hdev,
  760. struct hid_input *hi, struct hid_field *field,
  761. struct hid_usage *usage, unsigned long **bit, int *max)
  762. {
  763. /* we want to make HID ignore the advertised HID collection */
  764. return -1;
  765. }
  766. static int rmi_probe(struct hid_device *hdev, const struct hid_device_id *id)
  767. {
  768. struct rmi_data *data = NULL;
  769. int ret;
  770. size_t alloc_size;
  771. struct hid_report *input_report;
  772. struct hid_report *output_report;
  773. data = devm_kzalloc(&hdev->dev, sizeof(struct rmi_data), GFP_KERNEL);
  774. if (!data)
  775. return -ENOMEM;
  776. INIT_WORK(&data->reset_work, rmi_reset_work);
  777. data->hdev = hdev;
  778. hid_set_drvdata(hdev, data);
  779. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  780. ret = hid_parse(hdev);
  781. if (ret) {
  782. hid_err(hdev, "parse failed\n");
  783. return ret;
  784. }
  785. input_report = hdev->report_enum[HID_INPUT_REPORT]
  786. .report_id_hash[RMI_ATTN_REPORT_ID];
  787. if (!input_report) {
  788. hid_err(hdev, "device does not have expected input report\n");
  789. ret = -ENODEV;
  790. return ret;
  791. }
  792. data->input_report_size = (input_report->size >> 3) + 1 /* report id */;
  793. output_report = hdev->report_enum[HID_OUTPUT_REPORT]
  794. .report_id_hash[RMI_WRITE_REPORT_ID];
  795. if (!output_report) {
  796. hid_err(hdev, "device does not have expected output report\n");
  797. ret = -ENODEV;
  798. return ret;
  799. }
  800. data->output_report_size = (output_report->size >> 3)
  801. + 1 /* report id */;
  802. alloc_size = data->output_report_size + data->input_report_size;
  803. data->writeReport = devm_kzalloc(&hdev->dev, alloc_size, GFP_KERNEL);
  804. if (!data->writeReport) {
  805. ret = -ENOMEM;
  806. return ret;
  807. }
  808. data->readReport = data->writeReport + data->output_report_size;
  809. init_waitqueue_head(&data->wait);
  810. mutex_init(&data->page_mutex);
  811. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  812. if (ret) {
  813. hid_err(hdev, "hw start failed\n");
  814. return ret;
  815. }
  816. if (!test_bit(RMI_STARTED, &data->flags))
  817. /*
  818. * The device maybe in the bootloader if rmi_input_configured
  819. * failed to find F11 in the PDT. Print an error, but don't
  820. * return an error from rmi_probe so that hidraw will be
  821. * accessible from userspace. That way a userspace tool
  822. * can be used to reload working firmware on the touchpad.
  823. */
  824. hid_err(hdev, "Device failed to be properly configured\n");
  825. return 0;
  826. }
  827. static void rmi_remove(struct hid_device *hdev)
  828. {
  829. struct rmi_data *hdata = hid_get_drvdata(hdev);
  830. clear_bit(RMI_STARTED, &hdata->flags);
  831. hid_hw_stop(hdev);
  832. }
  833. static const struct hid_device_id rmi_id[] = {
  834. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_RMI, HID_ANY_ID, HID_ANY_ID) },
  835. { }
  836. };
  837. MODULE_DEVICE_TABLE(hid, rmi_id);
  838. static struct hid_driver rmi_driver = {
  839. .name = "hid-rmi",
  840. .id_table = rmi_id,
  841. .probe = rmi_probe,
  842. .remove = rmi_remove,
  843. .raw_event = rmi_raw_event,
  844. .input_mapping = rmi_input_mapping,
  845. .input_configured = rmi_input_configured,
  846. #ifdef CONFIG_PM
  847. .resume = rmi_post_resume,
  848. .reset_resume = rmi_post_reset,
  849. #endif
  850. };
  851. module_hid_driver(rmi_driver);
  852. MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
  853. MODULE_DESCRIPTION("RMI HID driver");
  854. MODULE_LICENSE("GPL");