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