2 * $Id: mtdchar.c,v 1.49 2003/01/24 12:02:58 dwmw2 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;
130 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
132 if (pos < 0 || pos > mtd->size)
135 if (count > mtd->size - pos)
136 count = mtd->size - pos;
141 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
142 and pass them directly to the MTD functions */
144 if (count > MAX_KMALLOC_SIZE)
145 len = MAX_KMALLOC_SIZE;
149 kbuf=kmalloc(len,GFP_KERNEL);
153 ret = MTD_READ(mtd, pos, len, &retlen, kbuf);
156 if (copy_to_user(buf, kbuf, retlen)) {
161 total_retlen += retlen;
179 static ssize_t mtd_write(struct file *file, const char *buf, size_t count,loff_t *ppos)
181 struct mtd_info *mtd = (struct mtd_info *)file->private_data;
184 size_t total_retlen=0;
189 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
191 if (pos < 0 || pos >= mtd->size)
194 if (count > mtd->size - pos)
195 count = mtd->size - pos;
201 if (count > MAX_KMALLOC_SIZE)
202 len = MAX_KMALLOC_SIZE;
206 kbuf=kmalloc(len,GFP_KERNEL);
208 printk("kmalloc is null\n");
212 if (copy_from_user(kbuf, buf, len)) {
217 ret = (*(mtd->write))(mtd, pos, len, &retlen, kbuf);
220 total_retlen += retlen;
236 /*======================================================================
238 IOCTL calls for getting device parameters.
240 ======================================================================*/
241 static void mtd_erase_callback (struct erase_info *instr)
243 wake_up((wait_queue_head_t *)instr->priv);
246 static int mtd_ioctl(struct inode *inode, struct file *file,
247 u_int cmd, u_long arg)
249 struct mtd_info *mtd = (struct mtd_info *)file->private_data;
253 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
255 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
257 ret = verify_area(VERIFY_READ, (char *)arg, size);
261 ret = verify_area(VERIFY_WRITE, (char *)arg, size);
266 case MEMGETREGIONCOUNT:
267 if (copy_to_user((int *) arg, &(mtd->numeraseregions), sizeof(int)))
271 case MEMGETREGIONINFO:
273 struct region_info_user ur;
275 if (copy_from_user( &ur,
276 (struct region_info_user *)arg,
277 sizeof(struct region_info_user))) {
281 if (ur.regionindex >= mtd->numeraseregions)
283 if (copy_to_user((struct mtd_erase_region_info *) arg,
284 &(mtd->eraseregions[ur.regionindex]),
285 sizeof(struct mtd_erase_region_info)))
291 if (copy_to_user((struct mtd_info *)arg, mtd,
292 sizeof(struct mtd_info_user)))
298 struct erase_info *erase;
300 if(!(file->f_mode & 2))
303 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
307 wait_queue_head_t waitq;
308 DECLARE_WAITQUEUE(wait, current);
310 init_waitqueue_head(&waitq);
312 memset (erase,0,sizeof(struct erase_info));
313 if (copy_from_user(&erase->addr, (u_long *)arg,
314 2 * sizeof(u_long))) {
319 erase->callback = mtd_erase_callback;
320 erase->priv = (unsigned long)&waitq;
323 FIXME: Allow INTERRUPTIBLE. Which means
324 not having the wait_queue head on the stack.
326 If the wq_head is on the stack, and we
327 leave because we got interrupted, then the
328 wq_head is no longer there when the
329 callback routine tries to wake us up.
331 ret = mtd->erase(mtd, erase);
333 set_current_state(TASK_UNINTERRUPTIBLE);
334 add_wait_queue(&waitq, &wait);
335 if (erase->state != MTD_ERASE_DONE &&
336 erase->state != MTD_ERASE_FAILED)
338 remove_wait_queue(&waitq, &wait);
339 set_current_state(TASK_RUNNING);
341 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
350 struct mtd_oob_buf buf;
354 if(!(file->f_mode & 2))
357 if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
360 if (buf.length > 0x4096)
366 ret = verify_area(VERIFY_READ, (char *)buf.ptr, buf.length);
371 databuf = kmalloc(buf.length, GFP_KERNEL);
375 if (copy_from_user(databuf, buf.ptr, buf.length)) {
380 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
382 if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
392 struct mtd_oob_buf buf;
396 if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
399 if (buf.length > 0x4096)
405 ret = verify_area(VERIFY_WRITE, (char *)buf.ptr, buf.length);
410 databuf = kmalloc(buf.length, GFP_KERNEL);
414 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
416 if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
418 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
427 unsigned long adrs[2];
429 if (copy_from_user(adrs ,(void *)arg, 2* sizeof(unsigned long)))
435 ret = mtd->lock(mtd, adrs[0], adrs[1]);
441 unsigned long adrs[2];
443 if (copy_from_user(adrs, (void *)arg, 2* sizeof(unsigned long)))
449 ret = mtd->unlock(mtd, adrs[0], adrs[1]);
455 struct mtd_oob_buf buf;
459 if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
462 if (buf.length > 0x4096)
468 ret = verify_area(VERIFY_READ, (char *)buf.ptr, buf.length);
473 databuf = kmalloc(buf.length, GFP_KERNEL);
477 if (copy_from_user(databuf, buf.ptr, buf.length)) {
482 ret = (mtd->write_ecc)(mtd, buf.start, buf.length, &retlen, databuf, NULL, 0);
484 if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
494 struct mtd_oob_buf buf;
498 if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
501 if (buf.length > 0x4096)
507 ret = verify_area(VERIFY_WRITE, (char *)buf.ptr, buf.length);
512 databuf = kmalloc(buf.length, GFP_KERNEL);
516 ret = (mtd->read_ecc)(mtd, buf.start, buf.length, &retlen, databuf, NULL, 0);
518 if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
520 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
529 DEBUG(MTD_DEBUG_LEVEL0, "Invalid ioctl %x (MEMGETINFO = %x)\n", cmd, MEMGETINFO);
536 static struct file_operations mtd_fops = {
538 llseek: mtd_lseek, /* lseek */
539 read: mtd_read, /* read */
540 write: mtd_write, /* write */
541 ioctl: mtd_ioctl, /* ioctl */
542 open: mtd_open, /* open */
543 release: mtd_close, /* release */
547 #ifdef CONFIG_DEVFS_FS
548 /* Notification that a new device has been added. Create the devfs entry for
551 static void mtd_notify_add(struct mtd_info* mtd)
558 sprintf(name, "%d", mtd->index);
559 devfs_rw_handle[mtd->index] = devfs_register(devfs_dir_handle, name,
560 DEVFS_FL_DEFAULT, MTD_CHAR_MAJOR, mtd->index*2,
561 S_IFCHR | S_IRUGO | S_IWUGO,
564 sprintf(name, "%dro", mtd->index);
565 devfs_ro_handle[mtd->index] = devfs_register(devfs_dir_handle, name,
566 DEVFS_FL_DEFAULT, MTD_CHAR_MAJOR, mtd->index*2+1,
571 static void mtd_notify_remove(struct mtd_info* mtd)
576 devfs_unregister(devfs_rw_handle[mtd->index]);
577 devfs_unregister(devfs_ro_handle[mtd->index]);
581 static int __init init_mtdchar(void)
583 #ifdef CONFIG_DEVFS_FS
584 if (devfs_register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops))
586 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
591 devfs_dir_handle = devfs_mk_dir(NULL, "mtd", NULL);
593 register_mtd_user(¬ifier);
595 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops))
597 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
606 static void __exit cleanup_mtdchar(void)
608 #ifdef CONFIG_DEVFS_FS
609 unregister_mtd_user(¬ifier);
610 devfs_unregister(devfs_dir_handle);
611 devfs_unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
613 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
617 module_init(init_mtdchar);
618 module_exit(cleanup_mtdchar);
621 MODULE_LICENSE("GPL");
622 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
623 MODULE_DESCRIPTION("Direct character-device access to MTD devices");