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