blob: 8a3c70e5407565648c345be6dad970539af700c9 [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);
Dan Williams954ee162007-08-20 11:43:25 -040048static int if_usb_prog_firmware(struct usb_card_rec *cardp);
Holger Schurig208fdd22007-05-25 12:17:06 -040049static int if_usb_host_to_card(wlan_private * priv, u8 type, u8 * payload, u16 nb);
50static int if_usb_get_int_status(wlan_private * priv, u8 *);
51static int if_usb_read_event_cause(wlan_private *);
Dan Williams954ee162007-08-20 11:43:25 -040052static int usb_tx_block(struct usb_card_rec *cardp, u8 *payload, u16 nb);
Holger Schurigac558ca2007-08-02 11:49:06 -040053static void if_usb_free(struct usb_card_rec *cardp);
Dan Williams954ee162007-08-20 11:43:25 -040054static int if_usb_submit_rx_urb(struct usb_card_rec *cardp);
55static int if_usb_reset_device(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{
Dan Williams954ee162007-08-20 11:43:25 -040064 struct usb_card_rec *cardp = (struct usb_card_rec *) urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020065
66 /* handle the transmission complete validations */
67
Dan Williams954ee162007-08-20 11:43:25 -040068 if (urb->status == 0) {
69 wlan_private *priv = cardp->priv;
70
Holger Schurig9012b282007-05-25 11:27:16 -040071 /*
72 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
73 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020074 urb->actual_length);
Holger Schurig9012b282007-05-25 11:27:16 -040075 */
Dan Williams954ee162007-08-20 11:43:25 -040076
77 /* Used for both firmware TX and regular TX. priv isn't
78 * valid at firmware load time.
79 */
80 if (priv) {
81 wlan_adapter *adapter = priv->adapter;
82 struct net_device *dev = priv->dev;
83
84 priv->dnld_sent = DNLD_RES_RECEIVED;
85
86 /* Wake main thread if commands are pending */
87 if (!adapter->cur_cmd)
88 wake_up_interruptible(&priv->waitq);
89
90 if ((adapter->connect_status == LIBERTAS_CONNECTED)) {
91 netif_wake_queue(dev);
92 netif_wake_queue(priv->mesh_dev);
93 }
Javier Cardona51d84f52007-05-25 12:06:56 -040094 }
Dan Williams954ee162007-08-20 11:43:25 -040095 } else {
96 /* print the failure status number for debug */
97 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020098 }
99
100 return;
101}
102
103/**
104 * @brief free tx/rx urb, skb and rx buffer
105 * @param cardp pointer usb_card_rec
106 * @return N/A
107 */
Holger Schurigac558ca2007-08-02 11:49:06 -0400108static void if_usb_free(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200109{
Holger Schurig9012b282007-05-25 11:27:16 -0400110 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200111
112 /* Unlink tx & rx urb */
113 usb_kill_urb(cardp->tx_urb);
114 usb_kill_urb(cardp->rx_urb);
115
116 usb_free_urb(cardp->tx_urb);
117 cardp->tx_urb = NULL;
118
119 usb_free_urb(cardp->rx_urb);
120 cardp->rx_urb = NULL;
121
122 kfree(cardp->bulk_out_buffer);
123 cardp->bulk_out_buffer = NULL;
124
Holger Schurig9012b282007-05-25 11:27:16 -0400125 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200126}
127
128/**
129 * @brief sets the configuration values
130 * @param ifnum interface number
131 * @param id pointer to usb_device_id
132 * @return 0 on success, error code on failure
133 */
134static int if_usb_probe(struct usb_interface *intf,
135 const struct usb_device_id *id)
136{
137 struct usb_device *udev;
138 struct usb_host_interface *iface_desc;
139 struct usb_endpoint_descriptor *endpoint;
Holger Schurig78523da2007-05-25 11:49:19 -0400140 wlan_private *priv;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400141 struct usb_card_rec *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200142 int i;
143
144 udev = interface_to_usbdev(intf);
145
Holger Schurig435a1ac2007-05-25 12:41:52 -0400146 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
147 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200148 lbs_pr_err("Out of memory allocating private data.\n");
149 goto error;
150 }
151
Holger Schurig435a1ac2007-05-25 12:41:52 -0400152 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200153 iface_desc = intf->cur_altsetting;
154
Holger Schurig9012b282007-05-25 11:27:16 -0400155 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200156 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400157 le16_to_cpu(udev->descriptor.bcdUSB),
158 udev->descriptor.bDeviceClass,
159 udev->descriptor.bDeviceSubClass,
160 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200161
162 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
163 endpoint = &iface_desc->endpoint[i].desc;
164 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
165 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
166 USB_ENDPOINT_XFER_BULK)) {
167 /* we found a bulk in endpoint */
Holger Schurig9012b282007-05-25 11:27:16 -0400168 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400169 le16_to_cpu(endpoint->wMaxPacketSize));
170 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400171 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200172 "Rx URB allocation failed\n");
173 goto dealloc;
174 }
Holger Schurig435a1ac2007-05-25 12:41:52 -0400175 cardp->rx_urb_recall = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200176
Holger Schurig435a1ac2007-05-25 12:41:52 -0400177 cardp->bulk_in_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400178 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400179 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200180 (endpoint->
181 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400182 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200183 endpoint->bEndpointAddress);
184 }
185
186 if (((endpoint->
187 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
188 USB_DIR_OUT)
189 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
190 USB_ENDPOINT_XFER_BULK)) {
191 /* We found bulk out endpoint */
David Woodhouse981f1872007-05-25 23:36:54 -0400192 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400193 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200194 "Tx URB allocation failed\n");
195 goto dealloc;
196 }
197
Holger Schurig435a1ac2007-05-25 12:41:52 -0400198 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400199 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400200 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400201 "Bulk out size is %d\n",
202 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400203 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200204 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400205 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200206 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400207 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200208 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
209 GFP_KERNEL);
210
Holger Schurig435a1ac2007-05-25 12:41:52 -0400211 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400212 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200213 "Could not allocate buffer\n");
214 goto dealloc;
215 }
216 }
217 }
218
Dan Williams954ee162007-08-20 11:43:25 -0400219 /* Upload firmware */
220 cardp->rinfo.cardp = cardp;
221 if (if_usb_prog_firmware(cardp)) {
222 lbs_deb_usbd(&udev->dev, "FW upload failed");
223 goto err_prog_firmware;
224 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400225
Dan Williams954ee162007-08-20 11:43:25 -0400226 if (!(priv = libertas_add_card(cardp, &udev->dev)))
227 goto err_prog_firmware;
228
229 cardp->priv = priv;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400230
Dan Williams7732ca42007-05-25 13:13:25 -0400231 if (libertas_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400232 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400233
Dan Williams954ee162007-08-20 11:43:25 -0400234 cardp->eth_dev = priv->dev;
235
Holger Schurig208fdd22007-05-25 12:17:06 -0400236 priv->hw_host_to_card = if_usb_host_to_card;
237 priv->hw_get_int_status = if_usb_get_int_status;
238 priv->hw_read_event_cause = if_usb_read_event_cause;
Luis Carlos Cobo63f00232007-08-02 13:19:24 -0400239 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400240
Dan Williams954ee162007-08-20 11:43:25 -0400241 /* Delay 200 ms to waiting for the FW ready */
242 if_usb_submit_rx_urb(cardp);
243 msleep_interruptible(200);
244 priv->adapter->fw_ready = 1;
245
246 if (libertas_start_card(priv))
247 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400248
Holger Schurig435a1ac2007-05-25 12:41:52 -0400249 list_add_tail(&cardp->list, &usb_devices);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400250
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200251 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400252 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200253
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200254 return 0;
255
Dan Williams954ee162007-08-20 11:43:25 -0400256err_start_card:
Dan Williamsc7236832007-05-25 13:35:23 -0400257 libertas_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400258err_add_mesh:
Dan Williams954ee162007-08-20 11:43:25 -0400259 libertas_remove_card(priv);
260err_prog_firmware:
261 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200262dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400263 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200264
265error:
266 return -ENOMEM;
267}
268
269/**
270 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400271 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200272 * @return N/A
273 */
274static void if_usb_disconnect(struct usb_interface *intf)
275{
276 struct usb_card_rec *cardp = usb_get_intfdata(intf);
277 wlan_private *priv = (wlan_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200278
Dan Williams954ee162007-08-20 11:43:25 -0400279 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200280
Dan Williams954ee162007-08-20 11:43:25 -0400281 /* Update Surprise removed to TRUE */
282 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200283
Holger Schurig435a1ac2007-05-25 12:41:52 -0400284 list_del(&cardp->list);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400285
Dan Williams954ee162007-08-20 11:43:25 -0400286 if (priv) {
287 wlan_adapter *adapter = priv->adapter;
288
289 adapter->surpriseremoved = 1;
290 libertas_stop_card(priv);
291 libertas_remove_mesh(priv);
292 libertas_remove_card(priv);
293 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200294
295 /* Unlink and free urb */
296 if_usb_free(cardp);
297
298 usb_set_intfdata(intf, NULL);
299 usb_put_dev(interface_to_usbdev(intf));
300
Dan Williams954ee162007-08-20 11:43:25 -0400301 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200302}
303
304/**
305 * @brief This function download FW
306 * @param priv pointer to wlan_private
307 * @return 0
308 */
Dan Williams954ee162007-08-20 11:43:25 -0400309static int if_prog_firmware(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200310{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200311 struct FWData *fwdata;
312 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400313 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200314
315 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
316
317 if (!fwdata)
318 return -1;
319
320 fwheader = &fwdata->fwheader;
321
322 if (!cardp->CRC_OK) {
323 cardp->totalbytes = cardp->fwlastblksent;
324 cardp->fwseqnum = cardp->lastseqnum - 1;
325 }
326
Holger Schurig9012b282007-05-25 11:27:16 -0400327 /*
328 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200329 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400330 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200331
332 memcpy(fwheader, &firmware[cardp->totalbytes],
333 sizeof(struct fwheader));
334
335 cardp->fwlastblksent = cardp->totalbytes;
336 cardp->totalbytes += sizeof(struct fwheader);
337
Holger Schurig9012b282007-05-25 11:27:16 -0400338 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200339 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400340 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200341
Holger Schurig9012b282007-05-25 11:27:16 -0400342 /*
343 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400344 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400345 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200346
347 cardp->fwseqnum = cardp->fwseqnum + 1;
348
David Woodhouse981f1872007-05-25 23:36:54 -0400349 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
350 cardp->lastseqnum = cardp->fwseqnum;
351 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200352
David Woodhouse981f1872007-05-25 23:36:54 -0400353 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400354 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400355 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400356 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200357 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
358 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400359 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200360 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400361 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200362
David Woodhousebb793e22007-05-25 23:38:14 -0400363 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400364 /*
365 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200366 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400367 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200368 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400369 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400371 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200372 cardp->fwfinalblk = 1;
373 }
374
Holger Schurig9012b282007-05-25 11:27:16 -0400375 /*
376 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200377 "The firmware download is done size is %d\n",
378 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400379 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200380
381 kfree(fwdata);
382
383 return 0;
384}
385
Dan Williams954ee162007-08-20 11:43:25 -0400386static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200387{
388 int ret;
Dan Williams954ee162007-08-20 11:43:25 -0400389 wlan_private * priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200390
Holger Schurig3874d0f2007-05-25 12:01:42 -0400391 lbs_deb_enter(LBS_DEB_USB);
392
Dan Williamseedc2a32007-08-02 11:36:22 -0400393 /* Try a USB port reset first, if that fails send the reset
394 * command to the firmware.
395 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200396 ret = usb_reset_device(cardp->udev);
Dan Williams954ee162007-08-20 11:43:25 -0400397 if (!ret && priv) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200398 msleep(10);
Dan Williamseedc2a32007-08-02 11:36:22 -0400399 ret = libertas_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200400 msleep(10);
401 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400402
403 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
404
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200405 return ret;
406}
407
408/**
409 * @brief This function transfer the data to the device.
410 * @param priv pointer to wlan_private
411 * @param payload pointer to payload data
412 * @param nb data length
413 * @return 0 or -1
414 */
Dan Williams954ee162007-08-20 11:43:25 -0400415static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200416{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200417 int ret = -1;
418
419 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400420 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400421 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200422 goto tx_ret;
423 }
424
425 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
426 usb_sndbulkpipe(cardp->udev,
427 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400428 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200429
430 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
431
432 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
433 /* transfer failed */
Holger Schurig9012b282007-05-25 11:27:16 -0400434 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200435 ret = -1;
436 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400437 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438 ret = 0;
439 }
440
441tx_ret:
442 return ret;
443}
444
Dan Williams954ee162007-08-20 11:43:25 -0400445static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
446 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200447{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200448 struct sk_buff *skb;
449 struct read_cb_info *rinfo = &cardp->rinfo;
450 int ret = -1;
451
452 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
453 lbs_pr_err("No free skb\n");
454 goto rx_ret;
455 }
456
457 rinfo->skb = skb;
458
459 /* Fill the receive configuration URB and initialise the Rx call back */
460 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
461 usb_rcvbulkpipe(cardp->udev,
462 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400463 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200464 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
465 rinfo);
466
467 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
468
Holger Schurig9012b282007-05-25 11:27:16 -0400469 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200470 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
471 /* handle failure conditions */
Holger Schurig9012b282007-05-25 11:27:16 -0400472 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200473 ret = -1;
474 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400475 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476 ret = 0;
477 }
478
479rx_ret:
480 return ret;
481}
482
Dan Williams954ee162007-08-20 11:43:25 -0400483static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200484{
Dan Williams954ee162007-08-20 11:43:25 -0400485 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486}
487
Dan Williams954ee162007-08-20 11:43:25 -0400488static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200489{
Dan Williams954ee162007-08-20 11:43:25 -0400490 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200491}
492
493static void if_usb_receive_fwload(struct urb *urb)
494{
495 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200496 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400497 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200498 struct fwsyncheader *syncfwheader;
499 struct bootcmdrespStr bootcmdresp;
500
501 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400502 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200503 "URB status is failed during fw load\n");
504 kfree_skb(skb);
505 return;
506 }
507
508 if (cardp->bootcmdresp == 0) {
509 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
510 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400511 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200512 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400513 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200514 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 return;
518 }
David Woodhouse981f1872007-05-25 23:36:54 -0400519 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200520 lbs_pr_info(
521 "boot cmd response wrong magic number (0x%x)\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400522 le32_to_cpu(bootcmdresp.u32magicnumber));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200523 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
524 lbs_pr_info(
525 "boot cmd response cmd_tag error (%d)\n",
526 bootcmdresp.u8cmd_tag);
527 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
528 lbs_pr_info(
529 "boot cmd response result error (%d)\n",
530 bootcmdresp.u8result);
531 } else {
532 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400533 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200534 "Received valid boot command response\n");
535 }
536 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400537 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538 return;
539 }
540
541 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
542 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400543 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200544 kfree_skb(skb);
545 return;
546 }
547
548 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
549 sizeof(struct fwsyncheader));
550
551 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400552 /*
553 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200554 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400555 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200556 "FW received Blk seqnum = %d\n",
557 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400558 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559 cardp->CRC_OK = 1;
560 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400561 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200562 "FW received Blk with CRC error\n");
563 cardp->CRC_OK = 0;
564 }
565
566 kfree_skb(skb);
567
568 if (cardp->fwfinalblk) {
569 cardp->fwdnldover = 1;
570 goto exit;
571 }
572
Dan Williams954ee162007-08-20 11:43:25 -0400573 if_prog_firmware(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200574
Dan Williams954ee162007-08-20 11:43:25 -0400575 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200576exit:
577 kfree(syncfwheader);
578
579 return;
580
581}
582
583#define MRVDRV_MIN_PKT_LEN 30
584
585static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
586 struct usb_card_rec *cardp,
587 wlan_private *priv)
588{
589 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
590 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400591 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200592 "Packet length is Invalid\n");
593 kfree_skb(skb);
594 return;
595 }
596
597 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
598 skb_put(skb, recvlength);
599 skb_pull(skb, MESSAGE_HEADER_LEN);
600 libertas_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400601 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200602}
603
604static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
605 struct sk_buff *skb,
606 struct usb_card_rec *cardp,
607 wlan_private *priv)
608{
609 u8 *cmdbuf;
610 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400611 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200612 "The receive buffer is too large\n");
613 kfree_skb(skb);
614 return;
615 }
616
617 if (!in_interrupt())
618 BUG();
619
620 spin_lock(&priv->adapter->driver_lock);
621 /* take care of cur_cmd = NULL case by reading the
622 * data to clear the interrupt */
623 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400624 cmdbuf = priv->upld_buf;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400625 priv->adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626 } else
627 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
628
Holger Schurigc95c7f92007-08-02 11:49:45 -0400629 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400630 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200631 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400632 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200633
634 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400635 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200636 spin_unlock(&priv->adapter->driver_lock);
637
Holger Schurig9012b282007-05-25 11:27:16 -0400638 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200639 "Wake up main thread to handle cmd response\n");
640
641 return;
642}
643
644/**
645 * @brief This function reads of the packet into the upload buff,
646 * wake up the main thread and initialise the Rx callack.
647 *
648 * @param urb pointer to struct urb
649 * @return N/A
650 */
651static void if_usb_receive(struct urb *urb)
652{
653 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200654 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400655 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
656 wlan_private * priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200657
658 int recvlength = urb->actual_length;
659 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400660 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200661
Holger Schurig9012b282007-05-25 11:27:16 -0400662 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663
664 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400665 __le32 tmp;
666
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200667 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400668 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200669 "URB status is failed\n");
670 kfree_skb(skb);
671 goto setup_for_next;
672 }
673
674 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400675 memcpy(&tmp, recvbuff, sizeof(u32));
676 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400677 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400678 "Recv length = 0x%x, Recv type = 0x%X\n",
679 recvlength, recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200680 } else if (urb->status)
681 goto rx_exit;
682
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683 switch (recvtype) {
684 case CMD_TYPE_DATA:
685 process_cmdtypedata(recvlength, skb, cardp, priv);
686 break;
687
688 case CMD_TYPE_REQUEST:
689 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
690 break;
691
692 case CMD_TYPE_INDICATION:
693 /* Event cause handling */
694 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400695 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400696 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697 cardp->usb_event_cause);
698 if (cardp->usb_event_cause & 0xffff0000) {
699 libertas_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400700 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200701 break;
702 }
David Woodhouse981f1872007-05-25 23:36:54 -0400703 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400704 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200705 kfree_skb(skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400706 libertas_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200707 spin_unlock(&priv->adapter->driver_lock);
708 goto rx_exit;
709 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400710 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
711 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200712 kfree_skb(skb);
713 break;
714 }
715
716setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400717 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200718rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400719 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200720}
721
722/**
723 * @brief This function downloads data to FW
724 * @param priv pointer to wlan_private structure
725 * @param type type of data
726 * @param buf pointer to data buffer
727 * @param len number of bytes
728 * @return 0 or -1
729 */
Holger Schurig208fdd22007-05-25 12:17:06 -0400730static int if_usb_host_to_card(wlan_private * priv, u8 type, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200731{
Holger Schurig634b8f42007-05-25 13:05:16 -0400732 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200733
Holger Schurig9012b282007-05-25 11:27:16 -0400734 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
735 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200736
737 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400738 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400739 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200740 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
741 MESSAGE_HEADER_LEN);
742
743 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400744 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400745 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
747 MESSAGE_HEADER_LEN);
748 }
749
750 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
751
Dan Williams954ee162007-08-20 11:43:25 -0400752 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400753 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200754}
755
756/* called with adapter->driver_lock held */
Holger Schurig208fdd22007-05-25 12:17:06 -0400757static int if_usb_get_int_status(wlan_private * priv, u8 * ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200758{
Holger Schurig634b8f42007-05-25 13:05:16 -0400759 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200760
761 *ireg = cardp->usb_int_cause;
762 cardp->usb_int_cause = 0;
763
Holger Schurig9012b282007-05-25 11:27:16 -0400764 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200765
766 return 0;
767}
768
Holger Schurig208fdd22007-05-25 12:17:06 -0400769static int if_usb_read_event_cause(wlan_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200770{
Holger Schurig634b8f42007-05-25 13:05:16 -0400771 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400772
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200773 priv->adapter->eventcause = cardp->usb_event_cause;
774 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400775 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200776 return 0;
777}
778
Dan Williams9e22cb62007-08-02 11:14:49 -0400779/**
780 * @brief This function issues Boot command to the Boot2 code
781 * @param ivalue 1:Boot from FW by USB-Download
782 * 2:Boot from FW in EEPROM
783 * @return 0
784 */
Dan Williams954ee162007-08-20 11:43:25 -0400785static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400786{
Dan Williams954ee162007-08-20 11:43:25 -0400787 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400788 int i;
789
790 /* Prepare command */
791 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
792 sbootcmd.u8cmd_tag = ivalue;
793 for (i=0; i<11; i++)
794 sbootcmd.au8dumy[i]=0x00;
795 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
796
797 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400798 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400799
800 return 0;
801}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200802
803
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400804/**
805 * @brief This function checks the validity of Boot2/FW image.
806 *
807 * @param data pointer to image
808 * len image length
809 * @return 0 or -1
810 */
811static int check_fwfile_format(u8 *data, u32 totlen)
812{
813 u32 bincmd, exit;
814 u32 blksize, offset, len;
815 int ret;
816
817 ret = 1;
818 exit = len = 0;
819
820 do {
821 struct fwheader *fwh = (void *)data;
822
823 bincmd = le32_to_cpu(fwh->dnldcmd);
824 blksize = le32_to_cpu(fwh->datalength);
825 switch (bincmd) {
826 case FW_HAS_DATA_TO_RECV:
827 offset = sizeof(struct fwheader) + blksize;
828 data += offset;
829 len += offset;
830 if (len >= totlen)
831 exit = 1;
832 break;
833 case FW_HAS_LAST_BLOCK:
834 exit = 1;
835 ret = 0;
836 break;
837 default:
838 exit = 1;
839 break;
840 }
841 } while (!exit);
842
843 if (ret)
844 lbs_pr_err("firmware file format check FAIL\n");
845 else
846 lbs_deb_fw("firmware file format check PASS\n");
847
848 return ret;
849}
850
851
Dan Williams954ee162007-08-20 11:43:25 -0400852static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400853{
Dan Williams954ee162007-08-20 11:43:25 -0400854 int i = 0;
855 static int reset_count = 10;
856 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400857
Dan Williams954ee162007-08-20 11:43:25 -0400858 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400859
Dan Williams954ee162007-08-20 11:43:25 -0400860 if ((ret = request_firmware(&cardp->fw, libertas_fw_name,
861 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400862 lbs_pr_err("request_firmware() failed with %#x\n", ret);
863 lbs_pr_err("firmware %s not found\n", libertas_fw_name);
864 goto done;
865 }
866
Dan Williams954ee162007-08-20 11:43:25 -0400867 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
868 goto release_fw;
869
870restart:
871 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
872 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
873 ret = -1;
874 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400875 }
876
Dan Williams954ee162007-08-20 11:43:25 -0400877 cardp->bootcmdresp = 0;
878 do {
879 int j = 0;
880 i++;
881 /* Issue Boot command = 1, Boot from Download-FW */
882 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
883 /* wait for command response */
884 do {
885 j++;
886 msleep_interruptible(100);
887 } while (cardp->bootcmdresp == 0 && j < 10);
888 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400889
Dan Williams954ee162007-08-20 11:43:25 -0400890 if (cardp->bootcmdresp == 0) {
891 if (--reset_count >= 0) {
892 if_usb_reset_device(cardp);
893 goto restart;
894 }
895 return -1;
896 }
897
898 i = 0;
899
900 cardp->totalbytes = 0;
901 cardp->fwlastblksent = 0;
902 cardp->CRC_OK = 1;
903 cardp->fwdnldover = 0;
904 cardp->fwseqnum = -1;
905 cardp->totalbytes = 0;
906 cardp->fwfinalblk = 0;
907
908 if_prog_firmware(cardp);
909
910 do {
911 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
912 i++;
913 msleep_interruptible(100);
914 if (cardp->surprise_removed || i >= 20)
915 break;
916 } while (!cardp->fwdnldover);
917
918 if (!cardp->fwdnldover) {
919 lbs_pr_info("failed to load fw, resetting device!\n");
920 if (--reset_count >= 0) {
921 if_usb_reset_device(cardp);
922 goto restart;
923 }
924
925 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
926 ret = -1;
927 goto release_fw;
928 }
929
930release_fw:
931 release_firmware(cardp->fw);
932 cardp->fw = NULL;
933
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400934done:
Dan Williams954ee162007-08-20 11:43:25 -0400935 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400936 return ret;
937}
938
939
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200940#ifdef CONFIG_PM
941static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
942{
943 struct usb_card_rec *cardp = usb_get_intfdata(intf);
944 wlan_private *priv = cardp->priv;
945
Holger Schurig9012b282007-05-25 11:27:16 -0400946 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200947
948 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
949 return -1;
950
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400951 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
952 /* Mesh autostart must be activated while sleeping
953 * On resume it will go back to the current state
954 */
955 struct cmd_ds_mesh_access mesh_access;
956 memset(&mesh_access, 0, sizeof(mesh_access));
957 mesh_access.data[0] = cpu_to_le32(1);
958 libertas_prepare_and_send_command(priv,
959 CMD_MESH_ACCESS,
960 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
961 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
962 }
963
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200964 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400965 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200966
967 /* Unlink tx & rx urb */
968 usb_kill_urb(cardp->tx_urb);
969 usb_kill_urb(cardp->rx_urb);
970
971 cardp->rx_urb_recall = 1;
972
Holger Schurig9012b282007-05-25 11:27:16 -0400973 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200974 return 0;
975}
976
977static int if_usb_resume(struct usb_interface *intf)
978{
979 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Javier Cardona51d84f52007-05-25 12:06:56 -0400980 wlan_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200981
Holger Schurig9012b282007-05-25 11:27:16 -0400982 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200983
984 cardp->rx_urb_recall = 0;
985
986 if_usb_submit_rx_urb(cardp->priv);
987
988 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400989 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200990
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400991 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
992 /* Mesh autostart was activated while sleeping
993 * Disable it if appropriate
994 */
995 struct cmd_ds_mesh_access mesh_access;
996 memset(&mesh_access, 0, sizeof(mesh_access));
997 mesh_access.data[0] = cpu_to_le32(0);
998 libertas_prepare_and_send_command(priv,
999 CMD_MESH_ACCESS,
1000 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
1001 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1002 }
1003
Holger Schurig9012b282007-05-25 11:27:16 -04001004 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001005 return 0;
1006}
1007#else
1008#define if_usb_suspend NULL
1009#define if_usb_resume NULL
1010#endif
1011
1012static struct usb_driver if_usb_driver = {
1013 /* driver name */
1014 .name = usbdriver_name,
1015 /* probe function name */
1016 .probe = if_usb_probe,
1017 /* disconnect function name */
1018 .disconnect = if_usb_disconnect,
1019 /* device signature table */
1020 .id_table = if_usb_table,
1021 .suspend = if_usb_suspend,
1022 .resume = if_usb_resume,
1023};
1024
Holger Schurig084708b2007-05-25 12:37:58 -04001025static int if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001026{
Holger Schurig084708b2007-05-25 12:37:58 -04001027 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001028
Holger Schurig084708b2007-05-25 12:37:58 -04001029 lbs_deb_enter(LBS_DEB_MAIN);
1030
1031 if (libertas_fw_name == NULL) {
1032 libertas_fw_name = default_fw_name;
1033 }
1034
1035 ret = usb_register(&if_usb_driver);
1036
1037 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1038 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001039}
1040
Holger Schurig084708b2007-05-25 12:37:58 -04001041static void if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001042{
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -04001043 struct usb_card_rec *cardp, *cardp_temp;
Holger Schurig3874d0f2007-05-25 12:01:42 -04001044
Holger Schurig084708b2007-05-25 12:37:58 -04001045 lbs_deb_enter(LBS_DEB_MAIN);
1046
Dan Williamsb6e99dd2007-08-20 12:22:15 -04001047 list_for_each_entry_safe(cardp, cardp_temp, &usb_devices, list) {
1048 libertas_prepare_and_send_command(cardp->priv, CMD_802_11_RESET,
1049 CMD_ACT_HALT, 0, 0, NULL);
1050 }
Holger Schurig3874d0f2007-05-25 12:01:42 -04001051
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001052 /* API unregisters the driver from USB subsystem */
1053 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001054
1055 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001056}
Holger Schurig084708b2007-05-25 12:37:58 -04001057
1058module_init(if_usb_init_module);
1059module_exit(if_usb_exit_module);
1060
1061MODULE_DESCRIPTION("8388 USB WLAN Driver");
1062MODULE_AUTHOR("Marvell International Ltd.");
1063MODULE_LICENSE("GPL");