Merge branch 'master' of /home/tglx/work/mtd/git/linux-2.6.git/
[linux-2.6.22.y-op.git] / drivers / mtd / mtdchar.c
blob8c0d94b72b63671cae6c1aa51ab30bfa9957da96
1 /*
2 * $Id: mtdchar.c,v 1.74 2005/08/04 01:05:48 tpoynor Exp $
4 * Character-device access to raw MTD devices.
6 */
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>
15 #include <linux/fs.h>
16 #include <linux/sched.h> /* TASK_* */
17 #include <asm/uaccess.h>
19 #include <linux/device.h>
21 static struct class *mtd_class;
23 static void mtd_notify_add(struct mtd_info* mtd)
25 if (!mtd)
26 return;
28 class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
29 NULL, "mtd%d", mtd->index);
31 class_device_create(mtd_class, NULL,
32 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
33 NULL, "mtd%dro", mtd->index);
36 static void mtd_notify_remove(struct mtd_info* mtd)
38 if (!mtd)
39 return;
41 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
42 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
45 static struct mtd_notifier notifier = {
46 .add = mtd_notify_add,
47 .remove = mtd_notify_remove,
51 * We use file->private_data to store a pointer to the MTDdevice.
52 * Since alighment is at least 32 bits, we have 2 bits free for OTP
53 * modes as well.
56 #define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
58 #define MTD_MODE_OTP_FACT 1
59 #define MTD_MODE_OTP_USER 2
60 #define MTD_MODE(file) ((long)((file)->private_data) & 3)
62 #define SET_MTD_MODE(file, mode) \
63 do { long __p = (long)((file)->private_data); \
64 (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
66 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
68 struct mtd_info *mtd = TO_MTD(file);
70 switch (orig) {
71 case 0:
72 /* SEEK_SET */
73 break;
74 case 1:
75 /* SEEK_CUR */
76 offset += file->f_pos;
77 break;
78 case 2:
79 /* SEEK_END */
80 offset += mtd->size;
81 break;
82 default:
83 return -EINVAL;
86 if (offset >= 0 && offset < mtd->size)
87 return file->f_pos = offset;
89 return -EINVAL;
94 static int mtd_open(struct inode *inode, struct file *file)
96 int minor = iminor(inode);
97 int devnum = minor >> 1;
98 struct mtd_info *mtd;
100 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
102 if (devnum >= MAX_MTD_DEVICES)
103 return -ENODEV;
105 /* You can't open the RO devices RW */
106 if ((file->f_mode & 2) && (minor & 1))
107 return -EACCES;
109 mtd = get_mtd_device(NULL, devnum);
111 if (!mtd)
112 return -ENODEV;
114 if (MTD_ABSENT == mtd->type) {
115 put_mtd_device(mtd);
116 return -ENODEV;
119 file->private_data = mtd;
121 /* You can't open it RW if it's not a writeable device */
122 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
123 put_mtd_device(mtd);
124 return -EACCES;
127 return 0;
128 } /* mtd_open */
130 /*====================================================================*/
132 static int mtd_close(struct inode *inode, struct file *file)
134 struct mtd_info *mtd;
136 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
138 mtd = TO_MTD(file);
140 if (mtd->sync)
141 mtd->sync(mtd);
143 put_mtd_device(mtd);
145 return 0;
146 } /* mtd_close */
148 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
149 userspace buffer down and use it directly with readv/writev.
151 #define MAX_KMALLOC_SIZE 0x20000
153 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
155 struct mtd_info *mtd = TO_MTD(file);
156 size_t retlen=0;
157 size_t total_retlen=0;
158 int ret=0;
159 int len;
160 char *kbuf;
162 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
164 if (*ppos + count > mtd->size)
165 count = mtd->size - *ppos;
167 if (!count)
168 return 0;
170 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
171 and pass them directly to the MTD functions */
172 while (count) {
173 if (count > MAX_KMALLOC_SIZE)
174 len = MAX_KMALLOC_SIZE;
175 else
176 len = count;
178 kbuf=kmalloc(len,GFP_KERNEL);
179 if (!kbuf)
180 return -ENOMEM;
182 switch (MTD_MODE(file)) {
183 case MTD_MODE_OTP_FACT:
184 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
185 break;
186 case MTD_MODE_OTP_USER:
187 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
188 break;
189 default:
190 ret = MTD_READ(mtd, *ppos, len, &retlen, kbuf);
192 /* Nand returns -EBADMSG on ecc errors, but it returns
193 * the data. For our userspace tools it is important
194 * to dump areas with ecc errors !
195 * Userspace software which accesses NAND this way
196 * must be aware of the fact that it deals with NAND
198 if (!ret || (ret == -EBADMSG)) {
199 *ppos += retlen;
200 if (copy_to_user(buf, kbuf, retlen)) {
201 kfree(kbuf);
202 return -EFAULT;
204 else
205 total_retlen += retlen;
207 count -= retlen;
208 buf += retlen;
209 if (retlen == 0)
210 count = 0;
212 else {
213 kfree(kbuf);
214 return ret;
217 kfree(kbuf);
220 return total_retlen;
221 } /* mtd_read */
223 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
225 struct mtd_info *mtd = TO_MTD(file);
226 char *kbuf;
227 size_t retlen;
228 size_t total_retlen=0;
229 int ret=0;
230 int len;
232 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
234 if (*ppos == mtd->size)
235 return -ENOSPC;
237 if (*ppos + count > mtd->size)
238 count = mtd->size - *ppos;
240 if (!count)
241 return 0;
243 while (count) {
244 if (count > MAX_KMALLOC_SIZE)
245 len = MAX_KMALLOC_SIZE;
246 else
247 len = count;
249 kbuf=kmalloc(len,GFP_KERNEL);
250 if (!kbuf) {
251 printk("kmalloc is null\n");
252 return -ENOMEM;
255 if (copy_from_user(kbuf, buf, len)) {
256 kfree(kbuf);
257 return -EFAULT;
260 switch (MTD_MODE(file)) {
261 case MTD_MODE_OTP_FACT:
262 ret = -EROFS;
263 break;
264 case MTD_MODE_OTP_USER:
265 if (!mtd->write_user_prot_reg) {
266 ret = -EOPNOTSUPP;
267 break;
269 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
270 break;
271 default:
272 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
274 if (!ret) {
275 *ppos += retlen;
276 total_retlen += retlen;
277 count -= retlen;
278 buf += retlen;
280 else {
281 kfree(kbuf);
282 return ret;
285 kfree(kbuf);
288 return total_retlen;
289 } /* mtd_write */
291 /*======================================================================
293 IOCTL calls for getting device parameters.
295 ======================================================================*/
296 static void mtdchar_erase_callback (struct erase_info *instr)
298 wake_up((wait_queue_head_t *)instr->priv);
301 static int mtd_ioctl(struct inode *inode, struct file *file,
302 u_int cmd, u_long arg)
304 struct mtd_info *mtd = TO_MTD(file);
305 void __user *argp = (void __user *)arg;
306 int ret = 0;
307 u_long size;
309 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
311 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
312 if (cmd & IOC_IN) {
313 if (!access_ok(VERIFY_READ, argp, size))
314 return -EFAULT;
316 if (cmd & IOC_OUT) {
317 if (!access_ok(VERIFY_WRITE, argp, size))
318 return -EFAULT;
321 switch (cmd) {
322 case MEMGETREGIONCOUNT:
323 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
324 return -EFAULT;
325 break;
327 case MEMGETREGIONINFO:
329 struct region_info_user ur;
331 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
332 return -EFAULT;
334 if (ur.regionindex >= mtd->numeraseregions)
335 return -EINVAL;
336 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
337 sizeof(struct mtd_erase_region_info)))
338 return -EFAULT;
339 break;
342 case MEMGETINFO:
343 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
344 return -EFAULT;
345 break;
347 case MEMERASE:
349 struct erase_info *erase;
351 if(!(file->f_mode & 2))
352 return -EPERM;
354 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
355 if (!erase)
356 ret = -ENOMEM;
357 else {
358 wait_queue_head_t waitq;
359 DECLARE_WAITQUEUE(wait, current);
361 init_waitqueue_head(&waitq);
363 memset (erase,0,sizeof(struct erase_info));
364 if (copy_from_user(&erase->addr, argp,
365 sizeof(struct erase_info_user))) {
366 kfree(erase);
367 return -EFAULT;
369 erase->mtd = mtd;
370 erase->callback = mtdchar_erase_callback;
371 erase->priv = (unsigned long)&waitq;
374 FIXME: Allow INTERRUPTIBLE. Which means
375 not having the wait_queue head on the stack.
377 If the wq_head is on the stack, and we
378 leave because we got interrupted, then the
379 wq_head is no longer there when the
380 callback routine tries to wake us up.
382 ret = mtd->erase(mtd, erase);
383 if (!ret) {
384 set_current_state(TASK_UNINTERRUPTIBLE);
385 add_wait_queue(&waitq, &wait);
386 if (erase->state != MTD_ERASE_DONE &&
387 erase->state != MTD_ERASE_FAILED)
388 schedule();
389 remove_wait_queue(&waitq, &wait);
390 set_current_state(TASK_RUNNING);
392 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
394 kfree(erase);
396 break;
399 case MEMWRITEOOB:
401 struct mtd_oob_buf buf;
402 void *databuf;
403 ssize_t retlen;
405 if(!(file->f_mode & 2))
406 return -EPERM;
408 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
409 return -EFAULT;
411 if (buf.length > 0x4096)
412 return -EINVAL;
414 if (!mtd->write_oob)
415 ret = -EOPNOTSUPP;
416 else
417 ret = access_ok(VERIFY_READ, buf.ptr,
418 buf.length) ? 0 : EFAULT;
420 if (ret)
421 return ret;
423 databuf = kmalloc(buf.length, GFP_KERNEL);
424 if (!databuf)
425 return -ENOMEM;
427 if (copy_from_user(databuf, buf.ptr, buf.length)) {
428 kfree(databuf);
429 return -EFAULT;
432 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
434 if (copy_to_user(argp + sizeof(uint32_t), &retlen, sizeof(uint32_t)))
435 ret = -EFAULT;
437 kfree(databuf);
438 break;
442 case MEMREADOOB:
444 struct mtd_oob_buf buf;
445 void *databuf;
446 ssize_t retlen;
448 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
449 return -EFAULT;
451 if (buf.length > 0x4096)
452 return -EINVAL;
454 if (!mtd->read_oob)
455 ret = -EOPNOTSUPP;
456 else
457 ret = access_ok(VERIFY_WRITE, buf.ptr,
458 buf.length) ? 0 : -EFAULT;
460 if (ret)
461 return ret;
463 databuf = kmalloc(buf.length, GFP_KERNEL);
464 if (!databuf)
465 return -ENOMEM;
467 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
469 if (put_user(retlen, (uint32_t __user *)argp))
470 ret = -EFAULT;
471 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
472 ret = -EFAULT;
474 kfree(databuf);
475 break;
478 case MEMLOCK:
480 struct erase_info_user info;
482 if (copy_from_user(&info, argp, sizeof(info)))
483 return -EFAULT;
485 if (!mtd->lock)
486 ret = -EOPNOTSUPP;
487 else
488 ret = mtd->lock(mtd, info.start, info.length);
489 break;
492 case MEMUNLOCK:
494 struct erase_info_user info;
496 if (copy_from_user(&info, argp, sizeof(info)))
497 return -EFAULT;
499 if (!mtd->unlock)
500 ret = -EOPNOTSUPP;
501 else
502 ret = mtd->unlock(mtd, info.start, info.length);
503 break;
506 case MEMSETOOBSEL:
508 if (copy_from_user(&mtd->oobinfo, argp, sizeof(struct nand_oobinfo)))
509 return -EFAULT;
510 break;
513 case MEMGETOOBSEL:
515 if (copy_to_user(argp, &(mtd->oobinfo), sizeof(struct nand_oobinfo)))
516 return -EFAULT;
517 break;
520 case MEMGETBADBLOCK:
522 loff_t offs;
524 if (copy_from_user(&offs, argp, sizeof(loff_t)))
525 return -EFAULT;
526 if (!mtd->block_isbad)
527 ret = -EOPNOTSUPP;
528 else
529 return mtd->block_isbad(mtd, offs);
530 break;
533 case MEMSETBADBLOCK:
535 loff_t offs;
537 if (copy_from_user(&offs, argp, sizeof(loff_t)))
538 return -EFAULT;
539 if (!mtd->block_markbad)
540 ret = -EOPNOTSUPP;
541 else
542 return mtd->block_markbad(mtd, offs);
543 break;
546 #ifdef CONFIG_MTD_OTP
547 case OTPSELECT:
549 int mode;
550 if (copy_from_user(&mode, argp, sizeof(int)))
551 return -EFAULT;
552 SET_MTD_MODE(file, 0);
553 switch (mode) {
554 case MTD_OTP_FACTORY:
555 if (!mtd->read_fact_prot_reg)
556 ret = -EOPNOTSUPP;
557 else
558 SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
559 break;
560 case MTD_OTP_USER:
561 if (!mtd->read_fact_prot_reg)
562 ret = -EOPNOTSUPP;
563 else
564 SET_MTD_MODE(file, MTD_MODE_OTP_USER);
565 break;
566 default:
567 ret = -EINVAL;
568 case MTD_OTP_OFF:
569 break;
571 file->f_pos = 0;
572 break;
575 case OTPGETREGIONCOUNT:
576 case OTPGETREGIONINFO:
578 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
579 if (!buf)
580 return -ENOMEM;
581 ret = -EOPNOTSUPP;
582 switch (MTD_MODE(file)) {
583 case MTD_MODE_OTP_FACT:
584 if (mtd->get_fact_prot_info)
585 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
586 break;
587 case MTD_MODE_OTP_USER:
588 if (mtd->get_user_prot_info)
589 ret = mtd->get_user_prot_info(mtd, buf, 4096);
590 break;
592 if (ret >= 0) {
593 if (cmd == OTPGETREGIONCOUNT) {
594 int nbr = ret / sizeof(struct otp_info);
595 ret = copy_to_user(argp, &nbr, sizeof(int));
596 } else
597 ret = copy_to_user(argp, buf, ret);
598 if (ret)
599 ret = -EFAULT;
601 kfree(buf);
602 break;
605 case OTPLOCK:
607 struct otp_info info;
609 if (MTD_MODE(file) != MTD_MODE_OTP_USER)
610 return -EINVAL;
611 if (copy_from_user(&info, argp, sizeof(info)))
612 return -EFAULT;
613 if (!mtd->lock_user_prot_reg)
614 return -EOPNOTSUPP;
615 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
616 break;
618 #endif
620 default:
621 ret = -ENOTTY;
624 return ret;
625 } /* memory_ioctl */
627 static struct file_operations mtd_fops = {
628 .owner = THIS_MODULE,
629 .llseek = mtd_lseek,
630 .read = mtd_read,
631 .write = mtd_write,
632 .ioctl = mtd_ioctl,
633 .open = mtd_open,
634 .release = mtd_close,
637 static int __init init_mtdchar(void)
639 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
640 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
641 MTD_CHAR_MAJOR);
642 return -EAGAIN;
645 mtd_class = class_create(THIS_MODULE, "mtd");
647 if (IS_ERR(mtd_class)) {
648 printk(KERN_ERR "Error creating mtd class.\n");
649 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
650 return PTR_ERR(mtd_class);
653 register_mtd_user(&notifier);
654 return 0;
657 static void __exit cleanup_mtdchar(void)
659 unregister_mtd_user(&notifier);
660 class_destroy(mtd_class);
661 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
664 module_init(init_mtdchar);
665 module_exit(cleanup_mtdchar);
668 MODULE_LICENSE("GPL");
669 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
670 MODULE_DESCRIPTION("Direct character-device access to MTD devices");