usb.c 27 KB

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