18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/kernel.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/usb.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include "mt76x0.h"
118c2ecf20Sopenharmony_ci#include "mcu.h"
128c2ecf20Sopenharmony_ci#include "../mt76x02_usb.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic struct usb_device_id mt76x0_device_table[] = {
158c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x148F, 0x7610) },	/* MT7610U */
168c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x13B1, 0x003E) },	/* Linksys AE6000 */
178c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0E8D, 0x7610) },	/* Sabrent NTWLAC */
188c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x7392, 0xa711) },	/* Edimax 7711mac */
198c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x7392, 0xb711) },	/* Edimax / Elecom  */
208c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x148f, 0x761a) },	/* TP-Link TL-WDN5200 */
218c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x148f, 0x760a) },	/* TP-Link unknown */
228c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0b05, 0x17d1) },	/* Asus USB-AC51 */
238c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0b05, 0x17db) },	/* Asus USB-AC50 */
248c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0df6, 0x0075) },	/* Sitecom WLA-3100 */
258c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x2019, 0xab31) },	/* Planex GW-450D */
268c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x2001, 0x3d02) },	/* D-LINK DWA-171 rev B1 */
278c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0586, 0x3425) },	/* Zyxel NWD6505 */
288c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x07b8, 0x7610) },	/* AboCom AU7212 */
298c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x04bb, 0x0951) },	/* I-O DATA WN-AC433UK */
308c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x057c, 0x8502) },	/* AVM FRITZ!WLAN USB Stick AC 430 */
318c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x293c, 0x5702) },	/* Comcast Xfinity KXW02AAA  */
328c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x20f4, 0x806b) },	/* TRENDnet TEW-806UBH  */
338c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x7392, 0xc711) }, /* Devolo Wifi ac Stick */
348c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0df6, 0x0079) }, /* Sitecom Europe B.V. ac  Stick */
358c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x2357, 0x0123) }, /* TP-LINK T2UHP */
368c2ecf20Sopenharmony_ci	/* TP-LINK Archer T1U */
378c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x2357, 0x0105), .driver_info = 1, },
388c2ecf20Sopenharmony_ci	/* MT7630U */
398c2ecf20Sopenharmony_ci	{ USB_DEVICE_AND_INTERFACE_INFO(0x0E8D, 0x7630, 0xff, 0x2, 0xff)},
408c2ecf20Sopenharmony_ci	/* MT7650U */
418c2ecf20Sopenharmony_ci	{ USB_DEVICE_AND_INTERFACE_INFO(0x0E8D, 0x7650, 0xff, 0x2, 0xff)},
428c2ecf20Sopenharmony_ci	{ 0, }
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic void mt76x0_init_usb_dma(struct mt76x02_dev *dev)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	u32 val;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	val = mt76_rr(dev, MT_USB_DMA_CFG);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	val |= MT_USB_DMA_CFG_RX_BULK_EN |
528c2ecf20Sopenharmony_ci	       MT_USB_DMA_CFG_TX_BULK_EN;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	/* disable AGGR_BULK_RX in order to receive one
558c2ecf20Sopenharmony_ci	 * frame in each rx urb and avoid copies
568c2ecf20Sopenharmony_ci	 */
578c2ecf20Sopenharmony_ci	val &= ~MT_USB_DMA_CFG_RX_BULK_AGG_EN;
588c2ecf20Sopenharmony_ci	mt76_wr(dev, MT_USB_DMA_CFG, val);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	val = mt76_rr(dev, MT_COM_REG0);
618c2ecf20Sopenharmony_ci	if (val & 1)
628c2ecf20Sopenharmony_ci		dev_dbg(dev->mt76.dev, "MCU not ready\n");
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	val = mt76_rr(dev, MT_USB_DMA_CFG);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	val |= MT_USB_DMA_CFG_RX_DROP_OR_PAD;
678c2ecf20Sopenharmony_ci	mt76_wr(dev, MT_USB_DMA_CFG, val);
688c2ecf20Sopenharmony_ci	val &= ~MT_USB_DMA_CFG_RX_DROP_OR_PAD;
698c2ecf20Sopenharmony_ci	mt76_wr(dev, MT_USB_DMA_CFG, val);
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic void mt76x0u_cleanup(struct mt76x02_dev *dev)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
758c2ecf20Sopenharmony_ci	mt76x0_chip_onoff(dev, false, false);
768c2ecf20Sopenharmony_ci	mt76u_queues_deinit(&dev->mt76);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic void mt76x0u_stop(struct ieee80211_hw *hw)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct mt76x02_dev *dev = hw->priv;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	clear_bit(MT76_STATE_RUNNING, &dev->mphy.state);
848c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&dev->cal_work);
858c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&dev->mt76.mac_work);
868c2ecf20Sopenharmony_ci	mt76u_stop_tx(&dev->mt76);
878c2ecf20Sopenharmony_ci	mt76x02u_exit_beacon_config(dev);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	if (test_bit(MT76_REMOVED, &dev->mphy.state))
908c2ecf20Sopenharmony_ci		return;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (!mt76_poll(dev, MT_USB_DMA_CFG, MT_USB_DMA_CFG_TX_BUSY, 0, 1000))
938c2ecf20Sopenharmony_ci		dev_warn(dev->mt76.dev, "TX DMA did not stop\n");
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	mt76x0_mac_stop(dev);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	if (!mt76_poll(dev, MT_USB_DMA_CFG, MT_USB_DMA_CFG_RX_BUSY, 0, 1000))
988c2ecf20Sopenharmony_ci		dev_warn(dev->mt76.dev, "RX DMA did not stop\n");
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic int mt76x0u_start(struct ieee80211_hw *hw)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct mt76x02_dev *dev = hw->priv;
1048c2ecf20Sopenharmony_ci	int ret;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	ret = mt76x02u_mac_start(dev);
1078c2ecf20Sopenharmony_ci	if (ret)
1088c2ecf20Sopenharmony_ci		return ret;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	mt76x0_phy_calibrate(dev, true);
1118c2ecf20Sopenharmony_ci	ieee80211_queue_delayed_work(dev->mt76.hw, &dev->mt76.mac_work,
1128c2ecf20Sopenharmony_ci				     MT_MAC_WORK_INTERVAL);
1138c2ecf20Sopenharmony_ci	ieee80211_queue_delayed_work(dev->mt76.hw, &dev->cal_work,
1148c2ecf20Sopenharmony_ci				     MT_CALIBRATE_INTERVAL);
1158c2ecf20Sopenharmony_ci	set_bit(MT76_STATE_RUNNING, &dev->mphy.state);
1168c2ecf20Sopenharmony_ci	return 0;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic const struct ieee80211_ops mt76x0u_ops = {
1208c2ecf20Sopenharmony_ci	.tx = mt76x02_tx,
1218c2ecf20Sopenharmony_ci	.start = mt76x0u_start,
1228c2ecf20Sopenharmony_ci	.stop = mt76x0u_stop,
1238c2ecf20Sopenharmony_ci	.add_interface = mt76x02_add_interface,
1248c2ecf20Sopenharmony_ci	.remove_interface = mt76x02_remove_interface,
1258c2ecf20Sopenharmony_ci	.config = mt76x0_config,
1268c2ecf20Sopenharmony_ci	.configure_filter = mt76x02_configure_filter,
1278c2ecf20Sopenharmony_ci	.bss_info_changed = mt76x02_bss_info_changed,
1288c2ecf20Sopenharmony_ci	.sta_state = mt76_sta_state,
1298c2ecf20Sopenharmony_ci	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1308c2ecf20Sopenharmony_ci	.set_key = mt76x02_set_key,
1318c2ecf20Sopenharmony_ci	.conf_tx = mt76x02_conf_tx,
1328c2ecf20Sopenharmony_ci	.sw_scan_start = mt76_sw_scan,
1338c2ecf20Sopenharmony_ci	.sw_scan_complete = mt76x02_sw_scan_complete,
1348c2ecf20Sopenharmony_ci	.ampdu_action = mt76x02_ampdu_action,
1358c2ecf20Sopenharmony_ci	.sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
1368c2ecf20Sopenharmony_ci	.set_rts_threshold = mt76x02_set_rts_threshold,
1378c2ecf20Sopenharmony_ci	.wake_tx_queue = mt76_wake_tx_queue,
1388c2ecf20Sopenharmony_ci	.get_txpower = mt76_get_txpower,
1398c2ecf20Sopenharmony_ci	.get_survey = mt76_get_survey,
1408c2ecf20Sopenharmony_ci	.set_tim = mt76_set_tim,
1418c2ecf20Sopenharmony_ci	.release_buffered_frames = mt76_release_buffered_frames,
1428c2ecf20Sopenharmony_ci	.get_antenna = mt76_get_antenna,
1438c2ecf20Sopenharmony_ci};
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic int mt76x0u_init_hardware(struct mt76x02_dev *dev, bool reset)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	int err;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	mt76x0_chip_onoff(dev, true, reset);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	if (!mt76x02_wait_for_mac(&dev->mt76))
1528c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	err = mt76x0u_mcu_init(dev);
1558c2ecf20Sopenharmony_ci	if (err < 0)
1568c2ecf20Sopenharmony_ci		return err;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	mt76x0_init_usb_dma(dev);
1598c2ecf20Sopenharmony_ci	err = mt76x0_init_hardware(dev);
1608c2ecf20Sopenharmony_ci	if (err < 0)
1618c2ecf20Sopenharmony_ci		return err;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	mt76x02u_init_beacon_config(dev);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	mt76_rmw(dev, MT_US_CYC_CFG, MT_US_CYC_CNT, 0x1e);
1668c2ecf20Sopenharmony_ci	mt76_wr(dev, MT_TXOP_CTRL_CFG,
1678c2ecf20Sopenharmony_ci		FIELD_PREP(MT_TXOP_TRUN_EN, 0x3f) |
1688c2ecf20Sopenharmony_ci		FIELD_PREP(MT_TXOP_EXT_CCA_DLY, 0x58));
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	return 0;
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic int mt76x0u_register_device(struct mt76x02_dev *dev)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = dev->mt76.hw;
1768c2ecf20Sopenharmony_ci	struct mt76_usb *usb = &dev->mt76.usb;
1778c2ecf20Sopenharmony_ci	int err;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	usb->mcu.data = devm_kmalloc(dev->mt76.dev, MCU_RESP_URB_SIZE,
1808c2ecf20Sopenharmony_ci				     GFP_KERNEL);
1818c2ecf20Sopenharmony_ci	if (!usb->mcu.data)
1828c2ecf20Sopenharmony_ci		return -ENOMEM;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	err = mt76u_alloc_queues(&dev->mt76);
1858c2ecf20Sopenharmony_ci	if (err < 0)
1868c2ecf20Sopenharmony_ci		goto out_err;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	err = mt76x0u_init_hardware(dev, true);
1898c2ecf20Sopenharmony_ci	if (err < 0)
1908c2ecf20Sopenharmony_ci		goto out_err;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* check hw sg support in order to enable AMSDU */
1938c2ecf20Sopenharmony_ci	hw->max_tx_fragments = dev->mt76.usb.sg_en ? MT_TX_SG_MAX_SIZE : 1;
1948c2ecf20Sopenharmony_ci	err = mt76x0_register_device(dev);
1958c2ecf20Sopenharmony_ci	if (err < 0)
1968c2ecf20Sopenharmony_ci		goto out_err;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	return 0;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ciout_err:
2038c2ecf20Sopenharmony_ci	mt76x0u_cleanup(dev);
2048c2ecf20Sopenharmony_ci	return err;
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic int mt76x0u_probe(struct usb_interface *usb_intf,
2088c2ecf20Sopenharmony_ci			 const struct usb_device_id *id)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	static const struct mt76_driver_ops drv_ops = {
2118c2ecf20Sopenharmony_ci		.drv_flags = MT_DRV_SW_RX_AIRTIME,
2128c2ecf20Sopenharmony_ci		.survey_flags = SURVEY_INFO_TIME_TX,
2138c2ecf20Sopenharmony_ci		.update_survey = mt76x02_update_channel,
2148c2ecf20Sopenharmony_ci		.tx_prepare_skb = mt76x02u_tx_prepare_skb,
2158c2ecf20Sopenharmony_ci		.tx_complete_skb = mt76x02u_tx_complete_skb,
2168c2ecf20Sopenharmony_ci		.tx_status_data = mt76x02_tx_status_data,
2178c2ecf20Sopenharmony_ci		.rx_skb = mt76x02_queue_rx_skb,
2188c2ecf20Sopenharmony_ci		.sta_ps = mt76x02_sta_ps,
2198c2ecf20Sopenharmony_ci		.sta_add = mt76x02_sta_add,
2208c2ecf20Sopenharmony_ci		.sta_remove = mt76x02_sta_remove,
2218c2ecf20Sopenharmony_ci	};
2228c2ecf20Sopenharmony_ci	struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
2238c2ecf20Sopenharmony_ci	struct mt76x02_dev *dev;
2248c2ecf20Sopenharmony_ci	struct mt76_dev *mdev;
2258c2ecf20Sopenharmony_ci	u32 mac_rev;
2268c2ecf20Sopenharmony_ci	int ret;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), &mt76x0u_ops,
2298c2ecf20Sopenharmony_ci				 &drv_ops);
2308c2ecf20Sopenharmony_ci	if (!mdev)
2318c2ecf20Sopenharmony_ci		return -ENOMEM;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	dev = container_of(mdev, struct mt76x02_dev, mt76);
2348c2ecf20Sopenharmony_ci	mutex_init(&dev->phy_mutex);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	/* Quirk for Archer T1U */
2378c2ecf20Sopenharmony_ci	if (id->driver_info)
2388c2ecf20Sopenharmony_ci		dev->no_2ghz = true;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	usb_dev = usb_get_dev(usb_dev);
2418c2ecf20Sopenharmony_ci	usb_reset_device(usb_dev);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	usb_set_intfdata(usb_intf, dev);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	mt76x02u_init_mcu(mdev);
2468c2ecf20Sopenharmony_ci	ret = mt76u_init(mdev, usb_intf, false);
2478c2ecf20Sopenharmony_ci	if (ret)
2488c2ecf20Sopenharmony_ci		goto err;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	/* Disable the HW, otherwise MCU fail to initialize on hot reboot */
2518c2ecf20Sopenharmony_ci	mt76x0_chip_onoff(dev, false, false);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (!mt76x02_wait_for_mac(mdev)) {
2548c2ecf20Sopenharmony_ci		ret = -ETIMEDOUT;
2558c2ecf20Sopenharmony_ci		goto err;
2568c2ecf20Sopenharmony_ci	}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
2598c2ecf20Sopenharmony_ci	mac_rev = mt76_rr(dev, MT_MAC_CSR0);
2608c2ecf20Sopenharmony_ci	dev_info(mdev->dev, "ASIC revision: %08x MAC revision: %08x\n",
2618c2ecf20Sopenharmony_ci		 mdev->rev, mac_rev);
2628c2ecf20Sopenharmony_ci	if (!is_mt76x0(dev)) {
2638c2ecf20Sopenharmony_ci		ret = -ENODEV;
2648c2ecf20Sopenharmony_ci		goto err;
2658c2ecf20Sopenharmony_ci	}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	/* Note: vendor driver skips this check for MT76X0U */
2688c2ecf20Sopenharmony_ci	if (!(mt76_rr(dev, MT_EFUSE_CTRL) & MT_EFUSE_CTRL_SEL))
2698c2ecf20Sopenharmony_ci		dev_warn(mdev->dev, "Warning: eFUSE not present\n");
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	ret = mt76x0u_register_device(dev);
2728c2ecf20Sopenharmony_ci	if (ret < 0)
2738c2ecf20Sopenharmony_ci		goto err;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	return 0;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cierr:
2788c2ecf20Sopenharmony_ci	usb_set_intfdata(usb_intf, NULL);
2798c2ecf20Sopenharmony_ci	usb_put_dev(interface_to_usbdev(usb_intf));
2808c2ecf20Sopenharmony_ci	mt76_free_device(&dev->mt76);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	return ret;
2838c2ecf20Sopenharmony_ci}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_cistatic void mt76x0_disconnect(struct usb_interface *usb_intf)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	struct mt76x02_dev *dev = usb_get_intfdata(usb_intf);
2888c2ecf20Sopenharmony_ci	bool initialized = test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (!initialized)
2918c2ecf20Sopenharmony_ci		return;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	ieee80211_unregister_hw(dev->mt76.hw);
2948c2ecf20Sopenharmony_ci	mt76x0u_cleanup(dev);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	usb_set_intfdata(usb_intf, NULL);
2978c2ecf20Sopenharmony_ci	usb_put_dev(interface_to_usbdev(usb_intf));
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	mt76_free_device(&dev->mt76);
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic int __maybe_unused mt76x0_suspend(struct usb_interface *usb_intf,
3038c2ecf20Sopenharmony_ci					 pm_message_t state)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	struct mt76x02_dev *dev = usb_get_intfdata(usb_intf);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	mt76u_stop_rx(&dev->mt76);
3088c2ecf20Sopenharmony_ci	clear_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
3098c2ecf20Sopenharmony_ci	mt76x0_chip_onoff(dev, false, false);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	return 0;
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic int __maybe_unused mt76x0_resume(struct usb_interface *usb_intf)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	struct mt76x02_dev *dev = usb_get_intfdata(usb_intf);
3178c2ecf20Sopenharmony_ci	int ret;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	ret = mt76u_resume_rx(&dev->mt76);
3208c2ecf20Sopenharmony_ci	if (ret < 0)
3218c2ecf20Sopenharmony_ci		goto err;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	ret = mt76x0u_init_hardware(dev, false);
3248c2ecf20Sopenharmony_ci	if (ret)
3258c2ecf20Sopenharmony_ci		goto err;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	return 0;
3288c2ecf20Sopenharmony_cierr:
3298c2ecf20Sopenharmony_ci	mt76x0u_cleanup(dev);
3308c2ecf20Sopenharmony_ci	return ret;
3318c2ecf20Sopenharmony_ci}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, mt76x0_device_table);
3348c2ecf20Sopenharmony_ciMODULE_FIRMWARE(MT7610E_FIRMWARE);
3358c2ecf20Sopenharmony_ciMODULE_FIRMWARE(MT7610U_FIRMWARE);
3368c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic struct usb_driver mt76x0_driver = {
3398c2ecf20Sopenharmony_ci	.name		= KBUILD_MODNAME,
3408c2ecf20Sopenharmony_ci	.id_table	= mt76x0_device_table,
3418c2ecf20Sopenharmony_ci	.probe		= mt76x0u_probe,
3428c2ecf20Sopenharmony_ci	.disconnect	= mt76x0_disconnect,
3438c2ecf20Sopenharmony_ci	.suspend	= mt76x0_suspend,
3448c2ecf20Sopenharmony_ci	.resume		= mt76x0_resume,
3458c2ecf20Sopenharmony_ci	.reset_resume	= mt76x0_resume,
3468c2ecf20Sopenharmony_ci	.soft_unbind	= 1,
3478c2ecf20Sopenharmony_ci	.disable_hub_initiated_lpm = 1,
3488c2ecf20Sopenharmony_ci};
3498c2ecf20Sopenharmony_cimodule_usb_driver(mt76x0_driver);
350