cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

usb.c (31815B)


      1/*
      2 * Copyright (c) 2007-2011 Atheros Communications Inc.
      3 * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
      4 *
      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.
      8 *
      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.
     16 */
     17
     18#include <linux/module.h>
     19#include <linux/usb.h>
     20
     21#include "debug.h"
     22#include "core.h"
     23
     24/* constants */
     25#define TX_URB_COUNT            32
     26#define RX_URB_COUNT            32
     27#define ATH6KL_USB_RX_BUFFER_SIZE  4096
     28
     29/* tx/rx pipes for usb */
     30enum 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,
     39	ATH6KL_USB_PIPE_MAX
     40};
     41
     42#define ATH6KL_USB_PIPE_INVALID ATH6KL_USB_PIPE_MAX
     43
     44struct ath6kl_usb_pipe {
     45	struct list_head urb_list_head;
     46	struct usb_anchor urb_submitted;
     47	u32 urb_alloc;
     48	u32 urb_cnt;
     49	u32 urb_cnt_thresh;
     50	unsigned int usb_pipe_handle;
     51	u32 flags;
     52	u8 ep_address;
     53	u8 logical_pipe_num;
     54	struct ath6kl_usb *ar_usb;
     55	u16 max_packet_size;
     56	struct work_struct io_complete_work;
     57	struct sk_buff_head io_comp_queue;
     58	struct usb_endpoint_descriptor *ep_desc;
     59};
     60
     61#define ATH6KL_USB_PIPE_FLAG_TX    (1 << 0)
     62
     63/* usb device object */
     64struct ath6kl_usb {
     65	/* protects pipe->urb_list_head and  pipe->urb_cnt */
     66	spinlock_t cs_lock;
     67
     68	struct usb_device *udev;
     69	struct usb_interface *interface;
     70	struct ath6kl_usb_pipe pipes[ATH6KL_USB_PIPE_MAX];
     71	u8 *diag_cmd_buffer;
     72	u8 *diag_resp_buffer;
     73	struct ath6kl *ar;
     74};
     75
     76/* usb urb object */
     77struct ath6kl_urb_context {
     78	struct list_head link;
     79	struct ath6kl_usb_pipe *pipe;
     80	struct sk_buff *skb;
     81	struct ath6kl *ar;
     82};
     83
     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
     89
     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
     94
     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
    100
    101#define ATH6KL_USB_CTRL_DIAG_CC_READ               0
    102#define ATH6KL_USB_CTRL_DIAG_CC_WRITE              1
    103
    104struct ath6kl_usb_ctrl_diag_cmd_write {
    105	__le32 cmd;
    106	__le32 address;
    107	__le32 value;
    108	__le32 _pad[1];
    109} __packed;
    110
    111struct ath6kl_usb_ctrl_diag_cmd_read {
    112	__le32 cmd;
    113	__le32 address;
    114} __packed;
    115
    116struct ath6kl_usb_ctrl_diag_resp_read {
    117	__le32 value;
    118} __packed;
    119
    120/* function declarations */
    121static void ath6kl_usb_recv_complete(struct urb *urb);
    122
    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)
    127
    128/* pipe/urb operations */
    129static struct ath6kl_urb_context *
    130ath6kl_usb_alloc_urb_from_pipe(struct ath6kl_usb_pipe *pipe)
    131{
    132	struct ath6kl_urb_context *urb_context = NULL;
    133	unsigned long flags;
    134
    135	/* bail if this pipe is not initialized */
    136	if (!pipe->ar_usb)
    137		return NULL;
    138
    139	spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
    140	if (!list_empty(&pipe->urb_list_head)) {
    141		urb_context =
    142		    list_first_entry(&pipe->urb_list_head,
    143				     struct ath6kl_urb_context, link);
    144		list_del(&urb_context->link);
    145		pipe->urb_cnt--;
    146	}
    147	spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
    148
    149	return urb_context;
    150}
    151
    152static void ath6kl_usb_free_urb_to_pipe(struct ath6kl_usb_pipe *pipe,
    153					struct ath6kl_urb_context *urb_context)
    154{
    155	unsigned long flags;
    156
    157	/* bail if this pipe is not initialized */
    158	if (!pipe->ar_usb)
    159		return;
    160
    161	spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
    162	pipe->urb_cnt++;
    163
    164	list_add(&urb_context->link, &pipe->urb_list_head);
    165	spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
    166}
    167
    168static void ath6kl_usb_cleanup_recv_urb(struct ath6kl_urb_context *urb_context)
    169{
    170	dev_kfree_skb(urb_context->skb);
    171	urb_context->skb = NULL;
    172
    173	ath6kl_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
    174}
    175
    176static inline struct ath6kl_usb *ath6kl_usb_priv(struct ath6kl *ar)
    177{
    178	return ar->hif_priv;
    179}
    180
    181/* pipe resource allocation/cleanup */
    182static int ath6kl_usb_alloc_pipe_resources(struct ath6kl_usb_pipe *pipe,
    183					   int urb_cnt)
    184{
    185	struct ath6kl_urb_context *urb_context;
    186	int status = 0, i;
    187
    188	INIT_LIST_HEAD(&pipe->urb_list_head);
    189	init_usb_anchor(&pipe->urb_submitted);
    190
    191	for (i = 0; i < urb_cnt; i++) {
    192		urb_context = kzalloc(sizeof(struct ath6kl_urb_context),
    193				      GFP_KERNEL);
    194		if (urb_context == NULL) {
    195			status = -ENOMEM;
    196			goto fail_alloc_pipe_resources;
    197		}
    198
    199		urb_context->pipe = pipe;
    200
    201		/*
    202		 * we are only allocate the urb contexts here, the actual URB
    203		 * is allocated from the kernel as needed to do a transaction
    204		 */
    205		pipe->urb_alloc++;
    206		ath6kl_usb_free_urb_to_pipe(pipe, urb_context);
    207	}
    208
    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,
    212		   pipe->urb_alloc);
    213
    214fail_alloc_pipe_resources:
    215	return status;
    216}
    217
    218static void ath6kl_usb_free_pipe_resources(struct ath6kl_usb_pipe *pipe)
    219{
    220	struct ath6kl_urb_context *urb_context;
    221
    222	if (pipe->ar_usb == NULL) {
    223		/* nothing allocated for this pipe */
    224		return;
    225	}
    226
    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);
    232
    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);
    239	}
    240
    241	while (true) {
    242		urb_context = ath6kl_usb_alloc_urb_from_pipe(pipe);
    243		if (urb_context == NULL)
    244			break;
    245		kfree(urb_context);
    246	}
    247}
    248
    249static void ath6kl_usb_cleanup_pipe_resources(struct ath6kl_usb *ar_usb)
    250{
    251	int i;
    252
    253	for (i = 0; i < ATH6KL_USB_PIPE_MAX; i++)
    254		ath6kl_usb_free_pipe_resources(&ar_usb->pipes[i]);
    255}
    256
    257static u8 ath6kl_usb_get_logical_pipe_num(struct ath6kl_usb *ar_usb,
    258					  u8 ep_address, int *urb_count)
    259{
    260	u8 pipe_num = ATH6KL_USB_PIPE_INVALID;
    261
    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;
    266		break;
    267	case ATH6KL_USB_EP_ADDR_APP_DATA_IN:
    268		pipe_num = ATH6KL_USB_PIPE_RX_DATA;
    269		*urb_count = RX_URB_COUNT;
    270		break;
    271	case ATH6KL_USB_EP_ADDR_APP_INT_IN:
    272		pipe_num = ATH6KL_USB_PIPE_RX_INT;
    273		*urb_count = RX_URB_COUNT;
    274		break;
    275	case ATH6KL_USB_EP_ADDR_APP_DATA2_IN:
    276		pipe_num = ATH6KL_USB_PIPE_RX_DATA2;
    277		*urb_count = RX_URB_COUNT;
    278		break;
    279	case ATH6KL_USB_EP_ADDR_APP_CTRL_OUT:
    280		pipe_num = ATH6KL_USB_PIPE_TX_CTRL;
    281		*urb_count = TX_URB_COUNT;
    282		break;
    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;
    286		break;
    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;
    290		break;
    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;
    294		break;
    295	default:
    296		/* note: there may be endpoints not currently used */
    297		break;
    298	}
    299
    300	return pipe_num;
    301}
    302
    303static int ath6kl_usb_setup_pipe_resources(struct ath6kl_usb *ar_usb)
    304{
    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;
    310	u8 pipe_num;
    311
    312	ath6kl_dbg(ATH6KL_DBG_USB, "setting up USB Pipes using interface\n");
    313
    314	/* walk descriptors and setup pipes */
    315	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
    316		endpoint = &iface_desc->endpoint[i].desc;
    317
    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",
    321				   ATH6KL_USB_IS_DIR_IN
    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",
    328				   ATH6KL_USB_IS_DIR_IN
    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)) {
    334			/* TODO for ISO */
    335			ath6kl_dbg(ATH6KL_DBG_USB,
    336				   "%s ISOC Ep:0x%2.2X maxpktsz:%d interval:%d\n",
    337				   ATH6KL_USB_IS_DIR_IN
    338				   (endpoint->bEndpointAddress) ?
    339				   "RX" : "TX", endpoint->bEndpointAddress,
    340				   le16_to_cpu(endpoint->wMaxPacketSize),
    341				   endpoint->bInterval);
    342		}
    343
    344		/* Ignore broken descriptors. */
    345		if (usb_endpoint_maxp(endpoint) == 0)
    346			continue;
    347
    348		urbcount = 0;
    349
    350		pipe_num =
    351		    ath6kl_usb_get_logical_pipe_num(ar_usb,
    352						    endpoint->bEndpointAddress,
    353						    &urbcount);
    354		if (pipe_num == ATH6KL_USB_PIPE_INVALID)
    355			continue;
    356
    357		pipe = &ar_usb->pipes[pipe_num];
    358		if (pipe->ar_usb != NULL) {
    359			/* hmmm..pipe was already setup */
    360			continue;
    361		}
    362
    363		pipe->ar_usb = ar_usb;
    364		pipe->logical_pipe_num = pipe_num;
    365		pipe->ep_address = endpoint->bEndpointAddress;
    366		pipe->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize);
    367
    368		if (ATH6KL_USB_IS_BULK_EP(endpoint->bmAttributes)) {
    369			if (ATH6KL_USB_IS_DIR_IN(pipe->ep_address)) {
    370				pipe->usb_pipe_handle =
    371				    usb_rcvbulkpipe(ar_usb->udev,
    372						    pipe->ep_address);
    373			} else {
    374				pipe->usb_pipe_handle =
    375				    usb_sndbulkpipe(ar_usb->udev,
    376						    pipe->ep_address);
    377			}
    378		} else if (ATH6KL_USB_IS_INT_EP(endpoint->bmAttributes)) {
    379			if (ATH6KL_USB_IS_DIR_IN(pipe->ep_address)) {
    380				pipe->usb_pipe_handle =
    381				    usb_rcvintpipe(ar_usb->udev,
    382						   pipe->ep_address);
    383			} else {
    384				pipe->usb_pipe_handle =
    385				    usb_sndintpipe(ar_usb->udev,
    386						   pipe->ep_address);
    387			}
    388		} else if (ATH6KL_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
    389			/* TODO for ISO */
    390			if (ATH6KL_USB_IS_DIR_IN(pipe->ep_address)) {
    391				pipe->usb_pipe_handle =
    392				    usb_rcvisocpipe(ar_usb->udev,
    393						    pipe->ep_address);
    394			} else {
    395				pipe->usb_pipe_handle =
    396				    usb_sndisocpipe(ar_usb->udev,
    397						    pipe->ep_address);
    398			}
    399		}
    400
    401		pipe->ep_desc = endpoint;
    402
    403		if (!ATH6KL_USB_IS_DIR_IN(pipe->ep_address))
    404			pipe->flags |= ATH6KL_USB_PIPE_FLAG_TX;
    405
    406		status = ath6kl_usb_alloc_pipe_resources(pipe, urbcount);
    407		if (status != 0)
    408			break;
    409	}
    410
    411	return status;
    412}
    413
    414/* pipe operations */
    415static void ath6kl_usb_post_recv_transfers(struct ath6kl_usb_pipe *recv_pipe,
    416					   int buffer_length)
    417{
    418	struct ath6kl_urb_context *urb_context;
    419	struct urb *urb;
    420	int usb_status;
    421
    422	while (true) {
    423		urb_context = ath6kl_usb_alloc_urb_from_pipe(recv_pipe);
    424		if (urb_context == NULL)
    425			break;
    426
    427		urb_context->skb = dev_alloc_skb(buffer_length);
    428		if (urb_context->skb == NULL)
    429			goto err_cleanup_urb;
    430
    431		urb = usb_alloc_urb(0, GFP_ATOMIC);
    432		if (urb == NULL)
    433			goto err_cleanup_urb;
    434
    435		usb_fill_bulk_urb(urb,
    436				  recv_pipe->ar_usb->udev,
    437				  recv_pipe->usb_pipe_handle,
    438				  urb_context->skb->data,
    439				  buffer_length,
    440				  ath6kl_usb_recv_complete, urb_context);
    441
    442		ath6kl_dbg(ATH6KL_DBG_USB_BULK,
    443			   "ath6kl usb: bulk recv submit:%d, 0x%X (ep:0x%2.2X), %d bytes buf:0x%p\n",
    444			   recv_pipe->logical_pipe_num,
    445			   recv_pipe->usb_pipe_handle, recv_pipe->ep_address,
    446			   buffer_length, urb_context->skb);
    447
    448		usb_anchor_urb(urb, &recv_pipe->urb_submitted);
    449		usb_status = usb_submit_urb(urb, GFP_ATOMIC);
    450
    451		if (usb_status) {
    452			ath6kl_dbg(ATH6KL_DBG_USB_BULK,
    453				   "ath6kl usb : usb bulk recv failed %d\n",
    454				   usb_status);
    455			usb_unanchor_urb(urb);
    456			usb_free_urb(urb);
    457			goto err_cleanup_urb;
    458		}
    459		usb_free_urb(urb);
    460	}
    461	return;
    462
    463err_cleanup_urb:
    464	ath6kl_usb_cleanup_recv_urb(urb_context);
    465	return;
    466}
    467
    468static void ath6kl_usb_flush_all(struct ath6kl_usb *ar_usb)
    469{
    470	int i;
    471
    472	for (i = 0; i < ATH6KL_USB_PIPE_MAX; i++) {
    473		if (ar_usb->pipes[i].ar_usb != NULL)
    474			usb_kill_anchored_urbs(&ar_usb->pipes[i].urb_submitted);
    475	}
    476
    477	/*
    478	 * Flushing any pending I/O may schedule work this call will block
    479	 * until all scheduled work runs to completion.
    480	 */
    481	flush_scheduled_work();
    482}
    483
    484static void ath6kl_usb_start_recv_pipes(struct ath6kl_usb *ar_usb)
    485{
    486	/*
    487	 * note: control pipe is no longer used
    488	 * ar_usb->pipes[ATH6KL_USB_PIPE_RX_CTRL].urb_cnt_thresh =
    489	 *      ar_usb->pipes[ATH6KL_USB_PIPE_RX_CTRL].urb_alloc/2;
    490	 * ath6kl_usb_post_recv_transfers(&ar_usb->
    491	 *		pipes[ATH6KL_USB_PIPE_RX_CTRL],
    492	 *		ATH6KL_USB_RX_BUFFER_SIZE);
    493	 */
    494
    495	ar_usb->pipes[ATH6KL_USB_PIPE_RX_DATA].urb_cnt_thresh = 1;
    496
    497	ath6kl_usb_post_recv_transfers(&ar_usb->pipes[ATH6KL_USB_PIPE_RX_DATA],
    498				       ATH6KL_USB_RX_BUFFER_SIZE);
    499}
    500
    501/* hif usb rx/tx completion functions */
    502static void ath6kl_usb_recv_complete(struct urb *urb)
    503{
    504	struct ath6kl_urb_context *urb_context = urb->context;
    505	struct ath6kl_usb_pipe *pipe = urb_context->pipe;
    506	struct sk_buff *skb = NULL;
    507	int status = 0;
    508
    509	ath6kl_dbg(ATH6KL_DBG_USB_BULK,
    510		   "%s: recv pipe: %d, stat:%d, len:%d urb:0x%p\n", __func__,
    511		   pipe->logical_pipe_num, urb->status, urb->actual_length,
    512		   urb);
    513
    514	if (urb->status != 0) {
    515		status = -EIO;
    516		switch (urb->status) {
    517		case -ECONNRESET:
    518		case -ENOENT:
    519		case -ESHUTDOWN:
    520			/*
    521			 * no need to spew these errors when device
    522			 * removed or urb killed due to driver shutdown
    523			 */
    524			status = -ECANCELED;
    525			break;
    526		default:
    527			ath6kl_dbg(ATH6KL_DBG_USB_BULK,
    528				   "%s recv pipe: %d (ep:0x%2.2X), failed:%d\n",
    529				   __func__, pipe->logical_pipe_num,
    530				   pipe->ep_address, urb->status);
    531			break;
    532		}
    533		goto cleanup_recv_urb;
    534	}
    535
    536	if (urb->actual_length == 0)
    537		goto cleanup_recv_urb;
    538
    539	skb = urb_context->skb;
    540
    541	/* we are going to pass it up */
    542	urb_context->skb = NULL;
    543	skb_put(skb, urb->actual_length);
    544
    545	/* note: queue implements a lock */
    546	skb_queue_tail(&pipe->io_comp_queue, skb);
    547	schedule_work(&pipe->io_complete_work);
    548
    549cleanup_recv_urb:
    550	ath6kl_usb_cleanup_recv_urb(urb_context);
    551
    552	if (status == 0 &&
    553	    pipe->urb_cnt >= pipe->urb_cnt_thresh) {
    554		/* our free urbs are piling up, post more transfers */
    555		ath6kl_usb_post_recv_transfers(pipe, ATH6KL_USB_RX_BUFFER_SIZE);
    556	}
    557}
    558
    559static void ath6kl_usb_usb_transmit_complete(struct urb *urb)
    560{
    561	struct ath6kl_urb_context *urb_context = urb->context;
    562	struct ath6kl_usb_pipe *pipe = urb_context->pipe;
    563	struct sk_buff *skb;
    564
    565	ath6kl_dbg(ATH6KL_DBG_USB_BULK,
    566		   "%s: pipe: %d, stat:%d, len:%d\n",
    567		   __func__, pipe->logical_pipe_num, urb->status,
    568		   urb->actual_length);
    569
    570	if (urb->status != 0) {
    571		ath6kl_dbg(ATH6KL_DBG_USB_BULK,
    572			   "%s:  pipe: %d, failed:%d\n",
    573			   __func__, pipe->logical_pipe_num, urb->status);
    574	}
    575
    576	skb = urb_context->skb;
    577	urb_context->skb = NULL;
    578	ath6kl_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
    579
    580	/* note: queue implements a lock */
    581	skb_queue_tail(&pipe->io_comp_queue, skb);
    582	schedule_work(&pipe->io_complete_work);
    583}
    584
    585static void ath6kl_usb_io_comp_work(struct work_struct *work)
    586{
    587	struct ath6kl_usb_pipe *pipe = container_of(work,
    588						    struct ath6kl_usb_pipe,
    589						    io_complete_work);
    590	struct ath6kl_usb *ar_usb;
    591	struct sk_buff *skb;
    592
    593	ar_usb = pipe->ar_usb;
    594
    595	while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
    596		if (pipe->flags & ATH6KL_USB_PIPE_FLAG_TX) {
    597			ath6kl_dbg(ATH6KL_DBG_USB_BULK,
    598				   "ath6kl usb xmit callback buf:0x%p\n", skb);
    599			ath6kl_core_tx_complete(ar_usb->ar, skb);
    600		} else {
    601			ath6kl_dbg(ATH6KL_DBG_USB_BULK,
    602				   "ath6kl usb recv callback buf:0x%p\n", skb);
    603			ath6kl_core_rx_complete(ar_usb->ar, skb,
    604						pipe->logical_pipe_num);
    605		}
    606	}
    607}
    608
    609#define ATH6KL_USB_MAX_DIAG_CMD (sizeof(struct ath6kl_usb_ctrl_diag_cmd_write))
    610#define ATH6KL_USB_MAX_DIAG_RESP (sizeof(struct ath6kl_usb_ctrl_diag_resp_read))
    611
    612static void ath6kl_usb_destroy(struct ath6kl_usb *ar_usb)
    613{
    614	ath6kl_usb_flush_all(ar_usb);
    615
    616	ath6kl_usb_cleanup_pipe_resources(ar_usb);
    617
    618	usb_set_intfdata(ar_usb->interface, NULL);
    619
    620	kfree(ar_usb->diag_cmd_buffer);
    621	kfree(ar_usb->diag_resp_buffer);
    622
    623	kfree(ar_usb);
    624}
    625
    626static struct ath6kl_usb *ath6kl_usb_create(struct usb_interface *interface)
    627{
    628	struct usb_device *dev = interface_to_usbdev(interface);
    629	struct ath6kl_usb *ar_usb;
    630	struct ath6kl_usb_pipe *pipe;
    631	int status = 0;
    632	int i;
    633
    634	ar_usb = kzalloc(sizeof(struct ath6kl_usb), GFP_KERNEL);
    635	if (ar_usb == NULL)
    636		goto fail_ath6kl_usb_create;
    637
    638	usb_set_intfdata(interface, ar_usb);
    639	spin_lock_init(&(ar_usb->cs_lock));
    640	ar_usb->udev = dev;
    641	ar_usb->interface = interface;
    642
    643	for (i = 0; i < ATH6KL_USB_PIPE_MAX; i++) {
    644		pipe = &ar_usb->pipes[i];
    645		INIT_WORK(&pipe->io_complete_work,
    646			  ath6kl_usb_io_comp_work);
    647		skb_queue_head_init(&pipe->io_comp_queue);
    648	}
    649
    650	ar_usb->diag_cmd_buffer = kzalloc(ATH6KL_USB_MAX_DIAG_CMD, GFP_KERNEL);
    651	if (ar_usb->diag_cmd_buffer == NULL) {
    652		status = -ENOMEM;
    653		goto fail_ath6kl_usb_create;
    654	}
    655
    656	ar_usb->diag_resp_buffer = kzalloc(ATH6KL_USB_MAX_DIAG_RESP,
    657					   GFP_KERNEL);
    658	if (ar_usb->diag_resp_buffer == NULL) {
    659		status = -ENOMEM;
    660		goto fail_ath6kl_usb_create;
    661	}
    662
    663	status = ath6kl_usb_setup_pipe_resources(ar_usb);
    664
    665fail_ath6kl_usb_create:
    666	if (status != 0) {
    667		ath6kl_usb_destroy(ar_usb);
    668		ar_usb = NULL;
    669	}
    670	return ar_usb;
    671}
    672
    673static void ath6kl_usb_device_detached(struct usb_interface *interface)
    674{
    675	struct ath6kl_usb *ar_usb;
    676
    677	ar_usb = usb_get_intfdata(interface);
    678	if (ar_usb == NULL)
    679		return;
    680
    681	ath6kl_stop_txrx(ar_usb->ar);
    682
    683	/* Delay to wait for the target to reboot */
    684	mdelay(20);
    685	ath6kl_core_cleanup(ar_usb->ar);
    686	ath6kl_usb_destroy(ar_usb);
    687}
    688
    689/* exported hif usb APIs for htc pipe */
    690static void hif_start(struct ath6kl *ar)
    691{
    692	struct ath6kl_usb *device = ath6kl_usb_priv(ar);
    693	int i;
    694
    695	ath6kl_usb_start_recv_pipes(device);
    696
    697	/* set the TX resource avail threshold for each TX pipe */
    698	for (i = ATH6KL_USB_PIPE_TX_CTRL;
    699	     i <= ATH6KL_USB_PIPE_TX_DATA_HP; i++) {
    700		device->pipes[i].urb_cnt_thresh =
    701		    device->pipes[i].urb_alloc / 2;
    702	}
    703}
    704
    705static int ath6kl_usb_send(struct ath6kl *ar, u8 PipeID,
    706			   struct sk_buff *hdr_skb, struct sk_buff *skb)
    707{
    708	struct ath6kl_usb *device = ath6kl_usb_priv(ar);
    709	struct ath6kl_usb_pipe *pipe = &device->pipes[PipeID];
    710	struct ath6kl_urb_context *urb_context;
    711	int usb_status, status = 0;
    712	struct urb *urb;
    713	u8 *data;
    714	u32 len;
    715
    716	ath6kl_dbg(ATH6KL_DBG_USB_BULK, "+%s pipe : %d, buf:0x%p\n",
    717		   __func__, PipeID, skb);
    718
    719	urb_context = ath6kl_usb_alloc_urb_from_pipe(pipe);
    720
    721	if (urb_context == NULL) {
    722		/*
    723		 * TODO: it is possible to run out of urbs if
    724		 * 2 endpoints map to the same pipe ID
    725		 */
    726		ath6kl_dbg(ATH6KL_DBG_USB_BULK,
    727			   "%s pipe:%d no urbs left. URB Cnt : %d\n",
    728			   __func__, PipeID, pipe->urb_cnt);
    729		status = -ENOMEM;
    730		goto fail_hif_send;
    731	}
    732
    733	urb_context->skb = skb;
    734
    735	data = skb->data;
    736	len = skb->len;
    737
    738	urb = usb_alloc_urb(0, GFP_ATOMIC);
    739	if (urb == NULL) {
    740		status = -ENOMEM;
    741		ath6kl_usb_free_urb_to_pipe(urb_context->pipe,
    742					    urb_context);
    743		goto fail_hif_send;
    744	}
    745
    746	usb_fill_bulk_urb(urb,
    747			  device->udev,
    748			  pipe->usb_pipe_handle,
    749			  data,
    750			  len,
    751			  ath6kl_usb_usb_transmit_complete, urb_context);
    752
    753	if ((len % pipe->max_packet_size) == 0) {
    754		/* hit a max packet boundary on this pipe */
    755		urb->transfer_flags |= URB_ZERO_PACKET;
    756	}
    757
    758	ath6kl_dbg(ATH6KL_DBG_USB_BULK,
    759		   "athusb bulk send submit:%d, 0x%X (ep:0x%2.2X), %d bytes\n",
    760		   pipe->logical_pipe_num, pipe->usb_pipe_handle,
    761		   pipe->ep_address, len);
    762
    763	usb_anchor_urb(urb, &pipe->urb_submitted);
    764	usb_status = usb_submit_urb(urb, GFP_ATOMIC);
    765
    766	if (usb_status) {
    767		ath6kl_dbg(ATH6KL_DBG_USB_BULK,
    768			   "ath6kl usb : usb bulk transmit failed %d\n",
    769			   usb_status);
    770		usb_unanchor_urb(urb);
    771		ath6kl_usb_free_urb_to_pipe(urb_context->pipe,
    772					    urb_context);
    773		status = -EINVAL;
    774	}
    775	usb_free_urb(urb);
    776
    777fail_hif_send:
    778	return status;
    779}
    780
    781static void hif_stop(struct ath6kl *ar)
    782{
    783	struct ath6kl_usb *device = ath6kl_usb_priv(ar);
    784
    785	ath6kl_usb_flush_all(device);
    786}
    787
    788static void ath6kl_usb_get_default_pipe(struct ath6kl *ar,
    789					u8 *ul_pipe, u8 *dl_pipe)
    790{
    791	*ul_pipe = ATH6KL_USB_PIPE_TX_CTRL;
    792	*dl_pipe = ATH6KL_USB_PIPE_RX_CTRL;
    793}
    794
    795static int ath6kl_usb_map_service_pipe(struct ath6kl *ar, u16 svc_id,
    796				       u8 *ul_pipe, u8 *dl_pipe)
    797{
    798	int status = 0;
    799
    800	switch (svc_id) {
    801	case HTC_CTRL_RSVD_SVC:
    802	case WMI_CONTROL_SVC:
    803		*ul_pipe = ATH6KL_USB_PIPE_TX_CTRL;
    804		/* due to large control packets, shift to data pipe */
    805		*dl_pipe = ATH6KL_USB_PIPE_RX_DATA;
    806		break;
    807	case WMI_DATA_BE_SVC:
    808	case WMI_DATA_BK_SVC:
    809		*ul_pipe = ATH6KL_USB_PIPE_TX_DATA_LP;
    810		/*
    811		* Disable rxdata2 directly, it will be enabled
    812		* if FW enable rxdata2
    813		*/
    814		*dl_pipe = ATH6KL_USB_PIPE_RX_DATA;
    815		break;
    816	case WMI_DATA_VI_SVC:
    817
    818		if (test_bit(ATH6KL_FW_CAPABILITY_MAP_LP_ENDPOINT,
    819			     ar->fw_capabilities))
    820			*ul_pipe = ATH6KL_USB_PIPE_TX_DATA_LP;
    821		else
    822			*ul_pipe = ATH6KL_USB_PIPE_TX_DATA_MP;
    823		/*
    824		* Disable rxdata2 directly, it will be enabled
    825		* if FW enable rxdata2
    826		*/
    827		*dl_pipe = ATH6KL_USB_PIPE_RX_DATA;
    828		break;
    829	case WMI_DATA_VO_SVC:
    830
    831		if (test_bit(ATH6KL_FW_CAPABILITY_MAP_LP_ENDPOINT,
    832			     ar->fw_capabilities))
    833			*ul_pipe = ATH6KL_USB_PIPE_TX_DATA_LP;
    834		else
    835			*ul_pipe = ATH6KL_USB_PIPE_TX_DATA_MP;
    836		/*
    837		* Disable rxdata2 directly, it will be enabled
    838		* if FW enable rxdata2
    839		*/
    840		*dl_pipe = ATH6KL_USB_PIPE_RX_DATA;
    841		break;
    842	default:
    843		status = -EPERM;
    844		break;
    845	}
    846
    847	return status;
    848}
    849
    850static u16 ath6kl_usb_get_free_queue_number(struct ath6kl *ar, u8 pipe_id)
    851{
    852	struct ath6kl_usb *device = ath6kl_usb_priv(ar);
    853
    854	return device->pipes[pipe_id].urb_cnt;
    855}
    856
    857static void hif_detach_htc(struct ath6kl *ar)
    858{
    859	struct ath6kl_usb *device = ath6kl_usb_priv(ar);
    860
    861	ath6kl_usb_flush_all(device);
    862}
    863
    864static int ath6kl_usb_submit_ctrl_out(struct ath6kl_usb *ar_usb,
    865				   u8 req, u16 value, u16 index, void *data,
    866				   u32 size)
    867{
    868	u8 *buf = NULL;
    869	int ret;
    870
    871	if (size > 0) {
    872		buf = kmemdup(data, size, GFP_KERNEL);
    873		if (buf == NULL)
    874			return -ENOMEM;
    875	}
    876
    877	/* note: if successful returns number of bytes transfered */
    878	ret = usb_control_msg(ar_usb->udev,
    879			      usb_sndctrlpipe(ar_usb->udev, 0),
    880			      req,
    881			      USB_DIR_OUT | USB_TYPE_VENDOR |
    882			      USB_RECIP_DEVICE, value, index, buf,
    883			      size, 1000);
    884
    885	if (ret < 0) {
    886		ath6kl_warn("Failed to submit usb control message: %d\n", ret);
    887		kfree(buf);
    888		return ret;
    889	}
    890
    891	kfree(buf);
    892
    893	return 0;
    894}
    895
    896static int ath6kl_usb_submit_ctrl_in(struct ath6kl_usb *ar_usb,
    897				  u8 req, u16 value, u16 index, void *data,
    898				  u32 size)
    899{
    900	u8 *buf = NULL;
    901	int ret;
    902
    903	if (size > 0) {
    904		buf = kmalloc(size, GFP_KERNEL);
    905		if (buf == NULL)
    906			return -ENOMEM;
    907	}
    908
    909	/* note: if successful returns number of bytes transfered */
    910	ret = usb_control_msg(ar_usb->udev,
    911				 usb_rcvctrlpipe(ar_usb->udev, 0),
    912				 req,
    913				 USB_DIR_IN | USB_TYPE_VENDOR |
    914				 USB_RECIP_DEVICE, value, index, buf,
    915				 size, 2000);
    916
    917	if (ret < 0) {
    918		ath6kl_warn("Failed to read usb control message: %d\n", ret);
    919		kfree(buf);
    920		return ret;
    921	}
    922
    923	memcpy((u8 *) data, buf, size);
    924
    925	kfree(buf);
    926
    927	return 0;
    928}
    929
    930static int ath6kl_usb_ctrl_msg_exchange(struct ath6kl_usb *ar_usb,
    931				     u8 req_val, u8 *req_buf, u32 req_len,
    932				     u8 resp_val, u8 *resp_buf, u32 *resp_len)
    933{
    934	int ret;
    935
    936	/* send command */
    937	ret = ath6kl_usb_submit_ctrl_out(ar_usb, req_val, 0, 0,
    938					 req_buf, req_len);
    939
    940	if (ret != 0)
    941		return ret;
    942
    943	if (resp_buf == NULL) {
    944		/* no expected response */
    945		return ret;
    946	}
    947
    948	/* get response */
    949	ret = ath6kl_usb_submit_ctrl_in(ar_usb, resp_val, 0, 0,
    950					resp_buf, *resp_len);
    951
    952	return ret;
    953}
    954
    955static int ath6kl_usb_diag_read32(struct ath6kl *ar, u32 address, u32 *data)
    956{
    957	struct ath6kl_usb *ar_usb = ar->hif_priv;
    958	struct ath6kl_usb_ctrl_diag_resp_read *resp;
    959	struct ath6kl_usb_ctrl_diag_cmd_read *cmd;
    960	u32 resp_len;
    961	int ret;
    962
    963	cmd = (struct ath6kl_usb_ctrl_diag_cmd_read *) ar_usb->diag_cmd_buffer;
    964
    965	memset(cmd, 0, sizeof(*cmd));
    966	cmd->cmd = ATH6KL_USB_CTRL_DIAG_CC_READ;
    967	cmd->address = cpu_to_le32(address);
    968	resp_len = sizeof(*resp);
    969
    970	ret = ath6kl_usb_ctrl_msg_exchange(ar_usb,
    971				ATH6KL_USB_CONTROL_REQ_DIAG_CMD,
    972				(u8 *) cmd,
    973				sizeof(struct ath6kl_usb_ctrl_diag_cmd_write),
    974				ATH6KL_USB_CONTROL_REQ_DIAG_RESP,
    975				ar_usb->diag_resp_buffer, &resp_len);
    976
    977	if (ret) {
    978		ath6kl_warn("diag read32 failed: %d\n", ret);
    979		return ret;
    980	}
    981
    982	resp = (struct ath6kl_usb_ctrl_diag_resp_read *)
    983		ar_usb->diag_resp_buffer;
    984
    985	*data = le32_to_cpu(resp->value);
    986
    987	return ret;
    988}
    989
    990static int ath6kl_usb_diag_write32(struct ath6kl *ar, u32 address, __le32 data)
    991{
    992	struct ath6kl_usb *ar_usb = ar->hif_priv;
    993	struct ath6kl_usb_ctrl_diag_cmd_write *cmd;
    994	int ret;
    995
    996	cmd = (struct ath6kl_usb_ctrl_diag_cmd_write *) ar_usb->diag_cmd_buffer;
    997
    998	memset(cmd, 0, sizeof(struct ath6kl_usb_ctrl_diag_cmd_write));
    999	cmd->cmd = cpu_to_le32(ATH6KL_USB_CTRL_DIAG_CC_WRITE);
   1000	cmd->address = cpu_to_le32(address);
   1001	cmd->value = data;
   1002
   1003	ret = ath6kl_usb_ctrl_msg_exchange(ar_usb,
   1004					   ATH6KL_USB_CONTROL_REQ_DIAG_CMD,
   1005					   (u8 *) cmd,
   1006					   sizeof(*cmd),
   1007					   0, NULL, NULL);
   1008	if (ret) {
   1009		ath6kl_warn("diag_write32 failed: %d\n", ret);
   1010		return ret;
   1011	}
   1012
   1013	return 0;
   1014}
   1015
   1016static int ath6kl_usb_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
   1017{
   1018	struct ath6kl_usb *ar_usb = ar->hif_priv;
   1019	int ret;
   1020
   1021	/* get response */
   1022	ret = ath6kl_usb_submit_ctrl_in(ar_usb,
   1023					ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP,
   1024					0, 0, buf, len);
   1025	if (ret) {
   1026		ath6kl_err("Unable to read the bmi data from the device: %d\n",
   1027			   ret);
   1028		return ret;
   1029	}
   1030
   1031	return 0;
   1032}
   1033
   1034static int ath6kl_usb_bmi_write(struct ath6kl *ar, u8 *buf, u32 len)
   1035{
   1036	struct ath6kl_usb *ar_usb = ar->hif_priv;
   1037	int ret;
   1038
   1039	/* send command */
   1040	ret = ath6kl_usb_submit_ctrl_out(ar_usb,
   1041					 ATH6KL_USB_CONTROL_REQ_SEND_BMI_CMD,
   1042					 0, 0, buf, len);
   1043	if (ret) {
   1044		ath6kl_err("unable to send the bmi data to the device: %d\n",
   1045			   ret);
   1046		return ret;
   1047	}
   1048
   1049	return 0;
   1050}
   1051
   1052static int ath6kl_usb_power_on(struct ath6kl *ar)
   1053{
   1054	hif_start(ar);
   1055	return 0;
   1056}
   1057
   1058static int ath6kl_usb_power_off(struct ath6kl *ar)
   1059{
   1060	hif_detach_htc(ar);
   1061	return 0;
   1062}
   1063
   1064static void ath6kl_usb_stop(struct ath6kl *ar)
   1065{
   1066	hif_stop(ar);
   1067}
   1068
   1069static void ath6kl_usb_cleanup_scatter(struct ath6kl *ar)
   1070{
   1071	/*
   1072	 * USB doesn't support it. Just return.
   1073	 */
   1074	return;
   1075}
   1076
   1077static int ath6kl_usb_suspend(struct ath6kl *ar, struct cfg80211_wowlan *wow)
   1078{
   1079	/*
   1080	 * cfg80211 suspend/WOW currently not supported for USB.
   1081	 */
   1082	return 0;
   1083}
   1084
   1085static int ath6kl_usb_resume(struct ath6kl *ar)
   1086{
   1087	/*
   1088	 * cfg80211 resume currently not supported for USB.
   1089	 */
   1090	return 0;
   1091}
   1092
   1093static const struct ath6kl_hif_ops ath6kl_usb_ops = {
   1094	.diag_read32 = ath6kl_usb_diag_read32,
   1095	.diag_write32 = ath6kl_usb_diag_write32,
   1096	.bmi_read = ath6kl_usb_bmi_read,
   1097	.bmi_write = ath6kl_usb_bmi_write,
   1098	.power_on = ath6kl_usb_power_on,
   1099	.power_off = ath6kl_usb_power_off,
   1100	.stop = ath6kl_usb_stop,
   1101	.pipe_send = ath6kl_usb_send,
   1102	.pipe_get_default = ath6kl_usb_get_default_pipe,
   1103	.pipe_map_service = ath6kl_usb_map_service_pipe,
   1104	.pipe_get_free_queue_number = ath6kl_usb_get_free_queue_number,
   1105	.cleanup_scatter = ath6kl_usb_cleanup_scatter,
   1106	.suspend = ath6kl_usb_suspend,
   1107	.resume = ath6kl_usb_resume,
   1108};
   1109
   1110/* ath6kl usb driver registered functions */
   1111static int ath6kl_usb_probe(struct usb_interface *interface,
   1112			    const struct usb_device_id *id)
   1113{
   1114	struct usb_device *dev = interface_to_usbdev(interface);
   1115	struct ath6kl *ar;
   1116	struct ath6kl_usb *ar_usb = NULL;
   1117	int vendor_id, product_id;
   1118	int ret = 0;
   1119
   1120	usb_get_dev(dev);
   1121
   1122	vendor_id = le16_to_cpu(dev->descriptor.idVendor);
   1123	product_id = le16_to_cpu(dev->descriptor.idProduct);
   1124
   1125	ath6kl_dbg(ATH6KL_DBG_USB, "vendor_id = %04x\n", vendor_id);
   1126	ath6kl_dbg(ATH6KL_DBG_USB, "product_id = %04x\n", product_id);
   1127
   1128	if (interface->cur_altsetting)
   1129		ath6kl_dbg(ATH6KL_DBG_USB, "USB Interface %d\n",
   1130			   interface->cur_altsetting->desc.bInterfaceNumber);
   1131
   1132
   1133	if (dev->speed == USB_SPEED_HIGH)
   1134		ath6kl_dbg(ATH6KL_DBG_USB, "USB 2.0 Host\n");
   1135	else
   1136		ath6kl_dbg(ATH6KL_DBG_USB, "USB 1.1 Host\n");
   1137
   1138	ar_usb = ath6kl_usb_create(interface);
   1139
   1140	if (ar_usb == NULL) {
   1141		ret = -ENOMEM;
   1142		goto err_usb_put;
   1143	}
   1144
   1145	ar = ath6kl_core_create(&ar_usb->udev->dev);
   1146	if (ar == NULL) {
   1147		ath6kl_err("Failed to alloc ath6kl core\n");
   1148		ret = -ENOMEM;
   1149		goto err_usb_destroy;
   1150	}
   1151
   1152	ar->hif_priv = ar_usb;
   1153	ar->hif_type = ATH6KL_HIF_TYPE_USB;
   1154	ar->hif_ops = &ath6kl_usb_ops;
   1155	ar->mbox_info.block_size = 16;
   1156	ar->bmi.max_data_size = 252;
   1157
   1158	ar_usb->ar = ar;
   1159
   1160	ret = ath6kl_core_init(ar, ATH6KL_HTC_TYPE_PIPE);
   1161	if (ret) {
   1162		ath6kl_err("Failed to init ath6kl core: %d\n", ret);
   1163		goto err_core_free;
   1164	}
   1165
   1166	return ret;
   1167
   1168err_core_free:
   1169	ath6kl_core_destroy(ar);
   1170err_usb_destroy:
   1171	ath6kl_usb_destroy(ar_usb);
   1172err_usb_put:
   1173	usb_put_dev(dev);
   1174
   1175	return ret;
   1176}
   1177
   1178static void ath6kl_usb_remove(struct usb_interface *interface)
   1179{
   1180	usb_put_dev(interface_to_usbdev(interface));
   1181	ath6kl_usb_device_detached(interface);
   1182}
   1183
   1184#ifdef CONFIG_PM
   1185
   1186static int ath6kl_usb_pm_suspend(struct usb_interface *interface,
   1187			      pm_message_t message)
   1188{
   1189	struct ath6kl_usb *device;
   1190	device = usb_get_intfdata(interface);
   1191
   1192	ath6kl_usb_flush_all(device);
   1193	return 0;
   1194}
   1195
   1196static int ath6kl_usb_pm_resume(struct usb_interface *interface)
   1197{
   1198	struct ath6kl_usb *device;
   1199	device = usb_get_intfdata(interface);
   1200
   1201	ath6kl_usb_post_recv_transfers(&device->pipes[ATH6KL_USB_PIPE_RX_DATA],
   1202				       ATH6KL_USB_RX_BUFFER_SIZE);
   1203	ath6kl_usb_post_recv_transfers(&device->pipes[ATH6KL_USB_PIPE_RX_DATA2],
   1204				       ATH6KL_USB_RX_BUFFER_SIZE);
   1205
   1206	return 0;
   1207}
   1208
   1209#else
   1210
   1211#define ath6kl_usb_pm_suspend NULL
   1212#define ath6kl_usb_pm_resume NULL
   1213
   1214#endif
   1215
   1216/* table of devices that work with this driver */
   1217static const struct usb_device_id ath6kl_usb_ids[] = {
   1218	{USB_DEVICE(0x0cf3, 0x9375)},
   1219	{USB_DEVICE(0x0cf3, 0x9374)},
   1220	{USB_DEVICE(0x04da, 0x390d)},
   1221	{ /* Terminating entry */ },
   1222};
   1223
   1224MODULE_DEVICE_TABLE(usb, ath6kl_usb_ids);
   1225
   1226static struct usb_driver ath6kl_usb_driver = {
   1227	.name = "ath6kl_usb",
   1228	.probe = ath6kl_usb_probe,
   1229	.suspend = ath6kl_usb_pm_suspend,
   1230	.resume = ath6kl_usb_pm_resume,
   1231	.disconnect = ath6kl_usb_remove,
   1232	.id_table = ath6kl_usb_ids,
   1233	.supports_autosuspend = true,
   1234	.disable_hub_initiated_lpm = 1,
   1235};
   1236
   1237module_usb_driver(ath6kl_usb_driver);
   1238
   1239MODULE_AUTHOR("Atheros Communications, Inc.");
   1240MODULE_DESCRIPTION("Driver support for Atheros AR600x USB devices");
   1241MODULE_LICENSE("Dual BSD/GPL");
   1242MODULE_FIRMWARE(AR6004_HW_1_0_FIRMWARE_FILE);
   1243MODULE_FIRMWARE(AR6004_HW_1_0_BOARD_DATA_FILE);
   1244MODULE_FIRMWARE(AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE);
   1245MODULE_FIRMWARE(AR6004_HW_1_1_FIRMWARE_FILE);
   1246MODULE_FIRMWARE(AR6004_HW_1_1_BOARD_DATA_FILE);
   1247MODULE_FIRMWARE(AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE);
   1248MODULE_FIRMWARE(AR6004_HW_1_2_FIRMWARE_FILE);
   1249MODULE_FIRMWARE(AR6004_HW_1_2_BOARD_DATA_FILE);
   1250MODULE_FIRMWARE(AR6004_HW_1_2_DEFAULT_BOARD_DATA_FILE);
   1251MODULE_FIRMWARE(AR6004_HW_1_3_FW_DIR "/" AR6004_HW_1_3_FIRMWARE_FILE);
   1252MODULE_FIRMWARE(AR6004_HW_1_3_BOARD_DATA_FILE);
   1253MODULE_FIRMWARE(AR6004_HW_1_3_DEFAULT_BOARD_DATA_FILE);