[MTD] driver model updates
[deliverable/linux.git] / drivers / mtd / mtdcore.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Core registration and callback routines for MTD
3 * drivers and users.
4 *
5 */
6
1da177e4
LT
7#include <linux/module.h>
8#include <linux/kernel.h>
1da177e4
LT
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>
7799308f 15#include <linux/err.h>
1da177e4
LT
16#include <linux/ioctl.h>
17#include <linux/init.h>
18#include <linux/mtd/compatmac.h>
1da177e4 19#include <linux/proc_fs.h>
1da177e4
LT
20
21#include <linux/mtd/mtd.h>
402d3265 22#include "internal.h"
1da177e4 23
356d70f1
BD
24#include "mtdcore.h"
25
1f24b5a8
DB
26
27static struct class *mtd_class;
28
97894cda 29/* These are exported solely for the purpose of mtd_blkdevs.c. You
1da177e4 30 should not use them for _anything_ else */
48b19268 31DEFINE_MUTEX(mtd_table_mutex);
1da177e4
LT
32struct mtd_info *mtd_table[MAX_MTD_DEVICES];
33
34EXPORT_SYMBOL_GPL(mtd_table_mutex);
35EXPORT_SYMBOL_GPL(mtd_table);
36
37static LIST_HEAD(mtd_notifiers);
38
1f24b5a8
DB
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 */
49static 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
58static 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}
92static DEVICE_ATTR(mtd_type, S_IRUGO, mtd_type_show, NULL);
93
94static struct attribute *mtd_attrs[] = {
95 &dev_attr_mtd_type.attr,
96 /* FIXME provide a /proc/mtd superset */
97 NULL,
98};
99
100struct attribute_group mtd_group = {
101 .attrs = mtd_attrs,
102};
103
104struct attribute_group *mtd_groups[] = {
105 &mtd_group,
106 NULL,
107};
108
109static struct device_type mtd_devtype = {
110 .name = "mtd",
111 .groups = mtd_groups,
112 .release = mtd_release,
113};
114
1da177e4
LT
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)
1f24b5a8 123 * or there's a sysfs error.
1da177e4
LT
124 */
125
126int add_mtd_device(struct mtd_info *mtd)
127{
128 int i;
129
402d3265
DH
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
783ed81f 144 BUG_ON(mtd->writesize == 0);
48b19268 145 mutex_lock(&mtd_table_mutex);
1da177e4
LT
146
147 for (i=0; i < MAX_MTD_DEVICES; i++)
148 if (!mtd_table[i]) {
2606c797 149 struct mtd_notifier *not;
1da177e4
LT
150
151 mtd_table[i] = mtd;
152 mtd->index = i;
153 mtd->usecount = 0;
154
69423d99
AH
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
187ef152
HS
168 /* Some chips always power up locked. Unlock them now */
169 if ((mtd->flags & MTD_WRITEABLE)
e619a75f 170 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
187ef152
HS
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
1f24b5a8
DB
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
1da177e4
LT
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 */
71a928c0 198 list_for_each_entry(not, &mtd_notifiers, list)
1da177e4 199 not->add(mtd);
97894cda 200
48b19268 201 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
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 }
97894cda 209
48b19268 210 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
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
224int del_mtd_device (struct mtd_info *mtd)
225{
226 int ret;
97894cda 227
48b19268 228 mutex_lock(&mtd_table_mutex);
1da177e4
LT
229
230 if (mtd_table[mtd->index] != mtd) {
231 ret = -ENODEV;
232 } else if (mtd->usecount) {
97894cda 233 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
1da177e4
LT
234 mtd->index, mtd->name, mtd->usecount);
235 ret = -EBUSY;
236 } else {
856613c9 237 struct mtd_notifier *not;
1da177e4
LT
238
239 /* No need to get a refcount on the module containing
240 the notifier, since we hold the mtd_table_mutex */
71a928c0 241 list_for_each_entry(not, &mtd_notifiers, list)
1da177e4 242 not->remove(mtd);
1da177e4
LT
243
244 mtd_table[mtd->index] = NULL;
245
246 module_put(THIS_MODULE);
247 ret = 0;
248 }
249
48b19268 250 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
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
263void register_mtd_user (struct mtd_notifier *new)
264{
265 int i;
266
48b19268 267 mutex_lock(&mtd_table_mutex);
1da177e4
LT
268
269 list_add(&new->list, &mtd_notifiers);
270
271 __module_get(THIS_MODULE);
97894cda 272
1da177e4
LT
273 for (i=0; i< MAX_MTD_DEVICES; i++)
274 if (mtd_table[i])
275 new->add(mtd_table[i]);
276
48b19268 277 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
278}
279
280/**
49450795
AB
281 * unregister_mtd_user - unregister a 'user' of MTD devices.
282 * @old: pointer to notifier info structure
1da177e4
LT
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
290int unregister_mtd_user (struct mtd_notifier *old)
291{
292 int i;
293
48b19268 294 mutex_lock(&mtd_table_mutex);
1da177e4
LT
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]);
97894cda 301
1da177e4 302 list_del(&old->list);
48b19268 303 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
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
9c74034f
AB
316 * both, return the num'th driver only if its address matches. Return
317 * error code if not.
1da177e4 318 */
97894cda 319
1da177e4
LT
320struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
321{
322 struct mtd_info *ret = NULL;
9c74034f 323 int i, err = -ENODEV;
1da177e4 324
48b19268 325 mutex_lock(&mtd_table_mutex);
1da177e4
LT
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
9fe912ce
AB
337 if (!ret)
338 goto out_unlock;
339
9c74034f 340 if (!try_module_get(ret->owner))
9fe912ce 341 goto out_unlock;
9fe912ce 342
9c74034f
AB
343 if (ret->get_device) {
344 err = ret->get_device(ret);
345 if (err)
346 goto out_put;
9fe912ce 347 }
1da177e4 348
9fe912ce 349 ret->usecount++;
9c74034f
AB
350 mutex_unlock(&mtd_table_mutex);
351 return ret;
1da177e4 352
9c74034f
AB
353out_put:
354 module_put(ret->owner);
9fe912ce 355out_unlock:
48b19268 356 mutex_unlock(&mtd_table_mutex);
9c74034f 357 return ERR_PTR(err);
1da177e4
LT
358}
359
7799308f
AB
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
369struct mtd_info *get_mtd_device_nm(const char *name)
370{
9fe912ce
AB
371 int i, err = -ENODEV;
372 struct mtd_info *mtd = NULL;
7799308f
AB
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
9fe912ce 383 if (!mtd)
7799308f
AB
384 goto out_unlock;
385
386 if (!try_module_get(mtd->owner))
387 goto out_unlock;
388
9fe912ce
AB
389 if (mtd->get_device) {
390 err = mtd->get_device(mtd);
391 if (err)
392 goto out_put;
393 }
394
7799308f 395 mtd->usecount++;
9fe912ce
AB
396 mutex_unlock(&mtd_table_mutex);
397 return mtd;
7799308f 398
9fe912ce
AB
399out_put:
400 module_put(mtd->owner);
7799308f
AB
401out_unlock:
402 mutex_unlock(&mtd_table_mutex);
9fe912ce 403 return ERR_PTR(err);
7799308f
AB
404}
405
1da177e4
LT
406void put_mtd_device(struct mtd_info *mtd)
407{
408 int c;
409
48b19268 410 mutex_lock(&mtd_table_mutex);
1da177e4 411 c = --mtd->usecount;
9fe912ce
AB
412 if (mtd->put_device)
413 mtd->put_device(mtd);
48b19268 414 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
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
dd36f267 421 * don't implement their own
1da177e4
LT
422 */
423
424int 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
dd36f267
DW
449EXPORT_SYMBOL_GPL(add_mtd_device);
450EXPORT_SYMBOL_GPL(del_mtd_device);
451EXPORT_SYMBOL_GPL(get_mtd_device);
452EXPORT_SYMBOL_GPL(get_mtd_device_nm);
453EXPORT_SYMBOL_GPL(put_mtd_device);
454EXPORT_SYMBOL_GPL(register_mtd_user);
455EXPORT_SYMBOL_GPL(unregister_mtd_user);
456EXPORT_SYMBOL_GPL(default_mtd_writev);
1da177e4 457
1f24b5a8
DB
458static 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}
468core_initcall(mtd_setup);
469
470static void __exit mtd_teardown(void)
471{
472 class_destroy(mtd_class);
473}
474__exitcall(mtd_teardown);
475
2d2dce0e
PM
476#ifdef CONFIG_PROC_FS
477
1da177e4
LT
478/*====================================================================*/
479/* Support for /proc/mtd */
480
1da177e4
LT
481static struct proc_dir_entry *proc_mtd;
482
483static 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
69423d99
AH
490 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
491 (unsigned long long)this->size,
1da177e4
LT
492 this->erasesize, this->name);
493}
494
495static 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
48b19268 501 mutex_lock(&mtd_table_mutex);
1da177e4
LT
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
518done:
48b19268 519 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
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
1da177e4
LT
526/*====================================================================*/
527/* Init code */
528
529static int __init init_mtd(void)
530{
1da177e4
LT
531 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
532 proc_mtd->read_proc = mtd_read_proc;
1da177e4
LT
533 return 0;
534}
535
536static void __exit cleanup_mtd(void)
537{
1da177e4
LT
538 if (proc_mtd)
539 remove_proc_entry( "mtd", NULL);
1da177e4
LT
540}
541
542module_init(init_mtd);
543module_exit(cleanup_mtd);
544
2d2dce0e
PM
545#endif /* CONFIG_PROC_FS */
546
1da177e4
LT
547
548MODULE_LICENSE("GPL");
549MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
550MODULE_DESCRIPTION("Core MTD registration and access routines");
This page took 0.322082 seconds and 5 git commands to generate.