]>
git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/mtd/mtdchar.c
2 * $Id: mtdchar.c,v 1.68 2005/02/08 19:12:50 nico 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/mtd/compatmac.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
16 #include <asm/uaccess.h>
18 #ifdef CONFIG_DEVFS_FS
19 #include <linux/devfs_fs_kernel.h>
21 static void mtd_notify_add(struct mtd_info
* mtd
)
26 devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2),
27 S_IFCHR
| S_IRUGO
| S_IWUGO
, "mtd/%d", mtd
->index
);
29 devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2+1),
30 S_IFCHR
| S_IRUGO
, "mtd/%dro", mtd
->index
);
33 static void mtd_notify_remove(struct mtd_info
* mtd
)
37 devfs_remove("mtd/%d", mtd
->index
);
38 devfs_remove("mtd/%dro", mtd
->index
);
41 static struct mtd_notifier notifier
= {
42 .add
= mtd_notify_add
,
43 .remove
= mtd_notify_remove
,
46 static inline void mtdchar_devfs_init(void)
49 register_mtd_user(¬ifier
);
52 static inline void mtdchar_devfs_exit(void)
54 unregister_mtd_user(¬ifier
);
58 #define mtdchar_devfs_init() do { } while(0)
59 #define mtdchar_devfs_exit() do { } while(0)
63 * We use file->private_data to store a pointer to the MTDdevice.
64 * Since alighment is at least 32 bits, we have 2 bits free for OTP
68 #define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
70 #define MTD_MODE_OTP_FACT 1
71 #define MTD_MODE_OTP_USER 2
72 #define MTD_MODE(file) ((long)((file)->private_data) & 3)
74 #define SET_MTD_MODE(file, mode) \
75 do { long __p = (long)((file)->private_data); \
76 (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
78 static loff_t
mtd_lseek (struct file
*file
, loff_t offset
, int orig
)
80 struct mtd_info
*mtd
= TO_MTD(file
);
89 file
->f_pos
+= offset
;
93 file
->f_pos
=mtd
->size
+ offset
;
101 else if (file
->f_pos
>= mtd
->size
)
102 file
->f_pos
= mtd
->size
- 1;
109 static int mtd_open(struct inode
*inode
, struct file
*file
)
111 int minor
= iminor(inode
);
112 int devnum
= minor
>> 1;
113 struct mtd_info
*mtd
;
115 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_open\n");
117 if (devnum
>= MAX_MTD_DEVICES
)
120 /* You can't open the RO devices RW */
121 if ((file
->f_mode
& 2) && (minor
& 1))
124 mtd
= get_mtd_device(NULL
, devnum
);
129 if (MTD_ABSENT
== mtd
->type
) {
134 file
->private_data
= mtd
;
136 /* You can't open it RW if it's not a writeable device */
137 if ((file
->f_mode
& 2) && !(mtd
->flags
& MTD_WRITEABLE
)) {
145 /*====================================================================*/
147 static int mtd_close(struct inode
*inode
, struct file
*file
)
149 struct mtd_info
*mtd
;
151 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_close\n");
163 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
164 userspace buffer down and use it directly with readv/writev.
166 #define MAX_KMALLOC_SIZE 0x20000
168 static ssize_t
mtd_read(struct file
*file
, char __user
*buf
, size_t count
,loff_t
*ppos
)
170 struct mtd_info
*mtd
= TO_MTD(file
);
172 size_t total_retlen
=0;
177 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_read\n");
179 if (*ppos
+ count
> mtd
->size
)
180 count
= mtd
->size
- *ppos
;
185 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
186 and pass them directly to the MTD functions */
188 if (count
> MAX_KMALLOC_SIZE
)
189 len
= MAX_KMALLOC_SIZE
;
193 kbuf
=kmalloc(len
,GFP_KERNEL
);
197 switch (MTD_MODE(file
)) {
198 case MTD_MODE_OTP_FACT
:
199 ret
= mtd
->read_fact_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
201 case MTD_MODE_OTP_USER
:
202 ret
= mtd
->read_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
205 ret
= MTD_READ(mtd
, *ppos
, len
, &retlen
, kbuf
);
207 /* Nand returns -EBADMSG on ecc errors, but it returns
208 * the data. For our userspace tools it is important
209 * to dump areas with ecc errors !
210 * Userspace software which accesses NAND this way
211 * must be aware of the fact that it deals with NAND
213 if (!ret
|| (ret
== -EBADMSG
)) {
215 if (copy_to_user(buf
, kbuf
, retlen
)) {
220 total_retlen
+= retlen
;
238 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
240 struct mtd_info
*mtd
= TO_MTD(file
);
243 size_t total_retlen
=0;
247 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
249 if (*ppos
== mtd
->size
)
252 if (*ppos
+ count
> mtd
->size
)
253 count
= mtd
->size
- *ppos
;
259 if (count
> MAX_KMALLOC_SIZE
)
260 len
= MAX_KMALLOC_SIZE
;
264 kbuf
=kmalloc(len
,GFP_KERNEL
);
266 printk("kmalloc is null\n");
270 if (copy_from_user(kbuf
, buf
, len
)) {
275 switch (MTD_MODE(file
)) {
276 case MTD_MODE_OTP_FACT
:
279 case MTD_MODE_OTP_USER
:
280 if (!mtd
->write_user_prot_reg
) {
284 ret
= mtd
->write_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
287 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
291 total_retlen
+= retlen
;
306 /*======================================================================
308 IOCTL calls for getting device parameters.
310 ======================================================================*/
311 static void mtdchar_erase_callback (struct erase_info
*instr
)
313 wake_up((wait_queue_head_t
*)instr
->priv
);
316 static int mtd_ioctl(struct inode
*inode
, struct file
*file
,
317 u_int cmd
, u_long arg
)
319 struct mtd_info
*mtd
= TO_MTD(file
);
320 void __user
*argp
= (void __user
*)arg
;
324 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
326 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
328 if (!access_ok(VERIFY_READ
, argp
, size
))
332 if (!access_ok(VERIFY_WRITE
, argp
, size
))
337 case MEMGETREGIONCOUNT
:
338 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
342 case MEMGETREGIONINFO
:
344 struct region_info_user ur
;
346 if (copy_from_user(&ur
, argp
, sizeof(struct region_info_user
)))
349 if (ur
.regionindex
>= mtd
->numeraseregions
)
351 if (copy_to_user(argp
, &(mtd
->eraseregions
[ur
.regionindex
]),
352 sizeof(struct mtd_erase_region_info
)))
358 if (copy_to_user(argp
, mtd
, sizeof(struct mtd_info_user
)))
364 struct erase_info
*erase
;
366 if(!(file
->f_mode
& 2))
369 erase
=kmalloc(sizeof(struct erase_info
),GFP_KERNEL
);
373 wait_queue_head_t waitq
;
374 DECLARE_WAITQUEUE(wait
, current
);
376 init_waitqueue_head(&waitq
);
378 memset (erase
,0,sizeof(struct erase_info
));
379 if (copy_from_user(&erase
->addr
, argp
,
380 sizeof(struct erase_info_user
))) {
385 erase
->callback
= mtdchar_erase_callback
;
386 erase
->priv
= (unsigned long)&waitq
;
389 FIXME: Allow INTERRUPTIBLE. Which means
390 not having the wait_queue head on the stack.
392 If the wq_head is on the stack, and we
393 leave because we got interrupted, then the
394 wq_head is no longer there when the
395 callback routine tries to wake us up.
397 ret
= mtd
->erase(mtd
, erase
);
399 set_current_state(TASK_UNINTERRUPTIBLE
);
400 add_wait_queue(&waitq
, &wait
);
401 if (erase
->state
!= MTD_ERASE_DONE
&&
402 erase
->state
!= MTD_ERASE_FAILED
)
404 remove_wait_queue(&waitq
, &wait
);
405 set_current_state(TASK_RUNNING
);
407 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
416 struct mtd_oob_buf buf
;
420 if(!(file
->f_mode
& 2))
423 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
426 if (buf
.length
> 0x4096)
432 ret
= access_ok(VERIFY_READ
, buf
.ptr
,
433 buf
.length
) ? 0 : EFAULT
;
438 databuf
= kmalloc(buf
.length
, GFP_KERNEL
);
442 if (copy_from_user(databuf
, buf
.ptr
, buf
.length
)) {
447 ret
= (mtd
->write_oob
)(mtd
, buf
.start
, buf
.length
, &retlen
, databuf
);
449 if (copy_to_user(argp
+ sizeof(uint32_t), &retlen
, sizeof(uint32_t)))
459 struct mtd_oob_buf buf
;
463 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
466 if (buf
.length
> 0x4096)
472 ret
= access_ok(VERIFY_WRITE
, buf
.ptr
,
473 buf
.length
) ? 0 : -EFAULT
;
478 databuf
= kmalloc(buf
.length
, GFP_KERNEL
);
482 ret
= (mtd
->read_oob
)(mtd
, buf
.start
, buf
.length
, &retlen
, databuf
);
484 if (put_user(retlen
, (uint32_t __user
*)argp
))
486 else if (retlen
&& copy_to_user(buf
.ptr
, databuf
, retlen
))
495 struct erase_info_user info
;
497 if (copy_from_user(&info
, argp
, sizeof(info
)))
503 ret
= mtd
->lock(mtd
, info
.start
, info
.length
);
509 struct erase_info_user info
;
511 if (copy_from_user(&info
, argp
, sizeof(info
)))
517 ret
= mtd
->unlock(mtd
, info
.start
, info
.length
);
523 if (copy_from_user(&mtd
->oobinfo
, argp
, sizeof(struct nand_oobinfo
)))
530 if (copy_to_user(argp
, &(mtd
->oobinfo
), sizeof(struct nand_oobinfo
)))
539 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
541 if (!mtd
->block_isbad
)
544 return mtd
->block_isbad(mtd
, offs
);
552 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
554 if (!mtd
->block_markbad
)
557 return mtd
->block_markbad(mtd
, offs
);
561 #ifdef CONFIG_MTD_OTP
565 if (copy_from_user(&mode
, argp
, sizeof(int)))
567 SET_MTD_MODE(file
, 0);
569 case MTD_OTP_FACTORY
:
570 if (!mtd
->read_fact_prot_reg
)
573 SET_MTD_MODE(file
, MTD_MODE_OTP_FACT
);
576 if (!mtd
->read_fact_prot_reg
)
579 SET_MTD_MODE(file
, MTD_MODE_OTP_USER
);
589 case OTPGETREGIONCOUNT
:
590 case OTPGETREGIONINFO
:
592 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
596 switch (MTD_MODE(file
)) {
597 case MTD_MODE_OTP_FACT
:
598 if (mtd
->get_fact_prot_info
)
599 ret
= mtd
->get_fact_prot_info(mtd
, buf
, 4096);
601 case MTD_MODE_OTP_USER
:
602 if (mtd
->get_user_prot_info
)
603 ret
= mtd
->get_user_prot_info(mtd
, buf
, 4096);
607 if (cmd
== OTPGETREGIONCOUNT
) {
608 int nbr
= ret
/ sizeof(struct otp_info
);
609 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
611 ret
= copy_to_user(argp
, buf
, ret
);
621 struct otp_info info
;
623 if (MTD_MODE(file
) != MTD_MODE_OTP_USER
)
625 if (copy_from_user(&info
, argp
, sizeof(info
)))
627 if (!mtd
->lock_user_prot_reg
)
629 ret
= mtd
->lock_user_prot_reg(mtd
, info
.start
, info
.length
);
641 static struct file_operations mtd_fops
= {
642 .owner
= THIS_MODULE
,
648 .release
= mtd_close
,
651 static int __init
init_mtdchar(void)
653 if (register_chrdev(MTD_CHAR_MAJOR
, "mtd", &mtd_fops
)) {
654 printk(KERN_NOTICE
"Can't allocate major number %d for Memory Technology Devices.\n",
659 mtdchar_devfs_init();
663 static void __exit
cleanup_mtdchar(void)
665 mtdchar_devfs_exit();
666 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
669 module_init(init_mtdchar
);
670 module_exit(cleanup_mtdchar
);
673 MODULE_LICENSE("GPL");
674 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
675 MODULE_DESCRIPTION("Direct character-device access to MTD devices");