NOMMU: Present backing device capabilities for MTD chardevs
[deliverable/linux.git] / drivers / mtd / mtdcore.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Core registration and callback routines for MTD
3 * drivers and users.
4 *
5 */
6
1da177e4
LT
7#include <linux/module.h>
8#include <linux/kernel.h>
1da177e4
LT
9#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>
7799308f 15#include <linux/err.h>
1da177e4
LT
16#include <linux/ioctl.h>
17#include <linux/init.h>
18#include <linux/mtd/compatmac.h>
1da177e4 19#include <linux/proc_fs.h>
1da177e4
LT
20
21#include <linux/mtd/mtd.h>
402d3265 22#include "internal.h"
1da177e4 23
356d70f1
BD
24#include "mtdcore.h"
25
97894cda 26/* These are exported solely for the purpose of mtd_blkdevs.c. You
1da177e4 27 should not use them for _anything_ else */
48b19268 28DEFINE_MUTEX(mtd_table_mutex);
1da177e4
LT
29struct 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
402d3265
DH
50 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
783ed81f 64 BUG_ON(mtd->writesize == 0);
48b19268 65 mutex_lock(&mtd_table_mutex);
1da177e4
LT
66
67 for (i=0; i < MAX_MTD_DEVICES; i++)
68 if (!mtd_table[i]) {
2606c797 69 struct mtd_notifier *not;
1da177e4
LT
70
71 mtd_table[i] = mtd;
72 mtd->index = i;
73 mtd->usecount = 0;
74
69423d99
AH
75 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
187ef152
HS
88 /* Some chips always power up locked. Unlock them now */
89 if ((mtd->flags & MTD_WRITEABLE)
e619a75f 90 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
187ef152
HS
91 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
1da177e4
LT
98 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 */
71a928c0 101 list_for_each_entry(not, &mtd_notifiers, list)
1da177e4 102 not->add(mtd);
97894cda 103
48b19268 104 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
105 /* 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 }
97894cda 112
48b19268 113 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
114 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;
97894cda 130
48b19268 131 mutex_lock(&mtd_table_mutex);
1da177e4
LT
132
133 if (mtd_table[mtd->index] != mtd) {
134 ret = -ENODEV;
135 } else if (mtd->usecount) {
97894cda 136 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
1da177e4
LT
137 mtd->index, mtd->name, mtd->usecount);
138 ret = -EBUSY;
139 } else {
856613c9 140 struct mtd_notifier *not;
1da177e4
LT
141
142 /* No need to get a refcount on the module containing
143 the notifier, since we hold the mtd_table_mutex */
71a928c0 144 list_for_each_entry(not, &mtd_notifiers, list)
1da177e4 145 not->remove(mtd);
1da177e4
LT
146
147 mtd_table[mtd->index] = NULL;
148
149 module_put(THIS_MODULE);
150 ret = 0;
151 }
152
48b19268 153 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
154 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
48b19268 170 mutex_lock(&mtd_table_mutex);
1da177e4
LT
171
172 list_add(&new->list, &mtd_notifiers);
173
174 __module_get(THIS_MODULE);
97894cda 175
1da177e4
LT
176 for (i=0; i< MAX_MTD_DEVICES; i++)
177 if (mtd_table[i])
178 new->add(mtd_table[i]);
179
48b19268 180 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
181}
182
183/**
49450795
AB
184 * unregister_mtd_user - unregister a 'user' of MTD devices.
185 * @old: pointer to notifier info structure
1da177e4
LT
186 *
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
48b19268 197 mutex_lock(&mtd_table_mutex);
1da177e4
LT
198
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]);
97894cda 204
1da177e4 205 list_del(&old->list);
48b19268 206 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
207 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
9c74034f
AB
219 * both, return the num'th driver only if its address matches. Return
220 * error code if not.
1da177e4 221 */
97894cda 222
1da177e4
LT
223struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
224{
225 struct mtd_info *ret = NULL;
9c74034f 226 int i, err = -ENODEV;
1da177e4 227
48b19268 228 mutex_lock(&mtd_table_mutex);
1da177e4
LT
229
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
9fe912ce
AB
240 if (!ret)
241 goto out_unlock;
242
9c74034f 243 if (!try_module_get(ret->owner))
9fe912ce 244 goto out_unlock;
9fe912ce 245
9c74034f
AB
246 if (ret->get_device) {
247 err = ret->get_device(ret);
248 if (err)
249 goto out_put;
9fe912ce 250 }
1da177e4 251
9fe912ce 252 ret->usecount++;
9c74034f
AB
253 mutex_unlock(&mtd_table_mutex);
254 return ret;
1da177e4 255
9c74034f
AB
256out_put:
257 module_put(ret->owner);
9fe912ce 258out_unlock:
48b19268 259 mutex_unlock(&mtd_table_mutex);
9c74034f 260 return ERR_PTR(err);
1da177e4
LT
261}
262
7799308f
AB
263/**
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{
9fe912ce
AB
274 int i, err = -ENODEV;
275 struct mtd_info *mtd = NULL;
7799308f
AB
276
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
9fe912ce 286 if (!mtd)
7799308f
AB
287 goto out_unlock;
288
289 if (!try_module_get(mtd->owner))
290 goto out_unlock;
291
9fe912ce
AB
292 if (mtd->get_device) {
293 err = mtd->get_device(mtd);
294 if (err)
295 goto out_put;
296 }
297
7799308f 298 mtd->usecount++;
9fe912ce
AB
299 mutex_unlock(&mtd_table_mutex);
300 return mtd;
7799308f 301
9fe912ce
AB
302out_put:
303 module_put(mtd->owner);
7799308f
AB
304out_unlock:
305 mutex_unlock(&mtd_table_mutex);
9fe912ce 306 return ERR_PTR(err);
7799308f
AB
307}
308
1da177e4
LT
309void put_mtd_device(struct mtd_info *mtd)
310{
311 int c;
312
48b19268 313 mutex_lock(&mtd_table_mutex);
1da177e4 314 c = --mtd->usecount;
9fe912ce
AB
315 if (mtd->put_device)
316 mtd->put_device(mtd);
48b19268 317 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
318 BUG_ON(c < 0);
319
320 module_put(mtd->owner);
321}
322
323/* default_mtd_writev - default mtd writev method for MTD devices that
dd36f267 324 * don't implement their own
1da177e4
LT
325 */
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
dd36f267
DW
352EXPORT_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);
1da177e4 360
2d2dce0e
PM
361#ifdef CONFIG_PROC_FS
362
1da177e4
LT
363/*====================================================================*/
364/* Support for /proc/mtd */
365
1da177e4
LT
366static 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
69423d99
AH
375 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
376 (unsigned long long)this->size,
1da177e4
LT
377 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
48b19268 386 mutex_lock(&mtd_table_mutex);
1da177e4
LT
387
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:
48b19268 404 mutex_unlock(&mtd_table_mutex);
1da177e4
LT
405 if (off >= len+begin)
406 return 0;
407 *start = page + (off-begin);
408 return ((count < begin+len-off) ? count : begin+len-off);
409}
410
1da177e4
LT
411/*====================================================================*/
412/* Init code */
413
414static int __init init_mtd(void)
415{
1da177e4
LT
416 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
417 proc_mtd->read_proc = mtd_read_proc;
1da177e4
LT
418 return 0;
419}
420
421static void __exit cleanup_mtd(void)
422{
1da177e4
LT
423 if (proc_mtd)
424 remove_proc_entry( "mtd", NULL);
1da177e4
LT
425}
426
427module_init(init_mtd);
428module_exit(cleanup_mtd);
429
2d2dce0e
PM
430#endif /* CONFIG_PROC_FS */
431
1da177e4
LT
432
433MODULE_LICENSE("GPL");
434MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
435MODULE_DESCRIPTION("Core MTD registration and access routines");
This page took 0.292599 seconds and 5 git commands to generate.