blob: 5f2d944312a5c8f0486e7767ba0fe04b8ad59ba7 [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
19#define MESSAGE_HEADER_LEN 4
20
Andres Salomon82209ad2007-11-20 17:43:32 -050021static char *lbs_fw_name = "usb8388.bin";
Holger Schurig10078322007-11-15 18:05:47 -050022module_param_named(fw_name, lbs_fw_name, charp, 0644);
Holger Schurig084708b2007-05-25 12:37:58 -040023
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020024static struct usb_device_id if_usb_table[] = {
25 /* Enter the device signature inside */
Holger Schurig66fcc552007-05-25 00:11:58 -040026 { USB_DEVICE(0x1286, 0x2001) },
27 { USB_DEVICE(0x05a3, 0x8388) },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020028 {} /* Terminating entry */
29};
30
31MODULE_DEVICE_TABLE(usb, if_usb_table);
32
33static void if_usb_receive(struct urb *urb);
34static void if_usb_receive_fwload(struct urb *urb);
Dan Williams954ee162007-08-20 11:43:25 -040035static int if_usb_prog_firmware(struct usb_card_rec *cardp);
Holger Schurig69f90322007-11-23 15:43:44 +010036static int if_usb_host_to_card(struct lbs_private *priv,
37 u8 type,
38 u8 *payload,
39 u16 nb);
40static int if_usb_get_int_status(struct lbs_private *priv, u8 *);
41static int if_usb_read_event_cause(struct lbs_private *);
Dan Williams954ee162007-08-20 11:43:25 -040042static int usb_tx_block(struct usb_card_rec *cardp, u8 *payload, u16 nb);
Holger Schurigac558ca2007-08-02 11:49:06 -040043static void if_usb_free(struct usb_card_rec *cardp);
Dan Williams954ee162007-08-20 11:43:25 -040044static int if_usb_submit_rx_urb(struct usb_card_rec *cardp);
45static int if_usb_reset_device(struct usb_card_rec *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{
Dan Williams954ee162007-08-20 11:43:25 -040054 struct usb_card_rec *cardp = (struct usb_card_rec *) 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
Holger Schurig9012b282007-05-25 11:27:16 -040061 /*
62 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
63 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020064 urb->actual_length);
Holger Schurig9012b282007-05-25 11:27:16 -040065 */
Dan Williams954ee162007-08-20 11:43:25 -040066
67 /* Used for both firmware TX and regular TX. priv isn't
68 * valid at firmware load time.
69 */
David Woodhousee775ed72007-12-06 14:36:11 +000070 if (priv)
71 lbs_host_to_card_done(priv);
Dan Williams954ee162007-08-20 11:43:25 -040072 } else {
73 /* print the failure status number for debug */
74 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020075 }
76
77 return;
78}
79
80/**
81 * @brief free tx/rx urb, skb and rx buffer
82 * @param cardp pointer usb_card_rec
83 * @return N/A
84 */
Holger Schurigac558ca2007-08-02 11:49:06 -040085static void if_usb_free(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020086{
Holger Schurig9012b282007-05-25 11:27:16 -040087 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020088
89 /* Unlink tx & rx urb */
90 usb_kill_urb(cardp->tx_urb);
91 usb_kill_urb(cardp->rx_urb);
92
93 usb_free_urb(cardp->tx_urb);
94 cardp->tx_urb = NULL;
95
96 usb_free_urb(cardp->rx_urb);
97 cardp->rx_urb = NULL;
98
99 kfree(cardp->bulk_out_buffer);
100 cardp->bulk_out_buffer = NULL;
101
Holger Schurig9012b282007-05-25 11:27:16 -0400102 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200103}
104
David Woodhouse04c80f12007-12-06 12:51:00 +0000105static void if_usb_set_boot2_ver(struct lbs_private *priv)
106{
107 struct cmd_ds_set_boot2_ver b2_cmd;
David Woodhouse04c80f12007-12-06 12:51:00 +0000108
109 b2_cmd.action = 0;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000110 b2_cmd.version = priv->boot2_version;
David Woodhouse04c80f12007-12-06 12:51:00 +0000111
Dan Williams14e865b2007-12-10 15:11:23 -0500112 if (lbs_cmd(priv, CMD_SET_BOOT2_VER, b2_cmd, NULL, 0))
David Woodhouse04c80f12007-12-06 12:51:00 +0000113 lbs_deb_usb("Setting boot2 version failed\n");
David Woodhouse04c80f12007-12-06 12:51:00 +0000114}
115
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500116void if_usb_fw_timeo(unsigned long priv)
117{
118 struct usb_card_rec *cardp = (void *)priv;
David Woodhouse04c80f12007-12-06 12:51:00 +0000119
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500120 if (cardp->fwdnldover) {
121 lbs_deb_usb("Download complete, no event. Assuming success\n");
122 } else {
123 lbs_pr_err("Download timed out\n");
124 cardp->surprise_removed = 1;
125 }
126 wake_up(&cardp->fw_wq);
127}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200128/**
129 * @brief sets the configuration values
130 * @param ifnum interface number
131 * @param id pointer to usb_device_id
132 * @return 0 on success, error code on failure
133 */
134static int if_usb_probe(struct usb_interface *intf,
135 const struct usb_device_id *id)
136{
137 struct usb_device *udev;
138 struct usb_host_interface *iface_desc;
139 struct usb_endpoint_descriptor *endpoint;
Holger Schurig69f90322007-11-23 15:43:44 +0100140 struct lbs_private *priv;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400141 struct usb_card_rec *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200142 int i;
143
144 udev = interface_to_usbdev(intf);
145
Holger Schurig435a1ac2007-05-25 12:41:52 -0400146 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
147 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200148 lbs_pr_err("Out of memory allocating private data.\n");
149 goto error;
150 }
151
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500152 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
153 init_waitqueue_head(&cardp->fw_wq);
154
Holger Schurig435a1ac2007-05-25 12:41:52 -0400155 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200156 iface_desc = intf->cur_altsetting;
157
Holger Schurig9012b282007-05-25 11:27:16 -0400158 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200159 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400160 le16_to_cpu(udev->descriptor.bcdUSB),
161 udev->descriptor.bDeviceClass,
162 udev->descriptor.bDeviceSubClass,
163 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200164
165 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
166 endpoint = &iface_desc->endpoint[i].desc;
167 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
168 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
169 USB_ENDPOINT_XFER_BULK)) {
170 /* we found a bulk in endpoint */
Holger Schurig9012b282007-05-25 11:27:16 -0400171 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400172 le16_to_cpu(endpoint->wMaxPacketSize));
173 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400174 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200175 "Rx URB allocation failed\n");
176 goto dealloc;
177 }
Holger Schurig435a1ac2007-05-25 12:41:52 -0400178 cardp->rx_urb_recall = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200179
Holger Schurig435a1ac2007-05-25 12:41:52 -0400180 cardp->bulk_in_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400181 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400182 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200183 (endpoint->
184 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400185 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200186 endpoint->bEndpointAddress);
187 }
188
189 if (((endpoint->
190 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
191 USB_DIR_OUT)
192 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
193 USB_ENDPOINT_XFER_BULK)) {
194 /* We found bulk out endpoint */
David Woodhouse981f1872007-05-25 23:36:54 -0400195 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400196 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200197 "Tx URB allocation failed\n");
198 goto dealloc;
199 }
200
Holger Schurig435a1ac2007-05-25 12:41:52 -0400201 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400202 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400203 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400204 "Bulk out size is %d\n",
205 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400206 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200207 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400208 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200209 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400210 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200211 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
212 GFP_KERNEL);
213
Holger Schurig435a1ac2007-05-25 12:41:52 -0400214 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400215 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200216 "Could not allocate buffer\n");
217 goto dealloc;
218 }
219 }
220 }
221
Dan Williams954ee162007-08-20 11:43:25 -0400222 /* Upload firmware */
223 cardp->rinfo.cardp = cardp;
224 if (if_usb_prog_firmware(cardp)) {
225 lbs_deb_usbd(&udev->dev, "FW upload failed");
226 goto err_prog_firmware;
227 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400228
Holger Schurig10078322007-11-15 18:05:47 -0500229 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400230 goto err_prog_firmware;
231
232 cardp->priv = priv;
David Woodhousec9cd6f92007-12-11 13:15:25 -0500233 cardp->priv->fw_ready = 1;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400234
Holger Schurig10078322007-11-15 18:05:47 -0500235 if (lbs_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400236 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400237
Dan Williams954ee162007-08-20 11:43:25 -0400238 cardp->eth_dev = priv->dev;
239
Holger Schurig208fdd22007-05-25 12:17:06 -0400240 priv->hw_host_to_card = if_usb_host_to_card;
241 priv->hw_get_int_status = if_usb_get_int_status;
242 priv->hw_read_event_cause = if_usb_read_event_cause;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000243 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400244
Dan Williams954ee162007-08-20 11:43:25 -0400245 if_usb_submit_rx_urb(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400246
Holger Schurig10078322007-11-15 18:05:47 -0500247 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400248 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400249
David Woodhouse04c80f12007-12-06 12:51:00 +0000250 if_usb_set_boot2_ver(priv);
David Woodhouse2c944042007-12-06 14:41:08 +0000251
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200252 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400253 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200254
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200255 return 0;
256
Dan Williams954ee162007-08-20 11:43:25 -0400257err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500258 lbs_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400259err_add_mesh:
Holger Schurig10078322007-11-15 18:05:47 -0500260 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400261err_prog_firmware:
262 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200263dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400264 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200265
266error:
267 return -ENOMEM;
268}
269
270/**
271 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400272 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200273 * @return N/A
274 */
275static void if_usb_disconnect(struct usb_interface *intf)
276{
277 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100278 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200279
Dan Williams954ee162007-08-20 11:43:25 -0400280 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200281
Dan Williams954ee162007-08-20 11:43:25 -0400282 /* Update Surprise removed to TRUE */
283 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200284
Dan Williams954ee162007-08-20 11:43:25 -0400285 if (priv) {
Dan Williams954ee162007-08-20 11:43:25 -0400286
David Woodhouseaa21c002007-12-08 20:04:36 +0000287 priv->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500288 lbs_stop_card(priv);
289 lbs_remove_mesh(priv);
290 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400291 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200292
Andres Salomonbe13f182007-11-20 17:43:55 -0500293 /* this is (apparently?) necessary for future usage of the device */
294 lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
295 0, 0, NULL);
296
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200297 /* Unlink and free urb */
298 if_usb_free(cardp);
299
300 usb_set_intfdata(intf, NULL);
301 usb_put_dev(interface_to_usbdev(intf));
302
Dan Williams954ee162007-08-20 11:43:25 -0400303 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200304}
305
306/**
307 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100308 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200309 * @return 0
310 */
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500311static int if_usb_send_fw_pkt(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200312{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200313 struct FWData *fwdata;
314 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400315 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200316
317 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
318
319 if (!fwdata)
320 return -1;
321
322 fwheader = &fwdata->fwheader;
323
324 if (!cardp->CRC_OK) {
325 cardp->totalbytes = cardp->fwlastblksent;
326 cardp->fwseqnum = cardp->lastseqnum - 1;
327 }
328
Holger Schurig9012b282007-05-25 11:27:16 -0400329 /*
330 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200331 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400332 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200333
334 memcpy(fwheader, &firmware[cardp->totalbytes],
335 sizeof(struct fwheader));
336
337 cardp->fwlastblksent = cardp->totalbytes;
338 cardp->totalbytes += sizeof(struct fwheader);
339
Holger Schurig9012b282007-05-25 11:27:16 -0400340 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200341 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400342 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200343
Holger Schurig9012b282007-05-25 11:27:16 -0400344 /*
345 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400346 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400347 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200348
349 cardp->fwseqnum = cardp->fwseqnum + 1;
350
David Woodhouse981f1872007-05-25 23:36:54 -0400351 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
352 cardp->lastseqnum = cardp->fwseqnum;
353 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200354
David Woodhouse981f1872007-05-25 23:36:54 -0400355 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400356 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400357 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400358 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200359 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
360 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400361 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200362 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400363 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200364
David Woodhousebb793e22007-05-25 23:38:14 -0400365 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400366 /*
367 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200368 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400369 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400371 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200372 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400373 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200374 cardp->fwfinalblk = 1;
375 }
376
Holger Schurig9012b282007-05-25 11:27:16 -0400377 /*
378 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200379 "The firmware download is done size is %d\n",
380 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400381 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200382
383 kfree(fwdata);
384
385 return 0;
386}
387
Dan Williams954ee162007-08-20 11:43:25 -0400388static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200389{
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000390 struct cmd_ds_command *cmd = (void *)&cardp->bulk_out_buffer[4];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200391 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200392
Holger Schurig3874d0f2007-05-25 12:01:42 -0400393 lbs_deb_enter(LBS_DEB_USB);
394
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000395 *(__le32 *)cardp->bulk_out_buffer = cpu_to_le32(CMD_TYPE_REQUEST);
396
397 cmd->command = cpu_to_le16(CMD_802_11_RESET);
398 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
399 cmd->result = cpu_to_le16(0);
400 cmd->seqnum = cpu_to_le16(0x5a5a);
401 cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
402 usb_tx_block(cardp, cardp->bulk_out_buffer, 4 + S_DS_GEN + sizeof(struct cmd_ds_802_11_reset));
403
David Woodhousec8ba39d2007-12-10 18:53:34 -0500404 msleep(100);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200405 ret = usb_reset_device(cardp->udev);
David Woodhousec8ba39d2007-12-10 18:53:34 -0500406 msleep(100);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400407
408 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
409
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200410 return ret;
411}
412
413/**
414 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100415 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200416 * @param payload pointer to payload data
417 * @param nb data length
418 * @return 0 or -1
419 */
Dan Williams954ee162007-08-20 11:43:25 -0400420static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200421{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200422 int ret = -1;
423
424 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400425 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400426 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200427 goto tx_ret;
428 }
429
430 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
431 usb_sndbulkpipe(cardp->udev,
432 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400433 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200434
435 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
436
437 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
438 /* transfer failed */
David Woodhouse0856e682007-12-07 15:12:26 +0000439 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200440 ret = -1;
441 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400442 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200443 ret = 0;
444 }
445
446tx_ret:
447 return ret;
448}
449
Dan Williams954ee162007-08-20 11:43:25 -0400450static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
451 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200452{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200453 struct sk_buff *skb;
454 struct read_cb_info *rinfo = &cardp->rinfo;
455 int ret = -1;
456
457 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
458 lbs_pr_err("No free skb\n");
459 goto rx_ret;
460 }
461
462 rinfo->skb = skb;
463
464 /* Fill the receive configuration URB and initialise the Rx call back */
465 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
466 usb_rcvbulkpipe(cardp->udev,
467 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400468 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
470 rinfo);
471
472 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
473
Holger Schurig9012b282007-05-25 11:27:16 -0400474 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200475 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
476 /* handle failure conditions */
David Woodhouse0856e682007-12-07 15:12:26 +0000477 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000478 kfree_skb(skb);
479 rinfo->skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200480 ret = -1;
481 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400482 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200483 ret = 0;
484 }
485
486rx_ret:
487 return ret;
488}
489
Dan Williams954ee162007-08-20 11:43:25 -0400490static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200491{
Dan Williams954ee162007-08-20 11:43:25 -0400492 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200493}
494
Dan Williams954ee162007-08-20 11:43:25 -0400495static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200496{
Dan Williams954ee162007-08-20 11:43:25 -0400497 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200498}
499
500static void if_usb_receive_fwload(struct urb *urb)
501{
502 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200503 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400504 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200505 struct fwsyncheader *syncfwheader;
506 struct bootcmdrespStr bootcmdresp;
507
508 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400509 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510 "URB status is failed during fw load\n");
511 kfree_skb(skb);
512 return;
513 }
514
David Woodhousec9cd6f92007-12-11 13:15:25 -0500515 if (cardp->fwdnldover) {
516 __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
517
518 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
519 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
520 lbs_pr_info("Firmware ready event received\n");
521 wake_up(&cardp->fw_wq);
522 } else {
523 lbs_deb_usb("Waiting for confirmation; got %x %x\n", le32_to_cpu(tmp[0]),
524 le32_to_cpu(tmp[1]));
525 if_usb_submit_rx_urb_fwload(cardp);
526 }
527 kfree_skb(skb);
528 return;
529 }
David Woodhousec8ba39d2007-12-10 18:53:34 -0500530 if (cardp->bootcmdresp <= 0) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200531 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
532 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400533 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200534 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400535 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200536 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400537 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538 "Received valid boot command response\n");
539 return;
540 }
David Woodhouse981f1872007-05-25 23:36:54 -0400541 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000542 if (bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_REQUEST) ||
543 bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_DATA) ||
544 bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_INDICATION)) {
David Woodhousec9cd6f92007-12-11 13:15:25 -0500545 if (!cardp->bootcmdresp)
546 lbs_pr_info("Firmware already seems alive; resetting\n");
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000547 cardp->bootcmdresp = -1;
548 } else {
549 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
550 le32_to_cpu(bootcmdresp.u32magicnumber));
551 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200552 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
553 lbs_pr_info(
554 "boot cmd response cmd_tag error (%d)\n",
555 bootcmdresp.u8cmd_tag);
556 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
557 lbs_pr_info(
558 "boot cmd response result error (%d)\n",
559 bootcmdresp.u8result);
560 } else {
561 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400562 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200563 "Received valid boot command response\n");
564 }
565 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400566 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200567 return;
568 }
569
570 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
571 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400572 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200573 kfree_skb(skb);
574 return;
575 }
576
577 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
578 sizeof(struct fwsyncheader));
579
580 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400581 /*
582 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200583 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400584 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200585 "FW received Blk seqnum = %d\n",
586 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400587 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200588 cardp->CRC_OK = 1;
589 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400590 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200591 "FW received Blk with CRC error\n");
592 cardp->CRC_OK = 0;
593 }
594
595 kfree_skb(skb);
596
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500597 /* reschedule timer for 200ms hence */
598 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
599
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200600 if (cardp->fwfinalblk) {
601 cardp->fwdnldover = 1;
602 goto exit;
603 }
604
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500605 if_usb_send_fw_pkt(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200606
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500607 exit:
David Woodhousec9cd6f92007-12-11 13:15:25 -0500608 if_usb_submit_rx_urb_fwload(cardp);
609
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200610 kfree(syncfwheader);
611
612 return;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200613}
614
615#define MRVDRV_MIN_PKT_LEN 30
616
617static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
618 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100619 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200620{
621 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
622 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400623 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200624 "Packet length is Invalid\n");
625 kfree_skb(skb);
626 return;
627 }
628
629 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
630 skb_put(skb, recvlength);
631 skb_pull(skb, MESSAGE_HEADER_LEN);
Holger Schurig10078322007-11-15 18:05:47 -0500632 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400633 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634}
635
636static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
637 struct sk_buff *skb,
638 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100639 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200640{
641 u8 *cmdbuf;
Dan Williamsddac4522007-12-11 13:49:39 -0500642 if (recvlength > LBS_CMD_BUFFER_SIZE) {
Holger Schurig9012b282007-05-25 11:27:16 -0400643 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200644 "The receive buffer is too large\n");
645 kfree_skb(skb);
646 return;
647 }
648
649 if (!in_interrupt())
650 BUG();
651
David Woodhouseaa21c002007-12-08 20:04:36 +0000652 spin_lock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200653 /* take care of cur_cmd = NULL case by reading the
654 * data to clear the interrupt */
David Woodhouseaa21c002007-12-08 20:04:36 +0000655 if (!priv->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400656 cmdbuf = priv->upld_buf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000657 priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200658 } else
Dan Williamsddac4522007-12-11 13:49:39 -0500659 cmdbuf = (u8 *) priv->cur_cmd->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200660
Holger Schurigc95c7f92007-08-02 11:49:45 -0400661 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400662 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400664 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200665
666 kfree_skb(skb);
David Woodhouse4f679492007-12-10 14:58:37 -0500667 lbs_interrupt(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000668 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200669
Holger Schurig9012b282007-05-25 11:27:16 -0400670 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200671 "Wake up main thread to handle cmd response\n");
672
673 return;
674}
675
676/**
677 * @brief This function reads of the packet into the upload buff,
678 * wake up the main thread and initialise the Rx callack.
679 *
680 * @param urb pointer to struct urb
681 * @return N/A
682 */
683static void if_usb_receive(struct urb *urb)
684{
685 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200686 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400687 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
Holger Schurig69f90322007-11-23 15:43:44 +0100688 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689
690 int recvlength = urb->actual_length;
691 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400692 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200693
Holger Schurig9012b282007-05-25 11:27:16 -0400694 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200695
696 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400697 __le32 tmp;
698
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400700 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200701 "URB status is failed\n");
702 kfree_skb(skb);
703 goto setup_for_next;
704 }
705
706 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400707 memcpy(&tmp, recvbuff, sizeof(u32));
708 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400709 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400710 "Recv length = 0x%x, Recv type = 0x%X\n",
711 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000712 } else if (urb->status) {
713 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200714 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000715 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200717 switch (recvtype) {
718 case CMD_TYPE_DATA:
719 process_cmdtypedata(recvlength, skb, cardp, priv);
720 break;
721
722 case CMD_TYPE_REQUEST:
723 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
724 break;
725
726 case CMD_TYPE_INDICATION:
727 /* Event cause handling */
David Woodhouseaa21c002007-12-08 20:04:36 +0000728 spin_lock(&priv->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400729 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400730 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200731 cardp->usb_event_cause);
732 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500733 lbs_send_tx_feedback(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000734 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200735 break;
736 }
David Woodhouse981f1872007-05-25 23:36:54 -0400737 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400738 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200739 kfree_skb(skb);
David Woodhouse4f679492007-12-10 14:58:37 -0500740 lbs_interrupt(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000741 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200742 goto rx_exit;
743 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400744 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
745 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746 kfree_skb(skb);
747 break;
748 }
749
750setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400751 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200752rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400753 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200754}
755
756/**
757 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100758 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759 * @param type type of data
760 * @param buf pointer to data buffer
761 * @param len number of bytes
762 * @return 0 or -1
763 */
Holger Schurig69f90322007-11-23 15:43:44 +0100764static int if_usb_host_to_card(struct lbs_private *priv,
765 u8 type,
766 u8 *payload,
767 u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200768{
Holger Schurig634b8f42007-05-25 13:05:16 -0400769 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200770
Holger Schurig9012b282007-05-25 11:27:16 -0400771 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
772 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200773
774 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400775 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400776 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200777 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
778 MESSAGE_HEADER_LEN);
779
780 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400781 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400782 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200783 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
784 MESSAGE_HEADER_LEN);
785 }
786
787 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
788
Dan Williams954ee162007-08-20 11:43:25 -0400789 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400790 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200791}
792
David Woodhouseaa21c002007-12-08 20:04:36 +0000793/* called with priv->driver_lock held */
Holger Schurig69f90322007-11-23 15:43:44 +0100794static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200795{
Holger Schurig634b8f42007-05-25 13:05:16 -0400796 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200797
798 *ireg = cardp->usb_int_cause;
799 cardp->usb_int_cause = 0;
800
Holger Schurig9012b282007-05-25 11:27:16 -0400801 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200802
803 return 0;
804}
805
Holger Schurig69f90322007-11-23 15:43:44 +0100806static int if_usb_read_event_cause(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200807{
Holger Schurig634b8f42007-05-25 13:05:16 -0400808 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400809
David Woodhouseaa21c002007-12-08 20:04:36 +0000810 priv->eventcause = cardp->usb_event_cause;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200811 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400812 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200813 return 0;
814}
815
Dan Williams9e22cb62007-08-02 11:14:49 -0400816/**
817 * @brief This function issues Boot command to the Boot2 code
818 * @param ivalue 1:Boot from FW by USB-Download
819 * 2:Boot from FW in EEPROM
820 * @return 0
821 */
Dan Williams954ee162007-08-20 11:43:25 -0400822static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400823{
Dan Williams954ee162007-08-20 11:43:25 -0400824 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400825 int i;
826
827 /* Prepare command */
828 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
829 sbootcmd.u8cmd_tag = ivalue;
830 for (i=0; i<11; i++)
831 sbootcmd.au8dumy[i]=0x00;
832 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
833
834 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400835 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400836
837 return 0;
838}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200839
840
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400841/**
842 * @brief This function checks the validity of Boot2/FW image.
843 *
844 * @param data pointer to image
845 * len image length
846 * @return 0 or -1
847 */
848static int check_fwfile_format(u8 *data, u32 totlen)
849{
850 u32 bincmd, exit;
851 u32 blksize, offset, len;
852 int ret;
853
854 ret = 1;
855 exit = len = 0;
856
857 do {
858 struct fwheader *fwh = (void *)data;
859
860 bincmd = le32_to_cpu(fwh->dnldcmd);
861 blksize = le32_to_cpu(fwh->datalength);
862 switch (bincmd) {
863 case FW_HAS_DATA_TO_RECV:
864 offset = sizeof(struct fwheader) + blksize;
865 data += offset;
866 len += offset;
867 if (len >= totlen)
868 exit = 1;
869 break;
870 case FW_HAS_LAST_BLOCK:
871 exit = 1;
872 ret = 0;
873 break;
874 default:
875 exit = 1;
876 break;
877 }
878 } while (!exit);
879
880 if (ret)
881 lbs_pr_err("firmware file format check FAIL\n");
882 else
883 lbs_deb_fw("firmware file format check PASS\n");
884
885 return ret;
886}
887
888
Dan Williams954ee162007-08-20 11:43:25 -0400889static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400890{
Dan Williams954ee162007-08-20 11:43:25 -0400891 int i = 0;
892 static int reset_count = 10;
893 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400894
Dan Williams954ee162007-08-20 11:43:25 -0400895 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400896
Holger Schurig10078322007-11-15 18:05:47 -0500897 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400898 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400899 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500900 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400901 goto done;
902 }
903
Dan Williams954ee162007-08-20 11:43:25 -0400904 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
905 goto release_fw;
906
907restart:
908 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
909 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
910 ret = -1;
911 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400912 }
913
Dan Williams954ee162007-08-20 11:43:25 -0400914 cardp->bootcmdresp = 0;
915 do {
916 int j = 0;
917 i++;
918 /* Issue Boot command = 1, Boot from Download-FW */
919 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
920 /* wait for command response */
921 do {
922 j++;
923 msleep_interruptible(100);
924 } while (cardp->bootcmdresp == 0 && j < 10);
925 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400926
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000927 if (cardp->bootcmdresp <= 0) {
Dan Williams954ee162007-08-20 11:43:25 -0400928 if (--reset_count >= 0) {
929 if_usb_reset_device(cardp);
930 goto restart;
931 }
932 return -1;
933 }
934
935 i = 0;
936
937 cardp->totalbytes = 0;
938 cardp->fwlastblksent = 0;
939 cardp->CRC_OK = 1;
940 cardp->fwdnldover = 0;
941 cardp->fwseqnum = -1;
942 cardp->totalbytes = 0;
943 cardp->fwfinalblk = 0;
944
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500945 /* Send the first firmware packet... */
946 if_usb_send_fw_pkt(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400947
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500948 /* ... and wait for the process to complete */
949 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
950
951 del_timer_sync(&cardp->fw_timeout);
David Woodhousec9cd6f92007-12-11 13:15:25 -0500952 usb_kill_urb(cardp->rx_urb);
Dan Williams954ee162007-08-20 11:43:25 -0400953
954 if (!cardp->fwdnldover) {
955 lbs_pr_info("failed to load fw, resetting device!\n");
956 if (--reset_count >= 0) {
957 if_usb_reset_device(cardp);
958 goto restart;
959 }
960
961 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
962 ret = -1;
963 goto release_fw;
964 }
965
966release_fw:
967 release_firmware(cardp->fw);
968 cardp->fw = NULL;
969
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400970done:
Dan Williams954ee162007-08-20 11:43:25 -0400971 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400972 return ret;
973}
974
975
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200976#ifdef CONFIG_PM
977static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
978{
979 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100980 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200981
Holger Schurig9012b282007-05-25 11:27:16 -0400982 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200983
David Woodhouseaa21c002007-12-08 20:04:36 +0000984 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200985 return -1;
986
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400987 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
988 /* Mesh autostart must be activated while sleeping
989 * On resume it will go back to the current state
990 */
991 struct cmd_ds_mesh_access mesh_access;
992 memset(&mesh_access, 0, sizeof(mesh_access));
993 mesh_access.data[0] = cpu_to_le32(1);
Holger Schurig10078322007-11-15 18:05:47 -0500994 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400995 CMD_MESH_ACCESS,
996 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
997 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
998 }
999
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001000 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -04001001 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001002
1003 /* Unlink tx & rx urb */
1004 usb_kill_urb(cardp->tx_urb);
1005 usb_kill_urb(cardp->rx_urb);
1006
1007 cardp->rx_urb_recall = 1;
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
1013static int if_usb_resume(struct usb_interface *intf)
1014{
1015 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +01001016 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001017
Holger Schurig9012b282007-05-25 11:27:16 -04001018 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001019
1020 cardp->rx_urb_recall = 0;
1021
David Woodhouse6bc822b2007-12-11 12:53:43 -05001022 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001023
1024 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -04001025 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001026
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -04001027 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
1028 /* Mesh autostart was activated while sleeping
1029 * Disable it if appropriate
1030 */
1031 struct cmd_ds_mesh_access mesh_access;
1032 memset(&mesh_access, 0, sizeof(mesh_access));
1033 mesh_access.data[0] = cpu_to_le32(0);
Holger Schurig10078322007-11-15 18:05:47 -05001034 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -04001035 CMD_MESH_ACCESS,
1036 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
1037 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1038 }
1039
Holger Schurig9012b282007-05-25 11:27:16 -04001040 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001041 return 0;
1042}
1043#else
1044#define if_usb_suspend NULL
1045#define if_usb_resume NULL
1046#endif
1047
1048static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -05001049 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001052 .id_table = if_usb_table,
1053 .suspend = if_usb_suspend,
1054 .resume = if_usb_resume,
1055};
1056
Andres Salomon4fb910f2007-11-20 17:43:45 -05001057static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001058{
Holger Schurig084708b2007-05-25 12:37:58 -04001059 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001060
Holger Schurig084708b2007-05-25 12:37:58 -04001061 lbs_deb_enter(LBS_DEB_MAIN);
1062
Holger Schurig084708b2007-05-25 12:37:58 -04001063 ret = usb_register(&if_usb_driver);
1064
1065 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1066 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001067}
1068
Andres Salomon4fb910f2007-11-20 17:43:45 -05001069static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001070{
Holger Schurig084708b2007-05-25 12:37:58 -04001071 lbs_deb_enter(LBS_DEB_MAIN);
1072
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001073 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001074
1075 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076}
Holger Schurig084708b2007-05-25 12:37:58 -04001077
1078module_init(if_usb_init_module);
1079module_exit(if_usb_exit_module);
1080
1081MODULE_DESCRIPTION("8388 USB WLAN Driver");
1082MODULE_AUTHOR("Marvell International Ltd.");
1083MODULE_LICENSE("GPL");