1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2017 Red Hat, Inc
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/libps2.h>
11 #include <linux/i2c.h>
12 #include <linux/serio.h>
13 #include <linux/slab.h>
14 #include <linux/workqueue.h>
17 struct psmouse_smbus_dev
{
18 struct i2c_board_info board
;
19 struct psmouse
*psmouse
;
20 struct i2c_client
*client
;
21 struct list_head node
;
26 static LIST_HEAD(psmouse_smbus_list
);
27 static DEFINE_MUTEX(psmouse_smbus_mutex
);
29 static struct workqueue_struct
*psmouse_smbus_wq
;
31 static void psmouse_smbus_check_adapter(struct i2c_adapter
*adapter
)
33 struct psmouse_smbus_dev
*smbdev
;
35 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_HOST_NOTIFY
))
38 guard(mutex
)(&psmouse_smbus_mutex
);
40 list_for_each_entry(smbdev
, &psmouse_smbus_list
, node
) {
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.
54 psmouse_dbg(smbdev
->psmouse
,
55 "SMBus candidate adapter appeared, triggering rescan\n");
56 serio_rescan(smbdev
->psmouse
->ps2dev
.serio
);
60 static void psmouse_smbus_detach_i2c_client(struct i2c_client
*client
)
62 struct psmouse_smbus_dev
*smbdev
, *tmp
;
64 guard(mutex
)(&psmouse_smbus_mutex
);
66 list_for_each_entry_safe(smbdev
, tmp
, &psmouse_smbus_list
, node
) {
67 if (smbdev
->client
!= client
)
70 kfree(client
->dev
.platform_data
);
71 client
->dev
.platform_data
= NULL
;
74 psmouse_dbg(smbdev
->psmouse
,
75 "Marking SMBus companion %s as gone\n",
76 dev_name(&smbdev
->client
->dev
));
78 device_link_remove(&smbdev
->client
->dev
,
79 &smbdev
->psmouse
->ps2dev
.serio
->dev
);
80 serio_rescan(smbdev
->psmouse
->ps2dev
.serio
);
82 list_del(&smbdev
->node
);
88 static int psmouse_smbus_notifier_call(struct notifier_block
*nb
,
89 unsigned long action
, void *data
)
91 struct device
*dev
= data
;
94 case BUS_NOTIFY_ADD_DEVICE
:
95 if (dev
->type
== &i2c_adapter_type
)
96 psmouse_smbus_check_adapter(to_i2c_adapter(dev
));
99 case BUS_NOTIFY_REMOVED_DEVICE
:
100 if (dev
->type
== &i2c_client_type
)
101 psmouse_smbus_detach_i2c_client(to_i2c_client(dev
));
108 static struct notifier_block psmouse_smbus_notifier
= {
109 .notifier_call
= psmouse_smbus_notifier_call
,
112 static psmouse_ret_t
psmouse_smbus_process_byte(struct psmouse
*psmouse
)
114 return PSMOUSE_FULL_PACKET
;
117 static int psmouse_smbus_reconnect(struct psmouse
*psmouse
)
119 struct psmouse_smbus_dev
*smbdev
= psmouse
->private;
121 if (smbdev
->need_deactivate
)
122 psmouse_deactivate(psmouse
);
127 struct psmouse_smbus_removal_work
{
128 struct work_struct work
;
129 struct i2c_client
*client
;
132 static void psmouse_smbus_remove_i2c_device(struct work_struct
*work
)
134 struct psmouse_smbus_removal_work
*rwork
=
135 container_of(work
, struct psmouse_smbus_removal_work
, work
);
137 dev_dbg(&rwork
->client
->dev
, "destroying SMBus companion device\n");
138 i2c_unregister_device(rwork
->client
);
144 * This schedules removal of SMBus companion device. We have to do
145 * it in a separate tread to avoid deadlocking on psmouse_mutex in
146 * case the device has a trackstick (which is also driven by psmouse).
148 * Note that this may be racing with i2c adapter removal, but we
149 * can't do anything about that: i2c automatically destroys clients
150 * attached to an adapter that is being removed. This has to be
153 static void psmouse_smbus_schedule_remove(struct i2c_client
*client
)
155 struct psmouse_smbus_removal_work
*rwork
;
157 rwork
= kzalloc(sizeof(*rwork
), GFP_KERNEL
);
159 INIT_WORK(&rwork
->work
, psmouse_smbus_remove_i2c_device
);
160 rwork
->client
= client
;
162 queue_work(psmouse_smbus_wq
, &rwork
->work
);
166 static void psmouse_smbus_disconnect(struct psmouse
*psmouse
)
168 struct psmouse_smbus_dev
*smbdev
= psmouse
->private;
170 guard(mutex
)(&psmouse_smbus_mutex
);
173 list_del(&smbdev
->node
);
177 device_link_remove(&smbdev
->client
->dev
,
178 &psmouse
->ps2dev
.serio
->dev
);
179 psmouse_dbg(smbdev
->psmouse
,
180 "posting removal request for SMBus companion %s\n",
181 dev_name(&smbdev
->client
->dev
));
182 psmouse_smbus_schedule_remove(smbdev
->client
);
185 psmouse
->private = NULL
;
188 static int psmouse_smbus_create_companion(struct device
*dev
, void *data
)
190 struct psmouse_smbus_dev
*smbdev
= data
;
191 unsigned short addr_list
[] = { smbdev
->board
.addr
, I2C_CLIENT_END
};
192 struct i2c_adapter
*adapter
;
193 struct i2c_client
*client
;
195 adapter
= i2c_verify_adapter(dev
);
199 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_HOST_NOTIFY
))
202 client
= i2c_new_scanned_device(adapter
, &smbdev
->board
,
207 /* We have our(?) device, stop iterating i2c bus. */
208 smbdev
->client
= client
;
212 void psmouse_smbus_cleanup(struct psmouse
*psmouse
)
214 struct psmouse_smbus_dev
*smbdev
, *tmp
;
216 guard(mutex
)(&psmouse_smbus_mutex
);
218 list_for_each_entry_safe(smbdev
, tmp
, &psmouse_smbus_list
, node
) {
219 if (psmouse
== smbdev
->psmouse
) {
220 list_del(&smbdev
->node
);
226 int psmouse_smbus_init(struct psmouse
*psmouse
,
227 const struct i2c_board_info
*board
,
228 const void *pdata
, size_t pdata_size
,
229 bool need_deactivate
,
230 bool leave_breadcrumbs
)
232 struct psmouse_smbus_dev
*smbdev
;
235 smbdev
= kzalloc(sizeof(*smbdev
), GFP_KERNEL
);
239 smbdev
->psmouse
= psmouse
;
240 smbdev
->board
= *board
;
241 smbdev
->need_deactivate
= need_deactivate
;
244 smbdev
->board
.platform_data
= kmemdup(pdata
, pdata_size
,
246 if (!smbdev
->board
.platform_data
) {
253 psmouse_deactivate(psmouse
);
255 psmouse
->private = smbdev
;
256 psmouse
->protocol_handler
= psmouse_smbus_process_byte
;
257 psmouse
->reconnect
= psmouse_smbus_reconnect
;
258 psmouse
->fast_reconnect
= psmouse_smbus_reconnect
;
259 psmouse
->disconnect
= psmouse_smbus_disconnect
;
260 psmouse
->resync_time
= 0;
262 scoped_guard(mutex
, &psmouse_smbus_mutex
) {
263 list_add_tail(&smbdev
->node
, &psmouse_smbus_list
);
266 /* Bind to already existing adapters right away */
267 error
= i2c_for_each_dev(smbdev
, psmouse_smbus_create_companion
);
269 if (smbdev
->client
) {
270 /* We have our companion device */
271 if (!device_link_add(&smbdev
->client
->dev
,
272 &psmouse
->ps2dev
.serio
->dev
,
274 psmouse_warn(psmouse
,
275 "failed to set up link with iSMBus companion %s\n",
276 dev_name(&smbdev
->client
->dev
));
281 * If we did not create i2c device we will not need platform
282 * data even if we are leaving breadcrumbs.
284 kfree(smbdev
->board
.platform_data
);
285 smbdev
->board
.platform_data
= NULL
;
287 if (error
< 0 || !leave_breadcrumbs
) {
288 scoped_guard(mutex
, &psmouse_smbus_mutex
) {
289 list_del(&smbdev
->node
);
295 return error
< 0 ? error
: -EAGAIN
;
298 int __init
psmouse_smbus_module_init(void)
302 psmouse_smbus_wq
= alloc_workqueue("psmouse-smbus", 0, 0);
303 if (!psmouse_smbus_wq
)
306 error
= bus_register_notifier(&i2c_bus_type
, &psmouse_smbus_notifier
);
308 pr_err("failed to register i2c bus notifier: %d\n", error
);
309 destroy_workqueue(psmouse_smbus_wq
);
316 void psmouse_smbus_module_exit(void)
318 bus_unregister_notifier(&i2c_bus_type
, &psmouse_smbus_notifier
);
319 destroy_workqueue(psmouse_smbus_wq
);