pcmcia: CompactFlash driver for PA Semi Electra boards
[pv_ops_mirror.git] / drivers / net / wireless / libertas / if_usb.c
blobcb59f46ed126471816cee87712f89ee32d2cd4ef
1 /**
2 * This file contains functions used in USB interface module.
3 */
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>
9 #include <linux/usb.h>
11 #define DRV_NAME "usb8xxx"
13 #include "host.h"
14 #include "decl.h"
15 #include "defs.h"
16 #include "dev.h"
17 #include "if_usb.h"
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);
56 /**
57 * @brief call back function to handle the status of the URB
58 * @param urb pointer to urb structure
59 * @return N/A
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",
73 urb->actual_length);
76 /* Used for both firmware TX and regular TX. priv isn't
77 * valid at firmware load time.
79 if (priv) {
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);
94 } else {
95 /* print the failure status number for debug */
96 lbs_pr_info("URB in failure status: %d\n", urb->status);
99 return;
103 * @brief free tx/rx urb, skb and rx buffer
104 * @param cardp pointer usb_card_rec
105 * @return N/A
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;
139 wlan_private *priv;
140 struct usb_card_rec *cardp;
141 int i;
143 udev = interface_to_usbdev(intf);
145 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
146 if (!cardp) {
147 lbs_pr_err("Out of memory allocating private data.\n");
148 goto error;
151 cardp->udev = udev;
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");
172 goto dealloc;
174 cardp->rx_urb_recall = 0;
176 cardp->bulk_in_size =
177 le16_to_cpu(endpoint->wMaxPacketSize);
178 cardp->bulk_in_endpointAddr =
179 (endpoint->
180 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
181 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
182 endpoint->bEndpointAddress);
185 if (((endpoint->
186 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
187 USB_DIR_OUT)
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");
194 goto dealloc;
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,
208 GFP_KERNEL);
210 if (!cardp->bulk_out_buffer) {
211 lbs_deb_usbd(&udev->dev,
212 "Could not allocate buffer\n");
213 goto dealloc;
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;
228 cardp->priv = priv;
230 if (libertas_add_mesh(priv, &udev->dev))
231 goto err_add_mesh;
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))
246 goto err_start_card;
248 list_add_tail(&cardp->list, &usb_devices);
250 usb_get_dev(udev);
251 usb_set_intfdata(intf, cardp);
253 return 0;
255 err_start_card:
256 libertas_remove_mesh(priv);
257 err_add_mesh:
258 libertas_remove_card(priv);
259 err_prog_firmware:
260 if_usb_reset_device(cardp);
261 dealloc:
262 if_usb_free(cardp);
264 error:
265 return -ENOMEM;
269 * @brief free resource and cleanup
270 * @param intf USB interface structure
271 * @return N/A
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);
285 if (priv) {
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 */
295 if_usb_free(cardp);
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
306 * @return 0
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);
316 if (!fwdata)
317 return -1;
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",
328 cardp->totalbytes);
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,
357 cardp->totalbytes);
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",
377 cardp->totalbytes);
380 kfree(fwdata);
382 return 0;
385 static int if_usb_reset_device(struct usb_card_rec *cardp)
387 int ret;
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);
396 if (!ret && priv) {
397 msleep(10);
398 ret = libertas_reset_device(priv);
399 msleep(10);
402 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
404 return 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
412 * @return 0 or -1
414 static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
416 int ret = -1;
418 /* check if device is removed */
419 if (cardp->surprise_removed) {
420 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
421 goto tx_ret;
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");
434 ret = -1;
435 } else {
436 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
437 ret = 0;
440 tx_ret:
441 return ret;
444 static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
445 void (*callbackfn)(struct urb *urb))
447 struct sk_buff *skb;
448 struct read_cb_info *rinfo = &cardp->rinfo;
449 int ret = -1;
451 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
452 lbs_pr_err("No free skb\n");
453 goto rx_ret;
456 rinfo->skb = skb;
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,
464 rinfo);
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");
472 ret = -1;
473 } else {
474 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
475 ret = 0;
478 rx_ret:
479 return ret;
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;
500 if (urb->status) {
501 lbs_deb_usbd(&cardp->udev->dev,
502 "URB status is failed during fw load\n");
503 kfree_skb(skb);
504 return;
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) {
511 kfree_skb(skb);
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");
516 return;
518 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
519 lbs_pr_info(
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) {
523 lbs_pr_info(
524 "boot cmd response cmd_tag error (%d)\n",
525 bootcmdresp.u8cmd_tag);
526 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
527 lbs_pr_info(
528 "boot cmd response result error (%d)\n",
529 bootcmdresp.u8result);
530 } else {
531 cardp->bootcmdresp = 1;
532 lbs_deb_usbd(&cardp->udev->dev,
533 "Received valid boot command response\n");
535 kfree_skb(skb);
536 if_usb_submit_rx_urb_fwload(cardp);
537 return;
540 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
541 if (!syncfwheader) {
542 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
543 kfree_skb(skb);
544 return;
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);
558 cardp->CRC_OK = 1;
559 } else {
560 lbs_deb_usbd(&cardp->udev->dev,
561 "FW received Blk with CRC error\n");
562 cardp->CRC_OK = 0;
565 kfree_skb(skb);
567 if (cardp->fwfinalblk) {
568 cardp->fwdnldover = 1;
569 goto exit;
572 if_prog_firmware(cardp);
574 if_usb_submit_rx_urb_fwload(cardp);
575 exit:
576 kfree(syncfwheader);
578 return;
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,
586 wlan_private *priv)
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");
592 kfree_skb(skb);
593 return;
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,
604 struct sk_buff *skb,
605 struct usb_card_rec *cardp,
606 wlan_private *priv)
608 u8 *cmdbuf;
609 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
610 lbs_deb_usbd(&cardp->udev->dev,
611 "The receive buffer is too large\n");
612 kfree_skb(skb);
613 return;
616 if (!in_interrupt())
617 BUG();
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;
625 } else
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,
631 priv->upld_len);
633 kfree_skb(skb);
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");
640 return;
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
648 * @return N/A
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;
658 u8 *recvbuff = NULL;
659 u32 recvtype = 0;
661 lbs_deb_enter(LBS_DEB_USB);
663 if (recvlength) {
664 __le32 tmp;
666 if (urb->status) {
667 lbs_deb_usbd(&cardp->udev->dev,
668 "URB status is failed\n");
669 kfree_skb(skb);
670 goto setup_for_next;
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)
680 goto rx_exit;
682 switch (recvtype) {
683 case CMD_TYPE_DATA:
684 process_cmdtypedata(recvlength, skb, cardp, priv);
685 break;
687 case CMD_TYPE_REQUEST:
688 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
689 break;
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);
700 break;
702 cardp->usb_event_cause <<= 3;
703 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
704 kfree_skb(skb);
705 libertas_interrupt(priv->dev);
706 spin_unlock(&priv->adapter->driver_lock);
707 goto rx_exit;
708 default:
709 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
710 recvtype);
711 kfree_skb(skb);
712 break;
715 setup_for_next:
716 if_usb_submit_rx_urb(cardp);
717 rx_exit:
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
727 * @return 0 or -1
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,
740 MESSAGE_HEADER_LEN);
742 } else {
743 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
744 priv->dnld_sent = DNLD_DATA_SENT;
745 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
746 MESSAGE_HEADER_LEN);
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);
765 return 0;
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);
775 return 0;
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
782 * @return 0
784 static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
786 struct bootcmdstr sbootcmd;
787 int i;
789 /* Prepare command */
790 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
791 sbootcmd.u8cmd_tag = ivalue;
792 for (i=0; i<11; i++)
793 sbootcmd.au8dumy[i]=0x00;
794 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
796 /* Issue command */
797 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
799 return 0;
804 * @brief This function checks the validity of Boot2/FW image.
806 * @param data pointer to image
807 * len image length
808 * @return 0 or -1
810 static int check_fwfile_format(u8 *data, u32 totlen)
812 u32 bincmd, exit;
813 u32 blksize, offset, len;
814 int ret;
816 ret = 1;
817 exit = len = 0;
819 do {
820 struct fwheader *fwh = (void *)data;
822 bincmd = le32_to_cpu(fwh->dnldcmd);
823 blksize = le32_to_cpu(fwh->datalength);
824 switch (bincmd) {
825 case FW_HAS_DATA_TO_RECV:
826 offset = sizeof(struct fwheader) + blksize;
827 data += offset;
828 len += offset;
829 if (len >= totlen)
830 exit = 1;
831 break;
832 case FW_HAS_LAST_BLOCK:
833 exit = 1;
834 ret = 0;
835 break;
836 default:
837 exit = 1;
838 break;
840 } while (!exit);
842 if (ret)
843 lbs_pr_err("firmware file format check FAIL\n");
844 else
845 lbs_deb_fw("firmware file format check PASS\n");
847 return ret;
851 static int if_usb_prog_firmware(struct usb_card_rec *cardp)
853 int i = 0;
854 static int reset_count = 10;
855 int ret = 0;
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);
863 goto done;
866 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
867 goto release_fw;
869 restart:
870 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
871 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
872 ret = -1;
873 goto release_fw;
876 cardp->bootcmdresp = 0;
877 do {
878 int j = 0;
879 i++;
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 */
883 do {
884 j++;
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);
892 goto restart;
894 return -1;
897 i = 0;
899 cardp->totalbytes = 0;
900 cardp->fwlastblksent = 0;
901 cardp->CRC_OK = 1;
902 cardp->fwdnldover = 0;
903 cardp->fwseqnum = -1;
904 cardp->totalbytes = 0;
905 cardp->fwfinalblk = 0;
907 if_prog_firmware(cardp);
909 do {
910 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
911 i++;
912 msleep_interruptible(100);
913 if (cardp->surprise_removed || i >= 20)
914 break;
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);
921 goto restart;
924 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
925 ret = -1;
926 goto release_fw;
929 release_fw:
930 release_firmware(cardp->fw);
931 cardp->fw = NULL;
933 done:
934 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
935 return ret;
939 #ifdef CONFIG_PM
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)
948 return -1;
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,
958 CMD_MESH_ACCESS,
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);
973 return 0;
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,
998 CMD_MESH_ACCESS,
999 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
1000 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1003 lbs_deb_leave(LBS_DEB_USB);
1004 return 0;
1006 #else
1007 #define if_usb_suspend NULL
1008 #define if_usb_resume NULL
1009 #endif
1011 static struct usb_driver if_usb_driver = {
1012 /* driver name */
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)
1026 int ret = 0;
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);
1037 return 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");