blob: 25925bd761d8b4b586a8b765749653589bd0f2c2 [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"
Dan Williams14e865b2007-12-10 15:11:23 -050016#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020017#include "if_usb.h"
18
19#define MESSAGE_HEADER_LEN 4
20
Andres Salomon82209ad2007-11-20 17:43:32 -050021static char *lbs_fw_name = "usb8388.bin";
Holger Schurig10078322007-11-15 18:05:47 -050022module_param_named(fw_name, lbs_fw_name, charp, 0644);
Holger Schurig084708b2007-05-25 12:37:58 -040023
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020024static struct usb_device_id if_usb_table[] = {
25 /* Enter the device signature inside */
Holger Schurig66fcc552007-05-25 00:11:58 -040026 { USB_DEVICE(0x1286, 0x2001) },
27 { USB_DEVICE(0x05a3, 0x8388) },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020028 {} /* Terminating entry */
29};
30
31MODULE_DEVICE_TABLE(usb, if_usb_table);
32
33static void if_usb_receive(struct urb *urb);
34static void if_usb_receive_fwload(struct urb *urb);
Dan Williams954ee162007-08-20 11:43:25 -040035static int if_usb_prog_firmware(struct usb_card_rec *cardp);
Holger Schurig69f90322007-11-23 15:43:44 +010036static int if_usb_host_to_card(struct lbs_private *priv,
37 u8 type,
38 u8 *payload,
39 u16 nb);
40static int if_usb_get_int_status(struct lbs_private *priv, u8 *);
41static int if_usb_read_event_cause(struct lbs_private *);
Dan Williams954ee162007-08-20 11:43:25 -040042static int usb_tx_block(struct usb_card_rec *cardp, u8 *payload, u16 nb);
Holger Schurigac558ca2007-08-02 11:49:06 -040043static void if_usb_free(struct usb_card_rec *cardp);
Dan Williams954ee162007-08-20 11:43:25 -040044static int if_usb_submit_rx_urb(struct usb_card_rec *cardp);
45static int if_usb_reset_device(struct usb_card_rec *cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020046
47/**
48 * @brief call back function to handle the status of the URB
49 * @param urb pointer to urb structure
50 * @return N/A
51 */
52static void if_usb_write_bulk_callback(struct urb *urb)
53{
Dan Williams954ee162007-08-20 11:43:25 -040054 struct usb_card_rec *cardp = (struct usb_card_rec *) urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020055
56 /* handle the transmission complete validations */
57
Dan Williams954ee162007-08-20 11:43:25 -040058 if (urb->status == 0) {
Holger Schurig69f90322007-11-23 15:43:44 +010059 struct lbs_private *priv = cardp->priv;
Dan Williams954ee162007-08-20 11:43:25 -040060
Holger Schurig9012b282007-05-25 11:27:16 -040061 /*
62 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
63 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020064 urb->actual_length);
Holger Schurig9012b282007-05-25 11:27:16 -040065 */
Dan Williams954ee162007-08-20 11:43:25 -040066
67 /* Used for both firmware TX and regular TX. priv isn't
68 * valid at firmware load time.
69 */
David Woodhousee775ed72007-12-06 14:36:11 +000070 if (priv)
71 lbs_host_to_card_done(priv);
Dan Williams954ee162007-08-20 11:43:25 -040072 } else {
73 /* print the failure status number for debug */
74 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020075 }
76
77 return;
78}
79
80/**
81 * @brief free tx/rx urb, skb and rx buffer
82 * @param cardp pointer usb_card_rec
83 * @return N/A
84 */
Holger Schurigac558ca2007-08-02 11:49:06 -040085static void if_usb_free(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020086{
Holger Schurig9012b282007-05-25 11:27:16 -040087 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020088
89 /* Unlink tx & rx urb */
90 usb_kill_urb(cardp->tx_urb);
91 usb_kill_urb(cardp->rx_urb);
92
93 usb_free_urb(cardp->tx_urb);
94 cardp->tx_urb = NULL;
95
96 usb_free_urb(cardp->rx_urb);
97 cardp->rx_urb = NULL;
98
99 kfree(cardp->bulk_out_buffer);
100 cardp->bulk_out_buffer = NULL;
101
Holger Schurig9012b282007-05-25 11:27:16 -0400102 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200103}
104
David Woodhouse04c80f12007-12-06 12:51:00 +0000105static void if_usb_set_boot2_ver(struct lbs_private *priv)
106{
107 struct cmd_ds_set_boot2_ver b2_cmd;
David Woodhouse04c80f12007-12-06 12:51:00 +0000108
109 b2_cmd.action = 0;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000110 b2_cmd.version = priv->boot2_version;
David Woodhouse04c80f12007-12-06 12:51:00 +0000111
David Woodhouse689442d2007-12-12 16:00:42 -0500112 if (lbs_cmd(priv, CMD_SET_BOOT2_VER, &b2_cmd, NULL, 0))
David Woodhouse04c80f12007-12-06 12:51:00 +0000113 lbs_deb_usb("Setting boot2 version failed\n");
David Woodhouse04c80f12007-12-06 12:51:00 +0000114}
115
David Woodhouse2fd6cfe2007-12-11 17:44:10 -0500116static void if_usb_fw_timeo(unsigned long priv)
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500117{
118 struct usb_card_rec *cardp = (void *)priv;
David Woodhouse04c80f12007-12-06 12:51:00 +0000119
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500120 if (cardp->fwdnldover) {
121 lbs_deb_usb("Download complete, no event. Assuming success\n");
122 } else {
123 lbs_pr_err("Download timed out\n");
124 cardp->surprise_removed = 1;
125 }
126 wake_up(&cardp->fw_wq);
127}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200128/**
129 * @brief sets the configuration values
130 * @param ifnum interface number
131 * @param id pointer to usb_device_id
132 * @return 0 on success, error code on failure
133 */
134static int if_usb_probe(struct usb_interface *intf,
135 const struct usb_device_id *id)
136{
137 struct usb_device *udev;
138 struct usb_host_interface *iface_desc;
139 struct usb_endpoint_descriptor *endpoint;
Holger Schurig69f90322007-11-23 15:43:44 +0100140 struct lbs_private *priv;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400141 struct usb_card_rec *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200142 int i;
143
144 udev = interface_to_usbdev(intf);
145
Holger Schurig435a1ac2007-05-25 12:41:52 -0400146 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
147 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200148 lbs_pr_err("Out of memory allocating private data.\n");
149 goto error;
150 }
151
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500152 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
153 init_waitqueue_head(&cardp->fw_wq);
154
Holger Schurig435a1ac2007-05-25 12:41:52 -0400155 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200156 iface_desc = intf->cur_altsetting;
157
Holger Schurig9012b282007-05-25 11:27:16 -0400158 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200159 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400160 le16_to_cpu(udev->descriptor.bcdUSB),
161 udev->descriptor.bDeviceClass,
162 udev->descriptor.bDeviceSubClass,
163 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200164
165 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
166 endpoint = &iface_desc->endpoint[i].desc;
167 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
168 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
169 USB_ENDPOINT_XFER_BULK)) {
170 /* we found a bulk in endpoint */
Holger Schurig9012b282007-05-25 11:27:16 -0400171 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400172 le16_to_cpu(endpoint->wMaxPacketSize));
173 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400174 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200175 "Rx URB allocation failed\n");
176 goto dealloc;
177 }
Holger Schurig435a1ac2007-05-25 12:41:52 -0400178 cardp->bulk_in_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400179 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400180 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200181 (endpoint->
182 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400183 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200184 endpoint->bEndpointAddress);
185 }
186
187 if (((endpoint->
188 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
189 USB_DIR_OUT)
190 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
191 USB_ENDPOINT_XFER_BULK)) {
192 /* We found bulk out endpoint */
David Woodhouse981f1872007-05-25 23:36:54 -0400193 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400194 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200195 "Tx URB allocation failed\n");
196 goto dealloc;
197 }
198
Holger Schurig435a1ac2007-05-25 12:41:52 -0400199 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400200 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400201 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400202 "Bulk out size is %d\n",
203 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400204 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200205 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400206 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200207 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400208 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200209 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
210 GFP_KERNEL);
211
Holger Schurig435a1ac2007-05-25 12:41:52 -0400212 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400213 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200214 "Could not allocate buffer\n");
215 goto dealloc;
216 }
217 }
218 }
219
Dan Williams954ee162007-08-20 11:43:25 -0400220 /* Upload firmware */
221 cardp->rinfo.cardp = cardp;
222 if (if_usb_prog_firmware(cardp)) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500223 lbs_deb_usbd(&udev->dev, "FW upload failed\n");
Dan Williams954ee162007-08-20 11:43:25 -0400224 goto err_prog_firmware;
225 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400226
Holger Schurig10078322007-11-15 18:05:47 -0500227 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400228 goto err_prog_firmware;
229
230 cardp->priv = priv;
David Woodhousec9cd6f92007-12-11 13:15:25 -0500231 cardp->priv->fw_ready = 1;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400232
Holger Schurig208fdd22007-05-25 12:17:06 -0400233 priv->hw_host_to_card = if_usb_host_to_card;
234 priv->hw_get_int_status = if_usb_get_int_status;
235 priv->hw_read_event_cause = if_usb_read_event_cause;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000236 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400237
Dan Williams954ee162007-08-20 11:43:25 -0400238 if_usb_submit_rx_urb(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400239
Holger Schurig10078322007-11-15 18:05:47 -0500240 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400241 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400242
David Woodhouse04c80f12007-12-06 12:51:00 +0000243 if_usb_set_boot2_ver(priv);
David Woodhouse2c944042007-12-06 14:41:08 +0000244
David Woodhouse506e9022007-12-12 20:06:06 -0500245 priv->wol_gpio = 2; /* Wake via GPIO2... */
246 priv->wol_gap = 20; /* ... after 20ms */
247 lbs_host_sleep_cfg(priv, EHS_WAKE_ON_UNICAST_DATA);
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500248
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200249 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400250 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200251
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200252 return 0;
253
Dan Williams954ee162007-08-20 11:43:25 -0400254err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500255 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400256err_prog_firmware:
257 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200258dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400259 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200260
261error:
262 return -ENOMEM;
263}
264
265/**
266 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400267 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200268 * @return N/A
269 */
270static void if_usb_disconnect(struct usb_interface *intf)
271{
272 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100273 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200274
Dan Williams954ee162007-08-20 11:43:25 -0400275 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200276
Dan Williams954ee162007-08-20 11:43:25 -0400277 /* Update Surprise removed to TRUE */
278 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200279
Dan Williams954ee162007-08-20 11:43:25 -0400280 if (priv) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000281 priv->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500282 lbs_stop_card(priv);
Holger Schurig10078322007-11-15 18:05:47 -0500283 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400284 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200285
286 /* Unlink and free urb */
287 if_usb_free(cardp);
288
289 usb_set_intfdata(intf, NULL);
290 usb_put_dev(interface_to_usbdev(intf));
291
Dan Williams954ee162007-08-20 11:43:25 -0400292 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200293}
294
295/**
296 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100297 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200298 * @return 0
299 */
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500300static int if_usb_send_fw_pkt(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200301{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200302 struct FWData *fwdata;
303 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400304 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200305
306 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
307
308 if (!fwdata)
309 return -1;
310
311 fwheader = &fwdata->fwheader;
312
313 if (!cardp->CRC_OK) {
314 cardp->totalbytes = cardp->fwlastblksent;
315 cardp->fwseqnum = cardp->lastseqnum - 1;
316 }
317
Holger Schurig9012b282007-05-25 11:27:16 -0400318 /*
319 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200320 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400321 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200322
323 memcpy(fwheader, &firmware[cardp->totalbytes],
324 sizeof(struct fwheader));
325
326 cardp->fwlastblksent = cardp->totalbytes;
327 cardp->totalbytes += sizeof(struct fwheader);
328
Holger Schurig9012b282007-05-25 11:27:16 -0400329 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200330 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400331 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200332
Holger Schurig9012b282007-05-25 11:27:16 -0400333 /*
334 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400335 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400336 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200337
338 cardp->fwseqnum = cardp->fwseqnum + 1;
339
David Woodhouse981f1872007-05-25 23:36:54 -0400340 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
341 cardp->lastseqnum = cardp->fwseqnum;
342 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200343
David Woodhouse981f1872007-05-25 23:36:54 -0400344 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400345 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400346 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400347 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200348 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
349 cardp->totalbytes);
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
David Woodhousebb793e22007-05-25 23:38:14 -0400354 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400355 /*
356 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200357 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400358 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200359 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400360 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200361 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400362 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200363 cardp->fwfinalblk = 1;
364 }
365
Holger Schurig9012b282007-05-25 11:27:16 -0400366 /*
367 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200368 "The firmware download is done size is %d\n",
369 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400370 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200371
372 kfree(fwdata);
373
374 return 0;
375}
376
Dan Williams954ee162007-08-20 11:43:25 -0400377static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200378{
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000379 struct cmd_ds_command *cmd = (void *)&cardp->bulk_out_buffer[4];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200380 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200381
Holger Schurig3874d0f2007-05-25 12:01:42 -0400382 lbs_deb_enter(LBS_DEB_USB);
383
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000384 *(__le32 *)cardp->bulk_out_buffer = cpu_to_le32(CMD_TYPE_REQUEST);
385
386 cmd->command = cpu_to_le16(CMD_802_11_RESET);
387 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
388 cmd->result = cpu_to_le16(0);
389 cmd->seqnum = cpu_to_le16(0x5a5a);
390 cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
391 usb_tx_block(cardp, cardp->bulk_out_buffer, 4 + S_DS_GEN + sizeof(struct cmd_ds_802_11_reset));
392
David Woodhousec8ba39d2007-12-10 18:53:34 -0500393 msleep(100);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200394 ret = usb_reset_device(cardp->udev);
David Woodhousec8ba39d2007-12-10 18:53:34 -0500395 msleep(100);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400396
397 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
398
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200399 return ret;
400}
401
402/**
403 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100404 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200405 * @param payload pointer to payload data
406 * @param nb data length
407 * @return 0 or -1
408 */
Dan Williams954ee162007-08-20 11:43:25 -0400409static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200410{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200411 int ret = -1;
412
413 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400414 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400415 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200416 goto tx_ret;
417 }
418
419 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
420 usb_sndbulkpipe(cardp->udev,
421 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400422 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200423
424 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
425
426 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
427 /* transfer failed */
David Woodhouse0856e682007-12-07 15:12:26 +0000428 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200429 ret = -1;
430 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400431 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200432 ret = 0;
433 }
434
435tx_ret:
436 return ret;
437}
438
Dan Williams954ee162007-08-20 11:43:25 -0400439static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
440 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200441{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200442 struct sk_buff *skb;
443 struct read_cb_info *rinfo = &cardp->rinfo;
444 int ret = -1;
445
446 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
447 lbs_pr_err("No free skb\n");
448 goto rx_ret;
449 }
450
451 rinfo->skb = skb;
452
453 /* Fill the receive configuration URB and initialise the Rx call back */
454 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
455 usb_rcvbulkpipe(cardp->udev,
456 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400457 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200458 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
459 rinfo);
460
461 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
462
Holger Schurig9012b282007-05-25 11:27:16 -0400463 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200464 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
465 /* handle failure conditions */
David Woodhouse0856e682007-12-07 15:12:26 +0000466 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000467 kfree_skb(skb);
468 rinfo->skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469 ret = -1;
470 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400471 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200472 ret = 0;
473 }
474
475rx_ret:
476 return ret;
477}
478
Dan Williams954ee162007-08-20 11:43:25 -0400479static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200480{
Dan Williams954ee162007-08-20 11:43:25 -0400481 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200482}
483
Dan Williams954ee162007-08-20 11:43:25 -0400484static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200485{
Dan Williams954ee162007-08-20 11:43:25 -0400486 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200487}
488
489static void if_usb_receive_fwload(struct urb *urb)
490{
491 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200492 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400493 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200494 struct fwsyncheader *syncfwheader;
495 struct bootcmdrespStr bootcmdresp;
496
497 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400498 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200499 "URB status is failed during fw load\n");
500 kfree_skb(skb);
501 return;
502 }
503
David Woodhousec9cd6f92007-12-11 13:15:25 -0500504 if (cardp->fwdnldover) {
505 __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
506
507 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
508 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
509 lbs_pr_info("Firmware ready event received\n");
510 wake_up(&cardp->fw_wq);
511 } else {
512 lbs_deb_usb("Waiting for confirmation; got %x %x\n", le32_to_cpu(tmp[0]),
513 le32_to_cpu(tmp[1]));
514 if_usb_submit_rx_urb_fwload(cardp);
515 }
516 kfree_skb(skb);
517 return;
518 }
David Woodhousec8ba39d2007-12-10 18:53:34 -0500519 if (cardp->bootcmdresp <= 0) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200520 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
521 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400522 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200523 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400524 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200525 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400526 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200527 "Received valid boot command response\n");
528 return;
529 }
David Woodhouse981f1872007-05-25 23:36:54 -0400530 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000531 if (bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_REQUEST) ||
532 bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_DATA) ||
533 bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_INDICATION)) {
David Woodhousec9cd6f92007-12-11 13:15:25 -0500534 if (!cardp->bootcmdresp)
535 lbs_pr_info("Firmware already seems alive; resetting\n");
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000536 cardp->bootcmdresp = -1;
537 } else {
538 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
539 le32_to_cpu(bootcmdresp.u32magicnumber));
540 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200541 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
542 lbs_pr_info(
543 "boot cmd response cmd_tag error (%d)\n",
544 bootcmdresp.u8cmd_tag);
545 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
546 lbs_pr_info(
547 "boot cmd response result error (%d)\n",
548 bootcmdresp.u8result);
549 } else {
550 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400551 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200552 "Received valid boot command response\n");
553 }
554 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400555 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200556 return;
557 }
558
559 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
560 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400561 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200562 kfree_skb(skb);
563 return;
564 }
565
566 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
567 sizeof(struct fwsyncheader));
568
569 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400570 /*
571 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200572 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400573 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200574 "FW received Blk seqnum = %d\n",
575 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400576 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200577 cardp->CRC_OK = 1;
578 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400579 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200580 "FW received Blk with CRC error\n");
581 cardp->CRC_OK = 0;
582 }
583
584 kfree_skb(skb);
585
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500586 /* reschedule timer for 200ms hence */
587 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
588
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200589 if (cardp->fwfinalblk) {
590 cardp->fwdnldover = 1;
591 goto exit;
592 }
593
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500594 if_usb_send_fw_pkt(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200595
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500596 exit:
David Woodhousec9cd6f92007-12-11 13:15:25 -0500597 if_usb_submit_rx_urb_fwload(cardp);
598
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200599 kfree(syncfwheader);
600
601 return;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200602}
603
604#define MRVDRV_MIN_PKT_LEN 30
605
606static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
607 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100608 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609{
610 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
611 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400612 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200613 "Packet length is Invalid\n");
614 kfree_skb(skb);
615 return;
616 }
617
618 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
619 skb_put(skb, recvlength);
620 skb_pull(skb, MESSAGE_HEADER_LEN);
Holger Schurig10078322007-11-15 18:05:47 -0500621 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400622 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200623}
624
625static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
626 struct sk_buff *skb,
627 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100628 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200629{
630 u8 *cmdbuf;
Dan Williamsddac4522007-12-11 13:49:39 -0500631 if (recvlength > LBS_CMD_BUFFER_SIZE) {
Holger Schurig9012b282007-05-25 11:27:16 -0400632 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200633 "The receive buffer is too large\n");
634 kfree_skb(skb);
635 return;
636 }
637
638 if (!in_interrupt())
639 BUG();
640
David Woodhouseaa21c002007-12-08 20:04:36 +0000641 spin_lock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200642 /* take care of cur_cmd = NULL case by reading the
643 * data to clear the interrupt */
David Woodhouseaa21c002007-12-08 20:04:36 +0000644 if (!priv->cur_cmd) {
David Woodhousee1258172007-12-11 23:42:49 -0500645 lbs_deb_hex(LBS_DEB_HOST, "Unsolicited CMD_RESP", (void *) recvbuff + MESSAGE_HEADER_LEN, priv->upld_len);
Holger Schurig634b8f42007-05-25 13:05:16 -0400646 cmdbuf = priv->upld_buf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000647 priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200648 } else
Dan Williamsddac4522007-12-11 13:49:39 -0500649 cmdbuf = (u8 *) priv->cur_cmd->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200650
Holger Schurigc95c7f92007-08-02 11:49:45 -0400651 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400652 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200653 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400654 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200655
656 kfree_skb(skb);
David Woodhouse4f679492007-12-10 14:58:37 -0500657 lbs_interrupt(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000658 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200659
Holger Schurig9012b282007-05-25 11:27:16 -0400660 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200661 "Wake up main thread to handle cmd response\n");
662
663 return;
664}
665
666/**
667 * @brief This function reads of the packet into the upload buff,
668 * wake up the main thread and initialise the Rx callack.
669 *
670 * @param urb pointer to struct urb
671 * @return N/A
672 */
673static void if_usb_receive(struct urb *urb)
674{
675 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200676 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400677 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
Holger Schurig69f90322007-11-23 15:43:44 +0100678 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200679
680 int recvlength = urb->actual_length;
681 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400682 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683
Holger Schurig9012b282007-05-25 11:27:16 -0400684 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200685
686 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400687 __le32 tmp;
688
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400690 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691 "URB status is failed\n");
692 kfree_skb(skb);
693 goto setup_for_next;
694 }
695
696 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400697 memcpy(&tmp, recvbuff, sizeof(u32));
698 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400699 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400700 "Recv length = 0x%x, Recv type = 0x%X\n",
701 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000702 } else if (urb->status) {
703 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200704 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000705 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200706
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200707 switch (recvtype) {
708 case CMD_TYPE_DATA:
709 process_cmdtypedata(recvlength, skb, cardp, priv);
710 break;
711
712 case CMD_TYPE_REQUEST:
713 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
714 break;
715
716 case CMD_TYPE_INDICATION:
717 /* Event cause handling */
David Woodhouseaa21c002007-12-08 20:04:36 +0000718 spin_lock(&priv->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400719 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400720 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200721 cardp->usb_event_cause);
722 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500723 lbs_send_tx_feedback(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000724 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200725 break;
726 }
David Woodhouse981f1872007-05-25 23:36:54 -0400727 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400728 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729 kfree_skb(skb);
David Woodhouse4f679492007-12-10 14:58:37 -0500730 lbs_interrupt(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000731 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200732 goto rx_exit;
733 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400734 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
735 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200736 kfree_skb(skb);
737 break;
738 }
739
740setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400741 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200742rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400743 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200744}
745
746/**
747 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100748 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200749 * @param type type of data
750 * @param buf pointer to data buffer
751 * @param len number of bytes
752 * @return 0 or -1
753 */
Holger Schurig69f90322007-11-23 15:43:44 +0100754static int if_usb_host_to_card(struct lbs_private *priv,
755 u8 type,
756 u8 *payload,
757 u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200758{
Holger Schurig634b8f42007-05-25 13:05:16 -0400759 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200760
Holger Schurig9012b282007-05-25 11:27:16 -0400761 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
762 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200763
764 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400765 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400766 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200767 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
768 MESSAGE_HEADER_LEN);
769
770 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400771 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400772 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200773 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
774 MESSAGE_HEADER_LEN);
775 }
776
777 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
778
Dan Williams954ee162007-08-20 11:43:25 -0400779 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400780 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200781}
782
David Woodhouseaa21c002007-12-08 20:04:36 +0000783/* called with priv->driver_lock held */
Holger Schurig69f90322007-11-23 15:43:44 +0100784static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200785{
Holger Schurig634b8f42007-05-25 13:05:16 -0400786 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200787
788 *ireg = cardp->usb_int_cause;
789 cardp->usb_int_cause = 0;
790
Holger Schurig9012b282007-05-25 11:27:16 -0400791 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200792
793 return 0;
794}
795
Holger Schurig69f90322007-11-23 15:43:44 +0100796static int if_usb_read_event_cause(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200797{
Holger Schurig634b8f42007-05-25 13:05:16 -0400798 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400799
David Woodhouseaa21c002007-12-08 20:04:36 +0000800 priv->eventcause = cardp->usb_event_cause;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200801 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400802 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200803 return 0;
804}
805
Dan Williams9e22cb62007-08-02 11:14:49 -0400806/**
807 * @brief This function issues Boot command to the Boot2 code
808 * @param ivalue 1:Boot from FW by USB-Download
809 * 2:Boot from FW in EEPROM
810 * @return 0
811 */
Dan Williams954ee162007-08-20 11:43:25 -0400812static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400813{
Dan Williams954ee162007-08-20 11:43:25 -0400814 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400815 int i;
816
817 /* Prepare command */
818 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
819 sbootcmd.u8cmd_tag = ivalue;
820 for (i=0; i<11; i++)
821 sbootcmd.au8dumy[i]=0x00;
822 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
823
824 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400825 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400826
827 return 0;
828}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200829
830
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400831/**
832 * @brief This function checks the validity of Boot2/FW image.
833 *
834 * @param data pointer to image
835 * len image length
836 * @return 0 or -1
837 */
838static int check_fwfile_format(u8 *data, u32 totlen)
839{
840 u32 bincmd, exit;
841 u32 blksize, offset, len;
842 int ret;
843
844 ret = 1;
845 exit = len = 0;
846
847 do {
848 struct fwheader *fwh = (void *)data;
849
850 bincmd = le32_to_cpu(fwh->dnldcmd);
851 blksize = le32_to_cpu(fwh->datalength);
852 switch (bincmd) {
853 case FW_HAS_DATA_TO_RECV:
854 offset = sizeof(struct fwheader) + blksize;
855 data += offset;
856 len += offset;
857 if (len >= totlen)
858 exit = 1;
859 break;
860 case FW_HAS_LAST_BLOCK:
861 exit = 1;
862 ret = 0;
863 break;
864 default:
865 exit = 1;
866 break;
867 }
868 } while (!exit);
869
870 if (ret)
871 lbs_pr_err("firmware file format check FAIL\n");
872 else
873 lbs_deb_fw("firmware file format check PASS\n");
874
875 return ret;
876}
877
878
Dan Williams954ee162007-08-20 11:43:25 -0400879static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400880{
Dan Williams954ee162007-08-20 11:43:25 -0400881 int i = 0;
882 static int reset_count = 10;
883 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400884
Dan Williams954ee162007-08-20 11:43:25 -0400885 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400886
Holger Schurig10078322007-11-15 18:05:47 -0500887 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400888 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400889 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500890 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400891 goto done;
892 }
893
Dan Williams954ee162007-08-20 11:43:25 -0400894 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
895 goto release_fw;
896
897restart:
898 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
899 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
900 ret = -1;
901 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400902 }
903
Dan Williams954ee162007-08-20 11:43:25 -0400904 cardp->bootcmdresp = 0;
905 do {
906 int j = 0;
907 i++;
908 /* Issue Boot command = 1, Boot from Download-FW */
909 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
910 /* wait for command response */
911 do {
912 j++;
913 msleep_interruptible(100);
914 } while (cardp->bootcmdresp == 0 && j < 10);
915 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400916
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000917 if (cardp->bootcmdresp <= 0) {
Dan Williams954ee162007-08-20 11:43:25 -0400918 if (--reset_count >= 0) {
919 if_usb_reset_device(cardp);
920 goto restart;
921 }
922 return -1;
923 }
924
925 i = 0;
926
927 cardp->totalbytes = 0;
928 cardp->fwlastblksent = 0;
929 cardp->CRC_OK = 1;
930 cardp->fwdnldover = 0;
931 cardp->fwseqnum = -1;
932 cardp->totalbytes = 0;
933 cardp->fwfinalblk = 0;
934
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500935 /* Send the first firmware packet... */
936 if_usb_send_fw_pkt(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400937
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500938 /* ... and wait for the process to complete */
939 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
940
941 del_timer_sync(&cardp->fw_timeout);
David Woodhousec9cd6f92007-12-11 13:15:25 -0500942 usb_kill_urb(cardp->rx_urb);
Dan Williams954ee162007-08-20 11:43:25 -0400943
944 if (!cardp->fwdnldover) {
945 lbs_pr_info("failed to load fw, resetting device!\n");
946 if (--reset_count >= 0) {
947 if_usb_reset_device(cardp);
948 goto restart;
949 }
950
951 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
952 ret = -1;
953 goto release_fw;
954 }
955
956release_fw:
957 release_firmware(cardp->fw);
958 cardp->fw = NULL;
959
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400960done:
Dan Williams954ee162007-08-20 11:43:25 -0400961 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400962 return ret;
963}
964
965
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200966#ifdef CONFIG_PM
967static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
968{
969 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100970 struct lbs_private *priv = cardp->priv;
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500971 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200972
Holger Schurig9012b282007-05-25 11:27:16 -0400973 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200974
David Woodhouseaa21c002007-12-08 20:04:36 +0000975 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200976 return -1;
977
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500978 ret = lbs_suspend(priv);
979 if (ret)
980 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200981
982 /* Unlink tx & rx urb */
983 usb_kill_urb(cardp->tx_urb);
984 usb_kill_urb(cardp->rx_urb);
985
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500986 out:
Holger Schurig9012b282007-05-25 11:27:16 -0400987 lbs_deb_leave(LBS_DEB_USB);
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500988 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200989}
990
991static int if_usb_resume(struct usb_interface *intf)
992{
993 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100994 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200995
Holger Schurig9012b282007-05-25 11:27:16 -0400996 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200997
David Woodhouse6bc822b2007-12-11 12:53:43 -0500998 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200999
David Woodhoused1f7a5b2007-12-12 17:40:56 -05001000 lbs_resume(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001001
Holger Schurig9012b282007-05-25 11:27:16 -04001002 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001003 return 0;
1004}
1005#else
1006#define if_usb_suspend NULL
1007#define if_usb_resume NULL
1008#endif
1009
1010static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -05001011 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001012 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001013 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001014 .id_table = if_usb_table,
1015 .suspend = if_usb_suspend,
1016 .resume = if_usb_resume,
1017};
1018
Andres Salomon4fb910f2007-11-20 17:43:45 -05001019static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020{
Holger Schurig084708b2007-05-25 12:37:58 -04001021 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001022
Holger Schurig084708b2007-05-25 12:37:58 -04001023 lbs_deb_enter(LBS_DEB_MAIN);
1024
Holger Schurig084708b2007-05-25 12:37:58 -04001025 ret = usb_register(&if_usb_driver);
1026
1027 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1028 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001029}
1030
Andres Salomon4fb910f2007-11-20 17:43:45 -05001031static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001032{
Holger Schurig084708b2007-05-25 12:37:58 -04001033 lbs_deb_enter(LBS_DEB_MAIN);
1034
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001035 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001036
1037 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001038}
Holger Schurig084708b2007-05-25 12:37:58 -04001039
1040module_init(if_usb_init_module);
1041module_exit(if_usb_exit_module);
1042
1043MODULE_DESCRIPTION("8388 USB WLAN Driver");
1044MODULE_AUTHOR("Marvell International Ltd.");
1045MODULE_LICENSE("GPL");