blob: 043a33d49f465224bfd9e6592929ef37f3ea0050 [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 Schurig10078322007-11-15 18:05:47 -050035static int if_usb_host_to_card(lbs_private *priv, u8 type, u8 *payload, u16 nb);
36static int if_usb_get_int_status(lbs_private *priv, u8 *);
37static int if_usb_read_event_cause(lbs_private *);
Dan Williams954ee162007-08-20 11:43:25 -040038static int usb_tx_block(struct usb_card_rec *cardp, u8 *payload, u16 nb);
Holger Schurigac558ca2007-08-02 11:49:06 -040039static void if_usb_free(struct usb_card_rec *cardp);
Dan Williams954ee162007-08-20 11:43:25 -040040static int if_usb_submit_rx_urb(struct usb_card_rec *cardp);
41static int if_usb_reset_device(struct usb_card_rec *cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020042
43/**
44 * @brief call back function to handle the status of the URB
45 * @param urb pointer to urb structure
46 * @return N/A
47 */
48static void if_usb_write_bulk_callback(struct urb *urb)
49{
Dan Williams954ee162007-08-20 11:43:25 -040050 struct usb_card_rec *cardp = (struct usb_card_rec *) urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020051
52 /* handle the transmission complete validations */
53
Dan Williams954ee162007-08-20 11:43:25 -040054 if (urb->status == 0) {
Holger Schurig10078322007-11-15 18:05:47 -050055 lbs_private *priv = cardp->priv;
Dan Williams954ee162007-08-20 11:43:25 -040056
Holger Schurig9012b282007-05-25 11:27:16 -040057 /*
58 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
59 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020060 urb->actual_length);
Holger Schurig9012b282007-05-25 11:27:16 -040061 */
Dan Williams954ee162007-08-20 11:43:25 -040062
63 /* Used for both firmware TX and regular TX. priv isn't
64 * valid at firmware load time.
65 */
66 if (priv) {
Holger Schurig10078322007-11-15 18:05:47 -050067 lbs_adapter *adapter = priv->adapter;
Dan Williams954ee162007-08-20 11:43:25 -040068 struct net_device *dev = priv->dev;
69
70 priv->dnld_sent = DNLD_RES_RECEIVED;
71
72 /* Wake main thread if commands are pending */
73 if (!adapter->cur_cmd)
74 wake_up_interruptible(&priv->waitq);
75
Holger Schurig10078322007-11-15 18:05:47 -050076 if ((adapter->connect_status == LBS_CONNECTED)) {
Dan Williams954ee162007-08-20 11:43:25 -040077 netif_wake_queue(dev);
78 netif_wake_queue(priv->mesh_dev);
79 }
Javier Cardona51d84f52007-05-25 12:06:56 -040080 }
Dan Williams954ee162007-08-20 11:43:25 -040081 } else {
82 /* print the failure status number for debug */
83 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020084 }
85
86 return;
87}
88
89/**
90 * @brief free tx/rx urb, skb and rx buffer
91 * @param cardp pointer usb_card_rec
92 * @return N/A
93 */
Holger Schurigac558ca2007-08-02 11:49:06 -040094static void if_usb_free(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020095{
Holger Schurig9012b282007-05-25 11:27:16 -040096 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020097
98 /* Unlink tx & rx urb */
99 usb_kill_urb(cardp->tx_urb);
100 usb_kill_urb(cardp->rx_urb);
101
102 usb_free_urb(cardp->tx_urb);
103 cardp->tx_urb = NULL;
104
105 usb_free_urb(cardp->rx_urb);
106 cardp->rx_urb = NULL;
107
108 kfree(cardp->bulk_out_buffer);
109 cardp->bulk_out_buffer = NULL;
110
Holger Schurig9012b282007-05-25 11:27:16 -0400111 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200112}
113
114/**
115 * @brief sets the configuration values
116 * @param ifnum interface number
117 * @param id pointer to usb_device_id
118 * @return 0 on success, error code on failure
119 */
120static int if_usb_probe(struct usb_interface *intf,
121 const struct usb_device_id *id)
122{
123 struct usb_device *udev;
124 struct usb_host_interface *iface_desc;
125 struct usb_endpoint_descriptor *endpoint;
Holger Schurig10078322007-11-15 18:05:47 -0500126 lbs_private *priv;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400127 struct usb_card_rec *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200128 int i;
129
130 udev = interface_to_usbdev(intf);
131
Holger Schurig435a1ac2007-05-25 12:41:52 -0400132 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
133 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200134 lbs_pr_err("Out of memory allocating private data.\n");
135 goto error;
136 }
137
Holger Schurig435a1ac2007-05-25 12:41:52 -0400138 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200139 iface_desc = intf->cur_altsetting;
140
Holger Schurig9012b282007-05-25 11:27:16 -0400141 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200142 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400143 le16_to_cpu(udev->descriptor.bcdUSB),
144 udev->descriptor.bDeviceClass,
145 udev->descriptor.bDeviceSubClass,
146 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200147
148 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
149 endpoint = &iface_desc->endpoint[i].desc;
150 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
151 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
152 USB_ENDPOINT_XFER_BULK)) {
153 /* we found a bulk in endpoint */
Holger Schurig9012b282007-05-25 11:27:16 -0400154 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400155 le16_to_cpu(endpoint->wMaxPacketSize));
156 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400157 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200158 "Rx URB allocation failed\n");
159 goto dealloc;
160 }
Holger Schurig435a1ac2007-05-25 12:41:52 -0400161 cardp->rx_urb_recall = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200162
Holger Schurig435a1ac2007-05-25 12:41:52 -0400163 cardp->bulk_in_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400164 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400165 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200166 (endpoint->
167 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400168 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200169 endpoint->bEndpointAddress);
170 }
171
172 if (((endpoint->
173 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
174 USB_DIR_OUT)
175 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
176 USB_ENDPOINT_XFER_BULK)) {
177 /* We found bulk out endpoint */
David Woodhouse981f1872007-05-25 23:36:54 -0400178 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400179 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200180 "Tx URB allocation failed\n");
181 goto dealloc;
182 }
183
Holger Schurig435a1ac2007-05-25 12:41:52 -0400184 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400185 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400186 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400187 "Bulk out size is %d\n",
188 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400189 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200190 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400191 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200192 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400193 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200194 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
195 GFP_KERNEL);
196
Holger Schurig435a1ac2007-05-25 12:41:52 -0400197 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400198 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200199 "Could not allocate buffer\n");
200 goto dealloc;
201 }
202 }
203 }
204
Dan Williams954ee162007-08-20 11:43:25 -0400205 /* Upload firmware */
206 cardp->rinfo.cardp = cardp;
207 if (if_usb_prog_firmware(cardp)) {
208 lbs_deb_usbd(&udev->dev, "FW upload failed");
209 goto err_prog_firmware;
210 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400211
Holger Schurig10078322007-11-15 18:05:47 -0500212 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400213 goto err_prog_firmware;
214
215 cardp->priv = priv;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400216
Holger Schurig10078322007-11-15 18:05:47 -0500217 if (lbs_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400218 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400219
Dan Williams954ee162007-08-20 11:43:25 -0400220 cardp->eth_dev = priv->dev;
221
Holger Schurig208fdd22007-05-25 12:17:06 -0400222 priv->hw_host_to_card = if_usb_host_to_card;
223 priv->hw_get_int_status = if_usb_get_int_status;
224 priv->hw_read_event_cause = if_usb_read_event_cause;
Luis Carlos Cobo63f00232007-08-02 13:19:24 -0400225 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400226
Dan Williams954ee162007-08-20 11:43:25 -0400227 /* Delay 200 ms to waiting for the FW ready */
228 if_usb_submit_rx_urb(cardp);
229 msleep_interruptible(200);
230 priv->adapter->fw_ready = 1;
231
Holger Schurig10078322007-11-15 18:05:47 -0500232 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400233 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400234
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200235 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400236 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200237
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200238 return 0;
239
Dan Williams954ee162007-08-20 11:43:25 -0400240err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500241 lbs_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400242err_add_mesh:
Holger Schurig10078322007-11-15 18:05:47 -0500243 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400244err_prog_firmware:
245 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200246dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400247 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200248
249error:
250 return -ENOMEM;
251}
252
253/**
254 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400255 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200256 * @return N/A
257 */
258static void if_usb_disconnect(struct usb_interface *intf)
259{
260 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig10078322007-11-15 18:05:47 -0500261 lbs_private *priv = (lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200262
Dan Williams954ee162007-08-20 11:43:25 -0400263 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200264
Dan Williams954ee162007-08-20 11:43:25 -0400265 /* Update Surprise removed to TRUE */
266 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200267
Dan Williams954ee162007-08-20 11:43:25 -0400268 if (priv) {
Holger Schurig10078322007-11-15 18:05:47 -0500269 lbs_adapter *adapter = priv->adapter;
Dan Williams954ee162007-08-20 11:43:25 -0400270
271 adapter->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500272 lbs_stop_card(priv);
273 lbs_remove_mesh(priv);
274 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400275 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200276
Andres Salomonbe13f182007-11-20 17:43:55 -0500277 /* this is (apparently?) necessary for future usage of the device */
278 lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
279 0, 0, NULL);
280
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200281 /* Unlink and free urb */
282 if_usb_free(cardp);
283
284 usb_set_intfdata(intf, NULL);
285 usb_put_dev(interface_to_usbdev(intf));
286
Dan Williams954ee162007-08-20 11:43:25 -0400287 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200288}
289
290/**
291 * @brief This function download FW
Holger Schurig10078322007-11-15 18:05:47 -0500292 * @param priv pointer to lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200293 * @return 0
294 */
Dan Williams954ee162007-08-20 11:43:25 -0400295static int if_prog_firmware(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200296{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200297 struct FWData *fwdata;
298 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400299 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200300
301 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
302
303 if (!fwdata)
304 return -1;
305
306 fwheader = &fwdata->fwheader;
307
308 if (!cardp->CRC_OK) {
309 cardp->totalbytes = cardp->fwlastblksent;
310 cardp->fwseqnum = cardp->lastseqnum - 1;
311 }
312
Holger Schurig9012b282007-05-25 11:27:16 -0400313 /*
314 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200315 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400316 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200317
318 memcpy(fwheader, &firmware[cardp->totalbytes],
319 sizeof(struct fwheader));
320
321 cardp->fwlastblksent = cardp->totalbytes;
322 cardp->totalbytes += sizeof(struct fwheader);
323
Holger Schurig9012b282007-05-25 11:27:16 -0400324 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200325 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400326 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200327
Holger Schurig9012b282007-05-25 11:27:16 -0400328 /*
329 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400330 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400331 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200332
333 cardp->fwseqnum = cardp->fwseqnum + 1;
334
David Woodhouse981f1872007-05-25 23:36:54 -0400335 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
336 cardp->lastseqnum = cardp->fwseqnum;
337 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338
David Woodhouse981f1872007-05-25 23:36:54 -0400339 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400340 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400341 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400342 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200343 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
344 cardp->totalbytes);
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
David Woodhousebb793e22007-05-25 23:38:14 -0400349 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400350 /*
351 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200352 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400353 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200354 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400355 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200356 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400357 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200358 cardp->fwfinalblk = 1;
359 }
360
Holger Schurig9012b282007-05-25 11:27:16 -0400361 /*
362 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200363 "The firmware download is done size is %d\n",
364 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400365 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200366
367 kfree(fwdata);
368
369 return 0;
370}
371
Dan Williams954ee162007-08-20 11:43:25 -0400372static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200373{
374 int ret;
Holger Schurig10078322007-11-15 18:05:47 -0500375 lbs_private * priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200376
Holger Schurig3874d0f2007-05-25 12:01:42 -0400377 lbs_deb_enter(LBS_DEB_USB);
378
Dan Williamseedc2a32007-08-02 11:36:22 -0400379 /* Try a USB port reset first, if that fails send the reset
380 * command to the firmware.
381 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200382 ret = usb_reset_device(cardp->udev);
Dan Williams954ee162007-08-20 11:43:25 -0400383 if (!ret && priv) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200384 msleep(10);
Holger Schurig10078322007-11-15 18:05:47 -0500385 ret = lbs_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200386 msleep(10);
387 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400388
389 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
390
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200391 return ret;
392}
393
394/**
395 * @brief This function transfer the data to the device.
Holger Schurig10078322007-11-15 18:05:47 -0500396 * @param priv pointer to lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200397 * @param payload pointer to payload data
398 * @param nb data length
399 * @return 0 or -1
400 */
Dan Williams954ee162007-08-20 11:43:25 -0400401static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200402{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200403 int ret = -1;
404
405 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400406 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400407 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200408 goto tx_ret;
409 }
410
411 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
412 usb_sndbulkpipe(cardp->udev,
413 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400414 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200415
416 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
417
418 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
419 /* transfer failed */
Holger Schurig9012b282007-05-25 11:27:16 -0400420 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200421 ret = -1;
422 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400423 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200424 ret = 0;
425 }
426
427tx_ret:
428 return ret;
429}
430
Dan Williams954ee162007-08-20 11:43:25 -0400431static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
432 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200433{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200434 struct sk_buff *skb;
435 struct read_cb_info *rinfo = &cardp->rinfo;
436 int ret = -1;
437
438 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
439 lbs_pr_err("No free skb\n");
440 goto rx_ret;
441 }
442
443 rinfo->skb = skb;
444
445 /* Fill the receive configuration URB and initialise the Rx call back */
446 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
447 usb_rcvbulkpipe(cardp->udev,
448 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400449 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200450 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
451 rinfo);
452
453 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
454
Holger Schurig9012b282007-05-25 11:27:16 -0400455 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
457 /* handle failure conditions */
Holger Schurig9012b282007-05-25 11:27:16 -0400458 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200459 ret = -1;
460 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400461 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200462 ret = 0;
463 }
464
465rx_ret:
466 return ret;
467}
468
Dan Williams954ee162007-08-20 11:43:25 -0400469static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200470{
Dan Williams954ee162007-08-20 11:43:25 -0400471 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200472}
473
Dan Williams954ee162007-08-20 11:43:25 -0400474static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200475{
Dan Williams954ee162007-08-20 11:43:25 -0400476 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200477}
478
479static void if_usb_receive_fwload(struct urb *urb)
480{
481 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200482 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400483 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200484 struct fwsyncheader *syncfwheader;
485 struct bootcmdrespStr bootcmdresp;
486
487 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400488 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200489 "URB status is failed during fw load\n");
490 kfree_skb(skb);
491 return;
492 }
493
494 if (cardp->bootcmdresp == 0) {
495 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
496 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400497 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200498 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400499 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400501 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200502 "Received valid boot command response\n");
503 return;
504 }
David Woodhouse981f1872007-05-25 23:36:54 -0400505 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200506 lbs_pr_info(
507 "boot cmd response wrong magic number (0x%x)\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400508 le32_to_cpu(bootcmdresp.u32magicnumber));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200509 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
510 lbs_pr_info(
511 "boot cmd response cmd_tag error (%d)\n",
512 bootcmdresp.u8cmd_tag);
513 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
514 lbs_pr_info(
515 "boot cmd response result error (%d)\n",
516 bootcmdresp.u8result);
517 } else {
518 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400519 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200520 "Received valid boot command response\n");
521 }
522 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400523 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200524 return;
525 }
526
527 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
528 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400529 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200530 kfree_skb(skb);
531 return;
532 }
533
534 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
535 sizeof(struct fwsyncheader));
536
537 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400538 /*
539 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200540 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400541 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200542 "FW received Blk seqnum = %d\n",
543 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400544 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200545 cardp->CRC_OK = 1;
546 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400547 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200548 "FW received Blk with CRC error\n");
549 cardp->CRC_OK = 0;
550 }
551
552 kfree_skb(skb);
553
554 if (cardp->fwfinalblk) {
555 cardp->fwdnldover = 1;
556 goto exit;
557 }
558
Dan Williams954ee162007-08-20 11:43:25 -0400559 if_prog_firmware(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200560
Dan Williams954ee162007-08-20 11:43:25 -0400561 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200562exit:
563 kfree(syncfwheader);
564
565 return;
566
567}
568
569#define MRVDRV_MIN_PKT_LEN 30
570
571static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
572 struct usb_card_rec *cardp,
Holger Schurig10078322007-11-15 18:05:47 -0500573 lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200574{
575 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
576 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400577 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200578 "Packet length is Invalid\n");
579 kfree_skb(skb);
580 return;
581 }
582
583 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
584 skb_put(skb, recvlength);
585 skb_pull(skb, MESSAGE_HEADER_LEN);
Holger Schurig10078322007-11-15 18:05:47 -0500586 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400587 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200588}
589
590static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
591 struct sk_buff *skb,
592 struct usb_card_rec *cardp,
Holger Schurig10078322007-11-15 18:05:47 -0500593 lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200594{
595 u8 *cmdbuf;
596 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400597 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200598 "The receive buffer is too large\n");
599 kfree_skb(skb);
600 return;
601 }
602
603 if (!in_interrupt())
604 BUG();
605
606 spin_lock(&priv->adapter->driver_lock);
607 /* take care of cur_cmd = NULL case by reading the
608 * data to clear the interrupt */
609 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400610 cmdbuf = priv->upld_buf;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400611 priv->adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200612 } else
613 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
614
Holger Schurigc95c7f92007-08-02 11:49:45 -0400615 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400616 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200617 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400618 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200619
620 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500621 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200622 spin_unlock(&priv->adapter->driver_lock);
623
Holger Schurig9012b282007-05-25 11:27:16 -0400624 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200625 "Wake up main thread to handle cmd response\n");
626
627 return;
628}
629
630/**
631 * @brief This function reads of the packet into the upload buff,
632 * wake up the main thread and initialise the Rx callack.
633 *
634 * @param urb pointer to struct urb
635 * @return N/A
636 */
637static void if_usb_receive(struct urb *urb)
638{
639 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200640 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400641 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
Holger Schurig10078322007-11-15 18:05:47 -0500642 lbs_private * priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200643
644 int recvlength = urb->actual_length;
645 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400646 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200647
Holger Schurig9012b282007-05-25 11:27:16 -0400648 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200649
650 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400651 __le32 tmp;
652
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200653 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400654 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200655 "URB status is failed\n");
656 kfree_skb(skb);
657 goto setup_for_next;
658 }
659
660 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400661 memcpy(&tmp, recvbuff, sizeof(u32));
662 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400663 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400664 "Recv length = 0x%x, Recv type = 0x%X\n",
665 recvlength, recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200666 } else if (urb->status)
667 goto rx_exit;
668
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200669 switch (recvtype) {
670 case CMD_TYPE_DATA:
671 process_cmdtypedata(recvlength, skb, cardp, priv);
672 break;
673
674 case CMD_TYPE_REQUEST:
675 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
676 break;
677
678 case CMD_TYPE_INDICATION:
679 /* Event cause handling */
680 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400681 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400682 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683 cardp->usb_event_cause);
684 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500685 lbs_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400686 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200687 break;
688 }
David Woodhouse981f1872007-05-25 23:36:54 -0400689 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400690 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500692 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200693 spin_unlock(&priv->adapter->driver_lock);
694 goto rx_exit;
695 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400696 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
697 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200698 kfree_skb(skb);
699 break;
700 }
701
702setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400703 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200704rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400705 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200706}
707
708/**
709 * @brief This function downloads data to FW
Holger Schurig10078322007-11-15 18:05:47 -0500710 * @param priv pointer to lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200711 * @param type type of data
712 * @param buf pointer to data buffer
713 * @param len number of bytes
714 * @return 0 or -1
715 */
Holger Schurig10078322007-11-15 18:05:47 -0500716static int if_usb_host_to_card(lbs_private *priv, u8 type, u8 *payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200717{
Holger Schurig634b8f42007-05-25 13:05:16 -0400718 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719
Holger Schurig9012b282007-05-25 11:27:16 -0400720 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
721 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722
723 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400724 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400725 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200726 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
727 MESSAGE_HEADER_LEN);
728
729 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400730 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400731 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200732 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
733 MESSAGE_HEADER_LEN);
734 }
735
736 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
737
Dan Williams954ee162007-08-20 11:43:25 -0400738 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400739 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200740}
741
742/* called with adapter->driver_lock held */
Holger Schurig10078322007-11-15 18:05:47 -0500743static int if_usb_get_int_status(lbs_private *priv, u8 *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200744{
Holger Schurig634b8f42007-05-25 13:05:16 -0400745 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746
747 *ireg = cardp->usb_int_cause;
748 cardp->usb_int_cause = 0;
749
Holger Schurig9012b282007-05-25 11:27:16 -0400750 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200751
752 return 0;
753}
754
Holger Schurig10078322007-11-15 18:05:47 -0500755static int if_usb_read_event_cause(lbs_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756{
Holger Schurig634b8f42007-05-25 13:05:16 -0400757 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400758
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759 priv->adapter->eventcause = cardp->usb_event_cause;
760 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400761 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200762 return 0;
763}
764
Dan Williams9e22cb62007-08-02 11:14:49 -0400765/**
766 * @brief This function issues Boot command to the Boot2 code
767 * @param ivalue 1:Boot from FW by USB-Download
768 * 2:Boot from FW in EEPROM
769 * @return 0
770 */
Dan Williams954ee162007-08-20 11:43:25 -0400771static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400772{
Dan Williams954ee162007-08-20 11:43:25 -0400773 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400774 int i;
775
776 /* Prepare command */
777 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
778 sbootcmd.u8cmd_tag = ivalue;
779 for (i=0; i<11; i++)
780 sbootcmd.au8dumy[i]=0x00;
781 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
782
783 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400784 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400785
786 return 0;
787}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200788
789
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400790/**
791 * @brief This function checks the validity of Boot2/FW image.
792 *
793 * @param data pointer to image
794 * len image length
795 * @return 0 or -1
796 */
797static int check_fwfile_format(u8 *data, u32 totlen)
798{
799 u32 bincmd, exit;
800 u32 blksize, offset, len;
801 int ret;
802
803 ret = 1;
804 exit = len = 0;
805
806 do {
807 struct fwheader *fwh = (void *)data;
808
809 bincmd = le32_to_cpu(fwh->dnldcmd);
810 blksize = le32_to_cpu(fwh->datalength);
811 switch (bincmd) {
812 case FW_HAS_DATA_TO_RECV:
813 offset = sizeof(struct fwheader) + blksize;
814 data += offset;
815 len += offset;
816 if (len >= totlen)
817 exit = 1;
818 break;
819 case FW_HAS_LAST_BLOCK:
820 exit = 1;
821 ret = 0;
822 break;
823 default:
824 exit = 1;
825 break;
826 }
827 } while (!exit);
828
829 if (ret)
830 lbs_pr_err("firmware file format check FAIL\n");
831 else
832 lbs_deb_fw("firmware file format check PASS\n");
833
834 return ret;
835}
836
837
Dan Williams954ee162007-08-20 11:43:25 -0400838static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400839{
Dan Williams954ee162007-08-20 11:43:25 -0400840 int i = 0;
841 static int reset_count = 10;
842 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400843
Dan Williams954ee162007-08-20 11:43:25 -0400844 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400845
Holger Schurig10078322007-11-15 18:05:47 -0500846 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400847 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400848 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500849 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400850 goto done;
851 }
852
Dan Williams954ee162007-08-20 11:43:25 -0400853 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
854 goto release_fw;
855
856restart:
857 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
858 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
859 ret = -1;
860 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400861 }
862
Dan Williams954ee162007-08-20 11:43:25 -0400863 cardp->bootcmdresp = 0;
864 do {
865 int j = 0;
866 i++;
867 /* Issue Boot command = 1, Boot from Download-FW */
868 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
869 /* wait for command response */
870 do {
871 j++;
872 msleep_interruptible(100);
873 } while (cardp->bootcmdresp == 0 && j < 10);
874 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400875
Dan Williams954ee162007-08-20 11:43:25 -0400876 if (cardp->bootcmdresp == 0) {
877 if (--reset_count >= 0) {
878 if_usb_reset_device(cardp);
879 goto restart;
880 }
881 return -1;
882 }
883
884 i = 0;
885
886 cardp->totalbytes = 0;
887 cardp->fwlastblksent = 0;
888 cardp->CRC_OK = 1;
889 cardp->fwdnldover = 0;
890 cardp->fwseqnum = -1;
891 cardp->totalbytes = 0;
892 cardp->fwfinalblk = 0;
893
894 if_prog_firmware(cardp);
895
896 do {
897 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
898 i++;
899 msleep_interruptible(100);
900 if (cardp->surprise_removed || i >= 20)
901 break;
902 } while (!cardp->fwdnldover);
903
904 if (!cardp->fwdnldover) {
905 lbs_pr_info("failed to load fw, resetting device!\n");
906 if (--reset_count >= 0) {
907 if_usb_reset_device(cardp);
908 goto restart;
909 }
910
911 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
912 ret = -1;
913 goto release_fw;
914 }
915
916release_fw:
917 release_firmware(cardp->fw);
918 cardp->fw = NULL;
919
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400920done:
Dan Williams954ee162007-08-20 11:43:25 -0400921 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400922 return ret;
923}
924
925
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200926#ifdef CONFIG_PM
927static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
928{
929 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig10078322007-11-15 18:05:47 -0500930 lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200931
Holger Schurig9012b282007-05-25 11:27:16 -0400932 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200933
934 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
935 return -1;
936
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400937 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
938 /* Mesh autostart must be activated while sleeping
939 * On resume it will go back to the current state
940 */
941 struct cmd_ds_mesh_access mesh_access;
942 memset(&mesh_access, 0, sizeof(mesh_access));
943 mesh_access.data[0] = cpu_to_le32(1);
Holger Schurig10078322007-11-15 18:05:47 -0500944 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400945 CMD_MESH_ACCESS,
946 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
947 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
948 }
949
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200950 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400951 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200952
953 /* Unlink tx & rx urb */
954 usb_kill_urb(cardp->tx_urb);
955 usb_kill_urb(cardp->rx_urb);
956
957 cardp->rx_urb_recall = 1;
958
Holger Schurig9012b282007-05-25 11:27:16 -0400959 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200960 return 0;
961}
962
963static int if_usb_resume(struct usb_interface *intf)
964{
965 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig10078322007-11-15 18:05:47 -0500966 lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200967
Holger Schurig9012b282007-05-25 11:27:16 -0400968 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200969
970 cardp->rx_urb_recall = 0;
971
972 if_usb_submit_rx_urb(cardp->priv);
973
974 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400975 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200976
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400977 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
978 /* Mesh autostart was activated while sleeping
979 * Disable it if appropriate
980 */
981 struct cmd_ds_mesh_access mesh_access;
982 memset(&mesh_access, 0, sizeof(mesh_access));
983 mesh_access.data[0] = cpu_to_le32(0);
Holger Schurig10078322007-11-15 18:05:47 -0500984 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400985 CMD_MESH_ACCESS,
986 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
987 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
988 }
989
Holger Schurig9012b282007-05-25 11:27:16 -0400990 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200991 return 0;
992}
993#else
994#define if_usb_suspend NULL
995#define if_usb_resume NULL
996#endif
997
998static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -0500999 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001000 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001001 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001002 .id_table = if_usb_table,
1003 .suspend = if_usb_suspend,
1004 .resume = if_usb_resume,
1005};
1006
Andres Salomon4fb910f2007-11-20 17:43:45 -05001007static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001008{
Holger Schurig084708b2007-05-25 12:37:58 -04001009 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001010
Holger Schurig084708b2007-05-25 12:37:58 -04001011 lbs_deb_enter(LBS_DEB_MAIN);
1012
Holger Schurig084708b2007-05-25 12:37:58 -04001013 ret = usb_register(&if_usb_driver);
1014
1015 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1016 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001017}
1018
Andres Salomon4fb910f2007-11-20 17:43:45 -05001019static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020{
Holger Schurig084708b2007-05-25 12:37:58 -04001021 lbs_deb_enter(LBS_DEB_MAIN);
1022
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001023 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001024
1025 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001026}
Holger Schurig084708b2007-05-25 12:37:58 -04001027
1028module_init(if_usb_init_module);
1029module_exit(if_usb_exit_module);
1030
1031MODULE_DESCRIPTION("8388 USB WLAN Driver");
1032MODULE_AUTHOR("Marvell International Ltd.");
1033MODULE_LICENSE("GPL");