Merge branch 'master' of /home/tglx/work/mtd/git/linux-2.6.git/
[linux-block.git] / drivers / mtd / mtdchar.c
CommitLineData
1da177e4 1/*
8b491d75 2 * $Id: mtdchar.c,v 1.74 2005/08/04 01:05:48 tpoynor Exp $
1da177e4
LT
3 *
4 * Character-device access to raw MTD devices.
5 *
6 */
7
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>
4e57b681 16#include <linux/sched.h> /* TASK_* */
1da177e4
LT
17#include <asm/uaccess.h>
18
9bc7b387
TP
19#include <linux/device.h>
20
21static struct class *mtd_class;
1da177e4
LT
22
23static void mtd_notify_add(struct mtd_info* mtd)
24{
25 if (!mtd)
26 return;
27
53f46542 28 class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
9bc7b387
TP
29 NULL, "mtd%d", mtd->index);
30
53f46542 31 class_device_create(mtd_class, NULL,
9bc7b387
TP
32 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
33 NULL, "mtd%dro", mtd->index);
1da177e4
LT
34}
35
36static void mtd_notify_remove(struct mtd_info* mtd)
37{
38 if (!mtd)
39 return;
9bc7b387
TP
40
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));
1da177e4
LT
43}
44
45static struct mtd_notifier notifier = {
46 .add = mtd_notify_add,
47 .remove = mtd_notify_remove,
48};
49
045e9a5d
NP
50/*
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.
54 */
55
56#define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
31f4233b 57
045e9a5d
NP
58#define MTD_MODE_OTP_FACT 1
59#define MTD_MODE_OTP_USER 2
60#define MTD_MODE(file) ((long)((file)->private_data) & 3)
61
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)
31f4233b 65
1da177e4
LT
66static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
67{
045e9a5d 68 struct mtd_info *mtd = TO_MTD(file);
1da177e4
LT
69
70 switch (orig) {
71 case 0:
72 /* SEEK_SET */
1da177e4
LT
73 break;
74 case 1:
75 /* SEEK_CUR */
8b491d75 76 offset += file->f_pos;
1da177e4
LT
77 break;
78 case 2:
79 /* SEEK_END */
8b491d75 80 offset += mtd->size;
1da177e4
LT
81 break;
82 default:
83 return -EINVAL;
84 }
85
8b491d75
TP
86 if (offset >= 0 && offset < mtd->size)
87 return file->f_pos = offset;
1da177e4 88
8b491d75 89 return -EINVAL;
1da177e4
LT
90}
91
92
93
94static int mtd_open(struct inode *inode, struct file *file)
95{
96 int minor = iminor(inode);
97 int devnum = minor >> 1;
98 struct mtd_info *mtd;
99
100 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
101
102 if (devnum >= MAX_MTD_DEVICES)
103 return -ENODEV;
104
105 /* You can't open the RO devices RW */
106 if ((file->f_mode & 2) && (minor & 1))
107 return -EACCES;
108
109 mtd = get_mtd_device(NULL, devnum);
110
111 if (!mtd)
112 return -ENODEV;
113
114 if (MTD_ABSENT == mtd->type) {
115 put_mtd_device(mtd);
116 return -ENODEV;
117 }
118
119 file->private_data = mtd;
120
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;
125 }
126
127 return 0;
128} /* mtd_open */
129
130/*====================================================================*/
131
132static int mtd_close(struct inode *inode, struct file *file)
133{
134 struct mtd_info *mtd;
135
136 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
137
045e9a5d 138 mtd = TO_MTD(file);
1da177e4
LT
139
140 if (mtd->sync)
141 mtd->sync(mtd);
142
143 put_mtd_device(mtd);
144
145 return 0;
146} /* mtd_close */
147
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.
150*/
151#define MAX_KMALLOC_SIZE 0x20000
152
153static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
154{
045e9a5d 155 struct mtd_info *mtd = TO_MTD(file);
1da177e4
LT
156 size_t retlen=0;
157 size_t total_retlen=0;
158 int ret=0;
159 int len;
160 char *kbuf;
161
162 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
163
164 if (*ppos + count > mtd->size)
165 count = mtd->size - *ppos;
166
167 if (!count)
168 return 0;
169
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;
177
178 kbuf=kmalloc(len,GFP_KERNEL);
179 if (!kbuf)
180 return -ENOMEM;
181
045e9a5d 182 switch (MTD_MODE(file)) {
31f4233b
NP
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);
191 }
1da177e4
LT
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
197 */
198 if (!ret || (ret == -EBADMSG)) {
199 *ppos += retlen;
200 if (copy_to_user(buf, kbuf, retlen)) {
201 kfree(kbuf);
202 return -EFAULT;
203 }
204 else
205 total_retlen += retlen;
206
207 count -= retlen;
208 buf += retlen;
31f4233b
NP
209 if (retlen == 0)
210 count = 0;
1da177e4
LT
211 }
212 else {
213 kfree(kbuf);
214 return ret;
215 }
216
217 kfree(kbuf);
218 }
219
220 return total_retlen;
221} /* mtd_read */
222
223static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
224{
045e9a5d 225 struct mtd_info *mtd = TO_MTD(file);
1da177e4
LT
226 char *kbuf;
227 size_t retlen;
228 size_t total_retlen=0;
229 int ret=0;
230 int len;
231
232 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
233
234 if (*ppos == mtd->size)
235 return -ENOSPC;
236
237 if (*ppos + count > mtd->size)
238 count = mtd->size - *ppos;
239
240 if (!count)
241 return 0;
242
243 while (count) {
244 if (count > MAX_KMALLOC_SIZE)
245 len = MAX_KMALLOC_SIZE;
246 else
247 len = count;
248
249 kbuf=kmalloc(len,GFP_KERNEL);
250 if (!kbuf) {
251 printk("kmalloc is null\n");
252 return -ENOMEM;
253 }
254
255 if (copy_from_user(kbuf, buf, len)) {
256 kfree(kbuf);
257 return -EFAULT;
258 }
259
045e9a5d 260 switch (MTD_MODE(file)) {
31f4233b
NP
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;
268 }
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);
273 }
1da177e4
LT
274 if (!ret) {
275 *ppos += retlen;
276 total_retlen += retlen;
277 count -= retlen;
278 buf += retlen;
279 }
280 else {
281 kfree(kbuf);
282 return ret;
283 }
284
285 kfree(kbuf);
286 }
287
288 return total_retlen;
289} /* mtd_write */
290
291/*======================================================================
292
293 IOCTL calls for getting device parameters.
294
295======================================================================*/
296static void mtdchar_erase_callback (struct erase_info *instr)
297{
298 wake_up((wait_queue_head_t *)instr->priv);
299}
300
301static int mtd_ioctl(struct inode *inode, struct file *file,
302 u_int cmd, u_long arg)
303{
045e9a5d 304 struct mtd_info *mtd = TO_MTD(file);
1da177e4
LT
305 void __user *argp = (void __user *)arg;
306 int ret = 0;
307 u_long size;
308
309 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
310
311 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
312 if (cmd & IOC_IN) {
313 if (!access_ok(VERIFY_READ, argp, size))
314 return -EFAULT;
315 }
316 if (cmd & IOC_OUT) {
317 if (!access_ok(VERIFY_WRITE, argp, size))
318 return -EFAULT;
319 }
320
321 switch (cmd) {
322 case MEMGETREGIONCOUNT:
323 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
324 return -EFAULT;
325 break;
326
327 case MEMGETREGIONINFO:
328 {
329 struct region_info_user ur;
330
331 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
332 return -EFAULT;
333
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;
340 }
341
342 case MEMGETINFO:
343 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
344 return -EFAULT;
345 break;
346
347 case MEMERASE:
348 {
349 struct erase_info *erase;
350
351 if(!(file->f_mode & 2))
352 return -EPERM;
353
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);
360
361 init_waitqueue_head(&waitq);
362
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;
368 }
369 erase->mtd = mtd;
370 erase->callback = mtdchar_erase_callback;
371 erase->priv = (unsigned long)&waitq;
372
373 /*
374 FIXME: Allow INTERRUPTIBLE. Which means
375 not having the wait_queue head on the stack.
376
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.
381 */
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);
391
392 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
393 }
394 kfree(erase);
395 }
396 break;
397 }
398
399 case MEMWRITEOOB:
400 {
401 struct mtd_oob_buf buf;
402 void *databuf;
403 ssize_t retlen;
404
405 if(!(file->f_mode & 2))
406 return -EPERM;
407
408 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
409 return -EFAULT;
410
411 if (buf.length > 0x4096)
412 return -EINVAL;
413
414 if (!mtd->write_oob)
415 ret = -EOPNOTSUPP;
416 else
417 ret = access_ok(VERIFY_READ, buf.ptr,
418 buf.length) ? 0 : EFAULT;
419
420 if (ret)
421 return ret;
422
423 databuf = kmalloc(buf.length, GFP_KERNEL);
424 if (!databuf)
425 return -ENOMEM;
426
427 if (copy_from_user(databuf, buf.ptr, buf.length)) {
428 kfree(databuf);
429 return -EFAULT;
430 }
431
432 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
433
434 if (copy_to_user(argp + sizeof(uint32_t), &retlen, sizeof(uint32_t)))
435 ret = -EFAULT;
436
437 kfree(databuf);
438 break;
439
440 }
441
442 case MEMREADOOB:
443 {
444 struct mtd_oob_buf buf;
445 void *databuf;
446 ssize_t retlen;
447
448 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
449 return -EFAULT;
450
451 if (buf.length > 0x4096)
452 return -EINVAL;
453
454 if (!mtd->read_oob)
455 ret = -EOPNOTSUPP;
456 else
457 ret = access_ok(VERIFY_WRITE, buf.ptr,
458 buf.length) ? 0 : -EFAULT;
459
460 if (ret)
461 return ret;
462
463 databuf = kmalloc(buf.length, GFP_KERNEL);
464 if (!databuf)
465 return -ENOMEM;
466
467 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
468
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;
473
474 kfree(databuf);
475 break;
476 }
477
478 case MEMLOCK:
479 {
480 struct erase_info_user info;
481
482 if (copy_from_user(&info, argp, sizeof(info)))
483 return -EFAULT;
484
485 if (!mtd->lock)
486 ret = -EOPNOTSUPP;
487 else
488 ret = mtd->lock(mtd, info.start, info.length);
489 break;
490 }
491
492 case MEMUNLOCK:
493 {
494 struct erase_info_user info;
495
496 if (copy_from_user(&info, argp, sizeof(info)))
497 return -EFAULT;
498
499 if (!mtd->unlock)
500 ret = -EOPNOTSUPP;
501 else
502 ret = mtd->unlock(mtd, info.start, info.length);
503 break;
504 }
505
506 case MEMSETOOBSEL:
507 {
508 if (copy_from_user(&mtd->oobinfo, argp, sizeof(struct nand_oobinfo)))
509 return -EFAULT;
510 break;
511 }
512
513 case MEMGETOOBSEL:
514 {
515 if (copy_to_user(argp, &(mtd->oobinfo), sizeof(struct nand_oobinfo)))
516 return -EFAULT;
517 break;
518 }
519
520 case MEMGETBADBLOCK:
521 {
522 loff_t offs;
523
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;
531 }
532
533 case MEMSETBADBLOCK:
534 {
535 loff_t offs;
536
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;
544 }
545
31f4233b
NP
546#ifdef CONFIG_MTD_OTP
547 case OTPSELECT:
548 {
549 int mode;
550 if (copy_from_user(&mode, argp, sizeof(int)))
551 return -EFAULT;
045e9a5d 552 SET_MTD_MODE(file, 0);
31f4233b
NP
553 switch (mode) {
554 case MTD_OTP_FACTORY:
555 if (!mtd->read_fact_prot_reg)
556 ret = -EOPNOTSUPP;
557 else
045e9a5d 558 SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
31f4233b
NP
559 break;
560 case MTD_OTP_USER:
561 if (!mtd->read_fact_prot_reg)
562 ret = -EOPNOTSUPP;
563 else
045e9a5d 564 SET_MTD_MODE(file, MTD_MODE_OTP_USER);
31f4233b
NP
565 break;
566 default:
567 ret = -EINVAL;
568 case MTD_OTP_OFF:
569 break;
570 }
81dba488 571 file->f_pos = 0;
31f4233b
NP
572 break;
573 }
574
575 case OTPGETREGIONCOUNT:
576 case OTPGETREGIONINFO:
577 {
578 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
579 if (!buf)
580 return -ENOMEM;
581 ret = -EOPNOTSUPP;
045e9a5d 582 switch (MTD_MODE(file)) {
31f4233b
NP
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;
591 }
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;
600 }
601 kfree(buf);
602 break;
603 }
604
605 case OTPLOCK:
606 {
607 struct otp_info info;
608
045e9a5d 609 if (MTD_MODE(file) != MTD_MODE_OTP_USER)
31f4233b
NP
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;
617 }
618#endif
619
1da177e4
LT
620 default:
621 ret = -ENOTTY;
622 }
623
624 return ret;
625} /* memory_ioctl */
626
627static 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,
635};
636
637static int __init init_mtdchar(void)
638{
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;
643 }
644
9bc7b387
TP
645 mtd_class = class_create(THIS_MODULE, "mtd");
646
647 if (IS_ERR(mtd_class)) {
648 printk(KERN_ERR "Error creating mtd class.\n");
649 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
3a7a8824 650 return PTR_ERR(mtd_class);
9bc7b387
TP
651 }
652
653 register_mtd_user(&notifier);
1da177e4
LT
654 return 0;
655}
656
657static void __exit cleanup_mtdchar(void)
658{
9bc7b387
TP
659 unregister_mtd_user(&notifier);
660 class_destroy(mtd_class);
1da177e4
LT
661 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
662}
663
664module_init(init_mtdchar);
665module_exit(cleanup_mtdchar);
666
667
668MODULE_LICENSE("GPL");
669MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
670MODULE_DESCRIPTION("Direct character-device access to MTD devices");