usb.c 26 KB

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