LANANA: Fix dates
[pandora-kernel.git] / drivers / mtd / mtdcore.c
1 /*
2  * Core registration and callback routines for MTD
3  * drivers and users.
4  *
5  */
6
7 #include <linux/module.h>
8 #include <linux/kernel.h>
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>
15 #include <linux/err.h>
16 #include <linux/ioctl.h>
17 #include <linux/init.h>
18 #include <linux/mtd/compatmac.h>
19 #include <linux/proc_fs.h>
20
21 #include <linux/mtd/mtd.h>
22
23 #include "mtdcore.h"
24
25 /* These are exported solely for the purpose of mtd_blkdevs.c. You
26    should not use them for _anything_ else */
27 DEFINE_MUTEX(mtd_table_mutex);
28 struct mtd_info *mtd_table[MAX_MTD_DEVICES];
29
30 EXPORT_SYMBOL_GPL(mtd_table_mutex);
31 EXPORT_SYMBOL_GPL(mtd_table);
32
33 static LIST_HEAD(mtd_notifiers);
34
35 /**
36  *      add_mtd_device - register an MTD device
37  *      @mtd: pointer to new MTD device info structure
38  *
39  *      Add a device to the list of MTD devices present in the system, and
40  *      notify each currently active MTD 'user' of its arrival. Returns
41  *      zero on success or 1 on failure, which currently will only happen
42  *      if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
43  */
44
45 int add_mtd_device(struct mtd_info *mtd)
46 {
47         int i;
48
49         BUG_ON(mtd->writesize == 0);
50         mutex_lock(&mtd_table_mutex);
51
52         for (i=0; i < MAX_MTD_DEVICES; i++)
53                 if (!mtd_table[i]) {
54                         struct mtd_notifier *not;
55
56                         mtd_table[i] = mtd;
57                         mtd->index = i;
58                         mtd->usecount = 0;
59
60                         if (is_power_of_2(mtd->erasesize))
61                                 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
62                         else
63                                 mtd->erasesize_shift = 0;
64
65                         if (is_power_of_2(mtd->writesize))
66                                 mtd->writesize_shift = ffs(mtd->writesize) - 1;
67                         else
68                                 mtd->writesize_shift = 0;
69
70                         mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
71                         mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
72
73                         /* Some chips always power up locked. Unlock them now */
74                         if ((mtd->flags & MTD_WRITEABLE)
75                             && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
76                                 if (mtd->unlock(mtd, 0, mtd->size))
77                                         printk(KERN_WARNING
78                                                "%s: unlock failed, "
79                                                "writes may not work\n",
80                                                mtd->name);
81                         }
82
83                         DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
84                         /* No need to get a refcount on the module containing
85                            the notifier, since we hold the mtd_table_mutex */
86                         list_for_each_entry(not, &mtd_notifiers, list)
87                                 not->add(mtd);
88
89                         mutex_unlock(&mtd_table_mutex);
90                         /* We _know_ we aren't being removed, because
91                            our caller is still holding us here. So none
92                            of this try_ nonsense, and no bitching about it
93                            either. :) */
94                         __module_get(THIS_MODULE);
95                         return 0;
96                 }
97
98         mutex_unlock(&mtd_table_mutex);
99         return 1;
100 }
101
102 /**
103  *      del_mtd_device - unregister an MTD device
104  *      @mtd: pointer to MTD device info structure
105  *
106  *      Remove a device from the list of MTD devices present in the system,
107  *      and notify each currently active MTD 'user' of its departure.
108  *      Returns zero on success or 1 on failure, which currently will happen
109  *      if the requested device does not appear to be present in the list.
110  */
111
112 int del_mtd_device (struct mtd_info *mtd)
113 {
114         int ret;
115
116         mutex_lock(&mtd_table_mutex);
117
118         if (mtd_table[mtd->index] != mtd) {
119                 ret = -ENODEV;
120         } else if (mtd->usecount) {
121                 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
122                        mtd->index, mtd->name, mtd->usecount);
123                 ret = -EBUSY;
124         } else {
125                 struct mtd_notifier *not;
126
127                 /* No need to get a refcount on the module containing
128                    the notifier, since we hold the mtd_table_mutex */
129                 list_for_each_entry(not, &mtd_notifiers, list)
130                         not->remove(mtd);
131
132                 mtd_table[mtd->index] = NULL;
133
134                 module_put(THIS_MODULE);
135                 ret = 0;
136         }
137
138         mutex_unlock(&mtd_table_mutex);
139         return ret;
140 }
141
142 /**
143  *      register_mtd_user - register a 'user' of MTD devices.
144  *      @new: pointer to notifier info structure
145  *
146  *      Registers a pair of callbacks function to be called upon addition
147  *      or removal of MTD devices. Causes the 'add' callback to be immediately
148  *      invoked for each MTD device currently present in the system.
149  */
150
151 void register_mtd_user (struct mtd_notifier *new)
152 {
153         int i;
154
155         mutex_lock(&mtd_table_mutex);
156
157         list_add(&new->list, &mtd_notifiers);
158
159         __module_get(THIS_MODULE);
160
161         for (i=0; i< MAX_MTD_DEVICES; i++)
162                 if (mtd_table[i])
163                         new->add(mtd_table[i]);
164
165         mutex_unlock(&mtd_table_mutex);
166 }
167
168 /**
169  *      unregister_mtd_user - unregister a 'user' of MTD devices.
170  *      @old: pointer to notifier info structure
171  *
172  *      Removes a callback function pair from the list of 'users' to be
173  *      notified upon addition or removal of MTD devices. Causes the
174  *      'remove' callback to be immediately invoked for each MTD device
175  *      currently present in the system.
176  */
177
178 int unregister_mtd_user (struct mtd_notifier *old)
179 {
180         int i;
181
182         mutex_lock(&mtd_table_mutex);
183
184         module_put(THIS_MODULE);
185
186         for (i=0; i< MAX_MTD_DEVICES; i++)
187                 if (mtd_table[i])
188                         old->remove(mtd_table[i]);
189
190         list_del(&old->list);
191         mutex_unlock(&mtd_table_mutex);
192         return 0;
193 }
194
195
196 /**
197  *      get_mtd_device - obtain a validated handle for an MTD device
198  *      @mtd: last known address of the required MTD device
199  *      @num: internal device number of the required MTD device
200  *
201  *      Given a number and NULL address, return the num'th entry in the device
202  *      table, if any.  Given an address and num == -1, search the device table
203  *      for a device with that address and return if it's still present. Given
204  *      both, return the num'th driver only if its address matches. Return
205  *      error code if not.
206  */
207
208 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
209 {
210         struct mtd_info *ret = NULL;
211         int i, err = -ENODEV;
212
213         mutex_lock(&mtd_table_mutex);
214
215         if (num == -1) {
216                 for (i=0; i< MAX_MTD_DEVICES; i++)
217                         if (mtd_table[i] == mtd)
218                                 ret = mtd_table[i];
219         } else if (num < MAX_MTD_DEVICES) {
220                 ret = mtd_table[num];
221                 if (mtd && mtd != ret)
222                         ret = NULL;
223         }
224
225         if (!ret)
226                 goto out_unlock;
227
228         if (!try_module_get(ret->owner))
229                 goto out_unlock;
230
231         if (ret->get_device) {
232                 err = ret->get_device(ret);
233                 if (err)
234                         goto out_put;
235         }
236
237         ret->usecount++;
238         mutex_unlock(&mtd_table_mutex);
239         return ret;
240
241 out_put:
242         module_put(ret->owner);
243 out_unlock:
244         mutex_unlock(&mtd_table_mutex);
245         return ERR_PTR(err);
246 }
247
248 /**
249  *      get_mtd_device_nm - obtain a validated handle for an MTD device by
250  *      device name
251  *      @name: MTD device name to open
252  *
253  *      This function returns MTD device description structure in case of
254  *      success and an error code in case of failure.
255  */
256
257 struct mtd_info *get_mtd_device_nm(const char *name)
258 {
259         int i, err = -ENODEV;
260         struct mtd_info *mtd = NULL;
261
262         mutex_lock(&mtd_table_mutex);
263
264         for (i = 0; i < MAX_MTD_DEVICES; i++) {
265                 if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
266                         mtd = mtd_table[i];
267                         break;
268                 }
269         }
270
271         if (!mtd)
272                 goto out_unlock;
273
274         if (!try_module_get(mtd->owner))
275                 goto out_unlock;
276
277         if (mtd->get_device) {
278                 err = mtd->get_device(mtd);
279                 if (err)
280                         goto out_put;
281         }
282
283         mtd->usecount++;
284         mutex_unlock(&mtd_table_mutex);
285         return mtd;
286
287 out_put:
288         module_put(mtd->owner);
289 out_unlock:
290         mutex_unlock(&mtd_table_mutex);
291         return ERR_PTR(err);
292 }
293
294 void put_mtd_device(struct mtd_info *mtd)
295 {
296         int c;
297
298         mutex_lock(&mtd_table_mutex);
299         c = --mtd->usecount;
300         if (mtd->put_device)
301                 mtd->put_device(mtd);
302         mutex_unlock(&mtd_table_mutex);
303         BUG_ON(c < 0);
304
305         module_put(mtd->owner);
306 }
307
308 /* default_mtd_writev - default mtd writev method for MTD devices that
309  *                      don't implement their own
310  */
311
312 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
313                        unsigned long count, loff_t to, size_t *retlen)
314 {
315         unsigned long i;
316         size_t totlen = 0, thislen;
317         int ret = 0;
318
319         if(!mtd->write) {
320                 ret = -EROFS;
321         } else {
322                 for (i=0; i<count; i++) {
323                         if (!vecs[i].iov_len)
324                                 continue;
325                         ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
326                         totlen += thislen;
327                         if (ret || thislen != vecs[i].iov_len)
328                                 break;
329                         to += vecs[i].iov_len;
330                 }
331         }
332         if (retlen)
333                 *retlen = totlen;
334         return ret;
335 }
336
337 EXPORT_SYMBOL_GPL(add_mtd_device);
338 EXPORT_SYMBOL_GPL(del_mtd_device);
339 EXPORT_SYMBOL_GPL(get_mtd_device);
340 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
341 EXPORT_SYMBOL_GPL(put_mtd_device);
342 EXPORT_SYMBOL_GPL(register_mtd_user);
343 EXPORT_SYMBOL_GPL(unregister_mtd_user);
344 EXPORT_SYMBOL_GPL(default_mtd_writev);
345
346 #ifdef CONFIG_PROC_FS
347
348 /*====================================================================*/
349 /* Support for /proc/mtd */
350
351 static struct proc_dir_entry *proc_mtd;
352
353 static inline int mtd_proc_info (char *buf, int i)
354 {
355         struct mtd_info *this = mtd_table[i];
356
357         if (!this)
358                 return 0;
359
360         return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
361                        (unsigned long long)this->size,
362                        this->erasesize, this->name);
363 }
364
365 static int mtd_read_proc (char *page, char **start, off_t off, int count,
366                           int *eof, void *data_unused)
367 {
368         int len, l, i;
369         off_t   begin = 0;
370
371         mutex_lock(&mtd_table_mutex);
372
373         len = sprintf(page, "dev:    size   erasesize  name\n");
374         for (i=0; i< MAX_MTD_DEVICES; i++) {
375
376                 l = mtd_proc_info(page + len, i);
377                 len += l;
378                 if (len+begin > off+count)
379                         goto done;
380                 if (len+begin < off) {
381                         begin += len;
382                         len = 0;
383                 }
384         }
385
386         *eof = 1;
387
388 done:
389         mutex_unlock(&mtd_table_mutex);
390         if (off >= len+begin)
391                 return 0;
392         *start = page + (off-begin);
393         return ((count < begin+len-off) ? count : begin+len-off);
394 }
395
396 /*====================================================================*/
397 /* Init code */
398
399 static int __init init_mtd(void)
400 {
401         if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
402                 proc_mtd->read_proc = mtd_read_proc;
403         return 0;
404 }
405
406 static void __exit cleanup_mtd(void)
407 {
408         if (proc_mtd)
409                 remove_proc_entry( "mtd", NULL);
410 }
411
412 module_init(init_mtd);
413 module_exit(cleanup_mtd);
414
415 #endif /* CONFIG_PROC_FS */
416
417
418 MODULE_LICENSE("GPL");
419 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
420 MODULE_DESCRIPTION("Core MTD registration and access routines");