PCI PM: Fix handling of devices without PM support by pci_target_state()
[linux-2.6/verdex.git] / drivers / mtd / mtdcore.c
blobbccb4b1ffc46ff21b127f9e80cafef118f05f3ca
1 /*
2 * Core registration and callback routines for MTD
3 * drivers and users.
5 */
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>
21 #include <linux/mtd/mtd.h>
22 #include "internal.h"
24 #include "mtdcore.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)
42 #else
43 #define MTD_DEVT(index) 0
44 #endif
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 */
54 if (index)
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);
62 char *type;
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";
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,
165 NULL);
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[] = {
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,
190 struct attribute_group mtd_group = {
191 .attrs = mtd_attrs,
194 struct attribute_group *mtd_groups[] = {
195 &mtd_group,
196 NULL,
199 static struct device_type mtd_devtype = {
200 .name = "mtd",
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)
218 int i;
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;
234 BUG_ON(mtd->writesize == 0);
235 mutex_lock(&mtd_table_mutex);
237 for (i=0; i < MAX_MTD_DEVICES; i++)
238 if (!mtd_table[i]) {
239 struct mtd_notifier *not;
241 mtd_table[i] = mtd;
242 mtd->index = i;
243 mtd->usecount = 0;
245 if (is_power_of_2(mtd->erasesize))
246 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
247 else
248 mtd->erasesize_shift = 0;
250 if (is_power_of_2(mtd->writesize))
251 mtd->writesize_shift = ffs(mtd->writesize) - 1;
252 else
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))
262 printk(KERN_WARNING
263 "%s: unlock failed, "
264 "writes may not work\n",
265 mtd->name);
268 /* Caller should have set dev.parent to match the
269 * physical device.
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;
280 if (MTD_DEVT(i))
281 device_create(mtd_class, mtd->dev.parent,
282 MTD_DEVT(i) + 1,
283 NULL, "mtd%dro", i);
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);
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;
300 mutex_unlock(&mtd_table_mutex);
301 return 1;
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)
316 int ret;
318 mutex_lock(&mtd_table_mutex);
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;
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)
334 not->remove(mtd);
336 mtd_table[mtd->index] = NULL;
338 module_put(THIS_MODULE);
339 ret = 0;
342 mutex_unlock(&mtd_table_mutex);
343 return ret;
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)
357 int i;
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++)
366 if (mtd_table[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)
384 int i;
386 mutex_lock(&mtd_table_mutex);
388 module_put(THIS_MODULE);
390 for (i=0; i< MAX_MTD_DEVICES; i++)
391 if (mtd_table[i])
392 old->remove(mtd_table[i]);
394 list_del(&old->list);
395 mutex_unlock(&mtd_table_mutex);
396 return 0;
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
409 * error code if not.
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);
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;
429 if (!ret)
430 goto out_unlock;
432 if (!try_module_get(ret->owner))
433 goto out_unlock;
435 if (ret->get_device) {
436 err = ret->get_device(ret);
437 if (err)
438 goto out_put;
441 ret->usecount++;
442 mutex_unlock(&mtd_table_mutex);
443 return ret;
445 out_put:
446 module_put(ret->owner);
447 out_unlock:
448 mutex_unlock(&mtd_table_mutex);
449 return ERR_PTR(err);
453 * get_mtd_device_nm - obtain a validated handle for an MTD device by
454 * device name
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)) {
470 mtd = mtd_table[i];
471 break;
475 if (!mtd)
476 goto out_unlock;
478 if (!try_module_get(mtd->owner))
479 goto out_unlock;
481 if (mtd->get_device) {
482 err = mtd->get_device(mtd);
483 if (err)
484 goto out_put;
487 mtd->usecount++;
488 mutex_unlock(&mtd_table_mutex);
489 return mtd;
491 out_put:
492 module_put(mtd->owner);
493 out_unlock:
494 mutex_unlock(&mtd_table_mutex);
495 return ERR_PTR(err);
498 void put_mtd_device(struct mtd_info *mtd)
500 int c;
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);
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)
519 unsigned long i;
520 size_t totlen = 0, thislen;
521 int ret = 0;
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;
536 if (retlen)
537 *retlen = totlen;
538 return ret;
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];
561 if (!this)
562 return 0;
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)
572 int len, l, i;
573 off_t begin = 0;
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);
581 len += l;
582 if (len+begin > off+count)
583 goto done;
584 if (len+begin < off) {
585 begin += len;
586 len = 0;
590 *eof = 1;
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);
600 #endif /* CONFIG_PROC_FS */
602 /*====================================================================*/
603 /* Init code */
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 */
617 return 0;
620 static void __exit cleanup_mtd(void)
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);
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");