usb.c 9.3 KB

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