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