blob: f8589283d7bcc6c5e688a038c2d129d438e4466e [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
David Woodhouse2c944042007-12-06 14:41:08 +0000225 /* Set the boot2 version in firmware, ignoring errors. */
226 (void)lbs_prepare_and_send_command(priv, CMD_SET_BOOT2_VER,
227 0, CMD_OPTION_WAITFORRSP, 0, NULL);
228
229
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200230 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400231 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200232
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200233 return 0;
234
Dan Williams954ee162007-08-20 11:43:25 -0400235err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500236 lbs_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400237err_add_mesh:
Holger Schurig10078322007-11-15 18:05:47 -0500238 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400239err_prog_firmware:
240 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200241dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400242 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200243
244error:
245 return -ENOMEM;
246}
247
248/**
249 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400250 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200251 * @return N/A
252 */
253static void if_usb_disconnect(struct usb_interface *intf)
254{
255 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100256 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200257
Dan Williams954ee162007-08-20 11:43:25 -0400258 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200259
Dan Williams954ee162007-08-20 11:43:25 -0400260 /* Update Surprise removed to TRUE */
261 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200262
Dan Williams954ee162007-08-20 11:43:25 -0400263 if (priv) {
Holger Schurig69f90322007-11-23 15:43:44 +0100264 struct lbs_adapter *adapter = priv->adapter;
Dan Williams954ee162007-08-20 11:43:25 -0400265
266 adapter->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500267 lbs_stop_card(priv);
268 lbs_remove_mesh(priv);
269 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400270 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271
Andres Salomonbe13f182007-11-20 17:43:55 -0500272 /* this is (apparently?) necessary for future usage of the device */
273 lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
274 0, 0, NULL);
275
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200276 /* Unlink and free urb */
277 if_usb_free(cardp);
278
279 usb_set_intfdata(intf, NULL);
280 usb_put_dev(interface_to_usbdev(intf));
281
Dan Williams954ee162007-08-20 11:43:25 -0400282 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200283}
284
285/**
286 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100287 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200288 * @return 0
289 */
Dan Williams954ee162007-08-20 11:43:25 -0400290static int if_prog_firmware(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200291{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200292 struct FWData *fwdata;
293 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400294 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200295
296 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
297
298 if (!fwdata)
299 return -1;
300
301 fwheader = &fwdata->fwheader;
302
303 if (!cardp->CRC_OK) {
304 cardp->totalbytes = cardp->fwlastblksent;
305 cardp->fwseqnum = cardp->lastseqnum - 1;
306 }
307
Holger Schurig9012b282007-05-25 11:27:16 -0400308 /*
309 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200310 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400311 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200312
313 memcpy(fwheader, &firmware[cardp->totalbytes],
314 sizeof(struct fwheader));
315
316 cardp->fwlastblksent = cardp->totalbytes;
317 cardp->totalbytes += sizeof(struct fwheader);
318
Holger Schurig9012b282007-05-25 11:27:16 -0400319 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200320 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400321 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200322
Holger Schurig9012b282007-05-25 11:27:16 -0400323 /*
324 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400325 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400326 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200327
328 cardp->fwseqnum = cardp->fwseqnum + 1;
329
David Woodhouse981f1872007-05-25 23:36:54 -0400330 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
331 cardp->lastseqnum = cardp->fwseqnum;
332 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200333
David Woodhouse981f1872007-05-25 23:36:54 -0400334 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400335 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400336 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400337 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
339 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400340 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200341 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400342 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200343
David Woodhousebb793e22007-05-25 23:38:14 -0400344 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400345 /*
346 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200347 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400348 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200349 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400350 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200351 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400352 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200353 cardp->fwfinalblk = 1;
354 }
355
Holger Schurig9012b282007-05-25 11:27:16 -0400356 /*
357 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200358 "The firmware download is done size is %d\n",
359 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400360 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200361
362 kfree(fwdata);
363
364 return 0;
365}
366
Dan Williams954ee162007-08-20 11:43:25 -0400367static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200368{
369 int ret;
Holger Schurig69f90322007-11-23 15:43:44 +0100370 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200371
Holger Schurig3874d0f2007-05-25 12:01:42 -0400372 lbs_deb_enter(LBS_DEB_USB);
373
Dan Williamseedc2a32007-08-02 11:36:22 -0400374 /* Try a USB port reset first, if that fails send the reset
375 * command to the firmware.
376 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200377 ret = usb_reset_device(cardp->udev);
Dan Williams954ee162007-08-20 11:43:25 -0400378 if (!ret && priv) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200379 msleep(10);
Holger Schurig10078322007-11-15 18:05:47 -0500380 ret = lbs_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200381 msleep(10);
382 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400383
384 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
385
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200386 return ret;
387}
388
389/**
390 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100391 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200392 * @param payload pointer to payload data
393 * @param nb data length
394 * @return 0 or -1
395 */
Dan Williams954ee162007-08-20 11:43:25 -0400396static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200397{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200398 int ret = -1;
399
400 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400401 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400402 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200403 goto tx_ret;
404 }
405
406 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
407 usb_sndbulkpipe(cardp->udev,
408 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400409 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200410
411 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
412
413 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
414 /* transfer failed */
Holger Schurig9012b282007-05-25 11:27:16 -0400415 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200416 ret = -1;
417 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400418 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200419 ret = 0;
420 }
421
422tx_ret:
423 return ret;
424}
425
Dan Williams954ee162007-08-20 11:43:25 -0400426static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
427 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200428{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200429 struct sk_buff *skb;
430 struct read_cb_info *rinfo = &cardp->rinfo;
431 int ret = -1;
432
433 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
434 lbs_pr_err("No free skb\n");
435 goto rx_ret;
436 }
437
438 rinfo->skb = skb;
439
440 /* Fill the receive configuration URB and initialise the Rx call back */
441 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
442 usb_rcvbulkpipe(cardp->udev,
443 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400444 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200445 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
446 rinfo);
447
448 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
449
Holger Schurig9012b282007-05-25 11:27:16 -0400450 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
452 /* handle failure conditions */
Holger Schurig9012b282007-05-25 11:27:16 -0400453 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
David Woodhouse77d8cf22007-11-28 16:20:51 +0000454 kfree_skb(skb);
455 rinfo->skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456 ret = -1;
457 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400458 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200459 ret = 0;
460 }
461
462rx_ret:
463 return ret;
464}
465
Dan Williams954ee162007-08-20 11:43:25 -0400466static int if_usb_submit_rx_urb_fwload(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_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469}
470
Dan Williams954ee162007-08-20 11:43:25 -0400471static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200472{
Dan Williams954ee162007-08-20 11:43:25 -0400473 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200474}
475
476static void if_usb_receive_fwload(struct urb *urb)
477{
478 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200479 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400480 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200481 struct fwsyncheader *syncfwheader;
482 struct bootcmdrespStr bootcmdresp;
483
484 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400485 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486 "URB status is failed during fw load\n");
487 kfree_skb(skb);
488 return;
489 }
490
491 if (cardp->bootcmdresp == 0) {
492 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
493 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400494 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200495 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400496 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200497 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400498 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200499 "Received valid boot command response\n");
500 return;
501 }
David Woodhouse981f1872007-05-25 23:36:54 -0400502 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200503 lbs_pr_info(
504 "boot cmd response wrong magic number (0x%x)\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400505 le32_to_cpu(bootcmdresp.u32magicnumber));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200506 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
507 lbs_pr_info(
508 "boot cmd response cmd_tag error (%d)\n",
509 bootcmdresp.u8cmd_tag);
510 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
511 lbs_pr_info(
512 "boot cmd response result error (%d)\n",
513 bootcmdresp.u8result);
514 } else {
515 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400516 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200517 "Received valid boot command response\n");
518 }
519 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400520 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200521 return;
522 }
523
524 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
525 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400526 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200527 kfree_skb(skb);
528 return;
529 }
530
531 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
532 sizeof(struct fwsyncheader));
533
534 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400535 /*
536 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200537 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400538 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200539 "FW received Blk seqnum = %d\n",
540 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400541 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200542 cardp->CRC_OK = 1;
543 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400544 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200545 "FW received Blk with CRC error\n");
546 cardp->CRC_OK = 0;
547 }
548
549 kfree_skb(skb);
550
551 if (cardp->fwfinalblk) {
552 cardp->fwdnldover = 1;
553 goto exit;
554 }
555
Dan Williams954ee162007-08-20 11:43:25 -0400556 if_prog_firmware(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200557
Dan Williams954ee162007-08-20 11:43:25 -0400558 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559exit:
560 kfree(syncfwheader);
561
562 return;
563
564}
565
566#define MRVDRV_MIN_PKT_LEN 30
567
568static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
569 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100570 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200571{
572 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
573 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400574 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200575 "Packet length is Invalid\n");
576 kfree_skb(skb);
577 return;
578 }
579
580 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
581 skb_put(skb, recvlength);
582 skb_pull(skb, MESSAGE_HEADER_LEN);
Holger Schurig10078322007-11-15 18:05:47 -0500583 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400584 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200585}
586
587static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
588 struct sk_buff *skb,
589 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100590 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200591{
592 u8 *cmdbuf;
593 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400594 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200595 "The receive buffer is too large\n");
596 kfree_skb(skb);
597 return;
598 }
599
600 if (!in_interrupt())
601 BUG();
602
603 spin_lock(&priv->adapter->driver_lock);
604 /* take care of cur_cmd = NULL case by reading the
605 * data to clear the interrupt */
606 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400607 cmdbuf = priv->upld_buf;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400608 priv->adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609 } else
610 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
611
Holger Schurigc95c7f92007-08-02 11:49:45 -0400612 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400613 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200614 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400615 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200616
617 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500618 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200619 spin_unlock(&priv->adapter->driver_lock);
620
Holger Schurig9012b282007-05-25 11:27:16 -0400621 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200622 "Wake up main thread to handle cmd response\n");
623
624 return;
625}
626
627/**
628 * @brief This function reads of the packet into the upload buff,
629 * wake up the main thread and initialise the Rx callack.
630 *
631 * @param urb pointer to struct urb
632 * @return N/A
633 */
634static void if_usb_receive(struct urb *urb)
635{
636 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200637 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400638 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
Holger Schurig69f90322007-11-23 15:43:44 +0100639 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200640
641 int recvlength = urb->actual_length;
642 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400643 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200644
Holger Schurig9012b282007-05-25 11:27:16 -0400645 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200646
647 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400648 __le32 tmp;
649
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200650 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400651 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200652 "URB status is failed\n");
653 kfree_skb(skb);
654 goto setup_for_next;
655 }
656
657 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400658 memcpy(&tmp, recvbuff, sizeof(u32));
659 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400660 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400661 "Recv length = 0x%x, Recv type = 0x%X\n",
662 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000663 } else if (urb->status) {
664 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200665 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000666 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200667
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200668 switch (recvtype) {
669 case CMD_TYPE_DATA:
670 process_cmdtypedata(recvlength, skb, cardp, priv);
671 break;
672
673 case CMD_TYPE_REQUEST:
674 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
675 break;
676
677 case CMD_TYPE_INDICATION:
678 /* Event cause handling */
679 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400680 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400681 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200682 cardp->usb_event_cause);
683 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500684 lbs_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400685 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200686 break;
687 }
David Woodhouse981f1872007-05-25 23:36:54 -0400688 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400689 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200690 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500691 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200692 spin_unlock(&priv->adapter->driver_lock);
693 goto rx_exit;
694 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400695 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
696 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697 kfree_skb(skb);
698 break;
699 }
700
701setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400702 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400704 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200705}
706
707/**
708 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100709 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710 * @param type type of data
711 * @param buf pointer to data buffer
712 * @param len number of bytes
713 * @return 0 or -1
714 */
Holger Schurig69f90322007-11-23 15:43:44 +0100715static int if_usb_host_to_card(struct lbs_private *priv,
716 u8 type,
717 u8 *payload,
718 u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719{
Holger Schurig634b8f42007-05-25 13:05:16 -0400720 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200721
Holger Schurig9012b282007-05-25 11:27:16 -0400722 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
723 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200724
725 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400726 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400727 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200728 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
729 MESSAGE_HEADER_LEN);
730
731 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400732 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400733 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200734 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
735 MESSAGE_HEADER_LEN);
736 }
737
738 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
739
Dan Williams954ee162007-08-20 11:43:25 -0400740 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400741 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200742}
743
744/* called with adapter->driver_lock held */
Holger Schurig69f90322007-11-23 15:43:44 +0100745static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746{
Holger Schurig634b8f42007-05-25 13:05:16 -0400747 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200748
749 *ireg = cardp->usb_int_cause;
750 cardp->usb_int_cause = 0;
751
Holger Schurig9012b282007-05-25 11:27:16 -0400752 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753
754 return 0;
755}
756
Holger Schurig69f90322007-11-23 15:43:44 +0100757static int if_usb_read_event_cause(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200758{
Holger Schurig634b8f42007-05-25 13:05:16 -0400759 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400760
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200761 priv->adapter->eventcause = cardp->usb_event_cause;
762 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400763 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200764 return 0;
765}
766
Dan Williams9e22cb62007-08-02 11:14:49 -0400767/**
768 * @brief This function issues Boot command to the Boot2 code
769 * @param ivalue 1:Boot from FW by USB-Download
770 * 2:Boot from FW in EEPROM
771 * @return 0
772 */
Dan Williams954ee162007-08-20 11:43:25 -0400773static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400774{
Dan Williams954ee162007-08-20 11:43:25 -0400775 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400776 int i;
777
778 /* Prepare command */
779 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
780 sbootcmd.u8cmd_tag = ivalue;
781 for (i=0; i<11; i++)
782 sbootcmd.au8dumy[i]=0x00;
783 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
784
785 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400786 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400787
788 return 0;
789}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200790
791
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400792/**
793 * @brief This function checks the validity of Boot2/FW image.
794 *
795 * @param data pointer to image
796 * len image length
797 * @return 0 or -1
798 */
799static int check_fwfile_format(u8 *data, u32 totlen)
800{
801 u32 bincmd, exit;
802 u32 blksize, offset, len;
803 int ret;
804
805 ret = 1;
806 exit = len = 0;
807
808 do {
809 struct fwheader *fwh = (void *)data;
810
811 bincmd = le32_to_cpu(fwh->dnldcmd);
812 blksize = le32_to_cpu(fwh->datalength);
813 switch (bincmd) {
814 case FW_HAS_DATA_TO_RECV:
815 offset = sizeof(struct fwheader) + blksize;
816 data += offset;
817 len += offset;
818 if (len >= totlen)
819 exit = 1;
820 break;
821 case FW_HAS_LAST_BLOCK:
822 exit = 1;
823 ret = 0;
824 break;
825 default:
826 exit = 1;
827 break;
828 }
829 } while (!exit);
830
831 if (ret)
832 lbs_pr_err("firmware file format check FAIL\n");
833 else
834 lbs_deb_fw("firmware file format check PASS\n");
835
836 return ret;
837}
838
839
Dan Williams954ee162007-08-20 11:43:25 -0400840static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400841{
Dan Williams954ee162007-08-20 11:43:25 -0400842 int i = 0;
843 static int reset_count = 10;
844 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400845
Dan Williams954ee162007-08-20 11:43:25 -0400846 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400847
Holger Schurig10078322007-11-15 18:05:47 -0500848 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400849 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400850 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500851 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400852 goto done;
853 }
854
Dan Williams954ee162007-08-20 11:43:25 -0400855 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
856 goto release_fw;
857
858restart:
859 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
860 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
861 ret = -1;
862 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400863 }
864
Dan Williams954ee162007-08-20 11:43:25 -0400865 cardp->bootcmdresp = 0;
866 do {
867 int j = 0;
868 i++;
869 /* Issue Boot command = 1, Boot from Download-FW */
870 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
871 /* wait for command response */
872 do {
873 j++;
874 msleep_interruptible(100);
875 } while (cardp->bootcmdresp == 0 && j < 10);
876 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400877
Dan Williams954ee162007-08-20 11:43:25 -0400878 if (cardp->bootcmdresp == 0) {
879 if (--reset_count >= 0) {
880 if_usb_reset_device(cardp);
881 goto restart;
882 }
883 return -1;
884 }
885
886 i = 0;
887
888 cardp->totalbytes = 0;
889 cardp->fwlastblksent = 0;
890 cardp->CRC_OK = 1;
891 cardp->fwdnldover = 0;
892 cardp->fwseqnum = -1;
893 cardp->totalbytes = 0;
894 cardp->fwfinalblk = 0;
895
896 if_prog_firmware(cardp);
897
898 do {
899 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
900 i++;
901 msleep_interruptible(100);
902 if (cardp->surprise_removed || i >= 20)
903 break;
904 } while (!cardp->fwdnldover);
905
906 if (!cardp->fwdnldover) {
907 lbs_pr_info("failed to load fw, resetting device!\n");
908 if (--reset_count >= 0) {
909 if_usb_reset_device(cardp);
910 goto restart;
911 }
912
913 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
914 ret = -1;
915 goto release_fw;
916 }
917
918release_fw:
919 release_firmware(cardp->fw);
920 cardp->fw = NULL;
921
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400922done:
Dan Williams954ee162007-08-20 11:43:25 -0400923 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400924 return ret;
925}
926
927
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200928#ifdef CONFIG_PM
929static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
930{
931 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100932 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200933
Holger Schurig9012b282007-05-25 11:27:16 -0400934 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200935
936 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
937 return -1;
938
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400939 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
940 /* Mesh autostart must be activated while sleeping
941 * On resume it will go back to the current state
942 */
943 struct cmd_ds_mesh_access mesh_access;
944 memset(&mesh_access, 0, sizeof(mesh_access));
945 mesh_access.data[0] = cpu_to_le32(1);
Holger Schurig10078322007-11-15 18:05:47 -0500946 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400947 CMD_MESH_ACCESS,
948 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
949 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
950 }
951
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200952 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400953 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200954
955 /* Unlink tx & rx urb */
956 usb_kill_urb(cardp->tx_urb);
957 usb_kill_urb(cardp->rx_urb);
958
959 cardp->rx_urb_recall = 1;
960
Holger Schurig9012b282007-05-25 11:27:16 -0400961 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200962 return 0;
963}
964
965static int if_usb_resume(struct usb_interface *intf)
966{
967 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100968 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200969
Holger Schurig9012b282007-05-25 11:27:16 -0400970 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200971
972 cardp->rx_urb_recall = 0;
973
974 if_usb_submit_rx_urb(cardp->priv);
975
976 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400977 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200978
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400979 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
980 /* Mesh autostart was activated while sleeping
981 * Disable it if appropriate
982 */
983 struct cmd_ds_mesh_access mesh_access;
984 memset(&mesh_access, 0, sizeof(mesh_access));
985 mesh_access.data[0] = cpu_to_le32(0);
Holger Schurig10078322007-11-15 18:05:47 -0500986 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400987 CMD_MESH_ACCESS,
988 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
989 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
990 }
991
Holger Schurig9012b282007-05-25 11:27:16 -0400992 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200993 return 0;
994}
995#else
996#define if_usb_suspend NULL
997#define if_usb_resume NULL
998#endif
999
1000static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -05001001 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001002 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001003 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001004 .id_table = if_usb_table,
1005 .suspend = if_usb_suspend,
1006 .resume = if_usb_resume,
1007};
1008
Andres Salomon4fb910f2007-11-20 17:43:45 -05001009static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001010{
Holger Schurig084708b2007-05-25 12:37:58 -04001011 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001012
Holger Schurig084708b2007-05-25 12:37:58 -04001013 lbs_deb_enter(LBS_DEB_MAIN);
1014
Holger Schurig084708b2007-05-25 12:37:58 -04001015 ret = usb_register(&if_usb_driver);
1016
1017 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1018 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001019}
1020
Andres Salomon4fb910f2007-11-20 17:43:45 -05001021static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001022{
Holger Schurig084708b2007-05-25 12:37:58 -04001023 lbs_deb_enter(LBS_DEB_MAIN);
1024
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001025 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001026
1027 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001028}
Holger Schurig084708b2007-05-25 12:37:58 -04001029
1030module_init(if_usb_init_module);
1031module_exit(if_usb_exit_module);
1032
1033MODULE_DESCRIPTION("8388 USB WLAN Driver");
1034MODULE_AUTHOR("Marvell International Ltd.");
1035MODULE_LICENSE("GPL");