1 /*
2  * Copyright (c) 2010-2011 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <asm/unaligned.h>
18 #include "htc.h"
19 
20 MODULE_FIRMWARE(HTC_7010_MODULE_FW);
21 MODULE_FIRMWARE(HTC_9271_MODULE_FW);
22 
23 static const struct usb_device_id ath9k_hif_usb_ids[] = {
24 	{ USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
25 	{ USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
26 	{ USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
27 	{ USB_DEVICE(0x07b8, 0x9271) }, /* Altai WA1011N-GU */
28 	{ USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
29 	{ USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
30 	{ USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
31 	{ USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
32 	{ USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
33 	{ USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
34 	{ USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
35 	{ USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
36 	{ USB_DEVICE(0x040D, 0x3801) }, /* VIA */
37 	{ USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
38 	{ USB_DEVICE(0x0cf3, 0xb002) }, /* Ubiquiti WifiStation */
39 	{ USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */
40 	{ USB_DEVICE(0x0471, 0x209e) }, /* Philips (or NXP) PTA01 */
41 	{ USB_DEVICE(0x1eda, 0x2315) }, /* AirTies */
42 
43 	{ USB_DEVICE(0x0cf3, 0x7015),
44 	  .driver_info = AR9287_USB },  /* Atheros */
45 	{ USB_DEVICE(0x1668, 0x1200),
46 	  .driver_info = AR9287_USB },  /* Verizon */
47 
48 	{ USB_DEVICE(0x0cf3, 0x7010),
49 	  .driver_info = AR9280_USB },  /* Atheros */
50 	{ USB_DEVICE(0x0846, 0x9018),
51 	  .driver_info = AR9280_USB },  /* Netgear WNDA3200 */
52 	{ USB_DEVICE(0x083A, 0xA704),
53 	  .driver_info = AR9280_USB },  /* SMC Networks */
54 	{ USB_DEVICE(0x0411, 0x017f),
55 	  .driver_info = AR9280_USB },  /* Sony UWA-BR100 */
56 	{ USB_DEVICE(0x0411, 0x0197),
57 	  .driver_info = AR9280_USB },  /* Buffalo WLI-UV-AG300P */
58 	{ USB_DEVICE(0x04da, 0x3904),
59 	  .driver_info = AR9280_USB },
60 	{ USB_DEVICE(0x0930, 0x0a08),
61 	  .driver_info = AR9280_USB },  /* Toshiba WLM-20U2 and GN-1080 */
62 
63 	{ USB_DEVICE(0x0cf3, 0x20ff),
64 	  .driver_info = STORAGE_DEVICE },
65 
66 	{ },
67 };
68 
69 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
70 
71 static int __hif_usb_tx(struct hif_device_usb *hif_dev);
72 
73 static void hif_usb_regout_cb(struct urb *urb)
74 {
75 	struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
76 
77 	switch (urb->status) {
78 	case 0:
79 		break;
80 	case -ENOENT:
81 	case -ECONNRESET:
82 	case -ENODEV:
83 	case -ESHUTDOWN:
84 		goto free;
85 	default:
86 		break;
87 	}
88 
89 	if (cmd) {
90 		ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
91 					  cmd->skb, true);
92 		kfree(cmd);
93 	}
94 
95 	return;
96 free:
97 	kfree_skb(cmd->skb);
98 	kfree(cmd);
99 }
100 
101 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
102 			       struct sk_buff *skb)
103 {
104 	struct urb *urb;
105 	struct cmd_buf *cmd;
106 	int ret = 0;
107 
108 	urb = usb_alloc_urb(0, GFP_KERNEL);
109 	if (urb == NULL)
110 		return -ENOMEM;
111 
112 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
113 	if (cmd == NULL) {
114 		usb_free_urb(urb);
115 		return -ENOMEM;
116 	}
117 
118 	cmd->skb = skb;
119 	cmd->hif_dev = hif_dev;
120 
121 	usb_fill_int_urb(urb, hif_dev->udev,
122 			 usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
123 			 skb->data, skb->len,
124 			 hif_usb_regout_cb, cmd, 1);
125 
126 	usb_anchor_urb(urb, &hif_dev->regout_submitted);
127 	ret = usb_submit_urb(urb, GFP_KERNEL);
128 	if (ret) {
129 		usb_unanchor_urb(urb);
130 		kfree(cmd);
131 	}
132 	usb_free_urb(urb);
133 
134 	return ret;
135 }
136 
137 static void hif_usb_mgmt_cb(struct urb *urb)
138 {
139 	struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
140 	struct hif_device_usb *hif_dev;
141 	unsigned long flags;
142 	bool txok = true;
143 
144 	if (!cmd || !cmd->skb || !cmd->hif_dev)
145 		return;
146 
147 	hif_dev = cmd->hif_dev;
148 
149 	switch (urb->status) {
150 	case 0:
151 		break;
152 	case -ENOENT:
153 	case -ECONNRESET:
154 	case -ENODEV:
155 	case -ESHUTDOWN:
156 		txok = false;
157 
158 		/*
159 		 * If the URBs are being flushed, no need to complete
160 		 * this packet.
161 		 */
162 		spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
163 		if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
164 			spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
165 			dev_kfree_skb_any(cmd->skb);
166 			kfree(cmd);
167 			return;
168 		}
169 		spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
170 
171 		break;
172 	default:
173 		txok = false;
174 		break;
175 	}
176 
177 	skb_pull(cmd->skb, 4);
178 	ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
179 				  cmd->skb, txok);
180 	kfree(cmd);
181 }
182 
183 static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev,
184 			     struct sk_buff *skb)
185 {
186 	struct urb *urb;
187 	struct cmd_buf *cmd;
188 	int ret = 0;
189 	__le16 *hdr;
190 
191 	urb = usb_alloc_urb(0, GFP_ATOMIC);
192 	if (urb == NULL)
193 		return -ENOMEM;
194 
195 	cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
196 	if (cmd == NULL) {
197 		usb_free_urb(urb);
198 		return -ENOMEM;
199 	}
200 
201 	cmd->skb = skb;
202 	cmd->hif_dev = hif_dev;
203 
204 	hdr = skb_push(skb, 4);
205 	*hdr++ = cpu_to_le16(skb->len - 4);
206 	*hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
207 
208 	usb_fill_bulk_urb(urb, hif_dev->udev,
209 			 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
210 			 skb->data, skb->len,
211 			 hif_usb_mgmt_cb, cmd);
212 
213 	usb_anchor_urb(urb, &hif_dev->mgmt_submitted);
214 	ret = usb_submit_urb(urb, GFP_ATOMIC);
215 	if (ret) {
216 		usb_unanchor_urb(urb);
217 		kfree(cmd);
218 	}
219 	usb_free_urb(urb);
220 
221 	return ret;
222 }
223 
224 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
225 					 struct sk_buff_head *list)
226 {
227 	struct sk_buff *skb;
228 
229 	while ((skb = __skb_dequeue(list)) != NULL) {
230 		dev_kfree_skb_any(skb);
231 	}
232 }
233 
234 static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
235 					    struct sk_buff_head *queue,
236 					    bool txok)
237 {
238 	struct sk_buff *skb;
239 
240 	while ((skb = __skb_dequeue(queue)) != NULL) {
241 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
242 		int ln = skb->len;
243 #endif
244 		ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
245 					  skb, txok);
246 		if (txok) {
247 			TX_STAT_INC(skb_success);
248 			TX_STAT_ADD(skb_success_bytes, ln);
249 		}
250 		else
251 			TX_STAT_INC(skb_failed);
252 	}
253 }
254 
255 static void hif_usb_tx_cb(struct urb *urb)
256 {
257 	struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
258 	struct hif_device_usb *hif_dev;
259 	bool txok = true;
260 
261 	if (!tx_buf || !tx_buf->hif_dev)
262 		return;
263 
264 	hif_dev = tx_buf->hif_dev;
265 
266 	switch (urb->status) {
267 	case 0:
268 		break;
269 	case -ENOENT:
270 	case -ECONNRESET:
271 	case -ENODEV:
272 	case -ESHUTDOWN:
273 		txok = false;
274 
275 		/*
276 		 * If the URBs are being flushed, no need to add this
277 		 * URB to the free list.
278 		 */
279 		spin_lock(&hif_dev->tx.tx_lock);
280 		if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
281 			spin_unlock(&hif_dev->tx.tx_lock);
282 			ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
283 			return;
284 		}
285 		spin_unlock(&hif_dev->tx.tx_lock);
286 
287 		break;
288 	default:
289 		txok = false;
290 		break;
291 	}
292 
293 	ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
294 
295 	/* Re-initialize the SKB queue */
296 	tx_buf->len = tx_buf->offset = 0;
297 	__skb_queue_head_init(&tx_buf->skb_queue);
298 
299 	/* Add this TX buffer to the free list */
300 	spin_lock(&hif_dev->tx.tx_lock);
301 	list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
302 	hif_dev->tx.tx_buf_cnt++;
303 	if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
304 		__hif_usb_tx(hif_dev); /* Check for pending SKBs */
305 	TX_STAT_INC(buf_completed);
306 	spin_unlock(&hif_dev->tx.tx_lock);
307 }
308 
309 /* TX lock has to be taken */
310 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
311 {
312 	struct tx_buf *tx_buf = NULL;
313 	struct sk_buff *nskb = NULL;
314 	int ret = 0, i;
315 	u16 tx_skb_cnt = 0;
316 	u8 *buf;
317 	__le16 *hdr;
318 
319 	if (hif_dev->tx.tx_skb_cnt == 0)
320 		return 0;
321 
322 	/* Check if a free TX buffer is available */
323 	if (list_empty(&hif_dev->tx.tx_buf))
324 		return 0;
325 
326 	tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
327 	list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
328 	hif_dev->tx.tx_buf_cnt--;
329 
330 	tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
331 
332 	for (i = 0; i < tx_skb_cnt; i++) {
333 		nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
334 
335 		/* Should never be NULL */
336 		BUG_ON(!nskb);
337 
338 		hif_dev->tx.tx_skb_cnt--;
339 
340 		buf = tx_buf->buf;
341 		buf += tx_buf->offset;
342 		hdr = (__le16 *)buf;
343 		*hdr++ = cpu_to_le16(nskb->len);
344 		*hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
345 		buf += 4;
346 		memcpy(buf, nskb->data, nskb->len);
347 		tx_buf->len = nskb->len + 4;
348 
349 		if (i < (tx_skb_cnt - 1))
350 			tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
351 
352 		if (i == (tx_skb_cnt - 1))
353 			tx_buf->len += tx_buf->offset;
354 
355 		__skb_queue_tail(&tx_buf->skb_queue, nskb);
356 		TX_STAT_INC(skb_queued);
357 	}
358 
359 	usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
360 			  usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
361 			  tx_buf->buf, tx_buf->len,
362 			  hif_usb_tx_cb, tx_buf);
363 
364 	ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
365 	if (ret) {
366 		tx_buf->len = tx_buf->offset = 0;
367 		ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
368 		__skb_queue_head_init(&tx_buf->skb_queue);
369 		list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
370 		hif_dev->tx.tx_buf_cnt++;
371 	}
372 
373 	if (!ret)
374 		TX_STAT_INC(buf_queued);
375 
376 	return ret;
377 }
378 
379 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
380 {
381 	struct ath9k_htc_tx_ctl *tx_ctl;
382 	unsigned long flags;
383 	int ret = 0;
384 
385 	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
386 
387 	if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
388 		spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
389 		return -ENODEV;
390 	}
391 
392 	/* Check if the max queue count has been reached */
393 	if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
394 		spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
395 		return -ENOMEM;
396 	}
397 
398 	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
399 
400 	tx_ctl = HTC_SKB_CB(skb);
401 
402 	/* Mgmt/Beacon frames don't use the TX buffer pool */
403 	if ((tx_ctl->type == ATH9K_HTC_MGMT) ||
404 	    (tx_ctl->type == ATH9K_HTC_BEACON)) {
405 		ret = hif_usb_send_mgmt(hif_dev, skb);
406 	}
407 
408 	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
409 
410 	if ((tx_ctl->type == ATH9K_HTC_NORMAL) ||
411 	    (tx_ctl->type == ATH9K_HTC_AMPDU)) {
412 		__skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
413 		hif_dev->tx.tx_skb_cnt++;
414 	}
415 
416 	/* Check if AMPDUs have to be sent immediately */
417 	if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
418 	    (hif_dev->tx.tx_skb_cnt < 2)) {
419 		__hif_usb_tx(hif_dev);
420 	}
421 
422 	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
423 
424 	return ret;
425 }
426 
427 static void hif_usb_start(void *hif_handle)
428 {
429 	struct hif_device_usb *hif_dev = hif_handle;
430 	unsigned long flags;
431 
432 	hif_dev->flags |= HIF_USB_START;
433 
434 	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
435 	hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
436 	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
437 }
438 
439 static void hif_usb_stop(void *hif_handle)
440 {
441 	struct hif_device_usb *hif_dev = hif_handle;
442 	struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
443 	unsigned long flags;
444 
445 	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
446 	ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
447 	hif_dev->tx.tx_skb_cnt = 0;
448 	hif_dev->tx.flags |= HIF_USB_TX_STOP;
449 	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
450 
451 	/* The pending URBs have to be canceled. */
452 	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
453 	list_for_each_entry_safe(tx_buf, tx_buf_tmp,
454 				 &hif_dev->tx.tx_pending, list) {
455 		usb_get_urb(tx_buf->urb);
456 		spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
457 		usb_kill_urb(tx_buf->urb);
458 		list_del(&tx_buf->list);
459 		usb_free_urb(tx_buf->urb);
460 		kfree(tx_buf->buf);
461 		kfree(tx_buf);
462 		spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
463 	}
464 	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
465 
466 	usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
467 }
468 
469 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
470 {
471 	struct hif_device_usb *hif_dev = hif_handle;
472 	int ret = 0;
473 
474 	switch (pipe_id) {
475 	case USB_WLAN_TX_PIPE:
476 		ret = hif_usb_send_tx(hif_dev, skb);
477 		break;
478 	case USB_REG_OUT_PIPE:
479 		ret = hif_usb_send_regout(hif_dev, skb);
480 		break;
481 	default:
482 		dev_err(&hif_dev->udev->dev,
483 			"ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
484 		ret = -EINVAL;
485 		break;
486 	}
487 
488 	return ret;
489 }
490 
491 static inline bool check_index(struct sk_buff *skb, u8 idx)
492 {
493 	struct ath9k_htc_tx_ctl *tx_ctl;
494 
495 	tx_ctl = HTC_SKB_CB(skb);
496 
497 	if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
498 	    (tx_ctl->sta_idx == idx))
499 		return true;
500 
501 	return false;
502 }
503 
504 static void hif_usb_sta_drain(void *hif_handle, u8 idx)
505 {
506 	struct hif_device_usb *hif_dev = hif_handle;
507 	struct sk_buff *skb, *tmp;
508 	unsigned long flags;
509 
510 	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
511 
512 	skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
513 		if (check_index(skb, idx)) {
514 			__skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
515 			ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
516 						  skb, false);
517 			hif_dev->tx.tx_skb_cnt--;
518 			TX_STAT_INC(skb_failed);
519 		}
520 	}
521 
522 	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
523 }
524 
525 static struct ath9k_htc_hif hif_usb = {
526 	.transport = ATH9K_HIF_USB,
527 	.name = "ath9k_hif_usb",
528 
529 	.control_ul_pipe = USB_REG_OUT_PIPE,
530 	.control_dl_pipe = USB_REG_IN_PIPE,
531 
532 	.start = hif_usb_start,
533 	.stop = hif_usb_stop,
534 	.sta_drain = hif_usb_sta_drain,
535 	.send = hif_usb_send,
536 };
537 
538 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
539 				    struct sk_buff *skb)
540 {
541 	struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
542 	int index = 0, i, len = skb->len;
543 	int rx_remain_len, rx_pkt_len;
544 	u16 pool_index = 0;
545 	u8 *ptr;
546 
547 	spin_lock(&hif_dev->rx_lock);
548 
549 	rx_remain_len = hif_dev->rx_remain_len;
550 	rx_pkt_len = hif_dev->rx_transfer_len;
551 
552 	if (rx_remain_len != 0) {
553 		struct sk_buff *remain_skb = hif_dev->remain_skb;
554 
555 		if (remain_skb) {
556 			ptr = (u8 *) remain_skb->data;
557 
558 			index = rx_remain_len;
559 			rx_remain_len -= hif_dev->rx_pad_len;
560 			ptr += rx_pkt_len;
561 
562 			memcpy(ptr, skb->data, rx_remain_len);
563 
564 			rx_pkt_len += rx_remain_len;
565 			hif_dev->rx_remain_len = 0;
566 			skb_put(remain_skb, rx_pkt_len);
567 
568 			skb_pool[pool_index++] = remain_skb;
569 
570 		} else {
571 			index = rx_remain_len;
572 		}
573 	}
574 
575 	spin_unlock(&hif_dev->rx_lock);
576 
577 	while (index < len) {
578 		u16 pkt_len;
579 		u16 pkt_tag;
580 		u16 pad_len;
581 		int chk_idx;
582 
583 		ptr = (u8 *) skb->data;
584 
585 		pkt_len = get_unaligned_le16(ptr + index);
586 		pkt_tag = get_unaligned_le16(ptr + index + 2);
587 
588 		if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
589 			RX_STAT_INC(skb_dropped);
590 			return;
591 		}
592 
593 		pad_len = 4 - (pkt_len & 0x3);
594 		if (pad_len == 4)
595 			pad_len = 0;
596 
597 		chk_idx = index;
598 		index = index + 4 + pkt_len + pad_len;
599 
600 		if (index > MAX_RX_BUF_SIZE) {
601 			spin_lock(&hif_dev->rx_lock);
602 			hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
603 			hif_dev->rx_transfer_len =
604 				MAX_RX_BUF_SIZE - chk_idx - 4;
605 			hif_dev->rx_pad_len = pad_len;
606 
607 			nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
608 			if (!nskb) {
609 				dev_err(&hif_dev->udev->dev,
610 					"ath9k_htc: RX memory allocation error\n");
611 				spin_unlock(&hif_dev->rx_lock);
612 				goto err;
613 			}
614 			skb_reserve(nskb, 32);
615 			RX_STAT_INC(skb_allocated);
616 
617 			memcpy(nskb->data, &(skb->data[chk_idx+4]),
618 			       hif_dev->rx_transfer_len);
619 
620 			/* Record the buffer pointer */
621 			hif_dev->remain_skb = nskb;
622 			spin_unlock(&hif_dev->rx_lock);
623 		} else {
624 			if (pool_index == MAX_PKT_NUM_IN_TRANSFER) {
625 				dev_err(&hif_dev->udev->dev,
626 					"ath9k_htc: over RX MAX_PKT_NUM\n");
627 				goto err;
628 			}
629 			nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
630 			if (!nskb) {
631 				dev_err(&hif_dev->udev->dev,
632 					"ath9k_htc: RX memory allocation error\n");
633 				goto err;
634 			}
635 			skb_reserve(nskb, 32);
636 			RX_STAT_INC(skb_allocated);
637 
638 			memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
639 			skb_put(nskb, pkt_len);
640 			skb_pool[pool_index++] = nskb;
641 		}
642 	}
643 
644 err:
645 	for (i = 0; i < pool_index; i++) {
646 		RX_STAT_ADD(skb_completed_bytes, skb_pool[i]->len);
647 		ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
648 				 skb_pool[i]->len, USB_WLAN_RX_PIPE);
649 		RX_STAT_INC(skb_completed);
650 	}
651 }
652 
653 static void ath9k_hif_usb_rx_cb(struct urb *urb)
654 {
655 	struct rx_buf *rx_buf = (struct rx_buf *)urb->context;
656 	struct hif_device_usb *hif_dev = rx_buf->hif_dev;
657 	struct sk_buff *skb = rx_buf->skb;
658 	int ret;
659 
660 	if (!skb)
661 		return;
662 
663 	if (!hif_dev)
664 		goto free;
665 
666 	switch (urb->status) {
667 	case 0:
668 		break;
669 	case -ENOENT:
670 	case -ECONNRESET:
671 	case -ENODEV:
672 	case -ESHUTDOWN:
673 		goto free;
674 	default:
675 		goto resubmit;
676 	}
677 
678 	if (likely(urb->actual_length != 0)) {
679 		skb_put(skb, urb->actual_length);
680 		ath9k_hif_usb_rx_stream(hif_dev, skb);
681 	}
682 
683 resubmit:
684 	skb_reset_tail_pointer(skb);
685 	skb_trim(skb, 0);
686 
687 	usb_anchor_urb(urb, &hif_dev->rx_submitted);
688 	ret = usb_submit_urb(urb, GFP_ATOMIC);
689 	if (ret) {
690 		usb_unanchor_urb(urb);
691 		goto free;
692 	}
693 
694 	return;
695 free:
696 	kfree_skb(skb);
697 	kfree(rx_buf);
698 }
699 
700 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
701 {
702 	struct rx_buf *rx_buf = (struct rx_buf *)urb->context;
703 	struct hif_device_usb *hif_dev = rx_buf->hif_dev;
704 	struct sk_buff *skb = rx_buf->skb;
705 	struct sk_buff *nskb;
706 	int ret;
707 
708 	if (!skb)
709 		return;
710 
711 	if (!hif_dev)
712 		goto free;
713 
714 	switch (urb->status) {
715 	case 0:
716 		break;
717 	case -ENOENT:
718 	case -ECONNRESET:
719 	case -ENODEV:
720 	case -ESHUTDOWN:
721 		goto free;
722 	default:
723 		skb_reset_tail_pointer(skb);
724 		skb_trim(skb, 0);
725 
726 		goto resubmit;
727 	}
728 
729 	if (likely(urb->actual_length != 0)) {
730 		skb_put(skb, urb->actual_length);
731 
732 		/* Process the command first */
733 		ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
734 				 skb->len, USB_REG_IN_PIPE);
735 
736 
737 		nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
738 		if (!nskb) {
739 			dev_err(&hif_dev->udev->dev,
740 				"ath9k_htc: REG_IN memory allocation failure\n");
741 			urb->context = NULL;
742 			return;
743 		}
744 
745 		rx_buf->skb = nskb;
746 
747 		usb_fill_int_urb(urb, hif_dev->udev,
748 				 usb_rcvintpipe(hif_dev->udev,
749 						 USB_REG_IN_PIPE),
750 				 nskb->data, MAX_REG_IN_BUF_SIZE,
751 				 ath9k_hif_usb_reg_in_cb, rx_buf, 1);
752 	}
753 
754 resubmit:
755 	usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
756 	ret = usb_submit_urb(urb, GFP_ATOMIC);
757 	if (ret) {
758 		usb_unanchor_urb(urb);
759 		goto free;
760 	}
761 
762 	return;
763 free:
764 	kfree_skb(skb);
765 	kfree(rx_buf);
766 	urb->context = NULL;
767 }
768 
769 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
770 {
771 	struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
772 	unsigned long flags;
773 
774 	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
775 	list_for_each_entry_safe(tx_buf, tx_buf_tmp,
776 				 &hif_dev->tx.tx_buf, list) {
777 		usb_get_urb(tx_buf->urb);
778 		spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
779 		usb_kill_urb(tx_buf->urb);
780 		list_del(&tx_buf->list);
781 		usb_free_urb(tx_buf->urb);
782 		kfree(tx_buf->buf);
783 		kfree(tx_buf);
784 		spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
785 	}
786 	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
787 
788 	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
789 	hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
790 	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
791 
792 	spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
793 	list_for_each_entry_safe(tx_buf, tx_buf_tmp,
794 				 &hif_dev->tx.tx_pending, list) {
795 		usb_get_urb(tx_buf->urb);
796 		spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
797 		usb_kill_urb(tx_buf->urb);
798 		list_del(&tx_buf->list);
799 		usb_free_urb(tx_buf->urb);
800 		kfree(tx_buf->buf);
801 		kfree(tx_buf);
802 		spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
803 	}
804 	spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
805 
806 	usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
807 }
808 
809 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
810 {
811 	struct tx_buf *tx_buf;
812 	int i;
813 
814 	INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
815 	INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
816 	spin_lock_init(&hif_dev->tx.tx_lock);
817 	__skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
818 	init_usb_anchor(&hif_dev->mgmt_submitted);
819 
820 	for (i = 0; i < MAX_TX_URB_NUM; i++) {
821 		tx_buf = kzalloc(sizeof(*tx_buf), GFP_KERNEL);
822 		if (!tx_buf)
823 			goto err;
824 
825 		tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
826 		if (!tx_buf->buf)
827 			goto err;
828 
829 		tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
830 		if (!tx_buf->urb)
831 			goto err;
832 
833 		tx_buf->hif_dev = hif_dev;
834 		__skb_queue_head_init(&tx_buf->skb_queue);
835 
836 		list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
837 	}
838 
839 	hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
840 
841 	return 0;
842 err:
843 	if (tx_buf) {
844 		kfree(tx_buf->buf);
845 		kfree(tx_buf);
846 	}
847 	ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
848 	return -ENOMEM;
849 }
850 
851 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
852 {
853 	usb_kill_anchored_urbs(&hif_dev->rx_submitted);
854 }
855 
856 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
857 {
858 	struct rx_buf *rx_buf = NULL;
859 	struct sk_buff *skb = NULL;
860 	struct urb *urb = NULL;
861 	int i, ret;
862 
863 	init_usb_anchor(&hif_dev->rx_submitted);
864 	spin_lock_init(&hif_dev->rx_lock);
865 
866 	for (i = 0; i < MAX_RX_URB_NUM; i++) {
867 
868 		rx_buf = kzalloc(sizeof(*rx_buf), GFP_KERNEL);
869 		if (!rx_buf) {
870 			ret = -ENOMEM;
871 			goto err_rxb;
872 		}
873 
874 		/* Allocate URB */
875 		urb = usb_alloc_urb(0, GFP_KERNEL);
876 		if (urb == NULL) {
877 			ret = -ENOMEM;
878 			goto err_urb;
879 		}
880 
881 		/* Allocate buffer */
882 		skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
883 		if (!skb) {
884 			ret = -ENOMEM;
885 			goto err_skb;
886 		}
887 
888 		rx_buf->hif_dev = hif_dev;
889 		rx_buf->skb = skb;
890 
891 		usb_fill_bulk_urb(urb, hif_dev->udev,
892 				  usb_rcvbulkpipe(hif_dev->udev,
893 						  USB_WLAN_RX_PIPE),
894 				  skb->data, MAX_RX_BUF_SIZE,
895 				  ath9k_hif_usb_rx_cb, rx_buf);
896 
897 		/* Anchor URB */
898 		usb_anchor_urb(urb, &hif_dev->rx_submitted);
899 
900 		/* Submit URB */
901 		ret = usb_submit_urb(urb, GFP_KERNEL);
902 		if (ret) {
903 			usb_unanchor_urb(urb);
904 			goto err_submit;
905 		}
906 
907 		/*
908 		 * Drop reference count.
909 		 * This ensures that the URB is freed when killing them.
910 		 */
911 		usb_free_urb(urb);
912 	}
913 
914 	return 0;
915 
916 err_submit:
917 	kfree_skb(skb);
918 err_skb:
919 	usb_free_urb(urb);
920 err_urb:
921 	kfree(rx_buf);
922 err_rxb:
923 	ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
924 	return ret;
925 }
926 
927 static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
928 {
929 	usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
930 }
931 
932 static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
933 {
934 	struct rx_buf *rx_buf = NULL;
935 	struct sk_buff *skb = NULL;
936 	struct urb *urb = NULL;
937 	int i, ret;
938 
939 	init_usb_anchor(&hif_dev->reg_in_submitted);
940 
941 	for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
942 
943 		rx_buf = kzalloc(sizeof(*rx_buf), GFP_KERNEL);
944 		if (!rx_buf) {
945 			ret = -ENOMEM;
946 			goto err_rxb;
947 		}
948 
949 		/* Allocate URB */
950 		urb = usb_alloc_urb(0, GFP_KERNEL);
951 		if (urb == NULL) {
952 			ret = -ENOMEM;
953 			goto err_urb;
954 		}
955 
956 		/* Allocate buffer */
957 		skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
958 		if (!skb) {
959 			ret = -ENOMEM;
960 			goto err_skb;
961 		}
962 
963 		rx_buf->hif_dev = hif_dev;
964 		rx_buf->skb = skb;
965 
966 		usb_fill_int_urb(urb, hif_dev->udev,
967 				  usb_rcvintpipe(hif_dev->udev,
968 						  USB_REG_IN_PIPE),
969 				  skb->data, MAX_REG_IN_BUF_SIZE,
970 				  ath9k_hif_usb_reg_in_cb, rx_buf, 1);
971 
972 		/* Anchor URB */
973 		usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
974 
975 		/* Submit URB */
976 		ret = usb_submit_urb(urb, GFP_KERNEL);
977 		if (ret) {
978 			usb_unanchor_urb(urb);
979 			goto err_submit;
980 		}
981 
982 		/*
983 		 * Drop reference count.
984 		 * This ensures that the URB is freed when killing them.
985 		 */
986 		usb_free_urb(urb);
987 	}
988 
989 	return 0;
990 
991 err_submit:
992 	kfree_skb(skb);
993 err_skb:
994 	usb_free_urb(urb);
995 err_urb:
996 	kfree(rx_buf);
997 err_rxb:
998 	ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
999 	return ret;
1000 }
1001 
1002 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
1003 {
1004 	/* Register Write */
1005 	init_usb_anchor(&hif_dev->regout_submitted);
1006 
1007 	/* TX */
1008 	if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
1009 		goto err;
1010 
1011 	/* RX */
1012 	if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
1013 		goto err_rx;
1014 
1015 	/* Register Read */
1016 	if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
1017 		goto err_reg;
1018 
1019 	return 0;
1020 err_reg:
1021 	ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
1022 err_rx:
1023 	ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
1024 err:
1025 	return -ENOMEM;
1026 }
1027 
1028 void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
1029 {
1030 	usb_kill_anchored_urbs(&hif_dev->regout_submitted);
1031 	ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
1032 	ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
1033 	ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
1034 }
1035 
1036 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
1037 {
1038 	int transfer, err;
1039 	const void *data = hif_dev->fw_data;
1040 	size_t len = hif_dev->fw_size;
1041 	u32 addr = AR9271_FIRMWARE;
1042 	u8 *buf = kzalloc(4096, GFP_KERNEL);
1043 	u32 firm_offset;
1044 
1045 	if (!buf)
1046 		return -ENOMEM;
1047 
1048 	while (len) {
1049 		transfer = min_t(size_t, len, 4096);
1050 		memcpy(buf, data, transfer);
1051 
1052 		err = usb_control_msg(hif_dev->udev,
1053 				      usb_sndctrlpipe(hif_dev->udev, 0),
1054 				      FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
1055 				      addr >> 8, 0, buf, transfer,
1056 				      USB_MSG_TIMEOUT);
1057 		if (err < 0) {
1058 			kfree(buf);
1059 			return err;
1060 		}
1061 
1062 		len -= transfer;
1063 		data += transfer;
1064 		addr += transfer;
1065 	}
1066 	kfree(buf);
1067 
1068 	if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1069 		firm_offset = AR7010_FIRMWARE_TEXT;
1070 	else
1071 		firm_offset = AR9271_FIRMWARE_TEXT;
1072 
1073 	/*
1074 	 * Issue FW download complete command to firmware.
1075 	 */
1076 	err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
1077 			      FIRMWARE_DOWNLOAD_COMP,
1078 			      0x40 | USB_DIR_OUT,
1079 			      firm_offset >> 8, 0, NULL, 0, USB_MSG_TIMEOUT);
1080 	if (err)
1081 		return -EIO;
1082 
1083 	dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
1084 		 hif_dev->fw_name, (unsigned long) hif_dev->fw_size);
1085 
1086 	return 0;
1087 }
1088 
1089 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
1090 {
1091 	int ret;
1092 
1093 	ret = ath9k_hif_usb_download_fw(hif_dev);
1094 	if (ret) {
1095 		dev_err(&hif_dev->udev->dev,
1096 			"ath9k_htc: Firmware - %s download failed\n",
1097 			hif_dev->fw_name);
1098 		return ret;
1099 	}
1100 
1101 	/* Alloc URBs */
1102 	ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1103 	if (ret) {
1104 		dev_err(&hif_dev->udev->dev,
1105 			"ath9k_htc: Unable to allocate URBs\n");
1106 		return ret;
1107 	}
1108 
1109 	return 0;
1110 }
1111 
1112 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
1113 {
1114 	ath9k_hif_usb_dealloc_urbs(hif_dev);
1115 }
1116 
1117 /*
1118  * If initialization fails or the FW cannot be retrieved,
1119  * detach the device.
1120  */
1121 static void ath9k_hif_usb_firmware_fail(struct hif_device_usb *hif_dev)
1122 {
1123 	struct device *dev = &hif_dev->udev->dev;
1124 	struct device *parent = dev->parent;
1125 
1126 	complete_all(&hif_dev->fw_done);
1127 
1128 	if (parent)
1129 		device_lock(parent);
1130 
1131 	device_release_driver(dev);
1132 
1133 	if (parent)
1134 		device_unlock(parent);
1135 }
1136 
1137 static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context);
1138 
1139 /* taken from iwlwifi */
1140 static int ath9k_hif_request_firmware(struct hif_device_usb *hif_dev,
1141 				      bool first)
1142 {
1143 	char index[8], *chip;
1144 	int ret;
1145 
1146 	if (first) {
1147 		if (htc_use_dev_fw) {
1148 			hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX + 1;
1149 			sprintf(index, "%s", "dev");
1150 		} else {
1151 			hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX;
1152 			sprintf(index, "%d", hif_dev->fw_minor_index);
1153 		}
1154 	} else {
1155 		hif_dev->fw_minor_index--;
1156 		sprintf(index, "%d", hif_dev->fw_minor_index);
1157 	}
1158 
1159 	/* test for FW 1.3 */
1160 	if (MAJOR_VERSION_REQ == 1 && hif_dev->fw_minor_index == 3) {
1161 		const char *filename;
1162 
1163 		if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1164 			filename = FIRMWARE_AR7010_1_1;
1165 		else
1166 			filename = FIRMWARE_AR9271;
1167 
1168 		/* expected fw locations:
1169 		 * - htc_9271.fw   (stable version 1.3, depricated)
1170 		 */
1171 		snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
1172 			 "%s", filename);
1173 
1174 	} else if (hif_dev->fw_minor_index < FIRMWARE_MINOR_IDX_MIN) {
1175 		dev_err(&hif_dev->udev->dev, "no suitable firmware found!\n");
1176 
1177 		return -ENOENT;
1178 	} else {
1179 		if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1180 			chip = "7010";
1181 		else
1182 			chip = "9271";
1183 
1184 		/* expected fw locations:
1185 		 * - ath9k_htc/htc_9271-1.dev.0.fw (development version)
1186 		 * - ath9k_htc/htc_9271-1.4.0.fw   (stable version)
1187 		 */
1188 		snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
1189 			 "%s/htc_%s-%d.%s.0.fw", HTC_FW_PATH,
1190 			 chip, MAJOR_VERSION_REQ, index);
1191 	}
1192 
1193 	ret = request_firmware_nowait(THIS_MODULE, true, hif_dev->fw_name,
1194 				      &hif_dev->udev->dev, GFP_KERNEL,
1195 				      hif_dev, ath9k_hif_usb_firmware_cb);
1196 	if (ret) {
1197 		dev_err(&hif_dev->udev->dev,
1198 			"ath9k_htc: Async request for firmware %s failed\n",
1199 			hif_dev->fw_name);
1200 		return ret;
1201 	}
1202 
1203 	dev_info(&hif_dev->udev->dev, "ath9k_htc: Firmware %s requested\n",
1204 		 hif_dev->fw_name);
1205 
1206 	return ret;
1207 }
1208 
1209 static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context)
1210 {
1211 	struct hif_device_usb *hif_dev = context;
1212 	int ret;
1213 
1214 	if (!fw) {
1215 		ret = ath9k_hif_request_firmware(hif_dev, false);
1216 		if (!ret)
1217 			return;
1218 
1219 		dev_err(&hif_dev->udev->dev,
1220 			"ath9k_htc: Failed to get firmware %s\n",
1221 			hif_dev->fw_name);
1222 		goto err_fw;
1223 	}
1224 
1225 	hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1226 						 &hif_dev->udev->dev);
1227 	if (hif_dev->htc_handle == NULL)
1228 		goto err_dev_alloc;
1229 
1230 	hif_dev->fw_data = fw->data;
1231 	hif_dev->fw_size = fw->size;
1232 
1233 	/* Proceed with initialization */
1234 
1235 	ret = ath9k_hif_usb_dev_init(hif_dev);
1236 	if (ret)
1237 		goto err_dev_init;
1238 
1239 	ret = ath9k_htc_hw_init(hif_dev->htc_handle,
1240 				&hif_dev->interface->dev,
1241 				hif_dev->usb_device_id->idProduct,
1242 				hif_dev->udev->product,
1243 				hif_dev->usb_device_id->driver_info);
1244 	if (ret) {
1245 		ret = -EINVAL;
1246 		goto err_htc_hw_init;
1247 	}
1248 
1249 	release_firmware(fw);
1250 	hif_dev->flags |= HIF_USB_READY;
1251 	complete_all(&hif_dev->fw_done);
1252 
1253 	return;
1254 
1255 err_htc_hw_init:
1256 	ath9k_hif_usb_dev_deinit(hif_dev);
1257 err_dev_init:
1258 	ath9k_htc_hw_free(hif_dev->htc_handle);
1259 err_dev_alloc:
1260 	release_firmware(fw);
1261 err_fw:
1262 	ath9k_hif_usb_firmware_fail(hif_dev);
1263 }
1264 
1265 /*
1266  * An exact copy of the function from zd1211rw.
1267  */
1268 static int send_eject_command(struct usb_interface *interface)
1269 {
1270 	struct usb_device *udev = interface_to_usbdev(interface);
1271 	struct usb_host_interface *iface_desc = interface->cur_altsetting;
1272 	struct usb_endpoint_descriptor *endpoint;
1273 	unsigned char *cmd;
1274 	u8 bulk_out_ep;
1275 	int r;
1276 
1277 	if (iface_desc->desc.bNumEndpoints < 2)
1278 		return -ENODEV;
1279 
1280 	/* Find bulk out endpoint */
1281 	for (r = 1; r >= 0; r--) {
1282 		endpoint = &iface_desc->endpoint[r].desc;
1283 		if (usb_endpoint_dir_out(endpoint) &&
1284 		    usb_endpoint_xfer_bulk(endpoint)) {
1285 			bulk_out_ep = endpoint->bEndpointAddress;
1286 			break;
1287 		}
1288 	}
1289 	if (r == -1) {
1290 		dev_err(&udev->dev,
1291 			"ath9k_htc: Could not find bulk out endpoint\n");
1292 		return -ENODEV;
1293 	}
1294 
1295 	cmd = kzalloc(31, GFP_KERNEL);
1296 	if (cmd == NULL)
1297 		return -ENODEV;
1298 
1299 	/* USB bulk command block */
1300 	cmd[0] = 0x55;	/* bulk command signature */
1301 	cmd[1] = 0x53;	/* bulk command signature */
1302 	cmd[2] = 0x42;	/* bulk command signature */
1303 	cmd[3] = 0x43;	/* bulk command signature */
1304 	cmd[14] = 6;	/* command length */
1305 
1306 	cmd[15] = 0x1b;	/* SCSI command: START STOP UNIT */
1307 	cmd[19] = 0x2;	/* eject disc */
1308 
1309 	dev_info(&udev->dev, "Ejecting storage device...\n");
1310 	r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1311 		cmd, 31, NULL, 2 * USB_MSG_TIMEOUT);
1312 	kfree(cmd);
1313 	if (r)
1314 		return r;
1315 
1316 	/* At this point, the device disconnects and reconnects with the real
1317 	 * ID numbers. */
1318 
1319 	usb_set_intfdata(interface, NULL);
1320 	return 0;
1321 }
1322 
1323 static int ath9k_hif_usb_probe(struct usb_interface *interface,
1324 			       const struct usb_device_id *id)
1325 {
1326 	struct usb_device *udev = interface_to_usbdev(interface);
1327 	struct hif_device_usb *hif_dev;
1328 	int ret = 0;
1329 
1330 	if (id->driver_info == STORAGE_DEVICE)
1331 		return send_eject_command(interface);
1332 
1333 	hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1334 	if (!hif_dev) {
1335 		ret = -ENOMEM;
1336 		goto err_alloc;
1337 	}
1338 
1339 	usb_get_dev(udev);
1340 
1341 	hif_dev->udev = udev;
1342 	hif_dev->interface = interface;
1343 	hif_dev->usb_device_id = id;
1344 #ifdef CONFIG_PM
1345 	udev->reset_resume = 1;
1346 #endif
1347 	usb_set_intfdata(interface, hif_dev);
1348 
1349 	init_completion(&hif_dev->fw_done);
1350 
1351 	ret = ath9k_hif_request_firmware(hif_dev, true);
1352 	if (ret)
1353 		goto err_fw_req;
1354 
1355 	return ret;
1356 
1357 err_fw_req:
1358 	usb_set_intfdata(interface, NULL);
1359 	kfree(hif_dev);
1360 	usb_put_dev(udev);
1361 err_alloc:
1362 	return ret;
1363 }
1364 
1365 static void ath9k_hif_usb_reboot(struct usb_device *udev)
1366 {
1367 	u32 reboot_cmd = 0xffffffff;
1368 	void *buf;
1369 	int ret;
1370 
1371 	buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1372 	if (!buf)
1373 		return;
1374 
1375 	ret = usb_interrupt_msg(udev, usb_sndintpipe(udev, USB_REG_OUT_PIPE),
1376 			   buf, 4, NULL, USB_MSG_TIMEOUT);
1377 	if (ret)
1378 		dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1379 
1380 	kfree(buf);
1381 }
1382 
1383 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1384 {
1385 	struct usb_device *udev = interface_to_usbdev(interface);
1386 	struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1387 	bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1388 
1389 	if (!hif_dev)
1390 		return;
1391 
1392 	wait_for_completion(&hif_dev->fw_done);
1393 
1394 	if (hif_dev->flags & HIF_USB_READY) {
1395 		ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1396 		ath9k_hif_usb_dev_deinit(hif_dev);
1397 		ath9k_destroy_wmi(hif_dev->htc_handle->drv_priv);
1398 		ath9k_htc_hw_free(hif_dev->htc_handle);
1399 	}
1400 
1401 	usb_set_intfdata(interface, NULL);
1402 
1403 	/* If firmware was loaded we should drop it
1404 	 * go back to first stage bootloader. */
1405 	if (!unplugged && (hif_dev->flags & HIF_USB_READY))
1406 		ath9k_hif_usb_reboot(udev);
1407 
1408 	kfree(hif_dev);
1409 	dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1410 	usb_put_dev(udev);
1411 }
1412 
1413 #ifdef CONFIG_PM
1414 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1415 				 pm_message_t message)
1416 {
1417 	struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1418 
1419 	/*
1420 	 * The device has to be set to FULLSLEEP mode in case no
1421 	 * interface is up.
1422 	 */
1423 	if (!(hif_dev->flags & HIF_USB_START))
1424 		ath9k_htc_suspend(hif_dev->htc_handle);
1425 
1426 	wait_for_completion(&hif_dev->fw_done);
1427 
1428 	if (hif_dev->flags & HIF_USB_READY)
1429 		ath9k_hif_usb_dealloc_urbs(hif_dev);
1430 
1431 	return 0;
1432 }
1433 
1434 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1435 {
1436 	struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1437 	struct htc_target *htc_handle = hif_dev->htc_handle;
1438 	int ret;
1439 	const struct firmware *fw;
1440 
1441 	ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1442 	if (ret)
1443 		return ret;
1444 
1445 	if (hif_dev->flags & HIF_USB_READY) {
1446 		/* request cached firmware during suspend/resume cycle */
1447 		ret = request_firmware(&fw, hif_dev->fw_name,
1448 				       &hif_dev->udev->dev);
1449 		if (ret)
1450 			goto fail_resume;
1451 
1452 		hif_dev->fw_data = fw->data;
1453 		hif_dev->fw_size = fw->size;
1454 		ret = ath9k_hif_usb_download_fw(hif_dev);
1455 		release_firmware(fw);
1456 		if (ret)
1457 			goto fail_resume;
1458 	} else {
1459 		ath9k_hif_usb_dealloc_urbs(hif_dev);
1460 		return -EIO;
1461 	}
1462 
1463 	mdelay(100);
1464 
1465 	ret = ath9k_htc_resume(htc_handle);
1466 
1467 	if (ret)
1468 		goto fail_resume;
1469 
1470 	return 0;
1471 
1472 fail_resume:
1473 	ath9k_hif_usb_dealloc_urbs(hif_dev);
1474 
1475 	return ret;
1476 }
1477 #endif
1478 
1479 static struct usb_driver ath9k_hif_usb_driver = {
1480 	.name = KBUILD_MODNAME,
1481 	.probe = ath9k_hif_usb_probe,
1482 	.disconnect = ath9k_hif_usb_disconnect,
1483 #ifdef CONFIG_PM
1484 	.suspend = ath9k_hif_usb_suspend,
1485 	.resume = ath9k_hif_usb_resume,
1486 	.reset_resume = ath9k_hif_usb_resume,
1487 #endif
1488 	.id_table = ath9k_hif_usb_ids,
1489 	.soft_unbind = 1,
1490 	.disable_hub_initiated_lpm = 1,
1491 };
1492 
1493 int ath9k_hif_usb_init(void)
1494 {
1495 	return usb_register(&ath9k_hif_usb_driver);
1496 }
1497 
1498 void ath9k_hif_usb_exit(void)
1499 {
1500 	usb_deregister(&ath9k_hif_usb_driver);
1501 }
1502