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>
9 //#include <asm/olpc.h>
11 #define DRV_NAME "usb8xxx"
21 #define lbs_deb_usb2(...) do { if (INSANEDEBUG) lbs_deb_usbd(__VA_ARGS__); } while (0)
23 #define MESSAGE_HEADER_LEN 4
25 static char *lbs_fw_name
= "usb8388.bin";
26 module_param_named(fw_name
, lbs_fw_name
, charp
, 0644);
28 static struct usb_device_id if_usb_table
[] = {
29 /* Enter the device signature inside */
30 { USB_DEVICE(0x1286, 0x2001) },
31 { USB_DEVICE(0x05a3, 0x8388) },
32 {} /* Terminating entry */
35 MODULE_DEVICE_TABLE(usb
, if_usb_table
);
37 static void if_usb_receive(struct urb
*urb
);
38 static void if_usb_receive_fwload(struct urb
*urb
);
39 static int if_usb_prog_firmware(struct if_usb_card
*cardp
);
40 static int if_usb_host_to_card(struct lbs_private
*priv
, uint8_t type
,
41 uint8_t *payload
, uint16_t nb
);
42 static int usb_tx_block(struct if_usb_card
*cardp
, uint8_t *payload
,
44 static void if_usb_free(struct if_usb_card
*cardp
);
45 static int if_usb_submit_rx_urb(struct if_usb_card
*cardp
);
46 static int if_usb_reset_device(struct if_usb_card
*cardp
);
49 * @brief call back function to handle the status of the URB
50 * @param urb pointer to urb structure
53 static void if_usb_write_bulk_callback(struct urb
*urb
)
55 struct if_usb_card
*cardp
= (struct if_usb_card
*) urb
->context
;
57 /* handle the transmission complete validations */
59 if (urb
->status
== 0) {
60 struct lbs_private
*priv
= cardp
->priv
;
62 lbs_deb_usb2(&urb
->dev
->dev
, "URB status is successful\n");
63 lbs_deb_usb2(&urb
->dev
->dev
, "Actual length transmitted %d\n",
66 /* Used for both firmware TX and regular TX. priv isn't
67 * valid at firmware load time.
70 lbs_host_to_card_done(priv
);
72 /* print the failure status number for debug */
73 lbs_pr_info("URB in failure status: %d\n", urb
->status
);
80 * @brief free tx/rx urb, skb and rx buffer
81 * @param cardp pointer if_usb_card
84 static void if_usb_free(struct if_usb_card
*cardp
)
86 lbs_deb_enter(LBS_DEB_USB
);
88 /* Unlink tx & rx urb */
89 usb_kill_urb(cardp
->tx_urb
);
90 usb_kill_urb(cardp
->rx_urb
);
92 usb_free_urb(cardp
->tx_urb
);
95 usb_free_urb(cardp
->rx_urb
);
98 kfree(cardp
->ep_out_buf
);
99 cardp
->ep_out_buf
= NULL
;
101 lbs_deb_leave(LBS_DEB_USB
);
104 static void if_usb_setup_firmware(struct lbs_private
*priv
)
106 struct if_usb_card
*cardp
= priv
->card
;
107 struct cmd_ds_set_boot2_ver b2_cmd
;
108 struct cmd_ds_802_11_fw_wake_method wake_method
;
110 b2_cmd
.hdr
.size
= cpu_to_le16(sizeof(b2_cmd
));
112 b2_cmd
.version
= cardp
->boot2_version
;
114 if (lbs_cmd_with_response(priv
, CMD_SET_BOOT2_VER
, &b2_cmd
))
115 lbs_deb_usb("Setting boot2 version failed\n");
117 priv
->wol_gpio
= 2; /* Wake via GPIO2... */
118 priv
->wol_gap
= 20; /* ... after 20ms */
119 lbs_host_sleep_cfg(priv
, EHS_WAKE_ON_UNICAST_DATA
);
121 wake_method
.hdr
.size
= cpu_to_le16(sizeof(wake_method
));
122 wake_method
.action
= cpu_to_le16(CMD_ACT_GET
);
123 if (lbs_cmd_with_response(priv
, CMD_802_11_FW_WAKE_METHOD
, &wake_method
)) {
124 lbs_pr_info("Firmware does not seem to support PS mode\n");
126 if (le16_to_cpu(wake_method
.method
) == CMD_WAKE_METHOD_COMMAND_INT
) {
127 lbs_deb_usb("Firmware seems to support PS with wake-via-command\n");
128 priv
->ps_supported
= 1;
130 /* The versions which boot up this way don't seem to
131 work even if we set it to the command interrupt */
132 lbs_pr_info("Firmware doesn't wake via command interrupt; disabling PS mode\n");
137 static void if_usb_fw_timeo(unsigned long priv
)
139 struct if_usb_card
*cardp
= (void *)priv
;
141 if (cardp
->fwdnldover
) {
142 lbs_deb_usb("Download complete, no event. Assuming success\n");
144 lbs_pr_err("Download timed out\n");
145 cardp
->surprise_removed
= 1;
147 wake_up(&cardp
->fw_wq
);
151 * @brief sets the configuration values
152 * @param ifnum interface number
153 * @param id pointer to usb_device_id
154 * @return 0 on success, error code on failure
156 static int if_usb_probe(struct usb_interface
*intf
,
157 const struct usb_device_id
*id
)
159 struct usb_device
*udev
;
160 struct usb_host_interface
*iface_desc
;
161 struct usb_endpoint_descriptor
*endpoint
;
162 struct lbs_private
*priv
;
163 struct if_usb_card
*cardp
;
166 udev
= interface_to_usbdev(intf
);
168 cardp
= kzalloc(sizeof(struct if_usb_card
), GFP_KERNEL
);
170 lbs_pr_err("Out of memory allocating private data.\n");
174 setup_timer(&cardp
->fw_timeout
, if_usb_fw_timeo
, (unsigned long)cardp
);
175 init_waitqueue_head(&cardp
->fw_wq
);
178 iface_desc
= intf
->cur_altsetting
;
180 lbs_deb_usbd(&udev
->dev
, "bcdUSB = 0x%X bDeviceClass = 0x%X"
181 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
182 le16_to_cpu(udev
->descriptor
.bcdUSB
),
183 udev
->descriptor
.bDeviceClass
,
184 udev
->descriptor
.bDeviceSubClass
,
185 udev
->descriptor
.bDeviceProtocol
);
187 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
188 endpoint
= &iface_desc
->endpoint
[i
].desc
;
189 if (usb_endpoint_is_bulk_in(endpoint
)) {
190 cardp
->ep_in_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
191 cardp
->ep_in
= usb_endpoint_num(endpoint
);
193 lbs_deb_usbd(&udev
->dev
, "in_endpoint = %d\n", cardp
->ep_in
);
194 lbs_deb_usbd(&udev
->dev
, "Bulk in size is %d\n", cardp
->ep_in_size
);
196 } else if (usb_endpoint_is_bulk_out(endpoint
)) {
197 cardp
->ep_out_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
198 cardp
->ep_out
= usb_endpoint_num(endpoint
);
200 lbs_deb_usbd(&udev
->dev
, "out_endpoint = %d\n", cardp
->ep_out
);
201 lbs_deb_usbd(&udev
->dev
, "Bulk out size is %d\n", cardp
->ep_out_size
);
204 if (!cardp
->ep_out_size
|| !cardp
->ep_in_size
) {
205 lbs_deb_usbd(&udev
->dev
, "Endpoints not found\n");
208 if (!(cardp
->rx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
209 lbs_deb_usbd(&udev
->dev
, "Rx URB allocation failed\n");
212 if (!(cardp
->tx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
213 lbs_deb_usbd(&udev
->dev
, "Tx URB allocation failed\n");
216 cardp
->ep_out_buf
= kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE
, GFP_KERNEL
);
217 if (!cardp
->ep_out_buf
) {
218 lbs_deb_usbd(&udev
->dev
, "Could not allocate buffer\n");
222 /* Upload firmware */
223 if (if_usb_prog_firmware(cardp
)) {
224 lbs_deb_usbd(&udev
->dev
, "FW upload failed\n");
225 goto err_prog_firmware
;
228 if (!(priv
= lbs_add_card(cardp
, &udev
->dev
)))
229 goto err_prog_firmware
;
232 cardp
->priv
->fw_ready
= 1;
234 priv
->hw_host_to_card
= if_usb_host_to_card
;
235 cardp
->boot2_version
= udev
->descriptor
.bcdDevice
;
237 if_usb_submit_rx_urb(cardp
);
239 if (lbs_start_card(priv
))
242 if_usb_setup_firmware(priv
);
245 usb_set_intfdata(intf
, cardp
);
250 lbs_remove_card(priv
);
252 if_usb_reset_device(cardp
);
261 * @brief free resource and cleanup
262 * @param intf USB interface structure
265 static void if_usb_disconnect(struct usb_interface
*intf
)
267 struct if_usb_card
*cardp
= usb_get_intfdata(intf
);
268 struct lbs_private
*priv
= (struct lbs_private
*) cardp
->priv
;
270 lbs_deb_enter(LBS_DEB_MAIN
);
272 cardp
->surprise_removed
= 1;
275 priv
->surpriseremoved
= 1;
277 lbs_remove_card(priv
);
280 /* Unlink and free urb */
283 usb_set_intfdata(intf
, NULL
);
284 usb_put_dev(interface_to_usbdev(intf
));
286 lbs_deb_leave(LBS_DEB_MAIN
);
290 * @brief This function download FW
291 * @param priv pointer to struct lbs_private
294 static int if_usb_send_fw_pkt(struct if_usb_card
*cardp
)
296 struct fwdata
*fwdata
= cardp
->ep_out_buf
;
297 uint8_t *firmware
= cardp
->fw
->data
;
299 /* If we got a CRC failure on the last block, back
301 if (!cardp
->CRC_OK
) {
302 cardp
->totalbytes
= cardp
->fwlastblksent
;
306 lbs_deb_usb2(&cardp
->udev
->dev
, "totalbytes = %d\n",
309 /* struct fwdata (which we sent to the card) has an
310 extra __le32 field in between the header and the data,
311 which is not in the struct fwheader in the actual
312 firmware binary. Insert the seqnum in the middle... */
313 memcpy(&fwdata
->hdr
, &firmware
[cardp
->totalbytes
],
314 sizeof(struct fwheader
));
316 cardp
->fwlastblksent
= cardp
->totalbytes
;
317 cardp
->totalbytes
+= sizeof(struct fwheader
);
319 memcpy(fwdata
->data
, &firmware
[cardp
->totalbytes
],
320 le32_to_cpu(fwdata
->hdr
.datalength
));
322 lbs_deb_usb2(&cardp
->udev
->dev
, "Data length = %d\n",
323 le32_to_cpu(fwdata
->hdr
.datalength
));
325 fwdata
->seqnum
= cpu_to_le32(++cardp
->fwseqnum
);
326 cardp
->totalbytes
+= le32_to_cpu(fwdata
->hdr
.datalength
);
328 usb_tx_block(cardp
, cardp
->ep_out_buf
, sizeof(struct fwdata
) +
329 le32_to_cpu(fwdata
->hdr
.datalength
));
331 if (fwdata
->hdr
.dnldcmd
== cpu_to_le32(FW_HAS_DATA_TO_RECV
)) {
332 lbs_deb_usb2(&cardp
->udev
->dev
, "There are data to follow\n");
333 lbs_deb_usb2(&cardp
->udev
->dev
, "seqnum = %d totalbytes = %d\n",
334 cardp
->fwseqnum
, cardp
->totalbytes
);
335 } else if (fwdata
->hdr
.dnldcmd
== cpu_to_le32(FW_HAS_LAST_BLOCK
)) {
336 lbs_deb_usb2(&cardp
->udev
->dev
, "Host has finished FW downloading\n");
337 lbs_deb_usb2(&cardp
->udev
->dev
, "Donwloading FW JUMP BLOCK\n");
339 cardp
->fwfinalblk
= 1;
342 lbs_deb_usb2(&cardp
->udev
->dev
, "Firmware download done; size %d\n",
348 static int if_usb_reset_device(struct if_usb_card
*cardp
)
350 struct cmd_ds_command
*cmd
= cardp
->ep_out_buf
+ 4;
353 lbs_deb_enter(LBS_DEB_USB
);
355 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_REQUEST
);
357 cmd
->command
= cpu_to_le16(CMD_802_11_RESET
);
358 cmd
->size
= cpu_to_le16(sizeof(struct cmd_ds_802_11_reset
) + S_DS_GEN
);
359 cmd
->result
= cpu_to_le16(0);
360 cmd
->seqnum
= cpu_to_le16(0x5a5a);
361 cmd
->params
.reset
.action
= cpu_to_le16(CMD_ACT_HALT
);
362 usb_tx_block(cardp
, cardp
->ep_out_buf
, 4 + S_DS_GEN
+ sizeof(struct cmd_ds_802_11_reset
));
365 ret
= usb_reset_device(cardp
->udev
);
369 if (ret
&& machine_is_olpc()) {
370 printk(KERN_CRIT
"Resetting OLPC wireless via EC...\n");
371 olpc_ec_cmd(0x25, NULL
, 0, NULL
, 0);
375 lbs_deb_leave_args(LBS_DEB_USB
, "ret %d", ret
);
381 * @brief This function transfer the data to the device.
382 * @param priv pointer to struct lbs_private
383 * @param payload pointer to payload data
384 * @param nb data length
387 static int usb_tx_block(struct if_usb_card
*cardp
, uint8_t *payload
, uint16_t nb
)
391 /* check if device is removed */
392 if (cardp
->surprise_removed
) {
393 lbs_deb_usbd(&cardp
->udev
->dev
, "Device removed\n");
397 usb_fill_bulk_urb(cardp
->tx_urb
, cardp
->udev
,
398 usb_sndbulkpipe(cardp
->udev
,
400 payload
, nb
, if_usb_write_bulk_callback
, cardp
);
402 cardp
->tx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
404 if ((ret
= usb_submit_urb(cardp
->tx_urb
, GFP_ATOMIC
))) {
405 lbs_deb_usbd(&cardp
->udev
->dev
, "usb_submit_urb failed: %d\n", ret
);
408 lbs_deb_usb2(&cardp
->udev
->dev
, "usb_submit_urb success\n");
416 static int __if_usb_submit_rx_urb(struct if_usb_card
*cardp
,
417 void (*callbackfn
)(struct urb
*urb
))
422 if (!(skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
))) {
423 lbs_pr_err("No free skb\n");
429 /* Fill the receive configuration URB and initialise the Rx call back */
430 usb_fill_bulk_urb(cardp
->rx_urb
, cardp
->udev
,
431 usb_rcvbulkpipe(cardp
->udev
, cardp
->ep_in
),
432 (void *) (skb
->tail
+ (size_t) IPFIELD_ALIGN_OFFSET
),
433 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
, callbackfn
,
436 cardp
->rx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
438 lbs_deb_usb2(&cardp
->udev
->dev
, "Pointer for rx_urb %p\n", cardp
->rx_urb
);
439 if ((ret
= usb_submit_urb(cardp
->rx_urb
, GFP_ATOMIC
))) {
440 lbs_deb_usbd(&cardp
->udev
->dev
, "Submit Rx URB failed: %d\n", ret
);
442 cardp
->rx_skb
= NULL
;
445 lbs_deb_usb2(&cardp
->udev
->dev
, "Submit Rx URB success\n");
453 static int if_usb_submit_rx_urb_fwload(struct if_usb_card
*cardp
)
455 return __if_usb_submit_rx_urb(cardp
, &if_usb_receive_fwload
);
458 static int if_usb_submit_rx_urb(struct if_usb_card
*cardp
)
460 return __if_usb_submit_rx_urb(cardp
, &if_usb_receive
);
463 static void if_usb_receive_fwload(struct urb
*urb
)
465 struct if_usb_card
*cardp
= urb
->context
;
466 struct sk_buff
*skb
= cardp
->rx_skb
;
467 struct fwsyncheader
*syncfwheader
;
468 struct bootcmdresp bootcmdresp
;
471 lbs_deb_usbd(&cardp
->udev
->dev
,
472 "URB status is failed during fw load\n");
477 if (cardp
->fwdnldover
) {
478 __le32
*tmp
= (__le32
*)(skb
->data
+ IPFIELD_ALIGN_OFFSET
);
480 if (tmp
[0] == cpu_to_le32(CMD_TYPE_INDICATION
) &&
481 tmp
[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY
)) {
482 lbs_pr_info("Firmware ready event received\n");
483 wake_up(&cardp
->fw_wq
);
485 lbs_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
);
492 if (cardp
->bootcmdresp
<= 0) {
493 memcpy (&bootcmdresp
, skb
->data
+ IPFIELD_ALIGN_OFFSET
,
494 sizeof(bootcmdresp
));
496 if (le16_to_cpu(cardp
->udev
->descriptor
.bcdDevice
) < 0x3106) {
498 if_usb_submit_rx_urb_fwload(cardp
);
499 cardp
->bootcmdresp
= 1;
500 lbs_deb_usbd(&cardp
->udev
->dev
,
501 "Received valid boot command response\n");
504 if (bootcmdresp
.magic
!= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
)) {
505 if (bootcmdresp
.magic
== cpu_to_le32(CMD_TYPE_REQUEST
) ||
506 bootcmdresp
.magic
== cpu_to_le32(CMD_TYPE_DATA
) ||
507 bootcmdresp
.magic
== cpu_to_le32(CMD_TYPE_INDICATION
)) {
508 if (!cardp
->bootcmdresp
)
509 lbs_pr_info("Firmware already seems alive; resetting\n");
510 cardp
->bootcmdresp
= -1;
512 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
513 le32_to_cpu(bootcmdresp
.magic
));
515 } else if (bootcmdresp
.cmd
!= BOOT_CMD_FW_BY_USB
) {
516 lbs_pr_info("boot cmd response cmd_tag error (%d)\n",
518 } else if (bootcmdresp
.result
!= BOOT_CMD_RESP_OK
) {
519 lbs_pr_info("boot cmd response result error (%d)\n",
522 cardp
->bootcmdresp
= 1;
523 lbs_deb_usbd(&cardp
->udev
->dev
,
524 "Received valid boot command response\n");
527 if_usb_submit_rx_urb_fwload(cardp
);
531 syncfwheader
= kmalloc(sizeof(struct fwsyncheader
), GFP_ATOMIC
);
533 lbs_deb_usbd(&cardp
->udev
->dev
, "Failure to allocate syncfwheader\n");
538 memcpy(syncfwheader
, skb
->data
+ IPFIELD_ALIGN_OFFSET
,
539 sizeof(struct fwsyncheader
));
541 if (!syncfwheader
->cmd
) {
542 lbs_deb_usb2(&cardp
->udev
->dev
, "FW received Blk with correct CRC\n");
543 lbs_deb_usb2(&cardp
->udev
->dev
, "FW received Blk seqnum = %d\n",
544 le32_to_cpu(syncfwheader
->seqnum
));
547 lbs_deb_usbd(&cardp
->udev
->dev
, "FW received Blk with CRC error\n");
553 /* reschedule timer for 200ms hence */
554 mod_timer(&cardp
->fw_timeout
, jiffies
+ (HZ
/5));
556 if (cardp
->fwfinalblk
) {
557 cardp
->fwdnldover
= 1;
561 if_usb_send_fw_pkt(cardp
);
564 if_usb_submit_rx_urb_fwload(cardp
);
571 #define MRVDRV_MIN_PKT_LEN 30
573 static inline void process_cmdtypedata(int recvlength
, struct sk_buff
*skb
,
574 struct if_usb_card
*cardp
,
575 struct lbs_private
*priv
)
577 if (recvlength
> MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
+ MESSAGE_HEADER_LEN
578 || recvlength
< MRVDRV_MIN_PKT_LEN
) {
579 lbs_deb_usbd(&cardp
->udev
->dev
, "Packet length is Invalid\n");
584 skb_reserve(skb
, IPFIELD_ALIGN_OFFSET
);
585 skb_put(skb
, recvlength
);
586 skb_pull(skb
, MESSAGE_HEADER_LEN
);
588 lbs_process_rxed_packet(priv
, skb
);
591 static inline void process_cmdrequest(int recvlength
, uint8_t *recvbuff
,
593 struct if_usb_card
*cardp
,
594 struct lbs_private
*priv
)
598 if (recvlength
> LBS_CMD_BUFFER_SIZE
) {
599 lbs_deb_usbd(&cardp
->udev
->dev
,
600 "The receive buffer is too large\n");
608 spin_lock(&priv
->driver_lock
);
610 i
= (priv
->resp_idx
== 0) ? 1 : 0;
611 BUG_ON(priv
->resp_len
[i
]);
612 priv
->resp_len
[i
] = (recvlength
- MESSAGE_HEADER_LEN
);
613 memcpy(priv
->resp_buf
[i
], recvbuff
+ MESSAGE_HEADER_LEN
,
616 lbs_notify_command_response(priv
, i
);
618 spin_unlock(&priv
->driver_lock
);
620 lbs_deb_usbd(&cardp
->udev
->dev
,
621 "Wake up main thread to handle cmd response\n");
625 * @brief This function reads of the packet into the upload buff,
626 * wake up the main thread and initialise the Rx callack.
628 * @param urb pointer to struct urb
631 static void if_usb_receive(struct urb
*urb
)
633 struct if_usb_card
*cardp
= urb
->context
;
634 struct sk_buff
*skb
= cardp
->rx_skb
;
635 struct lbs_private
*priv
= cardp
->priv
;
636 int recvlength
= urb
->actual_length
;
637 uint8_t *recvbuff
= NULL
;
638 uint32_t recvtype
= 0;
639 __le32
*pkt
= (__le32
*)(skb
->data
+ IPFIELD_ALIGN_OFFSET
);
642 lbs_deb_enter(LBS_DEB_USB
);
646 lbs_deb_usbd(&cardp
->udev
->dev
, "RX URB failed: %d\n",
652 recvbuff
= skb
->data
+ IPFIELD_ALIGN_OFFSET
;
653 recvtype
= le32_to_cpu(pkt
[0]);
654 lbs_deb_usbd(&cardp
->udev
->dev
,
655 "Recv length = 0x%x, Recv type = 0x%X\n",
656 recvlength
, recvtype
);
657 } else if (urb
->status
) {
664 process_cmdtypedata(recvlength
, skb
, cardp
, priv
);
667 case CMD_TYPE_REQUEST
:
668 process_cmdrequest(recvlength
, recvbuff
, skb
, cardp
, priv
);
671 case CMD_TYPE_INDICATION
:
673 event
= le32_to_cpu(pkt
[1]);
674 lbs_deb_usbd(&cardp
->udev
->dev
, "**EVENT** 0x%X\n", event
);
677 /* Icky undocumented magic special case */
678 if (event
& 0xffff0000) {
679 u32 trycount
= (event
& 0xffff0000) >> 16;
681 lbs_send_tx_feedback(priv
, trycount
);
683 lbs_queue_event(priv
, event
& 0xFF);
687 lbs_deb_usbd(&cardp
->udev
->dev
, "Unknown command type 0x%X\n",
694 if_usb_submit_rx_urb(cardp
);
696 lbs_deb_leave(LBS_DEB_USB
);
700 * @brief This function downloads data to FW
701 * @param priv pointer to struct lbs_private structure
702 * @param type type of data
703 * @param buf pointer to data buffer
704 * @param len number of bytes
707 static int if_usb_host_to_card(struct lbs_private
*priv
, uint8_t type
,
708 uint8_t *payload
, uint16_t nb
)
710 struct if_usb_card
*cardp
= priv
->card
;
712 lbs_deb_usbd(&cardp
->udev
->dev
,"*** type = %u\n", type
);
713 lbs_deb_usbd(&cardp
->udev
->dev
,"size after = %d\n", nb
);
715 if (type
== MVMS_CMD
) {
716 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_REQUEST
);
717 priv
->dnld_sent
= DNLD_CMD_SENT
;
719 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_DATA
);
720 priv
->dnld_sent
= DNLD_DATA_SENT
;
723 memcpy((cardp
->ep_out_buf
+ MESSAGE_HEADER_LEN
), payload
, nb
);
725 return usb_tx_block(cardp
, cardp
->ep_out_buf
, nb
+ MESSAGE_HEADER_LEN
);
729 * @brief This function issues Boot command to the Boot2 code
730 * @param ivalue 1:Boot from FW by USB-Download
731 * 2:Boot from FW in EEPROM
734 static int if_usb_issue_boot_command(struct if_usb_card
*cardp
, int ivalue
)
736 struct bootcmd
*bootcmd
= cardp
->ep_out_buf
;
738 /* Prepare command */
739 bootcmd
->magic
= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
);
740 bootcmd
->cmd
= ivalue
;
741 memset(bootcmd
->pad
, 0, sizeof(bootcmd
->pad
));
744 usb_tx_block(cardp
, cardp
->ep_out_buf
, sizeof(*bootcmd
));
751 * @brief This function checks the validity of Boot2/FW image.
753 * @param data pointer to image
757 static int check_fwfile_format(uint8_t *data
, uint32_t totlen
)
759 uint32_t bincmd
, exit
;
760 uint32_t blksize
, offset
, len
;
767 struct fwheader
*fwh
= (void *)data
;
769 bincmd
= le32_to_cpu(fwh
->dnldcmd
);
770 blksize
= le32_to_cpu(fwh
->datalength
);
772 case FW_HAS_DATA_TO_RECV
:
773 offset
= sizeof(struct fwheader
) + blksize
;
779 case FW_HAS_LAST_BLOCK
:
790 lbs_pr_err("firmware file format check FAIL\n");
792 lbs_deb_fw("firmware file format check PASS\n");
798 static int if_usb_prog_firmware(struct if_usb_card
*cardp
)
801 static int reset_count
= 10;
804 lbs_deb_enter(LBS_DEB_USB
);
806 if ((ret
= request_firmware(&cardp
->fw
, lbs_fw_name
,
807 &cardp
->udev
->dev
)) < 0) {
808 lbs_pr_err("request_firmware() failed with %#x\n", ret
);
809 lbs_pr_err("firmware %s not found\n", lbs_fw_name
);
813 if (check_fwfile_format(cardp
->fw
->data
, cardp
->fw
->size
))
817 if (if_usb_submit_rx_urb_fwload(cardp
) < 0) {
818 lbs_deb_usbd(&cardp
->udev
->dev
, "URB submission is failed\n");
823 cardp
->bootcmdresp
= 0;
827 /* Issue Boot command = 1, Boot from Download-FW */
828 if_usb_issue_boot_command(cardp
, BOOT_CMD_FW_BY_USB
);
829 /* wait for command response */
832 msleep_interruptible(100);
833 } while (cardp
->bootcmdresp
== 0 && j
< 10);
834 } while (cardp
->bootcmdresp
== 0 && i
< 5);
836 if (cardp
->bootcmdresp
<= 0) {
837 if (--reset_count
>= 0) {
838 if_usb_reset_device(cardp
);
846 cardp
->totalbytes
= 0;
847 cardp
->fwlastblksent
= 0;
849 cardp
->fwdnldover
= 0;
850 cardp
->fwseqnum
= -1;
851 cardp
->totalbytes
= 0;
852 cardp
->fwfinalblk
= 0;
854 /* Send the first firmware packet... */
855 if_usb_send_fw_pkt(cardp
);
857 /* ... and wait for the process to complete */
858 wait_event_interruptible(cardp
->fw_wq
, cardp
->surprise_removed
|| cardp
->fwdnldover
);
860 del_timer_sync(&cardp
->fw_timeout
);
861 usb_kill_urb(cardp
->rx_urb
);
863 if (!cardp
->fwdnldover
) {
864 lbs_pr_info("failed to load fw, resetting device!\n");
865 if (--reset_count
>= 0) {
866 if_usb_reset_device(cardp
);
870 lbs_pr_info("FW download failure, time = %d ms\n", i
* 100);
876 release_firmware(cardp
->fw
);
880 lbs_deb_leave_args(LBS_DEB_USB
, "ret %d", ret
);
886 static int if_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
888 struct if_usb_card
*cardp
= usb_get_intfdata(intf
);
889 struct lbs_private
*priv
= cardp
->priv
;
892 lbs_deb_enter(LBS_DEB_USB
);
894 if (priv
->psstate
!= PS_STATE_FULL_POWER
)
897 ret
= lbs_suspend(priv
);
901 /* Unlink tx & rx urb */
902 usb_kill_urb(cardp
->tx_urb
);
903 usb_kill_urb(cardp
->rx_urb
);
906 lbs_deb_leave(LBS_DEB_USB
);
910 static int if_usb_resume(struct usb_interface
*intf
)
912 struct if_usb_card
*cardp
= usb_get_intfdata(intf
);
913 struct lbs_private
*priv
= cardp
->priv
;
915 lbs_deb_enter(LBS_DEB_USB
);
917 if_usb_submit_rx_urb(cardp
);
921 lbs_deb_leave(LBS_DEB_USB
);
925 #define if_usb_suspend NULL
926 #define if_usb_resume NULL
929 static struct usb_driver if_usb_driver
= {
931 .probe
= if_usb_probe
,
932 .disconnect
= if_usb_disconnect
,
933 .id_table
= if_usb_table
,
934 .suspend
= if_usb_suspend
,
935 .resume
= if_usb_resume
,
936 .reset_resume
= if_usb_resume
,
939 static int __init
if_usb_init_module(void)
943 lbs_deb_enter(LBS_DEB_MAIN
);
945 ret
= usb_register(&if_usb_driver
);
947 lbs_deb_leave_args(LBS_DEB_MAIN
, "ret %d", ret
);
951 static void __exit
if_usb_exit_module(void)
953 lbs_deb_enter(LBS_DEB_MAIN
);
955 usb_deregister(&if_usb_driver
);
957 lbs_deb_leave(LBS_DEB_MAIN
);
960 module_init(if_usb_init_module
);
961 module_exit(if_usb_exit_module
);
963 MODULE_DESCRIPTION("8388 USB WLAN Driver");
964 MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
965 MODULE_LICENSE("GPL");