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