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/irq.h>
18 #include <linux/irqdomain.h>
19 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/wait.h>
23 #include <linux/sched.h>
24 #include <linux/rmi.h>
27 #define RMI_MOUSE_REPORT_ID 0x01 /* Mouse emulation Report */
28 #define RMI_WRITE_REPORT_ID 0x09 /* Output Report */
29 #define RMI_READ_ADDR_REPORT_ID 0x0a /* Output Report */
30 #define RMI_READ_DATA_REPORT_ID 0x0b /* Input Report */
31 #define RMI_ATTN_REPORT_ID 0x0c /* Input Report */
32 #define RMI_SET_RMI_MODE_REPORT_ID 0x0f /* Feature Report */
35 #define RMI_READ_REQUEST_PENDING 0
36 #define RMI_READ_DATA_PENDING 1
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 * struct rmi_data - stores information for hid communication
60 * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
61 * @page: Keeps track of the current virtual page
62 * @xport: transport device to be registered with the RMI4 core.
64 * @wait: Used for waiting for read data
66 * @writeReport: output buffer when writing RMI registers
67 * @readReport: input buffer when reading RMI registers
69 * @input_report_size: size of an input report (advertised by HID)
70 * @output_report_size: size of an output report (advertised by HID)
72 * @flags: flags for the current device (started, reading, etc...)
74 * @reset_work: worker which will be called in case of a mouse report
75 * @hdev: pointer to the struct hid_device
77 * @device_flags: flags which describe the device
79 * @domain: the IRQ domain allocated for this RMI4 device
80 * @rmi_irq: the irq that will be used to generate events to rmi-core
83 struct mutex page_mutex
;
85 struct rmi_transport_dev xport
;
87 wait_queue_head_t wait
;
92 u32 input_report_size
;
93 u32 output_report_size
;
97 struct work_struct reset_work
;
98 struct hid_device
*hdev
;
100 unsigned long device_flags
;
102 struct irq_domain
*domain
;
106 #define RMI_PAGE(addr) (((addr) >> 8) & 0xff)
108 static int rmi_write_report(struct hid_device
*hdev
, u8
*report
, int len
);
111 * rmi_set_page - Set RMI page
112 * @hdev: The pointer to the hid_device struct
113 * @page: The new page address.
115 * RMI devices have 16-bit addressing, but some of the physical
116 * implementations (like SMBus) only have 8-bit addressing. So RMI implements
117 * a page address at 0xff of every page so we can reliable page addresses
118 * every 256 registers.
120 * The page_mutex lock must be held when this function is entered.
122 * Returns zero on success, non-zero on failure.
124 static int rmi_set_page(struct hid_device
*hdev
, u8 page
)
126 struct rmi_data
*data
= hid_get_drvdata(hdev
);
129 data
->writeReport
[0] = RMI_WRITE_REPORT_ID
;
130 data
->writeReport
[1] = 1;
131 data
->writeReport
[2] = 0xFF;
132 data
->writeReport
[4] = page
;
134 retval
= rmi_write_report(hdev
, data
->writeReport
,
135 data
->output_report_size
);
136 if (retval
!= data
->output_report_size
) {
138 "%s: set page failed: %d.", __func__
, retval
);
146 static int rmi_set_mode(struct hid_device
*hdev
, u8 mode
)
149 const u8 txbuf
[2] = {RMI_SET_RMI_MODE_REPORT_ID
, mode
};
152 buf
= kmemdup(txbuf
, sizeof(txbuf
), GFP_KERNEL
);
156 ret
= hid_hw_raw_request(hdev
, RMI_SET_RMI_MODE_REPORT_ID
, buf
,
157 sizeof(txbuf
), HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
160 dev_err(&hdev
->dev
, "unable to set rmi mode to %d (%d)\n", mode
,
168 static int rmi_write_report(struct hid_device
*hdev
, u8
*report
, int len
)
172 ret
= hid_hw_output_report(hdev
, (void *)report
, len
);
174 dev_err(&hdev
->dev
, "failed to write hid report (%d)\n", ret
);
181 static int rmi_hid_read_block(struct rmi_transport_dev
*xport
, u16 addr
,
182 void *buf
, size_t len
)
184 struct rmi_data
*data
= container_of(xport
, struct rmi_data
, xport
);
185 struct hid_device
*hdev
= data
->hdev
;
190 int read_input_count
;
192 mutex_lock(&data
->page_mutex
);
194 if (RMI_PAGE(addr
) != data
->page
) {
195 ret
= rmi_set_page(hdev
, RMI_PAGE(addr
));
200 for (retries
= 5; retries
> 0; retries
--) {
201 data
->writeReport
[0] = RMI_READ_ADDR_REPORT_ID
;
202 data
->writeReport
[1] = 0; /* old 1 byte read count */
203 data
->writeReport
[2] = addr
& 0xFF;
204 data
->writeReport
[3] = (addr
>> 8) & 0xFF;
205 data
->writeReport
[4] = len
& 0xFF;
206 data
->writeReport
[5] = (len
>> 8) & 0xFF;
208 set_bit(RMI_READ_REQUEST_PENDING
, &data
->flags
);
210 ret
= rmi_write_report(hdev
, data
->writeReport
,
211 data
->output_report_size
);
212 if (ret
!= data
->output_report_size
) {
213 clear_bit(RMI_READ_REQUEST_PENDING
, &data
->flags
);
215 "failed to write request output report (%d)\n",
222 while (bytes_read
< len
) {
223 if (!wait_event_timeout(data
->wait
,
224 test_bit(RMI_READ_DATA_PENDING
, &data
->flags
),
225 msecs_to_jiffies(1000))) {
226 hid_warn(hdev
, "%s: timeout elapsed\n",
232 read_input_count
= data
->readReport
[1];
233 memcpy(buf
+ bytes_read
, &data
->readReport
[2],
234 read_input_count
< bytes_needed
?
235 read_input_count
: bytes_needed
);
237 bytes_read
+= read_input_count
;
238 bytes_needed
-= read_input_count
;
239 clear_bit(RMI_READ_DATA_PENDING
, &data
->flags
);
249 clear_bit(RMI_READ_REQUEST_PENDING
, &data
->flags
);
250 mutex_unlock(&data
->page_mutex
);
254 static int rmi_hid_write_block(struct rmi_transport_dev
*xport
, u16 addr
,
255 const void *buf
, size_t len
)
257 struct rmi_data
*data
= container_of(xport
, struct rmi_data
, xport
);
258 struct hid_device
*hdev
= data
->hdev
;
261 mutex_lock(&data
->page_mutex
);
263 if (RMI_PAGE(addr
) != data
->page
) {
264 ret
= rmi_set_page(hdev
, RMI_PAGE(addr
));
269 data
->writeReport
[0] = RMI_WRITE_REPORT_ID
;
270 data
->writeReport
[1] = len
;
271 data
->writeReport
[2] = addr
& 0xFF;
272 data
->writeReport
[3] = (addr
>> 8) & 0xFF;
273 memcpy(&data
->writeReport
[4], buf
, len
);
275 ret
= rmi_write_report(hdev
, data
->writeReport
,
276 data
->output_report_size
);
279 "failed to write request output report (%d)\n",
286 mutex_unlock(&data
->page_mutex
);
290 static int rmi_reset_attn_mode(struct hid_device
*hdev
)
292 struct rmi_data
*data
= hid_get_drvdata(hdev
);
293 struct rmi_device
*rmi_dev
= data
->xport
.rmi_dev
;
296 ret
= rmi_set_mode(hdev
, RMI_MODE_ATTN_REPORTS
);
300 if (test_bit(RMI_STARTED
, &data
->flags
))
301 ret
= rmi_dev
->driver
->reset_handler(rmi_dev
);
306 static void rmi_reset_work(struct work_struct
*work
)
308 struct rmi_data
*hdata
= container_of(work
, struct rmi_data
,
311 /* switch the device to RMI if we receive a generic mouse report */
312 rmi_reset_attn_mode(hdata
->hdev
);
315 static int rmi_input_event(struct hid_device
*hdev
, u8
*data
, int size
)
317 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
318 struct rmi_device
*rmi_dev
= hdata
->xport
.rmi_dev
;
321 if (!(test_bit(RMI_STARTED
, &hdata
->flags
)))
324 local_irq_save(flags
);
326 rmi_set_attn_data(rmi_dev
, data
[1], &data
[2], size
- 2);
328 generic_handle_irq(hdata
->rmi_irq
);
330 local_irq_restore(flags
);
335 static int rmi_read_data_event(struct hid_device
*hdev
, u8
*data
, int size
)
337 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
339 if (!test_bit(RMI_READ_REQUEST_PENDING
, &hdata
->flags
)) {
340 hid_dbg(hdev
, "no read request pending\n");
344 memcpy(hdata
->readReport
, data
, size
< hdata
->input_report_size
?
345 size
: hdata
->input_report_size
);
346 set_bit(RMI_READ_DATA_PENDING
, &hdata
->flags
);
347 wake_up(&hdata
->wait
);
352 static int rmi_check_sanity(struct hid_device
*hdev
, u8
*data
, int size
)
354 int valid_size
= size
;
356 * On the Dell XPS 13 9333, the bus sometimes get confused and fills
357 * the report with a sentinel value "ff". Synaptics told us that such
358 * behavior does not comes from the touchpad itself, so we filter out
362 while ((data
[valid_size
- 1] == 0xff) && valid_size
> 0)
368 static int rmi_raw_event(struct hid_device
*hdev
,
369 struct hid_report
*report
, u8
*data
, int size
)
371 size
= rmi_check_sanity(hdev
, data
, size
);
376 case RMI_READ_DATA_REPORT_ID
:
377 return rmi_read_data_event(hdev
, data
, size
);
378 case RMI_ATTN_REPORT_ID
:
379 return rmi_input_event(hdev
, data
, size
);
387 static int rmi_event(struct hid_device
*hdev
, struct hid_field
*field
,
388 struct hid_usage
*usage
, __s32 value
)
390 struct rmi_data
*data
= hid_get_drvdata(hdev
);
392 if ((data
->device_flags
& RMI_DEVICE
) &&
393 (field
->application
== HID_GD_POINTER
||
394 field
->application
== HID_GD_MOUSE
)) {
395 if (data
->device_flags
& RMI_DEVICE_HAS_PHYS_BUTTONS
) {
396 if ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_BUTTON
)
399 if ((usage
->hid
== HID_GD_X
|| usage
->hid
== HID_GD_Y
)
404 schedule_work(&data
->reset_work
);
412 static int rmi_suspend(struct hid_device
*hdev
, pm_message_t message
)
414 struct rmi_data
*data
= hid_get_drvdata(hdev
);
415 struct rmi_device
*rmi_dev
= data
->xport
.rmi_dev
;
418 if (!(data
->device_flags
& RMI_DEVICE
))
421 ret
= rmi_driver_suspend(rmi_dev
, false);
423 hid_warn(hdev
, "Failed to suspend device: %d\n", ret
);
430 static int rmi_post_resume(struct hid_device
*hdev
)
432 struct rmi_data
*data
= hid_get_drvdata(hdev
);
433 struct rmi_device
*rmi_dev
= data
->xport
.rmi_dev
;
436 if (!(data
->device_flags
& RMI_DEVICE
))
439 /* Make sure the HID device is ready to receive events */
440 ret
= hid_hw_open(hdev
);
444 ret
= rmi_reset_attn_mode(hdev
);
448 ret
= rmi_driver_resume(rmi_dev
, false);
450 hid_warn(hdev
, "Failed to resume device: %d\n", ret
);
458 #endif /* CONFIG_PM */
460 static int rmi_hid_reset(struct rmi_transport_dev
*xport
, u16 reset_addr
)
462 struct rmi_data
*data
= container_of(xport
, struct rmi_data
, xport
);
463 struct hid_device
*hdev
= data
->hdev
;
465 return rmi_reset_attn_mode(hdev
);
468 static int rmi_input_configured(struct hid_device
*hdev
, struct hid_input
*hi
)
470 struct rmi_data
*data
= hid_get_drvdata(hdev
);
471 struct input_dev
*input
= hi
->input
;
474 if (!(data
->device_flags
& RMI_DEVICE
))
477 data
->xport
.input
= input
;
479 hid_dbg(hdev
, "Opening low level driver\n");
480 ret
= hid_hw_open(hdev
);
484 /* Allow incoming hid reports */
485 hid_device_io_start(hdev
);
487 ret
= rmi_set_mode(hdev
, RMI_MODE_ATTN_REPORTS
);
489 dev_err(&hdev
->dev
, "failed to set rmi mode\n");
493 ret
= rmi_set_page(hdev
, 0);
495 dev_err(&hdev
->dev
, "failed to set page select to 0.\n");
499 ret
= rmi_register_transport_device(&data
->xport
);
501 dev_err(&hdev
->dev
, "failed to register transport driver\n");
505 set_bit(RMI_STARTED
, &data
->flags
);
508 hid_device_io_stop(hdev
);
513 static int rmi_input_mapping(struct hid_device
*hdev
,
514 struct hid_input
*hi
, struct hid_field
*field
,
515 struct hid_usage
*usage
, unsigned long **bit
, int *max
)
517 struct rmi_data
*data
= hid_get_drvdata(hdev
);
520 * we want to make HID ignore the advertised HID collection
523 if (data
->device_flags
& RMI_DEVICE
) {
524 if ((data
->device_flags
& RMI_DEVICE_HAS_PHYS_BUTTONS
) &&
525 ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_BUTTON
))
534 static int rmi_check_valid_report_id(struct hid_device
*hdev
, unsigned type
,
535 unsigned id
, struct hid_report
**report
)
539 *report
= hdev
->report_enum
[type
].report_id_hash
[id
];
541 for (i
= 0; i
< (*report
)->maxfield
; i
++) {
542 unsigned app
= (*report
)->field
[i
]->application
;
543 if ((app
& HID_USAGE_PAGE
) >= HID_UP_MSVENDOR
)
551 static struct rmi_device_platform_data rmi_hid_pdata
= {
553 .sensor_type
= rmi_sensor_touchpad
,
554 .axis_align
.flip_y
= true,
555 .dribble
= RMI_REG_STATE_ON
,
556 .palm_detect
= RMI_REG_STATE_OFF
,
560 static const struct rmi_transport_ops hid_rmi_ops
= {
561 .write_block
= rmi_hid_write_block
,
562 .read_block
= rmi_hid_read_block
,
563 .reset
= rmi_hid_reset
,
566 static void rmi_irq_teardown(void *data
)
568 struct rmi_data
*hdata
= data
;
569 struct irq_domain
*domain
= hdata
->domain
;
574 irq_dispose_mapping(irq_find_mapping(domain
, 0));
576 irq_domain_remove(domain
);
577 hdata
->domain
= NULL
;
581 static int rmi_irq_map(struct irq_domain
*h
, unsigned int virq
,
582 irq_hw_number_t hw_irq_num
)
584 irq_set_chip_and_handler(virq
, &dummy_irq_chip
, handle_simple_irq
);
589 static const struct irq_domain_ops rmi_irq_ops
= {
593 static int rmi_setup_irq_domain(struct hid_device
*hdev
)
595 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
598 hdata
->domain
= irq_domain_create_linear(hdev
->dev
.fwnode
, 1,
599 &rmi_irq_ops
, hdata
);
603 ret
= devm_add_action_or_reset(&hdev
->dev
, &rmi_irq_teardown
, hdata
);
607 hdata
->rmi_irq
= irq_create_mapping(hdata
->domain
, 0);
608 if (hdata
->rmi_irq
<= 0) {
609 hid_err(hdev
, "Can't allocate an IRQ\n");
610 return hdata
->rmi_irq
< 0 ? hdata
->rmi_irq
: -ENXIO
;
616 static int rmi_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
618 struct rmi_data
*data
= NULL
;
621 struct hid_report
*input_report
;
622 struct hid_report
*output_report
;
623 struct hid_report
*feature_report
;
625 data
= devm_kzalloc(&hdev
->dev
, sizeof(struct rmi_data
), GFP_KERNEL
);
629 INIT_WORK(&data
->reset_work
, rmi_reset_work
);
632 hid_set_drvdata(hdev
, data
);
634 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
636 ret
= hid_parse(hdev
);
638 hid_err(hdev
, "parse failed\n");
643 data
->device_flags
= id
->driver_data
;
646 * Check for the RMI specific report ids. If they are misisng
647 * simply return and let the events be processed by hid-input
649 if (!rmi_check_valid_report_id(hdev
, HID_FEATURE_REPORT
,
650 RMI_SET_RMI_MODE_REPORT_ID
, &feature_report
)) {
651 hid_dbg(hdev
, "device does not have set mode feature report\n");
655 if (!rmi_check_valid_report_id(hdev
, HID_INPUT_REPORT
,
656 RMI_ATTN_REPORT_ID
, &input_report
)) {
657 hid_dbg(hdev
, "device does not have attention input report\n");
661 data
->input_report_size
= hid_report_len(input_report
);
663 if (!rmi_check_valid_report_id(hdev
, HID_OUTPUT_REPORT
,
664 RMI_WRITE_REPORT_ID
, &output_report
)) {
666 "device does not have rmi write output report\n");
670 data
->output_report_size
= hid_report_len(output_report
);
672 data
->device_flags
|= RMI_DEVICE
;
673 alloc_size
= data
->output_report_size
+ data
->input_report_size
;
675 data
->writeReport
= devm_kzalloc(&hdev
->dev
, alloc_size
, GFP_KERNEL
);
676 if (!data
->writeReport
) {
677 hid_err(hdev
, "failed to allocate buffer for HID reports\n");
681 data
->readReport
= data
->writeReport
+ data
->output_report_size
;
683 init_waitqueue_head(&data
->wait
);
685 mutex_init(&data
->page_mutex
);
687 ret
= rmi_setup_irq_domain(hdev
);
689 hid_err(hdev
, "failed to allocate IRQ domain\n");
693 if (data
->device_flags
& RMI_DEVICE_HAS_PHYS_BUTTONS
)
694 rmi_hid_pdata
.f30_data
.disable
= true;
696 data
->xport
.dev
= hdev
->dev
.parent
;
697 data
->xport
.pdata
= rmi_hid_pdata
;
698 data
->xport
.pdata
.irq
= data
->rmi_irq
;
699 data
->xport
.proto_name
= "hid";
700 data
->xport
.ops
= &hid_rmi_ops
;
703 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
705 hid_err(hdev
, "hw start failed\n");
712 static void rmi_remove(struct hid_device
*hdev
)
714 struct rmi_data
*hdata
= hid_get_drvdata(hdev
);
716 clear_bit(RMI_STARTED
, &hdata
->flags
);
717 cancel_work_sync(&hdata
->reset_work
);
718 rmi_unregister_transport_device(&hdata
->xport
);
723 static const struct hid_device_id rmi_id
[] = {
724 { HID_USB_DEVICE(USB_VENDOR_ID_RAZER
, USB_DEVICE_ID_RAZER_BLADE_14
),
725 .driver_data
= RMI_DEVICE_HAS_PHYS_BUTTONS
},
726 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO
, USB_DEVICE_ID_LENOVO_X1_COVER
) },
727 { HID_DEVICE(HID_BUS_ANY
, HID_GROUP_RMI
, HID_ANY_ID
, HID_ANY_ID
) },
730 MODULE_DEVICE_TABLE(hid
, rmi_id
);
732 static struct hid_driver rmi_driver
= {
736 .remove
= rmi_remove
,
738 .raw_event
= rmi_raw_event
,
739 .input_mapping
= rmi_input_mapping
,
740 .input_configured
= rmi_input_configured
,
742 .suspend
= rmi_suspend
,
743 .resume
= rmi_post_resume
,
744 .reset_resume
= rmi_post_resume
,
748 module_hid_driver(rmi_driver
);
750 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
751 MODULE_DESCRIPTION("RMI HID driver");
752 MODULE_LICENSE("GPL");