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
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)
13 #include <linux/kernel.h>
14 #include <linux/hid.h>
15 #include <linux/input.h>
16 #include <linux/input/mt.h>
17 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/wait.h>
21 #include <linux/sched.h>
24 #define RMI_MOUSE_REPORT_ID 0x01 /* Mouse emulation Report */
25 #define RMI_WRITE_REPORT_ID 0x09 /* Output Report */
26 #define RMI_READ_ADDR_REPORT_ID 0x0a /* Output Report */
27 #define RMI_READ_DATA_REPORT_ID 0x0b /* Input Report */
28 #define RMI_ATTN_REPORT_ID 0x0c /* Input Report */
29 #define RMI_SET_RMI_MODE_REPORT_ID 0x0f /* Feature Report */
32 #define RMI_READ_REQUEST_PENDING 0
33 #define RMI_READ_DATA_PENDING 1
37 #define RMI_DEVICE BIT(0)
38 #define RMI_DEVICE_HAS_PHYS_BUTTONS BIT(1)
42 RMI_MODE_ATTN_REPORTS
= 1,
43 RMI_MODE_NO_PACKED_ATTN_REPORTS
= 2,
47 unsigned page
; /* page of the function */
48 u16 query_base_addr
; /* base address for queries */
49 u16 command_base_addr
; /* base address for commands */
50 u16 control_base_addr
; /* base address for controls */
51 u16 data_base_addr
; /* base address for datas */
52 unsigned int interrupt_base
; /* cross-function interrupt number
53 * (uniq in the device)*/
54 unsigned int interrupt_count
; /* number of interrupts */
55 unsigned int report_size
; /* size of a report */
56 unsigned long irq_mask
; /* mask of the interrupts
57 * (to be applied against ATTN IRQ) */
61 * struct rmi_data - stores information for hid communication
63 * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
64 * @page: Keeps track of the current virtual page
66 * @wait: Used for waiting for read data
68 * @writeReport: output buffer when writing RMI registers
69 * @readReport: input buffer when reading RMI registers
71 * @input_report_size: size of an input report (advertised by HID)
72 * @output_report_size: size of an output report (advertised by HID)
74 * @flags: flags for the current device (started, reading, etc...)
76 * @f11: placeholder of internal RMI function F11 description
77 * @f30: placeholder of internal RMI function F30 description
79 * @max_fingers: maximum finger count reported by the device
80 * @max_x: maximum x value reported by the device
81 * @max_y: maximum y value reported by the device
83 * @gpio_led_count: count of GPIOs + LEDs reported by F30
84 * @button_count: actual physical buttons count
85 * @button_mask: button mask used to decode GPIO ATTN reports
86 * @button_state_mask: pull state of the buttons
88 * @input: pointer to the kernel input device
90 * @reset_work: worker which will be called in case of a mouse report
91 * @hdev: pointer to the struct hid_device
94 struct mutex page_mutex
;
97 wait_queue_head_t wait
;
102 int input_report_size
;
103 int output_report_size
;
107 struct rmi_function f01
;
108 struct rmi_function f11
;
109 struct rmi_function f30
;
111 unsigned int max_fingers
;
114 unsigned int x_size_mm
;
115 unsigned int y_size_mm
;
117 unsigned int gpio_led_count
;
118 unsigned int button_count
;
119 unsigned long button_mask
;
120 unsigned long button_state_mask
;
122 struct input_dev
*input
;
124 struct work_struct reset_work
;
125 struct hid_device
*hdev
;
127 unsigned long device_flags
;
128 unsigned long firmware_id
;
131 #define RMI_PAGE(addr) (((addr) >> 8) & 0xff)
133 static int rmi_write_report(struct hid_device
*hdev
, u8
*report
, int len
);
136 * rmi_set_page - Set RMI page
137 * @hdev: The pointer to the hid_device struct
138 * @page: The new page address.
140 * RMI devices have 16-bit addressing, but some of the physical
141 * implementations (like SMBus) only have 8-bit addressing. So RMI implements
142 * a page address at 0xff of every page so we can reliable page addresses
143 * every 256 registers.
145 * The page_mutex lock must be held when this function is entered.
147 * Returns zero on success, non-zero on failure.
149 static int rmi_set_page(struct hid_device
*hdev
, u8 page
)
151 struct rmi_data
*data
= hid_get_drvdata(hdev
);
154 data
->writeReport
[0] = RMI_WRITE_REPORT_ID
;
155 data
->writeReport
[1] = 1;
156 data
->writeReport
[2] = 0xFF;
157 data
->writeReport
[4] = page
;
159 retval
= rmi_write_report(hdev
, data
->writeReport
,
160 data
->output_report_size
);
161 if (retval
!= data
->output_report_size
) {
163 "%s: set page failed: %d.", __func__
, retval
);
171 static int rmi_set_mode(struct hid_device
*hdev
, u8 mode
)
174 u8 txbuf
[2] = {RMI_SET_RMI_MODE_REPORT_ID
, mode
};
176 ret
= hid_hw_raw_request(hdev
, RMI_SET_RMI_MODE_REPORT_ID
, txbuf
,
177 sizeof(txbuf
), HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
179 dev_err(&hdev
->dev
, "unable to set rmi mode to %d (%d)\n", mode
,
187 static int rmi_write_report(struct hid_device
*hdev
, u8
*report
, int len
)
191 ret
= hid_hw_output_report(hdev
, (void *)report
, len
);
193 dev_err(&hdev
->dev
, "failed to write hid report (%d)\n", ret
);
200 static int rmi_read_block(struct hid_device
*hdev
, u16 addr
, void *buf
,
203 struct rmi_data
*data
= hid_get_drvdata(hdev
);
208 int read_input_count
;
210 mutex_lock(&data
->page_mutex
);
212 if (RMI_PAGE(addr
) != data
->page
) {
213 ret
= rmi_set_page(hdev
, RMI_PAGE(addr
));
218 for (retries
= 5; retries
> 0; retries
--) {
219 data
->writeReport
[0] = RMI_READ_ADDR_REPORT_ID
;
220 data
->writeReport
[1] = 0; /* old 1 byte read count */
221 data
->writeReport
[2] = addr
& 0xFF;
222 data
->writeReport
[3] = (addr
>> 8) & 0xFF;
223 data
->writeReport
[4] = len
& 0xFF;
224 data
->writeReport
[5] = (len
>> 8) & 0xFF;
226 set_bit(RMI_READ_REQUEST_PENDING
, &data
->flags
);
228 ret
= rmi_write_report(hdev
, data
->writeReport
,
229 data
->output_report_size
);
230 if (ret
!= data
->output_report_size
) {
231 clear_bit(RMI_READ_REQUEST_PENDING
, &data
->flags
);
233 "failed to write request output report (%d)\n",
240 while (bytes_read
< len
) {
241 if (!wait_event_timeout(data
->wait
,
242 test_bit(RMI_READ_DATA_PENDING
, &data
->flags
),
243 msecs_to_jiffies(1000))) {
244 hid_warn(hdev
, "%s: timeout elapsed\n",
250 read_input_count
= data
->readReport
[1];
251 memcpy(buf
+ bytes_read
, &data
->readReport
[2],
252 read_input_count
< bytes_needed
?
253 read_input_count
: bytes_needed
);
255 bytes_read
+= read_input_count
;
256 bytes_needed
-= read_input_count
;
257 clear_bit(RMI_READ_DATA_PENDING
, &data
->flags
);
267 clear_bit(RMI_READ_REQUEST_PENDING
, &data
->flags
);
268 mutex_unlock(&data
->page_mutex
);
272 static inline int rmi_read(struct hid_device
*hdev
, u16 addr
, void *buf
)
274 return rmi_read_block(hdev
, addr
, buf
, 1);
277 static int rmi_write_block(struct hid_device
*hdev
, u16 addr
, void *buf
,
280 struct rmi_data
*data
= hid_get_drvdata(hdev
);
283 mutex_lock(&data
->page_mutex
);
285 if (RMI_PAGE(addr
) != data
->page
) {
286 ret
= rmi_set_page(hdev
, RMI_PAGE(addr
));
291 data
->writeReport
[0] = RMI_WRITE_REPORT_ID
;
292 data
->writeReport
[1] = len
;
293 data
->writeReport
[2] = addr
& 0xFF;
294 data
->writeReport
[3] = (addr
>> 8) & 0xFF;
295 memcpy(&data
->writeReport
[4], buf
, len
);
297 ret
= rmi_write_report(hdev
, data
->writeReport
,
298 data
->output_report_size
);
301 "failed to write request output report (%d)\n",
308 mutex_unlock(&data
->page_mutex
);
312 static inline int rmi_write(struct hid_device
*hdev
, u16 addr
, void *buf
)
314 return rmi_write_block(hdev
, addr
, buf
, 1);
317 static void rmi_f11_process_touch(struct rmi_data
*hdata
, int slot
,
318 u8 finger_state
, u8
*touch_data
)
321 int wide
, major
, minor
;
324 input_mt_slot(hdata
->input
, slot
);
325 input_mt_report_slot_state(hdata
->input
, MT_TOOL_FINGER
,
326 finger_state
== 0x01);
327 if (finger_state
== 0x01) {
328 x
= (touch_data
[0] << 4) | (touch_data
[2] & 0x0F);
329 y
= (touch_data
[1] << 4) | (touch_data
[2] >> 4);
330 wx
= touch_data
[3] & 0x0F;
331 wy
= touch_data
[3] >> 4;
338 y
= hdata
->max_y
- y
;
340 input_event(hdata
->input
, EV_ABS
, ABS_MT_POSITION_X
, x
);
341 input_event(hdata
->input
, EV_ABS
, ABS_MT_POSITION_Y
, y
);
342 input_event(hdata
->input
, EV_ABS
, ABS_MT_ORIENTATION
, wide
);
343 input_event(hdata
->input
, EV_ABS
, ABS_MT_PRESSURE
, z
);
344 input_event(hdata
->input
, EV_ABS
, ABS_MT_TOUCH_MAJOR
, major
);
345 input_event(hdata
->input
, EV_ABS
, ABS_MT_TOUCH_MINOR
, minor
);
349 static void rmi_reset_work(struct work_struct
*work
)
351 struct rmi_data
*hdata
= container_of(work
, struct rmi_data
,
354 /* switch the device to RMI if we receive a generic mouse report */
355 rmi_set_mode(hdata
->hdev
, RMI_MODE_ATTN_REPORTS
);
358 static inline int rmi_schedule_reset(struct hid_device
*hdev
)
360 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
361 return schedule_work(&hdata
->reset_work
);
364 static int rmi_f11_input_event(struct hid_device
*hdev
, u8 irq
, u8
*data
,
367 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
371 if (!(irq
& hdata
->f11
.irq_mask
) || size
<= 0)
374 offset
= (hdata
->max_fingers
>> 2) + 1;
375 for (i
= 0; i
< hdata
->max_fingers
; i
++) {
376 int fs_byte_position
= i
>> 2;
377 int fs_bit_position
= (i
& 0x3) << 1;
378 int finger_state
= (data
[fs_byte_position
] >> fs_bit_position
) &
380 int position
= offset
+ 5 * i
;
382 if (position
+ 5 > size
) {
383 /* partial report, go on with what we received */
384 printk_once(KERN_WARNING
385 "%s %s: Detected incomplete finger report. Finger reports may occasionally get dropped on this platform.\n",
386 dev_driver_string(&hdev
->dev
),
387 dev_name(&hdev
->dev
));
388 hid_dbg(hdev
, "Incomplete finger report\n");
392 rmi_f11_process_touch(hdata
, i
, finger_state
, &data
[position
]);
394 input_mt_sync_frame(hdata
->input
);
395 input_sync(hdata
->input
);
396 return hdata
->f11
.report_size
;
399 static int rmi_f30_input_event(struct hid_device
*hdev
, u8 irq
, u8
*data
,
402 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
407 if (!(irq
& hdata
->f30
.irq_mask
))
410 if (size
< (int)hdata
->f30
.report_size
) {
411 hid_warn(hdev
, "Click Button pressed, but the click data is missing\n");
415 for (i
= 0; i
< hdata
->gpio_led_count
; i
++) {
416 if (test_bit(i
, &hdata
->button_mask
)) {
417 value
= (data
[i
/ 8] >> (i
& 0x07)) & BIT(0);
418 if (test_bit(i
, &hdata
->button_state_mask
))
420 input_event(hdata
->input
, EV_KEY
, BTN_LEFT
+ button
++,
424 return hdata
->f30
.report_size
;
427 static int rmi_input_event(struct hid_device
*hdev
, u8
*data
, int size
)
429 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
430 unsigned long irq_mask
= 0;
433 if (!(test_bit(RMI_STARTED
, &hdata
->flags
)))
436 irq_mask
|= hdata
->f11
.irq_mask
;
437 irq_mask
|= hdata
->f30
.irq_mask
;
439 if (data
[1] & ~irq_mask
)
440 hid_dbg(hdev
, "unknown intr source:%02lx %s:%d\n",
441 data
[1] & ~irq_mask
, __FILE__
, __LINE__
);
443 if (hdata
->f11
.interrupt_base
< hdata
->f30
.interrupt_base
) {
444 index
+= rmi_f11_input_event(hdev
, data
[1], &data
[index
],
446 index
+= rmi_f30_input_event(hdev
, data
[1], &data
[index
],
449 index
+= rmi_f30_input_event(hdev
, data
[1], &data
[index
],
451 index
+= rmi_f11_input_event(hdev
, data
[1], &data
[index
],
458 static int rmi_read_data_event(struct hid_device
*hdev
, u8
*data
, int size
)
460 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
462 if (!test_bit(RMI_READ_REQUEST_PENDING
, &hdata
->flags
)) {
463 hid_dbg(hdev
, "no read request pending\n");
467 memcpy(hdata
->readReport
, data
, size
< hdata
->input_report_size
?
468 size
: hdata
->input_report_size
);
469 set_bit(RMI_READ_DATA_PENDING
, &hdata
->flags
);
470 wake_up(&hdata
->wait
);
475 static int rmi_check_sanity(struct hid_device
*hdev
, u8
*data
, int size
)
477 int valid_size
= size
;
479 * On the Dell XPS 13 9333, the bus sometimes get confused and fills
480 * the report with a sentinel value "ff". Synaptics told us that such
481 * behavior does not comes from the touchpad itself, so we filter out
485 while ((data
[valid_size
- 1] == 0xff) && valid_size
> 0)
491 static int rmi_raw_event(struct hid_device
*hdev
,
492 struct hid_report
*report
, u8
*data
, int size
)
494 size
= rmi_check_sanity(hdev
, data
, size
);
499 case RMI_READ_DATA_REPORT_ID
:
500 return rmi_read_data_event(hdev
, data
, size
);
501 case RMI_ATTN_REPORT_ID
:
502 return rmi_input_event(hdev
, data
, size
);
510 static int rmi_event(struct hid_device
*hdev
, struct hid_field
*field
,
511 struct hid_usage
*usage
, __s32 value
)
513 struct rmi_data
*data
= hid_get_drvdata(hdev
);
515 if ((data
->device_flags
& RMI_DEVICE
) &&
516 (field
->application
== HID_GD_POINTER
||
517 field
->application
== HID_GD_MOUSE
)) {
518 if (data
->device_flags
& RMI_DEVICE_HAS_PHYS_BUTTONS
) {
519 if ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_BUTTON
)
522 if ((usage
->hid
== HID_GD_X
|| usage
->hid
== HID_GD_Y
)
527 rmi_schedule_reset(hdev
);
535 static int rmi_post_reset(struct hid_device
*hdev
)
537 return rmi_set_mode(hdev
, RMI_MODE_ATTN_REPORTS
);
540 static int rmi_post_resume(struct hid_device
*hdev
)
542 return rmi_set_mode(hdev
, RMI_MODE_ATTN_REPORTS
);
544 #endif /* CONFIG_PM */
546 #define RMI4_MAX_PAGE 0xff
547 #define RMI4_PAGE_SIZE 0x0100
549 #define PDT_START_SCAN_LOCATION 0x00e9
550 #define PDT_END_SCAN_LOCATION 0x0005
551 #define RMI4_END_OF_PDT(id) ((id) == 0x00 || (id) == 0xff)
554 u8 query_base_addr
:8;
555 u8 command_base_addr
:8;
556 u8 control_base_addr
:8;
558 u8 interrupt_source_count
:3;
560 u8 function_version
:2;
562 u8 function_number
:8;
563 } __attribute__((__packed__
));
565 static inline unsigned long rmi_gen_mask(unsigned irq_base
, unsigned irq_count
)
567 return GENMASK(irq_count
+ irq_base
- 1, irq_base
);
570 static void rmi_register_function(struct rmi_data
*data
,
571 struct pdt_entry
*pdt_entry
, int page
, unsigned interrupt_count
)
573 struct rmi_function
*f
= NULL
;
574 u16 page_base
= page
<< 8;
576 switch (pdt_entry
->function_number
) {
590 f
->query_base_addr
= page_base
| pdt_entry
->query_base_addr
;
591 f
->command_base_addr
= page_base
| pdt_entry
->command_base_addr
;
592 f
->control_base_addr
= page_base
| pdt_entry
->control_base_addr
;
593 f
->data_base_addr
= page_base
| pdt_entry
->data_base_addr
;
594 f
->interrupt_base
= interrupt_count
;
595 f
->interrupt_count
= pdt_entry
->interrupt_source_count
;
596 f
->irq_mask
= rmi_gen_mask(f
->interrupt_base
,
601 static int rmi_scan_pdt(struct hid_device
*hdev
)
603 struct rmi_data
*data
= hid_get_drvdata(hdev
);
604 struct pdt_entry entry
;
606 bool page_has_function
;
610 u16 page_start
, pdt_start
, pdt_end
;
612 hid_info(hdev
, "Scanning PDT...\n");
614 for (page
= 0; (page
<= RMI4_MAX_PAGE
); page
++) {
615 page_start
= RMI4_PAGE_SIZE
* page
;
616 pdt_start
= page_start
+ PDT_START_SCAN_LOCATION
;
617 pdt_end
= page_start
+ PDT_END_SCAN_LOCATION
;
619 page_has_function
= false;
620 for (i
= pdt_start
; i
>= pdt_end
; i
-= sizeof(entry
)) {
621 retval
= rmi_read_block(hdev
, i
, &entry
, sizeof(entry
));
624 "Read of PDT entry at %#06x failed.\n",
629 if (RMI4_END_OF_PDT(entry
.function_number
))
632 page_has_function
= true;
634 hid_info(hdev
, "Found F%02X on page %#04x\n",
635 entry
.function_number
, page
);
637 rmi_register_function(data
, &entry
, page
, interrupt
);
638 interrupt
+= entry
.interrupt_source_count
;
641 if (!page_has_function
)
645 hid_info(hdev
, "%s: Done with PDT scan.\n", __func__
);
652 #define RMI_DEVICE_F01_BASIC_QUERY_LEN 11
654 static int rmi_populate_f01(struct hid_device
*hdev
)
656 struct rmi_data
*data
= hid_get_drvdata(hdev
);
657 u8 basic_queries
[RMI_DEVICE_F01_BASIC_QUERY_LEN
];
663 bool has_ds4_queries
= false;
664 bool has_build_id_query
= false;
665 bool has_package_id_query
= false;
666 u16 query_offset
= data
->f01
.query_base_addr
;
670 ret
= rmi_read_block(hdev
, query_offset
, basic_queries
,
671 RMI_DEVICE_F01_BASIC_QUERY_LEN
);
673 hid_err(hdev
, "Can not read basic queries from Function 0x1.\n");
677 has_lts
= !!(basic_queries
[0] & BIT(2));
678 has_sensor_id
= !!(basic_queries
[1] & BIT(3));
679 has_query42
= !!(basic_queries
[1] & BIT(7));
682 prod_info_addr
= query_offset
+ 6;
692 ret
= rmi_read(hdev
, query_offset
, info
);
694 hid_err(hdev
, "Can not read query42.\n");
697 has_ds4_queries
= !!(info
[0] & BIT(0));
701 if (has_ds4_queries
) {
702 ret
= rmi_read(hdev
, query_offset
, &ds4_query_len
);
704 hid_err(hdev
, "Can not read DS4 Query length.\n");
709 if (ds4_query_len
> 0) {
710 ret
= rmi_read(hdev
, query_offset
, info
);
712 hid_err(hdev
, "Can not read DS4 query.\n");
716 has_package_id_query
= !!(info
[0] & BIT(0));
717 has_build_id_query
= !!(info
[0] & BIT(1));
721 if (has_package_id_query
)
724 if (has_build_id_query
) {
725 ret
= rmi_read_block(hdev
, prod_info_addr
, info
, 3);
727 hid_err(hdev
, "Can not read product info.\n");
731 data
->firmware_id
= info
[1] << 8 | info
[0];
732 data
->firmware_id
+= info
[2] * 65536;
738 static int rmi_populate_f11(struct hid_device
*hdev
)
740 struct rmi_data
*data
= hid_get_drvdata(hdev
);
744 bool has_query10
= false;
749 bool has_query36
= false;
750 bool has_physical_props
;
753 bool has_data40
= false;
754 bool has_dribble
= false;
755 bool has_palm_detect
= false;
756 unsigned x_size
, y_size
;
759 if (!data
->f11
.query_base_addr
) {
760 hid_err(hdev
, "No 2D sensor found, giving up.\n");
764 /* query 0 contains some useful information */
765 ret
= rmi_read(hdev
, data
->f11
.query_base_addr
, buf
);
767 hid_err(hdev
, "can not get query 0: %d.\n", ret
);
770 has_query9
= !!(buf
[0] & BIT(3));
771 has_query11
= !!(buf
[0] & BIT(4));
772 has_query12
= !!(buf
[0] & BIT(5));
773 has_query27
= !!(buf
[0] & BIT(6));
774 has_query28
= !!(buf
[0] & BIT(7));
776 /* query 1 to get the max number of fingers */
777 ret
= rmi_read(hdev
, data
->f11
.query_base_addr
+ 1, buf
);
779 hid_err(hdev
, "can not get NumberOfFingers: %d.\n", ret
);
782 data
->max_fingers
= (buf
[0] & 0x07) + 1;
783 if (data
->max_fingers
> 5)
784 data
->max_fingers
= 10;
786 data
->f11
.report_size
= data
->max_fingers
* 5 +
787 DIV_ROUND_UP(data
->max_fingers
, 4);
789 if (!(buf
[0] & BIT(4))) {
790 hid_err(hdev
, "No absolute events, giving up.\n");
794 has_rel
= !!(buf
[0] & BIT(3));
795 has_gestures
= !!(buf
[0] & BIT(5));
797 ret
= rmi_read(hdev
, data
->f11
.query_base_addr
+ 5, buf
);
799 hid_err(hdev
, "can not get absolute data sources: %d.\n", ret
);
803 has_dribble
= !!(buf
[0] & BIT(4));
806 * At least 4 queries are guaranteed to be present in F11
807 * +1 for query 5 which is present since absolute events are
808 * reported and +1 for query 12.
813 ++query_offset
; /* query 6 is present */
816 /* query 8 to find out if query 10 exists */
818 data
->f11
.query_base_addr
+ query_offset
+ 1, buf
);
820 hid_err(hdev
, "can not read gesture information: %d.\n",
824 has_palm_detect
= !!(buf
[0] & BIT(0));
825 has_query10
= !!(buf
[0] & BIT(2));
827 query_offset
+= 2; /* query 7 and 8 are present */
839 /* query 12 to know if the physical properties are reported */
841 ret
= rmi_read(hdev
, data
->f11
.query_base_addr
842 + query_offset
, buf
);
844 hid_err(hdev
, "can not get query 12: %d.\n", ret
);
847 has_physical_props
= !!(buf
[0] & BIT(5));
849 if (has_physical_props
) {
851 ret
= rmi_read_block(hdev
,
852 data
->f11
.query_base_addr
853 + query_offset
, buf
, 4);
855 hid_err(hdev
, "can not read query 15-18: %d.\n",
860 x_size
= buf
[0] | (buf
[1] << 8);
861 y_size
= buf
[2] | (buf
[3] << 8);
863 data
->x_size_mm
= DIV_ROUND_CLOSEST(x_size
, 10);
864 data
->y_size_mm
= DIV_ROUND_CLOSEST(y_size
, 10);
866 hid_info(hdev
, "%s: size in mm: %d x %d\n",
867 __func__
, data
->x_size_mm
, data
->y_size_mm
);
870 * query 15 - 18 contain the size of the sensor
871 * and query 19 - 26 contain bezel dimensions
881 ret
= rmi_read(hdev
, data
->f11
.query_base_addr
882 + query_offset
, buf
);
884 hid_err(hdev
, "can not get query 28: %d.\n", ret
);
888 has_query36
= !!(buf
[0] & BIT(6));
893 ret
= rmi_read(hdev
, data
->f11
.query_base_addr
894 + query_offset
, buf
);
896 hid_err(hdev
, "can not get query 36: %d.\n", ret
);
900 has_data40
= !!(buf
[0] & BIT(5));
905 data
->f11
.report_size
+= data
->max_fingers
* 2;
908 * retrieve the ctrl registers
909 * the ctrl register has a size of 20 but a fw bug split it into 16 + 4,
910 * and there is no way to know if the first 20 bytes are here or not.
911 * We use only the first 12 bytes, so get only them.
913 ret
= rmi_read_block(hdev
, data
->f11
.control_base_addr
, buf
, 12);
915 hid_err(hdev
, "can not read ctrl block of size 11: %d.\n", ret
);
919 data
->max_x
= buf
[6] | (buf
[7] << 8);
920 data
->max_y
= buf
[8] | (buf
[9] << 8);
923 buf
[0] = buf
[0] & ~BIT(6);
924 ret
= rmi_write(hdev
, data
->f11
.control_base_addr
, buf
);
926 hid_err(hdev
, "can not write to control reg 0: %d.\n",
932 if (has_palm_detect
) {
933 buf
[11] = buf
[11] & ~BIT(0);
934 ret
= rmi_write(hdev
, data
->f11
.control_base_addr
+ 11,
937 hid_err(hdev
, "can not write to control reg 11: %d.\n",
946 static int rmi_populate_f30(struct hid_device
*hdev
)
948 struct rmi_data
*data
= hid_get_drvdata(hdev
);
951 bool has_gpio
, has_led
;
952 unsigned bytes_per_ctrl
;
957 /* function F30 is for physical buttons */
958 if (!data
->f30
.query_base_addr
) {
959 hid_err(hdev
, "No GPIO/LEDs found, giving up.\n");
963 ret
= rmi_read_block(hdev
, data
->f30
.query_base_addr
, buf
, 2);
965 hid_err(hdev
, "can not get F30 query registers: %d.\n", ret
);
969 has_gpio
= !!(buf
[0] & BIT(3));
970 has_led
= !!(buf
[0] & BIT(2));
971 data
->gpio_led_count
= buf
[1] & 0x1f;
973 /* retrieve ctrl 2 & 3 registers */
974 bytes_per_ctrl
= (data
->gpio_led_count
+ 7) / 8;
975 /* Ctrl0 is present only if both has_gpio and has_led are set*/
976 ctrl2_addr
= (has_gpio
&& has_led
) ? bytes_per_ctrl
: 0;
977 /* Ctrl1 is always be present */
978 ctrl2_addr
+= bytes_per_ctrl
;
979 ctrl2_3_length
= 2 * bytes_per_ctrl
;
981 data
->f30
.report_size
= bytes_per_ctrl
;
983 ret
= rmi_read_block(hdev
, data
->f30
.control_base_addr
+ ctrl2_addr
,
984 buf
, ctrl2_3_length
);
986 hid_err(hdev
, "can not read ctrl 2&3 block of size %d: %d.\n",
987 ctrl2_3_length
, ret
);
991 for (i
= 0; i
< data
->gpio_led_count
; i
++) {
992 int byte_position
= i
>> 3;
993 int bit_position
= i
& 0x07;
994 u8 dir_byte
= buf
[byte_position
];
995 u8 data_byte
= buf
[byte_position
+ bytes_per_ctrl
];
996 bool dir
= (dir_byte
>> bit_position
) & BIT(0);
997 bool dat
= (data_byte
>> bit_position
) & BIT(0);
1002 /* actual buttons have pull up resistor */
1003 data
->button_count
++;
1004 set_bit(i
, &data
->button_mask
);
1005 set_bit(i
, &data
->button_state_mask
);
1014 static int rmi_populate(struct hid_device
*hdev
)
1016 struct rmi_data
*data
= hid_get_drvdata(hdev
);
1019 ret
= rmi_scan_pdt(hdev
);
1021 hid_err(hdev
, "PDT scan failed with code %d.\n", ret
);
1025 ret
= rmi_populate_f01(hdev
);
1027 hid_err(hdev
, "Error while initializing F01 (%d).\n", ret
);
1031 ret
= rmi_populate_f11(hdev
);
1033 hid_err(hdev
, "Error while initializing F11 (%d).\n", ret
);
1037 if (!(data
->device_flags
& RMI_DEVICE_HAS_PHYS_BUTTONS
)) {
1038 ret
= rmi_populate_f30(hdev
);
1040 hid_warn(hdev
, "Error while initializing F30 (%d).\n", ret
);
1046 static void rmi_input_configured(struct hid_device
*hdev
, struct hid_input
*hi
)
1048 struct rmi_data
*data
= hid_get_drvdata(hdev
);
1049 struct input_dev
*input
= hi
->input
;
1051 int res_x
, res_y
, i
;
1053 data
->input
= input
;
1055 hid_dbg(hdev
, "Opening low level driver\n");
1056 ret
= hid_hw_open(hdev
);
1060 if (!(data
->device_flags
& RMI_DEVICE
))
1063 /* Allow incoming hid reports */
1064 hid_device_io_start(hdev
);
1066 ret
= rmi_set_mode(hdev
, RMI_MODE_ATTN_REPORTS
);
1068 dev_err(&hdev
->dev
, "failed to set rmi mode\n");
1072 ret
= rmi_set_page(hdev
, 0);
1074 dev_err(&hdev
->dev
, "failed to set page select to 0.\n");
1078 ret
= rmi_populate(hdev
);
1082 hid_info(hdev
, "firmware id: %ld\n", data
->firmware_id
);
1084 __set_bit(EV_ABS
, input
->evbit
);
1085 input_set_abs_params(input
, ABS_MT_POSITION_X
, 1, data
->max_x
, 0, 0);
1086 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 1, data
->max_y
, 0, 0);
1088 if (data
->x_size_mm
&& data
->y_size_mm
) {
1089 res_x
= (data
->max_x
- 1) / data
->x_size_mm
;
1090 res_y
= (data
->max_y
- 1) / data
->y_size_mm
;
1092 input_abs_set_res(input
, ABS_MT_POSITION_X
, res_x
);
1093 input_abs_set_res(input
, ABS_MT_POSITION_Y
, res_y
);
1096 input_set_abs_params(input
, ABS_MT_ORIENTATION
, 0, 1, 0, 0);
1097 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0, 0xff, 0, 0);
1098 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
, 0, 0x0f, 0, 0);
1099 input_set_abs_params(input
, ABS_MT_TOUCH_MINOR
, 0, 0x0f, 0, 0);
1101 input_mt_init_slots(input
, data
->max_fingers
, INPUT_MT_POINTER
);
1103 if (data
->button_count
) {
1104 __set_bit(EV_KEY
, input
->evbit
);
1105 for (i
= 0; i
< data
->button_count
; i
++)
1106 __set_bit(BTN_LEFT
+ i
, input
->keybit
);
1108 if (data
->button_count
== 1)
1109 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
1112 set_bit(RMI_STARTED
, &data
->flags
);
1115 hid_device_io_stop(hdev
);
1119 static int rmi_input_mapping(struct hid_device
*hdev
,
1120 struct hid_input
*hi
, struct hid_field
*field
,
1121 struct hid_usage
*usage
, unsigned long **bit
, int *max
)
1123 struct rmi_data
*data
= hid_get_drvdata(hdev
);
1126 * we want to make HID ignore the advertised HID collection
1129 if (data
->device_flags
& RMI_DEVICE
) {
1130 if ((data
->device_flags
& RMI_DEVICE_HAS_PHYS_BUTTONS
) &&
1131 ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_BUTTON
))
1140 static int rmi_check_valid_report_id(struct hid_device
*hdev
, unsigned type
,
1141 unsigned id
, struct hid_report
**report
)
1145 *report
= hdev
->report_enum
[type
].report_id_hash
[id
];
1147 for (i
= 0; i
< (*report
)->maxfield
; i
++) {
1148 unsigned app
= (*report
)->field
[i
]->application
;
1149 if ((app
& HID_USAGE_PAGE
) >= HID_UP_MSVENDOR
)
1157 static int rmi_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
1159 struct rmi_data
*data
= NULL
;
1162 struct hid_report
*input_report
;
1163 struct hid_report
*output_report
;
1164 struct hid_report
*feature_report
;
1166 data
= devm_kzalloc(&hdev
->dev
, sizeof(struct rmi_data
), GFP_KERNEL
);
1170 INIT_WORK(&data
->reset_work
, rmi_reset_work
);
1173 hid_set_drvdata(hdev
, data
);
1175 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
1177 ret
= hid_parse(hdev
);
1179 hid_err(hdev
, "parse failed\n");
1183 if (id
->driver_data
)
1184 data
->device_flags
= id
->driver_data
;
1187 * Check for the RMI specific report ids. If they are misisng
1188 * simply return and let the events be processed by hid-input
1190 if (!rmi_check_valid_report_id(hdev
, HID_FEATURE_REPORT
,
1191 RMI_SET_RMI_MODE_REPORT_ID
, &feature_report
)) {
1192 hid_dbg(hdev
, "device does not have set mode feature report\n");
1196 if (!rmi_check_valid_report_id(hdev
, HID_INPUT_REPORT
,
1197 RMI_ATTN_REPORT_ID
, &input_report
)) {
1198 hid_dbg(hdev
, "device does not have attention input report\n");
1202 data
->input_report_size
= hid_report_len(input_report
);
1204 if (!rmi_check_valid_report_id(hdev
, HID_OUTPUT_REPORT
,
1205 RMI_WRITE_REPORT_ID
, &output_report
)) {
1207 "device does not have rmi write output report\n");
1211 data
->output_report_size
= hid_report_len(output_report
);
1213 data
->device_flags
|= RMI_DEVICE
;
1214 alloc_size
= data
->output_report_size
+ data
->input_report_size
;
1216 data
->writeReport
= devm_kzalloc(&hdev
->dev
, alloc_size
, GFP_KERNEL
);
1217 if (!data
->writeReport
) {
1222 data
->readReport
= data
->writeReport
+ data
->output_report_size
;
1224 init_waitqueue_head(&data
->wait
);
1226 mutex_init(&data
->page_mutex
);
1229 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
1231 hid_err(hdev
, "hw start failed\n");
1235 if ((data
->device_flags
& RMI_DEVICE
) &&
1236 !test_bit(RMI_STARTED
, &data
->flags
))
1238 * The device maybe in the bootloader if rmi_input_configured
1239 * failed to find F11 in the PDT. Print an error, but don't
1240 * return an error from rmi_probe so that hidraw will be
1241 * accessible from userspace. That way a userspace tool
1242 * can be used to reload working firmware on the touchpad.
1244 hid_err(hdev
, "Device failed to be properly configured\n");
1249 static void rmi_remove(struct hid_device
*hdev
)
1251 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
1253 clear_bit(RMI_STARTED
, &hdata
->flags
);
1258 static const struct hid_device_id rmi_id
[] = {
1259 { HID_USB_DEVICE(USB_VENDOR_ID_RAZER
, USB_DEVICE_ID_RAZER_BLADE_14
),
1260 .driver_data
= RMI_DEVICE_HAS_PHYS_BUTTONS
},
1261 { HID_DEVICE(HID_BUS_ANY
, HID_GROUP_RMI
, HID_ANY_ID
, HID_ANY_ID
) },
1264 MODULE_DEVICE_TABLE(hid
, rmi_id
);
1266 static struct hid_driver rmi_driver
= {
1270 .remove
= rmi_remove
,
1272 .raw_event
= rmi_raw_event
,
1273 .input_mapping
= rmi_input_mapping
,
1274 .input_configured
= rmi_input_configured
,
1276 .resume
= rmi_post_resume
,
1277 .reset_resume
= rmi_post_reset
,
1281 module_hid_driver(rmi_driver
);
1283 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
1284 MODULE_DESCRIPTION("RMI HID driver");
1285 MODULE_LICENSE("GPL");