usb.c 27 KB

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