Linux Audio

Check our new training course

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