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