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
[] = "usb8xxx";
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
[] = {
33 {USB_DEVICE(USB8XXX_VID
, USB8797_PID_1
)},
34 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID
, USB8797_PID_2
,
35 USB_CLASS_VENDOR_SPEC
,
36 USB_SUBCLASS_VENDOR_SPEC
, 0xff)},
38 {USB_DEVICE(USB8XXX_VID
, USB8897_PID_1
)},
39 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID
, USB8897_PID_2
,
40 USB_CLASS_VENDOR_SPEC
,
41 USB_SUBCLASS_VENDOR_SPEC
, 0xff)},
42 { } /* Terminating entry */
45 MODULE_DEVICE_TABLE(usb
, mwifiex_usb_table
);
47 static int mwifiex_usb_submit_rx_urb(struct urb_context
*ctx
, int size
);
49 /* This function handles received packet. Necessary action is taken based on
52 static int mwifiex_usb_recv(struct mwifiex_adapter
*adapter
,
53 struct sk_buff
*skb
, u8 ep
)
55 struct device
*dev
= adapter
->dev
;
60 if (adapter
->hs_activated
)
61 mwifiex_process_hs_config(adapter
);
63 if (skb
->len
< INTF_HEADER_LEN
) {
64 dev_err(dev
, "%s: invalid skb->len\n", __func__
);
69 case MWIFIEX_USB_EP_CMD_EVENT
:
70 dev_dbg(dev
, "%s: EP_CMD_EVENT\n", __func__
);
71 skb_copy_from_linear_data(skb
, &tmp
, INTF_HEADER_LEN
);
72 recv_type
= le32_to_cpu(tmp
);
73 skb_pull(skb
, INTF_HEADER_LEN
);
76 case MWIFIEX_USB_TYPE_CMD
:
77 if (skb
->len
> MWIFIEX_SIZE_OF_CMD_BUFFER
) {
78 dev_err(dev
, "CMD: skb->len too large\n");
80 goto exit_restore_skb
;
81 } else if (!adapter
->curr_cmd
) {
82 dev_dbg(dev
, "CMD: no curr_cmd\n");
83 if (adapter
->ps_state
== PS_STATE_SLEEP_CFM
) {
84 mwifiex_process_sleep_confirm_resp(
88 goto exit_restore_skb
;
91 goto exit_restore_skb
;
94 adapter
->curr_cmd
->resp_skb
= skb
;
95 adapter
->cmd_resp_received
= true;
97 case MWIFIEX_USB_TYPE_EVENT
:
98 if (skb
->len
< sizeof(u32
)) {
99 dev_err(dev
, "EVENT: skb->len too small\n");
101 goto exit_restore_skb
;
103 skb_copy_from_linear_data(skb
, &tmp
, sizeof(u32
));
104 adapter
->event_cause
= le32_to_cpu(tmp
);
105 dev_dbg(dev
, "event_cause %#x\n", adapter
->event_cause
);
107 if (skb
->len
> MAX_EVENT_SIZE
) {
108 dev_err(dev
, "EVENT: event body too large\n");
110 goto exit_restore_skb
;
113 memcpy(adapter
->event_body
, skb
->data
+
114 MWIFIEX_EVENT_HEADER_LEN
, skb
->len
);
116 adapter
->event_received
= true;
117 adapter
->event_skb
= skb
;
120 dev_err(dev
, "unknown recv_type %#x\n", recv_type
);
124 case MWIFIEX_USB_EP_DATA
:
125 dev_dbg(dev
, "%s: EP_DATA\n", __func__
);
126 if (skb
->len
> MWIFIEX_RX_DATA_BUF_SIZE
) {
127 dev_err(dev
, "DATA: skb->len too large\n");
130 skb_queue_tail(&adapter
->usb_rx_data_q
, skb
);
131 adapter
->data_received
= true;
134 dev_err(dev
, "%s: unknown endport %#x\n", __func__
, ep
);
141 /* The buffer will be reused for further cmds/events */
142 skb_push(skb
, INTF_HEADER_LEN
);
147 static void mwifiex_usb_rx_complete(struct urb
*urb
)
149 struct urb_context
*context
= (struct urb_context
*)urb
->context
;
150 struct mwifiex_adapter
*adapter
= context
->adapter
;
151 struct sk_buff
*skb
= context
->skb
;
152 struct usb_card_rec
*card
;
153 int recv_length
= urb
->actual_length
;
156 if (!adapter
|| !adapter
->card
) {
157 pr_err("mwifiex adapter or card structure is not valid\n");
161 card
= (struct usb_card_rec
*)adapter
->card
;
162 if (card
->rx_cmd_ep
== context
->ep
)
163 atomic_dec(&card
->rx_cmd_urb_pending
);
165 atomic_dec(&card
->rx_data_urb_pending
);
168 if (urb
->status
|| (adapter
->surprise_removed
)) {
169 dev_err(adapter
->dev
,
170 "URB status is failed: %d\n", urb
->status
);
171 /* Do not free skb in case of command ep */
172 if (card
->rx_cmd_ep
!= context
->ep
)
173 dev_kfree_skb_any(skb
);
176 if (skb
->len
> recv_length
)
177 skb_trim(skb
, recv_length
);
179 skb_put(skb
, recv_length
- skb
->len
);
181 atomic_inc(&adapter
->rx_pending
);
182 status
= mwifiex_usb_recv(adapter
, skb
, context
->ep
);
184 dev_dbg(adapter
->dev
, "info: recv_length=%d, status=%d\n",
185 recv_length
, status
);
186 if (status
== -EINPROGRESS
) {
187 queue_work(adapter
->workqueue
, &adapter
->main_work
);
189 /* urb for data_ep is re-submitted now;
190 * urb for cmd_ep will be re-submitted in callback
191 * mwifiex_usb_recv_complete
193 if (card
->rx_cmd_ep
== context
->ep
)
196 atomic_dec(&adapter
->rx_pending
);
198 dev_err(adapter
->dev
,
199 "received data processing failed!\n");
201 /* Do not free skb in case of command ep */
202 if (card
->rx_cmd_ep
!= context
->ep
)
203 dev_kfree_skb_any(skb
);
205 } else if (urb
->status
) {
206 if (!adapter
->is_suspended
) {
207 dev_warn(adapter
->dev
,
208 "Card is removed: %d\n", urb
->status
);
209 adapter
->surprise_removed
= true;
211 dev_kfree_skb_any(skb
);
214 /* Do not free skb in case of command ep */
215 if (card
->rx_cmd_ep
!= context
->ep
)
216 dev_kfree_skb_any(skb
);
218 /* fall through setup_for_next */
222 if (card
->rx_cmd_ep
== context
->ep
)
223 size
= MWIFIEX_RX_CMD_BUF_SIZE
;
225 size
= MWIFIEX_RX_DATA_BUF_SIZE
;
227 mwifiex_usb_submit_rx_urb(context
, size
);
232 static void mwifiex_usb_tx_complete(struct urb
*urb
)
234 struct urb_context
*context
= (struct urb_context
*)(urb
->context
);
235 struct mwifiex_adapter
*adapter
= context
->adapter
;
236 struct usb_card_rec
*card
= adapter
->card
;
238 dev_dbg(adapter
->dev
, "%s: status: %d\n", __func__
, urb
->status
);
240 if (context
->ep
== card
->tx_cmd_ep
) {
241 dev_dbg(adapter
->dev
, "%s: CMD\n", __func__
);
242 atomic_dec(&card
->tx_cmd_urb_pending
);
243 adapter
->cmd_sent
= false;
245 dev_dbg(adapter
->dev
, "%s: DATA\n", __func__
);
246 atomic_dec(&card
->tx_data_urb_pending
);
247 mwifiex_write_data_complete(adapter
, context
->skb
, 0,
248 urb
->status
? -1 : 0);
251 queue_work(adapter
->workqueue
, &adapter
->main_work
);
256 static int mwifiex_usb_submit_rx_urb(struct urb_context
*ctx
, int size
)
258 struct mwifiex_adapter
*adapter
= ctx
->adapter
;
259 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
261 if (card
->rx_cmd_ep
!= ctx
->ep
) {
262 ctx
->skb
= dev_alloc_skb(size
);
264 dev_err(adapter
->dev
,
265 "%s: dev_alloc_skb failed\n", __func__
);
270 usb_fill_bulk_urb(ctx
->urb
, card
->udev
,
271 usb_rcvbulkpipe(card
->udev
, ctx
->ep
), ctx
->skb
->data
,
272 size
, mwifiex_usb_rx_complete
, (void *)ctx
);
274 if (card
->rx_cmd_ep
== ctx
->ep
)
275 atomic_inc(&card
->rx_cmd_urb_pending
);
277 atomic_inc(&card
->rx_data_urb_pending
);
279 if (usb_submit_urb(ctx
->urb
, GFP_ATOMIC
)) {
280 dev_err(adapter
->dev
, "usb_submit_urb failed\n");
281 dev_kfree_skb_any(ctx
->skb
);
284 if (card
->rx_cmd_ep
== ctx
->ep
)
285 atomic_dec(&card
->rx_cmd_urb_pending
);
287 atomic_dec(&card
->rx_data_urb_pending
);
295 static void mwifiex_usb_free(struct usb_card_rec
*card
)
299 if (atomic_read(&card
->rx_cmd_urb_pending
) && card
->rx_cmd
.urb
)
300 usb_kill_urb(card
->rx_cmd
.urb
);
302 usb_free_urb(card
->rx_cmd
.urb
);
303 card
->rx_cmd
.urb
= NULL
;
305 if (atomic_read(&card
->rx_data_urb_pending
))
306 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++)
307 if (card
->rx_data_list
[i
].urb
)
308 usb_kill_urb(card
->rx_data_list
[i
].urb
);
310 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++) {
311 usb_free_urb(card
->rx_data_list
[i
].urb
);
312 card
->rx_data_list
[i
].urb
= NULL
;
315 for (i
= 0; i
< MWIFIEX_TX_DATA_URB
; i
++) {
316 usb_free_urb(card
->tx_data_list
[i
].urb
);
317 card
->tx_data_list
[i
].urb
= NULL
;
320 usb_free_urb(card
->tx_cmd
.urb
);
321 card
->tx_cmd
.urb
= NULL
;
326 /* This function probes an mwifiex device and registers it. It allocates
327 * the card structure, initiates the device registration and initialization
328 * procedure by adding a logical interface.
330 static int mwifiex_usb_probe(struct usb_interface
*intf
,
331 const struct usb_device_id
*id
)
333 struct usb_device
*udev
= interface_to_usbdev(intf
);
334 struct usb_host_interface
*iface_desc
= intf
->cur_altsetting
;
335 struct usb_endpoint_descriptor
*epd
;
337 struct usb_card_rec
*card
;
338 u16 id_vendor
, id_product
, bcd_device
, bcd_usb
;
340 card
= kzalloc(sizeof(struct usb_card_rec
), GFP_KERNEL
);
344 id_vendor
= le16_to_cpu(udev
->descriptor
.idVendor
);
345 id_product
= le16_to_cpu(udev
->descriptor
.idProduct
);
346 bcd_device
= le16_to_cpu(udev
->descriptor
.bcdDevice
);
347 bcd_usb
= le16_to_cpu(udev
->descriptor
.bcdUSB
);
348 pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n",
349 id_vendor
, id_product
, bcd_device
);
351 /* PID_1 is used for firmware downloading only */
352 switch (id_product
) {
355 card
->usb_boot_state
= USB8XXX_FW_DNLD
;
359 card
->usb_boot_state
= USB8XXX_FW_READY
;
362 pr_warning("unknown id_product %#x\n", id_product
);
363 card
->usb_boot_state
= USB8XXX_FW_DNLD
;
370 pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
371 udev
->descriptor
.bcdUSB
, udev
->descriptor
.bDeviceClass
,
372 udev
->descriptor
.bDeviceSubClass
,
373 udev
->descriptor
.bDeviceProtocol
);
375 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
376 epd
= &iface_desc
->endpoint
[i
].desc
;
377 if (usb_endpoint_dir_in(epd
) &&
378 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_CMD_EVENT
&&
379 usb_endpoint_xfer_bulk(epd
)) {
380 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
381 le16_to_cpu(epd
->wMaxPacketSize
),
382 epd
->bEndpointAddress
);
383 card
->rx_cmd_ep
= usb_endpoint_num(epd
);
384 atomic_set(&card
->rx_cmd_urb_pending
, 0);
386 if (usb_endpoint_dir_in(epd
) &&
387 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_DATA
&&
388 usb_endpoint_xfer_bulk(epd
)) {
389 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
390 le16_to_cpu(epd
->wMaxPacketSize
),
391 epd
->bEndpointAddress
);
392 card
->rx_data_ep
= usb_endpoint_num(epd
);
393 atomic_set(&card
->rx_data_urb_pending
, 0);
395 if (usb_endpoint_dir_out(epd
) &&
396 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_DATA
&&
397 usb_endpoint_xfer_bulk(epd
)) {
398 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
399 le16_to_cpu(epd
->wMaxPacketSize
),
400 epd
->bEndpointAddress
);
401 card
->tx_data_ep
= usb_endpoint_num(epd
);
402 atomic_set(&card
->tx_data_urb_pending
, 0);
404 if (usb_endpoint_dir_out(epd
) &&
405 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_CMD_EVENT
&&
406 usb_endpoint_xfer_bulk(epd
)) {
407 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
408 le16_to_cpu(epd
->wMaxPacketSize
),
409 epd
->bEndpointAddress
);
410 card
->tx_cmd_ep
= usb_endpoint_num(epd
);
411 atomic_set(&card
->tx_cmd_urb_pending
, 0);
412 card
->bulk_out_maxpktsize
=
413 le16_to_cpu(epd
->wMaxPacketSize
);
417 usb_set_intfdata(intf
, card
);
419 ret
= mwifiex_add_card(card
, &add_remove_card_sem
, &usb_ops
,
422 pr_err("%s: mwifiex_add_card failed: %d\n", __func__
, ret
);
423 usb_reset_device(udev
);
433 /* Kernel needs to suspend all functions separately. Therefore all
434 * registered functions must have drivers with suspend and resume
435 * methods. Failing that the kernel simply removes the whole card.
437 * If already not suspended, this function allocates and sends a
438 * 'host sleep activate' request to the firmware and turns off the traffic.
440 static int mwifiex_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
442 struct usb_card_rec
*card
= usb_get_intfdata(intf
);
443 struct mwifiex_adapter
*adapter
;
446 if (!card
|| !card
->adapter
) {
447 pr_err("%s: card or card->adapter is NULL\n", __func__
);
450 adapter
= card
->adapter
;
452 if (unlikely(adapter
->is_suspended
))
453 dev_warn(adapter
->dev
, "Device already suspended\n");
455 mwifiex_enable_hs(adapter
);
457 /* 'is_suspended' flag indicates device is suspended.
458 * It must be set here before the usb_kill_urb() calls. Reason
459 * is in the complete handlers, urb->status(= -ENOENT) and
460 * this flag is used in combination to distinguish between a
461 * 'suspended' state and a 'disconnect' one.
463 adapter
->is_suspended
= true;
465 if (atomic_read(&card
->rx_cmd_urb_pending
) && card
->rx_cmd
.urb
)
466 usb_kill_urb(card
->rx_cmd
.urb
);
468 if (atomic_read(&card
->rx_data_urb_pending
))
469 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++)
470 if (card
->rx_data_list
[i
].urb
)
471 usb_kill_urb(card
->rx_data_list
[i
].urb
);
473 for (i
= 0; i
< MWIFIEX_TX_DATA_URB
; i
++)
474 if (card
->tx_data_list
[i
].urb
)
475 usb_kill_urb(card
->tx_data_list
[i
].urb
);
477 if (card
->tx_cmd
.urb
)
478 usb_kill_urb(card
->tx_cmd
.urb
);
483 /* Kernel needs to suspend all functions separately. Therefore all
484 * registered functions must have drivers with suspend and resume
485 * methods. Failing that the kernel simply removes the whole card.
487 * If already not resumed, this function turns on the traffic and
488 * sends a 'host sleep cancel' request to the firmware.
490 static int mwifiex_usb_resume(struct usb_interface
*intf
)
492 struct usb_card_rec
*card
= usb_get_intfdata(intf
);
493 struct mwifiex_adapter
*adapter
;
496 if (!card
|| !card
->adapter
) {
497 pr_err("%s: card or card->adapter is NULL\n", __func__
);
500 adapter
= card
->adapter
;
502 if (unlikely(!adapter
->is_suspended
)) {
503 dev_warn(adapter
->dev
, "Device already resumed\n");
507 /* Indicate device resumed. The netdev queue will be resumed only
508 * after the urbs have been re-submitted
510 adapter
->is_suspended
= false;
512 if (!atomic_read(&card
->rx_data_urb_pending
))
513 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++)
514 mwifiex_usb_submit_rx_urb(&card
->rx_data_list
[i
],
515 MWIFIEX_RX_DATA_BUF_SIZE
);
517 if (!atomic_read(&card
->rx_cmd_urb_pending
)) {
518 card
->rx_cmd
.skb
= dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE
);
519 if (card
->rx_cmd
.skb
)
520 mwifiex_usb_submit_rx_urb(&card
->rx_cmd
,
521 MWIFIEX_RX_CMD_BUF_SIZE
);
524 /* Disable Host Sleep */
525 if (adapter
->hs_activated
)
526 mwifiex_cancel_hs(mwifiex_get_priv(adapter
,
527 MWIFIEX_BSS_ROLE_ANY
),
531 /* Resume handler may be called due to remote wakeup,
532 * force to exit suspend anyway
534 usb_disable_autosuspend(card
->udev
);
535 #endif /* CONFIG_PM */
540 static void mwifiex_usb_disconnect(struct usb_interface
*intf
)
542 struct usb_card_rec
*card
= usb_get_intfdata(intf
);
545 pr_err("%s: card is NULL\n", __func__
);
549 mwifiex_usb_free(card
);
552 struct mwifiex_adapter
*adapter
= card
->adapter
;
554 if (!adapter
->priv_num
)
557 dev_dbg(adapter
->dev
, "%s: removing card\n", __func__
);
558 mwifiex_remove_card(adapter
, &add_remove_card_sem
);
561 usb_set_intfdata(intf
, NULL
);
562 usb_put_dev(interface_to_usbdev(intf
));
569 static struct usb_driver mwifiex_usb_driver
= {
570 .name
= usbdriver_name
,
571 .probe
= mwifiex_usb_probe
,
572 .disconnect
= mwifiex_usb_disconnect
,
573 .id_table
= mwifiex_usb_table
,
574 .suspend
= mwifiex_usb_suspend
,
575 .resume
= mwifiex_usb_resume
,
576 .supports_autosuspend
= 1,
579 static int mwifiex_usb_tx_init(struct mwifiex_adapter
*adapter
)
581 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
584 card
->tx_cmd
.adapter
= adapter
;
585 card
->tx_cmd
.ep
= card
->tx_cmd_ep
;
587 card
->tx_cmd
.urb
= usb_alloc_urb(0, GFP_KERNEL
);
588 if (!card
->tx_cmd
.urb
) {
589 dev_err(adapter
->dev
, "tx_cmd.urb allocation failed\n");
593 card
->tx_data_ix
= 0;
595 for (i
= 0; i
< MWIFIEX_TX_DATA_URB
; i
++) {
596 card
->tx_data_list
[i
].adapter
= adapter
;
597 card
->tx_data_list
[i
].ep
= card
->tx_data_ep
;
599 card
->tx_data_list
[i
].urb
= usb_alloc_urb(0, GFP_KERNEL
);
600 if (!card
->tx_data_list
[i
].urb
) {
601 dev_err(adapter
->dev
,
602 "tx_data_list[] urb allocation failed\n");
610 static int mwifiex_usb_rx_init(struct mwifiex_adapter
*adapter
)
612 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
615 card
->rx_cmd
.adapter
= adapter
;
616 card
->rx_cmd
.ep
= card
->rx_cmd_ep
;
618 card
->rx_cmd
.urb
= usb_alloc_urb(0, GFP_KERNEL
);
619 if (!card
->rx_cmd
.urb
) {
620 dev_err(adapter
->dev
, "rx_cmd.urb allocation failed\n");
624 card
->rx_cmd
.skb
= dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE
);
625 if (!card
->rx_cmd
.skb
) {
626 dev_err(adapter
->dev
, "rx_cmd.skb allocation failed\n");
630 if (mwifiex_usb_submit_rx_urb(&card
->rx_cmd
, MWIFIEX_RX_CMD_BUF_SIZE
))
633 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++) {
634 card
->rx_data_list
[i
].adapter
= adapter
;
635 card
->rx_data_list
[i
].ep
= card
->rx_data_ep
;
637 card
->rx_data_list
[i
].urb
= usb_alloc_urb(0, GFP_KERNEL
);
638 if (!card
->rx_data_list
[i
].urb
) {
639 dev_err(adapter
->dev
,
640 "rx_data_list[] urb allocation failed\n");
643 if (mwifiex_usb_submit_rx_urb(&card
->rx_data_list
[i
],
644 MWIFIEX_RX_DATA_BUF_SIZE
))
651 static int mwifiex_write_data_sync(struct mwifiex_adapter
*adapter
, u8
*pbuf
,
652 u32
*len
, u8 ep
, u32 timeout
)
654 struct usb_card_rec
*card
= adapter
->card
;
655 int actual_length
, ret
;
657 if (!(*len
% card
->bulk_out_maxpktsize
))
660 /* Send the data block */
661 ret
= usb_bulk_msg(card
->udev
, usb_sndbulkpipe(card
->udev
, ep
), pbuf
,
662 *len
, &actual_length
, timeout
);
664 dev_err(adapter
->dev
, "usb_bulk_msg for tx failed: %d\n", ret
);
668 *len
= actual_length
;
673 static int mwifiex_read_data_sync(struct mwifiex_adapter
*adapter
, u8
*pbuf
,
674 u32
*len
, u8 ep
, u32 timeout
)
676 struct usb_card_rec
*card
= adapter
->card
;
677 int actual_length
, ret
;
679 /* Receive the data response */
680 ret
= usb_bulk_msg(card
->udev
, usb_rcvbulkpipe(card
->udev
, ep
), pbuf
,
681 *len
, &actual_length
, timeout
);
683 dev_err(adapter
->dev
, "usb_bulk_msg for rx failed: %d\n", ret
);
687 *len
= actual_length
;
692 /* This function write a command/data packet to card. */
693 static int mwifiex_usb_host_to_card(struct mwifiex_adapter
*adapter
, u8 ep
,
695 struct mwifiex_tx_param
*tx_param
)
697 struct usb_card_rec
*card
= adapter
->card
;
698 struct urb_context
*context
;
699 u8
*data
= (u8
*)skb
->data
;
702 if (adapter
->is_suspended
) {
703 dev_err(adapter
->dev
,
704 "%s: not allowed while suspended\n", __func__
);
708 if (adapter
->surprise_removed
) {
709 dev_err(adapter
->dev
, "%s: device removed\n", __func__
);
713 if (ep
== card
->tx_data_ep
&&
714 atomic_read(&card
->tx_data_urb_pending
) >= MWIFIEX_TX_DATA_URB
) {
718 dev_dbg(adapter
->dev
, "%s: ep=%d\n", __func__
, ep
);
720 if (ep
== card
->tx_cmd_ep
) {
721 context
= &card
->tx_cmd
;
723 if (card
->tx_data_ix
>= MWIFIEX_TX_DATA_URB
)
724 card
->tx_data_ix
= 0;
725 context
= &card
->tx_data_list
[card
->tx_data_ix
++];
728 context
->adapter
= adapter
;
731 tx_urb
= context
->urb
;
733 usb_fill_bulk_urb(tx_urb
, card
->udev
, usb_sndbulkpipe(card
->udev
, ep
),
734 data
, skb
->len
, mwifiex_usb_tx_complete
,
737 tx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
739 if (ep
== card
->tx_cmd_ep
)
740 atomic_inc(&card
->tx_cmd_urb_pending
);
742 atomic_inc(&card
->tx_data_urb_pending
);
744 if (usb_submit_urb(tx_urb
, GFP_ATOMIC
)) {
745 dev_err(adapter
->dev
, "%s: usb_submit_urb failed\n", __func__
);
746 if (ep
== card
->tx_cmd_ep
) {
747 atomic_dec(&card
->tx_cmd_urb_pending
);
749 atomic_dec(&card
->tx_data_urb_pending
);
750 if (card
->tx_data_ix
)
753 card
->tx_data_ix
= MWIFIEX_TX_DATA_URB
;
758 if (ep
== card
->tx_data_ep
&&
759 atomic_read(&card
->tx_data_urb_pending
) ==
767 /* This function register usb device and initialize parameter. */
768 static int mwifiex_register_dev(struct mwifiex_adapter
*adapter
)
770 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
772 card
->adapter
= adapter
;
773 adapter
->dev
= &card
->udev
->dev
;
776 switch (le16_to_cpu(card
->udev
->descriptor
.idProduct
)) {
779 strcpy(adapter
->fw_name
, USB8897_DEFAULT_FW_NAME
);
784 strcpy(adapter
->fw_name
, USB8797_DEFAULT_FW_NAME
);
791 static void mwifiex_unregister_dev(struct mwifiex_adapter
*adapter
)
793 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
795 card
->adapter
= NULL
;
798 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter
*adapter
,
799 struct mwifiex_fw_image
*fw
)
802 u8
*firmware
= fw
->fw_buf
, *recv_buff
;
803 u32 retries
= USB8XXX_FW_MAX_RETRY
, dlen
;
804 u32 fw_seqnum
= 0, tlen
= 0, dnld_cmd
= 0;
805 struct fw_data
*fwdata
;
806 struct fw_sync_header sync_fw
;
810 dev_err(adapter
->dev
,
811 "No firmware image found! Terminating download\n");
816 /* Allocate memory for transmit */
817 fwdata
= kzalloc(FW_DNLD_TX_BUF_SIZE
, GFP_KERNEL
);
821 /* Allocate memory for receive */
822 recv_buff
= kzalloc(FW_DNLD_RX_BUF_SIZE
, GFP_KERNEL
);
827 /* Send pseudo data to check winner status first */
829 memset(&fwdata
->fw_hdr
, 0, sizeof(struct fw_header
));
832 /* copy the header of the fw_data to get the length */
833 memcpy(&fwdata
->fw_hdr
, &firmware
[tlen
],
834 sizeof(struct fw_header
));
836 dlen
= le32_to_cpu(fwdata
->fw_hdr
.data_len
);
837 dnld_cmd
= le32_to_cpu(fwdata
->fw_hdr
.dnld_cmd
);
838 tlen
+= sizeof(struct fw_header
);
840 memcpy(fwdata
->data
, &firmware
[tlen
], dlen
);
842 fwdata
->seq_num
= cpu_to_le32(fw_seqnum
);
846 /* If the send/receive fails or CRC occurs then retry */
848 u8
*buf
= (u8
*)fwdata
;
849 u32 len
= FW_DATA_XMIT_SIZE
;
851 /* send the firmware block */
852 ret
= mwifiex_write_data_sync(adapter
, buf
, &len
,
853 MWIFIEX_USB_EP_CMD_EVENT
,
854 MWIFIEX_USB_TIMEOUT
);
856 dev_err(adapter
->dev
,
857 "write_data_sync: failed: %d\n", ret
);
862 len
= FW_DNLD_RX_BUF_SIZE
;
864 /* Receive the firmware block response */
865 ret
= mwifiex_read_data_sync(adapter
, buf
, &len
,
866 MWIFIEX_USB_EP_CMD_EVENT
,
867 MWIFIEX_USB_TIMEOUT
);
869 dev_err(adapter
->dev
,
870 "read_data_sync: failed: %d\n", ret
);
874 memcpy(&sync_fw
, recv_buff
,
875 sizeof(struct fw_sync_header
));
877 /* check 1st firmware block resp for highest bit set */
879 if (le32_to_cpu(sync_fw
.cmd
) & 0x80000000) {
880 dev_warn(adapter
->dev
,
881 "USB is not the winner %#x\n",
884 /* returning success */
889 dev_dbg(adapter
->dev
,
890 "USB is the winner, start to download FW\n");
896 /* check the firmware block response for CRC errors */
898 dev_err(adapter
->dev
,
899 "FW received block with CRC %#x\n",
905 retries
= USB8XXX_FW_MAX_RETRY
;
909 } while ((dnld_cmd
!= FW_HAS_LAST_BLOCK
) && retries
);
912 dev_dbg(adapter
->dev
, "%s: %d bytes downloaded\n", __func__
, tlen
);
923 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter
*adapter
,
924 struct mwifiex_fw_image
*fw
)
927 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
929 if (card
->usb_boot_state
== USB8XXX_FW_DNLD
) {
930 ret
= mwifiex_prog_fw_w_helper(adapter
, fw
);
934 /* Boot state changes after successful firmware download */
935 if (card
->usb_boot_state
== USB8XXX_FW_DNLD
)
939 ret
= mwifiex_usb_rx_init(adapter
);
941 ret
= mwifiex_usb_tx_init(adapter
);
946 static void mwifiex_submit_rx_urb(struct mwifiex_adapter
*adapter
, u8 ep
)
948 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
950 skb_push(card
->rx_cmd
.skb
, INTF_HEADER_LEN
);
951 if ((ep
== card
->rx_cmd_ep
) &&
952 (!atomic_read(&card
->rx_cmd_urb_pending
)))
953 mwifiex_usb_submit_rx_urb(&card
->rx_cmd
,
954 MWIFIEX_RX_CMD_BUF_SIZE
);
959 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter
*adapter
,
962 atomic_dec(&adapter
->rx_pending
);
963 mwifiex_submit_rx_urb(adapter
, MWIFIEX_USB_EP_CMD_EVENT
);
968 static int mwifiex_usb_data_complete(struct mwifiex_adapter
*adapter
)
970 atomic_dec(&adapter
->rx_pending
);
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 if (usb_card
&& usb_card
->adapter
) {
1036 struct mwifiex_adapter
*adapter
= usb_card
->adapter
;
1039 /* In case driver is removed when asynchronous FW downloading is
1042 wait_for_completion(&adapter
->fw_load
);
1045 if (adapter
->is_suspended
)
1046 mwifiex_usb_resume(usb_card
->intf
);
1048 for (i
= 0; i
< adapter
->priv_num
; i
++)
1049 if ((GET_BSS_ROLE(adapter
->priv
[i
]) ==
1050 MWIFIEX_BSS_ROLE_STA
) &&
1051 adapter
->priv
[i
]->media_connected
)
1052 mwifiex_deauthenticate(adapter
->priv
[i
], NULL
);
1054 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter
,
1055 MWIFIEX_BSS_ROLE_ANY
),
1056 MWIFIEX_FUNC_SHUTDOWN
);
1059 usb_deregister(&mwifiex_usb_driver
);
1062 module_init(mwifiex_usb_init_module
);
1063 module_exit(mwifiex_usb_cleanup_module
);
1065 MODULE_AUTHOR("Marvell International Ltd.");
1066 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION
);
1067 MODULE_VERSION(USB_VERSION
);
1068 MODULE_LICENSE("GPL v2");
1069 MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME
);
1070 MODULE_FIRMWARE(USB8897_DEFAULT_FW_NAME
);