blob: d25e3b58e96b67a47a1f81689dc18645a0359b58 [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>
Holger Schurig435a1ac2007-05-25 12:41:52 -04008#include <linux/list.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02009#include <linux/usb.h>
10
Holger Schurigec3eef22007-05-25 12:49:10 -040011#define DRV_NAME "usb8xxx"
12
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020013#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014#include "decl.h"
15#include "defs.h"
16#include "dev.h"
17#include "if_usb.h"
18
19#define MESSAGE_HEADER_LEN 4
20
21static const char usbdriver_name[] = "usb8xxx";
Holger Schurig084708b2007-05-25 12:37:58 -040022static u8 *default_fw_name = "usb8388.bin";
23
24char *libertas_fw_name = NULL;
25module_param_named(fw_name, libertas_fw_name, charp, 0644);
26
Holger Schurig435a1ac2007-05-25 12:41:52 -040027/*
28 * We need to send a RESET command to all USB devices before
29 * we tear down the USB connection. Otherwise we would not
30 * be able to re-init device the device if the module gets
31 * loaded again. This is a list of all initialized USB devices,
32 * for the reset code see if_usb_reset_device()
33*/
34static LIST_HEAD(usb_devices);
Holger Schurig3874d0f2007-05-25 12:01:42 -040035
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020036static struct usb_device_id if_usb_table[] = {
37 /* Enter the device signature inside */
Holger Schurig66fcc552007-05-25 00:11:58 -040038 { USB_DEVICE(0x1286, 0x2001) },
39 { USB_DEVICE(0x05a3, 0x8388) },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020040 {} /* Terminating entry */
41};
42
43MODULE_DEVICE_TABLE(usb, if_usb_table);
44
45static void if_usb_receive(struct urb *urb);
46static void if_usb_receive_fwload(struct urb *urb);
Holger Schurig435a1ac2007-05-25 12:41:52 -040047static int if_usb_reset_device(wlan_private *priv);
Holger Schurig208fdd22007-05-25 12:17:06 -040048static int if_usb_register_dev(wlan_private * priv);
49static int if_usb_unregister_dev(wlan_private *);
50static int if_usb_prog_firmware(wlan_private *);
51static int if_usb_host_to_card(wlan_private * priv, u8 type, u8 * payload, u16 nb);
52static int if_usb_get_int_status(wlan_private * priv, u8 *);
53static int if_usb_read_event_cause(wlan_private *);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020054
55/**
56 * @brief call back function to handle the status of the URB
57 * @param urb pointer to urb structure
58 * @return N/A
59 */
60static void if_usb_write_bulk_callback(struct urb *urb)
61{
62 wlan_private *priv = (wlan_private *) (urb->context);
63 wlan_adapter *adapter = priv->adapter;
Holger Schurig634b8f42007-05-25 13:05:16 -040064 struct net_device *dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020065
66 /* handle the transmission complete validations */
67
68 if (urb->status != 0) {
69 /* print the failure status number for debug */
Luis Carlos Cobob46794d2007-05-25 13:10:18 -040070 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020071 } else {
Holger Schurig9012b282007-05-25 11:27:16 -040072 /*
73 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
74 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020075 urb->actual_length);
Holger Schurig9012b282007-05-25 11:27:16 -040076 */
Holger Schurig634b8f42007-05-25 13:05:16 -040077 priv->dnld_sent = DNLD_RES_RECEIVED;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020078 /* Wake main thread if commands are pending */
79 if (!adapter->cur_cmd)
80 wake_up_interruptible(&priv->mainthread.waitq);
Javier Cardona51d84f52007-05-25 12:06:56 -040081 if ((adapter->connect_status == libertas_connected)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020082 netif_wake_queue(dev);
Javier Cardona51d84f52007-05-25 12:06:56 -040083 netif_wake_queue(priv->mesh_dev);
84 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020085 }
86
87 return;
88}
89
90/**
91 * @brief free tx/rx urb, skb and rx buffer
92 * @param cardp pointer usb_card_rec
93 * @return N/A
94 */
95void if_usb_free(struct usb_card_rec *cardp)
96{
Holger Schurig9012b282007-05-25 11:27:16 -040097 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020098
99 /* Unlink tx & rx urb */
100 usb_kill_urb(cardp->tx_urb);
101 usb_kill_urb(cardp->rx_urb);
102
103 usb_free_urb(cardp->tx_urb);
104 cardp->tx_urb = NULL;
105
106 usb_free_urb(cardp->rx_urb);
107 cardp->rx_urb = NULL;
108
109 kfree(cardp->bulk_out_buffer);
110 cardp->bulk_out_buffer = NULL;
111
Holger Schurig9012b282007-05-25 11:27:16 -0400112 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200113}
114
115/**
116 * @brief sets the configuration values
117 * @param ifnum interface number
118 * @param id pointer to usb_device_id
119 * @return 0 on success, error code on failure
120 */
121static int if_usb_probe(struct usb_interface *intf,
122 const struct usb_device_id *id)
123{
124 struct usb_device *udev;
125 struct usb_host_interface *iface_desc;
126 struct usb_endpoint_descriptor *endpoint;
Holger Schurig78523da2007-05-25 11:49:19 -0400127 wlan_private *priv;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400128 struct usb_card_rec *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200129 int i;
130
131 udev = interface_to_usbdev(intf);
132
Holger Schurig435a1ac2007-05-25 12:41:52 -0400133 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
134 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200135 lbs_pr_err("Out of memory allocating private data.\n");
136 goto error;
137 }
138
Holger Schurig435a1ac2007-05-25 12:41:52 -0400139 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200140 iface_desc = intf->cur_altsetting;
141
Holger Schurig9012b282007-05-25 11:27:16 -0400142 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200143 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
144 udev->descriptor.bcdUSB,
145 udev->descriptor.bDeviceClass,
146 udev->descriptor.bDeviceSubClass,
147 udev->descriptor.bDeviceProtocol);
148
149 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
150 endpoint = &iface_desc->endpoint[i].desc;
151 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
152 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
153 USB_ENDPOINT_XFER_BULK)) {
154 /* we found a bulk in endpoint */
Holger Schurig9012b282007-05-25 11:27:16 -0400155 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200156 endpoint->wMaxPacketSize);
157 if (!
Holger Schurig435a1ac2007-05-25 12:41:52 -0400158 (cardp->rx_urb =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200159 usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400160 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200161 "Rx URB allocation failed\n");
162 goto dealloc;
163 }
Holger Schurig435a1ac2007-05-25 12:41:52 -0400164 cardp->rx_urb_recall = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200165
Holger Schurig435a1ac2007-05-25 12:41:52 -0400166 cardp->bulk_in_size =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167 endpoint->wMaxPacketSize;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400168 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200169 (endpoint->
170 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400171 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200172 endpoint->bEndpointAddress);
173 }
174
175 if (((endpoint->
176 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
177 USB_DIR_OUT)
178 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
179 USB_ENDPOINT_XFER_BULK)) {
180 /* We found bulk out endpoint */
181 if (!
Holger Schurig435a1ac2007-05-25 12:41:52 -0400182 (cardp->tx_urb =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200183 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 =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200190 endpoint->wMaxPacketSize;
Holger Schurig9012b282007-05-25 11:27:16 -0400191 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200192 "Bulk out size is %d\n",
193 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
210
Holger Schurig084708b2007-05-25 12:37:58 -0400211 /* At this point libertas_add_card() will be called. Don't worry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200212 * about keeping pwlanpriv around since it will be set on our
Holger Schurig208fdd22007-05-25 12:17:06 -0400213 * usb device data in -> add() -> hw_register_dev() -> if_usb_register_dev.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200214 */
Holger Schurig435a1ac2007-05-25 12:41:52 -0400215 if (!(priv = libertas_add_card(cardp)))
Holger Schurig78523da2007-05-25 11:49:19 -0400216 goto dealloc;
Holger Schurig3874d0f2007-05-25 12:01:42 -0400217
Holger Schurig084708b2007-05-25 12:37:58 -0400218 if (libertas_add_mesh(priv))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400219 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400220
Holger Schurig208fdd22007-05-25 12:17:06 -0400221 priv->hw_register_dev = if_usb_register_dev;
222 priv->hw_unregister_dev = if_usb_unregister_dev;
223 priv->hw_prog_firmware = if_usb_prog_firmware;
224 priv->hw_host_to_card = if_usb_host_to_card;
225 priv->hw_get_int_status = if_usb_get_int_status;
226 priv->hw_read_event_cause = if_usb_read_event_cause;
227
Holger Schurig084708b2007-05-25 12:37:58 -0400228 if (libertas_activate_card(priv, libertas_fw_name))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400229 goto err_activate_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400230
Holger Schurig435a1ac2007-05-25 12:41:52 -0400231 list_add_tail(&cardp->list, &usb_devices);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400232
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200233 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400234 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200235
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236 return 0;
237
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400238err_activate_card:
239 unregister_netdev(priv->mesh_dev);
240 free_netdev(priv->mesh_dev);
241err_add_mesh:
Holger Schurig634b8f42007-05-25 13:05:16 -0400242 free_netdev(priv->dev);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400243 kfree(priv->adapter);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200244dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400245 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200246
247error:
248 return -ENOMEM;
249}
250
251/**
252 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400253 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200254 * @return N/A
255 */
256static void if_usb_disconnect(struct usb_interface *intf)
257{
258 struct usb_card_rec *cardp = usb_get_intfdata(intf);
259 wlan_private *priv = (wlan_private *) cardp->priv;
260 wlan_adapter *adapter = NULL;
261
262 adapter = priv->adapter;
263
264 /*
265 * Update Surprise removed to TRUE
266 */
267 adapter->surpriseremoved = 1;
268
Holger Schurig435a1ac2007-05-25 12:41:52 -0400269 list_del(&cardp->list);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400270
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271 /* card is removed and we can call wlan_remove_card */
Holger Schurig9012b282007-05-25 11:27:16 -0400272 lbs_deb_usbd(&cardp->udev->dev, "call remove card\n");
Holger Schurig084708b2007-05-25 12:37:58 -0400273 libertas_remove_mesh(priv);
274 libertas_remove_card(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200275
276 /* Unlink and free urb */
277 if_usb_free(cardp);
278
279 usb_set_intfdata(intf, NULL);
280 usb_put_dev(interface_to_usbdev(intf));
281
282 return;
283}
284
285/**
286 * @brief This function download FW
287 * @param priv pointer to wlan_private
288 * @return 0
289 */
290static int if_prog_firmware(wlan_private * priv)
291{
Holger Schurig634b8f42007-05-25 13:05:16 -0400292 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200293 struct FWData *fwdata;
294 struct fwheader *fwheader;
295 u8 *firmware = priv->firmware->data;
296
297 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
298
299 if (!fwdata)
300 return -1;
301
302 fwheader = &fwdata->fwheader;
303
304 if (!cardp->CRC_OK) {
305 cardp->totalbytes = cardp->fwlastblksent;
306 cardp->fwseqnum = cardp->lastseqnum - 1;
307 }
308
Holger Schurig9012b282007-05-25 11:27:16 -0400309 /*
310 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200311 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400312 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200313
314 memcpy(fwheader, &firmware[cardp->totalbytes],
315 sizeof(struct fwheader));
316
317 cardp->fwlastblksent = cardp->totalbytes;
318 cardp->totalbytes += sizeof(struct fwheader);
319
Holger Schurig9012b282007-05-25 11:27:16 -0400320 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200321 memcpy(fwdata->data, &firmware[cardp->totalbytes],
322 fwdata->fwheader.datalength);
323
Holger Schurig9012b282007-05-25 11:27:16 -0400324 /*
325 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200326 "Data length = %d\n", fwdata->fwheader.datalength);
Holger Schurig9012b282007-05-25 11:27:16 -0400327 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200328
329 cardp->fwseqnum = cardp->fwseqnum + 1;
330
331 fwdata->seqnum = cardp->fwseqnum;
332 cardp->lastseqnum = fwdata->seqnum;
333 cardp->totalbytes += fwdata->fwheader.datalength;
334
335 if (fwheader->dnldcmd == FW_HAS_DATA_TO_RECV) {
Holger Schurig9012b282007-05-25 11:27:16 -0400336 /*
337 lbs_deb_usbd(&cardp->udev->dev, "There is data to follow\n");
338 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200339 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
340 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400341 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200342 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
343 usb_tx_block(priv, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
344
345 } else if (fwdata->fwheader.dnldcmd == FW_HAS_LAST_BLOCK) {
Holger Schurig9012b282007-05-25 11:27:16 -0400346 /*
347 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200348 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400349 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200350 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400351 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200352 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
353 usb_tx_block(priv, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
354 cardp->fwfinalblk = 1;
355 }
356
Holger Schurig9012b282007-05-25 11:27:16 -0400357 /*
358 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200359 "The firmware download is done size is %d\n",
360 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400361 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200362
363 kfree(fwdata);
364
365 return 0;
366}
367
368static int libertas_do_reset(wlan_private *priv)
369{
370 int ret;
Holger Schurig634b8f42007-05-25 13:05:16 -0400371 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200372
Holger Schurig3874d0f2007-05-25 12:01:42 -0400373 lbs_deb_enter(LBS_DEB_USB);
374
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200375 ret = usb_reset_device(cardp->udev);
376 if (!ret) {
377 msleep(10);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400378 if_usb_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200379 msleep(10);
380 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400381
382 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
383
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200384 return ret;
385}
386
387/**
388 * @brief This function transfer the data to the device.
389 * @param priv pointer to wlan_private
390 * @param payload pointer to payload data
391 * @param nb data length
392 * @return 0 or -1
393 */
394int usb_tx_block(wlan_private * priv, u8 * payload, u16 nb)
395{
396 /* pointer to card structure */
Holger Schurig634b8f42007-05-25 13:05:16 -0400397 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200398 int ret = -1;
399
400 /* check if device is removed */
401 if (priv->adapter->surpriseremoved) {
Holger Schurig9012b282007-05-25 11:27:16 -0400402 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200403 goto tx_ret;
404 }
405
406 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
407 usb_sndbulkpipe(cardp->udev,
408 cardp->bulk_out_endpointAddr),
409 payload, nb, if_usb_write_bulk_callback, priv);
410
411 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
412
413 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
414 /* transfer failed */
Holger Schurig9012b282007-05-25 11:27:16 -0400415 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200416 ret = -1;
417 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400418 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200419 ret = 0;
420 }
421
422tx_ret:
423 return ret;
424}
425
426static int __if_usb_submit_rx_urb(wlan_private * priv,
427 void (*callbackfn)
428 (struct urb *urb))
429{
Holger Schurig634b8f42007-05-25 13:05:16 -0400430 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200431 struct sk_buff *skb;
432 struct read_cb_info *rinfo = &cardp->rinfo;
433 int ret = -1;
434
435 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
436 lbs_pr_err("No free skb\n");
437 goto rx_ret;
438 }
439
440 rinfo->skb = skb;
441
442 /* Fill the receive configuration URB and initialise the Rx call back */
443 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
444 usb_rcvbulkpipe(cardp->udev,
445 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400446 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200447 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
448 rinfo);
449
450 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
451
Holger Schurig9012b282007-05-25 11:27:16 -0400452 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200453 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
454 /* handle failure conditions */
Holger Schurig9012b282007-05-25 11:27:16 -0400455 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456 ret = -1;
457 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400458 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200459 ret = 0;
460 }
461
462rx_ret:
463 return ret;
464}
465
466static inline int if_usb_submit_rx_urb_fwload(wlan_private * priv)
467{
468 return __if_usb_submit_rx_urb(priv, &if_usb_receive_fwload);
469}
470
471static inline int if_usb_submit_rx_urb(wlan_private * priv)
472{
473 return __if_usb_submit_rx_urb(priv, &if_usb_receive);
474}
475
476static void if_usb_receive_fwload(struct urb *urb)
477{
478 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
479 wlan_private *priv = rinfo->priv;
480 struct sk_buff *skb = rinfo->skb;
Holger Schurig634b8f42007-05-25 13:05:16 -0400481 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200482 struct fwsyncheader *syncfwheader;
483 struct bootcmdrespStr bootcmdresp;
484
485 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400486 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200487 "URB status is failed during fw load\n");
488 kfree_skb(skb);
489 return;
490 }
491
492 if (cardp->bootcmdresp == 0) {
493 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
494 sizeof(bootcmdresp));
495 if (cardp->udev->descriptor.bcdDevice < 0x3106) {
496 kfree_skb(skb);
497 if_usb_submit_rx_urb_fwload(priv);
498 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400499 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500 "Received valid boot command response\n");
501 return;
502 }
503 if (bootcmdresp.u32magicnumber != BOOT_CMD_MAGIC_NUMBER) {
504 lbs_pr_info(
505 "boot cmd response wrong magic number (0x%x)\n",
506 bootcmdresp.u32magicnumber);
507 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
508 lbs_pr_info(
509 "boot cmd response cmd_tag error (%d)\n",
510 bootcmdresp.u8cmd_tag);
511 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
512 lbs_pr_info(
513 "boot cmd response result error (%d)\n",
514 bootcmdresp.u8result);
515 } else {
516 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400517 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200518 "Received valid boot command response\n");
519 }
520 kfree_skb(skb);
521 if_usb_submit_rx_urb_fwload(priv);
522 return;
523 }
524
525 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
526 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400527 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200528 kfree_skb(skb);
529 return;
530 }
531
532 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
533 sizeof(struct fwsyncheader));
534
535 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400536 /*
537 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400539 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200540 "FW received Blk seqnum = %d\n",
541 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400542 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200543 cardp->CRC_OK = 1;
544 } else {
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 with CRC error\n");
547 cardp->CRC_OK = 0;
548 }
549
550 kfree_skb(skb);
551
552 if (cardp->fwfinalblk) {
553 cardp->fwdnldover = 1;
554 goto exit;
555 }
556
557 if_prog_firmware(priv);
558
559 if_usb_submit_rx_urb_fwload(priv);
560exit:
561 kfree(syncfwheader);
562
563 return;
564
565}
566
567#define MRVDRV_MIN_PKT_LEN 30
568
569static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
570 struct usb_card_rec *cardp,
571 wlan_private *priv)
572{
573 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
574 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400575 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200576 "Packet length is Invalid\n");
577 kfree_skb(skb);
578 return;
579 }
580
581 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
582 skb_put(skb, recvlength);
583 skb_pull(skb, MESSAGE_HEADER_LEN);
584 libertas_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400585 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200586}
587
588static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
589 struct sk_buff *skb,
590 struct usb_card_rec *cardp,
591 wlan_private *priv)
592{
593 u8 *cmdbuf;
594 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400595 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200596 "The receive buffer is too large\n");
597 kfree_skb(skb);
598 return;
599 }
600
601 if (!in_interrupt())
602 BUG();
603
604 spin_lock(&priv->adapter->driver_lock);
605 /* take care of cur_cmd = NULL case by reading the
606 * data to clear the interrupt */
607 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400608 cmdbuf = priv->upld_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609 priv->adapter->hisregcpy &= ~his_cmdupldrdy;
610 } else
611 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
612
613 cardp->usb_int_cause |= his_cmdupldrdy;
Holger Schurig634b8f42007-05-25 13:05:16 -0400614 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200615 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400616 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200617
618 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400619 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200620 spin_unlock(&priv->adapter->driver_lock);
621
Holger Schurig9012b282007-05-25 11:27:16 -0400622 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200623 "Wake up main thread to handle cmd response\n");
624
625 return;
626}
627
628/**
629 * @brief This function reads of the packet into the upload buff,
630 * wake up the main thread and initialise the Rx callack.
631 *
632 * @param urb pointer to struct urb
633 * @return N/A
634 */
635static void if_usb_receive(struct urb *urb)
636{
637 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
638 wlan_private *priv = rinfo->priv;
639 struct sk_buff *skb = rinfo->skb;
Holger Schurig634b8f42007-05-25 13:05:16 -0400640 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200641
642 int recvlength = urb->actual_length;
643 u8 *recvbuff = NULL;
644 u32 recvtype;
645
Holger Schurig9012b282007-05-25 11:27:16 -0400646 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200647
648 if (recvlength) {
649 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400650 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200651 "URB status is failed\n");
652 kfree_skb(skb);
653 goto setup_for_next;
654 }
655
656 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
657 memcpy(&recvtype, recvbuff, sizeof(u32));
Holger Schurig9012b282007-05-25 11:27:16 -0400658 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200659 "Recv length = 0x%x\n", recvlength);
Holger Schurig9012b282007-05-25 11:27:16 -0400660 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200661 "Receive type = 0x%X\n", recvtype);
662 recvtype = le32_to_cpu(recvtype);
Holger Schurig9012b282007-05-25 11:27:16 -0400663 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200664 "Receive type after = 0x%X\n", recvtype);
665 } else if (urb->status)
666 goto rx_exit;
667
668
669 switch (recvtype) {
670 case CMD_TYPE_DATA:
671 process_cmdtypedata(recvlength, skb, cardp, priv);
672 break;
673
674 case CMD_TYPE_REQUEST:
675 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
676 break;
677
678 case CMD_TYPE_INDICATION:
679 /* Event cause handling */
680 spin_lock(&priv->adapter->driver_lock);
681 cardp->usb_event_cause = *(u32 *) (recvbuff + MESSAGE_HEADER_LEN);
Holger Schurig9012b282007-05-25 11:27:16 -0400682 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683 cardp->usb_event_cause);
684 if (cardp->usb_event_cause & 0xffff0000) {
685 libertas_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400686 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200687 break;
688 }
689 cardp->usb_event_cause = le32_to_cpu(cardp->usb_event_cause) << 3;
690 cardp->usb_int_cause |= his_cardevent;
691 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400692 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200693 spin_unlock(&priv->adapter->driver_lock);
694 goto rx_exit;
695 default:
696 kfree_skb(skb);
697 break;
698 }
699
700setup_for_next:
701 if_usb_submit_rx_urb(priv);
702rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400703 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200704}
705
706/**
707 * @brief This function downloads data to FW
708 * @param priv pointer to wlan_private structure
709 * @param type type of data
710 * @param buf pointer to data buffer
711 * @param len number of bytes
712 * @return 0 or -1
713 */
Holger Schurig208fdd22007-05-25 12:17:06 -0400714static int if_usb_host_to_card(wlan_private * priv, u8 type, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200715{
716 int ret = -1;
717 u32 tmp;
Holger Schurig634b8f42007-05-25 13:05:16 -0400718 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719
Holger Schurig9012b282007-05-25 11:27:16 -0400720 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
721 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722
723 if (type == MVMS_CMD) {
724 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400725 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200726 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
727 MESSAGE_HEADER_LEN);
728
729 } else {
730 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400731 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200732 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
733 MESSAGE_HEADER_LEN);
734 }
735
736 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
737
738 ret =
739 usb_tx_block(priv, cardp->bulk_out_buffer, nb + MESSAGE_HEADER_LEN);
740
741 return ret;
742}
743
744/* called with adapter->driver_lock held */
Holger Schurig208fdd22007-05-25 12:17:06 -0400745static int if_usb_get_int_status(wlan_private * priv, u8 * ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746{
Holger Schurig634b8f42007-05-25 13:05:16 -0400747 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200748
749 *ireg = cardp->usb_int_cause;
750 cardp->usb_int_cause = 0;
751
Holger Schurig9012b282007-05-25 11:27:16 -0400752 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753
754 return 0;
755}
756
Holger Schurig208fdd22007-05-25 12:17:06 -0400757static int if_usb_read_event_cause(wlan_private * priv)
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 priv->adapter->eventcause = cardp->usb_event_cause;
761 /* Re-submit rx urb here to avoid event lost issue */
762 if_usb_submit_rx_urb(priv);
763 return 0;
764}
765
Holger Schurig435a1ac2007-05-25 12:41:52 -0400766static int if_usb_reset_device(wlan_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200767{
768 int ret;
769
Holger Schurig3874d0f2007-05-25 12:01:42 -0400770 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200771 ret = libertas_prepare_and_send_command(priv, cmd_802_11_reset,
772 cmd_act_halt, 0, 0, NULL);
773 msleep_interruptible(10);
774
Holger Schurig3874d0f2007-05-25 12:01:42 -0400775 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200776 return ret;
777}
778
Holger Schurig208fdd22007-05-25 12:17:06 -0400779static int if_usb_unregister_dev(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200780{
781 int ret = 0;
782
783 /* Need to send a Reset command to device before USB resources freed
784 * and wlan_remove_card() called, then device can handle FW download
785 * again.
786 */
787 if (priv)
Holger Schurig435a1ac2007-05-25 12:41:52 -0400788 if_usb_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200789
790 return ret;
791}
792
793
794/**
795 * @brief This function register usb device and initialize parameter
796 * @param priv pointer to wlan_private
797 * @return 0 or -1
798 */
Holger Schurig208fdd22007-05-25 12:17:06 -0400799static int if_usb_register_dev(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200800{
Holger Schurig634b8f42007-05-25 13:05:16 -0400801 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Holger Schurig9012b282007-05-25 11:27:16 -0400802
803 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200804
805 cardp->priv = priv;
Holger Schurig634b8f42007-05-25 13:05:16 -0400806 cardp->eth_dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200807 priv->hotplug_device = &(cardp->udev->dev);
808
809 SET_NETDEV_DEV(cardp->eth_dev, &(cardp->udev->dev));
Javier Cardona51d84f52007-05-25 12:06:56 -0400810 SET_NETDEV_DEV(priv->mesh_dev, &(cardp->udev->dev));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200811
Holger Schurig9012b282007-05-25 11:27:16 -0400812 lbs_deb_usbd(&cardp->udev->dev, "udev pointer is at %p\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200813 cardp->udev);
814
Holger Schurig9012b282007-05-25 11:27:16 -0400815 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200816 return 0;
817}
818
819
820
Holger Schurig208fdd22007-05-25 12:17:06 -0400821static int if_usb_prog_firmware(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200822{
Holger Schurig634b8f42007-05-25 13:05:16 -0400823 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200824 int i = 0;
825 static int reset_count = 10;
Holger Schurig9012b282007-05-25 11:27:16 -0400826 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200827
Holger Schurig9012b282007-05-25 11:27:16 -0400828 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200829
830 cardp->rinfo.priv = priv;
831
832restart:
833 if (if_usb_submit_rx_urb_fwload(priv) < 0) {
Holger Schurig9012b282007-05-25 11:27:16 -0400834 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
835 ret = -1;
836 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200837 }
838
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200839 cardp->bootcmdresp = 0;
840 do {
841 int j = 0;
842 i++;
843 /* Issue Boot command = 1, Boot from Download-FW */
844 if_usb_issue_boot_command(priv, BOOT_CMD_FW_BY_USB);
845 /* wait for command response */
846 do {
847 j++;
848 msleep_interruptible(100);
849 } while (cardp->bootcmdresp == 0 && j < 10);
850 } while (cardp->bootcmdresp == 0 && i < 5);
851
852 if (cardp->bootcmdresp == 0) {
853 if (--reset_count >= 0) {
854 libertas_do_reset(priv);
855 goto restart;
856 }
857 return -1;
858 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200859
860 i = 0;
861 priv->adapter->fw_ready = 0;
862
863 cardp->totalbytes = 0;
864 cardp->fwlastblksent = 0;
865 cardp->CRC_OK = 1;
866 cardp->fwdnldover = 0;
867 cardp->fwseqnum = -1;
868 cardp->totalbytes = 0;
869 cardp->fwfinalblk = 0;
870
871 if_prog_firmware(priv);
872
873 do {
Holger Schurig9012b282007-05-25 11:27:16 -0400874 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200875 i++;
876 msleep_interruptible(100);
877 if (priv->adapter->surpriseremoved || i >= 20)
878 break;
879 } while (!cardp->fwdnldover);
880
881 if (!cardp->fwdnldover) {
882 lbs_pr_info("failed to load fw, resetting device!\n");
883 if (--reset_count >= 0) {
884 libertas_do_reset(priv);
885 goto restart;
886 }
887
888 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
Holger Schurig9012b282007-05-25 11:27:16 -0400889 ret = -1;
890 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200891 }
892
893 if_usb_submit_rx_urb(priv);
894
895 /* Delay 200 ms to waiting for the FW ready */
896 msleep_interruptible(200);
897
898 priv->adapter->fw_ready = 1;
899
Holger Schurig9012b282007-05-25 11:27:16 -0400900done:
901 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
902 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200903}
904
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200905#ifdef CONFIG_PM
906static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
907{
908 struct usb_card_rec *cardp = usb_get_intfdata(intf);
909 wlan_private *priv = cardp->priv;
910
Holger Schurig9012b282007-05-25 11:27:16 -0400911 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200912
913 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
914 return -1;
915
916 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400917 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200918
919 /* Unlink tx & rx urb */
920 usb_kill_urb(cardp->tx_urb);
921 usb_kill_urb(cardp->rx_urb);
922
923 cardp->rx_urb_recall = 1;
924
Holger Schurig9012b282007-05-25 11:27:16 -0400925 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200926 return 0;
927}
928
929static int if_usb_resume(struct usb_interface *intf)
930{
931 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Javier Cardona51d84f52007-05-25 12:06:56 -0400932 wlan_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200933
Holger Schurig9012b282007-05-25 11:27:16 -0400934 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200935
936 cardp->rx_urb_recall = 0;
937
938 if_usb_submit_rx_urb(cardp->priv);
939
940 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400941 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200942
Holger Schurig9012b282007-05-25 11:27:16 -0400943 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200944 return 0;
945}
946#else
947#define if_usb_suspend NULL
948#define if_usb_resume NULL
949#endif
950
951static struct usb_driver if_usb_driver = {
952 /* driver name */
953 .name = usbdriver_name,
954 /* probe function name */
955 .probe = if_usb_probe,
956 /* disconnect function name */
957 .disconnect = if_usb_disconnect,
958 /* device signature table */
959 .id_table = if_usb_table,
960 .suspend = if_usb_suspend,
961 .resume = if_usb_resume,
962};
963
Holger Schurig084708b2007-05-25 12:37:58 -0400964static int if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200965{
Holger Schurig084708b2007-05-25 12:37:58 -0400966 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200967
Holger Schurig084708b2007-05-25 12:37:58 -0400968 lbs_deb_enter(LBS_DEB_MAIN);
969
970 if (libertas_fw_name == NULL) {
971 libertas_fw_name = default_fw_name;
972 }
973
974 ret = usb_register(&if_usb_driver);
975
976 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
977 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200978}
979
Holger Schurig084708b2007-05-25 12:37:58 -0400980static void if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200981{
Holger Schurig435a1ac2007-05-25 12:41:52 -0400982 struct list_head *ptr;
983 struct usb_card_rec *cardp;
Holger Schurig3874d0f2007-05-25 12:01:42 -0400984
Holger Schurig084708b2007-05-25 12:37:58 -0400985 lbs_deb_enter(LBS_DEB_MAIN);
986
Holger Schurig435a1ac2007-05-25 12:41:52 -0400987 list_for_each(ptr, &usb_devices) {
988 cardp = list_entry(ptr, struct usb_card_rec, list);
989 if_usb_reset_device((wlan_private *) cardp->priv);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400990 }
991
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200992 /* API unregisters the driver from USB subsystem */
993 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -0400994
995 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200996}
Holger Schurig084708b2007-05-25 12:37:58 -0400997
998module_init(if_usb_init_module);
999module_exit(if_usb_exit_module);
1000
1001MODULE_DESCRIPTION("8388 USB WLAN Driver");
1002MODULE_AUTHOR("Marvell International Ltd.");
1003MODULE_LICENSE("GPL");