Rebased from upstream / out of band repository.
[librecmc/librecmc.git] / target / linux / generic / backport-4.9 / 066-v4.17-0002-mtd-get-rid-of-the-mtd_add_device_partitions.patch
1 From 0dbe4ea78d69756efeb0bba0764f6bd4a9ee9567 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Tue, 16 Jan 2018 16:45:42 +0100
4 Subject: [PATCH] mtd: get rid of the mtd_add_device_partitions()
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This simplifies code a bit by:
10 1) Avoiding an extra (tiny) function
11 2) Checking for amount of parsed (found) partitions just once
12 3) Avoiding clearing/filling struct mtd_partitions manually
13
14 With this commit proper functions are called directly from the
15 mtd_device_parse_register(). It doesn't need to use minor tricks like
16 memsetting struct to 0 to trigger an expected
17 mtd_add_device_partitions() behavior.
18
19 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
20 Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
21 ---
22  drivers/mtd/mtdcore.c | 43 ++++++++++++-------------------------------
23  1 file changed, 12 insertions(+), 31 deletions(-)
24
25 --- a/drivers/mtd/mtdcore.c
26 +++ b/drivers/mtd/mtdcore.c
27 @@ -626,21 +626,6 @@ out_error:
28         return ret;
29  }
30  
31 -static int mtd_add_device_partitions(struct mtd_info *mtd,
32 -                                    struct mtd_partitions *parts)
33 -{
34 -       const struct mtd_partition *real_parts = parts->parts;
35 -       int nbparts = parts->nr_parts;
36 -
37 -       if (!nbparts && !device_is_registered(&mtd->dev))
38 -               return add_mtd_device(mtd);
39 -
40 -       if (nbparts > 0)
41 -               return add_mtd_partitions(mtd, real_parts, nbparts);
42 -
43 -       return 0;
44 -}
45 -
46  /*
47   * Set a few defaults based on the parent devices, if not provided by the
48   * driver
49 @@ -691,7 +676,7 @@ int mtd_device_parse_register(struct mtd
50                               const struct mtd_partition *parts,
51                               int nr_parts)
52  {
53 -       struct mtd_partitions parsed;
54 +       struct mtd_partitions parsed = { };
55         int ret;
56  
57         mtd_set_dev_defaults(mtd);
58 @@ -702,24 +687,20 @@ int mtd_device_parse_register(struct mtd
59                         return ret;
60         }
61  
62 -       memset(&parsed, 0, sizeof(parsed));
63 -
64 +       /* Prefer parsed partitions over driver-provided fallback */
65         ret = parse_mtd_partitions(mtd, types, &parsed, parser_data);
66 -       if ((ret < 0 || parsed.nr_parts == 0) && parts && nr_parts) {
67 -               /* Fall back to driver-provided partitions */
68 -               parsed = (struct mtd_partitions){
69 -                       .parts          = parts,
70 -                       .nr_parts       = nr_parts,
71 -               };
72 -       } else if (ret < 0) {
73 -               /* Didn't come up with parsed OR fallback partitions */
74 -               pr_info("mtd: failed to find partitions; one or more parsers reports errors (%d)\n",
75 -                       ret);
76 -               /* Don't abort on errors; we can still use unpartitioned MTD */
77 -               memset(&parsed, 0, sizeof(parsed));
78 +       if (!ret && parsed.nr_parts) {
79 +               parts = parsed.parts;
80 +               nr_parts = parsed.nr_parts;
81         }
82  
83 -       ret = mtd_add_device_partitions(mtd, &parsed);
84 +       if (nr_parts)
85 +               ret = add_mtd_partitions(mtd, parts, nr_parts);
86 +       else if (!device_is_registered(&mtd->dev))
87 +               ret = add_mtd_device(mtd);
88 +       else
89 +               ret = 0;
90 +
91         if (ret)
92                 goto out;
93