Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Marvell Wireless LAN device driver: USB specific handling |
| 3 | * |
Xinming Hu | 65da33f | 2014-06-19 21:38:57 -0700 | [diff] [blame] | 4 | * Copyright (C) 2012-2014, Marvell International Ltd. |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 5 | * |
| 6 | * This software file (the "File") is distributed by Marvell International |
| 7 | * Ltd. under the terms of the GNU General Public License Version 2, June 1991 |
| 8 | * (the "License"). You may use, redistribute and/or modify this File in |
| 9 | * accordance with the terms and conditions of the License, a copy of which |
| 10 | * is available by writing to the Free Software Foundation, Inc., |
| 11 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the |
| 12 | * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. |
| 13 | * |
| 14 | * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE |
| 16 | * ARE EXPRESSLY DISCLAIMED. The License provides additional details about |
| 17 | * this warranty disclaimer. |
| 18 | */ |
| 19 | |
| 20 | #include "main.h" |
| 21 | #include "usb.h" |
| 22 | |
| 23 | #define USB_VERSION "1.0" |
| 24 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 25 | static struct mwifiex_if_ops usb_ops; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 26 | |
| 27 | static struct usb_device_id mwifiex_usb_table[] = { |
Kristian Evensen | 9e6f3f4 | 2014-11-20 16:52:58 +0100 | [diff] [blame] | 28 | /* 8766 */ |
| 29 | {USB_DEVICE(USB8XXX_VID, USB8766_PID_1)}, |
| 30 | {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8766_PID_2, |
| 31 | USB_CLASS_VENDOR_SPEC, |
| 32 | USB_SUBCLASS_VENDOR_SPEC, 0xff)}, |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 33 | /* 8797 */ |
| 34 | {USB_DEVICE(USB8XXX_VID, USB8797_PID_1)}, |
| 35 | {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8797_PID_2, |
| 36 | USB_CLASS_VENDOR_SPEC, |
| 37 | USB_SUBCLASS_VENDOR_SPEC, 0xff)}, |
Yogesh Ashok Powar | eaa3d9f | 2015-01-23 17:09:18 +0530 | [diff] [blame] | 38 | /* 8801 */ |
| 39 | {USB_DEVICE(USB8XXX_VID, USB8801_PID_1)}, |
| 40 | {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8801_PID_2, |
| 41 | USB_CLASS_VENDOR_SPEC, |
| 42 | USB_SUBCLASS_VENDOR_SPEC, 0xff)}, |
Zhaoyang Liu | 6d85ef0 | 2015-08-05 06:09:40 -0700 | [diff] [blame] | 43 | /* 8997 */ |
| 44 | {USB_DEVICE(USB8XXX_VID, USB8997_PID_1)}, |
| 45 | {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8997_PID_2, |
| 46 | USB_CLASS_VENDOR_SPEC, |
| 47 | USB_SUBCLASS_VENDOR_SPEC, 0xff)}, |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 48 | { } /* Terminating entry */ |
| 49 | }; |
| 50 | |
| 51 | MODULE_DEVICE_TABLE(usb, mwifiex_usb_table); |
| 52 | |
| 53 | static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size); |
| 54 | |
| 55 | /* This function handles received packet. Necessary action is taken based on |
| 56 | * cmd/event/data. |
| 57 | */ |
| 58 | static int mwifiex_usb_recv(struct mwifiex_adapter *adapter, |
| 59 | struct sk_buff *skb, u8 ep) |
| 60 | { |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 61 | u32 recv_type; |
| 62 | __le32 tmp; |
Amitkumar Karwar | 8311f0d | 2012-06-20 20:21:13 -0700 | [diff] [blame] | 63 | int ret; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 64 | |
| 65 | if (adapter->hs_activated) |
| 66 | mwifiex_process_hs_config(adapter); |
| 67 | |
| 68 | if (skb->len < INTF_HEADER_LEN) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 69 | mwifiex_dbg(adapter, ERROR, |
| 70 | "%s: invalid skb->len\n", __func__); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | switch (ep) { |
| 75 | case MWIFIEX_USB_EP_CMD_EVENT: |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 76 | mwifiex_dbg(adapter, EVENT, |
| 77 | "%s: EP_CMD_EVENT\n", __func__); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 78 | skb_copy_from_linear_data(skb, &tmp, INTF_HEADER_LEN); |
| 79 | recv_type = le32_to_cpu(tmp); |
| 80 | skb_pull(skb, INTF_HEADER_LEN); |
| 81 | |
| 82 | switch (recv_type) { |
| 83 | case MWIFIEX_USB_TYPE_CMD: |
| 84 | if (skb->len > MWIFIEX_SIZE_OF_CMD_BUFFER) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 85 | mwifiex_dbg(adapter, ERROR, |
| 86 | "CMD: skb->len too large\n"); |
Amitkumar Karwar | 8311f0d | 2012-06-20 20:21:13 -0700 | [diff] [blame] | 87 | ret = -1; |
| 88 | goto exit_restore_skb; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 89 | } else if (!adapter->curr_cmd) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 90 | mwifiex_dbg(adapter, WARN, "CMD: no curr_cmd\n"); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 91 | if (adapter->ps_state == PS_STATE_SLEEP_CFM) { |
| 92 | mwifiex_process_sleep_confirm_resp( |
| 93 | adapter, skb->data, |
| 94 | skb->len); |
Amitkumar Karwar | 8311f0d | 2012-06-20 20:21:13 -0700 | [diff] [blame] | 95 | ret = 0; |
| 96 | goto exit_restore_skb; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 97 | } |
Amitkumar Karwar | 8311f0d | 2012-06-20 20:21:13 -0700 | [diff] [blame] | 98 | ret = -1; |
| 99 | goto exit_restore_skb; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | adapter->curr_cmd->resp_skb = skb; |
| 103 | adapter->cmd_resp_received = true; |
| 104 | break; |
| 105 | case MWIFIEX_USB_TYPE_EVENT: |
| 106 | if (skb->len < sizeof(u32)) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 107 | mwifiex_dbg(adapter, ERROR, |
| 108 | "EVENT: skb->len too small\n"); |
Amitkumar Karwar | 8311f0d | 2012-06-20 20:21:13 -0700 | [diff] [blame] | 109 | ret = -1; |
| 110 | goto exit_restore_skb; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 111 | } |
| 112 | skb_copy_from_linear_data(skb, &tmp, sizeof(u32)); |
| 113 | adapter->event_cause = le32_to_cpu(tmp); |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 114 | mwifiex_dbg(adapter, EVENT, |
| 115 | "event_cause %#x\n", adapter->event_cause); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 116 | |
| 117 | if (skb->len > MAX_EVENT_SIZE) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 118 | mwifiex_dbg(adapter, ERROR, |
| 119 | "EVENT: event body too large\n"); |
Amitkumar Karwar | 8311f0d | 2012-06-20 20:21:13 -0700 | [diff] [blame] | 120 | ret = -1; |
| 121 | goto exit_restore_skb; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Amitkumar Karwar | e80c81d | 2012-06-20 20:21:12 -0700 | [diff] [blame] | 124 | memcpy(adapter->event_body, skb->data + |
| 125 | MWIFIEX_EVENT_HEADER_LEN, skb->len); |
| 126 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 127 | adapter->event_received = true; |
| 128 | adapter->event_skb = skb; |
| 129 | break; |
| 130 | default: |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 131 | mwifiex_dbg(adapter, ERROR, |
| 132 | "unknown recv_type %#x\n", recv_type); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 133 | return -1; |
| 134 | } |
| 135 | break; |
| 136 | case MWIFIEX_USB_EP_DATA: |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 137 | mwifiex_dbg(adapter, DATA, "%s: EP_DATA\n", __func__); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 138 | if (skb->len > MWIFIEX_RX_DATA_BUF_SIZE) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 139 | mwifiex_dbg(adapter, ERROR, |
| 140 | "DATA: skb->len too large\n"); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 141 | return -1; |
| 142 | } |
Avinash Patil | ec4a16b | 2014-11-05 17:04:27 +0530 | [diff] [blame] | 143 | |
| 144 | skb_queue_tail(&adapter->rx_data_q, skb); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 145 | adapter->data_received = true; |
Avinash Patil | ec4a16b | 2014-11-05 17:04:27 +0530 | [diff] [blame] | 146 | atomic_inc(&adapter->rx_pending); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 147 | break; |
| 148 | default: |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 149 | mwifiex_dbg(adapter, ERROR, |
| 150 | "%s: unknown endport %#x\n", __func__, ep); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | return -EINPROGRESS; |
Amitkumar Karwar | 8311f0d | 2012-06-20 20:21:13 -0700 | [diff] [blame] | 155 | |
| 156 | exit_restore_skb: |
| 157 | /* The buffer will be reused for further cmds/events */ |
| 158 | skb_push(skb, INTF_HEADER_LEN); |
| 159 | |
| 160 | return ret; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void mwifiex_usb_rx_complete(struct urb *urb) |
| 164 | { |
| 165 | struct urb_context *context = (struct urb_context *)urb->context; |
| 166 | struct mwifiex_adapter *adapter = context->adapter; |
| 167 | struct sk_buff *skb = context->skb; |
| 168 | struct usb_card_rec *card; |
| 169 | int recv_length = urb->actual_length; |
| 170 | int size, status; |
| 171 | |
| 172 | if (!adapter || !adapter->card) { |
| 173 | pr_err("mwifiex adapter or card structure is not valid\n"); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | card = (struct usb_card_rec *)adapter->card; |
| 178 | if (card->rx_cmd_ep == context->ep) |
| 179 | atomic_dec(&card->rx_cmd_urb_pending); |
| 180 | else |
| 181 | atomic_dec(&card->rx_data_urb_pending); |
| 182 | |
| 183 | if (recv_length) { |
| 184 | if (urb->status || (adapter->surprise_removed)) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 185 | mwifiex_dbg(adapter, ERROR, |
| 186 | "URB status is failed: %d\n", urb->status); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 187 | /* Do not free skb in case of command ep */ |
| 188 | if (card->rx_cmd_ep != context->ep) |
| 189 | dev_kfree_skb_any(skb); |
| 190 | goto setup_for_next; |
| 191 | } |
| 192 | if (skb->len > recv_length) |
| 193 | skb_trim(skb, recv_length); |
| 194 | else |
| 195 | skb_put(skb, recv_length - skb->len); |
| 196 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 197 | status = mwifiex_usb_recv(adapter, skb, context->ep); |
| 198 | |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 199 | mwifiex_dbg(adapter, INFO, |
| 200 | "info: recv_length=%d, status=%d\n", |
| 201 | recv_length, status); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 202 | if (status == -EINPROGRESS) { |
Shengzhen Li | b2713f6 | 2015-03-13 17:37:54 +0530 | [diff] [blame] | 203 | mwifiex_queue_main_work(adapter); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 204 | |
| 205 | /* urb for data_ep is re-submitted now; |
| 206 | * urb for cmd_ep will be re-submitted in callback |
| 207 | * mwifiex_usb_recv_complete |
| 208 | */ |
| 209 | if (card->rx_cmd_ep == context->ep) |
| 210 | return; |
| 211 | } else { |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 212 | if (status == -1) |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 213 | mwifiex_dbg(adapter, ERROR, |
| 214 | "received data processing failed!\n"); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 215 | |
| 216 | /* Do not free skb in case of command ep */ |
| 217 | if (card->rx_cmd_ep != context->ep) |
| 218 | dev_kfree_skb_any(skb); |
| 219 | } |
| 220 | } else if (urb->status) { |
| 221 | if (!adapter->is_suspended) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 222 | mwifiex_dbg(adapter, FATAL, |
| 223 | "Card is removed: %d\n", urb->status); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 224 | adapter->surprise_removed = true; |
| 225 | } |
| 226 | dev_kfree_skb_any(skb); |
| 227 | return; |
| 228 | } else { |
| 229 | /* Do not free skb in case of command ep */ |
| 230 | if (card->rx_cmd_ep != context->ep) |
| 231 | dev_kfree_skb_any(skb); |
| 232 | |
| 233 | /* fall through setup_for_next */ |
| 234 | } |
| 235 | |
| 236 | setup_for_next: |
| 237 | if (card->rx_cmd_ep == context->ep) |
| 238 | size = MWIFIEX_RX_CMD_BUF_SIZE; |
| 239 | else |
| 240 | size = MWIFIEX_RX_DATA_BUF_SIZE; |
| 241 | |
Amitkumar Karwar | cf6a64f | 2014-11-05 17:04:29 +0530 | [diff] [blame] | 242 | if (card->rx_cmd_ep == context->ep) { |
| 243 | mwifiex_usb_submit_rx_urb(context, size); |
| 244 | } else { |
Ujjal Roy | 354a194 | 2016-02-25 00:49:56 +0530 | [diff] [blame] | 245 | if (atomic_read(&adapter->rx_pending) <= HIGH_RX_PENDING) { |
Amitkumar Karwar | cf6a64f | 2014-11-05 17:04:29 +0530 | [diff] [blame] | 246 | mwifiex_usb_submit_rx_urb(context, size); |
Ujjal Roy | 354a194 | 2016-02-25 00:49:56 +0530 | [diff] [blame] | 247 | } else { |
Reyad Attiyat | 11bdc44 | 2015-06-28 20:07:38 -0500 | [diff] [blame] | 248 | context->skb = NULL; |
| 249 | } |
Amitkumar Karwar | cf6a64f | 2014-11-05 17:04:29 +0530 | [diff] [blame] | 250 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 251 | |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | static void mwifiex_usb_tx_complete(struct urb *urb) |
| 256 | { |
| 257 | struct urb_context *context = (struct urb_context *)(urb->context); |
| 258 | struct mwifiex_adapter *adapter = context->adapter; |
| 259 | struct usb_card_rec *card = adapter->card; |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 260 | struct usb_tx_data_port *port; |
| 261 | int i; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 262 | |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 263 | mwifiex_dbg(adapter, INFO, |
| 264 | "%s: status: %d\n", __func__, urb->status); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 265 | |
| 266 | if (context->ep == card->tx_cmd_ep) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 267 | mwifiex_dbg(adapter, CMD, |
| 268 | "%s: CMD\n", __func__); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 269 | atomic_dec(&card->tx_cmd_urb_pending); |
| 270 | adapter->cmd_sent = false; |
| 271 | } else { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 272 | mwifiex_dbg(adapter, DATA, |
| 273 | "%s: DATA\n", __func__); |
Cathy Luo | 1afac19 | 2016-09-20 20:49:02 +0530 | [diff] [blame] | 274 | mwifiex_write_data_complete(adapter, context->skb, 0, |
| 275 | urb->status ? -1 : 0); |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 276 | for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { |
| 277 | port = &card->port[i]; |
| 278 | if (context->ep == port->tx_data_ep) { |
| 279 | atomic_dec(&port->tx_data_urb_pending); |
Zhaoyang Liu | 735ab6b | 2015-09-18 06:32:18 -0700 | [diff] [blame] | 280 | port->block_status = false; |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 281 | break; |
| 282 | } |
| 283 | } |
Zhaoyang Liu | d941444 | 2015-09-18 06:32:14 -0700 | [diff] [blame] | 284 | adapter->data_sent = false; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Zhaoyang Liu | 7e4e5d2 | 2015-09-18 06:32:17 -0700 | [diff] [blame] | 287 | if (card->mc_resync_flag) |
| 288 | mwifiex_multi_chan_resync(adapter); |
| 289 | |
Shengzhen Li | b2713f6 | 2015-03-13 17:37:54 +0530 | [diff] [blame] | 290 | mwifiex_queue_main_work(adapter); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 291 | |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size) |
| 296 | { |
| 297 | struct mwifiex_adapter *adapter = ctx->adapter; |
| 298 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 299 | |
| 300 | if (card->rx_cmd_ep != ctx->ep) { |
| 301 | ctx->skb = dev_alloc_skb(size); |
| 302 | if (!ctx->skb) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 303 | mwifiex_dbg(adapter, ERROR, |
| 304 | "%s: dev_alloc_skb failed\n", __func__); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 305 | return -ENOMEM; |
| 306 | } |
| 307 | } |
| 308 | |
Ganapathi Bhat | 182f569 | 2017-03-27 18:27:44 +0530 | [diff] [blame] | 309 | if (card->rx_cmd_ep == ctx->ep && |
| 310 | card->rx_cmd_ep_type == USB_ENDPOINT_XFER_INT) |
| 311 | usb_fill_int_urb(ctx->urb, card->udev, |
| 312 | usb_rcvintpipe(card->udev, ctx->ep), |
| 313 | ctx->skb->data, size, mwifiex_usb_rx_complete, |
| 314 | (void *)ctx, card->rx_cmd_interval); |
| 315 | else |
| 316 | usb_fill_bulk_urb(ctx->urb, card->udev, |
| 317 | usb_rcvbulkpipe(card->udev, ctx->ep), |
| 318 | ctx->skb->data, size, mwifiex_usb_rx_complete, |
| 319 | (void *)ctx); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 320 | |
| 321 | if (card->rx_cmd_ep == ctx->ep) |
| 322 | atomic_inc(&card->rx_cmd_urb_pending); |
| 323 | else |
| 324 | atomic_inc(&card->rx_data_urb_pending); |
| 325 | |
| 326 | if (usb_submit_urb(ctx->urb, GFP_ATOMIC)) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 327 | mwifiex_dbg(adapter, ERROR, "usb_submit_urb failed\n"); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 328 | dev_kfree_skb_any(ctx->skb); |
| 329 | ctx->skb = NULL; |
| 330 | |
| 331 | if (card->rx_cmd_ep == ctx->ep) |
| 332 | atomic_dec(&card->rx_cmd_urb_pending); |
| 333 | else |
| 334 | atomic_dec(&card->rx_data_urb_pending); |
| 335 | |
| 336 | return -1; |
| 337 | } |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static void mwifiex_usb_free(struct usb_card_rec *card) |
| 343 | { |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 344 | struct usb_tx_data_port *port; |
| 345 | int i, j; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 346 | |
| 347 | if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb) |
| 348 | usb_kill_urb(card->rx_cmd.urb); |
| 349 | |
| 350 | usb_free_urb(card->rx_cmd.urb); |
| 351 | card->rx_cmd.urb = NULL; |
| 352 | |
| 353 | if (atomic_read(&card->rx_data_urb_pending)) |
| 354 | for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) |
| 355 | if (card->rx_data_list[i].urb) |
| 356 | usb_kill_urb(card->rx_data_list[i].urb); |
| 357 | |
| 358 | for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { |
| 359 | usb_free_urb(card->rx_data_list[i].urb); |
| 360 | card->rx_data_list[i].urb = NULL; |
| 361 | } |
| 362 | |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 363 | for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { |
| 364 | port = &card->port[i]; |
| 365 | for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { |
| 366 | usb_free_urb(port->tx_data_list[j].urb); |
| 367 | port->tx_data_list[j].urb = NULL; |
| 368 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | usb_free_urb(card->tx_cmd.urb); |
| 372 | card->tx_cmd.urb = NULL; |
| 373 | |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | /* This function probes an mwifiex device and registers it. It allocates |
| 378 | * the card structure, initiates the device registration and initialization |
| 379 | * procedure by adding a logical interface. |
| 380 | */ |
| 381 | static int mwifiex_usb_probe(struct usb_interface *intf, |
| 382 | const struct usb_device_id *id) |
| 383 | { |
| 384 | struct usb_device *udev = interface_to_usbdev(intf); |
| 385 | struct usb_host_interface *iface_desc = intf->cur_altsetting; |
| 386 | struct usb_endpoint_descriptor *epd; |
| 387 | int ret, i; |
| 388 | struct usb_card_rec *card; |
Kirtika Ruchandani | 4133828 | 2016-11-23 17:27:10 -0800 | [diff] [blame] | 389 | u16 id_vendor, id_product, bcd_device; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 390 | |
Brian Norris | 66b9c18 | 2016-10-26 16:29:20 -0700 | [diff] [blame] | 391 | card = devm_kzalloc(&intf->dev, sizeof(*card), GFP_KERNEL); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 392 | if (!card) |
| 393 | return -ENOMEM; |
| 394 | |
Brian Norris | 4a79aa1 | 2016-11-18 19:30:26 +0530 | [diff] [blame] | 395 | init_completion(&card->fw_done); |
| 396 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 397 | id_vendor = le16_to_cpu(udev->descriptor.idVendor); |
| 398 | id_product = le16_to_cpu(udev->descriptor.idProduct); |
| 399 | bcd_device = le16_to_cpu(udev->descriptor.bcdDevice); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 400 | pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n", |
| 401 | id_vendor, id_product, bcd_device); |
| 402 | |
| 403 | /* PID_1 is used for firmware downloading only */ |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 404 | switch (id_product) { |
Kristian Evensen | 9e6f3f4 | 2014-11-20 16:52:58 +0100 | [diff] [blame] | 405 | case USB8766_PID_1: |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 406 | case USB8797_PID_1: |
Yogesh Ashok Powar | eaa3d9f | 2015-01-23 17:09:18 +0530 | [diff] [blame] | 407 | case USB8801_PID_1: |
Zhaoyang Liu | 6d85ef0 | 2015-08-05 06:09:40 -0700 | [diff] [blame] | 408 | case USB8997_PID_1: |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 409 | card->usb_boot_state = USB8XXX_FW_DNLD; |
| 410 | break; |
Kristian Evensen | 9e6f3f4 | 2014-11-20 16:52:58 +0100 | [diff] [blame] | 411 | case USB8766_PID_2: |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 412 | case USB8797_PID_2: |
Yogesh Ashok Powar | eaa3d9f | 2015-01-23 17:09:18 +0530 | [diff] [blame] | 413 | case USB8801_PID_2: |
Zhaoyang Liu | 6d85ef0 | 2015-08-05 06:09:40 -0700 | [diff] [blame] | 414 | case USB8997_PID_2: |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 415 | card->usb_boot_state = USB8XXX_FW_READY; |
| 416 | break; |
| 417 | default: |
Joe Perches | fe3881c | 2014-09-09 20:27:44 -0700 | [diff] [blame] | 418 | pr_warn("unknown id_product %#x\n", id_product); |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 419 | card->usb_boot_state = USB8XXX_FW_DNLD; |
| 420 | break; |
| 421 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 422 | |
| 423 | card->udev = udev; |
| 424 | card->intf = intf; |
| 425 | |
Masanari Iida | 6979783 | 2012-11-30 22:08:31 +0900 | [diff] [blame] | 426 | pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n", |
Johan Hovold | a1ad719 | 2017-05-12 12:15:55 +0200 | [diff] [blame^] | 427 | le16_to_cpu(udev->descriptor.bcdUSB), |
| 428 | udev->descriptor.bDeviceClass, |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 429 | udev->descriptor.bDeviceSubClass, |
| 430 | udev->descriptor.bDeviceProtocol); |
| 431 | |
| 432 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 433 | epd = &iface_desc->endpoint[i].desc; |
| 434 | if (usb_endpoint_dir_in(epd) && |
| 435 | usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT && |
Ganapathi Bhat | 182f569 | 2017-03-27 18:27:44 +0530 | [diff] [blame] | 436 | (usb_endpoint_xfer_bulk(epd) || |
| 437 | usb_endpoint_xfer_int(epd))) { |
| 438 | card->rx_cmd_ep_type = usb_endpoint_type(epd); |
| 439 | card->rx_cmd_interval = epd->bInterval; |
| 440 | pr_debug("info: Rx CMD/EVT:: max pkt size: %d, addr: %d, ep_type: %d\n", |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 441 | le16_to_cpu(epd->wMaxPacketSize), |
Ganapathi Bhat | 182f569 | 2017-03-27 18:27:44 +0530 | [diff] [blame] | 442 | epd->bEndpointAddress, card->rx_cmd_ep_type); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 443 | card->rx_cmd_ep = usb_endpoint_num(epd); |
| 444 | atomic_set(&card->rx_cmd_urb_pending, 0); |
| 445 | } |
| 446 | if (usb_endpoint_dir_in(epd) && |
| 447 | usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA && |
| 448 | usb_endpoint_xfer_bulk(epd)) { |
| 449 | pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n", |
| 450 | le16_to_cpu(epd->wMaxPacketSize), |
| 451 | epd->bEndpointAddress); |
| 452 | card->rx_data_ep = usb_endpoint_num(epd); |
| 453 | atomic_set(&card->rx_data_urb_pending, 0); |
| 454 | } |
| 455 | if (usb_endpoint_dir_out(epd) && |
| 456 | usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA && |
| 457 | usb_endpoint_xfer_bulk(epd)) { |
| 458 | pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n", |
| 459 | le16_to_cpu(epd->wMaxPacketSize), |
| 460 | epd->bEndpointAddress); |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 461 | card->port[0].tx_data_ep = usb_endpoint_num(epd); |
| 462 | atomic_set(&card->port[0].tx_data_urb_pending, 0); |
| 463 | } |
| 464 | if (usb_endpoint_dir_out(epd) && |
| 465 | usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA_CH2 && |
| 466 | usb_endpoint_xfer_bulk(epd)) { |
| 467 | pr_debug("info: bulk OUT chan2:\t" |
| 468 | "max pkt size: %d, addr: %d\n", |
| 469 | le16_to_cpu(epd->wMaxPacketSize), |
| 470 | epd->bEndpointAddress); |
| 471 | card->port[1].tx_data_ep = usb_endpoint_num(epd); |
| 472 | atomic_set(&card->port[1].tx_data_urb_pending, 0); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 473 | } |
| 474 | if (usb_endpoint_dir_out(epd) && |
| 475 | usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT && |
Ganapathi Bhat | 182f569 | 2017-03-27 18:27:44 +0530 | [diff] [blame] | 476 | (usb_endpoint_xfer_bulk(epd) || |
| 477 | usb_endpoint_xfer_int(epd))) { |
| 478 | card->tx_cmd_ep_type = usb_endpoint_type(epd); |
| 479 | card->tx_cmd_interval = epd->bInterval; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 480 | pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n", |
| 481 | le16_to_cpu(epd->wMaxPacketSize), |
| 482 | epd->bEndpointAddress); |
Ganapathi Bhat | 182f569 | 2017-03-27 18:27:44 +0530 | [diff] [blame] | 483 | pr_debug("info: Tx CMD:: max pkt size: %d, addr: %d, ep_type: %d\n", |
| 484 | le16_to_cpu(epd->wMaxPacketSize), |
| 485 | epd->bEndpointAddress, card->tx_cmd_ep_type); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 486 | card->tx_cmd_ep = usb_endpoint_num(epd); |
| 487 | atomic_set(&card->tx_cmd_urb_pending, 0); |
| 488 | card->bulk_out_maxpktsize = |
| 489 | le16_to_cpu(epd->wMaxPacketSize); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | usb_set_intfdata(intf, card); |
| 494 | |
Brian Norris | 4a79aa1 | 2016-11-18 19:30:26 +0530 | [diff] [blame] | 495 | ret = mwifiex_add_card(card, &card->fw_done, &usb_ops, |
Rajat Jain | 2e02b58 | 2016-11-15 19:06:02 +0530 | [diff] [blame] | 496 | MWIFIEX_USB, &card->udev->dev); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 497 | if (ret) { |
| 498 | pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret); |
| 499 | usb_reset_device(udev); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 500 | return ret; |
| 501 | } |
| 502 | |
| 503 | usb_get_dev(udev); |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | /* Kernel needs to suspend all functions separately. Therefore all |
| 509 | * registered functions must have drivers with suspend and resume |
| 510 | * methods. Failing that the kernel simply removes the whole card. |
| 511 | * |
| 512 | * If already not suspended, this function allocates and sends a |
| 513 | * 'host sleep activate' request to the firmware and turns off the traffic. |
| 514 | */ |
| 515 | static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message) |
| 516 | { |
| 517 | struct usb_card_rec *card = usb_get_intfdata(intf); |
| 518 | struct mwifiex_adapter *adapter; |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 519 | struct usb_tx_data_port *port; |
| 520 | int i, j; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 521 | |
Brian Norris | b42dbb2 | 2016-11-18 19:30:29 +0530 | [diff] [blame] | 522 | /* Might still be loading firmware */ |
| 523 | wait_for_completion(&card->fw_done); |
| 524 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 525 | adapter = card->adapter; |
Brian Norris | b42dbb2 | 2016-11-18 19:30:29 +0530 | [diff] [blame] | 526 | if (!adapter) { |
| 527 | dev_err(&intf->dev, "card is not valid\n"); |
| 528 | return 0; |
| 529 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 530 | |
| 531 | if (unlikely(adapter->is_suspended)) |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 532 | mwifiex_dbg(adapter, WARN, |
| 533 | "Device already suspended\n"); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 534 | |
Brian Norris | 58b7033 | 2016-11-18 19:30:31 +0530 | [diff] [blame] | 535 | /* Enable the Host Sleep */ |
| 536 | if (!mwifiex_enable_hs(adapter)) { |
| 537 | mwifiex_dbg(adapter, ERROR, |
| 538 | "cmd: failed to suspend\n"); |
| 539 | adapter->hs_enabling = false; |
| 540 | return -EFAULT; |
| 541 | } |
| 542 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 543 | |
| 544 | /* 'is_suspended' flag indicates device is suspended. |
| 545 | * It must be set here before the usb_kill_urb() calls. Reason |
| 546 | * is in the complete handlers, urb->status(= -ENOENT) and |
| 547 | * this flag is used in combination to distinguish between a |
| 548 | * 'suspended' state and a 'disconnect' one. |
| 549 | */ |
| 550 | adapter->is_suspended = true; |
Amitkumar Karwar | c0dbba6 | 2014-03-25 19:01:20 -0700 | [diff] [blame] | 551 | adapter->hs_enabling = false; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 552 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 553 | if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb) |
| 554 | usb_kill_urb(card->rx_cmd.urb); |
| 555 | |
| 556 | if (atomic_read(&card->rx_data_urb_pending)) |
| 557 | for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) |
| 558 | if (card->rx_data_list[i].urb) |
| 559 | usb_kill_urb(card->rx_data_list[i].urb); |
| 560 | |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 561 | for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { |
| 562 | port = &card->port[i]; |
| 563 | for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { |
| 564 | if (port->tx_data_list[j].urb) |
| 565 | usb_kill_urb(port->tx_data_list[j].urb); |
| 566 | } |
| 567 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 568 | |
| 569 | if (card->tx_cmd.urb) |
| 570 | usb_kill_urb(card->tx_cmd.urb); |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | /* Kernel needs to suspend all functions separately. Therefore all |
| 576 | * registered functions must have drivers with suspend and resume |
| 577 | * methods. Failing that the kernel simply removes the whole card. |
| 578 | * |
| 579 | * If already not resumed, this function turns on the traffic and |
| 580 | * sends a 'host sleep cancel' request to the firmware. |
| 581 | */ |
| 582 | static int mwifiex_usb_resume(struct usb_interface *intf) |
| 583 | { |
| 584 | struct usb_card_rec *card = usb_get_intfdata(intf); |
| 585 | struct mwifiex_adapter *adapter; |
| 586 | int i; |
| 587 | |
Brian Norris | e98fb11 | 2016-11-18 19:30:33 +0530 | [diff] [blame] | 588 | if (!card->adapter) { |
| 589 | dev_err(&intf->dev, "%s: card->adapter is NULL\n", |
| 590 | __func__); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 591 | return 0; |
| 592 | } |
| 593 | adapter = card->adapter; |
| 594 | |
| 595 | if (unlikely(!adapter->is_suspended)) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 596 | mwifiex_dbg(adapter, WARN, |
| 597 | "Device already resumed\n"); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* Indicate device resumed. The netdev queue will be resumed only |
| 602 | * after the urbs have been re-submitted |
| 603 | */ |
| 604 | adapter->is_suspended = false; |
| 605 | |
| 606 | if (!atomic_read(&card->rx_data_urb_pending)) |
| 607 | for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) |
| 608 | mwifiex_usb_submit_rx_urb(&card->rx_data_list[i], |
| 609 | MWIFIEX_RX_DATA_BUF_SIZE); |
| 610 | |
| 611 | if (!atomic_read(&card->rx_cmd_urb_pending)) { |
| 612 | card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE); |
| 613 | if (card->rx_cmd.skb) |
| 614 | mwifiex_usb_submit_rx_urb(&card->rx_cmd, |
| 615 | MWIFIEX_RX_CMD_BUF_SIZE); |
| 616 | } |
| 617 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 618 | /* Disable Host Sleep */ |
| 619 | if (adapter->hs_activated) |
| 620 | mwifiex_cancel_hs(mwifiex_get_priv(adapter, |
| 621 | MWIFIEX_BSS_ROLE_ANY), |
| 622 | MWIFIEX_ASYNC_CMD); |
| 623 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static void mwifiex_usb_disconnect(struct usb_interface *intf) |
| 628 | { |
| 629 | struct usb_card_rec *card = usb_get_intfdata(intf); |
Amitkumar Karwar | 3fffd7c | 2014-04-14 15:32:54 -0700 | [diff] [blame] | 630 | struct mwifiex_adapter *adapter; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 631 | |
Brian Norris | 4a79aa1 | 2016-11-18 19:30:26 +0530 | [diff] [blame] | 632 | wait_for_completion(&card->fw_done); |
| 633 | |
Amitkumar Karwar | 3fffd7c | 2014-04-14 15:32:54 -0700 | [diff] [blame] | 634 | adapter = card->adapter; |
Brian Norris | 4a79aa1 | 2016-11-18 19:30:26 +0530 | [diff] [blame] | 635 | if (!adapter || !adapter->priv_num) |
Amitkumar Karwar | 3fffd7c | 2014-04-14 15:32:54 -0700 | [diff] [blame] | 636 | return; |
| 637 | |
Xinming Hu | 045f0c1 | 2016-11-30 20:22:17 +0530 | [diff] [blame] | 638 | if (card->udev->state != USB_STATE_NOTATTACHED && !adapter->mfg_mode) { |
Amitkumar Karwar | 3fffd7c | 2014-04-14 15:32:54 -0700 | [diff] [blame] | 639 | mwifiex_deauthenticate_all(adapter); |
| 640 | |
| 641 | mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter, |
| 642 | MWIFIEX_BSS_ROLE_ANY), |
| 643 | MWIFIEX_FUNC_SHUTDOWN); |
| 644 | } |
| 645 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 646 | mwifiex_usb_free(card); |
| 647 | |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 648 | mwifiex_dbg(adapter, FATAL, |
| 649 | "%s: removing card\n", __func__); |
Brian Norris | 4a79aa1 | 2016-11-18 19:30:26 +0530 | [diff] [blame] | 650 | mwifiex_remove_card(adapter); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 651 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 652 | usb_put_dev(interface_to_usbdev(intf)); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static struct usb_driver mwifiex_usb_driver = { |
Bing Zhao | e4ceb0f | 2014-02-21 12:23:00 -0800 | [diff] [blame] | 656 | .name = "mwifiex_usb", |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 657 | .probe = mwifiex_usb_probe, |
| 658 | .disconnect = mwifiex_usb_disconnect, |
| 659 | .id_table = mwifiex_usb_table, |
| 660 | .suspend = mwifiex_usb_suspend, |
| 661 | .resume = mwifiex_usb_resume, |
Amitkumar Karwar | 3fffd7c | 2014-04-14 15:32:54 -0700 | [diff] [blame] | 662 | .soft_unbind = 1, |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 663 | }; |
| 664 | |
| 665 | static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter) |
| 666 | { |
| 667 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 668 | struct usb_tx_data_port *port; |
| 669 | int i, j; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 670 | |
| 671 | card->tx_cmd.adapter = adapter; |
| 672 | card->tx_cmd.ep = card->tx_cmd_ep; |
| 673 | |
| 674 | card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL); |
Wolfram Sang | dbea99d | 2016-08-11 23:13:07 +0200 | [diff] [blame] | 675 | if (!card->tx_cmd.urb) |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 676 | return -ENOMEM; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 677 | |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 678 | for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { |
| 679 | port = &card->port[i]; |
| 680 | if (!port->tx_data_ep) |
| 681 | continue; |
| 682 | port->tx_data_ix = 0; |
Zhaoyang Liu | 7e4e5d2 | 2015-09-18 06:32:17 -0700 | [diff] [blame] | 683 | if (port->tx_data_ep == MWIFIEX_USB_EP_DATA) |
| 684 | port->block_status = false; |
| 685 | else |
| 686 | port->block_status = true; |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 687 | for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { |
| 688 | port->tx_data_list[j].adapter = adapter; |
| 689 | port->tx_data_list[j].ep = port->tx_data_ep; |
| 690 | port->tx_data_list[j].urb = |
| 691 | usb_alloc_urb(0, GFP_KERNEL); |
Wolfram Sang | dbea99d | 2016-08-11 23:13:07 +0200 | [diff] [blame] | 692 | if (!port->tx_data_list[j].urb) |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 693 | return -ENOMEM; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter) |
| 701 | { |
| 702 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 703 | int i; |
| 704 | |
| 705 | card->rx_cmd.adapter = adapter; |
| 706 | card->rx_cmd.ep = card->rx_cmd_ep; |
| 707 | |
| 708 | card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL); |
Wolfram Sang | dbea99d | 2016-08-11 23:13:07 +0200 | [diff] [blame] | 709 | if (!card->rx_cmd.urb) |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 710 | return -ENOMEM; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 711 | |
| 712 | card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE); |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 713 | if (!card->rx_cmd.skb) |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 714 | return -ENOMEM; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 715 | |
| 716 | if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE)) |
| 717 | return -1; |
| 718 | |
| 719 | for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { |
| 720 | card->rx_data_list[i].adapter = adapter; |
| 721 | card->rx_data_list[i].ep = card->rx_data_ep; |
| 722 | |
| 723 | card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL); |
Wolfram Sang | dbea99d | 2016-08-11 23:13:07 +0200 | [diff] [blame] | 724 | if (!card->rx_data_list[i].urb) |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 725 | return -1; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 726 | if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i], |
| 727 | MWIFIEX_RX_DATA_BUF_SIZE)) |
| 728 | return -1; |
| 729 | } |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf, |
| 735 | u32 *len, u8 ep, u32 timeout) |
| 736 | { |
| 737 | struct usb_card_rec *card = adapter->card; |
| 738 | int actual_length, ret; |
| 739 | |
| 740 | if (!(*len % card->bulk_out_maxpktsize)) |
| 741 | (*len)++; |
| 742 | |
| 743 | /* Send the data block */ |
| 744 | ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf, |
| 745 | *len, &actual_length, timeout); |
| 746 | if (ret) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 747 | mwifiex_dbg(adapter, ERROR, |
| 748 | "usb_bulk_msg for tx failed: %d\n", ret); |
Bing Zhao | 5b2e2ec | 2013-01-25 18:23:00 -0800 | [diff] [blame] | 749 | return ret; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | *len = actual_length; |
| 753 | |
| 754 | return ret; |
| 755 | } |
| 756 | |
| 757 | static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf, |
| 758 | u32 *len, u8 ep, u32 timeout) |
| 759 | { |
| 760 | struct usb_card_rec *card = adapter->card; |
| 761 | int actual_length, ret; |
| 762 | |
| 763 | /* Receive the data response */ |
| 764 | ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf, |
| 765 | *len, &actual_length, timeout); |
| 766 | if (ret) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 767 | mwifiex_dbg(adapter, ERROR, |
| 768 | "usb_bulk_msg for rx failed: %d\n", ret); |
Bing Zhao | 5b2e2ec | 2013-01-25 18:23:00 -0800 | [diff] [blame] | 769 | return ret; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | *len = actual_length; |
| 773 | |
| 774 | return ret; |
| 775 | } |
| 776 | |
Zhaoyang Liu | 7e4e5d2 | 2015-09-18 06:32:17 -0700 | [diff] [blame] | 777 | static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter) |
| 778 | { |
| 779 | struct usb_card_rec *card = adapter->card; |
| 780 | u8 active_port = MWIFIEX_USB_EP_DATA; |
| 781 | struct mwifiex_private *priv = NULL; |
| 782 | int i; |
| 783 | |
| 784 | if (adapter->usb_mc_status) { |
| 785 | for (i = 0; i < adapter->priv_num; i++) { |
| 786 | priv = adapter->priv[i]; |
| 787 | if (!priv) |
| 788 | continue; |
| 789 | if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP && |
| 790 | !priv->bss_started) || |
| 791 | (priv->bss_role == MWIFIEX_BSS_ROLE_STA && |
| 792 | !priv->media_connected)) |
| 793 | priv->usb_port = MWIFIEX_USB_EP_DATA; |
| 794 | } |
| 795 | for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) |
| 796 | card->port[i].block_status = false; |
| 797 | } else { |
| 798 | for (i = 0; i < adapter->priv_num; i++) { |
| 799 | priv = adapter->priv[i]; |
| 800 | if (!priv) |
| 801 | continue; |
| 802 | if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP && |
| 803 | priv->bss_started) || |
| 804 | (priv->bss_role == MWIFIEX_BSS_ROLE_STA && |
| 805 | priv->media_connected)) { |
| 806 | active_port = priv->usb_port; |
| 807 | break; |
| 808 | } |
| 809 | } |
| 810 | for (i = 0; i < adapter->priv_num; i++) { |
| 811 | priv = adapter->priv[i]; |
| 812 | if (priv) |
| 813 | priv->usb_port = active_port; |
| 814 | } |
| 815 | for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { |
| 816 | if (active_port == card->port[i].tx_data_ep) |
| 817 | card->port[i].block_status = false; |
| 818 | else |
| 819 | card->port[i].block_status = true; |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
Zhaoyang Liu | 735ab6b | 2015-09-18 06:32:18 -0700 | [diff] [blame] | 824 | static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv) |
| 825 | { |
| 826 | struct usb_card_rec *card = priv->adapter->card; |
| 827 | int idx; |
| 828 | |
| 829 | for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) { |
| 830 | if (priv->usb_port == card->port[idx].tx_data_ep) |
| 831 | return !card->port[idx].block_status; |
| 832 | } |
| 833 | |
| 834 | return false; |
| 835 | } |
| 836 | |
| 837 | static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter) |
| 838 | { |
| 839 | struct usb_card_rec *card = adapter->card; |
| 840 | int i; |
| 841 | |
| 842 | for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) |
| 843 | if (!card->port[i].block_status) |
| 844 | return false; |
| 845 | |
| 846 | return true; |
| 847 | } |
| 848 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 849 | /* This function write a command/data packet to card. */ |
| 850 | static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep, |
| 851 | struct sk_buff *skb, |
| 852 | struct mwifiex_tx_param *tx_param) |
| 853 | { |
| 854 | struct usb_card_rec *card = adapter->card; |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 855 | struct urb_context *context = NULL; |
| 856 | struct usb_tx_data_port *port = NULL; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 857 | u8 *data = (u8 *)skb->data; |
| 858 | struct urb *tx_urb; |
Amitkumar Karwar | ac3b561 | 2016-09-20 20:49:04 +0530 | [diff] [blame] | 859 | int idx, ret = -EINPROGRESS; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 860 | |
| 861 | if (adapter->is_suspended) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 862 | mwifiex_dbg(adapter, ERROR, |
| 863 | "%s: not allowed while suspended\n", __func__); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 864 | return -1; |
| 865 | } |
| 866 | |
| 867 | if (adapter->surprise_removed) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 868 | mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 869 | return -1; |
| 870 | } |
| 871 | |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 872 | mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 873 | |
| 874 | if (ep == card->tx_cmd_ep) { |
| 875 | context = &card->tx_cmd; |
| 876 | } else { |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 877 | for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) { |
| 878 | if (ep == card->port[idx].tx_data_ep) { |
| 879 | port = &card->port[idx]; |
| 880 | if (atomic_read(&port->tx_data_urb_pending) |
| 881 | >= MWIFIEX_TX_DATA_URB) { |
Zhaoyang Liu | 735ab6b | 2015-09-18 06:32:18 -0700 | [diff] [blame] | 882 | port->block_status = true; |
Amitkumar Karwar | ac3b561 | 2016-09-20 20:49:04 +0530 | [diff] [blame] | 883 | adapter->data_sent = |
| 884 | mwifiex_usb_data_sent(adapter); |
| 885 | return -EBUSY; |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 886 | } |
| 887 | if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB) |
| 888 | port->tx_data_ix = 0; |
| 889 | context = |
| 890 | &port->tx_data_list[port->tx_data_ix++]; |
| 891 | break; |
| 892 | } |
| 893 | } |
| 894 | if (!port) { |
| 895 | mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n"); |
| 896 | return -1; |
| 897 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | context->adapter = adapter; |
| 901 | context->ep = ep; |
| 902 | context->skb = skb; |
| 903 | tx_urb = context->urb; |
| 904 | |
Ganapathi Bhat | 182f569 | 2017-03-27 18:27:44 +0530 | [diff] [blame] | 905 | if (ep == card->tx_cmd_ep && |
| 906 | card->tx_cmd_ep_type == USB_ENDPOINT_XFER_INT) |
| 907 | usb_fill_int_urb(tx_urb, card->udev, |
| 908 | usb_sndintpipe(card->udev, ep), data, |
| 909 | skb->len, mwifiex_usb_tx_complete, |
| 910 | (void *)context, card->tx_cmd_interval); |
| 911 | else |
| 912 | usb_fill_bulk_urb(tx_urb, card->udev, |
| 913 | usb_sndbulkpipe(card->udev, ep), data, |
| 914 | skb->len, mwifiex_usb_tx_complete, |
| 915 | (void *)context); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 916 | |
| 917 | tx_urb->transfer_flags |= URB_ZERO_PACKET; |
| 918 | |
| 919 | if (ep == card->tx_cmd_ep) |
| 920 | atomic_inc(&card->tx_cmd_urb_pending); |
| 921 | else |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 922 | atomic_inc(&port->tx_data_urb_pending); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 923 | |
Cathy Luo | 5476f80 | 2016-09-20 20:49:03 +0530 | [diff] [blame] | 924 | if (ep != card->tx_cmd_ep && |
| 925 | atomic_read(&port->tx_data_urb_pending) == |
| 926 | MWIFIEX_TX_DATA_URB) { |
| 927 | port->block_status = true; |
| 928 | adapter->data_sent = mwifiex_usb_data_sent(adapter); |
Amitkumar Karwar | ac3b561 | 2016-09-20 20:49:04 +0530 | [diff] [blame] | 929 | ret = -ENOSR; |
Cathy Luo | 5476f80 | 2016-09-20 20:49:03 +0530 | [diff] [blame] | 930 | } |
| 931 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 932 | if (usb_submit_urb(tx_urb, GFP_ATOMIC)) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 933 | mwifiex_dbg(adapter, ERROR, |
| 934 | "%s: usb_submit_urb failed\n", __func__); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 935 | if (ep == card->tx_cmd_ep) { |
| 936 | atomic_dec(&card->tx_cmd_urb_pending); |
| 937 | } else { |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 938 | atomic_dec(&port->tx_data_urb_pending); |
Zhaoyang Liu | 735ab6b | 2015-09-18 06:32:18 -0700 | [diff] [blame] | 939 | port->block_status = false; |
Cathy Luo | 5476f80 | 2016-09-20 20:49:03 +0530 | [diff] [blame] | 940 | adapter->data_sent = false; |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 941 | if (port->tx_data_ix) |
| 942 | port->tx_data_ix--; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 943 | else |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 944 | port->tx_data_ix = MWIFIEX_TX_DATA_URB; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 945 | } |
Amitkumar Karwar | ac3b561 | 2016-09-20 20:49:04 +0530 | [diff] [blame] | 946 | ret = -1; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Zhaoyang Liu | 735ab6b | 2015-09-18 06:32:18 -0700 | [diff] [blame] | 949 | return ret; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /* This function register usb device and initialize parameter. */ |
| 953 | static int mwifiex_register_dev(struct mwifiex_adapter *adapter) |
| 954 | { |
| 955 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 956 | |
| 957 | card->adapter = adapter; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 958 | |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 959 | switch (le16_to_cpu(card->udev->descriptor.idProduct)) { |
Zhaoyang Liu | 6d85ef0 | 2015-08-05 06:09:40 -0700 | [diff] [blame] | 960 | case USB8997_PID_1: |
| 961 | case USB8997_PID_2: |
| 962 | adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K; |
| 963 | strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME); |
| 964 | adapter->ext_scan = true; |
| 965 | break; |
Kristian Evensen | 9e6f3f4 | 2014-11-20 16:52:58 +0100 | [diff] [blame] | 966 | case USB8766_PID_1: |
| 967 | case USB8766_PID_2: |
| 968 | adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; |
| 969 | strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME); |
Avinash Patil | 1fe192d | 2015-01-23 17:09:19 +0530 | [diff] [blame] | 970 | adapter->ext_scan = true; |
Kristian Evensen | 9e6f3f4 | 2014-11-20 16:52:58 +0100 | [diff] [blame] | 971 | break; |
Yogesh Ashok Powar | eaa3d9f | 2015-01-23 17:09:18 +0530 | [diff] [blame] | 972 | case USB8801_PID_1: |
| 973 | case USB8801_PID_2: |
| 974 | adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; |
| 975 | strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME); |
Avinash Patil | 1fe192d | 2015-01-23 17:09:19 +0530 | [diff] [blame] | 976 | adapter->ext_scan = false; |
Yogesh Ashok Powar | eaa3d9f | 2015-01-23 17:09:18 +0530 | [diff] [blame] | 977 | break; |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 978 | case USB8797_PID_1: |
| 979 | case USB8797_PID_2: |
| 980 | default: |
Amitkumar Karwar | 828cf22 | 2014-02-27 19:35:13 -0800 | [diff] [blame] | 981 | adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 982 | strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME); |
| 983 | break; |
| 984 | } |
| 985 | |
Zhaoyang Liu | 2b0f997 | 2015-09-18 06:32:15 -0700 | [diff] [blame] | 986 | adapter->usb_mc_status = false; |
Zhaoyang Liu | 7e4e5d2 | 2015-09-18 06:32:17 -0700 | [diff] [blame] | 987 | adapter->usb_mc_setup = false; |
Zhaoyang Liu | 2b0f997 | 2015-09-18 06:32:15 -0700 | [diff] [blame] | 988 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 989 | return 0; |
| 990 | } |
| 991 | |
Amitkumar Karwar | 2739a95 | 2013-07-22 19:17:51 -0700 | [diff] [blame] | 992 | static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter) |
| 993 | { |
| 994 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 995 | |
Ujjal Roy | 353d2a6 | 2013-11-14 19:10:42 -0800 | [diff] [blame] | 996 | card->adapter = NULL; |
Amitkumar Karwar | 2739a95 | 2013-07-22 19:17:51 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 999 | static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter, |
| 1000 | struct mwifiex_fw_image *fw) |
| 1001 | { |
| 1002 | int ret = 0; |
| 1003 | u8 *firmware = fw->fw_buf, *recv_buff; |
Dan Carpenter | 394f0ed | 2016-04-19 07:23:44 -0700 | [diff] [blame] | 1004 | u32 retries = USB8XXX_FW_MAX_RETRY + 1; |
| 1005 | u32 dlen; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1006 | u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0; |
| 1007 | struct fw_data *fwdata; |
| 1008 | struct fw_sync_header sync_fw; |
| 1009 | u8 check_winner = 1; |
| 1010 | |
| 1011 | if (!firmware) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1012 | mwifiex_dbg(adapter, ERROR, |
| 1013 | "No firmware image found! Terminating download\n"); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1014 | ret = -1; |
| 1015 | goto fw_exit; |
| 1016 | } |
| 1017 | |
| 1018 | /* Allocate memory for transmit */ |
| 1019 | fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL); |
Dan Carpenter | 3b1f0e8 | 2015-08-04 10:49:27 +0300 | [diff] [blame] | 1020 | if (!fwdata) { |
| 1021 | ret = -ENOMEM; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1022 | goto fw_exit; |
Dan Carpenter | 3b1f0e8 | 2015-08-04 10:49:27 +0300 | [diff] [blame] | 1023 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1024 | |
| 1025 | /* Allocate memory for receive */ |
| 1026 | recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL); |
Dan Carpenter | e0bdef0 | 2016-04-19 07:21:58 -0700 | [diff] [blame] | 1027 | if (!recv_buff) { |
| 1028 | ret = -ENOMEM; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1029 | goto cleanup; |
Dan Carpenter | e0bdef0 | 2016-04-19 07:21:58 -0700 | [diff] [blame] | 1030 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1031 | |
| 1032 | do { |
| 1033 | /* Send pseudo data to check winner status first */ |
| 1034 | if (check_winner) { |
| 1035 | memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header)); |
| 1036 | dlen = 0; |
| 1037 | } else { |
| 1038 | /* copy the header of the fw_data to get the length */ |
Sasha Levin | fc40ca9 | 2013-01-02 16:23:01 -0800 | [diff] [blame] | 1039 | memcpy(&fwdata->fw_hdr, &firmware[tlen], |
| 1040 | sizeof(struct fw_header)); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1041 | |
| 1042 | dlen = le32_to_cpu(fwdata->fw_hdr.data_len); |
| 1043 | dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd); |
| 1044 | tlen += sizeof(struct fw_header); |
| 1045 | |
Ganapathi Bhat | 7877646 | 2016-09-12 18:55:27 +0530 | [diff] [blame] | 1046 | /* Command 7 doesn't have data length field */ |
| 1047 | if (dnld_cmd == FW_CMD_7) |
| 1048 | dlen = 0; |
| 1049 | |
Sasha Levin | fc40ca9 | 2013-01-02 16:23:01 -0800 | [diff] [blame] | 1050 | memcpy(fwdata->data, &firmware[tlen], dlen); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1051 | |
| 1052 | fwdata->seq_num = cpu_to_le32(fw_seqnum); |
| 1053 | tlen += dlen; |
| 1054 | } |
| 1055 | |
| 1056 | /* If the send/receive fails or CRC occurs then retry */ |
Dan Carpenter | 394f0ed | 2016-04-19 07:23:44 -0700 | [diff] [blame] | 1057 | while (--retries) { |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1058 | u8 *buf = (u8 *)fwdata; |
| 1059 | u32 len = FW_DATA_XMIT_SIZE; |
| 1060 | |
| 1061 | /* send the firmware block */ |
| 1062 | ret = mwifiex_write_data_sync(adapter, buf, &len, |
| 1063 | MWIFIEX_USB_EP_CMD_EVENT, |
| 1064 | MWIFIEX_USB_TIMEOUT); |
| 1065 | if (ret) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1066 | mwifiex_dbg(adapter, ERROR, |
| 1067 | "write_data_sync: failed: %d\n", |
| 1068 | ret); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1069 | continue; |
| 1070 | } |
| 1071 | |
| 1072 | buf = recv_buff; |
| 1073 | len = FW_DNLD_RX_BUF_SIZE; |
| 1074 | |
| 1075 | /* Receive the firmware block response */ |
| 1076 | ret = mwifiex_read_data_sync(adapter, buf, &len, |
| 1077 | MWIFIEX_USB_EP_CMD_EVENT, |
| 1078 | MWIFIEX_USB_TIMEOUT); |
| 1079 | if (ret) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1080 | mwifiex_dbg(adapter, ERROR, |
| 1081 | "read_data_sync: failed: %d\n", |
| 1082 | ret); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1083 | continue; |
| 1084 | } |
| 1085 | |
| 1086 | memcpy(&sync_fw, recv_buff, |
| 1087 | sizeof(struct fw_sync_header)); |
| 1088 | |
| 1089 | /* check 1st firmware block resp for highest bit set */ |
| 1090 | if (check_winner) { |
| 1091 | if (le32_to_cpu(sync_fw.cmd) & 0x80000000) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1092 | mwifiex_dbg(adapter, WARN, |
| 1093 | "USB is not the winner %#x\n", |
| 1094 | sync_fw.cmd); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1095 | |
| 1096 | /* returning success */ |
| 1097 | ret = 0; |
| 1098 | goto cleanup; |
| 1099 | } |
| 1100 | |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1101 | mwifiex_dbg(adapter, MSG, |
| 1102 | "start to download FW...\n"); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1103 | |
| 1104 | check_winner = 0; |
| 1105 | break; |
| 1106 | } |
| 1107 | |
| 1108 | /* check the firmware block response for CRC errors */ |
| 1109 | if (sync_fw.cmd) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1110 | mwifiex_dbg(adapter, ERROR, |
| 1111 | "FW received block with CRC %#x\n", |
| 1112 | sync_fw.cmd); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1113 | ret = -1; |
| 1114 | continue; |
| 1115 | } |
| 1116 | |
Dan Carpenter | 394f0ed | 2016-04-19 07:23:44 -0700 | [diff] [blame] | 1117 | retries = USB8XXX_FW_MAX_RETRY + 1; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1118 | break; |
| 1119 | } |
| 1120 | fw_seqnum++; |
| 1121 | } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries); |
| 1122 | |
| 1123 | cleanup: |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1124 | mwifiex_dbg(adapter, MSG, |
| 1125 | "info: FW download over, size %d bytes\n", tlen); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1126 | |
| 1127 | kfree(recv_buff); |
| 1128 | kfree(fwdata); |
| 1129 | |
| 1130 | if (retries) |
| 1131 | ret = 0; |
| 1132 | fw_exit: |
| 1133 | return ret; |
| 1134 | } |
| 1135 | |
| 1136 | static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter, |
| 1137 | struct mwifiex_fw_image *fw) |
| 1138 | { |
| 1139 | int ret; |
| 1140 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 1141 | |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 1142 | if (card->usb_boot_state == USB8XXX_FW_DNLD) { |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1143 | ret = mwifiex_prog_fw_w_helper(adapter, fw); |
| 1144 | if (ret) |
| 1145 | return -1; |
| 1146 | |
| 1147 | /* Boot state changes after successful firmware download */ |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 1148 | if (card->usb_boot_state == USB8XXX_FW_DNLD) |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1149 | return -1; |
| 1150 | } |
| 1151 | |
| 1152 | ret = mwifiex_usb_rx_init(adapter); |
| 1153 | if (!ret) |
| 1154 | ret = mwifiex_usb_tx_init(adapter); |
| 1155 | |
| 1156 | return ret; |
| 1157 | } |
| 1158 | |
| 1159 | static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep) |
| 1160 | { |
| 1161 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 1162 | |
| 1163 | skb_push(card->rx_cmd.skb, INTF_HEADER_LEN); |
| 1164 | if ((ep == card->rx_cmd_ep) && |
| 1165 | (!atomic_read(&card->rx_cmd_urb_pending))) |
| 1166 | mwifiex_usb_submit_rx_urb(&card->rx_cmd, |
| 1167 | MWIFIEX_RX_CMD_BUF_SIZE); |
| 1168 | |
| 1169 | return; |
| 1170 | } |
| 1171 | |
| 1172 | static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter, |
| 1173 | struct sk_buff *skb) |
| 1174 | { |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1175 | mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT); |
| 1176 | |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1180 | /* This function wakes up the card. */ |
| 1181 | static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter) |
| 1182 | { |
| 1183 | /* Simulation of HS_AWAKE event */ |
| 1184 | adapter->pm_wakeup_fw_try = false; |
Amitkumar Karwar | 6e9344f | 2015-03-12 00:38:40 -0700 | [diff] [blame] | 1185 | del_timer(&adapter->wakeup_timer); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1186 | adapter->pm_wakeup_card_req = false; |
| 1187 | adapter->ps_state = PS_STATE_AWAKE; |
| 1188 | |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
Amitkumar Karwar | cf6a64f | 2014-11-05 17:04:29 +0530 | [diff] [blame] | 1192 | static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter) |
| 1193 | { |
| 1194 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 1195 | int i; |
| 1196 | struct urb_context *ctx; |
| 1197 | |
| 1198 | for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { |
| 1199 | if (card->rx_data_list[i].skb) |
| 1200 | continue; |
| 1201 | ctx = &card->rx_data_list[i]; |
| 1202 | mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE); |
| 1203 | } |
| 1204 | } |
| 1205 | |
Xinming Hu | 6175430 | 2014-12-23 19:14:08 +0530 | [diff] [blame] | 1206 | /* This function is called after the card has woken up. */ |
| 1207 | static inline int |
| 1208 | mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter) |
| 1209 | { |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1213 | static struct mwifiex_if_ops usb_ops = { |
| 1214 | .register_dev = mwifiex_register_dev, |
Amitkumar Karwar | 2739a95 | 2013-07-22 19:17:51 -0700 | [diff] [blame] | 1215 | .unregister_dev = mwifiex_unregister_dev, |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1216 | .wakeup = mwifiex_pm_wakeup_card, |
| 1217 | .wakeup_complete = mwifiex_pm_wakeup_card_complete, |
| 1218 | |
| 1219 | /* USB specific */ |
| 1220 | .dnld_fw = mwifiex_usb_dnld_fw, |
| 1221 | .cmdrsp_complete = mwifiex_usb_cmd_event_complete, |
| 1222 | .event_complete = mwifiex_usb_cmd_event_complete, |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1223 | .host_to_card = mwifiex_usb_host_to_card, |
Amitkumar Karwar | cf6a64f | 2014-11-05 17:04:29 +0530 | [diff] [blame] | 1224 | .submit_rem_rx_urbs = mwifiex_usb_submit_rem_rx_urbs, |
Zhaoyang Liu | 7e4e5d2 | 2015-09-18 06:32:17 -0700 | [diff] [blame] | 1225 | .multi_port_resync = mwifiex_usb_port_resync, |
Zhaoyang Liu | 735ab6b | 2015-09-18 06:32:18 -0700 | [diff] [blame] | 1226 | .is_port_ready = mwifiex_usb_is_port_ready, |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1227 | }; |
| 1228 | |
Amitkumar Karwar | c0e6aa4 | 2016-12-01 18:38:20 +0530 | [diff] [blame] | 1229 | module_usb_driver(mwifiex_usb_driver); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1230 | |
| 1231 | MODULE_AUTHOR("Marvell International Ltd."); |
| 1232 | MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION); |
| 1233 | MODULE_VERSION(USB_VERSION); |
| 1234 | MODULE_LICENSE("GPL v2"); |
Kristian Evensen | 9e6f3f4 | 2014-11-20 16:52:58 +0100 | [diff] [blame] | 1235 | MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME); |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 1236 | MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME); |
Yogesh Ashok Powar | eaa3d9f | 2015-01-23 17:09:18 +0530 | [diff] [blame] | 1237 | MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME); |
Zhaoyang Liu | 6d85ef0 | 2015-08-05 06:09:40 -0700 | [diff] [blame] | 1238 | MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME); |