2 * Core registration and callback routines for MTD
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>
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>
21 #include <linux/mtd/mtd.h>
26 /* These are exported solely for the purpose of mtd_blkdevs.c. You
27 should not use them for _anything_ else */
28 DEFINE_MUTEX(mtd_table_mutex
);
29 struct mtd_info
*mtd_table
[MAX_MTD_DEVICES
];
31 EXPORT_SYMBOL_GPL(mtd_table_mutex
);
32 EXPORT_SYMBOL_GPL(mtd_table
);
34 static LIST_HEAD(mtd_notifiers
);
37 * add_mtd_device - register an MTD device
38 * @mtd: pointer to new MTD device info structure
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)
46 int add_mtd_device(struct mtd_info
*mtd
)
50 if (!mtd
->backing_dev_info
) {
53 mtd
->backing_dev_info
= &mtd_bdi_rw_mappable
;
56 mtd
->backing_dev_info
= &mtd_bdi_ro_mappable
;
59 mtd
->backing_dev_info
= &mtd_bdi_unmappable
;
64 BUG_ON(mtd
->writesize
== 0);
65 mutex_lock(&mtd_table_mutex
);
67 for (i
=0; i
< MAX_MTD_DEVICES
; i
++)
69 struct mtd_notifier
*not;
75 if (is_power_of_2(mtd
->erasesize
))
76 mtd
->erasesize_shift
= ffs(mtd
->erasesize
) - 1;
78 mtd
->erasesize_shift
= 0;
80 if (is_power_of_2(mtd
->writesize
))
81 mtd
->writesize_shift
= ffs(mtd
->writesize
) - 1;
83 mtd
->writesize_shift
= 0;
85 mtd
->erasesize_mask
= (1 << mtd
->erasesize_shift
) - 1;
86 mtd
->writesize_mask
= (1 << mtd
->writesize_shift
) - 1;
88 /* Some chips always power up locked. Unlock them now */
89 if ((mtd
->flags
& MTD_WRITEABLE
)
90 && (mtd
->flags
& MTD_POWERUP_LOCK
) && mtd
->unlock
) {
91 if (mtd
->unlock(mtd
, 0, mtd
->size
))
94 "writes may not work\n",
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 */
101 list_for_each_entry(not, &mtd_notifiers
, list
)
104 mutex_unlock(&mtd_table_mutex
);
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
109 __module_get(THIS_MODULE
);
113 mutex_unlock(&mtd_table_mutex
);
118 * del_mtd_device - unregister an MTD device
119 * @mtd: pointer to MTD device info structure
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.
127 int del_mtd_device (struct mtd_info
*mtd
)
131 mutex_lock(&mtd_table_mutex
);
133 if (mtd_table
[mtd
->index
] != mtd
) {
135 } else if (mtd
->usecount
) {
136 printk(KERN_NOTICE
"Removing MTD device #%d (%s) with use count %d\n",
137 mtd
->index
, mtd
->name
, mtd
->usecount
);
140 struct mtd_notifier
*not;
142 /* No need to get a refcount on the module containing
143 the notifier, since we hold the mtd_table_mutex */
144 list_for_each_entry(not, &mtd_notifiers
, list
)
147 mtd_table
[mtd
->index
] = NULL
;
149 module_put(THIS_MODULE
);
153 mutex_unlock(&mtd_table_mutex
);
158 * register_mtd_user - register a 'user' of MTD devices.
159 * @new: pointer to notifier info structure
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.
166 void register_mtd_user (struct mtd_notifier
*new)
170 mutex_lock(&mtd_table_mutex
);
172 list_add(&new->list
, &mtd_notifiers
);
174 __module_get(THIS_MODULE
);
176 for (i
=0; i
< MAX_MTD_DEVICES
; i
++)
178 new->add(mtd_table
[i
]);
180 mutex_unlock(&mtd_table_mutex
);
184 * unregister_mtd_user - unregister a 'user' of MTD devices.
185 * @old: pointer to notifier info structure
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.
193 int unregister_mtd_user (struct mtd_notifier
*old
)
197 mutex_lock(&mtd_table_mutex
);
199 module_put(THIS_MODULE
);
201 for (i
=0; i
< MAX_MTD_DEVICES
; i
++)
203 old
->remove(mtd_table
[i
]);
205 list_del(&old
->list
);
206 mutex_unlock(&mtd_table_mutex
);
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
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
219 * both, return the num'th driver only if its address matches. Return
223 struct mtd_info
*get_mtd_device(struct mtd_info
*mtd
, int num
)
225 struct mtd_info
*ret
= NULL
;
226 int i
, err
= -ENODEV
;
228 mutex_lock(&mtd_table_mutex
);
231 for (i
=0; i
< MAX_MTD_DEVICES
; i
++)
232 if (mtd_table
[i
] == mtd
)
234 } else if (num
< MAX_MTD_DEVICES
) {
235 ret
= mtd_table
[num
];
236 if (mtd
&& mtd
!= ret
)
243 if (!try_module_get(ret
->owner
))
246 if (ret
->get_device
) {
247 err
= ret
->get_device(ret
);
253 mutex_unlock(&mtd_table_mutex
);
257 module_put(ret
->owner
);
259 mutex_unlock(&mtd_table_mutex
);
264 * get_mtd_device_nm - obtain a validated handle for an MTD device by
266 * @name: MTD device name to open
268 * This function returns MTD device description structure in case of
269 * success and an error code in case of failure.
272 struct mtd_info
*get_mtd_device_nm(const char *name
)
274 int i
, err
= -ENODEV
;
275 struct mtd_info
*mtd
= NULL
;
277 mutex_lock(&mtd_table_mutex
);
279 for (i
= 0; i
< MAX_MTD_DEVICES
; i
++) {
280 if (mtd_table
[i
] && !strcmp(name
, mtd_table
[i
]->name
)) {
289 if (!try_module_get(mtd
->owner
))
292 if (mtd
->get_device
) {
293 err
= mtd
->get_device(mtd
);
299 mutex_unlock(&mtd_table_mutex
);
303 module_put(mtd
->owner
);
305 mutex_unlock(&mtd_table_mutex
);
309 void put_mtd_device(struct mtd_info
*mtd
)
313 mutex_lock(&mtd_table_mutex
);
316 mtd
->put_device(mtd
);
317 mutex_unlock(&mtd_table_mutex
);
320 module_put(mtd
->owner
);
323 /* default_mtd_writev - default mtd writev method for MTD devices that
324 * don't implement their own
327 int default_mtd_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
328 unsigned long count
, loff_t to
, size_t *retlen
)
331 size_t totlen
= 0, thislen
;
337 for (i
=0; i
<count
; i
++) {
338 if (!vecs
[i
].iov_len
)
340 ret
= mtd
->write(mtd
, to
, vecs
[i
].iov_len
, &thislen
, vecs
[i
].iov_base
);
342 if (ret
|| thislen
!= vecs
[i
].iov_len
)
344 to
+= vecs
[i
].iov_len
;
352 EXPORT_SYMBOL_GPL(add_mtd_device
);
353 EXPORT_SYMBOL_GPL(del_mtd_device
);
354 EXPORT_SYMBOL_GPL(get_mtd_device
);
355 EXPORT_SYMBOL_GPL(get_mtd_device_nm
);
356 EXPORT_SYMBOL_GPL(put_mtd_device
);
357 EXPORT_SYMBOL_GPL(register_mtd_user
);
358 EXPORT_SYMBOL_GPL(unregister_mtd_user
);
359 EXPORT_SYMBOL_GPL(default_mtd_writev
);
361 #ifdef CONFIG_PROC_FS
363 /*====================================================================*/
364 /* Support for /proc/mtd */
366 static struct proc_dir_entry
*proc_mtd
;
368 static inline int mtd_proc_info (char *buf
, int i
)
370 struct mtd_info
*this = mtd_table
[i
];
375 return sprintf(buf
, "mtd%d: %8.8llx %8.8x \"%s\"\n", i
,
376 (unsigned long long)this->size
,
377 this->erasesize
, this->name
);
380 static int mtd_read_proc (char *page
, char **start
, off_t off
, int count
,
381 int *eof
, void *data_unused
)
386 mutex_lock(&mtd_table_mutex
);
388 len
= sprintf(page
, "dev: size erasesize name\n");
389 for (i
=0; i
< MAX_MTD_DEVICES
; i
++) {
391 l
= mtd_proc_info(page
+ len
, i
);
393 if (len
+begin
> off
+count
)
395 if (len
+begin
< off
) {
404 mutex_unlock(&mtd_table_mutex
);
405 if (off
>= len
+begin
)
407 *start
= page
+ (off
-begin
);
408 return ((count
< begin
+len
-off
) ? count
: begin
+len
-off
);
411 /*====================================================================*/
414 static int __init
init_mtd(void)
416 if ((proc_mtd
= create_proc_entry( "mtd", 0, NULL
)))
417 proc_mtd
->read_proc
= mtd_read_proc
;
421 static void __exit
cleanup_mtd(void)
424 remove_proc_entry( "mtd", NULL
);
427 module_init(init_mtd
);
428 module_exit(cleanup_mtd
);
430 #endif /* CONFIG_PROC_FS */
433 MODULE_LICENSE("GPL");
434 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
435 MODULE_DESCRIPTION("Core MTD registration and access routines");