1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2022 MediaTek Inc.
3  *
4  * Author: Lorenzo Bianconi <lorenzo@kernel.org>
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/usb.h>
10 
11 #include "mt7921.h"
12 #include "mcu.h"
13 #include "../mt76_connac2_mac.h"
14 
15 static const struct usb_device_id mt7921u_device_table[] = {
16 	{ USB_DEVICE_AND_INTERFACE_INFO(0x0e8d, 0x7961, 0xff, 0xff, 0xff),
17 		.driver_info = (kernel_ulong_t)MT7921_FIRMWARE_WM },
18 	/* Comfast CF-952AX */
19 	{ USB_DEVICE_AND_INTERFACE_INFO(0x3574, 0x6211, 0xff, 0xff, 0xff),
20 		.driver_info = (kernel_ulong_t)MT7921_FIRMWARE_WM },
21 	/* Netgear, Inc. [A8000,AXE3000] */
22 	{ USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0x9060, 0xff, 0xff, 0xff),
23 		.driver_info = (kernel_ulong_t)MT7921_FIRMWARE_WM },
24 	{ },
25 };
26 
27 static int
mt7921u_mcu_send_message(struct mt76_dev * mdev,struct sk_buff * skb,int cmd,int * seq)28 mt7921u_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb,
29 			 int cmd, int *seq)
30 {
31 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
32 	u32 pad, ep;
33 	int ret;
34 
35 	ret = mt76_connac2_mcu_fill_message(mdev, skb, cmd, seq);
36 	if (ret)
37 		return ret;
38 
39 	mdev->mcu.timeout = 3 * HZ;
40 
41 	if (cmd != MCU_CMD(FW_SCATTER))
42 		ep = MT_EP_OUT_INBAND_CMD;
43 	else
44 		ep = MT_EP_OUT_AC_BE;
45 
46 	mt7921_skb_add_usb_sdio_hdr(dev, skb, 0);
47 	pad = round_up(skb->len, 4) + 4 - skb->len;
48 	__skb_put_zero(skb, pad);
49 
50 	ret = mt76u_bulk_msg(&dev->mt76, skb->data, skb->len, NULL,
51 			     1000, ep);
52 	dev_kfree_skb(skb);
53 
54 	return ret;
55 }
56 
mt7921u_mcu_init(struct mt792x_dev * dev)57 static int mt7921u_mcu_init(struct mt792x_dev *dev)
58 {
59 	static const struct mt76_mcu_ops mcu_ops = {
60 		.headroom = MT_SDIO_HDR_SIZE +
61 			    sizeof(struct mt76_connac2_mcu_txd),
62 		.tailroom = MT_USB_TAIL_SIZE,
63 		.mcu_skb_send_msg = mt7921u_mcu_send_message,
64 		.mcu_parse_response = mt7921_mcu_parse_response,
65 	};
66 	int ret;
67 
68 	dev->mt76.mcu_ops = &mcu_ops;
69 
70 	mt76_set(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
71 	ret = mt7921_run_firmware(dev);
72 	if (ret)
73 		return ret;
74 
75 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
76 	mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
77 
78 	return 0;
79 }
80 
mt7921u_mac_reset(struct mt792x_dev * dev)81 static int mt7921u_mac_reset(struct mt792x_dev *dev)
82 {
83 	int err;
84 
85 	mt76_txq_schedule_all(&dev->mphy);
86 	mt76_worker_disable(&dev->mt76.tx_worker);
87 
88 	set_bit(MT76_RESET, &dev->mphy.state);
89 	set_bit(MT76_MCU_RESET, &dev->mphy.state);
90 
91 	wake_up(&dev->mt76.mcu.wait);
92 	skb_queue_purge(&dev->mt76.mcu.res_q);
93 
94 	mt76u_stop_rx(&dev->mt76);
95 	mt76u_stop_tx(&dev->mt76);
96 
97 	mt792xu_wfsys_reset(dev);
98 
99 	clear_bit(MT76_MCU_RESET, &dev->mphy.state);
100 	err = mt76u_resume_rx(&dev->mt76);
101 	if (err)
102 		goto out;
103 
104 	err = mt792xu_mcu_power_on(dev);
105 	if (err)
106 		goto out;
107 
108 	err = mt792xu_dma_init(dev, false);
109 	if (err)
110 		goto out;
111 
112 	mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);
113 	mt76_set(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
114 
115 	err = mt7921_run_firmware(dev);
116 	if (err)
117 		goto out;
118 
119 	mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
120 
121 	err = mt7921_mcu_set_eeprom(dev);
122 	if (err)
123 		goto out;
124 
125 	err = mt7921_mac_init(dev);
126 	if (err)
127 		goto out;
128 
129 	err = __mt7921_start(&dev->phy);
130 out:
131 	clear_bit(MT76_RESET, &dev->mphy.state);
132 
133 	mt76_worker_enable(&dev->mt76.tx_worker);
134 
135 	return err;
136 }
137 
mt7921u_stop(struct ieee80211_hw * hw)138 static void mt7921u_stop(struct ieee80211_hw *hw)
139 {
140 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
141 
142 	mt76u_stop_tx(&dev->mt76);
143 	mt7921_stop(hw);
144 }
145 
mt7921u_probe(struct usb_interface * usb_intf,const struct usb_device_id * id)146 static int mt7921u_probe(struct usb_interface *usb_intf,
147 			 const struct usb_device_id *id)
148 {
149 	static const struct mt76_driver_ops drv_ops = {
150 		.txwi_size = MT_SDIO_TXD_SIZE,
151 		.drv_flags = MT_DRV_RX_DMA_HDR | MT_DRV_HW_MGMT_TXQ |
152 			     MT_DRV_AMSDU_OFFLOAD,
153 		.survey_flags = SURVEY_INFO_TIME_TX |
154 				SURVEY_INFO_TIME_RX |
155 				SURVEY_INFO_TIME_BSS_RX,
156 		.tx_prepare_skb = mt7921_usb_sdio_tx_prepare_skb,
157 		.tx_complete_skb = mt7921_usb_sdio_tx_complete_skb,
158 		.tx_status_data = mt7921_usb_sdio_tx_status_data,
159 		.rx_skb = mt7921_queue_rx_skb,
160 		.rx_check = mt7921_rx_check,
161 		.sta_add = mt7921_mac_sta_add,
162 		.sta_assoc = mt7921_mac_sta_assoc,
163 		.sta_remove = mt7921_mac_sta_remove,
164 		.update_survey = mt792x_update_channel,
165 	};
166 	static const struct mt792x_hif_ops hif_ops = {
167 		.mcu_init = mt7921u_mcu_init,
168 		.init_reset = mt792xu_init_reset,
169 		.reset = mt7921u_mac_reset,
170 	};
171 	static struct mt76_bus_ops bus_ops = {
172 		.rr = mt792xu_rr,
173 		.wr = mt792xu_wr,
174 		.rmw = mt792xu_rmw,
175 		.read_copy = mt76u_read_copy,
176 		.write_copy = mt792xu_copy,
177 		.type = MT76_BUS_USB,
178 	};
179 	struct usb_device *udev = interface_to_usbdev(usb_intf);
180 	struct ieee80211_ops *ops;
181 	struct ieee80211_hw *hw;
182 	struct mt792x_dev *dev;
183 	struct mt76_dev *mdev;
184 	u8 features;
185 	int ret;
186 
187 	ops = mt792x_get_mac80211_ops(&usb_intf->dev, &mt7921_ops,
188 				      (void *)id->driver_info, &features);
189 	if (!ops)
190 		return -ENOMEM;
191 
192 	ops->stop = mt7921u_stop;
193 	mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), ops, &drv_ops);
194 	if (!mdev)
195 		return -ENOMEM;
196 
197 	dev = container_of(mdev, struct mt792x_dev, mt76);
198 	dev->fw_features = features;
199 	dev->hif_ops = &hif_ops;
200 
201 	udev = usb_get_dev(udev);
202 	usb_reset_device(udev);
203 
204 	usb_set_intfdata(usb_intf, dev);
205 
206 	ret = __mt76u_init(mdev, usb_intf, &bus_ops);
207 	if (ret < 0)
208 		goto error;
209 
210 	mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
211 		    (mt76_rr(dev, MT_HW_REV) & 0xff);
212 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
213 
214 	if (mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY)) {
215 		ret = mt792xu_wfsys_reset(dev);
216 		if (ret)
217 			goto error;
218 	}
219 
220 	ret = mt792xu_mcu_power_on(dev);
221 	if (ret)
222 		goto error;
223 
224 	ret = mt76u_alloc_mcu_queue(&dev->mt76);
225 	if (ret)
226 		goto error;
227 
228 	ret = mt76u_alloc_queues(&dev->mt76);
229 	if (ret)
230 		goto error;
231 
232 	ret = mt792xu_dma_init(dev, false);
233 	if (ret)
234 		goto error;
235 
236 	hw = mt76_hw(dev);
237 	/* check hw sg support in order to enable AMSDU */
238 	hw->max_tx_fragments = mdev->usb.sg_en ? MT_HW_TXP_MAX_BUF_NUM : 1;
239 
240 	ret = mt7921_register_device(dev);
241 	if (ret)
242 		goto error;
243 
244 	return 0;
245 
246 error:
247 	mt76u_queues_deinit(&dev->mt76);
248 
249 	usb_set_intfdata(usb_intf, NULL);
250 	usb_put_dev(interface_to_usbdev(usb_intf));
251 
252 	mt76_free_device(&dev->mt76);
253 
254 	return ret;
255 }
256 
257 #ifdef CONFIG_PM
mt7921u_suspend(struct usb_interface * intf,pm_message_t state)258 static int mt7921u_suspend(struct usb_interface *intf, pm_message_t state)
259 {
260 	struct mt792x_dev *dev = usb_get_intfdata(intf);
261 	struct mt76_connac_pm *pm = &dev->pm;
262 	int err;
263 
264 	pm->suspended = true;
265 	flush_work(&dev->reset_work);
266 
267 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
268 	if (err)
269 		goto failed;
270 
271 	mt76u_stop_rx(&dev->mt76);
272 	mt76u_stop_tx(&dev->mt76);
273 
274 	return 0;
275 
276 failed:
277 	pm->suspended = false;
278 
279 	if (err < 0)
280 		mt792x_reset(&dev->mt76);
281 
282 	return err;
283 }
284 
mt7921u_resume(struct usb_interface * intf)285 static int mt7921u_resume(struct usb_interface *intf)
286 {
287 	struct mt792x_dev *dev = usb_get_intfdata(intf);
288 	struct mt76_connac_pm *pm = &dev->pm;
289 	bool reinit = true;
290 	int err, i;
291 
292 	for (i = 0; i < 10; i++) {
293 		u32 val = mt76_rr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT);
294 
295 		if (!(val & MT_WF_SW_SER_TRIGGER_SUSPEND)) {
296 			reinit = false;
297 			break;
298 		}
299 		if (val & MT_WF_SW_SER_DONE_SUSPEND) {
300 			mt76_wr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT, 0);
301 			break;
302 		}
303 
304 		msleep(20);
305 	}
306 
307 	if (reinit || mt792x_dma_need_reinit(dev)) {
308 		err = mt792xu_dma_init(dev, true);
309 		if (err)
310 			goto failed;
311 	}
312 
313 	err = mt76u_resume_rx(&dev->mt76);
314 	if (err < 0)
315 		goto failed;
316 
317 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
318 failed:
319 	pm->suspended = false;
320 
321 	if (err < 0)
322 		mt792x_reset(&dev->mt76);
323 
324 	return err;
325 }
326 #endif /* CONFIG_PM */
327 
328 MODULE_DEVICE_TABLE(usb, mt7921u_device_table);
329 MODULE_FIRMWARE(MT7921_FIRMWARE_WM);
330 MODULE_FIRMWARE(MT7921_ROM_PATCH);
331 
332 static struct usb_driver mt7921u_driver = {
333 	.name		= KBUILD_MODNAME,
334 	.id_table	= mt7921u_device_table,
335 	.probe		= mt7921u_probe,
336 	.disconnect	= mt792xu_disconnect,
337 #ifdef CONFIG_PM
338 	.suspend	= mt7921u_suspend,
339 	.resume		= mt7921u_resume,
340 	.reset_resume	= mt7921u_resume,
341 #endif /* CONFIG_PM */
342 	.soft_unbind	= 1,
343 	.disable_hub_initiated_lpm = 1,
344 };
345 module_usb_driver(mt7921u_driver);
346 
347 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
348 MODULE_LICENSE("Dual BSD/GPL");
349