mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tomas Winkler <tomas.winkler@intel.com>
To: gregkh@linuxfoundation.org, sameo@linux.intel.com
Cc: arnd@arndb.de, linux-kernel@vger.kernel.org,
	Tomas Winkler <tomas.winkler@intel.com>
Subject: [char-misc-next 03/11] mei: bus: Initial implementation for I/O routines
Date: Thu,  7 Feb 2013 23:03:09 +0200	[thread overview]
Message-ID: <1360270997-7639-4-git-send-email-tomas.winkler@intel.com> (raw)
In-Reply-To: <1360270997-7639-1-git-send-email-tomas.winkler@intel.com>

From: Samuel Ortiz <sameo@linux.intel.com>

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 drivers/misc/mei/bus.c     |  226 ++++++++++++++++++++++++++++++++++++++++++++
 drivers/misc/mei/bus.h     |    3 +
 drivers/misc/mei/mei_dev.h |   14 +++
 include/linux/mei_bus.h    |   11 ++
 4 files changed, 254 insertions(+), 0 deletions(-)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index ea24e7c..9689b95 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -16,6 +16,7 @@
 #include <linux/module.h>
 #include <linux/device.h>
 #include <linux/kernel.h>
+#include <linux/sched.h>
 #include <linux/init.h>
 #include <linux/errno.h>
 #include <linux/slab.h>
@@ -25,6 +26,8 @@
 #include <linux/mei_bus.h>
 
 #include "mei_dev.h"
+#include "hw-me.h"
+#include "client.h"
 #include "bus.h"
 
 static int mei_device_match(struct device *dev, struct device_driver *drv)
@@ -73,6 +76,11 @@ static int mei_device_remove(struct device *dev)
 	if (!client || !dev->driver)
 		return 0;
 
+	if (client->event_cb) {
+		client->event_cb = NULL;
+		cancel_work_sync(&client->event_work);
+	}
+
 	driver = to_mei_driver(dev->driver);
 	if (!driver->remove) {
 		dev->driver = NULL;
@@ -182,3 +190,221 @@ void mei_del_driver(struct mei_bus_driver *driver)
 	pr_debug("mei: driver [%s] unregistered\n", driver->driver.name);
 }
 EXPORT_SYMBOL(mei_del_driver);
+
+int mei_send(struct mei_cl *cl, u8 *buf, size_t length)
+{
+	struct mei_device *dev;
+	struct mei_msg_hdr mei_hdr;
+	struct mei_cl_cb *cb;
+	int me_cl_id, err;
+
+	if (WARN_ON(!cl || !cl->dev))
+		return -ENODEV;
+
+	if (cl->state != MEI_FILE_CONNECTED)
+		return -ENODEV;
+
+	cb = mei_io_cb_init(cl, NULL);
+	if (!cb)
+		return -ENOMEM;
+
+	err = mei_io_cb_alloc_req_buf(cb, length);
+	if (err < 0) {
+		mei_io_cb_free(cb);
+		return err;
+	}
+
+	memcpy(cb->request_buffer.data, buf, length);
+	cb->fop_type = MEI_FOP_WRITE;
+
+	dev = cl->dev;
+
+	mutex_lock(&dev->device_lock);
+
+	/* Check if we have an ME client device */
+	me_cl_id = mei_me_cl_by_id(dev, cl->me_client_id);
+	if (me_cl_id == dev->me_clients_num) {
+		err = -ENODEV;
+		goto out_err;
+	}
+
+	if (length > dev->me_clients[me_cl_id].props.max_msg_length) {
+		err = -EINVAL;
+		goto out_err;
+	}
+
+	err = mei_cl_flow_ctrl_creds(cl);
+	if (err < 0)
+		goto out_err;
+
+	/* Host buffer is not ready, we queue the request */
+	if (err == 0 || !dev->hbuf_is_ready) {
+		cb->buf_idx = 0;
+		mei_hdr.msg_complete = 0;
+		cl->writing_state = MEI_WRITING;
+		list_add_tail(&cb->list, &dev->write_list.list);
+
+		mutex_unlock(&dev->device_lock);
+
+		return length;
+	}
+
+	dev->hbuf_is_ready = false;
+
+	/* Check for a maximum length */
+	if (length > mei_hbuf_max_len(dev)) {
+		mei_hdr.length = mei_hbuf_max_len(dev);
+		mei_hdr.msg_complete = 0;
+	} else {
+		mei_hdr.length = length;
+		mei_hdr.msg_complete = 1;
+	}
+
+	mei_hdr.host_addr = cl->host_client_id;
+	mei_hdr.me_addr = cl->me_client_id;
+	mei_hdr.reserved = 0;
+
+	if (mei_write_message(dev, &mei_hdr, buf)) {
+		err = -EIO;
+		goto out_err;
+	}
+
+	cl->writing_state = MEI_WRITING;
+	cb->buf_idx = mei_hdr.length;
+
+	if (!mei_hdr.msg_complete) {
+		list_add_tail(&cb->list, &dev->write_list.list);
+	} else {
+		if (mei_cl_flow_ctrl_reduce(cl)) {
+			err = -EIO;
+			goto out_err;
+		}
+
+		list_add_tail(&cb->list, &dev->write_waiting_list.list);
+	}
+
+	mutex_unlock(&dev->device_lock);
+
+	return mei_hdr.length;
+
+out_err:
+	mutex_unlock(&dev->device_lock);
+	mei_io_cb_free(cb);
+
+	return err;
+}
+
+int mei_recv(struct mei_cl *cl, u8 *buf, size_t length)
+{
+	struct mei_device *dev;
+	struct mei_cl_cb *cb;
+	size_t r_length;
+	int err;
+
+	if (WARN_ON(!cl || !cl->dev))
+		return -ENODEV;
+
+	dev = cl->dev;
+
+	mutex_lock(&dev->device_lock);
+
+	if (!cl->read_cb) {
+		err = mei_cl_read_start(cl);
+		if (err < 0) {
+			mutex_unlock(&dev->device_lock);
+			return err;
+		}
+	}
+
+	if (cl->reading_state != MEI_READ_COMPLETE &&
+	    !waitqueue_active(&cl->rx_wait)) {
+		mutex_unlock(&dev->device_lock);
+
+		if (wait_event_interruptible(cl->rx_wait,
+				(MEI_READ_COMPLETE == cl->reading_state))) {
+			if (signal_pending(current))
+				return -EINTR;
+			return -ERESTARTSYS;
+		}
+
+		mutex_lock(&dev->device_lock);
+	}
+
+	cb = cl->read_cb;
+
+	if (cl->reading_state != MEI_READ_COMPLETE) {
+		r_length = 0;
+		goto out;
+	}
+
+	r_length = min_t(size_t, length, cb->buf_idx);
+
+	memcpy(buf, cb->response_buffer.data, r_length);
+
+	mei_io_cb_free(cb);
+	cl->reading_state = MEI_IDLE;
+	cl->read_cb = NULL;
+
+out:
+	mutex_unlock(&dev->device_lock);
+
+	return r_length;
+}
+
+int mei_bus_send(struct mei_bus_client *client, u8 *buf, size_t length)
+{
+	struct mei_cl *cl = NULL;
+
+	/* TODO: hook between mei_bus_client and mei_cl */
+
+	if (client->ops && client->ops->send)
+		return client->ops->send(client, buf, length);
+
+	return mei_send(cl, buf, length);
+}
+EXPORT_SYMBOL(mei_bus_send);
+
+int mei_bus_recv(struct mei_bus_client *client, u8 *buf, size_t length)
+{
+	struct mei_cl *cl = NULL;
+
+	/* TODO: hook between mei_bus_client and mei_cl */
+
+	if (client->ops && client->ops->recv)
+		return client->ops->recv(client, buf, length);
+
+	return mei_recv(cl, buf, length);
+}
+EXPORT_SYMBOL(mei_bus_recv);
+
+static void mei_bus_event_work(struct work_struct *work)
+{
+	struct mei_bus_client *client;
+
+	client = container_of(work, struct mei_bus_client, event_work);
+
+	if (client->event_cb)
+		client->event_cb(client, client->events, client->event_context);
+
+	client->events = 0;
+
+	/* Prepare for the next read */
+	mei_cl_read_start(client->cl);
+}
+
+int mei_bus_register_event_cb(struct mei_bus_client *client,
+			      mei_bus_event_cb_t event_cb, void *context)
+{
+	if (client->event_cb)
+		return -EALREADY;
+
+	client->events = 0;
+	client->event_cb = event_cb;
+	client->event_context = context;
+	INIT_WORK(&client->event_work, mei_bus_event_work);
+
+	mei_cl_read_start(client->cl);
+
+	return 0;
+}
+EXPORT_SYMBOL(mei_bus_register_event_cb);
diff --git a/drivers/misc/mei/bus.h b/drivers/misc/mei/bus.h
index 7130b67..81789f6 100644
--- a/drivers/misc/mei/bus.h
+++ b/drivers/misc/mei/bus.h
@@ -24,4 +24,7 @@ struct mei_bus_client *mei_add_device(struct mei_device *mei_dev,
 					uuid_le uuid, char *name);
 void mei_remove_device(struct mei_bus_client *client);
 
+int mei_send(struct mei_cl *cl, u8 *buf, size_t length);
+int mei_recv(struct mei_cl *cl, u8 *buf, size_t length);
+
 #endif /* _MEI_BUS_H_ */
diff --git a/drivers/misc/mei/mei_dev.h b/drivers/misc/mei/mei_dev.h
index ce19b26..948f791 100644
--- a/drivers/misc/mei/mei_dev.h
+++ b/drivers/misc/mei/mei_dev.h
@@ -264,6 +264,13 @@ struct mei_hw_ops {
 		     unsigned char *buf, unsigned long len);
 };
 
+struct mei_bus_client;
+
+struct mei_bus_ops {
+	int (*send)(struct mei_bus_client *client, u8 *buf, size_t length);
+	int (*recv)(struct mei_bus_client *client, u8 *buf, size_t length);
+};
+
 /**
  * mei_bus_client
  *
@@ -284,6 +291,13 @@ struct mei_bus_client {
 	struct mei_bus_driver *driver;
 	struct device dev;
 
+	struct mei_bus_ops *ops;
+
+	struct work_struct event_work;
+	mei_bus_event_cb_t event_cb;
+	void *event_context;
+	unsigned long events;
+
 	void *priv_data;
 };
 
diff --git a/include/linux/mei_bus.h b/include/linux/mei_bus.h
index 395f573..241325f 100644
--- a/include/linux/mei_bus.h
+++ b/include/linux/mei_bus.h
@@ -88,7 +88,18 @@ struct mei_bus_driver {
 	int (*remove)(struct mei_bus_client *client);
 };
 
+#define MEI_BUS_EVENT_RX 0
+#define MEI_BUS_EVENT_TX 1
+
 int mei_add_driver(struct mei_bus_driver *driver);
 void mei_del_driver(struct mei_bus_driver *driver);
 
+int mei_bus_send(struct mei_bus_client *client, u8 *buf, size_t length);
+int mei_bus_recv(struct mei_bus_client *client, u8 *buf, size_t length);
+
+typedef void (*mei_bus_event_cb_t)(struct mei_bus_client *client,
+				   u32 events, void *context);
+int mei_bus_register_event_cb(struct mei_bus_client *client,
+			      mei_bus_event_cb_t read_cb, void *context);
+
 #endif /* _LINUX_MEI_BUS_H */
-- 
1.7.4.4


  parent reply	other threads:[~2013-02-07 21:06 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-07 21:03 [char-misc-next 00/11] Add MEI BUS and NFC Device Tomas Winkler
2013-02-07 21:03 ` [char-misc-next 01/11] mei: bus: Initial MEI bus type implementation Tomas Winkler
2013-02-07 22:29   ` Arnd Bergmann
2013-02-07 22:41   ` Arnd Bergmann
2013-02-07 22:59     ` Samuel Ortiz
2013-02-07 23:07       ` Winkler, Tomas
2013-02-07 21:03 ` [char-misc-next 02/11] mei: bus: Implement driver registration Tomas Winkler
2013-02-07 22:30   ` Arnd Bergmann
2013-02-07 21:03 ` Tomas Winkler [this message]
2013-02-07 22:34   ` [char-misc-next 03/11] mei: bus: Initial implementation for I/O routines Arnd Bergmann
2013-02-07 22:55     ` Samuel Ortiz
2013-02-11 11:52       ` Arnd Bergmann
2013-02-11 12:58         ` Samuel Ortiz
2013-02-11 15:08           ` Arnd Bergmann
2013-02-11 15:48             ` Samuel Ortiz
2013-02-11 16:12               ` Arnd Bergmann
2013-02-07 21:03 ` [char-misc-next 04/11] mei: bus: Add bus related structures to mei_cl Tomas Winkler
2013-02-07 21:03 ` [char-misc-next 05/11] mei: bus: Call bus routines from the core code Tomas Winkler
2013-02-07 22:37   ` Arnd Bergmann
2013-02-07 22:57     ` Winkler, Tomas
2013-02-07 23:09       ` Arnd Bergmann
2013-02-07 22:57     ` Samuel Ortiz
2013-02-07 21:03 ` [char-misc-next 06/11] mei: bus: Synchronous API for the data transmission Tomas Winkler
2013-02-07 21:03 ` [char-misc-next 07/11] mei: bus: Implement bus driver data setter/getter Tomas Winkler
2013-02-07 22:38   ` Arnd Bergmann
2013-02-07 22:58     ` Samuel Ortiz
2013-02-07 23:57       ` Samuel Ortiz
2013-02-11 14:58         ` Arnd Bergmann
2013-02-11 15:29           ` Samuel Ortiz
2013-02-11 16:03             ` Greg KH
2013-02-11 16:05               ` Samuel Ortiz
2013-02-07 21:03 ` [char-misc-next 08/11] mei: nfc: Initial nfc implementation Tomas Winkler
2013-02-07 22:26   ` Arnd Bergmann
2013-02-07 22:41     ` Samuel Ortiz
2013-02-07 21:03 ` [char-misc-next 09/11] mei: nfc: Connect also the regular ME client Tomas Winkler
2013-02-07 21:03 ` [char-misc-next 10/11] mei: nfc: Add NFC device to the MEI bus Tomas Winkler
2013-02-07 21:03 ` [char-misc-next 11/11] mei: nfc: Implement MEI bus IO ops Tomas Winkler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1360270997-7639-4-git-send-email-tomas.winkler@intel.com \
    --to=tomas.winkler@intel.com \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sameo@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®