blob: 3931fe6267ce8e8e568fdb3b0083c10a53818517 [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 Woodhouse46949612007-12-17 14:42:33 -0500105static void if_usb_setup_firmware(struct lbs_private *priv)
David Woodhouse04c80f12007-12-06 12:51:00 +0000106{
107 struct cmd_ds_set_boot2_ver b2_cmd;
David Woodhouse46949612007-12-17 14:42:33 -0500108 struct cmd_ds_802_11_fw_wake_method wake_method;
David Woodhouse04c80f12007-12-06 12:51:00 +0000109
David Woodhouse9fae8992007-12-15 03:46:44 -0500110 b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
David Woodhouse04c80f12007-12-06 12:51:00 +0000111 b2_cmd.action = 0;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000112 b2_cmd.version = priv->boot2_version;
David Woodhouse04c80f12007-12-06 12:51:00 +0000113
David Woodhouseb23b2062007-12-15 01:22:09 -0500114 if (lbs_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
David Woodhouse04c80f12007-12-06 12:51:00 +0000115 lbs_deb_usb("Setting boot2 version failed\n");
David Woodhouse46949612007-12-17 14:42:33 -0500116
117 priv->wol_gpio = 2; /* Wake via GPIO2... */
118 priv->wol_gap = 20; /* ... after 20ms */
119 lbs_host_sleep_cfg(priv, EHS_WAKE_ON_UNICAST_DATA);
120
121 wake_method.hdr.size = cpu_to_le16(sizeof(wake_method));
122 wake_method.action = cpu_to_le16(CMD_ACT_GET);
123 if (lbs_cmd_with_response(priv, CMD_802_11_FW_WAKE_METHOD, &wake_method)) {
124 lbs_pr_info("Firmware does not seem to support PS mode\n");
125 } else {
126 if (le16_to_cpu(wake_method.method) == CMD_WAKE_METHOD_COMMAND_INT) {
127 lbs_deb_usb("Firmware seems to support PS with wake-via-command\n");
128 priv->ps_supported = 1;
129 } else {
130 /* The versions which boot up this way don't seem to
131 work even if we set it to the command interrupt */
132 lbs_pr_info("Firmware doesn't wake via command interrupt; disabling PS mode\n");
133 }
134 }
David Woodhouse04c80f12007-12-06 12:51:00 +0000135}
136
David Woodhouse2fd6cfe2007-12-11 17:44:10 -0500137static void if_usb_fw_timeo(unsigned long priv)
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500138{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500139 struct if_usb_card *cardp = (void *)priv;
David Woodhouse04c80f12007-12-06 12:51:00 +0000140
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500141 if (cardp->fwdnldover) {
142 lbs_deb_usb("Download complete, no event. Assuming success\n");
143 } else {
144 lbs_pr_err("Download timed out\n");
145 cardp->surprise_removed = 1;
146 }
147 wake_up(&cardp->fw_wq);
148}
David Woodhouseeae86bf2007-12-14 00:47:05 -0500149
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200150/**
151 * @brief sets the configuration values
152 * @param ifnum interface number
153 * @param id pointer to usb_device_id
154 * @return 0 on success, error code on failure
155 */
156static int if_usb_probe(struct usb_interface *intf,
157 const struct usb_device_id *id)
158{
159 struct usb_device *udev;
160 struct usb_host_interface *iface_desc;
161 struct usb_endpoint_descriptor *endpoint;
Holger Schurig69f90322007-11-23 15:43:44 +0100162 struct lbs_private *priv;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500163 struct if_usb_card *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200164 int i;
165
166 udev = interface_to_usbdev(intf);
167
David Woodhouseeae86bf2007-12-14 00:47:05 -0500168 cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400169 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200170 lbs_pr_err("Out of memory allocating private data.\n");
171 goto error;
172 }
173
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500174 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
175 init_waitqueue_head(&cardp->fw_wq);
David Woodhouse7e226272007-12-14 22:53:41 -0500176
Holger Schurig435a1ac2007-05-25 12:41:52 -0400177 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200178 iface_desc = intf->cur_altsetting;
179
Holger Schurig9012b282007-05-25 11:27:16 -0400180 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
David Woodhouseeae86bf2007-12-14 00:47:05 -0500181 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400182 le16_to_cpu(udev->descriptor.bcdUSB),
183 udev->descriptor.bDeviceClass,
184 udev->descriptor.bDeviceSubClass,
185 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200186
187 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
188 endpoint = &iface_desc->endpoint[i].desc;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500189 if (usb_endpoint_is_bulk_in(endpoint)) {
190 cardp->ep_in_size = le16_to_cpu(endpoint->wMaxPacketSize);
191 cardp->ep_in = usb_endpoint_num(endpoint);
192
193 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n", cardp->ep_in);
194 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n", cardp->ep_in_size);
195
196 } else if (usb_endpoint_is_bulk_out(endpoint)) {
197 cardp->ep_out_size = le16_to_cpu(endpoint->wMaxPacketSize);
198 cardp->ep_out = usb_endpoint_num(endpoint);
199
200 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n", cardp->ep_out);
201 lbs_deb_usbd(&udev->dev, "Bulk out size is %d\n", cardp->ep_out_size);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200202 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500203 }
204 if (!cardp->ep_out_size || !cardp->ep_in_size) {
205 lbs_deb_usbd(&udev->dev, "Endpoints not found\n");
206 goto dealloc;
207 }
208 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
209 lbs_deb_usbd(&udev->dev, "Rx URB allocation failed\n");
210 goto dealloc;
211 }
212 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
213 lbs_deb_usbd(&udev->dev, "Tx URB allocation failed\n");
214 goto dealloc;
215 }
216 cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE, GFP_KERNEL);
217 if (!cardp->ep_out_buf) {
218 lbs_deb_usbd(&udev->dev, "Could not allocate buffer\n");
219 goto dealloc;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200220 }
221
Dan Williams954ee162007-08-20 11:43:25 -0400222 /* Upload firmware */
Dan Williams954ee162007-08-20 11:43:25 -0400223 if (if_usb_prog_firmware(cardp)) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500224 lbs_deb_usbd(&udev->dev, "FW upload failed\n");
Dan Williams954ee162007-08-20 11:43:25 -0400225 goto err_prog_firmware;
226 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400227
Holger Schurig10078322007-11-15 18:05:47 -0500228 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400229 goto err_prog_firmware;
230
231 cardp->priv = priv;
David Woodhousec9cd6f92007-12-11 13:15:25 -0500232 cardp->priv->fw_ready = 1;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400233
Holger Schurig208fdd22007-05-25 12:17:06 -0400234 priv->hw_host_to_card = if_usb_host_to_card;
235 priv->hw_get_int_status = if_usb_get_int_status;
236 priv->hw_read_event_cause = if_usb_read_event_cause;
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000237 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400238
Dan Williams954ee162007-08-20 11:43:25 -0400239 if_usb_submit_rx_urb(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400240
Holger Schurig10078322007-11-15 18:05:47 -0500241 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400242 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400243
David Woodhouse46949612007-12-17 14:42:33 -0500244 if_usb_setup_firmware(priv);
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500245
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200246 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400247 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200248
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200249 return 0;
250
Dan Williams954ee162007-08-20 11:43:25 -0400251err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500252 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400253err_prog_firmware:
254 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200255dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400256 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200257
258error:
259 return -ENOMEM;
260}
261
262/**
263 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400264 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200265 * @return N/A
266 */
267static void if_usb_disconnect(struct usb_interface *intf)
268{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500269 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100270 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271
Dan Williams954ee162007-08-20 11:43:25 -0400272 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200273
Dan Williams954ee162007-08-20 11:43:25 -0400274 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200275
Dan Williams954ee162007-08-20 11:43:25 -0400276 if (priv) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000277 priv->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500278 lbs_stop_card(priv);
Holger Schurig10078322007-11-15 18:05:47 -0500279 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400280 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200281
282 /* Unlink and free urb */
283 if_usb_free(cardp);
284
285 usb_set_intfdata(intf, NULL);
286 usb_put_dev(interface_to_usbdev(intf));
287
Dan Williams954ee162007-08-20 11:43:25 -0400288 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200289}
290
291/**
292 * @brief This function download FW
Holger Schurig69f90322007-11-23 15:43:44 +0100293 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200294 * @return 0
295 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500296static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200297{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500298 struct fwdata *fwdata = cardp->ep_out_buf;
299 uint8_t *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200300
David Woodhouseeae86bf2007-12-14 00:47:05 -0500301 /* If we got a CRC failure on the last block, back
302 up and retry it */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200303 if (!cardp->CRC_OK) {
304 cardp->totalbytes = cardp->fwlastblksent;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500305 cardp->fwseqnum--;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200306 }
307
David Woodhouseeae86bf2007-12-14 00:47:05 -0500308 lbs_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
309 cardp->totalbytes);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200310
David Woodhouseeae86bf2007-12-14 00:47:05 -0500311 /* struct fwdata (which we sent to the card) has an
312 extra __le32 field in between the header and the data,
313 which is not in the struct fwheader in the actual
314 firmware binary. Insert the seqnum in the middle... */
315 memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200316 sizeof(struct fwheader));
317
318 cardp->fwlastblksent = cardp->totalbytes;
319 cardp->totalbytes += sizeof(struct fwheader);
320
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200321 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouseeae86bf2007-12-14 00:47:05 -0500322 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200323
David Woodhouseeae86bf2007-12-14 00:47:05 -0500324 lbs_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
325 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200326
David Woodhouseeae86bf2007-12-14 00:47:05 -0500327 fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
328 cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200329
David Woodhouseeae86bf2007-12-14 00:47:05 -0500330 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
331 le32_to_cpu(fwdata->hdr.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200332
David Woodhouseeae86bf2007-12-14 00:47:05 -0500333 if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
334 lbs_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
335 lbs_deb_usb2(&cardp->udev->dev, "seqnum = %d totalbytes = %d\n",
336 cardp->fwseqnum, cardp->totalbytes);
337 } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
338 lbs_deb_usb2(&cardp->udev->dev, "Host has finished FW downloading\n");
339 lbs_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200340
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200341 cardp->fwfinalblk = 1;
342 }
343
David Woodhouseeae86bf2007-12-14 00:47:05 -0500344 lbs_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
345 cardp->totalbytes);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200346
347 return 0;
348}
349
David Woodhouseeae86bf2007-12-14 00:47:05 -0500350static int if_usb_reset_device(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200351{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500352 struct cmd_ds_command *cmd = cardp->ep_out_buf + 4;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200353 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200354
Holger Schurig3874d0f2007-05-25 12:01:42 -0400355 lbs_deb_enter(LBS_DEB_USB);
356
David Woodhouseeae86bf2007-12-14 00:47:05 -0500357 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000358
359 cmd->command = cpu_to_le16(CMD_802_11_RESET);
360 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
361 cmd->result = cpu_to_le16(0);
362 cmd->seqnum = cpu_to_le16(0x5a5a);
363 cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500364 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 +0000365
David Woodhousec8ba39d2007-12-10 18:53:34 -0500366 msleep(100);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200367 ret = usb_reset_device(cardp->udev);
David Woodhousec8ba39d2007-12-10 18:53:34 -0500368 msleep(100);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400369
370 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
371
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200372 return ret;
373}
374
375/**
376 * @brief This function transfer the data to the device.
Holger Schurig69f90322007-11-23 15:43:44 +0100377 * @param priv pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200378 * @param payload pointer to payload data
379 * @param nb data length
380 * @return 0 or -1
381 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500382static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, uint16_t nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200383{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200384 int ret = -1;
385
386 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400387 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400388 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200389 goto tx_ret;
390 }
391
392 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
393 usb_sndbulkpipe(cardp->udev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500394 cardp->ep_out),
Dan Williams954ee162007-08-20 11:43:25 -0400395 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200396
397 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
398
399 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
David Woodhouse0856e682007-12-07 15:12:26 +0000400 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200401 ret = -1;
402 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500403 lbs_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200404 ret = 0;
405 }
406
407tx_ret:
408 return ret;
409}
410
David Woodhouseeae86bf2007-12-14 00:47:05 -0500411static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
Dan Williams954ee162007-08-20 11:43:25 -0400412 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200413{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200414 struct sk_buff *skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200415 int ret = -1;
416
417 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
418 lbs_pr_err("No free skb\n");
419 goto rx_ret;
420 }
421
David Woodhouseeae86bf2007-12-14 00:47:05 -0500422 cardp->rx_skb = skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200423
424 /* Fill the receive configuration URB and initialise the Rx call back */
425 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500426 usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
Dan Williams4269e2a2007-05-10 23:10:18 -0400427 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200428 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500429 cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200430
431 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
432
David Woodhouseeae86bf2007-12-14 00:47:05 -0500433 lbs_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200434 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
David Woodhouse0856e682007-12-07 15:12:26 +0000435 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000436 kfree_skb(skb);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500437 cardp->rx_skb = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438 ret = -1;
439 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500440 lbs_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200441 ret = 0;
442 }
443
444rx_ret:
445 return ret;
446}
447
David Woodhouseeae86bf2007-12-14 00:47:05 -0500448static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200449{
Dan Williams954ee162007-08-20 11:43:25 -0400450 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451}
452
David Woodhouseeae86bf2007-12-14 00:47:05 -0500453static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200454{
Dan Williams954ee162007-08-20 11:43:25 -0400455 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456}
457
458static void if_usb_receive_fwload(struct urb *urb)
459{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500460 struct if_usb_card *cardp = urb->context;
461 struct sk_buff *skb = cardp->rx_skb;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200462 struct fwsyncheader *syncfwheader;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500463 struct bootcmdresp bootcmdresp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200464
465 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400466 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500467 "URB status is failed during fw load\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200468 kfree_skb(skb);
469 return;
470 }
471
David Woodhousec9cd6f92007-12-11 13:15:25 -0500472 if (cardp->fwdnldover) {
473 __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
474
475 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
476 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
477 lbs_pr_info("Firmware ready event received\n");
478 wake_up(&cardp->fw_wq);
479 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500480 lbs_deb_usb("Waiting for confirmation; got %x %x\n",
481 le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
David Woodhousec9cd6f92007-12-11 13:15:25 -0500482 if_usb_submit_rx_urb_fwload(cardp);
483 }
484 kfree_skb(skb);
485 return;
486 }
David Woodhousec8ba39d2007-12-10 18:53:34 -0500487 if (cardp->bootcmdresp <= 0) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200488 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
489 sizeof(bootcmdresp));
David Woodhouseeae86bf2007-12-14 00:47:05 -0500490
David Woodhouse981f1872007-05-25 23:36:54 -0400491 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200492 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400493 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200494 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400495 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500496 "Received valid boot command response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200497 return;
498 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500499 if (bootcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
500 if (bootcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
501 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
502 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
David Woodhousec9cd6f92007-12-11 13:15:25 -0500503 if (!cardp->bootcmdresp)
504 lbs_pr_info("Firmware already seems alive; resetting\n");
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000505 cardp->bootcmdresp = -1;
506 } else {
507 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
David Woodhouseeae86bf2007-12-14 00:47:05 -0500508 le32_to_cpu(bootcmdresp.magic));
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000509 }
David Woodhouseeae86bf2007-12-14 00:47:05 -0500510 } else if (bootcmdresp.cmd != BOOT_CMD_FW_BY_USB) {
511 lbs_pr_info("boot cmd response cmd_tag error (%d)\n",
512 bootcmdresp.cmd);
513 } else if (bootcmdresp.result != BOOT_CMD_RESP_OK) {
514 lbs_pr_info("boot cmd response result error (%d)\n",
515 bootcmdresp.result);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200516 } else {
517 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400518 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500519 "Received valid boot command response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200520 }
521 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400522 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200523 return;
524 }
525
526 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
527 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400528 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200529 kfree_skb(skb);
530 return;
531 }
532
533 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500534 sizeof(struct fwsyncheader));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200535
536 if (!syncfwheader->cmd) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500537 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk with correct CRC\n");
538 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk seqnum = %d\n",
539 le32_to_cpu(syncfwheader->seqnum));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200540 cardp->CRC_OK = 1;
541 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500542 lbs_deb_usbd(&cardp->udev->dev, "FW received Blk with CRC error\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200543 cardp->CRC_OK = 0;
544 }
545
546 kfree_skb(skb);
547
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500548 /* reschedule timer for 200ms hence */
549 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
550
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200551 if (cardp->fwfinalblk) {
552 cardp->fwdnldover = 1;
553 goto exit;
554 }
555
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500556 if_usb_send_fw_pkt(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200557
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500558 exit:
David Woodhousec9cd6f92007-12-11 13:15:25 -0500559 if_usb_submit_rx_urb_fwload(cardp);
560
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200561 kfree(syncfwheader);
562
563 return;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200564}
565
566#define MRVDRV_MIN_PKT_LEN 30
567
568static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500569 struct if_usb_card *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100570 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200571{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500572 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
573 || recvlength < MRVDRV_MIN_PKT_LEN) {
574 lbs_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200575 kfree_skb(skb);
576 return;
577 }
578
579 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
580 skb_put(skb, recvlength);
581 skb_pull(skb, MESSAGE_HEADER_LEN);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500582
Holger Schurig10078322007-11-15 18:05:47 -0500583 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400584 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200585}
586
David Woodhouseeae86bf2007-12-14 00:47:05 -0500587static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200588 struct sk_buff *skb,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500589 struct if_usb_card *cardp,
Holger Schurig69f90322007-11-23 15:43:44 +0100590 struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200591{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500592 uint8_t *cmdbuf;
593
Dan Williamsddac4522007-12-11 13:49:39 -0500594 if (recvlength > LBS_CMD_BUFFER_SIZE) {
Holger Schurig9012b282007-05-25 11:27:16 -0400595 lbs_deb_usbd(&cardp->udev->dev,
David Woodhouseeae86bf2007-12-14 00:47:05 -0500596 "The receive buffer is too large\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200597 kfree_skb(skb);
598 return;
599 }
600
601 if (!in_interrupt())
602 BUG();
603
David Woodhouseaa21c002007-12-08 20:04:36 +0000604 spin_lock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200605 /* take care of cur_cmd = NULL case by reading the
606 * data to clear the interrupt */
David Woodhouseaa21c002007-12-08 20:04:36 +0000607 if (!priv->cur_cmd) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500608 lbs_deb_hex(LBS_DEB_HOST, "Unsolicited CMD_RESP",
609 (void *) recvbuff + MESSAGE_HEADER_LEN, priv->upld_len);
Holger Schurig634b8f42007-05-25 13:05:16 -0400610 cmdbuf = priv->upld_buf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000611 priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200612 } else
David Woodhouseeae86bf2007-12-14 00:47:05 -0500613 cmdbuf = (uint8_t *) priv->cur_cmd->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200614
Holger Schurigc95c7f92007-08-02 11:49:45 -0400615 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400616 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500617 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN, priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200618
619 kfree_skb(skb);
David Woodhouse4f679492007-12-10 14:58:37 -0500620 lbs_interrupt(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000621 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200622
Holger Schurig9012b282007-05-25 11:27:16 -0400623 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200624 "Wake up main thread to handle cmd response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200625}
626
627/**
628 * @brief This function reads of the packet into the upload buff,
629 * wake up the main thread and initialise the Rx callack.
630 *
631 * @param urb pointer to struct urb
632 * @return N/A
633 */
634static void if_usb_receive(struct urb *urb)
635{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500636 struct if_usb_card *cardp = urb->context;
637 struct sk_buff *skb = cardp->rx_skb;
Holger Schurig69f90322007-11-23 15:43:44 +0100638 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200639 int recvlength = urb->actual_length;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500640 uint8_t *recvbuff = NULL;
641 uint32_t recvtype = 0;
642 __le32 *pkt = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200643
Holger Schurig9012b282007-05-25 11:27:16 -0400644 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200645
646 if (recvlength) {
647 if (urb->status) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500648 lbs_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
649 urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200650 kfree_skb(skb);
651 goto setup_for_next;
652 }
653
654 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
David Woodhouseeae86bf2007-12-14 00:47:05 -0500655 recvtype = le32_to_cpu(pkt[0]);
Holger Schurig9012b282007-05-25 11:27:16 -0400656 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400657 "Recv length = 0x%x, Recv type = 0x%X\n",
658 recvlength, recvtype);
David Woodhouse77d8cf22007-11-28 16:20:51 +0000659 } else if (urb->status) {
660 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200661 goto rx_exit;
David Woodhouse77d8cf22007-11-28 16:20:51 +0000662 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200664 switch (recvtype) {
665 case CMD_TYPE_DATA:
666 process_cmdtypedata(recvlength, skb, cardp, priv);
667 break;
668
669 case CMD_TYPE_REQUEST:
670 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
671 break;
672
673 case CMD_TYPE_INDICATION:
674 /* Event cause handling */
David Woodhouseaa21c002007-12-08 20:04:36 +0000675 spin_lock(&priv->driver_lock);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500676
677 cardp->usb_event_cause = le32_to_cpu(pkt[1]);
678
Holger Schurig9012b282007-05-25 11:27:16 -0400679 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
David Woodhouseeae86bf2007-12-14 00:47:05 -0500680 cardp->usb_event_cause);
681
682 /* Icky undocumented magic special case */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500684 lbs_send_tx_feedback(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000685 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200686 break;
687 }
David Woodhouse981f1872007-05-25 23:36:54 -0400688 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400689 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200690 kfree_skb(skb);
David Woodhouse4f679492007-12-10 14:58:37 -0500691 lbs_interrupt(priv);
David Woodhouseaa21c002007-12-08 20:04:36 +0000692 spin_unlock(&priv->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200693 goto rx_exit;
694 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400695 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
David Woodhouseeae86bf2007-12-14 00:47:05 -0500696 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697 kfree_skb(skb);
698 break;
699 }
700
701setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400702 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400704 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200705}
706
707/**
708 * @brief This function downloads data to FW
Holger Schurig69f90322007-11-23 15:43:44 +0100709 * @param priv pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710 * @param type type of data
711 * @param buf pointer to data buffer
712 * @param len number of bytes
713 * @return 0 or -1
714 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500715static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
716 uint8_t *payload, uint16_t nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200717{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500718 struct if_usb_card *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719
Holger Schurig9012b282007-05-25 11:27:16 -0400720 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
721 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722
723 if (type == MVMS_CMD) {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500724 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400725 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200726 } else {
David Woodhouseeae86bf2007-12-14 00:47:05 -0500727 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400728 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729 }
730
David Woodhouseeae86bf2007-12-14 00:47:05 -0500731 memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200732
David Woodhouseeae86bf2007-12-14 00:47:05 -0500733 return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200734}
735
David Woodhouseaa21c002007-12-08 20:04:36 +0000736/* called with priv->driver_lock held */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500737static int if_usb_get_int_status(struct lbs_private *priv, uint8_t *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200738{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500739 struct if_usb_card *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200740
741 *ireg = cardp->usb_int_cause;
742 cardp->usb_int_cause = 0;
743
David Woodhouseeae86bf2007-12-14 00:47:05 -0500744 lbs_deb_usbd(&cardp->udev->dev, "Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200745
746 return 0;
747}
748
Holger Schurig69f90322007-11-23 15:43:44 +0100749static int if_usb_read_event_cause(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200750{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500751 struct if_usb_card *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400752
David Woodhouseaa21c002007-12-08 20:04:36 +0000753 priv->eventcause = cardp->usb_event_cause;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200754 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400755 if_usb_submit_rx_urb(cardp);
David Woodhouseeae86bf2007-12-14 00:47:05 -0500756
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200757 return 0;
758}
759
Dan Williams9e22cb62007-08-02 11:14:49 -0400760/**
761 * @brief This function issues Boot command to the Boot2 code
762 * @param ivalue 1:Boot from FW by USB-Download
763 * 2:Boot from FW in EEPROM
764 * @return 0
765 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500766static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400767{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500768 struct bootcmd *bootcmd = cardp->ep_out_buf;
Dan Williams9e22cb62007-08-02 11:14:49 -0400769
770 /* Prepare command */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500771 bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
772 bootcmd->cmd = ivalue;
773 memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
Dan Williams9e22cb62007-08-02 11:14:49 -0400774
775 /* Issue command */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500776 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd));
Dan Williams9e22cb62007-08-02 11:14:49 -0400777
778 return 0;
779}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200780
781
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400782/**
783 * @brief This function checks the validity of Boot2/FW image.
784 *
785 * @param data pointer to image
786 * len image length
787 * @return 0 or -1
788 */
David Woodhouseeae86bf2007-12-14 00:47:05 -0500789static int check_fwfile_format(uint8_t *data, uint32_t totlen)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400790{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500791 uint32_t bincmd, exit;
792 uint32_t blksize, offset, len;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400793 int ret;
794
795 ret = 1;
796 exit = len = 0;
797
798 do {
799 struct fwheader *fwh = (void *)data;
800
801 bincmd = le32_to_cpu(fwh->dnldcmd);
802 blksize = le32_to_cpu(fwh->datalength);
803 switch (bincmd) {
804 case FW_HAS_DATA_TO_RECV:
805 offset = sizeof(struct fwheader) + blksize;
806 data += offset;
807 len += offset;
808 if (len >= totlen)
809 exit = 1;
810 break;
811 case FW_HAS_LAST_BLOCK:
812 exit = 1;
813 ret = 0;
814 break;
815 default:
816 exit = 1;
817 break;
818 }
819 } while (!exit);
820
821 if (ret)
822 lbs_pr_err("firmware file format check FAIL\n");
823 else
824 lbs_deb_fw("firmware file format check PASS\n");
825
826 return ret;
827}
828
829
David Woodhouseeae86bf2007-12-14 00:47:05 -0500830static int if_usb_prog_firmware(struct if_usb_card *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400831{
Dan Williams954ee162007-08-20 11:43:25 -0400832 int i = 0;
833 static int reset_count = 10;
834 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400835
Dan Williams954ee162007-08-20 11:43:25 -0400836 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400837
Holger Schurig10078322007-11-15 18:05:47 -0500838 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400839 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400840 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500841 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400842 goto done;
843 }
844
Dan Williams954ee162007-08-20 11:43:25 -0400845 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
846 goto release_fw;
847
848restart:
849 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
850 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
851 ret = -1;
852 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400853 }
854
Dan Williams954ee162007-08-20 11:43:25 -0400855 cardp->bootcmdresp = 0;
856 do {
857 int j = 0;
858 i++;
859 /* Issue Boot command = 1, Boot from Download-FW */
860 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
861 /* wait for command response */
862 do {
863 j++;
864 msleep_interruptible(100);
865 } while (cardp->bootcmdresp == 0 && j < 10);
866 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400867
David Woodhouse6d35fdf2007-12-08 23:49:06 +0000868 if (cardp->bootcmdresp <= 0) {
Dan Williams954ee162007-08-20 11:43:25 -0400869 if (--reset_count >= 0) {
870 if_usb_reset_device(cardp);
871 goto restart;
872 }
873 return -1;
874 }
875
876 i = 0;
877
878 cardp->totalbytes = 0;
879 cardp->fwlastblksent = 0;
880 cardp->CRC_OK = 1;
881 cardp->fwdnldover = 0;
882 cardp->fwseqnum = -1;
883 cardp->totalbytes = 0;
884 cardp->fwfinalblk = 0;
885
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500886 /* Send the first firmware packet... */
887 if_usb_send_fw_pkt(cardp);
Dan Williams954ee162007-08-20 11:43:25 -0400888
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500889 /* ... and wait for the process to complete */
890 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
David Woodhouse7e226272007-12-14 22:53:41 -0500891
David Woodhouse4f82f5c2007-12-11 00:07:58 -0500892 del_timer_sync(&cardp->fw_timeout);
David Woodhousec9cd6f92007-12-11 13:15:25 -0500893 usb_kill_urb(cardp->rx_urb);
Dan Williams954ee162007-08-20 11:43:25 -0400894
895 if (!cardp->fwdnldover) {
896 lbs_pr_info("failed to load fw, resetting device!\n");
897 if (--reset_count >= 0) {
898 if_usb_reset_device(cardp);
899 goto restart;
900 }
901
902 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
903 ret = -1;
904 goto release_fw;
905 }
906
David Woodhouseeae86bf2007-12-14 00:47:05 -0500907 release_fw:
Dan Williams954ee162007-08-20 11:43:25 -0400908 release_firmware(cardp->fw);
909 cardp->fw = NULL;
910
David Woodhouseeae86bf2007-12-14 00:47:05 -0500911 done:
Dan Williams954ee162007-08-20 11:43:25 -0400912 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400913 return ret;
914}
915
916
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200917#ifdef CONFIG_PM
918static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
919{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500920 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100921 struct lbs_private *priv = cardp->priv;
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500922 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200923
Holger Schurig9012b282007-05-25 11:27:16 -0400924 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200925
David Woodhouseaa21c002007-12-08 20:04:36 +0000926 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200927 return -1;
928
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500929 ret = lbs_suspend(priv);
930 if (ret)
931 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200932
933 /* Unlink tx & rx urb */
934 usb_kill_urb(cardp->tx_urb);
935 usb_kill_urb(cardp->rx_urb);
936
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500937 out:
Holger Schurig9012b282007-05-25 11:27:16 -0400938 lbs_deb_leave(LBS_DEB_USB);
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500939 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200940}
941
942static int if_usb_resume(struct usb_interface *intf)
943{
David Woodhouseeae86bf2007-12-14 00:47:05 -0500944 struct if_usb_card *cardp = usb_get_intfdata(intf);
Holger Schurig69f90322007-11-23 15:43:44 +0100945 struct lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200946
Holger Schurig9012b282007-05-25 11:27:16 -0400947 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200948
David Woodhouse6bc822b2007-12-11 12:53:43 -0500949 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200950
David Woodhoused1f7a5b2007-12-12 17:40:56 -0500951 lbs_resume(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200952
Holger Schurig9012b282007-05-25 11:27:16 -0400953 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200954 return 0;
955}
956#else
957#define if_usb_suspend NULL
958#define if_usb_resume NULL
959#endif
960
961static struct usb_driver if_usb_driver = {
Andres Salomon798fbfe2007-11-20 17:44:04 -0500962 .name = DRV_NAME,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200963 .probe = if_usb_probe,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200964 .disconnect = if_usb_disconnect,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200965 .id_table = if_usb_table,
966 .suspend = if_usb_suspend,
967 .resume = if_usb_resume,
968};
969
Andres Salomon4fb910f2007-11-20 17:43:45 -0500970static int __init if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200971{
Holger Schurig084708b2007-05-25 12:37:58 -0400972 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200973
Holger Schurig084708b2007-05-25 12:37:58 -0400974 lbs_deb_enter(LBS_DEB_MAIN);
975
Holger Schurig084708b2007-05-25 12:37:58 -0400976 ret = usb_register(&if_usb_driver);
977
978 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
979 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200980}
981
Andres Salomon4fb910f2007-11-20 17:43:45 -0500982static void __exit if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200983{
Holger Schurig084708b2007-05-25 12:37:58 -0400984 lbs_deb_enter(LBS_DEB_MAIN);
985
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200986 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -0400987
988 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200989}
Holger Schurig084708b2007-05-25 12:37:58 -0400990
991module_init(if_usb_init_module);
992module_exit(if_usb_exit_module);
993
994MODULE_DESCRIPTION("8388 USB WLAN Driver");
David Woodhouseeae86bf2007-12-14 00:47:05 -0500995MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
Holger Schurig084708b2007-05-25 12:37:58 -0400996MODULE_LICENSE("GPL");