2 * This file contains functions used in USB interface module.
4 #include <linux/delay.h>
5 #include <linux/moduleparam.h>
6 #include <linux/firmware.h>
7 #include <linux/netdevice.h>
8 #include <linux/list.h>
11 #define DRV_NAME "usb8xxx"
19 #define MESSAGE_HEADER_LEN 4
21 static const char usbdriver_name
[] = "usb8xxx";
22 static u8
*default_fw_name
= "usb8388.bin";
24 static char *libertas_fw_name
= NULL
;
25 module_param_named(fw_name
, libertas_fw_name
, charp
, 0644);
28 * We need to send a RESET command to all USB devices before
29 * we tear down the USB connection. Otherwise we would not
30 * be able to re-init device the device if the module gets
31 * loaded again. This is a list of all initialized USB devices,
32 * for the reset code see if_usb_reset_device()
34 static LIST_HEAD(usb_devices
);
36 static struct usb_device_id if_usb_table
[] = {
37 /* Enter the device signature inside */
38 { USB_DEVICE(0x1286, 0x2001) },
39 { USB_DEVICE(0x05a3, 0x8388) },
40 {} /* Terminating entry */
43 MODULE_DEVICE_TABLE(usb
, if_usb_table
);
45 static void if_usb_receive(struct urb
*urb
);
46 static void if_usb_receive_fwload(struct urb
*urb
);
47 static int if_usb_prog_firmware(struct usb_card_rec
*cardp
);
48 static int if_usb_host_to_card(wlan_private
* priv
, u8 type
, u8
* payload
, u16 nb
);
49 static int if_usb_get_int_status(wlan_private
* priv
, u8
*);
50 static int if_usb_read_event_cause(wlan_private
*);
51 static int usb_tx_block(struct usb_card_rec
*cardp
, u8
*payload
, u16 nb
);
52 static void if_usb_free(struct usb_card_rec
*cardp
);
53 static int if_usb_submit_rx_urb(struct usb_card_rec
*cardp
);
54 static int if_usb_reset_device(struct usb_card_rec
*cardp
);
57 * @brief call back function to handle the status of the URB
58 * @param urb pointer to urb structure
61 static void if_usb_write_bulk_callback(struct urb
*urb
)
63 struct usb_card_rec
*cardp
= (struct usb_card_rec
*) urb
->context
;
65 /* handle the transmission complete validations */
67 if (urb
->status
== 0) {
68 wlan_private
*priv
= cardp
->priv
;
71 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
72 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
76 /* Used for both firmware TX and regular TX. priv isn't
77 * valid at firmware load time.
80 wlan_adapter
*adapter
= priv
->adapter
;
81 struct net_device
*dev
= priv
->dev
;
83 priv
->dnld_sent
= DNLD_RES_RECEIVED
;
85 /* Wake main thread if commands are pending */
86 if (!adapter
->cur_cmd
)
87 wake_up_interruptible(&priv
->waitq
);
89 if ((adapter
->connect_status
== LIBERTAS_CONNECTED
)) {
90 netif_wake_queue(dev
);
91 netif_wake_queue(priv
->mesh_dev
);
95 /* print the failure status number for debug */
96 lbs_pr_info("URB in failure status: %d\n", urb
->status
);
103 * @brief free tx/rx urb, skb and rx buffer
104 * @param cardp pointer usb_card_rec
107 static void if_usb_free(struct usb_card_rec
*cardp
)
109 lbs_deb_enter(LBS_DEB_USB
);
111 /* Unlink tx & rx urb */
112 usb_kill_urb(cardp
->tx_urb
);
113 usb_kill_urb(cardp
->rx_urb
);
115 usb_free_urb(cardp
->tx_urb
);
116 cardp
->tx_urb
= NULL
;
118 usb_free_urb(cardp
->rx_urb
);
119 cardp
->rx_urb
= NULL
;
121 kfree(cardp
->bulk_out_buffer
);
122 cardp
->bulk_out_buffer
= NULL
;
124 lbs_deb_leave(LBS_DEB_USB
);
128 * @brief sets the configuration values
129 * @param ifnum interface number
130 * @param id pointer to usb_device_id
131 * @return 0 on success, error code on failure
133 static int if_usb_probe(struct usb_interface
*intf
,
134 const struct usb_device_id
*id
)
136 struct usb_device
*udev
;
137 struct usb_host_interface
*iface_desc
;
138 struct usb_endpoint_descriptor
*endpoint
;
140 struct usb_card_rec
*cardp
;
143 udev
= interface_to_usbdev(intf
);
145 cardp
= kzalloc(sizeof(struct usb_card_rec
), GFP_KERNEL
);
147 lbs_pr_err("Out of memory allocating private data.\n");
152 iface_desc
= intf
->cur_altsetting
;
154 lbs_deb_usbd(&udev
->dev
, "bcdUSB = 0x%X bDeviceClass = 0x%X"
155 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
156 le16_to_cpu(udev
->descriptor
.bcdUSB
),
157 udev
->descriptor
.bDeviceClass
,
158 udev
->descriptor
.bDeviceSubClass
,
159 udev
->descriptor
.bDeviceProtocol
);
161 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
162 endpoint
= &iface_desc
->endpoint
[i
].desc
;
163 if ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
164 && ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) ==
165 USB_ENDPOINT_XFER_BULK
)) {
166 /* we found a bulk in endpoint */
167 lbs_deb_usbd(&udev
->dev
, "Bulk in size is %d\n",
168 le16_to_cpu(endpoint
->wMaxPacketSize
));
169 if (!(cardp
->rx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
170 lbs_deb_usbd(&udev
->dev
,
171 "Rx URB allocation failed\n");
174 cardp
->rx_urb_recall
= 0;
176 cardp
->bulk_in_size
=
177 le16_to_cpu(endpoint
->wMaxPacketSize
);
178 cardp
->bulk_in_endpointAddr
=
180 bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
181 lbs_deb_usbd(&udev
->dev
, "in_endpoint = %d\n",
182 endpoint
->bEndpointAddress
);
186 bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) ==
188 && ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) ==
189 USB_ENDPOINT_XFER_BULK
)) {
190 /* We found bulk out endpoint */
191 if (!(cardp
->tx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
192 lbs_deb_usbd(&udev
->dev
,
193 "Tx URB allocation failed\n");
197 cardp
->bulk_out_size
=
198 le16_to_cpu(endpoint
->wMaxPacketSize
);
199 lbs_deb_usbd(&udev
->dev
,
200 "Bulk out size is %d\n",
201 le16_to_cpu(endpoint
->wMaxPacketSize
));
202 cardp
->bulk_out_endpointAddr
=
203 endpoint
->bEndpointAddress
;
204 lbs_deb_usbd(&udev
->dev
, "out_endpoint = %d\n",
205 endpoint
->bEndpointAddress
);
206 cardp
->bulk_out_buffer
=
207 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE
,
210 if (!cardp
->bulk_out_buffer
) {
211 lbs_deb_usbd(&udev
->dev
,
212 "Could not allocate buffer\n");
218 /* Upload firmware */
219 cardp
->rinfo
.cardp
= cardp
;
220 if (if_usb_prog_firmware(cardp
)) {
221 lbs_deb_usbd(&udev
->dev
, "FW upload failed");
222 goto err_prog_firmware
;
225 if (!(priv
= libertas_add_card(cardp
, &udev
->dev
)))
226 goto err_prog_firmware
;
230 if (libertas_add_mesh(priv
, &udev
->dev
))
233 cardp
->eth_dev
= priv
->dev
;
235 priv
->hw_host_to_card
= if_usb_host_to_card
;
236 priv
->hw_get_int_status
= if_usb_get_int_status
;
237 priv
->hw_read_event_cause
= if_usb_read_event_cause
;
238 priv
->boot2_version
= udev
->descriptor
.bcdDevice
;
240 /* Delay 200 ms to waiting for the FW ready */
241 if_usb_submit_rx_urb(cardp
);
242 msleep_interruptible(200);
243 priv
->adapter
->fw_ready
= 1;
245 if (libertas_start_card(priv
))
248 list_add_tail(&cardp
->list
, &usb_devices
);
251 usb_set_intfdata(intf
, cardp
);
256 libertas_remove_mesh(priv
);
258 libertas_remove_card(priv
);
260 if_usb_reset_device(cardp
);
269 * @brief free resource and cleanup
270 * @param intf USB interface structure
273 static void if_usb_disconnect(struct usb_interface
*intf
)
275 struct usb_card_rec
*cardp
= usb_get_intfdata(intf
);
276 wlan_private
*priv
= (wlan_private
*) cardp
->priv
;
278 lbs_deb_enter(LBS_DEB_MAIN
);
280 /* Update Surprise removed to TRUE */
281 cardp
->surprise_removed
= 1;
283 list_del(&cardp
->list
);
286 wlan_adapter
*adapter
= priv
->adapter
;
288 adapter
->surpriseremoved
= 1;
289 libertas_stop_card(priv
);
290 libertas_remove_mesh(priv
);
291 libertas_remove_card(priv
);
294 /* Unlink and free urb */
297 usb_set_intfdata(intf
, NULL
);
298 usb_put_dev(interface_to_usbdev(intf
));
300 lbs_deb_leave(LBS_DEB_MAIN
);
304 * @brief This function download FW
305 * @param priv pointer to wlan_private
308 static int if_prog_firmware(struct usb_card_rec
*cardp
)
310 struct FWData
*fwdata
;
311 struct fwheader
*fwheader
;
312 u8
*firmware
= cardp
->fw
->data
;
314 fwdata
= kmalloc(sizeof(struct FWData
), GFP_ATOMIC
);
319 fwheader
= &fwdata
->fwheader
;
321 if (!cardp
->CRC_OK
) {
322 cardp
->totalbytes
= cardp
->fwlastblksent
;
323 cardp
->fwseqnum
= cardp
->lastseqnum
- 1;
327 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
331 memcpy(fwheader
, &firmware
[cardp
->totalbytes
],
332 sizeof(struct fwheader
));
334 cardp
->fwlastblksent
= cardp
->totalbytes
;
335 cardp
->totalbytes
+= sizeof(struct fwheader
);
337 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
338 memcpy(fwdata
->data
, &firmware
[cardp
->totalbytes
],
339 le32_to_cpu(fwdata
->fwheader
.datalength
));
342 lbs_deb_usbd(&cardp->udev->dev,
343 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
346 cardp
->fwseqnum
= cardp
->fwseqnum
+ 1;
348 fwdata
->seqnum
= cpu_to_le32(cardp
->fwseqnum
);
349 cardp
->lastseqnum
= cardp
->fwseqnum
;
350 cardp
->totalbytes
+= le32_to_cpu(fwdata
->fwheader
.datalength
);
352 if (fwheader
->dnldcmd
== cpu_to_le32(FW_HAS_DATA_TO_RECV
)) {
354 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
355 lbs_deb_usbd(&cardp->udev->dev,
356 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
359 memcpy(cardp
->bulk_out_buffer
, fwheader
, FW_DATA_XMIT_SIZE
);
360 usb_tx_block(cardp
, cardp
->bulk_out_buffer
, FW_DATA_XMIT_SIZE
);
362 } else if (fwdata
->fwheader
.dnldcmd
== cpu_to_le32(FW_HAS_LAST_BLOCK
)) {
364 lbs_deb_usbd(&cardp->udev->dev,
365 "Host has finished FW downloading\n");
366 lbs_deb_usbd(&cardp->udev->dev,
367 "Donwloading FW JUMP BLOCK\n");
369 memcpy(cardp
->bulk_out_buffer
, fwheader
, FW_DATA_XMIT_SIZE
);
370 usb_tx_block(cardp
, cardp
->bulk_out_buffer
, FW_DATA_XMIT_SIZE
);
371 cardp
->fwfinalblk
= 1;
375 lbs_deb_usbd(&cardp->udev->dev,
376 "The firmware download is done size is %d\n",
385 static int if_usb_reset_device(struct usb_card_rec
*cardp
)
388 wlan_private
* priv
= cardp
->priv
;
390 lbs_deb_enter(LBS_DEB_USB
);
392 /* Try a USB port reset first, if that fails send the reset
393 * command to the firmware.
395 ret
= usb_reset_device(cardp
->udev
);
398 ret
= libertas_reset_device(priv
);
402 lbs_deb_leave_args(LBS_DEB_USB
, "ret %d", ret
);
408 * @brief This function transfer the data to the device.
409 * @param priv pointer to wlan_private
410 * @param payload pointer to payload data
411 * @param nb data length
414 static int usb_tx_block(struct usb_card_rec
*cardp
, u8
* payload
, u16 nb
)
418 /* check if device is removed */
419 if (cardp
->surprise_removed
) {
420 lbs_deb_usbd(&cardp
->udev
->dev
, "Device removed\n");
424 usb_fill_bulk_urb(cardp
->tx_urb
, cardp
->udev
,
425 usb_sndbulkpipe(cardp
->udev
,
426 cardp
->bulk_out_endpointAddr
),
427 payload
, nb
, if_usb_write_bulk_callback
, cardp
);
429 cardp
->tx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
431 if ((ret
= usb_submit_urb(cardp
->tx_urb
, GFP_ATOMIC
))) {
432 /* transfer failed */
433 lbs_deb_usbd(&cardp
->udev
->dev
, "usb_submit_urb failed\n");
436 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
444 static int __if_usb_submit_rx_urb(struct usb_card_rec
*cardp
,
445 void (*callbackfn
)(struct urb
*urb
))
448 struct read_cb_info
*rinfo
= &cardp
->rinfo
;
451 if (!(skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
))) {
452 lbs_pr_err("No free skb\n");
458 /* Fill the receive configuration URB and initialise the Rx call back */
459 usb_fill_bulk_urb(cardp
->rx_urb
, cardp
->udev
,
460 usb_rcvbulkpipe(cardp
->udev
,
461 cardp
->bulk_in_endpointAddr
),
462 (void *) (skb
->tail
+ (size_t) IPFIELD_ALIGN_OFFSET
),
463 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
, callbackfn
,
466 cardp
->rx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
468 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
469 if ((ret
= usb_submit_urb(cardp
->rx_urb
, GFP_ATOMIC
))) {
470 /* handle failure conditions */
471 lbs_deb_usbd(&cardp
->udev
->dev
, "Submit Rx URB failed\n");
474 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
482 static int if_usb_submit_rx_urb_fwload(struct usb_card_rec
*cardp
)
484 return __if_usb_submit_rx_urb(cardp
, &if_usb_receive_fwload
);
487 static int if_usb_submit_rx_urb(struct usb_card_rec
*cardp
)
489 return __if_usb_submit_rx_urb(cardp
, &if_usb_receive
);
492 static void if_usb_receive_fwload(struct urb
*urb
)
494 struct read_cb_info
*rinfo
= (struct read_cb_info
*)urb
->context
;
495 struct sk_buff
*skb
= rinfo
->skb
;
496 struct usb_card_rec
*cardp
= (struct usb_card_rec
*)rinfo
->cardp
;
497 struct fwsyncheader
*syncfwheader
;
498 struct bootcmdrespStr bootcmdresp
;
501 lbs_deb_usbd(&cardp
->udev
->dev
,
502 "URB status is failed during fw load\n");
507 if (cardp
->bootcmdresp
== 0) {
508 memcpy (&bootcmdresp
, skb
->data
+ IPFIELD_ALIGN_OFFSET
,
509 sizeof(bootcmdresp
));
510 if (le16_to_cpu(cardp
->udev
->descriptor
.bcdDevice
) < 0x3106) {
512 if_usb_submit_rx_urb_fwload(cardp
);
513 cardp
->bootcmdresp
= 1;
514 lbs_deb_usbd(&cardp
->udev
->dev
,
515 "Received valid boot command response\n");
518 if (bootcmdresp
.u32magicnumber
!= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
)) {
520 "boot cmd response wrong magic number (0x%x)\n",
521 le32_to_cpu(bootcmdresp
.u32magicnumber
));
522 } else if (bootcmdresp
.u8cmd_tag
!= BOOT_CMD_FW_BY_USB
) {
524 "boot cmd response cmd_tag error (%d)\n",
525 bootcmdresp
.u8cmd_tag
);
526 } else if (bootcmdresp
.u8result
!= BOOT_CMD_RESP_OK
) {
528 "boot cmd response result error (%d)\n",
529 bootcmdresp
.u8result
);
531 cardp
->bootcmdresp
= 1;
532 lbs_deb_usbd(&cardp
->udev
->dev
,
533 "Received valid boot command response\n");
536 if_usb_submit_rx_urb_fwload(cardp
);
540 syncfwheader
= kmalloc(sizeof(struct fwsyncheader
), GFP_ATOMIC
);
542 lbs_deb_usbd(&cardp
->udev
->dev
, "Failure to allocate syncfwheader\n");
547 memcpy(syncfwheader
, skb
->data
+ IPFIELD_ALIGN_OFFSET
,
548 sizeof(struct fwsyncheader
));
550 if (!syncfwheader
->cmd
) {
552 lbs_deb_usbd(&cardp->udev->dev,
553 "FW received Blk with correct CRC\n");
554 lbs_deb_usbd(&cardp->udev->dev,
555 "FW received Blk seqnum = %d\n",
556 syncfwheader->seqnum);
560 lbs_deb_usbd(&cardp
->udev
->dev
,
561 "FW received Blk with CRC error\n");
567 if (cardp
->fwfinalblk
) {
568 cardp
->fwdnldover
= 1;
572 if_prog_firmware(cardp
);
574 if_usb_submit_rx_urb_fwload(cardp
);
582 #define MRVDRV_MIN_PKT_LEN 30
584 static inline void process_cmdtypedata(int recvlength
, struct sk_buff
*skb
,
585 struct usb_card_rec
*cardp
,
588 if (recvlength
> MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
+
589 MESSAGE_HEADER_LEN
|| recvlength
< MRVDRV_MIN_PKT_LEN
) {
590 lbs_deb_usbd(&cardp
->udev
->dev
,
591 "Packet length is Invalid\n");
596 skb_reserve(skb
, IPFIELD_ALIGN_OFFSET
);
597 skb_put(skb
, recvlength
);
598 skb_pull(skb
, MESSAGE_HEADER_LEN
);
599 libertas_process_rxed_packet(priv
, skb
);
600 priv
->upld_len
= (recvlength
- MESSAGE_HEADER_LEN
);
603 static inline void process_cmdrequest(int recvlength
, u8
*recvbuff
,
605 struct usb_card_rec
*cardp
,
609 if (recvlength
> MRVDRV_SIZE_OF_CMD_BUFFER
) {
610 lbs_deb_usbd(&cardp
->udev
->dev
,
611 "The receive buffer is too large\n");
619 spin_lock(&priv
->adapter
->driver_lock
);
620 /* take care of cur_cmd = NULL case by reading the
621 * data to clear the interrupt */
622 if (!priv
->adapter
->cur_cmd
) {
623 cmdbuf
= priv
->upld_buf
;
624 priv
->adapter
->hisregcpy
&= ~MRVDRV_CMD_UPLD_RDY
;
626 cmdbuf
= priv
->adapter
->cur_cmd
->bufvirtualaddr
;
628 cardp
->usb_int_cause
|= MRVDRV_CMD_UPLD_RDY
;
629 priv
->upld_len
= (recvlength
- MESSAGE_HEADER_LEN
);
630 memcpy(cmdbuf
, recvbuff
+ MESSAGE_HEADER_LEN
,
634 libertas_interrupt(priv
->dev
);
635 spin_unlock(&priv
->adapter
->driver_lock
);
637 lbs_deb_usbd(&cardp
->udev
->dev
,
638 "Wake up main thread to handle cmd response\n");
644 * @brief This function reads of the packet into the upload buff,
645 * wake up the main thread and initialise the Rx callack.
647 * @param urb pointer to struct urb
650 static void if_usb_receive(struct urb
*urb
)
652 struct read_cb_info
*rinfo
= (struct read_cb_info
*)urb
->context
;
653 struct sk_buff
*skb
= rinfo
->skb
;
654 struct usb_card_rec
*cardp
= (struct usb_card_rec
*) rinfo
->cardp
;
655 wlan_private
* priv
= cardp
->priv
;
657 int recvlength
= urb
->actual_length
;
661 lbs_deb_enter(LBS_DEB_USB
);
667 lbs_deb_usbd(&cardp
->udev
->dev
,
668 "URB status is failed\n");
673 recvbuff
= skb
->data
+ IPFIELD_ALIGN_OFFSET
;
674 memcpy(&tmp
, recvbuff
, sizeof(u32
));
675 recvtype
= le32_to_cpu(tmp
);
676 lbs_deb_usbd(&cardp
->udev
->dev
,
677 "Recv length = 0x%x, Recv type = 0x%X\n",
678 recvlength
, recvtype
);
679 } else if (urb
->status
)
684 process_cmdtypedata(recvlength
, skb
, cardp
, priv
);
687 case CMD_TYPE_REQUEST
:
688 process_cmdrequest(recvlength
, recvbuff
, skb
, cardp
, priv
);
691 case CMD_TYPE_INDICATION
:
692 /* Event cause handling */
693 spin_lock(&priv
->adapter
->driver_lock
);
694 cardp
->usb_event_cause
= le32_to_cpu(*(__le32
*) (recvbuff
+ MESSAGE_HEADER_LEN
));
695 lbs_deb_usbd(&cardp
->udev
->dev
,"**EVENT** 0x%X\n",
696 cardp
->usb_event_cause
);
697 if (cardp
->usb_event_cause
& 0xffff0000) {
698 libertas_send_tx_feedback(priv
);
699 spin_unlock(&priv
->adapter
->driver_lock
);
702 cardp
->usb_event_cause
<<= 3;
703 cardp
->usb_int_cause
|= MRVDRV_CARDEVENT
;
705 libertas_interrupt(priv
->dev
);
706 spin_unlock(&priv
->adapter
->driver_lock
);
709 lbs_deb_usbd(&cardp
->udev
->dev
, "Unknown command type 0x%X\n",
716 if_usb_submit_rx_urb(cardp
);
718 lbs_deb_leave(LBS_DEB_USB
);
722 * @brief This function downloads data to FW
723 * @param priv pointer to wlan_private structure
724 * @param type type of data
725 * @param buf pointer to data buffer
726 * @param len number of bytes
729 static int if_usb_host_to_card(wlan_private
* priv
, u8 type
, u8
* payload
, u16 nb
)
731 struct usb_card_rec
*cardp
= (struct usb_card_rec
*)priv
->card
;
733 lbs_deb_usbd(&cardp
->udev
->dev
,"*** type = %u\n", type
);
734 lbs_deb_usbd(&cardp
->udev
->dev
,"size after = %d\n", nb
);
736 if (type
== MVMS_CMD
) {
737 __le32 tmp
= cpu_to_le32(CMD_TYPE_REQUEST
);
738 priv
->dnld_sent
= DNLD_CMD_SENT
;
739 memcpy(cardp
->bulk_out_buffer
, (u8
*) & tmp
,
743 __le32 tmp
= cpu_to_le32(CMD_TYPE_DATA
);
744 priv
->dnld_sent
= DNLD_DATA_SENT
;
745 memcpy(cardp
->bulk_out_buffer
, (u8
*) & tmp
,
749 memcpy((cardp
->bulk_out_buffer
+ MESSAGE_HEADER_LEN
), payload
, nb
);
751 return usb_tx_block(cardp
, cardp
->bulk_out_buffer
,
752 nb
+ MESSAGE_HEADER_LEN
);
755 /* called with adapter->driver_lock held */
756 static int if_usb_get_int_status(wlan_private
* priv
, u8
* ireg
)
758 struct usb_card_rec
*cardp
= priv
->card
;
760 *ireg
= cardp
->usb_int_cause
;
761 cardp
->usb_int_cause
= 0;
763 lbs_deb_usbd(&cardp
->udev
->dev
,"Int cause is 0x%X\n", *ireg
);
768 static int if_usb_read_event_cause(wlan_private
* priv
)
770 struct usb_card_rec
*cardp
= priv
->card
;
772 priv
->adapter
->eventcause
= cardp
->usb_event_cause
;
773 /* Re-submit rx urb here to avoid event lost issue */
774 if_usb_submit_rx_urb(cardp
);
779 * @brief This function issues Boot command to the Boot2 code
780 * @param ivalue 1:Boot from FW by USB-Download
781 * 2:Boot from FW in EEPROM
784 static int if_usb_issue_boot_command(struct usb_card_rec
*cardp
, int ivalue
)
786 struct bootcmdstr sbootcmd
;
789 /* Prepare command */
790 sbootcmd
.u32magicnumber
= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
);
791 sbootcmd
.u8cmd_tag
= ivalue
;
793 sbootcmd
.au8dumy
[i
]=0x00;
794 memcpy(cardp
->bulk_out_buffer
, &sbootcmd
, sizeof(struct bootcmdstr
));
797 usb_tx_block(cardp
, cardp
->bulk_out_buffer
, sizeof(struct bootcmdstr
));
804 * @brief This function checks the validity of Boot2/FW image.
806 * @param data pointer to image
810 static int check_fwfile_format(u8
*data
, u32 totlen
)
813 u32 blksize
, offset
, len
;
820 struct fwheader
*fwh
= (void *)data
;
822 bincmd
= le32_to_cpu(fwh
->dnldcmd
);
823 blksize
= le32_to_cpu(fwh
->datalength
);
825 case FW_HAS_DATA_TO_RECV
:
826 offset
= sizeof(struct fwheader
) + blksize
;
832 case FW_HAS_LAST_BLOCK
:
843 lbs_pr_err("firmware file format check FAIL\n");
845 lbs_deb_fw("firmware file format check PASS\n");
851 static int if_usb_prog_firmware(struct usb_card_rec
*cardp
)
854 static int reset_count
= 10;
857 lbs_deb_enter(LBS_DEB_USB
);
859 if ((ret
= request_firmware(&cardp
->fw
, libertas_fw_name
,
860 &cardp
->udev
->dev
)) < 0) {
861 lbs_pr_err("request_firmware() failed with %#x\n", ret
);
862 lbs_pr_err("firmware %s not found\n", libertas_fw_name
);
866 if (check_fwfile_format(cardp
->fw
->data
, cardp
->fw
->size
))
870 if (if_usb_submit_rx_urb_fwload(cardp
) < 0) {
871 lbs_deb_usbd(&cardp
->udev
->dev
, "URB submission is failed\n");
876 cardp
->bootcmdresp
= 0;
880 /* Issue Boot command = 1, Boot from Download-FW */
881 if_usb_issue_boot_command(cardp
, BOOT_CMD_FW_BY_USB
);
882 /* wait for command response */
885 msleep_interruptible(100);
886 } while (cardp
->bootcmdresp
== 0 && j
< 10);
887 } while (cardp
->bootcmdresp
== 0 && i
< 5);
889 if (cardp
->bootcmdresp
== 0) {
890 if (--reset_count
>= 0) {
891 if_usb_reset_device(cardp
);
899 cardp
->totalbytes
= 0;
900 cardp
->fwlastblksent
= 0;
902 cardp
->fwdnldover
= 0;
903 cardp
->fwseqnum
= -1;
904 cardp
->totalbytes
= 0;
905 cardp
->fwfinalblk
= 0;
907 if_prog_firmware(cardp
);
910 lbs_deb_usbd(&cardp
->udev
->dev
,"Wlan sched timeout\n");
912 msleep_interruptible(100);
913 if (cardp
->surprise_removed
|| i
>= 20)
915 } while (!cardp
->fwdnldover
);
917 if (!cardp
->fwdnldover
) {
918 lbs_pr_info("failed to load fw, resetting device!\n");
919 if (--reset_count
>= 0) {
920 if_usb_reset_device(cardp
);
924 lbs_pr_info("FW download failure, time = %d ms\n", i
* 100);
930 release_firmware(cardp
->fw
);
934 lbs_deb_leave_args(LBS_DEB_USB
, "ret %d", ret
);
940 static int if_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
942 struct usb_card_rec
*cardp
= usb_get_intfdata(intf
);
943 wlan_private
*priv
= cardp
->priv
;
945 lbs_deb_enter(LBS_DEB_USB
);
947 if (priv
->adapter
->psstate
!= PS_STATE_FULL_POWER
)
950 if (priv
->mesh_dev
&& !priv
->mesh_autostart_enabled
) {
951 /* Mesh autostart must be activated while sleeping
952 * On resume it will go back to the current state
954 struct cmd_ds_mesh_access mesh_access
;
955 memset(&mesh_access
, 0, sizeof(mesh_access
));
956 mesh_access
.data
[0] = cpu_to_le32(1);
957 libertas_prepare_and_send_command(priv
,
959 CMD_ACT_MESH_SET_AUTOSTART_ENABLED
,
960 CMD_OPTION_WAITFORRSP
, 0, (void *)&mesh_access
);
963 netif_device_detach(cardp
->eth_dev
);
964 netif_device_detach(priv
->mesh_dev
);
966 /* Unlink tx & rx urb */
967 usb_kill_urb(cardp
->tx_urb
);
968 usb_kill_urb(cardp
->rx_urb
);
970 cardp
->rx_urb_recall
= 1;
972 lbs_deb_leave(LBS_DEB_USB
);
976 static int if_usb_resume(struct usb_interface
*intf
)
978 struct usb_card_rec
*cardp
= usb_get_intfdata(intf
);
979 wlan_private
*priv
= cardp
->priv
;
981 lbs_deb_enter(LBS_DEB_USB
);
983 cardp
->rx_urb_recall
= 0;
985 if_usb_submit_rx_urb(cardp
->priv
);
987 netif_device_attach(cardp
->eth_dev
);
988 netif_device_attach(priv
->mesh_dev
);
990 if (priv
->mesh_dev
&& !priv
->mesh_autostart_enabled
) {
991 /* Mesh autostart was activated while sleeping
992 * Disable it if appropriate
994 struct cmd_ds_mesh_access mesh_access
;
995 memset(&mesh_access
, 0, sizeof(mesh_access
));
996 mesh_access
.data
[0] = cpu_to_le32(0);
997 libertas_prepare_and_send_command(priv
,
999 CMD_ACT_MESH_SET_AUTOSTART_ENABLED
,
1000 CMD_OPTION_WAITFORRSP
, 0, (void *)&mesh_access
);
1003 lbs_deb_leave(LBS_DEB_USB
);
1007 #define if_usb_suspend NULL
1008 #define if_usb_resume NULL
1011 static struct usb_driver if_usb_driver
= {
1013 .name
= usbdriver_name
,
1014 /* probe function name */
1015 .probe
= if_usb_probe
,
1016 /* disconnect function name */
1017 .disconnect
= if_usb_disconnect
,
1018 /* device signature table */
1019 .id_table
= if_usb_table
,
1020 .suspend
= if_usb_suspend
,
1021 .resume
= if_usb_resume
,
1024 static int if_usb_init_module(void)
1028 lbs_deb_enter(LBS_DEB_MAIN
);
1030 if (libertas_fw_name
== NULL
) {
1031 libertas_fw_name
= default_fw_name
;
1034 ret
= usb_register(&if_usb_driver
);
1036 lbs_deb_leave_args(LBS_DEB_MAIN
, "ret %d", ret
);
1040 static void if_usb_exit_module(void)
1042 struct usb_card_rec
*cardp
, *cardp_temp
;
1044 lbs_deb_enter(LBS_DEB_MAIN
);
1046 list_for_each_entry_safe(cardp
, cardp_temp
, &usb_devices
, list
) {
1047 libertas_prepare_and_send_command(cardp
->priv
, CMD_802_11_RESET
,
1048 CMD_ACT_HALT
, 0, 0, NULL
);
1051 /* API unregisters the driver from USB subsystem */
1052 usb_deregister(&if_usb_driver
);
1054 lbs_deb_leave(LBS_DEB_MAIN
);
1057 module_init(if_usb_init_module
);
1058 module_exit(if_usb_exit_module
);
1060 MODULE_DESCRIPTION("8388 USB WLAN Driver");
1061 MODULE_AUTHOR("Marvell International Ltd.");
1062 MODULE_LICENSE("GPL");