blob: bccb4b1ffc46ff21b127f9e80cafef118f05f3ca [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 Brownell1f24b5a2009-03-26 00:42:41 -070026
27static struct class *mtd_class;
28
Thomas Gleixner97894cd2005-11-07 11:15:26 +000029/* These are exported solely for the purpose of mtd_blkdevs.c. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 should not use them for _anything_ else */
Ingo Molnar48b19262006-03-31 02:29:41 -080031DEFINE_MUTEX(mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct 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
David Brownell1f24b5a2009-03-26 00:42:41 -070039
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{
Denis V. Lunev2fdb1142009-04-12 08:14:46 +010051 dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
David Brownell1f24b5a2009-03-26 00:42:41 -070052
53 /* remove /dev/mtdXro node if needed */
Denis V. Lunev2fdb1142009-04-12 08:14:46 +010054 if (index)
55 device_destroy(mtd_class, index + 1);
David Brownell1f24b5a2009-03-26 00:42:41 -070056}
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}
Kevin Cernekee694bb7f2009-04-03 13:00:45 -070092static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
93
94static ssize_t mtd_flags_show(struct device *dev,
95 struct device_attribute *attr, char *buf)
96{
97 struct mtd_info *mtd = dev_to_mtd(dev);
98
99 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
100
101}
102static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
103
104static ssize_t mtd_size_show(struct device *dev,
105 struct device_attribute *attr, char *buf)
106{
107 struct mtd_info *mtd = dev_to_mtd(dev);
108
109 return snprintf(buf, PAGE_SIZE, "%llu\n",
110 (unsigned long long)mtd->size);
111
112}
113static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
114
115static ssize_t mtd_erasesize_show(struct device *dev,
116 struct device_attribute *attr, char *buf)
117{
118 struct mtd_info *mtd = dev_to_mtd(dev);
119
120 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
121
122}
123static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
124
125static ssize_t mtd_writesize_show(struct device *dev,
126 struct device_attribute *attr, char *buf)
127{
128 struct mtd_info *mtd = dev_to_mtd(dev);
129
130 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
131
132}
133static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
134
Artem Bityutskiye7693542009-04-18 12:29:42 +0300135static ssize_t mtd_subpagesize_show(struct device *dev,
136 struct device_attribute *attr, char *buf)
137{
138 struct mtd_info *mtd = dev_to_mtd(dev);
139 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
140
141 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
142
143}
144static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
145
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700146static ssize_t mtd_oobsize_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
148{
149 struct mtd_info *mtd = dev_to_mtd(dev);
150
151 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
152
153}
154static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
155
156static ssize_t mtd_numeraseregions_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, "%u\n", mtd->numeraseregions);
162
163}
164static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
165 NULL);
166
167static ssize_t mtd_name_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
169{
170 struct mtd_info *mtd = dev_to_mtd(dev);
171
172 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
173
174}
175static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
David Brownell1f24b5a2009-03-26 00:42:41 -0700176
177static struct attribute *mtd_attrs[] = {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700178 &dev_attr_type.attr,
179 &dev_attr_flags.attr,
180 &dev_attr_size.attr,
181 &dev_attr_erasesize.attr,
182 &dev_attr_writesize.attr,
Artem Bityutskiye7693542009-04-18 12:29:42 +0300183 &dev_attr_subpagesize.attr,
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700184 &dev_attr_oobsize.attr,
185 &dev_attr_numeraseregions.attr,
186 &dev_attr_name.attr,
David Brownell1f24b5a2009-03-26 00:42:41 -0700187 NULL,
188};
189
190struct attribute_group mtd_group = {
191 .attrs = mtd_attrs,
192};
193
194struct attribute_group *mtd_groups[] = {
195 &mtd_group,
196 NULL,
197};
198
199static struct device_type mtd_devtype = {
200 .name = "mtd",
201 .groups = mtd_groups,
202 .release = mtd_release,
203};
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/**
206 * add_mtd_device - register an MTD device
207 * @mtd: pointer to new MTD device info structure
208 *
209 * Add a device to the list of MTD devices present in the system, and
210 * notify each currently active MTD 'user' of its arrival. Returns
211 * zero on success or 1 on failure, which currently will only happen
212 * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
David Brownell1f24b5a2009-03-26 00:42:41 -0700213 * or there's a sysfs error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215
216int add_mtd_device(struct mtd_info *mtd)
217{
218 int i;
219
David Howells402d3262009-02-12 10:40:00 +0000220 if (!mtd->backing_dev_info) {
221 switch (mtd->type) {
222 case MTD_RAM:
223 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
224 break;
225 case MTD_ROM:
226 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
227 break;
228 default:
229 mtd->backing_dev_info = &mtd_bdi_unmappable;
230 break;
231 }
232 }
233
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400234 BUG_ON(mtd->writesize == 0);
Ingo Molnar48b19262006-03-31 02:29:41 -0800235 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 for (i=0; i < MAX_MTD_DEVICES; i++)
238 if (!mtd_table[i]) {
matthias@kaehlcke.net2606c792008-05-31 15:28:09 +0200239 struct mtd_notifier *not;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 mtd_table[i] = mtd;
242 mtd->index = i;
243 mtd->usecount = 0;
244
Adrian Hunter69423d92008-12-10 13:37:21 +0000245 if (is_power_of_2(mtd->erasesize))
246 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
247 else
248 mtd->erasesize_shift = 0;
249
250 if (is_power_of_2(mtd->writesize))
251 mtd->writesize_shift = ffs(mtd->writesize) - 1;
252 else
253 mtd->writesize_shift = 0;
254
255 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
256 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
257
HÃ¥vard Skinnemoen187ef152006-09-22 10:07:08 +0100258 /* Some chips always power up locked. Unlock them now */
259 if ((mtd->flags & MTD_WRITEABLE)
Justin Treone619a752008-01-30 10:25:49 -0800260 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
HÃ¥vard Skinnemoen187ef152006-09-22 10:07:08 +0100261 if (mtd->unlock(mtd, 0, mtd->size))
262 printk(KERN_WARNING
263 "%s: unlock failed, "
264 "writes may not work\n",
265 mtd->name);
266 }
267
David Brownell1f24b5a2009-03-26 00:42:41 -0700268 /* Caller should have set dev.parent to match the
269 * physical device.
270 */
271 mtd->dev.type = &mtd_devtype;
272 mtd->dev.class = mtd_class;
273 mtd->dev.devt = MTD_DEVT(i);
274 dev_set_name(&mtd->dev, "mtd%d", i);
275 if (device_register(&mtd->dev) != 0) {
276 mtd_table[i] = NULL;
277 break;
278 }
279
280 if (MTD_DEVT(i))
281 device_create(mtd_class, mtd->dev.parent,
282 MTD_DEVT(i) + 1,
283 NULL, "mtd%dro", i);
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
286 /* No need to get a refcount on the module containing
287 the notifier, since we hold the mtd_table_mutex */
Chris Malley71a928c2008-05-19 20:11:50 +0100288 list_for_each_entry(not, &mtd_notifiers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 not->add(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000290
Ingo Molnar48b19262006-03-31 02:29:41 -0800291 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* We _know_ we aren't being removed, because
293 our caller is still holding us here. So none
294 of this try_ nonsense, and no bitching about it
295 either. :) */
296 __module_get(THIS_MODULE);
297 return 0;
298 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000299
Ingo Molnar48b19262006-03-31 02:29:41 -0800300 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 1;
302}
303
304/**
305 * del_mtd_device - unregister an MTD device
306 * @mtd: pointer to MTD device info structure
307 *
308 * Remove a device from the list of MTD devices present in the system,
309 * and notify each currently active MTD 'user' of its departure.
310 * Returns zero on success or 1 on failure, which currently will happen
311 * if the requested device does not appear to be present in the list.
312 */
313
314int del_mtd_device (struct mtd_info *mtd)
315{
316 int ret;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000317
Ingo Molnar48b19262006-03-31 02:29:41 -0800318 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 if (mtd_table[mtd->index] != mtd) {
321 ret = -ENODEV;
322 } else if (mtd->usecount) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000323 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 mtd->index, mtd->name, mtd->usecount);
325 ret = -EBUSY;
326 } else {
matthias@kaehlcke.net856613c2008-05-31 15:28:10 +0200327 struct mtd_notifier *not;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700329 device_unregister(&mtd->dev);
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /* No need to get a refcount on the module containing
332 the notifier, since we hold the mtd_table_mutex */
Chris Malley71a928c2008-05-19 20:11:50 +0100333 list_for_each_entry(not, &mtd_notifiers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 not->remove(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 mtd_table[mtd->index] = NULL;
337
338 module_put(THIS_MODULE);
339 ret = 0;
340 }
341
Ingo Molnar48b19262006-03-31 02:29:41 -0800342 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return ret;
344}
345
346/**
347 * register_mtd_user - register a 'user' of MTD devices.
348 * @new: pointer to notifier info structure
349 *
350 * Registers a pair of callbacks function to be called upon addition
351 * or removal of MTD devices. Causes the 'add' callback to be immediately
352 * invoked for each MTD device currently present in the system.
353 */
354
355void register_mtd_user (struct mtd_notifier *new)
356{
357 int i;
358
Ingo Molnar48b19262006-03-31 02:29:41 -0800359 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 list_add(&new->list, &mtd_notifiers);
362
363 __module_get(THIS_MODULE);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 for (i=0; i< MAX_MTD_DEVICES; i++)
366 if (mtd_table[i])
367 new->add(mtd_table[i]);
368
Ingo Molnar48b19262006-03-31 02:29:41 -0800369 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372/**
Artem B. Bityuckiy49450792005-02-18 14:34:54 +0000373 * unregister_mtd_user - unregister a 'user' of MTD devices.
374 * @old: pointer to notifier info structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *
376 * Removes a callback function pair from the list of 'users' to be
377 * notified upon addition or removal of MTD devices. Causes the
378 * 'remove' callback to be immediately invoked for each MTD device
379 * currently present in the system.
380 */
381
382int unregister_mtd_user (struct mtd_notifier *old)
383{
384 int i;
385
Ingo Molnar48b19262006-03-31 02:29:41 -0800386 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 module_put(THIS_MODULE);
389
390 for (i=0; i< MAX_MTD_DEVICES; i++)
391 if (mtd_table[i])
392 old->remove(mtd_table[i]);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 list_del(&old->list);
Ingo Molnar48b19262006-03-31 02:29:41 -0800395 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 0;
397}
398
399
400/**
401 * get_mtd_device - obtain a validated handle for an MTD device
402 * @mtd: last known address of the required MTD device
403 * @num: internal device number of the required MTD device
404 *
405 * Given a number and NULL address, return the num'th entry in the device
406 * table, if any. Given an address and num == -1, search the device table
407 * for a device with that address and return if it's still present. Given
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300408 * both, return the num'th driver only if its address matches. Return
409 * error code if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
413{
414 struct mtd_info *ret = NULL;
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300415 int i, err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Ingo Molnar48b19262006-03-31 02:29:41 -0800417 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 if (num == -1) {
420 for (i=0; i< MAX_MTD_DEVICES; i++)
421 if (mtd_table[i] == mtd)
422 ret = mtd_table[i];
423 } else if (num < MAX_MTD_DEVICES) {
424 ret = mtd_table[num];
425 if (mtd && mtd != ret)
426 ret = NULL;
427 }
428
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300429 if (!ret)
430 goto out_unlock;
431
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300432 if (!try_module_get(ret->owner))
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300433 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300435 if (ret->get_device) {
436 err = ret->get_device(ret);
437 if (err)
438 goto out_put;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300441 ret->usecount++;
Ingo Molnar48b19262006-03-31 02:29:41 -0800442 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return ret;
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300444
445out_put:
446 module_put(ret->owner);
447out_unlock:
448 mutex_unlock(&mtd_table_mutex);
449 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Artem Bityutskiy77993082006-10-11 14:52:44 +0300452/**
453 * get_mtd_device_nm - obtain a validated handle for an MTD device by
454 * device name
455 * @name: MTD device name to open
456 *
457 * This function returns MTD device description structure in case of
458 * success and an error code in case of failure.
459 */
460
461struct mtd_info *get_mtd_device_nm(const char *name)
462{
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300463 int i, err = -ENODEV;
464 struct mtd_info *mtd = NULL;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300465
466 mutex_lock(&mtd_table_mutex);
467
468 for (i = 0; i < MAX_MTD_DEVICES; i++) {
469 if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
470 mtd = mtd_table[i];
471 break;
472 }
473 }
474
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300475 if (!mtd)
Artem Bityutskiy77993082006-10-11 14:52:44 +0300476 goto out_unlock;
477
478 if (!try_module_get(mtd->owner))
479 goto out_unlock;
480
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300481 if (mtd->get_device) {
482 err = mtd->get_device(mtd);
483 if (err)
484 goto out_put;
485 }
Artem Bityutskiy77993082006-10-11 14:52:44 +0300486
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300487 mtd->usecount++;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300488 mutex_unlock(&mtd_table_mutex);
489 return mtd;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300490
491out_put:
492 module_put(mtd->owner);
493out_unlock:
494 mutex_unlock(&mtd_table_mutex);
495 return ERR_PTR(err);
Artem Bityutskiy77993082006-10-11 14:52:44 +0300496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498void put_mtd_device(struct mtd_info *mtd)
499{
500 int c;
501
Ingo Molnar48b19262006-03-31 02:29:41 -0800502 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 c = --mtd->usecount;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300504 if (mtd->put_device)
505 mtd->put_device(mtd);
Ingo Molnar48b19262006-03-31 02:29:41 -0800506 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 BUG_ON(c < 0);
508
509 module_put(mtd->owner);
510}
511
512/* default_mtd_writev - default mtd writev method for MTD devices that
David Woodhousedd36f262006-11-29 16:33:03 +0000513 * don't implement their own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
515
516int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
517 unsigned long count, loff_t to, size_t *retlen)
518{
519 unsigned long i;
520 size_t totlen = 0, thislen;
521 int ret = 0;
522
523 if(!mtd->write) {
524 ret = -EROFS;
525 } else {
526 for (i=0; i<count; i++) {
527 if (!vecs[i].iov_len)
528 continue;
529 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
530 totlen += thislen;
531 if (ret || thislen != vecs[i].iov_len)
532 break;
533 to += vecs[i].iov_len;
534 }
535 }
536 if (retlen)
537 *retlen = totlen;
538 return ret;
539}
540
David Woodhousedd36f262006-11-29 16:33:03 +0000541EXPORT_SYMBOL_GPL(add_mtd_device);
542EXPORT_SYMBOL_GPL(del_mtd_device);
543EXPORT_SYMBOL_GPL(get_mtd_device);
544EXPORT_SYMBOL_GPL(get_mtd_device_nm);
545EXPORT_SYMBOL_GPL(put_mtd_device);
546EXPORT_SYMBOL_GPL(register_mtd_user);
547EXPORT_SYMBOL_GPL(unregister_mtd_user);
548EXPORT_SYMBOL_GPL(default_mtd_writev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Pavel Machek2d2dce02006-03-31 02:29:49 -0800550#ifdef CONFIG_PROC_FS
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/*====================================================================*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553/* Support for /proc/mtd */
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static struct proc_dir_entry *proc_mtd;
556
557static inline int mtd_proc_info (char *buf, int i)
558{
559 struct mtd_info *this = mtd_table[i];
560
561 if (!this)
562 return 0;
563
Adrian Hunter69423d92008-12-10 13:37:21 +0000564 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
565 (unsigned long long)this->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 this->erasesize, this->name);
567}
568
569static int mtd_read_proc (char *page, char **start, off_t off, int count,
570 int *eof, void *data_unused)
571{
572 int len, l, i;
573 off_t begin = 0;
574
Ingo Molnar48b19262006-03-31 02:29:41 -0800575 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 len = sprintf(page, "dev: size erasesize name\n");
578 for (i=0; i< MAX_MTD_DEVICES; i++) {
579
580 l = mtd_proc_info(page + len, i);
581 len += l;
582 if (len+begin > off+count)
583 goto done;
584 if (len+begin < off) {
585 begin += len;
586 len = 0;
587 }
588 }
589
590 *eof = 1;
591
592done:
Ingo Molnar48b19262006-03-31 02:29:41 -0800593 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (off >= len+begin)
595 return 0;
596 *start = page + (off-begin);
597 return ((count < begin+len-off) ? count : begin+len-off);
598}
599
Kevin Cernekee45b09072009-04-04 11:03:04 -0700600#endif /* CONFIG_PROC_FS */
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/*====================================================================*/
603/* Init code */
604
605static int __init init_mtd(void)
606{
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700607 mtd_class = class_create(THIS_MODULE, "mtd");
608
609 if (IS_ERR(mtd_class)) {
610 pr_err("Error creating mtd class.\n");
611 return PTR_ERR(mtd_class);
612 }
Kevin Cernekee45b09072009-04-04 11:03:04 -0700613#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
615 proc_mtd->read_proc = mtd_read_proc;
Kevin Cernekee45b09072009-04-04 11:03:04 -0700616#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return 0;
618}
619
620static void __exit cleanup_mtd(void)
621{
Kevin Cernekee45b09072009-04-04 11:03:04 -0700622#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (proc_mtd)
624 remove_proc_entry( "mtd", NULL);
Kevin Cernekee45b09072009-04-04 11:03:04 -0700625#endif /* CONFIG_PROC_FS */
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700626 class_destroy(mtd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629module_init(init_mtd);
630module_exit(cleanup_mtd);
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632MODULE_LICENSE("GPL");
633MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
634MODULE_DESCRIPTION("Core MTD registration and access routines");