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"
19 #define MESSAGE_HEADER_LEN 4
21 static char *lbs_fw_name = "usb8388.bin";
22 module_param_named(fw_name, lbs_fw_name, charp, 0644);
24 static struct usb_device_id if_usb_table[] = {
25 /* Enter the device signature inside */
26 { USB_DEVICE(0x1286, 0x2001) },
27 { USB_DEVICE(0x05a3, 0x8388) },
28 {} /* Terminating entry */
31 MODULE_DEVICE_TABLE(usb, if_usb_table);
33 static void if_usb_receive(struct urb *urb);
34 static void if_usb_receive_fwload(struct urb *urb);
35 static int if_usb_prog_firmware(struct usb_card_rec *cardp);
36 static int if_usb_host_to_card(struct lbs_private *priv,
40 static int if_usb_get_int_status(struct lbs_private *priv, u8 *);
41 static int if_usb_read_event_cause(struct lbs_private *);
42 static int usb_tx_block(struct usb_card_rec *cardp, u8 *payload, u16 nb);
43 static void if_usb_free(struct usb_card_rec *cardp);
44 static int if_usb_submit_rx_urb(struct usb_card_rec *cardp);
45 static int if_usb_reset_device(struct usb_card_rec *cardp);
48 * @brief call back function to handle the status of the URB
49 * @param urb pointer to urb structure
52 static void if_usb_write_bulk_callback(struct urb *urb)
54 struct usb_card_rec *cardp = (struct usb_card_rec *) urb->context;
56 /* handle the transmission complete validations */
58 if (urb->status == 0) {
59 struct lbs_private *priv = cardp->priv;
62 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
63 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
67 /* Used for both firmware TX and regular TX. priv isn't
68 * valid at firmware load time.
71 lbs_host_to_card_done(priv);
73 /* print the failure status number for debug */
74 lbs_pr_info("URB in failure status: %d\n", urb->status);
81 * @brief free tx/rx urb, skb and rx buffer
82 * @param cardp pointer usb_card_rec
85 static void if_usb_free(struct usb_card_rec *cardp)
87 lbs_deb_enter(LBS_DEB_USB);
89 /* Unlink tx & rx urb */
90 usb_kill_urb(cardp->tx_urb);
91 usb_kill_urb(cardp->rx_urb);
93 usb_free_urb(cardp->tx_urb);
96 usb_free_urb(cardp->rx_urb);
99 kfree(cardp->bulk_out_buffer);
100 cardp->bulk_out_buffer = NULL;
102 lbs_deb_leave(LBS_DEB_USB);
105 static void if_usb_set_boot2_ver(struct lbs_private *priv)
107 struct cmd_ds_set_boot2_ver b2_cmd;
110 b2_cmd.version = priv->boot2_version;
112 if (lbs_cmd(priv, CMD_SET_BOOT2_VER, b2_cmd, NULL, 0))
113 lbs_deb_usb("Setting boot2 version failed\n");
116 void if_usb_fw_timeo(unsigned long priv)
118 struct usb_card_rec *cardp = (void *)priv;
120 if (cardp->fwdnldover) {
121 lbs_deb_usb("Download complete, no event. Assuming success\n");
123 lbs_pr_err("Download timed out\n");
124 cardp->surprise_removed = 1;
126 wake_up(&cardp->fw_wq);
129 * @brief sets the configuration values
130 * @param ifnum interface number
131 * @param id pointer to usb_device_id
132 * @return 0 on success, error code on failure
134 static int if_usb_probe(struct usb_interface *intf,
135 const struct usb_device_id *id)
137 struct usb_device *udev;
138 struct usb_host_interface *iface_desc;
139 struct usb_endpoint_descriptor *endpoint;
140 struct lbs_private *priv;
141 struct usb_card_rec *cardp;
144 udev = interface_to_usbdev(intf);
146 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
148 lbs_pr_err("Out of memory allocating private data.\n");
152 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
153 init_waitqueue_head(&cardp->fw_wq);
156 iface_desc = intf->cur_altsetting;
158 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
159 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
160 le16_to_cpu(udev->descriptor.bcdUSB),
161 udev->descriptor.bDeviceClass,
162 udev->descriptor.bDeviceSubClass,
163 udev->descriptor.bDeviceProtocol);
165 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
166 endpoint = &iface_desc->endpoint[i].desc;
167 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
168 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
169 USB_ENDPOINT_XFER_BULK)) {
170 /* we found a bulk in endpoint */
171 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
172 le16_to_cpu(endpoint->wMaxPacketSize));
173 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
174 lbs_deb_usbd(&udev->dev,
175 "Rx URB allocation failed\n");
178 cardp->rx_urb_recall = 0;
180 cardp->bulk_in_size =
181 le16_to_cpu(endpoint->wMaxPacketSize);
182 cardp->bulk_in_endpointAddr =
184 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
185 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
186 endpoint->bEndpointAddress);
190 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
192 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
193 USB_ENDPOINT_XFER_BULK)) {
194 /* We found bulk out endpoint */
195 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
196 lbs_deb_usbd(&udev->dev,
197 "Tx URB allocation failed\n");
201 cardp->bulk_out_size =
202 le16_to_cpu(endpoint->wMaxPacketSize);
203 lbs_deb_usbd(&udev->dev,
204 "Bulk out size is %d\n",
205 le16_to_cpu(endpoint->wMaxPacketSize));
206 cardp->bulk_out_endpointAddr =
207 endpoint->bEndpointAddress;
208 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
209 endpoint->bEndpointAddress);
210 cardp->bulk_out_buffer =
211 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
214 if (!cardp->bulk_out_buffer) {
215 lbs_deb_usbd(&udev->dev,
216 "Could not allocate buffer\n");
222 /* Upload firmware */
223 cardp->rinfo.cardp = cardp;
224 if (if_usb_prog_firmware(cardp)) {
225 lbs_deb_usbd(&udev->dev, "FW upload failed");
226 goto err_prog_firmware;
229 if (!(priv = lbs_add_card(cardp, &udev->dev)))
230 goto err_prog_firmware;
234 if (lbs_add_mesh(priv, &udev->dev))
237 cardp->eth_dev = priv->dev;
239 priv->hw_host_to_card = if_usb_host_to_card;
240 priv->hw_get_int_status = if_usb_get_int_status;
241 priv->hw_read_event_cause = if_usb_read_event_cause;
242 priv->boot2_version = udev->descriptor.bcdDevice;
244 /* Delay 200 ms to waiting for the FW ready */
245 if_usb_submit_rx_urb(cardp);
246 msleep_interruptible(200);
249 if (lbs_start_card(priv))
252 if_usb_set_boot2_ver(priv);
255 usb_set_intfdata(intf, cardp);
260 lbs_remove_mesh(priv);
262 lbs_remove_card(priv);
264 if_usb_reset_device(cardp);
273 * @brief free resource and cleanup
274 * @param intf USB interface structure
277 static void if_usb_disconnect(struct usb_interface *intf)
279 struct usb_card_rec *cardp = usb_get_intfdata(intf);
280 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
282 lbs_deb_enter(LBS_DEB_MAIN);
284 /* Update Surprise removed to TRUE */
285 cardp->surprise_removed = 1;
289 priv->surpriseremoved = 1;
291 lbs_remove_mesh(priv);
292 lbs_remove_card(priv);
295 /* this is (apparently?) necessary for future usage of the device */
296 lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
299 /* Unlink and free urb */
302 usb_set_intfdata(intf, NULL);
303 usb_put_dev(interface_to_usbdev(intf));
305 lbs_deb_leave(LBS_DEB_MAIN);
309 * @brief This function download FW
310 * @param priv pointer to struct lbs_private
313 static int if_usb_send_fw_pkt(struct usb_card_rec *cardp)
315 struct FWData *fwdata;
316 struct fwheader *fwheader;
317 u8 *firmware = cardp->fw->data;
319 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
324 fwheader = &fwdata->fwheader;
326 if (!cardp->CRC_OK) {
327 cardp->totalbytes = cardp->fwlastblksent;
328 cardp->fwseqnum = cardp->lastseqnum - 1;
332 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
336 memcpy(fwheader, &firmware[cardp->totalbytes],
337 sizeof(struct fwheader));
339 cardp->fwlastblksent = cardp->totalbytes;
340 cardp->totalbytes += sizeof(struct fwheader);
342 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
343 memcpy(fwdata->data, &firmware[cardp->totalbytes],
344 le32_to_cpu(fwdata->fwheader.datalength));
347 lbs_deb_usbd(&cardp->udev->dev,
348 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
351 cardp->fwseqnum = cardp->fwseqnum + 1;
353 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
354 cardp->lastseqnum = cardp->fwseqnum;
355 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
357 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
359 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
360 lbs_deb_usbd(&cardp->udev->dev,
361 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
364 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
365 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
367 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
369 lbs_deb_usbd(&cardp->udev->dev,
370 "Host has finished FW downloading\n");
371 lbs_deb_usbd(&cardp->udev->dev,
372 "Donwloading FW JUMP BLOCK\n");
374 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
375 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
376 cardp->fwfinalblk = 1;
380 lbs_deb_usbd(&cardp->udev->dev,
381 "The firmware download is done size is %d\n",
390 static int if_usb_reset_device(struct usb_card_rec *cardp)
392 struct cmd_ds_command *cmd = (void *)&cardp->bulk_out_buffer[4];
395 lbs_deb_enter(LBS_DEB_USB);
397 *(__le32 *)cardp->bulk_out_buffer = cpu_to_le32(CMD_TYPE_REQUEST);
399 cmd->command = cpu_to_le16(CMD_802_11_RESET);
400 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
401 cmd->result = cpu_to_le16(0);
402 cmd->seqnum = cpu_to_le16(0x5a5a);
403 cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
404 usb_tx_block(cardp, cardp->bulk_out_buffer, 4 + S_DS_GEN + sizeof(struct cmd_ds_802_11_reset));
407 ret = usb_reset_device(cardp->udev);
410 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
416 * @brief This function transfer the data to the device.
417 * @param priv pointer to struct lbs_private
418 * @param payload pointer to payload data
419 * @param nb data length
422 static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
426 /* check if device is removed */
427 if (cardp->surprise_removed) {
428 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
432 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
433 usb_sndbulkpipe(cardp->udev,
434 cardp->bulk_out_endpointAddr),
435 payload, nb, if_usb_write_bulk_callback, cardp);
437 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
439 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
440 /* transfer failed */
441 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
444 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
452 static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
453 void (*callbackfn)(struct urb *urb))
456 struct read_cb_info *rinfo = &cardp->rinfo;
459 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
460 lbs_pr_err("No free skb\n");
466 /* Fill the receive configuration URB and initialise the Rx call back */
467 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
468 usb_rcvbulkpipe(cardp->udev,
469 cardp->bulk_in_endpointAddr),
470 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
471 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
474 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
476 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
477 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
478 /* handle failure conditions */
479 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
484 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
492 static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
494 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
497 static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
499 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
502 static void if_usb_receive_fwload(struct urb *urb)
504 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
505 struct sk_buff *skb = rinfo->skb;
506 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
507 struct fwsyncheader *syncfwheader;
508 struct bootcmdrespStr bootcmdresp;
511 lbs_deb_usbd(&cardp->udev->dev,
512 "URB status is failed during fw load\n");
517 if (cardp->bootcmdresp <= 0) {
518 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
519 sizeof(bootcmdresp));
520 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
522 if_usb_submit_rx_urb_fwload(cardp);
523 cardp->bootcmdresp = 1;
524 lbs_deb_usbd(&cardp->udev->dev,
525 "Received valid boot command response\n");
528 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
529 if (bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_REQUEST) ||
530 bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_DATA) ||
531 bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_INDICATION)) {
532 lbs_pr_info("Firmware already seems alive; resetting\n");
533 cardp->bootcmdresp = -1;
535 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
536 le32_to_cpu(bootcmdresp.u32magicnumber));
538 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
540 "boot cmd response cmd_tag error (%d)\n",
541 bootcmdresp.u8cmd_tag);
542 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
544 "boot cmd response result error (%d)\n",
545 bootcmdresp.u8result);
547 cardp->bootcmdresp = 1;
548 lbs_deb_usbd(&cardp->udev->dev,
549 "Received valid boot command response\n");
552 if_usb_submit_rx_urb_fwload(cardp);
556 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
558 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
563 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
564 sizeof(struct fwsyncheader));
566 if (!syncfwheader->cmd) {
568 lbs_deb_usbd(&cardp->udev->dev,
569 "FW received Blk with correct CRC\n");
570 lbs_deb_usbd(&cardp->udev->dev,
571 "FW received Blk seqnum = %d\n",
572 syncfwheader->seqnum);
576 lbs_deb_usbd(&cardp->udev->dev,
577 "FW received Blk with CRC error\n");
583 /* reschedule timer for 200ms hence */
584 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
586 if (cardp->fwfinalblk) {
587 cardp->fwdnldover = 1;
591 if_usb_send_fw_pkt(cardp);
593 if_usb_submit_rx_urb_fwload(cardp);
600 #define MRVDRV_MIN_PKT_LEN 30
602 static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
603 struct usb_card_rec *cardp,
604 struct lbs_private *priv)
606 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
607 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
608 lbs_deb_usbd(&cardp->udev->dev,
609 "Packet length is Invalid\n");
614 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
615 skb_put(skb, recvlength);
616 skb_pull(skb, MESSAGE_HEADER_LEN);
617 lbs_process_rxed_packet(priv, skb);
618 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
621 static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
623 struct usb_card_rec *cardp,
624 struct lbs_private *priv)
627 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
628 lbs_deb_usbd(&cardp->udev->dev,
629 "The receive buffer is too large\n");
637 spin_lock(&priv->driver_lock);
638 /* take care of cur_cmd = NULL case by reading the
639 * data to clear the interrupt */
640 if (!priv->cur_cmd) {
641 cmdbuf = priv->upld_buf;
642 priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
644 cmdbuf = priv->cur_cmd->bufvirtualaddr;
646 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
647 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
648 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
653 spin_unlock(&priv->driver_lock);
655 lbs_deb_usbd(&cardp->udev->dev,
656 "Wake up main thread to handle cmd response\n");
662 * @brief This function reads of the packet into the upload buff,
663 * wake up the main thread and initialise the Rx callack.
665 * @param urb pointer to struct urb
668 static void if_usb_receive(struct urb *urb)
670 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
671 struct sk_buff *skb = rinfo->skb;
672 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
673 struct lbs_private *priv = cardp->priv;
675 int recvlength = urb->actual_length;
679 lbs_deb_enter(LBS_DEB_USB);
685 lbs_deb_usbd(&cardp->udev->dev,
686 "URB status is failed\n");
691 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
692 memcpy(&tmp, recvbuff, sizeof(u32));
693 recvtype = le32_to_cpu(tmp);
694 lbs_deb_usbd(&cardp->udev->dev,
695 "Recv length = 0x%x, Recv type = 0x%X\n",
696 recvlength, recvtype);
697 } else if (urb->status) {
704 process_cmdtypedata(recvlength, skb, cardp, priv);
707 case CMD_TYPE_REQUEST:
708 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
711 case CMD_TYPE_INDICATION:
712 /* Event cause handling */
713 spin_lock(&priv->driver_lock);
714 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
715 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
716 cardp->usb_event_cause);
717 if (cardp->usb_event_cause & 0xffff0000) {
718 lbs_send_tx_feedback(priv);
719 spin_unlock(&priv->driver_lock);
722 cardp->usb_event_cause <<= 3;
723 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
726 spin_unlock(&priv->driver_lock);
729 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
736 if_usb_submit_rx_urb(cardp);
738 lbs_deb_leave(LBS_DEB_USB);
742 * @brief This function downloads data to FW
743 * @param priv pointer to struct lbs_private structure
744 * @param type type of data
745 * @param buf pointer to data buffer
746 * @param len number of bytes
749 static int if_usb_host_to_card(struct lbs_private *priv,
754 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
756 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
757 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
759 if (type == MVMS_CMD) {
760 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
761 priv->dnld_sent = DNLD_CMD_SENT;
762 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
766 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
767 priv->dnld_sent = DNLD_DATA_SENT;
768 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
772 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
774 return usb_tx_block(cardp, cardp->bulk_out_buffer,
775 nb + MESSAGE_HEADER_LEN);
778 /* called with priv->driver_lock held */
779 static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
781 struct usb_card_rec *cardp = priv->card;
783 *ireg = cardp->usb_int_cause;
784 cardp->usb_int_cause = 0;
786 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
791 static int if_usb_read_event_cause(struct lbs_private *priv)
793 struct usb_card_rec *cardp = priv->card;
795 priv->eventcause = cardp->usb_event_cause;
796 /* Re-submit rx urb here to avoid event lost issue */
797 if_usb_submit_rx_urb(cardp);
802 * @brief This function issues Boot command to the Boot2 code
803 * @param ivalue 1:Boot from FW by USB-Download
804 * 2:Boot from FW in EEPROM
807 static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
809 struct bootcmdstr sbootcmd;
812 /* Prepare command */
813 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
814 sbootcmd.u8cmd_tag = ivalue;
816 sbootcmd.au8dumy[i]=0x00;
817 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
820 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
827 * @brief This function checks the validity of Boot2/FW image.
829 * @param data pointer to image
833 static int check_fwfile_format(u8 *data, u32 totlen)
836 u32 blksize, offset, len;
843 struct fwheader *fwh = (void *)data;
845 bincmd = le32_to_cpu(fwh->dnldcmd);
846 blksize = le32_to_cpu(fwh->datalength);
848 case FW_HAS_DATA_TO_RECV:
849 offset = sizeof(struct fwheader) + blksize;
855 case FW_HAS_LAST_BLOCK:
866 lbs_pr_err("firmware file format check FAIL\n");
868 lbs_deb_fw("firmware file format check PASS\n");
874 static int if_usb_prog_firmware(struct usb_card_rec *cardp)
877 static int reset_count = 10;
880 lbs_deb_enter(LBS_DEB_USB);
882 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
883 &cardp->udev->dev)) < 0) {
884 lbs_pr_err("request_firmware() failed with %#x\n", ret);
885 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
889 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
893 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
894 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
899 cardp->bootcmdresp = 0;
903 /* Issue Boot command = 1, Boot from Download-FW */
904 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
905 /* wait for command response */
908 msleep_interruptible(100);
909 } while (cardp->bootcmdresp == 0 && j < 10);
910 } while (cardp->bootcmdresp == 0 && i < 5);
912 if (cardp->bootcmdresp <= 0) {
913 if (--reset_count >= 0) {
914 if_usb_reset_device(cardp);
922 cardp->totalbytes = 0;
923 cardp->fwlastblksent = 0;
925 cardp->fwdnldover = 0;
926 cardp->fwseqnum = -1;
927 cardp->totalbytes = 0;
928 cardp->fwfinalblk = 0;
930 /* Send the first firmware packet... */
931 if_usb_send_fw_pkt(cardp);
933 /* ... and wait for the process to complete */
934 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
936 del_timer_sync(&cardp->fw_timeout);
938 if (!cardp->fwdnldover) {
939 lbs_pr_info("failed to load fw, resetting device!\n");
940 if (--reset_count >= 0) {
941 if_usb_reset_device(cardp);
945 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
951 release_firmware(cardp->fw);
955 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
961 static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
963 struct usb_card_rec *cardp = usb_get_intfdata(intf);
964 struct lbs_private *priv = cardp->priv;
966 lbs_deb_enter(LBS_DEB_USB);
968 if (priv->psstate != PS_STATE_FULL_POWER)
971 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
972 /* Mesh autostart must be activated while sleeping
973 * On resume it will go back to the current state
975 struct cmd_ds_mesh_access mesh_access;
976 memset(&mesh_access, 0, sizeof(mesh_access));
977 mesh_access.data[0] = cpu_to_le32(1);
978 lbs_prepare_and_send_command(priv,
980 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
981 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
984 netif_device_detach(cardp->eth_dev);
985 netif_device_detach(priv->mesh_dev);
987 /* Unlink tx & rx urb */
988 usb_kill_urb(cardp->tx_urb);
989 usb_kill_urb(cardp->rx_urb);
991 cardp->rx_urb_recall = 1;
993 lbs_deb_leave(LBS_DEB_USB);
997 static int if_usb_resume(struct usb_interface *intf)
999 struct usb_card_rec *cardp = usb_get_intfdata(intf);
1000 struct lbs_private *priv = cardp->priv;
1002 lbs_deb_enter(LBS_DEB_USB);
1004 cardp->rx_urb_recall = 0;
1006 if_usb_submit_rx_urb(cardp->priv);
1008 netif_device_attach(cardp->eth_dev);
1009 netif_device_attach(priv->mesh_dev);
1011 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
1012 /* Mesh autostart was activated while sleeping
1013 * Disable it if appropriate
1015 struct cmd_ds_mesh_access mesh_access;
1016 memset(&mesh_access, 0, sizeof(mesh_access));
1017 mesh_access.data[0] = cpu_to_le32(0);
1018 lbs_prepare_and_send_command(priv,
1020 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
1021 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1024 lbs_deb_leave(LBS_DEB_USB);
1028 #define if_usb_suspend NULL
1029 #define if_usb_resume NULL
1032 static struct usb_driver if_usb_driver = {
1034 .probe = if_usb_probe,
1035 .disconnect = if_usb_disconnect,
1036 .id_table = if_usb_table,
1037 .suspend = if_usb_suspend,
1038 .resume = if_usb_resume,
1041 static int __init if_usb_init_module(void)
1045 lbs_deb_enter(LBS_DEB_MAIN);
1047 ret = usb_register(&if_usb_driver);
1049 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1053 static void __exit if_usb_exit_module(void)
1055 lbs_deb_enter(LBS_DEB_MAIN);
1057 usb_deregister(&if_usb_driver);
1059 lbs_deb_leave(LBS_DEB_MAIN);
1062 module_init(if_usb_init_module);
1063 module_exit(if_usb_exit_module);
1065 MODULE_DESCRIPTION("8388 USB WLAN Driver");
1066 MODULE_AUTHOR("Marvell International Ltd.");
1067 MODULE_LICENSE("GPL");