usb.c 26 KB

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