2 * Copyright (C) 2008, cozybit Inc.
3 * Copyright (C) 2003-2006, Marvell International Ltd.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 #include <linux/delay.h>
11 #include <linux/moduleparam.h>
12 #include <linux/firmware.h>
13 #include <linux/netdevice.h>
14 #include <linux/usb.h>
16 #define DRV_NAME "lbtf_usb"
18 #include "libertas_tf.h"
21 #define MESSAGE_HEADER_LEN 4
23 static char *lbtf_fw_name
= "lbtf_usb.bin";
24 module_param_named(fw_name
, lbtf_fw_name
, charp
, 0644);
26 static struct usb_device_id if_usb_table
[] = {
27 /* Enter the device signature inside */
28 { USB_DEVICE(0x1286, 0x2001) },
29 { USB_DEVICE(0x05a3, 0x8388) },
30 {} /* Terminating entry */
33 MODULE_DEVICE_TABLE(usb
, if_usb_table
);
35 static void if_usb_receive(struct urb
*urb
);
36 static void if_usb_receive_fwload(struct urb
*urb
);
37 static int if_usb_prog_firmware(struct if_usb_card
*cardp
);
38 static int if_usb_host_to_card(struct lbtf_private
*priv
, uint8_t type
,
39 uint8_t *payload
, uint16_t nb
);
40 static int usb_tx_block(struct if_usb_card
*cardp
, uint8_t *payload
,
41 uint16_t nb
, u8 data
);
42 static void if_usb_free(struct if_usb_card
*cardp
);
43 static int if_usb_submit_rx_urb(struct if_usb_card
*cardp
);
44 static int if_usb_reset_device(struct if_usb_card
*cardp
);
47 * if_usb_wrike_bulk_callback - call back to handle URB status
49 * @param urb pointer to urb structure
51 static void if_usb_write_bulk_callback(struct urb
*urb
)
54 printk(KERN_INFO
"libertastf: URB in failure status: %d\n",
59 * if_usb_free - free tx/rx urb, skb and rx buffer
61 * @param cardp pointer if_usb_card
63 static void if_usb_free(struct if_usb_card
*cardp
)
65 /* Unlink tx & rx urb */
66 usb_kill_urb(cardp
->tx_urb
);
67 usb_kill_urb(cardp
->rx_urb
);
68 usb_kill_urb(cardp
->cmd_urb
);
70 usb_free_urb(cardp
->tx_urb
);
73 usb_free_urb(cardp
->rx_urb
);
76 usb_free_urb(cardp
->cmd_urb
);
77 cardp
->cmd_urb
= NULL
;
79 kfree(cardp
->ep_out_buf
);
80 cardp
->ep_out_buf
= NULL
;
83 static void if_usb_setup_firmware(struct lbtf_private
*priv
)
85 struct if_usb_card
*cardp
= priv
->card
;
86 struct cmd_ds_set_boot2_ver b2_cmd
;
88 if_usb_submit_rx_urb(cardp
);
89 b2_cmd
.hdr
.size
= cpu_to_le16(sizeof(b2_cmd
));
91 b2_cmd
.version
= cardp
->boot2_version
;
93 if (lbtf_cmd_with_response(priv
, CMD_SET_BOOT2_VER
, &b2_cmd
))
94 printk(KERN_INFO
"libertastf: setting boot2 version failed\n");
97 static void if_usb_fw_timeo(unsigned long priv
)
99 struct if_usb_card
*cardp
= (void *)priv
;
101 if (!cardp
->fwdnldover
)
102 /* Download timed out */
103 cardp
->priv
->surpriseremoved
= 1;
104 wake_up(&cardp
->fw_wq
);
108 * if_usb_probe - sets the configuration values
110 * @ifnum interface number
111 * @id pointer to usb_device_id
113 * Returns: 0 on success, error code on failure
115 static int if_usb_probe(struct usb_interface
*intf
,
116 const struct usb_device_id
*id
)
118 struct usb_device
*udev
;
119 struct usb_host_interface
*iface_desc
;
120 struct usb_endpoint_descriptor
*endpoint
;
121 struct lbtf_private
*priv
;
122 struct if_usb_card
*cardp
;
125 udev
= interface_to_usbdev(intf
);
127 cardp
= kzalloc(sizeof(struct if_usb_card
), GFP_KERNEL
);
131 setup_timer(&cardp
->fw_timeout
, if_usb_fw_timeo
, (unsigned long)cardp
);
132 init_waitqueue_head(&cardp
->fw_wq
);
135 iface_desc
= intf
->cur_altsetting
;
137 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
138 endpoint
= &iface_desc
->endpoint
[i
].desc
;
139 if (usb_endpoint_is_bulk_in(endpoint
)) {
141 le16_to_cpu(endpoint
->wMaxPacketSize
);
142 cardp
->ep_in
= usb_endpoint_num(endpoint
);
143 } else if (usb_endpoint_is_bulk_out(endpoint
)) {
145 le16_to_cpu(endpoint
->wMaxPacketSize
);
146 cardp
->ep_out
= usb_endpoint_num(endpoint
);
149 if (!cardp
->ep_out_size
|| !cardp
->ep_in_size
)
150 /* Endpoints not found */
153 cardp
->rx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
157 cardp
->tx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
161 cardp
->cmd_urb
= usb_alloc_urb(0, GFP_KERNEL
);
165 cardp
->ep_out_buf
= kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE
,
167 if (!cardp
->ep_out_buf
)
170 priv
= lbtf_add_card(cardp
, &udev
->dev
);
176 priv
->hw_host_to_card
= if_usb_host_to_card
;
177 priv
->hw_prog_firmware
= if_usb_prog_firmware
;
178 priv
->hw_reset_device
= if_usb_reset_device
;
179 cardp
->boot2_version
= udev
->descriptor
.bcdDevice
;
182 usb_set_intfdata(intf
, cardp
);
193 * if_usb_disconnect - free resource and cleanup
195 * @intf USB interface structure
197 static void if_usb_disconnect(struct usb_interface
*intf
)
199 struct if_usb_card
*cardp
= usb_get_intfdata(intf
);
200 struct lbtf_private
*priv
= (struct lbtf_private
*) cardp
->priv
;
202 if_usb_reset_device(cardp
);
205 lbtf_remove_card(priv
);
207 /* Unlink and free urb */
210 usb_set_intfdata(intf
, NULL
);
211 usb_put_dev(interface_to_usbdev(intf
));
215 * if_usb_send_fw_pkt - This function downloads the FW
217 * @priv pointer to struct lbtf_private
221 static int if_usb_send_fw_pkt(struct if_usb_card
*cardp
)
223 struct fwdata
*fwdata
= cardp
->ep_out_buf
;
224 u8
*firmware
= (u8
*) cardp
->fw
->data
;
226 /* If we got a CRC failure on the last block, back
228 if (!cardp
->CRC_OK
) {
229 cardp
->totalbytes
= cardp
->fwlastblksent
;
233 /* struct fwdata (which we sent to the card) has an
234 extra __le32 field in between the header and the data,
235 which is not in the struct fwheader in the actual
236 firmware binary. Insert the seqnum in the middle... */
237 memcpy(&fwdata
->hdr
, &firmware
[cardp
->totalbytes
],
238 sizeof(struct fwheader
));
240 cardp
->fwlastblksent
= cardp
->totalbytes
;
241 cardp
->totalbytes
+= sizeof(struct fwheader
);
243 memcpy(fwdata
->data
, &firmware
[cardp
->totalbytes
],
244 le32_to_cpu(fwdata
->hdr
.datalength
));
246 fwdata
->seqnum
= cpu_to_le32(++cardp
->fwseqnum
);
247 cardp
->totalbytes
+= le32_to_cpu(fwdata
->hdr
.datalength
);
249 usb_tx_block(cardp
, cardp
->ep_out_buf
, sizeof(struct fwdata
) +
250 le32_to_cpu(fwdata
->hdr
.datalength
), 0);
252 if (fwdata
->hdr
.dnldcmd
== cpu_to_le32(FW_HAS_LAST_BLOCK
))
253 /* Host has finished FW downloading
254 * Donwloading FW JUMP BLOCK
256 cardp
->fwfinalblk
= 1;
261 static int if_usb_reset_device(struct if_usb_card
*cardp
)
263 struct cmd_ds_802_11_reset
*cmd
= cardp
->ep_out_buf
+ 4;
266 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_REQUEST
);
268 cmd
->hdr
.command
= cpu_to_le16(CMD_802_11_RESET
);
269 cmd
->hdr
.size
= cpu_to_le16(sizeof(struct cmd_ds_802_11_reset
));
270 cmd
->hdr
.result
= cpu_to_le16(0);
271 cmd
->hdr
.seqnum
= cpu_to_le16(0x5a5a);
272 cmd
->action
= cpu_to_le16(CMD_ACT_HALT
);
273 usb_tx_block(cardp
, cardp
->ep_out_buf
,
274 4 + sizeof(struct cmd_ds_802_11_reset
), 0);
277 ret
= usb_reset_device(cardp
->udev
);
282 EXPORT_SYMBOL_GPL(if_usb_reset_device
);
285 * usb_tx_block - transfer data to the device
287 * @priv pointer to struct lbtf_private
288 * @payload pointer to payload data
290 * @data non-zero for data, zero for commands
292 * Returns: 0 on success, nonzero otherwise.
294 static int usb_tx_block(struct if_usb_card
*cardp
, uint8_t *payload
,
295 uint16_t nb
, u8 data
)
299 /* check if device is removed */
300 if (cardp
->priv
->surpriseremoved
)
306 urb
= cardp
->cmd_urb
;
308 usb_fill_bulk_urb(urb
, cardp
->udev
,
309 usb_sndbulkpipe(cardp
->udev
,
311 payload
, nb
, if_usb_write_bulk_callback
, cardp
);
313 urb
->transfer_flags
|= URB_ZERO_PACKET
;
315 if (usb_submit_urb(urb
, GFP_ATOMIC
))
320 static int __if_usb_submit_rx_urb(struct if_usb_card
*cardp
,
321 void (*callbackfn
)(struct urb
*urb
))
325 skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
);
331 /* Fill the receive configuration URB and initialise the Rx call back */
332 usb_fill_bulk_urb(cardp
->rx_urb
, cardp
->udev
,
333 usb_rcvbulkpipe(cardp
->udev
, cardp
->ep_in
),
334 skb_tail_pointer(skb
),
335 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
, callbackfn
, cardp
);
337 cardp
->rx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
339 if (usb_submit_urb(cardp
->rx_urb
, GFP_ATOMIC
)) {
341 cardp
->rx_skb
= NULL
;
347 static int if_usb_submit_rx_urb_fwload(struct if_usb_card
*cardp
)
349 return __if_usb_submit_rx_urb(cardp
, &if_usb_receive_fwload
);
352 static int if_usb_submit_rx_urb(struct if_usb_card
*cardp
)
354 return __if_usb_submit_rx_urb(cardp
, &if_usb_receive
);
357 static void if_usb_receive_fwload(struct urb
*urb
)
359 struct if_usb_card
*cardp
= urb
->context
;
360 struct sk_buff
*skb
= cardp
->rx_skb
;
361 struct fwsyncheader
*syncfwheader
;
362 struct bootcmdresp bcmdresp
;
369 if (cardp
->fwdnldover
) {
370 __le32
*tmp
= (__le32
*)(skb
->data
);
372 if (tmp
[0] == cpu_to_le32(CMD_TYPE_INDICATION
) &&
373 tmp
[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY
))
374 /* Firmware ready event received */
375 wake_up(&cardp
->fw_wq
);
377 if_usb_submit_rx_urb_fwload(cardp
);
381 if (cardp
->bootcmdresp
<= 0) {
382 memcpy(&bcmdresp
, skb
->data
, sizeof(bcmdresp
));
384 if (le16_to_cpu(cardp
->udev
->descriptor
.bcdDevice
) < 0x3106) {
386 if_usb_submit_rx_urb_fwload(cardp
);
387 cardp
->bootcmdresp
= 1;
388 /* Received valid boot command response */
391 if (bcmdresp
.magic
!= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
)) {
392 if (bcmdresp
.magic
== cpu_to_le32(CMD_TYPE_REQUEST
) ||
393 bcmdresp
.magic
== cpu_to_le32(CMD_TYPE_DATA
) ||
394 bcmdresp
.magic
== cpu_to_le32(CMD_TYPE_INDICATION
))
395 cardp
->bootcmdresp
= -1;
396 } else if (bcmdresp
.cmd
== BOOT_CMD_FW_BY_USB
&&
397 bcmdresp
.result
== BOOT_CMD_RESP_OK
)
398 cardp
->bootcmdresp
= 1;
401 if_usb_submit_rx_urb_fwload(cardp
);
405 syncfwheader
= kmalloc(sizeof(struct fwsyncheader
), GFP_ATOMIC
);
411 memcpy(syncfwheader
, skb
->data
, sizeof(struct fwsyncheader
));
413 if (!syncfwheader
->cmd
)
419 /* reschedule timer for 200ms hence */
420 mod_timer(&cardp
->fw_timeout
, jiffies
+ (HZ
/5));
422 if (cardp
->fwfinalblk
) {
423 cardp
->fwdnldover
= 1;
427 if_usb_send_fw_pkt(cardp
);
430 if_usb_submit_rx_urb_fwload(cardp
);
437 #define MRVDRV_MIN_PKT_LEN 30
439 static inline void process_cmdtypedata(int recvlength
, struct sk_buff
*skb
,
440 struct if_usb_card
*cardp
,
441 struct lbtf_private
*priv
)
443 if (recvlength
> MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
+ MESSAGE_HEADER_LEN
444 || recvlength
< MRVDRV_MIN_PKT_LEN
) {
449 skb_put(skb
, recvlength
);
450 skb_pull(skb
, MESSAGE_HEADER_LEN
);
454 static inline void process_cmdrequest(int recvlength
, uint8_t *recvbuff
,
456 struct if_usb_card
*cardp
,
457 struct lbtf_private
*priv
)
459 if (recvlength
> LBS_CMD_BUFFER_SIZE
) {
464 BUG_ON(!in_interrupt());
466 spin_lock(&priv
->driver_lock
);
467 memcpy(priv
->cmd_resp_buff
, recvbuff
+ MESSAGE_HEADER_LEN
,
468 recvlength
- MESSAGE_HEADER_LEN
);
470 lbtf_cmd_response_rx(priv
);
471 spin_unlock(&priv
->driver_lock
);
475 * if_usb_receive - read data received from the device.
477 * @urb pointer to struct urb
479 static void if_usb_receive(struct urb
*urb
)
481 struct if_usb_card
*cardp
= urb
->context
;
482 struct sk_buff
*skb
= cardp
->rx_skb
;
483 struct lbtf_private
*priv
= cardp
->priv
;
484 int recvlength
= urb
->actual_length
;
485 uint8_t *recvbuff
= NULL
;
486 uint32_t recvtype
= 0;
487 __le32
*pkt
= (__le32
*) skb
->data
;
495 recvbuff
= skb
->data
;
496 recvtype
= le32_to_cpu(pkt
[0]);
497 } else if (urb
->status
) {
504 process_cmdtypedata(recvlength
, skb
, cardp
, priv
);
507 case CMD_TYPE_REQUEST
:
508 process_cmdrequest(recvlength
, recvbuff
, skb
, cardp
, priv
);
511 case CMD_TYPE_INDICATION
:
513 /* Event cause handling */
514 u32 event_cause
= le32_to_cpu(pkt
[1]);
516 /* Icky undocumented magic special case */
517 if (event_cause
& 0xffff0000) {
522 tmp
= event_cause
>> 16;
523 retrycnt
= tmp
& 0x00ff;
524 failure
= (tmp
& 0xff00) >> 8;
525 lbtf_send_tx_feedback(priv
, retrycnt
, failure
);
526 } else if (event_cause
== LBTF_EVENT_BCN_SENT
)
530 "Unsupported notification %d received\n",
536 printk(KERN_DEBUG
"libertastf: unknown command type 0x%X\n",
543 if_usb_submit_rx_urb(cardp
);
547 * if_usb_host_to_card - Download data to the device
549 * @priv pointer to struct lbtf_private structure
551 * @buf pointer to data buffer
552 * @len number of bytes
554 * Returns: 0 on success, nonzero otherwise
556 static int if_usb_host_to_card(struct lbtf_private
*priv
, uint8_t type
,
557 uint8_t *payload
, uint16_t nb
)
559 struct if_usb_card
*cardp
= priv
->card
;
562 if (type
== MVMS_CMD
) {
563 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_REQUEST
);
565 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_DATA
);
569 memcpy((cardp
->ep_out_buf
+ MESSAGE_HEADER_LEN
), payload
, nb
);
571 return usb_tx_block(cardp
, cardp
->ep_out_buf
, nb
+ MESSAGE_HEADER_LEN
,
576 * if_usb_issue_boot_command - Issue boot command to Boot2.
578 * @ivalue 1 boots from FW by USB-Download, 2 boots from FW in EEPROM.
582 static int if_usb_issue_boot_command(struct if_usb_card
*cardp
, int ivalue
)
584 struct bootcmd
*bootcmd
= cardp
->ep_out_buf
;
586 /* Prepare command */
587 bootcmd
->magic
= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
);
588 bootcmd
->cmd
= ivalue
;
589 memset(bootcmd
->pad
, 0, sizeof(bootcmd
->pad
));
592 usb_tx_block(cardp
, cardp
->ep_out_buf
, sizeof(*bootcmd
), 0);
599 * check_fwfile_format - Check the validity of Boot2/FW image.
601 * @data pointer to image
602 * @totlen image length
604 * Returns: 0 if the image is valid, nonzero otherwise.
606 static int check_fwfile_format(const u8
*data
, u32 totlen
)
609 u32 blksize
, offset
, len
;
616 struct fwheader
*fwh
= (void *) data
;
618 bincmd
= le32_to_cpu(fwh
->dnldcmd
);
619 blksize
= le32_to_cpu(fwh
->datalength
);
621 case FW_HAS_DATA_TO_RECV
:
622 offset
= sizeof(struct fwheader
) + blksize
;
628 case FW_HAS_LAST_BLOCK
:
640 "libertastf: firmware file format check failed\n");
645 static int if_usb_prog_firmware(struct if_usb_card
*cardp
)
648 static int reset_count
= 10;
651 ret
= request_firmware(&cardp
->fw
, lbtf_fw_name
, &cardp
->udev
->dev
);
653 printk(KERN_INFO
"libertastf: firmware %s not found\n",
658 if (check_fwfile_format(cardp
->fw
->data
, cardp
->fw
->size
))
662 if (if_usb_submit_rx_urb_fwload(cardp
) < 0) {
667 cardp
->bootcmdresp
= 0;
671 /* Issue Boot command = 1, Boot from Download-FW */
672 if_usb_issue_boot_command(cardp
, BOOT_CMD_FW_BY_USB
);
673 /* wait for command response */
676 msleep_interruptible(100);
677 } while (cardp
->bootcmdresp
== 0 && j
< 10);
678 } while (cardp
->bootcmdresp
== 0 && i
< 5);
680 if (cardp
->bootcmdresp
<= 0) {
681 if (--reset_count
>= 0) {
682 if_usb_reset_device(cardp
);
690 cardp
->totalbytes
= 0;
691 cardp
->fwlastblksent
= 0;
693 cardp
->fwdnldover
= 0;
694 cardp
->fwseqnum
= -1;
695 cardp
->totalbytes
= 0;
696 cardp
->fwfinalblk
= 0;
698 /* Send the first firmware packet... */
699 if_usb_send_fw_pkt(cardp
);
701 /* ... and wait for the process to complete */
702 wait_event_interruptible(cardp
->fw_wq
, cardp
->priv
->surpriseremoved
||
705 del_timer_sync(&cardp
->fw_timeout
);
706 usb_kill_urb(cardp
->rx_urb
);
708 if (!cardp
->fwdnldover
) {
709 printk(KERN_INFO
"libertastf: failed to load fw,"
710 " resetting device!\n");
711 if (--reset_count
>= 0) {
712 if_usb_reset_device(cardp
);
716 printk(KERN_INFO
"libertastf: fw download failure\n");
721 cardp
->priv
->fw_ready
= 1;
724 release_firmware(cardp
->fw
);
727 if_usb_setup_firmware(cardp
->priv
);
732 EXPORT_SYMBOL_GPL(if_usb_prog_firmware
);
735 #define if_usb_suspend NULL
736 #define if_usb_resume NULL
738 static struct usb_driver if_usb_driver
= {
740 .probe
= if_usb_probe
,
741 .disconnect
= if_usb_disconnect
,
742 .id_table
= if_usb_table
,
743 .suspend
= if_usb_suspend
,
744 .resume
= if_usb_resume
,
747 static int __init
if_usb_init_module(void)
751 ret
= usb_register(&if_usb_driver
);
755 static void __exit
if_usb_exit_module(void)
757 usb_deregister(&if_usb_driver
);
760 module_init(if_usb_init_module
);
761 module_exit(if_usb_exit_module
);
763 MODULE_DESCRIPTION("8388 USB WLAN Thinfirm Driver");
764 MODULE_AUTHOR("Cozybit Inc.");
765 MODULE_LICENSE("GPL");