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