update libertas driver
[openwrt.git] / package / libertas / src / if_usb.c
1 /**
2 * This file contains functions used in USB interface module.
3 */
4 #include <linux/delay.h>
5 #include <linux/moduleparam.h>
6 #include <linux/firmware.h>
7 #include <linux/netdevice.h>
8 #include <linux/usb.h>
9 //#include <asm/olpc.h>
10
11 #define DRV_NAME "usb8xxx"
12
13 #include "host.h"
14 #include "decl.h"
15 #include "defs.h"
16 #include "dev.h"
17 #include "cmd.h"
18 #include "if_usb.h"
19
20 #define INSANEDEBUG 0
21 #define lbs_deb_usb2(...) do { if (INSANEDEBUG) lbs_deb_usbd(__VA_ARGS__); } while (0)
22
23 #define MESSAGE_HEADER_LEN 4
24
25 static char *lbs_fw_name = "usb8388.bin";
26 module_param_named(fw_name, lbs_fw_name, charp, 0644);
27
28 static struct usb_device_id if_usb_table[] = {
29 /* Enter the device signature inside */
30 { USB_DEVICE(0x1286, 0x2001) },
31 { USB_DEVICE(0x05a3, 0x8388) },
32 {} /* Terminating entry */
33 };
34
35 MODULE_DEVICE_TABLE(usb, if_usb_table);
36
37 static void if_usb_receive(struct urb *urb);
38 static void if_usb_receive_fwload(struct urb *urb);
39 static int if_usb_prog_firmware(struct if_usb_card *cardp);
40 static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
41 uint8_t *payload, uint16_t nb);
42 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
43 uint16_t nb);
44 static void if_usb_free(struct if_usb_card *cardp);
45 static int if_usb_submit_rx_urb(struct if_usb_card *cardp);
46 static int if_usb_reset_device(struct if_usb_card *cardp);
47
48 /**
49 * @brief call back function to handle the status of the URB
50 * @param urb pointer to urb structure
51 * @return N/A
52 */
53 static void if_usb_write_bulk_callback(struct urb *urb)
54 {
55 struct if_usb_card *cardp = (struct if_usb_card *) urb->context;
56
57 /* handle the transmission complete validations */
58
59 if (urb->status == 0) {
60 struct lbs_private *priv = cardp->priv;
61
62 lbs_deb_usb2(&urb->dev->dev, "URB status is successful\n");
63 lbs_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
64 urb->actual_length);
65
66 /* Used for both firmware TX and regular TX. priv isn't
67 * valid at firmware load time.
68 */
69 if (priv)
70 lbs_host_to_card_done(priv);
71 } else {
72 /* print the failure status number for debug */
73 lbs_pr_info("URB in failure status: %d\n", urb->status);
74 }
75
76 return;
77 }
78
79 /**
80 * @brief free tx/rx urb, skb and rx buffer
81 * @param cardp pointer if_usb_card
82 * @return N/A
83 */
84 static void if_usb_free(struct if_usb_card *cardp)
85 {
86 lbs_deb_enter(LBS_DEB_USB);
87
88 /* Unlink tx & rx urb */
89 usb_kill_urb(cardp->tx_urb);
90 usb_kill_urb(cardp->rx_urb);
91
92 usb_free_urb(cardp->tx_urb);
93 cardp->tx_urb = NULL;
94
95 usb_free_urb(cardp->rx_urb);
96 cardp->rx_urb = NULL;
97
98 kfree(cardp->ep_out_buf);
99 cardp->ep_out_buf = NULL;
100
101 lbs_deb_leave(LBS_DEB_USB);
102 }
103
104 static void if_usb_setup_firmware(struct lbs_private *priv)
105 {
106 struct if_usb_card *cardp = priv->card;
107 struct cmd_ds_set_boot2_ver b2_cmd;
108 struct cmd_ds_802_11_fw_wake_method wake_method;
109
110 b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
111 b2_cmd.action = 0;
112 b2_cmd.version = cardp->boot2_version;
113
114 if (lbs_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
115 lbs_deb_usb("Setting boot2 version failed\n");
116
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 }
135 }
136
137 static void if_usb_fw_timeo(unsigned long priv)
138 {
139 struct if_usb_card *cardp = (void *)priv;
140
141 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 }
149
150 /**
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 */
156 static 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;
162 struct lbs_private *priv;
163 struct if_usb_card *cardp;
164 int i;
165
166 udev = interface_to_usbdev(intf);
167
168 cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
169 if (!cardp) {
170 lbs_pr_err("Out of memory allocating private data.\n");
171 goto error;
172 }
173
174 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
175 init_waitqueue_head(&cardp->fw_wq);
176
177 cardp->udev = udev;
178 iface_desc = intf->cur_altsetting;
179
180 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
181 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
182 le16_to_cpu(udev->descriptor.bcdUSB),
183 udev->descriptor.bDeviceClass,
184 udev->descriptor.bDeviceSubClass,
185 udev->descriptor.bDeviceProtocol);
186
187 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
188 endpoint = &iface_desc->endpoint[i].desc;
189 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);
202 }
203 }
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;
220 }
221
222 /* Upload firmware */
223 if (if_usb_prog_firmware(cardp)) {
224 lbs_deb_usbd(&udev->dev, "FW upload failed\n");
225 goto err_prog_firmware;
226 }
227
228 if (!(priv = lbs_add_card(cardp, &udev->dev)))
229 goto err_prog_firmware;
230
231 cardp->priv = priv;
232 cardp->priv->fw_ready = 1;
233
234 priv->hw_host_to_card = if_usb_host_to_card;
235 cardp->boot2_version = udev->descriptor.bcdDevice;
236
237 if_usb_submit_rx_urb(cardp);
238
239 if (lbs_start_card(priv))
240 goto err_start_card;
241
242 if_usb_setup_firmware(priv);
243
244 usb_get_dev(udev);
245 usb_set_intfdata(intf, cardp);
246
247 return 0;
248
249 err_start_card:
250 lbs_remove_card(priv);
251 err_prog_firmware:
252 if_usb_reset_device(cardp);
253 dealloc:
254 if_usb_free(cardp);
255
256 error:
257 return -ENOMEM;
258 }
259
260 /**
261 * @brief free resource and cleanup
262 * @param intf USB interface structure
263 * @return N/A
264 */
265 static void if_usb_disconnect(struct usb_interface *intf)
266 {
267 struct if_usb_card *cardp = usb_get_intfdata(intf);
268 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
269
270 lbs_deb_enter(LBS_DEB_MAIN);
271
272 cardp->surprise_removed = 1;
273
274 if (priv) {
275 priv->surpriseremoved = 1;
276 lbs_stop_card(priv);
277 lbs_remove_card(priv);
278 }
279
280 /* Unlink and free urb */
281 if_usb_free(cardp);
282
283 usb_set_intfdata(intf, NULL);
284 usb_put_dev(interface_to_usbdev(intf));
285
286 lbs_deb_leave(LBS_DEB_MAIN);
287 }
288
289 /**
290 * @brief This function download FW
291 * @param priv pointer to struct lbs_private
292 * @return 0
293 */
294 static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
295 {
296 struct fwdata *fwdata = cardp->ep_out_buf;
297 uint8_t *firmware = cardp->fw->data;
298
299 /* If we got a CRC failure on the last block, back
300 up and retry it */
301 if (!cardp->CRC_OK) {
302 cardp->totalbytes = cardp->fwlastblksent;
303 cardp->fwseqnum--;
304 }
305
306 lbs_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
307 cardp->totalbytes);
308
309 /* struct fwdata (which we sent to the card) has an
310 extra __le32 field in between the header and the data,
311 which is not in the struct fwheader in the actual
312 firmware binary. Insert the seqnum in the middle... */
313 memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
314 sizeof(struct fwheader));
315
316 cardp->fwlastblksent = cardp->totalbytes;
317 cardp->totalbytes += sizeof(struct fwheader);
318
319 memcpy(fwdata->data, &firmware[cardp->totalbytes],
320 le32_to_cpu(fwdata->hdr.datalength));
321
322 lbs_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
323 le32_to_cpu(fwdata->hdr.datalength));
324
325 fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
326 cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
327
328 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
329 le32_to_cpu(fwdata->hdr.datalength));
330
331 if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
332 lbs_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
333 lbs_deb_usb2(&cardp->udev->dev, "seqnum = %d totalbytes = %d\n",
334 cardp->fwseqnum, cardp->totalbytes);
335 } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
336 lbs_deb_usb2(&cardp->udev->dev, "Host has finished FW downloading\n");
337 lbs_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
338
339 cardp->fwfinalblk = 1;
340 }
341
342 lbs_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
343 cardp->totalbytes);
344
345 return 0;
346 }
347
348 static int if_usb_reset_device(struct if_usb_card *cardp)
349 {
350 struct cmd_ds_command *cmd = cardp->ep_out_buf + 4;
351 int ret;
352
353 lbs_deb_enter(LBS_DEB_USB);
354
355 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
356
357 cmd->command = cpu_to_le16(CMD_802_11_RESET);
358 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
359 cmd->result = cpu_to_le16(0);
360 cmd->seqnum = cpu_to_le16(0x5a5a);
361 cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
362 usb_tx_block(cardp, cardp->ep_out_buf, 4 + S_DS_GEN + sizeof(struct cmd_ds_802_11_reset));
363
364 msleep(100);
365 ret = usb_reset_device(cardp->udev);
366 msleep(100);
367
368 #ifdef CONFIG_OLPC
369 if (ret && machine_is_olpc()) {
370 printk(KERN_CRIT "Resetting OLPC wireless via EC...\n");
371 olpc_ec_cmd(0x25, NULL, 0, NULL, 0);
372 }
373 #endif
374
375 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
376
377 return ret;
378 }
379
380 /**
381 * @brief This function transfer the data to the device.
382 * @param priv pointer to struct lbs_private
383 * @param payload pointer to payload data
384 * @param nb data length
385 * @return 0 or -1
386 */
387 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, uint16_t nb)
388 {
389 int ret = -1;
390
391 /* check if device is removed */
392 if (cardp->surprise_removed) {
393 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
394 goto tx_ret;
395 }
396
397 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
398 usb_sndbulkpipe(cardp->udev,
399 cardp->ep_out),
400 payload, nb, if_usb_write_bulk_callback, cardp);
401
402 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
403
404 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
405 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
406 ret = -1;
407 } else {
408 lbs_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
409 ret = 0;
410 }
411
412 tx_ret:
413 return ret;
414 }
415
416 static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
417 void (*callbackfn)(struct urb *urb))
418 {
419 struct sk_buff *skb;
420 int ret = -1;
421
422 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
423 lbs_pr_err("No free skb\n");
424 goto rx_ret;
425 }
426
427 cardp->rx_skb = skb;
428
429 /* Fill the receive configuration URB and initialise the Rx call back */
430 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
431 usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
432 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
433 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
434 cardp);
435
436 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
437
438 lbs_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb);
439 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
440 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
441 kfree_skb(skb);
442 cardp->rx_skb = NULL;
443 ret = -1;
444 } else {
445 lbs_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
446 ret = 0;
447 }
448
449 rx_ret:
450 return ret;
451 }
452
453 static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
454 {
455 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
456 }
457
458 static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
459 {
460 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
461 }
462
463 static void if_usb_receive_fwload(struct urb *urb)
464 {
465 struct if_usb_card *cardp = urb->context;
466 struct sk_buff *skb = cardp->rx_skb;
467 struct fwsyncheader *syncfwheader;
468 struct bootcmdresp bootcmdresp;
469
470 if (urb->status) {
471 lbs_deb_usbd(&cardp->udev->dev,
472 "URB status is failed during fw load\n");
473 kfree_skb(skb);
474 return;
475 }
476
477 if (cardp->fwdnldover) {
478 __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
479
480 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
481 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
482 lbs_pr_info("Firmware ready event received\n");
483 wake_up(&cardp->fw_wq);
484 } else {
485 lbs_deb_usb("Waiting for confirmation; got %x %x\n",
486 le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
487 if_usb_submit_rx_urb_fwload(cardp);
488 }
489 kfree_skb(skb);
490 return;
491 }
492 if (cardp->bootcmdresp <= 0) {
493 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
494 sizeof(bootcmdresp));
495
496 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
497 kfree_skb(skb);
498 if_usb_submit_rx_urb_fwload(cardp);
499 cardp->bootcmdresp = 1;
500 lbs_deb_usbd(&cardp->udev->dev,
501 "Received valid boot command response\n");
502 return;
503 }
504 if (bootcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
505 if (bootcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
506 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
507 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
508 if (!cardp->bootcmdresp)
509 lbs_pr_info("Firmware already seems alive; resetting\n");
510 cardp->bootcmdresp = -1;
511 } else {
512 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
513 le32_to_cpu(bootcmdresp.magic));
514 }
515 } else if (bootcmdresp.cmd != BOOT_CMD_FW_BY_USB) {
516 lbs_pr_info("boot cmd response cmd_tag error (%d)\n",
517 bootcmdresp.cmd);
518 } else if (bootcmdresp.result != BOOT_CMD_RESP_OK) {
519 lbs_pr_info("boot cmd response result error (%d)\n",
520 bootcmdresp.result);
521 } else {
522 cardp->bootcmdresp = 1;
523 lbs_deb_usbd(&cardp->udev->dev,
524 "Received valid boot command response\n");
525 }
526 kfree_skb(skb);
527 if_usb_submit_rx_urb_fwload(cardp);
528 return;
529 }
530
531 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
532 if (!syncfwheader) {
533 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
534 kfree_skb(skb);
535 return;
536 }
537
538 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
539 sizeof(struct fwsyncheader));
540
541 if (!syncfwheader->cmd) {
542 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk with correct CRC\n");
543 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk seqnum = %d\n",
544 le32_to_cpu(syncfwheader->seqnum));
545 cardp->CRC_OK = 1;
546 } else {
547 lbs_deb_usbd(&cardp->udev->dev, "FW received Blk with CRC error\n");
548 cardp->CRC_OK = 0;
549 }
550
551 kfree_skb(skb);
552
553 /* reschedule timer for 200ms hence */
554 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
555
556 if (cardp->fwfinalblk) {
557 cardp->fwdnldover = 1;
558 goto exit;
559 }
560
561 if_usb_send_fw_pkt(cardp);
562
563 exit:
564 if_usb_submit_rx_urb_fwload(cardp);
565
566 kfree(syncfwheader);
567
568 return;
569 }
570
571 #define MRVDRV_MIN_PKT_LEN 30
572
573 static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
574 struct if_usb_card *cardp,
575 struct lbs_private *priv)
576 {
577 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
578 || recvlength < MRVDRV_MIN_PKT_LEN) {
579 lbs_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
580 kfree_skb(skb);
581 return;
582 }
583
584 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
585 skb_put(skb, recvlength);
586 skb_pull(skb, MESSAGE_HEADER_LEN);
587
588 lbs_process_rxed_packet(priv, skb);
589 }
590
591 static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
592 struct sk_buff *skb,
593 struct if_usb_card *cardp,
594 struct lbs_private *priv)
595 {
596 u8 i;
597
598 if (recvlength > LBS_CMD_BUFFER_SIZE) {
599 lbs_deb_usbd(&cardp->udev->dev,
600 "The receive buffer is too large\n");
601 kfree_skb(skb);
602 return;
603 }
604
605 if (!in_interrupt())
606 BUG();
607
608 spin_lock(&priv->driver_lock);
609
610 i = (priv->resp_idx == 0) ? 1 : 0;
611 BUG_ON(priv->resp_len[i]);
612 priv->resp_len[i] = (recvlength - MESSAGE_HEADER_LEN);
613 memcpy(priv->resp_buf[i], recvbuff + MESSAGE_HEADER_LEN,
614 priv->resp_len[i]);
615 kfree_skb(skb);
616 lbs_notify_command_response(priv, i);
617
618 spin_unlock(&priv->driver_lock);
619
620 lbs_deb_usbd(&cardp->udev->dev,
621 "Wake up main thread to handle cmd response\n");
622 }
623
624 /**
625 * @brief This function reads of the packet into the upload buff,
626 * wake up the main thread and initialise the Rx callack.
627 *
628 * @param urb pointer to struct urb
629 * @return N/A
630 */
631 static void if_usb_receive(struct urb *urb)
632 {
633 struct if_usb_card *cardp = urb->context;
634 struct sk_buff *skb = cardp->rx_skb;
635 struct lbs_private *priv = cardp->priv;
636 int recvlength = urb->actual_length;
637 uint8_t *recvbuff = NULL;
638 uint32_t recvtype = 0;
639 __le32 *pkt = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
640 uint32_t event;
641
642 lbs_deb_enter(LBS_DEB_USB);
643
644 if (recvlength) {
645 if (urb->status) {
646 lbs_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
647 urb->status);
648 kfree_skb(skb);
649 goto setup_for_next;
650 }
651
652 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
653 recvtype = le32_to_cpu(pkt[0]);
654 lbs_deb_usbd(&cardp->udev->dev,
655 "Recv length = 0x%x, Recv type = 0x%X\n",
656 recvlength, recvtype);
657 } else if (urb->status) {
658 kfree_skb(skb);
659 goto rx_exit;
660 }
661
662 switch (recvtype) {
663 case CMD_TYPE_DATA:
664 process_cmdtypedata(recvlength, skb, cardp, priv);
665 break;
666
667 case CMD_TYPE_REQUEST:
668 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
669 break;
670
671 case CMD_TYPE_INDICATION:
672 /* Event handling */
673 event = le32_to_cpu(pkt[1]);
674 lbs_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n", event);
675 kfree_skb(skb);
676
677 /* Icky undocumented magic special case */
678 if (event & 0xffff0000) {
679 u32 trycount = (event & 0xffff0000) >> 16;
680
681 lbs_send_tx_feedback(priv, trycount);
682 } else
683 lbs_queue_event(priv, event & 0xFF);
684 break;
685
686 default:
687 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
688 recvtype);
689 kfree_skb(skb);
690 break;
691 }
692
693 setup_for_next:
694 if_usb_submit_rx_urb(cardp);
695 rx_exit:
696 lbs_deb_leave(LBS_DEB_USB);
697 }
698
699 /**
700 * @brief This function downloads data to FW
701 * @param priv pointer to struct lbs_private structure
702 * @param type type of data
703 * @param buf pointer to data buffer
704 * @param len number of bytes
705 * @return 0 or -1
706 */
707 static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
708 uint8_t *payload, uint16_t nb)
709 {
710 struct if_usb_card *cardp = priv->card;
711
712 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
713 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
714
715 if (type == MVMS_CMD) {
716 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
717 priv->dnld_sent = DNLD_CMD_SENT;
718 } else {
719 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
720 priv->dnld_sent = DNLD_DATA_SENT;
721 }
722
723 memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
724
725 return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN);
726 }
727
728 /**
729 * @brief This function issues Boot command to the Boot2 code
730 * @param ivalue 1:Boot from FW by USB-Download
731 * 2:Boot from FW in EEPROM
732 * @return 0
733 */
734 static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
735 {
736 struct bootcmd *bootcmd = cardp->ep_out_buf;
737
738 /* Prepare command */
739 bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
740 bootcmd->cmd = ivalue;
741 memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
742
743 /* Issue command */
744 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd));
745
746 return 0;
747 }
748
749
750 /**
751 * @brief This function checks the validity of Boot2/FW image.
752 *
753 * @param data pointer to image
754 * len image length
755 * @return 0 or -1
756 */
757 static int check_fwfile_format(uint8_t *data, uint32_t totlen)
758 {
759 uint32_t bincmd, exit;
760 uint32_t blksize, offset, len;
761 int ret;
762
763 ret = 1;
764 exit = len = 0;
765
766 do {
767 struct fwheader *fwh = (void *)data;
768
769 bincmd = le32_to_cpu(fwh->dnldcmd);
770 blksize = le32_to_cpu(fwh->datalength);
771 switch (bincmd) {
772 case FW_HAS_DATA_TO_RECV:
773 offset = sizeof(struct fwheader) + blksize;
774 data += offset;
775 len += offset;
776 if (len >= totlen)
777 exit = 1;
778 break;
779 case FW_HAS_LAST_BLOCK:
780 exit = 1;
781 ret = 0;
782 break;
783 default:
784 exit = 1;
785 break;
786 }
787 } while (!exit);
788
789 if (ret)
790 lbs_pr_err("firmware file format check FAIL\n");
791 else
792 lbs_deb_fw("firmware file format check PASS\n");
793
794 return ret;
795 }
796
797
798 static int if_usb_prog_firmware(struct if_usb_card *cardp)
799 {
800 int i = 0;
801 static int reset_count = 10;
802 int ret = 0;
803
804 lbs_deb_enter(LBS_DEB_USB);
805
806 if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
807 &cardp->udev->dev)) < 0) {
808 lbs_pr_err("request_firmware() failed with %#x\n", ret);
809 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
810 goto done;
811 }
812
813 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
814 goto release_fw;
815
816 restart:
817 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
818 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
819 ret = -1;
820 goto release_fw;
821 }
822
823 cardp->bootcmdresp = 0;
824 do {
825 int j = 0;
826 i++;
827 /* Issue Boot command = 1, Boot from Download-FW */
828 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
829 /* wait for command response */
830 do {
831 j++;
832 msleep_interruptible(100);
833 } while (cardp->bootcmdresp == 0 && j < 10);
834 } while (cardp->bootcmdresp == 0 && i < 5);
835
836 if (cardp->bootcmdresp <= 0) {
837 if (--reset_count >= 0) {
838 if_usb_reset_device(cardp);
839 goto restart;
840 }
841 return -1;
842 }
843
844 i = 0;
845
846 cardp->totalbytes = 0;
847 cardp->fwlastblksent = 0;
848 cardp->CRC_OK = 1;
849 cardp->fwdnldover = 0;
850 cardp->fwseqnum = -1;
851 cardp->totalbytes = 0;
852 cardp->fwfinalblk = 0;
853
854 /* Send the first firmware packet... */
855 if_usb_send_fw_pkt(cardp);
856
857 /* ... and wait for the process to complete */
858 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
859
860 del_timer_sync(&cardp->fw_timeout);
861 usb_kill_urb(cardp->rx_urb);
862
863 if (!cardp->fwdnldover) {
864 lbs_pr_info("failed to load fw, resetting device!\n");
865 if (--reset_count >= 0) {
866 if_usb_reset_device(cardp);
867 goto restart;
868 }
869
870 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
871 ret = -1;
872 goto release_fw;
873 }
874
875 release_fw:
876 release_firmware(cardp->fw);
877 cardp->fw = NULL;
878
879 done:
880 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
881 return ret;
882 }
883
884
885 #ifdef CONFIG_PM
886 static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
887 {
888 struct if_usb_card *cardp = usb_get_intfdata(intf);
889 struct lbs_private *priv = cardp->priv;
890 int ret;
891
892 lbs_deb_enter(LBS_DEB_USB);
893
894 if (priv->psstate != PS_STATE_FULL_POWER)
895 return -1;
896
897 ret = lbs_suspend(priv);
898 if (ret)
899 goto out;
900
901 /* Unlink tx & rx urb */
902 usb_kill_urb(cardp->tx_urb);
903 usb_kill_urb(cardp->rx_urb);
904
905 out:
906 lbs_deb_leave(LBS_DEB_USB);
907 return ret;
908 }
909
910 static int if_usb_resume(struct usb_interface *intf)
911 {
912 struct if_usb_card *cardp = usb_get_intfdata(intf);
913 struct lbs_private *priv = cardp->priv;
914
915 lbs_deb_enter(LBS_DEB_USB);
916
917 if_usb_submit_rx_urb(cardp);
918
919 lbs_resume(priv);
920
921 lbs_deb_leave(LBS_DEB_USB);
922 return 0;
923 }
924 #else
925 #define if_usb_suspend NULL
926 #define if_usb_resume NULL
927 #endif
928
929 static struct usb_driver if_usb_driver = {
930 .name = DRV_NAME,
931 .probe = if_usb_probe,
932 .disconnect = if_usb_disconnect,
933 .id_table = if_usb_table,
934 .suspend = if_usb_suspend,
935 .resume = if_usb_resume,
936 .reset_resume = if_usb_resume,
937 };
938
939 static int __init if_usb_init_module(void)
940 {
941 int ret = 0;
942
943 lbs_deb_enter(LBS_DEB_MAIN);
944
945 ret = usb_register(&if_usb_driver);
946
947 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
948 return ret;
949 }
950
951 static void __exit if_usb_exit_module(void)
952 {
953 lbs_deb_enter(LBS_DEB_MAIN);
954
955 usb_deregister(&if_usb_driver);
956
957 lbs_deb_leave(LBS_DEB_MAIN);
958 }
959
960 module_init(if_usb_init_module);
961 module_exit(if_usb_exit_module);
962
963 MODULE_DESCRIPTION("8388 USB WLAN Driver");
964 MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
965 MODULE_LICENSE("GPL");
This page took 0.094358 seconds and 5 git commands to generate.