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 	switch (card->usb_boot_state) {
509 	case USB8XXX_FW_DNLD:
510 		/* Reject broken descriptors. */
511 		if (!card->rx_cmd_ep || !card->tx_cmd_ep)
512 			return -ENODEV;
513 		if (card->bulk_out_maxpktsize == 0)
514 			return -ENODEV;
515 		break;
516 	case USB8XXX_FW_READY:
517 		/* Assume the driver can handle missing endpoints for now. */
518 		break;
519 	default:
520 		WARN_ON(1);
521 		return -ENODEV;
522 	}
523 
524 	usb_set_intfdata(intf, card);
525 
526 	ret = mwifiex_add_card(card, &card->fw_done, &usb_ops,
527 			       MWIFIEX_USB, &card->udev->dev);
528 	if (ret) {
529 		pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret);
530 		usb_reset_device(udev);
531 		return ret;
532 	}
533 
534 	usb_get_dev(udev);
535 
536 	return 0;
537 }
538 
539 /* Kernel needs to suspend all functions separately. Therefore all
540  * registered functions must have drivers with suspend and resume
541  * methods. Failing that the kernel simply removes the whole card.
542  *
543  * If already not suspended, this function allocates and sends a
544  * 'host sleep activate' request to the firmware and turns off the traffic.
545  */
546 static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message)
547 {
548 	struct usb_card_rec *card = usb_get_intfdata(intf);
549 	struct mwifiex_adapter *adapter;
550 	struct usb_tx_data_port *port;
551 	int i, j;
552 
553 	/* Might still be loading firmware */
554 	wait_for_completion(&card->fw_done);
555 
556 	adapter = card->adapter;
557 	if (!adapter) {
558 		dev_err(&intf->dev, "card is not valid\n");
559 		return 0;
560 	}
561 
562 	if (unlikely(test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)))
563 		mwifiex_dbg(adapter, WARN,
564 			    "Device already suspended\n");
565 
566 	/* Enable the Host Sleep */
567 	if (!mwifiex_enable_hs(adapter)) {
568 		mwifiex_dbg(adapter, ERROR,
569 			    "cmd: failed to suspend\n");
570 		clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
571 		return -EFAULT;
572 	}
573 
574 
575 	/* 'MWIFIEX_IS_SUSPENDED' bit indicates device is suspended.
576 	 * It must be set here before the usb_kill_urb() calls. Reason
577 	 * is in the complete handlers, urb->status(= -ENOENT) and
578 	 * this flag is used in combination to distinguish between a
579 	 * 'suspended' state and a 'disconnect' one.
580 	 */
581 	set_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
582 	clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
583 
584 	if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
585 		usb_kill_urb(card->rx_cmd.urb);
586 
587 	if (atomic_read(&card->rx_data_urb_pending))
588 		for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
589 			if (card->rx_data_list[i].urb)
590 				usb_kill_urb(card->rx_data_list[i].urb);
591 
592 	for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
593 		port = &card->port[i];
594 		for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
595 			if (port->tx_data_list[j].urb)
596 				usb_kill_urb(port->tx_data_list[j].urb);
597 		}
598 	}
599 
600 	if (card->tx_cmd.urb)
601 		usb_kill_urb(card->tx_cmd.urb);
602 
603 	return 0;
604 }
605 
606 /* Kernel needs to suspend all functions separately. Therefore all
607  * registered functions must have drivers with suspend and resume
608  * methods. Failing that the kernel simply removes the whole card.
609  *
610  * If already not resumed, this function turns on the traffic and
611  * sends a 'host sleep cancel' request to the firmware.
612  */
613 static int mwifiex_usb_resume(struct usb_interface *intf)
614 {
615 	struct usb_card_rec *card = usb_get_intfdata(intf);
616 	struct mwifiex_adapter *adapter;
617 	int i;
618 
619 	if (!card->adapter) {
620 		dev_err(&intf->dev, "%s: card->adapter is NULL\n",
621 			__func__);
622 		return 0;
623 	}
624 	adapter = card->adapter;
625 
626 	if (unlikely(!test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags))) {
627 		mwifiex_dbg(adapter, WARN,
628 			    "Device already resumed\n");
629 		return 0;
630 	}
631 
632 	/* Indicate device resumed. The netdev queue will be resumed only
633 	 * after the urbs have been re-submitted
634 	 */
635 	clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
636 
637 	if (!atomic_read(&card->rx_data_urb_pending))
638 		for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
639 			mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
640 						  MWIFIEX_RX_DATA_BUF_SIZE);
641 
642 	if (!atomic_read(&card->rx_cmd_urb_pending)) {
643 		card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
644 		if (card->rx_cmd.skb)
645 			mwifiex_usb_submit_rx_urb(&card->rx_cmd,
646 						  MWIFIEX_RX_CMD_BUF_SIZE);
647 	}
648 
649 	/* Disable Host Sleep */
650 	if (adapter->hs_activated)
651 		mwifiex_cancel_hs(mwifiex_get_priv(adapter,
652 						   MWIFIEX_BSS_ROLE_ANY),
653 				  MWIFIEX_ASYNC_CMD);
654 
655 	return 0;
656 }
657 
658 static void mwifiex_usb_disconnect(struct usb_interface *intf)
659 {
660 	struct usb_card_rec *card = usb_get_intfdata(intf);
661 	struct mwifiex_adapter *adapter;
662 
663 	wait_for_completion(&card->fw_done);
664 
665 	adapter = card->adapter;
666 	if (!adapter || !adapter->priv_num)
667 		return;
668 
669 	if (card->udev->state != USB_STATE_NOTATTACHED && !adapter->mfg_mode) {
670 		mwifiex_deauthenticate_all(adapter);
671 
672 		mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
673 							  MWIFIEX_BSS_ROLE_ANY),
674 					 MWIFIEX_FUNC_SHUTDOWN);
675 	}
676 
677 	mwifiex_dbg(adapter, FATAL,
678 		    "%s: removing card\n", __func__);
679 	mwifiex_remove_card(adapter);
680 
681 	usb_put_dev(interface_to_usbdev(intf));
682 }
683 
684 static void mwifiex_usb_coredump(struct device *dev)
685 {
686 	struct usb_interface *intf = to_usb_interface(dev);
687 	struct usb_card_rec *card = usb_get_intfdata(intf);
688 
689 	mwifiex_fw_dump_event(mwifiex_get_priv(card->adapter,
690 					       MWIFIEX_BSS_ROLE_ANY));
691 }
692 
693 static struct usb_driver mwifiex_usb_driver = {
694 	.name = "mwifiex_usb",
695 	.probe = mwifiex_usb_probe,
696 	.disconnect = mwifiex_usb_disconnect,
697 	.id_table = mwifiex_usb_table,
698 	.suspend = mwifiex_usb_suspend,
699 	.resume = mwifiex_usb_resume,
700 	.soft_unbind = 1,
701 	.drvwrap.driver = {
702 		.coredump = mwifiex_usb_coredump,
703 	},
704 };
705 
706 static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
707 				   u32 *len, u8 ep, u32 timeout)
708 {
709 	struct usb_card_rec *card = adapter->card;
710 	int actual_length, ret;
711 
712 	if (!(*len % card->bulk_out_maxpktsize))
713 		(*len)++;
714 
715 	/* Send the data block */
716 	ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
717 			   *len, &actual_length, timeout);
718 	if (ret) {
719 		mwifiex_dbg(adapter, ERROR,
720 			    "usb_bulk_msg for tx failed: %d\n", ret);
721 		return ret;
722 	}
723 
724 	*len = actual_length;
725 
726 	return ret;
727 }
728 
729 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
730 				  u32 *len, u8 ep, u32 timeout)
731 {
732 	struct usb_card_rec *card = adapter->card;
733 	int actual_length, ret;
734 
735 	/* Receive the data response */
736 	ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
737 			   *len, &actual_length, timeout);
738 	if (ret) {
739 		mwifiex_dbg(adapter, ERROR,
740 			    "usb_bulk_msg for rx failed: %d\n", ret);
741 		return ret;
742 	}
743 
744 	*len = actual_length;
745 
746 	return ret;
747 }
748 
749 static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter)
750 {
751 	struct usb_card_rec *card = adapter->card;
752 	u8 active_port = MWIFIEX_USB_EP_DATA;
753 	struct mwifiex_private *priv = NULL;
754 	int i;
755 
756 	if (adapter->usb_mc_status) {
757 		for (i = 0; i < adapter->priv_num; i++) {
758 			priv = adapter->priv[i];
759 			if (!priv)
760 				continue;
761 			if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
762 			     !priv->bss_started) ||
763 			    (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
764 			     !priv->media_connected))
765 				priv->usb_port = MWIFIEX_USB_EP_DATA;
766 		}
767 		for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
768 			card->port[i].block_status = false;
769 	} else {
770 		for (i = 0; i < adapter->priv_num; i++) {
771 			priv = adapter->priv[i];
772 			if (!priv)
773 				continue;
774 			if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
775 			     priv->bss_started) ||
776 			    (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
777 			     priv->media_connected)) {
778 				active_port = priv->usb_port;
779 				break;
780 			}
781 		}
782 		for (i = 0; i < adapter->priv_num; i++) {
783 			priv = adapter->priv[i];
784 			if (priv)
785 				priv->usb_port = active_port;
786 		}
787 		for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
788 			if (active_port == card->port[i].tx_data_ep)
789 				card->port[i].block_status = false;
790 			else
791 				card->port[i].block_status = true;
792 		}
793 	}
794 }
795 
796 static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv)
797 {
798 	struct usb_card_rec *card = priv->adapter->card;
799 	int idx;
800 
801 	for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
802 		if (priv->usb_port == card->port[idx].tx_data_ep)
803 			return !card->port[idx].block_status;
804 	}
805 
806 	return false;
807 }
808 
809 static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter)
810 {
811 	struct usb_card_rec *card = adapter->card;
812 	int i;
813 
814 	for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
815 		if (!card->port[i].block_status)
816 			return false;
817 
818 	return true;
819 }
820 
821 static int mwifiex_usb_construct_send_urb(struct mwifiex_adapter *adapter,
822 					  struct usb_tx_data_port *port, u8 ep,
823 					  struct urb_context *context,
824 					  struct sk_buff *skb_send)
825 {
826 	struct usb_card_rec *card = adapter->card;
827 	int ret = -EINPROGRESS;
828 	struct urb *tx_urb;
829 
830 	context->adapter = adapter;
831 	context->ep = ep;
832 	context->skb = skb_send;
833 	tx_urb = context->urb;
834 
835 	if (ep == card->tx_cmd_ep &&
836 	    card->tx_cmd_ep_type == USB_ENDPOINT_XFER_INT)
837 		usb_fill_int_urb(tx_urb, card->udev,
838 				 usb_sndintpipe(card->udev, ep), skb_send->data,
839 				 skb_send->len, mwifiex_usb_tx_complete,
840 				 (void *)context, card->tx_cmd_interval);
841 	else
842 		usb_fill_bulk_urb(tx_urb, card->udev,
843 				  usb_sndbulkpipe(card->udev, ep),
844 				  skb_send->data, skb_send->len,
845 				  mwifiex_usb_tx_complete, (void *)context);
846 
847 	tx_urb->transfer_flags |= URB_ZERO_PACKET;
848 
849 	if (ep == card->tx_cmd_ep)
850 		atomic_inc(&card->tx_cmd_urb_pending);
851 	else
852 		atomic_inc(&port->tx_data_urb_pending);
853 
854 	if (ep != card->tx_cmd_ep &&
855 	    atomic_read(&port->tx_data_urb_pending) ==
856 					MWIFIEX_TX_DATA_URB) {
857 		port->block_status = true;
858 		adapter->data_sent = mwifiex_usb_data_sent(adapter);
859 		ret = -ENOSR;
860 	}
861 
862 	if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
863 		mwifiex_dbg(adapter, ERROR,
864 			    "%s: usb_submit_urb failed\n", __func__);
865 		if (ep == card->tx_cmd_ep) {
866 			atomic_dec(&card->tx_cmd_urb_pending);
867 		} else {
868 			atomic_dec(&port->tx_data_urb_pending);
869 			port->block_status = false;
870 			adapter->data_sent = false;
871 			if (port->tx_data_ix)
872 				port->tx_data_ix--;
873 			else
874 				port->tx_data_ix = MWIFIEX_TX_DATA_URB;
875 		}
876 		ret = -1;
877 	}
878 
879 	return ret;
880 }
881 
882 static int mwifiex_usb_prepare_tx_aggr_skb(struct mwifiex_adapter *adapter,
883 					   struct usb_tx_data_port *port,
884 					   struct sk_buff **skb_send)
885 {
886 	struct sk_buff *skb_aggr, *skb_tmp;
887 	u8 *payload, pad;
888 	u16 align = adapter->bus_aggr.tx_aggr_align;
889 	struct mwifiex_txinfo *tx_info = NULL;
890 	bool is_txinfo_set = false;
891 
892 	/* Packets in aggr_list will be send in either skb_aggr or
893 	 * write complete, delete the tx_aggr timer
894 	 */
895 	if (port->tx_aggr.timer_cnxt.is_hold_timer_set) {
896 		del_timer(&port->tx_aggr.timer_cnxt.hold_timer);
897 		port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
898 		port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
899 	}
900 
901 	skb_aggr = mwifiex_alloc_dma_align_buf(port->tx_aggr.aggr_len,
902 					       GFP_ATOMIC);
903 	if (!skb_aggr) {
904 		mwifiex_dbg(adapter, ERROR,
905 			    "%s: alloc skb_aggr failed\n", __func__);
906 
907 		while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list)))
908 			mwifiex_write_data_complete(adapter, skb_tmp, 0, -1);
909 
910 		port->tx_aggr.aggr_num = 0;
911 		port->tx_aggr.aggr_len = 0;
912 		return -EBUSY;
913 	}
914 
915 	tx_info = MWIFIEX_SKB_TXCB(skb_aggr);
916 	memset(tx_info, 0, sizeof(*tx_info));
917 
918 	while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list))) {
919 		/* padding for aligning next packet header*/
920 		pad = (align - (skb_tmp->len & (align - 1))) % align;
921 		payload = skb_put(skb_aggr, skb_tmp->len + pad);
922 		memcpy(payload, skb_tmp->data, skb_tmp->len);
923 		if (skb_queue_empty(&port->tx_aggr.aggr_list)) {
924 			/* do not padding for last packet*/
925 			*(u16 *)payload = cpu_to_le16(skb_tmp->len);
926 			*(u16 *)&payload[2] =
927 				cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80);
928 			skb_trim(skb_aggr, skb_aggr->len - pad);
929 		} else {
930 			/* add aggregation interface header */
931 			*(u16 *)payload = cpu_to_le16(skb_tmp->len + pad);
932 			*(u16 *)&payload[2] =
933 				cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2);
934 		}
935 
936 		if (!is_txinfo_set) {
937 			tx_info->bss_num = MWIFIEX_SKB_TXCB(skb_tmp)->bss_num;
938 			tx_info->bss_type = MWIFIEX_SKB_TXCB(skb_tmp)->bss_type;
939 			is_txinfo_set = true;
940 		}
941 
942 		port->tx_aggr.aggr_num--;
943 		port->tx_aggr.aggr_len -= (skb_tmp->len + pad);
944 		mwifiex_write_data_complete(adapter, skb_tmp, 0, 0);
945 	}
946 
947 	tx_info->pkt_len = skb_aggr->len -
948 			(sizeof(struct txpd) + adapter->intf_hdr_len);
949 	tx_info->flags |= MWIFIEX_BUF_FLAG_AGGR_PKT;
950 
951 	port->tx_aggr.aggr_num = 0;
952 	port->tx_aggr.aggr_len = 0;
953 	*skb_send = skb_aggr;
954 
955 	return 0;
956 }
957 
958 /* This function prepare data packet to be send under usb tx aggregation
959  * protocol, check current usb aggregation status, link packet to aggrgation
960  * list if possible, work flow as below:
961  * (1) if only 1 packet available, add usb tx aggregation header and send.
962  * (2) if packet is able to aggregated, link it to current aggregation list.
963  * (3) if packet is not able to aggregated, aggregate and send exist packets
964  *     in aggrgation list. Then, link packet in the list if there is more
965  *     packet in transmit queue, otherwise try to transmit single packet.
966  */
967 static int mwifiex_usb_aggr_tx_data(struct mwifiex_adapter *adapter, u8 ep,
968 				    struct sk_buff *skb,
969 				    struct mwifiex_tx_param *tx_param,
970 				    struct usb_tx_data_port *port)
971 {
972 	u8 *payload, pad;
973 	u16 align = adapter->bus_aggr.tx_aggr_align;
974 	struct sk_buff *skb_send = NULL;
975 	struct urb_context *context = NULL;
976 	struct txpd *local_tx_pd =
977 		(struct txpd *)((u8 *)skb->data + adapter->intf_hdr_len);
978 	u8 f_send_aggr_buf = 0;
979 	u8 f_send_cur_buf = 0;
980 	u8 f_precopy_cur_buf = 0;
981 	u8 f_postcopy_cur_buf = 0;
982 	u32 timeout;
983 	int ret;
984 
985 	/* padding to ensure each packet alginment */
986 	pad = (align - (skb->len & (align - 1))) % align;
987 
988 	if (tx_param && tx_param->next_pkt_len) {
989 		/* next packet available in tx queue*/
990 		if (port->tx_aggr.aggr_len + skb->len + pad >
991 		    adapter->bus_aggr.tx_aggr_max_size) {
992 			f_send_aggr_buf = 1;
993 			f_postcopy_cur_buf = 1;
994 		} else {
995 			/* current packet could be aggregated*/
996 			f_precopy_cur_buf = 1;
997 
998 			if (port->tx_aggr.aggr_len + skb->len + pad +
999 			    tx_param->next_pkt_len >
1000 			    adapter->bus_aggr.tx_aggr_max_size ||
1001 			    port->tx_aggr.aggr_num + 2 >
1002 			    adapter->bus_aggr.tx_aggr_max_num) {
1003 			    /* next packet could not be aggregated
1004 			     * send current aggregation buffer
1005 			     */
1006 				f_send_aggr_buf = 1;
1007 			}
1008 		}
1009 	} else {
1010 		/* last packet in tx queue */
1011 		if (port->tx_aggr.aggr_num > 0) {
1012 			/* pending packets in aggregation buffer*/
1013 			if (port->tx_aggr.aggr_len + skb->len + pad >
1014 			    adapter->bus_aggr.tx_aggr_max_size) {
1015 				/* current packet not be able to aggregated,
1016 				 * send aggr buffer first, then send packet.
1017 				 */
1018 				f_send_cur_buf = 1;
1019 			} else {
1020 				/* last packet, Aggregation and send */
1021 				f_precopy_cur_buf = 1;
1022 			}
1023 
1024 			f_send_aggr_buf = 1;
1025 		} else {
1026 			/* no pending packets in aggregation buffer,
1027 			 * send current packet immediately
1028 			 */
1029 			 f_send_cur_buf = 1;
1030 		}
1031 	}
1032 
1033 	if (local_tx_pd->flags & MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET) {
1034 		/* Send NULL packet immediately*/
1035 		if (f_precopy_cur_buf) {
1036 			if (skb_queue_empty(&port->tx_aggr.aggr_list)) {
1037 				f_precopy_cur_buf = 0;
1038 				f_send_aggr_buf = 0;
1039 				f_send_cur_buf = 1;
1040 			} else {
1041 				f_send_aggr_buf = 1;
1042 			}
1043 		} else if (f_postcopy_cur_buf) {
1044 			f_send_cur_buf = 1;
1045 			f_postcopy_cur_buf = 0;
1046 		}
1047 	}
1048 
1049 	if (f_precopy_cur_buf) {
1050 		skb_queue_tail(&port->tx_aggr.aggr_list, skb);
1051 		port->tx_aggr.aggr_len += (skb->len + pad);
1052 		port->tx_aggr.aggr_num++;
1053 		if (f_send_aggr_buf)
1054 			goto send_aggr_buf;
1055 
1056 		/* packet will not been send immediately,
1057 		 * set a timer to make sure it will be sent under
1058 		 * strict time limit. Dynamically fit the timeout
1059 		 * value, according to packets number in aggr_list
1060 		 */
1061 		if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) {
1062 			port->tx_aggr.timer_cnxt.hold_tmo_msecs =
1063 					MWIFIEX_USB_TX_AGGR_TMO_MIN;
1064 			timeout =
1065 				port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1066 			mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1067 				  jiffies + msecs_to_jiffies(timeout));
1068 			port->tx_aggr.timer_cnxt.is_hold_timer_set = true;
1069 		} else {
1070 			if (port->tx_aggr.timer_cnxt.hold_tmo_msecs <
1071 			    MWIFIEX_USB_TX_AGGR_TMO_MAX) {
1072 				/* Dyanmic fit timeout */
1073 				timeout =
1074 				++port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1075 				mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1076 					  jiffies + msecs_to_jiffies(timeout));
1077 			}
1078 		}
1079 	}
1080 
1081 send_aggr_buf:
1082 	if (f_send_aggr_buf) {
1083 		ret = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send);
1084 		if (!ret) {
1085 			context = &port->tx_data_list[port->tx_data_ix++];
1086 			ret = mwifiex_usb_construct_send_urb(adapter, port, ep,
1087 							     context, skb_send);
1088 			if (ret == -1)
1089 				mwifiex_write_data_complete(adapter, skb_send,
1090 							    0, -1);
1091 		}
1092 	}
1093 
1094 	if (f_send_cur_buf) {
1095 		if (f_send_aggr_buf) {
1096 			if (atomic_read(&port->tx_data_urb_pending) >=
1097 			    MWIFIEX_TX_DATA_URB) {
1098 				port->block_status = true;
1099 				adapter->data_sent =
1100 					mwifiex_usb_data_sent(adapter);
1101 				/* no available urb, postcopy packet*/
1102 				f_postcopy_cur_buf = 1;
1103 				goto postcopy_cur_buf;
1104 			}
1105 
1106 			if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1107 				port->tx_data_ix = 0;
1108 		}
1109 
1110 		payload = skb->data;
1111 		*(u16 *)&payload[2] =
1112 			cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80);
1113 		*(u16 *)payload = cpu_to_le16(skb->len);
1114 		skb_send = skb;
1115 		context = &port->tx_data_list[port->tx_data_ix++];
1116 		return mwifiex_usb_construct_send_urb(adapter, port, ep,
1117 						      context, skb_send);
1118 	}
1119 
1120 postcopy_cur_buf:
1121 	if (f_postcopy_cur_buf) {
1122 		skb_queue_tail(&port->tx_aggr.aggr_list, skb);
1123 		port->tx_aggr.aggr_len += (skb->len + pad);
1124 		port->tx_aggr.aggr_num++;
1125 		/* New aggregation begin, start timer */
1126 		if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) {
1127 			port->tx_aggr.timer_cnxt.hold_tmo_msecs =
1128 					MWIFIEX_USB_TX_AGGR_TMO_MIN;
1129 			timeout = port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1130 			mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1131 				  jiffies + msecs_to_jiffies(timeout));
1132 			port->tx_aggr.timer_cnxt.is_hold_timer_set = true;
1133 		}
1134 	}
1135 
1136 	return -EINPROGRESS;
1137 }
1138 
1139 static void mwifiex_usb_tx_aggr_tmo(struct timer_list *t)
1140 {
1141 	struct urb_context *urb_cnxt = NULL;
1142 	struct sk_buff *skb_send = NULL;
1143 	struct tx_aggr_tmr_cnxt *timer_context =
1144 		from_timer(timer_context, t, hold_timer);
1145 	struct mwifiex_adapter *adapter = timer_context->adapter;
1146 	struct usb_tx_data_port *port = timer_context->port;
1147 	int err = 0;
1148 
1149 	spin_lock_bh(&port->tx_aggr_lock);
1150 	err = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send);
1151 	if (err) {
1152 		mwifiex_dbg(adapter, ERROR,
1153 			    "prepare tx aggr skb failed, err=%d\n", err);
1154 		goto unlock;
1155 	}
1156 
1157 	if (atomic_read(&port->tx_data_urb_pending) >=
1158 	    MWIFIEX_TX_DATA_URB) {
1159 		port->block_status = true;
1160 		adapter->data_sent =
1161 			mwifiex_usb_data_sent(adapter);
1162 		err = -1;
1163 		goto done;
1164 	}
1165 
1166 	if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1167 		port->tx_data_ix = 0;
1168 
1169 	urb_cnxt = &port->tx_data_list[port->tx_data_ix++];
1170 	err = mwifiex_usb_construct_send_urb(adapter, port, port->tx_data_ep,
1171 					     urb_cnxt, skb_send);
1172 done:
1173 	if (err == -1)
1174 		mwifiex_write_data_complete(adapter, skb_send, 0, -1);
1175 unlock:
1176 	spin_unlock_bh(&port->tx_aggr_lock);
1177 }
1178 
1179 /* This function write a command/data packet to card. */
1180 static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
1181 				    struct sk_buff *skb,
1182 				    struct mwifiex_tx_param *tx_param)
1183 {
1184 	struct usb_card_rec *card = adapter->card;
1185 	struct urb_context *context = NULL;
1186 	struct usb_tx_data_port *port = NULL;
1187 	int idx, ret;
1188 
1189 	if (test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) {
1190 		mwifiex_dbg(adapter, ERROR,
1191 			    "%s: not allowed while suspended\n", __func__);
1192 		return -1;
1193 	}
1194 
1195 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags)) {
1196 		mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__);
1197 		return -1;
1198 	}
1199 
1200 	mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep);
1201 
1202 	if (ep == card->tx_cmd_ep) {
1203 		context = &card->tx_cmd;
1204 	} else {
1205 		/* get the data port structure for endpoint */
1206 		for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
1207 			if (ep == card->port[idx].tx_data_ep) {
1208 				port = &card->port[idx];
1209 				if (atomic_read(&port->tx_data_urb_pending)
1210 				    >= MWIFIEX_TX_DATA_URB) {
1211 					port->block_status = true;
1212 					adapter->data_sent =
1213 						mwifiex_usb_data_sent(adapter);
1214 					return -EBUSY;
1215 				}
1216 				if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1217 					port->tx_data_ix = 0;
1218 				break;
1219 			}
1220 		}
1221 
1222 		if (!port) {
1223 			mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n");
1224 			return -1;
1225 		}
1226 
1227 		if (adapter->bus_aggr.enable) {
1228 			spin_lock_bh(&port->tx_aggr_lock);
1229 			ret =  mwifiex_usb_aggr_tx_data(adapter, ep, skb,
1230 							tx_param, port);
1231 			spin_unlock_bh(&port->tx_aggr_lock);
1232 			return ret;
1233 		}
1234 
1235 		context = &port->tx_data_list[port->tx_data_ix++];
1236 	}
1237 
1238 	return mwifiex_usb_construct_send_urb(adapter, port, ep, context, skb);
1239 }
1240 
1241 static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter)
1242 {
1243 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1244 	struct usb_tx_data_port *port;
1245 	int i, j;
1246 
1247 	card->tx_cmd.adapter = adapter;
1248 	card->tx_cmd.ep = card->tx_cmd_ep;
1249 
1250 	card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
1251 	if (!card->tx_cmd.urb)
1252 		return -ENOMEM;
1253 
1254 	for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
1255 		port = &card->port[i];
1256 		if (!port->tx_data_ep)
1257 			continue;
1258 		port->tx_data_ix = 0;
1259 		skb_queue_head_init(&port->tx_aggr.aggr_list);
1260 		if (port->tx_data_ep == MWIFIEX_USB_EP_DATA)
1261 			port->block_status = false;
1262 		else
1263 			port->block_status = true;
1264 		for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
1265 			port->tx_data_list[j].adapter = adapter;
1266 			port->tx_data_list[j].ep = port->tx_data_ep;
1267 			port->tx_data_list[j].urb =
1268 					usb_alloc_urb(0, GFP_KERNEL);
1269 			if (!port->tx_data_list[j].urb)
1270 				return -ENOMEM;
1271 		}
1272 
1273 		port->tx_aggr.timer_cnxt.adapter = adapter;
1274 		port->tx_aggr.timer_cnxt.port = port;
1275 		port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
1276 		port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
1277 		timer_setup(&port->tx_aggr.timer_cnxt.hold_timer,
1278 			    mwifiex_usb_tx_aggr_tmo, 0);
1279 	}
1280 
1281 	return 0;
1282 }
1283 
1284 static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
1285 {
1286 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1287 	int i;
1288 
1289 	card->rx_cmd.adapter = adapter;
1290 	card->rx_cmd.ep = card->rx_cmd_ep;
1291 
1292 	card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
1293 	if (!card->rx_cmd.urb)
1294 		return -ENOMEM;
1295 
1296 	card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
1297 	if (!card->rx_cmd.skb)
1298 		return -ENOMEM;
1299 
1300 	if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
1301 		return -1;
1302 
1303 	for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1304 		card->rx_data_list[i].adapter = adapter;
1305 		card->rx_data_list[i].ep = card->rx_data_ep;
1306 
1307 		card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1308 		if (!card->rx_data_list[i].urb)
1309 			return -1;
1310 		if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
1311 					      MWIFIEX_RX_DATA_BUF_SIZE))
1312 			return -1;
1313 	}
1314 
1315 	return 0;
1316 }
1317 
1318 /* This function register usb device and initialize parameter. */
1319 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1320 {
1321 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1322 
1323 	card->adapter = adapter;
1324 
1325 	switch (le16_to_cpu(card->udev->descriptor.idProduct)) {
1326 	case USB8997_PID_1:
1327 	case USB8997_PID_2:
1328 		adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K;
1329 		strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME);
1330 		adapter->ext_scan = true;
1331 		break;
1332 	case USB8766_PID_1:
1333 	case USB8766_PID_2:
1334 		adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1335 		strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME);
1336 		adapter->ext_scan = true;
1337 		break;
1338 	case USB8801_PID_1:
1339 	case USB8801_PID_2:
1340 		adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1341 		strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME);
1342 		adapter->ext_scan = false;
1343 		break;
1344 	case USB8797_PID_1:
1345 	case USB8797_PID_2:
1346 	default:
1347 		adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1348 		strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
1349 		break;
1350 	}
1351 
1352 	adapter->usb_mc_status = false;
1353 	adapter->usb_mc_setup = false;
1354 
1355 	return 0;
1356 }
1357 
1358 static void mwifiex_usb_cleanup_tx_aggr(struct mwifiex_adapter *adapter)
1359 {
1360 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1361 	struct usb_tx_data_port *port;
1362 	struct sk_buff *skb_tmp;
1363 	int idx;
1364 
1365 	for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
1366 		port = &card->port[idx];
1367 		if (adapter->bus_aggr.enable)
1368 			while ((skb_tmp =
1369 				skb_dequeue(&port->tx_aggr.aggr_list)))
1370 				mwifiex_write_data_complete(adapter, skb_tmp,
1371 							    0, -1);
1372 		if (port->tx_aggr.timer_cnxt.hold_timer.function)
1373 			del_timer_sync(&port->tx_aggr.timer_cnxt.hold_timer);
1374 		port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
1375 		port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
1376 	}
1377 }
1378 
1379 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1380 {
1381 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1382 
1383 	mwifiex_usb_free(card);
1384 
1385 	mwifiex_usb_cleanup_tx_aggr(adapter);
1386 
1387 	card->adapter = NULL;
1388 }
1389 
1390 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1391 				    struct mwifiex_fw_image *fw)
1392 {
1393 	int ret = 0;
1394 	u8 *firmware = fw->fw_buf, *recv_buff;
1395 	u32 retries = USB8XXX_FW_MAX_RETRY + 1;
1396 	u32 dlen;
1397 	u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
1398 	struct fw_data *fwdata;
1399 	struct fw_sync_header sync_fw;
1400 	u8 check_winner = 1;
1401 
1402 	if (!firmware) {
1403 		mwifiex_dbg(adapter, ERROR,
1404 			    "No firmware image found! Terminating download\n");
1405 		ret = -1;
1406 		goto fw_exit;
1407 	}
1408 
1409 	/* Allocate memory for transmit */
1410 	fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
1411 	if (!fwdata) {
1412 		ret = -ENOMEM;
1413 		goto fw_exit;
1414 	}
1415 
1416 	/* Allocate memory for receive */
1417 	recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
1418 	if (!recv_buff) {
1419 		ret = -ENOMEM;
1420 		goto cleanup;
1421 	}
1422 
1423 	do {
1424 		/* Send pseudo data to check winner status first */
1425 		if (check_winner) {
1426 			memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
1427 			dlen = 0;
1428 		} else {
1429 			/* copy the header of the fw_data to get the length */
1430 			memcpy(&fwdata->fw_hdr, &firmware[tlen],
1431 			       sizeof(struct fw_header));
1432 
1433 			dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
1434 			dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
1435 			tlen += sizeof(struct fw_header);
1436 
1437 			/* Command 7 doesn't have data length field */
1438 			if (dnld_cmd == FW_CMD_7)
1439 				dlen = 0;
1440 
1441 			memcpy(fwdata->data, &firmware[tlen], dlen);
1442 
1443 			fwdata->seq_num = cpu_to_le32(fw_seqnum);
1444 			tlen += dlen;
1445 		}
1446 
1447 		/* If the send/receive fails or CRC occurs then retry */
1448 		while (--retries) {
1449 			u8 *buf = (u8 *)fwdata;
1450 			u32 len = FW_DATA_XMIT_SIZE;
1451 
1452 			/* send the firmware block */
1453 			ret = mwifiex_write_data_sync(adapter, buf, &len,
1454 						MWIFIEX_USB_EP_CMD_EVENT,
1455 						MWIFIEX_USB_TIMEOUT);
1456 			if (ret) {
1457 				mwifiex_dbg(adapter, ERROR,
1458 					    "write_data_sync: failed: %d\n",
1459 					    ret);
1460 				continue;
1461 			}
1462 
1463 			buf = recv_buff;
1464 			len = FW_DNLD_RX_BUF_SIZE;
1465 
1466 			/* Receive the firmware block response */
1467 			ret = mwifiex_read_data_sync(adapter, buf, &len,
1468 						MWIFIEX_USB_EP_CMD_EVENT,
1469 						MWIFIEX_USB_TIMEOUT);
1470 			if (ret) {
1471 				mwifiex_dbg(adapter, ERROR,
1472 					    "read_data_sync: failed: %d\n",
1473 					    ret);
1474 				continue;
1475 			}
1476 
1477 			memcpy(&sync_fw, recv_buff,
1478 			       sizeof(struct fw_sync_header));
1479 
1480 			/* check 1st firmware block resp for highest bit set */
1481 			if (check_winner) {
1482 				if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
1483 					mwifiex_dbg(adapter, WARN,
1484 						    "USB is not the winner %#x\n",
1485 						    sync_fw.cmd);
1486 
1487 					/* returning success */
1488 					ret = 0;
1489 					goto cleanup;
1490 				}
1491 
1492 				mwifiex_dbg(adapter, MSG,
1493 					    "start to download FW...\n");
1494 
1495 				check_winner = 0;
1496 				break;
1497 			}
1498 
1499 			/* check the firmware block response for CRC errors */
1500 			if (sync_fw.cmd) {
1501 				mwifiex_dbg(adapter, ERROR,
1502 					    "FW received block with CRC %#x\n",
1503 					    sync_fw.cmd);
1504 				ret = -1;
1505 				continue;
1506 			}
1507 
1508 			retries = USB8XXX_FW_MAX_RETRY + 1;
1509 			break;
1510 		}
1511 		fw_seqnum++;
1512 	} while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
1513 
1514 cleanup:
1515 	mwifiex_dbg(adapter, MSG,
1516 		    "info: FW download over, size %d bytes\n", tlen);
1517 
1518 	kfree(recv_buff);
1519 	kfree(fwdata);
1520 
1521 	if (retries)
1522 		ret = 0;
1523 fw_exit:
1524 	return ret;
1525 }
1526 
1527 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
1528 			struct mwifiex_fw_image *fw)
1529 {
1530 	int ret;
1531 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1532 
1533 	if (card->usb_boot_state == USB8XXX_FW_DNLD) {
1534 		ret = mwifiex_prog_fw_w_helper(adapter, fw);
1535 		if (ret)
1536 			return -1;
1537 
1538 		/* Boot state changes after successful firmware download */
1539 		if (card->usb_boot_state == USB8XXX_FW_DNLD)
1540 			return -1;
1541 	}
1542 
1543 	ret = mwifiex_usb_rx_init(adapter);
1544 	if (!ret)
1545 		ret = mwifiex_usb_tx_init(adapter);
1546 
1547 	return ret;
1548 }
1549 
1550 static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
1551 {
1552 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1553 
1554 	skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
1555 	if ((ep == card->rx_cmd_ep) &&
1556 	    (!atomic_read(&card->rx_cmd_urb_pending)))
1557 		mwifiex_usb_submit_rx_urb(&card->rx_cmd,
1558 					  MWIFIEX_RX_CMD_BUF_SIZE);
1559 
1560 	return;
1561 }
1562 
1563 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
1564 				       struct sk_buff *skb)
1565 {
1566 	mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
1567 
1568 	return 0;
1569 }
1570 
1571 /* This function wakes up the card. */
1572 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
1573 {
1574 	/* Simulation of HS_AWAKE event */
1575 	adapter->pm_wakeup_fw_try = false;
1576 	del_timer(&adapter->wakeup_timer);
1577 	adapter->pm_wakeup_card_req = false;
1578 	adapter->ps_state = PS_STATE_AWAKE;
1579 
1580 	return 0;
1581 }
1582 
1583 static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter)
1584 {
1585 	struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1586 	int i;
1587 	struct urb_context *ctx;
1588 
1589 	for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1590 		if (card->rx_data_list[i].skb)
1591 			continue;
1592 		ctx = &card->rx_data_list[i];
1593 		mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE);
1594 	}
1595 }
1596 
1597 /* This function is called after the card has woken up. */
1598 static inline int
1599 mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
1600 {
1601 	return 0;
1602 }
1603 
1604 static struct mwifiex_if_ops usb_ops = {
1605 	.register_dev =		mwifiex_register_dev,
1606 	.unregister_dev =	mwifiex_unregister_dev,
1607 	.wakeup =		mwifiex_pm_wakeup_card,
1608 	.wakeup_complete =	mwifiex_pm_wakeup_card_complete,
1609 
1610 	/* USB specific */
1611 	.dnld_fw =		mwifiex_usb_dnld_fw,
1612 	.cmdrsp_complete =	mwifiex_usb_cmd_event_complete,
1613 	.event_complete =	mwifiex_usb_cmd_event_complete,
1614 	.host_to_card =		mwifiex_usb_host_to_card,
1615 	.submit_rem_rx_urbs =	mwifiex_usb_submit_rem_rx_urbs,
1616 	.multi_port_resync =	mwifiex_usb_port_resync,
1617 	.is_port_ready =	mwifiex_usb_is_port_ready,
1618 };
1619 
1620 module_usb_driver(mwifiex_usb_driver);
1621 
1622 MODULE_AUTHOR("Marvell International Ltd.");
1623 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1624 MODULE_VERSION(USB_VERSION);
1625 MODULE_LICENSE("GPL v2");
1626 MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME);
1627 MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME);
1628 MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME);
1629 MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME);
1630