return ret;
 }
 
+/*
+ * Copies (and truncates, if necessary) data from the larger struct,
+ * nand_ecclayout, to the smaller, deprecated layout struct,
+ * nand_ecclayout_user. This is necessary only to suppport the deprecated
+ * API ioctl ECCGETLAYOUT while allowing all new functionality to use
+ * nand_ecclayout flexibly (i.e. the struct may change size in new
+ * releases without requiring major rewrites).
+ */
+static int shrink_ecclayout(const struct nand_ecclayout *from,
+               struct nand_ecclayout_user *to)
+{
+       int i;
+
+       if (!from || !to)
+               return -EINVAL;
+
+       memset(to, 0, sizeof(*to));
+
+       to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES_OLD);
+       for (i = 0; i < to->eccbytes; i++)
+               to->eccpos[i] = from->eccpos[i];
+
+       for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
+               if (from->oobfree[i].length == 0 &&
+                               from->oobfree[i].offset == 0)
+                       break;
+               to->oobavail += from->oobfree[i].length;
+               to->oobfree[i] = from->oobfree[i];
+       }
+
+       return 0;
+}
+
 static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
 {
        struct mtd_file_info *mfi = file->private_data;
        }
 #endif
 
+       /* This ioctl is being deprecated - it truncates the ecc layout */
        case ECCGETLAYOUT:
        {
+               struct nand_ecclayout_user *usrlay;
+
                if (!mtd->ecclayout)
                        return -EOPNOTSUPP;
 
-               if (copy_to_user(argp, mtd->ecclayout,
-                                sizeof(struct nand_ecclayout)))
-                       return -EFAULT;
+               usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
+               if (!usrlay)
+                       return -ENOMEM;
+
+               shrink_ecclayout(mtd->ecclayout, usrlay);
+
+               if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
+                       ret = -EFAULT;
+               kfree(usrlay);
                break;
        }
 
 
                 * breaks userspace ioctl interface with mtd-utils. Once we
                 * resolve this issue, NAND_ECC_HW_OOB_FIRST mode can be used
                 * for the 4KiB page chips.
+                *
+                * TODO: Note that nand_ecclayout has now been expanded and can
+                *  hold plenty of OOB entries.
                 */
                dev_warn(&pdev->dev, "no 4-bit ECC support yet "
                                "for 4KiB-page NAND\n");
 
        uint8_t         *oobbuf;
 };
 
+#define MTD_MAX_OOBFREE_ENTRIES_LARGE  32
+#define MTD_MAX_ECCPOS_ENTRIES_LARGE   448
+#define MTD_MAX_ECCPOS_ENTRIES_OLD     64      /* Previous maximum */
+/*
+ * Correct ECC layout control structure. This replaces old nand_ecclayout
+ * (mtd-abi.h) that is exported via ECCGETLAYOUT ioctl. It should be expandable
+ *  in the future simply by the above macros.
+ */
+struct nand_ecclayout {
+       __u32 eccbytes;
+       __u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE];
+       __u32 oobavail;
+       struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
+};
+
 struct mtd_info {
        u_char type;
        uint32_t flags;
 
        uint64_t size;                  /* partition size */
        uint64_t offset;                /* offset within the master MTD space */
        uint32_t mask_flags;            /* master MTD flags to mask out for this partition */
-       struct nand_ecclayout *ecclayout;       /* out of band layout for this partition (NAND only)*/
+       struct nand_ecclayout *ecclayout;       /* out of band layout for this partition (NAND only) */
 };
 
 #define MTDPART_OFS_NXTBLK     (-2)
 
 #define OTPGETREGIONCOUNT      _IOW('M', 14, int)
 #define OTPGETREGIONINFO       _IOW('M', 15, struct otp_info)
 #define OTPLOCK                        _IOR('M', 16, struct otp_info)
-#define ECCGETLAYOUT           _IOR('M', 17, struct nand_ecclayout)
+#define ECCGETLAYOUT           _IOR('M', 17, struct nand_ecclayout_user)
 #define ECCGETSTATS            _IOR('M', 18, struct mtd_ecc_stats)
 #define MTDFILEMODE            _IO('M', 19)
 #define MEMERASE64             _IOW('M', 20, struct erase_info_user64)
  * ECC layout control structure. Exported to userspace for
  * diagnosis and to allow creation of raw images
  */
-struct nand_ecclayout {
+struct nand_ecclayout_user {
        __u32 eccbytes;
        __u32 eccpos[64];
        __u32 oobavail;
 
 typedef struct erase_info_user erase_info_t;
 typedef struct region_info_user region_info_t;
 typedef struct nand_oobinfo nand_oobinfo_t;
-typedef struct nand_ecclayout nand_ecclayout_t;
+typedef struct nand_ecclayout_user nand_ecclayout_t;
 
 #endif /* __MTD_USER_H__ */