^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2017 Red Hat, Inc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/libps2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/serio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "psmouse.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct psmouse_smbus_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct i2c_board_info board;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct psmouse *psmouse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) bool dead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) bool need_deactivate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static LIST_HEAD(psmouse_smbus_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static DEFINE_MUTEX(psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static void psmouse_smbus_check_adapter(struct i2c_adapter *adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct psmouse_smbus_dev *smbdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) mutex_lock(&psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) list_for_each_entry(smbdev, &psmouse_smbus_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (smbdev->dead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (smbdev->client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Here would be a good place to check if device is actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * present, but it seems that SMBus will not respond unless we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * fully reset PS/2 connection. So cross our fingers, and try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * to switch over, hopefully our system will not have too many
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * "host notify" I2C adapters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) psmouse_dbg(smbdev->psmouse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "SMBus candidate adapter appeared, triggering rescan\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) serio_rescan(smbdev->psmouse->ps2dev.serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) mutex_unlock(&psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static void psmouse_smbus_detach_i2c_client(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct psmouse_smbus_dev *smbdev, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) mutex_lock(&psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (smbdev->client != client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) kfree(client->dev.platform_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) client->dev.platform_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!smbdev->dead) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) psmouse_dbg(smbdev->psmouse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) "Marking SMBus companion %s as gone\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dev_name(&smbdev->client->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) smbdev->dead = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) serio_rescan(smbdev->psmouse->ps2dev.serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) list_del(&smbdev->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) kfree(smbdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) mutex_unlock(&psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static int psmouse_smbus_notifier_call(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) unsigned long action, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct device *dev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) case BUS_NOTIFY_ADD_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (dev->type == &i2c_adapter_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) psmouse_smbus_check_adapter(to_i2c_adapter(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) case BUS_NOTIFY_REMOVED_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (dev->type == &i2c_client_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) psmouse_smbus_detach_i2c_client(to_i2c_client(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static struct notifier_block psmouse_smbus_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .notifier_call = psmouse_smbus_notifier_call,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static psmouse_ret_t psmouse_smbus_process_byte(struct psmouse *psmouse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return PSMOUSE_FULL_PACKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int psmouse_smbus_reconnect(struct psmouse *psmouse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct psmouse_smbus_dev *smbdev = psmouse->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (smbdev->need_deactivate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) psmouse_deactivate(psmouse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct psmouse_smbus_removal_work {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void psmouse_smbus_remove_i2c_device(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct psmouse_smbus_removal_work *rwork =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) container_of(work, struct psmouse_smbus_removal_work, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) dev_dbg(&rwork->client->dev, "destroying SMBus companion device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) i2c_unregister_device(rwork->client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) kfree(rwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * This schedules removal of SMBus companion device. We have to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * it in a separate tread to avoid deadlocking on psmouse_mutex in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * case the device has a trackstick (which is also driven by psmouse).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * Note that this may be racing with i2c adapter removal, but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * can't do anything about that: i2c automatically destroys clients
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * attached to an adapter that is being removed. This has to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * fixed in i2c core.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static void psmouse_smbus_schedule_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct psmouse_smbus_removal_work *rwork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) rwork = kzalloc(sizeof(*rwork), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (rwork) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) INIT_WORK(&rwork->work, psmouse_smbus_remove_i2c_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) rwork->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) schedule_work(&rwork->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void psmouse_smbus_disconnect(struct psmouse *psmouse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct psmouse_smbus_dev *smbdev = psmouse->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) mutex_lock(&psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (smbdev->dead) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) list_del(&smbdev->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) kfree(smbdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) smbdev->dead = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) psmouse_dbg(smbdev->psmouse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) "posting removal request for SMBus companion %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) dev_name(&smbdev->client->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) psmouse_smbus_schedule_remove(smbdev->client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) mutex_unlock(&psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) psmouse->private = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static int psmouse_smbus_create_companion(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct psmouse_smbus_dev *smbdev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) unsigned short addr_list[] = { smbdev->board.addr, I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct i2c_adapter *adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) adapter = i2c_verify_adapter(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (!adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) client = i2c_new_scanned_device(adapter, &smbdev->board,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) addr_list, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (IS_ERR(client))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /* We have our(?) device, stop iterating i2c bus. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) smbdev->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) void psmouse_smbus_cleanup(struct psmouse *psmouse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct psmouse_smbus_dev *smbdev, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) mutex_lock(&psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (psmouse == smbdev->psmouse) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) list_del(&smbdev->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) kfree(smbdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) mutex_unlock(&psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int psmouse_smbus_init(struct psmouse *psmouse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) const struct i2c_board_info *board,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) const void *pdata, size_t pdata_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) bool need_deactivate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) bool leave_breadcrumbs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct psmouse_smbus_dev *smbdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) smbdev = kzalloc(sizeof(*smbdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (!smbdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) smbdev->psmouse = psmouse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) smbdev->board = *board;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) smbdev->need_deactivate = need_deactivate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) smbdev->board.platform_data = kmemdup(pdata, pdata_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (!smbdev->board.platform_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) kfree(smbdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (need_deactivate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) psmouse_deactivate(psmouse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) psmouse->private = smbdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) psmouse->protocol_handler = psmouse_smbus_process_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) psmouse->reconnect = psmouse_smbus_reconnect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) psmouse->fast_reconnect = psmouse_smbus_reconnect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) psmouse->disconnect = psmouse_smbus_disconnect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) psmouse->resync_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) mutex_lock(&psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) list_add_tail(&smbdev->node, &psmouse_smbus_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) mutex_unlock(&psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /* Bind to already existing adapters right away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) error = i2c_for_each_dev(smbdev, psmouse_smbus_create_companion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (smbdev->client) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /* We have our companion device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * If we did not create i2c device we will not need platform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * data even if we are leaving breadcrumbs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) kfree(smbdev->board.platform_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) smbdev->board.platform_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (error < 0 || !leave_breadcrumbs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) mutex_lock(&psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) list_del(&smbdev->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) mutex_unlock(&psmouse_smbus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) kfree(smbdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return error < 0 ? error : -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int __init psmouse_smbus_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) error = bus_register_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) pr_err("failed to register i2c bus notifier: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) void psmouse_smbus_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) bus_unregister_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) flush_scheduled_work();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }