blob: 45d4b2403a52653a7a531a39171d2be433279c0e [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 Manoharan0b5ead92010-12-07 16:31:38 +053041
42 { USB_DEVICE(0x0cf3, 0x7015),
43 .driver_info = AR9287_USB }, /* Atheros */
Rajkumar Manoharan64f12172010-11-19 16:53:20 +053044 { USB_DEVICE(0x1668, 0x1200),
Sujith Manoharan0b5ead92010-12-07 16:31:38 +053045 .driver_info = AR9287_USB }, /* Verizon */
46
47 { USB_DEVICE(0x0cf3, 0x7010),
48 .driver_info = AR9280_USB }, /* Atheros */
49 { USB_DEVICE(0x0846, 0x9018),
50 .driver_info = AR9280_USB }, /* Netgear WNDA3200 */
51 { USB_DEVICE(0x083A, 0xA704),
52 .driver_info = AR9280_USB }, /* SMC Networks */
53
Sujithfb9987d2010-03-17 14:25:25 +053054 { },
55};
56
57MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
58
59static int __hif_usb_tx(struct hif_device_usb *hif_dev);
60
61static void hif_usb_regout_cb(struct urb *urb)
62{
63 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
Sujithfb9987d2010-03-17 14:25:25 +053064
65 switch (urb->status) {
66 case 0:
67 break;
68 case -ENOENT:
69 case -ECONNRESET:
Sujithfb9987d2010-03-17 14:25:25 +053070 case -ENODEV:
71 case -ESHUTDOWN:
Sujith6f0f2662010-04-06 15:28:17 +053072 goto free;
Sujithfb9987d2010-03-17 14:25:25 +053073 default:
74 break;
75 }
76
77 if (cmd) {
78 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
79 cmd->skb, 1);
80 kfree(cmd);
Sujithfb9987d2010-03-17 14:25:25 +053081 }
Sujith6f0f2662010-04-06 15:28:17 +053082
83 return;
84free:
Ming Lei0fa35a52010-04-13 00:29:15 +080085 kfree_skb(cmd->skb);
Sujith6f0f2662010-04-06 15:28:17 +053086 kfree(cmd);
Sujithfb9987d2010-03-17 14:25:25 +053087}
88
89static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
90 struct sk_buff *skb)
91{
92 struct urb *urb;
93 struct cmd_buf *cmd;
94 int ret = 0;
95
96 urb = usb_alloc_urb(0, GFP_KERNEL);
97 if (urb == NULL)
98 return -ENOMEM;
99
100 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
101 if (cmd == NULL) {
102 usb_free_urb(urb);
103 return -ENOMEM;
104 }
105
106 cmd->skb = skb;
107 cmd->hif_dev = hif_dev;
108
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530109 usb_fill_bulk_urb(urb, hif_dev->udev,
110 usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530111 skb->data, skb->len,
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530112 hif_usb_regout_cb, cmd);
Sujithfb9987d2010-03-17 14:25:25 +0530113
Sujith6f0f2662010-04-06 15:28:17 +0530114 usb_anchor_urb(urb, &hif_dev->regout_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530115 ret = usb_submit_urb(urb, GFP_KERNEL);
116 if (ret) {
Sujith6f0f2662010-04-06 15:28:17 +0530117 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530118 kfree(cmd);
119 }
Sujith6f0f2662010-04-06 15:28:17 +0530120 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530121
122 return ret;
123}
124
Sujitheac8e382010-04-16 11:54:00 +0530125static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
126 struct sk_buff_head *list)
Ming Leif8e1d082010-04-13 00:29:27 +0800127{
128 struct sk_buff *skb;
Sujitheac8e382010-04-16 11:54:00 +0530129
130 while ((skb = __skb_dequeue(list)) != NULL) {
Ming Leif8e1d082010-04-13 00:29:27 +0800131 dev_kfree_skb_any(skb);
Sujitheac8e382010-04-16 11:54:00 +0530132 TX_STAT_INC(skb_dropped);
133 }
Ming Leif8e1d082010-04-13 00:29:27 +0800134}
135
Sujithc11d8f82010-04-23 10:28:09 +0530136static void hif_usb_tx_cb(struct urb *urb)
137{
138 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
Dan Carpenter690e7812010-05-14 16:50:56 +0200139 struct hif_device_usb *hif_dev;
Sujithc11d8f82010-04-23 10:28:09 +0530140 struct sk_buff *skb;
141
Dan Carpenter690e7812010-05-14 16:50:56 +0200142 if (!tx_buf || !tx_buf->hif_dev)
Sujithc11d8f82010-04-23 10:28:09 +0530143 return;
144
Dan Carpenter690e7812010-05-14 16:50:56 +0200145 hif_dev = tx_buf->hif_dev;
146
Sujithc11d8f82010-04-23 10:28:09 +0530147 switch (urb->status) {
148 case 0:
149 break;
150 case -ENOENT:
151 case -ECONNRESET:
152 case -ENODEV:
153 case -ESHUTDOWN:
154 /*
155 * The URB has been killed, free the SKBs
156 * and return.
157 */
158 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
159 return;
160 default:
161 break;
162 }
163
164 /* Check if TX has been stopped */
165 spin_lock(&hif_dev->tx.tx_lock);
166 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
167 spin_unlock(&hif_dev->tx.tx_lock);
168 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
169 goto add_free;
170 }
171 spin_unlock(&hif_dev->tx.tx_lock);
172
173 /* Complete the queued SKBs. */
174 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
175 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
176 skb, 1);
177 TX_STAT_INC(skb_completed);
178 }
179
180add_free:
181 /* Re-initialize the SKB queue */
182 tx_buf->len = tx_buf->offset = 0;
183 __skb_queue_head_init(&tx_buf->skb_queue);
184
185 /* Add this TX buffer to the free list */
186 spin_lock(&hif_dev->tx.tx_lock);
187 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
188 hif_dev->tx.tx_buf_cnt++;
189 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
190 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
191 TX_STAT_INC(buf_completed);
192 spin_unlock(&hif_dev->tx.tx_lock);
193}
194
Sujithfb9987d2010-03-17 14:25:25 +0530195/* TX lock has to be taken */
196static int __hif_usb_tx(struct hif_device_usb *hif_dev)
197{
198 struct tx_buf *tx_buf = NULL;
199 struct sk_buff *nskb = NULL;
200 int ret = 0, i;
201 u16 *hdr, tx_skb_cnt = 0;
202 u8 *buf;
203
204 if (hif_dev->tx.tx_skb_cnt == 0)
205 return 0;
206
207 /* Check if a free TX buffer is available */
208 if (list_empty(&hif_dev->tx.tx_buf))
209 return 0;
210
211 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
Sujithc11d8f82010-04-23 10:28:09 +0530212 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
Sujithfb9987d2010-03-17 14:25:25 +0530213 hif_dev->tx.tx_buf_cnt--;
214
215 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
216
217 for (i = 0; i < tx_skb_cnt; i++) {
218 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
219
220 /* Should never be NULL */
221 BUG_ON(!nskb);
222
223 hif_dev->tx.tx_skb_cnt--;
224
225 buf = tx_buf->buf;
226 buf += tx_buf->offset;
227 hdr = (u16 *)buf;
228 *hdr++ = nskb->len;
229 *hdr++ = ATH_USB_TX_STREAM_MODE_TAG;
230 buf += 4;
231 memcpy(buf, nskb->data, nskb->len);
232 tx_buf->len = nskb->len + 4;
233
234 if (i < (tx_skb_cnt - 1))
235 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
236
237 if (i == (tx_skb_cnt - 1))
238 tx_buf->len += tx_buf->offset;
239
240 __skb_queue_tail(&tx_buf->skb_queue, nskb);
241 TX_STAT_INC(skb_queued);
242 }
243
244 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
245 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
246 tx_buf->buf, tx_buf->len,
247 hif_usb_tx_cb, tx_buf);
248
249 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
250 if (ret) {
251 tx_buf->len = tx_buf->offset = 0;
Sujitheac8e382010-04-16 11:54:00 +0530252 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
Sujithfb9987d2010-03-17 14:25:25 +0530253 __skb_queue_head_init(&tx_buf->skb_queue);
254 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
255 hif_dev->tx.tx_buf_cnt++;
256 }
257
258 if (!ret)
259 TX_STAT_INC(buf_queued);
260
261 return ret;
262}
263
264static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
265 struct ath9k_htc_tx_ctl *tx_ctl)
266{
267 unsigned long flags;
268
269 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
270
271 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
272 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
273 return -ENODEV;
274 }
275
276 /* Check if the max queue count has been reached */
277 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
278 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
279 return -ENOMEM;
280 }
281
282 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
283 hif_dev->tx.tx_skb_cnt++;
284
285 /* Send normal frames immediately */
286 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
287 __hif_usb_tx(hif_dev);
288
289 /* Check if AMPDUs have to be sent immediately */
290 if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
291 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
292 (hif_dev->tx.tx_skb_cnt < 2)) {
293 __hif_usb_tx(hif_dev);
294 }
295
296 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
297
298 return 0;
299}
300
301static void hif_usb_start(void *hif_handle, u8 pipe_id)
302{
303 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
304 unsigned long flags;
305
306 hif_dev->flags |= HIF_USB_START;
307
308 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
309 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
310 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
311}
312
313static void hif_usb_stop(void *hif_handle, u8 pipe_id)
314{
315 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
316 unsigned long flags;
317
318 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
Sujitheac8e382010-04-16 11:54:00 +0530319 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
Sujithfb9987d2010-03-17 14:25:25 +0530320 hif_dev->tx.tx_skb_cnt = 0;
321 hif_dev->tx.flags |= HIF_USB_TX_STOP;
322 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
323}
324
325static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
326 struct ath9k_htc_tx_ctl *tx_ctl)
327{
328 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
329 int ret = 0;
330
331 switch (pipe_id) {
332 case USB_WLAN_TX_PIPE:
333 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
334 break;
335 case USB_REG_OUT_PIPE:
336 ret = hif_usb_send_regout(hif_dev, skb);
337 break;
338 default:
Sujith6335ed02010-03-29 16:07:15 +0530339 dev_err(&hif_dev->udev->dev,
340 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
Sujithfb9987d2010-03-17 14:25:25 +0530341 ret = -EINVAL;
342 break;
343 }
344
345 return ret;
346}
347
348static struct ath9k_htc_hif hif_usb = {
349 .transport = ATH9K_HIF_USB,
350 .name = "ath9k_hif_usb",
351
352 .control_ul_pipe = USB_REG_OUT_PIPE,
353 .control_dl_pipe = USB_REG_IN_PIPE,
354
355 .start = hif_usb_start,
356 .stop = hif_usb_stop,
357 .send = hif_usb_send,
358};
359
360static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
361 struct sk_buff *skb)
362{
Sujithc5032692010-04-06 15:28:15 +0530363 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
Joe Perches44b23b42010-11-30 12:19:11 -0800364 int index = 0, i = 0, len = skb->len;
365 int rx_remain_len, rx_pkt_len;
366 u16 pool_index = 0;
Sujithfb9987d2010-03-17 14:25:25 +0530367 u8 *ptr;
368
Sujith46baa1a2010-04-06 15:28:11 +0530369 spin_lock(&hif_dev->rx_lock);
370
Sujithfb9987d2010-03-17 14:25:25 +0530371 rx_remain_len = hif_dev->rx_remain_len;
372 rx_pkt_len = hif_dev->rx_transfer_len;
373
374 if (rx_remain_len != 0) {
375 struct sk_buff *remain_skb = hif_dev->remain_skb;
376
377 if (remain_skb) {
378 ptr = (u8 *) remain_skb->data;
379
380 index = rx_remain_len;
381 rx_remain_len -= hif_dev->rx_pad_len;
382 ptr += rx_pkt_len;
383
384 memcpy(ptr, skb->data, rx_remain_len);
385
386 rx_pkt_len += rx_remain_len;
387 hif_dev->rx_remain_len = 0;
388 skb_put(remain_skb, rx_pkt_len);
389
390 skb_pool[pool_index++] = remain_skb;
391
392 } else {
393 index = rx_remain_len;
394 }
395 }
396
Sujith46baa1a2010-04-06 15:28:11 +0530397 spin_unlock(&hif_dev->rx_lock);
398
Sujithfb9987d2010-03-17 14:25:25 +0530399 while (index < len) {
Joe Perches44b23b42010-11-30 12:19:11 -0800400 u16 pkt_len;
401 u16 pkt_tag;
402 u16 pad_len;
403 int chk_idx;
404
Sujithfb9987d2010-03-17 14:25:25 +0530405 ptr = (u8 *) skb->data;
406
407 pkt_len = ptr[index] + (ptr[index+1] << 8);
408 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
409
Joe Perches44b23b42010-11-30 12:19:11 -0800410 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
Sujithfb9987d2010-03-17 14:25:25 +0530411 RX_STAT_INC(skb_dropped);
Sujithfb9987d2010-03-17 14:25:25 +0530412 return;
413 }
Joe Perches44b23b42010-11-30 12:19:11 -0800414
415 pad_len = 4 - (pkt_len & 0x3);
416 if (pad_len == 4)
417 pad_len = 0;
418
419 chk_idx = index;
420 index = index + 4 + pkt_len + pad_len;
421
422 if (index > MAX_RX_BUF_SIZE) {
423 spin_lock(&hif_dev->rx_lock);
424 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
425 hif_dev->rx_transfer_len =
426 MAX_RX_BUF_SIZE - chk_idx - 4;
427 hif_dev->rx_pad_len = pad_len;
428
429 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
430 if (!nskb) {
431 dev_err(&hif_dev->udev->dev,
432 "ath9k_htc: RX memory allocation error\n");
433 spin_unlock(&hif_dev->rx_lock);
434 goto err;
435 }
436 skb_reserve(nskb, 32);
437 RX_STAT_INC(skb_allocated);
438
439 memcpy(nskb->data, &(skb->data[chk_idx+4]),
440 hif_dev->rx_transfer_len);
441
442 /* Record the buffer pointer */
443 hif_dev->remain_skb = nskb;
444 spin_unlock(&hif_dev->rx_lock);
445 } else {
446 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
447 if (!nskb) {
448 dev_err(&hif_dev->udev->dev,
449 "ath9k_htc: RX memory allocation error\n");
450 goto err;
451 }
452 skb_reserve(nskb, 32);
453 RX_STAT_INC(skb_allocated);
454
455 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
456 skb_put(nskb, pkt_len);
457 skb_pool[pool_index++] = nskb;
458 }
Sujithfb9987d2010-03-17 14:25:25 +0530459 }
460
461err:
Sujithfb9987d2010-03-17 14:25:25 +0530462 for (i = 0; i < pool_index; i++) {
463 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
464 skb_pool[i]->len, USB_WLAN_RX_PIPE);
465 RX_STAT_INC(skb_completed);
466 }
467}
468
469static void ath9k_hif_usb_rx_cb(struct urb *urb)
470{
471 struct sk_buff *skb = (struct sk_buff *) urb->context;
Joe Perchesb2767362010-11-30 13:42:08 -0800472 struct hif_device_usb *hif_dev =
Sujithfb9987d2010-03-17 14:25:25 +0530473 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
474 int ret;
475
Sujith6335ed02010-03-29 16:07:15 +0530476 if (!skb)
477 return;
478
Sujithfb9987d2010-03-17 14:25:25 +0530479 if (!hif_dev)
480 goto free;
481
482 switch (urb->status) {
483 case 0:
484 break;
485 case -ENOENT:
486 case -ECONNRESET:
487 case -ENODEV:
488 case -ESHUTDOWN:
489 goto free;
490 default:
491 goto resubmit;
492 }
493
494 if (likely(urb->actual_length != 0)) {
495 skb_put(skb, urb->actual_length);
Sujithfb9987d2010-03-17 14:25:25 +0530496 ath9k_hif_usb_rx_stream(hif_dev, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530497 }
498
499resubmit:
500 skb_reset_tail_pointer(skb);
501 skb_trim(skb, 0);
502
Sujith6335ed02010-03-29 16:07:15 +0530503 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530504 ret = usb_submit_urb(urb, GFP_ATOMIC);
Sujith6335ed02010-03-29 16:07:15 +0530505 if (ret) {
506 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530507 goto free;
Sujith6335ed02010-03-29 16:07:15 +0530508 }
Sujithfb9987d2010-03-17 14:25:25 +0530509
510 return;
511free:
Ming Leif28a7b32010-04-13 00:28:53 +0800512 kfree_skb(skb);
Sujithfb9987d2010-03-17 14:25:25 +0530513}
514
515static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
516{
517 struct sk_buff *skb = (struct sk_buff *) urb->context;
518 struct sk_buff *nskb;
Joe Perchesb2767362010-11-30 13:42:08 -0800519 struct hif_device_usb *hif_dev =
Sujithfb9987d2010-03-17 14:25:25 +0530520 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
521 int ret;
522
Sujith6335ed02010-03-29 16:07:15 +0530523 if (!skb)
524 return;
525
Sujithfb9987d2010-03-17 14:25:25 +0530526 if (!hif_dev)
527 goto free;
528
529 switch (urb->status) {
530 case 0:
531 break;
532 case -ENOENT:
533 case -ECONNRESET:
534 case -ENODEV:
535 case -ESHUTDOWN:
536 goto free;
537 default:
538 goto resubmit;
539 }
540
541 if (likely(urb->actual_length != 0)) {
542 skb_put(skb, urb->actual_length);
543
Sujith5ab0af32010-04-23 10:28:17 +0530544 /* Process the command first */
545 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
546 skb->len, USB_REG_IN_PIPE);
547
548
Ming Leie6c6d332010-04-13 00:29:05 +0800549 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
Sujith5ab0af32010-04-23 10:28:17 +0530550 if (!nskb) {
551 dev_err(&hif_dev->udev->dev,
552 "ath9k_htc: REG_IN memory allocation failure\n");
553 urb->context = NULL;
554 return;
555 }
Sujithfb9987d2010-03-17 14:25:25 +0530556
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530557 usb_fill_bulk_urb(urb, hif_dev->udev,
Rajkumar Manoharan0f529e92010-09-16 19:56:55 +0530558 usb_rcvbulkpipe(hif_dev->udev,
559 USB_REG_IN_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530560 nskb->data, MAX_REG_IN_BUF_SIZE,
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530561 ath9k_hif_usb_reg_in_cb, nskb);
Sujithfb9987d2010-03-17 14:25:25 +0530562
563 ret = usb_submit_urb(urb, GFP_ATOMIC);
564 if (ret) {
Ming Leie6c6d332010-04-13 00:29:05 +0800565 kfree_skb(nskb);
Sujith5ab0af32010-04-23 10:28:17 +0530566 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530567 }
568
Sujithfb9987d2010-03-17 14:25:25 +0530569 return;
570 }
571
572resubmit:
573 skb_reset_tail_pointer(skb);
574 skb_trim(skb, 0);
575
576 ret = usb_submit_urb(urb, GFP_ATOMIC);
577 if (ret)
578 goto free;
579
580 return;
581free:
Ming Leie6c6d332010-04-13 00:29:05 +0800582 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530583 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530584}
585
586static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
587{
Sujithfb9987d2010-03-17 14:25:25 +0530588 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
589
Sujithc11d8f82010-04-23 10:28:09 +0530590 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
591 &hif_dev->tx.tx_buf, list) {
592 usb_kill_urb(tx_buf->urb);
Sujithfb9987d2010-03-17 14:25:25 +0530593 list_del(&tx_buf->list);
594 usb_free_urb(tx_buf->urb);
595 kfree(tx_buf->buf);
596 kfree(tx_buf);
597 }
598
Sujithfb9987d2010-03-17 14:25:25 +0530599 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
600 &hif_dev->tx.tx_pending, list) {
601 usb_kill_urb(tx_buf->urb);
602 list_del(&tx_buf->list);
603 usb_free_urb(tx_buf->urb);
604 kfree(tx_buf->buf);
605 kfree(tx_buf);
606 }
Sujithfb9987d2010-03-17 14:25:25 +0530607}
608
609static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
610{
611 struct tx_buf *tx_buf;
612 int i;
613
614 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
615 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
616 spin_lock_init(&hif_dev->tx.tx_lock);
617 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
618
619 for (i = 0; i < MAX_TX_URB_NUM; i++) {
620 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
621 if (!tx_buf)
622 goto err;
623
624 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
625 if (!tx_buf->buf)
626 goto err;
627
628 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
629 if (!tx_buf->urb)
630 goto err;
631
632 tx_buf->hif_dev = hif_dev;
633 __skb_queue_head_init(&tx_buf->skb_queue);
634
635 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
636 }
637
638 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
639
640 return 0;
641err:
Dan Carpenter76066882010-05-14 16:52:37 +0200642 if (tx_buf) {
643 kfree(tx_buf->buf);
644 kfree(tx_buf);
645 }
Sujithfb9987d2010-03-17 14:25:25 +0530646 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
647 return -ENOMEM;
648}
649
Sujithfb9987d2010-03-17 14:25:25 +0530650static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
651{
Sujith6335ed02010-03-29 16:07:15 +0530652 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530653}
654
655static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
656{
Sujith6335ed02010-03-29 16:07:15 +0530657 struct urb *urb = NULL;
658 struct sk_buff *skb = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530659 int i, ret;
660
Sujith6335ed02010-03-29 16:07:15 +0530661 init_usb_anchor(&hif_dev->rx_submitted);
Sujith46baa1a2010-04-06 15:28:11 +0530662 spin_lock_init(&hif_dev->rx_lock);
Sujith6335ed02010-03-29 16:07:15 +0530663
Sujithfb9987d2010-03-17 14:25:25 +0530664 for (i = 0; i < MAX_RX_URB_NUM; i++) {
665
666 /* Allocate URB */
Sujith6335ed02010-03-29 16:07:15 +0530667 urb = usb_alloc_urb(0, GFP_KERNEL);
668 if (urb == NULL) {
Sujithfb9987d2010-03-17 14:25:25 +0530669 ret = -ENOMEM;
Sujith6335ed02010-03-29 16:07:15 +0530670 goto err_urb;
Sujithfb9987d2010-03-17 14:25:25 +0530671 }
672
673 /* Allocate buffer */
Ming Leif28a7b32010-04-13 00:28:53 +0800674 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
Sujith6335ed02010-03-29 16:07:15 +0530675 if (!skb) {
676 ret = -ENOMEM;
677 goto err_skb;
678 }
679
680 usb_fill_bulk_urb(urb, hif_dev->udev,
681 usb_rcvbulkpipe(hif_dev->udev,
682 USB_WLAN_RX_PIPE),
683 skb->data, MAX_RX_BUF_SIZE,
684 ath9k_hif_usb_rx_cb, skb);
685
686 /* Anchor URB */
687 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530688
689 /* Submit URB */
Sujith6335ed02010-03-29 16:07:15 +0530690 ret = usb_submit_urb(urb, GFP_KERNEL);
691 if (ret) {
692 usb_unanchor_urb(urb);
693 goto err_submit;
694 }
Sujith66b10e32010-04-06 15:28:13 +0530695
696 /*
697 * Drop reference count.
698 * This ensures that the URB is freed when killing them.
699 */
700 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530701 }
702
703 return 0;
704
Sujith6335ed02010-03-29 16:07:15 +0530705err_submit:
Ming Leif28a7b32010-04-13 00:28:53 +0800706 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530707err_skb:
708 usb_free_urb(urb);
709err_urb:
Sujithfb9987d2010-03-17 14:25:25 +0530710 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
711 return ret;
712}
713
714static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
715{
716 if (hif_dev->reg_in_urb) {
717 usb_kill_urb(hif_dev->reg_in_urb);
Sujith6335ed02010-03-29 16:07:15 +0530718 if (hif_dev->reg_in_urb->context)
Ming Leie6c6d332010-04-13 00:29:05 +0800719 kfree_skb((void *)hif_dev->reg_in_urb->context);
Sujithfb9987d2010-03-17 14:25:25 +0530720 usb_free_urb(hif_dev->reg_in_urb);
721 hif_dev->reg_in_urb = NULL;
722 }
723}
724
725static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
726{
727 struct sk_buff *skb;
728
729 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
730 if (hif_dev->reg_in_urb == NULL)
731 return -ENOMEM;
732
Ming Leie6c6d332010-04-13 00:29:05 +0800733 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
Sujithfb9987d2010-03-17 14:25:25 +0530734 if (!skb)
735 goto err;
736
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530737 usb_fill_bulk_urb(hif_dev->reg_in_urb, hif_dev->udev,
Rajkumar Manoharan0f529e92010-09-16 19:56:55 +0530738 usb_rcvbulkpipe(hif_dev->udev,
739 USB_REG_IN_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530740 skb->data, MAX_REG_IN_BUF_SIZE,
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530741 ath9k_hif_usb_reg_in_cb, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530742
743 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
Sujith6335ed02010-03-29 16:07:15 +0530744 goto err;
Sujithfb9987d2010-03-17 14:25:25 +0530745
746 return 0;
747
Sujithfb9987d2010-03-17 14:25:25 +0530748err:
749 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
750 return -ENOMEM;
751}
752
753static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
754{
Sujith6f0f2662010-04-06 15:28:17 +0530755 /* Register Write */
756 init_usb_anchor(&hif_dev->regout_submitted);
757
Sujithfb9987d2010-03-17 14:25:25 +0530758 /* TX */
759 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
760 goto err;
761
762 /* RX */
763 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530764 goto err_rx;
Sujithfb9987d2010-03-17 14:25:25 +0530765
Sujith6f0f2662010-04-06 15:28:17 +0530766 /* Register Read */
Sujithfb9987d2010-03-17 14:25:25 +0530767 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530768 goto err_reg;
Sujithfb9987d2010-03-17 14:25:25 +0530769
770 return 0;
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530771err_reg:
772 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
773err_rx:
774 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
Sujithfb9987d2010-03-17 14:25:25 +0530775err:
776 return -ENOMEM;
777}
778
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530779static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
780{
781 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
782 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
783 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
784 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
785}
786
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530787static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
788 u32 drv_info)
Sujithfb9987d2010-03-17 14:25:25 +0530789{
790 int transfer, err;
791 const void *data = hif_dev->firmware->data;
792 size_t len = hif_dev->firmware->size;
793 u32 addr = AR9271_FIRMWARE;
794 u8 *buf = kzalloc(4096, GFP_KERNEL);
Sujithb1762862010-06-02 15:53:34 +0530795 u32 firm_offset;
Sujithfb9987d2010-03-17 14:25:25 +0530796
797 if (!buf)
798 return -ENOMEM;
799
800 while (len) {
801 transfer = min_t(int, len, 4096);
802 memcpy(buf, data, transfer);
803
804 err = usb_control_msg(hif_dev->udev,
805 usb_sndctrlpipe(hif_dev->udev, 0),
806 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
807 addr >> 8, 0, buf, transfer, HZ);
808 if (err < 0) {
809 kfree(buf);
810 return err;
811 }
812
813 len -= transfer;
814 data += transfer;
815 addr += transfer;
816 }
817 kfree(buf);
818
Sujith Manoharan0b5ead92010-12-07 16:31:38 +0530819 if (IS_AR7010_DEVICE(drv_info))
Sujithb1762862010-06-02 15:53:34 +0530820 firm_offset = AR7010_FIRMWARE_TEXT;
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530821 else
Sujithb1762862010-06-02 15:53:34 +0530822 firm_offset = AR9271_FIRMWARE_TEXT;
823
Sujithfb9987d2010-03-17 14:25:25 +0530824 /*
825 * Issue FW download complete command to firmware.
826 */
827 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
828 FIRMWARE_DOWNLOAD_COMP,
829 0x40 | USB_DIR_OUT,
Sujithb1762862010-06-02 15:53:34 +0530830 firm_offset >> 8, 0, NULL, 0, HZ);
Sujithfb9987d2010-03-17 14:25:25 +0530831 if (err)
832 return -EIO;
833
834 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
Sujithce43cee2010-06-02 15:53:30 +0530835 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
Sujithfb9987d2010-03-17 14:25:25 +0530836
837 return 0;
838}
839
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530840static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
Sujithfb9987d2010-03-17 14:25:25 +0530841{
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530842 int ret, idx;
843 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
844 struct usb_endpoint_descriptor *endp;
Sujithfb9987d2010-03-17 14:25:25 +0530845
846 /* Request firmware */
Sujithce43cee2010-06-02 15:53:30 +0530847 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
848 &hif_dev->udev->dev);
Sujithfb9987d2010-03-17 14:25:25 +0530849 if (ret) {
850 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +0530851 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
Sujithfb9987d2010-03-17 14:25:25 +0530852 goto err_fw_req;
853 }
854
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530855 /* Download firmware */
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530856 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530857 if (ret) {
858 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +0530859 "ath9k_htc: Firmware - %s download failed\n",
860 hif_dev->fw_name);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530861 goto err_fw_download;
862 }
863
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530864 /* On downloading the firmware to the target, the USB descriptor of EP4
865 * is 'patched' to change the type of the endpoint to Bulk. This will
866 * bring down CPU usage during the scan period.
867 */
868 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
869 endp = &alt->endpoint[idx].desc;
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530870 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
871 == USB_ENDPOINT_XFER_INT) {
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530872 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
873 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
874 endp->bInterval = 0;
875 }
876 }
877
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530878 /* Alloc URBs */
879 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
880 if (ret) {
881 dev_err(&hif_dev->udev->dev,
882 "ath9k_htc: Unable to allocate URBs\n");
883 goto err_urb;
884 }
885
Sujithfb9987d2010-03-17 14:25:25 +0530886 return 0;
887
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530888err_urb:
Sujith Manoharancaa0a992010-12-07 16:32:02 +0530889 ath9k_hif_usb_dealloc_urbs(hif_dev);
890err_fw_download:
Sujithfb9987d2010-03-17 14:25:25 +0530891 release_firmware(hif_dev->firmware);
892err_fw_req:
893 hif_dev->firmware = NULL;
894 return ret;
895}
896
Sujithfb9987d2010-03-17 14:25:25 +0530897static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
898{
899 ath9k_hif_usb_dealloc_urbs(hif_dev);
900 if (hif_dev->firmware)
901 release_firmware(hif_dev->firmware);
902}
903
904static int ath9k_hif_usb_probe(struct usb_interface *interface,
905 const struct usb_device_id *id)
906{
907 struct usb_device *udev = interface_to_usbdev(interface);
908 struct hif_device_usb *hif_dev;
Sujithfb9987d2010-03-17 14:25:25 +0530909 int ret = 0;
910
911 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
912 if (!hif_dev) {
913 ret = -ENOMEM;
914 goto err_alloc;
915 }
916
917 usb_get_dev(udev);
918 hif_dev->udev = udev;
919 hif_dev->interface = interface;
920 hif_dev->device_id = id->idProduct;
921#ifdef CONFIG_PM
922 udev->reset_resume = 1;
923#endif
924 usb_set_intfdata(interface, hif_dev);
925
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +0530926 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
927 &hif_dev->udev->dev);
928 if (hif_dev->htc_handle == NULL) {
929 ret = -ENOMEM;
930 goto err_htc_hw_alloc;
931 }
932
Sujithce43cee2010-06-02 15:53:30 +0530933 /* Find out which firmware to load */
934
Sujith Manoharan0b5ead92010-12-07 16:31:38 +0530935 if (IS_AR7010_DEVICE(id->driver_info))
Sujithb1762862010-06-02 15:53:34 +0530936 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
John W. Linvilled0ee0eb2010-06-23 14:20:45 -0400937 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
Sujithb1762862010-06-02 15:53:34 +0530938 else
John W. Linvilled0ee0eb2010-06-23 14:20:45 -0400939 hif_dev->fw_name = FIRMWARE_AR7010;
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530940 else
John W. Linvilled0ee0eb2010-06-23 14:20:45 -0400941 hif_dev->fw_name = FIRMWARE_AR9271;
Sujithce43cee2010-06-02 15:53:30 +0530942
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530943 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
Sujithfb9987d2010-03-17 14:25:25 +0530944 if (ret) {
945 ret = -EINVAL;
946 goto err_hif_init_usb;
947 }
948
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +0530949 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
Vivek Natarajan21cb9872010-08-18 19:57:49 +0530950 &hif_dev->udev->dev, hif_dev->device_id,
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530951 hif_dev->udev->product, id->driver_info);
Sujithfb9987d2010-03-17 14:25:25 +0530952 if (ret) {
953 ret = -EINVAL;
954 goto err_htc_hw_init;
955 }
956
957 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
958
959 return 0;
960
961err_htc_hw_init:
Sujithfb9987d2010-03-17 14:25:25 +0530962 ath9k_hif_usb_dev_deinit(hif_dev);
963err_hif_init_usb:
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +0530964 ath9k_htc_hw_free(hif_dev->htc_handle);
965err_htc_hw_alloc:
Sujithfb9987d2010-03-17 14:25:25 +0530966 usb_set_intfdata(interface, NULL);
967 kfree(hif_dev);
968 usb_put_dev(udev);
969err_alloc:
970 return ret;
971}
972
Sujith62e47162010-04-23 10:28:16 +0530973static void ath9k_hif_usb_reboot(struct usb_device *udev)
974{
975 u32 reboot_cmd = 0xffffffff;
976 void *buf;
977 int ret;
978
Julia Lawalla465a2c2010-05-15 23:17:19 +0200979 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
Sujith62e47162010-04-23 10:28:16 +0530980 if (!buf)
981 return;
982
Sujith62e47162010-04-23 10:28:16 +0530983 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
984 buf, 4, NULL, HZ);
985 if (ret)
986 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
987
988 kfree(buf);
989}
990
Sujithfb9987d2010-03-17 14:25:25 +0530991static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
992{
993 struct usb_device *udev = interface_to_usbdev(interface);
Joe Perchesb2767362010-11-30 13:42:08 -0800994 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Sujithfb9987d2010-03-17 14:25:25 +0530995
996 if (hif_dev) {
Sujithd8f996f2010-04-23 10:28:20 +0530997 ath9k_htc_hw_deinit(hif_dev->htc_handle,
998 (udev->state == USB_STATE_NOTATTACHED) ? true : false);
Sujithfb9987d2010-03-17 14:25:25 +0530999 ath9k_htc_hw_free(hif_dev->htc_handle);
1000 ath9k_hif_usb_dev_deinit(hif_dev);
1001 usb_set_intfdata(interface, NULL);
1002 }
1003
1004 if (hif_dev->flags & HIF_USB_START)
Sujith62e47162010-04-23 10:28:16 +05301005 ath9k_hif_usb_reboot(udev);
Sujithfb9987d2010-03-17 14:25:25 +05301006
1007 kfree(hif_dev);
1008 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1009 usb_put_dev(udev);
1010}
1011
1012#ifdef CONFIG_PM
1013static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1014 pm_message_t message)
1015{
Joe Perchesb2767362010-11-30 13:42:08 -08001016 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Sujithfb9987d2010-03-17 14:25:25 +05301017
1018 ath9k_hif_usb_dealloc_urbs(hif_dev);
1019
1020 return 0;
1021}
1022
1023static int ath9k_hif_usb_resume(struct usb_interface *interface)
1024{
Joe Perchesb2767362010-11-30 13:42:08 -08001025 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301026 struct htc_target *htc_handle = hif_dev->htc_handle;
Sujithfb9987d2010-03-17 14:25:25 +05301027 int ret;
1028
1029 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1030 if (ret)
1031 return ret;
1032
1033 if (hif_dev->firmware) {
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301034 ret = ath9k_hif_usb_download_fw(hif_dev,
Sujith Manoharan0b5ead92010-12-07 16:31:38 +05301035 htc_handle->drv_priv->ah->hw_version.usbdev);
Sujithfb9987d2010-03-17 14:25:25 +05301036 if (ret)
1037 goto fail_resume;
1038 } else {
1039 ath9k_hif_usb_dealloc_urbs(hif_dev);
1040 return -EIO;
1041 }
1042
1043 mdelay(100);
1044
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301045 ret = ath9k_htc_resume(htc_handle);
Sujithfb9987d2010-03-17 14:25:25 +05301046
1047 if (ret)
1048 goto fail_resume;
1049
1050 return 0;
1051
1052fail_resume:
1053 ath9k_hif_usb_dealloc_urbs(hif_dev);
1054
1055 return ret;
1056}
1057#endif
1058
1059static struct usb_driver ath9k_hif_usb_driver = {
1060 .name = "ath9k_hif_usb",
1061 .probe = ath9k_hif_usb_probe,
1062 .disconnect = ath9k_hif_usb_disconnect,
1063#ifdef CONFIG_PM
1064 .suspend = ath9k_hif_usb_suspend,
1065 .resume = ath9k_hif_usb_resume,
1066 .reset_resume = ath9k_hif_usb_resume,
1067#endif
1068 .id_table = ath9k_hif_usb_ids,
1069 .soft_unbind = 1,
1070};
1071
1072int ath9k_hif_usb_init(void)
1073{
1074 return usb_register(&ath9k_hif_usb_driver);
1075}
1076
1077void ath9k_hif_usb_exit(void)
1078{
1079 usb_deregister(&ath9k_hif_usb_driver);
1080}