blob: 53d479688ff9f2dd73e594adc3c15754a1491fa0 [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;
642 u32 recvtype;
643
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) {
647 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400648 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200649 "URB status is failed\n");
650 kfree_skb(skb);
651 goto setup_for_next;
652 }
653
654 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
655 memcpy(&recvtype, recvbuff, sizeof(u32));
Holger Schurig9012b282007-05-25 11:27:16 -0400656 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200657 "Recv length = 0x%x\n", recvlength);
Holger Schurig9012b282007-05-25 11:27:16 -0400658 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200659 "Receive type = 0x%X\n", recvtype);
660 recvtype = le32_to_cpu(recvtype);
Holger Schurig9012b282007-05-25 11:27:16 -0400661 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200662 "Receive type after = 0x%X\n", recvtype);
663 } else if (urb->status)
664 goto rx_exit;
665
666
667 switch (recvtype) {
668 case CMD_TYPE_DATA:
669 process_cmdtypedata(recvlength, skb, cardp, priv);
670 break;
671
672 case CMD_TYPE_REQUEST:
673 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
674 break;
675
676 case CMD_TYPE_INDICATION:
677 /* Event cause handling */
678 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400679 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400680 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200681 cardp->usb_event_cause);
682 if (cardp->usb_event_cause & 0xffff0000) {
683 libertas_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400684 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200685 break;
686 }
David Woodhouse981f1872007-05-25 23:36:54 -0400687 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400688 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400690 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691 spin_unlock(&priv->adapter->driver_lock);
692 goto rx_exit;
693 default:
694 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{
714 int ret = -1;
715 u32 tmp;
Holger Schurig634b8f42007-05-25 13:05:16 -0400716 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200717
Holger Schurig9012b282007-05-25 11:27:16 -0400718 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
719 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200720
721 if (type == MVMS_CMD) {
722 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400723 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200724 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
725 MESSAGE_HEADER_LEN);
726
727 } else {
728 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400729 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200730 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
731 MESSAGE_HEADER_LEN);
732 }
733
734 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
735
736 ret =
737 usb_tx_block(priv, cardp->bulk_out_buffer, nb + MESSAGE_HEADER_LEN);
738
739 return ret;
740}
741
742/* called with adapter->driver_lock held */
Holger Schurig208fdd22007-05-25 12:17:06 -0400743static int if_usb_get_int_status(wlan_private * priv, u8 * ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200744{
Holger Schurig634b8f42007-05-25 13:05:16 -0400745 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746
747 *ireg = cardp->usb_int_cause;
748 cardp->usb_int_cause = 0;
749
Holger Schurig9012b282007-05-25 11:27:16 -0400750 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200751
752 return 0;
753}
754
Holger Schurig208fdd22007-05-25 12:17:06 -0400755static int if_usb_read_event_cause(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756{
Holger Schurig634b8f42007-05-25 13:05:16 -0400757 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200758 priv->adapter->eventcause = cardp->usb_event_cause;
759 /* Re-submit rx urb here to avoid event lost issue */
760 if_usb_submit_rx_urb(priv);
761 return 0;
762}
763
Holger Schurig208fdd22007-05-25 12:17:06 -0400764static int if_usb_unregister_dev(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200765{
766 int ret = 0;
767
768 /* Need to send a Reset command to device before USB resources freed
769 * and wlan_remove_card() called, then device can handle FW download
770 * again.
771 */
772 if (priv)
Dan Williamseedc2a32007-08-02 11:36:22 -0400773 libertas_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200774
775 return ret;
776}
777
778
779/**
780 * @brief This function register usb device and initialize parameter
781 * @param priv pointer to wlan_private
782 * @return 0 or -1
783 */
Holger Schurig208fdd22007-05-25 12:17:06 -0400784static int if_usb_register_dev(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200785{
Holger Schurig634b8f42007-05-25 13:05:16 -0400786 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Holger Schurig9012b282007-05-25 11:27:16 -0400787
788 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200789
790 cardp->priv = priv;
Holger Schurig634b8f42007-05-25 13:05:16 -0400791 cardp->eth_dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200792 priv->hotplug_device = &(cardp->udev->dev);
793
Holger Schurig9012b282007-05-25 11:27:16 -0400794 lbs_deb_usbd(&cardp->udev->dev, "udev pointer is at %p\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200795 cardp->udev);
796
Holger Schurig9012b282007-05-25 11:27:16 -0400797 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200798 return 0;
799}
800
Dan Williams9e22cb62007-08-02 11:14:49 -0400801/**
802 * @brief This function issues Boot command to the Boot2 code
803 * @param ivalue 1:Boot from FW by USB-Download
804 * 2:Boot from FW in EEPROM
805 * @return 0
806 */
807static int if_usb_issue_boot_command(wlan_private *priv, int ivalue)
808{
809 struct usb_card_rec *cardp = priv->card;
810 struct bootcmdstr sbootcmd;
811 int i;
812
813 /* Prepare command */
814 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
815 sbootcmd.u8cmd_tag = ivalue;
816 for (i=0; i<11; i++)
817 sbootcmd.au8dumy[i]=0x00;
818 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
819
820 /* Issue command */
821 usb_tx_block(priv, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
822
823 return 0;
824}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200825
826
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400827static int if_usb_do_prog_firmware(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200828{
Holger Schurig634b8f42007-05-25 13:05:16 -0400829 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200830 int i = 0;
831 static int reset_count = 10;
Holger Schurig9012b282007-05-25 11:27:16 -0400832 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200833
Holger Schurig9012b282007-05-25 11:27:16 -0400834 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200835
836 cardp->rinfo.priv = priv;
837
838restart:
839 if (if_usb_submit_rx_urb_fwload(priv) < 0) {
Holger Schurig9012b282007-05-25 11:27:16 -0400840 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
841 ret = -1;
842 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200843 }
844
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200845 cardp->bootcmdresp = 0;
846 do {
847 int j = 0;
848 i++;
849 /* Issue Boot command = 1, Boot from Download-FW */
850 if_usb_issue_boot_command(priv, BOOT_CMD_FW_BY_USB);
851 /* wait for command response */
852 do {
853 j++;
854 msleep_interruptible(100);
855 } while (cardp->bootcmdresp == 0 && j < 10);
856 } while (cardp->bootcmdresp == 0 && i < 5);
857
858 if (cardp->bootcmdresp == 0) {
859 if (--reset_count >= 0) {
Dan Williamseedc2a32007-08-02 11:36:22 -0400860 if_usb_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200861 goto restart;
862 }
863 return -1;
864 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200865
866 i = 0;
867 priv->adapter->fw_ready = 0;
868
869 cardp->totalbytes = 0;
870 cardp->fwlastblksent = 0;
871 cardp->CRC_OK = 1;
872 cardp->fwdnldover = 0;
873 cardp->fwseqnum = -1;
874 cardp->totalbytes = 0;
875 cardp->fwfinalblk = 0;
876
877 if_prog_firmware(priv);
878
879 do {
Holger Schurig9012b282007-05-25 11:27:16 -0400880 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200881 i++;
882 msleep_interruptible(100);
883 if (priv->adapter->surpriseremoved || i >= 20)
884 break;
885 } while (!cardp->fwdnldover);
886
887 if (!cardp->fwdnldover) {
888 lbs_pr_info("failed to load fw, resetting device!\n");
889 if (--reset_count >= 0) {
Dan Williamseedc2a32007-08-02 11:36:22 -0400890 if_usb_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200891 goto restart;
892 }
893
894 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
Holger Schurig9012b282007-05-25 11:27:16 -0400895 ret = -1;
896 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200897 }
898
899 if_usb_submit_rx_urb(priv);
900
901 /* Delay 200 ms to waiting for the FW ready */
902 msleep_interruptible(200);
903
904 priv->adapter->fw_ready = 1;
905
Holger Schurig9012b282007-05-25 11:27:16 -0400906done:
907 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
908 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200909}
910
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400911/**
912 * @brief This function checks the validity of Boot2/FW image.
913 *
914 * @param data pointer to image
915 * len image length
916 * @return 0 or -1
917 */
918static int check_fwfile_format(u8 *data, u32 totlen)
919{
920 u32 bincmd, exit;
921 u32 blksize, offset, len;
922 int ret;
923
924 ret = 1;
925 exit = len = 0;
926
927 do {
928 struct fwheader *fwh = (void *)data;
929
930 bincmd = le32_to_cpu(fwh->dnldcmd);
931 blksize = le32_to_cpu(fwh->datalength);
932 switch (bincmd) {
933 case FW_HAS_DATA_TO_RECV:
934 offset = sizeof(struct fwheader) + blksize;
935 data += offset;
936 len += offset;
937 if (len >= totlen)
938 exit = 1;
939 break;
940 case FW_HAS_LAST_BLOCK:
941 exit = 1;
942 ret = 0;
943 break;
944 default:
945 exit = 1;
946 break;
947 }
948 } while (!exit);
949
950 if (ret)
951 lbs_pr_err("firmware file format check FAIL\n");
952 else
953 lbs_deb_fw("firmware file format check PASS\n");
954
955 return ret;
956}
957
958
959static int if_usb_prog_firmware(wlan_private *priv)
960{
961 int ret = -1;
962
963 lbs_deb_enter(LBS_DEB_FW);
964
965 if ((ret = request_firmware(&priv->firmware, libertas_fw_name,
966 priv->hotplug_device)) < 0) {
967 lbs_pr_err("request_firmware() failed with %#x\n", ret);
968 lbs_pr_err("firmware %s not found\n", libertas_fw_name);
969 goto done;
970 }
971
972 if (check_fwfile_format(priv->firmware->data, priv->firmware->size)) {
973 release_firmware(priv->firmware);
974 goto done;
975 }
976
977 ret = if_usb_do_prog_firmware(priv);
978
979 release_firmware(priv->firmware);
980done:
981 return ret;
982}
983
984
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200985#ifdef CONFIG_PM
986static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
987{
988 struct usb_card_rec *cardp = usb_get_intfdata(intf);
989 wlan_private *priv = cardp->priv;
990
Holger Schurig9012b282007-05-25 11:27:16 -0400991 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200992
993 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
994 return -1;
995
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400996 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
997 /* Mesh autostart must be activated while sleeping
998 * On resume it will go back to the current state
999 */
1000 struct cmd_ds_mesh_access mesh_access;
1001 memset(&mesh_access, 0, sizeof(mesh_access));
1002 mesh_access.data[0] = cpu_to_le32(1);
1003 libertas_prepare_and_send_command(priv,
1004 CMD_MESH_ACCESS,
1005 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
1006 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1007 }
1008
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001009 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -04001010 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001011
1012 /* Unlink tx & rx urb */
1013 usb_kill_urb(cardp->tx_urb);
1014 usb_kill_urb(cardp->rx_urb);
1015
1016 cardp->rx_urb_recall = 1;
1017
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
1022static int if_usb_resume(struct usb_interface *intf)
1023{
1024 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Javier Cardona51d84f52007-05-25 12:06:56 -04001025 wlan_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001026
Holger Schurig9012b282007-05-25 11:27:16 -04001027 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001028
1029 cardp->rx_urb_recall = 0;
1030
1031 if_usb_submit_rx_urb(cardp->priv);
1032
1033 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -04001034 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001035
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -04001036 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
1037 /* Mesh autostart was activated while sleeping
1038 * Disable it if appropriate
1039 */
1040 struct cmd_ds_mesh_access mesh_access;
1041 memset(&mesh_access, 0, sizeof(mesh_access));
1042 mesh_access.data[0] = cpu_to_le32(0);
1043 libertas_prepare_and_send_command(priv,
1044 CMD_MESH_ACCESS,
1045 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
1046 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1047 }
1048
Holger Schurig9012b282007-05-25 11:27:16 -04001049 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050 return 0;
1051}
1052#else
1053#define if_usb_suspend NULL
1054#define if_usb_resume NULL
1055#endif
1056
1057static struct usb_driver if_usb_driver = {
1058 /* driver name */
1059 .name = usbdriver_name,
1060 /* probe function name */
1061 .probe = if_usb_probe,
1062 /* disconnect function name */
1063 .disconnect = if_usb_disconnect,
1064 /* device signature table */
1065 .id_table = if_usb_table,
1066 .suspend = if_usb_suspend,
1067 .resume = if_usb_resume,
1068};
1069
Holger Schurig084708b2007-05-25 12:37:58 -04001070static int if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001071{
Holger Schurig084708b2007-05-25 12:37:58 -04001072 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001073
Holger Schurig084708b2007-05-25 12:37:58 -04001074 lbs_deb_enter(LBS_DEB_MAIN);
1075
1076 if (libertas_fw_name == NULL) {
1077 libertas_fw_name = default_fw_name;
1078 }
1079
1080 ret = usb_register(&if_usb_driver);
1081
1082 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1083 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001084}
1085
Holger Schurig084708b2007-05-25 12:37:58 -04001086static void if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001087{
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -04001088 struct usb_card_rec *cardp, *cardp_temp;
Holger Schurig3874d0f2007-05-25 12:01:42 -04001089
Holger Schurig084708b2007-05-25 12:37:58 -04001090 lbs_deb_enter(LBS_DEB_MAIN);
1091
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -04001092 list_for_each_entry_safe(cardp, cardp_temp, &usb_devices, list)
Dan Williamseedc2a32007-08-02 11:36:22 -04001093 libertas_reset_device((wlan_private *) cardp->priv);
Holger Schurig3874d0f2007-05-25 12:01:42 -04001094
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001095 /* API unregisters the driver from USB subsystem */
1096 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001097
1098 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001099}
Holger Schurig084708b2007-05-25 12:37:58 -04001100
1101module_init(if_usb_init_module);
1102module_exit(if_usb_exit_module);
1103
1104MODULE_DESCRIPTION("8388 USB WLAN Driver");
1105MODULE_AUTHOR("Marvell International Ltd.");
1106MODULE_LICENSE("GPL");