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) {
190 goto fail_alloc_pipe_resources;
193 urb_context->pipe = pipe;
196 * we are only allocate the urb contexts here, the actual URB
197 * is allocated from the kernel as needed to do a transaction
200 ath6kl_usb_free_urb_to_pipe(pipe, urb_context);
203 ath6kl_dbg(ATH6KL_DBG_USB,
204 "ath6kl usb: alloc resources lpipe:%d hpipe:0x%X urbs:%d\n",
205 pipe->logical_pipe_num, pipe->usb_pipe_handle,
208 fail_alloc_pipe_resources:
212 static void ath6kl_usb_free_pipe_resources(struct ath6kl_usb_pipe *pipe)
214 struct ath6kl_urb_context *urb_context;
216 if (pipe->ar_usb == NULL) {
217 /* nothing allocated for this pipe */
221 ath6kl_dbg(ATH6KL_DBG_USB,
222 "ath6kl usb: free resources lpipe:%d"
223 "hpipe:0x%X urbs:%d avail:%d\n",
224 pipe->logical_pipe_num, pipe->usb_pipe_handle,
225 pipe->urb_alloc, pipe->urb_cnt);
227 if (pipe->urb_alloc != pipe->urb_cnt) {
228 ath6kl_dbg(ATH6KL_DBG_USB,
229 "ath6kl usb: urb leak! 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);
236 urb_context = ath6kl_usb_alloc_urb_from_pipe(pipe);
237 if (urb_context == NULL)
244 static void ath6kl_usb_cleanup_pipe_resources(struct ath6kl_usb *ar_usb)
248 for (i = 0; i < ATH6KL_USB_PIPE_MAX; i++)
249 ath6kl_usb_free_pipe_resources(&ar_usb->pipes[i]);
253 static u8 ath6kl_usb_get_logical_pipe_num(struct ath6kl_usb *ar_usb,
254 u8 ep_address, int *urb_count)
256 u8 pipe_num = ATH6KL_USB_PIPE_INVALID;
258 switch (ep_address) {
259 case ATH6KL_USB_EP_ADDR_APP_CTRL_IN:
260 pipe_num = ATH6KL_USB_PIPE_RX_CTRL;
261 *urb_count = RX_URB_COUNT;
263 case ATH6KL_USB_EP_ADDR_APP_DATA_IN:
264 pipe_num = ATH6KL_USB_PIPE_RX_DATA;
265 *urb_count = RX_URB_COUNT;
267 case ATH6KL_USB_EP_ADDR_APP_INT_IN:
268 pipe_num = ATH6KL_USB_PIPE_RX_INT;
269 *urb_count = RX_URB_COUNT;
271 case ATH6KL_USB_EP_ADDR_APP_DATA2_IN:
272 pipe_num = ATH6KL_USB_PIPE_RX_DATA2;
273 *urb_count = RX_URB_COUNT;
275 case ATH6KL_USB_EP_ADDR_APP_CTRL_OUT:
276 pipe_num = ATH6KL_USB_PIPE_TX_CTRL;
277 *urb_count = TX_URB_COUNT;
279 case ATH6KL_USB_EP_ADDR_APP_DATA_LP_OUT:
280 pipe_num = ATH6KL_USB_PIPE_TX_DATA_LP;
281 *urb_count = TX_URB_COUNT;
283 case ATH6KL_USB_EP_ADDR_APP_DATA_MP_OUT:
284 pipe_num = ATH6KL_USB_PIPE_TX_DATA_MP;
285 *urb_count = TX_URB_COUNT;
287 case ATH6KL_USB_EP_ADDR_APP_DATA_HP_OUT:
288 pipe_num = ATH6KL_USB_PIPE_TX_DATA_HP;
289 *urb_count = TX_URB_COUNT;
292 /* note: there may be endpoints not currently used */
299 static int ath6kl_usb_setup_pipe_resources(struct ath6kl_usb *ar_usb)
301 struct usb_interface *interface = ar_usb->interface;
302 struct usb_host_interface *iface_desc = interface->cur_altsetting;
303 struct usb_endpoint_descriptor *endpoint;
304 struct ath6kl_usb_pipe *pipe;
305 int i, urbcount, status = 0;
308 ath6kl_dbg(ATH6KL_DBG_USB, "setting up USB Pipes using interface\n");
310 /* walk decriptors and setup pipes */
311 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
312 endpoint = &iface_desc->endpoint[i].desc;
314 if (ATH6KL_USB_IS_BULK_EP(endpoint->bmAttributes)) {
315 ath6kl_dbg(ATH6KL_DBG_USB,
316 "%s Bulk Ep:0x%2.2X maxpktsz:%d\n",
318 (endpoint->bEndpointAddress) ?
319 "RX" : "TX", endpoint->bEndpointAddress,
320 le16_to_cpu(endpoint->wMaxPacketSize));
321 } else if (ATH6KL_USB_IS_INT_EP(endpoint->bmAttributes)) {
322 ath6kl_dbg(ATH6KL_DBG_USB,
323 "%s Int Ep:0x%2.2X maxpktsz:%d interval:%d\n",
325 (endpoint->bEndpointAddress) ?
326 "RX" : "TX", endpoint->bEndpointAddress,
327 le16_to_cpu(endpoint->wMaxPacketSize),
328 endpoint->bInterval);
329 } else if (ATH6KL_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
331 ath6kl_dbg(ATH6KL_DBG_USB,
332 "%s ISOC Ep:0x%2.2X maxpktsz:%d interval:%d\n",
334 (endpoint->bEndpointAddress) ?
335 "RX" : "TX", endpoint->bEndpointAddress,
336 le16_to_cpu(endpoint->wMaxPacketSize),
337 endpoint->bInterval);
342 ath6kl_usb_get_logical_pipe_num(ar_usb,
343 endpoint->bEndpointAddress,
345 if (pipe_num == ATH6KL_USB_PIPE_INVALID)
348 pipe = &ar_usb->pipes[pipe_num];
349 if (pipe->ar_usb != NULL) {
350 /* hmmm..pipe was already setup */
354 pipe->ar_usb = ar_usb;
355 pipe->logical_pipe_num = pipe_num;
356 pipe->ep_address = endpoint->bEndpointAddress;
357 pipe->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize);
359 if (ATH6KL_USB_IS_BULK_EP(endpoint->bmAttributes)) {
360 if (ATH6KL_USB_IS_DIR_IN(pipe->ep_address)) {
361 pipe->usb_pipe_handle =
362 usb_rcvbulkpipe(ar_usb->udev,
365 pipe->usb_pipe_handle =
366 usb_sndbulkpipe(ar_usb->udev,
369 } else if (ATH6KL_USB_IS_INT_EP(endpoint->bmAttributes)) {
370 if (ATH6KL_USB_IS_DIR_IN(pipe->ep_address)) {
371 pipe->usb_pipe_handle =
372 usb_rcvintpipe(ar_usb->udev,
375 pipe->usb_pipe_handle =
376 usb_sndintpipe(ar_usb->udev,
379 } else if (ATH6KL_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
381 if (ATH6KL_USB_IS_DIR_IN(pipe->ep_address)) {
382 pipe->usb_pipe_handle =
383 usb_rcvisocpipe(ar_usb->udev,
386 pipe->usb_pipe_handle =
387 usb_sndisocpipe(ar_usb->udev,
392 pipe->ep_desc = endpoint;
394 if (!ATH6KL_USB_IS_DIR_IN(pipe->ep_address))
395 pipe->flags |= ATH6KL_USB_PIPE_FLAG_TX;
397 status = ath6kl_usb_alloc_pipe_resources(pipe, urbcount);
405 /* pipe operations */
406 static void ath6kl_usb_post_recv_transfers(struct ath6kl_usb_pipe *recv_pipe,
409 struct ath6kl_urb_context *urb_context;
414 urb_context = ath6kl_usb_alloc_urb_from_pipe(recv_pipe);
415 if (urb_context == NULL)
418 urb_context->skb = dev_alloc_skb(buffer_length);
419 if (urb_context->skb == NULL)
420 goto err_cleanup_urb;
422 urb = usb_alloc_urb(0, GFP_ATOMIC);
424 goto err_cleanup_urb;
426 usb_fill_bulk_urb(urb,
427 recv_pipe->ar_usb->udev,
428 recv_pipe->usb_pipe_handle,
429 urb_context->skb->data,
431 ath6kl_usb_recv_complete, urb_context);
433 ath6kl_dbg(ATH6KL_DBG_USB_BULK,
434 "ath6kl usb: bulk recv submit:%d, 0x%X (ep:0x%2.2X), %d bytes buf:0x%p\n",
435 recv_pipe->logical_pipe_num,
436 recv_pipe->usb_pipe_handle, recv_pipe->ep_address,
437 buffer_length, urb_context->skb);
439 usb_anchor_urb(urb, &recv_pipe->urb_submitted);
440 usb_status = usb_submit_urb(urb, GFP_ATOMIC);
443 ath6kl_dbg(ATH6KL_DBG_USB_BULK,
444 "ath6kl usb : usb bulk recv failed %d\n",
446 usb_unanchor_urb(urb);
448 goto err_cleanup_urb;
455 ath6kl_usb_cleanup_recv_urb(urb_context);
459 static void ath6kl_usb_flush_all(struct ath6kl_usb *ar_usb)
463 for (i = 0; i < ATH6KL_USB_PIPE_MAX; i++) {
464 if (ar_usb->pipes[i].ar_usb != NULL)
465 usb_kill_anchored_urbs(&ar_usb->pipes[i].urb_submitted);
469 * Flushing any pending I/O may schedule work this call will block
470 * until all scheduled work runs to completion.
472 flush_scheduled_work();
475 static void ath6kl_usb_start_recv_pipes(struct ath6kl_usb *ar_usb)
478 * note: control pipe is no longer used
479 * ar_usb->pipes[ATH6KL_USB_PIPE_RX_CTRL].urb_cnt_thresh =
480 * ar_usb->pipes[ATH6KL_USB_PIPE_RX_CTRL].urb_alloc/2;
481 * ath6kl_usb_post_recv_transfers(&ar_usb->
482 * pipes[ATH6KL_USB_PIPE_RX_CTRL],
483 * ATH6KL_USB_RX_BUFFER_SIZE);
486 ar_usb->pipes[ATH6KL_USB_PIPE_RX_DATA].urb_cnt_thresh =
487 ar_usb->pipes[ATH6KL_USB_PIPE_RX_DATA].urb_alloc / 2;
488 ath6kl_usb_post_recv_transfers(&ar_usb->pipes[ATH6KL_USB_PIPE_RX_DATA],
489 ATH6KL_USB_RX_BUFFER_SIZE);
492 /* hif usb rx/tx completion functions */
493 static void ath6kl_usb_recv_complete(struct urb *urb)
495 struct ath6kl_urb_context *urb_context = urb->context;
496 struct ath6kl_usb_pipe *pipe = urb_context->pipe;
497 struct sk_buff *skb = NULL;
500 ath6kl_dbg(ATH6KL_DBG_USB_BULK,
501 "%s: recv pipe: %d, stat:%d, len:%d urb:0x%p\n", __func__,
502 pipe->logical_pipe_num, urb->status, urb->actual_length,
505 if (urb->status != 0) {
507 switch (urb->status) {
512 * no need to spew these errors when device
513 * removed or urb killed due to driver shutdown
518 ath6kl_dbg(ATH6KL_DBG_USB_BULK,
519 "%s recv pipe: %d (ep:0x%2.2X), failed:%d\n",
520 __func__, pipe->logical_pipe_num,
521 pipe->ep_address, urb->status);
524 goto cleanup_recv_urb;
527 if (urb->actual_length == 0)
528 goto cleanup_recv_urb;
530 skb = urb_context->skb;
532 /* we are going to pass it up */
533 urb_context->skb = NULL;
534 skb_put(skb, urb->actual_length);
536 /* note: queue implements a lock */
537 skb_queue_tail(&pipe->io_comp_queue, skb);
538 schedule_work(&pipe->io_complete_work);
541 ath6kl_usb_cleanup_recv_urb(urb_context);
544 pipe->urb_cnt >= pipe->urb_cnt_thresh) {
545 /* our free urbs are piling up, post more transfers */
546 ath6kl_usb_post_recv_transfers(pipe, ATH6KL_USB_RX_BUFFER_SIZE);
550 static void ath6kl_usb_usb_transmit_complete(struct urb *urb)
552 struct ath6kl_urb_context *urb_context = urb->context;
553 struct ath6kl_usb_pipe *pipe = urb_context->pipe;
556 ath6kl_dbg(ATH6KL_DBG_USB_BULK,
557 "%s: pipe: %d, stat:%d, len:%d\n",
558 __func__, pipe->logical_pipe_num, urb->status,
561 if (urb->status != 0) {
562 ath6kl_dbg(ATH6KL_DBG_USB_BULK,
563 "%s: pipe: %d, failed:%d\n",
564 __func__, pipe->logical_pipe_num, urb->status);
567 skb = urb_context->skb;
568 urb_context->skb = NULL;
569 ath6kl_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
571 /* note: queue implements a lock */
572 skb_queue_tail(&pipe->io_comp_queue, skb);
573 schedule_work(&pipe->io_complete_work);
576 static void ath6kl_usb_io_comp_work(struct work_struct *work)
578 struct ath6kl_usb_pipe *pipe = container_of(work,
579 struct ath6kl_usb_pipe,
581 struct ath6kl_usb *ar_usb;
584 ar_usb = pipe->ar_usb;
586 while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
587 if (pipe->flags & ATH6KL_USB_PIPE_FLAG_TX) {
588 ath6kl_dbg(ATH6KL_DBG_USB_BULK,
589 "ath6kl usb xmit callback buf:0x%p\n", skb);
590 ath6kl_core_tx_complete(ar_usb->ar, skb);
592 ath6kl_dbg(ATH6KL_DBG_USB_BULK,
593 "ath6kl usb recv callback buf:0x%p\n", skb);
594 ath6kl_core_rx_complete(ar_usb->ar, skb,
595 pipe->logical_pipe_num);
600 #define ATH6KL_USB_MAX_DIAG_CMD (sizeof(struct ath6kl_usb_ctrl_diag_cmd_write))
601 #define ATH6KL_USB_MAX_DIAG_RESP (sizeof(struct ath6kl_usb_ctrl_diag_resp_read))
603 static void ath6kl_usb_destroy(struct ath6kl_usb *ar_usb)
605 ath6kl_usb_flush_all(ar_usb);
607 ath6kl_usb_cleanup_pipe_resources(ar_usb);
609 usb_set_intfdata(ar_usb->interface, NULL);
611 kfree(ar_usb->diag_cmd_buffer);
612 kfree(ar_usb->diag_resp_buffer);
617 static struct ath6kl_usb *ath6kl_usb_create(struct usb_interface *interface)
619 struct usb_device *dev = interface_to_usbdev(interface);
620 struct ath6kl_usb *ar_usb;
621 struct ath6kl_usb_pipe *pipe;
625 ar_usb = kzalloc(sizeof(struct ath6kl_usb), GFP_KERNEL);
627 goto fail_ath6kl_usb_create;
629 usb_set_intfdata(interface, ar_usb);
630 spin_lock_init(&(ar_usb->cs_lock));
632 ar_usb->interface = interface;
634 for (i = 0; i < ATH6KL_USB_PIPE_MAX; i++) {
635 pipe = &ar_usb->pipes[i];
636 INIT_WORK(&pipe->io_complete_work,
637 ath6kl_usb_io_comp_work);
638 skb_queue_head_init(&pipe->io_comp_queue);
641 ar_usb->diag_cmd_buffer = kzalloc(ATH6KL_USB_MAX_DIAG_CMD, GFP_KERNEL);
642 if (ar_usb->diag_cmd_buffer == NULL) {
644 goto fail_ath6kl_usb_create;
647 ar_usb->diag_resp_buffer = kzalloc(ATH6KL_USB_MAX_DIAG_RESP,
649 if (ar_usb->diag_resp_buffer == NULL) {
651 goto fail_ath6kl_usb_create;
654 status = ath6kl_usb_setup_pipe_resources(ar_usb);
656 fail_ath6kl_usb_create:
658 ath6kl_usb_destroy(ar_usb);
664 static void ath6kl_usb_device_detached(struct usb_interface *interface)
666 struct ath6kl_usb *ar_usb;
668 ar_usb = usb_get_intfdata(interface);
672 ath6kl_stop_txrx(ar_usb->ar);
674 /* Delay to wait for the target to reboot */
676 ath6kl_core_cleanup(ar_usb->ar);
677 ath6kl_usb_destroy(ar_usb);
680 /* exported hif usb APIs for htc pipe */
681 static void hif_start(struct ath6kl *ar)
683 struct ath6kl_usb *device = ath6kl_usb_priv(ar);
686 ath6kl_usb_start_recv_pipes(device);
688 /* set the TX resource avail threshold for each TX pipe */
689 for (i = ATH6KL_USB_PIPE_TX_CTRL;
690 i <= ATH6KL_USB_PIPE_TX_DATA_HP; i++) {
691 device->pipes[i].urb_cnt_thresh =
692 device->pipes[i].urb_alloc / 2;
696 static int ath6kl_usb_send(struct ath6kl *ar, u8 PipeID,
697 struct sk_buff *hdr_skb, struct sk_buff *skb)
699 struct ath6kl_usb *device = ath6kl_usb_priv(ar);
700 struct ath6kl_usb_pipe *pipe = &device->pipes[PipeID];
701 struct ath6kl_urb_context *urb_context;
702 int usb_status, status = 0;
707 ath6kl_dbg(ATH6KL_DBG_USB_BULK, "+%s pipe : %d, buf:0x%p\n",
708 __func__, PipeID, skb);
710 urb_context = ath6kl_usb_alloc_urb_from_pipe(pipe);
712 if (urb_context == NULL) {
714 * TODO: it is possible to run out of urbs if
715 * 2 endpoints map to the same pipe ID
717 ath6kl_dbg(ATH6KL_DBG_USB_BULK,
718 "%s pipe:%d no urbs left. URB Cnt : %d\n",
719 __func__, PipeID, pipe->urb_cnt);
724 urb_context->skb = skb;
729 urb = usb_alloc_urb(0, GFP_ATOMIC);
732 ath6kl_usb_free_urb_to_pipe(urb_context->pipe,
737 usb_fill_bulk_urb(urb,
739 pipe->usb_pipe_handle,
742 ath6kl_usb_usb_transmit_complete, urb_context);
744 if ((len % pipe->max_packet_size) == 0) {
745 /* hit a max packet boundary on this pipe */
746 urb->transfer_flags |= URB_ZERO_PACKET;
749 ath6kl_dbg(ATH6KL_DBG_USB_BULK,
750 "athusb bulk send submit:%d, 0x%X (ep:0x%2.2X), %d bytes\n",
751 pipe->logical_pipe_num, pipe->usb_pipe_handle,
752 pipe->ep_address, len);
754 usb_anchor_urb(urb, &pipe->urb_submitted);
755 usb_status = usb_submit_urb(urb, GFP_ATOMIC);
758 ath6kl_dbg(ATH6KL_DBG_USB_BULK,
759 "ath6kl usb : usb bulk transmit failed %d\n",
761 usb_unanchor_urb(urb);
762 ath6kl_usb_free_urb_to_pipe(urb_context->pipe,
772 static void hif_stop(struct ath6kl *ar)
774 struct ath6kl_usb *device = ath6kl_usb_priv(ar);
776 ath6kl_usb_flush_all(device);
779 static void ath6kl_usb_get_default_pipe(struct ath6kl *ar,
780 u8 *ul_pipe, u8 *dl_pipe)
782 *ul_pipe = ATH6KL_USB_PIPE_TX_CTRL;
783 *dl_pipe = ATH6KL_USB_PIPE_RX_CTRL;
786 static int ath6kl_usb_map_service_pipe(struct ath6kl *ar, u16 svc_id,
787 u8 *ul_pipe, u8 *dl_pipe)
792 case HTC_CTRL_RSVD_SVC:
793 case WMI_CONTROL_SVC:
794 *ul_pipe = ATH6KL_USB_PIPE_TX_CTRL;
795 /* due to large control packets, shift to data pipe */
796 *dl_pipe = ATH6KL_USB_PIPE_RX_DATA;
798 case WMI_DATA_BE_SVC:
799 case WMI_DATA_BK_SVC:
800 *ul_pipe = ATH6KL_USB_PIPE_TX_DATA_LP;
802 * Disable rxdata2 directly, it will be enabled
803 * if FW enable rxdata2
805 *dl_pipe = ATH6KL_USB_PIPE_RX_DATA;
807 case WMI_DATA_VI_SVC:
809 if (ar->hw.flags & ATH6KL_HW_MAP_LP_ENDPOINT)
810 *ul_pipe = ATH6KL_USB_PIPE_TX_DATA_LP;
812 *ul_pipe = ATH6KL_USB_PIPE_TX_DATA_MP;
814 * Disable rxdata2 directly, it will be enabled
815 * if FW enable rxdata2
817 *dl_pipe = ATH6KL_USB_PIPE_RX_DATA;
819 case WMI_DATA_VO_SVC:
821 if (ar->hw.flags & ATH6KL_HW_MAP_LP_ENDPOINT)
822 *ul_pipe = ATH6KL_USB_PIPE_TX_DATA_LP;
824 *ul_pipe = ATH6KL_USB_PIPE_TX_DATA_MP;
826 * Disable rxdata2 directly, it will be enabled
827 * if FW enable rxdata2
829 *dl_pipe = ATH6KL_USB_PIPE_RX_DATA;
839 static u16 ath6kl_usb_get_free_queue_number(struct ath6kl *ar, u8 pipe_id)
841 struct ath6kl_usb *device = ath6kl_usb_priv(ar);
843 return device->pipes[pipe_id].urb_cnt;
846 static void hif_detach_htc(struct ath6kl *ar)
848 struct ath6kl_usb *device = ath6kl_usb_priv(ar);
850 ath6kl_usb_flush_all(device);
853 static int ath6kl_usb_submit_ctrl_out(struct ath6kl_usb *ar_usb,
854 u8 req, u16 value, u16 index, void *data,
861 buf = kmalloc(size, GFP_KERNEL);
865 memcpy(buf, data, size);
868 /* note: if successful returns number of bytes transfered */
869 ret = usb_control_msg(ar_usb->udev,
870 usb_sndctrlpipe(ar_usb->udev, 0),
872 USB_DIR_OUT | USB_TYPE_VENDOR |
873 USB_RECIP_DEVICE, value, index, buf,
877 ath6kl_dbg(ATH6KL_DBG_USB, "%s failed,result = %d\n",
886 static int ath6kl_usb_submit_ctrl_in(struct ath6kl_usb *ar_usb,
887 u8 req, u16 value, u16 index, void *data,
894 buf = kmalloc(size, GFP_KERNEL);
899 /* note: if successful returns number of bytes transfered */
900 ret = usb_control_msg(ar_usb->udev,
901 usb_rcvctrlpipe(ar_usb->udev, 0),
903 USB_DIR_IN | USB_TYPE_VENDOR |
904 USB_RECIP_DEVICE, value, index, buf,
908 ath6kl_dbg(ATH6KL_DBG_USB, "%s failed,result = %d\n",
912 memcpy((u8 *) data, buf, size);
919 static int ath6kl_usb_ctrl_msg_exchange(struct ath6kl_usb *ar_usb,
920 u8 req_val, u8 *req_buf, u32 req_len,
921 u8 resp_val, u8 *resp_buf, u32 *resp_len)
926 ret = ath6kl_usb_submit_ctrl_out(ar_usb, req_val, 0, 0,
932 if (resp_buf == NULL) {
933 /* no expected response */
938 ret = ath6kl_usb_submit_ctrl_in(ar_usb, resp_val, 0, 0,
939 resp_buf, *resp_len);
944 static int ath6kl_usb_diag_read32(struct ath6kl *ar, u32 address, u32 *data)
946 struct ath6kl_usb *ar_usb = ar->hif_priv;
947 struct ath6kl_usb_ctrl_diag_resp_read *resp;
948 struct ath6kl_usb_ctrl_diag_cmd_read *cmd;
952 cmd = (struct ath6kl_usb_ctrl_diag_cmd_read *) ar_usb->diag_cmd_buffer;
954 memset(cmd, 0, sizeof(*cmd));
955 cmd->cmd = ATH6KL_USB_CTRL_DIAG_CC_READ;
956 cmd->address = cpu_to_le32(address);
957 resp_len = sizeof(*resp);
959 ret = ath6kl_usb_ctrl_msg_exchange(ar_usb,
960 ATH6KL_USB_CONTROL_REQ_DIAG_CMD,
962 sizeof(struct ath6kl_usb_ctrl_diag_cmd_write),
963 ATH6KL_USB_CONTROL_REQ_DIAG_RESP,
964 ar_usb->diag_resp_buffer, &resp_len);
969 resp = (struct ath6kl_usb_ctrl_diag_resp_read *)
970 ar_usb->diag_resp_buffer;
972 *data = le32_to_cpu(resp->value);
977 static int ath6kl_usb_diag_write32(struct ath6kl *ar, u32 address, __le32 data)
979 struct ath6kl_usb *ar_usb = ar->hif_priv;
980 struct ath6kl_usb_ctrl_diag_cmd_write *cmd;
982 cmd = (struct ath6kl_usb_ctrl_diag_cmd_write *) ar_usb->diag_cmd_buffer;
984 memset(cmd, 0, sizeof(struct ath6kl_usb_ctrl_diag_cmd_write));
985 cmd->cmd = cpu_to_le32(ATH6KL_USB_CTRL_DIAG_CC_WRITE);
986 cmd->address = cpu_to_le32(address);
989 return ath6kl_usb_ctrl_msg_exchange(ar_usb,
990 ATH6KL_USB_CONTROL_REQ_DIAG_CMD,
997 static int ath6kl_usb_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
999 struct ath6kl_usb *ar_usb = ar->hif_priv;
1003 ret = ath6kl_usb_submit_ctrl_in(ar_usb,
1004 ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP,
1007 ath6kl_err("Unable to read the bmi data from the device: %d\n",
1015 static int ath6kl_usb_bmi_write(struct ath6kl *ar, u8 *buf, u32 len)
1017 struct ath6kl_usb *ar_usb = ar->hif_priv;
1021 ret = ath6kl_usb_submit_ctrl_out(ar_usb,
1022 ATH6KL_USB_CONTROL_REQ_SEND_BMI_CMD,
1025 ath6kl_err("unable to send the bmi data to the device: %d\n",
1033 static int ath6kl_usb_power_on(struct ath6kl *ar)
1039 static int ath6kl_usb_power_off(struct ath6kl *ar)
1045 static void ath6kl_usb_stop(struct ath6kl *ar)
1050 static void ath6kl_usb_cleanup_scatter(struct ath6kl *ar)
1053 * USB doesn't support it. Just return.
1058 static const struct ath6kl_hif_ops ath6kl_usb_ops = {
1059 .diag_read32 = ath6kl_usb_diag_read32,
1060 .diag_write32 = ath6kl_usb_diag_write32,
1061 .bmi_read = ath6kl_usb_bmi_read,
1062 .bmi_write = ath6kl_usb_bmi_write,
1063 .power_on = ath6kl_usb_power_on,
1064 .power_off = ath6kl_usb_power_off,
1065 .stop = ath6kl_usb_stop,
1066 .pipe_send = ath6kl_usb_send,
1067 .pipe_get_default = ath6kl_usb_get_default_pipe,
1068 .pipe_map_service = ath6kl_usb_map_service_pipe,
1069 .pipe_get_free_queue_number = ath6kl_usb_get_free_queue_number,
1070 .cleanup_scatter = ath6kl_usb_cleanup_scatter,
1073 /* ath6kl usb driver registered functions */
1074 static int ath6kl_usb_probe(struct usb_interface *interface,
1075 const struct usb_device_id *id)
1077 struct usb_device *dev = interface_to_usbdev(interface);
1079 struct ath6kl_usb *ar_usb = NULL;
1080 int vendor_id, product_id;
1085 vendor_id = le16_to_cpu(dev->descriptor.idVendor);
1086 product_id = le16_to_cpu(dev->descriptor.idProduct);
1088 ath6kl_dbg(ATH6KL_DBG_USB, "vendor_id = %04x\n", vendor_id);
1089 ath6kl_dbg(ATH6KL_DBG_USB, "product_id = %04x\n", product_id);
1091 if (interface->cur_altsetting)
1092 ath6kl_dbg(ATH6KL_DBG_USB, "USB Interface %d\n",
1093 interface->cur_altsetting->desc.bInterfaceNumber);
1096 if (dev->speed == USB_SPEED_HIGH)
1097 ath6kl_dbg(ATH6KL_DBG_USB, "USB 2.0 Host\n");
1099 ath6kl_dbg(ATH6KL_DBG_USB, "USB 1.1 Host\n");
1101 ar_usb = ath6kl_usb_create(interface);
1103 if (ar_usb == NULL) {
1108 ar = ath6kl_core_create(&ar_usb->udev->dev);
1110 ath6kl_err("Failed to alloc ath6kl core\n");
1112 goto err_usb_destroy;
1115 ar->hif_priv = ar_usb;
1116 ar->hif_type = ATH6KL_HIF_TYPE_USB;
1117 ar->hif_ops = &ath6kl_usb_ops;
1118 ar->mbox_info.block_size = 16;
1119 ar->bmi.max_data_size = 252;
1123 ret = ath6kl_core_init(ar, ATH6KL_HTC_TYPE_PIPE);
1125 ath6kl_err("Failed to init ath6kl core: %d\n", ret);
1132 ath6kl_core_destroy(ar);
1134 ath6kl_usb_destroy(ar_usb);
1141 static void ath6kl_usb_remove(struct usb_interface *interface)
1143 usb_put_dev(interface_to_usbdev(interface));
1144 ath6kl_usb_device_detached(interface);
1149 static int ath6kl_usb_suspend(struct usb_interface *interface,
1150 pm_message_t message)
1152 struct ath6kl_usb *device;
1153 device = usb_get_intfdata(interface);
1155 ath6kl_usb_flush_all(device);
1159 static int ath6kl_usb_resume(struct usb_interface *interface)
1161 struct ath6kl_usb *device;
1162 device = usb_get_intfdata(interface);
1164 ath6kl_usb_post_recv_transfers(&device->pipes[ATH6KL_USB_PIPE_RX_DATA],
1165 ATH6KL_USB_RX_BUFFER_SIZE);
1166 ath6kl_usb_post_recv_transfers(&device->pipes[ATH6KL_USB_PIPE_RX_DATA2],
1167 ATH6KL_USB_RX_BUFFER_SIZE);
1172 static int ath6kl_usb_reset_resume(struct usb_interface *intf)
1174 if (usb_get_intfdata(intf))
1175 ath6kl_usb_remove(intf);
1181 #define ath6kl_usb_suspend NULL
1182 #define ath6kl_usb_resume NULL
1183 #define ath6kl_usb_reset_resume NULL
1187 /* table of devices that work with this driver */
1188 static struct usb_device_id ath6kl_usb_ids[] = {
1189 {USB_DEVICE(0x0cf3, 0x9374)},
1190 { /* Terminating entry */ },
1193 MODULE_DEVICE_TABLE(usb, ath6kl_usb_ids);
1195 static struct usb_driver ath6kl_usb_driver = {
1196 .name = "ath6kl_usb",
1197 .probe = ath6kl_usb_probe,
1198 .suspend = ath6kl_usb_suspend,
1199 .resume = ath6kl_usb_resume,
1200 .reset_resume = ath6kl_usb_reset_resume,
1201 .disconnect = ath6kl_usb_remove,
1202 .id_table = ath6kl_usb_ids,
1203 .supports_autosuspend = true,
1204 .disable_hub_initiated_lpm = 1,
1207 static int ath6kl_usb_init(void)
1211 ret = usb_register(&ath6kl_usb_driver);
1213 ath6kl_err("usb registration failed: %d\n", ret);
1220 static void ath6kl_usb_exit(void)
1222 usb_deregister(&ath6kl_usb_driver);
1225 module_init(ath6kl_usb_init);
1226 module_exit(ath6kl_usb_exit);
1228 MODULE_AUTHOR("Atheros Communications, Inc.");
1229 MODULE_DESCRIPTION("Driver support for Atheros AR600x USB devices");
1230 MODULE_LICENSE("Dual BSD/GPL");
1231 MODULE_FIRMWARE(AR6004_HW_1_0_FIRMWARE_FILE);
1232 MODULE_FIRMWARE(AR6004_HW_1_0_BOARD_DATA_FILE);
1233 MODULE_FIRMWARE(AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE);
1234 MODULE_FIRMWARE(AR6004_HW_1_1_FIRMWARE_FILE);
1235 MODULE_FIRMWARE(AR6004_HW_1_1_BOARD_DATA_FILE);
1236 MODULE_FIRMWARE(AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE);
1237 MODULE_FIRMWARE(AR6004_HW_1_2_FIRMWARE_FILE);
1238 MODULE_FIRMWARE(AR6004_HW_1_2_BOARD_DATA_FILE);
1239 MODULE_FIRMWARE(AR6004_HW_1_2_DEFAULT_BOARD_DATA_FILE);
1240 MODULE_FIRMWARE(AR6004_HW_1_3_FW_DIR "/" AR6004_HW_1_3_FIRMWARE_FILE);
1241 MODULE_FIRMWARE(AR6004_HW_1_3_BOARD_DATA_FILE);
1242 MODULE_FIRMWARE(AR6004_HW_1_3_DEFAULT_BOARD_DATA_FILE);