Linux 3.11-rc3
[cris-mirror.git] / drivers / net / wireless / mwifiex / usb.c
blobf90fe21e5bfda65d5bc2a0d6fc6638ac8765e72e
1 /*
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.
20 #include "main.h"
21 #include "usb.h"
23 #define USB_VERSION "1.0"
25 static const char usbdriver_name[] = "usb8797";
27 static u8 user_rmmod;
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
44 * cmd/event/data.
46 static int mwifiex_usb_recv(struct mwifiex_adapter *adapter,
47 struct sk_buff *skb, u8 ep)
49 struct device *dev = adapter->dev;
50 u32 recv_type;
51 __le32 tmp;
52 int ret;
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__);
59 return -1;
62 switch (ep) {
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);
69 switch (recv_type) {
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");
73 ret = -1;
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(
79 adapter, skb->data,
80 skb->len);
81 ret = 0;
82 goto exit_restore_skb;
84 ret = -1;
85 goto exit_restore_skb;
88 adapter->curr_cmd->resp_skb = skb;
89 adapter->cmd_resp_received = true;
90 break;
91 case MWIFIEX_USB_TYPE_EVENT:
92 if (skb->len < sizeof(u32)) {
93 dev_err(dev, "EVENT: skb->len too small\n");
94 ret = -1;
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");
103 ret = -1;
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;
112 break;
113 default:
114 dev_err(dev, "unknown recv_type %#x\n", recv_type);
115 return -1;
117 break;
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");
122 return -1;
124 skb_queue_tail(&adapter->usb_rx_data_q, skb);
125 adapter->data_received = true;
126 break;
127 default:
128 dev_err(dev, "%s: unknown endport %#x\n", __func__, ep);
129 return -1;
132 return -EINPROGRESS;
134 exit_restore_skb:
135 /* The buffer will be reused for further cmds/events */
136 skb_push(skb, INTF_HEADER_LEN);
138 return ret;
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;
148 int size, status;
150 if (!adapter || !adapter->card) {
151 pr_err("mwifiex adapter or card structure is not valid\n");
152 return;
155 card = (struct usb_card_rec *)adapter->card;
156 if (card->rx_cmd_ep == context->ep)
157 atomic_dec(&card->rx_cmd_urb_pending);
158 else
159 atomic_dec(&card->rx_data_urb_pending);
161 if (recv_length) {
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);
168 goto setup_for_next;
170 if (skb->len > recv_length)
171 skb_trim(skb, recv_length);
172 else
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)
188 return;
189 } else {
190 atomic_dec(&adapter->rx_pending);
191 if (status == -1)
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);
206 return;
207 } else {
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 */
215 setup_for_next:
216 if (card->rx_cmd_ep == context->ep)
217 size = MWIFIEX_RX_CMD_BUF_SIZE;
218 else
219 size = MWIFIEX_RX_DATA_BUF_SIZE;
221 mwifiex_usb_submit_rx_urb(context, size);
223 return;
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;
238 } else {
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);
247 return;
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);
257 if (!ctx->skb) {
258 dev_err(adapter->dev,
259 "%s: dev_alloc_skb failed\n", __func__);
260 return -ENOMEM;
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);
270 else
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);
276 ctx->skb = NULL;
278 if (card->rx_cmd_ep == ctx->ep)
279 atomic_dec(&card->rx_cmd_urb_pending);
280 else
281 atomic_dec(&card->rx_data_urb_pending);
283 return -1;
286 return 0;
289 static void mwifiex_usb_free(struct usb_card_rec *card)
291 int i;
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;
317 return;
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;
330 int ret, i;
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);
335 if (!card)
336 return -ENOMEM;
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;
348 else
349 card->usb_boot_state = USB8797_FW_READY;
351 card->udev = udev;
352 card->intf = intf;
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,
404 MWIFIEX_USB);
405 if (ret) {
406 pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret);
407 usb_reset_device(udev);
408 kfree(card);
409 return ret;
412 usb_get_dev(udev);
414 return 0;
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;
428 int i;
430 if (!card || !card->adapter) {
431 pr_err("%s: card or card->adapter is NULL\n", __func__);
432 return 0;
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);
467 return 0;
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;
481 int i;
483 if (!card || !card->adapter) {
484 pr_err("%s: card or card->adapter is NULL\n", __func__);
485 return 0;
487 adapter = card->adapter;
489 if (unlikely(!adapter->is_suspended)) {
490 dev_warn(adapter->dev, "Device already resumed\n");
491 return 0;
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),
519 MWIFIEX_ASYNC_CMD);
521 #ifdef CONFIG_PM
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 */
528 return 0;
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;
535 int i;
537 if (!card || !card->adapter) {
538 pr_err("%s: card or card->adapter is NULL\n", __func__);
539 return;
542 adapter = card->adapter;
543 if (!adapter->priv_num)
544 return;
546 /* In case driver is removed when asynchronous FW downloading is
547 * in progress
549 wait_for_completion(&adapter->fw_load);
551 if (user_rmmod) {
552 #ifdef CONFIG_PM
553 if (adapter->is_suspended)
554 mwifiex_usb_resume(intf);
555 #endif
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));
574 kfree(card);
576 return;
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;
592 int i;
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");
600 return -ENOMEM;
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");
613 return -ENOMEM;
617 return 0;
620 static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
622 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
623 int i;
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");
631 return -ENOMEM;
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");
637 return -ENOMEM;
640 if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
641 return -1;
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");
651 return -1;
653 if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
654 MWIFIEX_RX_DATA_BUF_SIZE))
655 return -1;
658 return 0;
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))
668 (*len)++;
670 /* Send the data block */
671 ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
672 *len, &actual_length, timeout);
673 if (ret) {
674 dev_err(adapter->dev, "usb_bulk_msg for tx failed: %d\n", ret);
675 return ret;
678 *len = actual_length;
680 return ret;
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);
692 if (ret) {
693 dev_err(adapter->dev, "usb_bulk_msg for rx failed: %d\n", ret);
694 return ret;
697 *len = actual_length;
699 return ret;
702 /* This function write a command/data packet to card. */
703 static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
704 struct sk_buff *skb,
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;
710 struct urb *tx_urb;
712 if (adapter->is_suspended) {
713 dev_err(adapter->dev,
714 "%s: not allowed while suspended\n", __func__);
715 return -1;
718 if (adapter->surprise_removed) {
719 dev_err(adapter->dev, "%s: device removed\n", __func__);
720 return -1;
723 if (ep == card->tx_data_ep &&
724 atomic_read(&card->tx_data_urb_pending) >= MWIFIEX_TX_DATA_URB) {
725 return -EBUSY;
728 dev_dbg(adapter->dev, "%s: ep=%d\n", __func__, ep);
730 if (ep == card->tx_cmd_ep) {
731 context = &card->tx_cmd;
732 } else {
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;
739 context->ep = ep;
740 context->skb = skb;
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,
745 (void *)context);
747 tx_urb->transfer_flags |= URB_ZERO_PACKET;
749 if (ep == card->tx_cmd_ep)
750 atomic_inc(&card->tx_cmd_urb_pending);
751 else
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);
758 } else {
759 atomic_dec(&card->tx_data_urb_pending);
760 if (card->tx_data_ix)
761 card->tx_data_ix--;
762 else
763 card->tx_data_ix = MWIFIEX_TX_DATA_URB;
766 return -1;
767 } else {
768 if (ep == card->tx_data_ep &&
769 atomic_read(&card->tx_data_urb_pending) ==
770 MWIFIEX_TX_DATA_URB)
771 return -ENOSR;
774 return -EINPROGRESS;
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);
786 return 0;
789 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
790 struct mwifiex_fw_image *fw)
792 int ret = 0;
793 u8 *firmware = fw->fw_buf, *recv_buff;
794 u32 retries = USB8797_FW_MAX_RETRY, dlen;
795 u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
796 struct fw_data *fwdata;
797 struct fw_sync_header sync_fw;
798 u8 check_winner = 1;
800 if (!firmware) {
801 dev_err(adapter->dev,
802 "No firmware image found! Terminating download\n");
803 ret = -1;
804 goto fw_exit;
807 /* Allocate memory for transmit */
808 fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
809 if (!fwdata)
810 goto fw_exit;
812 /* Allocate memory for receive */
813 recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
814 if (!recv_buff)
815 goto cleanup;
817 do {
818 /* Send pseudo data to check winner status first */
819 if (check_winner) {
820 memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
821 dlen = 0;
822 } else {
823 /* copy the header of the fw_data to get the length */
824 memcpy(&fwdata->fw_hdr, &firmware[tlen],
825 sizeof(struct fw_header));
827 dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
828 dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
829 tlen += sizeof(struct fw_header);
831 memcpy(fwdata->data, &firmware[tlen], dlen);
833 fwdata->seq_num = cpu_to_le32(fw_seqnum);
834 tlen += dlen;
837 /* If the send/receive fails or CRC occurs then retry */
838 while (retries--) {
839 u8 *buf = (u8 *)fwdata;
840 u32 len = FW_DATA_XMIT_SIZE;
842 /* send the firmware block */
843 ret = mwifiex_write_data_sync(adapter, buf, &len,
844 MWIFIEX_USB_EP_CMD_EVENT,
845 MWIFIEX_USB_TIMEOUT);
846 if (ret) {
847 dev_err(adapter->dev,
848 "write_data_sync: failed: %d\n", ret);
849 continue;
852 buf = recv_buff;
853 len = FW_DNLD_RX_BUF_SIZE;
855 /* Receive the firmware block response */
856 ret = mwifiex_read_data_sync(adapter, buf, &len,
857 MWIFIEX_USB_EP_CMD_EVENT,
858 MWIFIEX_USB_TIMEOUT);
859 if (ret) {
860 dev_err(adapter->dev,
861 "read_data_sync: failed: %d\n", ret);
862 continue;
865 memcpy(&sync_fw, recv_buff,
866 sizeof(struct fw_sync_header));
868 /* check 1st firmware block resp for highest bit set */
869 if (check_winner) {
870 if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
871 dev_warn(adapter->dev,
872 "USB is not the winner %#x\n",
873 sync_fw.cmd);
875 /* returning success */
876 ret = 0;
877 goto cleanup;
880 dev_dbg(adapter->dev,
881 "USB is the winner, start to download FW\n");
883 check_winner = 0;
884 break;
887 /* check the firmware block response for CRC errors */
888 if (sync_fw.cmd) {
889 dev_err(adapter->dev,
890 "FW received block with CRC %#x\n",
891 sync_fw.cmd);
892 ret = -1;
893 continue;
896 retries = USB8797_FW_MAX_RETRY;
897 break;
899 fw_seqnum++;
900 } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
902 cleanup:
903 dev_dbg(adapter->dev, "%s: %d bytes downloaded\n", __func__, tlen);
905 kfree(recv_buff);
906 kfree(fwdata);
908 if (retries)
909 ret = 0;
910 fw_exit:
911 return ret;
914 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
915 struct mwifiex_fw_image *fw)
917 int ret;
918 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
920 if (card->usb_boot_state == USB8797_FW_DNLD) {
921 ret = mwifiex_prog_fw_w_helper(adapter, fw);
922 if (ret)
923 return -1;
925 /* Boot state changes after successful firmware download */
926 if (card->usb_boot_state == USB8797_FW_DNLD)
927 return -1;
930 ret = mwifiex_usb_rx_init(adapter);
931 if (!ret)
932 ret = mwifiex_usb_tx_init(adapter);
934 return ret;
937 static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
939 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
941 skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
942 if ((ep == card->rx_cmd_ep) &&
943 (!atomic_read(&card->rx_cmd_urb_pending)))
944 mwifiex_usb_submit_rx_urb(&card->rx_cmd,
945 MWIFIEX_RX_CMD_BUF_SIZE);
947 return;
950 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
951 struct sk_buff *skb)
953 atomic_dec(&adapter->rx_pending);
954 mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
956 return 0;
959 static int mwifiex_usb_data_complete(struct mwifiex_adapter *adapter,
960 struct sk_buff *skb)
962 atomic_dec(&adapter->rx_pending);
963 dev_kfree_skb_any(skb);
965 return 0;
968 /* This function wakes up the card. */
969 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
971 /* Simulation of HS_AWAKE event */
972 adapter->pm_wakeup_fw_try = false;
973 adapter->pm_wakeup_card_req = false;
974 adapter->ps_state = PS_STATE_AWAKE;
976 return 0;
979 static struct mwifiex_if_ops usb_ops = {
980 .register_dev = mwifiex_register_dev,
981 .wakeup = mwifiex_pm_wakeup_card,
982 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
984 /* USB specific */
985 .dnld_fw = mwifiex_usb_dnld_fw,
986 .cmdrsp_complete = mwifiex_usb_cmd_event_complete,
987 .event_complete = mwifiex_usb_cmd_event_complete,
988 .data_complete = mwifiex_usb_data_complete,
989 .host_to_card = mwifiex_usb_host_to_card,
992 /* This function initializes the USB driver module.
994 * This initiates the semaphore and registers the device with
995 * USB bus.
997 static int mwifiex_usb_init_module(void)
999 int ret;
1001 pr_debug("Marvell USB8797 Driver\n");
1003 sema_init(&add_remove_card_sem, 1);
1005 ret = usb_register(&mwifiex_usb_driver);
1006 if (ret)
1007 pr_err("Driver register failed!\n");
1008 else
1009 pr_debug("info: Driver registered successfully!\n");
1011 return ret;
1014 /* This function cleans up the USB driver.
1016 * The following major steps are followed in .disconnect for cleanup:
1017 * - Resume the device if its suspended
1018 * - Disconnect the device if connected
1019 * - Shutdown the firmware
1020 * - Unregister the device from USB bus.
1022 static void mwifiex_usb_cleanup_module(void)
1024 if (!down_interruptible(&add_remove_card_sem))
1025 up(&add_remove_card_sem);
1027 /* set the flag as user is removing this module */
1028 user_rmmod = 1;
1030 usb_deregister(&mwifiex_usb_driver);
1033 module_init(mwifiex_usb_init_module);
1034 module_exit(mwifiex_usb_cleanup_module);
1036 MODULE_AUTHOR("Marvell International Ltd.");
1037 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1038 MODULE_VERSION(USB_VERSION);
1039 MODULE_LICENSE("GPL v2");
1040 MODULE_FIRMWARE("mrvl/usb8797_uapsta.bin");