blob: 998317571ec26cdc3b97342b2da376a0d2f2561f [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",
David Woodhouse981f1872007-05-25 23:36:54 -0400144 le16_to_cpu(udev->descriptor.bcdUSB),
145 udev->descriptor.bDeviceClass,
146 udev->descriptor.bDeviceSubClass,
147 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200148
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",
David Woodhouse981f1872007-05-25 23:36:54 -0400156 le16_to_cpu(endpoint->wMaxPacketSize));
157 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400158 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200159 "Rx URB allocation failed\n");
160 goto dealloc;
161 }
Holger Schurig435a1ac2007-05-25 12:41:52 -0400162 cardp->rx_urb_recall = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200163
Holger Schurig435a1ac2007-05-25 12:41:52 -0400164 cardp->bulk_in_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400165 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400166 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167 (endpoint->
168 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400169 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200170 endpoint->bEndpointAddress);
171 }
172
173 if (((endpoint->
174 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
175 USB_DIR_OUT)
176 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
177 USB_ENDPOINT_XFER_BULK)) {
178 /* We found bulk out endpoint */
David Woodhouse981f1872007-05-25 23:36:54 -0400179 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400180 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200181 "Tx URB allocation failed\n");
182 goto dealloc;
183 }
184
Holger Schurig435a1ac2007-05-25 12:41:52 -0400185 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400186 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400187 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400188 "Bulk out size is %d\n",
189 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400190 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200191 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400192 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200193 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400194 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200195 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
196 GFP_KERNEL);
197
Holger Schurig435a1ac2007-05-25 12:41:52 -0400198 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400199 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200200 "Could not allocate buffer\n");
201 goto dealloc;
202 }
203 }
204 }
205
Dan Williams7732ca42007-05-25 13:13:25 -0400206 if (!(priv = libertas_add_card(cardp, &udev->dev)))
Holger Schurig78523da2007-05-25 11:49:19 -0400207 goto dealloc;
Holger Schurig3874d0f2007-05-25 12:01:42 -0400208
Dan Williams7732ca42007-05-25 13:13:25 -0400209 if (libertas_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400210 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400211
Holger Schurig208fdd22007-05-25 12:17:06 -0400212 priv->hw_register_dev = if_usb_register_dev;
213 priv->hw_unregister_dev = if_usb_unregister_dev;
214 priv->hw_prog_firmware = if_usb_prog_firmware;
215 priv->hw_host_to_card = if_usb_host_to_card;
216 priv->hw_get_int_status = if_usb_get_int_status;
217 priv->hw_read_event_cause = if_usb_read_event_cause;
218
Holger Schurig084708b2007-05-25 12:37:58 -0400219 if (libertas_activate_card(priv, libertas_fw_name))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400220 goto err_activate_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400221
Holger Schurig435a1ac2007-05-25 12:41:52 -0400222 list_add_tail(&cardp->list, &usb_devices);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400223
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200224 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400225 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200226
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200227 return 0;
228
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400229err_activate_card:
Dan Williamsc7236832007-05-25 13:35:23 -0400230 libertas_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400231err_add_mesh:
Holger Schurig634b8f42007-05-25 13:05:16 -0400232 free_netdev(priv->dev);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400233 kfree(priv->adapter);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200234dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400235 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236
237error:
238 return -ENOMEM;
239}
240
241/**
242 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400243 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200244 * @return N/A
245 */
246static void if_usb_disconnect(struct usb_interface *intf)
247{
248 struct usb_card_rec *cardp = usb_get_intfdata(intf);
249 wlan_private *priv = (wlan_private *) cardp->priv;
250 wlan_adapter *adapter = NULL;
251
252 adapter = priv->adapter;
253
254 /*
255 * Update Surprise removed to TRUE
256 */
257 adapter->surpriseremoved = 1;
258
Holger Schurig435a1ac2007-05-25 12:41:52 -0400259 list_del(&cardp->list);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400260
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200261 /* card is removed and we can call wlan_remove_card */
Holger Schurig9012b282007-05-25 11:27:16 -0400262 lbs_deb_usbd(&cardp->udev->dev, "call remove card\n");
Holger Schurig084708b2007-05-25 12:37:58 -0400263 libertas_remove_mesh(priv);
264 libertas_remove_card(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200265
266 /* Unlink and free urb */
267 if_usb_free(cardp);
268
269 usb_set_intfdata(intf, NULL);
270 usb_put_dev(interface_to_usbdev(intf));
271
272 return;
273}
274
275/**
276 * @brief This function download FW
277 * @param priv pointer to wlan_private
278 * @return 0
279 */
280static int if_prog_firmware(wlan_private * priv)
281{
Holger Schurig634b8f42007-05-25 13:05:16 -0400282 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200283 struct FWData *fwdata;
284 struct fwheader *fwheader;
285 u8 *firmware = priv->firmware->data;
286
287 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
288
289 if (!fwdata)
290 return -1;
291
292 fwheader = &fwdata->fwheader;
293
294 if (!cardp->CRC_OK) {
295 cardp->totalbytes = cardp->fwlastblksent;
296 cardp->fwseqnum = cardp->lastseqnum - 1;
297 }
298
Holger Schurig9012b282007-05-25 11:27:16 -0400299 /*
300 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200301 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400302 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200303
304 memcpy(fwheader, &firmware[cardp->totalbytes],
305 sizeof(struct fwheader));
306
307 cardp->fwlastblksent = cardp->totalbytes;
308 cardp->totalbytes += sizeof(struct fwheader);
309
Holger Schurig9012b282007-05-25 11:27:16 -0400310 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200311 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400312 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200313
Holger Schurig9012b282007-05-25 11:27:16 -0400314 /*
315 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400316 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400317 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200318
319 cardp->fwseqnum = cardp->fwseqnum + 1;
320
David Woodhouse981f1872007-05-25 23:36:54 -0400321 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
322 cardp->lastseqnum = cardp->fwseqnum;
323 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200324
David Woodhouse981f1872007-05-25 23:36:54 -0400325 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400326 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400327 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400328 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200329 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
330 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400331 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200332 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
333 usb_tx_block(priv, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
334
David Woodhousebb793e22007-05-25 23:38:14 -0400335 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400336 /*
337 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400339 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200340 "Donwloading FW JUMP BLOCK\n");
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 cardp->fwfinalblk = 1;
345 }
346
Holger Schurig9012b282007-05-25 11:27:16 -0400347 /*
348 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200349 "The firmware download is done size is %d\n",
350 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400351 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200352
353 kfree(fwdata);
354
355 return 0;
356}
357
358static int libertas_do_reset(wlan_private *priv)
359{
360 int ret;
Holger Schurig634b8f42007-05-25 13:05:16 -0400361 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200362
Holger Schurig3874d0f2007-05-25 12:01:42 -0400363 lbs_deb_enter(LBS_DEB_USB);
364
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200365 ret = usb_reset_device(cardp->udev);
366 if (!ret) {
367 msleep(10);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400368 if_usb_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200369 msleep(10);
370 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400371
372 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
373
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200374 return ret;
375}
376
377/**
378 * @brief This function transfer the data to the device.
379 * @param priv pointer to wlan_private
380 * @param payload pointer to payload data
381 * @param nb data length
382 * @return 0 or -1
383 */
384int usb_tx_block(wlan_private * priv, u8 * payload, u16 nb)
385{
386 /* pointer to card structure */
Holger Schurig634b8f42007-05-25 13:05:16 -0400387 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200388 int ret = -1;
389
390 /* check if device is removed */
391 if (priv->adapter->surpriseremoved) {
Holger Schurig9012b282007-05-25 11:27:16 -0400392 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200393 goto tx_ret;
394 }
395
396 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
397 usb_sndbulkpipe(cardp->udev,
398 cardp->bulk_out_endpointAddr),
399 payload, nb, if_usb_write_bulk_callback, priv);
400
401 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
402
403 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
404 /* transfer failed */
Holger Schurig9012b282007-05-25 11:27:16 -0400405 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200406 ret = -1;
407 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400408 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200409 ret = 0;
410 }
411
412tx_ret:
413 return ret;
414}
415
416static int __if_usb_submit_rx_urb(wlan_private * priv,
417 void (*callbackfn)
418 (struct urb *urb))
419{
Holger Schurig634b8f42007-05-25 13:05:16 -0400420 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200421 struct sk_buff *skb;
422 struct read_cb_info *rinfo = &cardp->rinfo;
423 int ret = -1;
424
425 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
426 lbs_pr_err("No free skb\n");
427 goto rx_ret;
428 }
429
430 rinfo->skb = skb;
431
432 /* Fill the receive configuration URB and initialise the Rx call back */
433 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
434 usb_rcvbulkpipe(cardp->udev,
435 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400436 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200437 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
438 rinfo);
439
440 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
441
Holger Schurig9012b282007-05-25 11:27:16 -0400442 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200443 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
444 /* handle failure conditions */
Holger Schurig9012b282007-05-25 11:27:16 -0400445 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446 ret = -1;
447 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400448 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200449 ret = 0;
450 }
451
452rx_ret:
453 return ret;
454}
455
456static inline int if_usb_submit_rx_urb_fwload(wlan_private * priv)
457{
458 return __if_usb_submit_rx_urb(priv, &if_usb_receive_fwload);
459}
460
461static inline int if_usb_submit_rx_urb(wlan_private * priv)
462{
463 return __if_usb_submit_rx_urb(priv, &if_usb_receive);
464}
465
466static void if_usb_receive_fwload(struct urb *urb)
467{
468 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
469 wlan_private *priv = rinfo->priv;
470 struct sk_buff *skb = rinfo->skb;
Holger Schurig634b8f42007-05-25 13:05:16 -0400471 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200472 struct fwsyncheader *syncfwheader;
473 struct bootcmdrespStr bootcmdresp;
474
475 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400476 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200477 "URB status is failed during fw load\n");
478 kfree_skb(skb);
479 return;
480 }
481
482 if (cardp->bootcmdresp == 0) {
483 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
484 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400485 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486 kfree_skb(skb);
487 if_usb_submit_rx_urb_fwload(priv);
488 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400489 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200490 "Received valid boot command response\n");
491 return;
492 }
David Woodhouse981f1872007-05-25 23:36:54 -0400493 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200494 lbs_pr_info(
495 "boot cmd response wrong magic number (0x%x)\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400496 le32_to_cpu(bootcmdresp.u32magicnumber));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200497 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
498 lbs_pr_info(
499 "boot cmd response cmd_tag error (%d)\n",
500 bootcmdresp.u8cmd_tag);
501 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
502 lbs_pr_info(
503 "boot cmd response result error (%d)\n",
504 bootcmdresp.u8result);
505 } else {
506 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400507 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200508 "Received valid boot command response\n");
509 }
510 kfree_skb(skb);
511 if_usb_submit_rx_urb_fwload(priv);
512 return;
513 }
514
515 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
516 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400517 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200518 kfree_skb(skb);
519 return;
520 }
521
522 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
523 sizeof(struct fwsyncheader));
524
525 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400526 /*
527 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200528 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400529 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200530 "FW received Blk seqnum = %d\n",
531 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400532 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533 cardp->CRC_OK = 1;
534 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400535 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200536 "FW received Blk with CRC error\n");
537 cardp->CRC_OK = 0;
538 }
539
540 kfree_skb(skb);
541
542 if (cardp->fwfinalblk) {
543 cardp->fwdnldover = 1;
544 goto exit;
545 }
546
547 if_prog_firmware(priv);
548
549 if_usb_submit_rx_urb_fwload(priv);
550exit:
551 kfree(syncfwheader);
552
553 return;
554
555}
556
557#define MRVDRV_MIN_PKT_LEN 30
558
559static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
560 struct usb_card_rec *cardp,
561 wlan_private *priv)
562{
563 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
564 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400565 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200566 "Packet length is Invalid\n");
567 kfree_skb(skb);
568 return;
569 }
570
571 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
572 skb_put(skb, recvlength);
573 skb_pull(skb, MESSAGE_HEADER_LEN);
574 libertas_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400575 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200576}
577
578static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
579 struct sk_buff *skb,
580 struct usb_card_rec *cardp,
581 wlan_private *priv)
582{
583 u8 *cmdbuf;
584 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400585 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200586 "The receive buffer is too large\n");
587 kfree_skb(skb);
588 return;
589 }
590
591 if (!in_interrupt())
592 BUG();
593
594 spin_lock(&priv->adapter->driver_lock);
595 /* take care of cur_cmd = NULL case by reading the
596 * data to clear the interrupt */
597 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400598 cmdbuf = priv->upld_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200599 priv->adapter->hisregcpy &= ~his_cmdupldrdy;
600 } else
601 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
602
603 cardp->usb_int_cause |= his_cmdupldrdy;
Holger Schurig634b8f42007-05-25 13:05:16 -0400604 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200605 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400606 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200607
608 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400609 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200610 spin_unlock(&priv->adapter->driver_lock);
611
Holger Schurig9012b282007-05-25 11:27:16 -0400612 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200613 "Wake up main thread to handle cmd response\n");
614
615 return;
616}
617
618/**
619 * @brief This function reads of the packet into the upload buff,
620 * wake up the main thread and initialise the Rx callack.
621 *
622 * @param urb pointer to struct urb
623 * @return N/A
624 */
625static void if_usb_receive(struct urb *urb)
626{
627 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
628 wlan_private *priv = rinfo->priv;
629 struct sk_buff *skb = rinfo->skb;
Holger Schurig634b8f42007-05-25 13:05:16 -0400630 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200631
632 int recvlength = urb->actual_length;
633 u8 *recvbuff = NULL;
634 u32 recvtype;
635
Holger Schurig9012b282007-05-25 11:27:16 -0400636 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200637
638 if (recvlength) {
639 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400640 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200641 "URB status is failed\n");
642 kfree_skb(skb);
643 goto setup_for_next;
644 }
645
646 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
647 memcpy(&recvtype, recvbuff, sizeof(u32));
Holger Schurig9012b282007-05-25 11:27:16 -0400648 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200649 "Recv length = 0x%x\n", recvlength);
Holger Schurig9012b282007-05-25 11:27:16 -0400650 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200651 "Receive type = 0x%X\n", recvtype);
652 recvtype = le32_to_cpu(recvtype);
Holger Schurig9012b282007-05-25 11:27:16 -0400653 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200654 "Receive type after = 0x%X\n", recvtype);
655 } else if (urb->status)
656 goto rx_exit;
657
658
659 switch (recvtype) {
660 case CMD_TYPE_DATA:
661 process_cmdtypedata(recvlength, skb, cardp, priv);
662 break;
663
664 case CMD_TYPE_REQUEST:
665 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
666 break;
667
668 case CMD_TYPE_INDICATION:
669 /* Event cause handling */
670 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400671 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400672 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200673 cardp->usb_event_cause);
674 if (cardp->usb_event_cause & 0xffff0000) {
675 libertas_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400676 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200677 break;
678 }
David Woodhouse981f1872007-05-25 23:36:54 -0400679 cardp->usb_event_cause <<= 3;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200680 cardp->usb_int_cause |= his_cardevent;
681 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400682 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683 spin_unlock(&priv->adapter->driver_lock);
684 goto rx_exit;
685 default:
686 kfree_skb(skb);
687 break;
688 }
689
690setup_for_next:
691 if_usb_submit_rx_urb(priv);
692rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400693 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694}
695
696/**
697 * @brief This function downloads data to FW
698 * @param priv pointer to wlan_private structure
699 * @param type type of data
700 * @param buf pointer to data buffer
701 * @param len number of bytes
702 * @return 0 or -1
703 */
Holger Schurig208fdd22007-05-25 12:17:06 -0400704static int if_usb_host_to_card(wlan_private * priv, u8 type, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200705{
706 int ret = -1;
707 u32 tmp;
Holger Schurig634b8f42007-05-25 13:05:16 -0400708 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200709
Holger Schurig9012b282007-05-25 11:27:16 -0400710 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
711 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200712
713 if (type == MVMS_CMD) {
714 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400715 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
717 MESSAGE_HEADER_LEN);
718
719 } else {
720 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400721 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
723 MESSAGE_HEADER_LEN);
724 }
725
726 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
727
728 ret =
729 usb_tx_block(priv, cardp->bulk_out_buffer, nb + MESSAGE_HEADER_LEN);
730
731 return ret;
732}
733
734/* called with adapter->driver_lock held */
Holger Schurig208fdd22007-05-25 12:17:06 -0400735static int if_usb_get_int_status(wlan_private * priv, u8 * ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200736{
Holger Schurig634b8f42007-05-25 13:05:16 -0400737 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200738
739 *ireg = cardp->usb_int_cause;
740 cardp->usb_int_cause = 0;
741
Holger Schurig9012b282007-05-25 11:27:16 -0400742 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200743
744 return 0;
745}
746
Holger Schurig208fdd22007-05-25 12:17:06 -0400747static int if_usb_read_event_cause(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200748{
Holger Schurig634b8f42007-05-25 13:05:16 -0400749 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200750 priv->adapter->eventcause = cardp->usb_event_cause;
751 /* Re-submit rx urb here to avoid event lost issue */
752 if_usb_submit_rx_urb(priv);
753 return 0;
754}
755
Holger Schurig435a1ac2007-05-25 12:41:52 -0400756static int if_usb_reset_device(wlan_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200757{
758 int ret;
759
Holger Schurig3874d0f2007-05-25 12:01:42 -0400760 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200761 ret = libertas_prepare_and_send_command(priv, cmd_802_11_reset,
762 cmd_act_halt, 0, 0, NULL);
763 msleep_interruptible(10);
764
Holger Schurig3874d0f2007-05-25 12:01:42 -0400765 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200766 return ret;
767}
768
Holger Schurig208fdd22007-05-25 12:17:06 -0400769static int if_usb_unregister_dev(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200770{
771 int ret = 0;
772
773 /* Need to send a Reset command to device before USB resources freed
774 * and wlan_remove_card() called, then device can handle FW download
775 * again.
776 */
777 if (priv)
Holger Schurig435a1ac2007-05-25 12:41:52 -0400778 if_usb_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200779
780 return ret;
781}
782
783
784/**
785 * @brief This function register usb device and initialize parameter
786 * @param priv pointer to wlan_private
787 * @return 0 or -1
788 */
Holger Schurig208fdd22007-05-25 12:17:06 -0400789static int if_usb_register_dev(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200790{
Holger Schurig634b8f42007-05-25 13:05:16 -0400791 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Holger Schurig9012b282007-05-25 11:27:16 -0400792
793 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200794
795 cardp->priv = priv;
Holger Schurig634b8f42007-05-25 13:05:16 -0400796 cardp->eth_dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200797 priv->hotplug_device = &(cardp->udev->dev);
798
Holger Schurig9012b282007-05-25 11:27:16 -0400799 lbs_deb_usbd(&cardp->udev->dev, "udev pointer is at %p\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200800 cardp->udev);
801
Holger Schurig9012b282007-05-25 11:27:16 -0400802 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200803 return 0;
804}
805
806
807
Holger Schurig208fdd22007-05-25 12:17:06 -0400808static int if_usb_prog_firmware(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200809{
Holger Schurig634b8f42007-05-25 13:05:16 -0400810 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200811 int i = 0;
812 static int reset_count = 10;
Holger Schurig9012b282007-05-25 11:27:16 -0400813 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200814
Holger Schurig9012b282007-05-25 11:27:16 -0400815 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200816
817 cardp->rinfo.priv = priv;
818
819restart:
820 if (if_usb_submit_rx_urb_fwload(priv) < 0) {
Holger Schurig9012b282007-05-25 11:27:16 -0400821 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
822 ret = -1;
823 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200824 }
825
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200826 cardp->bootcmdresp = 0;
827 do {
828 int j = 0;
829 i++;
830 /* Issue Boot command = 1, Boot from Download-FW */
831 if_usb_issue_boot_command(priv, BOOT_CMD_FW_BY_USB);
832 /* wait for command response */
833 do {
834 j++;
835 msleep_interruptible(100);
836 } while (cardp->bootcmdresp == 0 && j < 10);
837 } while (cardp->bootcmdresp == 0 && i < 5);
838
839 if (cardp->bootcmdresp == 0) {
840 if (--reset_count >= 0) {
841 libertas_do_reset(priv);
842 goto restart;
843 }
844 return -1;
845 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200846
847 i = 0;
848 priv->adapter->fw_ready = 0;
849
850 cardp->totalbytes = 0;
851 cardp->fwlastblksent = 0;
852 cardp->CRC_OK = 1;
853 cardp->fwdnldover = 0;
854 cardp->fwseqnum = -1;
855 cardp->totalbytes = 0;
856 cardp->fwfinalblk = 0;
857
858 if_prog_firmware(priv);
859
860 do {
Holger Schurig9012b282007-05-25 11:27:16 -0400861 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200862 i++;
863 msleep_interruptible(100);
864 if (priv->adapter->surpriseremoved || i >= 20)
865 break;
866 } while (!cardp->fwdnldover);
867
868 if (!cardp->fwdnldover) {
869 lbs_pr_info("failed to load fw, resetting device!\n");
870 if (--reset_count >= 0) {
871 libertas_do_reset(priv);
872 goto restart;
873 }
874
875 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
Holger Schurig9012b282007-05-25 11:27:16 -0400876 ret = -1;
877 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200878 }
879
880 if_usb_submit_rx_urb(priv);
881
882 /* Delay 200 ms to waiting for the FW ready */
883 msleep_interruptible(200);
884
885 priv->adapter->fw_ready = 1;
886
Holger Schurig9012b282007-05-25 11:27:16 -0400887done:
888 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
889 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200890}
891
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200892#ifdef CONFIG_PM
893static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
894{
895 struct usb_card_rec *cardp = usb_get_intfdata(intf);
896 wlan_private *priv = cardp->priv;
897
Holger Schurig9012b282007-05-25 11:27:16 -0400898 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200899
900 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
901 return -1;
902
903 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400904 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200905
906 /* Unlink tx & rx urb */
907 usb_kill_urb(cardp->tx_urb);
908 usb_kill_urb(cardp->rx_urb);
909
910 cardp->rx_urb_recall = 1;
911
Holger Schurig9012b282007-05-25 11:27:16 -0400912 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200913 return 0;
914}
915
916static int if_usb_resume(struct usb_interface *intf)
917{
918 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Javier Cardona51d84f52007-05-25 12:06:56 -0400919 wlan_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200920
Holger Schurig9012b282007-05-25 11:27:16 -0400921 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200922
923 cardp->rx_urb_recall = 0;
924
925 if_usb_submit_rx_urb(cardp->priv);
926
927 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400928 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200929
Holger Schurig9012b282007-05-25 11:27:16 -0400930 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200931 return 0;
932}
933#else
934#define if_usb_suspend NULL
935#define if_usb_resume NULL
936#endif
937
938static struct usb_driver if_usb_driver = {
939 /* driver name */
940 .name = usbdriver_name,
941 /* probe function name */
942 .probe = if_usb_probe,
943 /* disconnect function name */
944 .disconnect = if_usb_disconnect,
945 /* device signature table */
946 .id_table = if_usb_table,
947 .suspend = if_usb_suspend,
948 .resume = if_usb_resume,
949};
950
Holger Schurig084708b2007-05-25 12:37:58 -0400951static int if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200952{
Holger Schurig084708b2007-05-25 12:37:58 -0400953 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200954
Holger Schurig084708b2007-05-25 12:37:58 -0400955 lbs_deb_enter(LBS_DEB_MAIN);
956
957 if (libertas_fw_name == NULL) {
958 libertas_fw_name = default_fw_name;
959 }
960
961 ret = usb_register(&if_usb_driver);
962
963 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
964 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200965}
966
Holger Schurig084708b2007-05-25 12:37:58 -0400967static void if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200968{
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -0400969 struct usb_card_rec *cardp, *cardp_temp;
Holger Schurig3874d0f2007-05-25 12:01:42 -0400970
Holger Schurig084708b2007-05-25 12:37:58 -0400971 lbs_deb_enter(LBS_DEB_MAIN);
972
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -0400973 list_for_each_entry_safe(cardp, cardp_temp, &usb_devices, list)
Holger Schurig435a1ac2007-05-25 12:41:52 -0400974 if_usb_reset_device((wlan_private *) cardp->priv);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400975
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200976 /* API unregisters the driver from USB subsystem */
977 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -0400978
979 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200980}
Holger Schurig084708b2007-05-25 12:37:58 -0400981
982module_init(if_usb_init_module);
983module_exit(if_usb_exit_module);
984
985MODULE_DESCRIPTION("8388 USB WLAN Driver");
986MODULE_AUTHOR("Marvell International Ltd.");
987MODULE_LICENSE("GPL");