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 "mt7601u.h" 19 #include "usb.h" 20 #include "trace.h" 21 22 static const struct usb_device_id mt7601u_device_table[] = { 23 { USB_DEVICE(0x0b05, 0x17d3) }, 24 { USB_DEVICE(0x0e8d, 0x760a) }, 25 { USB_DEVICE(0x0e8d, 0x760b) }, 26 { USB_DEVICE(0x13d3, 0x3431) }, 27 { USB_DEVICE(0x13d3, 0x3434) }, 28 { USB_DEVICE(0x148f, 0x7601) }, 29 { USB_DEVICE(0x148f, 0x760a) }, 30 { USB_DEVICE(0x148f, 0x760b) }, 31 { USB_DEVICE(0x148f, 0x760c) }, 32 { USB_DEVICE(0x148f, 0x760d) }, 33 { USB_DEVICE(0x2001, 0x3d04) }, 34 { USB_DEVICE(0x2717, 0x4106) }, 35 { USB_DEVICE(0x2955, 0x0001) }, 36 { USB_DEVICE(0x2955, 0x1001) }, 37 { USB_DEVICE(0x2a5f, 0x1000) }, 38 { USB_DEVICE(0x7392, 0x7710) }, 39 { 0, } 40 }; 41 42 bool mt7601u_usb_alloc_buf(struct mt7601u_dev *dev, size_t len, 43 struct mt7601u_dma_buf *buf) 44 { 45 struct usb_device *usb_dev = mt7601u_to_usb_dev(dev); 46 47 buf->len = len; 48 buf->urb = usb_alloc_urb(0, GFP_KERNEL); 49 buf->buf = usb_alloc_coherent(usb_dev, buf->len, GFP_KERNEL, &buf->dma); 50 51 return !buf->urb || !buf->buf; 52 } 53 54 void mt7601u_usb_free_buf(struct mt7601u_dev *dev, struct mt7601u_dma_buf *buf) 55 { 56 struct usb_device *usb_dev = mt7601u_to_usb_dev(dev); 57 58 usb_free_coherent(usb_dev, buf->len, buf->buf, buf->dma); 59 usb_free_urb(buf->urb); 60 } 61 62 int mt7601u_usb_submit_buf(struct mt7601u_dev *dev, int dir, int ep_idx, 63 struct mt7601u_dma_buf *buf, gfp_t gfp, 64 usb_complete_t complete_fn, void *context) 65 { 66 struct usb_device *usb_dev = mt7601u_to_usb_dev(dev); 67 unsigned pipe; 68 int ret; 69 70 if (dir == USB_DIR_IN) 71 pipe = usb_rcvbulkpipe(usb_dev, dev->in_eps[ep_idx]); 72 else 73 pipe = usb_sndbulkpipe(usb_dev, dev->out_eps[ep_idx]); 74 75 usb_fill_bulk_urb(buf->urb, usb_dev, pipe, buf->buf, buf->len, 76 complete_fn, context); 77 buf->urb->transfer_dma = buf->dma; 78 buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 79 80 trace_mt_submit_urb(dev, buf->urb); 81 ret = usb_submit_urb(buf->urb, gfp); 82 if (ret) 83 dev_err(dev->dev, "Error: submit URB dir:%d ep:%d failed:%d\n", 84 dir, ep_idx, ret); 85 return ret; 86 } 87 88 void mt7601u_complete_urb(struct urb *urb) 89 { 90 struct completion *cmpl = urb->context; 91 92 complete(cmpl); 93 } 94 95 int mt7601u_vendor_request(struct mt7601u_dev *dev, const u8 req, 96 const u8 direction, const u16 val, const u16 offset, 97 void *buf, const size_t buflen) 98 { 99 int i, ret; 100 struct usb_device *usb_dev = mt7601u_to_usb_dev(dev); 101 const u8 req_type = direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE; 102 const unsigned int pipe = (direction == USB_DIR_IN) ? 103 usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0); 104 105 for (i = 0; i < MT_VEND_REQ_MAX_RETRY; i++) { 106 ret = usb_control_msg(usb_dev, pipe, req, req_type, 107 val, offset, buf, buflen, 108 MT_VEND_REQ_TOUT_MS); 109 trace_mt_vend_req(dev, pipe, req, req_type, val, offset, 110 buf, buflen, ret); 111 112 if (ret == -ENODEV) 113 set_bit(MT7601U_STATE_REMOVED, &dev->state); 114 if (ret >= 0 || ret == -ENODEV) 115 return ret; 116 117 msleep(5); 118 } 119 120 dev_err(dev->dev, "Vendor request req:%02x off:%04x failed:%d\n", 121 req, offset, ret); 122 123 return ret; 124 } 125 126 void mt7601u_vendor_reset(struct mt7601u_dev *dev) 127 { 128 mt7601u_vendor_request(dev, MT_VEND_DEV_MODE, USB_DIR_OUT, 129 MT_VEND_DEV_MODE_RESET, 0, NULL, 0); 130 } 131 132 /* should be called with vendor_req_mutex held */ 133 static u32 __mt7601u_rr(struct mt7601u_dev *dev, u32 offset) 134 { 135 int ret; 136 u32 val = ~0; 137 138 WARN_ONCE(offset > USHRT_MAX, "read high off:%08x", offset); 139 140 ret = mt7601u_vendor_request(dev, MT_VEND_MULTI_READ, USB_DIR_IN, 141 0, offset, dev->vend_buf, MT_VEND_BUF); 142 if (ret == MT_VEND_BUF) 143 val = get_unaligned_le32(dev->vend_buf); 144 else if (ret > 0) 145 dev_err(dev->dev, "Error: wrong size read:%d off:%08x\n", 146 ret, offset); 147 148 trace_reg_read(dev, offset, val); 149 return val; 150 } 151 152 u32 mt7601u_rr(struct mt7601u_dev *dev, u32 offset) 153 { 154 u32 ret; 155 156 mutex_lock(&dev->vendor_req_mutex); 157 ret = __mt7601u_rr(dev, offset); 158 mutex_unlock(&dev->vendor_req_mutex); 159 160 return ret; 161 } 162 163 /* should be called with vendor_req_mutex held */ 164 static int __mt7601u_vendor_single_wr(struct mt7601u_dev *dev, const u8 req, 165 const u16 offset, const u32 val) 166 { 167 int ret = mt7601u_vendor_request(dev, req, USB_DIR_OUT, 168 val & 0xffff, offset, NULL, 0); 169 if (!ret) 170 ret = mt7601u_vendor_request(dev, req, USB_DIR_OUT, 171 val >> 16, offset + 2, NULL, 0); 172 trace_reg_write(dev, offset, val); 173 return ret; 174 } 175 176 int mt7601u_vendor_single_wr(struct mt7601u_dev *dev, const u8 req, 177 const u16 offset, const u32 val) 178 { 179 int ret; 180 181 mutex_lock(&dev->vendor_req_mutex); 182 ret = __mt7601u_vendor_single_wr(dev, req, offset, val); 183 mutex_unlock(&dev->vendor_req_mutex); 184 185 return ret; 186 } 187 188 void mt7601u_wr(struct mt7601u_dev *dev, u32 offset, u32 val) 189 { 190 WARN_ONCE(offset > USHRT_MAX, "write high off:%08x", offset); 191 192 mt7601u_vendor_single_wr(dev, MT_VEND_WRITE, offset, val); 193 } 194 195 u32 mt7601u_rmw(struct mt7601u_dev *dev, u32 offset, u32 mask, u32 val) 196 { 197 mutex_lock(&dev->vendor_req_mutex); 198 val |= __mt7601u_rr(dev, offset) & ~mask; 199 __mt7601u_vendor_single_wr(dev, MT_VEND_WRITE, offset, val); 200 mutex_unlock(&dev->vendor_req_mutex); 201 202 return val; 203 } 204 205 u32 mt7601u_rmc(struct mt7601u_dev *dev, u32 offset, u32 mask, u32 val) 206 { 207 u32 reg; 208 209 mutex_lock(&dev->vendor_req_mutex); 210 reg = __mt7601u_rr(dev, offset); 211 val |= reg & ~mask; 212 if (reg != val) 213 __mt7601u_vendor_single_wr(dev, MT_VEND_WRITE, 214 offset, val); 215 mutex_unlock(&dev->vendor_req_mutex); 216 217 return val; 218 } 219 220 void mt7601u_wr_copy(struct mt7601u_dev *dev, u32 offset, 221 const void *data, int len) 222 { 223 WARN_ONCE(offset & 3, "unaligned write copy off:%08x", offset); 224 WARN_ONCE(len & 3, "short write copy off:%08x", offset); 225 226 mt7601u_burst_write_regs(dev, offset, data, len / 4); 227 } 228 229 void mt7601u_addr_wr(struct mt7601u_dev *dev, const u32 offset, const u8 *addr) 230 { 231 mt7601u_wr(dev, offset, get_unaligned_le32(addr)); 232 mt7601u_wr(dev, offset + 4, addr[4] | addr[5] << 8); 233 } 234 235 static int mt7601u_assign_pipes(struct usb_interface *usb_intf, 236 struct mt7601u_dev *dev) 237 { 238 struct usb_endpoint_descriptor *ep_desc; 239 struct usb_host_interface *intf_desc = usb_intf->cur_altsetting; 240 unsigned i, ep_i = 0, ep_o = 0; 241 242 BUILD_BUG_ON(sizeof(dev->in_eps) < __MT_EP_IN_MAX); 243 BUILD_BUG_ON(sizeof(dev->out_eps) < __MT_EP_OUT_MAX); 244 245 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) { 246 ep_desc = &intf_desc->endpoint[i].desc; 247 248 if (usb_endpoint_is_bulk_in(ep_desc) && 249 ep_i++ < __MT_EP_IN_MAX) { 250 dev->in_eps[ep_i - 1] = usb_endpoint_num(ep_desc); 251 dev->in_max_packet = usb_endpoint_maxp(ep_desc); 252 /* Note: this is ignored by usb sub-system but vendor 253 * code does it. We can drop this at some point. 254 */ 255 dev->in_eps[ep_i - 1] |= USB_DIR_IN; 256 } else if (usb_endpoint_is_bulk_out(ep_desc) && 257 ep_o++ < __MT_EP_OUT_MAX) { 258 dev->out_eps[ep_o - 1] = usb_endpoint_num(ep_desc); 259 dev->out_max_packet = usb_endpoint_maxp(ep_desc); 260 } 261 } 262 263 if (ep_i != __MT_EP_IN_MAX || ep_o != __MT_EP_OUT_MAX) { 264 dev_err(dev->dev, "Error: wrong pipe number in:%d out:%d\n", 265 ep_i, ep_o); 266 return -EINVAL; 267 } 268 269 return 0; 270 } 271 272 static int mt7601u_probe(struct usb_interface *usb_intf, 273 const struct usb_device_id *id) 274 { 275 struct usb_device *usb_dev = interface_to_usbdev(usb_intf); 276 struct mt7601u_dev *dev; 277 u32 asic_rev, mac_rev; 278 int ret; 279 280 dev = mt7601u_alloc_device(&usb_intf->dev); 281 if (!dev) 282 return -ENOMEM; 283 284 usb_dev = usb_get_dev(usb_dev); 285 usb_reset_device(usb_dev); 286 287 usb_set_intfdata(usb_intf, dev); 288 289 dev->vend_buf = devm_kmalloc(dev->dev, MT_VEND_BUF, GFP_KERNEL); 290 if (!dev->vend_buf) { 291 ret = -ENOMEM; 292 goto err; 293 } 294 295 ret = mt7601u_assign_pipes(usb_intf, dev); 296 if (ret) 297 goto err; 298 ret = mt7601u_wait_asic_ready(dev); 299 if (ret) 300 goto err; 301 302 asic_rev = mt7601u_rr(dev, MT_ASIC_VERSION); 303 mac_rev = mt7601u_rr(dev, MT_MAC_CSR0); 304 dev_info(dev->dev, "ASIC revision: %08x MAC revision: %08x\n", 305 asic_rev, mac_rev); 306 307 /* Note: vendor driver skips this check for MT7601U */ 308 if (!(mt7601u_rr(dev, MT_EFUSE_CTRL) & MT_EFUSE_CTRL_SEL)) 309 dev_warn(dev->dev, "Warning: eFUSE not present\n"); 310 311 ret = mt7601u_init_hardware(dev); 312 if (ret) 313 goto err; 314 ret = mt7601u_register_device(dev); 315 if (ret) 316 goto err_hw; 317 318 set_bit(MT7601U_STATE_INITIALIZED, &dev->state); 319 320 return 0; 321 err_hw: 322 mt7601u_cleanup(dev); 323 err: 324 usb_set_intfdata(usb_intf, NULL); 325 usb_put_dev(interface_to_usbdev(usb_intf)); 326 327 destroy_workqueue(dev->stat_wq); 328 ieee80211_free_hw(dev->hw); 329 return ret; 330 } 331 332 static void mt7601u_disconnect(struct usb_interface *usb_intf) 333 { 334 struct mt7601u_dev *dev = usb_get_intfdata(usb_intf); 335 336 ieee80211_unregister_hw(dev->hw); 337 mt7601u_cleanup(dev); 338 339 usb_set_intfdata(usb_intf, NULL); 340 usb_put_dev(interface_to_usbdev(usb_intf)); 341 342 destroy_workqueue(dev->stat_wq); 343 ieee80211_free_hw(dev->hw); 344 } 345 346 static int mt7601u_suspend(struct usb_interface *usb_intf, pm_message_t state) 347 { 348 struct mt7601u_dev *dev = usb_get_intfdata(usb_intf); 349 350 mt7601u_cleanup(dev); 351 352 return 0; 353 } 354 355 static int mt7601u_resume(struct usb_interface *usb_intf) 356 { 357 struct mt7601u_dev *dev = usb_get_intfdata(usb_intf); 358 int ret; 359 360 ret = mt7601u_init_hardware(dev); 361 if (ret) 362 return ret; 363 364 set_bit(MT7601U_STATE_INITIALIZED, &dev->state); 365 366 return 0; 367 } 368 369 MODULE_DEVICE_TABLE(usb, mt7601u_device_table); 370 MODULE_FIRMWARE(MT7601U_FIRMWARE); 371 MODULE_LICENSE("GPL"); 372 373 static struct usb_driver mt7601u_driver = { 374 .name = KBUILD_MODNAME, 375 .id_table = mt7601u_device_table, 376 .probe = mt7601u_probe, 377 .disconnect = mt7601u_disconnect, 378 .suspend = mt7601u_suspend, 379 .resume = mt7601u_resume, 380 .reset_resume = mt7601u_resume, 381 .soft_unbind = 1, 382 .disable_hub_initiated_lpm = 1, 383 }; 384 module_usb_driver(mt7601u_driver); 385