]>
Commit | Line | Data |
---|---|---|
fb9987d0 S |
1 | /* |
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 | ||
19 | #define ATH9K_FW_USB_DEV(devid, fw) \ | |
20 | { USB_DEVICE(0x0cf3, devid), .driver_info = (unsigned long) fw } | |
21 | ||
22 | static struct usb_device_id ath9k_hif_usb_ids[] = { | |
23 | ATH9K_FW_USB_DEV(0x9271, "ar9271.fw"), | |
e92119ca | 24 | ATH9K_FW_USB_DEV(0x1006, "ar9271.fw"), |
fb9987d0 S |
25 | { }, |
26 | }; | |
27 | ||
28 | MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids); | |
29 | ||
30 | static int __hif_usb_tx(struct hif_device_usb *hif_dev); | |
31 | ||
32 | static void hif_usb_regout_cb(struct urb *urb) | |
33 | { | |
34 | struct cmd_buf *cmd = (struct cmd_buf *)urb->context; | |
fb9987d0 S |
35 | |
36 | switch (urb->status) { | |
37 | case 0: | |
38 | break; | |
39 | case -ENOENT: | |
40 | case -ECONNRESET: | |
fb9987d0 S |
41 | case -ENODEV: |
42 | case -ESHUTDOWN: | |
6f0f2669 | 43 | goto free; |
fb9987d0 S |
44 | default: |
45 | break; | |
46 | } | |
47 | ||
48 | if (cmd) { | |
49 | ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle, | |
50 | cmd->skb, 1); | |
51 | kfree(cmd); | |
fb9987d0 | 52 | } |
6f0f2669 S |
53 | |
54 | return; | |
55 | free: | |
0fa35a58 | 56 | kfree_skb(cmd->skb); |
6f0f2669 | 57 | kfree(cmd); |
fb9987d0 S |
58 | } |
59 | ||
60 | static int hif_usb_send_regout(struct hif_device_usb *hif_dev, | |
61 | struct sk_buff *skb) | |
62 | { | |
63 | struct urb *urb; | |
64 | struct cmd_buf *cmd; | |
65 | int ret = 0; | |
66 | ||
67 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
68 | if (urb == NULL) | |
69 | return -ENOMEM; | |
70 | ||
71 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); | |
72 | if (cmd == NULL) { | |
73 | usb_free_urb(urb); | |
74 | return -ENOMEM; | |
75 | } | |
76 | ||
77 | cmd->skb = skb; | |
78 | cmd->hif_dev = hif_dev; | |
79 | ||
80 | usb_fill_int_urb(urb, hif_dev->udev, | |
81 | usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE), | |
82 | skb->data, skb->len, | |
83 | hif_usb_regout_cb, cmd, 1); | |
84 | ||
6f0f2669 | 85 | usb_anchor_urb(urb, &hif_dev->regout_submitted); |
fb9987d0 S |
86 | ret = usb_submit_urb(urb, GFP_KERNEL); |
87 | if (ret) { | |
6f0f2669 | 88 | usb_unanchor_urb(urb); |
fb9987d0 S |
89 | kfree(cmd); |
90 | } | |
6f0f2669 | 91 | usb_free_urb(urb); |
fb9987d0 S |
92 | |
93 | return ret; | |
94 | } | |
95 | ||
96 | static void hif_usb_tx_cb(struct urb *urb) | |
97 | { | |
98 | struct tx_buf *tx_buf = (struct tx_buf *) urb->context; | |
99 | struct hif_device_usb *hif_dev = tx_buf->hif_dev; | |
100 | struct sk_buff *skb; | |
101 | bool drop, flush; | |
102 | ||
103 | if (!hif_dev) | |
104 | return; | |
105 | ||
106 | switch (urb->status) { | |
107 | case 0: | |
108 | break; | |
109 | case -ENOENT: | |
110 | case -ECONNRESET: | |
111 | break; | |
112 | case -ENODEV: | |
113 | case -ESHUTDOWN: | |
114 | return; | |
115 | default: | |
116 | break; | |
117 | } | |
118 | ||
119 | if (tx_buf) { | |
120 | spin_lock(&hif_dev->tx.tx_lock); | |
121 | drop = !!(hif_dev->tx.flags & HIF_USB_TX_STOP); | |
122 | flush = !!(hif_dev->tx.flags & HIF_USB_TX_FLUSH); | |
123 | spin_unlock(&hif_dev->tx.tx_lock); | |
124 | ||
125 | while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) { | |
126 | if (!drop && !flush) { | |
127 | ath9k_htc_txcompletion_cb(hif_dev->htc_handle, | |
128 | skb, 1); | |
129 | TX_STAT_INC(skb_completed); | |
130 | } else { | |
131 | dev_kfree_skb_any(skb); | |
eac8e385 | 132 | TX_STAT_INC(skb_dropped); |
fb9987d0 S |
133 | } |
134 | } | |
135 | ||
136 | if (flush) | |
137 | return; | |
138 | ||
139 | tx_buf->len = tx_buf->offset = 0; | |
140 | __skb_queue_head_init(&tx_buf->skb_queue); | |
141 | ||
142 | spin_lock(&hif_dev->tx.tx_lock); | |
143 | list_del(&tx_buf->list); | |
144 | list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf); | |
145 | hif_dev->tx.tx_buf_cnt++; | |
146 | if (!drop) | |
147 | __hif_usb_tx(hif_dev); /* Check for pending SKBs */ | |
148 | TX_STAT_INC(buf_completed); | |
149 | spin_unlock(&hif_dev->tx.tx_lock); | |
150 | } | |
151 | } | |
152 | ||
eac8e385 S |
153 | static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev, |
154 | struct sk_buff_head *list) | |
f8e1d080 ML |
155 | { |
156 | struct sk_buff *skb; | |
eac8e385 S |
157 | |
158 | while ((skb = __skb_dequeue(list)) != NULL) { | |
f8e1d080 | 159 | dev_kfree_skb_any(skb); |
eac8e385 S |
160 | TX_STAT_INC(skb_dropped); |
161 | } | |
f8e1d080 ML |
162 | } |
163 | ||
fb9987d0 S |
164 | /* TX lock has to be taken */ |
165 | static int __hif_usb_tx(struct hif_device_usb *hif_dev) | |
166 | { | |
167 | struct tx_buf *tx_buf = NULL; | |
168 | struct sk_buff *nskb = NULL; | |
169 | int ret = 0, i; | |
170 | u16 *hdr, tx_skb_cnt = 0; | |
171 | u8 *buf; | |
172 | ||
173 | if (hif_dev->tx.tx_skb_cnt == 0) | |
174 | return 0; | |
175 | ||
176 | /* Check if a free TX buffer is available */ | |
177 | if (list_empty(&hif_dev->tx.tx_buf)) | |
178 | return 0; | |
179 | ||
180 | tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list); | |
181 | list_del(&tx_buf->list); | |
182 | list_add_tail(&tx_buf->list, &hif_dev->tx.tx_pending); | |
183 | hif_dev->tx.tx_buf_cnt--; | |
184 | ||
185 | tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM); | |
186 | ||
187 | for (i = 0; i < tx_skb_cnt; i++) { | |
188 | nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue); | |
189 | ||
190 | /* Should never be NULL */ | |
191 | BUG_ON(!nskb); | |
192 | ||
193 | hif_dev->tx.tx_skb_cnt--; | |
194 | ||
195 | buf = tx_buf->buf; | |
196 | buf += tx_buf->offset; | |
197 | hdr = (u16 *)buf; | |
198 | *hdr++ = nskb->len; | |
199 | *hdr++ = ATH_USB_TX_STREAM_MODE_TAG; | |
200 | buf += 4; | |
201 | memcpy(buf, nskb->data, nskb->len); | |
202 | tx_buf->len = nskb->len + 4; | |
203 | ||
204 | if (i < (tx_skb_cnt - 1)) | |
205 | tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4; | |
206 | ||
207 | if (i == (tx_skb_cnt - 1)) | |
208 | tx_buf->len += tx_buf->offset; | |
209 | ||
210 | __skb_queue_tail(&tx_buf->skb_queue, nskb); | |
211 | TX_STAT_INC(skb_queued); | |
212 | } | |
213 | ||
214 | usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev, | |
215 | usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE), | |
216 | tx_buf->buf, tx_buf->len, | |
217 | hif_usb_tx_cb, tx_buf); | |
218 | ||
219 | ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC); | |
220 | if (ret) { | |
221 | tx_buf->len = tx_buf->offset = 0; | |
eac8e385 | 222 | ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue); |
fb9987d0 S |
223 | __skb_queue_head_init(&tx_buf->skb_queue); |
224 | list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf); | |
225 | hif_dev->tx.tx_buf_cnt++; | |
226 | } | |
227 | ||
228 | if (!ret) | |
229 | TX_STAT_INC(buf_queued); | |
230 | ||
231 | return ret; | |
232 | } | |
233 | ||
234 | static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb, | |
235 | struct ath9k_htc_tx_ctl *tx_ctl) | |
236 | { | |
237 | unsigned long flags; | |
238 | ||
239 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); | |
240 | ||
241 | if (hif_dev->tx.flags & HIF_USB_TX_STOP) { | |
242 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
243 | return -ENODEV; | |
244 | } | |
245 | ||
246 | /* Check if the max queue count has been reached */ | |
247 | if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) { | |
248 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
249 | return -ENOMEM; | |
250 | } | |
251 | ||
252 | __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb); | |
253 | hif_dev->tx.tx_skb_cnt++; | |
254 | ||
255 | /* Send normal frames immediately */ | |
256 | if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL))) | |
257 | __hif_usb_tx(hif_dev); | |
258 | ||
259 | /* Check if AMPDUs have to be sent immediately */ | |
260 | if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) && | |
261 | (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) && | |
262 | (hif_dev->tx.tx_skb_cnt < 2)) { | |
263 | __hif_usb_tx(hif_dev); | |
264 | } | |
265 | ||
266 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
267 | ||
268 | return 0; | |
269 | } | |
270 | ||
271 | static void hif_usb_start(void *hif_handle, u8 pipe_id) | |
272 | { | |
273 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; | |
274 | unsigned long flags; | |
275 | ||
276 | hif_dev->flags |= HIF_USB_START; | |
277 | ||
278 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); | |
279 | hif_dev->tx.flags &= ~HIF_USB_TX_STOP; | |
280 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
281 | } | |
282 | ||
283 | static void hif_usb_stop(void *hif_handle, u8 pipe_id) | |
284 | { | |
285 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; | |
286 | unsigned long flags; | |
287 | ||
288 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); | |
eac8e385 | 289 | ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue); |
fb9987d0 S |
290 | hif_dev->tx.tx_skb_cnt = 0; |
291 | hif_dev->tx.flags |= HIF_USB_TX_STOP; | |
292 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
293 | } | |
294 | ||
295 | static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb, | |
296 | struct ath9k_htc_tx_ctl *tx_ctl) | |
297 | { | |
298 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; | |
299 | int ret = 0; | |
300 | ||
301 | switch (pipe_id) { | |
302 | case USB_WLAN_TX_PIPE: | |
303 | ret = hif_usb_send_tx(hif_dev, skb, tx_ctl); | |
304 | break; | |
305 | case USB_REG_OUT_PIPE: | |
306 | ret = hif_usb_send_regout(hif_dev, skb); | |
307 | break; | |
308 | default: | |
6335ed0f S |
309 | dev_err(&hif_dev->udev->dev, |
310 | "ath9k_htc: Invalid TX pipe: %d\n", pipe_id); | |
fb9987d0 S |
311 | ret = -EINVAL; |
312 | break; | |
313 | } | |
314 | ||
315 | return ret; | |
316 | } | |
317 | ||
318 | static struct ath9k_htc_hif hif_usb = { | |
319 | .transport = ATH9K_HIF_USB, | |
320 | .name = "ath9k_hif_usb", | |
321 | ||
322 | .control_ul_pipe = USB_REG_OUT_PIPE, | |
323 | .control_dl_pipe = USB_REG_IN_PIPE, | |
324 | ||
325 | .start = hif_usb_start, | |
326 | .stop = hif_usb_stop, | |
327 | .send = hif_usb_send, | |
328 | }; | |
329 | ||
330 | static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev, | |
331 | struct sk_buff *skb) | |
332 | { | |
c503269a | 333 | struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER]; |
fb9987d0 S |
334 | int index = 0, i = 0, chk_idx, len = skb->len; |
335 | int rx_remain_len = 0, rx_pkt_len = 0; | |
336 | u16 pkt_len, pkt_tag, pool_index = 0; | |
337 | u8 *ptr; | |
338 | ||
46baa1a2 S |
339 | spin_lock(&hif_dev->rx_lock); |
340 | ||
fb9987d0 S |
341 | rx_remain_len = hif_dev->rx_remain_len; |
342 | rx_pkt_len = hif_dev->rx_transfer_len; | |
343 | ||
344 | if (rx_remain_len != 0) { | |
345 | struct sk_buff *remain_skb = hif_dev->remain_skb; | |
346 | ||
347 | if (remain_skb) { | |
348 | ptr = (u8 *) remain_skb->data; | |
349 | ||
350 | index = rx_remain_len; | |
351 | rx_remain_len -= hif_dev->rx_pad_len; | |
352 | ptr += rx_pkt_len; | |
353 | ||
354 | memcpy(ptr, skb->data, rx_remain_len); | |
355 | ||
356 | rx_pkt_len += rx_remain_len; | |
357 | hif_dev->rx_remain_len = 0; | |
358 | skb_put(remain_skb, rx_pkt_len); | |
359 | ||
360 | skb_pool[pool_index++] = remain_skb; | |
361 | ||
362 | } else { | |
363 | index = rx_remain_len; | |
364 | } | |
365 | } | |
366 | ||
46baa1a2 S |
367 | spin_unlock(&hif_dev->rx_lock); |
368 | ||
fb9987d0 S |
369 | while (index < len) { |
370 | ptr = (u8 *) skb->data; | |
371 | ||
372 | pkt_len = ptr[index] + (ptr[index+1] << 8); | |
373 | pkt_tag = ptr[index+2] + (ptr[index+3] << 8); | |
374 | ||
375 | if (pkt_tag == ATH_USB_RX_STREAM_MODE_TAG) { | |
376 | u16 pad_len; | |
377 | ||
378 | pad_len = 4 - (pkt_len & 0x3); | |
379 | if (pad_len == 4) | |
380 | pad_len = 0; | |
381 | ||
382 | chk_idx = index; | |
383 | index = index + 4 + pkt_len + pad_len; | |
384 | ||
385 | if (index > MAX_RX_BUF_SIZE) { | |
46baa1a2 | 386 | spin_lock(&hif_dev->rx_lock); |
fb9987d0 S |
387 | hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE; |
388 | hif_dev->rx_transfer_len = | |
389 | MAX_RX_BUF_SIZE - chk_idx - 4; | |
390 | hif_dev->rx_pad_len = pad_len; | |
391 | ||
392 | nskb = __dev_alloc_skb(pkt_len + 32, | |
393 | GFP_ATOMIC); | |
394 | if (!nskb) { | |
395 | dev_err(&hif_dev->udev->dev, | |
396 | "ath9k_htc: RX memory allocation" | |
397 | " error\n"); | |
46baa1a2 | 398 | spin_unlock(&hif_dev->rx_lock); |
fb9987d0 S |
399 | goto err; |
400 | } | |
401 | skb_reserve(nskb, 32); | |
402 | RX_STAT_INC(skb_allocated); | |
403 | ||
404 | memcpy(nskb->data, &(skb->data[chk_idx+4]), | |
405 | hif_dev->rx_transfer_len); | |
406 | ||
407 | /* Record the buffer pointer */ | |
408 | hif_dev->remain_skb = nskb; | |
46baa1a2 | 409 | spin_unlock(&hif_dev->rx_lock); |
fb9987d0 S |
410 | } else { |
411 | nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC); | |
412 | if (!nskb) { | |
413 | dev_err(&hif_dev->udev->dev, | |
414 | "ath9k_htc: RX memory allocation" | |
415 | " error\n"); | |
416 | goto err; | |
417 | } | |
418 | skb_reserve(nskb, 32); | |
419 | RX_STAT_INC(skb_allocated); | |
420 | ||
421 | memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len); | |
422 | skb_put(nskb, pkt_len); | |
423 | skb_pool[pool_index++] = nskb; | |
424 | } | |
425 | } else { | |
426 | RX_STAT_INC(skb_dropped); | |
fb9987d0 S |
427 | return; |
428 | } | |
429 | } | |
430 | ||
431 | err: | |
fb9987d0 S |
432 | for (i = 0; i < pool_index; i++) { |
433 | ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i], | |
434 | skb_pool[i]->len, USB_WLAN_RX_PIPE); | |
435 | RX_STAT_INC(skb_completed); | |
436 | } | |
437 | } | |
438 | ||
439 | static void ath9k_hif_usb_rx_cb(struct urb *urb) | |
440 | { | |
441 | struct sk_buff *skb = (struct sk_buff *) urb->context; | |
fb9987d0 S |
442 | struct hif_device_usb *hif_dev = (struct hif_device_usb *) |
443 | usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0)); | |
444 | int ret; | |
445 | ||
6335ed0f S |
446 | if (!skb) |
447 | return; | |
448 | ||
fb9987d0 S |
449 | if (!hif_dev) |
450 | goto free; | |
451 | ||
452 | switch (urb->status) { | |
453 | case 0: | |
454 | break; | |
455 | case -ENOENT: | |
456 | case -ECONNRESET: | |
457 | case -ENODEV: | |
458 | case -ESHUTDOWN: | |
459 | goto free; | |
460 | default: | |
461 | goto resubmit; | |
462 | } | |
463 | ||
464 | if (likely(urb->actual_length != 0)) { | |
465 | skb_put(skb, urb->actual_length); | |
fb9987d0 | 466 | ath9k_hif_usb_rx_stream(hif_dev, skb); |
fb9987d0 S |
467 | } |
468 | ||
469 | resubmit: | |
470 | skb_reset_tail_pointer(skb); | |
471 | skb_trim(skb, 0); | |
472 | ||
6335ed0f | 473 | usb_anchor_urb(urb, &hif_dev->rx_submitted); |
fb9987d0 | 474 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
6335ed0f S |
475 | if (ret) { |
476 | usb_unanchor_urb(urb); | |
fb9987d0 | 477 | goto free; |
6335ed0f | 478 | } |
fb9987d0 S |
479 | |
480 | return; | |
481 | free: | |
f28a7b30 | 482 | kfree_skb(skb); |
fb9987d0 S |
483 | } |
484 | ||
485 | static void ath9k_hif_usb_reg_in_cb(struct urb *urb) | |
486 | { | |
487 | struct sk_buff *skb = (struct sk_buff *) urb->context; | |
488 | struct sk_buff *nskb; | |
489 | struct hif_device_usb *hif_dev = (struct hif_device_usb *) | |
490 | usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0)); | |
491 | int ret; | |
492 | ||
6335ed0f S |
493 | if (!skb) |
494 | return; | |
495 | ||
fb9987d0 S |
496 | if (!hif_dev) |
497 | goto free; | |
498 | ||
499 | switch (urb->status) { | |
500 | case 0: | |
501 | break; | |
502 | case -ENOENT: | |
503 | case -ECONNRESET: | |
504 | case -ENODEV: | |
505 | case -ESHUTDOWN: | |
506 | goto free; | |
507 | default: | |
508 | goto resubmit; | |
509 | } | |
510 | ||
511 | if (likely(urb->actual_length != 0)) { | |
512 | skb_put(skb, urb->actual_length); | |
513 | ||
e6c6d33c | 514 | nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC); |
fb9987d0 S |
515 | if (!nskb) |
516 | goto resubmit; | |
517 | ||
518 | usb_fill_int_urb(urb, hif_dev->udev, | |
519 | usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE), | |
520 | nskb->data, MAX_REG_IN_BUF_SIZE, | |
521 | ath9k_hif_usb_reg_in_cb, nskb, 1); | |
522 | ||
523 | ret = usb_submit_urb(urb, GFP_ATOMIC); | |
524 | if (ret) { | |
e6c6d33c | 525 | kfree_skb(nskb); |
fb9987d0 S |
526 | goto free; |
527 | } | |
528 | ||
529 | ath9k_htc_rx_msg(hif_dev->htc_handle, skb, | |
530 | skb->len, USB_REG_IN_PIPE); | |
531 | ||
532 | return; | |
533 | } | |
534 | ||
535 | resubmit: | |
536 | skb_reset_tail_pointer(skb); | |
537 | skb_trim(skb, 0); | |
538 | ||
539 | ret = usb_submit_urb(urb, GFP_ATOMIC); | |
540 | if (ret) | |
541 | goto free; | |
542 | ||
543 | return; | |
544 | free: | |
e6c6d33c | 545 | kfree_skb(skb); |
6335ed0f | 546 | urb->context = NULL; |
fb9987d0 S |
547 | } |
548 | ||
549 | static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev) | |
550 | { | |
551 | unsigned long flags; | |
552 | struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL; | |
553 | ||
554 | list_for_each_entry_safe(tx_buf, tx_buf_tmp, &hif_dev->tx.tx_buf, list) { | |
555 | list_del(&tx_buf->list); | |
556 | usb_free_urb(tx_buf->urb); | |
557 | kfree(tx_buf->buf); | |
558 | kfree(tx_buf); | |
559 | } | |
560 | ||
561 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); | |
562 | hif_dev->tx.flags |= HIF_USB_TX_FLUSH; | |
563 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
564 | ||
565 | list_for_each_entry_safe(tx_buf, tx_buf_tmp, | |
566 | &hif_dev->tx.tx_pending, list) { | |
567 | usb_kill_urb(tx_buf->urb); | |
568 | list_del(&tx_buf->list); | |
569 | usb_free_urb(tx_buf->urb); | |
570 | kfree(tx_buf->buf); | |
571 | kfree(tx_buf); | |
572 | } | |
573 | ||
574 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); | |
575 | hif_dev->tx.flags &= ~HIF_USB_TX_FLUSH; | |
576 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); | |
577 | } | |
578 | ||
579 | static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev) | |
580 | { | |
581 | struct tx_buf *tx_buf; | |
582 | int i; | |
583 | ||
584 | INIT_LIST_HEAD(&hif_dev->tx.tx_buf); | |
585 | INIT_LIST_HEAD(&hif_dev->tx.tx_pending); | |
586 | spin_lock_init(&hif_dev->tx.tx_lock); | |
587 | __skb_queue_head_init(&hif_dev->tx.tx_skb_queue); | |
588 | ||
589 | for (i = 0; i < MAX_TX_URB_NUM; i++) { | |
590 | tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL); | |
591 | if (!tx_buf) | |
592 | goto err; | |
593 | ||
594 | tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL); | |
595 | if (!tx_buf->buf) | |
596 | goto err; | |
597 | ||
598 | tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL); | |
599 | if (!tx_buf->urb) | |
600 | goto err; | |
601 | ||
602 | tx_buf->hif_dev = hif_dev; | |
603 | __skb_queue_head_init(&tx_buf->skb_queue); | |
604 | ||
605 | list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf); | |
606 | } | |
607 | ||
608 | hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM; | |
609 | ||
610 | return 0; | |
611 | err: | |
612 | ath9k_hif_usb_dealloc_tx_urbs(hif_dev); | |
613 | return -ENOMEM; | |
614 | } | |
615 | ||
fb9987d0 S |
616 | static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev) |
617 | { | |
6335ed0f | 618 | usb_kill_anchored_urbs(&hif_dev->rx_submitted); |
fb9987d0 S |
619 | } |
620 | ||
621 | static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev) | |
622 | { | |
6335ed0f S |
623 | struct urb *urb = NULL; |
624 | struct sk_buff *skb = NULL; | |
fb9987d0 S |
625 | int i, ret; |
626 | ||
6335ed0f | 627 | init_usb_anchor(&hif_dev->rx_submitted); |
46baa1a2 | 628 | spin_lock_init(&hif_dev->rx_lock); |
6335ed0f | 629 | |
fb9987d0 S |
630 | for (i = 0; i < MAX_RX_URB_NUM; i++) { |
631 | ||
632 | /* Allocate URB */ | |
6335ed0f S |
633 | urb = usb_alloc_urb(0, GFP_KERNEL); |
634 | if (urb == NULL) { | |
fb9987d0 | 635 | ret = -ENOMEM; |
6335ed0f | 636 | goto err_urb; |
fb9987d0 S |
637 | } |
638 | ||
639 | /* Allocate buffer */ | |
f28a7b30 | 640 | skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL); |
6335ed0f S |
641 | if (!skb) { |
642 | ret = -ENOMEM; | |
643 | goto err_skb; | |
644 | } | |
fb9987d0 | 645 | |
6335ed0f S |
646 | usb_fill_bulk_urb(urb, hif_dev->udev, |
647 | usb_rcvbulkpipe(hif_dev->udev, | |
648 | USB_WLAN_RX_PIPE), | |
649 | skb->data, MAX_RX_BUF_SIZE, | |
650 | ath9k_hif_usb_rx_cb, skb); | |
651 | ||
652 | /* Anchor URB */ | |
653 | usb_anchor_urb(urb, &hif_dev->rx_submitted); | |
fb9987d0 | 654 | |
6335ed0f S |
655 | /* Submit URB */ |
656 | ret = usb_submit_urb(urb, GFP_KERNEL); | |
657 | if (ret) { | |
658 | usb_unanchor_urb(urb); | |
659 | goto err_submit; | |
660 | } | |
66b10e33 S |
661 | |
662 | /* | |
663 | * Drop reference count. | |
664 | * This ensures that the URB is freed when killing them. | |
665 | */ | |
666 | usb_free_urb(urb); | |
fb9987d0 S |
667 | } |
668 | ||
669 | return 0; | |
670 | ||
6335ed0f | 671 | err_submit: |
f28a7b30 | 672 | kfree_skb(skb); |
6335ed0f S |
673 | err_skb: |
674 | usb_free_urb(urb); | |
675 | err_urb: | |
fb9987d0 S |
676 | ath9k_hif_usb_dealloc_rx_urbs(hif_dev); |
677 | return ret; | |
678 | } | |
679 | ||
680 | static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev) | |
681 | { | |
682 | if (hif_dev->reg_in_urb) { | |
683 | usb_kill_urb(hif_dev->reg_in_urb); | |
6335ed0f | 684 | if (hif_dev->reg_in_urb->context) |
e6c6d33c | 685 | kfree_skb((void *)hif_dev->reg_in_urb->context); |
fb9987d0 S |
686 | usb_free_urb(hif_dev->reg_in_urb); |
687 | hif_dev->reg_in_urb = NULL; | |
688 | } | |
689 | } | |
690 | ||
691 | static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev) | |
692 | { | |
693 | struct sk_buff *skb; | |
694 | ||
695 | hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL); | |
696 | if (hif_dev->reg_in_urb == NULL) | |
697 | return -ENOMEM; | |
698 | ||
e6c6d33c | 699 | skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL); |
fb9987d0 S |
700 | if (!skb) |
701 | goto err; | |
702 | ||
703 | usb_fill_int_urb(hif_dev->reg_in_urb, hif_dev->udev, | |
704 | usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE), | |
705 | skb->data, MAX_REG_IN_BUF_SIZE, | |
706 | ath9k_hif_usb_reg_in_cb, skb, 1); | |
707 | ||
708 | if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0) | |
6335ed0f | 709 | goto err; |
fb9987d0 S |
710 | |
711 | return 0; | |
712 | ||
fb9987d0 S |
713 | err: |
714 | ath9k_hif_usb_dealloc_reg_in_urb(hif_dev); | |
715 | return -ENOMEM; | |
716 | } | |
717 | ||
718 | static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev) | |
719 | { | |
6f0f2669 S |
720 | /* Register Write */ |
721 | init_usb_anchor(&hif_dev->regout_submitted); | |
722 | ||
fb9987d0 S |
723 | /* TX */ |
724 | if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0) | |
725 | goto err; | |
726 | ||
727 | /* RX */ | |
728 | if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0) | |
729 | goto err; | |
730 | ||
6f0f2669 | 731 | /* Register Read */ |
fb9987d0 S |
732 | if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0) |
733 | goto err; | |
734 | ||
735 | return 0; | |
736 | err: | |
737 | return -ENOMEM; | |
738 | } | |
739 | ||
740 | static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev) | |
741 | { | |
742 | int transfer, err; | |
743 | const void *data = hif_dev->firmware->data; | |
744 | size_t len = hif_dev->firmware->size; | |
745 | u32 addr = AR9271_FIRMWARE; | |
746 | u8 *buf = kzalloc(4096, GFP_KERNEL); | |
747 | ||
748 | if (!buf) | |
749 | return -ENOMEM; | |
750 | ||
751 | while (len) { | |
752 | transfer = min_t(int, len, 4096); | |
753 | memcpy(buf, data, transfer); | |
754 | ||
755 | err = usb_control_msg(hif_dev->udev, | |
756 | usb_sndctrlpipe(hif_dev->udev, 0), | |
757 | FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT, | |
758 | addr >> 8, 0, buf, transfer, HZ); | |
759 | if (err < 0) { | |
760 | kfree(buf); | |
761 | return err; | |
762 | } | |
763 | ||
764 | len -= transfer; | |
765 | data += transfer; | |
766 | addr += transfer; | |
767 | } | |
768 | kfree(buf); | |
769 | ||
770 | /* | |
771 | * Issue FW download complete command to firmware. | |
772 | */ | |
773 | err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0), | |
774 | FIRMWARE_DOWNLOAD_COMP, | |
775 | 0x40 | USB_DIR_OUT, | |
776 | AR9271_FIRMWARE_TEXT >> 8, 0, NULL, 0, HZ); | |
777 | if (err) | |
778 | return -EIO; | |
779 | ||
780 | dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n", | |
781 | "ar9271.fw", (unsigned long) hif_dev->firmware->size); | |
782 | ||
783 | return 0; | |
784 | } | |
785 | ||
786 | static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, | |
787 | const char *fw_name) | |
788 | { | |
789 | int ret; | |
790 | ||
791 | /* Request firmware */ | |
792 | ret = request_firmware(&hif_dev->firmware, fw_name, &hif_dev->udev->dev); | |
793 | if (ret) { | |
794 | dev_err(&hif_dev->udev->dev, | |
795 | "ath9k_htc: Firmware - %s not found\n", fw_name); | |
796 | goto err_fw_req; | |
797 | } | |
798 | ||
799 | /* Download firmware */ | |
800 | ret = ath9k_hif_usb_download_fw(hif_dev); | |
801 | if (ret) { | |
802 | dev_err(&hif_dev->udev->dev, | |
803 | "ath9k_htc: Firmware - %s download failed\n", fw_name); | |
804 | goto err_fw_download; | |
805 | } | |
806 | ||
807 | /* Alloc URBs */ | |
808 | ret = ath9k_hif_usb_alloc_urbs(hif_dev); | |
809 | if (ret) { | |
810 | dev_err(&hif_dev->udev->dev, | |
811 | "ath9k_htc: Unable to allocate URBs\n"); | |
812 | goto err_urb; | |
813 | } | |
814 | ||
815 | return 0; | |
816 | ||
817 | err_urb: | |
818 | /* Nothing */ | |
819 | err_fw_download: | |
820 | release_firmware(hif_dev->firmware); | |
821 | err_fw_req: | |
822 | hif_dev->firmware = NULL; | |
823 | return ret; | |
824 | } | |
825 | ||
826 | static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev) | |
827 | { | |
6f0f2669 | 828 | usb_kill_anchored_urbs(&hif_dev->regout_submitted); |
fb9987d0 S |
829 | ath9k_hif_usb_dealloc_reg_in_urb(hif_dev); |
830 | ath9k_hif_usb_dealloc_tx_urbs(hif_dev); | |
831 | ath9k_hif_usb_dealloc_rx_urbs(hif_dev); | |
832 | } | |
833 | ||
834 | static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev) | |
835 | { | |
836 | ath9k_hif_usb_dealloc_urbs(hif_dev); | |
837 | if (hif_dev->firmware) | |
838 | release_firmware(hif_dev->firmware); | |
839 | } | |
840 | ||
841 | static int ath9k_hif_usb_probe(struct usb_interface *interface, | |
842 | const struct usb_device_id *id) | |
843 | { | |
844 | struct usb_device *udev = interface_to_usbdev(interface); | |
845 | struct hif_device_usb *hif_dev; | |
846 | const char *fw_name = (const char *) id->driver_info; | |
847 | int ret = 0; | |
848 | ||
849 | hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL); | |
850 | if (!hif_dev) { | |
851 | ret = -ENOMEM; | |
852 | goto err_alloc; | |
853 | } | |
854 | ||
855 | usb_get_dev(udev); | |
856 | hif_dev->udev = udev; | |
857 | hif_dev->interface = interface; | |
858 | hif_dev->device_id = id->idProduct; | |
859 | #ifdef CONFIG_PM | |
860 | udev->reset_resume = 1; | |
861 | #endif | |
862 | usb_set_intfdata(interface, hif_dev); | |
863 | ||
864 | ret = ath9k_hif_usb_dev_init(hif_dev, fw_name); | |
865 | if (ret) { | |
866 | ret = -EINVAL; | |
867 | goto err_hif_init_usb; | |
868 | } | |
869 | ||
870 | hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev); | |
871 | if (hif_dev->htc_handle == NULL) { | |
872 | ret = -ENOMEM; | |
873 | goto err_htc_hw_alloc; | |
874 | } | |
875 | ||
876 | ret = ath9k_htc_hw_init(&hif_usb, hif_dev->htc_handle, hif_dev, | |
877 | &hif_dev->udev->dev, hif_dev->device_id, | |
878 | ATH9K_HIF_USB); | |
879 | if (ret) { | |
880 | ret = -EINVAL; | |
881 | goto err_htc_hw_init; | |
882 | } | |
883 | ||
884 | dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n"); | |
885 | ||
886 | return 0; | |
887 | ||
888 | err_htc_hw_init: | |
889 | ath9k_htc_hw_free(hif_dev->htc_handle); | |
890 | err_htc_hw_alloc: | |
891 | ath9k_hif_usb_dev_deinit(hif_dev); | |
892 | err_hif_init_usb: | |
893 | usb_set_intfdata(interface, NULL); | |
894 | kfree(hif_dev); | |
895 | usb_put_dev(udev); | |
896 | err_alloc: | |
897 | return ret; | |
898 | } | |
899 | ||
900 | static void ath9k_hif_usb_disconnect(struct usb_interface *interface) | |
901 | { | |
902 | struct usb_device *udev = interface_to_usbdev(interface); | |
903 | struct hif_device_usb *hif_dev = | |
904 | (struct hif_device_usb *) usb_get_intfdata(interface); | |
905 | ||
906 | if (hif_dev) { | |
907 | ath9k_htc_hw_deinit(hif_dev->htc_handle, true); | |
908 | ath9k_htc_hw_free(hif_dev->htc_handle); | |
909 | ath9k_hif_usb_dev_deinit(hif_dev); | |
910 | usb_set_intfdata(interface, NULL); | |
911 | } | |
912 | ||
913 | if (hif_dev->flags & HIF_USB_START) | |
914 | usb_reset_device(udev); | |
915 | ||
916 | kfree(hif_dev); | |
917 | dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n"); | |
918 | usb_put_dev(udev); | |
919 | } | |
920 | ||
921 | #ifdef CONFIG_PM | |
922 | static int ath9k_hif_usb_suspend(struct usb_interface *interface, | |
923 | pm_message_t message) | |
924 | { | |
925 | struct hif_device_usb *hif_dev = | |
926 | (struct hif_device_usb *) usb_get_intfdata(interface); | |
927 | ||
928 | ath9k_hif_usb_dealloc_urbs(hif_dev); | |
929 | ||
930 | return 0; | |
931 | } | |
932 | ||
933 | static int ath9k_hif_usb_resume(struct usb_interface *interface) | |
934 | { | |
935 | struct hif_device_usb *hif_dev = | |
936 | (struct hif_device_usb *) usb_get_intfdata(interface); | |
937 | int ret; | |
938 | ||
939 | ret = ath9k_hif_usb_alloc_urbs(hif_dev); | |
940 | if (ret) | |
941 | return ret; | |
942 | ||
943 | if (hif_dev->firmware) { | |
944 | ret = ath9k_hif_usb_download_fw(hif_dev); | |
945 | if (ret) | |
946 | goto fail_resume; | |
947 | } else { | |
948 | ath9k_hif_usb_dealloc_urbs(hif_dev); | |
949 | return -EIO; | |
950 | } | |
951 | ||
952 | mdelay(100); | |
953 | ||
954 | ret = ath9k_htc_resume(hif_dev->htc_handle); | |
955 | ||
956 | if (ret) | |
957 | goto fail_resume; | |
958 | ||
959 | return 0; | |
960 | ||
961 | fail_resume: | |
962 | ath9k_hif_usb_dealloc_urbs(hif_dev); | |
963 | ||
964 | return ret; | |
965 | } | |
966 | #endif | |
967 | ||
968 | static struct usb_driver ath9k_hif_usb_driver = { | |
969 | .name = "ath9k_hif_usb", | |
970 | .probe = ath9k_hif_usb_probe, | |
971 | .disconnect = ath9k_hif_usb_disconnect, | |
972 | #ifdef CONFIG_PM | |
973 | .suspend = ath9k_hif_usb_suspend, | |
974 | .resume = ath9k_hif_usb_resume, | |
975 | .reset_resume = ath9k_hif_usb_resume, | |
976 | #endif | |
977 | .id_table = ath9k_hif_usb_ids, | |
978 | .soft_unbind = 1, | |
979 | }; | |
980 | ||
981 | int ath9k_hif_usb_init(void) | |
982 | { | |
983 | return usb_register(&ath9k_hif_usb_driver); | |
984 | } | |
985 | ||
986 | void ath9k_hif_usb_exit(void) | |
987 | { | |
988 | usb_deregister(&ath9k_hif_usb_driver); | |
989 | } |