blob: 74bc80f502920bfe4065b750286c814ccd90cb36 [file] [log] [blame]
Sujithfb9987d2010-03-17 14:25:25 +05301/*
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
Sujithfb9987d2010-03-17 14:25:25 +053019static struct usb_device_id ath9k_hif_usb_ids[] = {
Sujith4e63f762010-06-17 10:29:01 +053020 { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
21 { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
22 { USB_DEVICE(0x0cf3, 0x7010) }, /* Atheros */
23 { USB_DEVICE(0x0cf3, 0x7015) }, /* Atheros */
24 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
25 { USB_DEVICE(0x0846, 0x9018) }, /* Netgear WNDA3200 */
26 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
27 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
28 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
29 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
30 { USB_DEVICE(0x083A, 0xA704) }, /* SMC Networks */
Sujithfb9987d2010-03-17 14:25:25 +053031 { },
32};
33
34MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
35
36static int __hif_usb_tx(struct hif_device_usb *hif_dev);
37
38static void hif_usb_regout_cb(struct urb *urb)
39{
40 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
Sujithfb9987d2010-03-17 14:25:25 +053041
42 switch (urb->status) {
43 case 0:
44 break;
45 case -ENOENT:
46 case -ECONNRESET:
Sujithfb9987d2010-03-17 14:25:25 +053047 case -ENODEV:
48 case -ESHUTDOWN:
Sujith6f0f2662010-04-06 15:28:17 +053049 goto free;
Sujithfb9987d2010-03-17 14:25:25 +053050 default:
51 break;
52 }
53
54 if (cmd) {
55 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
56 cmd->skb, 1);
57 kfree(cmd);
Sujithfb9987d2010-03-17 14:25:25 +053058 }
Sujith6f0f2662010-04-06 15:28:17 +053059
60 return;
61free:
Ming Lei0fa35a52010-04-13 00:29:15 +080062 kfree_skb(cmd->skb);
Sujith6f0f2662010-04-06 15:28:17 +053063 kfree(cmd);
Sujithfb9987d2010-03-17 14:25:25 +053064}
65
66static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
67 struct sk_buff *skb)
68{
69 struct urb *urb;
70 struct cmd_buf *cmd;
71 int ret = 0;
72
73 urb = usb_alloc_urb(0, GFP_KERNEL);
74 if (urb == NULL)
75 return -ENOMEM;
76
77 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
78 if (cmd == NULL) {
79 usb_free_urb(urb);
80 return -ENOMEM;
81 }
82
83 cmd->skb = skb;
84 cmd->hif_dev = hif_dev;
85
86 usb_fill_int_urb(urb, hif_dev->udev,
87 usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
88 skb->data, skb->len,
89 hif_usb_regout_cb, cmd, 1);
90
Sujith6f0f2662010-04-06 15:28:17 +053091 usb_anchor_urb(urb, &hif_dev->regout_submitted);
Sujithfb9987d2010-03-17 14:25:25 +053092 ret = usb_submit_urb(urb, GFP_KERNEL);
93 if (ret) {
Sujith6f0f2662010-04-06 15:28:17 +053094 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +053095 kfree(cmd);
96 }
Sujith6f0f2662010-04-06 15:28:17 +053097 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +053098
99 return ret;
100}
101
Sujitheac8e382010-04-16 11:54:00 +0530102static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
103 struct sk_buff_head *list)
Ming Leif8e1d082010-04-13 00:29:27 +0800104{
105 struct sk_buff *skb;
Sujitheac8e382010-04-16 11:54:00 +0530106
107 while ((skb = __skb_dequeue(list)) != NULL) {
Ming Leif8e1d082010-04-13 00:29:27 +0800108 dev_kfree_skb_any(skb);
Sujitheac8e382010-04-16 11:54:00 +0530109 TX_STAT_INC(skb_dropped);
110 }
Ming Leif8e1d082010-04-13 00:29:27 +0800111}
112
Sujithc11d8f82010-04-23 10:28:09 +0530113static void hif_usb_tx_cb(struct urb *urb)
114{
115 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
Dan Carpenter690e7812010-05-14 16:50:56 +0200116 struct hif_device_usb *hif_dev;
Sujithc11d8f82010-04-23 10:28:09 +0530117 struct sk_buff *skb;
118
Dan Carpenter690e7812010-05-14 16:50:56 +0200119 if (!tx_buf || !tx_buf->hif_dev)
Sujithc11d8f82010-04-23 10:28:09 +0530120 return;
121
Dan Carpenter690e7812010-05-14 16:50:56 +0200122 hif_dev = tx_buf->hif_dev;
123
Sujithc11d8f82010-04-23 10:28:09 +0530124 switch (urb->status) {
125 case 0:
126 break;
127 case -ENOENT:
128 case -ECONNRESET:
129 case -ENODEV:
130 case -ESHUTDOWN:
131 /*
132 * The URB has been killed, free the SKBs
133 * and return.
134 */
135 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
136 return;
137 default:
138 break;
139 }
140
141 /* Check if TX has been stopped */
142 spin_lock(&hif_dev->tx.tx_lock);
143 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
144 spin_unlock(&hif_dev->tx.tx_lock);
145 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
146 goto add_free;
147 }
148 spin_unlock(&hif_dev->tx.tx_lock);
149
150 /* Complete the queued SKBs. */
151 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
152 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
153 skb, 1);
154 TX_STAT_INC(skb_completed);
155 }
156
157add_free:
158 /* Re-initialize the SKB queue */
159 tx_buf->len = tx_buf->offset = 0;
160 __skb_queue_head_init(&tx_buf->skb_queue);
161
162 /* Add this TX buffer to the free list */
163 spin_lock(&hif_dev->tx.tx_lock);
164 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
165 hif_dev->tx.tx_buf_cnt++;
166 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
167 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
168 TX_STAT_INC(buf_completed);
169 spin_unlock(&hif_dev->tx.tx_lock);
170}
171
Sujithfb9987d2010-03-17 14:25:25 +0530172/* TX lock has to be taken */
173static int __hif_usb_tx(struct hif_device_usb *hif_dev)
174{
175 struct tx_buf *tx_buf = NULL;
176 struct sk_buff *nskb = NULL;
177 int ret = 0, i;
178 u16 *hdr, tx_skb_cnt = 0;
179 u8 *buf;
180
181 if (hif_dev->tx.tx_skb_cnt == 0)
182 return 0;
183
184 /* Check if a free TX buffer is available */
185 if (list_empty(&hif_dev->tx.tx_buf))
186 return 0;
187
188 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
Sujithc11d8f82010-04-23 10:28:09 +0530189 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
Sujithfb9987d2010-03-17 14:25:25 +0530190 hif_dev->tx.tx_buf_cnt--;
191
192 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
193
194 for (i = 0; i < tx_skb_cnt; i++) {
195 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
196
197 /* Should never be NULL */
198 BUG_ON(!nskb);
199
200 hif_dev->tx.tx_skb_cnt--;
201
202 buf = tx_buf->buf;
203 buf += tx_buf->offset;
204 hdr = (u16 *)buf;
205 *hdr++ = nskb->len;
206 *hdr++ = ATH_USB_TX_STREAM_MODE_TAG;
207 buf += 4;
208 memcpy(buf, nskb->data, nskb->len);
209 tx_buf->len = nskb->len + 4;
210
211 if (i < (tx_skb_cnt - 1))
212 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
213
214 if (i == (tx_skb_cnt - 1))
215 tx_buf->len += tx_buf->offset;
216
217 __skb_queue_tail(&tx_buf->skb_queue, nskb);
218 TX_STAT_INC(skb_queued);
219 }
220
221 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
222 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
223 tx_buf->buf, tx_buf->len,
224 hif_usb_tx_cb, tx_buf);
225
226 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
227 if (ret) {
228 tx_buf->len = tx_buf->offset = 0;
Sujitheac8e382010-04-16 11:54:00 +0530229 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
Sujithfb9987d2010-03-17 14:25:25 +0530230 __skb_queue_head_init(&tx_buf->skb_queue);
231 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
232 hif_dev->tx.tx_buf_cnt++;
233 }
234
235 if (!ret)
236 TX_STAT_INC(buf_queued);
237
238 return ret;
239}
240
241static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
242 struct ath9k_htc_tx_ctl *tx_ctl)
243{
244 unsigned long flags;
245
246 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
247
248 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
249 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
250 return -ENODEV;
251 }
252
253 /* Check if the max queue count has been reached */
254 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
255 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
256 return -ENOMEM;
257 }
258
259 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
260 hif_dev->tx.tx_skb_cnt++;
261
262 /* Send normal frames immediately */
263 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
264 __hif_usb_tx(hif_dev);
265
266 /* Check if AMPDUs have to be sent immediately */
267 if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
268 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
269 (hif_dev->tx.tx_skb_cnt < 2)) {
270 __hif_usb_tx(hif_dev);
271 }
272
273 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
274
275 return 0;
276}
277
278static void hif_usb_start(void *hif_handle, u8 pipe_id)
279{
280 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
281 unsigned long flags;
282
283 hif_dev->flags |= HIF_USB_START;
284
285 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
286 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
287 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
288}
289
290static void hif_usb_stop(void *hif_handle, u8 pipe_id)
291{
292 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
293 unsigned long flags;
294
295 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
Sujitheac8e382010-04-16 11:54:00 +0530296 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
Sujithfb9987d2010-03-17 14:25:25 +0530297 hif_dev->tx.tx_skb_cnt = 0;
298 hif_dev->tx.flags |= HIF_USB_TX_STOP;
299 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
300}
301
302static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
303 struct ath9k_htc_tx_ctl *tx_ctl)
304{
305 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
306 int ret = 0;
307
308 switch (pipe_id) {
309 case USB_WLAN_TX_PIPE:
310 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
311 break;
312 case USB_REG_OUT_PIPE:
313 ret = hif_usb_send_regout(hif_dev, skb);
314 break;
315 default:
Sujith6335ed02010-03-29 16:07:15 +0530316 dev_err(&hif_dev->udev->dev,
317 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
Sujithfb9987d2010-03-17 14:25:25 +0530318 ret = -EINVAL;
319 break;
320 }
321
322 return ret;
323}
324
325static struct ath9k_htc_hif hif_usb = {
326 .transport = ATH9K_HIF_USB,
327 .name = "ath9k_hif_usb",
328
329 .control_ul_pipe = USB_REG_OUT_PIPE,
330 .control_dl_pipe = USB_REG_IN_PIPE,
331
332 .start = hif_usb_start,
333 .stop = hif_usb_stop,
334 .send = hif_usb_send,
335};
336
337static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
338 struct sk_buff *skb)
339{
Sujithc5032692010-04-06 15:28:15 +0530340 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
Sujithfb9987d2010-03-17 14:25:25 +0530341 int index = 0, i = 0, chk_idx, len = skb->len;
342 int rx_remain_len = 0, rx_pkt_len = 0;
343 u16 pkt_len, pkt_tag, pool_index = 0;
344 u8 *ptr;
345
Sujith46baa1a2010-04-06 15:28:11 +0530346 spin_lock(&hif_dev->rx_lock);
347
Sujithfb9987d2010-03-17 14:25:25 +0530348 rx_remain_len = hif_dev->rx_remain_len;
349 rx_pkt_len = hif_dev->rx_transfer_len;
350
351 if (rx_remain_len != 0) {
352 struct sk_buff *remain_skb = hif_dev->remain_skb;
353
354 if (remain_skb) {
355 ptr = (u8 *) remain_skb->data;
356
357 index = rx_remain_len;
358 rx_remain_len -= hif_dev->rx_pad_len;
359 ptr += rx_pkt_len;
360
361 memcpy(ptr, skb->data, rx_remain_len);
362
363 rx_pkt_len += rx_remain_len;
364 hif_dev->rx_remain_len = 0;
365 skb_put(remain_skb, rx_pkt_len);
366
367 skb_pool[pool_index++] = remain_skb;
368
369 } else {
370 index = rx_remain_len;
371 }
372 }
373
Sujith46baa1a2010-04-06 15:28:11 +0530374 spin_unlock(&hif_dev->rx_lock);
375
Sujithfb9987d2010-03-17 14:25:25 +0530376 while (index < len) {
377 ptr = (u8 *) skb->data;
378
379 pkt_len = ptr[index] + (ptr[index+1] << 8);
380 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
381
382 if (pkt_tag == ATH_USB_RX_STREAM_MODE_TAG) {
383 u16 pad_len;
384
385 pad_len = 4 - (pkt_len & 0x3);
386 if (pad_len == 4)
387 pad_len = 0;
388
389 chk_idx = index;
390 index = index + 4 + pkt_len + pad_len;
391
392 if (index > MAX_RX_BUF_SIZE) {
Sujith46baa1a2010-04-06 15:28:11 +0530393 spin_lock(&hif_dev->rx_lock);
Sujithfb9987d2010-03-17 14:25:25 +0530394 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
395 hif_dev->rx_transfer_len =
396 MAX_RX_BUF_SIZE - chk_idx - 4;
397 hif_dev->rx_pad_len = pad_len;
398
399 nskb = __dev_alloc_skb(pkt_len + 32,
400 GFP_ATOMIC);
401 if (!nskb) {
402 dev_err(&hif_dev->udev->dev,
403 "ath9k_htc: RX memory allocation"
404 " error\n");
Sujith46baa1a2010-04-06 15:28:11 +0530405 spin_unlock(&hif_dev->rx_lock);
Sujithfb9987d2010-03-17 14:25:25 +0530406 goto err;
407 }
408 skb_reserve(nskb, 32);
409 RX_STAT_INC(skb_allocated);
410
411 memcpy(nskb->data, &(skb->data[chk_idx+4]),
412 hif_dev->rx_transfer_len);
413
414 /* Record the buffer pointer */
415 hif_dev->remain_skb = nskb;
Sujith46baa1a2010-04-06 15:28:11 +0530416 spin_unlock(&hif_dev->rx_lock);
Sujithfb9987d2010-03-17 14:25:25 +0530417 } else {
418 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
419 if (!nskb) {
420 dev_err(&hif_dev->udev->dev,
421 "ath9k_htc: RX memory allocation"
422 " error\n");
423 goto err;
424 }
425 skb_reserve(nskb, 32);
426 RX_STAT_INC(skb_allocated);
427
428 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
429 skb_put(nskb, pkt_len);
430 skb_pool[pool_index++] = nskb;
431 }
432 } else {
433 RX_STAT_INC(skb_dropped);
Sujithfb9987d2010-03-17 14:25:25 +0530434 return;
435 }
436 }
437
438err:
Sujithfb9987d2010-03-17 14:25:25 +0530439 for (i = 0; i < pool_index; i++) {
440 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
441 skb_pool[i]->len, USB_WLAN_RX_PIPE);
442 RX_STAT_INC(skb_completed);
443 }
444}
445
446static void ath9k_hif_usb_rx_cb(struct urb *urb)
447{
448 struct sk_buff *skb = (struct sk_buff *) urb->context;
Sujithfb9987d2010-03-17 14:25:25 +0530449 struct hif_device_usb *hif_dev = (struct hif_device_usb *)
450 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
451 int ret;
452
Sujith6335ed02010-03-29 16:07:15 +0530453 if (!skb)
454 return;
455
Sujithfb9987d2010-03-17 14:25:25 +0530456 if (!hif_dev)
457 goto free;
458
459 switch (urb->status) {
460 case 0:
461 break;
462 case -ENOENT:
463 case -ECONNRESET:
464 case -ENODEV:
465 case -ESHUTDOWN:
466 goto free;
467 default:
468 goto resubmit;
469 }
470
471 if (likely(urb->actual_length != 0)) {
472 skb_put(skb, urb->actual_length);
Sujithfb9987d2010-03-17 14:25:25 +0530473 ath9k_hif_usb_rx_stream(hif_dev, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530474 }
475
476resubmit:
477 skb_reset_tail_pointer(skb);
478 skb_trim(skb, 0);
479
Sujith6335ed02010-03-29 16:07:15 +0530480 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530481 ret = usb_submit_urb(urb, GFP_ATOMIC);
Sujith6335ed02010-03-29 16:07:15 +0530482 if (ret) {
483 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530484 goto free;
Sujith6335ed02010-03-29 16:07:15 +0530485 }
Sujithfb9987d2010-03-17 14:25:25 +0530486
487 return;
488free:
Ming Leif28a7b32010-04-13 00:28:53 +0800489 kfree_skb(skb);
Sujithfb9987d2010-03-17 14:25:25 +0530490}
491
492static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
493{
494 struct sk_buff *skb = (struct sk_buff *) urb->context;
495 struct sk_buff *nskb;
496 struct hif_device_usb *hif_dev = (struct hif_device_usb *)
497 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
498 int ret;
499
Sujith6335ed02010-03-29 16:07:15 +0530500 if (!skb)
501 return;
502
Sujithfb9987d2010-03-17 14:25:25 +0530503 if (!hif_dev)
504 goto free;
505
506 switch (urb->status) {
507 case 0:
508 break;
509 case -ENOENT:
510 case -ECONNRESET:
511 case -ENODEV:
512 case -ESHUTDOWN:
513 goto free;
514 default:
515 goto resubmit;
516 }
517
518 if (likely(urb->actual_length != 0)) {
519 skb_put(skb, urb->actual_length);
520
Sujith5ab0af32010-04-23 10:28:17 +0530521 /* Process the command first */
522 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
523 skb->len, USB_REG_IN_PIPE);
524
525
Ming Leie6c6d332010-04-13 00:29:05 +0800526 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
Sujith5ab0af32010-04-23 10:28:17 +0530527 if (!nskb) {
528 dev_err(&hif_dev->udev->dev,
529 "ath9k_htc: REG_IN memory allocation failure\n");
530 urb->context = NULL;
531 return;
532 }
Sujithfb9987d2010-03-17 14:25:25 +0530533
534 usb_fill_int_urb(urb, hif_dev->udev,
535 usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
536 nskb->data, MAX_REG_IN_BUF_SIZE,
537 ath9k_hif_usb_reg_in_cb, nskb, 1);
538
539 ret = usb_submit_urb(urb, GFP_ATOMIC);
540 if (ret) {
Ming Leie6c6d332010-04-13 00:29:05 +0800541 kfree_skb(nskb);
Sujith5ab0af32010-04-23 10:28:17 +0530542 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530543 }
544
Sujithfb9987d2010-03-17 14:25:25 +0530545 return;
546 }
547
548resubmit:
549 skb_reset_tail_pointer(skb);
550 skb_trim(skb, 0);
551
552 ret = usb_submit_urb(urb, GFP_ATOMIC);
553 if (ret)
554 goto free;
555
556 return;
557free:
Ming Leie6c6d332010-04-13 00:29:05 +0800558 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530559 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530560}
561
562static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
563{
Sujithfb9987d2010-03-17 14:25:25 +0530564 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
565
Sujithc11d8f82010-04-23 10:28:09 +0530566 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
567 &hif_dev->tx.tx_buf, list) {
568 usb_kill_urb(tx_buf->urb);
Sujithfb9987d2010-03-17 14:25:25 +0530569 list_del(&tx_buf->list);
570 usb_free_urb(tx_buf->urb);
571 kfree(tx_buf->buf);
572 kfree(tx_buf);
573 }
574
Sujithfb9987d2010-03-17 14:25:25 +0530575 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
576 &hif_dev->tx.tx_pending, list) {
577 usb_kill_urb(tx_buf->urb);
578 list_del(&tx_buf->list);
579 usb_free_urb(tx_buf->urb);
580 kfree(tx_buf->buf);
581 kfree(tx_buf);
582 }
Sujithfb9987d2010-03-17 14:25:25 +0530583}
584
585static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
586{
587 struct tx_buf *tx_buf;
588 int i;
589
590 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
591 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
592 spin_lock_init(&hif_dev->tx.tx_lock);
593 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
594
595 for (i = 0; i < MAX_TX_URB_NUM; i++) {
596 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
597 if (!tx_buf)
598 goto err;
599
600 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
601 if (!tx_buf->buf)
602 goto err;
603
604 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
605 if (!tx_buf->urb)
606 goto err;
607
608 tx_buf->hif_dev = hif_dev;
609 __skb_queue_head_init(&tx_buf->skb_queue);
610
611 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
612 }
613
614 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
615
616 return 0;
617err:
Dan Carpenter76066882010-05-14 16:52:37 +0200618 if (tx_buf) {
619 kfree(tx_buf->buf);
620 kfree(tx_buf);
621 }
Sujithfb9987d2010-03-17 14:25:25 +0530622 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
623 return -ENOMEM;
624}
625
Sujithfb9987d2010-03-17 14:25:25 +0530626static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
627{
Sujith6335ed02010-03-29 16:07:15 +0530628 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530629}
630
631static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
632{
Sujith6335ed02010-03-29 16:07:15 +0530633 struct urb *urb = NULL;
634 struct sk_buff *skb = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530635 int i, ret;
636
Sujith6335ed02010-03-29 16:07:15 +0530637 init_usb_anchor(&hif_dev->rx_submitted);
Sujith46baa1a2010-04-06 15:28:11 +0530638 spin_lock_init(&hif_dev->rx_lock);
Sujith6335ed02010-03-29 16:07:15 +0530639
Sujithfb9987d2010-03-17 14:25:25 +0530640 for (i = 0; i < MAX_RX_URB_NUM; i++) {
641
642 /* Allocate URB */
Sujith6335ed02010-03-29 16:07:15 +0530643 urb = usb_alloc_urb(0, GFP_KERNEL);
644 if (urb == NULL) {
Sujithfb9987d2010-03-17 14:25:25 +0530645 ret = -ENOMEM;
Sujith6335ed02010-03-29 16:07:15 +0530646 goto err_urb;
Sujithfb9987d2010-03-17 14:25:25 +0530647 }
648
649 /* Allocate buffer */
Ming Leif28a7b32010-04-13 00:28:53 +0800650 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
Sujith6335ed02010-03-29 16:07:15 +0530651 if (!skb) {
652 ret = -ENOMEM;
653 goto err_skb;
654 }
655
656 usb_fill_bulk_urb(urb, hif_dev->udev,
657 usb_rcvbulkpipe(hif_dev->udev,
658 USB_WLAN_RX_PIPE),
659 skb->data, MAX_RX_BUF_SIZE,
660 ath9k_hif_usb_rx_cb, skb);
661
662 /* Anchor URB */
663 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530664
665 /* Submit URB */
Sujith6335ed02010-03-29 16:07:15 +0530666 ret = usb_submit_urb(urb, GFP_KERNEL);
667 if (ret) {
668 usb_unanchor_urb(urb);
669 goto err_submit;
670 }
Sujith66b10e32010-04-06 15:28:13 +0530671
672 /*
673 * Drop reference count.
674 * This ensures that the URB is freed when killing them.
675 */
676 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530677 }
678
679 return 0;
680
Sujith6335ed02010-03-29 16:07:15 +0530681err_submit:
Ming Leif28a7b32010-04-13 00:28:53 +0800682 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530683err_skb:
684 usb_free_urb(urb);
685err_urb:
Sujithfb9987d2010-03-17 14:25:25 +0530686 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
687 return ret;
688}
689
690static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
691{
692 if (hif_dev->reg_in_urb) {
693 usb_kill_urb(hif_dev->reg_in_urb);
Sujith6335ed02010-03-29 16:07:15 +0530694 if (hif_dev->reg_in_urb->context)
Ming Leie6c6d332010-04-13 00:29:05 +0800695 kfree_skb((void *)hif_dev->reg_in_urb->context);
Sujithfb9987d2010-03-17 14:25:25 +0530696 usb_free_urb(hif_dev->reg_in_urb);
697 hif_dev->reg_in_urb = NULL;
698 }
699}
700
701static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
702{
703 struct sk_buff *skb;
704
705 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
706 if (hif_dev->reg_in_urb == NULL)
707 return -ENOMEM;
708
Ming Leie6c6d332010-04-13 00:29:05 +0800709 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
Sujithfb9987d2010-03-17 14:25:25 +0530710 if (!skb)
711 goto err;
712
713 usb_fill_int_urb(hif_dev->reg_in_urb, hif_dev->udev,
714 usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
715 skb->data, MAX_REG_IN_BUF_SIZE,
716 ath9k_hif_usb_reg_in_cb, skb, 1);
717
718 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
Sujith6335ed02010-03-29 16:07:15 +0530719 goto err;
Sujithfb9987d2010-03-17 14:25:25 +0530720
721 return 0;
722
Sujithfb9987d2010-03-17 14:25:25 +0530723err:
724 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
725 return -ENOMEM;
726}
727
728static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
729{
Sujith6f0f2662010-04-06 15:28:17 +0530730 /* Register Write */
731 init_usb_anchor(&hif_dev->regout_submitted);
732
Sujithfb9987d2010-03-17 14:25:25 +0530733 /* TX */
734 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
735 goto err;
736
737 /* RX */
738 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
739 goto err;
740
Sujith6f0f2662010-04-06 15:28:17 +0530741 /* Register Read */
Sujithfb9987d2010-03-17 14:25:25 +0530742 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
743 goto err;
744
745 return 0;
746err:
747 return -ENOMEM;
748}
749
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530750static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
751{
752 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
753 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
754 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
755 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
756}
757
Sujithfb9987d2010-03-17 14:25:25 +0530758static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
759{
760 int transfer, err;
761 const void *data = hif_dev->firmware->data;
762 size_t len = hif_dev->firmware->size;
763 u32 addr = AR9271_FIRMWARE;
764 u8 *buf = kzalloc(4096, GFP_KERNEL);
Sujithb1762862010-06-02 15:53:34 +0530765 u32 firm_offset;
Sujithfb9987d2010-03-17 14:25:25 +0530766
767 if (!buf)
768 return -ENOMEM;
769
770 while (len) {
771 transfer = min_t(int, len, 4096);
772 memcpy(buf, data, transfer);
773
774 err = usb_control_msg(hif_dev->udev,
775 usb_sndctrlpipe(hif_dev->udev, 0),
776 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
777 addr >> 8, 0, buf, transfer, HZ);
778 if (err < 0) {
779 kfree(buf);
780 return err;
781 }
782
783 len -= transfer;
784 data += transfer;
785 addr += transfer;
786 }
787 kfree(buf);
788
Sujithb1762862010-06-02 15:53:34 +0530789 if (hif_dev->device_id == 0x7010)
790 firm_offset = AR7010_FIRMWARE_TEXT;
791 else
792 firm_offset = AR9271_FIRMWARE_TEXT;
793
Sujithfb9987d2010-03-17 14:25:25 +0530794 /*
795 * Issue FW download complete command to firmware.
796 */
797 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
798 FIRMWARE_DOWNLOAD_COMP,
799 0x40 | USB_DIR_OUT,
Sujithb1762862010-06-02 15:53:34 +0530800 firm_offset >> 8, 0, NULL, 0, HZ);
Sujithfb9987d2010-03-17 14:25:25 +0530801 if (err)
802 return -EIO;
803
804 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
Sujithce43cee2010-06-02 15:53:30 +0530805 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
Sujithfb9987d2010-03-17 14:25:25 +0530806
807 return 0;
808}
809
Sujithce43cee2010-06-02 15:53:30 +0530810static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
Sujithfb9987d2010-03-17 14:25:25 +0530811{
812 int ret;
813
814 /* Request firmware */
Sujithce43cee2010-06-02 15:53:30 +0530815 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
816 &hif_dev->udev->dev);
Sujithfb9987d2010-03-17 14:25:25 +0530817 if (ret) {
818 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +0530819 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
Sujithfb9987d2010-03-17 14:25:25 +0530820 goto err_fw_req;
821 }
822
Sujithfb9987d2010-03-17 14:25:25 +0530823 /* Alloc URBs */
824 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
825 if (ret) {
826 dev_err(&hif_dev->udev->dev,
827 "ath9k_htc: Unable to allocate URBs\n");
828 goto err_urb;
829 }
830
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530831 /* Download firmware */
832 ret = ath9k_hif_usb_download_fw(hif_dev);
833 if (ret) {
834 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +0530835 "ath9k_htc: Firmware - %s download failed\n",
836 hif_dev->fw_name);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530837 goto err_fw_download;
838 }
839
Sujithfb9987d2010-03-17 14:25:25 +0530840 return 0;
841
Sujithfb9987d2010-03-17 14:25:25 +0530842err_fw_download:
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530843 ath9k_hif_usb_dealloc_urbs(hif_dev);
844err_urb:
Sujithfb9987d2010-03-17 14:25:25 +0530845 release_firmware(hif_dev->firmware);
846err_fw_req:
847 hif_dev->firmware = NULL;
848 return ret;
849}
850
Sujithfb9987d2010-03-17 14:25:25 +0530851static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
852{
853 ath9k_hif_usb_dealloc_urbs(hif_dev);
854 if (hif_dev->firmware)
855 release_firmware(hif_dev->firmware);
856}
857
858static int ath9k_hif_usb_probe(struct usb_interface *interface,
859 const struct usb_device_id *id)
860{
861 struct usb_device *udev = interface_to_usbdev(interface);
862 struct hif_device_usb *hif_dev;
Sujithfb9987d2010-03-17 14:25:25 +0530863 int ret = 0;
864
865 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
866 if (!hif_dev) {
867 ret = -ENOMEM;
868 goto err_alloc;
869 }
870
871 usb_get_dev(udev);
872 hif_dev->udev = udev;
873 hif_dev->interface = interface;
874 hif_dev->device_id = id->idProduct;
875#ifdef CONFIG_PM
876 udev->reset_resume = 1;
877#endif
878 usb_set_intfdata(interface, hif_dev);
879
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +0530880 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
881 &hif_dev->udev->dev);
882 if (hif_dev->htc_handle == NULL) {
883 ret = -ENOMEM;
884 goto err_htc_hw_alloc;
885 }
886
Sujithce43cee2010-06-02 15:53:30 +0530887 /* Find out which firmware to load */
888
889 switch(hif_dev->device_id) {
Sujithb1762862010-06-02 15:53:34 +0530890 case 0x7010:
Sujith4e63f762010-06-17 10:29:01 +0530891 case 0x9018:
Sujithb1762862010-06-02 15:53:34 +0530892 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
893 hif_dev->fw_name = "ar7010_1_1.fw";
894 else
895 hif_dev->fw_name = "ar7010.fw";
896 break;
Sujithce43cee2010-06-02 15:53:30 +0530897 default:
Sujith4e63f762010-06-17 10:29:01 +0530898 hif_dev->fw_name = "ar9271.fw";
Sujithce43cee2010-06-02 15:53:30 +0530899 break;
900 }
901
902 if (!hif_dev->fw_name) {
903 dev_err(&udev->dev, "Can't determine firmware !\n");
904 goto err_htc_hw_alloc;
905 }
906
907 ret = ath9k_hif_usb_dev_init(hif_dev);
Sujithfb9987d2010-03-17 14:25:25 +0530908 if (ret) {
909 ret = -EINVAL;
910 goto err_hif_init_usb;
911 }
912
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +0530913 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
914 &hif_dev->udev->dev, hif_dev->device_id);
Sujithfb9987d2010-03-17 14:25:25 +0530915 if (ret) {
916 ret = -EINVAL;
917 goto err_htc_hw_init;
918 }
919
920 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
921
922 return 0;
923
924err_htc_hw_init:
Sujithfb9987d2010-03-17 14:25:25 +0530925 ath9k_hif_usb_dev_deinit(hif_dev);
926err_hif_init_usb:
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +0530927 ath9k_htc_hw_free(hif_dev->htc_handle);
928err_htc_hw_alloc:
Sujithfb9987d2010-03-17 14:25:25 +0530929 usb_set_intfdata(interface, NULL);
930 kfree(hif_dev);
931 usb_put_dev(udev);
932err_alloc:
933 return ret;
934}
935
Sujith62e47162010-04-23 10:28:16 +0530936static void ath9k_hif_usb_reboot(struct usb_device *udev)
937{
938 u32 reboot_cmd = 0xffffffff;
939 void *buf;
940 int ret;
941
Julia Lawalla465a2c2010-05-15 23:17:19 +0200942 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
Sujith62e47162010-04-23 10:28:16 +0530943 if (!buf)
944 return;
945
Sujith62e47162010-04-23 10:28:16 +0530946 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
947 buf, 4, NULL, HZ);
948 if (ret)
949 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
950
951 kfree(buf);
952}
953
Sujithfb9987d2010-03-17 14:25:25 +0530954static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
955{
956 struct usb_device *udev = interface_to_usbdev(interface);
957 struct hif_device_usb *hif_dev =
958 (struct hif_device_usb *) usb_get_intfdata(interface);
959
960 if (hif_dev) {
Sujithd8f996f2010-04-23 10:28:20 +0530961 ath9k_htc_hw_deinit(hif_dev->htc_handle,
962 (udev->state == USB_STATE_NOTATTACHED) ? true : false);
Sujithfb9987d2010-03-17 14:25:25 +0530963 ath9k_htc_hw_free(hif_dev->htc_handle);
964 ath9k_hif_usb_dev_deinit(hif_dev);
965 usb_set_intfdata(interface, NULL);
966 }
967
968 if (hif_dev->flags & HIF_USB_START)
Sujith62e47162010-04-23 10:28:16 +0530969 ath9k_hif_usb_reboot(udev);
Sujithfb9987d2010-03-17 14:25:25 +0530970
971 kfree(hif_dev);
972 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
973 usb_put_dev(udev);
974}
975
976#ifdef CONFIG_PM
977static int ath9k_hif_usb_suspend(struct usb_interface *interface,
978 pm_message_t message)
979{
980 struct hif_device_usb *hif_dev =
981 (struct hif_device_usb *) usb_get_intfdata(interface);
982
983 ath9k_hif_usb_dealloc_urbs(hif_dev);
984
985 return 0;
986}
987
988static int ath9k_hif_usb_resume(struct usb_interface *interface)
989{
990 struct hif_device_usb *hif_dev =
991 (struct hif_device_usb *) usb_get_intfdata(interface);
992 int ret;
993
994 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
995 if (ret)
996 return ret;
997
998 if (hif_dev->firmware) {
999 ret = ath9k_hif_usb_download_fw(hif_dev);
1000 if (ret)
1001 goto fail_resume;
1002 } else {
1003 ath9k_hif_usb_dealloc_urbs(hif_dev);
1004 return -EIO;
1005 }
1006
1007 mdelay(100);
1008
1009 ret = ath9k_htc_resume(hif_dev->htc_handle);
1010
1011 if (ret)
1012 goto fail_resume;
1013
1014 return 0;
1015
1016fail_resume:
1017 ath9k_hif_usb_dealloc_urbs(hif_dev);
1018
1019 return ret;
1020}
1021#endif
1022
1023static struct usb_driver ath9k_hif_usb_driver = {
1024 .name = "ath9k_hif_usb",
1025 .probe = ath9k_hif_usb_probe,
1026 .disconnect = ath9k_hif_usb_disconnect,
1027#ifdef CONFIG_PM
1028 .suspend = ath9k_hif_usb_suspend,
1029 .resume = ath9k_hif_usb_resume,
1030 .reset_resume = ath9k_hif_usb_resume,
1031#endif
1032 .id_table = ath9k_hif_usb_ids,
1033 .soft_unbind = 1,
1034};
1035
1036int ath9k_hif_usb_init(void)
1037{
1038 return usb_register(&ath9k_hif_usb_driver);
1039}
1040
1041void ath9k_hif_usb_exit(void)
1042{
1043 usb_deregister(&ath9k_hif_usb_driver);
1044}