xref: /openbmc/linux/drivers/usb/dwc3/debugfs.c (revision d2ba09c1)
1 // SPDX-License-Identifier: GPL-2.0
2 /**
3  * debugfs.c - DesignWare USB3 DRD Controller DebugFS file
4  *
5  * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
6  *
7  * Authors: Felipe Balbi <balbi@ti.com>,
8  *	    Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/ptrace.h>
14 #include <linux/types.h>
15 #include <linux/spinlock.h>
16 #include <linux/debugfs.h>
17 #include <linux/seq_file.h>
18 #include <linux/delay.h>
19 #include <linux/uaccess.h>
20 
21 #include <linux/usb/ch9.h>
22 
23 #include "core.h"
24 #include "gadget.h"
25 #include "io.h"
26 #include "debug.h"
27 
28 #define dump_register(nm)				\
29 {							\
30 	.name	= __stringify(nm),			\
31 	.offset	= DWC3_ ##nm,				\
32 }
33 
34 #define dump_ep_register_set(n)			\
35 	{					\
36 		.name = "DEPCMDPAR2("__stringify(n)")",	\
37 		.offset = DWC3_DEP_BASE(n) +	\
38 			DWC3_DEPCMDPAR2,	\
39 	},					\
40 	{					\
41 		.name = "DEPCMDPAR1("__stringify(n)")",	\
42 		.offset = DWC3_DEP_BASE(n) +	\
43 			DWC3_DEPCMDPAR1,	\
44 	},					\
45 	{					\
46 		.name = "DEPCMDPAR0("__stringify(n)")",	\
47 		.offset = DWC3_DEP_BASE(n) +	\
48 			DWC3_DEPCMDPAR0,	\
49 	},					\
50 	{					\
51 		.name = "DEPCMD("__stringify(n)")",	\
52 		.offset = DWC3_DEP_BASE(n) +	\
53 			DWC3_DEPCMD,		\
54 	}
55 
56 
57 static const struct debugfs_reg32 dwc3_regs[] = {
58 	dump_register(GSBUSCFG0),
59 	dump_register(GSBUSCFG1),
60 	dump_register(GTXTHRCFG),
61 	dump_register(GRXTHRCFG),
62 	dump_register(GCTL),
63 	dump_register(GEVTEN),
64 	dump_register(GSTS),
65 	dump_register(GUCTL1),
66 	dump_register(GSNPSID),
67 	dump_register(GGPIO),
68 	dump_register(GUID),
69 	dump_register(GUCTL),
70 	dump_register(GBUSERRADDR0),
71 	dump_register(GBUSERRADDR1),
72 	dump_register(GPRTBIMAP0),
73 	dump_register(GPRTBIMAP1),
74 	dump_register(GHWPARAMS0),
75 	dump_register(GHWPARAMS1),
76 	dump_register(GHWPARAMS2),
77 	dump_register(GHWPARAMS3),
78 	dump_register(GHWPARAMS4),
79 	dump_register(GHWPARAMS5),
80 	dump_register(GHWPARAMS6),
81 	dump_register(GHWPARAMS7),
82 	dump_register(GDBGFIFOSPACE),
83 	dump_register(GDBGLTSSM),
84 	dump_register(GDBGBMU),
85 	dump_register(GDBGLSPMUX),
86 	dump_register(GDBGLSP),
87 	dump_register(GDBGEPINFO0),
88 	dump_register(GDBGEPINFO1),
89 	dump_register(GPRTBIMAP_HS0),
90 	dump_register(GPRTBIMAP_HS1),
91 	dump_register(GPRTBIMAP_FS0),
92 	dump_register(GPRTBIMAP_FS1),
93 
94 	dump_register(GUSB2PHYCFG(0)),
95 	dump_register(GUSB2PHYCFG(1)),
96 	dump_register(GUSB2PHYCFG(2)),
97 	dump_register(GUSB2PHYCFG(3)),
98 	dump_register(GUSB2PHYCFG(4)),
99 	dump_register(GUSB2PHYCFG(5)),
100 	dump_register(GUSB2PHYCFG(6)),
101 	dump_register(GUSB2PHYCFG(7)),
102 	dump_register(GUSB2PHYCFG(8)),
103 	dump_register(GUSB2PHYCFG(9)),
104 	dump_register(GUSB2PHYCFG(10)),
105 	dump_register(GUSB2PHYCFG(11)),
106 	dump_register(GUSB2PHYCFG(12)),
107 	dump_register(GUSB2PHYCFG(13)),
108 	dump_register(GUSB2PHYCFG(14)),
109 	dump_register(GUSB2PHYCFG(15)),
110 
111 	dump_register(GUSB2I2CCTL(0)),
112 	dump_register(GUSB2I2CCTL(1)),
113 	dump_register(GUSB2I2CCTL(2)),
114 	dump_register(GUSB2I2CCTL(3)),
115 	dump_register(GUSB2I2CCTL(4)),
116 	dump_register(GUSB2I2CCTL(5)),
117 	dump_register(GUSB2I2CCTL(6)),
118 	dump_register(GUSB2I2CCTL(7)),
119 	dump_register(GUSB2I2CCTL(8)),
120 	dump_register(GUSB2I2CCTL(9)),
121 	dump_register(GUSB2I2CCTL(10)),
122 	dump_register(GUSB2I2CCTL(11)),
123 	dump_register(GUSB2I2CCTL(12)),
124 	dump_register(GUSB2I2CCTL(13)),
125 	dump_register(GUSB2I2CCTL(14)),
126 	dump_register(GUSB2I2CCTL(15)),
127 
128 	dump_register(GUSB2PHYACC(0)),
129 	dump_register(GUSB2PHYACC(1)),
130 	dump_register(GUSB2PHYACC(2)),
131 	dump_register(GUSB2PHYACC(3)),
132 	dump_register(GUSB2PHYACC(4)),
133 	dump_register(GUSB2PHYACC(5)),
134 	dump_register(GUSB2PHYACC(6)),
135 	dump_register(GUSB2PHYACC(7)),
136 	dump_register(GUSB2PHYACC(8)),
137 	dump_register(GUSB2PHYACC(9)),
138 	dump_register(GUSB2PHYACC(10)),
139 	dump_register(GUSB2PHYACC(11)),
140 	dump_register(GUSB2PHYACC(12)),
141 	dump_register(GUSB2PHYACC(13)),
142 	dump_register(GUSB2PHYACC(14)),
143 	dump_register(GUSB2PHYACC(15)),
144 
145 	dump_register(GUSB3PIPECTL(0)),
146 	dump_register(GUSB3PIPECTL(1)),
147 	dump_register(GUSB3PIPECTL(2)),
148 	dump_register(GUSB3PIPECTL(3)),
149 	dump_register(GUSB3PIPECTL(4)),
150 	dump_register(GUSB3PIPECTL(5)),
151 	dump_register(GUSB3PIPECTL(6)),
152 	dump_register(GUSB3PIPECTL(7)),
153 	dump_register(GUSB3PIPECTL(8)),
154 	dump_register(GUSB3PIPECTL(9)),
155 	dump_register(GUSB3PIPECTL(10)),
156 	dump_register(GUSB3PIPECTL(11)),
157 	dump_register(GUSB3PIPECTL(12)),
158 	dump_register(GUSB3PIPECTL(13)),
159 	dump_register(GUSB3PIPECTL(14)),
160 	dump_register(GUSB3PIPECTL(15)),
161 
162 	dump_register(GTXFIFOSIZ(0)),
163 	dump_register(GTXFIFOSIZ(1)),
164 	dump_register(GTXFIFOSIZ(2)),
165 	dump_register(GTXFIFOSIZ(3)),
166 	dump_register(GTXFIFOSIZ(4)),
167 	dump_register(GTXFIFOSIZ(5)),
168 	dump_register(GTXFIFOSIZ(6)),
169 	dump_register(GTXFIFOSIZ(7)),
170 	dump_register(GTXFIFOSIZ(8)),
171 	dump_register(GTXFIFOSIZ(9)),
172 	dump_register(GTXFIFOSIZ(10)),
173 	dump_register(GTXFIFOSIZ(11)),
174 	dump_register(GTXFIFOSIZ(12)),
175 	dump_register(GTXFIFOSIZ(13)),
176 	dump_register(GTXFIFOSIZ(14)),
177 	dump_register(GTXFIFOSIZ(15)),
178 	dump_register(GTXFIFOSIZ(16)),
179 	dump_register(GTXFIFOSIZ(17)),
180 	dump_register(GTXFIFOSIZ(18)),
181 	dump_register(GTXFIFOSIZ(19)),
182 	dump_register(GTXFIFOSIZ(20)),
183 	dump_register(GTXFIFOSIZ(21)),
184 	dump_register(GTXFIFOSIZ(22)),
185 	dump_register(GTXFIFOSIZ(23)),
186 	dump_register(GTXFIFOSIZ(24)),
187 	dump_register(GTXFIFOSIZ(25)),
188 	dump_register(GTXFIFOSIZ(26)),
189 	dump_register(GTXFIFOSIZ(27)),
190 	dump_register(GTXFIFOSIZ(28)),
191 	dump_register(GTXFIFOSIZ(29)),
192 	dump_register(GTXFIFOSIZ(30)),
193 	dump_register(GTXFIFOSIZ(31)),
194 
195 	dump_register(GRXFIFOSIZ(0)),
196 	dump_register(GRXFIFOSIZ(1)),
197 	dump_register(GRXFIFOSIZ(2)),
198 	dump_register(GRXFIFOSIZ(3)),
199 	dump_register(GRXFIFOSIZ(4)),
200 	dump_register(GRXFIFOSIZ(5)),
201 	dump_register(GRXFIFOSIZ(6)),
202 	dump_register(GRXFIFOSIZ(7)),
203 	dump_register(GRXFIFOSIZ(8)),
204 	dump_register(GRXFIFOSIZ(9)),
205 	dump_register(GRXFIFOSIZ(10)),
206 	dump_register(GRXFIFOSIZ(11)),
207 	dump_register(GRXFIFOSIZ(12)),
208 	dump_register(GRXFIFOSIZ(13)),
209 	dump_register(GRXFIFOSIZ(14)),
210 	dump_register(GRXFIFOSIZ(15)),
211 	dump_register(GRXFIFOSIZ(16)),
212 	dump_register(GRXFIFOSIZ(17)),
213 	dump_register(GRXFIFOSIZ(18)),
214 	dump_register(GRXFIFOSIZ(19)),
215 	dump_register(GRXFIFOSIZ(20)),
216 	dump_register(GRXFIFOSIZ(21)),
217 	dump_register(GRXFIFOSIZ(22)),
218 	dump_register(GRXFIFOSIZ(23)),
219 	dump_register(GRXFIFOSIZ(24)),
220 	dump_register(GRXFIFOSIZ(25)),
221 	dump_register(GRXFIFOSIZ(26)),
222 	dump_register(GRXFIFOSIZ(27)),
223 	dump_register(GRXFIFOSIZ(28)),
224 	dump_register(GRXFIFOSIZ(29)),
225 	dump_register(GRXFIFOSIZ(30)),
226 	dump_register(GRXFIFOSIZ(31)),
227 
228 	dump_register(GEVNTADRLO(0)),
229 	dump_register(GEVNTADRHI(0)),
230 	dump_register(GEVNTSIZ(0)),
231 	dump_register(GEVNTCOUNT(0)),
232 
233 	dump_register(GHWPARAMS8),
234 	dump_register(DCFG),
235 	dump_register(DCTL),
236 	dump_register(DEVTEN),
237 	dump_register(DSTS),
238 	dump_register(DGCMDPAR),
239 	dump_register(DGCMD),
240 	dump_register(DALEPENA),
241 
242 	dump_ep_register_set(0),
243 	dump_ep_register_set(1),
244 	dump_ep_register_set(2),
245 	dump_ep_register_set(3),
246 	dump_ep_register_set(4),
247 	dump_ep_register_set(5),
248 	dump_ep_register_set(6),
249 	dump_ep_register_set(7),
250 	dump_ep_register_set(8),
251 	dump_ep_register_set(9),
252 	dump_ep_register_set(10),
253 	dump_ep_register_set(11),
254 	dump_ep_register_set(12),
255 	dump_ep_register_set(13),
256 	dump_ep_register_set(14),
257 	dump_ep_register_set(15),
258 	dump_ep_register_set(16),
259 	dump_ep_register_set(17),
260 	dump_ep_register_set(18),
261 	dump_ep_register_set(19),
262 	dump_ep_register_set(20),
263 	dump_ep_register_set(21),
264 	dump_ep_register_set(22),
265 	dump_ep_register_set(23),
266 	dump_ep_register_set(24),
267 	dump_ep_register_set(25),
268 	dump_ep_register_set(26),
269 	dump_ep_register_set(27),
270 	dump_ep_register_set(28),
271 	dump_ep_register_set(29),
272 	dump_ep_register_set(30),
273 	dump_ep_register_set(31),
274 
275 	dump_register(OCFG),
276 	dump_register(OCTL),
277 	dump_register(OEVT),
278 	dump_register(OEVTEN),
279 	dump_register(OSTS),
280 };
281 
282 static int dwc3_mode_show(struct seq_file *s, void *unused)
283 {
284 	struct dwc3		*dwc = s->private;
285 	unsigned long		flags;
286 	u32			reg;
287 
288 	spin_lock_irqsave(&dwc->lock, flags);
289 	reg = dwc3_readl(dwc->regs, DWC3_GCTL);
290 	spin_unlock_irqrestore(&dwc->lock, flags);
291 
292 	switch (DWC3_GCTL_PRTCAP(reg)) {
293 	case DWC3_GCTL_PRTCAP_HOST:
294 		seq_printf(s, "host\n");
295 		break;
296 	case DWC3_GCTL_PRTCAP_DEVICE:
297 		seq_printf(s, "device\n");
298 		break;
299 	case DWC3_GCTL_PRTCAP_OTG:
300 		seq_printf(s, "otg\n");
301 		break;
302 	default:
303 		seq_printf(s, "UNKNOWN %08x\n", DWC3_GCTL_PRTCAP(reg));
304 	}
305 
306 	return 0;
307 }
308 
309 static int dwc3_mode_open(struct inode *inode, struct file *file)
310 {
311 	return single_open(file, dwc3_mode_show, inode->i_private);
312 }
313 
314 static ssize_t dwc3_mode_write(struct file *file,
315 		const char __user *ubuf, size_t count, loff_t *ppos)
316 {
317 	struct seq_file		*s = file->private_data;
318 	struct dwc3		*dwc = s->private;
319 	u32			mode = 0;
320 	char			buf[32];
321 
322 	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
323 		return -EFAULT;
324 
325 	if (!strncmp(buf, "host", 4))
326 		mode = DWC3_GCTL_PRTCAP_HOST;
327 
328 	if (!strncmp(buf, "device", 6))
329 		mode = DWC3_GCTL_PRTCAP_DEVICE;
330 
331 	if (!strncmp(buf, "otg", 3))
332 		mode = DWC3_GCTL_PRTCAP_OTG;
333 
334 	dwc3_set_mode(dwc, mode);
335 
336 	return count;
337 }
338 
339 static const struct file_operations dwc3_mode_fops = {
340 	.open			= dwc3_mode_open,
341 	.write			= dwc3_mode_write,
342 	.read			= seq_read,
343 	.llseek			= seq_lseek,
344 	.release		= single_release,
345 };
346 
347 static int dwc3_testmode_show(struct seq_file *s, void *unused)
348 {
349 	struct dwc3		*dwc = s->private;
350 	unsigned long		flags;
351 	u32			reg;
352 
353 	spin_lock_irqsave(&dwc->lock, flags);
354 	reg = dwc3_readl(dwc->regs, DWC3_DCTL);
355 	reg &= DWC3_DCTL_TSTCTRL_MASK;
356 	reg >>= 1;
357 	spin_unlock_irqrestore(&dwc->lock, flags);
358 
359 	switch (reg) {
360 	case 0:
361 		seq_printf(s, "no test\n");
362 		break;
363 	case TEST_J:
364 		seq_printf(s, "test_j\n");
365 		break;
366 	case TEST_K:
367 		seq_printf(s, "test_k\n");
368 		break;
369 	case TEST_SE0_NAK:
370 		seq_printf(s, "test_se0_nak\n");
371 		break;
372 	case TEST_PACKET:
373 		seq_printf(s, "test_packet\n");
374 		break;
375 	case TEST_FORCE_EN:
376 		seq_printf(s, "test_force_enable\n");
377 		break;
378 	default:
379 		seq_printf(s, "UNKNOWN %d\n", reg);
380 	}
381 
382 	return 0;
383 }
384 
385 static int dwc3_testmode_open(struct inode *inode, struct file *file)
386 {
387 	return single_open(file, dwc3_testmode_show, inode->i_private);
388 }
389 
390 static ssize_t dwc3_testmode_write(struct file *file,
391 		const char __user *ubuf, size_t count, loff_t *ppos)
392 {
393 	struct seq_file		*s = file->private_data;
394 	struct dwc3		*dwc = s->private;
395 	unsigned long		flags;
396 	u32			testmode = 0;
397 	char			buf[32];
398 
399 	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
400 		return -EFAULT;
401 
402 	if (!strncmp(buf, "test_j", 6))
403 		testmode = TEST_J;
404 	else if (!strncmp(buf, "test_k", 6))
405 		testmode = TEST_K;
406 	else if (!strncmp(buf, "test_se0_nak", 12))
407 		testmode = TEST_SE0_NAK;
408 	else if (!strncmp(buf, "test_packet", 11))
409 		testmode = TEST_PACKET;
410 	else if (!strncmp(buf, "test_force_enable", 17))
411 		testmode = TEST_FORCE_EN;
412 	else
413 		testmode = 0;
414 
415 	spin_lock_irqsave(&dwc->lock, flags);
416 	dwc3_gadget_set_test_mode(dwc, testmode);
417 	spin_unlock_irqrestore(&dwc->lock, flags);
418 
419 	return count;
420 }
421 
422 static const struct file_operations dwc3_testmode_fops = {
423 	.open			= dwc3_testmode_open,
424 	.write			= dwc3_testmode_write,
425 	.read			= seq_read,
426 	.llseek			= seq_lseek,
427 	.release		= single_release,
428 };
429 
430 static int dwc3_link_state_show(struct seq_file *s, void *unused)
431 {
432 	struct dwc3		*dwc = s->private;
433 	unsigned long		flags;
434 	enum dwc3_link_state	state;
435 	u32			reg;
436 
437 	spin_lock_irqsave(&dwc->lock, flags);
438 	reg = dwc3_readl(dwc->regs, DWC3_DSTS);
439 	state = DWC3_DSTS_USBLNKST(reg);
440 	spin_unlock_irqrestore(&dwc->lock, flags);
441 
442 	seq_printf(s, "%s\n", dwc3_gadget_link_string(state));
443 
444 	return 0;
445 }
446 
447 static int dwc3_link_state_open(struct inode *inode, struct file *file)
448 {
449 	return single_open(file, dwc3_link_state_show, inode->i_private);
450 }
451 
452 static ssize_t dwc3_link_state_write(struct file *file,
453 		const char __user *ubuf, size_t count, loff_t *ppos)
454 {
455 	struct seq_file		*s = file->private_data;
456 	struct dwc3		*dwc = s->private;
457 	unsigned long		flags;
458 	enum dwc3_link_state	state = 0;
459 	char			buf[32];
460 
461 	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
462 		return -EFAULT;
463 
464 	if (!strncmp(buf, "SS.Disabled", 11))
465 		state = DWC3_LINK_STATE_SS_DIS;
466 	else if (!strncmp(buf, "Rx.Detect", 9))
467 		state = DWC3_LINK_STATE_RX_DET;
468 	else if (!strncmp(buf, "SS.Inactive", 11))
469 		state = DWC3_LINK_STATE_SS_INACT;
470 	else if (!strncmp(buf, "Recovery", 8))
471 		state = DWC3_LINK_STATE_RECOV;
472 	else if (!strncmp(buf, "Compliance", 10))
473 		state = DWC3_LINK_STATE_CMPLY;
474 	else if (!strncmp(buf, "Loopback", 8))
475 		state = DWC3_LINK_STATE_LPBK;
476 	else
477 		return -EINVAL;
478 
479 	spin_lock_irqsave(&dwc->lock, flags);
480 	dwc3_gadget_set_link_state(dwc, state);
481 	spin_unlock_irqrestore(&dwc->lock, flags);
482 
483 	return count;
484 }
485 
486 static const struct file_operations dwc3_link_state_fops = {
487 	.open			= dwc3_link_state_open,
488 	.write			= dwc3_link_state_write,
489 	.read			= seq_read,
490 	.llseek			= seq_lseek,
491 	.release		= single_release,
492 };
493 
494 struct dwc3_ep_file_map {
495 	const char name[25];
496 	const struct file_operations *const fops;
497 };
498 
499 static int dwc3_tx_fifo_queue_show(struct seq_file *s, void *unused)
500 {
501 	struct dwc3_ep		*dep = s->private;
502 	struct dwc3		*dwc = dep->dwc;
503 	unsigned long		flags;
504 	u32			val;
505 
506 	spin_lock_irqsave(&dwc->lock, flags);
507 	val = dwc3_core_fifo_space(dep, DWC3_TXFIFOQ);
508 	seq_printf(s, "%u\n", val);
509 	spin_unlock_irqrestore(&dwc->lock, flags);
510 
511 	return 0;
512 }
513 
514 static int dwc3_rx_fifo_queue_show(struct seq_file *s, void *unused)
515 {
516 	struct dwc3_ep		*dep = s->private;
517 	struct dwc3		*dwc = dep->dwc;
518 	unsigned long		flags;
519 	u32			val;
520 
521 	spin_lock_irqsave(&dwc->lock, flags);
522 	val = dwc3_core_fifo_space(dep, DWC3_RXFIFOQ);
523 	seq_printf(s, "%u\n", val);
524 	spin_unlock_irqrestore(&dwc->lock, flags);
525 
526 	return 0;
527 }
528 
529 static int dwc3_tx_request_queue_show(struct seq_file *s, void *unused)
530 {
531 	struct dwc3_ep		*dep = s->private;
532 	struct dwc3		*dwc = dep->dwc;
533 	unsigned long		flags;
534 	u32			val;
535 
536 	spin_lock_irqsave(&dwc->lock, flags);
537 	val = dwc3_core_fifo_space(dep, DWC3_TXREQQ);
538 	seq_printf(s, "%u\n", val);
539 	spin_unlock_irqrestore(&dwc->lock, flags);
540 
541 	return 0;
542 }
543 
544 static int dwc3_rx_request_queue_show(struct seq_file *s, void *unused)
545 {
546 	struct dwc3_ep		*dep = s->private;
547 	struct dwc3		*dwc = dep->dwc;
548 	unsigned long		flags;
549 	u32			val;
550 
551 	spin_lock_irqsave(&dwc->lock, flags);
552 	val = dwc3_core_fifo_space(dep, DWC3_RXREQQ);
553 	seq_printf(s, "%u\n", val);
554 	spin_unlock_irqrestore(&dwc->lock, flags);
555 
556 	return 0;
557 }
558 
559 static int dwc3_rx_info_queue_show(struct seq_file *s, void *unused)
560 {
561 	struct dwc3_ep		*dep = s->private;
562 	struct dwc3		*dwc = dep->dwc;
563 	unsigned long		flags;
564 	u32			val;
565 
566 	spin_lock_irqsave(&dwc->lock, flags);
567 	val = dwc3_core_fifo_space(dep, DWC3_RXINFOQ);
568 	seq_printf(s, "%u\n", val);
569 	spin_unlock_irqrestore(&dwc->lock, flags);
570 
571 	return 0;
572 }
573 
574 static int dwc3_descriptor_fetch_queue_show(struct seq_file *s, void *unused)
575 {
576 	struct dwc3_ep		*dep = s->private;
577 	struct dwc3		*dwc = dep->dwc;
578 	unsigned long		flags;
579 	u32			val;
580 
581 	spin_lock_irqsave(&dwc->lock, flags);
582 	val = dwc3_core_fifo_space(dep, DWC3_DESCFETCHQ);
583 	seq_printf(s, "%u\n", val);
584 	spin_unlock_irqrestore(&dwc->lock, flags);
585 
586 	return 0;
587 }
588 
589 static int dwc3_event_queue_show(struct seq_file *s, void *unused)
590 {
591 	struct dwc3_ep		*dep = s->private;
592 	struct dwc3		*dwc = dep->dwc;
593 	unsigned long		flags;
594 	u32			val;
595 
596 	spin_lock_irqsave(&dwc->lock, flags);
597 	val = dwc3_core_fifo_space(dep, DWC3_EVENTQ);
598 	seq_printf(s, "%u\n", val);
599 	spin_unlock_irqrestore(&dwc->lock, flags);
600 
601 	return 0;
602 }
603 
604 static int dwc3_transfer_type_show(struct seq_file *s, void *unused)
605 {
606 	struct dwc3_ep		*dep = s->private;
607 	struct dwc3		*dwc = dep->dwc;
608 	unsigned long		flags;
609 
610 	spin_lock_irqsave(&dwc->lock, flags);
611 	if (!(dep->flags & DWC3_EP_ENABLED) ||
612 			!dep->endpoint.desc) {
613 		seq_printf(s, "--\n");
614 		goto out;
615 	}
616 
617 	switch (usb_endpoint_type(dep->endpoint.desc)) {
618 	case USB_ENDPOINT_XFER_CONTROL:
619 		seq_printf(s, "control\n");
620 		break;
621 	case USB_ENDPOINT_XFER_ISOC:
622 		seq_printf(s, "isochronous\n");
623 		break;
624 	case USB_ENDPOINT_XFER_BULK:
625 		seq_printf(s, "bulk\n");
626 		break;
627 	case USB_ENDPOINT_XFER_INT:
628 		seq_printf(s, "interrupt\n");
629 		break;
630 	default:
631 		seq_printf(s, "--\n");
632 	}
633 
634 out:
635 	spin_unlock_irqrestore(&dwc->lock, flags);
636 
637 	return 0;
638 }
639 
640 static int dwc3_trb_ring_show(struct seq_file *s, void *unused)
641 {
642 	struct dwc3_ep		*dep = s->private;
643 	struct dwc3		*dwc = dep->dwc;
644 	unsigned long		flags;
645 	int			i;
646 
647 	spin_lock_irqsave(&dwc->lock, flags);
648 	if (dep->number <= 1) {
649 		seq_printf(s, "--\n");
650 		goto out;
651 	}
652 
653 	seq_printf(s, "buffer_addr,size,type,ioc,isp_imi,csp,chn,lst,hwo\n");
654 
655 	for (i = 0; i < DWC3_TRB_NUM; i++) {
656 		struct dwc3_trb *trb = &dep->trb_pool[i];
657 		unsigned int type = DWC3_TRBCTL_TYPE(trb->ctrl);
658 
659 		seq_printf(s, "%08x%08x,%d,%s,%d,%d,%d,%d,%d,%d       %c%c\n",
660 				trb->bph, trb->bpl, trb->size,
661 				dwc3_trb_type_string(type),
662 				!!(trb->ctrl & DWC3_TRB_CTRL_IOC),
663 				!!(trb->ctrl & DWC3_TRB_CTRL_ISP_IMI),
664 				!!(trb->ctrl & DWC3_TRB_CTRL_CSP),
665 				!!(trb->ctrl & DWC3_TRB_CTRL_CHN),
666 				!!(trb->ctrl & DWC3_TRB_CTRL_LST),
667 				!!(trb->ctrl & DWC3_TRB_CTRL_HWO),
668 				dep->trb_enqueue == i ? 'E' : ' ',
669 				dep->trb_dequeue == i ? 'D' : ' ');
670 	}
671 
672 out:
673 	spin_unlock_irqrestore(&dwc->lock, flags);
674 
675 	return 0;
676 }
677 
678 DEFINE_SHOW_ATTRIBUTE(dwc3_tx_fifo_queue);
679 DEFINE_SHOW_ATTRIBUTE(dwc3_rx_fifo_queue);
680 DEFINE_SHOW_ATTRIBUTE(dwc3_tx_request_queue);
681 DEFINE_SHOW_ATTRIBUTE(dwc3_rx_request_queue);
682 DEFINE_SHOW_ATTRIBUTE(dwc3_rx_info_queue);
683 DEFINE_SHOW_ATTRIBUTE(dwc3_descriptor_fetch_queue);
684 DEFINE_SHOW_ATTRIBUTE(dwc3_event_queue);
685 DEFINE_SHOW_ATTRIBUTE(dwc3_transfer_type);
686 DEFINE_SHOW_ATTRIBUTE(dwc3_trb_ring);
687 
688 static const struct dwc3_ep_file_map dwc3_ep_file_map[] = {
689 	{ "tx_fifo_queue", &dwc3_tx_fifo_queue_fops, },
690 	{ "rx_fifo_queue", &dwc3_rx_fifo_queue_fops, },
691 	{ "tx_request_queue", &dwc3_tx_request_queue_fops, },
692 	{ "rx_request_queue", &dwc3_rx_request_queue_fops, },
693 	{ "rx_info_queue", &dwc3_rx_info_queue_fops, },
694 	{ "descriptor_fetch_queue", &dwc3_descriptor_fetch_queue_fops, },
695 	{ "event_queue", &dwc3_event_queue_fops, },
696 	{ "transfer_type", &dwc3_transfer_type_fops, },
697 	{ "trb_ring", &dwc3_trb_ring_fops, },
698 };
699 
700 static void dwc3_debugfs_create_endpoint_files(struct dwc3_ep *dep,
701 		struct dentry *parent)
702 {
703 	int			i;
704 
705 	for (i = 0; i < ARRAY_SIZE(dwc3_ep_file_map); i++) {
706 		const struct file_operations *fops = dwc3_ep_file_map[i].fops;
707 		const char *name = dwc3_ep_file_map[i].name;
708 
709 		debugfs_create_file(name, S_IRUGO, parent, dep, fops);
710 	}
711 }
712 
713 static void dwc3_debugfs_create_endpoint_dir(struct dwc3_ep *dep,
714 		struct dentry *parent)
715 {
716 	struct dentry		*dir;
717 
718 	dir = debugfs_create_dir(dep->name, parent);
719 	if (IS_ERR_OR_NULL(dir))
720 		return;
721 
722 	dwc3_debugfs_create_endpoint_files(dep, dir);
723 }
724 
725 static void dwc3_debugfs_create_endpoint_dirs(struct dwc3 *dwc,
726 		struct dentry *parent)
727 {
728 	int			i;
729 
730 	for (i = 0; i < dwc->num_eps; i++) {
731 		struct dwc3_ep	*dep = dwc->eps[i];
732 
733 		if (!dep)
734 			continue;
735 
736 		dwc3_debugfs_create_endpoint_dir(dep, parent);
737 	}
738 }
739 
740 void dwc3_debugfs_init(struct dwc3 *dwc)
741 {
742 	struct dentry		*root;
743 	struct dentry           *file;
744 
745 	root = debugfs_create_dir(dev_name(dwc->dev), NULL);
746 	if (IS_ERR_OR_NULL(root)) {
747 		if (!root)
748 			dev_err(dwc->dev, "Can't create debugfs root\n");
749 		return;
750 	}
751 	dwc->root = root;
752 
753 	dwc->regset = kzalloc(sizeof(*dwc->regset), GFP_KERNEL);
754 	if (!dwc->regset) {
755 		debugfs_remove_recursive(root);
756 		return;
757 	}
758 
759 	dwc->regset->regs = dwc3_regs;
760 	dwc->regset->nregs = ARRAY_SIZE(dwc3_regs);
761 	dwc->regset->base = dwc->regs - DWC3_GLOBALS_REGS_START;
762 
763 	file = debugfs_create_regset32("regdump", S_IRUGO, root, dwc->regset);
764 	if (!file)
765 		dev_dbg(dwc->dev, "Can't create debugfs regdump\n");
766 
767 	if (IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)) {
768 		file = debugfs_create_file("mode", S_IRUGO | S_IWUSR, root,
769 				dwc, &dwc3_mode_fops);
770 		if (!file)
771 			dev_dbg(dwc->dev, "Can't create debugfs mode\n");
772 	}
773 
774 	if (IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE) ||
775 			IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
776 		file = debugfs_create_file("testmode", S_IRUGO | S_IWUSR, root,
777 				dwc, &dwc3_testmode_fops);
778 		if (!file)
779 			dev_dbg(dwc->dev, "Can't create debugfs testmode\n");
780 
781 		file = debugfs_create_file("link_state", S_IRUGO | S_IWUSR,
782 				root, dwc, &dwc3_link_state_fops);
783 		if (!file)
784 			dev_dbg(dwc->dev, "Can't create debugfs link_state\n");
785 
786 		dwc3_debugfs_create_endpoint_dirs(dwc, root);
787 	}
788 }
789 
790 void dwc3_debugfs_exit(struct dwc3 *dwc)
791 {
792 	debugfs_remove_recursive(dwc->root);
793 	kfree(dwc->regset);
794 }
795