Linux 4.16.11
[linux/fpc-iii.git] / drivers / net / wireless / marvell / mwifiex / usb.c
blob4bc244801636902860e3ee9542af3ceada1a41d6
1 /*
2 * Marvell Wireless LAN device driver: USB specific handling
4 * Copyright (C) 2012-2014, Marvell International Ltd.
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.
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.
20 #include "main.h"
21 #include "usb.h"
23 #define USB_VERSION "1.0"
25 static struct mwifiex_if_ops usb_ops;
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 */
51 MODULE_DEVICE_TABLE(usb, mwifiex_usb_table);
53 static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size);
55 /* This function handles received packet. Necessary action is taken based on
56 * cmd/event/data.
58 static int mwifiex_usb_recv(struct mwifiex_adapter *adapter,
59 struct sk_buff *skb, u8 ep)
61 u32 recv_type;
62 __le32 tmp;
63 int ret;
65 if (adapter->hs_activated)
66 mwifiex_process_hs_config(adapter);
68 if (skb->len < INTF_HEADER_LEN) {
69 mwifiex_dbg(adapter, ERROR,
70 "%s: invalid skb->len\n", __func__);
71 return -1;
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);
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;
98 ret = -1;
99 goto exit_restore_skb;
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;
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);
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;
124 memcpy(adapter->event_body, skb->data +
125 MWIFIEX_EVENT_HEADER_LEN, skb->len);
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;
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;
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;
154 return -EINPROGRESS;
156 exit_restore_skb:
157 /* The buffer will be reused for further cmds/events */
158 skb_push(skb, INTF_HEADER_LEN);
160 return ret;
163 static void mwifiex_usb_rx_complete(struct urb *urb)
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;
172 if (!adapter || !adapter->card) {
173 pr_err("mwifiex adapter or card structure is not valid\n");
174 return;
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);
183 if (recv_length) {
184 if (urb->status || (adapter->surprise_removed)) {
185 mwifiex_dbg(adapter, ERROR,
186 "URB status is failed: %d\n", urb->status);
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;
192 if (skb->len > recv_length)
193 skb_trim(skb, recv_length);
194 else
195 skb_put(skb, recv_length - skb->len);
197 status = mwifiex_usb_recv(adapter, skb, context->ep);
199 mwifiex_dbg(adapter, INFO,
200 "info: recv_length=%d, status=%d\n",
201 recv_length, status);
202 if (status == -EINPROGRESS) {
203 mwifiex_queue_main_work(adapter);
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
209 if (card->rx_cmd_ep == context->ep)
210 return;
211 } else {
212 if (status == -1)
213 mwifiex_dbg(adapter, ERROR,
214 "received data processing failed!\n");
216 /* Do not free skb in case of command ep */
217 if (card->rx_cmd_ep != context->ep)
218 dev_kfree_skb_any(skb);
220 } else if (urb->status) {
221 if (!adapter->is_suspended) {
222 mwifiex_dbg(adapter, FATAL,
223 "Card is removed: %d\n", urb->status);
224 adapter->surprise_removed = true;
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);
233 /* fall through setup_for_next */
236 setup_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;
242 if (card->rx_cmd_ep == context->ep) {
243 mwifiex_usb_submit_rx_urb(context, size);
244 } else {
245 if (atomic_read(&adapter->rx_pending) <= HIGH_RX_PENDING) {
246 mwifiex_usb_submit_rx_urb(context, size);
247 } else {
248 context->skb = NULL;
252 return;
255 static void mwifiex_usb_tx_complete(struct urb *urb)
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;
260 struct usb_tx_data_port *port;
261 int i;
263 mwifiex_dbg(adapter, INFO,
264 "%s: status: %d\n", __func__, urb->status);
266 if (context->ep == card->tx_cmd_ep) {
267 mwifiex_dbg(adapter, CMD,
268 "%s: CMD\n", __func__);
269 atomic_dec(&card->tx_cmd_urb_pending);
270 adapter->cmd_sent = false;
271 } else {
272 mwifiex_dbg(adapter, DATA,
273 "%s: DATA\n", __func__);
274 mwifiex_write_data_complete(adapter, context->skb, 0,
275 urb->status ? -1 : 0);
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);
280 port->block_status = false;
281 break;
284 adapter->data_sent = false;
287 if (card->mc_resync_flag)
288 mwifiex_multi_chan_resync(adapter);
290 mwifiex_queue_main_work(adapter);
292 return;
295 static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size)
297 struct mwifiex_adapter *adapter = ctx->adapter;
298 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
300 if (card->rx_cmd_ep != ctx->ep) {
301 ctx->skb = dev_alloc_skb(size);
302 if (!ctx->skb) {
303 mwifiex_dbg(adapter, ERROR,
304 "%s: dev_alloc_skb failed\n", __func__);
305 return -ENOMEM;
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);
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);
326 if (usb_submit_urb(ctx->urb, GFP_ATOMIC)) {
327 mwifiex_dbg(adapter, ERROR, "usb_submit_urb failed\n");
328 dev_kfree_skb_any(ctx->skb);
329 ctx->skb = NULL;
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);
336 return -1;
339 return 0;
342 static void mwifiex_usb_free(struct usb_card_rec *card)
344 struct usb_tx_data_port *port;
345 int i, j;
347 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
348 usb_kill_urb(card->rx_cmd.urb);
350 usb_free_urb(card->rx_cmd.urb);
351 card->rx_cmd.urb = NULL;
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);
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;
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++) {
366 usb_kill_urb(port->tx_data_list[j].urb);
367 usb_free_urb(port->tx_data_list[j].urb);
368 port->tx_data_list[j].urb = NULL;
372 usb_free_urb(card->tx_cmd.urb);
373 card->tx_cmd.urb = NULL;
375 return;
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.
382 static int mwifiex_usb_probe(struct usb_interface *intf,
383 const struct usb_device_id *id)
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;
390 u16 id_vendor, id_product, bcd_device;
392 card = devm_kzalloc(&intf->dev, sizeof(*card), GFP_KERNEL);
393 if (!card)
394 return -ENOMEM;
396 init_completion(&card->fw_done);
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);
401 pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n",
402 id_vendor, id_product, bcd_device);
404 /* PID_1 is used for firmware downloading only */
405 switch (id_product) {
406 case USB8766_PID_1:
407 case USB8797_PID_1:
408 case USB8801_PID_1:
409 case USB8997_PID_1:
410 card->usb_boot_state = USB8XXX_FW_DNLD;
411 break;
412 case USB8766_PID_2:
413 case USB8797_PID_2:
414 case USB8801_PID_2:
415 case USB8997_PID_2:
416 card->usb_boot_state = USB8XXX_FW_READY;
417 break;
418 default:
419 pr_warn("unknown id_product %#x\n", id_product);
420 card->usb_boot_state = USB8XXX_FW_DNLD;
421 break;
424 card->udev = udev;
425 card->intf = intf;
427 pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
428 le16_to_cpu(udev->descriptor.bcdUSB),
429 udev->descriptor.bDeviceClass,
430 udev->descriptor.bDeviceSubClass,
431 udev->descriptor.bDeviceProtocol);
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 &&
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",
442 le16_to_cpu(epd->wMaxPacketSize),
443 epd->bEndpointAddress, card->rx_cmd_ep_type);
444 card->rx_cmd_ep = usb_endpoint_num(epd);
445 atomic_set(&card->rx_cmd_urb_pending, 0);
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);
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);
462 card->port[0].tx_data_ep = usb_endpoint_num(epd);
463 atomic_set(&card->port[0].tx_data_urb_pending, 0);
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);
475 if (usb_endpoint_dir_out(epd) &&
476 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
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;
481 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
482 le16_to_cpu(epd->wMaxPacketSize),
483 epd->bEndpointAddress);
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);
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);
494 usb_set_intfdata(intf, card);
496 ret = mwifiex_add_card(card, &card->fw_done, &usb_ops,
497 MWIFIEX_USB, &card->udev->dev);
498 if (ret) {
499 pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret);
500 usb_reset_device(udev);
501 return ret;
504 usb_get_dev(udev);
506 return 0;
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.
513 * If already not suspended, this function allocates and sends a
514 * 'host sleep activate' request to the firmware and turns off the traffic.
516 static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message)
518 struct usb_card_rec *card = usb_get_intfdata(intf);
519 struct mwifiex_adapter *adapter;
520 struct usb_tx_data_port *port;
521 int i, j;
523 /* Might still be loading firmware */
524 wait_for_completion(&card->fw_done);
526 adapter = card->adapter;
527 if (!adapter) {
528 dev_err(&intf->dev, "card is not valid\n");
529 return 0;
532 if (unlikely(adapter->is_suspended))
533 mwifiex_dbg(adapter, WARN,
534 "Device already suspended\n");
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;
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.
551 adapter->is_suspended = true;
552 adapter->hs_enabling = false;
554 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
555 usb_kill_urb(card->rx_cmd.urb);
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);
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);
570 if (card->tx_cmd.urb)
571 usb_kill_urb(card->tx_cmd.urb);
573 return 0;
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.
580 * If already not resumed, this function turns on the traffic and
581 * sends a 'host sleep cancel' request to the firmware.
583 static int mwifiex_usb_resume(struct usb_interface *intf)
585 struct usb_card_rec *card = usb_get_intfdata(intf);
586 struct mwifiex_adapter *adapter;
587 int i;
589 if (!card->adapter) {
590 dev_err(&intf->dev, "%s: card->adapter is NULL\n",
591 __func__);
592 return 0;
594 adapter = card->adapter;
596 if (unlikely(!adapter->is_suspended)) {
597 mwifiex_dbg(adapter, WARN,
598 "Device already resumed\n");
599 return 0;
602 /* Indicate device resumed. The netdev queue will be resumed only
603 * after the urbs have been re-submitted
605 adapter->is_suspended = false;
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);
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);
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);
625 return 0;
628 static void mwifiex_usb_disconnect(struct usb_interface *intf)
630 struct usb_card_rec *card = usb_get_intfdata(intf);
631 struct mwifiex_adapter *adapter;
633 wait_for_completion(&card->fw_done);
635 adapter = card->adapter;
636 if (!adapter || !adapter->priv_num)
637 return;
639 if (card->udev->state != USB_STATE_NOTATTACHED && !adapter->mfg_mode) {
640 mwifiex_deauthenticate_all(adapter);
642 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
643 MWIFIEX_BSS_ROLE_ANY),
644 MWIFIEX_FUNC_SHUTDOWN);
647 mwifiex_usb_free(card);
649 mwifiex_dbg(adapter, FATAL,
650 "%s: removing card\n", __func__);
651 mwifiex_remove_card(adapter);
653 usb_put_dev(interface_to_usbdev(intf));
656 static struct usb_driver mwifiex_usb_driver = {
657 .name = "mwifiex_usb",
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,
663 .soft_unbind = 1,
666 static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
667 u32 *len, u8 ep, u32 timeout)
669 struct usb_card_rec *card = adapter->card;
670 int actual_length, ret;
672 if (!(*len % card->bulk_out_maxpktsize))
673 (*len)++;
675 /* Send the data block */
676 ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
677 *len, &actual_length, timeout);
678 if (ret) {
679 mwifiex_dbg(adapter, ERROR,
680 "usb_bulk_msg for tx failed: %d\n", ret);
681 return ret;
684 *len = actual_length;
686 return ret;
689 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
690 u32 *len, u8 ep, u32 timeout)
692 struct usb_card_rec *card = adapter->card;
693 int actual_length, ret;
695 /* Receive the data response */
696 ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
697 *len, &actual_length, timeout);
698 if (ret) {
699 mwifiex_dbg(adapter, ERROR,
700 "usb_bulk_msg for rx failed: %d\n", ret);
701 return ret;
704 *len = actual_length;
706 return ret;
709 static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter)
711 struct usb_card_rec *card = adapter->card;
712 u8 active_port = MWIFIEX_USB_EP_DATA;
713 struct mwifiex_private *priv = NULL;
714 int i;
716 if (adapter->usb_mc_status) {
717 for (i = 0; i < adapter->priv_num; i++) {
718 priv = adapter->priv[i];
719 if (!priv)
720 continue;
721 if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
722 !priv->bss_started) ||
723 (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
724 !priv->media_connected))
725 priv->usb_port = MWIFIEX_USB_EP_DATA;
727 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
728 card->port[i].block_status = false;
729 } else {
730 for (i = 0; i < adapter->priv_num; i++) {
731 priv = adapter->priv[i];
732 if (!priv)
733 continue;
734 if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
735 priv->bss_started) ||
736 (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
737 priv->media_connected)) {
738 active_port = priv->usb_port;
739 break;
742 for (i = 0; i < adapter->priv_num; i++) {
743 priv = adapter->priv[i];
744 if (priv)
745 priv->usb_port = active_port;
747 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
748 if (active_port == card->port[i].tx_data_ep)
749 card->port[i].block_status = false;
750 else
751 card->port[i].block_status = true;
756 static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv)
758 struct usb_card_rec *card = priv->adapter->card;
759 int idx;
761 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
762 if (priv->usb_port == card->port[idx].tx_data_ep)
763 return !card->port[idx].block_status;
766 return false;
769 static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter)
771 struct usb_card_rec *card = adapter->card;
772 int i;
774 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
775 if (!card->port[i].block_status)
776 return false;
778 return true;
781 static int mwifiex_usb_construct_send_urb(struct mwifiex_adapter *adapter,
782 struct usb_tx_data_port *port, u8 ep,
783 struct urb_context *context,
784 struct sk_buff *skb_send)
786 struct usb_card_rec *card = adapter->card;
787 int ret = -EINPROGRESS;
788 struct urb *tx_urb;
790 context->adapter = adapter;
791 context->ep = ep;
792 context->skb = skb_send;
793 tx_urb = context->urb;
795 if (ep == card->tx_cmd_ep &&
796 card->tx_cmd_ep_type == USB_ENDPOINT_XFER_INT)
797 usb_fill_int_urb(tx_urb, card->udev,
798 usb_sndintpipe(card->udev, ep), skb_send->data,
799 skb_send->len, mwifiex_usb_tx_complete,
800 (void *)context, card->tx_cmd_interval);
801 else
802 usb_fill_bulk_urb(tx_urb, card->udev,
803 usb_sndbulkpipe(card->udev, ep),
804 skb_send->data, skb_send->len,
805 mwifiex_usb_tx_complete, (void *)context);
807 tx_urb->transfer_flags |= URB_ZERO_PACKET;
809 if (ep == card->tx_cmd_ep)
810 atomic_inc(&card->tx_cmd_urb_pending);
811 else
812 atomic_inc(&port->tx_data_urb_pending);
814 if (ep != card->tx_cmd_ep &&
815 atomic_read(&port->tx_data_urb_pending) ==
816 MWIFIEX_TX_DATA_URB) {
817 port->block_status = true;
818 adapter->data_sent = mwifiex_usb_data_sent(adapter);
819 ret = -ENOSR;
822 if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
823 mwifiex_dbg(adapter, ERROR,
824 "%s: usb_submit_urb failed\n", __func__);
825 if (ep == card->tx_cmd_ep) {
826 atomic_dec(&card->tx_cmd_urb_pending);
827 } else {
828 atomic_dec(&port->tx_data_urb_pending);
829 port->block_status = false;
830 adapter->data_sent = false;
831 if (port->tx_data_ix)
832 port->tx_data_ix--;
833 else
834 port->tx_data_ix = MWIFIEX_TX_DATA_URB;
836 ret = -1;
839 return ret;
842 static int mwifiex_usb_prepare_tx_aggr_skb(struct mwifiex_adapter *adapter,
843 struct usb_tx_data_port *port,
844 struct sk_buff **skb_send)
846 struct sk_buff *skb_aggr, *skb_tmp;
847 u8 *payload, pad;
848 u16 align = adapter->bus_aggr.tx_aggr_align;
849 struct mwifiex_txinfo *tx_info = NULL;
850 bool is_txinfo_set = false;
852 /* Packets in aggr_list will be send in either skb_aggr or
853 * write complete, delete the tx_aggr timer
855 if (port->tx_aggr.timer_cnxt.is_hold_timer_set) {
856 del_timer(&port->tx_aggr.timer_cnxt.hold_timer);
857 port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
858 port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
861 skb_aggr = mwifiex_alloc_dma_align_buf(port->tx_aggr.aggr_len,
862 GFP_ATOMIC);
863 if (!skb_aggr) {
864 mwifiex_dbg(adapter, ERROR,
865 "%s: alloc skb_aggr failed\n", __func__);
867 while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list)))
868 mwifiex_write_data_complete(adapter, skb_tmp, 0, -1);
870 port->tx_aggr.aggr_num = 0;
871 port->tx_aggr.aggr_len = 0;
872 return -EBUSY;
875 tx_info = MWIFIEX_SKB_TXCB(skb_aggr);
876 memset(tx_info, 0, sizeof(*tx_info));
878 while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list))) {
879 /* padding for aligning next packet header*/
880 pad = (align - (skb_tmp->len & (align - 1))) % align;
881 payload = skb_put(skb_aggr, skb_tmp->len + pad);
882 memcpy(payload, skb_tmp->data, skb_tmp->len);
883 if (skb_queue_empty(&port->tx_aggr.aggr_list)) {
884 /* do not padding for last packet*/
885 *(u16 *)payload = cpu_to_le16(skb_tmp->len);
886 *(u16 *)&payload[2] =
887 cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80);
888 skb_trim(skb_aggr, skb_aggr->len - pad);
889 } else {
890 /* add aggregation interface header */
891 *(u16 *)payload = cpu_to_le16(skb_tmp->len + pad);
892 *(u16 *)&payload[2] =
893 cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2);
896 if (!is_txinfo_set) {
897 tx_info->bss_num = MWIFIEX_SKB_TXCB(skb_tmp)->bss_num;
898 tx_info->bss_type = MWIFIEX_SKB_TXCB(skb_tmp)->bss_type;
899 is_txinfo_set = true;
902 port->tx_aggr.aggr_num--;
903 port->tx_aggr.aggr_len -= (skb_tmp->len + pad);
904 mwifiex_write_data_complete(adapter, skb_tmp, 0, 0);
907 tx_info->pkt_len = skb_aggr->len -
908 (sizeof(struct txpd) + adapter->intf_hdr_len);
909 tx_info->flags |= MWIFIEX_BUF_FLAG_AGGR_PKT;
911 port->tx_aggr.aggr_num = 0;
912 port->tx_aggr.aggr_len = 0;
913 *skb_send = skb_aggr;
915 return 0;
918 /* This function prepare data packet to be send under usb tx aggregation
919 * protocol, check current usb aggregation status, link packet to aggrgation
920 * list if possible, work flow as below:
921 * (1) if only 1 packet available, add usb tx aggregation header and send.
922 * (2) if packet is able to aggregated, link it to current aggregation list.
923 * (3) if packet is not able to aggregated, aggregate and send exist packets
924 * in aggrgation list. Then, link packet in the list if there is more
925 * packet in transmit queue, otherwise try to transmit single packet.
927 static int mwifiex_usb_aggr_tx_data(struct mwifiex_adapter *adapter, u8 ep,
928 struct sk_buff *skb,
929 struct mwifiex_tx_param *tx_param,
930 struct usb_tx_data_port *port)
932 u8 *payload, pad;
933 u16 align = adapter->bus_aggr.tx_aggr_align;
934 struct sk_buff *skb_send = NULL;
935 struct urb_context *context = NULL;
936 struct txpd *local_tx_pd =
937 (struct txpd *)((u8 *)skb->data + adapter->intf_hdr_len);
938 u8 f_send_aggr_buf = 0;
939 u8 f_send_cur_buf = 0;
940 u8 f_precopy_cur_buf = 0;
941 u8 f_postcopy_cur_buf = 0;
942 u32 timeout;
943 int ret;
945 /* padding to ensure each packet alginment */
946 pad = (align - (skb->len & (align - 1))) % align;
948 if (tx_param && tx_param->next_pkt_len) {
949 /* next packet available in tx queue*/
950 if (port->tx_aggr.aggr_len + skb->len + pad >
951 adapter->bus_aggr.tx_aggr_max_size) {
952 f_send_aggr_buf = 1;
953 f_postcopy_cur_buf = 1;
954 } else {
955 /* current packet could be aggregated*/
956 f_precopy_cur_buf = 1;
958 if (port->tx_aggr.aggr_len + skb->len + pad +
959 tx_param->next_pkt_len >
960 adapter->bus_aggr.tx_aggr_max_size ||
961 port->tx_aggr.aggr_num + 2 >
962 adapter->bus_aggr.tx_aggr_max_num) {
963 /* next packet could not be aggregated
964 * send current aggregation buffer
966 f_send_aggr_buf = 1;
969 } else {
970 /* last packet in tx queue */
971 if (port->tx_aggr.aggr_num > 0) {
972 /* pending packets in aggregation buffer*/
973 if (port->tx_aggr.aggr_len + skb->len + pad >
974 adapter->bus_aggr.tx_aggr_max_size) {
975 /* current packet not be able to aggregated,
976 * send aggr buffer first, then send packet.
978 f_send_cur_buf = 1;
979 } else {
980 /* last packet, Aggregation and send */
981 f_precopy_cur_buf = 1;
984 f_send_aggr_buf = 1;
985 } else {
986 /* no pending packets in aggregation buffer,
987 * send current packet immediately
989 f_send_cur_buf = 1;
993 if (local_tx_pd->flags & MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET) {
994 /* Send NULL packet immediately*/
995 if (f_precopy_cur_buf) {
996 if (skb_queue_empty(&port->tx_aggr.aggr_list)) {
997 f_precopy_cur_buf = 0;
998 f_send_aggr_buf = 0;
999 f_send_cur_buf = 1;
1000 } else {
1001 f_send_aggr_buf = 1;
1003 } else if (f_postcopy_cur_buf) {
1004 f_send_cur_buf = 1;
1005 f_postcopy_cur_buf = 0;
1009 if (f_precopy_cur_buf) {
1010 skb_queue_tail(&port->tx_aggr.aggr_list, skb);
1011 port->tx_aggr.aggr_len += (skb->len + pad);
1012 port->tx_aggr.aggr_num++;
1013 if (f_send_aggr_buf)
1014 goto send_aggr_buf;
1016 /* packet will not been send immediately,
1017 * set a timer to make sure it will be sent under
1018 * strict time limit. Dynamically fit the timeout
1019 * value, according to packets number in aggr_list
1021 if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) {
1022 port->tx_aggr.timer_cnxt.hold_tmo_msecs =
1023 MWIFIEX_USB_TX_AGGR_TMO_MIN;
1024 timeout =
1025 port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1026 mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1027 jiffies + msecs_to_jiffies(timeout));
1028 port->tx_aggr.timer_cnxt.is_hold_timer_set = true;
1029 } else {
1030 if (port->tx_aggr.timer_cnxt.hold_tmo_msecs <
1031 MWIFIEX_USB_TX_AGGR_TMO_MAX) {
1032 /* Dyanmic fit timeout */
1033 timeout =
1034 ++port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1035 mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1036 jiffies + msecs_to_jiffies(timeout));
1041 send_aggr_buf:
1042 if (f_send_aggr_buf) {
1043 ret = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send);
1044 if (!ret) {
1045 context = &port->tx_data_list[port->tx_data_ix++];
1046 ret = mwifiex_usb_construct_send_urb(adapter, port, ep,
1047 context, skb_send);
1048 if (ret == -1)
1049 mwifiex_write_data_complete(adapter, skb_send,
1050 0, -1);
1054 if (f_send_cur_buf) {
1055 if (f_send_aggr_buf) {
1056 if (atomic_read(&port->tx_data_urb_pending) >=
1057 MWIFIEX_TX_DATA_URB) {
1058 port->block_status = true;
1059 adapter->data_sent =
1060 mwifiex_usb_data_sent(adapter);
1061 /* no available urb, postcopy packet*/
1062 f_postcopy_cur_buf = 1;
1063 goto postcopy_cur_buf;
1066 if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1067 port->tx_data_ix = 0;
1070 payload = skb->data;
1071 *(u16 *)&payload[2] =
1072 cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80);
1073 *(u16 *)payload = cpu_to_le16(skb->len);
1074 skb_send = skb;
1075 context = &port->tx_data_list[port->tx_data_ix++];
1076 return mwifiex_usb_construct_send_urb(adapter, port, ep,
1077 context, skb_send);
1080 postcopy_cur_buf:
1081 if (f_postcopy_cur_buf) {
1082 skb_queue_tail(&port->tx_aggr.aggr_list, skb);
1083 port->tx_aggr.aggr_len += (skb->len + pad);
1084 port->tx_aggr.aggr_num++;
1085 /* New aggregation begin, start timer */
1086 if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) {
1087 port->tx_aggr.timer_cnxt.hold_tmo_msecs =
1088 MWIFIEX_USB_TX_AGGR_TMO_MIN;
1089 timeout = port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1090 mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1091 jiffies + msecs_to_jiffies(timeout));
1092 port->tx_aggr.timer_cnxt.is_hold_timer_set = true;
1096 return -EINPROGRESS;
1099 static void mwifiex_usb_tx_aggr_tmo(struct timer_list *t)
1101 struct urb_context *urb_cnxt = NULL;
1102 struct sk_buff *skb_send = NULL;
1103 struct tx_aggr_tmr_cnxt *timer_context =
1104 from_timer(timer_context, t, hold_timer);
1105 struct mwifiex_adapter *adapter = timer_context->adapter;
1106 struct usb_tx_data_port *port = timer_context->port;
1107 unsigned long flags;
1108 int err = 0;
1110 spin_lock_irqsave(&port->tx_aggr_lock, flags);
1111 err = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send);
1112 if (err) {
1113 mwifiex_dbg(adapter, ERROR,
1114 "prepare tx aggr skb failed, err=%d\n", err);
1115 goto unlock;
1118 if (atomic_read(&port->tx_data_urb_pending) >=
1119 MWIFIEX_TX_DATA_URB) {
1120 port->block_status = true;
1121 adapter->data_sent =
1122 mwifiex_usb_data_sent(adapter);
1123 err = -1;
1124 goto done;
1127 if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1128 port->tx_data_ix = 0;
1130 urb_cnxt = &port->tx_data_list[port->tx_data_ix++];
1131 err = mwifiex_usb_construct_send_urb(adapter, port, port->tx_data_ep,
1132 urb_cnxt, skb_send);
1133 done:
1134 if (err == -1)
1135 mwifiex_write_data_complete(adapter, skb_send, 0, -1);
1136 unlock:
1137 spin_unlock_irqrestore(&port->tx_aggr_lock, flags);
1140 /* This function write a command/data packet to card. */
1141 static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
1142 struct sk_buff *skb,
1143 struct mwifiex_tx_param *tx_param)
1145 struct usb_card_rec *card = adapter->card;
1146 struct urb_context *context = NULL;
1147 struct usb_tx_data_port *port = NULL;
1148 unsigned long flags;
1149 int idx, ret;
1151 if (adapter->is_suspended) {
1152 mwifiex_dbg(adapter, ERROR,
1153 "%s: not allowed while suspended\n", __func__);
1154 return -1;
1157 if (adapter->surprise_removed) {
1158 mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__);
1159 return -1;
1162 mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep);
1164 if (ep == card->tx_cmd_ep) {
1165 context = &card->tx_cmd;
1166 } else {
1167 /* get the data port structure for endpoint */
1168 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
1169 if (ep == card->port[idx].tx_data_ep) {
1170 port = &card->port[idx];
1171 if (atomic_read(&port->tx_data_urb_pending)
1172 >= MWIFIEX_TX_DATA_URB) {
1173 port->block_status = true;
1174 adapter->data_sent =
1175 mwifiex_usb_data_sent(adapter);
1176 return -EBUSY;
1178 if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1179 port->tx_data_ix = 0;
1180 break;
1184 if (!port) {
1185 mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n");
1186 return -1;
1189 if (adapter->bus_aggr.enable) {
1190 spin_lock_irqsave(&port->tx_aggr_lock, flags);
1191 ret = mwifiex_usb_aggr_tx_data(adapter, ep, skb,
1192 tx_param, port);
1193 spin_unlock_irqrestore(&port->tx_aggr_lock, flags);
1194 return ret;
1197 context = &port->tx_data_list[port->tx_data_ix++];
1200 return mwifiex_usb_construct_send_urb(adapter, port, ep, context, skb);
1203 static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter)
1205 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1206 struct usb_tx_data_port *port;
1207 int i, j;
1209 card->tx_cmd.adapter = adapter;
1210 card->tx_cmd.ep = card->tx_cmd_ep;
1212 card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
1213 if (!card->tx_cmd.urb)
1214 return -ENOMEM;
1216 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
1217 port = &card->port[i];
1218 if (!port->tx_data_ep)
1219 continue;
1220 port->tx_data_ix = 0;
1221 skb_queue_head_init(&port->tx_aggr.aggr_list);
1222 if (port->tx_data_ep == MWIFIEX_USB_EP_DATA)
1223 port->block_status = false;
1224 else
1225 port->block_status = true;
1226 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
1227 port->tx_data_list[j].adapter = adapter;
1228 port->tx_data_list[j].ep = port->tx_data_ep;
1229 port->tx_data_list[j].urb =
1230 usb_alloc_urb(0, GFP_KERNEL);
1231 if (!port->tx_data_list[j].urb)
1232 return -ENOMEM;
1235 port->tx_aggr.timer_cnxt.adapter = adapter;
1236 port->tx_aggr.timer_cnxt.port = port;
1237 port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
1238 port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
1239 timer_setup(&port->tx_aggr.timer_cnxt.hold_timer,
1240 mwifiex_usb_tx_aggr_tmo, 0);
1243 return 0;
1246 static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
1248 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1249 int i;
1251 card->rx_cmd.adapter = adapter;
1252 card->rx_cmd.ep = card->rx_cmd_ep;
1254 card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
1255 if (!card->rx_cmd.urb)
1256 return -ENOMEM;
1258 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
1259 if (!card->rx_cmd.skb)
1260 return -ENOMEM;
1262 if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
1263 return -1;
1265 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1266 card->rx_data_list[i].adapter = adapter;
1267 card->rx_data_list[i].ep = card->rx_data_ep;
1269 card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1270 if (!card->rx_data_list[i].urb)
1271 return -1;
1272 if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
1273 MWIFIEX_RX_DATA_BUF_SIZE))
1274 return -1;
1277 return 0;
1280 /* This function register usb device and initialize parameter. */
1281 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1283 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1285 card->adapter = adapter;
1287 switch (le16_to_cpu(card->udev->descriptor.idProduct)) {
1288 case USB8997_PID_1:
1289 case USB8997_PID_2:
1290 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K;
1291 strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME);
1292 adapter->ext_scan = true;
1293 break;
1294 case USB8766_PID_1:
1295 case USB8766_PID_2:
1296 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1297 strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME);
1298 adapter->ext_scan = true;
1299 break;
1300 case USB8801_PID_1:
1301 case USB8801_PID_2:
1302 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1303 strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME);
1304 adapter->ext_scan = false;
1305 break;
1306 case USB8797_PID_1:
1307 case USB8797_PID_2:
1308 default:
1309 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1310 strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
1311 break;
1314 adapter->usb_mc_status = false;
1315 adapter->usb_mc_setup = false;
1317 return 0;
1320 static void mwifiex_usb_cleanup_tx_aggr(struct mwifiex_adapter *adapter)
1322 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1323 struct usb_tx_data_port *port;
1324 struct sk_buff *skb_tmp;
1325 int idx;
1327 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
1328 port = &card->port[idx];
1329 if (adapter->bus_aggr.enable)
1330 while ((skb_tmp =
1331 skb_dequeue(&port->tx_aggr.aggr_list)))
1332 mwifiex_write_data_complete(adapter, skb_tmp,
1333 0, -1);
1334 del_timer_sync(&port->tx_aggr.timer_cnxt.hold_timer);
1335 port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
1336 port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
1340 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1342 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1344 mwifiex_usb_cleanup_tx_aggr(adapter);
1346 card->adapter = NULL;
1349 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1350 struct mwifiex_fw_image *fw)
1352 int ret = 0;
1353 u8 *firmware = fw->fw_buf, *recv_buff;
1354 u32 retries = USB8XXX_FW_MAX_RETRY + 1;
1355 u32 dlen;
1356 u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
1357 struct fw_data *fwdata;
1358 struct fw_sync_header sync_fw;
1359 u8 check_winner = 1;
1361 if (!firmware) {
1362 mwifiex_dbg(adapter, ERROR,
1363 "No firmware image found! Terminating download\n");
1364 ret = -1;
1365 goto fw_exit;
1368 /* Allocate memory for transmit */
1369 fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
1370 if (!fwdata) {
1371 ret = -ENOMEM;
1372 goto fw_exit;
1375 /* Allocate memory for receive */
1376 recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
1377 if (!recv_buff) {
1378 ret = -ENOMEM;
1379 goto cleanup;
1382 do {
1383 /* Send pseudo data to check winner status first */
1384 if (check_winner) {
1385 memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
1386 dlen = 0;
1387 } else {
1388 /* copy the header of the fw_data to get the length */
1389 memcpy(&fwdata->fw_hdr, &firmware[tlen],
1390 sizeof(struct fw_header));
1392 dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
1393 dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
1394 tlen += sizeof(struct fw_header);
1396 /* Command 7 doesn't have data length field */
1397 if (dnld_cmd == FW_CMD_7)
1398 dlen = 0;
1400 memcpy(fwdata->data, &firmware[tlen], dlen);
1402 fwdata->seq_num = cpu_to_le32(fw_seqnum);
1403 tlen += dlen;
1406 /* If the send/receive fails or CRC occurs then retry */
1407 while (--retries) {
1408 u8 *buf = (u8 *)fwdata;
1409 u32 len = FW_DATA_XMIT_SIZE;
1411 /* send the firmware block */
1412 ret = mwifiex_write_data_sync(adapter, buf, &len,
1413 MWIFIEX_USB_EP_CMD_EVENT,
1414 MWIFIEX_USB_TIMEOUT);
1415 if (ret) {
1416 mwifiex_dbg(adapter, ERROR,
1417 "write_data_sync: failed: %d\n",
1418 ret);
1419 continue;
1422 buf = recv_buff;
1423 len = FW_DNLD_RX_BUF_SIZE;
1425 /* Receive the firmware block response */
1426 ret = mwifiex_read_data_sync(adapter, buf, &len,
1427 MWIFIEX_USB_EP_CMD_EVENT,
1428 MWIFIEX_USB_TIMEOUT);
1429 if (ret) {
1430 mwifiex_dbg(adapter, ERROR,
1431 "read_data_sync: failed: %d\n",
1432 ret);
1433 continue;
1436 memcpy(&sync_fw, recv_buff,
1437 sizeof(struct fw_sync_header));
1439 /* check 1st firmware block resp for highest bit set */
1440 if (check_winner) {
1441 if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
1442 mwifiex_dbg(adapter, WARN,
1443 "USB is not the winner %#x\n",
1444 sync_fw.cmd);
1446 /* returning success */
1447 ret = 0;
1448 goto cleanup;
1451 mwifiex_dbg(adapter, MSG,
1452 "start to download FW...\n");
1454 check_winner = 0;
1455 break;
1458 /* check the firmware block response for CRC errors */
1459 if (sync_fw.cmd) {
1460 mwifiex_dbg(adapter, ERROR,
1461 "FW received block with CRC %#x\n",
1462 sync_fw.cmd);
1463 ret = -1;
1464 continue;
1467 retries = USB8XXX_FW_MAX_RETRY + 1;
1468 break;
1470 fw_seqnum++;
1471 } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
1473 cleanup:
1474 mwifiex_dbg(adapter, MSG,
1475 "info: FW download over, size %d bytes\n", tlen);
1477 kfree(recv_buff);
1478 kfree(fwdata);
1480 if (retries)
1481 ret = 0;
1482 fw_exit:
1483 return ret;
1486 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
1487 struct mwifiex_fw_image *fw)
1489 int ret;
1490 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1492 if (card->usb_boot_state == USB8XXX_FW_DNLD) {
1493 ret = mwifiex_prog_fw_w_helper(adapter, fw);
1494 if (ret)
1495 return -1;
1497 /* Boot state changes after successful firmware download */
1498 if (card->usb_boot_state == USB8XXX_FW_DNLD)
1499 return -1;
1502 ret = mwifiex_usb_rx_init(adapter);
1503 if (!ret)
1504 ret = mwifiex_usb_tx_init(adapter);
1506 return ret;
1509 static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
1511 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1513 skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
1514 if ((ep == card->rx_cmd_ep) &&
1515 (!atomic_read(&card->rx_cmd_urb_pending)))
1516 mwifiex_usb_submit_rx_urb(&card->rx_cmd,
1517 MWIFIEX_RX_CMD_BUF_SIZE);
1519 return;
1522 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
1523 struct sk_buff *skb)
1525 mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
1527 return 0;
1530 /* This function wakes up the card. */
1531 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
1533 /* Simulation of HS_AWAKE event */
1534 adapter->pm_wakeup_fw_try = false;
1535 del_timer(&adapter->wakeup_timer);
1536 adapter->pm_wakeup_card_req = false;
1537 adapter->ps_state = PS_STATE_AWAKE;
1539 return 0;
1542 static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter)
1544 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1545 int i;
1546 struct urb_context *ctx;
1548 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1549 if (card->rx_data_list[i].skb)
1550 continue;
1551 ctx = &card->rx_data_list[i];
1552 mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE);
1556 /* This function is called after the card has woken up. */
1557 static inline int
1558 mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
1560 return 0;
1563 static struct mwifiex_if_ops usb_ops = {
1564 .register_dev = mwifiex_register_dev,
1565 .unregister_dev = mwifiex_unregister_dev,
1566 .wakeup = mwifiex_pm_wakeup_card,
1567 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1569 /* USB specific */
1570 .dnld_fw = mwifiex_usb_dnld_fw,
1571 .cmdrsp_complete = mwifiex_usb_cmd_event_complete,
1572 .event_complete = mwifiex_usb_cmd_event_complete,
1573 .host_to_card = mwifiex_usb_host_to_card,
1574 .submit_rem_rx_urbs = mwifiex_usb_submit_rem_rx_urbs,
1575 .multi_port_resync = mwifiex_usb_port_resync,
1576 .is_port_ready = mwifiex_usb_is_port_ready,
1579 module_usb_driver(mwifiex_usb_driver);
1581 MODULE_AUTHOR("Marvell International Ltd.");
1582 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1583 MODULE_VERSION(USB_VERSION);
1584 MODULE_LICENSE("GPL v2");
1585 MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME);
1586 MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME);
1587 MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME);
1588 MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME);