blob: f1b8af64569cbdeea7d80316d071aea329eab494 [file] [log] [blame]
Sujithfb9987d2010-03-17 14:25:25 +05301/*
2 * Copyright (c) 2010 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "htc.h"
18
John W. Linvilled0ee0eb2010-06-23 14:20:45 -040019/* identify firmware images */
20#define FIRMWARE_AR7010 "ar7010.fw"
21#define FIRMWARE_AR7010_1_1 "ar7010_1_1.fw"
22#define FIRMWARE_AR9271 "ar9271.fw"
23
24MODULE_FIRMWARE(FIRMWARE_AR7010);
25MODULE_FIRMWARE(FIRMWARE_AR7010_1_1);
26MODULE_FIRMWARE(FIRMWARE_AR9271);
27
Sujithfb9987d2010-03-17 14:25:25 +053028static struct usb_device_id ath9k_hif_usb_ids[] = {
Sujith4e63f762010-06-17 10:29:01 +053029 { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
30 { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
Sujith4e63f762010-06-17 10:29:01 +053031 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
Sujith4e63f762010-06-17 10:29:01 +053032 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
33 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
34 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
Haitao Zhangac618d72010-11-07 12:50:24 +080035 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
Rajkumar Manoharan32b08952010-11-10 17:51:24 +053036 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
37 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
38 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
Sujith4e63f762010-06-17 10:29:01 +053039 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
Rajkumar Manoharan32b08952010-11-10 17:51:24 +053040 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
Sujith Manoharan452d7dd2010-12-13 07:39:32 +053041 { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
Sujith Manoharan0b5ead92010-12-07 16:31:38 +053042
43 { USB_DEVICE(0x0cf3, 0x7015),
44 .driver_info = AR9287_USB }, /* Atheros */
Rajkumar Manoharan64f12172010-11-19 16:53:20 +053045 { USB_DEVICE(0x1668, 0x1200),
Sujith Manoharan0b5ead92010-12-07 16:31:38 +053046 .driver_info = AR9287_USB }, /* Verizon */
47
48 { USB_DEVICE(0x0cf3, 0x7010),
49 .driver_info = AR9280_USB }, /* Atheros */
50 { USB_DEVICE(0x0846, 0x9018),
51 .driver_info = AR9280_USB }, /* Netgear WNDA3200 */
52 { USB_DEVICE(0x083A, 0xA704),
53 .driver_info = AR9280_USB }, /* SMC Networks */
54
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,
83 cmd->skb, 1);
84 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 Manoharan4a0e8ecc2010-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 Manoharan4a0e8ecc2010-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
Sujitheac8e382010-04-16 11:54:00 +0530129static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
130 struct sk_buff_head *list)
Ming Leif8e1d082010-04-13 00:29:27 +0800131{
132 struct sk_buff *skb;
Sujitheac8e382010-04-16 11:54:00 +0530133
134 while ((skb = __skb_dequeue(list)) != NULL) {
Ming Leif8e1d082010-04-13 00:29:27 +0800135 dev_kfree_skb_any(skb);
Sujitheac8e382010-04-16 11:54:00 +0530136 TX_STAT_INC(skb_dropped);
137 }
Ming Leif8e1d082010-04-13 00:29:27 +0800138}
139
Sujithc11d8f82010-04-23 10:28:09 +0530140static void hif_usb_tx_cb(struct urb *urb)
141{
142 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
Dan Carpenter690e7812010-05-14 16:50:56 +0200143 struct hif_device_usb *hif_dev;
Sujithc11d8f82010-04-23 10:28:09 +0530144 struct sk_buff *skb;
145
Dan Carpenter690e7812010-05-14 16:50:56 +0200146 if (!tx_buf || !tx_buf->hif_dev)
Sujithc11d8f82010-04-23 10:28:09 +0530147 return;
148
Dan Carpenter690e7812010-05-14 16:50:56 +0200149 hif_dev = tx_buf->hif_dev;
150
Sujithc11d8f82010-04-23 10:28:09 +0530151 switch (urb->status) {
152 case 0:
153 break;
154 case -ENOENT:
155 case -ECONNRESET:
156 case -ENODEV:
157 case -ESHUTDOWN:
158 /*
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530159 * The URB has been killed, free the SKBs.
Sujithc11d8f82010-04-23 10:28:09 +0530160 */
161 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530162
163 /*
164 * If the URBs are being flushed, no need to add this
165 * URB to the free list.
166 */
167 spin_lock(&hif_dev->tx.tx_lock);
168 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
169 spin_unlock(&hif_dev->tx.tx_lock);
170 return;
171 }
172 spin_unlock(&hif_dev->tx.tx_lock);
173
174 /*
175 * In the stop() case, this URB has to be added to
176 * the free list.
177 */
178 goto add_free;
Sujithc11d8f82010-04-23 10:28:09 +0530179 default:
180 break;
181 }
182
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530183 /*
184 * Check if TX has been stopped, this is needed because
185 * this CB could have been invoked just after the TX lock
186 * was released in hif_stop() and kill_urb() hasn't been
187 * called yet.
188 */
Sujithc11d8f82010-04-23 10:28:09 +0530189 spin_lock(&hif_dev->tx.tx_lock);
190 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
191 spin_unlock(&hif_dev->tx.tx_lock);
192 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
193 goto add_free;
194 }
195 spin_unlock(&hif_dev->tx.tx_lock);
196
197 /* Complete the queued SKBs. */
198 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
199 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
200 skb, 1);
201 TX_STAT_INC(skb_completed);
202 }
203
204add_free:
205 /* Re-initialize the SKB queue */
206 tx_buf->len = tx_buf->offset = 0;
207 __skb_queue_head_init(&tx_buf->skb_queue);
208
209 /* Add this TX buffer to the free list */
210 spin_lock(&hif_dev->tx.tx_lock);
211 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
212 hif_dev->tx.tx_buf_cnt++;
213 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
214 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
215 TX_STAT_INC(buf_completed);
216 spin_unlock(&hif_dev->tx.tx_lock);
217}
218
Sujithfb9987d2010-03-17 14:25:25 +0530219/* TX lock has to be taken */
220static int __hif_usb_tx(struct hif_device_usb *hif_dev)
221{
222 struct tx_buf *tx_buf = NULL;
223 struct sk_buff *nskb = NULL;
224 int ret = 0, i;
Sujith Manoharan2c273922011-02-27 09:23:52 +0530225 u16 tx_skb_cnt = 0;
Sujithfb9987d2010-03-17 14:25:25 +0530226 u8 *buf;
Sujith Manoharan2c273922011-02-27 09:23:52 +0530227 __le16 *hdr;
Sujithfb9987d2010-03-17 14:25:25 +0530228
229 if (hif_dev->tx.tx_skb_cnt == 0)
230 return 0;
231
232 /* Check if a free TX buffer is available */
233 if (list_empty(&hif_dev->tx.tx_buf))
234 return 0;
235
236 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
Sujithc11d8f82010-04-23 10:28:09 +0530237 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
Sujithfb9987d2010-03-17 14:25:25 +0530238 hif_dev->tx.tx_buf_cnt--;
239
240 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
241
242 for (i = 0; i < tx_skb_cnt; i++) {
243 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
244
245 /* Should never be NULL */
246 BUG_ON(!nskb);
247
248 hif_dev->tx.tx_skb_cnt--;
249
250 buf = tx_buf->buf;
251 buf += tx_buf->offset;
Sujith Manoharan2c273922011-02-27 09:23:52 +0530252 hdr = (__le16 *)buf;
253 *hdr++ = cpu_to_le16(nskb->len);
254 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
Sujithfb9987d2010-03-17 14:25:25 +0530255 buf += 4;
256 memcpy(buf, nskb->data, nskb->len);
257 tx_buf->len = nskb->len + 4;
258
259 if (i < (tx_skb_cnt - 1))
260 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
261
262 if (i == (tx_skb_cnt - 1))
263 tx_buf->len += tx_buf->offset;
264
265 __skb_queue_tail(&tx_buf->skb_queue, nskb);
266 TX_STAT_INC(skb_queued);
267 }
268
269 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
270 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
271 tx_buf->buf, tx_buf->len,
272 hif_usb_tx_cb, tx_buf);
273
274 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
275 if (ret) {
276 tx_buf->len = tx_buf->offset = 0;
Sujitheac8e382010-04-16 11:54:00 +0530277 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
Sujithfb9987d2010-03-17 14:25:25 +0530278 __skb_queue_head_init(&tx_buf->skb_queue);
279 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
280 hif_dev->tx.tx_buf_cnt++;
281 }
282
283 if (!ret)
284 TX_STAT_INC(buf_queued);
285
286 return ret;
287}
288
289static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
290 struct ath9k_htc_tx_ctl *tx_ctl)
291{
292 unsigned long flags;
293
294 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
295
296 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
297 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
298 return -ENODEV;
299 }
300
301 /* Check if the max queue count has been reached */
302 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
303 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
304 return -ENOMEM;
305 }
306
307 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
308 hif_dev->tx.tx_skb_cnt++;
309
310 /* Send normal frames immediately */
311 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
312 __hif_usb_tx(hif_dev);
313
314 /* Check if AMPDUs have to be sent immediately */
315 if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
316 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
317 (hif_dev->tx.tx_skb_cnt < 2)) {
318 __hif_usb_tx(hif_dev);
319 }
320
321 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
322
323 return 0;
324}
325
326static void hif_usb_start(void *hif_handle, u8 pipe_id)
327{
328 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
329 unsigned long flags;
330
331 hif_dev->flags |= HIF_USB_START;
332
333 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
334 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
335 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
336}
337
338static void hif_usb_stop(void *hif_handle, u8 pipe_id)
339{
340 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530341 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530342 unsigned long flags;
343
344 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
Sujitheac8e382010-04-16 11:54:00 +0530345 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
Sujithfb9987d2010-03-17 14:25:25 +0530346 hif_dev->tx.tx_skb_cnt = 0;
347 hif_dev->tx.flags |= HIF_USB_TX_STOP;
348 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530349
350 /* The pending URBs have to be canceled. */
351 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
352 &hif_dev->tx.tx_pending, list) {
353 usb_kill_urb(tx_buf->urb);
354 }
Sujithfb9987d2010-03-17 14:25:25 +0530355}
356
357static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
358 struct ath9k_htc_tx_ctl *tx_ctl)
359{
360 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
361 int ret = 0;
362
363 switch (pipe_id) {
364 case USB_WLAN_TX_PIPE:
365 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
366 break;
367 case USB_REG_OUT_PIPE:
368 ret = hif_usb_send_regout(hif_dev, skb);
369 break;
370 default:
Sujith6335ed02010-03-29 16:07:15 +0530371 dev_err(&hif_dev->udev->dev,
372 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
Sujithfb9987d2010-03-17 14:25:25 +0530373 ret = -EINVAL;
374 break;
375 }
376
377 return ret;
378}
379
380static struct ath9k_htc_hif hif_usb = {
381 .transport = ATH9K_HIF_USB,
382 .name = "ath9k_hif_usb",
383
384 .control_ul_pipe = USB_REG_OUT_PIPE,
385 .control_dl_pipe = USB_REG_IN_PIPE,
386
387 .start = hif_usb_start,
388 .stop = hif_usb_stop,
389 .send = hif_usb_send,
390};
391
392static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
393 struct sk_buff *skb)
394{
Sujithc5032692010-04-06 15:28:15 +0530395 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
Joe Perches44b23b42010-11-30 12:19:11 -0800396 int index = 0, i = 0, len = skb->len;
397 int rx_remain_len, rx_pkt_len;
398 u16 pool_index = 0;
Sujithfb9987d2010-03-17 14:25:25 +0530399 u8 *ptr;
400
Sujith46baa1a2010-04-06 15:28:11 +0530401 spin_lock(&hif_dev->rx_lock);
402
Sujithfb9987d2010-03-17 14:25:25 +0530403 rx_remain_len = hif_dev->rx_remain_len;
404 rx_pkt_len = hif_dev->rx_transfer_len;
405
406 if (rx_remain_len != 0) {
407 struct sk_buff *remain_skb = hif_dev->remain_skb;
408
409 if (remain_skb) {
410 ptr = (u8 *) remain_skb->data;
411
412 index = rx_remain_len;
413 rx_remain_len -= hif_dev->rx_pad_len;
414 ptr += rx_pkt_len;
415
416 memcpy(ptr, skb->data, rx_remain_len);
417
418 rx_pkt_len += rx_remain_len;
419 hif_dev->rx_remain_len = 0;
420 skb_put(remain_skb, rx_pkt_len);
421
422 skb_pool[pool_index++] = remain_skb;
423
424 } else {
425 index = rx_remain_len;
426 }
427 }
428
Sujith46baa1a2010-04-06 15:28:11 +0530429 spin_unlock(&hif_dev->rx_lock);
430
Sujithfb9987d2010-03-17 14:25:25 +0530431 while (index < len) {
Joe Perches44b23b42010-11-30 12:19:11 -0800432 u16 pkt_len;
433 u16 pkt_tag;
434 u16 pad_len;
435 int chk_idx;
436
Sujithfb9987d2010-03-17 14:25:25 +0530437 ptr = (u8 *) skb->data;
438
439 pkt_len = ptr[index] + (ptr[index+1] << 8);
440 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
441
Joe Perches44b23b42010-11-30 12:19:11 -0800442 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
Sujithfb9987d2010-03-17 14:25:25 +0530443 RX_STAT_INC(skb_dropped);
Sujithfb9987d2010-03-17 14:25:25 +0530444 return;
445 }
Joe Perches44b23b42010-11-30 12:19:11 -0800446
447 pad_len = 4 - (pkt_len & 0x3);
448 if (pad_len == 4)
449 pad_len = 0;
450
451 chk_idx = index;
452 index = index + 4 + pkt_len + pad_len;
453
454 if (index > MAX_RX_BUF_SIZE) {
455 spin_lock(&hif_dev->rx_lock);
456 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
457 hif_dev->rx_transfer_len =
458 MAX_RX_BUF_SIZE - chk_idx - 4;
459 hif_dev->rx_pad_len = pad_len;
460
461 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
462 if (!nskb) {
463 dev_err(&hif_dev->udev->dev,
464 "ath9k_htc: RX memory allocation error\n");
465 spin_unlock(&hif_dev->rx_lock);
466 goto err;
467 }
468 skb_reserve(nskb, 32);
469 RX_STAT_INC(skb_allocated);
470
471 memcpy(nskb->data, &(skb->data[chk_idx+4]),
472 hif_dev->rx_transfer_len);
473
474 /* Record the buffer pointer */
475 hif_dev->remain_skb = nskb;
476 spin_unlock(&hif_dev->rx_lock);
477 } else {
478 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
479 if (!nskb) {
480 dev_err(&hif_dev->udev->dev,
481 "ath9k_htc: RX memory allocation error\n");
482 goto err;
483 }
484 skb_reserve(nskb, 32);
485 RX_STAT_INC(skb_allocated);
486
487 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
488 skb_put(nskb, pkt_len);
489 skb_pool[pool_index++] = nskb;
490 }
Sujithfb9987d2010-03-17 14:25:25 +0530491 }
492
493err:
Sujithfb9987d2010-03-17 14:25:25 +0530494 for (i = 0; i < pool_index; i++) {
495 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
496 skb_pool[i]->len, USB_WLAN_RX_PIPE);
497 RX_STAT_INC(skb_completed);
498 }
499}
500
501static void ath9k_hif_usb_rx_cb(struct urb *urb)
502{
503 struct sk_buff *skb = (struct sk_buff *) urb->context;
Joe Perchesb2767362010-11-30 13:42:08 -0800504 struct hif_device_usb *hif_dev =
Sujithfb9987d2010-03-17 14:25:25 +0530505 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
506 int ret;
507
Sujith6335ed02010-03-29 16:07:15 +0530508 if (!skb)
509 return;
510
Sujithfb9987d2010-03-17 14:25:25 +0530511 if (!hif_dev)
512 goto free;
513
514 switch (urb->status) {
515 case 0:
516 break;
517 case -ENOENT:
518 case -ECONNRESET:
519 case -ENODEV:
520 case -ESHUTDOWN:
521 goto free;
522 default:
523 goto resubmit;
524 }
525
526 if (likely(urb->actual_length != 0)) {
527 skb_put(skb, urb->actual_length);
Sujithfb9987d2010-03-17 14:25:25 +0530528 ath9k_hif_usb_rx_stream(hif_dev, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530529 }
530
531resubmit:
532 skb_reset_tail_pointer(skb);
533 skb_trim(skb, 0);
534
Sujith6335ed02010-03-29 16:07:15 +0530535 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530536 ret = usb_submit_urb(urb, GFP_ATOMIC);
Sujith6335ed02010-03-29 16:07:15 +0530537 if (ret) {
538 usb_unanchor_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530539 goto free;
Sujith6335ed02010-03-29 16:07:15 +0530540 }
Sujithfb9987d2010-03-17 14:25:25 +0530541
542 return;
543free:
Ming Leif28a7b32010-04-13 00:28:53 +0800544 kfree_skb(skb);
Sujithfb9987d2010-03-17 14:25:25 +0530545}
546
547static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
548{
549 struct sk_buff *skb = (struct sk_buff *) urb->context;
550 struct sk_buff *nskb;
Joe Perchesb2767362010-11-30 13:42:08 -0800551 struct hif_device_usb *hif_dev =
Sujithfb9987d2010-03-17 14:25:25 +0530552 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
553 int ret;
554
Sujith6335ed02010-03-29 16:07:15 +0530555 if (!skb)
556 return;
557
Sujithfb9987d2010-03-17 14:25:25 +0530558 if (!hif_dev)
559 goto free;
560
561 switch (urb->status) {
562 case 0:
563 break;
564 case -ENOENT:
565 case -ECONNRESET:
566 case -ENODEV:
567 case -ESHUTDOWN:
568 goto free;
569 default:
570 goto resubmit;
571 }
572
573 if (likely(urb->actual_length != 0)) {
574 skb_put(skb, urb->actual_length);
575
Sujith5ab0af32010-04-23 10:28:17 +0530576 /* Process the command first */
577 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
578 skb->len, USB_REG_IN_PIPE);
579
580
Ming Leie6c6d332010-04-13 00:29:05 +0800581 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
Sujith5ab0af32010-04-23 10:28:17 +0530582 if (!nskb) {
583 dev_err(&hif_dev->udev->dev,
584 "ath9k_htc: REG_IN memory allocation failure\n");
585 urb->context = NULL;
586 return;
587 }
Sujithfb9987d2010-03-17 14:25:25 +0530588
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530589 usb_fill_bulk_urb(urb, hif_dev->udev,
Rajkumar Manoharan0f529e92010-09-16 19:56:55 +0530590 usb_rcvbulkpipe(hif_dev->udev,
591 USB_REG_IN_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530592 nskb->data, MAX_REG_IN_BUF_SIZE,
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530593 ath9k_hif_usb_reg_in_cb, nskb);
Sujithfb9987d2010-03-17 14:25:25 +0530594
595 ret = usb_submit_urb(urb, GFP_ATOMIC);
596 if (ret) {
Ming Leie6c6d332010-04-13 00:29:05 +0800597 kfree_skb(nskb);
Sujith5ab0af32010-04-23 10:28:17 +0530598 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530599 }
600
Sujithfb9987d2010-03-17 14:25:25 +0530601 return;
602 }
603
604resubmit:
605 skb_reset_tail_pointer(skb);
606 skb_trim(skb, 0);
607
608 ret = usb_submit_urb(urb, GFP_ATOMIC);
609 if (ret)
610 goto free;
611
612 return;
613free:
Ming Leie6c6d332010-04-13 00:29:05 +0800614 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530615 urb->context = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530616}
617
618static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
619{
Sujithfb9987d2010-03-17 14:25:25 +0530620 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530621 unsigned long flags;
Sujithfb9987d2010-03-17 14:25:25 +0530622
Sujithc11d8f82010-04-23 10:28:09 +0530623 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
624 &hif_dev->tx.tx_buf, list) {
625 usb_kill_urb(tx_buf->urb);
Sujithfb9987d2010-03-17 14:25:25 +0530626 list_del(&tx_buf->list);
627 usb_free_urb(tx_buf->urb);
628 kfree(tx_buf->buf);
629 kfree(tx_buf);
630 }
631
Sujith Manoharanff8f59b2010-12-28 14:28:05 +0530632 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
633 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
634 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
635
Sujithfb9987d2010-03-17 14:25:25 +0530636 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
637 &hif_dev->tx.tx_pending, list) {
638 usb_kill_urb(tx_buf->urb);
639 list_del(&tx_buf->list);
640 usb_free_urb(tx_buf->urb);
641 kfree(tx_buf->buf);
642 kfree(tx_buf);
643 }
Sujithfb9987d2010-03-17 14:25:25 +0530644}
645
646static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
647{
648 struct tx_buf *tx_buf;
649 int i;
650
651 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
652 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
653 spin_lock_init(&hif_dev->tx.tx_lock);
654 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
655
656 for (i = 0; i < MAX_TX_URB_NUM; i++) {
657 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
658 if (!tx_buf)
659 goto err;
660
661 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
662 if (!tx_buf->buf)
663 goto err;
664
665 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
666 if (!tx_buf->urb)
667 goto err;
668
669 tx_buf->hif_dev = hif_dev;
670 __skb_queue_head_init(&tx_buf->skb_queue);
671
672 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
673 }
674
675 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
676
677 return 0;
678err:
Dan Carpenter76066882010-05-14 16:52:37 +0200679 if (tx_buf) {
680 kfree(tx_buf->buf);
681 kfree(tx_buf);
682 }
Sujithfb9987d2010-03-17 14:25:25 +0530683 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
684 return -ENOMEM;
685}
686
Sujithfb9987d2010-03-17 14:25:25 +0530687static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
688{
Sujith6335ed02010-03-29 16:07:15 +0530689 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530690}
691
692static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
693{
Sujith6335ed02010-03-29 16:07:15 +0530694 struct urb *urb = NULL;
695 struct sk_buff *skb = NULL;
Sujithfb9987d2010-03-17 14:25:25 +0530696 int i, ret;
697
Sujith6335ed02010-03-29 16:07:15 +0530698 init_usb_anchor(&hif_dev->rx_submitted);
Sujith46baa1a2010-04-06 15:28:11 +0530699 spin_lock_init(&hif_dev->rx_lock);
Sujith6335ed02010-03-29 16:07:15 +0530700
Sujithfb9987d2010-03-17 14:25:25 +0530701 for (i = 0; i < MAX_RX_URB_NUM; i++) {
702
703 /* Allocate URB */
Sujith6335ed02010-03-29 16:07:15 +0530704 urb = usb_alloc_urb(0, GFP_KERNEL);
705 if (urb == NULL) {
Sujithfb9987d2010-03-17 14:25:25 +0530706 ret = -ENOMEM;
Sujith6335ed02010-03-29 16:07:15 +0530707 goto err_urb;
Sujithfb9987d2010-03-17 14:25:25 +0530708 }
709
710 /* Allocate buffer */
Ming Leif28a7b32010-04-13 00:28:53 +0800711 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
Sujith6335ed02010-03-29 16:07:15 +0530712 if (!skb) {
713 ret = -ENOMEM;
714 goto err_skb;
715 }
716
717 usb_fill_bulk_urb(urb, hif_dev->udev,
718 usb_rcvbulkpipe(hif_dev->udev,
719 USB_WLAN_RX_PIPE),
720 skb->data, MAX_RX_BUF_SIZE,
721 ath9k_hif_usb_rx_cb, skb);
722
723 /* Anchor URB */
724 usb_anchor_urb(urb, &hif_dev->rx_submitted);
Sujithfb9987d2010-03-17 14:25:25 +0530725
726 /* Submit URB */
Sujith6335ed02010-03-29 16:07:15 +0530727 ret = usb_submit_urb(urb, GFP_KERNEL);
728 if (ret) {
729 usb_unanchor_urb(urb);
730 goto err_submit;
731 }
Sujith66b10e32010-04-06 15:28:13 +0530732
733 /*
734 * Drop reference count.
735 * This ensures that the URB is freed when killing them.
736 */
737 usb_free_urb(urb);
Sujithfb9987d2010-03-17 14:25:25 +0530738 }
739
740 return 0;
741
Sujith6335ed02010-03-29 16:07:15 +0530742err_submit:
Ming Leif28a7b32010-04-13 00:28:53 +0800743 kfree_skb(skb);
Sujith6335ed02010-03-29 16:07:15 +0530744err_skb:
745 usb_free_urb(urb);
746err_urb:
Sujithfb9987d2010-03-17 14:25:25 +0530747 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
748 return ret;
749}
750
751static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
752{
753 if (hif_dev->reg_in_urb) {
754 usb_kill_urb(hif_dev->reg_in_urb);
Sujith6335ed02010-03-29 16:07:15 +0530755 if (hif_dev->reg_in_urb->context)
Ming Leie6c6d332010-04-13 00:29:05 +0800756 kfree_skb((void *)hif_dev->reg_in_urb->context);
Sujithfb9987d2010-03-17 14:25:25 +0530757 usb_free_urb(hif_dev->reg_in_urb);
758 hif_dev->reg_in_urb = NULL;
759 }
760}
761
762static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
763{
764 struct sk_buff *skb;
765
766 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
767 if (hif_dev->reg_in_urb == NULL)
768 return -ENOMEM;
769
Ming Leie6c6d332010-04-13 00:29:05 +0800770 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
Sujithfb9987d2010-03-17 14:25:25 +0530771 if (!skb)
772 goto err;
773
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530774 usb_fill_bulk_urb(hif_dev->reg_in_urb, hif_dev->udev,
Rajkumar Manoharan0f529e92010-09-16 19:56:55 +0530775 usb_rcvbulkpipe(hif_dev->udev,
776 USB_REG_IN_PIPE),
Sujithfb9987d2010-03-17 14:25:25 +0530777 skb->data, MAX_REG_IN_BUF_SIZE,
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530778 ath9k_hif_usb_reg_in_cb, skb);
Sujithfb9987d2010-03-17 14:25:25 +0530779
780 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
Sujith6335ed02010-03-29 16:07:15 +0530781 goto err;
Sujithfb9987d2010-03-17 14:25:25 +0530782
783 return 0;
784
Sujithfb9987d2010-03-17 14:25:25 +0530785err:
786 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
787 return -ENOMEM;
788}
789
790static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
791{
Sujith6f0f2662010-04-06 15:28:17 +0530792 /* Register Write */
793 init_usb_anchor(&hif_dev->regout_submitted);
794
Sujithfb9987d2010-03-17 14:25:25 +0530795 /* TX */
796 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
797 goto err;
798
799 /* RX */
800 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530801 goto err_rx;
Sujithfb9987d2010-03-17 14:25:25 +0530802
Sujith6f0f2662010-04-06 15:28:17 +0530803 /* Register Read */
Sujithfb9987d2010-03-17 14:25:25 +0530804 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530805 goto err_reg;
Sujithfb9987d2010-03-17 14:25:25 +0530806
807 return 0;
Rajkumar Manoharanf8036962010-07-07 15:19:18 +0530808err_reg:
809 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
810err_rx:
811 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
Sujithfb9987d2010-03-17 14:25:25 +0530812err:
813 return -ENOMEM;
814}
815
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530816static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
817{
818 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
819 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
820 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
821 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
822}
823
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530824static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
825 u32 drv_info)
Sujithfb9987d2010-03-17 14:25:25 +0530826{
827 int transfer, err;
828 const void *data = hif_dev->firmware->data;
829 size_t len = hif_dev->firmware->size;
830 u32 addr = AR9271_FIRMWARE;
831 u8 *buf = kzalloc(4096, GFP_KERNEL);
Sujithb1762862010-06-02 15:53:34 +0530832 u32 firm_offset;
Sujithfb9987d2010-03-17 14:25:25 +0530833
834 if (!buf)
835 return -ENOMEM;
836
837 while (len) {
838 transfer = min_t(int, len, 4096);
839 memcpy(buf, data, transfer);
840
841 err = usb_control_msg(hif_dev->udev,
842 usb_sndctrlpipe(hif_dev->udev, 0),
843 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
844 addr >> 8, 0, buf, transfer, HZ);
845 if (err < 0) {
846 kfree(buf);
847 return err;
848 }
849
850 len -= transfer;
851 data += transfer;
852 addr += transfer;
853 }
854 kfree(buf);
855
Sujith Manoharan0b5ead92010-12-07 16:31:38 +0530856 if (IS_AR7010_DEVICE(drv_info))
Sujithb1762862010-06-02 15:53:34 +0530857 firm_offset = AR7010_FIRMWARE_TEXT;
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530858 else
Sujithb1762862010-06-02 15:53:34 +0530859 firm_offset = AR9271_FIRMWARE_TEXT;
860
Sujithfb9987d2010-03-17 14:25:25 +0530861 /*
862 * Issue FW download complete command to firmware.
863 */
864 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
865 FIRMWARE_DOWNLOAD_COMP,
866 0x40 | USB_DIR_OUT,
Sujithb1762862010-06-02 15:53:34 +0530867 firm_offset >> 8, 0, NULL, 0, HZ);
Sujithfb9987d2010-03-17 14:25:25 +0530868 if (err)
869 return -EIO;
870
871 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
Sujithce43cee2010-06-02 15:53:30 +0530872 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
Sujithfb9987d2010-03-17 14:25:25 +0530873
874 return 0;
875}
876
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530877static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
Sujithfb9987d2010-03-17 14:25:25 +0530878{
Rajkumar Manoharan4a0e8ecc2010-09-14 14:35:55 +0530879 int ret, idx;
880 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
881 struct usb_endpoint_descriptor *endp;
Sujithfb9987d2010-03-17 14:25:25 +0530882
883 /* Request firmware */
Sujithce43cee2010-06-02 15:53:30 +0530884 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
885 &hif_dev->udev->dev);
Sujithfb9987d2010-03-17 14:25:25 +0530886 if (ret) {
887 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +0530888 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
Sujithfb9987d2010-03-17 14:25:25 +0530889 goto err_fw_req;
890 }
891
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530892 /* Download firmware */
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +0530893 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530894 if (ret) {
895 dev_err(&hif_dev->udev->dev,
Sujithce43cee2010-06-02 15:53:30 +0530896 "ath9k_htc: Firmware - %s download failed\n",
897 hif_dev->fw_name);
Sujith.Manoharan@atheros.com1d8af8c2010-05-11 16:24:40 +0530898 goto err_fw_download;
899 }
900
Rajkumar Manoharan4a0e8ecc2010-09-14 14:35:55 +0530901 /* On downloading the firmware to the target, the USB descriptor of EP4
902 * is 'patched' to change the type of the endpoint to Bulk. This will
903 * bring down CPU usage during the scan period.
904 */
905 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
906 endp = &alt->endpoint[idx].desc;
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530907 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
908 == USB_ENDPOINT_XFER_INT) {
Rajkumar Manoharan4a0e8ecc2010-09-14 14:35:55 +0530909 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
910 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
911 endp->bInterval = 0;
912 }
913 }
914
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530915 /* Alloc URBs */
916 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
917 if (ret) {
918 dev_err(&hif_dev->udev->dev,
919 "ath9k_htc: Unable to allocate URBs\n");
Sujith Manoharan512c0442011-02-21 07:50:38 +0530920 goto err_fw_download;
Rajkumar Manoharan490b3f42010-11-08 12:49:12 +0530921 }
922
Sujithfb9987d2010-03-17 14:25:25 +0530923 return 0;
924
Sujith Manoharancaa0a992010-12-07 16:32:02 +0530925err_fw_download:
Sujithfb9987d2010-03-17 14:25:25 +0530926 release_firmware(hif_dev->firmware);
927err_fw_req:
928 hif_dev->firmware = NULL;
929 return ret;
930}
931
Sujithfb9987d2010-03-17 14:25:25 +0530932static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
933{
934 ath9k_hif_usb_dealloc_urbs(hif_dev);
935 if (hif_dev->firmware)
936 release_firmware(hif_dev->firmware);
937}
938
Sujith Manoharan36bcce42011-02-21 07:47:52 +0530939/*
940 * An exact copy of the function from zd1211rw.
941 */
942static int send_eject_command(struct usb_interface *interface)
943{
944 struct usb_device *udev = interface_to_usbdev(interface);
945 struct usb_host_interface *iface_desc = &interface->altsetting[0];
946 struct usb_endpoint_descriptor *endpoint;
947 unsigned char *cmd;
948 u8 bulk_out_ep;
949 int r;
950
951 /* Find bulk out endpoint */
952 for (r = 1; r >= 0; r--) {
953 endpoint = &iface_desc->endpoint[r].desc;
954 if (usb_endpoint_dir_out(endpoint) &&
955 usb_endpoint_xfer_bulk(endpoint)) {
956 bulk_out_ep = endpoint->bEndpointAddress;
957 break;
958 }
959 }
960 if (r == -1) {
961 dev_err(&udev->dev,
962 "ath9k_htc: Could not find bulk out endpoint\n");
963 return -ENODEV;
964 }
965
966 cmd = kzalloc(31, GFP_KERNEL);
967 if (cmd == NULL)
968 return -ENODEV;
969
970 /* USB bulk command block */
971 cmd[0] = 0x55; /* bulk command signature */
972 cmd[1] = 0x53; /* bulk command signature */
973 cmd[2] = 0x42; /* bulk command signature */
974 cmd[3] = 0x43; /* bulk command signature */
975 cmd[14] = 6; /* command length */
976
977 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
978 cmd[19] = 0x2; /* eject disc */
979
980 dev_info(&udev->dev, "Ejecting storage device...\n");
981 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
982 cmd, 31, NULL, 2000);
983 kfree(cmd);
984 if (r)
985 return r;
986
987 /* At this point, the device disconnects and reconnects with the real
988 * ID numbers. */
989
990 usb_set_intfdata(interface, NULL);
991 return 0;
992}
993
Sujithfb9987d2010-03-17 14:25:25 +0530994static int ath9k_hif_usb_probe(struct usb_interface *interface,
995 const struct usb_device_id *id)
996{
997 struct usb_device *udev = interface_to_usbdev(interface);
998 struct hif_device_usb *hif_dev;
Sujithfb9987d2010-03-17 14:25:25 +0530999 int ret = 0;
1000
Sujith Manoharan36bcce42011-02-21 07:47:52 +05301001 if (id->driver_info == STORAGE_DEVICE)
1002 return send_eject_command(interface);
1003
Sujithfb9987d2010-03-17 14:25:25 +05301004 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1005 if (!hif_dev) {
1006 ret = -ENOMEM;
1007 goto err_alloc;
1008 }
1009
1010 usb_get_dev(udev);
1011 hif_dev->udev = udev;
1012 hif_dev->interface = interface;
1013 hif_dev->device_id = id->idProduct;
1014#ifdef CONFIG_PM
1015 udev->reset_resume = 1;
1016#endif
1017 usb_set_intfdata(interface, hif_dev);
1018
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +05301019 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1020 &hif_dev->udev->dev);
1021 if (hif_dev->htc_handle == NULL) {
1022 ret = -ENOMEM;
1023 goto err_htc_hw_alloc;
1024 }
1025
Sujithce43cee2010-06-02 15:53:30 +05301026 /* Find out which firmware to load */
1027
Sujith Manoharan0b5ead92010-12-07 16:31:38 +05301028 if (IS_AR7010_DEVICE(id->driver_info))
Sujithb1762862010-06-02 15:53:34 +05301029 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
John W. Linvilled0ee0eb2010-06-23 14:20:45 -04001030 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
Sujithb1762862010-06-02 15:53:34 +05301031 else
John W. Linvilled0ee0eb2010-06-23 14:20:45 -04001032 hif_dev->fw_name = FIRMWARE_AR7010;
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301033 else
John W. Linvilled0ee0eb2010-06-23 14:20:45 -04001034 hif_dev->fw_name = FIRMWARE_AR9271;
Sujithce43cee2010-06-02 15:53:30 +05301035
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301036 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
Sujithfb9987d2010-03-17 14:25:25 +05301037 if (ret) {
1038 ret = -EINVAL;
1039 goto err_hif_init_usb;
1040 }
1041
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +05301042 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
Vivek Natarajan21cb9872010-08-18 19:57:49 +05301043 &hif_dev->udev->dev, hif_dev->device_id,
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301044 hif_dev->udev->product, id->driver_info);
Sujithfb9987d2010-03-17 14:25:25 +05301045 if (ret) {
1046 ret = -EINVAL;
1047 goto err_htc_hw_init;
1048 }
1049
1050 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
1051
1052 return 0;
1053
1054err_htc_hw_init:
Sujithfb9987d2010-03-17 14:25:25 +05301055 ath9k_hif_usb_dev_deinit(hif_dev);
1056err_hif_init_usb:
Sujith.Manoharan@atheros.com47fce022010-05-11 16:24:41 +05301057 ath9k_htc_hw_free(hif_dev->htc_handle);
1058err_htc_hw_alloc:
Sujithfb9987d2010-03-17 14:25:25 +05301059 usb_set_intfdata(interface, NULL);
1060 kfree(hif_dev);
1061 usb_put_dev(udev);
1062err_alloc:
1063 return ret;
1064}
1065
Sujith62e47162010-04-23 10:28:16 +05301066static void ath9k_hif_usb_reboot(struct usb_device *udev)
1067{
1068 u32 reboot_cmd = 0xffffffff;
1069 void *buf;
1070 int ret;
1071
Julia Lawalla465a2c2010-05-15 23:17:19 +02001072 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
Sujith62e47162010-04-23 10:28:16 +05301073 if (!buf)
1074 return;
1075
Sujith62e47162010-04-23 10:28:16 +05301076 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1077 buf, 4, NULL, HZ);
1078 if (ret)
1079 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1080
1081 kfree(buf);
1082}
1083
Sujithfb9987d2010-03-17 14:25:25 +05301084static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1085{
1086 struct usb_device *udev = interface_to_usbdev(interface);
Joe Perchesb2767362010-11-30 13:42:08 -08001087 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Sujith Manoharan97dcec52010-12-20 08:02:42 +05301088 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
Sujithfb9987d2010-03-17 14:25:25 +05301089
Sujith Manoharan36bcce42011-02-21 07:47:52 +05301090 if (!hif_dev)
1091 return;
1092
1093 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1094 ath9k_htc_hw_free(hif_dev->htc_handle);
1095 ath9k_hif_usb_dev_deinit(hif_dev);
1096 usb_set_intfdata(interface, NULL);
Sujithfb9987d2010-03-17 14:25:25 +05301097
Sujith Manoharan97dcec52010-12-20 08:02:42 +05301098 if (!unplugged && (hif_dev->flags & HIF_USB_START))
Sujith62e47162010-04-23 10:28:16 +05301099 ath9k_hif_usb_reboot(udev);
Sujithfb9987d2010-03-17 14:25:25 +05301100
1101 kfree(hif_dev);
1102 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1103 usb_put_dev(udev);
1104}
1105
1106#ifdef CONFIG_PM
1107static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1108 pm_message_t message)
1109{
Joe Perchesb2767362010-11-30 13:42:08 -08001110 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Sujithfb9987d2010-03-17 14:25:25 +05301111
Sujith Manoharanf933ebe2010-12-01 12:30:27 +05301112 /*
1113 * The device has to be set to FULLSLEEP mode in case no
1114 * interface is up.
1115 */
1116 if (!(hif_dev->flags & HIF_USB_START))
1117 ath9k_htc_suspend(hif_dev->htc_handle);
1118
Sujithfb9987d2010-03-17 14:25:25 +05301119 ath9k_hif_usb_dealloc_urbs(hif_dev);
1120
1121 return 0;
1122}
1123
1124static int ath9k_hif_usb_resume(struct usb_interface *interface)
1125{
Joe Perchesb2767362010-11-30 13:42:08 -08001126 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301127 struct htc_target *htc_handle = hif_dev->htc_handle;
Sujithfb9987d2010-03-17 14:25:25 +05301128 int ret;
1129
1130 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1131 if (ret)
1132 return ret;
1133
1134 if (hif_dev->firmware) {
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301135 ret = ath9k_hif_usb_download_fw(hif_dev,
Sujith Manoharan0b5ead92010-12-07 16:31:38 +05301136 htc_handle->drv_priv->ah->hw_version.usbdev);
Sujithfb9987d2010-03-17 14:25:25 +05301137 if (ret)
1138 goto fail_resume;
1139 } else {
1140 ath9k_hif_usb_dealloc_urbs(hif_dev);
1141 return -EIO;
1142 }
1143
1144 mdelay(100);
1145
Rajkumar Manoharanfa6e15e2010-11-19 16:53:22 +05301146 ret = ath9k_htc_resume(htc_handle);
Sujithfb9987d2010-03-17 14:25:25 +05301147
1148 if (ret)
1149 goto fail_resume;
1150
1151 return 0;
1152
1153fail_resume:
1154 ath9k_hif_usb_dealloc_urbs(hif_dev);
1155
1156 return ret;
1157}
1158#endif
1159
1160static struct usb_driver ath9k_hif_usb_driver = {
1161 .name = "ath9k_hif_usb",
1162 .probe = ath9k_hif_usb_probe,
1163 .disconnect = ath9k_hif_usb_disconnect,
1164#ifdef CONFIG_PM
1165 .suspend = ath9k_hif_usb_suspend,
1166 .resume = ath9k_hif_usb_resume,
1167 .reset_resume = ath9k_hif_usb_resume,
1168#endif
1169 .id_table = ath9k_hif_usb_ids,
1170 .soft_unbind = 1,
1171};
1172
1173int ath9k_hif_usb_init(void)
1174{
1175 return usb_register(&ath9k_hif_usb_driver);
1176}
1177
1178void ath9k_hif_usb_exit(void)
1179{
1180 usb_deregister(&ath9k_hif_usb_driver);
1181}