1 /*
2  * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 
18 #include "mt76x0.h"
19 #include "usb.h"
20 #include "trace.h"
21 
22 static struct usb_device_id mt76x0_device_table[] = {
23 	{ USB_DEVICE(0x148F, 0x7610) },	/* MT7610U */
24 	{ USB_DEVICE(0x13B1, 0x003E) },	/* Linksys AE6000 */
25 	{ USB_DEVICE(0x0E8D, 0x7610) },	/* Sabrent NTWLAC */
26 	{ USB_DEVICE(0x7392, 0xa711) },	/* Edimax 7711mac */
27 	{ USB_DEVICE(0x7392, 0xb711) },	/* Edimax / Elecom  */
28 	{ USB_DEVICE(0x148f, 0x761a) },	/* TP-Link TL-WDN5200 */
29 	{ USB_DEVICE(0x148f, 0x760a) },	/* TP-Link unknown */
30 	{ USB_DEVICE(0x0b05, 0x17d1) },	/* Asus USB-AC51 */
31 	{ USB_DEVICE(0x0b05, 0x17db) },	/* Asus USB-AC50 */
32 	{ USB_DEVICE(0x0df6, 0x0075) },	/* Sitecom WLA-3100 */
33 	{ USB_DEVICE(0x2019, 0xab31) },	/* Planex GW-450D */
34 	{ USB_DEVICE(0x2001, 0x3d02) },	/* D-LINK DWA-171 rev B1 */
35 	{ USB_DEVICE(0x0586, 0x3425) },	/* Zyxel NWD6505 */
36 	{ USB_DEVICE(0x07b8, 0x7610) },	/* AboCom AU7212 */
37 	{ USB_DEVICE(0x04bb, 0x0951) },	/* I-O DATA WN-AC433UK */
38 	{ USB_DEVICE(0x057c, 0x8502) },	/* AVM FRITZ!WLAN USB Stick AC 430 */
39 	{ USB_DEVICE(0x293c, 0x5702) },	/* Comcast Xfinity KXW02AAA  */
40 	{ USB_DEVICE(0x20f4, 0x806b) },	/* TRENDnet TEW-806UBH  */
41 	{ USB_DEVICE(0x7392, 0xc711) }, /* Devolo Wifi ac Stick */
42 	{ USB_DEVICE(0x0df6, 0x0079) }, /* Sitecom Europe B.V. ac  Stick */
43 	{ USB_DEVICE(0x2357, 0x0105) }, /* TP-LINK Archer T1U */
44 	{ USB_DEVICE_AND_INTERFACE_INFO(0x0E8D, 0x7630, 0xff, 0x2, 0xff)}, /* MT7630U */
45 	{ USB_DEVICE_AND_INTERFACE_INFO(0x0E8D, 0x7650, 0xff, 0x2, 0xff)}, /* MT7650U */
46 	{ 0, }
47 };
48 
mt76x0_usb_alloc_buf(struct mt76x0_dev * dev,size_t len,struct mt76x0_dma_buf * buf)49 bool mt76x0_usb_alloc_buf(struct mt76x0_dev *dev, size_t len,
50 			   struct mt76x0_dma_buf *buf)
51 {
52 	struct usb_device *usb_dev = mt76x0_to_usb_dev(dev);
53 
54 	buf->len = len;
55 	buf->urb = usb_alloc_urb(0, GFP_KERNEL);
56 	buf->buf = usb_alloc_coherent(usb_dev, buf->len, GFP_KERNEL, &buf->dma);
57 
58 	return !buf->urb || !buf->buf;
59 }
60 
mt76x0_usb_free_buf(struct mt76x0_dev * dev,struct mt76x0_dma_buf * buf)61 void mt76x0_usb_free_buf(struct mt76x0_dev *dev, struct mt76x0_dma_buf *buf)
62 {
63 	struct usb_device *usb_dev = mt76x0_to_usb_dev(dev);
64 
65 	usb_free_coherent(usb_dev, buf->len, buf->buf, buf->dma);
66 	usb_free_urb(buf->urb);
67 }
68 
mt76x0_usb_submit_buf(struct mt76x0_dev * dev,int dir,int ep_idx,struct mt76x0_dma_buf * buf,gfp_t gfp,usb_complete_t complete_fn,void * context)69 int mt76x0_usb_submit_buf(struct mt76x0_dev *dev, int dir, int ep_idx,
70 			   struct mt76x0_dma_buf *buf, gfp_t gfp,
71 			   usb_complete_t complete_fn, void *context)
72 {
73 	struct usb_device *usb_dev = mt76x0_to_usb_dev(dev);
74 	unsigned pipe;
75 	int ret;
76 
77 	if (dir == USB_DIR_IN)
78 		pipe = usb_rcvbulkpipe(usb_dev, dev->in_ep[ep_idx]);
79 	else
80 		pipe = usb_sndbulkpipe(usb_dev, dev->out_ep[ep_idx]);
81 
82 	usb_fill_bulk_urb(buf->urb, usb_dev, pipe, buf->buf, buf->len,
83 			  complete_fn, context);
84 	buf->urb->transfer_dma = buf->dma;
85 	buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
86 
87 	trace_mt76x0_submit_urb(&dev->mt76, buf->urb);
88 	ret = usb_submit_urb(buf->urb, gfp);
89 	if (ret)
90 		dev_err(dev->mt76.dev, "Error: submit URB dir:%d ep:%d failed:%d\n",
91 			dir, ep_idx, ret);
92 	return ret;
93 }
94 
mt76x0_complete_urb(struct urb * urb)95 void mt76x0_complete_urb(struct urb *urb)
96 {
97 	struct completion *cmpl = urb->context;
98 
99 	complete(cmpl);
100 }
101 
mt76x0_vendor_request(struct mt76x0_dev * dev,const u8 req,const u8 direction,const u16 val,const u16 offset,void * buf,const size_t buflen)102 int mt76x0_vendor_request(struct mt76x0_dev *dev, const u8 req,
103 			   const u8 direction, const u16 val, const u16 offset,
104 			   void *buf, const size_t buflen)
105 {
106 	int i, ret;
107 	struct usb_device *usb_dev = mt76x0_to_usb_dev(dev);
108 	const u8 req_type = direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
109 	const unsigned int pipe = (direction == USB_DIR_IN) ?
110 		usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
111 
112 	for (i = 0; i < MT_VEND_REQ_MAX_RETRY; i++) {
113 		ret = usb_control_msg(usb_dev, pipe, req, req_type,
114 				      val, offset, buf, buflen,
115 				      MT_VEND_REQ_TOUT_MS);
116 		trace_mt76x0_vend_req(&dev->mt76, pipe, req, req_type, val, offset,
117 				  buf, buflen, ret);
118 
119 		if (ret == -ENODEV)
120 			set_bit(MT76_REMOVED, &dev->mt76.state);
121 		if (ret >= 0 || ret == -ENODEV)
122 			return ret;
123 
124 		msleep(5);
125 	}
126 
127 	dev_err(dev->mt76.dev, "Vendor request req:%02x off:%04x failed:%d\n",
128 		req, offset, ret);
129 
130 	return ret;
131 }
132 
mt76x0_vendor_reset(struct mt76x0_dev * dev)133 void mt76x0_vendor_reset(struct mt76x0_dev *dev)
134 {
135 	mt76x0_vendor_request(dev, MT_VEND_DEV_MODE, USB_DIR_OUT,
136 			      MT_VEND_DEV_MODE_RESET, 0, NULL, 0);
137 }
138 
mt76x0_rr(struct mt76_dev * dev,u32 offset)139 static u32 mt76x0_rr(struct mt76_dev *dev, u32 offset)
140 {
141 	struct mt76x0_dev *mdev = (struct mt76x0_dev *) dev;
142 	int ret;
143 	u32 val = ~0;
144 
145 	WARN_ONCE(offset > USHRT_MAX, "read high off:%08x", offset);
146 
147 	mutex_lock(&mdev->usb_ctrl_mtx);
148 
149 	ret = mt76x0_vendor_request((struct mt76x0_dev *)dev, MT_VEND_MULTI_READ, USB_DIR_IN,
150 				    0, offset, mdev->data, MT_VEND_BUF);
151 	if (ret == MT_VEND_BUF)
152 		val = get_unaligned_le32(mdev->data);
153 	else if (ret > 0)
154 		dev_err(dev->dev, "Error: wrong size read:%d off:%08x\n",
155 			ret, offset);
156 
157 	mutex_unlock(&mdev->usb_ctrl_mtx);
158 
159 	trace_mt76x0_reg_read(dev, offset, val);
160 	return val;
161 }
162 
mt76x0_vendor_single_wr(struct mt76x0_dev * dev,const u8 req,const u16 offset,const u32 val)163 int mt76x0_vendor_single_wr(struct mt76x0_dev *dev, const u8 req,
164 			     const u16 offset, const u32 val)
165 {
166 	struct mt76x0_dev *mdev = dev;
167 	int ret;
168 
169 	mutex_lock(&mdev->usb_ctrl_mtx);
170 
171 	ret = mt76x0_vendor_request(dev, req, USB_DIR_OUT,
172 				    val & 0xffff, offset, NULL, 0);
173 	if (!ret)
174 		ret = mt76x0_vendor_request(dev, req, USB_DIR_OUT,
175 					    val >> 16, offset + 2, NULL, 0);
176 
177 	mutex_unlock(&mdev->usb_ctrl_mtx);
178 
179 	return ret;
180 }
181 
mt76x0_wr(struct mt76_dev * dev,u32 offset,u32 val)182 static void mt76x0_wr(struct mt76_dev *dev, u32 offset, u32 val)
183 {
184 	struct mt76x0_dev *mdev = (struct mt76x0_dev *) dev;
185 	int ret;
186 
187 	WARN_ONCE(offset > USHRT_MAX, "write high off:%08x", offset);
188 
189 	mutex_lock(&mdev->usb_ctrl_mtx);
190 
191 	put_unaligned_le32(val, mdev->data);
192 	ret = mt76x0_vendor_request(mdev, MT_VEND_MULTI_WRITE, USB_DIR_OUT,
193 				    0, offset, mdev->data, MT_VEND_BUF);
194 	trace_mt76x0_reg_write(dev, offset, val);
195 
196 	mutex_unlock(&mdev->usb_ctrl_mtx);
197 }
198 
mt76x0_rmw(struct mt76_dev * dev,u32 offset,u32 mask,u32 val)199 static u32 mt76x0_rmw(struct mt76_dev *dev, u32 offset, u32 mask, u32 val)
200 {
201 	val |= mt76x0_rr(dev, offset) & ~mask;
202 	mt76x0_wr(dev, offset, val);
203 	return val;
204 }
205 
mt76x0_wr_copy(struct mt76_dev * dev,u32 offset,const void * data,int len)206 static void mt76x0_wr_copy(struct mt76_dev *dev, u32 offset,
207 			   const void *data, int len)
208 {
209 	WARN_ONCE(offset & 3, "unaligned write copy off:%08x", offset);
210 	WARN_ONCE(len & 3, "short write copy off:%08x", offset);
211 
212 	mt76x0_burst_write_regs((struct mt76x0_dev *) dev, offset, data, len / 4);
213 }
214 
mt76x0_addr_wr(struct mt76x0_dev * dev,const u32 offset,const u8 * addr)215 void mt76x0_addr_wr(struct mt76x0_dev *dev, const u32 offset, const u8 *addr)
216 {
217 	mt76_wr(dev, offset, get_unaligned_le32(addr));
218 	mt76_wr(dev, offset + 4, addr[4] | addr[5] << 8);
219 }
220 
mt76x0_assign_pipes(struct usb_interface * usb_intf,struct mt76x0_dev * dev)221 static int mt76x0_assign_pipes(struct usb_interface *usb_intf,
222 				struct mt76x0_dev *dev)
223 {
224 	struct usb_endpoint_descriptor *ep_desc;
225 	struct usb_host_interface *intf_desc = usb_intf->cur_altsetting;
226 	unsigned i, ep_i = 0, ep_o = 0;
227 
228 	BUILD_BUG_ON(sizeof(dev->in_ep) < __MT_EP_IN_MAX);
229 	BUILD_BUG_ON(sizeof(dev->out_ep) < __MT_EP_OUT_MAX);
230 
231 	for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
232 		ep_desc = &intf_desc->endpoint[i].desc;
233 
234 		if (usb_endpoint_is_bulk_in(ep_desc) &&
235 		    ep_i++ < __MT_EP_IN_MAX) {
236 			dev->in_ep[ep_i - 1] = usb_endpoint_num(ep_desc);
237 			dev->in_max_packet = usb_endpoint_maxp(ep_desc);
238 			/* Note: this is ignored by usb sub-system but vendor
239 			 *	 code does it. We can drop this at some point.
240 			 */
241 			dev->in_ep[ep_i - 1] |= USB_DIR_IN;
242 		} else if (usb_endpoint_is_bulk_out(ep_desc) &&
243 			   ep_o++ < __MT_EP_OUT_MAX) {
244 			dev->out_ep[ep_o - 1] = usb_endpoint_num(ep_desc);
245 			dev->out_max_packet = usb_endpoint_maxp(ep_desc);
246 		}
247 	}
248 
249 	if (ep_i != __MT_EP_IN_MAX || ep_o != __MT_EP_OUT_MAX) {
250 		dev_err(dev->mt76.dev, "Error: wrong pipe number in:%d out:%d\n",
251 			ep_i, ep_o);
252 		return -EINVAL;
253 	}
254 
255 	return 0;
256 }
257 
mt76x0_probe(struct usb_interface * usb_intf,const struct usb_device_id * id)258 static int mt76x0_probe(struct usb_interface *usb_intf,
259 			 const struct usb_device_id *id)
260 {
261 	struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
262 	struct mt76x0_dev *dev;
263 	u32 asic_rev, mac_rev;
264 	int ret;
265 	static const struct mt76_bus_ops usb_ops = {
266 		.rr = mt76x0_rr,
267 		.wr = mt76x0_wr,
268 		.rmw = mt76x0_rmw,
269 		.copy = mt76x0_wr_copy,
270 	};
271 
272 	dev = mt76x0_alloc_device(&usb_intf->dev);
273 	if (!dev)
274 		return -ENOMEM;
275 
276 	usb_dev = usb_get_dev(usb_dev);
277 	usb_reset_device(usb_dev);
278 
279 	usb_set_intfdata(usb_intf, dev);
280 
281 	dev->mt76.bus = &usb_ops;
282 
283 	ret = mt76x0_assign_pipes(usb_intf, dev);
284 	if (ret)
285 		goto err;
286 
287 	/* Disable the HW, otherwise MCU fail to initalize on hot reboot */
288 	mt76x0_chip_onoff(dev, false, false);
289 
290 	ret = mt76x0_wait_asic_ready(dev);
291 	if (ret)
292 		goto err;
293 
294 	asic_rev = mt76_rr(dev, MT_ASIC_VERSION);
295 	mac_rev = mt76_rr(dev, MT_MAC_CSR0);
296 	dev_info(dev->mt76.dev, "ASIC revision: %08x MAC revision: %08x\n",
297 		 asic_rev, mac_rev);
298 
299 	/* Note: vendor driver skips this check for MT76X0U */
300 	if (!(mt76_rr(dev, MT_EFUSE_CTRL) & MT_EFUSE_CTRL_SEL))
301 		dev_warn(dev->mt76.dev, "Warning: eFUSE not present\n");
302 
303 	ret = mt76x0_init_hardware(dev);
304 	if (ret)
305 		goto err;
306 
307 	ret = mt76x0_register_device(dev);
308 	if (ret)
309 		goto err_hw;
310 
311 	set_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
312 
313 	return 0;
314 err_hw:
315 	mt76x0_cleanup(dev);
316 err:
317 	usb_set_intfdata(usb_intf, NULL);
318 	usb_put_dev(interface_to_usbdev(usb_intf));
319 
320 	destroy_workqueue(dev->stat_wq);
321 	ieee80211_free_hw(dev->mt76.hw);
322 	return ret;
323 }
324 
mt76x0_disconnect(struct usb_interface * usb_intf)325 static void mt76x0_disconnect(struct usb_interface *usb_intf)
326 {
327 	struct mt76x0_dev *dev = usb_get_intfdata(usb_intf);
328 	bool initalized = test_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
329 
330 	if (!initalized)
331 		return;
332 
333 	ieee80211_unregister_hw(dev->mt76.hw);
334 	mt76x0_cleanup(dev);
335 
336 	usb_set_intfdata(usb_intf, NULL);
337 	usb_put_dev(interface_to_usbdev(usb_intf));
338 
339 	destroy_workqueue(dev->stat_wq);
340 	ieee80211_free_hw(dev->mt76.hw);
341 }
342 
mt76x0_suspend(struct usb_interface * usb_intf,pm_message_t state)343 static int mt76x0_suspend(struct usb_interface *usb_intf, pm_message_t state)
344 {
345 	struct mt76x0_dev *dev = usb_get_intfdata(usb_intf);
346 
347 	mt76x0_cleanup(dev);
348 
349 	return 0;
350 }
351 
mt76x0_resume(struct usb_interface * usb_intf)352 static int mt76x0_resume(struct usb_interface *usb_intf)
353 {
354 	struct mt76x0_dev *dev = usb_get_intfdata(usb_intf);
355 	int ret;
356 
357 	ret = mt76x0_init_hardware(dev);
358 	if (ret)
359 		return ret;
360 
361 	set_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
362 
363 	return 0;
364 }
365 
366 MODULE_DEVICE_TABLE(usb, mt76x0_device_table);
367 MODULE_FIRMWARE(MT7610_FIRMWARE);
368 MODULE_LICENSE("GPL");
369 
370 static struct usb_driver mt76x0_driver = {
371 	.name		= KBUILD_MODNAME,
372 	.id_table	= mt76x0_device_table,
373 	.probe		= mt76x0_probe,
374 	.disconnect	= mt76x0_disconnect,
375 	.suspend	= mt76x0_suspend,
376 	.resume		= mt76x0_resume,
377 	.reset_resume	= mt76x0_resume,
378 	.soft_unbind	= 1,
379 	.disable_hub_initiated_lpm = 1,
380 };
381 module_usb_driver(mt76x0_driver);
382