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/delay.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/libps2.h>
12 #include <linux/i2c.h>
13 #include <linux/serio.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
18 struct psmouse_smbus_dev {
19 struct i2c_board_info board;
20 struct psmouse *psmouse;
21 struct i2c_client *client;
22 struct list_head node;
27 static LIST_HEAD(psmouse_smbus_list);
28 static DEFINE_MUTEX(psmouse_smbus_mutex);
30 static struct workqueue_struct *psmouse_smbus_wq;
32 static void psmouse_smbus_check_adapter(struct i2c_adapter *adapter)
34 struct psmouse_smbus_dev *smbdev;
36 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
39 mutex_lock(&psmouse_smbus_mutex);
41 list_for_each_entry(smbdev, &psmouse_smbus_list, node) {
49 * Here would be a good place to check if device is actually
50 * present, but it seems that SMBus will not respond unless we
51 * fully reset PS/2 connection. So cross our fingers, and try
52 * to switch over, hopefully our system will not have too many
53 * "host notify" I2C adapters.
55 psmouse_dbg(smbdev->psmouse,
56 "SMBus candidate adapter appeared, triggering rescan\n");
57 serio_rescan(smbdev->psmouse->ps2dev.serio);
60 mutex_unlock(&psmouse_smbus_mutex);
63 static void psmouse_smbus_detach_i2c_client(struct i2c_client *client)
65 struct psmouse_smbus_dev *smbdev, *tmp;
67 mutex_lock(&psmouse_smbus_mutex);
69 list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
70 if (smbdev->client != client)
73 kfree(client->dev.platform_data);
74 client->dev.platform_data = NULL;
77 psmouse_dbg(smbdev->psmouse,
78 "Marking SMBus companion %s as gone\n",
79 dev_name(&smbdev->client->dev));
81 device_link_remove(&smbdev->client->dev,
82 &smbdev->psmouse->ps2dev.serio->dev);
83 serio_rescan(smbdev->psmouse->ps2dev.serio);
85 list_del(&smbdev->node);
90 mutex_unlock(&psmouse_smbus_mutex);
93 static int psmouse_smbus_notifier_call(struct notifier_block *nb,
94 unsigned long action, void *data)
96 struct device *dev = data;
99 case BUS_NOTIFY_ADD_DEVICE:
100 if (dev->type == &i2c_adapter_type)
101 psmouse_smbus_check_adapter(to_i2c_adapter(dev));
104 case BUS_NOTIFY_REMOVED_DEVICE:
105 if (dev->type == &i2c_client_type)
106 psmouse_smbus_detach_i2c_client(to_i2c_client(dev));
113 static struct notifier_block psmouse_smbus_notifier = {
114 .notifier_call = psmouse_smbus_notifier_call,
117 static psmouse_ret_t psmouse_smbus_process_byte(struct psmouse *psmouse)
119 return PSMOUSE_FULL_PACKET;
122 static void psmouse_activate_smbus_mode(struct psmouse_smbus_dev *smbdev)
124 if (smbdev->need_deactivate) {
125 psmouse_deactivate(smbdev->psmouse);
126 /* Give the device time to switch into SMBus mode */
131 static int psmouse_smbus_reconnect(struct psmouse *psmouse)
133 psmouse_activate_smbus_mode(psmouse->private);
137 struct psmouse_smbus_removal_work {
138 struct work_struct work;
139 struct i2c_client *client;
142 static void psmouse_smbus_remove_i2c_device(struct work_struct *work)
144 struct psmouse_smbus_removal_work *rwork =
145 container_of(work, struct psmouse_smbus_removal_work, work);
147 dev_dbg(&rwork->client->dev, "destroying SMBus companion device\n");
148 i2c_unregister_device(rwork->client);
154 * This schedules removal of SMBus companion device. We have to do
155 * it in a separate tread to avoid deadlocking on psmouse_mutex in
156 * case the device has a trackstick (which is also driven by psmouse).
158 * Note that this may be racing with i2c adapter removal, but we
159 * can't do anything about that: i2c automatically destroys clients
160 * attached to an adapter that is being removed. This has to be
163 static void psmouse_smbus_schedule_remove(struct i2c_client *client)
165 struct psmouse_smbus_removal_work *rwork;
167 rwork = kzalloc(sizeof(*rwork), GFP_KERNEL);
169 INIT_WORK(&rwork->work, psmouse_smbus_remove_i2c_device);
170 rwork->client = client;
172 queue_work(psmouse_smbus_wq, &rwork->work);
176 static void psmouse_smbus_disconnect(struct psmouse *psmouse)
178 struct psmouse_smbus_dev *smbdev = psmouse->private;
180 mutex_lock(&psmouse_smbus_mutex);
183 list_del(&smbdev->node);
187 device_link_remove(&smbdev->client->dev,
188 &psmouse->ps2dev.serio->dev);
189 psmouse_dbg(smbdev->psmouse,
190 "posting removal request for SMBus companion %s\n",
191 dev_name(&smbdev->client->dev));
192 psmouse_smbus_schedule_remove(smbdev->client);
195 mutex_unlock(&psmouse_smbus_mutex);
197 psmouse->private = NULL;
200 static int psmouse_smbus_create_companion(struct device *dev, void *data)
202 struct psmouse_smbus_dev *smbdev = data;
203 unsigned short addr_list[] = { smbdev->board.addr, I2C_CLIENT_END };
204 struct i2c_adapter *adapter;
205 struct i2c_client *client;
207 adapter = i2c_verify_adapter(dev);
211 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
214 client = i2c_new_scanned_device(adapter, &smbdev->board,
219 /* We have our(?) device, stop iterating i2c bus. */
220 smbdev->client = client;
224 void psmouse_smbus_cleanup(struct psmouse *psmouse)
226 struct psmouse_smbus_dev *smbdev, *tmp;
228 mutex_lock(&psmouse_smbus_mutex);
230 list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
231 if (psmouse == smbdev->psmouse) {
232 list_del(&smbdev->node);
237 mutex_unlock(&psmouse_smbus_mutex);
240 int psmouse_smbus_init(struct psmouse *psmouse,
241 const struct i2c_board_info *board,
242 const void *pdata, size_t pdata_size,
243 bool need_deactivate,
244 bool leave_breadcrumbs)
246 struct psmouse_smbus_dev *smbdev;
249 smbdev = kzalloc(sizeof(*smbdev), GFP_KERNEL);
253 smbdev->psmouse = psmouse;
254 smbdev->board = *board;
255 smbdev->need_deactivate = need_deactivate;
258 smbdev->board.platform_data = kmemdup(pdata, pdata_size,
260 if (!smbdev->board.platform_data) {
266 psmouse_activate_smbus_mode(smbdev);
268 psmouse->private = smbdev;
269 psmouse->protocol_handler = psmouse_smbus_process_byte;
270 psmouse->reconnect = psmouse_smbus_reconnect;
271 psmouse->fast_reconnect = psmouse_smbus_reconnect;
272 psmouse->disconnect = psmouse_smbus_disconnect;
273 psmouse->resync_time = 0;
275 mutex_lock(&psmouse_smbus_mutex);
276 list_add_tail(&smbdev->node, &psmouse_smbus_list);
277 mutex_unlock(&psmouse_smbus_mutex);
279 /* Bind to already existing adapters right away */
280 error = i2c_for_each_dev(smbdev, psmouse_smbus_create_companion);
282 if (smbdev->client) {
283 /* We have our companion device */
284 if (!device_link_add(&smbdev->client->dev,
285 &psmouse->ps2dev.serio->dev,
287 psmouse_warn(psmouse,
288 "failed to set up link with iSMBus companion %s\n",
289 dev_name(&smbdev->client->dev));
294 * If we did not create i2c device we will not need platform
295 * data even if we are leaving breadcrumbs.
297 kfree(smbdev->board.platform_data);
298 smbdev->board.platform_data = NULL;
300 if (error < 0 || !leave_breadcrumbs) {
301 mutex_lock(&psmouse_smbus_mutex);
302 list_del(&smbdev->node);
303 mutex_unlock(&psmouse_smbus_mutex);
308 return error < 0 ? error : -EAGAIN;
311 int __init psmouse_smbus_module_init(void)
315 psmouse_smbus_wq = alloc_workqueue("psmouse-smbus", 0, 0);
316 if (!psmouse_smbus_wq)
319 error = bus_register_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
321 pr_err("failed to register i2c bus notifier: %d\n", error);
322 destroy_workqueue(psmouse_smbus_wq);
329 void psmouse_smbus_module_exit(void)
331 bus_unregister_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
332 destroy_workqueue(psmouse_smbus_wq);