mediatek: fix PWM fan on BPi-R4
[openwrt/openwrt.git] / target / linux / generic / pending-6.6 / 490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
1 From: Daniel Golle <daniel@makrotopia.org>
2 Subject: ubi: auto-attach mtd device named "ubi" or "data" on boot
3
4 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
5 ---
6 drivers/mtd/ubi/build.c | 36 ++++++++++++++++++++++++++++++++++++
7 1 file changed, 36 insertions(+)
8
9 --- a/drivers/mtd/ubi/build.c
10 +++ b/drivers/mtd/ubi/build.c
11 @@ -1264,6 +1264,74 @@ static struct mtd_notifier ubi_mtd_notif
12 .remove = ubi_notify_remove,
13 };
14
15 +
16 +/*
17 + * This function tries attaching mtd partitions named either "ubi" or "data"
18 + * during boot.
19 + */
20 +static void __init ubi_auto_attach(void)
21 +{
22 + int err;
23 + struct mtd_info *mtd;
24 + loff_t offset = 0;
25 + size_t len;
26 + char magic[4];
27 +
28 + /* try attaching mtd device named "ubi" or "data" */
29 + mtd = open_mtd_device("ubi");
30 + if (IS_ERR(mtd))
31 + mtd = open_mtd_device("data");
32 +
33 + if (IS_ERR(mtd))
34 + return;
35 +
36 + /* get the first not bad block */
37 + if (mtd_can_have_bb(mtd))
38 + while (mtd_block_isbad(mtd, offset)) {
39 + offset += mtd->erasesize;
40 +
41 + if (offset > mtd->size) {
42 + pr_err("UBI error: Failed to find a non-bad "
43 + "block on mtd%d\n", mtd->index);
44 + goto cleanup;
45 + }
46 + }
47 +
48 + /* check if the read from flash was successful */
49 + err = mtd_read(mtd, offset, 4, &len, (void *) magic);
50 + if ((err && !mtd_is_bitflip(err)) || len != 4) {
51 + pr_err("UBI error: unable to read from mtd%d\n", mtd->index);
52 + goto cleanup;
53 + }
54 +
55 + /* check for a valid ubi magic */
56 + if (strncmp(magic, "UBI#", 4)) {
57 + pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
58 + goto cleanup;
59 + }
60 +
61 + /* don't auto-add media types where UBI doesn't makes sense */
62 + if (mtd->type != MTD_NANDFLASH &&
63 + mtd->type != MTD_NORFLASH &&
64 + mtd->type != MTD_DATAFLASH &&
65 + mtd->type != MTD_MLCNANDFLASH)
66 + goto cleanup;
67 +
68 + mutex_lock(&ubi_devices_mutex);
69 + pr_notice("UBI: auto-attach mtd%d\n", mtd->index);
70 + err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0, false);
71 + mutex_unlock(&ubi_devices_mutex);
72 + if (err < 0) {
73 + pr_err("UBI error: cannot attach mtd%d\n", mtd->index);
74 + goto cleanup;
75 + }
76 +
77 + return;
78 +
79 +cleanup:
80 + put_mtd_device(mtd);
81 +}
82 +
83 static int __init ubi_init_attach(void)
84 {
85 int err, i, k;
86 @@ -1314,6 +1382,12 @@ static int __init ubi_init_attach(void)
87 }
88 }
89
90 + /* auto-attach mtd devices only if built-in to the kernel and no ubi.mtd
91 + * parameter was given */
92 + if (IS_ENABLED(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
93 + !ubi_is_module() && !mtd_devs)
94 + ubi_auto_attach();
95 +
96 return 0;
97
98 out_detach: