Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2017 Red Hat, Inc
0004  */
0005 
0006 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/libps2.h>
0011 #include <linux/i2c.h>
0012 #include <linux/serio.h>
0013 #include <linux/slab.h>
0014 #include <linux/workqueue.h>
0015 #include "psmouse.h"
0016 
0017 struct psmouse_smbus_dev {
0018     struct i2c_board_info board;
0019     struct psmouse *psmouse;
0020     struct i2c_client *client;
0021     struct list_head node;
0022     bool dead;
0023     bool need_deactivate;
0024 };
0025 
0026 static LIST_HEAD(psmouse_smbus_list);
0027 static DEFINE_MUTEX(psmouse_smbus_mutex);
0028 
0029 static struct workqueue_struct *psmouse_smbus_wq;
0030 
0031 static void psmouse_smbus_check_adapter(struct i2c_adapter *adapter)
0032 {
0033     struct psmouse_smbus_dev *smbdev;
0034 
0035     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
0036         return;
0037 
0038     mutex_lock(&psmouse_smbus_mutex);
0039 
0040     list_for_each_entry(smbdev, &psmouse_smbus_list, node) {
0041         if (smbdev->dead)
0042             continue;
0043 
0044         if (smbdev->client)
0045             continue;
0046 
0047         /*
0048          * Here would be a good place to check if device is actually
0049          * present, but it seems that SMBus will not respond unless we
0050          * fully reset PS/2 connection.  So cross our fingers, and try
0051          * to switch over, hopefully our system will not have too many
0052          * "host notify" I2C adapters.
0053          */
0054         psmouse_dbg(smbdev->psmouse,
0055                 "SMBus candidate adapter appeared, triggering rescan\n");
0056         serio_rescan(smbdev->psmouse->ps2dev.serio);
0057     }
0058 
0059     mutex_unlock(&psmouse_smbus_mutex);
0060 }
0061 
0062 static void psmouse_smbus_detach_i2c_client(struct i2c_client *client)
0063 {
0064     struct psmouse_smbus_dev *smbdev, *tmp;
0065 
0066     mutex_lock(&psmouse_smbus_mutex);
0067 
0068     list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
0069         if (smbdev->client != client)
0070             continue;
0071 
0072         kfree(client->dev.platform_data);
0073         client->dev.platform_data = NULL;
0074 
0075         if (!smbdev->dead) {
0076             psmouse_dbg(smbdev->psmouse,
0077                     "Marking SMBus companion %s as gone\n",
0078                     dev_name(&smbdev->client->dev));
0079             smbdev->dead = true;
0080             device_link_remove(&smbdev->client->dev,
0081                        &smbdev->psmouse->ps2dev.serio->dev);
0082             serio_rescan(smbdev->psmouse->ps2dev.serio);
0083         } else {
0084             list_del(&smbdev->node);
0085             kfree(smbdev);
0086         }
0087     }
0088 
0089     mutex_unlock(&psmouse_smbus_mutex);
0090 }
0091 
0092 static int psmouse_smbus_notifier_call(struct notifier_block *nb,
0093                        unsigned long action, void *data)
0094 {
0095     struct device *dev = data;
0096 
0097     switch (action) {
0098     case BUS_NOTIFY_ADD_DEVICE:
0099         if (dev->type == &i2c_adapter_type)
0100             psmouse_smbus_check_adapter(to_i2c_adapter(dev));
0101         break;
0102 
0103     case BUS_NOTIFY_REMOVED_DEVICE:
0104         if (dev->type == &i2c_client_type)
0105             psmouse_smbus_detach_i2c_client(to_i2c_client(dev));
0106         break;
0107     }
0108 
0109     return 0;
0110 }
0111 
0112 static struct notifier_block psmouse_smbus_notifier = {
0113     .notifier_call = psmouse_smbus_notifier_call,
0114 };
0115 
0116 static psmouse_ret_t psmouse_smbus_process_byte(struct psmouse *psmouse)
0117 {
0118     return PSMOUSE_FULL_PACKET;
0119 }
0120 
0121 static int psmouse_smbus_reconnect(struct psmouse *psmouse)
0122 {
0123     struct psmouse_smbus_dev *smbdev = psmouse->private;
0124 
0125     if (smbdev->need_deactivate)
0126         psmouse_deactivate(psmouse);
0127 
0128     return 0;
0129 }
0130 
0131 struct psmouse_smbus_removal_work {
0132     struct work_struct work;
0133     struct i2c_client *client;
0134 };
0135 
0136 static void psmouse_smbus_remove_i2c_device(struct work_struct *work)
0137 {
0138     struct psmouse_smbus_removal_work *rwork =
0139         container_of(work, struct psmouse_smbus_removal_work, work);
0140 
0141     dev_dbg(&rwork->client->dev, "destroying SMBus companion device\n");
0142     i2c_unregister_device(rwork->client);
0143 
0144     kfree(rwork);
0145 }
0146 
0147 /*
0148  * This schedules removal of SMBus companion device. We have to do
0149  * it in a separate tread to avoid deadlocking on psmouse_mutex in
0150  * case the device has a trackstick (which is also driven by psmouse).
0151  *
0152  * Note that this may be racing with i2c adapter removal, but we
0153  * can't do anything about that: i2c automatically destroys clients
0154  * attached to an adapter that is being removed. This has to be
0155  * fixed in i2c core.
0156  */
0157 static void psmouse_smbus_schedule_remove(struct i2c_client *client)
0158 {
0159     struct psmouse_smbus_removal_work *rwork;
0160 
0161     rwork = kzalloc(sizeof(*rwork), GFP_KERNEL);
0162     if (rwork) {
0163         INIT_WORK(&rwork->work, psmouse_smbus_remove_i2c_device);
0164         rwork->client = client;
0165 
0166         queue_work(psmouse_smbus_wq, &rwork->work);
0167     }
0168 }
0169 
0170 static void psmouse_smbus_disconnect(struct psmouse *psmouse)
0171 {
0172     struct psmouse_smbus_dev *smbdev = psmouse->private;
0173 
0174     mutex_lock(&psmouse_smbus_mutex);
0175 
0176     if (smbdev->dead) {
0177         list_del(&smbdev->node);
0178         kfree(smbdev);
0179     } else {
0180         smbdev->dead = true;
0181         device_link_remove(&smbdev->client->dev,
0182                    &psmouse->ps2dev.serio->dev);
0183         psmouse_dbg(smbdev->psmouse,
0184                 "posting removal request for SMBus companion %s\n",
0185                 dev_name(&smbdev->client->dev));
0186         psmouse_smbus_schedule_remove(smbdev->client);
0187     }
0188 
0189     mutex_unlock(&psmouse_smbus_mutex);
0190 
0191     psmouse->private = NULL;
0192 }
0193 
0194 static int psmouse_smbus_create_companion(struct device *dev, void *data)
0195 {
0196     struct psmouse_smbus_dev *smbdev = data;
0197     unsigned short addr_list[] = { smbdev->board.addr, I2C_CLIENT_END };
0198     struct i2c_adapter *adapter;
0199     struct i2c_client *client;
0200 
0201     adapter = i2c_verify_adapter(dev);
0202     if (!adapter)
0203         return 0;
0204 
0205     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
0206         return 0;
0207 
0208     client = i2c_new_scanned_device(adapter, &smbdev->board,
0209                     addr_list, NULL);
0210     if (IS_ERR(client))
0211         return 0;
0212 
0213     /* We have our(?) device, stop iterating i2c bus. */
0214     smbdev->client = client;
0215     return 1;
0216 }
0217 
0218 void psmouse_smbus_cleanup(struct psmouse *psmouse)
0219 {
0220     struct psmouse_smbus_dev *smbdev, *tmp;
0221 
0222     mutex_lock(&psmouse_smbus_mutex);
0223 
0224     list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
0225         if (psmouse == smbdev->psmouse) {
0226             list_del(&smbdev->node);
0227             kfree(smbdev);
0228         }
0229     }
0230 
0231     mutex_unlock(&psmouse_smbus_mutex);
0232 }
0233 
0234 int psmouse_smbus_init(struct psmouse *psmouse,
0235                const struct i2c_board_info *board,
0236                const void *pdata, size_t pdata_size,
0237                bool need_deactivate,
0238                bool leave_breadcrumbs)
0239 {
0240     struct psmouse_smbus_dev *smbdev;
0241     int error;
0242 
0243     smbdev = kzalloc(sizeof(*smbdev), GFP_KERNEL);
0244     if (!smbdev)
0245         return -ENOMEM;
0246 
0247     smbdev->psmouse = psmouse;
0248     smbdev->board = *board;
0249     smbdev->need_deactivate = need_deactivate;
0250 
0251     if (pdata) {
0252         smbdev->board.platform_data = kmemdup(pdata, pdata_size,
0253                               GFP_KERNEL);
0254         if (!smbdev->board.platform_data) {
0255             kfree(smbdev);
0256             return -ENOMEM;
0257         }
0258     }
0259 
0260     if (need_deactivate)
0261         psmouse_deactivate(psmouse);
0262 
0263     psmouse->private = smbdev;
0264     psmouse->protocol_handler = psmouse_smbus_process_byte;
0265     psmouse->reconnect = psmouse_smbus_reconnect;
0266     psmouse->fast_reconnect = psmouse_smbus_reconnect;
0267     psmouse->disconnect = psmouse_smbus_disconnect;
0268     psmouse->resync_time = 0;
0269 
0270     mutex_lock(&psmouse_smbus_mutex);
0271     list_add_tail(&smbdev->node, &psmouse_smbus_list);
0272     mutex_unlock(&psmouse_smbus_mutex);
0273 
0274     /* Bind to already existing adapters right away */
0275     error = i2c_for_each_dev(smbdev, psmouse_smbus_create_companion);
0276 
0277     if (smbdev->client) {
0278         /* We have our companion device */
0279         if (!device_link_add(&smbdev->client->dev,
0280                      &psmouse->ps2dev.serio->dev,
0281                      DL_FLAG_STATELESS))
0282             psmouse_warn(psmouse,
0283                      "failed to set up link with iSMBus companion %s\n",
0284                      dev_name(&smbdev->client->dev));
0285         return 0;
0286     }
0287 
0288     /*
0289      * If we did not create i2c device we will not need platform
0290      * data even if we are leaving breadcrumbs.
0291      */
0292     kfree(smbdev->board.platform_data);
0293     smbdev->board.platform_data = NULL;
0294 
0295     if (error < 0 || !leave_breadcrumbs) {
0296         mutex_lock(&psmouse_smbus_mutex);
0297         list_del(&smbdev->node);
0298         mutex_unlock(&psmouse_smbus_mutex);
0299 
0300         kfree(smbdev);
0301     }
0302 
0303     return error < 0 ? error : -EAGAIN;
0304 }
0305 
0306 int __init psmouse_smbus_module_init(void)
0307 {
0308     int error;
0309 
0310     psmouse_smbus_wq = alloc_workqueue("psmouse-smbus", 0, 0);
0311     if (!psmouse_smbus_wq)
0312         return -ENOMEM;
0313 
0314     error = bus_register_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
0315     if (error) {
0316         pr_err("failed to register i2c bus notifier: %d\n", error);
0317         destroy_workqueue(psmouse_smbus_wq);
0318         return error;
0319     }
0320 
0321     return 0;
0322 }
0323 
0324 void psmouse_smbus_module_exit(void)
0325 {
0326     bus_unregister_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
0327     destroy_workqueue(psmouse_smbus_wq);
0328 }