blob: 5ab3084eb9cb589a773f9f5c21d0181b9f67cdbd [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
John W. Linvilled0ee0eb2010-06-23 14:20:45 -040019/* identify firmware images */
20#define FIRMWARE_AR7010 "ar7010.fw"
21#define FIRMWARE_AR7010_1_1 "ar7010_1_1.fw"
22#define FIRMWARE_AR9271 "ar9271.fw"
23
24MODULE_FIRMWARE(FIRMWARE_AR7010);
25MODULE_FIRMWARE(FIRMWARE_AR7010_1_1);
26MODULE_FIRMWARE(FIRMWARE_AR9271);
27
Sujithfb9987d2010-03-17 14:25:25 +053028static struct usb_device_id ath9k_hif_usb_ids[] = {
Sujith4e63f762010-06-17 10:29:01 +053029 { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
30 { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
Sujith4e63f762010-06-17 10:29:01 +053031 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
Sujith4e63f762010-06-17 10:29:01 +053032 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
33 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
34 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
Haitao Zhangac618d72010-11-07 12:50:24 +080035 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
Rajkumar Manoharan32b08952010-11-10 17:51:24 +053036 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
37 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
38 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
Sujith4e63f762010-06-17 10:29:01 +053039 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
Rajkumar Manoharan32b08952010-11-10 17:51:24 +053040 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
Sujith Manoharan452d7dd2010-12-13 07:39:32 +053041 { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
Sujith Manoharan0b5ead92010-12-07 16:31:38 +053042
43 { USB_DEVICE(0x0cf3, 0x7015),
44 .driver_info = AR9287_USB }, /* Atheros */
Rajkumar Manoharan64f12172010-11-19 16:53:20 +053045 { USB_DEVICE(0x1668, 0x1200),
Sujith Manoharan0b5ead92010-12-07 16:31:38 +053046 .driver_info = AR9287_USB }, /* Verizon */
47
48 { USB_DEVICE(0x0cf3, 0x7010),
49 .driver_info = AR9280_USB }, /* Atheros */
50 { USB_DEVICE(0x0846, 0x9018),
51 .driver_info = AR9280_USB }, /* Netgear WNDA3200 */
52 { USB_DEVICE(0x083A, 0xA704),
53 .driver_info = AR9280_USB }, /* SMC Networks */
54
Sujithfb9987d2010-03-17 14:25:25 +053055 { },
56};
57
58MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
59
60static int __hif_usb_tx(struct hif_device_usb *hif_dev);
61
62static void hif_usb_regout_cb(struct urb *urb)
63{
64 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
Sujithfb9987d2010-03-17 14:25:25 +053065
66 switch (urb->status) {
67 case 0:
68 break;
69 case -ENOENT:
70 case -ECONNRESET:
Sujithfb9987d2010-03-17 14:25:25 +053071 case -ENODEV:
72 case -ESHUTDOWN:
Sujith6f0f2662010-04-06 15:28:17 +053073 goto free;
Sujithfb9987d2010-03-17 14:25:25 +053074 default:
75 break;
76 }
77
78 if (cmd) {
79 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
80 cmd->skb, 1);
81 kfree(cmd);
Sujithfb9987d2010-03-17 14:25:25 +053082 }
Sujith6f0f2662010-04-06 15:28:17 +053083
84 return;
85free:
Ming Lei0fa35a52010-04-13 00:29:15 +080086 kfree_skb(cmd->skb);
Sujith6f0f2662010-04-06 15:28:17 +053087 kfree(cmd);
Sujithfb9987d2010-03-17 14:25:25 +053088}
89
90static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
91 struct sk_buff *skb)
92{
93 struct urb *urb;
94 struct cmd_buf *cmd;
95 int ret = 0;
96
97 urb = usb_alloc_urb(0, GFP_KERNEL);
98 if (urb == NULL)
99 return -ENOMEM;
100
101 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
102 if (cmd == NULL) {
103 usb_free_urb(urb);
104 return -ENOMEM;
105 }
106
107 cmd->skb = skb;
108 cmd->hif_dev = hif_dev;
109
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530110 usb_fill_bulk_urb(urb, hif_dev->udev,
111 usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530112 skb->data, skb->len,
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530113 hif_usb_regout_cb, cmd);
Sujithfb9987d2010-03-17 14:25:25 +0530114
Sujith6f0f2662010-04-06 15:28:17 +0530115 usb_anchor_urb(urb, &hif_dev->regout_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530116 ret = usb_submit_urb(urb, GFP_KERNEL);
117 if (ret) {
Sujith6f0f2662010-04-06 15:28:17 +0530118 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530119 kfree(cmd);
120 }
Sujith6f0f2662010-04-06 15:28:17 +0530121 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530122
123 return ret;
124}
125
Sujitheac8e382010-04-16 11:54:00 +0530126static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
127 struct sk_buff_head *list)
Ming Leif8e1d082010-04-13 00:29:27 +0800128{
129 struct sk_buff *skb;
Sujitheac8e382010-04-16 11:54:00 +0530130
131 while ((skb = __skb_dequeue(list)) != NULL) {
Ming Leif8e1d082010-04-13 00:29:27 +0800132 dev_kfree_skb_any(skb);
Sujitheac8e382010-04-16 11:54:00 +0530133 TX_STAT_INC(skb_dropped);
134 }
Ming Leif8e1d082010-04-13 00:29:27 +0800135}
136
Sujithc11d8f82010-04-23 10:28:09 +0530137static void hif_usb_tx_cb(struct urb *urb)
138{
139 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
Dan Carpenter690e7812010-05-14 16:50:56 +0200140 struct hif_device_usb *hif_dev;
Sujithc11d8f82010-04-23 10:28:09 +0530141 struct sk_buff *skb;
142
Dan Carpenter690e7812010-05-14 16:50:56 +0200143 if (!tx_buf || !tx_buf->hif_dev)
Sujithc11d8f82010-04-23 10:28:09 +0530144 return;
145
Dan Carpenter690e7812010-05-14 16:50:56 +0200146 hif_dev = tx_buf->hif_dev;
147
Sujithc11d8f82010-04-23 10:28:09 +0530148 switch (urb->status) {
149 case 0:
150 break;
151 case -ENOENT:
152 case -ECONNRESET:
153 case -ENODEV:
154 case -ESHUTDOWN:
155 /*
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530156 * The URB has been killed, free the SKBs.
Sujithc11d8f82010-04-23 10:28:09 +0530157 */
158 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530159
160 /*
161 * If the URBs are being flushed, no need to add this
162 * URB to the free list.
163 */
164 spin_lock(&hif_dev->tx.tx_lock);
165 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
166 spin_unlock(&hif_dev->tx.tx_lock);
167 return;
168 }
169 spin_unlock(&hif_dev->tx.tx_lock);
170
171 /*
172 * In the stop() case, this URB has to be added to
173 * the free list.
174 */
175 goto add_free;
Sujithc11d8f82010-04-23 10:28:09 +0530176 default:
177 break;
178 }
179
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530180 /*
181 * Check if TX has been stopped, this is needed because
182 * this CB could have been invoked just after the TX lock
183 * was released in hif_stop() and kill_urb() hasn't been
184 * called yet.
185 */
Sujithc11d8f82010-04-23 10:28:09 +0530186 spin_lock(&hif_dev->tx.tx_lock);
187 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
188 spin_unlock(&hif_dev->tx.tx_lock);
189 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
190 goto add_free;
191 }
192 spin_unlock(&hif_dev->tx.tx_lock);
193
194 /* Complete the queued SKBs. */
195 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
196 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
197 skb, 1);
198 TX_STAT_INC(skb_completed);
199 }
200
201add_free:
202 /* Re-initialize the SKB queue */
203 tx_buf->len = tx_buf->offset = 0;
204 __skb_queue_head_init(&tx_buf->skb_queue);
205
206 /* Add this TX buffer to the free list */
207 spin_lock(&hif_dev->tx.tx_lock);
208 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
209 hif_dev->tx.tx_buf_cnt++;
210 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
211 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
212 TX_STAT_INC(buf_completed);
213 spin_unlock(&hif_dev->tx.tx_lock);
214}
215
Sujithfb9987d2010-03-17 14:25:25 +0530216/* TX lock has to be taken */
217static int __hif_usb_tx(struct hif_device_usb *hif_dev)
218{
219 struct tx_buf *tx_buf = NULL;
220 struct sk_buff *nskb = NULL;
221 int ret = 0, i;
222 u16 *hdr, tx_skb_cnt = 0;
223 u8 *buf;
224
225 if (hif_dev->tx.tx_skb_cnt == 0)
226 return 0;
227
228 /* Check if a free TX buffer is available */
229 if (list_empty(&hif_dev->tx.tx_buf))
230 return 0;
231
232 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
Sujithc11d8f82010-04-23 10:28:09 +0530233 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
Sujithfb9987d2010-03-17 14:25:25 +0530234 hif_dev->tx.tx_buf_cnt--;
235
236 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
237
238 for (i = 0; i < tx_skb_cnt; i++) {
239 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
240
241 /* Should never be NULL */
242 BUG_ON(!nskb);
243
244 hif_dev->tx.tx_skb_cnt--;
245
246 buf = tx_buf->buf;
247 buf += tx_buf->offset;
248 hdr = (u16 *)buf;
249 *hdr++ = nskb->len;
250 *hdr++ = ATH_USB_TX_STREAM_MODE_TAG;
251 buf += 4;
252 memcpy(buf, nskb->data, nskb->len);
253 tx_buf->len = nskb->len + 4;
254
255 if (i < (tx_skb_cnt - 1))
256 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
257
258 if (i == (tx_skb_cnt - 1))
259 tx_buf->len += tx_buf->offset;
260
261 __skb_queue_tail(&tx_buf->skb_queue, nskb);
262 TX_STAT_INC(skb_queued);
263 }
264
265 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
266 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
267 tx_buf->buf, tx_buf->len,
268 hif_usb_tx_cb, tx_buf);
269
270 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
271 if (ret) {
272 tx_buf->len = tx_buf->offset = 0;
Sujitheac8e382010-04-16 11:54:00 +0530273 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
Sujithfb9987d2010-03-17 14:25:25 +0530274 __skb_queue_head_init(&tx_buf->skb_queue);
275 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
276 hif_dev->tx.tx_buf_cnt++;
277 }
278
279 if (!ret)
280 TX_STAT_INC(buf_queued);
281
282 return ret;
283}
284
285static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
286 struct ath9k_htc_tx_ctl *tx_ctl)
287{
288 unsigned long flags;
289
290 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
291
292 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
293 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
294 return -ENODEV;
295 }
296
297 /* Check if the max queue count has been reached */
298 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
299 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
300 return -ENOMEM;
301 }
302
303 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
304 hif_dev->tx.tx_skb_cnt++;
305
306 /* Send normal frames immediately */
307 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
308 __hif_usb_tx(hif_dev);
309
310 /* Check if AMPDUs have to be sent immediately */
311 if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
312 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
313 (hif_dev->tx.tx_skb_cnt < 2)) {
314 __hif_usb_tx(hif_dev);
315 }
316
317 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
318
319 return 0;
320}
321
322static void hif_usb_start(void *hif_handle, u8 pipe_id)
323{
324 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
325 unsigned long flags;
326
327 hif_dev->flags |= HIF_USB_START;
328
329 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
330 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
331 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
332}
333
334static void hif_usb_stop(void *hif_handle, u8 pipe_id)
335{
336 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530337 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530338 unsigned long flags;
339
340 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
Sujitheac8e382010-04-16 11:54:00 +0530341 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
Sujithfb9987d2010-03-17 14:25:25 +0530342 hif_dev->tx.tx_skb_cnt = 0;
343 hif_dev->tx.flags |= HIF_USB_TX_STOP;
344 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530345
346 /* The pending URBs have to be canceled. */
347 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
348 &hif_dev->tx.tx_pending, list) {
349 usb_kill_urb(tx_buf->urb);
350 }
Sujithfb9987d2010-03-17 14:25:25 +0530351}
352
353static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
354 struct ath9k_htc_tx_ctl *tx_ctl)
355{
356 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
357 int ret = 0;
358
359 switch (pipe_id) {
360 case USB_WLAN_TX_PIPE:
361 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
362 break;
363 case USB_REG_OUT_PIPE:
364 ret = hif_usb_send_regout(hif_dev, skb);
365 break;
366 default:
Sujith6335ed02010-03-29 16:07:15 +0530367 dev_err(&hif_dev->udev->dev,
368 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
Sujithfb9987d2010-03-17 14:25:25 +0530369 ret = -EINVAL;
370 break;
371 }
372
373 return ret;
374}
375
376static struct ath9k_htc_hif hif_usb = {
377 .transport = ATH9K_HIF_USB,
378 .name = "ath9k_hif_usb",
379
380 .control_ul_pipe = USB_REG_OUT_PIPE,
381 .control_dl_pipe = USB_REG_IN_PIPE,
382
383 .start = hif_usb_start,
384 .stop = hif_usb_stop,
385 .send = hif_usb_send,
386};
387
388static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
389 struct sk_buff *skb)
390{
Sujithc5032692010-04-06 15:28:15 +0530391 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
Joe Perches44b23b42010-11-30 12:19:11 -0800392 int index = 0, i = 0, len = skb->len;
393 int rx_remain_len, rx_pkt_len;
394 u16 pool_index = 0;
Sujithfb9987d2010-03-17 14:25:25 +0530395 u8 *ptr;
396
Sujith46baa1a2010-04-06 15:28:11 +0530397 spin_lock(&hif_dev->rx_lock);
398
Sujithfb9987d2010-03-17 14:25:25 +0530399 rx_remain_len = hif_dev->rx_remain_len;
400 rx_pkt_len = hif_dev->rx_transfer_len;
401
402 if (rx_remain_len != 0) {
403 struct sk_buff *remain_skb = hif_dev->remain_skb;
404
405 if (remain_skb) {
406 ptr = (u8 *) remain_skb->data;
407
408 index = rx_remain_len;
409 rx_remain_len -= hif_dev->rx_pad_len;
410 ptr += rx_pkt_len;
411
412 memcpy(ptr, skb->data, rx_remain_len);
413
414 rx_pkt_len += rx_remain_len;
415 hif_dev->rx_remain_len = 0;
416 skb_put(remain_skb, rx_pkt_len);
417
418 skb_pool[pool_index++] = remain_skb;
419
420 } else {
421 index = rx_remain_len;
422 }
423 }
424
Sujith46baa1a2010-04-06 15:28:11 +0530425 spin_unlock(&hif_dev->rx_lock);
426
Sujithfb9987d2010-03-17 14:25:25 +0530427 while (index < len) {
Joe Perches44b23b42010-11-30 12:19:11 -0800428 u16 pkt_len;
429 u16 pkt_tag;
430 u16 pad_len;
431 int chk_idx;
432
Sujithfb9987d2010-03-17 14:25:25 +0530433 ptr = (u8 *) skb->data;
434
435 pkt_len = ptr[index] + (ptr[index+1] << 8);
436 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
437
Joe Perches44b23b42010-11-30 12:19:11 -0800438 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
Sujithfb9987d2010-03-17 14:25:25 +0530439 RX_STAT_INC(skb_dropped);
Sujithfb9987d2010-03-17 14:25:25 +0530440 return;
441 }
Joe Perches44b23b42010-11-30 12:19:11 -0800442
443 pad_len = 4 - (pkt_len & 0x3);
444 if (pad_len == 4)
445 pad_len = 0;
446
447 chk_idx = index;
448 index = index + 4 + pkt_len + pad_len;
449
450 if (index > MAX_RX_BUF_SIZE) {
451 spin_lock(&hif_dev->rx_lock);
452 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
453 hif_dev->rx_transfer_len =
454 MAX_RX_BUF_SIZE - chk_idx - 4;
455 hif_dev->rx_pad_len = pad_len;
456
457 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
458 if (!nskb) {
459 dev_err(&hif_dev->udev->dev,
460 "ath9k_htc: RX memory allocation error\n");
461 spin_unlock(&hif_dev->rx_lock);
462 goto err;
463 }
464 skb_reserve(nskb, 32);
465 RX_STAT_INC(skb_allocated);
466
467 memcpy(nskb->data, &(skb->data[chk_idx+4]),
468 hif_dev->rx_transfer_len);
469
470 /* Record the buffer pointer */
471 hif_dev->remain_skb = nskb;
472 spin_unlock(&hif_dev->rx_lock);
473 } else {
474 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
475 if (!nskb) {
476 dev_err(&hif_dev->udev->dev,
477 "ath9k_htc: RX memory allocation error\n");
478 goto err;
479 }
480 skb_reserve(nskb, 32);
481 RX_STAT_INC(skb_allocated);
482
483 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
484 skb_put(nskb, pkt_len);
485 skb_pool[pool_index++] = nskb;
486 }
Sujithfb9987d2010-03-17 14:25:25 +0530487 }
488
489err:
Sujithfb9987d2010-03-17 14:25:25 +0530490 for (i = 0; i < pool_index; i++) {
491 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
492 skb_pool[i]->len, USB_WLAN_RX_PIPE);
493 RX_STAT_INC(skb_completed);
494 }
495}
496
497static void ath9k_hif_usb_rx_cb(struct urb *urb)
498{
499 struct sk_buff *skb = (struct sk_buff *) urb->context;
Joe Perchesb2767362010-11-30 13:42:08 -0800500 struct hif_device_usb *hif_dev =
Sujithfb9987d2010-03-17 14:25:25 +0530501 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
502 int ret;
503
Sujith6335ed02010-03-29 16:07:15 +0530504 if (!skb)
505 return;
506
Sujithfb9987d2010-03-17 14:25:25 +0530507 if (!hif_dev)
508 goto free;
509
510 switch (urb->status) {
511 case 0:
512 break;
513 case -ENOENT:
514 case -ECONNRESET:
515 case -ENODEV:
516 case -ESHUTDOWN:
517 goto free;
518 default:
519 goto resubmit;
520 }
521
522 if (likely(urb->actual_length != 0)) {
523 skb_put(skb, urb->actual_length);
Sujithfb9987d2010-03-17 14:25:25 +0530524 ath9k_hif_usb_rx_stream(hif_dev, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530525 }
526
527resubmit:
528 skb_reset_tail_pointer(skb);
529 skb_trim(skb, 0);
530
Sujith6335ed02010-03-29 16:07:15 +0530531 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530532 ret = usb_submit_urb(urb, GFP_ATOMIC);
Sujith6335ed02010-03-29 16:07:15 +0530533 if (ret) {
534 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530535 goto free;
Sujith6335ed02010-03-29 16:07:15 +0530536 }
Sujithfb9987d2010-03-17 14:25:25 +0530537
538 return;
539free:
Ming Leif28a7b32010-04-13 00:28:53 +0800540 kfree_skb(skb);
Sujithfb9987d2010-03-17 14:25:25 +0530541}
542
543static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
544{
545 struct sk_buff *skb = (struct sk_buff *) urb->context;
546 struct sk_buff *nskb;
Joe Perchesb2767362010-11-30 13:42:08 -0800547 struct hif_device_usb *hif_dev =
Sujithfb9987d2010-03-17 14:25:25 +0530548 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
549 int ret;
550
Sujith6335ed02010-03-29 16:07:15 +0530551 if (!skb)
552 return;
553
Sujithfb9987d2010-03-17 14:25:25 +0530554 if (!hif_dev)
555 goto free;
556
557 switch (urb->status) {
558 case 0:
559 break;
560 case -ENOENT:
561 case -ECONNRESET:
562 case -ENODEV:
563 case -ESHUTDOWN:
564 goto free;
565 default:
566 goto resubmit;
567 }
568
569 if (likely(urb->actual_length != 0)) {
570 skb_put(skb, urb->actual_length);
571
Sujith5ab0af32010-04-23 10:28:17 +0530572 /* Process the command first */
573 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
574 skb->len, USB_REG_IN_PIPE);
575
576
Ming Leie6c6d332010-04-13 00:29:05 +0800577 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
Sujith5ab0af32010-04-23 10:28:17 +0530578 if (!nskb) {
579 dev_err(&hif_dev->udev->dev,
580 "ath9k_htc: REG_IN memory allocation failure\n");
581 urb->context = NULL;
582 return;
583 }
Sujithfb9987d2010-03-17 14:25:25 +0530584
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530585 usb_fill_bulk_urb(urb, hif_dev->udev,
Rajkumar Manoharan0f529e92010-09-16 19:56:55 +0530586 usb_rcvbulkpipe(hif_dev->udev,
587 USB_REG_IN_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530588 nskb->data, MAX_REG_IN_BUF_SIZE,
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530589 ath9k_hif_usb_reg_in_cb, nskb);
Sujithfb9987d2010-03-17 14:25:25 +0530590
591 ret = usb_submit_urb(urb, GFP_ATOMIC);
592 if (ret) {
Ming Leie6c6d332010-04-13 00:29:05 +0800593 kfree_skb(nskb);
Sujith5ab0af32010-04-23 10:28:17 +0530594 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530595 }
596
Sujithfb9987d2010-03-17 14:25:25 +0530597 return;
598 }
599
600resubmit:
601 skb_reset_tail_pointer(skb);
602 skb_trim(skb, 0);
603
604 ret = usb_submit_urb(urb, GFP_ATOMIC);
605 if (ret)
606 goto free;
607
608 return;
609free:
Ming Leie6c6d332010-04-13 00:29:05 +0800610 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530611 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530612}
613
614static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
615{
Sujithfb9987d2010-03-17 14:25:25 +0530616 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530617 unsigned long flags;
Sujithfb9987d2010-03-17 14:25:25 +0530618
Sujithc11d8f82010-04-23 10:28:09 +0530619 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
620 &hif_dev->tx.tx_buf, list) {
621 usb_kill_urb(tx_buf->urb);
Sujithfb9987d2010-03-17 14:25:25 +0530622 list_del(&tx_buf->list);
623 usb_free_urb(tx_buf->urb);
624 kfree(tx_buf->buf);
625 kfree(tx_buf);
626 }
627
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530628 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
629 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
630 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
631
Sujithfb9987d2010-03-17 14:25:25 +0530632 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
633 &hif_dev->tx.tx_pending, list) {
634 usb_kill_urb(tx_buf->urb);
635 list_del(&tx_buf->list);
636 usb_free_urb(tx_buf->urb);
637 kfree(tx_buf->buf);
638 kfree(tx_buf);
639 }
Sujithfb9987d2010-03-17 14:25:25 +0530640}
641
642static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
643{
644 struct tx_buf *tx_buf;
645 int i;
646
647 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
648 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
649 spin_lock_init(&hif_dev->tx.tx_lock);
650 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
651
652 for (i = 0; i < MAX_TX_URB_NUM; i++) {
653 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
654 if (!tx_buf)
655 goto err;
656
657 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
658 if (!tx_buf->buf)
659 goto err;
660
661 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
662 if (!tx_buf->urb)
663 goto err;
664
665 tx_buf->hif_dev = hif_dev;
666 __skb_queue_head_init(&tx_buf->skb_queue);
667
668 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
669 }
670
671 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
672
673 return 0;
674err:
Dan Carpenter76066882010-05-14 16:52:37 +0200675 if (tx_buf) {
676 kfree(tx_buf->buf);
677 kfree(tx_buf);
678 }
Sujithfb9987d2010-03-17 14:25:25 +0530679 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
680 return -ENOMEM;
681}
682
Sujithfb9987d2010-03-17 14:25:25 +0530683static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
684{
Sujith6335ed02010-03-29 16:07:15 +0530685 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530686}
687
688static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
689{
Sujith6335ed02010-03-29 16:07:15 +0530690 struct urb *urb = NULL;
691 struct sk_buff *skb = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530692 int i, ret;
693
Sujith6335ed02010-03-29 16:07:15 +0530694 init_usb_anchor(&hif_dev->rx_submitted);
Sujith46baa1a2010-04-06 15:28:11 +0530695 spin_lock_init(&hif_dev->rx_lock);
Sujith6335ed02010-03-29 16:07:15 +0530696
Sujithfb9987d2010-03-17 14:25:25 +0530697 for (i = 0; i < MAX_RX_URB_NUM; i++) {
698
699 /* Allocate URB */
Sujith6335ed02010-03-29 16:07:15 +0530700 urb = usb_alloc_urb(0, GFP_KERNEL);
701 if (urb == NULL) {
Sujithfb9987d2010-03-17 14:25:25 +0530702 ret = -ENOMEM;
Sujith6335ed02010-03-29 16:07:15 +0530703 goto err_urb;
Sujithfb9987d2010-03-17 14:25:25 +0530704 }
705
706 /* Allocate buffer */
Ming Leif28a7b32010-04-13 00:28:53 +0800707 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
Sujith6335ed02010-03-29 16:07:15 +0530708 if (!skb) {
709 ret = -ENOMEM;
710 goto err_skb;
711 }
712
713 usb_fill_bulk_urb(urb, hif_dev->udev,
714 usb_rcvbulkpipe(hif_dev->udev,
715 USB_WLAN_RX_PIPE),
716 skb->data, MAX_RX_BUF_SIZE,
717 ath9k_hif_usb_rx_cb, skb);
718
719 /* Anchor URB */
720 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530721
722 /* Submit URB */
Sujith6335ed02010-03-29 16:07:15 +0530723 ret = usb_submit_urb(urb, GFP_KERNEL);
724 if (ret) {
725 usb_unanchor_urb(urb);
726 goto err_submit;
727 }
Sujith66b10e32010-04-06 15:28:13 +0530728
729 /*
730 * Drop reference count.
731 * This ensures that the URB is freed when killing them.
732 */
733 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530734 }
735
736 return 0;
737
Sujith6335ed02010-03-29 16:07:15 +0530738err_submit:
Ming Leif28a7b32010-04-13 00:28:53 +0800739 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530740err_skb:
741 usb_free_urb(urb);
742err_urb:
Sujithfb9987d2010-03-17 14:25:25 +0530743 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
744 return ret;
745}
746
747static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
748{
749 if (hif_dev->reg_in_urb) {
750 usb_kill_urb(hif_dev->reg_in_urb);
Sujith6335ed02010-03-29 16:07:15 +0530751 if (hif_dev->reg_in_urb->context)
Ming Leie6c6d332010-04-13 00:29:05 +0800752 kfree_skb((void *)hif_dev->reg_in_urb->context);
Sujithfb9987d2010-03-17 14:25:25 +0530753 usb_free_urb(hif_dev->reg_in_urb);
754 hif_dev->reg_in_urb = NULL;
755 }
756}
757
758static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
759{
760 struct sk_buff *skb;
761
762 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
763 if (hif_dev->reg_in_urb == NULL)
764 return -ENOMEM;
765
Ming Leie6c6d332010-04-13 00:29:05 +0800766 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
Sujithfb9987d2010-03-17 14:25:25 +0530767 if (!skb)
768 goto err;
769
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530770 usb_fill_bulk_urb(hif_dev->reg_in_urb, hif_dev->udev,
Rajkumar Manoharan0f529e92010-09-16 19:56:55 +0530771 usb_rcvbulkpipe(hif_dev->udev,
772 USB_REG_IN_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530773 skb->data, MAX_REG_IN_BUF_SIZE,
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530774 ath9k_hif_usb_reg_in_cb, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530775
776 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
Sujith6335ed02010-03-29 16:07:15 +0530777 goto err;
Sujithfb9987d2010-03-17 14:25:25 +0530778
779 return 0;
780
Sujithfb9987d2010-03-17 14:25:25 +0530781err:
782 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
783 return -ENOMEM;
784}
785
786static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
787{
Sujith6f0f2662010-04-06 15:28:17 +0530788 /* Register Write */
789 init_usb_anchor(&hif_dev->regout_submitted);
790
Sujithfb9987d2010-03-17 14:25:25 +0530791 /* TX */
792 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
793 goto err;
794
795 /* RX */
796 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530797 goto err_rx;
Sujithfb9987d2010-03-17 14:25:25 +0530798
Sujith6f0f2662010-04-06 15:28:17 +0530799 /* Register Read */
Sujithfb9987d2010-03-17 14:25:25 +0530800 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530801 goto err_reg;
Sujithfb9987d2010-03-17 14:25:25 +0530802
803 return 0;
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530804err_reg:
805 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
806err_rx:
807 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
Sujithfb9987d2010-03-17 14:25:25 +0530808err:
809 return -ENOMEM;
810}
811
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530812static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
813{
814 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
815 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
816 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
817 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
818}
819
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530820static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
821 u32 drv_info)
Sujithfb9987d2010-03-17 14:25:25 +0530822{
823 int transfer, err;
824 const void *data = hif_dev->firmware->data;
825 size_t len = hif_dev->firmware->size;
826 u32 addr = AR9271_FIRMWARE;
827 u8 *buf = kzalloc(4096, GFP_KERNEL);
Sujithb1762862010-06-02 15:53:34 +0530828 u32 firm_offset;
Sujithfb9987d2010-03-17 14:25:25 +0530829
830 if (!buf)
831 return -ENOMEM;
832
833 while (len) {
834 transfer = min_t(int, len, 4096);
835 memcpy(buf, data, transfer);
836
837 err = usb_control_msg(hif_dev->udev,
838 usb_sndctrlpipe(hif_dev->udev, 0),
839 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
840 addr >> 8, 0, buf, transfer, HZ);
841 if (err < 0) {
842 kfree(buf);
843 return err;
844 }
845
846 len -= transfer;
847 data += transfer;
848 addr += transfer;
849 }
850 kfree(buf);
851
Sujith Manoharan0b5ead92010-12-07 16:31:38 +0530852 if (IS_AR7010_DEVICE(drv_info))
Sujithb1762862010-06-02 15:53:34 +0530853 firm_offset = AR7010_FIRMWARE_TEXT;
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530854 else
Sujithb1762862010-06-02 15:53:34 +0530855 firm_offset = AR9271_FIRMWARE_TEXT;
856
Sujithfb9987d2010-03-17 14:25:25 +0530857 /*
858 * Issue FW download complete command to firmware.
859 */
860 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
861 FIRMWARE_DOWNLOAD_COMP,
862 0x40 | USB_DIR_OUT,
Sujithb1762862010-06-02 15:53:34 +0530863 firm_offset >> 8, 0, NULL, 0, HZ);
Sujithfb9987d2010-03-17 14:25:25 +0530864 if (err)
865 return -EIO;
866
867 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
Sujithce43cee2010-06-02 15:53:30 +0530868 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
Sujithfb9987d2010-03-17 14:25:25 +0530869
870 return 0;
871}
872
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530873static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
Sujithfb9987d2010-03-17 14:25:25 +0530874{
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530875 int ret, idx;
876 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
877 struct usb_endpoint_descriptor *endp;
Sujithfb9987d2010-03-17 14:25:25 +0530878
879 /* Request firmware */
Sujithce43cee2010-06-02 15:53:30 +0530880 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
881 &hif_dev->udev->dev);
Sujithfb9987d2010-03-17 14:25:25 +0530882 if (ret) {
883 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +0530884 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
Sujithfb9987d2010-03-17 14:25:25 +0530885 goto err_fw_req;
886 }
887
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530888 /* Download firmware */
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530889 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530890 if (ret) {
891 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +0530892 "ath9k_htc: Firmware - %s download failed\n",
893 hif_dev->fw_name);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530894 goto err_fw_download;
895 }
896
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530897 /* On downloading the firmware to the target, the USB descriptor of EP4
898 * is 'patched' to change the type of the endpoint to Bulk. This will
899 * bring down CPU usage during the scan period.
900 */
901 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
902 endp = &alt->endpoint[idx].desc;
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530903 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
904 == USB_ENDPOINT_XFER_INT) {
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530905 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
906 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
907 endp->bInterval = 0;
908 }
909 }
910
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530911 /* Alloc URBs */
912 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
913 if (ret) {
914 dev_err(&hif_dev->udev->dev,
915 "ath9k_htc: Unable to allocate URBs\n");
916 goto err_urb;
917 }
918
Sujithfb9987d2010-03-17 14:25:25 +0530919 return 0;
920
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530921err_urb:
Sujith Manoharancaa0a992010-12-07 16:32:02 +0530922 ath9k_hif_usb_dealloc_urbs(hif_dev);
923err_fw_download:
Sujithfb9987d2010-03-17 14:25:25 +0530924 release_firmware(hif_dev->firmware);
925err_fw_req:
926 hif_dev->firmware = NULL;
927 return ret;
928}
929
Sujithfb9987d2010-03-17 14:25:25 +0530930static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
931{
932 ath9k_hif_usb_dealloc_urbs(hif_dev);
933 if (hif_dev->firmware)
934 release_firmware(hif_dev->firmware);
935}
936
937static int ath9k_hif_usb_probe(struct usb_interface *interface,
938 const struct usb_device_id *id)
939{
940 struct usb_device *udev = interface_to_usbdev(interface);
941 struct hif_device_usb *hif_dev;
Sujithfb9987d2010-03-17 14:25:25 +0530942 int ret = 0;
943
944 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
945 if (!hif_dev) {
946 ret = -ENOMEM;
947 goto err_alloc;
948 }
949
950 usb_get_dev(udev);
951 hif_dev->udev = udev;
952 hif_dev->interface = interface;
953 hif_dev->device_id = id->idProduct;
954#ifdef CONFIG_PM
955 udev->reset_resume = 1;
956#endif
957 usb_set_intfdata(interface, hif_dev);
958
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +0530959 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
960 &hif_dev->udev->dev);
961 if (hif_dev->htc_handle == NULL) {
962 ret = -ENOMEM;
963 goto err_htc_hw_alloc;
964 }
965
Sujithce43cee2010-06-02 15:53:30 +0530966 /* Find out which firmware to load */
967
Sujith Manoharan0b5ead92010-12-07 16:31:38 +0530968 if (IS_AR7010_DEVICE(id->driver_info))
Sujithb1762862010-06-02 15:53:34 +0530969 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
John W. Linvilled0ee0eb2010-06-23 14:20:45 -0400970 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
Sujithb1762862010-06-02 15:53:34 +0530971 else
John W. Linvilled0ee0eb2010-06-23 14:20:45 -0400972 hif_dev->fw_name = FIRMWARE_AR7010;
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530973 else
John W. Linvilled0ee0eb2010-06-23 14:20:45 -0400974 hif_dev->fw_name = FIRMWARE_AR9271;
Sujithce43cee2010-06-02 15:53:30 +0530975
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530976 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
Sujithfb9987d2010-03-17 14:25:25 +0530977 if (ret) {
978 ret = -EINVAL;
979 goto err_hif_init_usb;
980 }
981
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +0530982 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
Vivek Natarajan21cb9872010-08-18 19:57:49 +0530983 &hif_dev->udev->dev, hif_dev->device_id,
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530984 hif_dev->udev->product, id->driver_info);
Sujithfb9987d2010-03-17 14:25:25 +0530985 if (ret) {
986 ret = -EINVAL;
987 goto err_htc_hw_init;
988 }
989
990 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
991
992 return 0;
993
994err_htc_hw_init:
Sujithfb9987d2010-03-17 14:25:25 +0530995 ath9k_hif_usb_dev_deinit(hif_dev);
996err_hif_init_usb:
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +0530997 ath9k_htc_hw_free(hif_dev->htc_handle);
998err_htc_hw_alloc:
Sujithfb9987d2010-03-17 14:25:25 +0530999 usb_set_intfdata(interface, NULL);
1000 kfree(hif_dev);
1001 usb_put_dev(udev);
1002err_alloc:
1003 return ret;
1004}
1005
Sujith62e47162010-04-23 10:28:16 +05301006static void ath9k_hif_usb_reboot(struct usb_device *udev)
1007{
1008 u32 reboot_cmd = 0xffffffff;
1009 void *buf;
1010 int ret;
1011
Julia Lawalla465a2c2010-05-15 23:17:19 +02001012 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
Sujith62e47162010-04-23 10:28:16 +05301013 if (!buf)
1014 return;
1015
Sujith62e47162010-04-23 10:28:16 +05301016 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1017 buf, 4, NULL, HZ);
1018 if (ret)
1019 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1020
1021 kfree(buf);
1022}
1023
Sujithfb9987d2010-03-17 14:25:25 +05301024static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1025{
1026 struct usb_device *udev = interface_to_usbdev(interface);
Joe Perchesb2767362010-11-30 13:42:08 -08001027 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Sujith Manoharan97dcec52010-12-20 08:02:42 +05301028 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
Sujithfb9987d2010-03-17 14:25:25 +05301029
1030 if (hif_dev) {
Sujith Manoharan97dcec52010-12-20 08:02:42 +05301031 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
Sujithfb9987d2010-03-17 14:25:25 +05301032 ath9k_htc_hw_free(hif_dev->htc_handle);
1033 ath9k_hif_usb_dev_deinit(hif_dev);
1034 usb_set_intfdata(interface, NULL);
1035 }
1036
Sujith Manoharan97dcec52010-12-20 08:02:42 +05301037 if (!unplugged && (hif_dev->flags & HIF_USB_START))
Sujith62e47162010-04-23 10:28:16 +05301038 ath9k_hif_usb_reboot(udev);
Sujithfb9987d2010-03-17 14:25:25 +05301039
1040 kfree(hif_dev);
1041 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1042 usb_put_dev(udev);
1043}
1044
1045#ifdef CONFIG_PM
1046static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1047 pm_message_t message)
1048{
Joe Perchesb2767362010-11-30 13:42:08 -08001049 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Sujithfb9987d2010-03-17 14:25:25 +05301050
Sujith Manoharanf933ebe2010-12-01 12:30:27 +05301051 /*
1052 * The device has to be set to FULLSLEEP mode in case no
1053 * interface is up.
1054 */
1055 if (!(hif_dev->flags & HIF_USB_START))
1056 ath9k_htc_suspend(hif_dev->htc_handle);
1057
Sujithfb9987d2010-03-17 14:25:25 +05301058 ath9k_hif_usb_dealloc_urbs(hif_dev);
1059
1060 return 0;
1061}
1062
1063static int ath9k_hif_usb_resume(struct usb_interface *interface)
1064{
Joe Perchesb2767362010-11-30 13:42:08 -08001065 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301066 struct htc_target *htc_handle = hif_dev->htc_handle;
Sujithfb9987d2010-03-17 14:25:25 +05301067 int ret;
1068
1069 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1070 if (ret)
1071 return ret;
1072
1073 if (hif_dev->firmware) {
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301074 ret = ath9k_hif_usb_download_fw(hif_dev,
Sujith Manoharan0b5ead92010-12-07 16:31:38 +05301075 htc_handle->drv_priv->ah->hw_version.usbdev);
Sujithfb9987d2010-03-17 14:25:25 +05301076 if (ret)
1077 goto fail_resume;
1078 } else {
1079 ath9k_hif_usb_dealloc_urbs(hif_dev);
1080 return -EIO;
1081 }
1082
1083 mdelay(100);
1084
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301085 ret = ath9k_htc_resume(htc_handle);
Sujithfb9987d2010-03-17 14:25:25 +05301086
1087 if (ret)
1088 goto fail_resume;
1089
1090 return 0;
1091
1092fail_resume:
1093 ath9k_hif_usb_dealloc_urbs(hif_dev);
1094
1095 return ret;
1096}
1097#endif
1098
1099static struct usb_driver ath9k_hif_usb_driver = {
1100 .name = "ath9k_hif_usb",
1101 .probe = ath9k_hif_usb_probe,
1102 .disconnect = ath9k_hif_usb_disconnect,
1103#ifdef CONFIG_PM
1104 .suspend = ath9k_hif_usb_suspend,
1105 .resume = ath9k_hif_usb_resume,
1106 .reset_resume = ath9k_hif_usb_resume,
1107#endif
1108 .id_table = ath9k_hif_usb_ids,
1109 .soft_unbind = 1,
1110};
1111
1112int ath9k_hif_usb_init(void)
1113{
1114 return usb_register(&ath9k_hif_usb_driver);
1115}
1116
1117void ath9k_hif_usb_exit(void)
1118{
1119 usb_deregister(&ath9k_hif_usb_driver);
1120}