1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2019 MediaTek Inc.
3  *
4  * Author: Felix Fietkau <nbd@nbd.name>
5  *	   Lorenzo Bianconi <lorenzo@kernel.org>
6  *	   Sean Wang <sean.wang@mediatek.com>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 
13 #include "mt7615.h"
14 #include "mac.h"
15 #include "mcu.h"
16 #include "regs.h"
17 
18 static const struct usb_device_id mt7615_device_table[] = {
19 	{ USB_DEVICE_AND_INTERFACE_INFO(0x0e8d, 0x7663, 0xff, 0xff, 0xff) },
20 	{ },
21 };
22 
23 static void mt7663u_stop(struct ieee80211_hw *hw)
24 {
25 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
26 	struct mt7615_dev *dev = hw->priv;
27 
28 	clear_bit(MT76_STATE_RUNNING, &dev->mphy.state);
29 	del_timer_sync(&phy->roc_timer);
30 	cancel_work_sync(&phy->roc_work);
31 	cancel_delayed_work_sync(&phy->scan_work);
32 	cancel_delayed_work_sync(&phy->mt76->mac_work);
33 	mt76u_stop_tx(&dev->mt76);
34 }
35 
36 static void mt7663u_cleanup(struct mt7615_dev *dev)
37 {
38 	clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
39 	mt76u_queues_deinit(&dev->mt76);
40 }
41 
42 static void mt7663u_init_work(struct work_struct *work)
43 {
44 	struct mt7615_dev *dev;
45 
46 	dev = container_of(work, struct mt7615_dev, mcu_work);
47 	if (mt7663u_mcu_init(dev))
48 		return;
49 
50 	mt7615_init_work(dev);
51 }
52 
53 static int mt7663u_probe(struct usb_interface *usb_intf,
54 			 const struct usb_device_id *id)
55 {
56 	static const struct mt76_driver_ops drv_ops = {
57 		.txwi_size = MT_USB_TXD_SIZE,
58 		.drv_flags = MT_DRV_RX_DMA_HDR | MT_DRV_HW_MGMT_TXQ,
59 		.tx_prepare_skb = mt7663_usb_sdio_tx_prepare_skb,
60 		.tx_complete_skb = mt7663_usb_sdio_tx_complete_skb,
61 		.tx_status_data = mt7663_usb_sdio_tx_status_data,
62 		.rx_skb = mt7615_queue_rx_skb,
63 		.sta_ps = mt7615_sta_ps,
64 		.sta_add = mt7615_mac_sta_add,
65 		.sta_remove = mt7615_mac_sta_remove,
66 		.update_survey = mt7615_update_channel,
67 	};
68 	struct usb_device *udev = interface_to_usbdev(usb_intf);
69 	struct ieee80211_ops *ops;
70 	struct mt7615_dev *dev;
71 	struct mt76_dev *mdev;
72 	int ret;
73 
74 	ops = devm_kmemdup(&usb_intf->dev, &mt7615_ops, sizeof(mt7615_ops),
75 			   GFP_KERNEL);
76 	if (!ops)
77 		return -ENOMEM;
78 
79 	ops->stop = mt7663u_stop;
80 
81 	mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), ops, &drv_ops);
82 	if (!mdev)
83 		return -ENOMEM;
84 
85 	dev = container_of(mdev, struct mt7615_dev, mt76);
86 	udev = usb_get_dev(udev);
87 	usb_reset_device(udev);
88 
89 	usb_set_intfdata(usb_intf, dev);
90 
91 	INIT_WORK(&dev->mcu_work, mt7663u_init_work);
92 	dev->reg_map = mt7663_usb_sdio_reg_map;
93 	dev->ops = ops;
94 	ret = mt76u_init(mdev, usb_intf, true);
95 	if (ret < 0)
96 		goto error;
97 
98 	mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
99 		    (mt76_rr(dev, MT_HW_REV) & 0xff);
100 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
101 
102 	if (mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_PWR_ON,
103 			   FW_STATE_PWR_ON << 1, 500)) {
104 		dev_dbg(dev->mt76.dev, "Usb device already powered on\n");
105 		set_bit(MT76_STATE_POWER_OFF, &dev->mphy.state);
106 		goto alloc_queues;
107 	}
108 
109 	ret = mt76u_vendor_request(&dev->mt76, MT_VEND_POWER_ON,
110 				   USB_DIR_OUT | USB_TYPE_VENDOR,
111 				   0x0, 0x1, NULL, 0);
112 	if (ret)
113 		goto error;
114 
115 	if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_PWR_ON,
116 			    FW_STATE_PWR_ON << 1, 500)) {
117 		dev_err(dev->mt76.dev, "Timeout for power on\n");
118 		ret = -EIO;
119 		goto error;
120 	}
121 
122 alloc_queues:
123 	ret = mt76u_alloc_mcu_queue(&dev->mt76);
124 	if (ret)
125 		goto error;
126 
127 	ret = mt76u_alloc_queues(&dev->mt76);
128 	if (ret)
129 		goto error;
130 
131 	ret = mt7663_usb_sdio_register_device(dev);
132 	if (ret)
133 		goto error;
134 
135 	return 0;
136 
137 error:
138 	mt76u_queues_deinit(&dev->mt76);
139 	usb_set_intfdata(usb_intf, NULL);
140 	usb_put_dev(interface_to_usbdev(usb_intf));
141 
142 	mt76_free_device(&dev->mt76);
143 
144 	return ret;
145 }
146 
147 static void mt7663u_disconnect(struct usb_interface *usb_intf)
148 {
149 	struct mt7615_dev *dev = usb_get_intfdata(usb_intf);
150 
151 	if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
152 		return;
153 
154 	ieee80211_unregister_hw(dev->mt76.hw);
155 	mt7663u_cleanup(dev);
156 
157 	usb_set_intfdata(usb_intf, NULL);
158 	usb_put_dev(interface_to_usbdev(usb_intf));
159 
160 	mt76_free_device(&dev->mt76);
161 }
162 
163 #ifdef CONFIG_PM
164 static int mt7663u_suspend(struct usb_interface *intf, pm_message_t state)
165 {
166 	struct mt7615_dev *dev = usb_get_intfdata(intf);
167 
168 	if (!test_bit(MT76_STATE_SUSPEND, &dev->mphy.state) &&
169 	    mt7615_firmware_offload(dev)) {
170 		int err;
171 
172 		err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
173 		if (err < 0)
174 			return err;
175 	}
176 
177 	mt76u_stop_rx(&dev->mt76);
178 	mt76u_stop_tx(&dev->mt76);
179 
180 	return 0;
181 }
182 
183 static int mt7663u_resume(struct usb_interface *intf)
184 {
185 	struct mt7615_dev *dev = usb_get_intfdata(intf);
186 	int err;
187 
188 	err = mt76u_vendor_request(&dev->mt76, MT_VEND_FEATURE_SET,
189 				   USB_DIR_OUT | USB_TYPE_VENDOR,
190 				   0x5, 0x0, NULL, 0);
191 	if (err)
192 		return err;
193 
194 	err = mt76u_resume_rx(&dev->mt76);
195 	if (err < 0)
196 		return err;
197 
198 	if (!test_bit(MT76_STATE_SUSPEND, &dev->mphy.state) &&
199 	    mt7615_firmware_offload(dev))
200 		err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
201 
202 	return err;
203 }
204 #endif /* CONFIG_PM */
205 
206 MODULE_DEVICE_TABLE(usb, mt7615_device_table);
207 MODULE_FIRMWARE(MT7663_OFFLOAD_FIRMWARE_N9);
208 MODULE_FIRMWARE(MT7663_OFFLOAD_ROM_PATCH);
209 MODULE_FIRMWARE(MT7663_FIRMWARE_N9);
210 MODULE_FIRMWARE(MT7663_ROM_PATCH);
211 
212 static struct usb_driver mt7663u_driver = {
213 	.name		= KBUILD_MODNAME,
214 	.id_table	= mt7615_device_table,
215 	.probe		= mt7663u_probe,
216 	.disconnect	= mt7663u_disconnect,
217 #ifdef CONFIG_PM
218 	.suspend	= mt7663u_suspend,
219 	.resume		= mt7663u_resume,
220 	.reset_resume	= mt7663u_resume,
221 #endif /* CONFIG_PM */
222 	.soft_unbind	= 1,
223 	.disable_hub_initiated_lpm = 1,
224 };
225 module_usb_driver(mt7663u_driver);
226 
227 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
228 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
229 MODULE_LICENSE("Dual BSD/GPL");
230