blob: 4fce0baa0711e158d02245fb5a65f81628ee1336 [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 */
69 if (priv) {
Holger Schurig69f90322007-11-23 15:43:44 +010070 struct lbs_adapter *adapter = priv->adapter;
Dan Williams954ee162007-08-20 11:43:25 -040071 struct net_device *dev = priv->dev;
72
73 priv->dnld_sent = DNLD_RES_RECEIVED;
74
75 /* Wake main thread if commands are pending */
76 if (!adapter->cur_cmd)
77 wake_up_interruptible(&priv->waitq);
78
Brajesh Dave01d77d82007-11-20 17:44:14 -050079 if (adapter->connect_status == LBS_CONNECTED)
Dan Williams954ee162007-08-20 11:43:25 -040080 netif_wake_queue(dev);
Brajesh Dave01d77d82007-11-20 17:44:14 -050081
82 if (priv->mesh_dev && (adapter->mesh_connect_status == LBS_CONNECTED))
Dan Williams954ee162007-08-20 11:43:25 -040083 netif_wake_queue(priv->mesh_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -040084 }
Dan Williams954ee162007-08-20 11:43:25 -040085 } else {
86 /* print the failure status number for debug */
87 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020088 }
89
90 return;
91}
92
93/**
94 * @brief free tx/rx urb, skb and rx buffer
95 * @param cardp pointer usb_card_rec
96 * @return N/A
97 */
Holger Schurigac558ca2007-08-02 11:49:06 -040098static void if_usb_free(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020099{
Holger Schurig9012b282007-05-25 11:27:16 -0400100 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200101
102 /* Unlink tx & rx urb */
103 usb_kill_urb(cardp->tx_urb);
104 usb_kill_urb(cardp->rx_urb);
105
106 usb_free_urb(cardp->tx_urb);
107 cardp->tx_urb = NULL;
108
109 usb_free_urb(cardp->rx_urb);
110 cardp->rx_urb = NULL;
111
112 kfree(cardp->bulk_out_buffer);
113 cardp->bulk_out_buffer = NULL;
114
Holger Schurig9012b282007-05-25 11:27:16 -0400115 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200116}
117
118/**
119 * @brief sets the configuration values
120 * @param ifnum interface number
121 * @param id pointer to usb_device_id
122 * @return 0 on success, error code on failure
123 */
124static int if_usb_probe(struct usb_interface *intf,
125 const struct usb_device_id *id)
126{
127 struct usb_device *udev;
128 struct usb_host_interface *iface_desc;
129 struct usb_endpoint_descriptor *endpoint;
Holger Schurig69f90322007-11-23 15:43:44 +0100130 struct lbs_private *priv;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400131 struct usb_card_rec *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200132 int i;
133
134 udev = interface_to_usbdev(intf);
135
Holger Schurig435a1ac2007-05-25 12:41:52 -0400136 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
137 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200138 lbs_pr_err("Out of memory allocating private data.\n");
139 goto error;
140 }
141
Holger Schurig435a1ac2007-05-25 12:41:52 -0400142 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200143 iface_desc = intf->cur_altsetting;
144
Holger Schurig9012b282007-05-25 11:27:16 -0400145 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200146 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400147 le16_to_cpu(udev->descriptor.bcdUSB),
148 udev->descriptor.bDeviceClass,
149 udev->descriptor.bDeviceSubClass,
150 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200151
152 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
153 endpoint = &iface_desc->endpoint[i].desc;
154 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
155 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
156 USB_ENDPOINT_XFER_BULK)) {
157 /* we found a bulk in endpoint */
Holger Schurig9012b282007-05-25 11:27:16 -0400158 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400159 le16_to_cpu(endpoint->wMaxPacketSize));
160 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400161 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200162 "Rx URB allocation failed\n");
163 goto dealloc;
164 }
Holger Schurig435a1ac2007-05-25 12:41:52 -0400165 cardp->rx_urb_recall = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200166
Holger Schurig435a1ac2007-05-25 12:41:52 -0400167 cardp->bulk_in_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400168 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400169 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200170 (endpoint->
171 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400172 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200173 endpoint->bEndpointAddress);
174 }
175
176 if (((endpoint->
177 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
178 USB_DIR_OUT)
179 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
180 USB_ENDPOINT_XFER_BULK)) {
181 /* We found bulk out endpoint */
David Woodhouse981f1872007-05-25 23:36:54 -0400182 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400183 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200184 "Tx URB allocation failed\n");
185 goto dealloc;
186 }
187
Holger Schurig435a1ac2007-05-25 12:41:52 -0400188 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400189 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400190 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400191 "Bulk out size is %d\n",
192 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400193 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200194 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400195 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200196 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400197 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200198 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
199 GFP_KERNEL);
200
Holger Schurig435a1ac2007-05-25 12:41:52 -0400201 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400202 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200203 "Could not allocate buffer\n");
204 goto dealloc;
205 }
206 }
207 }
208
Dan Williams954ee162007-08-20 11:43:25 -0400209 /* Upload firmware */
210 cardp->rinfo.cardp = cardp;
211 if (if_usb_prog_firmware(cardp)) {
212 lbs_deb_usbd(&udev->dev, "FW upload failed");
213 goto err_prog_firmware;
214 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400215
Holger Schurig10078322007-11-15 18:05:47 -0500216 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400217 goto err_prog_firmware;
218
219 cardp->priv = priv;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400220
Holger Schurig10078322007-11-15 18:05:47 -0500221 if (lbs_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400222 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400223
Dan Williams954ee162007-08-20 11:43:25 -0400224 cardp->eth_dev = priv->dev;
225
Holger Schurig208fdd22007-05-25 12:17:06 -0400226 priv->hw_host_to_card = if_usb_host_to_card;
227 priv->hw_get_int_status = if_usb_get_int_status;
228 priv->hw_read_event_cause = if_usb_read_event_cause;
Luis Carlos Cobo63f00232007-08-02 13:19:24 -0400229 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400230
Dan Williams954ee162007-08-20 11:43:25 -0400231 /* Delay 200 ms to waiting for the FW ready */
232 if_usb_submit_rx_urb(cardp);
233 msleep_interruptible(200);
234 priv->adapter->fw_ready = 1;
235
Holger Schurig10078322007-11-15 18:05:47 -0500236 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400237 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400238
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200239 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400240 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200241
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200242 return 0;
243
Dan Williams954ee162007-08-20 11:43:25 -0400244err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500245 lbs_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400246err_add_mesh:
Holger Schurig10078322007-11-15 18:05:47 -0500247 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400248err_prog_firmware:
249 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200250dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400251 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200252
253error:
254 return -ENOMEM;
255}
256
257/**
258 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400259 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200260 * @return N/A
261 */
262static void if_usb_disconnect(struct usb_interface *intf)
263{
264 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100265 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200266
Dan Williams954ee162007-08-20 11:43:25 -0400267 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200268
Dan Williams954ee162007-08-20 11:43:25 -0400269 /* Update Surprise removed to TRUE */
270 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271
Dan Williams954ee162007-08-20 11:43:25 -0400272 if (priv) {
Holger Schurig69f90322007-11-23 15:43:44 +0100273 struct lbs_adapter *adapter = priv->adapter;
Dan Williams954ee162007-08-20 11:43:25 -0400274
275 adapter->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500276 lbs_stop_card(priv);
277 lbs_remove_mesh(priv);
278 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400279 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200280
Andres Salomonbe13f182007-11-20 17:43:55 -0500281 /* this is (apparently?) necessary for future usage of the device */
282 lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
283 0, 0, NULL);
284
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200285 /* Unlink and free urb */
286 if_usb_free(cardp);
287
288 usb_set_intfdata(intf, NULL);
289 usb_put_dev(interface_to_usbdev(intf));
290
Dan Williams954ee162007-08-20 11:43:25 -0400291 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200292}
293
294/**
295 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100296 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200297 * @return 0
298 */
Dan Williams954ee162007-08-20 11:43:25 -0400299static int if_prog_firmware(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200300{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200301 struct FWData *fwdata;
302 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400303 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200304
305 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
306
307 if (!fwdata)
308 return -1;
309
310 fwheader = &fwdata->fwheader;
311
312 if (!cardp->CRC_OK) {
313 cardp->totalbytes = cardp->fwlastblksent;
314 cardp->fwseqnum = cardp->lastseqnum - 1;
315 }
316
Holger Schurig9012b282007-05-25 11:27:16 -0400317 /*
318 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200319 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400320 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200321
322 memcpy(fwheader, &firmware[cardp->totalbytes],
323 sizeof(struct fwheader));
324
325 cardp->fwlastblksent = cardp->totalbytes;
326 cardp->totalbytes += sizeof(struct fwheader);
327
Holger Schurig9012b282007-05-25 11:27:16 -0400328 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200329 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400330 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200331
Holger Schurig9012b282007-05-25 11:27:16 -0400332 /*
333 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400334 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400335 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200336
337 cardp->fwseqnum = cardp->fwseqnum + 1;
338
David Woodhouse981f1872007-05-25 23:36:54 -0400339 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
340 cardp->lastseqnum = cardp->fwseqnum;
341 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200342
David Woodhouse981f1872007-05-25 23:36:54 -0400343 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400344 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400345 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400346 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200347 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
348 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400349 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200350 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400351 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200352
David Woodhousebb793e22007-05-25 23:38:14 -0400353 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400354 /*
355 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200356 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400357 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200358 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400359 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200360 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400361 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200362 cardp->fwfinalblk = 1;
363 }
364
Holger Schurig9012b282007-05-25 11:27:16 -0400365 /*
366 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200367 "The firmware download is done size is %d\n",
368 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400369 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370
371 kfree(fwdata);
372
373 return 0;
374}
375
Dan Williams954ee162007-08-20 11:43:25 -0400376static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200377{
378 int ret;
Holger Schurig69f90322007-11-23 15:43:44 +0100379 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200380
Holger Schurig3874d0f2007-05-25 12:01:42 -0400381 lbs_deb_enter(LBS_DEB_USB);
382
Dan Williamseedc2a32007-08-02 11:36:22 -0400383 /* Try a USB port reset first, if that fails send the reset
384 * command to the firmware.
385 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200386 ret = usb_reset_device(cardp->udev);
Dan Williams954ee162007-08-20 11:43:25 -0400387 if (!ret && priv) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200388 msleep(10);
Holger Schurig10078322007-11-15 18:05:47 -0500389 ret = lbs_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200390 msleep(10);
391 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400392
393 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
394
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200395 return ret;
396}
397
398/**
399 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100400 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200401 * @param payload pointer to payload data
402 * @param nb data length
403 * @return 0 or -1
404 */
Dan Williams954ee162007-08-20 11:43:25 -0400405static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200406{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200407 int ret = -1;
408
409 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400410 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400411 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200412 goto tx_ret;
413 }
414
415 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
416 usb_sndbulkpipe(cardp->udev,
417 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400418 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200419
420 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
421
422 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
423 /* transfer failed */
Holger Schurig9012b282007-05-25 11:27:16 -0400424 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200425 ret = -1;
426 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400427 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200428 ret = 0;
429 }
430
431tx_ret:
432 return ret;
433}
434
Dan Williams954ee162007-08-20 11:43:25 -0400435static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
436 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200437{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438 struct sk_buff *skb;
439 struct read_cb_info *rinfo = &cardp->rinfo;
440 int ret = -1;
441
442 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
443 lbs_pr_err("No free skb\n");
444 goto rx_ret;
445 }
446
447 rinfo->skb = skb;
448
449 /* Fill the receive configuration URB and initialise the Rx call back */
450 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
451 usb_rcvbulkpipe(cardp->udev,
452 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400453 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200454 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
455 rinfo);
456
457 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
458
Holger Schurig9012b282007-05-25 11:27:16 -0400459 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200460 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
461 /* handle failure conditions */
Holger Schurig9012b282007-05-25 11:27:16 -0400462 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200463 ret = -1;
464 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400465 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200466 ret = 0;
467 }
468
469rx_ret:
470 return ret;
471}
472
Dan Williams954ee162007-08-20 11:43:25 -0400473static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200474{
Dan Williams954ee162007-08-20 11:43:25 -0400475 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476}
477
Dan Williams954ee162007-08-20 11:43:25 -0400478static int if_usb_submit_rx_urb(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);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200481}
482
483static void if_usb_receive_fwload(struct urb *urb)
484{
485 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400487 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200488 struct fwsyncheader *syncfwheader;
489 struct bootcmdrespStr bootcmdresp;
490
491 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400492 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200493 "URB status is failed during fw load\n");
494 kfree_skb(skb);
495 return;
496 }
497
498 if (cardp->bootcmdresp == 0) {
499 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
500 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400501 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200502 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400503 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200504 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400505 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200506 "Received valid boot command response\n");
507 return;
508 }
David Woodhouse981f1872007-05-25 23:36:54 -0400509 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510 lbs_pr_info(
511 "boot cmd response wrong magic number (0x%x)\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400512 le32_to_cpu(bootcmdresp.u32magicnumber));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
514 lbs_pr_info(
515 "boot cmd response cmd_tag error (%d)\n",
516 bootcmdresp.u8cmd_tag);
517 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
518 lbs_pr_info(
519 "boot cmd response result error (%d)\n",
520 bootcmdresp.u8result);
521 } else {
522 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400523 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200524 "Received valid boot command response\n");
525 }
526 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400527 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200528 return;
529 }
530
531 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
532 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400533 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200534 kfree_skb(skb);
535 return;
536 }
537
538 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
539 sizeof(struct fwsyncheader));
540
541 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400542 /*
543 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200544 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400545 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200546 "FW received Blk seqnum = %d\n",
547 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400548 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200549 cardp->CRC_OK = 1;
550 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400551 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200552 "FW received Blk with CRC error\n");
553 cardp->CRC_OK = 0;
554 }
555
556 kfree_skb(skb);
557
558 if (cardp->fwfinalblk) {
559 cardp->fwdnldover = 1;
560 goto exit;
561 }
562
Dan Williams954ee162007-08-20 11:43:25 -0400563 if_prog_firmware(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200564
Dan Williams954ee162007-08-20 11:43:25 -0400565 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200566exit:
567 kfree(syncfwheader);
568
569 return;
570
571}
572
573#define MRVDRV_MIN_PKT_LEN 30
574
575static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
576 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100577 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200578{
579 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
580 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400581 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200582 "Packet length is Invalid\n");
583 kfree_skb(skb);
584 return;
585 }
586
587 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
588 skb_put(skb, recvlength);
589 skb_pull(skb, MESSAGE_HEADER_LEN);
Holger Schurig10078322007-11-15 18:05:47 -0500590 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400591 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200592}
593
594static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
595 struct sk_buff *skb,
596 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100597 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200598{
599 u8 *cmdbuf;
600 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400601 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200602 "The receive buffer is too large\n");
603 kfree_skb(skb);
604 return;
605 }
606
607 if (!in_interrupt())
608 BUG();
609
610 spin_lock(&priv->adapter->driver_lock);
611 /* take care of cur_cmd = NULL case by reading the
612 * data to clear the interrupt */
613 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400614 cmdbuf = priv->upld_buf;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400615 priv->adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200616 } else
617 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
618
Holger Schurigc95c7f92007-08-02 11:49:45 -0400619 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400620 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200621 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400622 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200623
624 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500625 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626 spin_unlock(&priv->adapter->driver_lock);
627
Holger Schurig9012b282007-05-25 11:27:16 -0400628 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200629 "Wake up main thread to handle cmd response\n");
630
631 return;
632}
633
634/**
635 * @brief This function reads of the packet into the upload buff,
636 * wake up the main thread and initialise the Rx callack.
637 *
638 * @param urb pointer to struct urb
639 * @return N/A
640 */
641static void if_usb_receive(struct urb *urb)
642{
643 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200644 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400645 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
Holger Schurig69f90322007-11-23 15:43:44 +0100646 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200647
648 int recvlength = urb->actual_length;
649 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400650 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200651
Holger Schurig9012b282007-05-25 11:27:16 -0400652 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200653
654 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400655 __le32 tmp;
656
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200657 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400658 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200659 "URB status is failed\n");
660 kfree_skb(skb);
661 goto setup_for_next;
662 }
663
664 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400665 memcpy(&tmp, recvbuff, sizeof(u32));
666 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400667 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400668 "Recv length = 0x%x, Recv type = 0x%X\n",
669 recvlength, recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200670 } else if (urb->status)
671 goto rx_exit;
672
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200673 switch (recvtype) {
674 case CMD_TYPE_DATA:
675 process_cmdtypedata(recvlength, skb, cardp, priv);
676 break;
677
678 case CMD_TYPE_REQUEST:
679 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
680 break;
681
682 case CMD_TYPE_INDICATION:
683 /* Event cause handling */
684 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400685 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400686 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200687 cardp->usb_event_cause);
688 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500689 lbs_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400690 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691 break;
692 }
David Woodhouse981f1872007-05-25 23:36:54 -0400693 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400694 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200695 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500696 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697 spin_unlock(&priv->adapter->driver_lock);
698 goto rx_exit;
699 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400700 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
701 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200702 kfree_skb(skb);
703 break;
704 }
705
706setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400707 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200708rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400709 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710}
711
712/**
713 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100714 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200715 * @param type type of data
716 * @param buf pointer to data buffer
717 * @param len number of bytes
718 * @return 0 or -1
719 */
Holger Schurig69f90322007-11-23 15:43:44 +0100720static int if_usb_host_to_card(struct lbs_private *priv,
721 u8 type,
722 u8 *payload,
723 u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200724{
Holger Schurig634b8f42007-05-25 13:05:16 -0400725 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200726
Holger Schurig9012b282007-05-25 11:27:16 -0400727 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
728 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729
730 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400731 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400732 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200733 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
734 MESSAGE_HEADER_LEN);
735
736 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400737 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400738 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200739 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
740 MESSAGE_HEADER_LEN);
741 }
742
743 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
744
Dan Williams954ee162007-08-20 11:43:25 -0400745 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400746 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200747}
748
749/* called with adapter->driver_lock held */
Holger Schurig69f90322007-11-23 15:43:44 +0100750static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200751{
Holger Schurig634b8f42007-05-25 13:05:16 -0400752 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753
754 *ireg = cardp->usb_int_cause;
755 cardp->usb_int_cause = 0;
756
Holger Schurig9012b282007-05-25 11:27:16 -0400757 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200758
759 return 0;
760}
761
Holger Schurig69f90322007-11-23 15:43:44 +0100762static int if_usb_read_event_cause(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200763{
Holger Schurig634b8f42007-05-25 13:05:16 -0400764 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400765
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200766 priv->adapter->eventcause = cardp->usb_event_cause;
767 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400768 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200769 return 0;
770}
771
Dan Williams9e22cb62007-08-02 11:14:49 -0400772/**
773 * @brief This function issues Boot command to the Boot2 code
774 * @param ivalue 1:Boot from FW by USB-Download
775 * 2:Boot from FW in EEPROM
776 * @return 0
777 */
Dan Williams954ee162007-08-20 11:43:25 -0400778static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400779{
Dan Williams954ee162007-08-20 11:43:25 -0400780 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400781 int i;
782
783 /* Prepare command */
784 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
785 sbootcmd.u8cmd_tag = ivalue;
786 for (i=0; i<11; i++)
787 sbootcmd.au8dumy[i]=0x00;
788 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
789
790 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400791 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400792
793 return 0;
794}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200795
796
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400797/**
798 * @brief This function checks the validity of Boot2/FW image.
799 *
800 * @param data pointer to image
801 * len image length
802 * @return 0 or -1
803 */
804static int check_fwfile_format(u8 *data, u32 totlen)
805{
806 u32 bincmd, exit;
807 u32 blksize, offset, len;
808 int ret;
809
810 ret = 1;
811 exit = len = 0;
812
813 do {
814 struct fwheader *fwh = (void *)data;
815
816 bincmd = le32_to_cpu(fwh->dnldcmd);
817 blksize = le32_to_cpu(fwh->datalength);
818 switch (bincmd) {
819 case FW_HAS_DATA_TO_RECV:
820 offset = sizeof(struct fwheader) + blksize;
821 data += offset;
822 len += offset;
823 if (len >= totlen)
824 exit = 1;
825 break;
826 case FW_HAS_LAST_BLOCK:
827 exit = 1;
828 ret = 0;
829 break;
830 default:
831 exit = 1;
832 break;
833 }
834 } while (!exit);
835
836 if (ret)
837 lbs_pr_err("firmware file format check FAIL\n");
838 else
839 lbs_deb_fw("firmware file format check PASS\n");
840
841 return ret;
842}
843
844
Dan Williams954ee162007-08-20 11:43:25 -0400845static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400846{
Dan Williams954ee162007-08-20 11:43:25 -0400847 int i = 0;
848 static int reset_count = 10;
849 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400850
Dan Williams954ee162007-08-20 11:43:25 -0400851 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400852
Holger Schurig10078322007-11-15 18:05:47 -0500853 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400854 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400855 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500856 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400857 goto done;
858 }
859
Dan Williams954ee162007-08-20 11:43:25 -0400860 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
861 goto release_fw;
862
863restart:
864 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
865 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
866 ret = -1;
867 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400868 }
869
Dan Williams954ee162007-08-20 11:43:25 -0400870 cardp->bootcmdresp = 0;
871 do {
872 int j = 0;
873 i++;
874 /* Issue Boot command = 1, Boot from Download-FW */
875 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
876 /* wait for command response */
877 do {
878 j++;
879 msleep_interruptible(100);
880 } while (cardp->bootcmdresp == 0 && j < 10);
881 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400882
Dan Williams954ee162007-08-20 11:43:25 -0400883 if (cardp->bootcmdresp == 0) {
884 if (--reset_count >= 0) {
885 if_usb_reset_device(cardp);
886 goto restart;
887 }
888 return -1;
889 }
890
891 i = 0;
892
893 cardp->totalbytes = 0;
894 cardp->fwlastblksent = 0;
895 cardp->CRC_OK = 1;
896 cardp->fwdnldover = 0;
897 cardp->fwseqnum = -1;
898 cardp->totalbytes = 0;
899 cardp->fwfinalblk = 0;
900
901 if_prog_firmware(cardp);
902
903 do {
904 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
905 i++;
906 msleep_interruptible(100);
907 if (cardp->surprise_removed || i >= 20)
908 break;
909 } while (!cardp->fwdnldover);
910
911 if (!cardp->fwdnldover) {
912 lbs_pr_info("failed to load fw, resetting device!\n");
913 if (--reset_count >= 0) {
914 if_usb_reset_device(cardp);
915 goto restart;
916 }
917
918 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
919 ret = -1;
920 goto release_fw;
921 }
922
923release_fw:
924 release_firmware(cardp->fw);
925 cardp->fw = NULL;
926
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400927done:
Dan Williams954ee162007-08-20 11:43:25 -0400928 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400929 return ret;
930}
931
932
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200933#ifdef CONFIG_PM
934static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
935{
936 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100937 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200938
Holger Schurig9012b282007-05-25 11:27:16 -0400939 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200940
941 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
942 return -1;
943
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400944 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
945 /* Mesh autostart must be activated while sleeping
946 * On resume it will go back to the current state
947 */
948 struct cmd_ds_mesh_access mesh_access;
949 memset(&mesh_access, 0, sizeof(mesh_access));
950 mesh_access.data[0] = cpu_to_le32(1);
Holger Schurig10078322007-11-15 18:05:47 -0500951 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400952 CMD_MESH_ACCESS,
953 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
954 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
955 }
956
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200957 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400958 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200959
960 /* Unlink tx & rx urb */
961 usb_kill_urb(cardp->tx_urb);
962 usb_kill_urb(cardp->rx_urb);
963
964 cardp->rx_urb_recall = 1;
965
Holger Schurig9012b282007-05-25 11:27:16 -0400966 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200967 return 0;
968}
969
970static int if_usb_resume(struct usb_interface *intf)
971{
972 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100973 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200974
Holger Schurig9012b282007-05-25 11:27:16 -0400975 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200976
977 cardp->rx_urb_recall = 0;
978
979 if_usb_submit_rx_urb(cardp->priv);
980
981 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400982 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200983
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400984 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
985 /* Mesh autostart was activated while sleeping
986 * Disable it if appropriate
987 */
988 struct cmd_ds_mesh_access mesh_access;
989 memset(&mesh_access, 0, sizeof(mesh_access));
990 mesh_access.data[0] = cpu_to_le32(0);
Holger Schurig10078322007-11-15 18:05:47 -0500991 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400992 CMD_MESH_ACCESS,
993 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
994 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
995 }
996
Holger Schurig9012b282007-05-25 11:27:16 -0400997 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200998 return 0;
999}
1000#else
1001#define if_usb_suspend NULL
1002#define if_usb_resume NULL
1003#endif
1004
1005static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -05001006 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001007 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001008 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001009 .id_table = if_usb_table,
1010 .suspend = if_usb_suspend,
1011 .resume = if_usb_resume,
1012};
1013
Andres Salomon4fb910f2007-11-20 17:43:45 -05001014static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001015{
Holger Schurig084708b2007-05-25 12:37:58 -04001016 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001017
Holger Schurig084708b2007-05-25 12:37:58 -04001018 lbs_deb_enter(LBS_DEB_MAIN);
1019
Holger Schurig084708b2007-05-25 12:37:58 -04001020 ret = usb_register(&if_usb_driver);
1021
1022 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1023 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001024}
1025
Andres Salomon4fb910f2007-11-20 17:43:45 -05001026static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001027{
Holger Schurig084708b2007-05-25 12:37:58 -04001028 lbs_deb_enter(LBS_DEB_MAIN);
1029
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001030 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001031
1032 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001033}
Holger Schurig084708b2007-05-25 12:37:58 -04001034
1035module_init(if_usb_init_module);
1036module_exit(if_usb_exit_module);
1037
1038MODULE_DESCRIPTION("8388 USB WLAN Driver");
1039MODULE_AUTHOR("Marvell International Ltd.");
1040MODULE_LICENSE("GPL");