mtdram.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * mtdram - a test mtd device
  3. * Author: Alexander Larsson <alex@cendio.se>
  4. *
  5. * Copyright (c) 1999 Alexander Larsson <alex@cendio.se>
  6. * Copyright (c) 2005 Joern Engel <joern@wh.fh-wedel.de>
  7. *
  8. * This code is GPL
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/ioport.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/init.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/mtdram.h>
  18. static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
  19. static unsigned long erase_size = CONFIG_MTDRAM_ERASE_SIZE;
  20. static unsigned long writebuf_size = 64;
  21. #define MTDRAM_TOTAL_SIZE (total_size * 1024)
  22. #define MTDRAM_ERASE_SIZE (erase_size * 1024)
  23. #ifdef MODULE
  24. module_param(total_size, ulong, 0);
  25. MODULE_PARM_DESC(total_size, "Total device size in KiB");
  26. module_param(erase_size, ulong, 0);
  27. MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
  28. module_param(writebuf_size, ulong, 0);
  29. MODULE_PARM_DESC(writebuf_size, "Device write buf size in Bytes (Default: 64)");
  30. #endif
  31. // We could store these in the mtd structure, but we only support 1 device..
  32. static struct mtd_info *mtd_info;
  33. static int check_offs_len(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  34. {
  35. int ret = 0;
  36. /* Start address must align on block boundary */
  37. if (mtd_mod_by_eb(ofs, mtd)) {
  38. pr_debug("%s: unaligned address\n", __func__);
  39. ret = -EINVAL;
  40. }
  41. /* Length must align on block boundary */
  42. if (mtd_mod_by_eb(len, mtd)) {
  43. pr_debug("%s: length not block aligned\n", __func__);
  44. ret = -EINVAL;
  45. }
  46. return ret;
  47. }
  48. static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
  49. {
  50. if (check_offs_len(mtd, instr->addr, instr->len))
  51. return -EINVAL;
  52. memset((char *)mtd->priv + instr->addr, 0xff, instr->len);
  53. instr->state = MTD_ERASE_DONE;
  54. mtd_erase_callback(instr);
  55. return 0;
  56. }
  57. static int ram_point(struct mtd_info *mtd, loff_t from, size_t len,
  58. size_t *retlen, void **virt, resource_size_t *phys)
  59. {
  60. *virt = mtd->priv + from;
  61. *retlen = len;
  62. return 0;
  63. }
  64. static int ram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  65. {
  66. return 0;
  67. }
  68. /*
  69. * Allow NOMMU mmap() to directly map the device (if not NULL)
  70. * - return the address to which the offset maps
  71. * - return -ENOSYS to indicate refusal to do the mapping
  72. */
  73. static unsigned long ram_get_unmapped_area(struct mtd_info *mtd,
  74. unsigned long len,
  75. unsigned long offset,
  76. unsigned long flags)
  77. {
  78. return (unsigned long) mtd->priv + offset;
  79. }
  80. static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
  81. size_t *retlen, u_char *buf)
  82. {
  83. memcpy(buf, mtd->priv + from, len);
  84. *retlen = len;
  85. return 0;
  86. }
  87. static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
  88. size_t *retlen, const u_char *buf)
  89. {
  90. memcpy((char *)mtd->priv + to, buf, len);
  91. *retlen = len;
  92. return 0;
  93. }
  94. static void __exit cleanup_mtdram(void)
  95. {
  96. if (mtd_info) {
  97. mtd_device_unregister(mtd_info);
  98. vfree(mtd_info->priv);
  99. kfree(mtd_info);
  100. }
  101. }
  102. int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
  103. unsigned long size, const char *name)
  104. {
  105. memset(mtd, 0, sizeof(*mtd));
  106. /* Setup the MTD structure */
  107. mtd->name = name;
  108. mtd->type = MTD_RAM;
  109. mtd->flags = MTD_CAP_RAM;
  110. mtd->size = size;
  111. mtd->writesize = 1;
  112. mtd->writebufsize = writebuf_size;
  113. mtd->erasesize = MTDRAM_ERASE_SIZE;
  114. mtd->priv = mapped_address;
  115. mtd->owner = THIS_MODULE;
  116. mtd->_erase = ram_erase;
  117. mtd->_point = ram_point;
  118. mtd->_unpoint = ram_unpoint;
  119. mtd->_get_unmapped_area = ram_get_unmapped_area;
  120. mtd->_read = ram_read;
  121. mtd->_write = ram_write;
  122. if (mtd_device_register(mtd, NULL, 0))
  123. return -EIO;
  124. return 0;
  125. }
  126. static int __init init_mtdram(void)
  127. {
  128. void *addr;
  129. int err;
  130. if (!total_size)
  131. return -EINVAL;
  132. /* Allocate some memory */
  133. mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
  134. if (!mtd_info)
  135. return -ENOMEM;
  136. addr = vmalloc(MTDRAM_TOTAL_SIZE);
  137. if (!addr) {
  138. kfree(mtd_info);
  139. mtd_info = NULL;
  140. return -ENOMEM;
  141. }
  142. err = mtdram_init_device(mtd_info, addr, MTDRAM_TOTAL_SIZE, "mtdram test device");
  143. if (err) {
  144. vfree(addr);
  145. kfree(mtd_info);
  146. mtd_info = NULL;
  147. return err;
  148. }
  149. memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
  150. return err;
  151. }
  152. module_init(init_mtdram);
  153. module_exit(cleanup_mtdram);
  154. MODULE_LICENSE("GPL");
  155. MODULE_AUTHOR("Alexander Larsson <alexl@redhat.com>");
  156. MODULE_DESCRIPTION("Simulated MTD driver for testing");