x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / net / wireless / mwifiex / usb.c
blob9d0b0c442c95882327b4f7541612db117eb39530
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 struct mwifiex_if_ops usb_ops;
28 static struct semaphore add_remove_card_sem;
29 static struct usb_card_rec *usb_card;
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;
353 usb_card = card;
355 pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
356 udev->descriptor.bcdUSB, udev->descriptor.bDeviceClass,
357 udev->descriptor.bDeviceSubClass,
358 udev->descriptor.bDeviceProtocol);
360 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
361 epd = &iface_desc->endpoint[i].desc;
362 if (usb_endpoint_dir_in(epd) &&
363 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
364 usb_endpoint_xfer_bulk(epd)) {
365 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
366 le16_to_cpu(epd->wMaxPacketSize),
367 epd->bEndpointAddress);
368 card->rx_cmd_ep = usb_endpoint_num(epd);
369 atomic_set(&card->rx_cmd_urb_pending, 0);
371 if (usb_endpoint_dir_in(epd) &&
372 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
373 usb_endpoint_xfer_bulk(epd)) {
374 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
375 le16_to_cpu(epd->wMaxPacketSize),
376 epd->bEndpointAddress);
377 card->rx_data_ep = usb_endpoint_num(epd);
378 atomic_set(&card->rx_data_urb_pending, 0);
380 if (usb_endpoint_dir_out(epd) &&
381 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
382 usb_endpoint_xfer_bulk(epd)) {
383 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
384 le16_to_cpu(epd->wMaxPacketSize),
385 epd->bEndpointAddress);
386 card->tx_data_ep = usb_endpoint_num(epd);
387 atomic_set(&card->tx_data_urb_pending, 0);
389 if (usb_endpoint_dir_out(epd) &&
390 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
391 usb_endpoint_xfer_bulk(epd)) {
392 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
393 le16_to_cpu(epd->wMaxPacketSize),
394 epd->bEndpointAddress);
395 card->tx_cmd_ep = usb_endpoint_num(epd);
396 atomic_set(&card->tx_cmd_urb_pending, 0);
397 card->bulk_out_maxpktsize =
398 le16_to_cpu(epd->wMaxPacketSize);
402 usb_set_intfdata(intf, card);
404 ret = mwifiex_add_card(card, &add_remove_card_sem, &usb_ops,
405 MWIFIEX_USB);
406 if (ret) {
407 pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret);
408 usb_reset_device(udev);
409 kfree(card);
410 return ret;
413 usb_get_dev(udev);
415 return 0;
418 /* Kernel needs to suspend all functions separately. Therefore all
419 * registered functions must have drivers with suspend and resume
420 * methods. Failing that the kernel simply removes the whole card.
422 * If already not suspended, this function allocates and sends a
423 * 'host sleep activate' request to the firmware and turns off the traffic.
425 static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message)
427 struct usb_card_rec *card = usb_get_intfdata(intf);
428 struct mwifiex_adapter *adapter;
429 int i;
431 if (!card || !card->adapter) {
432 pr_err("%s: card or card->adapter is NULL\n", __func__);
433 return 0;
435 adapter = card->adapter;
437 if (unlikely(adapter->is_suspended))
438 dev_warn(adapter->dev, "Device already suspended\n");
440 mwifiex_enable_hs(adapter);
442 /* 'is_suspended' flag indicates device is suspended.
443 * It must be set here before the usb_kill_urb() calls. Reason
444 * is in the complete handlers, urb->status(= -ENOENT) and
445 * this flag is used in combination to distinguish between a
446 * 'suspended' state and a 'disconnect' one.
448 adapter->is_suspended = true;
450 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
451 usb_kill_urb(card->rx_cmd.urb);
453 if (atomic_read(&card->rx_data_urb_pending))
454 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
455 if (card->rx_data_list[i].urb)
456 usb_kill_urb(card->rx_data_list[i].urb);
458 for (i = 0; i < MWIFIEX_TX_DATA_URB; i++)
459 if (card->tx_data_list[i].urb)
460 usb_kill_urb(card->tx_data_list[i].urb);
462 if (card->tx_cmd.urb)
463 usb_kill_urb(card->tx_cmd.urb);
465 return 0;
468 /* Kernel needs to suspend all functions separately. Therefore all
469 * registered functions must have drivers with suspend and resume
470 * methods. Failing that the kernel simply removes the whole card.
472 * If already not resumed, this function turns on the traffic and
473 * sends a 'host sleep cancel' request to the firmware.
475 static int mwifiex_usb_resume(struct usb_interface *intf)
477 struct usb_card_rec *card = usb_get_intfdata(intf);
478 struct mwifiex_adapter *adapter;
479 int i;
481 if (!card || !card->adapter) {
482 pr_err("%s: card or card->adapter is NULL\n", __func__);
483 return 0;
485 adapter = card->adapter;
487 if (unlikely(!adapter->is_suspended)) {
488 dev_warn(adapter->dev, "Device already resumed\n");
489 return 0;
492 /* Indicate device resumed. The netdev queue will be resumed only
493 * after the urbs have been re-submitted
495 adapter->is_suspended = false;
497 if (!atomic_read(&card->rx_data_urb_pending))
498 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
499 mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
500 MWIFIEX_RX_DATA_BUF_SIZE);
502 if (!atomic_read(&card->rx_cmd_urb_pending)) {
503 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
504 if (card->rx_cmd.skb)
505 mwifiex_usb_submit_rx_urb(&card->rx_cmd,
506 MWIFIEX_RX_CMD_BUF_SIZE);
509 /* Disable Host Sleep */
510 if (adapter->hs_activated)
511 mwifiex_cancel_hs(mwifiex_get_priv(adapter,
512 MWIFIEX_BSS_ROLE_ANY),
513 MWIFIEX_ASYNC_CMD);
515 return 0;
518 static void mwifiex_usb_disconnect(struct usb_interface *intf)
520 struct usb_card_rec *card = usb_get_intfdata(intf);
521 struct mwifiex_adapter *adapter;
523 if (!card || !card->adapter) {
524 pr_err("%s: card or card->adapter is NULL\n", __func__);
525 return;
528 adapter = card->adapter;
529 if (!adapter->priv_num)
530 return;
532 mwifiex_usb_free(card);
534 dev_dbg(adapter->dev, "%s: removing card\n", __func__);
535 mwifiex_remove_card(adapter, &add_remove_card_sem);
537 usb_set_intfdata(intf, NULL);
538 usb_put_dev(interface_to_usbdev(intf));
539 kfree(card);
541 return;
544 static struct usb_driver mwifiex_usb_driver = {
545 .name = usbdriver_name,
546 .probe = mwifiex_usb_probe,
547 .disconnect = mwifiex_usb_disconnect,
548 .id_table = mwifiex_usb_table,
549 .suspend = mwifiex_usb_suspend,
550 .resume = mwifiex_usb_resume,
553 static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter)
555 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
556 int i;
558 card->tx_cmd.adapter = adapter;
559 card->tx_cmd.ep = card->tx_cmd_ep;
561 card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
562 if (!card->tx_cmd.urb) {
563 dev_err(adapter->dev, "tx_cmd.urb allocation failed\n");
564 return -ENOMEM;
567 card->tx_data_ix = 0;
569 for (i = 0; i < MWIFIEX_TX_DATA_URB; i++) {
570 card->tx_data_list[i].adapter = adapter;
571 card->tx_data_list[i].ep = card->tx_data_ep;
573 card->tx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
574 if (!card->tx_data_list[i].urb) {
575 dev_err(adapter->dev,
576 "tx_data_list[] urb allocation failed\n");
577 return -ENOMEM;
581 return 0;
584 static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
586 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
587 int i;
589 card->rx_cmd.adapter = adapter;
590 card->rx_cmd.ep = card->rx_cmd_ep;
592 card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
593 if (!card->rx_cmd.urb) {
594 dev_err(adapter->dev, "rx_cmd.urb allocation failed\n");
595 return -ENOMEM;
598 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
599 if (!card->rx_cmd.skb) {
600 dev_err(adapter->dev, "rx_cmd.skb allocation failed\n");
601 return -ENOMEM;
604 if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
605 return -1;
607 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
608 card->rx_data_list[i].adapter = adapter;
609 card->rx_data_list[i].ep = card->rx_data_ep;
611 card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
612 if (!card->rx_data_list[i].urb) {
613 dev_err(adapter->dev,
614 "rx_data_list[] urb allocation failed\n");
615 return -1;
617 if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
618 MWIFIEX_RX_DATA_BUF_SIZE))
619 return -1;
622 return 0;
625 static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
626 u32 *len, u8 ep, u32 timeout)
628 struct usb_card_rec *card = adapter->card;
629 int actual_length, ret;
631 if (!(*len % card->bulk_out_maxpktsize))
632 (*len)++;
634 /* Send the data block */
635 ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
636 *len, &actual_length, timeout);
637 if (ret) {
638 dev_err(adapter->dev, "usb_bulk_msg for tx failed: %d\n", ret);
639 return ret;
642 *len = actual_length;
644 return ret;
647 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
648 u32 *len, u8 ep, u32 timeout)
650 struct usb_card_rec *card = adapter->card;
651 int actual_length, ret;
653 /* Receive the data response */
654 ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
655 *len, &actual_length, timeout);
656 if (ret) {
657 dev_err(adapter->dev, "usb_bulk_msg for rx failed: %d\n", ret);
658 return ret;
661 *len = actual_length;
663 return ret;
666 /* This function write a command/data packet to card. */
667 static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
668 struct sk_buff *skb,
669 struct mwifiex_tx_param *tx_param)
671 struct usb_card_rec *card = adapter->card;
672 struct urb_context *context;
673 u8 *data = (u8 *)skb->data;
674 struct urb *tx_urb;
676 if (adapter->is_suspended) {
677 dev_err(adapter->dev,
678 "%s: not allowed while suspended\n", __func__);
679 return -1;
682 if (adapter->surprise_removed) {
683 dev_err(adapter->dev, "%s: device removed\n", __func__);
684 return -1;
687 if (ep == card->tx_data_ep &&
688 atomic_read(&card->tx_data_urb_pending) >= MWIFIEX_TX_DATA_URB) {
689 return -EBUSY;
692 dev_dbg(adapter->dev, "%s: ep=%d\n", __func__, ep);
694 if (ep == card->tx_cmd_ep) {
695 context = &card->tx_cmd;
696 } else {
697 if (card->tx_data_ix >= MWIFIEX_TX_DATA_URB)
698 card->tx_data_ix = 0;
699 context = &card->tx_data_list[card->tx_data_ix++];
702 context->adapter = adapter;
703 context->ep = ep;
704 context->skb = skb;
705 tx_urb = context->urb;
707 usb_fill_bulk_urb(tx_urb, card->udev, usb_sndbulkpipe(card->udev, ep),
708 data, skb->len, mwifiex_usb_tx_complete,
709 (void *)context);
711 tx_urb->transfer_flags |= URB_ZERO_PACKET;
713 if (ep == card->tx_cmd_ep)
714 atomic_inc(&card->tx_cmd_urb_pending);
715 else
716 atomic_inc(&card->tx_data_urb_pending);
718 if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
719 dev_err(adapter->dev, "%s: usb_submit_urb failed\n", __func__);
720 if (ep == card->tx_cmd_ep) {
721 atomic_dec(&card->tx_cmd_urb_pending);
722 } else {
723 atomic_dec(&card->tx_data_urb_pending);
724 if (card->tx_data_ix)
725 card->tx_data_ix--;
726 else
727 card->tx_data_ix = MWIFIEX_TX_DATA_URB;
730 return -1;
731 } else {
732 if (ep == card->tx_data_ep &&
733 atomic_read(&card->tx_data_urb_pending) ==
734 MWIFIEX_TX_DATA_URB)
735 return -ENOSR;
738 return -EINPROGRESS;
741 /* This function register usb device and initialize parameter. */
742 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
744 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
746 card->adapter = adapter;
747 adapter->dev = &card->udev->dev;
748 strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
750 return 0;
753 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
755 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
757 usb_set_intfdata(card->intf, NULL);
760 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
761 struct mwifiex_fw_image *fw)
763 int ret = 0;
764 u8 *firmware = fw->fw_buf, *recv_buff;
765 u32 retries = USB8797_FW_MAX_RETRY, dlen;
766 u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
767 struct fw_data *fwdata;
768 struct fw_sync_header sync_fw;
769 u8 check_winner = 1;
771 if (!firmware) {
772 dev_err(adapter->dev,
773 "No firmware image found! Terminating download\n");
774 ret = -1;
775 goto fw_exit;
778 /* Allocate memory for transmit */
779 fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
780 if (!fwdata)
781 goto fw_exit;
783 /* Allocate memory for receive */
784 recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
785 if (!recv_buff)
786 goto cleanup;
788 do {
789 /* Send pseudo data to check winner status first */
790 if (check_winner) {
791 memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
792 dlen = 0;
793 } else {
794 /* copy the header of the fw_data to get the length */
795 memcpy(&fwdata->fw_hdr, &firmware[tlen],
796 sizeof(struct fw_header));
798 dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
799 dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
800 tlen += sizeof(struct fw_header);
802 memcpy(fwdata->data, &firmware[tlen], dlen);
804 fwdata->seq_num = cpu_to_le32(fw_seqnum);
805 tlen += dlen;
808 /* If the send/receive fails or CRC occurs then retry */
809 while (retries--) {
810 u8 *buf = (u8 *)fwdata;
811 u32 len = FW_DATA_XMIT_SIZE;
813 /* send the firmware block */
814 ret = mwifiex_write_data_sync(adapter, buf, &len,
815 MWIFIEX_USB_EP_CMD_EVENT,
816 MWIFIEX_USB_TIMEOUT);
817 if (ret) {
818 dev_err(adapter->dev,
819 "write_data_sync: failed: %d\n", ret);
820 continue;
823 buf = recv_buff;
824 len = FW_DNLD_RX_BUF_SIZE;
826 /* Receive the firmware block response */
827 ret = mwifiex_read_data_sync(adapter, buf, &len,
828 MWIFIEX_USB_EP_CMD_EVENT,
829 MWIFIEX_USB_TIMEOUT);
830 if (ret) {
831 dev_err(adapter->dev,
832 "read_data_sync: failed: %d\n", ret);
833 continue;
836 memcpy(&sync_fw, recv_buff,
837 sizeof(struct fw_sync_header));
839 /* check 1st firmware block resp for highest bit set */
840 if (check_winner) {
841 if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
842 dev_warn(adapter->dev,
843 "USB is not the winner %#x\n",
844 sync_fw.cmd);
846 /* returning success */
847 ret = 0;
848 goto cleanup;
851 dev_dbg(adapter->dev,
852 "USB is the winner, start to download FW\n");
854 check_winner = 0;
855 break;
858 /* check the firmware block response for CRC errors */
859 if (sync_fw.cmd) {
860 dev_err(adapter->dev,
861 "FW received block with CRC %#x\n",
862 sync_fw.cmd);
863 ret = -1;
864 continue;
867 retries = USB8797_FW_MAX_RETRY;
868 break;
870 fw_seqnum++;
871 } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
873 cleanup:
874 dev_dbg(adapter->dev, "%s: %d bytes downloaded\n", __func__, tlen);
876 kfree(recv_buff);
877 kfree(fwdata);
879 if (retries)
880 ret = 0;
881 fw_exit:
882 return ret;
885 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
886 struct mwifiex_fw_image *fw)
888 int ret;
889 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
891 if (card->usb_boot_state == USB8797_FW_DNLD) {
892 ret = mwifiex_prog_fw_w_helper(adapter, fw);
893 if (ret)
894 return -1;
896 /* Boot state changes after successful firmware download */
897 if (card->usb_boot_state == USB8797_FW_DNLD)
898 return -1;
901 ret = mwifiex_usb_rx_init(adapter);
902 if (!ret)
903 ret = mwifiex_usb_tx_init(adapter);
905 return ret;
908 static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
910 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
912 skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
913 if ((ep == card->rx_cmd_ep) &&
914 (!atomic_read(&card->rx_cmd_urb_pending)))
915 mwifiex_usb_submit_rx_urb(&card->rx_cmd,
916 MWIFIEX_RX_CMD_BUF_SIZE);
918 return;
921 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
922 struct sk_buff *skb)
924 atomic_dec(&adapter->rx_pending);
925 mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
927 return 0;
930 static int mwifiex_usb_data_complete(struct mwifiex_adapter *adapter,
931 struct sk_buff *skb)
933 atomic_dec(&adapter->rx_pending);
934 dev_kfree_skb_any(skb);
936 return 0;
939 /* This function wakes up the card. */
940 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
942 /* Simulation of HS_AWAKE event */
943 adapter->pm_wakeup_fw_try = false;
944 adapter->pm_wakeup_card_req = false;
945 adapter->ps_state = PS_STATE_AWAKE;
947 return 0;
950 static struct mwifiex_if_ops usb_ops = {
951 .register_dev = mwifiex_register_dev,
952 .unregister_dev = mwifiex_unregister_dev,
953 .wakeup = mwifiex_pm_wakeup_card,
954 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
956 /* USB specific */
957 .dnld_fw = mwifiex_usb_dnld_fw,
958 .cmdrsp_complete = mwifiex_usb_cmd_event_complete,
959 .event_complete = mwifiex_usb_cmd_event_complete,
960 .data_complete = mwifiex_usb_data_complete,
961 .host_to_card = mwifiex_usb_host_to_card,
964 /* This function initializes the USB driver module.
966 * This initiates the semaphore and registers the device with
967 * USB bus.
969 static int mwifiex_usb_init_module(void)
971 int ret;
973 pr_debug("Marvell USB8797 Driver\n");
975 sema_init(&add_remove_card_sem, 1);
977 ret = usb_register(&mwifiex_usb_driver);
978 if (ret)
979 pr_err("Driver register failed!\n");
980 else
981 pr_debug("info: Driver registered successfully!\n");
983 return ret;
986 /* This function cleans up the USB driver.
988 * The following major steps are followed in .disconnect for cleanup:
989 * - Resume the device if its suspended
990 * - Disconnect the device if connected
991 * - Shutdown the firmware
992 * - Unregister the device from USB bus.
994 static void mwifiex_usb_cleanup_module(void)
996 if (!down_interruptible(&add_remove_card_sem))
997 up(&add_remove_card_sem);
999 if (usb_card) {
1000 struct mwifiex_adapter *adapter = usb_card->adapter;
1001 int i;
1003 /* In case driver is removed when asynchronous FW downloading is
1004 * in progress
1006 wait_for_completion(&adapter->fw_load);
1008 #ifdef CONFIG_PM
1009 if (adapter->is_suspended)
1010 mwifiex_usb_resume(usb_card->intf);
1011 #endif
1012 for (i = 0; i < adapter->priv_num; i++)
1013 if ((GET_BSS_ROLE(adapter->priv[i]) ==
1014 MWIFIEX_BSS_ROLE_STA) &&
1015 adapter->priv[i]->media_connected)
1016 mwifiex_deauthenticate(adapter->priv[i], NULL);
1018 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
1019 MWIFIEX_BSS_ROLE_ANY),
1020 MWIFIEX_FUNC_SHUTDOWN);
1023 usb_deregister(&mwifiex_usb_driver);
1026 module_init(mwifiex_usb_init_module);
1027 module_exit(mwifiex_usb_cleanup_module);
1029 MODULE_AUTHOR("Marvell International Ltd.");
1030 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1031 MODULE_VERSION(USB_VERSION);
1032 MODULE_LICENSE("GPL v2");
1033 MODULE_FIRMWARE("mrvl/usb8797_uapsta.bin");