blob: 3ae06c8935b5b286f2b418d7e928fc2a90dc740c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Core registration and callback routines for MTD
3 * drivers and users.
4 *
Jörn Engela33eb6b2010-04-27 09:40:52 +02005 * bdi bits are:
6 * Copyright © 2006 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/string.h>
14#include <linux/timer.h>
15#include <linux/major.h>
16#include <linux/fs.h>
Artem Bityutskiy77993082006-10-11 14:52:44 +030017#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/ioctl.h>
19#include <linux/init.h>
20#include <linux/mtd/compatmac.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/proc_fs.h>
Ben Hutchingsb520e412010-01-29 20:59:42 +000022#include <linux/idr.h>
Jörn Engela33eb6b2010-04-27 09:40:52 +020023#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <linux/mtd/mtd.h>
26
Ben Dooks356d70f2007-05-28 20:28:34 +010027#include "mtdcore.h"
Jörn Engela33eb6b2010-04-27 09:40:52 +020028/*
29 * backing device capabilities for non-mappable devices (such as NAND flash)
30 * - permits private mappings, copies are taken of the data
31 */
32struct backing_dev_info mtd_bdi_unmappable = {
33 .capabilities = BDI_CAP_MAP_COPY,
34};
35
36/*
37 * backing device capabilities for R/O mappable devices (such as ROM)
38 * - permits private mappings, copies are taken of the data
39 * - permits non-writable shared mappings
40 */
41struct backing_dev_info mtd_bdi_ro_mappable = {
42 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
43 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
44};
45
46/*
47 * backing device capabilities for writable mappable devices (such as RAM)
48 * - permits private mappings, copies are taken of the data
49 * - permits non-writable shared mappings
50 */
51struct backing_dev_info mtd_bdi_rw_mappable = {
52 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
53 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
54 BDI_CAP_WRITE_MAP),
55};
Ben Dooks356d70f2007-05-28 20:28:34 +010056
David Woodhouse15bce402009-04-05 07:40:58 -070057static int mtd_cls_suspend(struct device *dev, pm_message_t state);
58static int mtd_cls_resume(struct device *dev);
David Brownell1f24b5a2009-03-26 00:42:41 -070059
David Woodhouse15bce402009-04-05 07:40:58 -070060static struct class mtd_class = {
61 .name = "mtd",
62 .owner = THIS_MODULE,
63 .suspend = mtd_cls_suspend,
64 .resume = mtd_cls_resume,
65};
David Brownell1f24b5a2009-03-26 00:42:41 -070066
Ben Hutchingsb520e412010-01-29 20:59:42 +000067static DEFINE_IDR(mtd_idr);
68
Thomas Gleixner97894cd2005-11-07 11:15:26 +000069/* These are exported solely for the purpose of mtd_blkdevs.c. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 should not use them for _anything_ else */
Ingo Molnar48b19262006-03-31 02:29:41 -080071DEFINE_MUTEX(mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072EXPORT_SYMBOL_GPL(mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +000073
74struct mtd_info *__mtd_next_device(int i)
75{
76 return idr_get_next(&mtd_idr, &i);
77}
78EXPORT_SYMBOL_GPL(__mtd_next_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80static LIST_HEAD(mtd_notifiers);
81
David Brownell1f24b5a2009-03-26 00:42:41 -070082
83#if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
84#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
85#else
86#define MTD_DEVT(index) 0
87#endif
88
89/* REVISIT once MTD uses the driver model better, whoever allocates
90 * the mtd_info will probably want to use the release() hook...
91 */
92static void mtd_release(struct device *dev)
93{
Denis V. Lunev2fdb1142009-04-12 08:14:46 +010094 dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
David Brownell1f24b5a2009-03-26 00:42:41 -070095
96 /* remove /dev/mtdXro node if needed */
Denis V. Lunev2fdb1142009-04-12 08:14:46 +010097 if (index)
David Woodhouse15bce402009-04-05 07:40:58 -070098 device_destroy(&mtd_class, index + 1);
99}
100
101static int mtd_cls_suspend(struct device *dev, pm_message_t state)
102{
103 struct mtd_info *mtd = dev_to_mtd(dev);
Saeed Bishara6afc4fd2009-07-28 04:56:43 -0700104
105 if (mtd && mtd->suspend)
David Woodhouse15bce402009-04-05 07:40:58 -0700106 return mtd->suspend(mtd);
107 else
108 return 0;
109}
110
111static int mtd_cls_resume(struct device *dev)
112{
113 struct mtd_info *mtd = dev_to_mtd(dev);
114
Saeed Bishara6afc4fd2009-07-28 04:56:43 -0700115 if (mtd && mtd->resume)
David Woodhouse15bce402009-04-05 07:40:58 -0700116 mtd->resume(mtd);
117 return 0;
David Brownell1f24b5a2009-03-26 00:42:41 -0700118}
119
120static ssize_t mtd_type_show(struct device *dev,
121 struct device_attribute *attr, char *buf)
122{
123 struct mtd_info *mtd = dev_to_mtd(dev);
124 char *type;
125
126 switch (mtd->type) {
127 case MTD_ABSENT:
128 type = "absent";
129 break;
130 case MTD_RAM:
131 type = "ram";
132 break;
133 case MTD_ROM:
134 type = "rom";
135 break;
136 case MTD_NORFLASH:
137 type = "nor";
138 break;
139 case MTD_NANDFLASH:
140 type = "nand";
141 break;
142 case MTD_DATAFLASH:
143 type = "dataflash";
144 break;
145 case MTD_UBIVOLUME:
146 type = "ubi";
147 break;
148 default:
149 type = "unknown";
150 }
151
152 return snprintf(buf, PAGE_SIZE, "%s\n", type);
153}
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700154static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
155
156static ssize_t mtd_flags_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, "0x%lx\n", (unsigned long)mtd->flags);
162
163}
164static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
165
166static ssize_t mtd_size_show(struct device *dev,
167 struct device_attribute *attr, char *buf)
168{
169 struct mtd_info *mtd = dev_to_mtd(dev);
170
171 return snprintf(buf, PAGE_SIZE, "%llu\n",
172 (unsigned long long)mtd->size);
173
174}
175static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
176
177static ssize_t mtd_erasesize_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
179{
180 struct mtd_info *mtd = dev_to_mtd(dev);
181
182 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
183
184}
185static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
186
187static ssize_t mtd_writesize_show(struct device *dev,
188 struct device_attribute *attr, char *buf)
189{
190 struct mtd_info *mtd = dev_to_mtd(dev);
191
192 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
193
194}
195static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
196
Artem Bityutskiye7693542009-04-18 12:29:42 +0300197static ssize_t mtd_subpagesize_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
199{
200 struct mtd_info *mtd = dev_to_mtd(dev);
201 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
202
203 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
204
205}
206static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
207
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700208static ssize_t mtd_oobsize_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
210{
211 struct mtd_info *mtd = dev_to_mtd(dev);
212
213 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
214
215}
216static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
217
218static ssize_t mtd_numeraseregions_show(struct device *dev,
219 struct device_attribute *attr, char *buf)
220{
221 struct mtd_info *mtd = dev_to_mtd(dev);
222
223 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
224
225}
226static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
227 NULL);
228
229static ssize_t mtd_name_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
231{
232 struct mtd_info *mtd = dev_to_mtd(dev);
233
234 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
235
236}
237static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
David Brownell1f24b5a2009-03-26 00:42:41 -0700238
239static struct attribute *mtd_attrs[] = {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700240 &dev_attr_type.attr,
241 &dev_attr_flags.attr,
242 &dev_attr_size.attr,
243 &dev_attr_erasesize.attr,
244 &dev_attr_writesize.attr,
Artem Bityutskiye7693542009-04-18 12:29:42 +0300245 &dev_attr_subpagesize.attr,
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700246 &dev_attr_oobsize.attr,
247 &dev_attr_numeraseregions.attr,
248 &dev_attr_name.attr,
David Brownell1f24b5a2009-03-26 00:42:41 -0700249 NULL,
250};
251
H Hartley Sweetenfca91082009-08-06 16:05:32 -0700252static struct attribute_group mtd_group = {
David Brownell1f24b5a2009-03-26 00:42:41 -0700253 .attrs = mtd_attrs,
254};
255
David Woodhouse6469f542009-09-20 05:55:36 -0700256static const struct attribute_group *mtd_groups[] = {
David Brownell1f24b5a2009-03-26 00:42:41 -0700257 &mtd_group,
258 NULL,
259};
260
261static struct device_type mtd_devtype = {
262 .name = "mtd",
263 .groups = mtd_groups,
264 .release = mtd_release,
265};
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/**
268 * add_mtd_device - register an MTD device
269 * @mtd: pointer to new MTD device info structure
270 *
271 * Add a device to the list of MTD devices present in the system, and
272 * notify each currently active MTD 'user' of its arrival. Returns
273 * zero on success or 1 on failure, which currently will only happen
Ben Hutchingsb520e412010-01-29 20:59:42 +0000274 * if there is insufficient memory or a sysfs error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
276
277int add_mtd_device(struct mtd_info *mtd)
278{
Ben Hutchingsb520e412010-01-29 20:59:42 +0000279 struct mtd_notifier *not;
280 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
David Howells402d3262009-02-12 10:40:00 +0000282 if (!mtd->backing_dev_info) {
283 switch (mtd->type) {
284 case MTD_RAM:
285 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
286 break;
287 case MTD_ROM:
288 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
289 break;
290 default:
291 mtd->backing_dev_info = &mtd_bdi_unmappable;
292 break;
293 }
294 }
295
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400296 BUG_ON(mtd->writesize == 0);
Ingo Molnar48b19262006-03-31 02:29:41 -0800297 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Ben Hutchingsb520e412010-01-29 20:59:42 +0000299 do {
300 if (!idr_pre_get(&mtd_idr, GFP_KERNEL))
301 goto fail_locked;
302 error = idr_get_new(&mtd_idr, mtd, &i);
303 } while (error == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Ben Hutchingsb520e412010-01-29 20:59:42 +0000305 if (error)
306 goto fail_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Ben Hutchingsb520e412010-01-29 20:59:42 +0000308 mtd->index = i;
309 mtd->usecount = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000310
Ben Hutchingsb520e412010-01-29 20:59:42 +0000311 if (is_power_of_2(mtd->erasesize))
312 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
313 else
314 mtd->erasesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000315
Ben Hutchingsb520e412010-01-29 20:59:42 +0000316 if (is_power_of_2(mtd->writesize))
317 mtd->writesize_shift = ffs(mtd->writesize) - 1;
318 else
319 mtd->writesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000320
Ben Hutchingsb520e412010-01-29 20:59:42 +0000321 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
322 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
Håvard Skinnemoen187ef152006-09-22 10:07:08 +0100323
Ben Hutchingsb520e412010-01-29 20:59:42 +0000324 /* Some chips always power up locked. Unlock them now */
325 if ((mtd->flags & MTD_WRITEABLE)
326 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
327 if (mtd->unlock(mtd, 0, mtd->size))
328 printk(KERN_WARNING
329 "%s: unlock failed, writes may not work\n",
330 mtd->name);
331 }
David Brownell1f24b5a2009-03-26 00:42:41 -0700332
Ben Hutchingsb520e412010-01-29 20:59:42 +0000333 /* Caller should have set dev.parent to match the
334 * physical device.
335 */
336 mtd->dev.type = &mtd_devtype;
337 mtd->dev.class = &mtd_class;
338 mtd->dev.devt = MTD_DEVT(i);
339 dev_set_name(&mtd->dev, "mtd%d", i);
340 dev_set_drvdata(&mtd->dev, mtd);
341 if (device_register(&mtd->dev) != 0)
342 goto fail_added;
David Brownell1f24b5a2009-03-26 00:42:41 -0700343
Ben Hutchingsb520e412010-01-29 20:59:42 +0000344 if (MTD_DEVT(i))
345 device_create(&mtd_class, mtd->dev.parent,
346 MTD_DEVT(i) + 1,
347 NULL, "mtd%dro", i);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000348
Ben Hutchingsb520e412010-01-29 20:59:42 +0000349 DEBUG(0, "mtd: Giving out device %d to %s\n", i, mtd->name);
350 /* No need to get a refcount on the module containing
351 the notifier, since we hold the mtd_table_mutex */
352 list_for_each_entry(not, &mtd_notifiers, list)
353 not->add(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000354
Ingo Molnar48b19262006-03-31 02:29:41 -0800355 mutex_unlock(&mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000356 /* We _know_ we aren't being removed, because
357 our caller is still holding us here. So none
358 of this try_ nonsense, and no bitching about it
359 either. :) */
360 __module_get(THIS_MODULE);
361 return 0;
362
363fail_added:
364 idr_remove(&mtd_idr, i);
365fail_locked:
366 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 1;
368}
369
370/**
371 * del_mtd_device - unregister an MTD device
372 * @mtd: pointer to MTD device info structure
373 *
374 * Remove a device from the list of MTD devices present in the system,
375 * and notify each currently active MTD 'user' of its departure.
376 * Returns zero on success or 1 on failure, which currently will happen
377 * if the requested device does not appear to be present in the list.
378 */
379
380int del_mtd_device (struct mtd_info *mtd)
381{
382 int ret;
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200383 struct mtd_notifier *not;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000384
Ingo Molnar48b19262006-03-31 02:29:41 -0800385 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Ben Hutchingsb520e412010-01-29 20:59:42 +0000387 if (idr_find(&mtd_idr, mtd->index) != mtd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 ret = -ENODEV;
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200389 goto out_error;
390 }
391
392 /* No need to get a refcount on the module containing
393 the notifier, since we hold the mtd_table_mutex */
394 list_for_each_entry(not, &mtd_notifiers, list)
395 not->remove(mtd);
396
397 if (mtd->usecount) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000398 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 mtd->index, mtd->name, mtd->usecount);
400 ret = -EBUSY;
401 } else {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700402 device_unregister(&mtd->dev);
403
Ben Hutchingsb520e412010-01-29 20:59:42 +0000404 idr_remove(&mtd_idr, mtd->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 module_put(THIS_MODULE);
407 ret = 0;
408 }
409
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200410out_error:
Ingo Molnar48b19262006-03-31 02:29:41 -0800411 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return ret;
413}
414
415/**
416 * register_mtd_user - register a 'user' of MTD devices.
417 * @new: pointer to notifier info structure
418 *
419 * Registers a pair of callbacks function to be called upon addition
420 * or removal of MTD devices. Causes the 'add' callback to be immediately
421 * invoked for each MTD device currently present in the system.
422 */
423
424void register_mtd_user (struct mtd_notifier *new)
425{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000426 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Ingo Molnar48b19262006-03-31 02:29:41 -0800428 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 list_add(&new->list, &mtd_notifiers);
431
432 __module_get(THIS_MODULE);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000433
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000434 mtd_for_each_device(mtd)
435 new->add(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Ingo Molnar48b19262006-03-31 02:29:41 -0800437 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440/**
Artem B. Bityuckiy49450792005-02-18 14:34:54 +0000441 * unregister_mtd_user - unregister a 'user' of MTD devices.
442 * @old: pointer to notifier info structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 *
444 * Removes a callback function pair from the list of 'users' to be
445 * notified upon addition or removal of MTD devices. Causes the
446 * 'remove' callback to be immediately invoked for each MTD device
447 * currently present in the system.
448 */
449
450int unregister_mtd_user (struct mtd_notifier *old)
451{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000452 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Ingo Molnar48b19262006-03-31 02:29:41 -0800454 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 module_put(THIS_MODULE);
457
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000458 mtd_for_each_device(mtd)
459 old->remove(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 list_del(&old->list);
Ingo Molnar48b19262006-03-31 02:29:41 -0800462 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return 0;
464}
465
466
467/**
468 * get_mtd_device - obtain a validated handle for an MTD device
469 * @mtd: last known address of the required MTD device
470 * @num: internal device number of the required MTD device
471 *
472 * Given a number and NULL address, return the num'th entry in the device
473 * table, if any. Given an address and num == -1, search the device table
474 * for a device with that address and return if it's still present. Given
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300475 * both, return the num'th driver only if its address matches. Return
476 * error code if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
480{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000481 struct mtd_info *ret = NULL, *other;
482 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Ingo Molnar48b19262006-03-31 02:29:41 -0800484 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (num == -1) {
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000487 mtd_for_each_device(other) {
488 if (other == mtd) {
489 ret = mtd;
490 break;
491 }
492 }
Ben Hutchingsb520e412010-01-29 20:59:42 +0000493 } else if (num >= 0) {
494 ret = idr_find(&mtd_idr, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (mtd && mtd != ret)
496 ret = NULL;
497 }
498
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200499 if (!ret) {
500 ret = ERR_PTR(err);
501 goto out;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200504 err = __get_mtd_device(ret);
505 if (err)
506 ret = ERR_PTR(err);
507out:
Ingo Molnar48b19262006-03-31 02:29:41 -0800508 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return ret;
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200510}
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300511
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200512
513int __get_mtd_device(struct mtd_info *mtd)
514{
515 int err;
516
517 if (!try_module_get(mtd->owner))
518 return -ENODEV;
519
520 if (mtd->get_device) {
521
522 err = mtd->get_device(mtd);
523
524 if (err) {
525 module_put(mtd->owner);
526 return err;
527 }
528 }
529 mtd->usecount++;
530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Artem Bityutskiy77993082006-10-11 14:52:44 +0300533/**
534 * get_mtd_device_nm - obtain a validated handle for an MTD device by
535 * device name
536 * @name: MTD device name to open
537 *
538 * This function returns MTD device description structure in case of
539 * success and an error code in case of failure.
540 */
541
542struct mtd_info *get_mtd_device_nm(const char *name)
543{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000544 int err = -ENODEV;
545 struct mtd_info *mtd = NULL, *other;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300546
547 mutex_lock(&mtd_table_mutex);
548
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000549 mtd_for_each_device(other) {
550 if (!strcmp(name, other->name)) {
551 mtd = other;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300552 break;
553 }
554 }
555
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300556 if (!mtd)
Artem Bityutskiy77993082006-10-11 14:52:44 +0300557 goto out_unlock;
558
559 if (!try_module_get(mtd->owner))
560 goto out_unlock;
561
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300562 if (mtd->get_device) {
563 err = mtd->get_device(mtd);
564 if (err)
565 goto out_put;
566 }
Artem Bityutskiy77993082006-10-11 14:52:44 +0300567
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300568 mtd->usecount++;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300569 mutex_unlock(&mtd_table_mutex);
570 return mtd;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300571
572out_put:
573 module_put(mtd->owner);
574out_unlock:
575 mutex_unlock(&mtd_table_mutex);
576 return ERR_PTR(err);
Artem Bityutskiy77993082006-10-11 14:52:44 +0300577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579void put_mtd_device(struct mtd_info *mtd)
580{
Ingo Molnar48b19262006-03-31 02:29:41 -0800581 mutex_lock(&mtd_table_mutex);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200582 __put_mtd_device(mtd);
583 mutex_unlock(&mtd_table_mutex);
584
585}
586
587void __put_mtd_device(struct mtd_info *mtd)
588{
589 --mtd->usecount;
590 BUG_ON(mtd->usecount < 0);
591
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300592 if (mtd->put_device)
593 mtd->put_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 module_put(mtd->owner);
596}
597
598/* default_mtd_writev - default mtd writev method for MTD devices that
David Woodhousedd36f262006-11-29 16:33:03 +0000599 * don't implement their own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
601
602int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
603 unsigned long count, loff_t to, size_t *retlen)
604{
605 unsigned long i;
606 size_t totlen = 0, thislen;
607 int ret = 0;
608
609 if(!mtd->write) {
610 ret = -EROFS;
611 } else {
612 for (i=0; i<count; i++) {
613 if (!vecs[i].iov_len)
614 continue;
615 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
616 totlen += thislen;
617 if (ret || thislen != vecs[i].iov_len)
618 break;
619 to += vecs[i].iov_len;
620 }
621 }
622 if (retlen)
623 *retlen = totlen;
624 return ret;
625}
626
David Woodhousedd36f262006-11-29 16:33:03 +0000627EXPORT_SYMBOL_GPL(add_mtd_device);
628EXPORT_SYMBOL_GPL(del_mtd_device);
629EXPORT_SYMBOL_GPL(get_mtd_device);
630EXPORT_SYMBOL_GPL(get_mtd_device_nm);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200631EXPORT_SYMBOL_GPL(__get_mtd_device);
David Woodhousedd36f262006-11-29 16:33:03 +0000632EXPORT_SYMBOL_GPL(put_mtd_device);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200633EXPORT_SYMBOL_GPL(__put_mtd_device);
David Woodhousedd36f262006-11-29 16:33:03 +0000634EXPORT_SYMBOL_GPL(register_mtd_user);
635EXPORT_SYMBOL_GPL(unregister_mtd_user);
636EXPORT_SYMBOL_GPL(default_mtd_writev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Pavel Machek2d2dce02006-03-31 02:29:49 -0800638#ifdef CONFIG_PROC_FS
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640/*====================================================================*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/* Support for /proc/mtd */
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643static struct proc_dir_entry *proc_mtd;
644
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000645static inline int mtd_proc_info(char *buf, struct mtd_info *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000647 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
Adrian Hunter69423d92008-12-10 13:37:21 +0000648 (unsigned long long)this->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 this->erasesize, this->name);
650}
651
652static int mtd_read_proc (char *page, char **start, off_t off, int count,
653 int *eof, void *data_unused)
654{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000655 struct mtd_info *mtd;
656 int len, l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 off_t begin = 0;
658
Ingo Molnar48b19262006-03-31 02:29:41 -0800659 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 len = sprintf(page, "dev: size erasesize name\n");
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000662 mtd_for_each_device(mtd) {
663 l = mtd_proc_info(page + len, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 len += l;
665 if (len+begin > off+count)
666 goto done;
667 if (len+begin < off) {
668 begin += len;
669 len = 0;
670 }
671 }
672
673 *eof = 1;
674
675done:
Ingo Molnar48b19262006-03-31 02:29:41 -0800676 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (off >= len+begin)
678 return 0;
679 *start = page + (off-begin);
680 return ((count < begin+len-off) ? count : begin+len-off);
681}
682
Kevin Cernekee45b09072009-04-04 11:03:04 -0700683#endif /* CONFIG_PROC_FS */
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/*====================================================================*/
686/* Init code */
687
Jens Axboe0661b1a2010-04-27 09:49:47 +0200688static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
689{
690 int ret;
691
692 ret = bdi_init(bdi);
693 if (!ret)
694 ret = bdi_register(bdi, NULL, name);
695
696 if (ret)
697 bdi_destroy(bdi);
698
699 return ret;
700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702static int __init init_mtd(void)
703{
David Woodhouse15bce402009-04-05 07:40:58 -0700704 int ret;
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700705
Jens Axboe0661b1a2010-04-27 09:49:47 +0200706 ret = class_register(&mtd_class);
707 if (ret)
708 goto err_reg;
709
710 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
711 if (ret)
712 goto err_bdi1;
713
714 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
715 if (ret)
716 goto err_bdi2;
717
718 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
719 if (ret)
720 goto err_bdi3;
721
Kevin Cernekee45b09072009-04-04 11:03:04 -0700722#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
724 proc_mtd->read_proc = mtd_read_proc;
Kevin Cernekee45b09072009-04-04 11:03:04 -0700725#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 0;
Jens Axboe0661b1a2010-04-27 09:49:47 +0200727
728err_bdi3:
729 bdi_destroy(&mtd_bdi_ro_mappable);
730err_bdi2:
731 bdi_destroy(&mtd_bdi_unmappable);
732err_bdi1:
733 class_unregister(&mtd_class);
734err_reg:
735 pr_err("Error registering mtd class or bdi: %d\n", ret);
736 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739static void __exit cleanup_mtd(void)
740{
Kevin Cernekee45b09072009-04-04 11:03:04 -0700741#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (proc_mtd)
743 remove_proc_entry( "mtd", NULL);
Kevin Cernekee45b09072009-04-04 11:03:04 -0700744#endif /* CONFIG_PROC_FS */
David Woodhouse15bce402009-04-05 07:40:58 -0700745 class_unregister(&mtd_class);
Jens Axboe0661b1a2010-04-27 09:49:47 +0200746 bdi_destroy(&mtd_bdi_unmappable);
747 bdi_destroy(&mtd_bdi_ro_mappable);
748 bdi_destroy(&mtd_bdi_rw_mappable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751module_init(init_mtd);
752module_exit(cleanup_mtd);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754MODULE_LICENSE("GPL");
755MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
756MODULE_DESCRIPTION("Core MTD registration and access routines");