protocol: Add create_write_window

Change-Id: Ia1f55488c2aaefbe744305d3ed823e41e48a5934
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/protocol.c b/protocol.c
index 96c804f..080e424 100644
--- a/protocol.c
+++ b/protocol.c
@@ -82,8 +82,8 @@
 	return lpc_addr >> context->block_size_shift;
 }
 
-int protocol_v1_create_read_window(struct mbox_context *context,
-				   struct protocol_create_window *io)
+int protocol_v1_create_window(struct mbox_context *context,
+			      struct protocol_create_window *io)
 {
 	int rc;
 	uint32_t offset = io->req.offset << context->block_size_shift;
@@ -114,11 +114,13 @@
 				       context->version == API_VERSION_1);
 		if (rc < 0) { /* Unable to map offset */
 			MSG_ERR("Couldn't create window mapping for offset 0x%.8x\n",
-				io->req.offset);
+				offset);
 			return rc;
 		}
 	}
 
+	context->current_is_write = !io->req.ro;
+
 	MSG_INFO("Window @ %p for size 0x%.8x maps flash offset 0x%.8x\n",
 		 context->current->mem, context->current->size,
 		 context->current->flash_offset);
@@ -192,12 +194,12 @@
 	return 0;
 }
 
-int protocol_v2_create_read_window(struct mbox_context *context,
-				   struct protocol_create_window *io)
+int protocol_v2_create_window(struct mbox_context *context,
+			      struct protocol_create_window *io)
 {
 	int rc;
 
-	rc = protocol_v1_create_read_window(context, io);
+	rc = protocol_v1_create_window(context, io);
 	if (rc < 0)
 		return rc;
 
@@ -212,15 +214,14 @@
 	.reset = protocol_v1_reset,
 	.get_info = protocol_v1_get_info,
 	.get_flash_info = protocol_v1_get_flash_info,
-	.create_read_window = protocol_v1_create_read_window,
+	.create_window = protocol_v1_create_window,
 };
 
 static const struct protocol_ops protocol_ops_v2 = {
 	.reset = protocol_v1_reset,
 	.get_info = protocol_v2_get_info,
 	.get_flash_info = protocol_v2_get_flash_info,
-	.create_read_window = protocol_v2_create_read_window,
-
+	.create_window = protocol_v2_create_window,
 };
 
 static const struct protocol_ops *protocol_ops_map[] = {
diff --git a/protocol.h b/protocol.h
index 7608b0e..9b5fc09 100644
--- a/protocol.h
+++ b/protocol.h
@@ -49,6 +49,7 @@
 		uint16_t offset;
 		uint16_t size;
 		uint8_t id;
+		bool ro;
 	} req;
 	struct {
 		uint16_t lpc_address;
@@ -63,8 +64,8 @@
 			struct protocol_get_info *io);
 	int (*get_flash_info)(struct mbox_context *context,
 			      struct protocol_get_flash_info *io);
-	int (*create_read_window)(struct mbox_context *context,
-				  struct protocol_create_window *io);
+	int (*create_window)(struct mbox_context *context,
+			     struct protocol_create_window *io);
 };
 
 int protocol_init(struct mbox_context *context);
@@ -78,15 +79,15 @@
 			 struct protocol_get_info *io);
 int protocol_v1_get_flash_info(struct mbox_context *context,
 			       struct protocol_get_flash_info *io);
-int protocol_v1_create_read_window(struct mbox_context *context,
-				   struct protocol_create_window *io);
+int protocol_v1_create_window(struct mbox_context *context,
+			      struct protocol_create_window *io);
 
 /* Protocol v2 */
 int protocol_v2_get_info(struct mbox_context *context,
 			 struct protocol_get_info *io);
 int protocol_v2_get_flash_info(struct mbox_context *context,
 			       struct protocol_get_flash_info *io);
-int protocol_v2_create_read_window(struct mbox_context *context,
-				   struct protocol_create_window *io);
+int protocol_v2_create_window(struct mbox_context *context,
+			      struct protocol_create_window *io);
 
 #endif /* PROTOCOL_H */
diff --git a/transport_mbox.c b/transport_mbox.c
index a4e7efa..8ea400c 100644
--- a/transport_mbox.c
+++ b/transport_mbox.c
@@ -285,6 +285,29 @@
 	return lpc_addr >> context->block_size_shift;
 }
 
+int mbox_handle_create_window(struct mbox_context *context, bool ro,
+			      union mbox_regs *req, struct mbox_msg *resp)
+{
+	struct protocol_create_window io;
+	int rc;
+
+	io.req.offset = get_u16(&req->msg.args[0]);
+	io.req.ro = ro;
+
+	rc = context->protocol->create_window(context, &io);
+	if (rc < 0) {
+		return rc;
+	}
+
+	put_u16(&resp->args[0], io.resp.lpc_address);
+	if (context->version >= API_VERSION_2) {
+		put_u16(&resp->args[2], io.resp.size);
+		put_u16(&resp->args[4], io.resp.offset);
+	}
+
+	return 0;
+}
+
 /*
  * Command: CREATE_READ_WINDOW
  * Opens a read window
@@ -307,24 +330,11 @@
 int mbox_handle_read_window(struct mbox_context *context,
 				   union mbox_regs *req, struct mbox_msg *resp)
 {
-	struct protocol_create_window io;
-	int rc;
-
-	io.req.offset = get_u16(&req->msg.args[0]);
-
-	rc = context->protocol->create_read_window(context, &io);
+	int rc = mbox_handle_create_window(context, true, req, resp);
 	if (rc < 0) {
 		return mbox_xlate_errno(context, rc);
 	}
 
-	put_u16(&resp->args[0], io.resp.lpc_address);
-	if (context->version >= API_VERSION_2) {
-		put_u16(&resp->args[2], io.resp.size);
-		put_u16(&resp->args[4], io.resp.offset);
-	}
-
-	context->current_is_write = false;
-
 	return 0;
 }
 
@@ -350,19 +360,12 @@
 int mbox_handle_write_window(struct mbox_context *context,
 				    union mbox_regs *req, struct mbox_msg *resp)
 {
-	int rc;
-
-	/*
-	 * This is very similar to opening a read window (exactly the same
-	 * for now infact)
-	 */
-	rc = mbox_handle_read_window(context, req, resp);
+	int rc = mbox_handle_create_window(context, false, req, resp);
 	if (rc < 0) {
-		return rc;
+		return mbox_xlate_errno(context, rc);
 	}
 
-	context->current_is_write = true;
-	return rc;
+	return 0;
 }
 
 /*