2 * Copyright (c) 2007-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <linux/module.h>
19 #include <linux/usb.h>
25 #define TX_URB_COUNT 32
26 #define RX_URB_COUNT 32
27 #define ATH6KL_USB_RX_BUFFER_SIZE 4096
29 /* tx/rx pipes for usb */
30 enum ATH6KL_USB_PIPE_ID
{
31 ATH6KL_USB_PIPE_TX_CTRL
= 0,
32 ATH6KL_USB_PIPE_TX_DATA_LP
,
33 ATH6KL_USB_PIPE_TX_DATA_MP
,
34 ATH6KL_USB_PIPE_TX_DATA_HP
,
35 ATH6KL_USB_PIPE_RX_CTRL
,
36 ATH6KL_USB_PIPE_RX_DATA
,
37 ATH6KL_USB_PIPE_RX_DATA2
,
38 ATH6KL_USB_PIPE_RX_INT
,
42 #define ATH6KL_USB_PIPE_INVALID ATH6KL_USB_PIPE_MAX
44 struct ath6kl_usb_pipe
{
45 struct list_head urb_list_head
;
46 struct usb_anchor urb_submitted
;
50 unsigned int usb_pipe_handle
;
54 struct ath6kl_usb
*ar_usb
;
56 struct work_struct io_complete_work
;
57 struct sk_buff_head io_comp_queue
;
58 struct usb_endpoint_descriptor
*ep_desc
;
61 #define ATH6KL_USB_PIPE_FLAG_TX (1 << 0)
63 /* usb device object */
65 /* protects pipe->urb_list_head and pipe->urb_cnt */
68 struct usb_device
*udev
;
69 struct usb_interface
*interface
;
70 struct ath6kl_usb_pipe pipes
[ATH6KL_USB_PIPE_MAX
];
77 struct ath6kl_urb_context
{
78 struct list_head link
;
79 struct ath6kl_usb_pipe
*pipe
;
84 /* USB endpoint definitions */
85 #define ATH6KL_USB_EP_ADDR_APP_CTRL_IN 0x81
86 #define ATH6KL_USB_EP_ADDR_APP_DATA_IN 0x82
87 #define ATH6KL_USB_EP_ADDR_APP_DATA2_IN 0x83
88 #define ATH6KL_USB_EP_ADDR_APP_INT_IN 0x84
90 #define ATH6KL_USB_EP_ADDR_APP_CTRL_OUT 0x01
91 #define ATH6KL_USB_EP_ADDR_APP_DATA_LP_OUT 0x02
92 #define ATH6KL_USB_EP_ADDR_APP_DATA_MP_OUT 0x03
93 #define ATH6KL_USB_EP_ADDR_APP_DATA_HP_OUT 0x04
95 /* diagnostic command defnitions */
96 #define ATH6KL_USB_CONTROL_REQ_SEND_BMI_CMD 1
97 #define ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP 2
98 #define ATH6KL_USB_CONTROL_REQ_DIAG_CMD 3
99 #define ATH6KL_USB_CONTROL_REQ_DIAG_RESP 4
101 #define ATH6KL_USB_CTRL_DIAG_CC_READ 0
102 #define ATH6KL_USB_CTRL_DIAG_CC_WRITE 1
104 struct ath6kl_usb_ctrl_diag_cmd_write
{
111 struct ath6kl_usb_ctrl_diag_cmd_read
{
116 struct ath6kl_usb_ctrl_diag_resp_read
{
120 /* function declarations */
121 static void ath6kl_usb_recv_complete(struct urb
*urb
);
123 #define ATH6KL_USB_IS_BULK_EP(attr) (((attr) & 3) == 0x02)
124 #define ATH6KL_USB_IS_INT_EP(attr) (((attr) & 3) == 0x03)
125 #define ATH6KL_USB_IS_ISOC_EP(attr) (((attr) & 3) == 0x01)
126 #define ATH6KL_USB_IS_DIR_IN(addr) ((addr) & 0x80)
128 /* pipe/urb operations */
129 static struct ath6kl_urb_context
*
130 ath6kl_usb_alloc_urb_from_pipe(struct ath6kl_usb_pipe
*pipe
)
132 struct ath6kl_urb_context
*urb_context
= NULL
;
135 /* bail if this pipe is not initialized */
139 spin_lock_irqsave(&pipe
->ar_usb
->cs_lock
, flags
);
140 if (!list_empty(&pipe
->urb_list_head
)) {
142 list_first_entry(&pipe
->urb_list_head
,
143 struct ath6kl_urb_context
, link
);
144 list_del(&urb_context
->link
);
147 spin_unlock_irqrestore(&pipe
->ar_usb
->cs_lock
, flags
);
152 static void ath6kl_usb_free_urb_to_pipe(struct ath6kl_usb_pipe
*pipe
,
153 struct ath6kl_urb_context
*urb_context
)
157 /* bail if this pipe is not initialized */
161 spin_lock_irqsave(&pipe
->ar_usb
->cs_lock
, flags
);
164 list_add(&urb_context
->link
, &pipe
->urb_list_head
);
165 spin_unlock_irqrestore(&pipe
->ar_usb
->cs_lock
, flags
);
168 static void ath6kl_usb_cleanup_recv_urb(struct ath6kl_urb_context
*urb_context
)
170 dev_kfree_skb(urb_context
->skb
);
171 urb_context
->skb
= NULL
;
173 ath6kl_usb_free_urb_to_pipe(urb_context
->pipe
, urb_context
);
176 static inline struct ath6kl_usb
*ath6kl_usb_priv(struct ath6kl
*ar
)
181 /* pipe resource allocation/cleanup */
182 static int ath6kl_usb_alloc_pipe_resources(struct ath6kl_usb_pipe
*pipe
,
185 struct ath6kl_urb_context
*urb_context
;
188 INIT_LIST_HEAD(&pipe
->urb_list_head
);
189 init_usb_anchor(&pipe
->urb_submitted
);
191 for (i
= 0; i
< urb_cnt
; i
++) {
192 urb_context
= kzalloc(sizeof(struct ath6kl_urb_context
),
194 if (urb_context
== NULL
) {
196 goto fail_alloc_pipe_resources
;
199 urb_context
->pipe
= pipe
;
202 * we are only allocate the urb contexts here, the actual URB
203 * is allocated from the kernel as needed to do a transaction
206 ath6kl_usb_free_urb_to_pipe(pipe
, urb_context
);
209 ath6kl_dbg(ATH6KL_DBG_USB
,
210 "ath6kl usb: alloc resources lpipe:%d hpipe:0x%X urbs:%d\n",
211 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
214 fail_alloc_pipe_resources
:
218 static void ath6kl_usb_free_pipe_resources(struct ath6kl_usb_pipe
*pipe
)
220 struct ath6kl_urb_context
*urb_context
;
222 if (pipe
->ar_usb
== NULL
) {
223 /* nothing allocated for this pipe */
227 ath6kl_dbg(ATH6KL_DBG_USB
,
228 "ath6kl usb: free resources lpipe:%d"
229 "hpipe:0x%X urbs:%d avail:%d\n",
230 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
231 pipe
->urb_alloc
, pipe
->urb_cnt
);
233 if (pipe
->urb_alloc
!= pipe
->urb_cnt
) {
234 ath6kl_dbg(ATH6KL_DBG_USB
,
235 "ath6kl usb: urb leak! lpipe:%d"
236 "hpipe:0x%X urbs:%d avail:%d\n",
237 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
238 pipe
->urb_alloc
, pipe
->urb_cnt
);
242 urb_context
= ath6kl_usb_alloc_urb_from_pipe(pipe
);
243 if (urb_context
== NULL
)
249 static void ath6kl_usb_cleanup_pipe_resources(struct ath6kl_usb
*ar_usb
)
253 for (i
= 0; i
< ATH6KL_USB_PIPE_MAX
; i
++)
254 ath6kl_usb_free_pipe_resources(&ar_usb
->pipes
[i
]);
257 static u8
ath6kl_usb_get_logical_pipe_num(struct ath6kl_usb
*ar_usb
,
258 u8 ep_address
, int *urb_count
)
260 u8 pipe_num
= ATH6KL_USB_PIPE_INVALID
;
262 switch (ep_address
) {
263 case ATH6KL_USB_EP_ADDR_APP_CTRL_IN
:
264 pipe_num
= ATH6KL_USB_PIPE_RX_CTRL
;
265 *urb_count
= RX_URB_COUNT
;
267 case ATH6KL_USB_EP_ADDR_APP_DATA_IN
:
268 pipe_num
= ATH6KL_USB_PIPE_RX_DATA
;
269 *urb_count
= RX_URB_COUNT
;
271 case ATH6KL_USB_EP_ADDR_APP_INT_IN
:
272 pipe_num
= ATH6KL_USB_PIPE_RX_INT
;
273 *urb_count
= RX_URB_COUNT
;
275 case ATH6KL_USB_EP_ADDR_APP_DATA2_IN
:
276 pipe_num
= ATH6KL_USB_PIPE_RX_DATA2
;
277 *urb_count
= RX_URB_COUNT
;
279 case ATH6KL_USB_EP_ADDR_APP_CTRL_OUT
:
280 pipe_num
= ATH6KL_USB_PIPE_TX_CTRL
;
281 *urb_count
= TX_URB_COUNT
;
283 case ATH6KL_USB_EP_ADDR_APP_DATA_LP_OUT
:
284 pipe_num
= ATH6KL_USB_PIPE_TX_DATA_LP
;
285 *urb_count
= TX_URB_COUNT
;
287 case ATH6KL_USB_EP_ADDR_APP_DATA_MP_OUT
:
288 pipe_num
= ATH6KL_USB_PIPE_TX_DATA_MP
;
289 *urb_count
= TX_URB_COUNT
;
291 case ATH6KL_USB_EP_ADDR_APP_DATA_HP_OUT
:
292 pipe_num
= ATH6KL_USB_PIPE_TX_DATA_HP
;
293 *urb_count
= TX_URB_COUNT
;
296 /* note: there may be endpoints not currently used */
303 static int ath6kl_usb_setup_pipe_resources(struct ath6kl_usb
*ar_usb
)
305 struct usb_interface
*interface
= ar_usb
->interface
;
306 struct usb_host_interface
*iface_desc
= interface
->cur_altsetting
;
307 struct usb_endpoint_descriptor
*endpoint
;
308 struct ath6kl_usb_pipe
*pipe
;
309 int i
, urbcount
, status
= 0;
312 ath6kl_dbg(ATH6KL_DBG_USB
, "setting up USB Pipes using interface\n");
314 /* walk decriptors and setup pipes */
315 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
316 endpoint
= &iface_desc
->endpoint
[i
].desc
;
318 if (ATH6KL_USB_IS_BULK_EP(endpoint
->bmAttributes
)) {
319 ath6kl_dbg(ATH6KL_DBG_USB
,
320 "%s Bulk Ep:0x%2.2X maxpktsz:%d\n",
322 (endpoint
->bEndpointAddress
) ?
323 "RX" : "TX", endpoint
->bEndpointAddress
,
324 le16_to_cpu(endpoint
->wMaxPacketSize
));
325 } else if (ATH6KL_USB_IS_INT_EP(endpoint
->bmAttributes
)) {
326 ath6kl_dbg(ATH6KL_DBG_USB
,
327 "%s Int Ep:0x%2.2X maxpktsz:%d interval:%d\n",
329 (endpoint
->bEndpointAddress
) ?
330 "RX" : "TX", endpoint
->bEndpointAddress
,
331 le16_to_cpu(endpoint
->wMaxPacketSize
),
332 endpoint
->bInterval
);
333 } else if (ATH6KL_USB_IS_ISOC_EP(endpoint
->bmAttributes
)) {
335 ath6kl_dbg(ATH6KL_DBG_USB
,
336 "%s ISOC Ep:0x%2.2X maxpktsz:%d interval:%d\n",
338 (endpoint
->bEndpointAddress
) ?
339 "RX" : "TX", endpoint
->bEndpointAddress
,
340 le16_to_cpu(endpoint
->wMaxPacketSize
),
341 endpoint
->bInterval
);
346 ath6kl_usb_get_logical_pipe_num(ar_usb
,
347 endpoint
->bEndpointAddress
,
349 if (pipe_num
== ATH6KL_USB_PIPE_INVALID
)
352 pipe
= &ar_usb
->pipes
[pipe_num
];
353 if (pipe
->ar_usb
!= NULL
) {
354 /* hmmm..pipe was already setup */
358 pipe
->ar_usb
= ar_usb
;
359 pipe
->logical_pipe_num
= pipe_num
;
360 pipe
->ep_address
= endpoint
->bEndpointAddress
;
361 pipe
->max_packet_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
363 if (ATH6KL_USB_IS_BULK_EP(endpoint
->bmAttributes
)) {
364 if (ATH6KL_USB_IS_DIR_IN(pipe
->ep_address
)) {
365 pipe
->usb_pipe_handle
=
366 usb_rcvbulkpipe(ar_usb
->udev
,
369 pipe
->usb_pipe_handle
=
370 usb_sndbulkpipe(ar_usb
->udev
,
373 } else if (ATH6KL_USB_IS_INT_EP(endpoint
->bmAttributes
)) {
374 if (ATH6KL_USB_IS_DIR_IN(pipe
->ep_address
)) {
375 pipe
->usb_pipe_handle
=
376 usb_rcvintpipe(ar_usb
->udev
,
379 pipe
->usb_pipe_handle
=
380 usb_sndintpipe(ar_usb
->udev
,
383 } else if (ATH6KL_USB_IS_ISOC_EP(endpoint
->bmAttributes
)) {
385 if (ATH6KL_USB_IS_DIR_IN(pipe
->ep_address
)) {
386 pipe
->usb_pipe_handle
=
387 usb_rcvisocpipe(ar_usb
->udev
,
390 pipe
->usb_pipe_handle
=
391 usb_sndisocpipe(ar_usb
->udev
,
396 pipe
->ep_desc
= endpoint
;
398 if (!ATH6KL_USB_IS_DIR_IN(pipe
->ep_address
))
399 pipe
->flags
|= ATH6KL_USB_PIPE_FLAG_TX
;
401 status
= ath6kl_usb_alloc_pipe_resources(pipe
, urbcount
);
409 /* pipe operations */
410 static void ath6kl_usb_post_recv_transfers(struct ath6kl_usb_pipe
*recv_pipe
,
413 struct ath6kl_urb_context
*urb_context
;
418 urb_context
= ath6kl_usb_alloc_urb_from_pipe(recv_pipe
);
419 if (urb_context
== NULL
)
422 urb_context
->skb
= dev_alloc_skb(buffer_length
);
423 if (urb_context
->skb
== NULL
)
424 goto err_cleanup_urb
;
426 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
428 goto err_cleanup_urb
;
430 usb_fill_bulk_urb(urb
,
431 recv_pipe
->ar_usb
->udev
,
432 recv_pipe
->usb_pipe_handle
,
433 urb_context
->skb
->data
,
435 ath6kl_usb_recv_complete
, urb_context
);
437 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
438 "ath6kl usb: bulk recv submit:%d, 0x%X (ep:0x%2.2X), %d bytes buf:0x%p\n",
439 recv_pipe
->logical_pipe_num
,
440 recv_pipe
->usb_pipe_handle
, recv_pipe
->ep_address
,
441 buffer_length
, urb_context
->skb
);
443 usb_anchor_urb(urb
, &recv_pipe
->urb_submitted
);
444 usb_status
= usb_submit_urb(urb
, GFP_ATOMIC
);
447 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
448 "ath6kl usb : usb bulk recv failed %d\n",
450 usb_unanchor_urb(urb
);
452 goto err_cleanup_urb
;
459 ath6kl_usb_cleanup_recv_urb(urb_context
);
463 static void ath6kl_usb_flush_all(struct ath6kl_usb
*ar_usb
)
467 for (i
= 0; i
< ATH6KL_USB_PIPE_MAX
; i
++) {
468 if (ar_usb
->pipes
[i
].ar_usb
!= NULL
)
469 usb_kill_anchored_urbs(&ar_usb
->pipes
[i
].urb_submitted
);
473 * Flushing any pending I/O may schedule work this call will block
474 * until all scheduled work runs to completion.
476 flush_scheduled_work();
479 static void ath6kl_usb_start_recv_pipes(struct ath6kl_usb
*ar_usb
)
482 * note: control pipe is no longer used
483 * ar_usb->pipes[ATH6KL_USB_PIPE_RX_CTRL].urb_cnt_thresh =
484 * ar_usb->pipes[ATH6KL_USB_PIPE_RX_CTRL].urb_alloc/2;
485 * ath6kl_usb_post_recv_transfers(&ar_usb->
486 * pipes[ATH6KL_USB_PIPE_RX_CTRL],
487 * ATH6KL_USB_RX_BUFFER_SIZE);
490 ar_usb
->pipes
[ATH6KL_USB_PIPE_RX_DATA
].urb_cnt_thresh
= 1;
492 ath6kl_usb_post_recv_transfers(&ar_usb
->pipes
[ATH6KL_USB_PIPE_RX_DATA
],
493 ATH6KL_USB_RX_BUFFER_SIZE
);
496 /* hif usb rx/tx completion functions */
497 static void ath6kl_usb_recv_complete(struct urb
*urb
)
499 struct ath6kl_urb_context
*urb_context
= urb
->context
;
500 struct ath6kl_usb_pipe
*pipe
= urb_context
->pipe
;
501 struct sk_buff
*skb
= NULL
;
504 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
505 "%s: recv pipe: %d, stat:%d, len:%d urb:0x%p\n", __func__
,
506 pipe
->logical_pipe_num
, urb
->status
, urb
->actual_length
,
509 if (urb
->status
!= 0) {
511 switch (urb
->status
) {
516 * no need to spew these errors when device
517 * removed or urb killed due to driver shutdown
522 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
523 "%s recv pipe: %d (ep:0x%2.2X), failed:%d\n",
524 __func__
, pipe
->logical_pipe_num
,
525 pipe
->ep_address
, urb
->status
);
528 goto cleanup_recv_urb
;
531 if (urb
->actual_length
== 0)
532 goto cleanup_recv_urb
;
534 skb
= urb_context
->skb
;
536 /* we are going to pass it up */
537 urb_context
->skb
= NULL
;
538 skb_put(skb
, urb
->actual_length
);
540 /* note: queue implements a lock */
541 skb_queue_tail(&pipe
->io_comp_queue
, skb
);
542 schedule_work(&pipe
->io_complete_work
);
545 ath6kl_usb_cleanup_recv_urb(urb_context
);
548 pipe
->urb_cnt
>= pipe
->urb_cnt_thresh
) {
549 /* our free urbs are piling up, post more transfers */
550 ath6kl_usb_post_recv_transfers(pipe
, ATH6KL_USB_RX_BUFFER_SIZE
);
554 static void ath6kl_usb_usb_transmit_complete(struct urb
*urb
)
556 struct ath6kl_urb_context
*urb_context
= urb
->context
;
557 struct ath6kl_usb_pipe
*pipe
= urb_context
->pipe
;
560 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
561 "%s: pipe: %d, stat:%d, len:%d\n",
562 __func__
, pipe
->logical_pipe_num
, urb
->status
,
565 if (urb
->status
!= 0) {
566 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
567 "%s: pipe: %d, failed:%d\n",
568 __func__
, pipe
->logical_pipe_num
, urb
->status
);
571 skb
= urb_context
->skb
;
572 urb_context
->skb
= NULL
;
573 ath6kl_usb_free_urb_to_pipe(urb_context
->pipe
, urb_context
);
575 /* note: queue implements a lock */
576 skb_queue_tail(&pipe
->io_comp_queue
, skb
);
577 schedule_work(&pipe
->io_complete_work
);
580 static void ath6kl_usb_io_comp_work(struct work_struct
*work
)
582 struct ath6kl_usb_pipe
*pipe
= container_of(work
,
583 struct ath6kl_usb_pipe
,
585 struct ath6kl_usb
*ar_usb
;
588 ar_usb
= pipe
->ar_usb
;
590 while ((skb
= skb_dequeue(&pipe
->io_comp_queue
))) {
591 if (pipe
->flags
& ATH6KL_USB_PIPE_FLAG_TX
) {
592 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
593 "ath6kl usb xmit callback buf:0x%p\n", skb
);
594 ath6kl_core_tx_complete(ar_usb
->ar
, skb
);
596 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
597 "ath6kl usb recv callback buf:0x%p\n", skb
);
598 ath6kl_core_rx_complete(ar_usb
->ar
, skb
,
599 pipe
->logical_pipe_num
);
604 #define ATH6KL_USB_MAX_DIAG_CMD (sizeof(struct ath6kl_usb_ctrl_diag_cmd_write))
605 #define ATH6KL_USB_MAX_DIAG_RESP (sizeof(struct ath6kl_usb_ctrl_diag_resp_read))
607 static void ath6kl_usb_destroy(struct ath6kl_usb
*ar_usb
)
609 ath6kl_usb_flush_all(ar_usb
);
611 ath6kl_usb_cleanup_pipe_resources(ar_usb
);
613 usb_set_intfdata(ar_usb
->interface
, NULL
);
615 kfree(ar_usb
->diag_cmd_buffer
);
616 kfree(ar_usb
->diag_resp_buffer
);
621 static struct ath6kl_usb
*ath6kl_usb_create(struct usb_interface
*interface
)
623 struct usb_device
*dev
= interface_to_usbdev(interface
);
624 struct ath6kl_usb
*ar_usb
;
625 struct ath6kl_usb_pipe
*pipe
;
629 ar_usb
= kzalloc(sizeof(struct ath6kl_usb
), GFP_KERNEL
);
631 goto fail_ath6kl_usb_create
;
633 usb_set_intfdata(interface
, ar_usb
);
634 spin_lock_init(&(ar_usb
->cs_lock
));
636 ar_usb
->interface
= interface
;
638 for (i
= 0; i
< ATH6KL_USB_PIPE_MAX
; i
++) {
639 pipe
= &ar_usb
->pipes
[i
];
640 INIT_WORK(&pipe
->io_complete_work
,
641 ath6kl_usb_io_comp_work
);
642 skb_queue_head_init(&pipe
->io_comp_queue
);
645 ar_usb
->diag_cmd_buffer
= kzalloc(ATH6KL_USB_MAX_DIAG_CMD
, GFP_KERNEL
);
646 if (ar_usb
->diag_cmd_buffer
== NULL
) {
648 goto fail_ath6kl_usb_create
;
651 ar_usb
->diag_resp_buffer
= kzalloc(ATH6KL_USB_MAX_DIAG_RESP
,
653 if (ar_usb
->diag_resp_buffer
== NULL
) {
655 goto fail_ath6kl_usb_create
;
658 status
= ath6kl_usb_setup_pipe_resources(ar_usb
);
660 fail_ath6kl_usb_create
:
662 ath6kl_usb_destroy(ar_usb
);
668 static void ath6kl_usb_device_detached(struct usb_interface
*interface
)
670 struct ath6kl_usb
*ar_usb
;
672 ar_usb
= usb_get_intfdata(interface
);
676 ath6kl_stop_txrx(ar_usb
->ar
);
678 /* Delay to wait for the target to reboot */
680 ath6kl_core_cleanup(ar_usb
->ar
);
681 ath6kl_usb_destroy(ar_usb
);
684 /* exported hif usb APIs for htc pipe */
685 static void hif_start(struct ath6kl
*ar
)
687 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
690 ath6kl_usb_start_recv_pipes(device
);
692 /* set the TX resource avail threshold for each TX pipe */
693 for (i
= ATH6KL_USB_PIPE_TX_CTRL
;
694 i
<= ATH6KL_USB_PIPE_TX_DATA_HP
; i
++) {
695 device
->pipes
[i
].urb_cnt_thresh
=
696 device
->pipes
[i
].urb_alloc
/ 2;
700 static int ath6kl_usb_send(struct ath6kl
*ar
, u8 PipeID
,
701 struct sk_buff
*hdr_skb
, struct sk_buff
*skb
)
703 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
704 struct ath6kl_usb_pipe
*pipe
= &device
->pipes
[PipeID
];
705 struct ath6kl_urb_context
*urb_context
;
706 int usb_status
, status
= 0;
711 ath6kl_dbg(ATH6KL_DBG_USB_BULK
, "+%s pipe : %d, buf:0x%p\n",
712 __func__
, PipeID
, skb
);
714 urb_context
= ath6kl_usb_alloc_urb_from_pipe(pipe
);
716 if (urb_context
== NULL
) {
718 * TODO: it is possible to run out of urbs if
719 * 2 endpoints map to the same pipe ID
721 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
722 "%s pipe:%d no urbs left. URB Cnt : %d\n",
723 __func__
, PipeID
, pipe
->urb_cnt
);
728 urb_context
->skb
= skb
;
733 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
736 ath6kl_usb_free_urb_to_pipe(urb_context
->pipe
,
741 usb_fill_bulk_urb(urb
,
743 pipe
->usb_pipe_handle
,
746 ath6kl_usb_usb_transmit_complete
, urb_context
);
748 if ((len
% pipe
->max_packet_size
) == 0) {
749 /* hit a max packet boundary on this pipe */
750 urb
->transfer_flags
|= URB_ZERO_PACKET
;
753 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
754 "athusb bulk send submit:%d, 0x%X (ep:0x%2.2X), %d bytes\n",
755 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
756 pipe
->ep_address
, len
);
758 usb_anchor_urb(urb
, &pipe
->urb_submitted
);
759 usb_status
= usb_submit_urb(urb
, GFP_ATOMIC
);
762 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
763 "ath6kl usb : usb bulk transmit failed %d\n",
765 usb_unanchor_urb(urb
);
766 ath6kl_usb_free_urb_to_pipe(urb_context
->pipe
,
776 static void hif_stop(struct ath6kl
*ar
)
778 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
780 ath6kl_usb_flush_all(device
);
783 static void ath6kl_usb_get_default_pipe(struct ath6kl
*ar
,
784 u8
*ul_pipe
, u8
*dl_pipe
)
786 *ul_pipe
= ATH6KL_USB_PIPE_TX_CTRL
;
787 *dl_pipe
= ATH6KL_USB_PIPE_RX_CTRL
;
790 static int ath6kl_usb_map_service_pipe(struct ath6kl
*ar
, u16 svc_id
,
791 u8
*ul_pipe
, u8
*dl_pipe
)
796 case HTC_CTRL_RSVD_SVC
:
797 case WMI_CONTROL_SVC
:
798 *ul_pipe
= ATH6KL_USB_PIPE_TX_CTRL
;
799 /* due to large control packets, shift to data pipe */
800 *dl_pipe
= ATH6KL_USB_PIPE_RX_DATA
;
802 case WMI_DATA_BE_SVC
:
803 case WMI_DATA_BK_SVC
:
804 *ul_pipe
= ATH6KL_USB_PIPE_TX_DATA_LP
;
806 * Disable rxdata2 directly, it will be enabled
807 * if FW enable rxdata2
809 *dl_pipe
= ATH6KL_USB_PIPE_RX_DATA
;
811 case WMI_DATA_VI_SVC
:
813 if (test_bit(ATH6KL_FW_CAPABILITY_MAP_LP_ENDPOINT
,
814 ar
->fw_capabilities
))
815 *ul_pipe
= ATH6KL_USB_PIPE_TX_DATA_LP
;
817 *ul_pipe
= ATH6KL_USB_PIPE_TX_DATA_MP
;
819 * Disable rxdata2 directly, it will be enabled
820 * if FW enable rxdata2
822 *dl_pipe
= ATH6KL_USB_PIPE_RX_DATA
;
824 case WMI_DATA_VO_SVC
:
826 if (test_bit(ATH6KL_FW_CAPABILITY_MAP_LP_ENDPOINT
,
827 ar
->fw_capabilities
))
828 *ul_pipe
= ATH6KL_USB_PIPE_TX_DATA_LP
;
830 *ul_pipe
= ATH6KL_USB_PIPE_TX_DATA_MP
;
832 * Disable rxdata2 directly, it will be enabled
833 * if FW enable rxdata2
835 *dl_pipe
= ATH6KL_USB_PIPE_RX_DATA
;
845 static u16
ath6kl_usb_get_free_queue_number(struct ath6kl
*ar
, u8 pipe_id
)
847 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
849 return device
->pipes
[pipe_id
].urb_cnt
;
852 static void hif_detach_htc(struct ath6kl
*ar
)
854 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
856 ath6kl_usb_flush_all(device
);
859 static int ath6kl_usb_submit_ctrl_out(struct ath6kl_usb
*ar_usb
,
860 u8 req
, u16 value
, u16 index
, void *data
,
867 buf
= kmemdup(data
, size
, GFP_KERNEL
);
872 /* note: if successful returns number of bytes transfered */
873 ret
= usb_control_msg(ar_usb
->udev
,
874 usb_sndctrlpipe(ar_usb
->udev
, 0),
876 USB_DIR_OUT
| USB_TYPE_VENDOR
|
877 USB_RECIP_DEVICE
, value
, index
, buf
,
881 ath6kl_warn("Failed to submit usb control message: %d\n", ret
);
891 static int ath6kl_usb_submit_ctrl_in(struct ath6kl_usb
*ar_usb
,
892 u8 req
, u16 value
, u16 index
, void *data
,
899 buf
= kmalloc(size
, GFP_KERNEL
);
904 /* note: if successful returns number of bytes transfered */
905 ret
= usb_control_msg(ar_usb
->udev
,
906 usb_rcvctrlpipe(ar_usb
->udev
, 0),
908 USB_DIR_IN
| USB_TYPE_VENDOR
|
909 USB_RECIP_DEVICE
, value
, index
, buf
,
913 ath6kl_warn("Failed to read usb control message: %d\n", ret
);
918 memcpy((u8
*) data
, buf
, size
);
925 static int ath6kl_usb_ctrl_msg_exchange(struct ath6kl_usb
*ar_usb
,
926 u8 req_val
, u8
*req_buf
, u32 req_len
,
927 u8 resp_val
, u8
*resp_buf
, u32
*resp_len
)
932 ret
= ath6kl_usb_submit_ctrl_out(ar_usb
, req_val
, 0, 0,
938 if (resp_buf
== NULL
) {
939 /* no expected response */
944 ret
= ath6kl_usb_submit_ctrl_in(ar_usb
, resp_val
, 0, 0,
945 resp_buf
, *resp_len
);
950 static int ath6kl_usb_diag_read32(struct ath6kl
*ar
, u32 address
, u32
*data
)
952 struct ath6kl_usb
*ar_usb
= ar
->hif_priv
;
953 struct ath6kl_usb_ctrl_diag_resp_read
*resp
;
954 struct ath6kl_usb_ctrl_diag_cmd_read
*cmd
;
958 cmd
= (struct ath6kl_usb_ctrl_diag_cmd_read
*) ar_usb
->diag_cmd_buffer
;
960 memset(cmd
, 0, sizeof(*cmd
));
961 cmd
->cmd
= ATH6KL_USB_CTRL_DIAG_CC_READ
;
962 cmd
->address
= cpu_to_le32(address
);
963 resp_len
= sizeof(*resp
);
965 ret
= ath6kl_usb_ctrl_msg_exchange(ar_usb
,
966 ATH6KL_USB_CONTROL_REQ_DIAG_CMD
,
968 sizeof(struct ath6kl_usb_ctrl_diag_cmd_write
),
969 ATH6KL_USB_CONTROL_REQ_DIAG_RESP
,
970 ar_usb
->diag_resp_buffer
, &resp_len
);
973 ath6kl_warn("diag read32 failed: %d\n", ret
);
977 resp
= (struct ath6kl_usb_ctrl_diag_resp_read
*)
978 ar_usb
->diag_resp_buffer
;
980 *data
= le32_to_cpu(resp
->value
);
985 static int ath6kl_usb_diag_write32(struct ath6kl
*ar
, u32 address
, __le32 data
)
987 struct ath6kl_usb
*ar_usb
= ar
->hif_priv
;
988 struct ath6kl_usb_ctrl_diag_cmd_write
*cmd
;
991 cmd
= (struct ath6kl_usb_ctrl_diag_cmd_write
*) ar_usb
->diag_cmd_buffer
;
993 memset(cmd
, 0, sizeof(struct ath6kl_usb_ctrl_diag_cmd_write
));
994 cmd
->cmd
= cpu_to_le32(ATH6KL_USB_CTRL_DIAG_CC_WRITE
);
995 cmd
->address
= cpu_to_le32(address
);
998 ret
= ath6kl_usb_ctrl_msg_exchange(ar_usb
,
999 ATH6KL_USB_CONTROL_REQ_DIAG_CMD
,
1004 ath6kl_warn("diag_write32 failed: %d\n", ret
);
1011 static int ath6kl_usb_bmi_read(struct ath6kl
*ar
, u8
*buf
, u32 len
)
1013 struct ath6kl_usb
*ar_usb
= ar
->hif_priv
;
1017 ret
= ath6kl_usb_submit_ctrl_in(ar_usb
,
1018 ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP
,
1021 ath6kl_err("Unable to read the bmi data from the device: %d\n",
1029 static int ath6kl_usb_bmi_write(struct ath6kl
*ar
, u8
*buf
, u32 len
)
1031 struct ath6kl_usb
*ar_usb
= ar
->hif_priv
;
1035 ret
= ath6kl_usb_submit_ctrl_out(ar_usb
,
1036 ATH6KL_USB_CONTROL_REQ_SEND_BMI_CMD
,
1039 ath6kl_err("unable to send the bmi data to the device: %d\n",
1047 static int ath6kl_usb_power_on(struct ath6kl
*ar
)
1053 static int ath6kl_usb_power_off(struct ath6kl
*ar
)
1059 static void ath6kl_usb_stop(struct ath6kl
*ar
)
1064 static void ath6kl_usb_cleanup_scatter(struct ath6kl
*ar
)
1067 * USB doesn't support it. Just return.
1072 static int ath6kl_usb_suspend(struct ath6kl
*ar
, struct cfg80211_wowlan
*wow
)
1075 * cfg80211 suspend/WOW currently not supported for USB.
1080 static int ath6kl_usb_resume(struct ath6kl
*ar
)
1083 * cfg80211 resume currently not supported for USB.
1088 static const struct ath6kl_hif_ops ath6kl_usb_ops
= {
1089 .diag_read32
= ath6kl_usb_diag_read32
,
1090 .diag_write32
= ath6kl_usb_diag_write32
,
1091 .bmi_read
= ath6kl_usb_bmi_read
,
1092 .bmi_write
= ath6kl_usb_bmi_write
,
1093 .power_on
= ath6kl_usb_power_on
,
1094 .power_off
= ath6kl_usb_power_off
,
1095 .stop
= ath6kl_usb_stop
,
1096 .pipe_send
= ath6kl_usb_send
,
1097 .pipe_get_default
= ath6kl_usb_get_default_pipe
,
1098 .pipe_map_service
= ath6kl_usb_map_service_pipe
,
1099 .pipe_get_free_queue_number
= ath6kl_usb_get_free_queue_number
,
1100 .cleanup_scatter
= ath6kl_usb_cleanup_scatter
,
1101 .suspend
= ath6kl_usb_suspend
,
1102 .resume
= ath6kl_usb_resume
,
1105 /* ath6kl usb driver registered functions */
1106 static int ath6kl_usb_probe(struct usb_interface
*interface
,
1107 const struct usb_device_id
*id
)
1109 struct usb_device
*dev
= interface_to_usbdev(interface
);
1111 struct ath6kl_usb
*ar_usb
= NULL
;
1112 int vendor_id
, product_id
;
1117 vendor_id
= le16_to_cpu(dev
->descriptor
.idVendor
);
1118 product_id
= le16_to_cpu(dev
->descriptor
.idProduct
);
1120 ath6kl_dbg(ATH6KL_DBG_USB
, "vendor_id = %04x\n", vendor_id
);
1121 ath6kl_dbg(ATH6KL_DBG_USB
, "product_id = %04x\n", product_id
);
1123 if (interface
->cur_altsetting
)
1124 ath6kl_dbg(ATH6KL_DBG_USB
, "USB Interface %d\n",
1125 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
1128 if (dev
->speed
== USB_SPEED_HIGH
)
1129 ath6kl_dbg(ATH6KL_DBG_USB
, "USB 2.0 Host\n");
1131 ath6kl_dbg(ATH6KL_DBG_USB
, "USB 1.1 Host\n");
1133 ar_usb
= ath6kl_usb_create(interface
);
1135 if (ar_usb
== NULL
) {
1140 ar
= ath6kl_core_create(&ar_usb
->udev
->dev
);
1142 ath6kl_err("Failed to alloc ath6kl core\n");
1144 goto err_usb_destroy
;
1147 ar
->hif_priv
= ar_usb
;
1148 ar
->hif_type
= ATH6KL_HIF_TYPE_USB
;
1149 ar
->hif_ops
= &ath6kl_usb_ops
;
1150 ar
->mbox_info
.block_size
= 16;
1151 ar
->bmi
.max_data_size
= 252;
1155 ret
= ath6kl_core_init(ar
, ATH6KL_HTC_TYPE_PIPE
);
1157 ath6kl_err("Failed to init ath6kl core: %d\n", ret
);
1164 ath6kl_core_destroy(ar
);
1166 ath6kl_usb_destroy(ar_usb
);
1173 static void ath6kl_usb_remove(struct usb_interface
*interface
)
1175 usb_put_dev(interface_to_usbdev(interface
));
1176 ath6kl_usb_device_detached(interface
);
1181 static int ath6kl_usb_pm_suspend(struct usb_interface
*interface
,
1182 pm_message_t message
)
1184 struct ath6kl_usb
*device
;
1185 device
= usb_get_intfdata(interface
);
1187 ath6kl_usb_flush_all(device
);
1191 static int ath6kl_usb_pm_resume(struct usb_interface
*interface
)
1193 struct ath6kl_usb
*device
;
1194 device
= usb_get_intfdata(interface
);
1196 ath6kl_usb_post_recv_transfers(&device
->pipes
[ATH6KL_USB_PIPE_RX_DATA
],
1197 ATH6KL_USB_RX_BUFFER_SIZE
);
1198 ath6kl_usb_post_recv_transfers(&device
->pipes
[ATH6KL_USB_PIPE_RX_DATA2
],
1199 ATH6KL_USB_RX_BUFFER_SIZE
);
1206 #define ath6kl_usb_pm_suspend NULL
1207 #define ath6kl_usb_pm_resume NULL
1211 /* table of devices that work with this driver */
1212 static const struct usb_device_id ath6kl_usb_ids
[] = {
1213 {USB_DEVICE(0x0cf3, 0x9375)},
1214 {USB_DEVICE(0x0cf3, 0x9374)},
1215 { /* Terminating entry */ },
1218 MODULE_DEVICE_TABLE(usb
, ath6kl_usb_ids
);
1220 static struct usb_driver ath6kl_usb_driver
= {
1221 .name
= "ath6kl_usb",
1222 .probe
= ath6kl_usb_probe
,
1223 .suspend
= ath6kl_usb_pm_suspend
,
1224 .resume
= ath6kl_usb_pm_resume
,
1225 .disconnect
= ath6kl_usb_remove
,
1226 .id_table
= ath6kl_usb_ids
,
1227 .supports_autosuspend
= true,
1228 .disable_hub_initiated_lpm
= 1,
1231 module_usb_driver(ath6kl_usb_driver
);
1233 MODULE_AUTHOR("Atheros Communications, Inc.");
1234 MODULE_DESCRIPTION("Driver support for Atheros AR600x USB devices");
1235 MODULE_LICENSE("Dual BSD/GPL");
1236 MODULE_FIRMWARE(AR6004_HW_1_0_FIRMWARE_FILE
);
1237 MODULE_FIRMWARE(AR6004_HW_1_0_BOARD_DATA_FILE
);
1238 MODULE_FIRMWARE(AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE
);
1239 MODULE_FIRMWARE(AR6004_HW_1_1_FIRMWARE_FILE
);
1240 MODULE_FIRMWARE(AR6004_HW_1_1_BOARD_DATA_FILE
);
1241 MODULE_FIRMWARE(AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE
);
1242 MODULE_FIRMWARE(AR6004_HW_1_2_FIRMWARE_FILE
);
1243 MODULE_FIRMWARE(AR6004_HW_1_2_BOARD_DATA_FILE
);
1244 MODULE_FIRMWARE(AR6004_HW_1_2_DEFAULT_BOARD_DATA_FILE
);
1245 MODULE_FIRMWARE(AR6004_HW_1_3_FW_DIR
"/" AR6004_HW_1_3_FIRMWARE_FILE
);
1246 MODULE_FIRMWARE(AR6004_HW_1_3_BOARD_DATA_FILE
);
1247 MODULE_FIRMWARE(AR6004_HW_1_3_DEFAULT_BOARD_DATA_FILE
);