usb.c 27 KB

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