blob: 018f80f5fec6f3b1192c960fb6169c5bd902d23c [file] [log] [blame]
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -04001/*
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
6 *
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)
10 * any later version.
11 */
12
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>
18#include <linux/pm.h>
19#include <linux/slab.h>
20#include <linux/wait.h>
21#include <linux/sched.h>
22#include "hid-ids.h"
23
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 */
30
31/* flags */
32#define RMI_READ_REQUEST_PENDING BIT(0)
33#define RMI_READ_DATA_PENDING BIT(1)
34#define RMI_STARTED BIT(2)
35
Andrew Duggan2f43de62014-12-19 14:45:41 -080036/* device flags */
37#define RMI_DEVICE BIT(0)
38
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -040039enum rmi_mode_type {
40 RMI_MODE_OFF = 0,
41 RMI_MODE_ATTN_REPORTS = 1,
42 RMI_MODE_NO_PACKED_ATTN_REPORTS = 2,
43};
44
45struct rmi_function {
46 unsigned page; /* page of the function */
47 u16 query_base_addr; /* base address for queries */
48 u16 command_base_addr; /* base address for commands */
49 u16 control_base_addr; /* base address for controls */
50 u16 data_base_addr; /* base address for datas */
51 unsigned int interrupt_base; /* cross-function interrupt number
52 * (uniq in the device)*/
53 unsigned int interrupt_count; /* number of interrupts */
54 unsigned int report_size; /* size of a report */
55 unsigned long irq_mask; /* mask of the interrupts
56 * (to be applied against ATTN IRQ) */
57};
58
59/**
60 * struct rmi_data - stores information for hid communication
61 *
62 * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
63 * @page: Keeps track of the current virtual page
64 *
65 * @wait: Used for waiting for read data
66 *
67 * @writeReport: output buffer when writing RMI registers
68 * @readReport: input buffer when reading RMI registers
69 *
70 * @input_report_size: size of an input report (advertised by HID)
71 * @output_report_size: size of an output report (advertised by HID)
72 *
73 * @flags: flags for the current device (started, reading, etc...)
74 *
75 * @f11: placeholder of internal RMI function F11 description
76 * @f30: placeholder of internal RMI function F30 description
77 *
78 * @max_fingers: maximum finger count reported by the device
79 * @max_x: maximum x value reported by the device
80 * @max_y: maximum y value reported by the device
81 *
82 * @gpio_led_count: count of GPIOs + LEDs reported by F30
83 * @button_count: actual physical buttons count
84 * @button_mask: button mask used to decode GPIO ATTN reports
85 * @button_state_mask: pull state of the buttons
86 *
87 * @input: pointer to the kernel input device
88 *
89 * @reset_work: worker which will be called in case of a mouse report
90 * @hdev: pointer to the struct hid_device
91 */
92struct rmi_data {
93 struct mutex page_mutex;
94 int page;
95
96 wait_queue_head_t wait;
97
98 u8 *writeReport;
99 u8 *readReport;
100
101 int input_report_size;
102 int output_report_size;
103
104 unsigned long flags;
105
106 struct rmi_function f11;
107 struct rmi_function f30;
108
109 unsigned int max_fingers;
110 unsigned int max_x;
111 unsigned int max_y;
112 unsigned int x_size_mm;
113 unsigned int y_size_mm;
114
115 unsigned int gpio_led_count;
116 unsigned int button_count;
117 unsigned long button_mask;
118 unsigned long button_state_mask;
119
120 struct input_dev *input;
121
122 struct work_struct reset_work;
123 struct hid_device *hdev;
Andrew Duggan2f43de62014-12-19 14:45:41 -0800124
125 unsigned long device_flags;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400126};
127
128#define RMI_PAGE(addr) (((addr) >> 8) & 0xff)
129
130static int rmi_write_report(struct hid_device *hdev, u8 *report, int len);
131
132/**
133 * rmi_set_page - Set RMI page
134 * @hdev: The pointer to the hid_device struct
135 * @page: The new page address.
136 *
137 * RMI devices have 16-bit addressing, but some of the physical
138 * implementations (like SMBus) only have 8-bit addressing. So RMI implements
139 * a page address at 0xff of every page so we can reliable page addresses
140 * every 256 registers.
141 *
142 * The page_mutex lock must be held when this function is entered.
143 *
144 * Returns zero on success, non-zero on failure.
145 */
146static int rmi_set_page(struct hid_device *hdev, u8 page)
147{
148 struct rmi_data *data = hid_get_drvdata(hdev);
149 int retval;
150
151 data->writeReport[0] = RMI_WRITE_REPORT_ID;
152 data->writeReport[1] = 1;
153 data->writeReport[2] = 0xFF;
154 data->writeReport[4] = page;
155
156 retval = rmi_write_report(hdev, data->writeReport,
157 data->output_report_size);
158 if (retval != data->output_report_size) {
159 dev_err(&hdev->dev,
160 "%s: set page failed: %d.", __func__, retval);
161 return retval;
162 }
163
164 data->page = page;
165 return 0;
166}
167
168static int rmi_set_mode(struct hid_device *hdev, u8 mode)
169{
170 int ret;
171 u8 txbuf[2] = {RMI_SET_RMI_MODE_REPORT_ID, mode};
172
173 ret = hid_hw_raw_request(hdev, RMI_SET_RMI_MODE_REPORT_ID, txbuf,
174 sizeof(txbuf), HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
175 if (ret < 0) {
176 dev_err(&hdev->dev, "unable to set rmi mode to %d (%d)\n", mode,
177 ret);
178 return ret;
179 }
180
181 return 0;
182}
183
184static int rmi_write_report(struct hid_device *hdev, u8 *report, int len)
185{
186 int ret;
187
188 ret = hid_hw_output_report(hdev, (void *)report, len);
189 if (ret < 0) {
190 dev_err(&hdev->dev, "failed to write hid report (%d)\n", ret);
191 return ret;
192 }
193
194 return ret;
195}
196
197static int rmi_read_block(struct hid_device *hdev, u16 addr, void *buf,
198 const int len)
199{
200 struct rmi_data *data = hid_get_drvdata(hdev);
201 int ret;
202 int bytes_read;
203 int bytes_needed;
204 int retries;
205 int read_input_count;
206
207 mutex_lock(&data->page_mutex);
208
209 if (RMI_PAGE(addr) != data->page) {
210 ret = rmi_set_page(hdev, RMI_PAGE(addr));
211 if (ret < 0)
212 goto exit;
213 }
214
215 for (retries = 5; retries > 0; retries--) {
216 data->writeReport[0] = RMI_READ_ADDR_REPORT_ID;
217 data->writeReport[1] = 0; /* old 1 byte read count */
218 data->writeReport[2] = addr & 0xFF;
219 data->writeReport[3] = (addr >> 8) & 0xFF;
220 data->writeReport[4] = len & 0xFF;
221 data->writeReport[5] = (len >> 8) & 0xFF;
222
223 set_bit(RMI_READ_REQUEST_PENDING, &data->flags);
224
225 ret = rmi_write_report(hdev, data->writeReport,
226 data->output_report_size);
227 if (ret != data->output_report_size) {
228 clear_bit(RMI_READ_REQUEST_PENDING, &data->flags);
229 dev_err(&hdev->dev,
230 "failed to write request output report (%d)\n",
231 ret);
232 goto exit;
233 }
234
235 bytes_read = 0;
236 bytes_needed = len;
237 while (bytes_read < len) {
238 if (!wait_event_timeout(data->wait,
239 test_bit(RMI_READ_DATA_PENDING, &data->flags),
240 msecs_to_jiffies(1000))) {
241 hid_warn(hdev, "%s: timeout elapsed\n",
242 __func__);
243 ret = -EAGAIN;
244 break;
245 }
246
247 read_input_count = data->readReport[1];
248 memcpy(buf + bytes_read, &data->readReport[2],
249 read_input_count < bytes_needed ?
250 read_input_count : bytes_needed);
251
252 bytes_read += read_input_count;
253 bytes_needed -= read_input_count;
254 clear_bit(RMI_READ_DATA_PENDING, &data->flags);
255 }
256
257 if (ret >= 0) {
258 ret = 0;
259 break;
260 }
261 }
262
263exit:
264 clear_bit(RMI_READ_REQUEST_PENDING, &data->flags);
265 mutex_unlock(&data->page_mutex);
266 return ret;
267}
268
269static inline int rmi_read(struct hid_device *hdev, u16 addr, void *buf)
270{
271 return rmi_read_block(hdev, addr, buf, 1);
272}
273
274static void rmi_f11_process_touch(struct rmi_data *hdata, int slot,
275 u8 finger_state, u8 *touch_data)
276{
277 int x, y, wx, wy;
278 int wide, major, minor;
279 int z;
280
281 input_mt_slot(hdata->input, slot);
282 input_mt_report_slot_state(hdata->input, MT_TOOL_FINGER,
283 finger_state == 0x01);
284 if (finger_state == 0x01) {
Andrew Duggan876e7a82014-05-15 13:52:29 -0700285 x = (touch_data[0] << 4) | (touch_data[2] & 0x0F);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400286 y = (touch_data[1] << 4) | (touch_data[2] >> 4);
Andrew Duggan876e7a82014-05-15 13:52:29 -0700287 wx = touch_data[3] & 0x0F;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400288 wy = touch_data[3] >> 4;
289 wide = (wx > wy);
290 major = max(wx, wy);
291 minor = min(wx, wy);
292 z = touch_data[4];
293
294 /* y is inverted */
295 y = hdata->max_y - y;
296
297 input_event(hdata->input, EV_ABS, ABS_MT_POSITION_X, x);
298 input_event(hdata->input, EV_ABS, ABS_MT_POSITION_Y, y);
299 input_event(hdata->input, EV_ABS, ABS_MT_ORIENTATION, wide);
300 input_event(hdata->input, EV_ABS, ABS_MT_PRESSURE, z);
301 input_event(hdata->input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
302 input_event(hdata->input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
303 }
304}
305
306static void rmi_reset_work(struct work_struct *work)
307{
308 struct rmi_data *hdata = container_of(work, struct rmi_data,
309 reset_work);
310
311 /* switch the device to RMI if we receive a generic mouse report */
312 rmi_set_mode(hdata->hdev, RMI_MODE_ATTN_REPORTS);
313}
314
315static inline int rmi_schedule_reset(struct hid_device *hdev)
316{
317 struct rmi_data *hdata = hid_get_drvdata(hdev);
318 return schedule_work(&hdata->reset_work);
319}
320
321static int rmi_f11_input_event(struct hid_device *hdev, u8 irq, u8 *data,
322 int size)
323{
324 struct rmi_data *hdata = hid_get_drvdata(hdev);
325 int offset;
326 int i;
327
Benjamin Tissoires5b65c2a2014-09-10 18:02:37 -0700328 if (!(irq & hdata->f11.irq_mask) || size <= 0)
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400329 return 0;
330
331 offset = (hdata->max_fingers >> 2) + 1;
332 for (i = 0; i < hdata->max_fingers; i++) {
333 int fs_byte_position = i >> 2;
334 int fs_bit_position = (i & 0x3) << 1;
335 int finger_state = (data[fs_byte_position] >> fs_bit_position) &
336 0x03;
Benjamin Tissoires5b65c2a2014-09-10 18:02:37 -0700337 int position = offset + 5 * i;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400338
Benjamin Tissoires5b65c2a2014-09-10 18:02:37 -0700339 if (position + 5 > size) {
340 /* partial report, go on with what we received */
341 printk_once(KERN_WARNING
342 "%s %s: Detected incomplete finger report. Finger reports may occasionally get dropped on this platform.\n",
343 dev_driver_string(&hdev->dev),
344 dev_name(&hdev->dev));
345 hid_dbg(hdev, "Incomplete finger report\n");
346 break;
347 }
348
349 rmi_f11_process_touch(hdata, i, finger_state, &data[position]);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400350 }
351 input_mt_sync_frame(hdata->input);
352 input_sync(hdata->input);
353 return hdata->f11.report_size;
354}
355
356static int rmi_f30_input_event(struct hid_device *hdev, u8 irq, u8 *data,
357 int size)
358{
359 struct rmi_data *hdata = hid_get_drvdata(hdev);
360 int i;
361 int button = 0;
362 bool value;
363
364 if (!(irq & hdata->f30.irq_mask))
365 return 0;
366
Benjamin Tissoires5b65c2a2014-09-10 18:02:37 -0700367 if (size < (int)hdata->f30.report_size) {
368 hid_warn(hdev, "Click Button pressed, but the click data is missing\n");
369 return 0;
370 }
371
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400372 for (i = 0; i < hdata->gpio_led_count; i++) {
373 if (test_bit(i, &hdata->button_mask)) {
374 value = (data[i / 8] >> (i & 0x07)) & BIT(0);
375 if (test_bit(i, &hdata->button_state_mask))
376 value = !value;
377 input_event(hdata->input, EV_KEY, BTN_LEFT + button++,
378 value);
379 }
380 }
381 return hdata->f30.report_size;
382}
383
384static int rmi_input_event(struct hid_device *hdev, u8 *data, int size)
385{
386 struct rmi_data *hdata = hid_get_drvdata(hdev);
387 unsigned long irq_mask = 0;
388 unsigned index = 2;
389
390 if (!(test_bit(RMI_STARTED, &hdata->flags)))
391 return 0;
392
393 irq_mask |= hdata->f11.irq_mask;
394 irq_mask |= hdata->f30.irq_mask;
395
396 if (data[1] & ~irq_mask)
Andrew Duggan01a5f8a2014-07-11 14:35:51 -0700397 hid_dbg(hdev, "unknown intr source:%02lx %s:%d\n",
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400398 data[1] & ~irq_mask, __FILE__, __LINE__);
399
400 if (hdata->f11.interrupt_base < hdata->f30.interrupt_base) {
401 index += rmi_f11_input_event(hdev, data[1], &data[index],
402 size - index);
403 index += rmi_f30_input_event(hdev, data[1], &data[index],
404 size - index);
405 } else {
406 index += rmi_f30_input_event(hdev, data[1], &data[index],
407 size - index);
408 index += rmi_f11_input_event(hdev, data[1], &data[index],
409 size - index);
410 }
411
412 return 1;
413}
414
415static int rmi_read_data_event(struct hid_device *hdev, u8 *data, int size)
416{
417 struct rmi_data *hdata = hid_get_drvdata(hdev);
418
419 if (!test_bit(RMI_READ_REQUEST_PENDING, &hdata->flags)) {
Andrew Duggan01a5f8a2014-07-11 14:35:51 -0700420 hid_dbg(hdev, "no read request pending\n");
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400421 return 0;
422 }
423
424 memcpy(hdata->readReport, data, size < hdata->input_report_size ?
425 size : hdata->input_report_size);
426 set_bit(RMI_READ_DATA_PENDING, &hdata->flags);
427 wake_up(&hdata->wait);
428
429 return 1;
430}
431
Benjamin Tissoires5b65c2a2014-09-10 18:02:37 -0700432static int rmi_check_sanity(struct hid_device *hdev, u8 *data, int size)
433{
434 int valid_size = size;
435 /*
436 * On the Dell XPS 13 9333, the bus sometimes get confused and fills
437 * the report with a sentinel value "ff". Synaptics told us that such
438 * behavior does not comes from the touchpad itself, so we filter out
439 * such reports here.
440 */
441
442 while ((data[valid_size - 1] == 0xff) && valid_size > 0)
443 valid_size--;
444
445 return valid_size;
446}
447
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400448static int rmi_raw_event(struct hid_device *hdev,
449 struct hid_report *report, u8 *data, int size)
450{
Benjamin Tissoires5b65c2a2014-09-10 18:02:37 -0700451 size = rmi_check_sanity(hdev, data, size);
452 if (size < 2)
453 return 0;
454
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400455 switch (data[0]) {
456 case RMI_READ_DATA_REPORT_ID:
457 return rmi_read_data_event(hdev, data, size);
458 case RMI_ATTN_REPORT_ID:
459 return rmi_input_event(hdev, data, size);
Andrew Duggan2f43de62014-12-19 14:45:41 -0800460 default:
461 return 1;
462 }
463
464 return 0;
465}
466
467static int rmi_event(struct hid_device *hdev, struct hid_field *field,
468 struct hid_usage *usage, __s32 value)
469{
470 struct rmi_data *data = hid_get_drvdata(hdev);
471
472 if ((data->device_flags & RMI_DEVICE) &&
473 (field->application == HID_GD_POINTER ||
474 field->application == HID_GD_MOUSE)) {
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400475 rmi_schedule_reset(hdev);
Andrew Duggan2f43de62014-12-19 14:45:41 -0800476 return 1;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400477 }
478
479 return 0;
480}
481
Geert Uytterhoevena278e262014-06-11 21:03:18 +0200482#ifdef CONFIG_PM
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400483static int rmi_post_reset(struct hid_device *hdev)
484{
485 return rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
486}
487
488static int rmi_post_resume(struct hid_device *hdev)
489{
490 return rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
491}
Geert Uytterhoevena278e262014-06-11 21:03:18 +0200492#endif /* CONFIG_PM */
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400493
494#define RMI4_MAX_PAGE 0xff
495#define RMI4_PAGE_SIZE 0x0100
496
497#define PDT_START_SCAN_LOCATION 0x00e9
498#define PDT_END_SCAN_LOCATION 0x0005
499#define RMI4_END_OF_PDT(id) ((id) == 0x00 || (id) == 0xff)
500
501struct pdt_entry {
502 u8 query_base_addr:8;
503 u8 command_base_addr:8;
504 u8 control_base_addr:8;
505 u8 data_base_addr:8;
506 u8 interrupt_source_count:3;
507 u8 bits3and4:2;
508 u8 function_version:2;
509 u8 bit7:1;
510 u8 function_number:8;
511} __attribute__((__packed__));
512
513static inline unsigned long rmi_gen_mask(unsigned irq_base, unsigned irq_count)
514{
515 return GENMASK(irq_count + irq_base - 1, irq_base);
516}
517
518static void rmi_register_function(struct rmi_data *data,
519 struct pdt_entry *pdt_entry, int page, unsigned interrupt_count)
520{
521 struct rmi_function *f = NULL;
522 u16 page_base = page << 8;
523
524 switch (pdt_entry->function_number) {
525 case 0x11:
526 f = &data->f11;
527 break;
528 case 0x30:
529 f = &data->f30;
530 break;
531 }
532
533 if (f) {
534 f->page = page;
535 f->query_base_addr = page_base | pdt_entry->query_base_addr;
536 f->command_base_addr = page_base | pdt_entry->command_base_addr;
537 f->control_base_addr = page_base | pdt_entry->control_base_addr;
538 f->data_base_addr = page_base | pdt_entry->data_base_addr;
539 f->interrupt_base = interrupt_count;
540 f->interrupt_count = pdt_entry->interrupt_source_count;
541 f->irq_mask = rmi_gen_mask(f->interrupt_base,
542 f->interrupt_count);
543 }
544}
545
546static int rmi_scan_pdt(struct hid_device *hdev)
547{
548 struct rmi_data *data = hid_get_drvdata(hdev);
549 struct pdt_entry entry;
550 int page;
551 bool page_has_function;
552 int i;
553 int retval;
554 int interrupt = 0;
555 u16 page_start, pdt_start , pdt_end;
556
557 hid_info(hdev, "Scanning PDT...\n");
558
559 for (page = 0; (page <= RMI4_MAX_PAGE); page++) {
560 page_start = RMI4_PAGE_SIZE * page;
561 pdt_start = page_start + PDT_START_SCAN_LOCATION;
562 pdt_end = page_start + PDT_END_SCAN_LOCATION;
563
564 page_has_function = false;
565 for (i = pdt_start; i >= pdt_end; i -= sizeof(entry)) {
566 retval = rmi_read_block(hdev, i, &entry, sizeof(entry));
567 if (retval) {
568 hid_err(hdev,
569 "Read of PDT entry at %#06x failed.\n",
570 i);
571 goto error_exit;
572 }
573
574 if (RMI4_END_OF_PDT(entry.function_number))
575 break;
576
577 page_has_function = true;
578
579 hid_info(hdev, "Found F%02X on page %#04x\n",
580 entry.function_number, page);
581
582 rmi_register_function(data, &entry, page, interrupt);
583 interrupt += entry.interrupt_source_count;
584 }
585
586 if (!page_has_function)
587 break;
588 }
589
590 hid_info(hdev, "%s: Done with PDT scan.\n", __func__);
591 retval = 0;
592
593error_exit:
594 return retval;
595}
596
597static int rmi_populate_f11(struct hid_device *hdev)
598{
599 struct rmi_data *data = hid_get_drvdata(hdev);
600 u8 buf[20];
601 int ret;
Andrew Dugganf15475c2014-05-02 11:14:16 -0700602 bool has_query9;
Andrew Duggan9e2c3272014-07-11 14:35:50 -0700603 bool has_query10 = false;
Andrew Dugganf15475c2014-05-02 11:14:16 -0700604 bool has_query11;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400605 bool has_query12;
Andrew Duggan84149472014-12-08 15:01:59 -0800606 bool has_query27;
607 bool has_query28;
608 bool has_query36 = false;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400609 bool has_physical_props;
Andrew Duggan9e2c3272014-07-11 14:35:50 -0700610 bool has_gestures;
611 bool has_rel;
Andrew Duggan84149472014-12-08 15:01:59 -0800612 bool has_data40 = false;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400613 unsigned x_size, y_size;
Andrew Duggan84149472014-12-08 15:01:59 -0800614 u16 query_offset;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400615
616 if (!data->f11.query_base_addr) {
617 hid_err(hdev, "No 2D sensor found, giving up.\n");
618 return -ENODEV;
619 }
620
621 /* query 0 contains some useful information */
622 ret = rmi_read(hdev, data->f11.query_base_addr, buf);
623 if (ret) {
624 hid_err(hdev, "can not get query 0: %d.\n", ret);
625 return ret;
626 }
Andrew Dugganf15475c2014-05-02 11:14:16 -0700627 has_query9 = !!(buf[0] & BIT(3));
628 has_query11 = !!(buf[0] & BIT(4));
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400629 has_query12 = !!(buf[0] & BIT(5));
Andrew Duggan84149472014-12-08 15:01:59 -0800630 has_query27 = !!(buf[0] & BIT(6));
631 has_query28 = !!(buf[0] & BIT(7));
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400632
633 /* query 1 to get the max number of fingers */
634 ret = rmi_read(hdev, data->f11.query_base_addr + 1, buf);
635 if (ret) {
636 hid_err(hdev, "can not get NumberOfFingers: %d.\n", ret);
637 return ret;
638 }
639 data->max_fingers = (buf[0] & 0x07) + 1;
640 if (data->max_fingers > 5)
641 data->max_fingers = 10;
642
643 data->f11.report_size = data->max_fingers * 5 +
644 DIV_ROUND_UP(data->max_fingers, 4);
645
646 if (!(buf[0] & BIT(4))) {
647 hid_err(hdev, "No absolute events, giving up.\n");
648 return -ENODEV;
649 }
650
Andrew Duggan9e2c3272014-07-11 14:35:50 -0700651 has_rel = !!(buf[0] & BIT(3));
652 has_gestures = !!(buf[0] & BIT(5));
653
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400654 /*
Andrew Duggan9e2c3272014-07-11 14:35:50 -0700655 * At least 4 queries are guaranteed to be present in F11
656 * +1 for query 5 which is present since absolute events are
657 * reported and +1 for query 12.
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400658 */
Andrew Duggan84149472014-12-08 15:01:59 -0800659 query_offset = 6;
Andrew Duggan9e2c3272014-07-11 14:35:50 -0700660
661 if (has_rel)
Andrew Duggan84149472014-12-08 15:01:59 -0800662 ++query_offset; /* query 6 is present */
Andrew Duggan9e2c3272014-07-11 14:35:50 -0700663
Andrew Duggancabd9b52014-12-08 15:02:00 -0800664 if (has_gestures) {
665 /* query 8 to find out if query 10 exists */
666 ret = rmi_read(hdev,
667 data->f11.query_base_addr + query_offset + 1, buf);
668 if (ret) {
669 hid_err(hdev, "can not read gesture information: %d.\n",
670 ret);
671 return ret;
672 }
673 has_query10 = !!(buf[0] & BIT(2));
674
Andrew Duggan84149472014-12-08 15:01:59 -0800675 query_offset += 2; /* query 7 and 8 are present */
Andrew Duggancabd9b52014-12-08 15:02:00 -0800676 }
Andrew Dugganf15475c2014-05-02 11:14:16 -0700677
678 if (has_query9)
Andrew Duggan84149472014-12-08 15:01:59 -0800679 ++query_offset;
Andrew Dugganf15475c2014-05-02 11:14:16 -0700680
681 if (has_query10)
Andrew Duggan84149472014-12-08 15:01:59 -0800682 ++query_offset;
Andrew Dugganf15475c2014-05-02 11:14:16 -0700683
684 if (has_query11)
Andrew Duggan84149472014-12-08 15:01:59 -0800685 ++query_offset;
Andrew Dugganf15475c2014-05-02 11:14:16 -0700686
687 /* query 12 to know if the physical properties are reported */
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400688 if (has_query12) {
Andrew Dugganf15475c2014-05-02 11:14:16 -0700689 ret = rmi_read(hdev, data->f11.query_base_addr
Andrew Duggan84149472014-12-08 15:01:59 -0800690 + query_offset, buf);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400691 if (ret) {
692 hid_err(hdev, "can not get query 12: %d.\n", ret);
693 return ret;
694 }
695 has_physical_props = !!(buf[0] & BIT(5));
696
697 if (has_physical_props) {
Andrew Duggan84149472014-12-08 15:01:59 -0800698 query_offset += 1;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400699 ret = rmi_read_block(hdev,
Andrew Dugganf15475c2014-05-02 11:14:16 -0700700 data->f11.query_base_addr
Andrew Duggan84149472014-12-08 15:01:59 -0800701 + query_offset, buf, 4);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400702 if (ret) {
703 hid_err(hdev, "can not read query 15-18: %d.\n",
704 ret);
705 return ret;
706 }
707
708 x_size = buf[0] | (buf[1] << 8);
709 y_size = buf[2] | (buf[3] << 8);
710
711 data->x_size_mm = DIV_ROUND_CLOSEST(x_size, 10);
712 data->y_size_mm = DIV_ROUND_CLOSEST(y_size, 10);
713
714 hid_info(hdev, "%s: size in mm: %d x %d\n",
715 __func__, data->x_size_mm, data->y_size_mm);
Andrew Duggan84149472014-12-08 15:01:59 -0800716
717 /*
718 * query 15 - 18 contain the size of the sensor
719 * and query 19 - 26 contain bezel dimensions
720 */
721 query_offset += 12;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400722 }
723 }
724
Andrew Duggan84149472014-12-08 15:01:59 -0800725 if (has_query27)
726 ++query_offset;
727
728 if (has_query28) {
729 ret = rmi_read(hdev, data->f11.query_base_addr
730 + query_offset, buf);
731 if (ret) {
732 hid_err(hdev, "can not get query 28: %d.\n", ret);
733 return ret;
734 }
735
736 has_query36 = !!(buf[0] & BIT(6));
737 }
738
739 if (has_query36) {
740 query_offset += 2;
741 ret = rmi_read(hdev, data->f11.query_base_addr
742 + query_offset, buf);
743 if (ret) {
744 hid_err(hdev, "can not get query 36: %d.\n", ret);
745 return ret;
746 }
747
748 has_data40 = !!(buf[0] & BIT(5));
749 }
750
751
752 if (has_data40)
753 data->f11.report_size += data->max_fingers * 2;
754
Benjamin Tissoiresdcce5832014-04-24 18:26:38 -0400755 /*
756 * retrieve the ctrl registers
757 * the ctrl register has a size of 20 but a fw bug split it into 16 + 4,
758 * and there is no way to know if the first 20 bytes are here or not.
759 * We use only the first 10 bytes, so get only them.
760 */
761 ret = rmi_read_block(hdev, data->f11.control_base_addr, buf, 10);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400762 if (ret) {
Benjamin Tissoiresdcce5832014-04-24 18:26:38 -0400763 hid_err(hdev, "can not read ctrl block of size 10: %d.\n", ret);
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400764 return ret;
765 }
766
767 data->max_x = buf[6] | (buf[7] << 8);
768 data->max_y = buf[8] | (buf[9] << 8);
769
770 return 0;
771}
772
773static int rmi_populate_f30(struct hid_device *hdev)
774{
775 struct rmi_data *data = hid_get_drvdata(hdev);
776 u8 buf[20];
777 int ret;
778 bool has_gpio, has_led;
779 unsigned bytes_per_ctrl;
780 u8 ctrl2_addr;
781 int ctrl2_3_length;
782 int i;
783
784 /* function F30 is for physical buttons */
785 if (!data->f30.query_base_addr) {
786 hid_err(hdev, "No GPIO/LEDs found, giving up.\n");
787 return -ENODEV;
788 }
789
790 ret = rmi_read_block(hdev, data->f30.query_base_addr, buf, 2);
791 if (ret) {
792 hid_err(hdev, "can not get F30 query registers: %d.\n", ret);
793 return ret;
794 }
795
796 has_gpio = !!(buf[0] & BIT(3));
797 has_led = !!(buf[0] & BIT(2));
798 data->gpio_led_count = buf[1] & 0x1f;
799
800 /* retrieve ctrl 2 & 3 registers */
801 bytes_per_ctrl = (data->gpio_led_count + 7) / 8;
802 /* Ctrl0 is present only if both has_gpio and has_led are set*/
803 ctrl2_addr = (has_gpio && has_led) ? bytes_per_ctrl : 0;
804 /* Ctrl1 is always be present */
805 ctrl2_addr += bytes_per_ctrl;
806 ctrl2_3_length = 2 * bytes_per_ctrl;
807
808 data->f30.report_size = bytes_per_ctrl;
809
810 ret = rmi_read_block(hdev, data->f30.control_base_addr + ctrl2_addr,
811 buf, ctrl2_3_length);
812 if (ret) {
813 hid_err(hdev, "can not read ctrl 2&3 block of size %d: %d.\n",
814 ctrl2_3_length, ret);
815 return ret;
816 }
817
818 for (i = 0; i < data->gpio_led_count; i++) {
819 int byte_position = i >> 3;
820 int bit_position = i & 0x07;
821 u8 dir_byte = buf[byte_position];
822 u8 data_byte = buf[byte_position + bytes_per_ctrl];
823 bool dir = (dir_byte >> bit_position) & BIT(0);
824 bool dat = (data_byte >> bit_position) & BIT(0);
825
826 if (dir == 0) {
827 /* input mode */
828 if (dat) {
829 /* actual buttons have pull up resistor */
830 data->button_count++;
831 set_bit(i, &data->button_mask);
832 set_bit(i, &data->button_state_mask);
833 }
834 }
835
836 }
837
838 return 0;
839}
840
841static int rmi_populate(struct hid_device *hdev)
842{
843 int ret;
844
845 ret = rmi_scan_pdt(hdev);
846 if (ret) {
847 hid_err(hdev, "PDT scan failed with code %d.\n", ret);
848 return ret;
849 }
850
851 ret = rmi_populate_f11(hdev);
852 if (ret) {
853 hid_err(hdev, "Error while initializing F11 (%d).\n", ret);
854 return ret;
855 }
856
857 ret = rmi_populate_f30(hdev);
858 if (ret)
859 hid_warn(hdev, "Error while initializing F30 (%d).\n", ret);
860
861 return 0;
862}
863
864static void rmi_input_configured(struct hid_device *hdev, struct hid_input *hi)
865{
866 struct rmi_data *data = hid_get_drvdata(hdev);
867 struct input_dev *input = hi->input;
868 int ret;
869 int res_x, res_y, i;
870
871 data->input = input;
872
873 hid_dbg(hdev, "Opening low level driver\n");
874 ret = hid_hw_open(hdev);
875 if (ret)
876 return;
877
Andrew Duggan2f43de62014-12-19 14:45:41 -0800878 if (!(data->device_flags & RMI_DEVICE))
879 return;
880
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400881 /* Allow incoming hid reports */
882 hid_device_io_start(hdev);
883
884 ret = rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
885 if (ret < 0) {
886 dev_err(&hdev->dev, "failed to set rmi mode\n");
887 goto exit;
888 }
889
890 ret = rmi_set_page(hdev, 0);
891 if (ret < 0) {
892 dev_err(&hdev->dev, "failed to set page select to 0.\n");
893 goto exit;
894 }
895
896 ret = rmi_populate(hdev);
897 if (ret)
898 goto exit;
899
900 __set_bit(EV_ABS, input->evbit);
901 input_set_abs_params(input, ABS_MT_POSITION_X, 1, data->max_x, 0, 0);
902 input_set_abs_params(input, ABS_MT_POSITION_Y, 1, data->max_y, 0, 0);
903
Jiri Kosinab668fdc2014-05-13 21:17:29 +0200904 if (data->x_size_mm && data->y_size_mm) {
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400905 res_x = (data->max_x - 1) / data->x_size_mm;
Jiri Kosinab668fdc2014-05-13 21:17:29 +0200906 res_y = (data->max_y - 1) / data->y_size_mm;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400907
908 input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
909 input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
910 }
911
912 input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
913 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xff, 0, 0);
914 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 0x0f, 0, 0);
915 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 0x0f, 0, 0);
916
917 input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
918
919 if (data->button_count) {
920 __set_bit(EV_KEY, input->evbit);
921 for (i = 0; i < data->button_count; i++)
922 __set_bit(BTN_LEFT + i, input->keybit);
923
924 if (data->button_count == 1)
925 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
926 }
927
928 set_bit(RMI_STARTED, &data->flags);
929
930exit:
931 hid_device_io_stop(hdev);
932 hid_hw_close(hdev);
933}
934
935static int rmi_input_mapping(struct hid_device *hdev,
936 struct hid_input *hi, struct hid_field *field,
937 struct hid_usage *usage, unsigned long **bit, int *max)
938{
Andrew Duggan2f43de62014-12-19 14:45:41 -0800939 struct rmi_data *data = hid_get_drvdata(hdev);
940
941 /*
942 * we want to make HID ignore the advertised HID collection
943 * for RMI deivces
944 */
945 if (data->device_flags & RMI_DEVICE)
946 return -1;
947
948 return 0;
949}
950
951static int rmi_check_valid_report_id(struct hid_device *hdev, unsigned type,
952 unsigned id, struct hid_report **report)
953{
954 int i;
955
956 *report = hdev->report_enum[type].report_id_hash[id];
957 if (*report) {
958 for (i = 0; i < (*report)->maxfield; i++) {
959 unsigned app = (*report)->field[i]->application;
960 if ((app & HID_USAGE_PAGE) >= HID_UP_MSVENDOR)
961 return 1;
962 }
963 }
964
965 return 0;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400966}
967
968static int rmi_probe(struct hid_device *hdev, const struct hid_device_id *id)
969{
970 struct rmi_data *data = NULL;
971 int ret;
972 size_t alloc_size;
Andrew Duggandd3edeb2014-07-17 16:14:44 -0700973 struct hid_report *input_report;
974 struct hid_report *output_report;
Andrew Duggan2f43de62014-12-19 14:45:41 -0800975 struct hid_report *feature_report;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -0400976
977 data = devm_kzalloc(&hdev->dev, sizeof(struct rmi_data), GFP_KERNEL);
978 if (!data)
979 return -ENOMEM;
980
981 INIT_WORK(&data->reset_work, rmi_reset_work);
982 data->hdev = hdev;
983
984 hid_set_drvdata(hdev, data);
985
986 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
987
988 ret = hid_parse(hdev);
989 if (ret) {
990 hid_err(hdev, "parse failed\n");
991 return ret;
992 }
993
Andrew Duggan2f43de62014-12-19 14:45:41 -0800994 /*
995 * Check for the RMI specific report ids. If they are misisng
996 * simply return and let the events be processed by hid-input
997 */
998 if (!rmi_check_valid_report_id(hdev, HID_FEATURE_REPORT,
999 RMI_SET_RMI_MODE_REPORT_ID, &feature_report)) {
1000 hid_dbg(hdev, "device does not have set mode feature report\n");
1001 goto start;
1002 }
1003
1004 if (!rmi_check_valid_report_id(hdev, HID_INPUT_REPORT,
1005 RMI_ATTN_REPORT_ID, &input_report)) {
1006 hid_dbg(hdev, "device does not have attention input report\n");
1007 goto start;
Andrew Duggandd3edeb2014-07-17 16:14:44 -07001008 }
1009
1010 data->input_report_size = (input_report->size >> 3) + 1 /* report id */;
1011
Andrew Duggan2f43de62014-12-19 14:45:41 -08001012 if (!rmi_check_valid_report_id(hdev, HID_OUTPUT_REPORT,
1013 RMI_WRITE_REPORT_ID, &output_report)) {
1014 hid_dbg(hdev,
1015 "device does not have rmi write output report\n");
1016 goto start;
Andrew Duggandd3edeb2014-07-17 16:14:44 -07001017 }
1018
1019 data->output_report_size = (output_report->size >> 3)
1020 + 1 /* report id */;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -04001021
Andrew Duggan2f43de62014-12-19 14:45:41 -08001022 data->device_flags |= RMI_DEVICE;
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -04001023 alloc_size = data->output_report_size + data->input_report_size;
1024
1025 data->writeReport = devm_kzalloc(&hdev->dev, alloc_size, GFP_KERNEL);
1026 if (!data->writeReport) {
1027 ret = -ENOMEM;
1028 return ret;
1029 }
1030
1031 data->readReport = data->writeReport + data->output_report_size;
1032
1033 init_waitqueue_head(&data->wait);
1034
1035 mutex_init(&data->page_mutex);
1036
Andrew Duggan2f43de62014-12-19 14:45:41 -08001037start:
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -04001038 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1039 if (ret) {
1040 hid_err(hdev, "hw start failed\n");
1041 return ret;
1042 }
1043
Andrew Duggan2f43de62014-12-19 14:45:41 -08001044 if ((data->device_flags & RMI_DEVICE) &&
1045 !test_bit(RMI_STARTED, &data->flags))
Andrew Duggandaebdd72014-08-06 13:37:40 -07001046 /*
1047 * The device maybe in the bootloader if rmi_input_configured
1048 * failed to find F11 in the PDT. Print an error, but don't
1049 * return an error from rmi_probe so that hidraw will be
1050 * accessible from userspace. That way a userspace tool
1051 * can be used to reload working firmware on the touchpad.
1052 */
1053 hid_err(hdev, "Device failed to be properly configured\n");
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -04001054
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -04001055 return 0;
1056}
1057
1058static void rmi_remove(struct hid_device *hdev)
1059{
1060 struct rmi_data *hdata = hid_get_drvdata(hdev);
1061
1062 clear_bit(RMI_STARTED, &hdata->flags);
1063
1064 hid_hw_stop(hdev);
1065}
1066
1067static const struct hid_device_id rmi_id[] = {
Benjamin Tissoiresba391e52014-05-21 11:15:56 -04001068 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_RMI, HID_ANY_ID, HID_ANY_ID) },
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -04001069 { }
1070};
1071MODULE_DEVICE_TABLE(hid, rmi_id);
1072
1073static struct hid_driver rmi_driver = {
1074 .name = "hid-rmi",
1075 .id_table = rmi_id,
1076 .probe = rmi_probe,
1077 .remove = rmi_remove,
Andrew Duggan2f43de62014-12-19 14:45:41 -08001078 .event = rmi_event,
Benjamin Tissoires9fb6bf02014-04-07 13:39:33 -04001079 .raw_event = rmi_raw_event,
1080 .input_mapping = rmi_input_mapping,
1081 .input_configured = rmi_input_configured,
1082#ifdef CONFIG_PM
1083 .resume = rmi_post_resume,
1084 .reset_resume = rmi_post_reset,
1085#endif
1086};
1087
1088module_hid_driver(rmi_driver);
1089
1090MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
1091MODULE_DESCRIPTION("RMI HID driver");
1092MODULE_LICENSE("GPL");