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
];
74 struct workqueue_struct
*wq
;
78 struct ath6kl_urb_context
{
79 struct list_head link
;
80 struct ath6kl_usb_pipe
*pipe
;
85 /* USB endpoint definitions */
86 #define ATH6KL_USB_EP_ADDR_APP_CTRL_IN 0x81
87 #define ATH6KL_USB_EP_ADDR_APP_DATA_IN 0x82
88 #define ATH6KL_USB_EP_ADDR_APP_DATA2_IN 0x83
89 #define ATH6KL_USB_EP_ADDR_APP_INT_IN 0x84
91 #define ATH6KL_USB_EP_ADDR_APP_CTRL_OUT 0x01
92 #define ATH6KL_USB_EP_ADDR_APP_DATA_LP_OUT 0x02
93 #define ATH6KL_USB_EP_ADDR_APP_DATA_MP_OUT 0x03
94 #define ATH6KL_USB_EP_ADDR_APP_DATA_HP_OUT 0x04
96 /* diagnostic command defnitions */
97 #define ATH6KL_USB_CONTROL_REQ_SEND_BMI_CMD 1
98 #define ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP 2
99 #define ATH6KL_USB_CONTROL_REQ_DIAG_CMD 3
100 #define ATH6KL_USB_CONTROL_REQ_DIAG_RESP 4
102 #define ATH6KL_USB_CTRL_DIAG_CC_READ 0
103 #define ATH6KL_USB_CTRL_DIAG_CC_WRITE 1
105 struct ath6kl_usb_ctrl_diag_cmd_write
{
112 struct ath6kl_usb_ctrl_diag_cmd_read
{
117 struct ath6kl_usb_ctrl_diag_resp_read
{
121 /* function declarations */
122 static void ath6kl_usb_recv_complete(struct urb
*urb
);
124 #define ATH6KL_USB_IS_BULK_EP(attr) (((attr) & 3) == 0x02)
125 #define ATH6KL_USB_IS_INT_EP(attr) (((attr) & 3) == 0x03)
126 #define ATH6KL_USB_IS_ISOC_EP(attr) (((attr) & 3) == 0x01)
127 #define ATH6KL_USB_IS_DIR_IN(addr) ((addr) & 0x80)
129 /* pipe/urb operations */
130 static struct ath6kl_urb_context
*
131 ath6kl_usb_alloc_urb_from_pipe(struct ath6kl_usb_pipe
*pipe
)
133 struct ath6kl_urb_context
*urb_context
= NULL
;
136 /* bail if this pipe is not initialized */
140 spin_lock_irqsave(&pipe
->ar_usb
->cs_lock
, flags
);
141 if (!list_empty(&pipe
->urb_list_head
)) {
143 list_first_entry(&pipe
->urb_list_head
,
144 struct ath6kl_urb_context
, link
);
145 list_del(&urb_context
->link
);
148 spin_unlock_irqrestore(&pipe
->ar_usb
->cs_lock
, flags
);
153 static void ath6kl_usb_free_urb_to_pipe(struct ath6kl_usb_pipe
*pipe
,
154 struct ath6kl_urb_context
*urb_context
)
158 /* bail if this pipe is not initialized */
162 spin_lock_irqsave(&pipe
->ar_usb
->cs_lock
, flags
);
165 list_add(&urb_context
->link
, &pipe
->urb_list_head
);
166 spin_unlock_irqrestore(&pipe
->ar_usb
->cs_lock
, flags
);
169 static void ath6kl_usb_cleanup_recv_urb(struct ath6kl_urb_context
*urb_context
)
171 dev_kfree_skb(urb_context
->skb
);
172 urb_context
->skb
= NULL
;
174 ath6kl_usb_free_urb_to_pipe(urb_context
->pipe
, urb_context
);
177 static inline struct ath6kl_usb
*ath6kl_usb_priv(struct ath6kl
*ar
)
182 /* pipe resource allocation/cleanup */
183 static int ath6kl_usb_alloc_pipe_resources(struct ath6kl_usb_pipe
*pipe
,
186 struct ath6kl_urb_context
*urb_context
;
189 INIT_LIST_HEAD(&pipe
->urb_list_head
);
190 init_usb_anchor(&pipe
->urb_submitted
);
192 for (i
= 0; i
< urb_cnt
; i
++) {
193 urb_context
= kzalloc(sizeof(struct ath6kl_urb_context
),
195 if (urb_context
== NULL
) {
197 goto fail_alloc_pipe_resources
;
200 urb_context
->pipe
= pipe
;
203 * we are only allocate the urb contexts here, the actual URB
204 * is allocated from the kernel as needed to do a transaction
207 ath6kl_usb_free_urb_to_pipe(pipe
, urb_context
);
210 ath6kl_dbg(ATH6KL_DBG_USB
,
211 "ath6kl usb: alloc resources lpipe:%d hpipe:0x%X urbs:%d\n",
212 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
215 fail_alloc_pipe_resources
:
219 static void ath6kl_usb_free_pipe_resources(struct ath6kl_usb_pipe
*pipe
)
221 struct ath6kl_urb_context
*urb_context
;
223 if (pipe
->ar_usb
== NULL
) {
224 /* nothing allocated for this pipe */
228 ath6kl_dbg(ATH6KL_DBG_USB
,
229 "ath6kl usb: free resources lpipe:%d"
230 "hpipe:0x%X urbs:%d avail:%d\n",
231 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
232 pipe
->urb_alloc
, pipe
->urb_cnt
);
234 if (pipe
->urb_alloc
!= pipe
->urb_cnt
) {
235 ath6kl_dbg(ATH6KL_DBG_USB
,
236 "ath6kl usb: urb leak! lpipe:%d"
237 "hpipe:0x%X urbs:%d avail:%d\n",
238 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
239 pipe
->urb_alloc
, pipe
->urb_cnt
);
243 urb_context
= ath6kl_usb_alloc_urb_from_pipe(pipe
);
244 if (urb_context
== NULL
)
250 static void ath6kl_usb_cleanup_pipe_resources(struct ath6kl_usb
*ar_usb
)
254 for (i
= 0; i
< ATH6KL_USB_PIPE_MAX
; i
++)
255 ath6kl_usb_free_pipe_resources(&ar_usb
->pipes
[i
]);
258 static u8
ath6kl_usb_get_logical_pipe_num(struct ath6kl_usb
*ar_usb
,
259 u8 ep_address
, int *urb_count
)
261 u8 pipe_num
= ATH6KL_USB_PIPE_INVALID
;
263 switch (ep_address
) {
264 case ATH6KL_USB_EP_ADDR_APP_CTRL_IN
:
265 pipe_num
= ATH6KL_USB_PIPE_RX_CTRL
;
266 *urb_count
= RX_URB_COUNT
;
268 case ATH6KL_USB_EP_ADDR_APP_DATA_IN
:
269 pipe_num
= ATH6KL_USB_PIPE_RX_DATA
;
270 *urb_count
= RX_URB_COUNT
;
272 case ATH6KL_USB_EP_ADDR_APP_INT_IN
:
273 pipe_num
= ATH6KL_USB_PIPE_RX_INT
;
274 *urb_count
= RX_URB_COUNT
;
276 case ATH6KL_USB_EP_ADDR_APP_DATA2_IN
:
277 pipe_num
= ATH6KL_USB_PIPE_RX_DATA2
;
278 *urb_count
= RX_URB_COUNT
;
280 case ATH6KL_USB_EP_ADDR_APP_CTRL_OUT
:
281 pipe_num
= ATH6KL_USB_PIPE_TX_CTRL
;
282 *urb_count
= TX_URB_COUNT
;
284 case ATH6KL_USB_EP_ADDR_APP_DATA_LP_OUT
:
285 pipe_num
= ATH6KL_USB_PIPE_TX_DATA_LP
;
286 *urb_count
= TX_URB_COUNT
;
288 case ATH6KL_USB_EP_ADDR_APP_DATA_MP_OUT
:
289 pipe_num
= ATH6KL_USB_PIPE_TX_DATA_MP
;
290 *urb_count
= TX_URB_COUNT
;
292 case ATH6KL_USB_EP_ADDR_APP_DATA_HP_OUT
:
293 pipe_num
= ATH6KL_USB_PIPE_TX_DATA_HP
;
294 *urb_count
= TX_URB_COUNT
;
297 /* note: there may be endpoints not currently used */
304 static int ath6kl_usb_setup_pipe_resources(struct ath6kl_usb
*ar_usb
)
306 struct usb_interface
*interface
= ar_usb
->interface
;
307 struct usb_host_interface
*iface_desc
= interface
->cur_altsetting
;
308 struct usb_endpoint_descriptor
*endpoint
;
309 struct ath6kl_usb_pipe
*pipe
;
310 int i
, urbcount
, status
= 0;
313 ath6kl_dbg(ATH6KL_DBG_USB
, "setting up USB Pipes using interface\n");
315 /* walk descriptors and setup pipes */
316 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
317 endpoint
= &iface_desc
->endpoint
[i
].desc
;
319 if (ATH6KL_USB_IS_BULK_EP(endpoint
->bmAttributes
)) {
320 ath6kl_dbg(ATH6KL_DBG_USB
,
321 "%s Bulk Ep:0x%2.2X maxpktsz:%d\n",
323 (endpoint
->bEndpointAddress
) ?
324 "RX" : "TX", endpoint
->bEndpointAddress
,
325 le16_to_cpu(endpoint
->wMaxPacketSize
));
326 } else if (ATH6KL_USB_IS_INT_EP(endpoint
->bmAttributes
)) {
327 ath6kl_dbg(ATH6KL_DBG_USB
,
328 "%s Int Ep:0x%2.2X maxpktsz:%d interval:%d\n",
330 (endpoint
->bEndpointAddress
) ?
331 "RX" : "TX", endpoint
->bEndpointAddress
,
332 le16_to_cpu(endpoint
->wMaxPacketSize
),
333 endpoint
->bInterval
);
334 } else if (ATH6KL_USB_IS_ISOC_EP(endpoint
->bmAttributes
)) {
336 ath6kl_dbg(ATH6KL_DBG_USB
,
337 "%s ISOC Ep:0x%2.2X maxpktsz:%d interval:%d\n",
339 (endpoint
->bEndpointAddress
) ?
340 "RX" : "TX", endpoint
->bEndpointAddress
,
341 le16_to_cpu(endpoint
->wMaxPacketSize
),
342 endpoint
->bInterval
);
345 /* Ignore broken descriptors. */
346 if (usb_endpoint_maxp(endpoint
) == 0)
352 ath6kl_usb_get_logical_pipe_num(ar_usb
,
353 endpoint
->bEndpointAddress
,
355 if (pipe_num
== ATH6KL_USB_PIPE_INVALID
)
358 pipe
= &ar_usb
->pipes
[pipe_num
];
359 if (pipe
->ar_usb
!= NULL
) {
360 /* hmmm..pipe was already setup */
364 pipe
->ar_usb
= ar_usb
;
365 pipe
->logical_pipe_num
= pipe_num
;
366 pipe
->ep_address
= endpoint
->bEndpointAddress
;
367 pipe
->max_packet_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
369 if (ATH6KL_USB_IS_BULK_EP(endpoint
->bmAttributes
)) {
370 if (ATH6KL_USB_IS_DIR_IN(pipe
->ep_address
)) {
371 pipe
->usb_pipe_handle
=
372 usb_rcvbulkpipe(ar_usb
->udev
,
375 pipe
->usb_pipe_handle
=
376 usb_sndbulkpipe(ar_usb
->udev
,
379 } else if (ATH6KL_USB_IS_INT_EP(endpoint
->bmAttributes
)) {
380 if (ATH6KL_USB_IS_DIR_IN(pipe
->ep_address
)) {
381 pipe
->usb_pipe_handle
=
382 usb_rcvintpipe(ar_usb
->udev
,
385 pipe
->usb_pipe_handle
=
386 usb_sndintpipe(ar_usb
->udev
,
389 } else if (ATH6KL_USB_IS_ISOC_EP(endpoint
->bmAttributes
)) {
391 if (ATH6KL_USB_IS_DIR_IN(pipe
->ep_address
)) {
392 pipe
->usb_pipe_handle
=
393 usb_rcvisocpipe(ar_usb
->udev
,
396 pipe
->usb_pipe_handle
=
397 usb_sndisocpipe(ar_usb
->udev
,
402 pipe
->ep_desc
= endpoint
;
404 if (!ATH6KL_USB_IS_DIR_IN(pipe
->ep_address
))
405 pipe
->flags
|= ATH6KL_USB_PIPE_FLAG_TX
;
407 status
= ath6kl_usb_alloc_pipe_resources(pipe
, urbcount
);
415 /* pipe operations */
416 static void ath6kl_usb_post_recv_transfers(struct ath6kl_usb_pipe
*recv_pipe
,
419 struct ath6kl_urb_context
*urb_context
;
424 urb_context
= ath6kl_usb_alloc_urb_from_pipe(recv_pipe
);
425 if (urb_context
== NULL
)
428 urb_context
->skb
= dev_alloc_skb(buffer_length
);
429 if (urb_context
->skb
== NULL
)
430 goto err_cleanup_urb
;
432 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
434 goto err_cleanup_urb
;
436 usb_fill_bulk_urb(urb
,
437 recv_pipe
->ar_usb
->udev
,
438 recv_pipe
->usb_pipe_handle
,
439 urb_context
->skb
->data
,
441 ath6kl_usb_recv_complete
, urb_context
);
443 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
444 "ath6kl usb: bulk recv submit:%d, 0x%X (ep:0x%2.2X), %d bytes buf:0x%p\n",
445 recv_pipe
->logical_pipe_num
,
446 recv_pipe
->usb_pipe_handle
, recv_pipe
->ep_address
,
447 buffer_length
, urb_context
->skb
);
449 usb_anchor_urb(urb
, &recv_pipe
->urb_submitted
);
450 usb_status
= usb_submit_urb(urb
, GFP_ATOMIC
);
453 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
454 "ath6kl usb : usb bulk recv failed %d\n",
456 usb_unanchor_urb(urb
);
458 goto err_cleanup_urb
;
465 ath6kl_usb_cleanup_recv_urb(urb_context
);
469 static void ath6kl_usb_flush_all(struct ath6kl_usb
*ar_usb
)
473 for (i
= 0; i
< ATH6KL_USB_PIPE_MAX
; i
++) {
474 if (ar_usb
->pipes
[i
].ar_usb
!= NULL
)
475 usb_kill_anchored_urbs(&ar_usb
->pipes
[i
].urb_submitted
);
479 * Flushing any pending I/O may schedule work this call will block
480 * until all scheduled work runs to completion.
482 flush_workqueue(ar_usb
->wq
);
485 static void ath6kl_usb_start_recv_pipes(struct ath6kl_usb
*ar_usb
)
488 * note: control pipe is no longer used
489 * ar_usb->pipes[ATH6KL_USB_PIPE_RX_CTRL].urb_cnt_thresh =
490 * ar_usb->pipes[ATH6KL_USB_PIPE_RX_CTRL].urb_alloc/2;
491 * ath6kl_usb_post_recv_transfers(&ar_usb->
492 * pipes[ATH6KL_USB_PIPE_RX_CTRL],
493 * ATH6KL_USB_RX_BUFFER_SIZE);
496 ar_usb
->pipes
[ATH6KL_USB_PIPE_RX_DATA
].urb_cnt_thresh
= 1;
498 ath6kl_usb_post_recv_transfers(&ar_usb
->pipes
[ATH6KL_USB_PIPE_RX_DATA
],
499 ATH6KL_USB_RX_BUFFER_SIZE
);
502 /* hif usb rx/tx completion functions */
503 static void ath6kl_usb_recv_complete(struct urb
*urb
)
505 struct ath6kl_urb_context
*urb_context
= urb
->context
;
506 struct ath6kl_usb_pipe
*pipe
= urb_context
->pipe
;
507 struct sk_buff
*skb
= NULL
;
510 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
511 "%s: recv pipe: %d, stat:%d, len:%d urb:0x%p\n", __func__
,
512 pipe
->logical_pipe_num
, urb
->status
, urb
->actual_length
,
515 if (urb
->status
!= 0) {
517 switch (urb
->status
) {
522 * no need to spew these errors when device
523 * removed or urb killed due to driver shutdown
528 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
529 "%s recv pipe: %d (ep:0x%2.2X), failed:%d\n",
530 __func__
, pipe
->logical_pipe_num
,
531 pipe
->ep_address
, urb
->status
);
534 goto cleanup_recv_urb
;
537 if (urb
->actual_length
== 0)
538 goto cleanup_recv_urb
;
540 skb
= urb_context
->skb
;
542 /* we are going to pass it up */
543 urb_context
->skb
= NULL
;
544 skb_put(skb
, urb
->actual_length
);
546 /* note: queue implements a lock */
547 skb_queue_tail(&pipe
->io_comp_queue
, skb
);
548 queue_work(pipe
->ar_usb
->wq
, &pipe
->io_complete_work
);
551 ath6kl_usb_cleanup_recv_urb(urb_context
);
554 pipe
->urb_cnt
>= pipe
->urb_cnt_thresh
) {
555 /* our free urbs are piling up, post more transfers */
556 ath6kl_usb_post_recv_transfers(pipe
, ATH6KL_USB_RX_BUFFER_SIZE
);
560 static void ath6kl_usb_usb_transmit_complete(struct urb
*urb
)
562 struct ath6kl_urb_context
*urb_context
= urb
->context
;
563 struct ath6kl_usb_pipe
*pipe
= urb_context
->pipe
;
566 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
567 "%s: pipe: %d, stat:%d, len:%d\n",
568 __func__
, pipe
->logical_pipe_num
, urb
->status
,
571 if (urb
->status
!= 0) {
572 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
573 "%s: pipe: %d, failed:%d\n",
574 __func__
, pipe
->logical_pipe_num
, urb
->status
);
577 skb
= urb_context
->skb
;
578 urb_context
->skb
= NULL
;
579 ath6kl_usb_free_urb_to_pipe(urb_context
->pipe
, urb_context
);
581 /* note: queue implements a lock */
582 skb_queue_tail(&pipe
->io_comp_queue
, skb
);
583 queue_work(pipe
->ar_usb
->wq
, &pipe
->io_complete_work
);
586 static void ath6kl_usb_io_comp_work(struct work_struct
*work
)
588 struct ath6kl_usb_pipe
*pipe
= container_of(work
,
589 struct ath6kl_usb_pipe
,
591 struct ath6kl_usb
*ar_usb
;
594 ar_usb
= pipe
->ar_usb
;
596 while ((skb
= skb_dequeue(&pipe
->io_comp_queue
))) {
597 if (pipe
->flags
& ATH6KL_USB_PIPE_FLAG_TX
) {
598 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
599 "ath6kl usb xmit callback buf:0x%p\n", skb
);
600 ath6kl_core_tx_complete(ar_usb
->ar
, skb
);
602 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
603 "ath6kl usb recv callback buf:0x%p\n", skb
);
604 ath6kl_core_rx_complete(ar_usb
->ar
, skb
,
605 pipe
->logical_pipe_num
);
610 #define ATH6KL_USB_MAX_DIAG_CMD (sizeof(struct ath6kl_usb_ctrl_diag_cmd_write))
611 #define ATH6KL_USB_MAX_DIAG_RESP (sizeof(struct ath6kl_usb_ctrl_diag_resp_read))
613 static void ath6kl_usb_destroy(struct ath6kl_usb
*ar_usb
)
615 ath6kl_usb_flush_all(ar_usb
);
617 ath6kl_usb_cleanup_pipe_resources(ar_usb
);
619 usb_set_intfdata(ar_usb
->interface
, NULL
);
621 kfree(ar_usb
->diag_cmd_buffer
);
622 kfree(ar_usb
->diag_resp_buffer
);
623 destroy_workqueue(ar_usb
->wq
);
628 static struct ath6kl_usb
*ath6kl_usb_create(struct usb_interface
*interface
)
630 struct usb_device
*dev
= interface_to_usbdev(interface
);
631 struct ath6kl_usb
*ar_usb
;
632 struct ath6kl_usb_pipe
*pipe
;
636 /* ath6kl_usb_destroy() needs ar_usb != NULL && ar_usb->wq != NULL. */
637 ar_usb
= kzalloc(sizeof(struct ath6kl_usb
), GFP_KERNEL
);
640 ar_usb
->wq
= alloc_workqueue("ath6kl_wq", 0, 0);
646 usb_set_intfdata(interface
, ar_usb
);
647 spin_lock_init(&(ar_usb
->cs_lock
));
649 ar_usb
->interface
= interface
;
651 for (i
= 0; i
< ATH6KL_USB_PIPE_MAX
; i
++) {
652 pipe
= &ar_usb
->pipes
[i
];
653 INIT_WORK(&pipe
->io_complete_work
,
654 ath6kl_usb_io_comp_work
);
655 skb_queue_head_init(&pipe
->io_comp_queue
);
658 ar_usb
->diag_cmd_buffer
= kzalloc(ATH6KL_USB_MAX_DIAG_CMD
, GFP_KERNEL
);
659 if (ar_usb
->diag_cmd_buffer
== NULL
) {
661 goto fail_ath6kl_usb_create
;
664 ar_usb
->diag_resp_buffer
= kzalloc(ATH6KL_USB_MAX_DIAG_RESP
,
666 if (ar_usb
->diag_resp_buffer
== NULL
) {
668 goto fail_ath6kl_usb_create
;
671 status
= ath6kl_usb_setup_pipe_resources(ar_usb
);
673 fail_ath6kl_usb_create
:
675 ath6kl_usb_destroy(ar_usb
);
681 static void ath6kl_usb_device_detached(struct usb_interface
*interface
)
683 struct ath6kl_usb
*ar_usb
;
685 ar_usb
= usb_get_intfdata(interface
);
689 ath6kl_stop_txrx(ar_usb
->ar
);
691 /* Delay to wait for the target to reboot */
693 ath6kl_core_cleanup(ar_usb
->ar
);
694 ath6kl_usb_destroy(ar_usb
);
697 /* exported hif usb APIs for htc pipe */
698 static void hif_start(struct ath6kl
*ar
)
700 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
703 ath6kl_usb_start_recv_pipes(device
);
705 /* set the TX resource avail threshold for each TX pipe */
706 for (i
= ATH6KL_USB_PIPE_TX_CTRL
;
707 i
<= ATH6KL_USB_PIPE_TX_DATA_HP
; i
++) {
708 device
->pipes
[i
].urb_cnt_thresh
=
709 device
->pipes
[i
].urb_alloc
/ 2;
713 static int ath6kl_usb_send(struct ath6kl
*ar
, u8 PipeID
,
714 struct sk_buff
*hdr_skb
, struct sk_buff
*skb
)
716 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
717 struct ath6kl_usb_pipe
*pipe
= &device
->pipes
[PipeID
];
718 struct ath6kl_urb_context
*urb_context
;
719 int usb_status
, status
= 0;
724 ath6kl_dbg(ATH6KL_DBG_USB_BULK
, "+%s pipe : %d, buf:0x%p\n",
725 __func__
, PipeID
, skb
);
727 urb_context
= ath6kl_usb_alloc_urb_from_pipe(pipe
);
729 if (urb_context
== NULL
) {
731 * TODO: it is possible to run out of urbs if
732 * 2 endpoints map to the same pipe ID
734 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
735 "%s pipe:%d no urbs left. URB Cnt : %d\n",
736 __func__
, PipeID
, pipe
->urb_cnt
);
741 urb_context
->skb
= skb
;
746 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
749 ath6kl_usb_free_urb_to_pipe(urb_context
->pipe
,
754 usb_fill_bulk_urb(urb
,
756 pipe
->usb_pipe_handle
,
759 ath6kl_usb_usb_transmit_complete
, urb_context
);
761 if ((len
% pipe
->max_packet_size
) == 0) {
762 /* hit a max packet boundary on this pipe */
763 urb
->transfer_flags
|= URB_ZERO_PACKET
;
766 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
767 "athusb bulk send submit:%d, 0x%X (ep:0x%2.2X), %d bytes\n",
768 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
769 pipe
->ep_address
, len
);
771 usb_anchor_urb(urb
, &pipe
->urb_submitted
);
772 usb_status
= usb_submit_urb(urb
, GFP_ATOMIC
);
775 ath6kl_dbg(ATH6KL_DBG_USB_BULK
,
776 "ath6kl usb : usb bulk transmit failed %d\n",
778 usb_unanchor_urb(urb
);
779 ath6kl_usb_free_urb_to_pipe(urb_context
->pipe
,
789 static void hif_stop(struct ath6kl
*ar
)
791 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
793 ath6kl_usb_flush_all(device
);
796 static void ath6kl_usb_get_default_pipe(struct ath6kl
*ar
,
797 u8
*ul_pipe
, u8
*dl_pipe
)
799 *ul_pipe
= ATH6KL_USB_PIPE_TX_CTRL
;
800 *dl_pipe
= ATH6KL_USB_PIPE_RX_CTRL
;
803 static int ath6kl_usb_map_service_pipe(struct ath6kl
*ar
, u16 svc_id
,
804 u8
*ul_pipe
, u8
*dl_pipe
)
809 case HTC_CTRL_RSVD_SVC
:
810 case WMI_CONTROL_SVC
:
811 *ul_pipe
= ATH6KL_USB_PIPE_TX_CTRL
;
812 /* due to large control packets, shift to data pipe */
813 *dl_pipe
= ATH6KL_USB_PIPE_RX_DATA
;
815 case WMI_DATA_BE_SVC
:
816 case WMI_DATA_BK_SVC
:
817 *ul_pipe
= ATH6KL_USB_PIPE_TX_DATA_LP
;
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_VI_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
;
837 case WMI_DATA_VO_SVC
:
839 if (test_bit(ATH6KL_FW_CAPABILITY_MAP_LP_ENDPOINT
,
840 ar
->fw_capabilities
))
841 *ul_pipe
= ATH6KL_USB_PIPE_TX_DATA_LP
;
843 *ul_pipe
= ATH6KL_USB_PIPE_TX_DATA_MP
;
845 * Disable rxdata2 directly, it will be enabled
846 * if FW enable rxdata2
848 *dl_pipe
= ATH6KL_USB_PIPE_RX_DATA
;
858 static u16
ath6kl_usb_get_free_queue_number(struct ath6kl
*ar
, u8 pipe_id
)
860 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
862 return device
->pipes
[pipe_id
].urb_cnt
;
865 static void hif_detach_htc(struct ath6kl
*ar
)
867 struct ath6kl_usb
*device
= ath6kl_usb_priv(ar
);
869 ath6kl_usb_flush_all(device
);
872 static int ath6kl_usb_submit_ctrl_out(struct ath6kl_usb
*ar_usb
,
873 u8 req
, u16 value
, u16 index
, void *data
,
880 buf
= kmemdup(data
, size
, GFP_KERNEL
);
885 /* note: if successful returns number of bytes transfered */
886 ret
= usb_control_msg(ar_usb
->udev
,
887 usb_sndctrlpipe(ar_usb
->udev
, 0),
889 USB_DIR_OUT
| USB_TYPE_VENDOR
|
890 USB_RECIP_DEVICE
, value
, index
, buf
,
894 ath6kl_warn("Failed to submit usb control message: %d\n", ret
);
904 static int ath6kl_usb_submit_ctrl_in(struct ath6kl_usb
*ar_usb
,
905 u8 req
, u16 value
, u16 index
, void *data
,
912 buf
= kmalloc(size
, GFP_KERNEL
);
917 /* note: if successful returns number of bytes transfered */
918 ret
= usb_control_msg(ar_usb
->udev
,
919 usb_rcvctrlpipe(ar_usb
->udev
, 0),
921 USB_DIR_IN
| USB_TYPE_VENDOR
|
922 USB_RECIP_DEVICE
, value
, index
, buf
,
926 ath6kl_warn("Failed to read usb control message: %d\n", ret
);
931 memcpy((u8
*) data
, buf
, size
);
938 static int ath6kl_usb_ctrl_msg_exchange(struct ath6kl_usb
*ar_usb
,
939 u8 req_val
, u8
*req_buf
, u32 req_len
,
940 u8 resp_val
, u8
*resp_buf
, u32
*resp_len
)
945 ret
= ath6kl_usb_submit_ctrl_out(ar_usb
, req_val
, 0, 0,
951 if (resp_buf
== NULL
) {
952 /* no expected response */
957 ret
= ath6kl_usb_submit_ctrl_in(ar_usb
, resp_val
, 0, 0,
958 resp_buf
, *resp_len
);
963 static int ath6kl_usb_diag_read32(struct ath6kl
*ar
, u32 address
, u32
*data
)
965 struct ath6kl_usb
*ar_usb
= ar
->hif_priv
;
966 struct ath6kl_usb_ctrl_diag_resp_read
*resp
;
967 struct ath6kl_usb_ctrl_diag_cmd_read
*cmd
;
971 cmd
= (struct ath6kl_usb_ctrl_diag_cmd_read
*) ar_usb
->diag_cmd_buffer
;
973 memset(cmd
, 0, sizeof(*cmd
));
974 cmd
->cmd
= ATH6KL_USB_CTRL_DIAG_CC_READ
;
975 cmd
->address
= cpu_to_le32(address
);
976 resp_len
= sizeof(*resp
);
978 ret
= ath6kl_usb_ctrl_msg_exchange(ar_usb
,
979 ATH6KL_USB_CONTROL_REQ_DIAG_CMD
,
981 sizeof(struct ath6kl_usb_ctrl_diag_cmd_write
),
982 ATH6KL_USB_CONTROL_REQ_DIAG_RESP
,
983 ar_usb
->diag_resp_buffer
, &resp_len
);
986 ath6kl_warn("diag read32 failed: %d\n", ret
);
990 resp
= (struct ath6kl_usb_ctrl_diag_resp_read
*)
991 ar_usb
->diag_resp_buffer
;
993 *data
= le32_to_cpu(resp
->value
);
998 static int ath6kl_usb_diag_write32(struct ath6kl
*ar
, u32 address
, __le32 data
)
1000 struct ath6kl_usb
*ar_usb
= ar
->hif_priv
;
1001 struct ath6kl_usb_ctrl_diag_cmd_write
*cmd
;
1004 cmd
= (struct ath6kl_usb_ctrl_diag_cmd_write
*) ar_usb
->diag_cmd_buffer
;
1006 memset(cmd
, 0, sizeof(struct ath6kl_usb_ctrl_diag_cmd_write
));
1007 cmd
->cmd
= cpu_to_le32(ATH6KL_USB_CTRL_DIAG_CC_WRITE
);
1008 cmd
->address
= cpu_to_le32(address
);
1011 ret
= ath6kl_usb_ctrl_msg_exchange(ar_usb
,
1012 ATH6KL_USB_CONTROL_REQ_DIAG_CMD
,
1017 ath6kl_warn("diag_write32 failed: %d\n", ret
);
1024 static int ath6kl_usb_bmi_read(struct ath6kl
*ar
, u8
*buf
, u32 len
)
1026 struct ath6kl_usb
*ar_usb
= ar
->hif_priv
;
1030 ret
= ath6kl_usb_submit_ctrl_in(ar_usb
,
1031 ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP
,
1034 ath6kl_err("Unable to read the bmi data from the device: %d\n",
1042 static int ath6kl_usb_bmi_write(struct ath6kl
*ar
, u8
*buf
, u32 len
)
1044 struct ath6kl_usb
*ar_usb
= ar
->hif_priv
;
1048 ret
= ath6kl_usb_submit_ctrl_out(ar_usb
,
1049 ATH6KL_USB_CONTROL_REQ_SEND_BMI_CMD
,
1052 ath6kl_err("unable to send the bmi data to the device: %d\n",
1060 static int ath6kl_usb_power_on(struct ath6kl
*ar
)
1066 static int ath6kl_usb_power_off(struct ath6kl
*ar
)
1072 static void ath6kl_usb_stop(struct ath6kl
*ar
)
1077 static void ath6kl_usb_cleanup_scatter(struct ath6kl
*ar
)
1080 * USB doesn't support it. Just return.
1085 static int ath6kl_usb_suspend(struct ath6kl
*ar
, struct cfg80211_wowlan
*wow
)
1088 * cfg80211 suspend/WOW currently not supported for USB.
1093 static int ath6kl_usb_resume(struct ath6kl
*ar
)
1096 * cfg80211 resume currently not supported for USB.
1101 static const struct ath6kl_hif_ops ath6kl_usb_ops
= {
1102 .diag_read32
= ath6kl_usb_diag_read32
,
1103 .diag_write32
= ath6kl_usb_diag_write32
,
1104 .bmi_read
= ath6kl_usb_bmi_read
,
1105 .bmi_write
= ath6kl_usb_bmi_write
,
1106 .power_on
= ath6kl_usb_power_on
,
1107 .power_off
= ath6kl_usb_power_off
,
1108 .stop
= ath6kl_usb_stop
,
1109 .pipe_send
= ath6kl_usb_send
,
1110 .pipe_get_default
= ath6kl_usb_get_default_pipe
,
1111 .pipe_map_service
= ath6kl_usb_map_service_pipe
,
1112 .pipe_get_free_queue_number
= ath6kl_usb_get_free_queue_number
,
1113 .cleanup_scatter
= ath6kl_usb_cleanup_scatter
,
1114 .suspend
= ath6kl_usb_suspend
,
1115 .resume
= ath6kl_usb_resume
,
1118 /* ath6kl usb driver registered functions */
1119 static int ath6kl_usb_probe(struct usb_interface
*interface
,
1120 const struct usb_device_id
*id
)
1122 struct usb_device
*dev
= interface_to_usbdev(interface
);
1124 struct ath6kl_usb
*ar_usb
= NULL
;
1125 int vendor_id
, product_id
;
1130 vendor_id
= le16_to_cpu(dev
->descriptor
.idVendor
);
1131 product_id
= le16_to_cpu(dev
->descriptor
.idProduct
);
1133 ath6kl_dbg(ATH6KL_DBG_USB
, "vendor_id = %04x\n", vendor_id
);
1134 ath6kl_dbg(ATH6KL_DBG_USB
, "product_id = %04x\n", product_id
);
1136 if (interface
->cur_altsetting
)
1137 ath6kl_dbg(ATH6KL_DBG_USB
, "USB Interface %d\n",
1138 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
1141 if (dev
->speed
== USB_SPEED_HIGH
)
1142 ath6kl_dbg(ATH6KL_DBG_USB
, "USB 2.0 Host\n");
1144 ath6kl_dbg(ATH6KL_DBG_USB
, "USB 1.1 Host\n");
1146 ar_usb
= ath6kl_usb_create(interface
);
1148 if (ar_usb
== NULL
) {
1153 ar
= ath6kl_core_create(&ar_usb
->udev
->dev
);
1155 ath6kl_err("Failed to alloc ath6kl core\n");
1157 goto err_usb_destroy
;
1160 ar
->hif_priv
= ar_usb
;
1161 ar
->hif_type
= ATH6KL_HIF_TYPE_USB
;
1162 ar
->hif_ops
= &ath6kl_usb_ops
;
1163 ar
->mbox_info
.block_size
= 16;
1164 ar
->bmi
.max_data_size
= 252;
1168 ret
= ath6kl_core_init(ar
, ATH6KL_HTC_TYPE_PIPE
);
1170 ath6kl_err("Failed to init ath6kl core: %d\n", ret
);
1177 ath6kl_core_destroy(ar
);
1179 ath6kl_usb_destroy(ar_usb
);
1186 static void ath6kl_usb_remove(struct usb_interface
*interface
)
1188 usb_put_dev(interface_to_usbdev(interface
));
1189 ath6kl_usb_device_detached(interface
);
1194 static int ath6kl_usb_pm_suspend(struct usb_interface
*interface
,
1195 pm_message_t message
)
1197 struct ath6kl_usb
*device
;
1198 device
= usb_get_intfdata(interface
);
1200 ath6kl_usb_flush_all(device
);
1204 static int ath6kl_usb_pm_resume(struct usb_interface
*interface
)
1206 struct ath6kl_usb
*device
;
1207 device
= usb_get_intfdata(interface
);
1209 ath6kl_usb_post_recv_transfers(&device
->pipes
[ATH6KL_USB_PIPE_RX_DATA
],
1210 ATH6KL_USB_RX_BUFFER_SIZE
);
1211 ath6kl_usb_post_recv_transfers(&device
->pipes
[ATH6KL_USB_PIPE_RX_DATA2
],
1212 ATH6KL_USB_RX_BUFFER_SIZE
);
1219 #define ath6kl_usb_pm_suspend NULL
1220 #define ath6kl_usb_pm_resume NULL
1224 /* table of devices that work with this driver */
1225 static const struct usb_device_id ath6kl_usb_ids
[] = {
1226 {USB_DEVICE(0x0cf3, 0x9375)},
1227 {USB_DEVICE(0x0cf3, 0x9374)},
1228 {USB_DEVICE(0x04da, 0x390d)},
1229 { /* Terminating entry */ },
1232 MODULE_DEVICE_TABLE(usb
, ath6kl_usb_ids
);
1234 static struct usb_driver ath6kl_usb_driver
= {
1235 .name
= "ath6kl_usb",
1236 .probe
= ath6kl_usb_probe
,
1237 .suspend
= ath6kl_usb_pm_suspend
,
1238 .resume
= ath6kl_usb_pm_resume
,
1239 .disconnect
= ath6kl_usb_remove
,
1240 .id_table
= ath6kl_usb_ids
,
1241 .supports_autosuspend
= true,
1242 .disable_hub_initiated_lpm
= 1,
1245 module_usb_driver(ath6kl_usb_driver
);
1247 MODULE_AUTHOR("Atheros Communications, Inc.");
1248 MODULE_DESCRIPTION("Driver support for Atheros AR600x USB devices");
1249 MODULE_LICENSE("Dual BSD/GPL");
1250 MODULE_FIRMWARE(AR6004_HW_1_0_FIRMWARE_FILE
);
1251 MODULE_FIRMWARE(AR6004_HW_1_0_BOARD_DATA_FILE
);
1252 MODULE_FIRMWARE(AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE
);
1253 MODULE_FIRMWARE(AR6004_HW_1_1_FIRMWARE_FILE
);
1254 MODULE_FIRMWARE(AR6004_HW_1_1_BOARD_DATA_FILE
);
1255 MODULE_FIRMWARE(AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE
);
1256 MODULE_FIRMWARE(AR6004_HW_1_2_FIRMWARE_FILE
);
1257 MODULE_FIRMWARE(AR6004_HW_1_2_BOARD_DATA_FILE
);
1258 MODULE_FIRMWARE(AR6004_HW_1_2_DEFAULT_BOARD_DATA_FILE
);
1259 MODULE_FIRMWARE(AR6004_HW_1_3_FW_DIR
"/" AR6004_HW_1_3_FIRMWARE_FILE
);
1260 MODULE_FIRMWARE(AR6004_HW_1_3_BOARD_DATA_FILE
);
1261 MODULE_FIRMWARE(AR6004_HW_1_3_DEFAULT_BOARD_DATA_FILE
);