usb.c 28 KB

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