Skip to content

Commit 14dd1ff

Browse files
David Brownelltorvalds
authored andcommitted
memory_accessor: implement the new memory_accessor interfaces for SPI EEPROMs
- Define new setup() hook to export the accessor - Implement accessor methods Moves some error checking out of the sysfs interface code into the layer below it, which is now shared by both sysfs and memory access code. Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Kevin Hilman <khilman@deeprootsystems.com> Cc: Jean Delvare <khali@linux-fr.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7274ec8 commit 14dd1ff

2 files changed

Lines changed: 50 additions & 14 deletions

File tree

‎drivers/misc/eeprom/at25.c‎

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
struct at25_data {
3232
struct spi_device *spi;
33+
struct memory_accessor mem;
3334
struct mutex lock;
3435
struct spi_eeprom chip;
3536
struct bin_attribute bin;
@@ -75,6 +76,13 @@ at25_ee_read(
7576
struct spi_transfer t[2];
7677
struct spi_message m;
7778

79+
if (unlikely(offset >= at25->bin.size))
80+
return 0;
81+
if ((offset + count) > at25->bin.size)
82+
count = at25->bin.size - offset;
83+
if (unlikely(!count))
84+
return count;
85+
7886
cp = command;
7987
*cp++ = AT25_READ;
8088

@@ -127,13 +135,6 @@ at25_bin_read(struct kobject *kobj, struct bin_attribute *bin_attr,
127135
dev = container_of(kobj, struct device, kobj);
128136
at25 = dev_get_drvdata(dev);
129137

130-
if (unlikely(off >= at25->bin.size))
131-
return 0;
132-
if ((off + count) > at25->bin.size)
133-
count = at25->bin.size - off;
134-
if (unlikely(!count))
135-
return count;
136-
137138
return at25_ee_read(at25, buf, off, count);
138139
}
139140

@@ -146,6 +147,13 @@ at25_ee_write(struct at25_data *at25, char *buf, loff_t off, size_t count)
146147
unsigned buf_size;
147148
u8 *bounce;
148149

150+
if (unlikely(off >= at25->bin.size))
151+
return -EFBIG;
152+
if ((off + count) > at25->bin.size)
153+
count = at25->bin.size - off;
154+
if (unlikely(!count))
155+
return count;
156+
149157
/* Temp buffer starts with command and address */
150158
buf_size = at25->chip.page_size;
151159
if (buf_size > io_limit)
@@ -253,18 +261,31 @@ at25_bin_write(struct kobject *kobj, struct bin_attribute *bin_attr,
253261
dev = container_of(kobj, struct device, kobj);
254262
at25 = dev_get_drvdata(dev);
255263

256-
if (unlikely(off >= at25->bin.size))
257-
return -EFBIG;
258-
if ((off + count) > at25->bin.size)
259-
count = at25->bin.size - off;
260-
if (unlikely(!count))
261-
return count;
262-
263264
return at25_ee_write(at25, buf, off, count);
264265
}
265266

266267
/*-------------------------------------------------------------------------*/
267268

269+
/* Let in-kernel code access the eeprom data. */
270+
271+
static ssize_t at25_mem_read(struct memory_accessor *mem, char *buf,
272+
off_t offset, size_t count)
273+
{
274+
struct at25_data *at25 = container_of(mem, struct at25_data, mem);
275+
276+
return at25_ee_read(at25, buf, offset, count);
277+
}
278+
279+
static ssize_t at25_mem_write(struct memory_accessor *mem, char *buf,
280+
off_t offset, size_t count)
281+
{
282+
struct at25_data *at25 = container_of(mem, struct at25_data, mem);
283+
284+
return at25_ee_write(at25, buf, offset, count);
285+
}
286+
287+
/*-------------------------------------------------------------------------*/
288+
268289
static int at25_probe(struct spi_device *spi)
269290
{
270291
struct at25_data *at25 = NULL;
@@ -317,24 +338,33 @@ static int at25_probe(struct spi_device *spi)
317338
at25->addrlen = addrlen;
318339

319340
/* Export the EEPROM bytes through sysfs, since that's convenient.
341+
* And maybe to other kernel code; it might hold a board's Ethernet
342+
* address, or board-specific calibration data generated on the
343+
* manufacturing floor.
344+
*
320345
* Default to root-only access to the data; EEPROMs often hold data
321346
* that's sensitive for read and/or write, like ethernet addresses,
322347
* security codes, board-specific manufacturing calibrations, etc.
323348
*/
324349
at25->bin.attr.name = "eeprom";
325350
at25->bin.attr.mode = S_IRUSR;
326351
at25->bin.read = at25_bin_read;
352+
at25->mem.read = at25_mem_read;
327353

328354
at25->bin.size = at25->chip.byte_len;
329355
if (!(chip->flags & EE_READONLY)) {
330356
at25->bin.write = at25_bin_write;
331357
at25->bin.attr.mode |= S_IWUSR;
358+
at25->mem.write = at25_mem_write;
332359
}
333360

334361
err = sysfs_create_bin_file(&spi->dev.kobj, &at25->bin);
335362
if (err)
336363
goto fail;
337364

365+
if (chip->setup)
366+
chip->setup(&at25->mem, chip->context);
367+
338368
dev_info(&spi->dev, "%Zd %s %s eeprom%s, pagesize %u\n",
339369
(at25->bin.size < 1024)
340370
? at25->bin.size

‎include/linux/spi/eeprom.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef __LINUX_SPI_EEPROM_H
22
#define __LINUX_SPI_EEPROM_H
33

4+
#include <linux/memory.h>
5+
46
/*
57
* Put one of these structures in platform_data for SPI EEPROMS handled
68
* by the "at25" driver. On SPI, most EEPROMS understand the same core
@@ -17,6 +19,10 @@ struct spi_eeprom {
1719
#define EE_ADDR2 0x0002 /* 16 bit addrs */
1820
#define EE_ADDR3 0x0004 /* 24 bit addrs */
1921
#define EE_READONLY 0x0008 /* disallow writes */
22+
23+
/* for exporting this chip's data to other kernel code */
24+
void (*setup)(struct memory_accessor *mem, void *context);
25+
void *context;
2026
};
2127

2228
#endif /* __LINUX_SPI_EEPROM_H */

0 commit comments

Comments
 (0)