blob: 797d3fbf7154e4cc2ee1563257988ff02a58fa00 [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>
Holger Schurig435a1ac2007-05-25 12:41:52 -04008#include <linux/list.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02009#include <linux/usb.h>
10
Holger Schurigec3eef22007-05-25 12:49:10 -040011#define DRV_NAME "usb8xxx"
12
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020013#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014#include "decl.h"
15#include "defs.h"
16#include "dev.h"
17#include "if_usb.h"
18
19#define MESSAGE_HEADER_LEN 4
20
21static const char usbdriver_name[] = "usb8xxx";
Holger Schurig084708b2007-05-25 12:37:58 -040022static u8 *default_fw_name = "usb8388.bin";
23
Holger Schurig10078322007-11-15 18:05:47 -050024static char *lbs_fw_name;
25module_param_named(fw_name, lbs_fw_name, charp, 0644);
Holger Schurig084708b2007-05-25 12:37:58 -040026
Holger Schurig435a1ac2007-05-25 12:41:52 -040027/*
28 * We need to send a RESET command to all USB devices before
29 * we tear down the USB connection. Otherwise we would not
30 * be able to re-init device the device if the module gets
31 * loaded again. This is a list of all initialized USB devices,
32 * for the reset code see if_usb_reset_device()
33*/
34static LIST_HEAD(usb_devices);
Holger Schurig3874d0f2007-05-25 12:01:42 -040035
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020036static struct usb_device_id if_usb_table[] = {
37 /* Enter the device signature inside */
Holger Schurig66fcc552007-05-25 00:11:58 -040038 { USB_DEVICE(0x1286, 0x2001) },
39 { USB_DEVICE(0x05a3, 0x8388) },
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020040 {} /* Terminating entry */
41};
42
43MODULE_DEVICE_TABLE(usb, if_usb_table);
44
45static void if_usb_receive(struct urb *urb);
46static void if_usb_receive_fwload(struct urb *urb);
Dan Williams954ee162007-08-20 11:43:25 -040047static int if_usb_prog_firmware(struct usb_card_rec *cardp);
Holger Schurig10078322007-11-15 18:05:47 -050048static int if_usb_host_to_card(lbs_private *priv, u8 type, u8 *payload, u16 nb);
49static int if_usb_get_int_status(lbs_private *priv, u8 *);
50static int if_usb_read_event_cause(lbs_private *);
Dan Williams954ee162007-08-20 11:43:25 -040051static int usb_tx_block(struct usb_card_rec *cardp, u8 *payload, u16 nb);
Holger Schurigac558ca2007-08-02 11:49:06 -040052static void if_usb_free(struct usb_card_rec *cardp);
Dan Williams954ee162007-08-20 11:43:25 -040053static int if_usb_submit_rx_urb(struct usb_card_rec *cardp);
54static int if_usb_reset_device(struct usb_card_rec *cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020055
56/**
57 * @brief call back function to handle the status of the URB
58 * @param urb pointer to urb structure
59 * @return N/A
60 */
61static void if_usb_write_bulk_callback(struct urb *urb)
62{
Dan Williams954ee162007-08-20 11:43:25 -040063 struct usb_card_rec *cardp = (struct usb_card_rec *) urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020064
65 /* handle the transmission complete validations */
66
Dan Williams954ee162007-08-20 11:43:25 -040067 if (urb->status == 0) {
Holger Schurig10078322007-11-15 18:05:47 -050068 lbs_private *priv = cardp->priv;
Dan Williams954ee162007-08-20 11:43:25 -040069
Holger Schurig9012b282007-05-25 11:27:16 -040070 /*
71 lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
72 lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020073 urb->actual_length);
Holger Schurig9012b282007-05-25 11:27:16 -040074 */
Dan Williams954ee162007-08-20 11:43:25 -040075
76 /* Used for both firmware TX and regular TX. priv isn't
77 * valid at firmware load time.
78 */
79 if (priv) {
Holger Schurig10078322007-11-15 18:05:47 -050080 lbs_adapter *adapter = priv->adapter;
Dan Williams954ee162007-08-20 11:43:25 -040081 struct net_device *dev = priv->dev;
82
83 priv->dnld_sent = DNLD_RES_RECEIVED;
84
85 /* Wake main thread if commands are pending */
86 if (!adapter->cur_cmd)
87 wake_up_interruptible(&priv->waitq);
88
Holger Schurig10078322007-11-15 18:05:47 -050089 if ((adapter->connect_status == LBS_CONNECTED)) {
Dan Williams954ee162007-08-20 11:43:25 -040090 netif_wake_queue(dev);
91 netif_wake_queue(priv->mesh_dev);
92 }
Javier Cardona51d84f52007-05-25 12:06:56 -040093 }
Dan Williams954ee162007-08-20 11:43:25 -040094 } else {
95 /* print the failure status number for debug */
96 lbs_pr_info("URB in failure status: %d\n", urb->status);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020097 }
98
99 return;
100}
101
102/**
103 * @brief free tx/rx urb, skb and rx buffer
104 * @param cardp pointer usb_card_rec
105 * @return N/A
106 */
Holger Schurigac558ca2007-08-02 11:49:06 -0400107static void if_usb_free(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200108{
Holger Schurig9012b282007-05-25 11:27:16 -0400109 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200110
111 /* Unlink tx & rx urb */
112 usb_kill_urb(cardp->tx_urb);
113 usb_kill_urb(cardp->rx_urb);
114
115 usb_free_urb(cardp->tx_urb);
116 cardp->tx_urb = NULL;
117
118 usb_free_urb(cardp->rx_urb);
119 cardp->rx_urb = NULL;
120
121 kfree(cardp->bulk_out_buffer);
122 cardp->bulk_out_buffer = NULL;
123
Holger Schurig9012b282007-05-25 11:27:16 -0400124 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200125}
126
127/**
128 * @brief sets the configuration values
129 * @param ifnum interface number
130 * @param id pointer to usb_device_id
131 * @return 0 on success, error code on failure
132 */
133static int if_usb_probe(struct usb_interface *intf,
134 const struct usb_device_id *id)
135{
136 struct usb_device *udev;
137 struct usb_host_interface *iface_desc;
138 struct usb_endpoint_descriptor *endpoint;
Holger Schurig10078322007-11-15 18:05:47 -0500139 lbs_private *priv;
Holger Schurig435a1ac2007-05-25 12:41:52 -0400140 struct usb_card_rec *cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200141 int i;
142
143 udev = interface_to_usbdev(intf);
144
Holger Schurig435a1ac2007-05-25 12:41:52 -0400145 cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
146 if (!cardp) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200147 lbs_pr_err("Out of memory allocating private data.\n");
148 goto error;
149 }
150
Holger Schurig435a1ac2007-05-25 12:41:52 -0400151 cardp->udev = udev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200152 iface_desc = intf->cur_altsetting;
153
Holger Schurig9012b282007-05-25 11:27:16 -0400154 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200155 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400156 le16_to_cpu(udev->descriptor.bcdUSB),
157 udev->descriptor.bDeviceClass,
158 udev->descriptor.bDeviceSubClass,
159 udev->descriptor.bDeviceProtocol);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200160
161 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
162 endpoint = &iface_desc->endpoint[i].desc;
163 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
164 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
165 USB_ENDPOINT_XFER_BULK)) {
166 /* we found a bulk in endpoint */
Holger Schurig9012b282007-05-25 11:27:16 -0400167 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400168 le16_to_cpu(endpoint->wMaxPacketSize));
169 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400170 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200171 "Rx URB allocation failed\n");
172 goto dealloc;
173 }
Holger Schurig435a1ac2007-05-25 12:41:52 -0400174 cardp->rx_urb_recall = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200175
Holger Schurig435a1ac2007-05-25 12:41:52 -0400176 cardp->bulk_in_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400177 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400178 cardp->bulk_in_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200179 (endpoint->
180 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Holger Schurig9012b282007-05-25 11:27:16 -0400181 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200182 endpoint->bEndpointAddress);
183 }
184
185 if (((endpoint->
186 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
187 USB_DIR_OUT)
188 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
189 USB_ENDPOINT_XFER_BULK)) {
190 /* We found bulk out endpoint */
David Woodhouse981f1872007-05-25 23:36:54 -0400191 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
Holger Schurig9012b282007-05-25 11:27:16 -0400192 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200193 "Tx URB allocation failed\n");
194 goto dealloc;
195 }
196
Holger Schurig435a1ac2007-05-25 12:41:52 -0400197 cardp->bulk_out_size =
David Woodhouse981f1872007-05-25 23:36:54 -0400198 le16_to_cpu(endpoint->wMaxPacketSize);
Holger Schurig9012b282007-05-25 11:27:16 -0400199 lbs_deb_usbd(&udev->dev,
David Woodhouse981f1872007-05-25 23:36:54 -0400200 "Bulk out size is %d\n",
201 le16_to_cpu(endpoint->wMaxPacketSize));
Holger Schurig435a1ac2007-05-25 12:41:52 -0400202 cardp->bulk_out_endpointAddr =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200203 endpoint->bEndpointAddress;
Holger Schurig9012b282007-05-25 11:27:16 -0400204 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200205 endpoint->bEndpointAddress);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400206 cardp->bulk_out_buffer =
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200207 kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
208 GFP_KERNEL);
209
Holger Schurig435a1ac2007-05-25 12:41:52 -0400210 if (!cardp->bulk_out_buffer) {
Holger Schurig9012b282007-05-25 11:27:16 -0400211 lbs_deb_usbd(&udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200212 "Could not allocate buffer\n");
213 goto dealloc;
214 }
215 }
216 }
217
Dan Williams954ee162007-08-20 11:43:25 -0400218 /* Upload firmware */
219 cardp->rinfo.cardp = cardp;
220 if (if_usb_prog_firmware(cardp)) {
221 lbs_deb_usbd(&udev->dev, "FW upload failed");
222 goto err_prog_firmware;
223 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400224
Holger Schurig10078322007-11-15 18:05:47 -0500225 if (!(priv = lbs_add_card(cardp, &udev->dev)))
Dan Williams954ee162007-08-20 11:43:25 -0400226 goto err_prog_firmware;
227
228 cardp->priv = priv;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400229
Holger Schurig10078322007-11-15 18:05:47 -0500230 if (lbs_add_mesh(priv, &udev->dev))
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400231 goto err_add_mesh;
Javier Cardona51d84f52007-05-25 12:06:56 -0400232
Dan Williams954ee162007-08-20 11:43:25 -0400233 cardp->eth_dev = priv->dev;
234
Holger Schurig208fdd22007-05-25 12:17:06 -0400235 priv->hw_host_to_card = if_usb_host_to_card;
236 priv->hw_get_int_status = if_usb_get_int_status;
237 priv->hw_read_event_cause = if_usb_read_event_cause;
Luis Carlos Cobo63f00232007-08-02 13:19:24 -0400238 priv->boot2_version = udev->descriptor.bcdDevice;
Holger Schurig208fdd22007-05-25 12:17:06 -0400239
Dan Williams954ee162007-08-20 11:43:25 -0400240 /* Delay 200 ms to waiting for the FW ready */
241 if_usb_submit_rx_urb(cardp);
242 msleep_interruptible(200);
243 priv->adapter->fw_ready = 1;
244
Holger Schurig10078322007-11-15 18:05:47 -0500245 if (lbs_start_card(priv))
Dan Williams954ee162007-08-20 11:43:25 -0400246 goto err_start_card;
Holger Schurig32a74b72007-05-25 12:04:31 -0400247
Holger Schurig435a1ac2007-05-25 12:41:52 -0400248 list_add_tail(&cardp->list, &usb_devices);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400249
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200250 usb_get_dev(udev);
Holger Schurig435a1ac2007-05-25 12:41:52 -0400251 usb_set_intfdata(intf, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200252
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200253 return 0;
254
Dan Williams954ee162007-08-20 11:43:25 -0400255err_start_card:
Holger Schurig10078322007-11-15 18:05:47 -0500256 lbs_remove_mesh(priv);
Marcelo Tosatti6a812152007-05-25 12:09:13 -0400257err_add_mesh:
Holger Schurig10078322007-11-15 18:05:47 -0500258 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400259err_prog_firmware:
260 if_usb_reset_device(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200261dealloc:
Holger Schurig435a1ac2007-05-25 12:41:52 -0400262 if_usb_free(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200263
264error:
265 return -ENOMEM;
266}
267
268/**
269 * @brief free resource and cleanup
Holger Schurig435a1ac2007-05-25 12:41:52 -0400270 * @param intf USB interface structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271 * @return N/A
272 */
273static void if_usb_disconnect(struct usb_interface *intf)
274{
275 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig10078322007-11-15 18:05:47 -0500276 lbs_private *priv = (lbs_private *) cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200277
Dan Williams954ee162007-08-20 11:43:25 -0400278 lbs_deb_enter(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200279
Dan Williams954ee162007-08-20 11:43:25 -0400280 /* Update Surprise removed to TRUE */
281 cardp->surprise_removed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200282
Holger Schurig435a1ac2007-05-25 12:41:52 -0400283 list_del(&cardp->list);
Holger Schurig3874d0f2007-05-25 12:01:42 -0400284
Dan Williams954ee162007-08-20 11:43:25 -0400285 if (priv) {
Holger Schurig10078322007-11-15 18:05:47 -0500286 lbs_adapter *adapter = priv->adapter;
Dan Williams954ee162007-08-20 11:43:25 -0400287
288 adapter->surpriseremoved = 1;
Holger Schurig10078322007-11-15 18:05:47 -0500289 lbs_stop_card(priv);
290 lbs_remove_mesh(priv);
291 lbs_remove_card(priv);
Dan Williams954ee162007-08-20 11:43:25 -0400292 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200293
294 /* Unlink and free urb */
295 if_usb_free(cardp);
296
297 usb_set_intfdata(intf, NULL);
298 usb_put_dev(interface_to_usbdev(intf));
299
Dan Williams954ee162007-08-20 11:43:25 -0400300 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200301}
302
303/**
304 * @brief This function download FW
Holger Schurig10078322007-11-15 18:05:47 -0500305 * @param priv pointer to lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200306 * @return 0
307 */
Dan Williams954ee162007-08-20 11:43:25 -0400308static int if_prog_firmware(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200309{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200310 struct FWData *fwdata;
311 struct fwheader *fwheader;
Dan Williams954ee162007-08-20 11:43:25 -0400312 u8 *firmware = cardp->fw->data;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200313
314 fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
315
316 if (!fwdata)
317 return -1;
318
319 fwheader = &fwdata->fwheader;
320
321 if (!cardp->CRC_OK) {
322 cardp->totalbytes = cardp->fwlastblksent;
323 cardp->fwseqnum = cardp->lastseqnum - 1;
324 }
325
Holger Schurig9012b282007-05-25 11:27:16 -0400326 /*
327 lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200328 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400329 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200330
331 memcpy(fwheader, &firmware[cardp->totalbytes],
332 sizeof(struct fwheader));
333
334 cardp->fwlastblksent = cardp->totalbytes;
335 cardp->totalbytes += sizeof(struct fwheader);
336
Holger Schurig9012b282007-05-25 11:27:16 -0400337 /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338 memcpy(fwdata->data, &firmware[cardp->totalbytes],
David Woodhouse981f1872007-05-25 23:36:54 -0400339 le32_to_cpu(fwdata->fwheader.datalength));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200340
Holger Schurig9012b282007-05-25 11:27:16 -0400341 /*
342 lbs_deb_usbd(&cardp->udev->dev,
David Woodhousebb793e22007-05-25 23:38:14 -0400343 "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
Holger Schurig9012b282007-05-25 11:27:16 -0400344 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200345
346 cardp->fwseqnum = cardp->fwseqnum + 1;
347
David Woodhouse981f1872007-05-25 23:36:54 -0400348 fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
349 cardp->lastseqnum = cardp->fwseqnum;
350 cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200351
David Woodhouse981f1872007-05-25 23:36:54 -0400352 if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400353 /*
David Woodhouse981f1872007-05-25 23:36:54 -0400354 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400355 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200356 "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
357 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400358 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200359 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400360 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200361
David Woodhousebb793e22007-05-25 23:38:14 -0400362 } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400363 /*
364 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200365 "Host has finished FW downloading\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400366 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200367 "Donwloading FW JUMP BLOCK\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400368 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200369 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
Dan Williams954ee162007-08-20 11:43:25 -0400370 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200371 cardp->fwfinalblk = 1;
372 }
373
Holger Schurig9012b282007-05-25 11:27:16 -0400374 /*
375 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200376 "The firmware download is done size is %d\n",
377 cardp->totalbytes);
Holger Schurig9012b282007-05-25 11:27:16 -0400378 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200379
380 kfree(fwdata);
381
382 return 0;
383}
384
Dan Williams954ee162007-08-20 11:43:25 -0400385static int if_usb_reset_device(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200386{
387 int ret;
Holger Schurig10078322007-11-15 18:05:47 -0500388 lbs_private * priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200389
Holger Schurig3874d0f2007-05-25 12:01:42 -0400390 lbs_deb_enter(LBS_DEB_USB);
391
Dan Williamseedc2a32007-08-02 11:36:22 -0400392 /* Try a USB port reset first, if that fails send the reset
393 * command to the firmware.
394 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200395 ret = usb_reset_device(cardp->udev);
Dan Williams954ee162007-08-20 11:43:25 -0400396 if (!ret && priv) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200397 msleep(10);
Holger Schurig10078322007-11-15 18:05:47 -0500398 ret = lbs_reset_device(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200399 msleep(10);
400 }
Holger Schurig3874d0f2007-05-25 12:01:42 -0400401
402 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
403
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200404 return ret;
405}
406
407/**
408 * @brief This function transfer the data to the device.
Holger Schurig10078322007-11-15 18:05:47 -0500409 * @param priv pointer to lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200410 * @param payload pointer to payload data
411 * @param nb data length
412 * @return 0 or -1
413 */
Dan Williams954ee162007-08-20 11:43:25 -0400414static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200415{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200416 int ret = -1;
417
418 /* check if device is removed */
Dan Williams954ee162007-08-20 11:43:25 -0400419 if (cardp->surprise_removed) {
Holger Schurig9012b282007-05-25 11:27:16 -0400420 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200421 goto tx_ret;
422 }
423
424 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
425 usb_sndbulkpipe(cardp->udev,
426 cardp->bulk_out_endpointAddr),
Dan Williams954ee162007-08-20 11:43:25 -0400427 payload, nb, if_usb_write_bulk_callback, cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200428
429 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
430
431 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
432 /* transfer failed */
Holger Schurig9012b282007-05-25 11:27:16 -0400433 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200434 ret = -1;
435 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400436 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200437 ret = 0;
438 }
439
440tx_ret:
441 return ret;
442}
443
Dan Williams954ee162007-08-20 11:43:25 -0400444static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
445 void (*callbackfn)(struct urb *urb))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200447 struct sk_buff *skb;
448 struct read_cb_info *rinfo = &cardp->rinfo;
449 int ret = -1;
450
451 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
452 lbs_pr_err("No free skb\n");
453 goto rx_ret;
454 }
455
456 rinfo->skb = skb;
457
458 /* Fill the receive configuration URB and initialise the Rx call back */
459 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
460 usb_rcvbulkpipe(cardp->udev,
461 cardp->bulk_in_endpointAddr),
Dan Williams4269e2a2007-05-10 23:10:18 -0400462 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200463 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
464 rinfo);
465
466 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
467
Holger Schurig9012b282007-05-25 11:27:16 -0400468 /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
470 /* handle failure conditions */
Holger Schurig9012b282007-05-25 11:27:16 -0400471 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200472 ret = -1;
473 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400474 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200475 ret = 0;
476 }
477
478rx_ret:
479 return ret;
480}
481
Dan Williams954ee162007-08-20 11:43:25 -0400482static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200483{
Dan Williams954ee162007-08-20 11:43:25 -0400484 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200485}
486
Dan Williams954ee162007-08-20 11:43:25 -0400487static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200488{
Dan Williams954ee162007-08-20 11:43:25 -0400489 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200490}
491
492static void if_usb_receive_fwload(struct urb *urb)
493{
494 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200495 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400496 struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200497 struct fwsyncheader *syncfwheader;
498 struct bootcmdrespStr bootcmdresp;
499
500 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400501 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200502 "URB status is failed during fw load\n");
503 kfree_skb(skb);
504 return;
505 }
506
507 if (cardp->bootcmdresp == 0) {
508 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
509 sizeof(bootcmdresp));
David Woodhouse981f1872007-05-25 23:36:54 -0400510 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200511 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400512 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400514 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200515 "Received valid boot command response\n");
516 return;
517 }
David Woodhouse981f1872007-05-25 23:36:54 -0400518 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200519 lbs_pr_info(
520 "boot cmd response wrong magic number (0x%x)\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400521 le32_to_cpu(bootcmdresp.u32magicnumber));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200522 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
523 lbs_pr_info(
524 "boot cmd response cmd_tag error (%d)\n",
525 bootcmdresp.u8cmd_tag);
526 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
527 lbs_pr_info(
528 "boot cmd response result error (%d)\n",
529 bootcmdresp.u8result);
530 } else {
531 cardp->bootcmdresp = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400532 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533 "Received valid boot command response\n");
534 }
535 kfree_skb(skb);
Dan Williams954ee162007-08-20 11:43:25 -0400536 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200537 return;
538 }
539
540 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
541 if (!syncfwheader) {
Holger Schurig9012b282007-05-25 11:27:16 -0400542 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200543 kfree_skb(skb);
544 return;
545 }
546
547 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
548 sizeof(struct fwsyncheader));
549
550 if (!syncfwheader->cmd) {
Holger Schurig9012b282007-05-25 11:27:16 -0400551 /*
552 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200553 "FW received Blk with correct CRC\n");
Holger Schurig9012b282007-05-25 11:27:16 -0400554 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200555 "FW received Blk seqnum = %d\n",
556 syncfwheader->seqnum);
Holger Schurig9012b282007-05-25 11:27:16 -0400557 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200558 cardp->CRC_OK = 1;
559 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400560 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200561 "FW received Blk with CRC error\n");
562 cardp->CRC_OK = 0;
563 }
564
565 kfree_skb(skb);
566
567 if (cardp->fwfinalblk) {
568 cardp->fwdnldover = 1;
569 goto exit;
570 }
571
Dan Williams954ee162007-08-20 11:43:25 -0400572 if_prog_firmware(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200573
Dan Williams954ee162007-08-20 11:43:25 -0400574 if_usb_submit_rx_urb_fwload(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200575exit:
576 kfree(syncfwheader);
577
578 return;
579
580}
581
582#define MRVDRV_MIN_PKT_LEN 30
583
584static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
585 struct usb_card_rec *cardp,
Holger Schurig10078322007-11-15 18:05:47 -0500586 lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200587{
588 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
589 MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
Holger Schurig9012b282007-05-25 11:27:16 -0400590 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200591 "Packet length is Invalid\n");
592 kfree_skb(skb);
593 return;
594 }
595
596 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
597 skb_put(skb, recvlength);
598 skb_pull(skb, MESSAGE_HEADER_LEN);
Holger Schurig10078322007-11-15 18:05:47 -0500599 lbs_process_rxed_packet(priv, skb);
Holger Schurig634b8f42007-05-25 13:05:16 -0400600 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200601}
602
603static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
604 struct sk_buff *skb,
605 struct usb_card_rec *cardp,
Holger Schurig10078322007-11-15 18:05:47 -0500606 lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200607{
608 u8 *cmdbuf;
609 if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400610 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200611 "The receive buffer is too large\n");
612 kfree_skb(skb);
613 return;
614 }
615
616 if (!in_interrupt())
617 BUG();
618
619 spin_lock(&priv->adapter->driver_lock);
620 /* take care of cur_cmd = NULL case by reading the
621 * data to clear the interrupt */
622 if (!priv->adapter->cur_cmd) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400623 cmdbuf = priv->upld_buf;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400624 priv->adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200625 } else
626 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
627
Holger Schurigc95c7f92007-08-02 11:49:45 -0400628 cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
Holger Schurig634b8f42007-05-25 13:05:16 -0400629 priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200630 memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
Holger Schurig634b8f42007-05-25 13:05:16 -0400631 priv->upld_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200632
633 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500634 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200635 spin_unlock(&priv->adapter->driver_lock);
636
Holger Schurig9012b282007-05-25 11:27:16 -0400637 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200638 "Wake up main thread to handle cmd response\n");
639
640 return;
641}
642
643/**
644 * @brief This function reads of the packet into the upload buff,
645 * wake up the main thread and initialise the Rx callack.
646 *
647 * @param urb pointer to struct urb
648 * @return N/A
649 */
650static void if_usb_receive(struct urb *urb)
651{
652 struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200653 struct sk_buff *skb = rinfo->skb;
Dan Williams954ee162007-08-20 11:43:25 -0400654 struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
Holger Schurig10078322007-11-15 18:05:47 -0500655 lbs_private * priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200656
657 int recvlength = urb->actual_length;
658 u8 *recvbuff = NULL;
Dan Williams8362cd42007-08-03 09:40:55 -0400659 u32 recvtype = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200660
Holger Schurig9012b282007-05-25 11:27:16 -0400661 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200662
663 if (recvlength) {
Dan Williams8362cd42007-08-03 09:40:55 -0400664 __le32 tmp;
665
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200666 if (urb->status) {
Holger Schurig9012b282007-05-25 11:27:16 -0400667 lbs_deb_usbd(&cardp->udev->dev,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200668 "URB status is failed\n");
669 kfree_skb(skb);
670 goto setup_for_next;
671 }
672
673 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
Dan Williams8362cd42007-08-03 09:40:55 -0400674 memcpy(&tmp, recvbuff, sizeof(u32));
675 recvtype = le32_to_cpu(tmp);
Holger Schurig9012b282007-05-25 11:27:16 -0400676 lbs_deb_usbd(&cardp->udev->dev,
Dan Williams8362cd42007-08-03 09:40:55 -0400677 "Recv length = 0x%x, Recv type = 0x%X\n",
678 recvlength, recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200679 } else if (urb->status)
680 goto rx_exit;
681
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200682 switch (recvtype) {
683 case CMD_TYPE_DATA:
684 process_cmdtypedata(recvlength, skb, cardp, priv);
685 break;
686
687 case CMD_TYPE_REQUEST:
688 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
689 break;
690
691 case CMD_TYPE_INDICATION:
692 /* Event cause handling */
693 spin_lock(&priv->adapter->driver_lock);
David Woodhouse981f1872007-05-25 23:36:54 -0400694 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
Holger Schurig9012b282007-05-25 11:27:16 -0400695 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200696 cardp->usb_event_cause);
697 if (cardp->usb_event_cause & 0xffff0000) {
Holger Schurig10078322007-11-15 18:05:47 -0500698 lbs_send_tx_feedback(priv);
Dan Williams12a4d262007-05-10 23:08:54 -0400699 spin_unlock(&priv->adapter->driver_lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200700 break;
701 }
David Woodhouse981f1872007-05-25 23:36:54 -0400702 cardp->usb_event_cause <<= 3;
Holger Schurigc95c7f92007-08-02 11:49:45 -0400703 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200704 kfree_skb(skb);
Holger Schurig10078322007-11-15 18:05:47 -0500705 lbs_interrupt(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200706 spin_unlock(&priv->adapter->driver_lock);
707 goto rx_exit;
708 default:
Dan Williams8362cd42007-08-03 09:40:55 -0400709 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
710 recvtype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200711 kfree_skb(skb);
712 break;
713 }
714
715setup_for_next:
Dan Williams954ee162007-08-20 11:43:25 -0400716 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200717rx_exit:
Holger Schurig9012b282007-05-25 11:27:16 -0400718 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719}
720
721/**
722 * @brief This function downloads data to FW
Holger Schurig10078322007-11-15 18:05:47 -0500723 * @param priv pointer to lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200724 * @param type type of data
725 * @param buf pointer to data buffer
726 * @param len number of bytes
727 * @return 0 or -1
728 */
Holger Schurig10078322007-11-15 18:05:47 -0500729static int if_usb_host_to_card(lbs_private *priv, u8 type, u8 *payload, u16 nb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200730{
Holger Schurig634b8f42007-05-25 13:05:16 -0400731 struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200732
Holger Schurig9012b282007-05-25 11:27:16 -0400733 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
734 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200735
736 if (type == MVMS_CMD) {
Dan Williams8362cd42007-08-03 09:40:55 -0400737 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
Holger Schurig634b8f42007-05-25 13:05:16 -0400738 priv->dnld_sent = DNLD_CMD_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200739 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
740 MESSAGE_HEADER_LEN);
741
742 } else {
Dan Williams8362cd42007-08-03 09:40:55 -0400743 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
Holger Schurig634b8f42007-05-25 13:05:16 -0400744 priv->dnld_sent = DNLD_DATA_SENT;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200745 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
746 MESSAGE_HEADER_LEN);
747 }
748
749 memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
750
Dan Williams954ee162007-08-20 11:43:25 -0400751 return usb_tx_block(cardp, cardp->bulk_out_buffer,
Dan Williams8362cd42007-08-03 09:40:55 -0400752 nb + MESSAGE_HEADER_LEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753}
754
755/* called with adapter->driver_lock held */
Holger Schurig10078322007-11-15 18:05:47 -0500756static int if_usb_get_int_status(lbs_private *priv, u8 *ireg)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200757{
Holger Schurig634b8f42007-05-25 13:05:16 -0400758 struct usb_card_rec *cardp = priv->card;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759
760 *ireg = cardp->usb_int_cause;
761 cardp->usb_int_cause = 0;
762
Holger Schurig9012b282007-05-25 11:27:16 -0400763 lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200764
765 return 0;
766}
767
Holger Schurig10078322007-11-15 18:05:47 -0500768static int if_usb_read_event_cause(lbs_private * priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200769{
Holger Schurig634b8f42007-05-25 13:05:16 -0400770 struct usb_card_rec *cardp = priv->card;
Dan Williams954ee162007-08-20 11:43:25 -0400771
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200772 priv->adapter->eventcause = cardp->usb_event_cause;
773 /* Re-submit rx urb here to avoid event lost issue */
Dan Williams954ee162007-08-20 11:43:25 -0400774 if_usb_submit_rx_urb(cardp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200775 return 0;
776}
777
Dan Williams9e22cb62007-08-02 11:14:49 -0400778/**
779 * @brief This function issues Boot command to the Boot2 code
780 * @param ivalue 1:Boot from FW by USB-Download
781 * 2:Boot from FW in EEPROM
782 * @return 0
783 */
Dan Williams954ee162007-08-20 11:43:25 -0400784static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
Dan Williams9e22cb62007-08-02 11:14:49 -0400785{
Dan Williams954ee162007-08-20 11:43:25 -0400786 struct bootcmdstr sbootcmd;
Dan Williams9e22cb62007-08-02 11:14:49 -0400787 int i;
788
789 /* Prepare command */
790 sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
791 sbootcmd.u8cmd_tag = ivalue;
792 for (i=0; i<11; i++)
793 sbootcmd.au8dumy[i]=0x00;
794 memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
795
796 /* Issue command */
Dan Williams954ee162007-08-20 11:43:25 -0400797 usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
Dan Williams9e22cb62007-08-02 11:14:49 -0400798
799 return 0;
800}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200801
802
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400803/**
804 * @brief This function checks the validity of Boot2/FW image.
805 *
806 * @param data pointer to image
807 * len image length
808 * @return 0 or -1
809 */
810static int check_fwfile_format(u8 *data, u32 totlen)
811{
812 u32 bincmd, exit;
813 u32 blksize, offset, len;
814 int ret;
815
816 ret = 1;
817 exit = len = 0;
818
819 do {
820 struct fwheader *fwh = (void *)data;
821
822 bincmd = le32_to_cpu(fwh->dnldcmd);
823 blksize = le32_to_cpu(fwh->datalength);
824 switch (bincmd) {
825 case FW_HAS_DATA_TO_RECV:
826 offset = sizeof(struct fwheader) + blksize;
827 data += offset;
828 len += offset;
829 if (len >= totlen)
830 exit = 1;
831 break;
832 case FW_HAS_LAST_BLOCK:
833 exit = 1;
834 ret = 0;
835 break;
836 default:
837 exit = 1;
838 break;
839 }
840 } while (!exit);
841
842 if (ret)
843 lbs_pr_err("firmware file format check FAIL\n");
844 else
845 lbs_deb_fw("firmware file format check PASS\n");
846
847 return ret;
848}
849
850
Dan Williams954ee162007-08-20 11:43:25 -0400851static int if_usb_prog_firmware(struct usb_card_rec *cardp)
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400852{
Dan Williams954ee162007-08-20 11:43:25 -0400853 int i = 0;
854 static int reset_count = 10;
855 int ret = 0;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400856
Dan Williams954ee162007-08-20 11:43:25 -0400857 lbs_deb_enter(LBS_DEB_USB);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400858
Holger Schurig10078322007-11-15 18:05:47 -0500859 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
Dan Williams954ee162007-08-20 11:43:25 -0400860 &cardp->udev->dev)) < 0) {
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400861 lbs_pr_err("request_firmware() failed with %#x\n", ret);
Holger Schurig10078322007-11-15 18:05:47 -0500862 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400863 goto done;
864 }
865
Dan Williams954ee162007-08-20 11:43:25 -0400866 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
867 goto release_fw;
868
869restart:
870 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
871 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
872 ret = -1;
873 goto release_fw;
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400874 }
875
Dan Williams954ee162007-08-20 11:43:25 -0400876 cardp->bootcmdresp = 0;
877 do {
878 int j = 0;
879 i++;
880 /* Issue Boot command = 1, Boot from Download-FW */
881 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
882 /* wait for command response */
883 do {
884 j++;
885 msleep_interruptible(100);
886 } while (cardp->bootcmdresp == 0 && j < 10);
887 } while (cardp->bootcmdresp == 0 && i < 5);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400888
Dan Williams954ee162007-08-20 11:43:25 -0400889 if (cardp->bootcmdresp == 0) {
890 if (--reset_count >= 0) {
891 if_usb_reset_device(cardp);
892 goto restart;
893 }
894 return -1;
895 }
896
897 i = 0;
898
899 cardp->totalbytes = 0;
900 cardp->fwlastblksent = 0;
901 cardp->CRC_OK = 1;
902 cardp->fwdnldover = 0;
903 cardp->fwseqnum = -1;
904 cardp->totalbytes = 0;
905 cardp->fwfinalblk = 0;
906
907 if_prog_firmware(cardp);
908
909 do {
910 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
911 i++;
912 msleep_interruptible(100);
913 if (cardp->surprise_removed || i >= 20)
914 break;
915 } while (!cardp->fwdnldover);
916
917 if (!cardp->fwdnldover) {
918 lbs_pr_info("failed to load fw, resetting device!\n");
919 if (--reset_count >= 0) {
920 if_usb_reset_device(cardp);
921 goto restart;
922 }
923
924 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
925 ret = -1;
926 goto release_fw;
927 }
928
929release_fw:
930 release_firmware(cardp->fw);
931 cardp->fw = NULL;
932
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400933done:
Dan Williams954ee162007-08-20 11:43:25 -0400934 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
Holger Schurig1df4e8f2007-08-02 11:45:12 -0400935 return ret;
936}
937
938
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200939#ifdef CONFIG_PM
940static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
941{
942 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig10078322007-11-15 18:05:47 -0500943 lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200944
Holger Schurig9012b282007-05-25 11:27:16 -0400945 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200946
947 if (priv->adapter->psstate != PS_STATE_FULL_POWER)
948 return -1;
949
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400950 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
951 /* Mesh autostart must be activated while sleeping
952 * On resume it will go back to the current state
953 */
954 struct cmd_ds_mesh_access mesh_access;
955 memset(&mesh_access, 0, sizeof(mesh_access));
956 mesh_access.data[0] = cpu_to_le32(1);
Holger Schurig10078322007-11-15 18:05:47 -0500957 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400958 CMD_MESH_ACCESS,
959 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
960 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
961 }
962
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200963 netif_device_detach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400964 netif_device_detach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200965
966 /* Unlink tx & rx urb */
967 usb_kill_urb(cardp->tx_urb);
968 usb_kill_urb(cardp->rx_urb);
969
970 cardp->rx_urb_recall = 1;
971
Holger Schurig9012b282007-05-25 11:27:16 -0400972 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200973 return 0;
974}
975
976static int if_usb_resume(struct usb_interface *intf)
977{
978 struct usb_card_rec *cardp = usb_get_intfdata(intf);
Holger Schurig10078322007-11-15 18:05:47 -0500979 lbs_private *priv = cardp->priv;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200980
Holger Schurig9012b282007-05-25 11:27:16 -0400981 lbs_deb_enter(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200982
983 cardp->rx_urb_recall = 0;
984
985 if_usb_submit_rx_urb(cardp->priv);
986
987 netif_device_attach(cardp->eth_dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400988 netif_device_attach(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200989
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400990 if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
991 /* Mesh autostart was activated while sleeping
992 * Disable it if appropriate
993 */
994 struct cmd_ds_mesh_access mesh_access;
995 memset(&mesh_access, 0, sizeof(mesh_access));
996 mesh_access.data[0] = cpu_to_le32(0);
Holger Schurig10078322007-11-15 18:05:47 -0500997 lbs_prepare_and_send_command(priv,
Luis Carlos Cobod21b31f2007-08-02 13:16:02 -0400998 CMD_MESH_ACCESS,
999 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
1000 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
1001 }
1002
Holger Schurig9012b282007-05-25 11:27:16 -04001003 lbs_deb_leave(LBS_DEB_USB);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001004 return 0;
1005}
1006#else
1007#define if_usb_suspend NULL
1008#define if_usb_resume NULL
1009#endif
1010
1011static struct usb_driver if_usb_driver = {
1012 /* driver name */
1013 .name = usbdriver_name,
1014 /* probe function name */
1015 .probe = if_usb_probe,
1016 /* disconnect function name */
1017 .disconnect = if_usb_disconnect,
1018 /* device signature table */
1019 .id_table = if_usb_table,
1020 .suspend = if_usb_suspend,
1021 .resume = if_usb_resume,
1022};
1023
Holger Schurig084708b2007-05-25 12:37:58 -04001024static int if_usb_init_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001025{
Holger Schurig084708b2007-05-25 12:37:58 -04001026 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001027
Holger Schurig084708b2007-05-25 12:37:58 -04001028 lbs_deb_enter(LBS_DEB_MAIN);
1029
Holger Schurig10078322007-11-15 18:05:47 -05001030 if (lbs_fw_name == NULL) {
1031 lbs_fw_name = default_fw_name;
Holger Schurig084708b2007-05-25 12:37:58 -04001032 }
1033
1034 ret = usb_register(&if_usb_driver);
1035
1036 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1037 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001038}
1039
Holger Schurig084708b2007-05-25 12:37:58 -04001040static void if_usb_exit_module(void)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001041{
Marcelo Tosattid43fb8e2007-05-25 16:28:20 -04001042 struct usb_card_rec *cardp, *cardp_temp;
Holger Schurig3874d0f2007-05-25 12:01:42 -04001043
Holger Schurig084708b2007-05-25 12:37:58 -04001044 lbs_deb_enter(LBS_DEB_MAIN);
1045
Dan Williamsb6e99dd2007-08-20 12:22:15 -04001046 list_for_each_entry_safe(cardp, cardp_temp, &usb_devices, list) {
Holger Schurig10078322007-11-15 18:05:47 -05001047 lbs_prepare_and_send_command(cardp->priv, CMD_802_11_RESET,
Dan Williamsb6e99dd2007-08-20 12:22:15 -04001048 CMD_ACT_HALT, 0, 0, NULL);
1049 }
Holger Schurig3874d0f2007-05-25 12:01:42 -04001050
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051 /* API unregisters the driver from USB subsystem */
1052 usb_deregister(&if_usb_driver);
Holger Schurig084708b2007-05-25 12:37:58 -04001053
1054 lbs_deb_leave(LBS_DEB_MAIN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001055}
Holger Schurig084708b2007-05-25 12:37:58 -04001056
1057module_init(if_usb_init_module);
1058module_exit(if_usb_exit_module);
1059
1060MODULE_DESCRIPTION("8388 USB WLAN Driver");
1061MODULE_AUTHOR("Marvell International Ltd.");
1062MODULE_LICENSE("GPL");