blob: 6cd6c962937de5d33758b7c02f41a3dc9d92b5b1 [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;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400233
Holger Schurig10078322007-11-15 18:05:47 -0500234 if (lbs_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400235 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400236
Dan Williams954ee162007-08-20 11:43:25 -0400237 cardp->eth_dev = priv->dev;
238
Holger Schurig208fdd22007-05-25 12:17:06 -0400239 priv->hw_host_to_card = if_usb_host_to_card;
240 priv->hw_get_int_status = if_usb_get_int_status;
241 priv->hw_read_event_cause = if_usb_read_event_cause;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000242 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400243
Dan Williams954ee162007-08-20 11:43:25 -0400244 /* Delay 200 ms to waiting for the FW ready */
245 if_usb_submit_rx_urb(cardp);
246 msleep_interruptible(200);
David Woodhouseaa21c002007-12-08 20:04:36 +0000247 priv->fw_ready = 1;
Dan Williams954ee162007-08-20 11:43:25 -0400248
Holger Schurig10078322007-11-15 18:05:47 -0500249 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400250 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400251
David Woodhouse04c80f12007-12-06 12:51:00 +0000252 if_usb_set_boot2_ver(priv);
David Woodhouse2c944042007-12-06 14:41:08 +0000253
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200254 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400255 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200256
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200257 return 0;
258
Dan Williams954ee162007-08-20 11:43:25 -0400259err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500260 lbs_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400261err_add_mesh:
Holger Schurig10078322007-11-15 18:05:47 -0500262 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400263err_prog_firmware:
264 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200265dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400266 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200267
268error:
269 return -ENOMEM;
270}
271
272/**
273 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400274 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200275 * @return N/A
276 */
277static void if_usb_disconnect(struct usb_interface *intf)
278{
279 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100280 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200281
Dan Williams954ee162007-08-20 11:43:25 -0400282 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200283
Dan Williams954ee162007-08-20 11:43:25 -0400284 /* Update Surprise removed to TRUE */
285 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200286
Dan Williams954ee162007-08-20 11:43:25 -0400287 if (priv) {
Dan Williams954ee162007-08-20 11:43:25 -0400288
David Woodhouseaa21c002007-12-08 20:04:36 +0000289 priv->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500290 lbs_stop_card(priv);
291 lbs_remove_mesh(priv);
292 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400293 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200294
Andres Salomonbe13f182007-11-20 17:43:55 -0500295 /* this is (apparently?) necessary for future usage of the device */
296 lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
297 0, 0, NULL);
298
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200299 /* Unlink and free urb */
300 if_usb_free(cardp);
301
302 usb_set_intfdata(intf, NULL);
303 usb_put_dev(interface_to_usbdev(intf));
304
Dan Williams954ee162007-08-20 11:43:25 -0400305 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200306}
307
308/**
309 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100310 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200311 * @return 0
312 */
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500313static int if_usb_send_fw_pkt(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200314{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200315 struct FWData *fwdata;
316 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400317 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200318
319 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
320
321 if (!fwdata)
322 return -1;
323
324 fwheader = &fwdata->fwheader;
325
326 if (!cardp->CRC_OK) {
327 cardp->totalbytes = cardp->fwlastblksent;
328 cardp->fwseqnum = cardp->lastseqnum - 1;
329 }
330
Holger Schurig9012b282007-05-25 11:27:16 -0400331 /*
332 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200333 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400334 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200335
336 memcpy(fwheader, &firmware[cardp->totalbytes],
337 sizeof(struct fwheader));
338
339 cardp->fwlastblksent = cardp->totalbytes;
340 cardp->totalbytes += sizeof(struct fwheader);
341
Holger Schurig9012b282007-05-25 11:27:16 -0400342 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200343 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400344 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200345
Holger Schurig9012b282007-05-25 11:27:16 -0400346 /*
347 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400348 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400349 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200350
351 cardp->fwseqnum = cardp->fwseqnum + 1;
352
David Woodhouse981f1872007-05-25 23:36:54 -0400353 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
354 cardp->lastseqnum = cardp->fwseqnum;
355 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200356
David Woodhouse981f1872007-05-25 23:36:54 -0400357 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400358 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400359 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400360 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200361 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
362 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400363 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200364 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400365 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200366
David Woodhousebb793e22007-05-25 23:38:14 -0400367 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400368 /*
369 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400371 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200372 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400373 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200374 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400375 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200376 cardp->fwfinalblk = 1;
377 }
378
Holger Schurig9012b282007-05-25 11:27:16 -0400379 /*
380 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200381 "The firmware download is done size is %d\n",
382 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400383 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200384
385 kfree(fwdata);
386
387 return 0;
388}
389
Dan Williams954ee162007-08-20 11:43:25 -0400390static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200391{
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000392 struct cmd_ds_command *cmd = (void *)&cardp->bulk_out_buffer[4];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200393 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200394
Holger Schurig3874d0f2007-05-25 12:01:42 -0400395 lbs_deb_enter(LBS_DEB_USB);
396
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000397 *(__le32 *)cardp->bulk_out_buffer = cpu_to_le32(CMD_TYPE_REQUEST);
398
399 cmd->command = cpu_to_le16(CMD_802_11_RESET);
400 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
401 cmd->result = cpu_to_le16(0);
402 cmd->seqnum = cpu_to_le16(0x5a5a);
403 cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
404 usb_tx_block(cardp, cardp->bulk_out_buffer, 4 + S_DS_GEN + sizeof(struct cmd_ds_802_11_reset));
405
David Woodhousec8ba39d2007-12-10 18:53:34 -0500406 msleep(100);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200407 ret = usb_reset_device(cardp->udev);
David Woodhousec8ba39d2007-12-10 18:53:34 -0500408 msleep(100);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400409
410 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
411
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200412 return ret;
413}
414
415/**
416 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100417 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200418 * @param payload pointer to payload data
419 * @param nb data length
420 * @return 0 or -1
421 */
Dan Williams954ee162007-08-20 11:43:25 -0400422static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200423{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200424 int ret = -1;
425
426 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400427 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400428 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200429 goto tx_ret;
430 }
431
432 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
433 usb_sndbulkpipe(cardp->udev,
434 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400435 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200436
437 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
438
439 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
440 /* transfer failed */
David Woodhouse0856e682007-12-07 15:12:26 +0000441 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200442 ret = -1;
443 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400444 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200445 ret = 0;
446 }
447
448tx_ret:
449 return ret;
450}
451
Dan Williams954ee162007-08-20 11:43:25 -0400452static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
453 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200454{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200455 struct sk_buff *skb;
456 struct read_cb_info *rinfo = &cardp->rinfo;
457 int ret = -1;
458
459 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
460 lbs_pr_err("No free skb\n");
461 goto rx_ret;
462 }
463
464 rinfo->skb = skb;
465
466 /* Fill the receive configuration URB and initialise the Rx call back */
467 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
468 usb_rcvbulkpipe(cardp->udev,
469 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400470 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200471 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
472 rinfo);
473
474 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
475
Holger Schurig9012b282007-05-25 11:27:16 -0400476 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200477 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
478 /* handle failure conditions */
David Woodhouse0856e682007-12-07 15:12:26 +0000479 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000480 kfree_skb(skb);
481 rinfo->skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200482 ret = -1;
483 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400484 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200485 ret = 0;
486 }
487
488rx_ret:
489 return ret;
490}
491
Dan Williams954ee162007-08-20 11:43:25 -0400492static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200493{
Dan Williams954ee162007-08-20 11:43:25 -0400494 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200495}
496
Dan Williams954ee162007-08-20 11:43:25 -0400497static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200498{
Dan Williams954ee162007-08-20 11:43:25 -0400499 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500}
501
502static void if_usb_receive_fwload(struct urb *urb)
503{
504 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200505 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400506 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200507 struct fwsyncheader *syncfwheader;
508 struct bootcmdrespStr bootcmdresp;
509
510 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400511 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200512 "URB status is failed during fw load\n");
513 kfree_skb(skb);
514 return;
515 }
516
David Woodhousec8ba39d2007-12-10 18:53:34 -0500517 if (cardp->bootcmdresp <= 0) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200518 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
519 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400520 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200521 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400522 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200523 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400524 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200525 "Received valid boot command response\n");
526 return;
527 }
David Woodhouse981f1872007-05-25 23:36:54 -0400528 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000529 if (bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_REQUEST) ||
530 bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_DATA) ||
531 bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_INDICATION)) {
532 lbs_pr_info("Firmware already seems alive; resetting\n");
533 cardp->bootcmdresp = -1;
534 } else {
535 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
536 le32_to_cpu(bootcmdresp.u32magicnumber));
537 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
539 lbs_pr_info(
540 "boot cmd response cmd_tag error (%d)\n",
541 bootcmdresp.u8cmd_tag);
542 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
543 lbs_pr_info(
544 "boot cmd response result error (%d)\n",
545 bootcmdresp.u8result);
546 } else {
547 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400548 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200549 "Received valid boot command response\n");
550 }
551 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400552 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200553 return;
554 }
555
556 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
557 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400558 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559 kfree_skb(skb);
560 return;
561 }
562
563 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
564 sizeof(struct fwsyncheader));
565
566 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400567 /*
568 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200569 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400570 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200571 "FW received Blk seqnum = %d\n",
572 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400573 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200574 cardp->CRC_OK = 1;
575 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400576 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200577 "FW received Blk with CRC error\n");
578 cardp->CRC_OK = 0;
579 }
580
581 kfree_skb(skb);
582
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500583 /* reschedule timer for 200ms hence */
584 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
585
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200586 if (cardp->fwfinalblk) {
587 cardp->fwdnldover = 1;
588 goto exit;
589 }
590
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500591 if_usb_send_fw_pkt(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200592
Dan Williams954ee162007-08-20 11:43:25 -0400593 if_usb_submit_rx_urb_fwload(cardp);
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500594 exit:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200595 kfree(syncfwheader);
596
597 return;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200598}
599
600#define MRVDRV_MIN_PKT_LEN 30
601
602static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
603 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100604 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200605{
606 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
607 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400608 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609 "Packet length is Invalid\n");
610 kfree_skb(skb);
611 return;
612 }
613
614 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
615 skb_put(skb, recvlength);
616 skb_pull(skb, MESSAGE_HEADER_LEN);
Holger Schurig10078322007-11-15 18:05:47 -0500617 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400618 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200619}
620
621static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
622 struct sk_buff *skb,
623 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100624 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200625{
626 u8 *cmdbuf;
627 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400628 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200629 "The receive buffer is too large\n");
630 kfree_skb(skb);
631 return;
632 }
633
634 if (!in_interrupt())
635 BUG();
636
David Woodhouseaa21c002007-12-08 20:04:36 +0000637 spin_lock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200638 /* take care of cur_cmd = NULL case by reading the
639 * data to clear the interrupt */
David Woodhouseaa21c002007-12-08 20:04:36 +0000640 if (!priv->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400641 cmdbuf = priv->upld_buf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000642 priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200643 } else
David Woodhouseaa21c002007-12-08 20:04:36 +0000644 cmdbuf = priv->cur_cmd->bufvirtualaddr;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200645
Holger Schurigc95c7f92007-08-02 11:49:45 -0400646 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400647 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200648 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400649 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200650
651 kfree_skb(skb);
David Woodhouse4f679492007-12-10 14:58:37 -0500652 lbs_interrupt(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000653 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200654
Holger Schurig9012b282007-05-25 11:27:16 -0400655 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200656 "Wake up main thread to handle cmd response\n");
657
658 return;
659}
660
661/**
662 * @brief This function reads of the packet into the upload buff,
663 * wake up the main thread and initialise the Rx callack.
664 *
665 * @param urb pointer to struct urb
666 * @return N/A
667 */
668static void if_usb_receive(struct urb *urb)
669{
670 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200671 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400672 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
Holger Schurig69f90322007-11-23 15:43:44 +0100673 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200674
675 int recvlength = urb->actual_length;
676 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400677 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200678
Holger Schurig9012b282007-05-25 11:27:16 -0400679 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200680
681 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400682 __le32 tmp;
683
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200684 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400685 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200686 "URB status is failed\n");
687 kfree_skb(skb);
688 goto setup_for_next;
689 }
690
691 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400692 memcpy(&tmp, recvbuff, sizeof(u32));
693 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400694 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400695 "Recv length = 0x%x, Recv type = 0x%X\n",
696 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000697 } else if (urb->status) {
698 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000700 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200701
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200702 switch (recvtype) {
703 case CMD_TYPE_DATA:
704 process_cmdtypedata(recvlength, skb, cardp, priv);
705 break;
706
707 case CMD_TYPE_REQUEST:
708 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
709 break;
710
711 case CMD_TYPE_INDICATION:
712 /* Event cause handling */
David Woodhouseaa21c002007-12-08 20:04:36 +0000713 spin_lock(&priv->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400714 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400715 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716 cardp->usb_event_cause);
717 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500718 lbs_send_tx_feedback(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000719 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200720 break;
721 }
David Woodhouse981f1872007-05-25 23:36:54 -0400722 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400723 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200724 kfree_skb(skb);
David Woodhouse4f679492007-12-10 14:58:37 -0500725 lbs_interrupt(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000726 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200727 goto rx_exit;
728 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400729 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
730 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200731 kfree_skb(skb);
732 break;
733 }
734
735setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400736 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200737rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400738 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200739}
740
741/**
742 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100743 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200744 * @param type type of data
745 * @param buf pointer to data buffer
746 * @param len number of bytes
747 * @return 0 or -1
748 */
Holger Schurig69f90322007-11-23 15:43:44 +0100749static int if_usb_host_to_card(struct lbs_private *priv,
750 u8 type,
751 u8 *payload,
752 u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753{
Holger Schurig634b8f42007-05-25 13:05:16 -0400754 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200755
Holger Schurig9012b282007-05-25 11:27:16 -0400756 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
757 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200758
759 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400760 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400761 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200762 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
763 MESSAGE_HEADER_LEN);
764
765 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400766 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400767 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200768 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
769 MESSAGE_HEADER_LEN);
770 }
771
772 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
773
Dan Williams954ee162007-08-20 11:43:25 -0400774 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400775 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200776}
777
David Woodhouseaa21c002007-12-08 20:04:36 +0000778/* called with priv->driver_lock held */
Holger Schurig69f90322007-11-23 15:43:44 +0100779static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200780{
Holger Schurig634b8f42007-05-25 13:05:16 -0400781 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200782
783 *ireg = cardp->usb_int_cause;
784 cardp->usb_int_cause = 0;
785
Holger Schurig9012b282007-05-25 11:27:16 -0400786 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200787
788 return 0;
789}
790
Holger Schurig69f90322007-11-23 15:43:44 +0100791static int if_usb_read_event_cause(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200792{
Holger Schurig634b8f42007-05-25 13:05:16 -0400793 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400794
David Woodhouseaa21c002007-12-08 20:04:36 +0000795 priv->eventcause = cardp->usb_event_cause;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200796 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400797 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200798 return 0;
799}
800
Dan Williams9e22cb62007-08-02 11:14:49 -0400801/**
802 * @brief This function issues Boot command to the Boot2 code
803 * @param ivalue 1:Boot from FW by USB-Download
804 * 2:Boot from FW in EEPROM
805 * @return 0
806 */
Dan Williams954ee162007-08-20 11:43:25 -0400807static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400808{
Dan Williams954ee162007-08-20 11:43:25 -0400809 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400810 int i;
811
812 /* Prepare command */
813 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
814 sbootcmd.u8cmd_tag = ivalue;
815 for (i=0; i<11; i++)
816 sbootcmd.au8dumy[i]=0x00;
817 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
818
819 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400820 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400821
822 return 0;
823}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200824
825
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400826/**
827 * @brief This function checks the validity of Boot2/FW image.
828 *
829 * @param data pointer to image
830 * len image length
831 * @return 0 or -1
832 */
833static int check_fwfile_format(u8 *data, u32 totlen)
834{
835 u32 bincmd, exit;
836 u32 blksize, offset, len;
837 int ret;
838
839 ret = 1;
840 exit = len = 0;
841
842 do {
843 struct fwheader *fwh = (void *)data;
844
845 bincmd = le32_to_cpu(fwh->dnldcmd);
846 blksize = le32_to_cpu(fwh->datalength);
847 switch (bincmd) {
848 case FW_HAS_DATA_TO_RECV:
849 offset = sizeof(struct fwheader) + blksize;
850 data += offset;
851 len += offset;
852 if (len >= totlen)
853 exit = 1;
854 break;
855 case FW_HAS_LAST_BLOCK:
856 exit = 1;
857 ret = 0;
858 break;
859 default:
860 exit = 1;
861 break;
862 }
863 } while (!exit);
864
865 if (ret)
866 lbs_pr_err("firmware file format check FAIL\n");
867 else
868 lbs_deb_fw("firmware file format check PASS\n");
869
870 return ret;
871}
872
873
Dan Williams954ee162007-08-20 11:43:25 -0400874static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400875{
Dan Williams954ee162007-08-20 11:43:25 -0400876 int i = 0;
877 static int reset_count = 10;
878 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400879
Dan Williams954ee162007-08-20 11:43:25 -0400880 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400881
Holger Schurig10078322007-11-15 18:05:47 -0500882 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400883 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400884 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500885 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400886 goto done;
887 }
888
Dan Williams954ee162007-08-20 11:43:25 -0400889 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
890 goto release_fw;
891
892restart:
893 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
894 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
895 ret = -1;
896 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400897 }
898
Dan Williams954ee162007-08-20 11:43:25 -0400899 cardp->bootcmdresp = 0;
900 do {
901 int j = 0;
902 i++;
903 /* Issue Boot command = 1, Boot from Download-FW */
904 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
905 /* wait for command response */
906 do {
907 j++;
908 msleep_interruptible(100);
909 } while (cardp->bootcmdresp == 0 && j < 10);
910 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400911
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000912 if (cardp->bootcmdresp <= 0) {
Dan Williams954ee162007-08-20 11:43:25 -0400913 if (--reset_count >= 0) {
914 if_usb_reset_device(cardp);
915 goto restart;
916 }
917 return -1;
918 }
919
920 i = 0;
921
922 cardp->totalbytes = 0;
923 cardp->fwlastblksent = 0;
924 cardp->CRC_OK = 1;
925 cardp->fwdnldover = 0;
926 cardp->fwseqnum = -1;
927 cardp->totalbytes = 0;
928 cardp->fwfinalblk = 0;
929
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500930 /* Send the first firmware packet... */
931 if_usb_send_fw_pkt(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400932
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500933 /* ... and wait for the process to complete */
934 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
935
936 del_timer_sync(&cardp->fw_timeout);
Dan Williams954ee162007-08-20 11:43:25 -0400937
938 if (!cardp->fwdnldover) {
939 lbs_pr_info("failed to load fw, resetting device!\n");
940 if (--reset_count >= 0) {
941 if_usb_reset_device(cardp);
942 goto restart;
943 }
944
945 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
946 ret = -1;
947 goto release_fw;
948 }
949
950release_fw:
951 release_firmware(cardp->fw);
952 cardp->fw = NULL;
953
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400954done:
Dan Williams954ee162007-08-20 11:43:25 -0400955 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400956 return ret;
957}
958
959
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200960#ifdef CONFIG_PM
961static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
962{
963 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100964 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200965
Holger Schurig9012b282007-05-25 11:27:16 -0400966 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200967
David Woodhouseaa21c002007-12-08 20:04:36 +0000968 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200969 return -1;
970
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400971 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
972 /* Mesh autostart must be activated while sleeping
973 * On resume it will go back to the current state
974 */
975 struct cmd_ds_mesh_access mesh_access;
976 memset(&mesh_access, 0, sizeof(mesh_access));
977 mesh_access.data[0] = cpu_to_le32(1);
Holger Schurig10078322007-11-15 18:05:47 -0500978 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400979 CMD_MESH_ACCESS,
980 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
981 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
982 }
983
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200984 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400985 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200986
987 /* Unlink tx & rx urb */
988 usb_kill_urb(cardp->tx_urb);
989 usb_kill_urb(cardp->rx_urb);
990
991 cardp->rx_urb_recall = 1;
992
Holger Schurig9012b282007-05-25 11:27:16 -0400993 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200994 return 0;
995}
996
997static int if_usb_resume(struct usb_interface *intf)
998{
999 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +01001000 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001001
Holger Schurig9012b282007-05-25 11:27:16 -04001002 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001003
1004 cardp->rx_urb_recall = 0;
1005
David Woodhouse6bc822b2007-12-11 12:53:43 -05001006 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001007
1008 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -04001009 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001010
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -04001011 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
1012 /* Mesh autostart was activated while sleeping
1013 * Disable it if appropriate
1014 */
1015 struct cmd_ds_mesh_access mesh_access;
1016 memset(&mesh_access, 0, sizeof(mesh_access));
1017 mesh_access.data[0] = cpu_to_le32(0);
Holger Schurig10078322007-11-15 18:05:47 -05001018 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -04001019 CMD_MESH_ACCESS,
1020 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
1021 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1022 }
1023
Holger Schurig9012b282007-05-25 11:27:16 -04001024 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001025 return 0;
1026}
1027#else
1028#define if_usb_suspend NULL
1029#define if_usb_resume NULL
1030#endif
1031
1032static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -05001033 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001034 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001035 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001036 .id_table = if_usb_table,
1037 .suspend = if_usb_suspend,
1038 .resume = if_usb_resume,
1039};
1040
Andres Salomon4fb910f2007-11-20 17:43:45 -05001041static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001042{
Holger Schurig084708b2007-05-25 12:37:58 -04001043 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001044
Holger Schurig084708b2007-05-25 12:37:58 -04001045 lbs_deb_enter(LBS_DEB_MAIN);
1046
Holger Schurig084708b2007-05-25 12:37:58 -04001047 ret = usb_register(&if_usb_driver);
1048
1049 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1050 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051}
1052
Andres Salomon4fb910f2007-11-20 17:43:45 -05001053static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001054{
Holger Schurig084708b2007-05-25 12:37:58 -04001055 lbs_deb_enter(LBS_DEB_MAIN);
1056
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001057 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001058
1059 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001060}
Holger Schurig084708b2007-05-25 12:37:58 -04001061
1062module_init(if_usb_init_module);
1063module_exit(if_usb_exit_module);
1064
1065MODULE_DESCRIPTION("8388 USB WLAN Driver");
1066MODULE_AUTHOR("Marvell International Ltd.");
1067MODULE_LICENSE("GPL");