Skip to content

Commit 8eb92e5

Browse files
bentissdtor
authored andcommitted
Input: psmouse - add support for SMBus companions
This provides glue between PS/2 devices that enumerate the RMI4 devices and Elan touchpads to the RMI4 (or Elan) SMBus driver. The SMBus devices keep their PS/2 connection alive. If the initialization process goes too far (psmouse_activate called), the device disconnects from the I2C bus and stays on the PS/2 bus, that is why we explicitly disable PS/2 device reporting (by calling psmouse_deactivate) before trying to register SMBus companion device. The HID over I2C devices are enumerated through the ACPI DSDT, and their PS/2 device also exports the InterTouch bit in the extended capability 0x0C. However, the firmware keeps its I2C connection open even after going further in the PS/2 initialization. We don't need to take extra precautions with those device, especially because they block their PS/2 communication when HID over I2C is used. Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent c774326 commit 8eb92e5

5 files changed

Lines changed: 343 additions & 2 deletions

File tree

‎drivers/input/mouse/Kconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ config MOUSE_PS2_VMMOUSE
171171

172172
If unsure, say N.
173173

174+
config MOUSE_PS2_SMBUS
175+
bool
176+
depends on MOUSE_PS2
177+
174178
config MOUSE_SERIAL
175179
tristate "Serial mouse"
176180
select SERIO

‎drivers/input/mouse/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ psmouse-$(CONFIG_MOUSE_PS2_TOUCHKIT) += touchkit_ps2.o
3939
psmouse-$(CONFIG_MOUSE_PS2_CYPRESS) += cypress_ps2.o
4040
psmouse-$(CONFIG_MOUSE_PS2_VMMOUSE) += vmmouse.o
4141

42+
psmouse-$(CONFIG_MOUSE_PS2_SMBUS) += psmouse-smbus.o
43+
4244
elan_i2c-objs := elan_i2c_core.o
4345
elan_i2c-$(CONFIG_MOUSE_ELAN_I2C_I2C) += elan_i2c_i2c.o
4446
elan_i2c-$(CONFIG_MOUSE_ELAN_I2C_SMBUS) += elan_i2c_smbus.o

‎drivers/input/mouse/psmouse-base.c‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,23 +1999,35 @@ static int __init psmouse_init(void)
19991999
synaptics_module_init();
20002000
hgpk_module_init();
20012001

2002+
err = psmouse_smbus_module_init();
2003+
if (err)
2004+
return err;
2005+
20022006
kpsmoused_wq = alloc_ordered_workqueue("kpsmoused", 0);
20032007
if (!kpsmoused_wq) {
20042008
pr_err("failed to create kpsmoused workqueue\n");
2005-
return -ENOMEM;
2009+
err = -ENOMEM;
2010+
goto err_smbus_exit;
20062011
}
20072012

20082013
err = serio_register_driver(&psmouse_drv);
20092014
if (err)
2010-
destroy_workqueue(kpsmoused_wq);
2015+
goto err_destroy_wq;
20112016

2017+
return 0;
2018+
2019+
err_destroy_wq:
2020+
destroy_workqueue(kpsmoused_wq);
2021+
err_smbus_exit:
2022+
psmouse_smbus_module_exit();
20122023
return err;
20132024
}
20142025

20152026
static void __exit psmouse_exit(void)
20162027
{
20172028
serio_unregister_driver(&psmouse_drv);
20182029
destroy_workqueue(kpsmoused_wq);
2030+
psmouse_smbus_module_exit();
20192031
}
20202032

20212033
module_init(psmouse_init);
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
/*
2+
* Copyright (c) 2017 Red Hat, Inc
3+
*
4+
* This program is free software; you can redistribute it and/or modify it
5+
* under the terms of the GNU General Public License version 2 as published by
6+
* the Free Software Foundation.
7+
*/
8+
9+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10+
11+
#include <linux/kernel.h>
12+
#include <linux/module.h>
13+
#include <linux/libps2.h>
14+
#include <linux/i2c.h>
15+
#include <linux/serio.h>
16+
#include <linux/slab.h>
17+
#include <linux/workqueue.h>
18+
#include "psmouse.h"
19+
20+
struct psmouse_smbus_dev {
21+
struct i2c_board_info board;
22+
struct psmouse *psmouse;
23+
struct i2c_client *client;
24+
struct list_head node;
25+
bool dead;
26+
};
27+
28+
static LIST_HEAD(psmouse_smbus_list);
29+
static DEFINE_MUTEX(psmouse_smbus_mutex);
30+
31+
static void psmouse_smbus_check_adapter(struct i2c_adapter *adapter)
32+
{
33+
struct psmouse_smbus_dev *smbdev;
34+
35+
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
36+
return;
37+
38+
mutex_lock(&psmouse_smbus_mutex);
39+
40+
list_for_each_entry(smbdev, &psmouse_smbus_list, node) {
41+
if (smbdev->dead)
42+
continue;
43+
44+
if (smbdev->client)
45+
continue;
46+
47+
/*
48+
* Here would be a good place to check if device is actually
49+
* present, but it seems that SMBus will not respond unless we
50+
* fully reset PS/2 connection. So cross our fingers, and try
51+
* to switch over, hopefully our system will not have too many
52+
* "host notify" I2C adapters.
53+
*/
54+
psmouse_dbg(smbdev->psmouse,
55+
"SMBus candidate adapter appeared, triggering rescan\n");
56+
serio_rescan(smbdev->psmouse->ps2dev.serio);
57+
}
58+
59+
mutex_unlock(&psmouse_smbus_mutex);
60+
}
61+
62+
static void psmouse_smbus_detach_i2c_client(struct i2c_client *client)
63+
{
64+
struct psmouse_smbus_dev *smbdev;
65+
66+
mutex_lock(&psmouse_smbus_mutex);
67+
68+
list_for_each_entry(smbdev, &psmouse_smbus_list, node) {
69+
if (smbdev->client == client) {
70+
psmouse_dbg(smbdev->psmouse,
71+
"Marking SMBus companion %s as gone\n",
72+
dev_name(&smbdev->client->dev));
73+
smbdev->client = NULL;
74+
smbdev->dead = true;
75+
serio_rescan(smbdev->psmouse->ps2dev.serio);
76+
}
77+
}
78+
79+
kfree(client->dev.platform_data);
80+
client->dev.platform_data = NULL;
81+
82+
mutex_unlock(&psmouse_smbus_mutex);
83+
}
84+
85+
static int psmouse_smbus_notifier_call(struct notifier_block *nb,
86+
unsigned long action, void *data)
87+
{
88+
struct device *dev = data;
89+
90+
switch (action) {
91+
case BUS_NOTIFY_ADD_DEVICE:
92+
if (dev->type == &i2c_adapter_type)
93+
psmouse_smbus_check_adapter(to_i2c_adapter(dev));
94+
break;
95+
96+
case BUS_NOTIFY_REMOVED_DEVICE:
97+
if (dev->type != &i2c_adapter_type)
98+
psmouse_smbus_detach_i2c_client(to_i2c_client(dev));
99+
break;
100+
}
101+
102+
return 0;
103+
}
104+
105+
static struct notifier_block psmouse_smbus_notifier = {
106+
.notifier_call = psmouse_smbus_notifier_call,
107+
};
108+
109+
static psmouse_ret_t psmouse_smbus_process_byte(struct psmouse *psmouse)
110+
{
111+
return PSMOUSE_FULL_PACKET;
112+
}
113+
114+
static int psmouse_smbus_reconnect(struct psmouse *psmouse)
115+
{
116+
psmouse_deactivate(psmouse);
117+
118+
return 0;
119+
}
120+
121+
struct psmouse_smbus_removal_work {
122+
struct work_struct work;
123+
struct i2c_client *client;
124+
};
125+
126+
static void psmouse_smbus_remove_i2c_device(struct work_struct *work)
127+
{
128+
struct psmouse_smbus_removal_work *rwork =
129+
container_of(work, struct psmouse_smbus_removal_work, work);
130+
131+
dev_dbg(&rwork->client->dev, "destroying SMBus companion device\n");
132+
i2c_unregister_device(rwork->client);
133+
134+
kfree(rwork);
135+
}
136+
137+
/*
138+
* This schedules removal of SMBus companion device. We have to do
139+
* it in a separate tread to avoid deadlocking on psmouse_mutex in
140+
* case the device has a trackstick (which is also driven by psmouse).
141+
*
142+
* Note that this may be racing with i2c adapter removal, but we
143+
* can't do anything about that: i2c automatically destroys clients
144+
* attached to an adapter that is being removed. This has to be
145+
* fixed in i2c core.
146+
*/
147+
static void psmouse_smbus_schedule_remove(struct i2c_client *client)
148+
{
149+
struct psmouse_smbus_removal_work *rwork;
150+
151+
rwork = kzalloc(sizeof(*rwork), GFP_KERNEL);
152+
if (rwork) {
153+
INIT_WORK(&rwork->work, psmouse_smbus_remove_i2c_device);
154+
rwork->client = client;
155+
156+
schedule_work(&rwork->work);
157+
}
158+
}
159+
160+
static void psmouse_smbus_disconnect(struct psmouse *psmouse)
161+
{
162+
struct psmouse_smbus_dev *smbdev = psmouse->private;
163+
164+
mutex_lock(&psmouse_smbus_mutex);
165+
list_del(&smbdev->node);
166+
mutex_unlock(&psmouse_smbus_mutex);
167+
168+
if (smbdev->client) {
169+
psmouse_dbg(smbdev->psmouse,
170+
"posting removal request for SMBus companion %s\n",
171+
dev_name(&smbdev->client->dev));
172+
psmouse_smbus_schedule_remove(smbdev->client);
173+
}
174+
175+
kfree(smbdev);
176+
psmouse->private = NULL;
177+
}
178+
179+
static int psmouse_smbus_create_companion(struct device *dev, void *data)
180+
{
181+
struct psmouse_smbus_dev *smbdev = data;
182+
unsigned short addr_list[] = { smbdev->board.addr, I2C_CLIENT_END };
183+
struct i2c_adapter *adapter;
184+
185+
adapter = i2c_verify_adapter(dev);
186+
if (!adapter)
187+
return 0;
188+
189+
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
190+
return 0;
191+
192+
smbdev->client = i2c_new_probed_device(adapter, &smbdev->board,
193+
addr_list, NULL);
194+
if (!smbdev->client)
195+
return 0;
196+
197+
/* We have our(?) device, stop iterating i2c bus. */
198+
return 1;
199+
}
200+
201+
void psmouse_smbus_cleanup(struct psmouse *psmouse)
202+
{
203+
struct psmouse_smbus_dev *smbdev, *tmp;
204+
205+
mutex_lock(&psmouse_smbus_mutex);
206+
207+
list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
208+
if (psmouse == smbdev->psmouse) {
209+
list_del(&smbdev->node);
210+
kfree(smbdev);
211+
}
212+
}
213+
214+
mutex_unlock(&psmouse_smbus_mutex);
215+
}
216+
217+
int psmouse_smbus_init(struct psmouse *psmouse,
218+
const struct i2c_board_info *board,
219+
const void *pdata, size_t pdata_size,
220+
bool leave_breadcrumbs)
221+
{
222+
struct psmouse_smbus_dev *smbdev;
223+
int error;
224+
225+
smbdev = kzalloc(sizeof(*smbdev), GFP_KERNEL);
226+
if (!smbdev)
227+
return -ENOMEM;
228+
229+
smbdev->psmouse = psmouse;
230+
smbdev->board = *board;
231+
232+
smbdev->board.platform_data = kmemdup(pdata, pdata_size, GFP_KERNEL);
233+
if (!smbdev->board.platform_data) {
234+
kfree(smbdev);
235+
return -ENOMEM;
236+
}
237+
238+
psmouse->private = smbdev;
239+
psmouse->protocol_handler = psmouse_smbus_process_byte;
240+
psmouse->reconnect = psmouse_smbus_reconnect;
241+
psmouse->fast_reconnect = psmouse_smbus_reconnect;
242+
psmouse->disconnect = psmouse_smbus_disconnect;
243+
psmouse->resync_time = 0;
244+
245+
psmouse_deactivate(psmouse);
246+
247+
mutex_lock(&psmouse_smbus_mutex);
248+
list_add_tail(&smbdev->node, &psmouse_smbus_list);
249+
mutex_unlock(&psmouse_smbus_mutex);
250+
251+
/* Bind to already existing adapters right away */
252+
error = i2c_for_each_dev(smbdev, psmouse_smbus_create_companion);
253+
254+
if (smbdev->client) {
255+
/* We have our companion device */
256+
return 0;
257+
}
258+
259+
/*
260+
* If we did not create i2c device we will not need platform
261+
* data even if we are leaving breadcrumbs.
262+
*/
263+
kfree(smbdev->board.platform_data);
264+
smbdev->board.platform_data = NULL;
265+
266+
if (error < 0 || !leave_breadcrumbs) {
267+
mutex_lock(&psmouse_smbus_mutex);
268+
list_del(&smbdev->node);
269+
mutex_unlock(&psmouse_smbus_mutex);
270+
271+
kfree(smbdev);
272+
}
273+
274+
return error < 0 ? error : -EAGAIN;
275+
}
276+
277+
int __init psmouse_smbus_module_init(void)
278+
{
279+
int error;
280+
281+
error = bus_register_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
282+
if (error) {
283+
pr_err("failed to register i2c bus notifier: %d\n", error);
284+
return error;
285+
}
286+
287+
return 0;
288+
}
289+
290+
void psmouse_smbus_module_exit(void)
291+
{
292+
bus_unregister_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
293+
flush_scheduled_work();
294+
}

‎drivers/input/mouse/psmouse.h‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,5 +209,34 @@ static struct psmouse_attribute psmouse_attr_##_name = { \
209209
&(psmouse)->ps2dev.serio->dev, \
210210
psmouse_fmt(format), ##__VA_ARGS__)
211211

212+
#ifdef CONFIG_MOUSE_PS2_SMBUS
213+
214+
int psmouse_smbus_module_init(void);
215+
void psmouse_smbus_module_exit(void);
216+
217+
struct i2c_board_info;
218+
219+
int psmouse_smbus_init(struct psmouse *psmouse,
220+
const struct i2c_board_info *board,
221+
const void *pdata, size_t pdata_size,
222+
bool leave_breadcrumbs);
223+
void psmouse_smbus_cleanup(struct psmouse *psmouse);
224+
225+
#else /* !CONFIG_MOUSE_PS2_SMBUS */
226+
227+
static inline int psmouse_smbus_module_init(void)
228+
{
229+
return 0;
230+
}
231+
232+
static inline void psmouse_smbus_module_exit(void)
233+
{
234+
}
235+
236+
static inline void psmouse_smbus_cleanup(struct psmouse *psmouse)
237+
{
238+
}
239+
240+
#endif /* CONFIG_MOUSE_PS2_SMBUS */
212241

213242
#endif /* _PSMOUSE_H */

0 commit comments

Comments
 (0)