Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * cros_ec_vbc - Expose the vboot context nvram to userspace
  3 *
  4 * Copyright (C) 2015 Collabora Ltd.
  5 *
  6 * based on vendor driver,
  7 *
  8 * Copyright (C) 2012 The Chromium OS Authors
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 */
 20
 21#include <linux/of.h>
 22#include <linux/platform_device.h>
 23#include <linux/mfd/cros_ec.h>
 24#include <linux/mfd/cros_ec_commands.h>
 
 25#include <linux/slab.h>
 26
 
 
 27static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
 28				  struct bin_attribute *att, char *buf,
 29				  loff_t pos, size_t count)
 30{
 31	struct device *dev = container_of(kobj, struct device, kobj);
 32	struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
 33					      class_dev);
 34	struct cros_ec_device *ecdev = ec->ec_dev;
 35	struct ec_params_vbnvcontext *params;
 36	struct cros_ec_command *msg;
 37	int err;
 38	const size_t para_sz = sizeof(params->op);
 39	const size_t resp_sz = sizeof(struct ec_response_vbnvcontext);
 40	const size_t payload = max(para_sz, resp_sz);
 41
 42	msg = kmalloc(sizeof(*msg) + payload, GFP_KERNEL);
 43	if (!msg)
 44		return -ENOMEM;
 45
 46	/* NB: we only kmalloc()ated enough space for the op field */
 47	params = (struct ec_params_vbnvcontext *)msg->data;
 48	params->op = EC_VBNV_CONTEXT_OP_READ;
 49
 50	msg->version = EC_VER_VBNV_CONTEXT;
 51	msg->command = EC_CMD_VBNV_CONTEXT;
 52	msg->outsize = para_sz;
 53	msg->insize = resp_sz;
 54
 55	err = cros_ec_cmd_xfer(ecdev, msg);
 56	if (err < 0) {
 57		dev_err(dev, "Error sending read request: %d\n", err);
 58		kfree(msg);
 59		return err;
 60	}
 61
 62	memcpy(buf, msg->data, resp_sz);
 63
 64	kfree(msg);
 65	return resp_sz;
 66}
 67
 68static ssize_t vboot_context_write(struct file *filp, struct kobject *kobj,
 69				   struct bin_attribute *attr, char *buf,
 70				   loff_t pos, size_t count)
 71{
 72	struct device *dev = container_of(kobj, struct device, kobj);
 73	struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
 74					      class_dev);
 75	struct cros_ec_device *ecdev = ec->ec_dev;
 76	struct ec_params_vbnvcontext *params;
 77	struct cros_ec_command *msg;
 78	int err;
 79	const size_t para_sz = sizeof(*params);
 80	const size_t data_sz = sizeof(params->block);
 81
 82	/* Only write full values */
 83	if (count != data_sz)
 84		return -EINVAL;
 85
 86	msg = kmalloc(sizeof(*msg) + para_sz, GFP_KERNEL);
 87	if (!msg)
 88		return -ENOMEM;
 89
 90	params = (struct ec_params_vbnvcontext *)msg->data;
 91	params->op = EC_VBNV_CONTEXT_OP_WRITE;
 92	memcpy(params->block, buf, data_sz);
 93
 94	msg->version = EC_VER_VBNV_CONTEXT;
 95	msg->command = EC_CMD_VBNV_CONTEXT;
 96	msg->outsize = para_sz;
 97	msg->insize = 0;
 98
 99	err = cros_ec_cmd_xfer(ecdev, msg);
100	if (err < 0) {
101		dev_err(dev, "Error sending write request: %d\n", err);
102		kfree(msg);
103		return err;
104	}
105
106	kfree(msg);
107	return data_sz;
108}
109
110static umode_t cros_ec_vbc_is_visible(struct kobject *kobj,
111				      struct bin_attribute *a, int n)
112{
113	struct device *dev = container_of(kobj, struct device, kobj);
114	struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
115					      class_dev);
116	struct device_node *np = ec->ec_dev->dev->of_node;
117
118	if (IS_ENABLED(CONFIG_OF) && np) {
119		if (of_property_read_bool(np, "google,has-vbc-nvram"))
120			return a->attr.mode;
121	}
122
123	return 0;
124}
125
126static BIN_ATTR_RW(vboot_context, 16);
127
128static struct bin_attribute *cros_ec_vbc_bin_attrs[] = {
129	&bin_attr_vboot_context,
130	NULL
131};
132
133struct attribute_group cros_ec_vbc_attr_group = {
134	.name = "vbc",
135	.bin_attrs = cros_ec_vbc_bin_attrs,
136	.is_bin_visible = cros_ec_vbc_is_visible,
137};
v6.2
  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 ec_params_vbnvcontext *params;
 24	struct cros_ec_command *msg;
 25	int err;
 26	const size_t para_sz = sizeof(params->op);
 27	const size_t resp_sz = sizeof(struct ec_response_vbnvcontext);
 28	const size_t payload = max(para_sz, resp_sz);
 29
 30	msg = kmalloc(sizeof(*msg) + payload, GFP_KERNEL);
 31	if (!msg)
 32		return -ENOMEM;
 33
 34	/* NB: we only kmalloc()ated enough space for the op field */
 35	params = (struct ec_params_vbnvcontext *)msg->data;
 36	params->op = EC_VBNV_CONTEXT_OP_READ;
 37
 38	msg->version = EC_VER_VBNV_CONTEXT;
 39	msg->command = EC_CMD_VBNV_CONTEXT;
 40	msg->outsize = para_sz;
 41	msg->insize = resp_sz;
 42
 43	err = cros_ec_cmd_xfer_status(ecdev, msg);
 44	if (err < 0) {
 45		dev_err(dev, "Error sending read request: %d\n", err);
 46		kfree(msg);
 47		return err;
 48	}
 49
 50	memcpy(buf, msg->data, resp_sz);
 51
 52	kfree(msg);
 53	return resp_sz;
 54}
 55
 56static ssize_t vboot_context_write(struct file *filp, struct kobject *kobj,
 57				   struct bin_attribute *attr, char *buf,
 58				   loff_t pos, size_t count)
 59{
 60	struct device *dev = kobj_to_dev(kobj);
 61	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
 
 62	struct cros_ec_device *ecdev = ec->ec_dev;
 63	struct ec_params_vbnvcontext *params;
 64	struct cros_ec_command *msg;
 65	int err;
 66	const size_t para_sz = sizeof(*params);
 67	const size_t data_sz = sizeof(params->block);
 68
 69	/* Only write full values */
 70	if (count != data_sz)
 71		return -EINVAL;
 72
 73	msg = kmalloc(sizeof(*msg) + para_sz, GFP_KERNEL);
 74	if (!msg)
 75		return -ENOMEM;
 76
 77	params = (struct ec_params_vbnvcontext *)msg->data;
 78	params->op = EC_VBNV_CONTEXT_OP_WRITE;
 79	memcpy(params->block, buf, data_sz);
 80
 81	msg->version = EC_VER_VBNV_CONTEXT;
 82	msg->command = EC_CMD_VBNV_CONTEXT;
 83	msg->outsize = para_sz;
 84	msg->insize = 0;
 85
 86	err = cros_ec_cmd_xfer_status(ecdev, msg);
 87	if (err < 0) {
 88		dev_err(dev, "Error sending write request: %d\n", err);
 89		kfree(msg);
 90		return err;
 91	}
 92
 93	kfree(msg);
 94	return data_sz;
 95}
 96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 97static BIN_ATTR_RW(vboot_context, 16);
 98
 99static struct bin_attribute *cros_ec_vbc_bin_attrs[] = {
100	&bin_attr_vboot_context,
101	NULL
102};
103
104static const struct attribute_group cros_ec_vbc_attr_group = {
105	.name = "vbc",
106	.bin_attrs = cros_ec_vbc_bin_attrs,
 
107};
108
109static int cros_ec_vbc_probe(struct platform_device *pd)
110{
111	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
112	struct device *dev = &pd->dev;
113	int ret;
114
115	ret = sysfs_create_group(&ec_dev->class_dev.kobj,
116				 &cros_ec_vbc_attr_group);
117	if (ret < 0)
118		dev_err(dev, "failed to create %s attributes. err=%d\n",
119			cros_ec_vbc_attr_group.name, ret);
120
121	return ret;
122}
123
124static int cros_ec_vbc_remove(struct platform_device *pd)
125{
126	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
127
128	sysfs_remove_group(&ec_dev->class_dev.kobj,
129			   &cros_ec_vbc_attr_group);
130
131	return 0;
132}
133
134static struct platform_driver cros_ec_vbc_driver = {
135	.driver = {
136		.name = DRV_NAME,
137	},
138	.probe = cros_ec_vbc_probe,
139	.remove = cros_ec_vbc_remove,
140};
141
142module_platform_driver(cros_ec_vbc_driver);
143
144MODULE_LICENSE("GPL");
145MODULE_DESCRIPTION("Expose the vboot context nvram to userspace");
146MODULE_ALIAS("platform:" DRV_NAME);