blob: 36288b29abf7da14fe7a58b4d130a517e952daf2 [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
Holger Schurigec3eef22007-05-25 12:49:10 -040010#define DRV_NAME "usb8xxx"
11
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020012#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020013#include "decl.h"
14#include "defs.h"
15#include "dev.h"
Dan Williams14e865b2007-12-10 15:11:23 -050016#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020017#include "if_usb.h"
18
David Woodhouseeae86bf2007-12-14 00:47:05 -050019#define INSANEDEBUG 0
20#define lbs_deb_usb2(...) do { if (INSANEDEBUG) lbs_deb_usbd(__VA_ARGS__); } while (0)
21
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020022#define MESSAGE_HEADER_LEN 4
23
Andres Salomon82209ad2007-11-20 17:43:32 -050024static char *lbs_fw_name = "usb8388.bin";
Holger Schurig10078322007-11-15 18:05:47 -050025module_param_named(fw_name, lbs_fw_name, charp, 0644);
Holger Schurig084708b2007-05-25 12:37:58 -040026
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020027static struct usb_device_id if_usb_table[] = {
28 /* Enter the device signature inside */
Holger Schurig66fcc552007-05-25 00:11:58 -040029 { USB_DEVICE(0x1286, 0x2001) },
30 { USB_DEVICE(0x05a3, 0x8388) },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020031 {} /* Terminating entry */
32};
33
34MODULE_DEVICE_TABLE(usb, if_usb_table);
35
36static void if_usb_receive(struct urb *urb);
37static void if_usb_receive_fwload(struct urb *urb);
David Woodhouseeae86bf2007-12-14 00:47:05 -050038static int if_usb_prog_firmware(struct if_usb_card *cardp);
39static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
40 uint8_t *payload, uint16_t nb);
David Woodhouseeae86bf2007-12-14 00:47:05 -050041static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
42 uint16_t nb);
43static void if_usb_free(struct if_usb_card *cardp);
44static int if_usb_submit_rx_urb(struct if_usb_card *cardp);
45static int if_usb_reset_device(struct if_usb_card *cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020046
47/**
48 * @brief call back function to handle the status of the URB
49 * @param urb pointer to urb structure
50 * @return N/A
51 */
52static void if_usb_write_bulk_callback(struct urb *urb)
53{
David Woodhouseeae86bf2007-12-14 00:47:05 -050054 struct if_usb_card *cardp = (struct if_usb_card *) urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020055
56 /* handle the transmission complete validations */
57
Dan Williams954ee162007-08-20 11:43:25 -040058 if (urb->status == 0) {
Holger Schurig69f90322007-11-23 15:43:44 +010059 struct lbs_private *priv = cardp->priv;
Dan Williams954ee162007-08-20 11:43:25 -040060
David Woodhouseeae86bf2007-12-14 00:47:05 -050061 lbs_deb_usb2(&urb->dev->dev, "URB status is successful\n");
62 lbs_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
63 urb->actual_length);
Dan Williams954ee162007-08-20 11:43:25 -040064
65 /* Used for both firmware TX and regular TX. priv isn't
66 * valid at firmware load time.
67 */
David Woodhousee775ed72007-12-06 14:36:11 +000068 if (priv)
69 lbs_host_to_card_done(priv);
Dan Williams954ee162007-08-20 11:43:25 -040070 } else {
71 /* print the failure status number for debug */
72 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020073 }
74
75 return;
76}
77
78/**
79 * @brief free tx/rx urb, skb and rx buffer
David Woodhouseeae86bf2007-12-14 00:47:05 -050080 * @param cardp pointer if_usb_card
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020081 * @return N/A
82 */
David Woodhouseeae86bf2007-12-14 00:47:05 -050083static void if_usb_free(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020084{
Holger Schurig9012b282007-05-25 11:27:16 -040085 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020086
87 /* Unlink tx & rx urb */
88 usb_kill_urb(cardp->tx_urb);
89 usb_kill_urb(cardp->rx_urb);
90
91 usb_free_urb(cardp->tx_urb);
92 cardp->tx_urb = NULL;
93
94 usb_free_urb(cardp->rx_urb);
95 cardp->rx_urb = NULL;
96
David Woodhouseeae86bf2007-12-14 00:47:05 -050097 kfree(cardp->ep_out_buf);
98 cardp->ep_out_buf = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020099
Holger Schurig9012b282007-05-25 11:27:16 -0400100 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200101}
102
David Woodhouse46949612007-12-17 14:42:33 -0500103static void if_usb_setup_firmware(struct lbs_private *priv)
David Woodhouse04c80f12007-12-06 12:51:00 +0000104{
Holger Schurig43659292008-01-16 15:52:58 +0100105 struct if_usb_card *cardp = priv->card;
David Woodhouse04c80f12007-12-06 12:51:00 +0000106 struct cmd_ds_set_boot2_ver b2_cmd;
David Woodhouse46949612007-12-17 14:42:33 -0500107 struct cmd_ds_802_11_fw_wake_method wake_method;
David Woodhouse04c80f12007-12-06 12:51:00 +0000108
David Woodhouse9fae8992007-12-15 03:46:44 -0500109 b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
David Woodhouse04c80f12007-12-06 12:51:00 +0000110 b2_cmd.action = 0;
Holger Schurig43659292008-01-16 15:52:58 +0100111 b2_cmd.version = cardp->boot2_version;
David Woodhouse04c80f12007-12-06 12:51:00 +0000112
David Woodhouseb23b2062007-12-15 01:22:09 -0500113 if (lbs_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
David Woodhouse04c80f12007-12-06 12:51:00 +0000114 lbs_deb_usb("Setting boot2 version failed\n");
David Woodhouse46949612007-12-17 14:42:33 -0500115
116 priv->wol_gpio = 2; /* Wake via GPIO2... */
117 priv->wol_gap = 20; /* ... after 20ms */
118 lbs_host_sleep_cfg(priv, EHS_WAKE_ON_UNICAST_DATA);
119
120 wake_method.hdr.size = cpu_to_le16(sizeof(wake_method));
121 wake_method.action = cpu_to_le16(CMD_ACT_GET);
122 if (lbs_cmd_with_response(priv, CMD_802_11_FW_WAKE_METHOD, &wake_method)) {
123 lbs_pr_info("Firmware does not seem to support PS mode\n");
124 } else {
125 if (le16_to_cpu(wake_method.method) == CMD_WAKE_METHOD_COMMAND_INT) {
126 lbs_deb_usb("Firmware seems to support PS with wake-via-command\n");
127 priv->ps_supported = 1;
128 } else {
129 /* The versions which boot up this way don't seem to
130 work even if we set it to the command interrupt */
131 lbs_pr_info("Firmware doesn't wake via command interrupt; disabling PS mode\n");
132 }
133 }
David Woodhouse04c80f12007-12-06 12:51:00 +0000134}
135
David Woodhouse2fd6cfe2007-12-11 17:44:10 -0500136static void if_usb_fw_timeo(unsigned long priv)
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500137{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500138 struct if_usb_card *cardp = (void *)priv;
David Woodhouse04c80f12007-12-06 12:51:00 +0000139
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500140 if (cardp->fwdnldover) {
141 lbs_deb_usb("Download complete, no event. Assuming success\n");
142 } else {
143 lbs_pr_err("Download timed out\n");
144 cardp->surprise_removed = 1;
145 }
146 wake_up(&cardp->fw_wq);
147}
David Woodhouseeae86bf2007-12-14 00:47:05 -0500148
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200149/**
150 * @brief sets the configuration values
151 * @param ifnum interface number
152 * @param id pointer to usb_device_id
153 * @return 0 on success, error code on failure
154 */
155static int if_usb_probe(struct usb_interface *intf,
156 const struct usb_device_id *id)
157{
158 struct usb_device *udev;
159 struct usb_host_interface *iface_desc;
160 struct usb_endpoint_descriptor *endpoint;
Holger Schurig69f90322007-11-23 15:43:44 +0100161 struct lbs_private *priv;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500162 struct if_usb_card *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200163 int i;
164
165 udev = interface_to_usbdev(intf);
166
David Woodhouseeae86bf2007-12-14 00:47:05 -0500167 cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400168 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200169 lbs_pr_err("Out of memory allocating private data.\n");
170 goto error;
171 }
172
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500173 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
174 init_waitqueue_head(&cardp->fw_wq);
David Woodhouse7e226272007-12-14 22:53:41 -0500175
Holger Schurig435a1ac2007-05-25 12:41:52 -0400176 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200177 iface_desc = intf->cur_altsetting;
178
Holger Schurig9012b282007-05-25 11:27:16 -0400179 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
David Woodhouseeae86bf2007-12-14 00:47:05 -0500180 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400181 le16_to_cpu(udev->descriptor.bcdUSB),
182 udev->descriptor.bDeviceClass,
183 udev->descriptor.bDeviceSubClass,
184 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200185
186 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
187 endpoint = &iface_desc->endpoint[i].desc;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500188 if (usb_endpoint_is_bulk_in(endpoint)) {
189 cardp->ep_in_size = le16_to_cpu(endpoint->wMaxPacketSize);
190 cardp->ep_in = usb_endpoint_num(endpoint);
191
192 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n", cardp->ep_in);
193 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n", cardp->ep_in_size);
194
195 } else if (usb_endpoint_is_bulk_out(endpoint)) {
196 cardp->ep_out_size = le16_to_cpu(endpoint->wMaxPacketSize);
197 cardp->ep_out = usb_endpoint_num(endpoint);
198
199 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n", cardp->ep_out);
200 lbs_deb_usbd(&udev->dev, "Bulk out size is %d\n", cardp->ep_out_size);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200201 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500202 }
203 if (!cardp->ep_out_size || !cardp->ep_in_size) {
204 lbs_deb_usbd(&udev->dev, "Endpoints not found\n");
205 goto dealloc;
206 }
207 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
208 lbs_deb_usbd(&udev->dev, "Rx URB allocation failed\n");
209 goto dealloc;
210 }
211 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
212 lbs_deb_usbd(&udev->dev, "Tx URB allocation failed\n");
213 goto dealloc;
214 }
215 cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE, GFP_KERNEL);
216 if (!cardp->ep_out_buf) {
217 lbs_deb_usbd(&udev->dev, "Could not allocate buffer\n");
218 goto dealloc;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200219 }
220
Dan Williams954ee162007-08-20 11:43:25 -0400221 /* Upload firmware */
Dan Williams954ee162007-08-20 11:43:25 -0400222 if (if_usb_prog_firmware(cardp)) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500223 lbs_deb_usbd(&udev->dev, "FW upload failed\n");
Dan Williams954ee162007-08-20 11:43:25 -0400224 goto err_prog_firmware;
225 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400226
Holger Schurig10078322007-11-15 18:05:47 -0500227 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400228 goto err_prog_firmware;
229
230 cardp->priv = priv;
David Woodhousec9cd6f92007-12-11 13:15:25 -0500231 cardp->priv->fw_ready = 1;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400232
Holger Schurig208fdd22007-05-25 12:17:06 -0400233 priv->hw_host_to_card = if_usb_host_to_card;
Holger Schurig43659292008-01-16 15:52:58 +0100234 cardp->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400235
Dan Williams954ee162007-08-20 11:43:25 -0400236 if_usb_submit_rx_urb(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400237
Holger Schurig10078322007-11-15 18:05:47 -0500238 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400239 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400240
David Woodhouse46949612007-12-17 14:42:33 -0500241 if_usb_setup_firmware(priv);
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500242
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200243 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400244 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200245
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200246 return 0;
247
Dan Williams954ee162007-08-20 11:43:25 -0400248err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500249 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400250err_prog_firmware:
251 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200252dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400253 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200254
255error:
256 return -ENOMEM;
257}
258
259/**
260 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400261 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200262 * @return N/A
263 */
264static void if_usb_disconnect(struct usb_interface *intf)
265{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500266 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100267 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200268
Dan Williams954ee162007-08-20 11:43:25 -0400269 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200270
Dan Williams954ee162007-08-20 11:43:25 -0400271 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200272
Dan Williams954ee162007-08-20 11:43:25 -0400273 if (priv) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000274 priv->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500275 lbs_stop_card(priv);
Holger Schurig10078322007-11-15 18:05:47 -0500276 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400277 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200278
279 /* Unlink and free urb */
280 if_usb_free(cardp);
281
282 usb_set_intfdata(intf, NULL);
283 usb_put_dev(interface_to_usbdev(intf));
284
Dan Williams954ee162007-08-20 11:43:25 -0400285 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200286}
287
288/**
289 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100290 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200291 * @return 0
292 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500293static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200294{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500295 struct fwdata *fwdata = cardp->ep_out_buf;
296 uint8_t *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200297
David Woodhouseeae86bf2007-12-14 00:47:05 -0500298 /* If we got a CRC failure on the last block, back
299 up and retry it */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200300 if (!cardp->CRC_OK) {
301 cardp->totalbytes = cardp->fwlastblksent;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500302 cardp->fwseqnum--;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200303 }
304
David Woodhouseeae86bf2007-12-14 00:47:05 -0500305 lbs_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
306 cardp->totalbytes);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200307
David Woodhouseeae86bf2007-12-14 00:47:05 -0500308 /* struct fwdata (which we sent to the card) has an
309 extra __le32 field in between the header and the data,
310 which is not in the struct fwheader in the actual
311 firmware binary. Insert the seqnum in the middle... */
312 memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200313 sizeof(struct fwheader));
314
315 cardp->fwlastblksent = cardp->totalbytes;
316 cardp->totalbytes += sizeof(struct fwheader);
317
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200318 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouseeae86bf2007-12-14 00:47:05 -0500319 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200320
David Woodhouseeae86bf2007-12-14 00:47:05 -0500321 lbs_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
322 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200323
David Woodhouseeae86bf2007-12-14 00:47:05 -0500324 fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
325 cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200326
David Woodhouseeae86bf2007-12-14 00:47:05 -0500327 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
328 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200329
David Woodhouseeae86bf2007-12-14 00:47:05 -0500330 if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
331 lbs_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
332 lbs_deb_usb2(&cardp->udev->dev, "seqnum = %d totalbytes = %d\n",
333 cardp->fwseqnum, cardp->totalbytes);
334 } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
335 lbs_deb_usb2(&cardp->udev->dev, "Host has finished FW downloading\n");
336 lbs_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200337
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338 cardp->fwfinalblk = 1;
339 }
340
David Woodhouseeae86bf2007-12-14 00:47:05 -0500341 lbs_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
342 cardp->totalbytes);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200343
344 return 0;
345}
346
David Woodhouseeae86bf2007-12-14 00:47:05 -0500347static int if_usb_reset_device(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200348{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500349 struct cmd_ds_command *cmd = cardp->ep_out_buf + 4;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200350 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200351
Holger Schurig3874d0f2007-05-25 12:01:42 -0400352 lbs_deb_enter(LBS_DEB_USB);
353
David Woodhouseeae86bf2007-12-14 00:47:05 -0500354 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000355
356 cmd->command = cpu_to_le16(CMD_802_11_RESET);
357 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
358 cmd->result = cpu_to_le16(0);
359 cmd->seqnum = cpu_to_le16(0x5a5a);
360 cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500361 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 +0000362
David Woodhousec8ba39d2007-12-10 18:53:34 -0500363 msleep(100);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200364 ret = usb_reset_device(cardp->udev);
David Woodhousec8ba39d2007-12-10 18:53:34 -0500365 msleep(100);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400366
367 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
368
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200369 return ret;
370}
371
372/**
373 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100374 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200375 * @param payload pointer to payload data
376 * @param nb data length
377 * @return 0 or -1
378 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500379static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, uint16_t nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200380{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200381 int ret = -1;
382
383 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400384 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400385 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200386 goto tx_ret;
387 }
388
389 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
390 usb_sndbulkpipe(cardp->udev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500391 cardp->ep_out),
Dan Williams954ee162007-08-20 11:43:25 -0400392 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200393
394 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
395
396 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
David Woodhouse0856e682007-12-07 15:12:26 +0000397 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200398 ret = -1;
399 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500400 lbs_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200401 ret = 0;
402 }
403
404tx_ret:
405 return ret;
406}
407
David Woodhouseeae86bf2007-12-14 00:47:05 -0500408static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
Dan Williams954ee162007-08-20 11:43:25 -0400409 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200410{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200411 struct sk_buff *skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200412 int ret = -1;
413
414 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
415 lbs_pr_err("No free skb\n");
416 goto rx_ret;
417 }
418
David Woodhouseeae86bf2007-12-14 00:47:05 -0500419 cardp->rx_skb = skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200420
421 /* Fill the receive configuration URB and initialise the Rx call back */
422 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500423 usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
Dan Williams4269e2a2007-05-10 23:10:18 -0400424 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200425 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500426 cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200427
428 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
429
David Woodhouseeae86bf2007-12-14 00:47:05 -0500430 lbs_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200431 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
David Woodhouse0856e682007-12-07 15:12:26 +0000432 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000433 kfree_skb(skb);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500434 cardp->rx_skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200435 ret = -1;
436 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500437 lbs_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438 ret = 0;
439 }
440
441rx_ret:
442 return ret;
443}
444
David Woodhouseeae86bf2007-12-14 00:47:05 -0500445static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446{
Dan Williams954ee162007-08-20 11:43:25 -0400447 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200448}
449
David Woodhouseeae86bf2007-12-14 00:47:05 -0500450static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451{
Dan Williams954ee162007-08-20 11:43:25 -0400452 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200453}
454
455static void if_usb_receive_fwload(struct urb *urb)
456{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500457 struct if_usb_card *cardp = urb->context;
458 struct sk_buff *skb = cardp->rx_skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200459 struct fwsyncheader *syncfwheader;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500460 struct bootcmdresp bootcmdresp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200461
462 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400463 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500464 "URB status is failed during fw load\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200465 kfree_skb(skb);
466 return;
467 }
468
David Woodhousec9cd6f92007-12-11 13:15:25 -0500469 if (cardp->fwdnldover) {
470 __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
471
472 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
473 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
474 lbs_pr_info("Firmware ready event received\n");
475 wake_up(&cardp->fw_wq);
476 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500477 lbs_deb_usb("Waiting for confirmation; got %x %x\n",
478 le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
David Woodhousec9cd6f92007-12-11 13:15:25 -0500479 if_usb_submit_rx_urb_fwload(cardp);
480 }
481 kfree_skb(skb);
482 return;
483 }
David Woodhousec8ba39d2007-12-10 18:53:34 -0500484 if (cardp->bootcmdresp <= 0) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200485 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
486 sizeof(bootcmdresp));
David Woodhouseeae86bf2007-12-14 00:47:05 -0500487
David Woodhouse981f1872007-05-25 23:36:54 -0400488 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200489 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400490 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200491 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400492 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500493 "Received valid boot command response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200494 return;
495 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500496 if (bootcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
497 if (bootcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
498 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
499 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
David Woodhousec9cd6f92007-12-11 13:15:25 -0500500 if (!cardp->bootcmdresp)
501 lbs_pr_info("Firmware already seems alive; resetting\n");
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000502 cardp->bootcmdresp = -1;
503 } else {
504 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
David Woodhouseeae86bf2007-12-14 00:47:05 -0500505 le32_to_cpu(bootcmdresp.magic));
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000506 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500507 } else if (bootcmdresp.cmd != BOOT_CMD_FW_BY_USB) {
508 lbs_pr_info("boot cmd response cmd_tag error (%d)\n",
509 bootcmdresp.cmd);
510 } else if (bootcmdresp.result != BOOT_CMD_RESP_OK) {
511 lbs_pr_info("boot cmd response result error (%d)\n",
512 bootcmdresp.result);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513 } else {
514 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400515 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500516 "Received valid boot command response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200517 }
518 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400519 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200520 return;
521 }
522
523 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
524 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400525 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200526 kfree_skb(skb);
527 return;
528 }
529
530 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500531 sizeof(struct fwsyncheader));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200532
533 if (!syncfwheader->cmd) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500534 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk with correct CRC\n");
535 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk seqnum = %d\n",
536 le32_to_cpu(syncfwheader->seqnum));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200537 cardp->CRC_OK = 1;
538 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500539 lbs_deb_usbd(&cardp->udev->dev, "FW received Blk with CRC error\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200540 cardp->CRC_OK = 0;
541 }
542
543 kfree_skb(skb);
544
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500545 /* reschedule timer for 200ms hence */
546 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
547
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200548 if (cardp->fwfinalblk) {
549 cardp->fwdnldover = 1;
550 goto exit;
551 }
552
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500553 if_usb_send_fw_pkt(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200554
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500555 exit:
David Woodhousec9cd6f92007-12-11 13:15:25 -0500556 if_usb_submit_rx_urb_fwload(cardp);
557
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200558 kfree(syncfwheader);
559
560 return;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200561}
562
563#define MRVDRV_MIN_PKT_LEN 30
564
565static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500566 struct if_usb_card *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100567 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200568{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500569 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
570 || recvlength < MRVDRV_MIN_PKT_LEN) {
571 lbs_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200572 kfree_skb(skb);
573 return;
574 }
575
576 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
577 skb_put(skb, recvlength);
578 skb_pull(skb, MESSAGE_HEADER_LEN);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500579
Holger Schurig10078322007-11-15 18:05:47 -0500580 lbs_process_rxed_packet(priv, skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200581}
582
David Woodhouseeae86bf2007-12-14 00:47:05 -0500583static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200584 struct sk_buff *skb,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500585 struct if_usb_card *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100586 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200587{
Holger Schurig7919b892008-04-01 14:50:43 +0200588 u8 i;
589
Dan Williamsddac4522007-12-11 13:49:39 -0500590 if (recvlength > LBS_CMD_BUFFER_SIZE) {
Holger Schurig9012b282007-05-25 11:27:16 -0400591 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500592 "The receive buffer is too large\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200593 kfree_skb(skb);
594 return;
595 }
596
597 if (!in_interrupt())
598 BUG();
599
David Woodhouseaa21c002007-12-08 20:04:36 +0000600 spin_lock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200601
Holger Schurig7919b892008-04-01 14:50:43 +0200602 i = (priv->resp_idx == 0) ? 1 : 0;
603 BUG_ON(priv->resp_len[i]);
604 priv->resp_len[i] = (recvlength - MESSAGE_HEADER_LEN);
605 memcpy(priv->resp_buf[i], recvbuff + MESSAGE_HEADER_LEN,
606 priv->resp_len[i]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200607 kfree_skb(skb);
Holger Schurig7919b892008-04-01 14:50:43 +0200608 lbs_notify_command_response(priv, i);
609
David Woodhouseaa21c002007-12-08 20:04:36 +0000610 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200611
Holger Schurig9012b282007-05-25 11:27:16 -0400612 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200613 "Wake up main thread to handle cmd response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200614}
615
616/**
617 * @brief This function reads of the packet into the upload buff,
618 * wake up the main thread and initialise the Rx callack.
619 *
620 * @param urb pointer to struct urb
621 * @return N/A
622 */
623static void if_usb_receive(struct urb *urb)
624{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500625 struct if_usb_card *cardp = urb->context;
626 struct sk_buff *skb = cardp->rx_skb;
Holger Schurig69f90322007-11-23 15:43:44 +0100627 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200628 int recvlength = urb->actual_length;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500629 uint8_t *recvbuff = NULL;
630 uint32_t recvtype = 0;
631 __le32 *pkt = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
Holger Schurig7919b892008-04-01 14:50:43 +0200632 uint32_t event;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200633
Holger Schurig9012b282007-05-25 11:27:16 -0400634 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200635
636 if (recvlength) {
637 if (urb->status) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500638 lbs_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
639 urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200640 kfree_skb(skb);
641 goto setup_for_next;
642 }
643
644 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500645 recvtype = le32_to_cpu(pkt[0]);
Holger Schurig9012b282007-05-25 11:27:16 -0400646 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400647 "Recv length = 0x%x, Recv type = 0x%X\n",
648 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000649 } else if (urb->status) {
650 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200651 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000652 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200653
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200654 switch (recvtype) {
655 case CMD_TYPE_DATA:
656 process_cmdtypedata(recvlength, skb, cardp, priv);
657 break;
658
659 case CMD_TYPE_REQUEST:
660 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
661 break;
662
663 case CMD_TYPE_INDICATION:
Holger Schurig7919b892008-04-01 14:50:43 +0200664 /* Event handling */
665 event = le32_to_cpu(pkt[1]);
666 lbs_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n", event);
667 kfree_skb(skb);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500668
669 /* Icky undocumented magic special case */
Holger Schurig7919b892008-04-01 14:50:43 +0200670 if (event & 0xffff0000) {
671 u32 trycount = (event & 0xffff0000) >> 16;
672
673 lbs_send_tx_feedback(priv, trycount);
674 } else
675 lbs_queue_event(priv, event & 0xFF);
676 break;
677
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200678 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400679 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
David Woodhouseeae86bf2007-12-14 00:47:05 -0500680 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200681 kfree_skb(skb);
682 break;
683 }
684
685setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400686 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200687rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400688 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689}
690
691/**
692 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100693 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694 * @param type type of data
695 * @param buf pointer to data buffer
696 * @param len number of bytes
697 * @return 0 or -1
698 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500699static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
700 uint8_t *payload, uint16_t nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200701{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500702 struct if_usb_card *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703
Holger Schurig9012b282007-05-25 11:27:16 -0400704 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
705 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200706
707 if (type == MVMS_CMD) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500708 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400709 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500711 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400712 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200713 }
714
David Woodhouseeae86bf2007-12-14 00:47:05 -0500715 memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716
David Woodhouseeae86bf2007-12-14 00:47:05 -0500717 return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200718}
719
Dan Williams9e22cb62007-08-02 11:14:49 -0400720/**
721 * @brief This function issues Boot command to the Boot2 code
722 * @param ivalue 1:Boot from FW by USB-Download
723 * 2:Boot from FW in EEPROM
724 * @return 0
725 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500726static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400727{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500728 struct bootcmd *bootcmd = cardp->ep_out_buf;
Dan Williams9e22cb62007-08-02 11:14:49 -0400729
730 /* Prepare command */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500731 bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
732 bootcmd->cmd = ivalue;
733 memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
Dan Williams9e22cb62007-08-02 11:14:49 -0400734
735 /* Issue command */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500736 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd));
Dan Williams9e22cb62007-08-02 11:14:49 -0400737
738 return 0;
739}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200740
741
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400742/**
743 * @brief This function checks the validity of Boot2/FW image.
744 *
745 * @param data pointer to image
746 * len image length
747 * @return 0 or -1
748 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500749static int check_fwfile_format(uint8_t *data, uint32_t totlen)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400750{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500751 uint32_t bincmd, exit;
752 uint32_t blksize, offset, len;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400753 int ret;
754
755 ret = 1;
756 exit = len = 0;
757
758 do {
759 struct fwheader *fwh = (void *)data;
760
761 bincmd = le32_to_cpu(fwh->dnldcmd);
762 blksize = le32_to_cpu(fwh->datalength);
763 switch (bincmd) {
764 case FW_HAS_DATA_TO_RECV:
765 offset = sizeof(struct fwheader) + blksize;
766 data += offset;
767 len += offset;
768 if (len >= totlen)
769 exit = 1;
770 break;
771 case FW_HAS_LAST_BLOCK:
772 exit = 1;
773 ret = 0;
774 break;
775 default:
776 exit = 1;
777 break;
778 }
779 } while (!exit);
780
781 if (ret)
782 lbs_pr_err("firmware file format check FAIL\n");
783 else
784 lbs_deb_fw("firmware file format check PASS\n");
785
786 return ret;
787}
788
789
David Woodhouseeae86bf2007-12-14 00:47:05 -0500790static int if_usb_prog_firmware(struct if_usb_card *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400791{
Dan Williams954ee162007-08-20 11:43:25 -0400792 int i = 0;
793 static int reset_count = 10;
794 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400795
Dan Williams954ee162007-08-20 11:43:25 -0400796 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400797
Holger Schurig10078322007-11-15 18:05:47 -0500798 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400799 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400800 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500801 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400802 goto done;
803 }
804
Dan Williams954ee162007-08-20 11:43:25 -0400805 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
806 goto release_fw;
807
808restart:
809 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
810 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
811 ret = -1;
812 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400813 }
814
Dan Williams954ee162007-08-20 11:43:25 -0400815 cardp->bootcmdresp = 0;
816 do {
817 int j = 0;
818 i++;
819 /* Issue Boot command = 1, Boot from Download-FW */
820 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
821 /* wait for command response */
822 do {
823 j++;
824 msleep_interruptible(100);
825 } while (cardp->bootcmdresp == 0 && j < 10);
826 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400827
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000828 if (cardp->bootcmdresp <= 0) {
Dan Williams954ee162007-08-20 11:43:25 -0400829 if (--reset_count >= 0) {
830 if_usb_reset_device(cardp);
831 goto restart;
832 }
833 return -1;
834 }
835
836 i = 0;
837
838 cardp->totalbytes = 0;
839 cardp->fwlastblksent = 0;
840 cardp->CRC_OK = 1;
841 cardp->fwdnldover = 0;
842 cardp->fwseqnum = -1;
843 cardp->totalbytes = 0;
844 cardp->fwfinalblk = 0;
845
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500846 /* Send the first firmware packet... */
847 if_usb_send_fw_pkt(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400848
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500849 /* ... and wait for the process to complete */
850 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
David Woodhouse7e226272007-12-14 22:53:41 -0500851
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500852 del_timer_sync(&cardp->fw_timeout);
David Woodhousec9cd6f92007-12-11 13:15:25 -0500853 usb_kill_urb(cardp->rx_urb);
Dan Williams954ee162007-08-20 11:43:25 -0400854
855 if (!cardp->fwdnldover) {
856 lbs_pr_info("failed to load fw, resetting device!\n");
857 if (--reset_count >= 0) {
858 if_usb_reset_device(cardp);
859 goto restart;
860 }
861
862 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
863 ret = -1;
864 goto release_fw;
865 }
866
David Woodhouseeae86bf2007-12-14 00:47:05 -0500867 release_fw:
Dan Williams954ee162007-08-20 11:43:25 -0400868 release_firmware(cardp->fw);
869 cardp->fw = NULL;
870
David Woodhouseeae86bf2007-12-14 00:47:05 -0500871 done:
Dan Williams954ee162007-08-20 11:43:25 -0400872 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400873 return ret;
874}
875
876
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200877#ifdef CONFIG_PM
878static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
879{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500880 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100881 struct lbs_private *priv = cardp->priv;
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500882 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200883
Holger Schurig9012b282007-05-25 11:27:16 -0400884 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200885
David Woodhouseaa21c002007-12-08 20:04:36 +0000886 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200887 return -1;
888
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500889 ret = lbs_suspend(priv);
890 if (ret)
891 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200892
893 /* Unlink tx & rx urb */
894 usb_kill_urb(cardp->tx_urb);
895 usb_kill_urb(cardp->rx_urb);
896
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500897 out:
Holger Schurig9012b282007-05-25 11:27:16 -0400898 lbs_deb_leave(LBS_DEB_USB);
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500899 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200900}
901
902static int if_usb_resume(struct usb_interface *intf)
903{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500904 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100905 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200906
Holger Schurig9012b282007-05-25 11:27:16 -0400907 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200908
David Woodhouse6bc822b2007-12-11 12:53:43 -0500909 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200910
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500911 lbs_resume(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200912
Holger Schurig9012b282007-05-25 11:27:16 -0400913 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200914 return 0;
915}
916#else
917#define if_usb_suspend NULL
918#define if_usb_resume NULL
919#endif
920
921static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -0500922 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200923 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200924 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200925 .id_table = if_usb_table,
926 .suspend = if_usb_suspend,
927 .resume = if_usb_resume,
andrey@cozybit.com7b58ccf2008-07-01 11:43:53 -0700928 .reset_resume = if_usb_resume,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200929};
930
Andres Salomon4fb910f2007-11-20 17:43:45 -0500931static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200932{
Holger Schurig084708b2007-05-25 12:37:58 -0400933 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200934
Holger Schurig084708b2007-05-25 12:37:58 -0400935 lbs_deb_enter(LBS_DEB_MAIN);
936
Holger Schurig084708b2007-05-25 12:37:58 -0400937 ret = usb_register(&if_usb_driver);
938
939 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
940 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200941}
942
Andres Salomon4fb910f2007-11-20 17:43:45 -0500943static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200944{
Holger Schurig084708b2007-05-25 12:37:58 -0400945 lbs_deb_enter(LBS_DEB_MAIN);
946
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200947 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -0400948
949 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200950}
Holger Schurig084708b2007-05-25 12:37:58 -0400951
952module_init(if_usb_init_module);
953module_exit(if_usb_exit_module);
954
955MODULE_DESCRIPTION("8388 USB WLAN Driver");
David Woodhouseeae86bf2007-12-14 00:47:05 -0500956MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
Holger Schurig084708b2007-05-25 12:37:58 -0400957MODULE_LICENSE("GPL");