From 4f273959b850569253299987eee611927f048de7 Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Wed, 8 Jul 2015 00:22:03 +0300 Subject: mei: nfc: fix deadlock on shutdown/suspend path In function mei_nfc_host_exit mei_cl_remove_device cannot be called under the device mutex as device removing flow invokes the device driver remove handler that calls in turn to mei_cl_disable_device which naturally acquires the device mutex. Also remove mei_cl_bus_remove_devices which has the same issue, but is never executed as currently the only device on the mei client bus is NFC and a new device cannot be easily added till the bus revamp is completed. This fixes regression caused by commit be9b720a0ccb ("mei_phy: move all nfc logic from mei driver to nfc") Prior to this change the nfc driver remove handler called to no-op disable function while actual nfc device was disabled directly from the mei driver. Reported-by: Linus Torvalds Acked-by: Greg Kroah-Hartman Cc: Samuel Ortiz Signed-off-by: Tomas Winkler Signed-off-by: Linus Torvalds --- drivers/misc/mei/bus.c | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index 357b6ae4d207..458aa5a09c52 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -552,22 +552,6 @@ void mei_cl_bus_rx_event(struct mei_cl *cl) schedule_work(&device->event_work); } -void mei_cl_bus_remove_devices(struct mei_device *dev) -{ - struct mei_cl *cl, *next; - - mutex_lock(&dev->device_lock); - list_for_each_entry_safe(cl, next, &dev->device_list, device_link) { - if (cl->device) - mei_cl_remove_device(cl->device); - - list_del(&cl->device_link); - mei_cl_unlink(cl); - kfree(cl); - } - mutex_unlock(&dev->device_lock); -} - int __init mei_cl_bus_init(void) { return bus_register(&mei_cl_bus_type); -- cgit v1.2.3 From b37719c31f8448ba36abc218a96663b4a6c66eb6 Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Thu, 23 Jul 2015 15:08:33 +0300 Subject: mei: bus: fix drivers and devices names confusion In the mei bus layer there is use of different variables of driver and device types with no clear naming convention. There are generic struct device and struct driver, then mei_cl_{device, driver}, and finally mei_device which in this context serves as a bus device. The patch sets following naming convention: the variables of type struct device remains dev the variables of type struct driver remains drv the variables of type struct mei_cl_device are now cldev the variables of type struct mei_cl_driver are now cldrv the variables of type struct mei_device are now bus, in bus layer context Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus.c | 260 ++++++++++++++++++++++----------------------- drivers/misc/mei/mei_dev.h | 12 +-- drivers/misc/mei/nfc.c | 80 +++++++------- 3 files changed, 176 insertions(+), 176 deletions(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index 458aa5a09c52..18c37af22f07 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -32,22 +32,22 @@ static int mei_cl_device_match(struct device *dev, struct device_driver *drv) { - struct mei_cl_device *device = to_mei_cl_device(dev); - struct mei_cl_driver *driver = to_mei_cl_driver(drv); + struct mei_cl_device *cldev = to_mei_cl_device(dev); + struct mei_cl_driver *cldrv = to_mei_cl_driver(drv); const struct mei_cl_device_id *id; const uuid_le *uuid; const char *name; - if (!device) + if (!cldev) return 0; - uuid = mei_me_cl_uuid(device->me_cl); - name = device->name; + uuid = mei_me_cl_uuid(cldev->me_cl); + name = cldev->name; - if (!driver || !driver->id_table) + if (!cldrv || !cldrv->id_table) return 0; - id = driver->id_table; + id = cldrv->id_table; while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) { @@ -68,54 +68,54 @@ static int mei_cl_device_match(struct device *dev, struct device_driver *drv) static int mei_cl_device_probe(struct device *dev) { - struct mei_cl_device *device = to_mei_cl_device(dev); - struct mei_cl_driver *driver; + struct mei_cl_device *cldev = to_mei_cl_device(dev); + struct mei_cl_driver *cldrv; struct mei_cl_device_id id; - if (!device) + if (!cldev) return 0; - driver = to_mei_cl_driver(dev->driver); - if (!driver || !driver->probe) + cldrv = to_mei_cl_driver(dev->driver); + if (!cldrv || !cldrv->probe) return -ENODEV; dev_dbg(dev, "Device probe\n"); - strlcpy(id.name, device->name, sizeof(id.name)); + strlcpy(id.name, cldev->name, sizeof(id.name)); - return driver->probe(device, &id); + return cldrv->probe(cldev, &id); } static int mei_cl_device_remove(struct device *dev) { - struct mei_cl_device *device = to_mei_cl_device(dev); - struct mei_cl_driver *driver; + struct mei_cl_device *cldev = to_mei_cl_device(dev); + struct mei_cl_driver *cldrv; - if (!device || !dev->driver) + if (!cldev || !dev->driver) return 0; - if (device->event_cb) { - device->event_cb = NULL; - cancel_work_sync(&device->event_work); + if (cldev->event_cb) { + cldev->event_cb = NULL; + cancel_work_sync(&cldev->event_work); } - driver = to_mei_cl_driver(dev->driver); - if (!driver->remove) { + cldrv = to_mei_cl_driver(dev->driver); + if (!cldrv->remove) { dev->driver = NULL; return 0; } - return driver->remove(device); + return cldrv->remove(cldev); } static ssize_t name_show(struct device *dev, struct device_attribute *a, char *buf) { - struct mei_cl_device *device = to_mei_cl_device(dev); + struct mei_cl_device *cldev = to_mei_cl_device(dev); size_t len; - len = snprintf(buf, PAGE_SIZE, "%s", device->name); + len = snprintf(buf, PAGE_SIZE, "%s", cldev->name); return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; } @@ -124,8 +124,8 @@ static DEVICE_ATTR_RO(name); static ssize_t uuid_show(struct device *dev, struct device_attribute *a, char *buf) { - struct mei_cl_device *device = to_mei_cl_device(dev); - const uuid_le *uuid = mei_me_cl_uuid(device->me_cl); + struct mei_cl_device *cldev = to_mei_cl_device(dev); + const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); size_t len; len = snprintf(buf, PAGE_SIZE, "%pUl", uuid); @@ -137,12 +137,12 @@ static DEVICE_ATTR_RO(uuid); static ssize_t modalias_show(struct device *dev, struct device_attribute *a, char *buf) { - struct mei_cl_device *device = to_mei_cl_device(dev); - const uuid_le *uuid = mei_me_cl_uuid(device->me_cl); + struct mei_cl_device *cldev = to_mei_cl_device(dev); + const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); size_t len; len = snprintf(buf, PAGE_SIZE, "mei:%s:" MEI_CL_UUID_FMT ":", - device->name, MEI_CL_UUID_ARGS(uuid->b)); + cldev->name, MEI_CL_UUID_ARGS(uuid->b)); return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; } @@ -158,17 +158,17 @@ ATTRIBUTE_GROUPS(mei_cl_dev); static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env) { - struct mei_cl_device *device = to_mei_cl_device(dev); - const uuid_le *uuid = mei_me_cl_uuid(device->me_cl); + struct mei_cl_device *cldev = to_mei_cl_device(dev); + const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid)) return -ENOMEM; - if (add_uevent_var(env, "MEI_CL_NAME=%s", device->name)) + if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name)) return -ENOMEM; if (add_uevent_var(env, "MODALIAS=mei:%s:" MEI_CL_UUID_FMT ":", - device->name, MEI_CL_UUID_ARGS(uuid->b))) + cldev->name, MEI_CL_UUID_ARGS(uuid->b))) return -ENOMEM; return 0; @@ -185,121 +185,121 @@ static struct bus_type mei_cl_bus_type = { static void mei_cl_dev_release(struct device *dev) { - struct mei_cl_device *device = to_mei_cl_device(dev); + struct mei_cl_device *cldev = to_mei_cl_device(dev); - if (!device) + if (!cldev) return; - mei_me_cl_put(device->me_cl); - kfree(device); + mei_me_cl_put(cldev->me_cl); + kfree(cldev); } static struct device_type mei_cl_device_type = { .release = mei_cl_dev_release, }; -struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *dev, +struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *bus, uuid_le uuid) { struct mei_cl *cl; - list_for_each_entry(cl, &dev->device_list, device_link) { - if (cl->device && cl->device->me_cl && - !uuid_le_cmp(uuid, *mei_me_cl_uuid(cl->device->me_cl))) + list_for_each_entry(cl, &bus->device_list, device_link) { + if (cl->cldev && cl->cldev->me_cl && + !uuid_le_cmp(uuid, *mei_me_cl_uuid(cl->cldev->me_cl))) return cl; } return NULL; } -struct mei_cl_device *mei_cl_add_device(struct mei_device *dev, +struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, struct mei_me_client *me_cl, struct mei_cl *cl, char *name) { - struct mei_cl_device *device; + struct mei_cl_device *cldev; int status; - device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL); - if (!device) + cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL); + if (!cldev) return NULL; - device->me_cl = mei_me_cl_get(me_cl); - if (!device->me_cl) { - kfree(device); + cldev->me_cl = mei_me_cl_get(me_cl); + if (!cldev->me_cl) { + kfree(cldev); return NULL; } - device->cl = cl; - device->dev.parent = dev->dev; - device->dev.bus = &mei_cl_bus_type; - device->dev.type = &mei_cl_device_type; + cldev->cl = cl; + cldev->dev.parent = bus->dev; + cldev->dev.bus = &mei_cl_bus_type; + cldev->dev.type = &mei_cl_device_type; - strlcpy(device->name, name, sizeof(device->name)); + strlcpy(cldev->name, name, sizeof(cldev->name)); - dev_set_name(&device->dev, "mei:%s:%pUl", name, mei_me_cl_uuid(me_cl)); + dev_set_name(&cldev->dev, "mei:%s:%pUl", name, mei_me_cl_uuid(me_cl)); - status = device_register(&device->dev); + status = device_register(&cldev->dev); if (status) { - dev_err(dev->dev, "Failed to register MEI device\n"); - mei_me_cl_put(device->me_cl); - kfree(device); + dev_err(bus->dev, "Failed to register MEI device\n"); + mei_me_cl_put(cldev->me_cl); + kfree(cldev); return NULL; } - cl->device = device; + cl->cldev = cldev; - dev_dbg(&device->dev, "client %s registered\n", name); + dev_dbg(&cldev->dev, "client %s registered\n", name); - return device; + return cldev; } EXPORT_SYMBOL_GPL(mei_cl_add_device); -void mei_cl_remove_device(struct mei_cl_device *device) +void mei_cl_remove_device(struct mei_cl_device *cldev) { - device_unregister(&device->dev); + device_unregister(&cldev->dev); } EXPORT_SYMBOL_GPL(mei_cl_remove_device); -int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner) +int __mei_cl_driver_register(struct mei_cl_driver *cldrv, struct module *owner) { int err; - driver->driver.name = driver->name; - driver->driver.owner = owner; - driver->driver.bus = &mei_cl_bus_type; + cldrv->driver.name = cldrv->name; + cldrv->driver.owner = owner; + cldrv->driver.bus = &mei_cl_bus_type; - err = driver_register(&driver->driver); + err = driver_register(&cldrv->driver); if (err) return err; - pr_debug("mei: driver [%s] registered\n", driver->driver.name); + pr_debug("mei: driver [%s] registered\n", cldrv->driver.name); return 0; } EXPORT_SYMBOL_GPL(__mei_cl_driver_register); -void mei_cl_driver_unregister(struct mei_cl_driver *driver) +void mei_cl_driver_unregister(struct mei_cl_driver *cldrv) { - driver_unregister(&driver->driver); + driver_unregister(&cldrv->driver); - pr_debug("mei: driver [%s] unregistered\n", driver->driver.name); + pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name); } EXPORT_SYMBOL_GPL(mei_cl_driver_unregister); ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, bool blocking) { - struct mei_device *dev; + struct mei_device *bus; struct mei_cl_cb *cb = NULL; ssize_t rets; if (WARN_ON(!cl || !cl->dev)) return -ENODEV; - dev = cl->dev; + bus = cl->dev; - mutex_lock(&dev->device_lock); + mutex_lock(&bus->device_lock); if (!mei_cl_is_connected(cl)) { rets = -ENODEV; goto out; @@ -327,7 +327,7 @@ ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, rets = mei_cl_write(cl, cb, blocking); out: - mutex_unlock(&dev->device_lock); + mutex_unlock(&bus->device_lock); if (rets < 0) mei_io_cb_free(cb); @@ -336,7 +336,7 @@ out: ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length) { - struct mei_device *dev; + struct mei_device *bus; struct mei_cl_cb *cb; size_t r_length; ssize_t rets; @@ -344,9 +344,9 @@ ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length) if (WARN_ON(!cl || !cl->dev)) return -ENODEV; - dev = cl->dev; + bus = cl->dev; - mutex_lock(&dev->device_lock); + mutex_lock(&bus->device_lock); cb = mei_cl_read_cb(cl, NULL); if (cb) @@ -358,7 +358,7 @@ ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length) if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) { - mutex_unlock(&dev->device_lock); + mutex_unlock(&bus->device_lock); if (wait_event_interruptible(cl->rx_wait, (!list_empty(&cl->rd_completed)) || @@ -369,7 +369,7 @@ ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length) return -ERESTARTSYS; } - mutex_lock(&dev->device_lock); + mutex_lock(&bus->device_lock); if (!mei_cl_is_connected(cl)) { rets = -EBUSY; @@ -396,14 +396,14 @@ copy: free: mei_io_cb_free(cb); out: - mutex_unlock(&dev->device_lock); + mutex_unlock(&bus->device_lock); return rets; } -ssize_t mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length) +ssize_t mei_cl_send(struct mei_cl_device *cldev, u8 *buf, size_t length) { - struct mei_cl *cl = device->cl; + struct mei_cl *cl = cldev->cl; if (cl == NULL) return -ENODEV; @@ -412,9 +412,9 @@ ssize_t mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length) } EXPORT_SYMBOL_GPL(mei_cl_send); -ssize_t mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length) +ssize_t mei_cl_recv(struct mei_cl_device *cldev, u8 *buf, size_t length) { - struct mei_cl *cl = device->cl; + struct mei_cl *cl = cldev->cl; if (cl == NULL) return -ENODEV; @@ -425,108 +425,108 @@ EXPORT_SYMBOL_GPL(mei_cl_recv); static void mei_bus_event_work(struct work_struct *work) { - struct mei_cl_device *device; + struct mei_cl_device *cldev; - device = container_of(work, struct mei_cl_device, event_work); + cldev = container_of(work, struct mei_cl_device, event_work); - if (device->event_cb) - device->event_cb(device, device->events, device->event_context); + if (cldev->event_cb) + cldev->event_cb(cldev, cldev->events, cldev->event_context); - device->events = 0; + cldev->events = 0; /* Prepare for the next read */ - mei_cl_read_start(device->cl, 0, NULL); + mei_cl_read_start(cldev->cl, 0, NULL); } -int mei_cl_register_event_cb(struct mei_cl_device *device, +int mei_cl_register_event_cb(struct mei_cl_device *cldev, mei_cl_event_cb_t event_cb, void *context) { - if (device->event_cb) + if (cldev->event_cb) return -EALREADY; - device->events = 0; - device->event_cb = event_cb; - device->event_context = context; - INIT_WORK(&device->event_work, mei_bus_event_work); + cldev->events = 0; + cldev->event_cb = event_cb; + cldev->event_context = context; + INIT_WORK(&cldev->event_work, mei_bus_event_work); - mei_cl_read_start(device->cl, 0, NULL); + mei_cl_read_start(cldev->cl, 0, NULL); return 0; } EXPORT_SYMBOL_GPL(mei_cl_register_event_cb); -void *mei_cl_get_drvdata(const struct mei_cl_device *device) +void *mei_cl_get_drvdata(const struct mei_cl_device *cldev) { - return dev_get_drvdata(&device->dev); + return dev_get_drvdata(&cldev->dev); } EXPORT_SYMBOL_GPL(mei_cl_get_drvdata); -void mei_cl_set_drvdata(struct mei_cl_device *device, void *data) +void mei_cl_set_drvdata(struct mei_cl_device *cldev, void *data) { - dev_set_drvdata(&device->dev, data); + dev_set_drvdata(&cldev->dev, data); } EXPORT_SYMBOL_GPL(mei_cl_set_drvdata); -int mei_cl_enable_device(struct mei_cl_device *device) +int mei_cl_enable_device(struct mei_cl_device *cldev) { int err; - struct mei_device *dev; - struct mei_cl *cl = device->cl; + struct mei_device *bus; + struct mei_cl *cl = cldev->cl; if (cl == NULL) return -ENODEV; - dev = cl->dev; + bus = cl->dev; - mutex_lock(&dev->device_lock); + mutex_lock(&bus->device_lock); if (mei_cl_is_connected(cl)) { - mutex_unlock(&dev->device_lock); - dev_warn(dev->dev, "Already connected"); + mutex_unlock(&bus->device_lock); + dev_warn(bus->dev, "Already connected"); return -EBUSY; } - err = mei_cl_connect(cl, device->me_cl, NULL); + err = mei_cl_connect(cl, cldev->me_cl, NULL); if (err < 0) { - mutex_unlock(&dev->device_lock); - dev_err(dev->dev, "Could not connect to the ME client"); + mutex_unlock(&bus->device_lock); + dev_err(bus->dev, "Could not connect to the ME client"); return err; } - mutex_unlock(&dev->device_lock); + mutex_unlock(&bus->device_lock); - if (device->event_cb) - mei_cl_read_start(device->cl, 0, NULL); + if (cldev->event_cb) + mei_cl_read_start(cldev->cl, 0, NULL); return 0; } EXPORT_SYMBOL_GPL(mei_cl_enable_device); -int mei_cl_disable_device(struct mei_cl_device *device) +int mei_cl_disable_device(struct mei_cl_device *cldev) { int err; - struct mei_device *dev; - struct mei_cl *cl = device->cl; + struct mei_device *bus; + struct mei_cl *cl = cldev->cl; if (cl == NULL) return -ENODEV; - dev = cl->dev; + bus = cl->dev; - device->event_cb = NULL; + cldev->event_cb = NULL; - mutex_lock(&dev->device_lock); + mutex_lock(&bus->device_lock); if (!mei_cl_is_connected(cl)) { - dev_err(dev->dev, "Already disconnected"); + dev_err(bus->dev, "Already disconnected"); err = 0; goto out; } err = mei_cl_disconnect(cl); if (err < 0) { - dev_err(dev->dev, "Could not disconnect from the ME client"); + dev_err(bus->dev, "Could not disconnect from the ME client"); goto out; } @@ -534,7 +534,7 @@ int mei_cl_disable_device(struct mei_cl_device *device) mei_cl_flush_queues(cl, NULL); out: - mutex_unlock(&dev->device_lock); + mutex_unlock(&bus->device_lock); return err; } @@ -542,14 +542,14 @@ EXPORT_SYMBOL_GPL(mei_cl_disable_device); void mei_cl_bus_rx_event(struct mei_cl *cl) { - struct mei_cl_device *device = cl->device; + struct mei_cl_device *cldev = cl->cldev; - if (!device || !device->event_cb) + if (!cldev || !cldev->event_cb) return; - set_bit(MEI_CL_EVENT_RX, &device->events); + set_bit(MEI_CL_EVENT_RX, &cldev->events); - schedule_work(&device->event_work); + schedule_work(&cldev->event_work); } int __init mei_cl_bus_init(void) diff --git a/drivers/misc/mei/mei_dev.h b/drivers/misc/mei/mei_dev.h index 453f6a333b42..bc65fb42aea9 100644 --- a/drivers/misc/mei/mei_dev.h +++ b/drivers/misc/mei/mei_dev.h @@ -240,7 +240,7 @@ struct mei_cl_cb { * @rd_pending: pending read credits * @rd_completed: completed read * - * @device: device on the mei client bus + * @cldev: device on the mei client bus * @device_link: link to bus clients */ struct mei_cl { @@ -261,7 +261,7 @@ struct mei_cl { struct list_head rd_completed; /* MEI CL bus data */ - struct mei_cl_device *device; + struct mei_cl_device *cldev; struct list_head device_link; }; @@ -330,20 +330,20 @@ struct mei_hw_ops { /* MEI bus API*/ -struct mei_cl_device *mei_cl_add_device(struct mei_device *dev, +struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, struct mei_me_client *me_cl, struct mei_cl *cl, char *name); -void mei_cl_remove_device(struct mei_cl_device *device); +void mei_cl_remove_device(struct mei_cl_device *cldev); ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, bool blocking); ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length); void mei_cl_bus_rx_event(struct mei_cl *cl); -void mei_cl_bus_remove_devices(struct mei_device *dev); +void mei_cl_bus_remove_devices(struct mei_device *bus); int mei_cl_bus_init(void); void mei_cl_bus_exit(void); -struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *dev, uuid_le uuid); +struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *bus, uuid_le uuid); /** * enum mei_pg_event - power gating transition events diff --git a/drivers/misc/mei/nfc.c b/drivers/misc/mei/nfc.c index 290ef3037437..47aa1523d9e1 100644 --- a/drivers/misc/mei/nfc.c +++ b/drivers/misc/mei/nfc.c @@ -152,12 +152,12 @@ static void mei_nfc_free(struct mei_nfc_dev *ndev) static int mei_nfc_build_bus_name(struct mei_nfc_dev *ndev) { - struct mei_device *dev; + struct mei_device *bus; if (!ndev->cl) return -ENODEV; - dev = ndev->cl->dev; + bus = ndev->cl->dev; switch (ndev->vendor_id) { case MEI_NFC_VENDOR_INSIDE: @@ -167,7 +167,7 @@ static int mei_nfc_build_bus_name(struct mei_nfc_dev *ndev) return 0; default: - dev_err(dev->dev, "Unknown radio type 0x%x\n", + dev_err(bus->dev, "Unknown radio type 0x%x\n", ndev->radio_type); return -EINVAL; @@ -179,14 +179,14 @@ static int mei_nfc_build_bus_name(struct mei_nfc_dev *ndev) ndev->bus_name = "pn544"; return 0; default: - dev_err(dev->dev, "Unknown radio type 0x%x\n", + dev_err(bus->dev, "Unknown radio type 0x%x\n", ndev->radio_type); return -EINVAL; } default: - dev_err(dev->dev, "Unknown vendor ID 0x%x\n", + dev_err(bus->dev, "Unknown vendor ID 0x%x\n", ndev->vendor_id); return -EINVAL; @@ -197,7 +197,7 @@ static int mei_nfc_build_bus_name(struct mei_nfc_dev *ndev) static int mei_nfc_if_version(struct mei_nfc_dev *ndev) { - struct mei_device *dev; + struct mei_device *bus; struct mei_cl *cl; struct mei_nfc_cmd cmd; @@ -207,7 +207,7 @@ static int mei_nfc_if_version(struct mei_nfc_dev *ndev) int bytes_recv, ret; cl = ndev->cl_info; - dev = cl->dev; + bus = cl->dev; memset(&cmd, 0, sizeof(struct mei_nfc_cmd)); cmd.command = MEI_NFC_CMD_MAINTENANCE; @@ -216,7 +216,7 @@ static int mei_nfc_if_version(struct mei_nfc_dev *ndev) ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd), 1); if (ret < 0) { - dev_err(dev->dev, "Could not send IF version cmd\n"); + dev_err(bus->dev, "Could not send IF version cmd\n"); return ret; } @@ -230,7 +230,7 @@ static int mei_nfc_if_version(struct mei_nfc_dev *ndev) bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length); if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) { - dev_err(dev->dev, "Could not read IF version\n"); + dev_err(bus->dev, "Could not read IF version\n"); ret = -EIO; goto err; } @@ -248,7 +248,7 @@ err: static void mei_nfc_init(struct work_struct *work) { - struct mei_device *dev; + struct mei_device *bus; struct mei_cl_device *cldev; struct mei_nfc_dev *ndev; struct mei_cl *cl_info; @@ -257,57 +257,57 @@ static void mei_nfc_init(struct work_struct *work) ndev = container_of(work, struct mei_nfc_dev, init_work); cl_info = ndev->cl_info; - dev = cl_info->dev; + bus = cl_info->dev; - mutex_lock(&dev->device_lock); + mutex_lock(&bus->device_lock); /* check for valid client id */ - me_cl_info = mei_me_cl_by_uuid(dev, &mei_nfc_info_guid); + me_cl_info = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid); if (!me_cl_info) { - mutex_unlock(&dev->device_lock); - dev_info(dev->dev, "nfc: failed to find the info client\n"); + mutex_unlock(&bus->device_lock); + dev_info(bus->dev, "nfc: failed to find the info client\n"); goto err; } if (mei_cl_connect(cl_info, me_cl_info, NULL) < 0) { mei_me_cl_put(me_cl_info); - mutex_unlock(&dev->device_lock); - dev_err(dev->dev, "Could not connect to the NFC INFO ME client"); + mutex_unlock(&bus->device_lock); + dev_err(bus->dev, "Could not connect to the NFC INFO ME client"); goto err; } mei_me_cl_put(me_cl_info); - mutex_unlock(&dev->device_lock); + mutex_unlock(&bus->device_lock); if (mei_nfc_if_version(ndev) < 0) { - dev_err(dev->dev, "Could not get the NFC interface version"); + dev_err(bus->dev, "Could not get the NFC interface version"); goto err; } - dev_info(dev->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n", + dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n", ndev->fw_ivn, ndev->vendor_id, ndev->radio_type); - mutex_lock(&dev->device_lock); + mutex_lock(&bus->device_lock); if (mei_cl_disconnect(cl_info) < 0) { - mutex_unlock(&dev->device_lock); - dev_err(dev->dev, "Could not disconnect the NFC INFO ME client"); + mutex_unlock(&bus->device_lock); + dev_err(bus->dev, "Could not disconnect the NFC INFO ME client"); goto err; } - mutex_unlock(&dev->device_lock); + mutex_unlock(&bus->device_lock); if (mei_nfc_build_bus_name(ndev) < 0) { - dev_err(dev->dev, "Could not build the bus ID name\n"); + dev_err(bus->dev, "Could not build the bus ID name\n"); return; } - cldev = mei_cl_add_device(dev, ndev->me_cl, ndev->cl, + cldev = mei_cl_add_device(bus, ndev->me_cl, ndev->cl, ndev->bus_name); if (!cldev) { - dev_err(dev->dev, "Could not add the NFC device to the MEI bus\n"); + dev_err(bus->dev, "Could not add the NFC device to the MEI bus\n"); goto err; } @@ -318,14 +318,14 @@ static void mei_nfc_init(struct work_struct *work) return; err: - mutex_lock(&dev->device_lock); + mutex_lock(&bus->device_lock); mei_nfc_free(ndev); - mutex_unlock(&dev->device_lock); + mutex_unlock(&bus->device_lock); } -int mei_nfc_host_init(struct mei_device *dev, struct mei_me_client *me_cl) +int mei_nfc_host_init(struct mei_device *bus, struct mei_me_client *me_cl) { struct mei_nfc_dev *ndev; struct mei_cl *cl_info, *cl; @@ -335,7 +335,7 @@ int mei_nfc_host_init(struct mei_device *dev, struct mei_me_client *me_cl) /* in case of internal reset bail out * as the device is already setup */ - cl = mei_cl_bus_find_cl_by_uuid(dev, mei_nfc_guid); + cl = mei_cl_bus_find_cl_by_uuid(bus, mei_nfc_guid); if (cl) return 0; @@ -351,23 +351,23 @@ int mei_nfc_host_init(struct mei_device *dev, struct mei_me_client *me_cl) goto err; } - cl_info = mei_cl_alloc_linked(dev, MEI_HOST_CLIENT_ID_ANY); + cl_info = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY); if (IS_ERR(cl_info)) { ret = PTR_ERR(cl_info); goto err; } - list_add_tail(&cl_info->device_link, &dev->device_list); + list_add_tail(&cl_info->device_link, &bus->device_list); ndev->cl_info = cl_info; - cl = mei_cl_alloc_linked(dev, MEI_HOST_CLIENT_ID_ANY); + cl = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY); if (IS_ERR(cl)) { ret = PTR_ERR(cl); goto err; } - list_add_tail(&cl->device_link, &dev->device_list); + list_add_tail(&cl->device_link, &bus->device_list); ndev->cl = cl; @@ -382,17 +382,17 @@ err: return ret; } -void mei_nfc_host_exit(struct mei_device *dev) +void mei_nfc_host_exit(struct mei_device *bus) { struct mei_nfc_dev *ndev; struct mei_cl *cl; struct mei_cl_device *cldev; - cl = mei_cl_bus_find_cl_by_uuid(dev, mei_nfc_guid); + cl = mei_cl_bus_find_cl_by_uuid(bus, mei_nfc_guid); if (!cl) return; - cldev = cl->device; + cldev = cl->cldev; if (!cldev) return; @@ -407,9 +407,9 @@ void mei_nfc_host_exit(struct mei_device *dev) */ mei_cl_remove_device(cldev); - mutex_lock(&dev->device_lock); + mutex_lock(&bus->device_lock); mei_nfc_free(ndev); - mutex_unlock(&dev->device_lock); + mutex_unlock(&bus->device_lock); } -- cgit v1.2.3 From 6238299774377b12c3e24507b100b2687eb5ea32 Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Thu, 23 Jul 2015 15:08:35 +0300 Subject: mei: bus: move driver api functions at the start of the file To make the file more organize move mei client driver api to the start of the file and add Kdoc. There are no functional changes in this patch. Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus.c | 799 +++++++++++++++++++++++++++---------------------- 1 file changed, 444 insertions(+), 355 deletions(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index 18c37af22f07..6ea8a408f477 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -30,527 +30,616 @@ #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver) #define to_mei_cl_device(d) container_of(d, struct mei_cl_device, dev) -static int mei_cl_device_match(struct device *dev, struct device_driver *drv) +/** + * __mei_cl_send - internal client send (write) + * + * @cl: host client + * @buf: buffer to send + * @length: buffer length + * @blocking: wait for write completion + * + * Return: written size bytes or < 0 on error + */ +ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, + bool blocking) { - struct mei_cl_device *cldev = to_mei_cl_device(dev); - struct mei_cl_driver *cldrv = to_mei_cl_driver(drv); - const struct mei_cl_device_id *id; - const uuid_le *uuid; - const char *name; - - if (!cldev) - return 0; + struct mei_device *bus; + struct mei_cl_cb *cb = NULL; + ssize_t rets; - uuid = mei_me_cl_uuid(cldev->me_cl); - name = cldev->name; + if (WARN_ON(!cl || !cl->dev)) + return -ENODEV; - if (!cldrv || !cldrv->id_table) - return 0; + bus = cl->dev; - id = cldrv->id_table; + mutex_lock(&bus->device_lock); + if (!mei_cl_is_connected(cl)) { + rets = -ENODEV; + goto out; + } - while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) { + /* Check if we have an ME client device */ + if (!mei_me_cl_is_active(cl->me_cl)) { + rets = -ENOTTY; + goto out; + } - if (!uuid_le_cmp(*uuid, id->uuid)) { - if (id->name[0]) { - if (!strncmp(name, id->name, sizeof(id->name))) - return 1; - } else { - return 1; - } - } + if (length > mei_cl_mtu(cl)) { + rets = -EFBIG; + goto out; + } - id++; + cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL); + if (!cb) { + rets = -ENOMEM; + goto out; } - return 0; + memcpy(cb->buf.data, buf, length); + + rets = mei_cl_write(cl, cb, blocking); + +out: + mutex_unlock(&bus->device_lock); + if (rets < 0) + mei_io_cb_free(cb); + + return rets; } -static int mei_cl_device_probe(struct device *dev) +/** + * __mei_cl_recv - internal client receive (read) + * + * @cl: host client + * @buf: buffer to send + * @length: buffer length + * + * Return: read size in bytes of < 0 on error + */ +ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length) { - struct mei_cl_device *cldev = to_mei_cl_device(dev); - struct mei_cl_driver *cldrv; - struct mei_cl_device_id id; - - if (!cldev) - return 0; + struct mei_device *bus; + struct mei_cl_cb *cb; + size_t r_length; + ssize_t rets; - cldrv = to_mei_cl_driver(dev->driver); - if (!cldrv || !cldrv->probe) + if (WARN_ON(!cl || !cl->dev)) return -ENODEV; - dev_dbg(dev, "Device probe\n"); + bus = cl->dev; - strlcpy(id.name, cldev->name, sizeof(id.name)); + mutex_lock(&bus->device_lock); - return cldrv->probe(cldev, &id); -} + cb = mei_cl_read_cb(cl, NULL); + if (cb) + goto copy; -static int mei_cl_device_remove(struct device *dev) -{ - struct mei_cl_device *cldev = to_mei_cl_device(dev); - struct mei_cl_driver *cldrv; + rets = mei_cl_read_start(cl, length, NULL); + if (rets && rets != -EBUSY) + goto out; - if (!cldev || !dev->driver) - return 0; + /* wait on event only if there is no other waiter */ + if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) { - if (cldev->event_cb) { - cldev->event_cb = NULL; - cancel_work_sync(&cldev->event_work); - } + mutex_unlock(&bus->device_lock); - cldrv = to_mei_cl_driver(dev->driver); - if (!cldrv->remove) { - dev->driver = NULL; + if (wait_event_interruptible(cl->rx_wait, + (!list_empty(&cl->rd_completed)) || + (!mei_cl_is_connected(cl)))) { - return 0; + if (signal_pending(current)) + return -EINTR; + return -ERESTARTSYS; + } + + mutex_lock(&bus->device_lock); + + if (!mei_cl_is_connected(cl)) { + rets = -EBUSY; + goto out; + } } - return cldrv->remove(cldev); -} + cb = mei_cl_read_cb(cl, NULL); + if (!cb) { + rets = 0; + goto out; + } -static ssize_t name_show(struct device *dev, struct device_attribute *a, - char *buf) -{ - struct mei_cl_device *cldev = to_mei_cl_device(dev); - size_t len; +copy: + if (cb->status) { + rets = cb->status; + goto free; + } - len = snprintf(buf, PAGE_SIZE, "%s", cldev->name); + r_length = min_t(size_t, length, cb->buf_idx); + memcpy(buf, cb->buf.data, r_length); + rets = r_length; - return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; +free: + mei_io_cb_free(cb); +out: + mutex_unlock(&bus->device_lock); + + return rets; } -static DEVICE_ATTR_RO(name); -static ssize_t uuid_show(struct device *dev, struct device_attribute *a, - char *buf) +/** + * mei_cl_send - me device send (write) + * + * @cldev: me client device + * @buf: buffer to send + * @length: buffer length + * + * Return: written size in bytes or < 0 on error + */ +ssize_t mei_cl_send(struct mei_cl_device *cldev, u8 *buf, size_t length) { - struct mei_cl_device *cldev = to_mei_cl_device(dev); - const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); - size_t len; + struct mei_cl *cl = cldev->cl; - len = snprintf(buf, PAGE_SIZE, "%pUl", uuid); + if (cl == NULL) + return -ENODEV; - return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; + return __mei_cl_send(cl, buf, length, 1); } -static DEVICE_ATTR_RO(uuid); +EXPORT_SYMBOL_GPL(mei_cl_send); -static ssize_t modalias_show(struct device *dev, struct device_attribute *a, - char *buf) +/** + * mei_cl_recv - client receive (read) + * + * @cldev: me client device + * @buf: buffer to send + * @length: buffer length + * + * Return: read size in bytes of < 0 on error + */ +ssize_t mei_cl_recv(struct mei_cl_device *cldev, u8 *buf, size_t length) { - struct mei_cl_device *cldev = to_mei_cl_device(dev); - const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); - size_t len; + struct mei_cl *cl = cldev->cl; - len = snprintf(buf, PAGE_SIZE, "mei:%s:" MEI_CL_UUID_FMT ":", - cldev->name, MEI_CL_UUID_ARGS(uuid->b)); + if (cl == NULL) + return -ENODEV; - return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; + return __mei_cl_recv(cl, buf, length); } -static DEVICE_ATTR_RO(modalias); - -static struct attribute *mei_cl_dev_attrs[] = { - &dev_attr_name.attr, - &dev_attr_uuid.attr, - &dev_attr_modalias.attr, - NULL, -}; -ATTRIBUTE_GROUPS(mei_cl_dev); +EXPORT_SYMBOL_GPL(mei_cl_recv); -static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env) +/** + * mei_bus_event_work - dispatch rx event for a bus device + * and schedule new work + * + * @work: work + */ +static void mei_bus_event_work(struct work_struct *work) { - struct mei_cl_device *cldev = to_mei_cl_device(dev); - const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); + struct mei_cl_device *cldev; - if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid)) - return -ENOMEM; + cldev = container_of(work, struct mei_cl_device, event_work); - if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name)) - return -ENOMEM; + if (cldev->event_cb) + cldev->event_cb(cldev, cldev->events, cldev->event_context); - if (add_uevent_var(env, "MODALIAS=mei:%s:" MEI_CL_UUID_FMT ":", - cldev->name, MEI_CL_UUID_ARGS(uuid->b))) - return -ENOMEM; + cldev->events = 0; - return 0; + /* Prepare for the next read */ + mei_cl_read_start(cldev->cl, 0, NULL); } -static struct bus_type mei_cl_bus_type = { - .name = "mei", - .dev_groups = mei_cl_dev_groups, - .match = mei_cl_device_match, - .probe = mei_cl_device_probe, - .remove = mei_cl_device_remove, - .uevent = mei_cl_uevent, -}; - -static void mei_cl_dev_release(struct device *dev) +/** + * mei_cl_bus_rx_event - schedule rx evenet + * + * @cl: host client + */ +void mei_cl_bus_rx_event(struct mei_cl *cl) { - struct mei_cl_device *cldev = to_mei_cl_device(dev); + struct mei_cl_device *cldev = cl->cldev; - if (!cldev) + if (!cldev || !cldev->event_cb) return; - mei_me_cl_put(cldev->me_cl); - kfree(cldev); -} - -static struct device_type mei_cl_device_type = { - .release = mei_cl_dev_release, -}; - -struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *bus, - uuid_le uuid) -{ - struct mei_cl *cl; - - list_for_each_entry(cl, &bus->device_list, device_link) { - if (cl->cldev && cl->cldev->me_cl && - !uuid_le_cmp(uuid, *mei_me_cl_uuid(cl->cldev->me_cl))) - return cl; - } + set_bit(MEI_CL_EVENT_RX, &cldev->events); - return NULL; + schedule_work(&cldev->event_work); } -struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, - struct mei_me_client *me_cl, - struct mei_cl *cl, - char *name) +/** + * mei_cl_register_event_cb - register event callback + * + * @cldev: me client devices + * @event_cb: callback function + * @context: driver context data + * + * Return: 0 on success + * -EALREADY if an callback is already registered + * <0 on other errors + */ +int mei_cl_register_event_cb(struct mei_cl_device *cldev, + mei_cl_event_cb_t event_cb, void *context) { - struct mei_cl_device *cldev; - int status; - - cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL); - if (!cldev) - return NULL; - - cldev->me_cl = mei_me_cl_get(me_cl); - if (!cldev->me_cl) { - kfree(cldev); - return NULL; - } - - cldev->cl = cl; - cldev->dev.parent = bus->dev; - cldev->dev.bus = &mei_cl_bus_type; - cldev->dev.type = &mei_cl_device_type; - - strlcpy(cldev->name, name, sizeof(cldev->name)); - - dev_set_name(&cldev->dev, "mei:%s:%pUl", name, mei_me_cl_uuid(me_cl)); + if (cldev->event_cb) + return -EALREADY; - status = device_register(&cldev->dev); - if (status) { - dev_err(bus->dev, "Failed to register MEI device\n"); - mei_me_cl_put(cldev->me_cl); - kfree(cldev); - return NULL; - } + cldev->events = 0; + cldev->event_cb = event_cb; + cldev->event_context = context; + INIT_WORK(&cldev->event_work, mei_bus_event_work); - cl->cldev = cldev; + mei_cl_read_start(cldev->cl, 0, NULL); - dev_dbg(&cldev->dev, "client %s registered\n", name); + return 0; +} +EXPORT_SYMBOL_GPL(mei_cl_register_event_cb); - return cldev; +/** + * mei_cl_get_drvdata - driver data getter + * + * @cldev: mei client device + * + * Return: driver private data + */ +void *mei_cl_get_drvdata(const struct mei_cl_device *cldev) +{ + return dev_get_drvdata(&cldev->dev); } -EXPORT_SYMBOL_GPL(mei_cl_add_device); +EXPORT_SYMBOL_GPL(mei_cl_get_drvdata); -void mei_cl_remove_device(struct mei_cl_device *cldev) +/** + * mei_cl_set_drvdata - driver data setter + * + * @cldev: mei client device + * @data: data to store + */ +void mei_cl_set_drvdata(struct mei_cl_device *cldev, void *data) { - device_unregister(&cldev->dev); + dev_set_drvdata(&cldev->dev, data); } -EXPORT_SYMBOL_GPL(mei_cl_remove_device); +EXPORT_SYMBOL_GPL(mei_cl_set_drvdata); -int __mei_cl_driver_register(struct mei_cl_driver *cldrv, struct module *owner) +/** + * mei_cl_enable_device - enable me client device + * create connection with me client + * + * @cldev: me client device + * + * Return: 0 on success and < 0 on error + */ +int mei_cl_enable_device(struct mei_cl_device *cldev) { int err; + struct mei_device *bus; + struct mei_cl *cl = cldev->cl; - cldrv->driver.name = cldrv->name; - cldrv->driver.owner = owner; - cldrv->driver.bus = &mei_cl_bus_type; + if (cl == NULL) + return -ENODEV; - err = driver_register(&cldrv->driver); - if (err) - return err; + bus = cl->dev; - pr_debug("mei: driver [%s] registered\n", cldrv->driver.name); + mutex_lock(&bus->device_lock); - return 0; -} -EXPORT_SYMBOL_GPL(__mei_cl_driver_register); + if (mei_cl_is_connected(cl)) { + mutex_unlock(&bus->device_lock); + dev_warn(bus->dev, "Already connected"); + return -EBUSY; + } -void mei_cl_driver_unregister(struct mei_cl_driver *cldrv) -{ - driver_unregister(&cldrv->driver); + err = mei_cl_connect(cl, cldev->me_cl, NULL); + if (err < 0) { + mutex_unlock(&bus->device_lock); + dev_err(bus->dev, "Could not connect to the ME client"); - pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name); + return err; + } + + mutex_unlock(&bus->device_lock); + + if (cldev->event_cb) + mei_cl_read_start(cldev->cl, 0, NULL); + + return 0; } -EXPORT_SYMBOL_GPL(mei_cl_driver_unregister); +EXPORT_SYMBOL_GPL(mei_cl_enable_device); -ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, - bool blocking) +/** + * mei_cl_disable_device - disable me client device + * disconnect form the me client + * + * @cldev: me client device + * + * Return: 0 on success and < 0 on error + */ +int mei_cl_disable_device(struct mei_cl_device *cldev) { + int err; struct mei_device *bus; - struct mei_cl_cb *cb = NULL; - ssize_t rets; + struct mei_cl *cl = cldev->cl; - if (WARN_ON(!cl || !cl->dev)) + if (cl == NULL) return -ENODEV; bus = cl->dev; - mutex_lock(&bus->device_lock); - if (!mei_cl_is_connected(cl)) { - rets = -ENODEV; - goto out; - } + cldev->event_cb = NULL; - /* Check if we have an ME client device */ - if (!mei_me_cl_is_active(cl->me_cl)) { - rets = -ENOTTY; - goto out; - } + mutex_lock(&bus->device_lock); - if (length > mei_cl_mtu(cl)) { - rets = -EFBIG; + if (!mei_cl_is_connected(cl)) { + dev_err(bus->dev, "Already disconnected"); + err = 0; goto out; } - cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL); - if (!cb) { - rets = -ENOMEM; + err = mei_cl_disconnect(cl); + if (err < 0) { + dev_err(bus->dev, "Could not disconnect from the ME client"); goto out; } - memcpy(cb->buf.data, buf, length); - - rets = mei_cl_write(cl, cb, blocking); + /* Flush queues and remove any pending read */ + mei_cl_flush_queues(cl, NULL); out: mutex_unlock(&bus->device_lock); - if (rets < 0) - mei_io_cb_free(cb); + return err; - return rets; } +EXPORT_SYMBOL_GPL(mei_cl_disable_device); -ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length) +static int mei_cl_device_match(struct device *dev, struct device_driver *drv) { - struct mei_device *bus; - struct mei_cl_cb *cb; - size_t r_length; - ssize_t rets; + struct mei_cl_device *cldev = to_mei_cl_device(dev); + struct mei_cl_driver *cldrv = to_mei_cl_driver(drv); + const struct mei_cl_device_id *id; + const uuid_le *uuid; + const char *name; - if (WARN_ON(!cl || !cl->dev)) - return -ENODEV; + if (!cldev) + return 0; - bus = cl->dev; + uuid = mei_me_cl_uuid(cldev->me_cl); + name = cldev->name; - mutex_lock(&bus->device_lock); + if (!cldrv || !cldrv->id_table) + return 0; - cb = mei_cl_read_cb(cl, NULL); - if (cb) - goto copy; + id = cldrv->id_table; - rets = mei_cl_read_start(cl, length, NULL); - if (rets && rets != -EBUSY) - goto out; + while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) { - if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) { + if (!uuid_le_cmp(*uuid, id->uuid)) { + if (id->name[0]) { + if (!strncmp(name, id->name, sizeof(id->name))) + return 1; + } else { + return 1; + } + } - mutex_unlock(&bus->device_lock); + id++; + } - if (wait_event_interruptible(cl->rx_wait, - (!list_empty(&cl->rd_completed)) || - (!mei_cl_is_connected(cl)))) { + return 0; +} - if (signal_pending(current)) - return -EINTR; - return -ERESTARTSYS; - } +static int mei_cl_device_probe(struct device *dev) +{ + struct mei_cl_device *cldev = to_mei_cl_device(dev); + struct mei_cl_driver *cldrv; + struct mei_cl_device_id id; - mutex_lock(&bus->device_lock); + if (!cldev) + return 0; - if (!mei_cl_is_connected(cl)) { - rets = -EBUSY; - goto out; - } - } + cldrv = to_mei_cl_driver(dev->driver); + if (!cldrv || !cldrv->probe) + return -ENODEV; + + dev_dbg(dev, "Device probe\n"); + + strlcpy(id.name, cldev->name, sizeof(id.name)); + + return cldrv->probe(cldev, &id); +} - cb = mei_cl_read_cb(cl, NULL); - if (!cb) { - rets = 0; - goto out; - } +static int mei_cl_device_remove(struct device *dev) +{ + struct mei_cl_device *cldev = to_mei_cl_device(dev); + struct mei_cl_driver *cldrv; -copy: - if (cb->status) { - rets = cb->status; - goto free; + if (!cldev || !dev->driver) + return 0; + + if (cldev->event_cb) { + cldev->event_cb = NULL; + cancel_work_sync(&cldev->event_work); } - r_length = min_t(size_t, length, cb->buf_idx); - memcpy(buf, cb->buf.data, r_length); - rets = r_length; + cldrv = to_mei_cl_driver(dev->driver); + if (!cldrv->remove) { + dev->driver = NULL; -free: - mei_io_cb_free(cb); -out: - mutex_unlock(&bus->device_lock); + return 0; + } - return rets; + return cldrv->remove(cldev); } -ssize_t mei_cl_send(struct mei_cl_device *cldev, u8 *buf, size_t length) +static ssize_t name_show(struct device *dev, struct device_attribute *a, + char *buf) { - struct mei_cl *cl = cldev->cl; + struct mei_cl_device *cldev = to_mei_cl_device(dev); + size_t len; - if (cl == NULL) - return -ENODEV; + len = snprintf(buf, PAGE_SIZE, "%s", cldev->name); - return __mei_cl_send(cl, buf, length, 1); + return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; } -EXPORT_SYMBOL_GPL(mei_cl_send); +static DEVICE_ATTR_RO(name); -ssize_t mei_cl_recv(struct mei_cl_device *cldev, u8 *buf, size_t length) +static ssize_t uuid_show(struct device *dev, struct device_attribute *a, + char *buf) { - struct mei_cl *cl = cldev->cl; + struct mei_cl_device *cldev = to_mei_cl_device(dev); + const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); + size_t len; - if (cl == NULL) - return -ENODEV; + len = snprintf(buf, PAGE_SIZE, "%pUl", uuid); - return __mei_cl_recv(cl, buf, length); + return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; } -EXPORT_SYMBOL_GPL(mei_cl_recv); +static DEVICE_ATTR_RO(uuid); -static void mei_bus_event_work(struct work_struct *work) +static ssize_t modalias_show(struct device *dev, struct device_attribute *a, + char *buf) { - struct mei_cl_device *cldev; - - cldev = container_of(work, struct mei_cl_device, event_work); - - if (cldev->event_cb) - cldev->event_cb(cldev, cldev->events, cldev->event_context); + struct mei_cl_device *cldev = to_mei_cl_device(dev); + const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); + size_t len; - cldev->events = 0; + len = snprintf(buf, PAGE_SIZE, "mei:%s:" MEI_CL_UUID_FMT ":", + cldev->name, MEI_CL_UUID_ARGS(uuid->b)); - /* Prepare for the next read */ - mei_cl_read_start(cldev->cl, 0, NULL); + return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; } +static DEVICE_ATTR_RO(modalias); -int mei_cl_register_event_cb(struct mei_cl_device *cldev, - mei_cl_event_cb_t event_cb, void *context) +static struct attribute *mei_cl_dev_attrs[] = { + &dev_attr_name.attr, + &dev_attr_uuid.attr, + &dev_attr_modalias.attr, + NULL, +}; +ATTRIBUTE_GROUPS(mei_cl_dev); + +static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env) { - if (cldev->event_cb) - return -EALREADY; + struct mei_cl_device *cldev = to_mei_cl_device(dev); + const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); - cldev->events = 0; - cldev->event_cb = event_cb; - cldev->event_context = context; - INIT_WORK(&cldev->event_work, mei_bus_event_work); + if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid)) + return -ENOMEM; - mei_cl_read_start(cldev->cl, 0, NULL); + if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name)) + return -ENOMEM; + + if (add_uevent_var(env, "MODALIAS=mei:%s:" MEI_CL_UUID_FMT ":", + cldev->name, MEI_CL_UUID_ARGS(uuid->b))) + return -ENOMEM; return 0; } -EXPORT_SYMBOL_GPL(mei_cl_register_event_cb); -void *mei_cl_get_drvdata(const struct mei_cl_device *cldev) +static struct bus_type mei_cl_bus_type = { + .name = "mei", + .dev_groups = mei_cl_dev_groups, + .match = mei_cl_device_match, + .probe = mei_cl_device_probe, + .remove = mei_cl_device_remove, + .uevent = mei_cl_uevent, +}; + +static void mei_cl_dev_release(struct device *dev) { - return dev_get_drvdata(&cldev->dev); + struct mei_cl_device *cldev = to_mei_cl_device(dev); + + if (!cldev) + return; + + mei_me_cl_put(cldev->me_cl); + kfree(cldev); } -EXPORT_SYMBOL_GPL(mei_cl_get_drvdata); -void mei_cl_set_drvdata(struct mei_cl_device *cldev, void *data) +static struct device_type mei_cl_device_type = { + .release = mei_cl_dev_release, +}; + +struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *bus, + uuid_le uuid) { - dev_set_drvdata(&cldev->dev, data); + struct mei_cl *cl; + + list_for_each_entry(cl, &bus->device_list, device_link) { + if (cl->cldev && cl->cldev->me_cl && + !uuid_le_cmp(uuid, *mei_me_cl_uuid(cl->cldev->me_cl))) + return cl; + } + + return NULL; } -EXPORT_SYMBOL_GPL(mei_cl_set_drvdata); -int mei_cl_enable_device(struct mei_cl_device *cldev) +struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, + struct mei_me_client *me_cl, + struct mei_cl *cl, + char *name) { - int err; - struct mei_device *bus; - struct mei_cl *cl = cldev->cl; + struct mei_cl_device *cldev; + int status; - if (cl == NULL) - return -ENODEV; + cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL); + if (!cldev) + return NULL; - bus = cl->dev; + cldev->me_cl = mei_me_cl_get(me_cl); + if (!cldev->me_cl) { + kfree(cldev); + return NULL; + } - mutex_lock(&bus->device_lock); + cldev->cl = cl; + cldev->dev.parent = bus->dev; + cldev->dev.bus = &mei_cl_bus_type; + cldev->dev.type = &mei_cl_device_type; - if (mei_cl_is_connected(cl)) { - mutex_unlock(&bus->device_lock); - dev_warn(bus->dev, "Already connected"); - return -EBUSY; - } + strlcpy(cldev->name, name, sizeof(cldev->name)); - err = mei_cl_connect(cl, cldev->me_cl, NULL); - if (err < 0) { - mutex_unlock(&bus->device_lock); - dev_err(bus->dev, "Could not connect to the ME client"); + dev_set_name(&cldev->dev, "mei:%s:%pUl", name, mei_me_cl_uuid(me_cl)); - return err; + status = device_register(&cldev->dev); + if (status) { + dev_err(bus->dev, "Failed to register MEI device\n"); + mei_me_cl_put(cldev->me_cl); + kfree(cldev); + return NULL; } - mutex_unlock(&bus->device_lock); + cl->cldev = cldev; - if (cldev->event_cb) - mei_cl_read_start(cldev->cl, 0, NULL); + dev_dbg(&cldev->dev, "client %s registered\n", name); - return 0; + return cldev; } -EXPORT_SYMBOL_GPL(mei_cl_enable_device); +EXPORT_SYMBOL_GPL(mei_cl_add_device); -int mei_cl_disable_device(struct mei_cl_device *cldev) +void mei_cl_remove_device(struct mei_cl_device *cldev) { - int err; - struct mei_device *bus; - struct mei_cl *cl = cldev->cl; - - if (cl == NULL) - return -ENODEV; - - bus = cl->dev; - - cldev->event_cb = NULL; - - mutex_lock(&bus->device_lock); + device_unregister(&cldev->dev); +} +EXPORT_SYMBOL_GPL(mei_cl_remove_device); - if (!mei_cl_is_connected(cl)) { - dev_err(bus->dev, "Already disconnected"); - err = 0; - goto out; - } +int __mei_cl_driver_register(struct mei_cl_driver *cldrv, struct module *owner) +{ + int err; - err = mei_cl_disconnect(cl); - if (err < 0) { - dev_err(bus->dev, "Could not disconnect from the ME client"); - goto out; - } + cldrv->driver.name = cldrv->name; + cldrv->driver.owner = owner; + cldrv->driver.bus = &mei_cl_bus_type; - /* Flush queues and remove any pending read */ - mei_cl_flush_queues(cl, NULL); + err = driver_register(&cldrv->driver); + if (err) + return err; -out: - mutex_unlock(&bus->device_lock); - return err; + pr_debug("mei: driver [%s] registered\n", cldrv->driver.name); + return 0; } -EXPORT_SYMBOL_GPL(mei_cl_disable_device); +EXPORT_SYMBOL_GPL(__mei_cl_driver_register); -void mei_cl_bus_rx_event(struct mei_cl *cl) +void mei_cl_driver_unregister(struct mei_cl_driver *cldrv) { - struct mei_cl_device *cldev = cl->cldev; - - if (!cldev || !cldev->event_cb) - return; - - set_bit(MEI_CL_EVENT_RX, &cldev->events); + driver_unregister(&cldrv->driver); - schedule_work(&cldev->event_work); + pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name); } +EXPORT_SYMBOL_GPL(mei_cl_driver_unregister); int __init mei_cl_bus_init(void) { -- cgit v1.2.3 From 38d3c00d3f312989e50aaf6f4a4b490b4e1e4c37 Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Thu, 23 Jul 2015 15:08:36 +0300 Subject: mei: bus: rename uevent handler to mei_cl_device_uevent Rename mei_cl_uevent to mei_cl_device_uevent to match the naming convention of mei_cl_bus_type functions Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index 6ea8a408f477..bec213b92550 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -510,7 +510,15 @@ static struct attribute *mei_cl_dev_attrs[] = { }; ATTRIBUTE_GROUPS(mei_cl_dev); -static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env) +/** + * mei_cl_device_uevent - me client bus uevent handler + * + * @dev: device + * @env: uevent kobject + * + * Return: 0 on success -ENOMEM on when add_uevent_var fails + */ +static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env) { struct mei_cl_device *cldev = to_mei_cl_device(dev); const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); @@ -534,7 +542,7 @@ static struct bus_type mei_cl_bus_type = { .match = mei_cl_device_match, .probe = mei_cl_device_probe, .remove = mei_cl_device_remove, - .uevent = mei_cl_uevent, + .uevent = mei_cl_device_uevent, }; static void mei_cl_dev_release(struct device *dev) -- cgit v1.2.3 From 7e280ab694e2885ee300de9cf5e7047c68230148 Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Thu, 23 Jul 2015 15:08:37 +0300 Subject: mei: bus: don't enable events implicitly in device enable Do not enable events implicitly in mei_cl_enable_device, it should be done explicitly using mei_cl_register_event_cb so the events are enabled only when needed. The NFC drivers has been already using it that way so no need for further changes just remove the code from mei_cl_enable_device. Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index bec213b92550..257e200b5343 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -332,9 +332,6 @@ int mei_cl_enable_device(struct mei_cl_device *cldev) mutex_unlock(&bus->device_lock); - if (cldev->event_cb) - mei_cl_read_start(cldev->cl, 0, NULL); - return 0; } EXPORT_SYMBOL_GPL(mei_cl_enable_device); -- cgit v1.2.3 From 48168f4561f479403dbd38379dc8793488a22a6a Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Thu, 23 Jul 2015 15:08:38 +0300 Subject: mei: bus: report if event registration failed If event registeration has failed, the caller should know about it. Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index 257e200b5343..aba7688d1ddd 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -256,6 +256,8 @@ void mei_cl_bus_rx_event(struct mei_cl *cl) int mei_cl_register_event_cb(struct mei_cl_device *cldev, mei_cl_event_cb_t event_cb, void *context) { + int ret; + if (cldev->event_cb) return -EALREADY; @@ -264,7 +266,9 @@ int mei_cl_register_event_cb(struct mei_cl_device *cldev, cldev->event_context = context; INIT_WORK(&cldev->event_work, mei_bus_event_work); - mei_cl_read_start(cldev->cl, 0, NULL); + ret = mei_cl_read_start(cldev->cl, 0, NULL); + if (ret && ret != -EBUSY) + return ret; return 0; } -- cgit v1.2.3 From 688a9cce0c8e9116038586ae1cd6adabb10fa5aa Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Thu, 23 Jul 2015 15:08:39 +0300 Subject: mei: bus: revamp device matching mei_cl_device_match now calls mei_cl_device_find that returns the matching device id in the device id table. We will utilize the mei_cl_device_find during probing to locate the matching entry. Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus.c | 63 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 19 deletions(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index aba7688d1ddd..4955a0908d04 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -385,39 +385,64 @@ out: } EXPORT_SYMBOL_GPL(mei_cl_disable_device); -static int mei_cl_device_match(struct device *dev, struct device_driver *drv) +/** + * mei_cl_device_find - find matching entry in the driver id table + * + * @cldev: me client device + * @cldrv: me client driver + * + * Return: id on success; NULL if no id is matching + */ +static const +struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev, + struct mei_cl_driver *cldrv) { - struct mei_cl_device *cldev = to_mei_cl_device(dev); - struct mei_cl_driver *cldrv = to_mei_cl_driver(drv); const struct mei_cl_device_id *id; const uuid_le *uuid; - const char *name; - - if (!cldev) - return 0; uuid = mei_me_cl_uuid(cldev->me_cl); - name = cldev->name; - - if (!cldrv || !cldrv->id_table) - return 0; id = cldrv->id_table; - while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) { - if (!uuid_le_cmp(*uuid, id->uuid)) { - if (id->name[0]) { - if (!strncmp(name, id->name, sizeof(id->name))) - return 1; - } else { - return 1; - } + + if (!cldev->name[0]) + return id; + + if (!strncmp(cldev->name, id->name, sizeof(id->name))) + return id; } id++; } + return NULL; +} + +/** + * mei_cl_device_match - device match function + * + * @dev: device + * @drv: driver + * + * Return: 1 if matching device was found 0 otherwise + */ +static int mei_cl_device_match(struct device *dev, struct device_driver *drv) +{ + struct mei_cl_device *cldev = to_mei_cl_device(dev); + struct mei_cl_driver *cldrv = to_mei_cl_driver(drv); + const struct mei_cl_device_id *found_id; + + if (!cldev) + return 0; + + if (!cldrv || !cldrv->id_table) + return 0; + + found_id = mei_cl_device_find(cldev, cldrv); + if (found_id) + return 1; + return 0; } -- cgit v1.2.3 From feb8cd0fe7d63fd259c28f8a52fc88745717c9ec Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Thu, 23 Jul 2015 15:08:40 +0300 Subject: mei: bus: revamp probe and remove functions Instead of generating device id on the fly during probing we find the matching id entry on the device id table. Get bus the module reference counter so it cannot be unloaded after the driver has bounded to the client device Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index 4955a0908d04..1d9ce9c491cf 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -446,30 +446,49 @@ static int mei_cl_device_match(struct device *dev, struct device_driver *drv) return 0; } +/** + * mei_cl_device_probe - bus probe function + * + * @dev: device + * + * Return: 0 on success; < 0 otherwise + */ static int mei_cl_device_probe(struct device *dev) { - struct mei_cl_device *cldev = to_mei_cl_device(dev); + struct mei_cl_device *cldev; struct mei_cl_driver *cldrv; - struct mei_cl_device_id id; + const struct mei_cl_device_id *id; + + cldev = to_mei_cl_device(dev); + cldrv = to_mei_cl_driver(dev->driver); if (!cldev) return 0; - cldrv = to_mei_cl_driver(dev->driver); if (!cldrv || !cldrv->probe) return -ENODEV; - dev_dbg(dev, "Device probe\n"); + id = mei_cl_device_find(cldev, cldrv); + if (!id) + return -ENODEV; - strlcpy(id.name, cldev->name, sizeof(id.name)); + __module_get(THIS_MODULE); - return cldrv->probe(cldev, &id); + return cldrv->probe(cldev, id); } +/** + * mei_cl_device_remove - remove device from the bus + * + * @dev: device + * + * Return: 0 on success; < 0 otherwise + */ static int mei_cl_device_remove(struct device *dev) { struct mei_cl_device *cldev = to_mei_cl_device(dev); struct mei_cl_driver *cldrv; + int ret = 0; if (!cldev || !dev->driver) return 0; @@ -480,13 +499,13 @@ static int mei_cl_device_remove(struct device *dev) } cldrv = to_mei_cl_driver(dev->driver); - if (!cldrv->remove) { - dev->driver = NULL; + if (cldrv->remove) + ret = cldrv->remove(cldev); - return 0; - } + module_put(THIS_MODULE); + dev->driver = NULL; + return ret; - return cldrv->remove(cldev); } static ssize_t name_show(struct device *dev, struct device_attribute *a, -- cgit v1.2.3 From 512f64d9f7467597388ffbd5a21589ee3f375d8b Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Thu, 23 Jul 2015 15:08:41 +0300 Subject: mei: bus: add reference to bus device in struct mei_cl_client Add reference to the bus device (mei_device) for easier access. To ensures that referencing cldev->bus is valid during cldev life time we increase the bus ref counter on a client device creation and drop it on the device release. Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus.c | 17 +++++++++++++++++ include/linux/mei_cl_bus.h | 3 +++ 2 files changed, 20 insertions(+) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index 1d9ce9c491cf..963731eb4383 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -590,6 +590,20 @@ static struct bus_type mei_cl_bus_type = { .uevent = mei_cl_device_uevent, }; +static struct mei_device *mei_dev_bus_get(struct mei_device *bus) +{ + if (bus) + get_device(bus->dev); + + return bus; +} + +static void mei_dev_bus_put(struct mei_device *bus) +{ + if (bus) + put_device(bus->dev); +} + static void mei_cl_dev_release(struct device *dev) { struct mei_cl_device *cldev = to_mei_cl_device(dev); @@ -598,6 +612,7 @@ static void mei_cl_dev_release(struct device *dev) return; mei_me_cl_put(cldev->me_cl); + mei_dev_bus_put(cldev->bus); kfree(cldev); } @@ -641,6 +656,7 @@ struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, cldev->dev.parent = bus->dev; cldev->dev.bus = &mei_cl_bus_type; cldev->dev.type = &mei_cl_device_type; + cldev->bus = mei_dev_bus_get(bus); strlcpy(cldev->name, name, sizeof(cldev->name)); @@ -650,6 +666,7 @@ struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, if (status) { dev_err(bus->dev, "Failed to register MEI device\n"); mei_me_cl_put(cldev->me_cl); + mei_dev_bus_put(bus); kfree(cldev); return NULL; } diff --git a/include/linux/mei_cl_bus.h b/include/linux/mei_cl_bus.h index a16b1f9c1aca..4c5c25b3222c 100644 --- a/include/linux/mei_cl_bus.h +++ b/include/linux/mei_cl_bus.h @@ -6,6 +6,7 @@ #include struct mei_cl_device; +struct mei_device; typedef void (*mei_cl_event_cb_t)(struct mei_cl_device *device, u32 events, void *context); @@ -17,6 +18,7 @@ typedef void (*mei_cl_event_cb_t)(struct mei_cl_device *device, * Drivers for MEI devices will get an mei_cl_device pointer * when being probed and shall use it for doing ME bus I/O. * + * @bus: parent mei device * @dev: linux driver model device pointer * @me_cl: me client * @cl: mei client @@ -29,6 +31,7 @@ typedef void (*mei_cl_event_cb_t)(struct mei_cl_device *device, * @priv_data: client private data */ struct mei_cl_device { + struct mei_device *bus; struct device dev; struct mei_me_client *me_cl; -- cgit v1.2.3 From 0ff0a8d853039aa60bba3ca3e04e4fb74584a736 Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Thu, 23 Jul 2015 15:08:42 +0300 Subject: mei: bus: add me client device list infrastructure Instead of holding the list of host clients (me_cl) we want to keep the list me client devices (mei_cl_device) This way we can create host to me client connection only when needed. Add list head to mei_cl_device and cl_bus_lock Add bus_added flag to the me client (mei_me_client) to track if the appropriate mei_cl_device was already created and is_added flag to mei_cl_device to track if it was already added to the device list across the bus rescans Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus.c | 1 + drivers/misc/mei/init.c | 1 + drivers/misc/mei/mei_dev.h | 6 ++++-- include/linux/mei_cl_bus.h | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index 963731eb4383..34b14dda050c 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -657,6 +657,7 @@ struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, cldev->dev.bus = &mei_cl_bus_type; cldev->dev.type = &mei_cl_device_type; cldev->bus = mei_dev_bus_get(bus); + INIT_LIST_HEAD(&cldev->bus_list); strlcpy(cldev->name, name, sizeof(cldev->name)); diff --git a/drivers/misc/mei/init.c b/drivers/misc/mei/init.c index 00c3865ca3b1..15000e9231b1 100644 --- a/drivers/misc/mei/init.c +++ b/drivers/misc/mei/init.c @@ -390,6 +390,7 @@ void mei_device_init(struct mei_device *dev, INIT_LIST_HEAD(&dev->me_clients); mutex_init(&dev->device_lock); init_rwsem(&dev->me_clients_rwsem); + mutex_init(&dev->cl_bus_lock); init_waitqueue_head(&dev->wait_hw_ready); init_waitqueue_head(&dev->wait_pg); init_waitqueue_head(&dev->wait_hbm_start); diff --git a/drivers/misc/mei/mei_dev.h b/drivers/misc/mei/mei_dev.h index bc65fb42aea9..882e6f77084a 100644 --- a/drivers/misc/mei/mei_dev.h +++ b/drivers/misc/mei/mei_dev.h @@ -178,7 +178,7 @@ struct mei_fw_status { * @client_id: me client id * @mei_flow_ctrl_creds: flow control credits * @connect_count: number connections to this client - * @reserved: reserved + * @bus_added: added to bus */ struct mei_me_client { struct list_head list; @@ -187,7 +187,7 @@ struct mei_me_client { u8 client_id; u8 mei_flow_ctrl_creds; u8 connect_count; - u8 reserved; + u8 bus_added; }; @@ -447,6 +447,7 @@ const char *mei_pg_state_str(enum mei_pg_state state); * @reset_work : work item for the device reset * * @device_list : mei client bus list + * @cl_bus_lock : client bus list lock * * @dbgfs_dir : debugfs mei root directory * @@ -543,6 +544,7 @@ struct mei_device { /* List of bus devices */ struct list_head device_list; + struct mutex cl_bus_lock; #if IS_ENABLED(CONFIG_DEBUG_FS) struct dentry *dbgfs_dir; diff --git a/include/linux/mei_cl_bus.h b/include/linux/mei_cl_bus.h index 4c5c25b3222c..85239138251c 100644 --- a/include/linux/mei_cl_bus.h +++ b/include/linux/mei_cl_bus.h @@ -18,6 +18,7 @@ typedef void (*mei_cl_event_cb_t)(struct mei_cl_device *device, * Drivers for MEI devices will get an mei_cl_device pointer * when being probed and shall use it for doing ME bus I/O. * + * @bus_list: device on the bus list * @bus: parent mei device * @dev: linux driver model device pointer * @me_cl: me client @@ -28,9 +29,11 @@ typedef void (*mei_cl_event_cb_t)(struct mei_cl_device *device, * events (e.g. Rx buffer pending) notifications. * @event_context: event callback run context * @events: Events bitmask sent to the driver. + * @is_added: device is already scanned * @priv_data: client private data */ struct mei_cl_device { + struct list_head bus_list; struct mei_device *bus; struct device dev; @@ -42,6 +45,7 @@ struct mei_cl_device { mei_cl_event_cb_t event_cb; void *event_context; unsigned long events; + unsigned int is_added:1; void *priv_data; }; -- cgit v1.2.3 From 71ce789115f878a07e4a6c43d6006cea6aee1078 Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Thu, 23 Jul 2015 15:08:43 +0300 Subject: mei: bus: enable running fixup routines before device registration Split the device registration into allocation and device struct initialization, device setup, and the final device registration. This why it is possible to run fixups and quirks during the setup stage on an initialized device. Each fixup routine effects do_match flag. If the flag is set to false at the end the device won't be registered on the bus. Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus-fixup.c | 30 +++++++++++++++ drivers/misc/mei/bus.c | 91 ++++++++++++++++++++++++++++++++++++-------- drivers/misc/mei/mei_dev.h | 2 +- include/linux/mei_cl_bus.h | 4 ++ 4 files changed, 111 insertions(+), 16 deletions(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus-fixup.c b/drivers/misc/mei/bus-fixup.c index 47aa1523d9e1..865e33bcd226 100644 --- a/drivers/misc/mei/bus-fixup.c +++ b/drivers/misc/mei/bus-fixup.c @@ -20,12 +20,15 @@ #include #include #include +#include #include #include "mei_dev.h" #include "client.h" +#define MEI_UUID_ANY NULL_UUID_LE + struct mei_nfc_cmd { u8 command; u8 status; @@ -412,4 +415,31 @@ void mei_nfc_host_exit(struct mei_device *bus) mutex_unlock(&bus->device_lock); } +#define MEI_FIXUP(_uuid, _hook) { _uuid, _hook } + +static struct mei_fixup { + + const uuid_le uuid; + void (*hook)(struct mei_cl_device *cldev); +} mei_fixups[] = {}; + +/** + * mei_cl_dev_fixup - run fixup handlers + * + * @cldev: me client device + */ +void mei_cl_dev_fixup(struct mei_cl_device *cldev) +{ + struct mei_fixup *f; + const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); + int i; + + for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) { + + f = &mei_fixups[i]; + if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 || + uuid_le_cmp(f->uuid, *uuid) == 0) + f->hook(cldev); + } +} diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index 34b14dda050c..68b7756bf384 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -436,6 +436,9 @@ static int mei_cl_device_match(struct device *dev, struct device_driver *drv) if (!cldev) return 0; + if (!cldev->do_match) + return 0; + if (!cldrv || !cldrv->id_table) return 0; @@ -634,6 +637,76 @@ struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *bus, return NULL; } +/** + * mei_cl_dev_alloc - initialize and allocate mei client device + * + * @bus: mei device + * @me_cl: me client + * + * Return: allocated device structur or NULL on allocation failure + */ +static struct mei_cl_device *mei_cl_dev_alloc(struct mei_device *bus, + struct mei_me_client *me_cl) +{ + struct mei_cl_device *cldev; + + cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL); + if (!cldev) + return NULL; + + device_initialize(&cldev->dev); + cldev->dev.parent = bus->dev; + cldev->dev.bus = &mei_cl_bus_type; + cldev->dev.type = &mei_cl_device_type; + cldev->bus = mei_dev_bus_get(bus); + cldev->me_cl = mei_me_cl_get(me_cl); + cldev->is_added = 0; + INIT_LIST_HEAD(&cldev->bus_list); + + return cldev; +} + +/** + * mei_cl_dev_setup - setup me client device + * run fix up routines and set the device name + * + * @bus: mei device + * @cldev: me client device + * + * Return: true if the device is eligible for enumeration + */ +static bool mei_cl_dev_setup(struct mei_device *bus, + struct mei_cl_device *cldev) +{ + cldev->do_match = 1; + mei_cl_dev_fixup(cldev); + + if (cldev->do_match) + dev_set_name(&cldev->dev, "mei:%s:%pUl", + cldev->name, mei_me_cl_uuid(cldev->me_cl)); + + return cldev->do_match == 1; +} + +/** + * mei_cl_bus_dev_add - add me client devices + * + * @cldev: me client device + * + * Return: 0 on success; < 0 on failre + */ +static int mei_cl_bus_dev_add(struct mei_cl_device *cldev) +{ + int ret; + + dev_dbg(cldev->bus->dev, "adding %pUL\n", mei_me_cl_uuid(cldev->me_cl)); + ret = device_add(&cldev->dev); + if (!ret) + cldev->is_added = 1; + + return ret; +} + struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, struct mei_me_client *me_cl, struct mei_cl *cl, @@ -642,28 +715,16 @@ struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, struct mei_cl_device *cldev; int status; - cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL); + cldev = mei_cl_dev_alloc(bus, me_cl); if (!cldev) return NULL; - cldev->me_cl = mei_me_cl_get(me_cl); - if (!cldev->me_cl) { - kfree(cldev); - return NULL; - } - cldev->cl = cl; - cldev->dev.parent = bus->dev; - cldev->dev.bus = &mei_cl_bus_type; - cldev->dev.type = &mei_cl_device_type; - cldev->bus = mei_dev_bus_get(bus); - INIT_LIST_HEAD(&cldev->bus_list); - strlcpy(cldev->name, name, sizeof(cldev->name)); - dev_set_name(&cldev->dev, "mei:%s:%pUl", name, mei_me_cl_uuid(me_cl)); + mei_cl_dev_setup(bus, cldev); - status = device_register(&cldev->dev); + status = mei_cl_bus_dev_add(cldev); if (status) { dev_err(bus->dev, "Failed to register MEI device\n"); mei_me_cl_put(cldev->me_cl); diff --git a/drivers/misc/mei/mei_dev.h b/drivers/misc/mei/mei_dev.h index 882e6f77084a..ad59ab776f2d 100644 --- a/drivers/misc/mei/mei_dev.h +++ b/drivers/misc/mei/mei_dev.h @@ -335,7 +335,7 @@ struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, struct mei_cl *cl, char *name); void mei_cl_remove_device(struct mei_cl_device *cldev); - +void mei_cl_dev_fixup(struct mei_cl_device *dev); ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, bool blocking); ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length); diff --git a/include/linux/mei_cl_bus.h b/include/linux/mei_cl_bus.h index 85239138251c..81ab56dd0ae0 100644 --- a/include/linux/mei_cl_bus.h +++ b/include/linux/mei_cl_bus.h @@ -29,6 +29,8 @@ typedef void (*mei_cl_event_cb_t)(struct mei_cl_device *device, * events (e.g. Rx buffer pending) notifications. * @event_context: event callback run context * @events: Events bitmask sent to the driver. + * + * @do_match: wheather device can be matched with a driver * @is_added: device is already scanned * @priv_data: client private data */ @@ -45,6 +47,8 @@ struct mei_cl_device { mei_cl_event_cb_t event_cb; void *event_context; unsigned long events; + + unsigned int do_match:1; unsigned int is_added:1; void *priv_data; -- cgit v1.2.3 From b39910c2e0ac7c3d0e2f1999b04308c771b1d8fc Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Thu, 23 Jul 2015 15:08:46 +0300 Subject: mei: bus: simplify how we build nfc bus name Remove the dependency on struct ndev from the nfc device name creation function so it is possible to use it in a fixup routine Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus-fixup.c | 115 ++++++++++++++++++++----------------------- drivers/misc/mei/bus.c | 2 +- drivers/misc/mei/mei_dev.h | 2 +- 3 files changed, 55 insertions(+), 64 deletions(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus-fixup.c b/drivers/misc/mei/bus-fixup.c index fd6470f42671..a4c6719d36b4 100644 --- a/drivers/misc/mei/bus-fixup.c +++ b/drivers/misc/mei/bus-fixup.c @@ -145,7 +145,7 @@ struct mei_nfc_dev { u8 fw_ivn; u8 vendor_id; u8 radio_type; - char *bus_name; + const char *bus_name; }; /* UUIDs for NFC F/W clients */ @@ -184,69 +184,30 @@ static void mei_nfc_free(struct mei_nfc_dev *ndev) kfree(ndev); } -static int mei_nfc_build_bus_name(struct mei_nfc_dev *ndev) -{ - struct mei_device *bus; - - if (!ndev->cl) - return -ENODEV; - - bus = ndev->cl->dev; - - switch (ndev->vendor_id) { - case MEI_NFC_VENDOR_INSIDE: - switch (ndev->radio_type) { - case MEI_NFC_VENDOR_INSIDE_UREAD: - ndev->bus_name = "microread"; - return 0; - - default: - dev_err(bus->dev, "Unknown radio type 0x%x\n", - ndev->radio_type); - - return -EINVAL; - } - - case MEI_NFC_VENDOR_NXP: - switch (ndev->radio_type) { - case MEI_NFC_VENDOR_NXP_PN544: - ndev->bus_name = "pn544"; - return 0; - default: - dev_err(bus->dev, "Unknown radio type 0x%x\n", - ndev->radio_type); - - return -EINVAL; - } - - default: - dev_err(bus->dev, "Unknown vendor ID 0x%x\n", - ndev->vendor_id); - - return -EINVAL; - } - - return 0; -} - -static int mei_nfc_if_version(struct mei_nfc_dev *ndev) +/** + * mei_nfc_if_version - get NFC interface version + * + * @cl: host client (nfc info) + * @ver: NFC interface version to be filled in + * + * Return: 0 on success; < 0 otherwise + */ +static int mei_nfc_if_version(struct mei_cl *cl, + struct mei_nfc_if_version *ver) { struct mei_device *bus; - struct mei_cl *cl; - - struct mei_nfc_cmd cmd; + struct mei_nfc_cmd cmd = { + .command = MEI_NFC_CMD_MAINTENANCE, + .data_size = 1, + .sub_command = MEI_NFC_SUBCMD_IF_VERSION, + }; struct mei_nfc_reply *reply = NULL; - struct mei_nfc_if_version *version; size_t if_version_length; int bytes_recv, ret; - cl = ndev->cl_info; bus = cl->dev; - memset(&cmd, 0, sizeof(struct mei_nfc_cmd)); - cmd.command = MEI_NFC_CMD_MAINTENANCE; - cmd.data_size = 1; - cmd.sub_command = MEI_NFC_SUBCMD_IF_VERSION; + WARN_ON(mutex_is_locked(&bus->device_lock)); ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd), 1); if (ret < 0) { @@ -262,6 +223,7 @@ static int mei_nfc_if_version(struct mei_nfc_dev *ndev) if (!reply) return -ENOMEM; + ret = 0; bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length); if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) { dev_err(bus->dev, "Could not read IF version\n"); @@ -269,17 +231,39 @@ static int mei_nfc_if_version(struct mei_nfc_dev *ndev) goto err; } - version = (struct mei_nfc_if_version *)reply->data; + memcpy(ver, reply->data, sizeof(struct mei_nfc_if_version)); - ndev->fw_ivn = version->fw_ivn; - ndev->vendor_id = version->vendor_id; - ndev->radio_type = version->radio_type; + dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n", + ver->fw_ivn, ver->vendor_id, ver->radio_type); err: kfree(reply); return ret; } +/** + * mei_nfc_radio_name - derive nfc radio name from the interface version + * + * @ver: NFC radio version + * + * Return: radio name string + */ +static const char *mei_nfc_radio_name(struct mei_nfc_if_version *ver) +{ + + if (ver->vendor_id == MEI_NFC_VENDOR_INSIDE) { + if (ver->radio_type == MEI_NFC_VENDOR_INSIDE_UREAD) + return "microread"; + } + + if (ver->vendor_id == MEI_NFC_VENDOR_NXP) { + if (ver->radio_type == MEI_NFC_VENDOR_NXP_PN544) + return "pn544"; + } + + return NULL; +} + static void mei_nfc_init(struct work_struct *work) { struct mei_device *bus; @@ -287,6 +271,7 @@ static void mei_nfc_init(struct work_struct *work) struct mei_nfc_dev *ndev; struct mei_cl *cl_info; struct mei_me_client *me_cl_info; + struct mei_nfc_if_version version; ndev = container_of(work, struct mei_nfc_dev, init_work); @@ -313,12 +298,16 @@ static void mei_nfc_init(struct work_struct *work) mei_me_cl_put(me_cl_info); mutex_unlock(&bus->device_lock); - if (mei_nfc_if_version(ndev) < 0) { + if (mei_nfc_if_version(cl_info, &version) < 0) { dev_err(bus->dev, "Could not get the NFC interface version"); goto err; } + ndev->fw_ivn = version.fw_ivn; + ndev->vendor_id = version.vendor_id; + ndev->radio_type = version.radio_type; + dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n", ndev->fw_ivn, ndev->vendor_id, ndev->radio_type); @@ -333,7 +322,9 @@ static void mei_nfc_init(struct work_struct *work) mutex_unlock(&bus->device_lock); - if (mei_nfc_build_bus_name(ndev) < 0) { + ndev->bus_name = mei_nfc_radio_name(&version); + + if (!ndev->bus_name) { dev_err(bus->dev, "Could not build the bus ID name\n"); return; } diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index 68b7756bf384..7e51700515f4 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -710,7 +710,7 @@ static int mei_cl_bus_dev_add(struct mei_cl_device *cldev) struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, struct mei_me_client *me_cl, struct mei_cl *cl, - char *name) + const char *name) { struct mei_cl_device *cldev; int status; diff --git a/drivers/misc/mei/mei_dev.h b/drivers/misc/mei/mei_dev.h index ad59ab776f2d..7098dcaccc96 100644 --- a/drivers/misc/mei/mei_dev.h +++ b/drivers/misc/mei/mei_dev.h @@ -333,7 +333,7 @@ struct mei_hw_ops { struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, struct mei_me_client *me_cl, struct mei_cl *cl, - char *name); + const char *name); void mei_cl_remove_device(struct mei_cl_device *cldev); void mei_cl_dev_fixup(struct mei_cl_device *dev); ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, -- cgit v1.2.3 From 6009595a66e460af0b170d736398c49395cb4499 Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Thu, 23 Jul 2015 15:08:47 +0300 Subject: mei: bus: link client devices instead of host clients MEI bus was designed around nfc and was hard to extend. Instead of the hard coded way of adding the devices on the mei bus we scan the whole me client list and create a device for each eligible me client (mei_cl_bus_rescan); currently we support only clients with single connection and fixed address clients. NFC radio name detection is run as a fixup routine The patch replaces handling the device list based on struct me_cl to device list based on me_cl_devices. The creating a connection is pushed from the device creation time to device enablement. Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus-fixup.c | 288 ++++++++++--------------------------------- drivers/misc/mei/bus.c | 213 ++++++++++++++++++++++---------- drivers/misc/mei/client.c | 9 +- drivers/misc/mei/init.c | 2 +- drivers/misc/mei/mei_dev.h | 11 +- 5 files changed, 213 insertions(+), 310 deletions(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus-fixup.c b/drivers/misc/mei/bus-fixup.c index a4c6719d36b4..3e536ca85f7d 100644 --- a/drivers/misc/mei/bus-fixup.c +++ b/drivers/misc/mei/bus-fixup.c @@ -30,6 +30,11 @@ #define MEI_UUID_NFC_INFO UUID_LE(0xd2de1625, 0x382d, 0x417d, \ 0x48, 0xa4, 0xef, 0xab, 0xba, 0x8a, 0x12, 0x06) +static const uuid_le mei_nfc_info_guid = MEI_UUID_NFC_INFO; + +#define MEI_UUID_NFC_HCI UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, \ + 0x94, 0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c) + #define MEI_UUID_ANY NULL_UUID_LE /** @@ -93,68 +98,10 @@ struct mei_nfc_if_version { u8 radio_type; } __packed; -struct mei_nfc_connect { - u8 fw_ivn; - u8 vendor_id; -} __packed; - -struct mei_nfc_connect_resp { - u8 fw_ivn; - u8 vendor_id; - u16 me_major; - u16 me_minor; - u16 me_hotfix; - u16 me_build; -} __packed; - -struct mei_nfc_hci_hdr { - u8 cmd; - u8 status; - u16 req_id; - u32 reserved; - u16 data_size; -} __packed; #define MEI_NFC_CMD_MAINTENANCE 0x00 -#define MEI_NFC_CMD_HCI_SEND 0x01 -#define MEI_NFC_CMD_HCI_RECV 0x02 - -#define MEI_NFC_SUBCMD_CONNECT 0x00 #define MEI_NFC_SUBCMD_IF_VERSION 0x01 -#define MEI_NFC_HEADER_SIZE 10 - -/** - * struct mei_nfc_dev - NFC mei device - * - * @me_cl: NFC me client - * @cl: NFC host client - * @cl_info: NFC info host client - * @init_work: perform connection to the info client - * @fw_ivn: NFC Interface Version Number - * @vendor_id: NFC manufacturer ID - * @radio_type: NFC radio type - * @bus_name: bus name - * - */ -struct mei_nfc_dev { - struct mei_me_client *me_cl; - struct mei_cl *cl; - struct mei_cl *cl_info; - struct work_struct init_work; - u8 fw_ivn; - u8 vendor_id; - u8 radio_type; - const char *bus_name; -}; - -/* UUIDs for NFC F/W clients */ -const uuid_le mei_nfc_guid = UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, - 0x94, 0xd4, 0x50, 0x26, - 0x67, 0x23, 0x77, 0x5c); - -static const uuid_le mei_nfc_info_guid = MEI_UUID_NFC_INFO; - /* Vendors */ #define MEI_NFC_VENDOR_INSIDE 0x00 #define MEI_NFC_VENDOR_NXP 0x01 @@ -163,27 +110,6 @@ static const uuid_le mei_nfc_info_guid = MEI_UUID_NFC_INFO; #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00 #define MEI_NFC_VENDOR_NXP_PN544 0x01 -static void mei_nfc_free(struct mei_nfc_dev *ndev) -{ - if (!ndev) - return; - - if (ndev->cl) { - list_del(&ndev->cl->device_link); - mei_cl_unlink(ndev->cl); - kfree(ndev->cl); - } - - if (ndev->cl_info) { - list_del(&ndev->cl_info->device_link); - mei_cl_unlink(ndev->cl_info); - kfree(ndev->cl_info); - } - - mei_me_cl_put(ndev->me_cl); - kfree(ndev); -} - /** * mei_nfc_if_version - get NFC interface version * @@ -264,177 +190,86 @@ static const char *mei_nfc_radio_name(struct mei_nfc_if_version *ver) return NULL; } -static void mei_nfc_init(struct work_struct *work) +/** + * mei_nfc - The nfc fixup function. The function retrieves nfc radio + * name and set is as device attribute so we can load + * the proper device driver for it + * + * @cldev: me client device (nfc) + */ +static void mei_nfc(struct mei_cl_device *cldev) { struct mei_device *bus; - struct mei_cl_device *cldev; - struct mei_nfc_dev *ndev; - struct mei_cl *cl_info; - struct mei_me_client *me_cl_info; - struct mei_nfc_if_version version; + struct mei_cl *cl; + struct mei_me_client *me_cl = NULL; + struct mei_nfc_if_version ver; + const char *radio_name = NULL; + int ret; - ndev = container_of(work, struct mei_nfc_dev, init_work); + bus = cldev->bus; - cl_info = ndev->cl_info; - bus = cl_info->dev; + dev_dbg(bus->dev, "running hook %s: %pUl match=%d\n", + __func__, mei_me_cl_uuid(cldev->me_cl), cldev->do_match); mutex_lock(&bus->device_lock); - - /* check for valid client id */ - me_cl_info = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid); - if (!me_cl_info) { - mutex_unlock(&bus->device_lock); - dev_info(bus->dev, "nfc: failed to find the info client\n"); - goto err; - } - - if (mei_cl_connect(cl_info, me_cl_info, NULL) < 0) { - mei_me_cl_put(me_cl_info); - mutex_unlock(&bus->device_lock); - dev_err(bus->dev, "Could not connect to the NFC INFO ME client"); - - goto err; + /* we need to connect to INFO GUID */ + cl = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY); + if (IS_ERR(cl)) { + ret = PTR_ERR(cl); + cl = NULL; + dev_err(bus->dev, "nfc hook alloc failed %d\n", ret); + goto out; } - mei_me_cl_put(me_cl_info); - mutex_unlock(&bus->device_lock); - if (mei_nfc_if_version(cl_info, &version) < 0) { - dev_err(bus->dev, "Could not get the NFC interface version"); - - goto err; + me_cl = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid); + if (!me_cl) { + ret = -ENOTTY; + dev_err(bus->dev, "Cannot find nfc info %d\n", ret); + goto out; } - ndev->fw_ivn = version.fw_ivn; - ndev->vendor_id = version.vendor_id; - ndev->radio_type = version.radio_type; - - dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n", - ndev->fw_ivn, ndev->vendor_id, ndev->radio_type); - - mutex_lock(&bus->device_lock); - - if (mei_cl_disconnect(cl_info) < 0) { - mutex_unlock(&bus->device_lock); - dev_err(bus->dev, "Could not disconnect the NFC INFO ME client"); - - goto err; + ret = mei_cl_connect(cl, me_cl, NULL); + if (ret < 0) { + dev_err(&cldev->dev, "Can't connect to the NFC INFO ME ret = %d\n", + ret); + goto out; } mutex_unlock(&bus->device_lock); - ndev->bus_name = mei_nfc_radio_name(&version); + ret = mei_nfc_if_version(cl, &ver); + if (ret) + goto disconnect; - if (!ndev->bus_name) { - dev_err(bus->dev, "Could not build the bus ID name\n"); - return; - } + radio_name = mei_nfc_radio_name(&ver); - cldev = mei_cl_add_device(bus, ndev->me_cl, ndev->cl, - ndev->bus_name); - if (!cldev) { - dev_err(bus->dev, "Could not add the NFC device to the MEI bus\n"); - - goto err; + if (!radio_name) { + ret = -ENOENT; + dev_err(&cldev->dev, "Can't get the NFC interface version ret = %d\n", + ret); + goto disconnect; } - cldev->priv_data = ndev; - - - return; + dev_dbg(bus->dev, "nfc radio %s\n", radio_name); + strlcpy(cldev->name, radio_name, sizeof(cldev->name)); -err: +disconnect: mutex_lock(&bus->device_lock); - mei_nfc_free(ndev); - mutex_unlock(&bus->device_lock); + if (mei_cl_disconnect(cl) < 0) + dev_err(bus->dev, "Can't disconnect the NFC INFO ME\n"); -} + mei_cl_flush_queues(cl, NULL); +out: + mei_cl_unlink(cl); + mutex_unlock(&bus->device_lock); + mei_me_cl_put(me_cl); + kfree(cl); -int mei_nfc_host_init(struct mei_device *bus, struct mei_me_client *me_cl) -{ - struct mei_nfc_dev *ndev; - struct mei_cl *cl_info, *cl; - int ret; - - - /* in case of internal reset bail out - * as the device is already setup - */ - cl = mei_cl_bus_find_cl_by_uuid(bus, mei_nfc_guid); - if (cl) - return 0; - - ndev = kzalloc(sizeof(struct mei_nfc_dev), GFP_KERNEL); - if (!ndev) { - ret = -ENOMEM; - goto err; - } - - ndev->me_cl = mei_me_cl_get(me_cl); - if (!ndev->me_cl) { - ret = -ENODEV; - goto err; - } - - cl_info = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY); - if (IS_ERR(cl_info)) { - ret = PTR_ERR(cl_info); - goto err; - } - - list_add_tail(&cl_info->device_link, &bus->device_list); - - ndev->cl_info = cl_info; - - cl = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY); - if (IS_ERR(cl)) { - ret = PTR_ERR(cl); - goto err; - } - - list_add_tail(&cl->device_link, &bus->device_list); - - ndev->cl = cl; - - INIT_WORK(&ndev->init_work, mei_nfc_init); - schedule_work(&ndev->init_work); - - return 0; - -err: - mei_nfc_free(ndev); - - return ret; -} - -void mei_nfc_host_exit(struct mei_device *bus) -{ - struct mei_nfc_dev *ndev; - struct mei_cl *cl; - struct mei_cl_device *cldev; - - cl = mei_cl_bus_find_cl_by_uuid(bus, mei_nfc_guid); - if (!cl) - return; - - cldev = cl->cldev; - if (!cldev) - return; - - ndev = (struct mei_nfc_dev *)cldev->priv_data; - if (ndev) - cancel_work_sync(&ndev->init_work); - - cldev->priv_data = NULL; - - /* Need to remove the device here - * since mei_nfc_free will unlink the clients - */ - mei_cl_remove_device(cldev); + if (ret) + cldev->do_match = 0; - mutex_lock(&bus->device_lock); - mei_nfc_free(ndev); - mutex_unlock(&bus->device_lock); + dev_dbg(bus->dev, "end of fixup match = %d\n", cldev->do_match); } #define MEI_FIXUP(_uuid, _hook) { _uuid, _hook } @@ -446,6 +281,7 @@ static struct mei_fixup { } mei_fixups[] = { MEI_FIXUP(MEI_UUID_ANY, number_of_connections), MEI_FIXUP(MEI_UUID_NFC_INFO, blacklist), + MEI_FIXUP(MEI_UUID_NFC_HCI, mei_nfc), }; /** diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index 7e51700515f4..3ab08e522fb8 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -309,34 +309,43 @@ EXPORT_SYMBOL_GPL(mei_cl_set_drvdata); */ int mei_cl_enable_device(struct mei_cl_device *cldev) { - int err; - struct mei_device *bus; - struct mei_cl *cl = cldev->cl; + struct mei_device *bus = cldev->bus; + struct mei_cl *cl; + int ret; - if (cl == NULL) - return -ENODEV; + cl = cldev->cl; - bus = cl->dev; + if (!cl) { + mutex_lock(&bus->device_lock); + cl = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY); + mutex_unlock(&bus->device_lock); + if (IS_ERR(cl)) + return PTR_ERR(cl); + /* update pointers */ + cldev->cl = cl; + cl->cldev = cldev; + } mutex_lock(&bus->device_lock); - if (mei_cl_is_connected(cl)) { - mutex_unlock(&bus->device_lock); - dev_warn(bus->dev, "Already connected"); - return -EBUSY; + ret = 0; + goto out; } - err = mei_cl_connect(cl, cldev->me_cl, NULL); - if (err < 0) { - mutex_unlock(&bus->device_lock); - dev_err(bus->dev, "Could not connect to the ME client"); - - return err; + if (!mei_me_cl_is_active(cldev->me_cl)) { + dev_err(&cldev->dev, "me client is not active\n"); + ret = -ENOTTY; + goto out; } + ret = mei_cl_connect(cl, cldev->me_cl, NULL); + if (ret < 0) + dev_err(&cldev->dev, "cannot connect\n"); + +out: mutex_unlock(&bus->device_lock); - return 0; + return ret; } EXPORT_SYMBOL_GPL(mei_cl_enable_device); @@ -350,14 +359,16 @@ EXPORT_SYMBOL_GPL(mei_cl_enable_device); */ int mei_cl_disable_device(struct mei_cl_device *cldev) { - int err; struct mei_device *bus; - struct mei_cl *cl = cldev->cl; + struct mei_cl *cl; + int err; - if (cl == NULL) + if (!cldev || !cldev->cl) return -ENODEV; - bus = cl->dev; + cl = cldev->cl; + + bus = cldev->bus; cldev->event_cb = NULL; @@ -370,18 +381,19 @@ int mei_cl_disable_device(struct mei_cl_device *cldev) } err = mei_cl_disconnect(cl); - if (err < 0) { + if (err < 0) dev_err(bus->dev, "Could not disconnect from the ME client"); - goto out; - } +out: /* Flush queues and remove any pending read */ mei_cl_flush_queues(cl, NULL); + mei_cl_unlink(cl); + + kfree(cl); + cldev->cl = NULL; -out: mutex_unlock(&bus->device_lock); return err; - } EXPORT_SYMBOL_GPL(mei_cl_disable_device); @@ -623,20 +635,6 @@ static struct device_type mei_cl_device_type = { .release = mei_cl_dev_release, }; -struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *bus, - uuid_le uuid) -{ - struct mei_cl *cl; - - list_for_each_entry(cl, &bus->device_list, device_link) { - if (cl->cldev && cl->cldev->me_cl && - !uuid_le_cmp(uuid, *mei_me_cl_uuid(cl->cldev->me_cl))) - return cl; - } - - return NULL; -} - /** * mei_cl_dev_alloc - initialize and allocate mei client device * @@ -707,45 +705,127 @@ static int mei_cl_bus_dev_add(struct mei_cl_device *cldev) return ret; } -struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, - struct mei_me_client *me_cl, - struct mei_cl *cl, - const char *name) +/** + * mei_cl_bus_dev_stop - stop the driver + * + * @cldev: me client device + */ +static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev) +{ + if (cldev->is_added) + device_release_driver(&cldev->dev); +} + +/** + * mei_cl_bus_dev_destroy - destroy me client devices object + * + * @cldev: me client device + */ +static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev) +{ + if (!cldev->is_added) + return; + + device_del(&cldev->dev); + + mutex_lock(&cldev->bus->cl_bus_lock); + list_del_init(&cldev->bus_list); + mutex_unlock(&cldev->bus->cl_bus_lock); + + cldev->is_added = 0; + put_device(&cldev->dev); +} + +/** + * mei_cl_bus_remove_device - remove a devices form the bus + * + * @cldev: me client device + */ +static void mei_cl_bus_remove_device(struct mei_cl_device *cldev) +{ + mei_cl_bus_dev_stop(cldev); + mei_cl_bus_dev_destroy(cldev); +} + +/** + * mei_cl_bus_remove_devices - remove all devices form the bus + * + * @bus: mei device + */ +void mei_cl_bus_remove_devices(struct mei_device *bus) +{ + struct mei_cl_device *cldev, *next; + + list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list) + mei_cl_bus_remove_device(cldev); +} + + +/** + * mei_cl_dev_init - allocate and initializes an mei client devices + * based on me client + * + * @bus: mei device + * @me_cl: me client + */ +static void mei_cl_dev_init(struct mei_device *bus, struct mei_me_client *me_cl) { struct mei_cl_device *cldev; - int status; + + dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl)); + + if (me_cl->bus_added) + return; cldev = mei_cl_dev_alloc(bus, me_cl); if (!cldev) - return NULL; + return; - cldev->cl = cl; - strlcpy(cldev->name, name, sizeof(cldev->name)); + mutex_lock(&cldev->bus->cl_bus_lock); + me_cl->bus_added = true; + list_add_tail(&cldev->bus_list, &bus->device_list); + mutex_unlock(&cldev->bus->cl_bus_lock); - mei_cl_dev_setup(bus, cldev); +} - status = mei_cl_bus_dev_add(cldev); - if (status) { - dev_err(bus->dev, "Failed to register MEI device\n"); - mei_me_cl_put(cldev->me_cl); - mei_dev_bus_put(bus); - kfree(cldev); - return NULL; - } +/** + * mei_cl_bus_rescan - scan me clients list and add create + * devices for eligible clients + * + * @bus: mei device + */ +void mei_cl_bus_rescan(struct mei_device *bus) +{ + struct mei_cl_device *cldev, *n; + struct mei_me_client *me_cl; - cl->cldev = cldev; + down_read(&bus->me_clients_rwsem); + list_for_each_entry(me_cl, &bus->me_clients, list) + mei_cl_dev_init(bus, me_cl); + up_read(&bus->me_clients_rwsem); - dev_dbg(&cldev->dev, "client %s registered\n", name); + mutex_lock(&bus->cl_bus_lock); + list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) { - return cldev; -} -EXPORT_SYMBOL_GPL(mei_cl_add_device); + if (!mei_me_cl_is_active(cldev->me_cl)) { + mei_cl_bus_remove_device(cldev); + continue; + } -void mei_cl_remove_device(struct mei_cl_device *cldev) -{ - device_unregister(&cldev->dev); + if (cldev->is_added) + continue; + + if (mei_cl_dev_setup(bus, cldev)) + mei_cl_bus_dev_add(cldev); + else { + list_del_init(&cldev->bus_list); + put_device(&cldev->dev); + } + } + mutex_unlock(&bus->cl_bus_lock); + + dev_dbg(bus->dev, "rescan end"); } -EXPORT_SYMBOL_GPL(mei_cl_remove_device); int __mei_cl_driver_register(struct mei_cl_driver *cldrv, struct module *owner) { @@ -773,6 +853,7 @@ void mei_cl_driver_unregister(struct mei_cl_driver *cldrv) } EXPORT_SYMBOL_GPL(mei_cl_driver_unregister); + int __init mei_cl_bus_init(void) { return bus_register(&mei_cl_bus_type); diff --git a/drivers/misc/mei/client.c b/drivers/misc/mei/client.c index 1987a201c7f3..9dacea7a9a60 100644 --- a/drivers/misc/mei/client.c +++ b/drivers/misc/mei/client.c @@ -558,7 +558,6 @@ void mei_cl_init(struct mei_cl *cl, struct mei_device *dev) INIT_LIST_HEAD(&cl->rd_completed); INIT_LIST_HEAD(&cl->rd_pending); INIT_LIST_HEAD(&cl->link); - INIT_LIST_HEAD(&cl->device_link); cl->writing_state = MEI_IDLE; cl->state = MEI_FILE_INITIALIZING; cl->dev = dev; @@ -690,16 +689,12 @@ void mei_host_client_init(struct work_struct *work) mei_wd_host_init(dev, me_cl); mei_me_cl_put(me_cl); - me_cl = mei_me_cl_by_uuid(dev, &mei_nfc_guid); - if (me_cl) - mei_nfc_host_init(dev, me_cl); - mei_me_cl_put(me_cl); - - dev->dev_state = MEI_DEV_ENABLED; dev->reset_count = 0; mutex_unlock(&dev->device_lock); + mei_cl_bus_rescan(dev); + pm_runtime_mark_last_busy(dev->dev); dev_dbg(dev->dev, "rpm: autosuspend\n"); pm_runtime_autosuspend(dev->dev); diff --git a/drivers/misc/mei/init.c b/drivers/misc/mei/init.c index 15000e9231b1..e374661652cd 100644 --- a/drivers/misc/mei/init.c +++ b/drivers/misc/mei/init.c @@ -331,7 +331,7 @@ void mei_stop(struct mei_device *dev) mei_cancel_work(dev); - mei_nfc_host_exit(dev); + mei_cl_bus_remove_devices(dev); mutex_lock(&dev->device_lock); diff --git a/drivers/misc/mei/mei_dev.h b/drivers/misc/mei/mei_dev.h index 7098dcaccc96..71c55f4cabb4 100644 --- a/drivers/misc/mei/mei_dev.h +++ b/drivers/misc/mei/mei_dev.h @@ -241,7 +241,6 @@ struct mei_cl_cb { * @rd_completed: completed read * * @cldev: device on the mei client bus - * @device_link: link to bus clients */ struct mei_cl { struct list_head link; @@ -260,9 +259,7 @@ struct mei_cl { struct list_head rd_pending; struct list_head rd_completed; - /* MEI CL bus data */ struct mei_cl_device *cldev; - struct list_head device_link; }; /** struct mei_hw_ops @@ -329,12 +326,7 @@ struct mei_hw_ops { }; /* MEI bus API*/ - -struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, - struct mei_me_client *me_cl, - struct mei_cl *cl, - const char *name); -void mei_cl_remove_device(struct mei_cl_device *cldev); +void mei_cl_bus_rescan(struct mei_device *bus); void mei_cl_dev_fixup(struct mei_cl_device *dev); ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, bool blocking); @@ -343,7 +335,6 @@ void mei_cl_bus_rx_event(struct mei_cl *cl); void mei_cl_bus_remove_devices(struct mei_device *bus); int mei_cl_bus_init(void); void mei_cl_bus_exit(void); -struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *bus, uuid_le uuid); /** * enum mei_pg_event - power gating transition events -- cgit v1.2.3 From bb2ef9c39db2e3c2562b4e439b2b00dc42e2c026 Mon Sep 17 00:00:00 2001 From: Alexander Usyskin Date: Sun, 26 Jul 2015 09:54:23 +0300 Subject: mei: bus: add and call callback on notify event Enable drivers on mei client bus to subscribe to asynchronous event notifications. Introduce events_mask to the existing callback infrastructure so it is possible to handle both RX and event notification. Signed-off-by: Alexander Usyskin Signed-off-by: Tomas Winkler Signed-off-by: Greg Kroah-Hartman --- drivers/misc/mei/bus.c | 50 ++++++++++++++++++++++++++++++++++++++++++---- drivers/misc/mei/client.c | 2 ++ drivers/misc/mei/mei_dev.h | 1 + drivers/nfc/mei_phy.c | 3 ++- include/linux/mei_cl_bus.h | 4 ++++ 5 files changed, 55 insertions(+), 5 deletions(-) (limited to 'drivers/misc/mei/bus.c') diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c index 3ab08e522fb8..eef1c6b46ad8 100644 --- a/drivers/misc/mei/bus.c +++ b/drivers/misc/mei/bus.c @@ -222,7 +222,33 @@ static void mei_bus_event_work(struct work_struct *work) cldev->events = 0; /* Prepare for the next read */ - mei_cl_read_start(cldev->cl, 0, NULL); + if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) + mei_cl_read_start(cldev->cl, 0, NULL); +} + +/** + * mei_cl_bus_notify_event - schedule notify cb on bus client + * + * @cl: host client + */ +void mei_cl_bus_notify_event(struct mei_cl *cl) +{ + struct mei_cl_device *cldev = cl->cldev; + + if (!cldev || !cldev->event_cb) + return; + + if (!(cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF))) + return; + + if (!cl->notify_ev) + return; + + set_bit(MEI_CL_EVENT_NOTIF, &cldev->events); + + schedule_work(&cldev->event_work); + + cl->notify_ev = false; } /** @@ -237,6 +263,9 @@ void mei_cl_bus_rx_event(struct mei_cl *cl) if (!cldev || !cldev->event_cb) return; + if (!(cldev->events_mask & BIT(MEI_CL_EVENT_RX))) + return; + set_bit(MEI_CL_EVENT_RX, &cldev->events); schedule_work(&cldev->event_work); @@ -247,6 +276,7 @@ void mei_cl_bus_rx_event(struct mei_cl *cl) * * @cldev: me client devices * @event_cb: callback function + * @events_mask: requested events bitmask * @context: driver context data * * Return: 0 on success @@ -254,6 +284,7 @@ void mei_cl_bus_rx_event(struct mei_cl *cl) * <0 on other errors */ int mei_cl_register_event_cb(struct mei_cl_device *cldev, + unsigned long events_mask, mei_cl_event_cb_t event_cb, void *context) { int ret; @@ -262,13 +293,24 @@ int mei_cl_register_event_cb(struct mei_cl_device *cldev, return -EALREADY; cldev->events = 0; + cldev->events_mask = events_mask; cldev->event_cb = event_cb; cldev->event_context = context; INIT_WORK(&cldev->event_work, mei_bus_event_work); - ret = mei_cl_read_start(cldev->cl, 0, NULL); - if (ret && ret != -EBUSY) - return ret; + if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) { + ret = mei_cl_read_start(cldev->cl, 0, NULL); + if (ret && ret != -EBUSY) + return ret; + } + + if (cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)) { + mutex_lock(&cldev->cl->dev->device_lock); + ret = mei_cl_notify_request(cldev->cl, NULL, event_cb ? 1 : 0); + mutex_unlock(&cldev->cl->dev->device_lock); + if (ret) + return ret; + } return 0; } diff --git a/drivers/misc/mei/client.c b/drivers/misc/mei/client.c index db2436aee2dc..5fcd70bcdf96 100644 --- a/drivers/misc/mei/client.c +++ b/drivers/misc/mei/client.c @@ -1375,6 +1375,8 @@ void mei_cl_notify(struct mei_cl *cl) if (cl->ev_async) kill_fasync(&cl->ev_async, SIGIO, POLL_PRI); + + mei_cl_bus_notify_event(cl); } /** diff --git a/drivers/misc/mei/mei_dev.h b/drivers/misc/mei/mei_dev.h index c960aaa538c0..e25ee16c658e 100644 --- a/drivers/misc/mei/mei_dev.h +++ b/drivers/misc/mei/mei_dev.h @@ -345,6 +345,7 @@ ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, bool blocking); ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length); void mei_cl_bus_rx_event(struct mei_cl *cl); +void mei_cl_bus_notify_event(struct mei_cl *cl); void mei_cl_bus_remove_devices(struct mei_device *bus); int mei_cl_bus_init(void); void mei_cl_bus_exit(void); diff --git a/drivers/nfc/mei_phy.c b/drivers/nfc/mei_phy.c index 2b77ccf77f81..754a9bb0f58d 100644 --- a/drivers/nfc/mei_phy.c +++ b/drivers/nfc/mei_phy.c @@ -355,7 +355,8 @@ static int nfc_mei_phy_enable(void *phy_id) goto err; } - r = mei_cl_register_event_cb(phy->device, nfc_mei_event_cb, phy); + r = mei_cl_register_event_cb(phy->device, BIT(MEI_CL_EVENT_RX), + nfc_mei_event_cb, phy); if (r) { pr_err("Event cb registration failed %d\n", r); goto err; diff --git a/include/linux/mei_cl_bus.h b/include/linux/mei_cl_bus.h index 81ab56dd0ae0..0962b2ca628a 100644 --- a/include/linux/mei_cl_bus.h +++ b/include/linux/mei_cl_bus.h @@ -28,6 +28,7 @@ typedef void (*mei_cl_event_cb_t)(struct mei_cl_device *device, * @event_cb: Drivers register this callback to get asynchronous ME * events (e.g. Rx buffer pending) notifications. * @event_context: event callback run context + * @events_mask: Events bit mask requested by driver. * @events: Events bitmask sent to the driver. * * @do_match: wheather device can be matched with a driver @@ -46,6 +47,7 @@ struct mei_cl_device { struct work_struct event_work; mei_cl_event_cb_t event_cb; void *event_context; + unsigned long events_mask; unsigned long events; unsigned int do_match:1; @@ -76,10 +78,12 @@ ssize_t mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length); ssize_t mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length); int mei_cl_register_event_cb(struct mei_cl_device *device, + unsigned long event_mask, mei_cl_event_cb_t read_cb, void *context); #define MEI_CL_EVENT_RX 0 #define MEI_CL_EVENT_TX 1 +#define MEI_CL_EVENT_NOTIF 2 void *mei_cl_get_drvdata(const struct mei_cl_device *device); void mei_cl_set_drvdata(struct mei_cl_device *device, void *data); -- cgit v1.2.3