2 * $Id: mtdchar.c,v 1.72 2005/06/30 00:23:24 tpoynor Exp $
4 * Character-device access to raw MTD devices.
8 #include <linux/config.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/mtd/mtd.h>
12 #include <linux/mtd/compatmac.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
16 #include <asm/uaccess.h>
18 #include <linux/device.h>
20 static struct class *mtd_class;
22 static void mtd_notify_add(struct mtd_info* mtd)
27 class_device_create(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
28 NULL, "mtd%d", mtd->index);
30 class_device_create(mtd_class,
31 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
32 NULL, "mtd%dro", mtd->index);
35 static void mtd_notify_remove(struct mtd_info* mtd)
40 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
41 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
44 static struct mtd_notifier notifier = {
45 .add = mtd_notify_add,
46 .remove = mtd_notify_remove,
50 * We use file->private_data to store a pointer to the MTDdevice.
51 * Since alighment is at least 32 bits, we have 2 bits free for OTP
55 #define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
57 #define MTD_MODE_OTP_FACT 1
58 #define MTD_MODE_OTP_USER 2
59 #define MTD_MODE(file) ((long)((file)->private_data) & 3)
61 #define SET_MTD_MODE(file, mode) \
62 do { long __p = (long)((file)->private_data); \
63 (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
65 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
67 struct mtd_info *mtd = TO_MTD(file);
76 file->f_pos += offset;
80 file->f_pos =mtd->size + offset;
88 else if (file->f_pos >= mtd->size)
89 file->f_pos = mtd->size - 1;
96 static int mtd_open(struct inode *inode, struct file *file)
98 int minor = iminor(inode);
99 int devnum = minor >> 1;
100 struct mtd_info *mtd;
102 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
104 if (devnum >= MAX_MTD_DEVICES)
107 /* You can't open the RO devices RW */
108 if ((file->f_mode & 2) && (minor & 1))
111 mtd = get_mtd_device(NULL, devnum);
116 if (MTD_ABSENT == mtd->type) {
121 file->private_data = mtd;
123 /* You can't open it RW if it's not a writeable device */
124 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
132 /*====================================================================*/
134 static int mtd_close(struct inode *inode, struct file *file)
136 struct mtd_info *mtd;
138 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
150 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
151 userspace buffer down and use it directly with readv/writev.
153 #define MAX_KMALLOC_SIZE 0x20000
155 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
157 struct mtd_info *mtd = TO_MTD(file);
159 size_t total_retlen=0;
164 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
166 if (*ppos + count > mtd->size)
167 count = mtd->size - *ppos;
172 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
173 and pass them directly to the MTD functions */
175 if (count > MAX_KMALLOC_SIZE)
176 len = MAX_KMALLOC_SIZE;
180 kbuf=kmalloc(len,GFP_KERNEL);
184 switch (MTD_MODE(file)) {
185 case MTD_MODE_OTP_FACT:
186 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
188 case MTD_MODE_OTP_USER:
189 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
192 ret = MTD_READ(mtd, *ppos, len, &retlen, kbuf);
194 /* Nand returns -EBADMSG on ecc errors, but it returns
195 * the data. For our userspace tools it is important
196 * to dump areas with ecc errors !
197 * Userspace software which accesses NAND this way
198 * must be aware of the fact that it deals with NAND
200 if (!ret || (ret == -EBADMSG)) {
202 if (copy_to_user(buf, kbuf, retlen)) {
207 total_retlen += retlen;
225 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
227 struct mtd_info *mtd = TO_MTD(file);
230 size_t total_retlen=0;
234 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
236 if (*ppos == mtd->size)
239 if (*ppos + count > mtd->size)
240 count = mtd->size - *ppos;
246 if (count > MAX_KMALLOC_SIZE)
247 len = MAX_KMALLOC_SIZE;
251 kbuf=kmalloc(len,GFP_KERNEL);
253 printk("kmalloc is null\n");
257 if (copy_from_user(kbuf, buf, len)) {
262 switch (MTD_MODE(file)) {
263 case MTD_MODE_OTP_FACT:
266 case MTD_MODE_OTP_USER:
267 if (!mtd->write_user_prot_reg) {
271 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
274 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
278 total_retlen += retlen;
293 /*======================================================================
295 IOCTL calls for getting device parameters.
297 ======================================================================*/
298 static void mtdchar_erase_callback (struct erase_info *instr)
300 wake_up((wait_queue_head_t *)instr->priv);
303 static int mtd_ioctl(struct inode *inode, struct file *file,
304 u_int cmd, u_long arg)
306 struct mtd_info *mtd = TO_MTD(file);
307 void __user *argp = (void __user *)arg;
311 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
313 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
315 if (!access_ok(VERIFY_READ, argp, size))
319 if (!access_ok(VERIFY_WRITE, argp, size))
324 case MEMGETREGIONCOUNT:
325 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
329 case MEMGETREGIONINFO:
331 struct region_info_user ur;
333 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
336 if (ur.regionindex >= mtd->numeraseregions)
338 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
339 sizeof(struct mtd_erase_region_info)))
345 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
351 struct erase_info *erase;
353 if(!(file->f_mode & 2))
356 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
360 wait_queue_head_t waitq;
361 DECLARE_WAITQUEUE(wait, current);
363 init_waitqueue_head(&waitq);
365 memset (erase,0,sizeof(struct erase_info));
366 if (copy_from_user(&erase->addr, argp,
367 sizeof(struct erase_info_user))) {
372 erase->callback = mtdchar_erase_callback;
373 erase->priv = (unsigned long)&waitq;
376 FIXME: Allow INTERRUPTIBLE. Which means
377 not having the wait_queue head on the stack.
379 If the wq_head is on the stack, and we
380 leave because we got interrupted, then the
381 wq_head is no longer there when the
382 callback routine tries to wake us up.
384 ret = mtd->erase(mtd, erase);
386 set_current_state(TASK_UNINTERRUPTIBLE);
387 add_wait_queue(&waitq, &wait);
388 if (erase->state != MTD_ERASE_DONE &&
389 erase->state != MTD_ERASE_FAILED)
391 remove_wait_queue(&waitq, &wait);
392 set_current_state(TASK_RUNNING);
394 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
403 struct mtd_oob_buf buf;
407 if(!(file->f_mode & 2))
410 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
413 if (buf.length > 0x4096)
419 ret = access_ok(VERIFY_READ, buf.ptr,
420 buf.length) ? 0 : EFAULT;
425 databuf = kmalloc(buf.length, GFP_KERNEL);
429 if (copy_from_user(databuf, buf.ptr, buf.length)) {
434 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
436 if (copy_to_user(argp + sizeof(uint32_t), &retlen, sizeof(uint32_t)))
446 struct mtd_oob_buf buf;
450 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
453 if (buf.length > 0x4096)
459 ret = access_ok(VERIFY_WRITE, buf.ptr,
460 buf.length) ? 0 : -EFAULT;
465 databuf = kmalloc(buf.length, GFP_KERNEL);
469 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
471 if (put_user(retlen, (uint32_t __user *)argp))
473 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
482 struct erase_info_user info;
484 if (copy_from_user(&info, argp, sizeof(info)))
490 ret = mtd->lock(mtd, info.start, info.length);
496 struct erase_info_user info;
498 if (copy_from_user(&info, argp, sizeof(info)))
504 ret = mtd->unlock(mtd, info.start, info.length);
510 if (copy_from_user(&mtd->oobinfo, argp, sizeof(struct nand_oobinfo)))
517 if (copy_to_user(argp, &(mtd->oobinfo), sizeof(struct nand_oobinfo)))
526 if (copy_from_user(&offs, argp, sizeof(loff_t)))
528 if (!mtd->block_isbad)
531 return mtd->block_isbad(mtd, offs);
539 if (copy_from_user(&offs, argp, sizeof(loff_t)))
541 if (!mtd->block_markbad)
544 return mtd->block_markbad(mtd, offs);
548 #ifdef CONFIG_MTD_OTP
552 if (copy_from_user(&mode, argp, sizeof(int)))
554 SET_MTD_MODE(file, 0);
556 case MTD_OTP_FACTORY:
557 if (!mtd->read_fact_prot_reg)
560 SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
563 if (!mtd->read_fact_prot_reg)
566 SET_MTD_MODE(file, MTD_MODE_OTP_USER);
577 case OTPGETREGIONCOUNT:
578 case OTPGETREGIONINFO:
580 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
584 switch (MTD_MODE(file)) {
585 case MTD_MODE_OTP_FACT:
586 if (mtd->get_fact_prot_info)
587 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
589 case MTD_MODE_OTP_USER:
590 if (mtd->get_user_prot_info)
591 ret = mtd->get_user_prot_info(mtd, buf, 4096);
595 if (cmd == OTPGETREGIONCOUNT) {
596 int nbr = ret / sizeof(struct otp_info);
597 ret = copy_to_user(argp, &nbr, sizeof(int));
599 ret = copy_to_user(argp, buf, ret);
609 struct otp_info info;
611 if (MTD_MODE(file) != MTD_MODE_OTP_USER)
613 if (copy_from_user(&info, argp, sizeof(info)))
615 if (!mtd->lock_user_prot_reg)
617 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
629 static struct file_operations mtd_fops = {
630 .owner = THIS_MODULE,
636 .release = mtd_close,
639 static int __init init_mtdchar(void)
641 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
642 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
647 mtd_class = class_create(THIS_MODULE, "mtd");
649 if (IS_ERR(mtd_class)) {
650 printk(KERN_ERR "Error creating mtd class.\n");
651 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
655 register_mtd_user(¬ifier);
659 static void __exit cleanup_mtdchar(void)
661 unregister_mtd_user(¬ifier);
662 class_destroy(mtd_class);
663 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
666 module_init(init_mtdchar);
667 module_exit(cleanup_mtdchar);
670 MODULE_LICENSE("GPL");
671 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
672 MODULE_DESCRIPTION("Direct character-device access to MTD devices");