]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
9fb6bf02 BT |
2 | /* |
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 | |
9fb6bf02 BT |
7 | */ |
8 | ||
9 | #include <linux/kernel.h> | |
10 | #include <linux/hid.h> | |
11 | #include <linux/input.h> | |
12 | #include <linux/input/mt.h> | |
0b2c7a89 AD |
13 | #include <linux/irq.h> |
14 | #include <linux/irqdomain.h> | |
9fb6bf02 BT |
15 | #include <linux/module.h> |
16 | #include <linux/pm.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/wait.h> | |
19 | #include <linux/sched.h> | |
0b2c7a89 | 20 | #include <linux/rmi.h> |
9fb6bf02 BT |
21 | #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 */ | |
af43c408 DC |
31 | #define RMI_READ_REQUEST_PENDING 0 |
32 | #define RMI_READ_DATA_PENDING 1 | |
33 | #define RMI_STARTED 2 | |
9fb6bf02 | 34 | |
2f43de60 AD |
35 | /* device flags */ |
36 | #define RMI_DEVICE BIT(0) | |
79364d87 | 37 | #define RMI_DEVICE_HAS_PHYS_BUTTONS BIT(1) |
2f43de60 | 38 | |
7035f3a4 AD |
39 | /* |
40 | * retrieve the ctrl registers | |
41 | * the ctrl register has a size of 20 but a fw bug split it into 16 + 4, | |
42 | * and there is no way to know if the first 20 bytes are here or not. | |
43 | * We use only the first 12 bytes, so get only them. | |
44 | */ | |
45 | #define RMI_F11_CTRL_REG_COUNT 12 | |
46 | ||
9fb6bf02 BT |
47 | enum rmi_mode_type { |
48 | RMI_MODE_OFF = 0, | |
49 | RMI_MODE_ATTN_REPORTS = 1, | |
50 | RMI_MODE_NO_PACKED_ATTN_REPORTS = 2, | |
51 | }; | |
52 | ||
9fb6bf02 BT |
53 | /** |
54 | * struct rmi_data - stores information for hid communication | |
55 | * | |
56 | * @page_mutex: Locks current page to avoid changing pages in unexpected ways. | |
57 | * @page: Keeps track of the current virtual page | |
0b2c7a89 | 58 | * @xport: transport device to be registered with the RMI4 core. |
9fb6bf02 BT |
59 | * |
60 | * @wait: Used for waiting for read data | |
61 | * | |
62 | * @writeReport: output buffer when writing RMI registers | |
63 | * @readReport: input buffer when reading RMI registers | |
64 | * | |
65 | * @input_report_size: size of an input report (advertised by HID) | |
66 | * @output_report_size: size of an output report (advertised by HID) | |
67 | * | |
68 | * @flags: flags for the current device (started, reading, etc...) | |
69 | * | |
9fb6bf02 BT |
70 | * @reset_work: worker which will be called in case of a mouse report |
71 | * @hdev: pointer to the struct hid_device | |
0b2c7a89 AD |
72 | * |
73 | * @device_flags: flags which describe the device | |
74 | * | |
75 | * @domain: the IRQ domain allocated for this RMI4 device | |
76 | * @rmi_irq: the irq that will be used to generate events to rmi-core | |
9fb6bf02 BT |
77 | */ |
78 | struct rmi_data { | |
79 | struct mutex page_mutex; | |
80 | int page; | |
0b2c7a89 | 81 | struct rmi_transport_dev xport; |
9fb6bf02 BT |
82 | |
83 | wait_queue_head_t wait; | |
84 | ||
85 | u8 *writeReport; | |
86 | u8 *readReport; | |
87 | ||
3064a03b AM |
88 | u32 input_report_size; |
89 | u32 output_report_size; | |
9fb6bf02 BT |
90 | |
91 | unsigned long flags; | |
92 | ||
9fb6bf02 BT |
93 | struct work_struct reset_work; |
94 | struct hid_device *hdev; | |
2f43de60 AD |
95 | |
96 | unsigned long device_flags; | |
09256360 | 97 | |
0b2c7a89 AD |
98 | struct irq_domain *domain; |
99 | int rmi_irq; | |
9fb6bf02 BT |
100 | }; |
101 | ||
102 | #define RMI_PAGE(addr) (((addr) >> 8) & 0xff) | |
103 | ||
104 | static int rmi_write_report(struct hid_device *hdev, u8 *report, int len); | |
105 | ||
106 | /** | |
107 | * rmi_set_page - Set RMI page | |
108 | * @hdev: The pointer to the hid_device struct | |
109 | * @page: The new page address. | |
110 | * | |
111 | * RMI devices have 16-bit addressing, but some of the physical | |
112 | * implementations (like SMBus) only have 8-bit addressing. So RMI implements | |
113 | * a page address at 0xff of every page so we can reliable page addresses | |
114 | * every 256 registers. | |
115 | * | |
116 | * The page_mutex lock must be held when this function is entered. | |
117 | * | |
118 | * Returns zero on success, non-zero on failure. | |
119 | */ | |
120 | static int rmi_set_page(struct hid_device *hdev, u8 page) | |
121 | { | |
122 | struct rmi_data *data = hid_get_drvdata(hdev); | |
123 | int retval; | |
124 | ||
125 | data->writeReport[0] = RMI_WRITE_REPORT_ID; | |
126 | data->writeReport[1] = 1; | |
127 | data->writeReport[2] = 0xFF; | |
128 | data->writeReport[4] = page; | |
129 | ||
130 | retval = rmi_write_report(hdev, data->writeReport, | |
131 | data->output_report_size); | |
132 | if (retval != data->output_report_size) { | |
133 | dev_err(&hdev->dev, | |
134 | "%s: set page failed: %d.", __func__, retval); | |
135 | return retval; | |
136 | } | |
137 | ||
138 | data->page = page; | |
139 | return 0; | |
140 | } | |
141 | ||
142 | static int rmi_set_mode(struct hid_device *hdev, u8 mode) | |
143 | { | |
144 | int ret; | |
6dab07df BT |
145 | const u8 txbuf[2] = {RMI_SET_RMI_MODE_REPORT_ID, mode}; |
146 | u8 *buf; | |
9fb6bf02 | 147 | |
6dab07df BT |
148 | buf = kmemdup(txbuf, sizeof(txbuf), GFP_KERNEL); |
149 | if (!buf) | |
150 | return -ENOMEM; | |
151 | ||
152 | ret = hid_hw_raw_request(hdev, RMI_SET_RMI_MODE_REPORT_ID, buf, | |
9fb6bf02 | 153 | sizeof(txbuf), HID_FEATURE_REPORT, HID_REQ_SET_REPORT); |
6dab07df | 154 | kfree(buf); |
9fb6bf02 BT |
155 | if (ret < 0) { |
156 | dev_err(&hdev->dev, "unable to set rmi mode to %d (%d)\n", mode, | |
157 | ret); | |
158 | return ret; | |
159 | } | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
164 | static int rmi_write_report(struct hid_device *hdev, u8 *report, int len) | |
165 | { | |
166 | int ret; | |
167 | ||
168 | ret = hid_hw_output_report(hdev, (void *)report, len); | |
169 | if (ret < 0) { | |
170 | dev_err(&hdev->dev, "failed to write hid report (%d)\n", ret); | |
171 | return ret; | |
172 | } | |
173 | ||
174 | return ret; | |
175 | } | |
176 | ||
0b2c7a89 AD |
177 | static int rmi_hid_read_block(struct rmi_transport_dev *xport, u16 addr, |
178 | void *buf, size_t len) | |
9fb6bf02 | 179 | { |
0b2c7a89 AD |
180 | struct rmi_data *data = container_of(xport, struct rmi_data, xport); |
181 | struct hid_device *hdev = data->hdev; | |
9fb6bf02 BT |
182 | int ret; |
183 | int bytes_read; | |
184 | int bytes_needed; | |
185 | int retries; | |
186 | int read_input_count; | |
187 | ||
188 | mutex_lock(&data->page_mutex); | |
189 | ||
190 | if (RMI_PAGE(addr) != data->page) { | |
191 | ret = rmi_set_page(hdev, RMI_PAGE(addr)); | |
192 | if (ret < 0) | |
193 | goto exit; | |
194 | } | |
195 | ||
196 | for (retries = 5; retries > 0; retries--) { | |
197 | data->writeReport[0] = RMI_READ_ADDR_REPORT_ID; | |
198 | data->writeReport[1] = 0; /* old 1 byte read count */ | |
199 | data->writeReport[2] = addr & 0xFF; | |
200 | data->writeReport[3] = (addr >> 8) & 0xFF; | |
201 | data->writeReport[4] = len & 0xFF; | |
202 | data->writeReport[5] = (len >> 8) & 0xFF; | |
203 | ||
204 | set_bit(RMI_READ_REQUEST_PENDING, &data->flags); | |
205 | ||
206 | ret = rmi_write_report(hdev, data->writeReport, | |
207 | data->output_report_size); | |
208 | if (ret != data->output_report_size) { | |
209 | clear_bit(RMI_READ_REQUEST_PENDING, &data->flags); | |
210 | dev_err(&hdev->dev, | |
211 | "failed to write request output report (%d)\n", | |
212 | ret); | |
213 | goto exit; | |
214 | } | |
215 | ||
216 | bytes_read = 0; | |
217 | bytes_needed = len; | |
218 | while (bytes_read < len) { | |
219 | if (!wait_event_timeout(data->wait, | |
220 | test_bit(RMI_READ_DATA_PENDING, &data->flags), | |
221 | msecs_to_jiffies(1000))) { | |
222 | hid_warn(hdev, "%s: timeout elapsed\n", | |
223 | __func__); | |
224 | ret = -EAGAIN; | |
225 | break; | |
226 | } | |
227 | ||
228 | read_input_count = data->readReport[1]; | |
229 | memcpy(buf + bytes_read, &data->readReport[2], | |
230 | read_input_count < bytes_needed ? | |
231 | read_input_count : bytes_needed); | |
232 | ||
233 | bytes_read += read_input_count; | |
234 | bytes_needed -= read_input_count; | |
235 | clear_bit(RMI_READ_DATA_PENDING, &data->flags); | |
236 | } | |
237 | ||
238 | if (ret >= 0) { | |
239 | ret = 0; | |
240 | break; | |
241 | } | |
242 | } | |
243 | ||
244 | exit: | |
245 | clear_bit(RMI_READ_REQUEST_PENDING, &data->flags); | |
246 | mutex_unlock(&data->page_mutex); | |
247 | return ret; | |
248 | } | |
249 | ||
0b2c7a89 AD |
250 | static int rmi_hid_write_block(struct rmi_transport_dev *xport, u16 addr, |
251 | const void *buf, size_t len) | |
dd8df284 | 252 | { |
0b2c7a89 AD |
253 | struct rmi_data *data = container_of(xport, struct rmi_data, xport); |
254 | struct hid_device *hdev = data->hdev; | |
dd8df284 AD |
255 | int ret; |
256 | ||
257 | mutex_lock(&data->page_mutex); | |
258 | ||
259 | if (RMI_PAGE(addr) != data->page) { | |
260 | ret = rmi_set_page(hdev, RMI_PAGE(addr)); | |
261 | if (ret < 0) | |
262 | goto exit; | |
263 | } | |
264 | ||
265 | data->writeReport[0] = RMI_WRITE_REPORT_ID; | |
266 | data->writeReport[1] = len; | |
267 | data->writeReport[2] = addr & 0xFF; | |
268 | data->writeReport[3] = (addr >> 8) & 0xFF; | |
269 | memcpy(&data->writeReport[4], buf, len); | |
270 | ||
271 | ret = rmi_write_report(hdev, data->writeReport, | |
272 | data->output_report_size); | |
273 | if (ret < 0) { | |
274 | dev_err(&hdev->dev, | |
275 | "failed to write request output report (%d)\n", | |
276 | ret); | |
277 | goto exit; | |
278 | } | |
279 | ret = 0; | |
280 | ||
281 | exit: | |
282 | mutex_unlock(&data->page_mutex); | |
283 | return ret; | |
284 | } | |
285 | ||
9a98b338 AD |
286 | static int rmi_reset_attn_mode(struct hid_device *hdev) |
287 | { | |
288 | struct rmi_data *data = hid_get_drvdata(hdev); | |
0b2c7a89 | 289 | struct rmi_device *rmi_dev = data->xport.rmi_dev; |
9a98b338 AD |
290 | int ret; |
291 | ||
292 | ret = rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS); | |
293 | if (ret) | |
294 | return ret; | |
295 | ||
0b2c7a89 AD |
296 | if (test_bit(RMI_STARTED, &data->flags)) |
297 | ret = rmi_dev->driver->reset_handler(rmi_dev); | |
9a98b338 | 298 | |
0b2c7a89 | 299 | return ret; |
9a98b338 AD |
300 | } |
301 | ||
9fb6bf02 BT |
302 | static void rmi_reset_work(struct work_struct *work) |
303 | { | |
304 | struct rmi_data *hdata = container_of(work, struct rmi_data, | |
305 | reset_work); | |
306 | ||
307 | /* switch the device to RMI if we receive a generic mouse report */ | |
9a98b338 | 308 | rmi_reset_attn_mode(hdata->hdev); |
9fb6bf02 BT |
309 | } |
310 | ||
0b2c7a89 | 311 | static int rmi_input_event(struct hid_device *hdev, u8 *data, int size) |
9fb6bf02 BT |
312 | { |
313 | struct rmi_data *hdata = hid_get_drvdata(hdev); | |
0b2c7a89 AD |
314 | struct rmi_device *rmi_dev = hdata->xport.rmi_dev; |
315 | unsigned long flags; | |
9fb6bf02 | 316 | |
0b2c7a89 | 317 | if (!(test_bit(RMI_STARTED, &hdata->flags))) |
9fb6bf02 BT |
318 | return 0; |
319 | ||
0b2c7a89 | 320 | local_irq_save(flags); |
5b65c2a0 | 321 | |
0b2c7a89 | 322 | rmi_set_attn_data(rmi_dev, data[1], &data[2], size - 2); |
9fb6bf02 | 323 | |
0b2c7a89 | 324 | generic_handle_irq(hdata->rmi_irq); |
9fb6bf02 | 325 | |
0b2c7a89 | 326 | local_irq_restore(flags); |
9fb6bf02 BT |
327 | |
328 | return 1; | |
329 | } | |
330 | ||
331 | static int rmi_read_data_event(struct hid_device *hdev, u8 *data, int size) | |
332 | { | |
333 | struct rmi_data *hdata = hid_get_drvdata(hdev); | |
334 | ||
335 | if (!test_bit(RMI_READ_REQUEST_PENDING, &hdata->flags)) { | |
01a5f8a4 | 336 | hid_dbg(hdev, "no read request pending\n"); |
9fb6bf02 BT |
337 | return 0; |
338 | } | |
339 | ||
340 | memcpy(hdata->readReport, data, size < hdata->input_report_size ? | |
341 | size : hdata->input_report_size); | |
342 | set_bit(RMI_READ_DATA_PENDING, &hdata->flags); | |
343 | wake_up(&hdata->wait); | |
344 | ||
345 | return 1; | |
346 | } | |
347 | ||
5b65c2a0 BT |
348 | static int rmi_check_sanity(struct hid_device *hdev, u8 *data, int size) |
349 | { | |
350 | int valid_size = size; | |
351 | /* | |
352 | * On the Dell XPS 13 9333, the bus sometimes get confused and fills | |
353 | * the report with a sentinel value "ff". Synaptics told us that such | |
354 | * behavior does not comes from the touchpad itself, so we filter out | |
355 | * such reports here. | |
356 | */ | |
357 | ||
358 | while ((data[valid_size - 1] == 0xff) && valid_size > 0) | |
359 | valid_size--; | |
360 | ||
361 | return valid_size; | |
362 | } | |
363 | ||
9fb6bf02 BT |
364 | static int rmi_raw_event(struct hid_device *hdev, |
365 | struct hid_report *report, u8 *data, int size) | |
366 | { | |
ef14a4bf AD |
367 | struct rmi_data *hdata = hid_get_drvdata(hdev); |
368 | ||
369 | if (!(hdata->device_flags & RMI_DEVICE)) | |
370 | return 0; | |
371 | ||
5b65c2a0 BT |
372 | size = rmi_check_sanity(hdev, data, size); |
373 | if (size < 2) | |
374 | return 0; | |
375 | ||
9fb6bf02 BT |
376 | switch (data[0]) { |
377 | case RMI_READ_DATA_REPORT_ID: | |
378 | return rmi_read_data_event(hdev, data, size); | |
379 | case RMI_ATTN_REPORT_ID: | |
380 | return rmi_input_event(hdev, data, size); | |
2f43de60 AD |
381 | default: |
382 | return 1; | |
383 | } | |
384 | ||
385 | return 0; | |
386 | } | |
387 | ||
388 | static int rmi_event(struct hid_device *hdev, struct hid_field *field, | |
389 | struct hid_usage *usage, __s32 value) | |
390 | { | |
391 | struct rmi_data *data = hid_get_drvdata(hdev); | |
392 | ||
393 | if ((data->device_flags & RMI_DEVICE) && | |
394 | (field->application == HID_GD_POINTER || | |
395 | field->application == HID_GD_MOUSE)) { | |
79364d87 AD |
396 | if (data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS) { |
397 | if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) | |
398 | return 0; | |
399 | ||
400 | if ((usage->hid == HID_GD_X || usage->hid == HID_GD_Y) | |
401 | && !value) | |
402 | return 1; | |
403 | } | |
404 | ||
0b2c7a89 | 405 | schedule_work(&data->reset_work); |
2f43de60 | 406 | return 1; |
9fb6bf02 BT |
407 | } |
408 | ||
409 | return 0; | |
410 | } | |
411 | ||
c94ba060 BT |
412 | static void rmi_report(struct hid_device *hid, struct hid_report *report) |
413 | { | |
414 | struct hid_field *field = report->field[0]; | |
415 | ||
416 | if (!(hid->claimed & HID_CLAIMED_INPUT)) | |
417 | return; | |
418 | ||
419 | switch (report->id) { | |
420 | case RMI_READ_DATA_REPORT_ID: | |
421 | /* fall-through */ | |
422 | case RMI_ATTN_REPORT_ID: | |
423 | return; | |
424 | } | |
425 | ||
426 | if (field && field->hidinput && field->hidinput->input) | |
427 | input_sync(field->hidinput->input); | |
428 | } | |
429 | ||
a278e268 | 430 | #ifdef CONFIG_PM |
09256360 AD |
431 | static int rmi_suspend(struct hid_device *hdev, pm_message_t message) |
432 | { | |
7035f3a4 | 433 | struct rmi_data *data = hid_get_drvdata(hdev); |
0b2c7a89 | 434 | struct rmi_device *rmi_dev = data->xport.rmi_dev; |
09256360 AD |
435 | int ret; |
436 | ||
b786ae8e AD |
437 | if (!(data->device_flags & RMI_DEVICE)) |
438 | return 0; | |
439 | ||
0b2c7a89 | 440 | ret = rmi_driver_suspend(rmi_dev, false); |
09256360 | 441 | if (ret) { |
0b2c7a89 | 442 | hid_warn(hdev, "Failed to suspend device: %d\n", ret); |
09256360 AD |
443 | return ret; |
444 | } | |
445 | ||
0b2c7a89 | 446 | return 0; |
9fb6bf02 BT |
447 | } |
448 | ||
449 | static int rmi_post_resume(struct hid_device *hdev) | |
450 | { | |
b786ae8e | 451 | struct rmi_data *data = hid_get_drvdata(hdev); |
0b2c7a89 AD |
452 | struct rmi_device *rmi_dev = data->xport.rmi_dev; |
453 | int ret; | |
b786ae8e AD |
454 | |
455 | if (!(data->device_flags & RMI_DEVICE)) | |
456 | return 0; | |
457 | ||
cac72b99 L |
458 | /* Make sure the HID device is ready to receive events */ |
459 | ret = hid_hw_open(hdev); | |
0b2c7a89 | 460 | if (ret) |
05ba999f | 461 | return ret; |
8414947a | 462 | |
cac72b99 L |
463 | ret = rmi_reset_attn_mode(hdev); |
464 | if (ret) | |
465 | goto out; | |
466 | ||
0b2c7a89 | 467 | ret = rmi_driver_resume(rmi_dev, false); |
9fb6bf02 | 468 | if (ret) { |
0b2c7a89 | 469 | hid_warn(hdev, "Failed to resume device: %d\n", ret); |
cac72b99 | 470 | goto out; |
9fb6bf02 BT |
471 | } |
472 | ||
cac72b99 L |
473 | out: |
474 | hid_hw_close(hdev); | |
475 | return ret; | |
9fb6bf02 | 476 | } |
0b2c7a89 | 477 | #endif /* CONFIG_PM */ |
9fb6bf02 | 478 | |
0b2c7a89 | 479 | static int rmi_hid_reset(struct rmi_transport_dev *xport, u16 reset_addr) |
9fb6bf02 | 480 | { |
0b2c7a89 AD |
481 | struct rmi_data *data = container_of(xport, struct rmi_data, xport); |
482 | struct hid_device *hdev = data->hdev; | |
9fb6bf02 | 483 | |
0b2c7a89 | 484 | return rmi_reset_attn_mode(hdev); |
9fb6bf02 BT |
485 | } |
486 | ||
9154301a | 487 | static int rmi_input_configured(struct hid_device *hdev, struct hid_input *hi) |
9fb6bf02 BT |
488 | { |
489 | struct rmi_data *data = hid_get_drvdata(hdev); | |
490 | struct input_dev *input = hi->input; | |
0b2c7a89 AD |
491 | int ret = 0; |
492 | ||
493 | if (!(data->device_flags & RMI_DEVICE)) | |
494 | return 0; | |
9fb6bf02 | 495 | |
0b2c7a89 | 496 | data->xport.input = input; |
9fb6bf02 BT |
497 | |
498 | hid_dbg(hdev, "Opening low level driver\n"); | |
499 | ret = hid_hw_open(hdev); | |
500 | if (ret) | |
9154301a | 501 | return ret; |
9fb6bf02 BT |
502 | |
503 | /* Allow incoming hid reports */ | |
504 | hid_device_io_start(hdev); | |
505 | ||
506 | ret = rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS); | |
507 | if (ret < 0) { | |
508 | dev_err(&hdev->dev, "failed to set rmi mode\n"); | |
509 | goto exit; | |
510 | } | |
511 | ||
512 | ret = rmi_set_page(hdev, 0); | |
513 | if (ret < 0) { | |
514 | dev_err(&hdev->dev, "failed to set page select to 0.\n"); | |
515 | goto exit; | |
516 | } | |
517 | ||
0b2c7a89 AD |
518 | ret = rmi_register_transport_device(&data->xport); |
519 | if (ret < 0) { | |
520 | dev_err(&hdev->dev, "failed to register transport driver\n"); | |
9154301a | 521 | goto exit; |
9fb6bf02 BT |
522 | } |
523 | ||
524 | set_bit(RMI_STARTED, &data->flags); | |
525 | ||
526 | exit: | |
527 | hid_device_io_stop(hdev); | |
528 | hid_hw_close(hdev); | |
9154301a | 529 | return ret; |
9fb6bf02 BT |
530 | } |
531 | ||
532 | static int rmi_input_mapping(struct hid_device *hdev, | |
533 | struct hid_input *hi, struct hid_field *field, | |
534 | struct hid_usage *usage, unsigned long **bit, int *max) | |
535 | { | |
2f43de60 AD |
536 | struct rmi_data *data = hid_get_drvdata(hdev); |
537 | ||
538 | /* | |
539 | * we want to make HID ignore the advertised HID collection | |
540 | * for RMI deivces | |
541 | */ | |
79364d87 AD |
542 | if (data->device_flags & RMI_DEVICE) { |
543 | if ((data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS) && | |
544 | ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)) | |
545 | return 0; | |
546 | ||
2f43de60 | 547 | return -1; |
79364d87 | 548 | } |
2f43de60 AD |
549 | |
550 | return 0; | |
551 | } | |
552 | ||
553 | static int rmi_check_valid_report_id(struct hid_device *hdev, unsigned type, | |
554 | unsigned id, struct hid_report **report) | |
555 | { | |
556 | int i; | |
557 | ||
558 | *report = hdev->report_enum[type].report_id_hash[id]; | |
559 | if (*report) { | |
560 | for (i = 0; i < (*report)->maxfield; i++) { | |
561 | unsigned app = (*report)->field[i]->application; | |
562 | if ((app & HID_USAGE_PAGE) >= HID_UP_MSVENDOR) | |
563 | return 1; | |
564 | } | |
565 | } | |
566 | ||
567 | return 0; | |
9fb6bf02 BT |
568 | } |
569 | ||
0b2c7a89 AD |
570 | static struct rmi_device_platform_data rmi_hid_pdata = { |
571 | .sensor_pdata = { | |
572 | .sensor_type = rmi_sensor_touchpad, | |
573 | .axis_align.flip_y = true, | |
574 | .dribble = RMI_REG_STATE_ON, | |
575 | .palm_detect = RMI_REG_STATE_OFF, | |
576 | }, | |
577 | }; | |
578 | ||
579 | static const struct rmi_transport_ops hid_rmi_ops = { | |
580 | .write_block = rmi_hid_write_block, | |
581 | .read_block = rmi_hid_read_block, | |
582 | .reset = rmi_hid_reset, | |
583 | }; | |
584 | ||
585 | static void rmi_irq_teardown(void *data) | |
586 | { | |
587 | struct rmi_data *hdata = data; | |
588 | struct irq_domain *domain = hdata->domain; | |
589 | ||
590 | if (!domain) | |
591 | return; | |
592 | ||
593 | irq_dispose_mapping(irq_find_mapping(domain, 0)); | |
594 | ||
595 | irq_domain_remove(domain); | |
596 | hdata->domain = NULL; | |
597 | hdata->rmi_irq = 0; | |
598 | } | |
599 | ||
600 | static int rmi_irq_map(struct irq_domain *h, unsigned int virq, | |
601 | irq_hw_number_t hw_irq_num) | |
602 | { | |
603 | irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq); | |
604 | ||
605 | return 0; | |
606 | } | |
607 | ||
608 | static const struct irq_domain_ops rmi_irq_ops = { | |
609 | .map = rmi_irq_map, | |
610 | }; | |
611 | ||
612 | static int rmi_setup_irq_domain(struct hid_device *hdev) | |
613 | { | |
614 | struct rmi_data *hdata = hid_get_drvdata(hdev); | |
615 | int ret; | |
616 | ||
617 | hdata->domain = irq_domain_create_linear(hdev->dev.fwnode, 1, | |
618 | &rmi_irq_ops, hdata); | |
619 | if (!hdata->domain) | |
620 | return -ENOMEM; | |
621 | ||
622 | ret = devm_add_action_or_reset(&hdev->dev, &rmi_irq_teardown, hdata); | |
623 | if (ret) | |
624 | return ret; | |
625 | ||
626 | hdata->rmi_irq = irq_create_mapping(hdata->domain, 0); | |
627 | if (hdata->rmi_irq <= 0) { | |
628 | hid_err(hdev, "Can't allocate an IRQ\n"); | |
629 | return hdata->rmi_irq < 0 ? hdata->rmi_irq : -ENXIO; | |
630 | } | |
631 | ||
632 | return 0; | |
633 | } | |
634 | ||
9fb6bf02 BT |
635 | static int rmi_probe(struct hid_device *hdev, const struct hid_device_id *id) |
636 | { | |
637 | struct rmi_data *data = NULL; | |
638 | int ret; | |
639 | size_t alloc_size; | |
dd3edeb6 AD |
640 | struct hid_report *input_report; |
641 | struct hid_report *output_report; | |
2f43de60 | 642 | struct hid_report *feature_report; |
9fb6bf02 BT |
643 | |
644 | data = devm_kzalloc(&hdev->dev, sizeof(struct rmi_data), GFP_KERNEL); | |
645 | if (!data) | |
646 | return -ENOMEM; | |
647 | ||
648 | INIT_WORK(&data->reset_work, rmi_reset_work); | |
649 | data->hdev = hdev; | |
650 | ||
651 | hid_set_drvdata(hdev, data); | |
652 | ||
653 | hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS; | |
c94ba060 | 654 | hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC; |
9fb6bf02 BT |
655 | |
656 | ret = hid_parse(hdev); | |
657 | if (ret) { | |
658 | hid_err(hdev, "parse failed\n"); | |
659 | return ret; | |
660 | } | |
661 | ||
79364d87 AD |
662 | if (id->driver_data) |
663 | data->device_flags = id->driver_data; | |
664 | ||
2f43de60 AD |
665 | /* |
666 | * Check for the RMI specific report ids. If they are misisng | |
667 | * simply return and let the events be processed by hid-input | |
668 | */ | |
669 | if (!rmi_check_valid_report_id(hdev, HID_FEATURE_REPORT, | |
670 | RMI_SET_RMI_MODE_REPORT_ID, &feature_report)) { | |
671 | hid_dbg(hdev, "device does not have set mode feature report\n"); | |
672 | goto start; | |
673 | } | |
674 | ||
675 | if (!rmi_check_valid_report_id(hdev, HID_INPUT_REPORT, | |
676 | RMI_ATTN_REPORT_ID, &input_report)) { | |
677 | hid_dbg(hdev, "device does not have attention input report\n"); | |
678 | goto start; | |
dd3edeb6 AD |
679 | } |
680 | ||
b8aed6ea | 681 | data->input_report_size = hid_report_len(input_report); |
dd3edeb6 | 682 | |
2f43de60 AD |
683 | if (!rmi_check_valid_report_id(hdev, HID_OUTPUT_REPORT, |
684 | RMI_WRITE_REPORT_ID, &output_report)) { | |
685 | hid_dbg(hdev, | |
686 | "device does not have rmi write output report\n"); | |
687 | goto start; | |
dd3edeb6 AD |
688 | } |
689 | ||
b8aed6ea | 690 | data->output_report_size = hid_report_len(output_report); |
9fb6bf02 | 691 | |
2f43de60 | 692 | data->device_flags |= RMI_DEVICE; |
9fb6bf02 BT |
693 | alloc_size = data->output_report_size + data->input_report_size; |
694 | ||
695 | data->writeReport = devm_kzalloc(&hdev->dev, alloc_size, GFP_KERNEL); | |
696 | if (!data->writeReport) { | |
0b2c7a89 AD |
697 | hid_err(hdev, "failed to allocate buffer for HID reports\n"); |
698 | return -ENOMEM; | |
9fb6bf02 BT |
699 | } |
700 | ||
701 | data->readReport = data->writeReport + data->output_report_size; | |
702 | ||
703 | init_waitqueue_head(&data->wait); | |
704 | ||
705 | mutex_init(&data->page_mutex); | |
706 | ||
0b2c7a89 AD |
707 | ret = rmi_setup_irq_domain(hdev); |
708 | if (ret) { | |
709 | hid_err(hdev, "failed to allocate IRQ domain\n"); | |
710 | return ret; | |
711 | } | |
712 | ||
713 | if (data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS) | |
714 | rmi_hid_pdata.f30_data.disable = true; | |
715 | ||
716 | data->xport.dev = hdev->dev.parent; | |
717 | data->xport.pdata = rmi_hid_pdata; | |
718 | data->xport.pdata.irq = data->rmi_irq; | |
719 | data->xport.proto_name = "hid"; | |
720 | data->xport.ops = &hid_rmi_ops; | |
721 | ||
2f43de60 | 722 | start: |
9fb6bf02 BT |
723 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
724 | if (ret) { | |
725 | hid_err(hdev, "hw start failed\n"); | |
726 | return ret; | |
727 | } | |
728 | ||
9fb6bf02 BT |
729 | return 0; |
730 | } | |
731 | ||
732 | static void rmi_remove(struct hid_device *hdev) | |
733 | { | |
734 | struct rmi_data *hdata = hid_get_drvdata(hdev); | |
735 | ||
ef14a4bf AD |
736 | if (hdata->device_flags & RMI_DEVICE) { |
737 | clear_bit(RMI_STARTED, &hdata->flags); | |
738 | cancel_work_sync(&hdata->reset_work); | |
739 | rmi_unregister_transport_device(&hdata->xport); | |
740 | } | |
9fb6bf02 BT |
741 | |
742 | hid_hw_stop(hdev); | |
743 | } | |
744 | ||
745 | static const struct hid_device_id rmi_id[] = { | |
e9287099 AD |
746 | { HID_USB_DEVICE(USB_VENDOR_ID_RAZER, USB_DEVICE_ID_RAZER_BLADE_14), |
747 | .driver_data = RMI_DEVICE_HAS_PHYS_BUTTONS }, | |
c7821d0f | 748 | { HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_COVER) }, |
c5293409 | 749 | { HID_USB_DEVICE(USB_VENDOR_ID_PRIMAX, USB_DEVICE_ID_PRIMAX_REZEL) }, |
ba391e5a | 750 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_RMI, HID_ANY_ID, HID_ANY_ID) }, |
9fb6bf02 BT |
751 | { } |
752 | }; | |
753 | MODULE_DEVICE_TABLE(hid, rmi_id); | |
754 | ||
755 | static struct hid_driver rmi_driver = { | |
756 | .name = "hid-rmi", | |
757 | .id_table = rmi_id, | |
758 | .probe = rmi_probe, | |
759 | .remove = rmi_remove, | |
2f43de60 | 760 | .event = rmi_event, |
9fb6bf02 | 761 | .raw_event = rmi_raw_event, |
c94ba060 | 762 | .report = rmi_report, |
9fb6bf02 BT |
763 | .input_mapping = rmi_input_mapping, |
764 | .input_configured = rmi_input_configured, | |
765 | #ifdef CONFIG_PM | |
09256360 | 766 | .suspend = rmi_suspend, |
9fb6bf02 | 767 | .resume = rmi_post_resume, |
0b2c7a89 | 768 | .reset_resume = rmi_post_resume, |
9fb6bf02 BT |
769 | #endif |
770 | }; | |
771 | ||
772 | module_hid_driver(rmi_driver); | |
773 | ||
774 | MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>"); | |
775 | MODULE_DESCRIPTION("RMI HID driver"); | |
776 | MODULE_LICENSE("GPL"); |