blob: 364eae374b935d44617928c7b8d80ad146dca776 [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
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400211 udev->dev.driver_data = priv;
212
Dan Williams7732ca42007-05-25 13:13:25 -0400213 if (libertas_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400214 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400215
Holger Schurig208fdd22007-05-25 12:17:06 -0400216 priv->hw_register_dev = if_usb_register_dev;
217 priv->hw_unregister_dev = if_usb_unregister_dev;
218 priv->hw_prog_firmware = if_usb_prog_firmware;
219 priv->hw_host_to_card = if_usb_host_to_card;
220 priv->hw_get_int_status = if_usb_get_int_status;
221 priv->hw_read_event_cause = if_usb_read_event_cause;
Luis Carlos Cobo63f00232007-08-02 13:19:24 -0400222 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400223
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400224 if (libertas_activate_card(priv))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400225 goto err_activate_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400226
Holger Schurig435a1ac2007-05-25 12:41:52 -0400227 list_add_tail(&cardp->list, &usb_devices);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400228
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200229 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400230 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200231
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200232 return 0;
233
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400234err_activate_card:
Dan Williamsc7236832007-05-25 13:35:23 -0400235 libertas_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400236err_add_mesh:
Holger Schurig634b8f42007-05-25 13:05:16 -0400237 free_netdev(priv->dev);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400238 kfree(priv->adapter);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200239dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400240 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200241
242error:
243 return -ENOMEM;
244}
245
246/**
247 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400248 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200249 * @return N/A
250 */
251static void if_usb_disconnect(struct usb_interface *intf)
252{
253 struct usb_card_rec *cardp = usb_get_intfdata(intf);
254 wlan_private *priv = (wlan_private *) cardp->priv;
255 wlan_adapter *adapter = NULL;
256
257 adapter = priv->adapter;
258
259 /*
260 * Update Surprise removed to TRUE
261 */
262 adapter->surpriseremoved = 1;
263
Holger Schurig435a1ac2007-05-25 12:41:52 -0400264 list_del(&cardp->list);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400265
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200266 /* card is removed and we can call wlan_remove_card */
Holger Schurig9012b282007-05-25 11:27:16 -0400267 lbs_deb_usbd(&cardp->udev->dev, "call remove card\n");
Holger Schurig084708b2007-05-25 12:37:58 -0400268 libertas_remove_mesh(priv);
269 libertas_remove_card(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200270
271 /* Unlink and free urb */
272 if_usb_free(cardp);
273
274 usb_set_intfdata(intf, NULL);
275 usb_put_dev(interface_to_usbdev(intf));
276
277 return;
278}
279
280/**
281 * @brief This function download FW
282 * @param priv pointer to wlan_private
283 * @return 0
284 */
285static int if_prog_firmware(wlan_private * priv)
286{
Holger Schurig634b8f42007-05-25 13:05:16 -0400287 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200288 struct FWData *fwdata;
289 struct fwheader *fwheader;
290 u8 *firmware = priv->firmware->data;
291
292 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
293
294 if (!fwdata)
295 return -1;
296
297 fwheader = &fwdata->fwheader;
298
299 if (!cardp->CRC_OK) {
300 cardp->totalbytes = cardp->fwlastblksent;
301 cardp->fwseqnum = cardp->lastseqnum - 1;
302 }
303
Holger Schurig9012b282007-05-25 11:27:16 -0400304 /*
305 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200306 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400307 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200308
309 memcpy(fwheader, &firmware[cardp->totalbytes],
310 sizeof(struct fwheader));
311
312 cardp->fwlastblksent = cardp->totalbytes;
313 cardp->totalbytes += sizeof(struct fwheader);
314
Holger Schurig9012b282007-05-25 11:27:16 -0400315 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200316 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400317 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200318
Holger Schurig9012b282007-05-25 11:27:16 -0400319 /*
320 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400321 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400322 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200323
324 cardp->fwseqnum = cardp->fwseqnum + 1;
325
David Woodhouse981f1872007-05-25 23:36:54 -0400326 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
327 cardp->lastseqnum = cardp->fwseqnum;
328 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200329
David Woodhouse981f1872007-05-25 23:36:54 -0400330 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400331 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400332 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400333 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200334 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
335 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400336 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200337 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
338 usb_tx_block(priv, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
339
David Woodhousebb793e22007-05-25 23:38:14 -0400340 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400341 /*
342 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200343 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400344 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200345 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400346 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200347 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
348 usb_tx_block(priv, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
349 cardp->fwfinalblk = 1;
350 }
351
Holger Schurig9012b282007-05-25 11:27:16 -0400352 /*
353 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200354 "The firmware download is done size is %d\n",
355 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400356 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200357
358 kfree(fwdata);
359
360 return 0;
361}
362
Dan Williamseedc2a32007-08-02 11:36:22 -0400363static int if_usb_reset_device(wlan_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200364{
365 int ret;
Holger Schurig634b8f42007-05-25 13:05:16 -0400366 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200367
Holger Schurig3874d0f2007-05-25 12:01:42 -0400368 lbs_deb_enter(LBS_DEB_USB);
369
Dan Williamseedc2a32007-08-02 11:36:22 -0400370 /* Try a USB port reset first, if that fails send the reset
371 * command to the firmware.
372 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200373 ret = usb_reset_device(cardp->udev);
374 if (!ret) {
375 msleep(10);
Dan Williamseedc2a32007-08-02 11:36:22 -0400376 ret = libertas_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200377 msleep(10);
378 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400379
380 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
381
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200382 return ret;
383}
384
385/**
386 * @brief This function transfer the data to the device.
387 * @param priv pointer to wlan_private
388 * @param payload pointer to payload data
389 * @param nb data length
390 * @return 0 or -1
391 */
Holger Schurigac558ca2007-08-02 11:49:06 -0400392static int usb_tx_block(wlan_private * priv, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200393{
394 /* pointer to card structure */
Holger Schurig634b8f42007-05-25 13:05:16 -0400395 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200396 int ret = -1;
397
398 /* check if device is removed */
399 if (priv->adapter->surpriseremoved) {
Holger Schurig9012b282007-05-25 11:27:16 -0400400 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200401 goto tx_ret;
402 }
403
404 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
405 usb_sndbulkpipe(cardp->udev,
406 cardp->bulk_out_endpointAddr),
407 payload, nb, if_usb_write_bulk_callback, priv);
408
409 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
410
411 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
412 /* transfer failed */
Holger Schurig9012b282007-05-25 11:27:16 -0400413 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200414 ret = -1;
415 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400416 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200417 ret = 0;
418 }
419
420tx_ret:
421 return ret;
422}
423
424static int __if_usb_submit_rx_urb(wlan_private * priv,
425 void (*callbackfn)
426 (struct urb *urb))
427{
Holger Schurig634b8f42007-05-25 13:05:16 -0400428 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200429 struct sk_buff *skb;
430 struct read_cb_info *rinfo = &cardp->rinfo;
431 int ret = -1;
432
433 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
434 lbs_pr_err("No free skb\n");
435 goto rx_ret;
436 }
437
438 rinfo->skb = skb;
439
440 /* Fill the receive configuration URB and initialise the Rx call back */
441 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
442 usb_rcvbulkpipe(cardp->udev,
443 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400444 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200445 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
446 rinfo);
447
448 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
449
Holger Schurig9012b282007-05-25 11:27:16 -0400450 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
452 /* handle failure conditions */
Holger Schurig9012b282007-05-25 11:27:16 -0400453 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200454 ret = -1;
455 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400456 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200457 ret = 0;
458 }
459
460rx_ret:
461 return ret;
462}
463
464static inline int if_usb_submit_rx_urb_fwload(wlan_private * priv)
465{
466 return __if_usb_submit_rx_urb(priv, &if_usb_receive_fwload);
467}
468
469static inline int if_usb_submit_rx_urb(wlan_private * priv)
470{
471 return __if_usb_submit_rx_urb(priv, &if_usb_receive);
472}
473
474static void if_usb_receive_fwload(struct urb *urb)
475{
476 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
477 wlan_private *priv = rinfo->priv;
478 struct sk_buff *skb = rinfo->skb;
Holger Schurig634b8f42007-05-25 13:05:16 -0400479 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200480 struct fwsyncheader *syncfwheader;
481 struct bootcmdrespStr bootcmdresp;
482
483 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400484 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200485 "URB status is failed during fw load\n");
486 kfree_skb(skb);
487 return;
488 }
489
490 if (cardp->bootcmdresp == 0) {
491 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
492 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400493 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200494 kfree_skb(skb);
495 if_usb_submit_rx_urb_fwload(priv);
496 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400497 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200498 "Received valid boot command response\n");
499 return;
500 }
David Woodhouse981f1872007-05-25 23:36:54 -0400501 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200502 lbs_pr_info(
503 "boot cmd response wrong magic number (0x%x)\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400504 le32_to_cpu(bootcmdresp.u32magicnumber));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200505 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
506 lbs_pr_info(
507 "boot cmd response cmd_tag error (%d)\n",
508 bootcmdresp.u8cmd_tag);
509 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
510 lbs_pr_info(
511 "boot cmd response result error (%d)\n",
512 bootcmdresp.u8result);
513 } else {
514 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400515 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200516 "Received valid boot command response\n");
517 }
518 kfree_skb(skb);
519 if_usb_submit_rx_urb_fwload(priv);
520 return;
521 }
522
523 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
524 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400525 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200526 kfree_skb(skb);
527 return;
528 }
529
530 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
531 sizeof(struct fwsyncheader));
532
533 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400534 /*
535 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200536 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400537 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538 "FW received Blk seqnum = %d\n",
539 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400540 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200541 cardp->CRC_OK = 1;
542 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400543 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200544 "FW received Blk with CRC error\n");
545 cardp->CRC_OK = 0;
546 }
547
548 kfree_skb(skb);
549
550 if (cardp->fwfinalblk) {
551 cardp->fwdnldover = 1;
552 goto exit;
553 }
554
555 if_prog_firmware(priv);
556
557 if_usb_submit_rx_urb_fwload(priv);
558exit:
559 kfree(syncfwheader);
560
561 return;
562
563}
564
565#define MRVDRV_MIN_PKT_LEN 30
566
567static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
568 struct usb_card_rec *cardp,
569 wlan_private *priv)
570{
571 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
572 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400573 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200574 "Packet length is Invalid\n");
575 kfree_skb(skb);
576 return;
577 }
578
579 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
580 skb_put(skb, recvlength);
581 skb_pull(skb, MESSAGE_HEADER_LEN);
582 libertas_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400583 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200584}
585
586static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
587 struct sk_buff *skb,
588 struct usb_card_rec *cardp,
589 wlan_private *priv)
590{
591 u8 *cmdbuf;
592 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400593 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200594 "The receive buffer is too large\n");
595 kfree_skb(skb);
596 return;
597 }
598
599 if (!in_interrupt())
600 BUG();
601
602 spin_lock(&priv->adapter->driver_lock);
603 /* take care of cur_cmd = NULL case by reading the
604 * data to clear the interrupt */
605 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400606 cmdbuf = priv->upld_buf;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400607 priv->adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200608 } else
609 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
610
Holger Schurigc95c7f92007-08-02 11:49:45 -0400611 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400612 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200613 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400614 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200615
616 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400617 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200618 spin_unlock(&priv->adapter->driver_lock);
619
Holger Schurig9012b282007-05-25 11:27:16 -0400620 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200621 "Wake up main thread to handle cmd response\n");
622
623 return;
624}
625
626/**
627 * @brief This function reads of the packet into the upload buff,
628 * wake up the main thread and initialise the Rx callack.
629 *
630 * @param urb pointer to struct urb
631 * @return N/A
632 */
633static void if_usb_receive(struct urb *urb)
634{
635 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
636 wlan_private *priv = rinfo->priv;
637 struct sk_buff *skb = rinfo->skb;
Holger Schurig634b8f42007-05-25 13:05:16 -0400638 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200639
640 int recvlength = urb->actual_length;
641 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400642 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200643
Holger Schurig9012b282007-05-25 11:27:16 -0400644 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200645
646 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400647 __le32 tmp;
648
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200649 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400650 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200651 "URB status is failed\n");
652 kfree_skb(skb);
653 goto setup_for_next;
654 }
655
656 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400657 memcpy(&tmp, recvbuff, sizeof(u32));
658 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400659 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400660 "Recv length = 0x%x, Recv type = 0x%X\n",
661 recvlength, recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200662 } else if (urb->status)
663 goto rx_exit;
664
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200665 switch (recvtype) {
666 case CMD_TYPE_DATA:
667 process_cmdtypedata(recvlength, skb, cardp, priv);
668 break;
669
670 case CMD_TYPE_REQUEST:
671 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
672 break;
673
674 case CMD_TYPE_INDICATION:
675 /* Event cause handling */
676 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400677 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400678 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200679 cardp->usb_event_cause);
680 if (cardp->usb_event_cause & 0xffff0000) {
681 libertas_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400682 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683 break;
684 }
David Woodhouse981f1872007-05-25 23:36:54 -0400685 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400686 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200687 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400688 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689 spin_unlock(&priv->adapter->driver_lock);
690 goto rx_exit;
691 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400692 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
693 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694 kfree_skb(skb);
695 break;
696 }
697
698setup_for_next:
699 if_usb_submit_rx_urb(priv);
700rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400701 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200702}
703
704/**
705 * @brief This function downloads data to FW
706 * @param priv pointer to wlan_private structure
707 * @param type type of data
708 * @param buf pointer to data buffer
709 * @param len number of bytes
710 * @return 0 or -1
711 */
Holger Schurig208fdd22007-05-25 12:17:06 -0400712static int if_usb_host_to_card(wlan_private * priv, u8 type, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200713{
Holger Schurig634b8f42007-05-25 13:05:16 -0400714 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200715
Holger Schurig9012b282007-05-25 11:27:16 -0400716 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
717 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200718
719 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400720 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400721 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
723 MESSAGE_HEADER_LEN);
724
725 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400726 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400727 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200728 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
729 MESSAGE_HEADER_LEN);
730 }
731
732 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
733
Dan Williams8362cd42007-08-03 09:40:55 -0400734 return usb_tx_block(priv, cardp->bulk_out_buffer,
735 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200736}
737
738/* called with adapter->driver_lock held */
Holger Schurig208fdd22007-05-25 12:17:06 -0400739static int if_usb_get_int_status(wlan_private * priv, u8 * ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200740{
Holger Schurig634b8f42007-05-25 13:05:16 -0400741 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200742
743 *ireg = cardp->usb_int_cause;
744 cardp->usb_int_cause = 0;
745
Holger Schurig9012b282007-05-25 11:27:16 -0400746 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200747
748 return 0;
749}
750
Holger Schurig208fdd22007-05-25 12:17:06 -0400751static int if_usb_read_event_cause(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200752{
Holger Schurig634b8f42007-05-25 13:05:16 -0400753 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200754 priv->adapter->eventcause = cardp->usb_event_cause;
755 /* Re-submit rx urb here to avoid event lost issue */
756 if_usb_submit_rx_urb(priv);
757 return 0;
758}
759
Holger Schurig208fdd22007-05-25 12:17:06 -0400760static int if_usb_unregister_dev(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200761{
762 int ret = 0;
763
764 /* Need to send a Reset command to device before USB resources freed
765 * and wlan_remove_card() called, then device can handle FW download
766 * again.
767 */
768 if (priv)
Dan Williamseedc2a32007-08-02 11:36:22 -0400769 libertas_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200770
771 return ret;
772}
773
774
775/**
776 * @brief This function register usb device and initialize parameter
777 * @param priv pointer to wlan_private
778 * @return 0 or -1
779 */
Holger Schurig208fdd22007-05-25 12:17:06 -0400780static int if_usb_register_dev(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200781{
Holger Schurig634b8f42007-05-25 13:05:16 -0400782 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Holger Schurig9012b282007-05-25 11:27:16 -0400783
784 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200785
786 cardp->priv = priv;
Holger Schurig634b8f42007-05-25 13:05:16 -0400787 cardp->eth_dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200788 priv->hotplug_device = &(cardp->udev->dev);
789
Holger Schurig9012b282007-05-25 11:27:16 -0400790 lbs_deb_usbd(&cardp->udev->dev, "udev pointer is at %p\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200791 cardp->udev);
792
Holger Schurig9012b282007-05-25 11:27:16 -0400793 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200794 return 0;
795}
796
Dan Williams9e22cb62007-08-02 11:14:49 -0400797/**
798 * @brief This function issues Boot command to the Boot2 code
799 * @param ivalue 1:Boot from FW by USB-Download
800 * 2:Boot from FW in EEPROM
801 * @return 0
802 */
803static int if_usb_issue_boot_command(wlan_private *priv, int ivalue)
804{
805 struct usb_card_rec *cardp = priv->card;
806 struct bootcmdstr sbootcmd;
807 int i;
808
809 /* Prepare command */
810 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
811 sbootcmd.u8cmd_tag = ivalue;
812 for (i=0; i<11; i++)
813 sbootcmd.au8dumy[i]=0x00;
814 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
815
816 /* Issue command */
817 usb_tx_block(priv, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
818
819 return 0;
820}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200821
822
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400823static int if_usb_do_prog_firmware(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200824{
Holger Schurig634b8f42007-05-25 13:05:16 -0400825 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200826 int i = 0;
827 static int reset_count = 10;
Holger Schurig9012b282007-05-25 11:27:16 -0400828 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200829
Holger Schurig9012b282007-05-25 11:27:16 -0400830 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200831
832 cardp->rinfo.priv = priv;
833
834restart:
835 if (if_usb_submit_rx_urb_fwload(priv) < 0) {
Holger Schurig9012b282007-05-25 11:27:16 -0400836 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
837 ret = -1;
838 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200839 }
840
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200841 cardp->bootcmdresp = 0;
842 do {
843 int j = 0;
844 i++;
845 /* Issue Boot command = 1, Boot from Download-FW */
846 if_usb_issue_boot_command(priv, BOOT_CMD_FW_BY_USB);
847 /* wait for command response */
848 do {
849 j++;
850 msleep_interruptible(100);
851 } while (cardp->bootcmdresp == 0 && j < 10);
852 } while (cardp->bootcmdresp == 0 && i < 5);
853
854 if (cardp->bootcmdresp == 0) {
855 if (--reset_count >= 0) {
Dan Williamseedc2a32007-08-02 11:36:22 -0400856 if_usb_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200857 goto restart;
858 }
859 return -1;
860 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200861
862 i = 0;
863 priv->adapter->fw_ready = 0;
864
865 cardp->totalbytes = 0;
866 cardp->fwlastblksent = 0;
867 cardp->CRC_OK = 1;
868 cardp->fwdnldover = 0;
869 cardp->fwseqnum = -1;
870 cardp->totalbytes = 0;
871 cardp->fwfinalblk = 0;
872
873 if_prog_firmware(priv);
874
875 do {
Holger Schurig9012b282007-05-25 11:27:16 -0400876 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200877 i++;
878 msleep_interruptible(100);
879 if (priv->adapter->surpriseremoved || i >= 20)
880 break;
881 } while (!cardp->fwdnldover);
882
883 if (!cardp->fwdnldover) {
884 lbs_pr_info("failed to load fw, resetting device!\n");
885 if (--reset_count >= 0) {
Dan Williamseedc2a32007-08-02 11:36:22 -0400886 if_usb_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200887 goto restart;
888 }
889
890 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
Holger Schurig9012b282007-05-25 11:27:16 -0400891 ret = -1;
892 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200893 }
894
895 if_usb_submit_rx_urb(priv);
896
897 /* Delay 200 ms to waiting for the FW ready */
898 msleep_interruptible(200);
899
900 priv->adapter->fw_ready = 1;
901
Holger Schurig9012b282007-05-25 11:27:16 -0400902done:
903 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
904 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200905}
906
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400907/**
908 * @brief This function checks the validity of Boot2/FW image.
909 *
910 * @param data pointer to image
911 * len image length
912 * @return 0 or -1
913 */
914static int check_fwfile_format(u8 *data, u32 totlen)
915{
916 u32 bincmd, exit;
917 u32 blksize, offset, len;
918 int ret;
919
920 ret = 1;
921 exit = len = 0;
922
923 do {
924 struct fwheader *fwh = (void *)data;
925
926 bincmd = le32_to_cpu(fwh->dnldcmd);
927 blksize = le32_to_cpu(fwh->datalength);
928 switch (bincmd) {
929 case FW_HAS_DATA_TO_RECV:
930 offset = sizeof(struct fwheader) + blksize;
931 data += offset;
932 len += offset;
933 if (len >= totlen)
934 exit = 1;
935 break;
936 case FW_HAS_LAST_BLOCK:
937 exit = 1;
938 ret = 0;
939 break;
940 default:
941 exit = 1;
942 break;
943 }
944 } while (!exit);
945
946 if (ret)
947 lbs_pr_err("firmware file format check FAIL\n");
948 else
949 lbs_deb_fw("firmware file format check PASS\n");
950
951 return ret;
952}
953
954
955static int if_usb_prog_firmware(wlan_private *priv)
956{
957 int ret = -1;
958
959 lbs_deb_enter(LBS_DEB_FW);
960
961 if ((ret = request_firmware(&priv->firmware, libertas_fw_name,
962 priv->hotplug_device)) < 0) {
963 lbs_pr_err("request_firmware() failed with %#x\n", ret);
964 lbs_pr_err("firmware %s not found\n", libertas_fw_name);
965 goto done;
966 }
967
968 if (check_fwfile_format(priv->firmware->data, priv->firmware->size)) {
969 release_firmware(priv->firmware);
970 goto done;
971 }
972
973 ret = if_usb_do_prog_firmware(priv);
974
975 release_firmware(priv->firmware);
976done:
977 return ret;
978}
979
980
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200981#ifdef CONFIG_PM
982static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
983{
984 struct usb_card_rec *cardp = usb_get_intfdata(intf);
985 wlan_private *priv = cardp->priv;
986
Holger Schurig9012b282007-05-25 11:27:16 -0400987 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200988
989 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
990 return -1;
991
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400992 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
993 /* Mesh autostart must be activated while sleeping
994 * On resume it will go back to the current state
995 */
996 struct cmd_ds_mesh_access mesh_access;
997 memset(&mesh_access, 0, sizeof(mesh_access));
998 mesh_access.data[0] = cpu_to_le32(1);
999 libertas_prepare_and_send_command(priv,
1000 CMD_MESH_ACCESS,
1001 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
1002 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1003 }
1004
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001005 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -04001006 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001007
1008 /* Unlink tx & rx urb */
1009 usb_kill_urb(cardp->tx_urb);
1010 usb_kill_urb(cardp->rx_urb);
1011
1012 cardp->rx_urb_recall = 1;
1013
Holger Schurig9012b282007-05-25 11:27:16 -04001014 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001015 return 0;
1016}
1017
1018static int if_usb_resume(struct usb_interface *intf)
1019{
1020 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Javier Cardona51d84f52007-05-25 12:06:56 -04001021 wlan_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001022
Holger Schurig9012b282007-05-25 11:27:16 -04001023 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001024
1025 cardp->rx_urb_recall = 0;
1026
1027 if_usb_submit_rx_urb(cardp->priv);
1028
1029 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -04001030 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001031
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -04001032 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
1033 /* Mesh autostart was activated while sleeping
1034 * Disable it if appropriate
1035 */
1036 struct cmd_ds_mesh_access mesh_access;
1037 memset(&mesh_access, 0, sizeof(mesh_access));
1038 mesh_access.data[0] = cpu_to_le32(0);
1039 libertas_prepare_and_send_command(priv,
1040 CMD_MESH_ACCESS,
1041 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
1042 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1043 }
1044
Holger Schurig9012b282007-05-25 11:27:16 -04001045 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001046 return 0;
1047}
1048#else
1049#define if_usb_suspend NULL
1050#define if_usb_resume NULL
1051#endif
1052
1053static struct usb_driver if_usb_driver = {
1054 /* driver name */
1055 .name = usbdriver_name,
1056 /* probe function name */
1057 .probe = if_usb_probe,
1058 /* disconnect function name */
1059 .disconnect = if_usb_disconnect,
1060 /* device signature table */
1061 .id_table = if_usb_table,
1062 .suspend = if_usb_suspend,
1063 .resume = if_usb_resume,
1064};
1065
Holger Schurig084708b2007-05-25 12:37:58 -04001066static int if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001067{
Holger Schurig084708b2007-05-25 12:37:58 -04001068 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001069
Holger Schurig084708b2007-05-25 12:37:58 -04001070 lbs_deb_enter(LBS_DEB_MAIN);
1071
1072 if (libertas_fw_name == NULL) {
1073 libertas_fw_name = default_fw_name;
1074 }
1075
1076 ret = usb_register(&if_usb_driver);
1077
1078 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1079 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001080}
1081
Holger Schurig084708b2007-05-25 12:37:58 -04001082static void if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001083{
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -04001084 struct usb_card_rec *cardp, *cardp_temp;
Holger Schurig3874d0f2007-05-25 12:01:42 -04001085
Holger Schurig084708b2007-05-25 12:37:58 -04001086 lbs_deb_enter(LBS_DEB_MAIN);
1087
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -04001088 list_for_each_entry_safe(cardp, cardp_temp, &usb_devices, list)
Dan Williamseedc2a32007-08-02 11:36:22 -04001089 libertas_reset_device((wlan_private *) cardp->priv);
Holger Schurig3874d0f2007-05-25 12:01:42 -04001090
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001091 /* API unregisters the driver from USB subsystem */
1092 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001093
1094 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001095}
Holger Schurig084708b2007-05-25 12:37:58 -04001096
1097module_init(if_usb_init_module);
1098module_exit(if_usb_exit_module);
1099
1100MODULE_DESCRIPTION("8388 USB WLAN Driver");
1101MODULE_AUTHOR("Marvell International Ltd.");
1102MODULE_LICENSE("GPL");