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
, USB8997_PID_1
)},
47 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID
, USB8997_PID_2
,
48 USB_CLASS_VENDOR_SPEC
,
49 USB_SUBCLASS_VENDOR_SPEC
, 0xff)},
50 { } /* Terminating entry */
53 MODULE_DEVICE_TABLE(usb
, mwifiex_usb_table
);
55 static int mwifiex_usb_submit_rx_urb(struct urb_context
*ctx
, int size
);
57 /* This function handles received packet. Necessary action is taken based on
60 static int mwifiex_usb_recv(struct mwifiex_adapter
*adapter
,
61 struct sk_buff
*skb
, u8 ep
)
67 if (adapter
->hs_activated
)
68 mwifiex_process_hs_config(adapter
);
70 if (skb
->len
< INTF_HEADER_LEN
) {
71 mwifiex_dbg(adapter
, ERROR
,
72 "%s: invalid skb->len\n", __func__
);
77 case MWIFIEX_USB_EP_CMD_EVENT
:
78 mwifiex_dbg(adapter
, EVENT
,
79 "%s: EP_CMD_EVENT\n", __func__
);
80 skb_copy_from_linear_data(skb
, &tmp
, INTF_HEADER_LEN
);
81 recv_type
= le32_to_cpu(tmp
);
82 skb_pull(skb
, INTF_HEADER_LEN
);
85 case MWIFIEX_USB_TYPE_CMD
:
86 if (skb
->len
> MWIFIEX_SIZE_OF_CMD_BUFFER
) {
87 mwifiex_dbg(adapter
, ERROR
,
88 "CMD: skb->len too large\n");
90 goto exit_restore_skb
;
91 } else if (!adapter
->curr_cmd
) {
92 mwifiex_dbg(adapter
, WARN
, "CMD: no curr_cmd\n");
93 if (adapter
->ps_state
== PS_STATE_SLEEP_CFM
) {
94 mwifiex_process_sleep_confirm_resp(
98 goto exit_restore_skb
;
101 goto exit_restore_skb
;
104 adapter
->curr_cmd
->resp_skb
= skb
;
105 adapter
->cmd_resp_received
= true;
107 case MWIFIEX_USB_TYPE_EVENT
:
108 if (skb
->len
< sizeof(u32
)) {
109 mwifiex_dbg(adapter
, ERROR
,
110 "EVENT: skb->len too small\n");
112 goto exit_restore_skb
;
114 skb_copy_from_linear_data(skb
, &tmp
, sizeof(u32
));
115 adapter
->event_cause
= le32_to_cpu(tmp
);
116 mwifiex_dbg(adapter
, EVENT
,
117 "event_cause %#x\n", adapter
->event_cause
);
119 if (skb
->len
> MAX_EVENT_SIZE
) {
120 mwifiex_dbg(adapter
, ERROR
,
121 "EVENT: event body too large\n");
123 goto exit_restore_skb
;
126 memcpy(adapter
->event_body
, skb
->data
+
127 MWIFIEX_EVENT_HEADER_LEN
, skb
->len
);
129 adapter
->event_received
= true;
130 adapter
->event_skb
= skb
;
133 mwifiex_dbg(adapter
, ERROR
,
134 "unknown recv_type %#x\n", recv_type
);
138 case MWIFIEX_USB_EP_DATA
:
139 mwifiex_dbg(adapter
, DATA
, "%s: EP_DATA\n", __func__
);
140 if (skb
->len
> MWIFIEX_RX_DATA_BUF_SIZE
) {
141 mwifiex_dbg(adapter
, ERROR
,
142 "DATA: skb->len too large\n");
146 skb_queue_tail(&adapter
->rx_data_q
, skb
);
147 adapter
->data_received
= true;
148 atomic_inc(&adapter
->rx_pending
);
151 mwifiex_dbg(adapter
, ERROR
,
152 "%s: unknown endport %#x\n", __func__
, ep
);
159 /* The buffer will be reused for further cmds/events */
160 skb_push(skb
, INTF_HEADER_LEN
);
165 static void mwifiex_usb_rx_complete(struct urb
*urb
)
167 struct urb_context
*context
= (struct urb_context
*)urb
->context
;
168 struct mwifiex_adapter
*adapter
= context
->adapter
;
169 struct sk_buff
*skb
= context
->skb
;
170 struct usb_card_rec
*card
;
171 int recv_length
= urb
->actual_length
;
174 if (!adapter
|| !adapter
->card
) {
175 pr_err("mwifiex adapter or card structure is not valid\n");
179 card
= (struct usb_card_rec
*)adapter
->card
;
180 if (card
->rx_cmd_ep
== context
->ep
)
181 atomic_dec(&card
->rx_cmd_urb_pending
);
183 atomic_dec(&card
->rx_data_urb_pending
);
186 if (urb
->status
|| (adapter
->surprise_removed
)) {
187 mwifiex_dbg(adapter
, ERROR
,
188 "URB status is failed: %d\n", urb
->status
);
189 /* Do not free skb in case of command ep */
190 if (card
->rx_cmd_ep
!= context
->ep
)
191 dev_kfree_skb_any(skb
);
194 if (skb
->len
> recv_length
)
195 skb_trim(skb
, recv_length
);
197 skb_put(skb
, recv_length
- skb
->len
);
199 status
= mwifiex_usb_recv(adapter
, skb
, context
->ep
);
201 mwifiex_dbg(adapter
, INFO
,
202 "info: recv_length=%d, status=%d\n",
203 recv_length
, status
);
204 if (status
== -EINPROGRESS
) {
205 mwifiex_queue_main_work(adapter
);
207 /* urb for data_ep is re-submitted now;
208 * urb for cmd_ep will be re-submitted in callback
209 * mwifiex_usb_recv_complete
211 if (card
->rx_cmd_ep
== context
->ep
)
215 mwifiex_dbg(adapter
, ERROR
,
216 "received data processing failed!\n");
218 /* Do not free skb in case of command ep */
219 if (card
->rx_cmd_ep
!= context
->ep
)
220 dev_kfree_skb_any(skb
);
222 } else if (urb
->status
) {
223 if (!adapter
->is_suspended
) {
224 mwifiex_dbg(adapter
, FATAL
,
225 "Card is removed: %d\n", urb
->status
);
226 adapter
->surprise_removed
= true;
228 dev_kfree_skb_any(skb
);
231 /* Do not free skb in case of command ep */
232 if (card
->rx_cmd_ep
!= context
->ep
)
233 dev_kfree_skb_any(skb
);
235 /* fall through setup_for_next */
239 if (card
->rx_cmd_ep
== context
->ep
)
240 size
= MWIFIEX_RX_CMD_BUF_SIZE
;
242 size
= MWIFIEX_RX_DATA_BUF_SIZE
;
244 if (card
->rx_cmd_ep
== context
->ep
) {
245 mwifiex_usb_submit_rx_urb(context
, size
);
247 if (atomic_read(&adapter
->rx_pending
) <= HIGH_RX_PENDING
) {
248 mwifiex_usb_submit_rx_urb(context
, size
);
257 static void mwifiex_usb_tx_complete(struct urb
*urb
)
259 struct urb_context
*context
= (struct urb_context
*)(urb
->context
);
260 struct mwifiex_adapter
*adapter
= context
->adapter
;
261 struct usb_card_rec
*card
= adapter
->card
;
262 struct usb_tx_data_port
*port
;
265 mwifiex_dbg(adapter
, INFO
,
266 "%s: status: %d\n", __func__
, urb
->status
);
268 if (context
->ep
== card
->tx_cmd_ep
) {
269 mwifiex_dbg(adapter
, CMD
,
270 "%s: CMD\n", __func__
);
271 atomic_dec(&card
->tx_cmd_urb_pending
);
272 adapter
->cmd_sent
= false;
274 mwifiex_dbg(adapter
, DATA
,
275 "%s: DATA\n", __func__
);
276 for (i
= 0; i
< MWIFIEX_TX_DATA_PORT
; i
++) {
277 port
= &card
->port
[i
];
278 if (context
->ep
== port
->tx_data_ep
) {
279 atomic_dec(&port
->tx_data_urb_pending
);
280 port
->block_status
= false;
284 adapter
->data_sent
= false;
285 mwifiex_write_data_complete(adapter
, context
->skb
, 0,
286 urb
->status
? -1 : 0);
289 if (card
->mc_resync_flag
)
290 mwifiex_multi_chan_resync(adapter
);
292 mwifiex_queue_main_work(adapter
);
297 static int mwifiex_usb_submit_rx_urb(struct urb_context
*ctx
, int size
)
299 struct mwifiex_adapter
*adapter
= ctx
->adapter
;
300 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
302 if (card
->rx_cmd_ep
!= ctx
->ep
) {
303 ctx
->skb
= dev_alloc_skb(size
);
305 mwifiex_dbg(adapter
, ERROR
,
306 "%s: dev_alloc_skb failed\n", __func__
);
311 usb_fill_bulk_urb(ctx
->urb
, card
->udev
,
312 usb_rcvbulkpipe(card
->udev
, ctx
->ep
), ctx
->skb
->data
,
313 size
, mwifiex_usb_rx_complete
, (void *)ctx
);
315 if (card
->rx_cmd_ep
== ctx
->ep
)
316 atomic_inc(&card
->rx_cmd_urb_pending
);
318 atomic_inc(&card
->rx_data_urb_pending
);
320 if (usb_submit_urb(ctx
->urb
, GFP_ATOMIC
)) {
321 mwifiex_dbg(adapter
, ERROR
, "usb_submit_urb failed\n");
322 dev_kfree_skb_any(ctx
->skb
);
325 if (card
->rx_cmd_ep
== ctx
->ep
)
326 atomic_dec(&card
->rx_cmd_urb_pending
);
328 atomic_dec(&card
->rx_data_urb_pending
);
336 static void mwifiex_usb_free(struct usb_card_rec
*card
)
338 struct usb_tx_data_port
*port
;
341 if (atomic_read(&card
->rx_cmd_urb_pending
) && card
->rx_cmd
.urb
)
342 usb_kill_urb(card
->rx_cmd
.urb
);
344 usb_free_urb(card
->rx_cmd
.urb
);
345 card
->rx_cmd
.urb
= NULL
;
347 if (atomic_read(&card
->rx_data_urb_pending
))
348 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++)
349 if (card
->rx_data_list
[i
].urb
)
350 usb_kill_urb(card
->rx_data_list
[i
].urb
);
352 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++) {
353 usb_free_urb(card
->rx_data_list
[i
].urb
);
354 card
->rx_data_list
[i
].urb
= NULL
;
357 for (i
= 0; i
< MWIFIEX_TX_DATA_PORT
; i
++) {
358 port
= &card
->port
[i
];
359 for (j
= 0; j
< MWIFIEX_TX_DATA_URB
; j
++) {
360 usb_free_urb(port
->tx_data_list
[j
].urb
);
361 port
->tx_data_list
[j
].urb
= NULL
;
365 usb_free_urb(card
->tx_cmd
.urb
);
366 card
->tx_cmd
.urb
= NULL
;
371 /* This function probes an mwifiex device and registers it. It allocates
372 * the card structure, initiates the device registration and initialization
373 * procedure by adding a logical interface.
375 static int mwifiex_usb_probe(struct usb_interface
*intf
,
376 const struct usb_device_id
*id
)
378 struct usb_device
*udev
= interface_to_usbdev(intf
);
379 struct usb_host_interface
*iface_desc
= intf
->cur_altsetting
;
380 struct usb_endpoint_descriptor
*epd
;
382 struct usb_card_rec
*card
;
383 u16 id_vendor
, id_product
, bcd_device
, bcd_usb
;
385 card
= kzalloc(sizeof(struct usb_card_rec
), GFP_KERNEL
);
389 id_vendor
= le16_to_cpu(udev
->descriptor
.idVendor
);
390 id_product
= le16_to_cpu(udev
->descriptor
.idProduct
);
391 bcd_device
= le16_to_cpu(udev
->descriptor
.bcdDevice
);
392 bcd_usb
= le16_to_cpu(udev
->descriptor
.bcdUSB
);
393 pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n",
394 id_vendor
, id_product
, bcd_device
);
396 /* PID_1 is used for firmware downloading only */
397 switch (id_product
) {
402 card
->usb_boot_state
= USB8XXX_FW_DNLD
;
408 card
->usb_boot_state
= USB8XXX_FW_READY
;
411 pr_warn("unknown id_product %#x\n", id_product
);
412 card
->usb_boot_state
= USB8XXX_FW_DNLD
;
419 pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
420 udev
->descriptor
.bcdUSB
, udev
->descriptor
.bDeviceClass
,
421 udev
->descriptor
.bDeviceSubClass
,
422 udev
->descriptor
.bDeviceProtocol
);
424 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
425 epd
= &iface_desc
->endpoint
[i
].desc
;
426 if (usb_endpoint_dir_in(epd
) &&
427 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_CMD_EVENT
&&
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_cmd_ep
= usb_endpoint_num(epd
);
433 atomic_set(&card
->rx_cmd_urb_pending
, 0);
435 if (usb_endpoint_dir_in(epd
) &&
436 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_DATA
&&
437 usb_endpoint_xfer_bulk(epd
)) {
438 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
439 le16_to_cpu(epd
->wMaxPacketSize
),
440 epd
->bEndpointAddress
);
441 card
->rx_data_ep
= usb_endpoint_num(epd
);
442 atomic_set(&card
->rx_data_urb_pending
, 0);
444 if (usb_endpoint_dir_out(epd
) &&
445 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_DATA
&&
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
->port
[0].tx_data_ep
= usb_endpoint_num(epd
);
451 atomic_set(&card
->port
[0].tx_data_urb_pending
, 0);
453 if (usb_endpoint_dir_out(epd
) &&
454 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_DATA_CH2
&&
455 usb_endpoint_xfer_bulk(epd
)) {
456 pr_debug("info: bulk OUT chan2:\t"
457 "max pkt size: %d, addr: %d\n",
458 le16_to_cpu(epd
->wMaxPacketSize
),
459 epd
->bEndpointAddress
);
460 card
->port
[1].tx_data_ep
= usb_endpoint_num(epd
);
461 atomic_set(&card
->port
[1].tx_data_urb_pending
, 0);
463 if (usb_endpoint_dir_out(epd
) &&
464 usb_endpoint_num(epd
) == MWIFIEX_USB_EP_CMD_EVENT
&&
465 usb_endpoint_xfer_bulk(epd
)) {
466 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
467 le16_to_cpu(epd
->wMaxPacketSize
),
468 epd
->bEndpointAddress
);
469 card
->tx_cmd_ep
= usb_endpoint_num(epd
);
470 atomic_set(&card
->tx_cmd_urb_pending
, 0);
471 card
->bulk_out_maxpktsize
=
472 le16_to_cpu(epd
->wMaxPacketSize
);
476 usb_set_intfdata(intf
, card
);
478 ret
= mwifiex_add_card(card
, &add_remove_card_sem
, &usb_ops
,
481 pr_err("%s: mwifiex_add_card failed: %d\n", __func__
, ret
);
482 usb_reset_device(udev
);
492 /* Kernel needs to suspend all functions separately. Therefore all
493 * registered functions must have drivers with suspend and resume
494 * methods. Failing that the kernel simply removes the whole card.
496 * If already not suspended, this function allocates and sends a
497 * 'host sleep activate' request to the firmware and turns off the traffic.
499 static int mwifiex_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
501 struct usb_card_rec
*card
= usb_get_intfdata(intf
);
502 struct mwifiex_adapter
*adapter
;
503 struct usb_tx_data_port
*port
;
506 if (!card
|| !card
->adapter
) {
507 pr_err("%s: card or card->adapter is NULL\n", __func__
);
510 adapter
= card
->adapter
;
512 if (unlikely(adapter
->is_suspended
))
513 mwifiex_dbg(adapter
, WARN
,
514 "Device already suspended\n");
516 mwifiex_enable_hs(adapter
);
518 /* 'is_suspended' flag indicates device is suspended.
519 * It must be set here before the usb_kill_urb() calls. Reason
520 * is in the complete handlers, urb->status(= -ENOENT) and
521 * this flag is used in combination to distinguish between a
522 * 'suspended' state and a 'disconnect' one.
524 adapter
->is_suspended
= true;
525 adapter
->hs_enabling
= false;
527 if (atomic_read(&card
->rx_cmd_urb_pending
) && card
->rx_cmd
.urb
)
528 usb_kill_urb(card
->rx_cmd
.urb
);
530 if (atomic_read(&card
->rx_data_urb_pending
))
531 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++)
532 if (card
->rx_data_list
[i
].urb
)
533 usb_kill_urb(card
->rx_data_list
[i
].urb
);
535 for (i
= 0; i
< MWIFIEX_TX_DATA_PORT
; i
++) {
536 port
= &card
->port
[i
];
537 for (j
= 0; j
< MWIFIEX_TX_DATA_URB
; j
++) {
538 if (port
->tx_data_list
[j
].urb
)
539 usb_kill_urb(port
->tx_data_list
[j
].urb
);
543 if (card
->tx_cmd
.urb
)
544 usb_kill_urb(card
->tx_cmd
.urb
);
549 /* Kernel needs to suspend all functions separately. Therefore all
550 * registered functions must have drivers with suspend and resume
551 * methods. Failing that the kernel simply removes the whole card.
553 * If already not resumed, this function turns on the traffic and
554 * sends a 'host sleep cancel' request to the firmware.
556 static int mwifiex_usb_resume(struct usb_interface
*intf
)
558 struct usb_card_rec
*card
= usb_get_intfdata(intf
);
559 struct mwifiex_adapter
*adapter
;
562 if (!card
|| !card
->adapter
) {
563 pr_err("%s: card or card->adapter is NULL\n", __func__
);
566 adapter
= card
->adapter
;
568 if (unlikely(!adapter
->is_suspended
)) {
569 mwifiex_dbg(adapter
, WARN
,
570 "Device already resumed\n");
574 /* Indicate device resumed. The netdev queue will be resumed only
575 * after the urbs have been re-submitted
577 adapter
->is_suspended
= false;
579 if (!atomic_read(&card
->rx_data_urb_pending
))
580 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++)
581 mwifiex_usb_submit_rx_urb(&card
->rx_data_list
[i
],
582 MWIFIEX_RX_DATA_BUF_SIZE
);
584 if (!atomic_read(&card
->rx_cmd_urb_pending
)) {
585 card
->rx_cmd
.skb
= dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE
);
586 if (card
->rx_cmd
.skb
)
587 mwifiex_usb_submit_rx_urb(&card
->rx_cmd
,
588 MWIFIEX_RX_CMD_BUF_SIZE
);
591 /* Disable Host Sleep */
592 if (adapter
->hs_activated
)
593 mwifiex_cancel_hs(mwifiex_get_priv(adapter
,
594 MWIFIEX_BSS_ROLE_ANY
),
600 static void mwifiex_usb_disconnect(struct usb_interface
*intf
)
602 struct usb_card_rec
*card
= usb_get_intfdata(intf
);
603 struct mwifiex_adapter
*adapter
;
605 if (!card
|| !card
->adapter
) {
606 pr_err("%s: card or card->adapter is NULL\n", __func__
);
610 adapter
= card
->adapter
;
611 if (!adapter
->priv_num
)
616 if (adapter
->is_suspended
)
617 mwifiex_usb_resume(intf
);
620 mwifiex_deauthenticate_all(adapter
);
622 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter
,
623 MWIFIEX_BSS_ROLE_ANY
),
624 MWIFIEX_FUNC_SHUTDOWN
);
627 mwifiex_usb_free(card
);
629 mwifiex_dbg(adapter
, FATAL
,
630 "%s: removing card\n", __func__
);
631 mwifiex_remove_card(adapter
, &add_remove_card_sem
);
633 usb_set_intfdata(intf
, NULL
);
634 usb_put_dev(interface_to_usbdev(intf
));
640 static struct usb_driver mwifiex_usb_driver
= {
641 .name
= "mwifiex_usb",
642 .probe
= mwifiex_usb_probe
,
643 .disconnect
= mwifiex_usb_disconnect
,
644 .id_table
= mwifiex_usb_table
,
645 .suspend
= mwifiex_usb_suspend
,
646 .resume
= mwifiex_usb_resume
,
650 static int mwifiex_usb_tx_init(struct mwifiex_adapter
*adapter
)
652 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
653 struct usb_tx_data_port
*port
;
656 card
->tx_cmd
.adapter
= adapter
;
657 card
->tx_cmd
.ep
= card
->tx_cmd_ep
;
659 card
->tx_cmd
.urb
= usb_alloc_urb(0, GFP_KERNEL
);
660 if (!card
->tx_cmd
.urb
) {
661 mwifiex_dbg(adapter
, ERROR
,
662 "tx_cmd.urb allocation failed\n");
666 for (i
= 0; i
< MWIFIEX_TX_DATA_PORT
; i
++) {
667 port
= &card
->port
[i
];
668 if (!port
->tx_data_ep
)
670 port
->tx_data_ix
= 0;
671 if (port
->tx_data_ep
== MWIFIEX_USB_EP_DATA
)
672 port
->block_status
= false;
674 port
->block_status
= true;
675 for (j
= 0; j
< MWIFIEX_TX_DATA_URB
; j
++) {
676 port
->tx_data_list
[j
].adapter
= adapter
;
677 port
->tx_data_list
[j
].ep
= port
->tx_data_ep
;
678 port
->tx_data_list
[j
].urb
=
679 usb_alloc_urb(0, GFP_KERNEL
);
680 if (!port
->tx_data_list
[j
].urb
) {
681 mwifiex_dbg(adapter
, ERROR
,
682 "urb allocation failed\n");
691 static int mwifiex_usb_rx_init(struct mwifiex_adapter
*adapter
)
693 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
696 card
->rx_cmd
.adapter
= adapter
;
697 card
->rx_cmd
.ep
= card
->rx_cmd_ep
;
699 card
->rx_cmd
.urb
= usb_alloc_urb(0, GFP_KERNEL
);
700 if (!card
->rx_cmd
.urb
) {
701 mwifiex_dbg(adapter
, ERROR
, "rx_cmd.urb allocation failed\n");
705 card
->rx_cmd
.skb
= dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE
);
706 if (!card
->rx_cmd
.skb
)
709 if (mwifiex_usb_submit_rx_urb(&card
->rx_cmd
, MWIFIEX_RX_CMD_BUF_SIZE
))
712 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++) {
713 card
->rx_data_list
[i
].adapter
= adapter
;
714 card
->rx_data_list
[i
].ep
= card
->rx_data_ep
;
716 card
->rx_data_list
[i
].urb
= usb_alloc_urb(0, GFP_KERNEL
);
717 if (!card
->rx_data_list
[i
].urb
) {
718 mwifiex_dbg(adapter
, ERROR
,
719 "rx_data_list[] urb allocation failed\n");
722 if (mwifiex_usb_submit_rx_urb(&card
->rx_data_list
[i
],
723 MWIFIEX_RX_DATA_BUF_SIZE
))
730 static int mwifiex_write_data_sync(struct mwifiex_adapter
*adapter
, u8
*pbuf
,
731 u32
*len
, u8 ep
, u32 timeout
)
733 struct usb_card_rec
*card
= adapter
->card
;
734 int actual_length
, ret
;
736 if (!(*len
% card
->bulk_out_maxpktsize
))
739 /* Send the data block */
740 ret
= usb_bulk_msg(card
->udev
, usb_sndbulkpipe(card
->udev
, ep
), pbuf
,
741 *len
, &actual_length
, timeout
);
743 mwifiex_dbg(adapter
, ERROR
,
744 "usb_bulk_msg for tx failed: %d\n", ret
);
748 *len
= actual_length
;
753 static int mwifiex_read_data_sync(struct mwifiex_adapter
*adapter
, u8
*pbuf
,
754 u32
*len
, u8 ep
, u32 timeout
)
756 struct usb_card_rec
*card
= adapter
->card
;
757 int actual_length
, ret
;
759 /* Receive the data response */
760 ret
= usb_bulk_msg(card
->udev
, usb_rcvbulkpipe(card
->udev
, ep
), pbuf
,
761 *len
, &actual_length
, timeout
);
763 mwifiex_dbg(adapter
, ERROR
,
764 "usb_bulk_msg for rx failed: %d\n", ret
);
768 *len
= actual_length
;
773 static void mwifiex_usb_port_resync(struct mwifiex_adapter
*adapter
)
775 struct usb_card_rec
*card
= adapter
->card
;
776 u8 active_port
= MWIFIEX_USB_EP_DATA
;
777 struct mwifiex_private
*priv
= NULL
;
780 if (adapter
->usb_mc_status
) {
781 for (i
= 0; i
< adapter
->priv_num
; i
++) {
782 priv
= adapter
->priv
[i
];
785 if ((priv
->bss_role
== MWIFIEX_BSS_ROLE_UAP
&&
786 !priv
->bss_started
) ||
787 (priv
->bss_role
== MWIFIEX_BSS_ROLE_STA
&&
788 !priv
->media_connected
))
789 priv
->usb_port
= MWIFIEX_USB_EP_DATA
;
791 for (i
= 0; i
< MWIFIEX_TX_DATA_PORT
; i
++)
792 card
->port
[i
].block_status
= false;
794 for (i
= 0; i
< adapter
->priv_num
; i
++) {
795 priv
= adapter
->priv
[i
];
798 if ((priv
->bss_role
== MWIFIEX_BSS_ROLE_UAP
&&
799 priv
->bss_started
) ||
800 (priv
->bss_role
== MWIFIEX_BSS_ROLE_STA
&&
801 priv
->media_connected
)) {
802 active_port
= priv
->usb_port
;
806 for (i
= 0; i
< adapter
->priv_num
; i
++) {
807 priv
= adapter
->priv
[i
];
809 priv
->usb_port
= active_port
;
811 for (i
= 0; i
< MWIFIEX_TX_DATA_PORT
; i
++) {
812 if (active_port
== card
->port
[i
].tx_data_ep
)
813 card
->port
[i
].block_status
= false;
815 card
->port
[i
].block_status
= true;
820 static bool mwifiex_usb_is_port_ready(struct mwifiex_private
*priv
)
822 struct usb_card_rec
*card
= priv
->adapter
->card
;
825 for (idx
= 0; idx
< MWIFIEX_TX_DATA_PORT
; idx
++) {
826 if (priv
->usb_port
== card
->port
[idx
].tx_data_ep
)
827 return !card
->port
[idx
].block_status
;
833 static inline u8
mwifiex_usb_data_sent(struct mwifiex_adapter
*adapter
)
835 struct usb_card_rec
*card
= adapter
->card
;
838 for (i
= 0; i
< MWIFIEX_TX_DATA_PORT
; i
++)
839 if (!card
->port
[i
].block_status
)
845 /* This function write a command/data packet to card. */
846 static int mwifiex_usb_host_to_card(struct mwifiex_adapter
*adapter
, u8 ep
,
848 struct mwifiex_tx_param
*tx_param
)
850 struct usb_card_rec
*card
= adapter
->card
;
851 struct urb_context
*context
= NULL
;
852 struct usb_tx_data_port
*port
= NULL
;
853 u8
*data
= (u8
*)skb
->data
;
857 if (adapter
->is_suspended
) {
858 mwifiex_dbg(adapter
, ERROR
,
859 "%s: not allowed while suspended\n", __func__
);
863 if (adapter
->surprise_removed
) {
864 mwifiex_dbg(adapter
, ERROR
, "%s: device removed\n", __func__
);
868 mwifiex_dbg(adapter
, INFO
, "%s: ep=%d\n", __func__
, ep
);
870 if (ep
== card
->tx_cmd_ep
) {
871 context
= &card
->tx_cmd
;
873 for (idx
= 0; idx
< MWIFIEX_TX_DATA_PORT
; idx
++) {
874 if (ep
== card
->port
[idx
].tx_data_ep
) {
875 port
= &card
->port
[idx
];
876 if (atomic_read(&port
->tx_data_urb_pending
)
877 >= MWIFIEX_TX_DATA_URB
) {
878 port
->block_status
= true;
882 if (port
->tx_data_ix
>= MWIFIEX_TX_DATA_URB
)
883 port
->tx_data_ix
= 0;
885 &port
->tx_data_list
[port
->tx_data_ix
++];
890 mwifiex_dbg(adapter
, ERROR
, "Wrong usb tx data port\n");
895 context
->adapter
= adapter
;
898 tx_urb
= context
->urb
;
900 usb_fill_bulk_urb(tx_urb
, card
->udev
, usb_sndbulkpipe(card
->udev
, ep
),
901 data
, skb
->len
, mwifiex_usb_tx_complete
,
904 tx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
906 if (ep
== card
->tx_cmd_ep
)
907 atomic_inc(&card
->tx_cmd_urb_pending
);
909 atomic_inc(&port
->tx_data_urb_pending
);
911 if (usb_submit_urb(tx_urb
, GFP_ATOMIC
)) {
912 mwifiex_dbg(adapter
, ERROR
,
913 "%s: usb_submit_urb failed\n", __func__
);
914 if (ep
== card
->tx_cmd_ep
) {
915 atomic_dec(&card
->tx_cmd_urb_pending
);
917 atomic_dec(&port
->tx_data_urb_pending
);
918 port
->block_status
= false;
919 if (port
->tx_data_ix
)
922 port
->tx_data_ix
= MWIFIEX_TX_DATA_URB
;
927 if (ep
!= card
->tx_cmd_ep
&&
928 atomic_read(&port
->tx_data_urb_pending
) ==
929 MWIFIEX_TX_DATA_URB
) {
930 port
->block_status
= true;
939 if (ep
!= card
->tx_cmd_ep
)
940 adapter
->data_sent
= mwifiex_usb_data_sent(adapter
);
945 /* This function register usb device and initialize parameter. */
946 static int mwifiex_register_dev(struct mwifiex_adapter
*adapter
)
948 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
950 card
->adapter
= adapter
;
951 adapter
->dev
= &card
->udev
->dev
;
953 switch (le16_to_cpu(card
->udev
->descriptor
.idProduct
)) {
956 adapter
->tx_buf_size
= MWIFIEX_TX_DATA_BUF_SIZE_4K
;
957 strcpy(adapter
->fw_name
, USB8997_DEFAULT_FW_NAME
);
958 adapter
->ext_scan
= true;
962 adapter
->tx_buf_size
= MWIFIEX_TX_DATA_BUF_SIZE_2K
;
963 strcpy(adapter
->fw_name
, USB8766_DEFAULT_FW_NAME
);
964 adapter
->ext_scan
= true;
968 adapter
->tx_buf_size
= MWIFIEX_TX_DATA_BUF_SIZE_2K
;
969 strcpy(adapter
->fw_name
, USB8801_DEFAULT_FW_NAME
);
970 adapter
->ext_scan
= false;
975 adapter
->tx_buf_size
= MWIFIEX_TX_DATA_BUF_SIZE_2K
;
976 strcpy(adapter
->fw_name
, USB8797_DEFAULT_FW_NAME
);
980 adapter
->usb_mc_status
= false;
981 adapter
->usb_mc_setup
= false;
986 static void mwifiex_unregister_dev(struct mwifiex_adapter
*adapter
)
988 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
990 card
->adapter
= NULL
;
993 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter
*adapter
,
994 struct mwifiex_fw_image
*fw
)
997 u8
*firmware
= fw
->fw_buf
, *recv_buff
;
998 u32 retries
= USB8XXX_FW_MAX_RETRY
, dlen
;
999 u32 fw_seqnum
= 0, tlen
= 0, dnld_cmd
= 0;
1000 struct fw_data
*fwdata
;
1001 struct fw_sync_header sync_fw
;
1002 u8 check_winner
= 1;
1005 mwifiex_dbg(adapter
, ERROR
,
1006 "No firmware image found! Terminating download\n");
1011 /* Allocate memory for transmit */
1012 fwdata
= kzalloc(FW_DNLD_TX_BUF_SIZE
, GFP_KERNEL
);
1018 /* Allocate memory for receive */
1019 recv_buff
= kzalloc(FW_DNLD_RX_BUF_SIZE
, GFP_KERNEL
);
1024 /* Send pseudo data to check winner status first */
1026 memset(&fwdata
->fw_hdr
, 0, sizeof(struct fw_header
));
1029 /* copy the header of the fw_data to get the length */
1030 memcpy(&fwdata
->fw_hdr
, &firmware
[tlen
],
1031 sizeof(struct fw_header
));
1033 dlen
= le32_to_cpu(fwdata
->fw_hdr
.data_len
);
1034 dnld_cmd
= le32_to_cpu(fwdata
->fw_hdr
.dnld_cmd
);
1035 tlen
+= sizeof(struct fw_header
);
1037 memcpy(fwdata
->data
, &firmware
[tlen
], dlen
);
1039 fwdata
->seq_num
= cpu_to_le32(fw_seqnum
);
1043 /* If the send/receive fails or CRC occurs then retry */
1045 u8
*buf
= (u8
*)fwdata
;
1046 u32 len
= FW_DATA_XMIT_SIZE
;
1048 /* send the firmware block */
1049 ret
= mwifiex_write_data_sync(adapter
, buf
, &len
,
1050 MWIFIEX_USB_EP_CMD_EVENT
,
1051 MWIFIEX_USB_TIMEOUT
);
1053 mwifiex_dbg(adapter
, ERROR
,
1054 "write_data_sync: failed: %d\n",
1060 len
= FW_DNLD_RX_BUF_SIZE
;
1062 /* Receive the firmware block response */
1063 ret
= mwifiex_read_data_sync(adapter
, buf
, &len
,
1064 MWIFIEX_USB_EP_CMD_EVENT
,
1065 MWIFIEX_USB_TIMEOUT
);
1067 mwifiex_dbg(adapter
, ERROR
,
1068 "read_data_sync: failed: %d\n",
1073 memcpy(&sync_fw
, recv_buff
,
1074 sizeof(struct fw_sync_header
));
1076 /* check 1st firmware block resp for highest bit set */
1078 if (le32_to_cpu(sync_fw
.cmd
) & 0x80000000) {
1079 mwifiex_dbg(adapter
, WARN
,
1080 "USB is not the winner %#x\n",
1083 /* returning success */
1088 mwifiex_dbg(adapter
, MSG
,
1089 "start to download FW...\n");
1095 /* check the firmware block response for CRC errors */
1097 mwifiex_dbg(adapter
, ERROR
,
1098 "FW received block with CRC %#x\n",
1104 retries
= USB8XXX_FW_MAX_RETRY
;
1108 } while ((dnld_cmd
!= FW_HAS_LAST_BLOCK
) && retries
);
1111 mwifiex_dbg(adapter
, MSG
,
1112 "info: FW download over, size %d bytes\n", tlen
);
1123 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter
*adapter
,
1124 struct mwifiex_fw_image
*fw
)
1127 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
1129 if (card
->usb_boot_state
== USB8XXX_FW_DNLD
) {
1130 ret
= mwifiex_prog_fw_w_helper(adapter
, fw
);
1134 /* Boot state changes after successful firmware download */
1135 if (card
->usb_boot_state
== USB8XXX_FW_DNLD
)
1139 ret
= mwifiex_usb_rx_init(adapter
);
1141 ret
= mwifiex_usb_tx_init(adapter
);
1146 static void mwifiex_submit_rx_urb(struct mwifiex_adapter
*adapter
, u8 ep
)
1148 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
1150 skb_push(card
->rx_cmd
.skb
, INTF_HEADER_LEN
);
1151 if ((ep
== card
->rx_cmd_ep
) &&
1152 (!atomic_read(&card
->rx_cmd_urb_pending
)))
1153 mwifiex_usb_submit_rx_urb(&card
->rx_cmd
,
1154 MWIFIEX_RX_CMD_BUF_SIZE
);
1159 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter
*adapter
,
1160 struct sk_buff
*skb
)
1162 mwifiex_submit_rx_urb(adapter
, MWIFIEX_USB_EP_CMD_EVENT
);
1167 /* This function wakes up the card. */
1168 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter
*adapter
)
1170 /* Simulation of HS_AWAKE event */
1171 adapter
->pm_wakeup_fw_try
= false;
1172 del_timer(&adapter
->wakeup_timer
);
1173 adapter
->pm_wakeup_card_req
= false;
1174 adapter
->ps_state
= PS_STATE_AWAKE
;
1179 static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter
*adapter
)
1181 struct usb_card_rec
*card
= (struct usb_card_rec
*)adapter
->card
;
1183 struct urb_context
*ctx
;
1185 for (i
= 0; i
< MWIFIEX_RX_DATA_URB
; i
++) {
1186 if (card
->rx_data_list
[i
].skb
)
1188 ctx
= &card
->rx_data_list
[i
];
1189 mwifiex_usb_submit_rx_urb(ctx
, MWIFIEX_RX_DATA_BUF_SIZE
);
1193 /* This function is called after the card has woken up. */
1195 mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter
*adapter
)
1200 static struct mwifiex_if_ops usb_ops
= {
1201 .register_dev
= mwifiex_register_dev
,
1202 .unregister_dev
= mwifiex_unregister_dev
,
1203 .wakeup
= mwifiex_pm_wakeup_card
,
1204 .wakeup_complete
= mwifiex_pm_wakeup_card_complete
,
1207 .dnld_fw
= mwifiex_usb_dnld_fw
,
1208 .cmdrsp_complete
= mwifiex_usb_cmd_event_complete
,
1209 .event_complete
= mwifiex_usb_cmd_event_complete
,
1210 .host_to_card
= mwifiex_usb_host_to_card
,
1211 .submit_rem_rx_urbs
= mwifiex_usb_submit_rem_rx_urbs
,
1212 .multi_port_resync
= mwifiex_usb_port_resync
,
1213 .is_port_ready
= mwifiex_usb_is_port_ready
,
1216 /* This function initializes the USB driver module.
1218 * This initiates the semaphore and registers the device with
1221 static int mwifiex_usb_init_module(void)
1225 pr_debug("Marvell USB8797 Driver\n");
1227 sema_init(&add_remove_card_sem
, 1);
1229 ret
= usb_register(&mwifiex_usb_driver
);
1231 pr_err("Driver register failed!\n");
1233 pr_debug("info: Driver registered successfully!\n");
1238 /* This function cleans up the USB driver.
1240 * The following major steps are followed in .disconnect for cleanup:
1241 * - Resume the device if its suspended
1242 * - Disconnect the device if connected
1243 * - Shutdown the firmware
1244 * - Unregister the device from USB bus.
1246 static void mwifiex_usb_cleanup_module(void)
1248 if (!down_interruptible(&add_remove_card_sem
))
1249 up(&add_remove_card_sem
);
1251 /* set the flag as user is removing this module */
1254 usb_deregister(&mwifiex_usb_driver
);
1257 module_init(mwifiex_usb_init_module
);
1258 module_exit(mwifiex_usb_cleanup_module
);
1260 MODULE_AUTHOR("Marvell International Ltd.");
1261 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION
);
1262 MODULE_VERSION(USB_VERSION
);
1263 MODULE_LICENSE("GPL v2");
1264 MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME
);
1265 MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME
);
1266 MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME
);
1267 MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME
);