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>
13 #define DRV_NAME "usb8xxx"
23 #define lbs_deb_usb2(...) do { if (INSANEDEBUG) lbs_deb_usbd(__VA_ARGS__); } while (0)
25 #define MESSAGE_HEADER_LEN 4
27 static char *lbs_fw_name
= "usb8388.bin";
28 module_param_named(fw_name
, lbs_fw_name
, charp
, 0644);
30 static 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 if_usb_card
*cardp
);
42 static int if_usb_host_to_card(struct lbs_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
,
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 if_usb_card
*cardp
);
51 * @brief call back function to handle the status of the URB
52 * @param urb pointer to urb structure
55 static void if_usb_write_bulk_callback(struct urb
*urb
)
57 struct if_usb_card
*cardp
= (struct if_usb_card
*) urb
->context
;
59 /* handle the transmission complete validations */
61 if (urb
->status
== 0) {
62 struct lbs_private
*priv
= cardp
->priv
;
64 lbs_deb_usb2(&urb
->dev
->dev
, "URB status is successful\n");
65 lbs_deb_usb2(&urb
->dev
->dev
, "Actual length transmitted %d\n",
68 /* Used for both firmware TX and regular TX. priv isn't
69 * valid at firmware load time.
72 lbs_host_to_card_done(priv
);
74 /* print the failure status number for debug */
75 lbs_pr_info("URB in failure status: %d\n", urb
->status
);
82 * @brief free tx/rx urb, skb and rx buffer
83 * @param cardp pointer if_usb_card
86 static void if_usb_free(struct if_usb_card
*cardp
)
88 lbs_deb_enter(LBS_DEB_USB
);
90 /* Unlink tx & rx urb */
91 usb_kill_urb(cardp
->tx_urb
);
92 usb_kill_urb(cardp
->rx_urb
);
94 usb_free_urb(cardp
->tx_urb
);
97 usb_free_urb(cardp
->rx_urb
);
100 kfree(cardp
->ep_out_buf
);
101 cardp
->ep_out_buf
= NULL
;
103 lbs_deb_leave(LBS_DEB_USB
);
106 static void if_usb_setup_firmware(struct lbs_private
*priv
)
108 struct if_usb_card
*cardp
= priv
->card
;
109 struct cmd_ds_set_boot2_ver b2_cmd
;
110 struct cmd_ds_802_11_fw_wake_method wake_method
;
112 b2_cmd
.hdr
.size
= cpu_to_le16(sizeof(b2_cmd
));
114 b2_cmd
.version
= cardp
->boot2_version
;
116 if (lbs_cmd_with_response(priv
, CMD_SET_BOOT2_VER
, &b2_cmd
))
117 lbs_deb_usb("Setting boot2 version failed\n");
119 priv
->wol_gpio
= 2; /* Wake via GPIO2... */
120 priv
->wol_gap
= 20; /* ... after 20ms */
121 lbs_host_sleep_cfg(priv
, EHS_WAKE_ON_UNICAST_DATA
);
123 wake_method
.hdr
.size
= cpu_to_le16(sizeof(wake_method
));
124 wake_method
.action
= cpu_to_le16(CMD_ACT_GET
);
125 if (lbs_cmd_with_response(priv
, CMD_802_11_FW_WAKE_METHOD
, &wake_method
)) {
126 lbs_pr_info("Firmware does not seem to support PS mode\n");
128 if (le16_to_cpu(wake_method
.method
) == CMD_WAKE_METHOD_COMMAND_INT
) {
129 lbs_deb_usb("Firmware seems to support PS with wake-via-command\n");
130 priv
->ps_supported
= 1;
132 /* The versions which boot up this way don't seem to
133 work even if we set it to the command interrupt */
134 lbs_pr_info("Firmware doesn't wake via command interrupt; disabling PS mode\n");
139 static void if_usb_fw_timeo(unsigned long priv
)
141 struct if_usb_card
*cardp
= (void *)priv
;
143 if (cardp
->fwdnldover
) {
144 lbs_deb_usb("Download complete, no event. Assuming success\n");
146 lbs_pr_err("Download timed out\n");
147 cardp
->surprise_removed
= 1;
149 wake_up(&cardp
->fw_wq
);
153 * @brief sets the configuration values
154 * @param ifnum interface number
155 * @param id pointer to usb_device_id
156 * @return 0 on success, error code on failure
158 static int if_usb_probe(struct usb_interface
*intf
,
159 const struct usb_device_id
*id
)
161 struct usb_device
*udev
;
162 struct usb_host_interface
*iface_desc
;
163 struct usb_endpoint_descriptor
*endpoint
;
164 struct lbs_private
*priv
;
165 struct if_usb_card
*cardp
;
168 udev
= interface_to_usbdev(intf
);
170 cardp
= kzalloc(sizeof(struct if_usb_card
), GFP_KERNEL
);
172 lbs_pr_err("Out of memory allocating private data.\n");
176 setup_timer(&cardp
->fw_timeout
, if_usb_fw_timeo
, (unsigned long)cardp
);
177 init_waitqueue_head(&cardp
->fw_wq
);
180 iface_desc
= intf
->cur_altsetting
;
182 lbs_deb_usbd(&udev
->dev
, "bcdUSB = 0x%X bDeviceClass = 0x%X"
183 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
184 le16_to_cpu(udev
->descriptor
.bcdUSB
),
185 udev
->descriptor
.bDeviceClass
,
186 udev
->descriptor
.bDeviceSubClass
,
187 udev
->descriptor
.bDeviceProtocol
);
189 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
190 endpoint
= &iface_desc
->endpoint
[i
].desc
;
191 if (usb_endpoint_is_bulk_in(endpoint
)) {
192 cardp
->ep_in_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
193 cardp
->ep_in
= usb_endpoint_num(endpoint
);
195 lbs_deb_usbd(&udev
->dev
, "in_endpoint = %d\n", cardp
->ep_in
);
196 lbs_deb_usbd(&udev
->dev
, "Bulk in size is %d\n", cardp
->ep_in_size
);
198 } else if (usb_endpoint_is_bulk_out(endpoint
)) {
199 cardp
->ep_out_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
200 cardp
->ep_out
= usb_endpoint_num(endpoint
);
202 lbs_deb_usbd(&udev
->dev
, "out_endpoint = %d\n", cardp
->ep_out
);
203 lbs_deb_usbd(&udev
->dev
, "Bulk out size is %d\n", cardp
->ep_out_size
);
206 if (!cardp
->ep_out_size
|| !cardp
->ep_in_size
) {
207 lbs_deb_usbd(&udev
->dev
, "Endpoints not found\n");
210 if (!(cardp
->rx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
211 lbs_deb_usbd(&udev
->dev
, "Rx URB allocation failed\n");
214 if (!(cardp
->tx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
215 lbs_deb_usbd(&udev
->dev
, "Tx URB allocation failed\n");
218 cardp
->ep_out_buf
= kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE
, GFP_KERNEL
);
219 if (!cardp
->ep_out_buf
) {
220 lbs_deb_usbd(&udev
->dev
, "Could not allocate buffer\n");
224 /* Upload firmware */
225 if (if_usb_prog_firmware(cardp
)) {
226 lbs_deb_usbd(&udev
->dev
, "FW upload failed\n");
227 goto err_prog_firmware
;
230 if (!(priv
= lbs_add_card(cardp
, &udev
->dev
)))
231 goto err_prog_firmware
;
234 cardp
->priv
->fw_ready
= 1;
236 priv
->hw_host_to_card
= if_usb_host_to_card
;
237 cardp
->boot2_version
= udev
->descriptor
.bcdDevice
;
239 if_usb_submit_rx_urb(cardp
);
241 if (lbs_start_card(priv
))
244 if_usb_setup_firmware(priv
);
247 usb_set_intfdata(intf
, cardp
);
252 lbs_remove_card(priv
);
254 if_usb_reset_device(cardp
);
263 * @brief free resource and cleanup
264 * @param intf USB interface structure
267 static void if_usb_disconnect(struct usb_interface
*intf
)
269 struct if_usb_card
*cardp
= usb_get_intfdata(intf
);
270 struct lbs_private
*priv
= (struct lbs_private
*) cardp
->priv
;
272 lbs_deb_enter(LBS_DEB_MAIN
);
274 cardp
->surprise_removed
= 1;
277 priv
->surpriseremoved
= 1;
279 lbs_remove_card(priv
);
282 /* Unlink and free urb */
285 usb_set_intfdata(intf
, NULL
);
286 usb_put_dev(interface_to_usbdev(intf
));
288 lbs_deb_leave(LBS_DEB_MAIN
);
292 * @brief This function download FW
293 * @param priv pointer to struct lbs_private
296 static int if_usb_send_fw_pkt(struct if_usb_card
*cardp
)
298 struct fwdata
*fwdata
= cardp
->ep_out_buf
;
299 uint8_t *firmware
= cardp
->fw
->data
;
301 /* If we got a CRC failure on the last block, back
303 if (!cardp
->CRC_OK
) {
304 cardp
->totalbytes
= cardp
->fwlastblksent
;
308 lbs_deb_usb2(&cardp
->udev
->dev
, "totalbytes = %d\n",
311 /* struct fwdata (which we sent to the card) has an
312 extra __le32 field in between the header and the data,
313 which is not in the struct fwheader in the actual
314 firmware binary. Insert the seqnum in the middle... */
315 memcpy(&fwdata
->hdr
, &firmware
[cardp
->totalbytes
],
316 sizeof(struct fwheader
));
318 cardp
->fwlastblksent
= cardp
->totalbytes
;
319 cardp
->totalbytes
+= sizeof(struct fwheader
);
321 memcpy(fwdata
->data
, &firmware
[cardp
->totalbytes
],
322 le32_to_cpu(fwdata
->hdr
.datalength
));
324 lbs_deb_usb2(&cardp
->udev
->dev
, "Data length = %d\n",
325 le32_to_cpu(fwdata
->hdr
.datalength
));
327 fwdata
->seqnum
= cpu_to_le32(++cardp
->fwseqnum
);
328 cardp
->totalbytes
+= le32_to_cpu(fwdata
->hdr
.datalength
);
330 usb_tx_block(cardp
, cardp
->ep_out_buf
, sizeof(struct fwdata
) +
331 le32_to_cpu(fwdata
->hdr
.datalength
));
333 if (fwdata
->hdr
.dnldcmd
== cpu_to_le32(FW_HAS_DATA_TO_RECV
)) {
334 lbs_deb_usb2(&cardp
->udev
->dev
, "There are data to follow\n");
335 lbs_deb_usb2(&cardp
->udev
->dev
, "seqnum = %d totalbytes = %d\n",
336 cardp
->fwseqnum
, cardp
->totalbytes
);
337 } else if (fwdata
->hdr
.dnldcmd
== cpu_to_le32(FW_HAS_LAST_BLOCK
)) {
338 lbs_deb_usb2(&cardp
->udev
->dev
, "Host has finished FW downloading\n");
339 lbs_deb_usb2(&cardp
->udev
->dev
, "Donwloading FW JUMP BLOCK\n");
341 cardp
->fwfinalblk
= 1;
344 lbs_deb_usb2(&cardp
->udev
->dev
, "Firmware download done; size %d\n",
350 static int if_usb_reset_device(struct if_usb_card
*cardp
)
352 struct cmd_ds_command
*cmd
= cardp
->ep_out_buf
+ 4;
355 lbs_deb_enter(LBS_DEB_USB
);
357 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_REQUEST
);
359 cmd
->command
= cpu_to_le16(CMD_802_11_RESET
);
360 cmd
->size
= cpu_to_le16(sizeof(struct cmd_ds_802_11_reset
) + S_DS_GEN
);
361 cmd
->result
= cpu_to_le16(0);
362 cmd
->seqnum
= cpu_to_le16(0x5a5a);
363 cmd
->params
.reset
.action
= cpu_to_le16(CMD_ACT_HALT
);
364 usb_tx_block(cardp
, cardp
->ep_out_buf
, 4 + S_DS_GEN
+ sizeof(struct cmd_ds_802_11_reset
));
367 ret
= usb_reset_device(cardp
->udev
);
371 if (ret
&& machine_is_olpc()) {
372 printk(KERN_CRIT
"Resetting OLPC wireless via EC...\n");
373 olpc_ec_cmd(0x25, NULL
, 0, NULL
, 0);
377 lbs_deb_leave_args(LBS_DEB_USB
, "ret %d", ret
);
383 * @brief This function transfer the data to the device.
384 * @param priv pointer to struct lbs_private
385 * @param payload pointer to payload data
386 * @param nb data length
389 static int usb_tx_block(struct if_usb_card
*cardp
, uint8_t *payload
, uint16_t nb
)
393 /* check if device is removed */
394 if (cardp
->surprise_removed
) {
395 lbs_deb_usbd(&cardp
->udev
->dev
, "Device removed\n");
399 usb_fill_bulk_urb(cardp
->tx_urb
, cardp
->udev
,
400 usb_sndbulkpipe(cardp
->udev
,
402 payload
, nb
, if_usb_write_bulk_callback
, cardp
);
404 cardp
->tx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
406 if ((ret
= usb_submit_urb(cardp
->tx_urb
, GFP_ATOMIC
))) {
407 lbs_deb_usbd(&cardp
->udev
->dev
, "usb_submit_urb failed: %d\n", ret
);
410 lbs_deb_usb2(&cardp
->udev
->dev
, "usb_submit_urb success\n");
418 static int __if_usb_submit_rx_urb(struct if_usb_card
*cardp
,
419 void (*callbackfn
)(struct urb
*urb
))
424 if (!(skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
))) {
425 lbs_pr_err("No free skb\n");
431 /* Fill the receive configuration URB and initialise the Rx call back */
432 usb_fill_bulk_urb(cardp
->rx_urb
, cardp
->udev
,
433 usb_rcvbulkpipe(cardp
->udev
, cardp
->ep_in
),
434 (void *) (skb
->tail
+ (size_t) IPFIELD_ALIGN_OFFSET
),
435 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
, callbackfn
,
438 cardp
->rx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
440 lbs_deb_usb2(&cardp
->udev
->dev
, "Pointer for rx_urb %p\n", cardp
->rx_urb
);
441 if ((ret
= usb_submit_urb(cardp
->rx_urb
, GFP_ATOMIC
))) {
442 lbs_deb_usbd(&cardp
->udev
->dev
, "Submit Rx URB failed: %d\n", ret
);
444 cardp
->rx_skb
= NULL
;
447 lbs_deb_usb2(&cardp
->udev
->dev
, "Submit Rx URB success\n");
455 static int if_usb_submit_rx_urb_fwload(struct if_usb_card
*cardp
)
457 return __if_usb_submit_rx_urb(cardp
, &if_usb_receive_fwload
);
460 static int if_usb_submit_rx_urb(struct if_usb_card
*cardp
)
462 return __if_usb_submit_rx_urb(cardp
, &if_usb_receive
);
465 static void if_usb_receive_fwload(struct urb
*urb
)
467 struct if_usb_card
*cardp
= urb
->context
;
468 struct sk_buff
*skb
= cardp
->rx_skb
;
469 struct fwsyncheader
*syncfwheader
;
470 struct bootcmdresp bootcmdresp
;
473 lbs_deb_usbd(&cardp
->udev
->dev
,
474 "URB status is failed during fw load\n");
479 if (cardp
->fwdnldover
) {
480 __le32
*tmp
= (__le32
*)(skb
->data
+ IPFIELD_ALIGN_OFFSET
);
482 if (tmp
[0] == cpu_to_le32(CMD_TYPE_INDICATION
) &&
483 tmp
[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY
)) {
484 lbs_pr_info("Firmware ready event received\n");
485 wake_up(&cardp
->fw_wq
);
487 lbs_deb_usb("Waiting for confirmation; got %x %x\n",
488 le32_to_cpu(tmp
[0]), le32_to_cpu(tmp
[1]));
489 if_usb_submit_rx_urb_fwload(cardp
);
494 if (cardp
->bootcmdresp
<= 0) {
495 memcpy (&bootcmdresp
, skb
->data
+ IPFIELD_ALIGN_OFFSET
,
496 sizeof(bootcmdresp
));
498 if (le16_to_cpu(cardp
->udev
->descriptor
.bcdDevice
) < 0x3106) {
500 if_usb_submit_rx_urb_fwload(cardp
);
501 cardp
->bootcmdresp
= 1;
502 lbs_deb_usbd(&cardp
->udev
->dev
,
503 "Received valid boot command response\n");
506 if (bootcmdresp
.magic
!= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
)) {
507 if (bootcmdresp
.magic
== cpu_to_le32(CMD_TYPE_REQUEST
) ||
508 bootcmdresp
.magic
== cpu_to_le32(CMD_TYPE_DATA
) ||
509 bootcmdresp
.magic
== cpu_to_le32(CMD_TYPE_INDICATION
)) {
510 if (!cardp
->bootcmdresp
)
511 lbs_pr_info("Firmware already seems alive; resetting\n");
512 cardp
->bootcmdresp
= -1;
514 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
515 le32_to_cpu(bootcmdresp
.magic
));
517 } else if (bootcmdresp
.cmd
!= BOOT_CMD_FW_BY_USB
) {
518 lbs_pr_info("boot cmd response cmd_tag error (%d)\n",
520 } else if (bootcmdresp
.result
!= BOOT_CMD_RESP_OK
) {
521 lbs_pr_info("boot cmd response result error (%d)\n",
524 cardp
->bootcmdresp
= 1;
525 lbs_deb_usbd(&cardp
->udev
->dev
,
526 "Received valid boot command response\n");
529 if_usb_submit_rx_urb_fwload(cardp
);
533 syncfwheader
= kmalloc(sizeof(struct fwsyncheader
), GFP_ATOMIC
);
535 lbs_deb_usbd(&cardp
->udev
->dev
, "Failure to allocate syncfwheader\n");
540 memcpy(syncfwheader
, skb
->data
+ IPFIELD_ALIGN_OFFSET
,
541 sizeof(struct fwsyncheader
));
543 if (!syncfwheader
->cmd
) {
544 lbs_deb_usb2(&cardp
->udev
->dev
, "FW received Blk with correct CRC\n");
545 lbs_deb_usb2(&cardp
->udev
->dev
, "FW received Blk seqnum = %d\n",
546 le32_to_cpu(syncfwheader
->seqnum
));
549 lbs_deb_usbd(&cardp
->udev
->dev
, "FW received Blk with CRC error\n");
555 /* reschedule timer for 200ms hence */
556 mod_timer(&cardp
->fw_timeout
, jiffies
+ (HZ
/5));
558 if (cardp
->fwfinalblk
) {
559 cardp
->fwdnldover
= 1;
563 if_usb_send_fw_pkt(cardp
);
566 if_usb_submit_rx_urb_fwload(cardp
);
573 #define MRVDRV_MIN_PKT_LEN 30
575 static inline void process_cmdtypedata(int recvlength
, struct sk_buff
*skb
,
576 struct if_usb_card
*cardp
,
577 struct lbs_private
*priv
)
579 if (recvlength
> MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
+ MESSAGE_HEADER_LEN
580 || recvlength
< MRVDRV_MIN_PKT_LEN
) {
581 lbs_deb_usbd(&cardp
->udev
->dev
, "Packet length is Invalid\n");
586 skb_reserve(skb
, IPFIELD_ALIGN_OFFSET
);
587 skb_put(skb
, recvlength
);
588 skb_pull(skb
, MESSAGE_HEADER_LEN
);
590 lbs_process_rxed_packet(priv
, skb
);
593 static inline void process_cmdrequest(int recvlength
, uint8_t *recvbuff
,
595 struct if_usb_card
*cardp
,
596 struct lbs_private
*priv
)
600 if (recvlength
> LBS_CMD_BUFFER_SIZE
) {
601 lbs_deb_usbd(&cardp
->udev
->dev
,
602 "The receive buffer is too large\n");
610 spin_lock(&priv
->driver_lock
);
612 i
= (priv
->resp_idx
== 0) ? 1 : 0;
613 BUG_ON(priv
->resp_len
[i
]);
614 priv
->resp_len
[i
] = (recvlength
- MESSAGE_HEADER_LEN
);
615 memcpy(priv
->resp_buf
[i
], recvbuff
+ MESSAGE_HEADER_LEN
,
618 lbs_notify_command_response(priv
, i
);
620 spin_unlock(&priv
->driver_lock
);
622 lbs_deb_usbd(&cardp
->udev
->dev
,
623 "Wake up main thread to handle cmd response\n");
627 * @brief This function reads of the packet into the upload buff,
628 * wake up the main thread and initialise the Rx callack.
630 * @param urb pointer to struct urb
633 static void if_usb_receive(struct urb
*urb
)
635 struct if_usb_card
*cardp
= urb
->context
;
636 struct sk_buff
*skb
= cardp
->rx_skb
;
637 struct lbs_private
*priv
= cardp
->priv
;
638 int recvlength
= urb
->actual_length
;
639 uint8_t *recvbuff
= NULL
;
640 uint32_t recvtype
= 0;
641 __le32
*pkt
= (__le32
*)(skb
->data
+ IPFIELD_ALIGN_OFFSET
);
644 lbs_deb_enter(LBS_DEB_USB
);
648 lbs_deb_usbd(&cardp
->udev
->dev
, "RX URB failed: %d\n",
654 recvbuff
= skb
->data
+ IPFIELD_ALIGN_OFFSET
;
655 recvtype
= le32_to_cpu(pkt
[0]);
656 lbs_deb_usbd(&cardp
->udev
->dev
,
657 "Recv length = 0x%x, Recv type = 0x%X\n",
658 recvlength
, recvtype
);
659 } else if (urb
->status
) {
666 process_cmdtypedata(recvlength
, skb
, cardp
, priv
);
669 case CMD_TYPE_REQUEST
:
670 process_cmdrequest(recvlength
, recvbuff
, skb
, cardp
, priv
);
673 case CMD_TYPE_INDICATION
:
675 event
= le32_to_cpu(pkt
[1]);
676 lbs_deb_usbd(&cardp
->udev
->dev
, "**EVENT** 0x%X\n", event
);
679 /* Icky undocumented magic special case */
680 if (event
& 0xffff0000) {
681 u32 trycount
= (event
& 0xffff0000) >> 16;
683 lbs_send_tx_feedback(priv
, trycount
);
685 lbs_queue_event(priv
, event
& 0xFF);
689 lbs_deb_usbd(&cardp
->udev
->dev
, "Unknown command type 0x%X\n",
696 if_usb_submit_rx_urb(cardp
);
698 lbs_deb_leave(LBS_DEB_USB
);
702 * @brief This function downloads data to FW
703 * @param priv pointer to struct lbs_private structure
704 * @param type type of data
705 * @param buf pointer to data buffer
706 * @param len number of bytes
709 static int if_usb_host_to_card(struct lbs_private
*priv
, uint8_t type
,
710 uint8_t *payload
, uint16_t nb
)
712 struct if_usb_card
*cardp
= priv
->card
;
714 lbs_deb_usbd(&cardp
->udev
->dev
,"*** type = %u\n", type
);
715 lbs_deb_usbd(&cardp
->udev
->dev
,"size after = %d\n", nb
);
717 if (type
== MVMS_CMD
) {
718 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_REQUEST
);
719 priv
->dnld_sent
= DNLD_CMD_SENT
;
721 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_DATA
);
722 priv
->dnld_sent
= DNLD_DATA_SENT
;
725 memcpy((cardp
->ep_out_buf
+ MESSAGE_HEADER_LEN
), payload
, nb
);
727 return usb_tx_block(cardp
, cardp
->ep_out_buf
, nb
+ MESSAGE_HEADER_LEN
);
731 * @brief This function issues Boot command to the Boot2 code
732 * @param ivalue 1:Boot from FW by USB-Download
733 * 2:Boot from FW in EEPROM
736 static int if_usb_issue_boot_command(struct if_usb_card
*cardp
, int ivalue
)
738 struct bootcmd
*bootcmd
= cardp
->ep_out_buf
;
740 /* Prepare command */
741 bootcmd
->magic
= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
);
742 bootcmd
->cmd
= ivalue
;
743 memset(bootcmd
->pad
, 0, sizeof(bootcmd
->pad
));
746 usb_tx_block(cardp
, cardp
->ep_out_buf
, sizeof(*bootcmd
));
753 * @brief This function checks the validity of Boot2/FW image.
755 * @param data pointer to image
759 static int check_fwfile_format(uint8_t *data
, uint32_t totlen
)
761 uint32_t bincmd
, exit
;
762 uint32_t blksize
, offset
, len
;
769 struct fwheader
*fwh
= (void *)data
;
771 bincmd
= le32_to_cpu(fwh
->dnldcmd
);
772 blksize
= le32_to_cpu(fwh
->datalength
);
774 case FW_HAS_DATA_TO_RECV
:
775 offset
= sizeof(struct fwheader
) + blksize
;
781 case FW_HAS_LAST_BLOCK
:
792 lbs_pr_err("firmware file format check FAIL\n");
794 lbs_deb_fw("firmware file format check PASS\n");
800 static int if_usb_prog_firmware(struct if_usb_card
*cardp
)
803 static int reset_count
= 10;
806 lbs_deb_enter(LBS_DEB_USB
);
808 if ((ret
= request_firmware(&cardp
->fw
, lbs_fw_name
,
809 &cardp
->udev
->dev
)) < 0) {
810 lbs_pr_err("request_firmware() failed with %#x\n", ret
);
811 lbs_pr_err("firmware %s not found\n", lbs_fw_name
);
815 if (check_fwfile_format(cardp
->fw
->data
, cardp
->fw
->size
))
819 if (if_usb_submit_rx_urb_fwload(cardp
) < 0) {
820 lbs_deb_usbd(&cardp
->udev
->dev
, "URB submission is failed\n");
825 cardp
->bootcmdresp
= 0;
829 /* Issue Boot command = 1, Boot from Download-FW */
830 if_usb_issue_boot_command(cardp
, BOOT_CMD_FW_BY_USB
);
831 /* wait for command response */
834 msleep_interruptible(100);
835 } while (cardp
->bootcmdresp
== 0 && j
< 10);
836 } while (cardp
->bootcmdresp
== 0 && i
< 5);
838 if (cardp
->bootcmdresp
<= 0) {
839 if (--reset_count
>= 0) {
840 if_usb_reset_device(cardp
);
848 cardp
->totalbytes
= 0;
849 cardp
->fwlastblksent
= 0;
851 cardp
->fwdnldover
= 0;
852 cardp
->fwseqnum
= -1;
853 cardp
->totalbytes
= 0;
854 cardp
->fwfinalblk
= 0;
856 /* Send the first firmware packet... */
857 if_usb_send_fw_pkt(cardp
);
859 /* ... and wait for the process to complete */
860 wait_event_interruptible(cardp
->fw_wq
, cardp
->surprise_removed
|| cardp
->fwdnldover
);
862 del_timer_sync(&cardp
->fw_timeout
);
863 usb_kill_urb(cardp
->rx_urb
);
865 if (!cardp
->fwdnldover
) {
866 lbs_pr_info("failed to load fw, resetting device!\n");
867 if (--reset_count
>= 0) {
868 if_usb_reset_device(cardp
);
872 lbs_pr_info("FW download failure, time = %d ms\n", i
* 100);
878 release_firmware(cardp
->fw
);
882 lbs_deb_leave_args(LBS_DEB_USB
, "ret %d", ret
);
888 static int if_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
890 struct if_usb_card
*cardp
= usb_get_intfdata(intf
);
891 struct lbs_private
*priv
= cardp
->priv
;
894 lbs_deb_enter(LBS_DEB_USB
);
896 if (priv
->psstate
!= PS_STATE_FULL_POWER
)
899 ret
= lbs_suspend(priv
);
903 /* Unlink tx & rx urb */
904 usb_kill_urb(cardp
->tx_urb
);
905 usb_kill_urb(cardp
->rx_urb
);
908 lbs_deb_leave(LBS_DEB_USB
);
912 static int if_usb_resume(struct usb_interface
*intf
)
914 struct if_usb_card
*cardp
= usb_get_intfdata(intf
);
915 struct lbs_private
*priv
= cardp
->priv
;
917 lbs_deb_enter(LBS_DEB_USB
);
919 if_usb_submit_rx_urb(cardp
);
923 lbs_deb_leave(LBS_DEB_USB
);
927 #define if_usb_suspend NULL
928 #define if_usb_resume NULL
931 static struct usb_driver if_usb_driver
= {
933 .probe
= if_usb_probe
,
934 .disconnect
= if_usb_disconnect
,
935 .id_table
= if_usb_table
,
936 .suspend
= if_usb_suspend
,
937 .resume
= if_usb_resume
,
938 .reset_resume
= if_usb_resume
,
941 static int __init
if_usb_init_module(void)
945 lbs_deb_enter(LBS_DEB_MAIN
);
947 ret
= usb_register(&if_usb_driver
);
949 lbs_deb_leave_args(LBS_DEB_MAIN
, "ret %d", ret
);
953 static void __exit
if_usb_exit_module(void)
955 lbs_deb_enter(LBS_DEB_MAIN
);
957 usb_deregister(&if_usb_driver
);
959 lbs_deb_leave(LBS_DEB_MAIN
);
962 module_init(if_usb_init_module
);
963 module_exit(if_usb_exit_module
);
965 MODULE_DESCRIPTION("8388 USB WLAN Driver");
966 MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
967 MODULE_LICENSE("GPL");