usb.c 27 KB

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