blob: a17371fe2fe400d5a15ec7493b94b45540b6123e [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/**
2 * This file contains functions used in USB interface module.
3 */
4#include <linux/delay.h>
Holger Schurig084708b2007-05-25 12:37:58 -04005#include <linux/moduleparam.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02006#include <linux/firmware.h>
7#include <linux/netdevice.h>
8#include <linux/usb.h>
9
Holger Schurigec3eef22007-05-25 12:49:10 -040010#define DRV_NAME "usb8xxx"
11
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020012#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020013#include "decl.h"
14#include "defs.h"
15#include "dev.h"
16#include "if_usb.h"
17
18#define MESSAGE_HEADER_LEN 4
19
Andres Salomon82209ad2007-11-20 17:43:32 -050020static char *lbs_fw_name = "usb8388.bin";
Holger Schurig10078322007-11-15 18:05:47 -050021module_param_named(fw_name, lbs_fw_name, charp, 0644);
Holger Schurig084708b2007-05-25 12:37:58 -040022
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020023static struct usb_device_id if_usb_table[] = {
24 /* Enter the device signature inside */
Holger Schurig66fcc552007-05-25 00:11:58 -040025 { USB_DEVICE(0x1286, 0x2001) },
26 { USB_DEVICE(0x05a3, 0x8388) },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020027 {} /* Terminating entry */
28};
29
30MODULE_DEVICE_TABLE(usb, if_usb_table);
31
32static void if_usb_receive(struct urb *urb);
33static void if_usb_receive_fwload(struct urb *urb);
Dan Williams954ee162007-08-20 11:43:25 -040034static int if_usb_prog_firmware(struct usb_card_rec *cardp);
Holger Schurig69f90322007-11-23 15:43:44 +010035static int if_usb_host_to_card(struct lbs_private *priv,
36 u8 type,
37 u8 *payload,
38 u16 nb);
39static int if_usb_get_int_status(struct lbs_private *priv, u8 *);
40static int if_usb_read_event_cause(struct lbs_private *);
Dan Williams954ee162007-08-20 11:43:25 -040041static int usb_tx_block(struct usb_card_rec *cardp, u8 *payload, u16 nb);
Holger Schurigac558ca2007-08-02 11:49:06 -040042static void if_usb_free(struct usb_card_rec *cardp);
Dan Williams954ee162007-08-20 11:43:25 -040043static int if_usb_submit_rx_urb(struct usb_card_rec *cardp);
44static int if_usb_reset_device(struct usb_card_rec *cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020045
46/**
47 * @brief call back function to handle the status of the URB
48 * @param urb pointer to urb structure
49 * @return N/A
50 */
51static void if_usb_write_bulk_callback(struct urb *urb)
52{
Dan Williams954ee162007-08-20 11:43:25 -040053 struct usb_card_rec *cardp = (struct usb_card_rec *) urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020054
55 /* handle the transmission complete validations */
56
Dan Williams954ee162007-08-20 11:43:25 -040057 if (urb->status == 0) {
Holger Schurig69f90322007-11-23 15:43:44 +010058 struct lbs_private *priv = cardp->priv;
Dan Williams954ee162007-08-20 11:43:25 -040059
Holger Schurig9012b282007-05-25 11:27:16 -040060 /*
61 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
62 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020063 urb->actual_length);
Holger Schurig9012b282007-05-25 11:27:16 -040064 */
Dan Williams954ee162007-08-20 11:43:25 -040065
66 /* Used for both firmware TX and regular TX. priv isn't
67 * valid at firmware load time.
68 */
David Woodhousee775ed72007-12-06 14:36:11 +000069 if (priv)
70 lbs_host_to_card_done(priv);
Dan Williams954ee162007-08-20 11:43:25 -040071 } else {
72 /* print the failure status number for debug */
73 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020074 }
75
76 return;
77}
78
79/**
80 * @brief free tx/rx urb, skb and rx buffer
81 * @param cardp pointer usb_card_rec
82 * @return N/A
83 */
Holger Schurigac558ca2007-08-02 11:49:06 -040084static void if_usb_free(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020085{
Holger Schurig9012b282007-05-25 11:27:16 -040086 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020087
88 /* Unlink tx & rx urb */
89 usb_kill_urb(cardp->tx_urb);
90 usb_kill_urb(cardp->rx_urb);
91
92 usb_free_urb(cardp->tx_urb);
93 cardp->tx_urb = NULL;
94
95 usb_free_urb(cardp->rx_urb);
96 cardp->rx_urb = NULL;
97
98 kfree(cardp->bulk_out_buffer);
99 cardp->bulk_out_buffer = NULL;
100
Holger Schurig9012b282007-05-25 11:27:16 -0400101 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200102}
103
David Woodhouse04c80f12007-12-06 12:51:00 +0000104static void if_usb_set_boot2_ver(struct lbs_private *priv)
105{
106 struct cmd_ds_set_boot2_ver b2_cmd;
107 int rsp_len = sizeof(b2_cmd);
108
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
112 if (lbs_cmd(priv, CMD_SET_BOOT2_VER, &b2_cmd, sizeof(b2_cmd),
113 &b2_cmd, &rsp_len)) {
114 lbs_deb_usb("Setting boot2 version failed\n");
115 }
116}
117
118
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200119/**
120 * @brief sets the configuration values
121 * @param ifnum interface number
122 * @param id pointer to usb_device_id
123 * @return 0 on success, error code on failure
124 */
125static int if_usb_probe(struct usb_interface *intf,
126 const struct usb_device_id *id)
127{
128 struct usb_device *udev;
129 struct usb_host_interface *iface_desc;
130 struct usb_endpoint_descriptor *endpoint;
Holger Schurig69f90322007-11-23 15:43:44 +0100131 struct lbs_private *priv;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400132 struct usb_card_rec *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200133 int i;
134
135 udev = interface_to_usbdev(intf);
136
Holger Schurig435a1ac2007-05-25 12:41:52 -0400137 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
138 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200139 lbs_pr_err("Out of memory allocating private data.\n");
140 goto error;
141 }
142
Holger Schurig435a1ac2007-05-25 12:41:52 -0400143 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200144 iface_desc = intf->cur_altsetting;
145
Holger Schurig9012b282007-05-25 11:27:16 -0400146 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200147 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400148 le16_to_cpu(udev->descriptor.bcdUSB),
149 udev->descriptor.bDeviceClass,
150 udev->descriptor.bDeviceSubClass,
151 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200152
153 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
154 endpoint = &iface_desc->endpoint[i].desc;
155 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
156 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
157 USB_ENDPOINT_XFER_BULK)) {
158 /* we found a bulk in endpoint */
Holger Schurig9012b282007-05-25 11:27:16 -0400159 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400160 le16_to_cpu(endpoint->wMaxPacketSize));
161 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400162 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200163 "Rx URB allocation failed\n");
164 goto dealloc;
165 }
Holger Schurig435a1ac2007-05-25 12:41:52 -0400166 cardp->rx_urb_recall = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167
Holger Schurig435a1ac2007-05-25 12:41:52 -0400168 cardp->bulk_in_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400169 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400170 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200171 (endpoint->
172 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400173 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200174 endpoint->bEndpointAddress);
175 }
176
177 if (((endpoint->
178 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
179 USB_DIR_OUT)
180 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
181 USB_ENDPOINT_XFER_BULK)) {
182 /* We found bulk out endpoint */
David Woodhouse981f1872007-05-25 23:36:54 -0400183 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400184 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200185 "Tx URB allocation failed\n");
186 goto dealloc;
187 }
188
Holger Schurig435a1ac2007-05-25 12:41:52 -0400189 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400190 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400191 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400192 "Bulk out size is %d\n",
193 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400194 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200195 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400196 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200197 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400198 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200199 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
200 GFP_KERNEL);
201
Holger Schurig435a1ac2007-05-25 12:41:52 -0400202 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400203 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200204 "Could not allocate buffer\n");
205 goto dealloc;
206 }
207 }
208 }
209
Dan Williams954ee162007-08-20 11:43:25 -0400210 /* Upload firmware */
211 cardp->rinfo.cardp = cardp;
212 if (if_usb_prog_firmware(cardp)) {
213 lbs_deb_usbd(&udev->dev, "FW upload failed");
214 goto err_prog_firmware;
215 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400216
Holger Schurig10078322007-11-15 18:05:47 -0500217 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400218 goto err_prog_firmware;
219
220 cardp->priv = priv;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400221
Holger Schurig10078322007-11-15 18:05:47 -0500222 if (lbs_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400223 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400224
Dan Williams954ee162007-08-20 11:43:25 -0400225 cardp->eth_dev = priv->dev;
226
Holger Schurig208fdd22007-05-25 12:17:06 -0400227 priv->hw_host_to_card = if_usb_host_to_card;
228 priv->hw_get_int_status = if_usb_get_int_status;
229 priv->hw_read_event_cause = if_usb_read_event_cause;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000230 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400231
Dan Williams954ee162007-08-20 11:43:25 -0400232 /* Delay 200 ms to waiting for the FW ready */
233 if_usb_submit_rx_urb(cardp);
234 msleep_interruptible(200);
235 priv->adapter->fw_ready = 1;
236
Holger Schurig10078322007-11-15 18:05:47 -0500237 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400238 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400239
David Woodhouse04c80f12007-12-06 12:51:00 +0000240 if_usb_set_boot2_ver(priv);
David Woodhouse2c944042007-12-06 14:41:08 +0000241
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200242 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400243 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200244
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200245 return 0;
246
Dan Williams954ee162007-08-20 11:43:25 -0400247err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500248 lbs_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400249err_add_mesh:
Holger Schurig10078322007-11-15 18:05:47 -0500250 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400251err_prog_firmware:
252 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200253dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400254 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200255
256error:
257 return -ENOMEM;
258}
259
260/**
261 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400262 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200263 * @return N/A
264 */
265static void if_usb_disconnect(struct usb_interface *intf)
266{
267 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100268 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200269
Dan Williams954ee162007-08-20 11:43:25 -0400270 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271
Dan Williams954ee162007-08-20 11:43:25 -0400272 /* Update Surprise removed to TRUE */
273 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200274
Dan Williams954ee162007-08-20 11:43:25 -0400275 if (priv) {
Holger Schurig69f90322007-11-23 15:43:44 +0100276 struct lbs_adapter *adapter = priv->adapter;
Dan Williams954ee162007-08-20 11:43:25 -0400277
278 adapter->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500279 lbs_stop_card(priv);
280 lbs_remove_mesh(priv);
281 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400282 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200283
Andres Salomonbe13f182007-11-20 17:43:55 -0500284 /* this is (apparently?) necessary for future usage of the device */
285 lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
286 0, 0, NULL);
287
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200288 /* Unlink and free urb */
289 if_usb_free(cardp);
290
291 usb_set_intfdata(intf, NULL);
292 usb_put_dev(interface_to_usbdev(intf));
293
Dan Williams954ee162007-08-20 11:43:25 -0400294 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200295}
296
297/**
298 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100299 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200300 * @return 0
301 */
Dan Williams954ee162007-08-20 11:43:25 -0400302static int if_prog_firmware(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200303{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200304 struct FWData *fwdata;
305 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400306 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200307
308 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
309
310 if (!fwdata)
311 return -1;
312
313 fwheader = &fwdata->fwheader;
314
315 if (!cardp->CRC_OK) {
316 cardp->totalbytes = cardp->fwlastblksent;
317 cardp->fwseqnum = cardp->lastseqnum - 1;
318 }
319
Holger Schurig9012b282007-05-25 11:27:16 -0400320 /*
321 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200322 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400323 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200324
325 memcpy(fwheader, &firmware[cardp->totalbytes],
326 sizeof(struct fwheader));
327
328 cardp->fwlastblksent = cardp->totalbytes;
329 cardp->totalbytes += sizeof(struct fwheader);
330
Holger Schurig9012b282007-05-25 11:27:16 -0400331 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200332 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400333 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200334
Holger Schurig9012b282007-05-25 11:27:16 -0400335 /*
336 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400337 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400338 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200339
340 cardp->fwseqnum = cardp->fwseqnum + 1;
341
David Woodhouse981f1872007-05-25 23:36:54 -0400342 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
343 cardp->lastseqnum = cardp->fwseqnum;
344 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200345
David Woodhouse981f1872007-05-25 23:36:54 -0400346 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400347 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400348 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400349 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200350 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
351 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400352 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200353 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400354 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200355
David Woodhousebb793e22007-05-25 23:38:14 -0400356 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400357 /*
358 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200359 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400360 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200361 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400362 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200363 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400364 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200365 cardp->fwfinalblk = 1;
366 }
367
Holger Schurig9012b282007-05-25 11:27:16 -0400368 /*
369 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370 "The firmware download is done size is %d\n",
371 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400372 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200373
374 kfree(fwdata);
375
376 return 0;
377}
378
Dan Williams954ee162007-08-20 11:43:25 -0400379static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200380{
381 int ret;
Holger Schurig69f90322007-11-23 15:43:44 +0100382 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200383
Holger Schurig3874d0f2007-05-25 12:01:42 -0400384 lbs_deb_enter(LBS_DEB_USB);
385
Dan Williamseedc2a32007-08-02 11:36:22 -0400386 /* Try a USB port reset first, if that fails send the reset
387 * command to the firmware.
388 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200389 ret = usb_reset_device(cardp->udev);
Dan Williams954ee162007-08-20 11:43:25 -0400390 if (!ret && priv) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200391 msleep(10);
Holger Schurig10078322007-11-15 18:05:47 -0500392 ret = lbs_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200393 msleep(10);
394 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400395
396 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
397
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200398 return ret;
399}
400
401/**
402 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100403 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200404 * @param payload pointer to payload data
405 * @param nb data length
406 * @return 0 or -1
407 */
Dan Williams954ee162007-08-20 11:43:25 -0400408static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200409{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200410 int ret = -1;
411
412 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400413 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400414 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200415 goto tx_ret;
416 }
417
418 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
419 usb_sndbulkpipe(cardp->udev,
420 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400421 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200422
423 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
424
425 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
426 /* transfer failed */
David Woodhouse0856e682007-12-07 15:12:26 +0000427 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200428 ret = -1;
429 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400430 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200431 ret = 0;
432 }
433
434tx_ret:
435 return ret;
436}
437
Dan Williams954ee162007-08-20 11:43:25 -0400438static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
439 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200440{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200441 struct sk_buff *skb;
442 struct read_cb_info *rinfo = &cardp->rinfo;
443 int ret = -1;
444
445 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
446 lbs_pr_err("No free skb\n");
447 goto rx_ret;
448 }
449
450 rinfo->skb = skb;
451
452 /* Fill the receive configuration URB and initialise the Rx call back */
453 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
454 usb_rcvbulkpipe(cardp->udev,
455 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400456 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200457 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
458 rinfo);
459
460 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
461
Holger Schurig9012b282007-05-25 11:27:16 -0400462 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200463 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
464 /* handle failure conditions */
David Woodhouse0856e682007-12-07 15:12:26 +0000465 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000466 kfree_skb(skb);
467 rinfo->skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200468 ret = -1;
469 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400470 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200471 ret = 0;
472 }
473
474rx_ret:
475 return ret;
476}
477
Dan Williams954ee162007-08-20 11:43:25 -0400478static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200479{
Dan Williams954ee162007-08-20 11:43:25 -0400480 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200481}
482
Dan Williams954ee162007-08-20 11:43:25 -0400483static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200484{
Dan Williams954ee162007-08-20 11:43:25 -0400485 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486}
487
488static void if_usb_receive_fwload(struct urb *urb)
489{
490 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200491 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400492 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200493 struct fwsyncheader *syncfwheader;
494 struct bootcmdrespStr bootcmdresp;
495
496 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400497 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200498 "URB status is failed during fw load\n");
499 kfree_skb(skb);
500 return;
501 }
502
503 if (cardp->bootcmdresp == 0) {
504 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
505 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400506 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200507 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400508 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200509 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400510 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200511 "Received valid boot command response\n");
512 return;
513 }
David Woodhouse981f1872007-05-25 23:36:54 -0400514 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200515 lbs_pr_info(
516 "boot cmd response wrong magic number (0x%x)\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400517 le32_to_cpu(bootcmdresp.u32magicnumber));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200518 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
519 lbs_pr_info(
520 "boot cmd response cmd_tag error (%d)\n",
521 bootcmdresp.u8cmd_tag);
522 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
523 lbs_pr_info(
524 "boot cmd response result error (%d)\n",
525 bootcmdresp.u8result);
526 } else {
527 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400528 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200529 "Received valid boot command response\n");
530 }
531 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400532 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533 return;
534 }
535
536 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
537 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400538 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200539 kfree_skb(skb);
540 return;
541 }
542
543 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
544 sizeof(struct fwsyncheader));
545
546 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400547 /*
548 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200549 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400550 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200551 "FW received Blk seqnum = %d\n",
552 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400553 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200554 cardp->CRC_OK = 1;
555 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400556 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200557 "FW received Blk with CRC error\n");
558 cardp->CRC_OK = 0;
559 }
560
561 kfree_skb(skb);
562
563 if (cardp->fwfinalblk) {
564 cardp->fwdnldover = 1;
565 goto exit;
566 }
567
Dan Williams954ee162007-08-20 11:43:25 -0400568 if_prog_firmware(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200569
Dan Williams954ee162007-08-20 11:43:25 -0400570 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200571exit:
572 kfree(syncfwheader);
573
574 return;
575
576}
577
578#define MRVDRV_MIN_PKT_LEN 30
579
580static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
581 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100582 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200583{
584 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
585 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400586 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200587 "Packet length is Invalid\n");
588 kfree_skb(skb);
589 return;
590 }
591
592 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
593 skb_put(skb, recvlength);
594 skb_pull(skb, MESSAGE_HEADER_LEN);
Holger Schurig10078322007-11-15 18:05:47 -0500595 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400596 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200597}
598
599static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
600 struct sk_buff *skb,
601 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100602 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200603{
604 u8 *cmdbuf;
605 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400606 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200607 "The receive buffer is too large\n");
608 kfree_skb(skb);
609 return;
610 }
611
612 if (!in_interrupt())
613 BUG();
614
615 spin_lock(&priv->adapter->driver_lock);
616 /* take care of cur_cmd = NULL case by reading the
617 * data to clear the interrupt */
618 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400619 cmdbuf = priv->upld_buf;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400620 priv->adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200621 } else
622 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
623
Holger Schurigc95c7f92007-08-02 11:49:45 -0400624 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400625 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400627 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200628
629 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500630 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200631 spin_unlock(&priv->adapter->driver_lock);
632
Holger Schurig9012b282007-05-25 11:27:16 -0400633 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634 "Wake up main thread to handle cmd response\n");
635
636 return;
637}
638
639/**
640 * @brief This function reads of the packet into the upload buff,
641 * wake up the main thread and initialise the Rx callack.
642 *
643 * @param urb pointer to struct urb
644 * @return N/A
645 */
646static void if_usb_receive(struct urb *urb)
647{
648 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200649 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400650 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
Holger Schurig69f90322007-11-23 15:43:44 +0100651 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200652
653 int recvlength = urb->actual_length;
654 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400655 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200656
Holger Schurig9012b282007-05-25 11:27:16 -0400657 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200658
659 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400660 __le32 tmp;
661
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200662 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400663 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200664 "URB status is failed\n");
665 kfree_skb(skb);
666 goto setup_for_next;
667 }
668
669 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400670 memcpy(&tmp, recvbuff, sizeof(u32));
671 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400672 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400673 "Recv length = 0x%x, Recv type = 0x%X\n",
674 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000675 } else if (urb->status) {
676 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200677 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000678 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200679
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200680 switch (recvtype) {
681 case CMD_TYPE_DATA:
682 process_cmdtypedata(recvlength, skb, cardp, priv);
683 break;
684
685 case CMD_TYPE_REQUEST:
686 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
687 break;
688
689 case CMD_TYPE_INDICATION:
690 /* Event cause handling */
691 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400692 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400693 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694 cardp->usb_event_cause);
695 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500696 lbs_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400697 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200698 break;
699 }
David Woodhouse981f1872007-05-25 23:36:54 -0400700 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400701 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200702 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500703 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200704 spin_unlock(&priv->adapter->driver_lock);
705 goto rx_exit;
706 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400707 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
708 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200709 kfree_skb(skb);
710 break;
711 }
712
713setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400714 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200715rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400716 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200717}
718
719/**
720 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100721 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722 * @param type type of data
723 * @param buf pointer to data buffer
724 * @param len number of bytes
725 * @return 0 or -1
726 */
Holger Schurig69f90322007-11-23 15:43:44 +0100727static int if_usb_host_to_card(struct lbs_private *priv,
728 u8 type,
729 u8 *payload,
730 u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200731{
Holger Schurig634b8f42007-05-25 13:05:16 -0400732 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200733
Holger Schurig9012b282007-05-25 11:27:16 -0400734 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
735 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200736
737 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400738 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400739 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200740 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
741 MESSAGE_HEADER_LEN);
742
743 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400744 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400745 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
747 MESSAGE_HEADER_LEN);
748 }
749
750 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
751
Dan Williams954ee162007-08-20 11:43:25 -0400752 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400753 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200754}
755
756/* called with adapter->driver_lock held */
Holger Schurig69f90322007-11-23 15:43:44 +0100757static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200758{
Holger Schurig634b8f42007-05-25 13:05:16 -0400759 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200760
761 *ireg = cardp->usb_int_cause;
762 cardp->usb_int_cause = 0;
763
Holger Schurig9012b282007-05-25 11:27:16 -0400764 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200765
766 return 0;
767}
768
Holger Schurig69f90322007-11-23 15:43:44 +0100769static int if_usb_read_event_cause(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200770{
Holger Schurig634b8f42007-05-25 13:05:16 -0400771 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400772
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200773 priv->adapter->eventcause = cardp->usb_event_cause;
774 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400775 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200776 return 0;
777}
778
Dan Williams9e22cb62007-08-02 11:14:49 -0400779/**
780 * @brief This function issues Boot command to the Boot2 code
781 * @param ivalue 1:Boot from FW by USB-Download
782 * 2:Boot from FW in EEPROM
783 * @return 0
784 */
Dan Williams954ee162007-08-20 11:43:25 -0400785static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400786{
Dan Williams954ee162007-08-20 11:43:25 -0400787 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400788 int i;
789
790 /* Prepare command */
791 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
792 sbootcmd.u8cmd_tag = ivalue;
793 for (i=0; i<11; i++)
794 sbootcmd.au8dumy[i]=0x00;
795 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
796
797 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400798 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400799
800 return 0;
801}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200802
803
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400804/**
805 * @brief This function checks the validity of Boot2/FW image.
806 *
807 * @param data pointer to image
808 * len image length
809 * @return 0 or -1
810 */
811static int check_fwfile_format(u8 *data, u32 totlen)
812{
813 u32 bincmd, exit;
814 u32 blksize, offset, len;
815 int ret;
816
817 ret = 1;
818 exit = len = 0;
819
820 do {
821 struct fwheader *fwh = (void *)data;
822
823 bincmd = le32_to_cpu(fwh->dnldcmd);
824 blksize = le32_to_cpu(fwh->datalength);
825 switch (bincmd) {
826 case FW_HAS_DATA_TO_RECV:
827 offset = sizeof(struct fwheader) + blksize;
828 data += offset;
829 len += offset;
830 if (len >= totlen)
831 exit = 1;
832 break;
833 case FW_HAS_LAST_BLOCK:
834 exit = 1;
835 ret = 0;
836 break;
837 default:
838 exit = 1;
839 break;
840 }
841 } while (!exit);
842
843 if (ret)
844 lbs_pr_err("firmware file format check FAIL\n");
845 else
846 lbs_deb_fw("firmware file format check PASS\n");
847
848 return ret;
849}
850
851
Dan Williams954ee162007-08-20 11:43:25 -0400852static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400853{
Dan Williams954ee162007-08-20 11:43:25 -0400854 int i = 0;
855 static int reset_count = 10;
856 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400857
Dan Williams954ee162007-08-20 11:43:25 -0400858 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400859
Holger Schurig10078322007-11-15 18:05:47 -0500860 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400861 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400862 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500863 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400864 goto done;
865 }
866
Dan Williams954ee162007-08-20 11:43:25 -0400867 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
868 goto release_fw;
869
870restart:
871 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
872 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
873 ret = -1;
874 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400875 }
876
Dan Williams954ee162007-08-20 11:43:25 -0400877 cardp->bootcmdresp = 0;
878 do {
879 int j = 0;
880 i++;
881 /* Issue Boot command = 1, Boot from Download-FW */
882 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
883 /* wait for command response */
884 do {
885 j++;
886 msleep_interruptible(100);
887 } while (cardp->bootcmdresp == 0 && j < 10);
888 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400889
Dan Williams954ee162007-08-20 11:43:25 -0400890 if (cardp->bootcmdresp == 0) {
891 if (--reset_count >= 0) {
892 if_usb_reset_device(cardp);
893 goto restart;
894 }
895 return -1;
896 }
897
898 i = 0;
899
900 cardp->totalbytes = 0;
901 cardp->fwlastblksent = 0;
902 cardp->CRC_OK = 1;
903 cardp->fwdnldover = 0;
904 cardp->fwseqnum = -1;
905 cardp->totalbytes = 0;
906 cardp->fwfinalblk = 0;
907
908 if_prog_firmware(cardp);
909
910 do {
911 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
912 i++;
913 msleep_interruptible(100);
914 if (cardp->surprise_removed || i >= 20)
915 break;
916 } while (!cardp->fwdnldover);
917
918 if (!cardp->fwdnldover) {
919 lbs_pr_info("failed to load fw, resetting device!\n");
920 if (--reset_count >= 0) {
921 if_usb_reset_device(cardp);
922 goto restart;
923 }
924
925 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
926 ret = -1;
927 goto release_fw;
928 }
929
930release_fw:
931 release_firmware(cardp->fw);
932 cardp->fw = NULL;
933
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400934done:
Dan Williams954ee162007-08-20 11:43:25 -0400935 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400936 return ret;
937}
938
939
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200940#ifdef CONFIG_PM
941static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
942{
943 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100944 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200945
Holger Schurig9012b282007-05-25 11:27:16 -0400946 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200947
948 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
949 return -1;
950
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400951 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
952 /* Mesh autostart must be activated while sleeping
953 * On resume it will go back to the current state
954 */
955 struct cmd_ds_mesh_access mesh_access;
956 memset(&mesh_access, 0, sizeof(mesh_access));
957 mesh_access.data[0] = cpu_to_le32(1);
Holger Schurig10078322007-11-15 18:05:47 -0500958 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400959 CMD_MESH_ACCESS,
960 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
961 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
962 }
963
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200964 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400965 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200966
967 /* Unlink tx & rx urb */
968 usb_kill_urb(cardp->tx_urb);
969 usb_kill_urb(cardp->rx_urb);
970
971 cardp->rx_urb_recall = 1;
972
Holger Schurig9012b282007-05-25 11:27:16 -0400973 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200974 return 0;
975}
976
977static int if_usb_resume(struct usb_interface *intf)
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
984 cardp->rx_urb_recall = 0;
985
986 if_usb_submit_rx_urb(cardp->priv);
987
988 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400989 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200990
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400991 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
992 /* Mesh autostart was activated while sleeping
993 * Disable it if appropriate
994 */
995 struct cmd_ds_mesh_access mesh_access;
996 memset(&mesh_access, 0, sizeof(mesh_access));
997 mesh_access.data[0] = cpu_to_le32(0);
Holger Schurig10078322007-11-15 18:05:47 -0500998 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400999 CMD_MESH_ACCESS,
1000 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
1001 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1002 }
1003
Holger Schurig9012b282007-05-25 11:27:16 -04001004 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001005 return 0;
1006}
1007#else
1008#define if_usb_suspend NULL
1009#define if_usb_resume NULL
1010#endif
1011
1012static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -05001013 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001014 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001015 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001016 .id_table = if_usb_table,
1017 .suspend = if_usb_suspend,
1018 .resume = if_usb_resume,
1019};
1020
Andres Salomon4fb910f2007-11-20 17:43:45 -05001021static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001022{
Holger Schurig084708b2007-05-25 12:37:58 -04001023 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001024
Holger Schurig084708b2007-05-25 12:37:58 -04001025 lbs_deb_enter(LBS_DEB_MAIN);
1026
Holger Schurig084708b2007-05-25 12:37:58 -04001027 ret = usb_register(&if_usb_driver);
1028
1029 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1030 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001031}
1032
Andres Salomon4fb910f2007-11-20 17:43:45 -05001033static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001034{
Holger Schurig084708b2007-05-25 12:37:58 -04001035 lbs_deb_enter(LBS_DEB_MAIN);
1036
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001037 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001038
1039 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001040}
Holger Schurig084708b2007-05-25 12:37:58 -04001041
1042module_init(if_usb_init_module);
1043module_exit(if_usb_exit_module);
1044
1045MODULE_DESCRIPTION("8388 USB WLAN Driver");
1046MODULE_AUTHOR("Marvell International Ltd.");
1047MODULE_LICENSE("GPL");