mac80211: fix virtual interfaces vs. injection
[linux/fpc-iii.git] / drivers / mtd / devices / phram.c
blob088fbb7595b5adbb5fee1e240ecd605b4a53b51e
1 /**
2 * Copyright (c) ???? Jochen Schäuble <psionic@psionic.de>
3 * Copyright (c) 2003-2004 Joern Engel <joern@wh.fh-wedel.de>
5 * Usage:
7 * one commend line parameter per device, each in the form:
8 * phram=<name>,<start>,<len>
9 * <name> may be up to 63 characters.
10 * <start> and <len> can be octal, decimal or hexadecimal. If followed
11 * by "ki", "Mi" or "Gi", the numbers will be interpreted as kilo, mega or
12 * gigabytes.
14 * Example:
15 * phram=swap,64Mi,128Mi phram=test,900Mi,1Mi
17 #include <asm/io.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/slab.h>
24 #include <linux/mtd/mtd.h>
26 #define ERROR(fmt, args...) printk(KERN_ERR "phram: " fmt , ## args)
28 struct phram_mtd_list {
29 struct mtd_info mtd;
30 struct list_head list;
33 static LIST_HEAD(phram_list);
36 static int phram_erase(struct mtd_info *mtd, struct erase_info *instr)
38 u_char *start = mtd->priv;
40 if (instr->addr + instr->len > mtd->size)
41 return -EINVAL;
43 memset(start + instr->addr, 0xff, instr->len);
45 /* This'll catch a few races. Free the thing before returning :)
46 * I don't feel at all ashamed. This kind of thing is possible anyway
47 * with flash, but unlikely.
50 instr->state = MTD_ERASE_DONE;
52 mtd_erase_callback(instr);
54 return 0;
57 static int phram_point(struct mtd_info *mtd, loff_t from, size_t len,
58 size_t *retlen, void **virt, resource_size_t *phys)
60 if (from + len > mtd->size)
61 return -EINVAL;
63 /* can we return a physical address with this driver? */
64 if (phys)
65 return -EINVAL;
67 *virt = mtd->priv + from;
68 *retlen = len;
69 return 0;
72 static void phram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
76 static int phram_read(struct mtd_info *mtd, loff_t from, size_t len,
77 size_t *retlen, u_char *buf)
79 u_char *start = mtd->priv;
81 if (from >= mtd->size)
82 return -EINVAL;
84 if (len > mtd->size - from)
85 len = mtd->size - from;
87 memcpy(buf, start + from, len);
89 *retlen = len;
90 return 0;
93 static int phram_write(struct mtd_info *mtd, loff_t to, size_t len,
94 size_t *retlen, const u_char *buf)
96 u_char *start = mtd->priv;
98 if (to >= mtd->size)
99 return -EINVAL;
101 if (len > mtd->size - to)
102 len = mtd->size - to;
104 memcpy(start + to, buf, len);
106 *retlen = len;
107 return 0;
112 static void unregister_devices(void)
114 struct phram_mtd_list *this, *safe;
116 list_for_each_entry_safe(this, safe, &phram_list, list) {
117 del_mtd_device(&this->mtd);
118 iounmap(this->mtd.priv);
119 kfree(this);
123 static int register_device(char *name, unsigned long start, unsigned long len)
125 struct phram_mtd_list *new;
126 int ret = -ENOMEM;
128 new = kzalloc(sizeof(*new), GFP_KERNEL);
129 if (!new)
130 goto out0;
132 ret = -EIO;
133 new->mtd.priv = ioremap(start, len);
134 if (!new->mtd.priv) {
135 ERROR("ioremap failed\n");
136 goto out1;
140 new->mtd.name = name;
141 new->mtd.size = len;
142 new->mtd.flags = MTD_CAP_RAM;
143 new->mtd.erase = phram_erase;
144 new->mtd.point = phram_point;
145 new->mtd.unpoint = phram_unpoint;
146 new->mtd.read = phram_read;
147 new->mtd.write = phram_write;
148 new->mtd.owner = THIS_MODULE;
149 new->mtd.type = MTD_RAM;
150 new->mtd.erasesize = PAGE_SIZE;
151 new->mtd.writesize = 1;
153 ret = -EAGAIN;
154 if (add_mtd_device(&new->mtd)) {
155 ERROR("Failed to register new device\n");
156 goto out2;
159 list_add_tail(&new->list, &phram_list);
160 return 0;
162 out2:
163 iounmap(new->mtd.priv);
164 out1:
165 kfree(new);
166 out0:
167 return ret;
170 static int ustrtoul(const char *cp, char **endp, unsigned int base)
172 unsigned long result = simple_strtoul(cp, endp, base);
174 switch (**endp) {
175 case 'G':
176 result *= 1024;
177 case 'M':
178 result *= 1024;
179 case 'k':
180 result *= 1024;
181 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
182 if ((*endp)[1] == 'i')
183 (*endp) += 2;
185 return result;
188 static int parse_num32(uint32_t *num32, const char *token)
190 char *endp;
191 unsigned long n;
193 n = ustrtoul(token, &endp, 0);
194 if (*endp)
195 return -EINVAL;
197 *num32 = n;
198 return 0;
201 static int parse_name(char **pname, const char *token)
203 size_t len;
204 char *name;
206 len = strlen(token) + 1;
207 if (len > 64)
208 return -ENOSPC;
210 name = kmalloc(len, GFP_KERNEL);
211 if (!name)
212 return -ENOMEM;
214 strcpy(name, token);
216 *pname = name;
217 return 0;
221 static inline void kill_final_newline(char *str)
223 char *newline = strrchr(str, '\n');
224 if (newline && !newline[1])
225 *newline = 0;
229 #define parse_err(fmt, args...) do { \
230 ERROR(fmt , ## args); \
231 return 0; \
232 } while (0)
234 static int phram_setup(const char *val, struct kernel_param *kp)
236 char buf[64+12+12], *str = buf;
237 char *token[3];
238 char *name;
239 uint32_t start;
240 uint32_t len;
241 int i, ret;
243 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
244 parse_err("parameter too long\n");
246 strcpy(str, val);
247 kill_final_newline(str);
249 for (i=0; i<3; i++)
250 token[i] = strsep(&str, ",");
252 if (str)
253 parse_err("too many arguments\n");
255 if (!token[2])
256 parse_err("not enough arguments\n");
258 ret = parse_name(&name, token[0]);
259 if (ret == -ENOMEM)
260 parse_err("out of memory\n");
261 if (ret == -ENOSPC)
262 parse_err("name too long\n");
263 if (ret)
264 return 0;
266 ret = parse_num32(&start, token[1]);
267 if (ret) {
268 kfree(name);
269 parse_err("illegal start address\n");
272 ret = parse_num32(&len, token[2]);
273 if (ret) {
274 kfree(name);
275 parse_err("illegal device length\n");
278 register_device(name, start, len);
280 return 0;
283 module_param_call(phram, phram_setup, NULL, NULL, 000);
284 MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>\"");
287 static int __init init_phram(void)
289 return 0;
292 static void __exit cleanup_phram(void)
294 unregister_devices();
297 module_init(init_phram);
298 module_exit(cleanup_phram);
300 MODULE_LICENSE("GPL");
301 MODULE_AUTHOR("Joern Engel <joern@wh.fh-wedel.de>");
302 MODULE_DESCRIPTION("MTD driver for physical RAM");