blob: 9ce22acdfaca261ccf9736794ba9581d0257e119 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -04002/*
3 * Copyright (c) 2013 Andrew Duggan <aduggan@synaptics.com>
4 * Copyright (c) 2013 Synaptics Incorporated
5 * Copyright (c) 2014 Benjamin Tissoires <benjamin.tissoires@gmail.com>
6 * Copyright (c) 2014 Red Hat, Inc
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -04007 */
8
9#include <linux/kernel.h>
10#include <linux/hid.h>
11#include <linux/input.h>
12#include <linux/input/mt.h>
Andrew Duggan0b2c7a82017-01-05 09:48:58 +010013#include <linux/irq.h>
14#include <linux/irqdomain.h>
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -040015#include <linux/module.h>
16#include <linux/pm.h>
17#include <linux/slab.h>
18#include <linux/wait.h>
19#include <linux/sched.h>
Andrew Duggan0b2c7a82017-01-05 09:48:58 +010020#include <linux/rmi.h>
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -040021#include "hid-ids.h"
22
23#define RMI_MOUSE_REPORT_ID 0x01 /* Mouse emulation Report */
24#define RMI_WRITE_REPORT_ID 0x09 /* Output Report */
25#define RMI_READ_ADDR_REPORT_ID 0x0a /* Output Report */
26#define RMI_READ_DATA_REPORT_ID 0x0b /* Input Report */
27#define RMI_ATTN_REPORT_ID 0x0c /* Input Report */
28#define RMI_SET_RMI_MODE_REPORT_ID 0x0f /* Feature Report */
29
30/* flags */
Dan Carpenteraf43c402015-05-14 11:32:56 +030031#define RMI_READ_REQUEST_PENDING 0
32#define RMI_READ_DATA_PENDING 1
33#define RMI_STARTED 2
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -040034
Andrew Duggan2f43de62014-12-19 14:45:41 -080035/* device flags */
36#define RMI_DEVICE BIT(0)
Andrew Duggan79364d82015-01-08 14:51:34 -080037#define RMI_DEVICE_HAS_PHYS_BUTTONS BIT(1)
Tobias Auerochs10235382019-04-20 21:19:51 +020038#define RMI_DEVICE_OUTPUT_SET_REPORT BIT(2)
Andrew Duggan2f43de62014-12-19 14:45:41 -080039
Andrew Duggan7035f3a2015-07-10 12:48:21 -070040/*
41 * retrieve the ctrl registers
42 * the ctrl register has a size of 20 but a fw bug split it into 16 + 4,
43 * and there is no way to know if the first 20 bytes are here or not.
44 * We use only the first 12 bytes, so get only them.
45 */
46#define RMI_F11_CTRL_REG_COUNT 12
47
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -040048enum rmi_mode_type {
49 RMI_MODE_OFF = 0,
50 RMI_MODE_ATTN_REPORTS = 1,
51 RMI_MODE_NO_PACKED_ATTN_REPORTS = 2,
52};
53
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -040054/**
55 * struct rmi_data - stores information for hid communication
56 *
57 * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
58 * @page: Keeps track of the current virtual page
Andrew Duggan0b2c7a82017-01-05 09:48:58 +010059 * @xport: transport device to be registered with the RMI4 core.
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -040060 *
61 * @wait: Used for waiting for read data
62 *
63 * @writeReport: output buffer when writing RMI registers
64 * @readReport: input buffer when reading RMI registers
65 *
66 * @input_report_size: size of an input report (advertised by HID)
67 * @output_report_size: size of an output report (advertised by HID)
68 *
69 * @flags: flags for the current device (started, reading, etc...)
70 *
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -040071 * @reset_work: worker which will be called in case of a mouse report
72 * @hdev: pointer to the struct hid_device
Andrew Duggan0b2c7a82017-01-05 09:48:58 +010073 *
74 * @device_flags: flags which describe the device
75 *
76 * @domain: the IRQ domain allocated for this RMI4 device
77 * @rmi_irq: the irq that will be used to generate events to rmi-core
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -040078 */
79struct rmi_data {
80 struct mutex page_mutex;
81 int page;
Andrew Duggan0b2c7a82017-01-05 09:48:58 +010082 struct rmi_transport_dev xport;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -040083
84 wait_queue_head_t wait;
85
86 u8 *writeReport;
87 u8 *readReport;
88
Aaron Ma3064a032018-02-03 23:57:15 +080089 u32 input_report_size;
90 u32 output_report_size;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -040091
92 unsigned long flags;
93
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -040094 struct work_struct reset_work;
95 struct hid_device *hdev;
Andrew Duggan2f43de62014-12-19 14:45:41 -080096
97 unsigned long device_flags;
Andrew Duggan09256362015-07-06 16:48:31 -070098
Andrew Duggan0b2c7a82017-01-05 09:48:58 +010099 struct irq_domain *domain;
100 int rmi_irq;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400101};
102
103#define RMI_PAGE(addr) (((addr) >> 8) & 0xff)
104
105static int rmi_write_report(struct hid_device *hdev, u8 *report, int len);
106
107/**
108 * rmi_set_page - Set RMI page
109 * @hdev: The pointer to the hid_device struct
110 * @page: The new page address.
111 *
112 * RMI devices have 16-bit addressing, but some of the physical
113 * implementations (like SMBus) only have 8-bit addressing. So RMI implements
114 * a page address at 0xff of every page so we can reliable page addresses
115 * every 256 registers.
116 *
117 * The page_mutex lock must be held when this function is entered.
118 *
119 * Returns zero on success, non-zero on failure.
120 */
121static int rmi_set_page(struct hid_device *hdev, u8 page)
122{
123 struct rmi_data *data = hid_get_drvdata(hdev);
124 int retval;
125
126 data->writeReport[0] = RMI_WRITE_REPORT_ID;
127 data->writeReport[1] = 1;
128 data->writeReport[2] = 0xFF;
129 data->writeReport[4] = page;
130
131 retval = rmi_write_report(hdev, data->writeReport,
132 data->output_report_size);
133 if (retval != data->output_report_size) {
134 dev_err(&hdev->dev,
135 "%s: set page failed: %d.", __func__, retval);
136 return retval;
137 }
138
139 data->page = page;
140 return 0;
141}
142
143static int rmi_set_mode(struct hid_device *hdev, u8 mode)
144{
145 int ret;
Benjamin Tissoires6dab07d2016-11-21 11:48:42 +0100146 const u8 txbuf[2] = {RMI_SET_RMI_MODE_REPORT_ID, mode};
147 u8 *buf;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400148
Benjamin Tissoires6dab07d2016-11-21 11:48:42 +0100149 buf = kmemdup(txbuf, sizeof(txbuf), GFP_KERNEL);
150 if (!buf)
151 return -ENOMEM;
152
153 ret = hid_hw_raw_request(hdev, RMI_SET_RMI_MODE_REPORT_ID, buf,
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400154 sizeof(txbuf), HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Benjamin Tissoires6dab07d2016-11-21 11:48:42 +0100155 kfree(buf);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400156 if (ret < 0) {
157 dev_err(&hdev->dev, "unable to set rmi mode to %d (%d)\n", mode,
158 ret);
159 return ret;
160 }
161
162 return 0;
163}
164
165static int rmi_write_report(struct hid_device *hdev, u8 *report, int len)
166{
Tobias Auerochs10235382019-04-20 21:19:51 +0200167 struct rmi_data *data = hid_get_drvdata(hdev);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400168 int ret;
169
Tobias Auerochs10235382019-04-20 21:19:51 +0200170 if (data->device_flags & RMI_DEVICE_OUTPUT_SET_REPORT) {
171 /*
172 * Talk to device by using SET_REPORT requests instead.
173 */
174 ret = hid_hw_raw_request(hdev, report[0], report,
175 len, HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
176 } else {
177 ret = hid_hw_output_report(hdev, (void *)report, len);
178 }
179
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400180 if (ret < 0) {
181 dev_err(&hdev->dev, "failed to write hid report (%d)\n", ret);
182 return ret;
183 }
184
185 return ret;
186}
187
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100188static int rmi_hid_read_block(struct rmi_transport_dev *xport, u16 addr,
189 void *buf, size_t len)
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400190{
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100191 struct rmi_data *data = container_of(xport, struct rmi_data, xport);
192 struct hid_device *hdev = data->hdev;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400193 int ret;
194 int bytes_read;
195 int bytes_needed;
196 int retries;
197 int read_input_count;
198
199 mutex_lock(&data->page_mutex);
200
201 if (RMI_PAGE(addr) != data->page) {
202 ret = rmi_set_page(hdev, RMI_PAGE(addr));
203 if (ret < 0)
204 goto exit;
205 }
206
207 for (retries = 5; retries > 0; retries--) {
208 data->writeReport[0] = RMI_READ_ADDR_REPORT_ID;
209 data->writeReport[1] = 0; /* old 1 byte read count */
210 data->writeReport[2] = addr & 0xFF;
211 data->writeReport[3] = (addr >> 8) & 0xFF;
212 data->writeReport[4] = len & 0xFF;
213 data->writeReport[5] = (len >> 8) & 0xFF;
214
215 set_bit(RMI_READ_REQUEST_PENDING, &data->flags);
216
217 ret = rmi_write_report(hdev, data->writeReport,
218 data->output_report_size);
219 if (ret != data->output_report_size) {
220 clear_bit(RMI_READ_REQUEST_PENDING, &data->flags);
221 dev_err(&hdev->dev,
222 "failed to write request output report (%d)\n",
223 ret);
224 goto exit;
225 }
226
227 bytes_read = 0;
228 bytes_needed = len;
229 while (bytes_read < len) {
230 if (!wait_event_timeout(data->wait,
231 test_bit(RMI_READ_DATA_PENDING, &data->flags),
232 msecs_to_jiffies(1000))) {
233 hid_warn(hdev, "%s: timeout elapsed\n",
234 __func__);
235 ret = -EAGAIN;
236 break;
237 }
238
239 read_input_count = data->readReport[1];
240 memcpy(buf + bytes_read, &data->readReport[2],
241 read_input_count < bytes_needed ?
242 read_input_count : bytes_needed);
243
244 bytes_read += read_input_count;
245 bytes_needed -= read_input_count;
246 clear_bit(RMI_READ_DATA_PENDING, &data->flags);
247 }
248
249 if (ret >= 0) {
250 ret = 0;
251 break;
252 }
253 }
254
255exit:
256 clear_bit(RMI_READ_REQUEST_PENDING, &data->flags);
257 mutex_unlock(&data->page_mutex);
258 return ret;
259}
260
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100261static int rmi_hid_write_block(struct rmi_transport_dev *xport, u16 addr,
262 const void *buf, size_t len)
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400263{
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100264 struct rmi_data *data = container_of(xport, struct rmi_data, xport);
265 struct hid_device *hdev = data->hdev;
Andrew Duggandd8df282015-02-24 17:36:48 -0800266 int ret;
267
268 mutex_lock(&data->page_mutex);
269
270 if (RMI_PAGE(addr) != data->page) {
271 ret = rmi_set_page(hdev, RMI_PAGE(addr));
272 if (ret < 0)
273 goto exit;
274 }
275
276 data->writeReport[0] = RMI_WRITE_REPORT_ID;
277 data->writeReport[1] = len;
278 data->writeReport[2] = addr & 0xFF;
279 data->writeReport[3] = (addr >> 8) & 0xFF;
280 memcpy(&data->writeReport[4], buf, len);
281
282 ret = rmi_write_report(hdev, data->writeReport,
283 data->output_report_size);
284 if (ret < 0) {
285 dev_err(&hdev->dev,
286 "failed to write request output report (%d)\n",
287 ret);
288 goto exit;
289 }
290 ret = 0;
291
292exit:
293 mutex_unlock(&data->page_mutex);
294 return ret;
295}
296
Andrew Duggan9a98b332015-07-16 17:14:00 -0700297static int rmi_reset_attn_mode(struct hid_device *hdev)
298{
299 struct rmi_data *data = hid_get_drvdata(hdev);
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100300 struct rmi_device *rmi_dev = data->xport.rmi_dev;
Andrew Duggan9a98b332015-07-16 17:14:00 -0700301 int ret;
302
303 ret = rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
304 if (ret)
305 return ret;
306
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100307 if (test_bit(RMI_STARTED, &data->flags))
308 ret = rmi_dev->driver->reset_handler(rmi_dev);
Andrew Duggan9a98b332015-07-16 17:14:00 -0700309
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100310 return ret;
Andrew Duggan9a98b332015-07-16 17:14:00 -0700311}
312
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400313static void rmi_reset_work(struct work_struct *work)
314{
315 struct rmi_data *hdata = container_of(work, struct rmi_data,
316 reset_work);
317
318 /* switch the device to RMI if we receive a generic mouse report */
Andrew Duggan9a98b332015-07-16 17:14:00 -0700319 rmi_reset_attn_mode(hdata->hdev);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400320}
321
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400322static int rmi_input_event(struct hid_device *hdev, u8 *data, int size)
323{
324 struct rmi_data *hdata = hid_get_drvdata(hdev);
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100325 struct rmi_device *rmi_dev = hdata->xport.rmi_dev;
326 unsigned long flags;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400327
328 if (!(test_bit(RMI_STARTED, &hdata->flags)))
329 return 0;
330
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100331 local_irq_save(flags);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400332
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100333 rmi_set_attn_data(rmi_dev, data[1], &data[2], size - 2);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400334
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100335 generic_handle_irq(hdata->rmi_irq);
336
337 local_irq_restore(flags);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400338
339 return 1;
340}
341
342static int rmi_read_data_event(struct hid_device *hdev, u8 *data, int size)
343{
344 struct rmi_data *hdata = hid_get_drvdata(hdev);
345
346 if (!test_bit(RMI_READ_REQUEST_PENDING, &hdata->flags)) {
Andrew Duggan01a5f8a2014-07-11 14:35:51 -0700347 hid_dbg(hdev, "no read request pending\n");
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400348 return 0;
349 }
350
351 memcpy(hdata->readReport, data, size < hdata->input_report_size ?
352 size : hdata->input_report_size);
353 set_bit(RMI_READ_DATA_PENDING, &hdata->flags);
354 wake_up(&hdata->wait);
355
356 return 1;
357}
358
Benjamin Tissoires5b65c2a2014-09-10 18:02:37 -0700359static int rmi_check_sanity(struct hid_device *hdev, u8 *data, int size)
360{
361 int valid_size = size;
362 /*
363 * On the Dell XPS 13 9333, the bus sometimes get confused and fills
364 * the report with a sentinel value "ff". Synaptics told us that such
365 * behavior does not comes from the touchpad itself, so we filter out
366 * such reports here.
367 */
368
369 while ((data[valid_size - 1] == 0xff) && valid_size > 0)
370 valid_size--;
371
372 return valid_size;
373}
374
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400375static int rmi_raw_event(struct hid_device *hdev,
376 struct hid_report *report, u8 *data, int size)
377{
Andrew Dugganef14a4bf2017-10-17 18:37:36 -0700378 struct rmi_data *hdata = hid_get_drvdata(hdev);
379
380 if (!(hdata->device_flags & RMI_DEVICE))
381 return 0;
382
Benjamin Tissoires5b65c2a2014-09-10 18:02:37 -0700383 size = rmi_check_sanity(hdev, data, size);
384 if (size < 2)
385 return 0;
386
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400387 switch (data[0]) {
388 case RMI_READ_DATA_REPORT_ID:
389 return rmi_read_data_event(hdev, data, size);
390 case RMI_ATTN_REPORT_ID:
391 return rmi_input_event(hdev, data, size);
Andrew Duggan2f43de62014-12-19 14:45:41 -0800392 default:
393 return 1;
394 }
395
396 return 0;
397}
398
399static int rmi_event(struct hid_device *hdev, struct hid_field *field,
400 struct hid_usage *usage, __s32 value)
401{
402 struct rmi_data *data = hid_get_drvdata(hdev);
403
404 if ((data->device_flags & RMI_DEVICE) &&
405 (field->application == HID_GD_POINTER ||
406 field->application == HID_GD_MOUSE)) {
Andrew Duggan79364d82015-01-08 14:51:34 -0800407 if (data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS) {
408 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
409 return 0;
410
411 if ((usage->hid == HID_GD_X || usage->hid == HID_GD_Y)
412 && !value)
413 return 1;
414 }
415
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100416 schedule_work(&data->reset_work);
Andrew Duggan2f43de62014-12-19 14:45:41 -0800417 return 1;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400418 }
419
420 return 0;
421}
422
Benjamin Tissoiresc94ba062018-05-25 14:51:06 +0200423static void rmi_report(struct hid_device *hid, struct hid_report *report)
424{
425 struct hid_field *field = report->field[0];
426
427 if (!(hid->claimed & HID_CLAIMED_INPUT))
428 return;
429
430 switch (report->id) {
431 case RMI_READ_DATA_REPORT_ID:
432 /* fall-through */
433 case RMI_ATTN_REPORT_ID:
434 return;
435 }
436
437 if (field && field->hidinput && field->hidinput->input)
438 input_sync(field->hidinput->input);
439}
440
Geert Uytterhoevena278e262014-06-11 21:03:18 +0200441#ifdef CONFIG_PM
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100442static int rmi_suspend(struct hid_device *hdev, pm_message_t message)
Andrew Duggan09256362015-07-06 16:48:31 -0700443{
444 struct rmi_data *data = hid_get_drvdata(hdev);
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100445 struct rmi_device *rmi_dev = data->xport.rmi_dev;
Andrew Duggan09256362015-07-06 16:48:31 -0700446 int ret;
Andrew Duggan09256362015-07-06 16:48:31 -0700447
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100448 if (!(data->device_flags & RMI_DEVICE))
449 return 0;
Andrew Duggan09256362015-07-06 16:48:31 -0700450
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100451 ret = rmi_driver_suspend(rmi_dev, false);
Andrew Duggan09256362015-07-06 16:48:31 -0700452 if (ret) {
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100453 hid_warn(hdev, "Failed to suspend device: %d\n", ret);
Andrew Duggan09256362015-07-06 16:48:31 -0700454 return ret;
455 }
456
457 return 0;
458}
459
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100460static int rmi_post_resume(struct hid_device *hdev)
Andrew Duggan09256362015-07-06 16:48:31 -0700461{
Andrew Duggan7035f3a2015-07-10 12:48:21 -0700462 struct rmi_data *data = hid_get_drvdata(hdev);
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100463 struct rmi_device *rmi_dev = data->xport.rmi_dev;
Andrew Duggan09256362015-07-06 16:48:31 -0700464 int ret;
465
Andrew Dugganb786ae82016-01-26 15:40:36 -0800466 if (!(data->device_flags & RMI_DEVICE))
467 return 0;
468
Lyudecac72b92017-07-22 21:15:09 -0400469 /* Make sure the HID device is ready to receive events */
470 ret = hid_hw_open(hdev);
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100471 if (ret)
472 return ret;
473
Lyudecac72b92017-07-22 21:15:09 -0400474 ret = rmi_reset_attn_mode(hdev);
475 if (ret)
476 goto out;
477
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100478 ret = rmi_driver_resume(rmi_dev, false);
Andrew Duggan09256362015-07-06 16:48:31 -0700479 if (ret) {
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100480 hid_warn(hdev, "Failed to resume device: %d\n", ret);
Lyudecac72b92017-07-22 21:15:09 -0400481 goto out;
Andrew Duggan09256362015-07-06 16:48:31 -0700482 }
483
Lyudecac72b92017-07-22 21:15:09 -0400484out:
485 hid_hw_close(hdev);
486 return ret;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400487}
Geert Uytterhoevena278e262014-06-11 21:03:18 +0200488#endif /* CONFIG_PM */
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400489
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100490static int rmi_hid_reset(struct rmi_transport_dev *xport, u16 reset_addr)
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400491{
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100492 struct rmi_data *data = container_of(xport, struct rmi_data, xport);
493 struct hid_device *hdev = data->hdev;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400494
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100495 return rmi_reset_attn_mode(hdev);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400496}
497
Dmitry Torokhov91543012015-09-29 15:52:59 -0700498static int rmi_input_configured(struct hid_device *hdev, struct hid_input *hi)
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400499{
500 struct rmi_data *data = hid_get_drvdata(hdev);
501 struct input_dev *input = hi->input;
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100502 int ret = 0;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400503
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100504 if (!(data->device_flags & RMI_DEVICE))
505 return 0;
506
507 data->xport.input = input;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400508
509 hid_dbg(hdev, "Opening low level driver\n");
510 ret = hid_hw_open(hdev);
511 if (ret)
Dmitry Torokhov91543012015-09-29 15:52:59 -0700512 return ret;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400513
514 /* Allow incoming hid reports */
515 hid_device_io_start(hdev);
516
517 ret = rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
518 if (ret < 0) {
519 dev_err(&hdev->dev, "failed to set rmi mode\n");
520 goto exit;
521 }
522
523 ret = rmi_set_page(hdev, 0);
524 if (ret < 0) {
525 dev_err(&hdev->dev, "failed to set page select to 0.\n");
526 goto exit;
527 }
528
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100529 ret = rmi_register_transport_device(&data->xport);
530 if (ret < 0) {
531 dev_err(&hdev->dev, "failed to register transport driver\n");
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400532 goto exit;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400533 }
534
535 set_bit(RMI_STARTED, &data->flags);
536
537exit:
538 hid_device_io_stop(hdev);
539 hid_hw_close(hdev);
Dmitry Torokhov91543012015-09-29 15:52:59 -0700540 return ret;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400541}
542
543static int rmi_input_mapping(struct hid_device *hdev,
544 struct hid_input *hi, struct hid_field *field,
545 struct hid_usage *usage, unsigned long **bit, int *max)
546{
Andrew Duggan2f43de62014-12-19 14:45:41 -0800547 struct rmi_data *data = hid_get_drvdata(hdev);
548
549 /*
550 * we want to make HID ignore the advertised HID collection
551 * for RMI deivces
552 */
Andrew Duggan79364d82015-01-08 14:51:34 -0800553 if (data->device_flags & RMI_DEVICE) {
554 if ((data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS) &&
555 ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON))
556 return 0;
557
Andrew Duggan2f43de62014-12-19 14:45:41 -0800558 return -1;
Andrew Duggan79364d82015-01-08 14:51:34 -0800559 }
Andrew Duggan2f43de62014-12-19 14:45:41 -0800560
561 return 0;
562}
563
564static int rmi_check_valid_report_id(struct hid_device *hdev, unsigned type,
565 unsigned id, struct hid_report **report)
566{
567 int i;
568
569 *report = hdev->report_enum[type].report_id_hash[id];
570 if (*report) {
571 for (i = 0; i < (*report)->maxfield; i++) {
572 unsigned app = (*report)->field[i]->application;
573 if ((app & HID_USAGE_PAGE) >= HID_UP_MSVENDOR)
574 return 1;
575 }
576 }
577
578 return 0;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400579}
580
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100581static struct rmi_device_platform_data rmi_hid_pdata = {
582 .sensor_pdata = {
583 .sensor_type = rmi_sensor_touchpad,
584 .axis_align.flip_y = true,
585 .dribble = RMI_REG_STATE_ON,
586 .palm_detect = RMI_REG_STATE_OFF,
587 },
588};
589
590static const struct rmi_transport_ops hid_rmi_ops = {
591 .write_block = rmi_hid_write_block,
592 .read_block = rmi_hid_read_block,
593 .reset = rmi_hid_reset,
594};
595
596static void rmi_irq_teardown(void *data)
597{
598 struct rmi_data *hdata = data;
599 struct irq_domain *domain = hdata->domain;
600
601 if (!domain)
602 return;
603
604 irq_dispose_mapping(irq_find_mapping(domain, 0));
605
606 irq_domain_remove(domain);
607 hdata->domain = NULL;
608 hdata->rmi_irq = 0;
609}
610
611static int rmi_irq_map(struct irq_domain *h, unsigned int virq,
612 irq_hw_number_t hw_irq_num)
613{
614 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
615
616 return 0;
617}
618
619static const struct irq_domain_ops rmi_irq_ops = {
620 .map = rmi_irq_map,
621};
622
623static int rmi_setup_irq_domain(struct hid_device *hdev)
624{
625 struct rmi_data *hdata = hid_get_drvdata(hdev);
626 int ret;
627
628 hdata->domain = irq_domain_create_linear(hdev->dev.fwnode, 1,
629 &rmi_irq_ops, hdata);
630 if (!hdata->domain)
631 return -ENOMEM;
632
633 ret = devm_add_action_or_reset(&hdev->dev, &rmi_irq_teardown, hdata);
634 if (ret)
635 return ret;
636
637 hdata->rmi_irq = irq_create_mapping(hdata->domain, 0);
638 if (hdata->rmi_irq <= 0) {
639 hid_err(hdev, "Can't allocate an IRQ\n");
640 return hdata->rmi_irq < 0 ? hdata->rmi_irq : -ENXIO;
641 }
642
643 return 0;
644}
645
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400646static int rmi_probe(struct hid_device *hdev, const struct hid_device_id *id)
647{
648 struct rmi_data *data = NULL;
649 int ret;
650 size_t alloc_size;
Andrew Duggandd3edeb2014-07-17 16:14:44 -0700651 struct hid_report *input_report;
652 struct hid_report *output_report;
Andrew Duggan2f43de62014-12-19 14:45:41 -0800653 struct hid_report *feature_report;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400654
655 data = devm_kzalloc(&hdev->dev, sizeof(struct rmi_data), GFP_KERNEL);
656 if (!data)
657 return -ENOMEM;
658
659 INIT_WORK(&data->reset_work, rmi_reset_work);
660 data->hdev = hdev;
661
662 hid_set_drvdata(hdev, data);
663
664 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
Benjamin Tissoiresc94ba062018-05-25 14:51:06 +0200665 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400666
667 ret = hid_parse(hdev);
668 if (ret) {
669 hid_err(hdev, "parse failed\n");
670 return ret;
671 }
672
Andrew Duggan79364d82015-01-08 14:51:34 -0800673 if (id->driver_data)
674 data->device_flags = id->driver_data;
675
Andrew Duggan2f43de62014-12-19 14:45:41 -0800676 /*
677 * Check for the RMI specific report ids. If they are misisng
678 * simply return and let the events be processed by hid-input
679 */
680 if (!rmi_check_valid_report_id(hdev, HID_FEATURE_REPORT,
681 RMI_SET_RMI_MODE_REPORT_ID, &feature_report)) {
682 hid_dbg(hdev, "device does not have set mode feature report\n");
683 goto start;
684 }
685
686 if (!rmi_check_valid_report_id(hdev, HID_INPUT_REPORT,
687 RMI_ATTN_REPORT_ID, &input_report)) {
688 hid_dbg(hdev, "device does not have attention input report\n");
689 goto start;
Andrew Duggandd3edeb2014-07-17 16:14:44 -0700690 }
691
Andrew Dugganb8aed6e2015-01-08 14:51:36 -0800692 data->input_report_size = hid_report_len(input_report);
Andrew Duggandd3edeb2014-07-17 16:14:44 -0700693
Andrew Duggan2f43de62014-12-19 14:45:41 -0800694 if (!rmi_check_valid_report_id(hdev, HID_OUTPUT_REPORT,
695 RMI_WRITE_REPORT_ID, &output_report)) {
696 hid_dbg(hdev,
697 "device does not have rmi write output report\n");
698 goto start;
Andrew Duggandd3edeb2014-07-17 16:14:44 -0700699 }
700
Andrew Dugganb8aed6e2015-01-08 14:51:36 -0800701 data->output_report_size = hid_report_len(output_report);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400702
Andrew Duggan2f43de62014-12-19 14:45:41 -0800703 data->device_flags |= RMI_DEVICE;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400704 alloc_size = data->output_report_size + data->input_report_size;
705
706 data->writeReport = devm_kzalloc(&hdev->dev, alloc_size, GFP_KERNEL);
707 if (!data->writeReport) {
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100708 hid_err(hdev, "failed to allocate buffer for HID reports\n");
709 return -ENOMEM;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400710 }
711
712 data->readReport = data->writeReport + data->output_report_size;
713
714 init_waitqueue_head(&data->wait);
715
716 mutex_init(&data->page_mutex);
717
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100718 ret = rmi_setup_irq_domain(hdev);
719 if (ret) {
720 hid_err(hdev, "failed to allocate IRQ domain\n");
721 return ret;
722 }
723
724 if (data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS)
725 rmi_hid_pdata.f30_data.disable = true;
726
727 data->xport.dev = hdev->dev.parent;
728 data->xport.pdata = rmi_hid_pdata;
729 data->xport.pdata.irq = data->rmi_irq;
730 data->xport.proto_name = "hid";
731 data->xport.ops = &hid_rmi_ops;
732
Andrew Duggan2f43de62014-12-19 14:45:41 -0800733start:
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400734 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
735 if (ret) {
736 hid_err(hdev, "hw start failed\n");
737 return ret;
738 }
739
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400740 return 0;
741}
742
743static void rmi_remove(struct hid_device *hdev)
744{
745 struct rmi_data *hdata = hid_get_drvdata(hdev);
746
Andrew Duggan8725aa42019-10-23 01:24:03 +0000747 if ((hdata->device_flags & RMI_DEVICE)
748 && test_bit(RMI_STARTED, &hdata->flags)) {
Andrew Dugganef14a4bf2017-10-17 18:37:36 -0700749 clear_bit(RMI_STARTED, &hdata->flags);
750 cancel_work_sync(&hdata->reset_work);
751 rmi_unregister_transport_device(&hdata->xport);
752 }
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400753
754 hid_hw_stop(hdev);
755}
756
757static const struct hid_device_id rmi_id[] = {
Andrew Duggane9287092015-01-08 14:51:35 -0800758 { HID_USB_DEVICE(USB_VENDOR_ID_RAZER, USB_DEVICE_ID_RAZER_BLADE_14),
759 .driver_data = RMI_DEVICE_HAS_PHYS_BUTTONS },
Andrew Dugganc7821d02017-01-05 09:49:00 +0100760 { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_COVER) },
Andrew Dugganc5293402017-11-17 17:21:30 -0800761 { HID_USB_DEVICE(USB_VENDOR_ID_PRIMAX, USB_DEVICE_ID_PRIMAX_REZEL) },
Tobias Auerochs10235382019-04-20 21:19:51 +0200762 { HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_ACER_SWITCH5),
763 .driver_data = RMI_DEVICE_OUTPUT_SET_REPORT },
Benjamin Tissoiresba391e52014-05-21 11:15:56 -0400764 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_RMI, HID_ANY_ID, HID_ANY_ID) },
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400765 { }
766};
767MODULE_DEVICE_TABLE(hid, rmi_id);
768
769static struct hid_driver rmi_driver = {
770 .name = "hid-rmi",
771 .id_table = rmi_id,
772 .probe = rmi_probe,
773 .remove = rmi_remove,
Andrew Duggan2f43de62014-12-19 14:45:41 -0800774 .event = rmi_event,
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400775 .raw_event = rmi_raw_event,
Benjamin Tissoiresc94ba062018-05-25 14:51:06 +0200776 .report = rmi_report,
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400777 .input_mapping = rmi_input_mapping,
778 .input_configured = rmi_input_configured,
779#ifdef CONFIG_PM
Andrew Duggan09256362015-07-06 16:48:31 -0700780 .suspend = rmi_suspend,
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400781 .resume = rmi_post_resume,
Andrew Duggan0b2c7a82017-01-05 09:48:58 +0100782 .reset_resume = rmi_post_resume,
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400783#endif
784};
785
786module_hid_driver(rmi_driver);
787
788MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
789MODULE_DESCRIPTION("RMI HID driver");
790MODULE_LICENSE("GPL");