Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0+
  2// Expose the vboot context nvram to userspace
  3//
  4// Copyright (C) 2012 Google, Inc.
  5// Copyright (C) 2015 Collabora Ltd.
  6
  7#include <linux/of.h>
  8#include <linux/platform_device.h>
 
  9#include <linux/module.h>
 10#include <linux/platform_data/cros_ec_commands.h>
 11#include <linux/platform_data/cros_ec_proto.h>
 12#include <linux/slab.h>
 13
 14#define DRV_NAME "cros-ec-vbc"
 15
 16static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
 17				  struct bin_attribute *att, char *buf,
 18				  loff_t pos, size_t count)
 19{
 20	struct device *dev = kobj_to_dev(kobj);
 21	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
 22	struct cros_ec_device *ecdev = ec->ec_dev;
 
 23	struct cros_ec_command *msg;
 24	/*
 25	 * This should be a pointer to the same type as op field in
 26	 * struct ec_params_vbnvcontext.
 27	 */
 28	uint32_t *params_op;
 29	int err;
 30	const size_t para_sz = sizeof(*params_op);
 31	const size_t resp_sz = sizeof(struct ec_response_vbnvcontext);
 32	const size_t payload = max(para_sz, resp_sz);
 33
 34	msg = kmalloc(sizeof(*msg) + payload, GFP_KERNEL);
 35	if (!msg)
 36		return -ENOMEM;
 37
 38	/* NB: we only kmalloc()ated enough space for the op field */
 39	params_op = (uint32_t *)msg->data;
 40	*params_op = EC_VBNV_CONTEXT_OP_READ;
 41
 42	msg->version = EC_VER_VBNV_CONTEXT;
 43	msg->command = EC_CMD_VBNV_CONTEXT;
 44	msg->outsize = para_sz;
 45	msg->insize = resp_sz;
 46
 47	err = cros_ec_cmd_xfer_status(ecdev, msg);
 48	if (err < 0) {
 49		dev_err(dev, "Error sending read request: %d\n", err);
 50		kfree(msg);
 51		return err;
 52	}
 53
 54	memcpy(buf, msg->data, resp_sz);
 55
 56	kfree(msg);
 57	return resp_sz;
 58}
 59
 60static ssize_t vboot_context_write(struct file *filp, struct kobject *kobj,
 61				   struct bin_attribute *attr, char *buf,
 62				   loff_t pos, size_t count)
 63{
 64	struct device *dev = kobj_to_dev(kobj);
 65	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
 66	struct cros_ec_device *ecdev = ec->ec_dev;
 67	struct ec_params_vbnvcontext *params;
 68	struct cros_ec_command *msg;
 69	int err;
 70	const size_t para_sz = sizeof(*params);
 71	const size_t data_sz = sizeof(params->block);
 72
 73	/* Only write full values */
 74	if (count != data_sz)
 75		return -EINVAL;
 76
 77	msg = kmalloc(sizeof(*msg) + para_sz, GFP_KERNEL);
 78	if (!msg)
 79		return -ENOMEM;
 80
 81	params = (struct ec_params_vbnvcontext *)msg->data;
 82	params->op = EC_VBNV_CONTEXT_OP_WRITE;
 83	memcpy(params->block, buf, data_sz);
 84
 85	msg->version = EC_VER_VBNV_CONTEXT;
 86	msg->command = EC_CMD_VBNV_CONTEXT;
 87	msg->outsize = para_sz;
 88	msg->insize = 0;
 89
 90	err = cros_ec_cmd_xfer_status(ecdev, msg);
 91	if (err < 0) {
 92		dev_err(dev, "Error sending write request: %d\n", err);
 93		kfree(msg);
 94		return err;
 95	}
 96
 97	kfree(msg);
 98	return data_sz;
 99}
100
101static BIN_ATTR_RW(vboot_context, 16);
102
103static struct bin_attribute *cros_ec_vbc_bin_attrs[] = {
104	&bin_attr_vboot_context,
105	NULL
106};
107
108static const struct attribute_group cros_ec_vbc_attr_group = {
109	.name = "vbc",
110	.bin_attrs = cros_ec_vbc_bin_attrs,
111};
112
113static int cros_ec_vbc_probe(struct platform_device *pd)
114{
115	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
116	struct device *dev = &pd->dev;
117	int ret;
118
119	ret = sysfs_create_group(&ec_dev->class_dev.kobj,
120				 &cros_ec_vbc_attr_group);
121	if (ret < 0)
122		dev_err(dev, "failed to create %s attributes. err=%d\n",
123			cros_ec_vbc_attr_group.name, ret);
124
125	return ret;
126}
127
128static void cros_ec_vbc_remove(struct platform_device *pd)
129{
130	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
131
132	sysfs_remove_group(&ec_dev->class_dev.kobj,
133			   &cros_ec_vbc_attr_group);
 
 
134}
135
136static struct platform_driver cros_ec_vbc_driver = {
137	.driver = {
138		.name = DRV_NAME,
139	},
140	.probe = cros_ec_vbc_probe,
141	.remove_new = cros_ec_vbc_remove,
142};
143
144module_platform_driver(cros_ec_vbc_driver);
145
146MODULE_LICENSE("GPL");
147MODULE_DESCRIPTION("Expose the vboot context nvram to userspace");
148MODULE_ALIAS("platform:" DRV_NAME);
v5.4
  1// SPDX-License-Identifier: GPL-2.0+
  2// Expose the vboot context nvram to userspace
  3//
  4// Copyright (C) 2012 Google, Inc.
  5// Copyright (C) 2015 Collabora Ltd.
  6
  7#include <linux/of.h>
  8#include <linux/platform_device.h>
  9#include <linux/mfd/cros_ec.h>
 10#include <linux/module.h>
 11#include <linux/platform_data/cros_ec_commands.h>
 12#include <linux/platform_data/cros_ec_proto.h>
 13#include <linux/slab.h>
 14
 15#define DRV_NAME "cros-ec-vbc"
 16
 17static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
 18				  struct bin_attribute *att, char *buf,
 19				  loff_t pos, size_t count)
 20{
 21	struct device *dev = container_of(kobj, struct device, kobj);
 22	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
 23	struct cros_ec_device *ecdev = ec->ec_dev;
 24	struct ec_params_vbnvcontext *params;
 25	struct cros_ec_command *msg;
 
 
 
 
 
 26	int err;
 27	const size_t para_sz = sizeof(params->op);
 28	const size_t resp_sz = sizeof(struct ec_response_vbnvcontext);
 29	const size_t payload = max(para_sz, resp_sz);
 30
 31	msg = kmalloc(sizeof(*msg) + payload, GFP_KERNEL);
 32	if (!msg)
 33		return -ENOMEM;
 34
 35	/* NB: we only kmalloc()ated enough space for the op field */
 36	params = (struct ec_params_vbnvcontext *)msg->data;
 37	params->op = EC_VBNV_CONTEXT_OP_READ;
 38
 39	msg->version = EC_VER_VBNV_CONTEXT;
 40	msg->command = EC_CMD_VBNV_CONTEXT;
 41	msg->outsize = para_sz;
 42	msg->insize = resp_sz;
 43
 44	err = cros_ec_cmd_xfer(ecdev, msg);
 45	if (err < 0) {
 46		dev_err(dev, "Error sending read request: %d\n", err);
 47		kfree(msg);
 48		return err;
 49	}
 50
 51	memcpy(buf, msg->data, resp_sz);
 52
 53	kfree(msg);
 54	return resp_sz;
 55}
 56
 57static ssize_t vboot_context_write(struct file *filp, struct kobject *kobj,
 58				   struct bin_attribute *attr, char *buf,
 59				   loff_t pos, size_t count)
 60{
 61	struct device *dev = container_of(kobj, struct device, kobj);
 62	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
 63	struct cros_ec_device *ecdev = ec->ec_dev;
 64	struct ec_params_vbnvcontext *params;
 65	struct cros_ec_command *msg;
 66	int err;
 67	const size_t para_sz = sizeof(*params);
 68	const size_t data_sz = sizeof(params->block);
 69
 70	/* Only write full values */
 71	if (count != data_sz)
 72		return -EINVAL;
 73
 74	msg = kmalloc(sizeof(*msg) + para_sz, GFP_KERNEL);
 75	if (!msg)
 76		return -ENOMEM;
 77
 78	params = (struct ec_params_vbnvcontext *)msg->data;
 79	params->op = EC_VBNV_CONTEXT_OP_WRITE;
 80	memcpy(params->block, buf, data_sz);
 81
 82	msg->version = EC_VER_VBNV_CONTEXT;
 83	msg->command = EC_CMD_VBNV_CONTEXT;
 84	msg->outsize = para_sz;
 85	msg->insize = 0;
 86
 87	err = cros_ec_cmd_xfer(ecdev, msg);
 88	if (err < 0) {
 89		dev_err(dev, "Error sending write request: %d\n", err);
 90		kfree(msg);
 91		return err;
 92	}
 93
 94	kfree(msg);
 95	return data_sz;
 96}
 97
 98static BIN_ATTR_RW(vboot_context, 16);
 99
100static struct bin_attribute *cros_ec_vbc_bin_attrs[] = {
101	&bin_attr_vboot_context,
102	NULL
103};
104
105static struct attribute_group cros_ec_vbc_attr_group = {
106	.name = "vbc",
107	.bin_attrs = cros_ec_vbc_bin_attrs,
108};
109
110static int cros_ec_vbc_probe(struct platform_device *pd)
111{
112	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
113	struct device *dev = &pd->dev;
114	int ret;
115
116	ret = sysfs_create_group(&ec_dev->class_dev.kobj,
117				 &cros_ec_vbc_attr_group);
118	if (ret < 0)
119		dev_err(dev, "failed to create %s attributes. err=%d\n",
120			cros_ec_vbc_attr_group.name, ret);
121
122	return ret;
123}
124
125static int cros_ec_vbc_remove(struct platform_device *pd)
126{
127	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
128
129	sysfs_remove_group(&ec_dev->class_dev.kobj,
130			   &cros_ec_vbc_attr_group);
131
132	return 0;
133}
134
135static struct platform_driver cros_ec_vbc_driver = {
136	.driver = {
137		.name = DRV_NAME,
138	},
139	.probe = cros_ec_vbc_probe,
140	.remove = cros_ec_vbc_remove,
141};
142
143module_platform_driver(cros_ec_vbc_driver);
144
145MODULE_LICENSE("GPL");
146MODULE_DESCRIPTION("Expose the vboot context nvram to userspace");
147MODULE_ALIAS("platform:" DRV_NAME);