2 * Marvell Wireless LAN device driver: USB specific handling
4 * Copyright (C) 2012, 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.
23 #define USB_VERSION "1.0"
25 static const char usbdriver_name[] = "usb8797";
28 static struct mwifiex_if_ops usb_ops;
29 static struct semaphore add_remove_card_sem;
31 static struct usb_device_id mwifiex_usb_table[] = {
32 {USB_DEVICE(USB8797_VID, USB8797_PID_1)},
33 {USB_DEVICE_AND_INTERFACE_INFO(USB8797_VID, USB8797_PID_2,
34 USB_CLASS_VENDOR_SPEC,
35 USB_SUBCLASS_VENDOR_SPEC, 0xff)},
36 { } /* Terminating entry */
39 MODULE_DEVICE_TABLE(usb, mwifiex_usb_table);
41 static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size);
43 /* This function handles received packet. Necessary action is taken based on
46 static int mwifiex_usb_recv(struct mwifiex_adapter *adapter,
47 struct sk_buff *skb, u8 ep)
49 struct device *dev = adapter->dev;
54 if (adapter->hs_activated)
55 mwifiex_process_hs_config(adapter);
57 if (skb->len < INTF_HEADER_LEN) {
58 dev_err(dev, "%s: invalid skb->len\n", __func__);
63 case MWIFIEX_USB_EP_CMD_EVENT:
64 dev_dbg(dev, "%s: EP_CMD_EVENT\n", __func__);
65 skb_copy_from_linear_data(skb, &tmp, INTF_HEADER_LEN);
66 recv_type = le32_to_cpu(tmp);
67 skb_pull(skb, INTF_HEADER_LEN);
70 case MWIFIEX_USB_TYPE_CMD:
71 if (skb->len > MWIFIEX_SIZE_OF_CMD_BUFFER) {
72 dev_err(dev, "CMD: skb->len too large\n");
74 goto exit_restore_skb;
75 } else if (!adapter->curr_cmd) {
76 dev_dbg(dev, "CMD: no curr_cmd\n");
77 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
78 mwifiex_process_sleep_confirm_resp(
82 goto exit_restore_skb;
85 goto exit_restore_skb;
88 adapter->curr_cmd->resp_skb = skb;
89 adapter->cmd_resp_received = true;
91 case MWIFIEX_USB_TYPE_EVENT:
92 if (skb->len < sizeof(u32)) {
93 dev_err(dev, "EVENT: skb->len too small\n");
95 goto exit_restore_skb;
97 skb_copy_from_linear_data(skb, &tmp, sizeof(u32));
98 adapter->event_cause = le32_to_cpu(tmp);
99 dev_dbg(dev, "event_cause %#x\n", adapter->event_cause);
101 if (skb->len > MAX_EVENT_SIZE) {
102 dev_err(dev, "EVENT: event body too large\n");
104 goto exit_restore_skb;
107 memcpy(adapter->event_body, skb->data +
108 MWIFIEX_EVENT_HEADER_LEN, skb->len);
110 adapter->event_received = true;
111 adapter->event_skb = skb;
114 dev_err(dev, "unknown recv_type %#x\n", recv_type);
118 case MWIFIEX_USB_EP_DATA:
119 dev_dbg(dev, "%s: EP_DATA\n", __func__);
120 if (skb->len > MWIFIEX_RX_DATA_BUF_SIZE) {
121 dev_err(dev, "DATA: skb->len too large\n");
124 skb_queue_tail(&adapter->usb_rx_data_q, skb);
125 adapter->data_received = true;
128 dev_err(dev, "%s: unknown endport %#x\n", __func__, ep);
135 /* The buffer will be reused for further cmds/events */
136 skb_push(skb, INTF_HEADER_LEN);
141 static void mwifiex_usb_rx_complete(struct urb *urb)
143 struct urb_context *context = (struct urb_context *)urb->context;
144 struct mwifiex_adapter *adapter = context->adapter;
145 struct sk_buff *skb = context->skb;
146 struct usb_card_rec *card;
147 int recv_length = urb->actual_length;
150 if (!adapter || !adapter->card) {
151 pr_err("mwifiex adapter or card structure is not valid\n");
155 card = (struct usb_card_rec *)adapter->card;
156 if (card->rx_cmd_ep == context->ep)
157 atomic_dec(&card->rx_cmd_urb_pending);
159 atomic_dec(&card->rx_data_urb_pending);
162 if (urb->status || (adapter->surprise_removed)) {
163 dev_err(adapter->dev,
164 "URB status is failed: %d\n", urb->status);
165 /* Do not free skb in case of command ep */
166 if (card->rx_cmd_ep != context->ep)
167 dev_kfree_skb_any(skb);
170 if (skb->len > recv_length)
171 skb_trim(skb, recv_length);
173 skb_put(skb, recv_length - skb->len);
175 atomic_inc(&adapter->rx_pending);
176 status = mwifiex_usb_recv(adapter, skb, context->ep);
178 dev_dbg(adapter->dev, "info: recv_length=%d, status=%d\n",
179 recv_length, status);
180 if (status == -EINPROGRESS) {
181 queue_work(adapter->workqueue, &adapter->main_work);
183 /* urb for data_ep is re-submitted now;
184 * urb for cmd_ep will be re-submitted in callback
185 * mwifiex_usb_recv_complete
187 if (card->rx_cmd_ep == context->ep)
190 atomic_dec(&adapter->rx_pending);
192 dev_err(adapter->dev,
193 "received data processing failed!\n");
195 /* Do not free skb in case of command ep */
196 if (card->rx_cmd_ep != context->ep)
197 dev_kfree_skb_any(skb);
199 } else if (urb->status) {
200 if (!adapter->is_suspended) {
201 dev_warn(adapter->dev,
202 "Card is removed: %d\n", urb->status);
203 adapter->surprise_removed = true;
205 dev_kfree_skb_any(skb);
208 /* Do not free skb in case of command ep */
209 if (card->rx_cmd_ep != context->ep)
210 dev_kfree_skb_any(skb);
212 /* fall through setup_for_next */
216 if (card->rx_cmd_ep == context->ep)
217 size = MWIFIEX_RX_CMD_BUF_SIZE;
219 size = MWIFIEX_RX_DATA_BUF_SIZE;
221 mwifiex_usb_submit_rx_urb(context, size);
226 static void mwifiex_usb_tx_complete(struct urb *urb)
228 struct urb_context *context = (struct urb_context *)(urb->context);
229 struct mwifiex_adapter *adapter = context->adapter;
230 struct usb_card_rec *card = adapter->card;
232 dev_dbg(adapter->dev, "%s: status: %d\n", __func__, urb->status);
234 if (context->ep == card->tx_cmd_ep) {
235 dev_dbg(adapter->dev, "%s: CMD\n", __func__);
236 atomic_dec(&card->tx_cmd_urb_pending);
237 adapter->cmd_sent = false;
239 dev_dbg(adapter->dev, "%s: DATA\n", __func__);
240 atomic_dec(&card->tx_data_urb_pending);
241 mwifiex_write_data_complete(adapter, context->skb, 0,
242 urb->status ? -1 : 0);
245 queue_work(adapter->workqueue, &adapter->main_work);
250 static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size)
252 struct mwifiex_adapter *adapter = ctx->adapter;
253 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
255 if (card->rx_cmd_ep != ctx->ep) {
256 ctx->skb = dev_alloc_skb(size);
258 dev_err(adapter->dev,
259 "%s: dev_alloc_skb failed\n", __func__);
264 usb_fill_bulk_urb(ctx->urb, card->udev,
265 usb_rcvbulkpipe(card->udev, ctx->ep), ctx->skb->data,
266 size, mwifiex_usb_rx_complete, (void *)ctx);
268 if (card->rx_cmd_ep == ctx->ep)
269 atomic_inc(&card->rx_cmd_urb_pending);
271 atomic_inc(&card->rx_data_urb_pending);
273 if (usb_submit_urb(ctx->urb, GFP_ATOMIC)) {
274 dev_err(adapter->dev, "usb_submit_urb failed\n");
275 dev_kfree_skb_any(ctx->skb);
278 if (card->rx_cmd_ep == ctx->ep)
279 atomic_dec(&card->rx_cmd_urb_pending);
281 atomic_dec(&card->rx_data_urb_pending);
289 static void mwifiex_usb_free(struct usb_card_rec *card)
293 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
294 usb_kill_urb(card->rx_cmd.urb);
296 usb_free_urb(card->rx_cmd.urb);
297 card->rx_cmd.urb = NULL;
299 if (atomic_read(&card->rx_data_urb_pending))
300 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
301 if (card->rx_data_list[i].urb)
302 usb_kill_urb(card->rx_data_list[i].urb);
304 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
305 usb_free_urb(card->rx_data_list[i].urb);
306 card->rx_data_list[i].urb = NULL;
309 for (i = 0; i < MWIFIEX_TX_DATA_URB; i++) {
310 usb_free_urb(card->tx_data_list[i].urb);
311 card->tx_data_list[i].urb = NULL;
314 usb_free_urb(card->tx_cmd.urb);
315 card->tx_cmd.urb = NULL;
320 /* This function probes an mwifiex device and registers it. It allocates
321 * the card structure, initiates the device registration and initialization
322 * procedure by adding a logical interface.
324 static int mwifiex_usb_probe(struct usb_interface *intf,
325 const struct usb_device_id *id)
327 struct usb_device *udev = interface_to_usbdev(intf);
328 struct usb_host_interface *iface_desc = intf->cur_altsetting;
329 struct usb_endpoint_descriptor *epd;
331 struct usb_card_rec *card;
332 u16 id_vendor, id_product, bcd_device, bcd_usb;
334 card = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
338 id_vendor = le16_to_cpu(udev->descriptor.idVendor);
339 id_product = le16_to_cpu(udev->descriptor.idProduct);
340 bcd_device = le16_to_cpu(udev->descriptor.bcdDevice);
341 bcd_usb = le16_to_cpu(udev->descriptor.bcdUSB);
342 pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n",
343 id_vendor, id_product, bcd_device);
345 /* PID_1 is used for firmware downloading only */
346 if (id_product == USB8797_PID_1)
347 card->usb_boot_state = USB8797_FW_DNLD;
349 card->usb_boot_state = USB8797_FW_READY;
354 pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
355 udev->descriptor.bcdUSB, udev->descriptor.bDeviceClass,
356 udev->descriptor.bDeviceSubClass,
357 udev->descriptor.bDeviceProtocol);
359 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
360 epd = &iface_desc->endpoint[i].desc;
361 if (usb_endpoint_dir_in(epd) &&
362 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
363 usb_endpoint_xfer_bulk(epd)) {
364 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
365 le16_to_cpu(epd->wMaxPacketSize),
366 epd->bEndpointAddress);
367 card->rx_cmd_ep = usb_endpoint_num(epd);
368 atomic_set(&card->rx_cmd_urb_pending, 0);
370 if (usb_endpoint_dir_in(epd) &&
371 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
372 usb_endpoint_xfer_bulk(epd)) {
373 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
374 le16_to_cpu(epd->wMaxPacketSize),
375 epd->bEndpointAddress);
376 card->rx_data_ep = usb_endpoint_num(epd);
377 atomic_set(&card->rx_data_urb_pending, 0);
379 if (usb_endpoint_dir_out(epd) &&
380 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
381 usb_endpoint_xfer_bulk(epd)) {
382 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
383 le16_to_cpu(epd->wMaxPacketSize),
384 epd->bEndpointAddress);
385 card->tx_data_ep = usb_endpoint_num(epd);
386 atomic_set(&card->tx_data_urb_pending, 0);
388 if (usb_endpoint_dir_out(epd) &&
389 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
390 usb_endpoint_xfer_bulk(epd)) {
391 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
392 le16_to_cpu(epd->wMaxPacketSize),
393 epd->bEndpointAddress);
394 card->tx_cmd_ep = usb_endpoint_num(epd);
395 atomic_set(&card->tx_cmd_urb_pending, 0);
396 card->bulk_out_maxpktsize =
397 le16_to_cpu(epd->wMaxPacketSize);
401 usb_set_intfdata(intf, card);
403 ret = mwifiex_add_card(card, &add_remove_card_sem, &usb_ops,
406 pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret);
407 usb_reset_device(udev);
417 /* Kernel needs to suspend all functions separately. Therefore all
418 * registered functions must have drivers with suspend and resume
419 * methods. Failing that the kernel simply removes the whole card.
421 * If already not suspended, this function allocates and sends a
422 * 'host sleep activate' request to the firmware and turns off the traffic.
424 static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message)
426 struct usb_card_rec *card = usb_get_intfdata(intf);
427 struct mwifiex_adapter *adapter;
430 if (!card || !card->adapter) {
431 pr_err("%s: card or card->adapter is NULL\n", __func__);
434 adapter = card->adapter;
436 if (unlikely(adapter->is_suspended))
437 dev_warn(adapter->dev, "Device already suspended\n");
439 mwifiex_enable_hs(adapter);
441 /* 'is_suspended' flag indicates device is suspended.
442 * It must be set here before the usb_kill_urb() calls. Reason
443 * is in the complete handlers, urb->status(= -ENOENT) and
444 * this flag is used in combination to distinguish between a
445 * 'suspended' state and a 'disconnect' one.
447 adapter->is_suspended = true;
449 for (i = 0; i < adapter->priv_num; i++)
450 netif_carrier_off(adapter->priv[i]->netdev);
452 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
453 usb_kill_urb(card->rx_cmd.urb);
455 if (atomic_read(&card->rx_data_urb_pending))
456 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
457 if (card->rx_data_list[i].urb)
458 usb_kill_urb(card->rx_data_list[i].urb);
460 for (i = 0; i < MWIFIEX_TX_DATA_URB; i++)
461 if (card->tx_data_list[i].urb)
462 usb_kill_urb(card->tx_data_list[i].urb);
464 if (card->tx_cmd.urb)
465 usb_kill_urb(card->tx_cmd.urb);
470 /* Kernel needs to suspend all functions separately. Therefore all
471 * registered functions must have drivers with suspend and resume
472 * methods. Failing that the kernel simply removes the whole card.
474 * If already not resumed, this function turns on the traffic and
475 * sends a 'host sleep cancel' request to the firmware.
477 static int mwifiex_usb_resume(struct usb_interface *intf)
479 struct usb_card_rec *card = usb_get_intfdata(intf);
480 struct mwifiex_adapter *adapter;
483 if (!card || !card->adapter) {
484 pr_err("%s: card or card->adapter is NULL\n", __func__);
487 adapter = card->adapter;
489 if (unlikely(!adapter->is_suspended)) {
490 dev_warn(adapter->dev, "Device already resumed\n");
494 /* Indicate device resumed. The netdev queue will be resumed only
495 * after the urbs have been re-submitted
497 adapter->is_suspended = false;
499 if (!atomic_read(&card->rx_data_urb_pending))
500 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
501 mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
502 MWIFIEX_RX_DATA_BUF_SIZE);
504 if (!atomic_read(&card->rx_cmd_urb_pending)) {
505 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
506 if (card->rx_cmd.skb)
507 mwifiex_usb_submit_rx_urb(&card->rx_cmd,
508 MWIFIEX_RX_CMD_BUF_SIZE);
511 for (i = 0; i < adapter->priv_num; i++)
512 if (adapter->priv[i]->media_connected)
513 netif_carrier_on(adapter->priv[i]->netdev);
515 /* Disable Host Sleep */
516 if (adapter->hs_activated)
517 mwifiex_cancel_hs(mwifiex_get_priv(adapter,
518 MWIFIEX_BSS_ROLE_ANY),
522 /* Resume handler may be called due to remote wakeup,
523 * force to exit suspend anyway
525 usb_disable_autosuspend(card->udev);
526 #endif /* CONFIG_PM */
531 static void mwifiex_usb_disconnect(struct usb_interface *intf)
533 struct usb_card_rec *card = usb_get_intfdata(intf);
534 struct mwifiex_adapter *adapter;
537 if (!card || !card->adapter) {
538 pr_err("%s: card or card->adapter is NULL\n", __func__);
542 adapter = card->adapter;
543 if (!adapter->priv_num)
546 /* In case driver is removed when asynchronous FW downloading is
549 wait_for_completion(&adapter->fw_load);
553 if (adapter->is_suspended)
554 mwifiex_usb_resume(intf);
556 for (i = 0; i < adapter->priv_num; i++)
557 if ((GET_BSS_ROLE(adapter->priv[i]) ==
558 MWIFIEX_BSS_ROLE_STA) &&
559 adapter->priv[i]->media_connected)
560 mwifiex_deauthenticate(adapter->priv[i], NULL);
562 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
563 MWIFIEX_BSS_ROLE_ANY),
564 MWIFIEX_FUNC_SHUTDOWN);
567 mwifiex_usb_free(card);
569 dev_dbg(adapter->dev, "%s: removing card\n", __func__);
570 mwifiex_remove_card(adapter, &add_remove_card_sem);
572 usb_set_intfdata(intf, NULL);
573 usb_put_dev(interface_to_usbdev(intf));
579 static struct usb_driver mwifiex_usb_driver = {
580 .name = usbdriver_name,
581 .probe = mwifiex_usb_probe,
582 .disconnect = mwifiex_usb_disconnect,
583 .id_table = mwifiex_usb_table,
584 .suspend = mwifiex_usb_suspend,
585 .resume = mwifiex_usb_resume,
586 .supports_autosuspend = 1,
589 static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter)
591 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
594 card->tx_cmd.adapter = adapter;
595 card->tx_cmd.ep = card->tx_cmd_ep;
597 card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
598 if (!card->tx_cmd.urb) {
599 dev_err(adapter->dev, "tx_cmd.urb allocation failed\n");
603 card->tx_data_ix = 0;
605 for (i = 0; i < MWIFIEX_TX_DATA_URB; i++) {
606 card->tx_data_list[i].adapter = adapter;
607 card->tx_data_list[i].ep = card->tx_data_ep;
609 card->tx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
610 if (!card->tx_data_list[i].urb) {
611 dev_err(adapter->dev,
612 "tx_data_list[] urb allocation failed\n");
620 static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
622 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
625 card->rx_cmd.adapter = adapter;
626 card->rx_cmd.ep = card->rx_cmd_ep;
628 card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
629 if (!card->rx_cmd.urb) {
630 dev_err(adapter->dev, "rx_cmd.urb allocation failed\n");
634 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
635 if (!card->rx_cmd.skb) {
636 dev_err(adapter->dev, "rx_cmd.skb allocation failed\n");
640 if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
643 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
644 card->rx_data_list[i].adapter = adapter;
645 card->rx_data_list[i].ep = card->rx_data_ep;
647 card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
648 if (!card->rx_data_list[i].urb) {
649 dev_err(adapter->dev,
650 "rx_data_list[] urb allocation failed\n");
653 if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
654 MWIFIEX_RX_DATA_BUF_SIZE))
661 static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
662 u32 *len, u8 ep, u32 timeout)
664 struct usb_card_rec *card = adapter->card;
665 int actual_length, ret;
667 if (!(*len % card->bulk_out_maxpktsize))
670 /* Send the data block */
671 ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
672 *len, &actual_length, timeout);
674 dev_err(adapter->dev, "usb_bulk_msg for tx failed: %d\n", ret);
678 *len = actual_length;
683 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
684 u32 *len, u8 ep, u32 timeout)
686 struct usb_card_rec *card = adapter->card;
687 int actual_length, ret;
689 /* Receive the data response */
690 ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
691 *len, &actual_length, timeout);
693 dev_err(adapter->dev, "usb_bulk_msg for rx failed: %d\n", ret);
697 *len = actual_length;
702 /* This function write a command/data packet to card. */
703 static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
705 struct mwifiex_tx_param *tx_param)
707 struct usb_card_rec *card = adapter->card;
708 struct urb_context *context;
709 u8 *data = (u8 *)skb->data;
712 if (adapter->is_suspended) {
713 dev_err(adapter->dev,
714 "%s: not allowed while suspended\n", __func__);
718 if (adapter->surprise_removed) {
719 dev_err(adapter->dev, "%s: device removed\n", __func__);
723 if (ep == card->tx_data_ep &&
724 atomic_read(&card->tx_data_urb_pending) >= MWIFIEX_TX_DATA_URB) {
728 dev_dbg(adapter->dev, "%s: ep=%d\n", __func__, ep);
730 if (ep == card->tx_cmd_ep) {
731 context = &card->tx_cmd;
733 if (card->tx_data_ix >= MWIFIEX_TX_DATA_URB)
734 card->tx_data_ix = 0;
735 context = &card->tx_data_list[card->tx_data_ix++];
738 context->adapter = adapter;
741 tx_urb = context->urb;
743 usb_fill_bulk_urb(tx_urb, card->udev, usb_sndbulkpipe(card->udev, ep),
744 data, skb->len, mwifiex_usb_tx_complete,
747 tx_urb->transfer_flags |= URB_ZERO_PACKET;
749 if (ep == card->tx_cmd_ep)
750 atomic_inc(&card->tx_cmd_urb_pending);
752 atomic_inc(&card->tx_data_urb_pending);
754 if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
755 dev_err(adapter->dev, "%s: usb_submit_urb failed\n", __func__);
756 if (ep == card->tx_cmd_ep) {
757 atomic_dec(&card->tx_cmd_urb_pending);
759 atomic_dec(&card->tx_data_urb_pending);
760 if (card->tx_data_ix)
763 card->tx_data_ix = MWIFIEX_TX_DATA_URB;
768 if (ep == card->tx_data_ep &&
769 atomic_read(&card->tx_data_urb_pending) ==
777 /* This function register usb device and initialize parameter. */
778 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
780 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
782 card->adapter = adapter;
783 adapter->dev = &card->udev->dev;
784 strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
789 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
791 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
793 usb_set_intfdata(card->intf, NULL);
796 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
797 struct mwifiex_fw_image *fw)
800 u8 *firmware = fw->fw_buf, *recv_buff;
801 u32 retries = USB8797_FW_MAX_RETRY, dlen;
802 u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
803 struct fw_data *fwdata;
804 struct fw_sync_header sync_fw;
808 dev_err(adapter->dev,
809 "No firmware image found! Terminating download\n");
814 /* Allocate memory for transmit */
815 fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
819 /* Allocate memory for receive */
820 recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
825 /* Send pseudo data to check winner status first */
827 memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
830 /* copy the header of the fw_data to get the length */
831 memcpy(&fwdata->fw_hdr, &firmware[tlen],
832 sizeof(struct fw_header));
834 dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
835 dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
836 tlen += sizeof(struct fw_header);
838 memcpy(fwdata->data, &firmware[tlen], dlen);
840 fwdata->seq_num = cpu_to_le32(fw_seqnum);
844 /* If the send/receive fails or CRC occurs then retry */
846 u8 *buf = (u8 *)fwdata;
847 u32 len = FW_DATA_XMIT_SIZE;
849 /* send the firmware block */
850 ret = mwifiex_write_data_sync(adapter, buf, &len,
851 MWIFIEX_USB_EP_CMD_EVENT,
852 MWIFIEX_USB_TIMEOUT);
854 dev_err(adapter->dev,
855 "write_data_sync: failed: %d\n", ret);
860 len = FW_DNLD_RX_BUF_SIZE;
862 /* Receive the firmware block response */
863 ret = mwifiex_read_data_sync(adapter, buf, &len,
864 MWIFIEX_USB_EP_CMD_EVENT,
865 MWIFIEX_USB_TIMEOUT);
867 dev_err(adapter->dev,
868 "read_data_sync: failed: %d\n", ret);
872 memcpy(&sync_fw, recv_buff,
873 sizeof(struct fw_sync_header));
875 /* check 1st firmware block resp for highest bit set */
877 if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
878 dev_warn(adapter->dev,
879 "USB is not the winner %#x\n",
882 /* returning success */
887 dev_dbg(adapter->dev,
888 "USB is the winner, start to download FW\n");
894 /* check the firmware block response for CRC errors */
896 dev_err(adapter->dev,
897 "FW received block with CRC %#x\n",
903 retries = USB8797_FW_MAX_RETRY;
907 } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
910 dev_dbg(adapter->dev, "%s: %d bytes downloaded\n", __func__, tlen);
921 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
922 struct mwifiex_fw_image *fw)
925 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
927 if (card->usb_boot_state == USB8797_FW_DNLD) {
928 ret = mwifiex_prog_fw_w_helper(adapter, fw);
932 /* Boot state changes after successful firmware download */
933 if (card->usb_boot_state == USB8797_FW_DNLD)
937 ret = mwifiex_usb_rx_init(adapter);
939 ret = mwifiex_usb_tx_init(adapter);
944 static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
946 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
948 skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
949 if ((ep == card->rx_cmd_ep) &&
950 (!atomic_read(&card->rx_cmd_urb_pending)))
951 mwifiex_usb_submit_rx_urb(&card->rx_cmd,
952 MWIFIEX_RX_CMD_BUF_SIZE);
957 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
960 atomic_dec(&adapter->rx_pending);
961 mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
966 static int mwifiex_usb_data_complete(struct mwifiex_adapter *adapter,
969 atomic_dec(&adapter->rx_pending);
970 dev_kfree_skb_any(skb);
975 /* This function wakes up the card. */
976 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
978 /* Simulation of HS_AWAKE event */
979 adapter->pm_wakeup_fw_try = false;
980 adapter->pm_wakeup_card_req = false;
981 adapter->ps_state = PS_STATE_AWAKE;
986 static struct mwifiex_if_ops usb_ops = {
987 .register_dev = mwifiex_register_dev,
988 .unregister_dev = mwifiex_unregister_dev,
989 .wakeup = mwifiex_pm_wakeup_card,
990 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
993 .dnld_fw = mwifiex_usb_dnld_fw,
994 .cmdrsp_complete = mwifiex_usb_cmd_event_complete,
995 .event_complete = mwifiex_usb_cmd_event_complete,
996 .data_complete = mwifiex_usb_data_complete,
997 .host_to_card = mwifiex_usb_host_to_card,
1000 /* This function initializes the USB driver module.
1002 * This initiates the semaphore and registers the device with
1005 static int mwifiex_usb_init_module(void)
1009 pr_debug("Marvell USB8797 Driver\n");
1011 sema_init(&add_remove_card_sem, 1);
1013 ret = usb_register(&mwifiex_usb_driver);
1015 pr_err("Driver register failed!\n");
1017 pr_debug("info: Driver registered successfully!\n");
1022 /* This function cleans up the USB driver.
1024 * The following major steps are followed in .disconnect for cleanup:
1025 * - Resume the device if its suspended
1026 * - Disconnect the device if connected
1027 * - Shutdown the firmware
1028 * - Unregister the device from USB bus.
1030 static void mwifiex_usb_cleanup_module(void)
1032 if (!down_interruptible(&add_remove_card_sem))
1033 up(&add_remove_card_sem);
1035 /* set the flag as user is removing this module */
1038 usb_deregister(&mwifiex_usb_driver);
1041 module_init(mwifiex_usb_init_module);
1042 module_exit(mwifiex_usb_cleanup_module);
1044 MODULE_AUTHOR("Marvell International Ltd.");
1045 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1046 MODULE_VERSION(USB_VERSION);
1047 MODULE_LICENSE("GPL v2");
1048 MODULE_FIRMWARE("mrvl/usb8797_uapsta.bin");