GNU Linux-libre 4.9.283-gnu1
[releases.git] / drivers / hid / hid-rmi.c
1 /*
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        0
33 #define RMI_READ_DATA_PENDING           1
34 #define RMI_STARTED                     2
35
36 #define RMI_SLEEP_NORMAL                0x0
37 #define RMI_SLEEP_DEEP_SLEEP            0x1
38
39 /* device flags */
40 #define RMI_DEVICE                      BIT(0)
41 #define RMI_DEVICE_HAS_PHYS_BUTTONS     BIT(1)
42
43 /*
44  * retrieve the ctrl registers
45  * the ctrl register has a size of 20 but a fw bug split it into 16 + 4,
46  * and there is no way to know if the first 20 bytes are here or not.
47  * We use only the first 12 bytes, so get only them.
48  */
49 #define RMI_F11_CTRL_REG_COUNT          12
50
51 enum rmi_mode_type {
52         RMI_MODE_OFF                    = 0,
53         RMI_MODE_ATTN_REPORTS           = 1,
54         RMI_MODE_NO_PACKED_ATTN_REPORTS = 2,
55 };
56
57 struct rmi_function {
58         unsigned page;                  /* page of the function */
59         u16 query_base_addr;            /* base address for queries */
60         u16 command_base_addr;          /* base address for commands */
61         u16 control_base_addr;          /* base address for controls */
62         u16 data_base_addr;             /* base address for datas */
63         unsigned int interrupt_base;    /* cross-function interrupt number
64                                          * (uniq in the device)*/
65         unsigned int interrupt_count;   /* number of interrupts */
66         unsigned int report_size;       /* size of a report */
67         unsigned long irq_mask;         /* mask of the interrupts
68                                          * (to be applied against ATTN IRQ) */
69 };
70
71 /**
72  * struct rmi_data - stores information for hid communication
73  *
74  * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
75  * @page: Keeps track of the current virtual page
76  *
77  * @wait: Used for waiting for read data
78  *
79  * @writeReport: output buffer when writing RMI registers
80  * @readReport: input buffer when reading RMI registers
81  *
82  * @input_report_size: size of an input report (advertised by HID)
83  * @output_report_size: size of an output report (advertised by HID)
84  *
85  * @flags: flags for the current device (started, reading, etc...)
86  *
87  * @f11: placeholder of internal RMI function F11 description
88  * @f30: placeholder of internal RMI function F30 description
89  *
90  * @max_fingers: maximum finger count reported by the device
91  * @max_x: maximum x value reported by the device
92  * @max_y: maximum y value reported by the device
93  *
94  * @gpio_led_count: count of GPIOs + LEDs reported by F30
95  * @button_count: actual physical buttons count
96  * @button_mask: button mask used to decode GPIO ATTN reports
97  * @button_state_mask: pull state of the buttons
98  *
99  * @input: pointer to the kernel input device
100  *
101  * @reset_work: worker which will be called in case of a mouse report
102  * @hdev: pointer to the struct hid_device
103  */
104 struct rmi_data {
105         struct mutex page_mutex;
106         int page;
107
108         wait_queue_head_t wait;
109
110         u8 *writeReport;
111         u8 *readReport;
112
113         u32 input_report_size;
114         u32 output_report_size;
115
116         unsigned long flags;
117
118         struct rmi_function f01;
119         struct rmi_function f11;
120         struct rmi_function f30;
121
122         unsigned int max_fingers;
123         unsigned int max_x;
124         unsigned int max_y;
125         unsigned int x_size_mm;
126         unsigned int y_size_mm;
127         bool read_f11_ctrl_regs;
128         u8 f11_ctrl_regs[RMI_F11_CTRL_REG_COUNT];
129
130         unsigned int gpio_led_count;
131         unsigned int button_count;
132         unsigned long button_mask;
133         unsigned long button_state_mask;
134
135         struct input_dev *input;
136
137         struct work_struct reset_work;
138         struct hid_device *hdev;
139
140         unsigned long device_flags;
141         unsigned long firmware_id;
142
143         u8 f01_ctrl0;
144         u8 interrupt_enable_mask;
145         bool restore_interrupt_mask;
146 };
147
148 #define RMI_PAGE(addr) (((addr) >> 8) & 0xff)
149
150 static int rmi_write_report(struct hid_device *hdev, u8 *report, int len);
151
152 /**
153  * rmi_set_page - Set RMI page
154  * @hdev: The pointer to the hid_device struct
155  * @page: The new page address.
156  *
157  * RMI devices have 16-bit addressing, but some of the physical
158  * implementations (like SMBus) only have 8-bit addressing. So RMI implements
159  * a page address at 0xff of every page so we can reliable page addresses
160  * every 256 registers.
161  *
162  * The page_mutex lock must be held when this function is entered.
163  *
164  * Returns zero on success, non-zero on failure.
165  */
166 static int rmi_set_page(struct hid_device *hdev, u8 page)
167 {
168         struct rmi_data *data = hid_get_drvdata(hdev);
169         int retval;
170
171         data->writeReport[0] = RMI_WRITE_REPORT_ID;
172         data->writeReport[1] = 1;
173         data->writeReport[2] = 0xFF;
174         data->writeReport[4] = page;
175
176         retval = rmi_write_report(hdev, data->writeReport,
177                         data->output_report_size);
178         if (retval != data->output_report_size) {
179                 dev_err(&hdev->dev,
180                         "%s: set page failed: %d.", __func__, retval);
181                 return retval;
182         }
183
184         data->page = page;
185         return 0;
186 }
187
188 static int rmi_set_mode(struct hid_device *hdev, u8 mode)
189 {
190         int ret;
191         const u8 txbuf[2] = {RMI_SET_RMI_MODE_REPORT_ID, mode};
192         u8 *buf;
193
194         buf = kmemdup(txbuf, sizeof(txbuf), GFP_KERNEL);
195         if (!buf)
196                 return -ENOMEM;
197
198         ret = hid_hw_raw_request(hdev, RMI_SET_RMI_MODE_REPORT_ID, buf,
199                         sizeof(txbuf), HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
200         kfree(buf);
201         if (ret < 0) {
202                 dev_err(&hdev->dev, "unable to set rmi mode to %d (%d)\n", mode,
203                         ret);
204                 return ret;
205         }
206
207         return 0;
208 }
209
210 static int rmi_write_report(struct hid_device *hdev, u8 *report, int len)
211 {
212         int ret;
213
214         ret = hid_hw_output_report(hdev, (void *)report, len);
215         if (ret < 0) {
216                 dev_err(&hdev->dev, "failed to write hid report (%d)\n", ret);
217                 return ret;
218         }
219
220         return ret;
221 }
222
223 static int rmi_read_block(struct hid_device *hdev, u16 addr, void *buf,
224                 const int len)
225 {
226         struct rmi_data *data = hid_get_drvdata(hdev);
227         int ret;
228         int bytes_read;
229         int bytes_needed;
230         int retries;
231         int read_input_count;
232
233         mutex_lock(&data->page_mutex);
234
235         if (RMI_PAGE(addr) != data->page) {
236                 ret = rmi_set_page(hdev, RMI_PAGE(addr));
237                 if (ret < 0)
238                         goto exit;
239         }
240
241         for (retries = 5; retries > 0; retries--) {
242                 data->writeReport[0] = RMI_READ_ADDR_REPORT_ID;
243                 data->writeReport[1] = 0; /* old 1 byte read count */
244                 data->writeReport[2] = addr & 0xFF;
245                 data->writeReport[3] = (addr >> 8) & 0xFF;
246                 data->writeReport[4] = len  & 0xFF;
247                 data->writeReport[5] = (len >> 8) & 0xFF;
248
249                 set_bit(RMI_READ_REQUEST_PENDING, &data->flags);
250
251                 ret = rmi_write_report(hdev, data->writeReport,
252                                                 data->output_report_size);
253                 if (ret != data->output_report_size) {
254                         clear_bit(RMI_READ_REQUEST_PENDING, &data->flags);
255                         dev_err(&hdev->dev,
256                                 "failed to write request output report (%d)\n",
257                                 ret);
258                         goto exit;
259                 }
260
261                 bytes_read = 0;
262                 bytes_needed = len;
263                 while (bytes_read < len) {
264                         if (!wait_event_timeout(data->wait,
265                                 test_bit(RMI_READ_DATA_PENDING, &data->flags),
266                                         msecs_to_jiffies(1000))) {
267                                 hid_warn(hdev, "%s: timeout elapsed\n",
268                                          __func__);
269                                 ret = -EAGAIN;
270                                 break;
271                         }
272
273                         read_input_count = data->readReport[1];
274                         memcpy(buf + bytes_read, &data->readReport[2],
275                                 read_input_count < bytes_needed ?
276                                         read_input_count : bytes_needed);
277
278                         bytes_read += read_input_count;
279                         bytes_needed -= read_input_count;
280                         clear_bit(RMI_READ_DATA_PENDING, &data->flags);
281                 }
282
283                 if (ret >= 0) {
284                         ret = 0;
285                         break;
286                 }
287         }
288
289 exit:
290         clear_bit(RMI_READ_REQUEST_PENDING, &data->flags);
291         mutex_unlock(&data->page_mutex);
292         return ret;
293 }
294
295 static inline int rmi_read(struct hid_device *hdev, u16 addr, void *buf)
296 {
297         return rmi_read_block(hdev, addr, buf, 1);
298 }
299
300 static int rmi_write_block(struct hid_device *hdev, u16 addr, void *buf,
301                 const int len)
302 {
303         struct rmi_data *data = hid_get_drvdata(hdev);
304         int ret;
305
306         mutex_lock(&data->page_mutex);
307
308         if (RMI_PAGE(addr) != data->page) {
309                 ret = rmi_set_page(hdev, RMI_PAGE(addr));
310                 if (ret < 0)
311                         goto exit;
312         }
313
314         data->writeReport[0] = RMI_WRITE_REPORT_ID;
315         data->writeReport[1] = len;
316         data->writeReport[2] = addr & 0xFF;
317         data->writeReport[3] = (addr >> 8) & 0xFF;
318         memcpy(&data->writeReport[4], buf, len);
319
320         ret = rmi_write_report(hdev, data->writeReport,
321                                         data->output_report_size);
322         if (ret < 0) {
323                 dev_err(&hdev->dev,
324                         "failed to write request output report (%d)\n",
325                         ret);
326                 goto exit;
327         }
328         ret = 0;
329
330 exit:
331         mutex_unlock(&data->page_mutex);
332         return ret;
333 }
334
335 static inline int rmi_write(struct hid_device *hdev, u16 addr, void *buf)
336 {
337         return rmi_write_block(hdev, addr, buf, 1);
338 }
339
340 static void rmi_f11_process_touch(struct rmi_data *hdata, int slot,
341                 u8 finger_state, u8 *touch_data)
342 {
343         int x, y, wx, wy;
344         int wide, major, minor;
345         int z;
346
347         input_mt_slot(hdata->input, slot);
348         input_mt_report_slot_state(hdata->input, MT_TOOL_FINGER,
349                         finger_state == 0x01);
350         if (finger_state == 0x01) {
351                 x = (touch_data[0] << 4) | (touch_data[2] & 0x0F);
352                 y = (touch_data[1] << 4) | (touch_data[2] >> 4);
353                 wx = touch_data[3] & 0x0F;
354                 wy = touch_data[3] >> 4;
355                 wide = (wx > wy);
356                 major = max(wx, wy);
357                 minor = min(wx, wy);
358                 z = touch_data[4];
359
360                 /* y is inverted */
361                 y = hdata->max_y - y;
362
363                 input_event(hdata->input, EV_ABS, ABS_MT_POSITION_X, x);
364                 input_event(hdata->input, EV_ABS, ABS_MT_POSITION_Y, y);
365                 input_event(hdata->input, EV_ABS, ABS_MT_ORIENTATION, wide);
366                 input_event(hdata->input, EV_ABS, ABS_MT_PRESSURE, z);
367                 input_event(hdata->input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
368                 input_event(hdata->input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
369         }
370 }
371
372 static int rmi_reset_attn_mode(struct hid_device *hdev)
373 {
374         struct rmi_data *data = hid_get_drvdata(hdev);
375         int ret;
376
377         ret = rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
378         if (ret)
379                 return ret;
380
381         if (data->restore_interrupt_mask) {
382                 ret = rmi_write(hdev, data->f01.control_base_addr + 1,
383                                 &data->interrupt_enable_mask);
384                 if (ret) {
385                         hid_err(hdev, "can not write F01 control register\n");
386                         return ret;
387                 }
388         }
389
390         return 0;
391 }
392
393 static void rmi_reset_work(struct work_struct *work)
394 {
395         struct rmi_data *hdata = container_of(work, struct rmi_data,
396                                                 reset_work);
397
398         /* switch the device to RMI if we receive a generic mouse report */
399         rmi_reset_attn_mode(hdata->hdev);
400 }
401
402 static inline int rmi_schedule_reset(struct hid_device *hdev)
403 {
404         struct rmi_data *hdata = hid_get_drvdata(hdev);
405         return schedule_work(&hdata->reset_work);
406 }
407
408 static int rmi_f11_input_event(struct hid_device *hdev, u8 irq, u8 *data,
409                 int size)
410 {
411         struct rmi_data *hdata = hid_get_drvdata(hdev);
412         int offset;
413         int i;
414
415         if (!(irq & hdata->f11.irq_mask) || size <= 0)
416                 return 0;
417
418         offset = (hdata->max_fingers >> 2) + 1;
419         for (i = 0; i < hdata->max_fingers; i++) {
420                 int fs_byte_position = i >> 2;
421                 int fs_bit_position = (i & 0x3) << 1;
422                 int finger_state = (data[fs_byte_position] >> fs_bit_position) &
423                                         0x03;
424                 int position = offset + 5 * i;
425
426                 if (position + 5 > size) {
427                         /* partial report, go on with what we received */
428                         printk_once(KERN_WARNING
429                                 "%s %s: Detected incomplete finger report. Finger reports may occasionally get dropped on this platform.\n",
430                                  dev_driver_string(&hdev->dev),
431                                  dev_name(&hdev->dev));
432                         hid_dbg(hdev, "Incomplete finger report\n");
433                         break;
434                 }
435
436                 rmi_f11_process_touch(hdata, i, finger_state, &data[position]);
437         }
438         input_mt_sync_frame(hdata->input);
439         input_sync(hdata->input);
440         return hdata->f11.report_size;
441 }
442
443 static int rmi_f30_input_event(struct hid_device *hdev, u8 irq, u8 *data,
444                 int size)
445 {
446         struct rmi_data *hdata = hid_get_drvdata(hdev);
447         int i;
448         int button = 0;
449         bool value;
450
451         if (!(irq & hdata->f30.irq_mask))
452                 return 0;
453
454         if (size < (int)hdata->f30.report_size) {
455                 hid_warn(hdev, "Click Button pressed, but the click data is missing\n");
456                 return 0;
457         }
458
459         for (i = 0; i < hdata->gpio_led_count; i++) {
460                 if (test_bit(i, &hdata->button_mask)) {
461                         value = (data[i / 8] >> (i & 0x07)) & BIT(0);
462                         if (test_bit(i, &hdata->button_state_mask))
463                                 value = !value;
464                         input_event(hdata->input, EV_KEY, BTN_LEFT + button++,
465                                         value);
466                 }
467         }
468         return hdata->f30.report_size;
469 }
470
471 static int rmi_input_event(struct hid_device *hdev, u8 *data, int size)
472 {
473         struct rmi_data *hdata = hid_get_drvdata(hdev);
474         unsigned long irq_mask = 0;
475         unsigned index = 2;
476
477         if (!(test_bit(RMI_STARTED, &hdata->flags)))
478                 return 0;
479
480         irq_mask |= hdata->f11.irq_mask;
481         irq_mask |= hdata->f30.irq_mask;
482
483         if (data[1] & ~irq_mask)
484                 hid_dbg(hdev, "unknown intr source:%02lx %s:%d\n",
485                         data[1] & ~irq_mask, __FILE__, __LINE__);
486
487         if (hdata->f11.interrupt_base < hdata->f30.interrupt_base) {
488                 index += rmi_f11_input_event(hdev, data[1], &data[index],
489                                 size - index);
490                 index += rmi_f30_input_event(hdev, data[1], &data[index],
491                                 size - index);
492         } else {
493                 index += rmi_f30_input_event(hdev, data[1], &data[index],
494                                 size - index);
495                 index += rmi_f11_input_event(hdev, data[1], &data[index],
496                                 size - index);
497         }
498
499         return 1;
500 }
501
502 static int rmi_read_data_event(struct hid_device *hdev, u8 *data, int size)
503 {
504         struct rmi_data *hdata = hid_get_drvdata(hdev);
505
506         if (!test_bit(RMI_READ_REQUEST_PENDING, &hdata->flags)) {
507                 hid_dbg(hdev, "no read request pending\n");
508                 return 0;
509         }
510
511         memcpy(hdata->readReport, data, size < hdata->input_report_size ?
512                         size : hdata->input_report_size);
513         set_bit(RMI_READ_DATA_PENDING, &hdata->flags);
514         wake_up(&hdata->wait);
515
516         return 1;
517 }
518
519 static int rmi_check_sanity(struct hid_device *hdev, u8 *data, int size)
520 {
521         int valid_size = size;
522         /*
523          * On the Dell XPS 13 9333, the bus sometimes get confused and fills
524          * the report with a sentinel value "ff". Synaptics told us that such
525          * behavior does not comes from the touchpad itself, so we filter out
526          * such reports here.
527          */
528
529         while ((data[valid_size - 1] == 0xff) && valid_size > 0)
530                 valid_size--;
531
532         return valid_size;
533 }
534
535 static int rmi_raw_event(struct hid_device *hdev,
536                 struct hid_report *report, u8 *data, int size)
537 {
538         size = rmi_check_sanity(hdev, data, size);
539         if (size < 2)
540                 return 0;
541
542         switch (data[0]) {
543         case RMI_READ_DATA_REPORT_ID:
544                 return rmi_read_data_event(hdev, data, size);
545         case RMI_ATTN_REPORT_ID:
546                 return rmi_input_event(hdev, data, size);
547         default:
548                 return 1;
549         }
550
551         return 0;
552 }
553
554 static int rmi_event(struct hid_device *hdev, struct hid_field *field,
555                         struct hid_usage *usage, __s32 value)
556 {
557         struct rmi_data *data = hid_get_drvdata(hdev);
558
559         if ((data->device_flags & RMI_DEVICE) &&
560             (field->application == HID_GD_POINTER ||
561             field->application == HID_GD_MOUSE)) {
562                 if (data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS) {
563                         if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
564                                 return 0;
565
566                         if ((usage->hid == HID_GD_X || usage->hid == HID_GD_Y)
567                             && !value)
568                                 return 1;
569                 }
570
571                 rmi_schedule_reset(hdev);
572                 return 1;
573         }
574
575         return 0;
576 }
577
578 #ifdef CONFIG_PM
579 static int rmi_set_sleep_mode(struct hid_device *hdev, int sleep_mode)
580 {
581         struct rmi_data *data = hid_get_drvdata(hdev);
582         int ret;
583         u8 f01_ctrl0;
584
585         f01_ctrl0 = (data->f01_ctrl0 & ~0x3) | sleep_mode;
586
587         ret = rmi_write(hdev, data->f01.control_base_addr,
588                         &f01_ctrl0);
589         if (ret) {
590                 hid_err(hdev, "can not write sleep mode\n");
591                 return ret;
592         }
593
594         return 0;
595 }
596
597 static int rmi_suspend(struct hid_device *hdev, pm_message_t message)
598 {
599         struct rmi_data *data = hid_get_drvdata(hdev);
600         int ret;
601         u8 buf[RMI_F11_CTRL_REG_COUNT];
602
603         if (!(data->device_flags & RMI_DEVICE))
604                 return 0;
605
606         ret = rmi_read_block(hdev, data->f11.control_base_addr, buf,
607                                 RMI_F11_CTRL_REG_COUNT);
608         if (ret)
609                 hid_warn(hdev, "can not read F11 control registers\n");
610         else
611                 memcpy(data->f11_ctrl_regs, buf, RMI_F11_CTRL_REG_COUNT);
612
613
614         if (!device_may_wakeup(hdev->dev.parent))
615                 return rmi_set_sleep_mode(hdev, RMI_SLEEP_DEEP_SLEEP);
616
617         return 0;
618 }
619
620 static int rmi_post_reset(struct hid_device *hdev)
621 {
622         struct rmi_data *data = hid_get_drvdata(hdev);
623         int ret;
624
625         if (!(data->device_flags & RMI_DEVICE))
626                 return 0;
627
628         ret = rmi_reset_attn_mode(hdev);
629         if (ret) {
630                 hid_err(hdev, "can not set rmi mode\n");
631                 return ret;
632         }
633
634         if (data->read_f11_ctrl_regs) {
635                 ret = rmi_write_block(hdev, data->f11.control_base_addr,
636                                 data->f11_ctrl_regs, RMI_F11_CTRL_REG_COUNT);
637                 if (ret)
638                         hid_warn(hdev,
639                                 "can not write F11 control registers after reset\n");
640         }
641
642         if (!device_may_wakeup(hdev->dev.parent)) {
643                 ret = rmi_set_sleep_mode(hdev, RMI_SLEEP_NORMAL);
644                 if (ret) {
645                         hid_err(hdev, "can not write sleep mode\n");
646                         return ret;
647                 }
648         }
649
650         return ret;
651 }
652
653 static int rmi_post_resume(struct hid_device *hdev)
654 {
655         struct rmi_data *data = hid_get_drvdata(hdev);
656
657         if (!(data->device_flags & RMI_DEVICE))
658                 return 0;
659
660         return rmi_reset_attn_mode(hdev);
661 }
662 #endif /* CONFIG_PM */
663
664 #define RMI4_MAX_PAGE 0xff
665 #define RMI4_PAGE_SIZE 0x0100
666
667 #define PDT_START_SCAN_LOCATION 0x00e9
668 #define PDT_END_SCAN_LOCATION   0x0005
669 #define RMI4_END_OF_PDT(id) ((id) == 0x00 || (id) == 0xff)
670
671 struct pdt_entry {
672         u8 query_base_addr:8;
673         u8 command_base_addr:8;
674         u8 control_base_addr:8;
675         u8 data_base_addr:8;
676         u8 interrupt_source_count:3;
677         u8 bits3and4:2;
678         u8 function_version:2;
679         u8 bit7:1;
680         u8 function_number:8;
681 } __attribute__((__packed__));
682
683 static inline unsigned long rmi_gen_mask(unsigned irq_base, unsigned irq_count)
684 {
685         return GENMASK(irq_count + irq_base - 1, irq_base);
686 }
687
688 static void rmi_register_function(struct rmi_data *data,
689         struct pdt_entry *pdt_entry, int page, unsigned interrupt_count)
690 {
691         struct rmi_function *f = NULL;
692         u16 page_base = page << 8;
693
694         switch (pdt_entry->function_number) {
695         case 0x01:
696                 f = &data->f01;
697                 break;
698         case 0x11:
699                 f = &data->f11;
700                 break;
701         case 0x30:
702                 f = &data->f30;
703                 break;
704         }
705
706         if (f) {
707                 f->page = page;
708                 f->query_base_addr = page_base | pdt_entry->query_base_addr;
709                 f->command_base_addr = page_base | pdt_entry->command_base_addr;
710                 f->control_base_addr = page_base | pdt_entry->control_base_addr;
711                 f->data_base_addr = page_base | pdt_entry->data_base_addr;
712                 f->interrupt_base = interrupt_count;
713                 f->interrupt_count = pdt_entry->interrupt_source_count;
714                 f->irq_mask = rmi_gen_mask(f->interrupt_base,
715                                                 f->interrupt_count);
716                 data->interrupt_enable_mask |= f->irq_mask;
717         }
718 }
719
720 static int rmi_scan_pdt(struct hid_device *hdev)
721 {
722         struct rmi_data *data = hid_get_drvdata(hdev);
723         struct pdt_entry entry;
724         int page;
725         bool page_has_function;
726         int i;
727         int retval;
728         int interrupt = 0;
729         u16 page_start, pdt_start , pdt_end;
730
731         hid_info(hdev, "Scanning PDT...\n");
732
733         for (page = 0; (page <= RMI4_MAX_PAGE); page++) {
734                 page_start = RMI4_PAGE_SIZE * page;
735                 pdt_start = page_start + PDT_START_SCAN_LOCATION;
736                 pdt_end = page_start + PDT_END_SCAN_LOCATION;
737
738                 page_has_function = false;
739                 for (i = pdt_start; i >= pdt_end; i -= sizeof(entry)) {
740                         retval = rmi_read_block(hdev, i, &entry, sizeof(entry));
741                         if (retval) {
742                                 hid_err(hdev,
743                                         "Read of PDT entry at %#06x failed.\n",
744                                         i);
745                                 goto error_exit;
746                         }
747
748                         if (RMI4_END_OF_PDT(entry.function_number))
749                                 break;
750
751                         page_has_function = true;
752
753                         hid_info(hdev, "Found F%02X on page %#04x\n",
754                                         entry.function_number, page);
755
756                         rmi_register_function(data, &entry, page, interrupt);
757                         interrupt += entry.interrupt_source_count;
758                 }
759
760                 if (!page_has_function)
761                         break;
762         }
763
764         hid_info(hdev, "%s: Done with PDT scan.\n", __func__);
765         retval = 0;
766
767 error_exit:
768         return retval;
769 }
770
771 #define RMI_DEVICE_F01_BASIC_QUERY_LEN  11
772
773 static int rmi_populate_f01(struct hid_device *hdev)
774 {
775         struct rmi_data *data = hid_get_drvdata(hdev);
776         u8 basic_queries[RMI_DEVICE_F01_BASIC_QUERY_LEN];
777         u8 info[3];
778         int ret;
779         bool has_query42;
780         bool has_lts;
781         bool has_sensor_id;
782         bool has_ds4_queries = false;
783         bool has_build_id_query = false;
784         bool has_package_id_query = false;
785         u16 query_offset = data->f01.query_base_addr;
786         u16 prod_info_addr;
787         u8 ds4_query_len;
788
789         ret = rmi_read_block(hdev, query_offset, basic_queries,
790                                 RMI_DEVICE_F01_BASIC_QUERY_LEN);
791         if (ret) {
792                 hid_err(hdev, "Can not read basic queries from Function 0x1.\n");
793                 return ret;
794         }
795
796         has_lts = !!(basic_queries[0] & BIT(2));
797         has_sensor_id = !!(basic_queries[1] & BIT(3));
798         has_query42 = !!(basic_queries[1] & BIT(7));
799
800         query_offset += 11;
801         prod_info_addr = query_offset + 6;
802         query_offset += 10;
803
804         if (has_lts)
805                 query_offset += 20;
806
807         if (has_sensor_id)
808                 query_offset++;
809
810         if (has_query42) {
811                 ret = rmi_read(hdev, query_offset, info);
812                 if (ret) {
813                         hid_err(hdev, "Can not read query42.\n");
814                         return ret;
815                 }
816                 has_ds4_queries = !!(info[0] & BIT(0));
817                 query_offset++;
818         }
819
820         if (has_ds4_queries) {
821                 ret = rmi_read(hdev, query_offset, &ds4_query_len);
822                 if (ret) {
823                         hid_err(hdev, "Can not read DS4 Query length.\n");
824                         return ret;
825                 }
826                 query_offset++;
827
828                 if (ds4_query_len > 0) {
829                         ret = rmi_read(hdev, query_offset, info);
830                         if (ret) {
831                                 hid_err(hdev, "Can not read DS4 query.\n");
832                                 return ret;
833                         }
834
835                         has_package_id_query = !!(info[0] & BIT(0));
836                         has_build_id_query = !!(info[0] & BIT(1));
837                 }
838         }
839
840         if (has_package_id_query)
841                 prod_info_addr++;
842
843         if (has_build_id_query) {
844                 ret = rmi_read_block(hdev, prod_info_addr, info, 3);
845                 if (ret) {
846                         hid_err(hdev, "Can not read product info.\n");
847                         return ret;
848                 }
849
850                 data->firmware_id = info[1] << 8 | info[0];
851                 data->firmware_id += info[2] * 65536;
852         }
853
854         ret = rmi_read_block(hdev, data->f01.control_base_addr, info,
855                                 2);
856
857         if (ret) {
858                 hid_err(hdev, "can not read f01 ctrl registers\n");
859                 return ret;
860         }
861
862         data->f01_ctrl0 = info[0];
863
864         if (!info[1]) {
865                 /*
866                  * Do to a firmware bug in some touchpads the F01 interrupt
867                  * enable control register will be cleared on reset.
868                  * This will stop the touchpad from reporting data, so
869                  * if F01 CTRL1 is 0 then we need to explicitly enable
870                  * interrupts for the functions we want data for.
871                  */
872                 data->restore_interrupt_mask = true;
873
874                 ret = rmi_write(hdev, data->f01.control_base_addr + 1,
875                                 &data->interrupt_enable_mask);
876                 if (ret) {
877                         hid_err(hdev, "can not write to control reg 1: %d.\n",
878                                 ret);
879                         return ret;
880                 }
881         }
882
883         return 0;
884 }
885
886 static int rmi_populate_f11(struct hid_device *hdev)
887 {
888         struct rmi_data *data = hid_get_drvdata(hdev);
889         u8 buf[20];
890         int ret;
891         bool has_query9;
892         bool has_query10 = false;
893         bool has_query11;
894         bool has_query12;
895         bool has_query27;
896         bool has_query28;
897         bool has_query36 = false;
898         bool has_physical_props;
899         bool has_gestures;
900         bool has_rel;
901         bool has_data40 = false;
902         bool has_dribble = false;
903         bool has_palm_detect = false;
904         unsigned x_size, y_size;
905         u16 query_offset;
906
907         if (!data->f11.query_base_addr) {
908                 hid_err(hdev, "No 2D sensor found, giving up.\n");
909                 return -ENODEV;
910         }
911
912         /* query 0 contains some useful information */
913         ret = rmi_read(hdev, data->f11.query_base_addr, buf);
914         if (ret) {
915                 hid_err(hdev, "can not get query 0: %d.\n", ret);
916                 return ret;
917         }
918         has_query9 = !!(buf[0] & BIT(3));
919         has_query11 = !!(buf[0] & BIT(4));
920         has_query12 = !!(buf[0] & BIT(5));
921         has_query27 = !!(buf[0] & BIT(6));
922         has_query28 = !!(buf[0] & BIT(7));
923
924         /* query 1 to get the max number of fingers */
925         ret = rmi_read(hdev, data->f11.query_base_addr + 1, buf);
926         if (ret) {
927                 hid_err(hdev, "can not get NumberOfFingers: %d.\n", ret);
928                 return ret;
929         }
930         data->max_fingers = (buf[0] & 0x07) + 1;
931         if (data->max_fingers > 5)
932                 data->max_fingers = 10;
933
934         data->f11.report_size = data->max_fingers * 5 +
935                                 DIV_ROUND_UP(data->max_fingers, 4);
936
937         if (!(buf[0] & BIT(4))) {
938                 hid_err(hdev, "No absolute events, giving up.\n");
939                 return -ENODEV;
940         }
941
942         has_rel = !!(buf[0] & BIT(3));
943         has_gestures = !!(buf[0] & BIT(5));
944
945         ret = rmi_read(hdev, data->f11.query_base_addr + 5, buf);
946         if (ret) {
947                 hid_err(hdev, "can not get absolute data sources: %d.\n", ret);
948                 return ret;
949         }
950
951         has_dribble = !!(buf[0] & BIT(4));
952
953         /*
954          * At least 4 queries are guaranteed to be present in F11
955          * +1 for query 5 which is present since absolute events are
956          * reported and +1 for query 12.
957          */
958         query_offset = 6;
959
960         if (has_rel)
961                 ++query_offset; /* query 6 is present */
962
963         if (has_gestures) {
964                 /* query 8 to find out if query 10 exists */
965                 ret = rmi_read(hdev,
966                         data->f11.query_base_addr + query_offset + 1, buf);
967                 if (ret) {
968                         hid_err(hdev, "can not read gesture information: %d.\n",
969                                 ret);
970                         return ret;
971                 }
972                 has_palm_detect = !!(buf[0] & BIT(0));
973                 has_query10 = !!(buf[0] & BIT(2));
974
975                 query_offset += 2; /* query 7 and 8 are present */
976         }
977
978         if (has_query9)
979                 ++query_offset;
980
981         if (has_query10)
982                 ++query_offset;
983
984         if (has_query11)
985                 ++query_offset;
986
987         /* query 12 to know if the physical properties are reported */
988         if (has_query12) {
989                 ret = rmi_read(hdev, data->f11.query_base_addr
990                                 + query_offset, buf);
991                 if (ret) {
992                         hid_err(hdev, "can not get query 12: %d.\n", ret);
993                         return ret;
994                 }
995                 has_physical_props = !!(buf[0] & BIT(5));
996
997                 if (has_physical_props) {
998                         query_offset += 1;
999                         ret = rmi_read_block(hdev,
1000                                         data->f11.query_base_addr
1001                                                 + query_offset, buf, 4);
1002                         if (ret) {
1003                                 hid_err(hdev, "can not read query 15-18: %d.\n",
1004                                         ret);
1005                                 return ret;
1006                         }
1007
1008                         x_size = buf[0] | (buf[1] << 8);
1009                         y_size = buf[2] | (buf[3] << 8);
1010
1011                         data->x_size_mm = DIV_ROUND_CLOSEST(x_size, 10);
1012                         data->y_size_mm = DIV_ROUND_CLOSEST(y_size, 10);
1013
1014                         hid_info(hdev, "%s: size in mm: %d x %d\n",
1015                                  __func__, data->x_size_mm, data->y_size_mm);
1016
1017                         /*
1018                          * query 15 - 18 contain the size of the sensor
1019                          * and query 19 - 26 contain bezel dimensions
1020                          */
1021                         query_offset += 12;
1022                 }
1023         }
1024
1025         if (has_query27)
1026                 ++query_offset;
1027
1028         if (has_query28) {
1029                 ret = rmi_read(hdev, data->f11.query_base_addr
1030                                 + query_offset, buf);
1031                 if (ret) {
1032                         hid_err(hdev, "can not get query 28: %d.\n", ret);
1033                         return ret;
1034                 }
1035
1036                 has_query36 = !!(buf[0] & BIT(6));
1037         }
1038
1039         if (has_query36) {
1040                 query_offset += 2;
1041                 ret = rmi_read(hdev, data->f11.query_base_addr
1042                                 + query_offset, buf);
1043                 if (ret) {
1044                         hid_err(hdev, "can not get query 36: %d.\n", ret);
1045                         return ret;
1046                 }
1047
1048                 has_data40 = !!(buf[0] & BIT(5));
1049         }
1050
1051
1052         if (has_data40)
1053                 data->f11.report_size += data->max_fingers * 2;
1054
1055         ret = rmi_read_block(hdev, data->f11.control_base_addr,
1056                         data->f11_ctrl_regs, RMI_F11_CTRL_REG_COUNT);
1057         if (ret) {
1058                 hid_err(hdev, "can not read ctrl block of size 11: %d.\n", ret);
1059                 return ret;
1060         }
1061
1062         /* data->f11_ctrl_regs now contains valid register data */
1063         data->read_f11_ctrl_regs = true;
1064
1065         data->max_x = data->f11_ctrl_regs[6] | (data->f11_ctrl_regs[7] << 8);
1066         data->max_y = data->f11_ctrl_regs[8] | (data->f11_ctrl_regs[9] << 8);
1067
1068         if (has_dribble) {
1069                 data->f11_ctrl_regs[0] = data->f11_ctrl_regs[0] & ~BIT(6);
1070                 ret = rmi_write(hdev, data->f11.control_base_addr,
1071                                 data->f11_ctrl_regs);
1072                 if (ret) {
1073                         hid_err(hdev, "can not write to control reg 0: %d.\n",
1074                                 ret);
1075                         return ret;
1076                 }
1077         }
1078
1079         if (has_palm_detect) {
1080                 data->f11_ctrl_regs[11] = data->f11_ctrl_regs[11] & ~BIT(0);
1081                 ret = rmi_write(hdev, data->f11.control_base_addr + 11,
1082                                 &data->f11_ctrl_regs[11]);
1083                 if (ret) {
1084                         hid_err(hdev, "can not write to control reg 11: %d.\n",
1085                                 ret);
1086                         return ret;
1087                 }
1088         }
1089
1090         return 0;
1091 }
1092
1093 static int rmi_populate_f30(struct hid_device *hdev)
1094 {
1095         struct rmi_data *data = hid_get_drvdata(hdev);
1096         u8 buf[20];
1097         int ret;
1098         bool has_gpio, has_led;
1099         unsigned bytes_per_ctrl;
1100         u8 ctrl2_addr;
1101         int ctrl2_3_length;
1102         int i;
1103
1104         /* function F30 is for physical buttons */
1105         if (!data->f30.query_base_addr) {
1106                 hid_err(hdev, "No GPIO/LEDs found, giving up.\n");
1107                 return -ENODEV;
1108         }
1109
1110         ret = rmi_read_block(hdev, data->f30.query_base_addr, buf, 2);
1111         if (ret) {
1112                 hid_err(hdev, "can not get F30 query registers: %d.\n", ret);
1113                 return ret;
1114         }
1115
1116         has_gpio = !!(buf[0] & BIT(3));
1117         has_led = !!(buf[0] & BIT(2));
1118         data->gpio_led_count = buf[1] & 0x1f;
1119
1120         /* retrieve ctrl 2 & 3 registers */
1121         bytes_per_ctrl = (data->gpio_led_count + 7) / 8;
1122         /* Ctrl0 is present only if both has_gpio and has_led are set*/
1123         ctrl2_addr = (has_gpio && has_led) ? bytes_per_ctrl : 0;
1124         /* Ctrl1 is always be present */
1125         ctrl2_addr += bytes_per_ctrl;
1126         ctrl2_3_length = 2 * bytes_per_ctrl;
1127
1128         data->f30.report_size = bytes_per_ctrl;
1129
1130         ret = rmi_read_block(hdev, data->f30.control_base_addr + ctrl2_addr,
1131                                 buf, ctrl2_3_length);
1132         if (ret) {
1133                 hid_err(hdev, "can not read ctrl 2&3 block of size %d: %d.\n",
1134                         ctrl2_3_length, ret);
1135                 return ret;
1136         }
1137
1138         for (i = 0; i < data->gpio_led_count; i++) {
1139                 int byte_position = i >> 3;
1140                 int bit_position = i & 0x07;
1141                 u8 dir_byte = buf[byte_position];
1142                 u8 data_byte = buf[byte_position + bytes_per_ctrl];
1143                 bool dir = (dir_byte >> bit_position) & BIT(0);
1144                 bool dat = (data_byte >> bit_position) & BIT(0);
1145
1146                 if (dir == 0) {
1147                         /* input mode */
1148                         if (dat) {
1149                                 /* actual buttons have pull up resistor */
1150                                 data->button_count++;
1151                                 set_bit(i, &data->button_mask);
1152                                 set_bit(i, &data->button_state_mask);
1153                         }
1154                 }
1155
1156         }
1157
1158         return 0;
1159 }
1160
1161 static int rmi_populate(struct hid_device *hdev)
1162 {
1163         struct rmi_data *data = hid_get_drvdata(hdev);
1164         int ret;
1165
1166         ret = rmi_scan_pdt(hdev);
1167         if (ret) {
1168                 hid_err(hdev, "PDT scan failed with code %d.\n", ret);
1169                 return ret;
1170         }
1171
1172         ret = rmi_populate_f01(hdev);
1173         if (ret) {
1174                 hid_err(hdev, "Error while initializing F01 (%d).\n", ret);
1175                 return ret;
1176         }
1177
1178         ret = rmi_populate_f11(hdev);
1179         if (ret) {
1180                 hid_err(hdev, "Error while initializing F11 (%d).\n", ret);
1181                 return ret;
1182         }
1183
1184         if (!(data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS)) {
1185                 ret = rmi_populate_f30(hdev);
1186                 if (ret)
1187                         hid_warn(hdev, "Error while initializing F30 (%d).\n", ret);
1188         }
1189
1190         return 0;
1191 }
1192
1193 static int rmi_input_configured(struct hid_device *hdev, struct hid_input *hi)
1194 {
1195         struct rmi_data *data = hid_get_drvdata(hdev);
1196         struct input_dev *input = hi->input;
1197         int ret;
1198         int res_x, res_y, i;
1199
1200         data->input = input;
1201
1202         hid_dbg(hdev, "Opening low level driver\n");
1203         ret = hid_hw_open(hdev);
1204         if (ret)
1205                 return ret;
1206
1207         if (!(data->device_flags & RMI_DEVICE))
1208                 return 0;
1209
1210         /* Allow incoming hid reports */
1211         hid_device_io_start(hdev);
1212
1213         ret = rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS);
1214         if (ret < 0) {
1215                 dev_err(&hdev->dev, "failed to set rmi mode\n");
1216                 goto exit;
1217         }
1218
1219         ret = rmi_set_page(hdev, 0);
1220         if (ret < 0) {
1221                 dev_err(&hdev->dev, "failed to set page select to 0.\n");
1222                 goto exit;
1223         }
1224
1225         ret = rmi_populate(hdev);
1226         if (ret)
1227                 goto exit;
1228
1229         hid_info(hdev, "firmware id: %ld\n", data->firmware_id);
1230
1231         __set_bit(EV_ABS, input->evbit);
1232         input_set_abs_params(input, ABS_MT_POSITION_X, 1, data->max_x, 0, 0);
1233         input_set_abs_params(input, ABS_MT_POSITION_Y, 1, data->max_y, 0, 0);
1234
1235         if (data->x_size_mm && data->y_size_mm) {
1236                 res_x = (data->max_x - 1) / data->x_size_mm;
1237                 res_y = (data->max_y - 1) / data->y_size_mm;
1238
1239                 input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
1240                 input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
1241         }
1242
1243         input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
1244         input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xff, 0, 0);
1245         input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 0x0f, 0, 0);
1246         input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 0x0f, 0, 0);
1247
1248         ret = input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
1249         if (ret < 0)
1250                 goto exit;
1251
1252         if (data->button_count) {
1253                 __set_bit(EV_KEY, input->evbit);
1254                 for (i = 0; i < data->button_count; i++)
1255                         __set_bit(BTN_LEFT + i, input->keybit);
1256
1257                 if (data->button_count == 1)
1258                         __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1259         }
1260
1261         set_bit(RMI_STARTED, &data->flags);
1262
1263 exit:
1264         hid_device_io_stop(hdev);
1265         hid_hw_close(hdev);
1266         return ret;
1267 }
1268
1269 static int rmi_input_mapping(struct hid_device *hdev,
1270                 struct hid_input *hi, struct hid_field *field,
1271                 struct hid_usage *usage, unsigned long **bit, int *max)
1272 {
1273         struct rmi_data *data = hid_get_drvdata(hdev);
1274
1275         /*
1276          * we want to make HID ignore the advertised HID collection
1277          * for RMI deivces
1278          */
1279         if (data->device_flags & RMI_DEVICE) {
1280                 if ((data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS) &&
1281                     ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON))
1282                         return 0;
1283
1284                 return -1;
1285         }
1286
1287         return 0;
1288 }
1289
1290 static int rmi_check_valid_report_id(struct hid_device *hdev, unsigned type,
1291                 unsigned id, struct hid_report **report)
1292 {
1293         int i;
1294
1295         *report = hdev->report_enum[type].report_id_hash[id];
1296         if (*report) {
1297                 for (i = 0; i < (*report)->maxfield; i++) {
1298                         unsigned app = (*report)->field[i]->application;
1299                         if ((app & HID_USAGE_PAGE) >= HID_UP_MSVENDOR)
1300                                 return 1;
1301                 }
1302         }
1303
1304         return 0;
1305 }
1306
1307 static int rmi_probe(struct hid_device *hdev, const struct hid_device_id *id)
1308 {
1309         struct rmi_data *data = NULL;
1310         int ret;
1311         size_t alloc_size;
1312         struct hid_report *input_report;
1313         struct hid_report *output_report;
1314         struct hid_report *feature_report;
1315
1316         data = devm_kzalloc(&hdev->dev, sizeof(struct rmi_data), GFP_KERNEL);
1317         if (!data)
1318                 return -ENOMEM;
1319
1320         INIT_WORK(&data->reset_work, rmi_reset_work);
1321         data->hdev = hdev;
1322
1323         hid_set_drvdata(hdev, data);
1324
1325         hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1326
1327         ret = hid_parse(hdev);
1328         if (ret) {
1329                 hid_err(hdev, "parse failed\n");
1330                 return ret;
1331         }
1332
1333         if (id->driver_data)
1334                 data->device_flags = id->driver_data;
1335
1336         /*
1337          * Check for the RMI specific report ids. If they are misisng
1338          * simply return and let the events be processed by hid-input
1339          */
1340         if (!rmi_check_valid_report_id(hdev, HID_FEATURE_REPORT,
1341             RMI_SET_RMI_MODE_REPORT_ID, &feature_report)) {
1342                 hid_dbg(hdev, "device does not have set mode feature report\n");
1343                 goto start;
1344         }
1345
1346         if (!rmi_check_valid_report_id(hdev, HID_INPUT_REPORT,
1347             RMI_ATTN_REPORT_ID, &input_report)) {
1348                 hid_dbg(hdev, "device does not have attention input report\n");
1349                 goto start;
1350         }
1351
1352         data->input_report_size = hid_report_len(input_report);
1353
1354         if (!rmi_check_valid_report_id(hdev, HID_OUTPUT_REPORT,
1355             RMI_WRITE_REPORT_ID, &output_report)) {
1356                 hid_dbg(hdev,
1357                         "device does not have rmi write output report\n");
1358                 goto start;
1359         }
1360
1361         data->output_report_size = hid_report_len(output_report);
1362
1363         data->device_flags |= RMI_DEVICE;
1364         alloc_size = data->output_report_size + data->input_report_size;
1365
1366         data->writeReport = devm_kzalloc(&hdev->dev, alloc_size, GFP_KERNEL);
1367         if (!data->writeReport) {
1368                 ret = -ENOMEM;
1369                 return ret;
1370         }
1371
1372         data->readReport = data->writeReport + data->output_report_size;
1373
1374         init_waitqueue_head(&data->wait);
1375
1376         mutex_init(&data->page_mutex);
1377
1378 start:
1379         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1380         if (ret) {
1381                 hid_err(hdev, "hw start failed\n");
1382                 return ret;
1383         }
1384
1385         if ((data->device_flags & RMI_DEVICE) &&
1386             !test_bit(RMI_STARTED, &data->flags))
1387                 /*
1388                  * The device maybe in the bootloader if rmi_input_configured
1389                  * failed to find F11 in the PDT. Print an error, but don't
1390                  * return an error from rmi_probe so that hidraw will be
1391                  * accessible from userspace. That way a userspace tool
1392                  * can be used to reload working firmware on the touchpad.
1393                  */
1394                 hid_err(hdev, "Device failed to be properly configured\n");
1395
1396         return 0;
1397 }
1398
1399 static void rmi_remove(struct hid_device *hdev)
1400 {
1401         struct rmi_data *hdata = hid_get_drvdata(hdev);
1402
1403         clear_bit(RMI_STARTED, &hdata->flags);
1404
1405         hid_hw_stop(hdev);
1406 }
1407
1408 static const struct hid_device_id rmi_id[] = {
1409         { HID_USB_DEVICE(USB_VENDOR_ID_RAZER, USB_DEVICE_ID_RAZER_BLADE_14),
1410                 .driver_data = RMI_DEVICE_HAS_PHYS_BUTTONS },
1411         { HID_DEVICE(HID_BUS_ANY, HID_GROUP_RMI, HID_ANY_ID, HID_ANY_ID) },
1412         { }
1413 };
1414 MODULE_DEVICE_TABLE(hid, rmi_id);
1415
1416 static struct hid_driver rmi_driver = {
1417         .name = "hid-rmi",
1418         .id_table               = rmi_id,
1419         .probe                  = rmi_probe,
1420         .remove                 = rmi_remove,
1421         .event                  = rmi_event,
1422         .raw_event              = rmi_raw_event,
1423         .input_mapping          = rmi_input_mapping,
1424         .input_configured       = rmi_input_configured,
1425 #ifdef CONFIG_PM
1426         .suspend                = rmi_suspend,
1427         .resume                 = rmi_post_resume,
1428         .reset_resume           = rmi_post_reset,
1429 #endif
1430 };
1431
1432 module_hid_driver(rmi_driver);
1433
1434 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
1435 MODULE_DESCRIPTION("RMI HID driver");
1436 MODULE_LICENSE("GPL");