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
36 #define RMI_SLEEP_NORMAL 0x0
37 #define RMI_SLEEP_DEEP_SLEEP 0x1
40 #define RMI_DEVICE BIT(0)
41 #define RMI_DEVICE_HAS_PHYS_BUTTONS BIT(1)
44 * retrieve the ctrl registers
45 * the ctrl register has a size of 20 but a fw bug split it into 16 + 4,
46 * and there is no way to know if the first 20 bytes are here or not.
47 * We use only the first 12 bytes, so get only them.
49 #define RMI_F11_CTRL_REG_COUNT 12
53 RMI_MODE_ATTN_REPORTS
= 1,
54 RMI_MODE_NO_PACKED_ATTN_REPORTS
= 2,
58 unsigned page
; /* page of the function */
59 u16 query_base_addr
; /* base address for queries */
60 u16 command_base_addr
; /* base address for commands */
61 u16 control_base_addr
; /* base address for controls */
62 u16 data_base_addr
; /* base address for datas */
63 unsigned int interrupt_base
; /* cross-function interrupt number
64 * (uniq in the device)*/
65 unsigned int interrupt_count
; /* number of interrupts */
66 unsigned int report_size
; /* size of a report */
67 unsigned long irq_mask
; /* mask of the interrupts
68 * (to be applied against ATTN IRQ) */
72 * struct rmi_data - stores information for hid communication
74 * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
75 * @page: Keeps track of the current virtual page
77 * @wait: Used for waiting for read data
79 * @writeReport: output buffer when writing RMI registers
80 * @readReport: input buffer when reading RMI registers
82 * @input_report_size: size of an input report (advertised by HID)
83 * @output_report_size: size of an output report (advertised by HID)
85 * @flags: flags for the current device (started, reading, etc...)
87 * @f11: placeholder of internal RMI function F11 description
88 * @f30: placeholder of internal RMI function F30 description
90 * @max_fingers: maximum finger count reported by the device
91 * @max_x: maximum x value reported by the device
92 * @max_y: maximum y value reported by the device
94 * @gpio_led_count: count of GPIOs + LEDs reported by F30
95 * @button_count: actual physical buttons count
96 * @button_mask: button mask used to decode GPIO ATTN reports
97 * @button_state_mask: pull state of the buttons
99 * @input: pointer to the kernel input device
101 * @reset_work: worker which will be called in case of a mouse report
102 * @hdev: pointer to the struct hid_device
105 struct mutex page_mutex
;
108 wait_queue_head_t wait
;
113 int input_report_size
;
114 int output_report_size
;
118 struct rmi_function f01
;
119 struct rmi_function f11
;
120 struct rmi_function f30
;
122 unsigned int max_fingers
;
125 unsigned int x_size_mm
;
126 unsigned int y_size_mm
;
127 bool read_f11_ctrl_regs
;
128 u8 f11_ctrl_regs
[RMI_F11_CTRL_REG_COUNT
];
130 unsigned int gpio_led_count
;
131 unsigned int button_count
;
132 unsigned long button_mask
;
133 unsigned long button_state_mask
;
135 struct input_dev
*input
;
137 struct work_struct reset_work
;
138 struct hid_device
*hdev
;
140 unsigned long device_flags
;
141 unsigned long firmware_id
;
144 u8 interrupt_enable_mask
;
145 bool restore_interrupt_mask
;
148 #define RMI_PAGE(addr) (((addr) >> 8) & 0xff)
150 static int rmi_write_report(struct hid_device
*hdev
, u8
*report
, int len
);
153 * rmi_set_page - Set RMI page
154 * @hdev: The pointer to the hid_device struct
155 * @page: The new page address.
157 * RMI devices have 16-bit addressing, but some of the physical
158 * implementations (like SMBus) only have 8-bit addressing. So RMI implements
159 * a page address at 0xff of every page so we can reliable page addresses
160 * every 256 registers.
162 * The page_mutex lock must be held when this function is entered.
164 * Returns zero on success, non-zero on failure.
166 static int rmi_set_page(struct hid_device
*hdev
, u8 page
)
168 struct rmi_data
*data
= hid_get_drvdata(hdev
);
171 data
->writeReport
[0] = RMI_WRITE_REPORT_ID
;
172 data
->writeReport
[1] = 1;
173 data
->writeReport
[2] = 0xFF;
174 data
->writeReport
[4] = page
;
176 retval
= rmi_write_report(hdev
, data
->writeReport
,
177 data
->output_report_size
);
178 if (retval
!= data
->output_report_size
) {
180 "%s: set page failed: %d.", __func__
, retval
);
188 static int rmi_set_mode(struct hid_device
*hdev
, u8 mode
)
191 u8 txbuf
[2] = {RMI_SET_RMI_MODE_REPORT_ID
, mode
};
193 ret
= hid_hw_raw_request(hdev
, RMI_SET_RMI_MODE_REPORT_ID
, txbuf
,
194 sizeof(txbuf
), HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
196 dev_err(&hdev
->dev
, "unable to set rmi mode to %d (%d)\n", mode
,
204 static int rmi_write_report(struct hid_device
*hdev
, u8
*report
, int len
)
208 ret
= hid_hw_output_report(hdev
, (void *)report
, len
);
210 dev_err(&hdev
->dev
, "failed to write hid report (%d)\n", ret
);
217 static int rmi_read_block(struct hid_device
*hdev
, u16 addr
, void *buf
,
220 struct rmi_data
*data
= hid_get_drvdata(hdev
);
225 int read_input_count
;
227 mutex_lock(&data
->page_mutex
);
229 if (RMI_PAGE(addr
) != data
->page
) {
230 ret
= rmi_set_page(hdev
, RMI_PAGE(addr
));
235 for (retries
= 5; retries
> 0; retries
--) {
236 data
->writeReport
[0] = RMI_READ_ADDR_REPORT_ID
;
237 data
->writeReport
[1] = 0; /* old 1 byte read count */
238 data
->writeReport
[2] = addr
& 0xFF;
239 data
->writeReport
[3] = (addr
>> 8) & 0xFF;
240 data
->writeReport
[4] = len
& 0xFF;
241 data
->writeReport
[5] = (len
>> 8) & 0xFF;
243 set_bit(RMI_READ_REQUEST_PENDING
, &data
->flags
);
245 ret
= rmi_write_report(hdev
, data
->writeReport
,
246 data
->output_report_size
);
247 if (ret
!= data
->output_report_size
) {
248 clear_bit(RMI_READ_REQUEST_PENDING
, &data
->flags
);
250 "failed to write request output report (%d)\n",
257 while (bytes_read
< len
) {
258 if (!wait_event_timeout(data
->wait
,
259 test_bit(RMI_READ_DATA_PENDING
, &data
->flags
),
260 msecs_to_jiffies(1000))) {
261 hid_warn(hdev
, "%s: timeout elapsed\n",
267 read_input_count
= data
->readReport
[1];
268 memcpy(buf
+ bytes_read
, &data
->readReport
[2],
269 read_input_count
< bytes_needed
?
270 read_input_count
: bytes_needed
);
272 bytes_read
+= read_input_count
;
273 bytes_needed
-= read_input_count
;
274 clear_bit(RMI_READ_DATA_PENDING
, &data
->flags
);
284 clear_bit(RMI_READ_REQUEST_PENDING
, &data
->flags
);
285 mutex_unlock(&data
->page_mutex
);
289 static inline int rmi_read(struct hid_device
*hdev
, u16 addr
, void *buf
)
291 return rmi_read_block(hdev
, addr
, buf
, 1);
294 static int rmi_write_block(struct hid_device
*hdev
, u16 addr
, void *buf
,
297 struct rmi_data
*data
= hid_get_drvdata(hdev
);
300 mutex_lock(&data
->page_mutex
);
302 if (RMI_PAGE(addr
) != data
->page
) {
303 ret
= rmi_set_page(hdev
, RMI_PAGE(addr
));
308 data
->writeReport
[0] = RMI_WRITE_REPORT_ID
;
309 data
->writeReport
[1] = len
;
310 data
->writeReport
[2] = addr
& 0xFF;
311 data
->writeReport
[3] = (addr
>> 8) & 0xFF;
312 memcpy(&data
->writeReport
[4], buf
, len
);
314 ret
= rmi_write_report(hdev
, data
->writeReport
,
315 data
->output_report_size
);
318 "failed to write request output report (%d)\n",
325 mutex_unlock(&data
->page_mutex
);
329 static inline int rmi_write(struct hid_device
*hdev
, u16 addr
, void *buf
)
331 return rmi_write_block(hdev
, addr
, buf
, 1);
334 static void rmi_f11_process_touch(struct rmi_data
*hdata
, int slot
,
335 u8 finger_state
, u8
*touch_data
)
338 int wide
, major
, minor
;
341 input_mt_slot(hdata
->input
, slot
);
342 input_mt_report_slot_state(hdata
->input
, MT_TOOL_FINGER
,
343 finger_state
== 0x01);
344 if (finger_state
== 0x01) {
345 x
= (touch_data
[0] << 4) | (touch_data
[2] & 0x0F);
346 y
= (touch_data
[1] << 4) | (touch_data
[2] >> 4);
347 wx
= touch_data
[3] & 0x0F;
348 wy
= touch_data
[3] >> 4;
355 y
= hdata
->max_y
- y
;
357 input_event(hdata
->input
, EV_ABS
, ABS_MT_POSITION_X
, x
);
358 input_event(hdata
->input
, EV_ABS
, ABS_MT_POSITION_Y
, y
);
359 input_event(hdata
->input
, EV_ABS
, ABS_MT_ORIENTATION
, wide
);
360 input_event(hdata
->input
, EV_ABS
, ABS_MT_PRESSURE
, z
);
361 input_event(hdata
->input
, EV_ABS
, ABS_MT_TOUCH_MAJOR
, major
);
362 input_event(hdata
->input
, EV_ABS
, ABS_MT_TOUCH_MINOR
, minor
);
366 static int rmi_reset_attn_mode(struct hid_device
*hdev
)
368 struct rmi_data
*data
= hid_get_drvdata(hdev
);
371 ret
= rmi_set_mode(hdev
, RMI_MODE_ATTN_REPORTS
);
375 if (data
->restore_interrupt_mask
) {
376 ret
= rmi_write(hdev
, data
->f01
.control_base_addr
+ 1,
377 &data
->interrupt_enable_mask
);
379 hid_err(hdev
, "can not write F01 control register\n");
387 static void rmi_reset_work(struct work_struct
*work
)
389 struct rmi_data
*hdata
= container_of(work
, struct rmi_data
,
392 /* switch the device to RMI if we receive a generic mouse report */
393 rmi_reset_attn_mode(hdata
->hdev
);
396 static inline int rmi_schedule_reset(struct hid_device
*hdev
)
398 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
399 return schedule_work(&hdata
->reset_work
);
402 static int rmi_f11_input_event(struct hid_device
*hdev
, u8 irq
, u8
*data
,
405 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
409 if (!(irq
& hdata
->f11
.irq_mask
) || size
<= 0)
412 offset
= (hdata
->max_fingers
>> 2) + 1;
413 for (i
= 0; i
< hdata
->max_fingers
; i
++) {
414 int fs_byte_position
= i
>> 2;
415 int fs_bit_position
= (i
& 0x3) << 1;
416 int finger_state
= (data
[fs_byte_position
] >> fs_bit_position
) &
418 int position
= offset
+ 5 * i
;
420 if (position
+ 5 > size
) {
421 /* partial report, go on with what we received */
422 printk_once(KERN_WARNING
423 "%s %s: Detected incomplete finger report. Finger reports may occasionally get dropped on this platform.\n",
424 dev_driver_string(&hdev
->dev
),
425 dev_name(&hdev
->dev
));
426 hid_dbg(hdev
, "Incomplete finger report\n");
430 rmi_f11_process_touch(hdata
, i
, finger_state
, &data
[position
]);
432 input_mt_sync_frame(hdata
->input
);
433 input_sync(hdata
->input
);
434 return hdata
->f11
.report_size
;
437 static int rmi_f30_input_event(struct hid_device
*hdev
, u8 irq
, u8
*data
,
440 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
445 if (!(irq
& hdata
->f30
.irq_mask
))
448 if (size
< (int)hdata
->f30
.report_size
) {
449 hid_warn(hdev
, "Click Button pressed, but the click data is missing\n");
453 for (i
= 0; i
< hdata
->gpio_led_count
; i
++) {
454 if (test_bit(i
, &hdata
->button_mask
)) {
455 value
= (data
[i
/ 8] >> (i
& 0x07)) & BIT(0);
456 if (test_bit(i
, &hdata
->button_state_mask
))
458 input_event(hdata
->input
, EV_KEY
, BTN_LEFT
+ button
++,
462 return hdata
->f30
.report_size
;
465 static int rmi_input_event(struct hid_device
*hdev
, u8
*data
, int size
)
467 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
468 unsigned long irq_mask
= 0;
471 if (!(test_bit(RMI_STARTED
, &hdata
->flags
)))
474 irq_mask
|= hdata
->f11
.irq_mask
;
475 irq_mask
|= hdata
->f30
.irq_mask
;
477 if (data
[1] & ~irq_mask
)
478 hid_dbg(hdev
, "unknown intr source:%02lx %s:%d\n",
479 data
[1] & ~irq_mask
, __FILE__
, __LINE__
);
481 if (hdata
->f11
.interrupt_base
< hdata
->f30
.interrupt_base
) {
482 index
+= rmi_f11_input_event(hdev
, data
[1], &data
[index
],
484 index
+= rmi_f30_input_event(hdev
, data
[1], &data
[index
],
487 index
+= rmi_f30_input_event(hdev
, data
[1], &data
[index
],
489 index
+= rmi_f11_input_event(hdev
, data
[1], &data
[index
],
496 static int rmi_read_data_event(struct hid_device
*hdev
, u8
*data
, int size
)
498 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
500 if (!test_bit(RMI_READ_REQUEST_PENDING
, &hdata
->flags
)) {
501 hid_dbg(hdev
, "no read request pending\n");
505 memcpy(hdata
->readReport
, data
, size
< hdata
->input_report_size
?
506 size
: hdata
->input_report_size
);
507 set_bit(RMI_READ_DATA_PENDING
, &hdata
->flags
);
508 wake_up(&hdata
->wait
);
513 static int rmi_check_sanity(struct hid_device
*hdev
, u8
*data
, int size
)
515 int valid_size
= size
;
517 * On the Dell XPS 13 9333, the bus sometimes get confused and fills
518 * the report with a sentinel value "ff". Synaptics told us that such
519 * behavior does not comes from the touchpad itself, so we filter out
523 while ((data
[valid_size
- 1] == 0xff) && valid_size
> 0)
529 static int rmi_raw_event(struct hid_device
*hdev
,
530 struct hid_report
*report
, u8
*data
, int size
)
532 size
= rmi_check_sanity(hdev
, data
, size
);
537 case RMI_READ_DATA_REPORT_ID
:
538 return rmi_read_data_event(hdev
, data
, size
);
539 case RMI_ATTN_REPORT_ID
:
540 return rmi_input_event(hdev
, data
, size
);
548 static int rmi_event(struct hid_device
*hdev
, struct hid_field
*field
,
549 struct hid_usage
*usage
, __s32 value
)
551 struct rmi_data
*data
= hid_get_drvdata(hdev
);
553 if ((data
->device_flags
& RMI_DEVICE
) &&
554 (field
->application
== HID_GD_POINTER
||
555 field
->application
== HID_GD_MOUSE
)) {
556 if (data
->device_flags
& RMI_DEVICE_HAS_PHYS_BUTTONS
) {
557 if ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_BUTTON
)
560 if ((usage
->hid
== HID_GD_X
|| usage
->hid
== HID_GD_Y
)
565 rmi_schedule_reset(hdev
);
573 static int rmi_set_sleep_mode(struct hid_device
*hdev
, int sleep_mode
)
575 struct rmi_data
*data
= hid_get_drvdata(hdev
);
579 f01_ctrl0
= (data
->f01_ctrl0
& ~0x3) | sleep_mode
;
581 ret
= rmi_write(hdev
, data
->f01
.control_base_addr
,
584 hid_err(hdev
, "can not write sleep mode\n");
591 static int rmi_suspend(struct hid_device
*hdev
, pm_message_t message
)
593 struct rmi_data
*data
= hid_get_drvdata(hdev
);
595 u8 buf
[RMI_F11_CTRL_REG_COUNT
];
597 if (!(data
->device_flags
& RMI_DEVICE
))
600 ret
= rmi_read_block(hdev
, data
->f11
.control_base_addr
, buf
,
601 RMI_F11_CTRL_REG_COUNT
);
603 hid_warn(hdev
, "can not read F11 control registers\n");
605 memcpy(data
->f11_ctrl_regs
, buf
, RMI_F11_CTRL_REG_COUNT
);
608 if (!device_may_wakeup(hdev
->dev
.parent
))
609 return rmi_set_sleep_mode(hdev
, RMI_SLEEP_DEEP_SLEEP
);
614 static int rmi_post_reset(struct hid_device
*hdev
)
616 struct rmi_data
*data
= hid_get_drvdata(hdev
);
619 if (!(data
->device_flags
& RMI_DEVICE
))
622 ret
= rmi_reset_attn_mode(hdev
);
624 hid_err(hdev
, "can not set rmi mode\n");
628 if (data
->read_f11_ctrl_regs
) {
629 ret
= rmi_write_block(hdev
, data
->f11
.control_base_addr
,
630 data
->f11_ctrl_regs
, RMI_F11_CTRL_REG_COUNT
);
633 "can not write F11 control registers after reset\n");
636 if (!device_may_wakeup(hdev
->dev
.parent
)) {
637 ret
= rmi_set_sleep_mode(hdev
, RMI_SLEEP_NORMAL
);
639 hid_err(hdev
, "can not write sleep mode\n");
647 static int rmi_post_resume(struct hid_device
*hdev
)
649 struct rmi_data
*data
= hid_get_drvdata(hdev
);
651 if (!(data
->device_flags
& RMI_DEVICE
))
654 return rmi_reset_attn_mode(hdev
);
656 #endif /* CONFIG_PM */
658 #define RMI4_MAX_PAGE 0xff
659 #define RMI4_PAGE_SIZE 0x0100
661 #define PDT_START_SCAN_LOCATION 0x00e9
662 #define PDT_END_SCAN_LOCATION 0x0005
663 #define RMI4_END_OF_PDT(id) ((id) == 0x00 || (id) == 0xff)
666 u8 query_base_addr
:8;
667 u8 command_base_addr
:8;
668 u8 control_base_addr
:8;
670 u8 interrupt_source_count
:3;
672 u8 function_version
:2;
674 u8 function_number
:8;
675 } __attribute__((__packed__
));
677 static inline unsigned long rmi_gen_mask(unsigned irq_base
, unsigned irq_count
)
679 return GENMASK(irq_count
+ irq_base
- 1, irq_base
);
682 static void rmi_register_function(struct rmi_data
*data
,
683 struct pdt_entry
*pdt_entry
, int page
, unsigned interrupt_count
)
685 struct rmi_function
*f
= NULL
;
686 u16 page_base
= page
<< 8;
688 switch (pdt_entry
->function_number
) {
702 f
->query_base_addr
= page_base
| pdt_entry
->query_base_addr
;
703 f
->command_base_addr
= page_base
| pdt_entry
->command_base_addr
;
704 f
->control_base_addr
= page_base
| pdt_entry
->control_base_addr
;
705 f
->data_base_addr
= page_base
| pdt_entry
->data_base_addr
;
706 f
->interrupt_base
= interrupt_count
;
707 f
->interrupt_count
= pdt_entry
->interrupt_source_count
;
708 f
->irq_mask
= rmi_gen_mask(f
->interrupt_base
,
710 data
->interrupt_enable_mask
|= f
->irq_mask
;
714 static int rmi_scan_pdt(struct hid_device
*hdev
)
716 struct rmi_data
*data
= hid_get_drvdata(hdev
);
717 struct pdt_entry entry
;
719 bool page_has_function
;
723 u16 page_start
, pdt_start
, pdt_end
;
725 hid_info(hdev
, "Scanning PDT...\n");
727 for (page
= 0; (page
<= RMI4_MAX_PAGE
); page
++) {
728 page_start
= RMI4_PAGE_SIZE
* page
;
729 pdt_start
= page_start
+ PDT_START_SCAN_LOCATION
;
730 pdt_end
= page_start
+ PDT_END_SCAN_LOCATION
;
732 page_has_function
= false;
733 for (i
= pdt_start
; i
>= pdt_end
; i
-= sizeof(entry
)) {
734 retval
= rmi_read_block(hdev
, i
, &entry
, sizeof(entry
));
737 "Read of PDT entry at %#06x failed.\n",
742 if (RMI4_END_OF_PDT(entry
.function_number
))
745 page_has_function
= true;
747 hid_info(hdev
, "Found F%02X on page %#04x\n",
748 entry
.function_number
, page
);
750 rmi_register_function(data
, &entry
, page
, interrupt
);
751 interrupt
+= entry
.interrupt_source_count
;
754 if (!page_has_function
)
758 hid_info(hdev
, "%s: Done with PDT scan.\n", __func__
);
765 #define RMI_DEVICE_F01_BASIC_QUERY_LEN 11
767 static int rmi_populate_f01(struct hid_device
*hdev
)
769 struct rmi_data
*data
= hid_get_drvdata(hdev
);
770 u8 basic_queries
[RMI_DEVICE_F01_BASIC_QUERY_LEN
];
776 bool has_ds4_queries
= false;
777 bool has_build_id_query
= false;
778 bool has_package_id_query
= false;
779 u16 query_offset
= data
->f01
.query_base_addr
;
783 ret
= rmi_read_block(hdev
, query_offset
, basic_queries
,
784 RMI_DEVICE_F01_BASIC_QUERY_LEN
);
786 hid_err(hdev
, "Can not read basic queries from Function 0x1.\n");
790 has_lts
= !!(basic_queries
[0] & BIT(2));
791 has_sensor_id
= !!(basic_queries
[1] & BIT(3));
792 has_query42
= !!(basic_queries
[1] & BIT(7));
795 prod_info_addr
= query_offset
+ 6;
805 ret
= rmi_read(hdev
, query_offset
, info
);
807 hid_err(hdev
, "Can not read query42.\n");
810 has_ds4_queries
= !!(info
[0] & BIT(0));
814 if (has_ds4_queries
) {
815 ret
= rmi_read(hdev
, query_offset
, &ds4_query_len
);
817 hid_err(hdev
, "Can not read DS4 Query length.\n");
822 if (ds4_query_len
> 0) {
823 ret
= rmi_read(hdev
, query_offset
, info
);
825 hid_err(hdev
, "Can not read DS4 query.\n");
829 has_package_id_query
= !!(info
[0] & BIT(0));
830 has_build_id_query
= !!(info
[0] & BIT(1));
834 if (has_package_id_query
)
837 if (has_build_id_query
) {
838 ret
= rmi_read_block(hdev
, prod_info_addr
, info
, 3);
840 hid_err(hdev
, "Can not read product info.\n");
844 data
->firmware_id
= info
[1] << 8 | info
[0];
845 data
->firmware_id
+= info
[2] * 65536;
848 ret
= rmi_read_block(hdev
, data
->f01
.control_base_addr
, info
,
852 hid_err(hdev
, "can not read f01 ctrl registers\n");
856 data
->f01_ctrl0
= info
[0];
860 * Do to a firmware bug in some touchpads the F01 interrupt
861 * enable control register will be cleared on reset.
862 * This will stop the touchpad from reporting data, so
863 * if F01 CTRL1 is 0 then we need to explicitly enable
864 * interrupts for the functions we want data for.
866 data
->restore_interrupt_mask
= true;
868 ret
= rmi_write(hdev
, data
->f01
.control_base_addr
+ 1,
869 &data
->interrupt_enable_mask
);
871 hid_err(hdev
, "can not write to control reg 1: %d.\n",
880 static int rmi_populate_f11(struct hid_device
*hdev
)
882 struct rmi_data
*data
= hid_get_drvdata(hdev
);
886 bool has_query10
= false;
891 bool has_query36
= false;
892 bool has_physical_props
;
895 bool has_data40
= false;
896 bool has_dribble
= false;
897 bool has_palm_detect
= false;
898 unsigned x_size
, y_size
;
901 if (!data
->f11
.query_base_addr
) {
902 hid_err(hdev
, "No 2D sensor found, giving up.\n");
906 /* query 0 contains some useful information */
907 ret
= rmi_read(hdev
, data
->f11
.query_base_addr
, buf
);
909 hid_err(hdev
, "can not get query 0: %d.\n", ret
);
912 has_query9
= !!(buf
[0] & BIT(3));
913 has_query11
= !!(buf
[0] & BIT(4));
914 has_query12
= !!(buf
[0] & BIT(5));
915 has_query27
= !!(buf
[0] & BIT(6));
916 has_query28
= !!(buf
[0] & BIT(7));
918 /* query 1 to get the max number of fingers */
919 ret
= rmi_read(hdev
, data
->f11
.query_base_addr
+ 1, buf
);
921 hid_err(hdev
, "can not get NumberOfFingers: %d.\n", ret
);
924 data
->max_fingers
= (buf
[0] & 0x07) + 1;
925 if (data
->max_fingers
> 5)
926 data
->max_fingers
= 10;
928 data
->f11
.report_size
= data
->max_fingers
* 5 +
929 DIV_ROUND_UP(data
->max_fingers
, 4);
931 if (!(buf
[0] & BIT(4))) {
932 hid_err(hdev
, "No absolute events, giving up.\n");
936 has_rel
= !!(buf
[0] & BIT(3));
937 has_gestures
= !!(buf
[0] & BIT(5));
939 ret
= rmi_read(hdev
, data
->f11
.query_base_addr
+ 5, buf
);
941 hid_err(hdev
, "can not get absolute data sources: %d.\n", ret
);
945 has_dribble
= !!(buf
[0] & BIT(4));
948 * At least 4 queries are guaranteed to be present in F11
949 * +1 for query 5 which is present since absolute events are
950 * reported and +1 for query 12.
955 ++query_offset
; /* query 6 is present */
958 /* query 8 to find out if query 10 exists */
960 data
->f11
.query_base_addr
+ query_offset
+ 1, buf
);
962 hid_err(hdev
, "can not read gesture information: %d.\n",
966 has_palm_detect
= !!(buf
[0] & BIT(0));
967 has_query10
= !!(buf
[0] & BIT(2));
969 query_offset
+= 2; /* query 7 and 8 are present */
981 /* query 12 to know if the physical properties are reported */
983 ret
= rmi_read(hdev
, data
->f11
.query_base_addr
984 + query_offset
, buf
);
986 hid_err(hdev
, "can not get query 12: %d.\n", ret
);
989 has_physical_props
= !!(buf
[0] & BIT(5));
991 if (has_physical_props
) {
993 ret
= rmi_read_block(hdev
,
994 data
->f11
.query_base_addr
995 + query_offset
, buf
, 4);
997 hid_err(hdev
, "can not read query 15-18: %d.\n",
1002 x_size
= buf
[0] | (buf
[1] << 8);
1003 y_size
= buf
[2] | (buf
[3] << 8);
1005 data
->x_size_mm
= DIV_ROUND_CLOSEST(x_size
, 10);
1006 data
->y_size_mm
= DIV_ROUND_CLOSEST(y_size
, 10);
1008 hid_info(hdev
, "%s: size in mm: %d x %d\n",
1009 __func__
, data
->x_size_mm
, data
->y_size_mm
);
1012 * query 15 - 18 contain the size of the sensor
1013 * and query 19 - 26 contain bezel dimensions
1023 ret
= rmi_read(hdev
, data
->f11
.query_base_addr
1024 + query_offset
, buf
);
1026 hid_err(hdev
, "can not get query 28: %d.\n", ret
);
1030 has_query36
= !!(buf
[0] & BIT(6));
1035 ret
= rmi_read(hdev
, data
->f11
.query_base_addr
1036 + query_offset
, buf
);
1038 hid_err(hdev
, "can not get query 36: %d.\n", ret
);
1042 has_data40
= !!(buf
[0] & BIT(5));
1047 data
->f11
.report_size
+= data
->max_fingers
* 2;
1049 ret
= rmi_read_block(hdev
, data
->f11
.control_base_addr
,
1050 data
->f11_ctrl_regs
, RMI_F11_CTRL_REG_COUNT
);
1052 hid_err(hdev
, "can not read ctrl block of size 11: %d.\n", ret
);
1056 /* data->f11_ctrl_regs now contains valid register data */
1057 data
->read_f11_ctrl_regs
= true;
1059 data
->max_x
= data
->f11_ctrl_regs
[6] | (data
->f11_ctrl_regs
[7] << 8);
1060 data
->max_y
= data
->f11_ctrl_regs
[8] | (data
->f11_ctrl_regs
[9] << 8);
1063 data
->f11_ctrl_regs
[0] = data
->f11_ctrl_regs
[0] & ~BIT(6);
1064 ret
= rmi_write(hdev
, data
->f11
.control_base_addr
,
1065 data
->f11_ctrl_regs
);
1067 hid_err(hdev
, "can not write to control reg 0: %d.\n",
1073 if (has_palm_detect
) {
1074 data
->f11_ctrl_regs
[11] = data
->f11_ctrl_regs
[11] & ~BIT(0);
1075 ret
= rmi_write(hdev
, data
->f11
.control_base_addr
+ 11,
1076 &data
->f11_ctrl_regs
[11]);
1078 hid_err(hdev
, "can not write to control reg 11: %d.\n",
1087 static int rmi_populate_f30(struct hid_device
*hdev
)
1089 struct rmi_data
*data
= hid_get_drvdata(hdev
);
1092 bool has_gpio
, has_led
;
1093 unsigned bytes_per_ctrl
;
1098 /* function F30 is for physical buttons */
1099 if (!data
->f30
.query_base_addr
) {
1100 hid_err(hdev
, "No GPIO/LEDs found, giving up.\n");
1104 ret
= rmi_read_block(hdev
, data
->f30
.query_base_addr
, buf
, 2);
1106 hid_err(hdev
, "can not get F30 query registers: %d.\n", ret
);
1110 has_gpio
= !!(buf
[0] & BIT(3));
1111 has_led
= !!(buf
[0] & BIT(2));
1112 data
->gpio_led_count
= buf
[1] & 0x1f;
1114 /* retrieve ctrl 2 & 3 registers */
1115 bytes_per_ctrl
= (data
->gpio_led_count
+ 7) / 8;
1116 /* Ctrl0 is present only if both has_gpio and has_led are set*/
1117 ctrl2_addr
= (has_gpio
&& has_led
) ? bytes_per_ctrl
: 0;
1118 /* Ctrl1 is always be present */
1119 ctrl2_addr
+= bytes_per_ctrl
;
1120 ctrl2_3_length
= 2 * bytes_per_ctrl
;
1122 data
->f30
.report_size
= bytes_per_ctrl
;
1124 ret
= rmi_read_block(hdev
, data
->f30
.control_base_addr
+ ctrl2_addr
,
1125 buf
, ctrl2_3_length
);
1127 hid_err(hdev
, "can not read ctrl 2&3 block of size %d: %d.\n",
1128 ctrl2_3_length
, ret
);
1132 for (i
= 0; i
< data
->gpio_led_count
; i
++) {
1133 int byte_position
= i
>> 3;
1134 int bit_position
= i
& 0x07;
1135 u8 dir_byte
= buf
[byte_position
];
1136 u8 data_byte
= buf
[byte_position
+ bytes_per_ctrl
];
1137 bool dir
= (dir_byte
>> bit_position
) & BIT(0);
1138 bool dat
= (data_byte
>> bit_position
) & BIT(0);
1143 /* actual buttons have pull up resistor */
1144 data
->button_count
++;
1145 set_bit(i
, &data
->button_mask
);
1146 set_bit(i
, &data
->button_state_mask
);
1155 static int rmi_populate(struct hid_device
*hdev
)
1157 struct rmi_data
*data
= hid_get_drvdata(hdev
);
1160 ret
= rmi_scan_pdt(hdev
);
1162 hid_err(hdev
, "PDT scan failed with code %d.\n", ret
);
1166 ret
= rmi_populate_f01(hdev
);
1168 hid_err(hdev
, "Error while initializing F01 (%d).\n", ret
);
1172 ret
= rmi_populate_f11(hdev
);
1174 hid_err(hdev
, "Error while initializing F11 (%d).\n", ret
);
1178 if (!(data
->device_flags
& RMI_DEVICE_HAS_PHYS_BUTTONS
)) {
1179 ret
= rmi_populate_f30(hdev
);
1181 hid_warn(hdev
, "Error while initializing F30 (%d).\n", ret
);
1187 static int rmi_input_configured(struct hid_device
*hdev
, struct hid_input
*hi
)
1189 struct rmi_data
*data
= hid_get_drvdata(hdev
);
1190 struct input_dev
*input
= hi
->input
;
1192 int res_x
, res_y
, i
;
1194 data
->input
= input
;
1196 hid_dbg(hdev
, "Opening low level driver\n");
1197 ret
= hid_hw_open(hdev
);
1201 if (!(data
->device_flags
& RMI_DEVICE
))
1204 /* Allow incoming hid reports */
1205 hid_device_io_start(hdev
);
1207 ret
= rmi_set_mode(hdev
, RMI_MODE_ATTN_REPORTS
);
1209 dev_err(&hdev
->dev
, "failed to set rmi mode\n");
1213 ret
= rmi_set_page(hdev
, 0);
1215 dev_err(&hdev
->dev
, "failed to set page select to 0.\n");
1219 ret
= rmi_populate(hdev
);
1223 hid_info(hdev
, "firmware id: %ld\n", data
->firmware_id
);
1225 __set_bit(EV_ABS
, input
->evbit
);
1226 input_set_abs_params(input
, ABS_MT_POSITION_X
, 1, data
->max_x
, 0, 0);
1227 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 1, data
->max_y
, 0, 0);
1229 if (data
->x_size_mm
&& data
->y_size_mm
) {
1230 res_x
= (data
->max_x
- 1) / data
->x_size_mm
;
1231 res_y
= (data
->max_y
- 1) / data
->y_size_mm
;
1233 input_abs_set_res(input
, ABS_MT_POSITION_X
, res_x
);
1234 input_abs_set_res(input
, ABS_MT_POSITION_Y
, res_y
);
1237 input_set_abs_params(input
, ABS_MT_ORIENTATION
, 0, 1, 0, 0);
1238 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0, 0xff, 0, 0);
1239 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
, 0, 0x0f, 0, 0);
1240 input_set_abs_params(input
, ABS_MT_TOUCH_MINOR
, 0, 0x0f, 0, 0);
1242 ret
= input_mt_init_slots(input
, data
->max_fingers
, INPUT_MT_POINTER
);
1246 if (data
->button_count
) {
1247 __set_bit(EV_KEY
, input
->evbit
);
1248 for (i
= 0; i
< data
->button_count
; i
++)
1249 __set_bit(BTN_LEFT
+ i
, input
->keybit
);
1251 if (data
->button_count
== 1)
1252 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
1255 set_bit(RMI_STARTED
, &data
->flags
);
1258 hid_device_io_stop(hdev
);
1263 static int rmi_input_mapping(struct hid_device
*hdev
,
1264 struct hid_input
*hi
, struct hid_field
*field
,
1265 struct hid_usage
*usage
, unsigned long **bit
, int *max
)
1267 struct rmi_data
*data
= hid_get_drvdata(hdev
);
1270 * we want to make HID ignore the advertised HID collection
1273 if (data
->device_flags
& RMI_DEVICE
) {
1274 if ((data
->device_flags
& RMI_DEVICE_HAS_PHYS_BUTTONS
) &&
1275 ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_BUTTON
))
1284 static int rmi_check_valid_report_id(struct hid_device
*hdev
, unsigned type
,
1285 unsigned id
, struct hid_report
**report
)
1289 *report
= hdev
->report_enum
[type
].report_id_hash
[id
];
1291 for (i
= 0; i
< (*report
)->maxfield
; i
++) {
1292 unsigned app
= (*report
)->field
[i
]->application
;
1293 if ((app
& HID_USAGE_PAGE
) >= HID_UP_MSVENDOR
)
1301 static int rmi_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
1303 struct rmi_data
*data
= NULL
;
1306 struct hid_report
*input_report
;
1307 struct hid_report
*output_report
;
1308 struct hid_report
*feature_report
;
1310 data
= devm_kzalloc(&hdev
->dev
, sizeof(struct rmi_data
), GFP_KERNEL
);
1314 INIT_WORK(&data
->reset_work
, rmi_reset_work
);
1317 hid_set_drvdata(hdev
, data
);
1319 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
1321 ret
= hid_parse(hdev
);
1323 hid_err(hdev
, "parse failed\n");
1327 if (id
->driver_data
)
1328 data
->device_flags
= id
->driver_data
;
1331 * Check for the RMI specific report ids. If they are misisng
1332 * simply return and let the events be processed by hid-input
1334 if (!rmi_check_valid_report_id(hdev
, HID_FEATURE_REPORT
,
1335 RMI_SET_RMI_MODE_REPORT_ID
, &feature_report
)) {
1336 hid_dbg(hdev
, "device does not have set mode feature report\n");
1340 if (!rmi_check_valid_report_id(hdev
, HID_INPUT_REPORT
,
1341 RMI_ATTN_REPORT_ID
, &input_report
)) {
1342 hid_dbg(hdev
, "device does not have attention input report\n");
1346 data
->input_report_size
= hid_report_len(input_report
);
1348 if (!rmi_check_valid_report_id(hdev
, HID_OUTPUT_REPORT
,
1349 RMI_WRITE_REPORT_ID
, &output_report
)) {
1351 "device does not have rmi write output report\n");
1355 data
->output_report_size
= hid_report_len(output_report
);
1357 data
->device_flags
|= RMI_DEVICE
;
1358 alloc_size
= data
->output_report_size
+ data
->input_report_size
;
1360 data
->writeReport
= devm_kzalloc(&hdev
->dev
, alloc_size
, GFP_KERNEL
);
1361 if (!data
->writeReport
) {
1366 data
->readReport
= data
->writeReport
+ data
->output_report_size
;
1368 init_waitqueue_head(&data
->wait
);
1370 mutex_init(&data
->page_mutex
);
1373 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
1375 hid_err(hdev
, "hw start failed\n");
1379 if ((data
->device_flags
& RMI_DEVICE
) &&
1380 !test_bit(RMI_STARTED
, &data
->flags
))
1382 * The device maybe in the bootloader if rmi_input_configured
1383 * failed to find F11 in the PDT. Print an error, but don't
1384 * return an error from rmi_probe so that hidraw will be
1385 * accessible from userspace. That way a userspace tool
1386 * can be used to reload working firmware on the touchpad.
1388 hid_err(hdev
, "Device failed to be properly configured\n");
1393 static void rmi_remove(struct hid_device
*hdev
)
1395 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
1397 clear_bit(RMI_STARTED
, &hdata
->flags
);
1402 static const struct hid_device_id rmi_id
[] = {
1403 { HID_USB_DEVICE(USB_VENDOR_ID_RAZER
, USB_DEVICE_ID_RAZER_BLADE_14
),
1404 .driver_data
= RMI_DEVICE_HAS_PHYS_BUTTONS
},
1405 { HID_DEVICE(HID_BUS_ANY
, HID_GROUP_RMI
, HID_ANY_ID
, HID_ANY_ID
) },
1408 MODULE_DEVICE_TABLE(hid
, rmi_id
);
1410 static struct hid_driver rmi_driver
= {
1414 .remove
= rmi_remove
,
1416 .raw_event
= rmi_raw_event
,
1417 .input_mapping
= rmi_input_mapping
,
1418 .input_configured
= rmi_input_configured
,
1420 .suspend
= rmi_suspend
,
1421 .resume
= rmi_post_resume
,
1422 .reset_resume
= rmi_post_reset
,
1426 module_hid_driver(rmi_driver
);
1428 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
1429 MODULE_DESCRIPTION("RMI HID driver");
1430 MODULE_LICENSE("GPL");