1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017 Red Hat, Inc
4  */
5 
6 #define pr_fmt(fmt)		KBUILD_MODNAME ": " fmt
7 
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>
16 #include "psmouse.h"
17 
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;
23 	bool dead;
24 	bool need_deactivate;
25 };
26 
27 static LIST_HEAD(psmouse_smbus_list);
28 static DEFINE_MUTEX(psmouse_smbus_mutex);
29 
30 static struct workqueue_struct *psmouse_smbus_wq;
31 
32 static void psmouse_smbus_check_adapter(struct i2c_adapter *adapter)
33 {
34 	struct psmouse_smbus_dev *smbdev;
35 
36 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
37 		return;
38 
39 	mutex_lock(&psmouse_smbus_mutex);
40 
41 	list_for_each_entry(smbdev, &psmouse_smbus_list, node) {
42 		if (smbdev->dead)
43 			continue;
44 
45 		if (smbdev->client)
46 			continue;
47 
48 		/*
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.
54 		 */
55 		psmouse_dbg(smbdev->psmouse,
56 			    "SMBus candidate adapter appeared, triggering rescan\n");
57 		serio_rescan(smbdev->psmouse->ps2dev.serio);
58 	}
59 
60 	mutex_unlock(&psmouse_smbus_mutex);
61 }
62 
63 static void psmouse_smbus_detach_i2c_client(struct i2c_client *client)
64 {
65 	struct psmouse_smbus_dev *smbdev, *tmp;
66 
67 	mutex_lock(&psmouse_smbus_mutex);
68 
69 	list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
70 		if (smbdev->client != client)
71 			continue;
72 
73 		kfree(client->dev.platform_data);
74 		client->dev.platform_data = NULL;
75 
76 		if (!smbdev->dead) {
77 			psmouse_dbg(smbdev->psmouse,
78 				    "Marking SMBus companion %s as gone\n",
79 				    dev_name(&smbdev->client->dev));
80 			smbdev->dead = true;
81 			device_link_remove(&smbdev->client->dev,
82 					   &smbdev->psmouse->ps2dev.serio->dev);
83 			serio_rescan(smbdev->psmouse->ps2dev.serio);
84 		} else {
85 			list_del(&smbdev->node);
86 			kfree(smbdev);
87 		}
88 	}
89 
90 	mutex_unlock(&psmouse_smbus_mutex);
91 }
92 
93 static int psmouse_smbus_notifier_call(struct notifier_block *nb,
94 				       unsigned long action, void *data)
95 {
96 	struct device *dev = data;
97 
98 	switch (action) {
99 	case BUS_NOTIFY_ADD_DEVICE:
100 		if (dev->type == &i2c_adapter_type)
101 			psmouse_smbus_check_adapter(to_i2c_adapter(dev));
102 		break;
103 
104 	case BUS_NOTIFY_REMOVED_DEVICE:
105 		if (dev->type == &i2c_client_type)
106 			psmouse_smbus_detach_i2c_client(to_i2c_client(dev));
107 		break;
108 	}
109 
110 	return 0;
111 }
112 
113 static struct notifier_block psmouse_smbus_notifier = {
114 	.notifier_call = psmouse_smbus_notifier_call,
115 };
116 
117 static psmouse_ret_t psmouse_smbus_process_byte(struct psmouse *psmouse)
118 {
119 	return PSMOUSE_FULL_PACKET;
120 }
121 
122 static void psmouse_activate_smbus_mode(struct psmouse_smbus_dev *smbdev)
123 {
124 	if (smbdev->need_deactivate) {
125 		psmouse_deactivate(smbdev->psmouse);
126 		/* Give the device time to switch into SMBus mode */
127 		msleep(30);
128 	}
129 }
130 
131 static int psmouse_smbus_reconnect(struct psmouse *psmouse)
132 {
133 	psmouse_activate_smbus_mode(psmouse->private);
134 	return 0;
135 }
136 
137 struct psmouse_smbus_removal_work {
138 	struct work_struct work;
139 	struct i2c_client *client;
140 };
141 
142 static void psmouse_smbus_remove_i2c_device(struct work_struct *work)
143 {
144 	struct psmouse_smbus_removal_work *rwork =
145 		container_of(work, struct psmouse_smbus_removal_work, work);
146 
147 	dev_dbg(&rwork->client->dev, "destroying SMBus companion device\n");
148 	i2c_unregister_device(rwork->client);
149 
150 	kfree(rwork);
151 }
152 
153 /*
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).
157  *
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
161  * fixed in i2c core.
162  */
163 static void psmouse_smbus_schedule_remove(struct i2c_client *client)
164 {
165 	struct psmouse_smbus_removal_work *rwork;
166 
167 	rwork = kzalloc(sizeof(*rwork), GFP_KERNEL);
168 	if (rwork) {
169 		INIT_WORK(&rwork->work, psmouse_smbus_remove_i2c_device);
170 		rwork->client = client;
171 
172 		queue_work(psmouse_smbus_wq, &rwork->work);
173 	}
174 }
175 
176 static void psmouse_smbus_disconnect(struct psmouse *psmouse)
177 {
178 	struct psmouse_smbus_dev *smbdev = psmouse->private;
179 
180 	mutex_lock(&psmouse_smbus_mutex);
181 
182 	if (smbdev->dead) {
183 		list_del(&smbdev->node);
184 		kfree(smbdev);
185 	} else {
186 		smbdev->dead = true;
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);
193 	}
194 
195 	mutex_unlock(&psmouse_smbus_mutex);
196 
197 	psmouse->private = NULL;
198 }
199 
200 static int psmouse_smbus_create_companion(struct device *dev, void *data)
201 {
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;
206 
207 	adapter = i2c_verify_adapter(dev);
208 	if (!adapter)
209 		return 0;
210 
211 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
212 		return 0;
213 
214 	client = i2c_new_scanned_device(adapter, &smbdev->board,
215 					addr_list, NULL);
216 	if (IS_ERR(client))
217 		return 0;
218 
219 	/* We have our(?) device, stop iterating i2c bus. */
220 	smbdev->client = client;
221 	return 1;
222 }
223 
224 void psmouse_smbus_cleanup(struct psmouse *psmouse)
225 {
226 	struct psmouse_smbus_dev *smbdev, *tmp;
227 
228 	mutex_lock(&psmouse_smbus_mutex);
229 
230 	list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
231 		if (psmouse == smbdev->psmouse) {
232 			list_del(&smbdev->node);
233 			kfree(smbdev);
234 		}
235 	}
236 
237 	mutex_unlock(&psmouse_smbus_mutex);
238 }
239 
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)
245 {
246 	struct psmouse_smbus_dev *smbdev;
247 	int error;
248 
249 	smbdev = kzalloc(sizeof(*smbdev), GFP_KERNEL);
250 	if (!smbdev)
251 		return -ENOMEM;
252 
253 	smbdev->psmouse = psmouse;
254 	smbdev->board = *board;
255 	smbdev->need_deactivate = need_deactivate;
256 
257 	if (pdata) {
258 		smbdev->board.platform_data = kmemdup(pdata, pdata_size,
259 						      GFP_KERNEL);
260 		if (!smbdev->board.platform_data) {
261 			kfree(smbdev);
262 			return -ENOMEM;
263 		}
264 	}
265 
266 	psmouse_activate_smbus_mode(smbdev);
267 
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;
274 
275 	mutex_lock(&psmouse_smbus_mutex);
276 	list_add_tail(&smbdev->node, &psmouse_smbus_list);
277 	mutex_unlock(&psmouse_smbus_mutex);
278 
279 	/* Bind to already existing adapters right away */
280 	error = i2c_for_each_dev(smbdev, psmouse_smbus_create_companion);
281 
282 	if (smbdev->client) {
283 		/* We have our companion device */
284 		if (!device_link_add(&smbdev->client->dev,
285 				     &psmouse->ps2dev.serio->dev,
286 				     DL_FLAG_STATELESS))
287 			psmouse_warn(psmouse,
288 				     "failed to set up link with iSMBus companion %s\n",
289 				     dev_name(&smbdev->client->dev));
290 		return 0;
291 	}
292 
293 	/*
294 	 * If we did not create i2c device we will not need platform
295 	 * data even if we are leaving breadcrumbs.
296 	 */
297 	kfree(smbdev->board.platform_data);
298 	smbdev->board.platform_data = NULL;
299 
300 	if (error < 0 || !leave_breadcrumbs) {
301 		mutex_lock(&psmouse_smbus_mutex);
302 		list_del(&smbdev->node);
303 		mutex_unlock(&psmouse_smbus_mutex);
304 
305 		kfree(smbdev);
306 	}
307 
308 	return error < 0 ? error : -EAGAIN;
309 }
310 
311 int __init psmouse_smbus_module_init(void)
312 {
313 	int error;
314 
315 	psmouse_smbus_wq = alloc_workqueue("psmouse-smbus", 0, 0);
316 	if (!psmouse_smbus_wq)
317 		return -ENOMEM;
318 
319 	error = bus_register_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
320 	if (error) {
321 		pr_err("failed to register i2c bus notifier: %d\n", error);
322 		destroy_workqueue(psmouse_smbus_wq);
323 		return error;
324 	}
325 
326 	return 0;
327 }
328 
329 void psmouse_smbus_module_exit(void)
330 {
331 	bus_unregister_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
332 	destroy_workqueue(psmouse_smbus_wq);
333 }
334