blob: 5f3ea7091ae085240b64ae519d2c2534c9bda15e [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[] = {
Sujithce43cee2010-06-02 15:53:30 +053020 { USB_DEVICE(0x0cf3, 0x9271) },
21 { USB_DEVICE(0x0cf3, 0x1006) },
Sujithb1762862010-06-02 15:53:34 +053022 { USB_DEVICE(0x0cf3, 0x7010) },
Sujithfb9987d2010-03-17 14:25:25 +053023 { },
24};
25
26MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
27
28static int __hif_usb_tx(struct hif_device_usb *hif_dev);
29
30static void hif_usb_regout_cb(struct urb *urb)
31{
32 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
Sujithfb9987d2010-03-17 14:25:25 +053033
34 switch (urb->status) {
35 case 0:
36 break;
37 case -ENOENT:
38 case -ECONNRESET:
Sujithfb9987d2010-03-17 14:25:25 +053039 case -ENODEV:
40 case -ESHUTDOWN:
Sujith6f0f2662010-04-06 15:28:17 +053041 goto free;
Sujithfb9987d2010-03-17 14:25:25 +053042 default:
43 break;
44 }
45
46 if (cmd) {
47 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
48 cmd->skb, 1);
49 kfree(cmd);
Sujithfb9987d2010-03-17 14:25:25 +053050 }
Sujith6f0f2662010-04-06 15:28:17 +053051
52 return;
53free:
Ming Lei0fa35a52010-04-13 00:29:15 +080054 kfree_skb(cmd->skb);
Sujith6f0f2662010-04-06 15:28:17 +053055 kfree(cmd);
Sujithfb9987d2010-03-17 14:25:25 +053056}
57
58static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
59 struct sk_buff *skb)
60{
61 struct urb *urb;
62 struct cmd_buf *cmd;
63 int ret = 0;
64
65 urb = usb_alloc_urb(0, GFP_KERNEL);
66 if (urb == NULL)
67 return -ENOMEM;
68
69 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
70 if (cmd == NULL) {
71 usb_free_urb(urb);
72 return -ENOMEM;
73 }
74
75 cmd->skb = skb;
76 cmd->hif_dev = hif_dev;
77
78 usb_fill_int_urb(urb, hif_dev->udev,
79 usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
80 skb->data, skb->len,
81 hif_usb_regout_cb, cmd, 1);
82
Sujith6f0f2662010-04-06 15:28:17 +053083 usb_anchor_urb(urb, &hif_dev->regout_submitted);
Sujithfb9987d2010-03-17 14:25:25 +053084 ret = usb_submit_urb(urb, GFP_KERNEL);
85 if (ret) {
Sujith6f0f2662010-04-06 15:28:17 +053086 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +053087 kfree(cmd);
88 }
Sujith6f0f2662010-04-06 15:28:17 +053089 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +053090
91 return ret;
92}
93
Sujitheac8e382010-04-16 11:54:00 +053094static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
95 struct sk_buff_head *list)
Ming Leif8e1d082010-04-13 00:29:27 +080096{
97 struct sk_buff *skb;
Sujitheac8e382010-04-16 11:54:00 +053098
99 while ((skb = __skb_dequeue(list)) != NULL) {
Ming Leif8e1d082010-04-13 00:29:27 +0800100 dev_kfree_skb_any(skb);
Sujitheac8e382010-04-16 11:54:00 +0530101 TX_STAT_INC(skb_dropped);
102 }
Ming Leif8e1d082010-04-13 00:29:27 +0800103}
104
Sujithc11d8f82010-04-23 10:28:09 +0530105static void hif_usb_tx_cb(struct urb *urb)
106{
107 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
Dan Carpenter690e7812010-05-14 16:50:56 +0200108 struct hif_device_usb *hif_dev;
Sujithc11d8f82010-04-23 10:28:09 +0530109 struct sk_buff *skb;
110
Dan Carpenter690e7812010-05-14 16:50:56 +0200111 if (!tx_buf || !tx_buf->hif_dev)
Sujithc11d8f82010-04-23 10:28:09 +0530112 return;
113
Dan Carpenter690e7812010-05-14 16:50:56 +0200114 hif_dev = tx_buf->hif_dev;
115
Sujithc11d8f82010-04-23 10:28:09 +0530116 switch (urb->status) {
117 case 0:
118 break;
119 case -ENOENT:
120 case -ECONNRESET:
121 case -ENODEV:
122 case -ESHUTDOWN:
123 /*
124 * The URB has been killed, free the SKBs
125 * and return.
126 */
127 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
128 return;
129 default:
130 break;
131 }
132
133 /* Check if TX has been stopped */
134 spin_lock(&hif_dev->tx.tx_lock);
135 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
136 spin_unlock(&hif_dev->tx.tx_lock);
137 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
138 goto add_free;
139 }
140 spin_unlock(&hif_dev->tx.tx_lock);
141
142 /* Complete the queued SKBs. */
143 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
144 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
145 skb, 1);
146 TX_STAT_INC(skb_completed);
147 }
148
149add_free:
150 /* Re-initialize the SKB queue */
151 tx_buf->len = tx_buf->offset = 0;
152 __skb_queue_head_init(&tx_buf->skb_queue);
153
154 /* Add this TX buffer to the free list */
155 spin_lock(&hif_dev->tx.tx_lock);
156 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
157 hif_dev->tx.tx_buf_cnt++;
158 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
159 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
160 TX_STAT_INC(buf_completed);
161 spin_unlock(&hif_dev->tx.tx_lock);
162}
163
Sujithfb9987d2010-03-17 14:25:25 +0530164/* TX lock has to be taken */
165static int __hif_usb_tx(struct hif_device_usb *hif_dev)
166{
167 struct tx_buf *tx_buf = NULL;
168 struct sk_buff *nskb = NULL;
169 int ret = 0, i;
170 u16 *hdr, tx_skb_cnt = 0;
171 u8 *buf;
172
173 if (hif_dev->tx.tx_skb_cnt == 0)
174 return 0;
175
176 /* Check if a free TX buffer is available */
177 if (list_empty(&hif_dev->tx.tx_buf))
178 return 0;
179
180 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
Sujithc11d8f82010-04-23 10:28:09 +0530181 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
Sujithfb9987d2010-03-17 14:25:25 +0530182 hif_dev->tx.tx_buf_cnt--;
183
184 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
185
186 for (i = 0; i < tx_skb_cnt; i++) {
187 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
188
189 /* Should never be NULL */
190 BUG_ON(!nskb);
191
192 hif_dev->tx.tx_skb_cnt--;
193
194 buf = tx_buf->buf;
195 buf += tx_buf->offset;
196 hdr = (u16 *)buf;
197 *hdr++ = nskb->len;
198 *hdr++ = ATH_USB_TX_STREAM_MODE_TAG;
199 buf += 4;
200 memcpy(buf, nskb->data, nskb->len);
201 tx_buf->len = nskb->len + 4;
202
203 if (i < (tx_skb_cnt - 1))
204 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
205
206 if (i == (tx_skb_cnt - 1))
207 tx_buf->len += tx_buf->offset;
208
209 __skb_queue_tail(&tx_buf->skb_queue, nskb);
210 TX_STAT_INC(skb_queued);
211 }
212
213 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
214 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
215 tx_buf->buf, tx_buf->len,
216 hif_usb_tx_cb, tx_buf);
217
218 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
219 if (ret) {
220 tx_buf->len = tx_buf->offset = 0;
Sujitheac8e382010-04-16 11:54:00 +0530221 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
Sujithfb9987d2010-03-17 14:25:25 +0530222 __skb_queue_head_init(&tx_buf->skb_queue);
223 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
224 hif_dev->tx.tx_buf_cnt++;
225 }
226
227 if (!ret)
228 TX_STAT_INC(buf_queued);
229
230 return ret;
231}
232
233static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
234 struct ath9k_htc_tx_ctl *tx_ctl)
235{
236 unsigned long flags;
237
238 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
239
240 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
241 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
242 return -ENODEV;
243 }
244
245 /* Check if the max queue count has been reached */
246 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
247 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
248 return -ENOMEM;
249 }
250
251 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
252 hif_dev->tx.tx_skb_cnt++;
253
254 /* Send normal frames immediately */
255 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
256 __hif_usb_tx(hif_dev);
257
258 /* Check if AMPDUs have to be sent immediately */
259 if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
260 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
261 (hif_dev->tx.tx_skb_cnt < 2)) {
262 __hif_usb_tx(hif_dev);
263 }
264
265 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
266
267 return 0;
268}
269
270static void hif_usb_start(void *hif_handle, u8 pipe_id)
271{
272 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
273 unsigned long flags;
274
275 hif_dev->flags |= HIF_USB_START;
276
277 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
278 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
279 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
280}
281
282static void hif_usb_stop(void *hif_handle, u8 pipe_id)
283{
284 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
285 unsigned long flags;
286
287 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
Sujitheac8e382010-04-16 11:54:00 +0530288 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
Sujithfb9987d2010-03-17 14:25:25 +0530289 hif_dev->tx.tx_skb_cnt = 0;
290 hif_dev->tx.flags |= HIF_USB_TX_STOP;
291 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
292}
293
294static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
295 struct ath9k_htc_tx_ctl *tx_ctl)
296{
297 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
298 int ret = 0;
299
300 switch (pipe_id) {
301 case USB_WLAN_TX_PIPE:
302 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
303 break;
304 case USB_REG_OUT_PIPE:
305 ret = hif_usb_send_regout(hif_dev, skb);
306 break;
307 default:
Sujith6335ed02010-03-29 16:07:15 +0530308 dev_err(&hif_dev->udev->dev,
309 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
Sujithfb9987d2010-03-17 14:25:25 +0530310 ret = -EINVAL;
311 break;
312 }
313
314 return ret;
315}
316
317static struct ath9k_htc_hif hif_usb = {
318 .transport = ATH9K_HIF_USB,
319 .name = "ath9k_hif_usb",
320
321 .control_ul_pipe = USB_REG_OUT_PIPE,
322 .control_dl_pipe = USB_REG_IN_PIPE,
323
324 .start = hif_usb_start,
325 .stop = hif_usb_stop,
326 .send = hif_usb_send,
327};
328
329static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
330 struct sk_buff *skb)
331{
Sujithc5032692010-04-06 15:28:15 +0530332 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
Sujithfb9987d2010-03-17 14:25:25 +0530333 int index = 0, i = 0, chk_idx, len = skb->len;
334 int rx_remain_len = 0, rx_pkt_len = 0;
335 u16 pkt_len, pkt_tag, pool_index = 0;
336 u8 *ptr;
337
Sujith46baa1a2010-04-06 15:28:11 +0530338 spin_lock(&hif_dev->rx_lock);
339
Sujithfb9987d2010-03-17 14:25:25 +0530340 rx_remain_len = hif_dev->rx_remain_len;
341 rx_pkt_len = hif_dev->rx_transfer_len;
342
343 if (rx_remain_len != 0) {
344 struct sk_buff *remain_skb = hif_dev->remain_skb;
345
346 if (remain_skb) {
347 ptr = (u8 *) remain_skb->data;
348
349 index = rx_remain_len;
350 rx_remain_len -= hif_dev->rx_pad_len;
351 ptr += rx_pkt_len;
352
353 memcpy(ptr, skb->data, rx_remain_len);
354
355 rx_pkt_len += rx_remain_len;
356 hif_dev->rx_remain_len = 0;
357 skb_put(remain_skb, rx_pkt_len);
358
359 skb_pool[pool_index++] = remain_skb;
360
361 } else {
362 index = rx_remain_len;
363 }
364 }
365
Sujith46baa1a2010-04-06 15:28:11 +0530366 spin_unlock(&hif_dev->rx_lock);
367
Sujithfb9987d2010-03-17 14:25:25 +0530368 while (index < len) {
369 ptr = (u8 *) skb->data;
370
371 pkt_len = ptr[index] + (ptr[index+1] << 8);
372 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
373
374 if (pkt_tag == ATH_USB_RX_STREAM_MODE_TAG) {
375 u16 pad_len;
376
377 pad_len = 4 - (pkt_len & 0x3);
378 if (pad_len == 4)
379 pad_len = 0;
380
381 chk_idx = index;
382 index = index + 4 + pkt_len + pad_len;
383
384 if (index > MAX_RX_BUF_SIZE) {
Sujith46baa1a2010-04-06 15:28:11 +0530385 spin_lock(&hif_dev->rx_lock);
Sujithfb9987d2010-03-17 14:25:25 +0530386 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
387 hif_dev->rx_transfer_len =
388 MAX_RX_BUF_SIZE - chk_idx - 4;
389 hif_dev->rx_pad_len = pad_len;
390
391 nskb = __dev_alloc_skb(pkt_len + 32,
392 GFP_ATOMIC);
393 if (!nskb) {
394 dev_err(&hif_dev->udev->dev,
395 "ath9k_htc: RX memory allocation"
396 " error\n");
Sujith46baa1a2010-04-06 15:28:11 +0530397 spin_unlock(&hif_dev->rx_lock);
Sujithfb9987d2010-03-17 14:25:25 +0530398 goto err;
399 }
400 skb_reserve(nskb, 32);
401 RX_STAT_INC(skb_allocated);
402
403 memcpy(nskb->data, &(skb->data[chk_idx+4]),
404 hif_dev->rx_transfer_len);
405
406 /* Record the buffer pointer */
407 hif_dev->remain_skb = nskb;
Sujith46baa1a2010-04-06 15:28:11 +0530408 spin_unlock(&hif_dev->rx_lock);
Sujithfb9987d2010-03-17 14:25:25 +0530409 } else {
410 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
411 if (!nskb) {
412 dev_err(&hif_dev->udev->dev,
413 "ath9k_htc: RX memory allocation"
414 " error\n");
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]), pkt_len);
421 skb_put(nskb, pkt_len);
422 skb_pool[pool_index++] = nskb;
423 }
424 } else {
425 RX_STAT_INC(skb_dropped);
Sujithfb9987d2010-03-17 14:25:25 +0530426 return;
427 }
428 }
429
430err:
Sujithfb9987d2010-03-17 14:25:25 +0530431 for (i = 0; i < pool_index; i++) {
432 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
433 skb_pool[i]->len, USB_WLAN_RX_PIPE);
434 RX_STAT_INC(skb_completed);
435 }
436}
437
438static void ath9k_hif_usb_rx_cb(struct urb *urb)
439{
440 struct sk_buff *skb = (struct sk_buff *) urb->context;
Sujithfb9987d2010-03-17 14:25:25 +0530441 struct hif_device_usb *hif_dev = (struct hif_device_usb *)
442 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
443 int ret;
444
Sujith6335ed02010-03-29 16:07:15 +0530445 if (!skb)
446 return;
447
Sujithfb9987d2010-03-17 14:25:25 +0530448 if (!hif_dev)
449 goto free;
450
451 switch (urb->status) {
452 case 0:
453 break;
454 case -ENOENT:
455 case -ECONNRESET:
456 case -ENODEV:
457 case -ESHUTDOWN:
458 goto free;
459 default:
460 goto resubmit;
461 }
462
463 if (likely(urb->actual_length != 0)) {
464 skb_put(skb, urb->actual_length);
Sujithfb9987d2010-03-17 14:25:25 +0530465 ath9k_hif_usb_rx_stream(hif_dev, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530466 }
467
468resubmit:
469 skb_reset_tail_pointer(skb);
470 skb_trim(skb, 0);
471
Sujith6335ed02010-03-29 16:07:15 +0530472 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530473 ret = usb_submit_urb(urb, GFP_ATOMIC);
Sujith6335ed02010-03-29 16:07:15 +0530474 if (ret) {
475 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530476 goto free;
Sujith6335ed02010-03-29 16:07:15 +0530477 }
Sujithfb9987d2010-03-17 14:25:25 +0530478
479 return;
480free:
Ming Leif28a7b32010-04-13 00:28:53 +0800481 kfree_skb(skb);
Sujithfb9987d2010-03-17 14:25:25 +0530482}
483
484static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
485{
486 struct sk_buff *skb = (struct sk_buff *) urb->context;
487 struct sk_buff *nskb;
488 struct hif_device_usb *hif_dev = (struct hif_device_usb *)
489 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
490 int ret;
491
Sujith6335ed02010-03-29 16:07:15 +0530492 if (!skb)
493 return;
494
Sujithfb9987d2010-03-17 14:25:25 +0530495 if (!hif_dev)
496 goto free;
497
498 switch (urb->status) {
499 case 0:
500 break;
501 case -ENOENT:
502 case -ECONNRESET:
503 case -ENODEV:
504 case -ESHUTDOWN:
505 goto free;
506 default:
507 goto resubmit;
508 }
509
510 if (likely(urb->actual_length != 0)) {
511 skb_put(skb, urb->actual_length);
512
Sujith5ab0af32010-04-23 10:28:17 +0530513 /* Process the command first */
514 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
515 skb->len, USB_REG_IN_PIPE);
516
517
Ming Leie6c6d332010-04-13 00:29:05 +0800518 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
Sujith5ab0af32010-04-23 10:28:17 +0530519 if (!nskb) {
520 dev_err(&hif_dev->udev->dev,
521 "ath9k_htc: REG_IN memory allocation failure\n");
522 urb->context = NULL;
523 return;
524 }
Sujithfb9987d2010-03-17 14:25:25 +0530525
526 usb_fill_int_urb(urb, hif_dev->udev,
527 usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
528 nskb->data, MAX_REG_IN_BUF_SIZE,
529 ath9k_hif_usb_reg_in_cb, nskb, 1);
530
531 ret = usb_submit_urb(urb, GFP_ATOMIC);
532 if (ret) {
Ming Leie6c6d332010-04-13 00:29:05 +0800533 kfree_skb(nskb);
Sujith5ab0af32010-04-23 10:28:17 +0530534 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530535 }
536
Sujithfb9987d2010-03-17 14:25:25 +0530537 return;
538 }
539
540resubmit:
541 skb_reset_tail_pointer(skb);
542 skb_trim(skb, 0);
543
544 ret = usb_submit_urb(urb, GFP_ATOMIC);
545 if (ret)
546 goto free;
547
548 return;
549free:
Ming Leie6c6d332010-04-13 00:29:05 +0800550 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530551 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530552}
553
554static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
555{
Sujithfb9987d2010-03-17 14:25:25 +0530556 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
557
Sujithc11d8f82010-04-23 10:28:09 +0530558 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
559 &hif_dev->tx.tx_buf, list) {
560 usb_kill_urb(tx_buf->urb);
Sujithfb9987d2010-03-17 14:25:25 +0530561 list_del(&tx_buf->list);
562 usb_free_urb(tx_buf->urb);
563 kfree(tx_buf->buf);
564 kfree(tx_buf);
565 }
566
Sujithfb9987d2010-03-17 14:25:25 +0530567 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
568 &hif_dev->tx.tx_pending, list) {
569 usb_kill_urb(tx_buf->urb);
570 list_del(&tx_buf->list);
571 usb_free_urb(tx_buf->urb);
572 kfree(tx_buf->buf);
573 kfree(tx_buf);
574 }
Sujithfb9987d2010-03-17 14:25:25 +0530575}
576
577static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
578{
579 struct tx_buf *tx_buf;
580 int i;
581
582 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
583 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
584 spin_lock_init(&hif_dev->tx.tx_lock);
585 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
586
587 for (i = 0; i < MAX_TX_URB_NUM; i++) {
588 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
589 if (!tx_buf)
590 goto err;
591
592 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
593 if (!tx_buf->buf)
594 goto err;
595
596 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
597 if (!tx_buf->urb)
598 goto err;
599
600 tx_buf->hif_dev = hif_dev;
601 __skb_queue_head_init(&tx_buf->skb_queue);
602
603 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
604 }
605
606 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
607
608 return 0;
609err:
Dan Carpenter76066882010-05-14 16:52:37 +0200610 if (tx_buf) {
611 kfree(tx_buf->buf);
612 kfree(tx_buf);
613 }
Sujithfb9987d2010-03-17 14:25:25 +0530614 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
615 return -ENOMEM;
616}
617
Sujithfb9987d2010-03-17 14:25:25 +0530618static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
619{
Sujith6335ed02010-03-29 16:07:15 +0530620 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530621}
622
623static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
624{
Sujith6335ed02010-03-29 16:07:15 +0530625 struct urb *urb = NULL;
626 struct sk_buff *skb = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530627 int i, ret;
628
Sujith6335ed02010-03-29 16:07:15 +0530629 init_usb_anchor(&hif_dev->rx_submitted);
Sujith46baa1a2010-04-06 15:28:11 +0530630 spin_lock_init(&hif_dev->rx_lock);
Sujith6335ed02010-03-29 16:07:15 +0530631
Sujithfb9987d2010-03-17 14:25:25 +0530632 for (i = 0; i < MAX_RX_URB_NUM; i++) {
633
634 /* Allocate URB */
Sujith6335ed02010-03-29 16:07:15 +0530635 urb = usb_alloc_urb(0, GFP_KERNEL);
636 if (urb == NULL) {
Sujithfb9987d2010-03-17 14:25:25 +0530637 ret = -ENOMEM;
Sujith6335ed02010-03-29 16:07:15 +0530638 goto err_urb;
Sujithfb9987d2010-03-17 14:25:25 +0530639 }
640
641 /* Allocate buffer */
Ming Leif28a7b32010-04-13 00:28:53 +0800642 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
Sujith6335ed02010-03-29 16:07:15 +0530643 if (!skb) {
644 ret = -ENOMEM;
645 goto err_skb;
646 }
647
648 usb_fill_bulk_urb(urb, hif_dev->udev,
649 usb_rcvbulkpipe(hif_dev->udev,
650 USB_WLAN_RX_PIPE),
651 skb->data, MAX_RX_BUF_SIZE,
652 ath9k_hif_usb_rx_cb, skb);
653
654 /* Anchor URB */
655 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530656
657 /* Submit URB */
Sujith6335ed02010-03-29 16:07:15 +0530658 ret = usb_submit_urb(urb, GFP_KERNEL);
659 if (ret) {
660 usb_unanchor_urb(urb);
661 goto err_submit;
662 }
Sujith66b10e32010-04-06 15:28:13 +0530663
664 /*
665 * Drop reference count.
666 * This ensures that the URB is freed when killing them.
667 */
668 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530669 }
670
671 return 0;
672
Sujith6335ed02010-03-29 16:07:15 +0530673err_submit:
Ming Leif28a7b32010-04-13 00:28:53 +0800674 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530675err_skb:
676 usb_free_urb(urb);
677err_urb:
Sujithfb9987d2010-03-17 14:25:25 +0530678 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
679 return ret;
680}
681
682static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
683{
684 if (hif_dev->reg_in_urb) {
685 usb_kill_urb(hif_dev->reg_in_urb);
Sujith6335ed02010-03-29 16:07:15 +0530686 if (hif_dev->reg_in_urb->context)
Ming Leie6c6d332010-04-13 00:29:05 +0800687 kfree_skb((void *)hif_dev->reg_in_urb->context);
Sujithfb9987d2010-03-17 14:25:25 +0530688 usb_free_urb(hif_dev->reg_in_urb);
689 hif_dev->reg_in_urb = NULL;
690 }
691}
692
693static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
694{
695 struct sk_buff *skb;
696
697 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
698 if (hif_dev->reg_in_urb == NULL)
699 return -ENOMEM;
700
Ming Leie6c6d332010-04-13 00:29:05 +0800701 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
Sujithfb9987d2010-03-17 14:25:25 +0530702 if (!skb)
703 goto err;
704
705 usb_fill_int_urb(hif_dev->reg_in_urb, hif_dev->udev,
706 usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
707 skb->data, MAX_REG_IN_BUF_SIZE,
708 ath9k_hif_usb_reg_in_cb, skb, 1);
709
710 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
Sujith6335ed02010-03-29 16:07:15 +0530711 goto err;
Sujithfb9987d2010-03-17 14:25:25 +0530712
713 return 0;
714
Sujithfb9987d2010-03-17 14:25:25 +0530715err:
716 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
717 return -ENOMEM;
718}
719
720static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
721{
Sujith6f0f2662010-04-06 15:28:17 +0530722 /* Register Write */
723 init_usb_anchor(&hif_dev->regout_submitted);
724
Sujithfb9987d2010-03-17 14:25:25 +0530725 /* TX */
726 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
727 goto err;
728
729 /* RX */
730 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
731 goto err;
732
Sujith6f0f2662010-04-06 15:28:17 +0530733 /* Register Read */
Sujithfb9987d2010-03-17 14:25:25 +0530734 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
735 goto err;
736
737 return 0;
738err:
739 return -ENOMEM;
740}
741
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530742static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
743{
744 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
745 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
746 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
747 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
748}
749
Sujithfb9987d2010-03-17 14:25:25 +0530750static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
751{
752 int transfer, err;
753 const void *data = hif_dev->firmware->data;
754 size_t len = hif_dev->firmware->size;
755 u32 addr = AR9271_FIRMWARE;
756 u8 *buf = kzalloc(4096, GFP_KERNEL);
Sujithb1762862010-06-02 15:53:34 +0530757 u32 firm_offset;
Sujithfb9987d2010-03-17 14:25:25 +0530758
759 if (!buf)
760 return -ENOMEM;
761
762 while (len) {
763 transfer = min_t(int, len, 4096);
764 memcpy(buf, data, transfer);
765
766 err = usb_control_msg(hif_dev->udev,
767 usb_sndctrlpipe(hif_dev->udev, 0),
768 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
769 addr >> 8, 0, buf, transfer, HZ);
770 if (err < 0) {
771 kfree(buf);
772 return err;
773 }
774
775 len -= transfer;
776 data += transfer;
777 addr += transfer;
778 }
779 kfree(buf);
780
Sujithb1762862010-06-02 15:53:34 +0530781 if (hif_dev->device_id == 0x7010)
782 firm_offset = AR7010_FIRMWARE_TEXT;
783 else
784 firm_offset = AR9271_FIRMWARE_TEXT;
785
Sujithfb9987d2010-03-17 14:25:25 +0530786 /*
787 * Issue FW download complete command to firmware.
788 */
789 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
790 FIRMWARE_DOWNLOAD_COMP,
791 0x40 | USB_DIR_OUT,
Sujithb1762862010-06-02 15:53:34 +0530792 firm_offset >> 8, 0, NULL, 0, HZ);
Sujithfb9987d2010-03-17 14:25:25 +0530793 if (err)
794 return -EIO;
795
796 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
Sujithce43cee2010-06-02 15:53:30 +0530797 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
Sujithfb9987d2010-03-17 14:25:25 +0530798
799 return 0;
800}
801
Sujithce43cee2010-06-02 15:53:30 +0530802static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
Sujithfb9987d2010-03-17 14:25:25 +0530803{
804 int ret;
805
806 /* Request firmware */
Sujithce43cee2010-06-02 15:53:30 +0530807 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
808 &hif_dev->udev->dev);
Sujithfb9987d2010-03-17 14:25:25 +0530809 if (ret) {
810 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +0530811 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
Sujithfb9987d2010-03-17 14:25:25 +0530812 goto err_fw_req;
813 }
814
Sujithfb9987d2010-03-17 14:25:25 +0530815 /* Alloc URBs */
816 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
817 if (ret) {
818 dev_err(&hif_dev->udev->dev,
819 "ath9k_htc: Unable to allocate URBs\n");
820 goto err_urb;
821 }
822
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530823 /* Download firmware */
824 ret = ath9k_hif_usb_download_fw(hif_dev);
825 if (ret) {
826 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +0530827 "ath9k_htc: Firmware - %s download failed\n",
828 hif_dev->fw_name);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530829 goto err_fw_download;
830 }
831
Sujithfb9987d2010-03-17 14:25:25 +0530832 return 0;
833
Sujithfb9987d2010-03-17 14:25:25 +0530834err_fw_download:
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530835 ath9k_hif_usb_dealloc_urbs(hif_dev);
836err_urb:
Sujithfb9987d2010-03-17 14:25:25 +0530837 release_firmware(hif_dev->firmware);
838err_fw_req:
839 hif_dev->firmware = NULL;
840 return ret;
841}
842
Sujithfb9987d2010-03-17 14:25:25 +0530843static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
844{
845 ath9k_hif_usb_dealloc_urbs(hif_dev);
846 if (hif_dev->firmware)
847 release_firmware(hif_dev->firmware);
848}
849
850static int ath9k_hif_usb_probe(struct usb_interface *interface,
851 const struct usb_device_id *id)
852{
853 struct usb_device *udev = interface_to_usbdev(interface);
854 struct hif_device_usb *hif_dev;
Sujithfb9987d2010-03-17 14:25:25 +0530855 int ret = 0;
856
857 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
858 if (!hif_dev) {
859 ret = -ENOMEM;
860 goto err_alloc;
861 }
862
863 usb_get_dev(udev);
864 hif_dev->udev = udev;
865 hif_dev->interface = interface;
866 hif_dev->device_id = id->idProduct;
867#ifdef CONFIG_PM
868 udev->reset_resume = 1;
869#endif
870 usb_set_intfdata(interface, hif_dev);
871
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +0530872 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
873 &hif_dev->udev->dev);
874 if (hif_dev->htc_handle == NULL) {
875 ret = -ENOMEM;
876 goto err_htc_hw_alloc;
877 }
878
Sujithce43cee2010-06-02 15:53:30 +0530879 /* Find out which firmware to load */
880
881 switch(hif_dev->device_id) {
882 case 0x9271:
883 case 0x1006:
884 hif_dev->fw_name = "ar9271.fw";
885 break;
Sujithb1762862010-06-02 15:53:34 +0530886 case 0x7010:
887 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
888 hif_dev->fw_name = "ar7010_1_1.fw";
889 else
890 hif_dev->fw_name = "ar7010.fw";
891 break;
Sujithce43cee2010-06-02 15:53:30 +0530892 default:
893 break;
894 }
895
896 if (!hif_dev->fw_name) {
897 dev_err(&udev->dev, "Can't determine firmware !\n");
898 goto err_htc_hw_alloc;
899 }
900
901 ret = ath9k_hif_usb_dev_init(hif_dev);
Sujithfb9987d2010-03-17 14:25:25 +0530902 if (ret) {
903 ret = -EINVAL;
904 goto err_hif_init_usb;
905 }
906
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +0530907 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
908 &hif_dev->udev->dev, hif_dev->device_id);
Sujithfb9987d2010-03-17 14:25:25 +0530909 if (ret) {
910 ret = -EINVAL;
911 goto err_htc_hw_init;
912 }
913
914 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
915
916 return 0;
917
918err_htc_hw_init:
Sujithfb9987d2010-03-17 14:25:25 +0530919 ath9k_hif_usb_dev_deinit(hif_dev);
920err_hif_init_usb:
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +0530921 ath9k_htc_hw_free(hif_dev->htc_handle);
922err_htc_hw_alloc:
Sujithfb9987d2010-03-17 14:25:25 +0530923 usb_set_intfdata(interface, NULL);
924 kfree(hif_dev);
925 usb_put_dev(udev);
926err_alloc:
927 return ret;
928}
929
Sujith62e47162010-04-23 10:28:16 +0530930static void ath9k_hif_usb_reboot(struct usb_device *udev)
931{
932 u32 reboot_cmd = 0xffffffff;
933 void *buf;
934 int ret;
935
Julia Lawalla465a2c2010-05-15 23:17:19 +0200936 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
Sujith62e47162010-04-23 10:28:16 +0530937 if (!buf)
938 return;
939
Sujith62e47162010-04-23 10:28:16 +0530940 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
941 buf, 4, NULL, HZ);
942 if (ret)
943 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
944
945 kfree(buf);
946}
947
Sujithfb9987d2010-03-17 14:25:25 +0530948static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
949{
950 struct usb_device *udev = interface_to_usbdev(interface);
951 struct hif_device_usb *hif_dev =
952 (struct hif_device_usb *) usb_get_intfdata(interface);
953
954 if (hif_dev) {
Sujithd8f996f2010-04-23 10:28:20 +0530955 ath9k_htc_hw_deinit(hif_dev->htc_handle,
956 (udev->state == USB_STATE_NOTATTACHED) ? true : false);
Sujithfb9987d2010-03-17 14:25:25 +0530957 ath9k_htc_hw_free(hif_dev->htc_handle);
958 ath9k_hif_usb_dev_deinit(hif_dev);
959 usb_set_intfdata(interface, NULL);
960 }
961
962 if (hif_dev->flags & HIF_USB_START)
Sujith62e47162010-04-23 10:28:16 +0530963 ath9k_hif_usb_reboot(udev);
Sujithfb9987d2010-03-17 14:25:25 +0530964
965 kfree(hif_dev);
966 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
967 usb_put_dev(udev);
968}
969
970#ifdef CONFIG_PM
971static int ath9k_hif_usb_suspend(struct usb_interface *interface,
972 pm_message_t message)
973{
974 struct hif_device_usb *hif_dev =
975 (struct hif_device_usb *) usb_get_intfdata(interface);
976
977 ath9k_hif_usb_dealloc_urbs(hif_dev);
978
979 return 0;
980}
981
982static int ath9k_hif_usb_resume(struct usb_interface *interface)
983{
984 struct hif_device_usb *hif_dev =
985 (struct hif_device_usb *) usb_get_intfdata(interface);
986 int ret;
987
988 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
989 if (ret)
990 return ret;
991
992 if (hif_dev->firmware) {
993 ret = ath9k_hif_usb_download_fw(hif_dev);
994 if (ret)
995 goto fail_resume;
996 } else {
997 ath9k_hif_usb_dealloc_urbs(hif_dev);
998 return -EIO;
999 }
1000
1001 mdelay(100);
1002
1003 ret = ath9k_htc_resume(hif_dev->htc_handle);
1004
1005 if (ret)
1006 goto fail_resume;
1007
1008 return 0;
1009
1010fail_resume:
1011 ath9k_hif_usb_dealloc_urbs(hif_dev);
1012
1013 return ret;
1014}
1015#endif
1016
1017static struct usb_driver ath9k_hif_usb_driver = {
1018 .name = "ath9k_hif_usb",
1019 .probe = ath9k_hif_usb_probe,
1020 .disconnect = ath9k_hif_usb_disconnect,
1021#ifdef CONFIG_PM
1022 .suspend = ath9k_hif_usb_suspend,
1023 .resume = ath9k_hif_usb_resume,
1024 .reset_resume = ath9k_hif_usb_resume,
1025#endif
1026 .id_table = ath9k_hif_usb_ids,
1027 .soft_unbind = 1,
1028};
1029
1030int ath9k_hif_usb_init(void)
1031{
1032 return usb_register(&ath9k_hif_usb_driver);
1033}
1034
1035void ath9k_hif_usb_exit(void)
1036{
1037 usb_deregister(&ath9k_hif_usb_driver);
1038}