blob: 0b63a48462c7d8cabb73c601800dc2829a83bc12 [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 */
Sujith Manoharance18f392011-04-13 11:22:42 +053020#define FIRMWARE_AR7010_1_1 "htc_7010.fw"
21#define FIRMWARE_AR9271 "htc_9271.fw"
John W. Linvilled0ee0eb2010-06-23 14:20:45 -040022
John W. Linvilled0ee0eb2010-06-23 14:20:45 -040023MODULE_FIRMWARE(FIRMWARE_AR7010_1_1);
24MODULE_FIRMWARE(FIRMWARE_AR9271);
25
Sujithfb9987d2010-03-17 14:25:25 +053026static struct usb_device_id ath9k_hif_usb_ids[] = {
Sujith4e63f762010-06-17 10:29:01 +053027 { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
28 { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
Sujith4e63f762010-06-17 10:29:01 +053029 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
Sujith4e63f762010-06-17 10:29:01 +053030 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
31 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
32 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
Haitao Zhangac618d72010-11-07 12:50:24 +080033 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
Rajkumar Manoharan32b08952010-11-10 17:51:24 +053034 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
35 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
36 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
Sujith4e63f762010-06-17 10:29:01 +053037 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
Rajkumar Manoharan32b08952010-11-10 17:51:24 +053038 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
Sujith Manoharan452d7dd2010-12-13 07:39:32 +053039 { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
Sujith Manoharan0b5ead92010-12-07 16:31:38 +053040
41 { USB_DEVICE(0x0cf3, 0x7015),
42 .driver_info = AR9287_USB }, /* Atheros */
Rajkumar Manoharan64f12172010-11-19 16:53:20 +053043 { USB_DEVICE(0x1668, 0x1200),
Sujith Manoharan0b5ead92010-12-07 16:31:38 +053044 .driver_info = AR9287_USB }, /* Verizon */
45
46 { USB_DEVICE(0x0cf3, 0x7010),
47 .driver_info = AR9280_USB }, /* Atheros */
48 { USB_DEVICE(0x0846, 0x9018),
49 .driver_info = AR9280_USB }, /* Netgear WNDA3200 */
50 { USB_DEVICE(0x083A, 0xA704),
51 .driver_info = AR9280_USB }, /* SMC Networks */
52
Sujith Manoharan36bcce42011-02-21 07:47:52 +053053 { USB_DEVICE(0x0cf3, 0x20ff),
54 .driver_info = STORAGE_DEVICE },
55
Sujithfb9987d2010-03-17 14:25:25 +053056 { },
57};
58
59MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
60
61static int __hif_usb_tx(struct hif_device_usb *hif_dev);
62
63static void hif_usb_regout_cb(struct urb *urb)
64{
65 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
Sujithfb9987d2010-03-17 14:25:25 +053066
67 switch (urb->status) {
68 case 0:
69 break;
70 case -ENOENT:
71 case -ECONNRESET:
Sujithfb9987d2010-03-17 14:25:25 +053072 case -ENODEV:
73 case -ESHUTDOWN:
Sujith6f0f2662010-04-06 15:28:17 +053074 goto free;
Sujithfb9987d2010-03-17 14:25:25 +053075 default:
76 break;
77 }
78
79 if (cmd) {
80 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
81 cmd->skb, 1);
82 kfree(cmd);
Sujithfb9987d2010-03-17 14:25:25 +053083 }
Sujith6f0f2662010-04-06 15:28:17 +053084
85 return;
86free:
Ming Lei0fa35a52010-04-13 00:29:15 +080087 kfree_skb(cmd->skb);
Sujith6f0f2662010-04-06 15:28:17 +053088 kfree(cmd);
Sujithfb9987d2010-03-17 14:25:25 +053089}
90
91static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
92 struct sk_buff *skb)
93{
94 struct urb *urb;
95 struct cmd_buf *cmd;
96 int ret = 0;
97
98 urb = usb_alloc_urb(0, GFP_KERNEL);
99 if (urb == NULL)
100 return -ENOMEM;
101
102 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
103 if (cmd == NULL) {
104 usb_free_urb(urb);
105 return -ENOMEM;
106 }
107
108 cmd->skb = skb;
109 cmd->hif_dev = hif_dev;
110
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530111 usb_fill_bulk_urb(urb, hif_dev->udev,
112 usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530113 skb->data, skb->len,
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530114 hif_usb_regout_cb, cmd);
Sujithfb9987d2010-03-17 14:25:25 +0530115
Sujith6f0f2662010-04-06 15:28:17 +0530116 usb_anchor_urb(urb, &hif_dev->regout_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530117 ret = usb_submit_urb(urb, GFP_KERNEL);
118 if (ret) {
Sujith6f0f2662010-04-06 15:28:17 +0530119 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530120 kfree(cmd);
121 }
Sujith6f0f2662010-04-06 15:28:17 +0530122 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530123
124 return ret;
125}
126
Sujitheac8e382010-04-16 11:54:00 +0530127static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
128 struct sk_buff_head *list)
Ming Leif8e1d082010-04-13 00:29:27 +0800129{
130 struct sk_buff *skb;
Sujitheac8e382010-04-16 11:54:00 +0530131
132 while ((skb = __skb_dequeue(list)) != NULL) {
Ming Leif8e1d082010-04-13 00:29:27 +0800133 dev_kfree_skb_any(skb);
Sujitheac8e382010-04-16 11:54:00 +0530134 TX_STAT_INC(skb_dropped);
135 }
Ming Leif8e1d082010-04-13 00:29:27 +0800136}
137
Sujithc11d8f82010-04-23 10:28:09 +0530138static void hif_usb_tx_cb(struct urb *urb)
139{
140 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
Dan Carpenter690e7812010-05-14 16:50:56 +0200141 struct hif_device_usb *hif_dev;
Sujithc11d8f82010-04-23 10:28:09 +0530142 struct sk_buff *skb;
143
Dan Carpenter690e7812010-05-14 16:50:56 +0200144 if (!tx_buf || !tx_buf->hif_dev)
Sujithc11d8f82010-04-23 10:28:09 +0530145 return;
146
Dan Carpenter690e7812010-05-14 16:50:56 +0200147 hif_dev = tx_buf->hif_dev;
148
Sujithc11d8f82010-04-23 10:28:09 +0530149 switch (urb->status) {
150 case 0:
151 break;
152 case -ENOENT:
153 case -ECONNRESET:
154 case -ENODEV:
155 case -ESHUTDOWN:
156 /*
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530157 * The URB has been killed, free the SKBs.
Sujithc11d8f82010-04-23 10:28:09 +0530158 */
159 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530160
161 /*
162 * If the URBs are being flushed, no need to add this
163 * URB to the free list.
164 */
165 spin_lock(&hif_dev->tx.tx_lock);
166 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
167 spin_unlock(&hif_dev->tx.tx_lock);
168 return;
169 }
170 spin_unlock(&hif_dev->tx.tx_lock);
171
172 /*
173 * In the stop() case, this URB has to be added to
174 * the free list.
175 */
176 goto add_free;
Sujithc11d8f82010-04-23 10:28:09 +0530177 default:
178 break;
179 }
180
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530181 /*
182 * Check if TX has been stopped, this is needed because
183 * this CB could have been invoked just after the TX lock
184 * was released in hif_stop() and kill_urb() hasn't been
185 * called yet.
186 */
Sujithc11d8f82010-04-23 10:28:09 +0530187 spin_lock(&hif_dev->tx.tx_lock);
188 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
189 spin_unlock(&hif_dev->tx.tx_lock);
190 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
191 goto add_free;
192 }
193 spin_unlock(&hif_dev->tx.tx_lock);
194
195 /* Complete the queued SKBs. */
196 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
197 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
198 skb, 1);
199 TX_STAT_INC(skb_completed);
200 }
201
202add_free:
203 /* Re-initialize the SKB queue */
204 tx_buf->len = tx_buf->offset = 0;
205 __skb_queue_head_init(&tx_buf->skb_queue);
206
207 /* Add this TX buffer to the free list */
208 spin_lock(&hif_dev->tx.tx_lock);
209 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
210 hif_dev->tx.tx_buf_cnt++;
211 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
212 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
213 TX_STAT_INC(buf_completed);
214 spin_unlock(&hif_dev->tx.tx_lock);
215}
216
Sujithfb9987d2010-03-17 14:25:25 +0530217/* TX lock has to be taken */
218static int __hif_usb_tx(struct hif_device_usb *hif_dev)
219{
220 struct tx_buf *tx_buf = NULL;
221 struct sk_buff *nskb = NULL;
222 int ret = 0, i;
Sujith Manoharan2c273922011-02-27 09:23:52 +0530223 u16 tx_skb_cnt = 0;
Sujithfb9987d2010-03-17 14:25:25 +0530224 u8 *buf;
Sujith Manoharan2c273922011-02-27 09:23:52 +0530225 __le16 *hdr;
Sujithfb9987d2010-03-17 14:25:25 +0530226
227 if (hif_dev->tx.tx_skb_cnt == 0)
228 return 0;
229
230 /* Check if a free TX buffer is available */
231 if (list_empty(&hif_dev->tx.tx_buf))
232 return 0;
233
234 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
Sujithc11d8f82010-04-23 10:28:09 +0530235 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
Sujithfb9987d2010-03-17 14:25:25 +0530236 hif_dev->tx.tx_buf_cnt--;
237
238 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
239
240 for (i = 0; i < tx_skb_cnt; i++) {
241 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
242
243 /* Should never be NULL */
244 BUG_ON(!nskb);
245
246 hif_dev->tx.tx_skb_cnt--;
247
248 buf = tx_buf->buf;
249 buf += tx_buf->offset;
Sujith Manoharan2c273922011-02-27 09:23:52 +0530250 hdr = (__le16 *)buf;
251 *hdr++ = cpu_to_le16(nskb->len);
252 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
Sujithfb9987d2010-03-17 14:25:25 +0530253 buf += 4;
254 memcpy(buf, nskb->data, nskb->len);
255 tx_buf->len = nskb->len + 4;
256
257 if (i < (tx_skb_cnt - 1))
258 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
259
260 if (i == (tx_skb_cnt - 1))
261 tx_buf->len += tx_buf->offset;
262
263 __skb_queue_tail(&tx_buf->skb_queue, nskb);
264 TX_STAT_INC(skb_queued);
265 }
266
267 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
268 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
269 tx_buf->buf, tx_buf->len,
270 hif_usb_tx_cb, tx_buf);
271
272 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
273 if (ret) {
274 tx_buf->len = tx_buf->offset = 0;
Sujitheac8e382010-04-16 11:54:00 +0530275 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
Sujithfb9987d2010-03-17 14:25:25 +0530276 __skb_queue_head_init(&tx_buf->skb_queue);
277 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
278 hif_dev->tx.tx_buf_cnt++;
279 }
280
281 if (!ret)
282 TX_STAT_INC(buf_queued);
283
284 return ret;
285}
286
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530287static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
Sujithfb9987d2010-03-17 14:25:25 +0530288{
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530289 struct ath9k_htc_tx_ctl *tx_ctl;
Sujithfb9987d2010-03-17 14:25:25 +0530290 unsigned long flags;
291
292 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
293
294 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
295 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
296 return -ENODEV;
297 }
298
299 /* Check if the max queue count has been reached */
300 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
301 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
302 return -ENOMEM;
303 }
304
305 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
306 hif_dev->tx.tx_skb_cnt++;
307
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530308 tx_ctl = HTC_SKB_CB(skb);
309
310 /* Send normal/mgmt/beacon frames immediately */
311 if (tx_ctl->type != ATH9K_HTC_AMPDU)
Sujithfb9987d2010-03-17 14:25:25 +0530312 __hif_usb_tx(hif_dev);
313
314 /* Check if AMPDUs have to be sent immediately */
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530315 if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
Sujithfb9987d2010-03-17 14:25:25 +0530316 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
317 (hif_dev->tx.tx_skb_cnt < 2)) {
318 __hif_usb_tx(hif_dev);
319 }
320
321 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
322
323 return 0;
324}
325
326static void hif_usb_start(void *hif_handle, u8 pipe_id)
327{
328 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
329 unsigned long flags;
330
331 hif_dev->flags |= HIF_USB_START;
332
333 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
334 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
335 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
336}
337
338static void hif_usb_stop(void *hif_handle, u8 pipe_id)
339{
340 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530341 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530342 unsigned long flags;
343
344 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
Sujitheac8e382010-04-16 11:54:00 +0530345 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
Sujithfb9987d2010-03-17 14:25:25 +0530346 hif_dev->tx.tx_skb_cnt = 0;
347 hif_dev->tx.flags |= HIF_USB_TX_STOP;
348 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530349
350 /* The pending URBs have to be canceled. */
351 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
352 &hif_dev->tx.tx_pending, list) {
353 usb_kill_urb(tx_buf->urb);
354 }
Sujithfb9987d2010-03-17 14:25:25 +0530355}
356
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530357static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
Sujithfb9987d2010-03-17 14:25:25 +0530358{
359 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
360 int ret = 0;
361
362 switch (pipe_id) {
363 case USB_WLAN_TX_PIPE:
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530364 ret = hif_usb_send_tx(hif_dev, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530365 break;
366 case USB_REG_OUT_PIPE:
367 ret = hif_usb_send_regout(hif_dev, skb);
368 break;
369 default:
Sujith6335ed02010-03-29 16:07:15 +0530370 dev_err(&hif_dev->udev->dev,
371 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
Sujithfb9987d2010-03-17 14:25:25 +0530372 ret = -EINVAL;
373 break;
374 }
375
376 return ret;
377}
378
379static struct ath9k_htc_hif hif_usb = {
380 .transport = ATH9K_HIF_USB,
381 .name = "ath9k_hif_usb",
382
383 .control_ul_pipe = USB_REG_OUT_PIPE,
384 .control_dl_pipe = USB_REG_IN_PIPE,
385
386 .start = hif_usb_start,
387 .stop = hif_usb_stop,
388 .send = hif_usb_send,
389};
390
391static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
392 struct sk_buff *skb)
393{
Sujithc5032692010-04-06 15:28:15 +0530394 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
Joe Perches44b23b42010-11-30 12:19:11 -0800395 int index = 0, i = 0, len = skb->len;
396 int rx_remain_len, rx_pkt_len;
397 u16 pool_index = 0;
Sujithfb9987d2010-03-17 14:25:25 +0530398 u8 *ptr;
399
Sujith46baa1a2010-04-06 15:28:11 +0530400 spin_lock(&hif_dev->rx_lock);
401
Sujithfb9987d2010-03-17 14:25:25 +0530402 rx_remain_len = hif_dev->rx_remain_len;
403 rx_pkt_len = hif_dev->rx_transfer_len;
404
405 if (rx_remain_len != 0) {
406 struct sk_buff *remain_skb = hif_dev->remain_skb;
407
408 if (remain_skb) {
409 ptr = (u8 *) remain_skb->data;
410
411 index = rx_remain_len;
412 rx_remain_len -= hif_dev->rx_pad_len;
413 ptr += rx_pkt_len;
414
415 memcpy(ptr, skb->data, rx_remain_len);
416
417 rx_pkt_len += rx_remain_len;
418 hif_dev->rx_remain_len = 0;
419 skb_put(remain_skb, rx_pkt_len);
420
421 skb_pool[pool_index++] = remain_skb;
422
423 } else {
424 index = rx_remain_len;
425 }
426 }
427
Sujith46baa1a2010-04-06 15:28:11 +0530428 spin_unlock(&hif_dev->rx_lock);
429
Sujithfb9987d2010-03-17 14:25:25 +0530430 while (index < len) {
Joe Perches44b23b42010-11-30 12:19:11 -0800431 u16 pkt_len;
432 u16 pkt_tag;
433 u16 pad_len;
434 int chk_idx;
435
Sujithfb9987d2010-03-17 14:25:25 +0530436 ptr = (u8 *) skb->data;
437
438 pkt_len = ptr[index] + (ptr[index+1] << 8);
439 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
440
Joe Perches44b23b42010-11-30 12:19:11 -0800441 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
Sujithfb9987d2010-03-17 14:25:25 +0530442 RX_STAT_INC(skb_dropped);
Sujithfb9987d2010-03-17 14:25:25 +0530443 return;
444 }
Joe Perches44b23b42010-11-30 12:19:11 -0800445
446 pad_len = 4 - (pkt_len & 0x3);
447 if (pad_len == 4)
448 pad_len = 0;
449
450 chk_idx = index;
451 index = index + 4 + pkt_len + pad_len;
452
453 if (index > MAX_RX_BUF_SIZE) {
454 spin_lock(&hif_dev->rx_lock);
455 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
456 hif_dev->rx_transfer_len =
457 MAX_RX_BUF_SIZE - chk_idx - 4;
458 hif_dev->rx_pad_len = pad_len;
459
460 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
461 if (!nskb) {
462 dev_err(&hif_dev->udev->dev,
463 "ath9k_htc: RX memory allocation error\n");
464 spin_unlock(&hif_dev->rx_lock);
465 goto err;
466 }
467 skb_reserve(nskb, 32);
468 RX_STAT_INC(skb_allocated);
469
470 memcpy(nskb->data, &(skb->data[chk_idx+4]),
471 hif_dev->rx_transfer_len);
472
473 /* Record the buffer pointer */
474 hif_dev->remain_skb = nskb;
475 spin_unlock(&hif_dev->rx_lock);
476 } else {
477 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
478 if (!nskb) {
479 dev_err(&hif_dev->udev->dev,
480 "ath9k_htc: RX memory allocation error\n");
481 goto err;
482 }
483 skb_reserve(nskb, 32);
484 RX_STAT_INC(skb_allocated);
485
486 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
487 skb_put(nskb, pkt_len);
488 skb_pool[pool_index++] = nskb;
489 }
Sujithfb9987d2010-03-17 14:25:25 +0530490 }
491
492err:
Sujithfb9987d2010-03-17 14:25:25 +0530493 for (i = 0; i < pool_index; i++) {
494 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
495 skb_pool[i]->len, USB_WLAN_RX_PIPE);
496 RX_STAT_INC(skb_completed);
497 }
498}
499
500static void ath9k_hif_usb_rx_cb(struct urb *urb)
501{
502 struct sk_buff *skb = (struct sk_buff *) urb->context;
Joe Perchesb2767362010-11-30 13:42:08 -0800503 struct hif_device_usb *hif_dev =
Sujithfb9987d2010-03-17 14:25:25 +0530504 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
505 int ret;
506
Sujith6335ed02010-03-29 16:07:15 +0530507 if (!skb)
508 return;
509
Sujithfb9987d2010-03-17 14:25:25 +0530510 if (!hif_dev)
511 goto free;
512
513 switch (urb->status) {
514 case 0:
515 break;
516 case -ENOENT:
517 case -ECONNRESET:
518 case -ENODEV:
519 case -ESHUTDOWN:
520 goto free;
521 default:
522 goto resubmit;
523 }
524
525 if (likely(urb->actual_length != 0)) {
526 skb_put(skb, urb->actual_length);
Sujithfb9987d2010-03-17 14:25:25 +0530527 ath9k_hif_usb_rx_stream(hif_dev, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530528 }
529
530resubmit:
531 skb_reset_tail_pointer(skb);
532 skb_trim(skb, 0);
533
Sujith6335ed02010-03-29 16:07:15 +0530534 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530535 ret = usb_submit_urb(urb, GFP_ATOMIC);
Sujith6335ed02010-03-29 16:07:15 +0530536 if (ret) {
537 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530538 goto free;
Sujith6335ed02010-03-29 16:07:15 +0530539 }
Sujithfb9987d2010-03-17 14:25:25 +0530540
541 return;
542free:
Ming Leif28a7b32010-04-13 00:28:53 +0800543 kfree_skb(skb);
Sujithfb9987d2010-03-17 14:25:25 +0530544}
545
546static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
547{
548 struct sk_buff *skb = (struct sk_buff *) urb->context;
549 struct sk_buff *nskb;
Joe Perchesb2767362010-11-30 13:42:08 -0800550 struct hif_device_usb *hif_dev =
Sujithfb9987d2010-03-17 14:25:25 +0530551 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
552 int ret;
553
Sujith6335ed02010-03-29 16:07:15 +0530554 if (!skb)
555 return;
556
Sujithfb9987d2010-03-17 14:25:25 +0530557 if (!hif_dev)
558 goto free;
559
560 switch (urb->status) {
561 case 0:
562 break;
563 case -ENOENT:
564 case -ECONNRESET:
565 case -ENODEV:
566 case -ESHUTDOWN:
567 goto free;
568 default:
Sujith Manoharan3deff762011-04-13 11:25:23 +0530569 skb_reset_tail_pointer(skb);
570 skb_trim(skb, 0);
571
Sujithfb9987d2010-03-17 14:25:25 +0530572 goto resubmit;
573 }
574
575 if (likely(urb->actual_length != 0)) {
576 skb_put(skb, urb->actual_length);
577
Sujith5ab0af32010-04-23 10:28:17 +0530578 /* Process the command first */
579 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
580 skb->len, USB_REG_IN_PIPE);
581
582
Ming Leie6c6d332010-04-13 00:29:05 +0800583 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
Sujith5ab0af32010-04-23 10:28:17 +0530584 if (!nskb) {
585 dev_err(&hif_dev->udev->dev,
586 "ath9k_htc: REG_IN memory allocation failure\n");
587 urb->context = NULL;
588 return;
589 }
Sujithfb9987d2010-03-17 14:25:25 +0530590
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530591 usb_fill_bulk_urb(urb, hif_dev->udev,
Rajkumar Manoharan0f529e92010-09-16 19:56:55 +0530592 usb_rcvbulkpipe(hif_dev->udev,
593 USB_REG_IN_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530594 nskb->data, MAX_REG_IN_BUF_SIZE,
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530595 ath9k_hif_usb_reg_in_cb, nskb);
Sujithfb9987d2010-03-17 14:25:25 +0530596 }
597
598resubmit:
Sujith Manoharan3deff762011-04-13 11:25:23 +0530599 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530600 ret = usb_submit_urb(urb, GFP_ATOMIC);
Sujith Manoharan3deff762011-04-13 11:25:23 +0530601 if (ret) {
602 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530603 goto free;
Sujith Manoharan3deff762011-04-13 11:25:23 +0530604 }
Sujithfb9987d2010-03-17 14:25:25 +0530605
606 return;
607free:
Ming Leie6c6d332010-04-13 00:29:05 +0800608 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530609 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530610}
611
612static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
613{
Sujithfb9987d2010-03-17 14:25:25 +0530614 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530615 unsigned long flags;
Sujithfb9987d2010-03-17 14:25:25 +0530616
Sujithc11d8f82010-04-23 10:28:09 +0530617 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
618 &hif_dev->tx.tx_buf, list) {
619 usb_kill_urb(tx_buf->urb);
Sujithfb9987d2010-03-17 14:25:25 +0530620 list_del(&tx_buf->list);
621 usb_free_urb(tx_buf->urb);
622 kfree(tx_buf->buf);
623 kfree(tx_buf);
624 }
625
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530626 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
627 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
628 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
629
Sujithfb9987d2010-03-17 14:25:25 +0530630 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
631 &hif_dev->tx.tx_pending, list) {
632 usb_kill_urb(tx_buf->urb);
633 list_del(&tx_buf->list);
634 usb_free_urb(tx_buf->urb);
635 kfree(tx_buf->buf);
636 kfree(tx_buf);
637 }
Sujithfb9987d2010-03-17 14:25:25 +0530638}
639
640static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
641{
642 struct tx_buf *tx_buf;
643 int i;
644
645 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
646 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
647 spin_lock_init(&hif_dev->tx.tx_lock);
648 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
649
650 for (i = 0; i < MAX_TX_URB_NUM; i++) {
651 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
652 if (!tx_buf)
653 goto err;
654
655 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
656 if (!tx_buf->buf)
657 goto err;
658
659 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
660 if (!tx_buf->urb)
661 goto err;
662
663 tx_buf->hif_dev = hif_dev;
664 __skb_queue_head_init(&tx_buf->skb_queue);
665
666 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
667 }
668
669 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
670
671 return 0;
672err:
Dan Carpenter76066882010-05-14 16:52:37 +0200673 if (tx_buf) {
674 kfree(tx_buf->buf);
675 kfree(tx_buf);
676 }
Sujithfb9987d2010-03-17 14:25:25 +0530677 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
678 return -ENOMEM;
679}
680
Sujithfb9987d2010-03-17 14:25:25 +0530681static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
682{
Sujith6335ed02010-03-29 16:07:15 +0530683 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530684}
685
686static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
687{
Sujith6335ed02010-03-29 16:07:15 +0530688 struct urb *urb = NULL;
689 struct sk_buff *skb = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530690 int i, ret;
691
Sujith6335ed02010-03-29 16:07:15 +0530692 init_usb_anchor(&hif_dev->rx_submitted);
Sujith46baa1a2010-04-06 15:28:11 +0530693 spin_lock_init(&hif_dev->rx_lock);
Sujith6335ed02010-03-29 16:07:15 +0530694
Sujithfb9987d2010-03-17 14:25:25 +0530695 for (i = 0; i < MAX_RX_URB_NUM; i++) {
696
697 /* Allocate URB */
Sujith6335ed02010-03-29 16:07:15 +0530698 urb = usb_alloc_urb(0, GFP_KERNEL);
699 if (urb == NULL) {
Sujithfb9987d2010-03-17 14:25:25 +0530700 ret = -ENOMEM;
Sujith6335ed02010-03-29 16:07:15 +0530701 goto err_urb;
Sujithfb9987d2010-03-17 14:25:25 +0530702 }
703
704 /* Allocate buffer */
Ming Leif28a7b32010-04-13 00:28:53 +0800705 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
Sujith6335ed02010-03-29 16:07:15 +0530706 if (!skb) {
707 ret = -ENOMEM;
708 goto err_skb;
709 }
710
711 usb_fill_bulk_urb(urb, hif_dev->udev,
712 usb_rcvbulkpipe(hif_dev->udev,
713 USB_WLAN_RX_PIPE),
714 skb->data, MAX_RX_BUF_SIZE,
715 ath9k_hif_usb_rx_cb, skb);
716
717 /* Anchor URB */
718 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530719
720 /* Submit URB */
Sujith6335ed02010-03-29 16:07:15 +0530721 ret = usb_submit_urb(urb, GFP_KERNEL);
722 if (ret) {
723 usb_unanchor_urb(urb);
724 goto err_submit;
725 }
Sujith66b10e32010-04-06 15:28:13 +0530726
727 /*
728 * Drop reference count.
729 * This ensures that the URB is freed when killing them.
730 */
731 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530732 }
733
734 return 0;
735
Sujith6335ed02010-03-29 16:07:15 +0530736err_submit:
Ming Leif28a7b32010-04-13 00:28:53 +0800737 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530738err_skb:
739 usb_free_urb(urb);
740err_urb:
Sujithfb9987d2010-03-17 14:25:25 +0530741 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
742 return ret;
743}
744
Sujith Manoharan3deff762011-04-13 11:25:23 +0530745static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
Sujithfb9987d2010-03-17 14:25:25 +0530746{
Sujith Manoharan3deff762011-04-13 11:25:23 +0530747 usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530748}
749
Sujith Manoharan3deff762011-04-13 11:25:23 +0530750static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
Sujithfb9987d2010-03-17 14:25:25 +0530751{
Sujith Manoharan3deff762011-04-13 11:25:23 +0530752 struct urb *urb = NULL;
753 struct sk_buff *skb = NULL;
754 int i, ret;
Sujithfb9987d2010-03-17 14:25:25 +0530755
Sujith Manoharan3deff762011-04-13 11:25:23 +0530756 init_usb_anchor(&hif_dev->reg_in_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530757
Sujith Manoharan3deff762011-04-13 11:25:23 +0530758 for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
Sujithfb9987d2010-03-17 14:25:25 +0530759
Sujith Manoharan3deff762011-04-13 11:25:23 +0530760 /* Allocate URB */
761 urb = usb_alloc_urb(0, GFP_KERNEL);
762 if (urb == NULL) {
763 ret = -ENOMEM;
764 goto err_urb;
765 }
Sujithfb9987d2010-03-17 14:25:25 +0530766
Sujith Manoharan3deff762011-04-13 11:25:23 +0530767 /* Allocate buffer */
768 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
769 if (!skb) {
770 ret = -ENOMEM;
771 goto err_skb;
772 }
773
774 usb_fill_bulk_urb(urb, hif_dev->udev,
775 usb_rcvbulkpipe(hif_dev->udev,
776 USB_REG_IN_PIPE),
777 skb->data, MAX_REG_IN_BUF_SIZE,
778 ath9k_hif_usb_reg_in_cb, skb);
779
780 /* Anchor URB */
781 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
782
783 /* Submit URB */
784 ret = usb_submit_urb(urb, GFP_KERNEL);
785 if (ret) {
786 usb_unanchor_urb(urb);
787 goto err_submit;
788 }
789
790 /*
791 * Drop reference count.
792 * This ensures that the URB is freed when killing them.
793 */
794 usb_free_urb(urb);
795 }
Sujithfb9987d2010-03-17 14:25:25 +0530796
797 return 0;
798
Sujith Manoharan3deff762011-04-13 11:25:23 +0530799err_submit:
800 kfree_skb(skb);
801err_skb:
802 usb_free_urb(urb);
803err_urb:
804 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
805 return ret;
Sujithfb9987d2010-03-17 14:25:25 +0530806}
807
808static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
809{
Sujith6f0f2662010-04-06 15:28:17 +0530810 /* Register Write */
811 init_usb_anchor(&hif_dev->regout_submitted);
812
Sujithfb9987d2010-03-17 14:25:25 +0530813 /* TX */
814 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
815 goto err;
816
817 /* RX */
818 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530819 goto err_rx;
Sujithfb9987d2010-03-17 14:25:25 +0530820
Sujith6f0f2662010-04-06 15:28:17 +0530821 /* Register Read */
Sujith Manoharan3deff762011-04-13 11:25:23 +0530822 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530823 goto err_reg;
Sujithfb9987d2010-03-17 14:25:25 +0530824
825 return 0;
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530826err_reg:
827 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
828err_rx:
829 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
Sujithfb9987d2010-03-17 14:25:25 +0530830err:
831 return -ENOMEM;
832}
833
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530834static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
835{
836 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
Sujith Manoharan3deff762011-04-13 11:25:23 +0530837 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530838 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
839 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
840}
841
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530842static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
843 u32 drv_info)
Sujithfb9987d2010-03-17 14:25:25 +0530844{
845 int transfer, err;
846 const void *data = hif_dev->firmware->data;
847 size_t len = hif_dev->firmware->size;
848 u32 addr = AR9271_FIRMWARE;
849 u8 *buf = kzalloc(4096, GFP_KERNEL);
Sujithb1762862010-06-02 15:53:34 +0530850 u32 firm_offset;
Sujithfb9987d2010-03-17 14:25:25 +0530851
852 if (!buf)
853 return -ENOMEM;
854
855 while (len) {
856 transfer = min_t(int, len, 4096);
857 memcpy(buf, data, transfer);
858
859 err = usb_control_msg(hif_dev->udev,
860 usb_sndctrlpipe(hif_dev->udev, 0),
861 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
862 addr >> 8, 0, buf, transfer, HZ);
863 if (err < 0) {
864 kfree(buf);
865 return err;
866 }
867
868 len -= transfer;
869 data += transfer;
870 addr += transfer;
871 }
872 kfree(buf);
873
Sujith Manoharan0b5ead92010-12-07 16:31:38 +0530874 if (IS_AR7010_DEVICE(drv_info))
Sujithb1762862010-06-02 15:53:34 +0530875 firm_offset = AR7010_FIRMWARE_TEXT;
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530876 else
Sujithb1762862010-06-02 15:53:34 +0530877 firm_offset = AR9271_FIRMWARE_TEXT;
878
Sujithfb9987d2010-03-17 14:25:25 +0530879 /*
880 * Issue FW download complete command to firmware.
881 */
882 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
883 FIRMWARE_DOWNLOAD_COMP,
884 0x40 | USB_DIR_OUT,
Sujithb1762862010-06-02 15:53:34 +0530885 firm_offset >> 8, 0, NULL, 0, HZ);
Sujithfb9987d2010-03-17 14:25:25 +0530886 if (err)
887 return -EIO;
888
889 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
Sujithce43cee2010-06-02 15:53:30 +0530890 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
Sujithfb9987d2010-03-17 14:25:25 +0530891
892 return 0;
893}
894
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530895static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
Sujithfb9987d2010-03-17 14:25:25 +0530896{
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530897 int ret, idx;
898 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
899 struct usb_endpoint_descriptor *endp;
Sujithfb9987d2010-03-17 14:25:25 +0530900
901 /* Request firmware */
Sujithce43cee2010-06-02 15:53:30 +0530902 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
903 &hif_dev->udev->dev);
Sujithfb9987d2010-03-17 14:25:25 +0530904 if (ret) {
905 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +0530906 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
Sujithfb9987d2010-03-17 14:25:25 +0530907 goto err_fw_req;
908 }
909
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530910 /* Download firmware */
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530911 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530912 if (ret) {
913 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +0530914 "ath9k_htc: Firmware - %s download failed\n",
915 hif_dev->fw_name);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530916 goto err_fw_download;
917 }
918
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530919 /* On downloading the firmware to the target, the USB descriptor of EP4
920 * is 'patched' to change the type of the endpoint to Bulk. This will
921 * bring down CPU usage during the scan period.
922 */
923 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
924 endp = &alt->endpoint[idx].desc;
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530925 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
926 == USB_ENDPOINT_XFER_INT) {
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530927 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
928 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
929 endp->bInterval = 0;
930 }
931 }
932
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530933 /* Alloc URBs */
934 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
935 if (ret) {
936 dev_err(&hif_dev->udev->dev,
937 "ath9k_htc: Unable to allocate URBs\n");
Sujith Manoharan512c0442011-02-21 07:50:38 +0530938 goto err_fw_download;
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530939 }
940
Sujithfb9987d2010-03-17 14:25:25 +0530941 return 0;
942
Sujith Manoharancaa0a992010-12-07 16:32:02 +0530943err_fw_download:
Sujithfb9987d2010-03-17 14:25:25 +0530944 release_firmware(hif_dev->firmware);
945err_fw_req:
946 hif_dev->firmware = NULL;
947 return ret;
948}
949
Sujithfb9987d2010-03-17 14:25:25 +0530950static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
951{
952 ath9k_hif_usb_dealloc_urbs(hif_dev);
953 if (hif_dev->firmware)
954 release_firmware(hif_dev->firmware);
955}
956
Sujith Manoharan36bcce42011-02-21 07:47:52 +0530957/*
958 * An exact copy of the function from zd1211rw.
959 */
960static int send_eject_command(struct usb_interface *interface)
961{
962 struct usb_device *udev = interface_to_usbdev(interface);
963 struct usb_host_interface *iface_desc = &interface->altsetting[0];
964 struct usb_endpoint_descriptor *endpoint;
965 unsigned char *cmd;
966 u8 bulk_out_ep;
967 int r;
968
969 /* Find bulk out endpoint */
970 for (r = 1; r >= 0; r--) {
971 endpoint = &iface_desc->endpoint[r].desc;
972 if (usb_endpoint_dir_out(endpoint) &&
973 usb_endpoint_xfer_bulk(endpoint)) {
974 bulk_out_ep = endpoint->bEndpointAddress;
975 break;
976 }
977 }
978 if (r == -1) {
979 dev_err(&udev->dev,
980 "ath9k_htc: Could not find bulk out endpoint\n");
981 return -ENODEV;
982 }
983
984 cmd = kzalloc(31, GFP_KERNEL);
985 if (cmd == NULL)
986 return -ENODEV;
987
988 /* USB bulk command block */
989 cmd[0] = 0x55; /* bulk command signature */
990 cmd[1] = 0x53; /* bulk command signature */
991 cmd[2] = 0x42; /* bulk command signature */
992 cmd[3] = 0x43; /* bulk command signature */
993 cmd[14] = 6; /* command length */
994
995 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
996 cmd[19] = 0x2; /* eject disc */
997
998 dev_info(&udev->dev, "Ejecting storage device...\n");
999 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1000 cmd, 31, NULL, 2000);
1001 kfree(cmd);
1002 if (r)
1003 return r;
1004
1005 /* At this point, the device disconnects and reconnects with the real
1006 * ID numbers. */
1007
1008 usb_set_intfdata(interface, NULL);
1009 return 0;
1010}
1011
Sujithfb9987d2010-03-17 14:25:25 +05301012static int ath9k_hif_usb_probe(struct usb_interface *interface,
1013 const struct usb_device_id *id)
1014{
1015 struct usb_device *udev = interface_to_usbdev(interface);
1016 struct hif_device_usb *hif_dev;
Sujithfb9987d2010-03-17 14:25:25 +05301017 int ret = 0;
1018
Sujith Manoharan36bcce42011-02-21 07:47:52 +05301019 if (id->driver_info == STORAGE_DEVICE)
1020 return send_eject_command(interface);
1021
Sujithfb9987d2010-03-17 14:25:25 +05301022 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1023 if (!hif_dev) {
1024 ret = -ENOMEM;
1025 goto err_alloc;
1026 }
1027
1028 usb_get_dev(udev);
1029 hif_dev->udev = udev;
1030 hif_dev->interface = interface;
1031 hif_dev->device_id = id->idProduct;
1032#ifdef CONFIG_PM
1033 udev->reset_resume = 1;
1034#endif
1035 usb_set_intfdata(interface, hif_dev);
1036
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +05301037 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1038 &hif_dev->udev->dev);
1039 if (hif_dev->htc_handle == NULL) {
1040 ret = -ENOMEM;
1041 goto err_htc_hw_alloc;
1042 }
1043
Sujithce43cee2010-06-02 15:53:30 +05301044 /* Find out which firmware to load */
1045
Sujith Manoharan0b5ead92010-12-07 16:31:38 +05301046 if (IS_AR7010_DEVICE(id->driver_info))
Sujith Manoharan9efabad2011-04-13 11:22:33 +05301047 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301048 else
John W. Linvilled0ee0eb2010-06-23 14:20:45 -04001049 hif_dev->fw_name = FIRMWARE_AR9271;
Sujithce43cee2010-06-02 15:53:30 +05301050
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301051 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
Sujithfb9987d2010-03-17 14:25:25 +05301052 if (ret) {
1053 ret = -EINVAL;
1054 goto err_hif_init_usb;
1055 }
1056
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +05301057 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
Vivek Natarajan21cb9872010-08-18 19:57:49 +05301058 &hif_dev->udev->dev, hif_dev->device_id,
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301059 hif_dev->udev->product, id->driver_info);
Sujithfb9987d2010-03-17 14:25:25 +05301060 if (ret) {
1061 ret = -EINVAL;
1062 goto err_htc_hw_init;
1063 }
1064
1065 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
1066
1067 return 0;
1068
1069err_htc_hw_init:
Sujithfb9987d2010-03-17 14:25:25 +05301070 ath9k_hif_usb_dev_deinit(hif_dev);
1071err_hif_init_usb:
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +05301072 ath9k_htc_hw_free(hif_dev->htc_handle);
1073err_htc_hw_alloc:
Sujithfb9987d2010-03-17 14:25:25 +05301074 usb_set_intfdata(interface, NULL);
1075 kfree(hif_dev);
1076 usb_put_dev(udev);
1077err_alloc:
1078 return ret;
1079}
1080
Sujith62e47162010-04-23 10:28:16 +05301081static void ath9k_hif_usb_reboot(struct usb_device *udev)
1082{
1083 u32 reboot_cmd = 0xffffffff;
1084 void *buf;
1085 int ret;
1086
Julia Lawalla465a2c2010-05-15 23:17:19 +02001087 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
Sujith62e47162010-04-23 10:28:16 +05301088 if (!buf)
1089 return;
1090
Sujith62e47162010-04-23 10:28:16 +05301091 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1092 buf, 4, NULL, HZ);
1093 if (ret)
1094 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1095
1096 kfree(buf);
1097}
1098
Sujithfb9987d2010-03-17 14:25:25 +05301099static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1100{
1101 struct usb_device *udev = interface_to_usbdev(interface);
Joe Perchesb2767362010-11-30 13:42:08 -08001102 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Sujith Manoharan97dcec52010-12-20 08:02:42 +05301103 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
Sujithfb9987d2010-03-17 14:25:25 +05301104
Sujith Manoharan36bcce42011-02-21 07:47:52 +05301105 if (!hif_dev)
1106 return;
1107
1108 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1109 ath9k_htc_hw_free(hif_dev->htc_handle);
1110 ath9k_hif_usb_dev_deinit(hif_dev);
1111 usb_set_intfdata(interface, NULL);
Sujithfb9987d2010-03-17 14:25:25 +05301112
Sujith Manoharan97dcec52010-12-20 08:02:42 +05301113 if (!unplugged && (hif_dev->flags & HIF_USB_START))
Sujith62e47162010-04-23 10:28:16 +05301114 ath9k_hif_usb_reboot(udev);
Sujithfb9987d2010-03-17 14:25:25 +05301115
1116 kfree(hif_dev);
1117 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1118 usb_put_dev(udev);
1119}
1120
1121#ifdef CONFIG_PM
1122static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1123 pm_message_t message)
1124{
Joe Perchesb2767362010-11-30 13:42:08 -08001125 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Sujithfb9987d2010-03-17 14:25:25 +05301126
Sujith Manoharanf933ebe2010-12-01 12:30:27 +05301127 /*
1128 * The device has to be set to FULLSLEEP mode in case no
1129 * interface is up.
1130 */
1131 if (!(hif_dev->flags & HIF_USB_START))
1132 ath9k_htc_suspend(hif_dev->htc_handle);
1133
Sujithfb9987d2010-03-17 14:25:25 +05301134 ath9k_hif_usb_dealloc_urbs(hif_dev);
1135
1136 return 0;
1137}
1138
1139static int ath9k_hif_usb_resume(struct usb_interface *interface)
1140{
Joe Perchesb2767362010-11-30 13:42:08 -08001141 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301142 struct htc_target *htc_handle = hif_dev->htc_handle;
Sujithfb9987d2010-03-17 14:25:25 +05301143 int ret;
1144
1145 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1146 if (ret)
1147 return ret;
1148
1149 if (hif_dev->firmware) {
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301150 ret = ath9k_hif_usb_download_fw(hif_dev,
Sujith Manoharan0b5ead92010-12-07 16:31:38 +05301151 htc_handle->drv_priv->ah->hw_version.usbdev);
Sujithfb9987d2010-03-17 14:25:25 +05301152 if (ret)
1153 goto fail_resume;
1154 } else {
1155 ath9k_hif_usb_dealloc_urbs(hif_dev);
1156 return -EIO;
1157 }
1158
1159 mdelay(100);
1160
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301161 ret = ath9k_htc_resume(htc_handle);
Sujithfb9987d2010-03-17 14:25:25 +05301162
1163 if (ret)
1164 goto fail_resume;
1165
1166 return 0;
1167
1168fail_resume:
1169 ath9k_hif_usb_dealloc_urbs(hif_dev);
1170
1171 return ret;
1172}
1173#endif
1174
1175static struct usb_driver ath9k_hif_usb_driver = {
1176 .name = "ath9k_hif_usb",
1177 .probe = ath9k_hif_usb_probe,
1178 .disconnect = ath9k_hif_usb_disconnect,
1179#ifdef CONFIG_PM
1180 .suspend = ath9k_hif_usb_suspend,
1181 .resume = ath9k_hif_usb_resume,
1182 .reset_resume = ath9k_hif_usb_resume,
1183#endif
1184 .id_table = ath9k_hif_usb_ids,
1185 .soft_unbind = 1,
1186};
1187
1188int ath9k_hif_usb_init(void)
1189{
1190 return usb_register(&ath9k_hif_usb_driver);
1191}
1192
1193void ath9k_hif_usb_exit(void)
1194{
1195 usb_deregister(&ath9k_hif_usb_driver);
1196}