hid-rmi.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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 0
  30. #define RMI_READ_DATA_PENDING 1
  31. #define RMI_STARTED 2
  32. #define RMI_SLEEP_NORMAL 0x0
  33. #define RMI_SLEEP_DEEP_SLEEP 0x1
  34. /* device flags */
  35. #define RMI_DEVICE BIT(0)
  36. #define RMI_DEVICE_HAS_PHYS_BUTTONS BIT(1)
  37. /*
  38. * retrieve the ctrl registers
  39. * the ctrl register has a size of 20 but a fw bug split it into 16 + 4,
  40. * and there is no way to know if the first 20 bytes are here or not.
  41. * We use only the first 12 bytes, so get only them.
  42. */
  43. #define RMI_F11_CTRL_REG_COUNT 12
  44. enum rmi_mode_type {
  45. RMI_MODE_OFF = 0,
  46. RMI_MODE_ATTN_REPORTS = 1,
  47. RMI_MODE_NO_PACKED_ATTN_REPORTS = 2,
  48. };
  49. struct rmi_function {
  50. unsigned page; /* page of the function */
  51. u16 query_base_addr; /* base address for queries */
  52. u16 command_base_addr; /* base address for commands */
  53. u16 control_base_addr; /* base address for controls */
  54. u16 data_base_addr; /* base address for datas */
  55. unsigned int interrupt_base; /* cross-function interrupt number
  56. * (uniq in the device)*/
  57. unsigned int interrupt_count; /* number of interrupts */
  58. unsigned int report_size; /* size of a report */
  59. unsigned long irq_mask; /* mask of the interrupts
  60. * (to be applied against ATTN IRQ) */
  61. };
  62. /**
  63. * struct rmi_data - stores information for hid communication
  64. *
  65. * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
  66. * @page: Keeps track of the current virtual page
  67. *
  68. * @wait: Used for waiting for read data
  69. *
  70. * @writeReport: output buffer when writing RMI registers
  71. * @readReport: input buffer when reading RMI registers
  72. *
  73. * @input_report_size: size of an input report (advertised by HID)
  74. * @output_report_size: size of an output report (advertised by HID)
  75. *
  76. * @flags: flags for the current device (started, reading, etc...)
  77. *
  78. * @f11: placeholder of internal RMI function F11 description
  79. * @f30: placeholder of internal RMI function F30 description
  80. *
  81. * @max_fingers: maximum finger count reported by the device
  82. * @max_x: maximum x value reported by the device
  83. * @max_y: maximum y value reported by the device
  84. *
  85. * @gpio_led_count: count of GPIOs + LEDs reported by F30
  86. * @button_count: actual physical buttons count
  87. * @button_mask: button mask used to decode GPIO ATTN reports
  88. * @button_state_mask: pull state of the buttons
  89. *
  90. * @input: pointer to the kernel input device
  91. *
  92. * @reset_work: worker which will be called in case of a mouse report
  93. * @hdev: pointer to the struct hid_device
  94. */
  95. struct rmi_data {
  96. struct mutex page_mutex;
  97. int page;
  98. wait_queue_head_t wait;
  99. u8 *writeReport;
  100. u8 *readReport;
  101. u32 input_report_size;
  102. u32 output_report_size;
  103. unsigned long flags;
  104. struct rmi_function f01;
  105. struct rmi_function f11;
  106. struct rmi_function f30;
  107. unsigned int max_fingers;
  108. unsigned int max_x;
  109. unsigned int max_y;
  110. unsigned int x_size_mm;
  111. unsigned int y_size_mm;
  112. bool read_f11_ctrl_regs;
  113. u8 f11_ctrl_regs[RMI_F11_CTRL_REG_COUNT];
  114. unsigned int gpio_led_count;
  115. unsigned int button_count;
  116. unsigned long button_mask;
  117. unsigned long button_state_mask;
  118. struct input_dev *input;
  119. struct work_struct reset_work;
  120. struct hid_device *hdev;
  121. unsigned long device_flags;
  122. unsigned long firmware_id;
  123. u8 f01_ctrl0;
  124. u8 interrupt_enable_mask;
  125. bool restore_interrupt_mask;
  126. };
  127. #define RMI_PAGE(addr) (((addr) >> 8) & 0xff)
  128. static int rmi_write_report(struct hid_device *hdev, u8 *report, int len);
  129. /**
  130. * rmi_set_page - Set RMI page
  131. * @hdev: The pointer to the hid_device struct
  132. * @page: The new page address.
  133. *
  134. * RMI devices have 16-bit addressing, but some of the physical
  135. * implementations (like SMBus) only have 8-bit addressing. So RMI implements
  136. * a page address at 0xff of every page so we can reliable page addresses
  137. * every 256 registers.
  138. *
  139. * The page_mutex lock must be held when this function is entered.
  140. *
  141. * Returns zero on success, non-zero on failure.
  142. */
  143. static int rmi_set_page(struct hid_device *hdev, u8 page)
  144. {
  145. struct rmi_data *data = hid_get_drvdata(hdev);
  146. int retval;
  147. data->writeReport[0] = RMI_WRITE_REPORT_ID;
  148. data->writeReport[1] = 1;
  149. data->writeReport[2] = 0xFF;
  150. data->writeReport[4] = page;
  151. retval = rmi_write_report(hdev, data->writeReport,
  152. data->output_report_size);
  153. if (retval != data->output_report_size) {
  154. dev_err(&hdev->dev,
  155. "%s: set page failed: %d.", __func__, retval);
  156. return retval;
  157. }
  158. data->page = page;
  159. return 0;
  160. }
  161. static int rmi_set_mode(struct hid_device *hdev, u8 mode)
  162. {
  163. int ret;
  164. const u8 txbuf[2] = {RMI_SET_RMI_MODE_REPORT_ID, mode};
  165. u8 *buf;
  166. buf = kmemdup(txbuf, sizeof(txbuf), GFP_KERNEL);
  167. if (!buf)
  168. return -ENOMEM;
  169. ret = hid_hw_raw_request(hdev, RMI_SET_RMI_MODE_REPORT_ID, buf,
  170. sizeof(txbuf), HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  171. kfree(buf);
  172. if (ret < 0) {
  173. dev_err(&hdev->dev, "unable to set rmi mode to %d (%d)\n", mode,
  174. ret);
  175. return ret;
  176. }
  177. return 0;
  178. }
  179. static int rmi_write_report(struct hid_device *hdev, u8 *report, int len)
  180. {
  181. int ret;
  182. ret = hid_hw_output_report(hdev, (void *)report, len);
  183. if (ret < 0) {
  184. dev_err(&hdev->dev, "failed to write hid report (%d)\n", ret);
  185. return ret;
  186. }
  187. return ret;
  188. }
  189. static int rmi_read_block(struct hid_device *hdev, u16 addr, void *buf,
  190. const int len)
  191. {
  192. struct rmi_data *data = hid_get_drvdata(hdev);
  193. int ret;
  194. int bytes_read;
  195. int bytes_needed;
  196. int retries;
  197. int read_input_count;
  198. mutex_lock(&data->page_mutex);
  199. if (RMI_PAGE(addr) != data->page) {
  200. ret = rmi_set_page(hdev, RMI_PAGE(addr));
  201. if (ret < 0)
  202. goto exit;
  203. }
  204. for (retries = 5; retries > 0; retries--) {
  205. data->writeReport[0] = RMI_READ_ADDR_REPORT_ID;
  206. data->writeReport[1] = 0; /* old 1 byte read count */
  207. data->writeReport[2] = addr & 0xFF;
  208. data->writeReport[3] = (addr >> 8) & 0xFF;
  209. data->writeReport[4] = len & 0xFF;
  210. data->writeReport[5] = (len >> 8) & 0xFF;
  211. set_bit(RMI_READ_REQUEST_PENDING, &data->flags);
  212. ret = rmi_write_report(hdev, data->writeReport,
  213. data->output_report_size);
  214. if (ret != data->output_report_size) {
  215. clear_bit(RMI_READ_REQUEST_PENDING, &data->flags);
  216. dev_err(&hdev->dev,
  217. "failed to write request output report (%d)\n",
  218. ret);
  219. goto exit;
  220. }
  221. bytes_read = 0;
  222. bytes_needed = len;
  223. while (bytes_read < len) {
  224. if (!wait_event_timeout(data->wait,
  225. test_bit(RMI_READ_DATA_PENDING, &data->flags),
  226. msecs_to_jiffies(1000))) {
  227. hid_warn(hdev, "%s: timeout elapsed\n",
  228. __func__);
  229. ret = -EAGAIN;
  230. break;
  231. }
  232. read_input_count = data->readReport[1];
  233. memcpy(buf + bytes_read, &data->readReport[2],
  234. read_input_count < bytes_needed ?
  235. read_input_count : bytes_needed);
  236. bytes_read += read_input_count;
  237. bytes_needed -= read_input_count;
  238. clear_bit(RMI_READ_DATA_PENDING, &data->flags);
  239. }
  240. if (ret >= 0) {
  241. ret = 0;
  242. break;
  243. }
  244. }
  245. exit:
  246. clear_bit(RMI_READ_REQUEST_PENDING, &data->flags);
  247. mutex_unlock(&data->page_mutex);
  248. return ret;
  249. }
  250. static inline int rmi_read(struct hid_device *hdev, u16 addr, void *buf)
  251. {
  252. return rmi_read_block(hdev, addr, buf, 1);
  253. }
  254. static int rmi_write_block(struct hid_device *hdev, u16 addr, void *buf,
  255. const int len)
  256. {
  257. struct rmi_data *data = hid_get_drvdata(hdev);
  258. int ret;
  259. mutex_lock(&data->page_mutex);
  260. if (RMI_PAGE(addr) != data->page) {
  261. ret = rmi_set_page(hdev, RMI_PAGE(addr));
  262. if (ret < 0)
  263. goto exit;
  264. }
  265. data->writeReport[0] = RMI_WRITE_REPORT_ID;
  266. data->writeReport[1] = len;
  267. data->writeReport[2] = addr & 0xFF;
  268. data->writeReport[3] = (addr >> 8) & 0xFF;
  269. memcpy(&data->writeReport[4], buf, len);
  270. ret = rmi_write_report(hdev, data->writeReport,
  271. data->output_report_size);
  272. if (ret < 0) {
  273. dev_err(&hdev->dev,
  274. "failed to write request output report (%d)\n",
  275. ret);
  276. goto exit;
  277. }
  278. ret = 0;
  279. exit:
  280. mutex_unlock(&data->page_mutex);
  281. return ret;
  282. }
  283. static inline int rmi_write(struct hid_device *hdev, u16 addr, void *buf)
  284. {
  285. return rmi_write_block(hdev, addr, buf, 1);
  286. }
  287. static void rmi_f11_process_touch(struct rmi_data *hdata, int slot,
  288. u8 finger_state, u8 *touch_data)
  289. {
  290. int x, y, wx, wy;
  291. int wide, major, minor;
  292. int z;
  293. input_mt_slot(hdata->input, slot);
  294. input_mt_report_slot_state(hdata->input, MT_TOOL_FINGER,
  295. finger_state == 0x01);
  296. if (finger_state == 0x01) {
  297. x = (touch_data[0] << 4) | (touch_data[2] & 0x0F);
  298. y = (touch_data[1] << 4) | (touch_data[2] >> 4);
  299. wx = touch_data[3] & 0x0F;
  300. wy = touch_data[3] >> 4;
  301. wide = (wx > wy);
  302. major = max(wx, wy);
  303. minor = min(wx, wy);
  304. z = touch_data[4];
  305. /* y is inverted */
  306. y = hdata->max_y - y;
  307. input_event(hdata->input, EV_ABS, ABS_MT_POSITION_X, x);
  308. input_event(hdata->input, EV_ABS, ABS_MT_POSITION_Y, y);
  309. input_event(hdata->input, EV_ABS, ABS_MT_ORIENTATION, wide);
  310. input_event(hdata->input, EV_ABS, ABS_MT_PRESSURE, z);
  311. input_event(hdata->input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
  312. input_event(hdata->input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
  313. }
  314. }
  315. static int rmi_reset_attn_mode(struct hid_device *hdev)
  316. {
  317. struct rmi_data *data = hid_get_drvdata(hdev);
  318. int ret;
  319. ret = rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
  320. if (ret)
  321. return ret;
  322. if (data->restore_interrupt_mask) {
  323. ret = rmi_write(hdev, data->f01.control_base_addr + 1,
  324. &data->interrupt_enable_mask);
  325. if (ret) {
  326. hid_err(hdev, "can not write F01 control register\n");
  327. return ret;
  328. }
  329. }
  330. return 0;
  331. }
  332. static void rmi_reset_work(struct work_struct *work)
  333. {
  334. struct rmi_data *hdata = container_of(work, struct rmi_data,
  335. reset_work);
  336. /* switch the device to RMI if we receive a generic mouse report */
  337. rmi_reset_attn_mode(hdata->hdev);
  338. }
  339. static inline int rmi_schedule_reset(struct hid_device *hdev)
  340. {
  341. struct rmi_data *hdata = hid_get_drvdata(hdev);
  342. return schedule_work(&hdata->reset_work);
  343. }
  344. static int rmi_f11_input_event(struct hid_device *hdev, u8 irq, u8 *data,
  345. int size)
  346. {
  347. struct rmi_data *hdata = hid_get_drvdata(hdev);
  348. int offset;
  349. int i;
  350. if (!(irq & hdata->f11.irq_mask) || size <= 0)
  351. return 0;
  352. offset = (hdata->max_fingers >> 2) + 1;
  353. for (i = 0; i < hdata->max_fingers; i++) {
  354. int fs_byte_position = i >> 2;
  355. int fs_bit_position = (i & 0x3) << 1;
  356. int finger_state = (data[fs_byte_position] >> fs_bit_position) &
  357. 0x03;
  358. int position = offset + 5 * i;
  359. if (position + 5 > size) {
  360. /* partial report, go on with what we received */
  361. printk_once(KERN_WARNING
  362. "%s %s: Detected incomplete finger report. Finger reports may occasionally get dropped on this platform.\n",
  363. dev_driver_string(&hdev->dev),
  364. dev_name(&hdev->dev));
  365. hid_dbg(hdev, "Incomplete finger report\n");
  366. break;
  367. }
  368. rmi_f11_process_touch(hdata, i, finger_state, &data[position]);
  369. }
  370. input_mt_sync_frame(hdata->input);
  371. input_sync(hdata->input);
  372. return hdata->f11.report_size;
  373. }
  374. static int rmi_f30_input_event(struct hid_device *hdev, u8 irq, u8 *data,
  375. int size)
  376. {
  377. struct rmi_data *hdata = hid_get_drvdata(hdev);
  378. int i;
  379. int button = 0;
  380. bool value;
  381. if (!(irq & hdata->f30.irq_mask))
  382. return 0;
  383. if (size < (int)hdata->f30.report_size) {
  384. hid_warn(hdev, "Click Button pressed, but the click data is missing\n");
  385. return 0;
  386. }
  387. for (i = 0; i < hdata->gpio_led_count; i++) {
  388. if (test_bit(i, &hdata->button_mask)) {
  389. value = (data[i / 8] >> (i & 0x07)) & BIT(0);
  390. if (test_bit(i, &hdata->button_state_mask))
  391. value = !value;
  392. input_event(hdata->input, EV_KEY, BTN_LEFT + button++,
  393. value);
  394. }
  395. }
  396. return hdata->f30.report_size;
  397. }
  398. static int rmi_input_event(struct hid_device *hdev, u8 *data, int size)
  399. {
  400. struct rmi_data *hdata = hid_get_drvdata(hdev);
  401. unsigned long irq_mask = 0;
  402. unsigned index = 2;
  403. if (!(test_bit(RMI_STARTED, &hdata->flags)))
  404. return 0;
  405. irq_mask |= hdata->f11.irq_mask;
  406. irq_mask |= hdata->f30.irq_mask;
  407. if (data[1] & ~irq_mask)
  408. hid_dbg(hdev, "unknown intr source:%02lx %s:%d\n",
  409. data[1] & ~irq_mask, __FILE__, __LINE__);
  410. if (hdata->f11.interrupt_base < hdata->f30.interrupt_base) {
  411. index += rmi_f11_input_event(hdev, data[1], &data[index],
  412. size - index);
  413. index += rmi_f30_input_event(hdev, data[1], &data[index],
  414. size - index);
  415. } else {
  416. index += rmi_f30_input_event(hdev, data[1], &data[index],
  417. size - index);
  418. index += rmi_f11_input_event(hdev, data[1], &data[index],
  419. size - index);
  420. }
  421. return 1;
  422. }
  423. static int rmi_read_data_event(struct hid_device *hdev, u8 *data, int size)
  424. {
  425. struct rmi_data *hdata = hid_get_drvdata(hdev);
  426. if (!test_bit(RMI_READ_REQUEST_PENDING, &hdata->flags)) {
  427. hid_dbg(hdev, "no read request pending\n");
  428. return 0;
  429. }
  430. memcpy(hdata->readReport, data, size < hdata->input_report_size ?
  431. size : hdata->input_report_size);
  432. set_bit(RMI_READ_DATA_PENDING, &hdata->flags);
  433. wake_up(&hdata->wait);
  434. return 1;
  435. }
  436. static int rmi_check_sanity(struct hid_device *hdev, u8 *data, int size)
  437. {
  438. int valid_size = size;
  439. /*
  440. * On the Dell XPS 13 9333, the bus sometimes get confused and fills
  441. * the report with a sentinel value "ff". Synaptics told us that such
  442. * behavior does not comes from the touchpad itself, so we filter out
  443. * such reports here.
  444. */
  445. while ((data[valid_size - 1] == 0xff) && valid_size > 0)
  446. valid_size--;
  447. return valid_size;
  448. }
  449. static int rmi_raw_event(struct hid_device *hdev,
  450. struct hid_report *report, u8 *data, int size)
  451. {
  452. size = rmi_check_sanity(hdev, data, size);
  453. if (size < 2)
  454. return 0;
  455. switch (data[0]) {
  456. case RMI_READ_DATA_REPORT_ID:
  457. return rmi_read_data_event(hdev, data, size);
  458. case RMI_ATTN_REPORT_ID:
  459. return rmi_input_event(hdev, data, size);
  460. default:
  461. return 1;
  462. }
  463. return 0;
  464. }
  465. static int rmi_event(struct hid_device *hdev, struct hid_field *field,
  466. struct hid_usage *usage, __s32 value)
  467. {
  468. struct rmi_data *data = hid_get_drvdata(hdev);
  469. if ((data->device_flags & RMI_DEVICE) &&
  470. (field->application == HID_GD_POINTER ||
  471. field->application == HID_GD_MOUSE)) {
  472. if (data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS) {
  473. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
  474. return 0;
  475. if ((usage->hid == HID_GD_X || usage->hid == HID_GD_Y)
  476. && !value)
  477. return 1;
  478. }
  479. rmi_schedule_reset(hdev);
  480. return 1;
  481. }
  482. return 0;
  483. }
  484. #ifdef CONFIG_PM
  485. static int rmi_set_sleep_mode(struct hid_device *hdev, int sleep_mode)
  486. {
  487. struct rmi_data *data = hid_get_drvdata(hdev);
  488. int ret;
  489. u8 f01_ctrl0;
  490. f01_ctrl0 = (data->f01_ctrl0 & ~0x3) | sleep_mode;
  491. ret = rmi_write(hdev, data->f01.control_base_addr,
  492. &f01_ctrl0);
  493. if (ret) {
  494. hid_err(hdev, "can not write sleep mode\n");
  495. return ret;
  496. }
  497. return 0;
  498. }
  499. static int rmi_suspend(struct hid_device *hdev, pm_message_t message)
  500. {
  501. struct rmi_data *data = hid_get_drvdata(hdev);
  502. int ret;
  503. u8 buf[RMI_F11_CTRL_REG_COUNT];
  504. if (!(data->device_flags & RMI_DEVICE))
  505. return 0;
  506. ret = rmi_read_block(hdev, data->f11.control_base_addr, buf,
  507. RMI_F11_CTRL_REG_COUNT);
  508. if (ret)
  509. hid_warn(hdev, "can not read F11 control registers\n");
  510. else
  511. memcpy(data->f11_ctrl_regs, buf, RMI_F11_CTRL_REG_COUNT);
  512. if (!device_may_wakeup(hdev->dev.parent))
  513. return rmi_set_sleep_mode(hdev, RMI_SLEEP_DEEP_SLEEP);
  514. return 0;
  515. }
  516. static int rmi_post_reset(struct hid_device *hdev)
  517. {
  518. struct rmi_data *data = hid_get_drvdata(hdev);
  519. int ret;
  520. if (!(data->device_flags & RMI_DEVICE))
  521. return 0;
  522. ret = rmi_reset_attn_mode(hdev);
  523. if (ret) {
  524. hid_err(hdev, "can not set rmi mode\n");
  525. return ret;
  526. }
  527. if (data->read_f11_ctrl_regs) {
  528. ret = rmi_write_block(hdev, data->f11.control_base_addr,
  529. data->f11_ctrl_regs, RMI_F11_CTRL_REG_COUNT);
  530. if (ret)
  531. hid_warn(hdev,
  532. "can not write F11 control registers after reset\n");
  533. }
  534. if (!device_may_wakeup(hdev->dev.parent)) {
  535. ret = rmi_set_sleep_mode(hdev, RMI_SLEEP_NORMAL);
  536. if (ret) {
  537. hid_err(hdev, "can not write sleep mode\n");
  538. return ret;
  539. }
  540. }
  541. return ret;
  542. }
  543. static int rmi_post_resume(struct hid_device *hdev)
  544. {
  545. struct rmi_data *data = hid_get_drvdata(hdev);
  546. if (!(data->device_flags & RMI_DEVICE))
  547. return 0;
  548. return rmi_reset_attn_mode(hdev);
  549. }
  550. #endif /* CONFIG_PM */
  551. #define RMI4_MAX_PAGE 0xff
  552. #define RMI4_PAGE_SIZE 0x0100
  553. #define PDT_START_SCAN_LOCATION 0x00e9
  554. #define PDT_END_SCAN_LOCATION 0x0005
  555. #define RMI4_END_OF_PDT(id) ((id) == 0x00 || (id) == 0xff)
  556. struct pdt_entry {
  557. u8 query_base_addr:8;
  558. u8 command_base_addr:8;
  559. u8 control_base_addr:8;
  560. u8 data_base_addr:8;
  561. u8 interrupt_source_count:3;
  562. u8 bits3and4:2;
  563. u8 function_version:2;
  564. u8 bit7:1;
  565. u8 function_number:8;
  566. } __attribute__((__packed__));
  567. static inline unsigned long rmi_gen_mask(unsigned irq_base, unsigned irq_count)
  568. {
  569. return GENMASK(irq_count + irq_base - 1, irq_base);
  570. }
  571. static void rmi_register_function(struct rmi_data *data,
  572. struct pdt_entry *pdt_entry, int page, unsigned interrupt_count)
  573. {
  574. struct rmi_function *f = NULL;
  575. u16 page_base = page << 8;
  576. switch (pdt_entry->function_number) {
  577. case 0x01:
  578. f = &data->f01;
  579. break;
  580. case 0x11:
  581. f = &data->f11;
  582. break;
  583. case 0x30:
  584. f = &data->f30;
  585. break;
  586. }
  587. if (f) {
  588. f->page = page;
  589. f->query_base_addr = page_base | pdt_entry->query_base_addr;
  590. f->command_base_addr = page_base | pdt_entry->command_base_addr;
  591. f->control_base_addr = page_base | pdt_entry->control_base_addr;
  592. f->data_base_addr = page_base | pdt_entry->data_base_addr;
  593. f->interrupt_base = interrupt_count;
  594. f->interrupt_count = pdt_entry->interrupt_source_count;
  595. f->irq_mask = rmi_gen_mask(f->interrupt_base,
  596. f->interrupt_count);
  597. data->interrupt_enable_mask |= f->irq_mask;
  598. }
  599. }
  600. static int rmi_scan_pdt(struct hid_device *hdev)
  601. {
  602. struct rmi_data *data = hid_get_drvdata(hdev);
  603. struct pdt_entry entry;
  604. int page;
  605. bool page_has_function;
  606. int i;
  607. int retval;
  608. int interrupt = 0;
  609. u16 page_start, pdt_start , pdt_end;
  610. hid_info(hdev, "Scanning PDT...\n");
  611. for (page = 0; (page <= RMI4_MAX_PAGE); page++) {
  612. page_start = RMI4_PAGE_SIZE * page;
  613. pdt_start = page_start + PDT_START_SCAN_LOCATION;
  614. pdt_end = page_start + PDT_END_SCAN_LOCATION;
  615. page_has_function = false;
  616. for (i = pdt_start; i >= pdt_end; i -= sizeof(entry)) {
  617. retval = rmi_read_block(hdev, i, &entry, sizeof(entry));
  618. if (retval) {
  619. hid_err(hdev,
  620. "Read of PDT entry at %#06x failed.\n",
  621. i);
  622. goto error_exit;
  623. }
  624. if (RMI4_END_OF_PDT(entry.function_number))
  625. break;
  626. page_has_function = true;
  627. hid_info(hdev, "Found F%02X on page %#04x\n",
  628. entry.function_number, page);
  629. rmi_register_function(data, &entry, page, interrupt);
  630. interrupt += entry.interrupt_source_count;
  631. }
  632. if (!page_has_function)
  633. break;
  634. }
  635. hid_info(hdev, "%s: Done with PDT scan.\n", __func__);
  636. retval = 0;
  637. error_exit:
  638. return retval;
  639. }
  640. #define RMI_DEVICE_F01_BASIC_QUERY_LEN 11
  641. static int rmi_populate_f01(struct hid_device *hdev)
  642. {
  643. struct rmi_data *data = hid_get_drvdata(hdev);
  644. u8 basic_queries[RMI_DEVICE_F01_BASIC_QUERY_LEN];
  645. u8 info[3];
  646. int ret;
  647. bool has_query42;
  648. bool has_lts;
  649. bool has_sensor_id;
  650. bool has_ds4_queries = false;
  651. bool has_build_id_query = false;
  652. bool has_package_id_query = false;
  653. u16 query_offset = data->f01.query_base_addr;
  654. u16 prod_info_addr;
  655. u8 ds4_query_len;
  656. ret = rmi_read_block(hdev, query_offset, basic_queries,
  657. RMI_DEVICE_F01_BASIC_QUERY_LEN);
  658. if (ret) {
  659. hid_err(hdev, "Can not read basic queries from Function 0x1.\n");
  660. return ret;
  661. }
  662. has_lts = !!(basic_queries[0] & BIT(2));
  663. has_sensor_id = !!(basic_queries[1] & BIT(3));
  664. has_query42 = !!(basic_queries[1] & BIT(7));
  665. query_offset += 11;
  666. prod_info_addr = query_offset + 6;
  667. query_offset += 10;
  668. if (has_lts)
  669. query_offset += 20;
  670. if (has_sensor_id)
  671. query_offset++;
  672. if (has_query42) {
  673. ret = rmi_read(hdev, query_offset, info);
  674. if (ret) {
  675. hid_err(hdev, "Can not read query42.\n");
  676. return ret;
  677. }
  678. has_ds4_queries = !!(info[0] & BIT(0));
  679. query_offset++;
  680. }
  681. if (has_ds4_queries) {
  682. ret = rmi_read(hdev, query_offset, &ds4_query_len);
  683. if (ret) {
  684. hid_err(hdev, "Can not read DS4 Query length.\n");
  685. return ret;
  686. }
  687. query_offset++;
  688. if (ds4_query_len > 0) {
  689. ret = rmi_read(hdev, query_offset, info);
  690. if (ret) {
  691. hid_err(hdev, "Can not read DS4 query.\n");
  692. return ret;
  693. }
  694. has_package_id_query = !!(info[0] & BIT(0));
  695. has_build_id_query = !!(info[0] & BIT(1));
  696. }
  697. }
  698. if (has_package_id_query)
  699. prod_info_addr++;
  700. if (has_build_id_query) {
  701. ret = rmi_read_block(hdev, prod_info_addr, info, 3);
  702. if (ret) {
  703. hid_err(hdev, "Can not read product info.\n");
  704. return ret;
  705. }
  706. data->firmware_id = info[1] << 8 | info[0];
  707. data->firmware_id += info[2] * 65536;
  708. }
  709. ret = rmi_read_block(hdev, data->f01.control_base_addr, info,
  710. 2);
  711. if (ret) {
  712. hid_err(hdev, "can not read f01 ctrl registers\n");
  713. return ret;
  714. }
  715. data->f01_ctrl0 = info[0];
  716. if (!info[1]) {
  717. /*
  718. * Do to a firmware bug in some touchpads the F01 interrupt
  719. * enable control register will be cleared on reset.
  720. * This will stop the touchpad from reporting data, so
  721. * if F01 CTRL1 is 0 then we need to explicitly enable
  722. * interrupts for the functions we want data for.
  723. */
  724. data->restore_interrupt_mask = true;
  725. ret = rmi_write(hdev, data->f01.control_base_addr + 1,
  726. &data->interrupt_enable_mask);
  727. if (ret) {
  728. hid_err(hdev, "can not write to control reg 1: %d.\n",
  729. ret);
  730. return ret;
  731. }
  732. }
  733. return 0;
  734. }
  735. static int rmi_populate_f11(struct hid_device *hdev)
  736. {
  737. struct rmi_data *data = hid_get_drvdata(hdev);
  738. u8 buf[20];
  739. int ret;
  740. bool has_query9;
  741. bool has_query10 = false;
  742. bool has_query11;
  743. bool has_query12;
  744. bool has_query27;
  745. bool has_query28;
  746. bool has_query36 = false;
  747. bool has_physical_props;
  748. bool has_gestures;
  749. bool has_rel;
  750. bool has_data40 = false;
  751. bool has_dribble = false;
  752. bool has_palm_detect = false;
  753. unsigned x_size, y_size;
  754. u16 query_offset;
  755. if (!data->f11.query_base_addr) {
  756. hid_err(hdev, "No 2D sensor found, giving up.\n");
  757. return -ENODEV;
  758. }
  759. /* query 0 contains some useful information */
  760. ret = rmi_read(hdev, data->f11.query_base_addr, buf);
  761. if (ret) {
  762. hid_err(hdev, "can not get query 0: %d.\n", ret);
  763. return ret;
  764. }
  765. has_query9 = !!(buf[0] & BIT(3));
  766. has_query11 = !!(buf[0] & BIT(4));
  767. has_query12 = !!(buf[0] & BIT(5));
  768. has_query27 = !!(buf[0] & BIT(6));
  769. has_query28 = !!(buf[0] & BIT(7));
  770. /* query 1 to get the max number of fingers */
  771. ret = rmi_read(hdev, data->f11.query_base_addr + 1, buf);
  772. if (ret) {
  773. hid_err(hdev, "can not get NumberOfFingers: %d.\n", ret);
  774. return ret;
  775. }
  776. data->max_fingers = (buf[0] & 0x07) + 1;
  777. if (data->max_fingers > 5)
  778. data->max_fingers = 10;
  779. data->f11.report_size = data->max_fingers * 5 +
  780. DIV_ROUND_UP(data->max_fingers, 4);
  781. if (!(buf[0] & BIT(4))) {
  782. hid_err(hdev, "No absolute events, giving up.\n");
  783. return -ENODEV;
  784. }
  785. has_rel = !!(buf[0] & BIT(3));
  786. has_gestures = !!(buf[0] & BIT(5));
  787. ret = rmi_read(hdev, data->f11.query_base_addr + 5, buf);
  788. if (ret) {
  789. hid_err(hdev, "can not get absolute data sources: %d.\n", ret);
  790. return ret;
  791. }
  792. has_dribble = !!(buf[0] & BIT(4));
  793. /*
  794. * At least 4 queries are guaranteed to be present in F11
  795. * +1 for query 5 which is present since absolute events are
  796. * reported and +1 for query 12.
  797. */
  798. query_offset = 6;
  799. if (has_rel)
  800. ++query_offset; /* query 6 is present */
  801. if (has_gestures) {
  802. /* query 8 to find out if query 10 exists */
  803. ret = rmi_read(hdev,
  804. data->f11.query_base_addr + query_offset + 1, buf);
  805. if (ret) {
  806. hid_err(hdev, "can not read gesture information: %d.\n",
  807. ret);
  808. return ret;
  809. }
  810. has_palm_detect = !!(buf[0] & BIT(0));
  811. has_query10 = !!(buf[0] & BIT(2));
  812. query_offset += 2; /* query 7 and 8 are present */
  813. }
  814. if (has_query9)
  815. ++query_offset;
  816. if (has_query10)
  817. ++query_offset;
  818. if (has_query11)
  819. ++query_offset;
  820. /* query 12 to know if the physical properties are reported */
  821. if (has_query12) {
  822. ret = rmi_read(hdev, data->f11.query_base_addr
  823. + query_offset, buf);
  824. if (ret) {
  825. hid_err(hdev, "can not get query 12: %d.\n", ret);
  826. return ret;
  827. }
  828. has_physical_props = !!(buf[0] & BIT(5));
  829. if (has_physical_props) {
  830. query_offset += 1;
  831. ret = rmi_read_block(hdev,
  832. data->f11.query_base_addr
  833. + query_offset, buf, 4);
  834. if (ret) {
  835. hid_err(hdev, "can not read query 15-18: %d.\n",
  836. ret);
  837. return ret;
  838. }
  839. x_size = buf[0] | (buf[1] << 8);
  840. y_size = buf[2] | (buf[3] << 8);
  841. data->x_size_mm = DIV_ROUND_CLOSEST(x_size, 10);
  842. data->y_size_mm = DIV_ROUND_CLOSEST(y_size, 10);
  843. hid_info(hdev, "%s: size in mm: %d x %d\n",
  844. __func__, data->x_size_mm, data->y_size_mm);
  845. /*
  846. * query 15 - 18 contain the size of the sensor
  847. * and query 19 - 26 contain bezel dimensions
  848. */
  849. query_offset += 12;
  850. }
  851. }
  852. if (has_query27)
  853. ++query_offset;
  854. if (has_query28) {
  855. ret = rmi_read(hdev, data->f11.query_base_addr
  856. + query_offset, buf);
  857. if (ret) {
  858. hid_err(hdev, "can not get query 28: %d.\n", ret);
  859. return ret;
  860. }
  861. has_query36 = !!(buf[0] & BIT(6));
  862. }
  863. if (has_query36) {
  864. query_offset += 2;
  865. ret = rmi_read(hdev, data->f11.query_base_addr
  866. + query_offset, buf);
  867. if (ret) {
  868. hid_err(hdev, "can not get query 36: %d.\n", ret);
  869. return ret;
  870. }
  871. has_data40 = !!(buf[0] & BIT(5));
  872. }
  873. if (has_data40)
  874. data->f11.report_size += data->max_fingers * 2;
  875. ret = rmi_read_block(hdev, data->f11.control_base_addr,
  876. data->f11_ctrl_regs, RMI_F11_CTRL_REG_COUNT);
  877. if (ret) {
  878. hid_err(hdev, "can not read ctrl block of size 11: %d.\n", ret);
  879. return ret;
  880. }
  881. /* data->f11_ctrl_regs now contains valid register data */
  882. data->read_f11_ctrl_regs = true;
  883. data->max_x = data->f11_ctrl_regs[6] | (data->f11_ctrl_regs[7] << 8);
  884. data->max_y = data->f11_ctrl_regs[8] | (data->f11_ctrl_regs[9] << 8);
  885. if (has_dribble) {
  886. data->f11_ctrl_regs[0] = data->f11_ctrl_regs[0] & ~BIT(6);
  887. ret = rmi_write(hdev, data->f11.control_base_addr,
  888. data->f11_ctrl_regs);
  889. if (ret) {
  890. hid_err(hdev, "can not write to control reg 0: %d.\n",
  891. ret);
  892. return ret;
  893. }
  894. }
  895. if (has_palm_detect) {
  896. data->f11_ctrl_regs[11] = data->f11_ctrl_regs[11] & ~BIT(0);
  897. ret = rmi_write(hdev, data->f11.control_base_addr + 11,
  898. &data->f11_ctrl_regs[11]);
  899. if (ret) {
  900. hid_err(hdev, "can not write to control reg 11: %d.\n",
  901. ret);
  902. return ret;
  903. }
  904. }
  905. return 0;
  906. }
  907. static int rmi_populate_f30(struct hid_device *hdev)
  908. {
  909. struct rmi_data *data = hid_get_drvdata(hdev);
  910. u8 buf[20];
  911. int ret;
  912. bool has_gpio, has_led;
  913. unsigned bytes_per_ctrl;
  914. u8 ctrl2_addr;
  915. int ctrl2_3_length;
  916. int i;
  917. /* function F30 is for physical buttons */
  918. if (!data->f30.query_base_addr) {
  919. hid_err(hdev, "No GPIO/LEDs found, giving up.\n");
  920. return -ENODEV;
  921. }
  922. ret = rmi_read_block(hdev, data->f30.query_base_addr, buf, 2);
  923. if (ret) {
  924. hid_err(hdev, "can not get F30 query registers: %d.\n", ret);
  925. return ret;
  926. }
  927. has_gpio = !!(buf[0] & BIT(3));
  928. has_led = !!(buf[0] & BIT(2));
  929. data->gpio_led_count = buf[1] & 0x1f;
  930. /* retrieve ctrl 2 & 3 registers */
  931. bytes_per_ctrl = (data->gpio_led_count + 7) / 8;
  932. /* Ctrl0 is present only if both has_gpio and has_led are set*/
  933. ctrl2_addr = (has_gpio && has_led) ? bytes_per_ctrl : 0;
  934. /* Ctrl1 is always be present */
  935. ctrl2_addr += bytes_per_ctrl;
  936. ctrl2_3_length = 2 * bytes_per_ctrl;
  937. data->f30.report_size = bytes_per_ctrl;
  938. ret = rmi_read_block(hdev, data->f30.control_base_addr + ctrl2_addr,
  939. buf, ctrl2_3_length);
  940. if (ret) {
  941. hid_err(hdev, "can not read ctrl 2&3 block of size %d: %d.\n",
  942. ctrl2_3_length, ret);
  943. return ret;
  944. }
  945. for (i = 0; i < data->gpio_led_count; i++) {
  946. int byte_position = i >> 3;
  947. int bit_position = i & 0x07;
  948. u8 dir_byte = buf[byte_position];
  949. u8 data_byte = buf[byte_position + bytes_per_ctrl];
  950. bool dir = (dir_byte >> bit_position) & BIT(0);
  951. bool dat = (data_byte >> bit_position) & BIT(0);
  952. if (dir == 0) {
  953. /* input mode */
  954. if (dat) {
  955. /* actual buttons have pull up resistor */
  956. data->button_count++;
  957. set_bit(i, &data->button_mask);
  958. set_bit(i, &data->button_state_mask);
  959. }
  960. }
  961. }
  962. return 0;
  963. }
  964. static int rmi_populate(struct hid_device *hdev)
  965. {
  966. struct rmi_data *data = hid_get_drvdata(hdev);
  967. int ret;
  968. ret = rmi_scan_pdt(hdev);
  969. if (ret) {
  970. hid_err(hdev, "PDT scan failed with code %d.\n", ret);
  971. return ret;
  972. }
  973. ret = rmi_populate_f01(hdev);
  974. if (ret) {
  975. hid_err(hdev, "Error while initializing F01 (%d).\n", ret);
  976. return ret;
  977. }
  978. ret = rmi_populate_f11(hdev);
  979. if (ret) {
  980. hid_err(hdev, "Error while initializing F11 (%d).\n", ret);
  981. return ret;
  982. }
  983. if (!(data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS)) {
  984. ret = rmi_populate_f30(hdev);
  985. if (ret)
  986. hid_warn(hdev, "Error while initializing F30 (%d).\n", ret);
  987. }
  988. return 0;
  989. }
  990. static int rmi_input_configured(struct hid_device *hdev, struct hid_input *hi)
  991. {
  992. struct rmi_data *data = hid_get_drvdata(hdev);
  993. struct input_dev *input = hi->input;
  994. int ret;
  995. int res_x, res_y, i;
  996. data->input = input;
  997. hid_dbg(hdev, "Opening low level driver\n");
  998. ret = hid_hw_open(hdev);
  999. if (ret)
  1000. return ret;
  1001. if (!(data->device_flags & RMI_DEVICE))
  1002. return 0;
  1003. /* Allow incoming hid reports */
  1004. hid_device_io_start(hdev);
  1005. ret = rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
  1006. if (ret < 0) {
  1007. dev_err(&hdev->dev, "failed to set rmi mode\n");
  1008. goto exit;
  1009. }
  1010. ret = rmi_set_page(hdev, 0);
  1011. if (ret < 0) {
  1012. dev_err(&hdev->dev, "failed to set page select to 0.\n");
  1013. goto exit;
  1014. }
  1015. ret = rmi_populate(hdev);
  1016. if (ret)
  1017. goto exit;
  1018. hid_info(hdev, "firmware id: %ld\n", data->firmware_id);
  1019. __set_bit(EV_ABS, input->evbit);
  1020. input_set_abs_params(input, ABS_MT_POSITION_X, 1, data->max_x, 0, 0);
  1021. input_set_abs_params(input, ABS_MT_POSITION_Y, 1, data->max_y, 0, 0);
  1022. if (data->x_size_mm && data->y_size_mm) {
  1023. res_x = (data->max_x - 1) / data->x_size_mm;
  1024. res_y = (data->max_y - 1) / data->y_size_mm;
  1025. input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
  1026. input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
  1027. }
  1028. input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
  1029. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xff, 0, 0);
  1030. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 0x0f, 0, 0);
  1031. input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 0x0f, 0, 0);
  1032. ret = input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
  1033. if (ret < 0)
  1034. goto exit;
  1035. if (data->button_count) {
  1036. __set_bit(EV_KEY, input->evbit);
  1037. for (i = 0; i < data->button_count; i++)
  1038. __set_bit(BTN_LEFT + i, input->keybit);
  1039. if (data->button_count == 1)
  1040. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  1041. }
  1042. set_bit(RMI_STARTED, &data->flags);
  1043. exit:
  1044. hid_device_io_stop(hdev);
  1045. hid_hw_close(hdev);
  1046. return ret;
  1047. }
  1048. static int rmi_input_mapping(struct hid_device *hdev,
  1049. struct hid_input *hi, struct hid_field *field,
  1050. struct hid_usage *usage, unsigned long **bit, int *max)
  1051. {
  1052. struct rmi_data *data = hid_get_drvdata(hdev);
  1053. /*
  1054. * we want to make HID ignore the advertised HID collection
  1055. * for RMI deivces
  1056. */
  1057. if (data->device_flags & RMI_DEVICE) {
  1058. if ((data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS) &&
  1059. ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON))
  1060. return 0;
  1061. return -1;
  1062. }
  1063. return 0;
  1064. }
  1065. static int rmi_check_valid_report_id(struct hid_device *hdev, unsigned type,
  1066. unsigned id, struct hid_report **report)
  1067. {
  1068. int i;
  1069. *report = hdev->report_enum[type].report_id_hash[id];
  1070. if (*report) {
  1071. for (i = 0; i < (*report)->maxfield; i++) {
  1072. unsigned app = (*report)->field[i]->application;
  1073. if ((app & HID_USAGE_PAGE) >= HID_UP_MSVENDOR)
  1074. return 1;
  1075. }
  1076. }
  1077. return 0;
  1078. }
  1079. static int rmi_probe(struct hid_device *hdev, const struct hid_device_id *id)
  1080. {
  1081. struct rmi_data *data = NULL;
  1082. int ret;
  1083. size_t alloc_size;
  1084. struct hid_report *input_report;
  1085. struct hid_report *output_report;
  1086. struct hid_report *feature_report;
  1087. data = devm_kzalloc(&hdev->dev, sizeof(struct rmi_data), GFP_KERNEL);
  1088. if (!data)
  1089. return -ENOMEM;
  1090. INIT_WORK(&data->reset_work, rmi_reset_work);
  1091. data->hdev = hdev;
  1092. hid_set_drvdata(hdev, data);
  1093. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  1094. ret = hid_parse(hdev);
  1095. if (ret) {
  1096. hid_err(hdev, "parse failed\n");
  1097. return ret;
  1098. }
  1099. if (id->driver_data)
  1100. data->device_flags = id->driver_data;
  1101. /*
  1102. * Check for the RMI specific report ids. If they are misisng
  1103. * simply return and let the events be processed by hid-input
  1104. */
  1105. if (!rmi_check_valid_report_id(hdev, HID_FEATURE_REPORT,
  1106. RMI_SET_RMI_MODE_REPORT_ID, &feature_report)) {
  1107. hid_dbg(hdev, "device does not have set mode feature report\n");
  1108. goto start;
  1109. }
  1110. if (!rmi_check_valid_report_id(hdev, HID_INPUT_REPORT,
  1111. RMI_ATTN_REPORT_ID, &input_report)) {
  1112. hid_dbg(hdev, "device does not have attention input report\n");
  1113. goto start;
  1114. }
  1115. data->input_report_size = hid_report_len(input_report);
  1116. if (!rmi_check_valid_report_id(hdev, HID_OUTPUT_REPORT,
  1117. RMI_WRITE_REPORT_ID, &output_report)) {
  1118. hid_dbg(hdev,
  1119. "device does not have rmi write output report\n");
  1120. goto start;
  1121. }
  1122. data->output_report_size = hid_report_len(output_report);
  1123. data->device_flags |= RMI_DEVICE;
  1124. alloc_size = data->output_report_size + data->input_report_size;
  1125. data->writeReport = devm_kzalloc(&hdev->dev, alloc_size, GFP_KERNEL);
  1126. if (!data->writeReport) {
  1127. ret = -ENOMEM;
  1128. return ret;
  1129. }
  1130. data->readReport = data->writeReport + data->output_report_size;
  1131. init_waitqueue_head(&data->wait);
  1132. mutex_init(&data->page_mutex);
  1133. start:
  1134. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  1135. if (ret) {
  1136. hid_err(hdev, "hw start failed\n");
  1137. return ret;
  1138. }
  1139. if ((data->device_flags & RMI_DEVICE) &&
  1140. !test_bit(RMI_STARTED, &data->flags))
  1141. /*
  1142. * The device maybe in the bootloader if rmi_input_configured
  1143. * failed to find F11 in the PDT. Print an error, but don't
  1144. * return an error from rmi_probe so that hidraw will be
  1145. * accessible from userspace. That way a userspace tool
  1146. * can be used to reload working firmware on the touchpad.
  1147. */
  1148. hid_err(hdev, "Device failed to be properly configured\n");
  1149. return 0;
  1150. }
  1151. static void rmi_remove(struct hid_device *hdev)
  1152. {
  1153. struct rmi_data *hdata = hid_get_drvdata(hdev);
  1154. clear_bit(RMI_STARTED, &hdata->flags);
  1155. hid_hw_stop(hdev);
  1156. }
  1157. static const struct hid_device_id rmi_id[] = {
  1158. { HID_USB_DEVICE(USB_VENDOR_ID_RAZER, USB_DEVICE_ID_RAZER_BLADE_14),
  1159. .driver_data = RMI_DEVICE_HAS_PHYS_BUTTONS },
  1160. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_RMI, HID_ANY_ID, HID_ANY_ID) },
  1161. { }
  1162. };
  1163. MODULE_DEVICE_TABLE(hid, rmi_id);
  1164. static struct hid_driver rmi_driver = {
  1165. .name = "hid-rmi",
  1166. .id_table = rmi_id,
  1167. .probe = rmi_probe,
  1168. .remove = rmi_remove,
  1169. .event = rmi_event,
  1170. .raw_event = rmi_raw_event,
  1171. .input_mapping = rmi_input_mapping,
  1172. .input_configured = rmi_input_configured,
  1173. #ifdef CONFIG_PM
  1174. .suspend = rmi_suspend,
  1175. .resume = rmi_post_resume,
  1176. .reset_resume = rmi_post_reset,
  1177. #endif
  1178. };
  1179. module_hid_driver(rmi_driver);
  1180. MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
  1181. MODULE_DESCRIPTION("RMI HID driver");
  1182. MODULE_LICENSE("GPL");