blob: 65a7f37038815addb7cc41d721de8d1be4d10b91 [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
Thomas Gleixner97894cd2005-11-07 11:15:26 +000026/* These are exported solely for the purpose of mtd_blkdevs.c. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 should not use them for _anything_ else */
Ingo Molnar48b19262006-03-31 02:29:41 -080028DEFINE_MUTEX(mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029struct mtd_info *mtd_table[MAX_MTD_DEVICES];
30
31EXPORT_SYMBOL_GPL(mtd_table_mutex);
32EXPORT_SYMBOL_GPL(mtd_table);
33
34static LIST_HEAD(mtd_notifiers);
35
36/**
37 * add_mtd_device - register an MTD device
38 * @mtd: pointer to new MTD device info structure
39 *
40 * Add a device to the list of MTD devices present in the system, and
41 * notify each currently active MTD 'user' of its arrival. Returns
42 * zero on success or 1 on failure, which currently will only happen
43 * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
44 */
45
46int add_mtd_device(struct mtd_info *mtd)
47{
48 int i;
49
David Howells402d3262009-02-12 10:40:00 +000050 if (!mtd->backing_dev_info) {
51 switch (mtd->type) {
52 case MTD_RAM:
53 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
54 break;
55 case MTD_ROM:
56 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
57 break;
58 default:
59 mtd->backing_dev_info = &mtd_bdi_unmappable;
60 break;
61 }
62 }
63
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +040064 BUG_ON(mtd->writesize == 0);
Ingo Molnar48b19262006-03-31 02:29:41 -080065 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 for (i=0; i < MAX_MTD_DEVICES; i++)
68 if (!mtd_table[i]) {
matthias@kaehlcke.net2606c792008-05-31 15:28:09 +020069 struct mtd_notifier *not;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 mtd_table[i] = mtd;
72 mtd->index = i;
73 mtd->usecount = 0;
74
Adrian Hunter69423d92008-12-10 13:37:21 +000075 if (is_power_of_2(mtd->erasesize))
76 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
77 else
78 mtd->erasesize_shift = 0;
79
80 if (is_power_of_2(mtd->writesize))
81 mtd->writesize_shift = ffs(mtd->writesize) - 1;
82 else
83 mtd->writesize_shift = 0;
84
85 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
86 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
87
Håvard Skinnemoen187ef152006-09-22 10:07:08 +010088 /* Some chips always power up locked. Unlock them now */
89 if ((mtd->flags & MTD_WRITEABLE)
Justin Treone619a752008-01-30 10:25:49 -080090 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
Håvard Skinnemoen187ef152006-09-22 10:07:08 +010091 if (mtd->unlock(mtd, 0, mtd->size))
92 printk(KERN_WARNING
93 "%s: unlock failed, "
94 "writes may not work\n",
95 mtd->name);
96 }
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
99 /* No need to get a refcount on the module containing
100 the notifier, since we hold the mtd_table_mutex */
Chris Malley71a928c2008-05-19 20:11:50 +0100101 list_for_each_entry(not, &mtd_notifiers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 not->add(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000103
Ingo Molnar48b19262006-03-31 02:29:41 -0800104 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /* We _know_ we aren't being removed, because
106 our caller is still holding us here. So none
107 of this try_ nonsense, and no bitching about it
108 either. :) */
109 __module_get(THIS_MODULE);
110 return 0;
111 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000112
Ingo Molnar48b19262006-03-31 02:29:41 -0800113 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return 1;
115}
116
117/**
118 * del_mtd_device - unregister an MTD device
119 * @mtd: pointer to MTD device info structure
120 *
121 * Remove a device from the list of MTD devices present in the system,
122 * and notify each currently active MTD 'user' of its departure.
123 * Returns zero on success or 1 on failure, which currently will happen
124 * if the requested device does not appear to be present in the list.
125 */
126
127int del_mtd_device (struct mtd_info *mtd)
128{
129 int ret;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000130
Ingo Molnar48b19262006-03-31 02:29:41 -0800131 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 if (mtd_table[mtd->index] != mtd) {
134 ret = -ENODEV;
135 } else if (mtd->usecount) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000136 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 mtd->index, mtd->name, mtd->usecount);
138 ret = -EBUSY;
139 } else {
matthias@kaehlcke.net856613c2008-05-31 15:28:10 +0200140 struct mtd_notifier *not;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 /* No need to get a refcount on the module containing
143 the notifier, since we hold the mtd_table_mutex */
Chris Malley71a928c2008-05-19 20:11:50 +0100144 list_for_each_entry(not, &mtd_notifiers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 not->remove(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 mtd_table[mtd->index] = NULL;
148
149 module_put(THIS_MODULE);
150 ret = 0;
151 }
152
Ingo Molnar48b19262006-03-31 02:29:41 -0800153 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return ret;
155}
156
157/**
158 * register_mtd_user - register a 'user' of MTD devices.
159 * @new: pointer to notifier info structure
160 *
161 * Registers a pair of callbacks function to be called upon addition
162 * or removal of MTD devices. Causes the 'add' callback to be immediately
163 * invoked for each MTD device currently present in the system.
164 */
165
166void register_mtd_user (struct mtd_notifier *new)
167{
168 int i;
169
Ingo Molnar48b19262006-03-31 02:29:41 -0800170 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 list_add(&new->list, &mtd_notifiers);
173
174 __module_get(THIS_MODULE);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 for (i=0; i< MAX_MTD_DEVICES; i++)
177 if (mtd_table[i])
178 new->add(mtd_table[i]);
179
Ingo Molnar48b19262006-03-31 02:29:41 -0800180 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/**
Artem B. Bityuckiy49450792005-02-18 14:34:54 +0000184 * unregister_mtd_user - unregister a 'user' of MTD devices.
185 * @old: pointer to notifier info structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 *
187 * Removes a callback function pair from the list of 'users' to be
188 * notified upon addition or removal of MTD devices. Causes the
189 * 'remove' callback to be immediately invoked for each MTD device
190 * currently present in the system.
191 */
192
193int unregister_mtd_user (struct mtd_notifier *old)
194{
195 int i;
196
Ingo Molnar48b19262006-03-31 02:29:41 -0800197 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 module_put(THIS_MODULE);
200
201 for (i=0; i< MAX_MTD_DEVICES; i++)
202 if (mtd_table[i])
203 old->remove(mtd_table[i]);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 list_del(&old->list);
Ingo Molnar48b19262006-03-31 02:29:41 -0800206 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return 0;
208}
209
210
211/**
212 * get_mtd_device - obtain a validated handle for an MTD device
213 * @mtd: last known address of the required MTD device
214 * @num: internal device number of the required MTD device
215 *
216 * Given a number and NULL address, return the num'th entry in the device
217 * table, if any. Given an address and num == -1, search the device table
218 * for a device with that address and return if it's still present. Given
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300219 * both, return the num'th driver only if its address matches. Return
220 * error code if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
224{
225 struct mtd_info *ret = NULL;
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300226 int i, err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Ingo Molnar48b19262006-03-31 02:29:41 -0800228 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (num == -1) {
231 for (i=0; i< MAX_MTD_DEVICES; i++)
232 if (mtd_table[i] == mtd)
233 ret = mtd_table[i];
234 } else if (num < MAX_MTD_DEVICES) {
235 ret = mtd_table[num];
236 if (mtd && mtd != ret)
237 ret = NULL;
238 }
239
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300240 if (!ret)
241 goto out_unlock;
242
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300243 if (!try_module_get(ret->owner))
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300244 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300246 if (ret->get_device) {
247 err = ret->get_device(ret);
248 if (err)
249 goto out_put;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300252 ret->usecount++;
Ingo Molnar48b19262006-03-31 02:29:41 -0800253 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return ret;
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300255
256out_put:
257 module_put(ret->owner);
258out_unlock:
259 mutex_unlock(&mtd_table_mutex);
260 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Artem Bityutskiy77993082006-10-11 14:52:44 +0300263/**
264 * get_mtd_device_nm - obtain a validated handle for an MTD device by
265 * device name
266 * @name: MTD device name to open
267 *
268 * This function returns MTD device description structure in case of
269 * success and an error code in case of failure.
270 */
271
272struct mtd_info *get_mtd_device_nm(const char *name)
273{
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300274 int i, err = -ENODEV;
275 struct mtd_info *mtd = NULL;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300276
277 mutex_lock(&mtd_table_mutex);
278
279 for (i = 0; i < MAX_MTD_DEVICES; i++) {
280 if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
281 mtd = mtd_table[i];
282 break;
283 }
284 }
285
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300286 if (!mtd)
Artem Bityutskiy77993082006-10-11 14:52:44 +0300287 goto out_unlock;
288
289 if (!try_module_get(mtd->owner))
290 goto out_unlock;
291
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300292 if (mtd->get_device) {
293 err = mtd->get_device(mtd);
294 if (err)
295 goto out_put;
296 }
Artem Bityutskiy77993082006-10-11 14:52:44 +0300297
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300298 mtd->usecount++;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300299 mutex_unlock(&mtd_table_mutex);
300 return mtd;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300301
302out_put:
303 module_put(mtd->owner);
304out_unlock:
305 mutex_unlock(&mtd_table_mutex);
306 return ERR_PTR(err);
Artem Bityutskiy77993082006-10-11 14:52:44 +0300307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309void put_mtd_device(struct mtd_info *mtd)
310{
311 int c;
312
Ingo Molnar48b19262006-03-31 02:29:41 -0800313 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 c = --mtd->usecount;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300315 if (mtd->put_device)
316 mtd->put_device(mtd);
Ingo Molnar48b19262006-03-31 02:29:41 -0800317 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 BUG_ON(c < 0);
319
320 module_put(mtd->owner);
321}
322
323/* default_mtd_writev - default mtd writev method for MTD devices that
David Woodhousedd36f262006-11-29 16:33:03 +0000324 * don't implement their own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
326
327int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
328 unsigned long count, loff_t to, size_t *retlen)
329{
330 unsigned long i;
331 size_t totlen = 0, thislen;
332 int ret = 0;
333
334 if(!mtd->write) {
335 ret = -EROFS;
336 } else {
337 for (i=0; i<count; i++) {
338 if (!vecs[i].iov_len)
339 continue;
340 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
341 totlen += thislen;
342 if (ret || thislen != vecs[i].iov_len)
343 break;
344 to += vecs[i].iov_len;
345 }
346 }
347 if (retlen)
348 *retlen = totlen;
349 return ret;
350}
351
David Woodhousedd36f262006-11-29 16:33:03 +0000352EXPORT_SYMBOL_GPL(add_mtd_device);
353EXPORT_SYMBOL_GPL(del_mtd_device);
354EXPORT_SYMBOL_GPL(get_mtd_device);
355EXPORT_SYMBOL_GPL(get_mtd_device_nm);
356EXPORT_SYMBOL_GPL(put_mtd_device);
357EXPORT_SYMBOL_GPL(register_mtd_user);
358EXPORT_SYMBOL_GPL(unregister_mtd_user);
359EXPORT_SYMBOL_GPL(default_mtd_writev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Pavel Machek2d2dce02006-03-31 02:29:49 -0800361#ifdef CONFIG_PROC_FS
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/*====================================================================*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/* Support for /proc/mtd */
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366static struct proc_dir_entry *proc_mtd;
367
368static inline int mtd_proc_info (char *buf, int i)
369{
370 struct mtd_info *this = mtd_table[i];
371
372 if (!this)
373 return 0;
374
Adrian Hunter69423d92008-12-10 13:37:21 +0000375 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
376 (unsigned long long)this->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 this->erasesize, this->name);
378}
379
380static int mtd_read_proc (char *page, char **start, off_t off, int count,
381 int *eof, void *data_unused)
382{
383 int len, l, i;
384 off_t begin = 0;
385
Ingo Molnar48b19262006-03-31 02:29:41 -0800386 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 len = sprintf(page, "dev: size erasesize name\n");
389 for (i=0; i< MAX_MTD_DEVICES; i++) {
390
391 l = mtd_proc_info(page + len, i);
392 len += l;
393 if (len+begin > off+count)
394 goto done;
395 if (len+begin < off) {
396 begin += len;
397 len = 0;
398 }
399 }
400
401 *eof = 1;
402
403done:
Ingo Molnar48b19262006-03-31 02:29:41 -0800404 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (off >= len+begin)
406 return 0;
407 *start = page + (off-begin);
408 return ((count < begin+len-off) ? count : begin+len-off);
409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/*====================================================================*/
412/* Init code */
413
414static int __init init_mtd(void)
415{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
417 proc_mtd->read_proc = mtd_read_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return 0;
419}
420
421static void __exit cleanup_mtd(void)
422{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (proc_mtd)
424 remove_proc_entry( "mtd", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427module_init(init_mtd);
428module_exit(cleanup_mtd);
429
Pavel Machek2d2dce02006-03-31 02:29:49 -0800430#endif /* CONFIG_PROC_FS */
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433MODULE_LICENSE("GPL");
434MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
435MODULE_DESCRIPTION("Core MTD registration and access routines");