blob: c28d9747b86508cf5dccac302021c54ece134859 [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
Holger Schurigac558ca2007-08-02 11:49:06 -040025static char *libertas_fw_name = NULL;
Holger Schurig084708b2007-05-25 12:37:58 -040026module_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 *);
Holger Schurigac558ca2007-08-02 11:49:06 -040054static int usb_tx_block(wlan_private *priv, u8 *payload, u16 nb);
55static void if_usb_free(struct usb_card_rec *cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020056
57/**
58 * @brief call back function to handle the status of the URB
59 * @param urb pointer to urb structure
60 * @return N/A
61 */
62static void if_usb_write_bulk_callback(struct urb *urb)
63{
64 wlan_private *priv = (wlan_private *) (urb->context);
65 wlan_adapter *adapter = priv->adapter;
Holger Schurig634b8f42007-05-25 13:05:16 -040066 struct net_device *dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020067
68 /* handle the transmission complete validations */
69
70 if (urb->status != 0) {
71 /* print the failure status number for debug */
Luis Carlos Cobob46794d2007-05-25 13:10:18 -040072 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020073 } else {
Holger Schurig9012b282007-05-25 11:27:16 -040074 /*
75 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
76 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020077 urb->actual_length);
Holger Schurig9012b282007-05-25 11:27:16 -040078 */
Holger Schurig634b8f42007-05-25 13:05:16 -040079 priv->dnld_sent = DNLD_RES_RECEIVED;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020080 /* Wake main thread if commands are pending */
81 if (!adapter->cur_cmd)
Dan Williamsfe336152007-08-02 11:32:25 -040082 wake_up_interruptible(&priv->waitq);
Dan Williams0aef64d2007-08-02 11:31:18 -040083 if ((adapter->connect_status == LIBERTAS_CONNECTED)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020084 netif_wake_queue(dev);
Javier Cardona51d84f52007-05-25 12:06:56 -040085 netif_wake_queue(priv->mesh_dev);
86 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020087 }
88
89 return;
90}
91
92/**
93 * @brief free tx/rx urb, skb and rx buffer
94 * @param cardp pointer usb_card_rec
95 * @return N/A
96 */
Holger Schurigac558ca2007-08-02 11:49:06 -040097static void if_usb_free(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020098{
Holger Schurig9012b282007-05-25 11:27:16 -040099 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200100
101 /* Unlink tx & rx urb */
102 usb_kill_urb(cardp->tx_urb);
103 usb_kill_urb(cardp->rx_urb);
104
105 usb_free_urb(cardp->tx_urb);
106 cardp->tx_urb = NULL;
107
108 usb_free_urb(cardp->rx_urb);
109 cardp->rx_urb = NULL;
110
111 kfree(cardp->bulk_out_buffer);
112 cardp->bulk_out_buffer = NULL;
113
Holger Schurig9012b282007-05-25 11:27:16 -0400114 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200115}
116
117/**
118 * @brief sets the configuration values
119 * @param ifnum interface number
120 * @param id pointer to usb_device_id
121 * @return 0 on success, error code on failure
122 */
123static int if_usb_probe(struct usb_interface *intf,
124 const struct usb_device_id *id)
125{
126 struct usb_device *udev;
127 struct usb_host_interface *iface_desc;
128 struct usb_endpoint_descriptor *endpoint;
Holger Schurig78523da2007-05-25 11:49:19 -0400129 wlan_private *priv;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400130 struct usb_card_rec *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200131 int i;
132
133 udev = interface_to_usbdev(intf);
134
Holger Schurig435a1ac2007-05-25 12:41:52 -0400135 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
136 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200137 lbs_pr_err("Out of memory allocating private data.\n");
138 goto error;
139 }
140
Holger Schurig435a1ac2007-05-25 12:41:52 -0400141 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200142 iface_desc = intf->cur_altsetting;
143
Holger Schurig9012b282007-05-25 11:27:16 -0400144 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200145 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400146 le16_to_cpu(udev->descriptor.bcdUSB),
147 udev->descriptor.bDeviceClass,
148 udev->descriptor.bDeviceSubClass,
149 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200150
151 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
152 endpoint = &iface_desc->endpoint[i].desc;
153 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
154 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
155 USB_ENDPOINT_XFER_BULK)) {
156 /* we found a bulk in endpoint */
Holger Schurig9012b282007-05-25 11:27:16 -0400157 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400158 le16_to_cpu(endpoint->wMaxPacketSize));
159 if (!(cardp->rx_urb = 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 =
David Woodhouse981f1872007-05-25 23:36:54 -0400167 le16_to_cpu(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 */
David Woodhouse981f1872007-05-25 23:36:54 -0400181 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400182 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200183 "Tx URB allocation failed\n");
184 goto dealloc;
185 }
186
Holger Schurig435a1ac2007-05-25 12:41:52 -0400187 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400188 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400189 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400190 "Bulk out size is %d\n",
191 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400192 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200193 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400194 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200195 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400196 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200197 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
198 GFP_KERNEL);
199
Holger Schurig435a1ac2007-05-25 12:41:52 -0400200 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400201 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200202 "Could not allocate buffer\n");
203 goto dealloc;
204 }
205 }
206 }
207
Dan Williams7732ca42007-05-25 13:13:25 -0400208 if (!(priv = libertas_add_card(cardp, &udev->dev)))
Holger Schurig78523da2007-05-25 11:49:19 -0400209 goto dealloc;
Holger Schurig3874d0f2007-05-25 12:01:42 -0400210
Dan Williams7732ca42007-05-25 13:13:25 -0400211 if (libertas_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400212 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400213
Holger Schurig208fdd22007-05-25 12:17:06 -0400214 priv->hw_register_dev = if_usb_register_dev;
215 priv->hw_unregister_dev = if_usb_unregister_dev;
216 priv->hw_prog_firmware = if_usb_prog_firmware;
217 priv->hw_host_to_card = if_usb_host_to_card;
218 priv->hw_get_int_status = if_usb_get_int_status;
219 priv->hw_read_event_cause = if_usb_read_event_cause;
220
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400221 if (libertas_activate_card(priv))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400222 goto err_activate_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400223
Holger Schurig435a1ac2007-05-25 12:41:52 -0400224 list_add_tail(&cardp->list, &usb_devices);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400225
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200226 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400227 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200228
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200229 return 0;
230
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400231err_activate_card:
Dan Williamsc7236832007-05-25 13:35:23 -0400232 libertas_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400233err_add_mesh:
Holger Schurig634b8f42007-05-25 13:05:16 -0400234 free_netdev(priv->dev);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400235 kfree(priv->adapter);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400237 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200238
239error:
240 return -ENOMEM;
241}
242
243/**
244 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400245 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200246 * @return N/A
247 */
248static void if_usb_disconnect(struct usb_interface *intf)
249{
250 struct usb_card_rec *cardp = usb_get_intfdata(intf);
251 wlan_private *priv = (wlan_private *) cardp->priv;
252 wlan_adapter *adapter = NULL;
253
254 adapter = priv->adapter;
255
256 /*
257 * Update Surprise removed to TRUE
258 */
259 adapter->surpriseremoved = 1;
260
Holger Schurig435a1ac2007-05-25 12:41:52 -0400261 list_del(&cardp->list);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400262
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200263 /* card is removed and we can call wlan_remove_card */
Holger Schurig9012b282007-05-25 11:27:16 -0400264 lbs_deb_usbd(&cardp->udev->dev, "call remove card\n");
Holger Schurig084708b2007-05-25 12:37:58 -0400265 libertas_remove_mesh(priv);
266 libertas_remove_card(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200267
268 /* Unlink and free urb */
269 if_usb_free(cardp);
270
271 usb_set_intfdata(intf, NULL);
272 usb_put_dev(interface_to_usbdev(intf));
273
274 return;
275}
276
277/**
278 * @brief This function download FW
279 * @param priv pointer to wlan_private
280 * @return 0
281 */
282static int if_prog_firmware(wlan_private * priv)
283{
Holger Schurig634b8f42007-05-25 13:05:16 -0400284 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200285 struct FWData *fwdata;
286 struct fwheader *fwheader;
287 u8 *firmware = priv->firmware->data;
288
289 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
290
291 if (!fwdata)
292 return -1;
293
294 fwheader = &fwdata->fwheader;
295
296 if (!cardp->CRC_OK) {
297 cardp->totalbytes = cardp->fwlastblksent;
298 cardp->fwseqnum = cardp->lastseqnum - 1;
299 }
300
Holger Schurig9012b282007-05-25 11:27:16 -0400301 /*
302 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200303 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400304 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200305
306 memcpy(fwheader, &firmware[cardp->totalbytes],
307 sizeof(struct fwheader));
308
309 cardp->fwlastblksent = cardp->totalbytes;
310 cardp->totalbytes += sizeof(struct fwheader);
311
Holger Schurig9012b282007-05-25 11:27:16 -0400312 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200313 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400314 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200315
Holger Schurig9012b282007-05-25 11:27:16 -0400316 /*
317 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400318 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400319 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200320
321 cardp->fwseqnum = cardp->fwseqnum + 1;
322
David Woodhouse981f1872007-05-25 23:36:54 -0400323 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
324 cardp->lastseqnum = cardp->fwseqnum;
325 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200326
David Woodhouse981f1872007-05-25 23:36:54 -0400327 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400328 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400329 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400330 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200331 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
332 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400333 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200334 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
335 usb_tx_block(priv, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
336
David Woodhousebb793e22007-05-25 23:38:14 -0400337 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400338 /*
339 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200340 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400341 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200342 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400343 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200344 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
345 usb_tx_block(priv, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
346 cardp->fwfinalblk = 1;
347 }
348
Holger Schurig9012b282007-05-25 11:27:16 -0400349 /*
350 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200351 "The firmware download is done size is %d\n",
352 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400353 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200354
355 kfree(fwdata);
356
357 return 0;
358}
359
Dan Williamseedc2a32007-08-02 11:36:22 -0400360static int if_usb_reset_device(wlan_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200361{
362 int ret;
Holger Schurig634b8f42007-05-25 13:05:16 -0400363 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200364
Holger Schurig3874d0f2007-05-25 12:01:42 -0400365 lbs_deb_enter(LBS_DEB_USB);
366
Dan Williamseedc2a32007-08-02 11:36:22 -0400367 /* Try a USB port reset first, if that fails send the reset
368 * command to the firmware.
369 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370 ret = usb_reset_device(cardp->udev);
371 if (!ret) {
372 msleep(10);
Dan Williamseedc2a32007-08-02 11:36:22 -0400373 ret = libertas_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200374 msleep(10);
375 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400376
377 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
378
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200379 return ret;
380}
381
382/**
383 * @brief This function transfer the data to the device.
384 * @param priv pointer to wlan_private
385 * @param payload pointer to payload data
386 * @param nb data length
387 * @return 0 or -1
388 */
Holger Schurigac558ca2007-08-02 11:49:06 -0400389static int usb_tx_block(wlan_private * priv, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200390{
391 /* pointer to card structure */
Holger Schurig634b8f42007-05-25 13:05:16 -0400392 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200393 int ret = -1;
394
395 /* check if device is removed */
396 if (priv->adapter->surpriseremoved) {
Holger Schurig9012b282007-05-25 11:27:16 -0400397 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200398 goto tx_ret;
399 }
400
401 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
402 usb_sndbulkpipe(cardp->udev,
403 cardp->bulk_out_endpointAddr),
404 payload, nb, if_usb_write_bulk_callback, priv);
405
406 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
407
408 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
409 /* transfer failed */
Holger Schurig9012b282007-05-25 11:27:16 -0400410 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200411 ret = -1;
412 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400413 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200414 ret = 0;
415 }
416
417tx_ret:
418 return ret;
419}
420
421static int __if_usb_submit_rx_urb(wlan_private * priv,
422 void (*callbackfn)
423 (struct urb *urb))
424{
Holger Schurig634b8f42007-05-25 13:05:16 -0400425 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200426 struct sk_buff *skb;
427 struct read_cb_info *rinfo = &cardp->rinfo;
428 int ret = -1;
429
430 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
431 lbs_pr_err("No free skb\n");
432 goto rx_ret;
433 }
434
435 rinfo->skb = skb;
436
437 /* Fill the receive configuration URB and initialise the Rx call back */
438 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
439 usb_rcvbulkpipe(cardp->udev,
440 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400441 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200442 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
443 rinfo);
444
445 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
446
Holger Schurig9012b282007-05-25 11:27:16 -0400447 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200448 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
449 /* handle failure conditions */
Holger Schurig9012b282007-05-25 11:27:16 -0400450 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451 ret = -1;
452 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400453 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200454 ret = 0;
455 }
456
457rx_ret:
458 return ret;
459}
460
461static inline int if_usb_submit_rx_urb_fwload(wlan_private * priv)
462{
463 return __if_usb_submit_rx_urb(priv, &if_usb_receive_fwload);
464}
465
466static inline int if_usb_submit_rx_urb(wlan_private * priv)
467{
468 return __if_usb_submit_rx_urb(priv, &if_usb_receive);
469}
470
471static void if_usb_receive_fwload(struct urb *urb)
472{
473 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
474 wlan_private *priv = rinfo->priv;
475 struct sk_buff *skb = rinfo->skb;
Holger Schurig634b8f42007-05-25 13:05:16 -0400476 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200477 struct fwsyncheader *syncfwheader;
478 struct bootcmdrespStr bootcmdresp;
479
480 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400481 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200482 "URB status is failed during fw load\n");
483 kfree_skb(skb);
484 return;
485 }
486
487 if (cardp->bootcmdresp == 0) {
488 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
489 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400490 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200491 kfree_skb(skb);
492 if_usb_submit_rx_urb_fwload(priv);
493 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400494 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200495 "Received valid boot command response\n");
496 return;
497 }
David Woodhouse981f1872007-05-25 23:36:54 -0400498 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200499 lbs_pr_info(
500 "boot cmd response wrong magic number (0x%x)\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400501 le32_to_cpu(bootcmdresp.u32magicnumber));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200502 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
503 lbs_pr_info(
504 "boot cmd response cmd_tag error (%d)\n",
505 bootcmdresp.u8cmd_tag);
506 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
507 lbs_pr_info(
508 "boot cmd response result error (%d)\n",
509 bootcmdresp.u8result);
510 } else {
511 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400512 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513 "Received valid boot command response\n");
514 }
515 kfree_skb(skb);
516 if_usb_submit_rx_urb_fwload(priv);
517 return;
518 }
519
520 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
521 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400522 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200523 kfree_skb(skb);
524 return;
525 }
526
527 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
528 sizeof(struct fwsyncheader));
529
530 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400531 /*
532 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400534 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200535 "FW received Blk seqnum = %d\n",
536 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400537 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538 cardp->CRC_OK = 1;
539 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400540 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200541 "FW received Blk with CRC error\n");
542 cardp->CRC_OK = 0;
543 }
544
545 kfree_skb(skb);
546
547 if (cardp->fwfinalblk) {
548 cardp->fwdnldover = 1;
549 goto exit;
550 }
551
552 if_prog_firmware(priv);
553
554 if_usb_submit_rx_urb_fwload(priv);
555exit:
556 kfree(syncfwheader);
557
558 return;
559
560}
561
562#define MRVDRV_MIN_PKT_LEN 30
563
564static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
565 struct usb_card_rec *cardp,
566 wlan_private *priv)
567{
568 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
569 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400570 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200571 "Packet length is Invalid\n");
572 kfree_skb(skb);
573 return;
574 }
575
576 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
577 skb_put(skb, recvlength);
578 skb_pull(skb, MESSAGE_HEADER_LEN);
579 libertas_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400580 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200581}
582
583static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
584 struct sk_buff *skb,
585 struct usb_card_rec *cardp,
586 wlan_private *priv)
587{
588 u8 *cmdbuf;
589 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400590 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200591 "The receive buffer is too large\n");
592 kfree_skb(skb);
593 return;
594 }
595
596 if (!in_interrupt())
597 BUG();
598
599 spin_lock(&priv->adapter->driver_lock);
600 /* take care of cur_cmd = NULL case by reading the
601 * data to clear the interrupt */
602 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400603 cmdbuf = priv->upld_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200604 priv->adapter->hisregcpy &= ~his_cmdupldrdy;
605 } else
606 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
607
608 cardp->usb_int_cause |= his_cmdupldrdy;
Holger Schurig634b8f42007-05-25 13:05:16 -0400609 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200610 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400611 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200612
613 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400614 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200615 spin_unlock(&priv->adapter->driver_lock);
616
Holger Schurig9012b282007-05-25 11:27:16 -0400617 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200618 "Wake up main thread to handle cmd response\n");
619
620 return;
621}
622
623/**
624 * @brief This function reads of the packet into the upload buff,
625 * wake up the main thread and initialise the Rx callack.
626 *
627 * @param urb pointer to struct urb
628 * @return N/A
629 */
630static void if_usb_receive(struct urb *urb)
631{
632 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
633 wlan_private *priv = rinfo->priv;
634 struct sk_buff *skb = rinfo->skb;
Holger Schurig634b8f42007-05-25 13:05:16 -0400635 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200636
637 int recvlength = urb->actual_length;
638 u8 *recvbuff = NULL;
639 u32 recvtype;
640
Holger Schurig9012b282007-05-25 11:27:16 -0400641 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200642
643 if (recvlength) {
644 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400645 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200646 "URB status is failed\n");
647 kfree_skb(skb);
648 goto setup_for_next;
649 }
650
651 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
652 memcpy(&recvtype, recvbuff, sizeof(u32));
Holger Schurig9012b282007-05-25 11:27:16 -0400653 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200654 "Recv length = 0x%x\n", recvlength);
Holger Schurig9012b282007-05-25 11:27:16 -0400655 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200656 "Receive type = 0x%X\n", recvtype);
657 recvtype = le32_to_cpu(recvtype);
Holger Schurig9012b282007-05-25 11:27:16 -0400658 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200659 "Receive type after = 0x%X\n", recvtype);
660 } else if (urb->status)
661 goto rx_exit;
662
663
664 switch (recvtype) {
665 case CMD_TYPE_DATA:
666 process_cmdtypedata(recvlength, skb, cardp, priv);
667 break;
668
669 case CMD_TYPE_REQUEST:
670 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
671 break;
672
673 case CMD_TYPE_INDICATION:
674 /* Event cause handling */
675 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400676 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400677 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200678 cardp->usb_event_cause);
679 if (cardp->usb_event_cause & 0xffff0000) {
680 libertas_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400681 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200682 break;
683 }
David Woodhouse981f1872007-05-25 23:36:54 -0400684 cardp->usb_event_cause <<= 3;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200685 cardp->usb_int_cause |= his_cardevent;
686 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400687 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200688 spin_unlock(&priv->adapter->driver_lock);
689 goto rx_exit;
690 default:
691 kfree_skb(skb);
692 break;
693 }
694
695setup_for_next:
696 if_usb_submit_rx_urb(priv);
697rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400698 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699}
700
701/**
702 * @brief This function downloads data to FW
703 * @param priv pointer to wlan_private structure
704 * @param type type of data
705 * @param buf pointer to data buffer
706 * @param len number of bytes
707 * @return 0 or -1
708 */
Holger Schurig208fdd22007-05-25 12:17:06 -0400709static int if_usb_host_to_card(wlan_private * priv, u8 type, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710{
711 int ret = -1;
712 u32 tmp;
Holger Schurig634b8f42007-05-25 13:05:16 -0400713 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200714
Holger Schurig9012b282007-05-25 11:27:16 -0400715 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
716 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200717
718 if (type == MVMS_CMD) {
719 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400720 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200721 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
722 MESSAGE_HEADER_LEN);
723
724 } else {
725 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400726 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200727 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
728 MESSAGE_HEADER_LEN);
729 }
730
731 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
732
733 ret =
734 usb_tx_block(priv, cardp->bulk_out_buffer, nb + MESSAGE_HEADER_LEN);
735
736 return ret;
737}
738
739/* called with adapter->driver_lock held */
Holger Schurig208fdd22007-05-25 12:17:06 -0400740static int if_usb_get_int_status(wlan_private * priv, u8 * ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200741{
Holger Schurig634b8f42007-05-25 13:05:16 -0400742 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200743
744 *ireg = cardp->usb_int_cause;
745 cardp->usb_int_cause = 0;
746
Holger Schurig9012b282007-05-25 11:27:16 -0400747 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200748
749 return 0;
750}
751
Holger Schurig208fdd22007-05-25 12:17:06 -0400752static int if_usb_read_event_cause(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753{
Holger Schurig634b8f42007-05-25 13:05:16 -0400754 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200755 priv->adapter->eventcause = cardp->usb_event_cause;
756 /* Re-submit rx urb here to avoid event lost issue */
757 if_usb_submit_rx_urb(priv);
758 return 0;
759}
760
Holger Schurig208fdd22007-05-25 12:17:06 -0400761static int if_usb_unregister_dev(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200762{
763 int ret = 0;
764
765 /* Need to send a Reset command to device before USB resources freed
766 * and wlan_remove_card() called, then device can handle FW download
767 * again.
768 */
769 if (priv)
Dan Williamseedc2a32007-08-02 11:36:22 -0400770 libertas_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200771
772 return ret;
773}
774
775
776/**
777 * @brief This function register usb device and initialize parameter
778 * @param priv pointer to wlan_private
779 * @return 0 or -1
780 */
Holger Schurig208fdd22007-05-25 12:17:06 -0400781static int if_usb_register_dev(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200782{
Holger Schurig634b8f42007-05-25 13:05:16 -0400783 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Holger Schurig9012b282007-05-25 11:27:16 -0400784
785 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200786
787 cardp->priv = priv;
Holger Schurig634b8f42007-05-25 13:05:16 -0400788 cardp->eth_dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200789 priv->hotplug_device = &(cardp->udev->dev);
790
Holger Schurig9012b282007-05-25 11:27:16 -0400791 lbs_deb_usbd(&cardp->udev->dev, "udev pointer is at %p\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200792 cardp->udev);
793
Holger Schurig9012b282007-05-25 11:27:16 -0400794 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200795 return 0;
796}
797
Dan Williams9e22cb62007-08-02 11:14:49 -0400798/**
799 * @brief This function issues Boot command to the Boot2 code
800 * @param ivalue 1:Boot from FW by USB-Download
801 * 2:Boot from FW in EEPROM
802 * @return 0
803 */
804static int if_usb_issue_boot_command(wlan_private *priv, int ivalue)
805{
806 struct usb_card_rec *cardp = priv->card;
807 struct bootcmdstr sbootcmd;
808 int i;
809
810 /* Prepare command */
811 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
812 sbootcmd.u8cmd_tag = ivalue;
813 for (i=0; i<11; i++)
814 sbootcmd.au8dumy[i]=0x00;
815 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
816
817 /* Issue command */
818 usb_tx_block(priv, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
819
820 return 0;
821}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200822
823
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400824static int if_usb_do_prog_firmware(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200825{
Holger Schurig634b8f42007-05-25 13:05:16 -0400826 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200827 int i = 0;
828 static int reset_count = 10;
Holger Schurig9012b282007-05-25 11:27:16 -0400829 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200830
Holger Schurig9012b282007-05-25 11:27:16 -0400831 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200832
833 cardp->rinfo.priv = priv;
834
835restart:
836 if (if_usb_submit_rx_urb_fwload(priv) < 0) {
Holger Schurig9012b282007-05-25 11:27:16 -0400837 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
838 ret = -1;
839 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200840 }
841
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200842 cardp->bootcmdresp = 0;
843 do {
844 int j = 0;
845 i++;
846 /* Issue Boot command = 1, Boot from Download-FW */
847 if_usb_issue_boot_command(priv, BOOT_CMD_FW_BY_USB);
848 /* wait for command response */
849 do {
850 j++;
851 msleep_interruptible(100);
852 } while (cardp->bootcmdresp == 0 && j < 10);
853 } while (cardp->bootcmdresp == 0 && i < 5);
854
855 if (cardp->bootcmdresp == 0) {
856 if (--reset_count >= 0) {
Dan Williamseedc2a32007-08-02 11:36:22 -0400857 if_usb_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200858 goto restart;
859 }
860 return -1;
861 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200862
863 i = 0;
864 priv->adapter->fw_ready = 0;
865
866 cardp->totalbytes = 0;
867 cardp->fwlastblksent = 0;
868 cardp->CRC_OK = 1;
869 cardp->fwdnldover = 0;
870 cardp->fwseqnum = -1;
871 cardp->totalbytes = 0;
872 cardp->fwfinalblk = 0;
873
874 if_prog_firmware(priv);
875
876 do {
Holger Schurig9012b282007-05-25 11:27:16 -0400877 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200878 i++;
879 msleep_interruptible(100);
880 if (priv->adapter->surpriseremoved || i >= 20)
881 break;
882 } while (!cardp->fwdnldover);
883
884 if (!cardp->fwdnldover) {
885 lbs_pr_info("failed to load fw, resetting device!\n");
886 if (--reset_count >= 0) {
Dan Williamseedc2a32007-08-02 11:36:22 -0400887 if_usb_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200888 goto restart;
889 }
890
891 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
Holger Schurig9012b282007-05-25 11:27:16 -0400892 ret = -1;
893 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200894 }
895
896 if_usb_submit_rx_urb(priv);
897
898 /* Delay 200 ms to waiting for the FW ready */
899 msleep_interruptible(200);
900
901 priv->adapter->fw_ready = 1;
902
Holger Schurig9012b282007-05-25 11:27:16 -0400903done:
904 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
905 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200906}
907
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400908/**
909 * @brief This function checks the validity of Boot2/FW image.
910 *
911 * @param data pointer to image
912 * len image length
913 * @return 0 or -1
914 */
915static int check_fwfile_format(u8 *data, u32 totlen)
916{
917 u32 bincmd, exit;
918 u32 blksize, offset, len;
919 int ret;
920
921 ret = 1;
922 exit = len = 0;
923
924 do {
925 struct fwheader *fwh = (void *)data;
926
927 bincmd = le32_to_cpu(fwh->dnldcmd);
928 blksize = le32_to_cpu(fwh->datalength);
929 switch (bincmd) {
930 case FW_HAS_DATA_TO_RECV:
931 offset = sizeof(struct fwheader) + blksize;
932 data += offset;
933 len += offset;
934 if (len >= totlen)
935 exit = 1;
936 break;
937 case FW_HAS_LAST_BLOCK:
938 exit = 1;
939 ret = 0;
940 break;
941 default:
942 exit = 1;
943 break;
944 }
945 } while (!exit);
946
947 if (ret)
948 lbs_pr_err("firmware file format check FAIL\n");
949 else
950 lbs_deb_fw("firmware file format check PASS\n");
951
952 return ret;
953}
954
955
956static int if_usb_prog_firmware(wlan_private *priv)
957{
958 int ret = -1;
959
960 lbs_deb_enter(LBS_DEB_FW);
961
962 if ((ret = request_firmware(&priv->firmware, libertas_fw_name,
963 priv->hotplug_device)) < 0) {
964 lbs_pr_err("request_firmware() failed with %#x\n", ret);
965 lbs_pr_err("firmware %s not found\n", libertas_fw_name);
966 goto done;
967 }
968
969 if (check_fwfile_format(priv->firmware->data, priv->firmware->size)) {
970 release_firmware(priv->firmware);
971 goto done;
972 }
973
974 ret = if_usb_do_prog_firmware(priv);
975
976 release_firmware(priv->firmware);
977done:
978 return ret;
979}
980
981
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200982#ifdef CONFIG_PM
983static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
984{
985 struct usb_card_rec *cardp = usb_get_intfdata(intf);
986 wlan_private *priv = cardp->priv;
987
Holger Schurig9012b282007-05-25 11:27:16 -0400988 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200989
990 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
991 return -1;
992
993 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400994 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200995
996 /* Unlink tx & rx urb */
997 usb_kill_urb(cardp->tx_urb);
998 usb_kill_urb(cardp->rx_urb);
999
1000 cardp->rx_urb_recall = 1;
1001
Holger Schurig9012b282007-05-25 11:27:16 -04001002 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001003 return 0;
1004}
1005
1006static int if_usb_resume(struct usb_interface *intf)
1007{
1008 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Javier Cardona51d84f52007-05-25 12:06:56 -04001009 wlan_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001010
Holger Schurig9012b282007-05-25 11:27:16 -04001011 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001012
1013 cardp->rx_urb_recall = 0;
1014
1015 if_usb_submit_rx_urb(cardp->priv);
1016
1017 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -04001018 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001019
Holger Schurig9012b282007-05-25 11:27:16 -04001020 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001021 return 0;
1022}
1023#else
1024#define if_usb_suspend NULL
1025#define if_usb_resume NULL
1026#endif
1027
1028static struct usb_driver if_usb_driver = {
1029 /* driver name */
1030 .name = usbdriver_name,
1031 /* probe function name */
1032 .probe = if_usb_probe,
1033 /* disconnect function name */
1034 .disconnect = if_usb_disconnect,
1035 /* device signature table */
1036 .id_table = if_usb_table,
1037 .suspend = if_usb_suspend,
1038 .resume = if_usb_resume,
1039};
1040
Holger Schurig084708b2007-05-25 12:37:58 -04001041static int if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001042{
Holger Schurig084708b2007-05-25 12:37:58 -04001043 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001044
Holger Schurig084708b2007-05-25 12:37:58 -04001045 lbs_deb_enter(LBS_DEB_MAIN);
1046
1047 if (libertas_fw_name == NULL) {
1048 libertas_fw_name = default_fw_name;
1049 }
1050
1051 ret = usb_register(&if_usb_driver);
1052
1053 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1054 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001055}
1056
Holger Schurig084708b2007-05-25 12:37:58 -04001057static void if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001058{
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -04001059 struct usb_card_rec *cardp, *cardp_temp;
Holger Schurig3874d0f2007-05-25 12:01:42 -04001060
Holger Schurig084708b2007-05-25 12:37:58 -04001061 lbs_deb_enter(LBS_DEB_MAIN);
1062
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -04001063 list_for_each_entry_safe(cardp, cardp_temp, &usb_devices, list)
Dan Williamseedc2a32007-08-02 11:36:22 -04001064 libertas_reset_device((wlan_private *) cardp->priv);
Holger Schurig3874d0f2007-05-25 12:01:42 -04001065
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001066 /* API unregisters the driver from USB subsystem */
1067 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001068
1069 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001070}
Holger Schurig084708b2007-05-25 12:37:58 -04001071
1072module_init(if_usb_init_module);
1073module_exit(if_usb_exit_module);
1074
1075MODULE_DESCRIPTION("8388 USB WLAN Driver");
1076MODULE_AUTHOR("Marvell International Ltd.");
1077MODULE_LICENSE("GPL");