Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Greybus driver for the log protocol
0004  *
0005  * Copyright 2016 Google Inc.
0006  */
0007 #include <linux/kernel.h>
0008 #include <linux/module.h>
0009 #include <linux/slab.h>
0010 #include <linux/sizes.h>
0011 #include <linux/uaccess.h>
0012 #include <linux/greybus.h>
0013 
0014 struct gb_log {
0015     struct gb_connection *connection;
0016 };
0017 
0018 static int gb_log_request_handler(struct gb_operation *op)
0019 {
0020     struct gb_connection *connection = op->connection;
0021     struct device *dev = &connection->bundle->dev;
0022     struct gb_log_send_log_request *receive;
0023     u16 len;
0024 
0025     if (op->type != GB_LOG_TYPE_SEND_LOG) {
0026         dev_err(dev, "unknown request type 0x%02x\n", op->type);
0027         return -EINVAL;
0028     }
0029 
0030     /* Verify size of payload */
0031     if (op->request->payload_size < sizeof(*receive)) {
0032         dev_err(dev, "log request too small (%zu < %zu)\n",
0033             op->request->payload_size, sizeof(*receive));
0034         return -EINVAL;
0035     }
0036     receive = op->request->payload;
0037     len = le16_to_cpu(receive->len);
0038     if (len != (op->request->payload_size - sizeof(*receive))) {
0039         dev_err(dev, "log request wrong size %d vs %zu\n", len,
0040             (op->request->payload_size - sizeof(*receive)));
0041         return -EINVAL;
0042     }
0043     if (len == 0) {
0044         dev_err(dev, "log request of 0 bytes?\n");
0045         return -EINVAL;
0046     }
0047 
0048     if (len > GB_LOG_MAX_LEN) {
0049         dev_err(dev, "log request too big: %d\n", len);
0050         return -EINVAL;
0051     }
0052 
0053     /* Ensure the buffer is 0 terminated */
0054     receive->msg[len - 1] = '\0';
0055 
0056     /*
0057      * Print with dev_dbg() so that it can be easily turned off using
0058      * dynamic debugging (and prevent any DoS)
0059      */
0060     dev_dbg(dev, "%s", receive->msg);
0061 
0062     return 0;
0063 }
0064 
0065 static int gb_log_probe(struct gb_bundle *bundle,
0066             const struct greybus_bundle_id *id)
0067 {
0068     struct greybus_descriptor_cport *cport_desc;
0069     struct gb_connection *connection;
0070     struct gb_log *log;
0071     int retval;
0072 
0073     if (bundle->num_cports != 1)
0074         return -ENODEV;
0075 
0076     cport_desc = &bundle->cport_desc[0];
0077     if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LOG)
0078         return -ENODEV;
0079 
0080     log = kzalloc(sizeof(*log), GFP_KERNEL);
0081     if (!log)
0082         return -ENOMEM;
0083 
0084     connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
0085                       gb_log_request_handler);
0086     if (IS_ERR(connection)) {
0087         retval = PTR_ERR(connection);
0088         goto error_free;
0089     }
0090 
0091     log->connection = connection;
0092     greybus_set_drvdata(bundle, log);
0093 
0094     retval = gb_connection_enable(connection);
0095     if (retval)
0096         goto error_connection_destroy;
0097 
0098     return 0;
0099 
0100 error_connection_destroy:
0101     gb_connection_destroy(connection);
0102 error_free:
0103     kfree(log);
0104     return retval;
0105 }
0106 
0107 static void gb_log_disconnect(struct gb_bundle *bundle)
0108 {
0109     struct gb_log *log = greybus_get_drvdata(bundle);
0110     struct gb_connection *connection = log->connection;
0111 
0112     gb_connection_disable(connection);
0113     gb_connection_destroy(connection);
0114 
0115     kfree(log);
0116 }
0117 
0118 static const struct greybus_bundle_id gb_log_id_table[] = {
0119     { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LOG) },
0120     { }
0121 };
0122 MODULE_DEVICE_TABLE(greybus, gb_log_id_table);
0123 
0124 static struct greybus_driver gb_log_driver = {
0125     .name           = "log",
0126     .probe          = gb_log_probe,
0127     .disconnect     = gb_log_disconnect,
0128     .id_table       = gb_log_id_table,
0129 };
0130 module_greybus_driver(gb_log_driver);
0131 
0132 MODULE_LICENSE("GPL v2");