1 // SPDX-License-Identifier: GPL-2.0
3 * UHCI-specific debugging code. Invaluable when something
4 * goes wrong, but don't get in my face.
6 * Kernel visible pointers are surrounded in []s and bus
7 * visible pointers are surrounded in ()s
9 * (C) Copyright 1999 Linus Torvalds
10 * (C) Copyright 1999-2001 Johannes Erdfelt
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/debugfs.h>
20 #define EXTRA_SPACE 1024
22 static struct dentry
*uhci_debugfs_root
;
24 #ifdef CONFIG_DYNAMIC_DEBUG
26 /* Handle REALLY large printks so we don't overflow buffers */
27 static void lprintk(char *buf
)
31 /* Just write one line at a time */
33 p
= strchr(buf
, '\n');
36 printk(KERN_DEBUG
"%s\n", buf
);
43 static int uhci_show_td(struct uhci_hcd
*uhci
, struct uhci_td
*td
, char *buf
,
50 status
= td_status(uhci
, td
);
51 out
+= sprintf(out
, "%*s[%p] link (%08x) ", space
, "", td
,
52 hc32_to_cpu(uhci
, td
->link
));
53 out
+= sprintf(out
, "e%d %s%s%s%s%s%s%s%s%s%sLength=%x ",
55 (status
& TD_CTRL_SPD
) ? "SPD " : "",
56 (status
& TD_CTRL_LS
) ? "LS " : "",
57 (status
& TD_CTRL_IOC
) ? "IOC " : "",
58 (status
& TD_CTRL_ACTIVE
) ? "Active " : "",
59 (status
& TD_CTRL_STALLED
) ? "Stalled " : "",
60 (status
& TD_CTRL_DBUFERR
) ? "DataBufErr " : "",
61 (status
& TD_CTRL_BABBLE
) ? "Babble " : "",
62 (status
& TD_CTRL_NAK
) ? "NAK " : "",
63 (status
& TD_CTRL_CRCTIMEO
) ? "CRC/Timeo " : "",
64 (status
& TD_CTRL_BITSTUFF
) ? "BitStuff " : "",
69 token
= td_token(uhci
, td
);
70 switch (uhci_packetid(token
)) {
85 out
+= sprintf(out
, "MaxLen=%x DT%d EndPt=%x Dev=%x, PID=%x(%s) ",
92 out
+= sprintf(out
, "(buf=%08x)\n", hc32_to_cpu(uhci
, td
->buffer
));
96 out
+= sprintf(out
, " ...\n");
100 static int uhci_show_urbp(struct uhci_hcd
*uhci
, struct urb_priv
*urbp
,
101 char *buf
, int len
, int space
)
105 int i
, nactive
, ninactive
;
109 out
+= sprintf(out
, "urb_priv [%p] ", urbp
);
110 out
+= sprintf(out
, "urb [%p] ", urbp
->urb
);
111 out
+= sprintf(out
, "qh [%p] ", urbp
->qh
);
112 out
+= sprintf(out
, "Dev=%d ", usb_pipedevice(urbp
->urb
->pipe
));
113 out
+= sprintf(out
, "EP=%x(%s) ", usb_pipeendpoint(urbp
->urb
->pipe
),
114 (usb_pipein(urbp
->urb
->pipe
) ? "IN" : "OUT"));
118 switch (usb_pipetype(urbp
->urb
->pipe
)) {
119 case PIPE_ISOCHRONOUS
: ptype
= "ISO"; break;
120 case PIPE_INTERRUPT
: ptype
= "INT"; break;
121 case PIPE_BULK
: ptype
= "BLK"; break;
123 case PIPE_CONTROL
: ptype
= "CTL"; break;
126 out
+= sprintf(out
, "%s%s", ptype
, (urbp
->fsbr
? " FSBR" : ""));
127 out
+= sprintf(out
, " Actlen=%d%s", urbp
->urb
->actual_length
,
128 (urbp
->qh
->type
== USB_ENDPOINT_XFER_CONTROL
?
131 if (urbp
->urb
->unlinked
)
132 out
+= sprintf(out
, " Unlinked=%d", urbp
->urb
->unlinked
);
133 out
+= sprintf(out
, "\n");
138 i
= nactive
= ninactive
= 0;
139 list_for_each_entry(td
, &urbp
->td_list
, list
) {
140 if (urbp
->qh
->type
!= USB_ENDPOINT_XFER_ISOC
&&
141 (++i
<= 10 || debug
> 2)) {
142 out
+= sprintf(out
, "%*s%d: ", space
+ 2, "", i
);
143 out
+= uhci_show_td(uhci
, td
, out
,
144 len
- (out
- buf
), 0);
148 if (td_status(uhci
, td
) & TD_CTRL_ACTIVE
)
154 if (nactive
+ ninactive
> 0)
156 "%*s[skipped %d inactive and %d active TDs]\n",
157 space
, "", ninactive
, nactive
);
160 out
+= sprintf(out
, " ...\n");
165 static int uhci_show_qh(struct uhci_hcd
*uhci
,
166 struct uhci_qh
*qh
, char *buf
, int len
, int space
)
170 __hc32 element
= qh_element(qh
);
174 case USB_ENDPOINT_XFER_ISOC
: qtype
= "ISO"; break;
175 case USB_ENDPOINT_XFER_INT
: qtype
= "INT"; break;
176 case USB_ENDPOINT_XFER_BULK
: qtype
= "BLK"; break;
177 case USB_ENDPOINT_XFER_CONTROL
: qtype
= "CTL"; break;
178 default: qtype
= "Skel" ; break;
181 out
+= sprintf(out
, "%*s[%p] %s QH link (%08x) element (%08x)\n",
182 space
, "", qh
, qtype
,
183 hc32_to_cpu(uhci
, qh
->link
),
184 hc32_to_cpu(uhci
, element
));
185 if (qh
->type
== USB_ENDPOINT_XFER_ISOC
)
187 "%*s period %d phase %d load %d us, frame %x desc [%p]\n",
188 space
, "", qh
->period
, qh
->phase
, qh
->load
,
189 qh
->iso_frame
, qh
->iso_packet_desc
);
190 else if (qh
->type
== USB_ENDPOINT_XFER_INT
)
191 out
+= sprintf(out
, "%*s period %d phase %d load %d us\n",
192 space
, "", qh
->period
, qh
->phase
, qh
->load
);
196 if (element
& UHCI_PTR_QH(uhci
))
197 out
+= sprintf(out
, "%*s Element points to QH (bug?)\n", space
, "");
199 if (element
& UHCI_PTR_DEPTH(uhci
))
200 out
+= sprintf(out
, "%*s Depth traverse\n", space
, "");
202 if (element
& cpu_to_hc32(uhci
, 8))
203 out
+= sprintf(out
, "%*s Bit 3 set (bug?)\n", space
, "");
205 if (!(element
& ~(UHCI_PTR_QH(uhci
) | UHCI_PTR_DEPTH(uhci
))))
206 out
+= sprintf(out
, "%*s Element is NULL (bug?)\n", space
, "");
211 if (list_empty(&qh
->queue
)) {
212 out
+= sprintf(out
, "%*s queue is empty\n", space
, "");
213 if (qh
== uhci
->skel_async_qh
) {
214 out
+= uhci_show_td(uhci
, uhci
->term_td
, out
,
215 len
- (out
- buf
), 0);
220 struct urb_priv
*urbp
= list_entry(qh
->queue
.next
,
221 struct urb_priv
, node
);
222 struct uhci_td
*td
= list_entry(urbp
->td_list
.next
,
223 struct uhci_td
, list
);
225 if (element
!= LINK_TO_TD(uhci
, td
))
226 out
+= sprintf(out
, "%*s Element != First TD\n",
229 list_for_each_entry(urbp
, &qh
->queue
, node
) {
231 out
+= uhci_show_urbp(uhci
, urbp
, out
,
232 len
- (out
- buf
), space
+ 2);
240 out
+= sprintf(out
, "%*s Skipped %d URBs\n",
248 out
+= sprintf(out
, "%*s Dummy TD\n", space
, "");
249 out
+= uhci_show_td(uhci
, qh
->dummy_td
, out
,
250 len
- (out
- buf
), 0);
257 out
+= sprintf(out
, " ...\n");
262 static int uhci_show_sc(int port
, unsigned short status
, char *buf
)
264 return sprintf(buf
, " stat%d = %04x %s%s%s%s%s%s%s%s%s%s\n",
267 (status
& USBPORTSC_SUSP
) ? " Suspend" : "",
268 (status
& USBPORTSC_OCC
) ? " OverCurrentChange" : "",
269 (status
& USBPORTSC_OC
) ? " OverCurrent" : "",
270 (status
& USBPORTSC_PR
) ? " Reset" : "",
271 (status
& USBPORTSC_LSDA
) ? " LowSpeed" : "",
272 (status
& USBPORTSC_RD
) ? " ResumeDetect" : "",
273 (status
& USBPORTSC_PEC
) ? " EnableChange" : "",
274 (status
& USBPORTSC_PE
) ? " Enabled" : "",
275 (status
& USBPORTSC_CSC
) ? " ConnectChange" : "",
276 (status
& USBPORTSC_CCS
) ? " Connected" : "");
279 static int uhci_show_root_hub_state(struct uhci_hcd
*uhci
, char *buf
)
283 switch (uhci
->rh_state
) {
285 rh_state
= "reset"; break;
286 case UHCI_RH_SUSPENDED
:
287 rh_state
= "suspended"; break;
288 case UHCI_RH_AUTO_STOPPED
:
289 rh_state
= "auto-stopped"; break;
290 case UHCI_RH_RESUMING
:
291 rh_state
= "resuming"; break;
292 case UHCI_RH_SUSPENDING
:
293 rh_state
= "suspending"; break;
294 case UHCI_RH_RUNNING
:
295 rh_state
= "running"; break;
296 case UHCI_RH_RUNNING_NODEVS
:
297 rh_state
= "running, no devs"; break;
299 rh_state
= "?"; break;
301 return sprintf(buf
, "Root-hub state: %s FSBR: %d\n",
302 rh_state
, uhci
->fsbr_is_on
);
305 static int uhci_show_status(struct uhci_hcd
*uhci
, char *buf
, int len
)
308 unsigned short usbcmd
, usbstat
, usbint
, usbfrnum
;
309 unsigned int flbaseadd
;
311 unsigned short portsc1
, portsc2
;
314 usbcmd
= uhci_readw(uhci
, USBCMD
);
315 usbstat
= uhci_readw(uhci
, USBSTS
);
316 usbint
= uhci_readw(uhci
, USBINTR
);
317 usbfrnum
= uhci_readw(uhci
, USBFRNUM
);
318 flbaseadd
= uhci_readl(uhci
, USBFLBASEADD
);
319 sof
= uhci_readb(uhci
, USBSOF
);
320 portsc1
= uhci_readw(uhci
, USBPORTSC1
);
321 portsc2
= uhci_readw(uhci
, USBPORTSC2
);
323 out
+= sprintf(out
, " usbcmd = %04x %s%s%s%s%s%s%s%s\n",
325 (usbcmd
& USBCMD_MAXP
) ? "Maxp64 " : "Maxp32 ",
326 (usbcmd
& USBCMD_CF
) ? "CF " : "",
327 (usbcmd
& USBCMD_SWDBG
) ? "SWDBG " : "",
328 (usbcmd
& USBCMD_FGR
) ? "FGR " : "",
329 (usbcmd
& USBCMD_EGSM
) ? "EGSM " : "",
330 (usbcmd
& USBCMD_GRESET
) ? "GRESET " : "",
331 (usbcmd
& USBCMD_HCRESET
) ? "HCRESET " : "",
332 (usbcmd
& USBCMD_RS
) ? "RS " : "");
336 out
+= sprintf(out
, " usbstat = %04x %s%s%s%s%s%s\n",
338 (usbstat
& USBSTS_HCH
) ? "HCHalted " : "",
339 (usbstat
& USBSTS_HCPE
) ? "HostControllerProcessError " : "",
340 (usbstat
& USBSTS_HSE
) ? "HostSystemError " : "",
341 (usbstat
& USBSTS_RD
) ? "ResumeDetect " : "",
342 (usbstat
& USBSTS_ERROR
) ? "USBError " : "",
343 (usbstat
& USBSTS_USBINT
) ? "USBINT " : "");
347 out
+= sprintf(out
, " usbint = %04x\n", usbint
);
348 out
+= sprintf(out
, " usbfrnum = (%d)%03x\n", (usbfrnum
>> 10) & 1,
349 0xfff & (4*(unsigned int)usbfrnum
));
350 out
+= sprintf(out
, " flbaseadd = %08x\n", flbaseadd
);
351 out
+= sprintf(out
, " sof = %02x\n", sof
);
355 out
+= uhci_show_sc(1, portsc1
, out
);
359 out
+= uhci_show_sc(2, portsc2
, out
);
364 "Most recent frame: %x (%d) Last ISO frame: %x (%d)\n",
365 uhci
->frame_number
, uhci
->frame_number
& 1023,
366 uhci
->last_iso_frame
, uhci
->last_iso_frame
& 1023);
370 out
+= sprintf(out
, " ...\n");
374 static int uhci_sprint_schedule(struct uhci_hcd
*uhci
, char *buf
, int len
)
380 struct list_head
*tmp
, *head
;
385 static const char * const qh_names
[] = {
386 "unlink", "iso", "int128", "int64", "int32", "int16",
387 "int8", "int4", "int2", "async", "term"
390 out
+= uhci_show_root_hub_state(uhci
, out
);
393 out
+= sprintf(out
, "HC status\n");
394 out
+= uhci_show_status(uhci
, out
, len
- (out
- buf
));
398 out
+= sprintf(out
, "Periodic load table\n");
399 for (i
= 0; i
< MAX_PHASE
; ++i
) {
400 out
+= sprintf(out
, "\t%d", uhci
->load
[i
]);
404 out
+= sprintf(out
, "Total: %d, #INT: %d, #ISO: %d\n",
406 uhci_to_hcd(uhci
)->self
.bandwidth_int_reqs
,
407 uhci_to_hcd(uhci
)->self
.bandwidth_isoc_reqs
);
411 out
+= sprintf(out
, "Frame List\n");
414 for (i
= 0; i
< UHCI_NUMFRAMES
; ++i
) {
420 td
= uhci
->frame_cpu
[i
];
421 link
= uhci
->frame
[i
];
426 out
+= sprintf(out
, "- Frame %d -> (%08x)\n",
427 i
, hc32_to_cpu(uhci
, link
));
434 td
= list_entry(tmp
, struct uhci_td
, fl_list
);
436 if (link
!= LINK_TO_TD(uhci
, td
)) {
439 " link does not match list entry!\n");
446 out
+= uhci_show_td(uhci
, td
, out
,
447 len
- (out
- buf
), 4);
452 } while (tmp
!= head
);
455 qh_dma
= uhci_frame_skel_link(uhci
, i
);
456 if (link
!= qh_dma
) {
460 "- Frame %d -> (%08x)\n",
461 i
, hc32_to_cpu(uhci
, link
));
465 " link does not match QH (%08x)!\n",
466 hc32_to_cpu(uhci
, qh_dma
));
475 out
+= sprintf(out
, "Skipped %d bad links\n", nerrs
);
477 out
+= sprintf(out
, "Skeleton QHs\n");
483 for (i
= 0; i
< UHCI_NUM_SKELQH
; ++i
) {
486 qh
= uhci
->skelqh
[i
];
487 out
+= sprintf(out
, "- skel_%s_qh\n", qh_names
[i
]);
488 out
+= uhci_show_qh(uhci
, qh
, out
, len
- (out
- buf
), 4);
492 /* Last QH is the Terminating QH, it's different */
493 if (i
== SKEL_TERM
) {
494 if (qh_element(qh
) != LINK_TO_TD(uhci
, uhci
->term_td
)) {
496 " skel_term_qh element is not set to term_td!\n");
502 link
= LINK_TO_QH(uhci
, uhci
->skel_term_qh
);
509 while (tmp
!= head
) {
510 qh
= list_entry(tmp
, struct uhci_qh
, node
);
513 out
+= uhci_show_qh(uhci
, qh
, out
,
514 len
- (out
- buf
), 4);
518 if (!fsbr_link
&& qh
->skel
>= SKEL_FSBR
)
519 fsbr_link
= LINK_TO_QH(uhci
, qh
);
522 out
+= sprintf(out
, " Skipped %d QHs\n", cnt
);
524 link
= UHCI_PTR_TERM(uhci
);
527 else if (i
< SKEL_ASYNC
)
528 link
= LINK_TO_QH(uhci
, uhci
->skel_async_qh
);
529 else if (!uhci
->fsbr_is_on
)
532 link
= LINK_TO_QH(uhci
, uhci
->skel_term_qh
);
534 if (qh
->link
!= link
)
536 " last QH not linked to next skeleton!\n");
544 out
+= sprintf(out
, " ...\n");
549 #ifdef CONFIG_DEBUG_FS
551 #define MAX_OUTPUT (64 * 1024)
558 static int uhci_debug_open(struct inode
*inode
, struct file
*file
)
560 struct uhci_hcd
*uhci
= inode
->i_private
;
561 struct uhci_debug
*up
;
564 up
= kmalloc(sizeof(*up
), GFP_KERNEL
);
568 up
->data
= kmalloc(MAX_OUTPUT
, GFP_KERNEL
);
575 spin_lock_irqsave(&uhci
->lock
, flags
);
576 if (uhci
->is_initialized
)
577 up
->size
= uhci_sprint_schedule(uhci
, up
->data
,
578 MAX_OUTPUT
- EXTRA_SPACE
);
579 spin_unlock_irqrestore(&uhci
->lock
, flags
);
581 file
->private_data
= up
;
586 static loff_t
uhci_debug_lseek(struct file
*file
, loff_t off
, int whence
)
588 struct uhci_debug
*up
= file
->private_data
;
589 return no_seek_end_llseek_size(file
, off
, whence
, up
->size
);
592 static ssize_t
uhci_debug_read(struct file
*file
, char __user
*buf
,
593 size_t nbytes
, loff_t
*ppos
)
595 struct uhci_debug
*up
= file
->private_data
;
596 return simple_read_from_buffer(buf
, nbytes
, ppos
, up
->data
, up
->size
);
599 static int uhci_debug_release(struct inode
*inode
, struct file
*file
)
601 struct uhci_debug
*up
= file
->private_data
;
609 static const struct file_operations uhci_debug_operations
= {
610 .owner
= THIS_MODULE
,
611 .open
= uhci_debug_open
,
612 .llseek
= uhci_debug_lseek
,
613 .read
= uhci_debug_read
,
614 .release
= uhci_debug_release
,
616 #define UHCI_DEBUG_OPS
618 #endif /* CONFIG_DEBUG_FS */
620 #else /* CONFIG_DYNAMIC_DEBUG*/
622 static inline void lprintk(char *buf
)
625 static inline int uhci_show_qh(struct uhci_hcd
*uhci
,
626 struct uhci_qh
*qh
, char *buf
, int len
, int space
)
631 static inline int uhci_sprint_schedule(struct uhci_hcd
*uhci
,