blob: b5013ce31b9abd7732005b918f5ad53dbcab6a04 [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/**
2 * This file contains functions used in USB interface module.
3 */
4#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>
8#include <linux/usb.h>
9
David Woodhouseb77ec4c2008-05-20 16:48:52 +010010#ifdef CONFIG_OLPC
11#include <asm/olpc.h>
12#endif
13
Holger Schurigec3eef22007-05-25 12:49:10 -040014#define DRV_NAME "usb8xxx"
15
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020016#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020017#include "decl.h"
18#include "defs.h"
19#include "dev.h"
Dan Williams14e865b2007-12-10 15:11:23 -050020#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020021#include "if_usb.h"
22
David Woodhouseeae86bf2007-12-14 00:47:05 -050023#define INSANEDEBUG 0
24#define lbs_deb_usb2(...) do { if (INSANEDEBUG) lbs_deb_usbd(__VA_ARGS__); } while (0)
25
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020026#define MESSAGE_HEADER_LEN 4
27
Andres Salomon82209ad2007-11-20 17:43:32 -050028static char *lbs_fw_name = "usb8388.bin";
Holger Schurig10078322007-11-15 18:05:47 -050029module_param_named(fw_name, lbs_fw_name, charp, 0644);
Holger Schurig084708b2007-05-25 12:37:58 -040030
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020031static struct usb_device_id if_usb_table[] = {
32 /* Enter the device signature inside */
Holger Schurig66fcc552007-05-25 00:11:58 -040033 { USB_DEVICE(0x1286, 0x2001) },
34 { USB_DEVICE(0x05a3, 0x8388) },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020035 {} /* Terminating entry */
36};
37
38MODULE_DEVICE_TABLE(usb, if_usb_table);
39
40static void if_usb_receive(struct urb *urb);
41static void if_usb_receive_fwload(struct urb *urb);
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -070042static int __if_usb_prog_firmware(struct if_usb_card *cardp,
43 const char *fwname, int cmd);
44static int if_usb_prog_firmware(struct if_usb_card *cardp,
45 const char *fwname, int cmd);
David Woodhouseeae86bf2007-12-14 00:47:05 -050046static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
47 uint8_t *payload, uint16_t nb);
David Woodhouseeae86bf2007-12-14 00:47:05 -050048static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
49 uint16_t nb);
50static void if_usb_free(struct if_usb_card *cardp);
51static int if_usb_submit_rx_urb(struct if_usb_card *cardp);
52static int if_usb_reset_device(struct if_usb_card *cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020053
54/**
55 * @brief call back function to handle the status of the URB
56 * @param urb pointer to urb structure
57 * @return N/A
58 */
59static void if_usb_write_bulk_callback(struct urb *urb)
60{
David Woodhouseeae86bf2007-12-14 00:47:05 -050061 struct if_usb_card *cardp = (struct if_usb_card *) urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020062
63 /* handle the transmission complete validations */
64
Dan Williams954ee162007-08-20 11:43:25 -040065 if (urb->status == 0) {
Holger Schurig69f90322007-11-23 15:43:44 +010066 struct lbs_private *priv = cardp->priv;
Dan Williams954ee162007-08-20 11:43:25 -040067
David Woodhouseeae86bf2007-12-14 00:47:05 -050068 lbs_deb_usb2(&urb->dev->dev, "URB status is successful\n");
69 lbs_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
70 urb->actual_length);
Dan Williams954ee162007-08-20 11:43:25 -040071
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -070072 /* Boot commands such as UPDATE_FW and UPDATE_BOOT2 are not
73 * passed up to the lbs level.
Dan Williams954ee162007-08-20 11:43:25 -040074 */
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -070075 if (priv && priv->dnld_sent != DNLD_BOOTCMD_SENT)
David Woodhousee775ed72007-12-06 14:36:11 +000076 lbs_host_to_card_done(priv);
Dan Williams954ee162007-08-20 11:43:25 -040077 } else {
78 /* print the failure status number for debug */
79 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020080 }
81
82 return;
83}
84
85/**
86 * @brief free tx/rx urb, skb and rx buffer
David Woodhouseeae86bf2007-12-14 00:47:05 -050087 * @param cardp pointer if_usb_card
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020088 * @return N/A
89 */
David Woodhouseeae86bf2007-12-14 00:47:05 -050090static void if_usb_free(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020091{
Holger Schurig9012b282007-05-25 11:27:16 -040092 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020093
94 /* Unlink tx & rx urb */
95 usb_kill_urb(cardp->tx_urb);
96 usb_kill_urb(cardp->rx_urb);
97
98 usb_free_urb(cardp->tx_urb);
99 cardp->tx_urb = NULL;
100
101 usb_free_urb(cardp->rx_urb);
102 cardp->rx_urb = NULL;
103
David Woodhouseeae86bf2007-12-14 00:47:05 -0500104 kfree(cardp->ep_out_buf);
105 cardp->ep_out_buf = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200106
Holger Schurig9012b282007-05-25 11:27:16 -0400107 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200108}
109
David Woodhouse46949612007-12-17 14:42:33 -0500110static void if_usb_setup_firmware(struct lbs_private *priv)
David Woodhouse04c80f12007-12-06 12:51:00 +0000111{
Holger Schurig43659292008-01-16 15:52:58 +0100112 struct if_usb_card *cardp = priv->card;
David Woodhouse04c80f12007-12-06 12:51:00 +0000113 struct cmd_ds_set_boot2_ver b2_cmd;
David Woodhouse46949612007-12-17 14:42:33 -0500114 struct cmd_ds_802_11_fw_wake_method wake_method;
David Woodhouse04c80f12007-12-06 12:51:00 +0000115
David Woodhouse9fae8992007-12-15 03:46:44 -0500116 b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
David Woodhouse04c80f12007-12-06 12:51:00 +0000117 b2_cmd.action = 0;
Holger Schurig43659292008-01-16 15:52:58 +0100118 b2_cmd.version = cardp->boot2_version;
David Woodhouse04c80f12007-12-06 12:51:00 +0000119
David Woodhouseb23b2062007-12-15 01:22:09 -0500120 if (lbs_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
David Woodhouse04c80f12007-12-06 12:51:00 +0000121 lbs_deb_usb("Setting boot2 version failed\n");
David Woodhouse46949612007-12-17 14:42:33 -0500122
123 priv->wol_gpio = 2; /* Wake via GPIO2... */
124 priv->wol_gap = 20; /* ... after 20ms */
125 lbs_host_sleep_cfg(priv, EHS_WAKE_ON_UNICAST_DATA);
126
127 wake_method.hdr.size = cpu_to_le16(sizeof(wake_method));
128 wake_method.action = cpu_to_le16(CMD_ACT_GET);
129 if (lbs_cmd_with_response(priv, CMD_802_11_FW_WAKE_METHOD, &wake_method)) {
130 lbs_pr_info("Firmware does not seem to support PS mode\n");
131 } else {
132 if (le16_to_cpu(wake_method.method) == CMD_WAKE_METHOD_COMMAND_INT) {
133 lbs_deb_usb("Firmware seems to support PS with wake-via-command\n");
134 priv->ps_supported = 1;
135 } else {
136 /* The versions which boot up this way don't seem to
137 work even if we set it to the command interrupt */
138 lbs_pr_info("Firmware doesn't wake via command interrupt; disabling PS mode\n");
139 }
140 }
David Woodhouse04c80f12007-12-06 12:51:00 +0000141}
142
David Woodhouse2fd6cfe2007-12-11 17:44:10 -0500143static void if_usb_fw_timeo(unsigned long priv)
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500144{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500145 struct if_usb_card *cardp = (void *)priv;
David Woodhouse04c80f12007-12-06 12:51:00 +0000146
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500147 if (cardp->fwdnldover) {
148 lbs_deb_usb("Download complete, no event. Assuming success\n");
149 } else {
150 lbs_pr_err("Download timed out\n");
151 cardp->surprise_removed = 1;
152 }
153 wake_up(&cardp->fw_wq);
154}
David Woodhouseeae86bf2007-12-14 00:47:05 -0500155
David Woodhouseb77ec4c2008-05-20 16:48:52 +0100156#ifdef CONFIG_OLPC
157static void if_usb_reset_olpc_card(struct lbs_private *priv)
158{
159 printk(KERN_CRIT "Resetting OLPC wireless via EC...\n");
160 olpc_ec_cmd(0x25, NULL, 0, NULL, 0);
161}
162#endif
163
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200164/**
165 * @brief sets the configuration values
166 * @param ifnum interface number
167 * @param id pointer to usb_device_id
168 * @return 0 on success, error code on failure
169 */
170static int if_usb_probe(struct usb_interface *intf,
171 const struct usb_device_id *id)
172{
173 struct usb_device *udev;
174 struct usb_host_interface *iface_desc;
175 struct usb_endpoint_descriptor *endpoint;
Holger Schurig69f90322007-11-23 15:43:44 +0100176 struct lbs_private *priv;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500177 struct if_usb_card *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200178 int i;
179
180 udev = interface_to_usbdev(intf);
181
David Woodhouseeae86bf2007-12-14 00:47:05 -0500182 cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400183 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200184 lbs_pr_err("Out of memory allocating private data.\n");
185 goto error;
186 }
187
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500188 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
189 init_waitqueue_head(&cardp->fw_wq);
David Woodhouse7e226272007-12-14 22:53:41 -0500190
Holger Schurig435a1ac2007-05-25 12:41:52 -0400191 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200192 iface_desc = intf->cur_altsetting;
193
Holger Schurig9012b282007-05-25 11:27:16 -0400194 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
David Woodhouseeae86bf2007-12-14 00:47:05 -0500195 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400196 le16_to_cpu(udev->descriptor.bcdUSB),
197 udev->descriptor.bDeviceClass,
198 udev->descriptor.bDeviceSubClass,
199 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200200
201 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
202 endpoint = &iface_desc->endpoint[i].desc;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500203 if (usb_endpoint_is_bulk_in(endpoint)) {
204 cardp->ep_in_size = le16_to_cpu(endpoint->wMaxPacketSize);
205 cardp->ep_in = usb_endpoint_num(endpoint);
206
207 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n", cardp->ep_in);
208 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n", cardp->ep_in_size);
209
210 } else if (usb_endpoint_is_bulk_out(endpoint)) {
211 cardp->ep_out_size = le16_to_cpu(endpoint->wMaxPacketSize);
212 cardp->ep_out = usb_endpoint_num(endpoint);
213
214 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n", cardp->ep_out);
215 lbs_deb_usbd(&udev->dev, "Bulk out size is %d\n", cardp->ep_out_size);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200216 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500217 }
218 if (!cardp->ep_out_size || !cardp->ep_in_size) {
219 lbs_deb_usbd(&udev->dev, "Endpoints not found\n");
220 goto dealloc;
221 }
222 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
223 lbs_deb_usbd(&udev->dev, "Rx URB allocation failed\n");
224 goto dealloc;
225 }
226 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
227 lbs_deb_usbd(&udev->dev, "Tx URB allocation failed\n");
228 goto dealloc;
229 }
230 cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE, GFP_KERNEL);
231 if (!cardp->ep_out_buf) {
232 lbs_deb_usbd(&udev->dev, "Could not allocate buffer\n");
233 goto dealloc;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200234 }
235
Dan Williams954ee162007-08-20 11:43:25 -0400236 /* Upload firmware */
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700237 if (__if_usb_prog_firmware(cardp, lbs_fw_name, BOOT_CMD_FW_BY_USB)) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500238 lbs_deb_usbd(&udev->dev, "FW upload failed\n");
Dan Williams954ee162007-08-20 11:43:25 -0400239 goto err_prog_firmware;
240 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400241
Holger Schurig10078322007-11-15 18:05:47 -0500242 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400243 goto err_prog_firmware;
244
245 cardp->priv = priv;
David Woodhousec9cd6f92007-12-11 13:15:25 -0500246 cardp->priv->fw_ready = 1;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400247
Holger Schurig208fdd22007-05-25 12:17:06 -0400248 priv->hw_host_to_card = if_usb_host_to_card;
David Woodhouseb77ec4c2008-05-20 16:48:52 +0100249#ifdef CONFIG_OLPC
250 if (machine_is_olpc())
251 priv->reset_card = if_usb_reset_olpc_card;
252#endif
253
Holger Schurig43659292008-01-16 15:52:58 +0100254 cardp->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400255
Dan Williams954ee162007-08-20 11:43:25 -0400256 if_usb_submit_rx_urb(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400257
Holger Schurig10078322007-11-15 18:05:47 -0500258 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400259 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400260
David Woodhouse46949612007-12-17 14:42:33 -0500261 if_usb_setup_firmware(priv);
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500262
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200263 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400264 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200265
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200266 return 0;
267
Dan Williams954ee162007-08-20 11:43:25 -0400268err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500269 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400270err_prog_firmware:
271 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200272dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400273 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200274
275error:
276 return -ENOMEM;
277}
278
279/**
280 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400281 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200282 * @return N/A
283 */
284static void if_usb_disconnect(struct usb_interface *intf)
285{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500286 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100287 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200288
Dan Williams954ee162007-08-20 11:43:25 -0400289 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200290
Dan Williams954ee162007-08-20 11:43:25 -0400291 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200292
Dan Williams954ee162007-08-20 11:43:25 -0400293 if (priv) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000294 priv->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500295 lbs_stop_card(priv);
Holger Schurig10078322007-11-15 18:05:47 -0500296 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400297 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200298
299 /* Unlink and free urb */
300 if_usb_free(cardp);
301
302 usb_set_intfdata(intf, NULL);
303 usb_put_dev(interface_to_usbdev(intf));
304
Dan Williams954ee162007-08-20 11:43:25 -0400305 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200306}
307
308/**
309 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100310 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200311 * @return 0
312 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500313static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200314{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500315 struct fwdata *fwdata = cardp->ep_out_buf;
David Woodhouse6dfff892008-05-23 18:37:51 +0100316 const uint8_t *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200317
David Woodhouseeae86bf2007-12-14 00:47:05 -0500318 /* If we got a CRC failure on the last block, back
319 up and retry it */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200320 if (!cardp->CRC_OK) {
321 cardp->totalbytes = cardp->fwlastblksent;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500322 cardp->fwseqnum--;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200323 }
324
David Woodhouseeae86bf2007-12-14 00:47:05 -0500325 lbs_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
326 cardp->totalbytes);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200327
David Woodhouseeae86bf2007-12-14 00:47:05 -0500328 /* struct fwdata (which we sent to the card) has an
329 extra __le32 field in between the header and the data,
330 which is not in the struct fwheader in the actual
331 firmware binary. Insert the seqnum in the middle... */
332 memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200333 sizeof(struct fwheader));
334
335 cardp->fwlastblksent = cardp->totalbytes;
336 cardp->totalbytes += sizeof(struct fwheader);
337
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouseeae86bf2007-12-14 00:47:05 -0500339 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200340
David Woodhouseeae86bf2007-12-14 00:47:05 -0500341 lbs_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
342 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200343
David Woodhouseeae86bf2007-12-14 00:47:05 -0500344 fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
345 cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200346
David Woodhouseeae86bf2007-12-14 00:47:05 -0500347 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
348 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200349
David Woodhouseeae86bf2007-12-14 00:47:05 -0500350 if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
351 lbs_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
352 lbs_deb_usb2(&cardp->udev->dev, "seqnum = %d totalbytes = %d\n",
353 cardp->fwseqnum, cardp->totalbytes);
354 } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
355 lbs_deb_usb2(&cardp->udev->dev, "Host has finished FW downloading\n");
356 lbs_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200357
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200358 cardp->fwfinalblk = 1;
359 }
360
David Woodhouseeae86bf2007-12-14 00:47:05 -0500361 lbs_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
362 cardp->totalbytes);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200363
364 return 0;
365}
366
David Woodhouseeae86bf2007-12-14 00:47:05 -0500367static int if_usb_reset_device(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200368{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500369 struct cmd_ds_command *cmd = cardp->ep_out_buf + 4;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200371
Holger Schurig3874d0f2007-05-25 12:01:42 -0400372 lbs_deb_enter(LBS_DEB_USB);
373
David Woodhouseeae86bf2007-12-14 00:47:05 -0500374 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000375
376 cmd->command = cpu_to_le16(CMD_802_11_RESET);
377 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
378 cmd->result = cpu_to_le16(0);
379 cmd->seqnum = cpu_to_le16(0x5a5a);
380 cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500381 usb_tx_block(cardp, cardp->ep_out_buf, 4 + S_DS_GEN + sizeof(struct cmd_ds_802_11_reset));
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000382
David Woodhousec8ba39d2007-12-10 18:53:34 -0500383 msleep(100);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200384 ret = usb_reset_device(cardp->udev);
David Woodhousec8ba39d2007-12-10 18:53:34 -0500385 msleep(100);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400386
David Woodhouseb77ec4c2008-05-20 16:48:52 +0100387#ifdef CONFIG_OLPC
388 if (ret && machine_is_olpc())
389 if_usb_reset_olpc_card(NULL);
390#endif
391
Holger Schurig3874d0f2007-05-25 12:01:42 -0400392 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
393
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200394 return ret;
395}
396
397/**
398 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100399 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200400 * @param payload pointer to payload data
401 * @param nb data length
402 * @return 0 or -1
403 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500404static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, uint16_t nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200405{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200406 int ret = -1;
407
408 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400409 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400410 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200411 goto tx_ret;
412 }
413
414 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
415 usb_sndbulkpipe(cardp->udev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500416 cardp->ep_out),
Dan Williams954ee162007-08-20 11:43:25 -0400417 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200418
419 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
420
421 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
David Woodhouse0856e682007-12-07 15:12:26 +0000422 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200423 ret = -1;
424 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500425 lbs_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200426 ret = 0;
427 }
428
429tx_ret:
430 return ret;
431}
432
David Woodhouseeae86bf2007-12-14 00:47:05 -0500433static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
Dan Williams954ee162007-08-20 11:43:25 -0400434 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200435{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200436 struct sk_buff *skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200437 int ret = -1;
438
439 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
440 lbs_pr_err("No free skb\n");
441 goto rx_ret;
442 }
443
David Woodhouseeae86bf2007-12-14 00:47:05 -0500444 cardp->rx_skb = skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200445
446 /* Fill the receive configuration URB and initialise the Rx call back */
447 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500448 usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
Dan Williams4269e2a2007-05-10 23:10:18 -0400449 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200450 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500451 cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200452
453 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
454
David Woodhouseeae86bf2007-12-14 00:47:05 -0500455 lbs_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
David Woodhouse0856e682007-12-07 15:12:26 +0000457 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000458 kfree_skb(skb);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500459 cardp->rx_skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200460 ret = -1;
461 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500462 lbs_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200463 ret = 0;
464 }
465
466rx_ret:
467 return ret;
468}
469
David Woodhouseeae86bf2007-12-14 00:47:05 -0500470static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200471{
Dan Williams954ee162007-08-20 11:43:25 -0400472 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200473}
474
David Woodhouseeae86bf2007-12-14 00:47:05 -0500475static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476{
Dan Williams954ee162007-08-20 11:43:25 -0400477 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200478}
479
480static void if_usb_receive_fwload(struct urb *urb)
481{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500482 struct if_usb_card *cardp = urb->context;
483 struct sk_buff *skb = cardp->rx_skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200484 struct fwsyncheader *syncfwheader;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500485 struct bootcmdresp bootcmdresp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486
487 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400488 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500489 "URB status is failed during fw load\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200490 kfree_skb(skb);
491 return;
492 }
493
David Woodhousec9cd6f92007-12-11 13:15:25 -0500494 if (cardp->fwdnldover) {
495 __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
496
497 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
498 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
499 lbs_pr_info("Firmware ready event received\n");
500 wake_up(&cardp->fw_wq);
501 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500502 lbs_deb_usb("Waiting for confirmation; got %x %x\n",
503 le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
David Woodhousec9cd6f92007-12-11 13:15:25 -0500504 if_usb_submit_rx_urb_fwload(cardp);
505 }
506 kfree_skb(skb);
507 return;
508 }
David Woodhousec8ba39d2007-12-10 18:53:34 -0500509 if (cardp->bootcmdresp <= 0) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
511 sizeof(bootcmdresp));
David Woodhouseeae86bf2007-12-14 00:47:05 -0500512
David Woodhouse981f1872007-05-25 23:36:54 -0400513 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200514 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400515 if_usb_submit_rx_urb_fwload(cardp);
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700516 cardp->bootcmdresp = BOOT_CMD_RESP_OK;
Holger Schurig9012b282007-05-25 11:27:16 -0400517 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500518 "Received valid boot command response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200519 return;
520 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500521 if (bootcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
522 if (bootcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
523 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
524 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
David Woodhousec9cd6f92007-12-11 13:15:25 -0500525 if (!cardp->bootcmdresp)
526 lbs_pr_info("Firmware already seems alive; resetting\n");
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000527 cardp->bootcmdresp = -1;
528 } else {
529 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
David Woodhouseeae86bf2007-12-14 00:47:05 -0500530 le32_to_cpu(bootcmdresp.magic));
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000531 }
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700532 } else if ((bootcmdresp.cmd != BOOT_CMD_FW_BY_USB) &&
533 (bootcmdresp.cmd != BOOT_CMD_UPDATE_FW) &&
534 (bootcmdresp.cmd != BOOT_CMD_UPDATE_BOOT2)) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500535 lbs_pr_info("boot cmd response cmd_tag error (%d)\n",
536 bootcmdresp.cmd);
537 } else if (bootcmdresp.result != BOOT_CMD_RESP_OK) {
538 lbs_pr_info("boot cmd response result error (%d)\n",
539 bootcmdresp.result);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200540 } else {
541 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400542 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500543 "Received valid boot command response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200544 }
545 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400546 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200547 return;
548 }
549
550 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
551 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400552 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200553 kfree_skb(skb);
554 return;
555 }
556
557 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500558 sizeof(struct fwsyncheader));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559
560 if (!syncfwheader->cmd) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500561 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk with correct CRC\n");
562 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk seqnum = %d\n",
563 le32_to_cpu(syncfwheader->seqnum));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200564 cardp->CRC_OK = 1;
565 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500566 lbs_deb_usbd(&cardp->udev->dev, "FW received Blk with CRC error\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200567 cardp->CRC_OK = 0;
568 }
569
570 kfree_skb(skb);
571
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700572 /* Give device 5s to either write firmware to its RAM or eeprom */
573 mod_timer(&cardp->fw_timeout, jiffies + (HZ*5));
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500574
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200575 if (cardp->fwfinalblk) {
576 cardp->fwdnldover = 1;
577 goto exit;
578 }
579
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500580 if_usb_send_fw_pkt(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200581
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500582 exit:
David Woodhousec9cd6f92007-12-11 13:15:25 -0500583 if_usb_submit_rx_urb_fwload(cardp);
584
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200585 kfree(syncfwheader);
586
587 return;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200588}
589
590#define MRVDRV_MIN_PKT_LEN 30
591
592static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500593 struct if_usb_card *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100594 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200595{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500596 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
597 || recvlength < MRVDRV_MIN_PKT_LEN) {
598 lbs_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200599 kfree_skb(skb);
600 return;
601 }
602
603 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
604 skb_put(skb, recvlength);
605 skb_pull(skb, MESSAGE_HEADER_LEN);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500606
Holger Schurig10078322007-11-15 18:05:47 -0500607 lbs_process_rxed_packet(priv, skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200608}
609
David Woodhouseeae86bf2007-12-14 00:47:05 -0500610static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200611 struct sk_buff *skb,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500612 struct if_usb_card *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100613 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200614{
Holger Schurig7919b892008-04-01 14:50:43 +0200615 u8 i;
616
Dan Williamsddac4522007-12-11 13:49:39 -0500617 if (recvlength > LBS_CMD_BUFFER_SIZE) {
Holger Schurig9012b282007-05-25 11:27:16 -0400618 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500619 "The receive buffer is too large\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200620 kfree_skb(skb);
621 return;
622 }
623
624 if (!in_interrupt())
625 BUG();
626
David Woodhouseaa21c002007-12-08 20:04:36 +0000627 spin_lock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200628
Holger Schurig7919b892008-04-01 14:50:43 +0200629 i = (priv->resp_idx == 0) ? 1 : 0;
630 BUG_ON(priv->resp_len[i]);
631 priv->resp_len[i] = (recvlength - MESSAGE_HEADER_LEN);
632 memcpy(priv->resp_buf[i], recvbuff + MESSAGE_HEADER_LEN,
633 priv->resp_len[i]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634 kfree_skb(skb);
Holger Schurig7919b892008-04-01 14:50:43 +0200635 lbs_notify_command_response(priv, i);
636
David Woodhouseaa21c002007-12-08 20:04:36 +0000637 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200638
Holger Schurig9012b282007-05-25 11:27:16 -0400639 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200640 "Wake up main thread to handle cmd response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200641}
642
643/**
644 * @brief This function reads of the packet into the upload buff,
645 * wake up the main thread and initialise the Rx callack.
646 *
647 * @param urb pointer to struct urb
648 * @return N/A
649 */
650static void if_usb_receive(struct urb *urb)
651{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500652 struct if_usb_card *cardp = urb->context;
653 struct sk_buff *skb = cardp->rx_skb;
Holger Schurig69f90322007-11-23 15:43:44 +0100654 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200655 int recvlength = urb->actual_length;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500656 uint8_t *recvbuff = NULL;
657 uint32_t recvtype = 0;
658 __le32 *pkt = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
Holger Schurig7919b892008-04-01 14:50:43 +0200659 uint32_t event;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200660
Holger Schurig9012b282007-05-25 11:27:16 -0400661 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200662
663 if (recvlength) {
664 if (urb->status) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500665 lbs_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
666 urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200667 kfree_skb(skb);
668 goto setup_for_next;
669 }
670
671 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500672 recvtype = le32_to_cpu(pkt[0]);
Holger Schurig9012b282007-05-25 11:27:16 -0400673 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400674 "Recv length = 0x%x, Recv type = 0x%X\n",
675 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000676 } else if (urb->status) {
677 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200678 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000679 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200680
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200681 switch (recvtype) {
682 case CMD_TYPE_DATA:
683 process_cmdtypedata(recvlength, skb, cardp, priv);
684 break;
685
686 case CMD_TYPE_REQUEST:
687 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
688 break;
689
690 case CMD_TYPE_INDICATION:
Holger Schurig7919b892008-04-01 14:50:43 +0200691 /* Event handling */
692 event = le32_to_cpu(pkt[1]);
693 lbs_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n", event);
694 kfree_skb(skb);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500695
696 /* Icky undocumented magic special case */
Holger Schurig7919b892008-04-01 14:50:43 +0200697 if (event & 0xffff0000) {
698 u32 trycount = (event & 0xffff0000) >> 16;
699
700 lbs_send_tx_feedback(priv, trycount);
701 } else
702 lbs_queue_event(priv, event & 0xFF);
703 break;
704
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200705 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400706 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
David Woodhouseeae86bf2007-12-14 00:47:05 -0500707 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200708 kfree_skb(skb);
709 break;
710 }
711
712setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400713 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200714rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400715 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716}
717
718/**
719 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100720 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200721 * @param type type of data
722 * @param buf pointer to data buffer
723 * @param len number of bytes
724 * @return 0 or -1
725 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500726static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
727 uint8_t *payload, uint16_t nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200728{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500729 struct if_usb_card *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200730
Holger Schurig9012b282007-05-25 11:27:16 -0400731 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
732 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200733
734 if (type == MVMS_CMD) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500735 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400736 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200737 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500738 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400739 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200740 }
741
David Woodhouseeae86bf2007-12-14 00:47:05 -0500742 memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200743
David Woodhouseeae86bf2007-12-14 00:47:05 -0500744 return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200745}
746
Dan Williams9e22cb62007-08-02 11:14:49 -0400747/**
748 * @brief This function issues Boot command to the Boot2 code
749 * @param ivalue 1:Boot from FW by USB-Download
750 * 2:Boot from FW in EEPROM
751 * @return 0
752 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500753static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400754{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500755 struct bootcmd *bootcmd = cardp->ep_out_buf;
Dan Williams9e22cb62007-08-02 11:14:49 -0400756
757 /* Prepare command */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500758 bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
759 bootcmd->cmd = ivalue;
760 memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
Dan Williams9e22cb62007-08-02 11:14:49 -0400761
762 /* Issue command */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500763 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd));
Dan Williams9e22cb62007-08-02 11:14:49 -0400764
765 return 0;
766}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200767
768
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400769/**
770 * @brief This function checks the validity of Boot2/FW image.
771 *
772 * @param data pointer to image
773 * len image length
774 * @return 0 or -1
775 */
David Woodhouse6dfff892008-05-23 18:37:51 +0100776static int check_fwfile_format(const uint8_t *data, uint32_t totlen)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400777{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500778 uint32_t bincmd, exit;
779 uint32_t blksize, offset, len;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400780 int ret;
781
782 ret = 1;
783 exit = len = 0;
784
785 do {
786 struct fwheader *fwh = (void *)data;
787
788 bincmd = le32_to_cpu(fwh->dnldcmd);
789 blksize = le32_to_cpu(fwh->datalength);
790 switch (bincmd) {
791 case FW_HAS_DATA_TO_RECV:
792 offset = sizeof(struct fwheader) + blksize;
793 data += offset;
794 len += offset;
795 if (len >= totlen)
796 exit = 1;
797 break;
798 case FW_HAS_LAST_BLOCK:
799 exit = 1;
800 ret = 0;
801 break;
802 default:
803 exit = 1;
804 break;
805 }
806 } while (!exit);
807
808 if (ret)
809 lbs_pr_err("firmware file format check FAIL\n");
810 else
811 lbs_deb_fw("firmware file format check PASS\n");
812
813 return ret;
814}
815
816
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700817/**
818* @brief This function programs the firmware subject to cmd
819*
820* @param cardp the if_usb_card descriptor
821* fwname firmware or boot2 image file name
822* cmd either BOOT_CMD_FW_BY_USB, BOOT_CMD_UPDATE_FW,
823* or BOOT_CMD_UPDATE_BOOT2.
824* @return 0 or error code
825*/
826static int if_usb_prog_firmware(struct if_usb_card *cardp,
827 const char *fwname, int cmd)
828{
829 struct lbs_private *priv = cardp->priv;
830 unsigned long flags, caps;
831 int ret;
832
833 caps = priv->fwcapinfo;
834 if (((cmd == BOOT_CMD_UPDATE_FW) && !(caps & FW_CAPINFO_FIRMWARE_UPGRADE)) ||
835 ((cmd == BOOT_CMD_UPDATE_BOOT2) && !(caps & FW_CAPINFO_BOOT2_UPGRADE)))
836 return -EOPNOTSUPP;
837
838 /* Ensure main thread is idle. */
839 spin_lock_irqsave(&priv->driver_lock, flags);
840 while (priv->cur_cmd != NULL || priv->dnld_sent != DNLD_RES_RECEIVED) {
841 spin_unlock_irqrestore(&priv->driver_lock, flags);
842 if (wait_event_interruptible(priv->waitq,
843 (priv->cur_cmd == NULL &&
844 priv->dnld_sent == DNLD_RES_RECEIVED))) {
845 return -ERESTARTSYS;
846 }
847 spin_lock_irqsave(&priv->driver_lock, flags);
848 }
849 priv->dnld_sent = DNLD_BOOTCMD_SENT;
850 spin_unlock_irqrestore(&priv->driver_lock, flags);
851
852 ret = __if_usb_prog_firmware(cardp, fwname, cmd);
853
854 spin_lock_irqsave(&priv->driver_lock, flags);
855 priv->dnld_sent = DNLD_RES_RECEIVED;
856 spin_unlock_irqrestore(&priv->driver_lock, flags);
857
858 wake_up_interruptible(&priv->waitq);
859
860 return ret;
861}
862
863static int __if_usb_prog_firmware(struct if_usb_card *cardp,
864 const char *fwname, int cmd)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400865{
Dan Williams954ee162007-08-20 11:43:25 -0400866 int i = 0;
867 static int reset_count = 10;
868 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400869
Dan Williams954ee162007-08-20 11:43:25 -0400870 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400871
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700872 ret = request_firmware(&cardp->fw, fwname, &cardp->udev->dev);
873 if (ret < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400874 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700875 lbs_pr_err("firmware %s not found\n", fwname);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400876 goto done;
877 }
878
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700879 if (check_fwfile_format(cardp->fw->data, cardp->fw->size)) {
880 ret = -EINVAL;
Dan Williams954ee162007-08-20 11:43:25 -0400881 goto release_fw;
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700882 }
883
884 /* Cancel any pending usb business */
885 usb_kill_urb(cardp->rx_urb);
886 usb_kill_urb(cardp->tx_urb);
887
888 cardp->fwlastblksent = 0;
889 cardp->fwdnldover = 0;
890 cardp->totalbytes = 0;
891 cardp->fwfinalblk = 0;
892 cardp->bootcmdresp = 0;
Dan Williams954ee162007-08-20 11:43:25 -0400893
894restart:
895 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
896 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700897 ret = -EIO;
Dan Williams954ee162007-08-20 11:43:25 -0400898 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400899 }
900
Dan Williams954ee162007-08-20 11:43:25 -0400901 cardp->bootcmdresp = 0;
902 do {
903 int j = 0;
904 i++;
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700905 if_usb_issue_boot_command(cardp, cmd);
Dan Williams954ee162007-08-20 11:43:25 -0400906 /* wait for command response */
907 do {
908 j++;
909 msleep_interruptible(100);
910 } while (cardp->bootcmdresp == 0 && j < 10);
911 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400912
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700913 if (cardp->bootcmdresp == BOOT_CMD_RESP_NOT_SUPPORTED) {
914 /* Return to normal operation */
915 ret = -EOPNOTSUPP;
916 usb_kill_urb(cardp->rx_urb);
917 usb_kill_urb(cardp->tx_urb);
918 if (if_usb_submit_rx_urb(cardp) < 0)
919 ret = -EIO;
920 goto release_fw;
921 } else if (cardp->bootcmdresp <= 0) {
Dan Williams954ee162007-08-20 11:43:25 -0400922 if (--reset_count >= 0) {
923 if_usb_reset_device(cardp);
924 goto restart;
925 }
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700926 ret = -EIO;
927 goto release_fw;
Dan Williams954ee162007-08-20 11:43:25 -0400928 }
929
930 i = 0;
931
932 cardp->totalbytes = 0;
933 cardp->fwlastblksent = 0;
934 cardp->CRC_OK = 1;
935 cardp->fwdnldover = 0;
936 cardp->fwseqnum = -1;
937 cardp->totalbytes = 0;
938 cardp->fwfinalblk = 0;
939
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500940 /* Send the first firmware packet... */
941 if_usb_send_fw_pkt(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400942
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500943 /* ... and wait for the process to complete */
944 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
David Woodhouse7e226272007-12-14 22:53:41 -0500945
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500946 del_timer_sync(&cardp->fw_timeout);
David Woodhousec9cd6f92007-12-11 13:15:25 -0500947 usb_kill_urb(cardp->rx_urb);
Dan Williams954ee162007-08-20 11:43:25 -0400948
949 if (!cardp->fwdnldover) {
950 lbs_pr_info("failed to load fw, resetting device!\n");
951 if (--reset_count >= 0) {
952 if_usb_reset_device(cardp);
953 goto restart;
954 }
955
956 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
Brian Cavagnolo1556c0f2008-07-21 11:02:46 -0700957 ret = -EIO;
Dan Williams954ee162007-08-20 11:43:25 -0400958 goto release_fw;
959 }
960
David Woodhouseeae86bf2007-12-14 00:47:05 -0500961 release_fw:
Dan Williams954ee162007-08-20 11:43:25 -0400962 release_firmware(cardp->fw);
963 cardp->fw = NULL;
964
David Woodhouseeae86bf2007-12-14 00:47:05 -0500965 done:
Dan Williams954ee162007-08-20 11:43:25 -0400966 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400967 return ret;
968}
969
970
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200971#ifdef CONFIG_PM
972static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
973{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500974 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100975 struct lbs_private *priv = cardp->priv;
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500976 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200977
Holger Schurig9012b282007-05-25 11:27:16 -0400978 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200979
David Woodhouseaa21c002007-12-08 20:04:36 +0000980 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200981 return -1;
982
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500983 ret = lbs_suspend(priv);
984 if (ret)
985 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200986
987 /* Unlink tx & rx urb */
988 usb_kill_urb(cardp->tx_urb);
989 usb_kill_urb(cardp->rx_urb);
990
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500991 out:
Holger Schurig9012b282007-05-25 11:27:16 -0400992 lbs_deb_leave(LBS_DEB_USB);
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500993 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200994}
995
996static int if_usb_resume(struct usb_interface *intf)
997{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500998 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100999 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001000
Holger Schurig9012b282007-05-25 11:27:16 -04001001 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001002
David Woodhouse6bc822b2007-12-11 12:53:43 -05001003 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001004
David Woodhoused1f7a5b2007-12-12 17:40:56 -05001005 lbs_resume(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001006
Holger Schurig9012b282007-05-25 11:27:16 -04001007 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001008 return 0;
1009}
1010#else
1011#define if_usb_suspend NULL
1012#define if_usb_resume NULL
1013#endif
1014
1015static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -05001016 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001017 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001018 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001019 .id_table = if_usb_table,
1020 .suspend = if_usb_suspend,
1021 .resume = if_usb_resume,
andrey@cozybit.com7b58ccf2008-07-01 11:43:53 -07001022 .reset_resume = if_usb_resume,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001023};
1024
Andres Salomon4fb910f2007-11-20 17:43:45 -05001025static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001026{
Holger Schurig084708b2007-05-25 12:37:58 -04001027 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001028
Holger Schurig084708b2007-05-25 12:37:58 -04001029 lbs_deb_enter(LBS_DEB_MAIN);
1030
Holger Schurig084708b2007-05-25 12:37:58 -04001031 ret = usb_register(&if_usb_driver);
1032
1033 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1034 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001035}
1036
Andres Salomon4fb910f2007-11-20 17:43:45 -05001037static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001038{
Holger Schurig084708b2007-05-25 12:37:58 -04001039 lbs_deb_enter(LBS_DEB_MAIN);
1040
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001041 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001042
1043 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001044}
Holger Schurig084708b2007-05-25 12:37:58 -04001045
1046module_init(if_usb_init_module);
1047module_exit(if_usb_exit_module);
1048
1049MODULE_DESCRIPTION("8388 USB WLAN Driver");
David Woodhouseeae86bf2007-12-14 00:47:05 -05001050MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
Holger Schurig084708b2007-05-25 12:37:58 -04001051MODULE_LICENSE("GPL");