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