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