1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2008, cozybit Inc.
4 * Copyright (C) 2003-2006, Marvell International Ltd.
6 #define DRV_NAME "lbtf_usb"
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include "libertas_tf.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>
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
);
51 * if_usb_wrike_bulk_callback - call back to handle URB status
53 * @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
);
61 lbtf_deb_usb2(&urb
->dev
->dev
, "URB status is successful\n");
62 lbtf_deb_usb2(&urb
->dev
->dev
, "Actual length transmitted %d\n",
68 * if_usb_free - free tx/rx urb, skb and rx buffer
70 * @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
);
84 usb_free_urb(cardp
->rx_urb
);
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
));
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");
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 * @intf: USB interface structure
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
;
154 lbtf_deb_enter(LBTF_DEB_USB
);
155 udev
= interface_to_usbdev(intf
);
157 cardp
= kzalloc(sizeof(struct if_usb_card
), GFP_KERNEL
);
161 timer_setup(&cardp
->fw_timeout
, if_usb_fw_timeo
, 0);
162 init_waitqueue_head(&cardp
->fw_wq
);
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
)) {
178 le16_to_cpu(endpoint
->wMaxPacketSize
);
179 cardp
->ep_in
= usb_endpoint_num(endpoint
);
181 lbtf_deb_usbd(&udev
->dev
, "in_endpoint = %d\n",
183 lbtf_deb_usbd(&udev
->dev
, "Bulk in size is %d\n",
185 } else if (usb_endpoint_is_bulk_out(endpoint
)) {
187 le16_to_cpu(endpoint
->wMaxPacketSize
);
188 cardp
->ep_out
= usb_endpoint_num(endpoint
);
190 lbtf_deb_usbd(&udev
->dev
, "out_endpoint = %d\n",
192 lbtf_deb_usbd(&udev
->dev
, "Bulk out size is %d\n",
196 if (!cardp
->ep_out_size
|| !cardp
->ep_in_size
) {
197 lbtf_deb_usbd(&udev
->dev
, "Endpoints not found\n");
198 /* Endpoints not found */
202 cardp
->rx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
206 cardp
->tx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
210 cardp
->cmd_urb
= usb_alloc_urb(0, GFP_KERNEL
);
214 cardp
->ep_out_buf
= kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE
,
216 if (!cardp
->ep_out_buf
) {
217 lbtf_deb_usbd(&udev
->dev
, "Could not allocate buffer\n");
221 cardp
->boot2_version
= udev
->descriptor
.bcdDevice
;
222 priv
= lbtf_add_card(cardp
, &udev
->dev
, &if_usb_ops
);
227 usb_set_intfdata(intf
, cardp
);
234 lbtf_deb_leave(LBTF_DEB_MAIN
);
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
);
251 if_usb_reset_device(priv
);
252 lbtf_remove_card(priv
);
255 /* Unlink and free urb */
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 * @cardp: pointer if_usb_card
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
280 if (!cardp
->CRC_OK
) {
281 cardp
->totalbytes
= cardp
->fwlastblksent
;
285 lbtf_deb_usb2(&cardp
->udev
->dev
, "totalbytes = %d\n",
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",
329 lbtf_deb_leave(LBTF_DEB_FW
);
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;
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);
352 ret
= usb_reset_device(cardp
->udev
);
355 lbtf_deb_leave_args(LBTF_DEB_USB
, "ret %d", ret
);
361 * usb_tx_block - transfer data to the device
363 * @cardp: pointer if_usb_card
364 * @payload: pointer to payload data
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
)
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");
386 urb
= cardp
->cmd_urb
;
388 usb_fill_bulk_urb(urb
, cardp
->udev
,
389 usb_sndbulkpipe(cardp
->udev
,
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
);
401 lbtf_deb_usb2(&cardp
->udev
->dev
, "usb_submit_urb success\n");
406 lbtf_deb_leave(LBTF_DEB_USB
);
410 static int __if_usb_submit_rx_urb(struct if_usb_card
*cardp
,
411 void (*callbackfn
)(struct urb
*urb
))
416 lbtf_deb_enter(LBTF_DEB_USB
);
418 skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
);
420 pr_err("No free skb\n");
421 lbtf_deb_leave(LBTF_DEB_USB
);
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",
435 ret
= usb_submit_urb(cardp
->rx_urb
, GFP_ATOMIC
);
437 lbtf_deb_usbd(&cardp
->udev
->dev
,
438 "Submit Rx URB failed: %d\n", ret
);
440 cardp
->rx_skb
= NULL
;
441 lbtf_deb_leave(LBTF_DEB_USB
);
444 lbtf_deb_usb2(&cardp
->udev
->dev
, "Submit Rx URB success\n");
445 lbtf_deb_leave(LBTF_DEB_USB
);
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
);
469 lbtf_deb_usbd(&cardp
->udev
->dev
,
470 "URB status is failed during fw load\n");
472 lbtf_deb_leave(LBTF_DEB_USB
);
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
);
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
);
490 lbtf_deb_leave(LBTF_DEB_USB
);
493 if (cardp
->bootcmdresp
<= 0) {
494 memcpy(&bcmdresp
, skb
->data
, sizeof(bcmdresp
));
496 if (le16_to_cpu(cardp
->udev
->descriptor
.bcdDevice
) < 0x3106) {
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
);
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;
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",
520 } else if (bcmdresp
.result
!= BOOT_CMD_RESP_OK
) {
521 pr_info("boot cmd response result error (%d)\n",
524 cardp
->bootcmdresp
= 1;
525 lbtf_deb_usbd(&cardp
->udev
->dev
,
526 "Received valid boot command response\n");
530 if_usb_submit_rx_urb_fwload(cardp
);
531 lbtf_deb_leave(LBTF_DEB_USB
);
535 syncfwheader
= kmemdup(skb
->data
, sizeof(struct fwsyncheader
),
538 lbtf_deb_usbd(&cardp
->udev
->dev
,
539 "Failure to allocate syncfwheader\n");
541 lbtf_deb_leave(LBTF_DEB_USB
);
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
));
553 lbtf_deb_usbd(&cardp
->udev
->dev
,
554 "FW received Blk with CRC error\n");
560 /* reschedule timer for 200ms hence */
561 mod_timer(&cardp
->fw_timeout
, jiffies
+ (HZ
/5));
563 if (cardp
->fwfinalblk
) {
564 cardp
->fwdnldover
= 1;
568 if_usb_send_fw_pkt(cardp
);
571 if_usb_submit_rx_urb_fwload(cardp
);
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");
591 skb_put(skb
, recvlength
);
592 skb_pull(skb
, MESSAGE_HEADER_LEN
);
596 static inline void process_cmdrequest(int recvlength
, uint8_t *recvbuff
,
598 struct if_usb_card
*cardp
,
599 struct lbtf_private
*priv
)
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
);
611 spin_lock_irqsave(&priv
->driver_lock
, flags
);
612 memcpy(priv
->cmd_resp_buff
, recvbuff
+ MESSAGE_HEADER_LEN
,
613 recvlength
- MESSAGE_HEADER_LEN
);
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
);
638 lbtf_deb_usbd(&cardp
->udev
->dev
, "RX URB failed: %d\n",
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
) {
651 lbtf_deb_leave(LBTF_DEB_USB
);
657 process_cmdtypedata(recvlength
, skb
, cardp
, priv
);
660 case CMD_TYPE_REQUEST
:
661 process_cmdrequest(recvlength
, recvbuff
, skb
, cardp
, priv
);
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",
671 /* Icky undocumented magic special case */
672 if (event_cause
& 0xffff0000) {
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
)
684 lbtf_deb_usbd(&cardp
->udev
->dev
,
685 "Unsupported notification %d received\n",
691 lbtf_deb_usbd(&cardp
->udev
->dev
,
692 "libertastf: unknown command type 0x%X\n", recvtype
);
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 * @payload: pointer to payload buffer
708 * @nb: 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
;
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
);
724 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_DATA
);
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
,
735 * if_usb_issue_boot_command - Issue boot command to Boot2.
737 * @cardp: pointer if_usb_card
738 * @ivalue: 1 boots from FW by USB-Download, 2 boots from FW in EEPROM.
742 static int if_usb_issue_boot_command(struct if_usb_card
*cardp
, int ivalue
)
744 struct bootcmd
*bootcmd
= cardp
->ep_out_buf
;
746 /* Prepare command */
747 bootcmd
->magic
= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
);
748 bootcmd
->cmd
= ivalue
;
749 memset(bootcmd
->pad
, 0, sizeof(bootcmd
->pad
));
752 usb_tx_block(cardp
, cardp
->ep_out_buf
, sizeof(*bootcmd
), 0);
759 * check_fwfile_format - Check the validity of Boot2/FW image.
761 * @data: pointer to image
762 * @totlen: image length
764 * Returns: 0 if the image is valid, nonzero otherwise.
766 static int check_fwfile_format(const u8
*data
, u32 totlen
)
769 u32 blksize
, offset
, len
;
776 struct fwheader
*fwh
= (void *) data
;
778 bincmd
= le32_to_cpu(fwh
->dnldcmd
);
779 blksize
= le32_to_cpu(fwh
->datalength
);
781 case FW_HAS_DATA_TO_RECV
:
782 offset
= sizeof(struct fwheader
) + blksize
;
788 case FW_HAS_LAST_BLOCK
:
799 pr_err("firmware file format check FAIL\n");
801 lbtf_deb_fw("firmware file format check PASS\n");
807 static int if_usb_prog_firmware(struct lbtf_private
*priv
)
809 struct if_usb_card
*cardp
= priv
->card
;
811 static int reset_count
= 10;
814 lbtf_deb_enter(LBTF_DEB_USB
);
818 kernel_param_lock(THIS_MODULE
);
819 ret
= request_firmware(&cardp
->fw
, lbtf_fw_name
, &cardp
->udev
->dev
);
821 pr_err("request_firmware() failed with %#x\n", ret
);
822 pr_err("firmware %s not found\n", lbtf_fw_name
);
823 kernel_param_unlock(THIS_MODULE
);
826 kernel_param_unlock(THIS_MODULE
);
828 if (check_fwfile_format(cardp
->fw
->data
, cardp
->fw
->size
))
832 if (if_usb_submit_rx_urb_fwload(cardp
) < 0) {
833 lbtf_deb_usbd(&cardp
->udev
->dev
, "URB submission is failed\n");
838 cardp
->bootcmdresp
= 0;
842 /* Issue Boot command = 1, Boot from Download-FW */
843 if_usb_issue_boot_command(cardp
, BOOT_CMD_FW_BY_USB
);
844 /* wait for command response */
847 msleep_interruptible(100);
848 } while (cardp
->bootcmdresp
== 0 && j
< 10);
849 } while (cardp
->bootcmdresp
== 0 && i
< 5);
851 if (cardp
->bootcmdresp
<= 0) {
852 if (--reset_count
>= 0) {
853 if_usb_reset_device(priv
);
861 cardp
->totalbytes
= 0;
862 cardp
->fwlastblksent
= 0;
864 cardp
->fwdnldover
= 0;
865 cardp
->fwseqnum
= -1;
866 cardp
->totalbytes
= 0;
867 cardp
->fwfinalblk
= 0;
869 /* Send the first firmware packet... */
870 if_usb_send_fw_pkt(cardp
);
872 /* ... and wait for the process to complete */
873 wait_event_interruptible(cardp
->fw_wq
, cardp
->priv
->surpriseremoved
||
876 del_timer_sync(&cardp
->fw_timeout
);
877 usb_kill_urb(cardp
->rx_urb
);
879 if (!cardp
->fwdnldover
) {
880 pr_info("failed to load fw, resetting device!\n");
881 if (--reset_count
>= 0) {
882 if_usb_reset_device(priv
);
886 pr_info("FW download failure, time = %d ms\n", i
* 100);
892 release_firmware(cardp
->fw
);
895 if_usb_setup_firmware(cardp
->priv
);
898 lbtf_deb_leave_args(LBTF_DEB_USB
, "ret %d", ret
);
903 #define if_usb_suspend NULL
904 #define if_usb_resume NULL
906 static struct usb_driver if_usb_driver
= {
908 .probe
= if_usb_probe
,
909 .disconnect
= if_usb_disconnect
,
910 .id_table
= if_usb_table
,
911 .suspend
= if_usb_suspend
,
912 .resume
= if_usb_resume
,
913 .disable_hub_initiated_lpm
= 1,
916 module_usb_driver(if_usb_driver
);
918 MODULE_DESCRIPTION("8388 USB WLAN Thinfirm Driver");
919 MODULE_AUTHOR("Cozybit Inc.");
920 MODULE_LICENSE("GPL");