xref: /openbmc/linux/drivers/mtd/mtdcore.c (revision b04b4f78)
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 
27 static struct class *mtd_class;
28 
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];
33 
34 EXPORT_SYMBOL_GPL(mtd_table_mutex);
35 EXPORT_SYMBOL_GPL(mtd_table);
36 
37 static LIST_HEAD(mtd_notifiers);
38 
39 
40 #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
41 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
42 #else
43 #define MTD_DEVT(index) 0
44 #endif
45 
46 /* REVISIT once MTD uses the driver model better, whoever allocates
47  * the mtd_info will probably want to use the release() hook...
48  */
49 static void mtd_release(struct device *dev)
50 {
51 	dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
52 
53 	/* remove /dev/mtdXro node if needed */
54 	if (index)
55 		device_destroy(mtd_class, index + 1);
56 }
57 
58 static ssize_t mtd_type_show(struct device *dev,
59 		struct device_attribute *attr, char *buf)
60 {
61 	struct mtd_info *mtd = dev_to_mtd(dev);
62 	char *type;
63 
64 	switch (mtd->type) {
65 	case MTD_ABSENT:
66 		type = "absent";
67 		break;
68 	case MTD_RAM:
69 		type = "ram";
70 		break;
71 	case MTD_ROM:
72 		type = "rom";
73 		break;
74 	case MTD_NORFLASH:
75 		type = "nor";
76 		break;
77 	case MTD_NANDFLASH:
78 		type = "nand";
79 		break;
80 	case MTD_DATAFLASH:
81 		type = "dataflash";
82 		break;
83 	case MTD_UBIVOLUME:
84 		type = "ubi";
85 		break;
86 	default:
87 		type = "unknown";
88 	}
89 
90 	return snprintf(buf, PAGE_SIZE, "%s\n", type);
91 }
92 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
93 
94 static ssize_t mtd_flags_show(struct device *dev,
95 		struct device_attribute *attr, char *buf)
96 {
97 	struct mtd_info *mtd = dev_to_mtd(dev);
98 
99 	return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
100 
101 }
102 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
103 
104 static ssize_t mtd_size_show(struct device *dev,
105 		struct device_attribute *attr, char *buf)
106 {
107 	struct mtd_info *mtd = dev_to_mtd(dev);
108 
109 	return snprintf(buf, PAGE_SIZE, "%llu\n",
110 		(unsigned long long)mtd->size);
111 
112 }
113 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
114 
115 static ssize_t mtd_erasesize_show(struct device *dev,
116 		struct device_attribute *attr, char *buf)
117 {
118 	struct mtd_info *mtd = dev_to_mtd(dev);
119 
120 	return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
121 
122 }
123 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
124 
125 static ssize_t mtd_writesize_show(struct device *dev,
126 		struct device_attribute *attr, char *buf)
127 {
128 	struct mtd_info *mtd = dev_to_mtd(dev);
129 
130 	return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
131 
132 }
133 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
134 
135 static ssize_t mtd_subpagesize_show(struct device *dev,
136 		struct device_attribute *attr, char *buf)
137 {
138 	struct mtd_info *mtd = dev_to_mtd(dev);
139 	unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
140 
141 	return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
142 
143 }
144 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
145 
146 static ssize_t mtd_oobsize_show(struct device *dev,
147 		struct device_attribute *attr, char *buf)
148 {
149 	struct mtd_info *mtd = dev_to_mtd(dev);
150 
151 	return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
152 
153 }
154 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
155 
156 static ssize_t mtd_numeraseregions_show(struct device *dev,
157 		struct device_attribute *attr, char *buf)
158 {
159 	struct mtd_info *mtd = dev_to_mtd(dev);
160 
161 	return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
162 
163 }
164 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
165 	NULL);
166 
167 static ssize_t mtd_name_show(struct device *dev,
168 		struct device_attribute *attr, char *buf)
169 {
170 	struct mtd_info *mtd = dev_to_mtd(dev);
171 
172 	return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
173 
174 }
175 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
176 
177 static struct attribute *mtd_attrs[] = {
178 	&dev_attr_type.attr,
179 	&dev_attr_flags.attr,
180 	&dev_attr_size.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,
186 	&dev_attr_name.attr,
187 	NULL,
188 };
189 
190 struct attribute_group mtd_group = {
191 	.attrs		= mtd_attrs,
192 };
193 
194 struct attribute_group *mtd_groups[] = {
195 	&mtd_group,
196 	NULL,
197 };
198 
199 static struct device_type mtd_devtype = {
200 	.name		= "mtd",
201 	.groups		= mtd_groups,
202 	.release	= mtd_release,
203 };
204 
205 /**
206  *	add_mtd_device - register an MTD device
207  *	@mtd: pointer to new MTD device info structure
208  *
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.
214  */
215 
216 int add_mtd_device(struct mtd_info *mtd)
217 {
218 	int i;
219 
220 	if (!mtd->backing_dev_info) {
221 		switch (mtd->type) {
222 		case MTD_RAM:
223 			mtd->backing_dev_info = &mtd_bdi_rw_mappable;
224 			break;
225 		case MTD_ROM:
226 			mtd->backing_dev_info = &mtd_bdi_ro_mappable;
227 			break;
228 		default:
229 			mtd->backing_dev_info = &mtd_bdi_unmappable;
230 			break;
231 		}
232 	}
233 
234 	BUG_ON(mtd->writesize == 0);
235 	mutex_lock(&mtd_table_mutex);
236 
237 	for (i=0; i < MAX_MTD_DEVICES; i++)
238 		if (!mtd_table[i]) {
239 			struct mtd_notifier *not;
240 
241 			mtd_table[i] = mtd;
242 			mtd->index = i;
243 			mtd->usecount = 0;
244 
245 			if (is_power_of_2(mtd->erasesize))
246 				mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
247 			else
248 				mtd->erasesize_shift = 0;
249 
250 			if (is_power_of_2(mtd->writesize))
251 				mtd->writesize_shift = ffs(mtd->writesize) - 1;
252 			else
253 				mtd->writesize_shift = 0;
254 
255 			mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
256 			mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
257 
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))
262 					printk(KERN_WARNING
263 					       "%s: unlock failed, "
264 					       "writes may not work\n",
265 					       mtd->name);
266 			}
267 
268 			/* Caller should have set dev.parent to match the
269 			 * physical device.
270 			 */
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) {
276 				mtd_table[i] = NULL;
277 				break;
278 			}
279 
280 			if (MTD_DEVT(i))
281 				device_create(mtd_class, mtd->dev.parent,
282 						MTD_DEVT(i) + 1,
283 						NULL, "mtd%dro", i);
284 
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)
289 				not->add(mtd);
290 
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
295 			   either. :) */
296 			__module_get(THIS_MODULE);
297 			return 0;
298 		}
299 
300 	mutex_unlock(&mtd_table_mutex);
301 	return 1;
302 }
303 
304 /**
305  *	del_mtd_device - unregister an MTD device
306  *	@mtd: pointer to MTD device info structure
307  *
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.
312  */
313 
314 int del_mtd_device (struct mtd_info *mtd)
315 {
316 	int ret;
317 
318 	mutex_lock(&mtd_table_mutex);
319 
320 	if (mtd_table[mtd->index] != mtd) {
321 		ret = -ENODEV;
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);
325 		ret = -EBUSY;
326 	} else {
327 		struct mtd_notifier *not;
328 
329 		device_unregister(&mtd->dev);
330 
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)
334 			not->remove(mtd);
335 
336 		mtd_table[mtd->index] = NULL;
337 
338 		module_put(THIS_MODULE);
339 		ret = 0;
340 	}
341 
342 	mutex_unlock(&mtd_table_mutex);
343 	return ret;
344 }
345 
346 /**
347  *	register_mtd_user - register a 'user' of MTD devices.
348  *	@new: pointer to notifier info structure
349  *
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.
353  */
354 
355 void register_mtd_user (struct mtd_notifier *new)
356 {
357 	int i;
358 
359 	mutex_lock(&mtd_table_mutex);
360 
361 	list_add(&new->list, &mtd_notifiers);
362 
363  	__module_get(THIS_MODULE);
364 
365 	for (i=0; i< MAX_MTD_DEVICES; i++)
366 		if (mtd_table[i])
367 			new->add(mtd_table[i]);
368 
369 	mutex_unlock(&mtd_table_mutex);
370 }
371 
372 /**
373  *	unregister_mtd_user - unregister a 'user' of MTD devices.
374  *	@old: pointer to notifier info structure
375  *
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.
380  */
381 
382 int unregister_mtd_user (struct mtd_notifier *old)
383 {
384 	int i;
385 
386 	mutex_lock(&mtd_table_mutex);
387 
388 	module_put(THIS_MODULE);
389 
390 	for (i=0; i< MAX_MTD_DEVICES; i++)
391 		if (mtd_table[i])
392 			old->remove(mtd_table[i]);
393 
394 	list_del(&old->list);
395 	mutex_unlock(&mtd_table_mutex);
396 	return 0;
397 }
398 
399 
400 /**
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
404  *
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
409  *	error code if not.
410  */
411 
412 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
413 {
414 	struct mtd_info *ret = NULL;
415 	int i, err = -ENODEV;
416 
417 	mutex_lock(&mtd_table_mutex);
418 
419 	if (num == -1) {
420 		for (i=0; i< MAX_MTD_DEVICES; i++)
421 			if (mtd_table[i] == mtd)
422 				ret = mtd_table[i];
423 	} else if (num < MAX_MTD_DEVICES) {
424 		ret = mtd_table[num];
425 		if (mtd && mtd != ret)
426 			ret = NULL;
427 	}
428 
429 	if (!ret)
430 		goto out_unlock;
431 
432 	if (!try_module_get(ret->owner))
433 		goto out_unlock;
434 
435 	if (ret->get_device) {
436 		err = ret->get_device(ret);
437 		if (err)
438 			goto out_put;
439 	}
440 
441 	ret->usecount++;
442 	mutex_unlock(&mtd_table_mutex);
443 	return ret;
444 
445 out_put:
446 	module_put(ret->owner);
447 out_unlock:
448 	mutex_unlock(&mtd_table_mutex);
449 	return ERR_PTR(err);
450 }
451 
452 /**
453  *	get_mtd_device_nm - obtain a validated handle for an MTD device by
454  *	device name
455  *	@name: MTD device name to open
456  *
457  * 	This function returns MTD device description structure in case of
458  * 	success and an error code in case of failure.
459  */
460 
461 struct mtd_info *get_mtd_device_nm(const char *name)
462 {
463 	int i, err = -ENODEV;
464 	struct mtd_info *mtd = NULL;
465 
466 	mutex_lock(&mtd_table_mutex);
467 
468 	for (i = 0; i < MAX_MTD_DEVICES; i++) {
469 		if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
470 			mtd = mtd_table[i];
471 			break;
472 		}
473 	}
474 
475 	if (!mtd)
476 		goto out_unlock;
477 
478 	if (!try_module_get(mtd->owner))
479 		goto out_unlock;
480 
481 	if (mtd->get_device) {
482 		err = mtd->get_device(mtd);
483 		if (err)
484 			goto out_put;
485 	}
486 
487 	mtd->usecount++;
488 	mutex_unlock(&mtd_table_mutex);
489 	return mtd;
490 
491 out_put:
492 	module_put(mtd->owner);
493 out_unlock:
494 	mutex_unlock(&mtd_table_mutex);
495 	return ERR_PTR(err);
496 }
497 
498 void put_mtd_device(struct mtd_info *mtd)
499 {
500 	int c;
501 
502 	mutex_lock(&mtd_table_mutex);
503 	c = --mtd->usecount;
504 	if (mtd->put_device)
505 		mtd->put_device(mtd);
506 	mutex_unlock(&mtd_table_mutex);
507 	BUG_ON(c < 0);
508 
509 	module_put(mtd->owner);
510 }
511 
512 /* default_mtd_writev - default mtd writev method for MTD devices that
513  *			don't implement their own
514  */
515 
516 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
517 		       unsigned long count, loff_t to, size_t *retlen)
518 {
519 	unsigned long i;
520 	size_t totlen = 0, thislen;
521 	int ret = 0;
522 
523 	if(!mtd->write) {
524 		ret = -EROFS;
525 	} else {
526 		for (i=0; i<count; i++) {
527 			if (!vecs[i].iov_len)
528 				continue;
529 			ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
530 			totlen += thislen;
531 			if (ret || thislen != vecs[i].iov_len)
532 				break;
533 			to += vecs[i].iov_len;
534 		}
535 	}
536 	if (retlen)
537 		*retlen = totlen;
538 	return ret;
539 }
540 
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);
549 
550 #ifdef CONFIG_PROC_FS
551 
552 /*====================================================================*/
553 /* Support for /proc/mtd */
554 
555 static struct proc_dir_entry *proc_mtd;
556 
557 static inline int mtd_proc_info (char *buf, int i)
558 {
559 	struct mtd_info *this = mtd_table[i];
560 
561 	if (!this)
562 		return 0;
563 
564 	return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
565 		       (unsigned long long)this->size,
566 		       this->erasesize, this->name);
567 }
568 
569 static int mtd_read_proc (char *page, char **start, off_t off, int count,
570 			  int *eof, void *data_unused)
571 {
572 	int len, l, i;
573         off_t   begin = 0;
574 
575 	mutex_lock(&mtd_table_mutex);
576 
577 	len = sprintf(page, "dev:    size   erasesize  name\n");
578         for (i=0; i< MAX_MTD_DEVICES; i++) {
579 
580                 l = mtd_proc_info(page + len, i);
581                 len += l;
582                 if (len+begin > off+count)
583                         goto done;
584                 if (len+begin < off) {
585                         begin += len;
586                         len = 0;
587                 }
588         }
589 
590         *eof = 1;
591 
592 done:
593 	mutex_unlock(&mtd_table_mutex);
594         if (off >= len+begin)
595                 return 0;
596         *start = page + (off-begin);
597         return ((count < begin+len-off) ? count : begin+len-off);
598 }
599 
600 #endif /* CONFIG_PROC_FS */
601 
602 /*====================================================================*/
603 /* Init code */
604 
605 static int __init init_mtd(void)
606 {
607 	mtd_class = class_create(THIS_MODULE, "mtd");
608 
609 	if (IS_ERR(mtd_class)) {
610 		pr_err("Error creating mtd class.\n");
611 		return PTR_ERR(mtd_class);
612 	}
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 */
617 	return 0;
618 }
619 
620 static void __exit cleanup_mtd(void)
621 {
622 #ifdef CONFIG_PROC_FS
623         if (proc_mtd)
624 		remove_proc_entry( "mtd", NULL);
625 #endif /* CONFIG_PROC_FS */
626 	class_destroy(mtd_class);
627 }
628 
629 module_init(init_mtd);
630 module_exit(cleanup_mtd);
631 
632 MODULE_LICENSE("GPL");
633 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
634 MODULE_DESCRIPTION("Core MTD registration and access routines");
635