[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / mtd / devices / blkmtd.c
blob662e807801ed046e71a99ad8b50f781bc14f1fee
1 /*
2 * $Id: blkmtd.c,v 1.24 2004/11/16 18:29:01 dwmw2 Exp $
4 * blkmtd.c - use a block device as a fake MTD
6 * Author: Simon Evans <spse@secret.org.uk>
8 * Copyright (C) 2001,2002 Simon Evans
10 * Licence: GPL
12 * How it works:
13 * The driver uses raw/io to read/write the device and the page
14 * cache to cache access. Writes update the page cache with the
15 * new data and mark it dirty and add the page into a BIO which
16 * is then written out.
18 * It can be loaded Read-Only to prevent erases and writes to the
19 * medium.
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/fs.h>
26 #include <linux/blkdev.h>
27 #include <linux/bio.h>
28 #include <linux/pagemap.h>
29 #include <linux/list.h>
30 #include <linux/init.h>
31 #include <linux/mtd/mtd.h>
34 #define err(format, arg...) printk(KERN_ERR "blkmtd: " format "\n" , ## arg)
35 #define info(format, arg...) printk(KERN_INFO "blkmtd: " format "\n" , ## arg)
36 #define warn(format, arg...) printk(KERN_WARNING "blkmtd: " format "\n" , ## arg)
37 #define crit(format, arg...) printk(KERN_CRIT "blkmtd: " format "\n" , ## arg)
40 /* Default erase size in K, always make it a multiple of PAGE_SIZE */
41 #define CONFIG_MTD_BLKDEV_ERASESIZE (128 << 10) /* 128KiB */
42 #define VERSION "$Revision: 1.24 $"
44 /* Info for the block device */
45 struct blkmtd_dev {
46 struct list_head list;
47 struct block_device *blkdev;
48 struct mtd_info mtd_info;
49 struct semaphore wrbuf_mutex;
53 /* Static info about the MTD, used in cleanup_module */
54 static LIST_HEAD(blkmtd_device_list);
57 static void blkmtd_sync(struct mtd_info *mtd);
59 #define MAX_DEVICES 4
61 /* Module parameters passed by insmod/modprobe */
62 static char *device[MAX_DEVICES]; /* the block device to use */
63 static int erasesz[MAX_DEVICES]; /* optional default erase size */
64 static int ro[MAX_DEVICES]; /* optional read only flag */
65 static int sync;
68 MODULE_LICENSE("GPL");
69 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
70 MODULE_DESCRIPTION("Emulate an MTD using a block device");
71 module_param_array(device, charp, NULL, 0);
72 MODULE_PARM_DESC(device, "block device to use");
73 module_param_array(erasesz, int, NULL, 0);
74 MODULE_PARM_DESC(erasesz, "optional erase size to use in KiB. eg 4=4KiB.");
75 module_param_array(ro, bool, NULL, 0);
76 MODULE_PARM_DESC(ro, "1=Read only, writes and erases cause errors");
77 module_param(sync, bool, 0);
78 MODULE_PARM_DESC(sync, "1=Synchronous writes");
81 /* completion handler for BIO reads */
82 static int bi_read_complete(struct bio *bio, unsigned int bytes_done, int error)
84 if (bio->bi_size)
85 return 1;
87 complete((struct completion*)bio->bi_private);
88 return 0;
92 /* completion handler for BIO writes */
93 static int bi_write_complete(struct bio *bio, unsigned int bytes_done, int error)
95 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
96 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
98 if (bio->bi_size)
99 return 1;
101 if(!uptodate)
102 err("bi_write_complete: not uptodate\n");
104 do {
105 struct page *page = bvec->bv_page;
106 DEBUG(3, "Cleaning up page %ld\n", page->index);
107 if (--bvec >= bio->bi_io_vec)
108 prefetchw(&bvec->bv_page->flags);
110 if (uptodate) {
111 SetPageUptodate(page);
112 } else {
113 ClearPageUptodate(page);
114 SetPageError(page);
116 ClearPageDirty(page);
117 unlock_page(page);
118 page_cache_release(page);
119 } while (bvec >= bio->bi_io_vec);
121 complete((struct completion*)bio->bi_private);
122 return 0;
126 /* read one page from the block device */
127 static int blkmtd_readpage(struct blkmtd_dev *dev, struct page *page)
129 struct bio *bio;
130 struct completion event;
131 int err = -ENOMEM;
133 if(PageUptodate(page)) {
134 DEBUG(2, "blkmtd: readpage page %ld is already upto date\n", page->index);
135 unlock_page(page);
136 return 0;
139 ClearPageUptodate(page);
140 ClearPageError(page);
142 bio = bio_alloc(GFP_KERNEL, 1);
143 if(bio) {
144 init_completion(&event);
145 bio->bi_bdev = dev->blkdev;
146 bio->bi_sector = page->index << (PAGE_SHIFT-9);
147 bio->bi_private = &event;
148 bio->bi_end_io = bi_read_complete;
149 if(bio_add_page(bio, page, PAGE_SIZE, 0) == PAGE_SIZE) {
150 submit_bio(READ_SYNC, bio);
151 wait_for_completion(&event);
152 err = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : -EIO;
153 bio_put(bio);
157 if(err)
158 SetPageError(page);
159 else
160 SetPageUptodate(page);
161 flush_dcache_page(page);
162 unlock_page(page);
163 return err;
167 /* write out the current BIO and wait for it to finish */
168 static int blkmtd_write_out(struct bio *bio)
170 struct completion event;
171 int err;
173 if(!bio->bi_vcnt) {
174 bio_put(bio);
175 return 0;
178 init_completion(&event);
179 bio->bi_private = &event;
180 bio->bi_end_io = bi_write_complete;
181 submit_bio(WRITE_SYNC, bio);
182 wait_for_completion(&event);
183 DEBUG(3, "submit_bio completed, bi_vcnt = %d\n", bio->bi_vcnt);
184 err = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : -EIO;
185 bio_put(bio);
186 return err;
191 * blkmtd_add_page - add a page to the current BIO
192 * @bio: bio to add to (NULL to alloc initial bio)
193 * @blkdev: block device
194 * @page: page to add
195 * @pagecnt: pages left to add
197 * Adds a page to the current bio, allocating it if necessary. If it cannot be
198 * added, the current bio is written out and a new one is allocated. Returns
199 * the new bio to add or NULL on error
201 static struct bio *blkmtd_add_page(struct bio *bio, struct block_device *blkdev,
202 struct page *page, int pagecnt)
205 retry:
206 if(!bio) {
207 bio = bio_alloc(GFP_KERNEL, pagecnt);
208 if(!bio)
209 return NULL;
210 bio->bi_sector = page->index << (PAGE_SHIFT-9);
211 bio->bi_bdev = blkdev;
214 if(bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE) {
215 blkmtd_write_out(bio);
216 bio = NULL;
217 goto retry;
219 return bio;
224 * write_pages - write block of data to device via the page cache
225 * @dev: device to write to
226 * @buf: data source or NULL if erase (output is set to 0xff)
227 * @to: offset into output device
228 * @len: amount to data to write
229 * @retlen: amount of data written
231 * Grab pages from the page cache and fill them with the source data.
232 * Non page aligned start and end result in a readin of the page and
233 * part of the page being modified. Pages are added to the bio and then written
234 * out.
236 static int write_pages(struct blkmtd_dev *dev, const u_char *buf, loff_t to,
237 size_t len, size_t *retlen)
239 int pagenr, offset;
240 size_t start_len = 0, end_len;
241 int pagecnt = 0;
242 int err = 0;
243 struct bio *bio = NULL;
244 size_t thislen = 0;
246 pagenr = to >> PAGE_SHIFT;
247 offset = to & ~PAGE_MASK;
249 DEBUG(2, "blkmtd: write_pages: buf = %p to = %ld len = %zd pagenr = %d offset = %d\n",
250 buf, (long)to, len, pagenr, offset);
252 /* see if we have to do a partial write at the start */
253 if(offset) {
254 start_len = ((offset + len) > PAGE_SIZE) ? PAGE_SIZE - offset : len;
255 len -= start_len;
258 /* calculate the length of the other two regions */
259 end_len = len & ~PAGE_MASK;
260 len -= end_len;
262 if(start_len)
263 pagecnt++;
265 if(len)
266 pagecnt += len >> PAGE_SHIFT;
268 if(end_len)
269 pagecnt++;
271 down(&dev->wrbuf_mutex);
273 DEBUG(3, "blkmtd: write: start_len = %zd len = %zd end_len = %zd pagecnt = %d\n",
274 start_len, len, end_len, pagecnt);
276 if(start_len) {
277 /* do partial start region */
278 struct page *page;
280 DEBUG(3, "blkmtd: write: doing partial start, page = %d len = %zd offset = %d\n",
281 pagenr, start_len, offset);
283 BUG_ON(!buf);
284 page = read_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr, (filler_t *)blkmtd_readpage, dev);
285 lock_page(page);
286 if(PageDirty(page)) {
287 err("to = %lld start_len = %zd len = %zd end_len = %zd pagenr = %d\n",
288 to, start_len, len, end_len, pagenr);
289 BUG();
291 memcpy(page_address(page)+offset, buf, start_len);
292 SetPageDirty(page);
293 SetPageUptodate(page);
294 buf += start_len;
295 thislen = start_len;
296 bio = blkmtd_add_page(bio, dev->blkdev, page, pagecnt);
297 if(!bio) {
298 err = -ENOMEM;
299 err("bio_add_page failed\n");
300 goto write_err;
302 pagecnt--;
303 pagenr++;
306 /* Now do the main loop to a page aligned, n page sized output */
307 if(len) {
308 int pagesc = len >> PAGE_SHIFT;
309 DEBUG(3, "blkmtd: write: whole pages start = %d, count = %d\n",
310 pagenr, pagesc);
311 while(pagesc) {
312 struct page *page;
314 /* see if page is in the page cache */
315 DEBUG(3, "blkmtd: write: grabbing page %d from page cache\n", pagenr);
316 page = grab_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr);
317 if(PageDirty(page)) {
318 BUG();
320 if(!page) {
321 warn("write: cannot grab cache page %d", pagenr);
322 err = -ENOMEM;
323 goto write_err;
325 if(!buf) {
326 memset(page_address(page), 0xff, PAGE_SIZE);
327 } else {
328 memcpy(page_address(page), buf, PAGE_SIZE);
329 buf += PAGE_SIZE;
331 bio = blkmtd_add_page(bio, dev->blkdev, page, pagecnt);
332 if(!bio) {
333 err = -ENOMEM;
334 err("bio_add_page failed\n");
335 goto write_err;
337 pagenr++;
338 pagecnt--;
339 SetPageDirty(page);
340 SetPageUptodate(page);
341 pagesc--;
342 thislen += PAGE_SIZE;
346 if(end_len) {
347 /* do the third region */
348 struct page *page;
349 DEBUG(3, "blkmtd: write: doing partial end, page = %d len = %zd\n",
350 pagenr, end_len);
351 BUG_ON(!buf);
352 page = read_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr, (filler_t *)blkmtd_readpage, dev);
353 lock_page(page);
354 if(PageDirty(page)) {
355 err("to = %lld start_len = %zd len = %zd end_len = %zd pagenr = %d\n",
356 to, start_len, len, end_len, pagenr);
357 BUG();
359 memcpy(page_address(page), buf, end_len);
360 SetPageDirty(page);
361 SetPageUptodate(page);
362 DEBUG(3, "blkmtd: write: writing out partial end\n");
363 thislen += end_len;
364 bio = blkmtd_add_page(bio, dev->blkdev, page, pagecnt);
365 if(!bio) {
366 err = -ENOMEM;
367 err("bio_add_page failed\n");
368 goto write_err;
370 pagenr++;
373 DEBUG(3, "blkmtd: write: got %d vectors to write\n", bio->bi_vcnt);
374 write_err:
375 if(bio)
376 blkmtd_write_out(bio);
378 DEBUG(2, "blkmtd: write: end, retlen = %zd, err = %d\n", *retlen, err);
379 up(&dev->wrbuf_mutex);
381 if(retlen)
382 *retlen = thislen;
383 return err;
387 /* erase a specified part of the device */
388 static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr)
390 struct blkmtd_dev *dev = mtd->priv;
391 struct mtd_erase_region_info *einfo = mtd->eraseregions;
392 int numregions = mtd->numeraseregions;
393 size_t from;
394 u_long len;
395 int err = -EIO;
396 size_t retlen;
398 instr->state = MTD_ERASING;
399 from = instr->addr;
400 len = instr->len;
402 /* check erase region has valid start and length */
403 DEBUG(2, "blkmtd: erase: dev = `%s' from = 0x%zx len = 0x%lx\n",
404 mtd->name+9, from, len);
405 while(numregions) {
406 DEBUG(3, "blkmtd: checking erase region = 0x%08X size = 0x%X num = 0x%x\n",
407 einfo->offset, einfo->erasesize, einfo->numblocks);
408 if(from >= einfo->offset
409 && from < einfo->offset + (einfo->erasesize * einfo->numblocks)) {
410 if(len == einfo->erasesize
411 && ( (from - einfo->offset) % einfo->erasesize == 0))
412 break;
414 numregions--;
415 einfo++;
418 if(!numregions) {
419 /* Not a valid erase block */
420 err("erase: invalid erase request 0x%lX @ 0x%08zX", len, from);
421 instr->state = MTD_ERASE_FAILED;
422 err = -EIO;
425 if(instr->state != MTD_ERASE_FAILED) {
426 /* do the erase */
427 DEBUG(3, "Doing erase from = %zd len = %ld\n", from, len);
428 err = write_pages(dev, NULL, from, len, &retlen);
429 if(err || retlen != len) {
430 err("erase failed err = %d", err);
431 instr->state = MTD_ERASE_FAILED;
432 } else {
433 instr->state = MTD_ERASE_DONE;
437 DEBUG(3, "blkmtd: erase: checking callback\n");
438 mtd_erase_callback(instr);
439 DEBUG(2, "blkmtd: erase: finished (err = %d)\n", err);
440 return err;
444 /* read a range of the data via the page cache */
445 static int blkmtd_read(struct mtd_info *mtd, loff_t from, size_t len,
446 size_t *retlen, u_char *buf)
448 struct blkmtd_dev *dev = mtd->priv;
449 int err = 0;
450 int offset;
451 int pagenr, pages;
452 size_t thislen = 0;
454 DEBUG(2, "blkmtd: read: dev = `%s' from = %lld len = %zd buf = %p\n",
455 mtd->name+9, from, len, buf);
457 if(from > mtd->size)
458 return -EINVAL;
459 if(from + len > mtd->size)
460 len = mtd->size - from;
462 pagenr = from >> PAGE_SHIFT;
463 offset = from - (pagenr << PAGE_SHIFT);
465 pages = (offset+len+PAGE_SIZE-1) >> PAGE_SHIFT;
466 DEBUG(3, "blkmtd: read: pagenr = %d offset = %d, pages = %d\n",
467 pagenr, offset, pages);
469 while(pages) {
470 struct page *page;
471 int cpylen;
473 DEBUG(3, "blkmtd: read: looking for page: %d\n", pagenr);
474 page = read_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr, (filler_t *)blkmtd_readpage, dev);
475 if(IS_ERR(page)) {
476 err = -EIO;
477 goto readerr;
480 cpylen = (PAGE_SIZE > len) ? len : PAGE_SIZE;
481 if(offset+cpylen > PAGE_SIZE)
482 cpylen = PAGE_SIZE-offset;
484 memcpy(buf + thislen, page_address(page) + offset, cpylen);
485 offset = 0;
486 len -= cpylen;
487 thislen += cpylen;
488 pagenr++;
489 pages--;
490 if(!PageDirty(page))
491 page_cache_release(page);
494 readerr:
495 if(retlen)
496 *retlen = thislen;
497 DEBUG(2, "blkmtd: end read: retlen = %zd, err = %d\n", thislen, err);
498 return err;
502 /* write data to the underlying device */
503 static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len,
504 size_t *retlen, const u_char *buf)
506 struct blkmtd_dev *dev = mtd->priv;
507 int err;
509 if(!len)
510 return 0;
512 DEBUG(2, "blkmtd: write: dev = `%s' to = %lld len = %zd buf = %p\n",
513 mtd->name+9, to, len, buf);
515 if(to >= mtd->size) {
516 return -ENOSPC;
519 if(to + len > mtd->size) {
520 len = mtd->size - to;
523 err = write_pages(dev, buf, to, len, retlen);
524 if(err > 0)
525 err = 0;
526 DEBUG(2, "blkmtd: write: end, err = %d\n", err);
527 return err;
531 /* sync the device - wait until the write queue is empty */
532 static void blkmtd_sync(struct mtd_info *mtd)
534 /* Currently all writes are synchronous */
538 static void free_device(struct blkmtd_dev *dev)
540 DEBUG(2, "blkmtd: free_device() dev = %p\n", dev);
541 if(dev) {
542 if(dev->mtd_info.eraseregions)
543 kfree(dev->mtd_info.eraseregions);
544 if(dev->mtd_info.name)
545 kfree(dev->mtd_info.name);
547 if(dev->blkdev) {
548 invalidate_inode_pages(dev->blkdev->bd_inode->i_mapping);
549 close_bdev_excl(dev->blkdev);
551 kfree(dev);
556 /* For a given size and initial erase size, calculate the number
557 * and size of each erase region. Goes round the loop twice,
558 * once to find out how many regions, then allocates space,
559 * then round the loop again to fill it in.
561 static struct mtd_erase_region_info *calc_erase_regions(
562 size_t erase_size, size_t total_size, int *regions)
564 struct mtd_erase_region_info *info = NULL;
566 DEBUG(2, "calc_erase_regions, es = %zd size = %zd regions = %d\n",
567 erase_size, total_size, *regions);
568 /* Make any user specified erasesize be a power of 2
569 and at least PAGE_SIZE */
570 if(erase_size) {
571 int es = erase_size;
572 erase_size = 1;
573 while(es != 1) {
574 es >>= 1;
575 erase_size <<= 1;
577 if(erase_size < PAGE_SIZE)
578 erase_size = PAGE_SIZE;
579 } else {
580 erase_size = CONFIG_MTD_BLKDEV_ERASESIZE;
583 *regions = 0;
585 do {
586 int tot_size = total_size;
587 int er_size = erase_size;
588 int count = 0, offset = 0, regcnt = 0;
590 while(tot_size) {
591 count = tot_size / er_size;
592 if(count) {
593 tot_size = tot_size % er_size;
594 if(info) {
595 DEBUG(2, "adding to erase info off=%d er=%d cnt=%d\n",
596 offset, er_size, count);
597 (info+regcnt)->offset = offset;
598 (info+regcnt)->erasesize = er_size;
599 (info+regcnt)->numblocks = count;
600 (*regions)++;
602 regcnt++;
603 offset += (count * er_size);
605 while(er_size > tot_size)
606 er_size >>= 1;
608 if(info == NULL) {
609 info = kmalloc(regcnt * sizeof(struct mtd_erase_region_info), GFP_KERNEL);
610 if(!info)
611 break;
613 } while(!(*regions));
614 DEBUG(2, "calc_erase_regions done, es = %zd size = %zd regions = %d\n",
615 erase_size, total_size, *regions);
616 return info;
620 extern dev_t __init name_to_dev_t(const char *line);
622 static struct blkmtd_dev *add_device(char *devname, int readonly, int erase_size)
624 struct block_device *bdev;
625 int mode;
626 struct blkmtd_dev *dev;
628 if(!devname)
629 return NULL;
631 /* Get a handle on the device */
634 #ifdef MODULE
635 mode = (readonly) ? O_RDONLY : O_RDWR;
636 bdev = open_bdev_excl(devname, mode, NULL);
637 #else
638 mode = (readonly) ? FMODE_READ : FMODE_WRITE;
639 bdev = open_by_devnum(name_to_dev_t(devname), mode);
640 #endif
641 if(IS_ERR(bdev)) {
642 err("error: cannot open device %s", devname);
643 DEBUG(2, "blkmtd: opening bdev returned %ld\n", PTR_ERR(bdev));
644 return NULL;
647 DEBUG(1, "blkmtd: found a block device major = %d, minor = %d\n",
648 MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
650 if(MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
651 err("attempting to use an MTD device as a block device");
652 blkdev_put(bdev);
653 return NULL;
656 dev = kmalloc(sizeof(struct blkmtd_dev), GFP_KERNEL);
657 if(dev == NULL) {
658 blkdev_put(bdev);
659 return NULL;
662 memset(dev, 0, sizeof(struct blkmtd_dev));
663 dev->blkdev = bdev;
664 if(!readonly) {
665 init_MUTEX(&dev->wrbuf_mutex);
668 dev->mtd_info.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
670 /* Setup the MTD structure */
671 /* make the name contain the block device in */
672 dev->mtd_info.name = kmalloc(sizeof("blkmtd: ") + strlen(devname), GFP_KERNEL);
673 if(dev->mtd_info.name == NULL)
674 goto devinit_err;
676 sprintf(dev->mtd_info.name, "blkmtd: %s", devname);
677 dev->mtd_info.eraseregions = calc_erase_regions(erase_size, dev->mtd_info.size,
678 &dev->mtd_info.numeraseregions);
679 if(dev->mtd_info.eraseregions == NULL)
680 goto devinit_err;
682 dev->mtd_info.erasesize = dev->mtd_info.eraseregions->erasesize;
683 DEBUG(1, "blkmtd: init: found %d erase regions\n",
684 dev->mtd_info.numeraseregions);
686 if(readonly) {
687 dev->mtd_info.type = MTD_ROM;
688 dev->mtd_info.flags = MTD_CAP_ROM;
689 } else {
690 dev->mtd_info.type = MTD_RAM;
691 dev->mtd_info.flags = MTD_CAP_RAM;
692 dev->mtd_info.erase = blkmtd_erase;
693 dev->mtd_info.write = blkmtd_write;
694 dev->mtd_info.writev = default_mtd_writev;
695 dev->mtd_info.sync = blkmtd_sync;
697 dev->mtd_info.read = blkmtd_read;
698 dev->mtd_info.readv = default_mtd_readv;
699 dev->mtd_info.priv = dev;
700 dev->mtd_info.owner = THIS_MODULE;
702 list_add(&dev->list, &blkmtd_device_list);
703 if (add_mtd_device(&dev->mtd_info)) {
704 /* Device didnt get added, so free the entry */
705 list_del(&dev->list);
706 goto devinit_err;
707 } else {
708 info("mtd%d: [%s] erase_size = %dKiB %s",
709 dev->mtd_info.index, dev->mtd_info.name + strlen("blkmtd: "),
710 dev->mtd_info.erasesize >> 10,
711 readonly ? "(read-only)" : "");
714 return dev;
716 devinit_err:
717 free_device(dev);
718 return NULL;
722 /* Cleanup and exit - sync the device and kill of the kernel thread */
723 static void __devexit cleanup_blkmtd(void)
725 struct list_head *temp1, *temp2;
727 /* Remove the MTD devices */
728 list_for_each_safe(temp1, temp2, &blkmtd_device_list) {
729 struct blkmtd_dev *dev = list_entry(temp1, struct blkmtd_dev,
730 list);
731 blkmtd_sync(&dev->mtd_info);
732 del_mtd_device(&dev->mtd_info);
733 info("mtd%d: [%s] removed", dev->mtd_info.index,
734 dev->mtd_info.name + strlen("blkmtd: "));
735 list_del(&dev->list);
736 free_device(dev);
740 #ifndef MODULE
742 /* Handle kernel boot params */
745 static int __init param_blkmtd_device(char *str)
747 int i;
749 for(i = 0; i < MAX_DEVICES; i++) {
750 device[i] = str;
751 DEBUG(2, "blkmtd: device setup: %d = %s\n", i, device[i]);
752 strsep(&str, ",");
754 return 1;
758 static int __init param_blkmtd_erasesz(char *str)
760 int i;
761 for(i = 0; i < MAX_DEVICES; i++) {
762 char *val = strsep(&str, ",");
763 if(val)
764 erasesz[i] = simple_strtoul(val, NULL, 0);
765 DEBUG(2, "blkmtd: erasesz setup: %d = %d\n", i, erasesz[i]);
768 return 1;
772 static int __init param_blkmtd_ro(char *str)
774 int i;
775 for(i = 0; i < MAX_DEVICES; i++) {
776 char *val = strsep(&str, ",");
777 if(val)
778 ro[i] = simple_strtoul(val, NULL, 0);
779 DEBUG(2, "blkmtd: ro setup: %d = %d\n", i, ro[i]);
782 return 1;
786 static int __init param_blkmtd_sync(char *str)
788 if(str[0] == '1')
789 sync = 1;
790 return 1;
793 __setup("blkmtd_device=", param_blkmtd_device);
794 __setup("blkmtd_erasesz=", param_blkmtd_erasesz);
795 __setup("blkmtd_ro=", param_blkmtd_ro);
796 __setup("blkmtd_sync=", param_blkmtd_sync);
798 #endif
801 /* Startup */
802 static int __init init_blkmtd(void)
804 int i;
806 info("version " VERSION);
807 /* Check args - device[0] is the bare minimum*/
808 if(!device[0]) {
809 err("error: missing `device' name\n");
810 return -EINVAL;
813 for(i = 0; i < MAX_DEVICES; i++)
814 add_device(device[i], ro[i], erasesz[i] << 10);
816 if(list_empty(&blkmtd_device_list))
817 return -EINVAL;
819 return 0;
822 module_init(init_blkmtd);
823 module_exit(cleanup_blkmtd);