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 struct mtd_info *mtd = dev_to_mtd(dev);
53 /* remove /dev/mtdXro node if needed */
54 if (MTD_DEVT(mtd->index))
55 device_destroy(mtd_class, MTD_DEVT(mtd->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(mtd_type, S_IRUGO, mtd_type_show, NULL);
94 static struct attribute *mtd_attrs[] = {
95 &dev_attr_mtd_type.attr,
96 /* FIXME provide a /proc/mtd superset */
100 struct attribute_group mtd_group = {
104 struct attribute_group *mtd_groups[] = {
109 static struct device_type mtd_devtype = {
111 .groups = mtd_groups,
112 .release = mtd_release,
116 * add_mtd_device - register an MTD device
117 * @mtd: pointer to new MTD device info structure
119 * Add a device to the list of MTD devices present in the system, and
120 * notify each currently active MTD 'user' of its arrival. Returns
121 * zero on success or 1 on failure, which currently will only happen
122 * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
123 * or there's a sysfs error.
126 int add_mtd_device(struct mtd_info *mtd)
130 if (!mtd->backing_dev_info) {
133 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
136 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
139 mtd->backing_dev_info = &mtd_bdi_unmappable;
144 BUG_ON(mtd->writesize == 0);
145 mutex_lock(&mtd_table_mutex);
147 for (i=0; i < MAX_MTD_DEVICES; i++)
149 struct mtd_notifier *not;
155 if (is_power_of_2(mtd->erasesize))
156 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
158 mtd->erasesize_shift = 0;
160 if (is_power_of_2(mtd->writesize))
161 mtd->writesize_shift = ffs(mtd->writesize) - 1;
163 mtd->writesize_shift = 0;
165 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
166 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
168 /* Some chips always power up locked. Unlock them now */
169 if ((mtd->flags & MTD_WRITEABLE)
170 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
171 if (mtd->unlock(mtd, 0, mtd->size))
173 "%s: unlock failed, "
174 "writes may not work\n",
178 /* Caller should have set dev.parent to match the
181 mtd->dev.type = &mtd_devtype;
182 mtd->dev.class = mtd_class;
183 mtd->dev.devt = MTD_DEVT(i);
184 dev_set_name(&mtd->dev, "mtd%d", i);
185 if (device_register(&mtd->dev) != 0) {
191 device_create(mtd_class, mtd->dev.parent,
195 DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
196 /* No need to get a refcount on the module containing
197 the notifier, since we hold the mtd_table_mutex */
198 list_for_each_entry(not, &mtd_notifiers, list)
201 mutex_unlock(&mtd_table_mutex);
202 /* We _know_ we aren't being removed, because
203 our caller is still holding us here. So none
204 of this try_ nonsense, and no bitching about it
206 __module_get(THIS_MODULE);
210 mutex_unlock(&mtd_table_mutex);
215 * del_mtd_device - unregister an MTD device
216 * @mtd: pointer to MTD device info structure
218 * Remove a device from the list of MTD devices present in the system,
219 * and notify each currently active MTD 'user' of its departure.
220 * Returns zero on success or 1 on failure, which currently will happen
221 * if the requested device does not appear to be present in the list.
224 int del_mtd_device (struct mtd_info *mtd)
228 mutex_lock(&mtd_table_mutex);
230 if (mtd_table[mtd->index] != mtd) {
232 } else if (mtd->usecount) {
233 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
234 mtd->index, mtd->name, mtd->usecount);
237 struct mtd_notifier *not;
239 /* No need to get a refcount on the module containing
240 the notifier, since we hold the mtd_table_mutex */
241 list_for_each_entry(not, &mtd_notifiers, list)
244 mtd_table[mtd->index] = NULL;
246 module_put(THIS_MODULE);
250 mutex_unlock(&mtd_table_mutex);
255 * register_mtd_user - register a 'user' of MTD devices.
256 * @new: pointer to notifier info structure
258 * Registers a pair of callbacks function to be called upon addition
259 * or removal of MTD devices. Causes the 'add' callback to be immediately
260 * invoked for each MTD device currently present in the system.
263 void register_mtd_user (struct mtd_notifier *new)
267 mutex_lock(&mtd_table_mutex);
269 list_add(&new->list, &mtd_notifiers);
271 __module_get(THIS_MODULE);
273 for (i=0; i< MAX_MTD_DEVICES; i++)
275 new->add(mtd_table[i]);
277 mutex_unlock(&mtd_table_mutex);
281 * unregister_mtd_user - unregister a 'user' of MTD devices.
282 * @old: pointer to notifier info structure
284 * Removes a callback function pair from the list of 'users' to be
285 * notified upon addition or removal of MTD devices. Causes the
286 * 'remove' callback to be immediately invoked for each MTD device
287 * currently present in the system.
290 int unregister_mtd_user (struct mtd_notifier *old)
294 mutex_lock(&mtd_table_mutex);
296 module_put(THIS_MODULE);
298 for (i=0; i< MAX_MTD_DEVICES; i++)
300 old->remove(mtd_table[i]);
302 list_del(&old->list);
303 mutex_unlock(&mtd_table_mutex);
309 * get_mtd_device - obtain a validated handle for an MTD device
310 * @mtd: last known address of the required MTD device
311 * @num: internal device number of the required MTD device
313 * Given a number and NULL address, return the num'th entry in the device
314 * table, if any. Given an address and num == -1, search the device table
315 * for a device with that address and return if it's still present. Given
316 * both, return the num'th driver only if its address matches. Return
320 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
322 struct mtd_info *ret = NULL;
323 int i, err = -ENODEV;
325 mutex_lock(&mtd_table_mutex);
328 for (i=0; i< MAX_MTD_DEVICES; i++)
329 if (mtd_table[i] == mtd)
331 } else if (num < MAX_MTD_DEVICES) {
332 ret = mtd_table[num];
333 if (mtd && mtd != ret)
340 if (!try_module_get(ret->owner))
343 if (ret->get_device) {
344 err = ret->get_device(ret);
350 mutex_unlock(&mtd_table_mutex);
354 module_put(ret->owner);
356 mutex_unlock(&mtd_table_mutex);
361 * get_mtd_device_nm - obtain a validated handle for an MTD device by
363 * @name: MTD device name to open
365 * This function returns MTD device description structure in case of
366 * success and an error code in case of failure.
369 struct mtd_info *get_mtd_device_nm(const char *name)
371 int i, err = -ENODEV;
372 struct mtd_info *mtd = NULL;
374 mutex_lock(&mtd_table_mutex);
376 for (i = 0; i < MAX_MTD_DEVICES; i++) {
377 if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
386 if (!try_module_get(mtd->owner))
389 if (mtd->get_device) {
390 err = mtd->get_device(mtd);
396 mutex_unlock(&mtd_table_mutex);
400 module_put(mtd->owner);
402 mutex_unlock(&mtd_table_mutex);
406 void put_mtd_device(struct mtd_info *mtd)
410 mutex_lock(&mtd_table_mutex);
413 mtd->put_device(mtd);
414 mutex_unlock(&mtd_table_mutex);
417 module_put(mtd->owner);
420 /* default_mtd_writev - default mtd writev method for MTD devices that
421 * don't implement their own
424 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
425 unsigned long count, loff_t to, size_t *retlen)
428 size_t totlen = 0, thislen;
434 for (i=0; i<count; i++) {
435 if (!vecs[i].iov_len)
437 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
439 if (ret || thislen != vecs[i].iov_len)
441 to += vecs[i].iov_len;
449 EXPORT_SYMBOL_GPL(add_mtd_device);
450 EXPORT_SYMBOL_GPL(del_mtd_device);
451 EXPORT_SYMBOL_GPL(get_mtd_device);
452 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
453 EXPORT_SYMBOL_GPL(put_mtd_device);
454 EXPORT_SYMBOL_GPL(register_mtd_user);
455 EXPORT_SYMBOL_GPL(unregister_mtd_user);
456 EXPORT_SYMBOL_GPL(default_mtd_writev);
458 static int __init mtd_setup(void)
460 mtd_class = class_create(THIS_MODULE, "mtd");
462 if (IS_ERR(mtd_class)) {
463 pr_err("Error creating mtd class.\n");
464 return PTR_ERR(mtd_class);
468 core_initcall(mtd_setup);
470 static void __exit mtd_teardown(void)
472 class_destroy(mtd_class);
474 __exitcall(mtd_teardown);
476 #ifdef CONFIG_PROC_FS
478 /*====================================================================*/
479 /* Support for /proc/mtd */
481 static struct proc_dir_entry *proc_mtd;
483 static inline int mtd_proc_info (char *buf, int i)
485 struct mtd_info *this = mtd_table[i];
490 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
491 (unsigned long long)this->size,
492 this->erasesize, this->name);
495 static int mtd_read_proc (char *page, char **start, off_t off, int count,
496 int *eof, void *data_unused)
501 mutex_lock(&mtd_table_mutex);
503 len = sprintf(page, "dev: size erasesize name\n");
504 for (i=0; i< MAX_MTD_DEVICES; i++) {
506 l = mtd_proc_info(page + len, i);
508 if (len+begin > off+count)
510 if (len+begin < off) {
519 mutex_unlock(&mtd_table_mutex);
520 if (off >= len+begin)
522 *start = page + (off-begin);
523 return ((count < begin+len-off) ? count : begin+len-off);
526 /*====================================================================*/
529 static int __init init_mtd(void)
531 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
532 proc_mtd->read_proc = mtd_read_proc;
536 static void __exit cleanup_mtd(void)
539 remove_proc_entry( "mtd", NULL);
542 module_init(init_mtd);
543 module_exit(cleanup_mtd);
545 #endif /* CONFIG_PROC_FS */
548 MODULE_LICENSE("GPL");
549 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
550 MODULE_DESCRIPTION("Core MTD registration and access routines");