xref: /openbmc/linux/drivers/input/rmi4/rmi_f03.c (revision ddc141e5)
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 
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 
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 
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 
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 
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 
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 
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, "RMI4 PS/2 pass-through", sizeof(serio->name));
188 	snprintf(serio->phys, sizeof(serio->phys), "%s/serio0",
189 		 dev_name(&f03->fn->dev));
190 	serio->dev.parent = &f03->fn->dev;
191 
192 	f03->serio = serio;
193 
194 	printk(KERN_INFO "serio: %s port at %s\n",
195 		serio->name, dev_name(&f03->fn->dev));
196 	serio_register_port(serio);
197 
198 	return 0;
199 }
200 
201 static int rmi_f03_probe(struct rmi_function *fn)
202 {
203 	struct device *dev = &fn->dev;
204 	struct f03_data *f03;
205 	int error;
206 
207 	f03 = devm_kzalloc(dev, sizeof(struct f03_data), GFP_KERNEL);
208 	if (!f03)
209 		return -ENOMEM;
210 
211 	f03->fn = fn;
212 
213 	error = rmi_f03_initialize(f03);
214 	if (error < 0)
215 		return error;
216 
217 	if (f03->device_count != 1)
218 		dev_warn(dev, "found %d devices on PS/2 passthrough",
219 			 f03->device_count);
220 
221 	dev_set_drvdata(dev, f03);
222 	return 0;
223 }
224 
225 static int rmi_f03_config(struct rmi_function *fn)
226 {
227 	struct f03_data *f03 = dev_get_drvdata(&fn->dev);
228 	int error;
229 
230 	if (!f03->serio_registered) {
231 		error = rmi_f03_register_pt(f03);
232 		if (error)
233 			return error;
234 
235 		f03->serio_registered = true;
236 	} else {
237 		/*
238 		 * We must be re-configuring the sensor, just enable
239 		 * interrupts for this function.
240 		 */
241 		fn->rmi_dev->driver->set_irq_bits(fn->rmi_dev, fn->irq_mask);
242 	}
243 
244 	return 0;
245 }
246 
247 static int rmi_f03_attention(struct rmi_function *fn, unsigned long *irq_bits)
248 {
249 	struct rmi_device *rmi_dev = fn->rmi_dev;
250 	struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
251 	struct f03_data *f03 = dev_get_drvdata(&fn->dev);
252 	const u16 data_addr = fn->fd.data_base_addr + RMI_F03_OB_OFFSET;
253 	const u8 ob_len = f03->rx_queue_length * RMI_F03_OB_SIZE;
254 	u8 obs[RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE];
255 	u8 ob_status;
256 	u8 ob_data;
257 	unsigned int serio_flags;
258 	int i;
259 	int error;
260 
261 	if (drvdata->attn_data.data) {
262 		/* First grab the data passed by the transport device */
263 		if (drvdata->attn_data.size < ob_len) {
264 			dev_warn(&fn->dev, "F03 interrupted, but data is missing!\n");
265 			return 0;
266 		}
267 
268 		memcpy(obs, drvdata->attn_data.data, ob_len);
269 
270 		drvdata->attn_data.data += ob_len;
271 		drvdata->attn_data.size -= ob_len;
272 	} else {
273 		/* Grab all of the data registers, and check them for data */
274 		error = rmi_read_block(fn->rmi_dev, data_addr, &obs, ob_len);
275 		if (error) {
276 			dev_err(&fn->dev,
277 				"%s: Failed to read F03 output buffers: %d\n",
278 				__func__, error);
279 			serio_interrupt(f03->serio, 0, SERIO_TIMEOUT);
280 			return error;
281 		}
282 	}
283 
284 	for (i = 0; i < ob_len; i += RMI_F03_OB_SIZE) {
285 		ob_status = obs[i];
286 		ob_data = obs[i + RMI_F03_OB_DATA_OFFSET];
287 		serio_flags = 0;
288 
289 		if (!(ob_status & RMI_F03_RX_DATA_OFB))
290 			continue;
291 
292 		if (ob_status & RMI_F03_OB_FLAG_TIMEOUT)
293 			serio_flags |= SERIO_TIMEOUT;
294 		if (ob_status & RMI_F03_OB_FLAG_PARITY)
295 			serio_flags |= SERIO_PARITY;
296 
297 		rmi_dbg(RMI_DEBUG_FN, &fn->dev,
298 			"%s: Received %.2hhx from PS2 guest T: %c P: %c\n",
299 			__func__, ob_data,
300 			serio_flags & SERIO_TIMEOUT ?  'Y' : 'N',
301 			serio_flags & SERIO_PARITY ? 'Y' : 'N');
302 
303 		serio_interrupt(f03->serio, ob_data, serio_flags);
304 	}
305 
306 	return 0;
307 }
308 
309 static void rmi_f03_remove(struct rmi_function *fn)
310 {
311 	struct f03_data *f03 = dev_get_drvdata(&fn->dev);
312 
313 	if (f03->serio_registered)
314 		serio_unregister_port(f03->serio);
315 }
316 
317 struct rmi_function_handler rmi_f03_handler = {
318 	.driver = {
319 		.name = "rmi4_f03",
320 	},
321 	.func = 0x03,
322 	.probe = rmi_f03_probe,
323 	.config = rmi_f03_config,
324 	.attention = rmi_f03_attention,
325 	.remove = rmi_f03_remove,
326 };
327 
328 MODULE_AUTHOR("Lyude Paul <thatslyude@gmail.com>");
329 MODULE_DESCRIPTION("RMI F03 module");
330 MODULE_LICENSE("GPL");
331