]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/libertas/if_usb.c
libertas: Communicate USB transfer errors
[net-next-2.6.git] / drivers / net / wireless / libertas / if_usb.c
CommitLineData
876c9d3a
MT
1/**
2 * This file contains functions used in USB interface module.
3 */
4#include <linux/delay.h>
084708b6 5#include <linux/moduleparam.h>
876c9d3a
MT
6#include <linux/firmware.h>
7#include <linux/netdevice.h>
5a0e3ad6 8#include <linux/slab.h>
876c9d3a
MT
9#include <linux/usb.h>
10
b77ec4ca
DW
11#ifdef CONFIG_OLPC
12#include <asm/olpc.h>
13#endif
14
ec3eef28
HS
15#define DRV_NAME "usb8xxx"
16
876c9d3a 17#include "host.h"
876c9d3a
MT
18#include "decl.h"
19#include "defs.h"
20#include "dev.h"
14e865ba 21#include "cmd.h"
876c9d3a
MT
22#include "if_usb.h"
23
eae86bf3
DW
24#define INSANEDEBUG 0
25#define lbs_deb_usb2(...) do { if (INSANEDEBUG) lbs_deb_usbd(__VA_ARGS__); } while (0)
26
876c9d3a
MT
27#define MESSAGE_HEADER_LEN 4
28
5cddea81 29static char *lbs_fw_name = NULL;
10078321 30module_param_named(fw_name, lbs_fw_name, charp, 0644);
084708b6 31
5cddea81
DW
32MODULE_FIRMWARE("libertas/usb8388_v9.bin");
33MODULE_FIRMWARE("libertas/usb8388_v5.bin");
34MODULE_FIRMWARE("libertas/usb8388.bin");
35MODULE_FIRMWARE("libertas/usb8682.bin");
a974a4bb
BH
36MODULE_FIRMWARE("usb8388.bin");
37
5cddea81
DW
38enum {
39 MODEL_UNKNOWN = 0x0,
40 MODEL_8388 = 0x1,
41 MODEL_8682 = 0x2
42};
43
876c9d3a
MT
44static struct usb_device_id if_usb_table[] = {
45 /* Enter the device signature inside */
5cddea81
DW
46 { USB_DEVICE(0x1286, 0x2001), .driver_info = MODEL_8388 },
47 { USB_DEVICE(0x05a3, 0x8388), .driver_info = MODEL_8388 },
876c9d3a
MT
48 {} /* Terminating entry */
49};
50
51MODULE_DEVICE_TABLE(usb, if_usb_table);
52
53static void if_usb_receive(struct urb *urb);
54static void if_usb_receive_fwload(struct urb *urb);
1556c0f2
BC
55static int __if_usb_prog_firmware(struct if_usb_card *cardp,
56 const char *fwname, int cmd);
57static int if_usb_prog_firmware(struct if_usb_card *cardp,
58 const char *fwname, int cmd);
eae86bf3
DW
59static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
60 uint8_t *payload, uint16_t nb);
eae86bf3
DW
61static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
62 uint16_t nb);
63static void if_usb_free(struct if_usb_card *cardp);
64static int if_usb_submit_rx_urb(struct if_usb_card *cardp);
65static int if_usb_reset_device(struct if_usb_card *cardp);
876c9d3a 66
1ff41eb0
BC
67/* sysfs hooks */
68
69/**
70 * Set function to write firmware to device's persistent memory
71 */
72static ssize_t if_usb_firmware_set(struct device *dev,
73 struct device_attribute *attr, const char *buf, size_t count)
74{
ab65f649 75 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
1ff41eb0 76 struct if_usb_card *cardp = priv->card;
1ff41eb0
BC
77 int ret;
78
5cddea81
DW
79 BUG_ON(buf == NULL);
80
cadeba31 81 ret = if_usb_prog_firmware(cardp, buf, BOOT_CMD_UPDATE_FW);
1ff41eb0
BC
82 if (ret == 0)
83 return count;
84
85 return ret;
86}
87
88/**
89 * lbs_flash_fw attribute to be exported per ethX interface through sysfs
90 * (/sys/class/net/ethX/lbs_flash_fw). Use this like so to write firmware to
91 * the device's persistent memory:
92 * echo usb8388-5.126.0.p5.bin > /sys/class/net/ethX/lbs_flash_fw
93 */
94static DEVICE_ATTR(lbs_flash_fw, 0200, NULL, if_usb_firmware_set);
95
96/**
97 * Set function to write firmware to device's persistent memory
98 */
99static ssize_t if_usb_boot2_set(struct device *dev,
100 struct device_attribute *attr, const char *buf, size_t count)
101{
ab65f649 102 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
1ff41eb0 103 struct if_usb_card *cardp = priv->card;
1ff41eb0
BC
104 int ret;
105
5cddea81
DW
106 BUG_ON(buf == NULL);
107
cadeba31 108 ret = if_usb_prog_firmware(cardp, buf, BOOT_CMD_UPDATE_BOOT2);
1ff41eb0
BC
109 if (ret == 0)
110 return count;
111
112 return ret;
113}
114
115/**
116 * lbs_flash_boot2 attribute to be exported per ethX interface through sysfs
117 * (/sys/class/net/ethX/lbs_flash_boot2). Use this like so to write firmware
118 * to the device's persistent memory:
119 * echo usb8388-5.126.0.p5.bin > /sys/class/net/ethX/lbs_flash_boot2
120 */
121static DEVICE_ATTR(lbs_flash_boot2, 0200, NULL, if_usb_boot2_set);
122
876c9d3a
MT
123/**
124 * @brief call back function to handle the status of the URB
125 * @param urb pointer to urb structure
126 * @return N/A
127 */
128static void if_usb_write_bulk_callback(struct urb *urb)
129{
eae86bf3 130 struct if_usb_card *cardp = (struct if_usb_card *) urb->context;
876c9d3a
MT
131
132 /* handle the transmission complete validations */
133
954ee164 134 if (urb->status == 0) {
69f9032d 135 struct lbs_private *priv = cardp->priv;
954ee164 136
eae86bf3
DW
137 lbs_deb_usb2(&urb->dev->dev, "URB status is successful\n");
138 lbs_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
139 urb->actual_length);
954ee164 140
1556c0f2
BC
141 /* Boot commands such as UPDATE_FW and UPDATE_BOOT2 are not
142 * passed up to the lbs level.
954ee164 143 */
1556c0f2 144 if (priv && priv->dnld_sent != DNLD_BOOTCMD_SENT)
e775ed7c 145 lbs_host_to_card_done(priv);
954ee164
DW
146 } else {
147 /* print the failure status number for debug */
148 lbs_pr_info("URB in failure status: %d\n", urb->status);
876c9d3a 149 }
876c9d3a
MT
150}
151
152/**
153 * @brief free tx/rx urb, skb and rx buffer
eae86bf3 154 * @param cardp pointer if_usb_card
876c9d3a
MT
155 * @return N/A
156 */
eae86bf3 157static void if_usb_free(struct if_usb_card *cardp)
876c9d3a 158{
9012b28a 159 lbs_deb_enter(LBS_DEB_USB);
876c9d3a
MT
160
161 /* Unlink tx & rx urb */
162 usb_kill_urb(cardp->tx_urb);
163 usb_kill_urb(cardp->rx_urb);
164
165 usb_free_urb(cardp->tx_urb);
166 cardp->tx_urb = NULL;
167
168 usb_free_urb(cardp->rx_urb);
169 cardp->rx_urb = NULL;
170
eae86bf3
DW
171 kfree(cardp->ep_out_buf);
172 cardp->ep_out_buf = NULL;
876c9d3a 173
9012b28a 174 lbs_deb_leave(LBS_DEB_USB);
876c9d3a
MT
175}
176
4694961c 177static void if_usb_setup_firmware(struct lbs_private *priv)
04c80f1a 178{
4365929d 179 struct if_usb_card *cardp = priv->card;
04c80f1a 180 struct cmd_ds_set_boot2_ver b2_cmd;
4694961c 181 struct cmd_ds_802_11_fw_wake_method wake_method;
04c80f1a 182
9fae899c 183 b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
04c80f1a 184 b2_cmd.action = 0;
4365929d 185 b2_cmd.version = cardp->boot2_version;
04c80f1a 186
b23b2061 187 if (lbs_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
04c80f1a 188 lbs_deb_usb("Setting boot2 version failed\n");
4694961c
DW
189
190 priv->wol_gpio = 2; /* Wake via GPIO2... */
191 priv->wol_gap = 20; /* ... after 20ms */
582c1b53
AN
192 lbs_host_sleep_cfg(priv, EHS_WAKE_ON_UNICAST_DATA,
193 (struct wol_config *) NULL);
4694961c
DW
194
195 wake_method.hdr.size = cpu_to_le16(sizeof(wake_method));
196 wake_method.action = cpu_to_le16(CMD_ACT_GET);
197 if (lbs_cmd_with_response(priv, CMD_802_11_FW_WAKE_METHOD, &wake_method)) {
198 lbs_pr_info("Firmware does not seem to support PS mode\n");
e0d6133c 199 priv->fwcapinfo &= ~FW_CAPINFO_PS;
4694961c
DW
200 } else {
201 if (le16_to_cpu(wake_method.method) == CMD_WAKE_METHOD_COMMAND_INT) {
202 lbs_deb_usb("Firmware seems to support PS with wake-via-command\n");
4694961c
DW
203 } else {
204 /* The versions which boot up this way don't seem to
205 work even if we set it to the command interrupt */
e0d6133c 206 priv->fwcapinfo &= ~FW_CAPINFO_PS;
4694961c
DW
207 lbs_pr_info("Firmware doesn't wake via command interrupt; disabling PS mode\n");
208 }
209 }
04c80f1a
DW
210}
211
2fd6cfe3 212static void if_usb_fw_timeo(unsigned long priv)
4f82f5c8 213{
eae86bf3 214 struct if_usb_card *cardp = (void *)priv;
04c80f1a 215
4f82f5c8
DW
216 if (cardp->fwdnldover) {
217 lbs_deb_usb("Download complete, no event. Assuming success\n");
218 } else {
219 lbs_pr_err("Download timed out\n");
220 cardp->surprise_removed = 1;
221 }
222 wake_up(&cardp->fw_wq);
223}
eae86bf3 224
b77ec4ca
DW
225#ifdef CONFIG_OLPC
226static void if_usb_reset_olpc_card(struct lbs_private *priv)
227{
228 printk(KERN_CRIT "Resetting OLPC wireless via EC...\n");
229 olpc_ec_cmd(0x25, NULL, 0, NULL, 0);
230}
231#endif
232
876c9d3a
MT
233/**
234 * @brief sets the configuration values
235 * @param ifnum interface number
236 * @param id pointer to usb_device_id
237 * @return 0 on success, error code on failure
238 */
239static int if_usb_probe(struct usb_interface *intf,
240 const struct usb_device_id *id)
241{
242 struct usb_device *udev;
243 struct usb_host_interface *iface_desc;
244 struct usb_endpoint_descriptor *endpoint;
69f9032d 245 struct lbs_private *priv;
eae86bf3 246 struct if_usb_card *cardp;
876c9d3a
MT
247 int i;
248
249 udev = interface_to_usbdev(intf);
250
eae86bf3 251 cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
435a1acb 252 if (!cardp) {
876c9d3a
MT
253 lbs_pr_err("Out of memory allocating private data.\n");
254 goto error;
255 }
256
4f82f5c8
DW
257 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
258 init_waitqueue_head(&cardp->fw_wq);
7e226272 259
435a1acb 260 cardp->udev = udev;
5cddea81 261 cardp->model = (uint32_t) id->driver_info;
876c9d3a
MT
262 iface_desc = intf->cur_altsetting;
263
9012b28a 264 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
eae86bf3 265 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
981f187b
DW
266 le16_to_cpu(udev->descriptor.bcdUSB),
267 udev->descriptor.bDeviceClass,
268 udev->descriptor.bDeviceSubClass,
269 udev->descriptor.bDeviceProtocol);
876c9d3a
MT
270
271 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
272 endpoint = &iface_desc->endpoint[i].desc;
eae86bf3
DW
273 if (usb_endpoint_is_bulk_in(endpoint)) {
274 cardp->ep_in_size = le16_to_cpu(endpoint->wMaxPacketSize);
275 cardp->ep_in = usb_endpoint_num(endpoint);
876c9d3a 276
eae86bf3
DW
277 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n", cardp->ep_in);
278 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n", cardp->ep_in_size);
876c9d3a 279
eae86bf3
DW
280 } else if (usb_endpoint_is_bulk_out(endpoint)) {
281 cardp->ep_out_size = le16_to_cpu(endpoint->wMaxPacketSize);
282 cardp->ep_out = usb_endpoint_num(endpoint);
283
284 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n", cardp->ep_out);
285 lbs_deb_usbd(&udev->dev, "Bulk out size is %d\n", cardp->ep_out_size);
876c9d3a
MT
286 }
287 }
eae86bf3
DW
288 if (!cardp->ep_out_size || !cardp->ep_in_size) {
289 lbs_deb_usbd(&udev->dev, "Endpoints not found\n");
290 goto dealloc;
291 }
292 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
293 lbs_deb_usbd(&udev->dev, "Rx URB allocation failed\n");
294 goto dealloc;
295 }
296 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
297 lbs_deb_usbd(&udev->dev, "Tx URB allocation failed\n");
298 goto dealloc;
299 }
300 cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE, GFP_KERNEL);
301 if (!cardp->ep_out_buf) {
302 lbs_deb_usbd(&udev->dev, "Could not allocate buffer\n");
303 goto dealloc;
304 }
876c9d3a 305
954ee164 306 /* Upload firmware */
1556c0f2 307 if (__if_usb_prog_firmware(cardp, lbs_fw_name, BOOT_CMD_FW_BY_USB)) {
23d36eec 308 lbs_deb_usbd(&udev->dev, "FW upload failed\n");
954ee164
DW
309 goto err_prog_firmware;
310 }
311
10078321 312 if (!(priv = lbs_add_card(cardp, &udev->dev)))
954ee164 313 goto err_prog_firmware;
3874d0fe 314
954ee164 315 cardp->priv = priv;
c9cd6f9d 316 cardp->priv->fw_ready = 1;
965f8bbc 317
208fdd2f 318 priv->hw_host_to_card = if_usb_host_to_card;
49125454
AK
319 priv->enter_deep_sleep = NULL;
320 priv->exit_deep_sleep = NULL;
321 priv->reset_deep_sleep_wakeup = NULL;
b77ec4ca
DW
322#ifdef CONFIG_OLPC
323 if (machine_is_olpc())
324 priv->reset_card = if_usb_reset_olpc_card;
325#endif
326
4365929d 327 cardp->boot2_version = udev->descriptor.bcdDevice;
208fdd2f 328
954ee164 329 if_usb_submit_rx_urb(cardp);
954ee164 330
10078321 331 if (lbs_start_card(priv))
954ee164 332 goto err_start_card;
32a74b7c 333
4694961c 334 if_usb_setup_firmware(priv);
d1f7a5b8 335
876c9d3a 336 usb_get_dev(udev);
435a1acb 337 usb_set_intfdata(intf, cardp);
876c9d3a 338
1ff41eb0
BC
339 if (device_create_file(&priv->dev->dev, &dev_attr_lbs_flash_fw))
340 lbs_pr_err("cannot register lbs_flash_fw attribute\n");
341
342 if (device_create_file(&priv->dev->dev, &dev_attr_lbs_flash_boot2))
343 lbs_pr_err("cannot register lbs_flash_boot2 attribute\n");
344
876c9d3a
MT
345 return 0;
346
954ee164 347err_start_card:
10078321 348 lbs_remove_card(priv);
954ee164
DW
349err_prog_firmware:
350 if_usb_reset_device(cardp);
876c9d3a 351dealloc:
435a1acb 352 if_usb_free(cardp);
876c9d3a
MT
353
354error:
355 return -ENOMEM;
356}
357
358/**
359 * @brief free resource and cleanup
435a1acb 360 * @param intf USB interface structure
876c9d3a
MT
361 * @return N/A
362 */
363static void if_usb_disconnect(struct usb_interface *intf)
364{
eae86bf3 365 struct if_usb_card *cardp = usb_get_intfdata(intf);
69f9032d 366 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
876c9d3a 367
954ee164 368 lbs_deb_enter(LBS_DEB_MAIN);
876c9d3a 369
1ff41eb0
BC
370 device_remove_file(&priv->dev->dev, &dev_attr_lbs_flash_boot2);
371 device_remove_file(&priv->dev->dev, &dev_attr_lbs_flash_fw);
372
954ee164 373 cardp->surprise_removed = 1;
876c9d3a 374
954ee164 375 if (priv) {
aa21c004 376 priv->surpriseremoved = 1;
10078321 377 lbs_stop_card(priv);
10078321 378 lbs_remove_card(priv);
954ee164 379 }
876c9d3a
MT
380
381 /* Unlink and free urb */
382 if_usb_free(cardp);
383
384 usb_set_intfdata(intf, NULL);
385 usb_put_dev(interface_to_usbdev(intf));
386
954ee164 387 lbs_deb_leave(LBS_DEB_MAIN);
876c9d3a
MT
388}
389
390/**
391 * @brief This function download FW
69f9032d 392 * @param priv pointer to struct lbs_private
876c9d3a
MT
393 * @return 0
394 */
eae86bf3 395static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
876c9d3a 396{
eae86bf3 397 struct fwdata *fwdata = cardp->ep_out_buf;
6dfff895 398 const uint8_t *firmware = cardp->fw->data;
876c9d3a 399
eae86bf3
DW
400 /* If we got a CRC failure on the last block, back
401 up and retry it */
876c9d3a
MT
402 if (!cardp->CRC_OK) {
403 cardp->totalbytes = cardp->fwlastblksent;
eae86bf3 404 cardp->fwseqnum--;
876c9d3a
MT
405 }
406
eae86bf3
DW
407 lbs_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
408 cardp->totalbytes);
876c9d3a 409
eae86bf3
DW
410 /* struct fwdata (which we sent to the card) has an
411 extra __le32 field in between the header and the data,
412 which is not in the struct fwheader in the actual
413 firmware binary. Insert the seqnum in the middle... */
414 memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
876c9d3a
MT
415 sizeof(struct fwheader));
416
417 cardp->fwlastblksent = cardp->totalbytes;
418 cardp->totalbytes += sizeof(struct fwheader);
419
876c9d3a 420 memcpy(fwdata->data, &firmware[cardp->totalbytes],
eae86bf3 421 le32_to_cpu(fwdata->hdr.datalength));
876c9d3a 422
eae86bf3
DW
423 lbs_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
424 le32_to_cpu(fwdata->hdr.datalength));
876c9d3a 425
eae86bf3
DW
426 fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
427 cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
876c9d3a 428
eae86bf3
DW
429 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
430 le32_to_cpu(fwdata->hdr.datalength));
431
432 if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
433 lbs_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
434 lbs_deb_usb2(&cardp->udev->dev, "seqnum = %d totalbytes = %d\n",
435 cardp->fwseqnum, cardp->totalbytes);
436 } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
437 lbs_deb_usb2(&cardp->udev->dev, "Host has finished FW downloading\n");
438 lbs_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
876c9d3a 439
876c9d3a
MT
440 cardp->fwfinalblk = 1;
441 }
442
eae86bf3
DW
443 lbs_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
444 cardp->totalbytes);
876c9d3a
MT
445
446 return 0;
447}
448
eae86bf3 449static int if_usb_reset_device(struct if_usb_card *cardp)
876c9d3a 450{
0bb64087 451 struct cmd_header *cmd = cardp->ep_out_buf + 4;
876c9d3a 452 int ret;
876c9d3a 453
3874d0fe
HS
454 lbs_deb_enter(LBS_DEB_USB);
455
eae86bf3 456 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
6d35fdfc
DW
457
458 cmd->command = cpu_to_le16(CMD_802_11_RESET);
0bb64087 459 cmd->size = cpu_to_le16(sizeof(cmd));
6d35fdfc
DW
460 cmd->result = cpu_to_le16(0);
461 cmd->seqnum = cpu_to_le16(0x5a5a);
f8e77cae 462 usb_tx_block(cardp, cardp->ep_out_buf, 4 + sizeof(struct cmd_header));
6d35fdfc 463
c8ba39d0 464 msleep(100);
876c9d3a 465 ret = usb_reset_device(cardp->udev);
c8ba39d0 466 msleep(100);
3874d0fe 467
b77ec4ca
DW
468#ifdef CONFIG_OLPC
469 if (ret && machine_is_olpc())
470 if_usb_reset_olpc_card(NULL);
471#endif
472
3874d0fe
HS
473 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
474
876c9d3a
MT
475 return ret;
476}
477
478/**
479 * @brief This function transfer the data to the device.
69f9032d 480 * @param priv pointer to struct lbs_private
876c9d3a
MT
481 * @param payload pointer to payload data
482 * @param nb data length
483 * @return 0 or -1
484 */
eae86bf3 485static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, uint16_t nb)
876c9d3a 486{
4f329c04 487 int ret;
876c9d3a
MT
488
489 /* check if device is removed */
954ee164 490 if (cardp->surprise_removed) {
9012b28a 491 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
4f329c04 492 ret = -ENODEV;
876c9d3a
MT
493 goto tx_ret;
494 }
495
496 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
497 usb_sndbulkpipe(cardp->udev,
eae86bf3 498 cardp->ep_out),
954ee164 499 payload, nb, if_usb_write_bulk_callback, cardp);
876c9d3a
MT
500
501 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
502
503 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
0856e681 504 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
876c9d3a 505 } else {
eae86bf3 506 lbs_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
876c9d3a
MT
507 ret = 0;
508 }
509
510tx_ret:
511 return ret;
512}
513
eae86bf3 514static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
954ee164 515 void (*callbackfn)(struct urb *urb))
876c9d3a 516{
876c9d3a 517 struct sk_buff *skb;
876c9d3a
MT
518 int ret = -1;
519
520 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
521 lbs_pr_err("No free skb\n");
522 goto rx_ret;
523 }
524
eae86bf3 525 cardp->rx_skb = skb;
876c9d3a
MT
526
527 /* Fill the receive configuration URB and initialise the Rx call back */
528 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
eae86bf3 529 usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
e9024a05 530 skb->data + IPFIELD_ALIGN_OFFSET,
876c9d3a 531 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
eae86bf3 532 cardp);
876c9d3a
MT
533
534 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
535
eae86bf3 536 lbs_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb);
876c9d3a 537 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
0856e681 538 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
77d8cf2c 539 kfree_skb(skb);
eae86bf3 540 cardp->rx_skb = NULL;
876c9d3a
MT
541 ret = -1;
542 } else {
eae86bf3 543 lbs_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
876c9d3a
MT
544 ret = 0;
545 }
546
547rx_ret:
548 return ret;
549}
550
eae86bf3 551static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
876c9d3a 552{
954ee164 553 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
876c9d3a
MT
554}
555
eae86bf3 556static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
876c9d3a 557{
954ee164 558 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
876c9d3a
MT
559}
560
561static void if_usb_receive_fwload(struct urb *urb)
562{
eae86bf3
DW
563 struct if_usb_card *cardp = urb->context;
564 struct sk_buff *skb = cardp->rx_skb;
876c9d3a 565 struct fwsyncheader *syncfwheader;
eae86bf3 566 struct bootcmdresp bootcmdresp;
876c9d3a
MT
567
568 if (urb->status) {
9012b28a 569 lbs_deb_usbd(&cardp->udev->dev,
eae86bf3 570 "URB status is failed during fw load\n");
876c9d3a
MT
571 kfree_skb(skb);
572 return;
573 }
574
c9cd6f9d
DW
575 if (cardp->fwdnldover) {
576 __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
577
578 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
579 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
580 lbs_pr_info("Firmware ready event received\n");
581 wake_up(&cardp->fw_wq);
582 } else {
eae86bf3
DW
583 lbs_deb_usb("Waiting for confirmation; got %x %x\n",
584 le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
c9cd6f9d
DW
585 if_usb_submit_rx_urb_fwload(cardp);
586 }
587 kfree_skb(skb);
588 return;
589 }
c8ba39d0 590 if (cardp->bootcmdresp <= 0) {
876c9d3a
MT
591 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
592 sizeof(bootcmdresp));
eae86bf3 593
981f187b 594 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
876c9d3a 595 kfree_skb(skb);
954ee164 596 if_usb_submit_rx_urb_fwload(cardp);
1556c0f2 597 cardp->bootcmdresp = BOOT_CMD_RESP_OK;
9012b28a 598 lbs_deb_usbd(&cardp->udev->dev,
eae86bf3 599 "Received valid boot command response\n");
876c9d3a
MT
600 return;
601 }
eae86bf3
DW
602 if (bootcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
603 if (bootcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
604 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
605 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
c9cd6f9d
DW
606 if (!cardp->bootcmdresp)
607 lbs_pr_info("Firmware already seems alive; resetting\n");
6d35fdfc
DW
608 cardp->bootcmdresp = -1;
609 } else {
610 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
eae86bf3 611 le32_to_cpu(bootcmdresp.magic));
6d35fdfc 612 }
1556c0f2
BC
613 } else if ((bootcmdresp.cmd != BOOT_CMD_FW_BY_USB) &&
614 (bootcmdresp.cmd != BOOT_CMD_UPDATE_FW) &&
615 (bootcmdresp.cmd != BOOT_CMD_UPDATE_BOOT2)) {
eae86bf3
DW
616 lbs_pr_info("boot cmd response cmd_tag error (%d)\n",
617 bootcmdresp.cmd);
618 } else if (bootcmdresp.result != BOOT_CMD_RESP_OK) {
619 lbs_pr_info("boot cmd response result error (%d)\n",
620 bootcmdresp.result);
876c9d3a
MT
621 } else {
622 cardp->bootcmdresp = 1;
9012b28a 623 lbs_deb_usbd(&cardp->udev->dev,
eae86bf3 624 "Received valid boot command response\n");
876c9d3a
MT
625 }
626 kfree_skb(skb);
954ee164 627 if_usb_submit_rx_urb_fwload(cardp);
876c9d3a
MT
628 return;
629 }
630
731a9b2a
JL
631 syncfwheader = kmemdup(skb->data + IPFIELD_ALIGN_OFFSET,
632 sizeof(struct fwsyncheader), GFP_ATOMIC);
876c9d3a 633 if (!syncfwheader) {
9012b28a 634 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
876c9d3a
MT
635 kfree_skb(skb);
636 return;
637 }
638
876c9d3a 639 if (!syncfwheader->cmd) {
eae86bf3
DW
640 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk with correct CRC\n");
641 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk seqnum = %d\n",
642 le32_to_cpu(syncfwheader->seqnum));
876c9d3a
MT
643 cardp->CRC_OK = 1;
644 } else {
eae86bf3 645 lbs_deb_usbd(&cardp->udev->dev, "FW received Blk with CRC error\n");
876c9d3a
MT
646 cardp->CRC_OK = 0;
647 }
648
649 kfree_skb(skb);
650
1556c0f2
BC
651 /* Give device 5s to either write firmware to its RAM or eeprom */
652 mod_timer(&cardp->fw_timeout, jiffies + (HZ*5));
4f82f5c8 653
876c9d3a
MT
654 if (cardp->fwfinalblk) {
655 cardp->fwdnldover = 1;
656 goto exit;
657 }
658
4f82f5c8 659 if_usb_send_fw_pkt(cardp);
876c9d3a 660
4f82f5c8 661 exit:
c9cd6f9d
DW
662 if_usb_submit_rx_urb_fwload(cardp);
663
876c9d3a 664 kfree(syncfwheader);
876c9d3a
MT
665}
666
667#define MRVDRV_MIN_PKT_LEN 30
668
669static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
eae86bf3 670 struct if_usb_card *cardp,
69f9032d 671 struct lbs_private *priv)
876c9d3a 672{
eae86bf3
DW
673 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
674 || recvlength < MRVDRV_MIN_PKT_LEN) {
675 lbs_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
876c9d3a
MT
676 kfree_skb(skb);
677 return;
678 }
679
680 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
681 skb_put(skb, recvlength);
682 skb_pull(skb, MESSAGE_HEADER_LEN);
eae86bf3 683
10078321 684 lbs_process_rxed_packet(priv, skb);
876c9d3a
MT
685}
686
eae86bf3 687static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
876c9d3a 688 struct sk_buff *skb,
eae86bf3 689 struct if_usb_card *cardp,
69f9032d 690 struct lbs_private *priv)
876c9d3a 691{
7919b89c
HS
692 u8 i;
693
ddac4526 694 if (recvlength > LBS_CMD_BUFFER_SIZE) {
9012b28a 695 lbs_deb_usbd(&cardp->udev->dev,
eae86bf3 696 "The receive buffer is too large\n");
876c9d3a
MT
697 kfree_skb(skb);
698 return;
699 }
700
0ee904c3 701 BUG_ON(!in_interrupt());
876c9d3a 702
aa21c004 703 spin_lock(&priv->driver_lock);
876c9d3a 704
7919b89c
HS
705 i = (priv->resp_idx == 0) ? 1 : 0;
706 BUG_ON(priv->resp_len[i]);
707 priv->resp_len[i] = (recvlength - MESSAGE_HEADER_LEN);
708 memcpy(priv->resp_buf[i], recvbuff + MESSAGE_HEADER_LEN,
709 priv->resp_len[i]);
876c9d3a 710 kfree_skb(skb);
7919b89c
HS
711 lbs_notify_command_response(priv, i);
712
aa21c004 713 spin_unlock(&priv->driver_lock);
876c9d3a 714
9012b28a 715 lbs_deb_usbd(&cardp->udev->dev,
876c9d3a 716 "Wake up main thread to handle cmd response\n");
876c9d3a
MT
717}
718
719/**
720 * @brief This function reads of the packet into the upload buff,
721 * wake up the main thread and initialise the Rx callack.
722 *
723 * @param urb pointer to struct urb
724 * @return N/A
725 */
726static void if_usb_receive(struct urb *urb)
727{
eae86bf3
DW
728 struct if_usb_card *cardp = urb->context;
729 struct sk_buff *skb = cardp->rx_skb;
69f9032d 730 struct lbs_private *priv = cardp->priv;
876c9d3a 731 int recvlength = urb->actual_length;
eae86bf3
DW
732 uint8_t *recvbuff = NULL;
733 uint32_t recvtype = 0;
734 __le32 *pkt = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
7919b89c 735 uint32_t event;
876c9d3a 736
9012b28a 737 lbs_deb_enter(LBS_DEB_USB);
876c9d3a
MT
738
739 if (recvlength) {
740 if (urb->status) {
eae86bf3
DW
741 lbs_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
742 urb->status);
876c9d3a
MT
743 kfree_skb(skb);
744 goto setup_for_next;
745 }
746
747 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
eae86bf3 748 recvtype = le32_to_cpu(pkt[0]);
9012b28a 749 lbs_deb_usbd(&cardp->udev->dev,
8362cd41
DW
750 "Recv length = 0x%x, Recv type = 0x%X\n",
751 recvlength, recvtype);
77d8cf2c
DW
752 } else if (urb->status) {
753 kfree_skb(skb);
876c9d3a 754 goto rx_exit;
77d8cf2c 755 }
876c9d3a 756
876c9d3a
MT
757 switch (recvtype) {
758 case CMD_TYPE_DATA:
759 process_cmdtypedata(recvlength, skb, cardp, priv);
760 break;
761
762 case CMD_TYPE_REQUEST:
763 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
764 break;
765
766 case CMD_TYPE_INDICATION:
7919b89c
HS
767 /* Event handling */
768 event = le32_to_cpu(pkt[1]);
769 lbs_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n", event);
770 kfree_skb(skb);
eae86bf3 771
7919b89c
HS
772 /* Icky undocumented magic special case */
773 if (event & 0xffff0000) {
774 u32 trycount = (event & 0xffff0000) >> 16;
eae86bf3 775
7919b89c
HS
776 lbs_send_tx_feedback(priv, trycount);
777 } else
778 lbs_queue_event(priv, event & 0xFF);
779 break;
eae86bf3 780
876c9d3a 781 default:
8362cd41 782 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
eae86bf3 783 recvtype);
876c9d3a
MT
784 kfree_skb(skb);
785 break;
786 }
787
788setup_for_next:
954ee164 789 if_usb_submit_rx_urb(cardp);
876c9d3a 790rx_exit:
9012b28a 791 lbs_deb_leave(LBS_DEB_USB);
876c9d3a
MT
792}
793
794/**
795 * @brief This function downloads data to FW
69f9032d 796 * @param priv pointer to struct lbs_private structure
876c9d3a
MT
797 * @param type type of data
798 * @param buf pointer to data buffer
799 * @param len number of bytes
800 * @return 0 or -1
801 */
eae86bf3
DW
802static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
803 uint8_t *payload, uint16_t nb)
876c9d3a 804{
eae86bf3 805 struct if_usb_card *cardp = priv->card;
876c9d3a 806
9012b28a
HS
807 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
808 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
876c9d3a
MT
809
810 if (type == MVMS_CMD) {
eae86bf3 811 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
634b8f49 812 priv->dnld_sent = DNLD_CMD_SENT;
876c9d3a 813 } else {
eae86bf3 814 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
634b8f49 815 priv->dnld_sent = DNLD_DATA_SENT;
876c9d3a
MT
816 }
817
eae86bf3 818 memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
876c9d3a 819
eae86bf3 820 return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN);
876c9d3a
MT
821}
822
9e22cb67
DW
823/**
824 * @brief This function issues Boot command to the Boot2 code
825 * @param ivalue 1:Boot from FW by USB-Download
826 * 2:Boot from FW in EEPROM
827 * @return 0
828 */
eae86bf3 829static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
9e22cb67 830{
eae86bf3 831 struct bootcmd *bootcmd = cardp->ep_out_buf;
9e22cb67
DW
832
833 /* Prepare command */
eae86bf3
DW
834 bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
835 bootcmd->cmd = ivalue;
836 memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
9e22cb67
DW
837
838 /* Issue command */
eae86bf3 839 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd));
9e22cb67
DW
840
841 return 0;
842}
876c9d3a
MT
843
844
954ee164
DW
845/**
846 * @brief This function checks the validity of Boot2/FW image.
847 *
848 * @param data pointer to image
849 * len image length
850 * @return 0 or -1
851 */
6dfff895 852static int check_fwfile_format(const uint8_t *data, uint32_t totlen)
954ee164 853{
eae86bf3
DW
854 uint32_t bincmd, exit;
855 uint32_t blksize, offset, len;
954ee164
DW
856 int ret;
857
858 ret = 1;
859 exit = len = 0;
860
861 do {
862 struct fwheader *fwh = (void *)data;
863
864 bincmd = le32_to_cpu(fwh->dnldcmd);
865 blksize = le32_to_cpu(fwh->datalength);
866 switch (bincmd) {
867 case FW_HAS_DATA_TO_RECV:
868 offset = sizeof(struct fwheader) + blksize;
869 data += offset;
870 len += offset;
871 if (len >= totlen)
872 exit = 1;
873 break;
874 case FW_HAS_LAST_BLOCK:
875 exit = 1;
876 ret = 0;
877 break;
878 default:
879 exit = 1;
880 break;
881 }
882 } while (!exit);
883
884 if (ret)
885 lbs_pr_err("firmware file format check FAIL\n");
886 else
887 lbs_deb_fw("firmware file format check PASS\n");
888
889 return ret;
890}
891
892
1556c0f2
BC
893/**
894* @brief This function programs the firmware subject to cmd
895*
896* @param cardp the if_usb_card descriptor
897* fwname firmware or boot2 image file name
898* cmd either BOOT_CMD_FW_BY_USB, BOOT_CMD_UPDATE_FW,
899* or BOOT_CMD_UPDATE_BOOT2.
900* @return 0 or error code
901*/
902static int if_usb_prog_firmware(struct if_usb_card *cardp,
903 const char *fwname, int cmd)
904{
905 struct lbs_private *priv = cardp->priv;
906 unsigned long flags, caps;
907 int ret;
908
909 caps = priv->fwcapinfo;
910 if (((cmd == BOOT_CMD_UPDATE_FW) && !(caps & FW_CAPINFO_FIRMWARE_UPGRADE)) ||
911 ((cmd == BOOT_CMD_UPDATE_BOOT2) && !(caps & FW_CAPINFO_BOOT2_UPGRADE)))
912 return -EOPNOTSUPP;
913
914 /* Ensure main thread is idle. */
915 spin_lock_irqsave(&priv->driver_lock, flags);
916 while (priv->cur_cmd != NULL || priv->dnld_sent != DNLD_RES_RECEIVED) {
917 spin_unlock_irqrestore(&priv->driver_lock, flags);
918 if (wait_event_interruptible(priv->waitq,
919 (priv->cur_cmd == NULL &&
920 priv->dnld_sent == DNLD_RES_RECEIVED))) {
921 return -ERESTARTSYS;
922 }
923 spin_lock_irqsave(&priv->driver_lock, flags);
924 }
925 priv->dnld_sent = DNLD_BOOTCMD_SENT;
926 spin_unlock_irqrestore(&priv->driver_lock, flags);
927
928 ret = __if_usb_prog_firmware(cardp, fwname, cmd);
929
930 spin_lock_irqsave(&priv->driver_lock, flags);
931 priv->dnld_sent = DNLD_RES_RECEIVED;
932 spin_unlock_irqrestore(&priv->driver_lock, flags);
933
934 wake_up_interruptible(&priv->waitq);
935
936 return ret;
937}
938
5cddea81
DW
939/* table of firmware file names */
940static const struct {
941 u32 model;
942 const char *fwname;
943} fw_table[] = {
944 { MODEL_8388, "libertas/usb8388_v9.bin" },
945 { MODEL_8388, "libertas/usb8388_v5.bin" },
946 { MODEL_8388, "libertas/usb8388.bin" },
947 { MODEL_8388, "usb8388.bin" },
948 { MODEL_8682, "libertas/usb8682.bin" }
949};
950
951static int get_fw(struct if_usb_card *cardp, const char *fwname)
952{
953 int i;
954
955 /* Try user-specified firmware first */
956 if (fwname)
957 return request_firmware(&cardp->fw, fwname, &cardp->udev->dev);
958
959 /* Otherwise search for firmware to use */
960 for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
961 if (fw_table[i].model != cardp->model)
962 continue;
963 if (request_firmware(&cardp->fw, fw_table[i].fwname,
964 &cardp->udev->dev) == 0)
965 return 0;
966 }
967
968 return -ENOENT;
969}
970
1556c0f2
BC
971static int __if_usb_prog_firmware(struct if_usb_card *cardp,
972 const char *fwname, int cmd)
876c9d3a 973{
876c9d3a
MT
974 int i = 0;
975 static int reset_count = 10;
9012b28a 976 int ret = 0;
876c9d3a 977
9012b28a 978 lbs_deb_enter(LBS_DEB_USB);
876c9d3a 979
5cddea81
DW
980 ret = get_fw(cardp, fwname);
981 if (ret) {
982 lbs_pr_err("failed to find firmware (%d)\n", ret);
954ee164
DW
983 goto done;
984 }
985
1556c0f2
BC
986 if (check_fwfile_format(cardp->fw->data, cardp->fw->size)) {
987 ret = -EINVAL;
954ee164 988 goto release_fw;
1556c0f2
BC
989 }
990
991 /* Cancel any pending usb business */
992 usb_kill_urb(cardp->rx_urb);
993 usb_kill_urb(cardp->tx_urb);
994
995 cardp->fwlastblksent = 0;
996 cardp->fwdnldover = 0;
997 cardp->totalbytes = 0;
998 cardp->fwfinalblk = 0;
999 cardp->bootcmdresp = 0;
876c9d3a
MT
1000
1001restart:
954ee164 1002 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
9012b28a 1003 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
1556c0f2 1004 ret = -EIO;
954ee164 1005 goto release_fw;
876c9d3a
MT
1006 }
1007
876c9d3a
MT
1008 cardp->bootcmdresp = 0;
1009 do {
1010 int j = 0;
1011 i++;
1556c0f2 1012 if_usb_issue_boot_command(cardp, cmd);
876c9d3a
MT
1013 /* wait for command response */
1014 do {
1015 j++;
1016 msleep_interruptible(100);
1017 } while (cardp->bootcmdresp == 0 && j < 10);
1018 } while (cardp->bootcmdresp == 0 && i < 5);
1019
1556c0f2
BC
1020 if (cardp->bootcmdresp == BOOT_CMD_RESP_NOT_SUPPORTED) {
1021 /* Return to normal operation */
1022 ret = -EOPNOTSUPP;
1023 usb_kill_urb(cardp->rx_urb);
1024 usb_kill_urb(cardp->tx_urb);
1025 if (if_usb_submit_rx_urb(cardp) < 0)
1026 ret = -EIO;
1027 goto release_fw;
1028 } else if (cardp->bootcmdresp <= 0) {
876c9d3a 1029 if (--reset_count >= 0) {
954ee164 1030 if_usb_reset_device(cardp);
876c9d3a
MT
1031 goto restart;
1032 }
1556c0f2
BC
1033 ret = -EIO;
1034 goto release_fw;
876c9d3a 1035 }
876c9d3a
MT
1036
1037 i = 0;
876c9d3a
MT
1038
1039 cardp->totalbytes = 0;
1040 cardp->fwlastblksent = 0;
1041 cardp->CRC_OK = 1;
1042 cardp->fwdnldover = 0;
1043 cardp->fwseqnum = -1;
1044 cardp->totalbytes = 0;
1045 cardp->fwfinalblk = 0;
1046
4f82f5c8
DW
1047 /* Send the first firmware packet... */
1048 if_usb_send_fw_pkt(cardp);
876c9d3a 1049
4f82f5c8
DW
1050 /* ... and wait for the process to complete */
1051 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
7e226272 1052
4f82f5c8 1053 del_timer_sync(&cardp->fw_timeout);
c9cd6f9d 1054 usb_kill_urb(cardp->rx_urb);
876c9d3a
MT
1055
1056 if (!cardp->fwdnldover) {
1057 lbs_pr_info("failed to load fw, resetting device!\n");
1058 if (--reset_count >= 0) {
954ee164 1059 if_usb_reset_device(cardp);
876c9d3a
MT
1060 goto restart;
1061 }
1062
1063 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
1556c0f2 1064 ret = -EIO;
954ee164 1065 goto release_fw;
876c9d3a
MT
1066 }
1067
eae86bf3 1068 release_fw:
954ee164
DW
1069 release_firmware(cardp->fw);
1070 cardp->fw = NULL;
876c9d3a 1071
eae86bf3 1072 done:
9012b28a
HS
1073 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
1074 return ret;
876c9d3a
MT
1075}
1076
1df4e8fe 1077
876c9d3a
MT
1078#ifdef CONFIG_PM
1079static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
1080{
eae86bf3 1081 struct if_usb_card *cardp = usb_get_intfdata(intf);
69f9032d 1082 struct lbs_private *priv = cardp->priv;
d1f7a5b8 1083 int ret;
876c9d3a 1084
9012b28a 1085 lbs_deb_enter(LBS_DEB_USB);
876c9d3a 1086
aa21c004 1087 if (priv->psstate != PS_STATE_FULL_POWER)
876c9d3a
MT
1088 return -1;
1089
66fceb69
AK
1090 if (priv->wol_criteria == EHS_REMOVE_WAKEUP) {
1091 lbs_pr_info("Suspend attempt without "
1092 "configuring wake params!\n");
1093 return -ENOSYS;
1094 }
1095
d1f7a5b8
DW
1096 ret = lbs_suspend(priv);
1097 if (ret)
1098 goto out;
876c9d3a
MT
1099
1100 /* Unlink tx & rx urb */
1101 usb_kill_urb(cardp->tx_urb);
1102 usb_kill_urb(cardp->rx_urb);
1103
d1f7a5b8 1104 out:
9012b28a 1105 lbs_deb_leave(LBS_DEB_USB);
d1f7a5b8 1106 return ret;
876c9d3a
MT
1107}
1108
1109static int if_usb_resume(struct usb_interface *intf)
1110{
eae86bf3 1111 struct if_usb_card *cardp = usb_get_intfdata(intf);
69f9032d 1112 struct lbs_private *priv = cardp->priv;
876c9d3a 1113
9012b28a 1114 lbs_deb_enter(LBS_DEB_USB);
876c9d3a 1115
6bc822b5 1116 if_usb_submit_rx_urb(cardp);
876c9d3a 1117
d1f7a5b8 1118 lbs_resume(priv);
876c9d3a 1119
9012b28a 1120 lbs_deb_leave(LBS_DEB_USB);
876c9d3a
MT
1121 return 0;
1122}
1123#else
1124#define if_usb_suspend NULL
1125#define if_usb_resume NULL
1126#endif
1127
1128static struct usb_driver if_usb_driver = {
798fbfec 1129 .name = DRV_NAME,
876c9d3a 1130 .probe = if_usb_probe,
876c9d3a 1131 .disconnect = if_usb_disconnect,
876c9d3a
MT
1132 .id_table = if_usb_table,
1133 .suspend = if_usb_suspend,
1134 .resume = if_usb_resume,
7b58ccfe 1135 .reset_resume = if_usb_resume,
876c9d3a
MT
1136};
1137
4fb910fd 1138static int __init if_usb_init_module(void)
876c9d3a 1139{
084708b6 1140 int ret = 0;
876c9d3a 1141
084708b6
HS
1142 lbs_deb_enter(LBS_DEB_MAIN);
1143
084708b6
HS
1144 ret = usb_register(&if_usb_driver);
1145
1146 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1147 return ret;
876c9d3a
MT
1148}
1149
4fb910fd 1150static void __exit if_usb_exit_module(void)
876c9d3a 1151{
084708b6
HS
1152 lbs_deb_enter(LBS_DEB_MAIN);
1153
876c9d3a 1154 usb_deregister(&if_usb_driver);
084708b6
HS
1155
1156 lbs_deb_leave(LBS_DEB_MAIN);
876c9d3a 1157}
084708b6
HS
1158
1159module_init(if_usb_init_module);
1160module_exit(if_usb_exit_module);
1161
1162MODULE_DESCRIPTION("8388 USB WLAN Driver");
eae86bf3 1163MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
084708b6 1164MODULE_LICENSE("GPL");