blob: 67669a76eaf5d028d56c55d70301daebb8f200bc [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>
Ben Hutchingsb520e412010-01-29 20:59:42 +000020#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <linux/mtd/mtd.h>
David Howells402d3262009-02-12 10:40:00 +000023#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ben Dooks356d70f2007-05-28 20:28:34 +010025#include "mtdcore.h"
26
David Woodhouse15bce402009-04-05 07:40:58 -070027static int mtd_cls_suspend(struct device *dev, pm_message_t state);
28static int mtd_cls_resume(struct device *dev);
David Brownell1f24b5a2009-03-26 00:42:41 -070029
David Woodhouse15bce402009-04-05 07:40:58 -070030static struct class mtd_class = {
31 .name = "mtd",
32 .owner = THIS_MODULE,
33 .suspend = mtd_cls_suspend,
34 .resume = mtd_cls_resume,
35};
David Brownell1f24b5a2009-03-26 00:42:41 -070036
Ben Hutchingsb520e412010-01-29 20:59:42 +000037static DEFINE_IDR(mtd_idr);
38
Thomas Gleixner97894cd2005-11-07 11:15:26 +000039/* These are exported solely for the purpose of mtd_blkdevs.c. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 should not use them for _anything_ else */
Ingo Molnar48b19262006-03-31 02:29:41 -080041DEFINE_MUTEX(mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042EXPORT_SYMBOL_GPL(mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +000043
44struct mtd_info *__mtd_next_device(int i)
45{
46 return idr_get_next(&mtd_idr, &i);
47}
48EXPORT_SYMBOL_GPL(__mtd_next_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static LIST_HEAD(mtd_notifiers);
51
David Brownell1f24b5a2009-03-26 00:42:41 -070052
53#if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
54#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
55#else
56#define MTD_DEVT(index) 0
57#endif
58
59/* REVISIT once MTD uses the driver model better, whoever allocates
60 * the mtd_info will probably want to use the release() hook...
61 */
62static void mtd_release(struct device *dev)
63{
Denis V. Lunev2fdb1142009-04-12 08:14:46 +010064 dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
David Brownell1f24b5a2009-03-26 00:42:41 -070065
66 /* remove /dev/mtdXro node if needed */
Denis V. Lunev2fdb1142009-04-12 08:14:46 +010067 if (index)
David Woodhouse15bce402009-04-05 07:40:58 -070068 device_destroy(&mtd_class, index + 1);
69}
70
71static int mtd_cls_suspend(struct device *dev, pm_message_t state)
72{
73 struct mtd_info *mtd = dev_to_mtd(dev);
Saeed Bishara6afc4fd2009-07-28 04:56:43 -070074
75 if (mtd && mtd->suspend)
David Woodhouse15bce402009-04-05 07:40:58 -070076 return mtd->suspend(mtd);
77 else
78 return 0;
79}
80
81static int mtd_cls_resume(struct device *dev)
82{
83 struct mtd_info *mtd = dev_to_mtd(dev);
84
Saeed Bishara6afc4fd2009-07-28 04:56:43 -070085 if (mtd && mtd->resume)
David Woodhouse15bce402009-04-05 07:40:58 -070086 mtd->resume(mtd);
87 return 0;
David Brownell1f24b5a2009-03-26 00:42:41 -070088}
89
90static ssize_t mtd_type_show(struct device *dev,
91 struct device_attribute *attr, char *buf)
92{
93 struct mtd_info *mtd = dev_to_mtd(dev);
94 char *type;
95
96 switch (mtd->type) {
97 case MTD_ABSENT:
98 type = "absent";
99 break;
100 case MTD_RAM:
101 type = "ram";
102 break;
103 case MTD_ROM:
104 type = "rom";
105 break;
106 case MTD_NORFLASH:
107 type = "nor";
108 break;
109 case MTD_NANDFLASH:
110 type = "nand";
111 break;
112 case MTD_DATAFLASH:
113 type = "dataflash";
114 break;
115 case MTD_UBIVOLUME:
116 type = "ubi";
117 break;
118 default:
119 type = "unknown";
120 }
121
122 return snprintf(buf, PAGE_SIZE, "%s\n", type);
123}
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700124static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
125
126static ssize_t mtd_flags_show(struct device *dev,
127 struct device_attribute *attr, char *buf)
128{
129 struct mtd_info *mtd = dev_to_mtd(dev);
130
131 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
132
133}
134static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
135
136static ssize_t mtd_size_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
138{
139 struct mtd_info *mtd = dev_to_mtd(dev);
140
141 return snprintf(buf, PAGE_SIZE, "%llu\n",
142 (unsigned long long)mtd->size);
143
144}
145static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
146
147static ssize_t mtd_erasesize_show(struct device *dev,
148 struct device_attribute *attr, char *buf)
149{
150 struct mtd_info *mtd = dev_to_mtd(dev);
151
152 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
153
154}
155static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
156
157static ssize_t mtd_writesize_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
159{
160 struct mtd_info *mtd = dev_to_mtd(dev);
161
162 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
163
164}
165static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
166
Artem Bityutskiye7693542009-04-18 12:29:42 +0300167static ssize_t mtd_subpagesize_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
169{
170 struct mtd_info *mtd = dev_to_mtd(dev);
171 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
172
173 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
174
175}
176static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
177
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700178static ssize_t mtd_oobsize_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
180{
181 struct mtd_info *mtd = dev_to_mtd(dev);
182
183 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
184
185}
186static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
187
188static ssize_t mtd_numeraseregions_show(struct device *dev,
189 struct device_attribute *attr, char *buf)
190{
191 struct mtd_info *mtd = dev_to_mtd(dev);
192
193 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
194
195}
196static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
197 NULL);
198
199static ssize_t mtd_name_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
201{
202 struct mtd_info *mtd = dev_to_mtd(dev);
203
204 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
205
206}
207static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
David Brownell1f24b5a2009-03-26 00:42:41 -0700208
209static struct attribute *mtd_attrs[] = {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700210 &dev_attr_type.attr,
211 &dev_attr_flags.attr,
212 &dev_attr_size.attr,
213 &dev_attr_erasesize.attr,
214 &dev_attr_writesize.attr,
Artem Bityutskiye7693542009-04-18 12:29:42 +0300215 &dev_attr_subpagesize.attr,
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700216 &dev_attr_oobsize.attr,
217 &dev_attr_numeraseregions.attr,
218 &dev_attr_name.attr,
David Brownell1f24b5a2009-03-26 00:42:41 -0700219 NULL,
220};
221
H Hartley Sweetenfca91082009-08-06 16:05:32 -0700222static struct attribute_group mtd_group = {
David Brownell1f24b5a2009-03-26 00:42:41 -0700223 .attrs = mtd_attrs,
224};
225
David Woodhouse6469f542009-09-20 05:55:36 -0700226static const struct attribute_group *mtd_groups[] = {
David Brownell1f24b5a2009-03-26 00:42:41 -0700227 &mtd_group,
228 NULL,
229};
230
231static struct device_type mtd_devtype = {
232 .name = "mtd",
233 .groups = mtd_groups,
234 .release = mtd_release,
235};
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/**
238 * add_mtd_device - register an MTD device
239 * @mtd: pointer to new MTD device info structure
240 *
241 * Add a device to the list of MTD devices present in the system, and
242 * notify each currently active MTD 'user' of its arrival. Returns
243 * zero on success or 1 on failure, which currently will only happen
Ben Hutchingsb520e412010-01-29 20:59:42 +0000244 * if there is insufficient memory or a sysfs error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246
247int add_mtd_device(struct mtd_info *mtd)
248{
Ben Hutchingsb520e412010-01-29 20:59:42 +0000249 struct mtd_notifier *not;
250 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David Howells402d3262009-02-12 10:40:00 +0000252 if (!mtd->backing_dev_info) {
253 switch (mtd->type) {
254 case MTD_RAM:
255 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
256 break;
257 case MTD_ROM:
258 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
259 break;
260 default:
261 mtd->backing_dev_info = &mtd_bdi_unmappable;
262 break;
263 }
264 }
265
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400266 BUG_ON(mtd->writesize == 0);
Ingo Molnar48b19262006-03-31 02:29:41 -0800267 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Ben Hutchingsb520e412010-01-29 20:59:42 +0000269 do {
270 if (!idr_pre_get(&mtd_idr, GFP_KERNEL))
271 goto fail_locked;
272 error = idr_get_new(&mtd_idr, mtd, &i);
273 } while (error == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Ben Hutchingsb520e412010-01-29 20:59:42 +0000275 if (error)
276 goto fail_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Ben Hutchingsb520e412010-01-29 20:59:42 +0000278 mtd->index = i;
279 mtd->usecount = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000280
Ben Hutchingsb520e412010-01-29 20:59:42 +0000281 if (is_power_of_2(mtd->erasesize))
282 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
283 else
284 mtd->erasesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000285
Ben Hutchingsb520e412010-01-29 20:59:42 +0000286 if (is_power_of_2(mtd->writesize))
287 mtd->writesize_shift = ffs(mtd->writesize) - 1;
288 else
289 mtd->writesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000290
Ben Hutchingsb520e412010-01-29 20:59:42 +0000291 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
292 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
HÃ¥vard Skinnemoen187ef152006-09-22 10:07:08 +0100293
Ben Hutchingsb520e412010-01-29 20:59:42 +0000294 /* Some chips always power up locked. Unlock them now */
295 if ((mtd->flags & MTD_WRITEABLE)
296 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
297 if (mtd->unlock(mtd, 0, mtd->size))
298 printk(KERN_WARNING
299 "%s: unlock failed, writes may not work\n",
300 mtd->name);
301 }
David Brownell1f24b5a2009-03-26 00:42:41 -0700302
Ben Hutchingsb520e412010-01-29 20:59:42 +0000303 /* Caller should have set dev.parent to match the
304 * physical device.
305 */
306 mtd->dev.type = &mtd_devtype;
307 mtd->dev.class = &mtd_class;
308 mtd->dev.devt = MTD_DEVT(i);
309 dev_set_name(&mtd->dev, "mtd%d", i);
310 dev_set_drvdata(&mtd->dev, mtd);
311 if (device_register(&mtd->dev) != 0)
312 goto fail_added;
David Brownell1f24b5a2009-03-26 00:42:41 -0700313
Ben Hutchingsb520e412010-01-29 20:59:42 +0000314 if (MTD_DEVT(i))
315 device_create(&mtd_class, mtd->dev.parent,
316 MTD_DEVT(i) + 1,
317 NULL, "mtd%dro", i);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000318
Ben Hutchingsb520e412010-01-29 20:59:42 +0000319 DEBUG(0, "mtd: Giving out device %d to %s\n", i, mtd->name);
320 /* No need to get a refcount on the module containing
321 the notifier, since we hold the mtd_table_mutex */
322 list_for_each_entry(not, &mtd_notifiers, list)
323 not->add(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000324
Ingo Molnar48b19262006-03-31 02:29:41 -0800325 mutex_unlock(&mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000326 /* We _know_ we aren't being removed, because
327 our caller is still holding us here. So none
328 of this try_ nonsense, and no bitching about it
329 either. :) */
330 __module_get(THIS_MODULE);
331 return 0;
332
333fail_added:
334 idr_remove(&mtd_idr, i);
335fail_locked:
336 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 1;
338}
339
340/**
341 * del_mtd_device - unregister an MTD device
342 * @mtd: pointer to MTD device info structure
343 *
344 * Remove a device from the list of MTD devices present in the system,
345 * and notify each currently active MTD 'user' of its departure.
346 * Returns zero on success or 1 on failure, which currently will happen
347 * if the requested device does not appear to be present in the list.
348 */
349
350int del_mtd_device (struct mtd_info *mtd)
351{
352 int ret;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000353
Ingo Molnar48b19262006-03-31 02:29:41 -0800354 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Ben Hutchingsb520e412010-01-29 20:59:42 +0000356 if (idr_find(&mtd_idr, mtd->index) != mtd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ret = -ENODEV;
358 } else if (mtd->usecount) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000359 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 mtd->index, mtd->name, mtd->usecount);
361 ret = -EBUSY;
362 } else {
matthias@kaehlcke.net856613c2008-05-31 15:28:10 +0200363 struct mtd_notifier *not;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700365 device_unregister(&mtd->dev);
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /* No need to get a refcount on the module containing
368 the notifier, since we hold the mtd_table_mutex */
Chris Malley71a928c2008-05-19 20:11:50 +0100369 list_for_each_entry(not, &mtd_notifiers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 not->remove(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Ben Hutchingsb520e412010-01-29 20:59:42 +0000372 idr_remove(&mtd_idr, mtd->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 module_put(THIS_MODULE);
375 ret = 0;
376 }
377
Ingo Molnar48b19262006-03-31 02:29:41 -0800378 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return ret;
380}
381
382/**
383 * register_mtd_user - register a 'user' of MTD devices.
384 * @new: pointer to notifier info structure
385 *
386 * Registers a pair of callbacks function to be called upon addition
387 * or removal of MTD devices. Causes the 'add' callback to be immediately
388 * invoked for each MTD device currently present in the system.
389 */
390
391void register_mtd_user (struct mtd_notifier *new)
392{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000393 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Ingo Molnar48b19262006-03-31 02:29:41 -0800395 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 list_add(&new->list, &mtd_notifiers);
398
399 __module_get(THIS_MODULE);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000400
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000401 mtd_for_each_device(mtd)
402 new->add(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Ingo Molnar48b19262006-03-31 02:29:41 -0800404 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407/**
Artem B. Bityuckiy49450792005-02-18 14:34:54 +0000408 * unregister_mtd_user - unregister a 'user' of MTD devices.
409 * @old: pointer to notifier info structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 *
411 * Removes a callback function pair from the list of 'users' to be
412 * notified upon addition or removal of MTD devices. Causes the
413 * 'remove' callback to be immediately invoked for each MTD device
414 * currently present in the system.
415 */
416
417int unregister_mtd_user (struct mtd_notifier *old)
418{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000419 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Ingo Molnar48b19262006-03-31 02:29:41 -0800421 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 module_put(THIS_MODULE);
424
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000425 mtd_for_each_device(mtd)
426 old->remove(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 list_del(&old->list);
Ingo Molnar48b19262006-03-31 02:29:41 -0800429 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431}
432
433
434/**
435 * get_mtd_device - obtain a validated handle for an MTD device
436 * @mtd: last known address of the required MTD device
437 * @num: internal device number of the required MTD device
438 *
439 * Given a number and NULL address, return the num'th entry in the device
440 * table, if any. Given an address and num == -1, search the device table
441 * for a device with that address and return if it's still present. Given
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300442 * both, return the num'th driver only if its address matches. Return
443 * error code if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
447{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000448 struct mtd_info *ret = NULL, *other;
449 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Ingo Molnar48b19262006-03-31 02:29:41 -0800451 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (num == -1) {
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000454 mtd_for_each_device(other) {
455 if (other == mtd) {
456 ret = mtd;
457 break;
458 }
459 }
Ben Hutchingsb520e412010-01-29 20:59:42 +0000460 } else if (num >= 0) {
461 ret = idr_find(&mtd_idr, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (mtd && mtd != ret)
463 ret = NULL;
464 }
465
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200466 if (!ret) {
467 ret = ERR_PTR(err);
468 goto out;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200471 err = __get_mtd_device(ret);
472 if (err)
473 ret = ERR_PTR(err);
474out:
Ingo Molnar48b19262006-03-31 02:29:41 -0800475 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return ret;
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200477}
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300478
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200479
480int __get_mtd_device(struct mtd_info *mtd)
481{
482 int err;
483
484 if (!try_module_get(mtd->owner))
485 return -ENODEV;
486
487 if (mtd->get_device) {
488
489 err = mtd->get_device(mtd);
490
491 if (err) {
492 module_put(mtd->owner);
493 return err;
494 }
495 }
496 mtd->usecount++;
497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Artem Bityutskiy77993082006-10-11 14:52:44 +0300500/**
501 * get_mtd_device_nm - obtain a validated handle for an MTD device by
502 * device name
503 * @name: MTD device name to open
504 *
505 * This function returns MTD device description structure in case of
506 * success and an error code in case of failure.
507 */
508
509struct mtd_info *get_mtd_device_nm(const char *name)
510{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000511 int err = -ENODEV;
512 struct mtd_info *mtd = NULL, *other;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300513
514 mutex_lock(&mtd_table_mutex);
515
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000516 mtd_for_each_device(other) {
517 if (!strcmp(name, other->name)) {
518 mtd = other;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300519 break;
520 }
521 }
522
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300523 if (!mtd)
Artem Bityutskiy77993082006-10-11 14:52:44 +0300524 goto out_unlock;
525
526 if (!try_module_get(mtd->owner))
527 goto out_unlock;
528
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300529 if (mtd->get_device) {
530 err = mtd->get_device(mtd);
531 if (err)
532 goto out_put;
533 }
Artem Bityutskiy77993082006-10-11 14:52:44 +0300534
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300535 mtd->usecount++;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300536 mutex_unlock(&mtd_table_mutex);
537 return mtd;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300538
539out_put:
540 module_put(mtd->owner);
541out_unlock:
542 mutex_unlock(&mtd_table_mutex);
543 return ERR_PTR(err);
Artem Bityutskiy77993082006-10-11 14:52:44 +0300544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546void put_mtd_device(struct mtd_info *mtd)
547{
Ingo Molnar48b19262006-03-31 02:29:41 -0800548 mutex_lock(&mtd_table_mutex);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200549 __put_mtd_device(mtd);
550 mutex_unlock(&mtd_table_mutex);
551
552}
553
554void __put_mtd_device(struct mtd_info *mtd)
555{
556 --mtd->usecount;
557 BUG_ON(mtd->usecount < 0);
558
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300559 if (mtd->put_device)
560 mtd->put_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 module_put(mtd->owner);
563}
564
565/* default_mtd_writev - default mtd writev method for MTD devices that
David Woodhousedd36f262006-11-29 16:33:03 +0000566 * don't implement their own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
568
569int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
570 unsigned long count, loff_t to, size_t *retlen)
571{
572 unsigned long i;
573 size_t totlen = 0, thislen;
574 int ret = 0;
575
576 if(!mtd->write) {
577 ret = -EROFS;
578 } else {
579 for (i=0; i<count; i++) {
580 if (!vecs[i].iov_len)
581 continue;
582 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
583 totlen += thislen;
584 if (ret || thislen != vecs[i].iov_len)
585 break;
586 to += vecs[i].iov_len;
587 }
588 }
589 if (retlen)
590 *retlen = totlen;
591 return ret;
592}
593
David Woodhousedd36f262006-11-29 16:33:03 +0000594EXPORT_SYMBOL_GPL(add_mtd_device);
595EXPORT_SYMBOL_GPL(del_mtd_device);
596EXPORT_SYMBOL_GPL(get_mtd_device);
597EXPORT_SYMBOL_GPL(get_mtd_device_nm);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200598EXPORT_SYMBOL_GPL(__get_mtd_device);
David Woodhousedd36f262006-11-29 16:33:03 +0000599EXPORT_SYMBOL_GPL(put_mtd_device);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200600EXPORT_SYMBOL_GPL(__put_mtd_device);
David Woodhousedd36f262006-11-29 16:33:03 +0000601EXPORT_SYMBOL_GPL(register_mtd_user);
602EXPORT_SYMBOL_GPL(unregister_mtd_user);
603EXPORT_SYMBOL_GPL(default_mtd_writev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Pavel Machek2d2dce02006-03-31 02:29:49 -0800605#ifdef CONFIG_PROC_FS
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607/*====================================================================*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/* Support for /proc/mtd */
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610static struct proc_dir_entry *proc_mtd;
611
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000612static inline int mtd_proc_info(char *buf, struct mtd_info *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000614 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
Adrian Hunter69423d92008-12-10 13:37:21 +0000615 (unsigned long long)this->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 this->erasesize, this->name);
617}
618
619static int mtd_read_proc (char *page, char **start, off_t off, int count,
620 int *eof, void *data_unused)
621{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000622 struct mtd_info *mtd;
623 int len, l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 off_t begin = 0;
625
Ingo Molnar48b19262006-03-31 02:29:41 -0800626 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 len = sprintf(page, "dev: size erasesize name\n");
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000629 mtd_for_each_device(mtd) {
630 l = mtd_proc_info(page + len, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 len += l;
632 if (len+begin > off+count)
633 goto done;
634 if (len+begin < off) {
635 begin += len;
636 len = 0;
637 }
638 }
639
640 *eof = 1;
641
642done:
Ingo Molnar48b19262006-03-31 02:29:41 -0800643 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (off >= len+begin)
645 return 0;
646 *start = page + (off-begin);
647 return ((count < begin+len-off) ? count : begin+len-off);
648}
649
Kevin Cernekee45b09072009-04-04 11:03:04 -0700650#endif /* CONFIG_PROC_FS */
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652/*====================================================================*/
653/* Init code */
654
655static int __init init_mtd(void)
656{
David Woodhouse15bce402009-04-05 07:40:58 -0700657 int ret;
658 ret = class_register(&mtd_class);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700659
David Woodhouse15bce402009-04-05 07:40:58 -0700660 if (ret) {
661 pr_err("Error registering mtd class: %d\n", ret);
662 return ret;
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700663 }
Kevin Cernekee45b09072009-04-04 11:03:04 -0700664#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
666 proc_mtd->read_proc = mtd_read_proc;
Kevin Cernekee45b09072009-04-04 11:03:04 -0700667#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 0;
669}
670
671static void __exit cleanup_mtd(void)
672{
Kevin Cernekee45b09072009-04-04 11:03:04 -0700673#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (proc_mtd)
675 remove_proc_entry( "mtd", NULL);
Kevin Cernekee45b09072009-04-04 11:03:04 -0700676#endif /* CONFIG_PROC_FS */
David Woodhouse15bce402009-04-05 07:40:58 -0700677 class_unregister(&mtd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680module_init(init_mtd);
681module_exit(cleanup_mtd);
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683MODULE_LICENSE("GPL");
684MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
685MODULE_DESCRIPTION("Core MTD registration and access routines");