mtd: call the remove notifiers before assuming it is in use
[deliverable/linux.git] / drivers / mtd / mtdcore.c
1 /*
2 * Core registration and callback routines for MTD
3 * drivers and users.
4 *
5 */
6
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>
20 #include <linux/idr.h>
21
22 #include <linux/mtd/mtd.h>
23 #include "internal.h"
24
25 #include "mtdcore.h"
26
27 static int mtd_cls_suspend(struct device *dev, pm_message_t state);
28 static int mtd_cls_resume(struct device *dev);
29
30 static struct class mtd_class = {
31 .name = "mtd",
32 .owner = THIS_MODULE,
33 .suspend = mtd_cls_suspend,
34 .resume = mtd_cls_resume,
35 };
36
37 static DEFINE_IDR(mtd_idr);
38
39 /* These are exported solely for the purpose of mtd_blkdevs.c. You
40 should not use them for _anything_ else */
41 DEFINE_MUTEX(mtd_table_mutex);
42 EXPORT_SYMBOL_GPL(mtd_table_mutex);
43
44 struct mtd_info *__mtd_next_device(int i)
45 {
46 return idr_get_next(&mtd_idr, &i);
47 }
48 EXPORT_SYMBOL_GPL(__mtd_next_device);
49
50 static LIST_HEAD(mtd_notifiers);
51
52
53 #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
54 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
55 #else
56 #define MTD_DEVT(index) 0
57 #endif
58
59 /* REVISIT once MTD uses the driver model better, whoever allocates
60 * the mtd_info will probably want to use the release() hook...
61 */
62 static void mtd_release(struct device *dev)
63 {
64 dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
65
66 /* remove /dev/mtdXro node if needed */
67 if (index)
68 device_destroy(&mtd_class, index + 1);
69 }
70
71 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
72 {
73 struct mtd_info *mtd = dev_to_mtd(dev);
74
75 if (mtd && mtd->suspend)
76 return mtd->suspend(mtd);
77 else
78 return 0;
79 }
80
81 static int mtd_cls_resume(struct device *dev)
82 {
83 struct mtd_info *mtd = dev_to_mtd(dev);
84
85 if (mtd && mtd->resume)
86 mtd->resume(mtd);
87 return 0;
88 }
89
90 static ssize_t mtd_type_show(struct device *dev,
91 struct device_attribute *attr, char *buf)
92 {
93 struct mtd_info *mtd = dev_to_mtd(dev);
94 char *type;
95
96 switch (mtd->type) {
97 case MTD_ABSENT:
98 type = "absent";
99 break;
100 case MTD_RAM:
101 type = "ram";
102 break;
103 case MTD_ROM:
104 type = "rom";
105 break;
106 case MTD_NORFLASH:
107 type = "nor";
108 break;
109 case MTD_NANDFLASH:
110 type = "nand";
111 break;
112 case MTD_DATAFLASH:
113 type = "dataflash";
114 break;
115 case MTD_UBIVOLUME:
116 type = "ubi";
117 break;
118 default:
119 type = "unknown";
120 }
121
122 return snprintf(buf, PAGE_SIZE, "%s\n", type);
123 }
124 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
125
126 static ssize_t mtd_flags_show(struct device *dev,
127 struct device_attribute *attr, char *buf)
128 {
129 struct mtd_info *mtd = dev_to_mtd(dev);
130
131 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
132
133 }
134 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
135
136 static ssize_t mtd_size_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
138 {
139 struct mtd_info *mtd = dev_to_mtd(dev);
140
141 return snprintf(buf, PAGE_SIZE, "%llu\n",
142 (unsigned long long)mtd->size);
143
144 }
145 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
146
147 static ssize_t mtd_erasesize_show(struct device *dev,
148 struct device_attribute *attr, char *buf)
149 {
150 struct mtd_info *mtd = dev_to_mtd(dev);
151
152 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
153
154 }
155 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
156
157 static ssize_t mtd_writesize_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
159 {
160 struct mtd_info *mtd = dev_to_mtd(dev);
161
162 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
163
164 }
165 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
166
167 static ssize_t mtd_subpagesize_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
169 {
170 struct mtd_info *mtd = dev_to_mtd(dev);
171 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
172
173 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
174
175 }
176 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
177
178 static ssize_t mtd_oobsize_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
180 {
181 struct mtd_info *mtd = dev_to_mtd(dev);
182
183 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
184
185 }
186 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
187
188 static ssize_t mtd_numeraseregions_show(struct device *dev,
189 struct device_attribute *attr, char *buf)
190 {
191 struct mtd_info *mtd = dev_to_mtd(dev);
192
193 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
194
195 }
196 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
197 NULL);
198
199 static ssize_t mtd_name_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
201 {
202 struct mtd_info *mtd = dev_to_mtd(dev);
203
204 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
205
206 }
207 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
208
209 static struct attribute *mtd_attrs[] = {
210 &dev_attr_type.attr,
211 &dev_attr_flags.attr,
212 &dev_attr_size.attr,
213 &dev_attr_erasesize.attr,
214 &dev_attr_writesize.attr,
215 &dev_attr_subpagesize.attr,
216 &dev_attr_oobsize.attr,
217 &dev_attr_numeraseregions.attr,
218 &dev_attr_name.attr,
219 NULL,
220 };
221
222 static struct attribute_group mtd_group = {
223 .attrs = mtd_attrs,
224 };
225
226 static const struct attribute_group *mtd_groups[] = {
227 &mtd_group,
228 NULL,
229 };
230
231 static struct device_type mtd_devtype = {
232 .name = "mtd",
233 .groups = mtd_groups,
234 .release = mtd_release,
235 };
236
237 /**
238 * add_mtd_device - register an MTD device
239 * @mtd: pointer to new MTD device info structure
240 *
241 * Add a device to the list of MTD devices present in the system, and
242 * notify each currently active MTD 'user' of its arrival. Returns
243 * zero on success or 1 on failure, which currently will only happen
244 * if there is insufficient memory or a sysfs error.
245 */
246
247 int add_mtd_device(struct mtd_info *mtd)
248 {
249 struct mtd_notifier *not;
250 int i, error;
251
252 if (!mtd->backing_dev_info) {
253 switch (mtd->type) {
254 case MTD_RAM:
255 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
256 break;
257 case MTD_ROM:
258 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
259 break;
260 default:
261 mtd->backing_dev_info = &mtd_bdi_unmappable;
262 break;
263 }
264 }
265
266 BUG_ON(mtd->writesize == 0);
267 mutex_lock(&mtd_table_mutex);
268
269 do {
270 if (!idr_pre_get(&mtd_idr, GFP_KERNEL))
271 goto fail_locked;
272 error = idr_get_new(&mtd_idr, mtd, &i);
273 } while (error == -EAGAIN);
274
275 if (error)
276 goto fail_locked;
277
278 mtd->index = i;
279 mtd->usecount = 0;
280
281 if (is_power_of_2(mtd->erasesize))
282 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
283 else
284 mtd->erasesize_shift = 0;
285
286 if (is_power_of_2(mtd->writesize))
287 mtd->writesize_shift = ffs(mtd->writesize) - 1;
288 else
289 mtd->writesize_shift = 0;
290
291 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
292 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
293
294 /* Some chips always power up locked. Unlock them now */
295 if ((mtd->flags & MTD_WRITEABLE)
296 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
297 if (mtd->unlock(mtd, 0, mtd->size))
298 printk(KERN_WARNING
299 "%s: unlock failed, writes may not work\n",
300 mtd->name);
301 }
302
303 /* Caller should have set dev.parent to match the
304 * physical device.
305 */
306 mtd->dev.type = &mtd_devtype;
307 mtd->dev.class = &mtd_class;
308 mtd->dev.devt = MTD_DEVT(i);
309 dev_set_name(&mtd->dev, "mtd%d", i);
310 dev_set_drvdata(&mtd->dev, mtd);
311 if (device_register(&mtd->dev) != 0)
312 goto fail_added;
313
314 if (MTD_DEVT(i))
315 device_create(&mtd_class, mtd->dev.parent,
316 MTD_DEVT(i) + 1,
317 NULL, "mtd%dro", i);
318
319 DEBUG(0, "mtd: Giving out device %d to %s\n", i, mtd->name);
320 /* No need to get a refcount on the module containing
321 the notifier, since we hold the mtd_table_mutex */
322 list_for_each_entry(not, &mtd_notifiers, list)
323 not->add(mtd);
324
325 mutex_unlock(&mtd_table_mutex);
326 /* We _know_ we aren't being removed, because
327 our caller is still holding us here. So none
328 of this try_ nonsense, and no bitching about it
329 either. :) */
330 __module_get(THIS_MODULE);
331 return 0;
332
333 fail_added:
334 idr_remove(&mtd_idr, i);
335 fail_locked:
336 mutex_unlock(&mtd_table_mutex);
337 return 1;
338 }
339
340 /**
341 * del_mtd_device - unregister an MTD device
342 * @mtd: pointer to MTD device info structure
343 *
344 * Remove a device from the list of MTD devices present in the system,
345 * and notify each currently active MTD 'user' of its departure.
346 * Returns zero on success or 1 on failure, which currently will happen
347 * if the requested device does not appear to be present in the list.
348 */
349
350 int del_mtd_device (struct mtd_info *mtd)
351 {
352 int ret;
353 struct mtd_notifier *not;
354
355 mutex_lock(&mtd_table_mutex);
356
357 if (idr_find(&mtd_idr, mtd->index) != mtd) {
358 ret = -ENODEV;
359 goto out_error;
360 }
361
362 /* No need to get a refcount on the module containing
363 the notifier, since we hold the mtd_table_mutex */
364 list_for_each_entry(not, &mtd_notifiers, list)
365 not->remove(mtd);
366
367 if (mtd->usecount) {
368 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
369 mtd->index, mtd->name, mtd->usecount);
370 ret = -EBUSY;
371 } else {
372 device_unregister(&mtd->dev);
373
374 idr_remove(&mtd_idr, mtd->index);
375
376 module_put(THIS_MODULE);
377 ret = 0;
378 }
379
380 out_error:
381 mutex_unlock(&mtd_table_mutex);
382 return ret;
383 }
384
385 /**
386 * register_mtd_user - register a 'user' of MTD devices.
387 * @new: pointer to notifier info structure
388 *
389 * Registers a pair of callbacks function to be called upon addition
390 * or removal of MTD devices. Causes the 'add' callback to be immediately
391 * invoked for each MTD device currently present in the system.
392 */
393
394 void register_mtd_user (struct mtd_notifier *new)
395 {
396 struct mtd_info *mtd;
397
398 mutex_lock(&mtd_table_mutex);
399
400 list_add(&new->list, &mtd_notifiers);
401
402 __module_get(THIS_MODULE);
403
404 mtd_for_each_device(mtd)
405 new->add(mtd);
406
407 mutex_unlock(&mtd_table_mutex);
408 }
409
410 /**
411 * unregister_mtd_user - unregister a 'user' of MTD devices.
412 * @old: pointer to notifier info structure
413 *
414 * Removes a callback function pair from the list of 'users' to be
415 * notified upon addition or removal of MTD devices. Causes the
416 * 'remove' callback to be immediately invoked for each MTD device
417 * currently present in the system.
418 */
419
420 int unregister_mtd_user (struct mtd_notifier *old)
421 {
422 struct mtd_info *mtd;
423
424 mutex_lock(&mtd_table_mutex);
425
426 module_put(THIS_MODULE);
427
428 mtd_for_each_device(mtd)
429 old->remove(mtd);
430
431 list_del(&old->list);
432 mutex_unlock(&mtd_table_mutex);
433 return 0;
434 }
435
436
437 /**
438 * get_mtd_device - obtain a validated handle for an MTD device
439 * @mtd: last known address of the required MTD device
440 * @num: internal device number of the required MTD device
441 *
442 * Given a number and NULL address, return the num'th entry in the device
443 * table, if any. Given an address and num == -1, search the device table
444 * for a device with that address and return if it's still present. Given
445 * both, return the num'th driver only if its address matches. Return
446 * error code if not.
447 */
448
449 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
450 {
451 struct mtd_info *ret = NULL, *other;
452 int err = -ENODEV;
453
454 mutex_lock(&mtd_table_mutex);
455
456 if (num == -1) {
457 mtd_for_each_device(other) {
458 if (other == mtd) {
459 ret = mtd;
460 break;
461 }
462 }
463 } else if (num >= 0) {
464 ret = idr_find(&mtd_idr, num);
465 if (mtd && mtd != ret)
466 ret = NULL;
467 }
468
469 if (!ret) {
470 ret = ERR_PTR(err);
471 goto out;
472 }
473
474 err = __get_mtd_device(ret);
475 if (err)
476 ret = ERR_PTR(err);
477 out:
478 mutex_unlock(&mtd_table_mutex);
479 return ret;
480 }
481
482
483 int __get_mtd_device(struct mtd_info *mtd)
484 {
485 int err;
486
487 if (!try_module_get(mtd->owner))
488 return -ENODEV;
489
490 if (mtd->get_device) {
491
492 err = mtd->get_device(mtd);
493
494 if (err) {
495 module_put(mtd->owner);
496 return err;
497 }
498 }
499 mtd->usecount++;
500 return 0;
501 }
502
503 /**
504 * get_mtd_device_nm - obtain a validated handle for an MTD device by
505 * device name
506 * @name: MTD device name to open
507 *
508 * This function returns MTD device description structure in case of
509 * success and an error code in case of failure.
510 */
511
512 struct mtd_info *get_mtd_device_nm(const char *name)
513 {
514 int err = -ENODEV;
515 struct mtd_info *mtd = NULL, *other;
516
517 mutex_lock(&mtd_table_mutex);
518
519 mtd_for_each_device(other) {
520 if (!strcmp(name, other->name)) {
521 mtd = other;
522 break;
523 }
524 }
525
526 if (!mtd)
527 goto out_unlock;
528
529 if (!try_module_get(mtd->owner))
530 goto out_unlock;
531
532 if (mtd->get_device) {
533 err = mtd->get_device(mtd);
534 if (err)
535 goto out_put;
536 }
537
538 mtd->usecount++;
539 mutex_unlock(&mtd_table_mutex);
540 return mtd;
541
542 out_put:
543 module_put(mtd->owner);
544 out_unlock:
545 mutex_unlock(&mtd_table_mutex);
546 return ERR_PTR(err);
547 }
548
549 void put_mtd_device(struct mtd_info *mtd)
550 {
551 mutex_lock(&mtd_table_mutex);
552 __put_mtd_device(mtd);
553 mutex_unlock(&mtd_table_mutex);
554
555 }
556
557 void __put_mtd_device(struct mtd_info *mtd)
558 {
559 --mtd->usecount;
560 BUG_ON(mtd->usecount < 0);
561
562 if (mtd->put_device)
563 mtd->put_device(mtd);
564
565 module_put(mtd->owner);
566 }
567
568 /* default_mtd_writev - default mtd writev method for MTD devices that
569 * don't implement their own
570 */
571
572 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
573 unsigned long count, loff_t to, size_t *retlen)
574 {
575 unsigned long i;
576 size_t totlen = 0, thislen;
577 int ret = 0;
578
579 if(!mtd->write) {
580 ret = -EROFS;
581 } else {
582 for (i=0; i<count; i++) {
583 if (!vecs[i].iov_len)
584 continue;
585 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
586 totlen += thislen;
587 if (ret || thislen != vecs[i].iov_len)
588 break;
589 to += vecs[i].iov_len;
590 }
591 }
592 if (retlen)
593 *retlen = totlen;
594 return ret;
595 }
596
597 EXPORT_SYMBOL_GPL(add_mtd_device);
598 EXPORT_SYMBOL_GPL(del_mtd_device);
599 EXPORT_SYMBOL_GPL(get_mtd_device);
600 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
601 EXPORT_SYMBOL_GPL(__get_mtd_device);
602 EXPORT_SYMBOL_GPL(put_mtd_device);
603 EXPORT_SYMBOL_GPL(__put_mtd_device);
604 EXPORT_SYMBOL_GPL(register_mtd_user);
605 EXPORT_SYMBOL_GPL(unregister_mtd_user);
606 EXPORT_SYMBOL_GPL(default_mtd_writev);
607
608 #ifdef CONFIG_PROC_FS
609
610 /*====================================================================*/
611 /* Support for /proc/mtd */
612
613 static struct proc_dir_entry *proc_mtd;
614
615 static inline int mtd_proc_info(char *buf, struct mtd_info *this)
616 {
617 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
618 (unsigned long long)this->size,
619 this->erasesize, this->name);
620 }
621
622 static int mtd_read_proc (char *page, char **start, off_t off, int count,
623 int *eof, void *data_unused)
624 {
625 struct mtd_info *mtd;
626 int len, l;
627 off_t begin = 0;
628
629 mutex_lock(&mtd_table_mutex);
630
631 len = sprintf(page, "dev: size erasesize name\n");
632 mtd_for_each_device(mtd) {
633 l = mtd_proc_info(page + len, mtd);
634 len += l;
635 if (len+begin > off+count)
636 goto done;
637 if (len+begin < off) {
638 begin += len;
639 len = 0;
640 }
641 }
642
643 *eof = 1;
644
645 done:
646 mutex_unlock(&mtd_table_mutex);
647 if (off >= len+begin)
648 return 0;
649 *start = page + (off-begin);
650 return ((count < begin+len-off) ? count : begin+len-off);
651 }
652
653 #endif /* CONFIG_PROC_FS */
654
655 /*====================================================================*/
656 /* Init code */
657
658 static int __init init_mtd(void)
659 {
660 int ret;
661 ret = class_register(&mtd_class);
662
663 if (ret) {
664 pr_err("Error registering mtd class: %d\n", ret);
665 return ret;
666 }
667 #ifdef CONFIG_PROC_FS
668 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
669 proc_mtd->read_proc = mtd_read_proc;
670 #endif /* CONFIG_PROC_FS */
671 return 0;
672 }
673
674 static void __exit cleanup_mtd(void)
675 {
676 #ifdef CONFIG_PROC_FS
677 if (proc_mtd)
678 remove_proc_entry( "mtd", NULL);
679 #endif /* CONFIG_PROC_FS */
680 class_unregister(&mtd_class);
681 }
682
683 module_init(init_mtd);
684 module_exit(cleanup_mtd);
685
686 MODULE_LICENSE("GPL");
687 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
688 MODULE_DESCRIPTION("Core MTD registration and access routines");
This page took 0.053367 seconds and 5 git commands to generate.