blob: 8028fe90f666f7f77e7dac72f4d754cbaa7c90ee [file] [log] [blame]
Sujithfb9987d2010-03-17 14:25:25 +05301/*
Sujith Manoharan5b681382011-05-17 13:36:18 +05302 * Copyright (c) 2010-2011 Atheros Communications Inc.
Sujithfb9987d2010-03-17 14:25:25 +05303 *
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 */
Mohammed Shafi Shajakhan5cf6fa72011-06-28 20:13:41 +053052 { USB_DEVICE(0x0411, 0x017f),
53 .driver_info = AR9280_USB }, /* Sony UWA-BR100 */
Sujith Manoharan0b5ead92010-12-07 16:31:38 +053054
Sujith Manoharan36bcce42011-02-21 07:47:52 +053055 { USB_DEVICE(0x0cf3, 0x20ff),
56 .driver_info = STORAGE_DEVICE },
57
Sujithfb9987d2010-03-17 14:25:25 +053058 { },
59};
60
61MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
62
63static int __hif_usb_tx(struct hif_device_usb *hif_dev);
64
65static void hif_usb_regout_cb(struct urb *urb)
66{
67 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
Sujithfb9987d2010-03-17 14:25:25 +053068
69 switch (urb->status) {
70 case 0:
71 break;
72 case -ENOENT:
73 case -ECONNRESET:
Sujithfb9987d2010-03-17 14:25:25 +053074 case -ENODEV:
75 case -ESHUTDOWN:
Sujith6f0f2662010-04-06 15:28:17 +053076 goto free;
Sujithfb9987d2010-03-17 14:25:25 +053077 default:
78 break;
79 }
80
81 if (cmd) {
82 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
Sujith Manoharan2f801942011-04-13 11:26:46 +053083 cmd->skb, true);
Sujithfb9987d2010-03-17 14:25:25 +053084 kfree(cmd);
Sujithfb9987d2010-03-17 14:25:25 +053085 }
Sujith6f0f2662010-04-06 15:28:17 +053086
87 return;
88free:
Ming Lei0fa35a52010-04-13 00:29:15 +080089 kfree_skb(cmd->skb);
Sujith6f0f2662010-04-06 15:28:17 +053090 kfree(cmd);
Sujithfb9987d2010-03-17 14:25:25 +053091}
92
93static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
94 struct sk_buff *skb)
95{
96 struct urb *urb;
97 struct cmd_buf *cmd;
98 int ret = 0;
99
100 urb = usb_alloc_urb(0, GFP_KERNEL);
101 if (urb == NULL)
102 return -ENOMEM;
103
104 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
105 if (cmd == NULL) {
106 usb_free_urb(urb);
107 return -ENOMEM;
108 }
109
110 cmd->skb = skb;
111 cmd->hif_dev = hif_dev;
112
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530113 usb_fill_bulk_urb(urb, hif_dev->udev,
114 usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530115 skb->data, skb->len,
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +0530116 hif_usb_regout_cb, cmd);
Sujithfb9987d2010-03-17 14:25:25 +0530117
Sujith6f0f2662010-04-06 15:28:17 +0530118 usb_anchor_urb(urb, &hif_dev->regout_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530119 ret = usb_submit_urb(urb, GFP_KERNEL);
120 if (ret) {
Sujith6f0f2662010-04-06 15:28:17 +0530121 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530122 kfree(cmd);
123 }
Sujith6f0f2662010-04-06 15:28:17 +0530124 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530125
126 return ret;
127}
128
Sujith Manoharan2f801942011-04-13 11:26:46 +0530129static void hif_usb_mgmt_cb(struct urb *urb)
130{
131 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
132 struct hif_device_usb *hif_dev = cmd->hif_dev;
133 bool txok = true;
134
135 if (!cmd || !cmd->skb || !cmd->hif_dev)
136 return;
137
138 switch (urb->status) {
139 case 0:
140 break;
141 case -ENOENT:
142 case -ECONNRESET:
143 case -ENODEV:
144 case -ESHUTDOWN:
145 txok = false;
146
147 /*
148 * If the URBs are being flushed, no need to complete
149 * this packet.
150 */
151 spin_lock(&hif_dev->tx.tx_lock);
152 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
153 spin_unlock(&hif_dev->tx.tx_lock);
154 dev_kfree_skb_any(cmd->skb);
155 kfree(cmd);
156 return;
157 }
158 spin_unlock(&hif_dev->tx.tx_lock);
159
160 break;
161 default:
162 txok = false;
163 break;
164 }
165
166 skb_pull(cmd->skb, 4);
167 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
168 cmd->skb, txok);
169 kfree(cmd);
170}
171
172static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev,
173 struct sk_buff *skb)
174{
175 struct urb *urb;
176 struct cmd_buf *cmd;
177 int ret = 0;
178 __le16 *hdr;
179
180 urb = usb_alloc_urb(0, GFP_ATOMIC);
181 if (urb == NULL)
182 return -ENOMEM;
183
184 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
185 if (cmd == NULL) {
186 usb_free_urb(urb);
187 return -ENOMEM;
188 }
189
190 cmd->skb = skb;
191 cmd->hif_dev = hif_dev;
192
193 hdr = (__le16 *) skb_push(skb, 4);
194 *hdr++ = cpu_to_le16(skb->len - 4);
195 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
196
197 usb_fill_bulk_urb(urb, hif_dev->udev,
198 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
199 skb->data, skb->len,
200 hif_usb_mgmt_cb, cmd);
201
202 usb_anchor_urb(urb, &hif_dev->mgmt_submitted);
203 ret = usb_submit_urb(urb, GFP_ATOMIC);
204 if (ret) {
205 usb_unanchor_urb(urb);
206 kfree(cmd);
207 }
208 usb_free_urb(urb);
209
210 return ret;
211}
212
Sujitheac8e382010-04-16 11:54:00 +0530213static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
214 struct sk_buff_head *list)
Ming Leif8e1d082010-04-13 00:29:27 +0800215{
216 struct sk_buff *skb;
Sujitheac8e382010-04-16 11:54:00 +0530217
218 while ((skb = __skb_dequeue(list)) != NULL) {
Ming Leif8e1d082010-04-13 00:29:27 +0800219 dev_kfree_skb_any(skb);
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530220 }
221}
222
223static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
224 struct sk_buff_head *queue,
225 bool txok)
226{
227 struct sk_buff *skb;
228
229 while ((skb = __skb_dequeue(queue)) != NULL) {
230 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
231 skb, txok);
John W. Linvilledfa8fc62011-04-14 10:38:22 -0400232 if (txok)
233 TX_STAT_INC(skb_success);
234 else
235 TX_STAT_INC(skb_failed);
Sujitheac8e382010-04-16 11:54:00 +0530236 }
Ming Leif8e1d082010-04-13 00:29:27 +0800237}
238
Sujithc11d8f82010-04-23 10:28:09 +0530239static void hif_usb_tx_cb(struct urb *urb)
240{
241 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
Dan Carpenter690e7812010-05-14 16:50:56 +0200242 struct hif_device_usb *hif_dev;
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530243 bool txok = true;
Sujithc11d8f82010-04-23 10:28:09 +0530244
Dan Carpenter690e7812010-05-14 16:50:56 +0200245 if (!tx_buf || !tx_buf->hif_dev)
Sujithc11d8f82010-04-23 10:28:09 +0530246 return;
247
Dan Carpenter690e7812010-05-14 16:50:56 +0200248 hif_dev = tx_buf->hif_dev;
249
Sujithc11d8f82010-04-23 10:28:09 +0530250 switch (urb->status) {
251 case 0:
252 break;
253 case -ENOENT:
254 case -ECONNRESET:
255 case -ENODEV:
256 case -ESHUTDOWN:
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530257 txok = false;
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530258
259 /*
260 * If the URBs are being flushed, no need to add this
261 * URB to the free list.
262 */
263 spin_lock(&hif_dev->tx.tx_lock);
264 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
265 spin_unlock(&hif_dev->tx.tx_lock);
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530266 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530267 return;
268 }
269 spin_unlock(&hif_dev->tx.tx_lock);
270
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530271 break;
Sujithc11d8f82010-04-23 10:28:09 +0530272 default:
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530273 txok = false;
Sujithc11d8f82010-04-23 10:28:09 +0530274 break;
275 }
276
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530277 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
Sujithc11d8f82010-04-23 10:28:09 +0530278
Sujithc11d8f82010-04-23 10:28:09 +0530279 /* Re-initialize the SKB queue */
280 tx_buf->len = tx_buf->offset = 0;
281 __skb_queue_head_init(&tx_buf->skb_queue);
282
283 /* Add this TX buffer to the free list */
284 spin_lock(&hif_dev->tx.tx_lock);
285 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
286 hif_dev->tx.tx_buf_cnt++;
287 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
288 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
289 TX_STAT_INC(buf_completed);
290 spin_unlock(&hif_dev->tx.tx_lock);
291}
292
Sujithfb9987d2010-03-17 14:25:25 +0530293/* TX lock has to be taken */
294static int __hif_usb_tx(struct hif_device_usb *hif_dev)
295{
296 struct tx_buf *tx_buf = NULL;
297 struct sk_buff *nskb = NULL;
298 int ret = 0, i;
Sujith Manoharan2c273922011-02-27 09:23:52 +0530299 u16 tx_skb_cnt = 0;
Sujithfb9987d2010-03-17 14:25:25 +0530300 u8 *buf;
Sujith Manoharan2c273922011-02-27 09:23:52 +0530301 __le16 *hdr;
Sujithfb9987d2010-03-17 14:25:25 +0530302
303 if (hif_dev->tx.tx_skb_cnt == 0)
304 return 0;
305
306 /* Check if a free TX buffer is available */
307 if (list_empty(&hif_dev->tx.tx_buf))
308 return 0;
309
310 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
Sujithc11d8f82010-04-23 10:28:09 +0530311 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
Sujithfb9987d2010-03-17 14:25:25 +0530312 hif_dev->tx.tx_buf_cnt--;
313
314 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
315
316 for (i = 0; i < tx_skb_cnt; i++) {
317 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
318
319 /* Should never be NULL */
320 BUG_ON(!nskb);
321
322 hif_dev->tx.tx_skb_cnt--;
323
324 buf = tx_buf->buf;
325 buf += tx_buf->offset;
Sujith Manoharan2c273922011-02-27 09:23:52 +0530326 hdr = (__le16 *)buf;
327 *hdr++ = cpu_to_le16(nskb->len);
328 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
Sujithfb9987d2010-03-17 14:25:25 +0530329 buf += 4;
330 memcpy(buf, nskb->data, nskb->len);
331 tx_buf->len = nskb->len + 4;
332
333 if (i < (tx_skb_cnt - 1))
334 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
335
336 if (i == (tx_skb_cnt - 1))
337 tx_buf->len += tx_buf->offset;
338
339 __skb_queue_tail(&tx_buf->skb_queue, nskb);
340 TX_STAT_INC(skb_queued);
341 }
342
343 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
344 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
345 tx_buf->buf, tx_buf->len,
346 hif_usb_tx_cb, tx_buf);
347
348 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
349 if (ret) {
350 tx_buf->len = tx_buf->offset = 0;
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530351 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
Sujithfb9987d2010-03-17 14:25:25 +0530352 __skb_queue_head_init(&tx_buf->skb_queue);
353 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
354 hif_dev->tx.tx_buf_cnt++;
355 }
356
357 if (!ret)
358 TX_STAT_INC(buf_queued);
359
360 return ret;
361}
362
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530363static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
Sujithfb9987d2010-03-17 14:25:25 +0530364{
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530365 struct ath9k_htc_tx_ctl *tx_ctl;
Sujithfb9987d2010-03-17 14:25:25 +0530366 unsigned long flags;
Sujith Manoharan2f801942011-04-13 11:26:46 +0530367 int ret = 0;
Sujithfb9987d2010-03-17 14:25:25 +0530368
369 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
370
371 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
372 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
373 return -ENODEV;
374 }
375
376 /* Check if the max queue count has been reached */
377 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
378 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
379 return -ENOMEM;
380 }
381
Sujith Manoharan2f801942011-04-13 11:26:46 +0530382 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
Sujithfb9987d2010-03-17 14:25:25 +0530383
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530384 tx_ctl = HTC_SKB_CB(skb);
385
Sujith Manoharan2f801942011-04-13 11:26:46 +0530386 /* Mgmt/Beacon frames don't use the TX buffer pool */
387 if ((tx_ctl->type == ATH9K_HTC_MGMT) ||
388 (tx_ctl->type == ATH9K_HTC_BEACON)) {
389 ret = hif_usb_send_mgmt(hif_dev, skb);
390 }
391
392 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
393
394 if ((tx_ctl->type == ATH9K_HTC_NORMAL) ||
395 (tx_ctl->type == ATH9K_HTC_AMPDU)) {
396 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
397 hif_dev->tx.tx_skb_cnt++;
398 }
Sujithfb9987d2010-03-17 14:25:25 +0530399
400 /* Check if AMPDUs have to be sent immediately */
Sujith Manoharan2f801942011-04-13 11:26:46 +0530401 if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
Sujithfb9987d2010-03-17 14:25:25 +0530402 (hif_dev->tx.tx_skb_cnt < 2)) {
403 __hif_usb_tx(hif_dev);
404 }
405
406 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
407
Sujith Manoharan2f801942011-04-13 11:26:46 +0530408 return ret;
Sujithfb9987d2010-03-17 14:25:25 +0530409}
410
Sujith Manoharane1fe7c32011-04-13 11:26:06 +0530411static void hif_usb_start(void *hif_handle)
Sujithfb9987d2010-03-17 14:25:25 +0530412{
413 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
414 unsigned long flags;
415
416 hif_dev->flags |= HIF_USB_START;
417
418 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
419 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
420 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
421}
422
Sujith Manoharane1fe7c32011-04-13 11:26:06 +0530423static void hif_usb_stop(void *hif_handle)
Sujithfb9987d2010-03-17 14:25:25 +0530424{
425 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530426 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530427 unsigned long flags;
428
429 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
Sujith Manoharanb587fc82011-04-13 11:25:59 +0530430 ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
Sujithfb9987d2010-03-17 14:25:25 +0530431 hif_dev->tx.tx_skb_cnt = 0;
432 hif_dev->tx.flags |= HIF_USB_TX_STOP;
433 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530434
435 /* The pending URBs have to be canceled. */
436 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
437 &hif_dev->tx.tx_pending, list) {
438 usb_kill_urb(tx_buf->urb);
439 }
Sujith Manoharan2f801942011-04-13 11:26:46 +0530440
441 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530442}
443
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530444static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
Sujithfb9987d2010-03-17 14:25:25 +0530445{
446 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
447 int ret = 0;
448
449 switch (pipe_id) {
450 case USB_WLAN_TX_PIPE:
Sujith Manoharan40dc9e42011-04-13 11:24:31 +0530451 ret = hif_usb_send_tx(hif_dev, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530452 break;
453 case USB_REG_OUT_PIPE:
454 ret = hif_usb_send_regout(hif_dev, skb);
455 break;
456 default:
Sujith6335ed02010-03-29 16:07:15 +0530457 dev_err(&hif_dev->udev->dev,
458 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
Sujithfb9987d2010-03-17 14:25:25 +0530459 ret = -EINVAL;
460 break;
461 }
462
463 return ret;
464}
465
Sujith Manoharan84c9e1642011-04-13 11:26:11 +0530466static inline bool check_index(struct sk_buff *skb, u8 idx)
467{
468 struct ath9k_htc_tx_ctl *tx_ctl;
469
470 tx_ctl = HTC_SKB_CB(skb);
471
472 if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
473 (tx_ctl->sta_idx == idx))
474 return true;
475
476 return false;
477}
478
479static void hif_usb_sta_drain(void *hif_handle, u8 idx)
480{
481 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
482 struct sk_buff *skb, *tmp;
483 unsigned long flags;
484
485 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
486
487 skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
488 if (check_index(skb, idx)) {
489 __skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
490 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
491 skb, false);
492 hif_dev->tx.tx_skb_cnt--;
493 TX_STAT_INC(skb_failed);
494 }
495 }
496
497 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
498}
499
Sujithfb9987d2010-03-17 14:25:25 +0530500static struct ath9k_htc_hif hif_usb = {
501 .transport = ATH9K_HIF_USB,
502 .name = "ath9k_hif_usb",
503
504 .control_ul_pipe = USB_REG_OUT_PIPE,
505 .control_dl_pipe = USB_REG_IN_PIPE,
506
507 .start = hif_usb_start,
508 .stop = hif_usb_stop,
Sujith Manoharan84c9e1642011-04-13 11:26:11 +0530509 .sta_drain = hif_usb_sta_drain,
Sujithfb9987d2010-03-17 14:25:25 +0530510 .send = hif_usb_send,
511};
512
513static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
514 struct sk_buff *skb)
515{
Sujithc5032692010-04-06 15:28:15 +0530516 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
Joe Perches44b23b42010-11-30 12:19:11 -0800517 int index = 0, i = 0, len = skb->len;
518 int rx_remain_len, rx_pkt_len;
519 u16 pool_index = 0;
Sujithfb9987d2010-03-17 14:25:25 +0530520 u8 *ptr;
521
Sujith46baa1a2010-04-06 15:28:11 +0530522 spin_lock(&hif_dev->rx_lock);
523
Sujithfb9987d2010-03-17 14:25:25 +0530524 rx_remain_len = hif_dev->rx_remain_len;
525 rx_pkt_len = hif_dev->rx_transfer_len;
526
527 if (rx_remain_len != 0) {
528 struct sk_buff *remain_skb = hif_dev->remain_skb;
529
530 if (remain_skb) {
531 ptr = (u8 *) remain_skb->data;
532
533 index = rx_remain_len;
534 rx_remain_len -= hif_dev->rx_pad_len;
535 ptr += rx_pkt_len;
536
537 memcpy(ptr, skb->data, rx_remain_len);
538
539 rx_pkt_len += rx_remain_len;
540 hif_dev->rx_remain_len = 0;
541 skb_put(remain_skb, rx_pkt_len);
542
543 skb_pool[pool_index++] = remain_skb;
544
545 } else {
546 index = rx_remain_len;
547 }
548 }
549
Sujith46baa1a2010-04-06 15:28:11 +0530550 spin_unlock(&hif_dev->rx_lock);
551
Sujithfb9987d2010-03-17 14:25:25 +0530552 while (index < len) {
Joe Perches44b23b42010-11-30 12:19:11 -0800553 u16 pkt_len;
554 u16 pkt_tag;
555 u16 pad_len;
556 int chk_idx;
557
Sujithfb9987d2010-03-17 14:25:25 +0530558 ptr = (u8 *) skb->data;
559
560 pkt_len = ptr[index] + (ptr[index+1] << 8);
561 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
562
Joe Perches44b23b42010-11-30 12:19:11 -0800563 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
Sujithfb9987d2010-03-17 14:25:25 +0530564 RX_STAT_INC(skb_dropped);
Sujithfb9987d2010-03-17 14:25:25 +0530565 return;
566 }
Joe Perches44b23b42010-11-30 12:19:11 -0800567
568 pad_len = 4 - (pkt_len & 0x3);
569 if (pad_len == 4)
570 pad_len = 0;
571
572 chk_idx = index;
573 index = index + 4 + pkt_len + pad_len;
574
575 if (index > MAX_RX_BUF_SIZE) {
576 spin_lock(&hif_dev->rx_lock);
577 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
578 hif_dev->rx_transfer_len =
579 MAX_RX_BUF_SIZE - chk_idx - 4;
580 hif_dev->rx_pad_len = pad_len;
581
582 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
583 if (!nskb) {
584 dev_err(&hif_dev->udev->dev,
585 "ath9k_htc: RX memory allocation error\n");
586 spin_unlock(&hif_dev->rx_lock);
587 goto err;
588 }
589 skb_reserve(nskb, 32);
590 RX_STAT_INC(skb_allocated);
591
592 memcpy(nskb->data, &(skb->data[chk_idx+4]),
593 hif_dev->rx_transfer_len);
594
595 /* Record the buffer pointer */
596 hif_dev->remain_skb = nskb;
597 spin_unlock(&hif_dev->rx_lock);
598 } else {
599 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
600 if (!nskb) {
601 dev_err(&hif_dev->udev->dev,
602 "ath9k_htc: RX memory allocation error\n");
603 goto err;
604 }
605 skb_reserve(nskb, 32);
606 RX_STAT_INC(skb_allocated);
607
608 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
609 skb_put(nskb, pkt_len);
610 skb_pool[pool_index++] = nskb;
611 }
Sujithfb9987d2010-03-17 14:25:25 +0530612 }
613
614err:
Sujithfb9987d2010-03-17 14:25:25 +0530615 for (i = 0; i < pool_index; i++) {
616 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
617 skb_pool[i]->len, USB_WLAN_RX_PIPE);
618 RX_STAT_INC(skb_completed);
619 }
620}
621
622static void ath9k_hif_usb_rx_cb(struct urb *urb)
623{
624 struct sk_buff *skb = (struct sk_buff *) urb->context;
Joe Perchesb2767362010-11-30 13:42:08 -0800625 struct hif_device_usb *hif_dev =
Sujithfb9987d2010-03-17 14:25:25 +0530626 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
627 int ret;
628
Sujith6335ed02010-03-29 16:07:15 +0530629 if (!skb)
630 return;
631
Sujithfb9987d2010-03-17 14:25:25 +0530632 if (!hif_dev)
633 goto free;
634
635 switch (urb->status) {
636 case 0:
637 break;
638 case -ENOENT:
639 case -ECONNRESET:
640 case -ENODEV:
641 case -ESHUTDOWN:
642 goto free;
643 default:
644 goto resubmit;
645 }
646
647 if (likely(urb->actual_length != 0)) {
648 skb_put(skb, urb->actual_length);
Sujithfb9987d2010-03-17 14:25:25 +0530649 ath9k_hif_usb_rx_stream(hif_dev, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530650 }
651
652resubmit:
653 skb_reset_tail_pointer(skb);
654 skb_trim(skb, 0);
655
Sujith6335ed02010-03-29 16:07:15 +0530656 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530657 ret = usb_submit_urb(urb, GFP_ATOMIC);
Sujith6335ed02010-03-29 16:07:15 +0530658 if (ret) {
659 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530660 goto free;
Sujith6335ed02010-03-29 16:07:15 +0530661 }
Sujithfb9987d2010-03-17 14:25:25 +0530662
663 return;
664free:
Ming Leif28a7b32010-04-13 00:28:53 +0800665 kfree_skb(skb);
Sujithfb9987d2010-03-17 14:25:25 +0530666}
667
668static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
669{
670 struct sk_buff *skb = (struct sk_buff *) urb->context;
671 struct sk_buff *nskb;
Joe Perchesb2767362010-11-30 13:42:08 -0800672 struct hif_device_usb *hif_dev =
Sujithfb9987d2010-03-17 14:25:25 +0530673 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
674 int ret;
675
Sujith6335ed02010-03-29 16:07:15 +0530676 if (!skb)
677 return;
678
Sujithfb9987d2010-03-17 14:25:25 +0530679 if (!hif_dev)
680 goto free;
681
682 switch (urb->status) {
683 case 0:
684 break;
685 case -ENOENT:
686 case -ECONNRESET:
687 case -ENODEV:
688 case -ESHUTDOWN:
689 goto free;
690 default:
Sujith Manoharan3deff762011-04-13 11:25:23 +0530691 skb_reset_tail_pointer(skb);
692 skb_trim(skb, 0);
693
Sujithfb9987d2010-03-17 14:25:25 +0530694 goto resubmit;
695 }
696
697 if (likely(urb->actual_length != 0)) {
698 skb_put(skb, urb->actual_length);
699
Sujith5ab0af32010-04-23 10:28:17 +0530700 /* Process the command first */
701 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
702 skb->len, USB_REG_IN_PIPE);
703
704
Ming Leie6c6d332010-04-13 00:29:05 +0800705 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
Sujith5ab0af32010-04-23 10:28:17 +0530706 if (!nskb) {
707 dev_err(&hif_dev->udev->dev,
708 "ath9k_htc: REG_IN memory allocation failure\n");
709 urb->context = NULL;
710 return;
711 }
Sujithfb9987d2010-03-17 14:25:25 +0530712
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530713 usb_fill_bulk_urb(urb, hif_dev->udev,
Rajkumar Manoharan0f529e92010-09-16 19:56:55 +0530714 usb_rcvbulkpipe(hif_dev->udev,
715 USB_REG_IN_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530716 nskb->data, MAX_REG_IN_BUF_SIZE,
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530717 ath9k_hif_usb_reg_in_cb, nskb);
Sujithfb9987d2010-03-17 14:25:25 +0530718 }
719
720resubmit:
Sujith Manoharan3deff762011-04-13 11:25:23 +0530721 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530722 ret = usb_submit_urb(urb, GFP_ATOMIC);
Sujith Manoharan3deff762011-04-13 11:25:23 +0530723 if (ret) {
724 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530725 goto free;
Sujith Manoharan3deff762011-04-13 11:25:23 +0530726 }
Sujithfb9987d2010-03-17 14:25:25 +0530727
728 return;
729free:
Ming Leie6c6d332010-04-13 00:29:05 +0800730 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530731 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530732}
733
734static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
735{
Sujithfb9987d2010-03-17 14:25:25 +0530736 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530737 unsigned long flags;
Sujithfb9987d2010-03-17 14:25:25 +0530738
Sujithc11d8f82010-04-23 10:28:09 +0530739 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
740 &hif_dev->tx.tx_buf, list) {
741 usb_kill_urb(tx_buf->urb);
Sujithfb9987d2010-03-17 14:25:25 +0530742 list_del(&tx_buf->list);
743 usb_free_urb(tx_buf->urb);
744 kfree(tx_buf->buf);
745 kfree(tx_buf);
746 }
747
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530748 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
749 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
750 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
751
Sujithfb9987d2010-03-17 14:25:25 +0530752 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
753 &hif_dev->tx.tx_pending, list) {
754 usb_kill_urb(tx_buf->urb);
755 list_del(&tx_buf->list);
756 usb_free_urb(tx_buf->urb);
757 kfree(tx_buf->buf);
758 kfree(tx_buf);
759 }
Sujith Manoharan2f801942011-04-13 11:26:46 +0530760
761 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530762}
763
764static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
765{
766 struct tx_buf *tx_buf;
767 int i;
768
769 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
770 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
771 spin_lock_init(&hif_dev->tx.tx_lock);
772 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
Sujith Manoharan2f801942011-04-13 11:26:46 +0530773 init_usb_anchor(&hif_dev->mgmt_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530774
775 for (i = 0; i < MAX_TX_URB_NUM; i++) {
776 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
777 if (!tx_buf)
778 goto err;
779
780 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
781 if (!tx_buf->buf)
782 goto err;
783
784 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
785 if (!tx_buf->urb)
786 goto err;
787
788 tx_buf->hif_dev = hif_dev;
789 __skb_queue_head_init(&tx_buf->skb_queue);
790
791 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
792 }
793
794 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
795
796 return 0;
797err:
Dan Carpenter76066882010-05-14 16:52:37 +0200798 if (tx_buf) {
799 kfree(tx_buf->buf);
800 kfree(tx_buf);
801 }
Sujithfb9987d2010-03-17 14:25:25 +0530802 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
803 return -ENOMEM;
804}
805
Sujithfb9987d2010-03-17 14:25:25 +0530806static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
807{
Sujith6335ed02010-03-29 16:07:15 +0530808 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530809}
810
811static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
812{
Sujith6335ed02010-03-29 16:07:15 +0530813 struct urb *urb = NULL;
814 struct sk_buff *skb = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530815 int i, ret;
816
Sujith6335ed02010-03-29 16:07:15 +0530817 init_usb_anchor(&hif_dev->rx_submitted);
Sujith46baa1a2010-04-06 15:28:11 +0530818 spin_lock_init(&hif_dev->rx_lock);
Sujith6335ed02010-03-29 16:07:15 +0530819
Sujithfb9987d2010-03-17 14:25:25 +0530820 for (i = 0; i < MAX_RX_URB_NUM; i++) {
821
822 /* Allocate URB */
Sujith6335ed02010-03-29 16:07:15 +0530823 urb = usb_alloc_urb(0, GFP_KERNEL);
824 if (urb == NULL) {
Sujithfb9987d2010-03-17 14:25:25 +0530825 ret = -ENOMEM;
Sujith6335ed02010-03-29 16:07:15 +0530826 goto err_urb;
Sujithfb9987d2010-03-17 14:25:25 +0530827 }
828
829 /* Allocate buffer */
Ming Leif28a7b32010-04-13 00:28:53 +0800830 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
Sujith6335ed02010-03-29 16:07:15 +0530831 if (!skb) {
832 ret = -ENOMEM;
833 goto err_skb;
834 }
835
836 usb_fill_bulk_urb(urb, hif_dev->udev,
837 usb_rcvbulkpipe(hif_dev->udev,
838 USB_WLAN_RX_PIPE),
839 skb->data, MAX_RX_BUF_SIZE,
840 ath9k_hif_usb_rx_cb, skb);
841
842 /* Anchor URB */
843 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530844
845 /* Submit URB */
Sujith6335ed02010-03-29 16:07:15 +0530846 ret = usb_submit_urb(urb, GFP_KERNEL);
847 if (ret) {
848 usb_unanchor_urb(urb);
849 goto err_submit;
850 }
Sujith66b10e32010-04-06 15:28:13 +0530851
852 /*
853 * Drop reference count.
854 * This ensures that the URB is freed when killing them.
855 */
856 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530857 }
858
859 return 0;
860
Sujith6335ed02010-03-29 16:07:15 +0530861err_submit:
Ming Leif28a7b32010-04-13 00:28:53 +0800862 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530863err_skb:
864 usb_free_urb(urb);
865err_urb:
Sujithfb9987d2010-03-17 14:25:25 +0530866 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
867 return ret;
868}
869
Sujith Manoharan3deff762011-04-13 11:25:23 +0530870static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
Sujithfb9987d2010-03-17 14:25:25 +0530871{
Sujith Manoharan3deff762011-04-13 11:25:23 +0530872 usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530873}
874
Sujith Manoharan3deff762011-04-13 11:25:23 +0530875static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
Sujithfb9987d2010-03-17 14:25:25 +0530876{
Sujith Manoharan3deff762011-04-13 11:25:23 +0530877 struct urb *urb = NULL;
878 struct sk_buff *skb = NULL;
879 int i, ret;
Sujithfb9987d2010-03-17 14:25:25 +0530880
Sujith Manoharan3deff762011-04-13 11:25:23 +0530881 init_usb_anchor(&hif_dev->reg_in_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530882
Sujith Manoharan3deff762011-04-13 11:25:23 +0530883 for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
Sujithfb9987d2010-03-17 14:25:25 +0530884
Sujith Manoharan3deff762011-04-13 11:25:23 +0530885 /* Allocate URB */
886 urb = usb_alloc_urb(0, GFP_KERNEL);
887 if (urb == NULL) {
888 ret = -ENOMEM;
889 goto err_urb;
890 }
Sujithfb9987d2010-03-17 14:25:25 +0530891
Sujith Manoharan3deff762011-04-13 11:25:23 +0530892 /* Allocate buffer */
893 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
894 if (!skb) {
895 ret = -ENOMEM;
896 goto err_skb;
897 }
898
899 usb_fill_bulk_urb(urb, hif_dev->udev,
900 usb_rcvbulkpipe(hif_dev->udev,
901 USB_REG_IN_PIPE),
902 skb->data, MAX_REG_IN_BUF_SIZE,
903 ath9k_hif_usb_reg_in_cb, skb);
904
905 /* Anchor URB */
906 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
907
908 /* Submit URB */
909 ret = usb_submit_urb(urb, GFP_KERNEL);
910 if (ret) {
911 usb_unanchor_urb(urb);
912 goto err_submit;
913 }
914
915 /*
916 * Drop reference count.
917 * This ensures that the URB is freed when killing them.
918 */
919 usb_free_urb(urb);
920 }
Sujithfb9987d2010-03-17 14:25:25 +0530921
922 return 0;
923
Sujith Manoharan3deff762011-04-13 11:25:23 +0530924err_submit:
925 kfree_skb(skb);
926err_skb:
927 usb_free_urb(urb);
928err_urb:
929 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
930 return ret;
Sujithfb9987d2010-03-17 14:25:25 +0530931}
932
933static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
934{
Sujith6f0f2662010-04-06 15:28:17 +0530935 /* Register Write */
936 init_usb_anchor(&hif_dev->regout_submitted);
937
Sujithfb9987d2010-03-17 14:25:25 +0530938 /* TX */
939 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
940 goto err;
941
942 /* RX */
943 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530944 goto err_rx;
Sujithfb9987d2010-03-17 14:25:25 +0530945
Sujith6f0f2662010-04-06 15:28:17 +0530946 /* Register Read */
Sujith Manoharan3deff762011-04-13 11:25:23 +0530947 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530948 goto err_reg;
Sujithfb9987d2010-03-17 14:25:25 +0530949
950 return 0;
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530951err_reg:
952 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
953err_rx:
954 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
Sujithfb9987d2010-03-17 14:25:25 +0530955err:
956 return -ENOMEM;
957}
958
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530959static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
960{
961 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
Sujith Manoharan3deff762011-04-13 11:25:23 +0530962 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530963 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
964 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
965}
966
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530967static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
968 u32 drv_info)
Sujithfb9987d2010-03-17 14:25:25 +0530969{
970 int transfer, err;
971 const void *data = hif_dev->firmware->data;
972 size_t len = hif_dev->firmware->size;
973 u32 addr = AR9271_FIRMWARE;
974 u8 *buf = kzalloc(4096, GFP_KERNEL);
Sujithb1762862010-06-02 15:53:34 +0530975 u32 firm_offset;
Sujithfb9987d2010-03-17 14:25:25 +0530976
977 if (!buf)
978 return -ENOMEM;
979
980 while (len) {
981 transfer = min_t(int, len, 4096);
982 memcpy(buf, data, transfer);
983
984 err = usb_control_msg(hif_dev->udev,
985 usb_sndctrlpipe(hif_dev->udev, 0),
986 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
987 addr >> 8, 0, buf, transfer, HZ);
988 if (err < 0) {
989 kfree(buf);
990 return err;
991 }
992
993 len -= transfer;
994 data += transfer;
995 addr += transfer;
996 }
997 kfree(buf);
998
Sujith Manoharan0b5ead92010-12-07 16:31:38 +0530999 if (IS_AR7010_DEVICE(drv_info))
Sujithb1762862010-06-02 15:53:34 +05301000 firm_offset = AR7010_FIRMWARE_TEXT;
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301001 else
Sujithb1762862010-06-02 15:53:34 +05301002 firm_offset = AR9271_FIRMWARE_TEXT;
1003
Sujithfb9987d2010-03-17 14:25:25 +05301004 /*
1005 * Issue FW download complete command to firmware.
1006 */
1007 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
1008 FIRMWARE_DOWNLOAD_COMP,
1009 0x40 | USB_DIR_OUT,
Sujithb1762862010-06-02 15:53:34 +05301010 firm_offset >> 8, 0, NULL, 0, HZ);
Sujithfb9987d2010-03-17 14:25:25 +05301011 if (err)
1012 return -EIO;
1013
1014 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
Sujithce43cee2010-06-02 15:53:30 +05301015 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
Sujithfb9987d2010-03-17 14:25:25 +05301016
1017 return 0;
1018}
1019
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301020static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
Sujithfb9987d2010-03-17 14:25:25 +05301021{
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +05301022 int ret, idx;
1023 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
1024 struct usb_endpoint_descriptor *endp;
Sujithfb9987d2010-03-17 14:25:25 +05301025
1026 /* Request firmware */
Sujithce43cee2010-06-02 15:53:30 +05301027 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
1028 &hif_dev->udev->dev);
Sujithfb9987d2010-03-17 14:25:25 +05301029 if (ret) {
1030 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +05301031 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
Sujithfb9987d2010-03-17 14:25:25 +05301032 goto err_fw_req;
1033 }
1034
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +05301035 /* Download firmware */
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301036 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +05301037 if (ret) {
1038 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +05301039 "ath9k_htc: Firmware - %s download failed\n",
1040 hif_dev->fw_name);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +05301041 goto err_fw_download;
1042 }
1043
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +05301044 /* On downloading the firmware to the target, the USB descriptor of EP4
1045 * is 'patched' to change the type of the endpoint to Bulk. This will
1046 * bring down CPU usage during the scan period.
1047 */
1048 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
1049 endp = &alt->endpoint[idx].desc;
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +05301050 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1051 == USB_ENDPOINT_XFER_INT) {
Rajkumar Manoharan4a0e8ec2010-09-14 14:35:55 +05301052 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
1053 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
1054 endp->bInterval = 0;
1055 }
1056 }
1057
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +05301058 /* Alloc URBs */
1059 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1060 if (ret) {
1061 dev_err(&hif_dev->udev->dev,
1062 "ath9k_htc: Unable to allocate URBs\n");
Sujith Manoharan512c0442011-02-21 07:50:38 +05301063 goto err_fw_download;
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +05301064 }
1065
Sujithfb9987d2010-03-17 14:25:25 +05301066 return 0;
1067
Sujith Manoharancaa0a992010-12-07 16:32:02 +05301068err_fw_download:
Sujithfb9987d2010-03-17 14:25:25 +05301069 release_firmware(hif_dev->firmware);
1070err_fw_req:
1071 hif_dev->firmware = NULL;
1072 return ret;
1073}
1074
Sujithfb9987d2010-03-17 14:25:25 +05301075static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
1076{
1077 ath9k_hif_usb_dealloc_urbs(hif_dev);
1078 if (hif_dev->firmware)
1079 release_firmware(hif_dev->firmware);
1080}
1081
Sujith Manoharan36bcce42011-02-21 07:47:52 +05301082/*
1083 * An exact copy of the function from zd1211rw.
1084 */
1085static int send_eject_command(struct usb_interface *interface)
1086{
1087 struct usb_device *udev = interface_to_usbdev(interface);
1088 struct usb_host_interface *iface_desc = &interface->altsetting[0];
1089 struct usb_endpoint_descriptor *endpoint;
1090 unsigned char *cmd;
1091 u8 bulk_out_ep;
1092 int r;
1093
1094 /* Find bulk out endpoint */
1095 for (r = 1; r >= 0; r--) {
1096 endpoint = &iface_desc->endpoint[r].desc;
1097 if (usb_endpoint_dir_out(endpoint) &&
1098 usb_endpoint_xfer_bulk(endpoint)) {
1099 bulk_out_ep = endpoint->bEndpointAddress;
1100 break;
1101 }
1102 }
1103 if (r == -1) {
1104 dev_err(&udev->dev,
1105 "ath9k_htc: Could not find bulk out endpoint\n");
1106 return -ENODEV;
1107 }
1108
1109 cmd = kzalloc(31, GFP_KERNEL);
1110 if (cmd == NULL)
1111 return -ENODEV;
1112
1113 /* USB bulk command block */
1114 cmd[0] = 0x55; /* bulk command signature */
1115 cmd[1] = 0x53; /* bulk command signature */
1116 cmd[2] = 0x42; /* bulk command signature */
1117 cmd[3] = 0x43; /* bulk command signature */
1118 cmd[14] = 6; /* command length */
1119
1120 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1121 cmd[19] = 0x2; /* eject disc */
1122
1123 dev_info(&udev->dev, "Ejecting storage device...\n");
1124 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1125 cmd, 31, NULL, 2000);
1126 kfree(cmd);
1127 if (r)
1128 return r;
1129
1130 /* At this point, the device disconnects and reconnects with the real
1131 * ID numbers. */
1132
1133 usb_set_intfdata(interface, NULL);
1134 return 0;
1135}
1136
Sujithfb9987d2010-03-17 14:25:25 +05301137static int ath9k_hif_usb_probe(struct usb_interface *interface,
1138 const struct usb_device_id *id)
1139{
1140 struct usb_device *udev = interface_to_usbdev(interface);
1141 struct hif_device_usb *hif_dev;
Sujithfb9987d2010-03-17 14:25:25 +05301142 int ret = 0;
1143
Sujith Manoharan36bcce42011-02-21 07:47:52 +05301144 if (id->driver_info == STORAGE_DEVICE)
1145 return send_eject_command(interface);
1146
Sujithfb9987d2010-03-17 14:25:25 +05301147 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1148 if (!hif_dev) {
1149 ret = -ENOMEM;
1150 goto err_alloc;
1151 }
1152
1153 usb_get_dev(udev);
1154 hif_dev->udev = udev;
1155 hif_dev->interface = interface;
1156 hif_dev->device_id = id->idProduct;
1157#ifdef CONFIG_PM
1158 udev->reset_resume = 1;
1159#endif
1160 usb_set_intfdata(interface, hif_dev);
1161
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +05301162 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1163 &hif_dev->udev->dev);
1164 if (hif_dev->htc_handle == NULL) {
1165 ret = -ENOMEM;
1166 goto err_htc_hw_alloc;
1167 }
1168
Sujithce43cee2010-06-02 15:53:30 +05301169 /* Find out which firmware to load */
1170
Sujith Manoharan0b5ead92010-12-07 16:31:38 +05301171 if (IS_AR7010_DEVICE(id->driver_info))
Sujith Manoharan9efabad2011-04-13 11:22:33 +05301172 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301173 else
John W. Linvilled0ee0eb2010-06-23 14:20:45 -04001174 hif_dev->fw_name = FIRMWARE_AR9271;
Sujithce43cee2010-06-02 15:53:30 +05301175
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301176 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
Sujithfb9987d2010-03-17 14:25:25 +05301177 if (ret) {
1178 ret = -EINVAL;
1179 goto err_hif_init_usb;
1180 }
1181
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +05301182 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
Sujith Manoharan50f68712011-04-11 22:56:55 +05301183 &interface->dev, hif_dev->device_id,
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301184 hif_dev->udev->product, id->driver_info);
Sujithfb9987d2010-03-17 14:25:25 +05301185 if (ret) {
1186 ret = -EINVAL;
1187 goto err_htc_hw_init;
1188 }
1189
1190 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
1191
1192 return 0;
1193
1194err_htc_hw_init:
Sujithfb9987d2010-03-17 14:25:25 +05301195 ath9k_hif_usb_dev_deinit(hif_dev);
1196err_hif_init_usb:
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +05301197 ath9k_htc_hw_free(hif_dev->htc_handle);
1198err_htc_hw_alloc:
Sujithfb9987d2010-03-17 14:25:25 +05301199 usb_set_intfdata(interface, NULL);
1200 kfree(hif_dev);
1201 usb_put_dev(udev);
1202err_alloc:
1203 return ret;
1204}
1205
Sujith62e47162010-04-23 10:28:16 +05301206static void ath9k_hif_usb_reboot(struct usb_device *udev)
1207{
1208 u32 reboot_cmd = 0xffffffff;
1209 void *buf;
1210 int ret;
1211
Julia Lawalla465a2c2010-05-15 23:17:19 +02001212 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
Sujith62e47162010-04-23 10:28:16 +05301213 if (!buf)
1214 return;
1215
Sujith62e47162010-04-23 10:28:16 +05301216 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1217 buf, 4, NULL, HZ);
1218 if (ret)
1219 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1220
1221 kfree(buf);
1222}
1223
Sujithfb9987d2010-03-17 14:25:25 +05301224static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1225{
1226 struct usb_device *udev = interface_to_usbdev(interface);
Joe Perchesb2767362010-11-30 13:42:08 -08001227 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Sujith Manoharan97dcec52010-12-20 08:02:42 +05301228 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
Sujithfb9987d2010-03-17 14:25:25 +05301229
Sujith Manoharan36bcce42011-02-21 07:47:52 +05301230 if (!hif_dev)
1231 return;
1232
1233 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1234 ath9k_htc_hw_free(hif_dev->htc_handle);
1235 ath9k_hif_usb_dev_deinit(hif_dev);
1236 usb_set_intfdata(interface, NULL);
Sujithfb9987d2010-03-17 14:25:25 +05301237
Sujith Manoharan97dcec52010-12-20 08:02:42 +05301238 if (!unplugged && (hif_dev->flags & HIF_USB_START))
Sujith62e47162010-04-23 10:28:16 +05301239 ath9k_hif_usb_reboot(udev);
Sujithfb9987d2010-03-17 14:25:25 +05301240
1241 kfree(hif_dev);
1242 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1243 usb_put_dev(udev);
1244}
1245
1246#ifdef CONFIG_PM
1247static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1248 pm_message_t message)
1249{
Joe Perchesb2767362010-11-30 13:42:08 -08001250 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Sujithfb9987d2010-03-17 14:25:25 +05301251
Sujith Manoharanf933ebe2010-12-01 12:30:27 +05301252 /*
1253 * The device has to be set to FULLSLEEP mode in case no
1254 * interface is up.
1255 */
1256 if (!(hif_dev->flags & HIF_USB_START))
1257 ath9k_htc_suspend(hif_dev->htc_handle);
1258
Sujithfb9987d2010-03-17 14:25:25 +05301259 ath9k_hif_usb_dealloc_urbs(hif_dev);
1260
1261 return 0;
1262}
1263
1264static int ath9k_hif_usb_resume(struct usb_interface *interface)
1265{
Joe Perchesb2767362010-11-30 13:42:08 -08001266 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301267 struct htc_target *htc_handle = hif_dev->htc_handle;
Sujithfb9987d2010-03-17 14:25:25 +05301268 int ret;
1269
1270 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1271 if (ret)
1272 return ret;
1273
1274 if (hif_dev->firmware) {
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301275 ret = ath9k_hif_usb_download_fw(hif_dev,
Sujith Manoharan0b5ead92010-12-07 16:31:38 +05301276 htc_handle->drv_priv->ah->hw_version.usbdev);
Sujithfb9987d2010-03-17 14:25:25 +05301277 if (ret)
1278 goto fail_resume;
1279 } else {
1280 ath9k_hif_usb_dealloc_urbs(hif_dev);
1281 return -EIO;
1282 }
1283
1284 mdelay(100);
1285
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301286 ret = ath9k_htc_resume(htc_handle);
Sujithfb9987d2010-03-17 14:25:25 +05301287
1288 if (ret)
1289 goto fail_resume;
1290
1291 return 0;
1292
1293fail_resume:
1294 ath9k_hif_usb_dealloc_urbs(hif_dev);
1295
1296 return ret;
1297}
1298#endif
1299
1300static struct usb_driver ath9k_hif_usb_driver = {
Sujith Manoharan50f68712011-04-11 22:56:55 +05301301 .name = KBUILD_MODNAME,
Sujithfb9987d2010-03-17 14:25:25 +05301302 .probe = ath9k_hif_usb_probe,
1303 .disconnect = ath9k_hif_usb_disconnect,
1304#ifdef CONFIG_PM
1305 .suspend = ath9k_hif_usb_suspend,
1306 .resume = ath9k_hif_usb_resume,
1307 .reset_resume = ath9k_hif_usb_resume,
1308#endif
1309 .id_table = ath9k_hif_usb_ids,
1310 .soft_unbind = 1,
1311};
1312
1313int ath9k_hif_usb_init(void)
1314{
1315 return usb_register(&ath9k_hif_usb_driver);
1316}
1317
1318void ath9k_hif_usb_exit(void)
1319{
1320 usb_deregister(&ath9k_hif_usb_driver);
1321}