2 * Copyright (C) 2015-2016 Red Hat
3 * Copyright (C) 2015 Lyude Paul <thatslyude@gmail.com>
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.
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"
16 #define RMI_F03_RX_DATA_OFB 0x01
17 #define RMI_F03_OB_SIZE 2
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)
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
29 #define PSMOUSE_OOB_EXTRA_BTNS 0x01
32 struct rmi_function
*fn
;
35 bool serio_registered
;
37 unsigned int overwrite_buttons
;
43 int rmi_f03_overwrite_button(struct rmi_function
*fn
, unsigned int button
,
46 struct f03_data
*f03
= dev_get_drvdata(&fn
->dev
);
49 if (button
< BTN_LEFT
|| button
> BTN_MIDDLE
)
52 bit
= BIT(button
- BTN_LEFT
);
55 f03
->overwrite_buttons
|= bit
;
57 f03
->overwrite_buttons
&= ~bit
;
62 void rmi_f03_commit_buttons(struct rmi_function
*fn
)
64 struct f03_data
*f03
= dev_get_drvdata(&fn
->dev
);
65 struct serio
*serio
= f03
->serio
;
67 serio_pause_rx(serio
);
69 serio
->drv
->interrupt(serio
, PSMOUSE_OOB_EXTRA_BTNS
,
71 serio
->drv
->interrupt(serio
, f03
->overwrite_buttons
,
74 serio_continue_rx(serio
);
77 static int rmi_f03_pt_write(struct serio
*id
, unsigned char val
)
79 struct f03_data
*f03
= id
->port_data
;
82 rmi_dbg(RMI_DEBUG_FN
, &f03
->fn
->dev
,
83 "%s: Wrote %.2hhx to PS/2 passthrough address",
86 error
= rmi_write(f03
->fn
->rmi_dev
, f03
->fn
->fd
.data_base_addr
, val
);
88 dev_err(&f03
->fn
->dev
,
89 "%s: Failed to write to F03 TX register (%d).\n",
97 static int rmi_f03_initialize(struct f03_data
*f03
)
99 struct rmi_function
*fn
= f03
->fn
;
100 struct device
*dev
= &fn
->dev
;
104 u8 query2
[RMI_F03_DEVICE_COUNT
* RMI_F03_BYTES_PER_DEVICE
];
107 error
= rmi_read(fn
->rmi_dev
, fn
->fd
.query_base_addr
, &query1
);
109 dev_err(dev
, "Failed to read query register (%d).\n", error
);
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
;
117 query2_len
= f03
->device_count
* bytes_per_device
;
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
123 if (query2_len
< 1) {
124 f03
->device_count
= 1;
125 f03
->rx_queue_length
= 7;
127 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
+ 1,
131 "Failed to read second set of query registers (%d).\n",
136 f03
->rx_queue_length
= query2
[0] & RMI_F03_QUEUE_LENGTH
;
142 static int rmi_f03_pt_open(struct serio
*serio
)
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
];
152 * Consume any pending data. Some devices like to spam with
153 * 0xaa 0x00 announcements which may confuse us as we try to
156 error
= rmi_read_block(fn
->rmi_dev
, data_addr
, &obs
, ob_len
);
158 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
,
159 "%s: Consumed %*ph (%d) from PS2 guest\n",
160 __func__
, ob_len
, obs
, ob_len
);
162 return fn
->rmi_dev
->driver
->set_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
165 static void rmi_f03_pt_close(struct serio
*serio
)
167 struct f03_data
*f03
= serio
->port_data
;
168 struct rmi_function
*fn
= f03
->fn
;
170 fn
->rmi_dev
->driver
->clear_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
173 static int rmi_f03_register_pt(struct f03_data
*f03
)
177 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
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
;
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
;
194 printk(KERN_INFO
"serio: %s port at %s\n",
195 serio
->name
, dev_name(&f03
->fn
->dev
));
196 serio_register_port(serio
);
201 static int rmi_f03_probe(struct rmi_function
*fn
)
203 struct device
*dev
= &fn
->dev
;
204 struct f03_data
*f03
;
207 f03
= devm_kzalloc(dev
, sizeof(struct f03_data
), GFP_KERNEL
);
213 error
= rmi_f03_initialize(f03
);
217 if (f03
->device_count
!= 1)
218 dev_warn(dev
, "found %d devices on PS/2 passthrough",
221 dev_set_drvdata(dev
, f03
);
225 static int rmi_f03_config(struct rmi_function
*fn
)
227 struct f03_data
*f03
= dev_get_drvdata(&fn
->dev
);
230 if (!f03
->serio_registered
) {
231 error
= rmi_f03_register_pt(f03
);
235 f03
->serio_registered
= true;
238 * We must be re-configuring the sensor, just enable
239 * interrupts for this function.
241 fn
->rmi_dev
->driver
->set_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
247 static irqreturn_t
rmi_f03_attention(int irq
, void *ctx
)
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
];
258 unsigned int serio_flags
;
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");
269 memcpy(obs
, drvdata
->attn_data
.data
, ob_len
);
271 drvdata
->attn_data
.data
+= ob_len
;
272 drvdata
->attn_data
.size
-= ob_len
;
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
);
278 "%s: Failed to read F03 output buffers: %d\n",
280 serio_interrupt(f03
->serio
, 0, SERIO_TIMEOUT
);
281 return IRQ_RETVAL(error
);
285 for (i
= 0; i
< ob_len
; i
+= RMI_F03_OB_SIZE
) {
287 ob_data
= obs
[i
+ RMI_F03_OB_DATA_OFFSET
];
290 if (!(ob_status
& RMI_F03_RX_DATA_OFB
))
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
;
298 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
,
299 "%s: Received %.2hhx from PS2 guest T: %c P: %c\n",
301 serio_flags
& SERIO_TIMEOUT
? 'Y' : 'N',
302 serio_flags
& SERIO_PARITY
? 'Y' : 'N');
304 serio_interrupt(f03
->serio
, ob_data
, serio_flags
);
310 static void rmi_f03_remove(struct rmi_function
*fn
)
312 struct f03_data
*f03
= dev_get_drvdata(&fn
->dev
);
314 if (f03
->serio_registered
)
315 serio_unregister_port(f03
->serio
);
318 struct rmi_function_handler rmi_f03_handler
= {
323 .probe
= rmi_f03_probe
,
324 .config
= rmi_f03_config
,
325 .attention
= rmi_f03_attention
,
326 .remove
= rmi_f03_remove
,
329 MODULE_AUTHOR("Lyude Paul <thatslyude@gmail.com>");
330 MODULE_DESCRIPTION("RMI F03 module");
331 MODULE_LICENSE("GPL");