blob: e1e2128f4113fc65d0471b2e90f08f791ddf8545 [file] [log] [blame]
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001/*
2 * This file contains functions used in USB interface module.
3 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02004#include <linux/delay.h>
Holger Schurig084708b2007-05-25 12:37:58 -04005#include <linux/moduleparam.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02006#include <linux/firmware.h>
7#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02009#include <linux/usb.h>
10
David Woodhouseb77ec4c2008-05-20 16:48:52 +010011#ifdef CONFIG_OLPC
12#include <asm/olpc.h>
13#endif
14
Holger Schurigec3eef22007-05-25 12:49:10 -040015#define DRV_NAME "usb8xxx"
16
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020017#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020018#include "decl.h"
19#include "defs.h"
20#include "dev.h"
Dan Williams14e865b2007-12-10 15:11:23 -050021#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020022#include "if_usb.h"
23
David Woodhouseeae86bf2007-12-14 00:47:05 -050024#define INSANEDEBUG 0
25#define lbs_deb_usb2(...) do { if (INSANEDEBUG) lbs_deb_usbd(__VA_ARGS__); } while (0)
26
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020027#define MESSAGE_HEADER_LEN 4
28
Dan Williams5cddea82010-08-07 21:13:57 -050029static char *lbs_fw_name = NULL;
Holger Schurig10078322007-11-15 18:05:47 -050030module_param_named(fw_name, lbs_fw_name, charp, 0644);
Holger Schurig084708b2007-05-25 12:37:58 -040031
Dan Williams5cddea82010-08-07 21:13:57 -050032MODULE_FIRMWARE("libertas/usb8388_v9.bin");
33MODULE_FIRMWARE("libertas/usb8388_v5.bin");
34MODULE_FIRMWARE("libertas/usb8388.bin");
35MODULE_FIRMWARE("libertas/usb8682.bin");
Ben Hutchingsa974a4b2009-11-07 22:00:03 +000036MODULE_FIRMWARE("usb8388.bin");
37
Dan Williams5cddea82010-08-07 21:13:57 -050038enum {
39 MODEL_UNKNOWN = 0x0,
40 MODEL_8388 = 0x1,
41 MODEL_8682 = 0x2
42};
43
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020044static struct usb_device_id if_usb_table[] = {
45 /* Enter the device signature inside */
Dan Williams5cddea82010-08-07 21:13:57 -050046 { USB_DEVICE(0x1286, 0x2001), .driver_info = MODEL_8388 },
47 { USB_DEVICE(0x05a3, 0x8388), .driver_info = MODEL_8388 },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020048 {} /* 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);
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -070055static 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);
David Woodhouseeae86bf2007-12-14 00:47:05 -050059static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
60 uint8_t *payload, uint16_t nb);
David Woodhouseeae86bf2007-12-14 00:47:05 -050061static 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);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020066
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -070067/* sysfs hooks */
68
Randy Dunlap8973a6e2011-04-26 15:25:29 -070069/*
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -070070 * 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{
Kiran Divekarab65f642009-02-19 19:32:39 -050075 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -070076 struct if_usb_card *cardp = priv->card;
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -070077 int ret;
78
Dan Williams5cddea82010-08-07 21:13:57 -050079 BUG_ON(buf == NULL);
80
John W. Linvillecadeba32009-05-27 00:49:36 +020081 ret = if_usb_prog_firmware(cardp, buf, BOOT_CMD_UPDATE_FW);
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -070082 if (ret == 0)
83 return count;
84
85 return ret;
86}
87
Randy Dunlap8973a6e2011-04-26 15:25:29 -070088/*
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -070089 * 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/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070097 * if_usb_boot2_set - write firmware to device's persistent memory
98 *
99 * @dev: target device
100 * @attr: device attributes
101 * @buf: firmware buffer to write
102 * @count: number of bytes to write
103 *
104 * returns: number of bytes written or negative error code
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -0700105 */
106static ssize_t if_usb_boot2_set(struct device *dev,
107 struct device_attribute *attr, const char *buf, size_t count)
108{
Kiran Divekarab65f642009-02-19 19:32:39 -0500109 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -0700110 struct if_usb_card *cardp = priv->card;
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -0700111 int ret;
112
Dan Williams5cddea82010-08-07 21:13:57 -0500113 BUG_ON(buf == NULL);
114
John W. Linvillecadeba32009-05-27 00:49:36 +0200115 ret = if_usb_prog_firmware(cardp, buf, BOOT_CMD_UPDATE_BOOT2);
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -0700116 if (ret == 0)
117 return count;
118
119 return ret;
120}
121
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700122/*
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -0700123 * lbs_flash_boot2 attribute to be exported per ethX interface through sysfs
124 * (/sys/class/net/ethX/lbs_flash_boot2). Use this like so to write firmware
125 * to the device's persistent memory:
126 * echo usb8388-5.126.0.p5.bin > /sys/class/net/ethX/lbs_flash_boot2
127 */
128static DEVICE_ATTR(lbs_flash_boot2, 0200, NULL, if_usb_boot2_set);
129
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200130/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700131 * if_usb_write_bulk_callback - callback function to handle the status
132 * of the URB
133 * @urb: pointer to &urb structure
134 * returns: N/A
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200135 */
136static void if_usb_write_bulk_callback(struct urb *urb)
137{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500138 struct if_usb_card *cardp = (struct if_usb_card *) urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200139
140 /* handle the transmission complete validations */
141
Dan Williams954ee162007-08-20 11:43:25 -0400142 if (urb->status == 0) {
Holger Schurig69f90322007-11-23 15:43:44 +0100143 struct lbs_private *priv = cardp->priv;
Dan Williams954ee162007-08-20 11:43:25 -0400144
David Woodhouseeae86bf2007-12-14 00:47:05 -0500145 lbs_deb_usb2(&urb->dev->dev, "URB status is successful\n");
146 lbs_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
147 urb->actual_length);
Dan Williams954ee162007-08-20 11:43:25 -0400148
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700149 /* Boot commands such as UPDATE_FW and UPDATE_BOOT2 are not
150 * passed up to the lbs level.
Dan Williams954ee162007-08-20 11:43:25 -0400151 */
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700152 if (priv && priv->dnld_sent != DNLD_BOOTCMD_SENT)
David Woodhousee775ed72007-12-06 14:36:11 +0000153 lbs_host_to_card_done(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400154 } else {
155 /* print the failure status number for debug */
156 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200157 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200158}
159
160/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700161 * if_usb_free - free tx/rx urb, skb and rx buffer
162 * @cardp: pointer to &if_usb_card
163 * returns: N/A
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200164 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500165static void if_usb_free(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200166{
Holger Schurig9012b282007-05-25 11:27:16 -0400167 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200168
169 /* Unlink tx & rx urb */
170 usb_kill_urb(cardp->tx_urb);
171 usb_kill_urb(cardp->rx_urb);
172
173 usb_free_urb(cardp->tx_urb);
174 cardp->tx_urb = NULL;
175
176 usb_free_urb(cardp->rx_urb);
177 cardp->rx_urb = NULL;
178
David Woodhouseeae86bf2007-12-14 00:47:05 -0500179 kfree(cardp->ep_out_buf);
180 cardp->ep_out_buf = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200181
Holger Schurig9012b282007-05-25 11:27:16 -0400182 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200183}
184
David Woodhouse46949612007-12-17 14:42:33 -0500185static void if_usb_setup_firmware(struct lbs_private *priv)
David Woodhouse04c80f12007-12-06 12:51:00 +0000186{
Holger Schurig43659292008-01-16 15:52:58 +0100187 struct if_usb_card *cardp = priv->card;
David Woodhouse04c80f12007-12-06 12:51:00 +0000188 struct cmd_ds_set_boot2_ver b2_cmd;
David Woodhouse46949612007-12-17 14:42:33 -0500189 struct cmd_ds_802_11_fw_wake_method wake_method;
David Woodhouse04c80f12007-12-06 12:51:00 +0000190
David Woodhouse9fae8992007-12-15 03:46:44 -0500191 b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
David Woodhouse04c80f12007-12-06 12:51:00 +0000192 b2_cmd.action = 0;
Holger Schurig43659292008-01-16 15:52:58 +0100193 b2_cmd.version = cardp->boot2_version;
David Woodhouse04c80f12007-12-06 12:51:00 +0000194
David Woodhouseb23b2062007-12-15 01:22:09 -0500195 if (lbs_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
David Woodhouse04c80f12007-12-06 12:51:00 +0000196 lbs_deb_usb("Setting boot2 version failed\n");
David Woodhouse46949612007-12-17 14:42:33 -0500197
198 priv->wol_gpio = 2; /* Wake via GPIO2... */
199 priv->wol_gap = 20; /* ... after 20ms */
Anna Neal582c1b52008-10-20 16:46:56 -0700200 lbs_host_sleep_cfg(priv, EHS_WAKE_ON_UNICAST_DATA,
201 (struct wol_config *) NULL);
David Woodhouse46949612007-12-17 14:42:33 -0500202
203 wake_method.hdr.size = cpu_to_le16(sizeof(wake_method));
204 wake_method.action = cpu_to_le16(CMD_ACT_GET);
205 if (lbs_cmd_with_response(priv, CMD_802_11_FW_WAKE_METHOD, &wake_method)) {
206 lbs_pr_info("Firmware does not seem to support PS mode\n");
Andrey Yurovskye0d61332009-06-16 13:20:01 -0700207 priv->fwcapinfo &= ~FW_CAPINFO_PS;
David Woodhouse46949612007-12-17 14:42:33 -0500208 } else {
209 if (le16_to_cpu(wake_method.method) == CMD_WAKE_METHOD_COMMAND_INT) {
210 lbs_deb_usb("Firmware seems to support PS with wake-via-command\n");
David Woodhouse46949612007-12-17 14:42:33 -0500211 } else {
212 /* The versions which boot up this way don't seem to
213 work even if we set it to the command interrupt */
Andrey Yurovskye0d61332009-06-16 13:20:01 -0700214 priv->fwcapinfo &= ~FW_CAPINFO_PS;
David Woodhouse46949612007-12-17 14:42:33 -0500215 lbs_pr_info("Firmware doesn't wake via command interrupt; disabling PS mode\n");
216 }
217 }
David Woodhouse04c80f12007-12-06 12:51:00 +0000218}
219
David Woodhouse2fd6cfe2007-12-11 17:44:10 -0500220static void if_usb_fw_timeo(unsigned long priv)
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500221{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500222 struct if_usb_card *cardp = (void *)priv;
David Woodhouse04c80f12007-12-06 12:51:00 +0000223
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500224 if (cardp->fwdnldover) {
225 lbs_deb_usb("Download complete, no event. Assuming success\n");
226 } else {
227 lbs_pr_err("Download timed out\n");
228 cardp->surprise_removed = 1;
229 }
230 wake_up(&cardp->fw_wq);
231}
David Woodhouseeae86bf2007-12-14 00:47:05 -0500232
David Woodhouseb77ec4c2008-05-20 16:48:52 +0100233#ifdef CONFIG_OLPC
234static void if_usb_reset_olpc_card(struct lbs_private *priv)
235{
236 printk(KERN_CRIT "Resetting OLPC wireless via EC...\n");
237 olpc_ec_cmd(0x25, NULL, 0, NULL, 0);
238}
239#endif
240
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200241/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700242 * if_usb_probe - sets the configuration values
243 * @intf: &usb_interface pointer
244 * @id: pointer to usb_device_id
245 * returns: 0 on success, error code on failure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200246 */
247static int if_usb_probe(struct usb_interface *intf,
248 const struct usb_device_id *id)
249{
250 struct usb_device *udev;
251 struct usb_host_interface *iface_desc;
252 struct usb_endpoint_descriptor *endpoint;
Holger Schurig69f90322007-11-23 15:43:44 +0100253 struct lbs_private *priv;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500254 struct if_usb_card *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200255 int i;
256
257 udev = interface_to_usbdev(intf);
258
David Woodhouseeae86bf2007-12-14 00:47:05 -0500259 cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400260 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200261 lbs_pr_err("Out of memory allocating private data.\n");
262 goto error;
263 }
264
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500265 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
266 init_waitqueue_head(&cardp->fw_wq);
David Woodhouse7e226272007-12-14 22:53:41 -0500267
Holger Schurig435a1ac2007-05-25 12:41:52 -0400268 cardp->udev = udev;
Dan Williams5cddea82010-08-07 21:13:57 -0500269 cardp->model = (uint32_t) id->driver_info;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200270 iface_desc = intf->cur_altsetting;
271
Holger Schurig9012b282007-05-25 11:27:16 -0400272 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
David Woodhouseeae86bf2007-12-14 00:47:05 -0500273 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400274 le16_to_cpu(udev->descriptor.bcdUSB),
275 udev->descriptor.bDeviceClass,
276 udev->descriptor.bDeviceSubClass,
277 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200278
279 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
280 endpoint = &iface_desc->endpoint[i].desc;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500281 if (usb_endpoint_is_bulk_in(endpoint)) {
282 cardp->ep_in_size = le16_to_cpu(endpoint->wMaxPacketSize);
283 cardp->ep_in = usb_endpoint_num(endpoint);
284
285 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n", cardp->ep_in);
286 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n", cardp->ep_in_size);
287
288 } else if (usb_endpoint_is_bulk_out(endpoint)) {
289 cardp->ep_out_size = le16_to_cpu(endpoint->wMaxPacketSize);
290 cardp->ep_out = usb_endpoint_num(endpoint);
291
292 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n", cardp->ep_out);
293 lbs_deb_usbd(&udev->dev, "Bulk out size is %d\n", cardp->ep_out_size);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200294 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500295 }
296 if (!cardp->ep_out_size || !cardp->ep_in_size) {
297 lbs_deb_usbd(&udev->dev, "Endpoints not found\n");
298 goto dealloc;
299 }
300 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
301 lbs_deb_usbd(&udev->dev, "Rx URB allocation failed\n");
302 goto dealloc;
303 }
304 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
305 lbs_deb_usbd(&udev->dev, "Tx URB allocation failed\n");
306 goto dealloc;
307 }
308 cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE, GFP_KERNEL);
309 if (!cardp->ep_out_buf) {
310 lbs_deb_usbd(&udev->dev, "Could not allocate buffer\n");
311 goto dealloc;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200312 }
313
Dan Williams954ee162007-08-20 11:43:25 -0400314 /* Upload firmware */
Rusty Russelld6d1b652010-08-11 23:04:27 -0600315 kparam_block_sysfs_write(fw_name);
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700316 if (__if_usb_prog_firmware(cardp, lbs_fw_name, BOOT_CMD_FW_BY_USB)) {
Rusty Russelld6d1b652010-08-11 23:04:27 -0600317 kparam_unblock_sysfs_write(fw_name);
David Woodhouse23d36ee2007-12-12 00:14:21 -0500318 lbs_deb_usbd(&udev->dev, "FW upload failed\n");
Dan Williams954ee162007-08-20 11:43:25 -0400319 goto err_prog_firmware;
320 }
Rusty Russelld6d1b652010-08-11 23:04:27 -0600321 kparam_unblock_sysfs_write(fw_name);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400322
Holger Schurig10078322007-11-15 18:05:47 -0500323 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400324 goto err_prog_firmware;
325
326 cardp->priv = priv;
David Woodhousec9cd6f92007-12-11 13:15:25 -0500327 cardp->priv->fw_ready = 1;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400328
Holger Schurig208fdd22007-05-25 12:17:06 -0400329 priv->hw_host_to_card = if_usb_host_to_card;
Amitkumar Karwar49125452009-09-30 20:04:38 -0700330 priv->enter_deep_sleep = NULL;
331 priv->exit_deep_sleep = NULL;
332 priv->reset_deep_sleep_wakeup = NULL;
David Woodhouseb77ec4c2008-05-20 16:48:52 +0100333#ifdef CONFIG_OLPC
334 if (machine_is_olpc())
335 priv->reset_card = if_usb_reset_olpc_card;
336#endif
337
Holger Schurig43659292008-01-16 15:52:58 +0100338 cardp->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400339
Dan Williams954ee162007-08-20 11:43:25 -0400340 if_usb_submit_rx_urb(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400341
Holger Schurig10078322007-11-15 18:05:47 -0500342 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400343 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400344
David Woodhouse46949612007-12-17 14:42:33 -0500345 if_usb_setup_firmware(priv);
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500346
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200347 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400348 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200349
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -0700350 if (device_create_file(&priv->dev->dev, &dev_attr_lbs_flash_fw))
351 lbs_pr_err("cannot register lbs_flash_fw attribute\n");
352
353 if (device_create_file(&priv->dev->dev, &dev_attr_lbs_flash_boot2))
354 lbs_pr_err("cannot register lbs_flash_boot2 attribute\n");
355
Deepak Saxenaae63a332010-10-31 13:40:33 +0000356 /*
357 * EHS_REMOVE_WAKEUP is not supported on all versions of the firmware.
358 */
359 priv->wol_criteria = EHS_REMOVE_WAKEUP;
360 if (lbs_host_sleep_cfg(priv, priv->wol_criteria, NULL))
361 priv->ehs_remove_supported = false;
362
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200363 return 0;
364
Dan Williams954ee162007-08-20 11:43:25 -0400365err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500366 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400367err_prog_firmware:
368 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200369dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400370 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200371
372error:
373 return -ENOMEM;
374}
375
376/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700377 * if_usb_disconnect - free resource and cleanup
378 * @intf: USB interface structure
379 * returns: N/A
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200380 */
381static void if_usb_disconnect(struct usb_interface *intf)
382{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500383 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100384 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200385
Dan Williams954ee162007-08-20 11:43:25 -0400386 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200387
Brian Cavagnolo1ff41eb2008-07-21 11:03:16 -0700388 device_remove_file(&priv->dev->dev, &dev_attr_lbs_flash_boot2);
389 device_remove_file(&priv->dev->dev, &dev_attr_lbs_flash_fw);
390
Dan Williams954ee162007-08-20 11:43:25 -0400391 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200392
Dan Williams954ee162007-08-20 11:43:25 -0400393 if (priv) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000394 priv->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500395 lbs_stop_card(priv);
Holger Schurig10078322007-11-15 18:05:47 -0500396 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400397 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200398
399 /* Unlink and free urb */
400 if_usb_free(cardp);
401
402 usb_set_intfdata(intf, NULL);
403 usb_put_dev(interface_to_usbdev(intf));
404
Dan Williams954ee162007-08-20 11:43:25 -0400405 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200406}
407
408/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700409 * if_usb_send_fw_pkt - download FW
410 * @cardp: pointer to &struct if_usb_card
411 * returns: 0
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200412 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500413static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200414{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500415 struct fwdata *fwdata = cardp->ep_out_buf;
David Woodhouse6dfff892008-05-23 18:37:51 +0100416 const uint8_t *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200417
David Woodhouseeae86bf2007-12-14 00:47:05 -0500418 /* If we got a CRC failure on the last block, back
419 up and retry it */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200420 if (!cardp->CRC_OK) {
421 cardp->totalbytes = cardp->fwlastblksent;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500422 cardp->fwseqnum--;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200423 }
424
David Woodhouseeae86bf2007-12-14 00:47:05 -0500425 lbs_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
426 cardp->totalbytes);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200427
David Woodhouseeae86bf2007-12-14 00:47:05 -0500428 /* struct fwdata (which we sent to the card) has an
429 extra __le32 field in between the header and the data,
430 which is not in the struct fwheader in the actual
431 firmware binary. Insert the seqnum in the middle... */
432 memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200433 sizeof(struct fwheader));
434
435 cardp->fwlastblksent = cardp->totalbytes;
436 cardp->totalbytes += sizeof(struct fwheader);
437
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouseeae86bf2007-12-14 00:47:05 -0500439 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200440
David Woodhouseeae86bf2007-12-14 00:47:05 -0500441 lbs_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
442 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200443
David Woodhouseeae86bf2007-12-14 00:47:05 -0500444 fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
445 cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446
David Woodhouseeae86bf2007-12-14 00:47:05 -0500447 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
448 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200449
David Woodhouseeae86bf2007-12-14 00:47:05 -0500450 if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
451 lbs_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
452 lbs_deb_usb2(&cardp->udev->dev, "seqnum = %d totalbytes = %d\n",
453 cardp->fwseqnum, cardp->totalbytes);
454 } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
455 lbs_deb_usb2(&cardp->udev->dev, "Host has finished FW downloading\n");
456 lbs_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200457
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200458 cardp->fwfinalblk = 1;
459 }
460
David Woodhouseeae86bf2007-12-14 00:47:05 -0500461 lbs_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
462 cardp->totalbytes);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200463
464 return 0;
465}
466
David Woodhouseeae86bf2007-12-14 00:47:05 -0500467static int if_usb_reset_device(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200468{
Dan Williams0bb64082010-07-27 13:08:08 -0700469 struct cmd_header *cmd = cardp->ep_out_buf + 4;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200470 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200471
Holger Schurig3874d0f2007-05-25 12:01:42 -0400472 lbs_deb_enter(LBS_DEB_USB);
473
David Woodhouseeae86bf2007-12-14 00:47:05 -0500474 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000475
476 cmd->command = cpu_to_le16(CMD_802_11_RESET);
Dan Williams0bb64082010-07-27 13:08:08 -0700477 cmd->size = cpu_to_le16(sizeof(cmd));
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000478 cmd->result = cpu_to_le16(0);
479 cmd->seqnum = cpu_to_le16(0x5a5a);
Dan Williamsf8e77ca2008-09-10 09:04:33 -0400480 usb_tx_block(cardp, cardp->ep_out_buf, 4 + sizeof(struct cmd_header));
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000481
David Woodhousec8ba39d2007-12-10 18:53:34 -0500482 msleep(100);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200483 ret = usb_reset_device(cardp->udev);
David Woodhousec8ba39d2007-12-10 18:53:34 -0500484 msleep(100);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400485
David Woodhouseb77ec4c2008-05-20 16:48:52 +0100486#ifdef CONFIG_OLPC
487 if (ret && machine_is_olpc())
488 if_usb_reset_olpc_card(NULL);
489#endif
490
Holger Schurig3874d0f2007-05-25 12:01:42 -0400491 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
492
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200493 return ret;
494}
495
496/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700497 * usb_tx_block - transfer the data to the device
498 * @cardp: pointer to &struct if_usb_card
499 * @payload: pointer to payload data
500 * @nb: data length
501 * returns: 0 for success or negative error code
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200502 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500503static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, uint16_t nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200504{
Paul Fox4f329c02010-10-13 20:14:56 +0100505 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200506
507 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400508 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400509 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Paul Fox4f329c02010-10-13 20:14:56 +0100510 ret = -ENODEV;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200511 goto tx_ret;
512 }
513
514 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
515 usb_sndbulkpipe(cardp->udev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500516 cardp->ep_out),
Dan Williams954ee162007-08-20 11:43:25 -0400517 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200518
519 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
520
521 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
David Woodhouse0856e682007-12-07 15:12:26 +0000522 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200523 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500524 lbs_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200525 ret = 0;
526 }
527
528tx_ret:
529 return ret;
530}
531
David Woodhouseeae86bf2007-12-14 00:47:05 -0500532static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
Dan Williams954ee162007-08-20 11:43:25 -0400533 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200534{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200535 struct sk_buff *skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200536 int ret = -1;
537
538 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
539 lbs_pr_err("No free skb\n");
540 goto rx_ret;
541 }
542
David Woodhouseeae86bf2007-12-14 00:47:05 -0500543 cardp->rx_skb = skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200544
545 /* Fill the receive configuration URB and initialise the Rx call back */
546 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500547 usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
David Woodhousee9024a02009-10-30 17:45:14 +0000548 skb->data + IPFIELD_ALIGN_OFFSET,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200549 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500550 cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200551
552 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
553
David Woodhouseeae86bf2007-12-14 00:47:05 -0500554 lbs_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200555 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
David Woodhouse0856e682007-12-07 15:12:26 +0000556 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000557 kfree_skb(skb);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500558 cardp->rx_skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559 ret = -1;
560 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500561 lbs_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200562 ret = 0;
563 }
564
565rx_ret:
566 return ret;
567}
568
David Woodhouseeae86bf2007-12-14 00:47:05 -0500569static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200570{
Dan Williams954ee162007-08-20 11:43:25 -0400571 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200572}
573
David Woodhouseeae86bf2007-12-14 00:47:05 -0500574static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200575{
Dan Williams954ee162007-08-20 11:43:25 -0400576 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200577}
578
579static void if_usb_receive_fwload(struct urb *urb)
580{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500581 struct if_usb_card *cardp = urb->context;
582 struct sk_buff *skb = cardp->rx_skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200583 struct fwsyncheader *syncfwheader;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500584 struct bootcmdresp bootcmdresp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200585
586 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400587 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500588 "URB status is failed during fw load\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200589 kfree_skb(skb);
590 return;
591 }
592
David Woodhousec9cd6f92007-12-11 13:15:25 -0500593 if (cardp->fwdnldover) {
594 __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
595
596 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
597 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
598 lbs_pr_info("Firmware ready event received\n");
599 wake_up(&cardp->fw_wq);
600 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500601 lbs_deb_usb("Waiting for confirmation; got %x %x\n",
602 le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
David Woodhousec9cd6f92007-12-11 13:15:25 -0500603 if_usb_submit_rx_urb_fwload(cardp);
604 }
605 kfree_skb(skb);
606 return;
607 }
David Woodhousec8ba39d2007-12-10 18:53:34 -0500608 if (cardp->bootcmdresp <= 0) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
610 sizeof(bootcmdresp));
David Woodhouseeae86bf2007-12-14 00:47:05 -0500611
David Woodhouse981f1872007-05-25 23:36:54 -0400612 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200613 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400614 if_usb_submit_rx_urb_fwload(cardp);
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700615 cardp->bootcmdresp = BOOT_CMD_RESP_OK;
Holger Schurig9012b282007-05-25 11:27:16 -0400616 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500617 "Received valid boot command response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200618 return;
619 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500620 if (bootcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
621 if (bootcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
622 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
623 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
David Woodhousec9cd6f92007-12-11 13:15:25 -0500624 if (!cardp->bootcmdresp)
625 lbs_pr_info("Firmware already seems alive; resetting\n");
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000626 cardp->bootcmdresp = -1;
627 } else {
628 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
David Woodhouseeae86bf2007-12-14 00:47:05 -0500629 le32_to_cpu(bootcmdresp.magic));
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000630 }
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700631 } else if ((bootcmdresp.cmd != BOOT_CMD_FW_BY_USB) &&
632 (bootcmdresp.cmd != BOOT_CMD_UPDATE_FW) &&
633 (bootcmdresp.cmd != BOOT_CMD_UPDATE_BOOT2)) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500634 lbs_pr_info("boot cmd response cmd_tag error (%d)\n",
635 bootcmdresp.cmd);
636 } else if (bootcmdresp.result != BOOT_CMD_RESP_OK) {
637 lbs_pr_info("boot cmd response result error (%d)\n",
638 bootcmdresp.result);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200639 } else {
640 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400641 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500642 "Received valid boot command response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200643 }
644 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400645 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200646 return;
647 }
648
Julia Lawall731a9b22010-05-15 23:12:28 +0200649 syncfwheader = kmemdup(skb->data + IPFIELD_ALIGN_OFFSET,
650 sizeof(struct fwsyncheader), GFP_ATOMIC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200651 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400652 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200653 kfree_skb(skb);
654 return;
655 }
656
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200657 if (!syncfwheader->cmd) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500658 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk with correct CRC\n");
659 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk seqnum = %d\n",
660 le32_to_cpu(syncfwheader->seqnum));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200661 cardp->CRC_OK = 1;
662 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500663 lbs_deb_usbd(&cardp->udev->dev, "FW received Blk with CRC error\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200664 cardp->CRC_OK = 0;
665 }
666
667 kfree_skb(skb);
668
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700669 /* Give device 5s to either write firmware to its RAM or eeprom */
670 mod_timer(&cardp->fw_timeout, jiffies + (HZ*5));
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500671
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200672 if (cardp->fwfinalblk) {
673 cardp->fwdnldover = 1;
674 goto exit;
675 }
676
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500677 if_usb_send_fw_pkt(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200678
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500679 exit:
David Woodhousec9cd6f92007-12-11 13:15:25 -0500680 if_usb_submit_rx_urb_fwload(cardp);
681
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200682 kfree(syncfwheader);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683}
684
685#define MRVDRV_MIN_PKT_LEN 30
686
687static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500688 struct if_usb_card *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100689 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200690{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500691 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
692 || recvlength < MRVDRV_MIN_PKT_LEN) {
693 lbs_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694 kfree_skb(skb);
695 return;
696 }
697
698 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
699 skb_put(skb, recvlength);
700 skb_pull(skb, MESSAGE_HEADER_LEN);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500701
Holger Schurig10078322007-11-15 18:05:47 -0500702 lbs_process_rxed_packet(priv, skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703}
704
David Woodhouseeae86bf2007-12-14 00:47:05 -0500705static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200706 struct sk_buff *skb,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500707 struct if_usb_card *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100708 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200709{
Holger Schurig7919b892008-04-01 14:50:43 +0200710 u8 i;
711
Dan Williamsddac4522007-12-11 13:49:39 -0500712 if (recvlength > LBS_CMD_BUFFER_SIZE) {
Holger Schurig9012b282007-05-25 11:27:16 -0400713 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500714 "The receive buffer is too large\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200715 kfree_skb(skb);
716 return;
717 }
718
Alexander Beregalov0ee904c2009-04-11 14:50:23 +0000719 BUG_ON(!in_interrupt());
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200720
David Woodhouseaa21c002007-12-08 20:04:36 +0000721 spin_lock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722
Holger Schurig7919b892008-04-01 14:50:43 +0200723 i = (priv->resp_idx == 0) ? 1 : 0;
724 BUG_ON(priv->resp_len[i]);
725 priv->resp_len[i] = (recvlength - MESSAGE_HEADER_LEN);
726 memcpy(priv->resp_buf[i], recvbuff + MESSAGE_HEADER_LEN,
727 priv->resp_len[i]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200728 kfree_skb(skb);
Holger Schurig7919b892008-04-01 14:50:43 +0200729 lbs_notify_command_response(priv, i);
730
David Woodhouseaa21c002007-12-08 20:04:36 +0000731 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200732
Holger Schurig9012b282007-05-25 11:27:16 -0400733 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200734 "Wake up main thread to handle cmd response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200735}
736
737/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700738 * if_usb_receive - read the packet into the upload buffer,
739 * wake up the main thread and initialise the Rx callack
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200740 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700741 * @urb: pointer to &struct urb
742 * returns: N/A
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200743 */
744static void if_usb_receive(struct urb *urb)
745{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500746 struct if_usb_card *cardp = urb->context;
747 struct sk_buff *skb = cardp->rx_skb;
Holger Schurig69f90322007-11-23 15:43:44 +0100748 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200749 int recvlength = urb->actual_length;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500750 uint8_t *recvbuff = NULL;
751 uint32_t recvtype = 0;
752 __le32 *pkt = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
Holger Schurig7919b892008-04-01 14:50:43 +0200753 uint32_t event;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200754
Holger Schurig9012b282007-05-25 11:27:16 -0400755 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756
757 if (recvlength) {
758 if (urb->status) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500759 lbs_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
760 urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200761 kfree_skb(skb);
762 goto setup_for_next;
763 }
764
765 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500766 recvtype = le32_to_cpu(pkt[0]);
Holger Schurig9012b282007-05-25 11:27:16 -0400767 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400768 "Recv length = 0x%x, Recv type = 0x%X\n",
769 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000770 } else if (urb->status) {
771 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200772 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000773 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200774
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200775 switch (recvtype) {
776 case CMD_TYPE_DATA:
777 process_cmdtypedata(recvlength, skb, cardp, priv);
778 break;
779
780 case CMD_TYPE_REQUEST:
781 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
782 break;
783
784 case CMD_TYPE_INDICATION:
Holger Schurig7919b892008-04-01 14:50:43 +0200785 /* Event handling */
786 event = le32_to_cpu(pkt[1]);
787 lbs_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n", event);
788 kfree_skb(skb);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500789
790 /* Icky undocumented magic special case */
Holger Schurig7919b892008-04-01 14:50:43 +0200791 if (event & 0xffff0000) {
792 u32 trycount = (event & 0xffff0000) >> 16;
793
794 lbs_send_tx_feedback(priv, trycount);
795 } else
796 lbs_queue_event(priv, event & 0xFF);
797 break;
798
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200799 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400800 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
David Woodhouseeae86bf2007-12-14 00:47:05 -0500801 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200802 kfree_skb(skb);
803 break;
804 }
805
806setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400807 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200808rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400809 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200810}
811
812/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700813 * if_usb_host_to_card - downloads data to FW
814 * @priv: pointer to &struct lbs_private structure
815 * @type: type of data
816 * @payload: pointer to data buffer
817 * @nb: number of bytes
818 * returns: 0 for success or negative error code
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200819 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500820static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
821 uint8_t *payload, uint16_t nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200822{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500823 struct if_usb_card *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200824
Holger Schurig9012b282007-05-25 11:27:16 -0400825 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
826 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200827
828 if (type == MVMS_CMD) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500829 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400830 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200831 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500832 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400833 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200834 }
835
David Woodhouseeae86bf2007-12-14 00:47:05 -0500836 memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200837
David Woodhouseeae86bf2007-12-14 00:47:05 -0500838 return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200839}
840
Dan Williams9e22cb62007-08-02 11:14:49 -0400841/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700842 * if_usb_issue_boot_command - issues Boot command to the Boot2 code
843 * @cardp: pointer to &if_usb_card
844 * @ivalue: 1:Boot from FW by USB-Download
845 * 2:Boot from FW in EEPROM
846 * returns: 0 for success or negative error code
Dan Williams9e22cb62007-08-02 11:14:49 -0400847 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500848static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400849{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500850 struct bootcmd *bootcmd = cardp->ep_out_buf;
Dan Williams9e22cb62007-08-02 11:14:49 -0400851
852 /* Prepare command */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500853 bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
854 bootcmd->cmd = ivalue;
855 memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
Dan Williams9e22cb62007-08-02 11:14:49 -0400856
857 /* Issue command */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500858 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd));
Dan Williams9e22cb62007-08-02 11:14:49 -0400859
860 return 0;
861}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200862
863
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400864/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700865 * check_fwfile_format - check the validity of Boot2/FW image
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400866 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700867 * @data: pointer to image
868 * @totlen: image length
869 * returns: 0 (good) or 1 (failure)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400870 */
David Woodhouse6dfff892008-05-23 18:37:51 +0100871static int check_fwfile_format(const uint8_t *data, uint32_t totlen)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400872{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500873 uint32_t bincmd, exit;
874 uint32_t blksize, offset, len;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400875 int ret;
876
877 ret = 1;
878 exit = len = 0;
879
880 do {
881 struct fwheader *fwh = (void *)data;
882
883 bincmd = le32_to_cpu(fwh->dnldcmd);
884 blksize = le32_to_cpu(fwh->datalength);
885 switch (bincmd) {
886 case FW_HAS_DATA_TO_RECV:
887 offset = sizeof(struct fwheader) + blksize;
888 data += offset;
889 len += offset;
890 if (len >= totlen)
891 exit = 1;
892 break;
893 case FW_HAS_LAST_BLOCK:
894 exit = 1;
895 ret = 0;
896 break;
897 default:
898 exit = 1;
899 break;
900 }
901 } while (!exit);
902
903 if (ret)
904 lbs_pr_err("firmware file format check FAIL\n");
905 else
906 lbs_deb_fw("firmware file format check PASS\n");
907
908 return ret;
909}
910
911
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700912/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700913* if_usb_prog_firmware - programs the firmware subject to cmd
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700914*
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700915* @cardp: the if_usb_card descriptor
916* @fwname: firmware or boot2 image file name
917* @cmd: either BOOT_CMD_FW_BY_USB, BOOT_CMD_UPDATE_FW,
918* or BOOT_CMD_UPDATE_BOOT2.
919* returns: 0 or error code
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700920*/
921static int if_usb_prog_firmware(struct if_usb_card *cardp,
922 const char *fwname, int cmd)
923{
924 struct lbs_private *priv = cardp->priv;
925 unsigned long flags, caps;
926 int ret;
927
928 caps = priv->fwcapinfo;
929 if (((cmd == BOOT_CMD_UPDATE_FW) && !(caps & FW_CAPINFO_FIRMWARE_UPGRADE)) ||
930 ((cmd == BOOT_CMD_UPDATE_BOOT2) && !(caps & FW_CAPINFO_BOOT2_UPGRADE)))
931 return -EOPNOTSUPP;
932
933 /* Ensure main thread is idle. */
934 spin_lock_irqsave(&priv->driver_lock, flags);
935 while (priv->cur_cmd != NULL || priv->dnld_sent != DNLD_RES_RECEIVED) {
936 spin_unlock_irqrestore(&priv->driver_lock, flags);
937 if (wait_event_interruptible(priv->waitq,
938 (priv->cur_cmd == NULL &&
939 priv->dnld_sent == DNLD_RES_RECEIVED))) {
940 return -ERESTARTSYS;
941 }
942 spin_lock_irqsave(&priv->driver_lock, flags);
943 }
944 priv->dnld_sent = DNLD_BOOTCMD_SENT;
945 spin_unlock_irqrestore(&priv->driver_lock, flags);
946
947 ret = __if_usb_prog_firmware(cardp, fwname, cmd);
948
949 spin_lock_irqsave(&priv->driver_lock, flags);
950 priv->dnld_sent = DNLD_RES_RECEIVED;
951 spin_unlock_irqrestore(&priv->driver_lock, flags);
952
953 wake_up_interruptible(&priv->waitq);
954
955 return ret;
956}
957
Dan Williams5cddea82010-08-07 21:13:57 -0500958/* table of firmware file names */
959static const struct {
960 u32 model;
961 const char *fwname;
962} fw_table[] = {
963 { MODEL_8388, "libertas/usb8388_v9.bin" },
964 { MODEL_8388, "libertas/usb8388_v5.bin" },
965 { MODEL_8388, "libertas/usb8388.bin" },
966 { MODEL_8388, "usb8388.bin" },
967 { MODEL_8682, "libertas/usb8682.bin" }
968};
969
970static int get_fw(struct if_usb_card *cardp, const char *fwname)
971{
972 int i;
973
974 /* Try user-specified firmware first */
975 if (fwname)
976 return request_firmware(&cardp->fw, fwname, &cardp->udev->dev);
977
978 /* Otherwise search for firmware to use */
979 for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
980 if (fw_table[i].model != cardp->model)
981 continue;
982 if (request_firmware(&cardp->fw, fw_table[i].fwname,
983 &cardp->udev->dev) == 0)
984 return 0;
985 }
986
987 return -ENOENT;
988}
989
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700990static int __if_usb_prog_firmware(struct if_usb_card *cardp,
991 const char *fwname, int cmd)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400992{
Dan Williams954ee162007-08-20 11:43:25 -0400993 int i = 0;
994 static int reset_count = 10;
995 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400996
Dan Williams954ee162007-08-20 11:43:25 -0400997 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400998
Dan Williams5cddea82010-08-07 21:13:57 -0500999 ret = get_fw(cardp, fwname);
1000 if (ret) {
1001 lbs_pr_err("failed to find firmware (%d)\n", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001002 goto done;
1003 }
1004
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -07001005 if (check_fwfile_format(cardp->fw->data, cardp->fw->size)) {
1006 ret = -EINVAL;
Dan Williams954ee162007-08-20 11:43:25 -04001007 goto release_fw;
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -07001008 }
1009
1010 /* Cancel any pending usb business */
1011 usb_kill_urb(cardp->rx_urb);
1012 usb_kill_urb(cardp->tx_urb);
1013
1014 cardp->fwlastblksent = 0;
1015 cardp->fwdnldover = 0;
1016 cardp->totalbytes = 0;
1017 cardp->fwfinalblk = 0;
1018 cardp->bootcmdresp = 0;
Dan Williams954ee162007-08-20 11:43:25 -04001019
1020restart:
1021 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
1022 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -07001023 ret = -EIO;
Dan Williams954ee162007-08-20 11:43:25 -04001024 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001025 }
1026
Dan Williams954ee162007-08-20 11:43:25 -04001027 cardp->bootcmdresp = 0;
1028 do {
1029 int j = 0;
1030 i++;
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -07001031 if_usb_issue_boot_command(cardp, cmd);
Dan Williams954ee162007-08-20 11:43:25 -04001032 /* wait for command response */
1033 do {
1034 j++;
1035 msleep_interruptible(100);
1036 } while (cardp->bootcmdresp == 0 && j < 10);
1037 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001038
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -07001039 if (cardp->bootcmdresp == BOOT_CMD_RESP_NOT_SUPPORTED) {
1040 /* Return to normal operation */
1041 ret = -EOPNOTSUPP;
1042 usb_kill_urb(cardp->rx_urb);
1043 usb_kill_urb(cardp->tx_urb);
1044 if (if_usb_submit_rx_urb(cardp) < 0)
1045 ret = -EIO;
1046 goto release_fw;
1047 } else if (cardp->bootcmdresp <= 0) {
Dan Williams954ee162007-08-20 11:43:25 -04001048 if (--reset_count >= 0) {
1049 if_usb_reset_device(cardp);
1050 goto restart;
1051 }
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -07001052 ret = -EIO;
1053 goto release_fw;
Dan Williams954ee162007-08-20 11:43:25 -04001054 }
1055
1056 i = 0;
1057
1058 cardp->totalbytes = 0;
1059 cardp->fwlastblksent = 0;
1060 cardp->CRC_OK = 1;
1061 cardp->fwdnldover = 0;
1062 cardp->fwseqnum = -1;
1063 cardp->totalbytes = 0;
1064 cardp->fwfinalblk = 0;
1065
David Woodhouse4f82f5c2007-12-11 00:07:58 -05001066 /* Send the first firmware packet... */
1067 if_usb_send_fw_pkt(cardp);
Dan Williams954ee162007-08-20 11:43:25 -04001068
David Woodhouse4f82f5c2007-12-11 00:07:58 -05001069 /* ... and wait for the process to complete */
1070 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
David Woodhouse7e226272007-12-14 22:53:41 -05001071
David Woodhouse4f82f5c2007-12-11 00:07:58 -05001072 del_timer_sync(&cardp->fw_timeout);
David Woodhousec9cd6f92007-12-11 13:15:25 -05001073 usb_kill_urb(cardp->rx_urb);
Dan Williams954ee162007-08-20 11:43:25 -04001074
1075 if (!cardp->fwdnldover) {
1076 lbs_pr_info("failed to load fw, resetting device!\n");
1077 if (--reset_count >= 0) {
1078 if_usb_reset_device(cardp);
1079 goto restart;
1080 }
1081
1082 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -07001083 ret = -EIO;
Dan Williams954ee162007-08-20 11:43:25 -04001084 goto release_fw;
1085 }
1086
David Woodhouseeae86bf2007-12-14 00:47:05 -05001087 release_fw:
Dan Williams954ee162007-08-20 11:43:25 -04001088 release_firmware(cardp->fw);
1089 cardp->fw = NULL;
1090
David Woodhouseeae86bf2007-12-14 00:47:05 -05001091 done:
Dan Williams954ee162007-08-20 11:43:25 -04001092 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -04001093 return ret;
1094}
1095
1096
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001097#ifdef CONFIG_PM
1098static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
1099{
David Woodhouseeae86bf2007-12-14 00:47:05 -05001100 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +01001101 struct lbs_private *priv = cardp->priv;
David Woodhoused1f7a5b2007-12-12 17:40:56 -05001102 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103
Holger Schurig9012b282007-05-25 11:27:16 -04001104 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001105
David Woodhouseaa21c002007-12-08 20:04:36 +00001106 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001107 return -1;
1108
David Woodhoused1f7a5b2007-12-12 17:40:56 -05001109 ret = lbs_suspend(priv);
1110 if (ret)
1111 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001112
1113 /* Unlink tx & rx urb */
1114 usb_kill_urb(cardp->tx_urb);
1115 usb_kill_urb(cardp->rx_urb);
1116
David Woodhoused1f7a5b2007-12-12 17:40:56 -05001117 out:
Holger Schurig9012b282007-05-25 11:27:16 -04001118 lbs_deb_leave(LBS_DEB_USB);
David Woodhoused1f7a5b2007-12-12 17:40:56 -05001119 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001120}
1121
1122static int if_usb_resume(struct usb_interface *intf)
1123{
David Woodhouseeae86bf2007-12-14 00:47:05 -05001124 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +01001125 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001126
Holger Schurig9012b282007-05-25 11:27:16 -04001127 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001128
David Woodhouse6bc822b2007-12-11 12:53:43 -05001129 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001130
David Woodhoused1f7a5b2007-12-12 17:40:56 -05001131 lbs_resume(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001132
Holger Schurig9012b282007-05-25 11:27:16 -04001133 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001134 return 0;
1135}
1136#else
1137#define if_usb_suspend NULL
1138#define if_usb_resume NULL
1139#endif
1140
1141static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -05001142 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001143 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001144 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001145 .id_table = if_usb_table,
1146 .suspend = if_usb_suspend,
1147 .resume = if_usb_resume,
andrey@cozybit.com7b58ccf2008-07-01 11:43:53 -07001148 .reset_resume = if_usb_resume,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001149};
1150
Andres Salomon4fb910f2007-11-20 17:43:45 -05001151static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001152{
Holger Schurig084708b2007-05-25 12:37:58 -04001153 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001154
Holger Schurig084708b2007-05-25 12:37:58 -04001155 lbs_deb_enter(LBS_DEB_MAIN);
1156
Holger Schurig084708b2007-05-25 12:37:58 -04001157 ret = usb_register(&if_usb_driver);
1158
1159 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1160 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001161}
1162
Andres Salomon4fb910f2007-11-20 17:43:45 -05001163static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001164{
Holger Schurig084708b2007-05-25 12:37:58 -04001165 lbs_deb_enter(LBS_DEB_MAIN);
1166
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001167 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001168
1169 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001170}
Holger Schurig084708b2007-05-25 12:37:58 -04001171
1172module_init(if_usb_init_module);
1173module_exit(if_usb_exit_module);
1174
1175MODULE_DESCRIPTION("8388 USB WLAN Driver");
David Woodhouseeae86bf2007-12-14 00:47:05 -05001176MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
Holger Schurig084708b2007-05-25 12:37:58 -04001177MODULE_LICENSE("GPL");