greybus: interface: Get manifest using Control protocol
[cascardo/linux.git] / drivers / staging / greybus / ap.c
index e606a45..113fd87 100644 (file)
@@ -2,6 +2,7 @@
  * Greybus "AP" message loop handling
  *
  * Copyright 2014 Google Inc.
+ * Copyright 2014 Linaro Ltd.
  *
  * Released under the GPLv2 only.
  */
@@ -14,7 +15,7 @@
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/uaccess.h>
-#include <linux/kthread.h>
+#include <linux/workqueue.h>
 #include <linux/device.h>
 #include "svc_msg.h"
 #include "greybus_manifest.h"
@@ -24,15 +25,12 @@ struct ap_msg {
        u8 *data;
        size_t size;
        struct greybus_host_device *hd;
-       struct list_head list;
+       struct work_struct event;
 };
 
-static LIST_HEAD(ap_msg_list);
-static spinlock_t ap_msg_list_lock;
-static struct task_struct *ap_thread;
-static wait_queue_head_t ap_wait;
+static struct workqueue_struct *ap_workqueue;
 
-static struct svc_msg *svc_msg_alloc(enum svc_function_type type)
+static struct svc_msg *svc_msg_alloc(enum svc_function_id id)
 {
        struct svc_msg *svc_msg;
 
@@ -40,8 +38,8 @@ static struct svc_msg *svc_msg_alloc(enum svc_function_type type)
        if (!svc_msg)
                return NULL;
 
-       // FIXME - verify we are only sending message types we should be
-       svc_msg->header.type = type;
+       // FIXME - verify we are only sending function IDs we should be
+       svc_msg->header.function_id = id;
        return svc_msg;
 }
 
@@ -56,22 +54,55 @@ static int svc_msg_send(struct svc_msg *svc_msg, struct greybus_host_device *hd)
 
        // FIXME - Do we need to do more than just pass it to the hd and then
        // free it?
-       retval = hd->driver->send_svc_msg(svc_msg, hd);
+       retval = hd->driver->submit_svc(svc_msg, hd);
 
        svc_msg_free(svc_msg);
        return retval;
 }
 
 
+int svc_set_route_send(struct gb_bundle *bundle,
+                              struct greybus_host_device *hd)
+{
+       struct svc_msg *svc_msg;
+
+       svc_msg = svc_msg_alloc(SVC_FUNCTION_UNIPRO_NETWORK_MANAGEMENT);
+       if (!svc_msg)
+               return -ENOMEM;
+
+       svc_msg->header.message_type = SVC_MSG_DATA;
+       svc_msg->header.payload_length =
+               cpu_to_le16(sizeof(struct svc_function_unipro_set_route));
+       svc_msg->management.set_route.device_id = bundle->device_id;
+
+       return svc_msg_send(svc_msg, hd);
+}
+
 static void svc_handshake(struct svc_function_handshake *handshake,
-                         struct greybus_host_device *hd)
+                         int payload_length, struct greybus_host_device *hd)
 {
        struct svc_msg *svc_msg;
 
-       /* A new SVC communication channel, let's verify it was for us */
+       if (payload_length != sizeof(*handshake)) {
+               dev_err(hd->parent,
+                       "Illegal size of svc handshake message %d\n",
+                       payload_length);
+               return;
+       }
+
+       /* A new SVC communication channel, let's verify a supported version */
+       if ((handshake->version_major != GREYBUS_VERSION_MAJOR) ||
+           (handshake->version_minor != GREYBUS_VERSION_MINOR)) {
+               dev_warn(hd->parent,
+                       "received invalid greybus version %u.%u\n",
+                       handshake->version_major, handshake->version_minor);
+               return;
+       }
+
+       /* Validate that the handshake came from the SVC */
        if (handshake->handshake_type != SVC_HANDSHAKE_SVC_HELLO) {
                /* we don't know what to do with this, log it and return */
-               dev_dbg(&hd->dev, "received invalid handshake type %d\n",
+               dev_dbg(hd->parent, "received invalid handshake type %d\n",
                        handshake->handshake_type);
                return;
        }
@@ -81,189 +112,217 @@ static void svc_handshake(struct svc_function_handshake *handshake,
        if (!svc_msg)
                return;
 
+       svc_msg->header.message_type = SVC_MSG_DATA;
+       svc_msg->header.payload_length =
+               cpu_to_le16(sizeof(*handshake));
+       svc_msg->handshake.version_major = GREYBUS_VERSION_MAJOR;
+       svc_msg->handshake.version_minor = GREYBUS_VERSION_MINOR;
        svc_msg->handshake.handshake_type = SVC_HANDSHAKE_AP_HELLO;
-       svc_msg_send(svc_msg, hd);
+
+       (void)svc_msg_send(svc_msg, hd);
 }
 
 static void svc_management(struct svc_function_unipro_management *management,
-                          struct greybus_host_device *hd)
+                          int payload_length, struct greybus_host_device *hd)
 {
-       /* What?  An AP should not get this message */
-       dev_err(&hd->dev, "Got an svc management message???\n");
+       struct gb_interface *intf;
+       int ret;
+
+       if (payload_length != sizeof(*management)) {
+               dev_err(hd->parent,
+                       "Illegal size of svc management message %d\n",
+                       payload_length);
+               return;
+       }
+
+       switch (management->management_packet_type) {
+       case SVC_MANAGEMENT_AP_ID:
+               hd->device_id = management->ap_id.device_id;
+               break;
+       case SVC_MANAGEMENT_LINK_UP:
+               intf = gb_interface_find(hd, management->link_up.interface_id);
+               if (!intf) {
+                       dev_err(hd->parent, "Interface ID %d not found\n",
+                               management->link_up.interface_id);
+                       return;
+               }
+               ret = gb_interface_init(intf, management->link_up.device_id);
+               if (ret) {
+                       dev_err(hd->parent,
+                               "error %d initializing interface %hhu\n",
+                               ret, management->link_up.interface_id);
+                       return;
+               }
+               break;
+       default:
+               dev_err(hd->parent, "Unhandled UniPro management message\n");
+       }
 }
 
 static void svc_hotplug(struct svc_function_hotplug *hotplug,
-                       struct greybus_host_device *hd)
+                       int payload_length, struct greybus_host_device *hd)
 {
-       u8 module_id = hotplug->module_id;
+       u8 interface_id = hotplug->interface_id;
 
        switch (hotplug->hotplug_event) {
        case SVC_HOTPLUG_EVENT:
-               dev_dbg(&hd->dev, "module id %d added\n", module_id);
-               // FIXME - add the module to the system
+               /* Add a new interface to the system */
+               if (payload_length < 0x03) {
+                       /* Hotplug message is at least 3 bytes big */
+                       dev_err(hd->parent,
+                               "Illegal size of svc hotplug message %d\n",
+                               payload_length);
+                       return;
+               }
+               dev_dbg(hd->parent, "interface id %d added\n", interface_id);
+               gb_interface_create(hd, interface_id);
                break;
 
        case SVC_HOTUNPLUG_EVENT:
-               dev_dbg(&hd->dev, "module id %d removed\n", module_id);
-               // FIXME - remove the module from the system
+               /* Remove a interface from the system */
+               if (payload_length != 0x02) {
+                       /* Hotunplug message is only 2 bytes big */
+                       dev_err(hd->parent,
+                               "Illegal size of svc hotunplug message %d\n",
+                               payload_length);
+                       return;
+               }
+               dev_dbg(hd->parent, "interface id %d removed\n", interface_id);
+               gb_interface_remove(hd, interface_id);
                break;
 
        default:
-               dev_err(&hd->dev, "received invalid hotplug message type %d\n",
+               dev_err(hd->parent,
+                       "Received invalid hotplug message type %d\n",
                        hotplug->hotplug_event);
                break;
        }
 }
 
-static void svc_ddb(struct svc_function_ddb *ddb,
-                   struct greybus_host_device *hd)
-{
-       /* What?  An AP should not get this message */
-       dev_err(&hd->dev, "Got an svc DDB message???\n");
-}
-
 static void svc_power(struct svc_function_power *power,
-                     struct greybus_host_device *hd)
+                     int payload_length, struct greybus_host_device *hd)
 {
-       u8 module_id = power->module_id;
+       u8 interface_id = power->interface_id;
 
+       /*
+        * The AP is only allowed to get a Battery Status message, not a Battery
+        * Status Request
+        */
        if (power->power_type != SVC_POWER_BATTERY_STATUS) {
-               dev_err(&hd->dev, "received invalid power type %d\n",
+               dev_err(hd->parent, "Received invalid power type %d\n",
                        power->power_type);
                return;
        }
 
-       dev_dbg(&hd->dev, "power status for module id %d is %d\n",
-               module_id, power->status.status);
+       /*
+        * As struct struct svc_function_power_battery_status_request is 0 bytes
+        * big, we can just check the union of the whole structure to validate
+        * the size of this message.
+        */
+       if (payload_length != sizeof(*power)) {
+               dev_err(hd->parent,
+                       "Illegal size of svc power message %d\n",
+                       payload_length);
+               return;
+       }
+
+       dev_dbg(hd->parent, "power status for interface id %d is %d\n",
+               interface_id, power->status.status);
 
        // FIXME - do something with the power information, like update our
        // battery information...
 }
 
 static void svc_epm(struct svc_function_epm *epm,
-                   struct greybus_host_device *hd)
+                   int payload_length, struct greybus_host_device *hd)
 {
        /* What?  An AP should not get this message */
-       dev_err(&hd->dev, "Got an EPM message???\n");
+       dev_err(hd->parent, "Got an EPM message???\n");
 }
 
 static void svc_suspend(struct svc_function_suspend *suspend,
-                       struct greybus_host_device *hd)
+                       int payload_length, struct greybus_host_device *hd)
 {
        /* What?  An AP should not get this message */
-       dev_err(&hd->dev, "Got an suspend message???\n");
+       dev_err(hd->parent, "Got an suspend message???\n");
 }
 
 static struct svc_msg *convert_ap_message(struct ap_msg *ap_msg)
 {
        struct svc_msg *svc_msg;
-
-       // FIXME - validate message, right now we are trusting the size and data
-       // from the AP, what could go wrong?  :)
-       // for now, just cast the pointer and run away...
+       struct svc_msg_header *header;
+       struct greybus_host_device *hd = ap_msg->hd;
 
        svc_msg = (struct svc_msg *)ap_msg->data;
+       header = &svc_msg->header;
 
-       /* Verify the version is something we can handle with this code */
-       if ((svc_msg->header.version_major != GREYBUS_VERSION_MAJOR) &&
-           (svc_msg->header.version_minor != GREYBUS_VERSION_MINOR))
+       /* Validate the message type */
+       if (header->message_type != SVC_MSG_DATA) {
+               dev_err(hd->parent, "message type %d received?\n",
+                       header->message_type);
                return NULL;
+       }
+
+       /*
+        * The validation of the size of the message buffer happens in each
+        * svc_* function, due to the different types of messages, keeping the
+        * logic for each message only in one place.
+        */
 
        return svc_msg;
 }
 
-static void process_ap_message(struct ap_msg *ap_msg)
+static void ap_process_event(struct work_struct *work)
 {
        struct svc_msg *svc_msg;
        struct greybus_host_device *hd;
+       struct ap_msg *ap_msg;
+       int payload_length;
+
+       ap_msg = container_of(work, struct ap_msg, event);
+       hd = ap_msg->hd;
 
        /* Turn the "raw" data into a real message */
        svc_msg = convert_ap_message(ap_msg);
-       if (!svc_msg) {
-               // FIXME log an error???
+       if (!svc_msg)
                return;
-       }
-
-       hd = ap_msg->hd;
 
-       /* Pass the message to the host controller */
-//     ap_msg->hd->driver->ap_msg(svc_msg, ap_msg->hd);
+       payload_length = le16_to_cpu(svc_msg->header.payload_length);
 
        /* Look at the message to figure out what to do with it */
-       switch (svc_msg->header.type) {
+       switch (svc_msg->header.function_id) {
        case SVC_FUNCTION_HANDSHAKE:
-               svc_handshake(&svc_msg->handshake, hd);
+               svc_handshake(&svc_msg->handshake, payload_length, hd);
                break;
        case SVC_FUNCTION_UNIPRO_NETWORK_MANAGEMENT:
-               svc_management(&svc_msg->management, hd);
+               svc_management(&svc_msg->management, payload_length, hd);
                break;
        case SVC_FUNCTION_HOTPLUG:
-               svc_hotplug(&svc_msg->hotplug, hd);
-               break;
-       case SVC_FUNCTION_DDB:
-               svc_ddb(&svc_msg->ddb, hd);
+               svc_hotplug(&svc_msg->hotplug, payload_length, hd);
                break;
        case SVC_FUNCTION_POWER:
-               svc_power(&svc_msg->power, hd);
+               svc_power(&svc_msg->power, payload_length, hd);
                break;
        case SVC_FUNCTION_EPM:
-               svc_epm(&svc_msg->epm, hd);
+               svc_epm(&svc_msg->epm, payload_length, hd);
                break;
        case SVC_FUNCTION_SUSPEND:
-               svc_suspend(&svc_msg->suspend, hd);
+               svc_suspend(&svc_msg->suspend, payload_length, hd);
                break;
        default:
-               dev_err(&hd->dev, "received invalid SVC message type %d\n",
-                       svc_msg->header.type);
+               dev_err(hd->parent, "received invalid SVC function ID %d\n",
+                       svc_msg->header.function_id);
        }
 
-
-}
-
-static struct ap_msg *get_ap_msg(void)
-{
-       struct ap_msg *ap_msg;
-       unsigned long flags;
-
-       spin_lock_irqsave(&ap_msg_list_lock, flags);
-
-       ap_msg = list_first_entry_or_null(&ap_msg_list, struct ap_msg, list);
-       if (ap_msg != NULL)
-               list_del(&ap_msg->list);
-       spin_unlock_irqrestore(&ap_msg_list_lock, flags);
-
-       return ap_msg;
+       /* clean the message up */
+       kfree(ap_msg->data);
+       kfree(ap_msg);
 }
 
-static int ap_process_loop(void *data)
+int greybus_svc_in(struct greybus_host_device *hd, u8 *data, int size)
 {
        struct ap_msg *ap_msg;
 
-       while (!kthread_should_stop()) {
-               wait_event_interruptible(ap_wait, kthread_should_stop());
-
-               if (kthread_should_stop())
-                       break;
-
-               /* Get some data off of the ap list and process it */
-               ap_msg = get_ap_msg();
-               if (!ap_msg)
-                       continue;
-
-               process_ap_message(ap_msg);
-
-               /* clean the message up */
-               kfree(ap_msg->data);
-               kfree(ap_msg);
-       }
-       return 0;
-}
-
-int gb_new_ap_msg(u8 *data, int size, struct greybus_host_device *hd)
-{
-       struct ap_msg *ap_msg;
-       unsigned long flags;
-
        /*
         * Totally naive copy the message into a new structure that we slowly
         * create and add it to the list.  Let's get this working, the odds of
@@ -287,39 +346,26 @@ int gb_new_ap_msg(u8 *data, int size, struct greybus_host_device *hd)
        ap_msg->size = size;
        ap_msg->hd = hd;
 
-       spin_lock_irqsave(&ap_msg_list_lock, flags);
-       list_add(&ap_msg->list, &ap_msg_list);
-       spin_unlock_irqrestore(&ap_msg_list_lock, flags);
-
-       /* kick our thread to handle the message */
-       wake_up_interruptible(&ap_wait);
+       INIT_WORK(&ap_msg->event, ap_process_event);
+       queue_work(ap_workqueue, &ap_msg->event);
 
        return 0;
 }
-EXPORT_SYMBOL_GPL(gb_new_ap_msg);
+EXPORT_SYMBOL_GPL(greybus_svc_in);
 
-void greybus_cport_in_data(struct greybus_host_device *hd, int cport, u8 *data,
-                          size_t length)
+int __init gb_ap_init(void)
 {
-       // FIXME - implement...
-}
-EXPORT_SYMBOL_GPL(greybus_cport_in_data);
-
-int gb_thread_init(void)
-{
-       init_waitqueue_head(&ap_wait);
-       spin_lock_init(&ap_msg_list_lock);
-
-       ap_thread = kthread_run(ap_process_loop, NULL, "greybus_ap");
-       if (IS_ERR(ap_thread))
-               return PTR_ERR(ap_thread);
+       ap_workqueue = alloc_ordered_workqueue("greybus_ap", 0);
+       if (!ap_workqueue)
+               return -ENOMEM;
 
        return 0;
 }
 
-void gb_thread_destroy(void)
+void gb_ap_exit(void)
 {
-       kthread_stop(ap_thread);
+       destroy_workqueue(ap_workqueue);
+       ap_workqueue = NULL;
 }