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>
10 #define DRV_NAME "usb8xxx"
18 #define MESSAGE_HEADER_LEN 4
20 static char *lbs_fw_name
= "usb8388.bin";
21 module_param_named(fw_name
, lbs_fw_name
, charp
, 0644);
23 static struct usb_device_id if_usb_table
[] = {
24 /* Enter the device signature inside */
25 { USB_DEVICE(0x1286, 0x2001) },
26 { USB_DEVICE(0x05a3, 0x8388) },
27 {} /* Terminating entry */
30 MODULE_DEVICE_TABLE(usb
, if_usb_table
);
32 static void if_usb_receive(struct urb
*urb
);
33 static void if_usb_receive_fwload(struct urb
*urb
);
34 static int if_usb_prog_firmware(struct usb_card_rec
*cardp
);
35 static int if_usb_host_to_card(struct lbs_private
*priv
,
39 static int if_usb_get_int_status(struct lbs_private
*priv
, u8
*);
40 static int if_usb_read_event_cause(struct lbs_private
*);
41 static int usb_tx_block(struct usb_card_rec
*cardp
, u8
*payload
, u16 nb
);
42 static void if_usb_free(struct usb_card_rec
*cardp
);
43 static int if_usb_submit_rx_urb(struct usb_card_rec
*cardp
);
44 static int if_usb_reset_device(struct usb_card_rec
*cardp
);
47 * @brief call back function to handle the status of the URB
48 * @param urb pointer to urb structure
51 static void if_usb_write_bulk_callback(struct urb
*urb
)
53 struct usb_card_rec
*cardp
= (struct usb_card_rec
*) urb
->context
;
55 /* handle the transmission complete validations */
57 if (urb
->status
== 0) {
58 struct lbs_private
*priv
= cardp
->priv
;
61 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
62 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
66 /* Used for both firmware TX and regular TX. priv isn't
67 * valid at firmware load time.
70 lbs_host_to_card_done(priv
);
72 /* print the failure status number for debug */
73 lbs_pr_info("URB in failure status: %d\n", urb
->status
);
80 * @brief free tx/rx urb, skb and rx buffer
81 * @param cardp pointer usb_card_rec
84 static void if_usb_free(struct usb_card_rec
*cardp
)
86 lbs_deb_enter(LBS_DEB_USB
);
88 /* Unlink tx & rx urb */
89 usb_kill_urb(cardp
->tx_urb
);
90 usb_kill_urb(cardp
->rx_urb
);
92 usb_free_urb(cardp
->tx_urb
);
95 usb_free_urb(cardp
->rx_urb
);
98 kfree(cardp
->bulk_out_buffer
);
99 cardp
->bulk_out_buffer
= NULL
;
101 lbs_deb_leave(LBS_DEB_USB
);
104 static void if_usb_set_boot2_ver(struct lbs_private
*priv
)
106 struct cmd_ds_set_boot2_ver b2_cmd
;
109 b2_cmd
.version
= priv
->boot2_version
;
111 if (lbs_cmd(priv
, CMD_SET_BOOT2_VER
, &b2_cmd
, sizeof(b2_cmd
), NULL
, 0))
112 lbs_deb_usb("Setting boot2 version failed\n");
117 * @brief sets the configuration values
118 * @param ifnum interface number
119 * @param id pointer to usb_device_id
120 * @return 0 on success, error code on failure
122 static int if_usb_probe(struct usb_interface
*intf
,
123 const struct usb_device_id
*id
)
125 struct usb_device
*udev
;
126 struct usb_host_interface
*iface_desc
;
127 struct usb_endpoint_descriptor
*endpoint
;
128 struct lbs_private
*priv
;
129 struct usb_card_rec
*cardp
;
132 udev
= interface_to_usbdev(intf
);
134 cardp
= kzalloc(sizeof(struct usb_card_rec
), GFP_KERNEL
);
136 lbs_pr_err("Out of memory allocating private data.\n");
141 iface_desc
= intf
->cur_altsetting
;
143 lbs_deb_usbd(&udev
->dev
, "bcdUSB = 0x%X bDeviceClass = 0x%X"
144 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
145 le16_to_cpu(udev
->descriptor
.bcdUSB
),
146 udev
->descriptor
.bDeviceClass
,
147 udev
->descriptor
.bDeviceSubClass
,
148 udev
->descriptor
.bDeviceProtocol
);
150 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
151 endpoint
= &iface_desc
->endpoint
[i
].desc
;
152 if ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
153 && ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) ==
154 USB_ENDPOINT_XFER_BULK
)) {
155 /* we found a bulk in endpoint */
156 lbs_deb_usbd(&udev
->dev
, "Bulk in size is %d\n",
157 le16_to_cpu(endpoint
->wMaxPacketSize
));
158 if (!(cardp
->rx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
159 lbs_deb_usbd(&udev
->dev
,
160 "Rx URB allocation failed\n");
163 cardp
->rx_urb_recall
= 0;
165 cardp
->bulk_in_size
=
166 le16_to_cpu(endpoint
->wMaxPacketSize
);
167 cardp
->bulk_in_endpointAddr
=
169 bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
170 lbs_deb_usbd(&udev
->dev
, "in_endpoint = %d\n",
171 endpoint
->bEndpointAddress
);
175 bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) ==
177 && ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) ==
178 USB_ENDPOINT_XFER_BULK
)) {
179 /* We found bulk out endpoint */
180 if (!(cardp
->tx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
181 lbs_deb_usbd(&udev
->dev
,
182 "Tx URB allocation failed\n");
186 cardp
->bulk_out_size
=
187 le16_to_cpu(endpoint
->wMaxPacketSize
);
188 lbs_deb_usbd(&udev
->dev
,
189 "Bulk out size is %d\n",
190 le16_to_cpu(endpoint
->wMaxPacketSize
));
191 cardp
->bulk_out_endpointAddr
=
192 endpoint
->bEndpointAddress
;
193 lbs_deb_usbd(&udev
->dev
, "out_endpoint = %d\n",
194 endpoint
->bEndpointAddress
);
195 cardp
->bulk_out_buffer
=
196 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE
,
199 if (!cardp
->bulk_out_buffer
) {
200 lbs_deb_usbd(&udev
->dev
,
201 "Could not allocate buffer\n");
207 /* Upload firmware */
208 cardp
->rinfo
.cardp
= cardp
;
209 if (if_usb_prog_firmware(cardp
)) {
210 lbs_deb_usbd(&udev
->dev
, "FW upload failed");
211 goto err_prog_firmware
;
214 if (!(priv
= lbs_add_card(cardp
, &udev
->dev
)))
215 goto err_prog_firmware
;
219 if (lbs_add_mesh(priv
, &udev
->dev
))
222 cardp
->eth_dev
= priv
->dev
;
224 priv
->hw_host_to_card
= if_usb_host_to_card
;
225 priv
->hw_get_int_status
= if_usb_get_int_status
;
226 priv
->hw_read_event_cause
= if_usb_read_event_cause
;
227 priv
->boot2_version
= udev
->descriptor
.bcdDevice
;
229 /* Delay 200 ms to waiting for the FW ready */
230 if_usb_submit_rx_urb(cardp
);
231 msleep_interruptible(200);
234 if (lbs_start_card(priv
))
237 if_usb_set_boot2_ver(priv
);
240 usb_set_intfdata(intf
, cardp
);
245 lbs_remove_mesh(priv
);
247 lbs_remove_card(priv
);
249 if_usb_reset_device(cardp
);
258 * @brief free resource and cleanup
259 * @param intf USB interface structure
262 static void if_usb_disconnect(struct usb_interface
*intf
)
264 struct usb_card_rec
*cardp
= usb_get_intfdata(intf
);
265 struct lbs_private
*priv
= (struct lbs_private
*) cardp
->priv
;
267 lbs_deb_enter(LBS_DEB_MAIN
);
269 /* Update Surprise removed to TRUE */
270 cardp
->surprise_removed
= 1;
274 priv
->surpriseremoved
= 1;
276 lbs_remove_mesh(priv
);
277 lbs_remove_card(priv
);
280 /* this is (apparently?) necessary for future usage of the device */
281 lbs_prepare_and_send_command(priv
, CMD_802_11_RESET
, CMD_ACT_HALT
,
284 /* Unlink and free urb */
287 usb_set_intfdata(intf
, NULL
);
288 usb_put_dev(interface_to_usbdev(intf
));
290 lbs_deb_leave(LBS_DEB_MAIN
);
294 * @brief This function download FW
295 * @param priv pointer to struct lbs_private
298 static int if_prog_firmware(struct usb_card_rec
*cardp
)
300 struct FWData
*fwdata
;
301 struct fwheader
*fwheader
;
302 u8
*firmware
= cardp
->fw
->data
;
304 fwdata
= kmalloc(sizeof(struct FWData
), GFP_ATOMIC
);
309 fwheader
= &fwdata
->fwheader
;
311 if (!cardp
->CRC_OK
) {
312 cardp
->totalbytes
= cardp
->fwlastblksent
;
313 cardp
->fwseqnum
= cardp
->lastseqnum
- 1;
317 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
321 memcpy(fwheader
, &firmware
[cardp
->totalbytes
],
322 sizeof(struct fwheader
));
324 cardp
->fwlastblksent
= cardp
->totalbytes
;
325 cardp
->totalbytes
+= sizeof(struct fwheader
);
327 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
328 memcpy(fwdata
->data
, &firmware
[cardp
->totalbytes
],
329 le32_to_cpu(fwdata
->fwheader
.datalength
));
332 lbs_deb_usbd(&cardp->udev->dev,
333 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
336 cardp
->fwseqnum
= cardp
->fwseqnum
+ 1;
338 fwdata
->seqnum
= cpu_to_le32(cardp
->fwseqnum
);
339 cardp
->lastseqnum
= cardp
->fwseqnum
;
340 cardp
->totalbytes
+= le32_to_cpu(fwdata
->fwheader
.datalength
);
342 if (fwheader
->dnldcmd
== cpu_to_le32(FW_HAS_DATA_TO_RECV
)) {
344 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
345 lbs_deb_usbd(&cardp->udev->dev,
346 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
349 memcpy(cardp
->bulk_out_buffer
, fwheader
, FW_DATA_XMIT_SIZE
);
350 usb_tx_block(cardp
, cardp
->bulk_out_buffer
, FW_DATA_XMIT_SIZE
);
352 } else if (fwdata
->fwheader
.dnldcmd
== cpu_to_le32(FW_HAS_LAST_BLOCK
)) {
354 lbs_deb_usbd(&cardp->udev->dev,
355 "Host has finished FW downloading\n");
356 lbs_deb_usbd(&cardp->udev->dev,
357 "Donwloading FW JUMP BLOCK\n");
359 memcpy(cardp
->bulk_out_buffer
, fwheader
, FW_DATA_XMIT_SIZE
);
360 usb_tx_block(cardp
, cardp
->bulk_out_buffer
, FW_DATA_XMIT_SIZE
);
361 cardp
->fwfinalblk
= 1;
365 lbs_deb_usbd(&cardp->udev->dev,
366 "The firmware download is done size is %d\n",
375 static int if_usb_reset_device(struct usb_card_rec
*cardp
)
377 struct cmd_ds_command
*cmd
= (void *)&cardp
->bulk_out_buffer
[4];
380 lbs_deb_enter(LBS_DEB_USB
);
382 *(__le32
*)cardp
->bulk_out_buffer
= cpu_to_le32(CMD_TYPE_REQUEST
);
384 cmd
->command
= cpu_to_le16(CMD_802_11_RESET
);
385 cmd
->size
= cpu_to_le16(sizeof(struct cmd_ds_802_11_reset
) + S_DS_GEN
);
386 cmd
->result
= cpu_to_le16(0);
387 cmd
->seqnum
= cpu_to_le16(0x5a5a);
388 cmd
->params
.reset
.action
= cpu_to_le16(CMD_ACT_HALT
);
389 usb_tx_block(cardp
, cardp
->bulk_out_buffer
, 4 + S_DS_GEN
+ sizeof(struct cmd_ds_802_11_reset
));
392 ret
= usb_reset_device(cardp
->udev
);
395 lbs_deb_leave_args(LBS_DEB_USB
, "ret %d", ret
);
401 * @brief This function transfer the data to the device.
402 * @param priv pointer to struct lbs_private
403 * @param payload pointer to payload data
404 * @param nb data length
407 static int usb_tx_block(struct usb_card_rec
*cardp
, u8
* payload
, u16 nb
)
411 /* check if device is removed */
412 if (cardp
->surprise_removed
) {
413 lbs_deb_usbd(&cardp
->udev
->dev
, "Device removed\n");
417 usb_fill_bulk_urb(cardp
->tx_urb
, cardp
->udev
,
418 usb_sndbulkpipe(cardp
->udev
,
419 cardp
->bulk_out_endpointAddr
),
420 payload
, nb
, if_usb_write_bulk_callback
, cardp
);
422 cardp
->tx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
424 if ((ret
= usb_submit_urb(cardp
->tx_urb
, GFP_ATOMIC
))) {
425 /* transfer failed */
426 lbs_deb_usbd(&cardp
->udev
->dev
, "usb_submit_urb failed: %d\n", ret
);
429 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
437 static int __if_usb_submit_rx_urb(struct usb_card_rec
*cardp
,
438 void (*callbackfn
)(struct urb
*urb
))
441 struct read_cb_info
*rinfo
= &cardp
->rinfo
;
444 if (!(skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
))) {
445 lbs_pr_err("No free skb\n");
451 /* Fill the receive configuration URB and initialise the Rx call back */
452 usb_fill_bulk_urb(cardp
->rx_urb
, cardp
->udev
,
453 usb_rcvbulkpipe(cardp
->udev
,
454 cardp
->bulk_in_endpointAddr
),
455 (void *) (skb
->tail
+ (size_t) IPFIELD_ALIGN_OFFSET
),
456 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
, callbackfn
,
459 cardp
->rx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
461 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
462 if ((ret
= usb_submit_urb(cardp
->rx_urb
, GFP_ATOMIC
))) {
463 /* handle failure conditions */
464 lbs_deb_usbd(&cardp
->udev
->dev
, "Submit Rx URB failed: %d\n", ret
);
469 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
477 static int if_usb_submit_rx_urb_fwload(struct usb_card_rec
*cardp
)
479 return __if_usb_submit_rx_urb(cardp
, &if_usb_receive_fwload
);
482 static int if_usb_submit_rx_urb(struct usb_card_rec
*cardp
)
484 return __if_usb_submit_rx_urb(cardp
, &if_usb_receive
);
487 static void if_usb_receive_fwload(struct urb
*urb
)
489 struct read_cb_info
*rinfo
= (struct read_cb_info
*)urb
->context
;
490 struct sk_buff
*skb
= rinfo
->skb
;
491 struct usb_card_rec
*cardp
= (struct usb_card_rec
*)rinfo
->cardp
;
492 struct fwsyncheader
*syncfwheader
;
493 struct bootcmdrespStr bootcmdresp
;
496 lbs_deb_usbd(&cardp
->udev
->dev
,
497 "URB status is failed during fw load\n");
502 if (cardp
->bootcmdresp
== 0) {
503 memcpy (&bootcmdresp
, skb
->data
+ IPFIELD_ALIGN_OFFSET
,
504 sizeof(bootcmdresp
));
505 if (le16_to_cpu(cardp
->udev
->descriptor
.bcdDevice
) < 0x3106) {
507 if_usb_submit_rx_urb_fwload(cardp
);
508 cardp
->bootcmdresp
= 1;
509 lbs_deb_usbd(&cardp
->udev
->dev
,
510 "Received valid boot command response\n");
513 if (bootcmdresp
.u32magicnumber
!= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
)) {
514 if (bootcmdresp
.u32magicnumber
== cpu_to_le32(CMD_TYPE_REQUEST
) ||
515 bootcmdresp
.u32magicnumber
== cpu_to_le32(CMD_TYPE_DATA
) ||
516 bootcmdresp
.u32magicnumber
== cpu_to_le32(CMD_TYPE_INDICATION
)) {
517 lbs_pr_info("Firmware already seems alive; resetting\n");
518 cardp
->bootcmdresp
= -1;
520 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
521 le32_to_cpu(bootcmdresp
.u32magicnumber
));
523 } else if (bootcmdresp
.u8cmd_tag
!= BOOT_CMD_FW_BY_USB
) {
525 "boot cmd response cmd_tag error (%d)\n",
526 bootcmdresp
.u8cmd_tag
);
527 } else if (bootcmdresp
.u8result
!= BOOT_CMD_RESP_OK
) {
529 "boot cmd response result error (%d)\n",
530 bootcmdresp
.u8result
);
532 cardp
->bootcmdresp
= 1;
533 lbs_deb_usbd(&cardp
->udev
->dev
,
534 "Received valid boot command response\n");
537 if_usb_submit_rx_urb_fwload(cardp
);
541 syncfwheader
= kmalloc(sizeof(struct fwsyncheader
), GFP_ATOMIC
);
543 lbs_deb_usbd(&cardp
->udev
->dev
, "Failure to allocate syncfwheader\n");
548 memcpy(syncfwheader
, skb
->data
+ IPFIELD_ALIGN_OFFSET
,
549 sizeof(struct fwsyncheader
));
551 if (!syncfwheader
->cmd
) {
553 lbs_deb_usbd(&cardp->udev->dev,
554 "FW received Blk with correct CRC\n");
555 lbs_deb_usbd(&cardp->udev->dev,
556 "FW received Blk seqnum = %d\n",
557 syncfwheader->seqnum);
561 lbs_deb_usbd(&cardp
->udev
->dev
,
562 "FW received Blk with CRC error\n");
568 if (cardp
->fwfinalblk
) {
569 cardp
->fwdnldover
= 1;
573 if_prog_firmware(cardp
);
575 if_usb_submit_rx_urb_fwload(cardp
);
583 #define MRVDRV_MIN_PKT_LEN 30
585 static inline void process_cmdtypedata(int recvlength
, struct sk_buff
*skb
,
586 struct usb_card_rec
*cardp
,
587 struct lbs_private
*priv
)
589 if (recvlength
> MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
+
590 MESSAGE_HEADER_LEN
|| recvlength
< MRVDRV_MIN_PKT_LEN
) {
591 lbs_deb_usbd(&cardp
->udev
->dev
,
592 "Packet length is Invalid\n");
597 skb_reserve(skb
, IPFIELD_ALIGN_OFFSET
);
598 skb_put(skb
, recvlength
);
599 skb_pull(skb
, MESSAGE_HEADER_LEN
);
600 lbs_process_rxed_packet(priv
, skb
);
601 priv
->upld_len
= (recvlength
- MESSAGE_HEADER_LEN
);
604 static inline void process_cmdrequest(int recvlength
, u8
*recvbuff
,
606 struct usb_card_rec
*cardp
,
607 struct lbs_private
*priv
)
610 if (recvlength
> MRVDRV_SIZE_OF_CMD_BUFFER
) {
611 lbs_deb_usbd(&cardp
->udev
->dev
,
612 "The receive buffer is too large\n");
620 spin_lock(&priv
->driver_lock
);
621 /* take care of cur_cmd = NULL case by reading the
622 * data to clear the interrupt */
623 if (!priv
->cur_cmd
) {
624 cmdbuf
= priv
->upld_buf
;
625 priv
->hisregcpy
&= ~MRVDRV_CMD_UPLD_RDY
;
627 cmdbuf
= priv
->cur_cmd
->bufvirtualaddr
;
629 cardp
->usb_int_cause
|= MRVDRV_CMD_UPLD_RDY
;
630 priv
->upld_len
= (recvlength
- MESSAGE_HEADER_LEN
);
631 memcpy(cmdbuf
, recvbuff
+ MESSAGE_HEADER_LEN
,
635 lbs_interrupt(priv
->dev
);
636 spin_unlock(&priv
->driver_lock
);
638 lbs_deb_usbd(&cardp
->udev
->dev
,
639 "Wake up main thread to handle cmd response\n");
645 * @brief This function reads of the packet into the upload buff,
646 * wake up the main thread and initialise the Rx callack.
648 * @param urb pointer to struct urb
651 static void if_usb_receive(struct urb
*urb
)
653 struct read_cb_info
*rinfo
= (struct read_cb_info
*)urb
->context
;
654 struct sk_buff
*skb
= rinfo
->skb
;
655 struct usb_card_rec
*cardp
= (struct usb_card_rec
*) rinfo
->cardp
;
656 struct lbs_private
*priv
= cardp
->priv
;
658 int recvlength
= urb
->actual_length
;
662 lbs_deb_enter(LBS_DEB_USB
);
668 lbs_deb_usbd(&cardp
->udev
->dev
,
669 "URB status is failed\n");
674 recvbuff
= skb
->data
+ IPFIELD_ALIGN_OFFSET
;
675 memcpy(&tmp
, recvbuff
, sizeof(u32
));
676 recvtype
= le32_to_cpu(tmp
);
677 lbs_deb_usbd(&cardp
->udev
->dev
,
678 "Recv length = 0x%x, Recv type = 0x%X\n",
679 recvlength
, recvtype
);
680 } else if (urb
->status
) {
687 process_cmdtypedata(recvlength
, skb
, cardp
, priv
);
690 case CMD_TYPE_REQUEST
:
691 process_cmdrequest(recvlength
, recvbuff
, skb
, cardp
, priv
);
694 case CMD_TYPE_INDICATION
:
695 /* Event cause handling */
696 spin_lock(&priv
->driver_lock
);
697 cardp
->usb_event_cause
= le32_to_cpu(*(__le32
*) (recvbuff
+ MESSAGE_HEADER_LEN
));
698 lbs_deb_usbd(&cardp
->udev
->dev
,"**EVENT** 0x%X\n",
699 cardp
->usb_event_cause
);
700 if (cardp
->usb_event_cause
& 0xffff0000) {
701 lbs_send_tx_feedback(priv
);
702 spin_unlock(&priv
->driver_lock
);
705 cardp
->usb_event_cause
<<= 3;
706 cardp
->usb_int_cause
|= MRVDRV_CARDEVENT
;
708 lbs_interrupt(priv
->dev
);
709 spin_unlock(&priv
->driver_lock
);
712 lbs_deb_usbd(&cardp
->udev
->dev
, "Unknown command type 0x%X\n",
719 if_usb_submit_rx_urb(cardp
);
721 lbs_deb_leave(LBS_DEB_USB
);
725 * @brief This function downloads data to FW
726 * @param priv pointer to struct lbs_private structure
727 * @param type type of data
728 * @param buf pointer to data buffer
729 * @param len number of bytes
732 static int if_usb_host_to_card(struct lbs_private
*priv
,
737 struct usb_card_rec
*cardp
= (struct usb_card_rec
*)priv
->card
;
739 lbs_deb_usbd(&cardp
->udev
->dev
,"*** type = %u\n", type
);
740 lbs_deb_usbd(&cardp
->udev
->dev
,"size after = %d\n", nb
);
742 if (type
== MVMS_CMD
) {
743 __le32 tmp
= cpu_to_le32(CMD_TYPE_REQUEST
);
744 priv
->dnld_sent
= DNLD_CMD_SENT
;
745 memcpy(cardp
->bulk_out_buffer
, (u8
*) & tmp
,
749 __le32 tmp
= cpu_to_le32(CMD_TYPE_DATA
);
750 priv
->dnld_sent
= DNLD_DATA_SENT
;
751 memcpy(cardp
->bulk_out_buffer
, (u8
*) & tmp
,
755 memcpy((cardp
->bulk_out_buffer
+ MESSAGE_HEADER_LEN
), payload
, nb
);
757 return usb_tx_block(cardp
, cardp
->bulk_out_buffer
,
758 nb
+ MESSAGE_HEADER_LEN
);
761 /* called with priv->driver_lock held */
762 static int if_usb_get_int_status(struct lbs_private
*priv
, u8
*ireg
)
764 struct usb_card_rec
*cardp
= priv
->card
;
766 *ireg
= cardp
->usb_int_cause
;
767 cardp
->usb_int_cause
= 0;
769 lbs_deb_usbd(&cardp
->udev
->dev
,"Int cause is 0x%X\n", *ireg
);
774 static int if_usb_read_event_cause(struct lbs_private
*priv
)
776 struct usb_card_rec
*cardp
= priv
->card
;
778 priv
->eventcause
= cardp
->usb_event_cause
;
779 /* Re-submit rx urb here to avoid event lost issue */
780 if_usb_submit_rx_urb(cardp
);
785 * @brief This function issues Boot command to the Boot2 code
786 * @param ivalue 1:Boot from FW by USB-Download
787 * 2:Boot from FW in EEPROM
790 static int if_usb_issue_boot_command(struct usb_card_rec
*cardp
, int ivalue
)
792 struct bootcmdstr sbootcmd
;
795 /* Prepare command */
796 sbootcmd
.u32magicnumber
= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
);
797 sbootcmd
.u8cmd_tag
= ivalue
;
799 sbootcmd
.au8dumy
[i
]=0x00;
800 memcpy(cardp
->bulk_out_buffer
, &sbootcmd
, sizeof(struct bootcmdstr
));
803 usb_tx_block(cardp
, cardp
->bulk_out_buffer
, sizeof(struct bootcmdstr
));
810 * @brief This function checks the validity of Boot2/FW image.
812 * @param data pointer to image
816 static int check_fwfile_format(u8
*data
, u32 totlen
)
819 u32 blksize
, offset
, len
;
826 struct fwheader
*fwh
= (void *)data
;
828 bincmd
= le32_to_cpu(fwh
->dnldcmd
);
829 blksize
= le32_to_cpu(fwh
->datalength
);
831 case FW_HAS_DATA_TO_RECV
:
832 offset
= sizeof(struct fwheader
) + blksize
;
838 case FW_HAS_LAST_BLOCK
:
849 lbs_pr_err("firmware file format check FAIL\n");
851 lbs_deb_fw("firmware file format check PASS\n");
857 static int if_usb_prog_firmware(struct usb_card_rec
*cardp
)
860 static int reset_count
= 10;
863 lbs_deb_enter(LBS_DEB_USB
);
865 if ((ret
= request_firmware(&cardp
->fw
, lbs_fw_name
,
866 &cardp
->udev
->dev
)) < 0) {
867 lbs_pr_err("request_firmware() failed with %#x\n", ret
);
868 lbs_pr_err("firmware %s not found\n", lbs_fw_name
);
872 if (check_fwfile_format(cardp
->fw
->data
, cardp
->fw
->size
))
876 if (if_usb_submit_rx_urb_fwload(cardp
) < 0) {
877 lbs_deb_usbd(&cardp
->udev
->dev
, "URB submission is failed\n");
882 cardp
->bootcmdresp
= 0;
886 /* Issue Boot command = 1, Boot from Download-FW */
887 if_usb_issue_boot_command(cardp
, BOOT_CMD_FW_BY_USB
);
888 /* wait for command response */
891 msleep_interruptible(100);
892 } while (cardp
->bootcmdresp
== 0 && j
< 10);
893 } while (cardp
->bootcmdresp
== 0 && i
< 5);
895 if (cardp
->bootcmdresp
<= 0) {
896 if (--reset_count
>= 0) {
897 if_usb_reset_device(cardp
);
905 cardp
->totalbytes
= 0;
906 cardp
->fwlastblksent
= 0;
908 cardp
->fwdnldover
= 0;
909 cardp
->fwseqnum
= -1;
910 cardp
->totalbytes
= 0;
911 cardp
->fwfinalblk
= 0;
913 if_prog_firmware(cardp
);
916 lbs_deb_usbd(&cardp
->udev
->dev
,"Wlan sched timeout\n");
918 msleep_interruptible(100);
919 if (cardp
->surprise_removed
|| i
>= 20)
921 } while (!cardp
->fwdnldover
);
923 if (!cardp
->fwdnldover
) {
924 lbs_pr_info("failed to load fw, resetting device!\n");
925 if (--reset_count
>= 0) {
926 if_usb_reset_device(cardp
);
930 lbs_pr_info("FW download failure, time = %d ms\n", i
* 100);
936 release_firmware(cardp
->fw
);
940 lbs_deb_leave_args(LBS_DEB_USB
, "ret %d", ret
);
946 static int if_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
948 struct usb_card_rec
*cardp
= usb_get_intfdata(intf
);
949 struct lbs_private
*priv
= cardp
->priv
;
951 lbs_deb_enter(LBS_DEB_USB
);
953 if (priv
->psstate
!= PS_STATE_FULL_POWER
)
956 if (priv
->mesh_dev
&& !priv
->mesh_autostart_enabled
) {
957 /* Mesh autostart must be activated while sleeping
958 * On resume it will go back to the current state
960 struct cmd_ds_mesh_access mesh_access
;
961 memset(&mesh_access
, 0, sizeof(mesh_access
));
962 mesh_access
.data
[0] = cpu_to_le32(1);
963 lbs_prepare_and_send_command(priv
,
965 CMD_ACT_MESH_SET_AUTOSTART_ENABLED
,
966 CMD_OPTION_WAITFORRSP
, 0, (void *)&mesh_access
);
969 netif_device_detach(cardp
->eth_dev
);
970 netif_device_detach(priv
->mesh_dev
);
972 /* Unlink tx & rx urb */
973 usb_kill_urb(cardp
->tx_urb
);
974 usb_kill_urb(cardp
->rx_urb
);
976 cardp
->rx_urb_recall
= 1;
978 lbs_deb_leave(LBS_DEB_USB
);
982 static int if_usb_resume(struct usb_interface
*intf
)
984 struct usb_card_rec
*cardp
= usb_get_intfdata(intf
);
985 struct lbs_private
*priv
= cardp
->priv
;
987 lbs_deb_enter(LBS_DEB_USB
);
989 cardp
->rx_urb_recall
= 0;
991 if_usb_submit_rx_urb(cardp
->priv
);
993 netif_device_attach(cardp
->eth_dev
);
994 netif_device_attach(priv
->mesh_dev
);
996 if (priv
->mesh_dev
&& !priv
->mesh_autostart_enabled
) {
997 /* Mesh autostart was activated while sleeping
998 * Disable it if appropriate
1000 struct cmd_ds_mesh_access mesh_access
;
1001 memset(&mesh_access
, 0, sizeof(mesh_access
));
1002 mesh_access
.data
[0] = cpu_to_le32(0);
1003 lbs_prepare_and_send_command(priv
,
1005 CMD_ACT_MESH_SET_AUTOSTART_ENABLED
,
1006 CMD_OPTION_WAITFORRSP
, 0, (void *)&mesh_access
);
1009 lbs_deb_leave(LBS_DEB_USB
);
1013 #define if_usb_suspend NULL
1014 #define if_usb_resume NULL
1017 static struct usb_driver if_usb_driver
= {
1019 .probe
= if_usb_probe
,
1020 .disconnect
= if_usb_disconnect
,
1021 .id_table
= if_usb_table
,
1022 .suspend
= if_usb_suspend
,
1023 .resume
= if_usb_resume
,
1026 static int __init
if_usb_init_module(void)
1030 lbs_deb_enter(LBS_DEB_MAIN
);
1032 ret
= usb_register(&if_usb_driver
);
1034 lbs_deb_leave_args(LBS_DEB_MAIN
, "ret %d", ret
);
1038 static void __exit
if_usb_exit_module(void)
1040 lbs_deb_enter(LBS_DEB_MAIN
);
1042 usb_deregister(&if_usb_driver
);
1044 lbs_deb_leave(LBS_DEB_MAIN
);
1047 module_init(if_usb_init_module
);
1048 module_exit(if_usb_exit_module
);
1050 MODULE_DESCRIPTION("8388 USB WLAN Driver");
1051 MODULE_AUTHOR("Marvell International Ltd.");
1052 MODULE_LICENSE("GPL");