From 67b81757dad8979e0cd85512fc0e5dfe80e2e96f Mon Sep 17 00:00:00 2001
From: Fabien Parent <fparent@baylibre.com>
Date: Tue, 13 Oct 2015 17:34:51 +0200
Subject: [PATCH] greybus: connection: destroy connection on failing to bind it

gb_connection_bind_protocol() returns proper error codes now and we
should destroy the connection on failures.

This change also fixes a NULL deref on hotplug when the control connection fails
to initialize.

Signed-off-by: Fabien Parent <fparent@baylibre.com>
Reviewed-by: Johan Hovold <johan@hovoldconsulting.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
---
 drivers/staging/greybus/connection.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/staging/greybus/connection.c b/drivers/staging/greybus/connection.c
index 6b56b3069fae..bd3f83b2ccb6 100644
--- a/drivers/staging/greybus/connection.c
+++ b/drivers/staging/greybus/connection.c
@@ -239,7 +239,14 @@ gb_connection_create_range(struct greybus_host_device *hd,
 
 	spin_unlock_irq(&gb_connections_lock);
 
-	gb_connection_bind_protocol(connection);
+	retval = gb_connection_bind_protocol(connection);
+	if (retval) {
+		dev_err(&connection->dev, "failed to bind protocol: %d\n",
+			retval);
+		gb_connection_destroy(connection);
+		return NULL;
+	}
+
 	if (!connection->protocol)
 		dev_warn(&connection->dev,
 			 "protocol 0x%02hhx handler not found\n", protocol_id);