All of lore.kernel.org
 help / color / mirror / Atom feed
* [char-misc-next 00/17] revamp mei bus
@ 2015-06-13 19:11 Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 01/17] MAINTAINERS: mei: add mei_cl_bus.h to maintained file list Tomas Winkler
                   ` (16 more replies)
  0 siblings, 17 replies; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

MEI bus was designed around nfc and was hard to extend.
Instead of hard coded way of adding the devices on the mei bus
we scan whole me client list and create a device for each
eligible me client


Tomas Winkler (17):
  MAINTAINERS: mei: add mei_cl_bus.h to maintained file list
  mei: bus: fix drivers and devices names confusion
  mei: bus: rename device_list to simple devices
  mei: bus: rename nfc.c to bus-fixup.c
  mei: bus: move driver api functions at the start of the file
  mei: bus: add kdoc for device attribute functions
  mei: bus: don't enable events implicitly in device enable
  mei: bus: report if event registration failed
  mei: bus: simplify device matching
  mei: bus: revamp probe and remove functions
  mei: bus: add reference to bus device in struct mei_cl_client
  mei: bus: add me client device list infrastructure
  mei: bus: enable running fixup routines before device registration
  mei: bus: blacklist clients by number of connections and nfc info
    client
  mei: bus: simplify how we build nfc bus name
  mei: bus: also unregister the driver when removing the device
  mei: bus: link client devices instead of host clients

 MAINTAINERS                  |   1 +
 drivers/misc/mei/Makefile    |   2 +-
 drivers/misc/mei/bus-fixup.c | 306 +++++++++++++
 drivers/misc/mei/bus.c       | 994 ++++++++++++++++++++++++++++---------------
 drivers/misc/mei/client.c    |   9 +-
 drivers/misc/mei/init.c      |   5 +-
 drivers/misc/mei/mei_dev.h   |  30 +-
 drivers/misc/mei/nfc.c       | 414 ------------------
 include/linux/mei_cl_bus.h   |  11 +
 9 files changed, 986 insertions(+), 786 deletions(-)
 create mode 100644 drivers/misc/mei/bus-fixup.c
 delete mode 100644 drivers/misc/mei/nfc.c

-- 
2.4.2


^ permalink raw reply	[flat|nested] 25+ messages in thread

* [char-misc-next 01/17] MAINTAINERS: mei: add mei_cl_bus.h to maintained file list
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 02/17] mei: bus: fix drivers and devices names confusion Tomas Winkler
                   ` (15 subsequent siblings)
  16 siblings, 0 replies; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

mei_cl_bus.h was overlooked when we added the mei bus

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index f6ba62c3db82..c58e2f0612e7 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5279,6 +5279,7 @@ M:	Tomas Winkler <tomas.winkler@intel.com>
 L:	linux-kernel@vger.kernel.org
 S:	Supported
 F:	include/uapi/linux/mei.h
+F:	include/linux/mei_cl_bus.h
 F:	drivers/misc/mei/*
 F:	Documentation/misc-devices/mei/*
 
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 02/17] mei: bus: fix drivers and devices names confusion
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 01/17] MAINTAINERS: mei: add mei_cl_bus.h to maintained file list Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 21:21   ` Greg KH
  2015-06-13 19:11 ` [char-misc-next 03/17] mei: bus: rename device_list to simple devices Tomas Winkler
                   ` (14 subsequent siblings)
  16 siblings, 1 reply; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

the variables of type mei_cl_device are now cldev
the variables of type mei_cl_driver are now cldrv
the variables of type mei_device are now bus in the
bus layer context

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus.c     | 272 ++++++++++++++++++++++-----------------------
 drivers/misc/mei/mei_dev.h |  12 +-
 drivers/misc/mei/nfc.c     |  80 ++++++-------
 3 files changed, 182 insertions(+), 182 deletions(-)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index 357b6ae4d207..a40be68dd7aa 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,30 +542,30 @@ 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);
 }
 
-void mei_cl_bus_remove_devices(struct mei_device *dev)
+void mei_cl_bus_remove_devices(struct mei_device *bus)
 {
 	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);
+	mutex_lock(&bus->device_lock);
+	list_for_each_entry_safe(cl, next, &bus->device_list, device_link) {
+		if (cl->cldev)
+			mei_cl_remove_device(cl->cldev);
 
 		list_del(&cl->device_link);
 		mei_cl_unlink(cl);
 		kfree(cl);
 	}
-	mutex_unlock(&dev->device_lock);
+	mutex_unlock(&bus->device_lock);
 }
 
 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 b983c4ecad38..51a864a2b2b5 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;
 
@@ -402,13 +402,13 @@ void mei_nfc_host_exit(struct mei_device *dev)
 
 	cldev->priv_data = NULL;
 
-	mutex_lock(&dev->device_lock);
+	mutex_lock(&bus->device_lock);
 	/* Need to remove the device here
 	 * since mei_nfc_free will unlink the clients
 	 */
 	mei_cl_remove_device(cldev);
 	mei_nfc_free(ndev);
-	mutex_unlock(&dev->device_lock);
+	mutex_unlock(&bus->device_lock);
 }
 
 
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 03/17] mei: bus: rename device_list to simple devices
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 01/17] MAINTAINERS: mei: add mei_cl_bus.h to maintained file list Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 02/17] mei: bus: fix drivers and devices names confusion Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 21:21   ` Greg KH
  2015-06-13 19:11 ` [char-misc-next 04/17] mei: bus: rename nfc.c to bus-fixup.c Tomas Winkler
                   ` (13 subsequent siblings)
  16 siblings, 1 reply; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus.c     | 4 ++--
 drivers/misc/mei/init.c    | 2 +-
 drivers/misc/mei/mei_dev.h | 5 ++---
 drivers/misc/mei/nfc.c     | 4 ++--
 4 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index a40be68dd7aa..2ae7e521f029 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -203,7 +203,7 @@ struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *bus,
 {
 	struct mei_cl *cl;
 
-	list_for_each_entry(cl, &bus->device_list, device_link) {
+	list_for_each_entry(cl, &bus->devices, device_link) {
 		if (cl->cldev && cl->cldev->me_cl &&
 		    !uuid_le_cmp(uuid, *mei_me_cl_uuid(cl->cldev->me_cl)))
 			return cl;
@@ -557,7 +557,7 @@ void mei_cl_bus_remove_devices(struct mei_device *bus)
 	struct mei_cl *cl, *next;
 
 	mutex_lock(&bus->device_lock);
-	list_for_each_entry_safe(cl, next, &bus->device_list, device_link) {
+	list_for_each_entry_safe(cl, next, &bus->devices, device_link) {
 		if (cl->cldev)
 			mei_cl_remove_device(cl->cldev);
 
diff --git a/drivers/misc/mei/init.c b/drivers/misc/mei/init.c
index 94514b2c7a50..64331a5ce990 100644
--- a/drivers/misc/mei/init.c
+++ b/drivers/misc/mei/init.c
@@ -388,7 +388,7 @@ void mei_device_init(struct mei_device *dev,
 {
 	/* setup our list array */
 	INIT_LIST_HEAD(&dev->file_list);
-	INIT_LIST_HEAD(&dev->device_list);
+	INIT_LIST_HEAD(&dev->devices);
 	INIT_LIST_HEAD(&dev->me_clients);
 	mutex_init(&dev->device_lock);
 	init_rwsem(&dev->me_clients_rwsem);
diff --git a/drivers/misc/mei/mei_dev.h b/drivers/misc/mei/mei_dev.h
index bc65fb42aea9..2a1ab7958734 100644
--- a/drivers/misc/mei/mei_dev.h
+++ b/drivers/misc/mei/mei_dev.h
@@ -446,7 +446,7 @@ const char *mei_pg_state_str(enum mei_pg_state state);
  * @init_work   : work item for the device init
  * @reset_work  : work item for the device reset
  *
- * @device_list : mei client bus list
+ * @devices     : mei client bus list
  *
  * @dbgfs_dir   : debugfs mei root directory
  *
@@ -541,8 +541,7 @@ struct mei_device {
 	struct work_struct init_work;
 	struct work_struct reset_work;
 
-	/* List of bus devices */
-	struct list_head device_list;
+	struct list_head devices;
 
 #if IS_ENABLED(CONFIG_DEBUG_FS)
 	struct dentry *dbgfs_dir;
diff --git a/drivers/misc/mei/nfc.c b/drivers/misc/mei/nfc.c
index 51a864a2b2b5..9bd76cda1608 100644
--- a/drivers/misc/mei/nfc.c
+++ b/drivers/misc/mei/nfc.c
@@ -357,7 +357,7 @@ int mei_nfc_host_init(struct mei_device *bus, struct mei_me_client *me_cl)
 		goto err;
 	}
 
-	list_add_tail(&cl_info->device_link, &bus->device_list);
+	list_add_tail(&cl_info->device_link, &bus->devices);
 
 	ndev->cl_info = cl_info;
 
@@ -367,7 +367,7 @@ int mei_nfc_host_init(struct mei_device *bus, struct mei_me_client *me_cl)
 		goto err;
 	}
 
-	list_add_tail(&cl->device_link, &bus->device_list);
+	list_add_tail(&cl->device_link, &bus->devices);
 
 	ndev->cl = cl;
 
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 04/17] mei: bus: rename nfc.c to bus-fixup.c
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (2 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 03/17] mei: bus: rename device_list to simple devices Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 21:22   ` Greg KH
  2015-06-13 19:11 ` [char-misc-next 05/17] mei: bus: move driver api functions at the start of the file Tomas Winkler
                   ` (12 subsequent siblings)
  16 siblings, 1 reply; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

In nfc.c we just create device name on the mei client bus
we make it more generic in the following patches

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 drivers/misc/mei/Makefile               | 2 +-
 drivers/misc/mei/{nfc.c => bus-fixup.c} | 0
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename drivers/misc/mei/{nfc.c => bus-fixup.c} (100%)

diff --git a/drivers/misc/mei/Makefile b/drivers/misc/mei/Makefile
index 518914a82b83..01447ca21c26 100644
--- a/drivers/misc/mei/Makefile
+++ b/drivers/misc/mei/Makefile
@@ -11,7 +11,7 @@ mei-objs += main.o
 mei-objs += amthif.o
 mei-objs += wd.o
 mei-objs += bus.o
-mei-objs += nfc.o
+mei-objs += bus-fixup.o
 mei-$(CONFIG_DEBUG_FS) += debugfs.o
 
 obj-$(CONFIG_INTEL_MEI_ME) += mei-me.o
diff --git a/drivers/misc/mei/nfc.c b/drivers/misc/mei/bus-fixup.c
similarity index 100%
rename from drivers/misc/mei/nfc.c
rename to drivers/misc/mei/bus-fixup.c
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 05/17] mei: bus: move driver api functions at the start of the file
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (3 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 04/17] mei: bus: rename nfc.c to bus-fixup.c Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 21:22   ` Greg KH
  2015-06-13 19:11 ` [char-misc-next 06/17] mei: bus: add kdoc for device attribute functions Tomas Winkler
                   ` (11 subsequent siblings)
  16 siblings, 1 reply; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

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 <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus.c | 798 +++++++++++++++++++++++++++----------------------
 1 file changed, 443 insertions(+), 355 deletions(-)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index 2ae7e521f029..b14662787d7e 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -30,527 +30,615 @@
 #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;
+	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->devices, 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->devices, 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);
 
 void mei_cl_bus_remove_devices(struct mei_device *bus)
 {
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 06/17] mei: bus: add kdoc for device attribute functions
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (4 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 05/17] mei: bus: move driver api functions at the start of the file Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 21:23   ` Greg KH
  2015-06-13 19:11 ` [char-misc-next 07/17] mei: bus: don't enable events implicitly in device enable Tomas Winkler
                   ` (10 subsequent siblings)
  16 siblings, 1 reply; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

1. Rename uevent handler to mei_cl_device_uevent
2. Add kdoc for uevent and mod alias functions

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus.c | 43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index b14662787d7e..201019c5f766 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -462,8 +462,17 @@ static int mei_cl_device_remove(struct device *dev)
 	return cldrv->remove(cldev);
 }
 
+/**
+ * name_show - client device name sysfs entry
+ *
+ * @dev: device
+ * @a: device attribute
+ * @buf: a buffer to fill
+ *
+ * Return: bytes to read or < 0 on error
+ */
 static ssize_t name_show(struct device *dev, struct device_attribute *a,
-			     char *buf)
+			 char *buf)
 {
 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
 	size_t len;
@@ -474,8 +483,17 @@ static ssize_t name_show(struct device *dev, struct device_attribute *a,
 }
 static DEVICE_ATTR_RO(name);
 
+/**
+ * uuid_show - client uuid sysfs entry
+ *
+ * @dev: device
+ * @a: device attribute
+ * @buf: a buffer to fill
+ *
+ * Return: bytes to read or < 0 on error
+ */
 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
-			     char *buf)
+			 char *buf)
 {
 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
 	const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
@@ -487,6 +505,15 @@ static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
 }
 static DEVICE_ATTR_RO(uuid);
 
+/**
+ * modalias_show - modalias sysfs entry
+ *
+ * @dev: device
+ * @a: device attribute
+ * @buf: a buffer to fill
+ *
+ * Return: bytes to read or < 0 on error
+ */
 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
 			     char *buf)
 {
@@ -509,7 +536,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);
@@ -533,7 +568,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)
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 07/17] mei: bus: don't enable events implicitly in device enable
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (5 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 06/17] mei: bus: add kdoc for device attribute functions Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 21:24   ` Greg KH
  2015-06-13 19:11 ` [char-misc-next 08/17] mei: bus: report if event registration failed Tomas Winkler
                   ` (9 subsequent siblings)
  16 siblings, 1 reply; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

Always call mei_cl_register_event_cb explicitly so the events
are enabled only when needed. The nfc driver is already using
it that way so no need for further changes.

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index 201019c5f766..9069d89b93d7 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -331,9 +331,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);
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 08/17] mei: bus: report if event registration failed
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (6 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 07/17] mei: bus: don't enable events implicitly in device enable Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 09/17] mei: bus: simplify device matching Tomas Winkler
                   ` (8 subsequent siblings)
  16 siblings, 0 replies; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index 9069d89b93d7..5bd16d4417bb 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -255,6 +255,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;
 
@@ -263,7 +265,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;
 }
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 09/17] mei: bus: simplify device matching
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (7 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 08/17] mei: bus: report if event registration failed Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 10/17] mei: bus: revamp probe and remove functions Tomas Winkler
                   ` (7 subsequent siblings)
  16 siblings, 0 replies; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

mei_cl_device_match now calls mei_cl_device_find that returns
the matching device id in the device id table

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus.c | 63 +++++++++++++++++++++++++++++++++++---------------
 1 file changed, 44 insertions(+), 19 deletions(-)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index 5bd16d4417bb..1a9c8d7906c9 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -384,39 +384,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;
 }
 
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 10/17] mei: bus: revamp probe and remove functions
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (8 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 09/17] mei: bus: simplify device matching Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 11/17] mei: bus: add reference to bus device in struct mei_cl_client Tomas Winkler
                   ` (6 subsequent siblings)
  16 siblings, 0 replies; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

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 <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus.c | 41 ++++++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index 1a9c8d7906c9..ee0a9e50ac76 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -445,30 +445,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;
@@ -479,13 +498,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);
 }
 
 /**
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 11/17] mei: bus: add reference to bus device in struct mei_cl_client
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (9 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 10/17] mei: bus: revamp probe and remove functions Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 12/17] mei: bus: add me client device list infrastructure Tomas Winkler
                   ` (5 subsequent siblings)
  16 siblings, 0 replies; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

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 <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus.c     | 17 +++++++++++++++++
 include/linux/mei_cl_bus.h |  3 +++
 2 files changed, 20 insertions(+)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index ee0a9e50ac76..2bed403b87f7 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -616,6 +616,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);
@@ -624,6 +638,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);
 }
 
@@ -667,6 +682,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));
 
@@ -676,6 +692,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 <linux/mod_devicetable.h>
 
 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;
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 12/17] mei: bus: add me client device list infrastructure
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (10 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 11/17] mei: bus: add reference to bus device in struct mei_cl_client Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 13/17] mei: bus: enable running fixup routines before device registration Tomas Winkler
                   ` (4 subsequent siblings)
  16 siblings, 0 replies; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

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 may postpone the creating of 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 list
across the bus rescans

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 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(-)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index 2bed403b87f7..592f2a1651bf 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -683,6 +683,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 64331a5ce990..6fbd13c10381 100644
--- a/drivers/misc/mei/init.c
+++ b/drivers/misc/mei/init.c
@@ -392,6 +392,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 2a1ab7958734..4ce11c98b61a 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;
 };
 
 
@@ -446,6 +446,7 @@ const char *mei_pg_state_str(enum mei_pg_state state);
  * @init_work   : work item for the device init
  * @reset_work  : work item for the device reset
  *
+ * @cl_bus_lock : client bus list lock
  * @devices     : mei client bus list
  *
  * @dbgfs_dir   : debugfs mei root directory
@@ -541,6 +542,7 @@ struct mei_device {
 	struct work_struct init_work;
 	struct work_struct reset_work;
 
+	struct mutex cl_bus_lock;
 	struct list_head devices;
 
 #if IS_ENABLED(CONFIG_DEBUG_FS)
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;
 };
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 13/17] mei: bus: enable running fixup routines before device registration
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (11 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 12/17] mei: bus: add me client device list infrastructure Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 14/17] mei: bus: blacklist clients by number of connections and nfc info client Tomas Winkler
                   ` (3 subsequent siblings)
  16 siblings, 0 replies; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

We split the device registration to allocation and device struct
initialization , setup, and final registration.
This way we can run fixups and quirks during the setup stage on
an initialized device. If a fixup sets do_match flag to false
the device won't be registered on the bus.

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 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(-)

diff --git a/drivers/misc/mei/bus-fixup.c b/drivers/misc/mei/bus-fixup.c
index 9bd76cda1608..2c02a338f63b 100644
--- a/drivers/misc/mei/bus-fixup.c
+++ b/drivers/misc/mei/bus-fixup.c
@@ -20,12 +20,15 @@
 #include <linux/moduleparam.h>
 #include <linux/device.h>
 #include <linux/slab.h>
+#include <linux/uuid.h>
 
 #include <linux/mei_cl_bus.h>
 
 #include "mei_dev.h"
 #include "client.h"
 
+#define MEI_UUID_ANY NULL_UUID_LE
+
 struct mei_nfc_cmd {
 	u8 command;
 	u8 status;
@@ -411,4 +414,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 592f2a1651bf..8ff824950597 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -435,6 +435,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;
 
@@ -660,6 +663,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,
@@ -668,28 +741,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 4ce11c98b61a..24c70fada758 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;
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 14/17] mei: bus: blacklist clients by number of connections and nfc info client
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (12 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 13/17] mei: bus: enable running fixup routines before device registration Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 15/17] mei: bus: simplify how we build nfc bus name Tomas Winkler
                   ` (2 subsequent siblings)
  16 siblings, 0 replies; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

Currently we support only clients with single connection
and fixed address clients so all other clients are blacklisted

Blacklist nfc info client which is only used for retrieval
of the NFC radio version

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus-fixup.c | 42 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/drivers/misc/mei/bus-fixup.c b/drivers/misc/mei/bus-fixup.c
index 2c02a338f63b..d4b1cccb5bc1 100644
--- a/drivers/misc/mei/bus-fixup.c
+++ b/drivers/misc/mei/bus-fixup.c
@@ -27,8 +27,41 @@
 #include "mei_dev.h"
 #include "client.h"
 
+#define MEI_UUID_NFC_INFO UUID_LE(0xd2de1625, 0x382d, 0x417d, \
+			0x48, 0xa4, 0xef, 0xab, 0xba, 0x8a, 0x12, 0x06)
+
 #define MEI_UUID_ANY NULL_UUID_LE
 
+/**
+ * number_of_connections - determine whether an client be on the bus
+ *    according number of connections
+ *    We support only clients:
+ *       1. with single connection
+ *       2. and fixed clients (max_number_of_connections == 0)
+ *
+ * @cldev: me clients device
+ */
+static void number_of_connections(struct mei_cl_device *cldev)
+{
+	dev_dbg(&cldev->dev, "running hook %s on %pUl\n",
+			__func__, mei_me_cl_uuid(cldev->me_cl));
+
+	if (cldev->me_cl->props.max_number_of_connections > 1)
+		cldev->do_match = 0;
+}
+
+/**
+ * blacklist - blacklist a client from the bus
+ *
+ * @cldev: me clients device
+ */
+static void blacklist(struct mei_cl_device *cldev)
+{
+	dev_dbg(&cldev->dev, "running hook %s on %pUl\n",
+			__func__, mei_me_cl_uuid(cldev->me_cl));
+	cldev->do_match = 0;
+}
+
 struct mei_nfc_cmd {
 	u8 command;
 	u8 status;
@@ -120,9 +153,7 @@ 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 = 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;
 
 /* Vendors */
 #define MEI_NFC_VENDOR_INSIDE 0x00
@@ -420,7 +451,10 @@ static struct mei_fixup {
 
 	const uuid_le uuid;
 	void (*hook)(struct mei_cl_device *cldev);
-} mei_fixups[] = {};
+} mei_fixups[] = {
+	MEI_FIXUP(MEI_UUID_ANY, number_of_connections),
+	MEI_FIXUP(MEI_UUID_NFC_INFO, blacklist),
+};
 
 /**
  * mei_cl_dev_fixup - run fixup handlers
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 15/17] mei: bus: simplify how we build nfc bus name
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (13 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 14/17] mei: bus: blacklist clients by number of connections and nfc info client Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 16/17] mei: bus: also unregister the driver when removing the device Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 17/17] mei: bus: link client devices instead of host clients Tomas Winkler
  16 siblings, 0 replies; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

we will need this simplification when name build will
be just part of the fixup rutine

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus-fixup.c | 116 ++++++++++++++++++++-----------------------
 drivers/misc/mei/bus.c       |   2 +-
 drivers/misc/mei/mei_dev.h   |   2 +-
 3 files changed, 56 insertions(+), 64 deletions(-)

diff --git a/drivers/misc/mei/bus-fixup.c b/drivers/misc/mei/bus-fixup.c
index d4b1cccb5bc1..ec03ecf80d41 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,17 @@ 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 +323,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 8ff824950597..fa3eff216eff 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -736,7 +736,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 24c70fada758..87ad08f333ac 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,
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 16/17] mei: bus: also unregister the driver when removing the device
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (14 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 15/17] mei: bus: simplify how we build nfc bus name Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  2015-06-13 19:11 ` [char-misc-next 17/17] mei: bus: link client devices instead of host clients Tomas Winkler
  16 siblings, 0 replies; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index fa3eff216eff..33898bed6209 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -767,11 +767,43 @@ struct mei_cl_device *mei_cl_add_device(struct mei_device *bus,
 }
 EXPORT_SYMBOL_GPL(mei_cl_add_device);
 
+/**
+ * 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);
+
+	cldev->is_added = 0;
+	put_device(&cldev->dev);
+}
+
+/**
+ * mei_cl_remove_device - remove a devices form the bus
+ *
+ * @cldev: me client device
+ */
 void mei_cl_remove_device(struct mei_cl_device *cldev)
 {
-	device_unregister(&cldev->dev);
+	mei_cl_bus_dev_stop(cldev);
+	mei_cl_bus_dev_destroy(cldev);
 }
-EXPORT_SYMBOL_GPL(mei_cl_remove_device);
 
 int __mei_cl_driver_register(struct mei_cl_driver *cldrv, struct module *owner)
 {
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [char-misc-next 17/17] mei: bus: link client devices instead of host clients
  2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
                   ` (15 preceding siblings ...)
  2015-06-13 19:11 ` [char-misc-next 16/17] mei: bus: also unregister the driver when removing the device Tomas Winkler
@ 2015-06-13 19:11 ` Tomas Winkler
  16 siblings, 0 replies; 25+ messages in thread
From: Tomas Winkler @ 2015-06-13 19:11 UTC (permalink / raw)
  To: gregkh; +Cc: arnd, linux-kernel, Tomas Winkler

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 <tomas.winkler@intel.com>
---
 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, 187 insertions(+), 336 deletions(-)

diff --git a/drivers/misc/mei/bus-fixup.c b/drivers/misc/mei/bus-fixup.c
index ec03ecf80d41..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;
-	}
-
-	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");
+	radio_name = mei_nfc_radio_name(&ver);
 
-		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;
-
+	dev_dbg(bus->dev, "nfc radio %s\n", radio_name);
+	strlcpy(cldev->name, radio_name, sizeof(cldev->name));
 
-	return;
-
-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");
 
-}
-
-
-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;
+	mei_cl_flush_queues(cl, NULL);
 
-	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->devices);
-
-	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->devices);
-
-	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);
+out:
+	mei_cl_unlink(cl);
+	mutex_unlock(&bus->device_lock);
+	mei_me_cl_put(me_cl);
+	kfree(cl);
 
-	cldev->priv_data = NULL;
+	if (ret)
+		cldev->do_match = 0;
 
-	mutex_lock(&bus->device_lock);
-	/* Need to remove the device here
-	 * since mei_nfc_free will unlink the clients
-	 */
-	mei_cl_remove_device(cldev);
-	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 33898bed6209..3857a3cca654 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -308,34 +308,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);
 
@@ -349,14 +358,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;
 
@@ -369,18 +380,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);
 
@@ -649,20 +661,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->devices, 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
  *
@@ -733,40 +731,6 @@ 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)
-{
-	struct mei_cl_device *cldev;
-	int status;
-
-	cldev = mei_cl_dev_alloc(bus, me_cl);
-	if (!cldev)
-		return NULL;
-
-	cldev->cl = cl;
-	strlcpy(cldev->name, name, sizeof(cldev->name));
-
-	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;
-	}
-
-	cl->cldev = cldev;
-
-	dev_dbg(&cldev->dev, "client %s registered\n", name);
-
-	return cldev;
-}
-EXPORT_SYMBOL_GPL(mei_cl_add_device);
-
 /**
  * mei_cl_bus_dev_stop - stop the driver
  *
@@ -790,21 +754,103 @@ static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
 
 	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_remove_device - remove a devices form the bus
+ * mei_cl_bus_remove_device - remove a devices form the bus
  *
  * @cldev: me client device
  */
-void mei_cl_remove_device(struct mei_cl_device *cldev)
+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->devices, 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;
+
+	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;
+
+	mutex_lock(&cldev->bus->cl_bus_lock);
+	me_cl->bus_added = true;
+	list_add_tail(&cldev->bus_list, &bus->devices);
+	mutex_unlock(&cldev->bus->cl_bus_lock);
+
+}
+
+/**
+ * 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;
+
+	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);
+
+	mutex_lock(&bus->cl_bus_lock);
+	list_for_each_entry_safe(cldev, n, &bus->devices, bus_list) {
+
+		if (!mei_me_cl_is_active(cldev->me_cl)) {
+			mei_cl_bus_remove_device(cldev);
+			continue;
+		}
+
+		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");
+}
+
 int __mei_cl_driver_register(struct mei_cl_driver *cldrv, struct module *owner)
 {
 	int err;
@@ -831,21 +877,6 @@ void mei_cl_driver_unregister(struct mei_cl_driver *cldrv)
 }
 EXPORT_SYMBOL_GPL(mei_cl_driver_unregister);
 
-void mei_cl_bus_remove_devices(struct mei_device *bus)
-{
-	struct mei_cl *cl, *next;
-
-	mutex_lock(&bus->device_lock);
-	list_for_each_entry_safe(cl, next, &bus->devices, device_link) {
-		if (cl->cldev)
-			mei_cl_remove_device(cl->cldev);
-
-		list_del(&cl->device_link);
-		mei_cl_unlink(cl);
-		kfree(cl);
-	}
-	mutex_unlock(&bus->device_lock);
-}
 
 int __init mei_cl_bus_init(void)
 {
diff --git a/drivers/misc/mei/client.c b/drivers/misc/mei/client.c
index 6decbe136ea7..cb9a19340054 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 6fbd13c10381..94453d78b12b 100644
--- a/drivers/misc/mei/init.c
+++ b/drivers/misc/mei/init.c
@@ -331,8 +331,6 @@ 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 87ad08f333ac..a88b89eee770 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
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* Re: [char-misc-next 02/17] mei: bus: fix drivers and devices names confusion
  2015-06-13 19:11 ` [char-misc-next 02/17] mei: bus: fix drivers and devices names confusion Tomas Winkler
@ 2015-06-13 21:21   ` Greg KH
  2015-06-14  6:30     ` Winkler, Tomas
  0 siblings, 1 reply; 25+ messages in thread
From: Greg KH @ 2015-06-13 21:21 UTC (permalink / raw)
  To: Tomas Winkler; +Cc: arnd, linux-kernel

On Sat, Jun 13, 2015 at 10:11:34PM +0300, Tomas Winkler wrote:
> the variables of type mei_cl_device are now cldev
> the variables of type mei_cl_driver are now cldrv

You are saying what you did, but not why you are doing it.  Renaming
variables for no good reason means I have to reject this patch, sorry.

> the variables of type mei_device are now bus in the
> bus layer context

What do you mean by this?

greg k-h

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [char-misc-next 03/17] mei: bus: rename device_list to simple devices
  2015-06-13 19:11 ` [char-misc-next 03/17] mei: bus: rename device_list to simple devices Tomas Winkler
@ 2015-06-13 21:21   ` Greg KH
  0 siblings, 0 replies; 25+ messages in thread
From: Greg KH @ 2015-06-13 21:21 UTC (permalink / raw)
  To: Tomas Winkler; +Cc: arnd, linux-kernel

On Sat, Jun 13, 2015 at 10:11:35PM +0300, Tomas Winkler wrote:
> Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>

No changelog entry?  Sorry, can't accept it.

And why are you doing this patch?  renames for no reason isn't ok.

greg k-h

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [char-misc-next 04/17] mei: bus: rename nfc.c to bus-fixup.c
  2015-06-13 19:11 ` [char-misc-next 04/17] mei: bus: rename nfc.c to bus-fixup.c Tomas Winkler
@ 2015-06-13 21:22   ` Greg KH
  0 siblings, 0 replies; 25+ messages in thread
From: Greg KH @ 2015-06-13 21:22 UTC (permalink / raw)
  To: Tomas Winkler; +Cc: arnd, linux-kernel

On Sat, Jun 13, 2015 at 10:11:36PM +0300, Tomas Winkler wrote:
> In nfc.c we just create device name on the mei client bus
> we make it more generic in the following patches

That doesn't make sense, please reword to justify renaming the file.

greg k-h

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [char-misc-next 05/17] mei: bus: move driver api functions at the start of the file
  2015-06-13 19:11 ` [char-misc-next 05/17] mei: bus: move driver api functions at the start of the file Tomas Winkler
@ 2015-06-13 21:22   ` Greg KH
  0 siblings, 0 replies; 25+ messages in thread
From: Greg KH @ 2015-06-13 21:22 UTC (permalink / raw)
  To: Tomas Winkler; +Cc: arnd, linux-kernel

On Sat, Jun 13, 2015 at 10:11:37PM +0300, Tomas Winkler wrote:
> 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

/me hands Tomas some extra '.' characters to use in changelogs...

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [char-misc-next 06/17] mei: bus: add kdoc for device attribute functions
  2015-06-13 19:11 ` [char-misc-next 06/17] mei: bus: add kdoc for device attribute functions Tomas Winkler
@ 2015-06-13 21:23   ` Greg KH
  0 siblings, 0 replies; 25+ messages in thread
From: Greg KH @ 2015-06-13 21:23 UTC (permalink / raw)
  To: Tomas Winkler; +Cc: arnd, linux-kernel

On Sat, Jun 13, 2015 at 10:11:38PM +0300, Tomas Winkler wrote:
> 1. Rename uevent handler to mei_cl_device_uevent
> 2. Add kdoc for uevent and mod alias functions

2 things in one patch?  No, please make these separate patches.

And why add kdoc to static functions?  That makes no sense.

greg k-h

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [char-misc-next 07/17] mei: bus: don't enable events implicitly in device enable
  2015-06-13 19:11 ` [char-misc-next 07/17] mei: bus: don't enable events implicitly in device enable Tomas Winkler
@ 2015-06-13 21:24   ` Greg KH
  0 siblings, 0 replies; 25+ messages in thread
From: Greg KH @ 2015-06-13 21:24 UTC (permalink / raw)
  To: Tomas Winkler; +Cc: arnd, linux-kernel

On Sat, Jun 13, 2015 at 10:11:39PM +0300, Tomas Winkler wrote:
> Always call mei_cl_register_event_cb explicitly so the events
> are enabled only when needed. The nfc driver is already using
> it that way so no need for further changes.

This doesn't seem to match up with the code being changed here.  You are
removing code.

I stopped reading here, please fix up the series and resend.

greg k-h

^ permalink raw reply	[flat|nested] 25+ messages in thread

* RE: [char-misc-next 02/17] mei: bus: fix drivers and devices names confusion
  2015-06-13 21:21   ` Greg KH
@ 2015-06-14  6:30     ` Winkler, Tomas
  0 siblings, 0 replies; 25+ messages in thread
From: Winkler, Tomas @ 2015-06-14  6:30 UTC (permalink / raw)
  To: Greg KH; +Cc: arnd, linux-kernel



> 
> On Sat, Jun 13, 2015 at 10:11:34PM +0300, Tomas Winkler wrote:
> > the variables of type mei_cl_device are now cldev
> > the variables of type mei_cl_driver are now cldrv
> 
> You are saying what you did, but not why you are doing it.  Renaming
> variables for no good reason means I have to reject this patch, sorry.

The reason is in title of this commit: "fix drivers and devices names confusion" 
> 
> > the variables of type mei_device are now bus in the
> > bus layer context
> 
> What do you mean by this?

mei_device plays bus role in this context so to make it clear I call the variable 'bus'

Thanks
Tomas


^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2015-06-14  6:30 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-13 19:11 [char-misc-next 00/17] revamp mei bus Tomas Winkler
2015-06-13 19:11 ` [char-misc-next 01/17] MAINTAINERS: mei: add mei_cl_bus.h to maintained file list Tomas Winkler
2015-06-13 19:11 ` [char-misc-next 02/17] mei: bus: fix drivers and devices names confusion Tomas Winkler
2015-06-13 21:21   ` Greg KH
2015-06-14  6:30     ` Winkler, Tomas
2015-06-13 19:11 ` [char-misc-next 03/17] mei: bus: rename device_list to simple devices Tomas Winkler
2015-06-13 21:21   ` Greg KH
2015-06-13 19:11 ` [char-misc-next 04/17] mei: bus: rename nfc.c to bus-fixup.c Tomas Winkler
2015-06-13 21:22   ` Greg KH
2015-06-13 19:11 ` [char-misc-next 05/17] mei: bus: move driver api functions at the start of the file Tomas Winkler
2015-06-13 21:22   ` Greg KH
2015-06-13 19:11 ` [char-misc-next 06/17] mei: bus: add kdoc for device attribute functions Tomas Winkler
2015-06-13 21:23   ` Greg KH
2015-06-13 19:11 ` [char-misc-next 07/17] mei: bus: don't enable events implicitly in device enable Tomas Winkler
2015-06-13 21:24   ` Greg KH
2015-06-13 19:11 ` [char-misc-next 08/17] mei: bus: report if event registration failed Tomas Winkler
2015-06-13 19:11 ` [char-misc-next 09/17] mei: bus: simplify device matching Tomas Winkler
2015-06-13 19:11 ` [char-misc-next 10/17] mei: bus: revamp probe and remove functions Tomas Winkler
2015-06-13 19:11 ` [char-misc-next 11/17] mei: bus: add reference to bus device in struct mei_cl_client Tomas Winkler
2015-06-13 19:11 ` [char-misc-next 12/17] mei: bus: add me client device list infrastructure Tomas Winkler
2015-06-13 19:11 ` [char-misc-next 13/17] mei: bus: enable running fixup routines before device registration Tomas Winkler
2015-06-13 19:11 ` [char-misc-next 14/17] mei: bus: blacklist clients by number of connections and nfc info client Tomas Winkler
2015-06-13 19:11 ` [char-misc-next 15/17] mei: bus: simplify how we build nfc bus name Tomas Winkler
2015-06-13 19:11 ` [char-misc-next 16/17] mei: bus: also unregister the driver when removing the device Tomas Winkler
2015-06-13 19:11 ` [char-misc-next 17/17] mei: bus: link client devices instead of host clients Tomas Winkler

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.