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