usb.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2012 Realtek Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * wlanfae <wlanfae@realtek.com>
  23. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  24. * Hsinchu 300, Taiwan.
  25. *
  26. *****************************************************************************/
  27. #include "wifi.h"
  28. #include "core.h"
  29. #include "usb.h"
  30. #include "base.h"
  31. #include "ps.h"
  32. #include "rtl8192c/fw_common.h"
  33. #include <linux/export.h>
  34. #define REALTEK_USB_VENQT_READ 0xC0
  35. #define REALTEK_USB_VENQT_WRITE 0x40
  36. #define REALTEK_USB_VENQT_CMD_REQ 0x05
  37. #define REALTEK_USB_VENQT_CMD_IDX 0x00
  38. #define MAX_USBCTRL_VENDORREQ_TIMES 10
  39. static void usbctrl_async_callback(struct urb *urb)
  40. {
  41. if (urb) {
  42. /* free dr */
  43. kfree(urb->setup_packet);
  44. /* free databuf */
  45. kfree(urb->transfer_buffer);
  46. }
  47. }
  48. static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
  49. u16 value, u16 index, void *pdata,
  50. u16 len)
  51. {
  52. int rc;
  53. unsigned int pipe;
  54. u8 reqtype;
  55. struct usb_ctrlrequest *dr;
  56. struct urb *urb;
  57. const u16 databuf_maxlen = REALTEK_USB_VENQT_MAX_BUF_SIZE;
  58. u8 *databuf;
  59. if (WARN_ON_ONCE(len > databuf_maxlen))
  60. len = databuf_maxlen;
  61. pipe = usb_sndctrlpipe(udev, 0); /* write_out */
  62. reqtype = REALTEK_USB_VENQT_WRITE;
  63. dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
  64. if (!dr)
  65. return -ENOMEM;
  66. databuf = kmalloc(databuf_maxlen, GFP_ATOMIC);
  67. if (!databuf) {
  68. kfree(dr);
  69. return -ENOMEM;
  70. }
  71. urb = usb_alloc_urb(0, GFP_ATOMIC);
  72. if (!urb) {
  73. kfree(databuf);
  74. kfree(dr);
  75. return -ENOMEM;
  76. }
  77. dr->bRequestType = reqtype;
  78. dr->bRequest = request;
  79. dr->wValue = cpu_to_le16(value);
  80. dr->wIndex = cpu_to_le16(index);
  81. dr->wLength = cpu_to_le16(len);
  82. /* data are already in little-endian order */
  83. memcpy(databuf, pdata, len);
  84. usb_fill_control_urb(urb, udev, pipe,
  85. (unsigned char *)dr, databuf, len,
  86. usbctrl_async_callback, NULL);
  87. rc = usb_submit_urb(urb, GFP_ATOMIC);
  88. if (rc < 0) {
  89. kfree(databuf);
  90. kfree(dr);
  91. }
  92. usb_free_urb(urb);
  93. return rc;
  94. }
  95. static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
  96. u16 value, u16 index, void *pdata,
  97. u16 len)
  98. {
  99. unsigned int pipe;
  100. int status;
  101. u8 reqtype;
  102. int vendorreq_times = 0;
  103. static int count;
  104. pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
  105. reqtype = REALTEK_USB_VENQT_READ;
  106. do {
  107. status = usb_control_msg(udev, pipe, request, reqtype, value,
  108. index, pdata, len, 0); /*max. timeout*/
  109. if (status < 0) {
  110. /* firmware download is checksumed, don't retry */
  111. if ((value >= FW_8192C_START_ADDRESS &&
  112. value <= FW_8192C_END_ADDRESS))
  113. break;
  114. } else {
  115. break;
  116. }
  117. } while (++vendorreq_times < MAX_USBCTRL_VENDORREQ_TIMES);
  118. if (status < 0 && count++ < 4)
  119. pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
  120. value, status, *(u32 *)pdata);
  121. return status;
  122. }
  123. static u32 _usb_read_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len)
  124. {
  125. struct device *dev = rtlpriv->io.dev;
  126. struct usb_device *udev = to_usb_device(dev);
  127. u8 request;
  128. u16 wvalue;
  129. u16 index;
  130. __le32 *data;
  131. unsigned long flags;
  132. spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags);
  133. if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
  134. rtlpriv->usb_data_index = 0;
  135. data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
  136. spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags);
  137. request = REALTEK_USB_VENQT_CMD_REQ;
  138. index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
  139. wvalue = (u16)addr;
  140. _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
  141. return le32_to_cpu(*data);
  142. }
  143. static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
  144. {
  145. return (u8)_usb_read_sync(rtlpriv, addr, 1);
  146. }
  147. static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
  148. {
  149. return (u16)_usb_read_sync(rtlpriv, addr, 2);
  150. }
  151. static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
  152. {
  153. return _usb_read_sync(rtlpriv, addr, 4);
  154. }
  155. static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
  156. u16 len)
  157. {
  158. u8 request;
  159. u16 wvalue;
  160. u16 index;
  161. __le32 data;
  162. request = REALTEK_USB_VENQT_CMD_REQ;
  163. index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
  164. wvalue = (u16)(addr&0x0000ffff);
  165. data = cpu_to_le32(val);
  166. _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
  167. len);
  168. }
  169. static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
  170. {
  171. struct device *dev = rtlpriv->io.dev;
  172. _usb_write_async(to_usb_device(dev), addr, val, 1);
  173. }
  174. static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
  175. {
  176. struct device *dev = rtlpriv->io.dev;
  177. _usb_write_async(to_usb_device(dev), addr, val, 2);
  178. }
  179. static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
  180. {
  181. struct device *dev = rtlpriv->io.dev;
  182. _usb_write_async(to_usb_device(dev), addr, val, 4);
  183. }
  184. static void _usb_writeN_sync(struct rtl_priv *rtlpriv, u32 addr, void *data,
  185. u16 len)
  186. {
  187. struct device *dev = rtlpriv->io.dev;
  188. struct usb_device *udev = to_usb_device(dev);
  189. u8 request = REALTEK_USB_VENQT_CMD_REQ;
  190. u8 reqtype = REALTEK_USB_VENQT_WRITE;
  191. u16 wvalue;
  192. u16 index = REALTEK_USB_VENQT_CMD_IDX;
  193. int pipe = usb_sndctrlpipe(udev, 0); /* write_out */
  194. u8 *buffer;
  195. wvalue = (u16)(addr & 0x0000ffff);
  196. buffer = kmemdup(data, len, GFP_ATOMIC);
  197. if (!buffer)
  198. return;
  199. usb_control_msg(udev, pipe, request, reqtype, wvalue,
  200. index, buffer, len, 50);
  201. kfree(buffer);
  202. }
  203. static void _rtl_usb_io_handler_init(struct device *dev,
  204. struct ieee80211_hw *hw)
  205. {
  206. struct rtl_priv *rtlpriv = rtl_priv(hw);
  207. rtlpriv->io.dev = dev;
  208. mutex_init(&rtlpriv->io.bb_mutex);
  209. rtlpriv->io.write8_async = _usb_write8_async;
  210. rtlpriv->io.write16_async = _usb_write16_async;
  211. rtlpriv->io.write32_async = _usb_write32_async;
  212. rtlpriv->io.read8_sync = _usb_read8_sync;
  213. rtlpriv->io.read16_sync = _usb_read16_sync;
  214. rtlpriv->io.read32_sync = _usb_read32_sync;
  215. rtlpriv->io.writeN_sync = _usb_writeN_sync;
  216. }
  217. static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
  218. {
  219. struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
  220. mutex_destroy(&rtlpriv->io.bb_mutex);
  221. }
  222. /**
  223. *
  224. * Default aggregation handler. Do nothing and just return the oldest skb.
  225. */
  226. static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
  227. struct sk_buff_head *list)
  228. {
  229. return skb_dequeue(list);
  230. }
  231. #define IS_HIGH_SPEED_USB(udev) \
  232. ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
  233. static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
  234. {
  235. u32 i;
  236. struct rtl_priv *rtlpriv = rtl_priv(hw);
  237. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  238. rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
  239. ? USB_HIGH_SPEED_BULK_SIZE
  240. : USB_FULL_SPEED_BULK_SIZE;
  241. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "USB Max Bulk-out Size=%d\n",
  242. rtlusb->max_bulk_out_size);
  243. for (i = 0; i < __RTL_TXQ_NUM; i++) {
  244. u32 ep_num = rtlusb->ep_map.ep_mapping[i];
  245. if (!ep_num) {
  246. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  247. "Invalid endpoint map setting!\n");
  248. return -EINVAL;
  249. }
  250. }
  251. rtlusb->usb_tx_post_hdl =
  252. rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
  253. rtlusb->usb_tx_cleanup =
  254. rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
  255. rtlusb->usb_tx_aggregate_hdl =
  256. (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
  257. ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
  258. : &_none_usb_tx_aggregate_hdl;
  259. init_usb_anchor(&rtlusb->tx_submitted);
  260. for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
  261. skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
  262. init_usb_anchor(&rtlusb->tx_pending[i]);
  263. }
  264. return 0;
  265. }
  266. static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
  267. {
  268. struct rtl_priv *rtlpriv = rtl_priv(hw);
  269. struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
  270. struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
  271. rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
  272. rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
  273. rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
  274. rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
  275. rtlusb->usb_rx_segregate_hdl =
  276. rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
  277. pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
  278. rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
  279. init_usb_anchor(&rtlusb->rx_submitted);
  280. init_usb_anchor(&rtlusb->rx_cleanup_urbs);
  281. return 0;
  282. }
  283. static int _rtl_usb_init(struct ieee80211_hw *hw)
  284. {
  285. struct rtl_priv *rtlpriv = rtl_priv(hw);
  286. struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
  287. struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
  288. int err;
  289. u8 epidx;
  290. struct usb_interface *usb_intf = rtlusb->intf;
  291. u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
  292. rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
  293. for (epidx = 0; epidx < epnums; epidx++) {
  294. struct usb_endpoint_descriptor *pep_desc;
  295. pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
  296. if (usb_endpoint_dir_in(pep_desc))
  297. rtlusb->in_ep_nums++;
  298. else if (usb_endpoint_dir_out(pep_desc))
  299. rtlusb->out_ep_nums++;
  300. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  301. "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
  302. pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
  303. pep_desc->bInterval);
  304. }
  305. if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num) {
  306. pr_err("Too few input end points found\n");
  307. return -EINVAL;
  308. }
  309. if (rtlusb->out_ep_nums == 0) {
  310. pr_err("No output end points found\n");
  311. return -EINVAL;
  312. }
  313. /* usb endpoint mapping */
  314. err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
  315. rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
  316. _rtl_usb_init_tx(hw);
  317. _rtl_usb_init_rx(hw);
  318. return err;
  319. }
  320. static void rtl_usb_init_sw(struct ieee80211_hw *hw)
  321. {
  322. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  323. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  324. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  325. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  326. rtlhal->hw = hw;
  327. ppsc->inactiveps = false;
  328. ppsc->leisure_ps = false;
  329. ppsc->fwctrl_lps = false;
  330. ppsc->reg_fwctrl_lps = 3;
  331. ppsc->reg_max_lps_awakeintvl = 5;
  332. ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
  333. /* IBSS */
  334. mac->beacon_interval = 100;
  335. /* AMPDU */
  336. mac->min_space_cfg = 0;
  337. mac->max_mss_density = 0;
  338. /* set sane AMPDU defaults */
  339. mac->current_ampdu_density = 7;
  340. mac->current_ampdu_factor = 3;
  341. /* QOS */
  342. rtlusb->acm_method = eAcmWay2_SW;
  343. /* IRQ */
  344. /* HIMR - turn all on */
  345. rtlusb->irq_mask[0] = 0xFFFFFFFF;
  346. /* HIMR_EX - turn all on */
  347. rtlusb->irq_mask[1] = 0xFFFFFFFF;
  348. rtlusb->disableHWSM = true;
  349. }
  350. static void _rtl_rx_completed(struct urb *urb);
  351. static int _rtl_prep_rx_urb(struct ieee80211_hw *hw, struct rtl_usb *rtlusb,
  352. struct urb *urb, gfp_t gfp_mask)
  353. {
  354. struct rtl_priv *rtlpriv = rtl_priv(hw);
  355. void *buf;
  356. buf = usb_alloc_coherent(rtlusb->udev, rtlusb->rx_max_size, gfp_mask,
  357. &urb->transfer_dma);
  358. if (!buf) {
  359. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  360. "Failed to usb_alloc_coherent!!\n");
  361. return -ENOMEM;
  362. }
  363. usb_fill_bulk_urb(urb, rtlusb->udev,
  364. usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
  365. buf, rtlusb->rx_max_size, _rtl_rx_completed, rtlusb);
  366. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  367. return 0;
  368. }
  369. static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
  370. struct sk_buff *skb)
  371. {
  372. struct rtl_priv *rtlpriv = rtl_priv(hw);
  373. u8 *rxdesc = skb->data;
  374. struct ieee80211_hdr *hdr;
  375. bool unicast = false;
  376. __le16 fc;
  377. struct ieee80211_rx_status rx_status = {0};
  378. struct rtl_stats stats = {
  379. .signal = 0,
  380. .noise = -98,
  381. .rate = 0,
  382. };
  383. skb_pull(skb, RTL_RX_DESC_SIZE);
  384. rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
  385. skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
  386. hdr = (struct ieee80211_hdr *)(skb->data);
  387. fc = hdr->frame_control;
  388. if (!stats.crc) {
  389. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  390. if (is_broadcast_ether_addr(hdr->addr1)) {
  391. /*TODO*/;
  392. } else if (is_multicast_ether_addr(hdr->addr1)) {
  393. /*TODO*/
  394. } else {
  395. unicast = true;
  396. rtlpriv->stats.rxbytesunicast += skb->len;
  397. }
  398. rtl_is_special_data(hw, skb, false);
  399. if (ieee80211_is_data(fc)) {
  400. rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
  401. if (unicast)
  402. rtlpriv->link_info.num_rx_inperiod++;
  403. }
  404. }
  405. }
  406. static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
  407. struct sk_buff *skb)
  408. {
  409. struct rtl_priv *rtlpriv = rtl_priv(hw);
  410. u8 *rxdesc = skb->data;
  411. struct ieee80211_hdr *hdr;
  412. bool unicast = false;
  413. __le16 fc;
  414. struct ieee80211_rx_status rx_status = {0};
  415. struct rtl_stats stats = {
  416. .signal = 0,
  417. .noise = -98,
  418. .rate = 0,
  419. };
  420. skb_pull(skb, RTL_RX_DESC_SIZE);
  421. rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
  422. skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
  423. hdr = (struct ieee80211_hdr *)(skb->data);
  424. fc = hdr->frame_control;
  425. if (!stats.crc) {
  426. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  427. if (is_broadcast_ether_addr(hdr->addr1)) {
  428. /*TODO*/;
  429. } else if (is_multicast_ether_addr(hdr->addr1)) {
  430. /*TODO*/
  431. } else {
  432. unicast = true;
  433. rtlpriv->stats.rxbytesunicast += skb->len;
  434. }
  435. rtl_is_special_data(hw, skb, false);
  436. if (ieee80211_is_data(fc)) {
  437. rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
  438. if (unicast)
  439. rtlpriv->link_info.num_rx_inperiod++;
  440. }
  441. if (likely(rtl_action_proc(hw, skb, false)))
  442. ieee80211_rx_irqsafe(hw, skb);
  443. else
  444. dev_kfree_skb_any(skb);
  445. }
  446. }
  447. static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
  448. {
  449. struct sk_buff *_skb;
  450. struct sk_buff_head rx_queue;
  451. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  452. skb_queue_head_init(&rx_queue);
  453. if (rtlusb->usb_rx_segregate_hdl)
  454. rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
  455. WARN_ON(skb_queue_empty(&rx_queue));
  456. while (!skb_queue_empty(&rx_queue)) {
  457. _skb = skb_dequeue(&rx_queue);
  458. _rtl_usb_rx_process_agg(hw, _skb);
  459. ieee80211_rx_irqsafe(hw, _skb);
  460. }
  461. }
  462. #define __RADIO_TAP_SIZE_RSV 32
  463. static void _rtl_rx_completed(struct urb *_urb)
  464. {
  465. struct rtl_usb *rtlusb = (struct rtl_usb *)_urb->context;
  466. struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
  467. struct rtl_priv *rtlpriv = rtl_priv(hw);
  468. int err = 0;
  469. if (unlikely(IS_USB_STOP(rtlusb)))
  470. goto free;
  471. if (likely(0 == _urb->status)) {
  472. struct sk_buff *skb;
  473. unsigned int size = _urb->actual_length;
  474. if (size < RTL_RX_DESC_SIZE + sizeof(struct ieee80211_hdr)) {
  475. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  476. "Too short packet from bulk IN! (len: %d)\n",
  477. size);
  478. goto resubmit;
  479. }
  480. skb = dev_alloc_skb(size + __RADIO_TAP_SIZE_RSV);
  481. if (!skb) {
  482. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  483. "Can't allocate skb for bulk IN!\n");
  484. goto resubmit;
  485. }
  486. _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
  487. /* reserve some space for mac80211's radiotap */
  488. skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
  489. memcpy(skb_put(skb, size), _urb->transfer_buffer, size);
  490. /* TODO: Do further processing in tasklet (queue skbs,
  491. * schedule tasklet)
  492. */
  493. if (likely(!rtlusb->usb_rx_segregate_hdl)) {
  494. _rtl_usb_rx_process_noagg(hw, skb);
  495. } else {
  496. /* TO DO */
  497. _rtl_rx_pre_process(hw, skb);
  498. pr_err("rx agg not supported\n");
  499. }
  500. goto resubmit;
  501. }
  502. switch (_urb->status) {
  503. /* disconnect */
  504. case -ENOENT:
  505. case -ECONNRESET:
  506. case -ENODEV:
  507. case -ESHUTDOWN:
  508. goto free;
  509. default:
  510. break;
  511. }
  512. resubmit:
  513. usb_anchor_urb(_urb, &rtlusb->rx_submitted);
  514. err = usb_submit_urb(_urb, GFP_ATOMIC);
  515. if (unlikely(err)) {
  516. usb_unanchor_urb(_urb);
  517. goto free;
  518. }
  519. return;
  520. free:
  521. /* On some architectures, usb_free_coherent must not be called from
  522. * hardirq context. Queue urb to cleanup list.
  523. */
  524. usb_anchor_urb(_urb, &rtlusb->rx_cleanup_urbs);
  525. }
  526. #undef __RADIO_TAP_SIZE_RSV
  527. static void _rtl_usb_cleanup_rx(struct ieee80211_hw *hw)
  528. {
  529. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  530. struct urb *urb;
  531. usb_kill_anchored_urbs(&rtlusb->rx_submitted);
  532. while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) {
  533. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  534. urb->transfer_buffer, urb->transfer_dma);
  535. usb_free_urb(urb);
  536. }
  537. }
  538. static int _rtl_usb_receive(struct ieee80211_hw *hw)
  539. {
  540. struct urb *urb;
  541. int err;
  542. int i;
  543. struct rtl_priv *rtlpriv = rtl_priv(hw);
  544. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  545. WARN_ON(0 == rtlusb->rx_urb_num);
  546. /* 1600 == 1514 + max WLAN header + rtk info */
  547. WARN_ON(rtlusb->rx_max_size < 1600);
  548. for (i = 0; i < rtlusb->rx_urb_num; i++) {
  549. err = -ENOMEM;
  550. urb = usb_alloc_urb(0, GFP_KERNEL);
  551. if (!urb) {
  552. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  553. "Failed to alloc URB!!\n");
  554. goto err_out;
  555. }
  556. err = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
  557. if (err < 0) {
  558. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  559. "Failed to prep_rx_urb!!\n");
  560. usb_free_urb(urb);
  561. goto err_out;
  562. }
  563. usb_anchor_urb(urb, &rtlusb->rx_submitted);
  564. err = usb_submit_urb(urb, GFP_KERNEL);
  565. if (err)
  566. goto err_out;
  567. usb_free_urb(urb);
  568. }
  569. return 0;
  570. err_out:
  571. usb_kill_anchored_urbs(&rtlusb->rx_submitted);
  572. _rtl_usb_cleanup_rx(hw);
  573. return err;
  574. }
  575. static int rtl_usb_start(struct ieee80211_hw *hw)
  576. {
  577. int err;
  578. struct rtl_priv *rtlpriv = rtl_priv(hw);
  579. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  580. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  581. err = rtlpriv->cfg->ops->hw_init(hw);
  582. if (!err) {
  583. rtl_init_rx_config(hw);
  584. /* Enable software */
  585. SET_USB_START(rtlusb);
  586. /* should after adapter start and interrupt enable. */
  587. set_hal_start(rtlhal);
  588. /* Start bulk IN */
  589. err = _rtl_usb_receive(hw);
  590. }
  591. return err;
  592. }
  593. /**
  594. *
  595. *
  596. */
  597. /*======================= tx =========================================*/
  598. static void rtl_usb_cleanup(struct ieee80211_hw *hw)
  599. {
  600. u32 i;
  601. struct sk_buff *_skb;
  602. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  603. struct ieee80211_tx_info *txinfo;
  604. SET_USB_STOP(rtlusb);
  605. /* clean up rx stuff. */
  606. _rtl_usb_cleanup_rx(hw);
  607. /* clean up tx stuff */
  608. for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
  609. while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
  610. rtlusb->usb_tx_cleanup(hw, _skb);
  611. txinfo = IEEE80211_SKB_CB(_skb);
  612. ieee80211_tx_info_clear_status(txinfo);
  613. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  614. ieee80211_tx_status_irqsafe(hw, _skb);
  615. }
  616. usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
  617. }
  618. usb_kill_anchored_urbs(&rtlusb->tx_submitted);
  619. }
  620. /**
  621. *
  622. * We may add some struct into struct rtl_usb later. Do deinit here.
  623. *
  624. */
  625. static void rtl_usb_deinit(struct ieee80211_hw *hw)
  626. {
  627. rtl_usb_cleanup(hw);
  628. }
  629. static void rtl_usb_stop(struct ieee80211_hw *hw)
  630. {
  631. struct rtl_priv *rtlpriv = rtl_priv(hw);
  632. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  633. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  634. /* should after adapter start and interrupt enable. */
  635. set_hal_stop(rtlhal);
  636. /* Enable software */
  637. SET_USB_STOP(rtlusb);
  638. rtl_usb_deinit(hw);
  639. rtlpriv->cfg->ops->hw_disable(hw);
  640. }
  641. static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
  642. {
  643. int err;
  644. struct rtl_priv *rtlpriv = rtl_priv(hw);
  645. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  646. usb_anchor_urb(_urb, &rtlusb->tx_submitted);
  647. err = usb_submit_urb(_urb, GFP_ATOMIC);
  648. if (err < 0) {
  649. struct sk_buff *skb;
  650. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  651. "Failed to submit urb\n");
  652. usb_unanchor_urb(_urb);
  653. skb = (struct sk_buff *)_urb->context;
  654. kfree_skb(skb);
  655. }
  656. usb_free_urb(_urb);
  657. }
  658. static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
  659. struct sk_buff *skb)
  660. {
  661. struct rtl_priv *rtlpriv = rtl_priv(hw);
  662. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  663. struct ieee80211_tx_info *txinfo;
  664. rtlusb->usb_tx_post_hdl(hw, urb, skb);
  665. skb_pull(skb, RTL_TX_HEADER_SIZE);
  666. txinfo = IEEE80211_SKB_CB(skb);
  667. ieee80211_tx_info_clear_status(txinfo);
  668. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  669. if (urb->status) {
  670. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  671. "Urb has error status 0x%X\n", urb->status);
  672. goto out;
  673. }
  674. /* TODO: statistics */
  675. out:
  676. ieee80211_tx_status_irqsafe(hw, skb);
  677. return urb->status;
  678. }
  679. static void _rtl_tx_complete(struct urb *urb)
  680. {
  681. struct sk_buff *skb = (struct sk_buff *)urb->context;
  682. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  683. struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
  684. struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
  685. int err;
  686. if (unlikely(IS_USB_STOP(rtlusb)))
  687. return;
  688. err = _usb_tx_post(hw, urb, skb);
  689. if (err) {
  690. /* Ignore error and keep issuiing other urbs */
  691. return;
  692. }
  693. }
  694. static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
  695. struct sk_buff *skb, u32 ep_num)
  696. {
  697. struct rtl_priv *rtlpriv = rtl_priv(hw);
  698. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  699. struct urb *_urb;
  700. WARN_ON(NULL == skb);
  701. _urb = usb_alloc_urb(0, GFP_ATOMIC);
  702. if (!_urb) {
  703. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  704. "Can't allocate URB for bulk out!\n");
  705. kfree_skb(skb);
  706. return NULL;
  707. }
  708. _rtl_install_trx_info(rtlusb, skb, ep_num);
  709. usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
  710. ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
  711. _urb->transfer_flags |= URB_ZERO_PACKET;
  712. return _urb;
  713. }
  714. static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
  715. enum rtl_txq qnum)
  716. {
  717. struct rtl_priv *rtlpriv = rtl_priv(hw);
  718. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  719. u32 ep_num;
  720. struct urb *_urb = NULL;
  721. struct sk_buff *_skb = NULL;
  722. WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
  723. if (unlikely(IS_USB_STOP(rtlusb))) {
  724. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  725. "USB device is stopping...\n");
  726. kfree_skb(skb);
  727. return;
  728. }
  729. ep_num = rtlusb->ep_map.ep_mapping[qnum];
  730. _skb = skb;
  731. _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
  732. if (unlikely(!_urb)) {
  733. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  734. "Can't allocate urb. Drop skb!\n");
  735. return;
  736. }
  737. _rtl_submit_tx_urb(hw, _urb);
  738. }
  739. static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw,
  740. struct ieee80211_sta *sta,
  741. struct sk_buff *skb,
  742. u16 hw_queue)
  743. {
  744. struct rtl_priv *rtlpriv = rtl_priv(hw);
  745. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  746. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  747. struct rtl_tx_desc *pdesc = NULL;
  748. struct rtl_tcb_desc tcb_desc;
  749. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  750. __le16 fc = hdr->frame_control;
  751. u8 *pda_addr = hdr->addr1;
  752. /* ssn */
  753. u8 *qc = NULL;
  754. u8 tid = 0;
  755. u16 seq_number = 0;
  756. memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
  757. if (ieee80211_is_auth(fc)) {
  758. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
  759. rtl_ips_nic_on(hw);
  760. }
  761. if (rtlpriv->psc.sw_ps_enabled) {
  762. if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
  763. !ieee80211_has_pm(fc))
  764. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  765. }
  766. rtl_action_proc(hw, skb, true);
  767. if (is_multicast_ether_addr(pda_addr))
  768. rtlpriv->stats.txbytesmulticast += skb->len;
  769. else if (is_broadcast_ether_addr(pda_addr))
  770. rtlpriv->stats.txbytesbroadcast += skb->len;
  771. else
  772. rtlpriv->stats.txbytesunicast += skb->len;
  773. if (ieee80211_is_data_qos(fc)) {
  774. qc = ieee80211_get_qos_ctl(hdr);
  775. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  776. seq_number = (le16_to_cpu(hdr->seq_ctrl) &
  777. IEEE80211_SCTL_SEQ) >> 4;
  778. seq_number += 1;
  779. seq_number <<= 4;
  780. }
  781. rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, sta, skb,
  782. hw_queue, &tcb_desc);
  783. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  784. if (qc)
  785. mac->tids[tid].seq_number = seq_number;
  786. }
  787. if (ieee80211_is_data(fc))
  788. rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
  789. }
  790. static int rtl_usb_tx(struct ieee80211_hw *hw,
  791. struct ieee80211_sta *sta,
  792. struct sk_buff *skb,
  793. struct rtl_tcb_desc *dummy)
  794. {
  795. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  796. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  797. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  798. __le16 fc = hdr->frame_control;
  799. u16 hw_queue;
  800. if (unlikely(is_hal_stop(rtlhal)))
  801. goto err_free;
  802. hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
  803. _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue);
  804. _rtl_usb_transmit(hw, skb, hw_queue);
  805. return NETDEV_TX_OK;
  806. err_free:
  807. dev_kfree_skb_any(skb);
  808. return NETDEV_TX_OK;
  809. }
  810. static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
  811. struct ieee80211_sta *sta,
  812. struct sk_buff *skb)
  813. {
  814. return false;
  815. }
  816. static struct rtl_intf_ops rtl_usb_ops = {
  817. .adapter_start = rtl_usb_start,
  818. .adapter_stop = rtl_usb_stop,
  819. .adapter_tx = rtl_usb_tx,
  820. .waitq_insert = rtl_usb_tx_chk_waitq_insert,
  821. };
  822. int rtl_usb_probe(struct usb_interface *intf,
  823. const struct usb_device_id *id,
  824. struct rtl_hal_cfg *rtl_hal_cfg)
  825. {
  826. int err;
  827. struct ieee80211_hw *hw = NULL;
  828. struct rtl_priv *rtlpriv = NULL;
  829. struct usb_device *udev;
  830. struct rtl_usb_priv *usb_priv;
  831. hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
  832. sizeof(struct rtl_usb_priv), &rtl_ops);
  833. if (!hw) {
  834. RT_ASSERT(false, "ieee80211 alloc failed\n");
  835. return -ENOMEM;
  836. }
  837. rtlpriv = hw->priv;
  838. rtlpriv->usb_data = kzalloc(RTL_USB_MAX_RX_COUNT * sizeof(u32),
  839. GFP_KERNEL);
  840. if (!rtlpriv->usb_data)
  841. return -ENOMEM;
  842. /* this spin lock must be initialized early */
  843. spin_lock_init(&rtlpriv->locks.usb_lock);
  844. rtlpriv->usb_data_index = 0;
  845. init_completion(&rtlpriv->firmware_loading_complete);
  846. SET_IEEE80211_DEV(hw, &intf->dev);
  847. udev = interface_to_usbdev(intf);
  848. usb_get_dev(udev);
  849. usb_priv = rtl_usbpriv(hw);
  850. memset(usb_priv, 0, sizeof(*usb_priv));
  851. usb_priv->dev.intf = intf;
  852. usb_priv->dev.udev = udev;
  853. usb_set_intfdata(intf, hw);
  854. /* init cfg & intf_ops */
  855. rtlpriv->rtlhal.interface = INTF_USB;
  856. rtlpriv->cfg = rtl_hal_cfg;
  857. rtlpriv->intf_ops = &rtl_usb_ops;
  858. rtl_dbgp_flag_init(hw);
  859. /* Init IO handler */
  860. _rtl_usb_io_handler_init(&udev->dev, hw);
  861. rtlpriv->cfg->ops->read_chip_version(hw);
  862. /*like read eeprom and so on */
  863. rtlpriv->cfg->ops->read_eeprom_info(hw);
  864. err = _rtl_usb_init(hw);
  865. if (err)
  866. goto error_out;
  867. rtl_usb_init_sw(hw);
  868. /* Init mac80211 sw */
  869. err = rtl_init_core(hw);
  870. if (err) {
  871. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  872. "Can't allocate sw for mac80211\n");
  873. goto error_out;
  874. }
  875. if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
  876. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n");
  877. goto error_out;
  878. }
  879. rtlpriv->cfg->ops->init_sw_leds(hw);
  880. return 0;
  881. error_out:
  882. rtl_deinit_core(hw);
  883. _rtl_usb_io_handler_release(hw);
  884. usb_put_dev(udev);
  885. complete(&rtlpriv->firmware_loading_complete);
  886. return -ENODEV;
  887. }
  888. EXPORT_SYMBOL(rtl_usb_probe);
  889. void rtl_usb_disconnect(struct usb_interface *intf)
  890. {
  891. struct ieee80211_hw *hw = usb_get_intfdata(intf);
  892. struct rtl_priv *rtlpriv = rtl_priv(hw);
  893. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  894. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  895. if (unlikely(!rtlpriv))
  896. return;
  897. /* just in case driver is removed before firmware callback */
  898. wait_for_completion(&rtlpriv->firmware_loading_complete);
  899. /*ieee80211_unregister_hw will call ops_stop */
  900. if (rtlmac->mac80211_registered == 1) {
  901. ieee80211_unregister_hw(hw);
  902. rtlmac->mac80211_registered = 0;
  903. } else {
  904. rtl_deinit_deferred_work(hw);
  905. rtlpriv->intf_ops->adapter_stop(hw);
  906. }
  907. /*deinit rfkill */
  908. /* rtl_deinit_rfkill(hw); */
  909. rtl_usb_deinit(hw);
  910. rtl_deinit_core(hw);
  911. kfree(rtlpriv->usb_data);
  912. rtlpriv->cfg->ops->deinit_sw_leds(hw);
  913. rtlpriv->cfg->ops->deinit_sw_vars(hw);
  914. _rtl_usb_io_handler_release(hw);
  915. usb_put_dev(rtlusb->udev);
  916. usb_set_intfdata(intf, NULL);
  917. ieee80211_free_hw(hw);
  918. }
  919. EXPORT_SYMBOL(rtl_usb_disconnect);
  920. int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
  921. {
  922. return 0;
  923. }
  924. EXPORT_SYMBOL(rtl_usb_suspend);
  925. int rtl_usb_resume(struct usb_interface *pusb_intf)
  926. {
  927. return 0;
  928. }
  929. EXPORT_SYMBOL(rtl_usb_resume);