2 * mtdram - a test mtd device
3 * $Id: mtdram.c,v 1.13 2000/07/03 10:01:38 dwmw2 Exp $
4 * Author: Alexander Larsson <alex@cendio.se>
6 * Copyright (c) 1999 Alexander Larsson <alex@cendio.se>
12 #include <linux/module.h>
14 #include <linux/malloc.h>
15 #include <linux/ioport.h>
16 #include <linux/mtd/compatmac.h>
17 #include <linux/mtd/mtd.h>
20 #define MTDRAM_TOTAL_SIZE 1024*1024*8
21 #define MTDRAM_ERASE_SIZE 4*1024
24 // We could store these in the mtd structure, but we only support 1 device..
25 static struct mtd_info
*mtd_info
;
29 ram_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
31 if (instr
->addr
+ instr
->len
> mtd
->size
)
34 memset((char *)mtd
->priv
+ instr
->addr
, 0xff, instr
->len
);
36 instr
->state
= MTD_ERASE_DONE
;
39 (*(instr
->callback
))(instr
);
43 static int ram_point (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
**mtdbuf
)
45 if (from
+ len
> mtd
->size
)
48 *mtdbuf
= mtd
->priv
+ from
;
53 static void ram_unpoint (struct mtd_info
*mtd
, u_char
*addr
)
57 static int ram_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
58 size_t *retlen
, u_char
*buf
)
60 if (from
+ len
> mtd
->size
)
63 memcpy(buf
, mtd
->priv
+ from
, len
);
69 static int ram_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
70 size_t *retlen
, const u_char
*buf
)
72 if (to
+ len
> mtd
->size
)
75 memcpy ((char *)mtd
->priv
+ to
, buf
, len
);
81 #if LINUX_VERSION_CODE < 0x20300
83 #define init_mtdram init_module
84 #define cleanup_mtdram cleanup_module
88 //static void __exit cleanup_mtdram(void)
89 mod_exit_t
cleanup_mtdram(void)
92 del_mtd_device(mtd_info
);
94 vfree(mtd_info
->priv
);
99 extern struct module __this_module
;
101 mod_init_t
init_mtdram(void)
103 // Allocate some memory
104 mtd_info
= (struct mtd_info
*)kmalloc(sizeof(struct mtd_info
), GFP_KERNEL
);
108 memset(mtd_info
, 0, sizeof(*mtd_info
));
110 // Setup the MTD structure
111 mtd_info
->name
= "mtdram test device";
112 mtd_info
->type
= MTD_RAM
;
113 mtd_info
->flags
= MTD_CAP_RAM
;
114 mtd_info
->size
= MTDRAM_TOTAL_SIZE
;
115 mtd_info
->erasesize
= MTDRAM_ERASE_SIZE
;
116 mtd_info
->priv
= vmalloc(MTDRAM_TOTAL_SIZE
);
117 memset(mtd_info
->priv
, 0xff, MTDRAM_TOTAL_SIZE
);
119 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
120 mtd_info
->module
= THIS_MODULE
;
123 if (!mtd_info
->priv
) {
128 mtd_info
->erase
= ram_erase
;
129 mtd_info
->point
= ram_point
;
130 mtd_info
->unpoint
= ram_unpoint
;
131 mtd_info
->read
= ram_read
;
132 mtd_info
->write
= ram_write
;
134 if (add_mtd_device(mtd_info
)) {
135 vfree(mtd_info
->priv
);
144 #if LINUX_VERSION_CODE > 0x20300
145 module_init(init_mtdram
);
146 module_exit(cleanup_mtdram
);