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