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