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