Linux Audio

Check our new training course

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