blob: 919451a2ee4a118df1661a054c660b36b48ee333 [file] [log] [blame]
Luis Carlos Coboc305a192008-08-14 10:41:06 -07001/*
2 * Copyright (C) 2008, cozybit Inc.
3 * Copyright (C) 2003-2006, Marvell International Ltd.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 */
Steve deRosiere9bd5bc2010-04-25 14:40:46 -070010#define DRV_NAME "lbtf_usb"
11
12#include "deb_defs.h"
13#include "libertas_tf.h"
14#include "if_usb.h"
15
Luis Carlos Coboc305a192008-08-14 10:41:06 -070016#include <linux/delay.h>
17#include <linux/moduleparam.h>
18#include <linux/firmware.h>
19#include <linux/netdevice.h>
20#include <linux/usb.h>
21
Steve deRosiere9bd5bc2010-04-25 14:40:46 -070022#define INSANEDEBUG 0
23#define lbtf_deb_usb2(...) do { if (INSANEDEBUG) lbtf_deb_usbd(__VA_ARGS__); } while (0)
Luis Carlos Coboc305a192008-08-14 10:41:06 -070024
25#define MESSAGE_HEADER_LEN 4
26
27static char *lbtf_fw_name = "lbtf_usb.bin";
28module_param_named(fw_name, lbtf_fw_name, charp, 0644);
29
Ben Hutchings790e7562009-11-07 22:00:38 +000030MODULE_FIRMWARE("lbtf_usb.bin");
31
Luis Carlos Coboc305a192008-08-14 10:41:06 -070032static struct usb_device_id if_usb_table[] = {
33 /* Enter the device signature inside */
34 { USB_DEVICE(0x1286, 0x2001) },
35 { USB_DEVICE(0x05a3, 0x8388) },
36 {} /* Terminating entry */
37};
38
39MODULE_DEVICE_TABLE(usb, if_usb_table);
40
41static void if_usb_receive(struct urb *urb);
42static void if_usb_receive_fwload(struct urb *urb);
43static int if_usb_prog_firmware(struct if_usb_card *cardp);
44static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type,
45 uint8_t *payload, uint16_t nb);
46static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
47 uint16_t nb, u8 data);
48static void if_usb_free(struct if_usb_card *cardp);
49static int if_usb_submit_rx_urb(struct if_usb_card *cardp);
50static int if_usb_reset_device(struct if_usb_card *cardp);
51
52/**
53 * if_usb_wrike_bulk_callback - call back to handle URB status
54 *
55 * @param urb pointer to urb structure
56 */
57static void if_usb_write_bulk_callback(struct urb *urb)
58{
Steve deRosiere9bd5bc2010-04-25 14:40:46 -070059 if (urb->status != 0) {
60 /* print the failure status number for debug */
61 pr_info("URB in failure status: %d\n", urb->status);
62 } else {
63 lbtf_deb_usb2(&urb->dev->dev, "URB status is successful\n");
64 lbtf_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
65 urb->actual_length);
66 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -070067}
68
69/**
70 * if_usb_free - free tx/rx urb, skb and rx buffer
71 *
72 * @param cardp pointer if_usb_card
73 */
74static void if_usb_free(struct if_usb_card *cardp)
75{
Steve deRosiere9bd5bc2010-04-25 14:40:46 -070076 lbtf_deb_enter(LBTF_DEB_USB);
77
Luis Carlos Coboc305a192008-08-14 10:41:06 -070078 /* Unlink tx & rx urb */
79 usb_kill_urb(cardp->tx_urb);
80 usb_kill_urb(cardp->rx_urb);
81 usb_kill_urb(cardp->cmd_urb);
82
83 usb_free_urb(cardp->tx_urb);
84 cardp->tx_urb = NULL;
85
86 usb_free_urb(cardp->rx_urb);
87 cardp->rx_urb = NULL;
88
89 usb_free_urb(cardp->cmd_urb);
90 cardp->cmd_urb = NULL;
91
92 kfree(cardp->ep_out_buf);
93 cardp->ep_out_buf = NULL;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -070094
95 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -070096}
97
98static void if_usb_setup_firmware(struct lbtf_private *priv)
99{
100 struct if_usb_card *cardp = priv->card;
101 struct cmd_ds_set_boot2_ver b2_cmd;
102
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700103 lbtf_deb_enter(LBTF_DEB_USB);
104
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700105 if_usb_submit_rx_urb(cardp);
106 b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
107 b2_cmd.action = 0;
108 b2_cmd.version = cardp->boot2_version;
109
110 if (lbtf_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700111 lbtf_deb_usb("Setting boot2 version failed\n");
112
113 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700114}
115
116static void if_usb_fw_timeo(unsigned long priv)
117{
118 struct if_usb_card *cardp = (void *)priv;
119
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700120 lbtf_deb_enter(LBTF_DEB_USB);
121 if (!cardp->fwdnldover) {
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700122 /* Download timed out */
123 cardp->priv->surpriseremoved = 1;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700124 pr_err("Download timed out\n");
125 } else {
126 lbtf_deb_usb("Download complete, no event. Assuming success\n");
127 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700128 wake_up(&cardp->fw_wq);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700129 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700130}
131
132/**
133 * if_usb_probe - sets the configuration values
134 *
135 * @ifnum interface number
136 * @id pointer to usb_device_id
137 *
138 * Returns: 0 on success, error code on failure
139 */
140static int if_usb_probe(struct usb_interface *intf,
141 const struct usb_device_id *id)
142{
143 struct usb_device *udev;
144 struct usb_host_interface *iface_desc;
145 struct usb_endpoint_descriptor *endpoint;
146 struct lbtf_private *priv;
147 struct if_usb_card *cardp;
148 int i;
149
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700150 lbtf_deb_enter(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700151 udev = interface_to_usbdev(intf);
152
153 cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700154 if (!cardp) {
155 pr_err("Out of memory allocating private data.\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700156 goto error;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700157 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700158
159 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
160 init_waitqueue_head(&cardp->fw_wq);
161
162 cardp->udev = udev;
163 iface_desc = intf->cur_altsetting;
164
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700165 lbtf_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
166 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
167 le16_to_cpu(udev->descriptor.bcdUSB),
168 udev->descriptor.bDeviceClass,
169 udev->descriptor.bDeviceSubClass,
170 udev->descriptor.bDeviceProtocol);
171
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700172 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
173 endpoint = &iface_desc->endpoint[i].desc;
174 if (usb_endpoint_is_bulk_in(endpoint)) {
175 cardp->ep_in_size =
176 le16_to_cpu(endpoint->wMaxPacketSize);
177 cardp->ep_in = usb_endpoint_num(endpoint);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700178
179 lbtf_deb_usbd(&udev->dev, "in_endpoint = %d\n", cardp->ep_in);
180 lbtf_deb_usbd(&udev->dev, "Bulk in size is %d\n", cardp->ep_in_size);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700181 } else if (usb_endpoint_is_bulk_out(endpoint)) {
182 cardp->ep_out_size =
183 le16_to_cpu(endpoint->wMaxPacketSize);
184 cardp->ep_out = usb_endpoint_num(endpoint);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700185
186 lbtf_deb_usbd(&udev->dev, "out_endpoint = %d\n", cardp->ep_out);
187 lbtf_deb_usbd(&udev->dev, "Bulk out size is %d\n",
188 cardp->ep_out_size);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700189 }
190 }
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700191 if (!cardp->ep_out_size || !cardp->ep_in_size) {
192 lbtf_deb_usbd(&udev->dev, "Endpoints not found\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700193 /* Endpoints not found */
194 goto dealloc;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700195 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700196
197 cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700198 if (!cardp->rx_urb) {
199 lbtf_deb_usbd(&udev->dev, "Rx URB allocation failed\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700200 goto dealloc;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700201 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700202
203 cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700204 if (!cardp->tx_urb) {
205 lbtf_deb_usbd(&udev->dev, "Tx URB allocation failed\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700206 goto dealloc;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700207 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700208
209 cardp->cmd_urb = usb_alloc_urb(0, GFP_KERNEL);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700210 if (!cardp->cmd_urb) {
211 lbtf_deb_usbd(&udev->dev, "Cmd URB allocation failed\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700212 goto dealloc;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700213 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700214
215 cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
216 GFP_KERNEL);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700217 if (!cardp->ep_out_buf) {
218 lbtf_deb_usbd(&udev->dev, "Could not allocate buffer\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700219 goto dealloc;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700220 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700221
222 priv = lbtf_add_card(cardp, &udev->dev);
223 if (!priv)
224 goto dealloc;
225
226 cardp->priv = priv;
227
228 priv->hw_host_to_card = if_usb_host_to_card;
229 priv->hw_prog_firmware = if_usb_prog_firmware;
230 priv->hw_reset_device = if_usb_reset_device;
231 cardp->boot2_version = udev->descriptor.bcdDevice;
232
233 usb_get_dev(udev);
234 usb_set_intfdata(intf, cardp);
235
236 return 0;
237
238dealloc:
239 if_usb_free(cardp);
240error:
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700241lbtf_deb_leave(LBTF_DEB_MAIN);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700242 return -ENOMEM;
243}
244
245/**
246 * if_usb_disconnect - free resource and cleanup
247 *
248 * @intf USB interface structure
249 */
250static void if_usb_disconnect(struct usb_interface *intf)
251{
252 struct if_usb_card *cardp = usb_get_intfdata(intf);
253 struct lbtf_private *priv = (struct lbtf_private *) cardp->priv;
254
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700255 lbtf_deb_enter(LBTF_DEB_MAIN);
256
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700257 if_usb_reset_device(cardp);
258
259 if (priv)
260 lbtf_remove_card(priv);
261
262 /* Unlink and free urb */
263 if_usb_free(cardp);
264
265 usb_set_intfdata(intf, NULL);
266 usb_put_dev(interface_to_usbdev(intf));
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700267
268 lbtf_deb_leave(LBTF_DEB_MAIN);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700269}
270
271/**
272 * if_usb_send_fw_pkt - This function downloads the FW
273 *
274 * @priv pointer to struct lbtf_private
275 *
276 * Returns: 0
277 */
278static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
279{
280 struct fwdata *fwdata = cardp->ep_out_buf;
281 u8 *firmware = (u8 *) cardp->fw->data;
282
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700283 lbtf_deb_enter(LBTF_DEB_FW);
284
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700285 /* If we got a CRC failure on the last block, back
286 up and retry it */
287 if (!cardp->CRC_OK) {
288 cardp->totalbytes = cardp->fwlastblksent;
289 cardp->fwseqnum--;
290 }
291
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700292 lbtf_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
293 cardp->totalbytes);
294
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700295 /* struct fwdata (which we sent to the card) has an
296 extra __le32 field in between the header and the data,
297 which is not in the struct fwheader in the actual
298 firmware binary. Insert the seqnum in the middle... */
299 memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
300 sizeof(struct fwheader));
301
302 cardp->fwlastblksent = cardp->totalbytes;
303 cardp->totalbytes += sizeof(struct fwheader);
304
305 memcpy(fwdata->data, &firmware[cardp->totalbytes],
306 le32_to_cpu(fwdata->hdr.datalength));
307
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700308 lbtf_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
309 le32_to_cpu(fwdata->hdr.datalength));
310
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700311 fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
312 cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
313
314 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
315 le32_to_cpu(fwdata->hdr.datalength), 0);
316
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700317 if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
318 lbtf_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
319 lbtf_deb_usb2(&cardp->udev->dev, "seqnum = %d totalbytes = %d\n",
320 cardp->fwseqnum, cardp->totalbytes);
321 } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
322 lbtf_deb_usb2(&cardp->udev->dev, "Host has finished FW downloading\n");
323 lbtf_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
324
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700325 /* Host has finished FW downloading
326 * Donwloading FW JUMP BLOCK
327 */
328 cardp->fwfinalblk = 1;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700329 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700330
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700331 lbtf_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
332 cardp->totalbytes);
333
334 lbtf_deb_leave(LBTF_DEB_FW);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700335 return 0;
336}
337
338static int if_usb_reset_device(struct if_usb_card *cardp)
339{
340 struct cmd_ds_802_11_reset *cmd = cardp->ep_out_buf + 4;
341 int ret;
342
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700343 lbtf_deb_enter(LBTF_DEB_USB);
344
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700345 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
346
347 cmd->hdr.command = cpu_to_le16(CMD_802_11_RESET);
348 cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset));
349 cmd->hdr.result = cpu_to_le16(0);
350 cmd->hdr.seqnum = cpu_to_le16(0x5a5a);
351 cmd->action = cpu_to_le16(CMD_ACT_HALT);
352 usb_tx_block(cardp, cardp->ep_out_buf,
353 4 + sizeof(struct cmd_ds_802_11_reset), 0);
354
355 msleep(100);
356 ret = usb_reset_device(cardp->udev);
357 msleep(100);
358
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700359 lbtf_deb_leave_args(LBTF_DEB_USB, "ret %d", ret);
360
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700361 return ret;
362}
363EXPORT_SYMBOL_GPL(if_usb_reset_device);
364
365/**
366 * usb_tx_block - transfer data to the device
367 *
368 * @priv pointer to struct lbtf_private
369 * @payload pointer to payload data
370 * @nb data length
371 * @data non-zero for data, zero for commands
372 *
373 * Returns: 0 on success, nonzero otherwise.
374 */
375static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
376 uint16_t nb, u8 data)
377{
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700378 int ret = -1;
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700379 struct urb *urb;
380
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700381 lbtf_deb_enter(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700382 /* check if device is removed */
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700383 if (cardp->priv->surpriseremoved) {
384 lbtf_deb_usbd(&cardp->udev->dev, "Device removed\n");
385 goto tx_ret;
386 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700387
388 if (data)
389 urb = cardp->tx_urb;
390 else
391 urb = cardp->cmd_urb;
392
393 usb_fill_bulk_urb(urb, cardp->udev,
394 usb_sndbulkpipe(cardp->udev,
395 cardp->ep_out),
396 payload, nb, if_usb_write_bulk_callback, cardp);
397
398 urb->transfer_flags |= URB_ZERO_PACKET;
399
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700400 if (usb_submit_urb(urb, GFP_ATOMIC)) {
401 lbtf_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
402 goto tx_ret;
403 }
404
405 lbtf_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
406
407 ret = 0;
408
409tx_ret:
410 lbtf_deb_leave(LBTF_DEB_USB);
411 return ret;
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700412}
413
414static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
415 void (*callbackfn)(struct urb *urb))
416{
417 struct sk_buff *skb;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700418 int ret = -1;
419
420 lbtf_deb_enter(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700421
422 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700423 if (!skb) {
424 pr_err("No free skb\n");
425 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700426 return -1;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700427 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700428
429 cardp->rx_skb = skb;
430
431 /* Fill the receive configuration URB and initialise the Rx call back */
432 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
433 usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
Johannes Berg9b44fb82008-10-29 23:24:14 +0100434 skb_tail_pointer(skb),
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700435 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn, cardp);
436
437 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
438
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700439 lbtf_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb);
440 ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC);
441 if (ret) {
442 lbtf_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700443 kfree_skb(skb);
444 cardp->rx_skb = NULL;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700445 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700446 return -1;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700447 } else {
448 lbtf_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
449 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700450 return 0;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700451 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700452}
453
454static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
455{
456 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
457}
458
459static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
460{
461 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
462}
463
464static void if_usb_receive_fwload(struct urb *urb)
465{
466 struct if_usb_card *cardp = urb->context;
467 struct sk_buff *skb = cardp->rx_skb;
468 struct fwsyncheader *syncfwheader;
469 struct bootcmdresp bcmdresp;
470
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700471 lbtf_deb_enter(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700472 if (urb->status) {
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700473 lbtf_deb_usbd(&cardp->udev->dev,
474 "URB status is failed during fw load\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700475 kfree_skb(skb);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700476 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700477 return;
478 }
479
480 if (cardp->fwdnldover) {
481 __le32 *tmp = (__le32 *)(skb->data);
482
483 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700484 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700485 /* Firmware ready event received */
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700486 pr_info("Firmware ready event received\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700487 wake_up(&cardp->fw_wq);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700488 } else {
489 lbtf_deb_usb("Waiting for confirmation; got %x %x\n",
490 le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700491 if_usb_submit_rx_urb_fwload(cardp);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700492 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700493 kfree_skb(skb);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700494 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700495 return;
496 }
497 if (cardp->bootcmdresp <= 0) {
498 memcpy(&bcmdresp, skb->data, sizeof(bcmdresp));
499
500 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
501 kfree_skb(skb);
502 if_usb_submit_rx_urb_fwload(cardp);
503 cardp->bootcmdresp = 1;
504 /* Received valid boot command response */
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700505 lbtf_deb_usbd(&cardp->udev->dev,
506 "Received valid boot command response\n");
507 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700508 return;
509 }
510 if (bcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
511 if (bcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
512 bcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700513 bcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
514 if (!cardp->bootcmdresp)
515 pr_info("Firmware already seems alive; resetting\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700516 cardp->bootcmdresp = -1;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700517 } else {
518 pr_info("boot cmd response wrong magic number (0x%x)\n",
519 le32_to_cpu(bcmdresp.magic));
520 }
521 } else if (bcmdresp.cmd != BOOT_CMD_FW_BY_USB) {
522 pr_info("boot cmd response cmd_tag error (%d)\n",
523 bcmdresp.cmd);
524 } else if (bcmdresp.result != BOOT_CMD_RESP_OK) {
525 pr_info("boot cmd response result error (%d)\n",
526 bcmdresp.result);
527 } else {
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700528 cardp->bootcmdresp = 1;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700529 lbtf_deb_usbd(&cardp->udev->dev,
530 "Received valid boot command response\n");
531 }
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700532
533 kfree_skb(skb);
534 if_usb_submit_rx_urb_fwload(cardp);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700535 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700536 return;
537 }
538
539 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
540 if (!syncfwheader) {
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700541 lbtf_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700542 kfree_skb(skb);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700543 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700544 return;
545 }
546
547 memcpy(syncfwheader, skb->data, sizeof(struct fwsyncheader));
548
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700549 if (!syncfwheader->cmd) {
550 lbtf_deb_usb2(&cardp->udev->dev, "FW received Blk with correct CRC\n");
551 lbtf_deb_usb2(&cardp->udev->dev, "FW received Blk seqnum = %d\n",
552 le32_to_cpu(syncfwheader->seqnum));
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700553 cardp->CRC_OK = 1;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700554 } else {
555 lbtf_deb_usbd(&cardp->udev->dev, "FW received Blk with CRC error\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700556 cardp->CRC_OK = 0;
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700557 }
558
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700559 kfree_skb(skb);
560
561 /* reschedule timer for 200ms hence */
562 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
563
564 if (cardp->fwfinalblk) {
565 cardp->fwdnldover = 1;
566 goto exit;
567 }
568
569 if_usb_send_fw_pkt(cardp);
570
571 exit:
572 if_usb_submit_rx_urb_fwload(cardp);
573
574 kfree(syncfwheader);
575
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700576 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700577 return;
578}
579
580#define MRVDRV_MIN_PKT_LEN 30
581
582static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
583 struct if_usb_card *cardp,
584 struct lbtf_private *priv)
585{
586 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
587 || recvlength < MRVDRV_MIN_PKT_LEN) {
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700588 lbtf_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700589 kfree_skb(skb);
590 return;
591 }
592
593 skb_put(skb, recvlength);
594 skb_pull(skb, MESSAGE_HEADER_LEN);
595 lbtf_rx(priv, skb);
596}
597
598static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
599 struct sk_buff *skb,
600 struct if_usb_card *cardp,
601 struct lbtf_private *priv)
602{
603 if (recvlength > LBS_CMD_BUFFER_SIZE) {
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700604 lbtf_deb_usbd(&cardp->udev->dev,
605 "The receive buffer is too large\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700606 kfree_skb(skb);
607 return;
608 }
609
Alexander Beregalov0ee904c2009-04-11 14:50:23 +0000610 BUG_ON(!in_interrupt());
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700611
612 spin_lock(&priv->driver_lock);
613 memcpy(priv->cmd_resp_buff, recvbuff + MESSAGE_HEADER_LEN,
614 recvlength - MESSAGE_HEADER_LEN);
615 kfree_skb(skb);
616 lbtf_cmd_response_rx(priv);
617 spin_unlock(&priv->driver_lock);
618}
619
620/**
621 * if_usb_receive - read data received from the device.
622 *
623 * @urb pointer to struct urb
624 */
625static void if_usb_receive(struct urb *urb)
626{
627 struct if_usb_card *cardp = urb->context;
628 struct sk_buff *skb = cardp->rx_skb;
629 struct lbtf_private *priv = cardp->priv;
630 int recvlength = urb->actual_length;
631 uint8_t *recvbuff = NULL;
632 uint32_t recvtype = 0;
633 __le32 *pkt = (__le32 *) skb->data;
634
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700635 lbtf_deb_enter(LBTF_DEB_USB);
636
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700637 if (recvlength) {
638 if (urb->status) {
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700639 lbtf_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
640 urb->status);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700641 kfree_skb(skb);
642 goto setup_for_next;
643 }
644
645 recvbuff = skb->data;
646 recvtype = le32_to_cpu(pkt[0]);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700647 lbtf_deb_usbd(&cardp->udev->dev,
648 "Recv length = 0x%x, Recv type = 0x%X\n",
649 recvlength, recvtype);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700650 } else if (urb->status) {
651 kfree_skb(skb);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700652 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700653 return;
654 }
655
656 switch (recvtype) {
657 case CMD_TYPE_DATA:
658 process_cmdtypedata(recvlength, skb, cardp, priv);
659 break;
660
661 case CMD_TYPE_REQUEST:
662 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
663 break;
664
665 case CMD_TYPE_INDICATION:
666 {
667 /* Event cause handling */
668 u32 event_cause = le32_to_cpu(pkt[1]);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700669 lbtf_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n", event_cause);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700670
671 /* Icky undocumented magic special case */
672 if (event_cause & 0xffff0000) {
673 u16 tmp;
674 u8 retrycnt;
675 u8 failure;
676
677 tmp = event_cause >> 16;
678 retrycnt = tmp & 0x00ff;
679 failure = (tmp & 0xff00) >> 8;
680 lbtf_send_tx_feedback(priv, retrycnt, failure);
681 } else if (event_cause == LBTF_EVENT_BCN_SENT)
682 lbtf_bcn_sent(priv);
683 else
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700684 lbtf_deb_usbd(&cardp->udev->dev,
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700685 "Unsupported notification %d received\n",
686 event_cause);
687 kfree_skb(skb);
688 break;
689 }
690 default:
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700691 lbtf_deb_usbd(&cardp->udev->dev,
692 "libertastf: unknown command type 0x%X\n", recvtype);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700693 kfree_skb(skb);
694 break;
695 }
696
697setup_for_next:
698 if_usb_submit_rx_urb(cardp);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700699 lbtf_deb_leave(LBTF_DEB_USB);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700700}
701
702/**
703 * if_usb_host_to_card - Download data to the device
704 *
705 * @priv pointer to struct lbtf_private structure
706 * @type type of data
707 * @buf pointer to data buffer
708 * @len number of bytes
709 *
710 * Returns: 0 on success, nonzero otherwise
711 */
712static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type,
713 uint8_t *payload, uint16_t nb)
714{
715 struct if_usb_card *cardp = priv->card;
716 u8 data = 0;
717
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700718 lbtf_deb_usbd(&cardp->udev->dev, "*** type = %u\n", type);
719 lbtf_deb_usbd(&cardp->udev->dev, "size after = %d\n", nb);
720
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700721 if (type == MVMS_CMD) {
722 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
723 } else {
724 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
725 data = 1;
726 }
727
728 memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
729
730 return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN,
731 data);
732}
733
734/**
735 * if_usb_issue_boot_command - Issue boot command to Boot2.
736 *
737 * @ivalue 1 boots from FW by USB-Download, 2 boots from FW in EEPROM.
738 *
739 * Returns: 0
740 */
741static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
742{
743 struct bootcmd *bootcmd = cardp->ep_out_buf;
744
745 /* Prepare command */
746 bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
747 bootcmd->cmd = ivalue;
748 memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
749
750 /* Issue command */
751 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd), 0);
752
753 return 0;
754}
755
756
757/**
758 * check_fwfile_format - Check the validity of Boot2/FW image.
759 *
760 * @data pointer to image
761 * @totlen image length
762 *
763 * Returns: 0 if the image is valid, nonzero otherwise.
764 */
765static int check_fwfile_format(const u8 *data, u32 totlen)
766{
767 u32 bincmd, exit;
768 u32 blksize, offset, len;
769 int ret;
770
771 ret = 1;
772 exit = len = 0;
773
774 do {
775 struct fwheader *fwh = (void *) data;
776
777 bincmd = le32_to_cpu(fwh->dnldcmd);
778 blksize = le32_to_cpu(fwh->datalength);
779 switch (bincmd) {
780 case FW_HAS_DATA_TO_RECV:
781 offset = sizeof(struct fwheader) + blksize;
782 data += offset;
783 len += offset;
784 if (len >= totlen)
785 exit = 1;
786 break;
787 case FW_HAS_LAST_BLOCK:
788 exit = 1;
789 ret = 0;
790 break;
791 default:
792 exit = 1;
793 break;
794 }
795 } while (!exit);
796
797 if (ret)
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700798 pr_err("firmware file format check FAIL\n");
799 else
800 lbtf_deb_fw("firmware file format check PASS\n");
801
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700802 return ret;
803}
804
805
806static int if_usb_prog_firmware(struct if_usb_card *cardp)
807{
808 int i = 0;
809 static int reset_count = 10;
810 int ret = 0;
811
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700812 lbtf_deb_enter(LBTF_DEB_USB);
813
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700814 ret = request_firmware(&cardp->fw, lbtf_fw_name, &cardp->udev->dev);
815 if (ret < 0) {
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700816 pr_err("request_firmware() failed with %#x\n", ret);
817 pr_err("firmware %s not found\n", lbtf_fw_name);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700818 goto done;
819 }
820
821 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
822 goto release_fw;
823
824restart:
825 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700826 lbtf_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700827 ret = -1;
828 goto release_fw;
829 }
830
831 cardp->bootcmdresp = 0;
832 do {
833 int j = 0;
834 i++;
835 /* Issue Boot command = 1, Boot from Download-FW */
836 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
837 /* wait for command response */
838 do {
839 j++;
840 msleep_interruptible(100);
841 } while (cardp->bootcmdresp == 0 && j < 10);
842 } while (cardp->bootcmdresp == 0 && i < 5);
843
844 if (cardp->bootcmdresp <= 0) {
845 if (--reset_count >= 0) {
846 if_usb_reset_device(cardp);
847 goto restart;
848 }
849 return -1;
850 }
851
852 i = 0;
853
854 cardp->totalbytes = 0;
855 cardp->fwlastblksent = 0;
856 cardp->CRC_OK = 1;
857 cardp->fwdnldover = 0;
858 cardp->fwseqnum = -1;
859 cardp->totalbytes = 0;
860 cardp->fwfinalblk = 0;
861
862 /* Send the first firmware packet... */
863 if_usb_send_fw_pkt(cardp);
864
865 /* ... and wait for the process to complete */
866 wait_event_interruptible(cardp->fw_wq, cardp->priv->surpriseremoved ||
867 cardp->fwdnldover);
868
869 del_timer_sync(&cardp->fw_timeout);
870 usb_kill_urb(cardp->rx_urb);
871
872 if (!cardp->fwdnldover) {
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700873 pr_info("failed to load fw, resetting device!\n");
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700874 if (--reset_count >= 0) {
875 if_usb_reset_device(cardp);
876 goto restart;
877 }
878
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700879 pr_info("FW download failure, time = %d ms\n", i * 100);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700880 ret = -1;
881 goto release_fw;
882 }
883
884 cardp->priv->fw_ready = 1;
885
886 release_fw:
887 release_firmware(cardp->fw);
888 cardp->fw = NULL;
889
890 if_usb_setup_firmware(cardp->priv);
891
892 done:
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700893 lbtf_deb_leave_args(LBTF_DEB_USB, "ret %d", ret);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700894 return ret;
895}
896EXPORT_SYMBOL_GPL(if_usb_prog_firmware);
897
898
899#define if_usb_suspend NULL
900#define if_usb_resume NULL
901
902static struct usb_driver if_usb_driver = {
903 .name = DRV_NAME,
904 .probe = if_usb_probe,
905 .disconnect = if_usb_disconnect,
906 .id_table = if_usb_table,
907 .suspend = if_usb_suspend,
908 .resume = if_usb_resume,
909};
910
911static int __init if_usb_init_module(void)
912{
913 int ret = 0;
914
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700915 lbtf_deb_enter(LBTF_DEB_MAIN);
916
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700917 ret = usb_register(&if_usb_driver);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700918
919 lbtf_deb_leave_args(LBTF_DEB_MAIN, "ret %d", ret);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700920 return ret;
921}
922
923static void __exit if_usb_exit_module(void)
924{
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700925 lbtf_deb_enter(LBTF_DEB_MAIN);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700926 usb_deregister(&if_usb_driver);
Steve deRosiere9bd5bc2010-04-25 14:40:46 -0700927 lbtf_deb_leave(LBTF_DEB_MAIN);
Luis Carlos Coboc305a192008-08-14 10:41:06 -0700928}
929
930module_init(if_usb_init_module);
931module_exit(if_usb_exit_module);
932
933MODULE_DESCRIPTION("8388 USB WLAN Thinfirm Driver");
934MODULE_AUTHOR("Cozybit Inc.");
935MODULE_LICENSE("GPL");