2 * $Id: mtdchar.c,v 1.1.1.1 2005/04/11 02:50:25 jack Exp $
4 * Character-device access to raw MTD devices.
5 * Pure 2.4 version - compatibility cruft removed to mtdchar-compat.c
9 #include <linux/config.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/mtd/mtd.h>
13 #include <linux/slab.h>
15 #ifdef CONFIG_DEVFS_FS
16 #include <linux/devfs_fs_kernel.h>
17 static void mtd_notify_add(struct mtd_info* mtd);
18 static void mtd_notify_remove(struct mtd_info* mtd);
20 static struct mtd_notifier notifier = {
22 remove: mtd_notify_remove,
25 static devfs_handle_t devfs_dir_handle;
26 static devfs_handle_t devfs_rw_handle[MAX_MTD_DEVICES];
27 static devfs_handle_t devfs_ro_handle[MAX_MTD_DEVICES];
30 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
32 struct mtd_info *mtd=(struct mtd_info *)file->private_data;
41 file->f_pos += offset;
45 file->f_pos =mtd->size + offset;
53 else if (file->f_pos >= mtd->size)
54 file->f_pos = mtd->size - 1;
61 static int mtd_open(struct inode *inode, struct file *file)
63 int minor = minor(inode->i_rdev);
64 int devnum = minor >> 1;
67 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
69 if (devnum >= MAX_MTD_DEVICES)
72 /* You can't open the RO devices RW */
73 if ((file->f_mode & 2) && (minor & 1))
76 mtd = get_mtd_device(NULL, devnum);
81 if (MTD_ABSENT == mtd->type) {
86 file->private_data = mtd;
88 /* You can't open it RW if it's not a writeable device */
89 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
97 /*====================================================================*/
99 static int mtd_close(struct inode *inode, struct file *file)
101 struct mtd_info *mtd;
103 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
105 mtd = (struct mtd_info *)file->private_data;
115 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
116 userspace buffer down and use it directly with readv/writev.
118 #define MAX_KMALLOC_SIZE 0x20000
120 static ssize_t mtd_read(struct file *file, char *buf, size_t count,loff_t *ppos)
122 struct mtd_info *mtd = (struct mtd_info *)file->private_data;
124 size_t total_retlen=0;
129 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
131 if (*ppos + count > mtd->size)
132 count = mtd->size - *ppos;
137 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
138 and pass them directly to the MTD functions */
140 if (count > MAX_KMALLOC_SIZE)
141 len = MAX_KMALLOC_SIZE;
145 kbuf=kmalloc(len,GFP_KERNEL);
149 ret = MTD_READ(mtd, *ppos, len, &retlen, kbuf);
152 if (copy_to_user(buf, kbuf, retlen)) {
157 total_retlen += retlen;
173 static ssize_t mtd_write(struct file *file, const char *buf, size_t count,loff_t *ppos)
175 struct mtd_info *mtd = (struct mtd_info *)file->private_data;
178 size_t total_retlen=0;
182 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
184 if (*ppos == mtd->size)
187 if (*ppos + count > mtd->size)
188 count = mtd->size - *ppos;
194 if (count > MAX_KMALLOC_SIZE)
195 len = MAX_KMALLOC_SIZE;
199 kbuf=kmalloc(len,GFP_KERNEL);
201 printk("kmalloc is null\n");
205 if (copy_from_user(kbuf, buf, len)) {
210 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
213 total_retlen += retlen;
228 /*======================================================================
230 IOCTL calls for getting device parameters.
232 ======================================================================*/
233 static void mtd_erase_callback (struct erase_info *instr)
235 wake_up((wait_queue_head_t *)instr->priv);
238 static int mtd_ioctl(struct inode *inode, struct file *file,
239 u_int cmd, u_long arg)
241 struct mtd_info *mtd = (struct mtd_info *)file->private_data;
245 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
247 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
249 ret = verify_area(VERIFY_READ, (char *)arg, size);
253 ret = verify_area(VERIFY_WRITE, (char *)arg, size);
258 case MEMGETREGIONCOUNT:
259 if (copy_to_user((int *) arg, &(mtd->numeraseregions), sizeof(int)))
263 case MEMGETREGIONINFO:
265 struct region_info_user ur;
267 if (copy_from_user( &ur,
268 (struct region_info_user *)arg,
269 sizeof(struct region_info_user))) {
273 if (ur.regionindex >= mtd->numeraseregions)
275 if (copy_to_user((struct mtd_erase_region_info *) arg,
276 &(mtd->eraseregions[ur.regionindex]),
277 sizeof(struct mtd_erase_region_info)))
283 if (copy_to_user((struct mtd_info *)arg, mtd,
284 sizeof(struct mtd_info_user)))
290 struct erase_info *erase;
292 if(!(file->f_mode & 2))
295 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
299 wait_queue_head_t waitq;
300 DECLARE_WAITQUEUE(wait, current);
302 init_waitqueue_head(&waitq);
304 memset (erase,0,sizeof(struct erase_info));
305 if (copy_from_user(&erase->addr, (u_long *)arg,
306 2 * sizeof(u_long))) {
311 erase->callback = mtd_erase_callback;
312 erase->priv = (unsigned long)&waitq;
315 FIXME: Allow INTERRUPTIBLE. Which means
316 not having the wait_queue head on the stack.
318 If the wq_head is on the stack, and we
319 leave because we got interrupted, then the
320 wq_head is no longer there when the
321 callback routine tries to wake us up.
323 ret = mtd->erase(mtd, erase);
325 set_current_state(TASK_UNINTERRUPTIBLE);
326 add_wait_queue(&waitq, &wait);
327 if (erase->state != MTD_ERASE_DONE &&
328 erase->state != MTD_ERASE_FAILED)
330 remove_wait_queue(&waitq, &wait);
331 set_current_state(TASK_RUNNING);
333 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
342 struct mtd_oob_buf buf;
346 if(!(file->f_mode & 2))
349 if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
352 if (buf.length > 0x4096)
358 ret = verify_area(VERIFY_READ, (char *)buf.ptr, buf.length);
363 databuf = kmalloc(buf.length, GFP_KERNEL);
367 if (copy_from_user(databuf, buf.ptr, buf.length)) {
372 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
374 if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
384 struct mtd_oob_buf buf;
388 if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
391 if (buf.length > 0x4096)
397 ret = verify_area(VERIFY_WRITE, (char *)buf.ptr, buf.length);
402 databuf = kmalloc(buf.length, GFP_KERNEL);
406 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
408 if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
410 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
419 unsigned long adrs[2];
421 if (copy_from_user(adrs ,(void *)arg, 2* sizeof(unsigned long)))
427 ret = mtd->lock(mtd, adrs[0], adrs[1]);
433 unsigned long adrs[2];
435 if (copy_from_user(adrs, (void *)arg, 2* sizeof(unsigned long)))
441 ret = mtd->unlock(mtd, adrs[0], adrs[1]);
447 struct mtd_oob_buf buf;
451 if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
454 if (buf.length > 0x4096)
460 ret = verify_area(VERIFY_READ, (char *)buf.ptr, buf.length);
465 databuf = kmalloc(buf.length, GFP_KERNEL);
469 if (copy_from_user(databuf, buf.ptr, buf.length)) {
474 ret = (mtd->write_ecc)(mtd, buf.start, buf.length, &retlen, databuf, NULL, 0);
476 if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
486 struct mtd_oob_buf buf;
490 if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
493 if (buf.length > 0x4096)
499 ret = verify_area(VERIFY_WRITE, (char *)buf.ptr, buf.length);
504 databuf = kmalloc(buf.length, GFP_KERNEL);
508 ret = (mtd->read_ecc)(mtd, buf.start, buf.length, &retlen, databuf, NULL, 0);
510 if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
512 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
521 DEBUG(MTD_DEBUG_LEVEL0, "Invalid ioctl %x (MEMGETINFO = %x)\n", cmd, MEMGETINFO);
528 static struct file_operations mtd_fops = {
530 llseek: mtd_lseek, /* lseek */
531 read: mtd_read, /* read */
532 write: mtd_write, /* write */
533 ioctl: mtd_ioctl, /* ioctl */
534 open: mtd_open, /* open */
535 release: mtd_close, /* release */
539 #ifdef CONFIG_DEVFS_FS
540 /* Notification that a new device has been added. Create the devfs entry for
543 static void mtd_notify_add(struct mtd_info* mtd)
550 sprintf(name, "%d", mtd->index);
551 devfs_rw_handle[mtd->index] = devfs_register(devfs_dir_handle, name,
552 DEVFS_FL_DEFAULT, MTD_CHAR_MAJOR, mtd->index*2,
553 S_IFCHR | S_IRUGO | S_IWUGO,
556 sprintf(name, "%dro", mtd->index);
557 devfs_ro_handle[mtd->index] = devfs_register(devfs_dir_handle, name,
558 DEVFS_FL_DEFAULT, MTD_CHAR_MAJOR, mtd->index*2+1,
563 static void mtd_notify_remove(struct mtd_info* mtd)
568 devfs_unregister(devfs_rw_handle[mtd->index]);
569 devfs_unregister(devfs_ro_handle[mtd->index]);
573 static int __init init_mtdchar(void)
575 #ifdef CONFIG_DEVFS_FS
576 if (devfs_register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops))
578 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
583 devfs_dir_handle = devfs_mk_dir(NULL, "mtd", NULL);
585 register_mtd_user(¬ifier);
587 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops))
589 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
598 static void __exit cleanup_mtdchar(void)
600 #ifdef CONFIG_DEVFS_FS
601 unregister_mtd_user(¬ifier);
602 devfs_unregister(devfs_dir_handle);
603 devfs_unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
605 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
609 module_init(init_mtdchar);
610 module_exit(cleanup_mtdchar);
613 MODULE_LICENSE("GPL");
614 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
615 MODULE_DESCRIPTION("Direct character-device access to MTD devices");