kernel-fxtec-pro1x/drivers/nvmem/nvmem-sysfs.c
Ivaylo Georgiev 24b44cf5bf Merge android-4.19-q.74 (09e57ab) into msm-4.19
* refs/heads/tmp-09e57ab:
  Linux 4.19.74
  x86/build: Add -Wnoaddress-of-packed-member to REALMODE_CFLAGS, to silence GCC9 build warning
  nvmem: Use the same permissions for eeprom as for nvmem
  rsi: fix a double free bug in rsi_91x_deinit()
  platform/x86: pmc_atom: Add CB4063 Beckhoff Automation board to critclk_systems DMI table
  modules: fix compile error if don't have strict module rwx
  modules: fix BUG when load module with rodata=n
  iio: adc: stm32-dfsdm: fix data type
  Revert "Bluetooth: btusb: driver to enable the usb-wakeup feature"
  drm/mediatek: mtk_drm_drv.c: Add of_node_put() before goto
  drm: panel-orientation-quirks: Add extra quirk table entry for GPD MicroPC
  firmware: ti_sci: Always request response from firmware
  crypto: talitos - HMAC SNOOP NO AFEU mode requires SW icv checking.
  crypto: talitos - Do not modify req->cryptlen on decryption.
  crypto: talitos - fix ECB algs ivsize
  crypto: talitos - check data blocksize in ablkcipher.
  crypto: talitos - fix CTR alg blocksize
  crypto: talitos - check AES key size
  driver core: Fix use-after-free and double free on glue directory
  ubifs: Correctly use tnc_next() in search_dh_cookie()
  gpio: fix line flag validation in lineevent_create
  PCI: Always allow probing with driver_override
  mtd: rawnand: mtk: Fix wrongly assigned OOB buffer pointer issue
  clk: rockchip: Don't yell about bad mmc phases when getting
  drm/meson: Add support for XBGR8888 & ABGR8888 formats
  powerpc: Add barrier_nospec to raw_copy_in_user()
  x86/purgatory: Change compiler flags from -mcmodel=kernel to -mcmodel=large to fix kexec relocation errors
  KVM: nVMX: handle page fault in vmread
  KVM: x86: work around leak of uninitialized stack contents
  KVM: s390: Do not leak kernel stack data in the KVM_S390_INTERRUPT ioctl
  KVM: s390: kvm_s390_vm_start_migration: check dirty_bitmap before using it as target for memset()
  genirq: Prevent NULL pointer dereference in resend_irqs()
  ixgbe: Prevent u8 wrapping of ITR value to something less than 10us
  Btrfs: fix assertion failure during fsync and use of stale transaction
  gpio: fix line flag validation in linehandle_create
  gpiolib: acpi: Add gpiolib_acpi_run_edge_events_on_boot option and blacklist
  tun: fix use-after-free when register netdev failed
  tipc: add NULL pointer check before calling kfree_rcu
  tcp: fix tcp_ecn_withdraw_cwr() to clear TCP_ECN_QUEUE_CWR
  sctp: use transport pf_retrans in sctp_do_8_2_transport_strike
  sctp: Fix the link time qualifier of 'sctp_ctrlsock_exit()'
  sch_hhf: ensure quantum and hhf_non_hh_weight are non-zero
  net: sched: fix reordering issues
  net: phylink: Fix flow control resolution
  net: gso: Fix skb_segment splat when splitting gso_size mangled skb having linear-headed frag_list
  net: Fix null de-reference of device refcount
  ixgbe: Fix secpath usage for IPsec TX offload.
  isdn/capi: check message length in capi_write()
  ipv6: Fix the link time qualifier of 'ping_v6_proc_exit_net()'
  cdc_ether: fix rndis support for Mediatek based smartphones
  bridge/mdb: remove wrong use of NLM_F_MULTI

Conflicts:
	drivers/base/core.c
	drivers/nvmem/core.c

Change-Id: I8c07f90247a135a354d3eac89283e0bf8a1b7edf
Signed-off-by: Ivaylo Georgiev <irgeorgiev@codeaurora.org>
2019-09-20 00:45:59 -07:00

237 lines
5 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019, Linaro Limited
*/
#include "nvmem.h"
#ifdef CONFIG_DEBUG_LOCK_ALLOC
static struct lock_class_key eeprom_lock_key;
#endif
static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
struct bin_attribute *attr,
char *buf, loff_t pos, size_t count)
{
struct device *dev;
struct nvmem_device *nvmem;
int rc;
if (attr->private)
dev = attr->private;
else
dev = container_of(kobj, struct device, kobj);
nvmem = to_nvmem_device(dev);
/* Stop the user from reading */
if (pos >= nvmem->size)
return 0;
if (count < nvmem->word_size)
return -EINVAL;
if (pos + count > nvmem->size)
count = nvmem->size - pos;
count = round_down(count, nvmem->word_size);
rc = nvmem->reg_read(nvmem->priv, pos, buf, count);
if (rc)
return rc;
return count;
}
static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
struct bin_attribute *attr,
char *buf, loff_t pos, size_t count)
{
struct device *dev;
struct nvmem_device *nvmem;
int rc;
if (attr->private)
dev = attr->private;
else
dev = container_of(kobj, struct device, kobj);
nvmem = to_nvmem_device(dev);
/* Stop the user from writing */
if (pos >= nvmem->size)
return -EFBIG;
if (count < nvmem->word_size)
return -EINVAL;
if (pos + count > nvmem->size)
count = nvmem->size - pos;
count = round_down(count, nvmem->word_size);
rc = nvmem->reg_write(nvmem->priv, pos, buf, count);
if (rc)
return rc;
return count;
}
/* default read/write permissions */
static struct bin_attribute bin_attr_rw_nvmem = {
.attr = {
.name = "nvmem",
.mode = 0644,
},
.read = bin_attr_nvmem_read,
.write = bin_attr_nvmem_write,
};
static struct bin_attribute *nvmem_bin_rw_attributes[] = {
&bin_attr_rw_nvmem,
NULL,
};
static const struct attribute_group nvmem_bin_rw_group = {
.bin_attrs = nvmem_bin_rw_attributes,
};
static const struct attribute_group *nvmem_rw_dev_groups[] = {
&nvmem_bin_rw_group,
NULL,
};
/* read only permission */
static struct bin_attribute bin_attr_ro_nvmem = {
.attr = {
.name = "nvmem",
.mode = 0444,
},
.read = bin_attr_nvmem_read,
};
static struct bin_attribute *nvmem_bin_ro_attributes[] = {
&bin_attr_ro_nvmem,
NULL,
};
static const struct attribute_group nvmem_bin_ro_group = {
.bin_attrs = nvmem_bin_ro_attributes,
};
static const struct attribute_group *nvmem_ro_dev_groups[] = {
&nvmem_bin_ro_group,
NULL,
};
/* default read/write permissions, root only */
static struct bin_attribute bin_attr_rw_root_nvmem = {
.attr = {
.name = "nvmem",
.mode = 0600,
},
.read = bin_attr_nvmem_read,
.write = bin_attr_nvmem_write,
};
static struct bin_attribute *nvmem_bin_rw_root_attributes[] = {
&bin_attr_rw_root_nvmem,
NULL,
};
static const struct attribute_group nvmem_bin_rw_root_group = {
.bin_attrs = nvmem_bin_rw_root_attributes,
};
static const struct attribute_group *nvmem_rw_root_dev_groups[] = {
&nvmem_bin_rw_root_group,
NULL,
};
/* read only permission, root only */
static struct bin_attribute bin_attr_ro_root_nvmem = {
.attr = {
.name = "nvmem",
.mode = 0400,
},
.read = bin_attr_nvmem_read,
};
static struct bin_attribute *nvmem_bin_ro_root_attributes[] = {
&bin_attr_ro_root_nvmem,
NULL,
};
static const struct attribute_group nvmem_bin_ro_root_group = {
.bin_attrs = nvmem_bin_ro_root_attributes,
};
static const struct attribute_group *nvmem_ro_root_dev_groups[] = {
&nvmem_bin_ro_root_group,
NULL,
};
const struct attribute_group **nvmem_sysfs_get_groups(
struct nvmem_device *nvmem,
const struct nvmem_config *config)
{
if (config->root_only)
return nvmem->read_only ?
nvmem_ro_root_dev_groups :
nvmem_rw_root_dev_groups;
return nvmem->read_only ? nvmem_ro_dev_groups : nvmem_rw_dev_groups;
}
/*
* nvmem_setup_compat() - Create an additional binary entry in
* drivers sys directory, to be backwards compatible with the older
* drivers/misc/eeprom drivers.
*/
int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
const struct nvmem_config *config)
{
int rval;
if (!config->compat)
return 0;
if (!config->base_dev)
return -EINVAL;
if (nvmem->read_only) {
if (config->root_only)
nvmem->eeprom = bin_attr_ro_root_nvmem;
else
nvmem->eeprom = bin_attr_ro_nvmem;
} else {
if (config->root_only)
nvmem->eeprom = bin_attr_rw_root_nvmem;
else
nvmem->eeprom = bin_attr_rw_nvmem;
}
nvmem->eeprom.attr.name = "eeprom";
nvmem->eeprom.size = nvmem->size;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
nvmem->eeprom.attr.key = &eeprom_lock_key;
#endif
nvmem->eeprom.private = &nvmem->dev;
nvmem->base_dev = config->base_dev;
rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
if (rval) {
dev_err(&nvmem->dev,
"Failed to create eeprom binary file %d\n", rval);
return rval;
}
nvmem->flags |= FLAG_COMPAT;
return 0;
}
void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
const struct nvmem_config *config)
{
if (config->compat)
device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
}