hif_usb.c 32.3 KB
Newer Older
S
Sujith 已提交
1
/*
2
 * Copyright (c) 2010-2011 Atheros Communications Inc.
S
Sujith 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

17
#include <asm/unaligned.h>
S
Sujith 已提交
18 19
#include "htc.h"

20 21
MODULE_FIRMWARE(HTC_7010_MODULE_FW);
MODULE_FIRMWARE(HTC_9271_MODULE_FW);
22

S
Sujith 已提交
23
static struct usb_device_id ath9k_hif_usb_ids[] = {
24 25 26 27 28 29
	{ USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
	{ USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
	{ USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
	{ USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
	{ USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
	{ USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
30
	{ USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
31 32 33
	{ USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
	{ USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
	{ USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
34
	{ USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
35
	{ USB_DEVICE(0x040D, 0x3801) }, /* VIA */
36
	{ USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
37
	{ USB_DEVICE(0x0cf3, 0xb002) }, /* Ubiquiti WifiStation */
38
	{ USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */
L
Leon Nardella 已提交
39
	{ USB_DEVICE(0x0471, 0x209e) }, /* Philips (or NXP) PTA01 */
40 41 42

	{ USB_DEVICE(0x0cf3, 0x7015),
	  .driver_info = AR9287_USB },  /* Atheros */
43
	{ USB_DEVICE(0x1668, 0x1200),
44 45 46 47 48 49 50 51
	  .driver_info = AR9287_USB },  /* Verizon */

	{ USB_DEVICE(0x0cf3, 0x7010),
	  .driver_info = AR9280_USB },  /* Atheros */
	{ USB_DEVICE(0x0846, 0x9018),
	  .driver_info = AR9280_USB },  /* Netgear WNDA3200 */
	{ USB_DEVICE(0x083A, 0xA704),
	  .driver_info = AR9280_USB },  /* SMC Networks */
52 53
	{ USB_DEVICE(0x0411, 0x017f),
	  .driver_info = AR9280_USB },  /* Sony UWA-BR100 */
54 55
	{ USB_DEVICE(0x0411, 0x0197),
	  .driver_info = AR9280_USB },  /* Buffalo WLI-UV-AG300P */
56 57
	{ USB_DEVICE(0x04da, 0x3904),
	  .driver_info = AR9280_USB },
58 59
	{ USB_DEVICE(0x0930, 0x0a08),
	  .driver_info = AR9280_USB },  /* Toshiba WLM-20U2 and GN-1080 */
60

61 62 63
	{ USB_DEVICE(0x0cf3, 0x20ff),
	  .driver_info = STORAGE_DEVICE },

S
Sujith 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	{ },
};

MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);

static int __hif_usb_tx(struct hif_device_usb *hif_dev);

static void hif_usb_regout_cb(struct urb *urb)
{
	struct cmd_buf *cmd = (struct cmd_buf *)urb->context;

	switch (urb->status) {
	case 0:
		break;
	case -ENOENT:
	case -ECONNRESET:
	case -ENODEV:
	case -ESHUTDOWN:
S
Sujith 已提交
82
		goto free;
S
Sujith 已提交
83 84 85 86 87 88
	default:
		break;
	}

	if (cmd) {
		ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
89
					  cmd->skb, true);
S
Sujith 已提交
90 91
		kfree(cmd);
	}
S
Sujith 已提交
92 93 94

	return;
free:
95
	kfree_skb(cmd->skb);
S
Sujith 已提交
96
	kfree(cmd);
S
Sujith 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
}

static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
			       struct sk_buff *skb)
{
	struct urb *urb;
	struct cmd_buf *cmd;
	int ret = 0;

	urb = usb_alloc_urb(0, GFP_KERNEL);
	if (urb == NULL)
		return -ENOMEM;

	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
	if (cmd == NULL) {
		usb_free_urb(urb);
		return -ENOMEM;
	}

	cmd->skb = skb;
	cmd->hif_dev = hif_dev;

119 120
	usb_fill_int_urb(urb, hif_dev->udev,
			 usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
S
Sujith 已提交
121
			 skb->data, skb->len,
122
			 hif_usb_regout_cb, cmd, 1);
S
Sujith 已提交
123

S
Sujith 已提交
124
	usb_anchor_urb(urb, &hif_dev->regout_submitted);
S
Sujith 已提交
125 126
	ret = usb_submit_urb(urb, GFP_KERNEL);
	if (ret) {
S
Sujith 已提交
127
		usb_unanchor_urb(urb);
S
Sujith 已提交
128 129
		kfree(cmd);
	}
S
Sujith 已提交
130
	usb_free_urb(urb);
S
Sujith 已提交
131 132 133 134

	return ret;
}

135 136 137
static void hif_usb_mgmt_cb(struct urb *urb)
{
	struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
138
	struct hif_device_usb *hif_dev;
139 140 141 142 143
	bool txok = true;

	if (!cmd || !cmd->skb || !cmd->hif_dev)
		return;

144 145
	hif_dev = cmd->hif_dev;

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	switch (urb->status) {
	case 0:
		break;
	case -ENOENT:
	case -ECONNRESET:
	case -ENODEV:
	case -ESHUTDOWN:
		txok = false;

		/*
		 * If the URBs are being flushed, no need to complete
		 * this packet.
		 */
		spin_lock(&hif_dev->tx.tx_lock);
		if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
			spin_unlock(&hif_dev->tx.tx_lock);
			dev_kfree_skb_any(cmd->skb);
			kfree(cmd);
			return;
		}
		spin_unlock(&hif_dev->tx.tx_lock);

		break;
	default:
		txok = false;
		break;
	}

	skb_pull(cmd->skb, 4);
	ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
				  cmd->skb, txok);
	kfree(cmd);
}

static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev,
			     struct sk_buff *skb)
{
	struct urb *urb;
	struct cmd_buf *cmd;
	int ret = 0;
	__le16 *hdr;

	urb = usb_alloc_urb(0, GFP_ATOMIC);
	if (urb == NULL)
		return -ENOMEM;

	cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
	if (cmd == NULL) {
		usb_free_urb(urb);
		return -ENOMEM;
	}

	cmd->skb = skb;
	cmd->hif_dev = hif_dev;

	hdr = (__le16 *) skb_push(skb, 4);
	*hdr++ = cpu_to_le16(skb->len - 4);
	*hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);

	usb_fill_bulk_urb(urb, hif_dev->udev,
			 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
			 skb->data, skb->len,
			 hif_usb_mgmt_cb, cmd);

	usb_anchor_urb(urb, &hif_dev->mgmt_submitted);
	ret = usb_submit_urb(urb, GFP_ATOMIC);
	if (ret) {
		usb_unanchor_urb(urb);
		kfree(cmd);
	}
	usb_free_urb(urb);

	return ret;
}

S
Sujith 已提交
221 222 223 224 225 226 227
static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
					 struct sk_buff_head *list)
{
	struct sk_buff *skb;

	while ((skb = __skb_dequeue(list)) != NULL) {
		dev_kfree_skb_any(skb);
228 229 230 231 232 233 234 235 236 237
	}
}

static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
					    struct sk_buff_head *queue,
					    bool txok)
{
	struct sk_buff *skb;

	while ((skb = __skb_dequeue(queue)) != NULL) {
238 239 240
#ifdef CONFIG_ATH9K_HTC_DEBUGFS
		int ln = skb->len;
#endif
241 242
		ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
					  skb, txok);
243
		if (txok) {
244
			TX_STAT_INC(skb_success);
245 246
			TX_STAT_ADD(skb_success_bytes, ln);
		}
247 248
		else
			TX_STAT_INC(skb_failed);
S
Sujith 已提交
249 250 251
	}
}

S
Sujith 已提交
252 253 254
static void hif_usb_tx_cb(struct urb *urb)
{
	struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
255
	struct hif_device_usb *hif_dev;
256
	bool txok = true;
S
Sujith 已提交
257

258
	if (!tx_buf || !tx_buf->hif_dev)
S
Sujith 已提交
259 260
		return;

261 262
	hif_dev = tx_buf->hif_dev;

S
Sujith 已提交
263 264 265 266 267 268 269
	switch (urb->status) {
	case 0:
		break;
	case -ENOENT:
	case -ECONNRESET:
	case -ENODEV:
	case -ESHUTDOWN:
270
		txok = false;
271 272 273 274 275 276 277 278

		/*
		 * If the URBs are being flushed, no need to add this
		 * URB to the free list.
		 */
		spin_lock(&hif_dev->tx.tx_lock);
		if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
			spin_unlock(&hif_dev->tx.tx_lock);
279
			ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
280 281 282 283
			return;
		}
		spin_unlock(&hif_dev->tx.tx_lock);

284
		break;
S
Sujith 已提交
285
	default:
286
		txok = false;
S
Sujith 已提交
287 288 289
		break;
	}

290
	ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
S
Sujith 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303

	/* Re-initialize the SKB queue */
	tx_buf->len = tx_buf->offset = 0;
	__skb_queue_head_init(&tx_buf->skb_queue);

	/* Add this TX buffer to the free list */
	spin_lock(&hif_dev->tx.tx_lock);
	list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
	hif_dev->tx.tx_buf_cnt++;
	if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
		__hif_usb_tx(hif_dev); /* Check for pending SKBs */
	TX_STAT_INC(buf_completed);
	spin_unlock(&hif_dev->tx.tx_lock);
304 305
}

S
Sujith 已提交
306 307 308 309 310 311
/* TX lock has to be taken */
static int __hif_usb_tx(struct hif_device_usb *hif_dev)
{
	struct tx_buf *tx_buf = NULL;
	struct sk_buff *nskb = NULL;
	int ret = 0, i;
312
	u16 tx_skb_cnt = 0;
S
Sujith 已提交
313
	u8 *buf;
314
	__le16 *hdr;
S
Sujith 已提交
315 316 317 318 319 320 321 322 323

	if (hif_dev->tx.tx_skb_cnt == 0)
		return 0;

	/* Check if a free TX buffer is available */
	if (list_empty(&hif_dev->tx.tx_buf))
		return 0;

	tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
S
Sujith 已提交
324
	list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
S
Sujith 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338
	hif_dev->tx.tx_buf_cnt--;

	tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);

	for (i = 0; i < tx_skb_cnt; i++) {
		nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);

		/* Should never be NULL */
		BUG_ON(!nskb);

		hif_dev->tx.tx_skb_cnt--;

		buf = tx_buf->buf;
		buf += tx_buf->offset;
339 340 341
		hdr = (__le16 *)buf;
		*hdr++ = cpu_to_le16(nskb->len);
		*hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
S
Sujith 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
		buf += 4;
		memcpy(buf, nskb->data, nskb->len);
		tx_buf->len = nskb->len + 4;

		if (i < (tx_skb_cnt - 1))
			tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;

		if (i == (tx_skb_cnt - 1))
			tx_buf->len += tx_buf->offset;

		__skb_queue_tail(&tx_buf->skb_queue, nskb);
		TX_STAT_INC(skb_queued);
	}

	usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
			  usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
			  tx_buf->buf, tx_buf->len,
			  hif_usb_tx_cb, tx_buf);

	ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
	if (ret) {
		tx_buf->len = tx_buf->offset = 0;
364
		ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
S
Sujith 已提交
365 366 367 368 369 370 371 372 373 374 375
		__skb_queue_head_init(&tx_buf->skb_queue);
		list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
		hif_dev->tx.tx_buf_cnt++;
	}

	if (!ret)
		TX_STAT_INC(buf_queued);

	return ret;
}

376
static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
S
Sujith 已提交
377
{
378
	struct ath9k_htc_tx_ctl *tx_ctl;
S
Sujith 已提交
379
	unsigned long flags;
380
	int ret = 0;
S
Sujith 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394

	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);

	if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
		spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
		return -ENODEV;
	}

	/* Check if the max queue count has been reached */
	if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
		spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
		return -ENOMEM;
	}

395
	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
S
Sujith 已提交
396

397 398
	tx_ctl = HTC_SKB_CB(skb);

399 400 401 402 403 404 405 406 407 408 409 410 411
	/* Mgmt/Beacon frames don't use the TX buffer pool */
	if ((tx_ctl->type == ATH9K_HTC_MGMT) ||
	    (tx_ctl->type == ATH9K_HTC_BEACON)) {
		ret = hif_usb_send_mgmt(hif_dev, skb);
	}

	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);

	if ((tx_ctl->type == ATH9K_HTC_NORMAL) ||
	    (tx_ctl->type == ATH9K_HTC_AMPDU)) {
		__skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
		hif_dev->tx.tx_skb_cnt++;
	}
S
Sujith 已提交
412 413

	/* Check if AMPDUs have to be sent immediately */
414
	if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
S
Sujith 已提交
415 416 417 418 419 420
	    (hif_dev->tx.tx_skb_cnt < 2)) {
		__hif_usb_tx(hif_dev);
	}

	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);

421
	return ret;
S
Sujith 已提交
422 423
}

424
static void hif_usb_start(void *hif_handle)
S
Sujith 已提交
425 426 427 428 429 430 431 432 433 434 435
{
	struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
	unsigned long flags;

	hif_dev->flags |= HIF_USB_START;

	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
	hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
}

436
static void hif_usb_stop(void *hif_handle)
S
Sujith 已提交
437 438
{
	struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
439
	struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
S
Sujith 已提交
440 441 442
	unsigned long flags;

	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
443
	ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
S
Sujith 已提交
444 445 446
	hif_dev->tx.tx_skb_cnt = 0;
	hif_dev->tx.flags |= HIF_USB_TX_STOP;
	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
447 448 449 450 451 452

	/* The pending URBs have to be canceled. */
	list_for_each_entry_safe(tx_buf, tx_buf_tmp,
				 &hif_dev->tx.tx_pending, list) {
		usb_kill_urb(tx_buf->urb);
	}
453 454

	usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
S
Sujith 已提交
455 456
}

457
static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
S
Sujith 已提交
458 459 460 461 462 463
{
	struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
	int ret = 0;

	switch (pipe_id) {
	case USB_WLAN_TX_PIPE:
464
		ret = hif_usb_send_tx(hif_dev, skb);
S
Sujith 已提交
465 466 467 468 469
		break;
	case USB_REG_OUT_PIPE:
		ret = hif_usb_send_regout(hif_dev, skb);
		break;
	default:
S
Sujith 已提交
470 471
		dev_err(&hif_dev->udev->dev,
			"ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
S
Sujith 已提交
472 473 474 475 476 477 478
		ret = -EINVAL;
		break;
	}

	return ret;
}

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
static inline bool check_index(struct sk_buff *skb, u8 idx)
{
	struct ath9k_htc_tx_ctl *tx_ctl;

	tx_ctl = HTC_SKB_CB(skb);

	if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
	    (tx_ctl->sta_idx == idx))
		return true;

	return false;
}

static void hif_usb_sta_drain(void *hif_handle, u8 idx)
{
	struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
	struct sk_buff *skb, *tmp;
	unsigned long flags;

	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);

	skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
		if (check_index(skb, idx)) {
			__skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
			ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
						  skb, false);
			hif_dev->tx.tx_skb_cnt--;
			TX_STAT_INC(skb_failed);
		}
	}

	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
}

S
Sujith 已提交
513 514 515 516 517 518 519 520 521
static struct ath9k_htc_hif hif_usb = {
	.transport = ATH9K_HIF_USB,
	.name = "ath9k_hif_usb",

	.control_ul_pipe = USB_REG_OUT_PIPE,
	.control_dl_pipe = USB_REG_IN_PIPE,

	.start = hif_usb_start,
	.stop = hif_usb_stop,
522
	.sta_drain = hif_usb_sta_drain,
S
Sujith 已提交
523 524 525 526 527 528
	.send = hif_usb_send,
};

static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
				    struct sk_buff *skb)
{
S
Sujith 已提交
529
	struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
530
	int index = 0, i, len = skb->len;
531 532
	int rx_remain_len, rx_pkt_len;
	u16 pool_index = 0;
S
Sujith 已提交
533 534
	u8 *ptr;

S
Sujith 已提交
535 536
	spin_lock(&hif_dev->rx_lock);

S
Sujith 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
	rx_remain_len = hif_dev->rx_remain_len;
	rx_pkt_len = hif_dev->rx_transfer_len;

	if (rx_remain_len != 0) {
		struct sk_buff *remain_skb = hif_dev->remain_skb;

		if (remain_skb) {
			ptr = (u8 *) remain_skb->data;

			index = rx_remain_len;
			rx_remain_len -= hif_dev->rx_pad_len;
			ptr += rx_pkt_len;

			memcpy(ptr, skb->data, rx_remain_len);

			rx_pkt_len += rx_remain_len;
			hif_dev->rx_remain_len = 0;
			skb_put(remain_skb, rx_pkt_len);

			skb_pool[pool_index++] = remain_skb;

		} else {
			index = rx_remain_len;
		}
	}

S
Sujith 已提交
563 564
	spin_unlock(&hif_dev->rx_lock);

S
Sujith 已提交
565
	while (index < len) {
566 567 568 569 570
		u16 pkt_len;
		u16 pkt_tag;
		u16 pad_len;
		int chk_idx;

S
Sujith 已提交
571 572
		ptr = (u8 *) skb->data;

573 574
		pkt_len = get_unaligned_le16(ptr + index);
		pkt_tag = get_unaligned_le16(ptr + index + 2);
S
Sujith 已提交
575

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
		if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
			RX_STAT_INC(skb_dropped);
			return;
		}

		pad_len = 4 - (pkt_len & 0x3);
		if (pad_len == 4)
			pad_len = 0;

		chk_idx = index;
		index = index + 4 + pkt_len + pad_len;

		if (index > MAX_RX_BUF_SIZE) {
			spin_lock(&hif_dev->rx_lock);
			hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
			hif_dev->rx_transfer_len =
				MAX_RX_BUF_SIZE - chk_idx - 4;
			hif_dev->rx_pad_len = pad_len;

			nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
			if (!nskb) {
				dev_err(&hif_dev->udev->dev,
					"ath9k_htc: RX memory allocation error\n");
S
Sujith 已提交
599
				spin_unlock(&hif_dev->rx_lock);
600
				goto err;
S
Sujith 已提交
601
			}
602 603 604 605 606 607 608 609 610
			skb_reserve(nskb, 32);
			RX_STAT_INC(skb_allocated);

			memcpy(nskb->data, &(skb->data[chk_idx+4]),
			       hif_dev->rx_transfer_len);

			/* Record the buffer pointer */
			hif_dev->remain_skb = nskb;
			spin_unlock(&hif_dev->rx_lock);
S
Sujith 已提交
611
		} else {
612 613 614 615 616 617 618 619 620 621 622 623
			nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
			if (!nskb) {
				dev_err(&hif_dev->udev->dev,
					"ath9k_htc: RX memory allocation error\n");
				goto err;
			}
			skb_reserve(nskb, 32);
			RX_STAT_INC(skb_allocated);

			memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
			skb_put(nskb, pkt_len);
			skb_pool[pool_index++] = nskb;
S
Sujith 已提交
624 625 626 627 628
		}
	}

err:
	for (i = 0; i < pool_index; i++) {
629
		RX_STAT_ADD(skb_completed_bytes, skb_pool[i]->len);
S
Sujith 已提交
630 631 632 633 634 635 636 637 638
		ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
				 skb_pool[i]->len, USB_WLAN_RX_PIPE);
		RX_STAT_INC(skb_completed);
	}
}

static void ath9k_hif_usb_rx_cb(struct urb *urb)
{
	struct sk_buff *skb = (struct sk_buff *) urb->context;
639
	struct hif_device_usb *hif_dev =
S
Sujith 已提交
640 641 642
		usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
	int ret;

S
Sujith 已提交
643 644 645
	if (!skb)
		return;

S
Sujith 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
	if (!hif_dev)
		goto free;

	switch (urb->status) {
	case 0:
		break;
	case -ENOENT:
	case -ECONNRESET:
	case -ENODEV:
	case -ESHUTDOWN:
		goto free;
	default:
		goto resubmit;
	}

	if (likely(urb->actual_length != 0)) {
		skb_put(skb, urb->actual_length);
		ath9k_hif_usb_rx_stream(hif_dev, skb);
	}

resubmit:
	skb_reset_tail_pointer(skb);
	skb_trim(skb, 0);

S
Sujith 已提交
670
	usb_anchor_urb(urb, &hif_dev->rx_submitted);
S
Sujith 已提交
671
	ret = usb_submit_urb(urb, GFP_ATOMIC);
S
Sujith 已提交
672 673
	if (ret) {
		usb_unanchor_urb(urb);
S
Sujith 已提交
674
		goto free;
S
Sujith 已提交
675
	}
S
Sujith 已提交
676 677 678

	return;
free:
679
	kfree_skb(skb);
S
Sujith 已提交
680 681 682 683 684 685
}

static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
{
	struct sk_buff *skb = (struct sk_buff *) urb->context;
	struct sk_buff *nskb;
686
	struct hif_device_usb *hif_dev =
S
Sujith 已提交
687 688 689
		usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
	int ret;

S
Sujith 已提交
690 691 692
	if (!skb)
		return;

S
Sujith 已提交
693 694 695 696 697 698 699 700 701 702 703 704
	if (!hif_dev)
		goto free;

	switch (urb->status) {
	case 0:
		break;
	case -ENOENT:
	case -ECONNRESET:
	case -ENODEV:
	case -ESHUTDOWN:
		goto free;
	default:
705 706 707
		skb_reset_tail_pointer(skb);
		skb_trim(skb, 0);

S
Sujith 已提交
708 709 710 711 712 713
		goto resubmit;
	}

	if (likely(urb->actual_length != 0)) {
		skb_put(skb, urb->actual_length);

714 715 716 717 718
		/* Process the command first */
		ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
				 skb->len, USB_REG_IN_PIPE);


719
		nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
720 721 722 723 724 725
		if (!nskb) {
			dev_err(&hif_dev->udev->dev,
				"ath9k_htc: REG_IN memory allocation failure\n");
			urb->context = NULL;
			return;
		}
S
Sujith 已提交
726

727 728
		usb_fill_int_urb(urb, hif_dev->udev,
				 usb_rcvintpipe(hif_dev->udev,
729
						 USB_REG_IN_PIPE),
S
Sujith 已提交
730
				 nskb->data, MAX_REG_IN_BUF_SIZE,
731
				 ath9k_hif_usb_reg_in_cb, nskb, 1);
S
Sujith 已提交
732 733 734
	}

resubmit:
735
	usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
S
Sujith 已提交
736
	ret = usb_submit_urb(urb, GFP_ATOMIC);
737 738
	if (ret) {
		usb_unanchor_urb(urb);
S
Sujith 已提交
739
		goto free;
740
	}
S
Sujith 已提交
741 742 743

	return;
free:
744
	kfree_skb(skb);
S
Sujith 已提交
745
	urb->context = NULL;
S
Sujith 已提交
746 747 748 749 750
}

static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
{
	struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
751
	unsigned long flags;
S
Sujith 已提交
752

S
Sujith 已提交
753 754 755
	list_for_each_entry_safe(tx_buf, tx_buf_tmp,
				 &hif_dev->tx.tx_buf, list) {
		usb_kill_urb(tx_buf->urb);
S
Sujith 已提交
756 757 758 759 760 761
		list_del(&tx_buf->list);
		usb_free_urb(tx_buf->urb);
		kfree(tx_buf->buf);
		kfree(tx_buf);
	}

762 763 764 765
	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
	hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);

S
Sujith 已提交
766 767 768 769 770 771 772 773
	list_for_each_entry_safe(tx_buf, tx_buf_tmp,
				 &hif_dev->tx.tx_pending, list) {
		usb_kill_urb(tx_buf->urb);
		list_del(&tx_buf->list);
		usb_free_urb(tx_buf->urb);
		kfree(tx_buf->buf);
		kfree(tx_buf);
	}
774 775

	usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
S
Sujith 已提交
776 777 778 779 780 781 782 783 784 785 786
}

static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
{
	struct tx_buf *tx_buf;
	int i;

	INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
	INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
	spin_lock_init(&hif_dev->tx.tx_lock);
	__skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
787
	init_usb_anchor(&hif_dev->mgmt_submitted);
S
Sujith 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811

	for (i = 0; i < MAX_TX_URB_NUM; i++) {
		tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
		if (!tx_buf)
			goto err;

		tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
		if (!tx_buf->buf)
			goto err;

		tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
		if (!tx_buf->urb)
			goto err;

		tx_buf->hif_dev = hif_dev;
		__skb_queue_head_init(&tx_buf->skb_queue);

		list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
	}

	hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;

	return 0;
err:
812 813 814 815
	if (tx_buf) {
		kfree(tx_buf->buf);
		kfree(tx_buf);
	}
S
Sujith 已提交
816 817 818 819 820 821
	ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
	return -ENOMEM;
}

static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
{
S
Sujith 已提交
822
	usb_kill_anchored_urbs(&hif_dev->rx_submitted);
S
Sujith 已提交
823 824 825 826
}

static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
{
S
Sujith 已提交
827 828
	struct urb *urb = NULL;
	struct sk_buff *skb = NULL;
S
Sujith 已提交
829 830
	int i, ret;

S
Sujith 已提交
831
	init_usb_anchor(&hif_dev->rx_submitted);
S
Sujith 已提交
832
	spin_lock_init(&hif_dev->rx_lock);
S
Sujith 已提交
833

S
Sujith 已提交
834 835 836
	for (i = 0; i < MAX_RX_URB_NUM; i++) {

		/* Allocate URB */
S
Sujith 已提交
837 838
		urb = usb_alloc_urb(0, GFP_KERNEL);
		if (urb == NULL) {
S
Sujith 已提交
839
			ret = -ENOMEM;
S
Sujith 已提交
840
			goto err_urb;
S
Sujith 已提交
841 842 843
		}

		/* Allocate buffer */
844
		skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
S
Sujith 已提交
845 846 847 848
		if (!skb) {
			ret = -ENOMEM;
			goto err_skb;
		}
S
Sujith 已提交
849

S
Sujith 已提交
850 851 852 853 854 855 856 857
		usb_fill_bulk_urb(urb, hif_dev->udev,
				  usb_rcvbulkpipe(hif_dev->udev,
						  USB_WLAN_RX_PIPE),
				  skb->data, MAX_RX_BUF_SIZE,
				  ath9k_hif_usb_rx_cb, skb);

		/* Anchor URB */
		usb_anchor_urb(urb, &hif_dev->rx_submitted);
S
Sujith 已提交
858

S
Sujith 已提交
859 860 861 862 863 864
		/* Submit URB */
		ret = usb_submit_urb(urb, GFP_KERNEL);
		if (ret) {
			usb_unanchor_urb(urb);
			goto err_submit;
		}
S
Sujith 已提交
865 866 867 868 869 870

		/*
		 * Drop reference count.
		 * This ensures that the URB is freed when killing them.
		 */
		usb_free_urb(urb);
S
Sujith 已提交
871 872 873 874
	}

	return 0;

S
Sujith 已提交
875
err_submit:
876
	kfree_skb(skb);
S
Sujith 已提交
877 878 879
err_skb:
	usb_free_urb(urb);
err_urb:
S
Sujith 已提交
880 881 882 883
	ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
	return ret;
}

884
static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
S
Sujith 已提交
885
{
886
	usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
S
Sujith 已提交
887 888
}

889
static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
S
Sujith 已提交
890
{
891 892 893
	struct urb *urb = NULL;
	struct sk_buff *skb = NULL;
	int i, ret;
S
Sujith 已提交
894

895
	init_usb_anchor(&hif_dev->reg_in_submitted);
S
Sujith 已提交
896

897
	for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
S
Sujith 已提交
898

899 900 901 902 903 904
		/* Allocate URB */
		urb = usb_alloc_urb(0, GFP_KERNEL);
		if (urb == NULL) {
			ret = -ENOMEM;
			goto err_urb;
		}
S
Sujith 已提交
905

906 907 908 909 910 911 912
		/* Allocate buffer */
		skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
		if (!skb) {
			ret = -ENOMEM;
			goto err_skb;
		}

913 914
		usb_fill_int_urb(urb, hif_dev->udev,
				  usb_rcvintpipe(hif_dev->udev,
915 916
						  USB_REG_IN_PIPE),
				  skb->data, MAX_REG_IN_BUF_SIZE,
917
				  ath9k_hif_usb_reg_in_cb, skb, 1);
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934

		/* Anchor URB */
		usb_anchor_urb(urb, &hif_dev->reg_in_submitted);

		/* Submit URB */
		ret = usb_submit_urb(urb, GFP_KERNEL);
		if (ret) {
			usb_unanchor_urb(urb);
			goto err_submit;
		}

		/*
		 * Drop reference count.
		 * This ensures that the URB is freed when killing them.
		 */
		usb_free_urb(urb);
	}
S
Sujith 已提交
935 936 937

	return 0;

938 939 940 941 942 943 944
err_submit:
	kfree_skb(skb);
err_skb:
	usb_free_urb(urb);
err_urb:
	ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
	return ret;
S
Sujith 已提交
945 946 947 948
}

static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
{
S
Sujith 已提交
949 950 951
	/* Register Write */
	init_usb_anchor(&hif_dev->regout_submitted);

S
Sujith 已提交
952 953 954 955 956 957
	/* TX */
	if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
		goto err;

	/* RX */
	if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
958
		goto err_rx;
S
Sujith 已提交
959

S
Sujith 已提交
960
	/* Register Read */
961
	if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
962
		goto err_reg;
S
Sujith 已提交
963 964

	return 0;
965 966 967 968
err_reg:
	ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
err_rx:
	ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
S
Sujith 已提交
969 970 971 972
err:
	return -ENOMEM;
}

973 974 975
static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
{
	usb_kill_anchored_urbs(&hif_dev->regout_submitted);
976
	ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
977 978 979 980
	ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
	ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
}

981
static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
S
Sujith 已提交
982 983
{
	int transfer, err;
984 985
	const void *data = hif_dev->fw_data;
	size_t len = hif_dev->fw_size;
S
Sujith 已提交
986 987
	u32 addr = AR9271_FIRMWARE;
	u8 *buf = kzalloc(4096, GFP_KERNEL);
S
Sujith 已提交
988
	u32 firm_offset;
S
Sujith 已提交
989 990 991 992 993

	if (!buf)
		return -ENOMEM;

	while (len) {
D
Dan Carpenter 已提交
994
		transfer = min_t(size_t, len, 4096);
S
Sujith 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
		memcpy(buf, data, transfer);

		err = usb_control_msg(hif_dev->udev,
				      usb_sndctrlpipe(hif_dev->udev, 0),
				      FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
				      addr >> 8, 0, buf, transfer, HZ);
		if (err < 0) {
			kfree(buf);
			return err;
		}

		len -= transfer;
		data += transfer;
		addr += transfer;
	}
	kfree(buf);

1012
	if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
S
Sujith 已提交
1013
		firm_offset = AR7010_FIRMWARE_TEXT;
1014
	else
S
Sujith 已提交
1015 1016
		firm_offset = AR9271_FIRMWARE_TEXT;

S
Sujith 已提交
1017 1018 1019 1020 1021 1022
	/*
	 * Issue FW download complete command to firmware.
	 */
	err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
			      FIRMWARE_DOWNLOAD_COMP,
			      0x40 | USB_DIR_OUT,
S
Sujith 已提交
1023
			      firm_offset >> 8, 0, NULL, 0, HZ);
S
Sujith 已提交
1024 1025 1026 1027
	if (err)
		return -EIO;

	dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
1028
		 hif_dev->fw_name, (unsigned long) hif_dev->fw_size);
S
Sujith 已提交
1029 1030 1031 1032

	return 0;
}

1033
static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
S
Sujith 已提交
1034
{
1035
	int ret;
S
Sujith 已提交
1036

1037
	ret = ath9k_hif_usb_download_fw(hif_dev);
1038 1039
	if (ret) {
		dev_err(&hif_dev->udev->dev,
S
Sujith 已提交
1040 1041
			"ath9k_htc: Firmware - %s download failed\n",
			hif_dev->fw_name);
1042
		return ret;
1043 1044
	}

1045 1046 1047 1048 1049
	/* Alloc URBs */
	ret = ath9k_hif_usb_alloc_urbs(hif_dev);
	if (ret) {
		dev_err(&hif_dev->udev->dev,
			"ath9k_htc: Unable to allocate URBs\n");
1050
		return ret;
1051 1052
	}

S
Sujith 已提交
1053 1054 1055 1056 1057 1058
	return 0;
}

static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
{
	ath9k_hif_usb_dealloc_urbs(hif_dev);
1059 1060 1061 1062 1063 1064 1065 1066
}

/*
 * If initialization fails or the FW cannot be retrieved,
 * detach the device.
 */
static void ath9k_hif_usb_firmware_fail(struct hif_device_usb *hif_dev)
{
1067 1068
	struct device *dev = &hif_dev->udev->dev;
	struct device *parent = dev->parent;
1069

1070
	complete_all(&hif_dev->fw_done);
1071 1072 1073 1074

	if (parent)
		device_lock(parent);

1075
	device_release_driver(dev);
1076 1077 1078 1079 1080

	if (parent)
		device_unlock(parent);
}

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context);

/* taken from iwlwifi */
static int ath9k_hif_request_firmware(struct hif_device_usb *hif_dev,
				      bool first)
{
	char index[8], *chip;
	int ret;

	if (first) {
		if (htc_use_dev_fw) {
			hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX + 1;
			sprintf(index, "%s", "dev");
		} else {
			hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX;
			sprintf(index, "%d", hif_dev->fw_minor_index);
		}
	} else {
		hif_dev->fw_minor_index--;
		sprintf(index, "%d", hif_dev->fw_minor_index);
	}

	/* test for FW 1.3 */
	if (MAJOR_VERSION_REQ == 1 && hif_dev->fw_minor_index == 3) {
		const char *filename;

		if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
			filename = FIRMWARE_AR7010_1_1;
		else
			filename = FIRMWARE_AR9271;

		/* expected fw locations:
		 * - htc_9271.fw   (stable version 1.3, depricated)
		 */
		snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
			 "%s", filename);

	} else if (hif_dev->fw_minor_index < FIRMWARE_MINOR_IDX_MIN) {
		dev_err(&hif_dev->udev->dev, "no suitable firmware found!\n");

		return -ENOENT;
	} else {
		if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
			chip = "7010";
		else
			chip = "9271";

		/* expected fw locations:
		 * - ath9k_htc/htc_9271-1.dev.0.fw (development version)
		 * - ath9k_htc/htc_9271-1.4.0.fw   (stable version)
		 */
		snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
			 "%s/htc_%s-%d.%s.0.fw", HTC_FW_PATH,
			 chip, MAJOR_VERSION_REQ, index);
	}

	ret = request_firmware_nowait(THIS_MODULE, true, hif_dev->fw_name,
				      &hif_dev->udev->dev, GFP_KERNEL,
				      hif_dev, ath9k_hif_usb_firmware_cb);
	if (ret) {
		dev_err(&hif_dev->udev->dev,
			"ath9k_htc: Async request for firmware %s failed\n",
			hif_dev->fw_name);
		return ret;
	}

	dev_info(&hif_dev->udev->dev, "ath9k_htc: Firmware %s requested\n",
		 hif_dev->fw_name);

	return ret;
}

1153 1154 1155 1156 1157 1158
static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context)
{
	struct hif_device_usb *hif_dev = context;
	int ret;

	if (!fw) {
1159 1160 1161 1162
		ret = ath9k_hif_request_firmware(hif_dev, false);
		if (!ret)
			return;

1163 1164 1165 1166 1167 1168 1169 1170
		dev_err(&hif_dev->udev->dev,
			"ath9k_htc: Failed to get firmware %s\n",
			hif_dev->fw_name);
		goto err_fw;
	}

	hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
						 &hif_dev->udev->dev);
1171 1172
	if (hif_dev->htc_handle == NULL)
		goto err_dev_alloc;
1173

1174 1175
	hif_dev->fw_data = fw->data;
	hif_dev->fw_size = fw->size;
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192

	/* Proceed with initialization */

	ret = ath9k_hif_usb_dev_init(hif_dev);
	if (ret)
		goto err_dev_init;

	ret = ath9k_htc_hw_init(hif_dev->htc_handle,
				&hif_dev->interface->dev,
				hif_dev->usb_device_id->idProduct,
				hif_dev->udev->product,
				hif_dev->usb_device_id->driver_info);
	if (ret) {
		ret = -EINVAL;
		goto err_htc_hw_init;
	}

1193 1194
	release_firmware(fw);
	hif_dev->flags |= HIF_USB_READY;
1195
	complete_all(&hif_dev->fw_done);
1196 1197 1198 1199 1200 1201 1202

	return;

err_htc_hw_init:
	ath9k_hif_usb_dev_deinit(hif_dev);
err_dev_init:
	ath9k_htc_hw_free(hif_dev->htc_handle);
1203
err_dev_alloc:
1204 1205 1206
	release_firmware(fw);
err_fw:
	ath9k_hif_usb_firmware_fail(hif_dev);
S
Sujith 已提交
1207 1208
}

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
/*
 * An exact copy of the function from zd1211rw.
 */
static int send_eject_command(struct usb_interface *interface)
{
	struct usb_device *udev = interface_to_usbdev(interface);
	struct usb_host_interface *iface_desc = &interface->altsetting[0];
	struct usb_endpoint_descriptor *endpoint;
	unsigned char *cmd;
	u8 bulk_out_ep;
	int r;

	/* Find bulk out endpoint */
	for (r = 1; r >= 0; r--) {
		endpoint = &iface_desc->endpoint[r].desc;
		if (usb_endpoint_dir_out(endpoint) &&
		    usb_endpoint_xfer_bulk(endpoint)) {
			bulk_out_ep = endpoint->bEndpointAddress;
			break;
		}
	}
	if (r == -1) {
		dev_err(&udev->dev,
			"ath9k_htc: Could not find bulk out endpoint\n");
		return -ENODEV;
	}

	cmd = kzalloc(31, GFP_KERNEL);
	if (cmd == NULL)
		return -ENODEV;

	/* USB bulk command block */
	cmd[0] = 0x55;	/* bulk command signature */
	cmd[1] = 0x53;	/* bulk command signature */
	cmd[2] = 0x42;	/* bulk command signature */
	cmd[3] = 0x43;	/* bulk command signature */
	cmd[14] = 6;	/* command length */

	cmd[15] = 0x1b;	/* SCSI command: START STOP UNIT */
	cmd[19] = 0x2;	/* eject disc */

	dev_info(&udev->dev, "Ejecting storage device...\n");
	r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
		cmd, 31, NULL, 2000);
	kfree(cmd);
	if (r)
		return r;

	/* At this point, the device disconnects and reconnects with the real
	 * ID numbers. */

	usb_set_intfdata(interface, NULL);
	return 0;
}

S
Sujith 已提交
1264 1265 1266 1267 1268 1269 1270
static int ath9k_hif_usb_probe(struct usb_interface *interface,
			       const struct usb_device_id *id)
{
	struct usb_device *udev = interface_to_usbdev(interface);
	struct hif_device_usb *hif_dev;
	int ret = 0;

1271 1272 1273
	if (id->driver_info == STORAGE_DEVICE)
		return send_eject_command(interface);

S
Sujith 已提交
1274 1275 1276 1277 1278 1279 1280
	hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
	if (!hif_dev) {
		ret = -ENOMEM;
		goto err_alloc;
	}

	usb_get_dev(udev);
1281

S
Sujith 已提交
1282 1283
	hif_dev->udev = udev;
	hif_dev->interface = interface;
1284
	hif_dev->usb_device_id = id;
S
Sujith 已提交
1285 1286 1287 1288 1289
#ifdef CONFIG_PM
	udev->reset_resume = 1;
#endif
	usb_set_intfdata(interface, hif_dev);

1290
	init_completion(&hif_dev->fw_done);
1291

1292 1293
	ret = ath9k_hif_request_firmware(hif_dev, true);
	if (ret)
1294
		goto err_fw_req;
S
Sujith 已提交
1295

1296
	return ret;
S
Sujith 已提交
1297

1298
err_fw_req:
S
Sujith 已提交
1299 1300 1301 1302 1303 1304 1305
	usb_set_intfdata(interface, NULL);
	kfree(hif_dev);
	usb_put_dev(udev);
err_alloc:
	return ret;
}

S
Sujith 已提交
1306 1307 1308 1309 1310 1311
static void ath9k_hif_usb_reboot(struct usb_device *udev)
{
	u32 reboot_cmd = 0xffffffff;
	void *buf;
	int ret;

1312
	buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
S
Sujith 已提交
1313 1314 1315
	if (!buf)
		return;

1316
	ret = usb_interrupt_msg(udev, usb_sndintpipe(udev, USB_REG_OUT_PIPE),
S
Sujith 已提交
1317 1318 1319 1320 1321 1322 1323
			   buf, 4, NULL, HZ);
	if (ret)
		dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");

	kfree(buf);
}

S
Sujith 已提交
1324 1325 1326
static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
{
	struct usb_device *udev = interface_to_usbdev(interface);
1327
	struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1328
	bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
S
Sujith 已提交
1329

1330 1331 1332
	if (!hif_dev)
		return;

1333 1334
	wait_for_completion(&hif_dev->fw_done);

1335
	if (hif_dev->flags & HIF_USB_READY) {
1336 1337 1338 1339 1340
		ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
		ath9k_htc_hw_free(hif_dev->htc_handle);
		ath9k_hif_usb_dev_deinit(hif_dev);
	}

1341
	usb_set_intfdata(interface, NULL);
S
Sujith 已提交
1342

1343 1344 1345
	/* If firmware was loaded we should drop it
	 * go back to first stage bootloader. */
	if (!unplugged && (hif_dev->flags & HIF_USB_READY))
S
Sujith 已提交
1346
		ath9k_hif_usb_reboot(udev);
S
Sujith 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

	kfree(hif_dev);
	dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
	usb_put_dev(udev);
}

#ifdef CONFIG_PM
static int ath9k_hif_usb_suspend(struct usb_interface *interface,
				 pm_message_t message)
{
1357
	struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
S
Sujith 已提交
1358

S
Sujith Manoharan 已提交
1359 1360 1361 1362 1363 1364 1365
	/*
	 * The device has to be set to FULLSLEEP mode in case no
	 * interface is up.
	 */
	if (!(hif_dev->flags & HIF_USB_START))
		ath9k_htc_suspend(hif_dev->htc_handle);

1366 1367 1368 1369
	wait_for_completion(&hif_dev->fw_done);

	if (hif_dev->flags & HIF_USB_READY)
		ath9k_hif_usb_dealloc_urbs(hif_dev);
S
Sujith 已提交
1370 1371 1372 1373 1374 1375

	return 0;
}

static int ath9k_hif_usb_resume(struct usb_interface *interface)
{
1376
	struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1377
	struct htc_target *htc_handle = hif_dev->htc_handle;
S
Sujith 已提交
1378
	int ret;
1379
	const struct firmware *fw;
S
Sujith 已提交
1380 1381 1382 1383 1384

	ret = ath9k_hif_usb_alloc_urbs(hif_dev);
	if (ret)
		return ret;

1385 1386 1387 1388 1389 1390 1391 1392 1393
	if (hif_dev->flags & HIF_USB_READY) {
		/* request cached firmware during suspend/resume cycle */
		ret = request_firmware(&fw, hif_dev->fw_name,
				       &hif_dev->udev->dev);
		if (ret)
			goto fail_resume;

		hif_dev->fw_data = fw->data;
		hif_dev->fw_size = fw->size;
1394
		ret = ath9k_hif_usb_download_fw(hif_dev);
1395
		release_firmware(fw);
S
Sujith 已提交
1396 1397 1398 1399 1400 1401 1402 1403 1404
		if (ret)
			goto fail_resume;
	} else {
		ath9k_hif_usb_dealloc_urbs(hif_dev);
		return -EIO;
	}

	mdelay(100);

1405
	ret = ath9k_htc_resume(htc_handle);
S
Sujith 已提交
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419

	if (ret)
		goto fail_resume;

	return 0;

fail_resume:
	ath9k_hif_usb_dealloc_urbs(hif_dev);

	return ret;
}
#endif

static struct usb_driver ath9k_hif_usb_driver = {
1420
	.name = KBUILD_MODNAME,
S
Sujith 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429
	.probe = ath9k_hif_usb_probe,
	.disconnect = ath9k_hif_usb_disconnect,
#ifdef CONFIG_PM
	.suspend = ath9k_hif_usb_suspend,
	.resume = ath9k_hif_usb_resume,
	.reset_resume = ath9k_hif_usb_resume,
#endif
	.id_table = ath9k_hif_usb_ids,
	.soft_unbind = 1,
1430
	.disable_hub_initiated_lpm = 1,
S
Sujith 已提交
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
};

int ath9k_hif_usb_init(void)
{
	return usb_register(&ath9k_hif_usb_driver);
}

void ath9k_hif_usb_exit(void)
{
	usb_deregister(&ath9k_hif_usb_driver);
}