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