2 * $Id: mtdchar.c,v 1.54 2003/05/21 10:50:43 dwmw2 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/slab.h>
13 #include <linux/init.h>
15 #include <asm/uaccess.h>
17 #ifdef CONFIG_DEVFS_FS
18 #include <linux/devfs_fs_kernel.h>
19 static void mtd_notify_add(struct mtd_info* mtd);
20 static void mtd_notify_remove(struct mtd_info* mtd);
22 static struct mtd_notifier notifier = {
23 .add = mtd_notify_add,
24 .remove = mtd_notify_remove,
29 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
31 struct mtd_info *mtd=(struct mtd_info *)file->private_data;
40 file->f_pos += offset;
44 file->f_pos =mtd->size + offset;
52 else if (file->f_pos >= mtd->size)
53 file->f_pos = mtd->size - 1;
60 static int mtd_open(struct inode *inode, struct file *file)
62 int minor = iminor(inode);
63 int devnum = minor >> 1;
66 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
68 if (devnum >= MAX_MTD_DEVICES)
71 /* You can't open the RO devices RW */
72 if ((file->f_mode & 2) && (minor & 1))
75 mtd = get_mtd_device(NULL, devnum);
80 if (MTD_ABSENT == mtd->type) {
85 file->private_data = mtd;
87 /* You can't open it RW if it's not a writeable device */
88 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
96 /*====================================================================*/
98 static int mtd_close(struct inode *inode, struct file *file)
100 struct mtd_info *mtd;
102 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
104 mtd = (struct mtd_info *)file->private_data;
114 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
115 userspace buffer down and use it directly with readv/writev.
117 #define MAX_KMALLOC_SIZE 0x20000
119 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
121 struct mtd_info *mtd = (struct mtd_info *)file->private_data;
123 size_t total_retlen=0;
128 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
130 if (*ppos + count > mtd->size)
131 count = mtd->size - *ppos;
136 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
137 and pass them directly to the MTD functions */
139 if (count > MAX_KMALLOC_SIZE)
140 len = MAX_KMALLOC_SIZE;
144 kbuf=kmalloc(len,GFP_KERNEL);
148 ret = MTD_READ(mtd, *ppos, len, &retlen, kbuf);
151 if (copy_to_user(buf, kbuf, retlen)) {
156 total_retlen += retlen;
172 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
174 struct mtd_info *mtd = (struct mtd_info *)file->private_data;
177 size_t total_retlen=0;
181 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
183 if (*ppos == mtd->size)
186 if (*ppos + count > mtd->size)
187 count = mtd->size - *ppos;
193 if (count > MAX_KMALLOC_SIZE)
194 len = MAX_KMALLOC_SIZE;
198 kbuf=kmalloc(len,GFP_KERNEL);
200 printk("kmalloc is null\n");
204 if (copy_from_user(kbuf, buf, len)) {
209 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
212 total_retlen += retlen;
227 /*======================================================================
229 IOCTL calls for getting device parameters.
231 ======================================================================*/
232 static void mtd_erase_callback (struct erase_info *instr)
234 wake_up((wait_queue_head_t *)instr->priv);
237 static int mtd_ioctl(struct inode *inode, struct file *file,
238 u_int cmd, u_long arg)
240 struct mtd_info *mtd = (struct mtd_info *)file->private_data;
241 void __user *argp = (void __user *)arg;
245 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
247 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
249 ret = verify_area(VERIFY_READ, argp, size);
253 ret = verify_area(VERIFY_WRITE, argp, size);
258 case MEMGETREGIONCOUNT:
259 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
263 case MEMGETREGIONINFO:
265 struct region_info_user ur;
267 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
270 if (ur.regionindex >= mtd->numeraseregions)
272 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
273 sizeof(struct mtd_erase_region_info)))
279 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
285 struct erase_info *erase;
287 if(!(file->f_mode & 2))
290 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
294 wait_queue_head_t waitq;
295 DECLARE_WAITQUEUE(wait, current);
297 init_waitqueue_head(&waitq);
299 memset (erase,0,sizeof(struct erase_info));
300 if (copy_from_user(&erase->addr, argp,
301 2 * sizeof(u_long))) {
306 erase->callback = mtd_erase_callback;
307 erase->priv = (unsigned long)&waitq;
310 FIXME: Allow INTERRUPTIBLE. Which means
311 not having the wait_queue head on the stack.
313 If the wq_head is on the stack, and we
314 leave because we got interrupted, then the
315 wq_head is no longer there when the
316 callback routine tries to wake us up.
318 ret = mtd->erase(mtd, erase);
320 set_current_state(TASK_UNINTERRUPTIBLE);
321 add_wait_queue(&waitq, &wait);
322 if (erase->state != MTD_ERASE_DONE &&
323 erase->state != MTD_ERASE_FAILED)
325 remove_wait_queue(&waitq, &wait);
326 set_current_state(TASK_RUNNING);
328 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
337 struct mtd_oob_buf buf;
341 if(!(file->f_mode & 2))
344 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
347 if (buf.length > 0x4096)
353 ret = verify_area(VERIFY_READ, buf.ptr, buf.length);
358 databuf = kmalloc(buf.length, GFP_KERNEL);
362 if (copy_from_user(databuf, buf.ptr, buf.length)) {
367 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
369 if (copy_to_user(argp + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
379 struct mtd_oob_buf buf;
383 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
386 if (buf.length > 0x4096)
392 ret = verify_area(VERIFY_WRITE, buf.ptr, buf.length);
397 databuf = kmalloc(buf.length, GFP_KERNEL);
401 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
403 if (copy_to_user(argp + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
405 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
414 unsigned long adrs[2];
416 if (copy_from_user(adrs, argp, 2* sizeof(unsigned long)))
422 ret = mtd->lock(mtd, adrs[0], adrs[1]);
428 unsigned long adrs[2];
430 if (copy_from_user(adrs, argp, 2* sizeof(unsigned long)))
436 ret = mtd->unlock(mtd, adrs[0], adrs[1]);
442 if (copy_from_user(&mtd->oobinfo, argp, sizeof(struct nand_oobinfo)))
448 DEBUG(MTD_DEBUG_LEVEL0, "Invalid ioctl %x (MEMGETINFO = %x)\n", cmd, MEMGETINFO);
455 static struct file_operations mtd_fops = {
456 .owner = THIS_MODULE,
462 .release = mtd_close,
466 #ifdef CONFIG_DEVFS_FS
467 /* Notification that a new device has been added. Create the devfs entry for
470 static void mtd_notify_add(struct mtd_info* mtd)
474 devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
475 S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%d", mtd->index);
476 devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
477 S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%dro", mtd->index);
480 static void mtd_notify_remove(struct mtd_info* mtd)
484 devfs_remove("mtd/%d", mtd->index);
485 devfs_remove("mtd/%dro", mtd->index);
489 static int __init init_mtdchar(void)
491 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
492 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
497 #ifdef CONFIG_DEVFS_FS
500 register_mtd_user(¬ifier);
505 static void __exit cleanup_mtdchar(void)
507 #ifdef CONFIG_DEVFS_FS
508 unregister_mtd_user(¬ifier);
511 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
514 module_init(init_mtdchar);
515 module_exit(cleanup_mtdchar);
518 MODULE_LICENSE("GPL");
519 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
520 MODULE_DESCRIPTION("Direct character-device access to MTD devices");