xref: /linux/drivers/net/wireless/mediatek/mt76/mt76x2/usb.c (revision 70b8250b30e2abbaa230e962a7198aa552654ce6)
10e3d6777SRyder Lee // SPDX-License-Identifier: ISC
21613c621SLorenzo Bianconi /*
31613c621SLorenzo Bianconi  * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
41613c621SLorenzo Bianconi  */
51613c621SLorenzo Bianconi 
61613c621SLorenzo Bianconi #include <linux/kernel.h>
71613c621SLorenzo Bianconi #include <linux/module.h>
81613c621SLorenzo Bianconi 
91613c621SLorenzo Bianconi #include "../mt76x02_usb.h"
101613c621SLorenzo Bianconi #include "mt76x2u.h"
111613c621SLorenzo Bianconi 
121613c621SLorenzo Bianconi static const struct usb_device_id mt76x2u_device_table[] = {
131613c621SLorenzo Bianconi 	{ USB_DEVICE(0x0b05, 0x1833) },	/* Asus USB-AC54 */
141613c621SLorenzo Bianconi 	{ USB_DEVICE(0x0b05, 0x17eb) },	/* Asus USB-AC55 */
151613c621SLorenzo Bianconi 	{ USB_DEVICE(0x0b05, 0x180b) },	/* Asus USB-N53 B1 */
16411e05f4SLorenzo Bianconi 	{ USB_DEVICE(0x0e8d, 0x7612) },	/* Aukey USBAC1200 - Alfa AWUS036ACM */
171613c621SLorenzo Bianconi 	{ USB_DEVICE(0x057c, 0x8503) },	/* Avm FRITZ!WLAN AC860 */
181613c621SLorenzo Bianconi 	{ USB_DEVICE(0x7392, 0xb711) },	/* Edimax EW 7722 UAC */
1905b5a339SLorenzo Bianconi 	{ USB_DEVICE(0x0e8d, 0x7632) },	/* HC-M7662BU1 */
203efdfbe0SLorenzo Bianconi 	{ USB_DEVICE(0x2c4e, 0x0103) },	/* Mercury UD13 */
21*70b8250bSDacio Romero 	{ USB_DEVICE(0x0846, 0x9014) },	/* Netgear WNDA3100v3 */
221613c621SLorenzo Bianconi 	{ USB_DEVICE(0x0846, 0x9053) },	/* Netgear A6210 */
231613c621SLorenzo Bianconi 	{ USB_DEVICE(0x045e, 0x02e6) },	/* XBox One Wireless Adapter */
24b2934279SMatthew Garrett 	{ USB_DEVICE(0x045e, 0x02fe) },	/* XBox One Wireless Adapter */
251613c621SLorenzo Bianconi 	{ },
261613c621SLorenzo Bianconi };
271613c621SLorenzo Bianconi 
281613c621SLorenzo Bianconi static int mt76x2u_probe(struct usb_interface *intf,
291613c621SLorenzo Bianconi 			 const struct usb_device_id *id)
301613c621SLorenzo Bianconi {
313bfaa974SLorenzo Bianconi 	static const struct mt76_driver_ops drv_ops = {
325ce09c1aSFelix Fietkau 		.drv_flags = MT_DRV_SW_RX_AIRTIME,
33355f8d00SFelix Fietkau 		.survey_flags = SURVEY_INFO_TIME_TX,
34168aea24SLorenzo Bianconi 		.update_survey = mt76x02_update_channel,
353bfaa974SLorenzo Bianconi 		.tx_prepare_skb = mt76x02u_tx_prepare_skb,
363bfaa974SLorenzo Bianconi 		.tx_complete_skb = mt76x02u_tx_complete_skb,
373bfaa974SLorenzo Bianconi 		.tx_status_data = mt76x02_tx_status_data,
383bfaa974SLorenzo Bianconi 		.rx_skb = mt76x02_queue_rx_skb,
392baed5dbSStanislaw Gruszka 		.sta_ps = mt76x02_sta_ps,
403bfaa974SLorenzo Bianconi 		.sta_add = mt76x02_sta_add,
413bfaa974SLorenzo Bianconi 		.sta_remove = mt76x02_sta_remove,
423bfaa974SLorenzo Bianconi 	};
431613c621SLorenzo Bianconi 	struct usb_device *udev = interface_to_usbdev(intf);
44e40803f2SLorenzo Bianconi 	struct mt76x02_dev *dev;
453bfaa974SLorenzo Bianconi 	struct mt76_dev *mdev;
461613c621SLorenzo Bianconi 	int err;
471613c621SLorenzo Bianconi 
4880df01f4SLorenzo Bianconi 	mdev = mt76_alloc_device(&intf->dev, sizeof(*dev), &mt76x2u_ops,
493bfaa974SLorenzo Bianconi 				 &drv_ops);
503bfaa974SLorenzo Bianconi 	if (!mdev)
511613c621SLorenzo Bianconi 		return -ENOMEM;
521613c621SLorenzo Bianconi 
533bfaa974SLorenzo Bianconi 	dev = container_of(mdev, struct mt76x02_dev, mt76);
543bfaa974SLorenzo Bianconi 
551613c621SLorenzo Bianconi 	udev = usb_get_dev(udev);
561613c621SLorenzo Bianconi 	usb_reset_device(udev);
571613c621SLorenzo Bianconi 
58112f980aSStanislaw Gruszka 	usb_set_intfdata(intf, dev);
59112f980aSStanislaw Gruszka 
603bfaa974SLorenzo Bianconi 	mt76x02u_init_mcu(mdev);
616cb596baSLorenzo Bianconi 	err = mt76u_init(mdev, intf);
621613c621SLorenzo Bianconi 	if (err < 0)
631613c621SLorenzo Bianconi 		goto err;
641613c621SLorenzo Bianconi 
653bfaa974SLorenzo Bianconi 	mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
663bfaa974SLorenzo Bianconi 	dev_info(mdev->dev, "ASIC revision: %08x\n", mdev->rev);
6740b94161SStanislaw Gruszka 	if (!is_mt76x2(dev)) {
6840b94161SStanislaw Gruszka 		err = -ENODEV;
6940b94161SStanislaw Gruszka 		goto err;
7040b94161SStanislaw Gruszka 	}
711613c621SLorenzo Bianconi 
721613c621SLorenzo Bianconi 	err = mt76x2u_register_device(dev);
731613c621SLorenzo Bianconi 	if (err < 0)
741613c621SLorenzo Bianconi 		goto err;
751613c621SLorenzo Bianconi 
761613c621SLorenzo Bianconi 	return 0;
771613c621SLorenzo Bianconi 
781613c621SLorenzo Bianconi err:
79be83a7e2SLorenzo Bianconi 	mt76u_queues_deinit(&dev->mt76);
80a86f1d01SLorenzo Bianconi 	mt76_free_device(&dev->mt76);
811613c621SLorenzo Bianconi 	usb_set_intfdata(intf, NULL);
821613c621SLorenzo Bianconi 	usb_put_dev(udev);
831613c621SLorenzo Bianconi 
841613c621SLorenzo Bianconi 	return err;
851613c621SLorenzo Bianconi }
861613c621SLorenzo Bianconi 
871613c621SLorenzo Bianconi static void mt76x2u_disconnect(struct usb_interface *intf)
881613c621SLorenzo Bianconi {
891613c621SLorenzo Bianconi 	struct usb_device *udev = interface_to_usbdev(intf);
90e40803f2SLorenzo Bianconi 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
911613c621SLorenzo Bianconi 	struct ieee80211_hw *hw = mt76_hw(dev);
921613c621SLorenzo Bianconi 
93011849e0SFelix Fietkau 	set_bit(MT76_REMOVED, &dev->mphy.state);
941613c621SLorenzo Bianconi 	ieee80211_unregister_hw(hw);
951613c621SLorenzo Bianconi 	mt76x2u_cleanup(dev);
96a86f1d01SLorenzo Bianconi 	mt76_free_device(&dev->mt76);
971613c621SLorenzo Bianconi 	usb_set_intfdata(intf, NULL);
981613c621SLorenzo Bianconi 	usb_put_dev(udev);
991613c621SLorenzo Bianconi }
1001613c621SLorenzo Bianconi 
1011613c621SLorenzo Bianconi static int __maybe_unused mt76x2u_suspend(struct usb_interface *intf,
1021613c621SLorenzo Bianconi 					  pm_message_t state)
1031613c621SLorenzo Bianconi {
104e40803f2SLorenzo Bianconi 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
1051613c621SLorenzo Bianconi 
10639d501d9SStanislaw Gruszka 	mt76u_stop_rx(&dev->mt76);
1071613c621SLorenzo Bianconi 
1081613c621SLorenzo Bianconi 	return 0;
1091613c621SLorenzo Bianconi }
1101613c621SLorenzo Bianconi 
1111613c621SLorenzo Bianconi static int __maybe_unused mt76x2u_resume(struct usb_interface *intf)
1121613c621SLorenzo Bianconi {
113e40803f2SLorenzo Bianconi 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
1141613c621SLorenzo Bianconi 	int err;
1151613c621SLorenzo Bianconi 
11639d501d9SStanislaw Gruszka 	err = mt76u_resume_rx(&dev->mt76);
1171613c621SLorenzo Bianconi 	if (err < 0)
1181613c621SLorenzo Bianconi 		goto err;
1191613c621SLorenzo Bianconi 
1201613c621SLorenzo Bianconi 	err = mt76x2u_init_hardware(dev);
1211613c621SLorenzo Bianconi 	if (err < 0)
1221613c621SLorenzo Bianconi 		goto err;
1231613c621SLorenzo Bianconi 
1241613c621SLorenzo Bianconi 	return 0;
1251613c621SLorenzo Bianconi 
1261613c621SLorenzo Bianconi err:
1271613c621SLorenzo Bianconi 	mt76x2u_cleanup(dev);
1281613c621SLorenzo Bianconi 	return err;
1291613c621SLorenzo Bianconi }
1301613c621SLorenzo Bianconi 
1311613c621SLorenzo Bianconi MODULE_DEVICE_TABLE(usb, mt76x2u_device_table);
132cac97ed6SLorenzo Bianconi MODULE_FIRMWARE(MT7662_FIRMWARE);
133cac97ed6SLorenzo Bianconi MODULE_FIRMWARE(MT7662_ROM_PATCH);
1341613c621SLorenzo Bianconi 
1351613c621SLorenzo Bianconi static struct usb_driver mt76x2u_driver = {
1361613c621SLorenzo Bianconi 	.name		= KBUILD_MODNAME,
1371613c621SLorenzo Bianconi 	.id_table	= mt76x2u_device_table,
1381613c621SLorenzo Bianconi 	.probe		= mt76x2u_probe,
1391613c621SLorenzo Bianconi 	.disconnect	= mt76x2u_disconnect,
1401613c621SLorenzo Bianconi #ifdef CONFIG_PM
1411613c621SLorenzo Bianconi 	.suspend	= mt76x2u_suspend,
1421613c621SLorenzo Bianconi 	.resume		= mt76x2u_resume,
1431613c621SLorenzo Bianconi 	.reset_resume	= mt76x2u_resume,
1441613c621SLorenzo Bianconi #endif /* CONFIG_PM */
1451613c621SLorenzo Bianconi 	.soft_unbind	= 1,
1461613c621SLorenzo Bianconi 	.disable_hub_initiated_lpm = 1,
1471613c621SLorenzo Bianconi };
1481613c621SLorenzo Bianconi module_usb_driver(mt76x2u_driver);
1491613c621SLorenzo Bianconi 
1501613c621SLorenzo Bianconi MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>");
151f3f8f050SBreno Leitao MODULE_DESCRIPTION("MediaTek MT76x2U (USB) wireless driver");
1521613c621SLorenzo Bianconi MODULE_LICENSE("Dual BSD/GPL");
153