hid-rmi.c 35 KB

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