blob: 48bcc1a21076b03c4fb2899e859a4a7f4de0aa8d [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,
Sujith Manoharan2f801942011-04-13 11:26:46 +053081 cmd->skb, true);
Sujithfb9987d2010-03-17 14:25:25 +053082 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
Sujith Manoharan2f801942011-04-13 11:26:46 +0530127static void hif_usb_mgmt_cb(struct urb *urb)
128{
129 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
130 struct hif_device_usb *hif_dev = cmd->hif_dev;
131 bool txok = true;
132
133 if (!cmd || !cmd->skb || !cmd->hif_dev)
134 return;
135
136 switch (urb->status) {
137 case 0:
138 break;
139 case -ENOENT:
140 case -ECONNRESET:
141 case -ENODEV:
142 case -ESHUTDOWN:
143 txok = false;
144
145 /*
146 * If the URBs are being flushed, no need to complete
147 * this packet.
148 */
149 spin_lock(&hif_dev->tx.tx_lock);
150 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
151 spin_unlock(&hif_dev->tx.tx_lock);
152 dev_kfree_skb_any(cmd->skb);
153 kfree(cmd);
154 return;
155 }
156 spin_unlock(&hif_dev->tx.tx_lock);
157
158 break;
159 default:
160 txok = false;
161 break;
162 }
163
164 skb_pull(cmd->skb, 4);
165 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
166 cmd->skb, txok);
167 kfree(cmd);
168}
169
170static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev,
171 struct sk_buff *skb)
172{
173 struct urb *urb;
174 struct cmd_buf *cmd;
175 int ret = 0;
176 __le16 *hdr;
177
178 urb = usb_alloc_urb(0, GFP_ATOMIC);
179 if (urb == NULL)
180 return -ENOMEM;
181
182 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
183 if (cmd == NULL) {
184 usb_free_urb(urb);
185 return -ENOMEM;
186 }
187
188 cmd->skb = skb;
189 cmd->hif_dev = hif_dev;
190
191 hdr = (__le16 *) skb_push(skb, 4);
192 *hdr++ = cpu_to_le16(skb->len - 4);
193 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
194
195 usb_fill_bulk_urb(urb, hif_dev->udev,
196 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
197 skb->data, skb->len,
198 hif_usb_mgmt_cb, cmd);
199
200 usb_anchor_urb(urb, &hif_dev->mgmt_submitted);
201 ret = usb_submit_urb(urb, GFP_ATOMIC);
202 if (ret) {
203 usb_unanchor_urb(urb);
204 kfree(cmd);
205 }
206 usb_free_urb(urb);
207
208 return ret;
209}
210
Sujitheac8e382010-04-16 11:54:00 +0530211static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
212 struct sk_buff_head *list)
Ming Leif8e1d082010-04-13 00:29:27 +0800213{
214 struct sk_buff *skb;
Sujitheac8e382010-04-16 11:54:00 +0530215
216 while ((skb = __skb_dequeue(list)) != NULL) {
Ming Leif8e1d082010-04-13 00:29:27 +0800217 dev_kfree_skb_any(skb);
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530218 }
219}
220
221static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
222 struct sk_buff_head *queue,
223 bool txok)
224{
225 struct sk_buff *skb;
226
227 while ((skb = __skb_dequeue(queue)) != NULL) {
228 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
229 skb, txok);
John W. Linvilledfa8fc62011-04-14 10:38:22 -0400230 if (txok)
231 TX_STAT_INC(skb_success);
232 else
233 TX_STAT_INC(skb_failed);
Sujitheac8e382010-04-16 11:54:00 +0530234 }
Ming Leif8e1d082010-04-13 00:29:27 +0800235}
236
Sujithc11d8f82010-04-23 10:28:09 +0530237static void hif_usb_tx_cb(struct urb *urb)
238{
239 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
Dan Carpenter690e7812010-05-14 16:50:56 +0200240 struct hif_device_usb *hif_dev;
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530241 bool txok = true;
Sujithc11d8f82010-04-23 10:28:09 +0530242
Dan Carpenter690e7812010-05-14 16:50:56 +0200243 if (!tx_buf || !tx_buf->hif_dev)
Sujithc11d8f82010-04-23 10:28:09 +0530244 return;
245
Dan Carpenter690e7812010-05-14 16:50:56 +0200246 hif_dev = tx_buf->hif_dev;
247
Sujithc11d8f82010-04-23 10:28:09 +0530248 switch (urb->status) {
249 case 0:
250 break;
251 case -ENOENT:
252 case -ECONNRESET:
253 case -ENODEV:
254 case -ESHUTDOWN:
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530255 txok = false;
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530256
257 /*
258 * If the URBs are being flushed, no need to add this
259 * URB to the free list.
260 */
261 spin_lock(&hif_dev->tx.tx_lock);
262 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
263 spin_unlock(&hif_dev->tx.tx_lock);
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530264 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530265 return;
266 }
267 spin_unlock(&hif_dev->tx.tx_lock);
268
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530269 break;
Sujithc11d8f82010-04-23 10:28:09 +0530270 default:
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530271 txok = false;
Sujithc11d8f82010-04-23 10:28:09 +0530272 break;
273 }
274
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530275 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
Sujithc11d8f82010-04-23 10:28:09 +0530276
Sujithc11d8f82010-04-23 10:28:09 +0530277 /* Re-initialize the SKB queue */
278 tx_buf->len = tx_buf->offset = 0;
279 __skb_queue_head_init(&tx_buf->skb_queue);
280
281 /* Add this TX buffer to the free list */
282 spin_lock(&hif_dev->tx.tx_lock);
283 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
284 hif_dev->tx.tx_buf_cnt++;
285 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
286 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
287 TX_STAT_INC(buf_completed);
288 spin_unlock(&hif_dev->tx.tx_lock);
289}
290
Sujithfb9987d2010-03-17 14:25:25 +0530291/* TX lock has to be taken */
292static int __hif_usb_tx(struct hif_device_usb *hif_dev)
293{
294 struct tx_buf *tx_buf = NULL;
295 struct sk_buff *nskb = NULL;
296 int ret = 0, i;
Sujith Manoharan2c273922011-02-27 09:23:52 +0530297 u16 tx_skb_cnt = 0;
Sujithfb9987d2010-03-17 14:25:25 +0530298 u8 *buf;
Sujith Manoharan2c273922011-02-27 09:23:52 +0530299 __le16 *hdr;
Sujithfb9987d2010-03-17 14:25:25 +0530300
301 if (hif_dev->tx.tx_skb_cnt == 0)
302 return 0;
303
304 /* Check if a free TX buffer is available */
305 if (list_empty(&hif_dev->tx.tx_buf))
306 return 0;
307
308 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
Sujithc11d8f82010-04-23 10:28:09 +0530309 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
Sujithfb9987d2010-03-17 14:25:25 +0530310 hif_dev->tx.tx_buf_cnt--;
311
312 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
313
314 for (i = 0; i < tx_skb_cnt; i++) {
315 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
316
317 /* Should never be NULL */
318 BUG_ON(!nskb);
319
320 hif_dev->tx.tx_skb_cnt--;
321
322 buf = tx_buf->buf;
323 buf += tx_buf->offset;
Sujith Manoharan2c273922011-02-27 09:23:52 +0530324 hdr = (__le16 *)buf;
325 *hdr++ = cpu_to_le16(nskb->len);
326 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
Sujithfb9987d2010-03-17 14:25:25 +0530327 buf += 4;
328 memcpy(buf, nskb->data, nskb->len);
329 tx_buf->len = nskb->len + 4;
330
331 if (i < (tx_skb_cnt - 1))
332 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
333
334 if (i == (tx_skb_cnt - 1))
335 tx_buf->len += tx_buf->offset;
336
337 __skb_queue_tail(&tx_buf->skb_queue, nskb);
338 TX_STAT_INC(skb_queued);
339 }
340
341 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
342 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
343 tx_buf->buf, tx_buf->len,
344 hif_usb_tx_cb, tx_buf);
345
346 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
347 if (ret) {
348 tx_buf->len = tx_buf->offset = 0;
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530349 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
Sujithfb9987d2010-03-17 14:25:25 +0530350 __skb_queue_head_init(&tx_buf->skb_queue);
351 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
352 hif_dev->tx.tx_buf_cnt++;
353 }
354
355 if (!ret)
356 TX_STAT_INC(buf_queued);
357
358 return ret;
359}
360
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530361static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
Sujithfb9987d2010-03-17 14:25:25 +0530362{
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530363 struct ath9k_htc_tx_ctl *tx_ctl;
Sujithfb9987d2010-03-17 14:25:25 +0530364 unsigned long flags;
Sujith Manoharan2f801942011-04-13 11:26:46 +0530365 int ret = 0;
Sujithfb9987d2010-03-17 14:25:25 +0530366
367 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
368
369 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
370 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
371 return -ENODEV;
372 }
373
374 /* Check if the max queue count has been reached */
375 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
376 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
377 return -ENOMEM;
378 }
379
Sujith Manoharan2f801942011-04-13 11:26:46 +0530380 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
Sujithfb9987d2010-03-17 14:25:25 +0530381
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530382 tx_ctl = HTC_SKB_CB(skb);
383
Sujith Manoharan2f801942011-04-13 11:26:46 +0530384 /* Mgmt/Beacon frames don't use the TX buffer pool */
385 if ((tx_ctl->type == ATH9K_HTC_MGMT) ||
386 (tx_ctl->type == ATH9K_HTC_BEACON)) {
387 ret = hif_usb_send_mgmt(hif_dev, skb);
388 }
389
390 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
391
392 if ((tx_ctl->type == ATH9K_HTC_NORMAL) ||
393 (tx_ctl->type == ATH9K_HTC_AMPDU)) {
394 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
395 hif_dev->tx.tx_skb_cnt++;
396 }
Sujithfb9987d2010-03-17 14:25:25 +0530397
398 /* Check if AMPDUs have to be sent immediately */
Sujith Manoharan2f801942011-04-13 11:26:46 +0530399 if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
Sujithfb9987d2010-03-17 14:25:25 +0530400 (hif_dev->tx.tx_skb_cnt < 2)) {
401 __hif_usb_tx(hif_dev);
402 }
403
404 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
405
Sujith Manoharan2f801942011-04-13 11:26:46 +0530406 return ret;
Sujithfb9987d2010-03-17 14:25:25 +0530407}
408
Sujith Manoharane1fe7c32011-04-13 11:26:06 +0530409static void hif_usb_start(void *hif_handle)
Sujithfb9987d2010-03-17 14:25:25 +0530410{
411 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
412 unsigned long flags;
413
414 hif_dev->flags |= HIF_USB_START;
415
416 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
417 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
418 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
419}
420
Sujith Manoharane1fe7c32011-04-13 11:26:06 +0530421static void hif_usb_stop(void *hif_handle)
Sujithfb9987d2010-03-17 14:25:25 +0530422{
423 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530424 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530425 unsigned long flags;
426
427 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530428 ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
Sujithfb9987d2010-03-17 14:25:25 +0530429 hif_dev->tx.tx_skb_cnt = 0;
430 hif_dev->tx.flags |= HIF_USB_TX_STOP;
431 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530432
433 /* The pending URBs have to be canceled. */
434 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
435 &hif_dev->tx.tx_pending, list) {
436 usb_kill_urb(tx_buf->urb);
437 }
Sujith Manoharan2f801942011-04-13 11:26:46 +0530438
439 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530440}
441
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530442static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
Sujithfb9987d2010-03-17 14:25:25 +0530443{
444 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
445 int ret = 0;
446
447 switch (pipe_id) {
448 case USB_WLAN_TX_PIPE:
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530449 ret = hif_usb_send_tx(hif_dev, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530450 break;
451 case USB_REG_OUT_PIPE:
452 ret = hif_usb_send_regout(hif_dev, skb);
453 break;
454 default:
Sujith6335ed02010-03-29 16:07:15 +0530455 dev_err(&hif_dev->udev->dev,
456 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
Sujithfb9987d2010-03-17 14:25:25 +0530457 ret = -EINVAL;
458 break;
459 }
460
461 return ret;
462}
463
Sujith Manoharan84c9e1642011-04-13 11:26:11 +0530464static inline bool check_index(struct sk_buff *skb, u8 idx)
465{
466 struct ath9k_htc_tx_ctl *tx_ctl;
467
468 tx_ctl = HTC_SKB_CB(skb);
469
470 if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
471 (tx_ctl->sta_idx == idx))
472 return true;
473
474 return false;
475}
476
477static void hif_usb_sta_drain(void *hif_handle, u8 idx)
478{
479 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
480 struct sk_buff *skb, *tmp;
481 unsigned long flags;
482
483 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
484
485 skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
486 if (check_index(skb, idx)) {
487 __skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
488 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
489 skb, false);
490 hif_dev->tx.tx_skb_cnt--;
491 TX_STAT_INC(skb_failed);
492 }
493 }
494
495 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
496}
497
Sujithfb9987d2010-03-17 14:25:25 +0530498static struct ath9k_htc_hif hif_usb = {
499 .transport = ATH9K_HIF_USB,
500 .name = "ath9k_hif_usb",
501
502 .control_ul_pipe = USB_REG_OUT_PIPE,
503 .control_dl_pipe = USB_REG_IN_PIPE,
504
505 .start = hif_usb_start,
506 .stop = hif_usb_stop,
Sujith Manoharan84c9e1642011-04-13 11:26:11 +0530507 .sta_drain = hif_usb_sta_drain,
Sujithfb9987d2010-03-17 14:25:25 +0530508 .send = hif_usb_send,
509};
510
511static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
512 struct sk_buff *skb)
513{
Sujithc5032692010-04-06 15:28:15 +0530514 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
Joe Perches44b23b42010-11-30 12:19:11 -0800515 int index = 0, i = 0, len = skb->len;
516 int rx_remain_len, rx_pkt_len;
517 u16 pool_index = 0;
Sujithfb9987d2010-03-17 14:25:25 +0530518 u8 *ptr;
519
Sujith46baa1a2010-04-06 15:28:11 +0530520 spin_lock(&hif_dev->rx_lock);
521
Sujithfb9987d2010-03-17 14:25:25 +0530522 rx_remain_len = hif_dev->rx_remain_len;
523 rx_pkt_len = hif_dev->rx_transfer_len;
524
525 if (rx_remain_len != 0) {
526 struct sk_buff *remain_skb = hif_dev->remain_skb;
527
528 if (remain_skb) {
529 ptr = (u8 *) remain_skb->data;
530
531 index = rx_remain_len;
532 rx_remain_len -= hif_dev->rx_pad_len;
533 ptr += rx_pkt_len;
534
535 memcpy(ptr, skb->data, rx_remain_len);
536
537 rx_pkt_len += rx_remain_len;
538 hif_dev->rx_remain_len = 0;
539 skb_put(remain_skb, rx_pkt_len);
540
541 skb_pool[pool_index++] = remain_skb;
542
543 } else {
544 index = rx_remain_len;
545 }
546 }
547
Sujith46baa1a2010-04-06 15:28:11 +0530548 spin_unlock(&hif_dev->rx_lock);
549
Sujithfb9987d2010-03-17 14:25:25 +0530550 while (index < len) {
Joe Perches44b23b42010-11-30 12:19:11 -0800551 u16 pkt_len;
552 u16 pkt_tag;
553 u16 pad_len;
554 int chk_idx;
555
Sujithfb9987d2010-03-17 14:25:25 +0530556 ptr = (u8 *) skb->data;
557
558 pkt_len = ptr[index] + (ptr[index+1] << 8);
559 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
560
Joe Perches44b23b42010-11-30 12:19:11 -0800561 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
Sujithfb9987d2010-03-17 14:25:25 +0530562 RX_STAT_INC(skb_dropped);
Sujithfb9987d2010-03-17 14:25:25 +0530563 return;
564 }
Joe Perches44b23b42010-11-30 12:19:11 -0800565
566 pad_len = 4 - (pkt_len & 0x3);
567 if (pad_len == 4)
568 pad_len = 0;
569
570 chk_idx = index;
571 index = index + 4 + pkt_len + pad_len;
572
573 if (index > MAX_RX_BUF_SIZE) {
574 spin_lock(&hif_dev->rx_lock);
575 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
576 hif_dev->rx_transfer_len =
577 MAX_RX_BUF_SIZE - chk_idx - 4;
578 hif_dev->rx_pad_len = pad_len;
579
580 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
581 if (!nskb) {
582 dev_err(&hif_dev->udev->dev,
583 "ath9k_htc: RX memory allocation error\n");
584 spin_unlock(&hif_dev->rx_lock);
585 goto err;
586 }
587 skb_reserve(nskb, 32);
588 RX_STAT_INC(skb_allocated);
589
590 memcpy(nskb->data, &(skb->data[chk_idx+4]),
591 hif_dev->rx_transfer_len);
592
593 /* Record the buffer pointer */
594 hif_dev->remain_skb = nskb;
595 spin_unlock(&hif_dev->rx_lock);
596 } else {
597 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
598 if (!nskb) {
599 dev_err(&hif_dev->udev->dev,
600 "ath9k_htc: RX memory allocation error\n");
601 goto err;
602 }
603 skb_reserve(nskb, 32);
604 RX_STAT_INC(skb_allocated);
605
606 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
607 skb_put(nskb, pkt_len);
608 skb_pool[pool_index++] = nskb;
609 }
Sujithfb9987d2010-03-17 14:25:25 +0530610 }
611
612err:
Sujithfb9987d2010-03-17 14:25:25 +0530613 for (i = 0; i < pool_index; i++) {
614 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
615 skb_pool[i]->len, USB_WLAN_RX_PIPE);
616 RX_STAT_INC(skb_completed);
617 }
618}
619
620static void ath9k_hif_usb_rx_cb(struct urb *urb)
621{
622 struct sk_buff *skb = (struct sk_buff *) urb->context;
Joe Perchesb2767362010-11-30 13:42:08 -0800623 struct hif_device_usb *hif_dev =
Sujithfb9987d2010-03-17 14:25:25 +0530624 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
625 int ret;
626
Sujith6335ed02010-03-29 16:07:15 +0530627 if (!skb)
628 return;
629
Sujithfb9987d2010-03-17 14:25:25 +0530630 if (!hif_dev)
631 goto free;
632
633 switch (urb->status) {
634 case 0:
635 break;
636 case -ENOENT:
637 case -ECONNRESET:
638 case -ENODEV:
639 case -ESHUTDOWN:
640 goto free;
641 default:
642 goto resubmit;
643 }
644
645 if (likely(urb->actual_length != 0)) {
646 skb_put(skb, urb->actual_length);
Sujithfb9987d2010-03-17 14:25:25 +0530647 ath9k_hif_usb_rx_stream(hif_dev, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530648 }
649
650resubmit:
651 skb_reset_tail_pointer(skb);
652 skb_trim(skb, 0);
653
Sujith6335ed02010-03-29 16:07:15 +0530654 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530655 ret = usb_submit_urb(urb, GFP_ATOMIC);
Sujith6335ed02010-03-29 16:07:15 +0530656 if (ret) {
657 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530658 goto free;
Sujith6335ed02010-03-29 16:07:15 +0530659 }
Sujithfb9987d2010-03-17 14:25:25 +0530660
661 return;
662free:
Ming Leif28a7b32010-04-13 00:28:53 +0800663 kfree_skb(skb);
Sujithfb9987d2010-03-17 14:25:25 +0530664}
665
666static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
667{
668 struct sk_buff *skb = (struct sk_buff *) urb->context;
669 struct sk_buff *nskb;
Joe Perchesb2767362010-11-30 13:42:08 -0800670 struct hif_device_usb *hif_dev =
Sujithfb9987d2010-03-17 14:25:25 +0530671 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
672 int ret;
673
Sujith6335ed02010-03-29 16:07:15 +0530674 if (!skb)
675 return;
676
Sujithfb9987d2010-03-17 14:25:25 +0530677 if (!hif_dev)
678 goto free;
679
680 switch (urb->status) {
681 case 0:
682 break;
683 case -ENOENT:
684 case -ECONNRESET:
685 case -ENODEV:
686 case -ESHUTDOWN:
687 goto free;
688 default:
Sujith Manoharan3deff762011-04-13 11:25:23 +0530689 skb_reset_tail_pointer(skb);
690 skb_trim(skb, 0);
691
Sujithfb9987d2010-03-17 14:25:25 +0530692 goto resubmit;
693 }
694
695 if (likely(urb->actual_length != 0)) {
696 skb_put(skb, urb->actual_length);
697
Sujith5ab0af32010-04-23 10:28:17 +0530698 /* Process the command first */
699 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
700 skb->len, USB_REG_IN_PIPE);
701
702
Ming Leie6c6d332010-04-13 00:29:05 +0800703 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
Sujith5ab0af32010-04-23 10:28:17 +0530704 if (!nskb) {
705 dev_err(&hif_dev->udev->dev,
706 "ath9k_htc: REG_IN memory allocation failure\n");
707 urb->context = NULL;
708 return;
709 }
Sujithfb9987d2010-03-17 14:25:25 +0530710
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530711 usb_fill_bulk_urb(urb, hif_dev->udev,
Rajkumar Manoharan0f529e92010-09-16 19:56:55 +0530712 usb_rcvbulkpipe(hif_dev->udev,
713 USB_REG_IN_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530714 nskb->data, MAX_REG_IN_BUF_SIZE,
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530715 ath9k_hif_usb_reg_in_cb, nskb);
Sujithfb9987d2010-03-17 14:25:25 +0530716 }
717
718resubmit:
Sujith Manoharan3deff762011-04-13 11:25:23 +0530719 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530720 ret = usb_submit_urb(urb, GFP_ATOMIC);
Sujith Manoharan3deff762011-04-13 11:25:23 +0530721 if (ret) {
722 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530723 goto free;
Sujith Manoharan3deff762011-04-13 11:25:23 +0530724 }
Sujithfb9987d2010-03-17 14:25:25 +0530725
726 return;
727free:
Ming Leie6c6d332010-04-13 00:29:05 +0800728 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530729 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530730}
731
732static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
733{
Sujithfb9987d2010-03-17 14:25:25 +0530734 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530735 unsigned long flags;
Sujithfb9987d2010-03-17 14:25:25 +0530736
Sujithc11d8f82010-04-23 10:28:09 +0530737 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
738 &hif_dev->tx.tx_buf, list) {
739 usb_kill_urb(tx_buf->urb);
Sujithfb9987d2010-03-17 14:25:25 +0530740 list_del(&tx_buf->list);
741 usb_free_urb(tx_buf->urb);
742 kfree(tx_buf->buf);
743 kfree(tx_buf);
744 }
745
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530746 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
747 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
748 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
749
Sujithfb9987d2010-03-17 14:25:25 +0530750 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
751 &hif_dev->tx.tx_pending, list) {
752 usb_kill_urb(tx_buf->urb);
753 list_del(&tx_buf->list);
754 usb_free_urb(tx_buf->urb);
755 kfree(tx_buf->buf);
756 kfree(tx_buf);
757 }
Sujith Manoharan2f801942011-04-13 11:26:46 +0530758
759 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530760}
761
762static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
763{
764 struct tx_buf *tx_buf;
765 int i;
766
767 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
768 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
769 spin_lock_init(&hif_dev->tx.tx_lock);
770 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
Sujith Manoharan2f801942011-04-13 11:26:46 +0530771 init_usb_anchor(&hif_dev->mgmt_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530772
773 for (i = 0; i < MAX_TX_URB_NUM; i++) {
774 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
775 if (!tx_buf)
776 goto err;
777
778 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
779 if (!tx_buf->buf)
780 goto err;
781
782 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
783 if (!tx_buf->urb)
784 goto err;
785
786 tx_buf->hif_dev = hif_dev;
787 __skb_queue_head_init(&tx_buf->skb_queue);
788
789 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
790 }
791
792 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
793
794 return 0;
795err:
Dan Carpenter76066882010-05-14 16:52:37 +0200796 if (tx_buf) {
797 kfree(tx_buf->buf);
798 kfree(tx_buf);
799 }
Sujithfb9987d2010-03-17 14:25:25 +0530800 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
801 return -ENOMEM;
802}
803
Sujithfb9987d2010-03-17 14:25:25 +0530804static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
805{
Sujith6335ed02010-03-29 16:07:15 +0530806 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530807}
808
809static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
810{
Sujith6335ed02010-03-29 16:07:15 +0530811 struct urb *urb = NULL;
812 struct sk_buff *skb = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530813 int i, ret;
814
Sujith6335ed02010-03-29 16:07:15 +0530815 init_usb_anchor(&hif_dev->rx_submitted);
Sujith46baa1a2010-04-06 15:28:11 +0530816 spin_lock_init(&hif_dev->rx_lock);
Sujith6335ed02010-03-29 16:07:15 +0530817
Sujithfb9987d2010-03-17 14:25:25 +0530818 for (i = 0; i < MAX_RX_URB_NUM; i++) {
819
820 /* Allocate URB */
Sujith6335ed02010-03-29 16:07:15 +0530821 urb = usb_alloc_urb(0, GFP_KERNEL);
822 if (urb == NULL) {
Sujithfb9987d2010-03-17 14:25:25 +0530823 ret = -ENOMEM;
Sujith6335ed02010-03-29 16:07:15 +0530824 goto err_urb;
Sujithfb9987d2010-03-17 14:25:25 +0530825 }
826
827 /* Allocate buffer */
Ming Leif28a7b32010-04-13 00:28:53 +0800828 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
Sujith6335ed02010-03-29 16:07:15 +0530829 if (!skb) {
830 ret = -ENOMEM;
831 goto err_skb;
832 }
833
834 usb_fill_bulk_urb(urb, hif_dev->udev,
835 usb_rcvbulkpipe(hif_dev->udev,
836 USB_WLAN_RX_PIPE),
837 skb->data, MAX_RX_BUF_SIZE,
838 ath9k_hif_usb_rx_cb, skb);
839
840 /* Anchor URB */
841 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530842
843 /* Submit URB */
Sujith6335ed02010-03-29 16:07:15 +0530844 ret = usb_submit_urb(urb, GFP_KERNEL);
845 if (ret) {
846 usb_unanchor_urb(urb);
847 goto err_submit;
848 }
Sujith66b10e32010-04-06 15:28:13 +0530849
850 /*
851 * Drop reference count.
852 * This ensures that the URB is freed when killing them.
853 */
854 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530855 }
856
857 return 0;
858
Sujith6335ed02010-03-29 16:07:15 +0530859err_submit:
Ming Leif28a7b32010-04-13 00:28:53 +0800860 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530861err_skb:
862 usb_free_urb(urb);
863err_urb:
Sujithfb9987d2010-03-17 14:25:25 +0530864 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
865 return ret;
866}
867
Sujith Manoharan3deff762011-04-13 11:25:23 +0530868static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
Sujithfb9987d2010-03-17 14:25:25 +0530869{
Sujith Manoharan3deff762011-04-13 11:25:23 +0530870 usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530871}
872
Sujith Manoharan3deff762011-04-13 11:25:23 +0530873static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
Sujithfb9987d2010-03-17 14:25:25 +0530874{
Sujith Manoharan3deff762011-04-13 11:25:23 +0530875 struct urb *urb = NULL;
876 struct sk_buff *skb = NULL;
877 int i, ret;
Sujithfb9987d2010-03-17 14:25:25 +0530878
Sujith Manoharan3deff762011-04-13 11:25:23 +0530879 init_usb_anchor(&hif_dev->reg_in_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530880
Sujith Manoharan3deff762011-04-13 11:25:23 +0530881 for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
Sujithfb9987d2010-03-17 14:25:25 +0530882
Sujith Manoharan3deff762011-04-13 11:25:23 +0530883 /* Allocate URB */
884 urb = usb_alloc_urb(0, GFP_KERNEL);
885 if (urb == NULL) {
886 ret = -ENOMEM;
887 goto err_urb;
888 }
Sujithfb9987d2010-03-17 14:25:25 +0530889
Sujith Manoharan3deff762011-04-13 11:25:23 +0530890 /* Allocate buffer */
891 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
892 if (!skb) {
893 ret = -ENOMEM;
894 goto err_skb;
895 }
896
897 usb_fill_bulk_urb(urb, hif_dev->udev,
898 usb_rcvbulkpipe(hif_dev->udev,
899 USB_REG_IN_PIPE),
900 skb->data, MAX_REG_IN_BUF_SIZE,
901 ath9k_hif_usb_reg_in_cb, skb);
902
903 /* Anchor URB */
904 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
905
906 /* Submit URB */
907 ret = usb_submit_urb(urb, GFP_KERNEL);
908 if (ret) {
909 usb_unanchor_urb(urb);
910 goto err_submit;
911 }
912
913 /*
914 * Drop reference count.
915 * This ensures that the URB is freed when killing them.
916 */
917 usb_free_urb(urb);
918 }
Sujithfb9987d2010-03-17 14:25:25 +0530919
920 return 0;
921
Sujith Manoharan3deff762011-04-13 11:25:23 +0530922err_submit:
923 kfree_skb(skb);
924err_skb:
925 usb_free_urb(urb);
926err_urb:
927 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
928 return ret;
Sujithfb9987d2010-03-17 14:25:25 +0530929}
930
931static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
932{
Sujith6f0f2662010-04-06 15:28:17 +0530933 /* Register Write */
934 init_usb_anchor(&hif_dev->regout_submitted);
935
Sujithfb9987d2010-03-17 14:25:25 +0530936 /* TX */
937 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
938 goto err;
939
940 /* RX */
941 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530942 goto err_rx;
Sujithfb9987d2010-03-17 14:25:25 +0530943
Sujith6f0f2662010-04-06 15:28:17 +0530944 /* Register Read */
Sujith Manoharan3deff762011-04-13 11:25:23 +0530945 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530946 goto err_reg;
Sujithfb9987d2010-03-17 14:25:25 +0530947
948 return 0;
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530949err_reg:
950 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
951err_rx:
952 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
Sujithfb9987d2010-03-17 14:25:25 +0530953err:
954 return -ENOMEM;
955}
956
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530957static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
958{
959 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
Sujith Manoharan3deff762011-04-13 11:25:23 +0530960 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530961 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
962 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
963}
964
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530965static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
966 u32 drv_info)
Sujithfb9987d2010-03-17 14:25:25 +0530967{
968 int transfer, err;
969 const void *data = hif_dev->firmware->data;
970 size_t len = hif_dev->firmware->size;
971 u32 addr = AR9271_FIRMWARE;
972 u8 *buf = kzalloc(4096, GFP_KERNEL);
Sujithb1762862010-06-02 15:53:34 +0530973 u32 firm_offset;
Sujithfb9987d2010-03-17 14:25:25 +0530974
975 if (!buf)
976 return -ENOMEM;
977
978 while (len) {
979 transfer = min_t(int, len, 4096);
980 memcpy(buf, data, transfer);
981
982 err = usb_control_msg(hif_dev->udev,
983 usb_sndctrlpipe(hif_dev->udev, 0),
984 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
985 addr >> 8, 0, buf, transfer, HZ);
986 if (err < 0) {
987 kfree(buf);
988 return err;
989 }
990
991 len -= transfer;
992 data += transfer;
993 addr += transfer;
994 }
995 kfree(buf);
996
Sujith Manoharan0b5ead92010-12-07 16:31:38 +0530997 if (IS_AR7010_DEVICE(drv_info))
Sujithb1762862010-06-02 15:53:34 +0530998 firm_offset = AR7010_FIRMWARE_TEXT;
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530999 else
Sujithb1762862010-06-02 15:53:34 +05301000 firm_offset = AR9271_FIRMWARE_TEXT;
1001
Sujithfb9987d2010-03-17 14:25:25 +05301002 /*
1003 * Issue FW download complete command to firmware.
1004 */
1005 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
1006 FIRMWARE_DOWNLOAD_COMP,
1007 0x40 | USB_DIR_OUT,
Sujithb1762862010-06-02 15:53:34 +05301008 firm_offset >> 8, 0, NULL, 0, HZ);
Sujithfb9987d2010-03-17 14:25:25 +05301009 if (err)
1010 return -EIO;
1011
1012 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
Sujithce43cee2010-06-02 15:53:30 +05301013 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
Sujithfb9987d2010-03-17 14:25:25 +05301014
1015 return 0;
1016}
1017
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301018static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
Sujithfb9987d2010-03-17 14:25:25 +05301019{
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +05301020 int ret, idx;
1021 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
1022 struct usb_endpoint_descriptor *endp;
Sujithfb9987d2010-03-17 14:25:25 +05301023
1024 /* Request firmware */
Sujithce43cee2010-06-02 15:53:30 +05301025 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
1026 &hif_dev->udev->dev);
Sujithfb9987d2010-03-17 14:25:25 +05301027 if (ret) {
1028 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +05301029 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
Sujithfb9987d2010-03-17 14:25:25 +05301030 goto err_fw_req;
1031 }
1032
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +05301033 /* Download firmware */
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301034 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +05301035 if (ret) {
1036 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +05301037 "ath9k_htc: Firmware - %s download failed\n",
1038 hif_dev->fw_name);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +05301039 goto err_fw_download;
1040 }
1041
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +05301042 /* On downloading the firmware to the target, the USB descriptor of EP4
1043 * is 'patched' to change the type of the endpoint to Bulk. This will
1044 * bring down CPU usage during the scan period.
1045 */
1046 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
1047 endp = &alt->endpoint[idx].desc;
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +05301048 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1049 == USB_ENDPOINT_XFER_INT) {
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +05301050 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
1051 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
1052 endp->bInterval = 0;
1053 }
1054 }
1055
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +05301056 /* Alloc URBs */
1057 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1058 if (ret) {
1059 dev_err(&hif_dev->udev->dev,
1060 "ath9k_htc: Unable to allocate URBs\n");
Sujith Manoharan512c0442011-02-21 07:50:38 +05301061 goto err_fw_download;
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +05301062 }
1063
Sujithfb9987d2010-03-17 14:25:25 +05301064 return 0;
1065
Sujith Manoharancaa0a992010-12-07 16:32:02 +05301066err_fw_download:
Sujithfb9987d2010-03-17 14:25:25 +05301067 release_firmware(hif_dev->firmware);
1068err_fw_req:
1069 hif_dev->firmware = NULL;
1070 return ret;
1071}
1072
Sujithfb9987d2010-03-17 14:25:25 +05301073static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
1074{
1075 ath9k_hif_usb_dealloc_urbs(hif_dev);
1076 if (hif_dev->firmware)
1077 release_firmware(hif_dev->firmware);
1078}
1079
Sujith Manoharan36bcce42011-02-21 07:47:52 +05301080/*
1081 * An exact copy of the function from zd1211rw.
1082 */
1083static int send_eject_command(struct usb_interface *interface)
1084{
1085 struct usb_device *udev = interface_to_usbdev(interface);
1086 struct usb_host_interface *iface_desc = &interface->altsetting[0];
1087 struct usb_endpoint_descriptor *endpoint;
1088 unsigned char *cmd;
1089 u8 bulk_out_ep;
1090 int r;
1091
1092 /* Find bulk out endpoint */
1093 for (r = 1; r >= 0; r--) {
1094 endpoint = &iface_desc->endpoint[r].desc;
1095 if (usb_endpoint_dir_out(endpoint) &&
1096 usb_endpoint_xfer_bulk(endpoint)) {
1097 bulk_out_ep = endpoint->bEndpointAddress;
1098 break;
1099 }
1100 }
1101 if (r == -1) {
1102 dev_err(&udev->dev,
1103 "ath9k_htc: Could not find bulk out endpoint\n");
1104 return -ENODEV;
1105 }
1106
1107 cmd = kzalloc(31, GFP_KERNEL);
1108 if (cmd == NULL)
1109 return -ENODEV;
1110
1111 /* USB bulk command block */
1112 cmd[0] = 0x55; /* bulk command signature */
1113 cmd[1] = 0x53; /* bulk command signature */
1114 cmd[2] = 0x42; /* bulk command signature */
1115 cmd[3] = 0x43; /* bulk command signature */
1116 cmd[14] = 6; /* command length */
1117
1118 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1119 cmd[19] = 0x2; /* eject disc */
1120
1121 dev_info(&udev->dev, "Ejecting storage device...\n");
1122 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1123 cmd, 31, NULL, 2000);
1124 kfree(cmd);
1125 if (r)
1126 return r;
1127
1128 /* At this point, the device disconnects and reconnects with the real
1129 * ID numbers. */
1130
1131 usb_set_intfdata(interface, NULL);
1132 return 0;
1133}
1134
Sujithfb9987d2010-03-17 14:25:25 +05301135static int ath9k_hif_usb_probe(struct usb_interface *interface,
1136 const struct usb_device_id *id)
1137{
1138 struct usb_device *udev = interface_to_usbdev(interface);
1139 struct hif_device_usb *hif_dev;
Sujithfb9987d2010-03-17 14:25:25 +05301140 int ret = 0;
1141
Sujith Manoharan36bcce42011-02-21 07:47:52 +05301142 if (id->driver_info == STORAGE_DEVICE)
1143 return send_eject_command(interface);
1144
Sujithfb9987d2010-03-17 14:25:25 +05301145 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1146 if (!hif_dev) {
1147 ret = -ENOMEM;
1148 goto err_alloc;
1149 }
1150
1151 usb_get_dev(udev);
1152 hif_dev->udev = udev;
1153 hif_dev->interface = interface;
1154 hif_dev->device_id = id->idProduct;
1155#ifdef CONFIG_PM
1156 udev->reset_resume = 1;
1157#endif
1158 usb_set_intfdata(interface, hif_dev);
1159
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +05301160 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1161 &hif_dev->udev->dev);
1162 if (hif_dev->htc_handle == NULL) {
1163 ret = -ENOMEM;
1164 goto err_htc_hw_alloc;
1165 }
1166
Sujithce43cee2010-06-02 15:53:30 +05301167 /* Find out which firmware to load */
1168
Sujith Manoharan0b5ead92010-12-07 16:31:38 +05301169 if (IS_AR7010_DEVICE(id->driver_info))
Sujith Manoharan9efabad2011-04-13 11:22:33 +05301170 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301171 else
John W. Linvilled0ee0eb2010-06-23 14:20:45 -04001172 hif_dev->fw_name = FIRMWARE_AR9271;
Sujithce43cee2010-06-02 15:53:30 +05301173
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301174 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
Sujithfb9987d2010-03-17 14:25:25 +05301175 if (ret) {
1176 ret = -EINVAL;
1177 goto err_hif_init_usb;
1178 }
1179
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +05301180 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
Vivek Natarajan21cb9872010-08-18 19:57:49 +05301181 &hif_dev->udev->dev, hif_dev->device_id,
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301182 hif_dev->udev->product, id->driver_info);
Sujithfb9987d2010-03-17 14:25:25 +05301183 if (ret) {
1184 ret = -EINVAL;
1185 goto err_htc_hw_init;
1186 }
1187
1188 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
1189
1190 return 0;
1191
1192err_htc_hw_init:
Sujithfb9987d2010-03-17 14:25:25 +05301193 ath9k_hif_usb_dev_deinit(hif_dev);
1194err_hif_init_usb:
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +05301195 ath9k_htc_hw_free(hif_dev->htc_handle);
1196err_htc_hw_alloc:
Sujithfb9987d2010-03-17 14:25:25 +05301197 usb_set_intfdata(interface, NULL);
1198 kfree(hif_dev);
1199 usb_put_dev(udev);
1200err_alloc:
1201 return ret;
1202}
1203
Sujith62e47162010-04-23 10:28:16 +05301204static void ath9k_hif_usb_reboot(struct usb_device *udev)
1205{
1206 u32 reboot_cmd = 0xffffffff;
1207 void *buf;
1208 int ret;
1209
Julia Lawalla465a2c2010-05-15 23:17:19 +02001210 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
Sujith62e47162010-04-23 10:28:16 +05301211 if (!buf)
1212 return;
1213
Sujith62e47162010-04-23 10:28:16 +05301214 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1215 buf, 4, NULL, HZ);
1216 if (ret)
1217 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1218
1219 kfree(buf);
1220}
1221
Sujithfb9987d2010-03-17 14:25:25 +05301222static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1223{
1224 struct usb_device *udev = interface_to_usbdev(interface);
Joe Perchesb2767362010-11-30 13:42:08 -08001225 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Sujith Manoharan97dcec52010-12-20 08:02:42 +05301226 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
Sujithfb9987d2010-03-17 14:25:25 +05301227
Sujith Manoharan36bcce42011-02-21 07:47:52 +05301228 if (!hif_dev)
1229 return;
1230
1231 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1232 ath9k_htc_hw_free(hif_dev->htc_handle);
1233 ath9k_hif_usb_dev_deinit(hif_dev);
1234 usb_set_intfdata(interface, NULL);
Sujithfb9987d2010-03-17 14:25:25 +05301235
Sujith Manoharan97dcec52010-12-20 08:02:42 +05301236 if (!unplugged && (hif_dev->flags & HIF_USB_START))
Sujith62e47162010-04-23 10:28:16 +05301237 ath9k_hif_usb_reboot(udev);
Sujithfb9987d2010-03-17 14:25:25 +05301238
1239 kfree(hif_dev);
1240 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1241 usb_put_dev(udev);
1242}
1243
1244#ifdef CONFIG_PM
1245static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1246 pm_message_t message)
1247{
Joe Perchesb2767362010-11-30 13:42:08 -08001248 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Sujithfb9987d2010-03-17 14:25:25 +05301249
Sujith Manoharanf933ebe2010-12-01 12:30:27 +05301250 /*
1251 * The device has to be set to FULLSLEEP mode in case no
1252 * interface is up.
1253 */
1254 if (!(hif_dev->flags & HIF_USB_START))
1255 ath9k_htc_suspend(hif_dev->htc_handle);
1256
Sujithfb9987d2010-03-17 14:25:25 +05301257 ath9k_hif_usb_dealloc_urbs(hif_dev);
1258
1259 return 0;
1260}
1261
1262static int ath9k_hif_usb_resume(struct usb_interface *interface)
1263{
Joe Perchesb2767362010-11-30 13:42:08 -08001264 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301265 struct htc_target *htc_handle = hif_dev->htc_handle;
Sujithfb9987d2010-03-17 14:25:25 +05301266 int ret;
1267
1268 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1269 if (ret)
1270 return ret;
1271
1272 if (hif_dev->firmware) {
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301273 ret = ath9k_hif_usb_download_fw(hif_dev,
Sujith Manoharan0b5ead92010-12-07 16:31:38 +05301274 htc_handle->drv_priv->ah->hw_version.usbdev);
Sujithfb9987d2010-03-17 14:25:25 +05301275 if (ret)
1276 goto fail_resume;
1277 } else {
1278 ath9k_hif_usb_dealloc_urbs(hif_dev);
1279 return -EIO;
1280 }
1281
1282 mdelay(100);
1283
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301284 ret = ath9k_htc_resume(htc_handle);
Sujithfb9987d2010-03-17 14:25:25 +05301285
1286 if (ret)
1287 goto fail_resume;
1288
1289 return 0;
1290
1291fail_resume:
1292 ath9k_hif_usb_dealloc_urbs(hif_dev);
1293
1294 return ret;
1295}
1296#endif
1297
1298static struct usb_driver ath9k_hif_usb_driver = {
1299 .name = "ath9k_hif_usb",
1300 .probe = ath9k_hif_usb_probe,
1301 .disconnect = ath9k_hif_usb_disconnect,
1302#ifdef CONFIG_PM
1303 .suspend = ath9k_hif_usb_suspend,
1304 .resume = ath9k_hif_usb_resume,
1305 .reset_resume = ath9k_hif_usb_resume,
1306#endif
1307 .id_table = ath9k_hif_usb_ids,
1308 .soft_unbind = 1,
1309};
1310
1311int ath9k_hif_usb_init(void)
1312{
1313 return usb_register(&ath9k_hif_usb_driver);
1314}
1315
1316void ath9k_hif_usb_exit(void)
1317{
1318 usb_deregister(&ath9k_hif_usb_driver);
1319}