• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015-2016 Red Hat
3  * Copyright (C) 2015 Lyude Paul <thatslyude@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/serio.h>
13 #include <linux/notifier.h>
14 #include "rmi_driver.h"
15 
16 #define RMI_F03_RX_DATA_OFB		0x01
17 #define RMI_F03_OB_SIZE			2
18 
19 #define RMI_F03_OB_OFFSET		2
20 #define RMI_F03_OB_DATA_OFFSET		1
21 #define RMI_F03_OB_FLAG_TIMEOUT		BIT(6)
22 #define RMI_F03_OB_FLAG_PARITY		BIT(7)
23 
24 #define RMI_F03_DEVICE_COUNT		0x07
25 #define RMI_F03_BYTES_PER_DEVICE	0x07
26 #define RMI_F03_BYTES_PER_DEVICE_SHIFT	4
27 #define RMI_F03_QUEUE_LENGTH		0x0F
28 
29 #define PSMOUSE_OOB_EXTRA_BTNS		0x01
30 
31 struct f03_data {
32 	struct rmi_function *fn;
33 
34 	struct serio *serio;
35 	bool serio_registered;
36 
37 	unsigned int overwrite_buttons;
38 
39 	u8 device_count;
40 	u8 rx_queue_length;
41 };
42 
rmi_f03_overwrite_button(struct rmi_function * fn,unsigned int button,int value)43 int rmi_f03_overwrite_button(struct rmi_function *fn, unsigned int button,
44 			     int value)
45 {
46 	struct f03_data *f03 = dev_get_drvdata(&fn->dev);
47 	unsigned int bit;
48 
49 	if (button < BTN_LEFT || button > BTN_MIDDLE)
50 		return -EINVAL;
51 
52 	bit = BIT(button - BTN_LEFT);
53 
54 	if (value)
55 		f03->overwrite_buttons |= bit;
56 	else
57 		f03->overwrite_buttons &= ~bit;
58 
59 	return 0;
60 }
61 
rmi_f03_commit_buttons(struct rmi_function * fn)62 void rmi_f03_commit_buttons(struct rmi_function *fn)
63 {
64 	struct f03_data *f03 = dev_get_drvdata(&fn->dev);
65 	struct serio *serio = f03->serio;
66 
67 	serio_pause_rx(serio);
68 	if (serio->drv) {
69 		serio->drv->interrupt(serio, PSMOUSE_OOB_EXTRA_BTNS,
70 				      SERIO_OOB_DATA);
71 		serio->drv->interrupt(serio, f03->overwrite_buttons,
72 				      SERIO_OOB_DATA);
73 	}
74 	serio_continue_rx(serio);
75 }
76 
rmi_f03_pt_write(struct serio * id,unsigned char val)77 static int rmi_f03_pt_write(struct serio *id, unsigned char val)
78 {
79 	struct f03_data *f03 = id->port_data;
80 	int error;
81 
82 	rmi_dbg(RMI_DEBUG_FN, &f03->fn->dev,
83 		"%s: Wrote %.2hhx to PS/2 passthrough address",
84 		__func__, val);
85 
86 	error = rmi_write(f03->fn->rmi_dev, f03->fn->fd.data_base_addr, val);
87 	if (error) {
88 		dev_err(&f03->fn->dev,
89 			"%s: Failed to write to F03 TX register (%d).\n",
90 			__func__, error);
91 		return error;
92 	}
93 
94 	return 0;
95 }
96 
rmi_f03_initialize(struct f03_data * f03)97 static int rmi_f03_initialize(struct f03_data *f03)
98 {
99 	struct rmi_function *fn = f03->fn;
100 	struct device *dev = &fn->dev;
101 	int error;
102 	u8 bytes_per_device;
103 	u8 query1;
104 	u8 query2[RMI_F03_DEVICE_COUNT * RMI_F03_BYTES_PER_DEVICE];
105 	size_t query2_len;
106 
107 	error = rmi_read(fn->rmi_dev, fn->fd.query_base_addr, &query1);
108 	if (error) {
109 		dev_err(dev, "Failed to read query register (%d).\n", error);
110 		return error;
111 	}
112 
113 	f03->device_count = query1 & RMI_F03_DEVICE_COUNT;
114 	bytes_per_device = (query1 >> RMI_F03_BYTES_PER_DEVICE_SHIFT) &
115 				RMI_F03_BYTES_PER_DEVICE;
116 
117 	query2_len = f03->device_count * bytes_per_device;
118 
119 	/*
120 	 * The first generation of image sensors don't have a second part to
121 	 * their f03 query, as such we have to set some of these values manually
122 	 */
123 	if (query2_len < 1) {
124 		f03->device_count = 1;
125 		f03->rx_queue_length = 7;
126 	} else {
127 		error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr + 1,
128 				       query2, query2_len);
129 		if (error) {
130 			dev_err(dev,
131 				"Failed to read second set of query registers (%d).\n",
132 				error);
133 			return error;
134 		}
135 
136 		f03->rx_queue_length = query2[0] & RMI_F03_QUEUE_LENGTH;
137 	}
138 
139 	return 0;
140 }
141 
rmi_f03_pt_open(struct serio * serio)142 static int rmi_f03_pt_open(struct serio *serio)
143 {
144 	struct f03_data *f03 = serio->port_data;
145 	struct rmi_function *fn = f03->fn;
146 	const u8 ob_len = f03->rx_queue_length * RMI_F03_OB_SIZE;
147 	const u16 data_addr = fn->fd.data_base_addr + RMI_F03_OB_OFFSET;
148 	u8 obs[RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE];
149 	int error;
150 
151 	/*
152 	 * Consume any pending data. Some devices like to spam with
153 	 * 0xaa 0x00 announcements which may confuse us as we try to
154 	 * probe the device.
155 	 */
156 	error = rmi_read_block(fn->rmi_dev, data_addr, &obs, ob_len);
157 	if (!error)
158 		rmi_dbg(RMI_DEBUG_FN, &fn->dev,
159 			"%s: Consumed %*ph (%d) from PS2 guest\n",
160 			__func__, ob_len, obs, ob_len);
161 
162 	return fn->rmi_dev->driver->set_irq_bits(fn->rmi_dev, fn->irq_mask);
163 }
164 
rmi_f03_pt_close(struct serio * serio)165 static void rmi_f03_pt_close(struct serio *serio)
166 {
167 	struct f03_data *f03 = serio->port_data;
168 	struct rmi_function *fn = f03->fn;
169 
170 	fn->rmi_dev->driver->clear_irq_bits(fn->rmi_dev, fn->irq_mask);
171 }
172 
rmi_f03_register_pt(struct f03_data * f03)173 static int rmi_f03_register_pt(struct f03_data *f03)
174 {
175 	struct serio *serio;
176 
177 	serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
178 	if (!serio)
179 		return -ENOMEM;
180 
181 	serio->id.type = SERIO_PS_PSTHRU;
182 	serio->write = rmi_f03_pt_write;
183 	serio->open = rmi_f03_pt_open;
184 	serio->close = rmi_f03_pt_close;
185 	serio->port_data = f03;
186 
187 	strlcpy(serio->name, "Synaptics RMI4 PS/2 pass-through",
188 		sizeof(serio->name));
189 	strlcpy(serio->phys, "synaptics-rmi4-pt/serio1",
190 		sizeof(serio->phys));
191 	serio->dev.parent = &f03->fn->dev;
192 
193 	f03->serio = serio;
194 
195 	serio_register_port(serio);
196 
197 	return 0;
198 }
199 
rmi_f03_probe(struct rmi_function * fn)200 static int rmi_f03_probe(struct rmi_function *fn)
201 {
202 	struct device *dev = &fn->dev;
203 	struct f03_data *f03;
204 	int error;
205 
206 	f03 = devm_kzalloc(dev, sizeof(struct f03_data), GFP_KERNEL);
207 	if (!f03)
208 		return -ENOMEM;
209 
210 	f03->fn = fn;
211 
212 	error = rmi_f03_initialize(f03);
213 	if (error < 0)
214 		return error;
215 
216 	if (f03->device_count != 1)
217 		dev_warn(dev, "found %d devices on PS/2 passthrough",
218 			 f03->device_count);
219 
220 	dev_set_drvdata(dev, f03);
221 	return 0;
222 }
223 
rmi_f03_config(struct rmi_function * fn)224 static int rmi_f03_config(struct rmi_function *fn)
225 {
226 	struct f03_data *f03 = dev_get_drvdata(&fn->dev);
227 	int error;
228 
229 	if (!f03->serio_registered) {
230 		error = rmi_f03_register_pt(f03);
231 		if (error)
232 			return error;
233 
234 		f03->serio_registered = true;
235 	} else {
236 		/*
237 		 * We must be re-configuring the sensor, just enable
238 		 * interrupts for this function.
239 		 */
240 		fn->rmi_dev->driver->set_irq_bits(fn->rmi_dev, fn->irq_mask);
241 	}
242 
243 	return 0;
244 }
245 
rmi_f03_attention(struct rmi_function * fn,unsigned long * irq_bits)246 static int rmi_f03_attention(struct rmi_function *fn, unsigned long *irq_bits)
247 {
248 	struct rmi_device *rmi_dev = fn->rmi_dev;
249 	struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
250 	struct f03_data *f03 = dev_get_drvdata(&fn->dev);
251 	const u16 data_addr = fn->fd.data_base_addr + RMI_F03_OB_OFFSET;
252 	const u8 ob_len = f03->rx_queue_length * RMI_F03_OB_SIZE;
253 	u8 obs[RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE];
254 	u8 ob_status;
255 	u8 ob_data;
256 	unsigned int serio_flags;
257 	int i;
258 	int error;
259 
260 	if (drvdata->attn_data.data) {
261 		/* First grab the data passed by the transport device */
262 		if (drvdata->attn_data.size < ob_len) {
263 			dev_warn(&fn->dev, "F03 interrupted, but data is missing!\n");
264 			return 0;
265 		}
266 
267 		memcpy(obs, drvdata->attn_data.data, ob_len);
268 
269 		drvdata->attn_data.data += ob_len;
270 		drvdata->attn_data.size -= ob_len;
271 	} else {
272 		/* Grab all of the data registers, and check them for data */
273 		error = rmi_read_block(fn->rmi_dev, data_addr, &obs, ob_len);
274 		if (error) {
275 			dev_err(&fn->dev,
276 				"%s: Failed to read F03 output buffers: %d\n",
277 				__func__, error);
278 			serio_interrupt(f03->serio, 0, SERIO_TIMEOUT);
279 			return error;
280 		}
281 	}
282 
283 	for (i = 0; i < ob_len; i += RMI_F03_OB_SIZE) {
284 		ob_status = obs[i];
285 		ob_data = obs[i + RMI_F03_OB_DATA_OFFSET];
286 		serio_flags = 0;
287 
288 		if (!(ob_status & RMI_F03_RX_DATA_OFB))
289 			continue;
290 
291 		if (ob_status & RMI_F03_OB_FLAG_TIMEOUT)
292 			serio_flags |= SERIO_TIMEOUT;
293 		if (ob_status & RMI_F03_OB_FLAG_PARITY)
294 			serio_flags |= SERIO_PARITY;
295 
296 		rmi_dbg(RMI_DEBUG_FN, &fn->dev,
297 			"%s: Received %.2hhx from PS2 guest T: %c P: %c\n",
298 			__func__, ob_data,
299 			serio_flags & SERIO_TIMEOUT ?  'Y' : 'N',
300 			serio_flags & SERIO_PARITY ? 'Y' : 'N');
301 
302 		serio_interrupt(f03->serio, ob_data, serio_flags);
303 	}
304 
305 	return 0;
306 }
307 
rmi_f03_remove(struct rmi_function * fn)308 static void rmi_f03_remove(struct rmi_function *fn)
309 {
310 	struct f03_data *f03 = dev_get_drvdata(&fn->dev);
311 
312 	if (f03->serio_registered)
313 		serio_unregister_port(f03->serio);
314 }
315 
316 struct rmi_function_handler rmi_f03_handler = {
317 	.driver = {
318 		.name = "rmi4_f03",
319 	},
320 	.func = 0x03,
321 	.probe = rmi_f03_probe,
322 	.config = rmi_f03_config,
323 	.attention = rmi_f03_attention,
324 	.remove = rmi_f03_remove,
325 };
326 
327 MODULE_AUTHOR("Lyude Paul <thatslyude@gmail.com>");
328 MODULE_DESCRIPTION("RMI F03 module");
329 MODULE_LICENSE("GPL");
330