gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / net / wireless / marvell / libertas_tf / if_usb.c
blob25ac9db35dbfe5b04060327566aa00399b5e2f67
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2008, cozybit Inc.
4 * Copyright (C) 2003-2006, Marvell International Ltd.
5 */
6 #define DRV_NAME "lbtf_usb"
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include "libertas_tf.h"
11 #include "if_usb.h"
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/firmware.h>
16 #include <linux/netdevice.h>
17 #include <linux/slab.h>
18 #include <linux/usb.h>
20 #define INSANEDEBUG 0
21 #define lbtf_deb_usb2(...) do { if (INSANEDEBUG) lbtf_deb_usbd(__VA_ARGS__); } while (0)
23 #define MESSAGE_HEADER_LEN 4
25 static char *lbtf_fw_name = "lbtf_usb.bin";
26 module_param_named(fw_name, lbtf_fw_name, charp, 0644);
28 MODULE_FIRMWARE("lbtf_usb.bin");
30 static const struct usb_device_id if_usb_table[] = {
31 /* Enter the device signature inside */
32 { USB_DEVICE(0x1286, 0x2001) },
33 { USB_DEVICE(0x05a3, 0x8388) },
34 {} /* Terminating entry */
37 MODULE_DEVICE_TABLE(usb, if_usb_table);
39 static void if_usb_receive(struct urb *urb);
40 static void if_usb_receive_fwload(struct urb *urb);
41 static int if_usb_prog_firmware(struct lbtf_private *priv);
42 static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type,
43 uint8_t *payload, uint16_t nb);
44 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
45 uint16_t nb, u8 data);
46 static void if_usb_free(struct if_usb_card *cardp);
47 static int if_usb_submit_rx_urb(struct if_usb_card *cardp);
48 static int if_usb_reset_device(struct lbtf_private *priv);
50 /**
51 * if_usb_wrike_bulk_callback - call back to handle URB status
53 * @param urb pointer to urb structure
55 static void if_usb_write_bulk_callback(struct urb *urb)
57 if (urb->status != 0) {
58 /* print the failure status number for debug */
59 pr_info("URB in failure status: %d\n", urb->status);
60 } else {
61 lbtf_deb_usb2(&urb->dev->dev, "URB status is successful\n");
62 lbtf_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
63 urb->actual_length);
67 /**
68 * if_usb_free - free tx/rx urb, skb and rx buffer
70 * @param cardp pointer if_usb_card
72 static void if_usb_free(struct if_usb_card *cardp)
74 lbtf_deb_enter(LBTF_DEB_USB);
76 /* Unlink tx & rx urb */
77 usb_kill_urb(cardp->tx_urb);
78 usb_kill_urb(cardp->rx_urb);
79 usb_kill_urb(cardp->cmd_urb);
81 usb_free_urb(cardp->tx_urb);
82 cardp->tx_urb = NULL;
84 usb_free_urb(cardp->rx_urb);
85 cardp->rx_urb = NULL;
87 usb_free_urb(cardp->cmd_urb);
88 cardp->cmd_urb = NULL;
90 kfree(cardp->ep_out_buf);
91 cardp->ep_out_buf = NULL;
93 lbtf_deb_leave(LBTF_DEB_USB);
96 static void if_usb_setup_firmware(struct lbtf_private *priv)
98 struct if_usb_card *cardp = priv->card;
99 struct cmd_ds_set_boot2_ver b2_cmd;
101 lbtf_deb_enter(LBTF_DEB_USB);
103 if_usb_submit_rx_urb(cardp);
104 b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
105 b2_cmd.action = 0;
106 b2_cmd.version = cardp->boot2_version;
108 if (lbtf_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
109 lbtf_deb_usb("Setting boot2 version failed\n");
111 lbtf_deb_leave(LBTF_DEB_USB);
114 static void if_usb_fw_timeo(struct timer_list *t)
116 struct if_usb_card *cardp = from_timer(cardp, t, fw_timeout);
118 lbtf_deb_enter(LBTF_DEB_USB);
119 if (!cardp->fwdnldover) {
120 /* Download timed out */
121 cardp->priv->surpriseremoved = 1;
122 pr_err("Download timed out\n");
123 } else {
124 lbtf_deb_usb("Download complete, no event. Assuming success\n");
126 wake_up(&cardp->fw_wq);
127 lbtf_deb_leave(LBTF_DEB_USB);
130 static const struct lbtf_ops if_usb_ops = {
131 .hw_host_to_card = if_usb_host_to_card,
132 .hw_prog_firmware = if_usb_prog_firmware,
133 .hw_reset_device = if_usb_reset_device,
137 * if_usb_probe - sets the configuration values
139 * @ifnum interface number
140 * @id pointer to usb_device_id
142 * Returns: 0 on success, error code on failure
144 static int if_usb_probe(struct usb_interface *intf,
145 const struct usb_device_id *id)
147 struct usb_device *udev;
148 struct usb_host_interface *iface_desc;
149 struct usb_endpoint_descriptor *endpoint;
150 struct lbtf_private *priv;
151 struct if_usb_card *cardp;
152 int i;
154 lbtf_deb_enter(LBTF_DEB_USB);
155 udev = interface_to_usbdev(intf);
157 cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
158 if (!cardp)
159 goto error;
161 timer_setup(&cardp->fw_timeout, if_usb_fw_timeo, 0);
162 init_waitqueue_head(&cardp->fw_wq);
164 cardp->udev = udev;
165 iface_desc = intf->cur_altsetting;
167 lbtf_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
168 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
169 le16_to_cpu(udev->descriptor.bcdUSB),
170 udev->descriptor.bDeviceClass,
171 udev->descriptor.bDeviceSubClass,
172 udev->descriptor.bDeviceProtocol);
174 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
175 endpoint = &iface_desc->endpoint[i].desc;
176 if (usb_endpoint_is_bulk_in(endpoint)) {
177 cardp->ep_in_size =
178 le16_to_cpu(endpoint->wMaxPacketSize);
179 cardp->ep_in = usb_endpoint_num(endpoint);
181 lbtf_deb_usbd(&udev->dev, "in_endpoint = %d\n",
182 cardp->ep_in);
183 lbtf_deb_usbd(&udev->dev, "Bulk in size is %d\n",
184 cardp->ep_in_size);
185 } else if (usb_endpoint_is_bulk_out(endpoint)) {
186 cardp->ep_out_size =
187 le16_to_cpu(endpoint->wMaxPacketSize);
188 cardp->ep_out = usb_endpoint_num(endpoint);
190 lbtf_deb_usbd(&udev->dev, "out_endpoint = %d\n",
191 cardp->ep_out);
192 lbtf_deb_usbd(&udev->dev, "Bulk out size is %d\n",
193 cardp->ep_out_size);
196 if (!cardp->ep_out_size || !cardp->ep_in_size) {
197 lbtf_deb_usbd(&udev->dev, "Endpoints not found\n");
198 /* Endpoints not found */
199 goto dealloc;
202 cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
203 if (!cardp->rx_urb)
204 goto dealloc;
206 cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
207 if (!cardp->tx_urb)
208 goto dealloc;
210 cardp->cmd_urb = usb_alloc_urb(0, GFP_KERNEL);
211 if (!cardp->cmd_urb)
212 goto dealloc;
214 cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
215 GFP_KERNEL);
216 if (!cardp->ep_out_buf) {
217 lbtf_deb_usbd(&udev->dev, "Could not allocate buffer\n");
218 goto dealloc;
221 cardp->boot2_version = udev->descriptor.bcdDevice;
222 priv = lbtf_add_card(cardp, &udev->dev, &if_usb_ops);
223 if (!priv)
224 goto dealloc;
226 usb_get_dev(udev);
227 usb_set_intfdata(intf, cardp);
229 return 0;
231 dealloc:
232 if_usb_free(cardp);
233 error:
234 lbtf_deb_leave(LBTF_DEB_MAIN);
235 return -ENOMEM;
239 * if_usb_disconnect - free resource and cleanup
241 * @intf USB interface structure
243 static void if_usb_disconnect(struct usb_interface *intf)
245 struct if_usb_card *cardp = usb_get_intfdata(intf);
246 struct lbtf_private *priv = cardp->priv;
248 lbtf_deb_enter(LBTF_DEB_MAIN);
250 if_usb_reset_device(priv);
252 if (priv)
253 lbtf_remove_card(priv);
255 /* Unlink and free urb */
256 if_usb_free(cardp);
258 usb_set_intfdata(intf, NULL);
259 usb_put_dev(interface_to_usbdev(intf));
261 lbtf_deb_leave(LBTF_DEB_MAIN);
265 * if_usb_send_fw_pkt - This function downloads the FW
267 * @priv pointer to struct lbtf_private
269 * Returns: 0
271 static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
273 struct fwdata *fwdata = cardp->ep_out_buf;
274 u8 *firmware = (u8 *) cardp->fw->data;
276 lbtf_deb_enter(LBTF_DEB_FW);
278 /* If we got a CRC failure on the last block, back
279 up and retry it */
280 if (!cardp->CRC_OK) {
281 cardp->totalbytes = cardp->fwlastblksent;
282 cardp->fwseqnum--;
285 lbtf_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
286 cardp->totalbytes);
288 /* struct fwdata (which we sent to the card) has an
289 extra __le32 field in between the header and the data,
290 which is not in the struct fwheader in the actual
291 firmware binary. Insert the seqnum in the middle... */
292 memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
293 sizeof(struct fwheader));
295 cardp->fwlastblksent = cardp->totalbytes;
296 cardp->totalbytes += sizeof(struct fwheader);
298 memcpy(fwdata->data, &firmware[cardp->totalbytes],
299 le32_to_cpu(fwdata->hdr.datalength));
301 lbtf_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
302 le32_to_cpu(fwdata->hdr.datalength));
304 fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
305 cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
307 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
308 le32_to_cpu(fwdata->hdr.datalength), 0);
310 if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
311 lbtf_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
312 lbtf_deb_usb2(&cardp->udev->dev,
313 "seqnum = %d totalbytes = %d\n",
314 cardp->fwseqnum, cardp->totalbytes);
315 } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
316 lbtf_deb_usb2(&cardp->udev->dev,
317 "Host has finished FW downloading\n");
318 lbtf_deb_usb2(&cardp->udev->dev, "Downloading FW JUMP BLOCK\n");
320 /* Host has finished FW downloading
321 * Donwloading FW JUMP BLOCK
323 cardp->fwfinalblk = 1;
326 lbtf_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
327 cardp->totalbytes);
329 lbtf_deb_leave(LBTF_DEB_FW);
330 return 0;
333 static int if_usb_reset_device(struct lbtf_private *priv)
335 struct if_usb_card *cardp = priv->card;
336 struct cmd_ds_802_11_reset *cmd = cardp->ep_out_buf + 4;
337 int ret;
339 lbtf_deb_enter(LBTF_DEB_USB);
341 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
343 cmd->hdr.command = cpu_to_le16(CMD_802_11_RESET);
344 cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset));
345 cmd->hdr.result = cpu_to_le16(0);
346 cmd->hdr.seqnum = cpu_to_le16(0x5a5a);
347 cmd->action = cpu_to_le16(CMD_ACT_HALT);
348 usb_tx_block(cardp, cardp->ep_out_buf,
349 4 + sizeof(struct cmd_ds_802_11_reset), 0);
351 msleep(100);
352 ret = usb_reset_device(cardp->udev);
353 msleep(100);
355 lbtf_deb_leave_args(LBTF_DEB_USB, "ret %d", ret);
357 return ret;
361 * usb_tx_block - transfer data to the device
363 * @priv pointer to struct lbtf_private
364 * @payload pointer to payload data
365 * @nb data length
366 * @data non-zero for data, zero for commands
368 * Returns: 0 on success, nonzero otherwise.
370 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
371 uint16_t nb, u8 data)
373 int ret = -1;
374 struct urb *urb;
376 lbtf_deb_enter(LBTF_DEB_USB);
377 /* check if device is removed */
378 if (cardp->priv->surpriseremoved) {
379 lbtf_deb_usbd(&cardp->udev->dev, "Device removed\n");
380 goto tx_ret;
383 if (data)
384 urb = cardp->tx_urb;
385 else
386 urb = cardp->cmd_urb;
388 usb_fill_bulk_urb(urb, cardp->udev,
389 usb_sndbulkpipe(cardp->udev,
390 cardp->ep_out),
391 payload, nb, if_usb_write_bulk_callback, cardp);
393 urb->transfer_flags |= URB_ZERO_PACKET;
395 if (usb_submit_urb(urb, GFP_ATOMIC)) {
396 lbtf_deb_usbd(&cardp->udev->dev,
397 "usb_submit_urb failed: %d\n", ret);
398 goto tx_ret;
401 lbtf_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
403 ret = 0;
405 tx_ret:
406 lbtf_deb_leave(LBTF_DEB_USB);
407 return ret;
410 static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
411 void (*callbackfn)(struct urb *urb))
413 struct sk_buff *skb;
414 int ret = -1;
416 lbtf_deb_enter(LBTF_DEB_USB);
418 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
419 if (!skb) {
420 pr_err("No free skb\n");
421 lbtf_deb_leave(LBTF_DEB_USB);
422 return -1;
425 cardp->rx_skb = skb;
427 /* Fill the receive configuration URB and initialise the Rx call back */
428 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
429 usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
430 skb_tail_pointer(skb),
431 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn, cardp);
433 lbtf_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n",
434 cardp->rx_urb);
435 ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC);
436 if (ret) {
437 lbtf_deb_usbd(&cardp->udev->dev,
438 "Submit Rx URB failed: %d\n", ret);
439 kfree_skb(skb);
440 cardp->rx_skb = NULL;
441 lbtf_deb_leave(LBTF_DEB_USB);
442 return -1;
443 } else {
444 lbtf_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
445 lbtf_deb_leave(LBTF_DEB_USB);
446 return 0;
450 static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
452 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
455 static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
457 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
460 static void if_usb_receive_fwload(struct urb *urb)
462 struct if_usb_card *cardp = urb->context;
463 struct sk_buff *skb = cardp->rx_skb;
464 struct fwsyncheader *syncfwheader;
465 struct bootcmdresp bcmdresp;
467 lbtf_deb_enter(LBTF_DEB_USB);
468 if (urb->status) {
469 lbtf_deb_usbd(&cardp->udev->dev,
470 "URB status is failed during fw load\n");
471 kfree_skb(skb);
472 lbtf_deb_leave(LBTF_DEB_USB);
473 return;
476 if (cardp->fwdnldover) {
477 __le32 *tmp = (__le32 *)(skb->data);
479 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
480 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
481 /* Firmware ready event received */
482 pr_info("Firmware ready event received\n");
483 wake_up(&cardp->fw_wq);
484 } else {
485 lbtf_deb_usb("Waiting for confirmation; got %x %x\n",
486 le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
487 if_usb_submit_rx_urb_fwload(cardp);
489 kfree_skb(skb);
490 lbtf_deb_leave(LBTF_DEB_USB);
491 return;
493 if (cardp->bootcmdresp <= 0) {
494 memcpy(&bcmdresp, skb->data, sizeof(bcmdresp));
496 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
497 kfree_skb(skb);
498 if_usb_submit_rx_urb_fwload(cardp);
499 cardp->bootcmdresp = 1;
500 /* Received valid boot command response */
501 lbtf_deb_usbd(&cardp->udev->dev,
502 "Received valid boot command response\n");
503 lbtf_deb_leave(LBTF_DEB_USB);
504 return;
506 if (bcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
507 if (bcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
508 bcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
509 bcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
510 if (!cardp->bootcmdresp)
511 pr_info("Firmware already seems alive; resetting\n");
512 cardp->bootcmdresp = -1;
513 } else {
514 pr_info("boot cmd response wrong magic number (0x%x)\n",
515 le32_to_cpu(bcmdresp.magic));
517 } else if (bcmdresp.cmd != BOOT_CMD_FW_BY_USB) {
518 pr_info("boot cmd response cmd_tag error (%d)\n",
519 bcmdresp.cmd);
520 } else if (bcmdresp.result != BOOT_CMD_RESP_OK) {
521 pr_info("boot cmd response result error (%d)\n",
522 bcmdresp.result);
523 } else {
524 cardp->bootcmdresp = 1;
525 lbtf_deb_usbd(&cardp->udev->dev,
526 "Received valid boot command response\n");
529 kfree_skb(skb);
530 if_usb_submit_rx_urb_fwload(cardp);
531 lbtf_deb_leave(LBTF_DEB_USB);
532 return;
535 syncfwheader = kmemdup(skb->data, sizeof(struct fwsyncheader),
536 GFP_ATOMIC);
537 if (!syncfwheader) {
538 lbtf_deb_usbd(&cardp->udev->dev,
539 "Failure to allocate syncfwheader\n");
540 kfree_skb(skb);
541 lbtf_deb_leave(LBTF_DEB_USB);
542 return;
545 if (!syncfwheader->cmd) {
546 lbtf_deb_usb2(&cardp->udev->dev,
547 "FW received Blk with correct CRC\n");
548 lbtf_deb_usb2(&cardp->udev->dev,
549 "FW received Blk seqnum = %d\n",
550 le32_to_cpu(syncfwheader->seqnum));
551 cardp->CRC_OK = 1;
552 } else {
553 lbtf_deb_usbd(&cardp->udev->dev,
554 "FW received Blk with CRC error\n");
555 cardp->CRC_OK = 0;
558 kfree_skb(skb);
560 /* reschedule timer for 200ms hence */
561 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
563 if (cardp->fwfinalblk) {
564 cardp->fwdnldover = 1;
565 goto exit;
568 if_usb_send_fw_pkt(cardp);
570 exit:
571 if_usb_submit_rx_urb_fwload(cardp);
573 kfree(syncfwheader);
575 lbtf_deb_leave(LBTF_DEB_USB);
578 #define MRVDRV_MIN_PKT_LEN 30
580 static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
581 struct if_usb_card *cardp,
582 struct lbtf_private *priv)
584 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
585 || recvlength < MRVDRV_MIN_PKT_LEN) {
586 lbtf_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
587 kfree_skb(skb);
588 return;
591 skb_put(skb, recvlength);
592 skb_pull(skb, MESSAGE_HEADER_LEN);
593 lbtf_rx(priv, skb);
596 static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
597 struct sk_buff *skb,
598 struct if_usb_card *cardp,
599 struct lbtf_private *priv)
601 unsigned long flags;
603 if (recvlength < MESSAGE_HEADER_LEN ||
604 recvlength > LBS_CMD_BUFFER_SIZE) {
605 lbtf_deb_usbd(&cardp->udev->dev,
606 "The receive buffer is invalid: %d\n", recvlength);
607 kfree_skb(skb);
608 return;
611 spin_lock_irqsave(&priv->driver_lock, flags);
612 memcpy(priv->cmd_resp_buff, recvbuff + MESSAGE_HEADER_LEN,
613 recvlength - MESSAGE_HEADER_LEN);
614 kfree_skb(skb);
615 lbtf_cmd_response_rx(priv);
616 spin_unlock_irqrestore(&priv->driver_lock, flags);
620 * if_usb_receive - read data received from the device.
622 * @urb pointer to struct urb
624 static void if_usb_receive(struct urb *urb)
626 struct if_usb_card *cardp = urb->context;
627 struct sk_buff *skb = cardp->rx_skb;
628 struct lbtf_private *priv = cardp->priv;
629 int recvlength = urb->actual_length;
630 uint8_t *recvbuff = NULL;
631 uint32_t recvtype = 0;
632 __le32 *pkt = (__le32 *) skb->data;
634 lbtf_deb_enter(LBTF_DEB_USB);
636 if (recvlength) {
637 if (urb->status) {
638 lbtf_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
639 urb->status);
640 kfree_skb(skb);
641 goto setup_for_next;
644 recvbuff = skb->data;
645 recvtype = le32_to_cpu(pkt[0]);
646 lbtf_deb_usbd(&cardp->udev->dev,
647 "Recv length = 0x%x, Recv type = 0x%X\n",
648 recvlength, recvtype);
649 } else if (urb->status) {
650 kfree_skb(skb);
651 lbtf_deb_leave(LBTF_DEB_USB);
652 return;
655 switch (recvtype) {
656 case CMD_TYPE_DATA:
657 process_cmdtypedata(recvlength, skb, cardp, priv);
658 break;
660 case CMD_TYPE_REQUEST:
661 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
662 break;
664 case CMD_TYPE_INDICATION:
666 /* Event cause handling */
667 u32 event_cause = le32_to_cpu(pkt[1]);
668 lbtf_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n",
669 event_cause);
671 /* Icky undocumented magic special case */
672 if (event_cause & 0xffff0000) {
673 u16 tmp;
674 u8 retrycnt;
675 u8 failure;
677 tmp = event_cause >> 16;
678 retrycnt = tmp & 0x00ff;
679 failure = (tmp & 0xff00) >> 8;
680 lbtf_send_tx_feedback(priv, retrycnt, failure);
681 } else if (event_cause == LBTF_EVENT_BCN_SENT)
682 lbtf_bcn_sent(priv);
683 else
684 lbtf_deb_usbd(&cardp->udev->dev,
685 "Unsupported notification %d received\n",
686 event_cause);
687 kfree_skb(skb);
688 break;
690 default:
691 lbtf_deb_usbd(&cardp->udev->dev,
692 "libertastf: unknown command type 0x%X\n", recvtype);
693 kfree_skb(skb);
694 break;
697 setup_for_next:
698 if_usb_submit_rx_urb(cardp);
699 lbtf_deb_leave(LBTF_DEB_USB);
703 * if_usb_host_to_card - Download data to the device
705 * @priv pointer to struct lbtf_private structure
706 * @type type of data
707 * @buf pointer to data buffer
708 * @len number of bytes
710 * Returns: 0 on success, nonzero otherwise
712 static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type,
713 uint8_t *payload, uint16_t nb)
715 struct if_usb_card *cardp = priv->card;
716 u8 data = 0;
718 lbtf_deb_usbd(&cardp->udev->dev, "*** type = %u\n", type);
719 lbtf_deb_usbd(&cardp->udev->dev, "size after = %d\n", nb);
721 if (type == MVMS_CMD) {
722 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
723 } else {
724 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
725 data = 1;
728 memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
730 return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN,
731 data);
735 * if_usb_issue_boot_command - Issue boot command to Boot2.
737 * @ivalue 1 boots from FW by USB-Download, 2 boots from FW in EEPROM.
739 * Returns: 0
741 static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
743 struct bootcmd *bootcmd = cardp->ep_out_buf;
745 /* Prepare command */
746 bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
747 bootcmd->cmd = ivalue;
748 memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
750 /* Issue command */
751 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd), 0);
753 return 0;
758 * check_fwfile_format - Check the validity of Boot2/FW image.
760 * @data pointer to image
761 * @totlen image length
763 * Returns: 0 if the image is valid, nonzero otherwise.
765 static int check_fwfile_format(const u8 *data, u32 totlen)
767 u32 bincmd, exit;
768 u32 blksize, offset, len;
769 int ret;
771 ret = 1;
772 exit = len = 0;
774 do {
775 struct fwheader *fwh = (void *) data;
777 bincmd = le32_to_cpu(fwh->dnldcmd);
778 blksize = le32_to_cpu(fwh->datalength);
779 switch (bincmd) {
780 case FW_HAS_DATA_TO_RECV:
781 offset = sizeof(struct fwheader) + blksize;
782 data += offset;
783 len += offset;
784 if (len >= totlen)
785 exit = 1;
786 break;
787 case FW_HAS_LAST_BLOCK:
788 exit = 1;
789 ret = 0;
790 break;
791 default:
792 exit = 1;
793 break;
795 } while (!exit);
797 if (ret)
798 pr_err("firmware file format check FAIL\n");
799 else
800 lbtf_deb_fw("firmware file format check PASS\n");
802 return ret;
806 static int if_usb_prog_firmware(struct lbtf_private *priv)
808 struct if_usb_card *cardp = priv->card;
809 int i = 0;
810 static int reset_count = 10;
811 int ret = 0;
813 lbtf_deb_enter(LBTF_DEB_USB);
815 cardp->priv = priv;
817 kernel_param_lock(THIS_MODULE);
818 ret = request_firmware(&cardp->fw, lbtf_fw_name, &cardp->udev->dev);
819 if (ret < 0) {
820 pr_err("request_firmware() failed with %#x\n", ret);
821 pr_err("firmware %s not found\n", lbtf_fw_name);
822 kernel_param_unlock(THIS_MODULE);
823 goto done;
825 kernel_param_unlock(THIS_MODULE);
827 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
828 goto release_fw;
830 restart:
831 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
832 lbtf_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
833 ret = -1;
834 goto release_fw;
837 cardp->bootcmdresp = 0;
838 do {
839 int j = 0;
840 i++;
841 /* Issue Boot command = 1, Boot from Download-FW */
842 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
843 /* wait for command response */
844 do {
845 j++;
846 msleep_interruptible(100);
847 } while (cardp->bootcmdresp == 0 && j < 10);
848 } while (cardp->bootcmdresp == 0 && i < 5);
850 if (cardp->bootcmdresp <= 0) {
851 if (--reset_count >= 0) {
852 if_usb_reset_device(priv);
853 goto restart;
855 return -1;
858 i = 0;
860 cardp->totalbytes = 0;
861 cardp->fwlastblksent = 0;
862 cardp->CRC_OK = 1;
863 cardp->fwdnldover = 0;
864 cardp->fwseqnum = -1;
865 cardp->totalbytes = 0;
866 cardp->fwfinalblk = 0;
868 /* Send the first firmware packet... */
869 if_usb_send_fw_pkt(cardp);
871 /* ... and wait for the process to complete */
872 wait_event_interruptible(cardp->fw_wq, cardp->priv->surpriseremoved ||
873 cardp->fwdnldover);
875 del_timer_sync(&cardp->fw_timeout);
876 usb_kill_urb(cardp->rx_urb);
878 if (!cardp->fwdnldover) {
879 pr_info("failed to load fw, resetting device!\n");
880 if (--reset_count >= 0) {
881 if_usb_reset_device(priv);
882 goto restart;
885 pr_info("FW download failure, time = %d ms\n", i * 100);
886 ret = -1;
887 goto release_fw;
890 release_fw:
891 release_firmware(cardp->fw);
892 cardp->fw = NULL;
894 if_usb_setup_firmware(cardp->priv);
896 done:
897 lbtf_deb_leave_args(LBTF_DEB_USB, "ret %d", ret);
898 return ret;
902 #define if_usb_suspend NULL
903 #define if_usb_resume NULL
905 static struct usb_driver if_usb_driver = {
906 .name = DRV_NAME,
907 .probe = if_usb_probe,
908 .disconnect = if_usb_disconnect,
909 .id_table = if_usb_table,
910 .suspend = if_usb_suspend,
911 .resume = if_usb_resume,
912 .disable_hub_initiated_lpm = 1,
915 module_usb_driver(if_usb_driver);
917 MODULE_DESCRIPTION("8388 USB WLAN Thinfirm Driver");
918 MODULE_AUTHOR("Cozybit Inc.");
919 MODULE_LICENSE("GPL");