blob: 402d41723c3fcbfce3f41400f943ea6c3ba7c0f4 [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 *
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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>
Artem Bityutskiy77993082006-10-11 14:52:44 +030015#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/ioctl.h>
17#include <linux/init.h>
18#include <linux/mtd/compatmac.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <linux/mtd/mtd.h>
David Howells402d3262009-02-12 10:40:00 +000022#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Ben Dooks356d70f2007-05-28 20:28:34 +010024#include "mtdcore.h"
25
David Woodhouse15bce402009-04-05 07:40:58 -070026static int mtd_cls_suspend(struct device *dev, pm_message_t state);
27static int mtd_cls_resume(struct device *dev);
David Brownell1f24b5a2009-03-26 00:42:41 -070028
David Woodhouse15bce402009-04-05 07:40:58 -070029static struct class mtd_class = {
30 .name = "mtd",
31 .owner = THIS_MODULE,
32 .suspend = mtd_cls_suspend,
33 .resume = mtd_cls_resume,
34};
David Brownell1f24b5a2009-03-26 00:42:41 -070035
Thomas Gleixner97894cd2005-11-07 11:15:26 +000036/* These are exported solely for the purpose of mtd_blkdevs.c. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 should not use them for _anything_ else */
Ingo Molnar48b19262006-03-31 02:29:41 -080038DEFINE_MUTEX(mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039struct mtd_info *mtd_table[MAX_MTD_DEVICES];
40
41EXPORT_SYMBOL_GPL(mtd_table_mutex);
42EXPORT_SYMBOL_GPL(mtd_table);
43
44static LIST_HEAD(mtd_notifiers);
45
David Brownell1f24b5a2009-03-26 00:42:41 -070046
47#if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
48#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
49#else
50#define MTD_DEVT(index) 0
51#endif
52
53/* REVISIT once MTD uses the driver model better, whoever allocates
54 * the mtd_info will probably want to use the release() hook...
55 */
56static void mtd_release(struct device *dev)
57{
Denis V. Lunev2fdb1142009-04-12 08:14:46 +010058 dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
David Brownell1f24b5a2009-03-26 00:42:41 -070059
60 /* remove /dev/mtdXro node if needed */
Denis V. Lunev2fdb1142009-04-12 08:14:46 +010061 if (index)
David Woodhouse15bce402009-04-05 07:40:58 -070062 device_destroy(&mtd_class, index + 1);
63}
64
65static int mtd_cls_suspend(struct device *dev, pm_message_t state)
66{
67 struct mtd_info *mtd = dev_to_mtd(dev);
Saeed Bishara6afc4fd2009-07-28 04:56:43 -070068
69 if (mtd && mtd->suspend)
David Woodhouse15bce402009-04-05 07:40:58 -070070 return mtd->suspend(mtd);
71 else
72 return 0;
73}
74
75static int mtd_cls_resume(struct device *dev)
76{
77 struct mtd_info *mtd = dev_to_mtd(dev);
78
Saeed Bishara6afc4fd2009-07-28 04:56:43 -070079 if (mtd && mtd->resume)
David Woodhouse15bce402009-04-05 07:40:58 -070080 mtd->resume(mtd);
81 return 0;
David Brownell1f24b5a2009-03-26 00:42:41 -070082}
83
84static ssize_t mtd_type_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
86{
87 struct mtd_info *mtd = dev_to_mtd(dev);
88 char *type;
89
90 switch (mtd->type) {
91 case MTD_ABSENT:
92 type = "absent";
93 break;
94 case MTD_RAM:
95 type = "ram";
96 break;
97 case MTD_ROM:
98 type = "rom";
99 break;
100 case MTD_NORFLASH:
101 type = "nor";
102 break;
103 case MTD_NANDFLASH:
104 type = "nand";
105 break;
106 case MTD_DATAFLASH:
107 type = "dataflash";
108 break;
109 case MTD_UBIVOLUME:
110 type = "ubi";
111 break;
112 default:
113 type = "unknown";
114 }
115
116 return snprintf(buf, PAGE_SIZE, "%s\n", type);
117}
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700118static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
119
120static ssize_t mtd_flags_show(struct device *dev,
121 struct device_attribute *attr, char *buf)
122{
123 struct mtd_info *mtd = dev_to_mtd(dev);
124
125 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
126
127}
128static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
129
130static ssize_t mtd_size_show(struct device *dev,
131 struct device_attribute *attr, char *buf)
132{
133 struct mtd_info *mtd = dev_to_mtd(dev);
134
135 return snprintf(buf, PAGE_SIZE, "%llu\n",
136 (unsigned long long)mtd->size);
137
138}
139static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
140
141static ssize_t mtd_erasesize_show(struct device *dev,
142 struct device_attribute *attr, char *buf)
143{
144 struct mtd_info *mtd = dev_to_mtd(dev);
145
146 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
147
148}
149static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
150
151static ssize_t mtd_writesize_show(struct device *dev,
152 struct device_attribute *attr, char *buf)
153{
154 struct mtd_info *mtd = dev_to_mtd(dev);
155
156 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
157
158}
159static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
160
Artem Bityutskiye7693542009-04-18 12:29:42 +0300161static ssize_t mtd_subpagesize_show(struct device *dev,
162 struct device_attribute *attr, char *buf)
163{
164 struct mtd_info *mtd = dev_to_mtd(dev);
165 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
166
167 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
168
169}
170static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
171
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700172static ssize_t mtd_oobsize_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
174{
175 struct mtd_info *mtd = dev_to_mtd(dev);
176
177 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
178
179}
180static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
181
182static ssize_t mtd_numeraseregions_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
184{
185 struct mtd_info *mtd = dev_to_mtd(dev);
186
187 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
188
189}
190static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
191 NULL);
192
193static ssize_t mtd_name_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
195{
196 struct mtd_info *mtd = dev_to_mtd(dev);
197
198 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
199
200}
201static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
David Brownell1f24b5a2009-03-26 00:42:41 -0700202
203static struct attribute *mtd_attrs[] = {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700204 &dev_attr_type.attr,
205 &dev_attr_flags.attr,
206 &dev_attr_size.attr,
207 &dev_attr_erasesize.attr,
208 &dev_attr_writesize.attr,
Artem Bityutskiye7693542009-04-18 12:29:42 +0300209 &dev_attr_subpagesize.attr,
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700210 &dev_attr_oobsize.attr,
211 &dev_attr_numeraseregions.attr,
212 &dev_attr_name.attr,
David Brownell1f24b5a2009-03-26 00:42:41 -0700213 NULL,
214};
215
H Hartley Sweetenfca91082009-08-06 16:05:32 -0700216static struct attribute_group mtd_group = {
David Brownell1f24b5a2009-03-26 00:42:41 -0700217 .attrs = mtd_attrs,
218};
219
David Woodhouse6469f542009-09-20 05:55:36 -0700220static const struct attribute_group *mtd_groups[] = {
David Brownell1f24b5a2009-03-26 00:42:41 -0700221 &mtd_group,
222 NULL,
223};
224
225static struct device_type mtd_devtype = {
226 .name = "mtd",
227 .groups = mtd_groups,
228 .release = mtd_release,
229};
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/**
232 * add_mtd_device - register an MTD device
233 * @mtd: pointer to new MTD device info structure
234 *
235 * Add a device to the list of MTD devices present in the system, and
236 * notify each currently active MTD 'user' of its arrival. Returns
237 * zero on success or 1 on failure, which currently will only happen
238 * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
David Brownell1f24b5a2009-03-26 00:42:41 -0700239 * or there's a sysfs error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241
242int add_mtd_device(struct mtd_info *mtd)
243{
244 int i;
245
David Howells402d3262009-02-12 10:40:00 +0000246 if (!mtd->backing_dev_info) {
247 switch (mtd->type) {
248 case MTD_RAM:
249 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
250 break;
251 case MTD_ROM:
252 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
253 break;
254 default:
255 mtd->backing_dev_info = &mtd_bdi_unmappable;
256 break;
257 }
258 }
259
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400260 BUG_ON(mtd->writesize == 0);
Ingo Molnar48b19262006-03-31 02:29:41 -0800261 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 for (i=0; i < MAX_MTD_DEVICES; i++)
264 if (!mtd_table[i]) {
matthias@kaehlcke.net2606c792008-05-31 15:28:09 +0200265 struct mtd_notifier *not;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 mtd_table[i] = mtd;
268 mtd->index = i;
269 mtd->usecount = 0;
270
Adrian Hunter69423d92008-12-10 13:37:21 +0000271 if (is_power_of_2(mtd->erasesize))
272 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
273 else
274 mtd->erasesize_shift = 0;
275
276 if (is_power_of_2(mtd->writesize))
277 mtd->writesize_shift = ffs(mtd->writesize) - 1;
278 else
279 mtd->writesize_shift = 0;
280
281 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
282 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
283
HÃ¥vard Skinnemoen187ef152006-09-22 10:07:08 +0100284 /* Some chips always power up locked. Unlock them now */
285 if ((mtd->flags & MTD_WRITEABLE)
Justin Treone619a752008-01-30 10:25:49 -0800286 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
HÃ¥vard Skinnemoen187ef152006-09-22 10:07:08 +0100287 if (mtd->unlock(mtd, 0, mtd->size))
288 printk(KERN_WARNING
289 "%s: unlock failed, "
290 "writes may not work\n",
291 mtd->name);
292 }
293
David Brownell1f24b5a2009-03-26 00:42:41 -0700294 /* Caller should have set dev.parent to match the
295 * physical device.
296 */
297 mtd->dev.type = &mtd_devtype;
David Woodhouse15bce402009-04-05 07:40:58 -0700298 mtd->dev.class = &mtd_class;
David Brownell1f24b5a2009-03-26 00:42:41 -0700299 mtd->dev.devt = MTD_DEVT(i);
300 dev_set_name(&mtd->dev, "mtd%d", i);
Saeed Bishara6afc4fd2009-07-28 04:56:43 -0700301 dev_set_drvdata(&mtd->dev, mtd);
David Brownell1f24b5a2009-03-26 00:42:41 -0700302 if (device_register(&mtd->dev) != 0) {
303 mtd_table[i] = NULL;
304 break;
305 }
306
307 if (MTD_DEVT(i))
David Woodhouse15bce402009-04-05 07:40:58 -0700308 device_create(&mtd_class, mtd->dev.parent,
David Brownell1f24b5a2009-03-26 00:42:41 -0700309 MTD_DEVT(i) + 1,
310 NULL, "mtd%dro", i);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
313 /* No need to get a refcount on the module containing
314 the notifier, since we hold the mtd_table_mutex */
Chris Malley71a928c2008-05-19 20:11:50 +0100315 list_for_each_entry(not, &mtd_notifiers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 not->add(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000317
Ingo Molnar48b19262006-03-31 02:29:41 -0800318 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* We _know_ we aren't being removed, because
320 our caller is still holding us here. So none
321 of this try_ nonsense, and no bitching about it
322 either. :) */
323 __module_get(THIS_MODULE);
324 return 0;
325 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000326
Ingo Molnar48b19262006-03-31 02:29:41 -0800327 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 1;
329}
330
331/**
332 * del_mtd_device - unregister an MTD device
333 * @mtd: pointer to MTD device info structure
334 *
335 * Remove a device from the list of MTD devices present in the system,
336 * and notify each currently active MTD 'user' of its departure.
337 * Returns zero on success or 1 on failure, which currently will happen
338 * if the requested device does not appear to be present in the list.
339 */
340
341int del_mtd_device (struct mtd_info *mtd)
342{
343 int ret;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000344
Ingo Molnar48b19262006-03-31 02:29:41 -0800345 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (mtd_table[mtd->index] != mtd) {
348 ret = -ENODEV;
349 } else if (mtd->usecount) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000350 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 mtd->index, mtd->name, mtd->usecount);
352 ret = -EBUSY;
353 } else {
matthias@kaehlcke.net856613c2008-05-31 15:28:10 +0200354 struct mtd_notifier *not;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700356 device_unregister(&mtd->dev);
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /* No need to get a refcount on the module containing
359 the notifier, since we hold the mtd_table_mutex */
Chris Malley71a928c2008-05-19 20:11:50 +0100360 list_for_each_entry(not, &mtd_notifiers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 not->remove(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 mtd_table[mtd->index] = NULL;
364
365 module_put(THIS_MODULE);
366 ret = 0;
367 }
368
Ingo Molnar48b19262006-03-31 02:29:41 -0800369 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return ret;
371}
372
373/**
374 * register_mtd_user - register a 'user' of MTD devices.
375 * @new: pointer to notifier info structure
376 *
377 * Registers a pair of callbacks function to be called upon addition
378 * or removal of MTD devices. Causes the 'add' callback to be immediately
379 * invoked for each MTD device currently present in the system.
380 */
381
382void register_mtd_user (struct mtd_notifier *new)
383{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000384 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Ingo Molnar48b19262006-03-31 02:29:41 -0800386 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 list_add(&new->list, &mtd_notifiers);
389
390 __module_get(THIS_MODULE);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000391
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000392 mtd_for_each_device(mtd)
393 new->add(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Ingo Molnar48b19262006-03-31 02:29:41 -0800395 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398/**
Artem B. Bityuckiy49450792005-02-18 14:34:54 +0000399 * unregister_mtd_user - unregister a 'user' of MTD devices.
400 * @old: pointer to notifier info structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 *
402 * Removes a callback function pair from the list of 'users' to be
403 * notified upon addition or removal of MTD devices. Causes the
404 * 'remove' callback to be immediately invoked for each MTD device
405 * currently present in the system.
406 */
407
408int unregister_mtd_user (struct mtd_notifier *old)
409{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000410 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Ingo Molnar48b19262006-03-31 02:29:41 -0800412 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 module_put(THIS_MODULE);
415
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000416 mtd_for_each_device(mtd)
417 old->remove(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 list_del(&old->list);
Ingo Molnar48b19262006-03-31 02:29:41 -0800420 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 0;
422}
423
424
425/**
426 * get_mtd_device - obtain a validated handle for an MTD device
427 * @mtd: last known address of the required MTD device
428 * @num: internal device number of the required MTD device
429 *
430 * Given a number and NULL address, return the num'th entry in the device
431 * table, if any. Given an address and num == -1, search the device table
432 * for a device with that address and return if it's still present. Given
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300433 * both, return the num'th driver only if its address matches. Return
434 * error code if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
438{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000439 struct mtd_info *ret = NULL, *other;
440 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Ingo Molnar48b19262006-03-31 02:29:41 -0800442 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (num == -1) {
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000445 mtd_for_each_device(other) {
446 if (other == mtd) {
447 ret = mtd;
448 break;
449 }
450 }
Roel Kluin35016dd2009-11-03 20:49:18 +0100451 } else if (num >= 0 && num < MAX_MTD_DEVICES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 ret = mtd_table[num];
453 if (mtd && mtd != ret)
454 ret = NULL;
455 }
456
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300457 if (!ret)
458 goto out_unlock;
459
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300460 if (!try_module_get(ret->owner))
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300461 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300463 if (ret->get_device) {
464 err = ret->get_device(ret);
465 if (err)
466 goto out_put;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300469 ret->usecount++;
Ingo Molnar48b19262006-03-31 02:29:41 -0800470 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return ret;
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300472
473out_put:
474 module_put(ret->owner);
475out_unlock:
476 mutex_unlock(&mtd_table_mutex);
477 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Artem Bityutskiy77993082006-10-11 14:52:44 +0300480/**
481 * get_mtd_device_nm - obtain a validated handle for an MTD device by
482 * device name
483 * @name: MTD device name to open
484 *
485 * This function returns MTD device description structure in case of
486 * success and an error code in case of failure.
487 */
488
489struct mtd_info *get_mtd_device_nm(const char *name)
490{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000491 int err = -ENODEV;
492 struct mtd_info *mtd = NULL, *other;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300493
494 mutex_lock(&mtd_table_mutex);
495
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000496 mtd_for_each_device(other) {
497 if (!strcmp(name, other->name)) {
498 mtd = other;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300499 break;
500 }
501 }
502
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300503 if (!mtd)
Artem Bityutskiy77993082006-10-11 14:52:44 +0300504 goto out_unlock;
505
506 if (!try_module_get(mtd->owner))
507 goto out_unlock;
508
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300509 if (mtd->get_device) {
510 err = mtd->get_device(mtd);
511 if (err)
512 goto out_put;
513 }
Artem Bityutskiy77993082006-10-11 14:52:44 +0300514
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300515 mtd->usecount++;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300516 mutex_unlock(&mtd_table_mutex);
517 return mtd;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300518
519out_put:
520 module_put(mtd->owner);
521out_unlock:
522 mutex_unlock(&mtd_table_mutex);
523 return ERR_PTR(err);
Artem Bityutskiy77993082006-10-11 14:52:44 +0300524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526void put_mtd_device(struct mtd_info *mtd)
527{
528 int c;
529
Ingo Molnar48b19262006-03-31 02:29:41 -0800530 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 c = --mtd->usecount;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300532 if (mtd->put_device)
533 mtd->put_device(mtd);
Ingo Molnar48b19262006-03-31 02:29:41 -0800534 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 BUG_ON(c < 0);
536
537 module_put(mtd->owner);
538}
539
540/* default_mtd_writev - default mtd writev method for MTD devices that
David Woodhousedd36f262006-11-29 16:33:03 +0000541 * don't implement their own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
543
544int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
545 unsigned long count, loff_t to, size_t *retlen)
546{
547 unsigned long i;
548 size_t totlen = 0, thislen;
549 int ret = 0;
550
551 if(!mtd->write) {
552 ret = -EROFS;
553 } else {
554 for (i=0; i<count; i++) {
555 if (!vecs[i].iov_len)
556 continue;
557 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
558 totlen += thislen;
559 if (ret || thislen != vecs[i].iov_len)
560 break;
561 to += vecs[i].iov_len;
562 }
563 }
564 if (retlen)
565 *retlen = totlen;
566 return ret;
567}
568
David Woodhousedd36f262006-11-29 16:33:03 +0000569EXPORT_SYMBOL_GPL(add_mtd_device);
570EXPORT_SYMBOL_GPL(del_mtd_device);
571EXPORT_SYMBOL_GPL(get_mtd_device);
572EXPORT_SYMBOL_GPL(get_mtd_device_nm);
573EXPORT_SYMBOL_GPL(put_mtd_device);
574EXPORT_SYMBOL_GPL(register_mtd_user);
575EXPORT_SYMBOL_GPL(unregister_mtd_user);
576EXPORT_SYMBOL_GPL(default_mtd_writev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Pavel Machek2d2dce02006-03-31 02:29:49 -0800578#ifdef CONFIG_PROC_FS
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/*====================================================================*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/* Support for /proc/mtd */
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static struct proc_dir_entry *proc_mtd;
584
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000585static inline int mtd_proc_info(char *buf, struct mtd_info *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000587 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
Adrian Hunter69423d92008-12-10 13:37:21 +0000588 (unsigned long long)this->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 this->erasesize, this->name);
590}
591
592static int mtd_read_proc (char *page, char **start, off_t off, int count,
593 int *eof, void *data_unused)
594{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000595 struct mtd_info *mtd;
596 int len, l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 off_t begin = 0;
598
Ingo Molnar48b19262006-03-31 02:29:41 -0800599 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 len = sprintf(page, "dev: size erasesize name\n");
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000602 mtd_for_each_device(mtd) {
603 l = mtd_proc_info(page + len, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 len += l;
605 if (len+begin > off+count)
606 goto done;
607 if (len+begin < off) {
608 begin += len;
609 len = 0;
610 }
611 }
612
613 *eof = 1;
614
615done:
Ingo Molnar48b19262006-03-31 02:29:41 -0800616 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (off >= len+begin)
618 return 0;
619 *start = page + (off-begin);
620 return ((count < begin+len-off) ? count : begin+len-off);
621}
622
Kevin Cernekee45b09072009-04-04 11:03:04 -0700623#endif /* CONFIG_PROC_FS */
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/*====================================================================*/
626/* Init code */
627
628static int __init init_mtd(void)
629{
David Woodhouse15bce402009-04-05 07:40:58 -0700630 int ret;
631 ret = class_register(&mtd_class);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700632
David Woodhouse15bce402009-04-05 07:40:58 -0700633 if (ret) {
634 pr_err("Error registering mtd class: %d\n", ret);
635 return ret;
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700636 }
Kevin Cernekee45b09072009-04-04 11:03:04 -0700637#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
639 proc_mtd->read_proc = mtd_read_proc;
Kevin Cernekee45b09072009-04-04 11:03:04 -0700640#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 0;
642}
643
644static void __exit cleanup_mtd(void)
645{
Kevin Cernekee45b09072009-04-04 11:03:04 -0700646#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (proc_mtd)
648 remove_proc_entry( "mtd", NULL);
Kevin Cernekee45b09072009-04-04 11:03:04 -0700649#endif /* CONFIG_PROC_FS */
David Woodhouse15bce402009-04-05 07:40:58 -0700650 class_unregister(&mtd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653module_init(init_mtd);
654module_exit(cleanup_mtd);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656MODULE_LICENSE("GPL");
657MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
658MODULE_DESCRIPTION("Core MTD registration and access routines");