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 1700
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 spin_lock_irqsave(&pipe
->ar_usb
->cs_lock
, flags
);
136 if (!list_empty(&pipe
->urb_list_head
)) {
138 list_first_entry(&pipe
->urb_list_head
,
139 struct ath6kl_urb_context
, link
);
140 list_del(&urb_context
->link
);
143 spin_unlock_irqrestore(&pipe
->ar_usb
->cs_lock
, flags
);
148 static void ath6kl_usb_free_urb_to_pipe(struct ath6kl_usb_pipe
*pipe
,
149 struct ath6kl_urb_context
*urb_context
)
153 spin_lock_irqsave(&pipe
->ar_usb
->cs_lock
, flags
);
156 list_add(&urb_context
->link
, &pipe
->urb_list_head
);
157 spin_unlock_irqrestore(&pipe
->ar_usb
->cs_lock
, flags
);
160 static void ath6kl_usb_cleanup_recv_urb(struct ath6kl_urb_context
*urb_context
)
162 if (urb_context
->skb
!= NULL
) {
163 dev_kfree_skb(urb_context
->skb
);
164 urb_context
->skb
= NULL
;
167 ath6kl_usb_free_urb_to_pipe(urb_context
->pipe
, urb_context
);
170 static inline struct ath6kl_usb
*ath6kl_usb_priv(struct ath6kl
*ar
)
175 /* pipe resource allocation/cleanup */
176 static int ath6kl_usb_alloc_pipe_resources(struct ath6kl_usb_pipe
*pipe
,
179 struct ath6kl_urb_context
*urb_context
;
182 INIT_LIST_HEAD(&pipe
->urb_list_head
);
183 init_usb_anchor(&pipe
->urb_submitted
);
185 for (i
= 0; i
< urb_cnt
; i
++) {
186 urb_context
= kzalloc(sizeof(struct ath6kl_urb_context
),
188 if (urb_context
== NULL
)
189 /* FIXME: set status to -ENOMEM */
192 urb_context
->pipe
= pipe
;
195 * we are only allocate the urb contexts here, the actual URB
196 * is allocated from the kernel as needed to do a transaction
199 ath6kl_usb_free_urb_to_pipe(pipe
, urb_context
);
202 ath6kl_dbg(ATH6KL_DBG_USB
,
203 "ath6kl usb: alloc resources lpipe:%d hpipe:0x%X urbs:%d\n",
204 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
210 static void ath6kl_usb_free_pipe_resources(struct ath6kl_usb_pipe
*pipe
)
212 struct ath6kl_urb_context
*urb_context
;
214 if (pipe
->ar_usb
== NULL
) {
215 /* nothing allocated for this pipe */
219 ath6kl_dbg(ATH6KL_DBG_USB
,
220 "ath6kl usb: free resources lpipe:%d"
221 "hpipe:0x%X urbs:%d avail:%d\n",
222 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
223 pipe
->urb_alloc
, pipe
->urb_cnt
);
225 if (pipe
->urb_alloc
!= pipe
->urb_cnt
) {
226 ath6kl_dbg(ATH6KL_DBG_USB
,
227 "ath6kl usb: urb leak! lpipe:%d"
228 "hpipe:0x%X urbs:%d avail:%d\n",
229 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
230 pipe
->urb_alloc
, pipe
->urb_cnt
);
234 urb_context
= ath6kl_usb_alloc_urb_from_pipe(pipe
);
235 if (urb_context
== NULL
)
242 static void ath6kl_usb_cleanup_pipe_resources(struct ath6kl_usb
*ar_usb
)
246 for (i
= 0; i
< ATH6KL_USB_PIPE_MAX
; i
++)
247 ath6kl_usb_free_pipe_resources(&ar_usb
->pipes
[i
]);
251 static u8
ath6kl_usb_get_logical_pipe_num(struct ath6kl_usb
*ar_usb
,
252 u8 ep_address
, int *urb_count
)
254 u8 pipe_num
= ATH6KL_USB_PIPE_INVALID
;
256 switch (ep_address
) {
257 case ATH6KL_USB_EP_ADDR_APP_CTRL_IN
:
258 pipe_num
= ATH6KL_USB_PIPE_RX_CTRL
;
259 *urb_count
= RX_URB_COUNT
;
261 case ATH6KL_USB_EP_ADDR_APP_DATA_IN
:
262 pipe_num
= ATH6KL_USB_PIPE_RX_DATA
;
263 *urb_count
= RX_URB_COUNT
;
265 case ATH6KL_USB_EP_ADDR_APP_INT_IN
:
266 pipe_num
= ATH6KL_USB_PIPE_RX_INT
;
267 *urb_count
= RX_URB_COUNT
;
269 case ATH6KL_USB_EP_ADDR_APP_DATA2_IN
:
270 pipe_num
= ATH6KL_USB_PIPE_RX_DATA2
;
271 *urb_count
= RX_URB_COUNT
;
273 case ATH6KL_USB_EP_ADDR_APP_CTRL_OUT
:
274 pipe_num
= ATH6KL_USB_PIPE_TX_CTRL
;
275 *urb_count
= TX_URB_COUNT
;
277 case ATH6KL_USB_EP_ADDR_APP_DATA_LP_OUT
:
278 pipe_num
= ATH6KL_USB_PIPE_TX_DATA_LP
;
279 *urb_count
= TX_URB_COUNT
;
281 case ATH6KL_USB_EP_ADDR_APP_DATA_MP_OUT
:
282 pipe_num
= ATH6KL_USB_PIPE_TX_DATA_MP
;
283 *urb_count
= TX_URB_COUNT
;
285 case ATH6KL_USB_EP_ADDR_APP_DATA_HP_OUT
:
286 pipe_num
= ATH6KL_USB_PIPE_TX_DATA_HP
;
287 *urb_count
= TX_URB_COUNT
;
290 /* note: there may be endpoints not currently used */
297 static int ath6kl_usb_setup_pipe_resources(struct ath6kl_usb
*ar_usb
)
299 struct usb_interface
*interface
= ar_usb
->interface
;
300 struct usb_host_interface
*iface_desc
= interface
->cur_altsetting
;
301 struct usb_endpoint_descriptor
*endpoint
;
302 struct ath6kl_usb_pipe
*pipe
;
303 int i
, urbcount
, status
= 0;
306 ath6kl_dbg(ATH6KL_DBG_USB
, "setting up USB Pipes using interface\n");
308 /* walk decriptors and setup pipes */
309 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
310 endpoint
= &iface_desc
->endpoint
[i
].desc
;
312 if (ATH6KL_USB_IS_BULK_EP(endpoint
->bmAttributes
)) {
313 ath6kl_dbg(ATH6KL_DBG_USB
,
314 "%s Bulk Ep:0x%2.2X maxpktsz:%d\n",
316 (endpoint
->bEndpointAddress
) ?
317 "RX" : "TX", endpoint
->bEndpointAddress
,
318 le16_to_cpu(endpoint
->wMaxPacketSize
));
319 } else if (ATH6KL_USB_IS_INT_EP(endpoint
->bmAttributes
)) {
320 ath6kl_dbg(ATH6KL_DBG_USB
,
321 "%s Int Ep:0x%2.2X maxpktsz:%d interval:%d\n",
323 (endpoint
->bEndpointAddress
) ?
324 "RX" : "TX", endpoint
->bEndpointAddress
,
325 le16_to_cpu(endpoint
->wMaxPacketSize
),
326 endpoint
->bInterval
);
327 } else if (ATH6KL_USB_IS_ISOC_EP(endpoint
->bmAttributes
)) {
329 ath6kl_dbg(ATH6KL_DBG_USB
,
330 "%s ISOC Ep:0x%2.2X maxpktsz:%d interval:%d\n",
332 (endpoint
->bEndpointAddress
) ?
333 "RX" : "TX", endpoint
->bEndpointAddress
,
334 le16_to_cpu(endpoint
->wMaxPacketSize
),
335 endpoint
->bInterval
);
340 ath6kl_usb_get_logical_pipe_num(ar_usb
,
341 endpoint
->bEndpointAddress
,
343 if (pipe_num
== ATH6KL_USB_PIPE_INVALID
)
346 pipe
= &ar_usb
->pipes
[pipe_num
];
347 if (pipe
->ar_usb
!= NULL
) {
348 /* hmmm..pipe was already setup */
352 pipe
->ar_usb
= ar_usb
;
353 pipe
->logical_pipe_num
= pipe_num
;
354 pipe
->ep_address
= endpoint
->bEndpointAddress
;
355 pipe
->max_packet_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
357 if (ATH6KL_USB_IS_BULK_EP(endpoint
->bmAttributes
)) {
358 if (ATH6KL_USB_IS_DIR_IN(pipe
->ep_address
)) {
359 pipe
->usb_pipe_handle
=
360 usb_rcvbulkpipe(ar_usb
->udev
,
363 pipe
->usb_pipe_handle
=
364 usb_sndbulkpipe(ar_usb
->udev
,
367 } else if (ATH6KL_USB_IS_INT_EP(endpoint
->bmAttributes
)) {
368 if (ATH6KL_USB_IS_DIR_IN(pipe
->ep_address
)) {
369 pipe
->usb_pipe_handle
=
370 usb_rcvintpipe(ar_usb
->udev
,
373 pipe
->usb_pipe_handle
=
374 usb_sndintpipe(ar_usb
->udev
,
377 } else if (ATH6KL_USB_IS_ISOC_EP(endpoint
->bmAttributes
)) {
379 if (ATH6KL_USB_IS_DIR_IN(pipe
->ep_address
)) {
380 pipe
->usb_pipe_handle
=
381 usb_rcvisocpipe(ar_usb
->udev
,
384 pipe
->usb_pipe_handle
=
385 usb_sndisocpipe(ar_usb
->udev
,
390 pipe
->ep_desc
= endpoint
;
392 if (!ATH6KL_USB_IS_DIR_IN(pipe
->ep_address
))
393 pipe
->flags
|= ATH6KL_USB_PIPE_FLAG_TX
;
395 status
= ath6kl_usb_alloc_pipe_resources(pipe
, urbcount
);
403 /* pipe operations */
404 static void ath6kl_usb_post_recv_transfers(struct ath6kl_usb_pipe
*recv_pipe
,
407 struct ath6kl_urb_context
*urb_context
;
412 urb_context
= ath6kl_usb_alloc_urb_from_pipe(recv_pipe
);
413 if (urb_context
== NULL
)
416 urb_context
->skb
= dev_alloc_skb(buffer_length
);
417 if (urb_context
->skb
== NULL
)
418 goto err_cleanup_urb
;
420 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
422 goto err_cleanup_urb
;
424 usb_fill_bulk_urb(urb
,
425 recv_pipe
->ar_usb
->udev
,
426 recv_pipe
->usb_pipe_handle
,
427 urb_context
->skb
->data
,
429 ath6kl_usb_recv_complete
, urb_context
);
431 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
432 "ath6kl usb: bulk recv submit:%d, 0x%X (ep:0x%2.2X), %d bytes buf:0x%p\n",
433 recv_pipe
->logical_pipe_num
,
434 recv_pipe
->usb_pipe_handle
, recv_pipe
->ep_address
,
435 buffer_length
, urb_context
->skb
);
437 usb_anchor_urb(urb
, &recv_pipe
->urb_submitted
);
438 usb_status
= usb_submit_urb(urb
, GFP_ATOMIC
);
441 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
442 "ath6kl usb : usb bulk recv failed %d\n",
444 usb_unanchor_urb(urb
);
446 goto err_cleanup_urb
;
453 ath6kl_usb_cleanup_recv_urb(urb_context
);
457 static void ath6kl_usb_flush_all(struct ath6kl_usb
*ar_usb
)
461 for (i
= 0; i
< ATH6KL_USB_PIPE_MAX
; i
++) {
462 if (ar_usb
->pipes
[i
].ar_usb
!= NULL
)
463 usb_kill_anchored_urbs(&ar_usb
->pipes
[i
].urb_submitted
);
467 * Flushing any pending I/O may schedule work this call will block
468 * until all scheduled work runs to completion.
470 flush_scheduled_work();
473 static void ath6kl_usb_start_recv_pipes(struct ath6kl_usb
*ar_usb
)
476 * note: control pipe is no longer used
477 * ar_usb->pipes[ATH6KL_USB_PIPE_RX_CTRL].urb_cnt_thresh =
478 * ar_usb->pipes[ATH6KL_USB_PIPE_RX_CTRL].urb_alloc/2;
479 * ath6kl_usb_post_recv_transfers(&ar_usb->
480 * pipes[ATH6KL_USB_PIPE_RX_CTRL],
481 * ATH6KL_USB_RX_BUFFER_SIZE);
484 ar_usb
->pipes
[ATH6KL_USB_PIPE_RX_DATA
].urb_cnt_thresh
=
485 ar_usb
->pipes
[ATH6KL_USB_PIPE_RX_DATA
].urb_alloc
/ 2;
486 ath6kl_usb_post_recv_transfers(&ar_usb
->pipes
[ATH6KL_USB_PIPE_RX_DATA
],
487 ATH6KL_USB_RX_BUFFER_SIZE
);
490 /* hif usb rx/tx completion functions */
491 static void ath6kl_usb_recv_complete(struct urb
*urb
)
493 struct ath6kl_urb_context
*urb_context
= urb
->context
;
494 struct ath6kl_usb_pipe
*pipe
= urb_context
->pipe
;
495 struct sk_buff
*skb
= NULL
;
498 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
499 "%s: recv pipe: %d, stat:%d, len:%d urb:0x%p\n", __func__
,
500 pipe
->logical_pipe_num
, urb
->status
, urb
->actual_length
,
503 if (urb
->status
!= 0) {
505 switch (urb
->status
) {
510 * no need to spew these errors when device
511 * removed or urb killed due to driver shutdown
516 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
517 "%s recv pipe: %d (ep:0x%2.2X), failed:%d\n",
518 __func__
, pipe
->logical_pipe_num
,
519 pipe
->ep_address
, urb
->status
);
522 goto cleanup_recv_urb
;
525 if (urb
->actual_length
== 0)
526 goto cleanup_recv_urb
;
528 skb
= urb_context
->skb
;
530 /* we are going to pass it up */
531 urb_context
->skb
= NULL
;
532 skb_put(skb
, urb
->actual_length
);
534 /* note: queue implements a lock */
535 skb_queue_tail(&pipe
->io_comp_queue
, skb
);
536 schedule_work(&pipe
->io_complete_work
);
539 ath6kl_usb_cleanup_recv_urb(urb_context
);
542 pipe
->urb_cnt
>= pipe
->urb_cnt_thresh
) {
543 /* our free urbs are piling up, post more transfers */
544 ath6kl_usb_post_recv_transfers(pipe
, ATH6KL_USB_RX_BUFFER_SIZE
);
548 static void ath6kl_usb_usb_transmit_complete(struct urb
*urb
)
550 struct ath6kl_urb_context
*urb_context
= urb
->context
;
551 struct ath6kl_usb_pipe
*pipe
= urb_context
->pipe
;
554 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
555 "%s: pipe: %d, stat:%d, len:%d\n",
556 __func__
, pipe
->logical_pipe_num
, urb
->status
,
559 if (urb
->status
!= 0) {
560 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
561 "%s: pipe: %d, failed:%d\n",
562 __func__
, pipe
->logical_pipe_num
, urb
->status
);
565 skb
= urb_context
->skb
;
566 urb_context
->skb
= NULL
;
567 ath6kl_usb_free_urb_to_pipe(urb_context
->pipe
, urb_context
);
569 /* note: queue implements a lock */
570 skb_queue_tail(&pipe
->io_comp_queue
, skb
);
571 schedule_work(&pipe
->io_complete_work
);
574 static void ath6kl_usb_io_comp_work(struct work_struct
*work
)
576 struct ath6kl_usb_pipe
*pipe
= container_of(work
,
577 struct ath6kl_usb_pipe
,
579 struct ath6kl_usb
*ar_usb
;
582 ar_usb
= pipe
->ar_usb
;
584 while ((skb
= skb_dequeue(&pipe
->io_comp_queue
))) {
585 if (pipe
->flags
& ATH6KL_USB_PIPE_FLAG_TX
) {
586 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
587 "ath6kl usb xmit callback buf:0x%p\n", skb
);
588 ath6kl_core_tx_complete(ar_usb
->ar
, skb
);
590 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
591 "ath6kl usb recv callback buf:0x%p\n", skb
);
592 ath6kl_core_rx_complete(ar_usb
->ar
, skb
,
593 pipe
->logical_pipe_num
);
598 #define ATH6KL_USB_MAX_DIAG_CMD (sizeof(struct ath6kl_usb_ctrl_diag_cmd_write))
599 #define ATH6KL_USB_MAX_DIAG_RESP (sizeof(struct ath6kl_usb_ctrl_diag_resp_read))
601 static void ath6kl_usb_destroy(struct ath6kl_usb
*ar_usb
)
603 ath6kl_usb_flush_all(ar_usb
);
605 ath6kl_usb_cleanup_pipe_resources(ar_usb
);
607 usb_set_intfdata(ar_usb
->interface
, NULL
);
609 kfree(ar_usb
->diag_cmd_buffer
);
610 kfree(ar_usb
->diag_resp_buffer
);
615 static struct ath6kl_usb
*ath6kl_usb_create(struct usb_interface
*interface
)
617 struct usb_device
*dev
= interface_to_usbdev(interface
);
618 struct ath6kl_usb
*ar_usb
;
619 struct ath6kl_usb_pipe
*pipe
;
623 ar_usb
= kzalloc(sizeof(struct ath6kl_usb
), GFP_KERNEL
);
625 goto fail_ath6kl_usb_create
;
627 usb_set_intfdata(interface
, ar_usb
);
628 spin_lock_init(&(ar_usb
->cs_lock
));
630 ar_usb
->interface
= interface
;
632 for (i
= 0; i
< ATH6KL_USB_PIPE_MAX
; i
++) {
633 pipe
= &ar_usb
->pipes
[i
];
634 INIT_WORK(&pipe
->io_complete_work
,
635 ath6kl_usb_io_comp_work
);
636 skb_queue_head_init(&pipe
->io_comp_queue
);
639 ar_usb
->diag_cmd_buffer
= kzalloc(ATH6KL_USB_MAX_DIAG_CMD
, GFP_KERNEL
);
640 if (ar_usb
->diag_cmd_buffer
== NULL
) {
642 goto fail_ath6kl_usb_create
;
645 ar_usb
->diag_resp_buffer
= kzalloc(ATH6KL_USB_MAX_DIAG_RESP
,
647 if (ar_usb
->diag_resp_buffer
== NULL
) {
649 goto fail_ath6kl_usb_create
;
652 status
= ath6kl_usb_setup_pipe_resources(ar_usb
);
654 fail_ath6kl_usb_create
:
656 ath6kl_usb_destroy(ar_usb
);
662 static void ath6kl_usb_device_detached(struct usb_interface
*interface
)
664 struct ath6kl_usb
*ar_usb
;
666 ar_usb
= usb_get_intfdata(interface
);
670 ath6kl_stop_txrx(ar_usb
->ar
);
672 /* Delay to wait for the target to reboot */
674 ath6kl_core_cleanup(ar_usb
->ar
);
675 ath6kl_usb_destroy(ar_usb
);
678 /* exported hif usb APIs for htc pipe */
679 static void hif_start(struct ath6kl
*ar
)
681 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
684 ath6kl_usb_start_recv_pipes(device
);
686 /* set the TX resource avail threshold for each TX pipe */
687 for (i
= ATH6KL_USB_PIPE_TX_CTRL
;
688 i
<= ATH6KL_USB_PIPE_TX_DATA_HP
; i
++) {
689 device
->pipes
[i
].urb_cnt_thresh
=
690 device
->pipes
[i
].urb_alloc
/ 2;
694 static int ath6kl_usb_send(struct ath6kl
*ar
, u8 PipeID
,
695 struct sk_buff
*hdr_skb
, struct sk_buff
*skb
)
697 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
698 struct ath6kl_usb_pipe
*pipe
= &device
->pipes
[PipeID
];
699 struct ath6kl_urb_context
*urb_context
;
700 int usb_status
, status
= 0;
705 ath6kl_dbg(ATH6KL_DBG_USB_BULK
, "+%s pipe : %d, buf:0x%p\n",
706 __func__
, PipeID
, skb
);
708 urb_context
= ath6kl_usb_alloc_urb_from_pipe(pipe
);
710 if (urb_context
== NULL
) {
712 * TODO: it is possible to run out of urbs if
713 * 2 endpoints map to the same pipe ID
715 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
716 "%s pipe:%d no urbs left. URB Cnt : %d\n",
717 __func__
, PipeID
, pipe
->urb_cnt
);
722 urb_context
->skb
= skb
;
727 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
730 ath6kl_usb_free_urb_to_pipe(urb_context
->pipe
,
735 usb_fill_bulk_urb(urb
,
737 pipe
->usb_pipe_handle
,
740 ath6kl_usb_usb_transmit_complete
, urb_context
);
742 if ((len
% pipe
->max_packet_size
) == 0) {
743 /* hit a max packet boundary on this pipe */
744 urb
->transfer_flags
|= URB_ZERO_PACKET
;
747 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
748 "athusb bulk send submit:%d, 0x%X (ep:0x%2.2X), %d bytes\n",
749 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
750 pipe
->ep_address
, len
);
752 usb_anchor_urb(urb
, &pipe
->urb_submitted
);
753 usb_status
= usb_submit_urb(urb
, GFP_ATOMIC
);
756 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
757 "ath6kl usb : usb bulk transmit failed %d\n",
759 usb_unanchor_urb(urb
);
760 ath6kl_usb_free_urb_to_pipe(urb_context
->pipe
,
770 static void hif_stop(struct ath6kl
*ar
)
772 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
774 ath6kl_usb_flush_all(device
);
777 static void ath6kl_usb_get_default_pipe(struct ath6kl
*ar
,
778 u8
*ul_pipe
, u8
*dl_pipe
)
780 *ul_pipe
= ATH6KL_USB_PIPE_TX_CTRL
;
781 *dl_pipe
= ATH6KL_USB_PIPE_RX_CTRL
;
784 static int ath6kl_usb_map_service_pipe(struct ath6kl
*ar
, u16 svc_id
,
785 u8
*ul_pipe
, u8
*dl_pipe
)
790 case HTC_CTRL_RSVD_SVC
:
791 case WMI_CONTROL_SVC
:
792 *ul_pipe
= ATH6KL_USB_PIPE_TX_CTRL
;
793 /* due to large control packets, shift to data pipe */
794 *dl_pipe
= ATH6KL_USB_PIPE_RX_DATA
;
796 case WMI_DATA_BE_SVC
:
797 case WMI_DATA_BK_SVC
:
798 *ul_pipe
= ATH6KL_USB_PIPE_TX_DATA_LP
;
800 * Disable rxdata2 directly, it will be enabled
801 * if FW enable rxdata2
803 *dl_pipe
= ATH6KL_USB_PIPE_RX_DATA
;
805 case WMI_DATA_VI_SVC
:
806 *ul_pipe
= ATH6KL_USB_PIPE_TX_DATA_MP
;
808 * Disable rxdata2 directly, it will be enabled
809 * if FW enable rxdata2
811 *dl_pipe
= ATH6KL_USB_PIPE_RX_DATA
;
813 case WMI_DATA_VO_SVC
:
814 *ul_pipe
= ATH6KL_USB_PIPE_TX_DATA_HP
;
816 * Disable rxdata2 directly, it will be enabled
817 * if FW enable rxdata2
819 *dl_pipe
= ATH6KL_USB_PIPE_RX_DATA
;
829 static u16
ath6kl_usb_get_free_queue_number(struct ath6kl
*ar
, u8 pipe_id
)
831 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
833 return device
->pipes
[pipe_id
].urb_cnt
;
836 static void hif_detach_htc(struct ath6kl
*ar
)
838 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
840 ath6kl_usb_flush_all(device
);
843 static int ath6kl_usb_submit_ctrl_out(struct ath6kl_usb
*ar_usb
,
844 u8 req
, u16 value
, u16 index
, void *data
,
851 buf
= kmalloc(size
, GFP_KERNEL
);
855 memcpy(buf
, data
, size
);
858 /* note: if successful returns number of bytes transfered */
859 ret
= usb_control_msg(ar_usb
->udev
,
860 usb_sndctrlpipe(ar_usb
->udev
, 0),
862 USB_DIR_OUT
| USB_TYPE_VENDOR
|
863 USB_RECIP_DEVICE
, value
, index
, buf
,
867 ath6kl_dbg(ATH6KL_DBG_USB
, "%s failed,result = %d\n",
876 static int ath6kl_usb_submit_ctrl_in(struct ath6kl_usb
*ar_usb
,
877 u8 req
, u16 value
, u16 index
, void *data
,
884 buf
= kmalloc(size
, GFP_KERNEL
);
889 /* note: if successful returns number of bytes transfered */
890 ret
= usb_control_msg(ar_usb
->udev
,
891 usb_rcvctrlpipe(ar_usb
->udev
, 0),
893 USB_DIR_IN
| USB_TYPE_VENDOR
|
894 USB_RECIP_DEVICE
, value
, index
, buf
,
898 ath6kl_dbg(ATH6KL_DBG_USB
, "%s failed,result = %d\n",
902 memcpy((u8
*) data
, buf
, size
);
909 static int ath6kl_usb_ctrl_msg_exchange(struct ath6kl_usb
*ar_usb
,
910 u8 req_val
, u8
*req_buf
, u32 req_len
,
911 u8 resp_val
, u8
*resp_buf
, u32
*resp_len
)
916 ret
= ath6kl_usb_submit_ctrl_out(ar_usb
, req_val
, 0, 0,
922 if (resp_buf
== NULL
) {
923 /* no expected response */
928 ret
= ath6kl_usb_submit_ctrl_in(ar_usb
, resp_val
, 0, 0,
929 resp_buf
, *resp_len
);
934 static int ath6kl_usb_diag_read32(struct ath6kl
*ar
, u32 address
, u32
*data
)
936 struct ath6kl_usb
*ar_usb
= ar
->hif_priv
;
937 struct ath6kl_usb_ctrl_diag_resp_read
*resp
;
938 struct ath6kl_usb_ctrl_diag_cmd_read
*cmd
;
942 cmd
= (struct ath6kl_usb_ctrl_diag_cmd_read
*) ar_usb
->diag_cmd_buffer
;
944 memset(cmd
, 0, sizeof(*cmd
));
945 cmd
->cmd
= ATH6KL_USB_CTRL_DIAG_CC_READ
;
946 cmd
->address
= cpu_to_le32(address
);
947 resp_len
= sizeof(*resp
);
949 ret
= ath6kl_usb_ctrl_msg_exchange(ar_usb
,
950 ATH6KL_USB_CONTROL_REQ_DIAG_CMD
,
952 sizeof(struct ath6kl_usb_ctrl_diag_cmd_write
),
953 ATH6KL_USB_CONTROL_REQ_DIAG_RESP
,
954 ar_usb
->diag_resp_buffer
, &resp_len
);
959 resp
= (struct ath6kl_usb_ctrl_diag_resp_read
*)
960 ar_usb
->diag_resp_buffer
;
962 *data
= le32_to_cpu(resp
->value
);
967 static int ath6kl_usb_diag_write32(struct ath6kl
*ar
, u32 address
, __le32 data
)
969 struct ath6kl_usb
*ar_usb
= ar
->hif_priv
;
970 struct ath6kl_usb_ctrl_diag_cmd_write
*cmd
;
972 cmd
= (struct ath6kl_usb_ctrl_diag_cmd_write
*) ar_usb
->diag_cmd_buffer
;
974 memset(cmd
, 0, sizeof(struct ath6kl_usb_ctrl_diag_cmd_write
));
975 cmd
->cmd
= cpu_to_le32(ATH6KL_USB_CTRL_DIAG_CC_WRITE
);
976 cmd
->address
= cpu_to_le32(address
);
979 return ath6kl_usb_ctrl_msg_exchange(ar_usb
,
980 ATH6KL_USB_CONTROL_REQ_DIAG_CMD
,
987 static int ath6kl_usb_bmi_read(struct ath6kl
*ar
, u8
*buf
, u32 len
)
989 struct ath6kl_usb
*ar_usb
= ar
->hif_priv
;
993 ret
= ath6kl_usb_submit_ctrl_in(ar_usb
,
994 ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP
,
997 ath6kl_err("Unable to read the bmi data from the device: %d\n",
1005 static int ath6kl_usb_bmi_write(struct ath6kl
*ar
, u8
*buf
, u32 len
)
1007 struct ath6kl_usb
*ar_usb
= ar
->hif_priv
;
1011 ret
= ath6kl_usb_submit_ctrl_out(ar_usb
,
1012 ATH6KL_USB_CONTROL_REQ_SEND_BMI_CMD
,
1015 ath6kl_err("unable to send the bmi data to the device: %d\n",
1023 static int ath6kl_usb_power_on(struct ath6kl
*ar
)
1029 static int ath6kl_usb_power_off(struct ath6kl
*ar
)
1035 static void ath6kl_usb_stop(struct ath6kl
*ar
)
1040 static void ath6kl_usb_cleanup_scatter(struct ath6kl
*ar
)
1043 * USB doesn't support it. Just return.
1048 static const struct ath6kl_hif_ops ath6kl_usb_ops
= {
1049 .diag_read32
= ath6kl_usb_diag_read32
,
1050 .diag_write32
= ath6kl_usb_diag_write32
,
1051 .bmi_read
= ath6kl_usb_bmi_read
,
1052 .bmi_write
= ath6kl_usb_bmi_write
,
1053 .power_on
= ath6kl_usb_power_on
,
1054 .power_off
= ath6kl_usb_power_off
,
1055 .stop
= ath6kl_usb_stop
,
1056 .pipe_send
= ath6kl_usb_send
,
1057 .pipe_get_default
= ath6kl_usb_get_default_pipe
,
1058 .pipe_map_service
= ath6kl_usb_map_service_pipe
,
1059 .pipe_get_free_queue_number
= ath6kl_usb_get_free_queue_number
,
1060 .cleanup_scatter
= ath6kl_usb_cleanup_scatter
,
1063 /* ath6kl usb driver registered functions */
1064 static int ath6kl_usb_probe(struct usb_interface
*interface
,
1065 const struct usb_device_id
*id
)
1067 struct usb_device
*dev
= interface_to_usbdev(interface
);
1069 struct ath6kl_usb
*ar_usb
= NULL
;
1070 int vendor_id
, product_id
;
1075 vendor_id
= le16_to_cpu(dev
->descriptor
.idVendor
);
1076 product_id
= le16_to_cpu(dev
->descriptor
.idProduct
);
1078 ath6kl_dbg(ATH6KL_DBG_USB
, "vendor_id = %04x\n", vendor_id
);
1079 ath6kl_dbg(ATH6KL_DBG_USB
, "product_id = %04x\n", product_id
);
1081 if (interface
->cur_altsetting
)
1082 ath6kl_dbg(ATH6KL_DBG_USB
, "USB Interface %d\n",
1083 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
1086 if (dev
->speed
== USB_SPEED_HIGH
)
1087 ath6kl_dbg(ATH6KL_DBG_USB
, "USB 2.0 Host\n");
1089 ath6kl_dbg(ATH6KL_DBG_USB
, "USB 1.1 Host\n");
1091 ar_usb
= ath6kl_usb_create(interface
);
1093 if (ar_usb
== NULL
) {
1098 ar
= ath6kl_core_create(&ar_usb
->udev
->dev
);
1100 ath6kl_err("Failed to alloc ath6kl core\n");
1102 goto err_usb_destroy
;
1105 ar
->hif_priv
= ar_usb
;
1106 ar
->hif_type
= ATH6KL_HIF_TYPE_USB
;
1107 ar
->hif_ops
= &ath6kl_usb_ops
;
1108 ar
->mbox_info
.block_size
= 16;
1109 ar
->bmi
.max_data_size
= 252;
1113 ret
= ath6kl_core_init(ar
, ATH6KL_HTC_TYPE_PIPE
);
1115 ath6kl_err("Failed to init ath6kl core: %d\n", ret
);
1122 ath6kl_core_destroy(ar
);
1124 ath6kl_usb_destroy(ar_usb
);
1131 static void ath6kl_usb_remove(struct usb_interface
*interface
)
1133 usb_put_dev(interface_to_usbdev(interface
));
1134 ath6kl_usb_device_detached(interface
);
1139 static int ath6kl_usb_suspend(struct usb_interface
*interface
,
1140 pm_message_t message
)
1142 struct ath6kl_usb
*device
;
1143 device
= usb_get_intfdata(interface
);
1145 ath6kl_usb_flush_all(device
);
1149 static int ath6kl_usb_resume(struct usb_interface
*interface
)
1151 struct ath6kl_usb
*device
;
1152 device
= usb_get_intfdata(interface
);
1154 ath6kl_usb_post_recv_transfers(&device
->pipes
[ATH6KL_USB_PIPE_RX_DATA
],
1155 ATH6KL_USB_RX_BUFFER_SIZE
);
1156 ath6kl_usb_post_recv_transfers(&device
->pipes
[ATH6KL_USB_PIPE_RX_DATA2
],
1157 ATH6KL_USB_RX_BUFFER_SIZE
);
1162 static int ath6kl_usb_reset_resume(struct usb_interface
*intf
)
1164 if (usb_get_intfdata(intf
))
1165 ath6kl_usb_remove(intf
);
1171 #define ath6kl_usb_suspend NULL
1172 #define ath6kl_usb_resume NULL
1173 #define ath6kl_usb_reset_resume NULL
1177 /* table of devices that work with this driver */
1178 static struct usb_device_id ath6kl_usb_ids
[] = {
1179 {USB_DEVICE(0x0cf3, 0x9374)},
1180 { /* Terminating entry */ },
1183 MODULE_DEVICE_TABLE(usb
, ath6kl_usb_ids
);
1185 static struct usb_driver ath6kl_usb_driver
= {
1186 .name
= "ath6kl_usb",
1187 .probe
= ath6kl_usb_probe
,
1188 .suspend
= ath6kl_usb_suspend
,
1189 .resume
= ath6kl_usb_resume
,
1190 .reset_resume
= ath6kl_usb_reset_resume
,
1191 .disconnect
= ath6kl_usb_remove
,
1192 .id_table
= ath6kl_usb_ids
,
1193 .supports_autosuspend
= true,
1196 static int ath6kl_usb_init(void)
1198 usb_register(&ath6kl_usb_driver
);
1202 static void ath6kl_usb_exit(void)
1204 usb_deregister(&ath6kl_usb_driver
);
1207 module_init(ath6kl_usb_init
);
1208 module_exit(ath6kl_usb_exit
);
1210 MODULE_AUTHOR("Atheros Communications, Inc.");
1211 MODULE_DESCRIPTION("Driver support for Atheros AR600x USB devices");
1212 MODULE_LICENSE("Dual BSD/GPL");
1213 MODULE_FIRMWARE(AR6004_HW_1_0_FIRMWARE_FILE
);
1214 MODULE_FIRMWARE(AR6004_HW_1_0_BOARD_DATA_FILE
);
1215 MODULE_FIRMWARE(AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE
);
1216 MODULE_FIRMWARE(AR6004_HW_1_1_FIRMWARE_FILE
);
1217 MODULE_FIRMWARE(AR6004_HW_1_1_BOARD_DATA_FILE
);
1218 MODULE_FIRMWARE(AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE
);
1219 MODULE_FIRMWARE(AR6004_HW_1_2_FIRMWARE_FILE
);
1220 MODULE_FIRMWARE(AR6004_HW_1_2_BOARD_DATA_FILE
);
1221 MODULE_FIRMWARE(AR6004_HW_1_2_DEFAULT_BOARD_DATA_FILE
);