^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * block2mtd.c - create an mtd from a block device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2004-2006 Joern Engel <joern@wh.fh-wedel.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Licence: GPL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * When the first attempt at device initialization fails, we may need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * wait a little bit and retry. This timeout, by default 3 seconds, gives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * device time to start up. Required on BCM2708 and a few other chipsets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define MTD_DEFAULT_TIMEOUT 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* Info for the block device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct block2mtd_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct block_device *blkdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct mtd_info mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct mutex write_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* Static info about the MTD, used in cleanup_module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static LIST_HEAD(blkmtd_device_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static struct page *page_read(struct address_space *mapping, pgoff_t index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return read_mapping_page(mapping, index, NULL);
^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) /* erase a specified part of the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) pgoff_t index = to >> PAGE_SHIFT; // page index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int pages = len >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) u_long *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) u_long *max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) while (pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) page = page_read(mapping, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (IS_ERR(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) max = page_address(page) + PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) for (p=page_address(page); p<max; p++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (*p != -1UL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) memset(page_address(page), 0xff, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) set_page_dirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) balance_dirty_pages_ratelimited(mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) break;
^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) put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) pages--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct block2mtd_dev *dev = mtd->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) size_t from = instr->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) size_t len = instr->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) mutex_lock(&dev->write_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) err = _block2mtd_erase(dev, from, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) mutex_unlock(&dev->write_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) pr_err("erase failed err = %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) size_t *retlen, u_char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct block2mtd_dev *dev = mtd->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) pgoff_t index = from >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) int offset = from & (PAGE_SIZE-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int cpylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) while (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if ((offset + len) > PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) cpylen = PAGE_SIZE - offset; // multiple pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) cpylen = len; // this page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) len = len - cpylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) page = page_read(dev->blkdev->bd_inode->i_mapping, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (IS_ERR(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) memcpy(buf, page_address(page) + offset, cpylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (retlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *retlen += cpylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) buf += cpylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /* write data to the underlying device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) loff_t to, size_t len, size_t *retlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pgoff_t index = to >> PAGE_SHIFT; // page index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int offset = to & ~PAGE_MASK; // page offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int cpylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) while (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if ((offset+len) > PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) cpylen = PAGE_SIZE - offset; // multiple pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) cpylen = len; // this page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) len = len - cpylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) page = page_read(mapping, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (IS_ERR(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (memcmp(page_address(page)+offset, buf, cpylen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) memcpy(page_address(page) + offset, buf, cpylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) set_page_dirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) balance_dirty_pages_ratelimited(mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (retlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) *retlen += cpylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) buf += cpylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) size_t *retlen, const u_char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct block2mtd_dev *dev = mtd->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) mutex_lock(&dev->write_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) err = _block2mtd_write(dev, buf, to, len, retlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) mutex_unlock(&dev->write_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /* sync the device - wait until the write queue is empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static void block2mtd_sync(struct mtd_info *mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct block2mtd_dev *dev = mtd->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) sync_blockdev(dev->blkdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static void block2mtd_free_device(struct block2mtd_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) kfree(dev->mtd.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (dev->blkdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 0, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static struct block2mtd_dev *add_device(char *devname, int erase_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) #ifndef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct block2mtd_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (!devname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /* Get a handle on the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) bdev = blkdev_get_by_path(devname, mode, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) #ifndef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * We might not have the root device mounted at this point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * Try to resolve the device name by other means.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) for (i = 0; IS_ERR(bdev) && i <= timeout; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) dev_t devt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * Calling wait_for_device_probe in the first loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * was not enough, sleep for a bit in subsequent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * go-arounds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) msleep(1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) wait_for_device_probe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) devt = name_to_dev_t(devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (!devt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) bdev = blkdev_get_by_dev(devt, mode, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (IS_ERR(bdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) pr_err("error: cannot open device %s\n", devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) goto err_free_block2mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) dev->blkdev = bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) pr_err("attempting to use an MTD device as a block device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) goto err_free_block2mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if ((long)dev->blkdev->bd_inode->i_size % erase_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) pr_err("erasesize must be a divisor of device size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) goto err_free_block2mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) mutex_init(&dev->write_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /* Setup the MTD structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /* make the name contain the block device in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) goto err_destroy_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) dev->mtd.name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) dev->mtd.erasesize = erase_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) dev->mtd.writesize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) dev->mtd.writebufsize = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) dev->mtd.type = MTD_RAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) dev->mtd.flags = MTD_CAP_RAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) dev->mtd._erase = block2mtd_erase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) dev->mtd._write = block2mtd_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) dev->mtd._sync = block2mtd_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) dev->mtd._read = block2mtd_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) dev->mtd.priv = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) dev->mtd.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (mtd_device_register(&dev->mtd, NULL, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* Device didn't get added, so free the entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) goto err_destroy_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) list_add(&dev->list, &blkmtd_device_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) dev->mtd.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) dev->mtd.name + strlen("block2mtd: "),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) dev->mtd.erasesize >> 10, dev->mtd.erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) err_destroy_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) mutex_destroy(&dev->write_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) err_free_block2mtd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) block2mtd_free_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /* This function works similar to reguler strtoul. In addition, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * allows some suffixes for a more human-readable number format:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * ki, Ki, kiB, KiB - multiply result with 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * Mi, MiB - multiply result with 1024^2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * Gi, GiB - multiply result with 1024^3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int ustrtoul(const char *cp, char **endp, unsigned int base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) unsigned long result = simple_strtoul(cp, endp, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) switch (**endp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) case 'G' :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) result *= 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) case 'M':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) result *= 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) case 'K':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) case 'k':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) result *= 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if ((*endp)[1] == 'i') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if ((*endp)[2] == 'B')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) (*endp) += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) (*endp) += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static int parse_num(size_t *num, const char *token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) char *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) size_t n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) n = (size_t) ustrtoul(token, &endp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (*endp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) *num = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static inline void kill_final_newline(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) char *newline = strrchr(str, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (newline && !newline[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) *newline = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) #ifndef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) static int block2mtd_init_called = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /* 80 for device, 12 for erase size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static char block2mtd_paramline[80 + 12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static int block2mtd_setup2(const char *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) /* 80 for device, 12 for erase size, 80 for name, 8 for timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) char buf[80 + 12 + 80 + 8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) char *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) char *token[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) size_t erase_size = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) unsigned long timeout = MTD_DEFAULT_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (strnlen(val, sizeof(buf)) >= sizeof(buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) pr_err("parameter too long\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) strcpy(str, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) kill_final_newline(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) for (i = 0; i < 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) token[i] = strsep(&str, ",");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) pr_err("too many arguments\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (!token[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) pr_err("no argument\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) name = token[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (strlen(name) + 1 > 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) pr_err("device name too long\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (token[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) ret = parse_num(&erase_size, token[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) pr_err("illegal erase size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) add_device(name, erase_size, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) static int block2mtd_setup(const char *val, const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) #ifdef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return block2mtd_setup2(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) /* If more parameters are later passed in via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) /sys/module/block2mtd/parameters/block2mtd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) and block2mtd_init() has already been called,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) we can parse the argument now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (block2mtd_init_called)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return block2mtd_setup2(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) /* During early boot stage, we only save the parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) here. We must parse them later: if the param passed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) from kernel boot command line, block2mtd_setup() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) called so early that it is not possible to resolve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) the device (even kmalloc() fails). Deter that work to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) block2mtd_setup2(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) static int __init block2mtd_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) #ifndef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (strlen(block2mtd_paramline))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) ret = block2mtd_setup2(block2mtd_paramline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) block2mtd_init_called = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) static void block2mtd_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct list_head *pos, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /* Remove the MTD devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) list_for_each_safe(pos, next, &blkmtd_device_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) block2mtd_sync(&dev->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) mtd_device_unregister(&dev->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) mutex_destroy(&dev->write_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) pr_info("mtd%d: [%s] removed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) dev->mtd.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) dev->mtd.name + strlen("block2mtd: "));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) list_del(&dev->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) block2mtd_free_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) late_initcall(block2mtd_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) module_exit(block2mtd_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) MODULE_DESCRIPTION("Emulate an MTD using a block device");