usb.c (e8a264ccd2de0db8ffc2c6ed2c23644ed15854d4) usb.c (d28e1a48952e60fe47bba5b79fdbafc65f6c892b)
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>

--- 10 unchanged lines hidden (view full) ---

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
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>

--- 10 unchanged lines hidden (view full) ---

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
27static u32 mt7921u_rr(struct mt76_dev *dev, u32 addr)
28{
29 u32 ret;
30
31 mutex_lock(&dev->usb.usb_ctrl_mtx);
32 ret = ___mt76u_rr(dev, MT_VEND_READ_EXT,
33 USB_DIR_IN | MT_USB_TYPE_VENDOR, addr);
34 mutex_unlock(&dev->usb.usb_ctrl_mtx);
35
36 return ret;
37}
38
39static void mt7921u_wr(struct mt76_dev *dev, u32 addr, u32 val)
40{
41 mutex_lock(&dev->usb.usb_ctrl_mtx);
42 ___mt76u_wr(dev, MT_VEND_WRITE_EXT,
43 USB_DIR_OUT | MT_USB_TYPE_VENDOR, addr, val);
44 mutex_unlock(&dev->usb.usb_ctrl_mtx);
45}
46
47static u32 mt7921u_rmw(struct mt76_dev *dev, u32 addr,
48 u32 mask, u32 val)
49{
50 mutex_lock(&dev->usb.usb_ctrl_mtx);
51 val |= ___mt76u_rr(dev, MT_VEND_READ_EXT,
52 USB_DIR_IN | MT_USB_TYPE_VENDOR, addr) & ~mask;
53 ___mt76u_wr(dev, MT_VEND_WRITE_EXT,
54 USB_DIR_OUT | MT_USB_TYPE_VENDOR, addr, val);
55 mutex_unlock(&dev->usb.usb_ctrl_mtx);
56
57 return val;
58}
59
60static void mt7921u_copy(struct mt76_dev *dev, u32 offset,
61 const void *data, int len)
62{
63 struct mt76_usb *usb = &dev->usb;
64 int ret, i = 0, batch_len;
65 const u8 *val = data;
66
67 len = round_up(len, 4);
68
69 mutex_lock(&usb->usb_ctrl_mtx);
70 while (i < len) {
71 batch_len = min_t(int, usb->data_len, len - i);
72 memcpy(usb->data, val + i, batch_len);
73 ret = __mt76u_vendor_request(dev, MT_VEND_WRITE_EXT,
74 USB_DIR_OUT | MT_USB_TYPE_VENDOR,
75 (offset + i) >> 16, offset + i,
76 usb->data, batch_len);
77 if (ret < 0)
78 break;
79
80 i += batch_len;
81 }
82 mutex_unlock(&usb->usb_ctrl_mtx);
83}
84
85int mt7921u_mcu_power_on(struct mt792x_dev *dev)
86{
87 int ret;
88
89 ret = mt76u_vendor_request(&dev->mt76, MT_VEND_POWER_ON,
90 USB_DIR_OUT | MT_USB_TYPE_VENDOR,
91 0x0, 0x1, NULL, 0);
92 if (ret)
93 return ret;
94
95 if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_PWR_ON,
96 MT_TOP_MISC2_FW_PWR_ON, 500)) {
97 dev_err(dev->mt76.dev, "Timeout for power on\n");
98 ret = -EIO;
99 }
100
101 return ret;
102}
103
104static int
105mt7921u_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb,
106 int cmd, int *seq)
107{
108 struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
109 u32 pad, ep;
110 int ret;
111

--- 38 unchanged lines hidden (view full) ---

150 return ret;
151
152 set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
153 mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN);
154
155 return 0;
156}
157
27static int
28mt7921u_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

--- 38 unchanged lines hidden (view full) ---

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
81static 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);
130out:
131 clear_bit(MT76_RESET, &dev->mphy.state);
132
133 mt76_worker_enable(&dev->mt76.tx_worker);
134
135 return err;
136}
137
158static void mt7921u_stop(struct ieee80211_hw *hw)
159{
160 struct mt792x_dev *dev = mt792x_hw_dev(hw);
161
162 mt76u_stop_tx(&dev->mt76);
163 mt7921_stop(hw);
164}
165
138static 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
166static void mt7921u_cleanup(struct mt792x_dev *dev)
167{
168 clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
169 mt7921u_wfsys_reset(dev);
170 skb_queue_purge(&dev->mt76.mcu.res_q);
171 mt76u_queues_deinit(&dev->mt76);
172}
173
174static int mt7921u_probe(struct usb_interface *usb_intf,
175 const struct usb_device_id *id)
176{
177 static const struct mt76_driver_ops drv_ops = {
178 .txwi_size = MT_SDIO_TXD_SIZE,
179 .drv_flags = MT_DRV_RX_DMA_HDR | MT_DRV_HW_MGMT_TXQ |
180 MT_DRV_AMSDU_OFFLOAD,
181 .survey_flags = SURVEY_INFO_TIME_TX |

--- 6 unchanged lines hidden (view full) ---

188 .rx_check = mt7921_rx_check,
189 .sta_add = mt7921_mac_sta_add,
190 .sta_assoc = mt7921_mac_sta_assoc,
191 .sta_remove = mt7921_mac_sta_remove,
192 .update_survey = mt792x_update_channel,
193 };
194 static const struct mt792x_hif_ops hif_ops = {
195 .mcu_init = mt7921u_mcu_init,
146static 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 |

--- 6 unchanged lines hidden (view full) ---

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,
196 .init_reset = mt7921u_init_reset,
168 .init_reset = mt792xu_init_reset,
197 .reset = mt7921u_mac_reset,
198 };
199 static struct mt76_bus_ops bus_ops = {
169 .reset = mt7921u_mac_reset,
170 };
171 static struct mt76_bus_ops bus_ops = {
200 .rr = mt7921u_rr,
201 .wr = mt7921u_wr,
202 .rmw = mt7921u_rmw,
172 .rr = mt792xu_rr,
173 .wr = mt792xu_wr,
174 .rmw = mt792xu_rmw,
203 .read_copy = mt76u_read_copy,
175 .read_copy = mt76u_read_copy,
204 .write_copy = mt7921u_copy,
176 .write_copy = mt792xu_copy,
205 .type = MT76_BUS_USB,
206 };
207 struct usb_device *udev = interface_to_usbdev(usb_intf);
208 struct ieee80211_ops *ops;
209 struct ieee80211_hw *hw;
210 struct mt792x_dev *dev;
211 struct mt76_dev *mdev;
212 u8 features;

--- 22 unchanged lines hidden (view full) ---

235 if (ret < 0)
236 goto error;
237
238 mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
239 (mt76_rr(dev, MT_HW_REV) & 0xff);
240 dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
241
242 if (mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY)) {
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;

--- 22 unchanged lines hidden (view full) ---

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)) {
243 ret = mt7921u_wfsys_reset(dev);
215 ret = mt792xu_wfsys_reset(dev);
244 if (ret)
245 goto error;
246 }
247
216 if (ret)
217 goto error;
218 }
219
248 ret = mt7921u_mcu_power_on(dev);
220 ret = mt792xu_mcu_power_on(dev);
249 if (ret)
250 goto error;
251
252 ret = mt76u_alloc_mcu_queue(&dev->mt76);
253 if (ret)
254 goto error;
255
256 ret = mt76u_alloc_queues(&dev->mt76);
257 if (ret)
258 goto error;
259
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
260 ret = mt7921u_dma_init(dev, false);
232 ret = mt792xu_dma_init(dev, false);
261 if (ret)
262 goto error;
263
264 hw = mt76_hw(dev);
265 /* check hw sg support in order to enable AMSDU */
266 hw->max_tx_fragments = mdev->usb.sg_en ? MT_HW_TXP_MAX_BUF_NUM : 1;
267
268 ret = mt7921_register_device(dev);

--- 17 unchanged lines hidden (view full) ---

286{
287 struct mt792x_dev *dev = usb_get_intfdata(usb_intf);
288
289 cancel_work_sync(&dev->init_work);
290 if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
291 return;
292
293 mt76_unregister_device(&dev->mt76);
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);

--- 17 unchanged lines hidden (view full) ---

258{
259 struct mt792x_dev *dev = usb_get_intfdata(usb_intf);
260
261 cancel_work_sync(&dev->init_work);
262 if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
263 return;
264
265 mt76_unregister_device(&dev->mt76);
294 mt7921u_cleanup(dev);
266 mt792xu_cleanup(dev);
295
296 usb_set_intfdata(usb_intf, NULL);
297 usb_put_dev(interface_to_usbdev(usb_intf));
298
299 mt76_free_device(&dev->mt76);
300}
301
302#ifdef CONFIG_PM

--- 42 unchanged lines hidden (view full) ---

345 mt76_wr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT, 0);
346 break;
347 }
348
349 msleep(20);
350 }
351
352 if (reinit || mt792x_dma_need_reinit(dev)) {
267
268 usb_set_intfdata(usb_intf, NULL);
269 usb_put_dev(interface_to_usbdev(usb_intf));
270
271 mt76_free_device(&dev->mt76);
272}
273
274#ifdef CONFIG_PM

--- 42 unchanged lines hidden (view full) ---

317 mt76_wr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT, 0);
318 break;
319 }
320
321 msleep(20);
322 }
323
324 if (reinit || mt792x_dma_need_reinit(dev)) {
353 err = mt7921u_dma_init(dev, true);
325 err = mt792xu_dma_init(dev, true);
354 if (err)
355 goto failed;
356 }
357
358 err = mt76u_resume_rx(&dev->mt76);
359 if (err < 0)
360 goto failed;
361

--- 32 unchanged lines hidden ---
326 if (err)
327 goto failed;
328 }
329
330 err = mt76u_resume_rx(&dev->mt76);
331 if (err < 0)
332 goto failed;
333

--- 32 unchanged lines hidden ---