blob: b19a6c9c2a783a829dd9ba8b3826ea85103bfb6c [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
Dan Williams14e865b2007-12-10 15:11:23 -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->rx_urb_recall = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200179
Holger Schurig435a1ac2007-05-25 12:41:52 -0400180 cardp->bulk_in_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400181 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400182 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200183 (endpoint->
184 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400185 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200186 endpoint->bEndpointAddress);
187 }
188
189 if (((endpoint->
190 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
191 USB_DIR_OUT)
192 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
193 USB_ENDPOINT_XFER_BULK)) {
194 /* We found bulk out endpoint */
David Woodhouse981f1872007-05-25 23:36:54 -0400195 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400196 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200197 "Tx URB allocation failed\n");
198 goto dealloc;
199 }
200
Holger Schurig435a1ac2007-05-25 12:41:52 -0400201 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400202 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400203 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400204 "Bulk out size is %d\n",
205 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400206 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200207 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400208 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200209 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400210 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200211 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
212 GFP_KERNEL);
213
Holger Schurig435a1ac2007-05-25 12:41:52 -0400214 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400215 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200216 "Could not allocate buffer\n");
217 goto dealloc;
218 }
219 }
220 }
221
Dan Williams954ee162007-08-20 11:43:25 -0400222 /* Upload firmware */
223 cardp->rinfo.cardp = cardp;
224 if (if_usb_prog_firmware(cardp)) {
225 lbs_deb_usbd(&udev->dev, "FW upload failed");
226 goto err_prog_firmware;
227 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400228
Holger Schurig10078322007-11-15 18:05:47 -0500229 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400230 goto err_prog_firmware;
231
232 cardp->priv = priv;
David Woodhousec9cd6f92007-12-11 13:15:25 -0500233 cardp->priv->fw_ready = 1;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400234
Dan Williams954ee162007-08-20 11:43:25 -0400235 cardp->eth_dev = priv->dev;
236
Holger Schurig208fdd22007-05-25 12:17:06 -0400237 priv->hw_host_to_card = if_usb_host_to_card;
238 priv->hw_get_int_status = if_usb_get_int_status;
239 priv->hw_read_event_cause = if_usb_read_event_cause;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000240 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400241
Dan Williams954ee162007-08-20 11:43:25 -0400242 if_usb_submit_rx_urb(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400243
Holger Schurig10078322007-11-15 18:05:47 -0500244 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400245 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400246
David Woodhouse04c80f12007-12-06 12:51:00 +0000247 if_usb_set_boot2_ver(priv);
David Woodhouse2c944042007-12-06 14:41:08 +0000248
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) {
Dan Williams954ee162007-08-20 11:43:25 -0400281
David Woodhouseaa21c002007-12-08 20:04:36 +0000282 priv->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500283 lbs_stop_card(priv);
Holger Schurig10078322007-11-15 18:05:47 -0500284 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400285 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200286
Andres Salomonbe13f182007-11-20 17:43:55 -0500287 /* this is (apparently?) necessary for future usage of the device */
288 lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
289 0, 0, NULL);
290
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200291 /* Unlink and free urb */
292 if_usb_free(cardp);
293
294 usb_set_intfdata(intf, NULL);
295 usb_put_dev(interface_to_usbdev(intf));
296
Dan Williams954ee162007-08-20 11:43:25 -0400297 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200298}
299
300/**
301 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100302 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200303 * @return 0
304 */
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500305static int if_usb_send_fw_pkt(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200306{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200307 struct FWData *fwdata;
308 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400309 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200310
311 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
312
313 if (!fwdata)
314 return -1;
315
316 fwheader = &fwdata->fwheader;
317
318 if (!cardp->CRC_OK) {
319 cardp->totalbytes = cardp->fwlastblksent;
320 cardp->fwseqnum = cardp->lastseqnum - 1;
321 }
322
Holger Schurig9012b282007-05-25 11:27:16 -0400323 /*
324 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200325 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400326 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200327
328 memcpy(fwheader, &firmware[cardp->totalbytes],
329 sizeof(struct fwheader));
330
331 cardp->fwlastblksent = cardp->totalbytes;
332 cardp->totalbytes += sizeof(struct fwheader);
333
Holger Schurig9012b282007-05-25 11:27:16 -0400334 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200335 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400336 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200337
Holger Schurig9012b282007-05-25 11:27:16 -0400338 /*
339 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400340 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400341 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200342
343 cardp->fwseqnum = cardp->fwseqnum + 1;
344
David Woodhouse981f1872007-05-25 23:36:54 -0400345 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
346 cardp->lastseqnum = cardp->fwseqnum;
347 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200348
David Woodhouse981f1872007-05-25 23:36:54 -0400349 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400350 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400351 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400352 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200353 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
354 cardp->totalbytes);
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
David Woodhousebb793e22007-05-25 23:38:14 -0400359 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400360 /*
361 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200362 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400363 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200364 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400365 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200366 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400367 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200368 cardp->fwfinalblk = 1;
369 }
370
Holger Schurig9012b282007-05-25 11:27:16 -0400371 /*
372 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200373 "The firmware download is done size is %d\n",
374 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400375 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200376
377 kfree(fwdata);
378
379 return 0;
380}
381
Dan Williams954ee162007-08-20 11:43:25 -0400382static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200383{
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000384 struct cmd_ds_command *cmd = (void *)&cardp->bulk_out_buffer[4];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200385 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200386
Holger Schurig3874d0f2007-05-25 12:01:42 -0400387 lbs_deb_enter(LBS_DEB_USB);
388
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000389 *(__le32 *)cardp->bulk_out_buffer = cpu_to_le32(CMD_TYPE_REQUEST);
390
391 cmd->command = cpu_to_le16(CMD_802_11_RESET);
392 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
393 cmd->result = cpu_to_le16(0);
394 cmd->seqnum = cpu_to_le16(0x5a5a);
395 cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
396 usb_tx_block(cardp, cardp->bulk_out_buffer, 4 + S_DS_GEN + sizeof(struct cmd_ds_802_11_reset));
397
David Woodhousec8ba39d2007-12-10 18:53:34 -0500398 msleep(100);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200399 ret = usb_reset_device(cardp->udev);
David Woodhousec8ba39d2007-12-10 18:53:34 -0500400 msleep(100);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400401
402 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
403
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200404 return ret;
405}
406
407/**
408 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100409 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200410 * @param payload pointer to payload data
411 * @param nb data length
412 * @return 0 or -1
413 */
Dan Williams954ee162007-08-20 11:43:25 -0400414static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200415{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200416 int ret = -1;
417
418 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400419 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400420 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200421 goto tx_ret;
422 }
423
424 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
425 usb_sndbulkpipe(cardp->udev,
426 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400427 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200428
429 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
430
431 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
432 /* transfer failed */
David Woodhouse0856e682007-12-07 15:12:26 +0000433 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200434 ret = -1;
435 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400436 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200437 ret = 0;
438 }
439
440tx_ret:
441 return ret;
442}
443
Dan Williams954ee162007-08-20 11:43:25 -0400444static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
445 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200447 struct sk_buff *skb;
448 struct read_cb_info *rinfo = &cardp->rinfo;
449 int ret = -1;
450
451 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
452 lbs_pr_err("No free skb\n");
453 goto rx_ret;
454 }
455
456 rinfo->skb = skb;
457
458 /* Fill the receive configuration URB and initialise the Rx call back */
459 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
460 usb_rcvbulkpipe(cardp->udev,
461 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400462 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200463 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
464 rinfo);
465
466 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
467
Holger Schurig9012b282007-05-25 11:27:16 -0400468 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
470 /* handle failure conditions */
David Woodhouse0856e682007-12-07 15:12:26 +0000471 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000472 kfree_skb(skb);
473 rinfo->skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200474 ret = -1;
475 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400476 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200477 ret = 0;
478 }
479
480rx_ret:
481 return ret;
482}
483
Dan Williams954ee162007-08-20 11:43:25 -0400484static int if_usb_submit_rx_urb_fwload(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_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200487}
488
Dan Williams954ee162007-08-20 11:43:25 -0400489static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200490{
Dan Williams954ee162007-08-20 11:43:25 -0400491 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200492}
493
494static void if_usb_receive_fwload(struct urb *urb)
495{
496 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200497 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400498 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200499 struct fwsyncheader *syncfwheader;
500 struct bootcmdrespStr bootcmdresp;
501
502 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400503 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200504 "URB status is failed during fw load\n");
505 kfree_skb(skb);
506 return;
507 }
508
David Woodhousec9cd6f92007-12-11 13:15:25 -0500509 if (cardp->fwdnldover) {
510 __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
511
512 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
513 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
514 lbs_pr_info("Firmware ready event received\n");
515 wake_up(&cardp->fw_wq);
516 } else {
517 lbs_deb_usb("Waiting for confirmation; got %x %x\n", le32_to_cpu(tmp[0]),
518 le32_to_cpu(tmp[1]));
519 if_usb_submit_rx_urb_fwload(cardp);
520 }
521 kfree_skb(skb);
522 return;
523 }
David Woodhousec8ba39d2007-12-10 18:53:34 -0500524 if (cardp->bootcmdresp <= 0) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200525 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
526 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400527 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200528 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400529 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200530 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400531 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200532 "Received valid boot command response\n");
533 return;
534 }
David Woodhouse981f1872007-05-25 23:36:54 -0400535 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000536 if (bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_REQUEST) ||
537 bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_DATA) ||
538 bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_INDICATION)) {
David Woodhousec9cd6f92007-12-11 13:15:25 -0500539 if (!cardp->bootcmdresp)
540 lbs_pr_info("Firmware already seems alive; resetting\n");
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000541 cardp->bootcmdresp = -1;
542 } else {
543 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
544 le32_to_cpu(bootcmdresp.u32magicnumber));
545 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200546 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
547 lbs_pr_info(
548 "boot cmd response cmd_tag error (%d)\n",
549 bootcmdresp.u8cmd_tag);
550 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
551 lbs_pr_info(
552 "boot cmd response result error (%d)\n",
553 bootcmdresp.u8result);
554 } else {
555 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400556 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200557 "Received valid boot command response\n");
558 }
559 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400560 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200561 return;
562 }
563
564 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
565 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400566 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200567 kfree_skb(skb);
568 return;
569 }
570
571 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
572 sizeof(struct fwsyncheader));
573
574 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400575 /*
576 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200577 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400578 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200579 "FW received Blk seqnum = %d\n",
580 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400581 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200582 cardp->CRC_OK = 1;
583 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400584 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200585 "FW received Blk with CRC error\n");
586 cardp->CRC_OK = 0;
587 }
588
589 kfree_skb(skb);
590
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500591 /* reschedule timer for 200ms hence */
592 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
593
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200594 if (cardp->fwfinalblk) {
595 cardp->fwdnldover = 1;
596 goto exit;
597 }
598
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500599 if_usb_send_fw_pkt(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200600
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500601 exit:
David Woodhousec9cd6f92007-12-11 13:15:25 -0500602 if_usb_submit_rx_urb_fwload(cardp);
603
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200604 kfree(syncfwheader);
605
606 return;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200607}
608
609#define MRVDRV_MIN_PKT_LEN 30
610
611static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
612 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100613 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200614{
615 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
616 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400617 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200618 "Packet length is Invalid\n");
619 kfree_skb(skb);
620 return;
621 }
622
623 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
624 skb_put(skb, recvlength);
625 skb_pull(skb, MESSAGE_HEADER_LEN);
Holger Schurig10078322007-11-15 18:05:47 -0500626 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400627 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200628}
629
630static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
631 struct sk_buff *skb,
632 struct usb_card_rec *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100633 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634{
635 u8 *cmdbuf;
Dan Williamsddac4522007-12-11 13:49:39 -0500636 if (recvlength > LBS_CMD_BUFFER_SIZE) {
Holger Schurig9012b282007-05-25 11:27:16 -0400637 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200638 "The receive buffer is too large\n");
639 kfree_skb(skb);
640 return;
641 }
642
643 if (!in_interrupt())
644 BUG();
645
David Woodhouseaa21c002007-12-08 20:04:36 +0000646 spin_lock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200647 /* take care of cur_cmd = NULL case by reading the
648 * data to clear the interrupt */
David Woodhouseaa21c002007-12-08 20:04:36 +0000649 if (!priv->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400650 cmdbuf = priv->upld_buf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000651 priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200652 } else
Dan Williamsddac4522007-12-11 13:49:39 -0500653 cmdbuf = (u8 *) priv->cur_cmd->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200654
Holger Schurigc95c7f92007-08-02 11:49:45 -0400655 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400656 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200657 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400658 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200659
660 kfree_skb(skb);
David Woodhouse4f679492007-12-10 14:58:37 -0500661 lbs_interrupt(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000662 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663
Holger Schurig9012b282007-05-25 11:27:16 -0400664 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200665 "Wake up main thread to handle cmd response\n");
666
667 return;
668}
669
670/**
671 * @brief This function reads of the packet into the upload buff,
672 * wake up the main thread and initialise the Rx callack.
673 *
674 * @param urb pointer to struct urb
675 * @return N/A
676 */
677static void if_usb_receive(struct urb *urb)
678{
679 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200680 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400681 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
Holger Schurig69f90322007-11-23 15:43:44 +0100682 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683
684 int recvlength = urb->actual_length;
685 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400686 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200687
Holger Schurig9012b282007-05-25 11:27:16 -0400688 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689
690 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400691 __le32 tmp;
692
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200693 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400694 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200695 "URB status is failed\n");
696 kfree_skb(skb);
697 goto setup_for_next;
698 }
699
700 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400701 memcpy(&tmp, recvbuff, sizeof(u32));
702 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400703 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400704 "Recv length = 0x%x, Recv type = 0x%X\n",
705 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000706 } else if (urb->status) {
707 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200708 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000709 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200711 switch (recvtype) {
712 case CMD_TYPE_DATA:
713 process_cmdtypedata(recvlength, skb, cardp, priv);
714 break;
715
716 case CMD_TYPE_REQUEST:
717 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
718 break;
719
720 case CMD_TYPE_INDICATION:
721 /* Event cause handling */
David Woodhouseaa21c002007-12-08 20:04:36 +0000722 spin_lock(&priv->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400723 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400724 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200725 cardp->usb_event_cause);
726 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500727 lbs_send_tx_feedback(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000728 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729 break;
730 }
David Woodhouse981f1872007-05-25 23:36:54 -0400731 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400732 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200733 kfree_skb(skb);
David Woodhouse4f679492007-12-10 14:58:37 -0500734 lbs_interrupt(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000735 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200736 goto rx_exit;
737 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400738 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
739 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200740 kfree_skb(skb);
741 break;
742 }
743
744setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400745 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400747 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200748}
749
750/**
751 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100752 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753 * @param type type of data
754 * @param buf pointer to data buffer
755 * @param len number of bytes
756 * @return 0 or -1
757 */
Holger Schurig69f90322007-11-23 15:43:44 +0100758static int if_usb_host_to_card(struct lbs_private *priv,
759 u8 type,
760 u8 *payload,
761 u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200762{
Holger Schurig634b8f42007-05-25 13:05:16 -0400763 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200764
Holger Schurig9012b282007-05-25 11:27:16 -0400765 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
766 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200767
768 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400769 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400770 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200771 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
772 MESSAGE_HEADER_LEN);
773
774 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400775 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400776 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200777 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
778 MESSAGE_HEADER_LEN);
779 }
780
781 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
782
Dan Williams954ee162007-08-20 11:43:25 -0400783 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400784 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200785}
786
David Woodhouseaa21c002007-12-08 20:04:36 +0000787/* called with priv->driver_lock held */
Holger Schurig69f90322007-11-23 15:43:44 +0100788static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200789{
Holger Schurig634b8f42007-05-25 13:05:16 -0400790 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200791
792 *ireg = cardp->usb_int_cause;
793 cardp->usb_int_cause = 0;
794
Holger Schurig9012b282007-05-25 11:27:16 -0400795 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200796
797 return 0;
798}
799
Holger Schurig69f90322007-11-23 15:43:44 +0100800static int if_usb_read_event_cause(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200801{
Holger Schurig634b8f42007-05-25 13:05:16 -0400802 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400803
David Woodhouseaa21c002007-12-08 20:04:36 +0000804 priv->eventcause = cardp->usb_event_cause;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200805 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400806 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200807 return 0;
808}
809
Dan Williams9e22cb62007-08-02 11:14:49 -0400810/**
811 * @brief This function issues Boot command to the Boot2 code
812 * @param ivalue 1:Boot from FW by USB-Download
813 * 2:Boot from FW in EEPROM
814 * @return 0
815 */
Dan Williams954ee162007-08-20 11:43:25 -0400816static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400817{
Dan Williams954ee162007-08-20 11:43:25 -0400818 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400819 int i;
820
821 /* Prepare command */
822 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
823 sbootcmd.u8cmd_tag = ivalue;
824 for (i=0; i<11; i++)
825 sbootcmd.au8dumy[i]=0x00;
826 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
827
828 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400829 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400830
831 return 0;
832}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200833
834
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400835/**
836 * @brief This function checks the validity of Boot2/FW image.
837 *
838 * @param data pointer to image
839 * len image length
840 * @return 0 or -1
841 */
842static int check_fwfile_format(u8 *data, u32 totlen)
843{
844 u32 bincmd, exit;
845 u32 blksize, offset, len;
846 int ret;
847
848 ret = 1;
849 exit = len = 0;
850
851 do {
852 struct fwheader *fwh = (void *)data;
853
854 bincmd = le32_to_cpu(fwh->dnldcmd);
855 blksize = le32_to_cpu(fwh->datalength);
856 switch (bincmd) {
857 case FW_HAS_DATA_TO_RECV:
858 offset = sizeof(struct fwheader) + blksize;
859 data += offset;
860 len += offset;
861 if (len >= totlen)
862 exit = 1;
863 break;
864 case FW_HAS_LAST_BLOCK:
865 exit = 1;
866 ret = 0;
867 break;
868 default:
869 exit = 1;
870 break;
871 }
872 } while (!exit);
873
874 if (ret)
875 lbs_pr_err("firmware file format check FAIL\n");
876 else
877 lbs_deb_fw("firmware file format check PASS\n");
878
879 return ret;
880}
881
882
Dan Williams954ee162007-08-20 11:43:25 -0400883static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400884{
Dan Williams954ee162007-08-20 11:43:25 -0400885 int i = 0;
886 static int reset_count = 10;
887 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400888
Dan Williams954ee162007-08-20 11:43:25 -0400889 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400890
Holger Schurig10078322007-11-15 18:05:47 -0500891 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400892 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400893 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500894 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400895 goto done;
896 }
897
Dan Williams954ee162007-08-20 11:43:25 -0400898 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
899 goto release_fw;
900
901restart:
902 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
903 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
904 ret = -1;
905 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400906 }
907
Dan Williams954ee162007-08-20 11:43:25 -0400908 cardp->bootcmdresp = 0;
909 do {
910 int j = 0;
911 i++;
912 /* Issue Boot command = 1, Boot from Download-FW */
913 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
914 /* wait for command response */
915 do {
916 j++;
917 msleep_interruptible(100);
918 } while (cardp->bootcmdresp == 0 && j < 10);
919 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400920
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000921 if (cardp->bootcmdresp <= 0) {
Dan Williams954ee162007-08-20 11:43:25 -0400922 if (--reset_count >= 0) {
923 if_usb_reset_device(cardp);
924 goto restart;
925 }
926 return -1;
927 }
928
929 i = 0;
930
931 cardp->totalbytes = 0;
932 cardp->fwlastblksent = 0;
933 cardp->CRC_OK = 1;
934 cardp->fwdnldover = 0;
935 cardp->fwseqnum = -1;
936 cardp->totalbytes = 0;
937 cardp->fwfinalblk = 0;
938
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500939 /* Send the first firmware packet... */
940 if_usb_send_fw_pkt(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400941
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500942 /* ... and wait for the process to complete */
943 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
944
945 del_timer_sync(&cardp->fw_timeout);
David Woodhousec9cd6f92007-12-11 13:15:25 -0500946 usb_kill_urb(cardp->rx_urb);
Dan Williams954ee162007-08-20 11:43:25 -0400947
948 if (!cardp->fwdnldover) {
949 lbs_pr_info("failed to load fw, resetting device!\n");
950 if (--reset_count >= 0) {
951 if_usb_reset_device(cardp);
952 goto restart;
953 }
954
955 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
956 ret = -1;
957 goto release_fw;
958 }
959
960release_fw:
961 release_firmware(cardp->fw);
962 cardp->fw = NULL;
963
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400964done:
Dan Williams954ee162007-08-20 11:43:25 -0400965 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400966 return ret;
967}
968
969
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200970#ifdef CONFIG_PM
971static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
972{
973 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100974 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200975
Holger Schurig9012b282007-05-25 11:27:16 -0400976 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200977
David Woodhouseaa21c002007-12-08 20:04:36 +0000978 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200979 return -1;
980
981 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400982 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200983
984 /* Unlink tx & rx urb */
985 usb_kill_urb(cardp->tx_urb);
986 usb_kill_urb(cardp->rx_urb);
987
988 cardp->rx_urb_recall = 1;
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
994static int if_usb_resume(struct usb_interface *intf)
995{
996 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100997 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200998
Holger Schurig9012b282007-05-25 11:27:16 -0400999 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001000
1001 cardp->rx_urb_recall = 0;
1002
David Woodhouse6bc822b2007-12-11 12:53:43 -05001003 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001004
1005 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -04001006 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001007
Holger Schurig9012b282007-05-25 11:27:16 -04001008 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001009 return 0;
1010}
1011#else
1012#define if_usb_suspend NULL
1013#define if_usb_resume NULL
1014#endif
1015
1016static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -05001017 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001018 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001019 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020 .id_table = if_usb_table,
1021 .suspend = if_usb_suspend,
1022 .resume = if_usb_resume,
1023};
1024
Andres Salomon4fb910f2007-11-20 17:43:45 -05001025static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001026{
Holger Schurig084708b2007-05-25 12:37:58 -04001027 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001028
Holger Schurig084708b2007-05-25 12:37:58 -04001029 lbs_deb_enter(LBS_DEB_MAIN);
1030
Holger Schurig084708b2007-05-25 12:37:58 -04001031 ret = usb_register(&if_usb_driver);
1032
1033 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1034 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001035}
1036
Andres Salomon4fb910f2007-11-20 17:43:45 -05001037static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001038{
Holger Schurig084708b2007-05-25 12:37:58 -04001039 lbs_deb_enter(LBS_DEB_MAIN);
1040
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001041 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001042
1043 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001044}
Holger Schurig084708b2007-05-25 12:37:58 -04001045
1046module_init(if_usb_init_module);
1047module_exit(if_usb_exit_module);
1048
1049MODULE_DESCRIPTION("8388 USB WLAN Driver");
1050MODULE_AUTHOR("Marvell International Ltd.");
1051MODULE_LICENSE("GPL");