2 * Marvell Wireless LAN device driver: USB specific handling
4 * Copyright (C) 2012-2014, Marvell International Ltd.
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
23 #define USB_VERSION "1.0"
26 static struct mwifiex_if_ops usb_ops
;
27 static struct semaphore add_remove_card_sem
;
29 static struct usb_device_id mwifiex_usb_table
[] = {
31 {USB_DEVICE(USB8XXX_VID
, USB8766_PID_1
)},
32 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID
, USB8766_PID_2
,
33 USB_CLASS_VENDOR_SPEC
,
34 USB_SUBCLASS_VENDOR_SPEC
, 0xff)},
36 {USB_DEVICE(USB8XXX_VID
, USB8797_PID_1
)},
37 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID
, USB8797_PID_2
,
38 USB_CLASS_VENDOR_SPEC
,
39 USB_SUBCLASS_VENDOR_SPEC
, 0xff)},
41 {USB_DEVICE(USB8XXX_VID
, USB8801_PID_1
)},
42 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID
, USB8801_PID_2
,
43 USB_CLASS_VENDOR_SPEC
,
44 USB_SUBCLASS_VENDOR_SPEC
, 0xff)},
46 {USB_DEVICE(USB8XXX_VID
, USB8897_PID_1
)},
47 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID
, USB8897_PID_2
,
48 USB_CLASS_VENDOR_SPEC
,
49 USB_SUBCLASS_VENDOR_SPEC
, 0xff)},
51 {USB_DEVICE(USB8XXX_VID
, USB8997_PID_1
)},
52 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID
, USB8997_PID_2
,
53 USB_CLASS_VENDOR_SPEC
,
54 USB_SUBCLASS_VENDOR_SPEC
, 0xff)},
55 { } /* Terminating entry */
58 MODULE_DEVICE_TABLE(usb
, mwifiex_usb_table
);
60 static int mwifiex_usb_submit_rx_urb(struct urb_context
*ctx
, int size
);
62 /* This function handles received packet. Necessary action is taken based on
65 static int mwifiex_usb_recv(struct mwifiex_adapter
*adapter
,
66 struct sk_buff
*skb
, u8 ep
)
72 if (adapter
->hs_activated
)
73 mwifiex_process_hs_config(adapter
);
75 if (skb
->len
< INTF_HEADER_LEN
) {
76 mwifiex_dbg(adapter
, ERROR
,
77 "%s: invalid skb->len\n", __func__
);
82 case MWIFIEX_USB_EP_CMD_EVENT
:
83 mwifiex_dbg(adapter
, EVENT
,
84 "%s: EP_CMD_EVENT\n", __func__
);
85 skb_copy_from_linear_data(skb
, &tmp
, INTF_HEADER_LEN
);
86 recv_type
= le32_to_cpu(tmp
);
87 skb_pull(skb
, INTF_HEADER_LEN
);
90 case MWIFIEX_USB_TYPE_CMD
:
91 if (skb
->len
> MWIFIEX_SIZE_OF_CMD_BUFFER
) {
92 mwifiex_dbg(adapter
, ERROR
,
93 "CMD: skb->len too large\n");
95 goto exit_restore_skb
;
96 } else if (!adapter
->curr_cmd
) {
97 mwifiex_dbg(adapter
, WARN
, "CMD: no curr_cmd\n");
98 if (adapter
->ps_state
== PS_STATE_SLEEP_CFM
) {
99 mwifiex_process_sleep_confirm_resp(
103 goto exit_restore_skb
;
106 goto exit_restore_skb
;
109 adapter
->curr_cmd
->resp_skb
= skb
;
110 adapter
->cmd_resp_received
= true;
112 case MWIFIEX_USB_TYPE_EVENT
:
113 if (skb
->len
< sizeof(u32
)) {
114 mwifiex_dbg(adapter
, ERROR
,
115 "EVENT: skb->len too small\n");
117 goto exit_restore_skb
;
119 skb_copy_from_linear_data(skb
, &tmp
, sizeof(u32
));
120 adapter
->event_cause
= le32_to_cpu(tmp
);
121 mwifiex_dbg(adapter
, EVENT
,
122 "event_cause %#x\n", adapter
->event_cause
);
124 if (skb
->len
> MAX_EVENT_SIZE
) {
125 mwifiex_dbg(adapter
, ERROR
,
126 "EVENT: event body too large\n");
128 goto exit_restore_skb
;
131 memcpy(adapter
->event_body
, skb
->data
+
132 MWIFIEX_EVENT_HEADER_LEN
, skb
->len
);
134 adapter
->event_received
= true;
135 adapter
->event_skb
= skb
;
138 mwifiex_dbg(adapter
, ERROR
,
139 "unknown recv_type %#x\n", recv_type
);
143 case MWIFIEX_USB_EP_DATA
:
144 mwifiex_dbg(adapter
, DATA
, "%s: EP_DATA\n", __func__
);
145 if (skb
->len
> MWIFIEX_RX_DATA_BUF_SIZE
) {
146 mwifiex_dbg(adapter
, ERROR
,
147 "DATA: skb->len too large\n");
151 skb_queue_tail(&adapter
->rx_data_q
, skb
);
152 adapter
->data_received
= true;
153 atomic_inc(&adapter
->rx_pending
);
156 mwifiex_dbg(adapter
, ERROR
,
157 "%s: unknown endport %#x\n", __func__
, ep
);
164 /* The buffer will be reused for further cmds/events */
165 skb_push(skb
, INTF_HEADER_LEN
);
170 static void mwifiex_usb_rx_complete(struct urb
*urb
)
172 struct urb_context
*context
= (struct urb_context
*)urb
->context
;
173 struct mwifiex_adapter
*adapter
= context
->adapter
;
174 struct sk_buff
*skb
= context
->skb
;
175 struct usb_card_rec
*card
;
176 int recv_length
= urb
->actual_length
;
179 if (!adapter
|| !adapter
->card
) {
180 pr_err("mwifiex adapter or card structure is not valid\n");
184 card
= (struct usb_card_rec
*)adapter
->card
;
185 if (card
->rx_cmd_ep
== context
->ep
)
186 atomic_dec(&card
->rx_cmd_urb_pending
);
188 atomic_dec(&card
->rx_data_urb_pending
);
191 if (urb
->status
|| (adapter
->surprise_removed
)) {
192 mwifiex_dbg(adapter
, ERROR
,
193 "URB status is failed: %d\n", urb
->status
);
194 /* Do not free skb in case of command ep */
195 if (card
->rx_cmd_ep
!= context
->ep
)
196 dev_kfree_skb_any(skb
);
199 if (skb
->len
> recv_length
)
200 skb_trim(skb
, recv_length
);
202 skb_put(skb
, recv_length
- skb
->len
);
204 status
= mwifiex_usb_recv(adapter
, skb
, context
->ep
);
206 mwifiex_dbg(adapter
, INFO
,
207 "info: recv_length=%d, status=%d\n",
208 recv_length
, status
);
209 if (status
== -EINPROGRESS
) {
210 mwifiex_queue_main_work(adapter
);
212 /* urb for data_ep is re-submitted now;
213 * urb for cmd_ep will be re-submitted in callback
214 * mwifiex_usb_recv_complete
216 if (card
->rx_cmd_ep
== context
->ep
)
220 mwifiex_dbg(adapter
, ERROR
,
221 "received data processing failed!\n");
223 /* Do not free skb in case of command ep */
224 if (card
->rx_cmd_ep
!= context
->ep
)
225 dev_kfree_skb_any(skb
);
227 } else if (urb
->status
) {
228 if (!adapter
->is_suspended
) {
229 mwifiex_dbg(adapter
, FATAL
,
230 "Card is removed: %d\n", urb
->status
);
231 adapter
->surprise_removed
= true;
233 dev_kfree_skb_any(skb
);
236 /* Do not free skb in case of command ep */
237 if (card
->rx_cmd_ep
!= context
->ep
)
238 dev_kfree_skb_any(skb
);
240 /* fall through setup_for_next */
244 if (card
->rx_cmd_ep
== context
->ep
)
245 size
= MWIFIEX_RX_CMD_BUF_SIZE
;
247 size
= MWIFIEX_RX_DATA_BUF_SIZE
;
249 if (card
->rx_cmd_ep
== context
->ep
) {
250 mwifiex_usb_submit_rx_urb(context
, size
);
252 if (atomic_read(&adapter
->rx_pending
) <= HIGH_RX_PENDING
){
253 mwifiex_usb_submit_rx_urb(context
, size
);
262 static void mwifiex_usb_tx_complete(struct urb
*urb
)
264 struct urb_context
*context
= (struct urb_context
*)(urb
->context
);
265 struct mwifiex_adapter
*adapter
= context
->adapter
;
266 struct usb_card_rec
*card
= adapter
->card
;
268 mwifiex_dbg(adapter
, INFO
,
269 "%s: status: %d\n", __func__
, urb
->status
);
271 if (context
->ep
== card
->tx_cmd_ep
) {
272 mwifiex_dbg(adapter
, CMD
,
273 "%s: CMD\n", __func__
);
274 atomic_dec(&card
->tx_cmd_urb_pending
);
275 adapter
->cmd_sent
= false;
277 mwifiex_dbg(adapter
, DATA
,
278 "%s: DATA\n", __func__
);
279 atomic_dec(&card
->tx_data_urb_pending
);
280 adapter
->data_sent
= false;
281 mwifiex_write_data_complete(adapter
, context
->skb
, 0,
282 urb
->status
? -1 : 0);
285 mwifiex_queue_main_work(adapter
);
290 static int mwifiex_usb_submit_rx_urb(struct urb_context
*ctx
, int size
)
292 struct mwifiex_adapter
*adapter
= ctx
->adapter
;
293 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
295 if (card
->rx_cmd_ep
!= ctx
->ep
) {
296 ctx
->skb
= dev_alloc_skb(size
);
298 mwifiex_dbg(adapter
, ERROR
,
299 "%s: dev_alloc_skb failed\n", __func__
);
304 usb_fill_bulk_urb(ctx
->urb
, card
->udev
,
305 usb_rcvbulkpipe(card
->udev
, ctx
->ep
), ctx
->skb
->data
,
306 size
, mwifiex_usb_rx_complete
, (void *)ctx
);
308 if (card
->rx_cmd_ep
== ctx
->ep
)
309 atomic_inc(&card
->rx_cmd_urb_pending
);
311 atomic_inc(&card
->rx_data_urb_pending
);
313 if (usb_submit_urb(ctx
->urb
, GFP_ATOMIC
)) {
314 mwifiex_dbg(adapter
, ERROR
, "usb_submit_urb failed\n");
315 dev_kfree_skb_any(ctx
->skb
);
318 if (card
->rx_cmd_ep
== ctx
->ep
)
319 atomic_dec(&card
->rx_cmd_urb_pending
);
321 atomic_dec(&card
->rx_data_urb_pending
);
329 static void mwifiex_usb_free(struct usb_card_rec
*card
)
333 if (atomic_read(&card
->rx_cmd_urb_pending
) && card
->rx_cmd
.urb
)
334 usb_kill_urb(card
->rx_cmd
.urb
);
336 usb_free_urb(card
->rx_cmd
.urb
);
337 card
->rx_cmd
.urb
= NULL
;
339 if (atomic_read(&card
->rx_data_urb_pending
))
340 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++)
341 if (card
->rx_data_list
[i
].urb
)
342 usb_kill_urb(card
->rx_data_list
[i
].urb
);
344 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++) {
345 usb_free_urb(card
->rx_data_list
[i
].urb
);
346 card
->rx_data_list
[i
].urb
= NULL
;
349 for (i
= 0; i
< MWIFIEX_TX_DATA_URB
; i
++) {
350 usb_free_urb(card
->tx_data_list
[i
].urb
);
351 card
->tx_data_list
[i
].urb
= NULL
;
354 usb_free_urb(card
->tx_cmd
.urb
);
355 card
->tx_cmd
.urb
= NULL
;
360 /* This function probes an mwifiex device and registers it. It allocates
361 * the card structure, initiates the device registration and initialization
362 * procedure by adding a logical interface.
364 static int mwifiex_usb_probe(struct usb_interface
*intf
,
365 const struct usb_device_id
*id
)
367 struct usb_device
*udev
= interface_to_usbdev(intf
);
368 struct usb_host_interface
*iface_desc
= intf
->cur_altsetting
;
369 struct usb_endpoint_descriptor
*epd
;
371 struct usb_card_rec
*card
;
372 u16 id_vendor
, id_product
, bcd_device
, bcd_usb
;
374 card
= kzalloc(sizeof(struct usb_card_rec
), GFP_KERNEL
);
378 id_vendor
= le16_to_cpu(udev
->descriptor
.idVendor
);
379 id_product
= le16_to_cpu(udev
->descriptor
.idProduct
);
380 bcd_device
= le16_to_cpu(udev
->descriptor
.bcdDevice
);
381 bcd_usb
= le16_to_cpu(udev
->descriptor
.bcdUSB
);
382 pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n",
383 id_vendor
, id_product
, bcd_device
);
385 /* PID_1 is used for firmware downloading only */
386 switch (id_product
) {
392 card
->usb_boot_state
= USB8XXX_FW_DNLD
;
399 card
->usb_boot_state
= USB8XXX_FW_READY
;
402 pr_warn("unknown id_product %#x\n", id_product
);
403 card
->usb_boot_state
= USB8XXX_FW_DNLD
;
410 pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
411 udev
->descriptor
.bcdUSB
, udev
->descriptor
.bDeviceClass
,
412 udev
->descriptor
.bDeviceSubClass
,
413 udev
->descriptor
.bDeviceProtocol
);
415 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
416 epd
= &iface_desc
->endpoint
[i
].desc
;
417 if (usb_endpoint_dir_in(epd
) &&
418 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_CMD_EVENT
&&
419 usb_endpoint_xfer_bulk(epd
)) {
420 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
421 le16_to_cpu(epd
->wMaxPacketSize
),
422 epd
->bEndpointAddress
);
423 card
->rx_cmd_ep
= usb_endpoint_num(epd
);
424 atomic_set(&card
->rx_cmd_urb_pending
, 0);
426 if (usb_endpoint_dir_in(epd
) &&
427 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_DATA
&&
428 usb_endpoint_xfer_bulk(epd
)) {
429 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
430 le16_to_cpu(epd
->wMaxPacketSize
),
431 epd
->bEndpointAddress
);
432 card
->rx_data_ep
= usb_endpoint_num(epd
);
433 atomic_set(&card
->rx_data_urb_pending
, 0);
435 if (usb_endpoint_dir_out(epd
) &&
436 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_DATA
&&
437 usb_endpoint_xfer_bulk(epd
)) {
438 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
439 le16_to_cpu(epd
->wMaxPacketSize
),
440 epd
->bEndpointAddress
);
441 card
->tx_data_ep
= usb_endpoint_num(epd
);
442 atomic_set(&card
->tx_data_urb_pending
, 0);
444 if (usb_endpoint_dir_out(epd
) &&
445 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_CMD_EVENT
&&
446 usb_endpoint_xfer_bulk(epd
)) {
447 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
448 le16_to_cpu(epd
->wMaxPacketSize
),
449 epd
->bEndpointAddress
);
450 card
->tx_cmd_ep
= usb_endpoint_num(epd
);
451 atomic_set(&card
->tx_cmd_urb_pending
, 0);
452 card
->bulk_out_maxpktsize
=
453 le16_to_cpu(epd
->wMaxPacketSize
);
457 usb_set_intfdata(intf
, card
);
459 ret
= mwifiex_add_card(card
, &add_remove_card_sem
, &usb_ops
,
462 pr_err("%s: mwifiex_add_card failed: %d\n", __func__
, ret
);
463 usb_reset_device(udev
);
473 /* Kernel needs to suspend all functions separately. Therefore all
474 * registered functions must have drivers with suspend and resume
475 * methods. Failing that the kernel simply removes the whole card.
477 * If already not suspended, this function allocates and sends a
478 * 'host sleep activate' request to the firmware and turns off the traffic.
480 static int mwifiex_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
482 struct usb_card_rec
*card
= usb_get_intfdata(intf
);
483 struct mwifiex_adapter
*adapter
;
486 if (!card
|| !card
->adapter
) {
487 pr_err("%s: card or card->adapter is NULL\n", __func__
);
490 adapter
= card
->adapter
;
492 if (unlikely(adapter
->is_suspended
))
493 mwifiex_dbg(adapter
, WARN
,
494 "Device already suspended\n");
496 mwifiex_enable_hs(adapter
);
498 /* 'is_suspended' flag indicates device is suspended.
499 * It must be set here before the usb_kill_urb() calls. Reason
500 * is in the complete handlers, urb->status(= -ENOENT) and
501 * this flag is used in combination to distinguish between a
502 * 'suspended' state and a 'disconnect' one.
504 adapter
->is_suspended
= true;
505 adapter
->hs_enabling
= false;
507 if (atomic_read(&card
->rx_cmd_urb_pending
) && card
->rx_cmd
.urb
)
508 usb_kill_urb(card
->rx_cmd
.urb
);
510 if (atomic_read(&card
->rx_data_urb_pending
))
511 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++)
512 if (card
->rx_data_list
[i
].urb
)
513 usb_kill_urb(card
->rx_data_list
[i
].urb
);
515 for (i
= 0; i
< MWIFIEX_TX_DATA_URB
; i
++)
516 if (card
->tx_data_list
[i
].urb
)
517 usb_kill_urb(card
->tx_data_list
[i
].urb
);
519 if (card
->tx_cmd
.urb
)
520 usb_kill_urb(card
->tx_cmd
.urb
);
525 /* Kernel needs to suspend all functions separately. Therefore all
526 * registered functions must have drivers with suspend and resume
527 * methods. Failing that the kernel simply removes the whole card.
529 * If already not resumed, this function turns on the traffic and
530 * sends a 'host sleep cancel' request to the firmware.
532 static int mwifiex_usb_resume(struct usb_interface
*intf
)
534 struct usb_card_rec
*card
= usb_get_intfdata(intf
);
535 struct mwifiex_adapter
*adapter
;
538 if (!card
|| !card
->adapter
) {
539 pr_err("%s: card or card->adapter is NULL\n", __func__
);
542 adapter
= card
->adapter
;
544 if (unlikely(!adapter
->is_suspended
)) {
545 mwifiex_dbg(adapter
, WARN
,
546 "Device already resumed\n");
550 /* Indicate device resumed. The netdev queue will be resumed only
551 * after the urbs have been re-submitted
553 adapter
->is_suspended
= false;
555 if (!atomic_read(&card
->rx_data_urb_pending
))
556 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++)
557 mwifiex_usb_submit_rx_urb(&card
->rx_data_list
[i
],
558 MWIFIEX_RX_DATA_BUF_SIZE
);
560 if (!atomic_read(&card
->rx_cmd_urb_pending
)) {
561 card
->rx_cmd
.skb
= dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE
);
562 if (card
->rx_cmd
.skb
)
563 mwifiex_usb_submit_rx_urb(&card
->rx_cmd
,
564 MWIFIEX_RX_CMD_BUF_SIZE
);
567 /* Disable Host Sleep */
568 if (adapter
->hs_activated
)
569 mwifiex_cancel_hs(mwifiex_get_priv(adapter
,
570 MWIFIEX_BSS_ROLE_ANY
),
576 static void mwifiex_usb_disconnect(struct usb_interface
*intf
)
578 struct usb_card_rec
*card
= usb_get_intfdata(intf
);
579 struct mwifiex_adapter
*adapter
;
581 if (!card
|| !card
->adapter
) {
582 pr_err("%s: card or card->adapter is NULL\n", __func__
);
586 adapter
= card
->adapter
;
587 if (!adapter
->priv_num
)
592 if (adapter
->is_suspended
)
593 mwifiex_usb_resume(intf
);
596 mwifiex_deauthenticate_all(adapter
);
598 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter
,
599 MWIFIEX_BSS_ROLE_ANY
),
600 MWIFIEX_FUNC_SHUTDOWN
);
603 mwifiex_usb_free(card
);
605 mwifiex_dbg(adapter
, FATAL
,
606 "%s: removing card\n", __func__
);
607 mwifiex_remove_card(adapter
, &add_remove_card_sem
);
609 usb_set_intfdata(intf
, NULL
);
610 usb_put_dev(interface_to_usbdev(intf
));
616 static struct usb_driver mwifiex_usb_driver
= {
617 .name
= "mwifiex_usb",
618 .probe
= mwifiex_usb_probe
,
619 .disconnect
= mwifiex_usb_disconnect
,
620 .id_table
= mwifiex_usb_table
,
621 .suspend
= mwifiex_usb_suspend
,
622 .resume
= mwifiex_usb_resume
,
626 static int mwifiex_usb_tx_init(struct mwifiex_adapter
*adapter
)
628 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
631 card
->tx_cmd
.adapter
= adapter
;
632 card
->tx_cmd
.ep
= card
->tx_cmd_ep
;
634 card
->tx_cmd
.urb
= usb_alloc_urb(0, GFP_KERNEL
);
635 if (!card
->tx_cmd
.urb
) {
636 mwifiex_dbg(adapter
, ERROR
,
637 "tx_cmd.urb allocation failed\n");
641 card
->tx_data_ix
= 0;
643 for (i
= 0; i
< MWIFIEX_TX_DATA_URB
; i
++) {
644 card
->tx_data_list
[i
].adapter
= adapter
;
645 card
->tx_data_list
[i
].ep
= card
->tx_data_ep
;
647 card
->tx_data_list
[i
].urb
= usb_alloc_urb(0, GFP_KERNEL
);
648 if (!card
->tx_data_list
[i
].urb
) {
649 mwifiex_dbg(adapter
, ERROR
,
650 "tx_data_list[] urb allocation failed\n");
658 static int mwifiex_usb_rx_init(struct mwifiex_adapter
*adapter
)
660 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
663 card
->rx_cmd
.adapter
= adapter
;
664 card
->rx_cmd
.ep
= card
->rx_cmd_ep
;
666 card
->rx_cmd
.urb
= usb_alloc_urb(0, GFP_KERNEL
);
667 if (!card
->rx_cmd
.urb
) {
668 mwifiex_dbg(adapter
, ERROR
, "rx_cmd.urb allocation failed\n");
672 card
->rx_cmd
.skb
= dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE
);
673 if (!card
->rx_cmd
.skb
)
676 if (mwifiex_usb_submit_rx_urb(&card
->rx_cmd
, MWIFIEX_RX_CMD_BUF_SIZE
))
679 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++) {
680 card
->rx_data_list
[i
].adapter
= adapter
;
681 card
->rx_data_list
[i
].ep
= card
->rx_data_ep
;
683 card
->rx_data_list
[i
].urb
= usb_alloc_urb(0, GFP_KERNEL
);
684 if (!card
->rx_data_list
[i
].urb
) {
685 mwifiex_dbg(adapter
, ERROR
,
686 "rx_data_list[] urb allocation failed\n");
689 if (mwifiex_usb_submit_rx_urb(&card
->rx_data_list
[i
],
690 MWIFIEX_RX_DATA_BUF_SIZE
))
697 static int mwifiex_write_data_sync(struct mwifiex_adapter
*adapter
, u8
*pbuf
,
698 u32
*len
, u8 ep
, u32 timeout
)
700 struct usb_card_rec
*card
= adapter
->card
;
701 int actual_length
, ret
;
703 if (!(*len
% card
->bulk_out_maxpktsize
))
706 /* Send the data block */
707 ret
= usb_bulk_msg(card
->udev
, usb_sndbulkpipe(card
->udev
, ep
), pbuf
,
708 *len
, &actual_length
, timeout
);
710 mwifiex_dbg(adapter
, ERROR
,
711 "usb_bulk_msg for tx failed: %d\n", ret
);
715 *len
= actual_length
;
720 static int mwifiex_read_data_sync(struct mwifiex_adapter
*adapter
, u8
*pbuf
,
721 u32
*len
, u8 ep
, u32 timeout
)
723 struct usb_card_rec
*card
= adapter
->card
;
724 int actual_length
, ret
;
726 /* Receive the data response */
727 ret
= usb_bulk_msg(card
->udev
, usb_rcvbulkpipe(card
->udev
, ep
), pbuf
,
728 *len
, &actual_length
, timeout
);
730 mwifiex_dbg(adapter
, ERROR
,
731 "usb_bulk_msg for rx failed: %d\n", ret
);
735 *len
= actual_length
;
740 /* This function write a command/data packet to card. */
741 static int mwifiex_usb_host_to_card(struct mwifiex_adapter
*adapter
, u8 ep
,
743 struct mwifiex_tx_param
*tx_param
)
745 struct usb_card_rec
*card
= adapter
->card
;
746 struct urb_context
*context
;
747 u8
*data
= (u8
*)skb
->data
;
750 if (adapter
->is_suspended
) {
751 mwifiex_dbg(adapter
, ERROR
,
752 "%s: not allowed while suspended\n", __func__
);
756 if (adapter
->surprise_removed
) {
757 mwifiex_dbg(adapter
, ERROR
, "%s: device removed\n", __func__
);
761 if (ep
== card
->tx_data_ep
&&
762 atomic_read(&card
->tx_data_urb_pending
) >= MWIFIEX_TX_DATA_URB
) {
763 adapter
->data_sent
= true;
767 mwifiex_dbg(adapter
, INFO
, "%s: ep=%d\n", __func__
, ep
);
769 if (ep
== card
->tx_cmd_ep
) {
770 context
= &card
->tx_cmd
;
772 if (card
->tx_data_ix
>= MWIFIEX_TX_DATA_URB
)
773 card
->tx_data_ix
= 0;
774 context
= &card
->tx_data_list
[card
->tx_data_ix
++];
777 context
->adapter
= adapter
;
780 tx_urb
= context
->urb
;
782 usb_fill_bulk_urb(tx_urb
, card
->udev
, usb_sndbulkpipe(card
->udev
, ep
),
783 data
, skb
->len
, mwifiex_usb_tx_complete
,
786 tx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
788 if (ep
== card
->tx_cmd_ep
)
789 atomic_inc(&card
->tx_cmd_urb_pending
);
791 atomic_inc(&card
->tx_data_urb_pending
);
793 if (usb_submit_urb(tx_urb
, GFP_ATOMIC
)) {
794 mwifiex_dbg(adapter
, ERROR
,
795 "%s: usb_submit_urb failed\n", __func__
);
796 if (ep
== card
->tx_cmd_ep
) {
797 atomic_dec(&card
->tx_cmd_urb_pending
);
799 atomic_dec(&card
->tx_data_urb_pending
);
800 adapter
->data_sent
= false;
801 if (card
->tx_data_ix
)
804 card
->tx_data_ix
= MWIFIEX_TX_DATA_URB
;
809 if (ep
== card
->tx_data_ep
&&
810 atomic_read(&card
->tx_data_urb_pending
) ==
811 MWIFIEX_TX_DATA_URB
) {
812 adapter
->data_sent
= true;
820 /* This function register usb device and initialize parameter. */
821 static int mwifiex_register_dev(struct mwifiex_adapter
*adapter
)
823 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
825 card
->adapter
= adapter
;
826 adapter
->dev
= &card
->udev
->dev
;
828 switch (le16_to_cpu(card
->udev
->descriptor
.idProduct
)) {
831 adapter
->tx_buf_size
= MWIFIEX_TX_DATA_BUF_SIZE_4K
;
832 strcpy(adapter
->fw_name
, USB8997_DEFAULT_FW_NAME
);
833 adapter
->ext_scan
= true;
837 adapter
->tx_buf_size
= MWIFIEX_TX_DATA_BUF_SIZE_4K
;
838 strcpy(adapter
->fw_name
, USB8897_DEFAULT_FW_NAME
);
839 adapter
->ext_scan
= true;
843 adapter
->tx_buf_size
= MWIFIEX_TX_DATA_BUF_SIZE_2K
;
844 strcpy(adapter
->fw_name
, USB8766_DEFAULT_FW_NAME
);
845 adapter
->ext_scan
= true;
849 adapter
->tx_buf_size
= MWIFIEX_TX_DATA_BUF_SIZE_2K
;
850 strcpy(adapter
->fw_name
, USB8801_DEFAULT_FW_NAME
);
851 adapter
->ext_scan
= false;
856 adapter
->tx_buf_size
= MWIFIEX_TX_DATA_BUF_SIZE_2K
;
857 strcpy(adapter
->fw_name
, USB8797_DEFAULT_FW_NAME
);
861 adapter
->usb_mc_status
= false;
866 static void mwifiex_unregister_dev(struct mwifiex_adapter
*adapter
)
868 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
870 card
->adapter
= NULL
;
873 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter
*adapter
,
874 struct mwifiex_fw_image
*fw
)
877 u8
*firmware
= fw
->fw_buf
, *recv_buff
;
878 u32 retries
= USB8XXX_FW_MAX_RETRY
, dlen
;
879 u32 fw_seqnum
= 0, tlen
= 0, dnld_cmd
= 0;
880 struct fw_data
*fwdata
;
881 struct fw_sync_header sync_fw
;
885 mwifiex_dbg(adapter
, ERROR
,
886 "No firmware image found! Terminating download\n");
891 /* Allocate memory for transmit */
892 fwdata
= kzalloc(FW_DNLD_TX_BUF_SIZE
, GFP_KERNEL
);
898 /* Allocate memory for receive */
899 recv_buff
= kzalloc(FW_DNLD_RX_BUF_SIZE
, GFP_KERNEL
);
904 /* Send pseudo data to check winner status first */
906 memset(&fwdata
->fw_hdr
, 0, sizeof(struct fw_header
));
909 /* copy the header of the fw_data to get the length */
910 memcpy(&fwdata
->fw_hdr
, &firmware
[tlen
],
911 sizeof(struct fw_header
));
913 dlen
= le32_to_cpu(fwdata
->fw_hdr
.data_len
);
914 dnld_cmd
= le32_to_cpu(fwdata
->fw_hdr
.dnld_cmd
);
915 tlen
+= sizeof(struct fw_header
);
917 memcpy(fwdata
->data
, &firmware
[tlen
], dlen
);
919 fwdata
->seq_num
= cpu_to_le32(fw_seqnum
);
923 /* If the send/receive fails or CRC occurs then retry */
925 u8
*buf
= (u8
*)fwdata
;
926 u32 len
= FW_DATA_XMIT_SIZE
;
928 /* send the firmware block */
929 ret
= mwifiex_write_data_sync(adapter
, buf
, &len
,
930 MWIFIEX_USB_EP_CMD_EVENT
,
931 MWIFIEX_USB_TIMEOUT
);
933 mwifiex_dbg(adapter
, ERROR
,
934 "write_data_sync: failed: %d\n",
940 len
= FW_DNLD_RX_BUF_SIZE
;
942 /* Receive the firmware block response */
943 ret
= mwifiex_read_data_sync(adapter
, buf
, &len
,
944 MWIFIEX_USB_EP_CMD_EVENT
,
945 MWIFIEX_USB_TIMEOUT
);
947 mwifiex_dbg(adapter
, ERROR
,
948 "read_data_sync: failed: %d\n",
953 memcpy(&sync_fw
, recv_buff
,
954 sizeof(struct fw_sync_header
));
956 /* check 1st firmware block resp for highest bit set */
958 if (le32_to_cpu(sync_fw
.cmd
) & 0x80000000) {
959 mwifiex_dbg(adapter
, WARN
,
960 "USB is not the winner %#x\n",
963 /* returning success */
968 mwifiex_dbg(adapter
, MSG
,
969 "start to download FW...\n");
975 /* check the firmware block response for CRC errors */
977 mwifiex_dbg(adapter
, ERROR
,
978 "FW received block with CRC %#x\n",
984 retries
= USB8XXX_FW_MAX_RETRY
;
988 } while ((dnld_cmd
!= FW_HAS_LAST_BLOCK
) && retries
);
991 mwifiex_dbg(adapter
, MSG
,
992 "info: FW download over, size %d bytes\n", tlen
);
1003 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter
*adapter
,
1004 struct mwifiex_fw_image
*fw
)
1007 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
1009 if (card
->usb_boot_state
== USB8XXX_FW_DNLD
) {
1010 ret
= mwifiex_prog_fw_w_helper(adapter
, fw
);
1014 /* Boot state changes after successful firmware download */
1015 if (card
->usb_boot_state
== USB8XXX_FW_DNLD
)
1019 ret
= mwifiex_usb_rx_init(adapter
);
1021 ret
= mwifiex_usb_tx_init(adapter
);
1026 static void mwifiex_submit_rx_urb(struct mwifiex_adapter
*adapter
, u8 ep
)
1028 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
1030 skb_push(card
->rx_cmd
.skb
, INTF_HEADER_LEN
);
1031 if ((ep
== card
->rx_cmd_ep
) &&
1032 (!atomic_read(&card
->rx_cmd_urb_pending
)))
1033 mwifiex_usb_submit_rx_urb(&card
->rx_cmd
,
1034 MWIFIEX_RX_CMD_BUF_SIZE
);
1039 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter
*adapter
,
1040 struct sk_buff
*skb
)
1042 mwifiex_submit_rx_urb(adapter
, MWIFIEX_USB_EP_CMD_EVENT
);
1047 /* This function wakes up the card. */
1048 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter
*adapter
)
1050 /* Simulation of HS_AWAKE event */
1051 adapter
->pm_wakeup_fw_try
= false;
1052 del_timer(&adapter
->wakeup_timer
);
1053 adapter
->pm_wakeup_card_req
= false;
1054 adapter
->ps_state
= PS_STATE_AWAKE
;
1059 static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter
*adapter
)
1061 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
1063 struct urb_context
*ctx
;
1065 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++) {
1066 if (card
->rx_data_list
[i
].skb
)
1068 ctx
= &card
->rx_data_list
[i
];
1069 mwifiex_usb_submit_rx_urb(ctx
, MWIFIEX_RX_DATA_BUF_SIZE
);
1073 /* This function is called after the card has woken up. */
1075 mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter
*adapter
)
1080 static struct mwifiex_if_ops usb_ops
= {
1081 .register_dev
= mwifiex_register_dev
,
1082 .unregister_dev
= mwifiex_unregister_dev
,
1083 .wakeup
= mwifiex_pm_wakeup_card
,
1084 .wakeup_complete
= mwifiex_pm_wakeup_card_complete
,
1087 .dnld_fw
= mwifiex_usb_dnld_fw
,
1088 .cmdrsp_complete
= mwifiex_usb_cmd_event_complete
,
1089 .event_complete
= mwifiex_usb_cmd_event_complete
,
1090 .host_to_card
= mwifiex_usb_host_to_card
,
1091 .submit_rem_rx_urbs
= mwifiex_usb_submit_rem_rx_urbs
,
1094 /* This function initializes the USB driver module.
1096 * This initiates the semaphore and registers the device with
1099 static int mwifiex_usb_init_module(void)
1103 pr_debug("Marvell USB8797 Driver\n");
1105 sema_init(&add_remove_card_sem
, 1);
1107 ret
= usb_register(&mwifiex_usb_driver
);
1109 pr_err("Driver register failed!\n");
1111 pr_debug("info: Driver registered successfully!\n");
1116 /* This function cleans up the USB driver.
1118 * The following major steps are followed in .disconnect for cleanup:
1119 * - Resume the device if its suspended
1120 * - Disconnect the device if connected
1121 * - Shutdown the firmware
1122 * - Unregister the device from USB bus.
1124 static void mwifiex_usb_cleanup_module(void)
1126 if (!down_interruptible(&add_remove_card_sem
))
1127 up(&add_remove_card_sem
);
1129 /* set the flag as user is removing this module */
1132 usb_deregister(&mwifiex_usb_driver
);
1135 module_init(mwifiex_usb_init_module
);
1136 module_exit(mwifiex_usb_cleanup_module
);
1138 MODULE_AUTHOR("Marvell International Ltd.");
1139 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION
);
1140 MODULE_VERSION(USB_VERSION
);
1141 MODULE_LICENSE("GPL v2");
1142 MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME
);
1143 MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME
);
1144 MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME
);
1145 MODULE_FIRMWARE(USB8897_DEFAULT_FW_NAME
);
1146 MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME
);