greybus: interface: implement generic mode-switch functionality
[cascardo/linux.git] / drivers / staging / greybus / svc.c
index af41e8e..9df3f57 100644 (file)
@@ -7,14 +7,16 @@
  * Released under the GPLv2 only.
  */
 
+#include <linux/debugfs.h>
+#include <linux/input.h>
 #include <linux/workqueue.h>
 
 #include "greybus.h"
 
-#define CPORT_FLAGS_E2EFC       BIT(0)
-#define CPORT_FLAGS_CSD_N       BIT(1)
-#define CPORT_FLAGS_CSV_N       BIT(2)
+#define SVC_KEY_ARA_BUTTON     KEY_A
 
+#define SVC_INTF_EJECT_TIMEOUT         9000
+#define SVC_INTF_ACTIVATE_TIMEOUT      6000
 
 struct gb_svc_deferred_request {
        struct work_struct work;
@@ -22,6 +24,8 @@ struct gb_svc_deferred_request {
 };
 
 
+static int gb_svc_queue_deferred_request(struct gb_operation *operation);
+
 static ssize_t endo_id_show(struct device *dev,
                        struct device_attribute *attr, char *buf)
 {
@@ -40,14 +44,185 @@ static ssize_t ap_intf_id_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(ap_intf_id);
 
+
+// FIXME
+// This is a hack, we need to do this "right" and clean the interface up
+// properly, not just forcibly yank the thing out of the system and hope for the
+// best.  But for now, people want their modules to come out without having to
+// throw the thing to the ground or get out a screwdriver.
+static ssize_t intf_eject_store(struct device *dev,
+                               struct device_attribute *attr, const char *buf,
+                               size_t len)
+{
+       struct gb_svc *svc = to_gb_svc(dev);
+       unsigned short intf_id;
+       int ret;
+
+       ret = kstrtou16(buf, 10, &intf_id);
+       if (ret < 0)
+               return ret;
+
+       dev_warn(dev, "Forcibly trying to eject interface %d\n", intf_id);
+
+       ret = gb_svc_intf_eject(svc, intf_id);
+       if (ret < 0)
+               return ret;
+
+       return len;
+}
+static DEVICE_ATTR_WO(intf_eject);
+
+static ssize_t watchdog_show(struct device *dev, struct device_attribute *attr,
+                            char *buf)
+{
+       struct gb_svc *svc = to_gb_svc(dev);
+
+       return sprintf(buf, "%s\n",
+                      gb_svc_watchdog_enabled(svc) ? "enabled" : "disabled");
+}
+
+static ssize_t watchdog_store(struct device *dev,
+                             struct device_attribute *attr, const char *buf,
+                             size_t len)
+{
+       struct gb_svc *svc = to_gb_svc(dev);
+       int retval;
+       bool user_request;
+
+       retval = strtobool(buf, &user_request);
+       if (retval)
+               return retval;
+
+       if (user_request)
+               retval = gb_svc_watchdog_enable(svc);
+       else
+               retval = gb_svc_watchdog_disable(svc);
+       if (retval)
+               return retval;
+       return len;
+}
+static DEVICE_ATTR_RW(watchdog);
+
+static int gb_svc_pwrmon_rail_count_get(struct gb_svc *svc, u8 *value)
+{
+       struct gb_svc_pwrmon_rail_count_get_response response;
+       int ret;
+
+       ret = gb_operation_sync(svc->connection,
+                               GB_SVC_TYPE_PWRMON_RAIL_COUNT_GET, NULL, 0,
+                               &response, sizeof(response));
+       if (ret) {
+               dev_err(&svc->dev, "failed to get rail count: %d\n", ret);
+               return ret;
+       }
+
+       *value = response.rail_count;
+
+       return 0;
+}
+
+static int gb_svc_pwrmon_rail_names_get(struct gb_svc *svc,
+               struct gb_svc_pwrmon_rail_names_get_response *response,
+               size_t bufsize)
+{
+       int ret;
+
+       ret = gb_operation_sync(svc->connection,
+                               GB_SVC_TYPE_PWRMON_RAIL_NAMES_GET, NULL, 0,
+                               response, bufsize);
+       if (ret) {
+               dev_err(&svc->dev, "failed to get rail names: %d\n", ret);
+               return ret;
+       }
+
+       return 0;
+}
+
+static int gb_svc_pwrmon_sample_get(struct gb_svc *svc, u8 rail_id,
+                                   u8 measurement_type, u32 *value)
+{
+       struct gb_svc_pwrmon_sample_get_request request;
+       struct gb_svc_pwrmon_sample_get_response response;
+       int ret;
+
+       request.rail_id = rail_id;
+       request.measurement_type = measurement_type;
+
+       ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_PWRMON_SAMPLE_GET,
+                               &request, sizeof(request),
+                               &response, sizeof(response));
+       if (ret) {
+               dev_err(&svc->dev, "failed to get rail sample: %d\n", ret);
+               return ret;
+       }
+
+       if (response.result) {
+               dev_err(&svc->dev,
+                       "UniPro error while getting rail power sample (%d %d): %d\n",
+                       rail_id, measurement_type, response.result);
+               switch (response.result) {
+               case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
+                       return -EINVAL;
+               case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
+                       return -ENOMSG;
+               default:
+                       return -EREMOTEIO;
+               }
+       }
+
+       *value = le32_to_cpu(response.measurement);
+
+       return 0;
+}
+
+int gb_svc_pwrmon_intf_sample_get(struct gb_svc *svc, u8 intf_id,
+                                 u8 measurement_type, u32 *value)
+{
+       struct gb_svc_pwrmon_intf_sample_get_request request;
+       struct gb_svc_pwrmon_intf_sample_get_response response;
+       int ret;
+
+       request.intf_id = intf_id;
+       request.measurement_type = measurement_type;
+
+       ret = gb_operation_sync(svc->connection,
+                               GB_SVC_TYPE_PWRMON_INTF_SAMPLE_GET,
+                               &request, sizeof(request),
+                               &response, sizeof(response));
+       if (ret) {
+               dev_err(&svc->dev, "failed to get intf sample: %d\n", ret);
+               return ret;
+       }
+
+       if (response.result) {
+               dev_err(&svc->dev,
+                       "UniPro error while getting intf power sample (%d %d): %d\n",
+                       intf_id, measurement_type, response.result);
+               switch (response.result) {
+               case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
+                       return -EINVAL;
+               case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
+                       return -ENOMSG;
+               default:
+                       return -EREMOTEIO;
+               }
+       }
+
+       *value = le32_to_cpu(response.measurement);
+
+       return 0;
+}
+
 static struct attribute *svc_attrs[] = {
        &dev_attr_endo_id.attr,
        &dev_attr_ap_intf_id.attr,
+       &dev_attr_intf_eject.attr,
+       &dev_attr_watchdog.attr,
        NULL,
 };
 ATTRIBUTE_GROUPS(svc);
 
-static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
+int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
 {
        struct gb_svc_intf_device_id_request request;
 
@@ -58,16 +233,123 @@ static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
                                 &request, sizeof(request), NULL, 0);
 }
 
-int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
+int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id)
 {
-       struct gb_svc_intf_reset_request request;
+       struct gb_svc_intf_eject_request request;
+       int ret;
 
        request.intf_id = intf_id;
 
-       return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
-                                &request, sizeof(request), NULL, 0);
+       /*
+        * The pulse width for module release in svc is long so we need to
+        * increase the timeout so the operation will not return to soon.
+        */
+       ret = gb_operation_sync_timeout(svc->connection,
+                                       GB_SVC_TYPE_INTF_EJECT, &request,
+                                       sizeof(request), NULL, 0,
+                                       SVC_INTF_EJECT_TIMEOUT);
+       if (ret) {
+               dev_err(&svc->dev, "failed to eject interface %u\n", intf_id);
+               return ret;
+       }
+
+       return 0;
+}
+
+int gb_svc_intf_vsys_set(struct gb_svc *svc, u8 intf_id, bool enable)
+{
+       struct gb_svc_intf_vsys_request request;
+       struct gb_svc_intf_vsys_response response;
+       int type, ret;
+
+       request.intf_id = intf_id;
+
+       if (enable)
+               type = GB_SVC_TYPE_INTF_VSYS_ENABLE;
+       else
+               type = GB_SVC_TYPE_INTF_VSYS_DISABLE;
+
+       ret = gb_operation_sync(svc->connection, type,
+                       &request, sizeof(request),
+                       &response, sizeof(response));
+       if (ret < 0)
+               return ret;
+       if (response.result_code != GB_SVC_INTF_VSYS_OK)
+               return -EREMOTEIO;
+       return 0;
+}
+
+int gb_svc_intf_refclk_set(struct gb_svc *svc, u8 intf_id, bool enable)
+{
+       struct gb_svc_intf_refclk_request request;
+       struct gb_svc_intf_refclk_response response;
+       int type, ret;
+
+       request.intf_id = intf_id;
+
+       if (enable)
+               type = GB_SVC_TYPE_INTF_REFCLK_ENABLE;
+       else
+               type = GB_SVC_TYPE_INTF_REFCLK_DISABLE;
+
+       ret = gb_operation_sync(svc->connection, type,
+                       &request, sizeof(request),
+                       &response, sizeof(response));
+       if (ret < 0)
+               return ret;
+       if (response.result_code != GB_SVC_INTF_REFCLK_OK)
+               return -EREMOTEIO;
+       return 0;
+}
+
+int gb_svc_intf_unipro_set(struct gb_svc *svc, u8 intf_id, bool enable)
+{
+       struct gb_svc_intf_unipro_request request;
+       struct gb_svc_intf_unipro_response response;
+       int type, ret;
+
+       request.intf_id = intf_id;
+
+       if (enable)
+               type = GB_SVC_TYPE_INTF_UNIPRO_ENABLE;
+       else
+               type = GB_SVC_TYPE_INTF_UNIPRO_DISABLE;
+
+       ret = gb_operation_sync(svc->connection, type,
+                       &request, sizeof(request),
+                       &response, sizeof(response));
+       if (ret < 0)
+               return ret;
+       if (response.result_code != GB_SVC_INTF_UNIPRO_OK)
+               return -EREMOTEIO;
+       return 0;
+}
+
+int gb_svc_intf_activate(struct gb_svc *svc, u8 intf_id, u8 *intf_type)
+{
+       struct gb_svc_intf_activate_request request;
+       struct gb_svc_intf_activate_response response;
+       int ret;
+
+       request.intf_id = intf_id;
+
+       ret = gb_operation_sync_timeout(svc->connection,
+                       GB_SVC_TYPE_INTF_ACTIVATE,
+                       &request, sizeof(request),
+                       &response, sizeof(response),
+                       SVC_INTF_ACTIVATE_TIMEOUT);
+       if (ret < 0)
+               return ret;
+       if (response.status != GB_SVC_OP_SUCCESS) {
+               dev_err(&svc->dev, "failed to activate interface %u: %u\n",
+                               intf_id, response.status);
+               return -EREMOTEIO;
+       }
+
+       *intf_type = response.intf_type;
+
+       return 0;
 }
-EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
 
 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
                        u32 *value)
@@ -85,16 +367,16 @@ int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
                                &request, sizeof(request),
                                &response, sizeof(response));
        if (ret) {
-               dev_err(&svc->dev, "failed to get DME attribute (%hhu %hx %hu): %d\n",
+               dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
                                intf_id, attr, selector, ret);
                return ret;
        }
 
        result = le16_to_cpu(response.result_code);
        if (result) {
-               dev_err(&svc->dev, "UniPro error while getting DME attribute (%hhu %hx %hu): %hu\n",
+               dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
                                intf_id, attr, selector, result);
-               return -EINVAL;
+               return -EREMOTEIO;
        }
 
        if (value)
@@ -121,74 +403,26 @@ int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
                                &request, sizeof(request),
                                &response, sizeof(response));
        if (ret) {
-               dev_err(&svc->dev, "failed to set DME attribute (%hhu %hx %hu %u): %d\n",
+               dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
                                intf_id, attr, selector, value, ret);
                return ret;
        }
 
        result = le16_to_cpu(response.result_code);
        if (result) {
-               dev_err(&svc->dev, "UniPro error while setting DME attribute (%hhu %hx %hu %u): %hu\n",
+               dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
                                intf_id, attr, selector, value, result);
-               return -EINVAL;
+               return -EREMOTEIO;
        }
 
        return 0;
 }
 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
 
-/*
- * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot
- * status attribute. AP needs to read and clear it, after reading a non-zero
- * value from it.
- *
- * FIXME: This is module-hardware dependent and needs to be extended for every
- * type of module we want to support.
- */
-static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
-{
-       struct gb_host_device *hd = intf->hd;
-       int ret;
-       u32 value;
-
-       /* Read and clear boot status in T_TstSrcIncrement */
-       ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id,
-                                 DME_ATTR_T_TST_SRC_INCREMENT,
-                                 DME_ATTR_SELECTOR_INDEX, &value);
-
-       if (ret)
-               return ret;
-
-       /*
-        * A nonzero boot status indicates the module has finished
-        * booting. Clear it.
-        */
-       if (!value) {
-               dev_err(&intf->dev, "Module not ready yet\n");
-               return -ENODEV;
-       }
-
-       /*
-        * Check if the module needs to boot from unipro.
-        * For ES2: We need to check lowest 8 bits of 'value'.
-        * For ES3: We need to check highest 8 bits out of 32 of 'value'.
-        *
-        * FIXME: Add code to find if we are on ES2 or ES3 to have separate
-        * checks.
-        */
-       if (value == DME_TSI_UNIPRO_BOOT_STARTED ||
-           value == DME_TSI_FALLBACK_UNIPRO_BOOT_STARTED)
-               intf->boot_over_unipro = true;
-
-       return gb_svc_dme_peer_set(hd->svc, intf->interface_id,
-                                  DME_ATTR_T_TST_SRC_INCREMENT,
-                                  DME_ATTR_SELECTOR_INDEX, 0);
-}
-
 int gb_svc_connection_create(struct gb_svc *svc,
                                u8 intf1_id, u16 cport1_id,
                                u8 intf2_id, u16 cport2_id,
-                               bool boot_over_unipro)
+                               u8 cport_flags)
 {
        struct gb_svc_conn_create_request request;
 
@@ -196,27 +430,24 @@ int gb_svc_connection_create(struct gb_svc *svc,
        request.cport1_id = cpu_to_le16(cport1_id);
        request.intf2_id = intf2_id;
        request.cport2_id = cpu_to_le16(cport2_id);
-       /*
-        * XXX: fix connections paramaters to TC0 and all CPort flags
-        * for now.
-        */
-       request.tc = 0;
-
-       /*
-        * We need to skip setting E2EFC and other flags to the connection
-        * create request, for all cports, on an interface that need to boot
-        * over unipro, i.e. interfaces required to download firmware.
-        */
-       if (boot_over_unipro)
-               request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_CSD_N;
-       else
-               request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
+       request.tc = 0;         /* TC0 */
+       request.flags = cport_flags;
 
        return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
                                 &request, sizeof(request), NULL, 0);
 }
 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
 
+void gb_svc_connection_quiescing(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
+                                       u8 intf2_id, u16 cport2_id)
+{
+       /* FIXME: implement */
+
+       dev_dbg(&svc->dev, "%s - (%u:%u %u:%u)\n", __func__,
+                               intf1_id, cport1_id, intf2_id, cport2_id);
+}
+EXPORT_SYMBOL_GPL(gb_svc_connection_quiescing);
+
 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
                               u8 intf2_id, u16 cport2_id)
 {
@@ -232,14 +463,99 @@ void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
        ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
                                &request, sizeof(request), NULL, 0);
        if (ret) {
-               dev_err(&svc->dev, "failed to destroy connection (%hhu:%hu %hhu:%hu): %d\n",
+               dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
                                intf1_id, cport1_id, intf2_id, cport2_id, ret);
        }
 }
 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
 
+int gb_svc_timesync_enable(struct gb_svc *svc, u8 count, u64 frame_time,
+                          u32 strobe_delay, u32 refclk)
+{
+       struct gb_connection *connection = svc->connection;
+       struct gb_svc_timesync_enable_request request;
+
+       request.count = count;
+       request.frame_time = cpu_to_le64(frame_time);
+       request.strobe_delay = cpu_to_le32(strobe_delay);
+       request.refclk = cpu_to_le32(refclk);
+       return gb_operation_sync(connection,
+                                GB_SVC_TYPE_TIMESYNC_ENABLE,
+                                &request, sizeof(request), NULL, 0);
+}
+EXPORT_SYMBOL_GPL(gb_svc_timesync_enable);
+
+int gb_svc_timesync_disable(struct gb_svc *svc)
+{
+       struct gb_connection *connection = svc->connection;
+
+       return gb_operation_sync(connection,
+                                GB_SVC_TYPE_TIMESYNC_DISABLE,
+                                NULL, 0, NULL, 0);
+}
+EXPORT_SYMBOL_GPL(gb_svc_timesync_disable);
+
+int gb_svc_timesync_authoritative(struct gb_svc *svc, u64 *frame_time)
+{
+       struct gb_connection *connection = svc->connection;
+       struct gb_svc_timesync_authoritative_response response;
+       int ret, i;
+
+       ret = gb_operation_sync(connection,
+                               GB_SVC_TYPE_TIMESYNC_AUTHORITATIVE, NULL, 0,
+                               &response, sizeof(response));
+       if (ret < 0)
+               return ret;
+
+       for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
+               frame_time[i] = le64_to_cpu(response.frame_time[i]);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(gb_svc_timesync_authoritative);
+
+int gb_svc_timesync_ping(struct gb_svc *svc, u64 *frame_time)
+{
+       struct gb_connection *connection = svc->connection;
+       struct gb_svc_timesync_ping_response response;
+       int ret;
+
+       ret = gb_operation_sync(connection,
+                               GB_SVC_TYPE_TIMESYNC_PING,
+                               NULL, 0,
+                               &response, sizeof(response));
+       if (ret < 0)
+               return ret;
+
+       *frame_time = le64_to_cpu(response.frame_time);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(gb_svc_timesync_ping);
+
+int gb_svc_timesync_wake_pins_acquire(struct gb_svc *svc, u32 strobe_mask)
+{
+       struct gb_connection *connection = svc->connection;
+       struct gb_svc_timesync_wake_pins_acquire_request request;
+
+       request.strobe_mask = cpu_to_le32(strobe_mask);
+       return gb_operation_sync(connection,
+                                GB_SVC_TYPE_TIMESYNC_WAKE_PINS_ACQUIRE,
+                                &request, sizeof(request),
+                                NULL, 0);
+}
+EXPORT_SYMBOL_GPL(gb_svc_timesync_wake_pins_acquire);
+
+int gb_svc_timesync_wake_pins_release(struct gb_svc *svc)
+{
+       struct gb_connection *connection = svc->connection;
+
+       return gb_operation_sync(connection,
+                                GB_SVC_TYPE_TIMESYNC_WAKE_PINS_RELEASE,
+                                NULL, 0, NULL, 0);
+}
+EXPORT_SYMBOL_GPL(gb_svc_timesync_wake_pins_release);
+
 /* Creates bi-directional routes between the devices */
-static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
+int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
                               u8 intf2_id, u8 dev2_id)
 {
        struct gb_svc_route_create_request request;
@@ -254,7 +570,7 @@ static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
 }
 
 /* Destroys bi-directional routes between the devices */
-static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
+void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
 {
        struct gb_svc_route_destroy_request request;
        int ret;
@@ -265,17 +581,55 @@ static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
        ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
                                &request, sizeof(request), NULL, 0);
        if (ret) {
-               dev_err(&svc->dev, "failed to destroy route (%hhu %hhu): %d\n",
+               dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
                                intf1_id, intf2_id, ret);
        }
 }
 
+int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series,
+                              u8 tx_mode, u8 tx_gear, u8 tx_nlanes,
+                              u8 rx_mode, u8 rx_gear, u8 rx_nlanes,
+                              u8 flags, u32 quirks)
+{
+       struct gb_svc_intf_set_pwrm_request request;
+       struct gb_svc_intf_set_pwrm_response response;
+       int ret;
+
+       request.intf_id = intf_id;
+       request.hs_series = hs_series;
+       request.tx_mode = tx_mode;
+       request.tx_gear = tx_gear;
+       request.tx_nlanes = tx_nlanes;
+       request.rx_mode = rx_mode;
+       request.rx_gear = rx_gear;
+       request.rx_nlanes = rx_nlanes;
+       request.flags = flags;
+       request.quirks = cpu_to_le32(quirks);
+
+       ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
+                               &request, sizeof(request),
+                               &response, sizeof(response));
+       if (ret < 0)
+               return ret;
+
+       return le16_to_cpu(response.result_code);
+}
+EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
+
+int gb_svc_ping(struct gb_svc *svc)
+{
+       return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
+                                        NULL, 0, NULL, 0,
+                                        GB_OPERATION_TIMEOUT_DEFAULT * 2);
+}
+EXPORT_SYMBOL_GPL(gb_svc_ping);
+
 static int gb_svc_version_request(struct gb_operation *op)
 {
        struct gb_connection *connection = op->connection;
-       struct gb_svc *svc = connection->private;
-       struct gb_protocol_version_request *request;
-       struct gb_protocol_version_response *response;
+       struct gb_svc *svc = gb_connection_get_data(connection);
+       struct gb_svc_version_request *request;
+       struct gb_svc_version_response *response;
 
        if (op->request->payload_size < sizeof(*request)) {
                dev_err(&svc->dev, "short version request (%zu < %zu)\n",
@@ -287,28 +641,186 @@ static int gb_svc_version_request(struct gb_operation *op)
        request = op->request->payload;
 
        if (request->major > GB_SVC_VERSION_MAJOR) {
-               dev_warn(&svc->dev, "unsupported major version (%hhu > %hhu)\n",
+               dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
                                request->major, GB_SVC_VERSION_MAJOR);
                return -ENOTSUPP;
        }
 
-       connection->module_major = request->major;
-       connection->module_minor = request->minor;
+       svc->protocol_major = request->major;
+       svc->protocol_minor = request->minor;
 
        if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
                return -ENOMEM;
 
        response = op->response->payload;
-       response->major = connection->module_major;
-       response->minor = connection->module_minor;
+       response->major = svc->protocol_major;
+       response->minor = svc->protocol_minor;
 
        return 0;
 }
 
+static ssize_t pwr_debugfs_voltage_read(struct file *file, char __user *buf,
+                                       size_t len, loff_t *offset)
+{
+       struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
+       struct gb_svc *svc = pwrmon_rails->svc;
+       int ret, desc;
+       u32 value;
+       char buff[16];
+
+       ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
+                                      GB_SVC_PWRMON_TYPE_VOL, &value);
+       if (ret) {
+               dev_err(&svc->dev,
+                       "failed to get voltage sample %u: %d\n",
+                       pwrmon_rails->id, ret);
+               return ret;
+       }
+
+       desc = scnprintf(buff, sizeof(buff), "%u\n", value);
+
+       return simple_read_from_buffer(buf, len, offset, buff, desc);
+}
+
+static ssize_t pwr_debugfs_current_read(struct file *file, char __user *buf,
+                                       size_t len, loff_t *offset)
+{
+       struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
+       struct gb_svc *svc = pwrmon_rails->svc;
+       int ret, desc;
+       u32 value;
+       char buff[16];
+
+       ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
+                                      GB_SVC_PWRMON_TYPE_CURR, &value);
+       if (ret) {
+               dev_err(&svc->dev,
+                       "failed to get current sample %u: %d\n",
+                       pwrmon_rails->id, ret);
+               return ret;
+       }
+
+       desc = scnprintf(buff, sizeof(buff), "%u\n", value);
+
+       return simple_read_from_buffer(buf, len, offset, buff, desc);
+}
+
+static ssize_t pwr_debugfs_power_read(struct file *file, char __user *buf,
+                                     size_t len, loff_t *offset)
+{
+       struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
+       struct gb_svc *svc = pwrmon_rails->svc;
+       int ret, desc;
+       u32 value;
+       char buff[16];
+
+       ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
+                                      GB_SVC_PWRMON_TYPE_PWR, &value);
+       if (ret) {
+               dev_err(&svc->dev, "failed to get power sample %u: %d\n",
+                       pwrmon_rails->id, ret);
+               return ret;
+       }
+
+       desc = scnprintf(buff, sizeof(buff), "%u\n", value);
+
+       return simple_read_from_buffer(buf, len, offset, buff, desc);
+}
+
+static const struct file_operations pwrmon_debugfs_voltage_fops = {
+       .read           = pwr_debugfs_voltage_read,
+};
+
+static const struct file_operations pwrmon_debugfs_current_fops = {
+       .read           = pwr_debugfs_current_read,
+};
+
+static const struct file_operations pwrmon_debugfs_power_fops = {
+       .read           = pwr_debugfs_power_read,
+};
+
+static void gb_svc_pwrmon_debugfs_init(struct gb_svc *svc)
+{
+       int i;
+       size_t bufsize;
+       struct dentry *dent;
+       struct gb_svc_pwrmon_rail_names_get_response *rail_names;
+       u8 rail_count;
+
+       dent = debugfs_create_dir("pwrmon", svc->debugfs_dentry);
+       if (IS_ERR_OR_NULL(dent))
+               return;
+
+       if (gb_svc_pwrmon_rail_count_get(svc, &rail_count))
+               goto err_pwrmon_debugfs;
+
+       if (!rail_count || rail_count > GB_SVC_PWRMON_MAX_RAIL_COUNT)
+               goto err_pwrmon_debugfs;
+
+       bufsize = GB_SVC_PWRMON_RAIL_NAME_BUFSIZE * rail_count;
+
+       rail_names = kzalloc(bufsize, GFP_KERNEL);
+       if (!rail_names)
+               goto err_pwrmon_debugfs;
+
+       svc->pwrmon_rails = kcalloc(rail_count, sizeof(*svc->pwrmon_rails),
+                                   GFP_KERNEL);
+       if (!svc->pwrmon_rails)
+               goto err_pwrmon_debugfs_free;
+
+       if (gb_svc_pwrmon_rail_names_get(svc, rail_names, bufsize))
+               goto err_pwrmon_debugfs_free;
+
+       for (i = 0; i < rail_count; i++) {
+               struct dentry *dir;
+               struct svc_debugfs_pwrmon_rail *rail = &svc->pwrmon_rails[i];
+               char fname[GB_SVC_PWRMON_RAIL_NAME_BUFSIZE];
+
+               snprintf(fname, sizeof(fname), "%s",
+                        (char *)&rail_names->name[i]);
+
+               rail->id = i;
+               rail->svc = svc;
+
+               dir = debugfs_create_dir(fname, dent);
+               debugfs_create_file("voltage_now", S_IRUGO, dir, rail,
+                                   &pwrmon_debugfs_voltage_fops);
+               debugfs_create_file("current_now", S_IRUGO, dir, rail,
+                                   &pwrmon_debugfs_current_fops);
+               debugfs_create_file("power_now", S_IRUGO, dir, rail,
+                                   &pwrmon_debugfs_power_fops);
+       }
+
+       kfree(rail_names);
+       return;
+
+err_pwrmon_debugfs_free:
+       kfree(rail_names);
+       kfree(svc->pwrmon_rails);
+       svc->pwrmon_rails = NULL;
+
+err_pwrmon_debugfs:
+       debugfs_remove(dent);
+}
+
+static void gb_svc_debugfs_init(struct gb_svc *svc)
+{
+       svc->debugfs_dentry = debugfs_create_dir(dev_name(&svc->dev),
+                                                gb_debugfs_get());
+       gb_svc_pwrmon_debugfs_init(svc);
+}
+
+static void gb_svc_debugfs_exit(struct gb_svc *svc)
+{
+       debugfs_remove_recursive(svc->debugfs_dentry);
+       kfree(svc->pwrmon_rails);
+       svc->pwrmon_rails = NULL;
+}
+
 static int gb_svc_hello(struct gb_operation *op)
 {
        struct gb_connection *connection = op->connection;
-       struct gb_svc *svc = connection->private;
+       struct gb_svc *svc = gb_connection_get_data(connection);
        struct gb_svc_hello_request *hello_request;
        int ret;
 
@@ -329,33 +841,97 @@ static int gb_svc_hello(struct gb_operation *op)
                return ret;
        }
 
-       return 0;
+       ret = input_register_device(svc->input);
+       if (ret) {
+               dev_err(&svc->dev, "failed to register input: %d\n", ret);
+               device_del(&svc->dev);
+               return ret;
+       }
+
+       ret = gb_svc_watchdog_create(svc);
+       if (ret) {
+               dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
+               input_unregister_device(svc->input);
+               device_del(&svc->dev);
+               return ret;
+       }
+
+       gb_svc_debugfs_init(svc);
+
+       return gb_svc_queue_deferred_request(op);
 }
 
-static void gb_svc_intf_remove(struct gb_svc *svc, struct gb_interface *intf)
+static struct gb_interface *gb_svc_interface_lookup(struct gb_svc *svc,
+                                                       u8 intf_id)
 {
-       u8 intf_id = intf->interface_id;
-       u8 device_id;
+       struct gb_host_device *hd = svc->hd;
+       struct gb_module *module;
+       size_t num_interfaces;
+       u8 module_id;
+
+       list_for_each_entry(module, &hd->modules, hd_node) {
+               module_id = module->module_id;
+               num_interfaces = module->num_interfaces;
+
+               if (intf_id >= module_id &&
+                               intf_id < module_id + num_interfaces) {
+                       return module->interfaces[intf_id - module_id];
+               }
+       }
+
+       return NULL;
+}
 
-       device_id = intf->device_id;
-       gb_interface_remove(intf);
+static struct gb_module *gb_svc_module_lookup(struct gb_svc *svc, u8 module_id)
+{
+       struct gb_host_device *hd = svc->hd;
+       struct gb_module *module;
+
+       list_for_each_entry(module, &hd->modules, hd_node) {
+               if (module->module_id == module_id)
+                       return module;
+       }
+
+       return NULL;
+}
+
+static void gb_svc_process_hello_deferred(struct gb_operation *operation)
+{
+       struct gb_connection *connection = operation->connection;
+       struct gb_svc *svc = gb_connection_get_data(connection);
+       int ret;
 
        /*
-        * Destroy the two-way route between the AP and the interface.
+        * XXX This is a hack/work-around to reconfigure the APBridgeA-Switch
+        * link to PWM G2, 1 Lane, Slow Auto, so that it has sufficient
+        * bandwidth for 3 audio streams plus boot-over-UniPro of a hot-plugged
+        * module.
+        *
+        * The code should be removed once SW-2217, Heuristic for UniPro
+        * Power Mode Changes is resolved.
         */
-       gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
+       ret = gb_svc_intf_set_power_mode(svc, svc->ap_intf_id,
+                                       GB_SVC_UNIPRO_HS_SERIES_A,
+                                       GB_SVC_UNIPRO_SLOW_AUTO_MODE,
+                                       2, 1,
+                                       GB_SVC_UNIPRO_SLOW_AUTO_MODE,
+                                       2, 1,
+                                       0, 0);
 
-       ida_simple_remove(&svc->device_id_map, device_id);
+       if (ret)
+               dev_warn(&svc->dev,
+                       "power mode change failed on AP to switch link: %d\n",
+                       ret);
 }
 
 static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
 {
        struct gb_svc_intf_hotplug_request *request;
        struct gb_connection *connection = operation->connection;
-       struct gb_svc *svc = connection->private;
+       struct gb_svc *svc = gb_connection_get_data(connection);
        struct gb_host_device *hd = connection->hd;
-       struct gb_interface *intf;
-       u8 intf_id, device_id;
+       struct gb_module *module;
+       u8 intf_id;
        int ret;
 
        /* The request message size has already been verified. */
@@ -364,122 +940,159 @@ static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
 
        dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
 
-       intf = gb_interface_find(hd, intf_id);
-       if (intf) {
-               /*
-                * We have received a hotplug request for an interface that
-                * already exists.
-                *
-                * This can happen in cases like:
-                * - bootrom loading the firmware image and booting into that,
-                *   which only generates a hotplug event. i.e. no hot-unplug
-                *   event.
-                * - Or the firmware on the module crashed and sent hotplug
-                *   request again to the SVC, which got propagated to AP.
-                *
-                * Remove the interface and add it again, and let user know
-                * about this with a print message.
-                */
-               dev_info(&svc->dev, "removing interface %hhu to add it again\n",
-                               intf_id);
-               gb_svc_intf_remove(svc, intf);
+       /* All modules are considered 1x2 for now */
+       module = gb_svc_module_lookup(svc, intf_id);
+       if (module) {
+               /* legacy mode switch */
+               return gb_interface_mailbox_event(module->interfaces[0], 0,
+                                               GB_SVC_INTF_MAILBOX_GREYBUS);
        }
 
-       intf = gb_interface_create(hd, intf_id);
-       if (!intf) {
-               dev_err(&svc->dev, "failed to create interface %hhu\n",
+       module = gb_module_create(hd, intf_id, 1);
+       if (!module) {
+               dev_err(&svc->dev, "failed to create module\n");
+               return;
+       }
+
+       ret = gb_module_add(module);
+       if (ret) {
+               gb_module_put(module);
+               return;
+       }
+
+       list_add(&module->hd_node, &hd->modules);
+}
+
+static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
+{
+       struct gb_svc *svc = gb_connection_get_data(operation->connection);
+       struct gb_svc_intf_hot_unplug_request *request;
+       struct gb_module *module;
+       u8 intf_id;
+
+       /* The request message size has already been verified. */
+       request = operation->request->payload;
+       intf_id = request->intf_id;
+
+       dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
+
+       /* All modules are considered 1x2 for now */
+       module = gb_svc_module_lookup(svc, intf_id);
+       if (!module) {
+               dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
                                intf_id);
                return;
        }
 
-       ret = gb_svc_read_and_clear_module_boot_status(intf);
-       if (ret)
-               goto destroy_interface;
+       module->disconnected = true;
 
-       intf->unipro_mfg_id = le32_to_cpu(request->data.unipro_mfg_id);
-       intf->unipro_prod_id = le32_to_cpu(request->data.unipro_prod_id);
-       intf->vendor_id = le32_to_cpu(request->data.ara_vend_id);
-       intf->product_id = le32_to_cpu(request->data.ara_prod_id);
+       gb_module_del(module);
+       list_del(&module->hd_node);
+       gb_module_put(module);
+}
 
-       /*
-        * Create a device id for the interface:
-        * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
-        * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
-        *
-        * XXX Do we need to allocate device ID for SVC or the AP here? And what
-        * XXX about an AP with multiple interface blocks?
-        */
-       device_id = ida_simple_get(&svc->device_id_map,
-                                  GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
-       if (device_id < 0) {
-               ret = device_id;
-               dev_err(&svc->dev, "failed to allocate device id for interface %hhu: %d\n",
-                               intf_id, ret);
-               goto destroy_interface;
+static void gb_svc_process_module_inserted(struct gb_operation *operation)
+{
+       struct gb_svc_module_inserted_request *request;
+       struct gb_connection *connection = operation->connection;
+       struct gb_svc *svc = gb_connection_get_data(connection);
+       struct gb_host_device *hd = svc->hd;
+       struct gb_module *module;
+       size_t num_interfaces;
+       u8 module_id;
+       u16 flags;
+       int ret;
+
+       /* The request message size has already been verified. */
+       request = operation->request->payload;
+       module_id = request->primary_intf_id;
+       num_interfaces = request->intf_count;
+       flags = le16_to_cpu(request->flags);
+
+       dev_dbg(&svc->dev, "%s - id = %u, num_interfaces = %zu, flags = 0x%04x\n",
+                       __func__, module_id, num_interfaces, flags);
+
+       if (flags & GB_SVC_MODULE_INSERTED_FLAG_NO_PRIMARY) {
+               dev_warn(&svc->dev, "no primary interface detected on module %u\n",
+                               module_id);
        }
 
-       ret = gb_svc_intf_device_id(svc, intf_id, device_id);
-       if (ret) {
-               dev_err(&svc->dev, "failed to set device id %hhu for interface %hhu: %d\n",
-                               device_id, intf_id, ret);
-               goto ida_put;
+       module = gb_svc_module_lookup(svc, module_id);
+       if (module) {
+               dev_warn(&svc->dev, "unexpected module-inserted event %u\n",
+                               module_id);
+               return;
        }
 
-       /*
-        * Create a two-way route between the AP and the new interface
-        */
-       ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_DEVICE_ID_AP,
-                                 intf_id, device_id);
-       if (ret) {
-               dev_err(&svc->dev, "failed to create route to interface %hhu (device id %hhu): %d\n",
-                               intf_id, device_id, ret);
-               goto svc_id_free;
+       module = gb_module_create(hd, module_id, num_interfaces);
+       if (!module) {
+               dev_err(&svc->dev, "failed to create module\n");
+               return;
        }
 
-       ret = gb_interface_init(intf, device_id);
+       ret = gb_module_add(module);
        if (ret) {
-               dev_err(&svc->dev, "failed to initialize interface %hhu (device id %hhu): %d\n",
-                               intf_id, device_id, ret);
-               goto destroy_route;
+               gb_module_put(module);
+               return;
        }
 
-       return;
+       list_add(&module->hd_node, &hd->modules);
+}
 
-destroy_route:
-       gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
-svc_id_free:
-       /*
-        * XXX Should we tell SVC that this id doesn't belong to interface
-        * XXX anymore.
-        */
-ida_put:
-       ida_simple_remove(&svc->device_id_map, device_id);
-destroy_interface:
-       gb_interface_remove(intf);
+static void gb_svc_process_module_removed(struct gb_operation *operation)
+{
+       struct gb_svc_module_removed_request *request;
+       struct gb_connection *connection = operation->connection;
+       struct gb_svc *svc = gb_connection_get_data(connection);
+       struct gb_module *module;
+       u8 module_id;
+
+       /* The request message size has already been verified. */
+       request = operation->request->payload;
+       module_id = request->primary_intf_id;
+
+       dev_dbg(&svc->dev, "%s - id = %u\n", __func__, module_id);
+
+       module = gb_svc_module_lookup(svc, module_id);
+       if (!module) {
+               dev_warn(&svc->dev, "unexpected module-removed event %u\n",
+                               module_id);
+               return;
+       }
+
+       module->disconnected = true;
+
+       gb_module_del(module);
+       list_del(&module->hd_node);
+       gb_module_put(module);
 }
 
-static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
+static void gb_svc_process_intf_mailbox_event(struct gb_operation *operation)
 {
-       struct gb_svc *svc = operation->connection->private;
-       struct gb_svc_intf_hot_unplug_request *request;
-       struct gb_host_device *hd = operation->connection->hd;
+       struct gb_svc_intf_mailbox_event_request *request;
+       struct gb_connection *connection = operation->connection;
+       struct gb_svc *svc = gb_connection_get_data(connection);
        struct gb_interface *intf;
        u8 intf_id;
+       u16 result_code;
+       u32 mailbox;
 
        /* The request message size has already been verified. */
        request = operation->request->payload;
        intf_id = request->intf_id;
+       result_code = le16_to_cpu(request->result_code);
+       mailbox = le32_to_cpu(request->mailbox);
 
-       dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
+       dev_dbg(&svc->dev, "%s - id = %u, result = 0x%04x, mailbox = 0x%08x\n",
+                       __func__, intf_id, result_code, mailbox);
 
-       intf = gb_interface_find(hd, intf_id);
+       intf = gb_svc_interface_lookup(svc, intf_id);
        if (!intf) {
-               dev_warn(&svc->dev, "could not find hot-unplug interface %hhu\n",
-                               intf_id);
+               dev_warn(&svc->dev, "unexpected mailbox event %u\n", intf_id);
                return;
        }
 
-       gb_svc_intf_remove(svc, intf);
+       gb_interface_mailbox_event(intf, result_code, mailbox);
 }
 
 static void gb_svc_process_deferred_request(struct work_struct *work)
@@ -491,18 +1104,30 @@ static void gb_svc_process_deferred_request(struct work_struct *work)
 
        dr = container_of(work, struct gb_svc_deferred_request, work);
        operation = dr->operation;
-       svc = operation->connection->private;
+       svc = gb_connection_get_data(operation->connection);
        type = operation->request->header->type;
 
        switch (type) {
+       case GB_SVC_TYPE_SVC_HELLO:
+               gb_svc_process_hello_deferred(operation);
+               break;
        case GB_SVC_TYPE_INTF_HOTPLUG:
                gb_svc_process_intf_hotplug(operation);
                break;
        case GB_SVC_TYPE_INTF_HOT_UNPLUG:
                gb_svc_process_intf_hot_unplug(operation);
                break;
+       case GB_SVC_TYPE_MODULE_INSERTED:
+               gb_svc_process_module_inserted(operation);
+               break;
+       case GB_SVC_TYPE_MODULE_REMOVED:
+               gb_svc_process_module_removed(operation);
+               break;
+       case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
+               gb_svc_process_intf_mailbox_event(operation);
+               break;
        default:
-               dev_err(&svc->dev, "bad deferred request type: %02x\n", type);
+               dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
        }
 
        gb_operation_put(operation);
@@ -511,7 +1136,7 @@ static void gb_svc_process_deferred_request(struct work_struct *work)
 
 static int gb_svc_queue_deferred_request(struct gb_operation *operation)
 {
-       struct gb_svc *svc = operation->connection->private;
+       struct gb_svc *svc = gb_connection_get_data(operation->connection);
        struct gb_svc_deferred_request *dr;
 
        dr = kmalloc(sizeof(*dr), GFP_KERNEL);
@@ -539,7 +1164,7 @@ static int gb_svc_queue_deferred_request(struct gb_operation *operation)
  */
 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
 {
-       struct gb_svc *svc = op->connection->private;
+       struct gb_svc *svc = gb_connection_get_data(op->connection);
        struct gb_svc_intf_hotplug_request *request;
 
        if (op->request->payload_size < sizeof(*request)) {
@@ -557,7 +1182,7 @@ static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
 
 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
 {
-       struct gb_svc *svc = op->connection->private;
+       struct gb_svc *svc = gb_connection_get_data(op->connection);
        struct gb_svc_intf_hot_unplug_request *request;
 
        if (op->request->payload_size < sizeof(*request)) {
@@ -575,7 +1200,7 @@ static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
 
 static int gb_svc_intf_reset_recv(struct gb_operation *op)
 {
-       struct gb_svc *svc = op->connection->private;
+       struct gb_svc *svc = gb_connection_get_data(op->connection);
        struct gb_message *request = op->request;
        struct gb_svc_intf_reset_request *reset;
        u8 intf_id;
@@ -594,10 +1219,114 @@ static int gb_svc_intf_reset_recv(struct gb_operation *op)
        return 0;
 }
 
-static int gb_svc_request_recv(u8 type, struct gb_operation *op)
+static int gb_svc_key_code_map(struct gb_svc *svc, u16 key_code, u16 *code)
+{
+       switch (key_code) {
+       case GB_KEYCODE_ARA:
+               *code = SVC_KEY_ARA_BUTTON;
+               break;
+       default:
+               dev_warn(&svc->dev, "unknown keycode received: %u\n", key_code);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int gb_svc_key_event_recv(struct gb_operation *op)
+{
+       struct gb_svc *svc = gb_connection_get_data(op->connection);
+       struct gb_message *request = op->request;
+       struct gb_svc_key_event_request *key;
+       u16 code;
+       u8 event;
+       int ret;
+
+       if (request->payload_size < sizeof(*key)) {
+               dev_warn(&svc->dev, "short key request received (%zu < %zu)\n",
+                        request->payload_size, sizeof(*key));
+               return -EINVAL;
+       }
+
+       key = request->payload;
+
+       ret = gb_svc_key_code_map(svc, le16_to_cpu(key->key_code), &code);
+       if (ret < 0)
+               return ret;
+
+       event = key->key_event;
+       if ((event != GB_SVC_KEY_PRESSED) && (event != GB_SVC_KEY_RELEASED)) {
+               dev_warn(&svc->dev, "unknown key event received: %u\n", event);
+               return -EINVAL;
+       }
+
+       input_report_key(svc->input, code, (event == GB_SVC_KEY_PRESSED));
+       input_sync(svc->input);
+
+       return 0;
+}
+
+static int gb_svc_module_inserted_recv(struct gb_operation *op)
+{
+       struct gb_svc *svc = gb_connection_get_data(op->connection);
+       struct gb_svc_module_inserted_request *request;
+
+       if (op->request->payload_size < sizeof(*request)) {
+               dev_warn(&svc->dev, "short module-inserted request received (%zu < %zu)\n",
+                               op->request->payload_size, sizeof(*request));
+               return -EINVAL;
+       }
+
+       request = op->request->payload;
+
+       dev_dbg(&svc->dev, "%s - id = %u\n", __func__,
+                       request->primary_intf_id);
+
+       return gb_svc_queue_deferred_request(op);
+}
+
+static int gb_svc_module_removed_recv(struct gb_operation *op)
+{
+       struct gb_svc *svc = gb_connection_get_data(op->connection);
+       struct gb_svc_module_removed_request *request;
+
+       if (op->request->payload_size < sizeof(*request)) {
+               dev_warn(&svc->dev, "short module-removed request received (%zu < %zu)\n",
+                               op->request->payload_size, sizeof(*request));
+               return -EINVAL;
+       }
+
+       request = op->request->payload;
+
+       dev_dbg(&svc->dev, "%s - id = %u\n", __func__,
+                       request->primary_intf_id);
+
+       return gb_svc_queue_deferred_request(op);
+}
+
+static int gb_svc_intf_mailbox_event_recv(struct gb_operation *op)
+{
+       struct gb_svc *svc = gb_connection_get_data(op->connection);
+       struct gb_svc_intf_mailbox_event_request *request;
+
+       if (op->request->payload_size < sizeof(*request)) {
+               dev_warn(&svc->dev, "short mailbox request received (%zu < %zu)\n",
+                               op->request->payload_size, sizeof(*request));
+               return -EINVAL;
+       }
+
+       request = op->request->payload;
+
+       dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
+
+       return gb_svc_queue_deferred_request(op);
+}
+
+static int gb_svc_request_handler(struct gb_operation *op)
 {
        struct gb_connection *connection = op->connection;
-       struct gb_svc *svc = connection->private;
+       struct gb_svc *svc = gb_connection_get_data(connection);
+       u8 type = op->type;
        int ret = 0;
 
        /*
@@ -611,7 +1340,7 @@ static int gb_svc_request_recv(u8 type, struct gb_operation *op)
         * need to protect 'state' for any races.
         */
        switch (type) {
-       case GB_REQUEST_TYPE_PROTOCOL_VERSION:
+       case GB_SVC_TYPE_PROTOCOL_VERSION:
                if (svc->state != GB_SVC_STATE_RESET)
                        ret = -EINVAL;
                break;
@@ -632,7 +1361,7 @@ static int gb_svc_request_recv(u8 type, struct gb_operation *op)
        }
 
        switch (type) {
-       case GB_REQUEST_TYPE_PROTOCOL_VERSION:
+       case GB_SVC_TYPE_PROTOCOL_VERSION:
                ret = gb_svc_version_request(op);
                if (!ret)
                        svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
@@ -648,18 +1377,57 @@ static int gb_svc_request_recv(u8 type, struct gb_operation *op)
                return gb_svc_intf_hot_unplug_recv(op);
        case GB_SVC_TYPE_INTF_RESET:
                return gb_svc_intf_reset_recv(op);
+       case GB_SVC_TYPE_KEY_EVENT:
+               return gb_svc_key_event_recv(op);
+       case GB_SVC_TYPE_MODULE_INSERTED:
+               return gb_svc_module_inserted_recv(op);
+       case GB_SVC_TYPE_MODULE_REMOVED:
+               return gb_svc_module_removed_recv(op);
+       case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
+               return gb_svc_intf_mailbox_event_recv(op);
        default:
                dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
                return -EINVAL;
        }
 }
 
+static struct input_dev *gb_svc_input_create(struct gb_svc *svc)
+{
+       struct input_dev *input_dev;
+
+       input_dev = input_allocate_device();
+       if (!input_dev)
+               return ERR_PTR(-ENOMEM);
+
+       input_dev->name = dev_name(&svc->dev);
+       svc->input_phys = kasprintf(GFP_KERNEL, "greybus-%s/input0",
+                                   input_dev->name);
+       if (!svc->input_phys)
+               goto err_free_input;
+
+       input_dev->phys = svc->input_phys;
+       input_dev->dev.parent = &svc->dev;
+
+       input_set_drvdata(input_dev, svc);
+
+       input_set_capability(input_dev, EV_KEY, SVC_KEY_ARA_BUTTON);
+
+       return input_dev;
+
+err_free_input:
+       input_free_device(svc->input);
+       return ERR_PTR(-ENOMEM);
+}
+
 static void gb_svc_release(struct device *dev)
 {
        struct gb_svc *svc = to_gb_svc(dev);
 
+       if (svc->connection)
+               gb_connection_destroy(svc->connection);
        ida_destroy(&svc->device_id_map);
        destroy_workqueue(svc->wq);
+       kfree(svc->input_phys);
        kfree(svc);
 }
 
@@ -668,19 +1436,18 @@ struct device_type greybus_svc_type = {
        .release        = gb_svc_release,
 };
 
-static int gb_svc_connection_init(struct gb_connection *connection)
+struct gb_svc *gb_svc_create(struct gb_host_device *hd)
 {
-       struct gb_host_device *hd = connection->hd;
        struct gb_svc *svc;
 
        svc = kzalloc(sizeof(*svc), GFP_KERNEL);
        if (!svc)
-               return -ENOMEM;
+               return NULL;
 
        svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
        if (!svc->wq) {
                kfree(svc);
-               return -ENOMEM;
+               return NULL;
        }
 
        svc->dev.parent = &hd->dev;
@@ -694,37 +1461,83 @@ static int gb_svc_connection_init(struct gb_connection *connection)
 
        ida_init(&svc->device_id_map);
        svc->state = GB_SVC_STATE_RESET;
-       svc->connection = connection;
-       connection->private = svc;
+       svc->hd = hd;
+
+       svc->input = gb_svc_input_create(svc);
+       if (IS_ERR(svc->input)) {
+               dev_err(&svc->dev, "failed to create input device: %ld\n",
+                       PTR_ERR(svc->input));
+               goto err_put_device;
+       }
+
+       svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
+                                               gb_svc_request_handler);
+       if (IS_ERR(svc->connection)) {
+               dev_err(&svc->dev, "failed to create connection: %ld\n",
+                               PTR_ERR(svc->connection));
+               goto err_free_input;
+       }
+
+       gb_connection_set_data(svc->connection, svc);
 
-       hd->svc = svc;
+       return svc;
+
+err_free_input:
+       input_free_device(svc->input);
+err_put_device:
+       put_device(&svc->dev);
+       return NULL;
+}
+
+int gb_svc_add(struct gb_svc *svc)
+{
+       int ret;
+
+       /*
+        * The SVC protocol is currently driven by the SVC, so the SVC device
+        * is added from the connection request handler when enough
+        * information has been received.
+        */
+       ret = gb_connection_enable(svc->connection);
+       if (ret)
+               return ret;
 
        return 0;
 }
 
-static void gb_svc_connection_exit(struct gb_connection *connection)
+static void gb_svc_remove_modules(struct gb_svc *svc)
+{
+       struct gb_host_device *hd = svc->hd;
+       struct gb_module *module, *tmp;
+
+       list_for_each_entry_safe(module, tmp, &hd->modules, hd_node) {
+               gb_module_del(module);
+               list_del(&module->hd_node);
+               gb_module_put(module);
+       }
+}
+
+void gb_svc_del(struct gb_svc *svc)
 {
-       struct gb_svc *svc = connection->private;
+       gb_connection_disable(svc->connection);
 
-       if (device_is_registered(&svc->dev))
+       /*
+        * The SVC device and input device may have been registered
+        * from the request handler.
+        */
+       if (device_is_registered(&svc->dev)) {
+               gb_svc_debugfs_exit(svc);
+               gb_svc_watchdog_destroy(svc);
+               input_unregister_device(svc->input);
                device_del(&svc->dev);
+       }
 
-       connection->hd->svc = NULL;
-       connection->private = NULL;
+       flush_workqueue(svc->wq);
 
-       put_device(&svc->dev);
+       gb_svc_remove_modules(svc);
 }
 
-static struct gb_protocol svc_protocol = {
-       .name                   = "svc",
-       .id                     = GREYBUS_PROTOCOL_SVC,
-       .major                  = GB_SVC_VERSION_MAJOR,
-       .minor                  = GB_SVC_VERSION_MINOR,
-       .connection_init        = gb_svc_connection_init,
-       .connection_exit        = gb_svc_connection_exit,
-       .request_recv           = gb_svc_request_recv,
-       .flags                  = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
-                                 GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
-                                 GB_PROTOCOL_SKIP_VERSION,
-};
-gb_builtin_protocol_driver(svc_protocol);
+void gb_svc_put(struct gb_svc *svc)
+{
+       put_device(&svc->dev);
+}