blob: a8e69dd2b2e47f1d2ae9dc9bfe9bae7d2ec2f655 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Character-device access to raw MTD devices.
3 *
4 */
5
Thomas Gleixner15fdc522005-11-07 00:14:42 +01006#include <linux/device.h>
7#include <linux/fs.h>
Andrew Morton0c1eafd2007-08-10 13:01:06 -07008#include <linux/mm.h>
Artem Bityutskiy9c740342006-10-11 14:52:47 +03009#include <linux/err.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010010#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/module.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010013#include <linux/slab.h>
14#include <linux/sched.h>
Jonathan Corbet60712392008-05-15 10:10:37 -060015#include <linux/smp_lock.h>
David Howells402d3262009-02-12 10:40:00 +000016#include <linux/backing-dev.h>
Kevin Cernekee97718542009-04-08 22:53:13 -070017#include <linux/compat.h>
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030018#include <linux/mount.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/mtd/mtd.h>
21#include <linux/mtd/compatmac.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Thomas Gleixner15fdc522005-11-07 00:14:42 +010023#include <asm/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010024
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030025#define MTD_INODE_FS_MAGIC 0x11307854
26static struct vfsmount *mtd_inode_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Nicolas Pitre045e9a52005-02-08 19:12:53 +000028/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020029 * Data structure to hold the pointer to the mtd device as well
30 * as mode information ofr various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000031 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020032struct mtd_file_info {
33 struct mtd_info *mtd;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030034 struct inode *ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020035 enum mtd_file_modes mode;
36};
Nicolas Pitre31f42332005-02-08 17:45:55 +000037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
39{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020040 struct mtd_file_info *mfi = file->private_data;
41 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 switch (orig) {
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040044 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040046 case SEEK_CUR:
Todd Poynor8b491d72005-08-04 02:05:51 +010047 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040049 case SEEK_END:
Todd Poynor8b491d72005-08-04 02:05:51 +010050 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 break;
52 default:
53 return -EINVAL;
54 }
55
Herbert Valerio Riedel1887f512006-06-24 00:03:36 +020056 if (offset >= 0 && offset <= mtd->size)
Todd Poynor8b491d72005-08-04 02:05:51 +010057 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Todd Poynor8b491d72005-08-04 02:05:51 +010059 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
62
63
64static int mtd_open(struct inode *inode, struct file *file)
65{
66 int minor = iminor(inode);
67 int devnum = minor >> 1;
Jonathan Corbet60712392008-05-15 10:10:37 -060068 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020070 struct mtd_file_info *mfi;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030071 struct inode *mtd_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 /* You can't open the RO devices RW */
Al Viroaeb5d722008-09-02 15:28:45 -040076 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -EACCES;
78
Jonathan Corbet60712392008-05-15 10:10:37 -060079 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000081
Jonathan Corbet60712392008-05-15 10:10:37 -060082 if (IS_ERR(mtd)) {
83 ret = PTR_ERR(mtd);
84 goto out;
85 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +000086
David Howells402d3262009-02-12 10:40:00 +000087 if (mtd->type == MTD_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -060089 ret = -ENODEV;
90 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030093 mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum);
94 if (!mtd_ino) {
95 put_mtd_device(mtd);
96 ret = -ENOMEM;
97 goto out;
98 }
99 if (mtd_ino->i_state & I_NEW) {
100 mtd_ino->i_private = mtd;
101 mtd_ino->i_mode = S_IFCHR;
102 mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
103 unlock_new_inode(mtd_ino);
104 }
105 file->f_mapping = mtd_ino->i_mapping;
David Howells402d3262009-02-12 10:40:00 +0000106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* You can't open it RW if it's not a writeable device */
Al Viroaeb5d722008-09-02 15:28:45 -0400108 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300109 iput(mtd_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600111 ret = -EACCES;
112 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000114
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200115 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
116 if (!mfi) {
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300117 iput(mtd_ino);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200118 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600119 ret = -ENOMEM;
120 goto out;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200121 }
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300122 mfi->ino = mtd_ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200123 mfi->mtd = mtd;
124 file->private_data = mfi;
125
Jonathan Corbet60712392008-05-15 10:10:37 -0600126out:
127 unlock_kernel();
128 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129} /* mtd_open */
130
131/*====================================================================*/
132
133static int mtd_close(struct inode *inode, struct file *file)
134{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200135 struct mtd_file_info *mfi = file->private_data;
136 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
139
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200140 /* Only sync if opened RW */
Al Viroaeb5d722008-09-02 15:28:45 -0400141 if ((file->f_mode & FMODE_WRITE) && mtd->sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000143
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300144 iput(mfi->ino);
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200147 file->private_data = NULL;
148 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return 0;
151} /* mtd_close */
152
153/* FIXME: This _really_ needs to die. In 2.5, we should lock the
154 userspace buffer down and use it directly with readv/writev.
155*/
156#define MAX_KMALLOC_SIZE 0x20000
157
158static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
159{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200160 struct mtd_file_info *mfi = file->private_data;
161 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 size_t retlen=0;
163 size_t total_retlen=0;
164 int ret=0;
165 int len;
166 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
169
170 if (*ppos + count > mtd->size)
171 count = mtd->size - *ppos;
172
173 if (!count)
174 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
177 and pass them directly to the MTD functions */
Thago Galesib802c072006-04-17 17:38:15 +0100178
179 if (count > MAX_KMALLOC_SIZE)
180 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
181 else
182 kbuf=kmalloc(count, GFP_KERNEL);
183
184 if (!kbuf)
185 return -ENOMEM;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100188
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000189 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 len = MAX_KMALLOC_SIZE;
191 else
192 len = count;
193
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200194 switch (mfi->mode) {
195 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000196 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
197 break;
198 case MTD_MODE_OTP_USER:
199 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
200 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200201 case MTD_MODE_RAW:
202 {
203 struct mtd_oob_ops ops;
204
205 ops.mode = MTD_OOB_RAW;
206 ops.datbuf = kbuf;
207 ops.oobbuf = NULL;
208 ops.len = len;
209
210 ret = mtd->read_oob(mtd, *ppos, &ops);
211 retlen = ops.retlen;
212 break;
213 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000214 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200215 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* Nand returns -EBADMSG on ecc errors, but it returns
218 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000219 * to dump areas with ecc errors !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200220 * For kernel internal usage it also might return -EUCLEAN
221 * to signal the caller that a bitflip has occured and has
222 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * Userspace software which accesses NAND this way
224 * must be aware of the fact that it deals with NAND
225 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200226 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *ppos += retlen;
228 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200229 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -EFAULT;
231 }
232 else
233 total_retlen += retlen;
234
235 count -= retlen;
236 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000237 if (retlen == 0)
238 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 else {
241 kfree(kbuf);
242 return ret;
243 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
Thago Galesib802c072006-04-17 17:38:15 +0100247 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return total_retlen;
249} /* mtd_read */
250
251static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
252{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200253 struct mtd_file_info *mfi = file->private_data;
254 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 char *kbuf;
256 size_t retlen;
257 size_t total_retlen=0;
258 int ret=0;
259 int len;
260
261 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (*ppos == mtd->size)
264 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (*ppos + count > mtd->size)
267 count = mtd->size - *ppos;
268
269 if (!count)
270 return 0;
271
Thago Galesib802c072006-04-17 17:38:15 +0100272 if (count > MAX_KMALLOC_SIZE)
273 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
274 else
275 kbuf=kmalloc(count, GFP_KERNEL);
276
277 if (!kbuf)
278 return -ENOMEM;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100281
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000282 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 len = MAX_KMALLOC_SIZE;
284 else
285 len = count;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (copy_from_user(kbuf, buf, len)) {
288 kfree(kbuf);
289 return -EFAULT;
290 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000291
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200292 switch (mfi->mode) {
293 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000294 ret = -EROFS;
295 break;
296 case MTD_MODE_OTP_USER:
297 if (!mtd->write_user_prot_reg) {
298 ret = -EOPNOTSUPP;
299 break;
300 }
301 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
302 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200303
304 case MTD_MODE_RAW:
305 {
306 struct mtd_oob_ops ops;
307
308 ops.mode = MTD_OOB_RAW;
309 ops.datbuf = kbuf;
310 ops.oobbuf = NULL;
311 ops.len = len;
312
313 ret = mtd->write_oob(mtd, *ppos, &ops);
314 retlen = ops.retlen;
315 break;
316 }
317
Nicolas Pitre31f42332005-02-08 17:45:55 +0000318 default:
319 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (!ret) {
322 *ppos += retlen;
323 total_retlen += retlen;
324 count -= retlen;
325 buf += retlen;
326 }
327 else {
328 kfree(kbuf);
329 return ret;
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
Thago Galesib802c072006-04-17 17:38:15 +0100333 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return total_retlen;
335} /* mtd_write */
336
337/*======================================================================
338
339 IOCTL calls for getting device parameters.
340
341======================================================================*/
342static void mtdchar_erase_callback (struct erase_info *instr)
343{
344 wake_up((wait_queue_head_t *)instr->priv);
345}
346
David Brownell34a82442008-07-30 12:35:05 -0700347#ifdef CONFIG_HAVE_MTD_OTP
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200348static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
349{
350 struct mtd_info *mtd = mfi->mtd;
351 int ret = 0;
352
353 switch (mode) {
354 case MTD_OTP_FACTORY:
355 if (!mtd->read_fact_prot_reg)
356 ret = -EOPNOTSUPP;
357 else
358 mfi->mode = MTD_MODE_OTP_FACTORY;
359 break;
360 case MTD_OTP_USER:
361 if (!mtd->read_fact_prot_reg)
362 ret = -EOPNOTSUPP;
363 else
364 mfi->mode = MTD_MODE_OTP_USER;
365 break;
366 default:
367 ret = -EINVAL;
368 case MTD_OTP_OFF:
369 break;
370 }
371 return ret;
372}
373#else
374# define otp_select_filemode(f,m) -EOPNOTSUPP
375#endif
376
Kevin Cernekee97718542009-04-08 22:53:13 -0700377static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
378 uint64_t start, uint32_t length, void __user *ptr,
379 uint32_t __user *retp)
380{
381 struct mtd_oob_ops ops;
382 uint32_t retlen;
383 int ret = 0;
384
385 if (!(file->f_mode & FMODE_WRITE))
386 return -EPERM;
387
388 if (length > 4096)
389 return -EINVAL;
390
391 if (!mtd->write_oob)
392 ret = -EOPNOTSUPP;
393 else
Roel Kluin00404762010-01-29 10:35:04 +0100394 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700395
396 if (ret)
397 return ret;
398
399 ops.ooblen = length;
400 ops.ooboffs = start & (mtd->oobsize - 1);
401 ops.datbuf = NULL;
402 ops.mode = MTD_OOB_PLACE;
403
404 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
405 return -EINVAL;
406
Julia Lawalldf1f1d12010-05-22 10:22:49 +0200407 ops.oobbuf = memdup_user(ptr, length);
408 if (IS_ERR(ops.oobbuf))
409 return PTR_ERR(ops.oobbuf);
Kevin Cernekee97718542009-04-08 22:53:13 -0700410
411 start &= ~((uint64_t)mtd->oobsize - 1);
412 ret = mtd->write_oob(mtd, start, &ops);
413
414 if (ops.oobretlen > 0xFFFFFFFFU)
415 ret = -EOVERFLOW;
416 retlen = ops.oobretlen;
417 if (copy_to_user(retp, &retlen, sizeof(length)))
418 ret = -EFAULT;
419
420 kfree(ops.oobbuf);
421 return ret;
422}
423
424static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
425 uint32_t length, void __user *ptr, uint32_t __user *retp)
426{
427 struct mtd_oob_ops ops;
428 int ret = 0;
429
430 if (length > 4096)
431 return -EINVAL;
432
433 if (!mtd->read_oob)
434 ret = -EOPNOTSUPP;
435 else
436 ret = access_ok(VERIFY_WRITE, ptr,
437 length) ? 0 : -EFAULT;
438 if (ret)
439 return ret;
440
441 ops.ooblen = length;
442 ops.ooboffs = start & (mtd->oobsize - 1);
443 ops.datbuf = NULL;
444 ops.mode = MTD_OOB_PLACE;
445
446 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
447 return -EINVAL;
448
449 ops.oobbuf = kmalloc(length, GFP_KERNEL);
450 if (!ops.oobbuf)
451 return -ENOMEM;
452
453 start &= ~((uint64_t)mtd->oobsize - 1);
454 ret = mtd->read_oob(mtd, start, &ops);
455
456 if (put_user(ops.oobretlen, retp))
457 ret = -EFAULT;
458 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
459 ops.oobretlen))
460 ret = -EFAULT;
461
462 kfree(ops.oobbuf);
463 return ret;
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466static int mtd_ioctl(struct inode *inode, struct file *file,
467 u_int cmd, u_long arg)
468{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200469 struct mtd_file_info *mfi = file->private_data;
470 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 void __user *argp = (void __user *)arg;
472 int ret = 0;
473 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200474 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
477
478 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
479 if (cmd & IOC_IN) {
480 if (!access_ok(VERIFY_READ, argp, size))
481 return -EFAULT;
482 }
483 if (cmd & IOC_OUT) {
484 if (!access_ok(VERIFY_WRITE, argp, size))
485 return -EFAULT;
486 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 switch (cmd) {
489 case MEMGETREGIONCOUNT:
490 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
491 return -EFAULT;
492 break;
493
494 case MEMGETREGIONINFO:
495 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700496 uint32_t ur_idx;
497 struct mtd_erase_region_info *kr;
H Hartley Sweetenbcc98a42010-01-15 11:25:38 -0700498 struct region_info_user __user *ur = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Zev Weissb67c5f82008-09-01 05:02:12 -0700500 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -EFAULT;
502
Zev Weissb67c5f82008-09-01 05:02:12 -0700503 kr = &(mtd->eraseregions[ur_idx]);
504
505 if (put_user(kr->offset, &(ur->offset))
506 || put_user(kr->erasesize, &(ur->erasesize))
507 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 break;
511 }
512
513 case MEMGETINFO:
Joern Engel73c619e2006-05-30 14:25:35 +0200514 info.type = mtd->type;
515 info.flags = mtd->flags;
516 info.size = mtd->size;
517 info.erasesize = mtd->erasesize;
518 info.writesize = mtd->writesize;
519 info.oobsize = mtd->oobsize;
Artem Bityutskiy64f60712007-01-30 10:50:43 +0200520 /* The below fields are obsolete */
521 info.ecctype = -1;
522 info.eccsize = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200523 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return -EFAULT;
525 break;
526
527 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700528 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 {
530 struct erase_info *erase;
531
Al Viroaeb5d722008-09-02 15:28:45 -0400532 if(!(file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return -EPERM;
534
Burman Yan95b93a02006-11-15 21:10:29 +0200535 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (!erase)
537 ret = -ENOMEM;
538 else {
539 wait_queue_head_t waitq;
540 DECLARE_WAITQUEUE(wait, current);
541
542 init_waitqueue_head(&waitq);
543
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700544 if (cmd == MEMERASE64) {
545 struct erase_info_user64 einfo64;
546
547 if (copy_from_user(&einfo64, argp,
548 sizeof(struct erase_info_user64))) {
549 kfree(erase);
550 return -EFAULT;
551 }
552 erase->addr = einfo64.start;
553 erase->len = einfo64.length;
554 } else {
555 struct erase_info_user einfo32;
556
557 if (copy_from_user(&einfo32, argp,
558 sizeof(struct erase_info_user))) {
559 kfree(erase);
560 return -EFAULT;
561 }
562 erase->addr = einfo32.start;
563 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 erase->mtd = mtd;
566 erase->callback = mtdchar_erase_callback;
567 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /*
570 FIXME: Allow INTERRUPTIBLE. Which means
571 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 If the wq_head is on the stack, and we
574 leave because we got interrupted, then the
575 wq_head is no longer there when the
576 callback routine tries to wake us up.
577 */
578 ret = mtd->erase(mtd, erase);
579 if (!ret) {
580 set_current_state(TASK_UNINTERRUPTIBLE);
581 add_wait_queue(&waitq, &wait);
582 if (erase->state != MTD_ERASE_DONE &&
583 erase->state != MTD_ERASE_FAILED)
584 schedule();
585 remove_wait_queue(&waitq, &wait);
586 set_current_state(TASK_RUNNING);
587
588 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
589 }
590 kfree(erase);
591 }
592 break;
593 }
594
595 case MEMWRITEOOB:
596 {
597 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700598 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000599
Kevin Cernekee97718542009-04-08 22:53:13 -0700600 /* NOTE: writes return length to buf_user->length */
601 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700603 else
604 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
605 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 case MEMREADOOB:
610 {
611 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700612 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Kevin Cernekee97718542009-04-08 22:53:13 -0700614 /* NOTE: writes return length to buf_user->start */
615 if (copy_from_user(&buf, argp, sizeof(buf)))
616 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 else
Kevin Cernekee97718542009-04-08 22:53:13 -0700618 ret = mtd_do_readoob(mtd, buf.start, buf.length,
619 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
621 }
622
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700623 case MEMWRITEOOB64:
624 {
625 struct mtd_oob_buf64 buf;
626 struct mtd_oob_buf64 __user *buf_user = argp;
627
628 if (copy_from_user(&buf, argp, sizeof(buf)))
629 ret = -EFAULT;
630 else
631 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
632 (void __user *)(uintptr_t)buf.usr_ptr,
633 &buf_user->length);
634 break;
635 }
636
637 case MEMREADOOB64:
638 {
639 struct mtd_oob_buf64 buf;
640 struct mtd_oob_buf64 __user *buf_user = argp;
641
642 if (copy_from_user(&buf, argp, sizeof(buf)))
643 ret = -EFAULT;
644 else
645 ret = mtd_do_readoob(mtd, buf.start, buf.length,
646 (void __user *)(uintptr_t)buf.usr_ptr,
647 &buf_user->length);
648 break;
649 }
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 case MEMLOCK:
652 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700653 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Harvey Harrison175428b2008-07-03 23:40:14 -0700655 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return -EFAULT;
657
658 if (!mtd->lock)
659 ret = -EOPNOTSUPP;
660 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700661 ret = mtd->lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
663 }
664
665 case MEMUNLOCK:
666 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700667 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Harvey Harrison175428b2008-07-03 23:40:14 -0700669 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -EFAULT;
671
672 if (!mtd->unlock)
673 ret = -EOPNOTSUPP;
674 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700675 ret = mtd->unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 break;
677 }
678
Richard Cochran99384242010-06-14 18:10:33 +0200679 case MEMISLOCKED:
680 {
681 struct erase_info_user einfo;
682
683 if (copy_from_user(&einfo, argp, sizeof(einfo)))
684 return -EFAULT;
685
686 if (!mtd->is_locked)
687 ret = -EOPNOTSUPP;
688 else
689 ret = mtd->is_locked(mtd, einfo.start, einfo.length);
690 break;
691 }
692
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200693 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 case MEMGETOOBSEL:
695 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200696 struct nand_oobinfo oi;
697
698 if (!mtd->ecclayout)
699 return -EOPNOTSUPP;
700 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
701 return -EINVAL;
702
703 oi.useecc = MTD_NANDECC_AUTOPLACE;
704 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
705 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
706 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200707 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200708
709 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -EFAULT;
711 break;
712 }
713
714 case MEMGETBADBLOCK:
715 {
716 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (copy_from_user(&offs, argp, sizeof(loff_t)))
719 return -EFAULT;
720 if (!mtd->block_isbad)
721 ret = -EOPNOTSUPP;
722 else
723 return mtd->block_isbad(mtd, offs);
724 break;
725 }
726
727 case MEMSETBADBLOCK:
728 {
729 loff_t offs;
730
731 if (copy_from_user(&offs, argp, sizeof(loff_t)))
732 return -EFAULT;
733 if (!mtd->block_markbad)
734 ret = -EOPNOTSUPP;
735 else
736 return mtd->block_markbad(mtd, offs);
737 break;
738 }
739
David Brownell34a82442008-07-30 12:35:05 -0700740#ifdef CONFIG_HAVE_MTD_OTP
Nicolas Pitre31f42332005-02-08 17:45:55 +0000741 case OTPSELECT:
742 {
743 int mode;
744 if (copy_from_user(&mode, argp, sizeof(int)))
745 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200746
747 mfi->mode = MTD_MODE_NORMAL;
748
749 ret = otp_select_filemode(mfi, mode);
750
Nicolas Pitre81dba482005-04-01 16:36:15 +0100751 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000752 break;
753 }
754
755 case OTPGETREGIONCOUNT:
756 case OTPGETREGIONINFO:
757 {
758 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
759 if (!buf)
760 return -ENOMEM;
761 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200762 switch (mfi->mode) {
763 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000764 if (mtd->get_fact_prot_info)
765 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
766 break;
767 case MTD_MODE_OTP_USER:
768 if (mtd->get_user_prot_info)
769 ret = mtd->get_user_prot_info(mtd, buf, 4096);
770 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200771 default:
772 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000773 }
774 if (ret >= 0) {
775 if (cmd == OTPGETREGIONCOUNT) {
776 int nbr = ret / sizeof(struct otp_info);
777 ret = copy_to_user(argp, &nbr, sizeof(int));
778 } else
779 ret = copy_to_user(argp, buf, ret);
780 if (ret)
781 ret = -EFAULT;
782 }
783 kfree(buf);
784 break;
785 }
786
787 case OTPLOCK:
788 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700789 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000790
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200791 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000792 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700793 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000794 return -EFAULT;
795 if (!mtd->lock_user_prot_reg)
796 return -EOPNOTSUPP;
Harvey Harrison175428b2008-07-03 23:40:14 -0700797 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000798 break;
799 }
800#endif
801
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200802 case ECCGETLAYOUT:
803 {
804 if (!mtd->ecclayout)
805 return -EOPNOTSUPP;
806
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200807 if (copy_to_user(argp, mtd->ecclayout,
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200808 sizeof(struct nand_ecclayout)))
809 return -EFAULT;
810 break;
811 }
812
813 case ECCGETSTATS:
814 {
815 if (copy_to_user(argp, &mtd->ecc_stats,
816 sizeof(struct mtd_ecc_stats)))
817 return -EFAULT;
818 break;
819 }
820
821 case MTDFILEMODE:
822 {
823 mfi->mode = 0;
824
825 switch(arg) {
826 case MTD_MODE_OTP_FACTORY:
827 case MTD_MODE_OTP_USER:
828 ret = otp_select_filemode(mfi, arg);
829 break;
830
831 case MTD_MODE_RAW:
832 if (!mtd->read_oob || !mtd->write_oob)
833 return -EOPNOTSUPP;
834 mfi->mode = arg;
835
836 case MTD_MODE_NORMAL:
837 break;
838 default:
839 ret = -EINVAL;
840 }
841 file->f_pos = 0;
842 break;
843 }
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 default:
846 ret = -ENOTTY;
847 }
848
849 return ret;
850} /* memory_ioctl */
851
Kevin Cernekee97718542009-04-08 22:53:13 -0700852#ifdef CONFIG_COMPAT
853
854struct mtd_oob_buf32 {
855 u_int32_t start;
856 u_int32_t length;
857 compat_caddr_t ptr; /* unsigned char* */
858};
859
860#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
861#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
862
863static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
864 unsigned long arg)
865{
Kevin Cernekee668ff9a2009-04-14 21:59:22 -0700866 struct inode *inode = file->f_path.dentry->d_inode;
Kevin Cernekee97718542009-04-08 22:53:13 -0700867 struct mtd_file_info *mfi = file->private_data;
868 struct mtd_info *mtd = mfi->mtd;
David Woodhouse0b6585c2009-05-29 16:09:08 +0100869 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -0700870 int ret = 0;
871
872 lock_kernel();
873
874 switch (cmd) {
875 case MEMWRITEOOB32:
876 {
877 struct mtd_oob_buf32 buf;
878 struct mtd_oob_buf32 __user *buf_user = argp;
879
880 if (copy_from_user(&buf, argp, sizeof(buf)))
881 ret = -EFAULT;
882 else
883 ret = mtd_do_writeoob(file, mtd, buf.start,
884 buf.length, compat_ptr(buf.ptr),
885 &buf_user->length);
886 break;
887 }
888
889 case MEMREADOOB32:
890 {
891 struct mtd_oob_buf32 buf;
892 struct mtd_oob_buf32 __user *buf_user = argp;
893
894 /* NOTE: writes return length to buf->start */
895 if (copy_from_user(&buf, argp, sizeof(buf)))
896 ret = -EFAULT;
897 else
898 ret = mtd_do_readoob(mtd, buf.start,
899 buf.length, compat_ptr(buf.ptr),
900 &buf_user->start);
901 break;
902 }
903 default:
David Woodhouse0b6585c2009-05-29 16:09:08 +0100904 ret = mtd_ioctl(inode, file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -0700905 }
906
907 unlock_kernel();
908
909 return ret;
910}
911
912#endif /* CONFIG_COMPAT */
913
David Howells402d3262009-02-12 10:40:00 +0000914/*
915 * try to determine where a shared mapping can be made
916 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
917 * mappings)
918 */
919#ifndef CONFIG_MMU
920static unsigned long mtd_get_unmapped_area(struct file *file,
921 unsigned long addr,
922 unsigned long len,
923 unsigned long pgoff,
924 unsigned long flags)
925{
926 struct mtd_file_info *mfi = file->private_data;
927 struct mtd_info *mtd = mfi->mtd;
928
929 if (mtd->get_unmapped_area) {
930 unsigned long offset;
931
932 if (addr != 0)
933 return (unsigned long) -EINVAL;
934
935 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
936 return (unsigned long) -EINVAL;
937
938 offset = pgoff << PAGE_SHIFT;
939 if (offset > mtd->size - len)
940 return (unsigned long) -EINVAL;
941
942 return mtd->get_unmapped_area(mtd, len, offset, flags);
943 }
944
945 /* can't map directly */
946 return (unsigned long) -ENOSYS;
947}
948#endif
949
950/*
951 * set up a mapping for shared memory segments
952 */
953static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
954{
955#ifdef CONFIG_MMU
956 struct mtd_file_info *mfi = file->private_data;
957 struct mtd_info *mtd = mfi->mtd;
958
959 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM)
960 return 0;
961 return -ENOSYS;
962#else
963 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
964#endif
965}
966
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800967static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 .owner = THIS_MODULE,
969 .llseek = mtd_lseek,
970 .read = mtd_read,
971 .write = mtd_write,
972 .ioctl = mtd_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -0700973#ifdef CONFIG_COMPAT
974 .compat_ioctl = mtd_compat_ioctl,
975#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 .open = mtd_open,
977 .release = mtd_close,
David Howells402d3262009-02-12 10:40:00 +0000978 .mmap = mtd_mmap,
979#ifndef CONFIG_MMU
980 .get_unmapped_area = mtd_get_unmapped_area,
981#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982};
983
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300984static int mtd_inodefs_get_sb(struct file_system_type *fs_type, int flags,
985 const char *dev_name, void *data,
986 struct vfsmount *mnt)
987{
988 return get_sb_pseudo(fs_type, "mtd_inode:", NULL, MTD_INODE_FS_MAGIC,
989 mnt);
990}
991
992static struct file_system_type mtd_inodefs_type = {
993 .name = "mtd_inodefs",
994 .get_sb = mtd_inodefs_get_sb,
995 .kill_sb = kill_anon_super,
996};
997
998static void mtdchar_notify_add(struct mtd_info *mtd)
999{
1000}
1001
1002static void mtdchar_notify_remove(struct mtd_info *mtd)
1003{
1004 struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
1005
1006 if (mtd_ino) {
1007 /* Destroy the inode if it exists */
1008 mtd_ino->i_nlink = 0;
1009 iput(mtd_ino);
1010 }
1011}
1012
1013static struct mtd_notifier mtdchar_notifier = {
1014 .add = mtdchar_notify_add,
1015 .remove = mtdchar_notify_remove,
1016};
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018static int __init init_mtdchar(void)
1019{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001020 int ret;
David Brownell1f24b5a2009-03-26 00:42:41 -07001021
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001022 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001023 "mtd", &mtd_fops);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001024 if (ret < 0) {
1025 pr_notice("Can't allocate major number %d for "
1026 "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
1027 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001030 ret = register_filesystem(&mtd_inodefs_type);
1031 if (ret) {
1032 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
1033 goto err_unregister_chdev;
1034 }
1035
1036 mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
1037 if (IS_ERR(mtd_inode_mnt)) {
1038 ret = PTR_ERR(mtd_inode_mnt);
1039 pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
1040 goto err_unregister_filesystem;
1041 }
1042 register_mtd_user(&mtdchar_notifier);
1043
1044 return ret;
1045
1046err_unregister_filesystem:
1047 unregister_filesystem(&mtd_inodefs_type);
1048err_unregister_chdev:
1049 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1050 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053static void __exit cleanup_mtdchar(void)
1054{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001055 unregister_mtd_user(&mtdchar_notifier);
1056 mntput(mtd_inode_mnt);
1057 unregister_filesystem(&mtd_inodefs_type);
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001058 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061module_init(init_mtdchar);
1062module_exit(cleanup_mtdchar);
1063
David Brownell1f24b5a2009-03-26 00:42:41 -07001064MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066MODULE_LICENSE("GPL");
1067MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1068MODULE_DESCRIPTION("Direct character-device access to MTD devices");
Scott James Remnant90160e12009-03-02 18:42:39 +00001069MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);