From: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
To: Miquel Raynal <miquel.raynal@bootlin.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Michael Walle" <michael@walle.cc>,
"Rafał Miłecki" <rafal@milecki.pl>,
"Rob Herring" <robh+dt@kernel.org>,
"Frank Rowand" <frowand.list@gmail.com>,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
"Robert Marko" <robert.marko@sartura.hr>,
"Thomas Petazzoni" <thomas.petazzoni@bootlin.com>,
"Luka Perkov" <luka.perkov@sartura.hr>,
"Randy Dunlap" <rdunlap@infradead.org>,
"Chen-Yu Tsai" <wenst@chromium.org>,
"Daniel Golle" <daniel@makrotopia.org>
Subject: Re: [PATCH v12 7/7] nvmem: core: Expose cells through sysfs
Date: Mon, 9 Oct 2023 10:48:17 +0100 [thread overview]
Message-ID: <318fe799-f53e-64ed-b631-d099bb5202f4@linaro.org> (raw)
In-Reply-To: <20231005155907.2701706-8-miquel.raynal@bootlin.com>
On 05/10/2023 16:59, Miquel Raynal wrote:
> , int index);
> +
> +static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj,
> + struct bin_attribute *attr, char *buf,
> + loff_t pos, size_t count)
> +{
> + struct nvmem_cell_entry *entry;
> + struct nvmem_cell *cell = NULL;
> + size_t cell_sz, read_len;
> + void *content;
> +
> + entry = attr->private;
> + cell = nvmem_create_cell(entry, entry->name, 0);
> + if (IS_ERR(cell))
> + return PTR_ERR(cell);
> +
> + if (!cell)
> + return -EINVAL;
> +
> + content = nvmem_cell_read(cell, &cell_sz);
> + if (IS_ERR(content)) {
> + read_len = PTR_ERR(content);
> + goto destroy_cell;
> + }
> +
> + read_len = min_t(unsigned int, cell_sz - pos, count);
> + memcpy(buf, content + pos, read_len);
> + kfree(content);
> +
> +destroy_cell:
> + kfree_const(cell->id);
> + kfree(cell);
> +
> + return read_len;
> +}
> +
> /* default read/write permissions */
> static struct bin_attribute bin_attr_rw_nvmem = {
> .attr = {
> @@ -320,11 +357,21 @@ static const struct attribute_group nvmem_bin_group = {
> .is_bin_visible = nvmem_bin_attr_is_visible,
> };
>
> +/* Cell attributes will be dynamically allocated */
> +static struct attribute_group nvmem_cells_group = {
> + .name = "cells",
> +};
> +
> static const struct attribute_group *nvmem_dev_groups[] = {
> &nvmem_bin_group,
> NULL,
> };
>
> +static const struct attribute_group *nvmem_cells_groups[] = {
> + &nvmem_cells_group,
> + NULL,
> +};
> +
> static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
> .attr = {
> .name = "eeprom",
> @@ -380,6 +427,68 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
> device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
> }
>
> +static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
> +{
> + struct bin_attribute **cells_attrs, *attrs;
> + struct nvmem_cell_entry *entry;
> + unsigned int ncells = 0, i = 0;
> + int ret = 0;
> +
> + mutex_lock(&nvmem_mutex);
> +
> + if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated) {
> + nvmem_cells_group.bin_attrs = NULL;
> + goto unlock_mutex;
> + }
> +
> + /* Allocate an array of attributes with a sentinel */
> + ncells = list_count_nodes(&nvmem->cells);
> + cells_attrs = devm_kcalloc(&nvmem->dev, ncells + 1,
> + sizeof(struct bin_attribute *), GFP_KERNEL);
> + if (!cells_attrs) {
> + ret = -ENOMEM;
> + goto unlock_mutex;
> + }
> +
> + attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
> + if (!attrs) {
> + ret = -ENOMEM;
> + goto unlock_mutex;
> + }
> +
> + /* Initialize each attribute to take the name and size of the cell */
> + list_for_each_entry(entry, &nvmem->cells, node) {
> + sysfs_bin_attr_init(&attrs[i]);
> + attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL,
> + "%s@%x", entry->name,
> + entry->offset);
> + attrs[i].attr.mode = 0444;
> + attrs[i].size = entry->bytes;
> + attrs[i].read = &nvmem_cell_attr_read;
> + attrs[i].private = entry;
> + if (!attrs[i].attr.name) {
> + ret = -ENOMEM;
> + goto unlock_mutex;
> + }
> +
> + cells_attrs[i] = &attrs[i];
> + i++;
> + }
> +
> + nvmem_cells_group.bin_attrs = cells_attrs;
> +
> + ret = devm_device_add_groups(&nvmem->dev, nvmem_cells_groups);
> + if (ret)
> + goto unlock_mutex;
This is going to create groups after the nvmem device is added, isn't
this going to be problem with user space notifications?
--srini
> +
> + nvmem->sysfs_cells_populated = true;
> +
> +unlock_mutex:
> + mutex_unlock(&nvmem_mutex);
> +
> + return ret;
next prev parent reply other threads:[~2023-10-09 9:48 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-05 15:59 [PATCH v12 0/7] NVMEM cells in sysfs Miquel Raynal
2023-10-05 15:59 ` [PATCH v12 1/7] of: device: Export of_device_make_bus_id() Miquel Raynal
2023-10-06 17:02 ` Rob Herring
2023-10-05 15:59 ` [PATCH v12 2/7] nvmem: Clarify the situation when there is no DT node available Miquel Raynal
2023-10-06 11:41 ` Rafał Miłecki
2023-10-06 16:32 ` Miquel Raynal
2023-10-07 16:09 ` Rafał Miłecki
2023-10-08 13:39 ` Miquel Raynal
2023-10-09 9:44 ` Srinivas Kandagatla
2023-10-05 15:59 ` [PATCH v12 3/7] nvmem: Move of_nvmem_layout_get_container() in another header Miquel Raynal
2023-10-05 15:59 ` [PATCH v12 4/7] nvmem: Create a header for internal sharing Miquel Raynal
2023-10-05 15:59 ` [PATCH v12 5/7] nvmem: core: Rework layouts to become regular devices Miquel Raynal
2023-10-06 11:49 ` Rafał Miłecki
2023-10-06 16:33 ` Miquel Raynal
2023-10-07 16:31 ` Greg Kroah-Hartman
2023-10-11 10:33 ` Miquel Raynal
2023-10-08 13:42 ` kernel test robot
2023-10-09 9:44 ` Srinivas Kandagatla
2023-10-11 7:38 ` Miquel Raynal
2023-10-11 10:02 ` Srinivas Kandagatla
2023-10-11 10:58 ` Miquel Raynal
2023-10-05 15:59 ` [PATCH v12 6/7] ABI: sysfs-nvmem-cells: Expose cells through sysfs Miquel Raynal
2023-10-05 15:59 ` [PATCH v12 7/7] nvmem: core: " Miquel Raynal
2023-10-09 9:48 ` Srinivas Kandagatla [this message]
2023-10-11 7:15 ` Miquel Raynal
2023-10-11 8:27 ` Srinivas Kandagatla
2023-10-11 8:33 ` Miquel Raynal
2023-10-11 8:45 ` Srinivas Kandagatla
2023-10-11 8:58 ` Miquel Raynal
2023-10-11 9:26 ` Srinivas Kandagatla
2023-10-11 9:44 ` Miquel Raynal
2023-10-11 10:02 ` Srinivas Kandagatla
2023-10-11 11:09 ` Miquel Raynal
2023-10-11 13:56 ` Srinivas Kandagatla
2023-10-11 14:02 ` Miquel Raynal
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=318fe799-f53e-64ed-b631-d099bb5202f4@linaro.org \
--to=srinivas.kandagatla@linaro.org \
--cc=daniel@makrotopia.org \
--cc=devicetree@vger.kernel.org \
--cc=frowand.list@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luka.perkov@sartura.hr \
--cc=michael@walle.cc \
--cc=miquel.raynal@bootlin.com \
--cc=rafal@milecki.pl \
--cc=rdunlap@infradead.org \
--cc=robert.marko@sartura.hr \
--cc=robh+dt@kernel.org \
--cc=thomas.petazzoni@bootlin.com \
--cc=wenst@chromium.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).