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 void psmouse_smbus_check_adapter(struct i2c_adapter *adapter)
31 struct psmouse_smbus_dev *smbdev;
33 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
36 mutex_lock(&psmouse_smbus_mutex);
38 list_for_each_entry(smbdev, &psmouse_smbus_list, node) {
46 * Here would be a good place to check if device is actually
47 * present, but it seems that SMBus will not respond unless we
48 * fully reset PS/2 connection. So cross our fingers, and try
49 * to switch over, hopefully our system will not have too many
50 * "host notify" I2C adapters.
52 psmouse_dbg(smbdev->psmouse,
53 "SMBus candidate adapter appeared, triggering rescan\n");
54 serio_rescan(smbdev->psmouse->ps2dev.serio);
57 mutex_unlock(&psmouse_smbus_mutex);
60 static void psmouse_smbus_detach_i2c_client(struct i2c_client *client)
62 struct psmouse_smbus_dev *smbdev, *tmp;
64 mutex_lock(&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);
87 mutex_unlock(&psmouse_smbus_mutex);
90 static int psmouse_smbus_notifier_call(struct notifier_block *nb,
91 unsigned long action, void *data)
93 struct device *dev = data;
96 case BUS_NOTIFY_ADD_DEVICE:
97 if (dev->type == &i2c_adapter_type)
98 psmouse_smbus_check_adapter(to_i2c_adapter(dev));
101 case BUS_NOTIFY_REMOVED_DEVICE:
102 if (dev->type == &i2c_client_type)
103 psmouse_smbus_detach_i2c_client(to_i2c_client(dev));
110 static struct notifier_block psmouse_smbus_notifier = {
111 .notifier_call = psmouse_smbus_notifier_call,
114 static psmouse_ret_t psmouse_smbus_process_byte(struct psmouse *psmouse)
116 return PSMOUSE_FULL_PACKET;
119 static int psmouse_smbus_reconnect(struct psmouse *psmouse)
121 struct psmouse_smbus_dev *smbdev = psmouse->private;
123 if (smbdev->need_deactivate)
124 psmouse_deactivate(psmouse);
129 struct psmouse_smbus_removal_work {
130 struct work_struct work;
131 struct i2c_client *client;
134 static void psmouse_smbus_remove_i2c_device(struct work_struct *work)
136 struct psmouse_smbus_removal_work *rwork =
137 container_of(work, struct psmouse_smbus_removal_work, work);
139 dev_dbg(&rwork->client->dev, "destroying SMBus companion device\n");
140 i2c_unregister_device(rwork->client);
146 * This schedules removal of SMBus companion device. We have to do
147 * it in a separate tread to avoid deadlocking on psmouse_mutex in
148 * case the device has a trackstick (which is also driven by psmouse).
150 * Note that this may be racing with i2c adapter removal, but we
151 * can't do anything about that: i2c automatically destroys clients
152 * attached to an adapter that is being removed. This has to be
155 static void psmouse_smbus_schedule_remove(struct i2c_client *client)
157 struct psmouse_smbus_removal_work *rwork;
159 rwork = kzalloc(sizeof(*rwork), GFP_KERNEL);
161 INIT_WORK(&rwork->work, psmouse_smbus_remove_i2c_device);
162 rwork->client = client;
164 schedule_work(&rwork->work);
168 static void psmouse_smbus_disconnect(struct psmouse *psmouse)
170 struct psmouse_smbus_dev *smbdev = psmouse->private;
172 mutex_lock(&psmouse_smbus_mutex);
175 list_del(&smbdev->node);
179 device_link_remove(&smbdev->client->dev,
180 &psmouse->ps2dev.serio->dev);
181 psmouse_dbg(smbdev->psmouse,
182 "posting removal request for SMBus companion %s\n",
183 dev_name(&smbdev->client->dev));
184 psmouse_smbus_schedule_remove(smbdev->client);
187 mutex_unlock(&psmouse_smbus_mutex);
189 psmouse->private = NULL;
192 static int psmouse_smbus_create_companion(struct device *dev, void *data)
194 struct psmouse_smbus_dev *smbdev = data;
195 unsigned short addr_list[] = { smbdev->board.addr, I2C_CLIENT_END };
196 struct i2c_adapter *adapter;
197 struct i2c_client *client;
199 adapter = i2c_verify_adapter(dev);
203 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
206 client = i2c_new_scanned_device(adapter, &smbdev->board,
211 /* We have our(?) device, stop iterating i2c bus. */
212 smbdev->client = client;
216 void psmouse_smbus_cleanup(struct psmouse *psmouse)
218 struct psmouse_smbus_dev *smbdev, *tmp;
220 mutex_lock(&psmouse_smbus_mutex);
222 list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
223 if (psmouse == smbdev->psmouse) {
224 list_del(&smbdev->node);
229 mutex_unlock(&psmouse_smbus_mutex);
232 int psmouse_smbus_init(struct psmouse *psmouse,
233 const struct i2c_board_info *board,
234 const void *pdata, size_t pdata_size,
235 bool need_deactivate,
236 bool leave_breadcrumbs)
238 struct psmouse_smbus_dev *smbdev;
241 smbdev = kzalloc(sizeof(*smbdev), GFP_KERNEL);
245 smbdev->psmouse = psmouse;
246 smbdev->board = *board;
247 smbdev->need_deactivate = need_deactivate;
250 smbdev->board.platform_data = kmemdup(pdata, pdata_size,
252 if (!smbdev->board.platform_data) {
259 psmouse_deactivate(psmouse);
261 psmouse->private = smbdev;
262 psmouse->protocol_handler = psmouse_smbus_process_byte;
263 psmouse->reconnect = psmouse_smbus_reconnect;
264 psmouse->fast_reconnect = psmouse_smbus_reconnect;
265 psmouse->disconnect = psmouse_smbus_disconnect;
266 psmouse->resync_time = 0;
268 mutex_lock(&psmouse_smbus_mutex);
269 list_add_tail(&smbdev->node, &psmouse_smbus_list);
270 mutex_unlock(&psmouse_smbus_mutex);
272 /* Bind to already existing adapters right away */
273 error = i2c_for_each_dev(smbdev, psmouse_smbus_create_companion);
275 if (smbdev->client) {
276 /* We have our companion device */
277 if (!device_link_add(&smbdev->client->dev,
278 &psmouse->ps2dev.serio->dev,
280 psmouse_warn(psmouse,
281 "failed to set up link with iSMBus companion %s\n",
282 dev_name(&smbdev->client->dev));
287 * If we did not create i2c device we will not need platform
288 * data even if we are leaving breadcrumbs.
290 kfree(smbdev->board.platform_data);
291 smbdev->board.platform_data = NULL;
293 if (error < 0 || !leave_breadcrumbs) {
294 mutex_lock(&psmouse_smbus_mutex);
295 list_del(&smbdev->node);
296 mutex_unlock(&psmouse_smbus_mutex);
301 return error < 0 ? error : -EAGAIN;
304 int __init psmouse_smbus_module_init(void)
308 error = bus_register_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
310 pr_err("failed to register i2c bus notifier: %d\n", error);
317 void psmouse_smbus_module_exit(void)
319 bus_unregister_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
320 flush_scheduled_work();