hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Dongle BUS interface for USB, OS independent
 *
 * Copyright (C) 1999-2016, Broadcom Corporation
 * 
 *      Unless you and Broadcom execute a separate written software license
 * agreement governing use of this software, this software is licensed to you
 * under the terms of the GNU General Public License version 2 (the "GPL"),
 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
 * following added to such license:
 * 
 *      As a special exception, the copyright holders of this software give you
 * permission to link this software with independent modules, and to copy and
 * distribute the resulting executable under terms of your choice, provided that
 * you also meet, for each linked independent module, the terms and conditions of
 * the license of that module.  An independent module is a module which is not
 * derived from this software.  The special exception does not apply to any
 * modifications of the software.
 * 
 *      Notwithstanding the above, under no circumstances may you combine this
 * software in any way with any other Broadcom software provided under a license
 * other than the GPL, without Broadcom's express prior written consent.
 *
 *
 * <<Broadcom-WL-IPTag/Open:>>
 *
 * $Id: dbus_usb.c 565557 2015-06-22 19:29:44Z $
 */
 
/**
 * @file @brief
 * This file contains DBUS code that is USB, but not OS specific. DBUS is a Broadcom proprietary
 * host specific abstraction layer.
 */
 
#include <osl.h>
#include <bcmdefs.h>
#include <bcmutils.h>
#include <dbus.h>
#include <usbrdl.h>
#include <bcmdevs.h>
#include <bcmendian.h>
 
uint dbus_msglevel = DBUS_ERROR_VAL;
module_param(dbus_msglevel, int, 0);
 
 
#define USB_DLIMAGE_RETRY_TIMEOUT    3000    /* retry Timeout */
#define USB_SFLASH_DLIMAGE_SPINWAIT  150     /* in unit of ms */
#define USB_SFLASH_DLIMAGE_LIMIT     2000    /* spinwait limit (ms) */
#define POSTBOOT_ID                  0xA123  /* ID to detect if dongle has boot up */
#define USB_RESETCFG_SPINWAIT        1       /* wait after resetcfg (ms) */
#define USB_DEV_ISBAD(u)             (u->pub->attrib.devid == 0xDEAD)
#define USB_DLGO_SPINWAIT            100     /* wait after DL_GO (ms) */
#define TEST_CHIP                    0x4328
 
typedef struct {
   dbus_pub_t  *pub;
 
   void        *cbarg;
   dbus_intf_callbacks_t *cbs;  /** callbacks into higher DBUS level (dbus.c) */
   dbus_intf_t *drvintf;
   void        *usbosl_info;
   uint32      rdlram_base_addr;
   uint32      rdlram_size;
} usb_info_t;
 
/*
 * Callbacks common to all USB
 */
static void dbus_usb_disconnect(void *handle);
static void dbus_usb_send_irb_timeout(void *handle, dbus_irb_tx_t *txirb);
static void dbus_usb_send_irb_complete(void *handle, dbus_irb_tx_t *txirb, int status);
static void dbus_usb_recv_irb_complete(void *handle, dbus_irb_rx_t *rxirb, int status);
static void dbus_usb_errhandler(void *handle, int err);
static void dbus_usb_ctl_complete(void *handle, int type, int status);
static void dbus_usb_state_change(void *handle, int state);
static struct dbus_irb* dbus_usb_getirb(void *handle, bool send);
static void dbus_usb_rxerr_indicate(void *handle, bool on);
#if !defined(BCM_REQUEST_FW)
static int dbus_usb_resetcfg(usb_info_t *usbinfo);
#endif
static int dbus_usb_iovar_op(void *bus, const char *name,
   void *params, int plen, void *arg, int len, bool set);
static int dbus_iovar_process(usb_info_t* usbinfo, const char *name,
                 void *params, int plen, void *arg, int len, bool set);
static int dbus_usb_doiovar(usb_info_t *bus, const bcm_iovar_t *vi, uint32 actionid,
   const char *name, void *params, int plen, void *arg, int len, int val_size);
static int dhdusb_downloadvars(usb_info_t *bus, void *arg, int len);
 
static int dbus_usb_dl_writeimage(usb_info_t *usbinfo, uint8 *fw, int fwlen);
static int dbus_usb_dlstart(void *bus, uint8 *fw, int len);
static int dbus_usb_dlneeded(void *bus);
static int dbus_usb_dlrun(void *bus);
static int dbus_usb_rdl_dwnld_state(usb_info_t *usbinfo);
 
 
/* OS specific */
extern bool dbus_usbos_dl_cmd(void *info, uint8 cmd, void *buffer, int buflen);
extern int dbus_usbos_wait(void *info, uint16 ms);
extern int dbus_write_membytes(usb_info_t *usbinfo, bool set, uint32 address,
   uint8 *data, uint size);
extern bool dbus_usbos_dl_send_bulk(void *info, void *buffer, int len);
extern int dbus_usbos_loopback_tx(void *usbos_info_ptr, int cnt, int size);
 
/**
 * These functions are called by the lower DBUS level (dbus_usb_os.c) to notify this DBUS level
 * (dbus_usb.c) of an event.
 */
static dbus_intf_callbacks_t dbus_usb_intf_cbs = {
   dbus_usb_send_irb_timeout,
   dbus_usb_send_irb_complete,
   dbus_usb_recv_irb_complete,
   dbus_usb_errhandler,
   dbus_usb_ctl_complete,
   dbus_usb_state_change,
   NULL,            /* isr */
   NULL,            /* dpc */
   NULL,            /* watchdog */
   NULL,            /* dbus_if_pktget */
   NULL,             /* dbus_if_pktfree */
   dbus_usb_getirb,
   dbus_usb_rxerr_indicate
};
 
/* IOVar table */
enum {
   IOV_SET_DOWNLOAD_STATE = 1,
   IOV_DBUS_MSGLEVEL,
   IOV_MEMBYTES,
   IOV_VARS,
   IOV_LOOPBACK_TX
};
 
const bcm_iovar_t dhdusb_iovars[] = {
   {"vars",    IOV_VARS,    0,    IOVT_BUFFER,    0 },
   {"dbus_msglevel",    IOV_DBUS_MSGLEVEL,    0,    IOVT_UINT32,    0 },
   {"dwnldstate",    IOV_SET_DOWNLOAD_STATE,    0,    IOVT_BOOL,    0 },
   {"membytes",    IOV_MEMBYTES,    0,    IOVT_BUFFER,    2 * sizeof(int) },
   {"usb_lb_txfer", IOV_LOOPBACK_TX, 0,    IOVT_BUFFER,    2 * sizeof(int) },
   {NULL, 0, 0, 0, 0 }
};
 
/*
 * Need global for probe() and disconnect() since
 * attach() is not called at probe and detach()
 * can be called inside disconnect()
 */
static probe_cb_t    probe_cb = NULL;
static disconnect_cb_t    disconnect_cb = NULL;
static void        *probe_arg = NULL;
static void        *disc_arg = NULL;
static dbus_intf_t    *g_dbusintf = NULL;
static dbus_intf_t    dbus_usb_intf; /** functions called by higher layer DBUS into lower layer */
 
/*
 * dbus_intf_t common to all USB
 * These functions override dbus_usb_<os>.c.
 */
static void *dbus_usb_attach(dbus_pub_t *pub, void *cbarg, dbus_intf_callbacks_t *cbs);
static void dbus_usb_detach(dbus_pub_t *pub, void *info);
static void * dbus_usb_probe(void *arg, const char *desc, uint32 bustype,
   uint16 bus_no, uint16 slot, uint32 hdrlen);
 
/* functions */
 
/**
 * As part of DBUS initialization/registration, the higher level DBUS (dbus.c) needs to know what
 * lower level DBUS functions to call (in both dbus_usb.c and dbus_usb_os.c).
 */
static void *
dbus_usb_probe(void *arg, const char *desc, uint32 bustype, uint16 bus_no,
   uint16 slot, uint32 hdrlen)
{
   DBUSTRACE(("%s(): \n", __FUNCTION__));
   if (probe_cb) {
 
       if (g_dbusintf != NULL) {
           /* First, initialize all lower-level functions as default
            * so that dbus.c simply calls directly to dbus_usb_os.c.
            */
           bcopy(g_dbusintf, &dbus_usb_intf, sizeof(dbus_intf_t));
 
           /* Second, selectively override functions we need, if any. */
           dbus_usb_intf.attach = dbus_usb_attach;
           dbus_usb_intf.detach = dbus_usb_detach;
           dbus_usb_intf.iovar_op = dbus_usb_iovar_op;
           dbus_usb_intf.dlstart = dbus_usb_dlstart;
           dbus_usb_intf.dlneeded = dbus_usb_dlneeded;
           dbus_usb_intf.dlrun = dbus_usb_dlrun;
       }
 
       disc_arg = probe_cb(probe_arg, "DBUS USB", USB_BUS, bus_no, slot, hdrlen);
       return disc_arg;
   }
 
   return NULL;
}
 
/**
 * On return, *intf contains this or lower-level DBUS functions to be called by higher
 * level (dbus.c)
 */
int
dbus_bus_register(int vid, int pid, probe_cb_t prcb,
   disconnect_cb_t discb, void *prarg, dbus_intf_t **intf, void *param1, void *param2)
{
   int err;
 
   DBUSTRACE(("%s(): \n", __FUNCTION__));
   probe_cb = prcb;
   disconnect_cb = discb;
   probe_arg = prarg;
 
   *intf = &dbus_usb_intf;
 
   err = dbus_bus_osl_register(vid, pid, dbus_usb_probe,
       dbus_usb_disconnect, NULL, &g_dbusintf, param1, param2);
 
   ASSERT(g_dbusintf);
   return err;
}
 
int
dbus_bus_deregister()
{
   DBUSTRACE(("%s(): \n", __FUNCTION__));
   return dbus_bus_osl_deregister();
}
 
/** initialization consists of registration followed by 'attach'. */
void *
dbus_usb_attach(dbus_pub_t *pub, void *cbarg, dbus_intf_callbacks_t *cbs)
{
   usb_info_t *usb_info;
 
   DBUSTRACE(("%s(): \n", __FUNCTION__));
 
   if ((g_dbusintf == NULL) || (g_dbusintf->attach == NULL))
       return NULL;
 
   /* Sanity check for BUS_INFO() */
   ASSERT(OFFSETOF(usb_info_t, pub) == 0);
 
   usb_info = MALLOC(pub->osh, sizeof(usb_info_t));
   if (usb_info == NULL)
       return NULL;
 
   bzero(usb_info, sizeof(usb_info_t));
 
   usb_info->pub = pub;
   usb_info->cbarg = cbarg;
   usb_info->cbs = cbs;
 
   usb_info->usbosl_info = (dbus_pub_t *)g_dbusintf->attach(pub,
       usb_info, &dbus_usb_intf_cbs);
   if (usb_info->usbosl_info == NULL) {
       MFREE(pub->osh, usb_info, sizeof(usb_info_t));
       return NULL;
   }
 
   /* Save USB OS-specific driver entry points */
   usb_info->drvintf = g_dbusintf;
 
   pub->bus = usb_info;
#if !defined(BCM_REQUEST_FW)
   if (!dbus_usb_resetcfg(usb_info)) {
       usb_info->pub->busstate = DBUS_STATE_DL_DONE;
   }
#endif
   /* Return Lower layer info */
   return (void *) usb_info->usbosl_info;
}
 
void
dbus_usb_detach(dbus_pub_t *pub, void *info)
{
   usb_info_t *usb_info = (usb_info_t *) pub->bus;
   osl_t *osh = pub->osh;
 
   if (usb_info == NULL)
       return;
 
   if (usb_info->drvintf && usb_info->drvintf->detach)
       usb_info->drvintf->detach(pub, usb_info->usbosl_info);
 
   MFREE(osh, usb_info, sizeof(usb_info_t));
}
 
void
dbus_usb_disconnect(void *handle)
{
   DBUSTRACE(("%s(): \n", __FUNCTION__));
   if (disconnect_cb)
       disconnect_cb(disc_arg);
}
 
/**
 * When the lower DBUS level (dbus_usb_os.c) signals this event, the higher DBUS level has to be
 * notified.
 */
static void
dbus_usb_send_irb_timeout(void *handle, dbus_irb_tx_t *txirb)
{
   usb_info_t *usb_info = (usb_info_t *) handle;
 
   DBUSTRACE(("%s\n", __FUNCTION__));
 
   if (usb_info == NULL)
       return;
 
   if (usb_info->cbs && usb_info->cbs->send_irb_timeout)
       usb_info->cbs->send_irb_timeout(usb_info->cbarg, txirb);
}
 
/**
 * When the lower DBUS level (dbus_usb_os.c) signals this event, the higher DBUS level has to be
 * notified.
 */
static void
dbus_usb_send_irb_complete(void *handle, dbus_irb_tx_t *txirb, int status)
{
   usb_info_t *usb_info = (usb_info_t *) handle;
 
   if (usb_info == NULL)
       return;
 
   if (usb_info->cbs && usb_info->cbs->send_irb_complete)
       usb_info->cbs->send_irb_complete(usb_info->cbarg, txirb, status);
}
 
/**
 * When the lower DBUS level (dbus_usb_os.c) signals this event, the higher DBUS level has to be
 * notified.
 */
static void
dbus_usb_recv_irb_complete(void *handle, dbus_irb_rx_t *rxirb, int status)
{
   usb_info_t *usb_info = (usb_info_t *) handle;
 
   if (usb_info == NULL)
       return;
 
   if (usb_info->cbs && usb_info->cbs->recv_irb_complete)
       usb_info->cbs->recv_irb_complete(usb_info->cbarg, rxirb, status);
}
 
/** Lower DBUS level (dbus_usb_os.c) requests a free IRB. Pass this on to the higher DBUS level. */
static struct dbus_irb*
dbus_usb_getirb(void *handle, bool send)
{
   usb_info_t *usb_info = (usb_info_t *) handle;
 
   if (usb_info == NULL)
       return NULL;
 
   if (usb_info->cbs && usb_info->cbs->getirb)
       return usb_info->cbs->getirb(usb_info->cbarg, send);
 
   return NULL;
}
 
/**
 * When the lower DBUS level (dbus_usb_os.c) signals this event, the higher DBUS level has to be
 * notified.
 */
static void
dbus_usb_rxerr_indicate(void *handle, bool on)
{
   usb_info_t *usb_info = (usb_info_t *) handle;
 
   if (usb_info == NULL)
       return;
 
   if (usb_info->cbs && usb_info->cbs->rxerr_indicate)
       usb_info->cbs->rxerr_indicate(usb_info->cbarg, on);
}
 
/**
 * When the lower DBUS level (dbus_usb_os.c) signals this event, the higher DBUS level has to be
 * notified.
 */
static void
dbus_usb_errhandler(void *handle, int err)
{
   usb_info_t *usb_info = (usb_info_t *) handle;
 
   if (usb_info == NULL)
       return;
 
   if (usb_info->cbs && usb_info->cbs->errhandler)
       usb_info->cbs->errhandler(usb_info->cbarg, err);
}
 
/**
 * When the lower DBUS level (dbus_usb_os.c) signals this event, the higher DBUS level has to be
 * notified.
 */
static void
dbus_usb_ctl_complete(void *handle, int type, int status)
{
   usb_info_t *usb_info = (usb_info_t *) handle;
 
   DBUSTRACE(("%s\n", __FUNCTION__));
 
   if (usb_info == NULL) {
       DBUSERR(("%s: usb_info is NULL\n", __FUNCTION__));
       return;
   }
 
   if (usb_info->cbs && usb_info->cbs->ctl_complete)
       usb_info->cbs->ctl_complete(usb_info->cbarg, type, status);
}
 
/**
 * When the lower DBUS level (dbus_usb_os.c) signals this event, the higher DBUS level has to be
 * notified.
 */
static void
dbus_usb_state_change(void *handle, int state)
{
   usb_info_t *usb_info = (usb_info_t *) handle;
 
   if (usb_info == NULL)
       return;
 
   if (usb_info->cbs && usb_info->cbs->state_change)
       usb_info->cbs->state_change(usb_info->cbarg, state);
}
 
/** called by higher DBUS level (dbus.c) */
static int
dbus_usb_iovar_op(void *bus, const char *name,
   void *params, int plen, void *arg, int len, bool set)
{
   int err = DBUS_OK;
 
   err = dbus_iovar_process((usb_info_t*)bus, name, params, plen, arg, len, set);
   return err;
}
 
/** process iovar request from higher DBUS level */
static int
dbus_iovar_process(usb_info_t* usbinfo, const char *name,
                 void *params, int plen, void *arg, int len, bool set)
{
   const bcm_iovar_t *vi = NULL;
   int bcmerror = 0;
   int val_size;
   uint32 actionid;
 
   DBUSTRACE(("%s: Enter\n", __FUNCTION__));
 
   ASSERT(name);
   ASSERT(len >= 0);
 
   /* Get MUST have return space */
   ASSERT(set || (arg && len));
 
   /* Set does NOT take qualifiers */
   ASSERT(!set || (!params && !plen));
 
   /* Look up var locally; if not found pass to host driver */
   if ((vi = bcm_iovar_lookup(dhdusb_iovars, name)) == NULL) {
       /* Not Supported */
       bcmerror = BCME_UNSUPPORTED;
       DBUSTRACE(("%s: IOVAR %s is not supported\n", name, __FUNCTION__));
       goto exit;
 
   }
 
   DBUSTRACE(("%s: %s %s, len %d plen %d\n", __FUNCTION__,
            name, (set ? "set" : "get"), len, plen));
 
   /* set up 'params' pointer in case this is a set command so that
    * the convenience int and bool code can be common to set and get
    */
   if (params == NULL) {
       params = arg;
       plen = len;
   }
 
   if (vi->type == IOVT_VOID)
       val_size = 0;
   else if (vi->type == IOVT_BUFFER)
       val_size = len;
   else
       /* all other types are integer sized */
       val_size = sizeof(int);
 
   actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
   bcmerror = dbus_usb_doiovar(usbinfo, vi, actionid,
       name, params, plen, arg, len, val_size);
 
exit:
   return bcmerror;
} /* dbus_iovar_process */
 
static int
dbus_usb_doiovar(usb_info_t *bus, const bcm_iovar_t *vi, uint32 actionid, const char *name,
                void *params, int plen, void *arg, int len, int val_size)
{
   int bcmerror = 0;
   int32 int_val = 0;
   int32 int_val2 = 0;
   bool bool_val = 0;
 
   DBUSTRACE(("%s: Enter, action %d name %s params %p plen %d arg %p len %d val_size %d\n",
              __FUNCTION__, actionid, name, params, plen, arg, len, val_size));
 
   if ((bcmerror = bcm_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid))) != 0)
       goto exit;
 
   if (plen >= (int)sizeof(int_val))
       bcopy(params, &int_val, sizeof(int_val));
 
   if (plen >= (int)sizeof(int_val) * 2)
       bcopy((void*)((uintptr)params + sizeof(int_val)), &int_val2, sizeof(int_val2));
 
   bool_val = (int_val != 0) ? TRUE : FALSE;
 
   switch (actionid) {
 
   case IOV_SVAL(IOV_MEMBYTES):
   case IOV_GVAL(IOV_MEMBYTES):
   {
       uint32 address;
       uint size, dsize;
       uint8 *data;
 
       bool set = (actionid == IOV_SVAL(IOV_MEMBYTES));
 
       ASSERT(plen >= 2*sizeof(int));
 
       address = (uint32)int_val;
       BCM_REFERENCE(address);
       bcopy((char *)params + sizeof(int_val), &int_val, sizeof(int_val));
       size = (uint)int_val;
 
       /* Do some validation */
       dsize = set ? plen - (2 * sizeof(int)) : len;
       if (dsize < size) {
           DBUSTRACE(("%s: error on %s membytes, addr 0x%08x size %d dsize %d\n",
                      __FUNCTION__, (set ? "set" : "get"), address, size, dsize));
           bcmerror = BCME_BADARG;
           break;
       }
       DBUSTRACE(("%s: Request to %s %d bytes at address 0x%08x\n", __FUNCTION__,
                 (set ? "write" : "read"), size, address));
 
       /* Generate the actual data pointer */
       data = set ? (uint8*)params + 2 * sizeof(int): (uint8*)arg;
 
       /* Call to do the transfer */
       bcmerror = dbus_usb_dl_writeimage(BUS_INFO(bus, usb_info_t), data, size);
   }
       break;
 
 
   case IOV_SVAL(IOV_SET_DOWNLOAD_STATE):
 
       if (bool_val == TRUE) {
           bcmerror = dbus_usb_dlneeded(bus);
           dbus_usb_rdl_dwnld_state(BUS_INFO(bus, usb_info_t));
       } else {
           usb_info_t *usbinfo = BUS_INFO(bus, usb_info_t);
           bcmerror = dbus_usb_dlrun(bus);
           usbinfo->pub->busstate = DBUS_STATE_DL_DONE;
       }
       break;
 
   case IOV_SVAL(IOV_VARS):
       bcmerror = dhdusb_downloadvars(BUS_INFO(bus, usb_info_t), arg, len);
       break;
 
   case IOV_GVAL(IOV_DBUS_MSGLEVEL):
       int_val = (int32)dbus_msglevel;
       bcopy(&int_val, arg, val_size);
       break;
 
   case IOV_SVAL(IOV_DBUS_MSGLEVEL):
       dbus_msglevel = int_val;
       break;
 
#ifdef DBUS_USB_LOOPBACK
   case IOV_SVAL(IOV_LOOPBACK_TX):
           bcmerror = dbus_usbos_loopback_tx(BUS_INFO(bus, usb_info_t), int_val,
             int_val2);
           break;
#endif
   default:
       bcmerror = BCME_UNSUPPORTED;
       break;
   }
 
exit:
   return bcmerror;
} /* dbus_usb_doiovar */
 
/** higher DBUS level (dbus.c) wants to set NVRAM variables in dongle */
static int
dhdusb_downloadvars(usb_info_t *bus, void *arg, int len)
{
   int bcmerror = 0;
   uint32 varsize;
   uint32 varaddr;
   uint32 varsizew;
 
   if (!len) {
       bcmerror = BCME_BUFTOOSHORT;
       goto err;
   }
 
   /* RAM size is not set. Set it at dbus_usb_dlneeded */
   if (!bus->rdlram_size)
       bcmerror = BCME_ERROR;
 
   /* Even if there are no vars are to be written, we still need to set the ramsize. */
   varsize = len ? ROUNDUP(len, 4) : 0;
   varaddr = (bus->rdlram_size - 4) - varsize;
 
   /* Write the vars list */
   DBUSTRACE(("WriteVars: @%x varsize=%d\n", varaddr, varsize));
   bcmerror = dbus_write_membytes(bus->usbosl_info, TRUE, (varaddr + bus->rdlram_base_addr),
       arg, varsize);
 
   /* adjust to the user specified RAM */
   DBUSTRACE(("Usable memory size: %d\n", bus->rdlram_size));
   DBUSTRACE(("Vars are at %d, orig varsize is %d\n", varaddr, varsize));
 
   varsize = ((bus->rdlram_size - 4) - varaddr);
 
   /*
    * Determine the length token:
    * Varsize, converted to words, in lower 16-bits, checksum in upper 16-bits.
    */
   if (bcmerror) {
       varsizew = 0;
   } else {
       varsizew = varsize / 4;
       varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
       varsizew = htol32(varsizew);
   }
 
   DBUSTRACE(("New varsize is %d, length token=0x%08x\n", varsize, varsizew));
 
   /* Write the length token to the last word */
   bcmerror = dbus_write_membytes(bus->usbosl_info, TRUE, ((bus->rdlram_size - 4) +
       bus->rdlram_base_addr), (uint8*)&varsizew, 4);
err:
   return bcmerror;
} /* dbus_usb_doiovar */
 
#if !defined(BCM_REQUEST_FW)
/**
 * After downloading firmware into dongle and starting it, we need to know if the firmware is
 * indeed up and running.
 */
static int
dbus_usb_resetcfg(usb_info_t *usbinfo)
{
   void *osinfo;
   bootrom_id_t id;
   uint16 waittime = 0;
 
   uint32 starttime = 0;
   uint32 endtime = 0;
 
   DBUSTRACE(("%s\n", __FUNCTION__));
 
   if (usbinfo == NULL)
       return DBUS_ERR;
 
   osinfo = usbinfo->usbosl_info;
   ASSERT(osinfo);
 
   /* Give dongle chance to boot */
   dbus_usbos_wait(osinfo, USB_SFLASH_DLIMAGE_SPINWAIT);
   waittime = USB_SFLASH_DLIMAGE_SPINWAIT;
   while (waittime < USB_DLIMAGE_RETRY_TIMEOUT) {
 
       starttime = OSL_SYSUPTIME();
 
       id.chip = 0xDEAD;       /* Get the ID */
       dbus_usbos_dl_cmd(osinfo, DL_GETVER, &id, sizeof(bootrom_id_t));
       id.chip = ltoh32(id.chip);
 
       endtime = OSL_SYSUPTIME();
       waittime += (endtime - starttime);
 
       if (id.chip == POSTBOOT_ID)
           break;
   }
 
   if (id.chip == POSTBOOT_ID) {
       DBUSERR(("%s: download done. Bootup time = %d ms postboot chip 0x%x/rev 0x%x\n",
           __FUNCTION__, waittime, id.chip, id.chiprev));
 
       dbus_usbos_dl_cmd(osinfo, DL_RESETCFG, &id, sizeof(bootrom_id_t));
 
       dbus_usbos_wait(osinfo, USB_RESETCFG_SPINWAIT);
       return DBUS_OK;
   } else {
       DBUSERR(("%s: Cannot talk to Dongle. Wait time = %d ms. Firmware is not UP \n",
           __FUNCTION__, waittime));
       return DBUS_ERR;
   }
 
   return DBUS_OK;
}
#endif
 
/** before firmware download, the dongle has to be prepared to receive the fw image */
static int
dbus_usb_rdl_dwnld_state(usb_info_t *usbinfo)
{
   void *osinfo = usbinfo->usbosl_info;
   rdl_state_t state;
   int err = DBUS_OK;
 
   /* 1) Prepare USB boot loader for runtime image */
   dbus_usbos_dl_cmd(osinfo, DL_START, &state, sizeof(rdl_state_t));
 
   state.state = ltoh32(state.state);
   state.bytes = ltoh32(state.bytes);
 
   /* 2) Check we are in the Waiting state */
   if (state.state != DL_WAITING) {
       DBUSERR(("%s: Failed to DL_START\n", __FUNCTION__));
       err = DBUS_ERR;
       goto fail;
   }
 
fail:
   return err;
}
 
/**
 * Dongle contains bootcode in ROM but firmware is (partially) contained in dongle RAM. Therefore,
 * firmware has to be downloaded into dongle RAM.
 */
static int
dbus_usb_dl_writeimage(usb_info_t *usbinfo, uint8 *fw, int fwlen)
{
   osl_t *osh = usbinfo->pub->osh;
   void *osinfo = usbinfo->usbosl_info;
   unsigned int sendlen, sent, dllen;
   char *bulkchunk = NULL, *dlpos;
   rdl_state_t state;
   int err = DBUS_OK;
   bootrom_id_t id;
   uint16 wait, wait_time;
   uint32 dl_trunk_size = RDL_CHUNK;
 
   if (BCM4350_CHIP(usbinfo->pub->attrib.devid))
       dl_trunk_size = RDL_CHUNK_MAX;
 
   while (!bulkchunk) {
       bulkchunk = MALLOC(osh, dl_trunk_size);
       if (dl_trunk_size == RDL_CHUNK)
           break;
       if (!bulkchunk) {
           dl_trunk_size /= 2;
           if (dl_trunk_size < RDL_CHUNK)
               dl_trunk_size = RDL_CHUNK;
       }
   }
 
   if (bulkchunk == NULL) {
       err = DBUS_ERR;
       goto fail;
   }
 
   sent = 0;
   dlpos = fw;
   dllen = fwlen;
 
   /* Get chip id and rev */
   id.chip = usbinfo->pub->attrib.devid;
   id.chiprev = usbinfo->pub->attrib.chiprev;
 
   DBUSTRACE(("enter %s: fwlen=%d\n", __FUNCTION__, fwlen));
 
   dbus_usbos_dl_cmd(osinfo, DL_GETSTATE, &state, sizeof(rdl_state_t));
 
   /* 3) Load the image */
   while ((sent < dllen)) {
       /* Wait until the usb device reports it received all the bytes we sent */
 
       if (sent < dllen) {
           if ((dllen-sent) < dl_trunk_size)
               sendlen = dllen-sent;
           else
               sendlen = dl_trunk_size;
 
           /* simply avoid having to send a ZLP by ensuring we never have an even
            * multiple of 64
            */
           if (!(sendlen % 64))
               sendlen -= 4;
 
           /* send data */
           memcpy(bulkchunk, dlpos, sendlen);
           if (!dbus_usbos_dl_send_bulk(osinfo, bulkchunk, sendlen)) {
               err = DBUS_ERR;
               goto fail;
           }
 
           dlpos += sendlen;
           sent += sendlen;
           DBUSTRACE(("%s: sendlen %d\n", __FUNCTION__, sendlen));
       }
 
       wait = 0;
       wait_time = USB_SFLASH_DLIMAGE_SPINWAIT;
       while (!dbus_usbos_dl_cmd(osinfo, DL_GETSTATE, &state,
           sizeof(rdl_state_t))) {
           if ((id.chip == 43236) && (id.chiprev == 0)) {
               DBUSERR(("%s: 43236a0 SFlash delay, waiting for dongle crc check "
                    "completion!!!\n", __FUNCTION__));
               dbus_usbos_wait(osinfo, wait_time);
               wait += wait_time;
               if (wait >= USB_SFLASH_DLIMAGE_LIMIT) {
                   DBUSERR(("%s: DL_GETSTATE Failed xxxx\n", __FUNCTION__));
                   err = DBUS_ERR;
                   goto fail;
                   break;
               }
           } else {
               DBUSERR(("%s: DL_GETSTATE Failed xxxx\n", __FUNCTION__));
               err = DBUS_ERR;
               goto fail;
           }
       }
 
       state.state = ltoh32(state.state);
       state.bytes = ltoh32(state.bytes);
 
       /* restart if an error is reported */
       if ((state.state == DL_BAD_HDR) || (state.state == DL_BAD_CRC)) {
           DBUSERR(("%s: Bad Hdr or Bad CRC\n", __FUNCTION__));
           err = DBUS_ERR;
           goto fail;
       }
 
   }
fail:
   if (bulkchunk)
       MFREE(osh, bulkchunk, dl_trunk_size);
 
   return err;
} /* dbus_usb_dl_writeimage */
 
/** Higher level DBUS layer (dbus.c) requests this layer to download image into dongle */
static int
dbus_usb_dlstart(void *bus, uint8 *fw, int len)
{
   usb_info_t *usbinfo = BUS_INFO(bus, usb_info_t);
   int err;
 
   DBUSTRACE(("%s\n", __FUNCTION__));
 
   if (usbinfo == NULL)
       return DBUS_ERR;
 
   if (USB_DEV_ISBAD(usbinfo))
       return DBUS_ERR;
 
   err = dbus_usb_rdl_dwnld_state(usbinfo);
 
   if (DBUS_OK == err) {
       err = dbus_usb_dl_writeimage(usbinfo, fw, len);
       if (err == DBUS_OK)
           usbinfo->pub->busstate = DBUS_STATE_DL_DONE;
       else
           usbinfo->pub->busstate = DBUS_STATE_DL_PENDING;
   } else
       usbinfo->pub->busstate = DBUS_STATE_DL_PENDING;
 
   return err;
}
 
static bool
dbus_usb_update_chipinfo(usb_info_t *usbinfo, uint32 chip)
{
   bool retval = TRUE;
   /* based on the CHIP Id, store the ram size which is needed for NVRAM download. */
   switch (chip) {
 
       case 0x4319:
           usbinfo->rdlram_size = RDL_RAM_SIZE_4319;
           usbinfo->rdlram_base_addr = RDL_RAM_BASE_4319;
           break;
 
       case 0x4329:
           usbinfo->rdlram_size = RDL_RAM_SIZE_4329;
           usbinfo->rdlram_base_addr = RDL_RAM_BASE_4329;
           break;
 
       case 43234:
       case 43235:
       case 43236:
           usbinfo->rdlram_size = RDL_RAM_SIZE_43236;
           usbinfo->rdlram_base_addr = RDL_RAM_BASE_43236;
           break;
 
       case 0x4328:
           usbinfo->rdlram_size = RDL_RAM_SIZE_4328;
           usbinfo->rdlram_base_addr = RDL_RAM_BASE_4328;
           break;
 
       case 0x4322:
           usbinfo->rdlram_size = RDL_RAM_SIZE_4322;
           usbinfo->rdlram_base_addr = RDL_RAM_BASE_4322;
           break;
 
       case 0x4360:
       case 0xAA06:
           usbinfo->rdlram_size = RDL_RAM_SIZE_4360;
           usbinfo->rdlram_base_addr = RDL_RAM_BASE_4360;
           break;
 
       case 43242:
       case 43243:
           usbinfo->rdlram_size = RDL_RAM_SIZE_43242;
           usbinfo->rdlram_base_addr = RDL_RAM_BASE_43242;
           break;
 
       case 43143:
           usbinfo->rdlram_size = RDL_RAM_SIZE_43143;
           usbinfo->rdlram_base_addr = RDL_RAM_BASE_43143;
           break;
 
       case 0x4350:
       case 43556:
       case 43558:
       case 43569:
           usbinfo->rdlram_size = RDL_RAM_SIZE_4350;
           usbinfo->rdlram_base_addr = RDL_RAM_BASE_4350;
           break;
 
       case POSTBOOT_ID:
           break;
 
       default:
           DBUSERR(("%s: Chip 0x%x Ram size is not known\n", __FUNCTION__, chip));
           retval = FALSE;
           break;
 
   }
 
   return retval;
} /* dbus_usb_update_chipinfo */
 
/** higher DBUS level (dbus.c) wants to know if firmware download is required. */
static int
dbus_usb_dlneeded(void *bus)
{
   usb_info_t *usbinfo = BUS_INFO(bus, usb_info_t);
   void *osinfo;
   bootrom_id_t id;
   int dl_needed = 1;
 
   DBUSTRACE(("%s\n", __FUNCTION__));
 
   if (usbinfo == NULL)
       return DBUS_ERR;
 
   osinfo = usbinfo->usbosl_info;
   ASSERT(osinfo);
 
   /* Check if firmware downloaded already by querying runtime ID */
   id.chip = 0xDEAD;
   dbus_usbos_dl_cmd(osinfo, DL_GETVER, &id, sizeof(bootrom_id_t));
 
   id.chip = ltoh32(id.chip);
   id.chiprev = ltoh32(id.chiprev);
 
   if (FALSE == dbus_usb_update_chipinfo(usbinfo, id.chip)) {
       dl_needed = DBUS_ERR;
       goto exit;
   }
 
   DBUSERR(("%s: chip 0x%x rev 0x%x\n", __FUNCTION__, id.chip, id.chiprev));
   if (id.chip == POSTBOOT_ID) {
       /* This code is  needed to support two enumerations on USB1.1 scenario */
       DBUSERR(("%s: Firmware already downloaded\n", __FUNCTION__));
 
       dbus_usbos_dl_cmd(osinfo, DL_RESETCFG, &id, sizeof(bootrom_id_t));
       dl_needed = DBUS_OK;
       if (usbinfo->pub->busstate == DBUS_STATE_DL_PENDING)
           usbinfo->pub->busstate = DBUS_STATE_DL_DONE;
   } else {
       usbinfo->pub->attrib.devid = id.chip;
       usbinfo->pub->attrib.chiprev = id.chiprev;
   }
 
exit:
   return dl_needed;
}
 
/** After issuing firmware download, higher DBUS level (dbus.c) wants to start the firmware. */
static int
dbus_usb_dlrun(void *bus)
{
   usb_info_t *usbinfo = BUS_INFO(bus, usb_info_t);
   void *osinfo;
   rdl_state_t state;
   int err = DBUS_OK;
 
   DBUSTRACE(("%s\n", __FUNCTION__));
 
   if (usbinfo == NULL)
       return DBUS_ERR;
 
   if (USB_DEV_ISBAD(usbinfo))
       return DBUS_ERR;
 
   osinfo = usbinfo->usbosl_info;
   ASSERT(osinfo);
 
   /* Check we are runnable */
   dbus_usbos_dl_cmd(osinfo, DL_GETSTATE, &state, sizeof(rdl_state_t));
 
   state.state = ltoh32(state.state);
   state.bytes = ltoh32(state.bytes);
 
   /* Start the image */
   if (state.state == DL_RUNNABLE) {
       DBUSTRACE(("%s: Issue DL_GO\n", __FUNCTION__));
       dbus_usbos_dl_cmd(osinfo, DL_GO, &state, sizeof(rdl_state_t));
 
       if (usbinfo->pub->attrib.devid == TEST_CHIP)
           dbus_usbos_wait(osinfo, USB_DLGO_SPINWAIT);
 
//        dbus_usb_resetcfg(usbinfo);
       /* The Donlge may go for re-enumeration. */
   } else {
       DBUSERR(("%s: Dongle not runnable\n", __FUNCTION__));
       err = DBUS_ERR;
   }
 
   return err;
}
 
/**
 * As preparation for firmware download, higher DBUS level (dbus.c) requests the firmware image
 * to be used for the type of dongle detected. Directly called by dbus.c (so not via a callback
 * construction)
 */
void
dbus_bus_fw_get(void *bus, uint8 **fw, int *fwlen, int *decomp)
{
   usb_info_t *usbinfo = BUS_INFO(bus, usb_info_t);
   unsigned int devid;
   unsigned int crev;
 
   devid = usbinfo->pub->attrib.devid;
   crev = usbinfo->pub->attrib.chiprev;
 
   *fw = NULL;
   *fwlen = 0;
 
   switch (devid) {
   case BCM43236_CHIP_ID:
   case BCM43235_CHIP_ID:
   case BCM43234_CHIP_ID:
   case BCM43238_CHIP_ID: {
       if (crev == 3 || crev == 2 || crev == 1) {
#ifdef EMBED_IMAGE_43236b
           *fw = (uint8 *)dlarray_43236b;
           *fwlen = sizeof(dlarray_43236b);
 
#endif
       }
       } break;
   case BCM4360_CHIP_ID:
   case BCM4352_CHIP_ID:
   case BCM43526_CHIP_ID:
#ifdef EMBED_IMAGE_43526a
       if (crev <= 2) {
           *fw = (uint8 *)dlarray_43526a;
           *fwlen = sizeof(dlarray_43526a);
       }
#endif
#ifdef EMBED_IMAGE_43526b
       if (crev > 2) {
           *fw = (uint8 *)dlarray_43526b;
           *fwlen = sizeof(dlarray_43526b);
       }
#endif
       break;
 
   case BCM43242_CHIP_ID:
#ifdef EMBED_IMAGE_43242a0
       *fw = (uint8 *)dlarray_43242a0;
       *fwlen = sizeof(dlarray_43242a0);
#endif
       break;
 
   case BCM43143_CHIP_ID:
#ifdef EMBED_IMAGE_43143a0
       *fw = (uint8 *)dlarray_43143a0;
       *fwlen = sizeof(dlarray_43143a0);
#endif
#ifdef EMBED_IMAGE_43143b0
       *fw = (uint8 *)dlarray_43143b0;
       *fwlen = sizeof(dlarray_43143b0);
#endif
       break;
 
   case BCM4350_CHIP_ID:
   case BCM4354_CHIP_ID:
   case BCM43556_CHIP_ID:
   case BCM43558_CHIP_ID:
   case BCM43566_CHIP_ID:
   case BCM43568_CHIP_ID:
   case BCM43570_CHIP_ID:
   case BCM4358_CHIP_ID:
#ifdef EMBED_IMAGE_4350a0
       if (crev == 0) {
           *fw = (uint8 *)dlarray_4350a0;
           *fwlen = sizeof(dlarray_4350a0);
       }
#endif
#ifdef EMBED_IMAGE_4350b0
       if (crev == 1) {
           *fw = (uint8 *)dlarray_4350b0;
           *fwlen = sizeof(dlarray_4350b0);
       }
#endif
#ifdef EMBED_IMAGE_4350b1
       if (crev == 2) {
           *fw = (uint8 *)dlarray_4350b1;
           *fwlen = sizeof(dlarray_4350b1);
       }
#endif
#ifdef EMBED_IMAGE_43556b1
       if (crev == 2) {
           *fw = (uint8 *)dlarray_43556b1;
           *fwlen = sizeof(dlarray_43556b1);
       }
#endif
#ifdef EMBED_IMAGE_4350c0
       if (crev == 3) {
           *fw = (uint8 *)dlarray_4350c0;
           *fwlen = sizeof(dlarray_4350c0);
       }
#endif /* EMBED_IMAGE_4350c0 */
#ifdef EMBED_IMAGE_4350c1
       if (crev == 4) {
           *fw = (uint8 *)dlarray_4350c1;
           *fwlen = sizeof(dlarray_4350c1);
       }
#endif /* EMBED_IMAGE_4350c1 */
       break;
   case BCM43569_CHIP_ID:
#ifdef EMBED_IMAGE_43569a0
       if (crev == 0) {
           *fw = (uint8 *)dlarray_43569a0;
           *fwlen = sizeof(dlarray_43569a0);
       }
#endif /* EMBED_IMAGE_43569a0 */
       break;
   default:
#ifdef EMBED_IMAGE_GENERIC
       *fw = (uint8 *)dlarray;
       *fwlen = sizeof(dlarray);
#endif
       break;
   }
} /* dbus_bus_fw_get */