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