~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/mtd/mtdram.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* 
  2  * mtdram - a test mtd device
  3  * $Id: mtdram.c,v 1.15 2000/07/13 12:40:46 scote1 Exp $
  4  * Author: Alexander Larsson <alex@cendio.se> 
  5  *
  6  * Copyright (c) 1999 Alexander Larsson <alex@cendio.se>
  7  *
  8  * This code is GPL
  9  *
 10  */
 11 
 12 #include <linux/config.h>
 13 #include <linux/module.h>
 14 
 15 #include <linux/malloc.h>
 16 #include <linux/ioport.h>
 17 #include <linux/mtd/compatmac.h>
 18 #include <linux/mtd/mtd.h>
 19 
 20 
 21 #define MTDRAM_TOTAL_SIZE (CONFIG_MTDRAM_TOTAL_SIZE * 1024)
 22 #define MTDRAM_ERASE_SIZE (CONFIG_MTDRAM_ERASE_SIZE * 1024)
 23 
 24 
 25 // We could store these in the mtd structure, but we only support 1 device..
 26 static struct mtd_info *mtd_info;
 27 
 28 
 29 static int
 30 ram_erase(struct mtd_info *mtd, struct erase_info *instr)
 31 {
 32   if (instr->addr + instr->len > mtd->size)
 33     return -EINVAL;
 34         
 35   memset((char *)mtd->priv + instr->addr, 0xff, instr->len);
 36   
 37   instr->state = MTD_ERASE_DONE;
 38 
 39   if (instr->callback)
 40     (*(instr->callback))(instr);
 41   return 0;
 42 }
 43 
 44 static int ram_point (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf)
 45 {
 46   if (from + len > mtd->size)
 47     return -EINVAL;
 48   
 49   *mtdbuf = mtd->priv + from;
 50   *retlen = len;
 51   return 0;
 52 }
 53 
 54 static void ram_unpoint (struct mtd_info *mtd, u_char *addr)
 55 {
 56 }
 57 
 58 static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
 59              size_t *retlen, u_char *buf)
 60 {
 61   if (from + len > mtd->size)
 62     return -EINVAL;
 63 
 64   memcpy(buf, mtd->priv + from, len);
 65 
 66   *retlen=len;
 67   return 0;
 68 }
 69 
 70 static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
 71               size_t *retlen, const u_char *buf)
 72 {
 73   if (to + len > mtd->size)
 74     return -EINVAL;
 75   
 76   memcpy ((char *)mtd->priv + to, buf, len);
 77 
 78   *retlen=len;
 79   return 0;
 80 }
 81 
 82 #if LINUX_VERSION_CODE < 0x20300
 83 #ifdef MODULE
 84 #define init_mtdram init_module
 85 #define cleanup_mtdram cleanup_module
 86 #endif
 87 #endif
 88 
 89 //static void __exit cleanup_mtdram(void)
 90 mod_exit_t cleanup_mtdram(void)
 91 {
 92   if (mtd_info) {
 93     del_mtd_device(mtd_info);
 94     if (mtd_info->priv)
 95       vfree(mtd_info->priv);
 96     kfree(mtd_info);
 97   }
 98 }
 99 
100 extern struct module __this_module;
101 
102 mod_init_t init_mtdram(void)
103 {
104    // Allocate some memory
105    mtd_info = (struct mtd_info *)kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
106    if (mtd_info == 0)
107       return 0;
108    
109    memset(mtd_info, 0, sizeof(*mtd_info));
110 
111    // Setup the MTD structure
112    mtd_info->name = "mtdram test device";
113    mtd_info->type = MTD_RAM;
114    mtd_info->flags = MTD_CAP_RAM;
115    mtd_info->size = MTDRAM_TOTAL_SIZE;
116    mtd_info->erasesize = MTDRAM_ERASE_SIZE;
117    mtd_info->priv = vmalloc(MTDRAM_TOTAL_SIZE);
118    memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
119 
120 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
121    mtd_info->module = THIS_MODULE;                      
122 #endif
123 
124    if (!mtd_info->priv) {
125      kfree(mtd_info);
126      mtd_info = NULL;
127      return -ENOMEM;
128    }
129    mtd_info->erase = ram_erase;
130    mtd_info->point = ram_point;
131    mtd_info->unpoint = ram_unpoint;
132    mtd_info->read = ram_read;
133    mtd_info->write = ram_write;
134 
135    if (add_mtd_device(mtd_info)) {
136      vfree(mtd_info->priv);
137      kfree(mtd_info);
138      mtd_info = NULL;
139      return -EIO;
140    }
141    
142    return 0;
143 }
144 
145 #if LINUX_VERSION_CODE > 0x20300
146 module_init(init_mtdram);
147 module_exit(cleanup_mtdram);
148 #endif
149 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.