greybus: hd: generalise cport allocation
[cascardo/linux.git] / drivers / staging / greybus / connection.c
index 01d31f6..395a9df 100644 (file)
@@ -142,21 +142,8 @@ _gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
                                unsigned long flags)
 {
        struct gb_connection *connection;
-       struct ida *id_map = &hd->cport_id_map;
-       int ida_start, ida_end;
        int ret;
 
-       if (hd_cport_id < 0) {
-               ida_start = 0;
-               ida_end = hd->num_cports;
-       } else if (hd_cport_id < hd->num_cports) {
-               ida_start = hd_cport_id;
-               ida_end = hd_cport_id + 1;
-       } else {
-               dev_err(&hd->dev, "cport %d not available\n", hd_cport_id);
-               return ERR_PTR(-EINVAL);
-       }
-
        mutex_lock(&gb_connection_mutex);
 
        if (intf && gb_connection_intf_find(intf, cport_id)) {
@@ -165,15 +152,17 @@ _gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
                goto err_unlock;
        }
 
-       ret = ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
-       if (ret < 0)
+       ret = gb_hd_cport_allocate(hd, hd_cport_id, flags);
+       if (ret < 0) {
+               dev_err(&hd->dev, "failed to allocate cport: %d\n", ret);
                goto err_unlock;
+       }
        hd_cport_id = ret;
 
        connection = kzalloc(sizeof(*connection), GFP_KERNEL);
        if (!connection) {
                ret = -ENOMEM;
-               goto err_remove_ida;
+               goto err_hd_cport_release;
        }
 
        connection->hd_cport_id = hd_cport_id;
@@ -183,6 +172,8 @@ _gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
        connection->bundle = bundle;
        connection->handler = handler;
        connection->flags = flags;
+       if (intf && (intf->quirks & GB_INTERFACE_QUIRK_NO_CPORT_FEATURES))
+               connection->flags |= GB_CONNECTION_FLAG_NO_FLOWCTRL;
        connection->state = GB_CONNECTION_STATE_DISABLED;
 
        atomic_set(&connection->op_cycle, 0);
@@ -217,8 +208,8 @@ _gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
 
 err_free_connection:
        kfree(connection);
-err_remove_ida:
-       ida_simple_remove(id_map, hd_cport_id);
+err_hd_cport_release:
+       gb_hd_cport_release(hd, hd_cport_id);
 err_unlock:
        mutex_unlock(&gb_connection_mutex);
 
@@ -262,6 +253,19 @@ gb_connection_create_flags(struct gb_bundle *bundle, u16 cport_id,
 }
 EXPORT_SYMBOL_GPL(gb_connection_create_flags);
 
+struct gb_connection *
+gb_connection_create_offloaded(struct gb_bundle *bundle, u16 cport_id,
+                                       unsigned long flags)
+{
+       struct gb_interface *intf = bundle->intf;
+
+       flags |= GB_CONNECTION_FLAG_OFFLOADED;
+
+       return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
+                                       NULL, flags);
+}
+EXPORT_SYMBOL_GPL(gb_connection_create_offloaded);
+
 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
 {
        struct gb_host_device *hd = connection->hd;
@@ -290,17 +294,18 @@ static void gb_connection_hd_cport_disable(struct gb_connection *connection)
        hd->driver->cport_disable(hd, connection->hd_cport_id);
 }
 
-static int gb_connection_hd_fct_flow_enable(struct gb_connection *connection)
+static int
+gb_connection_hd_cport_features_enable(struct gb_connection *connection)
 {
        struct gb_host_device *hd = connection->hd;
        int ret;
 
-       if (!hd->driver->fct_flow_enable)
+       if (!hd->driver->cport_features_enable)
                return 0;
 
-       ret = hd->driver->fct_flow_enable(hd, connection->hd_cport_id);
+       ret = hd->driver->cport_features_enable(hd, connection->hd_cport_id);
        if (ret) {
-               dev_err(&hd->dev, "%s: failed to enable FCT flow: %d\n",
+               dev_err(&hd->dev, "%s: failed to enable CPort features: %d\n",
                        connection->name, ret);
                return ret;
        }
@@ -308,14 +313,15 @@ static int gb_connection_hd_fct_flow_enable(struct gb_connection *connection)
        return 0;
 }
 
-static void gb_connection_hd_fct_flow_disable(struct gb_connection *connection)
+static void
+gb_connection_hd_cport_features_disable(struct gb_connection *connection)
 {
        struct gb_host_device *hd = connection->hd;
 
-       if (!hd->driver->fct_flow_disable)
+       if (!hd->driver->cport_features_disable)
                return;
 
-       hd->driver->fct_flow_disable(hd, connection->hd_cport_id);
+       hd->driver->cport_features_disable(hd, connection->hd_cport_id);
 }
 
 /*
@@ -331,15 +337,17 @@ gb_connection_svc_connection_create(struct gb_connection *connection)
        int ret;
 
        if (gb_connection_is_static(connection))
-               return gb_connection_hd_fct_flow_enable(connection);
+               return gb_connection_hd_cport_features_enable(connection);
 
        intf = connection->intf;
 
-       /* The ES2/ES3 bootrom requires E2EFC, CSD and CSV to be disabled. */
+       /*
+        * Enable either E2EFC or CSD, unless no flow control is requested.
+        */
        cport_flags = GB_SVC_CPORT_FLAG_CSV_N;
-       if (intf->boot_over_unipro) {
+       if (gb_connection_flow_control_disabled(connection)) {
                cport_flags |= GB_SVC_CPORT_FLAG_CSD_N;
-       } else {
+       } else if (gb_connection_e2efc_enabled(connection)) {
                cport_flags |= GB_SVC_CPORT_FLAG_CSD_N |
                                GB_SVC_CPORT_FLAG_E2EFC;
        }
@@ -357,7 +365,7 @@ gb_connection_svc_connection_create(struct gb_connection *connection)
                return ret;
        }
 
-       ret = gb_connection_hd_fct_flow_enable(connection);
+       ret = gb_connection_hd_cport_features_enable(connection);
        if (ret) {
                gb_svc_connection_destroy(hd->svc, hd->svc->ap_intf_id,
                                          connection->hd_cport_id,
@@ -372,7 +380,7 @@ gb_connection_svc_connection_create(struct gb_connection *connection)
 static void
 gb_connection_svc_connection_destroy(struct gb_connection *connection)
 {
-       gb_connection_hd_fct_flow_disable(connection);
+       gb_connection_hd_cport_features_disable(connection);
 
        if (gb_connection_is_static(connection))
                return;
@@ -612,6 +620,7 @@ void gb_connection_disable_rx(struct gb_connection *connection)
 out_unlock:
        mutex_unlock(&connection->mutex);
 }
+EXPORT_SYMBOL_GPL(gb_connection_disable_rx);
 
 void gb_connection_disable(struct gb_connection *connection)
 {
@@ -638,8 +647,6 @@ EXPORT_SYMBOL_GPL(gb_connection_disable);
 /* Caller must have disabled the connection before destroying it. */
 void gb_connection_destroy(struct gb_connection *connection)
 {
-       struct ida *id_map;
-
        if (!connection)
                return;
 
@@ -652,8 +659,7 @@ void gb_connection_destroy(struct gb_connection *connection)
 
        destroy_workqueue(connection->wq);
 
-       id_map = &connection->hd->cport_id_map;
-       ida_simple_remove(id_map, connection->hd_cport_id);
+       gb_hd_cport_release(connection->hd, connection->hd_cport_id);
        connection->hd_cport_id = CPORT_ID_BAD;
 
        mutex_unlock(&gb_connection_mutex);