blob: 6e3cf9817730b53f751f31401425ffd1c1dc82a4 [file] [log] [blame]
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001/*
2 * Marvell Wireless LAN device driver: USB specific handling
3 *
Xinming Hu65da33f2014-06-19 21:38:57 -07004 * Copyright (C) 2012-2014, Marvell International Ltd.
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07005 *
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 Karwar4daffe32012-04-18 20:08:28 -070025static struct mwifiex_if_ops usb_ops;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070026
Arvind Yadav7516dbd2017-08-09 21:57:59 +053027static const struct usb_device_id mwifiex_usb_table[] = {
Kristian Evensen9e6f3f42014-11-20 16:52:58 +010028 /* 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 Powar68458de2014-01-09 19:36:55 -080033 /* 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 Powareaa3d9f2015-01-23 17:09:18 +053038 /* 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 Liu6d85ef02015-08-05 06:09:40 -070043 /* 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 Karwar4daffe32012-04-18 20:08:28 -070048 { } /* Terminating entry */
49};
50
51MODULE_DEVICE_TABLE(usb, mwifiex_usb_table);
52
53static 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 */
58static int mwifiex_usb_recv(struct mwifiex_adapter *adapter,
59 struct sk_buff *skb, u8 ep)
60{
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070061 u32 recv_type;
62 __le32 tmp;
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -070063 int ret;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070064
65 if (adapter->hs_activated)
66 mwifiex_process_hs_config(adapter);
67
68 if (skb->len < INTF_HEADER_LEN) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +053069 mwifiex_dbg(adapter, ERROR,
70 "%s: invalid skb->len\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070071 return -1;
72 }
73
74 switch (ep) {
75 case MWIFIEX_USB_EP_CMD_EVENT:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +053076 mwifiex_dbg(adapter, EVENT,
77 "%s: EP_CMD_EVENT\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070078 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 Liuacebe8c2015-05-12 00:48:20 +053085 mwifiex_dbg(adapter, ERROR,
86 "CMD: skb->len too large\n");
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -070087 ret = -1;
88 goto exit_restore_skb;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070089 } else if (!adapter->curr_cmd) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +053090 mwifiex_dbg(adapter, WARN, "CMD: no curr_cmd\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070091 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
92 mwifiex_process_sleep_confirm_resp(
93 adapter, skb->data,
94 skb->len);
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -070095 ret = 0;
96 goto exit_restore_skb;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070097 }
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -070098 ret = -1;
99 goto exit_restore_skb;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700100 }
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 Liuacebe8c2015-05-12 00:48:20 +0530107 mwifiex_dbg(adapter, ERROR,
108 "EVENT: skb->len too small\n");
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -0700109 ret = -1;
110 goto exit_restore_skb;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700111 }
112 skb_copy_from_linear_data(skb, &tmp, sizeof(u32));
113 adapter->event_cause = le32_to_cpu(tmp);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530114 mwifiex_dbg(adapter, EVENT,
115 "event_cause %#x\n", adapter->event_cause);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700116
117 if (skb->len > MAX_EVENT_SIZE) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530118 mwifiex_dbg(adapter, ERROR,
119 "EVENT: event body too large\n");
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -0700120 ret = -1;
121 goto exit_restore_skb;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700122 }
123
Amitkumar Karware80c81d2012-06-20 20:21:12 -0700124 memcpy(adapter->event_body, skb->data +
125 MWIFIEX_EVENT_HEADER_LEN, skb->len);
126
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700127 adapter->event_received = true;
128 adapter->event_skb = skb;
129 break;
130 default:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530131 mwifiex_dbg(adapter, ERROR,
132 "unknown recv_type %#x\n", recv_type);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700133 return -1;
134 }
135 break;
136 case MWIFIEX_USB_EP_DATA:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530137 mwifiex_dbg(adapter, DATA, "%s: EP_DATA\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700138 if (skb->len > MWIFIEX_RX_DATA_BUF_SIZE) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530139 mwifiex_dbg(adapter, ERROR,
140 "DATA: skb->len too large\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700141 return -1;
142 }
Avinash Patilec4a16b2014-11-05 17:04:27 +0530143
144 skb_queue_tail(&adapter->rx_data_q, skb);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700145 adapter->data_received = true;
Avinash Patilec4a16b2014-11-05 17:04:27 +0530146 atomic_inc(&adapter->rx_pending);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700147 break;
148 default:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530149 mwifiex_dbg(adapter, ERROR,
150 "%s: unknown endport %#x\n", __func__, ep);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700151 return -1;
152 }
153
154 return -EINPROGRESS;
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -0700155
156exit_restore_skb:
157 /* The buffer will be reused for further cmds/events */
158 skb_push(skb, INTF_HEADER_LEN);
159
160 return ret;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700161}
162
163static 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 Liuacebe8c2015-05-12 00:48:20 +0530185 mwifiex_dbg(adapter, ERROR,
186 "URB status is failed: %d\n", urb->status);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700187 /* 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 Karwar4daffe32012-04-18 20:08:28 -0700197 status = mwifiex_usb_recv(adapter, skb, context->ep);
198
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530199 mwifiex_dbg(adapter, INFO,
200 "info: recv_length=%d, status=%d\n",
201 recv_length, status);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700202 if (status == -EINPROGRESS) {
Shengzhen Lib2713f62015-03-13 17:37:54 +0530203 mwifiex_queue_main_work(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700204
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 Karwar4daffe32012-04-18 20:08:28 -0700212 if (status == -1)
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530213 mwifiex_dbg(adapter, ERROR,
214 "received data processing failed!\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700215
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 Liuacebe8c2015-05-12 00:48:20 +0530222 mwifiex_dbg(adapter, FATAL,
223 "Card is removed: %d\n", urb->status);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700224 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
236setup_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 Karwarcf6a64f2014-11-05 17:04:29 +0530242 if (card->rx_cmd_ep == context->ep) {
243 mwifiex_usb_submit_rx_urb(context, size);
244 } else {
Ujjal Roy354a1942016-02-25 00:49:56 +0530245 if (atomic_read(&adapter->rx_pending) <= HIGH_RX_PENDING) {
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +0530246 mwifiex_usb_submit_rx_urb(context, size);
Ujjal Roy354a1942016-02-25 00:49:56 +0530247 } else {
Reyad Attiyat11bdc442015-06-28 20:07:38 -0500248 context->skb = NULL;
249 }
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +0530250 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700251
252 return;
253}
254
255static 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 Liu308fe292015-09-18 06:32:16 -0700260 struct usb_tx_data_port *port;
261 int i;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700262
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530263 mwifiex_dbg(adapter, INFO,
264 "%s: status: %d\n", __func__, urb->status);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700265
266 if (context->ep == card->tx_cmd_ep) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530267 mwifiex_dbg(adapter, CMD,
268 "%s: CMD\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700269 atomic_dec(&card->tx_cmd_urb_pending);
270 adapter->cmd_sent = false;
271 } else {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530272 mwifiex_dbg(adapter, DATA,
273 "%s: DATA\n", __func__);
Cathy Luo1afac192016-09-20 20:49:02 +0530274 mwifiex_write_data_complete(adapter, context->skb, 0,
275 urb->status ? -1 : 0);
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700276 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 Liu735ab6b2015-09-18 06:32:18 -0700280 port->block_status = false;
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700281 break;
282 }
283 }
Zhaoyang Liud9414442015-09-18 06:32:14 -0700284 adapter->data_sent = false;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700285 }
286
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700287 if (card->mc_resync_flag)
288 mwifiex_multi_chan_resync(adapter);
289
Shengzhen Lib2713f62015-03-13 17:37:54 +0530290 mwifiex_queue_main_work(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700291
292 return;
293}
294
295static 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 Liuacebe8c2015-05-12 00:48:20 +0530303 mwifiex_dbg(adapter, ERROR,
304 "%s: dev_alloc_skb failed\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700305 return -ENOMEM;
306 }
307 }
308
Ganapathi Bhat182f5692017-03-27 18:27:44 +0530309 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 Karwar4daffe32012-04-18 20:08:28 -0700320
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 Liuacebe8c2015-05-12 00:48:20 +0530327 mwifiex_dbg(adapter, ERROR, "usb_submit_urb failed\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700328 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
342static void mwifiex_usb_free(struct usb_card_rec *card)
343{
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700344 struct usb_tx_data_port *port;
345 int i, j;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700346
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 Liu308fe292015-09-18 06:32:16 -0700363 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 Hu42d1abb2017-05-23 07:12:31 +0000366 usb_kill_urb(port->tx_data_list[j].urb);
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700367 usb_free_urb(port->tx_data_list[j].urb);
368 port->tx_data_list[j].urb = NULL;
369 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700370 }
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 */
382static 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 Ruchandani41338282016-11-23 17:27:10 -0800390 u16 id_vendor, id_product, bcd_device;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700391
Brian Norris66b9c182016-10-26 16:29:20 -0700392 card = devm_kzalloc(&intf->dev, sizeof(*card), GFP_KERNEL);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700393 if (!card)
394 return -ENOMEM;
395
Brian Norris4a79aa12016-11-18 19:30:26 +0530396 init_completion(&card->fw_done);
397
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700398 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 Karwar4daffe32012-04-18 20:08:28 -0700401 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 Powar68458de2014-01-09 19:36:55 -0800405 switch (id_product) {
Kristian Evensen9e6f3f42014-11-20 16:52:58 +0100406 case USB8766_PID_1:
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800407 case USB8797_PID_1:
Yogesh Ashok Powareaa3d9f2015-01-23 17:09:18 +0530408 case USB8801_PID_1:
Zhaoyang Liu6d85ef02015-08-05 06:09:40 -0700409 case USB8997_PID_1:
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800410 card->usb_boot_state = USB8XXX_FW_DNLD;
411 break;
Kristian Evensen9e6f3f42014-11-20 16:52:58 +0100412 case USB8766_PID_2:
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800413 case USB8797_PID_2:
Yogesh Ashok Powareaa3d9f2015-01-23 17:09:18 +0530414 case USB8801_PID_2:
Zhaoyang Liu6d85ef02015-08-05 06:09:40 -0700415 case USB8997_PID_2:
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800416 card->usb_boot_state = USB8XXX_FW_READY;
417 break;
418 default:
Joe Perchesfe3881c2014-09-09 20:27:44 -0700419 pr_warn("unknown id_product %#x\n", id_product);
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800420 card->usb_boot_state = USB8XXX_FW_DNLD;
421 break;
422 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700423
424 card->udev = udev;
425 card->intf = intf;
426
Masanari Iida69797832012-11-30 22:08:31 +0900427 pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
Johan Hovolda1ad7192017-05-12 12:15:55 +0200428 le16_to_cpu(udev->descriptor.bcdUSB),
429 udev->descriptor.bDeviceClass,
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700430 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 Bhat182f5692017-03-27 18:27:44 +0530437 (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 Karwar4daffe32012-04-18 20:08:28 -0700442 le16_to_cpu(epd->wMaxPacketSize),
Ganapathi Bhat182f5692017-03-27 18:27:44 +0530443 epd->bEndpointAddress, card->rx_cmd_ep_type);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700444 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 Liu308fe292015-09-18 06:32:16 -0700462 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 Karwar4daffe32012-04-18 20:08:28 -0700474 }
475 if (usb_endpoint_dir_out(epd) &&
476 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
Ganapathi Bhat182f5692017-03-27 18:27:44 +0530477 (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 Karwar4daffe32012-04-18 20:08:28 -0700481 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
482 le16_to_cpu(epd->wMaxPacketSize),
483 epd->bEndpointAddress);
Ganapathi Bhat182f5692017-03-27 18:27:44 +0530484 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 Karwar4daffe32012-04-18 20:08:28 -0700487 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 Norris4a79aa12016-11-18 19:30:26 +0530496 ret = mwifiex_add_card(card, &card->fw_done, &usb_ops,
Rajat Jain2e02b582016-11-15 19:06:02 +0530497 MWIFIEX_USB, &card->udev->dev);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700498 if (ret) {
499 pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret);
500 usb_reset_device(udev);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700501 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 */
516static 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 Liu308fe292015-09-18 06:32:16 -0700520 struct usb_tx_data_port *port;
521 int i, j;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700522
Brian Norrisb42dbb22016-11-18 19:30:29 +0530523 /* Might still be loading firmware */
524 wait_for_completion(&card->fw_done);
525
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700526 adapter = card->adapter;
Brian Norrisb42dbb22016-11-18 19:30:29 +0530527 if (!adapter) {
528 dev_err(&intf->dev, "card is not valid\n");
529 return 0;
530 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700531
532 if (unlikely(adapter->is_suspended))
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530533 mwifiex_dbg(adapter, WARN,
534 "Device already suspended\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700535
Brian Norris58b70332016-11-18 19:30:31 +0530536 /* 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 Karwar4daffe32012-04-18 20:08:28 -0700544
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 Karwarc0dbba62014-03-25 19:01:20 -0700552 adapter->hs_enabling = false;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700553
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700554 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 Liu308fe292015-09-18 06:32:16 -0700562 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 Karwar4daffe32012-04-18 20:08:28 -0700569
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 */
583static 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 Norrise98fb112016-11-18 19:30:33 +0530589 if (!card->adapter) {
590 dev_err(&intf->dev, "%s: card->adapter is NULL\n",
591 __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700592 return 0;
593 }
594 adapter = card->adapter;
595
596 if (unlikely(!adapter->is_suspended)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530597 mwifiex_dbg(adapter, WARN,
598 "Device already resumed\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700599 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 Karwar4daffe32012-04-18 20:08:28 -0700619 /* 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 Karwar4daffe32012-04-18 20:08:28 -0700625 return 0;
626}
627
628static void mwifiex_usb_disconnect(struct usb_interface *intf)
629{
630 struct usb_card_rec *card = usb_get_intfdata(intf);
Amitkumar Karwar3fffd7c2014-04-14 15:32:54 -0700631 struct mwifiex_adapter *adapter;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700632
Brian Norris4a79aa12016-11-18 19:30:26 +0530633 wait_for_completion(&card->fw_done);
634
Amitkumar Karwar3fffd7c2014-04-14 15:32:54 -0700635 adapter = card->adapter;
Brian Norris4a79aa12016-11-18 19:30:26 +0530636 if (!adapter || !adapter->priv_num)
Amitkumar Karwar3fffd7c2014-04-14 15:32:54 -0700637 return;
638
Xinming Hu045f0c12016-11-30 20:22:17 +0530639 if (card->udev->state != USB_STATE_NOTATTACHED && !adapter->mfg_mode) {
Amitkumar Karwar3fffd7c2014-04-14 15:32:54 -0700640 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 Bhatb817047a2018-05-24 19:18:27 +0530647 if (adapter->workqueue)
648 flush_workqueue(adapter->workqueue);
649
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700650 mwifiex_usb_free(card);
651
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530652 mwifiex_dbg(adapter, FATAL,
653 "%s: removing card\n", __func__);
Brian Norris4a79aa12016-11-18 19:30:26 +0530654 mwifiex_remove_card(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700655
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700656 usb_put_dev(interface_to_usbdev(intf));
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700657}
658
Arend Van Spriel21c5c832018-05-16 14:12:00 +0200659static 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 Bhat618fd1e2018-05-24 16:36:28 +0530664 mwifiex_fw_dump_event(mwifiex_get_priv(card->adapter,
665 MWIFIEX_BSS_ROLE_ANY));
Arend Van Spriel21c5c832018-05-16 14:12:00 +0200666}
667
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700668static struct usb_driver mwifiex_usb_driver = {
Bing Zhaoe4ceb0f2014-02-21 12:23:00 -0800669 .name = "mwifiex_usb",
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700670 .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 Karwar3fffd7c2014-04-14 15:32:54 -0700675 .soft_unbind = 1,
Arend Van Spriel21c5c832018-05-16 14:12:00 +0200676 .drvwrap.driver = {
677 .coredump = mwifiex_usb_coredump,
678 },
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700679};
680
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700681static 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 Liuacebe8c2015-05-12 00:48:20 +0530694 mwifiex_dbg(adapter, ERROR,
695 "usb_bulk_msg for tx failed: %d\n", ret);
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800696 return ret;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700697 }
698
699 *len = actual_length;
700
701 return ret;
702}
703
704static 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 Liuacebe8c2015-05-12 00:48:20 +0530714 mwifiex_dbg(adapter, ERROR,
715 "usb_bulk_msg for rx failed: %d\n", ret);
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800716 return ret;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700717 }
718
719 *len = actual_length;
720
721 return ret;
722}
723
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700724static 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 Liu735ab6b2015-09-18 06:32:18 -0700771static 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
784static 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 Huc5994292017-05-23 07:12:32 +0000796static 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 Karwar4daffe32012-04-18 20:08:28 -0700800{
801 struct usb_card_rec *card = adapter->card;
Xinming Huc5994292017-05-23 07:12:32 +0000802 int ret = -EINPROGRESS;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700803 struct urb *tx_urb;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700804
805 context->adapter = adapter;
806 context->ep = ep;
Xinming Huc5994292017-05-23 07:12:32 +0000807 context->skb = skb_send;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700808 tx_urb = context->urb;
809
Ganapathi Bhat182f5692017-03-27 18:27:44 +0530810 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 Huc5994292017-05-23 07:12:32 +0000813 usb_sndintpipe(card->udev, ep), skb_send->data,
814 skb_send->len, mwifiex_usb_tx_complete,
Ganapathi Bhat182f5692017-03-27 18:27:44 +0530815 (void *)context, card->tx_cmd_interval);
816 else
817 usb_fill_bulk_urb(tx_urb, card->udev,
Xinming Huc5994292017-05-23 07:12:32 +0000818 usb_sndbulkpipe(card->udev, ep),
819 skb_send->data, skb_send->len,
820 mwifiex_usb_tx_complete, (void *)context);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700821
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 Liu308fe292015-09-18 06:32:16 -0700827 atomic_inc(&port->tx_data_urb_pending);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700828
Cathy Luo5476f802016-09-20 20:49:03 +0530829 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 Karwarac3b5612016-09-20 20:49:04 +0530834 ret = -ENOSR;
Cathy Luo5476f802016-09-20 20:49:03 +0530835 }
836
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700837 if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530838 mwifiex_dbg(adapter, ERROR,
839 "%s: usb_submit_urb failed\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700840 if (ep == card->tx_cmd_ep) {
841 atomic_dec(&card->tx_cmd_urb_pending);
842 } else {
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700843 atomic_dec(&port->tx_data_urb_pending);
Zhaoyang Liu735ab6b2015-09-18 06:32:18 -0700844 port->block_status = false;
Cathy Luo5476f802016-09-20 20:49:03 +0530845 adapter->data_sent = false;
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700846 if (port->tx_data_ix)
847 port->tx_data_ix--;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700848 else
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700849 port->tx_data_ix = MWIFIEX_TX_DATA_URB;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700850 }
Amitkumar Karwarac3b5612016-09-20 20:49:04 +0530851 ret = -1;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700852 }
853
Zhaoyang Liu735ab6b2015-09-18 06:32:18 -0700854 return ret;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700855}
856
Xinming Huc5994292017-05-23 07:12:32 +0000857static 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 Hua2ca85a2017-05-23 07:12:33 +0000867 /* 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 Huc5994292017-05-23 07:12:32 +0000876 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 */
942static 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 Hua2ca85a2017-05-23 07:12:33 +0000957 u32 timeout;
Xinming Huc5994292017-05-23 07:12:32 +0000958 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 Hua2ca85a2017-05-23 07:12:33 +00001028 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 Huc5994292017-05-23 07:12:32 +00001054 }
1055
Xinming Hua2ca85a2017-05-23 07:12:33 +00001056send_aggr_buf:
Xinming Huc5994292017-05-23 07:12:32 +00001057 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
1095postcopy_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 Hua2ca85a2017-05-23 07:12:33 +00001100 /* 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 Huc5994292017-05-23 07:12:32 +00001109 }
1110
1111 return -EINPROGRESS;
1112}
1113
Kees Cook08c2eb82017-10-24 02:29:19 -07001114static void mwifiex_usb_tx_aggr_tmo(struct timer_list *t)
Xinming Hua2ca85a2017-05-23 07:12:33 +00001115{
1116 struct urb_context *urb_cnxt = NULL;
1117 struct sk_buff *skb_send = NULL;
1118 struct tx_aggr_tmr_cnxt *timer_context =
Kees Cook08c2eb82017-10-24 02:29:19 -07001119 from_timer(timer_context, t, hold_timer);
Xinming Hua2ca85a2017-05-23 07:12:33 +00001120 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 Carpenterfe0c94b2017-07-10 10:21:15 +03001130 goto unlock;
Xinming Hua2ca85a2017-05-23 07:12:33 +00001131 }
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);
1148done:
1149 if (err == -1)
1150 mwifiex_write_data_complete(adapter, skb_send, 0, -1);
Dan Carpenterfe0c94b2017-07-10 10:21:15 +03001151unlock:
Xinming Hua2ca85a2017-05-23 07:12:33 +00001152 spin_unlock_irqrestore(&port->tx_aggr_lock, flags);
1153}
1154
Xinming Huc5994292017-05-23 07:12:32 +00001155/* This function write a command/data packet to card. */
1156static 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 Hua2ca85a2017-05-23 07:12:33 +00001163 unsigned long flags;
1164 int idx, ret;
Xinming Huc5994292017-05-23 07:12:32 +00001165
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 Hua2ca85a2017-05-23 07:12:33 +00001204 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 Huc5994292017-05-23 07:12:32 +00001207 tx_param, port);
Xinming Hua2ca85a2017-05-23 07:12:33 +00001208 spin_unlock_irqrestore(&port->tx_aggr_lock, flags);
1209 return ret;
1210 }
Xinming Huc5994292017-05-23 07:12:32 +00001211
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 Hua2ca85a2017-05-23 07:12:33 +00001218static 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 Cook08c2eb82017-10-24 02:29:19 -07001254 timer_setup(&port->tx_aggr.timer_cnxt.hold_timer,
1255 mwifiex_usb_tx_aggr_tmo, 0);
Xinming Hua2ca85a2017-05-23 07:12:33 +00001256 }
1257
1258 return 0;
1259}
1260
1261static 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 Karwar4daffe32012-04-18 20:08:28 -07001295/* This function register usb device and initialize parameter. */
1296static 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 Karwar4daffe32012-04-18 20:08:28 -07001301
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -08001302 switch (le16_to_cpu(card->udev->descriptor.idProduct)) {
Zhaoyang Liu6d85ef02015-08-05 06:09:40 -07001303 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 Evensen9e6f3f42014-11-20 16:52:58 +01001309 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 Patil1fe192d2015-01-23 17:09:19 +05301313 adapter->ext_scan = true;
Kristian Evensen9e6f3f42014-11-20 16:52:58 +01001314 break;
Yogesh Ashok Powareaa3d9f2015-01-23 17:09:18 +05301315 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 Patil1fe192d2015-01-23 17:09:19 +05301319 adapter->ext_scan = false;
Yogesh Ashok Powareaa3d9f2015-01-23 17:09:18 +05301320 break;
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -08001321 case USB8797_PID_1:
1322 case USB8797_PID_2:
1323 default:
Amitkumar Karwar828cf222014-02-27 19:35:13 -08001324 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -08001325 strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
1326 break;
1327 }
1328
Zhaoyang Liu2b0f9972015-09-18 06:32:15 -07001329 adapter->usb_mc_status = false;
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -07001330 adapter->usb_mc_setup = false;
Zhaoyang Liu2b0f9972015-09-18 06:32:15 -07001331
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001332 return 0;
1333}
1334
Xinming Huc5994292017-05-23 07:12:32 +00001335static 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 Hua2ca85a2017-05-23 07:12:33 +00001342 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
1343 port = &card->port[idx];
1344 if (adapter->bus_aggr.enable)
Xinming Huc5994292017-05-23 07:12:32 +00001345 while ((skb_tmp =
1346 skb_dequeue(&port->tx_aggr.aggr_list)))
1347 mwifiex_write_data_complete(adapter, skb_tmp,
1348 0, -1);
Xinming Hua2ca85a2017-05-23 07:12:33 +00001349 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 Huc5994292017-05-23 07:12:32 +00001352 }
1353}
1354
Amitkumar Karwar2739a952013-07-22 19:17:51 -07001355static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1356{
1357 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1358
Xinming Hua2ca85a2017-05-23 07:12:33 +00001359 mwifiex_usb_cleanup_tx_aggr(adapter);
Xinming Huc5994292017-05-23 07:12:32 +00001360
Ujjal Roy353d2a62013-11-14 19:10:42 -08001361 card->adapter = NULL;
Amitkumar Karwar2739a952013-07-22 19:17:51 -07001362}
1363
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001364static 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 Carpenter394f0ed2016-04-19 07:23:44 -07001369 u32 retries = USB8XXX_FW_MAX_RETRY + 1;
1370 u32 dlen;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001371 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 Liuacebe8c2015-05-12 00:48:20 +05301377 mwifiex_dbg(adapter, ERROR,
1378 "No firmware image found! Terminating download\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001379 ret = -1;
1380 goto fw_exit;
1381 }
1382
1383 /* Allocate memory for transmit */
1384 fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
Dan Carpenter3b1f0e82015-08-04 10:49:27 +03001385 if (!fwdata) {
1386 ret = -ENOMEM;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001387 goto fw_exit;
Dan Carpenter3b1f0e82015-08-04 10:49:27 +03001388 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001389
1390 /* Allocate memory for receive */
1391 recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
Dan Carpentere0bdef02016-04-19 07:21:58 -07001392 if (!recv_buff) {
1393 ret = -ENOMEM;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001394 goto cleanup;
Dan Carpentere0bdef02016-04-19 07:21:58 -07001395 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001396
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 Levinfc40ca92013-01-02 16:23:01 -08001404 memcpy(&fwdata->fw_hdr, &firmware[tlen],
1405 sizeof(struct fw_header));
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001406
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 Bhat78776462016-09-12 18:55:27 +05301411 /* Command 7 doesn't have data length field */
1412 if (dnld_cmd == FW_CMD_7)
1413 dlen = 0;
1414
Sasha Levinfc40ca92013-01-02 16:23:01 -08001415 memcpy(fwdata->data, &firmware[tlen], dlen);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001416
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 Carpenter394f0ed2016-04-19 07:23:44 -07001422 while (--retries) {
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001423 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 Liuacebe8c2015-05-12 00:48:20 +05301431 mwifiex_dbg(adapter, ERROR,
1432 "write_data_sync: failed: %d\n",
1433 ret);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001434 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 Liuacebe8c2015-05-12 00:48:20 +05301445 mwifiex_dbg(adapter, ERROR,
1446 "read_data_sync: failed: %d\n",
1447 ret);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001448 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 Liuacebe8c2015-05-12 00:48:20 +05301457 mwifiex_dbg(adapter, WARN,
1458 "USB is not the winner %#x\n",
1459 sync_fw.cmd);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001460
1461 /* returning success */
1462 ret = 0;
1463 goto cleanup;
1464 }
1465
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301466 mwifiex_dbg(adapter, MSG,
1467 "start to download FW...\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001468
1469 check_winner = 0;
1470 break;
1471 }
1472
1473 /* check the firmware block response for CRC errors */
1474 if (sync_fw.cmd) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301475 mwifiex_dbg(adapter, ERROR,
1476 "FW received block with CRC %#x\n",
1477 sync_fw.cmd);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001478 ret = -1;
1479 continue;
1480 }
1481
Dan Carpenter394f0ed2016-04-19 07:23:44 -07001482 retries = USB8XXX_FW_MAX_RETRY + 1;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001483 break;
1484 }
1485 fw_seqnum++;
1486 } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
1487
1488cleanup:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301489 mwifiex_dbg(adapter, MSG,
1490 "info: FW download over, size %d bytes\n", tlen);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001491
1492 kfree(recv_buff);
1493 kfree(fwdata);
1494
1495 if (retries)
1496 ret = 0;
1497fw_exit:
1498 return ret;
1499}
1500
1501static 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 Powar68458de2014-01-09 19:36:55 -08001507 if (card->usb_boot_state == USB8XXX_FW_DNLD) {
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001508 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 Powar68458de2014-01-09 19:36:55 -08001513 if (card->usb_boot_state == USB8XXX_FW_DNLD)
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001514 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
1524static 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
1537static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
1538 struct sk_buff *skb)
1539{
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001540 mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
1541
1542 return 0;
1543}
1544
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001545/* This function wakes up the card. */
1546static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
1547{
1548 /* Simulation of HS_AWAKE event */
1549 adapter->pm_wakeup_fw_try = false;
Amitkumar Karwar6e9344f2015-03-12 00:38:40 -07001550 del_timer(&adapter->wakeup_timer);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001551 adapter->pm_wakeup_card_req = false;
1552 adapter->ps_state = PS_STATE_AWAKE;
1553
1554 return 0;
1555}
1556
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +05301557static 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 Hu61754302014-12-23 19:14:08 +05301571/* This function is called after the card has woken up. */
1572static inline int
1573mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
1574{
1575 return 0;
1576}
1577
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001578static struct mwifiex_if_ops usb_ops = {
1579 .register_dev = mwifiex_register_dev,
Amitkumar Karwar2739a952013-07-22 19:17:51 -07001580 .unregister_dev = mwifiex_unregister_dev,
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001581 .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 Karwar4daffe32012-04-18 20:08:28 -07001588 .host_to_card = mwifiex_usb_host_to_card,
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +05301589 .submit_rem_rx_urbs = mwifiex_usb_submit_rem_rx_urbs,
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -07001590 .multi_port_resync = mwifiex_usb_port_resync,
Zhaoyang Liu735ab6b2015-09-18 06:32:18 -07001591 .is_port_ready = mwifiex_usb_is_port_ready,
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001592};
1593
Amitkumar Karwarc0e6aa42016-12-01 18:38:20 +05301594module_usb_driver(mwifiex_usb_driver);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001595
1596MODULE_AUTHOR("Marvell International Ltd.");
1597MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1598MODULE_VERSION(USB_VERSION);
1599MODULE_LICENSE("GPL v2");
Kristian Evensen9e6f3f42014-11-20 16:52:58 +01001600MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME);
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -08001601MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME);
Yogesh Ashok Powareaa3d9f2015-01-23 17:09:18 +05301602MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME);
Zhaoyang Liu6d85ef02015-08-05 06:09:40 -07001603MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME);