xref: /openbmc/linux/drivers/usb/host/xhci-debugfs.c (revision 25b892b5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * xhci-debugfs.c - xHCI debugfs interface
4  *
5  * Copyright (C) 2017 Intel Corporation
6  *
7  * Author: Lu Baolu <baolu.lu@linux.intel.com>
8  */
9 
10 #include <linux/slab.h>
11 #include <linux/uaccess.h>
12 
13 #include "xhci.h"
14 #include "xhci-debugfs.h"
15 
16 static const struct debugfs_reg32 xhci_cap_regs[] = {
17 	dump_register(CAPLENGTH),
18 	dump_register(HCSPARAMS1),
19 	dump_register(HCSPARAMS2),
20 	dump_register(HCSPARAMS3),
21 	dump_register(HCCPARAMS1),
22 	dump_register(DOORBELLOFF),
23 	dump_register(RUNTIMEOFF),
24 	dump_register(HCCPARAMS2),
25 };
26 
27 static const struct debugfs_reg32 xhci_op_regs[] = {
28 	dump_register(USBCMD),
29 	dump_register(USBSTS),
30 	dump_register(PAGESIZE),
31 	dump_register(DNCTRL),
32 	dump_register(CRCR),
33 	dump_register(DCBAAP_LOW),
34 	dump_register(DCBAAP_HIGH),
35 	dump_register(CONFIG),
36 };
37 
38 static const struct debugfs_reg32 xhci_runtime_regs[] = {
39 	dump_register(MFINDEX),
40 	dump_register(IR0_IMAN),
41 	dump_register(IR0_IMOD),
42 	dump_register(IR0_ERSTSZ),
43 	dump_register(IR0_ERSTBA_LOW),
44 	dump_register(IR0_ERSTBA_HIGH),
45 	dump_register(IR0_ERDP_LOW),
46 	dump_register(IR0_ERDP_HIGH),
47 };
48 
49 static const struct debugfs_reg32 xhci_extcap_legsup[] = {
50 	dump_register(EXTCAP_USBLEGSUP),
51 	dump_register(EXTCAP_USBLEGCTLSTS),
52 };
53 
54 static const struct debugfs_reg32 xhci_extcap_protocol[] = {
55 	dump_register(EXTCAP_REVISION),
56 	dump_register(EXTCAP_NAME),
57 	dump_register(EXTCAP_PORTINFO),
58 	dump_register(EXTCAP_PORTTYPE),
59 	dump_register(EXTCAP_MANTISSA1),
60 	dump_register(EXTCAP_MANTISSA2),
61 	dump_register(EXTCAP_MANTISSA3),
62 	dump_register(EXTCAP_MANTISSA4),
63 	dump_register(EXTCAP_MANTISSA5),
64 	dump_register(EXTCAP_MANTISSA6),
65 };
66 
67 static const struct debugfs_reg32 xhci_extcap_dbc[] = {
68 	dump_register(EXTCAP_DBC_CAPABILITY),
69 	dump_register(EXTCAP_DBC_DOORBELL),
70 	dump_register(EXTCAP_DBC_ERSTSIZE),
71 	dump_register(EXTCAP_DBC_ERST_LOW),
72 	dump_register(EXTCAP_DBC_ERST_HIGH),
73 	dump_register(EXTCAP_DBC_ERDP_LOW),
74 	dump_register(EXTCAP_DBC_ERDP_HIGH),
75 	dump_register(EXTCAP_DBC_CONTROL),
76 	dump_register(EXTCAP_DBC_STATUS),
77 	dump_register(EXTCAP_DBC_PORTSC),
78 	dump_register(EXTCAP_DBC_CONT_LOW),
79 	dump_register(EXTCAP_DBC_CONT_HIGH),
80 	dump_register(EXTCAP_DBC_DEVINFO1),
81 	dump_register(EXTCAP_DBC_DEVINFO2),
82 };
83 
84 static struct dentry *xhci_debugfs_root;
85 
86 static struct xhci_regset *xhci_debugfs_alloc_regset(struct xhci_hcd *xhci)
87 {
88 	struct xhci_regset	*regset;
89 
90 	regset = kzalloc(sizeof(*regset), GFP_KERNEL);
91 	if (!regset)
92 		return NULL;
93 
94 	/*
95 	 * The allocation and free of regset are executed in order.
96 	 * We needn't a lock here.
97 	 */
98 	INIT_LIST_HEAD(&regset->list);
99 	list_add_tail(&regset->list, &xhci->regset_list);
100 
101 	return regset;
102 }
103 
104 static void xhci_debugfs_free_regset(struct xhci_regset *regset)
105 {
106 	if (!regset)
107 		return;
108 
109 	list_del(&regset->list);
110 	kfree(regset);
111 }
112 
113 __printf(6, 7)
114 static void xhci_debugfs_regset(struct xhci_hcd *xhci, u32 base,
115 				const struct debugfs_reg32 *regs,
116 				size_t nregs, struct dentry *parent,
117 				const char *fmt, ...)
118 {
119 	struct xhci_regset	*rgs;
120 	va_list			args;
121 	struct debugfs_regset32	*regset;
122 	struct usb_hcd		*hcd = xhci_to_hcd(xhci);
123 
124 	rgs = xhci_debugfs_alloc_regset(xhci);
125 	if (!rgs)
126 		return;
127 
128 	va_start(args, fmt);
129 	vsnprintf(rgs->name, sizeof(rgs->name), fmt, args);
130 	va_end(args);
131 
132 	regset = &rgs->regset;
133 	regset->regs = regs;
134 	regset->nregs = nregs;
135 	regset->base = hcd->regs + base;
136 
137 	debugfs_create_regset32((const char *)rgs->name, 0444, parent, regset);
138 }
139 
140 static void xhci_debugfs_extcap_regset(struct xhci_hcd *xhci, int cap_id,
141 				       const struct debugfs_reg32 *regs,
142 				       size_t n, const char *cap_name)
143 {
144 	u32			offset;
145 	int			index = 0;
146 	size_t			psic, nregs = n;
147 	void __iomem		*base = &xhci->cap_regs->hc_capbase;
148 
149 	offset = xhci_find_next_ext_cap(base, 0, cap_id);
150 	while (offset) {
151 		if (cap_id == XHCI_EXT_CAPS_PROTOCOL) {
152 			psic = XHCI_EXT_PORT_PSIC(readl(base + offset + 8));
153 			nregs = min(4 + psic, n);
154 		}
155 
156 		xhci_debugfs_regset(xhci, offset, regs, nregs,
157 				    xhci->debugfs_root, "%s:%02d",
158 				    cap_name, index);
159 		offset = xhci_find_next_ext_cap(base, offset, cap_id);
160 		index++;
161 	}
162 }
163 
164 static int xhci_ring_enqueue_show(struct seq_file *s, void *unused)
165 {
166 	dma_addr_t		dma;
167 	struct xhci_ring	*ring = *(struct xhci_ring **)s->private;
168 
169 	dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
170 	seq_printf(s, "%pad\n", &dma);
171 
172 	return 0;
173 }
174 
175 static int xhci_ring_dequeue_show(struct seq_file *s, void *unused)
176 {
177 	dma_addr_t		dma;
178 	struct xhci_ring	*ring = *(struct xhci_ring **)s->private;
179 
180 	dma = xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
181 	seq_printf(s, "%pad\n", &dma);
182 
183 	return 0;
184 }
185 
186 static int xhci_ring_cycle_show(struct seq_file *s, void *unused)
187 {
188 	struct xhci_ring	*ring = *(struct xhci_ring **)s->private;
189 
190 	seq_printf(s, "%d\n", ring->cycle_state);
191 
192 	return 0;
193 }
194 
195 static void xhci_ring_dump_segment(struct seq_file *s,
196 				   struct xhci_segment *seg)
197 {
198 	int			i;
199 	dma_addr_t		dma;
200 	union xhci_trb		*trb;
201 	char			str[XHCI_MSG_MAX];
202 
203 	for (i = 0; i < TRBS_PER_SEGMENT; i++) {
204 		trb = &seg->trbs[i];
205 		dma = seg->dma + i * sizeof(*trb);
206 		seq_printf(s, "%pad: %s\n", &dma,
207 			   xhci_decode_trb(str, XHCI_MSG_MAX, le32_to_cpu(trb->generic.field[0]),
208 					   le32_to_cpu(trb->generic.field[1]),
209 					   le32_to_cpu(trb->generic.field[2]),
210 					   le32_to_cpu(trb->generic.field[3])));
211 	}
212 }
213 
214 static int xhci_ring_trb_show(struct seq_file *s, void *unused)
215 {
216 	int			i;
217 	struct xhci_ring	*ring = *(struct xhci_ring **)s->private;
218 	struct xhci_segment	*seg = ring->first_seg;
219 
220 	for (i = 0; i < ring->num_segs; i++) {
221 		xhci_ring_dump_segment(s, seg);
222 		seg = seg->next;
223 	}
224 
225 	return 0;
226 }
227 
228 static struct xhci_file_map ring_files[] = {
229 	{"enqueue",		xhci_ring_enqueue_show, },
230 	{"dequeue",		xhci_ring_dequeue_show, },
231 	{"cycle",		xhci_ring_cycle_show, },
232 	{"trbs",		xhci_ring_trb_show, },
233 };
234 
235 static int xhci_ring_open(struct inode *inode, struct file *file)
236 {
237 	int			i;
238 	struct xhci_file_map	*f_map;
239 	const char		*file_name = file_dentry(file)->d_iname;
240 
241 	for (i = 0; i < ARRAY_SIZE(ring_files); i++) {
242 		f_map = &ring_files[i];
243 
244 		if (strcmp(f_map->name, file_name) == 0)
245 			break;
246 	}
247 
248 	return single_open(file, f_map->show, inode->i_private);
249 }
250 
251 static const struct file_operations xhci_ring_fops = {
252 	.open			= xhci_ring_open,
253 	.read			= seq_read,
254 	.llseek			= seq_lseek,
255 	.release		= single_release,
256 };
257 
258 static int xhci_slot_context_show(struct seq_file *s, void *unused)
259 {
260 	struct xhci_hcd		*xhci;
261 	struct xhci_slot_ctx	*slot_ctx;
262 	struct xhci_slot_priv	*priv = s->private;
263 	struct xhci_virt_device	*dev = priv->dev;
264 	char			str[XHCI_MSG_MAX];
265 
266 	xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
267 	slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
268 	seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
269 		   xhci_decode_slot_context(str,
270 					    le32_to_cpu(slot_ctx->dev_info),
271 					    le32_to_cpu(slot_ctx->dev_info2),
272 					    le32_to_cpu(slot_ctx->tt_info),
273 					    le32_to_cpu(slot_ctx->dev_state)));
274 
275 	return 0;
276 }
277 
278 static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
279 {
280 	int			ep_index;
281 	dma_addr_t		dma;
282 	struct xhci_hcd		*xhci;
283 	struct xhci_ep_ctx	*ep_ctx;
284 	struct xhci_slot_priv	*priv = s->private;
285 	struct xhci_virt_device	*dev = priv->dev;
286 	char			str[XHCI_MSG_MAX];
287 
288 	xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
289 
290 	for (ep_index = 0; ep_index < 31; ep_index++) {
291 		ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
292 		dma = dev->out_ctx->dma + (ep_index + 1) * CTX_SIZE(xhci->hcc_params);
293 		seq_printf(s, "%pad: %s\n", &dma,
294 			   xhci_decode_ep_context(str,
295 						  le32_to_cpu(ep_ctx->ep_info),
296 						  le32_to_cpu(ep_ctx->ep_info2),
297 						  le64_to_cpu(ep_ctx->deq),
298 						  le32_to_cpu(ep_ctx->tx_info)));
299 	}
300 
301 	return 0;
302 }
303 
304 static int xhci_device_name_show(struct seq_file *s, void *unused)
305 {
306 	struct xhci_slot_priv	*priv = s->private;
307 	struct xhci_virt_device	*dev = priv->dev;
308 
309 	seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
310 
311 	return 0;
312 }
313 
314 static struct xhci_file_map context_files[] = {
315 	{"name",		xhci_device_name_show, },
316 	{"slot-context",	xhci_slot_context_show, },
317 	{"ep-context",		xhci_endpoint_context_show, },
318 };
319 
320 static int xhci_context_open(struct inode *inode, struct file *file)
321 {
322 	int			i;
323 	struct xhci_file_map	*f_map;
324 	const char		*file_name = file_dentry(file)->d_iname;
325 
326 	for (i = 0; i < ARRAY_SIZE(context_files); i++) {
327 		f_map = &context_files[i];
328 
329 		if (strcmp(f_map->name, file_name) == 0)
330 			break;
331 	}
332 
333 	return single_open(file, f_map->show, inode->i_private);
334 }
335 
336 static const struct file_operations xhci_context_fops = {
337 	.open			= xhci_context_open,
338 	.read			= seq_read,
339 	.llseek			= seq_lseek,
340 	.release		= single_release,
341 };
342 
343 
344 
345 static int xhci_portsc_show(struct seq_file *s, void *unused)
346 {
347 	struct xhci_port	*port = s->private;
348 	u32			portsc;
349 	char			str[XHCI_MSG_MAX];
350 
351 	portsc = readl(port->addr);
352 	seq_printf(s, "%s\n", xhci_decode_portsc(str, portsc));
353 
354 	return 0;
355 }
356 
357 static int xhci_port_open(struct inode *inode, struct file *file)
358 {
359 	return single_open(file, xhci_portsc_show, inode->i_private);
360 }
361 
362 static ssize_t xhci_port_write(struct file *file,  const char __user *ubuf,
363 			       size_t count, loff_t *ppos)
364 {
365 	struct seq_file         *s = file->private_data;
366 	struct xhci_port	*port = s->private;
367 	struct xhci_hcd		*xhci = hcd_to_xhci(port->rhub->hcd);
368 	char                    buf[32];
369 	u32			portsc;
370 	unsigned long		flags;
371 
372 	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
373 		return -EFAULT;
374 
375 	if (!strncmp(buf, "compliance", 10)) {
376 		/* If CTC is clear, compliance is enabled by default */
377 		if (!HCC2_CTC(xhci->hcc_params2))
378 			return count;
379 		spin_lock_irqsave(&xhci->lock, flags);
380 		/* compliance mode can only be enabled on ports in RxDetect */
381 		portsc = readl(port->addr);
382 		if ((portsc & PORT_PLS_MASK) != XDEV_RXDETECT) {
383 			spin_unlock_irqrestore(&xhci->lock, flags);
384 			return -EPERM;
385 		}
386 		portsc = xhci_port_state_to_neutral(portsc);
387 		portsc &= ~PORT_PLS_MASK;
388 		portsc |= PORT_LINK_STROBE | XDEV_COMP_MODE;
389 		writel(portsc, port->addr);
390 		spin_unlock_irqrestore(&xhci->lock, flags);
391 	} else {
392 		return -EINVAL;
393 	}
394 	return count;
395 }
396 
397 static const struct file_operations port_fops = {
398 	.open			= xhci_port_open,
399 	.write                  = xhci_port_write,
400 	.read			= seq_read,
401 	.llseek			= seq_lseek,
402 	.release		= single_release,
403 };
404 
405 static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
406 				      struct xhci_file_map *files,
407 				      size_t nentries, void *data,
408 				      struct dentry *parent,
409 				      const struct file_operations *fops)
410 {
411 	int			i;
412 
413 	for (i = 0; i < nentries; i++)
414 		debugfs_create_file(files[i].name, 0444, parent, data, fops);
415 }
416 
417 static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
418 						   struct xhci_ring **ring,
419 						   const char *name,
420 						   struct dentry *parent)
421 {
422 	struct dentry		*dir;
423 
424 	dir = debugfs_create_dir(name, parent);
425 	xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
426 				  ring, dir, &xhci_ring_fops);
427 
428 	return dir;
429 }
430 
431 static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
432 					      struct dentry *parent,
433 					      int slot_id)
434 {
435 	struct xhci_virt_device	*dev = xhci->devs[slot_id];
436 
437 	xhci_debugfs_create_files(xhci, context_files,
438 				  ARRAY_SIZE(context_files),
439 				  dev->debugfs_private,
440 				  parent, &xhci_context_fops);
441 }
442 
443 void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
444 				  struct xhci_virt_device *dev,
445 				  int ep_index)
446 {
447 	struct xhci_ep_priv	*epriv;
448 	struct xhci_slot_priv	*spriv = dev->debugfs_private;
449 
450 	if (!spriv)
451 		return;
452 
453 	if (spriv->eps[ep_index])
454 		return;
455 
456 	epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
457 	if (!epriv)
458 		return;
459 
460 	epriv->show_ring = dev->eps[ep_index].ring;
461 
462 	snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
463 	epriv->root = xhci_debugfs_create_ring_dir(xhci,
464 						   &epriv->show_ring,
465 						   epriv->name,
466 						   spriv->root);
467 	spriv->eps[ep_index] = epriv;
468 }
469 
470 void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
471 				  struct xhci_virt_device *dev,
472 				  int ep_index)
473 {
474 	struct xhci_ep_priv	*epriv;
475 	struct xhci_slot_priv	*spriv = dev->debugfs_private;
476 
477 	if (!spriv || !spriv->eps[ep_index])
478 		return;
479 
480 	epriv = spriv->eps[ep_index];
481 	debugfs_remove_recursive(epriv->root);
482 	spriv->eps[ep_index] = NULL;
483 	kfree(epriv);
484 }
485 
486 static int xhci_stream_id_show(struct seq_file *s, void *unused)
487 {
488 	struct xhci_ep_priv	*epriv = s->private;
489 
490 	if (!epriv->stream_info)
491 		return -EPERM;
492 
493 	seq_printf(s, "Show stream ID %d trb ring, supported [1 - %d]\n",
494 		   epriv->stream_id, epriv->stream_info->num_streams - 1);
495 
496 	return 0;
497 }
498 
499 static int xhci_stream_id_open(struct inode *inode, struct file *file)
500 {
501 	return single_open(file, xhci_stream_id_show, inode->i_private);
502 }
503 
504 static ssize_t xhci_stream_id_write(struct file *file,  const char __user *ubuf,
505 			       size_t count, loff_t *ppos)
506 {
507 	struct seq_file         *s = file->private_data;
508 	struct xhci_ep_priv	*epriv = s->private;
509 	int			ret;
510 	u16			stream_id; /* MaxPStreams + 1 <= 16 */
511 
512 	if (!epriv->stream_info)
513 		return -EPERM;
514 
515 	/* Decimal number */
516 	ret = kstrtou16_from_user(ubuf, count, 10, &stream_id);
517 	if (ret)
518 		return ret;
519 
520 	if (stream_id == 0 || stream_id >= epriv->stream_info->num_streams)
521 		return -EINVAL;
522 
523 	epriv->stream_id = stream_id;
524 	epriv->show_ring = epriv->stream_info->stream_rings[stream_id];
525 
526 	return count;
527 }
528 
529 static const struct file_operations stream_id_fops = {
530 	.open			= xhci_stream_id_open,
531 	.write                  = xhci_stream_id_write,
532 	.read			= seq_read,
533 	.llseek			= seq_lseek,
534 	.release		= single_release,
535 };
536 
537 static int xhci_stream_context_array_show(struct seq_file *s, void *unused)
538 {
539 	struct xhci_ep_priv	*epriv = s->private;
540 	struct xhci_stream_ctx	*stream_ctx;
541 	dma_addr_t		dma;
542 	int			id;
543 
544 	if (!epriv->stream_info)
545 		return -EPERM;
546 
547 	seq_printf(s, "Allocated %d streams and %d stream context array entries\n",
548 			epriv->stream_info->num_streams,
549 			epriv->stream_info->num_stream_ctxs);
550 
551 	for (id = 0; id < epriv->stream_info->num_stream_ctxs; id++) {
552 		stream_ctx = epriv->stream_info->stream_ctx_array + id;
553 		dma = epriv->stream_info->ctx_array_dma + id * 16;
554 		if (id < epriv->stream_info->num_streams)
555 			seq_printf(s, "%pad stream id %d deq %016llx\n", &dma,
556 				   id, le64_to_cpu(stream_ctx->stream_ring));
557 		else
558 			seq_printf(s, "%pad stream context entry not used deq %016llx\n",
559 				   &dma, le64_to_cpu(stream_ctx->stream_ring));
560 	}
561 
562 	return 0;
563 }
564 DEFINE_SHOW_ATTRIBUTE(xhci_stream_context_array);
565 
566 void xhci_debugfs_create_stream_files(struct xhci_hcd *xhci,
567 				      struct xhci_virt_device *dev,
568 				      int ep_index)
569 {
570 	struct xhci_slot_priv	*spriv = dev->debugfs_private;
571 	struct xhci_ep_priv	*epriv;
572 
573 	if (!spriv || !spriv->eps[ep_index] ||
574 	    !dev->eps[ep_index].stream_info)
575 		return;
576 
577 	epriv = spriv->eps[ep_index];
578 	epriv->stream_info = dev->eps[ep_index].stream_info;
579 
580 	/* Show trb ring of stream ID 1 by default */
581 	epriv->stream_id = 1;
582 	epriv->show_ring = epriv->stream_info->stream_rings[1];
583 	debugfs_create_file("stream_id", 0644,
584 			    epriv->root, epriv,
585 			    &stream_id_fops);
586 	debugfs_create_file("stream_context_array", 0444,
587 			    epriv->root, epriv,
588 			    &xhci_stream_context_array_fops);
589 }
590 
591 void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
592 {
593 	struct xhci_slot_priv	*priv;
594 	struct xhci_virt_device	*dev = xhci->devs[slot_id];
595 
596 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
597 	if (!priv)
598 		return;
599 
600 	snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
601 	priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
602 	priv->dev = dev;
603 	dev->debugfs_private = priv;
604 
605 	xhci_debugfs_create_ring_dir(xhci, &dev->eps[0].ring,
606 				     "ep00", priv->root);
607 
608 	xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
609 }
610 
611 void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
612 {
613 	int			i;
614 	struct xhci_slot_priv	*priv;
615 	struct xhci_virt_device	*dev = xhci->devs[slot_id];
616 
617 	if (!dev || !dev->debugfs_private)
618 		return;
619 
620 	priv = dev->debugfs_private;
621 
622 	debugfs_remove_recursive(priv->root);
623 
624 	for (i = 0; i < 31; i++)
625 		kfree(priv->eps[i]);
626 
627 	kfree(priv);
628 	dev->debugfs_private = NULL;
629 }
630 
631 static void xhci_debugfs_create_ports(struct xhci_hcd *xhci,
632 				      struct dentry *parent)
633 {
634 	unsigned int		num_ports;
635 	char			port_name[8];
636 	struct xhci_port	*port;
637 	struct dentry		*dir;
638 
639 	num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
640 
641 	parent = debugfs_create_dir("ports", parent);
642 
643 	while (num_ports--) {
644 		scnprintf(port_name, sizeof(port_name), "port%02d",
645 			  num_ports + 1);
646 		dir = debugfs_create_dir(port_name, parent);
647 		port = &xhci->hw_ports[num_ports];
648 		debugfs_create_file("portsc", 0644, dir, port, &port_fops);
649 	}
650 }
651 
652 void xhci_debugfs_init(struct xhci_hcd *xhci)
653 {
654 	struct device		*dev = xhci_to_hcd(xhci)->self.controller;
655 
656 	xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
657 						xhci_debugfs_root);
658 
659 	INIT_LIST_HEAD(&xhci->regset_list);
660 
661 	xhci_debugfs_regset(xhci,
662 			    0,
663 			    xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
664 			    xhci->debugfs_root, "reg-cap");
665 
666 	xhci_debugfs_regset(xhci,
667 			    HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
668 			    xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
669 			    xhci->debugfs_root, "reg-op");
670 
671 	xhci_debugfs_regset(xhci,
672 			    readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
673 			    xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
674 			    xhci->debugfs_root, "reg-runtime");
675 
676 	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
677 				   xhci_extcap_legsup,
678 				   ARRAY_SIZE(xhci_extcap_legsup),
679 				   "reg-ext-legsup");
680 
681 	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
682 				   xhci_extcap_protocol,
683 				   ARRAY_SIZE(xhci_extcap_protocol),
684 				   "reg-ext-protocol");
685 
686 	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
687 				   xhci_extcap_dbc,
688 				   ARRAY_SIZE(xhci_extcap_dbc),
689 				   "reg-ext-dbc");
690 
691 	xhci_debugfs_create_ring_dir(xhci, &xhci->cmd_ring,
692 				     "command-ring",
693 				     xhci->debugfs_root);
694 
695 	xhci_debugfs_create_ring_dir(xhci, &xhci->event_ring,
696 				     "event-ring",
697 				     xhci->debugfs_root);
698 
699 	xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
700 
701 	xhci_debugfs_create_ports(xhci, xhci->debugfs_root);
702 }
703 
704 void xhci_debugfs_exit(struct xhci_hcd *xhci)
705 {
706 	struct xhci_regset	*rgs, *tmp;
707 
708 	debugfs_remove_recursive(xhci->debugfs_root);
709 	xhci->debugfs_root = NULL;
710 	xhci->debugfs_slots = NULL;
711 
712 	list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
713 		xhci_debugfs_free_regset(rgs);
714 }
715 
716 void __init xhci_debugfs_create_root(void)
717 {
718 	xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
719 }
720 
721 void __exit xhci_debugfs_remove_root(void)
722 {
723 	debugfs_remove_recursive(xhci_debugfs_root);
724 	xhci_debugfs_root = NULL;
725 }
726