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