usb.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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 rtl_priv *rtlpriv, u32 addr, u16 len)
  113. {
  114. struct device *dev = rtlpriv->io.dev;
  115. struct usb_device *udev = to_usb_device(dev);
  116. u8 request;
  117. u16 wvalue;
  118. u16 index;
  119. __le32 *data;
  120. unsigned long flags;
  121. spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags);
  122. if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
  123. rtlpriv->usb_data_index = 0;
  124. data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
  125. spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags);
  126. request = REALTEK_USB_VENQT_CMD_REQ;
  127. index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
  128. wvalue = (u16)addr;
  129. _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
  130. return le32_to_cpu(*data);
  131. }
  132. static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
  133. {
  134. return (u8)_usb_read_sync(rtlpriv, addr, 1);
  135. }
  136. static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
  137. {
  138. return (u16)_usb_read_sync(rtlpriv, addr, 2);
  139. }
  140. static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
  141. {
  142. return _usb_read_sync(rtlpriv, addr, 4);
  143. }
  144. static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
  145. u16 len)
  146. {
  147. u8 request;
  148. u16 wvalue;
  149. u16 index;
  150. __le32 data;
  151. request = REALTEK_USB_VENQT_CMD_REQ;
  152. index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
  153. wvalue = (u16)(addr&0x0000ffff);
  154. data = cpu_to_le32(val);
  155. _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
  156. len);
  157. }
  158. static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
  159. {
  160. struct device *dev = rtlpriv->io.dev;
  161. _usb_write_async(to_usb_device(dev), addr, val, 1);
  162. }
  163. static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
  164. {
  165. struct device *dev = rtlpriv->io.dev;
  166. _usb_write_async(to_usb_device(dev), addr, val, 2);
  167. }
  168. static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
  169. {
  170. struct device *dev = rtlpriv->io.dev;
  171. _usb_write_async(to_usb_device(dev), addr, val, 4);
  172. }
  173. static void _usb_writeN_sync(struct rtl_priv *rtlpriv, u32 addr, void *data,
  174. u16 len)
  175. {
  176. struct device *dev = rtlpriv->io.dev;
  177. struct usb_device *udev = to_usb_device(dev);
  178. u8 request = REALTEK_USB_VENQT_CMD_REQ;
  179. u8 reqtype = REALTEK_USB_VENQT_WRITE;
  180. u16 wvalue;
  181. u16 index = REALTEK_USB_VENQT_CMD_IDX;
  182. int pipe = usb_sndctrlpipe(udev, 0); /* write_out */
  183. u8 *buffer;
  184. dma_addr_t dma_addr;
  185. wvalue = (u16)(addr&0x0000ffff);
  186. buffer = usb_alloc_coherent(udev, (size_t)len, GFP_ATOMIC, &dma_addr);
  187. if (!buffer)
  188. return;
  189. memcpy(buffer, data, len);
  190. usb_control_msg(udev, pipe, request, reqtype, wvalue,
  191. index, buffer, len, 50);
  192. usb_free_coherent(udev, (size_t)len, buffer, dma_addr);
  193. }
  194. static void _rtl_usb_io_handler_init(struct device *dev,
  195. struct ieee80211_hw *hw)
  196. {
  197. struct rtl_priv *rtlpriv = rtl_priv(hw);
  198. rtlpriv->io.dev = dev;
  199. mutex_init(&rtlpriv->io.bb_mutex);
  200. rtlpriv->io.write8_async = _usb_write8_async;
  201. rtlpriv->io.write16_async = _usb_write16_async;
  202. rtlpriv->io.write32_async = _usb_write32_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.writeN_sync = _usb_writeN_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. pr_info("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. pr_err("Too few input end points found\n");
  297. return -EINVAL;
  298. }
  299. if (rtlusb->out_ep_nums == 0) {
  300. pr_err("No output end points found\n");
  301. return -EINVAL;
  302. }
  303. /* usb endpoint mapping */
  304. err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
  305. rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
  306. _rtl_usb_init_tx(hw);
  307. _rtl_usb_init_rx(hw);
  308. return err;
  309. }
  310. static void rtl_usb_init_sw(struct ieee80211_hw *hw)
  311. {
  312. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  313. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  314. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  315. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  316. rtlhal->hw = hw;
  317. ppsc->inactiveps = false;
  318. ppsc->leisure_ps = false;
  319. ppsc->fwctrl_lps = false;
  320. ppsc->reg_fwctrl_lps = 3;
  321. ppsc->reg_max_lps_awakeintvl = 5;
  322. ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
  323. /* IBSS */
  324. mac->beacon_interval = 100;
  325. /* AMPDU */
  326. mac->min_space_cfg = 0;
  327. mac->max_mss_density = 0;
  328. /* set sane AMPDU defaults */
  329. mac->current_ampdu_density = 7;
  330. mac->current_ampdu_factor = 3;
  331. /* QOS */
  332. rtlusb->acm_method = eAcmWay2_SW;
  333. /* IRQ */
  334. /* HIMR - turn all on */
  335. rtlusb->irq_mask[0] = 0xFFFFFFFF;
  336. /* HIMR_EX - turn all on */
  337. rtlusb->irq_mask[1] = 0xFFFFFFFF;
  338. rtlusb->disableHWSM = true;
  339. }
  340. #define __RADIO_TAP_SIZE_RSV 32
  341. static void _rtl_rx_completed(struct urb *urb);
  342. static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
  343. struct rtl_usb *rtlusb,
  344. struct urb *urb,
  345. gfp_t gfp_mask)
  346. {
  347. struct sk_buff *skb;
  348. struct rtl_priv *rtlpriv = rtl_priv(hw);
  349. skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
  350. gfp_mask);
  351. if (!skb) {
  352. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  353. "Failed to __dev_alloc_skb!!\n");
  354. return ERR_PTR(-ENOMEM);
  355. }
  356. /* reserve some space for mac80211's radiotap */
  357. skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
  358. usb_fill_bulk_urb(urb, rtlusb->udev,
  359. usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
  360. skb->data, min(skb_tailroom(skb),
  361. (int)rtlusb->rx_max_size),
  362. _rtl_rx_completed, skb);
  363. _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
  364. return skb;
  365. }
  366. #undef __RADIO_TAP_SIZE_RSV
  367. static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
  368. struct sk_buff *skb)
  369. {
  370. struct rtl_priv *rtlpriv = rtl_priv(hw);
  371. u8 *rxdesc = skb->data;
  372. struct ieee80211_hdr *hdr;
  373. bool unicast = false;
  374. __le16 fc;
  375. struct ieee80211_rx_status rx_status = {0};
  376. struct rtl_stats stats = {
  377. .signal = 0,
  378. .noise = -98,
  379. .rate = 0,
  380. };
  381. skb_pull(skb, RTL_RX_DESC_SIZE);
  382. rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
  383. skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
  384. hdr = (struct ieee80211_hdr *)(skb->data);
  385. fc = hdr->frame_control;
  386. if (!stats.crc) {
  387. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  388. if (is_broadcast_ether_addr(hdr->addr1)) {
  389. /*TODO*/;
  390. } else if (is_multicast_ether_addr(hdr->addr1)) {
  391. /*TODO*/
  392. } else {
  393. unicast = true;
  394. rtlpriv->stats.rxbytesunicast += skb->len;
  395. }
  396. rtl_is_special_data(hw, skb, false);
  397. if (ieee80211_is_data(fc)) {
  398. rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
  399. if (unicast)
  400. rtlpriv->link_info.num_rx_inperiod++;
  401. }
  402. }
  403. }
  404. static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
  405. struct sk_buff *skb)
  406. {
  407. struct rtl_priv *rtlpriv = rtl_priv(hw);
  408. u8 *rxdesc = skb->data;
  409. struct ieee80211_hdr *hdr;
  410. bool unicast = false;
  411. __le16 fc;
  412. struct ieee80211_rx_status rx_status = {0};
  413. struct rtl_stats stats = {
  414. .signal = 0,
  415. .noise = -98,
  416. .rate = 0,
  417. };
  418. skb_pull(skb, RTL_RX_DESC_SIZE);
  419. rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
  420. skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
  421. hdr = (struct ieee80211_hdr *)(skb->data);
  422. fc = hdr->frame_control;
  423. if (!stats.crc) {
  424. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  425. if (is_broadcast_ether_addr(hdr->addr1)) {
  426. /*TODO*/;
  427. } else if (is_multicast_ether_addr(hdr->addr1)) {
  428. /*TODO*/
  429. } else {
  430. unicast = true;
  431. rtlpriv->stats.rxbytesunicast += skb->len;
  432. }
  433. rtl_is_special_data(hw, skb, false);
  434. if (ieee80211_is_data(fc)) {
  435. rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
  436. if (unicast)
  437. rtlpriv->link_info.num_rx_inperiod++;
  438. }
  439. if (likely(rtl_action_proc(hw, skb, false))) {
  440. struct sk_buff *uskb = NULL;
  441. u8 *pdata;
  442. uskb = dev_alloc_skb(skb->len + 128);
  443. if (uskb) { /* drop packet on allocation failure */
  444. memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
  445. sizeof(rx_status));
  446. pdata = (u8 *)skb_put(uskb, skb->len);
  447. memcpy(pdata, skb->data, skb->len);
  448. ieee80211_rx_irqsafe(hw, uskb);
  449. }
  450. dev_kfree_skb_any(skb);
  451. } else {
  452. dev_kfree_skb_any(skb);
  453. }
  454. }
  455. }
  456. static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
  457. {
  458. struct sk_buff *_skb;
  459. struct sk_buff_head rx_queue;
  460. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  461. skb_queue_head_init(&rx_queue);
  462. if (rtlusb->usb_rx_segregate_hdl)
  463. rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
  464. WARN_ON(skb_queue_empty(&rx_queue));
  465. while (!skb_queue_empty(&rx_queue)) {
  466. _skb = skb_dequeue(&rx_queue);
  467. _rtl_usb_rx_process_agg(hw, skb);
  468. ieee80211_rx_irqsafe(hw, skb);
  469. }
  470. }
  471. static void _rtl_rx_completed(struct urb *_urb)
  472. {
  473. struct sk_buff *skb = (struct sk_buff *)_urb->context;
  474. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  475. struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
  476. struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
  477. struct rtl_priv *rtlpriv = rtl_priv(hw);
  478. int err = 0;
  479. if (unlikely(IS_USB_STOP(rtlusb)))
  480. goto free;
  481. if (likely(0 == _urb->status)) {
  482. /* If this code were moved to work queue, would CPU
  483. * utilization be improved? NOTE: We shall allocate another skb
  484. * and reuse the original one.
  485. */
  486. skb_put(skb, _urb->actual_length);
  487. if (likely(!rtlusb->usb_rx_segregate_hdl)) {
  488. struct sk_buff *_skb;
  489. _rtl_usb_rx_process_noagg(hw, skb);
  490. _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
  491. if (IS_ERR(_skb)) {
  492. err = PTR_ERR(_skb);
  493. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  494. "Can't allocate skb for bulk IN!\n");
  495. return;
  496. }
  497. skb = _skb;
  498. } else{
  499. /* TO DO */
  500. _rtl_rx_pre_process(hw, skb);
  501. pr_err("rx agg not supported\n");
  502. }
  503. goto resubmit;
  504. }
  505. switch (_urb->status) {
  506. /* disconnect */
  507. case -ENOENT:
  508. case -ECONNRESET:
  509. case -ENODEV:
  510. case -ESHUTDOWN:
  511. goto free;
  512. default:
  513. break;
  514. }
  515. resubmit:
  516. skb_reset_tail_pointer(skb);
  517. skb_trim(skb, 0);
  518. usb_anchor_urb(_urb, &rtlusb->rx_submitted);
  519. err = usb_submit_urb(_urb, GFP_ATOMIC);
  520. if (unlikely(err)) {
  521. usb_unanchor_urb(_urb);
  522. goto free;
  523. }
  524. return;
  525. free:
  526. dev_kfree_skb_irq(skb);
  527. }
  528. static int _rtl_usb_receive(struct ieee80211_hw *hw)
  529. {
  530. struct urb *urb;
  531. struct sk_buff *skb;
  532. int err;
  533. int i;
  534. struct rtl_priv *rtlpriv = rtl_priv(hw);
  535. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  536. WARN_ON(0 == rtlusb->rx_urb_num);
  537. /* 1600 == 1514 + max WLAN header + rtk info */
  538. WARN_ON(rtlusb->rx_max_size < 1600);
  539. for (i = 0; i < rtlusb->rx_urb_num; i++) {
  540. err = -ENOMEM;
  541. urb = usb_alloc_urb(0, GFP_KERNEL);
  542. if (!urb) {
  543. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  544. "Failed to alloc URB!!\n");
  545. goto err_out;
  546. }
  547. skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
  548. if (IS_ERR(skb)) {
  549. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  550. "Failed to prep_rx_urb!!\n");
  551. err = PTR_ERR(skb);
  552. goto err_out;
  553. }
  554. usb_anchor_urb(urb, &rtlusb->rx_submitted);
  555. err = usb_submit_urb(urb, GFP_KERNEL);
  556. if (err)
  557. goto err_out;
  558. usb_free_urb(urb);
  559. }
  560. return 0;
  561. err_out:
  562. usb_kill_anchored_urbs(&rtlusb->rx_submitted);
  563. return err;
  564. }
  565. static int rtl_usb_start(struct ieee80211_hw *hw)
  566. {
  567. int err;
  568. struct rtl_priv *rtlpriv = rtl_priv(hw);
  569. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  570. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  571. err = rtlpriv->cfg->ops->hw_init(hw);
  572. if (!err) {
  573. rtl_init_rx_config(hw);
  574. /* Enable software */
  575. SET_USB_START(rtlusb);
  576. /* should after adapter start and interrupt enable. */
  577. set_hal_start(rtlhal);
  578. /* Start bulk IN */
  579. _rtl_usb_receive(hw);
  580. }
  581. return err;
  582. }
  583. /**
  584. *
  585. *
  586. */
  587. /*======================= tx =========================================*/
  588. static void rtl_usb_cleanup(struct ieee80211_hw *hw)
  589. {
  590. u32 i;
  591. struct sk_buff *_skb;
  592. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  593. struct ieee80211_tx_info *txinfo;
  594. SET_USB_STOP(rtlusb);
  595. /* clean up rx stuff. */
  596. usb_kill_anchored_urbs(&rtlusb->rx_submitted);
  597. /* clean up tx stuff */
  598. for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
  599. while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
  600. rtlusb->usb_tx_cleanup(hw, _skb);
  601. txinfo = IEEE80211_SKB_CB(_skb);
  602. ieee80211_tx_info_clear_status(txinfo);
  603. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  604. ieee80211_tx_status_irqsafe(hw, _skb);
  605. }
  606. usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
  607. }
  608. usb_kill_anchored_urbs(&rtlusb->tx_submitted);
  609. }
  610. /**
  611. *
  612. * We may add some struct into struct rtl_usb later. Do deinit here.
  613. *
  614. */
  615. static void rtl_usb_deinit(struct ieee80211_hw *hw)
  616. {
  617. rtl_usb_cleanup(hw);
  618. }
  619. static void rtl_usb_stop(struct ieee80211_hw *hw)
  620. {
  621. struct rtl_priv *rtlpriv = rtl_priv(hw);
  622. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  623. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  624. /* should after adapter start and interrupt enable. */
  625. set_hal_stop(rtlhal);
  626. /* Enable software */
  627. SET_USB_STOP(rtlusb);
  628. rtl_usb_deinit(hw);
  629. rtlpriv->cfg->ops->hw_disable(hw);
  630. }
  631. static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
  632. {
  633. int err;
  634. struct rtl_priv *rtlpriv = rtl_priv(hw);
  635. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  636. usb_anchor_urb(_urb, &rtlusb->tx_submitted);
  637. err = usb_submit_urb(_urb, GFP_ATOMIC);
  638. if (err < 0) {
  639. struct sk_buff *skb;
  640. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  641. "Failed to submit urb\n");
  642. usb_unanchor_urb(_urb);
  643. skb = (struct sk_buff *)_urb->context;
  644. kfree_skb(skb);
  645. }
  646. usb_free_urb(_urb);
  647. }
  648. static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
  649. struct sk_buff *skb)
  650. {
  651. struct rtl_priv *rtlpriv = rtl_priv(hw);
  652. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  653. struct ieee80211_tx_info *txinfo;
  654. rtlusb->usb_tx_post_hdl(hw, urb, skb);
  655. skb_pull(skb, RTL_TX_HEADER_SIZE);
  656. txinfo = IEEE80211_SKB_CB(skb);
  657. ieee80211_tx_info_clear_status(txinfo);
  658. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  659. if (urb->status) {
  660. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  661. "Urb has error status 0x%X\n", urb->status);
  662. goto out;
  663. }
  664. /* TODO: statistics */
  665. out:
  666. ieee80211_tx_status_irqsafe(hw, skb);
  667. return urb->status;
  668. }
  669. static void _rtl_tx_complete(struct urb *urb)
  670. {
  671. struct sk_buff *skb = (struct sk_buff *)urb->context;
  672. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  673. struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
  674. struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
  675. int err;
  676. if (unlikely(IS_USB_STOP(rtlusb)))
  677. return;
  678. err = _usb_tx_post(hw, urb, skb);
  679. if (err) {
  680. /* Ignore error and keep issuiing other urbs */
  681. return;
  682. }
  683. }
  684. static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
  685. struct sk_buff *skb, u32 ep_num)
  686. {
  687. struct rtl_priv *rtlpriv = rtl_priv(hw);
  688. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  689. struct urb *_urb;
  690. WARN_ON(NULL == skb);
  691. _urb = usb_alloc_urb(0, GFP_ATOMIC);
  692. if (!_urb) {
  693. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  694. "Can't allocate URB for bulk out!\n");
  695. kfree_skb(skb);
  696. return NULL;
  697. }
  698. _rtl_install_trx_info(rtlusb, skb, ep_num);
  699. usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
  700. ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
  701. _urb->transfer_flags |= URB_ZERO_PACKET;
  702. return _urb;
  703. }
  704. static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
  705. enum rtl_txq qnum)
  706. {
  707. struct rtl_priv *rtlpriv = rtl_priv(hw);
  708. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  709. u32 ep_num;
  710. struct urb *_urb = NULL;
  711. struct sk_buff *_skb = NULL;
  712. struct sk_buff_head *skb_list;
  713. struct usb_anchor *urb_list;
  714. WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
  715. if (unlikely(IS_USB_STOP(rtlusb))) {
  716. RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
  717. "USB device is stopping...\n");
  718. kfree_skb(skb);
  719. return;
  720. }
  721. ep_num = rtlusb->ep_map.ep_mapping[qnum];
  722. skb_list = &rtlusb->tx_skb_queue[ep_num];
  723. _skb = skb;
  724. _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
  725. if (unlikely(!_urb)) {
  726. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  727. "Can't allocate urb. Drop skb!\n");
  728. return;
  729. }
  730. urb_list = &rtlusb->tx_pending[ep_num];
  731. _rtl_submit_tx_urb(hw, _urb);
  732. }
  733. static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw,
  734. struct ieee80211_sta *sta,
  735. struct sk_buff *skb,
  736. u16 hw_queue)
  737. {
  738. struct rtl_priv *rtlpriv = rtl_priv(hw);
  739. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  740. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  741. struct rtl_tx_desc *pdesc = NULL;
  742. struct rtl_tcb_desc tcb_desc;
  743. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  744. __le16 fc = hdr->frame_control;
  745. u8 *pda_addr = hdr->addr1;
  746. /* ssn */
  747. u8 *qc = NULL;
  748. u8 tid = 0;
  749. u16 seq_number = 0;
  750. memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
  751. if (ieee80211_is_auth(fc)) {
  752. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
  753. rtl_ips_nic_on(hw);
  754. }
  755. if (rtlpriv->psc.sw_ps_enabled) {
  756. if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
  757. !ieee80211_has_pm(fc))
  758. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  759. }
  760. rtl_action_proc(hw, skb, true);
  761. if (is_multicast_ether_addr(pda_addr))
  762. rtlpriv->stats.txbytesmulticast += skb->len;
  763. else if (is_broadcast_ether_addr(pda_addr))
  764. rtlpriv->stats.txbytesbroadcast += skb->len;
  765. else
  766. rtlpriv->stats.txbytesunicast += skb->len;
  767. if (ieee80211_is_data_qos(fc)) {
  768. qc = ieee80211_get_qos_ctl(hdr);
  769. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  770. seq_number = (le16_to_cpu(hdr->seq_ctrl) &
  771. IEEE80211_SCTL_SEQ) >> 4;
  772. seq_number += 1;
  773. seq_number <<= 4;
  774. }
  775. rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, sta, skb,
  776. hw_queue, &tcb_desc);
  777. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  778. if (qc)
  779. mac->tids[tid].seq_number = seq_number;
  780. }
  781. if (ieee80211_is_data(fc))
  782. rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
  783. }
  784. static int rtl_usb_tx(struct ieee80211_hw *hw,
  785. struct ieee80211_sta *sta,
  786. struct sk_buff *skb,
  787. struct rtl_tcb_desc *dummy)
  788. {
  789. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  790. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  791. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  792. __le16 fc = hdr->frame_control;
  793. u16 hw_queue;
  794. if (unlikely(is_hal_stop(rtlhal)))
  795. goto err_free;
  796. hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
  797. _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue);
  798. _rtl_usb_transmit(hw, skb, hw_queue);
  799. return NETDEV_TX_OK;
  800. err_free:
  801. dev_kfree_skb_any(skb);
  802. return NETDEV_TX_OK;
  803. }
  804. static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
  805. struct ieee80211_sta *sta,
  806. struct sk_buff *skb)
  807. {
  808. return false;
  809. }
  810. static struct rtl_intf_ops rtl_usb_ops = {
  811. .adapter_start = rtl_usb_start,
  812. .adapter_stop = rtl_usb_stop,
  813. .adapter_tx = rtl_usb_tx,
  814. .waitq_insert = rtl_usb_tx_chk_waitq_insert,
  815. };
  816. int __devinit rtl_usb_probe(struct usb_interface *intf,
  817. const struct usb_device_id *id)
  818. {
  819. int err;
  820. struct ieee80211_hw *hw = NULL;
  821. struct rtl_priv *rtlpriv = NULL;
  822. struct usb_device *udev;
  823. struct rtl_usb_priv *usb_priv;
  824. hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
  825. sizeof(struct rtl_usb_priv), &rtl_ops);
  826. if (!hw) {
  827. RT_ASSERT(false, "ieee80211 alloc failed\n");
  828. return -ENOMEM;
  829. }
  830. rtlpriv = hw->priv;
  831. rtlpriv->usb_data = kzalloc(RTL_USB_MAX_RX_COUNT * sizeof(u32),
  832. GFP_KERNEL);
  833. if (!rtlpriv->usb_data)
  834. return -ENOMEM;
  835. /* this spin lock must be initialized early */
  836. spin_lock_init(&rtlpriv->locks.usb_lock);
  837. rtlpriv->usb_data_index = 0;
  838. init_completion(&rtlpriv->firmware_loading_complete);
  839. SET_IEEE80211_DEV(hw, &intf->dev);
  840. udev = interface_to_usbdev(intf);
  841. usb_get_dev(udev);
  842. usb_priv = rtl_usbpriv(hw);
  843. memset(usb_priv, 0, sizeof(*usb_priv));
  844. usb_priv->dev.intf = intf;
  845. usb_priv->dev.udev = udev;
  846. usb_set_intfdata(intf, hw);
  847. /* init cfg & intf_ops */
  848. rtlpriv->rtlhal.interface = INTF_USB;
  849. rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_info);
  850. rtlpriv->intf_ops = &rtl_usb_ops;
  851. rtl_dbgp_flag_init(hw);
  852. /* Init IO handler */
  853. _rtl_usb_io_handler_init(&udev->dev, hw);
  854. rtlpriv->cfg->ops->read_chip_version(hw);
  855. /*like read eeprom and so on */
  856. rtlpriv->cfg->ops->read_eeprom_info(hw);
  857. err = _rtl_usb_init(hw);
  858. if (err)
  859. goto error_out;
  860. rtl_usb_init_sw(hw);
  861. /* Init mac80211 sw */
  862. err = rtl_init_core(hw);
  863. if (err) {
  864. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  865. "Can't allocate sw for mac80211\n");
  866. goto error_out;
  867. }
  868. if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
  869. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n");
  870. goto error_out;
  871. }
  872. rtlpriv->cfg->ops->init_sw_leds(hw);
  873. return 0;
  874. error_out:
  875. rtl_deinit_core(hw);
  876. _rtl_usb_io_handler_release(hw);
  877. usb_put_dev(udev);
  878. complete(&rtlpriv->firmware_loading_complete);
  879. return -ENODEV;
  880. }
  881. EXPORT_SYMBOL(rtl_usb_probe);
  882. void rtl_usb_disconnect(struct usb_interface *intf)
  883. {
  884. struct ieee80211_hw *hw = usb_get_intfdata(intf);
  885. struct rtl_priv *rtlpriv = rtl_priv(hw);
  886. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  887. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  888. if (unlikely(!rtlpriv))
  889. return;
  890. /* just in case driver is removed before firmware callback */
  891. wait_for_completion(&rtlpriv->firmware_loading_complete);
  892. /*ieee80211_unregister_hw will call ops_stop */
  893. if (rtlmac->mac80211_registered == 1) {
  894. ieee80211_unregister_hw(hw);
  895. rtlmac->mac80211_registered = 0;
  896. } else {
  897. rtl_deinit_deferred_work(hw);
  898. rtlpriv->intf_ops->adapter_stop(hw);
  899. }
  900. /*deinit rfkill */
  901. /* rtl_deinit_rfkill(hw); */
  902. rtl_usb_deinit(hw);
  903. rtl_deinit_core(hw);
  904. kfree(rtlpriv->usb_data);
  905. rtlpriv->cfg->ops->deinit_sw_leds(hw);
  906. rtlpriv->cfg->ops->deinit_sw_vars(hw);
  907. _rtl_usb_io_handler_release(hw);
  908. usb_put_dev(rtlusb->udev);
  909. usb_set_intfdata(intf, NULL);
  910. ieee80211_free_hw(hw);
  911. }
  912. EXPORT_SYMBOL(rtl_usb_disconnect);
  913. int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
  914. {
  915. return 0;
  916. }
  917. EXPORT_SYMBOL(rtl_usb_suspend);
  918. int rtl_usb_resume(struct usb_interface *pusb_intf)
  919. {
  920. return 0;
  921. }
  922. EXPORT_SYMBOL(rtl_usb_resume);