blob: 418dcab8b1cc48cb3ec9732c59d9eec344628704 [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>
8#include <linux/usb.h>
9
Holger Schurigec3eef22007-05-25 12:49:10 -040010#define DRV_NAME "usb8xxx"
11
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020012#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020013#include "decl.h"
14#include "defs.h"
15#include "dev.h"
16#include "if_usb.h"
17
18#define MESSAGE_HEADER_LEN 4
19
Andres Salomon82209ad2007-11-20 17:43:32 -050020static char *lbs_fw_name = "usb8388.bin";
Holger Schurig10078322007-11-15 18:05:47 -050021module_param_named(fw_name, lbs_fw_name, charp, 0644);
Holger Schurig084708b2007-05-25 12:37:58 -040022
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020023static struct usb_device_id if_usb_table[] = {
24 /* Enter the device signature inside */
Holger Schurig66fcc552007-05-25 00:11:58 -040025 { USB_DEVICE(0x1286, 0x2001) },
26 { USB_DEVICE(0x05a3, 0x8388) },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020027 {} /* Terminating entry */
28};
29
30MODULE_DEVICE_TABLE(usb, if_usb_table);
31
32static void if_usb_receive(struct urb *urb);
33static void if_usb_receive_fwload(struct urb *urb);
Dan Williams954ee162007-08-20 11:43:25 -040034static int if_usb_prog_firmware(struct usb_card_rec *cardp);
Holger Schurig69f90322007-11-23 15:43:44 +010035static int if_usb_host_to_card(struct lbs_private *priv,
36 u8 type,
37 u8 *payload,
38 u16 nb);
39static int if_usb_get_int_status(struct lbs_private *priv, u8 *);
40static int if_usb_read_event_cause(struct lbs_private *);
Dan Williams954ee162007-08-20 11:43:25 -040041static int usb_tx_block(struct usb_card_rec *cardp, u8 *payload, u16 nb);
Holger Schurigac558ca2007-08-02 11:49:06 -040042static void if_usb_free(struct usb_card_rec *cardp);
Dan Williams954ee162007-08-20 11:43:25 -040043static int if_usb_submit_rx_urb(struct usb_card_rec *cardp);
44static int if_usb_reset_device(struct usb_card_rec *cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020045
46/**
47 * @brief call back function to handle the status of the URB
48 * @param urb pointer to urb structure
49 * @return N/A
50 */
51static void if_usb_write_bulk_callback(struct urb *urb)
52{
Dan Williams954ee162007-08-20 11:43:25 -040053 struct usb_card_rec *cardp = (struct usb_card_rec *) urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020054
55 /* handle the transmission complete validations */
56
Dan Williams954ee162007-08-20 11:43:25 -040057 if (urb->status == 0) {
Holger Schurig69f90322007-11-23 15:43:44 +010058 struct lbs_private *priv = cardp->priv;
Dan Williams954ee162007-08-20 11:43:25 -040059
Holger Schurig9012b282007-05-25 11:27:16 -040060 /*
61 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
62 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020063 urb->actual_length);
Holger Schurig9012b282007-05-25 11:27:16 -040064 */
Dan Williams954ee162007-08-20 11:43:25 -040065
66 /* Used for both firmware TX and regular TX. priv isn't
67 * valid at firmware load time.
68 */
David Woodhousee775ed72007-12-06 14:36:11 +000069 if (priv)
70 lbs_host_to_card_done(priv);
Dan Williams954ee162007-08-20 11:43:25 -040071 } else {
72 /* print the failure status number for debug */
73 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020074 }
75
76 return;
77}
78
79/**
80 * @brief free tx/rx urb, skb and rx buffer
81 * @param cardp pointer usb_card_rec
82 * @return N/A
83 */
Holger Schurigac558ca2007-08-02 11:49:06 -040084static void if_usb_free(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020085{
Holger Schurig9012b282007-05-25 11:27:16 -040086 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020087
88 /* Unlink tx & rx urb */
89 usb_kill_urb(cardp->tx_urb);
90 usb_kill_urb(cardp->rx_urb);
91
92 usb_free_urb(cardp->tx_urb);
93 cardp->tx_urb = NULL;
94
95 usb_free_urb(cardp->rx_urb);
96 cardp->rx_urb = NULL;
97
98 kfree(cardp->bulk_out_buffer);
99 cardp->bulk_out_buffer = NULL;
100
Holger Schurig9012b282007-05-25 11:27:16 -0400101 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200102}
103
104/**
105 * @brief sets the configuration values
106 * @param ifnum interface number
107 * @param id pointer to usb_device_id
108 * @return 0 on success, error code on failure
109 */
110static int if_usb_probe(struct usb_interface *intf,
111 const struct usb_device_id *id)
112{
113 struct usb_device *udev;
114 struct usb_host_interface *iface_desc;
115 struct usb_endpoint_descriptor *endpoint;
Holger Schurig69f90322007-11-23 15:43:44 +0100116 struct lbs_private *priv;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400117 struct usb_card_rec *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200118 int i;
119
120 udev = interface_to_usbdev(intf);
121
Holger Schurig435a1ac2007-05-25 12:41:52 -0400122 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
123 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200124 lbs_pr_err("Out of memory allocating private data.\n");
125 goto error;
126 }
127
Holger Schurig435a1ac2007-05-25 12:41:52 -0400128 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200129 iface_desc = intf->cur_altsetting;
130
Holger Schurig9012b282007-05-25 11:27:16 -0400131 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200132 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400133 le16_to_cpu(udev->descriptor.bcdUSB),
134 udev->descriptor.bDeviceClass,
135 udev->descriptor.bDeviceSubClass,
136 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200137
138 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
139 endpoint = &iface_desc->endpoint[i].desc;
140 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
141 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
142 USB_ENDPOINT_XFER_BULK)) {
143 /* we found a bulk in endpoint */
Holger Schurig9012b282007-05-25 11:27:16 -0400144 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400145 le16_to_cpu(endpoint->wMaxPacketSize));
146 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400147 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200148 "Rx URB allocation failed\n");
149 goto dealloc;
150 }
Holger Schurig435a1ac2007-05-25 12:41:52 -0400151 cardp->rx_urb_recall = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200152
Holger Schurig435a1ac2007-05-25 12:41:52 -0400153 cardp->bulk_in_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400154 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400155 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200156 (endpoint->
157 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400158 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200159 endpoint->bEndpointAddress);
160 }
161
162 if (((endpoint->
163 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
164 USB_DIR_OUT)
165 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
166 USB_ENDPOINT_XFER_BULK)) {
167 /* We found bulk out endpoint */
David Woodhouse981f1872007-05-25 23:36:54 -0400168 if (!(cardp->tx_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 "Tx URB allocation failed\n");
171 goto dealloc;
172 }
173
Holger Schurig435a1ac2007-05-25 12:41:52 -0400174 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400175 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400176 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400177 "Bulk out size is %d\n",
178 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400179 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200180 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400181 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200182 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400183 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200184 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
185 GFP_KERNEL);
186
Holger Schurig435a1ac2007-05-25 12:41:52 -0400187 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400188 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200189 "Could not allocate buffer\n");
190 goto dealloc;
191 }
192 }
193 }
194
Dan Williams954ee162007-08-20 11:43:25 -0400195 /* Upload firmware */
196 cardp->rinfo.cardp = cardp;
197 if (if_usb_prog_firmware(cardp)) {
198 lbs_deb_usbd(&udev->dev, "FW upload failed");
199 goto err_prog_firmware;
200 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400201
Holger Schurig10078322007-11-15 18:05:47 -0500202 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400203 goto err_prog_firmware;
204
205 cardp->priv = priv;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400206
Holger Schurig10078322007-11-15 18:05:47 -0500207 if (lbs_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400208 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400209
Dan Williams954ee162007-08-20 11:43:25 -0400210 cardp->eth_dev = priv->dev;
211
Holger Schurig208fdd22007-05-25 12:17:06 -0400212 priv->hw_host_to_card = if_usb_host_to_card;
213 priv->hw_get_int_status = if_usb_get_int_status;
214 priv->hw_read_event_cause = if_usb_read_event_cause;
Luis Carlos Cobo63f00232007-08-02 13:19:24 -0400215 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400216
Dan Williams954ee162007-08-20 11:43:25 -0400217 /* Delay 200 ms to waiting for the FW ready */
218 if_usb_submit_rx_urb(cardp);
219 msleep_interruptible(200);
220 priv->adapter->fw_ready = 1;
221
Holger Schurig10078322007-11-15 18:05:47 -0500222 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400223 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400224
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200225 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400226 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200227
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200228 return 0;
229
Dan Williams954ee162007-08-20 11:43:25 -0400230err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500231 lbs_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400232err_add_mesh:
Holger Schurig10078322007-11-15 18:05:47 -0500233 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400234err_prog_firmware:
235 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400237 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200238
239error:
240 return -ENOMEM;
241}
242
243/**
244 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400245 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200246 * @return N/A
247 */
248static void if_usb_disconnect(struct usb_interface *intf)
249{
250 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100251 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200252
Dan Williams954ee162007-08-20 11:43:25 -0400253 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200254
Dan Williams954ee162007-08-20 11:43:25 -0400255 /* Update Surprise removed to TRUE */
256 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200257
Dan Williams954ee162007-08-20 11:43:25 -0400258 if (priv) {
Holger Schurig69f90322007-11-23 15:43:44 +0100259 struct lbs_adapter *adapter = priv->adapter;
Dan Williams954ee162007-08-20 11:43:25 -0400260
261 adapter->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500262 lbs_stop_card(priv);
263 lbs_remove_mesh(priv);
264 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400265 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200266
Andres Salomonbe13f182007-11-20 17:43:55 -0500267 /* this is (apparently?) necessary for future usage of the device */
268 lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
269 0, 0, NULL);
270
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271 /* Unlink and free urb */
272 if_usb_free(cardp);
273
274 usb_set_intfdata(intf, NULL);
275 usb_put_dev(interface_to_usbdev(intf));
276
Dan Williams954ee162007-08-20 11:43:25 -0400277 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200278}
279
280/**
281 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100282 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200283 * @return 0
284 */
Dan Williams954ee162007-08-20 11:43:25 -0400285static int if_prog_firmware(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200286{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200287 struct FWData *fwdata;
288 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400289 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200290
291 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
292
293 if (!fwdata)
294 return -1;
295
296 fwheader = &fwdata->fwheader;
297
298 if (!cardp->CRC_OK) {
299 cardp->totalbytes = cardp->fwlastblksent;
300 cardp->fwseqnum = cardp->lastseqnum - 1;
301 }
302
Holger Schurig9012b282007-05-25 11:27:16 -0400303 /*
304 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200305 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400306 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200307
308 memcpy(fwheader, &firmware[cardp->totalbytes],
309 sizeof(struct fwheader));
310
311 cardp->fwlastblksent = cardp->totalbytes;
312 cardp->totalbytes += sizeof(struct fwheader);
313
Holger Schurig9012b282007-05-25 11:27:16 -0400314 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200315 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400316 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200317
Holger Schurig9012b282007-05-25 11:27:16 -0400318 /*
319 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400320 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400321 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200322
323 cardp->fwseqnum = cardp->fwseqnum + 1;
324
David Woodhouse981f1872007-05-25 23:36:54 -0400325 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
326 cardp->lastseqnum = cardp->fwseqnum;
327 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200328
David Woodhouse981f1872007-05-25 23:36:54 -0400329 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400330 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400331 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400332 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200333 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
334 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400335 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200336 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400337 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338
David Woodhousebb793e22007-05-25 23:38:14 -0400339 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400340 /*
341 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200342 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400343 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200344 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400345 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200346 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400347 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200348 cardp->fwfinalblk = 1;
349 }
350
Holger Schurig9012b282007-05-25 11:27:16 -0400351 /*
352 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200353 "The firmware download is done size is %d\n",
354 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400355 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200356
357 kfree(fwdata);
358
359 return 0;
360}
361
Dan Williams954ee162007-08-20 11:43:25 -0400362static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200363{
364 int ret;
Holger Schurig69f90322007-11-23 15:43:44 +0100365 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200366
Holger Schurig3874d0f2007-05-25 12:01:42 -0400367 lbs_deb_enter(LBS_DEB_USB);
368
Dan Williamseedc2a32007-08-02 11:36:22 -0400369 /* Try a USB port reset first, if that fails send the reset
370 * command to the firmware.
371 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200372 ret = usb_reset_device(cardp->udev);
Dan Williams954ee162007-08-20 11:43:25 -0400373 if (!ret && priv) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200374 msleep(10);
Holger Schurig10078322007-11-15 18:05:47 -0500375 ret = lbs_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200376 msleep(10);
377 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400378
379 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
380
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200381 return ret;
382}
383
384/**
385 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100386 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200387 * @param payload pointer to payload data
388 * @param nb data length
389 * @return 0 or -1
390 */
Dan Williams954ee162007-08-20 11:43:25 -0400391static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200392{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200393 int ret = -1;
394
395 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400396 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400397 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200398 goto tx_ret;
399 }
400
401 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
402 usb_sndbulkpipe(cardp->udev,
403 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400404 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200405
406 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
407
408 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
409 /* transfer failed */
Holger Schurig9012b282007-05-25 11:27:16 -0400410 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200411 ret = -1;
412 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400413 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200414 ret = 0;
415 }
416
417tx_ret:
418 return ret;
419}
420
Dan Williams954ee162007-08-20 11:43:25 -0400421static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
422 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200423{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200424 struct sk_buff *skb;
425 struct read_cb_info *rinfo = &cardp->rinfo;
426 int ret = -1;
427
428 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
429 lbs_pr_err("No free skb\n");
430 goto rx_ret;
431 }
432
433 rinfo->skb = skb;
434
435 /* Fill the receive configuration URB and initialise the Rx call back */
436 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
437 usb_rcvbulkpipe(cardp->udev,
438 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400439 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200440 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
441 rinfo);
442
443 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
444
Holger Schurig9012b282007-05-25 11:27:16 -0400445 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
447 /* handle failure conditions */
Holger Schurig9012b282007-05-25 11:27:16 -0400448 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
David Woodhouse77d8cf22007-11-28 16:20:51 +0000449 kfree_skb(skb);
450 rinfo->skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451 ret = -1;
452 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400453 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200454 ret = 0;
455 }
456
457rx_ret:
458 return ret;
459}
460
Dan Williams954ee162007-08-20 11:43:25 -0400461static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200462{
Dan Williams954ee162007-08-20 11:43:25 -0400463 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200464}
465
Dan Williams954ee162007-08-20 11:43:25 -0400466static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200467{
Dan Williams954ee162007-08-20 11:43:25 -0400468 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469}
470
471static void if_usb_receive_fwload(struct urb *urb)
472{
473 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200474 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400475 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476 struct fwsyncheader *syncfwheader;
477 struct bootcmdrespStr bootcmdresp;
478
479 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400480 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200481 "URB status is failed during fw load\n");
482 kfree_skb(skb);
483 return;
484 }
485
486 if (cardp->bootcmdresp == 0) {
487 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
488 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400489 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200490 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400491 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200492 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400493 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200494 "Received valid boot command response\n");
495 return;
496 }
David Woodhouse981f1872007-05-25 23:36:54 -0400497 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200498 lbs_pr_info(
499 "boot cmd response wrong magic number (0x%x)\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400500 le32_to_cpu(bootcmdresp.u32magicnumber));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200501 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
502 lbs_pr_info(
503 "boot cmd response cmd_tag error (%d)\n",
504 bootcmdresp.u8cmd_tag);
505 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
506 lbs_pr_info(
507 "boot cmd response result error (%d)\n",
508 bootcmdresp.u8result);
509 } else {
510 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400511 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200512 "Received valid boot command response\n");
513 }
514 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400515 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200516 return;
517 }
518
519 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
520 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400521 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200522 kfree_skb(skb);
523 return;
524 }
525
526 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
527 sizeof(struct fwsyncheader));
528
529 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400530 /*
531 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200532 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400533 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200534 "FW received Blk seqnum = %d\n",
535 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400536 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200537 cardp->CRC_OK = 1;
538 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400539 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200540 "FW received Blk with CRC error\n");
541 cardp->CRC_OK = 0;
542 }
543
544 kfree_skb(skb);
545
546 if (cardp->fwfinalblk) {
547 cardp->fwdnldover = 1;
548 goto exit;
549 }
550
Dan Williams954ee162007-08-20 11:43:25 -0400551 if_prog_firmware(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200552
Dan Williams954ee162007-08-20 11:43:25 -0400553 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200554exit:
555 kfree(syncfwheader);
556
557 return;
558
559}
560
561#define MRVDRV_MIN_PKT_LEN 30
562
563static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
564 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100565 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200566{
567 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
568 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400569 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200570 "Packet length is Invalid\n");
571 kfree_skb(skb);
572 return;
573 }
574
575 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
576 skb_put(skb, recvlength);
577 skb_pull(skb, MESSAGE_HEADER_LEN);
Holger Schurig10078322007-11-15 18:05:47 -0500578 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400579 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200580}
581
582static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
583 struct sk_buff *skb,
584 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100585 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200586{
587 u8 *cmdbuf;
588 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400589 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200590 "The receive buffer is too large\n");
591 kfree_skb(skb);
592 return;
593 }
594
595 if (!in_interrupt())
596 BUG();
597
598 spin_lock(&priv->adapter->driver_lock);
599 /* take care of cur_cmd = NULL case by reading the
600 * data to clear the interrupt */
601 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400602 cmdbuf = priv->upld_buf;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400603 priv->adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200604 } else
605 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
606
Holger Schurigc95c7f92007-08-02 11:49:45 -0400607 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400608 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400610 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200611
612 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500613 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200614 spin_unlock(&priv->adapter->driver_lock);
615
Holger Schurig9012b282007-05-25 11:27:16 -0400616 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200617 "Wake up main thread to handle cmd response\n");
618
619 return;
620}
621
622/**
623 * @brief This function reads of the packet into the upload buff,
624 * wake up the main thread and initialise the Rx callack.
625 *
626 * @param urb pointer to struct urb
627 * @return N/A
628 */
629static void if_usb_receive(struct urb *urb)
630{
631 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200632 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400633 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
Holger Schurig69f90322007-11-23 15:43:44 +0100634 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200635
636 int recvlength = urb->actual_length;
637 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400638 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200639
Holger Schurig9012b282007-05-25 11:27:16 -0400640 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200641
642 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400643 __le32 tmp;
644
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200645 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400646 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200647 "URB status is failed\n");
648 kfree_skb(skb);
649 goto setup_for_next;
650 }
651
652 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400653 memcpy(&tmp, recvbuff, sizeof(u32));
654 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400655 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400656 "Recv length = 0x%x, Recv type = 0x%X\n",
657 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000658 } else if (urb->status) {
659 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200660 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000661 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200662
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663 switch (recvtype) {
664 case CMD_TYPE_DATA:
665 process_cmdtypedata(recvlength, skb, cardp, priv);
666 break;
667
668 case CMD_TYPE_REQUEST:
669 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
670 break;
671
672 case CMD_TYPE_INDICATION:
673 /* Event cause handling */
674 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400675 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400676 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200677 cardp->usb_event_cause);
678 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500679 lbs_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400680 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200681 break;
682 }
David Woodhouse981f1872007-05-25 23:36:54 -0400683 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400684 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200685 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500686 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200687 spin_unlock(&priv->adapter->driver_lock);
688 goto rx_exit;
689 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400690 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
691 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200692 kfree_skb(skb);
693 break;
694 }
695
696setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400697 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200698rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400699 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200700}
701
702/**
703 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100704 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200705 * @param type type of data
706 * @param buf pointer to data buffer
707 * @param len number of bytes
708 * @return 0 or -1
709 */
Holger Schurig69f90322007-11-23 15:43:44 +0100710static int if_usb_host_to_card(struct lbs_private *priv,
711 u8 type,
712 u8 *payload,
713 u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200714{
Holger Schurig634b8f42007-05-25 13:05:16 -0400715 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716
Holger Schurig9012b282007-05-25 11:27:16 -0400717 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
718 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719
720 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400721 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400722 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200723 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
724 MESSAGE_HEADER_LEN);
725
726 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400727 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400728 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
730 MESSAGE_HEADER_LEN);
731 }
732
733 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
734
Dan Williams954ee162007-08-20 11:43:25 -0400735 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400736 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200737}
738
739/* called with adapter->driver_lock held */
Holger Schurig69f90322007-11-23 15:43:44 +0100740static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200741{
Holger Schurig634b8f42007-05-25 13:05:16 -0400742 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200743
744 *ireg = cardp->usb_int_cause;
745 cardp->usb_int_cause = 0;
746
Holger Schurig9012b282007-05-25 11:27:16 -0400747 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200748
749 return 0;
750}
751
Holger Schurig69f90322007-11-23 15:43:44 +0100752static int if_usb_read_event_cause(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753{
Holger Schurig634b8f42007-05-25 13:05:16 -0400754 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400755
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756 priv->adapter->eventcause = cardp->usb_event_cause;
757 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400758 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759 return 0;
760}
761
Dan Williams9e22cb62007-08-02 11:14:49 -0400762/**
763 * @brief This function issues Boot command to the Boot2 code
764 * @param ivalue 1:Boot from FW by USB-Download
765 * 2:Boot from FW in EEPROM
766 * @return 0
767 */
Dan Williams954ee162007-08-20 11:43:25 -0400768static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400769{
Dan Williams954ee162007-08-20 11:43:25 -0400770 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400771 int i;
772
773 /* Prepare command */
774 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
775 sbootcmd.u8cmd_tag = ivalue;
776 for (i=0; i<11; i++)
777 sbootcmd.au8dumy[i]=0x00;
778 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
779
780 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400781 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400782
783 return 0;
784}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200785
786
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400787/**
788 * @brief This function checks the validity of Boot2/FW image.
789 *
790 * @param data pointer to image
791 * len image length
792 * @return 0 or -1
793 */
794static int check_fwfile_format(u8 *data, u32 totlen)
795{
796 u32 bincmd, exit;
797 u32 blksize, offset, len;
798 int ret;
799
800 ret = 1;
801 exit = len = 0;
802
803 do {
804 struct fwheader *fwh = (void *)data;
805
806 bincmd = le32_to_cpu(fwh->dnldcmd);
807 blksize = le32_to_cpu(fwh->datalength);
808 switch (bincmd) {
809 case FW_HAS_DATA_TO_RECV:
810 offset = sizeof(struct fwheader) + blksize;
811 data += offset;
812 len += offset;
813 if (len >= totlen)
814 exit = 1;
815 break;
816 case FW_HAS_LAST_BLOCK:
817 exit = 1;
818 ret = 0;
819 break;
820 default:
821 exit = 1;
822 break;
823 }
824 } while (!exit);
825
826 if (ret)
827 lbs_pr_err("firmware file format check FAIL\n");
828 else
829 lbs_deb_fw("firmware file format check PASS\n");
830
831 return ret;
832}
833
834
Dan Williams954ee162007-08-20 11:43:25 -0400835static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400836{
Dan Williams954ee162007-08-20 11:43:25 -0400837 int i = 0;
838 static int reset_count = 10;
839 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400840
Dan Williams954ee162007-08-20 11:43:25 -0400841 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400842
Holger Schurig10078322007-11-15 18:05:47 -0500843 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400844 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400845 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500846 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400847 goto done;
848 }
849
Dan Williams954ee162007-08-20 11:43:25 -0400850 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
851 goto release_fw;
852
853restart:
854 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
855 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
856 ret = -1;
857 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400858 }
859
Dan Williams954ee162007-08-20 11:43:25 -0400860 cardp->bootcmdresp = 0;
861 do {
862 int j = 0;
863 i++;
864 /* Issue Boot command = 1, Boot from Download-FW */
865 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
866 /* wait for command response */
867 do {
868 j++;
869 msleep_interruptible(100);
870 } while (cardp->bootcmdresp == 0 && j < 10);
871 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400872
Dan Williams954ee162007-08-20 11:43:25 -0400873 if (cardp->bootcmdresp == 0) {
874 if (--reset_count >= 0) {
875 if_usb_reset_device(cardp);
876 goto restart;
877 }
878 return -1;
879 }
880
881 i = 0;
882
883 cardp->totalbytes = 0;
884 cardp->fwlastblksent = 0;
885 cardp->CRC_OK = 1;
886 cardp->fwdnldover = 0;
887 cardp->fwseqnum = -1;
888 cardp->totalbytes = 0;
889 cardp->fwfinalblk = 0;
890
891 if_prog_firmware(cardp);
892
893 do {
894 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
895 i++;
896 msleep_interruptible(100);
897 if (cardp->surprise_removed || i >= 20)
898 break;
899 } while (!cardp->fwdnldover);
900
901 if (!cardp->fwdnldover) {
902 lbs_pr_info("failed to load fw, resetting device!\n");
903 if (--reset_count >= 0) {
904 if_usb_reset_device(cardp);
905 goto restart;
906 }
907
908 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
909 ret = -1;
910 goto release_fw;
911 }
912
913release_fw:
914 release_firmware(cardp->fw);
915 cardp->fw = NULL;
916
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400917done:
Dan Williams954ee162007-08-20 11:43:25 -0400918 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400919 return ret;
920}
921
922
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200923#ifdef CONFIG_PM
924static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
925{
926 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100927 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200928
Holger Schurig9012b282007-05-25 11:27:16 -0400929 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200930
931 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
932 return -1;
933
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400934 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
935 /* Mesh autostart must be activated while sleeping
936 * On resume it will go back to the current state
937 */
938 struct cmd_ds_mesh_access mesh_access;
939 memset(&mesh_access, 0, sizeof(mesh_access));
940 mesh_access.data[0] = cpu_to_le32(1);
Holger Schurig10078322007-11-15 18:05:47 -0500941 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400942 CMD_MESH_ACCESS,
943 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
944 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
945 }
946
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200947 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400948 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200949
950 /* Unlink tx & rx urb */
951 usb_kill_urb(cardp->tx_urb);
952 usb_kill_urb(cardp->rx_urb);
953
954 cardp->rx_urb_recall = 1;
955
Holger Schurig9012b282007-05-25 11:27:16 -0400956 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200957 return 0;
958}
959
960static int if_usb_resume(struct usb_interface *intf)
961{
962 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100963 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200964
Holger Schurig9012b282007-05-25 11:27:16 -0400965 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200966
967 cardp->rx_urb_recall = 0;
968
969 if_usb_submit_rx_urb(cardp->priv);
970
971 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400972 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200973
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400974 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
975 /* Mesh autostart was activated while sleeping
976 * Disable it if appropriate
977 */
978 struct cmd_ds_mesh_access mesh_access;
979 memset(&mesh_access, 0, sizeof(mesh_access));
980 mesh_access.data[0] = cpu_to_le32(0);
Holger Schurig10078322007-11-15 18:05:47 -0500981 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400982 CMD_MESH_ACCESS,
983 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
984 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
985 }
986
Holger Schurig9012b282007-05-25 11:27:16 -0400987 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200988 return 0;
989}
990#else
991#define if_usb_suspend NULL
992#define if_usb_resume NULL
993#endif
994
995static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -0500996 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200997 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200998 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200999 .id_table = if_usb_table,
1000 .suspend = if_usb_suspend,
1001 .resume = if_usb_resume,
1002};
1003
Andres Salomon4fb910f2007-11-20 17:43:45 -05001004static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001005{
Holger Schurig084708b2007-05-25 12:37:58 -04001006 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001007
Holger Schurig084708b2007-05-25 12:37:58 -04001008 lbs_deb_enter(LBS_DEB_MAIN);
1009
Holger Schurig084708b2007-05-25 12:37:58 -04001010 ret = usb_register(&if_usb_driver);
1011
1012 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1013 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001014}
1015
Andres Salomon4fb910f2007-11-20 17:43:45 -05001016static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001017{
Holger Schurig084708b2007-05-25 12:37:58 -04001018 lbs_deb_enter(LBS_DEB_MAIN);
1019
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001021
1022 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001023}
Holger Schurig084708b2007-05-25 12:37:58 -04001024
1025module_init(if_usb_init_module);
1026module_exit(if_usb_exit_module);
1027
1028MODULE_DESCRIPTION("8388 USB WLAN Driver");
1029MODULE_AUTHOR("Marvell International Ltd.");
1030MODULE_LICENSE("GPL");