blob: ac1cfc253fdc2c9693b336df8776f99d47a4ef7b [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"
Dan Williamseedc2a32007-08-02 11:36:22 -040018#include "decl.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020019
20#define MESSAGE_HEADER_LEN 4
21
22static const char usbdriver_name[] = "usb8xxx";
Holger Schurig084708b2007-05-25 12:37:58 -040023static u8 *default_fw_name = "usb8388.bin";
24
25char *libertas_fw_name = NULL;
26module_param_named(fw_name, libertas_fw_name, charp, 0644);
27
Holger Schurig435a1ac2007-05-25 12:41:52 -040028/*
29 * We need to send a RESET command to all USB devices before
30 * we tear down the USB connection. Otherwise we would not
31 * be able to re-init device the device if the module gets
32 * loaded again. This is a list of all initialized USB devices,
33 * for the reset code see if_usb_reset_device()
34*/
35static LIST_HEAD(usb_devices);
Holger Schurig3874d0f2007-05-25 12:01:42 -040036
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020037static struct usb_device_id if_usb_table[] = {
38 /* Enter the device signature inside */
Holger Schurig66fcc552007-05-25 00:11:58 -040039 { USB_DEVICE(0x1286, 0x2001) },
40 { USB_DEVICE(0x05a3, 0x8388) },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020041 {} /* Terminating entry */
42};
43
44MODULE_DEVICE_TABLE(usb, if_usb_table);
45
46static void if_usb_receive(struct urb *urb);
47static void if_usb_receive_fwload(struct urb *urb);
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)
Dan Williamsfe336152007-08-02 11:32:25 -040080 wake_up_interruptible(&priv->waitq);
Dan Williams0aef64d2007-08-02 11:31:18 -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 Schurig1df4e8f2007-08-02 11:45:12 -0400219 if (libertas_activate_card(priv))
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
Dan Williamseedc2a32007-08-02 11:36:22 -0400358static int if_usb_reset_device(wlan_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200359{
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
Dan Williamseedc2a32007-08-02 11:36:22 -0400365 /* Try a USB port reset first, if that fails send the reset
366 * command to the firmware.
367 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200368 ret = usb_reset_device(cardp->udev);
369 if (!ret) {
370 msleep(10);
Dan Williamseedc2a32007-08-02 11:36:22 -0400371 ret = libertas_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200372 msleep(10);
373 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400374
375 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
376
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200377 return ret;
378}
379
380/**
381 * @brief This function transfer the data to the device.
382 * @param priv pointer to wlan_private
383 * @param payload pointer to payload data
384 * @param nb data length
385 * @return 0 or -1
386 */
387int usb_tx_block(wlan_private * priv, u8 * payload, u16 nb)
388{
389 /* pointer to card structure */
Holger Schurig634b8f42007-05-25 13:05:16 -0400390 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200391 int ret = -1;
392
393 /* check if device is removed */
394 if (priv->adapter->surpriseremoved) {
Holger Schurig9012b282007-05-25 11:27:16 -0400395 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200396 goto tx_ret;
397 }
398
399 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
400 usb_sndbulkpipe(cardp->udev,
401 cardp->bulk_out_endpointAddr),
402 payload, nb, if_usb_write_bulk_callback, priv);
403
404 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
405
406 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
407 /* transfer failed */
Holger Schurig9012b282007-05-25 11:27:16 -0400408 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200409 ret = -1;
410 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400411 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200412 ret = 0;
413 }
414
415tx_ret:
416 return ret;
417}
418
419static int __if_usb_submit_rx_urb(wlan_private * priv,
420 void (*callbackfn)
421 (struct urb *urb))
422{
Holger Schurig634b8f42007-05-25 13:05:16 -0400423 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200424 struct sk_buff *skb;
425 struct read_cb_info *rinfo = &cardp->rinfo;
426 int ret = -1;
427
428 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
429 lbs_pr_err("No free skb\n");
430 goto rx_ret;
431 }
432
433 rinfo->skb = skb;
434
435 /* Fill the receive configuration URB and initialise the Rx call back */
436 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
437 usb_rcvbulkpipe(cardp->udev,
438 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400439 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200440 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
441 rinfo);
442
443 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
444
Holger Schurig9012b282007-05-25 11:27:16 -0400445 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
447 /* handle failure conditions */
Holger Schurig9012b282007-05-25 11:27:16 -0400448 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200449 ret = -1;
450 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400451 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200452 ret = 0;
453 }
454
455rx_ret:
456 return ret;
457}
458
459static inline int if_usb_submit_rx_urb_fwload(wlan_private * priv)
460{
461 return __if_usb_submit_rx_urb(priv, &if_usb_receive_fwload);
462}
463
464static inline int if_usb_submit_rx_urb(wlan_private * priv)
465{
466 return __if_usb_submit_rx_urb(priv, &if_usb_receive);
467}
468
469static void if_usb_receive_fwload(struct urb *urb)
470{
471 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
472 wlan_private *priv = rinfo->priv;
473 struct sk_buff *skb = rinfo->skb;
Holger Schurig634b8f42007-05-25 13:05:16 -0400474 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200475 struct fwsyncheader *syncfwheader;
476 struct bootcmdrespStr bootcmdresp;
477
478 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400479 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200480 "URB status is failed during fw load\n");
481 kfree_skb(skb);
482 return;
483 }
484
485 if (cardp->bootcmdresp == 0) {
486 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
487 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400488 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200489 kfree_skb(skb);
490 if_usb_submit_rx_urb_fwload(priv);
491 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400492 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200493 "Received valid boot command response\n");
494 return;
495 }
David Woodhouse981f1872007-05-25 23:36:54 -0400496 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200497 lbs_pr_info(
498 "boot cmd response wrong magic number (0x%x)\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400499 le32_to_cpu(bootcmdresp.u32magicnumber));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
501 lbs_pr_info(
502 "boot cmd response cmd_tag error (%d)\n",
503 bootcmdresp.u8cmd_tag);
504 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
505 lbs_pr_info(
506 "boot cmd response result error (%d)\n",
507 bootcmdresp.u8result);
508 } else {
509 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400510 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200511 "Received valid boot command response\n");
512 }
513 kfree_skb(skb);
514 if_usb_submit_rx_urb_fwload(priv);
515 return;
516 }
517
518 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
519 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400520 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200521 kfree_skb(skb);
522 return;
523 }
524
525 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
526 sizeof(struct fwsyncheader));
527
528 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400529 /*
530 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200531 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400532 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533 "FW received Blk seqnum = %d\n",
534 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400535 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200536 cardp->CRC_OK = 1;
537 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400538 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200539 "FW received Blk with CRC error\n");
540 cardp->CRC_OK = 0;
541 }
542
543 kfree_skb(skb);
544
545 if (cardp->fwfinalblk) {
546 cardp->fwdnldover = 1;
547 goto exit;
548 }
549
550 if_prog_firmware(priv);
551
552 if_usb_submit_rx_urb_fwload(priv);
553exit:
554 kfree(syncfwheader);
555
556 return;
557
558}
559
560#define MRVDRV_MIN_PKT_LEN 30
561
562static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
563 struct usb_card_rec *cardp,
564 wlan_private *priv)
565{
566 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
567 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400568 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200569 "Packet length is Invalid\n");
570 kfree_skb(skb);
571 return;
572 }
573
574 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
575 skb_put(skb, recvlength);
576 skb_pull(skb, MESSAGE_HEADER_LEN);
577 libertas_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400578 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200579}
580
581static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
582 struct sk_buff *skb,
583 struct usb_card_rec *cardp,
584 wlan_private *priv)
585{
586 u8 *cmdbuf;
587 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400588 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200589 "The receive buffer is too large\n");
590 kfree_skb(skb);
591 return;
592 }
593
594 if (!in_interrupt())
595 BUG();
596
597 spin_lock(&priv->adapter->driver_lock);
598 /* take care of cur_cmd = NULL case by reading the
599 * data to clear the interrupt */
600 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400601 cmdbuf = priv->upld_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200602 priv->adapter->hisregcpy &= ~his_cmdupldrdy;
603 } else
604 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
605
606 cardp->usb_int_cause |= his_cmdupldrdy;
Holger Schurig634b8f42007-05-25 13:05:16 -0400607 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200608 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400609 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200610
611 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400612 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200613 spin_unlock(&priv->adapter->driver_lock);
614
Holger Schurig9012b282007-05-25 11:27:16 -0400615 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200616 "Wake up main thread to handle cmd response\n");
617
618 return;
619}
620
621/**
622 * @brief This function reads of the packet into the upload buff,
623 * wake up the main thread and initialise the Rx callack.
624 *
625 * @param urb pointer to struct urb
626 * @return N/A
627 */
628static void if_usb_receive(struct urb *urb)
629{
630 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
631 wlan_private *priv = rinfo->priv;
632 struct sk_buff *skb = rinfo->skb;
Holger Schurig634b8f42007-05-25 13:05:16 -0400633 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634
635 int recvlength = urb->actual_length;
636 u8 *recvbuff = NULL;
637 u32 recvtype;
638
Holger Schurig9012b282007-05-25 11:27:16 -0400639 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200640
641 if (recvlength) {
642 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400643 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200644 "URB status is failed\n");
645 kfree_skb(skb);
646 goto setup_for_next;
647 }
648
649 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
650 memcpy(&recvtype, recvbuff, sizeof(u32));
Holger Schurig9012b282007-05-25 11:27:16 -0400651 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200652 "Recv length = 0x%x\n", recvlength);
Holger Schurig9012b282007-05-25 11:27:16 -0400653 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200654 "Receive type = 0x%X\n", recvtype);
655 recvtype = le32_to_cpu(recvtype);
Holger Schurig9012b282007-05-25 11:27:16 -0400656 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200657 "Receive type after = 0x%X\n", recvtype);
658 } else if (urb->status)
659 goto rx_exit;
660
661
662 switch (recvtype) {
663 case CMD_TYPE_DATA:
664 process_cmdtypedata(recvlength, skb, cardp, priv);
665 break;
666
667 case CMD_TYPE_REQUEST:
668 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
669 break;
670
671 case CMD_TYPE_INDICATION:
672 /* Event cause handling */
673 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400674 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400675 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200676 cardp->usb_event_cause);
677 if (cardp->usb_event_cause & 0xffff0000) {
678 libertas_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400679 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200680 break;
681 }
David Woodhouse981f1872007-05-25 23:36:54 -0400682 cardp->usb_event_cause <<= 3;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683 cardp->usb_int_cause |= his_cardevent;
684 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400685 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200686 spin_unlock(&priv->adapter->driver_lock);
687 goto rx_exit;
688 default:
689 kfree_skb(skb);
690 break;
691 }
692
693setup_for_next:
694 if_usb_submit_rx_urb(priv);
695rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400696 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697}
698
699/**
700 * @brief This function downloads data to FW
701 * @param priv pointer to wlan_private structure
702 * @param type type of data
703 * @param buf pointer to data buffer
704 * @param len number of bytes
705 * @return 0 or -1
706 */
Holger Schurig208fdd22007-05-25 12:17:06 -0400707static int if_usb_host_to_card(wlan_private * priv, u8 type, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200708{
709 int ret = -1;
710 u32 tmp;
Holger Schurig634b8f42007-05-25 13:05:16 -0400711 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200712
Holger Schurig9012b282007-05-25 11:27:16 -0400713 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
714 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200715
716 if (type == MVMS_CMD) {
717 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400718 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
720 MESSAGE_HEADER_LEN);
721
722 } else {
723 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400724 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200725 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
726 MESSAGE_HEADER_LEN);
727 }
728
729 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
730
731 ret =
732 usb_tx_block(priv, cardp->bulk_out_buffer, nb + MESSAGE_HEADER_LEN);
733
734 return ret;
735}
736
737/* called with adapter->driver_lock held */
Holger Schurig208fdd22007-05-25 12:17:06 -0400738static int if_usb_get_int_status(wlan_private * priv, u8 * ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200739{
Holger Schurig634b8f42007-05-25 13:05:16 -0400740 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200741
742 *ireg = cardp->usb_int_cause;
743 cardp->usb_int_cause = 0;
744
Holger Schurig9012b282007-05-25 11:27:16 -0400745 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746
747 return 0;
748}
749
Holger Schurig208fdd22007-05-25 12:17:06 -0400750static int if_usb_read_event_cause(wlan_private * priv)
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 priv->adapter->eventcause = cardp->usb_event_cause;
754 /* Re-submit rx urb here to avoid event lost issue */
755 if_usb_submit_rx_urb(priv);
756 return 0;
757}
758
Holger Schurig208fdd22007-05-25 12:17:06 -0400759static int if_usb_unregister_dev(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200760{
761 int ret = 0;
762
763 /* Need to send a Reset command to device before USB resources freed
764 * and wlan_remove_card() called, then device can handle FW download
765 * again.
766 */
767 if (priv)
Dan Williamseedc2a32007-08-02 11:36:22 -0400768 libertas_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200769
770 return ret;
771}
772
773
774/**
775 * @brief This function register usb device and initialize parameter
776 * @param priv pointer to wlan_private
777 * @return 0 or -1
778 */
Holger Schurig208fdd22007-05-25 12:17:06 -0400779static int if_usb_register_dev(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200780{
Holger Schurig634b8f42007-05-25 13:05:16 -0400781 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Holger Schurig9012b282007-05-25 11:27:16 -0400782
783 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200784
785 cardp->priv = priv;
Holger Schurig634b8f42007-05-25 13:05:16 -0400786 cardp->eth_dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200787 priv->hotplug_device = &(cardp->udev->dev);
788
Holger Schurig9012b282007-05-25 11:27:16 -0400789 lbs_deb_usbd(&cardp->udev->dev, "udev pointer is at %p\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200790 cardp->udev);
791
Holger Schurig9012b282007-05-25 11:27:16 -0400792 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200793 return 0;
794}
795
Dan Williams9e22cb62007-08-02 11:14:49 -0400796/**
797 * @brief This function issues Boot command to the Boot2 code
798 * @param ivalue 1:Boot from FW by USB-Download
799 * 2:Boot from FW in EEPROM
800 * @return 0
801 */
802static int if_usb_issue_boot_command(wlan_private *priv, int ivalue)
803{
804 struct usb_card_rec *cardp = priv->card;
805 struct bootcmdstr sbootcmd;
806 int i;
807
808 /* Prepare command */
809 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
810 sbootcmd.u8cmd_tag = ivalue;
811 for (i=0; i<11; i++)
812 sbootcmd.au8dumy[i]=0x00;
813 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
814
815 /* Issue command */
816 usb_tx_block(priv, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
817
818 return 0;
819}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200820
821
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400822static int if_usb_do_prog_firmware(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200823{
Holger Schurig634b8f42007-05-25 13:05:16 -0400824 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200825 int i = 0;
826 static int reset_count = 10;
Holger Schurig9012b282007-05-25 11:27:16 -0400827 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200828
Holger Schurig9012b282007-05-25 11:27:16 -0400829 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200830
831 cardp->rinfo.priv = priv;
832
833restart:
834 if (if_usb_submit_rx_urb_fwload(priv) < 0) {
Holger Schurig9012b282007-05-25 11:27:16 -0400835 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
836 ret = -1;
837 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200838 }
839
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200840 cardp->bootcmdresp = 0;
841 do {
842 int j = 0;
843 i++;
844 /* Issue Boot command = 1, Boot from Download-FW */
845 if_usb_issue_boot_command(priv, BOOT_CMD_FW_BY_USB);
846 /* wait for command response */
847 do {
848 j++;
849 msleep_interruptible(100);
850 } while (cardp->bootcmdresp == 0 && j < 10);
851 } while (cardp->bootcmdresp == 0 && i < 5);
852
853 if (cardp->bootcmdresp == 0) {
854 if (--reset_count >= 0) {
Dan Williamseedc2a32007-08-02 11:36:22 -0400855 if_usb_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200856 goto restart;
857 }
858 return -1;
859 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200860
861 i = 0;
862 priv->adapter->fw_ready = 0;
863
864 cardp->totalbytes = 0;
865 cardp->fwlastblksent = 0;
866 cardp->CRC_OK = 1;
867 cardp->fwdnldover = 0;
868 cardp->fwseqnum = -1;
869 cardp->totalbytes = 0;
870 cardp->fwfinalblk = 0;
871
872 if_prog_firmware(priv);
873
874 do {
Holger Schurig9012b282007-05-25 11:27:16 -0400875 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200876 i++;
877 msleep_interruptible(100);
878 if (priv->adapter->surpriseremoved || i >= 20)
879 break;
880 } while (!cardp->fwdnldover);
881
882 if (!cardp->fwdnldover) {
883 lbs_pr_info("failed to load fw, resetting device!\n");
884 if (--reset_count >= 0) {
Dan Williamseedc2a32007-08-02 11:36:22 -0400885 if_usb_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200886 goto restart;
887 }
888
889 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
Holger Schurig9012b282007-05-25 11:27:16 -0400890 ret = -1;
891 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200892 }
893
894 if_usb_submit_rx_urb(priv);
895
896 /* Delay 200 ms to waiting for the FW ready */
897 msleep_interruptible(200);
898
899 priv->adapter->fw_ready = 1;
900
Holger Schurig9012b282007-05-25 11:27:16 -0400901done:
902 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
903 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200904}
905
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400906/**
907 * @brief This function checks the validity of Boot2/FW image.
908 *
909 * @param data pointer to image
910 * len image length
911 * @return 0 or -1
912 */
913static int check_fwfile_format(u8 *data, u32 totlen)
914{
915 u32 bincmd, exit;
916 u32 blksize, offset, len;
917 int ret;
918
919 ret = 1;
920 exit = len = 0;
921
922 do {
923 struct fwheader *fwh = (void *)data;
924
925 bincmd = le32_to_cpu(fwh->dnldcmd);
926 blksize = le32_to_cpu(fwh->datalength);
927 switch (bincmd) {
928 case FW_HAS_DATA_TO_RECV:
929 offset = sizeof(struct fwheader) + blksize;
930 data += offset;
931 len += offset;
932 if (len >= totlen)
933 exit = 1;
934 break;
935 case FW_HAS_LAST_BLOCK:
936 exit = 1;
937 ret = 0;
938 break;
939 default:
940 exit = 1;
941 break;
942 }
943 } while (!exit);
944
945 if (ret)
946 lbs_pr_err("firmware file format check FAIL\n");
947 else
948 lbs_deb_fw("firmware file format check PASS\n");
949
950 return ret;
951}
952
953
954static int if_usb_prog_firmware(wlan_private *priv)
955{
956 int ret = -1;
957
958 lbs_deb_enter(LBS_DEB_FW);
959
960 if ((ret = request_firmware(&priv->firmware, libertas_fw_name,
961 priv->hotplug_device)) < 0) {
962 lbs_pr_err("request_firmware() failed with %#x\n", ret);
963 lbs_pr_err("firmware %s not found\n", libertas_fw_name);
964 goto done;
965 }
966
967 if (check_fwfile_format(priv->firmware->data, priv->firmware->size)) {
968 release_firmware(priv->firmware);
969 goto done;
970 }
971
972 ret = if_usb_do_prog_firmware(priv);
973
974 release_firmware(priv->firmware);
975done:
976 return ret;
977}
978
979
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200980#ifdef CONFIG_PM
981static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
982{
983 struct usb_card_rec *cardp = usb_get_intfdata(intf);
984 wlan_private *priv = cardp->priv;
985
Holger Schurig9012b282007-05-25 11:27:16 -0400986 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200987
988 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
989 return -1;
990
991 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400992 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200993
994 /* Unlink tx & rx urb */
995 usb_kill_urb(cardp->tx_urb);
996 usb_kill_urb(cardp->rx_urb);
997
998 cardp->rx_urb_recall = 1;
999
Holger Schurig9012b282007-05-25 11:27:16 -04001000 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001001 return 0;
1002}
1003
1004static int if_usb_resume(struct usb_interface *intf)
1005{
1006 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Javier Cardona51d84f52007-05-25 12:06:56 -04001007 wlan_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001008
Holger Schurig9012b282007-05-25 11:27:16 -04001009 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001010
1011 cardp->rx_urb_recall = 0;
1012
1013 if_usb_submit_rx_urb(cardp->priv);
1014
1015 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -04001016 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001017
Holger Schurig9012b282007-05-25 11:27:16 -04001018 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001019 return 0;
1020}
1021#else
1022#define if_usb_suspend NULL
1023#define if_usb_resume NULL
1024#endif
1025
1026static struct usb_driver if_usb_driver = {
1027 /* driver name */
1028 .name = usbdriver_name,
1029 /* probe function name */
1030 .probe = if_usb_probe,
1031 /* disconnect function name */
1032 .disconnect = if_usb_disconnect,
1033 /* device signature table */
1034 .id_table = if_usb_table,
1035 .suspend = if_usb_suspend,
1036 .resume = if_usb_resume,
1037};
1038
Holger Schurig084708b2007-05-25 12:37:58 -04001039static int if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001040{
Holger Schurig084708b2007-05-25 12:37:58 -04001041 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001042
Holger Schurig084708b2007-05-25 12:37:58 -04001043 lbs_deb_enter(LBS_DEB_MAIN);
1044
1045 if (libertas_fw_name == NULL) {
1046 libertas_fw_name = default_fw_name;
1047 }
1048
1049 ret = usb_register(&if_usb_driver);
1050
1051 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1052 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001053}
1054
Holger Schurig084708b2007-05-25 12:37:58 -04001055static void if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001056{
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -04001057 struct usb_card_rec *cardp, *cardp_temp;
Holger Schurig3874d0f2007-05-25 12:01:42 -04001058
Holger Schurig084708b2007-05-25 12:37:58 -04001059 lbs_deb_enter(LBS_DEB_MAIN);
1060
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -04001061 list_for_each_entry_safe(cardp, cardp_temp, &usb_devices, list)
Dan Williamseedc2a32007-08-02 11:36:22 -04001062 libertas_reset_device((wlan_private *) cardp->priv);
Holger Schurig3874d0f2007-05-25 12:01:42 -04001063
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001064 /* API unregisters the driver from USB subsystem */
1065 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001066
1067 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001068}
Holger Schurig084708b2007-05-25 12:37:58 -04001069
1070module_init(if_usb_init_module);
1071module_exit(if_usb_exit_module);
1072
1073MODULE_DESCRIPTION("8388 USB WLAN Driver");
1074MODULE_AUTHOR("Marvell International Ltd.");
1075MODULE_LICENSE("GPL");