usb.c 26 KB

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