usb.c 27 KB

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