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