blob: 8bc23b3ae6406dc1d2c4c58f680065ae280b3443 [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
David Woodhouseeae86bf2007-12-14 00:47:05 -050019#define INSANEDEBUG 0
20#define lbs_deb_usb2(...) do { if (INSANEDEBUG) lbs_deb_usbd(__VA_ARGS__); } while (0)
21
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020022#define MESSAGE_HEADER_LEN 4
23
Andres Salomon82209ad2007-11-20 17:43:32 -050024static char *lbs_fw_name = "usb8388.bin";
Holger Schurig10078322007-11-15 18:05:47 -050025module_param_named(fw_name, lbs_fw_name, charp, 0644);
Holger Schurig084708b2007-05-25 12:37:58 -040026
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020027static struct usb_device_id if_usb_table[] = {
28 /* Enter the device signature inside */
Holger Schurig66fcc552007-05-25 00:11:58 -040029 { USB_DEVICE(0x1286, 0x2001) },
30 { USB_DEVICE(0x05a3, 0x8388) },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020031 {} /* Terminating entry */
32};
33
34MODULE_DEVICE_TABLE(usb, if_usb_table);
35
36static void if_usb_receive(struct urb *urb);
37static void if_usb_receive_fwload(struct urb *urb);
David Woodhouseeae86bf2007-12-14 00:47:05 -050038static int if_usb_prog_firmware(struct if_usb_card *cardp);
39static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
40 uint8_t *payload, uint16_t nb);
41static int if_usb_get_int_status(struct lbs_private *priv, uint8_t *);
Holger Schurig69f90322007-11-23 15:43:44 +010042static int if_usb_read_event_cause(struct lbs_private *);
David Woodhouseeae86bf2007-12-14 00:47:05 -050043static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
44 uint16_t nb);
45static void if_usb_free(struct if_usb_card *cardp);
46static int if_usb_submit_rx_urb(struct if_usb_card *cardp);
47static int if_usb_reset_device(struct if_usb_card *cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020048
49/**
50 * @brief call back function to handle the status of the URB
51 * @param urb pointer to urb structure
52 * @return N/A
53 */
54static void if_usb_write_bulk_callback(struct urb *urb)
55{
David Woodhouseeae86bf2007-12-14 00:47:05 -050056 struct if_usb_card *cardp = (struct if_usb_card *) urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020057
58 /* handle the transmission complete validations */
59
Dan Williams954ee162007-08-20 11:43:25 -040060 if (urb->status == 0) {
Holger Schurig69f90322007-11-23 15:43:44 +010061 struct lbs_private *priv = cardp->priv;
Dan Williams954ee162007-08-20 11:43:25 -040062
David Woodhouseeae86bf2007-12-14 00:47:05 -050063 lbs_deb_usb2(&urb->dev->dev, "URB status is successful\n");
64 lbs_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
65 urb->actual_length);
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
David Woodhouseeae86bf2007-12-14 00:47:05 -050082 * @param cardp pointer if_usb_card
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020083 * @return N/A
84 */
David Woodhouseeae86bf2007-12-14 00:47:05 -050085static void if_usb_free(struct if_usb_card *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
David Woodhouseeae86bf2007-12-14 00:47:05 -050099 kfree(cardp->ep_out_buf);
100 cardp->ep_out_buf = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200101
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
David Woodhouse9fae8992007-12-15 03:46:44 -0500109 b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
David Woodhouse04c80f12007-12-06 12:51:00 +0000110 b2_cmd.action = 0;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000111 b2_cmd.version = priv->boot2_version;
David Woodhouse04c80f12007-12-06 12:51:00 +0000112
David Woodhouseb23b2062007-12-15 01:22:09 -0500113 if (lbs_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
David Woodhouse04c80f12007-12-06 12:51:00 +0000114 lbs_deb_usb("Setting boot2 version failed\n");
David Woodhouse04c80f12007-12-06 12:51:00 +0000115}
116
David Woodhouse2fd6cfe2007-12-11 17:44:10 -0500117static void if_usb_fw_timeo(unsigned long priv)
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500118{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500119 struct if_usb_card *cardp = (void *)priv;
David Woodhouse04c80f12007-12-06 12:51:00 +0000120
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500121 if (cardp->fwdnldover) {
122 lbs_deb_usb("Download complete, no event. Assuming success\n");
123 } else {
124 lbs_pr_err("Download timed out\n");
125 cardp->surprise_removed = 1;
126 }
127 wake_up(&cardp->fw_wq);
128}
David Woodhouseeae86bf2007-12-14 00:47:05 -0500129
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200130/**
131 * @brief sets the configuration values
132 * @param ifnum interface number
133 * @param id pointer to usb_device_id
134 * @return 0 on success, error code on failure
135 */
136static int if_usb_probe(struct usb_interface *intf,
137 const struct usb_device_id *id)
138{
139 struct usb_device *udev;
140 struct usb_host_interface *iface_desc;
141 struct usb_endpoint_descriptor *endpoint;
Holger Schurig69f90322007-11-23 15:43:44 +0100142 struct lbs_private *priv;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500143 struct if_usb_card *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200144 int i;
145
146 udev = interface_to_usbdev(intf);
147
David Woodhouseeae86bf2007-12-14 00:47:05 -0500148 cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400149 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200150 lbs_pr_err("Out of memory allocating private data.\n");
151 goto error;
152 }
153
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500154 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
155 init_waitqueue_head(&cardp->fw_wq);
David Woodhouse7e226272007-12-14 22:53:41 -0500156
Holger Schurig435a1ac2007-05-25 12:41:52 -0400157 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200158 iface_desc = intf->cur_altsetting;
159
Holger Schurig9012b282007-05-25 11:27:16 -0400160 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
David Woodhouseeae86bf2007-12-14 00:47:05 -0500161 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400162 le16_to_cpu(udev->descriptor.bcdUSB),
163 udev->descriptor.bDeviceClass,
164 udev->descriptor.bDeviceSubClass,
165 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200166
167 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
168 endpoint = &iface_desc->endpoint[i].desc;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500169 if (usb_endpoint_is_bulk_in(endpoint)) {
170 cardp->ep_in_size = le16_to_cpu(endpoint->wMaxPacketSize);
171 cardp->ep_in = usb_endpoint_num(endpoint);
172
173 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n", cardp->ep_in);
174 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n", cardp->ep_in_size);
175
176 } else if (usb_endpoint_is_bulk_out(endpoint)) {
177 cardp->ep_out_size = le16_to_cpu(endpoint->wMaxPacketSize);
178 cardp->ep_out = usb_endpoint_num(endpoint);
179
180 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n", cardp->ep_out);
181 lbs_deb_usbd(&udev->dev, "Bulk out size is %d\n", cardp->ep_out_size);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200182 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500183 }
184 if (!cardp->ep_out_size || !cardp->ep_in_size) {
185 lbs_deb_usbd(&udev->dev, "Endpoints not found\n");
186 goto dealloc;
187 }
188 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
189 lbs_deb_usbd(&udev->dev, "Rx URB allocation failed\n");
190 goto dealloc;
191 }
192 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
193 lbs_deb_usbd(&udev->dev, "Tx URB allocation failed\n");
194 goto dealloc;
195 }
196 cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE, GFP_KERNEL);
197 if (!cardp->ep_out_buf) {
198 lbs_deb_usbd(&udev->dev, "Could not allocate buffer\n");
199 goto dealloc;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200200 }
201
Dan Williams954ee162007-08-20 11:43:25 -0400202 /* Upload firmware */
Dan Williams954ee162007-08-20 11:43:25 -0400203 if (if_usb_prog_firmware(cardp)) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500204 lbs_deb_usbd(&udev->dev, "FW upload failed\n");
Dan Williams954ee162007-08-20 11:43:25 -0400205 goto err_prog_firmware;
206 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400207
Holger Schurig10078322007-11-15 18:05:47 -0500208 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400209 goto err_prog_firmware;
210
211 cardp->priv = priv;
David Woodhousec9cd6f92007-12-11 13:15:25 -0500212 cardp->priv->fw_ready = 1;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400213
Holger Schurig208fdd22007-05-25 12:17:06 -0400214 priv->hw_host_to_card = if_usb_host_to_card;
215 priv->hw_get_int_status = if_usb_get_int_status;
216 priv->hw_read_event_cause = if_usb_read_event_cause;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000217 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400218
Dan Williams954ee162007-08-20 11:43:25 -0400219 if_usb_submit_rx_urb(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400220
Holger Schurig10078322007-11-15 18:05:47 -0500221 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400222 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400223
David Woodhouse04c80f12007-12-06 12:51:00 +0000224 if_usb_set_boot2_ver(priv);
David Woodhouse2c944042007-12-06 14:41:08 +0000225
David Woodhouse506e9022007-12-12 20:06:06 -0500226 priv->wol_gpio = 2; /* Wake via GPIO2... */
227 priv->wol_gap = 20; /* ... after 20ms */
228 lbs_host_sleep_cfg(priv, EHS_WAKE_ON_UNICAST_DATA);
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500229
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200230 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400231 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200232
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200233 return 0;
234
Dan Williams954ee162007-08-20 11:43:25 -0400235err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500236 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400237err_prog_firmware:
238 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200239dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400240 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200241
242error:
243 return -ENOMEM;
244}
245
246/**
247 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400248 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200249 * @return N/A
250 */
251static void if_usb_disconnect(struct usb_interface *intf)
252{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500253 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100254 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200255
Dan Williams954ee162007-08-20 11:43:25 -0400256 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200257
Dan Williams954ee162007-08-20 11:43:25 -0400258 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200259
Dan Williams954ee162007-08-20 11:43:25 -0400260 if (priv) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000261 priv->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500262 lbs_stop_card(priv);
Holger Schurig10078322007-11-15 18:05:47 -0500263 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400264 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200265
266 /* Unlink and free urb */
267 if_usb_free(cardp);
268
269 usb_set_intfdata(intf, NULL);
270 usb_put_dev(interface_to_usbdev(intf));
271
Dan Williams954ee162007-08-20 11:43:25 -0400272 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200273}
274
275/**
276 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100277 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200278 * @return 0
279 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500280static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200281{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500282 struct fwdata *fwdata = cardp->ep_out_buf;
283 uint8_t *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200284
David Woodhouseeae86bf2007-12-14 00:47:05 -0500285 /* If we got a CRC failure on the last block, back
286 up and retry it */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200287 if (!cardp->CRC_OK) {
288 cardp->totalbytes = cardp->fwlastblksent;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500289 cardp->fwseqnum--;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200290 }
291
David Woodhouseeae86bf2007-12-14 00:47:05 -0500292 lbs_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
293 cardp->totalbytes);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200294
David Woodhouseeae86bf2007-12-14 00:47:05 -0500295 /* struct fwdata (which we sent to the card) has an
296 extra __le32 field in between the header and the data,
297 which is not in the struct fwheader in the actual
298 firmware binary. Insert the seqnum in the middle... */
299 memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200300 sizeof(struct fwheader));
301
302 cardp->fwlastblksent = cardp->totalbytes;
303 cardp->totalbytes += sizeof(struct fwheader);
304
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200305 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouseeae86bf2007-12-14 00:47:05 -0500306 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200307
David Woodhouseeae86bf2007-12-14 00:47:05 -0500308 lbs_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
309 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200310
David Woodhouseeae86bf2007-12-14 00:47:05 -0500311 fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
312 cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200313
David Woodhouseeae86bf2007-12-14 00:47:05 -0500314 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
315 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200316
David Woodhouseeae86bf2007-12-14 00:47:05 -0500317 if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
318 lbs_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
319 lbs_deb_usb2(&cardp->udev->dev, "seqnum = %d totalbytes = %d\n",
320 cardp->fwseqnum, cardp->totalbytes);
321 } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
322 lbs_deb_usb2(&cardp->udev->dev, "Host has finished FW downloading\n");
323 lbs_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200324
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200325 cardp->fwfinalblk = 1;
326 }
327
David Woodhouseeae86bf2007-12-14 00:47:05 -0500328 lbs_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
329 cardp->totalbytes);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200330
331 return 0;
332}
333
David Woodhouseeae86bf2007-12-14 00:47:05 -0500334static int if_usb_reset_device(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200335{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500336 struct cmd_ds_command *cmd = cardp->ep_out_buf + 4;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200337 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338
Holger Schurig3874d0f2007-05-25 12:01:42 -0400339 lbs_deb_enter(LBS_DEB_USB);
340
David Woodhouseeae86bf2007-12-14 00:47:05 -0500341 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000342
343 cmd->command = cpu_to_le16(CMD_802_11_RESET);
344 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
345 cmd->result = cpu_to_le16(0);
346 cmd->seqnum = cpu_to_le16(0x5a5a);
347 cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500348 usb_tx_block(cardp, cardp->ep_out_buf, 4 + S_DS_GEN + sizeof(struct cmd_ds_802_11_reset));
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000349
David Woodhousec8ba39d2007-12-10 18:53:34 -0500350 msleep(100);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200351 ret = usb_reset_device(cardp->udev);
David Woodhousec8ba39d2007-12-10 18:53:34 -0500352 msleep(100);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400353
354 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
355
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200356 return ret;
357}
358
359/**
360 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100361 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200362 * @param payload pointer to payload data
363 * @param nb data length
364 * @return 0 or -1
365 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500366static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, uint16_t nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200367{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200368 int ret = -1;
369
370 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400371 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400372 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200373 goto tx_ret;
374 }
375
376 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
377 usb_sndbulkpipe(cardp->udev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500378 cardp->ep_out),
Dan Williams954ee162007-08-20 11:43:25 -0400379 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200380
381 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
382
383 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
David Woodhouse0856e682007-12-07 15:12:26 +0000384 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200385 ret = -1;
386 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500387 lbs_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200388 ret = 0;
389 }
390
391tx_ret:
392 return ret;
393}
394
David Woodhouseeae86bf2007-12-14 00:47:05 -0500395static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
Dan Williams954ee162007-08-20 11:43:25 -0400396 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200397{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200398 struct sk_buff *skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200399 int ret = -1;
400
401 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
402 lbs_pr_err("No free skb\n");
403 goto rx_ret;
404 }
405
David Woodhouseeae86bf2007-12-14 00:47:05 -0500406 cardp->rx_skb = skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200407
408 /* Fill the receive configuration URB and initialise the Rx call back */
409 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500410 usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
Dan Williams4269e2a2007-05-10 23:10:18 -0400411 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200412 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500413 cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200414
415 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
416
David Woodhouseeae86bf2007-12-14 00:47:05 -0500417 lbs_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200418 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
David Woodhouse0856e682007-12-07 15:12:26 +0000419 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000420 kfree_skb(skb);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500421 cardp->rx_skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200422 ret = -1;
423 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500424 lbs_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200425 ret = 0;
426 }
427
428rx_ret:
429 return ret;
430}
431
David Woodhouseeae86bf2007-12-14 00:47:05 -0500432static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200433{
Dan Williams954ee162007-08-20 11:43:25 -0400434 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200435}
436
David Woodhouseeae86bf2007-12-14 00:47:05 -0500437static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438{
Dan Williams954ee162007-08-20 11:43:25 -0400439 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200440}
441
442static void if_usb_receive_fwload(struct urb *urb)
443{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500444 struct if_usb_card *cardp = urb->context;
445 struct sk_buff *skb = cardp->rx_skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446 struct fwsyncheader *syncfwheader;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500447 struct bootcmdresp bootcmdresp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200448
449 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400450 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500451 "URB status is failed during fw load\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200452 kfree_skb(skb);
453 return;
454 }
455
David Woodhousec9cd6f92007-12-11 13:15:25 -0500456 if (cardp->fwdnldover) {
457 __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
458
459 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
460 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
461 lbs_pr_info("Firmware ready event received\n");
462 wake_up(&cardp->fw_wq);
463 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500464 lbs_deb_usb("Waiting for confirmation; got %x %x\n",
465 le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
David Woodhousec9cd6f92007-12-11 13:15:25 -0500466 if_usb_submit_rx_urb_fwload(cardp);
467 }
468 kfree_skb(skb);
469 return;
470 }
David Woodhousec8ba39d2007-12-10 18:53:34 -0500471 if (cardp->bootcmdresp <= 0) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200472 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
473 sizeof(bootcmdresp));
David Woodhouseeae86bf2007-12-14 00:47:05 -0500474
David Woodhouse981f1872007-05-25 23:36:54 -0400475 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400477 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200478 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400479 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500480 "Received valid boot command response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200481 return;
482 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500483 if (bootcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
484 if (bootcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
485 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
486 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
David Woodhousec9cd6f92007-12-11 13:15:25 -0500487 if (!cardp->bootcmdresp)
488 lbs_pr_info("Firmware already seems alive; resetting\n");
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000489 cardp->bootcmdresp = -1;
490 } else {
491 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
David Woodhouseeae86bf2007-12-14 00:47:05 -0500492 le32_to_cpu(bootcmdresp.magic));
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000493 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500494 } else if (bootcmdresp.cmd != BOOT_CMD_FW_BY_USB) {
495 lbs_pr_info("boot cmd response cmd_tag error (%d)\n",
496 bootcmdresp.cmd);
497 } else if (bootcmdresp.result != BOOT_CMD_RESP_OK) {
498 lbs_pr_info("boot cmd response result error (%d)\n",
499 bootcmdresp.result);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500 } else {
501 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400502 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500503 "Received valid boot command response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200504 }
505 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400506 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200507 return;
508 }
509
510 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
511 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400512 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513 kfree_skb(skb);
514 return;
515 }
516
517 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500518 sizeof(struct fwsyncheader));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200519
520 if (!syncfwheader->cmd) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500521 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk with correct CRC\n");
522 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk seqnum = %d\n",
523 le32_to_cpu(syncfwheader->seqnum));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200524 cardp->CRC_OK = 1;
525 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500526 lbs_deb_usbd(&cardp->udev->dev, "FW received Blk with CRC error\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200527 cardp->CRC_OK = 0;
528 }
529
530 kfree_skb(skb);
531
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500532 /* reschedule timer for 200ms hence */
533 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
534
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200535 if (cardp->fwfinalblk) {
536 cardp->fwdnldover = 1;
537 goto exit;
538 }
539
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500540 if_usb_send_fw_pkt(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200541
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500542 exit:
David Woodhousec9cd6f92007-12-11 13:15:25 -0500543 if_usb_submit_rx_urb_fwload(cardp);
544
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200545 kfree(syncfwheader);
546
547 return;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200548}
549
550#define MRVDRV_MIN_PKT_LEN 30
551
552static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500553 struct if_usb_card *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100554 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200555{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500556 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
557 || recvlength < MRVDRV_MIN_PKT_LEN) {
558 lbs_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559 kfree_skb(skb);
560 return;
561 }
562
563 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
564 skb_put(skb, recvlength);
565 skb_pull(skb, MESSAGE_HEADER_LEN);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500566
Holger Schurig10078322007-11-15 18:05:47 -0500567 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400568 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200569}
570
David Woodhouseeae86bf2007-12-14 00:47:05 -0500571static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200572 struct sk_buff *skb,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500573 struct if_usb_card *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100574 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200575{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500576 uint8_t *cmdbuf;
577
Dan Williamsddac4522007-12-11 13:49:39 -0500578 if (recvlength > LBS_CMD_BUFFER_SIZE) {
Holger Schurig9012b282007-05-25 11:27:16 -0400579 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500580 "The receive buffer is too large\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200581 kfree_skb(skb);
582 return;
583 }
584
585 if (!in_interrupt())
586 BUG();
587
David Woodhouseaa21c002007-12-08 20:04:36 +0000588 spin_lock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200589 /* take care of cur_cmd = NULL case by reading the
590 * data to clear the interrupt */
David Woodhouseaa21c002007-12-08 20:04:36 +0000591 if (!priv->cur_cmd) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500592 lbs_deb_hex(LBS_DEB_HOST, "Unsolicited CMD_RESP",
593 (void *) recvbuff + MESSAGE_HEADER_LEN, priv->upld_len);
Holger Schurig634b8f42007-05-25 13:05:16 -0400594 cmdbuf = priv->upld_buf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000595 priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200596 } else
David Woodhouseeae86bf2007-12-14 00:47:05 -0500597 cmdbuf = (uint8_t *) priv->cur_cmd->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200598
Holger Schurigc95c7f92007-08-02 11:49:45 -0400599 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400600 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500601 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN, priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200602
603 kfree_skb(skb);
David Woodhouse4f679492007-12-10 14:58:37 -0500604 lbs_interrupt(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000605 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200606
Holger Schurig9012b282007-05-25 11:27:16 -0400607 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200608 "Wake up main thread to handle cmd response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609}
610
611/**
612 * @brief This function reads of the packet into the upload buff,
613 * wake up the main thread and initialise the Rx callack.
614 *
615 * @param urb pointer to struct urb
616 * @return N/A
617 */
618static void if_usb_receive(struct urb *urb)
619{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500620 struct if_usb_card *cardp = urb->context;
621 struct sk_buff *skb = cardp->rx_skb;
Holger Schurig69f90322007-11-23 15:43:44 +0100622 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200623 int recvlength = urb->actual_length;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500624 uint8_t *recvbuff = NULL;
625 uint32_t recvtype = 0;
626 __le32 *pkt = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200627
Holger Schurig9012b282007-05-25 11:27:16 -0400628 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200629
630 if (recvlength) {
631 if (urb->status) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500632 lbs_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
633 urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634 kfree_skb(skb);
635 goto setup_for_next;
636 }
637
638 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500639 recvtype = le32_to_cpu(pkt[0]);
Holger Schurig9012b282007-05-25 11:27:16 -0400640 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400641 "Recv length = 0x%x, Recv type = 0x%X\n",
642 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000643 } else if (urb->status) {
644 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200645 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000646 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200647
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200648 switch (recvtype) {
649 case CMD_TYPE_DATA:
650 process_cmdtypedata(recvlength, skb, cardp, priv);
651 break;
652
653 case CMD_TYPE_REQUEST:
654 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
655 break;
656
657 case CMD_TYPE_INDICATION:
658 /* Event cause handling */
David Woodhouseaa21c002007-12-08 20:04:36 +0000659 spin_lock(&priv->driver_lock);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500660
661 cardp->usb_event_cause = le32_to_cpu(pkt[1]);
662
Holger Schurig9012b282007-05-25 11:27:16 -0400663 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
David Woodhouseeae86bf2007-12-14 00:47:05 -0500664 cardp->usb_event_cause);
665
666 /* Icky undocumented magic special case */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200667 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500668 lbs_send_tx_feedback(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000669 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200670 break;
671 }
David Woodhouse981f1872007-05-25 23:36:54 -0400672 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400673 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200674 kfree_skb(skb);
David Woodhouse4f679492007-12-10 14:58:37 -0500675 lbs_interrupt(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000676 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200677 goto rx_exit;
678 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400679 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
David Woodhouseeae86bf2007-12-14 00:47:05 -0500680 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200681 kfree_skb(skb);
682 break;
683 }
684
685setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400686 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200687rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400688 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689}
690
691/**
692 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100693 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694 * @param type type of data
695 * @param buf pointer to data buffer
696 * @param len number of bytes
697 * @return 0 or -1
698 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500699static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
700 uint8_t *payload, uint16_t nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200701{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500702 struct if_usb_card *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703
Holger Schurig9012b282007-05-25 11:27:16 -0400704 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
705 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200706
707 if (type == MVMS_CMD) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500708 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400709 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500711 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400712 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200713 }
714
David Woodhouseeae86bf2007-12-14 00:47:05 -0500715 memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716
David Woodhouseeae86bf2007-12-14 00:47:05 -0500717 return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200718}
719
David Woodhouseaa21c002007-12-08 20:04:36 +0000720/* called with priv->driver_lock held */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500721static int if_usb_get_int_status(struct lbs_private *priv, uint8_t *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500723 struct if_usb_card *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200724
725 *ireg = cardp->usb_int_cause;
726 cardp->usb_int_cause = 0;
727
David Woodhouseeae86bf2007-12-14 00:47:05 -0500728 lbs_deb_usbd(&cardp->udev->dev, "Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729
730 return 0;
731}
732
Holger Schurig69f90322007-11-23 15:43:44 +0100733static int if_usb_read_event_cause(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200734{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500735 struct if_usb_card *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400736
David Woodhouseaa21c002007-12-08 20:04:36 +0000737 priv->eventcause = cardp->usb_event_cause;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200738 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400739 if_usb_submit_rx_urb(cardp);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500740
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200741 return 0;
742}
743
Dan Williams9e22cb62007-08-02 11:14:49 -0400744/**
745 * @brief This function issues Boot command to the Boot2 code
746 * @param ivalue 1:Boot from FW by USB-Download
747 * 2:Boot from FW in EEPROM
748 * @return 0
749 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500750static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400751{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500752 struct bootcmd *bootcmd = cardp->ep_out_buf;
Dan Williams9e22cb62007-08-02 11:14:49 -0400753
754 /* Prepare command */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500755 bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
756 bootcmd->cmd = ivalue;
757 memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
Dan Williams9e22cb62007-08-02 11:14:49 -0400758
759 /* Issue command */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500760 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd));
Dan Williams9e22cb62007-08-02 11:14:49 -0400761
762 return 0;
763}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200764
765
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400766/**
767 * @brief This function checks the validity of Boot2/FW image.
768 *
769 * @param data pointer to image
770 * len image length
771 * @return 0 or -1
772 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500773static int check_fwfile_format(uint8_t *data, uint32_t totlen)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400774{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500775 uint32_t bincmd, exit;
776 uint32_t blksize, offset, len;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400777 int ret;
778
779 ret = 1;
780 exit = len = 0;
781
782 do {
783 struct fwheader *fwh = (void *)data;
784
785 bincmd = le32_to_cpu(fwh->dnldcmd);
786 blksize = le32_to_cpu(fwh->datalength);
787 switch (bincmd) {
788 case FW_HAS_DATA_TO_RECV:
789 offset = sizeof(struct fwheader) + blksize;
790 data += offset;
791 len += offset;
792 if (len >= totlen)
793 exit = 1;
794 break;
795 case FW_HAS_LAST_BLOCK:
796 exit = 1;
797 ret = 0;
798 break;
799 default:
800 exit = 1;
801 break;
802 }
803 } while (!exit);
804
805 if (ret)
806 lbs_pr_err("firmware file format check FAIL\n");
807 else
808 lbs_deb_fw("firmware file format check PASS\n");
809
810 return ret;
811}
812
813
David Woodhouseeae86bf2007-12-14 00:47:05 -0500814static int if_usb_prog_firmware(struct if_usb_card *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400815{
Dan Williams954ee162007-08-20 11:43:25 -0400816 int i = 0;
817 static int reset_count = 10;
818 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400819
Dan Williams954ee162007-08-20 11:43:25 -0400820 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400821
Holger Schurig10078322007-11-15 18:05:47 -0500822 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400823 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400824 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500825 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400826 goto done;
827 }
828
Dan Williams954ee162007-08-20 11:43:25 -0400829 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
830 goto release_fw;
831
832restart:
833 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
834 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
835 ret = -1;
836 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400837 }
838
Dan Williams954ee162007-08-20 11:43:25 -0400839 cardp->bootcmdresp = 0;
840 do {
841 int j = 0;
842 i++;
843 /* Issue Boot command = 1, Boot from Download-FW */
844 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
845 /* wait for command response */
846 do {
847 j++;
848 msleep_interruptible(100);
849 } while (cardp->bootcmdresp == 0 && j < 10);
850 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400851
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000852 if (cardp->bootcmdresp <= 0) {
Dan Williams954ee162007-08-20 11:43:25 -0400853 if (--reset_count >= 0) {
854 if_usb_reset_device(cardp);
855 goto restart;
856 }
857 return -1;
858 }
859
860 i = 0;
861
862 cardp->totalbytes = 0;
863 cardp->fwlastblksent = 0;
864 cardp->CRC_OK = 1;
865 cardp->fwdnldover = 0;
866 cardp->fwseqnum = -1;
867 cardp->totalbytes = 0;
868 cardp->fwfinalblk = 0;
869
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500870 /* Send the first firmware packet... */
871 if_usb_send_fw_pkt(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400872
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500873 /* ... and wait for the process to complete */
874 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
David Woodhouse7e226272007-12-14 22:53:41 -0500875
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500876 del_timer_sync(&cardp->fw_timeout);
David Woodhousec9cd6f92007-12-11 13:15:25 -0500877 usb_kill_urb(cardp->rx_urb);
Dan Williams954ee162007-08-20 11:43:25 -0400878
879 if (!cardp->fwdnldover) {
880 lbs_pr_info("failed to load fw, resetting device!\n");
881 if (--reset_count >= 0) {
882 if_usb_reset_device(cardp);
883 goto restart;
884 }
885
886 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
887 ret = -1;
888 goto release_fw;
889 }
890
David Woodhouseeae86bf2007-12-14 00:47:05 -0500891 release_fw:
Dan Williams954ee162007-08-20 11:43:25 -0400892 release_firmware(cardp->fw);
893 cardp->fw = NULL;
894
David Woodhouseeae86bf2007-12-14 00:47:05 -0500895 done:
Dan Williams954ee162007-08-20 11:43:25 -0400896 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400897 return ret;
898}
899
900
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200901#ifdef CONFIG_PM
902static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
903{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500904 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100905 struct lbs_private *priv = cardp->priv;
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500906 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200907
Holger Schurig9012b282007-05-25 11:27:16 -0400908 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200909
David Woodhouseaa21c002007-12-08 20:04:36 +0000910 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200911 return -1;
912
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500913 ret = lbs_suspend(priv);
914 if (ret)
915 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200916
917 /* Unlink tx & rx urb */
918 usb_kill_urb(cardp->tx_urb);
919 usb_kill_urb(cardp->rx_urb);
920
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500921 out:
Holger Schurig9012b282007-05-25 11:27:16 -0400922 lbs_deb_leave(LBS_DEB_USB);
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500923 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200924}
925
926static int if_usb_resume(struct usb_interface *intf)
927{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500928 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100929 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200930
Holger Schurig9012b282007-05-25 11:27:16 -0400931 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200932
David Woodhouse6bc822b2007-12-11 12:53:43 -0500933 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200934
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500935 lbs_resume(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200936
Holger Schurig9012b282007-05-25 11:27:16 -0400937 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200938 return 0;
939}
940#else
941#define if_usb_suspend NULL
942#define if_usb_resume NULL
943#endif
944
945static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -0500946 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200947 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200948 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200949 .id_table = if_usb_table,
950 .suspend = if_usb_suspend,
951 .resume = if_usb_resume,
952};
953
Andres Salomon4fb910f2007-11-20 17:43:45 -0500954static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200955{
Holger Schurig084708b2007-05-25 12:37:58 -0400956 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200957
Holger Schurig084708b2007-05-25 12:37:58 -0400958 lbs_deb_enter(LBS_DEB_MAIN);
959
Holger Schurig084708b2007-05-25 12:37:58 -0400960 ret = usb_register(&if_usb_driver);
961
962 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
963 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200964}
965
Andres Salomon4fb910f2007-11-20 17:43:45 -0500966static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200967{
Holger Schurig084708b2007-05-25 12:37:58 -0400968 lbs_deb_enter(LBS_DEB_MAIN);
969
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200970 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -0400971
972 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200973}
Holger Schurig084708b2007-05-25 12:37:58 -0400974
975module_init(if_usb_init_module);
976module_exit(if_usb_exit_module);
977
978MODULE_DESCRIPTION("8388 USB WLAN Driver");
David Woodhouseeae86bf2007-12-14 00:47:05 -0500979MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
Holger Schurig084708b2007-05-25 12:37:58 -0400980MODULE_LICENSE("GPL");