Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Direct MTD block device access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright © 2000-2003 Nicolas Pitre <nico@fluxnic.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/mtd/blktrans.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) struct mtdblk_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct mtd_blktrans_dev mbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct mutex cache_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	unsigned char *cache_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	unsigned long cache_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	unsigned int cache_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * Cache stuff...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * Since typical flash erasable sectors are much larger than what Linux's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * buffer cache can handle, we must implement read-modify-write on flash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * sectors for each block write requests.  To avoid over-erasing flash sectors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * and to speed things up, we locally cache a whole flash sector while it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * being written to until a different sector is required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static int erase_write (struct mtd_info *mtd, unsigned long pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 			unsigned int len, const char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct erase_info erase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	size_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	 * First, let's erase the flash block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	erase.addr = pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	erase.len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	ret = mtd_erase(mtd, &erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 				     "on \"%s\" failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			pos, len, mtd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	 * Next, write the data to flash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	ret = mtd_write(mtd, pos, len, &retlen, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (retlen != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static int write_cached_data (struct mtdblk_dev *mtdblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct mtd_info *mtd = mtdblk->mbd.mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (mtdblk->cache_state != STATE_DIRTY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	pr_debug("mtdblock: writing cached data for \"%s\" "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			"at 0x%lx, size 0x%x\n", mtd->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			mtdblk->cache_offset, mtdblk->cache_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	ret = erase_write (mtd, mtdblk->cache_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			   mtdblk->cache_size, mtdblk->cache_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 * Here we could arguably set the cache state to STATE_CLEAN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 * However this could lead to inconsistency since we will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	 * be notified if this content is altered on the flash by other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	 * means.  Let's declare it empty and leave buffering tasks to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	 * the buffer cache instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	 * If this cache_offset points to a bad block, data cannot be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	 * written to the device. Clear cache_state to avoid writing to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	 * bad blocks repeatedly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (ret == 0 || ret == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		mtdblk->cache_state = STATE_EMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			    int len, const char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct mtd_info *mtd = mtdblk->mbd.mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	unsigned int sect_size = mtdblk->cache_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	size_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	pr_debug("mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		mtd->name, pos, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (!sect_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return mtd_write(mtd, pos, len, &retlen, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		unsigned long sect_start = (pos/sect_size)*sect_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		unsigned int offset = pos - sect_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		unsigned int size = sect_size - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		if( size > len )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		if (size == sect_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			 * We are covering a whole sector.  Thus there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			 * need to bother with the cache while it may still be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			 * useful for other partial writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			ret = erase_write (mtd, pos, size, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			/* Partial sector: need to use the cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			if (mtdblk->cache_state == STATE_DIRTY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			    mtdblk->cache_offset != sect_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 				ret = write_cached_data(mtdblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 				if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 					return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			if (mtdblk->cache_state == STATE_EMPTY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			    mtdblk->cache_offset != sect_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				/* fill the cache with the current sector */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				mtdblk->cache_state = STATE_EMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 				ret = mtd_read(mtd, sect_start, sect_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 					       &retlen, mtdblk->cache_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 					return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				if (retlen != sect_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 					return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				mtdblk->cache_offset = sect_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				mtdblk->cache_size = sect_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				mtdblk->cache_state = STATE_CLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			/* write data to our local cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			memcpy (mtdblk->cache_data + offset, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			mtdblk->cache_state = STATE_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		buf += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		pos += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		len -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			   int len, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	struct mtd_info *mtd = mtdblk->mbd.mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	unsigned int sect_size = mtdblk->cache_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	size_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	pr_debug("mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			mtd->name, pos, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (!sect_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		return mtd_read(mtd, pos, len, &retlen, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		unsigned long sect_start = (pos/sect_size)*sect_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		unsigned int offset = pos - sect_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		unsigned int size = sect_size - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		if (size > len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		 * Check if the requested data is already cached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		 * Read the requested amount of data from our internal cache if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		 * contains what we want, otherwise we read the data directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		 * from flash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		if (mtdblk->cache_state != STATE_EMPTY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		    mtdblk->cache_offset == sect_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			memcpy (buf, mtdblk->cache_data + offset, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			ret = mtd_read(mtd, pos, size, &retlen, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			if (retlen != size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		buf += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		pos += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		len -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			      unsigned long block, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return do_cached_read(mtdblk, block<<9, 512, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			      unsigned long block, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		mtdblk->cache_data = vmalloc(mtdblk->mbd.mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		if (!mtdblk->cache_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		/* -EINTR is not really correct, but it is the best match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		 * documented in man 2 write for all cases.  We could also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		 * return -EAGAIN sometimes, but why bother?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return do_cached_write(mtdblk, block<<9, 512, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static int mtdblock_open(struct mtd_blktrans_dev *mbd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	pr_debug("mtdblock_open\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (mtdblk->count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		mtdblk->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	/* OK, it's not open. Create cache info for it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	mtdblk->count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	mutex_init(&mtdblk->cache_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	mtdblk->cache_state = STATE_EMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (!(mbd->mtd->flags & MTD_NO_ERASE) && mbd->mtd->erasesize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		mtdblk->cache_size = mbd->mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		mtdblk->cache_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	pr_debug("ok\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static void mtdblock_release(struct mtd_blktrans_dev *mbd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	pr_debug("mtdblock_release\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	mutex_lock(&mtdblk->cache_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	write_cached_data(mtdblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	mutex_unlock(&mtdblk->cache_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (!--mtdblk->count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		 * It was the last usage. Free the cache, but only sync if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		 * opened for writing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		if (mbd->file_mode & FMODE_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			mtd_sync(mbd->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		vfree(mtdblk->cache_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	pr_debug("ok\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static int mtdblock_flush(struct mtd_blktrans_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	mutex_lock(&mtdblk->cache_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	ret = write_cached_data(mtdblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	mutex_unlock(&mtdblk->cache_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	mtd_sync(dev->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	struct mtdblk_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	dev->mbd.mtd = mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	dev->mbd.devnum = mtd->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	dev->mbd.size = mtd->size >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	dev->mbd.tr = tr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (!(mtd->flags & MTD_WRITEABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		dev->mbd.readonly = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (add_mtd_blktrans_dev(&dev->mbd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	del_mtd_blktrans_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static struct mtd_blktrans_ops mtdblock_tr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	.name		= "mtdblock",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	.major		= MTD_BLOCK_MAJOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	.part_bits	= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	.blksize 	= 512,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	.open		= mtdblock_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	.flush		= mtdblock_flush,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	.release	= mtdblock_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	.readsect	= mtdblock_readsect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	.writesect	= mtdblock_writesect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	.add_mtd	= mtdblock_add_mtd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	.remove_dev	= mtdblock_remove_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static int __init init_mtdblock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	return register_mtd_blktrans(&mtdblock_tr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static void __exit cleanup_mtdblock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	deregister_mtd_blktrans(&mtdblock_tr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) module_init(init_mtdblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) module_exit(cleanup_mtdblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) MODULE_AUTHOR("Nicolas Pitre <nico@fluxnic.net> et al.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");