hid-rmi.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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. int input_report_size;
  102. int 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. if (!(data->device_flags & RMI_DEVICE))
  500. return 0;
  501. ret = rmi_read_block(hdev, data->f11.control_base_addr, buf,
  502. RMI_F11_CTRL_REG_COUNT);
  503. if (ret)
  504. hid_warn(hdev, "can not read F11 control registers\n");
  505. else
  506. memcpy(data->f11_ctrl_regs, buf, RMI_F11_CTRL_REG_COUNT);
  507. if (!device_may_wakeup(hdev->dev.parent))
  508. return rmi_set_sleep_mode(hdev, RMI_SLEEP_DEEP_SLEEP);
  509. return 0;
  510. }
  511. static int rmi_post_reset(struct hid_device *hdev)
  512. {
  513. struct rmi_data *data = hid_get_drvdata(hdev);
  514. int ret;
  515. if (!(data->device_flags & RMI_DEVICE))
  516. return 0;
  517. ret = rmi_reset_attn_mode(hdev);
  518. if (ret) {
  519. hid_err(hdev, "can not set rmi mode\n");
  520. return ret;
  521. }
  522. if (data->read_f11_ctrl_regs) {
  523. ret = rmi_write_block(hdev, data->f11.control_base_addr,
  524. data->f11_ctrl_regs, RMI_F11_CTRL_REG_COUNT);
  525. if (ret)
  526. hid_warn(hdev,
  527. "can not write F11 control registers after reset\n");
  528. }
  529. if (!device_may_wakeup(hdev->dev.parent)) {
  530. ret = rmi_set_sleep_mode(hdev, RMI_SLEEP_NORMAL);
  531. if (ret) {
  532. hid_err(hdev, "can not write sleep mode\n");
  533. return ret;
  534. }
  535. }
  536. return ret;
  537. }
  538. static int rmi_post_resume(struct hid_device *hdev)
  539. {
  540. struct rmi_data *data = hid_get_drvdata(hdev);
  541. if (!(data->device_flags & RMI_DEVICE))
  542. return 0;
  543. return rmi_reset_attn_mode(hdev);
  544. }
  545. #endif /* CONFIG_PM */
  546. #define RMI4_MAX_PAGE 0xff
  547. #define RMI4_PAGE_SIZE 0x0100
  548. #define PDT_START_SCAN_LOCATION 0x00e9
  549. #define PDT_END_SCAN_LOCATION 0x0005
  550. #define RMI4_END_OF_PDT(id) ((id) == 0x00 || (id) == 0xff)
  551. struct pdt_entry {
  552. u8 query_base_addr:8;
  553. u8 command_base_addr:8;
  554. u8 control_base_addr:8;
  555. u8 data_base_addr:8;
  556. u8 interrupt_source_count:3;
  557. u8 bits3and4:2;
  558. u8 function_version:2;
  559. u8 bit7:1;
  560. u8 function_number:8;
  561. } __attribute__((__packed__));
  562. static inline unsigned long rmi_gen_mask(unsigned irq_base, unsigned irq_count)
  563. {
  564. return GENMASK(irq_count + irq_base - 1, irq_base);
  565. }
  566. static void rmi_register_function(struct rmi_data *data,
  567. struct pdt_entry *pdt_entry, int page, unsigned interrupt_count)
  568. {
  569. struct rmi_function *f = NULL;
  570. u16 page_base = page << 8;
  571. switch (pdt_entry->function_number) {
  572. case 0x01:
  573. f = &data->f01;
  574. break;
  575. case 0x11:
  576. f = &data->f11;
  577. break;
  578. case 0x30:
  579. f = &data->f30;
  580. break;
  581. }
  582. if (f) {
  583. f->page = page;
  584. f->query_base_addr = page_base | pdt_entry->query_base_addr;
  585. f->command_base_addr = page_base | pdt_entry->command_base_addr;
  586. f->control_base_addr = page_base | pdt_entry->control_base_addr;
  587. f->data_base_addr = page_base | pdt_entry->data_base_addr;
  588. f->interrupt_base = interrupt_count;
  589. f->interrupt_count = pdt_entry->interrupt_source_count;
  590. f->irq_mask = rmi_gen_mask(f->interrupt_base,
  591. f->interrupt_count);
  592. data->interrupt_enable_mask |= f->irq_mask;
  593. }
  594. }
  595. static int rmi_scan_pdt(struct hid_device *hdev)
  596. {
  597. struct rmi_data *data = hid_get_drvdata(hdev);
  598. struct pdt_entry entry;
  599. int page;
  600. bool page_has_function;
  601. int i;
  602. int retval;
  603. int interrupt = 0;
  604. u16 page_start, pdt_start , pdt_end;
  605. hid_info(hdev, "Scanning PDT...\n");
  606. for (page = 0; (page <= RMI4_MAX_PAGE); page++) {
  607. page_start = RMI4_PAGE_SIZE * page;
  608. pdt_start = page_start + PDT_START_SCAN_LOCATION;
  609. pdt_end = page_start + PDT_END_SCAN_LOCATION;
  610. page_has_function = false;
  611. for (i = pdt_start; i >= pdt_end; i -= sizeof(entry)) {
  612. retval = rmi_read_block(hdev, i, &entry, sizeof(entry));
  613. if (retval) {
  614. hid_err(hdev,
  615. "Read of PDT entry at %#06x failed.\n",
  616. i);
  617. goto error_exit;
  618. }
  619. if (RMI4_END_OF_PDT(entry.function_number))
  620. break;
  621. page_has_function = true;
  622. hid_info(hdev, "Found F%02X on page %#04x\n",
  623. entry.function_number, page);
  624. rmi_register_function(data, &entry, page, interrupt);
  625. interrupt += entry.interrupt_source_count;
  626. }
  627. if (!page_has_function)
  628. break;
  629. }
  630. hid_info(hdev, "%s: Done with PDT scan.\n", __func__);
  631. retval = 0;
  632. error_exit:
  633. return retval;
  634. }
  635. #define RMI_DEVICE_F01_BASIC_QUERY_LEN 11
  636. static int rmi_populate_f01(struct hid_device *hdev)
  637. {
  638. struct rmi_data *data = hid_get_drvdata(hdev);
  639. u8 basic_queries[RMI_DEVICE_F01_BASIC_QUERY_LEN];
  640. u8 info[3];
  641. int ret;
  642. bool has_query42;
  643. bool has_lts;
  644. bool has_sensor_id;
  645. bool has_ds4_queries = false;
  646. bool has_build_id_query = false;
  647. bool has_package_id_query = false;
  648. u16 query_offset = data->f01.query_base_addr;
  649. u16 prod_info_addr;
  650. u8 ds4_query_len;
  651. ret = rmi_read_block(hdev, query_offset, basic_queries,
  652. RMI_DEVICE_F01_BASIC_QUERY_LEN);
  653. if (ret) {
  654. hid_err(hdev, "Can not read basic queries from Function 0x1.\n");
  655. return ret;
  656. }
  657. has_lts = !!(basic_queries[0] & BIT(2));
  658. has_sensor_id = !!(basic_queries[1] & BIT(3));
  659. has_query42 = !!(basic_queries[1] & BIT(7));
  660. query_offset += 11;
  661. prod_info_addr = query_offset + 6;
  662. query_offset += 10;
  663. if (has_lts)
  664. query_offset += 20;
  665. if (has_sensor_id)
  666. query_offset++;
  667. if (has_query42) {
  668. ret = rmi_read(hdev, query_offset, info);
  669. if (ret) {
  670. hid_err(hdev, "Can not read query42.\n");
  671. return ret;
  672. }
  673. has_ds4_queries = !!(info[0] & BIT(0));
  674. query_offset++;
  675. }
  676. if (has_ds4_queries) {
  677. ret = rmi_read(hdev, query_offset, &ds4_query_len);
  678. if (ret) {
  679. hid_err(hdev, "Can not read DS4 Query length.\n");
  680. return ret;
  681. }
  682. query_offset++;
  683. if (ds4_query_len > 0) {
  684. ret = rmi_read(hdev, query_offset, info);
  685. if (ret) {
  686. hid_err(hdev, "Can not read DS4 query.\n");
  687. return ret;
  688. }
  689. has_package_id_query = !!(info[0] & BIT(0));
  690. has_build_id_query = !!(info[0] & BIT(1));
  691. }
  692. }
  693. if (has_package_id_query)
  694. prod_info_addr++;
  695. if (has_build_id_query) {
  696. ret = rmi_read_block(hdev, prod_info_addr, info, 3);
  697. if (ret) {
  698. hid_err(hdev, "Can not read product info.\n");
  699. return ret;
  700. }
  701. data->firmware_id = info[1] << 8 | info[0];
  702. data->firmware_id += info[2] * 65536;
  703. }
  704. ret = rmi_read_block(hdev, data->f01.control_base_addr, info,
  705. 2);
  706. if (ret) {
  707. hid_err(hdev, "can not read f01 ctrl registers\n");
  708. return ret;
  709. }
  710. data->f01_ctrl0 = info[0];
  711. if (!info[1]) {
  712. /*
  713. * Do to a firmware bug in some touchpads the F01 interrupt
  714. * enable control register will be cleared on reset.
  715. * This will stop the touchpad from reporting data, so
  716. * if F01 CTRL1 is 0 then we need to explicitly enable
  717. * interrupts for the functions we want data for.
  718. */
  719. data->restore_interrupt_mask = true;
  720. ret = rmi_write(hdev, data->f01.control_base_addr + 1,
  721. &data->interrupt_enable_mask);
  722. if (ret) {
  723. hid_err(hdev, "can not write to control reg 1: %d.\n",
  724. ret);
  725. return ret;
  726. }
  727. }
  728. return 0;
  729. }
  730. static int rmi_populate_f11(struct hid_device *hdev)
  731. {
  732. struct rmi_data *data = hid_get_drvdata(hdev);
  733. u8 buf[20];
  734. int ret;
  735. bool has_query9;
  736. bool has_query10 = false;
  737. bool has_query11;
  738. bool has_query12;
  739. bool has_query27;
  740. bool has_query28;
  741. bool has_query36 = false;
  742. bool has_physical_props;
  743. bool has_gestures;
  744. bool has_rel;
  745. bool has_data40 = false;
  746. bool has_dribble = false;
  747. bool has_palm_detect = false;
  748. unsigned x_size, y_size;
  749. u16 query_offset;
  750. if (!data->f11.query_base_addr) {
  751. hid_err(hdev, "No 2D sensor found, giving up.\n");
  752. return -ENODEV;
  753. }
  754. /* query 0 contains some useful information */
  755. ret = rmi_read(hdev, data->f11.query_base_addr, buf);
  756. if (ret) {
  757. hid_err(hdev, "can not get query 0: %d.\n", ret);
  758. return ret;
  759. }
  760. has_query9 = !!(buf[0] & BIT(3));
  761. has_query11 = !!(buf[0] & BIT(4));
  762. has_query12 = !!(buf[0] & BIT(5));
  763. has_query27 = !!(buf[0] & BIT(6));
  764. has_query28 = !!(buf[0] & BIT(7));
  765. /* query 1 to get the max number of fingers */
  766. ret = rmi_read(hdev, data->f11.query_base_addr + 1, buf);
  767. if (ret) {
  768. hid_err(hdev, "can not get NumberOfFingers: %d.\n", ret);
  769. return ret;
  770. }
  771. data->max_fingers = (buf[0] & 0x07) + 1;
  772. if (data->max_fingers > 5)
  773. data->max_fingers = 10;
  774. data->f11.report_size = data->max_fingers * 5 +
  775. DIV_ROUND_UP(data->max_fingers, 4);
  776. if (!(buf[0] & BIT(4))) {
  777. hid_err(hdev, "No absolute events, giving up.\n");
  778. return -ENODEV;
  779. }
  780. has_rel = !!(buf[0] & BIT(3));
  781. has_gestures = !!(buf[0] & BIT(5));
  782. ret = rmi_read(hdev, data->f11.query_base_addr + 5, buf);
  783. if (ret) {
  784. hid_err(hdev, "can not get absolute data sources: %d.\n", ret);
  785. return ret;
  786. }
  787. has_dribble = !!(buf[0] & BIT(4));
  788. /*
  789. * At least 4 queries are guaranteed to be present in F11
  790. * +1 for query 5 which is present since absolute events are
  791. * reported and +1 for query 12.
  792. */
  793. query_offset = 6;
  794. if (has_rel)
  795. ++query_offset; /* query 6 is present */
  796. if (has_gestures) {
  797. /* query 8 to find out if query 10 exists */
  798. ret = rmi_read(hdev,
  799. data->f11.query_base_addr + query_offset + 1, buf);
  800. if (ret) {
  801. hid_err(hdev, "can not read gesture information: %d.\n",
  802. ret);
  803. return ret;
  804. }
  805. has_palm_detect = !!(buf[0] & BIT(0));
  806. has_query10 = !!(buf[0] & BIT(2));
  807. query_offset += 2; /* query 7 and 8 are present */
  808. }
  809. if (has_query9)
  810. ++query_offset;
  811. if (has_query10)
  812. ++query_offset;
  813. if (has_query11)
  814. ++query_offset;
  815. /* query 12 to know if the physical properties are reported */
  816. if (has_query12) {
  817. ret = rmi_read(hdev, data->f11.query_base_addr
  818. + query_offset, buf);
  819. if (ret) {
  820. hid_err(hdev, "can not get query 12: %d.\n", ret);
  821. return ret;
  822. }
  823. has_physical_props = !!(buf[0] & BIT(5));
  824. if (has_physical_props) {
  825. query_offset += 1;
  826. ret = rmi_read_block(hdev,
  827. data->f11.query_base_addr
  828. + query_offset, buf, 4);
  829. if (ret) {
  830. hid_err(hdev, "can not read query 15-18: %d.\n",
  831. ret);
  832. return ret;
  833. }
  834. x_size = buf[0] | (buf[1] << 8);
  835. y_size = buf[2] | (buf[3] << 8);
  836. data->x_size_mm = DIV_ROUND_CLOSEST(x_size, 10);
  837. data->y_size_mm = DIV_ROUND_CLOSEST(y_size, 10);
  838. hid_info(hdev, "%s: size in mm: %d x %d\n",
  839. __func__, data->x_size_mm, data->y_size_mm);
  840. /*
  841. * query 15 - 18 contain the size of the sensor
  842. * and query 19 - 26 contain bezel dimensions
  843. */
  844. query_offset += 12;
  845. }
  846. }
  847. if (has_query27)
  848. ++query_offset;
  849. if (has_query28) {
  850. ret = rmi_read(hdev, data->f11.query_base_addr
  851. + query_offset, buf);
  852. if (ret) {
  853. hid_err(hdev, "can not get query 28: %d.\n", ret);
  854. return ret;
  855. }
  856. has_query36 = !!(buf[0] & BIT(6));
  857. }
  858. if (has_query36) {
  859. query_offset += 2;
  860. ret = rmi_read(hdev, data->f11.query_base_addr
  861. + query_offset, buf);
  862. if (ret) {
  863. hid_err(hdev, "can not get query 36: %d.\n", ret);
  864. return ret;
  865. }
  866. has_data40 = !!(buf[0] & BIT(5));
  867. }
  868. if (has_data40)
  869. data->f11.report_size += data->max_fingers * 2;
  870. ret = rmi_read_block(hdev, data->f11.control_base_addr,
  871. data->f11_ctrl_regs, RMI_F11_CTRL_REG_COUNT);
  872. if (ret) {
  873. hid_err(hdev, "can not read ctrl block of size 11: %d.\n", ret);
  874. return ret;
  875. }
  876. /* data->f11_ctrl_regs now contains valid register data */
  877. data->read_f11_ctrl_regs = true;
  878. data->max_x = data->f11_ctrl_regs[6] | (data->f11_ctrl_regs[7] << 8);
  879. data->max_y = data->f11_ctrl_regs[8] | (data->f11_ctrl_regs[9] << 8);
  880. if (has_dribble) {
  881. data->f11_ctrl_regs[0] = data->f11_ctrl_regs[0] & ~BIT(6);
  882. ret = rmi_write(hdev, data->f11.control_base_addr,
  883. data->f11_ctrl_regs);
  884. if (ret) {
  885. hid_err(hdev, "can not write to control reg 0: %d.\n",
  886. ret);
  887. return ret;
  888. }
  889. }
  890. if (has_palm_detect) {
  891. data->f11_ctrl_regs[11] = data->f11_ctrl_regs[11] & ~BIT(0);
  892. ret = rmi_write(hdev, data->f11.control_base_addr + 11,
  893. &data->f11_ctrl_regs[11]);
  894. if (ret) {
  895. hid_err(hdev, "can not write to control reg 11: %d.\n",
  896. ret);
  897. return ret;
  898. }
  899. }
  900. return 0;
  901. }
  902. static int rmi_populate_f30(struct hid_device *hdev)
  903. {
  904. struct rmi_data *data = hid_get_drvdata(hdev);
  905. u8 buf[20];
  906. int ret;
  907. bool has_gpio, has_led;
  908. unsigned bytes_per_ctrl;
  909. u8 ctrl2_addr;
  910. int ctrl2_3_length;
  911. int i;
  912. /* function F30 is for physical buttons */
  913. if (!data->f30.query_base_addr) {
  914. hid_err(hdev, "No GPIO/LEDs found, giving up.\n");
  915. return -ENODEV;
  916. }
  917. ret = rmi_read_block(hdev, data->f30.query_base_addr, buf, 2);
  918. if (ret) {
  919. hid_err(hdev, "can not get F30 query registers: %d.\n", ret);
  920. return ret;
  921. }
  922. has_gpio = !!(buf[0] & BIT(3));
  923. has_led = !!(buf[0] & BIT(2));
  924. data->gpio_led_count = buf[1] & 0x1f;
  925. /* retrieve ctrl 2 & 3 registers */
  926. bytes_per_ctrl = (data->gpio_led_count + 7) / 8;
  927. /* Ctrl0 is present only if both has_gpio and has_led are set*/
  928. ctrl2_addr = (has_gpio && has_led) ? bytes_per_ctrl : 0;
  929. /* Ctrl1 is always be present */
  930. ctrl2_addr += bytes_per_ctrl;
  931. ctrl2_3_length = 2 * bytes_per_ctrl;
  932. data->f30.report_size = bytes_per_ctrl;
  933. ret = rmi_read_block(hdev, data->f30.control_base_addr + ctrl2_addr,
  934. buf, ctrl2_3_length);
  935. if (ret) {
  936. hid_err(hdev, "can not read ctrl 2&3 block of size %d: %d.\n",
  937. ctrl2_3_length, ret);
  938. return ret;
  939. }
  940. for (i = 0; i < data->gpio_led_count; i++) {
  941. int byte_position = i >> 3;
  942. int bit_position = i & 0x07;
  943. u8 dir_byte = buf[byte_position];
  944. u8 data_byte = buf[byte_position + bytes_per_ctrl];
  945. bool dir = (dir_byte >> bit_position) & BIT(0);
  946. bool dat = (data_byte >> bit_position) & BIT(0);
  947. if (dir == 0) {
  948. /* input mode */
  949. if (dat) {
  950. /* actual buttons have pull up resistor */
  951. data->button_count++;
  952. set_bit(i, &data->button_mask);
  953. set_bit(i, &data->button_state_mask);
  954. }
  955. }
  956. }
  957. return 0;
  958. }
  959. static int rmi_populate(struct hid_device *hdev)
  960. {
  961. struct rmi_data *data = hid_get_drvdata(hdev);
  962. int ret;
  963. ret = rmi_scan_pdt(hdev);
  964. if (ret) {
  965. hid_err(hdev, "PDT scan failed with code %d.\n", ret);
  966. return ret;
  967. }
  968. ret = rmi_populate_f01(hdev);
  969. if (ret) {
  970. hid_err(hdev, "Error while initializing F01 (%d).\n", ret);
  971. return ret;
  972. }
  973. ret = rmi_populate_f11(hdev);
  974. if (ret) {
  975. hid_err(hdev, "Error while initializing F11 (%d).\n", ret);
  976. return ret;
  977. }
  978. if (!(data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS)) {
  979. ret = rmi_populate_f30(hdev);
  980. if (ret)
  981. hid_warn(hdev, "Error while initializing F30 (%d).\n", ret);
  982. }
  983. return 0;
  984. }
  985. static int rmi_input_configured(struct hid_device *hdev, struct hid_input *hi)
  986. {
  987. struct rmi_data *data = hid_get_drvdata(hdev);
  988. struct input_dev *input = hi->input;
  989. int ret;
  990. int res_x, res_y, i;
  991. data->input = input;
  992. hid_dbg(hdev, "Opening low level driver\n");
  993. ret = hid_hw_open(hdev);
  994. if (ret)
  995. return ret;
  996. if (!(data->device_flags & RMI_DEVICE))
  997. return 0;
  998. /* Allow incoming hid reports */
  999. hid_device_io_start(hdev);
  1000. ret = rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
  1001. if (ret < 0) {
  1002. dev_err(&hdev->dev, "failed to set rmi mode\n");
  1003. goto exit;
  1004. }
  1005. ret = rmi_set_page(hdev, 0);
  1006. if (ret < 0) {
  1007. dev_err(&hdev->dev, "failed to set page select to 0.\n");
  1008. goto exit;
  1009. }
  1010. ret = rmi_populate(hdev);
  1011. if (ret)
  1012. goto exit;
  1013. hid_info(hdev, "firmware id: %ld\n", data->firmware_id);
  1014. __set_bit(EV_ABS, input->evbit);
  1015. input_set_abs_params(input, ABS_MT_POSITION_X, 1, data->max_x, 0, 0);
  1016. input_set_abs_params(input, ABS_MT_POSITION_Y, 1, data->max_y, 0, 0);
  1017. if (data->x_size_mm && data->y_size_mm) {
  1018. res_x = (data->max_x - 1) / data->x_size_mm;
  1019. res_y = (data->max_y - 1) / data->y_size_mm;
  1020. input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
  1021. input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
  1022. }
  1023. input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
  1024. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xff, 0, 0);
  1025. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 0x0f, 0, 0);
  1026. input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 0x0f, 0, 0);
  1027. ret = input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
  1028. if (ret < 0)
  1029. goto exit;
  1030. if (data->button_count) {
  1031. __set_bit(EV_KEY, input->evbit);
  1032. for (i = 0; i < data->button_count; i++)
  1033. __set_bit(BTN_LEFT + i, input->keybit);
  1034. if (data->button_count == 1)
  1035. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  1036. }
  1037. set_bit(RMI_STARTED, &data->flags);
  1038. exit:
  1039. hid_device_io_stop(hdev);
  1040. hid_hw_close(hdev);
  1041. return ret;
  1042. }
  1043. static int rmi_input_mapping(struct hid_device *hdev,
  1044. struct hid_input *hi, struct hid_field *field,
  1045. struct hid_usage *usage, unsigned long **bit, int *max)
  1046. {
  1047. struct rmi_data *data = hid_get_drvdata(hdev);
  1048. /*
  1049. * we want to make HID ignore the advertised HID collection
  1050. * for RMI deivces
  1051. */
  1052. if (data->device_flags & RMI_DEVICE) {
  1053. if ((data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS) &&
  1054. ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON))
  1055. return 0;
  1056. return -1;
  1057. }
  1058. return 0;
  1059. }
  1060. static int rmi_check_valid_report_id(struct hid_device *hdev, unsigned type,
  1061. unsigned id, struct hid_report **report)
  1062. {
  1063. int i;
  1064. *report = hdev->report_enum[type].report_id_hash[id];
  1065. if (*report) {
  1066. for (i = 0; i < (*report)->maxfield; i++) {
  1067. unsigned app = (*report)->field[i]->application;
  1068. if ((app & HID_USAGE_PAGE) >= HID_UP_MSVENDOR)
  1069. return 1;
  1070. }
  1071. }
  1072. return 0;
  1073. }
  1074. static int rmi_probe(struct hid_device *hdev, const struct hid_device_id *id)
  1075. {
  1076. struct rmi_data *data = NULL;
  1077. int ret;
  1078. size_t alloc_size;
  1079. struct hid_report *input_report;
  1080. struct hid_report *output_report;
  1081. struct hid_report *feature_report;
  1082. data = devm_kzalloc(&hdev->dev, sizeof(struct rmi_data), GFP_KERNEL);
  1083. if (!data)
  1084. return -ENOMEM;
  1085. INIT_WORK(&data->reset_work, rmi_reset_work);
  1086. data->hdev = hdev;
  1087. hid_set_drvdata(hdev, data);
  1088. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  1089. ret = hid_parse(hdev);
  1090. if (ret) {
  1091. hid_err(hdev, "parse failed\n");
  1092. return ret;
  1093. }
  1094. if (id->driver_data)
  1095. data->device_flags = id->driver_data;
  1096. /*
  1097. * Check for the RMI specific report ids. If they are misisng
  1098. * simply return and let the events be processed by hid-input
  1099. */
  1100. if (!rmi_check_valid_report_id(hdev, HID_FEATURE_REPORT,
  1101. RMI_SET_RMI_MODE_REPORT_ID, &feature_report)) {
  1102. hid_dbg(hdev, "device does not have set mode feature report\n");
  1103. goto start;
  1104. }
  1105. if (!rmi_check_valid_report_id(hdev, HID_INPUT_REPORT,
  1106. RMI_ATTN_REPORT_ID, &input_report)) {
  1107. hid_dbg(hdev, "device does not have attention input report\n");
  1108. goto start;
  1109. }
  1110. data->input_report_size = hid_report_len(input_report);
  1111. if (!rmi_check_valid_report_id(hdev, HID_OUTPUT_REPORT,
  1112. RMI_WRITE_REPORT_ID, &output_report)) {
  1113. hid_dbg(hdev,
  1114. "device does not have rmi write output report\n");
  1115. goto start;
  1116. }
  1117. data->output_report_size = hid_report_len(output_report);
  1118. data->device_flags |= RMI_DEVICE;
  1119. alloc_size = data->output_report_size + data->input_report_size;
  1120. data->writeReport = devm_kzalloc(&hdev->dev, alloc_size, GFP_KERNEL);
  1121. if (!data->writeReport) {
  1122. ret = -ENOMEM;
  1123. return ret;
  1124. }
  1125. data->readReport = data->writeReport + data->output_report_size;
  1126. init_waitqueue_head(&data->wait);
  1127. mutex_init(&data->page_mutex);
  1128. start:
  1129. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  1130. if (ret) {
  1131. hid_err(hdev, "hw start failed\n");
  1132. return ret;
  1133. }
  1134. if ((data->device_flags & RMI_DEVICE) &&
  1135. !test_bit(RMI_STARTED, &data->flags))
  1136. /*
  1137. * The device maybe in the bootloader if rmi_input_configured
  1138. * failed to find F11 in the PDT. Print an error, but don't
  1139. * return an error from rmi_probe so that hidraw will be
  1140. * accessible from userspace. That way a userspace tool
  1141. * can be used to reload working firmware on the touchpad.
  1142. */
  1143. hid_err(hdev, "Device failed to be properly configured\n");
  1144. return 0;
  1145. }
  1146. static void rmi_remove(struct hid_device *hdev)
  1147. {
  1148. struct rmi_data *hdata = hid_get_drvdata(hdev);
  1149. clear_bit(RMI_STARTED, &hdata->flags);
  1150. hid_hw_stop(hdev);
  1151. }
  1152. static const struct hid_device_id rmi_id[] = {
  1153. { HID_USB_DEVICE(USB_VENDOR_ID_RAZER, USB_DEVICE_ID_RAZER_BLADE_14),
  1154. .driver_data = RMI_DEVICE_HAS_PHYS_BUTTONS },
  1155. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_RMI, HID_ANY_ID, HID_ANY_ID) },
  1156. { }
  1157. };
  1158. MODULE_DEVICE_TABLE(hid, rmi_id);
  1159. static struct hid_driver rmi_driver = {
  1160. .name = "hid-rmi",
  1161. .id_table = rmi_id,
  1162. .probe = rmi_probe,
  1163. .remove = rmi_remove,
  1164. .event = rmi_event,
  1165. .raw_event = rmi_raw_event,
  1166. .input_mapping = rmi_input_mapping,
  1167. .input_configured = rmi_input_configured,
  1168. #ifdef CONFIG_PM
  1169. .suspend = rmi_suspend,
  1170. .resume = rmi_post_resume,
  1171. .reset_resume = rmi_post_reset,
  1172. #endif
  1173. };
  1174. module_hid_driver(rmi_driver);
  1175. MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
  1176. MODULE_DESCRIPTION("RMI HID driver");
  1177. MODULE_LICENSE("GPL");