1 /*
2  * NXP Wireless LAN device driver: USB specific handling
3  *
4  * Copyright 2011-2020 NXP
5  *
6  * This software file (the "File") is distributed by NXP
7  * 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 
25 static struct mwifiex_if_ops usb_ops;
26 
27 static const struct usb_device_id mwifiex_usb_table[] = {
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)},
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)},
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)},
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)},
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 {
61 	u32 recv_type;
62 	__le32 tmp;
63 	int ret;
64 
65 	if (adapter->hs_activated)
66 		mwifiex_process_hs_config(adapter);
67 
68 	if (skb->len < INTF_HEADER_LEN) {
69 		mwifiex_dbg(adapter, ERROR,
70 			    "%s: invalid skb->len\n", __func__);
71 		return -1;
72 	}
73 
74 	switch (ep) {
75 	case MWIFIEX_USB_EP_CMD_EVENT:
76 		mwifiex_dbg(adapter, EVENT,
77 			    "%s: EP_CMD_EVENT\n", __func__);
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) {
85 				mwifiex_dbg(adapter, ERROR,
86 					    "CMD: skb->len too large\n");
87 				ret = -1;
88 				goto exit_restore_skb;
89 			} else if (!adapter->curr_cmd) {
90 				mwifiex_dbg(adapter, WARN, "CMD: no curr_cmd\n");
91 				if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
92 					mwifiex_process_sleep_confirm_resp(
93 							adapter, skb->data,
94 							skb->len);
95 					ret = 0;
96 					goto exit_restore_skb;
97 				}
98 				ret = -1;
99 				goto exit_restore_skb;
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)) {
107 				mwifiex_dbg(adapter, ERROR,
108 					    "EVENT: skb->len too small\n");
109 				ret = -1;
110 				goto exit_restore_skb;
111 			}
112 			skb_copy_from_linear_data(skb, &tmp, sizeof(u32));
113 			adapter->event_cause = le32_to_cpu(tmp);
114 			mwifiex_dbg(adapter, EVENT,
115 				    "event_cause %#x\n", adapter->event_cause);
116 
117 			if (skb->len > MAX_EVENT_SIZE) {
118 				mwifiex_dbg(adapter, ERROR,
119 					    "EVENT: event body too large\n");
120 				ret = -1;
121 				goto exit_restore_skb;
122 			}
123 
124 			memcpy(adapter->event_body, skb->data +
125 			       MWIFIEX_EVENT_HEADER_LEN, skb->len);
126 
127 			adapter->event_received = true;
128 			adapter->event_skb = skb;
129 			break;
130 		default:
131 			mwifiex_dbg(adapter, ERROR,
132 				    "unknown recv_type %#x\n", recv_type);
133 			return -1;
134 		}
135 		break;
136 	case MWIFIEX_USB_EP_DATA:
137 		mwifiex_dbg(adapter, DATA, "%s: EP_DATA\n", __func__);
138 		if (skb->len > MWIFIEX_RX_DATA_BUF_SIZE) {
139 			mwifiex_dbg(adapter, ERROR,
140 				    "DATA: skb->len too large\n");
141 			return -1;
142 		}
143 
144 		skb_queue_tail(&adapter->rx_data_q, skb);
145 		adapter->data_received = true;
146 		atomic_inc(&adapter->rx_pending);
147 		break;
148 	default:
149 		mwifiex_dbg(adapter, ERROR,
150 			    "%s: unknown endport %#x\n", __func__, ep);
151 		return -1;
152 	}
153 
154 	return -EINPROGRESS;
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;
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 ||
185 		    test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags)) {
186 			mwifiex_dbg(adapter, ERROR,
187 				    "URB status is failed: %d\n", urb->status);
188 			/* Do not free skb in case of command ep */
189 			if (card->rx_cmd_ep != context->ep)
190 				dev_kfree_skb_any(skb);
191 			goto setup_for_next;
192 		}
193 		if (skb->len > recv_length)
194 			skb_trim(skb, recv_length);
195 		else
196 			skb_put(skb, recv_length - skb->len);
197 
198 		status = mwifiex_usb_recv(adapter, skb, context->ep);
199 
200 		mwifiex_dbg(adapter, INFO,
201 			    "info: recv_length=%d, status=%d\n",
202 			    recv_length, status);
203 		if (status == -EINPROGRESS) {
204 			mwifiex_queue_main_work(adapter);
205 
206 			/* urb for data_ep is re-submitted now;
207 			 * urb for cmd_ep will be re-submitted in callback
208 			 * mwifiex_usb_recv_complete
209 			 */
210 			if (card->rx_cmd_ep == context->ep)
211 				return;
212 		} else {
213 			if (status == -1)
214 				mwifiex_dbg(adapter, ERROR,
215 					    "received data processing failed!\n");
216 
217 			/* Do not free skb in case of command ep */
218 			if (card->rx_cmd_ep != context->ep)
219 				dev_kfree_skb_any(skb);
220 		}
221 	} else if (urb->status) {
222 		if (!test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) {
223 			mwifiex_dbg(adapter, FATAL,
224 				    "Card is removed: %d\n", urb->status);
225 			set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
226 		}
227 		dev_kfree_skb_any(skb);
228 		return;
229 	} else {
230 		/* Do not free skb in case of command ep */
231 		if (card->rx_cmd_ep != context->ep)
232 			dev_kfree_skb_any(skb);
233 
234 		/* fall through setup_for_next */
235 	}
236 
237 setup_for_next:
238 	if (card->rx_cmd_ep == context->ep)
239 		size = MWIFIEX_RX_CMD_BUF_SIZE;
240 	else
241 		size = MWIFIEX_RX_DATA_BUF_SIZE;
242 
243 	if (card->rx_cmd_ep == context->ep) {
244 		mwifiex_usb_submit_rx_urb(context, size);
245 	} else {
246 		if (atomic_read(&adapter->rx_pending) <= HIGH_RX_PENDING) {
247 			mwifiex_usb_submit_rx_urb(context, size);
248 		} else {
249 			context->skb = NULL;
250 		}
251 	}
252 
253 	return;
254 }
255 
256 static void mwifiex_usb_tx_complete(struct urb *urb)
257 {
258 	struct urb_context *context = (struct urb_context *)(urb->context);
259 	struct mwifiex_adapter *adapter = context->adapter;
260 	struct usb_card_rec *card = adapter->card;
261 	struct usb_tx_data_port *port;
262 	int i;
263 
264 	mwifiex_dbg(adapter, INFO,
265 		    "%s: status: %d\n", __func__, urb->status);
266 
267 	if (context->ep == card->tx_cmd_ep) {
268 		mwifiex_dbg(adapter, CMD,
269 			    "%s: CMD\n", __func__);
270 		atomic_dec(&card->tx_cmd_urb_pending);
271 		adapter->cmd_sent = false;
272 	} else {
273 		mwifiex_dbg(adapter, DATA,
274 			    "%s: DATA\n", __func__);
275 		mwifiex_write_data_complete(adapter, context->skb, 0,
276 					    urb->status ? -1 : 0);
277 		for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
278 			port = &card->port[i];
279 			if (context->ep == port->tx_data_ep) {
280 				atomic_dec(&port->tx_data_urb_pending);
281 				port->block_status = false;
282 				break;
283 			}
284 		}
285 		adapter->data_sent = false;
286 	}
287 
288 	if (card->mc_resync_flag)
289 		mwifiex_multi_chan_resync(adapter);
290 
291 	mwifiex_queue_main_work(adapter);
292 
293 	return;
294 }
295 
296 static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size)
297 {
298 	struct mwifiex_adapter *adapter = ctx->adapter;
299 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
300 
301 	if (test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) {
302 		if (card->rx_cmd_ep == ctx->ep) {
303 			mwifiex_dbg(adapter, INFO, "%s: free rx_cmd skb\n",
304 				    __func__);
305 			dev_kfree_skb_any(ctx->skb);
306 			ctx->skb = NULL;
307 		}
308 		mwifiex_dbg(adapter, ERROR,
309 			    "%s: card removed/suspended, EP %d rx_cmd URB submit skipped\n",
310 			    __func__, ctx->ep);
311 		return -1;
312 	}
313 
314 	if (card->rx_cmd_ep != ctx->ep) {
315 		ctx->skb = dev_alloc_skb(size);
316 		if (!ctx->skb) {
317 			mwifiex_dbg(adapter, ERROR,
318 				    "%s: dev_alloc_skb failed\n", __func__);
319 			return -ENOMEM;
320 		}
321 	}
322 
323 	if (card->rx_cmd_ep == ctx->ep &&
324 	    card->rx_cmd_ep_type == USB_ENDPOINT_XFER_INT)
325 		usb_fill_int_urb(ctx->urb, card->udev,
326 				 usb_rcvintpipe(card->udev, ctx->ep),
327 				 ctx->skb->data, size, mwifiex_usb_rx_complete,
328 				 (void *)ctx, card->rx_cmd_interval);
329 	else
330 		usb_fill_bulk_urb(ctx->urb, card->udev,
331 				  usb_rcvbulkpipe(card->udev, ctx->ep),
332 				  ctx->skb->data, size, mwifiex_usb_rx_complete,
333 				  (void *)ctx);
334 
335 	if (card->rx_cmd_ep == ctx->ep)
336 		atomic_inc(&card->rx_cmd_urb_pending);
337 	else
338 		atomic_inc(&card->rx_data_urb_pending);
339 
340 	if (usb_submit_urb(ctx->urb, GFP_ATOMIC)) {
341 		mwifiex_dbg(adapter, ERROR, "usb_submit_urb failed\n");
342 		dev_kfree_skb_any(ctx->skb);
343 		ctx->skb = NULL;
344 
345 		if (card->rx_cmd_ep == ctx->ep)
346 			atomic_dec(&card->rx_cmd_urb_pending);
347 		else
348 			atomic_dec(&card->rx_data_urb_pending);
349 
350 		return -1;
351 	}
352 
353 	return 0;
354 }
355 
356 static void mwifiex_usb_free(struct usb_card_rec *card)
357 {
358 	struct usb_tx_data_port *port;
359 	int i, j;
360 
361 	if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
362 		usb_kill_urb(card->rx_cmd.urb);
363 
364 	usb_free_urb(card->rx_cmd.urb);
365 	card->rx_cmd.urb = NULL;
366 
367 	if (atomic_read(&card->rx_data_urb_pending))
368 		for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
369 			if (card->rx_data_list[i].urb)
370 				usb_kill_urb(card->rx_data_list[i].urb);
371 
372 	for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
373 		usb_free_urb(card->rx_data_list[i].urb);
374 		card->rx_data_list[i].urb = NULL;
375 	}
376 
377 	for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
378 		port = &card->port[i];
379 		for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
380 			usb_kill_urb(port->tx_data_list[j].urb);
381 			usb_free_urb(port->tx_data_list[j].urb);
382 			port->tx_data_list[j].urb = NULL;
383 		}
384 	}
385 
386 	usb_free_urb(card->tx_cmd.urb);
387 	card->tx_cmd.urb = NULL;
388 
389 	return;
390 }
391 
392 /* This function probes an mwifiex device and registers it. It allocates
393  * the card structure, initiates the device registration and initialization
394  * procedure by adding a logical interface.
395  */
396 static int mwifiex_usb_probe(struct usb_interface *intf,
397 			     const struct usb_device_id *id)
398 {
399 	struct usb_device *udev = interface_to_usbdev(intf);
400 	struct usb_host_interface *iface_desc = intf->cur_altsetting;
401 	struct usb_endpoint_descriptor *epd;
402 	int ret, i;
403 	struct usb_card_rec *card;
404 	u16 id_vendor, id_product, bcd_device;
405 
406 	card = devm_kzalloc(&intf->dev, sizeof(*card), GFP_KERNEL);
407 	if (!card)
408 		return -ENOMEM;
409 
410 	init_completion(&card->fw_done);
411 
412 	id_vendor = le16_to_cpu(udev->descriptor.idVendor);
413 	id_product = le16_to_cpu(udev->descriptor.idProduct);
414 	bcd_device = le16_to_cpu(udev->descriptor.bcdDevice);
415 	pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n",
416 		 id_vendor, id_product, bcd_device);
417 
418 	/* PID_1 is used for firmware downloading only */
419 	switch (id_product) {
420 	case USB8766_PID_1:
421 	case USB8797_PID_1:
422 	case USB8801_PID_1:
423 	case USB8997_PID_1:
424 		card->usb_boot_state = USB8XXX_FW_DNLD;
425 		break;
426 	case USB8766_PID_2:
427 	case USB8797_PID_2:
428 	case USB8801_PID_2:
429 	case USB8997_PID_2:
430 		card->usb_boot_state = USB8XXX_FW_READY;
431 		break;
432 	default:
433 		pr_warn("unknown id_product %#x\n", id_product);
434 		card->usb_boot_state = USB8XXX_FW_DNLD;
435 		break;
436 	}
437 
438 	card->udev = udev;
439 	card->intf = intf;
440 
441 	pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
442 		 le16_to_cpu(udev->descriptor.bcdUSB),
443 		 udev->descriptor.bDeviceClass,
444 		 udev->descriptor.bDeviceSubClass,
445 		 udev->descriptor.bDeviceProtocol);
446 
447 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
448 		epd = &iface_desc->endpoint[i].desc;
449 		if (usb_endpoint_dir_in(epd) &&
450 		    usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
451 		    (usb_endpoint_xfer_bulk(epd) ||
452 		     usb_endpoint_xfer_int(epd))) {
453 			card->rx_cmd_ep_type = usb_endpoint_type(epd);
454 			card->rx_cmd_interval = epd->bInterval;
455 			pr_debug("info: Rx CMD/EVT:: max pkt size: %d, addr: %d, ep_type: %d\n",
456 				 le16_to_cpu(epd->wMaxPacketSize),
457 				 epd->bEndpointAddress, card->rx_cmd_ep_type);
458 			card->rx_cmd_ep = usb_endpoint_num(epd);
459 			atomic_set(&card->rx_cmd_urb_pending, 0);
460 		}
461 		if (usb_endpoint_dir_in(epd) &&
462 		    usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
463 		    usb_endpoint_xfer_bulk(epd)) {
464 			pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
465 				 le16_to_cpu(epd->wMaxPacketSize),
466 				 epd->bEndpointAddress);
467 			card->rx_data_ep = usb_endpoint_num(epd);
468 			atomic_set(&card->rx_data_urb_pending, 0);
469 		}
470 		if (usb_endpoint_dir_out(epd) &&
471 		    usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
472 		    usb_endpoint_xfer_bulk(epd)) {
473 			pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
474 				 le16_to_cpu(epd->wMaxPacketSize),
475 				 epd->bEndpointAddress);
476 			card->port[0].tx_data_ep = usb_endpoint_num(epd);
477 			atomic_set(&card->port[0].tx_data_urb_pending, 0);
478 		}
479 		if (usb_endpoint_dir_out(epd) &&
480 		    usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA_CH2 &&
481 		    usb_endpoint_xfer_bulk(epd)) {
482 			pr_debug("info: bulk OUT chan2:\t"
483 				 "max pkt size: %d, addr: %d\n",
484 				 le16_to_cpu(epd->wMaxPacketSize),
485 				 epd->bEndpointAddress);
486 			card->port[1].tx_data_ep = usb_endpoint_num(epd);
487 			atomic_set(&card->port[1].tx_data_urb_pending, 0);
488 		}
489 		if (usb_endpoint_dir_out(epd) &&
490 		    usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
491 		    (usb_endpoint_xfer_bulk(epd) ||
492 		     usb_endpoint_xfer_int(epd))) {
493 			card->tx_cmd_ep_type = usb_endpoint_type(epd);
494 			card->tx_cmd_interval = epd->bInterval;
495 			pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
496 				 le16_to_cpu(epd->wMaxPacketSize),
497 				 epd->bEndpointAddress);
498 			pr_debug("info: Tx CMD:: max pkt size: %d, addr: %d, ep_type: %d\n",
499 				 le16_to_cpu(epd->wMaxPacketSize),
500 				 epd->bEndpointAddress, card->tx_cmd_ep_type);
501 			card->tx_cmd_ep = usb_endpoint_num(epd);
502 			atomic_set(&card->tx_cmd_urb_pending, 0);
503 			card->bulk_out_maxpktsize =
504 					le16_to_cpu(epd->wMaxPacketSize);
505 		}
506 	}
507 
508 	usb_set_intfdata(intf, card);
509 
510 	ret = mwifiex_add_card(card, &card->fw_done, &usb_ops,
511 			       MWIFIEX_USB, &card->udev->dev);
512 	if (ret) {
513 		pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret);
514 		usb_reset_device(udev);
515 		return ret;
516 	}
517 
518 	usb_get_dev(udev);
519 
520 	return 0;
521 }
522 
523 /* Kernel needs to suspend all functions separately. Therefore all
524  * registered functions must have drivers with suspend and resume
525  * methods. Failing that the kernel simply removes the whole card.
526  *
527  * If already not suspended, this function allocates and sends a
528  * 'host sleep activate' request to the firmware and turns off the traffic.
529  */
530 static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message)
531 {
532 	struct usb_card_rec *card = usb_get_intfdata(intf);
533 	struct mwifiex_adapter *adapter;
534 	struct usb_tx_data_port *port;
535 	int i, j;
536 
537 	/* Might still be loading firmware */
538 	wait_for_completion(&card->fw_done);
539 
540 	adapter = card->adapter;
541 	if (!adapter) {
542 		dev_err(&intf->dev, "card is not valid\n");
543 		return 0;
544 	}
545 
546 	if (unlikely(test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)))
547 		mwifiex_dbg(adapter, WARN,
548 			    "Device already suspended\n");
549 
550 	/* Enable the Host Sleep */
551 	if (!mwifiex_enable_hs(adapter)) {
552 		mwifiex_dbg(adapter, ERROR,
553 			    "cmd: failed to suspend\n");
554 		clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
555 		return -EFAULT;
556 	}
557 
558 
559 	/* 'MWIFIEX_IS_SUSPENDED' bit indicates device is suspended.
560 	 * It must be set here before the usb_kill_urb() calls. Reason
561 	 * is in the complete handlers, urb->status(= -ENOENT) and
562 	 * this flag is used in combination to distinguish between a
563 	 * 'suspended' state and a 'disconnect' one.
564 	 */
565 	set_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
566 	clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
567 
568 	if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
569 		usb_kill_urb(card->rx_cmd.urb);
570 
571 	if (atomic_read(&card->rx_data_urb_pending))
572 		for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
573 			if (card->rx_data_list[i].urb)
574 				usb_kill_urb(card->rx_data_list[i].urb);
575 
576 	for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
577 		port = &card->port[i];
578 		for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
579 			if (port->tx_data_list[j].urb)
580 				usb_kill_urb(port->tx_data_list[j].urb);
581 		}
582 	}
583 
584 	if (card->tx_cmd.urb)
585 		usb_kill_urb(card->tx_cmd.urb);
586 
587 	return 0;
588 }
589 
590 /* Kernel needs to suspend all functions separately. Therefore all
591  * registered functions must have drivers with suspend and resume
592  * methods. Failing that the kernel simply removes the whole card.
593  *
594  * If already not resumed, this function turns on the traffic and
595  * sends a 'host sleep cancel' request to the firmware.
596  */
597 static int mwifiex_usb_resume(struct usb_interface *intf)
598 {
599 	struct usb_card_rec *card = usb_get_intfdata(intf);
600 	struct mwifiex_adapter *adapter;
601 	int i;
602 
603 	if (!card->adapter) {
604 		dev_err(&intf->dev, "%s: card->adapter is NULL\n",
605 			__func__);
606 		return 0;
607 	}
608 	adapter = card->adapter;
609 
610 	if (unlikely(!test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags))) {
611 		mwifiex_dbg(adapter, WARN,
612 			    "Device already resumed\n");
613 		return 0;
614 	}
615 
616 	/* Indicate device resumed. The netdev queue will be resumed only
617 	 * after the urbs have been re-submitted
618 	 */
619 	clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
620 
621 	if (!atomic_read(&card->rx_data_urb_pending))
622 		for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
623 			mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
624 						  MWIFIEX_RX_DATA_BUF_SIZE);
625 
626 	if (!atomic_read(&card->rx_cmd_urb_pending)) {
627 		card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
628 		if (card->rx_cmd.skb)
629 			mwifiex_usb_submit_rx_urb(&card->rx_cmd,
630 						  MWIFIEX_RX_CMD_BUF_SIZE);
631 	}
632 
633 	/* Disable Host Sleep */
634 	if (adapter->hs_activated)
635 		mwifiex_cancel_hs(mwifiex_get_priv(adapter,
636 						   MWIFIEX_BSS_ROLE_ANY),
637 				  MWIFIEX_ASYNC_CMD);
638 
639 	return 0;
640 }
641 
642 static void mwifiex_usb_disconnect(struct usb_interface *intf)
643 {
644 	struct usb_card_rec *card = usb_get_intfdata(intf);
645 	struct mwifiex_adapter *adapter;
646 
647 	wait_for_completion(&card->fw_done);
648 
649 	adapter = card->adapter;
650 	if (!adapter || !adapter->priv_num)
651 		return;
652 
653 	if (card->udev->state != USB_STATE_NOTATTACHED && !adapter->mfg_mode) {
654 		mwifiex_deauthenticate_all(adapter);
655 
656 		mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
657 							  MWIFIEX_BSS_ROLE_ANY),
658 					 MWIFIEX_FUNC_SHUTDOWN);
659 	}
660 
661 	mwifiex_dbg(adapter, FATAL,
662 		    "%s: removing card\n", __func__);
663 	mwifiex_remove_card(adapter);
664 
665 	usb_put_dev(interface_to_usbdev(intf));
666 }
667 
668 static void mwifiex_usb_coredump(struct device *dev)
669 {
670 	struct usb_interface *intf = to_usb_interface(dev);
671 	struct usb_card_rec *card = usb_get_intfdata(intf);
672 
673 	mwifiex_fw_dump_event(mwifiex_get_priv(card->adapter,
674 					       MWIFIEX_BSS_ROLE_ANY));
675 }
676 
677 static struct usb_driver mwifiex_usb_driver = {
678 	.name = "mwifiex_usb",
679 	.probe = mwifiex_usb_probe,
680 	.disconnect = mwifiex_usb_disconnect,
681 	.id_table = mwifiex_usb_table,
682 	.suspend = mwifiex_usb_suspend,
683 	.resume = mwifiex_usb_resume,
684 	.soft_unbind = 1,
685 	.drvwrap.driver = {
686 		.coredump = mwifiex_usb_coredump,
687 	},
688 };
689 
690 static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
691 				   u32 *len, u8 ep, u32 timeout)
692 {
693 	struct usb_card_rec *card = adapter->card;
694 	int actual_length, ret;
695 
696 	if (!(*len % card->bulk_out_maxpktsize))
697 		(*len)++;
698 
699 	/* Send the data block */
700 	ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
701 			   *len, &actual_length, timeout);
702 	if (ret) {
703 		mwifiex_dbg(adapter, ERROR,
704 			    "usb_bulk_msg for tx failed: %d\n", ret);
705 		return ret;
706 	}
707 
708 	*len = actual_length;
709 
710 	return ret;
711 }
712 
713 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
714 				  u32 *len, u8 ep, u32 timeout)
715 {
716 	struct usb_card_rec *card = adapter->card;
717 	int actual_length, ret;
718 
719 	/* Receive the data response */
720 	ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
721 			   *len, &actual_length, timeout);
722 	if (ret) {
723 		mwifiex_dbg(adapter, ERROR,
724 			    "usb_bulk_msg for rx failed: %d\n", ret);
725 		return ret;
726 	}
727 
728 	*len = actual_length;
729 
730 	return ret;
731 }
732 
733 static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter)
734 {
735 	struct usb_card_rec *card = adapter->card;
736 	u8 active_port = MWIFIEX_USB_EP_DATA;
737 	struct mwifiex_private *priv = NULL;
738 	int i;
739 
740 	if (adapter->usb_mc_status) {
741 		for (i = 0; i < adapter->priv_num; i++) {
742 			priv = adapter->priv[i];
743 			if (!priv)
744 				continue;
745 			if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
746 			     !priv->bss_started) ||
747 			    (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
748 			     !priv->media_connected))
749 				priv->usb_port = MWIFIEX_USB_EP_DATA;
750 		}
751 		for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
752 			card->port[i].block_status = false;
753 	} else {
754 		for (i = 0; i < adapter->priv_num; i++) {
755 			priv = adapter->priv[i];
756 			if (!priv)
757 				continue;
758 			if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
759 			     priv->bss_started) ||
760 			    (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
761 			     priv->media_connected)) {
762 				active_port = priv->usb_port;
763 				break;
764 			}
765 		}
766 		for (i = 0; i < adapter->priv_num; i++) {
767 			priv = adapter->priv[i];
768 			if (priv)
769 				priv->usb_port = active_port;
770 		}
771 		for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
772 			if (active_port == card->port[i].tx_data_ep)
773 				card->port[i].block_status = false;
774 			else
775 				card->port[i].block_status = true;
776 		}
777 	}
778 }
779 
780 static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv)
781 {
782 	struct usb_card_rec *card = priv->adapter->card;
783 	int idx;
784 
785 	for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
786 		if (priv->usb_port == card->port[idx].tx_data_ep)
787 			return !card->port[idx].block_status;
788 	}
789 
790 	return false;
791 }
792 
793 static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter)
794 {
795 	struct usb_card_rec *card = adapter->card;
796 	int i;
797 
798 	for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
799 		if (!card->port[i].block_status)
800 			return false;
801 
802 	return true;
803 }
804 
805 static int mwifiex_usb_construct_send_urb(struct mwifiex_adapter *adapter,
806 					  struct usb_tx_data_port *port, u8 ep,
807 					  struct urb_context *context,
808 					  struct sk_buff *skb_send)
809 {
810 	struct usb_card_rec *card = adapter->card;
811 	int ret = -EINPROGRESS;
812 	struct urb *tx_urb;
813 
814 	context->adapter = adapter;
815 	context->ep = ep;
816 	context->skb = skb_send;
817 	tx_urb = context->urb;
818 
819 	if (ep == card->tx_cmd_ep &&
820 	    card->tx_cmd_ep_type == USB_ENDPOINT_XFER_INT)
821 		usb_fill_int_urb(tx_urb, card->udev,
822 				 usb_sndintpipe(card->udev, ep), skb_send->data,
823 				 skb_send->len, mwifiex_usb_tx_complete,
824 				 (void *)context, card->tx_cmd_interval);
825 	else
826 		usb_fill_bulk_urb(tx_urb, card->udev,
827 				  usb_sndbulkpipe(card->udev, ep),
828 				  skb_send->data, skb_send->len,
829 				  mwifiex_usb_tx_complete, (void *)context);
830 
831 	tx_urb->transfer_flags |= URB_ZERO_PACKET;
832 
833 	if (ep == card->tx_cmd_ep)
834 		atomic_inc(&card->tx_cmd_urb_pending);
835 	else
836 		atomic_inc(&port->tx_data_urb_pending);
837 
838 	if (ep != card->tx_cmd_ep &&
839 	    atomic_read(&port->tx_data_urb_pending) ==
840 					MWIFIEX_TX_DATA_URB) {
841 		port->block_status = true;
842 		adapter->data_sent = mwifiex_usb_data_sent(adapter);
843 		ret = -ENOSR;
844 	}
845 
846 	if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
847 		mwifiex_dbg(adapter, ERROR,
848 			    "%s: usb_submit_urb failed\n", __func__);
849 		if (ep == card->tx_cmd_ep) {
850 			atomic_dec(&card->tx_cmd_urb_pending);
851 		} else {
852 			atomic_dec(&port->tx_data_urb_pending);
853 			port->block_status = false;
854 			adapter->data_sent = false;
855 			if (port->tx_data_ix)
856 				port->tx_data_ix--;
857 			else
858 				port->tx_data_ix = MWIFIEX_TX_DATA_URB;
859 		}
860 		ret = -1;
861 	}
862 
863 	return ret;
864 }
865 
866 static int mwifiex_usb_prepare_tx_aggr_skb(struct mwifiex_adapter *adapter,
867 					   struct usb_tx_data_port *port,
868 					   struct sk_buff **skb_send)
869 {
870 	struct sk_buff *skb_aggr, *skb_tmp;
871 	u8 *payload, pad;
872 	u16 align = adapter->bus_aggr.tx_aggr_align;
873 	struct mwifiex_txinfo *tx_info = NULL;
874 	bool is_txinfo_set = false;
875 
876 	/* Packets in aggr_list will be send in either skb_aggr or
877 	 * write complete, delete the tx_aggr timer
878 	 */
879 	if (port->tx_aggr.timer_cnxt.is_hold_timer_set) {
880 		del_timer(&port->tx_aggr.timer_cnxt.hold_timer);
881 		port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
882 		port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
883 	}
884 
885 	skb_aggr = mwifiex_alloc_dma_align_buf(port->tx_aggr.aggr_len,
886 					       GFP_ATOMIC);
887 	if (!skb_aggr) {
888 		mwifiex_dbg(adapter, ERROR,
889 			    "%s: alloc skb_aggr failed\n", __func__);
890 
891 		while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list)))
892 			mwifiex_write_data_complete(adapter, skb_tmp, 0, -1);
893 
894 		port->tx_aggr.aggr_num = 0;
895 		port->tx_aggr.aggr_len = 0;
896 		return -EBUSY;
897 	}
898 
899 	tx_info = MWIFIEX_SKB_TXCB(skb_aggr);
900 	memset(tx_info, 0, sizeof(*tx_info));
901 
902 	while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list))) {
903 		/* padding for aligning next packet header*/
904 		pad = (align - (skb_tmp->len & (align - 1))) % align;
905 		payload = skb_put(skb_aggr, skb_tmp->len + pad);
906 		memcpy(payload, skb_tmp->data, skb_tmp->len);
907 		if (skb_queue_empty(&port->tx_aggr.aggr_list)) {
908 			/* do not padding for last packet*/
909 			*(u16 *)payload = cpu_to_le16(skb_tmp->len);
910 			*(u16 *)&payload[2] =
911 				cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80);
912 			skb_trim(skb_aggr, skb_aggr->len - pad);
913 		} else {
914 			/* add aggregation interface header */
915 			*(u16 *)payload = cpu_to_le16(skb_tmp->len + pad);
916 			*(u16 *)&payload[2] =
917 				cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2);
918 		}
919 
920 		if (!is_txinfo_set) {
921 			tx_info->bss_num = MWIFIEX_SKB_TXCB(skb_tmp)->bss_num;
922 			tx_info->bss_type = MWIFIEX_SKB_TXCB(skb_tmp)->bss_type;
923 			is_txinfo_set = true;
924 		}
925 
926 		port->tx_aggr.aggr_num--;
927 		port->tx_aggr.aggr_len -= (skb_tmp->len + pad);
928 		mwifiex_write_data_complete(adapter, skb_tmp, 0, 0);
929 	}
930 
931 	tx_info->pkt_len = skb_aggr->len -
932 			(sizeof(struct txpd) + adapter->intf_hdr_len);
933 	tx_info->flags |= MWIFIEX_BUF_FLAG_AGGR_PKT;
934 
935 	port->tx_aggr.aggr_num = 0;
936 	port->tx_aggr.aggr_len = 0;
937 	*skb_send = skb_aggr;
938 
939 	return 0;
940 }
941 
942 /* This function prepare data packet to be send under usb tx aggregation
943  * protocol, check current usb aggregation status, link packet to aggrgation
944  * list if possible, work flow as below:
945  * (1) if only 1 packet available, add usb tx aggregation header and send.
946  * (2) if packet is able to aggregated, link it to current aggregation list.
947  * (3) if packet is not able to aggregated, aggregate and send exist packets
948  *     in aggrgation list. Then, link packet in the list if there is more
949  *     packet in transmit queue, otherwise try to transmit single packet.
950  */
951 static int mwifiex_usb_aggr_tx_data(struct mwifiex_adapter *adapter, u8 ep,
952 				    struct sk_buff *skb,
953 				    struct mwifiex_tx_param *tx_param,
954 				    struct usb_tx_data_port *port)
955 {
956 	u8 *payload, pad;
957 	u16 align = adapter->bus_aggr.tx_aggr_align;
958 	struct sk_buff *skb_send = NULL;
959 	struct urb_context *context = NULL;
960 	struct txpd *local_tx_pd =
961 		(struct txpd *)((u8 *)skb->data + adapter->intf_hdr_len);
962 	u8 f_send_aggr_buf = 0;
963 	u8 f_send_cur_buf = 0;
964 	u8 f_precopy_cur_buf = 0;
965 	u8 f_postcopy_cur_buf = 0;
966 	u32 timeout;
967 	int ret;
968 
969 	/* padding to ensure each packet alginment */
970 	pad = (align - (skb->len & (align - 1))) % align;
971 
972 	if (tx_param && tx_param->next_pkt_len) {
973 		/* next packet available in tx queue*/
974 		if (port->tx_aggr.aggr_len + skb->len + pad >
975 		    adapter->bus_aggr.tx_aggr_max_size) {
976 			f_send_aggr_buf = 1;
977 			f_postcopy_cur_buf = 1;
978 		} else {
979 			/* current packet could be aggregated*/
980 			f_precopy_cur_buf = 1;
981 
982 			if (port->tx_aggr.aggr_len + skb->len + pad +
983 			    tx_param->next_pkt_len >
984 			    adapter->bus_aggr.tx_aggr_max_size ||
985 			    port->tx_aggr.aggr_num + 2 >
986 			    adapter->bus_aggr.tx_aggr_max_num) {
987 			    /* next packet could not be aggregated
988 			     * send current aggregation buffer
989 			     */
990 				f_send_aggr_buf = 1;
991 			}
992 		}
993 	} else {
994 		/* last packet in tx queue */
995 		if (port->tx_aggr.aggr_num > 0) {
996 			/* pending packets in aggregation buffer*/
997 			if (port->tx_aggr.aggr_len + skb->len + pad >
998 			    adapter->bus_aggr.tx_aggr_max_size) {
999 				/* current packet not be able to aggregated,
1000 				 * send aggr buffer first, then send packet.
1001 				 */
1002 				f_send_cur_buf = 1;
1003 			} else {
1004 				/* last packet, Aggregation and send */
1005 				f_precopy_cur_buf = 1;
1006 			}
1007 
1008 			f_send_aggr_buf = 1;
1009 		} else {
1010 			/* no pending packets in aggregation buffer,
1011 			 * send current packet immediately
1012 			 */
1013 			 f_send_cur_buf = 1;
1014 		}
1015 	}
1016 
1017 	if (local_tx_pd->flags & MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET) {
1018 		/* Send NULL packet immediately*/
1019 		if (f_precopy_cur_buf) {
1020 			if (skb_queue_empty(&port->tx_aggr.aggr_list)) {
1021 				f_precopy_cur_buf = 0;
1022 				f_send_aggr_buf = 0;
1023 				f_send_cur_buf = 1;
1024 			} else {
1025 				f_send_aggr_buf = 1;
1026 			}
1027 		} else if (f_postcopy_cur_buf) {
1028 			f_send_cur_buf = 1;
1029 			f_postcopy_cur_buf = 0;
1030 		}
1031 	}
1032 
1033 	if (f_precopy_cur_buf) {
1034 		skb_queue_tail(&port->tx_aggr.aggr_list, skb);
1035 		port->tx_aggr.aggr_len += (skb->len + pad);
1036 		port->tx_aggr.aggr_num++;
1037 		if (f_send_aggr_buf)
1038 			goto send_aggr_buf;
1039 
1040 		/* packet will not been send immediately,
1041 		 * set a timer to make sure it will be sent under
1042 		 * strict time limit. Dynamically fit the timeout
1043 		 * value, according to packets number in aggr_list
1044 		 */
1045 		if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) {
1046 			port->tx_aggr.timer_cnxt.hold_tmo_msecs =
1047 					MWIFIEX_USB_TX_AGGR_TMO_MIN;
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 			port->tx_aggr.timer_cnxt.is_hold_timer_set = true;
1053 		} else {
1054 			if (port->tx_aggr.timer_cnxt.hold_tmo_msecs <
1055 			    MWIFIEX_USB_TX_AGGR_TMO_MAX) {
1056 				/* Dyanmic fit timeout */
1057 				timeout =
1058 				++port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1059 				mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1060 					  jiffies + msecs_to_jiffies(timeout));
1061 			}
1062 		}
1063 	}
1064 
1065 send_aggr_buf:
1066 	if (f_send_aggr_buf) {
1067 		ret = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send);
1068 		if (!ret) {
1069 			context = &port->tx_data_list[port->tx_data_ix++];
1070 			ret = mwifiex_usb_construct_send_urb(adapter, port, ep,
1071 							     context, skb_send);
1072 			if (ret == -1)
1073 				mwifiex_write_data_complete(adapter, skb_send,
1074 							    0, -1);
1075 		}
1076 	}
1077 
1078 	if (f_send_cur_buf) {
1079 		if (f_send_aggr_buf) {
1080 			if (atomic_read(&port->tx_data_urb_pending) >=
1081 			    MWIFIEX_TX_DATA_URB) {
1082 				port->block_status = true;
1083 				adapter->data_sent =
1084 					mwifiex_usb_data_sent(adapter);
1085 				/* no available urb, postcopy packet*/
1086 				f_postcopy_cur_buf = 1;
1087 				goto postcopy_cur_buf;
1088 			}
1089 
1090 			if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1091 				port->tx_data_ix = 0;
1092 		}
1093 
1094 		payload = skb->data;
1095 		*(u16 *)&payload[2] =
1096 			cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80);
1097 		*(u16 *)payload = cpu_to_le16(skb->len);
1098 		skb_send = skb;
1099 		context = &port->tx_data_list[port->tx_data_ix++];
1100 		return mwifiex_usb_construct_send_urb(adapter, port, ep,
1101 						      context, skb_send);
1102 	}
1103 
1104 postcopy_cur_buf:
1105 	if (f_postcopy_cur_buf) {
1106 		skb_queue_tail(&port->tx_aggr.aggr_list, skb);
1107 		port->tx_aggr.aggr_len += (skb->len + pad);
1108 		port->tx_aggr.aggr_num++;
1109 		/* New aggregation begin, start timer */
1110 		if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) {
1111 			port->tx_aggr.timer_cnxt.hold_tmo_msecs =
1112 					MWIFIEX_USB_TX_AGGR_TMO_MIN;
1113 			timeout = port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1114 			mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1115 				  jiffies + msecs_to_jiffies(timeout));
1116 			port->tx_aggr.timer_cnxt.is_hold_timer_set = true;
1117 		}
1118 	}
1119 
1120 	return -EINPROGRESS;
1121 }
1122 
1123 static void mwifiex_usb_tx_aggr_tmo(struct timer_list *t)
1124 {
1125 	struct urb_context *urb_cnxt = NULL;
1126 	struct sk_buff *skb_send = NULL;
1127 	struct tx_aggr_tmr_cnxt *timer_context =
1128 		from_timer(timer_context, t, hold_timer);
1129 	struct mwifiex_adapter *adapter = timer_context->adapter;
1130 	struct usb_tx_data_port *port = timer_context->port;
1131 	int err = 0;
1132 
1133 	spin_lock_bh(&port->tx_aggr_lock);
1134 	err = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send);
1135 	if (err) {
1136 		mwifiex_dbg(adapter, ERROR,
1137 			    "prepare tx aggr skb failed, err=%d\n", err);
1138 		goto unlock;
1139 	}
1140 
1141 	if (atomic_read(&port->tx_data_urb_pending) >=
1142 	    MWIFIEX_TX_DATA_URB) {
1143 		port->block_status = true;
1144 		adapter->data_sent =
1145 			mwifiex_usb_data_sent(adapter);
1146 		err = -1;
1147 		goto done;
1148 	}
1149 
1150 	if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1151 		port->tx_data_ix = 0;
1152 
1153 	urb_cnxt = &port->tx_data_list[port->tx_data_ix++];
1154 	err = mwifiex_usb_construct_send_urb(adapter, port, port->tx_data_ep,
1155 					     urb_cnxt, skb_send);
1156 done:
1157 	if (err == -1)
1158 		mwifiex_write_data_complete(adapter, skb_send, 0, -1);
1159 unlock:
1160 	spin_unlock_bh(&port->tx_aggr_lock);
1161 }
1162 
1163 /* This function write a command/data packet to card. */
1164 static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
1165 				    struct sk_buff *skb,
1166 				    struct mwifiex_tx_param *tx_param)
1167 {
1168 	struct usb_card_rec *card = adapter->card;
1169 	struct urb_context *context = NULL;
1170 	struct usb_tx_data_port *port = NULL;
1171 	int idx, ret;
1172 
1173 	if (test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) {
1174 		mwifiex_dbg(adapter, ERROR,
1175 			    "%s: not allowed while suspended\n", __func__);
1176 		return -1;
1177 	}
1178 
1179 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags)) {
1180 		mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__);
1181 		return -1;
1182 	}
1183 
1184 	mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep);
1185 
1186 	if (ep == card->tx_cmd_ep) {
1187 		context = &card->tx_cmd;
1188 	} else {
1189 		/* get the data port structure for endpoint */
1190 		for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
1191 			if (ep == card->port[idx].tx_data_ep) {
1192 				port = &card->port[idx];
1193 				if (atomic_read(&port->tx_data_urb_pending)
1194 				    >= MWIFIEX_TX_DATA_URB) {
1195 					port->block_status = true;
1196 					adapter->data_sent =
1197 						mwifiex_usb_data_sent(adapter);
1198 					return -EBUSY;
1199 				}
1200 				if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1201 					port->tx_data_ix = 0;
1202 				break;
1203 			}
1204 		}
1205 
1206 		if (!port) {
1207 			mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n");
1208 			return -1;
1209 		}
1210 
1211 		if (adapter->bus_aggr.enable) {
1212 			spin_lock_bh(&port->tx_aggr_lock);
1213 			ret =  mwifiex_usb_aggr_tx_data(adapter, ep, skb,
1214 							tx_param, port);
1215 			spin_unlock_bh(&port->tx_aggr_lock);
1216 			return ret;
1217 		}
1218 
1219 		context = &port->tx_data_list[port->tx_data_ix++];
1220 	}
1221 
1222 	return mwifiex_usb_construct_send_urb(adapter, port, ep, context, skb);
1223 }
1224 
1225 static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter)
1226 {
1227 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1228 	struct usb_tx_data_port *port;
1229 	int i, j;
1230 
1231 	card->tx_cmd.adapter = adapter;
1232 	card->tx_cmd.ep = card->tx_cmd_ep;
1233 
1234 	card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
1235 	if (!card->tx_cmd.urb)
1236 		return -ENOMEM;
1237 
1238 	for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
1239 		port = &card->port[i];
1240 		if (!port->tx_data_ep)
1241 			continue;
1242 		port->tx_data_ix = 0;
1243 		skb_queue_head_init(&port->tx_aggr.aggr_list);
1244 		if (port->tx_data_ep == MWIFIEX_USB_EP_DATA)
1245 			port->block_status = false;
1246 		else
1247 			port->block_status = true;
1248 		for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
1249 			port->tx_data_list[j].adapter = adapter;
1250 			port->tx_data_list[j].ep = port->tx_data_ep;
1251 			port->tx_data_list[j].urb =
1252 					usb_alloc_urb(0, GFP_KERNEL);
1253 			if (!port->tx_data_list[j].urb)
1254 				return -ENOMEM;
1255 		}
1256 
1257 		port->tx_aggr.timer_cnxt.adapter = adapter;
1258 		port->tx_aggr.timer_cnxt.port = port;
1259 		port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
1260 		port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
1261 		timer_setup(&port->tx_aggr.timer_cnxt.hold_timer,
1262 			    mwifiex_usb_tx_aggr_tmo, 0);
1263 	}
1264 
1265 	return 0;
1266 }
1267 
1268 static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
1269 {
1270 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1271 	int i;
1272 
1273 	card->rx_cmd.adapter = adapter;
1274 	card->rx_cmd.ep = card->rx_cmd_ep;
1275 
1276 	card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
1277 	if (!card->rx_cmd.urb)
1278 		return -ENOMEM;
1279 
1280 	card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
1281 	if (!card->rx_cmd.skb)
1282 		return -ENOMEM;
1283 
1284 	if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
1285 		return -1;
1286 
1287 	for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1288 		card->rx_data_list[i].adapter = adapter;
1289 		card->rx_data_list[i].ep = card->rx_data_ep;
1290 
1291 		card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1292 		if (!card->rx_data_list[i].urb)
1293 			return -1;
1294 		if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
1295 					      MWIFIEX_RX_DATA_BUF_SIZE))
1296 			return -1;
1297 	}
1298 
1299 	return 0;
1300 }
1301 
1302 /* This function register usb device and initialize parameter. */
1303 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1304 {
1305 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1306 
1307 	card->adapter = adapter;
1308 
1309 	switch (le16_to_cpu(card->udev->descriptor.idProduct)) {
1310 	case USB8997_PID_1:
1311 	case USB8997_PID_2:
1312 		adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K;
1313 		strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME);
1314 		adapter->ext_scan = true;
1315 		break;
1316 	case USB8766_PID_1:
1317 	case USB8766_PID_2:
1318 		adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1319 		strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME);
1320 		adapter->ext_scan = true;
1321 		break;
1322 	case USB8801_PID_1:
1323 	case USB8801_PID_2:
1324 		adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1325 		strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME);
1326 		adapter->ext_scan = false;
1327 		break;
1328 	case USB8797_PID_1:
1329 	case USB8797_PID_2:
1330 	default:
1331 		adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1332 		strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
1333 		break;
1334 	}
1335 
1336 	adapter->usb_mc_status = false;
1337 	adapter->usb_mc_setup = false;
1338 
1339 	return 0;
1340 }
1341 
1342 static void mwifiex_usb_cleanup_tx_aggr(struct mwifiex_adapter *adapter)
1343 {
1344 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1345 	struct usb_tx_data_port *port;
1346 	struct sk_buff *skb_tmp;
1347 	int idx;
1348 
1349 	for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
1350 		port = &card->port[idx];
1351 		if (adapter->bus_aggr.enable)
1352 			while ((skb_tmp =
1353 				skb_dequeue(&port->tx_aggr.aggr_list)))
1354 				mwifiex_write_data_complete(adapter, skb_tmp,
1355 							    0, -1);
1356 		if (port->tx_aggr.timer_cnxt.hold_timer.function)
1357 			del_timer_sync(&port->tx_aggr.timer_cnxt.hold_timer);
1358 		port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
1359 		port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
1360 	}
1361 }
1362 
1363 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1364 {
1365 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1366 
1367 	mwifiex_usb_free(card);
1368 
1369 	mwifiex_usb_cleanup_tx_aggr(adapter);
1370 
1371 	card->adapter = NULL;
1372 }
1373 
1374 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1375 				    struct mwifiex_fw_image *fw)
1376 {
1377 	int ret = 0;
1378 	u8 *firmware = fw->fw_buf, *recv_buff;
1379 	u32 retries = USB8XXX_FW_MAX_RETRY + 1;
1380 	u32 dlen;
1381 	u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
1382 	struct fw_data *fwdata;
1383 	struct fw_sync_header sync_fw;
1384 	u8 check_winner = 1;
1385 
1386 	if (!firmware) {
1387 		mwifiex_dbg(adapter, ERROR,
1388 			    "No firmware image found! Terminating download\n");
1389 		ret = -1;
1390 		goto fw_exit;
1391 	}
1392 
1393 	/* Allocate memory for transmit */
1394 	fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
1395 	if (!fwdata) {
1396 		ret = -ENOMEM;
1397 		goto fw_exit;
1398 	}
1399 
1400 	/* Allocate memory for receive */
1401 	recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
1402 	if (!recv_buff) {
1403 		ret = -ENOMEM;
1404 		goto cleanup;
1405 	}
1406 
1407 	do {
1408 		/* Send pseudo data to check winner status first */
1409 		if (check_winner) {
1410 			memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
1411 			dlen = 0;
1412 		} else {
1413 			/* copy the header of the fw_data to get the length */
1414 			memcpy(&fwdata->fw_hdr, &firmware[tlen],
1415 			       sizeof(struct fw_header));
1416 
1417 			dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
1418 			dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
1419 			tlen += sizeof(struct fw_header);
1420 
1421 			/* Command 7 doesn't have data length field */
1422 			if (dnld_cmd == FW_CMD_7)
1423 				dlen = 0;
1424 
1425 			memcpy(fwdata->data, &firmware[tlen], dlen);
1426 
1427 			fwdata->seq_num = cpu_to_le32(fw_seqnum);
1428 			tlen += dlen;
1429 		}
1430 
1431 		/* If the send/receive fails or CRC occurs then retry */
1432 		while (--retries) {
1433 			u8 *buf = (u8 *)fwdata;
1434 			u32 len = FW_DATA_XMIT_SIZE;
1435 
1436 			/* send the firmware block */
1437 			ret = mwifiex_write_data_sync(adapter, buf, &len,
1438 						MWIFIEX_USB_EP_CMD_EVENT,
1439 						MWIFIEX_USB_TIMEOUT);
1440 			if (ret) {
1441 				mwifiex_dbg(adapter, ERROR,
1442 					    "write_data_sync: failed: %d\n",
1443 					    ret);
1444 				continue;
1445 			}
1446 
1447 			buf = recv_buff;
1448 			len = FW_DNLD_RX_BUF_SIZE;
1449 
1450 			/* Receive the firmware block response */
1451 			ret = mwifiex_read_data_sync(adapter, buf, &len,
1452 						MWIFIEX_USB_EP_CMD_EVENT,
1453 						MWIFIEX_USB_TIMEOUT);
1454 			if (ret) {
1455 				mwifiex_dbg(adapter, ERROR,
1456 					    "read_data_sync: failed: %d\n",
1457 					    ret);
1458 				continue;
1459 			}
1460 
1461 			memcpy(&sync_fw, recv_buff,
1462 			       sizeof(struct fw_sync_header));
1463 
1464 			/* check 1st firmware block resp for highest bit set */
1465 			if (check_winner) {
1466 				if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
1467 					mwifiex_dbg(adapter, WARN,
1468 						    "USB is not the winner %#x\n",
1469 						    sync_fw.cmd);
1470 
1471 					/* returning success */
1472 					ret = 0;
1473 					goto cleanup;
1474 				}
1475 
1476 				mwifiex_dbg(adapter, MSG,
1477 					    "start to download FW...\n");
1478 
1479 				check_winner = 0;
1480 				break;
1481 			}
1482 
1483 			/* check the firmware block response for CRC errors */
1484 			if (sync_fw.cmd) {
1485 				mwifiex_dbg(adapter, ERROR,
1486 					    "FW received block with CRC %#x\n",
1487 					    sync_fw.cmd);
1488 				ret = -1;
1489 				continue;
1490 			}
1491 
1492 			retries = USB8XXX_FW_MAX_RETRY + 1;
1493 			break;
1494 		}
1495 		fw_seqnum++;
1496 	} while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
1497 
1498 cleanup:
1499 	mwifiex_dbg(adapter, MSG,
1500 		    "info: FW download over, size %d bytes\n", tlen);
1501 
1502 	kfree(recv_buff);
1503 	kfree(fwdata);
1504 
1505 	if (retries)
1506 		ret = 0;
1507 fw_exit:
1508 	return ret;
1509 }
1510 
1511 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
1512 			struct mwifiex_fw_image *fw)
1513 {
1514 	int ret;
1515 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1516 
1517 	if (card->usb_boot_state == USB8XXX_FW_DNLD) {
1518 		ret = mwifiex_prog_fw_w_helper(adapter, fw);
1519 		if (ret)
1520 			return -1;
1521 
1522 		/* Boot state changes after successful firmware download */
1523 		if (card->usb_boot_state == USB8XXX_FW_DNLD)
1524 			return -1;
1525 	}
1526 
1527 	ret = mwifiex_usb_rx_init(adapter);
1528 	if (!ret)
1529 		ret = mwifiex_usb_tx_init(adapter);
1530 
1531 	return ret;
1532 }
1533 
1534 static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
1535 {
1536 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1537 
1538 	skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
1539 	if ((ep == card->rx_cmd_ep) &&
1540 	    (!atomic_read(&card->rx_cmd_urb_pending)))
1541 		mwifiex_usb_submit_rx_urb(&card->rx_cmd,
1542 					  MWIFIEX_RX_CMD_BUF_SIZE);
1543 
1544 	return;
1545 }
1546 
1547 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
1548 				       struct sk_buff *skb)
1549 {
1550 	mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
1551 
1552 	return 0;
1553 }
1554 
1555 /* This function wakes up the card. */
1556 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
1557 {
1558 	/* Simulation of HS_AWAKE event */
1559 	adapter->pm_wakeup_fw_try = false;
1560 	del_timer(&adapter->wakeup_timer);
1561 	adapter->pm_wakeup_card_req = false;
1562 	adapter->ps_state = PS_STATE_AWAKE;
1563 
1564 	return 0;
1565 }
1566 
1567 static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter)
1568 {
1569 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1570 	int i;
1571 	struct urb_context *ctx;
1572 
1573 	for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1574 		if (card->rx_data_list[i].skb)
1575 			continue;
1576 		ctx = &card->rx_data_list[i];
1577 		mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE);
1578 	}
1579 }
1580 
1581 /* This function is called after the card has woken up. */
1582 static inline int
1583 mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
1584 {
1585 	return 0;
1586 }
1587 
1588 static struct mwifiex_if_ops usb_ops = {
1589 	.register_dev =		mwifiex_register_dev,
1590 	.unregister_dev =	mwifiex_unregister_dev,
1591 	.wakeup =		mwifiex_pm_wakeup_card,
1592 	.wakeup_complete =	mwifiex_pm_wakeup_card_complete,
1593 
1594 	/* USB specific */
1595 	.dnld_fw =		mwifiex_usb_dnld_fw,
1596 	.cmdrsp_complete =	mwifiex_usb_cmd_event_complete,
1597 	.event_complete =	mwifiex_usb_cmd_event_complete,
1598 	.host_to_card =		mwifiex_usb_host_to_card,
1599 	.submit_rem_rx_urbs =	mwifiex_usb_submit_rem_rx_urbs,
1600 	.multi_port_resync =	mwifiex_usb_port_resync,
1601 	.is_port_ready =	mwifiex_usb_is_port_ready,
1602 };
1603 
1604 module_usb_driver(mwifiex_usb_driver);
1605 
1606 MODULE_AUTHOR("Marvell International Ltd.");
1607 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1608 MODULE_VERSION(USB_VERSION);
1609 MODULE_LICENSE("GPL v2");
1610 MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME);
1611 MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME);
1612 MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME);
1613 MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME);
1614