2 * Dongle BUS interface Abstraction layer
3 * target serial buses like USB, SDIO, SPI, etc.
5 * Copyright (C) 2020, Broadcom.
7 * Unless you and Broadcom execute a separate written software license
8 * agreement governing use of this software, this software is licensed to you
9 * under the terms of the GNU General Public License version 2 (the "GPL"),
10 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
11 * following added to such license:
13 * As a special exception, the copyright holders of this software give you
14 * permission to link this software with independent modules, and to copy and
15 * distribute the resulting executable under terms of your choice, provided that
16 * you also meet, for each linked independent module, the terms and conditions of
17 * the license of that module. An independent module is a module which is not
18 * derived from this software. The special exception does not apply to any
19 * modifications of the software.
22 * <<Broadcom-WL-IPTag/Open:>>
29 #include <dhd_linux.h>
31 extern uint dbus_msglevel
;
32 #define DBUS_ERROR_VAL 0x0001
33 #define DBUS_TRACE_VAL 0x0002
34 #define DBUS_INFO_VAL 0x0004
36 #if defined(DHD_DEBUG)
37 #define DBUSERR(args) do {if (dbus_msglevel & DBUS_ERROR_VAL) printf args;} while (0)
38 #define DBUSTRACE(args) do {if (dbus_msglevel & DBUS_TRACE_VAL) printf args;} while (0)
39 #define DBUSINFO(args) do {if (dbus_msglevel & DBUS_INFO_VAL) printf args;} while (0)
40 #else /* defined(DHD_DEBUG) */
42 #define DBUSTRACE(args)
43 #define DBUSINFO(args)
63 DBUS_STATUS_CANCELLED
,
66 DBUS_JUMBO_BAD_FORMAT
,
71 #define ERR_CBMASK_TXFAIL 0x00000001
72 #define ERR_CBMASK_RXFAIL 0x00000002
73 #define ERR_CBMASK_ALL 0xFFFFFFFF
75 #define DBUS_CBCTL_WRITE 0
76 #define DBUS_CBCTL_READ 1
77 #if defined(INTR_EP_ENABLE)
78 #define DBUS_CBINTR_POLL 2
79 #endif /* defined(INTR_EP_ENABLE) */
81 #define DBUS_TX_RETRY_LIMIT 3 /* retries for failed txirb */
82 #define DBUS_TX_TIMEOUT_INTERVAL 250 /* timeout for txirb complete, in ms */
85 * The max TCB/RCB data buffer size
86 * With USB RPC aggregation on,
87 * rx buffer has to be a single big chunk memory due to dongle->host aggregation
88 * Upper layer has to do byte copy to deaggregate the buffer to satisfy WL driver
89 * one buffer per pkt requirement
90 * Windows Vista may be able to use MDL to workaround this requirement
91 * tx buffer has to copy over RPC buffer since they are managed in different domain
92 * Without copy, DBUS and RPC has to break the encapsulation, which is not implemented
93 * RPC aggregated buffer arrives as a chained buffers. bypte copy needs to traverse the chain
94 * to form one continuous USB irb.
95 * These buffer size must accomodate the MAX rpc agg size in both direction
96 * #define BCM_RPC_TP_DNGL_AGG_MAX_BYTE
97 * #define BCM_RPC_TP_HOST_AGG_MAX_BYTE
98 * Without USB RPC aggregation, these buffer size can be smaller like normal 2K
99 * to fit max tcp pkt(ETH_MAX_DATA_SIZE) + d11/phy/rpc/overhead
101 * The number of buffer needed is upper layer dependent. e.g. rpc defines BCM_RPC_TP_DBUS_NTXQ
103 #define DBUS_BUFFER_SIZE_TX 32000
104 #define DBUS_BUFFER_SIZE_RX 24000
106 #define DBUS_BUFFER_SIZE_TX_NOAGG 2048
107 #define DBUS_BUFFER_SIZE_RX_NOAGG 2048
118 DBUS_STATE_DL_PENDING
,
123 DBUS_STATE_DISCONNECT
,
128 enum dbus_pnp_state
{
139 typedef enum _DEVICE_SPEED
{
141 LOW_SPEED
= 1, /**< USB 1.1: 1.5 Mbps */
142 FULL_SPEED
, /**< USB 1.1: 12 Mbps */
143 HIGH_SPEED
, /**< USB 2.0: 480 Mbps */
144 SUPER_SPEED
, /**< USB 3.0: 4.8 Gbps */
152 int chiprev
; /**< chip revsion number */
154 int nchan
; /**< Data Channels */
155 int has_2nd_bulk_in_ep
;
158 /* FIX: Account for errors related to DBUS;
159 * Let upper layer account for packets/bytes
169 * Configurable BUS parameters
172 DBUS_CONFIG_ID_RXCTL_DEFERRES
= 1,
173 DBUS_CONFIG_ID_AGGR_LIMIT
,
174 DBUS_CONFIG_ID_KEEPIF_ON_DEVRESET
180 uint32 general_param
;
181 bool rxctl_deferrespok
;
192 * External Download Info
194 typedef struct dbus_extdl
{
201 struct dbus_callbacks
;
204 typedef void *(*probe_cb_t
)(void *arg
, const char *desc
, uint32 bustype
,
205 uint16 bus_no
, uint16 slot
, uint32 hdrlen
);
206 typedef void (*disconnect_cb_t
)(void *arg
);
207 typedef void *(*exec_cb_t
)(struct exec_parms
*args
);
209 /** Client callbacks registered during dbus_attach() */
210 typedef struct dbus_callbacks
{
211 void (*send_complete
)(void *cbarg
, void *info
, int status
);
212 void (*recv_buf
)(void *cbarg
, uint8
*buf
, int len
);
213 void (*recv_pkt
)(void *cbarg
, void *pkt
);
214 void (*txflowcontrol
)(void *cbarg
, bool onoff
);
215 void (*errhandler
)(void *cbarg
, int err
);
216 void (*ctl_complete
)(void *cbarg
, int type
, int status
);
217 void (*state_change
)(void *cbarg
, int state
);
218 void *(*pktget
)(void *cbarg
, uint len
, bool send
);
219 void (*pktfree
)(void *cbarg
, void *p
, bool send
);
227 struct dbus_intf_callbacks
;
230 void* (*attach
)(struct dbus_pub
*pub
, void *cbarg
, struct dbus_intf_callbacks
*cbs
);
231 void (*detach
)(struct dbus_pub
*pub
, void *bus
);
233 int (*up
)(void *bus
);
234 int (*down
)(void *bus
);
235 int (*send_irb
)(void *bus
, struct dbus_irb_tx
*txirb
);
236 int (*recv_irb
)(void *bus
, struct dbus_irb_rx
*rxirb
);
237 int (*cancel_irb
)(void *bus
, struct dbus_irb_tx
*txirb
);
238 int (*send_ctl
)(void *bus
, uint8
*buf
, int len
);
239 int (*recv_ctl
)(void *bus
, uint8
*buf
, int len
);
240 int (*get_stats
)(void *bus
, dbus_stats_t
*stats
);
241 int (*get_attrib
)(void *bus
, dbus_attrib_t
*attrib
);
243 int (*pnp
)(void *bus
, int evnt
);
244 int (*remove
)(void *bus
);
245 int (*resume
)(void *bus
);
246 int (*suspend
)(void *bus
);
247 int (*stop
)(void *bus
);
248 int (*reset
)(void *bus
);
250 /* Access to bus buffers directly */
251 void *(*pktget
)(void *bus
, int len
);
252 void (*pktfree
)(void *bus
, void *pkt
);
254 int (*iovar_op
)(void *bus
, const char *name
, void *params
, int plen
, void *arg
, int len
,
256 void (*dump
)(void *bus
, struct bcmstrbuf
*strbuf
);
257 int (*set_config
)(void *bus
, dbus_config_t
*config
);
258 int (*get_config
)(void *bus
, dbus_config_t
*config
);
260 bool (*device_exists
)(void *bus
);
261 int (*dlneeded
)(void *bus
);
262 int (*dlstart
)(void *bus
, uint8
*fw
, int len
);
263 int (*dlrun
)(void *bus
);
264 bool (*recv_needed
)(void *bus
);
266 void *(*exec_rxlock
)(void *bus
, exec_cb_t func
, struct exec_parms
*args
);
267 void *(*exec_txlock
)(void *bus
, exec_cb_t func
, struct exec_parms
*args
);
269 int (*tx_timer_init
)(void *bus
);
270 int (*tx_timer_start
)(void *bus
, uint timeout
);
271 int (*tx_timer_stop
)(void *bus
);
273 int (*sched_dpc
)(void *bus
);
274 int (*lock
)(void *bus
);
275 int (*unlock
)(void *bus
);
276 int (*sched_probe_cb
)(void *bus
);
278 int (*shutdown
)(void *bus
);
280 int (*recv_stop
)(void *bus
);
281 int (*recv_resume
)(void *bus
);
283 int (*recv_irb_from_ep
)(void *bus
, struct dbus_irb_rx
*rxirb
, uint ep_idx
);
285 int (*readreg
)(void *bus
, uint32 regaddr
, int datalen
, uint32
*value
);
287 /* Add from the bottom */
290 typedef struct dbus_pub
{
291 struct osl_info
*osh
;
293 dbus_attrib_t attrib
;
294 enum dbus_state busstate
;
295 DEVICE_SPEED device_speed
;
296 int ntxq
, nrxq
, rxsize
;
298 struct shared_info
*sh
;
302 #define BUS_INFO(bus, type) (((type *) bus)->pub->bus)
304 #define ALIGNED_LOCAL_VARIABLE(var, align) \
305 uint8 buffer[SDALIGN+64]; \
306 uint8 *var = (uint8 *)(((uintptr)&buffer[0]) & ~(align-1)) + align;
309 * Public Bus Function Interface
313 * FIX: Is there better way to pass OS/Host handles to DBUS but still
314 * maintain common interface for all OS??
315 * Under NDIS, param1 needs to be MiniportHandle
316 * For NDIS60, param2 is WdfDevice
317 * Under Linux, param1 and param2 are NULL;
319 extern int dbus_register(int vid
, int pid
, probe_cb_t prcb
, disconnect_cb_t discb
, void *prarg
,
320 void *param1
, void *param2
);
321 extern int dbus_deregister(void);
323 //extern int dbus_download_firmware(dbus_pub_t *pub);
324 //extern int dbus_up(struct dhd_bus *pub);
325 extern int dbus_down(dbus_pub_t
*pub
);
326 //extern int dbus_stop(struct dhd_bus *pub);
327 extern int dbus_shutdown(dbus_pub_t
*pub
);
328 extern void dbus_flowctrl_rx(dbus_pub_t
*pub
, bool on
);
330 extern int dbus_send_txdata(dbus_pub_t
*dbus
, void *pktbuf
);
331 extern int dbus_send_buf(dbus_pub_t
*pub
, uint8
*buf
, int len
, void *info
);
332 extern int dbus_send_pkt(dbus_pub_t
*pub
, void *pkt
, void *info
);
333 //extern int dbus_send_ctl(struct dhd_bus *pub, uint8 *buf, int len);
334 //extern int dbus_recv_ctl(struct dhd_bus *pub, uint8 *buf, int len);
335 extern int dbus_recv_bulk(dbus_pub_t
*pub
, uint32 ep_idx
);
336 extern int dbus_poll_intr(dbus_pub_t
*pub
);
337 extern int dbus_get_stats(dbus_pub_t
*pub
, dbus_stats_t
*stats
);
338 extern int dbus_get_device_speed(dbus_pub_t
*pub
);
339 extern int dbus_set_config(dbus_pub_t
*pub
, dbus_config_t
*config
);
340 extern int dbus_get_config(dbus_pub_t
*pub
, dbus_config_t
*config
);
341 extern void * dbus_get_devinfo(dbus_pub_t
*pub
);
343 extern void *dbus_pktget(dbus_pub_t
*pub
, int len
);
344 extern void dbus_pktfree(dbus_pub_t
*pub
, void* pkt
);
346 extern int dbus_set_errmask(dbus_pub_t
*pub
, uint32 mask
);
347 extern int dbus_pnp_sleep(dbus_pub_t
*pub
);
348 extern int dbus_pnp_resume(dbus_pub_t
*pub
, int *fw_reload
);
349 extern int dbus_pnp_disconnect(dbus_pub_t
*pub
);
351 //extern int dbus_iovar_op(dbus_pub_t *pub, const char *name,
352 // void *params, int plen, void *arg, int len, bool set);
354 extern void dbus_hist_dump(dbus_pub_t
*pub
, struct bcmstrbuf
*b
);
357 extern void *dhd_dbus_txq(const dbus_pub_t
*pub
);
358 extern uint
dhd_dbus_hdrlen(const dbus_pub_t
*pub
);
361 * Private Common Bus Interface
364 /** IO Request Block (IRB) */
365 typedef struct dbus_irb
{
366 struct dbus_irb
*next
; /**< it's casted from dbus_irb_tx or dbus_irb_rx struct */
369 typedef struct dbus_irb_rx
{
370 struct dbus_irb irb
; /* Must be first */
379 typedef struct dbus_irb_tx
{
380 struct dbus_irb irb
; /** Must be first */
381 uint8
*buf
; /** mutually exclusive with struct member 'pkt' */
382 int len
; /** length of field 'buf' */
383 void *pkt
; /** mutually exclusive with struct member 'buf' */
387 void *send_buf
; /**< linear bufffer for LINUX when aggreagtion is enabled */
391 * DBUS interface callbacks are different from user callbacks
392 * so, internally, different info can be passed to upper layer
394 typedef struct dbus_intf_callbacks
{
395 void (*send_irb_timeout
)(void *cbarg
, dbus_irb_tx_t
*txirb
);
396 void (*send_irb_complete
)(void *cbarg
, dbus_irb_tx_t
*txirb
, int status
);
397 void (*recv_irb_complete
)(void *cbarg
, dbus_irb_rx_t
*rxirb
, int status
);
398 void (*errhandler
)(void *cbarg
, int err
);
399 void (*ctl_complete
)(void *cbarg
, int type
, int status
);
400 void (*state_change
)(void *cbarg
, int state
);
401 bool (*isr
)(void *cbarg
, bool *wantdpc
);
402 bool (*dpc
)(void *cbarg
, bool bounded
);
403 void (*watchdog
)(void *cbarg
);
404 void *(*pktget
)(void *cbarg
, uint len
, bool send
);
405 void (*pktfree
)(void *cbarg
, void *p
, bool send
);
406 struct dbus_irb
* (*getirb
)(void *cbarg
, bool send
);
407 void (*rxerr_indicate
)(void *cbarg
, bool on
);
408 } dbus_intf_callbacks_t
;
411 * Porting: To support new bus, port these functions below
415 * Bus specific Interface
416 * Implemented by dbus_usb.c/dbus_sdio.c
418 extern int dbus_bus_register(int vid
, int pid
, probe_cb_t prcb
, disconnect_cb_t discb
, void *prarg
,
419 dbus_intf_t
**intf
, void *param1
, void *param2
);
420 extern int dbus_bus_deregister(void);
421 extern void dbus_bus_fw_get(void *bus
, uint8
**fw
, int *fwlen
, int *decomp
);
424 * Bus-specific and OS-specific Interface
425 * Implemented by dbus_usb_[linux/ndis].c/dbus_sdio_[linux/ndis].c
427 extern int dbus_bus_osl_register(int vid
, int pid
, probe_cb_t prcb
, disconnect_cb_t discb
,
428 void *prarg
, dbus_intf_t
**intf
, void *param1
, void *param2
);
429 extern int dbus_bus_osl_deregister(void);
432 * Bus-specific, OS-specific, HW-specific Interface
433 * Mainly for SDIO Host HW controller
435 extern int dbus_bus_osl_hw_register(int vid
, int pid
, probe_cb_t prcb
, disconnect_cb_t discb
,
436 void *prarg
, dbus_intf_t
**intf
);
437 extern int dbus_bus_osl_hw_deregister(void);
439 extern uint
usbdev_bulkin_eps(void);
440 #if defined(BCM_REQUEST_FW)
441 extern void *dbus_get_fw_nvfile(int devid
, int chiprev
, uint8
**fw
, int *fwlen
, int type
,
442 uint16 boardtype
, uint16 boardrev
);
443 extern void dbus_release_fw_nvfile(void *firmware
);
444 #endif /* #if defined(BCM_REQUEST_FW) */
446 #if defined(EHCI_FASTPATH_TX) || defined(EHCI_FASTPATH_RX)
448 * Include file for the ECHI fastpath optimized USB
449 * Practically all the lines below have equivalent in some structures in other include (or even
450 * source) files This violates all kind of structure and layering, but cutting through layers is
451 * what the optimization is about. The definitions are NOT literally borrowed from any GPLd code;
452 * the file is intended to be GPL-clean
454 * Note that while some resemblance between this code and GPLd code in Linux might exist, it is
455 * due to the common sibling. See FreeBSD: head/sys/dev/usb/controller/ehci.h for the source of
458 * The code assumes little endian throughout
461 #if !defined(__linux__)
462 #error "EHCI fastpath is for Linux only."
465 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
466 /* Backward compatibility */
467 typedef unsigned int gfp_t
;
469 #define dma_pool pci_pool
470 #define dma_pool_create(name, dev, size, align, alloc) \
471 pci_pool_create(name, dev, size, align, alloc, GFP_DMA | GFP_ATOMIC)
472 #define dma_pool_destroy(pool) pci_pool_destroy(pool)
473 #define dma_pool_alloc(pool, flags, handle) pci_pool_alloc(pool, flags, handle)
474 #define dma_pool_free(pool, vaddr, addr) pci_pool_free(pool, vaddr, addr)
476 #define dma_map_single(dev, addr, size, dir) pci_map_single(dev, addr, size, dir)
477 #define dma_unmap_single(dev, hnd, size, dir) pci_unmap_single(dev, hnd, size, dir)
478 #define DMA_FROM_DEVICE PCI_DMA_FROMDEVICE
479 #define DMA_TO_DEVICE PCI_DMA_TODEVICE
480 #endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)) */
482 /* Availability of these functions varies (when present, they have two arguments) */
484 #define hc32_to_cpu(x) le32_to_cpu(x)
485 #define cpu_to_hc32(x) cpu_to_le32(x)
486 typedef unsigned int __hc32
;
488 #error Two-argument functions needed
491 /* Private USB opcode base */
492 #define EHCI_FASTPATH 0x31
493 #define EHCI_SET_EP_BYPASS EHCI_FASTPATH
494 #define EHCI_SET_BYPASS_CB (EHCI_FASTPATH + 1)
495 #define EHCI_SET_BYPASS_DEV (EHCI_FASTPATH + 2)
496 #define EHCI_DUMP_STATE (EHCI_FASTPATH + 3)
497 #define EHCI_SET_BYPASS_POOL (EHCI_FASTPATH + 4)
498 #define EHCI_CLR_EP_BYPASS (EHCI_FASTPATH + 5)
501 * EHCI QTD structure (hardware and extension)
502 * NOTE that is does not need to (and does not) match its kernel counterpart
504 #define EHCI_QTD_NBUFFERS 5
505 #define EHCI_QTD_ALIGN 32
506 #define EHCI_BULK_PACKET_SIZE 512
507 #define EHCI_QTD_XACTERR_MAX 32
511 volatile uint32_t qtd_next
;
512 volatile uint32_t qtd_altnext
;
513 volatile uint32_t qtd_status
;
514 #define EHCI_QTD_GET_BYTES(x) (((x)>>16) & 0x7fff)
515 #define EHCI_QTD_IOC 0x00008000
516 #define EHCI_QTD_GET_CERR(x) (((x)>>10) & 0x3)
517 #define EHCI_QTD_SET_CERR(x) ((x) << 10)
518 #define EHCI_QTD_GET_PID(x) (((x)>>8) & 0x3)
519 #define EHCI_QTD_SET_PID(x) ((x) << 8)
520 #define EHCI_QTD_ACTIVE 0x80
521 #define EHCI_QTD_HALTED 0x40
522 #define EHCI_QTD_BUFERR 0x20
523 #define EHCI_QTD_BABBLE 0x10
524 #define EHCI_QTD_XACTERR 0x08
525 #define EHCI_QTD_MISSEDMICRO 0x04
526 volatile uint32_t qtd_buffer
[EHCI_QTD_NBUFFERS
];
527 volatile uint32_t qtd_buffer_hi
[EHCI_QTD_NBUFFERS
];
529 /* Implementation extension */
530 dma_addr_t qtd_self
; /**< own hardware address */
531 struct ehci_qtd
*obj_next
; /**< software link to the next QTD */
532 void *rpc
; /**< pointer to the rpc buffer */
533 size_t length
; /**< length of the data in the buffer */
534 void *buff
; /**< pointer to the reassembly buffer */
535 int xacterrs
; /**< retry counter for qtd xact error */
536 } __attribute__ ((aligned(EHCI_QTD_ALIGN
)));
538 #define EHCI_NULL __constant_cpu_to_le32(1) /* HW null pointer shall be odd */
540 #define SHORT_READ_Q(token) (EHCI_QTD_GET_BYTES(token) != 0 && EHCI_QTD_GET_PID(token) == 1)
544 * NOTE This structure is slightly different from the one in the kernel; but needs to stay
549 volatile uint32_t qh_link
;
550 volatile uint32_t qh_endp
;
551 volatile uint32_t qh_endphub
;
552 volatile uint32_t qh_curqtd
;
555 volatile uint32_t ow_next
;
556 volatile uint32_t ow_altnext
;
557 volatile uint32_t ow_status
;
558 volatile uint32_t ow_buffer
[EHCI_QTD_NBUFFERS
];
559 volatile uint32_t ow_buffer_hi
[EHCI_QTD_NBUFFERS
];
561 /* Extension (should match the kernel layout) */
564 struct list_head unused2
;
565 struct ehci_qtd
*dummy
;
566 struct ehci_qh
*unused3
;
568 struct ehci_hcd
*unused4
;
569 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
575 /* periodic schedule info */
582 struct usb_device
*unused14
;
588 /* periodic schedule info */
592 unsigned short unused10
;
593 unsigned short unused11
;
594 #define NO_FRAME ((unsigned short)~0)
595 #ifdef EHCI_QUIRK_FIX
596 struct usb_device
*unused12
;
597 #endif /* EHCI_QUIRK_FIX */
598 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)) */
599 struct ehci_qtd
*first_qtd
;
600 /* Link to the first QTD; this is an optimized equivalent of the qtd_list field */
601 /* NOTE that ehci_qh in ehci.h shall reserve this word */
602 } __attribute__ ((aligned(EHCI_QTD_ALIGN
)));
604 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
605 /** The corresponding structure in the kernel is used to get the QH */
606 struct hcd_dev
{ /* usb_device.hcpriv points to this */
607 struct list_head unused0
;
608 struct list_head unused1
;
610 /* array of QH pointers */
613 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)) */
615 int optimize_qtd_fill_with_rpc(const dbus_pub_t
*pub
, int epn
, struct ehci_qtd
*qtd
, void *rpc
,
617 int optimize_qtd_fill_with_data(const dbus_pub_t
*pub
, int epn
, struct ehci_qtd
*qtd
, void *data
,
619 int optimize_submit_async(struct ehci_qtd
*qtd
, int epn
);
620 void inline optimize_ehci_qtd_init(struct ehci_qtd
*qtd
, dma_addr_t dma
);
621 struct ehci_qtd
*optimize_ehci_qtd_alloc(gfp_t flags
);
622 void optimize_ehci_qtd_free(struct ehci_qtd
*qtd
);
623 void optimize_submit_rx_request(const dbus_pub_t
*pub
, int epn
, struct ehci_qtd
*qtd_in
, void *buf
);
624 #endif /* EHCI_FASTPATH_TX || EHCI_FASTPATH_RX */
626 void dbus_flowctrl_tx(void *dbi
, bool on
);
627 #endif /* __DBUS_H__ */