2 * Core registration and callback routines for MTD
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>
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>
26 static int mtd_cls_suspend(struct device *dev, pm_message_t state);
27 static int mtd_cls_resume(struct device *dev);
29 static struct class mtd_class = {
32 .suspend = mtd_cls_suspend,
33 .resume = mtd_cls_resume,
36 /* These are exported solely for the purpose of mtd_blkdevs.c. You
37 should not use them for _anything_ else */
38 DEFINE_MUTEX(mtd_table_mutex);
39 struct mtd_info *mtd_table[MAX_MTD_DEVICES];
41 EXPORT_SYMBOL_GPL(mtd_table_mutex);
42 EXPORT_SYMBOL_GPL(mtd_table);
44 static LIST_HEAD(mtd_notifiers);
47 #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
48 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
50 #define MTD_DEVT(index) 0
53 /* REVISIT once MTD uses the driver model better, whoever allocates
54 * the mtd_info will probably want to use the release() hook...
56 static void mtd_release(struct device *dev)
58 dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
60 /* remove /dev/mtdXro node if needed */
62 device_destroy(&mtd_class, index + 1);
65 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
67 struct mtd_info *mtd = dev_to_mtd(dev);
69 if (mtd && mtd->suspend)
70 return mtd->suspend(mtd);
75 static int mtd_cls_resume(struct device *dev)
77 struct mtd_info *mtd = dev_to_mtd(dev);
79 if (mtd && mtd->resume)
84 static ssize_t mtd_type_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
87 struct mtd_info *mtd = dev_to_mtd(dev);
116 return snprintf(buf, PAGE_SIZE, "%s\n", type);
118 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
120 static ssize_t mtd_flags_show(struct device *dev,
121 struct device_attribute *attr, char *buf)
123 struct mtd_info *mtd = dev_to_mtd(dev);
125 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
128 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
130 static ssize_t mtd_size_show(struct device *dev,
131 struct device_attribute *attr, char *buf)
133 struct mtd_info *mtd = dev_to_mtd(dev);
135 return snprintf(buf, PAGE_SIZE, "%llu\n",
136 (unsigned long long)mtd->size);
139 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
141 static ssize_t mtd_erasesize_show(struct device *dev,
142 struct device_attribute *attr, char *buf)
144 struct mtd_info *mtd = dev_to_mtd(dev);
146 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
149 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
151 static ssize_t mtd_writesize_show(struct device *dev,
152 struct device_attribute *attr, char *buf)
154 struct mtd_info *mtd = dev_to_mtd(dev);
156 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
159 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
161 static ssize_t mtd_subpagesize_show(struct device *dev,
162 struct device_attribute *attr, char *buf)
164 struct mtd_info *mtd = dev_to_mtd(dev);
165 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
167 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
170 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
172 static ssize_t mtd_oobsize_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
175 struct mtd_info *mtd = dev_to_mtd(dev);
177 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
180 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
182 static ssize_t mtd_numeraseregions_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
185 struct mtd_info *mtd = dev_to_mtd(dev);
187 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
190 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
193 static ssize_t mtd_name_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
196 struct mtd_info *mtd = dev_to_mtd(dev);
198 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
201 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
203 static struct attribute *mtd_attrs[] = {
205 &dev_attr_flags.attr,
207 &dev_attr_erasesize.attr,
208 &dev_attr_writesize.attr,
209 &dev_attr_subpagesize.attr,
210 &dev_attr_oobsize.attr,
211 &dev_attr_numeraseregions.attr,
216 static struct attribute_group mtd_group = {
220 static const struct attribute_group *mtd_groups[] = {
225 static struct device_type mtd_devtype = {
227 .groups = mtd_groups,
228 .release = mtd_release,
232 * add_mtd_device - register an MTD device
233 * @mtd: pointer to new MTD device info structure
235 * Add a device to the list of MTD devices present in the system, and
236 * notify each currently active MTD 'user' of its arrival. Returns
237 * zero on success or 1 on failure, which currently will only happen
238 * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
239 * or there's a sysfs error.
242 int add_mtd_device(struct mtd_info *mtd)
246 if (!mtd->backing_dev_info) {
249 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
252 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
255 mtd->backing_dev_info = &mtd_bdi_unmappable;
260 BUG_ON(mtd->writesize == 0);
261 mutex_lock(&mtd_table_mutex);
263 for (i=0; i < MAX_MTD_DEVICES; i++)
265 struct mtd_notifier *not;
271 if (is_power_of_2(mtd->erasesize))
272 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
274 mtd->erasesize_shift = 0;
276 if (is_power_of_2(mtd->writesize))
277 mtd->writesize_shift = ffs(mtd->writesize) - 1;
279 mtd->writesize_shift = 0;
281 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
282 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
284 /* Some chips always power up locked. Unlock them now */
285 if ((mtd->flags & MTD_WRITEABLE)
286 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
287 if (mtd->unlock(mtd, 0, mtd->size))
289 "%s: unlock failed, "
290 "writes may not work\n",
294 /* Caller should have set dev.parent to match the
297 mtd->dev.type = &mtd_devtype;
298 mtd->dev.class = &mtd_class;
299 mtd->dev.devt = MTD_DEVT(i);
300 dev_set_name(&mtd->dev, "mtd%d", i);
301 dev_set_drvdata(&mtd->dev, mtd);
302 if (device_register(&mtd->dev) != 0) {
308 device_create(&mtd_class, mtd->dev.parent,
312 DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
313 /* No need to get a refcount on the module containing
314 the notifier, since we hold the mtd_table_mutex */
315 list_for_each_entry(not, &mtd_notifiers, list)
318 mutex_unlock(&mtd_table_mutex);
319 /* We _know_ we aren't being removed, because
320 our caller is still holding us here. So none
321 of this try_ nonsense, and no bitching about it
323 __module_get(THIS_MODULE);
327 mutex_unlock(&mtd_table_mutex);
332 * del_mtd_device - unregister an MTD device
333 * @mtd: pointer to MTD device info structure
335 * Remove a device from the list of MTD devices present in the system,
336 * and notify each currently active MTD 'user' of its departure.
337 * Returns zero on success or 1 on failure, which currently will happen
338 * if the requested device does not appear to be present in the list.
341 int del_mtd_device (struct mtd_info *mtd)
345 mutex_lock(&mtd_table_mutex);
347 if (mtd_table[mtd->index] != mtd) {
349 } else if (mtd->usecount) {
350 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
351 mtd->index, mtd->name, mtd->usecount);
354 struct mtd_notifier *not;
356 device_unregister(&mtd->dev);
358 /* No need to get a refcount on the module containing
359 the notifier, since we hold the mtd_table_mutex */
360 list_for_each_entry(not, &mtd_notifiers, list)
363 mtd_table[mtd->index] = NULL;
365 module_put(THIS_MODULE);
369 mutex_unlock(&mtd_table_mutex);
374 * register_mtd_user - register a 'user' of MTD devices.
375 * @new: pointer to notifier info structure
377 * Registers a pair of callbacks function to be called upon addition
378 * or removal of MTD devices. Causes the 'add' callback to be immediately
379 * invoked for each MTD device currently present in the system.
382 void register_mtd_user (struct mtd_notifier *new)
384 struct mtd_info *mtd;
386 mutex_lock(&mtd_table_mutex);
388 list_add(&new->list, &mtd_notifiers);
390 __module_get(THIS_MODULE);
392 mtd_for_each_device(mtd)
395 mutex_unlock(&mtd_table_mutex);
399 * unregister_mtd_user - unregister a 'user' of MTD devices.
400 * @old: pointer to notifier info structure
402 * Removes a callback function pair from the list of 'users' to be
403 * notified upon addition or removal of MTD devices. Causes the
404 * 'remove' callback to be immediately invoked for each MTD device
405 * currently present in the system.
408 int unregister_mtd_user (struct mtd_notifier *old)
410 struct mtd_info *mtd;
412 mutex_lock(&mtd_table_mutex);
414 module_put(THIS_MODULE);
416 mtd_for_each_device(mtd)
419 list_del(&old->list);
420 mutex_unlock(&mtd_table_mutex);
426 * get_mtd_device - obtain a validated handle for an MTD device
427 * @mtd: last known address of the required MTD device
428 * @num: internal device number of the required MTD device
430 * Given a number and NULL address, return the num'th entry in the device
431 * table, if any. Given an address and num == -1, search the device table
432 * for a device with that address and return if it's still present. Given
433 * both, return the num'th driver only if its address matches. Return
437 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
439 struct mtd_info *ret = NULL, *other;
442 mutex_lock(&mtd_table_mutex);
445 mtd_for_each_device(other) {
451 } else if (num >= 0 && num < MAX_MTD_DEVICES) {
452 ret = mtd_table[num];
453 if (mtd && mtd != ret)
460 if (!try_module_get(ret->owner))
463 if (ret->get_device) {
464 err = ret->get_device(ret);
470 mutex_unlock(&mtd_table_mutex);
474 module_put(ret->owner);
476 mutex_unlock(&mtd_table_mutex);
481 * get_mtd_device_nm - obtain a validated handle for an MTD device by
483 * @name: MTD device name to open
485 * This function returns MTD device description structure in case of
486 * success and an error code in case of failure.
489 struct mtd_info *get_mtd_device_nm(const char *name)
492 struct mtd_info *mtd = NULL, *other;
494 mutex_lock(&mtd_table_mutex);
496 mtd_for_each_device(other) {
497 if (!strcmp(name, other->name)) {
506 if (!try_module_get(mtd->owner))
509 if (mtd->get_device) {
510 err = mtd->get_device(mtd);
516 mutex_unlock(&mtd_table_mutex);
520 module_put(mtd->owner);
522 mutex_unlock(&mtd_table_mutex);
526 void put_mtd_device(struct mtd_info *mtd)
530 mutex_lock(&mtd_table_mutex);
533 mtd->put_device(mtd);
534 mutex_unlock(&mtd_table_mutex);
537 module_put(mtd->owner);
540 /* default_mtd_writev - default mtd writev method for MTD devices that
541 * don't implement their own
544 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
545 unsigned long count, loff_t to, size_t *retlen)
548 size_t totlen = 0, thislen;
554 for (i=0; i<count; i++) {
555 if (!vecs[i].iov_len)
557 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
559 if (ret || thislen != vecs[i].iov_len)
561 to += vecs[i].iov_len;
569 EXPORT_SYMBOL_GPL(add_mtd_device);
570 EXPORT_SYMBOL_GPL(del_mtd_device);
571 EXPORT_SYMBOL_GPL(get_mtd_device);
572 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
573 EXPORT_SYMBOL_GPL(put_mtd_device);
574 EXPORT_SYMBOL_GPL(register_mtd_user);
575 EXPORT_SYMBOL_GPL(unregister_mtd_user);
576 EXPORT_SYMBOL_GPL(default_mtd_writev);
578 #ifdef CONFIG_PROC_FS
580 /*====================================================================*/
581 /* Support for /proc/mtd */
583 static struct proc_dir_entry *proc_mtd;
585 static inline int mtd_proc_info(char *buf, struct mtd_info *this)
587 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
588 (unsigned long long)this->size,
589 this->erasesize, this->name);
592 static int mtd_read_proc (char *page, char **start, off_t off, int count,
593 int *eof, void *data_unused)
595 struct mtd_info *mtd;
599 mutex_lock(&mtd_table_mutex);
601 len = sprintf(page, "dev: size erasesize name\n");
602 mtd_for_each_device(mtd) {
603 l = mtd_proc_info(page + len, mtd);
605 if (len+begin > off+count)
607 if (len+begin < off) {
616 mutex_unlock(&mtd_table_mutex);
617 if (off >= len+begin)
619 *start = page + (off-begin);
620 return ((count < begin+len-off) ? count : begin+len-off);
623 #endif /* CONFIG_PROC_FS */
625 /*====================================================================*/
628 static int __init init_mtd(void)
631 ret = class_register(&mtd_class);
634 pr_err("Error registering mtd class: %d\n", ret);
637 #ifdef CONFIG_PROC_FS
638 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
639 proc_mtd->read_proc = mtd_read_proc;
640 #endif /* CONFIG_PROC_FS */
644 static void __exit cleanup_mtd(void)
646 #ifdef CONFIG_PROC_FS
648 remove_proc_entry( "mtd", NULL);
649 #endif /* CONFIG_PROC_FS */
650 class_unregister(&mtd_class);
653 module_init(init_mtd);
654 module_exit(cleanup_mtd);
656 MODULE_LICENSE("GPL");
657 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
658 MODULE_DESCRIPTION("Core MTD registration and access routines");