]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/mtd/mtdcore.c
[MTD] driver model updates
[net-next-2.6.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
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         struct mtd_info *mtd = dev_to_mtd(dev);
52
53         /* remove /dev/mtdXro node if needed */
54         if (MTD_DEVT(mtd->index))
55                 device_destroy(mtd_class, MTD_DEVT(mtd->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(mtd_type, S_IRUGO, mtd_type_show, NULL);
93
94 static struct attribute *mtd_attrs[] = {
95         &dev_attr_mtd_type.attr,
96         /* FIXME provide a /proc/mtd superset */
97         NULL,
98 };
99
100 struct attribute_group mtd_group = {
101         .attrs          = mtd_attrs,
102 };
103
104 struct attribute_group *mtd_groups[] = {
105         &mtd_group,
106         NULL,
107 };
108
109 static struct device_type mtd_devtype = {
110         .name           = "mtd",
111         .groups         = mtd_groups,
112         .release        = mtd_release,
113 };
114
115 /**
116  *      add_mtd_device - register an MTD device
117  *      @mtd: pointer to new MTD device info structure
118  *
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.
124  */
125
126 int add_mtd_device(struct mtd_info *mtd)
127 {
128         int i;
129
130         if (!mtd->backing_dev_info) {
131                 switch (mtd->type) {
132                 case MTD_RAM:
133                         mtd->backing_dev_info = &mtd_bdi_rw_mappable;
134                         break;
135                 case MTD_ROM:
136                         mtd->backing_dev_info = &mtd_bdi_ro_mappable;
137                         break;
138                 default:
139                         mtd->backing_dev_info = &mtd_bdi_unmappable;
140                         break;
141                 }
142         }
143
144         BUG_ON(mtd->writesize == 0);
145         mutex_lock(&mtd_table_mutex);
146
147         for (i=0; i < MAX_MTD_DEVICES; i++)
148                 if (!mtd_table[i]) {
149                         struct mtd_notifier *not;
150
151                         mtd_table[i] = mtd;
152                         mtd->index = i;
153                         mtd->usecount = 0;
154
155                         if (is_power_of_2(mtd->erasesize))
156                                 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
157                         else
158                                 mtd->erasesize_shift = 0;
159
160                         if (is_power_of_2(mtd->writesize))
161                                 mtd->writesize_shift = ffs(mtd->writesize) - 1;
162                         else
163                                 mtd->writesize_shift = 0;
164
165                         mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
166                         mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
167
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))
172                                         printk(KERN_WARNING
173                                                "%s: unlock failed, "
174                                                "writes may not work\n",
175                                                mtd->name);
176                         }
177
178                         /* Caller should have set dev.parent to match the
179                          * physical device.
180                          */
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) {
186                                 mtd_table[i] = NULL;
187                                 break;
188                         }
189
190                         if (MTD_DEVT(i))
191                                 device_create(mtd_class, mtd->dev.parent,
192                                                 MTD_DEVT(i) + 1,
193                                                 NULL, "mtd%dro", i);
194
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)
199                                 not->add(mtd);
200
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
205                            either. :) */
206                         __module_get(THIS_MODULE);
207                         return 0;
208                 }
209
210         mutex_unlock(&mtd_table_mutex);
211         return 1;
212 }
213
214 /**
215  *      del_mtd_device - unregister an MTD device
216  *      @mtd: pointer to MTD device info structure
217  *
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.
222  */
223
224 int del_mtd_device (struct mtd_info *mtd)
225 {
226         int ret;
227
228         mutex_lock(&mtd_table_mutex);
229
230         if (mtd_table[mtd->index] != mtd) {
231                 ret = -ENODEV;
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);
235                 ret = -EBUSY;
236         } else {
237                 struct mtd_notifier *not;
238
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)
242                         not->remove(mtd);
243
244                 mtd_table[mtd->index] = NULL;
245
246                 module_put(THIS_MODULE);
247                 ret = 0;
248         }
249
250         mutex_unlock(&mtd_table_mutex);
251         return ret;
252 }
253
254 /**
255  *      register_mtd_user - register a 'user' of MTD devices.
256  *      @new: pointer to notifier info structure
257  *
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.
261  */
262
263 void register_mtd_user (struct mtd_notifier *new)
264 {
265         int i;
266
267         mutex_lock(&mtd_table_mutex);
268
269         list_add(&new->list, &mtd_notifiers);
270
271         __module_get(THIS_MODULE);
272
273         for (i=0; i< MAX_MTD_DEVICES; i++)
274                 if (mtd_table[i])
275                         new->add(mtd_table[i]);
276
277         mutex_unlock(&mtd_table_mutex);
278 }
279
280 /**
281  *      unregister_mtd_user - unregister a 'user' of MTD devices.
282  *      @old: pointer to notifier info structure
283  *
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.
288  */
289
290 int unregister_mtd_user (struct mtd_notifier *old)
291 {
292         int i;
293
294         mutex_lock(&mtd_table_mutex);
295
296         module_put(THIS_MODULE);
297
298         for (i=0; i< MAX_MTD_DEVICES; i++)
299                 if (mtd_table[i])
300                         old->remove(mtd_table[i]);
301
302         list_del(&old->list);
303         mutex_unlock(&mtd_table_mutex);
304         return 0;
305 }
306
307
308 /**
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
312  *
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
317  *      error code if not.
318  */
319
320 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
321 {
322         struct mtd_info *ret = NULL;
323         int i, err = -ENODEV;
324
325         mutex_lock(&mtd_table_mutex);
326
327         if (num == -1) {
328                 for (i=0; i< MAX_MTD_DEVICES; i++)
329                         if (mtd_table[i] == mtd)
330                                 ret = mtd_table[i];
331         } else if (num < MAX_MTD_DEVICES) {
332                 ret = mtd_table[num];
333                 if (mtd && mtd != ret)
334                         ret = NULL;
335         }
336
337         if (!ret)
338                 goto out_unlock;
339
340         if (!try_module_get(ret->owner))
341                 goto out_unlock;
342
343         if (ret->get_device) {
344                 err = ret->get_device(ret);
345                 if (err)
346                         goto out_put;
347         }
348
349         ret->usecount++;
350         mutex_unlock(&mtd_table_mutex);
351         return ret;
352
353 out_put:
354         module_put(ret->owner);
355 out_unlock:
356         mutex_unlock(&mtd_table_mutex);
357         return ERR_PTR(err);
358 }
359
360 /**
361  *      get_mtd_device_nm - obtain a validated handle for an MTD device by
362  *      device name
363  *      @name: MTD device name to open
364  *
365  *      This function returns MTD device description structure in case of
366  *      success and an error code in case of failure.
367  */
368
369 struct mtd_info *get_mtd_device_nm(const char *name)
370 {
371         int i, err = -ENODEV;
372         struct mtd_info *mtd = NULL;
373
374         mutex_lock(&mtd_table_mutex);
375
376         for (i = 0; i < MAX_MTD_DEVICES; i++) {
377                 if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
378                         mtd = mtd_table[i];
379                         break;
380                 }
381         }
382
383         if (!mtd)
384                 goto out_unlock;
385
386         if (!try_module_get(mtd->owner))
387                 goto out_unlock;
388
389         if (mtd->get_device) {
390                 err = mtd->get_device(mtd);
391                 if (err)
392                         goto out_put;
393         }
394
395         mtd->usecount++;
396         mutex_unlock(&mtd_table_mutex);
397         return mtd;
398
399 out_put:
400         module_put(mtd->owner);
401 out_unlock:
402         mutex_unlock(&mtd_table_mutex);
403         return ERR_PTR(err);
404 }
405
406 void put_mtd_device(struct mtd_info *mtd)
407 {
408         int c;
409
410         mutex_lock(&mtd_table_mutex);
411         c = --mtd->usecount;
412         if (mtd->put_device)
413                 mtd->put_device(mtd);
414         mutex_unlock(&mtd_table_mutex);
415         BUG_ON(c < 0);
416
417         module_put(mtd->owner);
418 }
419
420 /* default_mtd_writev - default mtd writev method for MTD devices that
421  *                      don't implement their own
422  */
423
424 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
425                        unsigned long count, loff_t to, size_t *retlen)
426 {
427         unsigned long i;
428         size_t totlen = 0, thislen;
429         int ret = 0;
430
431         if(!mtd->write) {
432                 ret = -EROFS;
433         } else {
434                 for (i=0; i<count; i++) {
435                         if (!vecs[i].iov_len)
436                                 continue;
437                         ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
438                         totlen += thislen;
439                         if (ret || thislen != vecs[i].iov_len)
440                                 break;
441                         to += vecs[i].iov_len;
442                 }
443         }
444         if (retlen)
445                 *retlen = totlen;
446         return ret;
447 }
448
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);
457
458 static int __init mtd_setup(void)
459 {
460         mtd_class = class_create(THIS_MODULE, "mtd");
461
462         if (IS_ERR(mtd_class)) {
463                 pr_err("Error creating mtd class.\n");
464                 return PTR_ERR(mtd_class);
465         }
466         return 0;
467 }
468 core_initcall(mtd_setup);
469
470 static void __exit mtd_teardown(void)
471 {
472         class_destroy(mtd_class);
473 }
474 __exitcall(mtd_teardown);
475
476 #ifdef CONFIG_PROC_FS
477
478 /*====================================================================*/
479 /* Support for /proc/mtd */
480
481 static struct proc_dir_entry *proc_mtd;
482
483 static inline int mtd_proc_info (char *buf, int i)
484 {
485         struct mtd_info *this = mtd_table[i];
486
487         if (!this)
488                 return 0;
489
490         return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
491                        (unsigned long long)this->size,
492                        this->erasesize, this->name);
493 }
494
495 static int mtd_read_proc (char *page, char **start, off_t off, int count,
496                           int *eof, void *data_unused)
497 {
498         int len, l, i;
499         off_t   begin = 0;
500
501         mutex_lock(&mtd_table_mutex);
502
503         len = sprintf(page, "dev:    size   erasesize  name\n");
504         for (i=0; i< MAX_MTD_DEVICES; i++) {
505
506                 l = mtd_proc_info(page + len, i);
507                 len += l;
508                 if (len+begin > off+count)
509                         goto done;
510                 if (len+begin < off) {
511                         begin += len;
512                         len = 0;
513                 }
514         }
515
516         *eof = 1;
517
518 done:
519         mutex_unlock(&mtd_table_mutex);
520         if (off >= len+begin)
521                 return 0;
522         *start = page + (off-begin);
523         return ((count < begin+len-off) ? count : begin+len-off);
524 }
525
526 /*====================================================================*/
527 /* Init code */
528
529 static int __init init_mtd(void)
530 {
531         if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
532                 proc_mtd->read_proc = mtd_read_proc;
533         return 0;
534 }
535
536 static void __exit cleanup_mtd(void)
537 {
538         if (proc_mtd)
539                 remove_proc_entry( "mtd", NULL);
540 }
541
542 module_init(init_mtd);
543 module_exit(cleanup_mtd);
544
545 #endif /* CONFIG_PROC_FS */
546
547
548 MODULE_LICENSE("GPL");
549 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
550 MODULE_DESCRIPTION("Core MTD registration and access routines");