mtdram.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #define MTDRAM_TOTAL_SIZE (total_size * 1024)
  21. #define MTDRAM_ERASE_SIZE (erase_size * 1024)
  22. #ifdef MODULE
  23. module_param(total_size, ulong, 0);
  24. MODULE_PARM_DESC(total_size, "Total device size in KiB");
  25. module_param(erase_size, ulong, 0);
  26. MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
  27. #endif
  28. // We could store these in the mtd structure, but we only support 1 device..
  29. static struct mtd_info *mtd_info;
  30. static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
  31. {
  32. if (instr->addr + instr->len > mtd->size)
  33. return -EINVAL;
  34. memset((char *)mtd->priv + instr->addr, 0xff, instr->len);
  35. instr->state = MTD_ERASE_DONE;
  36. mtd_erase_callback(instr);
  37. return 0;
  38. }
  39. static int ram_point(struct mtd_info *mtd, loff_t from, size_t len,
  40. size_t *retlen, void **virt, resource_size_t *phys)
  41. {
  42. if (from + len > mtd->size)
  43. return -EINVAL;
  44. /* can we return a physical address with this driver? */
  45. if (phys)
  46. return -EINVAL;
  47. *virt = mtd->priv + from;
  48. *retlen = len;
  49. return 0;
  50. }
  51. static void ram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  52. {
  53. }
  54. /*
  55. * Allow NOMMU mmap() to directly map the device (if not NULL)
  56. * - return the address to which the offset maps
  57. * - return -ENOSYS to indicate refusal to do the mapping
  58. */
  59. static unsigned long ram_get_unmapped_area(struct mtd_info *mtd,
  60. unsigned long len,
  61. unsigned long offset,
  62. unsigned long flags)
  63. {
  64. return (unsigned long) mtd->priv + offset;
  65. }
  66. static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
  67. size_t *retlen, u_char *buf)
  68. {
  69. if (from + len > mtd->size)
  70. return -EINVAL;
  71. memcpy(buf, mtd->priv + from, len);
  72. *retlen = len;
  73. return 0;
  74. }
  75. static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
  76. size_t *retlen, const u_char *buf)
  77. {
  78. if (to + len > mtd->size)
  79. return -EINVAL;
  80. memcpy((char *)mtd->priv + to, buf, len);
  81. *retlen = len;
  82. return 0;
  83. }
  84. static void __exit cleanup_mtdram(void)
  85. {
  86. if (mtd_info) {
  87. del_mtd_device(mtd_info);
  88. vfree(mtd_info->priv);
  89. kfree(mtd_info);
  90. }
  91. }
  92. int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
  93. unsigned long size, char *name)
  94. {
  95. memset(mtd, 0, sizeof(*mtd));
  96. /* Setup the MTD structure */
  97. mtd->name = name;
  98. mtd->type = MTD_RAM;
  99. mtd->flags = MTD_CAP_RAM;
  100. mtd->size = size;
  101. mtd->writesize = 1;
  102. mtd->erasesize = MTDRAM_ERASE_SIZE;
  103. mtd->priv = mapped_address;
  104. mtd->owner = THIS_MODULE;
  105. mtd->erase = ram_erase;
  106. mtd->point = ram_point;
  107. mtd->unpoint = ram_unpoint;
  108. mtd->get_unmapped_area = ram_get_unmapped_area;
  109. mtd->read = ram_read;
  110. mtd->write = ram_write;
  111. if (add_mtd_device(mtd)) {
  112. return -EIO;
  113. }
  114. return 0;
  115. }
  116. static int __init init_mtdram(void)
  117. {
  118. void *addr;
  119. int err;
  120. if (!total_size)
  121. return -EINVAL;
  122. /* Allocate some memory */
  123. mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
  124. if (!mtd_info)
  125. return -ENOMEM;
  126. addr = vmalloc(MTDRAM_TOTAL_SIZE);
  127. if (!addr) {
  128. kfree(mtd_info);
  129. mtd_info = NULL;
  130. return -ENOMEM;
  131. }
  132. err = mtdram_init_device(mtd_info, addr, MTDRAM_TOTAL_SIZE, "mtdram test device");
  133. if (err) {
  134. vfree(addr);
  135. kfree(mtd_info);
  136. mtd_info = NULL;
  137. return err;
  138. }
  139. memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
  140. return err;
  141. }
  142. module_init(init_mtdram);
  143. module_exit(cleanup_mtdram);
  144. MODULE_LICENSE("GPL");
  145. MODULE_AUTHOR("Alexander Larsson <alexl@redhat.com>");
  146. MODULE_DESCRIPTION("Simulated MTD driver for testing");