1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015-2016 Red Hat
4 * Copyright (C) 2015 Lyude Paul <thatslyude@gmail.com>
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/serio.h>
10 #include <linux/notifier.h>
11 #include "rmi_driver.h"
13 #define RMI_F03_RX_DATA_OFB 0x01
14 #define RMI_F03_OB_SIZE 2
16 #define RMI_F03_OB_OFFSET 2
17 #define RMI_F03_OB_DATA_OFFSET 1
18 #define RMI_F03_OB_FLAG_TIMEOUT BIT(6)
19 #define RMI_F03_OB_FLAG_PARITY BIT(7)
21 #define RMI_F03_DEVICE_COUNT 0x07
22 #define RMI_F03_BYTES_PER_DEVICE 0x07
23 #define RMI_F03_BYTES_PER_DEVICE_SHIFT 4
24 #define RMI_F03_QUEUE_LENGTH 0x0F
26 #define PSMOUSE_OOB_EXTRA_BTNS 0x01
29 struct rmi_function
*fn
;
32 bool serio_registered
;
34 unsigned int overwrite_buttons
;
40 int rmi_f03_overwrite_button(struct rmi_function
*fn
, unsigned int button
,
43 struct f03_data
*f03
= dev_get_drvdata(&fn
->dev
);
46 if (button
< BTN_LEFT
|| button
> BTN_MIDDLE
)
49 bit
= BIT(button
- BTN_LEFT
);
52 f03
->overwrite_buttons
|= bit
;
54 f03
->overwrite_buttons
&= ~bit
;
59 void rmi_f03_commit_buttons(struct rmi_function
*fn
)
61 struct f03_data
*f03
= dev_get_drvdata(&fn
->dev
);
62 struct serio
*serio
= f03
->serio
;
64 serio_pause_rx(serio
);
66 serio
->drv
->interrupt(serio
, PSMOUSE_OOB_EXTRA_BTNS
,
68 serio
->drv
->interrupt(serio
, f03
->overwrite_buttons
,
71 serio_continue_rx(serio
);
74 static int rmi_f03_pt_write(struct serio
*id
, unsigned char val
)
76 struct f03_data
*f03
= id
->port_data
;
79 rmi_dbg(RMI_DEBUG_FN
, &f03
->fn
->dev
,
80 "%s: Wrote %.2hhx to PS/2 passthrough address",
83 error
= rmi_write(f03
->fn
->rmi_dev
, f03
->fn
->fd
.data_base_addr
, val
);
85 dev_err(&f03
->fn
->dev
,
86 "%s: Failed to write to F03 TX register (%d).\n",
94 static int rmi_f03_initialize(struct f03_data
*f03
)
96 struct rmi_function
*fn
= f03
->fn
;
97 struct device
*dev
= &fn
->dev
;
101 u8 query2
[RMI_F03_DEVICE_COUNT
* RMI_F03_BYTES_PER_DEVICE
];
104 error
= rmi_read(fn
->rmi_dev
, fn
->fd
.query_base_addr
, &query1
);
106 dev_err(dev
, "Failed to read query register (%d).\n", error
);
110 f03
->device_count
= query1
& RMI_F03_DEVICE_COUNT
;
111 bytes_per_device
= (query1
>> RMI_F03_BYTES_PER_DEVICE_SHIFT
) &
112 RMI_F03_BYTES_PER_DEVICE
;
114 query2_len
= f03
->device_count
* bytes_per_device
;
117 * The first generation of image sensors don't have a second part to
118 * their f03 query, as such we have to set some of these values manually
120 if (query2_len
< 1) {
121 f03
->device_count
= 1;
122 f03
->rx_queue_length
= 7;
124 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
+ 1,
128 "Failed to read second set of query registers (%d).\n",
133 f03
->rx_queue_length
= query2
[0] & RMI_F03_QUEUE_LENGTH
;
139 static int rmi_f03_pt_open(struct serio
*serio
)
141 struct f03_data
*f03
= serio
->port_data
;
142 struct rmi_function
*fn
= f03
->fn
;
143 const u8 ob_len
= f03
->rx_queue_length
* RMI_F03_OB_SIZE
;
144 const u16 data_addr
= fn
->fd
.data_base_addr
+ RMI_F03_OB_OFFSET
;
145 u8 obs
[RMI_F03_QUEUE_LENGTH
* RMI_F03_OB_SIZE
];
149 * Consume any pending data. Some devices like to spam with
150 * 0xaa 0x00 announcements which may confuse us as we try to
153 error
= rmi_read_block(fn
->rmi_dev
, data_addr
, &obs
, ob_len
);
155 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
,
156 "%s: Consumed %*ph (%d) from PS2 guest\n",
157 __func__
, ob_len
, obs
, ob_len
);
159 return fn
->rmi_dev
->driver
->set_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
162 static void rmi_f03_pt_close(struct serio
*serio
)
164 struct f03_data
*f03
= serio
->port_data
;
165 struct rmi_function
*fn
= f03
->fn
;
167 fn
->rmi_dev
->driver
->clear_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
170 static int rmi_f03_register_pt(struct f03_data
*f03
)
174 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
178 serio
->id
.type
= SERIO_PS_PSTHRU
;
179 serio
->write
= rmi_f03_pt_write
;
180 serio
->open
= rmi_f03_pt_open
;
181 serio
->close
= rmi_f03_pt_close
;
182 serio
->port_data
= f03
;
184 strlcpy(serio
->name
, "RMI4 PS/2 pass-through", sizeof(serio
->name
));
185 snprintf(serio
->phys
, sizeof(serio
->phys
), "%s/serio0",
186 dev_name(&f03
->fn
->dev
));
187 serio
->dev
.parent
= &f03
->fn
->dev
;
191 printk(KERN_INFO
"serio: %s port at %s\n",
192 serio
->name
, dev_name(&f03
->fn
->dev
));
193 serio_register_port(serio
);
198 static int rmi_f03_probe(struct rmi_function
*fn
)
200 struct device
*dev
= &fn
->dev
;
201 struct f03_data
*f03
;
204 f03
= devm_kzalloc(dev
, sizeof(struct f03_data
), GFP_KERNEL
);
210 error
= rmi_f03_initialize(f03
);
214 if (f03
->device_count
!= 1)
215 dev_warn(dev
, "found %d devices on PS/2 passthrough",
218 dev_set_drvdata(dev
, f03
);
222 static int rmi_f03_config(struct rmi_function
*fn
)
224 struct f03_data
*f03
= dev_get_drvdata(&fn
->dev
);
227 if (!f03
->serio_registered
) {
228 error
= rmi_f03_register_pt(f03
);
232 f03
->serio_registered
= true;
235 * We must be re-configuring the sensor, just enable
236 * interrupts for this function.
238 fn
->rmi_dev
->driver
->set_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
244 static irqreturn_t
rmi_f03_attention(int irq
, void *ctx
)
246 struct rmi_function
*fn
= ctx
;
247 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
248 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
249 struct f03_data
*f03
= dev_get_drvdata(&fn
->dev
);
250 const u16 data_addr
= fn
->fd
.data_base_addr
+ RMI_F03_OB_OFFSET
;
251 const u8 ob_len
= f03
->rx_queue_length
* RMI_F03_OB_SIZE
;
252 u8 obs
[RMI_F03_QUEUE_LENGTH
* RMI_F03_OB_SIZE
];
255 unsigned int serio_flags
;
259 if (drvdata
->attn_data
.data
) {
260 /* First grab the data passed by the transport device */
261 if (drvdata
->attn_data
.size
< ob_len
) {
262 dev_warn(&fn
->dev
, "F03 interrupted, but data is missing!\n");
266 memcpy(obs
, drvdata
->attn_data
.data
, ob_len
);
268 drvdata
->attn_data
.data
+= ob_len
;
269 drvdata
->attn_data
.size
-= ob_len
;
271 /* Grab all of the data registers, and check them for data */
272 error
= rmi_read_block(fn
->rmi_dev
, data_addr
, &obs
, ob_len
);
275 "%s: Failed to read F03 output buffers: %d\n",
277 serio_interrupt(f03
->serio
, 0, SERIO_TIMEOUT
);
278 return IRQ_RETVAL(error
);
282 for (i
= 0; i
< ob_len
; i
+= RMI_F03_OB_SIZE
) {
284 ob_data
= obs
[i
+ RMI_F03_OB_DATA_OFFSET
];
287 if (!(ob_status
& RMI_F03_RX_DATA_OFB
))
290 if (ob_status
& RMI_F03_OB_FLAG_TIMEOUT
)
291 serio_flags
|= SERIO_TIMEOUT
;
292 if (ob_status
& RMI_F03_OB_FLAG_PARITY
)
293 serio_flags
|= SERIO_PARITY
;
295 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
,
296 "%s: Received %.2hhx from PS2 guest T: %c P: %c\n",
298 serio_flags
& SERIO_TIMEOUT
? 'Y' : 'N',
299 serio_flags
& SERIO_PARITY
? 'Y' : 'N');
301 serio_interrupt(f03
->serio
, ob_data
, serio_flags
);
307 static void rmi_f03_remove(struct rmi_function
*fn
)
309 struct f03_data
*f03
= dev_get_drvdata(&fn
->dev
);
311 if (f03
->serio_registered
)
312 serio_unregister_port(f03
->serio
);
315 struct rmi_function_handler rmi_f03_handler
= {
320 .probe
= rmi_f03_probe
,
321 .config
= rmi_f03_config
,
322 .attention
= rmi_f03_attention
,
323 .remove
= rmi_f03_remove
,
326 MODULE_AUTHOR("Lyude Paul <thatslyude@gmail.com>");
327 MODULE_DESCRIPTION("RMI F03 module");
328 MODULE_LICENSE("GPL");