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