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 *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 *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;
244 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
246 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
248 ret = verify_area(VERIFY_READ, (char *)arg, size);
252 ret = verify_area(VERIFY_WRITE, (char *)arg, size);
257 case MEMGETREGIONCOUNT:
258 if (copy_to_user((int *) arg, &(mtd->numeraseregions), sizeof(int)))
262 case MEMGETREGIONINFO:
264 struct region_info_user ur;
266 if (copy_from_user( &ur,
267 (struct region_info_user *)arg,
268 sizeof(struct region_info_user))) {
272 if (ur.regionindex >= mtd->numeraseregions)
274 if (copy_to_user((struct mtd_erase_region_info *) arg,
275 &(mtd->eraseregions[ur.regionindex]),
276 sizeof(struct mtd_erase_region_info)))
282 if (copy_to_user((struct mtd_info *)arg, mtd,
283 sizeof(struct mtd_info_user)))
289 struct erase_info *erase;
291 if(!(file->f_mode & 2))
294 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
298 wait_queue_head_t waitq;
299 DECLARE_WAITQUEUE(wait, current);
301 init_waitqueue_head(&waitq);
303 memset (erase,0,sizeof(struct erase_info));
304 if (copy_from_user(&erase->addr, (u_long *)arg,
305 2 * sizeof(u_long))) {
310 erase->callback = mtd_erase_callback;
311 erase->priv = (unsigned long)&waitq;
314 FIXME: Allow INTERRUPTIBLE. Which means
315 not having the wait_queue head on the stack.
317 If the wq_head is on the stack, and we
318 leave because we got interrupted, then the
319 wq_head is no longer there when the
320 callback routine tries to wake us up.
322 ret = mtd->erase(mtd, erase);
324 set_current_state(TASK_UNINTERRUPTIBLE);
325 add_wait_queue(&waitq, &wait);
326 if (erase->state != MTD_ERASE_DONE &&
327 erase->state != MTD_ERASE_FAILED)
329 remove_wait_queue(&waitq, &wait);
330 set_current_state(TASK_RUNNING);
332 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
341 struct mtd_oob_buf buf;
345 if(!(file->f_mode & 2))
348 if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
351 if (buf.length > 0x4096)
357 ret = verify_area(VERIFY_READ, (char *)buf.ptr, buf.length);
362 databuf = kmalloc(buf.length, GFP_KERNEL);
366 if (copy_from_user(databuf, buf.ptr, buf.length)) {
371 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
373 if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
383 struct mtd_oob_buf buf;
387 if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
390 if (buf.length > 0x4096)
396 ret = verify_area(VERIFY_WRITE, (char *)buf.ptr, buf.length);
401 databuf = kmalloc(buf.length, GFP_KERNEL);
405 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
407 if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
409 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
418 unsigned long adrs[2];
420 if (copy_from_user(adrs ,(void *)arg, 2* sizeof(unsigned long)))
426 ret = mtd->lock(mtd, adrs[0], adrs[1]);
432 unsigned long adrs[2];
434 if (copy_from_user(adrs, (void *)arg, 2* sizeof(unsigned long)))
440 ret = mtd->unlock(mtd, adrs[0], adrs[1]);
446 if (copy_from_user(&mtd->oobinfo ,(void *)arg, sizeof(struct nand_oobinfo)))
452 DEBUG(MTD_DEBUG_LEVEL0, "Invalid ioctl %x (MEMGETINFO = %x)\n", cmd, MEMGETINFO);
459 static struct file_operations mtd_fops = {
460 .owner = THIS_MODULE,
466 .release = mtd_close,
470 #ifdef CONFIG_DEVFS_FS
471 /* Notification that a new device has been added. Create the devfs entry for
474 static void mtd_notify_add(struct mtd_info* mtd)
478 devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
479 S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%d", mtd->index);
480 devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
481 S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%dro", mtd->index);
484 static void mtd_notify_remove(struct mtd_info* mtd)
488 devfs_remove("mtd/%d", mtd->index);
489 devfs_remove("mtd/%dro", mtd->index);
493 static int __init init_mtdchar(void)
495 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
496 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
501 #ifdef CONFIG_DEVFS_FS
504 register_mtd_user(¬ifier);
509 static void __exit cleanup_mtdchar(void)
511 #ifdef CONFIG_DEVFS_FS
512 unregister_mtd_user(¬ifier);
515 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
518 module_init(init_mtdchar);
519 module_exit(cleanup_mtdchar);
522 MODULE_LICENSE("GPL");
523 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
524 MODULE_DESCRIPTION("Direct character-device access to MTD devices");