1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include "htc.h"
18
19
20#define FIRMWARE_AR7010_1_1 "htc_7010.fw"
21#define FIRMWARE_AR9271 "htc_9271.fw"
22
23MODULE_FIRMWARE(FIRMWARE_AR7010_1_1);
24MODULE_FIRMWARE(FIRMWARE_AR9271);
25
26static struct usb_device_id ath9k_hif_usb_ids[] = {
27 { USB_DEVICE(0x0cf3, 0x9271) },
28 { USB_DEVICE(0x0cf3, 0x1006) },
29 { USB_DEVICE(0x0846, 0x9030) },
30 { USB_DEVICE(0x07D1, 0x3A10) },
31 { USB_DEVICE(0x13D3, 0x3327) },
32 { USB_DEVICE(0x13D3, 0x3328) },
33 { USB_DEVICE(0x13D3, 0x3346) },
34 { USB_DEVICE(0x13D3, 0x3348) },
35 { USB_DEVICE(0x13D3, 0x3349) },
36 { USB_DEVICE(0x13D3, 0x3350) },
37 { USB_DEVICE(0x04CA, 0x4605) },
38 { USB_DEVICE(0x040D, 0x3801) },
39 { USB_DEVICE(0x0cf3, 0xb003) },
40
41 { USB_DEVICE(0x0cf3, 0x7015),
42 .driver_info = AR9287_USB },
43 { USB_DEVICE(0x1668, 0x1200),
44 .driver_info = AR9287_USB },
45
46 { USB_DEVICE(0x0cf3, 0x7010),
47 .driver_info = AR9280_USB },
48 { USB_DEVICE(0x0846, 0x9018),
49 .driver_info = AR9280_USB },
50 { USB_DEVICE(0x083A, 0xA704),
51 .driver_info = AR9280_USB },
52
53 { USB_DEVICE(0x0cf3, 0x20ff),
54 .driver_info = STORAGE_DEVICE },
55
56 { },
57};
58
59MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
60
61static int __hif_usb_tx(struct hif_device_usb *hif_dev);
62
63static void hif_usb_regout_cb(struct urb *urb)
64{
65 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
66
67 switch (urb->status) {
68 case 0:
69 break;
70 case -ENOENT:
71 case -ECONNRESET:
72 case -ENODEV:
73 case -ESHUTDOWN:
74 goto free;
75 default:
76 break;
77 }
78
79 if (cmd) {
80 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
81 cmd->skb, true);
82 kfree(cmd);
83 }
84
85 return;
86free:
87 kfree_skb(cmd->skb);
88 kfree(cmd);
89}
90
91static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
92 struct sk_buff *skb)
93{
94 struct urb *urb;
95 struct cmd_buf *cmd;
96 int ret = 0;
97
98 urb = usb_alloc_urb(0, GFP_KERNEL);
99 if (urb == NULL)
100 return -ENOMEM;
101
102 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
103 if (cmd == NULL) {
104 usb_free_urb(urb);
105 return -ENOMEM;
106 }
107
108 cmd->skb = skb;
109 cmd->hif_dev = hif_dev;
110
111 usb_fill_bulk_urb(urb, hif_dev->udev,
112 usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
113 skb->data, skb->len,
114 hif_usb_regout_cb, cmd);
115
116 usb_anchor_urb(urb, &hif_dev->regout_submitted);
117 ret = usb_submit_urb(urb, GFP_KERNEL);
118 if (ret) {
119 usb_unanchor_urb(urb);
120 kfree(cmd);
121 }
122 usb_free_urb(urb);
123
124 return ret;
125}
126
127static void hif_usb_mgmt_cb(struct urb *urb)
128{
129 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
130 struct hif_device_usb *hif_dev = cmd->hif_dev;
131 bool txok = true;
132
133 if (!cmd || !cmd->skb || !cmd->hif_dev)
134 return;
135
136 switch (urb->status) {
137 case 0:
138 break;
139 case -ENOENT:
140 case -ECONNRESET:
141 case -ENODEV:
142 case -ESHUTDOWN:
143 txok = false;
144
145
146
147
148
149 spin_lock(&hif_dev->tx.tx_lock);
150 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
151 spin_unlock(&hif_dev->tx.tx_lock);
152 dev_kfree_skb_any(cmd->skb);
153 kfree(cmd);
154 return;
155 }
156 spin_unlock(&hif_dev->tx.tx_lock);
157
158 break;
159 default:
160 txok = false;
161 break;
162 }
163
164 skb_pull(cmd->skb, 4);
165 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
166 cmd->skb, txok);
167 kfree(cmd);
168}
169
170static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev,
171 struct sk_buff *skb)
172{
173 struct urb *urb;
174 struct cmd_buf *cmd;
175 int ret = 0;
176 __le16 *hdr;
177
178 urb = usb_alloc_urb(0, GFP_ATOMIC);
179 if (urb == NULL)
180 return -ENOMEM;
181
182 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
183 if (cmd == NULL) {
184 usb_free_urb(urb);
185 return -ENOMEM;
186 }
187
188 cmd->skb = skb;
189 cmd->hif_dev = hif_dev;
190
191 hdr = (__le16 *) skb_push(skb, 4);
192 *hdr++ = cpu_to_le16(skb->len - 4);
193 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
194
195 usb_fill_bulk_urb(urb, hif_dev->udev,
196 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
197 skb->data, skb->len,
198 hif_usb_mgmt_cb, cmd);
199
200 usb_anchor_urb(urb, &hif_dev->mgmt_submitted);
201 ret = usb_submit_urb(urb, GFP_ATOMIC);
202 if (ret) {
203 usb_unanchor_urb(urb);
204 kfree(cmd);
205 }
206 usb_free_urb(urb);
207
208 return ret;
209}
210
211static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
212 struct sk_buff_head *list)
213{
214 struct sk_buff *skb;
215
216 while ((skb = __skb_dequeue(list)) != NULL) {
217 dev_kfree_skb_any(skb);
218 }
219}
220
221static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
222 struct sk_buff_head *queue,
223 bool txok)
224{
225 struct sk_buff *skb;
226
227 while ((skb = __skb_dequeue(queue)) != NULL) {
228 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
229 skb, txok);
230 if (txok)
231 TX_STAT_INC(skb_success);
232 else
233 TX_STAT_INC(skb_failed);
234 }
235}
236
237static void hif_usb_tx_cb(struct urb *urb)
238{
239 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
240 struct hif_device_usb *hif_dev;
241 bool txok = true;
242
243 if (!tx_buf || !tx_buf->hif_dev)
244 return;
245
246 hif_dev = tx_buf->hif_dev;
247
248 switch (urb->status) {
249 case 0:
250 break;
251 case -ENOENT:
252 case -ECONNRESET:
253 case -ENODEV:
254 case -ESHUTDOWN:
255 txok = false;
256
257
258
259
260
261 spin_lock(&hif_dev->tx.tx_lock);
262 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
263 spin_unlock(&hif_dev->tx.tx_lock);
264 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
265 return;
266 }
267 spin_unlock(&hif_dev->tx.tx_lock);
268
269 break;
270 default:
271 txok = false;
272 break;
273 }
274
275 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
276
277
278 tx_buf->len = tx_buf->offset = 0;
279 __skb_queue_head_init(&tx_buf->skb_queue);
280
281
282 spin_lock(&hif_dev->tx.tx_lock);
283 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
284 hif_dev->tx.tx_buf_cnt++;
285 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
286 __hif_usb_tx(hif_dev);
287 TX_STAT_INC(buf_completed);
288 spin_unlock(&hif_dev->tx.tx_lock);
289}
290
291
292static int __hif_usb_tx(struct hif_device_usb *hif_dev)
293{
294 struct tx_buf *tx_buf = NULL;
295 struct sk_buff *nskb = NULL;
296 int ret = 0, i;
297 u16 tx_skb_cnt = 0;
298 u8 *buf;
299 __le16 *hdr;
300
301 if (hif_dev->tx.tx_skb_cnt == 0)
302 return 0;
303
304
305 if (list_empty(&hif_dev->tx.tx_buf))
306 return 0;
307
308 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
309 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
310 hif_dev->tx.tx_buf_cnt--;
311
312 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
313
314 for (i = 0; i < tx_skb_cnt; i++) {
315 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
316
317
318 BUG_ON(!nskb);
319
320 hif_dev->tx.tx_skb_cnt--;
321
322 buf = tx_buf->buf;
323 buf += tx_buf->offset;
324 hdr = (__le16 *)buf;
325 *hdr++ = cpu_to_le16(nskb->len);
326 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
327 buf += 4;
328 memcpy(buf, nskb->data, nskb->len);
329 tx_buf->len = nskb->len + 4;
330
331 if (i < (tx_skb_cnt - 1))
332 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
333
334 if (i == (tx_skb_cnt - 1))
335 tx_buf->len += tx_buf->offset;
336
337 __skb_queue_tail(&tx_buf->skb_queue, nskb);
338 TX_STAT_INC(skb_queued);
339 }
340
341 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
342 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
343 tx_buf->buf, tx_buf->len,
344 hif_usb_tx_cb, tx_buf);
345
346 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
347 if (ret) {
348 tx_buf->len = tx_buf->offset = 0;
349 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
350 __skb_queue_head_init(&tx_buf->skb_queue);
351 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
352 hif_dev->tx.tx_buf_cnt++;
353 }
354
355 if (!ret)
356 TX_STAT_INC(buf_queued);
357
358 return ret;
359}
360
361static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
362{
363 struct ath9k_htc_tx_ctl *tx_ctl;
364 unsigned long flags;
365 int ret = 0;
366
367 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
368
369 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
370 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
371 return -ENODEV;
372 }
373
374
375 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
376 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
377 return -ENOMEM;
378 }
379
380 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
381
382 tx_ctl = HTC_SKB_CB(skb);
383
384
385 if ((tx_ctl->type == ATH9K_HTC_MGMT) ||
386 (tx_ctl->type == ATH9K_HTC_BEACON)) {
387 ret = hif_usb_send_mgmt(hif_dev, skb);
388 }
389
390 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
391
392 if ((tx_ctl->type == ATH9K_HTC_NORMAL) ||
393 (tx_ctl->type == ATH9K_HTC_AMPDU)) {
394 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
395 hif_dev->tx.tx_skb_cnt++;
396 }
397
398
399 if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
400 (hif_dev->tx.tx_skb_cnt < 2)) {
401 __hif_usb_tx(hif_dev);
402 }
403
404 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
405
406 return ret;
407}
408
409static void hif_usb_start(void *hif_handle)
410{
411 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
412 unsigned long flags;
413
414 hif_dev->flags |= HIF_USB_START;
415
416 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
417 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
418 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
419}
420
421static void hif_usb_stop(void *hif_handle)
422{
423 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
424 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
425 unsigned long flags;
426
427 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
428 ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
429 hif_dev->tx.tx_skb_cnt = 0;
430 hif_dev->tx.flags |= HIF_USB_TX_STOP;
431 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
432
433
434 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
435 &hif_dev->tx.tx_pending, list) {
436 usb_kill_urb(tx_buf->urb);
437 }
438
439 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
440}
441
442static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
443{
444 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
445 int ret = 0;
446
447 switch (pipe_id) {
448 case USB_WLAN_TX_PIPE:
449 ret = hif_usb_send_tx(hif_dev, skb);
450 break;
451 case USB_REG_OUT_PIPE:
452 ret = hif_usb_send_regout(hif_dev, skb);
453 break;
454 default:
455 dev_err(&hif_dev->udev->dev,
456 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
457 ret = -EINVAL;
458 break;
459 }
460
461 return ret;
462}
463
464static inline bool check_index(struct sk_buff *skb, u8 idx)
465{
466 struct ath9k_htc_tx_ctl *tx_ctl;
467
468 tx_ctl = HTC_SKB_CB(skb);
469
470 if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
471 (tx_ctl->sta_idx == idx))
472 return true;
473
474 return false;
475}
476
477static void hif_usb_sta_drain(void *hif_handle, u8 idx)
478{
479 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
480 struct sk_buff *skb, *tmp;
481 unsigned long flags;
482
483 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
484
485 skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
486 if (check_index(skb, idx)) {
487 __skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
488 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
489 skb, false);
490 hif_dev->tx.tx_skb_cnt--;
491 TX_STAT_INC(skb_failed);
492 }
493 }
494
495 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
496}
497
498static struct ath9k_htc_hif hif_usb = {
499 .transport = ATH9K_HIF_USB,
500 .name = "ath9k_hif_usb",
501
502 .control_ul_pipe = USB_REG_OUT_PIPE,
503 .control_dl_pipe = USB_REG_IN_PIPE,
504
505 .start = hif_usb_start,
506 .stop = hif_usb_stop,
507 .sta_drain = hif_usb_sta_drain,
508 .send = hif_usb_send,
509};
510
511static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
512 struct sk_buff *skb)
513{
514 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
515 int index = 0, i = 0, len = skb->len;
516 int rx_remain_len, rx_pkt_len;
517 u16 pool_index = 0;
518 u8 *ptr;
519
520 spin_lock(&hif_dev->rx_lock);
521
522 rx_remain_len = hif_dev->rx_remain_len;
523 rx_pkt_len = hif_dev->rx_transfer_len;
524
525 if (rx_remain_len != 0) {
526 struct sk_buff *remain_skb = hif_dev->remain_skb;
527
528 if (remain_skb) {
529 ptr = (u8 *) remain_skb->data;
530
531 index = rx_remain_len;
532 rx_remain_len -= hif_dev->rx_pad_len;
533 ptr += rx_pkt_len;
534
535 memcpy(ptr, skb->data, rx_remain_len);
536
537 rx_pkt_len += rx_remain_len;
538 hif_dev->rx_remain_len = 0;
539 skb_put(remain_skb, rx_pkt_len);
540
541 skb_pool[pool_index++] = remain_skb;
542
543 } else {
544 index = rx_remain_len;
545 }
546 }
547
548 spin_unlock(&hif_dev->rx_lock);
549
550 while (index < len) {
551 u16 pkt_len;
552 u16 pkt_tag;
553 u16 pad_len;
554 int chk_idx;
555
556 ptr = (u8 *) skb->data;
557
558 pkt_len = ptr[index] + (ptr[index+1] << 8);
559 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
560
561 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
562 RX_STAT_INC(skb_dropped);
563 return;
564 }
565
566 pad_len = 4 - (pkt_len & 0x3);
567 if (pad_len == 4)
568 pad_len = 0;
569
570 chk_idx = index;
571 index = index + 4 + pkt_len + pad_len;
572
573 if (index > MAX_RX_BUF_SIZE) {
574 spin_lock(&hif_dev->rx_lock);
575 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
576 hif_dev->rx_transfer_len =
577 MAX_RX_BUF_SIZE - chk_idx - 4;
578 hif_dev->rx_pad_len = pad_len;
579
580 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
581 if (!nskb) {
582 dev_err(&hif_dev->udev->dev,
583 "ath9k_htc: RX memory allocation error\n");
584 spin_unlock(&hif_dev->rx_lock);
585 goto err;
586 }
587 skb_reserve(nskb, 32);
588 RX_STAT_INC(skb_allocated);
589
590 memcpy(nskb->data, &(skb->data[chk_idx+4]),
591 hif_dev->rx_transfer_len);
592
593
594 hif_dev->remain_skb = nskb;
595 spin_unlock(&hif_dev->rx_lock);
596 } else {
597 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
598 if (!nskb) {
599 dev_err(&hif_dev->udev->dev,
600 "ath9k_htc: RX memory allocation error\n");
601 goto err;
602 }
603 skb_reserve(nskb, 32);
604 RX_STAT_INC(skb_allocated);
605
606 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
607 skb_put(nskb, pkt_len);
608 skb_pool[pool_index++] = nskb;
609 }
610 }
611
612err:
613 for (i = 0; i < pool_index; i++) {
614 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
615 skb_pool[i]->len, USB_WLAN_RX_PIPE);
616 RX_STAT_INC(skb_completed);
617 }
618}
619
620static void ath9k_hif_usb_rx_cb(struct urb *urb)
621{
622 struct sk_buff *skb = (struct sk_buff *) urb->context;
623 struct hif_device_usb *hif_dev =
624 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
625 int ret;
626
627 if (!skb)
628 return;
629
630 if (!hif_dev)
631 goto free;
632
633 switch (urb->status) {
634 case 0:
635 break;
636 case -ENOENT:
637 case -ECONNRESET:
638 case -ENODEV:
639 case -ESHUTDOWN:
640 goto free;
641 default:
642 goto resubmit;
643 }
644
645 if (likely(urb->actual_length != 0)) {
646 skb_put(skb, urb->actual_length);
647 ath9k_hif_usb_rx_stream(hif_dev, skb);
648 }
649
650resubmit:
651 skb_reset_tail_pointer(skb);
652 skb_trim(skb, 0);
653
654 usb_anchor_urb(urb, &hif_dev->rx_submitted);
655 ret = usb_submit_urb(urb, GFP_ATOMIC);
656 if (ret) {
657 usb_unanchor_urb(urb);
658 goto free;
659 }
660
661 return;
662free:
663 kfree_skb(skb);
664}
665
666static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
667{
668 struct sk_buff *skb = (struct sk_buff *) urb->context;
669 struct sk_buff *nskb;
670 struct hif_device_usb *hif_dev =
671 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
672 int ret;
673
674 if (!skb)
675 return;
676
677 if (!hif_dev)
678 goto free;
679
680 switch (urb->status) {
681 case 0:
682 break;
683 case -ENOENT:
684 case -ECONNRESET:
685 case -ENODEV:
686 case -ESHUTDOWN:
687 goto free;
688 default:
689 skb_reset_tail_pointer(skb);
690 skb_trim(skb, 0);
691
692 goto resubmit;
693 }
694
695 if (likely(urb->actual_length != 0)) {
696 skb_put(skb, urb->actual_length);
697
698
699 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
700 skb->len, USB_REG_IN_PIPE);
701
702
703 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
704 if (!nskb) {
705 dev_err(&hif_dev->udev->dev,
706 "ath9k_htc: REG_IN memory allocation failure\n");
707 urb->context = NULL;
708 return;
709 }
710
711 usb_fill_bulk_urb(urb, hif_dev->udev,
712 usb_rcvbulkpipe(hif_dev->udev,
713 USB_REG_IN_PIPE),
714 nskb->data, MAX_REG_IN_BUF_SIZE,
715 ath9k_hif_usb_reg_in_cb, nskb);
716 }
717
718resubmit:
719 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
720 ret = usb_submit_urb(urb, GFP_ATOMIC);
721 if (ret) {
722 usb_unanchor_urb(urb);
723 goto free;
724 }
725
726 return;
727free:
728 kfree_skb(skb);
729 urb->context = NULL;
730}
731
732static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
733{
734 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
735 unsigned long flags;
736
737 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
738 &hif_dev->tx.tx_buf, list) {
739 usb_kill_urb(tx_buf->urb);
740 list_del(&tx_buf->list);
741 usb_free_urb(tx_buf->urb);
742 kfree(tx_buf->buf);
743 kfree(tx_buf);
744 }
745
746 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
747 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
748 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
749
750 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
751 &hif_dev->tx.tx_pending, list) {
752 usb_kill_urb(tx_buf->urb);
753 list_del(&tx_buf->list);
754 usb_free_urb(tx_buf->urb);
755 kfree(tx_buf->buf);
756 kfree(tx_buf);
757 }
758
759 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
760}
761
762static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
763{
764 struct tx_buf *tx_buf;
765 int i;
766
767 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
768 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
769 spin_lock_init(&hif_dev->tx.tx_lock);
770 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
771 init_usb_anchor(&hif_dev->mgmt_submitted);
772
773 for (i = 0; i < MAX_TX_URB_NUM; i++) {
774 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
775 if (!tx_buf)
776 goto err;
777
778 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
779 if (!tx_buf->buf)
780 goto err;
781
782 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
783 if (!tx_buf->urb)
784 goto err;
785
786 tx_buf->hif_dev = hif_dev;
787 __skb_queue_head_init(&tx_buf->skb_queue);
788
789 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
790 }
791
792 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
793
794 return 0;
795err:
796 if (tx_buf) {
797 kfree(tx_buf->buf);
798 kfree(tx_buf);
799 }
800 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
801 return -ENOMEM;
802}
803
804static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
805{
806 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
807}
808
809static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
810{
811 struct urb *urb = NULL;
812 struct sk_buff *skb = NULL;
813 int i, ret;
814
815 init_usb_anchor(&hif_dev->rx_submitted);
816 spin_lock_init(&hif_dev->rx_lock);
817
818 for (i = 0; i < MAX_RX_URB_NUM; i++) {
819
820
821 urb = usb_alloc_urb(0, GFP_KERNEL);
822 if (urb == NULL) {
823 ret = -ENOMEM;
824 goto err_urb;
825 }
826
827
828 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
829 if (!skb) {
830 ret = -ENOMEM;
831 goto err_skb;
832 }
833
834 usb_fill_bulk_urb(urb, hif_dev->udev,
835 usb_rcvbulkpipe(hif_dev->udev,
836 USB_WLAN_RX_PIPE),
837 skb->data, MAX_RX_BUF_SIZE,
838 ath9k_hif_usb_rx_cb, skb);
839
840
841 usb_anchor_urb(urb, &hif_dev->rx_submitted);
842
843
844 ret = usb_submit_urb(urb, GFP_KERNEL);
845 if (ret) {
846 usb_unanchor_urb(urb);
847 goto err_submit;
848 }
849
850
851
852
853
854 usb_free_urb(urb);
855 }
856
857 return 0;
858
859err_submit:
860 kfree_skb(skb);
861err_skb:
862 usb_free_urb(urb);
863err_urb:
864 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
865 return ret;
866}
867
868static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
869{
870 usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
871}
872
873static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
874{
875 struct urb *urb = NULL;
876 struct sk_buff *skb = NULL;
877 int i, ret;
878
879 init_usb_anchor(&hif_dev->reg_in_submitted);
880
881 for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
882
883
884 urb = usb_alloc_urb(0, GFP_KERNEL);
885 if (urb == NULL) {
886 ret = -ENOMEM;
887 goto err_urb;
888 }
889
890
891 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
892 if (!skb) {
893 ret = -ENOMEM;
894 goto err_skb;
895 }
896
897 usb_fill_bulk_urb(urb, hif_dev->udev,
898 usb_rcvbulkpipe(hif_dev->udev,
899 USB_REG_IN_PIPE),
900 skb->data, MAX_REG_IN_BUF_SIZE,
901 ath9k_hif_usb_reg_in_cb, skb);
902
903
904 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
905
906
907 ret = usb_submit_urb(urb, GFP_KERNEL);
908 if (ret) {
909 usb_unanchor_urb(urb);
910 goto err_submit;
911 }
912
913
914
915
916
917 usb_free_urb(urb);
918 }
919
920 return 0;
921
922err_submit:
923 kfree_skb(skb);
924err_skb:
925 usb_free_urb(urb);
926err_urb:
927 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
928 return ret;
929}
930
931static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
932{
933
934 init_usb_anchor(&hif_dev->regout_submitted);
935
936
937 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
938 goto err;
939
940
941 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
942 goto err_rx;
943
944
945 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
946 goto err_reg;
947
948 return 0;
949err_reg:
950 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
951err_rx:
952 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
953err:
954 return -ENOMEM;
955}
956
957static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
958{
959 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
960 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
961 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
962 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
963}
964
965static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
966 u32 drv_info)
967{
968 int transfer, err;
969 const void *data = hif_dev->firmware->data;
970 size_t len = hif_dev->firmware->size;
971 u32 addr = AR9271_FIRMWARE;
972 u8 *buf = kzalloc(4096, GFP_KERNEL);
973 u32 firm_offset;
974
975 if (!buf)
976 return -ENOMEM;
977
978 while (len) {
979 transfer = min_t(int, len, 4096);
980 memcpy(buf, data, transfer);
981
982 err = usb_control_msg(hif_dev->udev,
983 usb_sndctrlpipe(hif_dev->udev, 0),
984 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
985 addr >> 8, 0, buf, transfer, HZ);
986 if (err < 0) {
987 kfree(buf);
988 return err;
989 }
990
991 len -= transfer;
992 data += transfer;
993 addr += transfer;
994 }
995 kfree(buf);
996
997 if (IS_AR7010_DEVICE(drv_info))
998 firm_offset = AR7010_FIRMWARE_TEXT;
999 else
1000 firm_offset = AR9271_FIRMWARE_TEXT;
1001
1002
1003
1004
1005 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
1006 FIRMWARE_DOWNLOAD_COMP,
1007 0x40 | USB_DIR_OUT,
1008 firm_offset >> 8, 0, NULL, 0, HZ);
1009 if (err)
1010 return -EIO;
1011
1012 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
1013 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
1014
1015 return 0;
1016}
1017
1018static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
1019{
1020 int ret, idx;
1021 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
1022 struct usb_endpoint_descriptor *endp;
1023
1024
1025 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
1026 &hif_dev->udev->dev);
1027 if (ret) {
1028 dev_err(&hif_dev->udev->dev,
1029 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
1030 goto err_fw_req;
1031 }
1032
1033
1034 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
1035 if (ret) {
1036 dev_err(&hif_dev->udev->dev,
1037 "ath9k_htc: Firmware - %s download failed\n",
1038 hif_dev->fw_name);
1039 goto err_fw_download;
1040 }
1041
1042
1043
1044
1045
1046 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
1047 endp = &alt->endpoint[idx].desc;
1048 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1049 == USB_ENDPOINT_XFER_INT) {
1050 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
1051 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
1052 endp->bInterval = 0;
1053 }
1054 }
1055
1056
1057 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1058 if (ret) {
1059 dev_err(&hif_dev->udev->dev,
1060 "ath9k_htc: Unable to allocate URBs\n");
1061 goto err_fw_download;
1062 }
1063
1064 return 0;
1065
1066err_fw_download:
1067 release_firmware(hif_dev->firmware);
1068err_fw_req:
1069 hif_dev->firmware = NULL;
1070 return ret;
1071}
1072
1073static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
1074{
1075 ath9k_hif_usb_dealloc_urbs(hif_dev);
1076 if (hif_dev->firmware)
1077 release_firmware(hif_dev->firmware);
1078}
1079
1080
1081
1082
1083static int send_eject_command(struct usb_interface *interface)
1084{
1085 struct usb_device *udev = interface_to_usbdev(interface);
1086 struct usb_host_interface *iface_desc = &interface->altsetting[0];
1087 struct usb_endpoint_descriptor *endpoint;
1088 unsigned char *cmd;
1089 u8 bulk_out_ep;
1090 int r;
1091
1092
1093 for (r = 1; r >= 0; r--) {
1094 endpoint = &iface_desc->endpoint[r].desc;
1095 if (usb_endpoint_dir_out(endpoint) &&
1096 usb_endpoint_xfer_bulk(endpoint)) {
1097 bulk_out_ep = endpoint->bEndpointAddress;
1098 break;
1099 }
1100 }
1101 if (r == -1) {
1102 dev_err(&udev->dev,
1103 "ath9k_htc: Could not find bulk out endpoint\n");
1104 return -ENODEV;
1105 }
1106
1107 cmd = kzalloc(31, GFP_KERNEL);
1108 if (cmd == NULL)
1109 return -ENODEV;
1110
1111
1112 cmd[0] = 0x55;
1113 cmd[1] = 0x53;
1114 cmd[2] = 0x42;
1115 cmd[3] = 0x43;
1116 cmd[14] = 6;
1117
1118 cmd[15] = 0x1b;
1119 cmd[19] = 0x2;
1120
1121 dev_info(&udev->dev, "Ejecting storage device...\n");
1122 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1123 cmd, 31, NULL, 2000);
1124 kfree(cmd);
1125 if (r)
1126 return r;
1127
1128
1129
1130
1131 usb_set_intfdata(interface, NULL);
1132 return 0;
1133}
1134
1135static int ath9k_hif_usb_probe(struct usb_interface *interface,
1136 const struct usb_device_id *id)
1137{
1138 struct usb_device *udev = interface_to_usbdev(interface);
1139 struct hif_device_usb *hif_dev;
1140 int ret = 0;
1141
1142 if (id->driver_info == STORAGE_DEVICE)
1143 return send_eject_command(interface);
1144
1145 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1146 if (!hif_dev) {
1147 ret = -ENOMEM;
1148 goto err_alloc;
1149 }
1150
1151 usb_get_dev(udev);
1152 hif_dev->udev = udev;
1153 hif_dev->interface = interface;
1154 hif_dev->device_id = id->idProduct;
1155#ifdef CONFIG_PM
1156 udev->reset_resume = 1;
1157#endif
1158 usb_set_intfdata(interface, hif_dev);
1159
1160 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1161 &hif_dev->udev->dev);
1162 if (hif_dev->htc_handle == NULL) {
1163 ret = -ENOMEM;
1164 goto err_htc_hw_alloc;
1165 }
1166
1167
1168
1169 if (IS_AR7010_DEVICE(id->driver_info))
1170 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
1171 else
1172 hif_dev->fw_name = FIRMWARE_AR9271;
1173
1174 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
1175 if (ret) {
1176 ret = -EINVAL;
1177 goto err_hif_init_usb;
1178 }
1179
1180 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
1181 &interface->dev, hif_dev->device_id,
1182 hif_dev->udev->product, id->driver_info);
1183 if (ret) {
1184 ret = -EINVAL;
1185 goto err_htc_hw_init;
1186 }
1187
1188 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
1189
1190 return 0;
1191
1192err_htc_hw_init:
1193 ath9k_hif_usb_dev_deinit(hif_dev);
1194err_hif_init_usb:
1195 ath9k_htc_hw_free(hif_dev->htc_handle);
1196err_htc_hw_alloc:
1197 usb_set_intfdata(interface, NULL);
1198 kfree(hif_dev);
1199 usb_put_dev(udev);
1200err_alloc:
1201 return ret;
1202}
1203
1204static void ath9k_hif_usb_reboot(struct usb_device *udev)
1205{
1206 u32 reboot_cmd = 0xffffffff;
1207 void *buf;
1208 int ret;
1209
1210 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1211 if (!buf)
1212 return;
1213
1214 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1215 buf, 4, NULL, HZ);
1216 if (ret)
1217 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1218
1219 kfree(buf);
1220}
1221
1222static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1223{
1224 struct usb_device *udev = interface_to_usbdev(interface);
1225 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1226 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1227
1228 if (!hif_dev)
1229 return;
1230
1231 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1232 ath9k_htc_hw_free(hif_dev->htc_handle);
1233 ath9k_hif_usb_dev_deinit(hif_dev);
1234 usb_set_intfdata(interface, NULL);
1235
1236 if (!unplugged && (hif_dev->flags & HIF_USB_START))
1237 ath9k_hif_usb_reboot(udev);
1238
1239 kfree(hif_dev);
1240 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1241 usb_put_dev(udev);
1242}
1243
1244#ifdef CONFIG_PM
1245static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1246 pm_message_t message)
1247{
1248 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1249
1250
1251
1252
1253
1254 if (!(hif_dev->flags & HIF_USB_START))
1255 ath9k_htc_suspend(hif_dev->htc_handle);
1256
1257 ath9k_hif_usb_dealloc_urbs(hif_dev);
1258
1259 return 0;
1260}
1261
1262static int ath9k_hif_usb_resume(struct usb_interface *interface)
1263{
1264 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1265 struct htc_target *htc_handle = hif_dev->htc_handle;
1266 int ret;
1267
1268 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1269 if (ret)
1270 return ret;
1271
1272 if (hif_dev->firmware) {
1273 ret = ath9k_hif_usb_download_fw(hif_dev,
1274 htc_handle->drv_priv->ah->hw_version.usbdev);
1275 if (ret)
1276 goto fail_resume;
1277 } else {
1278 ath9k_hif_usb_dealloc_urbs(hif_dev);
1279 return -EIO;
1280 }
1281
1282 mdelay(100);
1283
1284 ret = ath9k_htc_resume(htc_handle);
1285
1286 if (ret)
1287 goto fail_resume;
1288
1289 return 0;
1290
1291fail_resume:
1292 ath9k_hif_usb_dealloc_urbs(hif_dev);
1293
1294 return ret;
1295}
1296#endif
1297
1298static struct usb_driver ath9k_hif_usb_driver = {
1299 .name = KBUILD_MODNAME,
1300 .probe = ath9k_hif_usb_probe,
1301 .disconnect = ath9k_hif_usb_disconnect,
1302#ifdef CONFIG_PM
1303 .suspend = ath9k_hif_usb_suspend,
1304 .resume = ath9k_hif_usb_resume,
1305 .reset_resume = ath9k_hif_usb_resume,
1306#endif
1307 .id_table = ath9k_hif_usb_ids,
1308 .soft_unbind = 1,
1309};
1310
1311int ath9k_hif_usb_init(void)
1312{
1313 return usb_register(&ath9k_hif_usb_driver);
1314}
1315
1316void ath9k_hif_usb_exit(void)
1317{
1318 usb_deregister(&ath9k_hif_usb_driver);
1319}
1320