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
31struct 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
43int 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
62void 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
77static 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
97static 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
142static 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
165static 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
173static 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
201static 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
225static 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
247static irqreturn_t rmi_f03_attention(int irq, void *ctx)
248{
249 struct rmi_function *fn = ctx;
250 struct rmi_device *rmi_dev = fn->rmi_dev;
251 struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
252 struct f03_data *f03 = dev_get_drvdata(&fn->dev);
253 const u16 data_addr = fn->fd.data_base_addr + RMI_F03_OB_OFFSET;
254 const u8 ob_len = f03->rx_queue_length * RMI_F03_OB_SIZE;
255 u8 obs[RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE];
256 u8 ob_status;
257 u8 ob_data;
258 unsigned int serio_flags;
259 int i;
260 int error;
261
262 if (drvdata->attn_data.data) {
263 /* First grab the data passed by the transport device */
264 if (drvdata->attn_data.size < ob_len) {
265 dev_warn(&fn->dev, "F03 interrupted, but data is missing!\n");
266 return IRQ_HANDLED;
267 }
268
269 memcpy(obs, drvdata->attn_data.data, ob_len);
270
271 drvdata->attn_data.data += ob_len;
272 drvdata->attn_data.size -= ob_len;
273 } else {
274 /* Grab all of the data registers, and check them for data */
275 error = rmi_read_block(fn->rmi_dev, data_addr, &obs, ob_len);
276 if (error) {
277 dev_err(&fn->dev,
278 "%s: Failed to read F03 output buffers: %d\n",
279 __func__, error);
280 serio_interrupt(f03->serio, 0, SERIO_TIMEOUT);
281 return IRQ_RETVAL(error);
282 }
283 }
284
285 for (i = 0; i < ob_len; i += RMI_F03_OB_SIZE) {
286 ob_status = obs[i];
287 ob_data = obs[i + RMI_F03_OB_DATA_OFFSET];
288 serio_flags = 0;
289
290 if (!(ob_status & RMI_F03_RX_DATA_OFB))
291 continue;
292
293 if (ob_status & RMI_F03_OB_FLAG_TIMEOUT)
294 serio_flags |= SERIO_TIMEOUT;
295 if (ob_status & RMI_F03_OB_FLAG_PARITY)
296 serio_flags |= SERIO_PARITY;
297
298 rmi_dbg(RMI_DEBUG_FN, &fn->dev,
299 "%s: Received %.2hhx from PS2 guest T: %c P: %c\n",
300 __func__, ob_data,
301 serio_flags & SERIO_TIMEOUT ? 'Y' : 'N',
302 serio_flags & SERIO_PARITY ? 'Y' : 'N');
303
304 serio_interrupt(f03->serio, ob_data, serio_flags);
305 }
306
307 return IRQ_HANDLED;
308}
309
310static void rmi_f03_remove(struct rmi_function *fn)
311{
312 struct f03_data *f03 = dev_get_drvdata(&fn->dev);
313
314 if (f03->serio_registered)
315 serio_unregister_port(f03->serio);
316}
317
318struct rmi_function_handler rmi_f03_handler = {
319 .driver = {
320 .name = "rmi4_f03",
321 },
322 .func = 0x03,
323 .probe = rmi_f03_probe,
324 .config = rmi_f03_config,
325 .attention = rmi_f03_attention,
326 .remove = rmi_f03_remove,
327};
328
329MODULE_AUTHOR("Lyude Paul <thatslyude@gmail.com>");
330MODULE_DESCRIPTION("RMI F03 module");
331MODULE_LICENSE("GPL");
332