kernel: generic: add kernel 4.3
[openwrt.git] / target / linux / generic / patches-4.3 / 440-block2mtd_init.patch
1 --- a/drivers/mtd/devices/block2mtd.c
2 +++ b/drivers/mtd/devices/block2mtd.c
3 @@ -26,6 +26,7 @@
4  #include <linux/list.h>
5  #include <linux/init.h>
6  #include <linux/mtd/mtd.h>
7 +#include <linux/mtd/partitions.h>
8  #include <linux/mutex.h>
9  #include <linux/mount.h>
10  #include <linux/slab.h>
11 @@ -219,7 +220,7 @@ static void block2mtd_free_device(struct
12  
13  
14  static struct block2mtd_dev *add_device(char *devname, int erase_size,
15 -               int timeout)
16 +               const char *mtdname, int timeout)
17  {
18  #ifndef MODULE
19         int i;
20 @@ -227,6 +228,7 @@ static struct block2mtd_dev *add_device(
21         const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
22         struct block_device *bdev = ERR_PTR(-ENODEV);
23         struct block2mtd_dev *dev;
24 +       struct mtd_partition *part;
25         char *name;
26  
27         if (!devname)
28 @@ -283,13 +285,16 @@ static struct block2mtd_dev *add_device(
29  
30         /* Setup the MTD structure */
31         /* make the name contain the block device in */
32 -       name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
33 +       if (!mtdname)
34 +               mtdname = devname;
35 +       name = kmalloc(strlen(mtdname) + 1, GFP_KERNEL);
36         if (!name)
37                 goto err_destroy_mutex;
38  
39 +       strcpy(name, mtdname);
40         dev->mtd.name = name;
41  
42 -       dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
43 +       dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK & ~(erase_size - 1);
44         dev->mtd.erasesize = erase_size;
45         dev->mtd.writesize = 1;
46         dev->mtd.writebufsize = PAGE_SIZE;
47 @@ -302,7 +307,11 @@ static struct block2mtd_dev *add_device(
48         dev->mtd.priv = dev;
49         dev->mtd.owner = THIS_MODULE;
50  
51 -       if (mtd_device_register(&dev->mtd, NULL, 0)) {
52 +       part = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
53 +       part->name = name;
54 +       part->offset = 0;
55 +       part->size = dev->mtd.size;
56 +       if (mtd_device_register(&dev->mtd, part, 1)) {
57                 /* Device didn't get added, so free the entry */
58                 goto err_destroy_mutex;
59         }
60 @@ -310,8 +319,7 @@ static struct block2mtd_dev *add_device(
61         list_add(&dev->list, &blkmtd_device_list);
62         pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
63                 dev->mtd.index,
64 -               dev->mtd.name + strlen("block2mtd: "),
65 -               dev->mtd.erasesize >> 10, dev->mtd.erasesize);
66 +               mtdname, dev->mtd.erasesize >> 10, dev->mtd.erasesize);
67         return dev;
68  
69  err_destroy_mutex:
70 @@ -384,7 +392,7 @@ static int block2mtd_setup2(const char *
71         /* 80 for device, 12 for erase size, 80 for name, 8 for timeout */
72         char buf[80 + 12 + 80 + 8];
73         char *str = buf;
74 -       char *token[2];
75 +       char *token[3];
76         char *name;
77         size_t erase_size = PAGE_SIZE;
78         unsigned long timeout = MTD_DEFAULT_TIMEOUT;
79 @@ -398,7 +406,7 @@ static int block2mtd_setup2(const char *
80         strcpy(str, val);
81         kill_final_newline(str);
82  
83 -       for (i = 0; i < 2; i++)
84 +       for (i = 0; i < 3; i++)
85                 token[i] = strsep(&str, ",");
86  
87         if (str) {
88 @@ -424,8 +432,10 @@ static int block2mtd_setup2(const char *
89                         return 0;
90                 }
91         }
92 +       if (token[2] && (strlen(token[2]) + 1 > 80))
93 +               pr_err("mtd device name too long\n");
94  
95 -       add_device(name, erase_size, timeout);
96 +       add_device(name, erase_size, token[2], timeout);
97  
98         return 0;
99  }
100 @@ -459,7 +469,7 @@ static int block2mtd_setup(const char *v
101  
102  
103  module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
104 -MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
105 +MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>[,<name>]]\"");
106  
107  static int __init block2mtd_init(void)
108  {