From b482b0d6f099e282c7499b5b1a3a69747bf4fa8b Mon Sep 17 00:00:00 2001
From: Johan Hovold <johan@hovoldconsulting.com>
Date: Sat, 23 Apr 2016 18:47:31 +0200
Subject: [PATCH] greybus: svc: implement interface mailbox event

Implement the new interface mailbox-event operation.

The event is sent by the SVC under certain conditions when an interface
updates its mailbox value. Specifically, this event will be used to
implement the new mode-switch functionality.

Upon reception the AP verifies that the interface is known and that the
mailbox has the expected MAILBOX_GREYBUS value. If so, the interface is
disabled before being re-enabled (re-enumerated).

Note that during mode-switch, the interface will typically already be in
a disabled state when the mailbox is written (with the ES3 bootrom being
the notable exception).

Signed-off-by: Johan Hovold <johan@hovoldconsulting.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
---
 drivers/staging/greybus/greybus_protocols.h | 13 +++
 drivers/staging/greybus/svc.c               | 96 +++++++++++++++++++++
 2 files changed, 109 insertions(+)

diff --git a/drivers/staging/greybus/greybus_protocols.h b/drivers/staging/greybus/greybus_protocols.h
index ece8c64ef13d2..bf3c132a32666 100644
--- a/drivers/staging/greybus/greybus_protocols.h
+++ b/drivers/staging/greybus/greybus_protocols.h
@@ -805,6 +805,7 @@ struct gb_spi_transfer_response {
 #define GB_SVC_TYPE_MODULE_INSERTED		0x1f
 #define GB_SVC_TYPE_MODULE_REMOVED		0x20
 #define GB_SVC_TYPE_INTF_ACTIVATE		0x27
+#define GB_SVC_TYPE_INTF_MAILBOX_EVENT		0x29
 
 /*
  * SVC version request/response has the same payload as
@@ -1038,6 +1039,18 @@ struct gb_svc_intf_activate_response {
 	__u8	intf_type;
 } __packed;
 
+#define GB_SVC_INTF_MAILBOX_NONE		0x00
+#define GB_SVC_INTF_MAILBOX_AP			0x01
+#define GB_SVC_INTF_MAILBOX_GREYBUS		0x02
+
+struct gb_svc_intf_mailbox_event_request {
+	__u8	intf_id;
+	__le16	result_code;
+	__le32	mailbox;
+} __packed;
+/* intf_mailbox_event response has no payload */
+
+
 /* RAW */
 
 /* Version of the Greybus raw protocol we support */
diff --git a/drivers/staging/greybus/svc.c b/drivers/staging/greybus/svc.c
index 9d90014ab2988..0a49698ce298f 100644
--- a/drivers/staging/greybus/svc.c
+++ b/drivers/staging/greybus/svc.c
@@ -695,6 +695,27 @@ static int gb_svc_hello(struct gb_operation *op)
 	return 0;
 }
 
+static struct gb_interface *gb_svc_interface_lookup(struct gb_svc *svc,
+							u8 intf_id)
+{
+	struct gb_host_device *hd = svc->hd;
+	struct gb_module *module;
+	size_t num_interfaces;
+	u8 module_id;
+
+	list_for_each_entry(module, &hd->modules, hd_node) {
+		module_id = module->module_id;
+		num_interfaces = module->num_interfaces;
+
+		if (intf_id >= module_id &&
+				intf_id < module_id + num_interfaces) {
+			return module->interfaces[intf_id - module_id];
+		}
+	}
+
+	return NULL;
+}
+
 static struct gb_module *gb_svc_module_lookup(struct gb_svc *svc, u8 module_id)
 {
 	struct gb_host_device *hd = svc->hd;
@@ -874,6 +895,58 @@ static void gb_svc_process_module_removed(struct gb_operation *operation)
 	gb_module_put(module);
 }
 
+static void gb_svc_process_intf_mailbox_event(struct gb_operation *operation)
+{
+	struct gb_svc_intf_mailbox_event_request *request;
+	struct gb_connection *connection = operation->connection;
+	struct gb_svc *svc = gb_connection_get_data(connection);
+	struct gb_interface *intf;
+	u8 intf_id;
+	u16 result_code;
+	u32 mailbox;
+
+	/* The request message size has already been verified. */
+	request = operation->request->payload;
+	intf_id = request->intf_id;
+	result_code = le16_to_cpu(request->result_code);
+	mailbox = le32_to_cpu(request->mailbox);
+
+	dev_dbg(&svc->dev, "%s - id = %u, result = 0x%04x, mailbox = 0x%08x\n",
+			__func__, intf_id, result_code, mailbox);
+
+	intf = gb_svc_interface_lookup(svc, intf_id);
+	if (!intf) {
+		dev_warn(&svc->dev, "unexpected mailbox event %u\n", intf_id);
+		return;
+	}
+
+	if (result_code) {
+		dev_warn(&svc->dev,
+				"mailbox event %u with UniPro error: 0x%04x\n",
+				intf_id, result_code);
+		goto err_disable_interface;
+	}
+
+	if (mailbox != GB_SVC_INTF_MAILBOX_GREYBUS) {
+		dev_warn(&svc->dev,
+				"mailbox event %u with unexected value: 0x%08x\n",
+				intf_id, mailbox);
+		goto err_disable_interface;
+	}
+
+	dev_info(&svc->dev, "mode switch detected on interface %u\n", intf_id);
+
+	gb_svc_intf_reenable(svc, intf);
+
+	return;
+
+err_disable_interface:
+	mutex_lock(&intf->mutex);
+	gb_interface_disable(intf);
+	gb_interface_deactivate(intf);
+	mutex_unlock(&intf->mutex);
+}
+
 static void gb_svc_process_deferred_request(struct work_struct *work)
 {
 	struct gb_svc_deferred_request *dr;
@@ -899,6 +972,9 @@ static void gb_svc_process_deferred_request(struct work_struct *work)
 	case GB_SVC_TYPE_MODULE_REMOVED:
 		gb_svc_process_module_removed(operation);
 		break;
+	case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
+		gb_svc_process_intf_mailbox_event(operation);
+		break;
 	default:
 		dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
 	}
@@ -1077,6 +1153,24 @@ static int gb_svc_module_removed_recv(struct gb_operation *op)
 	return gb_svc_queue_deferred_request(op);
 }
 
+static int gb_svc_intf_mailbox_event_recv(struct gb_operation *op)
+{
+	struct gb_svc *svc = gb_connection_get_data(op->connection);
+	struct gb_svc_intf_mailbox_event_request *request;
+
+	if (op->request->payload_size < sizeof(*request)) {
+		dev_warn(&svc->dev, "short mailbox request received (%zu < %zu)\n",
+				op->request->payload_size, sizeof(*request));
+		return -EINVAL;
+	}
+
+	request = op->request->payload;
+
+	dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
+
+	return gb_svc_queue_deferred_request(op);
+}
+
 static int gb_svc_request_handler(struct gb_operation *op)
 {
 	struct gb_connection *connection = op->connection;
@@ -1138,6 +1232,8 @@ static int gb_svc_request_handler(struct gb_operation *op)
 		return gb_svc_module_inserted_recv(op);
 	case GB_SVC_TYPE_MODULE_REMOVED:
 		return gb_svc_module_removed_recv(op);
+	case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
+		return gb_svc_intf_mailbox_event_recv(op);
 	default:
 		dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
 		return -EINVAL;
-- 
2.30.2