greybus: svc: connection: ask SVC to create connections
[cascardo/linux.git] / drivers / staging / greybus / connection.c
index b9f9b11..1a657f7 100644 (file)
@@ -7,6 +7,8 @@
  * Released under the GPLv2 only.
  */
 
+#include <linux/workqueue.h>
+
 #include "greybus.h"
 
 static DEFINE_SPINLOCK(gb_connections_lock);
@@ -65,8 +67,13 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr,
                          char *buf)
 {
        struct gb_connection *connection = to_gb_connection(dev);
+       enum gb_connection_state state;
+
+       spin_lock_irq(&connection->lock);
+       state = connection->state;
+       spin_unlock_irq(&connection->lock);
 
-       return sprintf(buf, "%d\n", connection->state);
+       return sprintf(buf, "%d\n", state);
 }
 static DEVICE_ATTR_RO(state);
 
@@ -75,7 +82,10 @@ protocol_id_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
        struct gb_connection *connection = to_gb_connection(dev);
 
-       return sprintf(buf, "%d\n", connection->protocol->id);
+       if (connection->protocol)
+               return sprintf(buf, "%d\n", connection->protocol->id);
+       else
+               return -EINVAL;
 }
 static DEVICE_ATTR_RO(protocol_id);
 
@@ -91,6 +101,7 @@ static void gb_connection_release(struct device *dev)
 {
        struct gb_connection *connection = to_gb_connection(dev);
 
+       destroy_workqueue(connection->wq);
        kfree(connection);
 }
 
@@ -100,9 +111,32 @@ struct device_type greybus_connection_type = {
 };
 
 
+int svc_update_connection(struct gb_interface *intf,
+                         struct gb_connection *connection)
+{
+       struct gb_bundle *bundle;
+
+       bundle = gb_bundle_create(intf, GB_SVC_BUNDLE_ID, GREYBUS_CLASS_SVC);
+       if (!bundle)
+               return -EINVAL;
+
+       device_del(&connection->dev);
+       connection->bundle = bundle;
+       connection->dev.parent = &bundle->dev;
+       dev_set_name(&connection->dev, "%s:%d", dev_name(&bundle->dev),
+                    GB_SVC_CPORT_ID);
+
+       WARN_ON(device_add(&connection->dev));
+
+       spin_lock_irq(&gb_connections_lock);
+       list_add(&connection->bundle_links, &bundle->connections);
+       spin_unlock_irq(&gb_connections_lock);
+
+       return 0;
+}
+
 void gb_connection_bind_protocol(struct gb_connection *connection)
 {
-       struct gb_interface *intf;
        struct gb_protocol *protocol;
 
        /* If we already have a protocol bound here, just return */
@@ -119,9 +153,10 @@ void gb_connection_bind_protocol(struct gb_connection *connection)
        /*
         * 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.
-        * */
-       intf = connection->bundle->intf;
-       if (intf->device_id != GB_DEVICE_ID_BAD)
+        */
+       if ((!connection->bundle &&
+            connection->hd_cport_id == GB_SVC_CPORT_ID) ||
+           connection->bundle->intf->device_id != GB_DEVICE_ID_BAD)
                gb_connection_init(connection);
 }
 
@@ -136,12 +171,15 @@ void gb_connection_bind_protocol(struct gb_connection *connection)
  * Returns a pointer to the new connection if successful, or a null
  * pointer otherwise.
  */
-struct gb_connection *gb_connection_create(struct gb_bundle *bundle,
-                               u16 cport_id, u8 protocol_id)
+struct gb_connection *
+gb_connection_create_range(struct greybus_host_device *hd,
+                          struct gb_bundle *bundle, struct device *parent,
+                          u16 cport_id, u8 protocol_id, u32 ida_start,
+                          u32 ida_end)
 {
        struct gb_connection *connection;
-       struct greybus_host_device *hd = bundle->intf->hd;
        struct ida *id_map = &hd->cport_id_map;
+       int hd_cport_id;
        int retval;
        u8 major = 0;
        u8 minor = 1;
@@ -151,21 +189,20 @@ struct gb_connection *gb_connection_create(struct gb_bundle *bundle,
         * initialize connections serially so we don't need to worry
         * about holding the connection lock.
         */
-       if (gb_connection_intf_find(bundle->intf, cport_id)) {
+       if (bundle && gb_connection_intf_find(bundle->intf, cport_id)) {
                pr_err("duplicate interface cport id 0x%04hx\n", cport_id);
                return NULL;
        }
 
+       hd_cport_id = ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
+       if (hd_cport_id < 0)
+               return NULL;
+
        connection = kzalloc(sizeof(*connection), GFP_KERNEL);
        if (!connection)
-               return NULL;
+               goto err_remove_ida;
 
-       retval = ida_simple_get(id_map, 0, CPORT_ID_MAX, GFP_KERNEL);
-       if (retval < 0) {
-               kfree(connection);
-               return NULL;
-       }
-       connection->hd_cport_id = (u16)retval;
+       connection->hd_cport_id = hd_cport_id;
        connection->intf_cport_id = cport_id;
        connection->hd = hd;
 
@@ -176,43 +213,100 @@ struct gb_connection *gb_connection_create(struct gb_bundle *bundle,
        connection->bundle = bundle;
        connection->state = GB_CONNECTION_STATE_DISABLED;
 
-       connection->dev.parent = &bundle->dev;
+       atomic_set(&connection->op_cycle, 0);
+       spin_lock_init(&connection->lock);
+       INIT_LIST_HEAD(&connection->operations);
+
+       connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
+                                        dev_name(parent), cport_id);
+       if (!connection->wq)
+               goto err_free_connection;
+
+       connection->dev.parent = parent;
        connection->dev.bus = &greybus_bus_type;
        connection->dev.type = &greybus_connection_type;
        connection->dev.groups = connection_groups;
        device_initialize(&connection->dev);
        dev_set_name(&connection->dev, "%s:%d",
-                    dev_name(&bundle->dev), cport_id);
+                    dev_name(parent), cport_id);
 
        retval = device_add(&connection->dev);
        if (retval) {
-               struct ida *id_map = &connection->hd->cport_id_map;
-
-               ida_simple_remove(id_map, connection->hd_cport_id);
                connection->hd_cport_id = CPORT_ID_BAD;
                put_device(&connection->dev);
 
                pr_err("failed to add connection device for cport 0x%04hx\n",
                        cport_id);
 
-               return NULL;
+               goto err_remove_ida;
        }
 
        spin_lock_irq(&gb_connections_lock);
        list_add(&connection->hd_links, &hd->connections);
-       list_add(&connection->bundle_links, &bundle->connections);
+
+       if (bundle)
+               list_add(&connection->bundle_links, &bundle->connections);
+       else
+               INIT_LIST_HEAD(&connection->bundle_links);
+
        spin_unlock_irq(&gb_connections_lock);
 
-       atomic_set(&connection->op_cycle, 0);
-       INIT_LIST_HEAD(&connection->operations);
+       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);
+       }
 
-       /* XXX Will have to establish connections to get version */
        gb_connection_bind_protocol(connection);
        if (!connection->protocol)
                dev_warn(&connection->dev,
                         "protocol 0x%02hhx handler not found\n", protocol_id);
 
        return connection;
+
+err_free_connection:
+       kfree(connection);
+err_remove_ida:
+       ida_simple_remove(id_map, hd_cport_id);
+
+       return NULL;
+}
+
+struct gb_connection *gb_connection_create(struct gb_bundle *bundle,
+                               u16 cport_id, u8 protocol_id)
+{
+       return gb_connection_create_range(bundle->intf->hd, bundle,
+                                         &bundle->dev, cport_id, protocol_id,
+                                         0, CPORT_ID_MAX);
+}
+
+/*
+ * Cancel all active operations on a connection.
+ *
+ * Should only be called during connection tear down.
+ */
+static void gb_connection_cancel_operations(struct gb_connection *connection,
+                                               int errno)
+{
+       struct gb_operation *operation;
+
+       spin_lock_irq(&connection->lock);
+       while (!list_empty(&connection->operations)) {
+               operation = list_last_entry(&connection->operations,
+                                               struct gb_operation, links);
+               gb_operation_get(operation);
+               spin_unlock_irq(&connection->lock);
+
+               if (gb_operation_is_incoming(operation))
+                       gb_operation_cancel_incoming(operation, errno);
+               else
+                       gb_operation_cancel(operation, errno);
+
+               gb_operation_put(operation);
+
+               spin_lock_irq(&connection->lock);
+       }
+       spin_unlock_irq(&connection->lock);
 }
 
 /*
@@ -220,24 +314,18 @@ struct gb_connection *gb_connection_create(struct gb_bundle *bundle,
  */
 void gb_connection_destroy(struct gb_connection *connection)
 {
-       struct gb_operation *operation;
-       struct gb_operation *next;
        struct ida *id_map;
 
        if (WARN_ON(!connection))
                return;
 
-       if (WARN_ON(!list_empty(&connection->operations))) {
-               list_for_each_entry_safe(operation, next,
-                                        &connection->operations, links)
-                       gb_operation_cancel(operation, -ESHUTDOWN);
-       }
        spin_lock_irq(&gb_connections_lock);
        list_del(&connection->bundle_links);
        list_del(&connection->hd_links);
        spin_unlock_irq(&gb_connections_lock);
 
-       gb_protocol_put(connection->protocol);
+       if (connection->protocol)
+               gb_protocol_put(connection->protocol);
        connection->protocol = NULL;
 
        id_map = &connection->hd->cport_id_map;
@@ -261,7 +349,8 @@ int gb_connection_init(struct gb_connection *connection)
         * Inform Interface about Active CPorts. We don't need to do this
         * operation for control cport.
         */
-       if (cport_id != GB_CONTROL_CPORT_ID) {
+       if (cport_id != GB_CONTROL_CPORT_ID &&
+           connection->hd_cport_id != GB_SVC_CPORT_ID) {
                struct gb_control *control = connection->bundle->intf->control;
 
                ret = gb_control_connected_operation(control, cport_id);
@@ -274,10 +363,16 @@ int gb_connection_init(struct gb_connection *connection)
        }
 
        /* Need to enable the connection to initialize it */
+       spin_lock_irq(&connection->lock);
        connection->state = GB_CONNECTION_STATE_ENABLED;
+       spin_unlock_irq(&connection->lock);
+
        ret = connection->protocol->connection_init(connection);
-       if (ret)
+       if (ret) {
+               spin_lock_irq(&connection->lock);
                connection->state = GB_CONNECTION_STATE_ERROR;
+               spin_unlock_irq(&connection->lock);
+       }
 
        return ret;
 }
@@ -291,17 +386,24 @@ void gb_connection_exit(struct gb_connection *connection)
                return;
        }
 
-       if (connection->state != GB_CONNECTION_STATE_ENABLED)
+       spin_lock_irq(&connection->lock);
+       if (connection->state != GB_CONNECTION_STATE_ENABLED) {
+               spin_unlock_irq(&connection->lock);
                return;
-
+       }
        connection->state = GB_CONNECTION_STATE_DESTROYING;
+       spin_unlock_irq(&connection->lock);
+
+       gb_connection_cancel_operations(connection, -ESHUTDOWN);
+
        connection->protocol->connection_exit(connection);
 
        /*
         * Inform Interface about In-active CPorts. We don't need to do this
         * operation for control cport.
         */
-       if (cport_id != GB_CONTROL_CPORT_ID) {
+       if (cport_id != GB_CONTROL_CPORT_ID &&
+           connection->hd_cport_id != GB_SVC_CPORT_ID) {
                struct gb_control *control = connection->bundle->intf->control;
                int ret;