-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdbus.c
3516 lines (2978 loc) · 87.7 KB
/
dbus.c
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
/** @file dbus.c
*
* Hides details of USB / SDIO / SPI interfaces and OS details. It is intended to shield details and
* provide the caller with one common bus interface for all dongle devices. In practice, it is only
* used for USB interfaces. DBUS is not a protocol, but an abstraction layer.
*
* Copyright (C) 2022, Broadcom.
*
* 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.
*
*
* <<Broadcom-WL-IPTag/Dual:>>
*/
#include <linux/usb.h>
#include "osl.h"
#include "dbus.h"
#include <bcmutils.h>
#include <dngl_stats.h>
#include <dhd.h>
#include <dhd_proto.h>
#ifdef PROP_TXSTATUS /* a form of flow control between host and dongle */
#include <dhd_wlfc.h>
#endif
#include <dhd_config.h>
#ifdef WL_CFG80211
#include <wl_cfg80211.h>
#include <wl_cfgp2p.h>
#endif
#include <bcmdevs_legacy.h>
#if defined(BCM_DNGL_EMBEDIMAGE)
#include <bcmsrom_fmt.h>
#include <trxhdr.h>
#include <usbrdl.h>
#include <bcmendian.h>
#include <zutil.h>
#include <sbpcmcia.h>
#include <bcmnvram.h>
#include <bcmdevs.h>
#elif defined(BCM_REQUEST_FW)
#include <bcmsrom_fmt.h>
#include <trxhdr.h>
#include <usbrdl.h>
#include <bcmendian.h>
#include <sbpcmcia.h>
#include <bcmnvram.h>
#include <bcmdevs.h>
#endif /* #if defined(BCM_DNGL_EMBEDIMAGE) */
#if defined(EHCI_FASTPATH_TX) || defined(EHCI_FASTPATH_RX)
#include <linux/usb.h>
#endif /* EHCI_FASTPATH_TX || EHCI_FASTPATH_RX */
#if defined(BCM_DNGL_EMBEDIMAGE)
/* zlib file format field ids etc from gzio.c */
#define Z_DEFLATED 8
#define ASCII_FLAG 0x01 /**< bit 0 set: file probably ascii text */
#define HEAD_CRC 0x02 /**< bit 1 set: header CRC present */
#define EXTRA_FIELD 0x04 /**< bit 2 set: extra field present */
#define ORIG_NAME 0x08 /**< bit 3 set: original file name present */
#define COMMENT 0x10 /**< bit 4 set: file comment present */
#define RESERVED 0xE0 /**< bits 5..7: reserved */
/* Rename define */
#ifdef WL_FW_DECOMP
#define UNZIP_ENAB(info) 1
#else
#define UNZIP_ENAB(info) 0
#ifdef inflateInit2
#undef inflateInit2
#define inflateInit2(a, b) Z_ERRNO
#endif
#define inflate(a, b) Z_STREAM_ERROR
#define inflateEnd(a) do {} while (0)
#define crc32(a, b, c) -1
#define free(a) do {} while (0)
#endif /* WL_FW_DECOMP */
#elif defined(BCM_REQUEST_FW)
#ifndef VARS_MAX
#define VARS_MAX 8192
#endif
#endif /* #if defined(BCM_DNGL_EMBEDIMAGE) */
#ifdef DBUS_USB_LOOPBACK
extern bool is_loopback_pkt(void *buf);
extern int matches_loopback_pkt(void *buf);
#endif
/** General info for all BUS types */
typedef struct dbus_irbq {
dbus_irb_t *head;
dbus_irb_t *tail;
int cnt;
} dbus_irbq_t;
/**
* This private structure dhd_bus_t is also declared in dbus_usb_linux.c.
* All the fields must be consistent in both declarations.
*/
typedef struct dhd_bus {
dbus_pub_t pub; /* MUST BE FIRST */
dhd_pub_t *dhd;
void *cbarg;
dbus_callbacks_t *cbs; /**< callbacks to higher level, eg dhd_linux.c */
void *bus_info;
dbus_intf_t *drvintf; /**< callbacks to lower level, eg dbus_usb.c or dbus_usb_linux.c */
uint8 *fw;
int fwlen;
uint32 errmask;
int rx_low_watermark; /**< avoid rx overflow by filling rx with free IRBs */
int tx_low_watermark;
bool txoff;
bool txoverride; /**< flow control related */
bool rxoff;
bool tx_timer_ticking;
uint ctl_completed;
dbus_irbq_t *rx_q;
dbus_irbq_t *tx_q;
#ifdef BCMDBG
int *txpend_q_hist;
int *rxpend_q_hist;
#endif /* BCMDBG */
#ifdef EHCI_FASTPATH_RX
atomic_t rx_outstanding;
#endif
uint8 *nvram;
int nvram_len;
uint8 *image; /**< buffer for combine fw and nvram */
int image_len;
uint8 *orig_fw;
int origfw_len;
int decomp_memsize;
dbus_extdl_t extdl;
int nvram_nontxt;
#if defined(BCM_REQUEST_FW)
void *firmware;
void *nvfile;
#endif
char *fw_path; /* module_param: path to firmware image */
char *nv_path; /* module_param: path to nvram vars file */
uint64 last_suspend_end_time;
} dhd_bus_t;
struct exec_parms {
union {
/* Can consolidate same params, if need be, but this shows
* group of parameters per function
*/
struct {
dbus_irbq_t *q;
dbus_irb_t *b;
} qenq;
struct {
dbus_irbq_t *q;
} qdeq;
};
};
#define EXEC_RXLOCK(info, fn, a) \
info->drvintf->exec_rxlock(dhd_bus->bus_info, ((exec_cb_t)fn), ((struct exec_parms *) a))
#define EXEC_TXLOCK(info, fn, a) \
info->drvintf->exec_txlock(dhd_bus->bus_info, ((exec_cb_t)fn), ((struct exec_parms *) a))
/*
* Callbacks common for all BUS
*/
static void dbus_if_send_irb_timeout(void *handle, dbus_irb_tx_t *txirb);
static void dbus_if_send_irb_complete(void *handle, dbus_irb_tx_t *txirb, int status);
static void dbus_if_recv_irb_complete(void *handle, dbus_irb_rx_t *rxirb, int status);
static void dbus_if_errhandler(void *handle, int err);
static void dbus_if_ctl_complete(void *handle, int type, int status);
static void dbus_if_state_change(void *handle, int state);
static void *dbus_if_pktget(void *handle, uint len, bool send);
static void dbus_if_pktfree(void *handle, void *p, bool send);
static struct dbus_irb *dbus_if_getirb(void *cbarg, bool send);
static void dbus_if_rxerr_indicate(void *handle, bool on);
static int dbus_suspend(void *context);
static int dbus_resume(void *context);
static void * dhd_dbus_probe_cb(uint16 bus_no, uint16 slot, uint32 hdrlen);
static void dhd_dbus_disconnect_cb(void *arg);
static void dbus_detach(dhd_bus_t *pub);
/** functions in this file that are called by lower DBUS levels, eg dbus_usb.c */
static dbus_intf_callbacks_t dbus_intf_cbs = {
dbus_if_send_irb_timeout,
dbus_if_send_irb_complete,
dbus_if_recv_irb_complete,
dbus_if_errhandler,
dbus_if_ctl_complete,
dbus_if_state_change,
NULL, /**< isr */
NULL, /**< dpc */
NULL, /**< watchdog */
dbus_if_pktget,
dbus_if_pktfree,
dbus_if_getirb,
dbus_if_rxerr_indicate
};
/*
* Need global for probe() and disconnect() since
* attach() is not called at probe and detach()
* can be called inside disconnect()
*/
static dbus_intf_t *g_busintf = NULL;
#if defined(BCM_REQUEST_FW)
int8 *nonfwnvram = NULL; /**< stand-alone multi-nvram given with driver load */
int nonfwnvramlen = 0;
#endif /* #if defined(BCM_REQUEST_FW) */
static void* q_enq(dbus_irbq_t *q, dbus_irb_t *b);
static void* q_enq_exec(struct exec_parms *args);
static dbus_irb_t*q_deq(dbus_irbq_t *q);
static void* q_deq_exec(struct exec_parms *args);
static int dbus_tx_timer_init(dhd_bus_t *dhd_bus);
static int dbus_tx_timer_start(dhd_bus_t *dhd_bus, uint timeout);
static int dbus_tx_timer_stop(dhd_bus_t *dhd_bus);
static int dbus_irbq_init(dhd_bus_t *dhd_bus, dbus_irbq_t *q, int nq, int size_irb);
static int dbus_irbq_deinit(dhd_bus_t *dhd_bus, dbus_irbq_t *q, int size_irb);
static int dbus_rxirbs_fill(dhd_bus_t *dhd_bus);
static int dbus_send_irb(dbus_pub_t *pub, uint8 *buf, int len, void *pkt, void *info);
#if (defined(BCM_DNGL_EMBEDIMAGE) || defined(BCM_REQUEST_FW))
#if defined(BCM_REQUEST_FW)
extern char * dngl_firmware;
extern unsigned int dngl_fwlen;
#endif /* #if defined(BCM_REQUEST_FW) */
#ifndef EXTERNAL_FW_PATH
static int dbus_get_nvram(dhd_bus_t *dhd_bus);
static int dbus_jumbo_nvram(dhd_bus_t *dhd_bus);
static int dbus_select_nvram(dhd_bus_t *dhd_bus, int8 *jumbonvram, int jumbolen,
uint16 boardtype, uint16 boardrev, int8 **nvram, int *nvram_len);
#endif /* !EXTERNAL_FW_PATH */
#ifndef BCM_REQUEST_FW
static int dbus_zlib_decomp(dhd_bus_t *dhd_bus);
extern void *dbus_zlib_calloc(int num, int size);
extern void dbus_zlib_free(void *ptr);
#endif
#endif /* defined(BCM_DNGL_EMBEDIMAGE) || defined(BCM_REQUEST_FW) */
/* function */
void
dbus_flowctrl_tx(void *dbi, bool on)
{
dhd_bus_t *dhd_bus = dbi;
if (dhd_bus == NULL)
return;
DBUSTRACE(("%s on %d\n", __FUNCTION__, on));
if (dhd_bus->txoff == on)
return;
dhd_bus->txoff = on;
if (dhd_bus->cbs && dhd_bus->cbs->txflowcontrol)
dhd_bus->cbs->txflowcontrol(dhd_bus->cbarg, on);
}
/**
* if lower level DBUS signaled a rx error, more free rx IRBs should be allocated or flow control
* should kick in to make more free rx IRBs available.
*/
static void
dbus_if_rxerr_indicate(void *handle, bool on)
{
dhd_bus_t *dhd_bus = (dhd_bus_t *) handle;
DBUSTRACE(("%s, on %d\n", __FUNCTION__, on));
if (dhd_bus == NULL)
return;
if (dhd_bus->txoverride == on)
return;
dhd_bus->txoverride = on; /* flow control */
if (!on)
dbus_rxirbs_fill(dhd_bus);
}
/** q_enq()/q_deq() are executed with protection via exec_rxlock()/exec_txlock() */
static void*
q_enq(dbus_irbq_t *q, dbus_irb_t *b)
{
ASSERT(q->tail != b);
ASSERT(b->next == NULL);
b->next = NULL;
if (q->tail) {
q->tail->next = b;
q->tail = b;
} else
q->head = q->tail = b;
q->cnt++;
return b;
}
static void*
q_enq_exec(struct exec_parms *args)
{
return q_enq(args->qenq.q, args->qenq.b);
}
static dbus_irb_t*
q_deq(dbus_irbq_t *q)
{
dbus_irb_t *b;
b = q->head;
if (b) {
q->head = q->head->next;
b->next = NULL;
if (q->head == NULL)
q->tail = q->head;
q->cnt--;
}
return b;
}
static void*
q_deq_exec(struct exec_parms *args)
{
return q_deq(args->qdeq.q);
}
/**
* called during attach phase. Status @ Dec 2012: this function does nothing since for all of the
* lower DBUS levels dhd_bus->drvintf->tx_timer_init is NULL.
*/
static int
dbus_tx_timer_init(dhd_bus_t *dhd_bus)
{
if (dhd_bus && dhd_bus->drvintf && dhd_bus->drvintf->tx_timer_init)
return dhd_bus->drvintf->tx_timer_init(dhd_bus->bus_info);
else
return DBUS_ERR;
}
static int
dbus_tx_timer_start(dhd_bus_t *dhd_bus, uint timeout)
{
if (dhd_bus == NULL)
return DBUS_ERR;
if (dhd_bus->tx_timer_ticking)
return DBUS_OK;
if (dhd_bus->drvintf && dhd_bus->drvintf->tx_timer_start) {
if (dhd_bus->drvintf->tx_timer_start(dhd_bus->bus_info, timeout) == DBUS_OK) {
dhd_bus->tx_timer_ticking = TRUE;
return DBUS_OK;
}
}
return DBUS_ERR;
}
static int
dbus_tx_timer_stop(dhd_bus_t *dhd_bus)
{
if (dhd_bus == NULL)
return DBUS_ERR;
if (!dhd_bus->tx_timer_ticking)
return DBUS_OK;
if (dhd_bus->drvintf && dhd_bus->drvintf->tx_timer_stop) {
if (dhd_bus->drvintf->tx_timer_stop(dhd_bus->bus_info) == DBUS_OK) {
dhd_bus->tx_timer_ticking = FALSE;
return DBUS_OK;
}
}
return DBUS_ERR;
}
/** called during attach phase. */
static int
dbus_irbq_init(dhd_bus_t *dhd_bus, dbus_irbq_t *q, int nq, int size_irb)
{
int i;
dbus_irb_t *irb;
ASSERT(q);
ASSERT(dhd_bus);
for (i = 0; i < nq; i++) {
/* MALLOCZ dbus_irb_tx or dbus_irb_rx, but cast to simple dbus_irb_t linkedlist */
irb = (dbus_irb_t *) MALLOCZ(dhd_bus->pub.osh, size_irb);
if (irb == NULL) {
ASSERT(irb);
return DBUS_ERR;
}
/* q_enq() does not need to go through EXEC_xxLOCK() during init() */
q_enq(q, irb);
}
return DBUS_OK;
}
/** called during detach phase or when attach failed */
static int
dbus_irbq_deinit(dhd_bus_t *dhd_bus, dbus_irbq_t *q, int size_irb)
{
dbus_irb_t *irb;
ASSERT(q);
ASSERT(dhd_bus);
/* q_deq() does not need to go through EXEC_xxLOCK()
* during deinit(); all callbacks are stopped by this time
*/
while ((irb = q_deq(q)) != NULL) {
MFREE(dhd_bus->pub.osh, irb, size_irb);
}
if (q->cnt)
DBUSERR(("deinit: q->cnt=%d > 0\n", q->cnt));
return DBUS_OK;
}
/** multiple code paths require the rx queue to be filled with more free IRBs */
static int
dbus_rxirbs_fill(dhd_bus_t *dhd_bus)
{
int err = DBUS_OK;
#ifdef EHCI_FASTPATH_RX
while (atomic_read(&dhd_bus->rx_outstanding) < 100) /* TODO: Improve constant */
{
#if defined(BCM_RPC_NOCOPY) || defined(BCM_RPC_RXNOCOPY)
/* NOCOPY force new packet allocation */
optimize_submit_rx_request(&dhd_bus->pub, 1, NULL, NULL);
#else
/* Copy mode - allocate own buffer to be reused */
void *buf = MALLOCZ(dhd_bus->pub.osh, 4000); /* usbos_info->rxbuf_len */
optimize_submit_rx_request(&dhd_bus->pub, 1, NULL, buf);
/* ME: Need to check result and set err = DBUS_ERR_RXDROP */
#endif /* BCM_RPC_NOCOPY || BCM_RPC_RXNOCOPY */
atomic_inc(&dhd_bus->rx_outstanding);
}
#else /* EHCI_FASTPATH_RX */
dbus_irb_rx_t *rxirb;
struct exec_parms args;
ASSERT(dhd_bus);
if (dhd_bus->pub.busstate != DBUS_STATE_UP) {
DBUSERR(("dbus_rxirbs_fill: DBUS not up \n"));
return DBUS_ERR;
} else if (!dhd_bus->drvintf || (dhd_bus->drvintf->recv_irb == NULL)) {
/* Lower edge bus interface does not support recv_irb().
* No need to pre-submit IRBs in this case.
*/
return DBUS_ERR;
}
/* The dongle recv callback is freerunning without lock. So multiple callbacks(and this
* refill) can run in parallel. While the rxoff condition is triggered outside,
* below while loop has to check and abort posting more to avoid RPC rxq overflow.
*/
args.qdeq.q = dhd_bus->rx_q;
while ((!dhd_bus->rxoff) &&
(rxirb = (EXEC_RXLOCK(dhd_bus, q_deq_exec, &args))) != NULL) {
err = dhd_bus->drvintf->recv_irb(dhd_bus->bus_info, rxirb);
if (err == DBUS_ERR_RXDROP || err == DBUS_ERR_RXFAIL) {
/* Add the the free rxirb back to the queue
* and wait till later
*/
bzero(rxirb, sizeof(dbus_irb_rx_t));
args.qenq.q = dhd_bus->rx_q;
args.qenq.b = (dbus_irb_t *) rxirb;
EXEC_RXLOCK(dhd_bus, q_enq_exec, &args);
break;
} else if (err != DBUS_OK) {
int i = 0;
while (i++ < 100) {
DBUSERR(("%s :: memory leak for rxirb note?\n", __FUNCTION__));
}
}
}
#endif /* EHCI_FASTPATH_RX */
return err;
} /* dbus_rxirbs_fill */
/** called when the DBUS interface state changed. */
void
dbus_flowctrl_rx(dbus_pub_t *pub, bool on)
{
dhd_bus_t *dhd_bus = (dhd_bus_t *) pub;
if (dhd_bus == NULL)
return;
DBUSTRACE(("%s\n", __FUNCTION__));
if (dhd_bus->rxoff == on)
return;
dhd_bus->rxoff = on;
if (dhd_bus->pub.busstate == DBUS_STATE_UP) {
if (!on) {
/* post more irbs, resume rx if necessary */
dbus_rxirbs_fill(dhd_bus);
if (dhd_bus && dhd_bus->drvintf->recv_resume) {
dhd_bus->drvintf->recv_resume(dhd_bus->bus_info);
}
} else {
/* ??? cancell posted irbs first */
if (dhd_bus && dhd_bus->drvintf->recv_stop) {
dhd_bus->drvintf->recv_stop(dhd_bus->bus_info);
}
}
}
}
/**
* Several code paths in this file want to send a buffer to the dongle. This function handles both
* sending of a buffer or a pkt.
*/
static int
dbus_send_irb(dbus_pub_t *pub, uint8 *buf, int len, void *pkt, void *info)
{
dhd_bus_t *dhd_bus = (dhd_bus_t *) pub;
int err = DBUS_OK;
#ifndef EHCI_FASTPATH_TX
dbus_irb_tx_t *txirb = NULL;
int txirb_pending;
struct exec_parms args;
#endif /* EHCI_FASTPATH_TX */
if (dhd_bus == NULL)
return DBUS_ERR;
DBUSTRACE(("%s\n", __FUNCTION__));
if (dhd_bus->pub.busstate == DBUS_STATE_UP ||
dhd_bus->pub.busstate == DBUS_STATE_SLEEP) {
#ifdef EHCI_FASTPATH_TX
struct ehci_qtd *qtd;
int token = EHCI_QTD_SET_CERR(3);
int len;
ASSERT(buf == NULL); /* Not handled */
ASSERT(pkt != NULL);
qtd = optimize_ehci_qtd_alloc(GFP_KERNEL);
if (qtd == NULL)
return DBUS_ERR;
len = PKTLEN(pub->osh, pkt);
len = ROUNDUP(len, sizeof(uint32));
#ifdef BCMDBG
/* The packet length is already padded to not to be multiple of 512 bytes
* in bcm_rpc_tp_buf_send_internal(), so it should not be 512*N bytes here.
*/
if (len % EHCI_BULK_PACKET_SIZE == 0) {
DBUSERR(("%s: len = %d (multiple of 512 bytes)\n", __FUNCTION__, len));
return DBUS_ERR_TXDROP;
}
#endif /* BCMDBG */
optimize_qtd_fill_with_rpc(pub, 0, qtd, pkt, token, len);
err = optimize_submit_async(qtd, 0);
if (err) {
optimize_ehci_qtd_free(qtd);
err = DBUS_ERR_TXDROP;
}
/* ME: Add timeout? */
#else
args.qdeq.q = dhd_bus->tx_q;
if (dhd_bus->drvintf)
txirb = EXEC_TXLOCK(dhd_bus, q_deq_exec, &args);
if (txirb == NULL) {
DBUSERR(("Out of tx dbus_bufs\n"));
return DBUS_ERR;
}
if (pkt != NULL) {
txirb->pkt = pkt;
txirb->buf = NULL;
txirb->len = 0;
} else if (buf != NULL) {
txirb->pkt = NULL;
txirb->buf = buf;
txirb->len = len;
} else {
ASSERT(0); /* Should not happen */
}
txirb->info = info;
txirb->arg = NULL;
txirb->retry_count = 0;
if (dhd_bus->drvintf && dhd_bus->drvintf->send_irb) {
/* call lower DBUS level send_irb function */
err = dhd_bus->drvintf->send_irb(dhd_bus->bus_info, txirb);
if (err == DBUS_ERR_TXDROP) {
/* tx fail and no completion routine to clean up, reclaim irb NOW */
DBUSERR(("%s: send_irb failed, status = %d\n", __FUNCTION__, err));
bzero(txirb, sizeof(dbus_irb_tx_t));
args.qenq.q = dhd_bus->tx_q;
args.qenq.b = (dbus_irb_t *) txirb;
EXEC_TXLOCK(dhd_bus, q_enq_exec, &args);
} else {
dbus_tx_timer_start(dhd_bus, DBUS_TX_TIMEOUT_INTERVAL);
txirb_pending = dhd_bus->pub.ntxq - dhd_bus->tx_q->cnt;
#ifdef BCMDBG
dhd_bus->txpend_q_hist[txirb_pending]++;
#endif /* BCMDBG */
if (txirb_pending > (dhd_bus->tx_low_watermark * 3)) {
dbus_flowctrl_tx(dhd_bus, TRUE);
}
}
}
#endif /* EHCI_FASTPATH_TX */
} else {
err = DBUS_ERR_TXFAIL;
DBUSTRACE(("%s: bus down, send_irb failed\n", __FUNCTION__));
}
return err;
} /* dbus_send_irb */
#if (defined(BCM_DNGL_EMBEDIMAGE) || defined(BCM_REQUEST_FW))
/**
* Before downloading a firmware image into the dongle, the validity of the image must be checked.
*/
static int
check_file(osl_t *osh, unsigned char *headers)
{
struct trx_header *trx;
int actual_len = -1;
/* Extract trx header */
trx = (struct trx_header *)headers;
if (ltoh32(trx->magic) != TRX_MAGIC) {
printf("Error: trx bad hdr %x\n", ltoh32(trx->magic));
return -1;
}
headers += SIZEOF_TRX(trx);
/* TRX V1: get firmware len */
/* TRX V2: get firmware len and DSG/CFG lengths */
if (ltoh32(trx->flag_version) & TRX_UNCOMP_IMAGE) {
actual_len = ltoh32(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]) +
SIZEOF_TRX(trx);
#ifdef BCMTRXV2
if (ISTRX_V2(trx)) {
actual_len += ltoh32(trx->offsets[TRX_OFFSETS_DSG_LEN_IDX]) +
ltoh32(trx->offsets[TRX_OFFSETS_CFG_LEN_IDX]);
}
#endif
return actual_len;
} else {
printf("compressed image\n");
}
return -1;
}
#ifdef EXTERNAL_FW_PATH
static int
dbus_get_fw_nvram(dhd_bus_t *dhd_bus)
{
int bcmerror = -1, i;
uint len, total_len;
void *nv_image = NULL, *fw_image = NULL;
char *nv_memblock = NULL, *fw_memblock = NULL;
char *bufp;
bool file_exists;
uint8 nvram_words_pad = 0;
uint memblock_size = 2048;
uint8 *memptr;
int actual_fwlen;
struct trx_header *hdr;
uint32 img_offset = 0;
int offset = 0;
char *pfw_path = dhd_bus->fw_path;
char *pnv_path = dhd_bus->nv_path;
/* For Get nvram */
file_exists = ((pnv_path != NULL) && (pnv_path[0] != '\0'));
if (file_exists) {
nv_image = dhd_os_open_image1(dhd_bus->dhd, pnv_path);
if (nv_image == NULL) {
printf("%s: Open nvram file failed %s\n", __FUNCTION__, pnv_path);
goto err;
}
}
nv_memblock = MALLOC(dhd_bus->pub.osh, MAX_NVRAMBUF_SIZE);
if (nv_memblock == NULL) {
DBUSERR(("%s: Failed to allocate memory %d bytes\n",
__FUNCTION__, MAX_NVRAMBUF_SIZE));
goto err;
}
len = dhd_os_get_image_block(nv_memblock, MAX_NVRAMBUF_SIZE, nv_image);
if (len > 0 && len < MAX_NVRAMBUF_SIZE) {
bufp = (char *)nv_memblock;
bufp[len] = 0;
dhd_bus->nvram_len = process_nvram_vars(bufp, len);
if (dhd_bus->nvram_len % 4)
nvram_words_pad = 4 - dhd_bus->nvram_len % 4;
} else {
DBUSERR(("%s: error reading nvram file: %d\n", __FUNCTION__, len));
bcmerror = DBUS_ERR_NVRAM;
goto err;
}
if (nv_image) {
dhd_os_close_image1(dhd_bus->dhd, nv_image);
nv_image = NULL;
}
/* For Get first block of fw to calculate total_len */
file_exists = ((pfw_path != NULL) && (pfw_path[0] != '\0'));
if (file_exists) {
fw_image = dhd_os_open_image1(dhd_bus->dhd, pfw_path);
if (fw_image == NULL) {
printf("%s: Open fw file failed %s\n", __FUNCTION__, pfw_path);
goto err;
}
}
memptr = fw_memblock = MALLOC(dhd_bus->pub.osh, memblock_size);
if (fw_memblock == NULL) {
DBUSERR(("%s: Failed to allocate memory %d bytes\n", __FUNCTION__,
memblock_size));
goto err;
}
len = dhd_os_get_image_block((char*)memptr, memblock_size, fw_image);
if ((actual_fwlen = check_file(dhd_bus->pub.osh, memptr)) <= 0) {
DBUSERR(("%s: bad firmware format!\n", __FUNCTION__));
goto err;
}
total_len = actual_fwlen + dhd_bus->nvram_len + nvram_words_pad;
#if defined(CONFIG_DHD_USE_STATIC_BUF)
dhd_bus->image = (uint8*)DHD_OS_PREALLOC(dhd_bus->dhd,
DHD_PREALLOC_MEMDUMP_RAM, total_len);
#else
dhd_bus->image = MALLOC(dhd_bus->pub.osh, total_len);
#endif /* CONFIG_DHD_USE_STATIC_BUF */
dhd_bus->image_len = total_len;
if (dhd_bus->image == NULL) {
DBUSERR(("%s: malloc failed! size=%d\n", __FUNCTION__, total_len));
goto err;
}
/* Step1: Copy trx header + firmwre */
memptr = fw_memblock;
do {
if (len < 0) {
DBUSERR(("%s: dhd_os_get_image_block failed (%d)\n", __FUNCTION__, len));
bcmerror = BCME_ERROR;
goto err;
}
bcopy(memptr, dhd_bus->image+offset, len);
offset += len;
} while ((len = dhd_os_get_image_block((char*)memptr, memblock_size, fw_image)));
/* Step2: Copy NVRAM + pad */
hdr = (struct trx_header *)dhd_bus->image;
img_offset = SIZEOF_TRX(hdr) + hdr->offsets[TRX_OFFSETS_DLFWLEN_IDX];
bcopy(nv_memblock, (uint8 *)(dhd_bus->image + img_offset),
dhd_bus->nvram_len);
img_offset += dhd_bus->nvram_len;
if (nvram_words_pad) {
bzero(&dhd_bus->image[img_offset], nvram_words_pad);
img_offset += nvram_words_pad;
}
#ifdef BCMTRXV2
/* Step3: Copy DSG/CFG for V2 */
if (ISTRX_V2(hdr) &&
(hdr->offsets[TRX_OFFSETS_DSG_LEN_IDX] ||
hdr->offsets[TRX_OFFSETS_CFG_LEN_IDX])) {
DBUSERR(("%s: fix me\n", __FUNCTION__));
}
#endif /* BCMTRXV2 */
/* Step4: update TRX header for nvram size */
hdr = (struct trx_header *)dhd_bus->image;
hdr->len = htol32(total_len);
/* Pass the actual fw len */
hdr->offsets[TRX_OFFSETS_NVM_LEN_IDX] =
htol32(dhd_bus->nvram_len + nvram_words_pad);
/* Calculate CRC over header */
hdr->crc32 = hndcrc32((uint8 *)&hdr->flag_version,
SIZEOF_TRX(hdr) - OFFSETOF(struct trx_header, flag_version),
CRC32_INIT_VALUE);
/* Calculate CRC over data */
for (i = SIZEOF_TRX(hdr); i < total_len; ++i)
hdr->crc32 = hndcrc32((uint8 *)&dhd_bus->image[i], 1, hdr->crc32);
hdr->crc32 = htol32(hdr->crc32);
bcmerror = DBUS_OK;
err:
if (fw_memblock)
MFREE(dhd_bus->pub.osh, fw_memblock, MAX_NVRAMBUF_SIZE);
if (fw_image)
dhd_os_close_image1(dhd_bus->dhd, fw_image);
if (nv_memblock)
MFREE(dhd_bus->pub.osh, nv_memblock, MAX_NVRAMBUF_SIZE);
if (nv_image)
dhd_os_close_image1(dhd_bus->dhd, nv_image);
return bcmerror;
}
/**
* during driver initialization ('attach') or after PnP 'resume', firmware needs to be loaded into
* the dongle
*/
static int
dbus_do_download(dhd_bus_t *dhd_bus)
{
int err = DBUS_OK;
err = dbus_get_fw_nvram(dhd_bus);
if (err) {
DBUSERR(("dbus_do_download: fail to get nvram %d\n", err));
return err;
}
if (dhd_bus->drvintf->dlstart && dhd_bus->drvintf->dlrun) {
err = dhd_bus->drvintf->dlstart(dhd_bus->bus_info,
dhd_bus->image, dhd_bus->image_len);
if (err == DBUS_OK) {
err = dhd_bus->drvintf->dlrun(dhd_bus->bus_info);
}
} else
err = DBUS_ERR;
if (dhd_bus->image) {
#if defined(CONFIG_DHD_USE_STATIC_BUF)
DHD_OS_PREFREE(dhd_bus->dhd, dhd_bus->image, dhd_bus->image_len);
#else
MFREE(dhd_bus->pub.osh, dhd_bus->image, dhd_bus->image_len);
#endif /* CONFIG_DHD_USE_STATIC_BUF */
dhd_bus->image = NULL;
dhd_bus->image_len = 0;
}
return err;
} /* dbus_do_download */
#else
/**
* It is easy for the user to pass one jumbo nvram file to the driver than a set of smaller files.
* The 'jumbo nvram' file format is essentially a set of nvram files. Before commencing firmware
* download, the dongle needs to be probed so that the correct nvram contents within the jumbo nvram
* file is selected.
*/
static int
dbus_jumbo_nvram(dhd_bus_t *dhd_bus)
{
int8 *nvram = NULL;
int nvram_len = 0;
int ret = DBUS_OK;
uint16 boardrev = 0xFFFF;
uint16 boardtype = 0xFFFF;
ret = dbus_select_nvram(dhd_bus, dhd_bus->extdl.vars, dhd_bus->extdl.varslen,
boardtype, boardrev, &nvram, &nvram_len);
if (ret == DBUS_JUMBO_BAD_FORMAT)
return DBUS_ERR_NVRAM;
else if (ret == DBUS_JUMBO_NOMATCH &&
(boardtype != 0xFFFF || boardrev != 0xFFFF)) {
DBUSERR(("No matching NVRAM for boardtype 0x%02x boardrev 0x%02x\n",
boardtype, boardrev));
return DBUS_ERR_NVRAM;
}
dhd_bus->nvram = nvram;
dhd_bus->nvram_len = nvram_len;
return DBUS_OK;
}
/** before commencing fw download, the correct NVRAM image to download has to be picked */
static int
dbus_get_nvram(dhd_bus_t *dhd_bus)
{
int len, i;
struct trx_header *hdr;
int actual_fwlen;
uint32 img_offset = 0;
dhd_bus->nvram_len = 0;
if (dhd_bus->extdl.varslen) {
if (DBUS_OK != dbus_jumbo_nvram(dhd_bus))
return DBUS_ERR_NVRAM;
DBUSERR(("NVRAM %d bytes downloaded\n", dhd_bus->nvram_len));
}
#if defined(BCM_REQUEST_FW)
else if (nonfwnvram) {
dhd_bus->nvram = nonfwnvram;
dhd_bus->nvram_len = nonfwnvramlen;
DBUSERR(("NVRAM %d bytes downloaded\n", dhd_bus->nvram_len));
}
#endif
if (dhd_bus->nvram) {
uint8 nvram_words_pad = 0;
/* Validate the format/length etc of the file */
if ((actual_fwlen = check_file(dhd_bus->pub.osh, dhd_bus->fw)) <= 0) {
DBUSERR(("%s: bad firmware format!\n", __FUNCTION__));
return DBUS_ERR_NVRAM;
}
if (!dhd_bus->nvram_nontxt) {
/* host supplied nvram could be in .txt format
* with all the comments etc...
*/
dhd_bus->nvram_len = process_nvram_vars(dhd_bus->nvram,
dhd_bus->nvram_len);
}
if (dhd_bus->nvram_len % 4)
nvram_words_pad = 4 - dhd_bus->nvram_len % 4;
len = actual_fwlen + dhd_bus->nvram_len + nvram_words_pad;
#ifdef USBAP
/* Allocate virtual memory otherwise it might fail on embedded systems */
dhd_bus->image = VMALLOC(dhd_bus->pub.osh, len);
#else
#if defined(CONFIG_DHD_USE_STATIC_BUF)
dhd_bus->image = (uint8*)DHD_OS_PREALLOC(dhd_bus->dhd,
DHD_PREALLOC_MEMDUMP_RAM, len);
#else
dhd_bus->image = MALLOCZ(dhd_bus->pub.osh, len);
#endif /* CONFIG_DHD_USE_STATIC_BUF */
#endif /* USBAP */
dhd_bus->image_len = len;
if (dhd_bus->image == NULL) {
DBUSERR(("%s: malloc failed!\n", __FUNCTION__));
return DBUS_ERR_NVRAM;
}
hdr = (struct trx_header *)dhd_bus->fw;
/* Step1: Copy trx header + firmwre */
img_offset = SIZEOF_TRX(hdr) + hdr->offsets[TRX_OFFSETS_DLFWLEN_IDX];
bcopy(dhd_bus->fw, dhd_bus->image, img_offset);
/* Step2: Copy NVRAM + pad */
bcopy(dhd_bus->nvram, (uint8 *)(dhd_bus->image + img_offset),
dhd_bus->nvram_len);
img_offset += dhd_bus->nvram_len;
if (nvram_words_pad) {
bzero(&dhd_bus->image[img_offset],
nvram_words_pad);
img_offset += nvram_words_pad;
}
#ifdef BCMTRXV2
/* Step3: Copy DSG/CFG for V2 */
if (ISTRX_V2(hdr) &&
(hdr->offsets[TRX_OFFSETS_DSG_LEN_IDX] ||
hdr->offsets[TRX_OFFSETS_CFG_LEN_IDX])) {
bcopy(dhd_bus->fw + SIZEOF_TRX(hdr) +
hdr->offsets[TRX_OFFSETS_DLFWLEN_IDX] +
hdr->offsets[TRX_OFFSETS_NVM_LEN_IDX],
dhd_bus->image + img_offset,
hdr->offsets[TRX_OFFSETS_DSG_LEN_IDX] +
hdr->offsets[TRX_OFFSETS_CFG_LEN_IDX]);
/* Not needed */
img_offset += hdr->offsets[TRX_OFFSETS_DSG_LEN_IDX] +