2 * Copyright (c) 2017 Red Hat, Inc
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.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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;
28 static LIST_HEAD(psmouse_smbus_list);
29 static DEFINE_MUTEX(psmouse_smbus_mutex);
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 mutex_lock(&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);
59 mutex_unlock(&psmouse_smbus_mutex);
62 static void psmouse_smbus_detach_i2c_client(struct i2c_client *client)
64 struct psmouse_smbus_dev *smbdev, *tmp;
66 mutex_lock(&psmouse_smbus_mutex);
68 list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
69 if (smbdev->client != client)
72 kfree(client->dev.platform_data);
73 client->dev.platform_data = NULL;
76 psmouse_dbg(smbdev->psmouse,
77 "Marking SMBus companion %s as gone\n",
78 dev_name(&smbdev->client->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_adapter_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 psmouse_deactivate(psmouse);
126 struct psmouse_smbus_removal_work {
127 struct work_struct work;
128 struct i2c_client *client;
131 static void psmouse_smbus_remove_i2c_device(struct work_struct *work)
133 struct psmouse_smbus_removal_work *rwork =
134 container_of(work, struct psmouse_smbus_removal_work, work);
136 dev_dbg(&rwork->client->dev, "destroying SMBus companion device\n");
137 i2c_unregister_device(rwork->client);
143 * This schedules removal of SMBus companion device. We have to do
144 * it in a separate tread to avoid deadlocking on psmouse_mutex in
145 * case the device has a trackstick (which is also driven by psmouse).
147 * Note that this may be racing with i2c adapter removal, but we
148 * can't do anything about that: i2c automatically destroys clients
149 * attached to an adapter that is being removed. This has to be
152 static void psmouse_smbus_schedule_remove(struct i2c_client *client)
154 struct psmouse_smbus_removal_work *rwork;
156 rwork = kzalloc(sizeof(*rwork), GFP_KERNEL);
158 INIT_WORK(&rwork->work, psmouse_smbus_remove_i2c_device);
159 rwork->client = client;
161 schedule_work(&rwork->work);
165 static void psmouse_smbus_disconnect(struct psmouse *psmouse)
167 struct psmouse_smbus_dev *smbdev = psmouse->private;
169 mutex_lock(&psmouse_smbus_mutex);
172 list_del(&smbdev->node);
176 psmouse_dbg(smbdev->psmouse,
177 "posting removal request for SMBus companion %s\n",
178 dev_name(&smbdev->client->dev));
179 psmouse_smbus_schedule_remove(smbdev->client);
182 mutex_unlock(&psmouse_smbus_mutex);
184 psmouse->private = NULL;
187 static int psmouse_smbus_create_companion(struct device *dev, void *data)
189 struct psmouse_smbus_dev *smbdev = data;
190 unsigned short addr_list[] = { smbdev->board.addr, I2C_CLIENT_END };
191 struct i2c_adapter *adapter;
193 adapter = i2c_verify_adapter(dev);
197 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
200 smbdev->client = i2c_new_probed_device(adapter, &smbdev->board,
205 /* We have our(?) device, stop iterating i2c bus. */
209 void psmouse_smbus_cleanup(struct psmouse *psmouse)
211 struct psmouse_smbus_dev *smbdev, *tmp;
213 mutex_lock(&psmouse_smbus_mutex);
215 list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
216 if (psmouse == smbdev->psmouse) {
217 list_del(&smbdev->node);
222 mutex_unlock(&psmouse_smbus_mutex);
225 int psmouse_smbus_init(struct psmouse *psmouse,
226 const struct i2c_board_info *board,
227 const void *pdata, size_t pdata_size,
228 bool leave_breadcrumbs)
230 struct psmouse_smbus_dev *smbdev;
233 smbdev = kzalloc(sizeof(*smbdev), GFP_KERNEL);
237 smbdev->psmouse = psmouse;
238 smbdev->board = *board;
240 smbdev->board.platform_data = kmemdup(pdata, pdata_size, GFP_KERNEL);
241 if (!smbdev->board.platform_data) {
246 psmouse->private = smbdev;
247 psmouse->protocol_handler = psmouse_smbus_process_byte;
248 psmouse->reconnect = psmouse_smbus_reconnect;
249 psmouse->fast_reconnect = psmouse_smbus_reconnect;
250 psmouse->disconnect = psmouse_smbus_disconnect;
251 psmouse->resync_time = 0;
253 psmouse_deactivate(psmouse);
255 mutex_lock(&psmouse_smbus_mutex);
256 list_add_tail(&smbdev->node, &psmouse_smbus_list);
257 mutex_unlock(&psmouse_smbus_mutex);
259 /* Bind to already existing adapters right away */
260 error = i2c_for_each_dev(smbdev, psmouse_smbus_create_companion);
262 if (smbdev->client) {
263 /* We have our companion device */
268 * If we did not create i2c device we will not need platform
269 * data even if we are leaving breadcrumbs.
271 kfree(smbdev->board.platform_data);
272 smbdev->board.platform_data = NULL;
274 if (error < 0 || !leave_breadcrumbs) {
275 mutex_lock(&psmouse_smbus_mutex);
276 list_del(&smbdev->node);
277 mutex_unlock(&psmouse_smbus_mutex);
282 return error < 0 ? error : -EAGAIN;
285 int __init psmouse_smbus_module_init(void)
289 error = bus_register_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
291 pr_err("failed to register i2c bus notifier: %d\n", error);
298 void psmouse_smbus_module_exit(void)
300 bus_unregister_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
301 flush_scheduled_work();