ath9k: fix initial sequence number after starting an ampdu session
[linux-2.6-block.git] / drivers / net / wireless / ath / ath9k / hif_usb.c
CommitLineData
fb9987d0
S
1/*
2 * Copyright (c) 2010 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 "htc.h"
18
d0ee0ebe
JL
19/* identify firmware images */
20#define FIRMWARE_AR7010 "ar7010.fw"
21#define FIRMWARE_AR7010_1_1 "ar7010_1_1.fw"
22#define FIRMWARE_AR9271 "ar9271.fw"
23
24MODULE_FIRMWARE(FIRMWARE_AR7010);
25MODULE_FIRMWARE(FIRMWARE_AR7010_1_1);
26MODULE_FIRMWARE(FIRMWARE_AR9271);
27
fb9987d0 28static struct usb_device_id ath9k_hif_usb_ids[] = {
4e63f768
S
29 { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
30 { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
4e63f768 31 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
4e63f768
S
32 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
33 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
34 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
ac618d70 35 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
32b08955
RM
36 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
37 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
38 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
4e63f768 39 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
32b08955 40 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
452d7dd8 41 { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
0b5ead91
SM
42
43 { USB_DEVICE(0x0cf3, 0x7015),
44 .driver_info = AR9287_USB }, /* Atheros */
64f12170 45 { USB_DEVICE(0x1668, 0x1200),
0b5ead91
SM
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
fb9987d0
S
55 { },
56};
57
58MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
59
60static int __hif_usb_tx(struct hif_device_usb *hif_dev);
61
62static void hif_usb_regout_cb(struct urb *urb)
63{
64 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
fb9987d0
S
65
66 switch (urb->status) {
67 case 0:
68 break;
69 case -ENOENT:
70 case -ECONNRESET:
fb9987d0
S
71 case -ENODEV:
72 case -ESHUTDOWN:
6f0f2669 73 goto free;
fb9987d0
S
74 default:
75 break;
76 }
77
78 if (cmd) {
79 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
80 cmd->skb, 1);
81 kfree(cmd);
fb9987d0 82 }
6f0f2669
S
83
84 return;
85free:
0fa35a58 86 kfree_skb(cmd->skb);
6f0f2669 87 kfree(cmd);
fb9987d0
S
88}
89
90static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
91 struct sk_buff *skb)
92{
93 struct urb *urb;
94 struct cmd_buf *cmd;
95 int ret = 0;
96
97 urb = usb_alloc_urb(0, GFP_KERNEL);
98 if (urb == NULL)
99 return -ENOMEM;
100
101 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
102 if (cmd == NULL) {
103 usb_free_urb(urb);
104 return -ENOMEM;
105 }
106
107 cmd->skb = skb;
108 cmd->hif_dev = hif_dev;
109
4a0e8ecc
RM
110 usb_fill_bulk_urb(urb, hif_dev->udev,
111 usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
fb9987d0 112 skb->data, skb->len,
4a0e8ecc 113 hif_usb_regout_cb, cmd);
fb9987d0 114
6f0f2669 115 usb_anchor_urb(urb, &hif_dev->regout_submitted);
fb9987d0
S
116 ret = usb_submit_urb(urb, GFP_KERNEL);
117 if (ret) {
6f0f2669 118 usb_unanchor_urb(urb);
fb9987d0
S
119 kfree(cmd);
120 }
6f0f2669 121 usb_free_urb(urb);
fb9987d0
S
122
123 return ret;
124}
125
c11d8f89
S
126static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
127 struct sk_buff_head *list)
128{
129 struct sk_buff *skb;
130
131 while ((skb = __skb_dequeue(list)) != NULL) {
132 dev_kfree_skb_any(skb);
133 TX_STAT_INC(skb_dropped);
134 }
135}
136
fb9987d0
S
137static void hif_usb_tx_cb(struct urb *urb)
138{
139 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
690e781c 140 struct hif_device_usb *hif_dev;
fb9987d0 141 struct sk_buff *skb;
fb9987d0 142
690e781c 143 if (!tx_buf || !tx_buf->hif_dev)
fb9987d0
S
144 return;
145
690e781c
DC
146 hif_dev = tx_buf->hif_dev;
147
fb9987d0
S
148 switch (urb->status) {
149 case 0:
150 break;
151 case -ENOENT:
152 case -ECONNRESET:
fb9987d0
S
153 case -ENODEV:
154 case -ESHUTDOWN:
c11d8f89 155 /*
ff8f59b5 156 * The URB has been killed, free the SKBs.
c11d8f89
S
157 */
158 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
ff8f59b5
SM
159
160 /*
161 * If the URBs are being flushed, no need to add this
162 * URB to the free list.
163 */
164 spin_lock(&hif_dev->tx.tx_lock);
165 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
166 spin_unlock(&hif_dev->tx.tx_lock);
167 return;
168 }
169 spin_unlock(&hif_dev->tx.tx_lock);
170
171 /*
172 * In the stop() case, this URB has to be added to
173 * the free list.
174 */
175 goto add_free;
fb9987d0
S
176 default:
177 break;
178 }
179
ff8f59b5
SM
180 /*
181 * Check if TX has been stopped, this is needed because
182 * this CB could have been invoked just after the TX lock
183 * was released in hif_stop() and kill_urb() hasn't been
184 * called yet.
185 */
c11d8f89
S
186 spin_lock(&hif_dev->tx.tx_lock);
187 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
fb9987d0 188 spin_unlock(&hif_dev->tx.tx_lock);
c11d8f89
S
189 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
190 goto add_free;
fb9987d0 191 }
c11d8f89 192 spin_unlock(&hif_dev->tx.tx_lock);
fb9987d0 193
c11d8f89
S
194 /* Complete the queued SKBs. */
195 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
196 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
197 skb, 1);
198 TX_STAT_INC(skb_completed);
eac8e385 199 }
c11d8f89
S
200
201add_free:
202 /* Re-initialize the SKB queue */
203 tx_buf->len = tx_buf->offset = 0;
204 __skb_queue_head_init(&tx_buf->skb_queue);
205
206 /* Add this TX buffer to the free list */
207 spin_lock(&hif_dev->tx.tx_lock);
208 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
209 hif_dev->tx.tx_buf_cnt++;
210 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
211 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
212 TX_STAT_INC(buf_completed);
213 spin_unlock(&hif_dev->tx.tx_lock);
f8e1d080
ML
214}
215
fb9987d0
S
216/* TX lock has to be taken */
217static int __hif_usb_tx(struct hif_device_usb *hif_dev)
218{
219 struct tx_buf *tx_buf = NULL;
220 struct sk_buff *nskb = NULL;
221 int ret = 0, i;
222 u16 *hdr, tx_skb_cnt = 0;
223 u8 *buf;
224
225 if (hif_dev->tx.tx_skb_cnt == 0)
226 return 0;
227
228 /* Check if a free TX buffer is available */
229 if (list_empty(&hif_dev->tx.tx_buf))
230 return 0;
231
232 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
c11d8f89 233 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
fb9987d0
S
234 hif_dev->tx.tx_buf_cnt--;
235
236 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
237
238 for (i = 0; i < tx_skb_cnt; i++) {
239 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
240
241 /* Should never be NULL */
242 BUG_ON(!nskb);
243
244 hif_dev->tx.tx_skb_cnt--;
245
246 buf = tx_buf->buf;
247 buf += tx_buf->offset;
248 hdr = (u16 *)buf;
249 *hdr++ = nskb->len;
250 *hdr++ = ATH_USB_TX_STREAM_MODE_TAG;
251 buf += 4;
252 memcpy(buf, nskb->data, nskb->len);
253 tx_buf->len = nskb->len + 4;
254
255 if (i < (tx_skb_cnt - 1))
256 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
257
258 if (i == (tx_skb_cnt - 1))
259 tx_buf->len += tx_buf->offset;
260
261 __skb_queue_tail(&tx_buf->skb_queue, nskb);
262 TX_STAT_INC(skb_queued);
263 }
264
265 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
266 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
267 tx_buf->buf, tx_buf->len,
268 hif_usb_tx_cb, tx_buf);
269
270 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
271 if (ret) {
272 tx_buf->len = tx_buf->offset = 0;
eac8e385 273 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
fb9987d0
S
274 __skb_queue_head_init(&tx_buf->skb_queue);
275 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
276 hif_dev->tx.tx_buf_cnt++;
277 }
278
279 if (!ret)
280 TX_STAT_INC(buf_queued);
281
282 return ret;
283}
284
285static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
286 struct ath9k_htc_tx_ctl *tx_ctl)
287{
288 unsigned long flags;
289
290 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
291
292 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
293 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
294 return -ENODEV;
295 }
296
297 /* Check if the max queue count has been reached */
298 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
299 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
300 return -ENOMEM;
301 }
302
303 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
304 hif_dev->tx.tx_skb_cnt++;
305
306 /* Send normal frames immediately */
307 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
308 __hif_usb_tx(hif_dev);
309
310 /* Check if AMPDUs have to be sent immediately */
311 if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
312 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
313 (hif_dev->tx.tx_skb_cnt < 2)) {
314 __hif_usb_tx(hif_dev);
315 }
316
317 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
318
319 return 0;
320}
321
322static void hif_usb_start(void *hif_handle, u8 pipe_id)
323{
324 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
325 unsigned long flags;
326
327 hif_dev->flags |= HIF_USB_START;
328
329 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
330 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
331 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
332}
333
334static void hif_usb_stop(void *hif_handle, u8 pipe_id)
335{
336 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
ff8f59b5 337 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
fb9987d0
S
338 unsigned long flags;
339
340 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
eac8e385 341 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
fb9987d0
S
342 hif_dev->tx.tx_skb_cnt = 0;
343 hif_dev->tx.flags |= HIF_USB_TX_STOP;
344 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
ff8f59b5
SM
345
346 /* The pending URBs have to be canceled. */
347 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
348 &hif_dev->tx.tx_pending, list) {
349 usb_kill_urb(tx_buf->urb);
350 }
fb9987d0
S
351}
352
353static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
354 struct ath9k_htc_tx_ctl *tx_ctl)
355{
356 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
357 int ret = 0;
358
359 switch (pipe_id) {
360 case USB_WLAN_TX_PIPE:
361 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
362 break;
363 case USB_REG_OUT_PIPE:
364 ret = hif_usb_send_regout(hif_dev, skb);
365 break;
366 default:
6335ed0f
S
367 dev_err(&hif_dev->udev->dev,
368 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
fb9987d0
S
369 ret = -EINVAL;
370 break;
371 }
372
373 return ret;
374}
375
376static struct ath9k_htc_hif hif_usb = {
377 .transport = ATH9K_HIF_USB,
378 .name = "ath9k_hif_usb",
379
380 .control_ul_pipe = USB_REG_OUT_PIPE,
381 .control_dl_pipe = USB_REG_IN_PIPE,
382
383 .start = hif_usb_start,
384 .stop = hif_usb_stop,
385 .send = hif_usb_send,
386};
387
388static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
389 struct sk_buff *skb)
390{
c503269a 391 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
44b23b48
JP
392 int index = 0, i = 0, len = skb->len;
393 int rx_remain_len, rx_pkt_len;
394 u16 pool_index = 0;
fb9987d0
S
395 u8 *ptr;
396
46baa1a2
S
397 spin_lock(&hif_dev->rx_lock);
398
fb9987d0
S
399 rx_remain_len = hif_dev->rx_remain_len;
400 rx_pkt_len = hif_dev->rx_transfer_len;
401
402 if (rx_remain_len != 0) {
403 struct sk_buff *remain_skb = hif_dev->remain_skb;
404
405 if (remain_skb) {
406 ptr = (u8 *) remain_skb->data;
407
408 index = rx_remain_len;
409 rx_remain_len -= hif_dev->rx_pad_len;
410 ptr += rx_pkt_len;
411
412 memcpy(ptr, skb->data, rx_remain_len);
413
414 rx_pkt_len += rx_remain_len;
415 hif_dev->rx_remain_len = 0;
416 skb_put(remain_skb, rx_pkt_len);
417
418 skb_pool[pool_index++] = remain_skb;
419
420 } else {
421 index = rx_remain_len;
422 }
423 }
424
46baa1a2
S
425 spin_unlock(&hif_dev->rx_lock);
426
fb9987d0 427 while (index < len) {
44b23b48
JP
428 u16 pkt_len;
429 u16 pkt_tag;
430 u16 pad_len;
431 int chk_idx;
432
fb9987d0
S
433 ptr = (u8 *) skb->data;
434
435 pkt_len = ptr[index] + (ptr[index+1] << 8);
436 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
437
44b23b48
JP
438 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
439 RX_STAT_INC(skb_dropped);
440 return;
441 }
442
443 pad_len = 4 - (pkt_len & 0x3);
444 if (pad_len == 4)
445 pad_len = 0;
446
447 chk_idx = index;
448 index = index + 4 + pkt_len + pad_len;
449
450 if (index > MAX_RX_BUF_SIZE) {
451 spin_lock(&hif_dev->rx_lock);
452 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
453 hif_dev->rx_transfer_len =
454 MAX_RX_BUF_SIZE - chk_idx - 4;
455 hif_dev->rx_pad_len = pad_len;
456
457 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
458 if (!nskb) {
459 dev_err(&hif_dev->udev->dev,
460 "ath9k_htc: RX memory allocation error\n");
46baa1a2 461 spin_unlock(&hif_dev->rx_lock);
44b23b48 462 goto err;
fb9987d0 463 }
44b23b48
JP
464 skb_reserve(nskb, 32);
465 RX_STAT_INC(skb_allocated);
466
467 memcpy(nskb->data, &(skb->data[chk_idx+4]),
468 hif_dev->rx_transfer_len);
469
470 /* Record the buffer pointer */
471 hif_dev->remain_skb = nskb;
472 spin_unlock(&hif_dev->rx_lock);
fb9987d0 473 } else {
44b23b48
JP
474 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
475 if (!nskb) {
476 dev_err(&hif_dev->udev->dev,
477 "ath9k_htc: RX memory allocation error\n");
478 goto err;
479 }
480 skb_reserve(nskb, 32);
481 RX_STAT_INC(skb_allocated);
482
483 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
484 skb_put(nskb, pkt_len);
485 skb_pool[pool_index++] = nskb;
fb9987d0
S
486 }
487 }
488
489err:
fb9987d0
S
490 for (i = 0; i < pool_index; i++) {
491 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
492 skb_pool[i]->len, USB_WLAN_RX_PIPE);
493 RX_STAT_INC(skb_completed);
494 }
495}
496
497static void ath9k_hif_usb_rx_cb(struct urb *urb)
498{
499 struct sk_buff *skb = (struct sk_buff *) urb->context;
b2767363 500 struct hif_device_usb *hif_dev =
fb9987d0
S
501 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
502 int ret;
503
6335ed0f
S
504 if (!skb)
505 return;
506
fb9987d0
S
507 if (!hif_dev)
508 goto free;
509
510 switch (urb->status) {
511 case 0:
512 break;
513 case -ENOENT:
514 case -ECONNRESET:
515 case -ENODEV:
516 case -ESHUTDOWN:
517 goto free;
518 default:
519 goto resubmit;
520 }
521
522 if (likely(urb->actual_length != 0)) {
523 skb_put(skb, urb->actual_length);
fb9987d0 524 ath9k_hif_usb_rx_stream(hif_dev, skb);
fb9987d0
S
525 }
526
527resubmit:
528 skb_reset_tail_pointer(skb);
529 skb_trim(skb, 0);
530
6335ed0f 531 usb_anchor_urb(urb, &hif_dev->rx_submitted);
fb9987d0 532 ret = usb_submit_urb(urb, GFP_ATOMIC);
6335ed0f
S
533 if (ret) {
534 usb_unanchor_urb(urb);
fb9987d0 535 goto free;
6335ed0f 536 }
fb9987d0
S
537
538 return;
539free:
f28a7b30 540 kfree_skb(skb);
fb9987d0
S
541}
542
543static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
544{
545 struct sk_buff *skb = (struct sk_buff *) urb->context;
546 struct sk_buff *nskb;
b2767363 547 struct hif_device_usb *hif_dev =
fb9987d0
S
548 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
549 int ret;
550
6335ed0f
S
551 if (!skb)
552 return;
553
fb9987d0
S
554 if (!hif_dev)
555 goto free;
556
557 switch (urb->status) {
558 case 0:
559 break;
560 case -ENOENT:
561 case -ECONNRESET:
562 case -ENODEV:
563 case -ESHUTDOWN:
564 goto free;
565 default:
566 goto resubmit;
567 }
568
569 if (likely(urb->actual_length != 0)) {
570 skb_put(skb, urb->actual_length);
571
5ab0af32
S
572 /* Process the command first */
573 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
574 skb->len, USB_REG_IN_PIPE);
575
576
e6c6d33c 577 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
5ab0af32
S
578 if (!nskb) {
579 dev_err(&hif_dev->udev->dev,
580 "ath9k_htc: REG_IN memory allocation failure\n");
581 urb->context = NULL;
582 return;
583 }
fb9987d0 584
490b3f4e 585 usb_fill_bulk_urb(urb, hif_dev->udev,
0f529e98
RM
586 usb_rcvbulkpipe(hif_dev->udev,
587 USB_REG_IN_PIPE),
fb9987d0 588 nskb->data, MAX_REG_IN_BUF_SIZE,
490b3f4e 589 ath9k_hif_usb_reg_in_cb, nskb);
fb9987d0
S
590
591 ret = usb_submit_urb(urb, GFP_ATOMIC);
592 if (ret) {
e6c6d33c 593 kfree_skb(nskb);
5ab0af32 594 urb->context = NULL;
fb9987d0
S
595 }
596
fb9987d0
S
597 return;
598 }
599
600resubmit:
601 skb_reset_tail_pointer(skb);
602 skb_trim(skb, 0);
603
604 ret = usb_submit_urb(urb, GFP_ATOMIC);
605 if (ret)
606 goto free;
607
608 return;
609free:
e6c6d33c 610 kfree_skb(skb);
6335ed0f 611 urb->context = NULL;
fb9987d0
S
612}
613
614static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
615{
fb9987d0 616 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
ff8f59b5 617 unsigned long flags;
fb9987d0 618
c11d8f89
S
619 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
620 &hif_dev->tx.tx_buf, list) {
621 usb_kill_urb(tx_buf->urb);
fb9987d0
S
622 list_del(&tx_buf->list);
623 usb_free_urb(tx_buf->urb);
624 kfree(tx_buf->buf);
625 kfree(tx_buf);
626 }
627
ff8f59b5
SM
628 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
629 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
630 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
631
fb9987d0
S
632 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
633 &hif_dev->tx.tx_pending, list) {
634 usb_kill_urb(tx_buf->urb);
635 list_del(&tx_buf->list);
636 usb_free_urb(tx_buf->urb);
637 kfree(tx_buf->buf);
638 kfree(tx_buf);
639 }
fb9987d0
S
640}
641
642static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
643{
644 struct tx_buf *tx_buf;
645 int i;
646
647 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
648 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
649 spin_lock_init(&hif_dev->tx.tx_lock);
650 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
651
652 for (i = 0; i < MAX_TX_URB_NUM; i++) {
653 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
654 if (!tx_buf)
655 goto err;
656
657 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
658 if (!tx_buf->buf)
659 goto err;
660
661 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
662 if (!tx_buf->urb)
663 goto err;
664
665 tx_buf->hif_dev = hif_dev;
666 __skb_queue_head_init(&tx_buf->skb_queue);
667
668 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
669 }
670
671 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
672
673 return 0;
674err:
7606688a
DC
675 if (tx_buf) {
676 kfree(tx_buf->buf);
677 kfree(tx_buf);
678 }
fb9987d0
S
679 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
680 return -ENOMEM;
681}
682
fb9987d0
S
683static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
684{
6335ed0f 685 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
fb9987d0
S
686}
687
688static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
689{
6335ed0f
S
690 struct urb *urb = NULL;
691 struct sk_buff *skb = NULL;
fb9987d0
S
692 int i, ret;
693
6335ed0f 694 init_usb_anchor(&hif_dev->rx_submitted);
46baa1a2 695 spin_lock_init(&hif_dev->rx_lock);
6335ed0f 696
fb9987d0
S
697 for (i = 0; i < MAX_RX_URB_NUM; i++) {
698
699 /* Allocate URB */
6335ed0f
S
700 urb = usb_alloc_urb(0, GFP_KERNEL);
701 if (urb == NULL) {
fb9987d0 702 ret = -ENOMEM;
6335ed0f 703 goto err_urb;
fb9987d0
S
704 }
705
706 /* Allocate buffer */
f28a7b30 707 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
6335ed0f
S
708 if (!skb) {
709 ret = -ENOMEM;
710 goto err_skb;
711 }
fb9987d0 712
6335ed0f
S
713 usb_fill_bulk_urb(urb, hif_dev->udev,
714 usb_rcvbulkpipe(hif_dev->udev,
715 USB_WLAN_RX_PIPE),
716 skb->data, MAX_RX_BUF_SIZE,
717 ath9k_hif_usb_rx_cb, skb);
718
719 /* Anchor URB */
720 usb_anchor_urb(urb, &hif_dev->rx_submitted);
fb9987d0 721
6335ed0f
S
722 /* Submit URB */
723 ret = usb_submit_urb(urb, GFP_KERNEL);
724 if (ret) {
725 usb_unanchor_urb(urb);
726 goto err_submit;
727 }
66b10e33
S
728
729 /*
730 * Drop reference count.
731 * This ensures that the URB is freed when killing them.
732 */
733 usb_free_urb(urb);
fb9987d0
S
734 }
735
736 return 0;
737
6335ed0f 738err_submit:
f28a7b30 739 kfree_skb(skb);
6335ed0f
S
740err_skb:
741 usb_free_urb(urb);
742err_urb:
fb9987d0
S
743 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
744 return ret;
745}
746
747static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
748{
749 if (hif_dev->reg_in_urb) {
750 usb_kill_urb(hif_dev->reg_in_urb);
6335ed0f 751 if (hif_dev->reg_in_urb->context)
e6c6d33c 752 kfree_skb((void *)hif_dev->reg_in_urb->context);
fb9987d0
S
753 usb_free_urb(hif_dev->reg_in_urb);
754 hif_dev->reg_in_urb = NULL;
755 }
756}
757
758static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
759{
760 struct sk_buff *skb;
761
762 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
763 if (hif_dev->reg_in_urb == NULL)
764 return -ENOMEM;
765
e6c6d33c 766 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
fb9987d0
S
767 if (!skb)
768 goto err;
769
490b3f4e 770 usb_fill_bulk_urb(hif_dev->reg_in_urb, hif_dev->udev,
0f529e98
RM
771 usb_rcvbulkpipe(hif_dev->udev,
772 USB_REG_IN_PIPE),
fb9987d0 773 skb->data, MAX_REG_IN_BUF_SIZE,
490b3f4e 774 ath9k_hif_usb_reg_in_cb, skb);
fb9987d0
S
775
776 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
6335ed0f 777 goto err;
fb9987d0
S
778
779 return 0;
780
fb9987d0
S
781err:
782 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
783 return -ENOMEM;
784}
785
786static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
787{
6f0f2669
S
788 /* Register Write */
789 init_usb_anchor(&hif_dev->regout_submitted);
790
fb9987d0
S
791 /* TX */
792 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
793 goto err;
794
795 /* RX */
796 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
f8036965 797 goto err_rx;
fb9987d0 798
6f0f2669 799 /* Register Read */
fb9987d0 800 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
f8036965 801 goto err_reg;
fb9987d0
S
802
803 return 0;
f8036965
RM
804err_reg:
805 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
806err_rx:
807 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
fb9987d0
S
808err:
809 return -ENOMEM;
810}
811
1d8af8ca
SM
812static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
813{
814 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
815 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
816 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
817 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
818}
819
fa6e15e0
RM
820static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
821 u32 drv_info)
fb9987d0
S
822{
823 int transfer, err;
824 const void *data = hif_dev->firmware->data;
825 size_t len = hif_dev->firmware->size;
826 u32 addr = AR9271_FIRMWARE;
827 u8 *buf = kzalloc(4096, GFP_KERNEL);
b1762862 828 u32 firm_offset;
fb9987d0
S
829
830 if (!buf)
831 return -ENOMEM;
832
833 while (len) {
834 transfer = min_t(int, len, 4096);
835 memcpy(buf, data, transfer);
836
837 err = usb_control_msg(hif_dev->udev,
838 usb_sndctrlpipe(hif_dev->udev, 0),
839 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
840 addr >> 8, 0, buf, transfer, HZ);
841 if (err < 0) {
842 kfree(buf);
843 return err;
844 }
845
846 len -= transfer;
847 data += transfer;
848 addr += transfer;
849 }
850 kfree(buf);
851
0b5ead91 852 if (IS_AR7010_DEVICE(drv_info))
b1762862 853 firm_offset = AR7010_FIRMWARE_TEXT;
fa6e15e0 854 else
b1762862
S
855 firm_offset = AR9271_FIRMWARE_TEXT;
856
fb9987d0
S
857 /*
858 * Issue FW download complete command to firmware.
859 */
860 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
861 FIRMWARE_DOWNLOAD_COMP,
862 0x40 | USB_DIR_OUT,
b1762862 863 firm_offset >> 8, 0, NULL, 0, HZ);
fb9987d0
S
864 if (err)
865 return -EIO;
866
867 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
ce43cee5 868 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
fb9987d0
S
869
870 return 0;
871}
872
fa6e15e0 873static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
fb9987d0 874{
4a0e8ecc
RM
875 int ret, idx;
876 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
877 struct usb_endpoint_descriptor *endp;
fb9987d0
S
878
879 /* Request firmware */
ce43cee5
S
880 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
881 &hif_dev->udev->dev);
fb9987d0
S
882 if (ret) {
883 dev_err(&hif_dev->udev->dev,
ce43cee5 884 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
fb9987d0
S
885 goto err_fw_req;
886 }
887
1d8af8ca 888 /* Download firmware */
fa6e15e0 889 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
1d8af8ca
SM
890 if (ret) {
891 dev_err(&hif_dev->udev->dev,
ce43cee5
S
892 "ath9k_htc: Firmware - %s download failed\n",
893 hif_dev->fw_name);
1d8af8ca
SM
894 goto err_fw_download;
895 }
896
4a0e8ecc
RM
897 /* On downloading the firmware to the target, the USB descriptor of EP4
898 * is 'patched' to change the type of the endpoint to Bulk. This will
899 * bring down CPU usage during the scan period.
900 */
901 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
902 endp = &alt->endpoint[idx].desc;
490b3f4e
RM
903 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
904 == USB_ENDPOINT_XFER_INT) {
4a0e8ecc
RM
905 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
906 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
907 endp->bInterval = 0;
908 }
909 }
910
490b3f4e
RM
911 /* Alloc URBs */
912 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
913 if (ret) {
914 dev_err(&hif_dev->udev->dev,
915 "ath9k_htc: Unable to allocate URBs\n");
916 goto err_urb;
917 }
918
fb9987d0
S
919 return 0;
920
1d8af8ca 921err_urb:
caa0a99a
SM
922 ath9k_hif_usb_dealloc_urbs(hif_dev);
923err_fw_download:
fb9987d0
S
924 release_firmware(hif_dev->firmware);
925err_fw_req:
926 hif_dev->firmware = NULL;
927 return ret;
928}
929
fb9987d0
S
930static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
931{
932 ath9k_hif_usb_dealloc_urbs(hif_dev);
933 if (hif_dev->firmware)
934 release_firmware(hif_dev->firmware);
935}
936
937static int ath9k_hif_usb_probe(struct usb_interface *interface,
938 const struct usb_device_id *id)
939{
940 struct usb_device *udev = interface_to_usbdev(interface);
941 struct hif_device_usb *hif_dev;
fb9987d0
S
942 int ret = 0;
943
944 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
945 if (!hif_dev) {
946 ret = -ENOMEM;
947 goto err_alloc;
948 }
949
950 usb_get_dev(udev);
951 hif_dev->udev = udev;
952 hif_dev->interface = interface;
953 hif_dev->device_id = id->idProduct;
954#ifdef CONFIG_PM
955 udev->reset_resume = 1;
956#endif
957 usb_set_intfdata(interface, hif_dev);
958
47fce026
SM
959 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
960 &hif_dev->udev->dev);
961 if (hif_dev->htc_handle == NULL) {
962 ret = -ENOMEM;
963 goto err_htc_hw_alloc;
964 }
965
ce43cee5
S
966 /* Find out which firmware to load */
967
0b5ead91 968 if (IS_AR7010_DEVICE(id->driver_info))
b1762862 969 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
d0ee0ebe 970 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
b1762862 971 else
d0ee0ebe 972 hif_dev->fw_name = FIRMWARE_AR7010;
fa6e15e0 973 else
d0ee0ebe 974 hif_dev->fw_name = FIRMWARE_AR9271;
ce43cee5 975
fa6e15e0 976 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
fb9987d0
S
977 if (ret) {
978 ret = -EINVAL;
979 goto err_hif_init_usb;
980 }
981
47fce026 982 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
21cb9879 983 &hif_dev->udev->dev, hif_dev->device_id,
fa6e15e0 984 hif_dev->udev->product, id->driver_info);
fb9987d0
S
985 if (ret) {
986 ret = -EINVAL;
987 goto err_htc_hw_init;
988 }
989
990 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
991
992 return 0;
993
994err_htc_hw_init:
fb9987d0
S
995 ath9k_hif_usb_dev_deinit(hif_dev);
996err_hif_init_usb:
47fce026
SM
997 ath9k_htc_hw_free(hif_dev->htc_handle);
998err_htc_hw_alloc:
fb9987d0
S
999 usb_set_intfdata(interface, NULL);
1000 kfree(hif_dev);
1001 usb_put_dev(udev);
1002err_alloc:
1003 return ret;
1004}
1005
62e4716a
S
1006static void ath9k_hif_usb_reboot(struct usb_device *udev)
1007{
1008 u32 reboot_cmd = 0xffffffff;
1009 void *buf;
1010 int ret;
1011
a465a2cc 1012 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
62e4716a
S
1013 if (!buf)
1014 return;
1015
62e4716a
S
1016 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1017 buf, 4, NULL, HZ);
1018 if (ret)
1019 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1020
1021 kfree(buf);
1022}
1023
fb9987d0
S
1024static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1025{
1026 struct usb_device *udev = interface_to_usbdev(interface);
b2767363 1027 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
97dcec57 1028 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
fb9987d0
S
1029
1030 if (hif_dev) {
97dcec57 1031 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
fb9987d0
S
1032 ath9k_htc_hw_free(hif_dev->htc_handle);
1033 ath9k_hif_usb_dev_deinit(hif_dev);
1034 usb_set_intfdata(interface, NULL);
1035 }
1036
97dcec57 1037 if (!unplugged && (hif_dev->flags & HIF_USB_START))
62e4716a 1038 ath9k_hif_usb_reboot(udev);
fb9987d0
S
1039
1040 kfree(hif_dev);
1041 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1042 usb_put_dev(udev);
1043}
1044
1045#ifdef CONFIG_PM
1046static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1047 pm_message_t message)
1048{
b2767363 1049 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
fb9987d0 1050
f933ebed
SM
1051 /*
1052 * The device has to be set to FULLSLEEP mode in case no
1053 * interface is up.
1054 */
1055 if (!(hif_dev->flags & HIF_USB_START))
1056 ath9k_htc_suspend(hif_dev->htc_handle);
1057
fb9987d0
S
1058 ath9k_hif_usb_dealloc_urbs(hif_dev);
1059
1060 return 0;
1061}
1062
1063static int ath9k_hif_usb_resume(struct usb_interface *interface)
1064{
b2767363 1065 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
fa6e15e0 1066 struct htc_target *htc_handle = hif_dev->htc_handle;
fb9987d0
S
1067 int ret;
1068
1069 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1070 if (ret)
1071 return ret;
1072
1073 if (hif_dev->firmware) {
fa6e15e0 1074 ret = ath9k_hif_usb_download_fw(hif_dev,
0b5ead91 1075 htc_handle->drv_priv->ah->hw_version.usbdev);
fb9987d0
S
1076 if (ret)
1077 goto fail_resume;
1078 } else {
1079 ath9k_hif_usb_dealloc_urbs(hif_dev);
1080 return -EIO;
1081 }
1082
1083 mdelay(100);
1084
fa6e15e0 1085 ret = ath9k_htc_resume(htc_handle);
fb9987d0
S
1086
1087 if (ret)
1088 goto fail_resume;
1089
1090 return 0;
1091
1092fail_resume:
1093 ath9k_hif_usb_dealloc_urbs(hif_dev);
1094
1095 return ret;
1096}
1097#endif
1098
1099static struct usb_driver ath9k_hif_usb_driver = {
1100 .name = "ath9k_hif_usb",
1101 .probe = ath9k_hif_usb_probe,
1102 .disconnect = ath9k_hif_usb_disconnect,
1103#ifdef CONFIG_PM
1104 .suspend = ath9k_hif_usb_suspend,
1105 .resume = ath9k_hif_usb_resume,
1106 .reset_resume = ath9k_hif_usb_resume,
1107#endif
1108 .id_table = ath9k_hif_usb_ids,
1109 .soft_unbind = 1,
1110};
1111
1112int ath9k_hif_usb_init(void)
1113{
1114 return usb_register(&ath9k_hif_usb_driver);
1115}
1116
1117void ath9k_hif_usb_exit(void)
1118{
1119 usb_deregister(&ath9k_hif_usb_driver);
1120}