1516c6cb4349aff2f3225c4e47248366c2f2c0f6
[openwrt/staging/ldir.git] /
1 From 1046fc9e98936991aeb0b0656c84833d96a63c0f Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Sun, 15 Oct 2023 14:22:49 +0200
4 Subject: [PATCH 1/5] wifi: rt2x00: Add support for loading EEPROM from
5 userspace
6
7 Add support for loading EEPROM from userspace.
8
9 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
10 ---
11 drivers/net/wireless/ralink/rt2x00/Kconfig | 5 ++
12 drivers/net/wireless/ralink/rt2x00/Makefile | 1 +
13 .../net/wireless/ralink/rt2x00/rt2800soc.c | 15 +---
14 drivers/net/wireless/ralink/rt2x00/rt2x00.h | 1 +
15 .../net/wireless/ralink/rt2x00/rt2x00dev.c | 9 +++
16 .../net/wireless/ralink/rt2x00/rt2x00eeprom.c | 75 +++++++++++++++++++
17 .../net/wireless/ralink/rt2x00/rt2x00soc.c | 1 +
18 .../net/wireless/ralink/rt2x00/rt2x00soc.h | 9 +++
19 8 files changed, 102 insertions(+), 14 deletions(-)
20 create mode 100644 drivers/net/wireless/ralink/rt2x00/rt2x00eeprom.c
21
22 --- a/local-symbols
23 +++ b/local-symbols
24 @@ -334,6 +334,7 @@ RT2X00_LIB_FIRMWARE=
25 RT2X00_LIB_CRYPTO=
26 RT2X00_LIB_LEDS=
27 RT2X00_LIB_DEBUGFS=
28 +RT2X00_LIB_EEPROM=
29 RT2X00_DEBUG=
30 WLAN_VENDOR_REALTEK=
31 RTL8180=
32 --- a/drivers/net/wireless/ralink/rt2x00/Kconfig
33 +++ b/drivers/net/wireless/ralink/rt2x00/Kconfig
34 @@ -70,6 +70,7 @@ config RT2800PCI
35 select RT2X00_LIB_MMIO
36 select RT2X00_LIB_PCI
37 select RT2X00_LIB_FIRMWARE
38 + select RT2X00_LIB_EEPROM
39 select RT2X00_LIB_CRYPTO
40 depends on CRC_CCITT
41 depends on EEPROM_93CX6
42 @@ -216,6 +217,7 @@ config RT2800SOC
43 select RT2X00_LIB_MMIO
44 select RT2X00_LIB_CRYPTO
45 select RT2X00_LIB_FIRMWARE
46 + select RT2X00_LIB_EEPROM
47 select RT2800_LIB
48 select RT2800_LIB_MMIO
49 help
50 @@ -266,6 +268,9 @@ config RT2X00_LIB_FIRMWARE
51 config RT2X00_LIB_CRYPTO
52 bool
53
54 +config RT2X00_LIB_EEPROM
55 + bool
56 +
57 config RT2X00_LIB_LEDS
58 bool
59 default y if (RT2X00_LIB=y && LEDS_CLASS=y) || (RT2X00_LIB=m && LEDS_CLASS!=n)
60 --- a/drivers/net/wireless/ralink/rt2x00/Makefile
61 +++ b/drivers/net/wireless/ralink/rt2x00/Makefile
62 @@ -8,6 +8,7 @@ rt2x00lib-$(CPTCFG_RT2X00_LIB_DEBUGFS) +
63 rt2x00lib-$(CPTCFG_RT2X00_LIB_CRYPTO) += rt2x00crypto.o
64 rt2x00lib-$(CPTCFG_RT2X00_LIB_FIRMWARE) += rt2x00firmware.o
65 rt2x00lib-$(CPTCFG_RT2X00_LIB_LEDS) += rt2x00leds.o
66 +rt2x00lib-$(CPTCFG_RT2X00_LIB_EEPROM) += rt2x00eeprom.o
67
68 obj-$(CPTCFG_RT2X00_LIB) += rt2x00lib.o
69 obj-$(CPTCFG_RT2X00_LIB_MMIO) += rt2x00mmio.o
70 --- a/drivers/net/wireless/ralink/rt2x00/rt2800soc.c
71 +++ b/drivers/net/wireless/ralink/rt2x00/rt2800soc.c
72 @@ -90,19 +90,6 @@ static int rt2800soc_set_device_state(st
73 return retval;
74 }
75
76 -static int rt2800soc_read_eeprom(struct rt2x00_dev *rt2x00dev)
77 -{
78 - void __iomem *base_addr = ioremap(0x1F040000, EEPROM_SIZE);
79 -
80 - if (!base_addr)
81 - return -ENOMEM;
82 -
83 - memcpy_fromio(rt2x00dev->eeprom, base_addr, EEPROM_SIZE);
84 -
85 - iounmap(base_addr);
86 - return 0;
87 -}
88 -
89 /* Firmware functions */
90 static char *rt2800soc_get_firmware_name(struct rt2x00_dev *rt2x00dev)
91 {
92 @@ -172,7 +159,7 @@ static const struct rt2800_ops rt2800soc
93 .register_multiread = rt2x00mmio_register_multiread,
94 .register_multiwrite = rt2x00mmio_register_multiwrite,
95 .regbusy_read = rt2x00mmio_regbusy_read,
96 - .read_eeprom = rt2800soc_read_eeprom,
97 + .read_eeprom = rt2x00lib_read_eeprom,
98 .hwcrypt_disabled = rt2800soc_hwcrypt_disabled,
99 .drv_write_firmware = rt2800soc_write_firmware,
100 .drv_init_registers = rt2800mmio_init_registers,
101 --- a/drivers/net/wireless/ralink/rt2x00/rt2x00.h
102 +++ b/drivers/net/wireless/ralink/rt2x00/rt2x00.h
103 @@ -703,6 +703,7 @@ enum rt2x00_capability_flags {
104 REQUIRE_HT_TX_DESC,
105 REQUIRE_PS_AUTOWAKE,
106 REQUIRE_DELAYED_RFKILL,
107 + REQUIRE_EEPROM_FILE,
108
109 /*
110 * Capabilities
111 --- /dev/null
112 +++ b/drivers/net/wireless/ralink/rt2x00/rt2x00eeprom.c
113 @@ -0,0 +1,77 @@
114 +// SPDX-License-Identifier: GPL-2.0-or-later
115 +/* Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
116 + * Copyright (C) 2004 - 2009 Gertjan van Wingerde <gwingerde@gmail.com>
117 + * <http://rt2x00.serialmonkey.com>
118 + */
119 +
120 +/* Module: rt2x00lib
121 + * Abstract: rt2x00 eeprom file loading routines.
122 + */
123 +
124 +#include <linux/kernel.h>
125 +#include <linux/module.h>
126 +
127 +#include "rt2x00.h"
128 +#include "rt2x00soc.h"
129 +
130 +static const char *
131 +rt2x00lib_get_eeprom_file_name(struct rt2x00_dev *rt2x00dev)
132 +{
133 + struct rt2x00_platform_data *pdata = rt2x00dev->dev->platform_data;
134 +
135 + if (pdata && pdata->eeprom_file_name)
136 + return pdata->eeprom_file_name;
137 +
138 + return NULL;
139 +}
140 +
141 +static int rt2x00lib_read_eeprom_file(struct rt2x00_dev *rt2x00dev)
142 +{
143 + const struct firmware *ee;
144 + const char *ee_name;
145 + int retval;
146 +
147 + ee_name = rt2x00lib_get_eeprom_file_name(rt2x00dev);
148 + if (!ee_name && test_bit(REQUIRE_EEPROM_FILE, &rt2x00dev->cap_flags)) {
149 + rt2x00_err(rt2x00dev, "Required EEPROM name is missing.");
150 + return -EINVAL;
151 + }
152 +
153 + if (!ee_name)
154 + return 0;
155 +
156 + rt2x00_info(rt2x00dev, "Loading EEPROM data from '%s'.\n", ee_name);
157 +
158 + retval = request_firmware(&ee, ee_name, rt2x00dev->dev);
159 + if (retval) {
160 + rt2x00_err(rt2x00dev, "Failed to request EEPROM.\n");
161 + return retval;
162 + }
163 +
164 + if (!ee || !ee->size || !ee->data) {
165 + rt2x00_err(rt2x00dev, "Failed to read EEPROM file.\n");
166 + retval = -ENOENT;
167 + goto err_exit;
168 + }
169 +
170 + if (ee->size != rt2x00dev->ops->eeprom_size) {
171 + rt2x00_err(rt2x00dev,
172 + "EEPROM file size is invalid, it should be %d bytes\n",
173 + rt2x00dev->ops->eeprom_size);
174 + retval = -EINVAL;
175 + goto err_release_ee;
176 + }
177 +
178 + memcpy(rt2x00dev->eeprom, ee->data, rt2x00dev->ops->eeprom_size);
179 +
180 +err_release_ee:
181 + release_firmware(ee);
182 +err_exit:
183 + return retval;
184 +}
185 +
186 +int rt2x00lib_read_eeprom(struct rt2x00_dev *rt2x00dev)
187 +{
188 + return rt2x00lib_read_eeprom_file(rt2x00dev);
189 +}
190 +EXPORT_SYMBOL_GPL(rt2x00lib_read_eeprom);
191 --- a/drivers/net/wireless/ralink/rt2x00/rt2x00soc.c
192 +++ b/drivers/net/wireless/ralink/rt2x00/rt2x00soc.c
193 @@ -86,6 +86,7 @@ int rt2x00soc_probe(struct platform_devi
194 if (IS_ERR(rt2x00dev->clk))
195 rt2x00dev->clk = NULL;
196
197 + set_bit(REQUIRE_EEPROM_FILE, &rt2x00dev->cap_flags);
198 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_SOC);
199
200 retval = rt2x00soc_alloc_reg(rt2x00dev);
201 --- a/drivers/net/wireless/ralink/rt2x00/rt2800pci.c
202 +++ b/drivers/net/wireless/ralink/rt2x00/rt2800pci.c
203 @@ -278,6 +278,9 @@ static int rt2800pci_read_eeprom(struct
204 {
205 int retval;
206
207 + if (!rt2x00lib_read_eeprom(rt2x00dev))
208 + return 0;
209 +
210 if (rt2800pci_efuse_detect(rt2x00dev))
211 retval = rt2800pci_read_eeprom_efuse(rt2x00dev);
212 else
213 --- a/drivers/net/wireless/ralink/rt2x00/rt2800lib.h
214 +++ b/drivers/net/wireless/ralink/rt2x00/rt2800lib.h
215 @@ -270,4 +270,13 @@ void rt2800_get_txwi_rxwi_size(struct rt
216 unsigned short *rxwi_size);
217 void rt2800_pre_reset_hw(struct rt2x00_dev *rt2x00dev);
218
219 +/*
220 + * EEPROM file handlers.
221 + */
222 +#ifdef CPTCFG_RT2X00_LIB_EEPROM
223 +int rt2x00lib_read_eeprom(struct rt2x00_dev *rt2x00dev);
224 +#else
225 +#define rt2x00lib_read_eeprom NULL
226 +#endif /* CPTCFG_RT2X00_LIB_EEPROM */
227 +
228 #endif /* RT2800LIB_H */