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