Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 2 | * $Id: phram.c,v 1.16 2005/11/07 11:14:25 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
David Woodhouse | 151e765 | 2006-05-14 01:51:54 +0100 | [diff] [blame] | 4 | * Copyright (c) ???? Jochen Schäuble <psionic@psionic.de> |
| 5 | * Copyright (c) 2003-2004 Jörn Engel <joern@wh.fh-wedel.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Usage: |
| 8 | * |
| 9 | * one commend line parameter per device, each in the form: |
| 10 | * phram=<name>,<start>,<len> |
| 11 | * <name> may be up to 63 characters. |
| 12 | * <start> and <len> can be octal, decimal or hexadecimal. If followed |
| 13 | * by "ki", "Mi" or "Gi", the numbers will be interpreted as kilo, mega or |
| 14 | * gigabytes. |
| 15 | * |
| 16 | * Example: |
| 17 | * phram=swap,64Mi,128Mi phram=test,900Mi,1Mi |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/io.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/list.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/moduleparam.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 25 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/mtd/mtd.h> |
| 27 | |
| 28 | #define ERROR(fmt, args...) printk(KERN_ERR "phram: " fmt , ## args) |
| 29 | |
| 30 | struct phram_mtd_list { |
| 31 | struct mtd_info mtd; |
| 32 | struct list_head list; |
| 33 | }; |
| 34 | |
| 35 | static LIST_HEAD(phram_list); |
| 36 | |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | static int phram_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 39 | { |
| 40 | u_char *start = mtd->priv; |
| 41 | |
| 42 | if (instr->addr + instr->len > mtd->size) |
| 43 | return -EINVAL; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | memset(start + instr->addr, 0xff, instr->len); |
| 46 | |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 47 | /* This'll catch a few races. Free the thing before returning :) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | * I don't feel at all ashamed. This kind of thing is possible anyway |
| 49 | * with flash, but unlikely. |
| 50 | */ |
| 51 | |
| 52 | instr->state = MTD_ERASE_DONE; |
| 53 | |
| 54 | mtd_erase_callback(instr); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int phram_point(struct mtd_info *mtd, loff_t from, size_t len, |
| 60 | size_t *retlen, u_char **mtdbuf) |
| 61 | { |
| 62 | u_char *start = mtd->priv; |
| 63 | |
| 64 | if (from + len > mtd->size) |
| 65 | return -EINVAL; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | *mtdbuf = start + from; |
| 68 | *retlen = len; |
| 69 | return 0; |
| 70 | } |
| 71 | |
Joern Engel | 663259a | 2005-03-07 21:43:42 +0000 | [diff] [blame] | 72 | static void phram_unpoint(struct mtd_info *mtd, u_char *addr, loff_t from, |
| 73 | size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
| 75 | } |
| 76 | |
| 77 | static int phram_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 78 | size_t *retlen, u_char *buf) |
| 79 | { |
| 80 | u_char *start = mtd->priv; |
| 81 | |
Joern Engel | 663259a | 2005-03-07 21:43:42 +0000 | [diff] [blame] | 82 | if (from >= mtd->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return -EINVAL; |
Joern Engel | 663259a | 2005-03-07 21:43:42 +0000 | [diff] [blame] | 84 | |
| 85 | if (len > mtd->size - from) |
| 86 | len = mtd->size - from; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | memcpy(buf, start + from, len); |
| 89 | |
| 90 | *retlen = len; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int phram_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 95 | size_t *retlen, const u_char *buf) |
| 96 | { |
| 97 | u_char *start = mtd->priv; |
| 98 | |
Joern Engel | 663259a | 2005-03-07 21:43:42 +0000 | [diff] [blame] | 99 | if (to >= mtd->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return -EINVAL; |
Joern Engel | 663259a | 2005-03-07 21:43:42 +0000 | [diff] [blame] | 101 | |
| 102 | if (len > mtd->size - to) |
| 103 | len = mtd->size - to; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | memcpy(start + to, buf, len); |
| 106 | |
| 107 | *retlen = len; |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | static void unregister_devices(void) |
| 114 | { |
Joern Engel | d30f11d | 2005-02-23 19:37:11 +0000 | [diff] [blame] | 115 | struct phram_mtd_list *this, *safe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Joern Engel | d30f11d | 2005-02-23 19:37:11 +0000 | [diff] [blame] | 117 | list_for_each_entry_safe(this, safe, &phram_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | del_mtd_device(&this->mtd); |
| 119 | iounmap(this->mtd.priv); |
| 120 | kfree(this); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static int register_device(char *name, unsigned long start, unsigned long len) |
| 125 | { |
| 126 | struct phram_mtd_list *new; |
| 127 | int ret = -ENOMEM; |
| 128 | |
| 129 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
| 130 | if (!new) |
| 131 | goto out0; |
| 132 | |
| 133 | memset(new, 0, sizeof(*new)); |
| 134 | |
| 135 | ret = -EIO; |
| 136 | new->mtd.priv = ioremap(start, len); |
| 137 | if (!new->mtd.priv) { |
| 138 | ERROR("ioremap failed\n"); |
| 139 | goto out1; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | new->mtd.name = name; |
| 144 | new->mtd.size = len; |
Jörn Engel | a6c591e | 2006-04-13 18:54:34 +0200 | [diff] [blame] | 145 | new->mtd.flags = MTD_CAP_RAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | new->mtd.erase = phram_erase; |
| 147 | new->mtd.point = phram_point; |
| 148 | new->mtd.unpoint = phram_unpoint; |
| 149 | new->mtd.read = phram_read; |
| 150 | new->mtd.write = phram_write; |
| 151 | new->mtd.owner = THIS_MODULE; |
David Woodhouse | 21c8db9 | 2006-06-14 21:39:48 +0100 | [diff] [blame] | 152 | new->mtd.type = MTD_RAM; |
Joern Engel | 663259a | 2005-03-07 21:43:42 +0000 | [diff] [blame] | 153 | new->mtd.erasesize = PAGE_SIZE; |
Artem B. Bityutskiy | 17ffc7b | 2006-06-22 18:15:48 +0400 | [diff] [blame] | 154 | new->mtd.writesize = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | ret = -EAGAIN; |
| 157 | if (add_mtd_device(&new->mtd)) { |
| 158 | ERROR("Failed to register new device\n"); |
| 159 | goto out2; |
| 160 | } |
| 161 | |
| 162 | list_add_tail(&new->list, &phram_list); |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 163 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | out2: |
| 166 | iounmap(new->mtd.priv); |
| 167 | out1: |
| 168 | kfree(new); |
| 169 | out0: |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | static int ustrtoul(const char *cp, char **endp, unsigned int base) |
| 174 | { |
| 175 | unsigned long result = simple_strtoul(cp, endp, base); |
| 176 | |
| 177 | switch (**endp) { |
| 178 | case 'G': |
| 179 | result *= 1024; |
| 180 | case 'M': |
| 181 | result *= 1024; |
| 182 | case 'k': |
| 183 | result *= 1024; |
| 184 | /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */ |
| 185 | if ((*endp)[1] == 'i') |
| 186 | (*endp) += 2; |
| 187 | } |
| 188 | return result; |
| 189 | } |
| 190 | |
| 191 | static int parse_num32(uint32_t *num32, const char *token) |
| 192 | { |
| 193 | char *endp; |
| 194 | unsigned long n; |
| 195 | |
| 196 | n = ustrtoul(token, &endp, 0); |
| 197 | if (*endp) |
| 198 | return -EINVAL; |
| 199 | |
| 200 | *num32 = n; |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int parse_name(char **pname, const char *token) |
| 205 | { |
| 206 | size_t len; |
| 207 | char *name; |
| 208 | |
| 209 | len = strlen(token) + 1; |
| 210 | if (len > 64) |
| 211 | return -ENOSPC; |
| 212 | |
| 213 | name = kmalloc(len, GFP_KERNEL); |
| 214 | if (!name) |
| 215 | return -ENOMEM; |
| 216 | |
| 217 | strcpy(name, token); |
| 218 | |
| 219 | *pname = name; |
| 220 | return 0; |
| 221 | } |
| 222 | |
Joern Engel | 663259a | 2005-03-07 21:43:42 +0000 | [diff] [blame] | 223 | |
| 224 | static inline void kill_final_newline(char *str) |
| 225 | { |
| 226 | char *newline = strrchr(str, '\n'); |
| 227 | if (newline && !newline[1]) |
| 228 | *newline = 0; |
| 229 | } |
| 230 | |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | #define parse_err(fmt, args...) do { \ |
| 233 | ERROR(fmt , ## args); \ |
| 234 | return 0; \ |
| 235 | } while (0) |
| 236 | |
| 237 | static int phram_setup(const char *val, struct kernel_param *kp) |
| 238 | { |
| 239 | char buf[64+12+12], *str = buf; |
| 240 | char *token[3]; |
| 241 | char *name; |
| 242 | uint32_t start; |
| 243 | uint32_t len; |
| 244 | int i, ret; |
| 245 | |
| 246 | if (strnlen(val, sizeof(buf)) >= sizeof(buf)) |
| 247 | parse_err("parameter too long\n"); |
| 248 | |
| 249 | strcpy(str, val); |
Joern Engel | 663259a | 2005-03-07 21:43:42 +0000 | [diff] [blame] | 250 | kill_final_newline(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | for (i=0; i<3; i++) |
| 253 | token[i] = strsep(&str, ","); |
| 254 | |
| 255 | if (str) |
| 256 | parse_err("too many arguments\n"); |
| 257 | |
| 258 | if (!token[2]) |
| 259 | parse_err("not enough arguments\n"); |
| 260 | |
| 261 | ret = parse_name(&name, token[0]); |
| 262 | if (ret == -ENOMEM) |
| 263 | parse_err("out of memory\n"); |
| 264 | if (ret == -ENOSPC) |
| 265 | parse_err("name too long\n"); |
| 266 | if (ret) |
| 267 | return 0; |
| 268 | |
| 269 | ret = parse_num32(&start, token[1]); |
Jesper Juhl | 4f678a5 | 2006-05-14 01:07:18 +0200 | [diff] [blame] | 270 | if (ret) { |
| 271 | kfree(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | parse_err("illegal start address\n"); |
Jesper Juhl | 4f678a5 | 2006-05-14 01:07:18 +0200 | [diff] [blame] | 273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | ret = parse_num32(&len, token[2]); |
Jesper Juhl | 4f678a5 | 2006-05-14 01:07:18 +0200 | [diff] [blame] | 276 | if (ret) { |
| 277 | kfree(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | parse_err("illegal device length\n"); |
Jesper Juhl | 4f678a5 | 2006-05-14 01:07:18 +0200 | [diff] [blame] | 279 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
| 281 | register_device(name, start, len); |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | module_param_call(phram, phram_setup, NULL, NULL, 000); |
| 287 | MODULE_PARM_DESC(phram,"Memory region to map. \"map=<name>,<start>,<length>\""); |
| 288 | |
| 289 | |
| 290 | static int __init init_phram(void) |
| 291 | { |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static void __exit cleanup_phram(void) |
| 296 | { |
| 297 | unregister_devices(); |
| 298 | } |
| 299 | |
| 300 | module_init(init_phram); |
| 301 | module_exit(cleanup_phram); |
| 302 | |
| 303 | MODULE_LICENSE("GPL"); |
David Woodhouse | 151e765 | 2006-05-14 01:51:54 +0100 | [diff] [blame] | 304 | MODULE_AUTHOR("Jörn Engel <joern@wh.fh-wedel.de>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | MODULE_DESCRIPTION("MTD driver for physical RAM"); |