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 | |
Arvind Yadav | 7516dbd | 2017-08-09 21:57:59 +0530 | [diff] [blame] | 27 | static const 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++) { |
Xinming Hu | 42d1abb | 2017-05-23 07:12:31 +0000 | [diff] [blame] | 366 | usb_kill_urb(port->tx_data_list[j].urb); |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 367 | usb_free_urb(port->tx_data_list[j].urb); |
| 368 | port->tx_data_list[j].urb = NULL; |
| 369 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | usb_free_urb(card->tx_cmd.urb); |
| 373 | card->tx_cmd.urb = NULL; |
| 374 | |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | /* This function probes an mwifiex device and registers it. It allocates |
| 379 | * the card structure, initiates the device registration and initialization |
| 380 | * procedure by adding a logical interface. |
| 381 | */ |
| 382 | static int mwifiex_usb_probe(struct usb_interface *intf, |
| 383 | const struct usb_device_id *id) |
| 384 | { |
| 385 | struct usb_device *udev = interface_to_usbdev(intf); |
| 386 | struct usb_host_interface *iface_desc = intf->cur_altsetting; |
| 387 | struct usb_endpoint_descriptor *epd; |
| 388 | int ret, i; |
| 389 | struct usb_card_rec *card; |
Kirtika Ruchandani | 4133828 | 2016-11-23 17:27:10 -0800 | [diff] [blame] | 390 | u16 id_vendor, id_product, bcd_device; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 391 | |
Brian Norris | 66b9c18 | 2016-10-26 16:29:20 -0700 | [diff] [blame] | 392 | card = devm_kzalloc(&intf->dev, sizeof(*card), GFP_KERNEL); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 393 | if (!card) |
| 394 | return -ENOMEM; |
| 395 | |
Brian Norris | 4a79aa1 | 2016-11-18 19:30:26 +0530 | [diff] [blame] | 396 | init_completion(&card->fw_done); |
| 397 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 398 | id_vendor = le16_to_cpu(udev->descriptor.idVendor); |
| 399 | id_product = le16_to_cpu(udev->descriptor.idProduct); |
| 400 | bcd_device = le16_to_cpu(udev->descriptor.bcdDevice); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 401 | pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n", |
| 402 | id_vendor, id_product, bcd_device); |
| 403 | |
| 404 | /* PID_1 is used for firmware downloading only */ |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 405 | switch (id_product) { |
Kristian Evensen | 9e6f3f4 | 2014-11-20 16:52:58 +0100 | [diff] [blame] | 406 | case USB8766_PID_1: |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 407 | case USB8797_PID_1: |
Yogesh Ashok Powar | eaa3d9f | 2015-01-23 17:09:18 +0530 | [diff] [blame] | 408 | case USB8801_PID_1: |
Zhaoyang Liu | 6d85ef0 | 2015-08-05 06:09:40 -0700 | [diff] [blame] | 409 | case USB8997_PID_1: |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 410 | card->usb_boot_state = USB8XXX_FW_DNLD; |
| 411 | break; |
Kristian Evensen | 9e6f3f4 | 2014-11-20 16:52:58 +0100 | [diff] [blame] | 412 | case USB8766_PID_2: |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 413 | case USB8797_PID_2: |
Yogesh Ashok Powar | eaa3d9f | 2015-01-23 17:09:18 +0530 | [diff] [blame] | 414 | case USB8801_PID_2: |
Zhaoyang Liu | 6d85ef0 | 2015-08-05 06:09:40 -0700 | [diff] [blame] | 415 | case USB8997_PID_2: |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 416 | card->usb_boot_state = USB8XXX_FW_READY; |
| 417 | break; |
| 418 | default: |
Joe Perches | fe3881c | 2014-09-09 20:27:44 -0700 | [diff] [blame] | 419 | pr_warn("unknown id_product %#x\n", id_product); |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 420 | card->usb_boot_state = USB8XXX_FW_DNLD; |
| 421 | break; |
| 422 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 423 | |
| 424 | card->udev = udev; |
| 425 | card->intf = intf; |
| 426 | |
Masanari Iida | 6979783 | 2012-11-30 22:08:31 +0900 | [diff] [blame] | 427 | 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] | 428 | le16_to_cpu(udev->descriptor.bcdUSB), |
| 429 | udev->descriptor.bDeviceClass, |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 430 | udev->descriptor.bDeviceSubClass, |
| 431 | udev->descriptor.bDeviceProtocol); |
| 432 | |
| 433 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 434 | epd = &iface_desc->endpoint[i].desc; |
| 435 | if (usb_endpoint_dir_in(epd) && |
| 436 | usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT && |
Ganapathi Bhat | 182f569 | 2017-03-27 18:27:44 +0530 | [diff] [blame] | 437 | (usb_endpoint_xfer_bulk(epd) || |
| 438 | usb_endpoint_xfer_int(epd))) { |
| 439 | card->rx_cmd_ep_type = usb_endpoint_type(epd); |
| 440 | card->rx_cmd_interval = epd->bInterval; |
| 441 | 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] | 442 | le16_to_cpu(epd->wMaxPacketSize), |
Ganapathi Bhat | 182f569 | 2017-03-27 18:27:44 +0530 | [diff] [blame] | 443 | epd->bEndpointAddress, card->rx_cmd_ep_type); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 444 | card->rx_cmd_ep = usb_endpoint_num(epd); |
| 445 | atomic_set(&card->rx_cmd_urb_pending, 0); |
| 446 | } |
| 447 | if (usb_endpoint_dir_in(epd) && |
| 448 | usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA && |
| 449 | usb_endpoint_xfer_bulk(epd)) { |
| 450 | pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n", |
| 451 | le16_to_cpu(epd->wMaxPacketSize), |
| 452 | epd->bEndpointAddress); |
| 453 | card->rx_data_ep = usb_endpoint_num(epd); |
| 454 | atomic_set(&card->rx_data_urb_pending, 0); |
| 455 | } |
| 456 | if (usb_endpoint_dir_out(epd) && |
| 457 | usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA && |
| 458 | usb_endpoint_xfer_bulk(epd)) { |
| 459 | pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n", |
| 460 | le16_to_cpu(epd->wMaxPacketSize), |
| 461 | epd->bEndpointAddress); |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 462 | card->port[0].tx_data_ep = usb_endpoint_num(epd); |
| 463 | atomic_set(&card->port[0].tx_data_urb_pending, 0); |
| 464 | } |
| 465 | if (usb_endpoint_dir_out(epd) && |
| 466 | usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA_CH2 && |
| 467 | usb_endpoint_xfer_bulk(epd)) { |
| 468 | pr_debug("info: bulk OUT chan2:\t" |
| 469 | "max pkt size: %d, addr: %d\n", |
| 470 | le16_to_cpu(epd->wMaxPacketSize), |
| 471 | epd->bEndpointAddress); |
| 472 | card->port[1].tx_data_ep = usb_endpoint_num(epd); |
| 473 | atomic_set(&card->port[1].tx_data_urb_pending, 0); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 474 | } |
| 475 | if (usb_endpoint_dir_out(epd) && |
| 476 | usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT && |
Ganapathi Bhat | 182f569 | 2017-03-27 18:27:44 +0530 | [diff] [blame] | 477 | (usb_endpoint_xfer_bulk(epd) || |
| 478 | usb_endpoint_xfer_int(epd))) { |
| 479 | card->tx_cmd_ep_type = usb_endpoint_type(epd); |
| 480 | card->tx_cmd_interval = epd->bInterval; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 481 | pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n", |
| 482 | le16_to_cpu(epd->wMaxPacketSize), |
| 483 | epd->bEndpointAddress); |
Ganapathi Bhat | 182f569 | 2017-03-27 18:27:44 +0530 | [diff] [blame] | 484 | pr_debug("info: Tx CMD:: max pkt size: %d, addr: %d, ep_type: %d\n", |
| 485 | le16_to_cpu(epd->wMaxPacketSize), |
| 486 | epd->bEndpointAddress, card->tx_cmd_ep_type); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 487 | card->tx_cmd_ep = usb_endpoint_num(epd); |
| 488 | atomic_set(&card->tx_cmd_urb_pending, 0); |
| 489 | card->bulk_out_maxpktsize = |
| 490 | le16_to_cpu(epd->wMaxPacketSize); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | usb_set_intfdata(intf, card); |
| 495 | |
Brian Norris | 4a79aa1 | 2016-11-18 19:30:26 +0530 | [diff] [blame] | 496 | ret = mwifiex_add_card(card, &card->fw_done, &usb_ops, |
Rajat Jain | 2e02b58 | 2016-11-15 19:06:02 +0530 | [diff] [blame] | 497 | MWIFIEX_USB, &card->udev->dev); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 498 | if (ret) { |
| 499 | pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret); |
| 500 | usb_reset_device(udev); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 501 | return ret; |
| 502 | } |
| 503 | |
| 504 | usb_get_dev(udev); |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | /* Kernel needs to suspend all functions separately. Therefore all |
| 510 | * registered functions must have drivers with suspend and resume |
| 511 | * methods. Failing that the kernel simply removes the whole card. |
| 512 | * |
| 513 | * If already not suspended, this function allocates and sends a |
| 514 | * 'host sleep activate' request to the firmware and turns off the traffic. |
| 515 | */ |
| 516 | static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message) |
| 517 | { |
| 518 | struct usb_card_rec *card = usb_get_intfdata(intf); |
| 519 | struct mwifiex_adapter *adapter; |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 520 | struct usb_tx_data_port *port; |
| 521 | int i, j; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 522 | |
Brian Norris | b42dbb2 | 2016-11-18 19:30:29 +0530 | [diff] [blame] | 523 | /* Might still be loading firmware */ |
| 524 | wait_for_completion(&card->fw_done); |
| 525 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 526 | adapter = card->adapter; |
Brian Norris | b42dbb2 | 2016-11-18 19:30:29 +0530 | [diff] [blame] | 527 | if (!adapter) { |
| 528 | dev_err(&intf->dev, "card is not valid\n"); |
| 529 | return 0; |
| 530 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 531 | |
| 532 | if (unlikely(adapter->is_suspended)) |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 533 | mwifiex_dbg(adapter, WARN, |
| 534 | "Device already suspended\n"); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 535 | |
Brian Norris | 58b7033 | 2016-11-18 19:30:31 +0530 | [diff] [blame] | 536 | /* Enable the Host Sleep */ |
| 537 | if (!mwifiex_enable_hs(adapter)) { |
| 538 | mwifiex_dbg(adapter, ERROR, |
| 539 | "cmd: failed to suspend\n"); |
| 540 | adapter->hs_enabling = false; |
| 541 | return -EFAULT; |
| 542 | } |
| 543 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 544 | |
| 545 | /* 'is_suspended' flag indicates device is suspended. |
| 546 | * It must be set here before the usb_kill_urb() calls. Reason |
| 547 | * is in the complete handlers, urb->status(= -ENOENT) and |
| 548 | * this flag is used in combination to distinguish between a |
| 549 | * 'suspended' state and a 'disconnect' one. |
| 550 | */ |
| 551 | adapter->is_suspended = true; |
Amitkumar Karwar | c0dbba6 | 2014-03-25 19:01:20 -0700 | [diff] [blame] | 552 | adapter->hs_enabling = false; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 553 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 554 | if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb) |
| 555 | usb_kill_urb(card->rx_cmd.urb); |
| 556 | |
| 557 | if (atomic_read(&card->rx_data_urb_pending)) |
| 558 | for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) |
| 559 | if (card->rx_data_list[i].urb) |
| 560 | usb_kill_urb(card->rx_data_list[i].urb); |
| 561 | |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 562 | for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { |
| 563 | port = &card->port[i]; |
| 564 | for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { |
| 565 | if (port->tx_data_list[j].urb) |
| 566 | usb_kill_urb(port->tx_data_list[j].urb); |
| 567 | } |
| 568 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 569 | |
| 570 | if (card->tx_cmd.urb) |
| 571 | usb_kill_urb(card->tx_cmd.urb); |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | /* Kernel needs to suspend all functions separately. Therefore all |
| 577 | * registered functions must have drivers with suspend and resume |
| 578 | * methods. Failing that the kernel simply removes the whole card. |
| 579 | * |
| 580 | * If already not resumed, this function turns on the traffic and |
| 581 | * sends a 'host sleep cancel' request to the firmware. |
| 582 | */ |
| 583 | static int mwifiex_usb_resume(struct usb_interface *intf) |
| 584 | { |
| 585 | struct usb_card_rec *card = usb_get_intfdata(intf); |
| 586 | struct mwifiex_adapter *adapter; |
| 587 | int i; |
| 588 | |
Brian Norris | e98fb11 | 2016-11-18 19:30:33 +0530 | [diff] [blame] | 589 | if (!card->adapter) { |
| 590 | dev_err(&intf->dev, "%s: card->adapter is NULL\n", |
| 591 | __func__); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | adapter = card->adapter; |
| 595 | |
| 596 | if (unlikely(!adapter->is_suspended)) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 597 | mwifiex_dbg(adapter, WARN, |
| 598 | "Device already resumed\n"); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | /* Indicate device resumed. The netdev queue will be resumed only |
| 603 | * after the urbs have been re-submitted |
| 604 | */ |
| 605 | adapter->is_suspended = false; |
| 606 | |
| 607 | if (!atomic_read(&card->rx_data_urb_pending)) |
| 608 | for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) |
| 609 | mwifiex_usb_submit_rx_urb(&card->rx_data_list[i], |
| 610 | MWIFIEX_RX_DATA_BUF_SIZE); |
| 611 | |
| 612 | if (!atomic_read(&card->rx_cmd_urb_pending)) { |
| 613 | card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE); |
| 614 | if (card->rx_cmd.skb) |
| 615 | mwifiex_usb_submit_rx_urb(&card->rx_cmd, |
| 616 | MWIFIEX_RX_CMD_BUF_SIZE); |
| 617 | } |
| 618 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 619 | /* Disable Host Sleep */ |
| 620 | if (adapter->hs_activated) |
| 621 | mwifiex_cancel_hs(mwifiex_get_priv(adapter, |
| 622 | MWIFIEX_BSS_ROLE_ANY), |
| 623 | MWIFIEX_ASYNC_CMD); |
| 624 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | static void mwifiex_usb_disconnect(struct usb_interface *intf) |
| 629 | { |
| 630 | struct usb_card_rec *card = usb_get_intfdata(intf); |
Amitkumar Karwar | 3fffd7c | 2014-04-14 15:32:54 -0700 | [diff] [blame] | 631 | struct mwifiex_adapter *adapter; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 632 | |
Brian Norris | 4a79aa1 | 2016-11-18 19:30:26 +0530 | [diff] [blame] | 633 | wait_for_completion(&card->fw_done); |
| 634 | |
Amitkumar Karwar | 3fffd7c | 2014-04-14 15:32:54 -0700 | [diff] [blame] | 635 | adapter = card->adapter; |
Brian Norris | 4a79aa1 | 2016-11-18 19:30:26 +0530 | [diff] [blame] | 636 | if (!adapter || !adapter->priv_num) |
Amitkumar Karwar | 3fffd7c | 2014-04-14 15:32:54 -0700 | [diff] [blame] | 637 | return; |
| 638 | |
Xinming Hu | 045f0c1 | 2016-11-30 20:22:17 +0530 | [diff] [blame] | 639 | if (card->udev->state != USB_STATE_NOTATTACHED && !adapter->mfg_mode) { |
Amitkumar Karwar | 3fffd7c | 2014-04-14 15:32:54 -0700 | [diff] [blame] | 640 | mwifiex_deauthenticate_all(adapter); |
| 641 | |
| 642 | mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter, |
| 643 | MWIFIEX_BSS_ROLE_ANY), |
| 644 | MWIFIEX_FUNC_SHUTDOWN); |
| 645 | } |
| 646 | |
Ganapathi Bhat | b817047a | 2018-05-24 19:18:27 +0530 | [diff] [blame] | 647 | if (adapter->workqueue) |
| 648 | flush_workqueue(adapter->workqueue); |
| 649 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 650 | mwifiex_usb_free(card); |
| 651 | |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 652 | mwifiex_dbg(adapter, FATAL, |
| 653 | "%s: removing card\n", __func__); |
Brian Norris | 4a79aa1 | 2016-11-18 19:30:26 +0530 | [diff] [blame] | 654 | mwifiex_remove_card(adapter); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 655 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 656 | usb_put_dev(interface_to_usbdev(intf)); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Arend Van Spriel | 21c5c83 | 2018-05-16 14:12:00 +0200 | [diff] [blame] | 659 | static void mwifiex_usb_coredump(struct device *dev) |
| 660 | { |
| 661 | struct usb_interface *intf = to_usb_interface(dev); |
| 662 | struct usb_card_rec *card = usb_get_intfdata(intf); |
| 663 | |
Ganapathi Bhat | 618fd1e | 2018-05-24 16:36:28 +0530 | [diff] [blame] | 664 | mwifiex_fw_dump_event(mwifiex_get_priv(card->adapter, |
| 665 | MWIFIEX_BSS_ROLE_ANY)); |
Arend Van Spriel | 21c5c83 | 2018-05-16 14:12:00 +0200 | [diff] [blame] | 666 | } |
| 667 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 668 | static struct usb_driver mwifiex_usb_driver = { |
Bing Zhao | e4ceb0f | 2014-02-21 12:23:00 -0800 | [diff] [blame] | 669 | .name = "mwifiex_usb", |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 670 | .probe = mwifiex_usb_probe, |
| 671 | .disconnect = mwifiex_usb_disconnect, |
| 672 | .id_table = mwifiex_usb_table, |
| 673 | .suspend = mwifiex_usb_suspend, |
| 674 | .resume = mwifiex_usb_resume, |
Amitkumar Karwar | 3fffd7c | 2014-04-14 15:32:54 -0700 | [diff] [blame] | 675 | .soft_unbind = 1, |
Arend Van Spriel | 21c5c83 | 2018-05-16 14:12:00 +0200 | [diff] [blame] | 676 | .drvwrap.driver = { |
| 677 | .coredump = mwifiex_usb_coredump, |
| 678 | }, |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 679 | }; |
| 680 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 681 | static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf, |
| 682 | u32 *len, u8 ep, u32 timeout) |
| 683 | { |
| 684 | struct usb_card_rec *card = adapter->card; |
| 685 | int actual_length, ret; |
| 686 | |
| 687 | if (!(*len % card->bulk_out_maxpktsize)) |
| 688 | (*len)++; |
| 689 | |
| 690 | /* Send the data block */ |
| 691 | ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf, |
| 692 | *len, &actual_length, timeout); |
| 693 | if (ret) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 694 | mwifiex_dbg(adapter, ERROR, |
| 695 | "usb_bulk_msg for tx failed: %d\n", ret); |
Bing Zhao | 5b2e2ec | 2013-01-25 18:23:00 -0800 | [diff] [blame] | 696 | return ret; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | *len = actual_length; |
| 700 | |
| 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf, |
| 705 | u32 *len, u8 ep, u32 timeout) |
| 706 | { |
| 707 | struct usb_card_rec *card = adapter->card; |
| 708 | int actual_length, ret; |
| 709 | |
| 710 | /* Receive the data response */ |
| 711 | ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf, |
| 712 | *len, &actual_length, timeout); |
| 713 | if (ret) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 714 | mwifiex_dbg(adapter, ERROR, |
| 715 | "usb_bulk_msg for rx failed: %d\n", ret); |
Bing Zhao | 5b2e2ec | 2013-01-25 18:23:00 -0800 | [diff] [blame] | 716 | return ret; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | *len = actual_length; |
| 720 | |
| 721 | return ret; |
| 722 | } |
| 723 | |
Zhaoyang Liu | 7e4e5d2 | 2015-09-18 06:32:17 -0700 | [diff] [blame] | 724 | static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter) |
| 725 | { |
| 726 | struct usb_card_rec *card = adapter->card; |
| 727 | u8 active_port = MWIFIEX_USB_EP_DATA; |
| 728 | struct mwifiex_private *priv = NULL; |
| 729 | int i; |
| 730 | |
| 731 | if (adapter->usb_mc_status) { |
| 732 | for (i = 0; i < adapter->priv_num; i++) { |
| 733 | priv = adapter->priv[i]; |
| 734 | if (!priv) |
| 735 | continue; |
| 736 | if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP && |
| 737 | !priv->bss_started) || |
| 738 | (priv->bss_role == MWIFIEX_BSS_ROLE_STA && |
| 739 | !priv->media_connected)) |
| 740 | priv->usb_port = MWIFIEX_USB_EP_DATA; |
| 741 | } |
| 742 | for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) |
| 743 | card->port[i].block_status = false; |
| 744 | } else { |
| 745 | for (i = 0; i < adapter->priv_num; i++) { |
| 746 | priv = adapter->priv[i]; |
| 747 | if (!priv) |
| 748 | continue; |
| 749 | if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP && |
| 750 | priv->bss_started) || |
| 751 | (priv->bss_role == MWIFIEX_BSS_ROLE_STA && |
| 752 | priv->media_connected)) { |
| 753 | active_port = priv->usb_port; |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | for (i = 0; i < adapter->priv_num; i++) { |
| 758 | priv = adapter->priv[i]; |
| 759 | if (priv) |
| 760 | priv->usb_port = active_port; |
| 761 | } |
| 762 | for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { |
| 763 | if (active_port == card->port[i].tx_data_ep) |
| 764 | card->port[i].block_status = false; |
| 765 | else |
| 766 | card->port[i].block_status = true; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
Zhaoyang Liu | 735ab6b | 2015-09-18 06:32:18 -0700 | [diff] [blame] | 771 | static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv) |
| 772 | { |
| 773 | struct usb_card_rec *card = priv->adapter->card; |
| 774 | int idx; |
| 775 | |
| 776 | for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) { |
| 777 | if (priv->usb_port == card->port[idx].tx_data_ep) |
| 778 | return !card->port[idx].block_status; |
| 779 | } |
| 780 | |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter) |
| 785 | { |
| 786 | struct usb_card_rec *card = adapter->card; |
| 787 | int i; |
| 788 | |
| 789 | for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) |
| 790 | if (!card->port[i].block_status) |
| 791 | return false; |
| 792 | |
| 793 | return true; |
| 794 | } |
| 795 | |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 796 | static int mwifiex_usb_construct_send_urb(struct mwifiex_adapter *adapter, |
| 797 | struct usb_tx_data_port *port, u8 ep, |
| 798 | struct urb_context *context, |
| 799 | struct sk_buff *skb_send) |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 800 | { |
| 801 | struct usb_card_rec *card = adapter->card; |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 802 | int ret = -EINPROGRESS; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 803 | struct urb *tx_urb; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 804 | |
| 805 | context->adapter = adapter; |
| 806 | context->ep = ep; |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 807 | context->skb = skb_send; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 808 | tx_urb = context->urb; |
| 809 | |
Ganapathi Bhat | 182f569 | 2017-03-27 18:27:44 +0530 | [diff] [blame] | 810 | if (ep == card->tx_cmd_ep && |
| 811 | card->tx_cmd_ep_type == USB_ENDPOINT_XFER_INT) |
| 812 | usb_fill_int_urb(tx_urb, card->udev, |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 813 | usb_sndintpipe(card->udev, ep), skb_send->data, |
| 814 | skb_send->len, mwifiex_usb_tx_complete, |
Ganapathi Bhat | 182f569 | 2017-03-27 18:27:44 +0530 | [diff] [blame] | 815 | (void *)context, card->tx_cmd_interval); |
| 816 | else |
| 817 | usb_fill_bulk_urb(tx_urb, card->udev, |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 818 | usb_sndbulkpipe(card->udev, ep), |
| 819 | skb_send->data, skb_send->len, |
| 820 | mwifiex_usb_tx_complete, (void *)context); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 821 | |
| 822 | tx_urb->transfer_flags |= URB_ZERO_PACKET; |
| 823 | |
| 824 | if (ep == card->tx_cmd_ep) |
| 825 | atomic_inc(&card->tx_cmd_urb_pending); |
| 826 | else |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 827 | atomic_inc(&port->tx_data_urb_pending); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 828 | |
Cathy Luo | 5476f80 | 2016-09-20 20:49:03 +0530 | [diff] [blame] | 829 | if (ep != card->tx_cmd_ep && |
| 830 | atomic_read(&port->tx_data_urb_pending) == |
| 831 | MWIFIEX_TX_DATA_URB) { |
| 832 | port->block_status = true; |
| 833 | adapter->data_sent = mwifiex_usb_data_sent(adapter); |
Amitkumar Karwar | ac3b561 | 2016-09-20 20:49:04 +0530 | [diff] [blame] | 834 | ret = -ENOSR; |
Cathy Luo | 5476f80 | 2016-09-20 20:49:03 +0530 | [diff] [blame] | 835 | } |
| 836 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 837 | if (usb_submit_urb(tx_urb, GFP_ATOMIC)) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 838 | mwifiex_dbg(adapter, ERROR, |
| 839 | "%s: usb_submit_urb failed\n", __func__); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 840 | if (ep == card->tx_cmd_ep) { |
| 841 | atomic_dec(&card->tx_cmd_urb_pending); |
| 842 | } else { |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 843 | atomic_dec(&port->tx_data_urb_pending); |
Zhaoyang Liu | 735ab6b | 2015-09-18 06:32:18 -0700 | [diff] [blame] | 844 | port->block_status = false; |
Cathy Luo | 5476f80 | 2016-09-20 20:49:03 +0530 | [diff] [blame] | 845 | adapter->data_sent = false; |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 846 | if (port->tx_data_ix) |
| 847 | port->tx_data_ix--; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 848 | else |
Zhaoyang Liu | 308fe29 | 2015-09-18 06:32:16 -0700 | [diff] [blame] | 849 | port->tx_data_ix = MWIFIEX_TX_DATA_URB; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 850 | } |
Amitkumar Karwar | ac3b561 | 2016-09-20 20:49:04 +0530 | [diff] [blame] | 851 | ret = -1; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Zhaoyang Liu | 735ab6b | 2015-09-18 06:32:18 -0700 | [diff] [blame] | 854 | return ret; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 857 | static int mwifiex_usb_prepare_tx_aggr_skb(struct mwifiex_adapter *adapter, |
| 858 | struct usb_tx_data_port *port, |
| 859 | struct sk_buff **skb_send) |
| 860 | { |
| 861 | struct sk_buff *skb_aggr, *skb_tmp; |
| 862 | u8 *payload, pad; |
| 863 | u16 align = adapter->bus_aggr.tx_aggr_align; |
| 864 | struct mwifiex_txinfo *tx_info = NULL; |
| 865 | bool is_txinfo_set = false; |
| 866 | |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 867 | /* Packets in aggr_list will be send in either skb_aggr or |
| 868 | * write complete, delete the tx_aggr timer |
| 869 | */ |
| 870 | if (port->tx_aggr.timer_cnxt.is_hold_timer_set) { |
| 871 | del_timer(&port->tx_aggr.timer_cnxt.hold_timer); |
| 872 | port->tx_aggr.timer_cnxt.is_hold_timer_set = false; |
| 873 | port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0; |
| 874 | } |
| 875 | |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 876 | skb_aggr = mwifiex_alloc_dma_align_buf(port->tx_aggr.aggr_len, |
| 877 | GFP_ATOMIC); |
| 878 | if (!skb_aggr) { |
| 879 | mwifiex_dbg(adapter, ERROR, |
| 880 | "%s: alloc skb_aggr failed\n", __func__); |
| 881 | |
| 882 | while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list))) |
| 883 | mwifiex_write_data_complete(adapter, skb_tmp, 0, -1); |
| 884 | |
| 885 | port->tx_aggr.aggr_num = 0; |
| 886 | port->tx_aggr.aggr_len = 0; |
| 887 | return -EBUSY; |
| 888 | } |
| 889 | |
| 890 | tx_info = MWIFIEX_SKB_TXCB(skb_aggr); |
| 891 | memset(tx_info, 0, sizeof(*tx_info)); |
| 892 | |
| 893 | while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list))) { |
| 894 | /* padding for aligning next packet header*/ |
| 895 | pad = (align - (skb_tmp->len & (align - 1))) % align; |
| 896 | payload = skb_put(skb_aggr, skb_tmp->len + pad); |
| 897 | memcpy(payload, skb_tmp->data, skb_tmp->len); |
| 898 | if (skb_queue_empty(&port->tx_aggr.aggr_list)) { |
| 899 | /* do not padding for last packet*/ |
| 900 | *(u16 *)payload = cpu_to_le16(skb_tmp->len); |
| 901 | *(u16 *)&payload[2] = |
| 902 | cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80); |
| 903 | skb_trim(skb_aggr, skb_aggr->len - pad); |
| 904 | } else { |
| 905 | /* add aggregation interface header */ |
| 906 | *(u16 *)payload = cpu_to_le16(skb_tmp->len + pad); |
| 907 | *(u16 *)&payload[2] = |
| 908 | cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2); |
| 909 | } |
| 910 | |
| 911 | if (!is_txinfo_set) { |
| 912 | tx_info->bss_num = MWIFIEX_SKB_TXCB(skb_tmp)->bss_num; |
| 913 | tx_info->bss_type = MWIFIEX_SKB_TXCB(skb_tmp)->bss_type; |
| 914 | is_txinfo_set = true; |
| 915 | } |
| 916 | |
| 917 | port->tx_aggr.aggr_num--; |
| 918 | port->tx_aggr.aggr_len -= (skb_tmp->len + pad); |
| 919 | mwifiex_write_data_complete(adapter, skb_tmp, 0, 0); |
| 920 | } |
| 921 | |
| 922 | tx_info->pkt_len = skb_aggr->len - |
| 923 | (sizeof(struct txpd) + adapter->intf_hdr_len); |
| 924 | tx_info->flags |= MWIFIEX_BUF_FLAG_AGGR_PKT; |
| 925 | |
| 926 | port->tx_aggr.aggr_num = 0; |
| 927 | port->tx_aggr.aggr_len = 0; |
| 928 | *skb_send = skb_aggr; |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | /* This function prepare data packet to be send under usb tx aggregation |
| 934 | * protocol, check current usb aggregation status, link packet to aggrgation |
| 935 | * list if possible, work flow as below: |
| 936 | * (1) if only 1 packet available, add usb tx aggregation header and send. |
| 937 | * (2) if packet is able to aggregated, link it to current aggregation list. |
| 938 | * (3) if packet is not able to aggregated, aggregate and send exist packets |
| 939 | * in aggrgation list. Then, link packet in the list if there is more |
| 940 | * packet in transmit queue, otherwise try to transmit single packet. |
| 941 | */ |
| 942 | static int mwifiex_usb_aggr_tx_data(struct mwifiex_adapter *adapter, u8 ep, |
| 943 | struct sk_buff *skb, |
| 944 | struct mwifiex_tx_param *tx_param, |
| 945 | struct usb_tx_data_port *port) |
| 946 | { |
| 947 | u8 *payload, pad; |
| 948 | u16 align = adapter->bus_aggr.tx_aggr_align; |
| 949 | struct sk_buff *skb_send = NULL; |
| 950 | struct urb_context *context = NULL; |
| 951 | struct txpd *local_tx_pd = |
| 952 | (struct txpd *)((u8 *)skb->data + adapter->intf_hdr_len); |
| 953 | u8 f_send_aggr_buf = 0; |
| 954 | u8 f_send_cur_buf = 0; |
| 955 | u8 f_precopy_cur_buf = 0; |
| 956 | u8 f_postcopy_cur_buf = 0; |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 957 | u32 timeout; |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 958 | int ret; |
| 959 | |
| 960 | /* padding to ensure each packet alginment */ |
| 961 | pad = (align - (skb->len & (align - 1))) % align; |
| 962 | |
| 963 | if (tx_param && tx_param->next_pkt_len) { |
| 964 | /* next packet available in tx queue*/ |
| 965 | if (port->tx_aggr.aggr_len + skb->len + pad > |
| 966 | adapter->bus_aggr.tx_aggr_max_size) { |
| 967 | f_send_aggr_buf = 1; |
| 968 | f_postcopy_cur_buf = 1; |
| 969 | } else { |
| 970 | /* current packet could be aggregated*/ |
| 971 | f_precopy_cur_buf = 1; |
| 972 | |
| 973 | if (port->tx_aggr.aggr_len + skb->len + pad + |
| 974 | tx_param->next_pkt_len > |
| 975 | adapter->bus_aggr.tx_aggr_max_size || |
| 976 | port->tx_aggr.aggr_num + 2 > |
| 977 | adapter->bus_aggr.tx_aggr_max_num) { |
| 978 | /* next packet could not be aggregated |
| 979 | * send current aggregation buffer |
| 980 | */ |
| 981 | f_send_aggr_buf = 1; |
| 982 | } |
| 983 | } |
| 984 | } else { |
| 985 | /* last packet in tx queue */ |
| 986 | if (port->tx_aggr.aggr_num > 0) { |
| 987 | /* pending packets in aggregation buffer*/ |
| 988 | if (port->tx_aggr.aggr_len + skb->len + pad > |
| 989 | adapter->bus_aggr.tx_aggr_max_size) { |
| 990 | /* current packet not be able to aggregated, |
| 991 | * send aggr buffer first, then send packet. |
| 992 | */ |
| 993 | f_send_cur_buf = 1; |
| 994 | } else { |
| 995 | /* last packet, Aggregation and send */ |
| 996 | f_precopy_cur_buf = 1; |
| 997 | } |
| 998 | |
| 999 | f_send_aggr_buf = 1; |
| 1000 | } else { |
| 1001 | /* no pending packets in aggregation buffer, |
| 1002 | * send current packet immediately |
| 1003 | */ |
| 1004 | f_send_cur_buf = 1; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | if (local_tx_pd->flags & MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET) { |
| 1009 | /* Send NULL packet immediately*/ |
| 1010 | if (f_precopy_cur_buf) { |
| 1011 | if (skb_queue_empty(&port->tx_aggr.aggr_list)) { |
| 1012 | f_precopy_cur_buf = 0; |
| 1013 | f_send_aggr_buf = 0; |
| 1014 | f_send_cur_buf = 1; |
| 1015 | } else { |
| 1016 | f_send_aggr_buf = 1; |
| 1017 | } |
| 1018 | } else if (f_postcopy_cur_buf) { |
| 1019 | f_send_cur_buf = 1; |
| 1020 | f_postcopy_cur_buf = 0; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | if (f_precopy_cur_buf) { |
| 1025 | skb_queue_tail(&port->tx_aggr.aggr_list, skb); |
| 1026 | port->tx_aggr.aggr_len += (skb->len + pad); |
| 1027 | port->tx_aggr.aggr_num++; |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1028 | if (f_send_aggr_buf) |
| 1029 | goto send_aggr_buf; |
| 1030 | |
| 1031 | /* packet will not been send immediately, |
| 1032 | * set a timer to make sure it will be sent under |
| 1033 | * strict time limit. Dynamically fit the timeout |
| 1034 | * value, according to packets number in aggr_list |
| 1035 | */ |
| 1036 | if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) { |
| 1037 | port->tx_aggr.timer_cnxt.hold_tmo_msecs = |
| 1038 | MWIFIEX_USB_TX_AGGR_TMO_MIN; |
| 1039 | timeout = |
| 1040 | port->tx_aggr.timer_cnxt.hold_tmo_msecs; |
| 1041 | mod_timer(&port->tx_aggr.timer_cnxt.hold_timer, |
| 1042 | jiffies + msecs_to_jiffies(timeout)); |
| 1043 | port->tx_aggr.timer_cnxt.is_hold_timer_set = true; |
| 1044 | } else { |
| 1045 | if (port->tx_aggr.timer_cnxt.hold_tmo_msecs < |
| 1046 | MWIFIEX_USB_TX_AGGR_TMO_MAX) { |
| 1047 | /* Dyanmic fit timeout */ |
| 1048 | timeout = |
| 1049 | ++port->tx_aggr.timer_cnxt.hold_tmo_msecs; |
| 1050 | mod_timer(&port->tx_aggr.timer_cnxt.hold_timer, |
| 1051 | jiffies + msecs_to_jiffies(timeout)); |
| 1052 | } |
| 1053 | } |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1056 | send_aggr_buf: |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 1057 | if (f_send_aggr_buf) { |
| 1058 | ret = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send); |
| 1059 | if (!ret) { |
| 1060 | context = &port->tx_data_list[port->tx_data_ix++]; |
| 1061 | ret = mwifiex_usb_construct_send_urb(adapter, port, ep, |
| 1062 | context, skb_send); |
| 1063 | if (ret == -1) |
| 1064 | mwifiex_write_data_complete(adapter, skb_send, |
| 1065 | 0, -1); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | if (f_send_cur_buf) { |
| 1070 | if (f_send_aggr_buf) { |
| 1071 | if (atomic_read(&port->tx_data_urb_pending) >= |
| 1072 | MWIFIEX_TX_DATA_URB) { |
| 1073 | port->block_status = true; |
| 1074 | adapter->data_sent = |
| 1075 | mwifiex_usb_data_sent(adapter); |
| 1076 | /* no available urb, postcopy packet*/ |
| 1077 | f_postcopy_cur_buf = 1; |
| 1078 | goto postcopy_cur_buf; |
| 1079 | } |
| 1080 | |
| 1081 | if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB) |
| 1082 | port->tx_data_ix = 0; |
| 1083 | } |
| 1084 | |
| 1085 | payload = skb->data; |
| 1086 | *(u16 *)&payload[2] = |
| 1087 | cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80); |
| 1088 | *(u16 *)payload = cpu_to_le16(skb->len); |
| 1089 | skb_send = skb; |
| 1090 | context = &port->tx_data_list[port->tx_data_ix++]; |
| 1091 | return mwifiex_usb_construct_send_urb(adapter, port, ep, |
| 1092 | context, skb_send); |
| 1093 | } |
| 1094 | |
| 1095 | postcopy_cur_buf: |
| 1096 | if (f_postcopy_cur_buf) { |
| 1097 | skb_queue_tail(&port->tx_aggr.aggr_list, skb); |
| 1098 | port->tx_aggr.aggr_len += (skb->len + pad); |
| 1099 | port->tx_aggr.aggr_num++; |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1100 | /* New aggregation begin, start timer */ |
| 1101 | if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) { |
| 1102 | port->tx_aggr.timer_cnxt.hold_tmo_msecs = |
| 1103 | MWIFIEX_USB_TX_AGGR_TMO_MIN; |
| 1104 | timeout = port->tx_aggr.timer_cnxt.hold_tmo_msecs; |
| 1105 | mod_timer(&port->tx_aggr.timer_cnxt.hold_timer, |
| 1106 | jiffies + msecs_to_jiffies(timeout)); |
| 1107 | port->tx_aggr.timer_cnxt.is_hold_timer_set = true; |
| 1108 | } |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | return -EINPROGRESS; |
| 1112 | } |
| 1113 | |
Kees Cook | 08c2eb8 | 2017-10-24 02:29:19 -0700 | [diff] [blame] | 1114 | static void mwifiex_usb_tx_aggr_tmo(struct timer_list *t) |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1115 | { |
| 1116 | struct urb_context *urb_cnxt = NULL; |
| 1117 | struct sk_buff *skb_send = NULL; |
| 1118 | struct tx_aggr_tmr_cnxt *timer_context = |
Kees Cook | 08c2eb8 | 2017-10-24 02:29:19 -0700 | [diff] [blame] | 1119 | from_timer(timer_context, t, hold_timer); |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1120 | struct mwifiex_adapter *adapter = timer_context->adapter; |
| 1121 | struct usb_tx_data_port *port = timer_context->port; |
| 1122 | unsigned long flags; |
| 1123 | int err = 0; |
| 1124 | |
| 1125 | spin_lock_irqsave(&port->tx_aggr_lock, flags); |
| 1126 | err = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send); |
| 1127 | if (err) { |
| 1128 | mwifiex_dbg(adapter, ERROR, |
| 1129 | "prepare tx aggr skb failed, err=%d\n", err); |
Dan Carpenter | fe0c94b | 2017-07-10 10:21:15 +0300 | [diff] [blame] | 1130 | goto unlock; |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | if (atomic_read(&port->tx_data_urb_pending) >= |
| 1134 | MWIFIEX_TX_DATA_URB) { |
| 1135 | port->block_status = true; |
| 1136 | adapter->data_sent = |
| 1137 | mwifiex_usb_data_sent(adapter); |
| 1138 | err = -1; |
| 1139 | goto done; |
| 1140 | } |
| 1141 | |
| 1142 | if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB) |
| 1143 | port->tx_data_ix = 0; |
| 1144 | |
| 1145 | urb_cnxt = &port->tx_data_list[port->tx_data_ix++]; |
| 1146 | err = mwifiex_usb_construct_send_urb(adapter, port, port->tx_data_ep, |
| 1147 | urb_cnxt, skb_send); |
| 1148 | done: |
| 1149 | if (err == -1) |
| 1150 | mwifiex_write_data_complete(adapter, skb_send, 0, -1); |
Dan Carpenter | fe0c94b | 2017-07-10 10:21:15 +0300 | [diff] [blame] | 1151 | unlock: |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1152 | spin_unlock_irqrestore(&port->tx_aggr_lock, flags); |
| 1153 | } |
| 1154 | |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 1155 | /* This function write a command/data packet to card. */ |
| 1156 | static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep, |
| 1157 | struct sk_buff *skb, |
| 1158 | struct mwifiex_tx_param *tx_param) |
| 1159 | { |
| 1160 | struct usb_card_rec *card = adapter->card; |
| 1161 | struct urb_context *context = NULL; |
| 1162 | struct usb_tx_data_port *port = NULL; |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1163 | unsigned long flags; |
| 1164 | int idx, ret; |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 1165 | |
| 1166 | if (adapter->is_suspended) { |
| 1167 | mwifiex_dbg(adapter, ERROR, |
| 1168 | "%s: not allowed while suspended\n", __func__); |
| 1169 | return -1; |
| 1170 | } |
| 1171 | |
| 1172 | if (adapter->surprise_removed) { |
| 1173 | mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__); |
| 1174 | return -1; |
| 1175 | } |
| 1176 | |
| 1177 | mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep); |
| 1178 | |
| 1179 | if (ep == card->tx_cmd_ep) { |
| 1180 | context = &card->tx_cmd; |
| 1181 | } else { |
| 1182 | /* get the data port structure for endpoint */ |
| 1183 | for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) { |
| 1184 | if (ep == card->port[idx].tx_data_ep) { |
| 1185 | port = &card->port[idx]; |
| 1186 | if (atomic_read(&port->tx_data_urb_pending) |
| 1187 | >= MWIFIEX_TX_DATA_URB) { |
| 1188 | port->block_status = true; |
| 1189 | adapter->data_sent = |
| 1190 | mwifiex_usb_data_sent(adapter); |
| 1191 | return -EBUSY; |
| 1192 | } |
| 1193 | if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB) |
| 1194 | port->tx_data_ix = 0; |
| 1195 | break; |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | if (!port) { |
| 1200 | mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n"); |
| 1201 | return -1; |
| 1202 | } |
| 1203 | |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1204 | if (adapter->bus_aggr.enable) { |
| 1205 | spin_lock_irqsave(&port->tx_aggr_lock, flags); |
| 1206 | ret = mwifiex_usb_aggr_tx_data(adapter, ep, skb, |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 1207 | tx_param, port); |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1208 | spin_unlock_irqrestore(&port->tx_aggr_lock, flags); |
| 1209 | return ret; |
| 1210 | } |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 1211 | |
| 1212 | context = &port->tx_data_list[port->tx_data_ix++]; |
| 1213 | } |
| 1214 | |
| 1215 | return mwifiex_usb_construct_send_urb(adapter, port, ep, context, skb); |
| 1216 | } |
| 1217 | |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1218 | static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter) |
| 1219 | { |
| 1220 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 1221 | struct usb_tx_data_port *port; |
| 1222 | int i, j; |
| 1223 | |
| 1224 | card->tx_cmd.adapter = adapter; |
| 1225 | card->tx_cmd.ep = card->tx_cmd_ep; |
| 1226 | |
| 1227 | card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1228 | if (!card->tx_cmd.urb) |
| 1229 | return -ENOMEM; |
| 1230 | |
| 1231 | for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { |
| 1232 | port = &card->port[i]; |
| 1233 | if (!port->tx_data_ep) |
| 1234 | continue; |
| 1235 | port->tx_data_ix = 0; |
| 1236 | skb_queue_head_init(&port->tx_aggr.aggr_list); |
| 1237 | if (port->tx_data_ep == MWIFIEX_USB_EP_DATA) |
| 1238 | port->block_status = false; |
| 1239 | else |
| 1240 | port->block_status = true; |
| 1241 | for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { |
| 1242 | port->tx_data_list[j].adapter = adapter; |
| 1243 | port->tx_data_list[j].ep = port->tx_data_ep; |
| 1244 | port->tx_data_list[j].urb = |
| 1245 | usb_alloc_urb(0, GFP_KERNEL); |
| 1246 | if (!port->tx_data_list[j].urb) |
| 1247 | return -ENOMEM; |
| 1248 | } |
| 1249 | |
| 1250 | port->tx_aggr.timer_cnxt.adapter = adapter; |
| 1251 | port->tx_aggr.timer_cnxt.port = port; |
| 1252 | port->tx_aggr.timer_cnxt.is_hold_timer_set = false; |
| 1253 | port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0; |
Kees Cook | 08c2eb8 | 2017-10-24 02:29:19 -0700 | [diff] [blame] | 1254 | timer_setup(&port->tx_aggr.timer_cnxt.hold_timer, |
| 1255 | mwifiex_usb_tx_aggr_tmo, 0); |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter) |
| 1262 | { |
| 1263 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 1264 | int i; |
| 1265 | |
| 1266 | card->rx_cmd.adapter = adapter; |
| 1267 | card->rx_cmd.ep = card->rx_cmd_ep; |
| 1268 | |
| 1269 | card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1270 | if (!card->rx_cmd.urb) |
| 1271 | return -ENOMEM; |
| 1272 | |
| 1273 | card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE); |
| 1274 | if (!card->rx_cmd.skb) |
| 1275 | return -ENOMEM; |
| 1276 | |
| 1277 | if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE)) |
| 1278 | return -1; |
| 1279 | |
| 1280 | for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { |
| 1281 | card->rx_data_list[i].adapter = adapter; |
| 1282 | card->rx_data_list[i].ep = card->rx_data_ep; |
| 1283 | |
| 1284 | card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1285 | if (!card->rx_data_list[i].urb) |
| 1286 | return -1; |
| 1287 | if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i], |
| 1288 | MWIFIEX_RX_DATA_BUF_SIZE)) |
| 1289 | return -1; |
| 1290 | } |
| 1291 | |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1295 | /* This function register usb device and initialize parameter. */ |
| 1296 | static int mwifiex_register_dev(struct mwifiex_adapter *adapter) |
| 1297 | { |
| 1298 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 1299 | |
| 1300 | card->adapter = adapter; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1301 | |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 1302 | switch (le16_to_cpu(card->udev->descriptor.idProduct)) { |
Zhaoyang Liu | 6d85ef0 | 2015-08-05 06:09:40 -0700 | [diff] [blame] | 1303 | case USB8997_PID_1: |
| 1304 | case USB8997_PID_2: |
| 1305 | adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K; |
| 1306 | strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME); |
| 1307 | adapter->ext_scan = true; |
| 1308 | break; |
Kristian Evensen | 9e6f3f4 | 2014-11-20 16:52:58 +0100 | [diff] [blame] | 1309 | case USB8766_PID_1: |
| 1310 | case USB8766_PID_2: |
| 1311 | adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; |
| 1312 | strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME); |
Avinash Patil | 1fe192d | 2015-01-23 17:09:19 +0530 | [diff] [blame] | 1313 | adapter->ext_scan = true; |
Kristian Evensen | 9e6f3f4 | 2014-11-20 16:52:58 +0100 | [diff] [blame] | 1314 | break; |
Yogesh Ashok Powar | eaa3d9f | 2015-01-23 17:09:18 +0530 | [diff] [blame] | 1315 | case USB8801_PID_1: |
| 1316 | case USB8801_PID_2: |
| 1317 | adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; |
| 1318 | strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME); |
Avinash Patil | 1fe192d | 2015-01-23 17:09:19 +0530 | [diff] [blame] | 1319 | adapter->ext_scan = false; |
Yogesh Ashok Powar | eaa3d9f | 2015-01-23 17:09:18 +0530 | [diff] [blame] | 1320 | break; |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 1321 | case USB8797_PID_1: |
| 1322 | case USB8797_PID_2: |
| 1323 | default: |
Amitkumar Karwar | 828cf22 | 2014-02-27 19:35:13 -0800 | [diff] [blame] | 1324 | adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 1325 | strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME); |
| 1326 | break; |
| 1327 | } |
| 1328 | |
Zhaoyang Liu | 2b0f997 | 2015-09-18 06:32:15 -0700 | [diff] [blame] | 1329 | adapter->usb_mc_status = false; |
Zhaoyang Liu | 7e4e5d2 | 2015-09-18 06:32:17 -0700 | [diff] [blame] | 1330 | adapter->usb_mc_setup = false; |
Zhaoyang Liu | 2b0f997 | 2015-09-18 06:32:15 -0700 | [diff] [blame] | 1331 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1332 | return 0; |
| 1333 | } |
| 1334 | |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 1335 | static void mwifiex_usb_cleanup_tx_aggr(struct mwifiex_adapter *adapter) |
| 1336 | { |
| 1337 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 1338 | struct usb_tx_data_port *port; |
| 1339 | struct sk_buff *skb_tmp; |
| 1340 | int idx; |
| 1341 | |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1342 | for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) { |
| 1343 | port = &card->port[idx]; |
| 1344 | if (adapter->bus_aggr.enable) |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 1345 | while ((skb_tmp = |
| 1346 | skb_dequeue(&port->tx_aggr.aggr_list))) |
| 1347 | mwifiex_write_data_complete(adapter, skb_tmp, |
| 1348 | 0, -1); |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1349 | del_timer_sync(&port->tx_aggr.timer_cnxt.hold_timer); |
| 1350 | port->tx_aggr.timer_cnxt.is_hold_timer_set = false; |
| 1351 | port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0; |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | |
Amitkumar Karwar | 2739a95 | 2013-07-22 19:17:51 -0700 | [diff] [blame] | 1355 | static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter) |
| 1356 | { |
| 1357 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 1358 | |
Xinming Hu | a2ca85a | 2017-05-23 07:12:33 +0000 | [diff] [blame] | 1359 | mwifiex_usb_cleanup_tx_aggr(adapter); |
Xinming Hu | c599429 | 2017-05-23 07:12:32 +0000 | [diff] [blame] | 1360 | |
Ujjal Roy | 353d2a6 | 2013-11-14 19:10:42 -0800 | [diff] [blame] | 1361 | card->adapter = NULL; |
Amitkumar Karwar | 2739a95 | 2013-07-22 19:17:51 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1364 | static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter, |
| 1365 | struct mwifiex_fw_image *fw) |
| 1366 | { |
| 1367 | int ret = 0; |
| 1368 | u8 *firmware = fw->fw_buf, *recv_buff; |
Dan Carpenter | 394f0ed | 2016-04-19 07:23:44 -0700 | [diff] [blame] | 1369 | u32 retries = USB8XXX_FW_MAX_RETRY + 1; |
| 1370 | u32 dlen; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1371 | u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0; |
| 1372 | struct fw_data *fwdata; |
| 1373 | struct fw_sync_header sync_fw; |
| 1374 | u8 check_winner = 1; |
| 1375 | |
| 1376 | if (!firmware) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1377 | mwifiex_dbg(adapter, ERROR, |
| 1378 | "No firmware image found! Terminating download\n"); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1379 | ret = -1; |
| 1380 | goto fw_exit; |
| 1381 | } |
| 1382 | |
| 1383 | /* Allocate memory for transmit */ |
| 1384 | fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL); |
Dan Carpenter | 3b1f0e8 | 2015-08-04 10:49:27 +0300 | [diff] [blame] | 1385 | if (!fwdata) { |
| 1386 | ret = -ENOMEM; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1387 | goto fw_exit; |
Dan Carpenter | 3b1f0e8 | 2015-08-04 10:49:27 +0300 | [diff] [blame] | 1388 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1389 | |
| 1390 | /* Allocate memory for receive */ |
| 1391 | recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL); |
Dan Carpenter | e0bdef0 | 2016-04-19 07:21:58 -0700 | [diff] [blame] | 1392 | if (!recv_buff) { |
| 1393 | ret = -ENOMEM; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1394 | goto cleanup; |
Dan Carpenter | e0bdef0 | 2016-04-19 07:21:58 -0700 | [diff] [blame] | 1395 | } |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1396 | |
| 1397 | do { |
| 1398 | /* Send pseudo data to check winner status first */ |
| 1399 | if (check_winner) { |
| 1400 | memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header)); |
| 1401 | dlen = 0; |
| 1402 | } else { |
| 1403 | /* copy the header of the fw_data to get the length */ |
Sasha Levin | fc40ca9 | 2013-01-02 16:23:01 -0800 | [diff] [blame] | 1404 | memcpy(&fwdata->fw_hdr, &firmware[tlen], |
| 1405 | sizeof(struct fw_header)); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1406 | |
| 1407 | dlen = le32_to_cpu(fwdata->fw_hdr.data_len); |
| 1408 | dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd); |
| 1409 | tlen += sizeof(struct fw_header); |
| 1410 | |
Ganapathi Bhat | 7877646 | 2016-09-12 18:55:27 +0530 | [diff] [blame] | 1411 | /* Command 7 doesn't have data length field */ |
| 1412 | if (dnld_cmd == FW_CMD_7) |
| 1413 | dlen = 0; |
| 1414 | |
Sasha Levin | fc40ca9 | 2013-01-02 16:23:01 -0800 | [diff] [blame] | 1415 | memcpy(fwdata->data, &firmware[tlen], dlen); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1416 | |
| 1417 | fwdata->seq_num = cpu_to_le32(fw_seqnum); |
| 1418 | tlen += dlen; |
| 1419 | } |
| 1420 | |
| 1421 | /* If the send/receive fails or CRC occurs then retry */ |
Dan Carpenter | 394f0ed | 2016-04-19 07:23:44 -0700 | [diff] [blame] | 1422 | while (--retries) { |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1423 | u8 *buf = (u8 *)fwdata; |
| 1424 | u32 len = FW_DATA_XMIT_SIZE; |
| 1425 | |
| 1426 | /* send the firmware block */ |
| 1427 | ret = mwifiex_write_data_sync(adapter, buf, &len, |
| 1428 | MWIFIEX_USB_EP_CMD_EVENT, |
| 1429 | MWIFIEX_USB_TIMEOUT); |
| 1430 | if (ret) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1431 | mwifiex_dbg(adapter, ERROR, |
| 1432 | "write_data_sync: failed: %d\n", |
| 1433 | ret); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1434 | continue; |
| 1435 | } |
| 1436 | |
| 1437 | buf = recv_buff; |
| 1438 | len = FW_DNLD_RX_BUF_SIZE; |
| 1439 | |
| 1440 | /* Receive the firmware block response */ |
| 1441 | ret = mwifiex_read_data_sync(adapter, buf, &len, |
| 1442 | MWIFIEX_USB_EP_CMD_EVENT, |
| 1443 | MWIFIEX_USB_TIMEOUT); |
| 1444 | if (ret) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1445 | mwifiex_dbg(adapter, ERROR, |
| 1446 | "read_data_sync: failed: %d\n", |
| 1447 | ret); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1448 | continue; |
| 1449 | } |
| 1450 | |
| 1451 | memcpy(&sync_fw, recv_buff, |
| 1452 | sizeof(struct fw_sync_header)); |
| 1453 | |
| 1454 | /* check 1st firmware block resp for highest bit set */ |
| 1455 | if (check_winner) { |
| 1456 | if (le32_to_cpu(sync_fw.cmd) & 0x80000000) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1457 | mwifiex_dbg(adapter, WARN, |
| 1458 | "USB is not the winner %#x\n", |
| 1459 | sync_fw.cmd); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1460 | |
| 1461 | /* returning success */ |
| 1462 | ret = 0; |
| 1463 | goto cleanup; |
| 1464 | } |
| 1465 | |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1466 | mwifiex_dbg(adapter, MSG, |
| 1467 | "start to download FW...\n"); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1468 | |
| 1469 | check_winner = 0; |
| 1470 | break; |
| 1471 | } |
| 1472 | |
| 1473 | /* check the firmware block response for CRC errors */ |
| 1474 | if (sync_fw.cmd) { |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1475 | mwifiex_dbg(adapter, ERROR, |
| 1476 | "FW received block with CRC %#x\n", |
| 1477 | sync_fw.cmd); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1478 | ret = -1; |
| 1479 | continue; |
| 1480 | } |
| 1481 | |
Dan Carpenter | 394f0ed | 2016-04-19 07:23:44 -0700 | [diff] [blame] | 1482 | retries = USB8XXX_FW_MAX_RETRY + 1; |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1483 | break; |
| 1484 | } |
| 1485 | fw_seqnum++; |
| 1486 | } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries); |
| 1487 | |
| 1488 | cleanup: |
Zhaoyang Liu | acebe8c | 2015-05-12 00:48:20 +0530 | [diff] [blame] | 1489 | mwifiex_dbg(adapter, MSG, |
| 1490 | "info: FW download over, size %d bytes\n", tlen); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1491 | |
| 1492 | kfree(recv_buff); |
| 1493 | kfree(fwdata); |
| 1494 | |
| 1495 | if (retries) |
| 1496 | ret = 0; |
| 1497 | fw_exit: |
| 1498 | return ret; |
| 1499 | } |
| 1500 | |
| 1501 | static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter, |
| 1502 | struct mwifiex_fw_image *fw) |
| 1503 | { |
| 1504 | int ret; |
| 1505 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 1506 | |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 1507 | if (card->usb_boot_state == USB8XXX_FW_DNLD) { |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1508 | ret = mwifiex_prog_fw_w_helper(adapter, fw); |
| 1509 | if (ret) |
| 1510 | return -1; |
| 1511 | |
| 1512 | /* Boot state changes after successful firmware download */ |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 1513 | if (card->usb_boot_state == USB8XXX_FW_DNLD) |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1514 | return -1; |
| 1515 | } |
| 1516 | |
| 1517 | ret = mwifiex_usb_rx_init(adapter); |
| 1518 | if (!ret) |
| 1519 | ret = mwifiex_usb_tx_init(adapter); |
| 1520 | |
| 1521 | return ret; |
| 1522 | } |
| 1523 | |
| 1524 | static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep) |
| 1525 | { |
| 1526 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 1527 | |
| 1528 | skb_push(card->rx_cmd.skb, INTF_HEADER_LEN); |
| 1529 | if ((ep == card->rx_cmd_ep) && |
| 1530 | (!atomic_read(&card->rx_cmd_urb_pending))) |
| 1531 | mwifiex_usb_submit_rx_urb(&card->rx_cmd, |
| 1532 | MWIFIEX_RX_CMD_BUF_SIZE); |
| 1533 | |
| 1534 | return; |
| 1535 | } |
| 1536 | |
| 1537 | static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter, |
| 1538 | struct sk_buff *skb) |
| 1539 | { |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1540 | mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT); |
| 1541 | |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1545 | /* This function wakes up the card. */ |
| 1546 | static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter) |
| 1547 | { |
| 1548 | /* Simulation of HS_AWAKE event */ |
| 1549 | adapter->pm_wakeup_fw_try = false; |
Amitkumar Karwar | 6e9344f | 2015-03-12 00:38:40 -0700 | [diff] [blame] | 1550 | del_timer(&adapter->wakeup_timer); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1551 | adapter->pm_wakeup_card_req = false; |
| 1552 | adapter->ps_state = PS_STATE_AWAKE; |
| 1553 | |
| 1554 | return 0; |
| 1555 | } |
| 1556 | |
Amitkumar Karwar | cf6a64f | 2014-11-05 17:04:29 +0530 | [diff] [blame] | 1557 | static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter) |
| 1558 | { |
| 1559 | struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; |
| 1560 | int i; |
| 1561 | struct urb_context *ctx; |
| 1562 | |
| 1563 | for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { |
| 1564 | if (card->rx_data_list[i].skb) |
| 1565 | continue; |
| 1566 | ctx = &card->rx_data_list[i]; |
| 1567 | mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE); |
| 1568 | } |
| 1569 | } |
| 1570 | |
Xinming Hu | 6175430 | 2014-12-23 19:14:08 +0530 | [diff] [blame] | 1571 | /* This function is called after the card has woken up. */ |
| 1572 | static inline int |
| 1573 | mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter) |
| 1574 | { |
| 1575 | return 0; |
| 1576 | } |
| 1577 | |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1578 | static struct mwifiex_if_ops usb_ops = { |
| 1579 | .register_dev = mwifiex_register_dev, |
Amitkumar Karwar | 2739a95 | 2013-07-22 19:17:51 -0700 | [diff] [blame] | 1580 | .unregister_dev = mwifiex_unregister_dev, |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1581 | .wakeup = mwifiex_pm_wakeup_card, |
| 1582 | .wakeup_complete = mwifiex_pm_wakeup_card_complete, |
| 1583 | |
| 1584 | /* USB specific */ |
| 1585 | .dnld_fw = mwifiex_usb_dnld_fw, |
| 1586 | .cmdrsp_complete = mwifiex_usb_cmd_event_complete, |
| 1587 | .event_complete = mwifiex_usb_cmd_event_complete, |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1588 | .host_to_card = mwifiex_usb_host_to_card, |
Amitkumar Karwar | cf6a64f | 2014-11-05 17:04:29 +0530 | [diff] [blame] | 1589 | .submit_rem_rx_urbs = mwifiex_usb_submit_rem_rx_urbs, |
Zhaoyang Liu | 7e4e5d2 | 2015-09-18 06:32:17 -0700 | [diff] [blame] | 1590 | .multi_port_resync = mwifiex_usb_port_resync, |
Zhaoyang Liu | 735ab6b | 2015-09-18 06:32:18 -0700 | [diff] [blame] | 1591 | .is_port_ready = mwifiex_usb_is_port_ready, |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1592 | }; |
| 1593 | |
Amitkumar Karwar | c0e6aa4 | 2016-12-01 18:38:20 +0530 | [diff] [blame] | 1594 | module_usb_driver(mwifiex_usb_driver); |
Amitkumar Karwar | 4daffe3 | 2012-04-18 20:08:28 -0700 | [diff] [blame] | 1595 | |
| 1596 | MODULE_AUTHOR("Marvell International Ltd."); |
| 1597 | MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION); |
| 1598 | MODULE_VERSION(USB_VERSION); |
| 1599 | MODULE_LICENSE("GPL v2"); |
Kristian Evensen | 9e6f3f4 | 2014-11-20 16:52:58 +0100 | [diff] [blame] | 1600 | MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME); |
Yogesh Ashok Powar | 68458de | 2014-01-09 19:36:55 -0800 | [diff] [blame] | 1601 | MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME); |
Yogesh Ashok Powar | eaa3d9f | 2015-01-23 17:09:18 +0530 | [diff] [blame] | 1602 | MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME); |
Zhaoyang Liu | 6d85ef0 | 2015-08-05 06:09:40 -0700 | [diff] [blame] | 1603 | MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME); |