Commit | Line | Data |
---|---|---|
fb9987d0 | 1 | /* |
5b68138e | 2 | * Copyright (c) 2010-2011 Atheros Communications Inc. |
fb9987d0 S |
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 | ||
78fa99ab | 17 | #include <asm/unaligned.h> |
fb9987d0 S |
18 | #include "htc.h" |
19 | ||
d0ee0ebe | 20 | /* identify firmware images */ |
ce18f391 SM |
21 | #define FIRMWARE_AR7010_1_1 "htc_7010.fw" |
22 | #define FIRMWARE_AR9271 "htc_9271.fw" | |
d0ee0ebe | 23 | |
d0ee0ebe JL |
24 | MODULE_FIRMWARE(FIRMWARE_AR7010_1_1); |
25 | MODULE_FIRMWARE(FIRMWARE_AR9271); | |
26 | ||
fb9987d0 | 27 | static struct usb_device_id ath9k_hif_usb_ids[] = { |
4e63f768 S |
28 | { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */ |
29 | { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */ | |
4e63f768 | 30 | { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */ |
4e63f768 S |
31 | { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */ |
32 | { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */ | |
33 | { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */ | |
ac618d70 | 34 | { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */ |
32b08955 RM |
35 | { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */ |
36 | { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */ | |
37 | { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */ | |
4e63f768 | 38 | { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */ |
32b08955 | 39 | { USB_DEVICE(0x040D, 0x3801) }, /* VIA */ |
452d7dd8 | 40 | { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */ |
763cbac0 | 41 | { USB_DEVICE(0x0cf3, 0xb002) }, /* Ubiquiti WifiStation */ |
8c34559b | 42 | { USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */ |
0b5ead91 SM |
43 | |
44 | { USB_DEVICE(0x0cf3, 0x7015), | |
45 | .driver_info = AR9287_USB }, /* Atheros */ | |
64f12170 | 46 | { USB_DEVICE(0x1668, 0x1200), |
0b5ead91 SM |
47 | .driver_info = AR9287_USB }, /* Verizon */ |
48 | ||
49 | { USB_DEVICE(0x0cf3, 0x7010), | |
50 | .driver_info = AR9280_USB }, /* Atheros */ | |
51 | { USB_DEVICE(0x0846, 0x9018), | |
52 | .driver_info = AR9280_USB }, /* Netgear WNDA3200 */ | |
53 | { USB_DEVICE(0x083A, 0xA704), | |
54 | .driver_info = AR9280_USB }, /* SMC Networks */ | |
5cf6fa78 MSS |
55 | { USB_DEVICE(0x0411, 0x017f), |
56 | .driver_info = AR9280_USB }, /* Sony UWA-BR100 */ | |
d90b5708 SM |
57 | { USB_DEVICE(0x04da, 0x3904), |
58 | .driver_info = AR9280_USB }, | |
0b5ead91 | 59 | |
36bcce43 SM |
60 | { USB_DEVICE(0x0cf3, 0x20ff), |
61 | .driver_info = STORAGE_DEVICE }, | |
62 | ||
fb9987d0 S |
63 | { }, |
64 | }; | |
65 | ||
66 | MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids); | |
67 | ||
68 | static int __hif_usb_tx(struct hif_device_usb *hif_dev); | |
69 | ||
70 | static void hif_usb_regout_cb(struct urb *urb) | |
71 | { | |
72 | struct cmd_buf *cmd = (struct cmd_buf *)urb->context; | |
fb9987d0 S |
73 | |
74 | switch (urb->status) { | |
75 | case 0: | |
76 | break; | |
77 | case -ENOENT: | |
78 | case -ECONNRESET: | |
fb9987d0 S |
79 | case -ENODEV: |
80 | case -ESHUTDOWN: | |
6f0f2669 | 81 | goto free; |
fb9987d0 S |
82 | default: |
83 | break; | |
84 | } | |
85 | ||
86 | if (cmd) { | |
87 | ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle, | |
2f80194c | 88 | cmd->skb, true); |
fb9987d0 | 89 | kfree(cmd); |
fb9987d0 | 90 | } |
6f0f2669 S |
91 | |
92 | return; | |
93 | free: | |
0fa35a58 | 94 | kfree_skb(cmd->skb); |
6f0f2669 | 95 | kfree(cmd); |
fb9987d0 S |
96 | } |
97 | ||
98 | static int hif_usb_send_regout(struct hif_device_usb *hif_dev, | |
99 | struct sk_buff *skb) | |
100 | { | |
101 | struct urb *urb; | |
102 | struct cmd_buf *cmd; | |
103 | int ret = 0; | |
104 | ||
105 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
106 | if (urb == NULL) | |
107 | return -ENOMEM; | |
108 | ||
109 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); | |
110 | if (cmd == NULL) { | |
111 | usb_free_urb(urb); | |
112 | return -ENOMEM; | |
113 | } | |
114 | ||
115 | cmd->skb = skb; | |
116 | cmd->hif_dev = hif_dev; | |
117 | ||
4a0e8ecc RM |
118 | usb_fill_bulk_urb(urb, hif_dev->udev, |
119 | usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE), | |
fb9987d0 | 120 | skb->data, skb->len, |
4a0e8ecc | 121 | hif_usb_regout_cb, cmd); |
fb9987d0 | 122 | |
6f0f2669 | 123 | usb_anchor_urb(urb, &hif_dev->regout_submitted); |
fb9987d0 S |
124 | ret = usb_submit_urb(urb, GFP_KERNEL); |
125 | if (ret) { | |
6f0f2669 | 126 | usb_unanchor_urb(urb); |
fb9987d0 S |
127 | kfree(cmd); |
128 | } | |
6f0f2669 | 129 | usb_free_urb(urb); |
fb9987d0 S |
130 | |
131 | return ret; | |
132 | } | |
133 | ||
2f80194c SM |
134 | static void hif_usb_mgmt_cb(struct urb *urb) |
135 | { | |
136 | struct cmd_buf *cmd = (struct cmd_buf *)urb->context; | |
02c5172c | 137 | struct hif_device_usb *hif_dev; |
2f80194c SM |
138 | bool txok = true; |
139 | ||
140 | if (!cmd || !cmd->skb || !cmd->hif_dev) | |
141 | return; | |
142 | ||
02c5172c RM |
143 | hif_dev = cmd->hif_dev; |
144 | ||
2f80194c SM |
145 | switch (urb->status) { |
146 | case 0: | |
147 | break; | |
148 | case -ENOENT: | |
149 | case -ECONNRESET: | |
150 | case -ENODEV: | |
151 | case -ESHUTDOWN: | |
152 | txok = false; | |
153 | ||
154 | /* | |
155 | * If the URBs are being flushed, no need to complete | |
156 | * this packet. | |
157 | */ | |
158 | spin_lock(&hif_dev->tx.tx_lock); | |
159 | if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) { | |
160 | spin_unlock(&hif_dev->tx.tx_lock); | |
161 | dev_kfree_skb_any(cmd->skb); | |
162 | kfree(cmd); | |
163 | return; | |
164 | } | |
165 | spin_unlock(&hif_dev->tx.tx_lock); | |
166 | ||
167 | break; | |
168 | default: | |
169 | txok = false; | |
170 | break; | |
171 | } | |
172 | ||
173 | skb_pull(cmd->skb, 4); | |
174 | ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle, | |
175 | cmd->skb, txok); | |
176 | kfree(cmd); | |
177 | } | |
178 | ||
179 | static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev, | |
180 | struct sk_buff *skb) | |
181 | { | |
182 | struct urb *urb; | |
183 | struct cmd_buf *cmd; | |
184 | int ret = 0; | |
185 | __le16 *hdr; | |
186 | ||
187 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
188 | if (urb == NULL) | |
189 | return -ENOMEM; | |
190 | ||
191 | cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC); | |
192 | if (cmd == NULL) { | |
193 | usb_free_urb(urb); | |
194 | return -ENOMEM; | |
195 | } | |
196 | ||
197 | cmd->skb = skb; | |
198 | cmd->hif_dev = hif_dev; | |
199 | ||
200 | hdr = (__le16 *) skb_push(skb, 4); | |
201 | *hdr++ = cpu_to_le16(skb->len - 4); | |
202 | *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG); | |
203 | ||
204 | usb_fill_bulk_urb(urb, hif_dev->udev, | |
205 | usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE), | |
206 | skb->data, skb->len, | |
207 | hif_usb_mgmt_cb, cmd); | |
208 | ||
209 | usb_anchor_urb(urb, &hif_dev->mgmt_submitted); | |
210 | ret = usb_submit_urb(urb, GFP_ATOMIC); | |
211 | if (ret) { | |
212 | usb_unanchor_urb(urb); | |
213 | kfree(cmd); | |
214 | } | |
215 | usb_free_urb(urb); | |
216 | ||
217 | return ret; | |
218 | } | |
219 | ||
c11d8f89 S |
220 | static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev, |
221 | struct sk_buff_head *list) | |
222 | { | |
223 | struct sk_buff *skb; | |
224 | ||
225 | while ((skb = __skb_dequeue(list)) != NULL) { | |
226 | dev_kfree_skb_any(skb); | |
b587fc81 SM |
227 | } |
228 | } | |
229 | ||
230 | static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev, | |
231 | struct sk_buff_head *queue, | |
232 | bool txok) | |
233 | { | |
234 | struct sk_buff *skb; | |
235 | ||
236 | while ((skb = __skb_dequeue(queue)) != NULL) { | |
68185a4b BG |
237 | #ifdef CONFIG_ATH9K_HTC_DEBUGFS |
238 | int ln = skb->len; | |
239 | #endif | |
b587fc81 SM |
240 | ath9k_htc_txcompletion_cb(hif_dev->htc_handle, |
241 | skb, txok); | |
68185a4b | 242 | if (txok) { |
dfa8fc69 | 243 | TX_STAT_INC(skb_success); |
68185a4b BG |
244 | TX_STAT_ADD(skb_success_bytes, ln); |
245 | } | |
dfa8fc69 JL |
246 | else |
247 | TX_STAT_INC(skb_failed); | |
c11d8f89 S |
248 | } |
249 | } | |
250 | ||
fb9987d0 S |
251 | static void hif_usb_tx_cb(struct urb *urb) |
252 | { | |
253 | struct tx_buf *tx_buf = (struct tx_buf *) urb->context; | |
690e781c | 254 | struct hif_device_usb *hif_dev; |
b587fc81 | 255 | bool txok = true; |
fb9987d0 | 256 | |
690e781c | 257 | if (!tx_buf || !tx_buf->hif_dev) |
fb9987d0 S |
258 | return; |
259 | ||
690e781c DC |
260 | hif_dev = tx_buf->hif_dev; |
261 | ||
fb9987d0 S |
262 | switch (urb->status) { |
263 | case 0: | |
264 | break; | |
265 | case -ENOENT: | |
266 | case -ECONNRESET: | |
fb9987d0 S |
267 | case -ENODEV: |
268 | case -ESHUTDOWN: | |
b587fc81 | 269 | txok = false; |
ff8f59b5 SM |
270 | |
271 | /* | |
272 | * If the URBs are being flushed, no need to add this | |
273 | * URB to the free list. | |
274 | */ | |
275 | spin_lock(&hif_dev->tx.tx_lock); | |
276 | if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) { | |
277 | spin_unlock(&hif_dev->tx.tx_lock); | |
b587fc81 | 278 | ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue); |
ff8f59b5 SM |
279 | return; |
280 | } | |
281 | spin_unlock(&hif_dev->tx.tx_lock); | |
282 | ||
b587fc81 | 283 | break; |
fb9987d0 | 284 | default: |
b587fc81 | 285 | txok = false; |
fb9987d0 S |
286 | break; |
287 | } | |
288 | ||
b587fc81 | 289 | ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok); |
c11d8f89 | 290 | |
c11d8f89 S |
291 | /* Re-initialize the SKB queue */ |
292 | tx_buf->len = tx_buf->offset = 0; | |
293 | __skb_queue_head_init(&tx_buf->skb_queue); | |
294 | ||
295 | /* Add this TX buffer to the free list */ | |
296 | spin_lock(&hif_dev->tx.tx_lock); | |
297 | list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf); | |
298 | hif_dev->tx.tx_buf_cnt++; | |
299 | if (!(hif_dev->tx.flags & HIF_USB_TX_STOP)) | |
300 | __hif_usb_tx(hif_dev); /* Check for pending SKBs */ | |
301 | TX_STAT_INC(buf_completed); | |
302 | spin_unlock(&hif_dev->tx.tx_lock); | |
f8e1d080 ML |
303 | } |
304 | ||
fb9987d0 S |
305 | /* TX lock has to be taken */ |
306 | static int __hif_usb_tx(struct hif_device_usb *hif_dev) | |
307 | { | |
308 | struct tx_buf *tx_buf = NULL; | |
309 | struct sk_buff *nskb = NULL; | |
310 | int ret = 0, i; | |
2c27392d | 311 | u16 tx_skb_cnt = 0; |
fb9987d0 | 312 | u8 *buf; |
2c27392d | 313 | __le16 *hdr; |
fb9987d0 S |
314 | |
315 | if (hif_dev->tx.tx_skb_cnt == 0) | |
316 | return 0; | |
317 | ||
318 | /* Check if a free TX buffer is available */ | |
319 | if (list_empty(&hif_dev->tx.tx_buf)) | |
320 | return 0; | |
321 | ||
322 | tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list); | |
c11d8f89 | 323 | list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending); |
fb9987d0 S |
324 | hif_dev->tx.tx_buf_cnt--; |
325 | ||
326 | tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM); | |
327 | ||
328 | for (i = 0; i < tx_skb_cnt; i++) { | |
329 | nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue); | |
330 | ||
331 | /* Should never be NULL */ | |
332 | BUG_ON(!nskb); | |
333 | ||
334 | hif_dev->tx.tx_skb_cnt--; | |
335 | ||
336 | buf = tx_buf->buf; | |
337 | buf += tx_buf->offset; | |
2c27392d SM |
338 | hdr = (__le16 *)buf; |
339 | *hdr++ = cpu_to_le16(nskb->len); | |
340 | *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG); | |
fb9987d0 S |
341 | buf += 4; |
342 | memcpy(buf, nskb->data, nskb->len); | |
343 | tx_buf->len = nskb->len + 4; | |
344 | ||
345 | if (i < (tx_skb_cnt - 1)) | |
346 | tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4; | |
347 | ||
348 | if (i == (tx_skb_cnt - 1)) | |
349 | tx_buf->len += tx_buf->offset; | |
350 | ||
351 | __skb_queue_tail(&tx_buf->skb_queue, nskb); | |
352 | TX_STAT_INC(skb_queued); | |
353 | } | |
354 | ||
355 | usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev, | |
356 | usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE), | |
357 | tx_buf->buf, tx_buf->len, | |
358 | hif_usb_tx_cb, tx_buf); | |
359 | ||
360 | ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC); | |
361 | if (ret) { | |
362 | tx_buf->len = tx_buf->offset = 0; | |
b587fc81 | 363 | ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false); |
fb9987d0 S |
364 | __skb_queue_head_init(&tx_buf->skb_queue); |
365 | list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf); | |
366 | hif_dev->tx.tx_buf_cnt++; | |
367 | } | |
368 | ||
369 | if (!ret) | |
370 | TX_STAT_INC(buf_queued); | |
371 | ||
372 | return ret; | |
373 | } | |
374 | ||
40dc9e4b | 375 | static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb) |
fb9987d0 | 376 | { |
40dc9e4b | 377 | struct ath9k_htc_tx_ctl *tx_ctl; |
fb9987d0 | 378 | unsigned long flags; |
2f80194c | 379 | int ret = 0; |
fb9987d0 S |
380 | |
381 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); | |
382 | ||
383 | if (hif_dev->tx.flags & HIF_USB_TX_STOP) { | |
384 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
385 | return -ENODEV; | |
386 | } | |
387 | ||
388 | /* Check if the max queue count has been reached */ | |
389 | if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) { | |
390 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
391 | return -ENOMEM; | |
392 | } | |
393 | ||
2f80194c | 394 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
fb9987d0 | 395 | |
40dc9e4b SM |
396 | tx_ctl = HTC_SKB_CB(skb); |
397 | ||
2f80194c SM |
398 | /* Mgmt/Beacon frames don't use the TX buffer pool */ |
399 | if ((tx_ctl->type == ATH9K_HTC_MGMT) || | |
400 | (tx_ctl->type == ATH9K_HTC_BEACON)) { | |
401 | ret = hif_usb_send_mgmt(hif_dev, skb); | |
402 | } | |
403 | ||
404 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); | |
405 | ||
406 | if ((tx_ctl->type == ATH9K_HTC_NORMAL) || | |
407 | (tx_ctl->type == ATH9K_HTC_AMPDU)) { | |
408 | __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb); | |
409 | hif_dev->tx.tx_skb_cnt++; | |
410 | } | |
fb9987d0 S |
411 | |
412 | /* Check if AMPDUs have to be sent immediately */ | |
2f80194c | 413 | if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) && |
fb9987d0 S |
414 | (hif_dev->tx.tx_skb_cnt < 2)) { |
415 | __hif_usb_tx(hif_dev); | |
416 | } | |
417 | ||
418 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
419 | ||
2f80194c | 420 | return ret; |
fb9987d0 S |
421 | } |
422 | ||
e1fe7c38 | 423 | static void hif_usb_start(void *hif_handle) |
fb9987d0 S |
424 | { |
425 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; | |
426 | unsigned long flags; | |
427 | ||
428 | hif_dev->flags |= HIF_USB_START; | |
429 | ||
430 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); | |
431 | hif_dev->tx.flags &= ~HIF_USB_TX_STOP; | |
432 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
433 | } | |
434 | ||
e1fe7c38 | 435 | static void hif_usb_stop(void *hif_handle) |
fb9987d0 S |
436 | { |
437 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; | |
ff8f59b5 | 438 | struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL; |
fb9987d0 S |
439 | unsigned long flags; |
440 | ||
441 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); | |
b587fc81 | 442 | ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false); |
fb9987d0 S |
443 | hif_dev->tx.tx_skb_cnt = 0; |
444 | hif_dev->tx.flags |= HIF_USB_TX_STOP; | |
445 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
ff8f59b5 SM |
446 | |
447 | /* The pending URBs have to be canceled. */ | |
448 | list_for_each_entry_safe(tx_buf, tx_buf_tmp, | |
449 | &hif_dev->tx.tx_pending, list) { | |
450 | usb_kill_urb(tx_buf->urb); | |
451 | } | |
2f80194c SM |
452 | |
453 | usb_kill_anchored_urbs(&hif_dev->mgmt_submitted); | |
fb9987d0 S |
454 | } |
455 | ||
40dc9e4b | 456 | static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb) |
fb9987d0 S |
457 | { |
458 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; | |
459 | int ret = 0; | |
460 | ||
461 | switch (pipe_id) { | |
462 | case USB_WLAN_TX_PIPE: | |
40dc9e4b | 463 | ret = hif_usb_send_tx(hif_dev, skb); |
fb9987d0 S |
464 | break; |
465 | case USB_REG_OUT_PIPE: | |
466 | ret = hif_usb_send_regout(hif_dev, skb); | |
467 | break; | |
468 | default: | |
6335ed0f S |
469 | dev_err(&hif_dev->udev->dev, |
470 | "ath9k_htc: Invalid TX pipe: %d\n", pipe_id); | |
fb9987d0 S |
471 | ret = -EINVAL; |
472 | break; | |
473 | } | |
474 | ||
475 | return ret; | |
476 | } | |
477 | ||
84c9e164 SM |
478 | static inline bool check_index(struct sk_buff *skb, u8 idx) |
479 | { | |
480 | struct ath9k_htc_tx_ctl *tx_ctl; | |
481 | ||
482 | tx_ctl = HTC_SKB_CB(skb); | |
483 | ||
484 | if ((tx_ctl->type == ATH9K_HTC_AMPDU) && | |
485 | (tx_ctl->sta_idx == idx)) | |
486 | return true; | |
487 | ||
488 | return false; | |
489 | } | |
490 | ||
491 | static void hif_usb_sta_drain(void *hif_handle, u8 idx) | |
492 | { | |
493 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; | |
494 | struct sk_buff *skb, *tmp; | |
495 | unsigned long flags; | |
496 | ||
497 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); | |
498 | ||
499 | skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) { | |
500 | if (check_index(skb, idx)) { | |
501 | __skb_unlink(skb, &hif_dev->tx.tx_skb_queue); | |
502 | ath9k_htc_txcompletion_cb(hif_dev->htc_handle, | |
503 | skb, false); | |
504 | hif_dev->tx.tx_skb_cnt--; | |
505 | TX_STAT_INC(skb_failed); | |
506 | } | |
507 | } | |
508 | ||
509 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
510 | } | |
511 | ||
fb9987d0 S |
512 | static struct ath9k_htc_hif hif_usb = { |
513 | .transport = ATH9K_HIF_USB, | |
514 | .name = "ath9k_hif_usb", | |
515 | ||
516 | .control_ul_pipe = USB_REG_OUT_PIPE, | |
517 | .control_dl_pipe = USB_REG_IN_PIPE, | |
518 | ||
519 | .start = hif_usb_start, | |
520 | .stop = hif_usb_stop, | |
84c9e164 | 521 | .sta_drain = hif_usb_sta_drain, |
fb9987d0 S |
522 | .send = hif_usb_send, |
523 | }; | |
524 | ||
525 | static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev, | |
526 | struct sk_buff *skb) | |
527 | { | |
c503269a | 528 | struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER]; |
44b23b48 JP |
529 | int index = 0, i = 0, len = skb->len; |
530 | int rx_remain_len, rx_pkt_len; | |
531 | u16 pool_index = 0; | |
fb9987d0 S |
532 | u8 *ptr; |
533 | ||
46baa1a2 S |
534 | spin_lock(&hif_dev->rx_lock); |
535 | ||
fb9987d0 S |
536 | rx_remain_len = hif_dev->rx_remain_len; |
537 | rx_pkt_len = hif_dev->rx_transfer_len; | |
538 | ||
539 | if (rx_remain_len != 0) { | |
540 | struct sk_buff *remain_skb = hif_dev->remain_skb; | |
541 | ||
542 | if (remain_skb) { | |
543 | ptr = (u8 *) remain_skb->data; | |
544 | ||
545 | index = rx_remain_len; | |
546 | rx_remain_len -= hif_dev->rx_pad_len; | |
547 | ptr += rx_pkt_len; | |
548 | ||
549 | memcpy(ptr, skb->data, rx_remain_len); | |
550 | ||
551 | rx_pkt_len += rx_remain_len; | |
552 | hif_dev->rx_remain_len = 0; | |
553 | skb_put(remain_skb, rx_pkt_len); | |
554 | ||
555 | skb_pool[pool_index++] = remain_skb; | |
556 | ||
557 | } else { | |
558 | index = rx_remain_len; | |
559 | } | |
560 | } | |
561 | ||
46baa1a2 S |
562 | spin_unlock(&hif_dev->rx_lock); |
563 | ||
fb9987d0 | 564 | while (index < len) { |
44b23b48 JP |
565 | u16 pkt_len; |
566 | u16 pkt_tag; | |
567 | u16 pad_len; | |
568 | int chk_idx; | |
569 | ||
fb9987d0 S |
570 | ptr = (u8 *) skb->data; |
571 | ||
78fa99ab PR |
572 | pkt_len = get_unaligned_le16(ptr + index); |
573 | pkt_tag = get_unaligned_le16(ptr + index + 2); | |
fb9987d0 | 574 | |
44b23b48 JP |
575 | if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) { |
576 | RX_STAT_INC(skb_dropped); | |
577 | return; | |
578 | } | |
579 | ||
580 | pad_len = 4 - (pkt_len & 0x3); | |
581 | if (pad_len == 4) | |
582 | pad_len = 0; | |
583 | ||
584 | chk_idx = index; | |
585 | index = index + 4 + pkt_len + pad_len; | |
586 | ||
587 | if (index > MAX_RX_BUF_SIZE) { | |
588 | spin_lock(&hif_dev->rx_lock); | |
589 | hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE; | |
590 | hif_dev->rx_transfer_len = | |
591 | MAX_RX_BUF_SIZE - chk_idx - 4; | |
592 | hif_dev->rx_pad_len = pad_len; | |
593 | ||
594 | nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC); | |
595 | if (!nskb) { | |
596 | dev_err(&hif_dev->udev->dev, | |
597 | "ath9k_htc: RX memory allocation error\n"); | |
46baa1a2 | 598 | spin_unlock(&hif_dev->rx_lock); |
44b23b48 | 599 | goto err; |
fb9987d0 | 600 | } |
44b23b48 JP |
601 | skb_reserve(nskb, 32); |
602 | RX_STAT_INC(skb_allocated); | |
603 | ||
604 | memcpy(nskb->data, &(skb->data[chk_idx+4]), | |
605 | hif_dev->rx_transfer_len); | |
606 | ||
607 | /* Record the buffer pointer */ | |
608 | hif_dev->remain_skb = nskb; | |
609 | spin_unlock(&hif_dev->rx_lock); | |
fb9987d0 | 610 | } else { |
44b23b48 JP |
611 | nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC); |
612 | if (!nskb) { | |
613 | dev_err(&hif_dev->udev->dev, | |
614 | "ath9k_htc: RX memory allocation error\n"); | |
615 | goto err; | |
616 | } | |
617 | skb_reserve(nskb, 32); | |
618 | RX_STAT_INC(skb_allocated); | |
619 | ||
620 | memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len); | |
621 | skb_put(nskb, pkt_len); | |
622 | skb_pool[pool_index++] = nskb; | |
fb9987d0 S |
623 | } |
624 | } | |
625 | ||
626 | err: | |
fb9987d0 | 627 | for (i = 0; i < pool_index; i++) { |
68185a4b | 628 | RX_STAT_ADD(skb_completed_bytes, skb_pool[i]->len); |
fb9987d0 S |
629 | ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i], |
630 | skb_pool[i]->len, USB_WLAN_RX_PIPE); | |
631 | RX_STAT_INC(skb_completed); | |
632 | } | |
633 | } | |
634 | ||
635 | static void ath9k_hif_usb_rx_cb(struct urb *urb) | |
636 | { | |
637 | struct sk_buff *skb = (struct sk_buff *) urb->context; | |
b2767363 | 638 | struct hif_device_usb *hif_dev = |
fb9987d0 S |
639 | usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0)); |
640 | int ret; | |
641 | ||
6335ed0f S |
642 | if (!skb) |
643 | return; | |
644 | ||
fb9987d0 S |
645 | if (!hif_dev) |
646 | goto free; | |
647 | ||
648 | switch (urb->status) { | |
649 | case 0: | |
650 | break; | |
651 | case -ENOENT: | |
652 | case -ECONNRESET: | |
653 | case -ENODEV: | |
654 | case -ESHUTDOWN: | |
655 | goto free; | |
656 | default: | |
657 | goto resubmit; | |
658 | } | |
659 | ||
660 | if (likely(urb->actual_length != 0)) { | |
661 | skb_put(skb, urb->actual_length); | |
fb9987d0 | 662 | ath9k_hif_usb_rx_stream(hif_dev, skb); |
fb9987d0 S |
663 | } |
664 | ||
665 | resubmit: | |
666 | skb_reset_tail_pointer(skb); | |
667 | skb_trim(skb, 0); | |
668 | ||
6335ed0f | 669 | usb_anchor_urb(urb, &hif_dev->rx_submitted); |
fb9987d0 | 670 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
6335ed0f S |
671 | if (ret) { |
672 | usb_unanchor_urb(urb); | |
fb9987d0 | 673 | goto free; |
6335ed0f | 674 | } |
fb9987d0 S |
675 | |
676 | return; | |
677 | free: | |
f28a7b30 | 678 | kfree_skb(skb); |
fb9987d0 S |
679 | } |
680 | ||
681 | static void ath9k_hif_usb_reg_in_cb(struct urb *urb) | |
682 | { | |
683 | struct sk_buff *skb = (struct sk_buff *) urb->context; | |
684 | struct sk_buff *nskb; | |
b2767363 | 685 | struct hif_device_usb *hif_dev = |
fb9987d0 S |
686 | usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0)); |
687 | int ret; | |
688 | ||
6335ed0f S |
689 | if (!skb) |
690 | return; | |
691 | ||
fb9987d0 S |
692 | if (!hif_dev) |
693 | goto free; | |
694 | ||
695 | switch (urb->status) { | |
696 | case 0: | |
697 | break; | |
698 | case -ENOENT: | |
699 | case -ECONNRESET: | |
700 | case -ENODEV: | |
701 | case -ESHUTDOWN: | |
702 | goto free; | |
703 | default: | |
3deff760 SM |
704 | skb_reset_tail_pointer(skb); |
705 | skb_trim(skb, 0); | |
706 | ||
fb9987d0 S |
707 | goto resubmit; |
708 | } | |
709 | ||
710 | if (likely(urb->actual_length != 0)) { | |
711 | skb_put(skb, urb->actual_length); | |
712 | ||
5ab0af32 S |
713 | /* Process the command first */ |
714 | ath9k_htc_rx_msg(hif_dev->htc_handle, skb, | |
715 | skb->len, USB_REG_IN_PIPE); | |
716 | ||
717 | ||
e6c6d33c | 718 | nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC); |
5ab0af32 S |
719 | if (!nskb) { |
720 | dev_err(&hif_dev->udev->dev, | |
721 | "ath9k_htc: REG_IN memory allocation failure\n"); | |
722 | urb->context = NULL; | |
723 | return; | |
724 | } | |
fb9987d0 | 725 | |
490b3f4e | 726 | usb_fill_bulk_urb(urb, hif_dev->udev, |
0f529e98 RM |
727 | usb_rcvbulkpipe(hif_dev->udev, |
728 | USB_REG_IN_PIPE), | |
fb9987d0 | 729 | nskb->data, MAX_REG_IN_BUF_SIZE, |
490b3f4e | 730 | ath9k_hif_usb_reg_in_cb, nskb); |
fb9987d0 S |
731 | } |
732 | ||
733 | resubmit: | |
3deff760 | 734 | usb_anchor_urb(urb, &hif_dev->reg_in_submitted); |
fb9987d0 | 735 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
3deff760 SM |
736 | if (ret) { |
737 | usb_unanchor_urb(urb); | |
fb9987d0 | 738 | goto free; |
3deff760 | 739 | } |
fb9987d0 S |
740 | |
741 | return; | |
742 | free: | |
e6c6d33c | 743 | kfree_skb(skb); |
6335ed0f | 744 | urb->context = NULL; |
fb9987d0 S |
745 | } |
746 | ||
747 | static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev) | |
748 | { | |
fb9987d0 | 749 | struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL; |
ff8f59b5 | 750 | unsigned long flags; |
fb9987d0 | 751 | |
c11d8f89 S |
752 | list_for_each_entry_safe(tx_buf, tx_buf_tmp, |
753 | &hif_dev->tx.tx_buf, list) { | |
754 | usb_kill_urb(tx_buf->urb); | |
fb9987d0 S |
755 | list_del(&tx_buf->list); |
756 | usb_free_urb(tx_buf->urb); | |
757 | kfree(tx_buf->buf); | |
758 | kfree(tx_buf); | |
759 | } | |
760 | ||
ff8f59b5 SM |
761 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); |
762 | hif_dev->tx.flags |= HIF_USB_TX_FLUSH; | |
763 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
764 | ||
fb9987d0 S |
765 | list_for_each_entry_safe(tx_buf, tx_buf_tmp, |
766 | &hif_dev->tx.tx_pending, list) { | |
767 | usb_kill_urb(tx_buf->urb); | |
768 | list_del(&tx_buf->list); | |
769 | usb_free_urb(tx_buf->urb); | |
770 | kfree(tx_buf->buf); | |
771 | kfree(tx_buf); | |
772 | } | |
2f80194c SM |
773 | |
774 | usb_kill_anchored_urbs(&hif_dev->mgmt_submitted); | |
fb9987d0 S |
775 | } |
776 | ||
777 | static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev) | |
778 | { | |
779 | struct tx_buf *tx_buf; | |
780 | int i; | |
781 | ||
782 | INIT_LIST_HEAD(&hif_dev->tx.tx_buf); | |
783 | INIT_LIST_HEAD(&hif_dev->tx.tx_pending); | |
784 | spin_lock_init(&hif_dev->tx.tx_lock); | |
785 | __skb_queue_head_init(&hif_dev->tx.tx_skb_queue); | |
2f80194c | 786 | init_usb_anchor(&hif_dev->mgmt_submitted); |
fb9987d0 S |
787 | |
788 | for (i = 0; i < MAX_TX_URB_NUM; i++) { | |
789 | tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL); | |
790 | if (!tx_buf) | |
791 | goto err; | |
792 | ||
793 | tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL); | |
794 | if (!tx_buf->buf) | |
795 | goto err; | |
796 | ||
797 | tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL); | |
798 | if (!tx_buf->urb) | |
799 | goto err; | |
800 | ||
801 | tx_buf->hif_dev = hif_dev; | |
802 | __skb_queue_head_init(&tx_buf->skb_queue); | |
803 | ||
804 | list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf); | |
805 | } | |
806 | ||
807 | hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM; | |
808 | ||
809 | return 0; | |
810 | err: | |
7606688a DC |
811 | if (tx_buf) { |
812 | kfree(tx_buf->buf); | |
813 | kfree(tx_buf); | |
814 | } | |
fb9987d0 S |
815 | ath9k_hif_usb_dealloc_tx_urbs(hif_dev); |
816 | return -ENOMEM; | |
817 | } | |
818 | ||
fb9987d0 S |
819 | static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev) |
820 | { | |
6335ed0f | 821 | usb_kill_anchored_urbs(&hif_dev->rx_submitted); |
fb9987d0 S |
822 | } |
823 | ||
824 | static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev) | |
825 | { | |
6335ed0f S |
826 | struct urb *urb = NULL; |
827 | struct sk_buff *skb = NULL; | |
fb9987d0 S |
828 | int i, ret; |
829 | ||
6335ed0f | 830 | init_usb_anchor(&hif_dev->rx_submitted); |
46baa1a2 | 831 | spin_lock_init(&hif_dev->rx_lock); |
6335ed0f | 832 | |
fb9987d0 S |
833 | for (i = 0; i < MAX_RX_URB_NUM; i++) { |
834 | ||
835 | /* Allocate URB */ | |
6335ed0f S |
836 | urb = usb_alloc_urb(0, GFP_KERNEL); |
837 | if (urb == NULL) { | |
fb9987d0 | 838 | ret = -ENOMEM; |
6335ed0f | 839 | goto err_urb; |
fb9987d0 S |
840 | } |
841 | ||
842 | /* Allocate buffer */ | |
f28a7b30 | 843 | skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL); |
6335ed0f S |
844 | if (!skb) { |
845 | ret = -ENOMEM; | |
846 | goto err_skb; | |
847 | } | |
fb9987d0 | 848 | |
6335ed0f S |
849 | usb_fill_bulk_urb(urb, hif_dev->udev, |
850 | usb_rcvbulkpipe(hif_dev->udev, | |
851 | USB_WLAN_RX_PIPE), | |
852 | skb->data, MAX_RX_BUF_SIZE, | |
853 | ath9k_hif_usb_rx_cb, skb); | |
854 | ||
855 | /* Anchor URB */ | |
856 | usb_anchor_urb(urb, &hif_dev->rx_submitted); | |
fb9987d0 | 857 | |
6335ed0f S |
858 | /* Submit URB */ |
859 | ret = usb_submit_urb(urb, GFP_KERNEL); | |
860 | if (ret) { | |
861 | usb_unanchor_urb(urb); | |
862 | goto err_submit; | |
863 | } | |
66b10e33 S |
864 | |
865 | /* | |
866 | * Drop reference count. | |
867 | * This ensures that the URB is freed when killing them. | |
868 | */ | |
869 | usb_free_urb(urb); | |
fb9987d0 S |
870 | } |
871 | ||
872 | return 0; | |
873 | ||
6335ed0f | 874 | err_submit: |
f28a7b30 | 875 | kfree_skb(skb); |
6335ed0f S |
876 | err_skb: |
877 | usb_free_urb(urb); | |
878 | err_urb: | |
fb9987d0 S |
879 | ath9k_hif_usb_dealloc_rx_urbs(hif_dev); |
880 | return ret; | |
881 | } | |
882 | ||
3deff760 | 883 | static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev) |
fb9987d0 | 884 | { |
3deff760 | 885 | usb_kill_anchored_urbs(&hif_dev->reg_in_submitted); |
fb9987d0 S |
886 | } |
887 | ||
3deff760 | 888 | static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev) |
fb9987d0 | 889 | { |
3deff760 SM |
890 | struct urb *urb = NULL; |
891 | struct sk_buff *skb = NULL; | |
892 | int i, ret; | |
fb9987d0 | 893 | |
3deff760 | 894 | init_usb_anchor(&hif_dev->reg_in_submitted); |
fb9987d0 | 895 | |
3deff760 | 896 | for (i = 0; i < MAX_REG_IN_URB_NUM; i++) { |
fb9987d0 | 897 | |
3deff760 SM |
898 | /* Allocate URB */ |
899 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
900 | if (urb == NULL) { | |
901 | ret = -ENOMEM; | |
902 | goto err_urb; | |
903 | } | |
fb9987d0 | 904 | |
3deff760 SM |
905 | /* Allocate buffer */ |
906 | skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL); | |
907 | if (!skb) { | |
908 | ret = -ENOMEM; | |
909 | goto err_skb; | |
910 | } | |
911 | ||
912 | usb_fill_bulk_urb(urb, hif_dev->udev, | |
913 | usb_rcvbulkpipe(hif_dev->udev, | |
914 | USB_REG_IN_PIPE), | |
915 | skb->data, MAX_REG_IN_BUF_SIZE, | |
916 | ath9k_hif_usb_reg_in_cb, skb); | |
917 | ||
918 | /* Anchor URB */ | |
919 | usb_anchor_urb(urb, &hif_dev->reg_in_submitted); | |
920 | ||
921 | /* Submit URB */ | |
922 | ret = usb_submit_urb(urb, GFP_KERNEL); | |
923 | if (ret) { | |
924 | usb_unanchor_urb(urb); | |
925 | goto err_submit; | |
926 | } | |
927 | ||
928 | /* | |
929 | * Drop reference count. | |
930 | * This ensures that the URB is freed when killing them. | |
931 | */ | |
932 | usb_free_urb(urb); | |
933 | } | |
fb9987d0 S |
934 | |
935 | return 0; | |
936 | ||
3deff760 SM |
937 | err_submit: |
938 | kfree_skb(skb); | |
939 | err_skb: | |
940 | usb_free_urb(urb); | |
941 | err_urb: | |
942 | ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev); | |
943 | return ret; | |
fb9987d0 S |
944 | } |
945 | ||
946 | static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev) | |
947 | { | |
6f0f2669 S |
948 | /* Register Write */ |
949 | init_usb_anchor(&hif_dev->regout_submitted); | |
950 | ||
fb9987d0 S |
951 | /* TX */ |
952 | if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0) | |
953 | goto err; | |
954 | ||
955 | /* RX */ | |
956 | if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0) | |
f8036965 | 957 | goto err_rx; |
fb9987d0 | 958 | |
6f0f2669 | 959 | /* Register Read */ |
3deff760 | 960 | if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0) |
f8036965 | 961 | goto err_reg; |
fb9987d0 S |
962 | |
963 | return 0; | |
f8036965 RM |
964 | err_reg: |
965 | ath9k_hif_usb_dealloc_rx_urbs(hif_dev); | |
966 | err_rx: | |
967 | ath9k_hif_usb_dealloc_tx_urbs(hif_dev); | |
fb9987d0 S |
968 | err: |
969 | return -ENOMEM; | |
970 | } | |
971 | ||
1d8af8ca SM |
972 | static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev) |
973 | { | |
974 | usb_kill_anchored_urbs(&hif_dev->regout_submitted); | |
3deff760 | 975 | ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev); |
1d8af8ca SM |
976 | ath9k_hif_usb_dealloc_tx_urbs(hif_dev); |
977 | ath9k_hif_usb_dealloc_rx_urbs(hif_dev); | |
978 | } | |
979 | ||
0ed7b93e | 980 | static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev) |
fb9987d0 S |
981 | { |
982 | int transfer, err; | |
32e31de5 ML |
983 | const void *data = hif_dev->fw_data; |
984 | size_t len = hif_dev->fw_size; | |
fb9987d0 S |
985 | u32 addr = AR9271_FIRMWARE; |
986 | u8 *buf = kzalloc(4096, GFP_KERNEL); | |
b1762862 | 987 | u32 firm_offset; |
fb9987d0 S |
988 | |
989 | if (!buf) | |
990 | return -ENOMEM; | |
991 | ||
992 | while (len) { | |
291689fc | 993 | transfer = min_t(size_t, len, 4096); |
fb9987d0 S |
994 | memcpy(buf, data, transfer); |
995 | ||
996 | err = usb_control_msg(hif_dev->udev, | |
997 | usb_sndctrlpipe(hif_dev->udev, 0), | |
998 | FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT, | |
999 | addr >> 8, 0, buf, transfer, HZ); | |
1000 | if (err < 0) { | |
1001 | kfree(buf); | |
1002 | return err; | |
1003 | } | |
1004 | ||
1005 | len -= transfer; | |
1006 | data += transfer; | |
1007 | addr += transfer; | |
1008 | } | |
1009 | kfree(buf); | |
1010 | ||
0ed7b93e | 1011 | if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info)) |
b1762862 | 1012 | firm_offset = AR7010_FIRMWARE_TEXT; |
fa6e15e0 | 1013 | else |
b1762862 S |
1014 | firm_offset = AR9271_FIRMWARE_TEXT; |
1015 | ||
fb9987d0 S |
1016 | /* |
1017 | * Issue FW download complete command to firmware. | |
1018 | */ | |
1019 | err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0), | |
1020 | FIRMWARE_DOWNLOAD_COMP, | |
1021 | 0x40 | USB_DIR_OUT, | |
b1762862 | 1022 | firm_offset >> 8, 0, NULL, 0, HZ); |
fb9987d0 S |
1023 | if (err) |
1024 | return -EIO; | |
1025 | ||
1026 | dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n", | |
32e31de5 | 1027 | hif_dev->fw_name, (unsigned long) hif_dev->fw_size); |
fb9987d0 S |
1028 | |
1029 | return 0; | |
1030 | } | |
1031 | ||
0ed7b93e | 1032 | static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev) |
fb9987d0 | 1033 | { |
4a0e8ecc RM |
1034 | struct usb_host_interface *alt = &hif_dev->interface->altsetting[0]; |
1035 | struct usb_endpoint_descriptor *endp; | |
0ed7b93e | 1036 | int ret, idx; |
fb9987d0 | 1037 | |
0ed7b93e | 1038 | ret = ath9k_hif_usb_download_fw(hif_dev); |
1d8af8ca SM |
1039 | if (ret) { |
1040 | dev_err(&hif_dev->udev->dev, | |
ce43cee5 S |
1041 | "ath9k_htc: Firmware - %s download failed\n", |
1042 | hif_dev->fw_name); | |
0ed7b93e | 1043 | return ret; |
1d8af8ca SM |
1044 | } |
1045 | ||
4a0e8ecc RM |
1046 | /* On downloading the firmware to the target, the USB descriptor of EP4 |
1047 | * is 'patched' to change the type of the endpoint to Bulk. This will | |
1048 | * bring down CPU usage during the scan period. | |
1049 | */ | |
1050 | for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) { | |
1051 | endp = &alt->endpoint[idx].desc; | |
490b3f4e RM |
1052 | if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
1053 | == USB_ENDPOINT_XFER_INT) { | |
4a0e8ecc RM |
1054 | endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK; |
1055 | endp->bmAttributes |= USB_ENDPOINT_XFER_BULK; | |
1056 | endp->bInterval = 0; | |
1057 | } | |
1058 | } | |
1059 | ||
490b3f4e RM |
1060 | /* Alloc URBs */ |
1061 | ret = ath9k_hif_usb_alloc_urbs(hif_dev); | |
1062 | if (ret) { | |
1063 | dev_err(&hif_dev->udev->dev, | |
1064 | "ath9k_htc: Unable to allocate URBs\n"); | |
0ed7b93e | 1065 | return ret; |
490b3f4e RM |
1066 | } |
1067 | ||
fb9987d0 | 1068 | return 0; |
fb9987d0 S |
1069 | } |
1070 | ||
fb9987d0 S |
1071 | static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev) |
1072 | { | |
1073 | ath9k_hif_usb_dealloc_urbs(hif_dev); | |
0ed7b93e SM |
1074 | } |
1075 | ||
1076 | /* | |
1077 | * If initialization fails or the FW cannot be retrieved, | |
1078 | * detach the device. | |
1079 | */ | |
1080 | static void ath9k_hif_usb_firmware_fail(struct hif_device_usb *hif_dev) | |
1081 | { | |
e962610f ML |
1082 | struct device *dev = &hif_dev->udev->dev; |
1083 | struct device *parent = dev->parent; | |
0ed7b93e | 1084 | |
9494849e | 1085 | complete_all(&hif_dev->fw_done); |
0ed7b93e SM |
1086 | |
1087 | if (parent) | |
1088 | device_lock(parent); | |
1089 | ||
e962610f | 1090 | device_release_driver(dev); |
0ed7b93e SM |
1091 | |
1092 | if (parent) | |
1093 | device_unlock(parent); | |
1094 | } | |
1095 | ||
1096 | static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context) | |
1097 | { | |
1098 | struct hif_device_usb *hif_dev = context; | |
1099 | int ret; | |
1100 | ||
1101 | if (!fw) { | |
1102 | dev_err(&hif_dev->udev->dev, | |
1103 | "ath9k_htc: Failed to get firmware %s\n", | |
1104 | hif_dev->fw_name); | |
1105 | goto err_fw; | |
1106 | } | |
1107 | ||
1108 | hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb, | |
1109 | &hif_dev->udev->dev); | |
32e31de5 ML |
1110 | if (hif_dev->htc_handle == NULL) |
1111 | goto err_dev_alloc; | |
0ed7b93e | 1112 | |
32e31de5 ML |
1113 | hif_dev->fw_data = fw->data; |
1114 | hif_dev->fw_size = fw->size; | |
0ed7b93e SM |
1115 | |
1116 | /* Proceed with initialization */ | |
1117 | ||
1118 | ret = ath9k_hif_usb_dev_init(hif_dev); | |
1119 | if (ret) | |
1120 | goto err_dev_init; | |
1121 | ||
1122 | ret = ath9k_htc_hw_init(hif_dev->htc_handle, | |
1123 | &hif_dev->interface->dev, | |
1124 | hif_dev->usb_device_id->idProduct, | |
1125 | hif_dev->udev->product, | |
1126 | hif_dev->usb_device_id->driver_info); | |
1127 | if (ret) { | |
1128 | ret = -EINVAL; | |
1129 | goto err_htc_hw_init; | |
1130 | } | |
1131 | ||
32e31de5 ML |
1132 | release_firmware(fw); |
1133 | hif_dev->flags |= HIF_USB_READY; | |
9494849e | 1134 | complete_all(&hif_dev->fw_done); |
0ed7b93e SM |
1135 | |
1136 | return; | |
1137 | ||
1138 | err_htc_hw_init: | |
1139 | ath9k_hif_usb_dev_deinit(hif_dev); | |
1140 | err_dev_init: | |
1141 | ath9k_htc_hw_free(hif_dev->htc_handle); | |
32e31de5 | 1142 | err_dev_alloc: |
0ed7b93e | 1143 | release_firmware(fw); |
0ed7b93e SM |
1144 | err_fw: |
1145 | ath9k_hif_usb_firmware_fail(hif_dev); | |
fb9987d0 S |
1146 | } |
1147 | ||
36bcce43 SM |
1148 | /* |
1149 | * An exact copy of the function from zd1211rw. | |
1150 | */ | |
1151 | static int send_eject_command(struct usb_interface *interface) | |
1152 | { | |
1153 | struct usb_device *udev = interface_to_usbdev(interface); | |
1154 | struct usb_host_interface *iface_desc = &interface->altsetting[0]; | |
1155 | struct usb_endpoint_descriptor *endpoint; | |
1156 | unsigned char *cmd; | |
1157 | u8 bulk_out_ep; | |
1158 | int r; | |
1159 | ||
1160 | /* Find bulk out endpoint */ | |
1161 | for (r = 1; r >= 0; r--) { | |
1162 | endpoint = &iface_desc->endpoint[r].desc; | |
1163 | if (usb_endpoint_dir_out(endpoint) && | |
1164 | usb_endpoint_xfer_bulk(endpoint)) { | |
1165 | bulk_out_ep = endpoint->bEndpointAddress; | |
1166 | break; | |
1167 | } | |
1168 | } | |
1169 | if (r == -1) { | |
1170 | dev_err(&udev->dev, | |
1171 | "ath9k_htc: Could not find bulk out endpoint\n"); | |
1172 | return -ENODEV; | |
1173 | } | |
1174 | ||
1175 | cmd = kzalloc(31, GFP_KERNEL); | |
1176 | if (cmd == NULL) | |
1177 | return -ENODEV; | |
1178 | ||
1179 | /* USB bulk command block */ | |
1180 | cmd[0] = 0x55; /* bulk command signature */ | |
1181 | cmd[1] = 0x53; /* bulk command signature */ | |
1182 | cmd[2] = 0x42; /* bulk command signature */ | |
1183 | cmd[3] = 0x43; /* bulk command signature */ | |
1184 | cmd[14] = 6; /* command length */ | |
1185 | ||
1186 | cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */ | |
1187 | cmd[19] = 0x2; /* eject disc */ | |
1188 | ||
1189 | dev_info(&udev->dev, "Ejecting storage device...\n"); | |
1190 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep), | |
1191 | cmd, 31, NULL, 2000); | |
1192 | kfree(cmd); | |
1193 | if (r) | |
1194 | return r; | |
1195 | ||
1196 | /* At this point, the device disconnects and reconnects with the real | |
1197 | * ID numbers. */ | |
1198 | ||
1199 | usb_set_intfdata(interface, NULL); | |
1200 | return 0; | |
1201 | } | |
1202 | ||
fb9987d0 S |
1203 | static int ath9k_hif_usb_probe(struct usb_interface *interface, |
1204 | const struct usb_device_id *id) | |
1205 | { | |
1206 | struct usb_device *udev = interface_to_usbdev(interface); | |
1207 | struct hif_device_usb *hif_dev; | |
fb9987d0 S |
1208 | int ret = 0; |
1209 | ||
36bcce43 SM |
1210 | if (id->driver_info == STORAGE_DEVICE) |
1211 | return send_eject_command(interface); | |
1212 | ||
fb9987d0 S |
1213 | hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL); |
1214 | if (!hif_dev) { | |
1215 | ret = -ENOMEM; | |
1216 | goto err_alloc; | |
1217 | } | |
1218 | ||
1219 | usb_get_dev(udev); | |
0ed7b93e | 1220 | |
fb9987d0 S |
1221 | hif_dev->udev = udev; |
1222 | hif_dev->interface = interface; | |
0ed7b93e | 1223 | hif_dev->usb_device_id = id; |
fb9987d0 S |
1224 | #ifdef CONFIG_PM |
1225 | udev->reset_resume = 1; | |
1226 | #endif | |
1227 | usb_set_intfdata(interface, hif_dev); | |
1228 | ||
0ed7b93e | 1229 | init_completion(&hif_dev->fw_done); |
47fce026 | 1230 | |
ce43cee5 S |
1231 | /* Find out which firmware to load */ |
1232 | ||
0b5ead91 | 1233 | if (IS_AR7010_DEVICE(id->driver_info)) |
9efabad2 | 1234 | hif_dev->fw_name = FIRMWARE_AR7010_1_1; |
fa6e15e0 | 1235 | else |
d0ee0ebe | 1236 | hif_dev->fw_name = FIRMWARE_AR9271; |
ce43cee5 | 1237 | |
0ed7b93e SM |
1238 | ret = request_firmware_nowait(THIS_MODULE, true, hif_dev->fw_name, |
1239 | &hif_dev->udev->dev, GFP_KERNEL, | |
1240 | hif_dev, ath9k_hif_usb_firmware_cb); | |
fb9987d0 | 1241 | if (ret) { |
0ed7b93e SM |
1242 | dev_err(&hif_dev->udev->dev, |
1243 | "ath9k_htc: Async request for firmware %s failed\n", | |
1244 | hif_dev->fw_name); | |
1245 | goto err_fw_req; | |
fb9987d0 S |
1246 | } |
1247 | ||
0ed7b93e SM |
1248 | dev_info(&hif_dev->udev->dev, "ath9k_htc: Firmware %s requested\n", |
1249 | hif_dev->fw_name); | |
fb9987d0 S |
1250 | |
1251 | return 0; | |
1252 | ||
0ed7b93e | 1253 | err_fw_req: |
fb9987d0 S |
1254 | usb_set_intfdata(interface, NULL); |
1255 | kfree(hif_dev); | |
1256 | usb_put_dev(udev); | |
1257 | err_alloc: | |
1258 | return ret; | |
1259 | } | |
1260 | ||
62e4716a S |
1261 | static void ath9k_hif_usb_reboot(struct usb_device *udev) |
1262 | { | |
1263 | u32 reboot_cmd = 0xffffffff; | |
1264 | void *buf; | |
1265 | int ret; | |
1266 | ||
a465a2cc | 1267 | buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL); |
62e4716a S |
1268 | if (!buf) |
1269 | return; | |
1270 | ||
62e4716a S |
1271 | ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE), |
1272 | buf, 4, NULL, HZ); | |
1273 | if (ret) | |
1274 | dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n"); | |
1275 | ||
1276 | kfree(buf); | |
1277 | } | |
1278 | ||
fb9987d0 S |
1279 | static void ath9k_hif_usb_disconnect(struct usb_interface *interface) |
1280 | { | |
1281 | struct usb_device *udev = interface_to_usbdev(interface); | |
b2767363 | 1282 | struct hif_device_usb *hif_dev = usb_get_intfdata(interface); |
97dcec57 | 1283 | bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false; |
fb9987d0 | 1284 | |
36bcce43 SM |
1285 | if (!hif_dev) |
1286 | return; | |
1287 | ||
0ed7b93e SM |
1288 | wait_for_completion(&hif_dev->fw_done); |
1289 | ||
32e31de5 | 1290 | if (hif_dev->flags & HIF_USB_READY) { |
0ed7b93e SM |
1291 | ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged); |
1292 | ath9k_htc_hw_free(hif_dev->htc_handle); | |
1293 | ath9k_hif_usb_dev_deinit(hif_dev); | |
0ed7b93e SM |
1294 | } |
1295 | ||
36bcce43 | 1296 | usb_set_intfdata(interface, NULL); |
fb9987d0 | 1297 | |
4928bd2e OR |
1298 | /* If firmware was loaded we should drop it |
1299 | * go back to first stage bootloader. */ | |
1300 | if (!unplugged && (hif_dev->flags & HIF_USB_READY)) | |
62e4716a | 1301 | ath9k_hif_usb_reboot(udev); |
fb9987d0 S |
1302 | |
1303 | kfree(hif_dev); | |
1304 | dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n"); | |
1305 | usb_put_dev(udev); | |
1306 | } | |
1307 | ||
1308 | #ifdef CONFIG_PM | |
1309 | static int ath9k_hif_usb_suspend(struct usb_interface *interface, | |
1310 | pm_message_t message) | |
1311 | { | |
b2767363 | 1312 | struct hif_device_usb *hif_dev = usb_get_intfdata(interface); |
fb9987d0 | 1313 | |
f933ebed SM |
1314 | /* |
1315 | * The device has to be set to FULLSLEEP mode in case no | |
1316 | * interface is up. | |
1317 | */ | |
1318 | if (!(hif_dev->flags & HIF_USB_START)) | |
1319 | ath9k_htc_suspend(hif_dev->htc_handle); | |
1320 | ||
9494849e AK |
1321 | wait_for_completion(&hif_dev->fw_done); |
1322 | ||
1323 | if (hif_dev->flags & HIF_USB_READY) | |
1324 | ath9k_hif_usb_dealloc_urbs(hif_dev); | |
fb9987d0 S |
1325 | |
1326 | return 0; | |
1327 | } | |
1328 | ||
1329 | static int ath9k_hif_usb_resume(struct usb_interface *interface) | |
1330 | { | |
b2767363 | 1331 | struct hif_device_usb *hif_dev = usb_get_intfdata(interface); |
fa6e15e0 | 1332 | struct htc_target *htc_handle = hif_dev->htc_handle; |
fb9987d0 | 1333 | int ret; |
32e31de5 | 1334 | const struct firmware *fw; |
fb9987d0 S |
1335 | |
1336 | ret = ath9k_hif_usb_alloc_urbs(hif_dev); | |
1337 | if (ret) | |
1338 | return ret; | |
1339 | ||
32e31de5 ML |
1340 | if (hif_dev->flags & HIF_USB_READY) { |
1341 | /* request cached firmware during suspend/resume cycle */ | |
1342 | ret = request_firmware(&fw, hif_dev->fw_name, | |
1343 | &hif_dev->udev->dev); | |
1344 | if (ret) | |
1345 | goto fail_resume; | |
1346 | ||
1347 | hif_dev->fw_data = fw->data; | |
1348 | hif_dev->fw_size = fw->size; | |
0ed7b93e | 1349 | ret = ath9k_hif_usb_download_fw(hif_dev); |
32e31de5 | 1350 | release_firmware(fw); |
fb9987d0 S |
1351 | if (ret) |
1352 | goto fail_resume; | |
1353 | } else { | |
1354 | ath9k_hif_usb_dealloc_urbs(hif_dev); | |
1355 | return -EIO; | |
1356 | } | |
1357 | ||
1358 | mdelay(100); | |
1359 | ||
fa6e15e0 | 1360 | ret = ath9k_htc_resume(htc_handle); |
fb9987d0 S |
1361 | |
1362 | if (ret) | |
1363 | goto fail_resume; | |
1364 | ||
1365 | return 0; | |
1366 | ||
1367 | fail_resume: | |
1368 | ath9k_hif_usb_dealloc_urbs(hif_dev); | |
1369 | ||
1370 | return ret; | |
1371 | } | |
1372 | #endif | |
1373 | ||
1374 | static struct usb_driver ath9k_hif_usb_driver = { | |
50f68712 | 1375 | .name = KBUILD_MODNAME, |
fb9987d0 S |
1376 | .probe = ath9k_hif_usb_probe, |
1377 | .disconnect = ath9k_hif_usb_disconnect, | |
1378 | #ifdef CONFIG_PM | |
1379 | .suspend = ath9k_hif_usb_suspend, | |
1380 | .resume = ath9k_hif_usb_resume, | |
1381 | .reset_resume = ath9k_hif_usb_resume, | |
1382 | #endif | |
1383 | .id_table = ath9k_hif_usb_ids, | |
1384 | .soft_unbind = 1, | |
e1f12eb6 | 1385 | .disable_hub_initiated_lpm = 1, |
fb9987d0 S |
1386 | }; |
1387 | ||
1388 | int ath9k_hif_usb_init(void) | |
1389 | { | |
1390 | return usb_register(&ath9k_hif_usb_driver); | |
1391 | } | |
1392 | ||
1393 | void ath9k_hif_usb_exit(void) | |
1394 | { | |
1395 | usb_deregister(&ath9k_hif_usb_driver); | |
1396 | } |