2 * This file contains functions used in USB interface module.
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/delay.h>
8 #include <linux/module.h>
9 #include <linux/firmware.h>
10 #include <linux/netdevice.h>
11 #include <linux/slab.h>
12 #include <linux/usb.h>
13 #include <linux/olpc-ec.h>
19 #define DRV_NAME "usb8xxx"
29 #define lbs_deb_usb2(...) do { if (INSANEDEBUG) lbs_deb_usbd(__VA_ARGS__); } while (0)
31 #define MESSAGE_HEADER_LEN 4
33 MODULE_FIRMWARE("libertas/usb8388_v9.bin");
34 MODULE_FIRMWARE("libertas/usb8388_v5.bin");
35 MODULE_FIRMWARE("libertas/usb8388.bin");
36 MODULE_FIRMWARE("libertas/usb8682.bin");
37 MODULE_FIRMWARE("usb8388.bin");
45 /* table of firmware file names */
46 static const struct lbs_fw_table fw_table
[] = {
47 { MODEL_8388
, "libertas/usb8388_olpc.bin", NULL
},
48 { MODEL_8388
, "libertas/usb8388_v9.bin", NULL
},
49 { MODEL_8388
, "libertas/usb8388_v5.bin", NULL
},
50 { MODEL_8388
, "libertas/usb8388.bin", NULL
},
51 { MODEL_8388
, "usb8388.bin", NULL
},
52 { MODEL_8682
, "libertas/usb8682.bin", NULL
}
55 static const struct usb_device_id if_usb_table
[] = {
56 /* Enter the device signature inside */
57 { USB_DEVICE(0x1286, 0x2001), .driver_info
= MODEL_8388
},
58 { USB_DEVICE(0x05a3, 0x8388), .driver_info
= MODEL_8388
},
59 {} /* Terminating entry */
62 MODULE_DEVICE_TABLE(usb
, if_usb_table
);
64 static void if_usb_receive(struct urb
*urb
);
65 static void if_usb_receive_fwload(struct urb
*urb
);
66 static void if_usb_prog_firmware(struct lbs_private
*priv
, int ret
,
67 const struct firmware
*fw
,
68 const struct firmware
*unused
);
69 static int if_usb_host_to_card(struct lbs_private
*priv
, uint8_t type
,
70 uint8_t *payload
, uint16_t nb
);
71 static int usb_tx_block(struct if_usb_card
*cardp
, uint8_t *payload
,
73 static void if_usb_free(struct if_usb_card
*cardp
);
74 static int if_usb_submit_rx_urb(struct if_usb_card
*cardp
);
75 static int if_usb_reset_device(struct if_usb_card
*cardp
);
78 * if_usb_write_bulk_callback - callback function to handle the status
80 * @urb: pointer to &urb structure
83 static void if_usb_write_bulk_callback(struct urb
*urb
)
85 struct if_usb_card
*cardp
= (struct if_usb_card
*) urb
->context
;
87 /* handle the transmission complete validations */
89 if (urb
->status
== 0) {
90 struct lbs_private
*priv
= cardp
->priv
;
92 lbs_deb_usb2(&urb
->dev
->dev
, "URB status is successful\n");
93 lbs_deb_usb2(&urb
->dev
->dev
, "Actual length transmitted %d\n",
96 /* Boot commands such as UPDATE_FW and UPDATE_BOOT2 are not
97 * passed up to the lbs level.
99 if (priv
&& priv
->dnld_sent
!= DNLD_BOOTCMD_SENT
)
100 lbs_host_to_card_done(priv
);
102 /* print the failure status number for debug */
103 pr_info("URB in failure status: %d\n", urb
->status
);
108 * if_usb_free - free tx/rx urb, skb and rx buffer
109 * @cardp: pointer to &if_usb_card
112 static void if_usb_free(struct if_usb_card
*cardp
)
114 /* Unlink tx & rx urb */
115 usb_kill_urb(cardp
->tx_urb
);
116 usb_kill_urb(cardp
->rx_urb
);
118 usb_free_urb(cardp
->tx_urb
);
119 cardp
->tx_urb
= NULL
;
121 usb_free_urb(cardp
->rx_urb
);
122 cardp
->rx_urb
= NULL
;
124 kfree(cardp
->ep_out_buf
);
125 cardp
->ep_out_buf
= NULL
;
128 static void if_usb_setup_firmware(struct lbs_private
*priv
)
130 struct if_usb_card
*cardp
= priv
->card
;
131 struct cmd_ds_set_boot2_ver b2_cmd
;
132 struct cmd_ds_802_11_fw_wake_method wake_method
;
134 b2_cmd
.hdr
.size
= cpu_to_le16(sizeof(b2_cmd
));
136 b2_cmd
.version
= cardp
->boot2_version
;
138 if (lbs_cmd_with_response(priv
, CMD_SET_BOOT2_VER
, &b2_cmd
))
139 lbs_deb_usb("Setting boot2 version failed\n");
141 priv
->wol_gpio
= 2; /* Wake via GPIO2... */
142 priv
->wol_gap
= 20; /* ... after 20ms */
143 lbs_host_sleep_cfg(priv
, EHS_WAKE_ON_UNICAST_DATA
,
144 (struct wol_config
*) NULL
);
146 wake_method
.hdr
.size
= cpu_to_le16(sizeof(wake_method
));
147 wake_method
.action
= cpu_to_le16(CMD_ACT_GET
);
148 if (lbs_cmd_with_response(priv
, CMD_802_11_FW_WAKE_METHOD
, &wake_method
)) {
149 netdev_info(priv
->dev
, "Firmware does not seem to support PS mode\n");
150 priv
->fwcapinfo
&= ~FW_CAPINFO_PS
;
152 if (le16_to_cpu(wake_method
.method
) == CMD_WAKE_METHOD_COMMAND_INT
) {
153 lbs_deb_usb("Firmware seems to support PS with wake-via-command\n");
155 /* The versions which boot up this way don't seem to
156 work even if we set it to the command interrupt */
157 priv
->fwcapinfo
&= ~FW_CAPINFO_PS
;
158 netdev_info(priv
->dev
,
159 "Firmware doesn't wake via command interrupt; disabling PS mode\n");
164 static void if_usb_fw_timeo(struct timer_list
*t
)
166 struct if_usb_card
*cardp
= from_timer(cardp
, t
, fw_timeout
);
168 if (cardp
->fwdnldover
) {
169 lbs_deb_usb("Download complete, no event. Assuming success\n");
171 pr_err("Download timed out\n");
172 cardp
->surprise_removed
= 1;
174 wake_up(&cardp
->fw_wq
);
178 static void if_usb_reset_olpc_card(struct lbs_private
*priv
)
180 printk(KERN_CRIT
"Resetting OLPC wireless via EC...\n");
181 olpc_ec_cmd(0x25, NULL
, 0, NULL
, 0);
186 * if_usb_probe - sets the configuration values
187 * @intf: &usb_interface pointer
188 * @id: pointer to usb_device_id
189 * returns: 0 on success, error code on failure
191 static int if_usb_probe(struct usb_interface
*intf
,
192 const struct usb_device_id
*id
)
194 struct usb_device
*udev
;
195 struct usb_host_interface
*iface_desc
;
196 struct usb_endpoint_descriptor
*endpoint
;
197 struct lbs_private
*priv
;
198 struct if_usb_card
*cardp
;
202 udev
= interface_to_usbdev(intf
);
204 cardp
= kzalloc(sizeof(struct if_usb_card
), GFP_KERNEL
);
208 timer_setup(&cardp
->fw_timeout
, if_usb_fw_timeo
, 0);
209 init_waitqueue_head(&cardp
->fw_wq
);
212 cardp
->model
= (uint32_t) id
->driver_info
;
213 iface_desc
= intf
->cur_altsetting
;
215 lbs_deb_usbd(&udev
->dev
, "bcdUSB = 0x%X bDeviceClass = 0x%X"
216 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
217 le16_to_cpu(udev
->descriptor
.bcdUSB
),
218 udev
->descriptor
.bDeviceClass
,
219 udev
->descriptor
.bDeviceSubClass
,
220 udev
->descriptor
.bDeviceProtocol
);
222 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
223 endpoint
= &iface_desc
->endpoint
[i
].desc
;
224 if (usb_endpoint_is_bulk_in(endpoint
)) {
225 cardp
->ep_in_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
226 cardp
->ep_in
= usb_endpoint_num(endpoint
);
228 lbs_deb_usbd(&udev
->dev
, "in_endpoint = %d\n", cardp
->ep_in
);
229 lbs_deb_usbd(&udev
->dev
, "Bulk in size is %d\n", cardp
->ep_in_size
);
231 } else if (usb_endpoint_is_bulk_out(endpoint
)) {
232 cardp
->ep_out_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
233 cardp
->ep_out
= usb_endpoint_num(endpoint
);
235 lbs_deb_usbd(&udev
->dev
, "out_endpoint = %d\n", cardp
->ep_out
);
236 lbs_deb_usbd(&udev
->dev
, "Bulk out size is %d\n", cardp
->ep_out_size
);
239 if (!cardp
->ep_out_size
|| !cardp
->ep_in_size
) {
240 lbs_deb_usbd(&udev
->dev
, "Endpoints not found\n");
243 if (!(cardp
->rx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
244 lbs_deb_usbd(&udev
->dev
, "Rx URB allocation failed\n");
247 if (!(cardp
->tx_urb
= usb_alloc_urb(0, GFP_KERNEL
))) {
248 lbs_deb_usbd(&udev
->dev
, "Tx URB allocation failed\n");
251 cardp
->ep_out_buf
= kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE
, GFP_KERNEL
);
252 if (!cardp
->ep_out_buf
) {
253 lbs_deb_usbd(&udev
->dev
, "Could not allocate buffer\n");
257 if (!(priv
= lbs_add_card(cardp
, &intf
->dev
)))
262 priv
->hw_host_to_card
= if_usb_host_to_card
;
263 priv
->enter_deep_sleep
= NULL
;
264 priv
->exit_deep_sleep
= NULL
;
265 priv
->reset_deep_sleep_wakeup
= NULL
;
266 priv
->is_polling
= false;
268 if (machine_is_olpc())
269 priv
->reset_card
= if_usb_reset_olpc_card
;
272 cardp
->boot2_version
= udev
->descriptor
.bcdDevice
;
275 usb_set_intfdata(intf
, cardp
);
277 r
= lbs_get_firmware_async(priv
, &udev
->dev
, cardp
->model
,
278 fw_table
, if_usb_prog_firmware
);
285 lbs_remove_card(priv
);
287 if_usb_reset_device(cardp
);
296 * if_usb_disconnect - free resource and cleanup
297 * @intf: USB interface structure
300 static void if_usb_disconnect(struct usb_interface
*intf
)
302 struct if_usb_card
*cardp
= usb_get_intfdata(intf
);
303 struct lbs_private
*priv
= cardp
->priv
;
305 cardp
->surprise_removed
= 1;
309 lbs_remove_card(priv
);
312 /* Unlink and free urb */
315 usb_set_intfdata(intf
, NULL
);
316 usb_put_dev(interface_to_usbdev(intf
));
320 * if_usb_send_fw_pkt - download FW
321 * @cardp: pointer to &struct if_usb_card
324 static int if_usb_send_fw_pkt(struct if_usb_card
*cardp
)
326 struct fwdata
*fwdata
= cardp
->ep_out_buf
;
327 const uint8_t *firmware
= cardp
->fw
->data
;
329 /* If we got a CRC failure on the last block, back
331 if (!cardp
->CRC_OK
) {
332 cardp
->totalbytes
= cardp
->fwlastblksent
;
336 lbs_deb_usb2(&cardp
->udev
->dev
, "totalbytes = %d\n",
339 /* struct fwdata (which we sent to the card) has an
340 extra __le32 field in between the header and the data,
341 which is not in the struct fwheader in the actual
342 firmware binary. Insert the seqnum in the middle... */
343 memcpy(&fwdata
->hdr
, &firmware
[cardp
->totalbytes
],
344 sizeof(struct fwheader
));
346 cardp
->fwlastblksent
= cardp
->totalbytes
;
347 cardp
->totalbytes
+= sizeof(struct fwheader
);
349 memcpy(fwdata
->data
, &firmware
[cardp
->totalbytes
],
350 le32_to_cpu(fwdata
->hdr
.datalength
));
352 lbs_deb_usb2(&cardp
->udev
->dev
, "Data length = %d\n",
353 le32_to_cpu(fwdata
->hdr
.datalength
));
355 fwdata
->seqnum
= cpu_to_le32(++cardp
->fwseqnum
);
356 cardp
->totalbytes
+= le32_to_cpu(fwdata
->hdr
.datalength
);
358 usb_tx_block(cardp
, cardp
->ep_out_buf
, sizeof(struct fwdata
) +
359 le32_to_cpu(fwdata
->hdr
.datalength
));
361 if (fwdata
->hdr
.dnldcmd
== cpu_to_le32(FW_HAS_DATA_TO_RECV
)) {
362 lbs_deb_usb2(&cardp
->udev
->dev
, "There are data to follow\n");
363 lbs_deb_usb2(&cardp
->udev
->dev
, "seqnum = %d totalbytes = %d\n",
364 cardp
->fwseqnum
, cardp
->totalbytes
);
365 } else if (fwdata
->hdr
.dnldcmd
== cpu_to_le32(FW_HAS_LAST_BLOCK
)) {
366 lbs_deb_usb2(&cardp
->udev
->dev
, "Host has finished FW downloading\n");
367 lbs_deb_usb2(&cardp
->udev
->dev
, "Donwloading FW JUMP BLOCK\n");
369 cardp
->fwfinalblk
= 1;
372 lbs_deb_usb2(&cardp
->udev
->dev
, "Firmware download done; size %d\n",
378 static int if_usb_reset_device(struct if_usb_card
*cardp
)
380 struct cmd_header
*cmd
= cardp
->ep_out_buf
+ 4;
383 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_REQUEST
);
385 cmd
->command
= cpu_to_le16(CMD_802_11_RESET
);
386 cmd
->size
= cpu_to_le16(sizeof(cmd
));
387 cmd
->result
= cpu_to_le16(0);
388 cmd
->seqnum
= cpu_to_le16(0x5a5a);
389 usb_tx_block(cardp
, cardp
->ep_out_buf
, 4 + sizeof(struct cmd_header
));
392 ret
= usb_reset_device(cardp
->udev
);
396 if (ret
&& machine_is_olpc())
397 if_usb_reset_olpc_card(NULL
);
404 * usb_tx_block - transfer the data to the device
405 * @cardp: pointer to &struct if_usb_card
406 * @payload: pointer to payload data
408 * returns: 0 for success or negative error code
410 static int usb_tx_block(struct if_usb_card
*cardp
, uint8_t *payload
, uint16_t nb
)
414 /* check if device is removed */
415 if (cardp
->surprise_removed
) {
416 lbs_deb_usbd(&cardp
->udev
->dev
, "Device removed\n");
421 usb_fill_bulk_urb(cardp
->tx_urb
, cardp
->udev
,
422 usb_sndbulkpipe(cardp
->udev
,
424 payload
, nb
, if_usb_write_bulk_callback
, cardp
);
426 cardp
->tx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
428 if ((ret
= usb_submit_urb(cardp
->tx_urb
, GFP_ATOMIC
))) {
429 lbs_deb_usbd(&cardp
->udev
->dev
, "usb_submit_urb failed: %d\n", ret
);
431 lbs_deb_usb2(&cardp
->udev
->dev
, "usb_submit_urb success\n");
439 static int __if_usb_submit_rx_urb(struct if_usb_card
*cardp
,
440 void (*callbackfn
)(struct urb
*urb
))
445 if (!(skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
))) {
446 pr_err("No free skb\n");
452 /* Fill the receive configuration URB and initialise the Rx call back */
453 usb_fill_bulk_urb(cardp
->rx_urb
, cardp
->udev
,
454 usb_rcvbulkpipe(cardp
->udev
, cardp
->ep_in
),
455 skb
->data
+ IPFIELD_ALIGN_OFFSET
,
456 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
, callbackfn
,
459 cardp
->rx_urb
->transfer_flags
|= URB_ZERO_PACKET
;
461 lbs_deb_usb2(&cardp
->udev
->dev
, "Pointer for rx_urb %p\n", cardp
->rx_urb
);
462 if ((ret
= usb_submit_urb(cardp
->rx_urb
, GFP_ATOMIC
))) {
463 lbs_deb_usbd(&cardp
->udev
->dev
, "Submit Rx URB failed: %d\n", ret
);
465 cardp
->rx_skb
= NULL
;
468 lbs_deb_usb2(&cardp
->udev
->dev
, "Submit Rx URB success\n");
476 static int if_usb_submit_rx_urb_fwload(struct if_usb_card
*cardp
)
478 return __if_usb_submit_rx_urb(cardp
, &if_usb_receive_fwload
);
481 static int if_usb_submit_rx_urb(struct if_usb_card
*cardp
)
483 return __if_usb_submit_rx_urb(cardp
, &if_usb_receive
);
486 static void if_usb_receive_fwload(struct urb
*urb
)
488 struct if_usb_card
*cardp
= urb
->context
;
489 struct sk_buff
*skb
= cardp
->rx_skb
;
490 struct fwsyncheader
*syncfwheader
;
491 struct bootcmdresp bootcmdresp
;
494 lbs_deb_usbd(&cardp
->udev
->dev
,
495 "URB status is failed during fw load\n");
500 if (cardp
->fwdnldover
) {
501 __le32
*tmp
= (__le32
*)(skb
->data
+ IPFIELD_ALIGN_OFFSET
);
503 if (tmp
[0] == cpu_to_le32(CMD_TYPE_INDICATION
) &&
504 tmp
[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY
)) {
505 pr_info("Firmware ready event received\n");
506 wake_up(&cardp
->fw_wq
);
508 lbs_deb_usb("Waiting for confirmation; got %x %x\n",
509 le32_to_cpu(tmp
[0]), le32_to_cpu(tmp
[1]));
510 if_usb_submit_rx_urb_fwload(cardp
);
515 if (cardp
->bootcmdresp
<= 0) {
516 memcpy (&bootcmdresp
, skb
->data
+ IPFIELD_ALIGN_OFFSET
,
517 sizeof(bootcmdresp
));
519 if (le16_to_cpu(cardp
->udev
->descriptor
.bcdDevice
) < 0x3106) {
521 if_usb_submit_rx_urb_fwload(cardp
);
522 cardp
->bootcmdresp
= BOOT_CMD_RESP_OK
;
523 lbs_deb_usbd(&cardp
->udev
->dev
,
524 "Received valid boot command response\n");
527 if (bootcmdresp
.magic
!= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
)) {
528 if (bootcmdresp
.magic
== cpu_to_le32(CMD_TYPE_REQUEST
) ||
529 bootcmdresp
.magic
== cpu_to_le32(CMD_TYPE_DATA
) ||
530 bootcmdresp
.magic
== cpu_to_le32(CMD_TYPE_INDICATION
)) {
531 if (!cardp
->bootcmdresp
)
532 pr_info("Firmware already seems alive; resetting\n");
533 cardp
->bootcmdresp
= -1;
535 pr_info("boot cmd response wrong magic number (0x%x)\n",
536 le32_to_cpu(bootcmdresp
.magic
));
538 } else if ((bootcmdresp
.cmd
!= BOOT_CMD_FW_BY_USB
) &&
539 (bootcmdresp
.cmd
!= BOOT_CMD_UPDATE_FW
) &&
540 (bootcmdresp
.cmd
!= BOOT_CMD_UPDATE_BOOT2
)) {
541 pr_info("boot cmd response cmd_tag error (%d)\n",
543 } else if (bootcmdresp
.result
!= BOOT_CMD_RESP_OK
) {
544 pr_info("boot cmd response result error (%d)\n",
547 cardp
->bootcmdresp
= 1;
548 lbs_deb_usbd(&cardp
->udev
->dev
,
549 "Received valid boot command response\n");
552 if_usb_submit_rx_urb_fwload(cardp
);
556 syncfwheader
= kmemdup(skb
->data
+ IPFIELD_ALIGN_OFFSET
,
557 sizeof(struct fwsyncheader
), GFP_ATOMIC
);
559 lbs_deb_usbd(&cardp
->udev
->dev
, "Failure to allocate syncfwheader\n");
564 if (!syncfwheader
->cmd
) {
565 lbs_deb_usb2(&cardp
->udev
->dev
, "FW received Blk with correct CRC\n");
566 lbs_deb_usb2(&cardp
->udev
->dev
, "FW received Blk seqnum = %d\n",
567 le32_to_cpu(syncfwheader
->seqnum
));
570 lbs_deb_usbd(&cardp
->udev
->dev
, "FW received Blk with CRC error\n");
576 /* Give device 5s to either write firmware to its RAM or eeprom */
577 mod_timer(&cardp
->fw_timeout
, jiffies
+ (HZ
*5));
579 if (cardp
->fwfinalblk
) {
580 cardp
->fwdnldover
= 1;
584 if_usb_send_fw_pkt(cardp
);
587 if_usb_submit_rx_urb_fwload(cardp
);
592 #define MRVDRV_MIN_PKT_LEN 30
594 static inline void process_cmdtypedata(int recvlength
, struct sk_buff
*skb
,
595 struct if_usb_card
*cardp
,
596 struct lbs_private
*priv
)
598 if (recvlength
> MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
+ MESSAGE_HEADER_LEN
599 || recvlength
< MRVDRV_MIN_PKT_LEN
) {
600 lbs_deb_usbd(&cardp
->udev
->dev
, "Packet length is Invalid\n");
605 skb_reserve(skb
, IPFIELD_ALIGN_OFFSET
);
606 skb_put(skb
, recvlength
);
607 skb_pull(skb
, MESSAGE_HEADER_LEN
);
609 lbs_process_rxed_packet(priv
, skb
);
612 static inline void process_cmdrequest(int recvlength
, uint8_t *recvbuff
,
614 struct if_usb_card
*cardp
,
615 struct lbs_private
*priv
)
619 if (recvlength
> LBS_CMD_BUFFER_SIZE
) {
620 lbs_deb_usbd(&cardp
->udev
->dev
,
621 "The receive buffer is too large\n");
626 BUG_ON(!in_interrupt());
628 spin_lock(&priv
->driver_lock
);
630 i
= (priv
->resp_idx
== 0) ? 1 : 0;
631 BUG_ON(priv
->resp_len
[i
]);
632 priv
->resp_len
[i
] = (recvlength
- MESSAGE_HEADER_LEN
);
633 memcpy(priv
->resp_buf
[i
], recvbuff
+ MESSAGE_HEADER_LEN
,
636 lbs_notify_command_response(priv
, i
);
638 spin_unlock(&priv
->driver_lock
);
640 lbs_deb_usbd(&cardp
->udev
->dev
,
641 "Wake up main thread to handle cmd response\n");
645 * if_usb_receive - read the packet into the upload buffer,
646 * wake up the main thread and initialise the Rx callack
648 * @urb: pointer to &struct urb
651 static void if_usb_receive(struct urb
*urb
)
653 struct if_usb_card
*cardp
= urb
->context
;
654 struct sk_buff
*skb
= cardp
->rx_skb
;
655 struct lbs_private
*priv
= cardp
->priv
;
656 int recvlength
= urb
->actual_length
;
657 uint8_t *recvbuff
= NULL
;
658 uint32_t recvtype
= 0;
659 __le32
*pkt
= (__le32
*)(skb
->data
+ IPFIELD_ALIGN_OFFSET
);
664 lbs_deb_usbd(&cardp
->udev
->dev
, "RX URB failed: %d\n",
670 recvbuff
= skb
->data
+ IPFIELD_ALIGN_OFFSET
;
671 recvtype
= le32_to_cpu(pkt
[0]);
672 lbs_deb_usbd(&cardp
->udev
->dev
,
673 "Recv length = 0x%x, Recv type = 0x%X\n",
674 recvlength
, recvtype
);
675 } else if (urb
->status
) {
682 process_cmdtypedata(recvlength
, skb
, cardp
, priv
);
685 case CMD_TYPE_REQUEST
:
686 process_cmdrequest(recvlength
, recvbuff
, skb
, cardp
, priv
);
689 case CMD_TYPE_INDICATION
:
691 event
= le32_to_cpu(pkt
[1]);
692 lbs_deb_usbd(&cardp
->udev
->dev
, "**EVENT** 0x%X\n", event
);
695 /* Icky undocumented magic special case */
696 if (event
& 0xffff0000) {
697 u32 trycount
= (event
& 0xffff0000) >> 16;
699 lbs_send_tx_feedback(priv
, trycount
);
701 lbs_queue_event(priv
, event
& 0xFF);
705 lbs_deb_usbd(&cardp
->udev
->dev
, "Unknown command type 0x%X\n",
712 if_usb_submit_rx_urb(cardp
);
716 * if_usb_host_to_card - downloads data to FW
717 * @priv: pointer to &struct lbs_private structure
718 * @type: type of data
719 * @payload: pointer to data buffer
720 * @nb: number of bytes
721 * returns: 0 for success or negative error code
723 static int if_usb_host_to_card(struct lbs_private
*priv
, uint8_t type
,
724 uint8_t *payload
, uint16_t nb
)
726 struct if_usb_card
*cardp
= priv
->card
;
728 lbs_deb_usbd(&cardp
->udev
->dev
,"*** type = %u\n", type
);
729 lbs_deb_usbd(&cardp
->udev
->dev
,"size after = %d\n", nb
);
731 if (type
== MVMS_CMD
) {
732 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_REQUEST
);
733 priv
->dnld_sent
= DNLD_CMD_SENT
;
735 *(__le32
*)cardp
->ep_out_buf
= cpu_to_le32(CMD_TYPE_DATA
);
736 priv
->dnld_sent
= DNLD_DATA_SENT
;
739 memcpy((cardp
->ep_out_buf
+ MESSAGE_HEADER_LEN
), payload
, nb
);
741 return usb_tx_block(cardp
, cardp
->ep_out_buf
, nb
+ MESSAGE_HEADER_LEN
);
745 * if_usb_issue_boot_command - issues Boot command to the Boot2 code
746 * @cardp: pointer to &if_usb_card
747 * @ivalue: 1:Boot from FW by USB-Download
748 * 2:Boot from FW in EEPROM
749 * returns: 0 for success or negative error code
751 static int if_usb_issue_boot_command(struct if_usb_card
*cardp
, int ivalue
)
753 struct bootcmd
*bootcmd
= cardp
->ep_out_buf
;
755 /* Prepare command */
756 bootcmd
->magic
= cpu_to_le32(BOOT_CMD_MAGIC_NUMBER
);
757 bootcmd
->cmd
= ivalue
;
758 memset(bootcmd
->pad
, 0, sizeof(bootcmd
->pad
));
761 usb_tx_block(cardp
, cardp
->ep_out_buf
, sizeof(*bootcmd
));
768 * check_fwfile_format - check the validity of Boot2/FW image
770 * @data: pointer to image
771 * @totlen: image length
772 * returns: 0 (good) or 1 (failure)
774 static int check_fwfile_format(const uint8_t *data
, uint32_t totlen
)
776 uint32_t bincmd
, exit
;
777 uint32_t blksize
, offset
, len
;
784 struct fwheader
*fwh
= (void *)data
;
786 bincmd
= le32_to_cpu(fwh
->dnldcmd
);
787 blksize
= le32_to_cpu(fwh
->datalength
);
789 case FW_HAS_DATA_TO_RECV
:
790 offset
= sizeof(struct fwheader
) + blksize
;
796 case FW_HAS_LAST_BLOCK
:
807 pr_err("firmware file format check FAIL\n");
809 lbs_deb_fw("firmware file format check PASS\n");
814 static void if_usb_prog_firmware(struct lbs_private
*priv
, int ret
,
815 const struct firmware
*fw
,
816 const struct firmware
*unused
)
818 struct if_usb_card
*cardp
= priv
->card
;
820 static int reset_count
= 10;
823 pr_err("failed to find firmware (%d)\n", ret
);
828 if (check_fwfile_format(cardp
->fw
->data
, cardp
->fw
->size
)) {
833 /* Cancel any pending usb business */
834 usb_kill_urb(cardp
->rx_urb
);
835 usb_kill_urb(cardp
->tx_urb
);
837 cardp
->fwlastblksent
= 0;
838 cardp
->fwdnldover
= 0;
839 cardp
->totalbytes
= 0;
840 cardp
->fwfinalblk
= 0;
841 cardp
->bootcmdresp
= 0;
844 if (if_usb_submit_rx_urb_fwload(cardp
) < 0) {
845 lbs_deb_usbd(&cardp
->udev
->dev
, "URB submission is failed\n");
850 cardp
->bootcmdresp
= 0;
854 if_usb_issue_boot_command(cardp
, BOOT_CMD_FW_BY_USB
);
855 /* wait for command response */
858 msleep_interruptible(100);
859 } while (cardp
->bootcmdresp
== 0 && j
< 10);
860 } while (cardp
->bootcmdresp
== 0 && i
< 5);
862 if (cardp
->bootcmdresp
== BOOT_CMD_RESP_NOT_SUPPORTED
) {
863 /* Return to normal operation */
865 usb_kill_urb(cardp
->rx_urb
);
866 usb_kill_urb(cardp
->tx_urb
);
867 if (if_usb_submit_rx_urb(cardp
) < 0)
870 } else if (cardp
->bootcmdresp
<= 0) {
871 if (--reset_count
>= 0) {
872 if_usb_reset_device(cardp
);
881 cardp
->totalbytes
= 0;
882 cardp
->fwlastblksent
= 0;
884 cardp
->fwdnldover
= 0;
885 cardp
->fwseqnum
= -1;
886 cardp
->totalbytes
= 0;
887 cardp
->fwfinalblk
= 0;
889 /* Send the first firmware packet... */
890 if_usb_send_fw_pkt(cardp
);
892 /* ... and wait for the process to complete */
893 wait_event_interruptible(cardp
->fw_wq
, cardp
->surprise_removed
|| cardp
->fwdnldover
);
895 del_timer_sync(&cardp
->fw_timeout
);
896 usb_kill_urb(cardp
->rx_urb
);
898 if (!cardp
->fwdnldover
) {
899 pr_info("failed to load fw, resetting device!\n");
900 if (--reset_count
>= 0) {
901 if_usb_reset_device(cardp
);
905 pr_info("FW download failure, time = %d ms\n", i
* 100);
910 cardp
->priv
->fw_ready
= 1;
911 if_usb_submit_rx_urb(cardp
);
913 if (lbs_start_card(priv
))
916 if_usb_setup_firmware(priv
);
919 * EHS_REMOVE_WAKEUP is not supported on all versions of the firmware.
921 priv
->wol_criteria
= EHS_REMOVE_WAKEUP
;
922 if (lbs_host_sleep_cfg(priv
, priv
->wol_criteria
, NULL
))
923 priv
->ehs_remove_supported
= false;
931 static int if_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
933 struct if_usb_card
*cardp
= usb_get_intfdata(intf
);
934 struct lbs_private
*priv
= cardp
->priv
;
937 if (priv
->psstate
!= PS_STATE_FULL_POWER
) {
943 if (machine_is_olpc()) {
944 if (priv
->wol_criteria
== EHS_REMOVE_WAKEUP
)
945 olpc_ec_wakeup_clear(EC_SCI_SRC_WLAN
);
947 olpc_ec_wakeup_set(EC_SCI_SRC_WLAN
);
951 ret
= lbs_suspend(priv
);
955 /* Unlink tx & rx urb */
956 usb_kill_urb(cardp
->tx_urb
);
957 usb_kill_urb(cardp
->rx_urb
);
963 static int if_usb_resume(struct usb_interface
*intf
)
965 struct if_usb_card
*cardp
= usb_get_intfdata(intf
);
966 struct lbs_private
*priv
= cardp
->priv
;
968 if_usb_submit_rx_urb(cardp
);
975 #define if_usb_suspend NULL
976 #define if_usb_resume NULL
979 static struct usb_driver if_usb_driver
= {
981 .probe
= if_usb_probe
,
982 .disconnect
= if_usb_disconnect
,
983 .id_table
= if_usb_table
,
984 .suspend
= if_usb_suspend
,
985 .resume
= if_usb_resume
,
986 .reset_resume
= if_usb_resume
,
987 .disable_hub_initiated_lpm
= 1,
990 module_usb_driver(if_usb_driver
);
992 MODULE_DESCRIPTION("8388 USB WLAN Driver");
993 MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
994 MODULE_LICENSE("GPL");