blob: f7d2b46340e82c15b10246b3fe43f9cd15df6574 [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"
18
19#define MESSAGE_HEADER_LEN 4
20
21static const char usbdriver_name[] = "usb8xxx";
Holger Schurig084708b2007-05-25 12:37:58 -040022
Andres Salomon82209ad2007-11-20 17:43:32 -050023static char *lbs_fw_name = "usb8388.bin";
Holger Schurig10078322007-11-15 18:05:47 -050024module_param_named(fw_name, lbs_fw_name, charp, 0644);
Holger Schurig084708b2007-05-25 12:37:58 -040025
Holger Schurig435a1ac2007-05-25 12:41:52 -040026/*
27 * We need to send a RESET command to all USB devices before
28 * we tear down the USB connection. Otherwise we would not
29 * be able to re-init device the device if the module gets
30 * loaded again. This is a list of all initialized USB devices,
31 * for the reset code see if_usb_reset_device()
32*/
33static LIST_HEAD(usb_devices);
Holger Schurig3874d0f2007-05-25 12:01:42 -040034
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020035static struct usb_device_id if_usb_table[] = {
36 /* Enter the device signature inside */
Holger Schurig66fcc552007-05-25 00:11:58 -040037 { USB_DEVICE(0x1286, 0x2001) },
38 { USB_DEVICE(0x05a3, 0x8388) },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020039 {} /* Terminating entry */
40};
41
42MODULE_DEVICE_TABLE(usb, if_usb_table);
43
44static void if_usb_receive(struct urb *urb);
45static void if_usb_receive_fwload(struct urb *urb);
Dan Williams954ee162007-08-20 11:43:25 -040046static int if_usb_prog_firmware(struct usb_card_rec *cardp);
Holger Schurig10078322007-11-15 18:05:47 -050047static int if_usb_host_to_card(lbs_private *priv, u8 type, u8 *payload, u16 nb);
48static int if_usb_get_int_status(lbs_private *priv, u8 *);
49static int if_usb_read_event_cause(lbs_private *);
Dan Williams954ee162007-08-20 11:43:25 -040050static int usb_tx_block(struct usb_card_rec *cardp, u8 *payload, u16 nb);
Holger Schurigac558ca2007-08-02 11:49:06 -040051static void if_usb_free(struct usb_card_rec *cardp);
Dan Williams954ee162007-08-20 11:43:25 -040052static int if_usb_submit_rx_urb(struct usb_card_rec *cardp);
53static int if_usb_reset_device(struct usb_card_rec *cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020054
55/**
56 * @brief call back function to handle the status of the URB
57 * @param urb pointer to urb structure
58 * @return N/A
59 */
60static void if_usb_write_bulk_callback(struct urb *urb)
61{
Dan Williams954ee162007-08-20 11:43:25 -040062 struct usb_card_rec *cardp = (struct usb_card_rec *) urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020063
64 /* handle the transmission complete validations */
65
Dan Williams954ee162007-08-20 11:43:25 -040066 if (urb->status == 0) {
Holger Schurig10078322007-11-15 18:05:47 -050067 lbs_private *priv = cardp->priv;
Dan Williams954ee162007-08-20 11:43:25 -040068
Holger Schurig9012b282007-05-25 11:27:16 -040069 /*
70 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
71 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020072 urb->actual_length);
Holger Schurig9012b282007-05-25 11:27:16 -040073 */
Dan Williams954ee162007-08-20 11:43:25 -040074
75 /* Used for both firmware TX and regular TX. priv isn't
76 * valid at firmware load time.
77 */
78 if (priv) {
Holger Schurig10078322007-11-15 18:05:47 -050079 lbs_adapter *adapter = priv->adapter;
Dan Williams954ee162007-08-20 11:43:25 -040080 struct net_device *dev = priv->dev;
81
82 priv->dnld_sent = DNLD_RES_RECEIVED;
83
84 /* Wake main thread if commands are pending */
85 if (!adapter->cur_cmd)
86 wake_up_interruptible(&priv->waitq);
87
Holger Schurig10078322007-11-15 18:05:47 -050088 if ((adapter->connect_status == LBS_CONNECTED)) {
Dan Williams954ee162007-08-20 11:43:25 -040089 netif_wake_queue(dev);
90 netif_wake_queue(priv->mesh_dev);
91 }
Javier Cardona51d84f52007-05-25 12:06:56 -040092 }
Dan Williams954ee162007-08-20 11:43:25 -040093 } else {
94 /* print the failure status number for debug */
95 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020096 }
97
98 return;
99}
100
101/**
102 * @brief free tx/rx urb, skb and rx buffer
103 * @param cardp pointer usb_card_rec
104 * @return N/A
105 */
Holger Schurigac558ca2007-08-02 11:49:06 -0400106static void if_usb_free(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200107{
Holger Schurig9012b282007-05-25 11:27:16 -0400108 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200109
110 /* Unlink tx & rx urb */
111 usb_kill_urb(cardp->tx_urb);
112 usb_kill_urb(cardp->rx_urb);
113
114 usb_free_urb(cardp->tx_urb);
115 cardp->tx_urb = NULL;
116
117 usb_free_urb(cardp->rx_urb);
118 cardp->rx_urb = NULL;
119
120 kfree(cardp->bulk_out_buffer);
121 cardp->bulk_out_buffer = NULL;
122
Holger Schurig9012b282007-05-25 11:27:16 -0400123 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200124}
125
126/**
127 * @brief sets the configuration values
128 * @param ifnum interface number
129 * @param id pointer to usb_device_id
130 * @return 0 on success, error code on failure
131 */
132static int if_usb_probe(struct usb_interface *intf,
133 const struct usb_device_id *id)
134{
135 struct usb_device *udev;
136 struct usb_host_interface *iface_desc;
137 struct usb_endpoint_descriptor *endpoint;
Holger Schurig10078322007-11-15 18:05:47 -0500138 lbs_private *priv;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400139 struct usb_card_rec *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200140 int i;
141
142 udev = interface_to_usbdev(intf);
143
Holger Schurig435a1ac2007-05-25 12:41:52 -0400144 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
145 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200146 lbs_pr_err("Out of memory allocating private data.\n");
147 goto error;
148 }
149
Holger Schurig435a1ac2007-05-25 12:41:52 -0400150 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200151 iface_desc = intf->cur_altsetting;
152
Holger Schurig9012b282007-05-25 11:27:16 -0400153 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200154 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400155 le16_to_cpu(udev->descriptor.bcdUSB),
156 udev->descriptor.bDeviceClass,
157 udev->descriptor.bDeviceSubClass,
158 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200159
160 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
161 endpoint = &iface_desc->endpoint[i].desc;
162 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
163 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
164 USB_ENDPOINT_XFER_BULK)) {
165 /* we found a bulk in endpoint */
Holger Schurig9012b282007-05-25 11:27:16 -0400166 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400167 le16_to_cpu(endpoint->wMaxPacketSize));
168 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400169 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200170 "Rx URB allocation failed\n");
171 goto dealloc;
172 }
Holger Schurig435a1ac2007-05-25 12:41:52 -0400173 cardp->rx_urb_recall = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200174
Holger Schurig435a1ac2007-05-25 12:41:52 -0400175 cardp->bulk_in_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400176 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400177 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200178 (endpoint->
179 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400180 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200181 endpoint->bEndpointAddress);
182 }
183
184 if (((endpoint->
185 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
186 USB_DIR_OUT)
187 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
188 USB_ENDPOINT_XFER_BULK)) {
189 /* We found bulk out endpoint */
David Woodhouse981f1872007-05-25 23:36:54 -0400190 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400191 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200192 "Tx URB allocation failed\n");
193 goto dealloc;
194 }
195
Holger Schurig435a1ac2007-05-25 12:41:52 -0400196 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400197 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400198 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400199 "Bulk out size is %d\n",
200 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400201 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200202 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400203 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200204 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400205 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200206 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
207 GFP_KERNEL);
208
Holger Schurig435a1ac2007-05-25 12:41:52 -0400209 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400210 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200211 "Could not allocate buffer\n");
212 goto dealloc;
213 }
214 }
215 }
216
Dan Williams954ee162007-08-20 11:43:25 -0400217 /* Upload firmware */
218 cardp->rinfo.cardp = cardp;
219 if (if_usb_prog_firmware(cardp)) {
220 lbs_deb_usbd(&udev->dev, "FW upload failed");
221 goto err_prog_firmware;
222 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400223
Holger Schurig10078322007-11-15 18:05:47 -0500224 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400225 goto err_prog_firmware;
226
227 cardp->priv = priv;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400228
Holger Schurig10078322007-11-15 18:05:47 -0500229 if (lbs_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400230 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400231
Dan Williams954ee162007-08-20 11:43:25 -0400232 cardp->eth_dev = priv->dev;
233
Holger Schurig208fdd22007-05-25 12:17:06 -0400234 priv->hw_host_to_card = if_usb_host_to_card;
235 priv->hw_get_int_status = if_usb_get_int_status;
236 priv->hw_read_event_cause = if_usb_read_event_cause;
Luis Carlos Cobo63f00232007-08-02 13:19:24 -0400237 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400238
Dan Williams954ee162007-08-20 11:43:25 -0400239 /* Delay 200 ms to waiting for the FW ready */
240 if_usb_submit_rx_urb(cardp);
241 msleep_interruptible(200);
242 priv->adapter->fw_ready = 1;
243
Holger Schurig10078322007-11-15 18:05:47 -0500244 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400245 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400246
Holger Schurig435a1ac2007-05-25 12:41:52 -0400247 list_add_tail(&cardp->list, &usb_devices);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400248
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200249 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400250 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200251
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200252 return 0;
253
Dan Williams954ee162007-08-20 11:43:25 -0400254err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500255 lbs_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400256err_add_mesh:
Holger Schurig10078322007-11-15 18:05:47 -0500257 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400258err_prog_firmware:
259 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200260dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400261 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200262
263error:
264 return -ENOMEM;
265}
266
267/**
268 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400269 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200270 * @return N/A
271 */
272static void if_usb_disconnect(struct usb_interface *intf)
273{
274 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig10078322007-11-15 18:05:47 -0500275 lbs_private *priv = (lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200276
Dan Williams954ee162007-08-20 11:43:25 -0400277 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200278
Dan Williams954ee162007-08-20 11:43:25 -0400279 /* Update Surprise removed to TRUE */
280 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200281
Holger Schurig435a1ac2007-05-25 12:41:52 -0400282 list_del(&cardp->list);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400283
Dan Williams954ee162007-08-20 11:43:25 -0400284 if (priv) {
Holger Schurig10078322007-11-15 18:05:47 -0500285 lbs_adapter *adapter = priv->adapter;
Dan Williams954ee162007-08-20 11:43:25 -0400286
287 adapter->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500288 lbs_stop_card(priv);
289 lbs_remove_mesh(priv);
290 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400291 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200292
293 /* Unlink and free urb */
294 if_usb_free(cardp);
295
296 usb_set_intfdata(intf, NULL);
297 usb_put_dev(interface_to_usbdev(intf));
298
Dan Williams954ee162007-08-20 11:43:25 -0400299 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200300}
301
302/**
303 * @brief This function download FW
Holger Schurig10078322007-11-15 18:05:47 -0500304 * @param priv pointer to lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200305 * @return 0
306 */
Dan Williams954ee162007-08-20 11:43:25 -0400307static int if_prog_firmware(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200308{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200309 struct FWData *fwdata;
310 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400311 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200312
313 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
314
315 if (!fwdata)
316 return -1;
317
318 fwheader = &fwdata->fwheader;
319
320 if (!cardp->CRC_OK) {
321 cardp->totalbytes = cardp->fwlastblksent;
322 cardp->fwseqnum = cardp->lastseqnum - 1;
323 }
324
Holger Schurig9012b282007-05-25 11:27:16 -0400325 /*
326 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200327 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400328 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200329
330 memcpy(fwheader, &firmware[cardp->totalbytes],
331 sizeof(struct fwheader));
332
333 cardp->fwlastblksent = cardp->totalbytes;
334 cardp->totalbytes += sizeof(struct fwheader);
335
Holger Schurig9012b282007-05-25 11:27:16 -0400336 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200337 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400338 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200339
Holger Schurig9012b282007-05-25 11:27:16 -0400340 /*
341 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400342 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400343 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200344
345 cardp->fwseqnum = cardp->fwseqnum + 1;
346
David Woodhouse981f1872007-05-25 23:36:54 -0400347 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
348 cardp->lastseqnum = cardp->fwseqnum;
349 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200350
David Woodhouse981f1872007-05-25 23:36:54 -0400351 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400352 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400353 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400354 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200355 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
356 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400357 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200358 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400359 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200360
David Woodhousebb793e22007-05-25 23:38:14 -0400361 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400362 /*
363 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200364 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400365 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200366 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400367 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200368 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400369 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370 cardp->fwfinalblk = 1;
371 }
372
Holger Schurig9012b282007-05-25 11:27:16 -0400373 /*
374 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200375 "The firmware download is done size is %d\n",
376 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400377 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200378
379 kfree(fwdata);
380
381 return 0;
382}
383
Dan Williams954ee162007-08-20 11:43:25 -0400384static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200385{
386 int ret;
Holger Schurig10078322007-11-15 18:05:47 -0500387 lbs_private * priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200388
Holger Schurig3874d0f2007-05-25 12:01:42 -0400389 lbs_deb_enter(LBS_DEB_USB);
390
Dan Williamseedc2a32007-08-02 11:36:22 -0400391 /* Try a USB port reset first, if that fails send the reset
392 * command to the firmware.
393 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200394 ret = usb_reset_device(cardp->udev);
Dan Williams954ee162007-08-20 11:43:25 -0400395 if (!ret && priv) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200396 msleep(10);
Holger Schurig10078322007-11-15 18:05:47 -0500397 ret = lbs_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200398 msleep(10);
399 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400400
401 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
402
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200403 return ret;
404}
405
406/**
407 * @brief This function transfer the data to the device.
Holger Schurig10078322007-11-15 18:05:47 -0500408 * @param priv pointer to lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200409 * @param payload pointer to payload data
410 * @param nb data length
411 * @return 0 or -1
412 */
Dan Williams954ee162007-08-20 11:43:25 -0400413static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200414{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200415 int ret = -1;
416
417 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400418 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400419 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200420 goto tx_ret;
421 }
422
423 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
424 usb_sndbulkpipe(cardp->udev,
425 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400426 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200427
428 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
429
430 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
431 /* transfer failed */
Holger Schurig9012b282007-05-25 11:27:16 -0400432 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200433 ret = -1;
434 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400435 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200436 ret = 0;
437 }
438
439tx_ret:
440 return ret;
441}
442
Dan Williams954ee162007-08-20 11:43:25 -0400443static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
444 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200445{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446 struct sk_buff *skb;
447 struct read_cb_info *rinfo = &cardp->rinfo;
448 int ret = -1;
449
450 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
451 lbs_pr_err("No free skb\n");
452 goto rx_ret;
453 }
454
455 rinfo->skb = skb;
456
457 /* Fill the receive configuration URB and initialise the Rx call back */
458 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
459 usb_rcvbulkpipe(cardp->udev,
460 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400461 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200462 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
463 rinfo);
464
465 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
466
Holger Schurig9012b282007-05-25 11:27:16 -0400467 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200468 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
469 /* handle failure conditions */
Holger Schurig9012b282007-05-25 11:27:16 -0400470 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200471 ret = -1;
472 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400473 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200474 ret = 0;
475 }
476
477rx_ret:
478 return ret;
479}
480
Dan Williams954ee162007-08-20 11:43:25 -0400481static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200482{
Dan Williams954ee162007-08-20 11:43:25 -0400483 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200484}
485
Dan Williams954ee162007-08-20 11:43:25 -0400486static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200487{
Dan Williams954ee162007-08-20 11:43:25 -0400488 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200489}
490
491static void if_usb_receive_fwload(struct urb *urb)
492{
493 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200494 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400495 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200496 struct fwsyncheader *syncfwheader;
497 struct bootcmdrespStr bootcmdresp;
498
499 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400500 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200501 "URB status is failed during fw load\n");
502 kfree_skb(skb);
503 return;
504 }
505
506 if (cardp->bootcmdresp == 0) {
507 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
508 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400509 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400511 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200512 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400513 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200514 "Received valid boot command response\n");
515 return;
516 }
David Woodhouse981f1872007-05-25 23:36:54 -0400517 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200518 lbs_pr_info(
519 "boot cmd response wrong magic number (0x%x)\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400520 le32_to_cpu(bootcmdresp.u32magicnumber));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200521 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
522 lbs_pr_info(
523 "boot cmd response cmd_tag error (%d)\n",
524 bootcmdresp.u8cmd_tag);
525 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
526 lbs_pr_info(
527 "boot cmd response result error (%d)\n",
528 bootcmdresp.u8result);
529 } else {
530 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400531 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200532 "Received valid boot command response\n");
533 }
534 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400535 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200536 return;
537 }
538
539 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
540 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400541 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200542 kfree_skb(skb);
543 return;
544 }
545
546 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
547 sizeof(struct fwsyncheader));
548
549 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400550 /*
551 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200552 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400553 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200554 "FW received Blk seqnum = %d\n",
555 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400556 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200557 cardp->CRC_OK = 1;
558 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400559 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200560 "FW received Blk with CRC error\n");
561 cardp->CRC_OK = 0;
562 }
563
564 kfree_skb(skb);
565
566 if (cardp->fwfinalblk) {
567 cardp->fwdnldover = 1;
568 goto exit;
569 }
570
Dan Williams954ee162007-08-20 11:43:25 -0400571 if_prog_firmware(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200572
Dan Williams954ee162007-08-20 11:43:25 -0400573 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200574exit:
575 kfree(syncfwheader);
576
577 return;
578
579}
580
581#define MRVDRV_MIN_PKT_LEN 30
582
583static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
584 struct usb_card_rec *cardp,
Holger Schurig10078322007-11-15 18:05:47 -0500585 lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200586{
587 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
588 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400589 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200590 "Packet length is Invalid\n");
591 kfree_skb(skb);
592 return;
593 }
594
595 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
596 skb_put(skb, recvlength);
597 skb_pull(skb, MESSAGE_HEADER_LEN);
Holger Schurig10078322007-11-15 18:05:47 -0500598 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400599 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200600}
601
602static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
603 struct sk_buff *skb,
604 struct usb_card_rec *cardp,
Holger Schurig10078322007-11-15 18:05:47 -0500605 lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200606{
607 u8 *cmdbuf;
608 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400609 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200610 "The receive buffer is too large\n");
611 kfree_skb(skb);
612 return;
613 }
614
615 if (!in_interrupt())
616 BUG();
617
618 spin_lock(&priv->adapter->driver_lock);
619 /* take care of cur_cmd = NULL case by reading the
620 * data to clear the interrupt */
621 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400622 cmdbuf = priv->upld_buf;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400623 priv->adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200624 } else
625 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
626
Holger Schurigc95c7f92007-08-02 11:49:45 -0400627 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400628 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200629 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400630 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200631
632 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500633 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634 spin_unlock(&priv->adapter->driver_lock);
635
Holger Schurig9012b282007-05-25 11:27:16 -0400636 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200637 "Wake up main thread to handle cmd response\n");
638
639 return;
640}
641
642/**
643 * @brief This function reads of the packet into the upload buff,
644 * wake up the main thread and initialise the Rx callack.
645 *
646 * @param urb pointer to struct urb
647 * @return N/A
648 */
649static void if_usb_receive(struct urb *urb)
650{
651 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200652 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400653 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
Holger Schurig10078322007-11-15 18:05:47 -0500654 lbs_private * priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200655
656 int recvlength = urb->actual_length;
657 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400658 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200659
Holger Schurig9012b282007-05-25 11:27:16 -0400660 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200661
662 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400663 __le32 tmp;
664
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200665 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400666 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200667 "URB status is failed\n");
668 kfree_skb(skb);
669 goto setup_for_next;
670 }
671
672 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400673 memcpy(&tmp, recvbuff, sizeof(u32));
674 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400675 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400676 "Recv length = 0x%x, Recv type = 0x%X\n",
677 recvlength, recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200678 } else if (urb->status)
679 goto rx_exit;
680
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200681 switch (recvtype) {
682 case CMD_TYPE_DATA:
683 process_cmdtypedata(recvlength, skb, cardp, priv);
684 break;
685
686 case CMD_TYPE_REQUEST:
687 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
688 break;
689
690 case CMD_TYPE_INDICATION:
691 /* Event cause handling */
692 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400693 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400694 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200695 cardp->usb_event_cause);
696 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500697 lbs_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400698 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699 break;
700 }
David Woodhouse981f1872007-05-25 23:36:54 -0400701 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400702 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500704 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200705 spin_unlock(&priv->adapter->driver_lock);
706 goto rx_exit;
707 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400708 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
709 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710 kfree_skb(skb);
711 break;
712 }
713
714setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400715 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400717 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200718}
719
720/**
721 * @brief This function downloads data to FW
Holger Schurig10078322007-11-15 18:05:47 -0500722 * @param priv pointer to lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200723 * @param type type of data
724 * @param buf pointer to data buffer
725 * @param len number of bytes
726 * @return 0 or -1
727 */
Holger Schurig10078322007-11-15 18:05:47 -0500728static int if_usb_host_to_card(lbs_private *priv, u8 type, u8 *payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729{
Holger Schurig634b8f42007-05-25 13:05:16 -0400730 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200731
Holger Schurig9012b282007-05-25 11:27:16 -0400732 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
733 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200734
735 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400736 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400737 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200738 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
739 MESSAGE_HEADER_LEN);
740
741 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400742 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400743 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200744 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
745 MESSAGE_HEADER_LEN);
746 }
747
748 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
749
Dan Williams954ee162007-08-20 11:43:25 -0400750 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400751 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200752}
753
754/* called with adapter->driver_lock held */
Holger Schurig10078322007-11-15 18:05:47 -0500755static int if_usb_get_int_status(lbs_private *priv, u8 *ireg)
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
759 *ireg = cardp->usb_int_cause;
760 cardp->usb_int_cause = 0;
761
Holger Schurig9012b282007-05-25 11:27:16 -0400762 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200763
764 return 0;
765}
766
Holger Schurig10078322007-11-15 18:05:47 -0500767static int if_usb_read_event_cause(lbs_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200768{
Holger Schurig634b8f42007-05-25 13:05:16 -0400769 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400770
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200771 priv->adapter->eventcause = cardp->usb_event_cause;
772 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400773 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200774 return 0;
775}
776
Dan Williams9e22cb62007-08-02 11:14:49 -0400777/**
778 * @brief This function issues Boot command to the Boot2 code
779 * @param ivalue 1:Boot from FW by USB-Download
780 * 2:Boot from FW in EEPROM
781 * @return 0
782 */
Dan Williams954ee162007-08-20 11:43:25 -0400783static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400784{
Dan Williams954ee162007-08-20 11:43:25 -0400785 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400786 int i;
787
788 /* Prepare command */
789 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
790 sbootcmd.u8cmd_tag = ivalue;
791 for (i=0; i<11; i++)
792 sbootcmd.au8dumy[i]=0x00;
793 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
794
795 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400796 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400797
798 return 0;
799}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200800
801
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400802/**
803 * @brief This function checks the validity of Boot2/FW image.
804 *
805 * @param data pointer to image
806 * len image length
807 * @return 0 or -1
808 */
809static int check_fwfile_format(u8 *data, u32 totlen)
810{
811 u32 bincmd, exit;
812 u32 blksize, offset, len;
813 int ret;
814
815 ret = 1;
816 exit = len = 0;
817
818 do {
819 struct fwheader *fwh = (void *)data;
820
821 bincmd = le32_to_cpu(fwh->dnldcmd);
822 blksize = le32_to_cpu(fwh->datalength);
823 switch (bincmd) {
824 case FW_HAS_DATA_TO_RECV:
825 offset = sizeof(struct fwheader) + blksize;
826 data += offset;
827 len += offset;
828 if (len >= totlen)
829 exit = 1;
830 break;
831 case FW_HAS_LAST_BLOCK:
832 exit = 1;
833 ret = 0;
834 break;
835 default:
836 exit = 1;
837 break;
838 }
839 } while (!exit);
840
841 if (ret)
842 lbs_pr_err("firmware file format check FAIL\n");
843 else
844 lbs_deb_fw("firmware file format check PASS\n");
845
846 return ret;
847}
848
849
Dan Williams954ee162007-08-20 11:43:25 -0400850static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400851{
Dan Williams954ee162007-08-20 11:43:25 -0400852 int i = 0;
853 static int reset_count = 10;
854 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400855
Dan Williams954ee162007-08-20 11:43:25 -0400856 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400857
Holger Schurig10078322007-11-15 18:05:47 -0500858 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400859 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400860 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500861 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400862 goto done;
863 }
864
Dan Williams954ee162007-08-20 11:43:25 -0400865 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
866 goto release_fw;
867
868restart:
869 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
870 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
871 ret = -1;
872 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400873 }
874
Dan Williams954ee162007-08-20 11:43:25 -0400875 cardp->bootcmdresp = 0;
876 do {
877 int j = 0;
878 i++;
879 /* Issue Boot command = 1, Boot from Download-FW */
880 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
881 /* wait for command response */
882 do {
883 j++;
884 msleep_interruptible(100);
885 } while (cardp->bootcmdresp == 0 && j < 10);
886 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400887
Dan Williams954ee162007-08-20 11:43:25 -0400888 if (cardp->bootcmdresp == 0) {
889 if (--reset_count >= 0) {
890 if_usb_reset_device(cardp);
891 goto restart;
892 }
893 return -1;
894 }
895
896 i = 0;
897
898 cardp->totalbytes = 0;
899 cardp->fwlastblksent = 0;
900 cardp->CRC_OK = 1;
901 cardp->fwdnldover = 0;
902 cardp->fwseqnum = -1;
903 cardp->totalbytes = 0;
904 cardp->fwfinalblk = 0;
905
906 if_prog_firmware(cardp);
907
908 do {
909 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
910 i++;
911 msleep_interruptible(100);
912 if (cardp->surprise_removed || i >= 20)
913 break;
914 } while (!cardp->fwdnldover);
915
916 if (!cardp->fwdnldover) {
917 lbs_pr_info("failed to load fw, resetting device!\n");
918 if (--reset_count >= 0) {
919 if_usb_reset_device(cardp);
920 goto restart;
921 }
922
923 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
924 ret = -1;
925 goto release_fw;
926 }
927
928release_fw:
929 release_firmware(cardp->fw);
930 cardp->fw = NULL;
931
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400932done:
Dan Williams954ee162007-08-20 11:43:25 -0400933 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400934 return ret;
935}
936
937
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200938#ifdef CONFIG_PM
939static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
940{
941 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig10078322007-11-15 18:05:47 -0500942 lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200943
Holger Schurig9012b282007-05-25 11:27:16 -0400944 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200945
946 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
947 return -1;
948
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400949 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
950 /* Mesh autostart must be activated while sleeping
951 * On resume it will go back to the current state
952 */
953 struct cmd_ds_mesh_access mesh_access;
954 memset(&mesh_access, 0, sizeof(mesh_access));
955 mesh_access.data[0] = cpu_to_le32(1);
Holger Schurig10078322007-11-15 18:05:47 -0500956 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400957 CMD_MESH_ACCESS,
958 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
959 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
960 }
961
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200962 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400963 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200964
965 /* Unlink tx & rx urb */
966 usb_kill_urb(cardp->tx_urb);
967 usb_kill_urb(cardp->rx_urb);
968
969 cardp->rx_urb_recall = 1;
970
Holger Schurig9012b282007-05-25 11:27:16 -0400971 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200972 return 0;
973}
974
975static int if_usb_resume(struct usb_interface *intf)
976{
977 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig10078322007-11-15 18:05:47 -0500978 lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200979
Holger Schurig9012b282007-05-25 11:27:16 -0400980 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200981
982 cardp->rx_urb_recall = 0;
983
984 if_usb_submit_rx_urb(cardp->priv);
985
986 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400987 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200988
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400989 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
990 /* Mesh autostart was activated while sleeping
991 * Disable it if appropriate
992 */
993 struct cmd_ds_mesh_access mesh_access;
994 memset(&mesh_access, 0, sizeof(mesh_access));
995 mesh_access.data[0] = cpu_to_le32(0);
Holger Schurig10078322007-11-15 18:05:47 -0500996 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400997 CMD_MESH_ACCESS,
998 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
999 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1000 }
1001
Holger Schurig9012b282007-05-25 11:27:16 -04001002 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001003 return 0;
1004}
1005#else
1006#define if_usb_suspend NULL
1007#define if_usb_resume NULL
1008#endif
1009
1010static struct usb_driver if_usb_driver = {
1011 /* driver name */
1012 .name = usbdriver_name,
1013 /* probe function name */
1014 .probe = if_usb_probe,
1015 /* disconnect function name */
1016 .disconnect = if_usb_disconnect,
1017 /* device signature table */
1018 .id_table = if_usb_table,
1019 .suspend = if_usb_suspend,
1020 .resume = if_usb_resume,
1021};
1022
Andres Salomon4fb910f2007-11-20 17:43:45 -05001023static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001024{
Holger Schurig084708b2007-05-25 12:37:58 -04001025 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001026
Holger Schurig084708b2007-05-25 12:37:58 -04001027 lbs_deb_enter(LBS_DEB_MAIN);
1028
Holger Schurig084708b2007-05-25 12:37:58 -04001029 ret = usb_register(&if_usb_driver);
1030
1031 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1032 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001033}
1034
Andres Salomon4fb910f2007-11-20 17:43:45 -05001035static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001036{
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -04001037 struct usb_card_rec *cardp, *cardp_temp;
Holger Schurig3874d0f2007-05-25 12:01:42 -04001038
Holger Schurig084708b2007-05-25 12:37:58 -04001039 lbs_deb_enter(LBS_DEB_MAIN);
1040
Dan Williamsb6e99dd2007-08-20 12:22:15 -04001041 list_for_each_entry_safe(cardp, cardp_temp, &usb_devices, list) {
Holger Schurig10078322007-11-15 18:05:47 -05001042 lbs_prepare_and_send_command(cardp->priv, CMD_802_11_RESET,
Dan Williamsb6e99dd2007-08-20 12:22:15 -04001043 CMD_ACT_HALT, 0, 0, NULL);
1044 }
Holger Schurig3874d0f2007-05-25 12:01:42 -04001045
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001046 /* API unregisters the driver from USB subsystem */
1047 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001048
1049 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050}
Holger Schurig084708b2007-05-25 12:37:58 -04001051
1052module_init(if_usb_init_module);
1053module_exit(if_usb_exit_module);
1054
1055MODULE_DESCRIPTION("8388 USB WLAN Driver");
1056MODULE_AUTHOR("Marvell International Ltd.");
1057MODULE_LICENSE("GPL");