blob: 141d185ac01bb9642ae0fa3187dc4b9ea0b54c28 [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"
16#include "if_usb.h"
17
18#define MESSAGE_HEADER_LEN 4
19
Andres Salomon82209ad2007-11-20 17:43:32 -050020static char *lbs_fw_name = "usb8388.bin";
Holger Schurig10078322007-11-15 18:05:47 -050021module_param_named(fw_name, lbs_fw_name, charp, 0644);
Holger Schurig084708b2007-05-25 12:37:58 -040022
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020023static struct usb_device_id if_usb_table[] = {
24 /* Enter the device signature inside */
Holger Schurig66fcc552007-05-25 00:11:58 -040025 { USB_DEVICE(0x1286, 0x2001) },
26 { USB_DEVICE(0x05a3, 0x8388) },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020027 {} /* Terminating entry */
28};
29
30MODULE_DEVICE_TABLE(usb, if_usb_table);
31
32static void if_usb_receive(struct urb *urb);
33static void if_usb_receive_fwload(struct urb *urb);
Dan Williams954ee162007-08-20 11:43:25 -040034static int if_usb_prog_firmware(struct usb_card_rec *cardp);
Holger Schurig69f90322007-11-23 15:43:44 +010035static int if_usb_host_to_card(struct lbs_private *priv,
36 u8 type,
37 u8 *payload,
38 u16 nb);
39static int if_usb_get_int_status(struct lbs_private *priv, u8 *);
40static int if_usb_read_event_cause(struct lbs_private *);
Dan Williams954ee162007-08-20 11:43:25 -040041static int usb_tx_block(struct usb_card_rec *cardp, u8 *payload, u16 nb);
Holger Schurigac558ca2007-08-02 11:49:06 -040042static void if_usb_free(struct usb_card_rec *cardp);
Dan Williams954ee162007-08-20 11:43:25 -040043static int if_usb_submit_rx_urb(struct usb_card_rec *cardp);
44static int if_usb_reset_device(struct usb_card_rec *cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020045
46/**
47 * @brief call back function to handle the status of the URB
48 * @param urb pointer to urb structure
49 * @return N/A
50 */
51static void if_usb_write_bulk_callback(struct urb *urb)
52{
Dan Williams954ee162007-08-20 11:43:25 -040053 struct usb_card_rec *cardp = (struct usb_card_rec *) urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020054
55 /* handle the transmission complete validations */
56
Dan Williams954ee162007-08-20 11:43:25 -040057 if (urb->status == 0) {
Holger Schurig69f90322007-11-23 15:43:44 +010058 struct lbs_private *priv = cardp->priv;
Dan Williams954ee162007-08-20 11:43:25 -040059
Holger Schurig9012b282007-05-25 11:27:16 -040060 /*
61 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
62 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020063 urb->actual_length);
Holger Schurig9012b282007-05-25 11:27:16 -040064 */
Dan Williams954ee162007-08-20 11:43:25 -040065
66 /* Used for both firmware TX and regular TX. priv isn't
67 * valid at firmware load time.
68 */
David Woodhousee775ed72007-12-06 14:36:11 +000069 if (priv)
70 lbs_host_to_card_done(priv);
Dan Williams954ee162007-08-20 11:43:25 -040071 } else {
72 /* print the failure status number for debug */
73 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020074 }
75
76 return;
77}
78
79/**
80 * @brief free tx/rx urb, skb and rx buffer
81 * @param cardp pointer usb_card_rec
82 * @return N/A
83 */
Holger Schurigac558ca2007-08-02 11:49:06 -040084static void if_usb_free(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020085{
Holger Schurig9012b282007-05-25 11:27:16 -040086 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020087
88 /* Unlink tx & rx urb */
89 usb_kill_urb(cardp->tx_urb);
90 usb_kill_urb(cardp->rx_urb);
91
92 usb_free_urb(cardp->tx_urb);
93 cardp->tx_urb = NULL;
94
95 usb_free_urb(cardp->rx_urb);
96 cardp->rx_urb = NULL;
97
98 kfree(cardp->bulk_out_buffer);
99 cardp->bulk_out_buffer = NULL;
100
Holger Schurig9012b282007-05-25 11:27:16 -0400101 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200102}
103
David Woodhouse04c80f12007-12-06 12:51:00 +0000104static void if_usb_set_boot2_ver(struct lbs_private *priv)
105{
106 struct cmd_ds_set_boot2_ver b2_cmd;
David Woodhouse04c80f12007-12-06 12:51:00 +0000107
108 b2_cmd.action = 0;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000109 b2_cmd.version = priv->boot2_version;
David Woodhouse04c80f12007-12-06 12:51:00 +0000110
David Woodhouse448a51a2007-12-08 00:59:54 +0000111 if (lbs_cmd(priv, CMD_SET_BOOT2_VER, &b2_cmd, sizeof(b2_cmd), NULL))
David Woodhouse04c80f12007-12-06 12:51:00 +0000112 lbs_deb_usb("Setting boot2 version failed\n");
David Woodhouse04c80f12007-12-06 12:51:00 +0000113}
114
115
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200116/**
117 * @brief sets the configuration values
118 * @param ifnum interface number
119 * @param id pointer to usb_device_id
120 * @return 0 on success, error code on failure
121 */
122static int if_usb_probe(struct usb_interface *intf,
123 const struct usb_device_id *id)
124{
125 struct usb_device *udev;
126 struct usb_host_interface *iface_desc;
127 struct usb_endpoint_descriptor *endpoint;
Holger Schurig69f90322007-11-23 15:43:44 +0100128 struct lbs_private *priv;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400129 struct usb_card_rec *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200130 int i;
131
132 udev = interface_to_usbdev(intf);
133
Holger Schurig435a1ac2007-05-25 12:41:52 -0400134 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
135 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200136 lbs_pr_err("Out of memory allocating private data.\n");
137 goto error;
138 }
139
Holger Schurig435a1ac2007-05-25 12:41:52 -0400140 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200141 iface_desc = intf->cur_altsetting;
142
Holger Schurig9012b282007-05-25 11:27:16 -0400143 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200144 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400145 le16_to_cpu(udev->descriptor.bcdUSB),
146 udev->descriptor.bDeviceClass,
147 udev->descriptor.bDeviceSubClass,
148 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200149
150 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
151 endpoint = &iface_desc->endpoint[i].desc;
152 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
153 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
154 USB_ENDPOINT_XFER_BULK)) {
155 /* we found a bulk in endpoint */
Holger Schurig9012b282007-05-25 11:27:16 -0400156 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400157 le16_to_cpu(endpoint->wMaxPacketSize));
158 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400159 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200160 "Rx URB allocation failed\n");
161 goto dealloc;
162 }
Holger Schurig435a1ac2007-05-25 12:41:52 -0400163 cardp->rx_urb_recall = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200164
Holger Schurig435a1ac2007-05-25 12:41:52 -0400165 cardp->bulk_in_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400166 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400167 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200168 (endpoint->
169 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400170 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200171 endpoint->bEndpointAddress);
172 }
173
174 if (((endpoint->
175 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
176 USB_DIR_OUT)
177 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
178 USB_ENDPOINT_XFER_BULK)) {
179 /* We found bulk out endpoint */
David Woodhouse981f1872007-05-25 23:36:54 -0400180 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400181 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200182 "Tx URB allocation failed\n");
183 goto dealloc;
184 }
185
Holger Schurig435a1ac2007-05-25 12:41:52 -0400186 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400187 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400188 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400189 "Bulk out size is %d\n",
190 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400191 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200192 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400193 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200194 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400195 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200196 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
197 GFP_KERNEL);
198
Holger Schurig435a1ac2007-05-25 12:41:52 -0400199 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400200 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200201 "Could not allocate buffer\n");
202 goto dealloc;
203 }
204 }
205 }
206
Dan Williams954ee162007-08-20 11:43:25 -0400207 /* Upload firmware */
208 cardp->rinfo.cardp = cardp;
209 if (if_usb_prog_firmware(cardp)) {
210 lbs_deb_usbd(&udev->dev, "FW upload failed");
211 goto err_prog_firmware;
212 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400213
Holger Schurig10078322007-11-15 18:05:47 -0500214 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400215 goto err_prog_firmware;
216
217 cardp->priv = priv;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400218
Holger Schurig10078322007-11-15 18:05:47 -0500219 if (lbs_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400220 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400221
Dan Williams954ee162007-08-20 11:43:25 -0400222 cardp->eth_dev = priv->dev;
223
Holger Schurig208fdd22007-05-25 12:17:06 -0400224 priv->hw_host_to_card = if_usb_host_to_card;
225 priv->hw_get_int_status = if_usb_get_int_status;
226 priv->hw_read_event_cause = if_usb_read_event_cause;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000227 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400228
Dan Williams954ee162007-08-20 11:43:25 -0400229 /* Delay 200 ms to waiting for the FW ready */
230 if_usb_submit_rx_urb(cardp);
231 msleep_interruptible(200);
David Woodhouseaa21c002007-12-08 20:04:36 +0000232 priv->fw_ready = 1;
Dan Williams954ee162007-08-20 11:43:25 -0400233
Holger Schurig10078322007-11-15 18:05:47 -0500234 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400235 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400236
David Woodhouse04c80f12007-12-06 12:51:00 +0000237 if_usb_set_boot2_ver(priv);
David Woodhouse2c944042007-12-06 14:41:08 +0000238
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200239 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400240 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200241
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200242 return 0;
243
Dan Williams954ee162007-08-20 11:43:25 -0400244err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500245 lbs_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400246err_add_mesh:
Holger Schurig10078322007-11-15 18:05:47 -0500247 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400248err_prog_firmware:
249 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200250dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400251 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200252
253error:
254 return -ENOMEM;
255}
256
257/**
258 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400259 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200260 * @return N/A
261 */
262static void if_usb_disconnect(struct usb_interface *intf)
263{
264 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100265 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200266
Dan Williams954ee162007-08-20 11:43:25 -0400267 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200268
Dan Williams954ee162007-08-20 11:43:25 -0400269 /* Update Surprise removed to TRUE */
270 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271
Dan Williams954ee162007-08-20 11:43:25 -0400272 if (priv) {
Dan Williams954ee162007-08-20 11:43:25 -0400273
David Woodhouseaa21c002007-12-08 20:04:36 +0000274 priv->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500275 lbs_stop_card(priv);
276 lbs_remove_mesh(priv);
277 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400278 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200279
Andres Salomonbe13f182007-11-20 17:43:55 -0500280 /* this is (apparently?) necessary for future usage of the device */
281 lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
282 0, 0, NULL);
283
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200284 /* Unlink and free urb */
285 if_usb_free(cardp);
286
287 usb_set_intfdata(intf, NULL);
288 usb_put_dev(interface_to_usbdev(intf));
289
Dan Williams954ee162007-08-20 11:43:25 -0400290 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200291}
292
293/**
294 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100295 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200296 * @return 0
297 */
Dan Williams954ee162007-08-20 11:43:25 -0400298static int if_prog_firmware(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200299{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200300 struct FWData *fwdata;
301 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400302 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200303
304 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
305
306 if (!fwdata)
307 return -1;
308
309 fwheader = &fwdata->fwheader;
310
311 if (!cardp->CRC_OK) {
312 cardp->totalbytes = cardp->fwlastblksent;
313 cardp->fwseqnum = cardp->lastseqnum - 1;
314 }
315
Holger Schurig9012b282007-05-25 11:27:16 -0400316 /*
317 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200318 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400319 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200320
321 memcpy(fwheader, &firmware[cardp->totalbytes],
322 sizeof(struct fwheader));
323
324 cardp->fwlastblksent = cardp->totalbytes;
325 cardp->totalbytes += sizeof(struct fwheader);
326
Holger Schurig9012b282007-05-25 11:27:16 -0400327 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200328 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400329 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200330
Holger Schurig9012b282007-05-25 11:27:16 -0400331 /*
332 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400333 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400334 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200335
336 cardp->fwseqnum = cardp->fwseqnum + 1;
337
David Woodhouse981f1872007-05-25 23:36:54 -0400338 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
339 cardp->lastseqnum = cardp->fwseqnum;
340 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200341
David Woodhouse981f1872007-05-25 23:36:54 -0400342 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400343 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400344 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400345 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200346 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
347 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400348 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200349 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400350 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200351
David Woodhousebb793e22007-05-25 23:38:14 -0400352 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400353 /*
354 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200355 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400356 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200357 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400358 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200359 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400360 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200361 cardp->fwfinalblk = 1;
362 }
363
Holger Schurig9012b282007-05-25 11:27:16 -0400364 /*
365 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200366 "The firmware download is done size is %d\n",
367 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400368 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200369
370 kfree(fwdata);
371
372 return 0;
373}
374
Dan Williams954ee162007-08-20 11:43:25 -0400375static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200376{
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000377 struct cmd_ds_command *cmd = (void *)&cardp->bulk_out_buffer[4];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200378 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200379
Holger Schurig3874d0f2007-05-25 12:01:42 -0400380 lbs_deb_enter(LBS_DEB_USB);
381
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000382 *(__le32 *)cardp->bulk_out_buffer = cpu_to_le32(CMD_TYPE_REQUEST);
383
384 cmd->command = cpu_to_le16(CMD_802_11_RESET);
385 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
386 cmd->result = cpu_to_le16(0);
387 cmd->seqnum = cpu_to_le16(0x5a5a);
388 cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
389 usb_tx_block(cardp, cardp->bulk_out_buffer, 4 + S_DS_GEN + sizeof(struct cmd_ds_802_11_reset));
390
391 msleep(10);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200392 ret = usb_reset_device(cardp->udev);
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000393 msleep(10);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400394
395 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
396
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200397 return ret;
398}
399
400/**
401 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100402 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200403 * @param payload pointer to payload data
404 * @param nb data length
405 * @return 0 or -1
406 */
Dan Williams954ee162007-08-20 11:43:25 -0400407static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200408{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200409 int ret = -1;
410
411 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400412 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400413 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200414 goto tx_ret;
415 }
416
417 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
418 usb_sndbulkpipe(cardp->udev,
419 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400420 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200421
422 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
423
424 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
425 /* transfer failed */
David Woodhouse0856e682007-12-07 15:12:26 +0000426 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200427 ret = -1;
428 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400429 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200430 ret = 0;
431 }
432
433tx_ret:
434 return ret;
435}
436
Dan Williams954ee162007-08-20 11:43:25 -0400437static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
438 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200439{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200440 struct sk_buff *skb;
441 struct read_cb_info *rinfo = &cardp->rinfo;
442 int ret = -1;
443
444 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
445 lbs_pr_err("No free skb\n");
446 goto rx_ret;
447 }
448
449 rinfo->skb = skb;
450
451 /* Fill the receive configuration URB and initialise the Rx call back */
452 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
453 usb_rcvbulkpipe(cardp->udev,
454 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400455 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
457 rinfo);
458
459 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
460
Holger Schurig9012b282007-05-25 11:27:16 -0400461 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200462 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
463 /* handle failure conditions */
David Woodhouse0856e682007-12-07 15:12:26 +0000464 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000465 kfree_skb(skb);
466 rinfo->skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200467 ret = -1;
468 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400469 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200470 ret = 0;
471 }
472
473rx_ret:
474 return ret;
475}
476
Dan Williams954ee162007-08-20 11:43:25 -0400477static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200478{
Dan Williams954ee162007-08-20 11:43:25 -0400479 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200480}
481
Dan Williams954ee162007-08-20 11:43:25 -0400482static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200483{
Dan Williams954ee162007-08-20 11:43:25 -0400484 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200485}
486
487static void if_usb_receive_fwload(struct urb *urb)
488{
489 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200490 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400491 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200492 struct fwsyncheader *syncfwheader;
493 struct bootcmdrespStr bootcmdresp;
494
495 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400496 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200497 "URB status is failed during fw load\n");
498 kfree_skb(skb);
499 return;
500 }
501
502 if (cardp->bootcmdresp == 0) {
503 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
504 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400505 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200506 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400507 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200508 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400509 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510 "Received valid boot command response\n");
511 return;
512 }
David Woodhouse981f1872007-05-25 23:36:54 -0400513 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000514 if (bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_REQUEST) ||
515 bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_DATA) ||
516 bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_INDICATION)) {
517 lbs_pr_info("Firmware already seems alive; resetting\n");
518 cardp->bootcmdresp = -1;
519 } else {
520 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
521 le32_to_cpu(bootcmdresp.u32magicnumber));
522 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200523 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
524 lbs_pr_info(
525 "boot cmd response cmd_tag error (%d)\n",
526 bootcmdresp.u8cmd_tag);
527 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
528 lbs_pr_info(
529 "boot cmd response result error (%d)\n",
530 bootcmdresp.u8result);
531 } else {
532 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400533 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200534 "Received valid boot command response\n");
535 }
536 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400537 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538 return;
539 }
540
541 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
542 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400543 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200544 kfree_skb(skb);
545 return;
546 }
547
548 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
549 sizeof(struct fwsyncheader));
550
551 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400552 /*
553 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200554 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400555 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200556 "FW received Blk seqnum = %d\n",
557 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400558 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559 cardp->CRC_OK = 1;
560 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400561 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200562 "FW received Blk with CRC error\n");
563 cardp->CRC_OK = 0;
564 }
565
566 kfree_skb(skb);
567
568 if (cardp->fwfinalblk) {
569 cardp->fwdnldover = 1;
570 goto exit;
571 }
572
Dan Williams954ee162007-08-20 11:43:25 -0400573 if_prog_firmware(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200574
Dan Williams954ee162007-08-20 11:43:25 -0400575 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200576exit:
577 kfree(syncfwheader);
578
579 return;
580
581}
582
583#define MRVDRV_MIN_PKT_LEN 30
584
585static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
586 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100587 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200588{
589 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
590 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400591 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200592 "Packet length is Invalid\n");
593 kfree_skb(skb);
594 return;
595 }
596
597 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
598 skb_put(skb, recvlength);
599 skb_pull(skb, MESSAGE_HEADER_LEN);
Holger Schurig10078322007-11-15 18:05:47 -0500600 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400601 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200602}
603
604static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
605 struct sk_buff *skb,
606 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100607 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200608{
609 u8 *cmdbuf;
610 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400611 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200612 "The receive buffer is too large\n");
613 kfree_skb(skb);
614 return;
615 }
616
617 if (!in_interrupt())
618 BUG();
619
David Woodhouseaa21c002007-12-08 20:04:36 +0000620 spin_lock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200621 /* take care of cur_cmd = NULL case by reading the
622 * data to clear the interrupt */
David Woodhouseaa21c002007-12-08 20:04:36 +0000623 if (!priv->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400624 cmdbuf = priv->upld_buf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000625 priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626 } else
David Woodhouseaa21c002007-12-08 20:04:36 +0000627 cmdbuf = priv->cur_cmd->bufvirtualaddr;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200628
Holger Schurigc95c7f92007-08-02 11:49:45 -0400629 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400630 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200631 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400632 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200633
634 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500635 lbs_interrupt(priv->dev);
David Woodhouseaa21c002007-12-08 20:04:36 +0000636 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200637
Holger Schurig9012b282007-05-25 11:27:16 -0400638 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200639 "Wake up main thread to handle cmd response\n");
640
641 return;
642}
643
644/**
645 * @brief This function reads of the packet into the upload buff,
646 * wake up the main thread and initialise the Rx callack.
647 *
648 * @param urb pointer to struct urb
649 * @return N/A
650 */
651static void if_usb_receive(struct urb *urb)
652{
653 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200654 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400655 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
Holger Schurig69f90322007-11-23 15:43:44 +0100656 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200657
658 int recvlength = urb->actual_length;
659 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400660 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200661
Holger Schurig9012b282007-05-25 11:27:16 -0400662 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663
664 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400665 __le32 tmp;
666
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200667 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400668 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200669 "URB status is failed\n");
670 kfree_skb(skb);
671 goto setup_for_next;
672 }
673
674 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400675 memcpy(&tmp, recvbuff, sizeof(u32));
676 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400677 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400678 "Recv length = 0x%x, Recv type = 0x%X\n",
679 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000680 } else if (urb->status) {
681 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200682 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000683 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200684
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200685 switch (recvtype) {
686 case CMD_TYPE_DATA:
687 process_cmdtypedata(recvlength, skb, cardp, priv);
688 break;
689
690 case CMD_TYPE_REQUEST:
691 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
692 break;
693
694 case CMD_TYPE_INDICATION:
695 /* Event cause handling */
David Woodhouseaa21c002007-12-08 20:04:36 +0000696 spin_lock(&priv->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400697 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400698 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699 cardp->usb_event_cause);
700 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500701 lbs_send_tx_feedback(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000702 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703 break;
704 }
David Woodhouse981f1872007-05-25 23:36:54 -0400705 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400706 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200707 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500708 lbs_interrupt(priv->dev);
David Woodhouseaa21c002007-12-08 20:04:36 +0000709 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710 goto rx_exit;
711 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400712 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
713 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200714 kfree_skb(skb);
715 break;
716 }
717
718setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400719 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200720rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400721 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722}
723
724/**
725 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100726 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200727 * @param type type of data
728 * @param buf pointer to data buffer
729 * @param len number of bytes
730 * @return 0 or -1
731 */
Holger Schurig69f90322007-11-23 15:43:44 +0100732static int if_usb_host_to_card(struct lbs_private *priv,
733 u8 type,
734 u8 *payload,
735 u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200736{
Holger Schurig634b8f42007-05-25 13:05:16 -0400737 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200738
Holger Schurig9012b282007-05-25 11:27:16 -0400739 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
740 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200741
742 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400743 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400744 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200745 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
746 MESSAGE_HEADER_LEN);
747
748 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400749 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400750 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200751 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
752 MESSAGE_HEADER_LEN);
753 }
754
755 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
756
Dan Williams954ee162007-08-20 11:43:25 -0400757 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400758 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759}
760
David Woodhouseaa21c002007-12-08 20:04:36 +0000761/* called with priv->driver_lock held */
Holger Schurig69f90322007-11-23 15:43:44 +0100762static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200763{
Holger Schurig634b8f42007-05-25 13:05:16 -0400764 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200765
766 *ireg = cardp->usb_int_cause;
767 cardp->usb_int_cause = 0;
768
Holger Schurig9012b282007-05-25 11:27:16 -0400769 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200770
771 return 0;
772}
773
Holger Schurig69f90322007-11-23 15:43:44 +0100774static int if_usb_read_event_cause(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200775{
Holger Schurig634b8f42007-05-25 13:05:16 -0400776 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400777
David Woodhouseaa21c002007-12-08 20:04:36 +0000778 priv->eventcause = cardp->usb_event_cause;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200779 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400780 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200781 return 0;
782}
783
Dan Williams9e22cb62007-08-02 11:14:49 -0400784/**
785 * @brief This function issues Boot command to the Boot2 code
786 * @param ivalue 1:Boot from FW by USB-Download
787 * 2:Boot from FW in EEPROM
788 * @return 0
789 */
Dan Williams954ee162007-08-20 11:43:25 -0400790static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400791{
Dan Williams954ee162007-08-20 11:43:25 -0400792 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400793 int i;
794
795 /* Prepare command */
796 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
797 sbootcmd.u8cmd_tag = ivalue;
798 for (i=0; i<11; i++)
799 sbootcmd.au8dumy[i]=0x00;
800 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
801
802 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400803 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400804
805 return 0;
806}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200807
808
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400809/**
810 * @brief This function checks the validity of Boot2/FW image.
811 *
812 * @param data pointer to image
813 * len image length
814 * @return 0 or -1
815 */
816static int check_fwfile_format(u8 *data, u32 totlen)
817{
818 u32 bincmd, exit;
819 u32 blksize, offset, len;
820 int ret;
821
822 ret = 1;
823 exit = len = 0;
824
825 do {
826 struct fwheader *fwh = (void *)data;
827
828 bincmd = le32_to_cpu(fwh->dnldcmd);
829 blksize = le32_to_cpu(fwh->datalength);
830 switch (bincmd) {
831 case FW_HAS_DATA_TO_RECV:
832 offset = sizeof(struct fwheader) + blksize;
833 data += offset;
834 len += offset;
835 if (len >= totlen)
836 exit = 1;
837 break;
838 case FW_HAS_LAST_BLOCK:
839 exit = 1;
840 ret = 0;
841 break;
842 default:
843 exit = 1;
844 break;
845 }
846 } while (!exit);
847
848 if (ret)
849 lbs_pr_err("firmware file format check FAIL\n");
850 else
851 lbs_deb_fw("firmware file format check PASS\n");
852
853 return ret;
854}
855
856
Dan Williams954ee162007-08-20 11:43:25 -0400857static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400858{
Dan Williams954ee162007-08-20 11:43:25 -0400859 int i = 0;
860 static int reset_count = 10;
861 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400862
Dan Williams954ee162007-08-20 11:43:25 -0400863 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400864
Holger Schurig10078322007-11-15 18:05:47 -0500865 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400866 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400867 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500868 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400869 goto done;
870 }
871
Dan Williams954ee162007-08-20 11:43:25 -0400872 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
873 goto release_fw;
874
875restart:
876 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
877 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
878 ret = -1;
879 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400880 }
881
Dan Williams954ee162007-08-20 11:43:25 -0400882 cardp->bootcmdresp = 0;
883 do {
884 int j = 0;
885 i++;
886 /* Issue Boot command = 1, Boot from Download-FW */
887 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
888 /* wait for command response */
889 do {
890 j++;
891 msleep_interruptible(100);
892 } while (cardp->bootcmdresp == 0 && j < 10);
893 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400894
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000895 if (cardp->bootcmdresp <= 0) {
Dan Williams954ee162007-08-20 11:43:25 -0400896 if (--reset_count >= 0) {
897 if_usb_reset_device(cardp);
898 goto restart;
899 }
900 return -1;
901 }
902
903 i = 0;
904
905 cardp->totalbytes = 0;
906 cardp->fwlastblksent = 0;
907 cardp->CRC_OK = 1;
908 cardp->fwdnldover = 0;
909 cardp->fwseqnum = -1;
910 cardp->totalbytes = 0;
911 cardp->fwfinalblk = 0;
912
913 if_prog_firmware(cardp);
914
915 do {
916 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
917 i++;
918 msleep_interruptible(100);
919 if (cardp->surprise_removed || i >= 20)
920 break;
921 } while (!cardp->fwdnldover);
922
923 if (!cardp->fwdnldover) {
924 lbs_pr_info("failed to load fw, resetting device!\n");
925 if (--reset_count >= 0) {
926 if_usb_reset_device(cardp);
927 goto restart;
928 }
929
930 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
931 ret = -1;
932 goto release_fw;
933 }
934
935release_fw:
936 release_firmware(cardp->fw);
937 cardp->fw = NULL;
938
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400939done:
Dan Williams954ee162007-08-20 11:43:25 -0400940 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400941 return ret;
942}
943
944
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200945#ifdef CONFIG_PM
946static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
947{
948 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100949 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200950
Holger Schurig9012b282007-05-25 11:27:16 -0400951 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200952
David Woodhouseaa21c002007-12-08 20:04:36 +0000953 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200954 return -1;
955
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400956 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
957 /* Mesh autostart must be activated while sleeping
958 * On resume it will go back to the current state
959 */
960 struct cmd_ds_mesh_access mesh_access;
961 memset(&mesh_access, 0, sizeof(mesh_access));
962 mesh_access.data[0] = cpu_to_le32(1);
Holger Schurig10078322007-11-15 18:05:47 -0500963 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400964 CMD_MESH_ACCESS,
965 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
966 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
967 }
968
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200969 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400970 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200971
972 /* Unlink tx & rx urb */
973 usb_kill_urb(cardp->tx_urb);
974 usb_kill_urb(cardp->rx_urb);
975
976 cardp->rx_urb_recall = 1;
977
Holger Schurig9012b282007-05-25 11:27:16 -0400978 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200979 return 0;
980}
981
982static int if_usb_resume(struct usb_interface *intf)
983{
984 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100985 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200986
Holger Schurig9012b282007-05-25 11:27:16 -0400987 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200988
989 cardp->rx_urb_recall = 0;
990
991 if_usb_submit_rx_urb(cardp->priv);
992
993 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400994 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200995
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400996 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
997 /* Mesh autostart was activated while sleeping
998 * Disable it if appropriate
999 */
1000 struct cmd_ds_mesh_access mesh_access;
1001 memset(&mesh_access, 0, sizeof(mesh_access));
1002 mesh_access.data[0] = cpu_to_le32(0);
Holger Schurig10078322007-11-15 18:05:47 -05001003 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -04001004 CMD_MESH_ACCESS,
1005 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
1006 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1007 }
1008
Holger Schurig9012b282007-05-25 11:27:16 -04001009 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001010 return 0;
1011}
1012#else
1013#define if_usb_suspend NULL
1014#define if_usb_resume NULL
1015#endif
1016
1017static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -05001018 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001019 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001021 .id_table = if_usb_table,
1022 .suspend = if_usb_suspend,
1023 .resume = if_usb_resume,
1024};
1025
Andres Salomon4fb910f2007-11-20 17:43:45 -05001026static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001027{
Holger Schurig084708b2007-05-25 12:37:58 -04001028 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001029
Holger Schurig084708b2007-05-25 12:37:58 -04001030 lbs_deb_enter(LBS_DEB_MAIN);
1031
Holger Schurig084708b2007-05-25 12:37:58 -04001032 ret = usb_register(&if_usb_driver);
1033
1034 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1035 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001036}
1037
Andres Salomon4fb910f2007-11-20 17:43:45 -05001038static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001039{
Holger Schurig084708b2007-05-25 12:37:58 -04001040 lbs_deb_enter(LBS_DEB_MAIN);
1041
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001042 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001043
1044 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001045}
Holger Schurig084708b2007-05-25 12:37:58 -04001046
1047module_init(if_usb_init_module);
1048module_exit(if_usb_exit_module);
1049
1050MODULE_DESCRIPTION("8388 USB WLAN Driver");
1051MODULE_AUTHOR("Marvell International Ltd.");
1052MODULE_LICENSE("GPL");