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 mt792x_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_probe(struct usb_interface * usb_intf,const struct usb_device_id * id)138 static int mt7921u_probe(struct usb_interface *usb_intf,
139 const struct usb_device_id *id)
140 {
141 static const struct mt76_driver_ops drv_ops = {
142 .txwi_size = MT_SDIO_TXD_SIZE,
143 .drv_flags = MT_DRV_RX_DMA_HDR | MT_DRV_HW_MGMT_TXQ |
144 MT_DRV_AMSDU_OFFLOAD,
145 .survey_flags = SURVEY_INFO_TIME_TX |
146 SURVEY_INFO_TIME_RX |
147 SURVEY_INFO_TIME_BSS_RX,
148 .tx_prepare_skb = mt7921_usb_sdio_tx_prepare_skb,
149 .tx_complete_skb = mt7921_usb_sdio_tx_complete_skb,
150 .tx_status_data = mt7921_usb_sdio_tx_status_data,
151 .rx_skb = mt7921_queue_rx_skb,
152 .rx_check = mt7921_rx_check,
153 .sta_add = mt7921_mac_sta_add,
154 .sta_event = mt7921_mac_sta_event,
155 .sta_remove = mt7921_mac_sta_remove,
156 .update_survey = mt792x_update_channel,
157 .set_channel = mt7921_set_channel,
158 };
159 static const struct mt792x_hif_ops hif_ops = {
160 .mcu_init = mt7921u_mcu_init,
161 .init_reset = mt792xu_init_reset,
162 .reset = mt7921u_mac_reset,
163 };
164 static struct mt76_bus_ops bus_ops = {
165 .rr = mt792xu_rr,
166 .wr = mt792xu_wr,
167 .rmw = mt792xu_rmw,
168 .read_copy = mt76u_read_copy,
169 .write_copy = mt792xu_copy,
170 .type = MT76_BUS_USB,
171 };
172 struct usb_device *udev = interface_to_usbdev(usb_intf);
173 struct ieee80211_ops *ops;
174 struct ieee80211_hw *hw;
175 struct mt792x_dev *dev;
176 struct mt76_dev *mdev;
177 u8 features;
178 int ret;
179
180 ops = mt792x_get_mac80211_ops(&usb_intf->dev, &mt7921_ops,
181 (void *)id->driver_info, &features);
182 if (!ops)
183 return -ENOMEM;
184
185 ops->stop = mt792xu_stop;
186 mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), ops, &drv_ops);
187 if (!mdev)
188 return -ENOMEM;
189
190 dev = container_of(mdev, struct mt792x_dev, mt76);
191 dev->fw_features = features;
192 dev->hif_ops = &hif_ops;
193
194 udev = usb_get_dev(udev);
195 usb_reset_device(udev);
196
197 usb_set_intfdata(usb_intf, dev);
198
199 ret = __mt76u_init(mdev, usb_intf, &bus_ops);
200 if (ret < 0)
201 goto error;
202
203 mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
204 (mt76_rr(dev, MT_HW_REV) & 0xff);
205 dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
206
207 if (mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY)) {
208 ret = mt792xu_wfsys_reset(dev);
209 if (ret)
210 goto error;
211 }
212
213 ret = mt792xu_mcu_power_on(dev);
214 if (ret)
215 goto error;
216
217 ret = mt76u_alloc_mcu_queue(&dev->mt76);
218 if (ret)
219 goto error;
220
221 ret = mt76u_alloc_queues(&dev->mt76);
222 if (ret)
223 goto error;
224
225 ret = mt792xu_dma_init(dev, false);
226 if (ret)
227 goto error;
228
229 hw = mt76_hw(dev);
230 /* check hw sg support in order to enable AMSDU */
231 hw->max_tx_fragments = mdev->usb.sg_en ? MT_HW_TXP_MAX_BUF_NUM : 1;
232
233 ret = mt7921_register_device(dev);
234 if (ret)
235 goto error;
236
237 return 0;
238
239 error:
240 mt76u_queues_deinit(&dev->mt76);
241
242 usb_set_intfdata(usb_intf, NULL);
243 usb_put_dev(interface_to_usbdev(usb_intf));
244
245 mt76_free_device(&dev->mt76);
246
247 return ret;
248 }
249
250 #ifdef CONFIG_PM
mt7921u_suspend(struct usb_interface * intf,pm_message_t state)251 static int mt7921u_suspend(struct usb_interface *intf, pm_message_t state)
252 {
253 struct mt792x_dev *dev = usb_get_intfdata(intf);
254 struct mt76_connac_pm *pm = &dev->pm;
255 int err;
256
257 pm->suspended = true;
258 flush_work(&dev->reset_work);
259
260 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
261 if (err)
262 goto failed;
263
264 mt76u_stop_rx(&dev->mt76);
265 mt76u_stop_tx(&dev->mt76);
266
267 return 0;
268
269 failed:
270 pm->suspended = false;
271
272 if (err < 0)
273 mt792x_reset(&dev->mt76);
274
275 return err;
276 }
277
mt7921u_resume(struct usb_interface * intf)278 static int mt7921u_resume(struct usb_interface *intf)
279 {
280 struct mt792x_dev *dev = usb_get_intfdata(intf);
281 struct mt76_connac_pm *pm = &dev->pm;
282 bool reinit = true;
283 int err, i;
284
285 for (i = 0; i < 10; i++) {
286 u32 val = mt76_rr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT);
287
288 if (!(val & MT_WF_SW_SER_TRIGGER_SUSPEND)) {
289 reinit = false;
290 break;
291 }
292 if (val & MT_WF_SW_SER_DONE_SUSPEND) {
293 mt76_wr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT, 0);
294 break;
295 }
296
297 msleep(20);
298 }
299
300 if (reinit || mt792x_dma_need_reinit(dev)) {
301 err = mt792xu_dma_init(dev, true);
302 if (err)
303 goto failed;
304 }
305
306 err = mt76u_resume_rx(&dev->mt76);
307 if (err < 0)
308 goto failed;
309
310 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
311 failed:
312 pm->suspended = false;
313
314 if (err < 0)
315 mt792x_reset(&dev->mt76);
316
317 return err;
318 }
319 #endif /* CONFIG_PM */
320
321 MODULE_DEVICE_TABLE(usb, mt7921u_device_table);
322 MODULE_FIRMWARE(MT7921_FIRMWARE_WM);
323 MODULE_FIRMWARE(MT7921_ROM_PATCH);
324
325 static struct usb_driver mt7921u_driver = {
326 .name = KBUILD_MODNAME,
327 .id_table = mt7921u_device_table,
328 .probe = mt7921u_probe,
329 .disconnect = mt792xu_disconnect,
330 #ifdef CONFIG_PM
331 .suspend = mt7921u_suspend,
332 .resume = mt7921u_resume,
333 .reset_resume = mt7921u_resume,
334 #endif /* CONFIG_PM */
335 .soft_unbind = 1,
336 .disable_hub_initiated_lpm = 1,
337 };
338 module_usb_driver(mt7921u_driver);
339
340 MODULE_DESCRIPTION("MediaTek MT7921U (USB) wireless driver");
341 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
342 MODULE_LICENSE("Dual BSD/GPL");
343