]> git.karo-electronics.de Git - linux-beck.git/commitdiff
greybus: add support for the log protocol
authorJoel Porquet <porquet_joel@projectara.com>
Fri, 24 Jun 2016 21:41:36 +0000 (14:41 -0700)
committerGreg Kroah-Hartman <gregkh@google.com>
Fri, 24 Jun 2016 22:46:52 +0000 (15:46 -0700)
Add support for the new Log class/protocol. This protocol allows modules
to send their internal logging messages to the AP in order to make
module debugging easier.

The protocol is, for now, composed a single module-initiated request.
This request contains a message and associated length. The message is
integrated in the kernel log with dev_dbg(). In order to be displayed
with 'dmesg', the following command needs to be entered first:

$ echo "file log.c +p" > /sys/kernel/debug/dynamic_debug/control

The major portion of this file was initially written by Greg KH.

Signed-off-by: Joel Porquet <porquet_joel@projectara.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
drivers/staging/greybus/Makefile
drivers/staging/greybus/greybus_manifest.h
drivers/staging/greybus/greybus_protocols.h
drivers/staging/greybus/log.c [new file with mode: 0644]

index 40325b32c5f7c0b392e40f2c0b7602a9e8eb501c..bd9967ca46c6b7e0b4203403725db2064c30a1b8 100644 (file)
@@ -18,6 +18,7 @@ gb-gbphy-y := gbphy.o
 # Prefix all modules with gb-
 gb-vibrator-y := vibrator.o
 gb-power-supply-y := power_supply.o
+gb-log-y := log.o
 gb-loopback-y := loopback.o
 gb-light-y := light.o
 gb-raw-y := raw.o
@@ -46,6 +47,7 @@ obj-m += greybus.o
 obj-m += gb-gbphy.o
 obj-m += gb-vibrator.o
 obj-m += gb-power-supply.o
+obj-m += gb-log.o
 obj-m += gb-loopback.o
 obj-m += gb-light.o
 obj-m += gb-hid.o
index 28bbadd057d1d8b531736e696aeb0d3298d2cb3f..2ae39ad923683fd9a8afbaba23f97eb9c3e14040 100644 (file)
@@ -47,6 +47,7 @@ enum greybus_protocol {
        GREYBUS_PROTOCOL_CAMERA_DATA    = 0x16,
        GREYBUS_PROTOCOL_FW_DOWNLOAD    = 0x17,
        GREYBUS_PROTOCOL_FW_MANAGEMENT  = 0x18,
+       GREYBUS_PROTOCOL_LOG            = 0x1a,
                /* ... */
        GREYBUS_PROTOCOL_RAW            = 0xfe,
        GREYBUS_PROTOCOL_VENDOR         = 0xff,
@@ -76,6 +77,7 @@ enum greybus_class_type {
        /* 0x14 is unused */
        GREYBUS_CLASS_BOOTROM           = 0x15,
        GREYBUS_CLASS_FW_MANAGEMENT     = 0x16,
+       GREYBUS_CLASS_LOG               = 0x17,
                /* ... */
        GREYBUS_CLASS_RAW               = 0xfe,
        GREYBUS_CLASS_VENDOR            = 0xff,
index 63dd2041fdeb20125bb89b6634c08e1ea99c0198..203bc46151e7d7e41f30a855a5f3fe7d397516bb 100644 (file)
@@ -2136,5 +2136,19 @@ struct gb_audio_send_data_request {
        __u8    data[0];
 } __packed;
 
+
+/* Log */
+
+/* operations */
+#define GB_LOG_TYPE_SEND_LOG   0x02
+
+/* length */
+#define GB_LOG_MAX_LEN         1024
+
+struct gb_log_send_log_request {
+       __le16  len;
+       __u8    msg[0];
+} __packed;
+
 #endif /* __GREYBUS_PROTOCOLS_H */
 
diff --git a/drivers/staging/greybus/log.c b/drivers/staging/greybus/log.c
new file mode 100644 (file)
index 0000000..70dd9e5
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * Greybus driver for the log protocol
+ *
+ * Copyright 2016 Google Inc.
+ *
+ * Released under the GPLv2 only.
+ */
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/sizes.h>
+#include <linux/uaccess.h>
+
+#include "greybus.h"
+
+struct gb_log {
+       struct gb_connection *connection;
+};
+
+static int gb_log_request_handler(struct gb_operation *op)
+{
+       struct gb_connection *connection = op->connection;
+       struct device *dev = &connection->bundle->dev;
+       struct gb_log_send_log_request *receive;
+       u16 len;
+
+       if (op->type != GB_LOG_TYPE_SEND_LOG) {
+               dev_err(dev, "unknown request type 0x%02x\n", op->type);
+               return -EINVAL;
+       }
+
+       /* Verify size of payload */
+       if (op->request->payload_size < sizeof(*receive)) {
+               dev_err(dev, "log request too small (%zu < %zu)\n",
+                               op->request->payload_size, sizeof(*receive));
+               return -EINVAL;
+       }
+       receive = op->request->payload;
+       len = le16_to_cpu(receive->len);
+       if (len != (int)(op->request->payload_size - sizeof(*receive))) {
+               dev_err(dev, "log request wrong size %d vs %d\n", len,
+                               (int)(op->request->payload_size - sizeof(*receive)));
+               return -EINVAL;
+       }
+       if (len == 0) {
+               dev_err(dev, "log request of 0 bytes?\n");
+               return -EINVAL;
+       }
+
+       if (len > GB_LOG_MAX_LEN) {
+               dev_err(dev, "log request too big: %d\n", len);
+               return -EINVAL;
+       }
+
+       /* Ensure the buffer is 0 terminated */
+       receive->msg[len - 1] = '\0';
+
+       /* Print with dev_dbg() so that it can be easily turned off using
+        * dynamic debugging (and prevent any DoS) */
+       dev_dbg(dev, "%s", receive->msg);
+
+       return 0;
+}
+
+static int gb_log_probe(struct gb_bundle *bundle,
+                       const struct greybus_bundle_id *id)
+{
+       struct greybus_descriptor_cport *cport_desc;
+       struct gb_connection *connection;
+       struct gb_log *log;
+       int retval;
+
+       if (bundle->num_cports != 1)
+               return -ENODEV;
+
+       cport_desc = &bundle->cport_desc[0];
+       if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LOG)
+               return -ENODEV;
+
+       log = kzalloc(sizeof(*log), GFP_KERNEL);
+       if (!log)
+               return -ENOMEM;
+
+       connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
+                       gb_log_request_handler);
+       if (IS_ERR(connection)) {
+               retval = PTR_ERR(connection);
+               goto error_free;
+       }
+
+       log->connection = connection;
+       greybus_set_drvdata(bundle, log);
+
+       retval = gb_connection_enable(connection);
+       if (retval)
+               goto error_connection_destroy;
+
+       return 0;
+
+error_connection_destroy:
+       gb_connection_destroy(connection);
+error_free:
+       kfree(log);
+       return retval;
+}
+
+static void gb_log_disconnect(struct gb_bundle *bundle)
+{
+       struct gb_log *log = greybus_get_drvdata(bundle);
+       struct gb_connection *connection = log->connection;
+
+       gb_connection_disable(connection);
+       gb_connection_destroy(connection);
+
+       kfree(log);
+}
+
+static const struct greybus_bundle_id gb_log_id_table[] = {
+       { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LOG) },
+       { }
+};
+MODULE_DEVICE_TABLE(greybus, gb_log_id_table);
+
+static struct greybus_driver gb_log_driver = {
+       .name           = "log",
+       .probe          = gb_log_probe,
+       .disconnect     = gb_log_disconnect,
+       .id_table       = gb_log_id_table,
+};
+module_greybus_driver(gb_log_driver);
+
+MODULE_LICENSE("GPL v2");