greybus: connection: clean up init error paths
[cascardo/linux.git] / drivers / staging / greybus / connection.c
index eae6ad1..5b8aa04 100644 (file)
@@ -116,9 +116,18 @@ protocol_id_show(struct device *dev, struct device_attribute *attr, char *buf)
 }
 static DEVICE_ATTR_RO(protocol_id);
 
+static ssize_t
+ap_cport_id_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct gb_connection *connection = to_gb_connection(dev);
+       return sprintf(buf, "%hu\n", connection->hd_cport_id);
+}
+static DEVICE_ATTR_RO(ap_cport_id);
+
 static struct attribute *connection_attrs[] = {
        &dev_attr_state.attr,
        &dev_attr_protocol_id.attr,
+       &dev_attr_ap_cport_id.attr,
        NULL,
 };
 
@@ -163,31 +172,6 @@ int svc_update_connection(struct gb_interface *intf,
        return 0;
 }
 
-void gb_connection_bind_protocol(struct gb_connection *connection)
-{
-       struct gb_protocol *protocol;
-
-       /* If we already have a protocol bound here, just return */
-       if (connection->protocol)
-               return;
-
-       protocol = gb_protocol_get(connection->protocol_id,
-                                  connection->major,
-                                  connection->minor);
-       if (!protocol)
-               return;
-       connection->protocol = protocol;
-
-       /*
-        * If we have a valid device_id for the interface block, then we have an
-        * active device, so bring up the connection at the same time.
-        */
-       if ((!connection->bundle &&
-            connection->hd_cport_id == GB_SVC_CPORT_ID) ||
-           connection->bundle->intf->device_id != GB_DEVICE_ID_BAD)
-               gb_connection_init(connection);
-}
-
 /*
  * Set up a Greybus connection, representing the bidirectional link
  * between a CPort on a (local) Greybus host device and a CPort on
@@ -252,7 +236,7 @@ gb_connection_create_range(struct greybus_host_device *hd,
 
        if (kfifo_alloc(&connection->ts_kfifo, GB_CONNECTION_TS_KFIFO_LEN,
                        GFP_KERNEL))
-               goto err_free_connection;
+               goto err_destroy_wq;
 
        connection->dev.parent = parent;
        connection->dev.bus = &greybus_bus_type;
@@ -270,7 +254,7 @@ gb_connection_create_range(struct greybus_host_device *hd,
                pr_err("failed to add connection device for cport 0x%04hx\n",
                        cport_id);
 
-               goto err_free_kfifo;
+               goto err_remove_ida;
        }
 
        spin_lock_irq(&gb_connections_lock);
@@ -283,12 +267,6 @@ gb_connection_create_range(struct greybus_host_device *hd,
 
        spin_unlock_irq(&gb_connections_lock);
 
-       if (hd_cport_id != GB_SVC_CPORT_ID) {
-               gb_svc_connection_create(hd->svc,
-                                        hd->endo->ap_intf_id, hd_cport_id,
-                                        bundle->intf->interface_id, cport_id);
-       }
-
        gb_connection_bind_protocol(connection);
        if (!connection->protocol)
                dev_warn(&connection->dev,
@@ -296,8 +274,8 @@ gb_connection_create_range(struct greybus_host_device *hd,
 
        return connection;
 
-err_free_kfifo:
-       kfifo_free(&connection->ts_kfifo);
+err_destroy_wq:
+       destroy_workqueue(connection->wq);
 err_free_connection:
        kfree(connection);
 err_remove_ida:
@@ -311,7 +289,7 @@ struct gb_connection *gb_connection_create(struct gb_bundle *bundle,
 {
        return gb_connection_create_range(bundle->intf->hd, bundle,
                                          &bundle->dev, cport_id, protocol_id,
-                                         0, CPORT_ID_MAX);
+                                         0, bundle->intf->hd->num_cports - 1);
 }
 
 /*
@@ -344,29 +322,50 @@ static void gb_connection_cancel_operations(struct gb_connection *connection,
 }
 
 /*
- * Tear down a previously set up connection.
+ * Request the SVC to create a connection from AP's cport to interface's
+ * cport.
  */
-void gb_connection_destroy(struct gb_connection *connection)
+static int
+gb_connection_svc_connection_create(struct gb_connection *connection)
 {
-       struct ida *id_map;
+       struct greybus_host_device *hd = connection->hd;
+       struct gb_protocol *protocol = connection->protocol;
+       int ret;
 
-       if (WARN_ON(!connection))
-               return;
+       if (protocol->flags & GB_PROTOCOL_SKIP_SVC_CONNECTION)
+               return 0;
 
-       spin_lock_irq(&gb_connections_lock);
-       list_del(&connection->bundle_links);
-       list_del(&connection->hd_links);
-       spin_unlock_irq(&gb_connections_lock);
+       ret = gb_svc_connection_create(hd->svc,
+                       hd->endo->ap_intf_id,
+                       connection->hd_cport_id,
+                       connection->bundle->intf->interface_id,
+                       connection->intf_cport_id);
+       if (ret) {
+               dev_err(&connection->dev,
+                               "failed to create svc connection: %d\n", ret);
+               return ret;
+       }
 
-       if (connection->protocol)
-               gb_protocol_put(connection->protocol);
-       connection->protocol = NULL;
+       if (hd->driver->connection_create)
+               hd->driver->connection_create(connection);
 
-       id_map = &connection->hd->cport_id_map;
-       ida_simple_remove(id_map, connection->hd_cport_id);
-       connection->hd_cport_id = CPORT_ID_BAD;
+       return 0;
+}
 
-       device_unregister(&connection->dev);
+static void
+gb_connection_svc_connection_destroy(struct gb_connection *connection)
+{
+       if (connection->protocol->flags & GB_PROTOCOL_SKIP_SVC_CONNECTION)
+               return;
+
+       if (connection->hd->driver->connection_destroy)
+               connection->hd->driver->connection_destroy(connection);
+
+       gb_svc_connection_destroy(connection->hd->svc,
+                                 connection->hd->endo->ap_intf_id,
+                                 connection->hd_cport_id,
+                                 connection->bundle->intf->interface_id,
+                                 connection->intf_cport_id);
 }
 
 static void gb_connection_disconnected(struct gb_connection *connection)
@@ -375,12 +374,8 @@ static void gb_connection_disconnected(struct gb_connection *connection)
        int cport_id = connection->intf_cport_id;
        int ret;
 
-       /*
-        * Inform Interface about In-active CPorts. We don't need to do this
-        * operation for control cport.
-        */
-       if ((cport_id == GB_CONTROL_CPORT_ID) ||
-           (connection->hd_cport_id == GB_SVC_CPORT_ID))
+       /* Inform Interface about inactive CPorts */
+       if (connection->protocol->flags & GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED)
                return;
 
        control = connection->bundle->intf->control;
@@ -391,30 +386,26 @@ static void gb_connection_disconnected(struct gb_connection *connection)
                        "Failed to disconnect CPort-%d (%d)\n", cport_id, ret);
 }
 
-int gb_connection_init(struct gb_connection *connection)
+static int gb_connection_init(struct gb_connection *connection)
 {
        int cport_id = connection->intf_cport_id;
+       struct gb_protocol *protocol = connection->protocol;
        int ret;
 
-       if (!connection->protocol) {
-               dev_warn(&connection->dev, "init without protocol.\n");
-               return 0;
-       }
+       ret = gb_connection_svc_connection_create(connection);
+       if (ret)
+               return ret;
 
-       /*
-        * Inform Interface about Active CPorts. We don't need to do this
-        * operation for control cport.
-        */
-       if (cport_id != GB_CONTROL_CPORT_ID &&
-           connection->hd_cport_id != GB_SVC_CPORT_ID) {
+       /* Inform Interface about active CPorts */
+       if (!(protocol->flags & GB_PROTOCOL_SKIP_CONTROL_CONNECTED)) {
                struct gb_control *control = connection->bundle->intf->control;
 
                ret = gb_control_connected_operation(control, cport_id);
                if (ret) {
-                       dev_warn(&connection->dev,
-                                "Failed to connect CPort-%d (%d)\n",
-                                cport_id, ret);
-                       return 0;
+                       dev_err(&connection->dev,
+                               "Failed to connect CPort-%d (%d)\n",
+                               cport_id, ret);
+                       goto err_svc_destroy;
                }
        }
 
@@ -427,47 +418,38 @@ int gb_connection_init(struct gb_connection *connection)
         * Request protocol version supported by the module. We don't need to do
         * this for SVC as that is initiated by the SVC.
         */
-       if (connection->hd_cport_id != GB_SVC_CPORT_ID) {
-               bool send_request = false;
-
-               /*
-                * We need to send the protocol version of the firmware protocol
-                * supported by AP and so this special case.
-                */
-               if (connection->protocol->id == GREYBUS_PROTOCOL_FIRMWARE)
-                       send_request = true;
-
-               // FIXME: Should we always send the protocol version AP can
-               // support ?
-
-               ret = gb_protocol_get_version(connection, send_request);
+       if (!(protocol->flags & GB_PROTOCOL_SKIP_VERSION)) {
+               ret = gb_protocol_get_version(connection);
                if (ret) {
                        dev_err(&connection->dev,
                                "Failed to get version CPort-%d (%d)\n",
                                cport_id, ret);
-                       goto disconnect;
+                       goto err_disconnect;
                }
        }
 
-       ret = connection->protocol->connection_init(connection);
-       if (!ret)
-               return 0;
+       ret = protocol->connection_init(connection);
+       if (ret)
+               goto err_disconnect;
+
+       return 0;
 
-disconnect:
+err_disconnect:
        spin_lock_irq(&connection->lock);
        connection->state = GB_CONNECTION_STATE_ERROR;
        spin_unlock_irq(&connection->lock);
 
        gb_connection_disconnected(connection);
+err_svc_destroy:
+       gb_connection_svc_connection_destroy(connection);
+
        return ret;
 }
 
-void gb_connection_exit(struct gb_connection *connection)
+static void gb_connection_exit(struct gb_connection *connection)
 {
-       if (!connection->protocol) {
-               dev_warn(&connection->dev, "exit without protocol.\n");
+       if (!connection->protocol)
                return;
-       }
 
        spin_lock_irq(&connection->lock);
        if (connection->state != GB_CONNECTION_STATE_ENABLED) {
@@ -481,14 +463,75 @@ void gb_connection_exit(struct gb_connection *connection)
 
        connection->protocol->connection_exit(connection);
        gb_connection_disconnected(connection);
+       gb_connection_svc_connection_destroy(connection);
+}
+
+/*
+ * Tear down a previously set up connection.
+ */
+void gb_connection_destroy(struct gb_connection *connection)
+{
+       struct ida *id_map;
+
+       if (WARN_ON(!connection))
+               return;
+
+       gb_connection_exit(connection);
+
+       spin_lock_irq(&gb_connections_lock);
+       list_del(&connection->bundle_links);
+       list_del(&connection->hd_links);
+       spin_unlock_irq(&gb_connections_lock);
+
+       if (connection->protocol)
+               gb_protocol_put(connection->protocol);
+       connection->protocol = NULL;
+
+       id_map = &connection->hd->cport_id_map;
+       ida_simple_remove(id_map, connection->hd_cport_id);
+       connection->hd_cport_id = CPORT_ID_BAD;
+
+       device_unregister(&connection->dev);
 }
 
 void gb_hd_connections_exit(struct greybus_host_device *hd)
 {
        struct gb_connection *connection;
 
-       list_for_each_entry(connection, &hd->connections, hd_links) {
-               gb_connection_exit(connection);
+       list_for_each_entry(connection, &hd->connections, hd_links)
                gb_connection_destroy(connection);
+}
+
+int gb_connection_bind_protocol(struct gb_connection *connection)
+{
+       struct gb_protocol *protocol;
+       int ret;
+
+       /* If we already have a protocol bound here, just return */
+       if (connection->protocol)
+               return 0;
+
+       protocol = gb_protocol_get(connection->protocol_id,
+                                  connection->major,
+                                  connection->minor);
+       if (!protocol)
+               return 0;
+       connection->protocol = protocol;
+
+       /*
+        * If we have a valid device_id for the interface block, then we have an
+        * active device, so bring up the connection at the same time.
+        */
+       if ((!connection->bundle &&
+            protocol->flags & GB_PROTOCOL_NO_BUNDLE) ||
+           connection->bundle->intf->device_id != GB_DEVICE_ID_BAD) {
+               ret = gb_connection_init(connection);
+               if (ret) {
+                       gb_protocol_put(protocol);
+                       connection->protocol = NULL;
+                       return ret;
+               }
        }
+
+       return 0;
 }