3030
3131struct 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+
268289static 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
0 commit comments