Lu Baolu | 4786d2e | 2018-05-21 16:39:49 +0300 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 2 | /** |
| 3 | * xhci-dbgcap.h - xHCI debug capability support |
| 4 | * |
| 5 | * Copyright (C) 2017 Intel Corporation |
| 6 | * |
| 7 | * Author: Lu Baolu <baolu.lu@linux.intel.com> |
| 8 | */ |
| 9 | #ifndef __LINUX_XHCI_DBGCAP_H |
| 10 | #define __LINUX_XHCI_DBGCAP_H |
| 11 | |
| 12 | #include <linux/tty.h> |
| 13 | #include <linux/kfifo.h> |
| 14 | |
| 15 | struct dbc_regs { |
| 16 | __le32 capability; |
| 17 | __le32 doorbell; |
| 18 | __le32 ersts; /* Event Ring Segment Table Size*/ |
| 19 | __le32 __reserved_0; /* 0c~0f reserved bits */ |
| 20 | __le64 erstba; /* Event Ring Segment Table Base Address */ |
| 21 | __le64 erdp; /* Event Ring Dequeue Pointer */ |
| 22 | __le32 control; |
| 23 | __le32 status; |
| 24 | __le32 portsc; /* Port status and control */ |
| 25 | __le32 __reserved_1; /* 2b~28 reserved bits */ |
| 26 | __le64 dccp; /* Debug Capability Context Pointer */ |
| 27 | __le32 devinfo1; /* Device Descriptor Info Register 1 */ |
| 28 | __le32 devinfo2; /* Device Descriptor Info Register 2 */ |
| 29 | }; |
| 30 | |
| 31 | struct dbc_info_context { |
| 32 | __le64 string0; |
| 33 | __le64 manufacturer; |
| 34 | __le64 product; |
| 35 | __le64 serial; |
| 36 | __le32 length; |
| 37 | __le32 __reserved_0[7]; |
| 38 | }; |
| 39 | |
| 40 | #define DBC_CTRL_DBC_RUN BIT(0) |
| 41 | #define DBC_CTRL_PORT_ENABLE BIT(1) |
| 42 | #define DBC_CTRL_HALT_OUT_TR BIT(2) |
| 43 | #define DBC_CTRL_HALT_IN_TR BIT(3) |
| 44 | #define DBC_CTRL_DBC_RUN_CHANGE BIT(4) |
| 45 | #define DBC_CTRL_DBC_ENABLE BIT(31) |
| 46 | #define DBC_CTRL_MAXBURST(p) (((p) >> 16) & 0xff) |
| 47 | #define DBC_DOOR_BELL_TARGET(p) (((p) & 0xff) << 8) |
| 48 | |
| 49 | #define DBC_MAX_PACKET 1024 |
| 50 | #define DBC_MAX_STRING_LENGTH 64 |
| 51 | #define DBC_STRING_MANUFACTURER "Linux Foundation" |
| 52 | #define DBC_STRING_PRODUCT "Linux USB Debug Target" |
| 53 | #define DBC_STRING_SERIAL "0001" |
| 54 | #define DBC_CONTEXT_SIZE 64 |
| 55 | |
| 56 | /* |
| 57 | * Port status: |
| 58 | */ |
| 59 | #define DBC_PORTSC_CONN_STATUS BIT(0) |
| 60 | #define DBC_PORTSC_PORT_ENABLED BIT(1) |
| 61 | #define DBC_PORTSC_CONN_CHANGE BIT(17) |
| 62 | #define DBC_PORTSC_RESET_CHANGE BIT(21) |
| 63 | #define DBC_PORTSC_LINK_CHANGE BIT(22) |
| 64 | #define DBC_PORTSC_CONFIG_CHANGE BIT(23) |
| 65 | |
| 66 | struct dbc_str_descs { |
| 67 | char string0[DBC_MAX_STRING_LENGTH]; |
| 68 | char manufacturer[DBC_MAX_STRING_LENGTH]; |
| 69 | char product[DBC_MAX_STRING_LENGTH]; |
| 70 | char serial[DBC_MAX_STRING_LENGTH]; |
| 71 | }; |
| 72 | |
| 73 | #define DBC_PROTOCOL 1 /* GNU Remote Debug Command */ |
| 74 | #define DBC_VENDOR_ID 0x1d6b /* Linux Foundation 0x1d6b */ |
| 75 | #define DBC_PRODUCT_ID 0x0010 /* device 0010 */ |
| 76 | #define DBC_DEVICE_REV 0x0010 /* 0.10 */ |
| 77 | |
| 78 | enum dbc_state { |
| 79 | DS_DISABLED = 0, |
| 80 | DS_INITIALIZED, |
| 81 | DS_ENABLED, |
| 82 | DS_CONNECTED, |
| 83 | DS_CONFIGURED, |
Andy Shevchenko | a230f1a | 2023-12-01 17:06:31 +0200 | [diff] [blame] | 84 | DS_MAX |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 87 | struct dbc_ep { |
| 88 | struct xhci_dbc *dbc; |
| 89 | struct list_head list_pending; |
| 90 | struct xhci_ring *ring; |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 91 | unsigned int direction:1; |
Mathias Nyman | 9044ad5 | 2024-09-05 17:32:49 +0300 | [diff] [blame] | 92 | unsigned int halted:1; |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | #define DBC_QUEUE_SIZE 16 |
| 96 | #define DBC_WRITE_BUF_SIZE 8192 |
Mathias Nyman | fb18e5b | 2024-02-29 16:14:37 +0200 | [diff] [blame] | 97 | #define DBC_POLL_INTERVAL_DEFAULT 64 /* milliseconds */ |
Uday M Bhat | de3edd4 | 2024-06-26 15:48:18 +0300 | [diff] [blame] | 98 | #define DBC_POLL_INTERVAL_MAX 5000 /* milliseconds */ |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 99 | /* |
| 100 | * Private structure for DbC hardware state: |
| 101 | */ |
| 102 | struct dbc_port { |
| 103 | struct tty_port port; |
| 104 | spinlock_t port_lock; /* port access */ |
Mathias Nyman | e1ec140 | 2022-02-16 11:51:49 +0200 | [diff] [blame] | 105 | int minor; |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 106 | |
| 107 | struct list_head read_pool; |
| 108 | struct list_head read_queue; |
| 109 | unsigned int n_read; |
| 110 | struct tasklet_struct push; |
| 111 | |
| 112 | struct list_head write_pool; |
Mathias Nyman | 30c9ae5 | 2024-10-16 17:00:00 +0300 | [diff] [blame] | 113 | unsigned int tx_boundary; |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 114 | |
| 115 | bool registered; |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
Mathias Nyman | 6ae6470 | 2020-07-23 17:45:28 +0300 | [diff] [blame] | 118 | struct dbc_driver { |
| 119 | int (*configure)(struct xhci_dbc *dbc); |
| 120 | void (*disconnect)(struct xhci_dbc *dbc); |
| 121 | }; |
| 122 | |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 123 | struct xhci_dbc { |
| 124 | spinlock_t lock; /* device access */ |
Mathias Nyman | be33f48 | 2020-07-23 17:45:10 +0300 | [diff] [blame] | 125 | struct device *dev; |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 126 | struct xhci_hcd *xhci; |
| 127 | struct dbc_regs __iomem *regs; |
| 128 | struct xhci_ring *ring_evt; |
| 129 | struct xhci_ring *ring_in; |
| 130 | struct xhci_ring *ring_out; |
| 131 | struct xhci_erst erst; |
| 132 | struct xhci_container_ctx *ctx; |
| 133 | |
| 134 | struct dbc_str_descs *string; |
| 135 | dma_addr_t string_dma; |
| 136 | size_t string_size; |
Mathias Nyman | edf1664 | 2023-03-17 17:47:09 +0200 | [diff] [blame] | 137 | u16 idVendor; |
| 138 | u16 idProduct; |
| 139 | u16 bcdDevice; |
| 140 | u8 bInterfaceProtocol; |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 141 | |
| 142 | enum dbc_state state; |
| 143 | struct delayed_work event_work; |
Mathias Nyman | fb18e5b | 2024-02-29 16:14:37 +0200 | [diff] [blame] | 144 | unsigned int poll_interval; /* ms */ |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 145 | unsigned resume_required:1; |
| 146 | struct dbc_ep eps[2]; |
| 147 | |
Mathias Nyman | 6ae6470 | 2020-07-23 17:45:28 +0300 | [diff] [blame] | 148 | const struct dbc_driver *driver; |
Mathias Nyman | 9a360a7 | 2020-07-23 17:45:30 +0300 | [diff] [blame] | 149 | void *priv; |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 150 | }; |
| 151 | |
Mathias Nyman | f39f3af | 2020-07-23 17:45:20 +0300 | [diff] [blame] | 152 | struct dbc_request { |
| 153 | void *buf; |
| 154 | unsigned int length; |
| 155 | dma_addr_t dma; |
| 156 | void (*complete)(struct xhci_dbc *dbc, |
| 157 | struct dbc_request *req); |
| 158 | struct list_head list_pool; |
| 159 | int status; |
| 160 | unsigned int actual; |
| 161 | |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 162 | struct xhci_dbc *dbc; |
Mathias Nyman | f39f3af | 2020-07-23 17:45:20 +0300 | [diff] [blame] | 163 | struct list_head list_pending; |
| 164 | dma_addr_t trb_dma; |
| 165 | union xhci_trb *trb; |
| 166 | unsigned direction:1; |
| 167 | }; |
| 168 | |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 169 | #define dbc_bulkout_ctx(d) \ |
| 170 | ((struct xhci_ep_ctx *)((d)->ctx->bytes + DBC_CONTEXT_SIZE)) |
| 171 | #define dbc_bulkin_ctx(d) \ |
| 172 | ((struct xhci_ep_ctx *)((d)->ctx->bytes + DBC_CONTEXT_SIZE * 2)) |
| 173 | #define dbc_bulkout_enq(d) \ |
| 174 | xhci_trb_virt_to_dma((d)->ring_out->enq_seg, (d)->ring_out->enqueue) |
| 175 | #define dbc_bulkin_enq(d) \ |
| 176 | xhci_trb_virt_to_dma((d)->ring_in->enq_seg, (d)->ring_in->enqueue) |
| 177 | #define dbc_epctx_info2(t, p, b) \ |
| 178 | cpu_to_le32(EP_TYPE(t) | MAX_PACKET(p) | MAX_BURST(b)) |
| 179 | #define dbc_ep_dma_direction(d) \ |
| 180 | ((d)->direction ? DMA_FROM_DEVICE : DMA_TO_DEVICE) |
| 181 | |
| 182 | #define BULK_OUT 0 |
| 183 | #define BULK_IN 1 |
| 184 | #define EPID_OUT 2 |
| 185 | #define EPID_IN 3 |
| 186 | |
| 187 | enum evtreturn { |
| 188 | EVT_ERR = -1, |
| 189 | EVT_DONE, |
| 190 | EVT_GSER, |
| 191 | EVT_DISC, |
| 192 | }; |
| 193 | |
Mathias Nyman | 91aaf97 | 2020-07-23 17:45:19 +0300 | [diff] [blame] | 194 | static inline struct dbc_ep *get_in_ep(struct xhci_dbc *dbc) |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 195 | { |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 196 | return &dbc->eps[BULK_IN]; |
| 197 | } |
| 198 | |
Mathias Nyman | 91aaf97 | 2020-07-23 17:45:19 +0300 | [diff] [blame] | 199 | static inline struct dbc_ep *get_out_ep(struct xhci_dbc *dbc) |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 200 | { |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 201 | return &dbc->eps[BULK_OUT]; |
| 202 | } |
| 203 | |
| 204 | #ifdef CONFIG_USB_XHCI_DBGCAP |
Mathias Nyman | 5c44d9d | 2022-02-16 11:51:47 +0200 | [diff] [blame] | 205 | int xhci_create_dbc_dev(struct xhci_hcd *xhci); |
| 206 | void xhci_remove_dbc_dev(struct xhci_hcd *xhci); |
Mathias Nyman | 6aec500 | 2022-02-16 11:51:48 +0200 | [diff] [blame] | 207 | int xhci_dbc_init(void); |
| 208 | void xhci_dbc_exit(void); |
| 209 | int dbc_tty_init(void); |
| 210 | void dbc_tty_exit(void); |
Mathias Nyman | 5ce036b | 2022-02-16 11:51:46 +0200 | [diff] [blame] | 211 | int xhci_dbc_tty_probe(struct device *dev, void __iomem *res, struct xhci_hcd *xhci); |
Mathias Nyman | 4521f16 | 2020-07-23 17:45:27 +0300 | [diff] [blame] | 212 | void xhci_dbc_tty_remove(struct xhci_dbc *dbc); |
Mathias Nyman | 5ce036b | 2022-02-16 11:51:46 +0200 | [diff] [blame] | 213 | struct xhci_dbc *xhci_alloc_dbc(struct device *dev, void __iomem *res, |
| 214 | const struct dbc_driver *driver); |
| 215 | void xhci_dbc_remove(struct xhci_dbc *dbc); |
Mathias Nyman | e0aa56d | 2020-07-23 17:45:25 +0300 | [diff] [blame] | 216 | struct dbc_request *dbc_alloc_request(struct xhci_dbc *dbc, |
| 217 | unsigned int direction, |
| 218 | gfp_t flags); |
| 219 | void dbc_free_request(struct dbc_request *req); |
| 220 | int dbc_ep_queue(struct dbc_request *req); |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 221 | #ifdef CONFIG_PM |
| 222 | int xhci_dbc_suspend(struct xhci_hcd *xhci); |
| 223 | int xhci_dbc_resume(struct xhci_hcd *xhci); |
| 224 | #endif /* CONFIG_PM */ |
| 225 | #else |
Mathias Nyman | 5c44d9d | 2022-02-16 11:51:47 +0200 | [diff] [blame] | 226 | static inline int xhci_create_dbc_dev(struct xhci_hcd *xhci) |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 227 | { |
| 228 | return 0; |
| 229 | } |
| 230 | |
Mathias Nyman | 5c44d9d | 2022-02-16 11:51:47 +0200 | [diff] [blame] | 231 | static inline void xhci_remove_dbc_dev(struct xhci_hcd *xhci) |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 232 | { |
| 233 | } |
Mathias Nyman | 6aec500 | 2022-02-16 11:51:48 +0200 | [diff] [blame] | 234 | static inline int xhci_dbc_init(void) |
| 235 | { |
| 236 | return 0; |
| 237 | } |
| 238 | static inline void xhci_dbc_exit(void) |
| 239 | { |
| 240 | } |
Lu Baolu | dfba217 | 2017-12-08 17:59:10 +0200 | [diff] [blame] | 241 | static inline int xhci_dbc_suspend(struct xhci_hcd *xhci) |
| 242 | { |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static inline int xhci_dbc_resume(struct xhci_hcd *xhci) |
| 247 | { |
| 248 | return 0; |
| 249 | } |
| 250 | #endif /* CONFIG_USB_XHCI_DBGCAP */ |
| 251 | #endif /* __LINUX_XHCI_DBGCAP_H */ |