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>
27 static struct class *mtd_class;
29 /* These are exported solely for the purpose of mtd_blkdevs.c. You
30 should not use them for _anything_ else */
31 DEFINE_MUTEX(mtd_table_mutex);
32 struct mtd_info *mtd_table[MAX_MTD_DEVICES];
34 EXPORT_SYMBOL_GPL(mtd_table_mutex);
35 EXPORT_SYMBOL_GPL(mtd_table);
37 static LIST_HEAD(mtd_notifiers);
40 #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
41 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
43 #define MTD_DEVT(index) 0
46 /* REVISIT once MTD uses the driver model better, whoever allocates
47 * the mtd_info will probably want to use the release() hook...
49 static void mtd_release(struct device *dev)
51 dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
53 /* remove /dev/mtdXro node if needed */
55 device_destroy(mtd_class, index + 1);
58 static ssize_t mtd_type_show(struct device *dev,
59 struct device_attribute *attr, char *buf)
61 struct mtd_info *mtd = dev_to_mtd(dev);
90 return snprintf(buf, PAGE_SIZE, "%s\n", type);
92 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
94 static ssize_t mtd_flags_show(struct device *dev,
95 struct device_attribute *attr, char *buf)
97 struct mtd_info *mtd = dev_to_mtd(dev);
99 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
102 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
104 static ssize_t mtd_size_show(struct device *dev,
105 struct device_attribute *attr, char *buf)
107 struct mtd_info *mtd = dev_to_mtd(dev);
109 return snprintf(buf, PAGE_SIZE, "%llu\n",
110 (unsigned long long)mtd->size);
113 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
115 static ssize_t mtd_erasesize_show(struct device *dev,
116 struct device_attribute *attr, char *buf)
118 struct mtd_info *mtd = dev_to_mtd(dev);
120 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
123 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
125 static ssize_t mtd_writesize_show(struct device *dev,
126 struct device_attribute *attr, char *buf)
128 struct mtd_info *mtd = dev_to_mtd(dev);
130 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
133 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
135 static ssize_t mtd_subpagesize_show(struct device *dev,
136 struct device_attribute *attr, char *buf)
138 struct mtd_info *mtd = dev_to_mtd(dev);
139 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
141 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
144 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
146 static ssize_t mtd_oobsize_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
149 struct mtd_info *mtd = dev_to_mtd(dev);
151 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
154 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
156 static ssize_t mtd_numeraseregions_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
159 struct mtd_info *mtd = dev_to_mtd(dev);
161 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
164 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
167 static ssize_t mtd_name_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
170 struct mtd_info *mtd = dev_to_mtd(dev);
172 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
175 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
177 static struct attribute *mtd_attrs[] = {
179 &dev_attr_flags.attr,
181 &dev_attr_erasesize.attr,
182 &dev_attr_writesize.attr,
183 &dev_attr_subpagesize.attr,
184 &dev_attr_oobsize.attr,
185 &dev_attr_numeraseregions.attr,
190 struct attribute_group mtd_group = {
194 struct attribute_group *mtd_groups[] = {
199 static struct device_type mtd_devtype = {
201 .groups = mtd_groups,
202 .release = mtd_release,
206 * add_mtd_device - register an MTD device
207 * @mtd: pointer to new MTD device info structure
209 * Add a device to the list of MTD devices present in the system, and
210 * notify each currently active MTD 'user' of its arrival. Returns
211 * zero on success or 1 on failure, which currently will only happen
212 * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
213 * or there's a sysfs error.
216 int add_mtd_device(struct mtd_info *mtd)
220 if (!mtd->backing_dev_info) {
223 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
226 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
229 mtd->backing_dev_info = &mtd_bdi_unmappable;
234 BUG_ON(mtd->writesize == 0);
235 mutex_lock(&mtd_table_mutex);
237 for (i=0; i < MAX_MTD_DEVICES; i++)
239 struct mtd_notifier *not;
245 if (is_power_of_2(mtd->erasesize))
246 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
248 mtd->erasesize_shift = 0;
250 if (is_power_of_2(mtd->writesize))
251 mtd->writesize_shift = ffs(mtd->writesize) - 1;
253 mtd->writesize_shift = 0;
255 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
256 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
258 /* Some chips always power up locked. Unlock them now */
259 if ((mtd->flags & MTD_WRITEABLE)
260 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
261 if (mtd->unlock(mtd, 0, mtd->size))
263 "%s: unlock failed, "
264 "writes may not work\n",
268 /* Caller should have set dev.parent to match the
271 mtd->dev.type = &mtd_devtype;
272 mtd->dev.class = mtd_class;
273 mtd->dev.devt = MTD_DEVT(i);
274 dev_set_name(&mtd->dev, "mtd%d", i);
275 if (device_register(&mtd->dev) != 0) {
281 device_create(mtd_class, mtd->dev.parent,
285 DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
286 /* No need to get a refcount on the module containing
287 the notifier, since we hold the mtd_table_mutex */
288 list_for_each_entry(not, &mtd_notifiers, list)
291 mutex_unlock(&mtd_table_mutex);
292 /* We _know_ we aren't being removed, because
293 our caller is still holding us here. So none
294 of this try_ nonsense, and no bitching about it
296 __module_get(THIS_MODULE);
300 mutex_unlock(&mtd_table_mutex);
305 * del_mtd_device - unregister an MTD device
306 * @mtd: pointer to MTD device info structure
308 * Remove a device from the list of MTD devices present in the system,
309 * and notify each currently active MTD 'user' of its departure.
310 * Returns zero on success or 1 on failure, which currently will happen
311 * if the requested device does not appear to be present in the list.
314 int del_mtd_device (struct mtd_info *mtd)
318 mutex_lock(&mtd_table_mutex);
320 if (mtd_table[mtd->index] != mtd) {
322 } else if (mtd->usecount) {
323 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
324 mtd->index, mtd->name, mtd->usecount);
327 struct mtd_notifier *not;
329 device_unregister(&mtd->dev);
331 /* No need to get a refcount on the module containing
332 the notifier, since we hold the mtd_table_mutex */
333 list_for_each_entry(not, &mtd_notifiers, list)
336 mtd_table[mtd->index] = NULL;
338 module_put(THIS_MODULE);
342 mutex_unlock(&mtd_table_mutex);
347 * register_mtd_user - register a 'user' of MTD devices.
348 * @new: pointer to notifier info structure
350 * Registers a pair of callbacks function to be called upon addition
351 * or removal of MTD devices. Causes the 'add' callback to be immediately
352 * invoked for each MTD device currently present in the system.
355 void register_mtd_user (struct mtd_notifier *new)
359 mutex_lock(&mtd_table_mutex);
361 list_add(&new->list, &mtd_notifiers);
363 __module_get(THIS_MODULE);
365 for (i=0; i< MAX_MTD_DEVICES; i++)
367 new->add(mtd_table[i]);
369 mutex_unlock(&mtd_table_mutex);
373 * unregister_mtd_user - unregister a 'user' of MTD devices.
374 * @old: pointer to notifier info structure
376 * Removes a callback function pair from the list of 'users' to be
377 * notified upon addition or removal of MTD devices. Causes the
378 * 'remove' callback to be immediately invoked for each MTD device
379 * currently present in the system.
382 int unregister_mtd_user (struct mtd_notifier *old)
386 mutex_lock(&mtd_table_mutex);
388 module_put(THIS_MODULE);
390 for (i=0; i< MAX_MTD_DEVICES; i++)
392 old->remove(mtd_table[i]);
394 list_del(&old->list);
395 mutex_unlock(&mtd_table_mutex);
401 * get_mtd_device - obtain a validated handle for an MTD device
402 * @mtd: last known address of the required MTD device
403 * @num: internal device number of the required MTD device
405 * Given a number and NULL address, return the num'th entry in the device
406 * table, if any. Given an address and num == -1, search the device table
407 * for a device with that address and return if it's still present. Given
408 * both, return the num'th driver only if its address matches. Return
412 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
414 struct mtd_info *ret = NULL;
415 int i, err = -ENODEV;
417 mutex_lock(&mtd_table_mutex);
420 for (i=0; i< MAX_MTD_DEVICES; i++)
421 if (mtd_table[i] == mtd)
423 } else if (num < MAX_MTD_DEVICES) {
424 ret = mtd_table[num];
425 if (mtd && mtd != ret)
432 if (!try_module_get(ret->owner))
435 if (ret->get_device) {
436 err = ret->get_device(ret);
442 mutex_unlock(&mtd_table_mutex);
446 module_put(ret->owner);
448 mutex_unlock(&mtd_table_mutex);
453 * get_mtd_device_nm - obtain a validated handle for an MTD device by
455 * @name: MTD device name to open
457 * This function returns MTD device description structure in case of
458 * success and an error code in case of failure.
461 struct mtd_info *get_mtd_device_nm(const char *name)
463 int i, err = -ENODEV;
464 struct mtd_info *mtd = NULL;
466 mutex_lock(&mtd_table_mutex);
468 for (i = 0; i < MAX_MTD_DEVICES; i++) {
469 if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
478 if (!try_module_get(mtd->owner))
481 if (mtd->get_device) {
482 err = mtd->get_device(mtd);
488 mutex_unlock(&mtd_table_mutex);
492 module_put(mtd->owner);
494 mutex_unlock(&mtd_table_mutex);
498 void put_mtd_device(struct mtd_info *mtd)
502 mutex_lock(&mtd_table_mutex);
505 mtd->put_device(mtd);
506 mutex_unlock(&mtd_table_mutex);
509 module_put(mtd->owner);
512 /* default_mtd_writev - default mtd writev method for MTD devices that
513 * don't implement their own
516 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
517 unsigned long count, loff_t to, size_t *retlen)
520 size_t totlen = 0, thislen;
526 for (i=0; i<count; i++) {
527 if (!vecs[i].iov_len)
529 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
531 if (ret || thislen != vecs[i].iov_len)
533 to += vecs[i].iov_len;
541 EXPORT_SYMBOL_GPL(add_mtd_device);
542 EXPORT_SYMBOL_GPL(del_mtd_device);
543 EXPORT_SYMBOL_GPL(get_mtd_device);
544 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
545 EXPORT_SYMBOL_GPL(put_mtd_device);
546 EXPORT_SYMBOL_GPL(register_mtd_user);
547 EXPORT_SYMBOL_GPL(unregister_mtd_user);
548 EXPORT_SYMBOL_GPL(default_mtd_writev);
550 #ifdef CONFIG_PROC_FS
552 /*====================================================================*/
553 /* Support for /proc/mtd */
555 static struct proc_dir_entry *proc_mtd;
557 static inline int mtd_proc_info (char *buf, int i)
559 struct mtd_info *this = mtd_table[i];
564 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
565 (unsigned long long)this->size,
566 this->erasesize, this->name);
569 static int mtd_read_proc (char *page, char **start, off_t off, int count,
570 int *eof, void *data_unused)
575 mutex_lock(&mtd_table_mutex);
577 len = sprintf(page, "dev: size erasesize name\n");
578 for (i=0; i< MAX_MTD_DEVICES; i++) {
580 l = mtd_proc_info(page + len, i);
582 if (len+begin > off+count)
584 if (len+begin < off) {
593 mutex_unlock(&mtd_table_mutex);
594 if (off >= len+begin)
596 *start = page + (off-begin);
597 return ((count < begin+len-off) ? count : begin+len-off);
600 #endif /* CONFIG_PROC_FS */
602 /*====================================================================*/
605 static int __init init_mtd(void)
607 mtd_class = class_create(THIS_MODULE, "mtd");
609 if (IS_ERR(mtd_class)) {
610 pr_err("Error creating mtd class.\n");
611 return PTR_ERR(mtd_class);
613 #ifdef CONFIG_PROC_FS
614 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
615 proc_mtd->read_proc = mtd_read_proc;
616 #endif /* CONFIG_PROC_FS */
620 static void __exit cleanup_mtd(void)
622 #ifdef CONFIG_PROC_FS
624 remove_proc_entry( "mtd", NULL);
625 #endif /* CONFIG_PROC_FS */
626 class_destroy(mtd_class);
629 module_init(init_mtd);
630 module_exit(cleanup_mtd);
632 MODULE_LICENSE("GPL");
633 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
634 MODULE_DESCRIPTION("Core MTD registration and access routines");