Create mtdram device at runtime.
Subsystems (2)
The following patch moves most of the stuff out from the init_mtdram() function into a non static mtdram_init_device() function that can be called from other places in the kernel,
e.g. from a mapping routine to map in a cramfs image in RAM using the mtd layer:-)
To prevent the device from being created by init_mtdram using the configured values, CONFIG_MTDRAM_TOTAL_SIZE should be set to 0 (The help should be updated as well)
A new file: include/linux/mtd/mtdram.h is created as well.
Still some things missing regarding cleanup on exit for modules.
This is a first shot at it and it seems to work for us, but the API should perhaps be tweaked a little bit more, i.e. providing the flags and erase size as well.
Does it make enough sense to go into the official version (after some tweaking)?
/Johan
drivers/mtd/devices/mtdram.c 30 Apr 2002 09:28:23 -0000 renamed from drivers/mtd/devices/mtdram.c 23 Apr 2002 13:18:59 -0000 1.1.1.3 +1 -1
@@ -118,7 +118,7 @@ static void __exit cleanup_mtdram(void) } } -int __init init_mtdram(void) +int mtdram_init_device(void *mapped_address, unsigned long size, char
*name)
{
// Allocate some memory
mtd_info = (struct mtd_info *)kmalloc(sizeof(struct mtd_info),GFP_KERNEL);
@@ -128,26 +128,14 @@ int __init init_mtdram(void)
memset(mtd_info, 0, sizeof(*mtd_info));
// Setup the MTD structure
- mtd_info->name = "mtdram test device";
+ mtd_info->name = name;
mtd_info->type = MTD_RAM;
mtd_info->flags = MTD_CAP_RAM;
- mtd_info->size = MTDRAM_TOTAL_SIZE;
+ mtd_info->size = size;
mtd_info->erasesize = MTDRAM_ERASE_SIZE;-#if CONFIG_MTDRAM_ABS_POS > 0
- mtd_info->priv = ioremap(CONFIG_MTDRAM_ABS_POS, MTDRAM_TOTAL_SIZE);
-#else
- mtd_info->priv = vmalloc(MTDRAM_TOTAL_SIZE);
-#endif
-
- if (!mtd_info->priv) {
- DEBUG(MTD_DEBUG_LEVEL1, "Failed to vmalloc(/ioremap) memory region of
size %ld (ABS_POS:%ld)\n", (long)MTDRAM_TOTAL_SIZE, (long)CONFIG_MTDRAM_ABS_POS);
- kfree(mtd_info); - mtd_info = NULL; - return -ENOMEM; - } - memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE); + mtd_info->priv = mapped_address;
- mtd_info->module = THIS_MODULE;
+ mtd_info->module = THIS_MODULE;
mtd_info->erase = ram_erase;
mtd_info->point = ram_point;
mtd_info->unpoint = ram_unpoint;
@@ -155,11 +143,6 @@ int __init init_mtdram(void)
mtd_info->write = ram_write;
if (add_mtd_device(mtd_info)) {-#if CONFIG_MTDRAM_ABS_POS > 0
- iounmap(mtd_info->priv);
-#else
- vfree(mtd_info->priv);
-#endif
kfree(mtd_info);
mtd_info = NULL;
return -EIO;
@@ -168,10 +151,42 @@ int __init init_mtdram(void)
return 0;}
+int __init init_mtdram(void)
+{
+ void *addr;
+ int err = 0;
+#if CONFIG_MTDRAM_TOTAL_SIZE > 0
+#if CONFIG_MTDRAM_ABS_POS > 0
+ addr = ioremap(CONFIG_MTDRAM_ABS_POS, MTDRAM_TOTAL_SIZE);
+#else
+ addr = vmalloc(MTDRAM_TOTAL_SIZE);
+#endif
+ if (!addr) {
+ DEBUG(MTD_DEBUG_LEVEL1, "Failed to vmalloc(/ioremap) memory region of
size %ld (ABS_POS:%ld)\n", (long)MTDRAM_TOTAL_SIZE, (long)CONFIG_MTDRAM_ABS_POS);
+ kfree(mtd_info);
+ mtd_info = NULL;
+ return -ENOMEM;
+ }
+ err = mtdram_init_device(addr, MTDRAM_TOTAL_SIZE, "mtdram test device";
+ if (err)
+ {+#if CONFIG_MTDRAM_ABS_POS > 0
+ iounmap(mtd_info->priv);
+#else
+ vfree(mtd_info->priv);
+#endif
+ }
+ memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
+#endif /* CONFIG_MTDRAM_TOTAL_SIZE > 0 */
+ return err;
+}
+
+
module_init(init_mtdram);
module_exit(cleanup_mtdram);
MODULE_LICENSE("GPL"); MODULE_AUTHOR("Alexander Larsson <[email protected]>"); MODULE_DESCRIPTION("Simulated MTD driver for testing");
+
include/linux/mtd/mtdram.h Mon Apr 29 18:12:14 2002 renamed from dev/null Sat Mar 24 05:37:44 2001 +5 -0
@@ -0,0 +1,8 @@ +#ifndef __MTD_MTDRAM_H__ +#define __MTD_MTDRAM_H__ + +#ifdef __KERNEL__ +int mtdram_init_device(void *mapped_address, unsigned long size, char
*name);
+
+#endif /* __KERNEL__ */
+#endif /* __MTD_MTDRAM_H__ */
Quoted 2 lines · [email protected] said:
Does it make enough sense to go into the official version (after
some tweaking)?
Looks OK. Can you split the new init_mtdram() into two entirely separate functions; one for the CONFIG_MTDRAM_ABS_POS case, one for the vmalloc() case?
--
dwmw2
Quoted 3 lines
Looks OK. Can you split the new
init_mtdram()into two entirely separate functions; one for theCONFIG_MTDRAM_ABS_POScase, one for thevmalloc()case?
Here it comes, not tested though...
Beleive I took care of the cleanup as well.
/Johan
drivers/mtd/devices/mtdram.c 30 Apr 2002 10:24:15 -0000 renamed from drivers/mtd/devices/mtdram.c 23 Apr 2002 13:18:59 -0000 1.1.1.3 +3 -1
@@ -108,17 +108,19 @@ static void __exit cleanup_mtdram(void) { if (mtd_info) { del_mtd_device(mtd_info); +#if CONFIG_MTDRAM_TOTAL_SIZE > 0 if (mtd_info->priv) #if CONFIG_MTDRAM_ABS_POS > 0 iounmap(mtd_info->priv); #else vfree(mtd_info->priv); #endif +#endif kfree(mtd_info); } } -int __init init_mtdram(void) +int mtdram_init_device(void *mapped_address, unsigned long size, char
*name)
{
// Allocate some memory
mtd_info = (struct mtd_info *)kmalloc(sizeof(struct mtd_info),GFP_KERNEL);
@@ -128,26 +130,14 @@ int __init init_mtdram(void)
memset(mtd_info, 0, sizeof(*mtd_info));
// Setup the MTD structure
- mtd_info->name = "mtdram test device";
+ mtd_info->name = name;
mtd_info->type = MTD_RAM;
mtd_info->flags = MTD_CAP_RAM;
- mtd_info->size = MTDRAM_TOTAL_SIZE;
+ mtd_info->size = size;
mtd_info->erasesize = MTDRAM_ERASE_SIZE;-#if CONFIG_MTDRAM_ABS_POS > 0
- mtd_info->priv = ioremap(CONFIG_MTDRAM_ABS_POS, MTDRAM_TOTAL_SIZE);
-#else
- mtd_info->priv = vmalloc(MTDRAM_TOTAL_SIZE);
-#endif
-
- if (!mtd_info->priv) {
- DEBUG(MTD_DEBUG_LEVEL1, "Failed to vmalloc(/ioremap) memory region of
size %ld (ABS_POS:%ld)\n", (long)MTDRAM_TOTAL_SIZE, (long)CONFIG_MTDRAM_ABS_POS);
- kfree(mtd_info); - mtd_info = NULL; - return -ENOMEM; - } - memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE); + mtd_info->priv = mapped_address;
- mtd_info->module = THIS_MODULE;
+ mtd_info->module = THIS_MODULE;
mtd_info->erase = ram_erase;
mtd_info->point = ram_point;
mtd_info->unpoint = ram_unpoint;
@@ -155,11 +145,6 @@ int __init init_mtdram(void)
mtd_info->write = ram_write;
if (add_mtd_device(mtd_info)) {-#if CONFIG_MTDRAM_ABS_POS > 0
- iounmap(mtd_info->priv);
-#else
- vfree(mtd_info->priv);
-#endif
kfree(mtd_info);
mtd_info = NULL;
return -EIO;@@ -167,6 +152,64 @@ int __init init_mtdram(void)
return 0;
}
+
+#if CONFIG_MTDRAM_TOTAL_SIZE > 0
+#if CONFIG_MTDRAM_ABS_POS > 0
+int __init init_mtdram(void)
+{
+ void *addr;
+ int err = 0;
+ addr = ioremap(CONFIG_MTDRAM_ABS_POS, MTDRAM_TOTAL_SIZE);
+ if (!addr) {
+ DEBUG(MTD_DEBUG_LEVEL1,
+ "Failed to ioremap) memory region of size %ld at ABS_POS:%ld\n",
+ (long)MTDRAM_TOTAL_SIZE, (long)CONFIG_MTDRAM_ABS_POS);
+ kfree(mtd_info);
+ mtd_info = NULL;
+ return -ENOMEM;
+ }
+ err = mtdram_init_device(addr, MTDRAM_TOTAL_SIZE, "mtdram test device");
+ if (err)
+ {
+ iounmap(mtd_info->priv);
+ }
+ memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
+ return err;+}
+
+#else /* CONFIG_MTDRAM_ABS_POS > 0 */
+
+int __init init_mtdram(void)
+{
+ void *addr;
+ int err = 0;
+ addr = vmalloc(MTDRAM_TOTAL_SIZE);
+ if (!addr) {
+ DEBUG(MTD_DEBUG_LEVEL1,
+ "Failed to vmalloc memory region of size %ld\n",
+ (long)MTDRAM_TOTAL_SIZE);
+ kfree(mtd_info);
+ mtd_info = NULL;
+ return -ENOMEM;
+ }
+ err = mtdram_init_device(addr, MTDRAM_TOTAL_SIZE, "mtdram test device");
+ if (err)
+ {+ vfree(mtd_info->priv);
+ }
+ memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE); +
+ return err;
+}
+#endif /* !(CONFIG_MTDRAM_ABS_POS > 0) */ +
+#else /* CONFIG_MTDRAM_TOTAL_SIZE > 0 */
+
+int __init init_mtdram(void)
+{
+ return 0;
+}
+#endif /* !(CONFIG_MTDRAM_TOTAL_SIZE > 0) */
module_init(init_mtdram);
module_exit(cleanup_mtdram);
Quoted 2 lines · [email protected] said:
Here it comes, not tested though... Beleive I took care of the cleanup as well.
What about the case where mtdram_init_device() is called from elsewhere? Perhaps we should register a cleanup function with as mtdram_init_device() and call _that_ in the later cleanup?
--
dwmw2
Quoted 9 lines
Quoted 2 lines · [email protected] said:
Here it comes, not tested though... Beleive I took care of the cleanup as well.
What about the case where
mtdram_init_device()is called from elsewhere? Perhaps we should register a cleanup function with asmtdram_init_device()and call _that_ in the later cleanup?
--
dwmw2
How about letting mtdram_init_device return an allocated mtd_info* and the caller is responsible for cleaning it up?
The hardcoded init functions store the returned pointer in the static mtd_info variable and do it's clean-up on exit.
/Johan
Quoted 4 lines · [email protected] said:
How about letting
mtdram_init_devicereturn an allocated mtd_info*
and the caller is responsible for cleaning it up? The hardcoded init functions store the returned pointer in the staticmtd_infovariable and do it's clean-up on exit.
Seems reasonable.
--
dwmw2
This time I think I got it right...
The caller of mtdram_init_device() is responsible for allocating the mtd_info struct and to provide the memoryarea.
The caller is also repsonsible for freeing and deleting the stuff if needed.cleanup_mtdram() handles that for the non dynamic case, but otherwise it's up to the caller, no helper function is provided (nor needed really).
/Johan
---
drivers/mtd/devices/mtdram.c 6 May 2002 08:31:52 -0000 renamed from drivers/mtd/devices/mtdram.c 23 Apr 2002 13:18:59 -0000 1.1.1.3 +58 -17
@@ -108,65 +108,119 @@ static void __exit cleanup_mtdram(void) { if (mtd_info) { del_mtd_device(mtd_info); +#if CONFIG_MTDRAM_TOTAL_SIZE > 0 if (mtd_info->priv) #if CONFIG_MTDRAM_ABS_POS > 0 iounmap(mtd_info->priv); #else vfree(mtd_info->priv); #endif +#endif kfree(mtd_info); } } +int mtdram_init_device(struct mtd_info *mtd, void *mapped_address, + unsigned long size, char *name) +{ + memset(mtd, 0, sizeof(*mtd)); + + /* Setup the MTD structure */ + mtd->name = name; + mtd->type = MTD_RAM; + mtd->flags = MTD_CAP_RAM; + mtd->size = size; + mtd->erasesize = MTDRAM_ERASE_SIZE; + mtd->priv = mapped_address; + + mtd->module = THIS_MODULE; + mtd->erase = ram_erase; + mtd->point = ram_point; + mtd->unpoint = ram_unpoint; + mtd->read = ram_read; + mtd->write = ram_write; + + if (add_mtd_device(mtd)) { + return -EIO; + } + + return 0; +} + +#if CONFIG_MTDRAM_TOTAL_SIZE > 0 +#if CONFIG_MTDRAM_ABS_POS > 0 int __init init_mtdram(void) { - // Allocate some memory - mtd_info = (struct mtd_info *)kmalloc(sizeof(struct mtd_info), GFP_KERNEL); + void *addr; + int err; + /* Allocate some memory */ + mtd_info = (struct mtd_info *)kmalloc(sizeof(struct mtd_info), GFP_KERNEL); if (!mtd_info) - return -ENOMEM; + return -ENOMEM; - memset(mtd_info, 0, sizeof(*mtd_info)); + addr = ioremap(CONFIG_MTDRAM_ABS_POS, MTDRAM_TOTAL_SIZE); + if (!addr) { + DEBUG(MTD_DEBUG_LEVEL1, + "Failed to ioremap) memory region of size %ld at ABS_POS:%ld\n", + (long)MTDRAM_TOTAL_SIZE, (long)CONFIG_MTDRAM_ABS_POS); + kfree(mtd_info); + mtd_info = NULL; + return -ENOMEM; + } + err = mtdram_init_device(mtd_info, addr, + MTDRAM_TOTAL_SIZE, "mtdram test device"); + if (err) + { + iounmap(addr); + kfree(mtd_info); + mtd_info = NULL; + return err; + } + memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE); + return err; +} - // Setup the MTD structure - mtd_info->name = "mtdram test device"; - mtd_info->type = MTD_RAM; - mtd_info->flags = MTD_CAP_RAM; - mtd_info->size = MTDRAM_TOTAL_SIZE; - mtd_info->erasesize = MTDRAM_ERASE_SIZE; -#if CONFIG_MTDRAM_ABS_POS > 0 - mtd_info->priv = ioremap(CONFIG_MTDRAM_ABS_POS, MTDRAM_TOTAL_SIZE); -#else - mtd_info->priv = vmalloc(MTDRAM_TOTAL_SIZE); -#endif +#else /* CONFIG_MTDRAM_ABS_POS > 0 */ - if (!mtd_info->priv) { - DEBUG(MTD_DEBUG_LEVEL1, "Failed to vmalloc(/ioremap) memory region of size %ld (ABS_POS:%ld)\n",
(long)MTDRAM_TOTAL_SIZE, (long)CONFIG_MTDRAM_ABS_POS);
- kfree(mtd_info);
- mtd_info = NULL;
+int __init init_mtdram(void)
+{
+ void *addr;
+ int err;
+ /* Allocate some memory */
+ mtd_info = (struct mtd_info *)kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
+ if (!mtd_info)
return -ENOMEM;- }
- memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
- mtd_info->module = THIS_MODULE; - mtd_info->erase = ram_erase; - mtd_info->point = ram_point; - mtd_info->unpoint = ram_unpoint; - mtd_info->read = ram_read; - mtd_info->write = ram_write;
+ addr = vmalloc(MTDRAM_TOTAL_SIZE);
+ if (!addr) {
+ DEBUG(MTD_DEBUG_LEVEL1,
+ "Failed to vmalloc memory region of size %ld\n",
+ (long)MTDRAM_TOTAL_SIZE);
+ kfree(mtd_info); + mtd_info = NULL; + return -ENOMEM;
+ }
+ err = mtdram_init_device(mtd_info, addr,
+ MTDRAM_TOTAL_SIZE, "mtdram test device");
+ if (err)
+ {
+ vfree(addr); + kfree(mtd_info); + mtd_info = NULL; + return err; + } + memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE); + return err;
+}
+#endif /* !(CONFIG_MTDRAM_ABS_POS > 0) */
+
+#else /* CONFIG_MTDRAM_TOTAL_SIZE > 0 */
- if (add_mtd_device(mtd_info)) {
-#if CONFIG_MTDRAM_ABS_POS > 0
- iounmap(mtd_info->priv);
-#else
- vfree(mtd_info->priv);
-#endif
- kfree(mtd_info); - mtd_info = NULL; - return -EIO;
- }
-
- return 0;
+int __init init_mtdram(void)
+{
+ return 0;
}
+#endif /* !(CONFIG_MTDRAM_TOTAL_SIZE > 0) */
module_init(init_mtdram);
module_exit(cleanup_mtdram);
include/linux/mtd/mtdram.h Mon May 6 10:02:26 2002 renamed from dev/null Sat Mar 24 05:37:44 2001 +10 -0
@@ -0,0 +1,10 @@ +#ifndef __MTD_MTDRAM_H__ +#define __MTD_MTDRAM_H__ + +#ifdef __KERNEL__ +#include <linux/mtd/mtd.h> +int mtdram_init_device(struct mtd_info *mtd, void *mapped_address, + unsigned long size, char *name); + +#endif /* __KERNEL__ */ +#endif /* __MTD_MTDRAM_H__ */
Documentation/Configure.help 6 May 2002 08:40:34 -0000 renamed from Documentation/Configure.help 23 Apr 2002 15:46:44 -0000 1.46 +5 -3
@@ -12639,9 +12639,9 @@ CONFIG_MTD_SLRAM Debugging RAM test driver CONFIG_MTD_MTDRAM - This enables a test MTD device driver which uses vmalloc() to - provide storage. You probably want to say 'N' unless you're - testing stuff. + This enables a test MTD device driver which uses vmalloc() + or an absolute address to provide storage. + You probably want to say 'N' unless you're testing stuff. MTDRAM erase block size in KB CONFIG_MTDRAM_ERASE_SIZE
@@ -12656,6 +12656,8 @@ CONFIG_MTDRAM_TOTAL_SIZE emulated by the MTDRAM driver. If the MTDRAM driver is built as a module, it is also possible to specify this as a parameter when loading the module. + If you want to set the size and position at runtime, set to 0, + in that case set the ABS_POS parameter to 0 as well. SRAM Hexadecimal Absolute position or 0 CONFIG_MTDRAM_ABS_POS