NOMMU: Present backing device capabilities for MTD chardevs
[linux-2.6/cjktty.git] / drivers / mtd / mtdcore.c
blob65a7f37038815addb7cc41d721de8d1be4d10b91
1 /*
2 * Core registration and callback routines for MTD
3 * drivers and users.
5 */
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/ptrace.h>
10 #include <linux/slab.h>
11 #include <linux/string.h>
12 #include <linux/timer.h>
13 #include <linux/major.h>
14 #include <linux/fs.h>
15 #include <linux/err.h>
16 #include <linux/ioctl.h>
17 #include <linux/init.h>
18 #include <linux/mtd/compatmac.h>
19 #include <linux/proc_fs.h>
21 #include <linux/mtd/mtd.h>
22 #include "internal.h"
24 #include "mtdcore.h"
26 /* These are exported solely for the purpose of mtd_blkdevs.c. You
27 should not use them for _anything_ else */
28 DEFINE_MUTEX(mtd_table_mutex);
29 struct mtd_info *mtd_table[MAX_MTD_DEVICES];
31 EXPORT_SYMBOL_GPL(mtd_table_mutex);
32 EXPORT_SYMBOL_GPL(mtd_table);
34 static LIST_HEAD(mtd_notifiers);
36 /**
37 * add_mtd_device - register an MTD device
38 * @mtd: pointer to new MTD device info structure
40 * Add a device to the list of MTD devices present in the system, and
41 * notify each currently active MTD 'user' of its arrival. Returns
42 * zero on success or 1 on failure, which currently will only happen
43 * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
46 int add_mtd_device(struct mtd_info *mtd)
48 int i;
50 if (!mtd->backing_dev_info) {
51 switch (mtd->type) {
52 case MTD_RAM:
53 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
54 break;
55 case MTD_ROM:
56 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
57 break;
58 default:
59 mtd->backing_dev_info = &mtd_bdi_unmappable;
60 break;
64 BUG_ON(mtd->writesize == 0);
65 mutex_lock(&mtd_table_mutex);
67 for (i=0; i < MAX_MTD_DEVICES; i++)
68 if (!mtd_table[i]) {
69 struct mtd_notifier *not;
71 mtd_table[i] = mtd;
72 mtd->index = i;
73 mtd->usecount = 0;
75 if (is_power_of_2(mtd->erasesize))
76 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
77 else
78 mtd->erasesize_shift = 0;
80 if (is_power_of_2(mtd->writesize))
81 mtd->writesize_shift = ffs(mtd->writesize) - 1;
82 else
83 mtd->writesize_shift = 0;
85 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
86 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
88 /* Some chips always power up locked. Unlock them now */
89 if ((mtd->flags & MTD_WRITEABLE)
90 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
91 if (mtd->unlock(mtd, 0, mtd->size))
92 printk(KERN_WARNING
93 "%s: unlock failed, "
94 "writes may not work\n",
95 mtd->name);
98 DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
99 /* No need to get a refcount on the module containing
100 the notifier, since we hold the mtd_table_mutex */
101 list_for_each_entry(not, &mtd_notifiers, list)
102 not->add(mtd);
104 mutex_unlock(&mtd_table_mutex);
105 /* We _know_ we aren't being removed, because
106 our caller is still holding us here. So none
107 of this try_ nonsense, and no bitching about it
108 either. :) */
109 __module_get(THIS_MODULE);
110 return 0;
113 mutex_unlock(&mtd_table_mutex);
114 return 1;
118 * del_mtd_device - unregister an MTD device
119 * @mtd: pointer to MTD device info structure
121 * Remove a device from the list of MTD devices present in the system,
122 * and notify each currently active MTD 'user' of its departure.
123 * Returns zero on success or 1 on failure, which currently will happen
124 * if the requested device does not appear to be present in the list.
127 int del_mtd_device (struct mtd_info *mtd)
129 int ret;
131 mutex_lock(&mtd_table_mutex);
133 if (mtd_table[mtd->index] != mtd) {
134 ret = -ENODEV;
135 } else if (mtd->usecount) {
136 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
137 mtd->index, mtd->name, mtd->usecount);
138 ret = -EBUSY;
139 } else {
140 struct mtd_notifier *not;
142 /* No need to get a refcount on the module containing
143 the notifier, since we hold the mtd_table_mutex */
144 list_for_each_entry(not, &mtd_notifiers, list)
145 not->remove(mtd);
147 mtd_table[mtd->index] = NULL;
149 module_put(THIS_MODULE);
150 ret = 0;
153 mutex_unlock(&mtd_table_mutex);
154 return ret;
158 * register_mtd_user - register a 'user' of MTD devices.
159 * @new: pointer to notifier info structure
161 * Registers a pair of callbacks function to be called upon addition
162 * or removal of MTD devices. Causes the 'add' callback to be immediately
163 * invoked for each MTD device currently present in the system.
166 void register_mtd_user (struct mtd_notifier *new)
168 int i;
170 mutex_lock(&mtd_table_mutex);
172 list_add(&new->list, &mtd_notifiers);
174 __module_get(THIS_MODULE);
176 for (i=0; i< MAX_MTD_DEVICES; i++)
177 if (mtd_table[i])
178 new->add(mtd_table[i]);
180 mutex_unlock(&mtd_table_mutex);
184 * unregister_mtd_user - unregister a 'user' of MTD devices.
185 * @old: pointer to notifier info structure
187 * Removes a callback function pair from the list of 'users' to be
188 * notified upon addition or removal of MTD devices. Causes the
189 * 'remove' callback to be immediately invoked for each MTD device
190 * currently present in the system.
193 int unregister_mtd_user (struct mtd_notifier *old)
195 int i;
197 mutex_lock(&mtd_table_mutex);
199 module_put(THIS_MODULE);
201 for (i=0; i< MAX_MTD_DEVICES; i++)
202 if (mtd_table[i])
203 old->remove(mtd_table[i]);
205 list_del(&old->list);
206 mutex_unlock(&mtd_table_mutex);
207 return 0;
212 * get_mtd_device - obtain a validated handle for an MTD device
213 * @mtd: last known address of the required MTD device
214 * @num: internal device number of the required MTD device
216 * Given a number and NULL address, return the num'th entry in the device
217 * table, if any. Given an address and num == -1, search the device table
218 * for a device with that address and return if it's still present. Given
219 * both, return the num'th driver only if its address matches. Return
220 * error code if not.
223 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
225 struct mtd_info *ret = NULL;
226 int i, err = -ENODEV;
228 mutex_lock(&mtd_table_mutex);
230 if (num == -1) {
231 for (i=0; i< MAX_MTD_DEVICES; i++)
232 if (mtd_table[i] == mtd)
233 ret = mtd_table[i];
234 } else if (num < MAX_MTD_DEVICES) {
235 ret = mtd_table[num];
236 if (mtd && mtd != ret)
237 ret = NULL;
240 if (!ret)
241 goto out_unlock;
243 if (!try_module_get(ret->owner))
244 goto out_unlock;
246 if (ret->get_device) {
247 err = ret->get_device(ret);
248 if (err)
249 goto out_put;
252 ret->usecount++;
253 mutex_unlock(&mtd_table_mutex);
254 return ret;
256 out_put:
257 module_put(ret->owner);
258 out_unlock:
259 mutex_unlock(&mtd_table_mutex);
260 return ERR_PTR(err);
264 * get_mtd_device_nm - obtain a validated handle for an MTD device by
265 * device name
266 * @name: MTD device name to open
268 * This function returns MTD device description structure in case of
269 * success and an error code in case of failure.
272 struct mtd_info *get_mtd_device_nm(const char *name)
274 int i, err = -ENODEV;
275 struct mtd_info *mtd = NULL;
277 mutex_lock(&mtd_table_mutex);
279 for (i = 0; i < MAX_MTD_DEVICES; i++) {
280 if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
281 mtd = mtd_table[i];
282 break;
286 if (!mtd)
287 goto out_unlock;
289 if (!try_module_get(mtd->owner))
290 goto out_unlock;
292 if (mtd->get_device) {
293 err = mtd->get_device(mtd);
294 if (err)
295 goto out_put;
298 mtd->usecount++;
299 mutex_unlock(&mtd_table_mutex);
300 return mtd;
302 out_put:
303 module_put(mtd->owner);
304 out_unlock:
305 mutex_unlock(&mtd_table_mutex);
306 return ERR_PTR(err);
309 void put_mtd_device(struct mtd_info *mtd)
311 int c;
313 mutex_lock(&mtd_table_mutex);
314 c = --mtd->usecount;
315 if (mtd->put_device)
316 mtd->put_device(mtd);
317 mutex_unlock(&mtd_table_mutex);
318 BUG_ON(c < 0);
320 module_put(mtd->owner);
323 /* default_mtd_writev - default mtd writev method for MTD devices that
324 * don't implement their own
327 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
328 unsigned long count, loff_t to, size_t *retlen)
330 unsigned long i;
331 size_t totlen = 0, thislen;
332 int ret = 0;
334 if(!mtd->write) {
335 ret = -EROFS;
336 } else {
337 for (i=0; i<count; i++) {
338 if (!vecs[i].iov_len)
339 continue;
340 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
341 totlen += thislen;
342 if (ret || thislen != vecs[i].iov_len)
343 break;
344 to += vecs[i].iov_len;
347 if (retlen)
348 *retlen = totlen;
349 return ret;
352 EXPORT_SYMBOL_GPL(add_mtd_device);
353 EXPORT_SYMBOL_GPL(del_mtd_device);
354 EXPORT_SYMBOL_GPL(get_mtd_device);
355 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
356 EXPORT_SYMBOL_GPL(put_mtd_device);
357 EXPORT_SYMBOL_GPL(register_mtd_user);
358 EXPORT_SYMBOL_GPL(unregister_mtd_user);
359 EXPORT_SYMBOL_GPL(default_mtd_writev);
361 #ifdef CONFIG_PROC_FS
363 /*====================================================================*/
364 /* Support for /proc/mtd */
366 static struct proc_dir_entry *proc_mtd;
368 static inline int mtd_proc_info (char *buf, int i)
370 struct mtd_info *this = mtd_table[i];
372 if (!this)
373 return 0;
375 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
376 (unsigned long long)this->size,
377 this->erasesize, this->name);
380 static int mtd_read_proc (char *page, char **start, off_t off, int count,
381 int *eof, void *data_unused)
383 int len, l, i;
384 off_t begin = 0;
386 mutex_lock(&mtd_table_mutex);
388 len = sprintf(page, "dev: size erasesize name\n");
389 for (i=0; i< MAX_MTD_DEVICES; i++) {
391 l = mtd_proc_info(page + len, i);
392 len += l;
393 if (len+begin > off+count)
394 goto done;
395 if (len+begin < off) {
396 begin += len;
397 len = 0;
401 *eof = 1;
403 done:
404 mutex_unlock(&mtd_table_mutex);
405 if (off >= len+begin)
406 return 0;
407 *start = page + (off-begin);
408 return ((count < begin+len-off) ? count : begin+len-off);
411 /*====================================================================*/
412 /* Init code */
414 static int __init init_mtd(void)
416 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
417 proc_mtd->read_proc = mtd_read_proc;
418 return 0;
421 static void __exit cleanup_mtd(void)
423 if (proc_mtd)
424 remove_proc_entry( "mtd", NULL);
427 module_init(init_mtd);
428 module_exit(cleanup_mtd);
430 #endif /* CONFIG_PROC_FS */
433 MODULE_LICENSE("GPL");
434 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
435 MODULE_DESCRIPTION("Core MTD registration and access routines");