mtd/drivers/mtd mtdchar.c,1.67,1.68

Nicolas Pitre nico at infradead.org
Tue Feb 8 14:12:53 EST 2005


Update of /home/cvs/mtd/drivers/mtd
In directory phoenix.infradead.org:/tmp/cvs-serv22841/drivers/mtd

Modified Files:
	mtdchar.c 
Log Message:
unabuse file-f_mode for OTP purpose

Signed-off-by: Nicolas Pitre <nico at cam.org>


Index: mtdchar.c
===================================================================
RCS file: /home/cvs/mtd/drivers/mtd/mtdchar.c,v
retrieving revision 1.67
retrieving revision 1.68
diff -u -r1.67 -r1.68
--- mtdchar.c	8 Feb 2005 17:45:51 -0000	1.67
+++ mtdchar.c	8 Feb 2005 19:12:50 -0000	1.68
@@ -59,15 +59,25 @@
 #define mtdchar_devfs_exit() do { } while(0)
 #endif
 
-
-/* Well... let's abuse the unused bits in file->f_mode for those */
-#define MTD_MODE_OTP_FACT	0x1000
-#define MTD_MODE_OTP_USER	0x2000
-#define MTD_MODE_MASK		0xf000
+/*
+ * We use file->private_data to store a pointer to the MTDdevice.
+ * Since alighment is at least 32 bits, we have 2 bits free for OTP
+ * modes as well.
+ */
+
+#define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
+
+#define MTD_MODE_OTP_FACT	1
+#define MTD_MODE_OTP_USER	2
+#define MTD_MODE(file)		((long)((file)->private_data) & 3)
+
+#define SET_MTD_MODE(file, mode) \
+	do { long __p = (long)((file)->private_data); \
+	     (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
 
 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
 {
-	struct mtd_info *mtd = file->private_data;
+	struct mtd_info *mtd = TO_MTD(file);
 
 	switch (orig) {
 	case 0:
@@ -111,10 +121,6 @@
 	if ((file->f_mode & 2) && (minor & 1))
 		return -EACCES;
 
-	/* make sure the locally abused bits are initialy clear */
-	if (file->f_mode & MTD_MODE_MASK)
-		return -EWOULDBLOCK;
-
 	mtd = get_mtd_device(NULL, devnum);
 	
 	if (!mtd)
@@ -144,7 +150,7 @@
 
 	DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
 
-	mtd = file->private_data;
+	mtd = TO_MTD(file);
 	
 	if (mtd->sync)
 		mtd->sync(mtd);
@@ -161,7 +167,7 @@
 
 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
 {
-	struct mtd_info *mtd = file->private_data;
+	struct mtd_info *mtd = TO_MTD(file);
 	size_t retlen=0;
 	size_t total_retlen=0;
 	int ret=0;
@@ -188,7 +194,7 @@
 		if (!kbuf)
 			return -ENOMEM;
 		
-		switch (file->f_mode & MTD_MODE_MASK) {
+		switch (MTD_MODE(file)) {
 		case MTD_MODE_OTP_FACT:
 			ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
 			break;
@@ -231,7 +237,7 @@
 
 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
 {
-	struct mtd_info *mtd = file->private_data;
+	struct mtd_info *mtd = TO_MTD(file);
 	char *kbuf;
 	size_t retlen;
 	size_t total_retlen=0;
@@ -266,7 +272,7 @@
 			return -EFAULT;
 		}
 		
-		switch (file->f_mode & MTD_MODE_MASK) {
+		switch (MTD_MODE(file)) {
 		case MTD_MODE_OTP_FACT:
 			ret = -EROFS;
 			break;
@@ -310,7 +316,7 @@
 static int mtd_ioctl(struct inode *inode, struct file *file,
 		     u_int cmd, u_long arg)
 {
-	struct mtd_info *mtd = file->private_data;
+	struct mtd_info *mtd = TO_MTD(file);
 	void __user *argp = (void __user *)arg;
 	int ret = 0;
 	u_long size;
@@ -556,19 +562,19 @@
 		int mode;
 		if (copy_from_user(&mode, argp, sizeof(int)))
 			return -EFAULT;
-		file->f_mode &= ~MTD_MODE_MASK;
+		SET_MTD_MODE(file, 0);
 		switch (mode) {
 		case MTD_OTP_FACTORY:
 			if (!mtd->read_fact_prot_reg)
 				ret = -EOPNOTSUPP;
 			else
-				file->f_mode |= MTD_MODE_OTP_FACT;
+				SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
 			break;
 		case MTD_OTP_USER:
 			if (!mtd->read_fact_prot_reg)
 				ret = -EOPNOTSUPP;
 			else
-				file->f_mode |= MTD_MODE_OTP_USER;
+				SET_MTD_MODE(file, MTD_MODE_OTP_USER);
 			break;
 		default:
 			ret = -EINVAL;
@@ -585,7 +591,7 @@
 		if (!buf)
 			return -ENOMEM;
 		ret = -EOPNOTSUPP;
-		switch (file->f_mode & MTD_MODE_MASK) {
+		switch (MTD_MODE(file)) {
 		case MTD_MODE_OTP_FACT:
 			if (mtd->get_fact_prot_info)
 				ret = mtd->get_fact_prot_info(mtd, buf, 4096);
@@ -612,7 +618,7 @@
 	{
 		struct otp_info info;
 
-		if ((file->f_mode & MTD_MODE_MASK) != MTD_MODE_OTP_USER)
+		if (MTD_MODE(file) != MTD_MODE_OTP_USER)
 			return -EINVAL;
 		if (copy_from_user(&info, argp, sizeof(info)))
 			return -EFAULT;





More information about the linux-mtd-cvs mailing list