]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/usb/host/ohci-dbg.c
Merge tag 'rmobile-for-linus' of git://github.com/pmundt/linux-sh
[karo-tx-linux.git] / drivers / usb / host / ohci-dbg.c
1 /*
2  * OHCI HCD (Host Controller Driver) for USB.
3  *
4  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5  * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
6  *
7  * This file is licenced under the GPL.
8  */
9
10 /*-------------------------------------------------------------------------*/
11
12 #ifdef DEBUG
13
14 #define edstring(ed_type) ({ char *temp; \
15         switch (ed_type) { \
16         case PIPE_CONTROL:      temp = "ctrl"; break; \
17         case PIPE_BULK:         temp = "bulk"; break; \
18         case PIPE_INTERRUPT:    temp = "intr"; break; \
19         default:                temp = "isoc"; break; \
20         }; temp;})
21 #define pipestring(pipe) edstring(usb_pipetype(pipe))
22
23 /* debug| print the main components of an URB
24  * small: 0) header + data packets 1) just header
25  */
26 static void __maybe_unused
27 urb_print(struct urb * urb, char * str, int small, int status)
28 {
29         unsigned int pipe= urb->pipe;
30
31         if (!urb->dev || !urb->dev->bus) {
32                 dbg("%s URB: no dev", str);
33                 return;
34         }
35
36 #ifndef OHCI_VERBOSE_DEBUG
37         if (status != 0)
38 #endif
39         dbg("%s %p dev=%d ep=%d%s-%s flags=%x len=%d/%d stat=%d",
40                     str,
41                     urb,
42                     usb_pipedevice (pipe),
43                     usb_pipeendpoint (pipe),
44                     usb_pipeout (pipe)? "out" : "in",
45                     pipestring (pipe),
46                     urb->transfer_flags,
47                     urb->actual_length,
48                     urb->transfer_buffer_length,
49                     status);
50
51 #ifdef  OHCI_VERBOSE_DEBUG
52         if (!small) {
53                 int i, len;
54
55                 if (usb_pipecontrol (pipe)) {
56                         printk (KERN_DEBUG "%s: setup(8):", __FILE__);
57                         for (i = 0; i < 8 ; i++)
58                                 printk (" %02x", ((__u8 *) urb->setup_packet) [i]);
59                         printk ("\n");
60                 }
61                 if (urb->transfer_buffer_length > 0 && urb->transfer_buffer) {
62                         printk (KERN_DEBUG "%s: data(%d/%d):", __FILE__,
63                                 urb->actual_length,
64                                 urb->transfer_buffer_length);
65                         len = usb_pipeout (pipe)?
66                                                 urb->transfer_buffer_length: urb->actual_length;
67                         for (i = 0; i < 16 && i < len; i++)
68                                 printk (" %02x", ((__u8 *) urb->transfer_buffer) [i]);
69                         printk ("%s stat:%d\n", i < len? "...": "", status);
70                 }
71         }
72 #endif
73 }
74
75 #define ohci_dbg_sw(ohci, next, size, format, arg...) \
76         do { \
77         if (next != NULL) { \
78                 unsigned s_len; \
79                 s_len = scnprintf (*next, *size, format, ## arg ); \
80                 *size -= s_len; *next += s_len; \
81         } else \
82                 ohci_dbg(ohci,format, ## arg ); \
83         } while (0);
84
85
86 static void ohci_dump_intr_mask (
87         struct ohci_hcd *ohci,
88         char *label,
89         u32 mask,
90         char **next,
91         unsigned *size)
92 {
93         ohci_dbg_sw (ohci, next, size, "%s 0x%08x%s%s%s%s%s%s%s%s%s\n",
94                 label,
95                 mask,
96                 (mask & OHCI_INTR_MIE) ? " MIE" : "",
97                 (mask & OHCI_INTR_OC) ? " OC" : "",
98                 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
99                 (mask & OHCI_INTR_FNO) ? " FNO" : "",
100                 (mask & OHCI_INTR_UE) ? " UE" : "",
101                 (mask & OHCI_INTR_RD) ? " RD" : "",
102                 (mask & OHCI_INTR_SF) ? " SF" : "",
103                 (mask & OHCI_INTR_WDH) ? " WDH" : "",
104                 (mask & OHCI_INTR_SO) ? " SO" : ""
105                 );
106 }
107
108 static void maybe_print_eds (
109         struct ohci_hcd *ohci,
110         char *label,
111         u32 value,
112         char **next,
113         unsigned *size)
114 {
115         if (value)
116                 ohci_dbg_sw (ohci, next, size, "%s %08x\n", label, value);
117 }
118
119 static char *hcfs2string (int state)
120 {
121         switch (state) {
122                 case OHCI_USB_RESET:    return "reset";
123                 case OHCI_USB_RESUME:   return "resume";
124                 case OHCI_USB_OPER:     return "operational";
125                 case OHCI_USB_SUSPEND:  return "suspend";
126         }
127         return "?";
128 }
129
130 static const char *rh_state_string(struct ohci_hcd *ohci)
131 {
132         switch (ohci->rh_state) {
133         case OHCI_RH_HALTED:
134                 return "halted";
135         case OHCI_RH_SUSPENDED:
136                 return "suspended";
137         case OHCI_RH_RUNNING:
138                 return "running";
139         }
140         return "?";
141 }
142
143 // dump control and status registers
144 static void
145 ohci_dump_status (struct ohci_hcd *controller, char **next, unsigned *size)
146 {
147         struct ohci_regs __iomem *regs = controller->regs;
148         u32                     temp;
149
150         temp = ohci_readl (controller, &regs->revision) & 0xff;
151         ohci_dbg_sw (controller, next, size,
152                 "OHCI %d.%d, %s legacy support registers, rh state %s\n",
153                 0x03 & (temp >> 4), (temp & 0x0f),
154                 (temp & 0x0100) ? "with" : "NO",
155                 rh_state_string(controller));
156
157         temp = ohci_readl (controller, &regs->control);
158         ohci_dbg_sw (controller, next, size,
159                 "control 0x%03x%s%s%s HCFS=%s%s%s%s%s CBSR=%d\n",
160                 temp,
161                 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
162                 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
163                 (temp & OHCI_CTRL_IR) ? " IR" : "",
164                 hcfs2string (temp & OHCI_CTRL_HCFS),
165                 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
166                 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
167                 (temp & OHCI_CTRL_IE) ? " IE" : "",
168                 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
169                 temp & OHCI_CTRL_CBSR
170                 );
171
172         temp = ohci_readl (controller, &regs->cmdstatus);
173         ohci_dbg_sw (controller, next, size,
174                 "cmdstatus 0x%05x SOC=%d%s%s%s%s\n", temp,
175                 (temp & OHCI_SOC) >> 16,
176                 (temp & OHCI_OCR) ? " OCR" : "",
177                 (temp & OHCI_BLF) ? " BLF" : "",
178                 (temp & OHCI_CLF) ? " CLF" : "",
179                 (temp & OHCI_HCR) ? " HCR" : ""
180                 );
181
182         ohci_dump_intr_mask (controller, "intrstatus",
183                         ohci_readl (controller, &regs->intrstatus),
184                         next, size);
185         ohci_dump_intr_mask (controller, "intrenable",
186                         ohci_readl (controller, &regs->intrenable),
187                         next, size);
188         // intrdisable always same as intrenable
189
190         maybe_print_eds (controller, "ed_periodcurrent",
191                         ohci_readl (controller, &regs->ed_periodcurrent),
192                         next, size);
193
194         maybe_print_eds (controller, "ed_controlhead",
195                         ohci_readl (controller, &regs->ed_controlhead),
196                         next, size);
197         maybe_print_eds (controller, "ed_controlcurrent",
198                         ohci_readl (controller, &regs->ed_controlcurrent),
199                         next, size);
200
201         maybe_print_eds (controller, "ed_bulkhead",
202                         ohci_readl (controller, &regs->ed_bulkhead),
203                         next, size);
204         maybe_print_eds (controller, "ed_bulkcurrent",
205                         ohci_readl (controller, &regs->ed_bulkcurrent),
206                         next, size);
207
208         maybe_print_eds (controller, "donehead",
209                         ohci_readl (controller, &regs->donehead), next, size);
210 }
211
212 #define dbg_port_sw(hc,num,value,next,size) \
213         ohci_dbg_sw (hc, next, size, \
214                 "roothub.portstatus [%d] " \
215                 "0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
216                 num, temp, \
217                 (temp & RH_PS_PRSC) ? " PRSC" : "", \
218                 (temp & RH_PS_OCIC) ? " OCIC" : "", \
219                 (temp & RH_PS_PSSC) ? " PSSC" : "", \
220                 (temp & RH_PS_PESC) ? " PESC" : "", \
221                 (temp & RH_PS_CSC) ? " CSC" : "", \
222                 \
223                 (temp & RH_PS_LSDA) ? " LSDA" : "", \
224                 (temp & RH_PS_PPS) ? " PPS" : "", \
225                 (temp & RH_PS_PRS) ? " PRS" : "", \
226                 (temp & RH_PS_POCI) ? " POCI" : "", \
227                 (temp & RH_PS_PSS) ? " PSS" : "", \
228                 \
229                 (temp & RH_PS_PES) ? " PES" : "", \
230                 (temp & RH_PS_CCS) ? " CCS" : "" \
231                 );
232
233
234 static void
235 ohci_dump_roothub (
236         struct ohci_hcd *controller,
237         int verbose,
238         char **next,
239         unsigned *size)
240 {
241         u32                     temp, i;
242
243         temp = roothub_a (controller);
244         if (temp == ~(u32)0)
245                 return;
246
247         if (verbose) {
248                 ohci_dbg_sw (controller, next, size,
249                         "roothub.a %08x POTPGT=%d%s%s%s%s%s NDP=%d(%d)\n", temp,
250                         ((temp & RH_A_POTPGT) >> 24) & 0xff,
251                         (temp & RH_A_NOCP) ? " NOCP" : "",
252                         (temp & RH_A_OCPM) ? " OCPM" : "",
253                         (temp & RH_A_DT) ? " DT" : "",
254                         (temp & RH_A_NPS) ? " NPS" : "",
255                         (temp & RH_A_PSM) ? " PSM" : "",
256                         (temp & RH_A_NDP), controller->num_ports
257                         );
258                 temp = roothub_b (controller);
259                 ohci_dbg_sw (controller, next, size,
260                         "roothub.b %08x PPCM=%04x DR=%04x\n",
261                         temp,
262                         (temp & RH_B_PPCM) >> 16,
263                         (temp & RH_B_DR)
264                         );
265                 temp = roothub_status (controller);
266                 ohci_dbg_sw (controller, next, size,
267                         "roothub.status %08x%s%s%s%s%s%s\n",
268                         temp,
269                         (temp & RH_HS_CRWE) ? " CRWE" : "",
270                         (temp & RH_HS_OCIC) ? " OCIC" : "",
271                         (temp & RH_HS_LPSC) ? " LPSC" : "",
272                         (temp & RH_HS_DRWE) ? " DRWE" : "",
273                         (temp & RH_HS_OCI) ? " OCI" : "",
274                         (temp & RH_HS_LPS) ? " LPS" : ""
275                         );
276         }
277
278         for (i = 0; i < controller->num_ports; i++) {
279                 temp = roothub_portstatus (controller, i);
280                 dbg_port_sw (controller, i, temp, next, size);
281         }
282 }
283
284 static void ohci_dump (struct ohci_hcd *controller, int verbose)
285 {
286         ohci_dbg (controller, "OHCI controller state\n");
287
288         // dumps some of the state we know about
289         ohci_dump_status (controller, NULL, NULL);
290         if (controller->hcca)
291                 ohci_dbg (controller,
292                         "hcca frame #%04x\n", ohci_frame_no(controller));
293         ohci_dump_roothub (controller, 1, NULL, NULL);
294 }
295
296 static const char data0 [] = "DATA0";
297 static const char data1 [] = "DATA1";
298
299 static void ohci_dump_td (const struct ohci_hcd *ohci, const char *label,
300                 const struct td *td)
301 {
302         u32     tmp = hc32_to_cpup (ohci, &td->hwINFO);
303
304         ohci_dbg (ohci, "%s td %p%s; urb %p index %d; hw next td %08x\n",
305                 label, td,
306                 (tmp & TD_DONE) ? " (DONE)" : "",
307                 td->urb, td->index,
308                 hc32_to_cpup (ohci, &td->hwNextTD));
309         if ((tmp & TD_ISO) == 0) {
310                 const char      *toggle, *pid;
311                 u32     cbp, be;
312
313                 switch (tmp & TD_T) {
314                 case TD_T_DATA0: toggle = data0; break;
315                 case TD_T_DATA1: toggle = data1; break;
316                 case TD_T_TOGGLE: toggle = "(CARRY)"; break;
317                 default: toggle = "(?)"; break;
318                 }
319                 switch (tmp & TD_DP) {
320                 case TD_DP_SETUP: pid = "SETUP"; break;
321                 case TD_DP_IN: pid = "IN"; break;
322                 case TD_DP_OUT: pid = "OUT"; break;
323                 default: pid = "(bad pid)"; break;
324                 }
325                 ohci_dbg (ohci, "     info %08x CC=%x %s DI=%d %s %s\n", tmp,
326                         TD_CC_GET(tmp), /* EC, */ toggle,
327                         (tmp & TD_DI) >> 21, pid,
328                         (tmp & TD_R) ? "R" : "");
329                 cbp = hc32_to_cpup (ohci, &td->hwCBP);
330                 be = hc32_to_cpup (ohci, &td->hwBE);
331                 ohci_dbg (ohci, "     cbp %08x be %08x (len %d)\n", cbp, be,
332                         cbp ? (be + 1 - cbp) : 0);
333         } else {
334                 unsigned        i;
335                 ohci_dbg (ohci, "  info %08x CC=%x FC=%d DI=%d SF=%04x\n", tmp,
336                         TD_CC_GET(tmp),
337                         (tmp >> 24) & 0x07,
338                         (tmp & TD_DI) >> 21,
339                         tmp & 0x0000ffff);
340                 ohci_dbg (ohci, "  bp0 %08x be %08x\n",
341                         hc32_to_cpup (ohci, &td->hwCBP) & ~0x0fff,
342                         hc32_to_cpup (ohci, &td->hwBE));
343                 for (i = 0; i < MAXPSW; i++) {
344                         u16     psw = ohci_hwPSW (ohci, td, i);
345                         int     cc = (psw >> 12) & 0x0f;
346                         ohci_dbg (ohci, "    psw [%d] = %2x, CC=%x %s=%d\n", i,
347                                 psw, cc,
348                                 (cc >= 0x0e) ? "OFFSET" : "SIZE",
349                                 psw & 0x0fff);
350                 }
351         }
352 }
353
354 /* caller MUST own hcd spinlock if verbose is set! */
355 static void __maybe_unused
356 ohci_dump_ed (const struct ohci_hcd *ohci, const char *label,
357                 const struct ed *ed, int verbose)
358 {
359         u32     tmp = hc32_to_cpu (ohci, ed->hwINFO);
360         char    *type = "";
361
362         ohci_dbg (ohci, "%s, ed %p state 0x%x type %s; next ed %08x\n",
363                 label,
364                 ed, ed->state, edstring (ed->type),
365                 hc32_to_cpup (ohci, &ed->hwNextED));
366         switch (tmp & (ED_IN|ED_OUT)) {
367         case ED_OUT: type = "-OUT"; break;
368         case ED_IN: type = "-IN"; break;
369         /* else from TDs ... control */
370         }
371         ohci_dbg (ohci,
372                 "  info %08x MAX=%d%s%s%s%s EP=%d%s DEV=%d\n", tmp,
373                 0x03ff & (tmp >> 16),
374                 (tmp & ED_DEQUEUE) ? " DQ" : "",
375                 (tmp & ED_ISO) ? " ISO" : "",
376                 (tmp & ED_SKIP) ? " SKIP" : "",
377                 (tmp & ED_LOWSPEED) ? " LOW" : "",
378                 0x000f & (tmp >> 7),
379                 type,
380                 0x007f & tmp);
381         tmp = hc32_to_cpup (ohci, &ed->hwHeadP);
382         ohci_dbg (ohci, "  tds: head %08x %s%s tail %08x%s\n",
383                 tmp,
384                 (tmp & ED_C) ? data1 : data0,
385                 (tmp & ED_H) ? " HALT" : "",
386                 hc32_to_cpup (ohci, &ed->hwTailP),
387                 verbose ? "" : " (not listing)");
388         if (verbose) {
389                 struct list_head        *tmp;
390
391                 /* use ed->td_list because HC concurrently modifies
392                  * hwNextTD as it accumulates ed_donelist.
393                  */
394                 list_for_each (tmp, &ed->td_list) {
395                         struct td               *td;
396                         td = list_entry (tmp, struct td, td_list);
397                         ohci_dump_td (ohci, "  ->", td);
398                 }
399         }
400 }
401
402 #else
403 static inline void ohci_dump (struct ohci_hcd *controller, int verbose) {}
404
405 #undef OHCI_VERBOSE_DEBUG
406
407 #endif /* DEBUG */
408
409 /*-------------------------------------------------------------------------*/
410
411 #ifdef STUB_DEBUG_FILES
412
413 static inline void create_debug_files (struct ohci_hcd *bus) { }
414 static inline void remove_debug_files (struct ohci_hcd *bus) { }
415
416 #else
417
418 static int debug_async_open(struct inode *, struct file *);
419 static int debug_periodic_open(struct inode *, struct file *);
420 static int debug_registers_open(struct inode *, struct file *);
421 static int debug_async_open(struct inode *, struct file *);
422 static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
423 static int debug_close(struct inode *, struct file *);
424
425 static const struct file_operations debug_async_fops = {
426         .owner          = THIS_MODULE,
427         .open           = debug_async_open,
428         .read           = debug_output,
429         .release        = debug_close,
430         .llseek         = default_llseek,
431 };
432 static const struct file_operations debug_periodic_fops = {
433         .owner          = THIS_MODULE,
434         .open           = debug_periodic_open,
435         .read           = debug_output,
436         .release        = debug_close,
437         .llseek         = default_llseek,
438 };
439 static const struct file_operations debug_registers_fops = {
440         .owner          = THIS_MODULE,
441         .open           = debug_registers_open,
442         .read           = debug_output,
443         .release        = debug_close,
444         .llseek         = default_llseek,
445 };
446
447 static struct dentry *ohci_debug_root;
448
449 struct debug_buffer {
450         ssize_t (*fill_func)(struct debug_buffer *);    /* fill method */
451         struct ohci_hcd *ohci;
452         struct mutex mutex;     /* protect filling of buffer */
453         size_t count;           /* number of characters filled into buffer */
454         char *page;
455 };
456
457 static ssize_t
458 show_list (struct ohci_hcd *ohci, char *buf, size_t count, struct ed *ed)
459 {
460         unsigned                temp, size = count;
461
462         if (!ed)
463                 return 0;
464
465         /* print first --> last */
466         while (ed->ed_prev)
467                 ed = ed->ed_prev;
468
469         /* dump a snapshot of the bulk or control schedule */
470         while (ed) {
471                 u32             info = hc32_to_cpu (ohci, ed->hwINFO);
472                 u32             headp = hc32_to_cpu (ohci, ed->hwHeadP);
473                 struct list_head *entry;
474                 struct td       *td;
475
476                 temp = scnprintf (buf, size,
477                         "ed/%p %cs dev%d ep%d%s max %d %08x%s%s %s",
478                         ed,
479                         (info & ED_LOWSPEED) ? 'l' : 'f',
480                         info & 0x7f,
481                         (info >> 7) & 0xf,
482                         (info & ED_IN) ? "in" : "out",
483                         0x03ff & (info >> 16),
484                         info,
485                         (info & ED_SKIP) ? " s" : "",
486                         (headp & ED_H) ? " H" : "",
487                         (headp & ED_C) ? data1 : data0);
488                 size -= temp;
489                 buf += temp;
490
491                 list_for_each (entry, &ed->td_list) {
492                         u32             cbp, be;
493
494                         td = list_entry (entry, struct td, td_list);
495                         info = hc32_to_cpup (ohci, &td->hwINFO);
496                         cbp = hc32_to_cpup (ohci, &td->hwCBP);
497                         be = hc32_to_cpup (ohci, &td->hwBE);
498                         temp = scnprintf (buf, size,
499                                         "\n\ttd %p %s %d cc=%x urb %p (%08x)",
500                                         td,
501                                         ({ char *pid;
502                                         switch (info & TD_DP) {
503                                         case TD_DP_SETUP: pid = "setup"; break;
504                                         case TD_DP_IN: pid = "in"; break;
505                                         case TD_DP_OUT: pid = "out"; break;
506                                         default: pid = "(?)"; break;
507                                          } pid;}),
508                                         cbp ? (be + 1 - cbp) : 0,
509                                         TD_CC_GET (info), td->urb, info);
510                         size -= temp;
511                         buf += temp;
512                 }
513
514                 temp = scnprintf (buf, size, "\n");
515                 size -= temp;
516                 buf += temp;
517
518                 ed = ed->ed_next;
519         }
520         return count - size;
521 }
522
523 static ssize_t fill_async_buffer(struct debug_buffer *buf)
524 {
525         struct ohci_hcd         *ohci;
526         size_t                  temp;
527         unsigned long           flags;
528
529         ohci = buf->ohci;
530
531         /* display control and bulk lists together, for simplicity */
532         spin_lock_irqsave (&ohci->lock, flags);
533         temp = show_list(ohci, buf->page, buf->count, ohci->ed_controltail);
534         temp += show_list(ohci, buf->page + temp, buf->count - temp,
535                           ohci->ed_bulktail);
536         spin_unlock_irqrestore (&ohci->lock, flags);
537
538         return temp;
539 }
540
541 #define DBG_SCHED_LIMIT 64
542
543 static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
544 {
545         struct ohci_hcd         *ohci;
546         struct ed               **seen, *ed;
547         unsigned long           flags;
548         unsigned                temp, size, seen_count;
549         char                    *next;
550         unsigned                i;
551
552         if (!(seen = kmalloc (DBG_SCHED_LIMIT * sizeof *seen, GFP_ATOMIC)))
553                 return 0;
554         seen_count = 0;
555
556         ohci = buf->ohci;
557         next = buf->page;
558         size = PAGE_SIZE;
559
560         temp = scnprintf (next, size, "size = %d\n", NUM_INTS);
561         size -= temp;
562         next += temp;
563
564         /* dump a snapshot of the periodic schedule (and load) */
565         spin_lock_irqsave (&ohci->lock, flags);
566         for (i = 0; i < NUM_INTS; i++) {
567                 if (!(ed = ohci->periodic [i]))
568                         continue;
569
570                 temp = scnprintf (next, size, "%2d [%3d]:", i, ohci->load [i]);
571                 size -= temp;
572                 next += temp;
573
574                 do {
575                         temp = scnprintf (next, size, " ed%d/%p",
576                                 ed->interval, ed);
577                         size -= temp;
578                         next += temp;
579                         for (temp = 0; temp < seen_count; temp++) {
580                                 if (seen [temp] == ed)
581                                         break;
582                         }
583
584                         /* show more info the first time around */
585                         if (temp == seen_count) {
586                                 u32     info = hc32_to_cpu (ohci, ed->hwINFO);
587                                 struct list_head        *entry;
588                                 unsigned                qlen = 0;
589
590                                 /* qlen measured here in TDs, not urbs */
591                                 list_for_each (entry, &ed->td_list)
592                                         qlen++;
593
594                                 temp = scnprintf (next, size,
595                                         " (%cs dev%d ep%d%s-%s qlen %u"
596                                         " max %d %08x%s%s)",
597                                         (info & ED_LOWSPEED) ? 'l' : 'f',
598                                         info & 0x7f,
599                                         (info >> 7) & 0xf,
600                                         (info & ED_IN) ? "in" : "out",
601                                         (info & ED_ISO) ? "iso" : "int",
602                                         qlen,
603                                         0x03ff & (info >> 16),
604                                         info,
605                                         (info & ED_SKIP) ? " K" : "",
606                                         (ed->hwHeadP &
607                                                 cpu_to_hc32(ohci, ED_H)) ?
608                                                         " H" : "");
609                                 size -= temp;
610                                 next += temp;
611
612                                 if (seen_count < DBG_SCHED_LIMIT)
613                                         seen [seen_count++] = ed;
614
615                                 ed = ed->ed_next;
616
617                         } else {
618                                 /* we've seen it and what's after */
619                                 temp = 0;
620                                 ed = NULL;
621                         }
622
623                 } while (ed);
624
625                 temp = scnprintf (next, size, "\n");
626                 size -= temp;
627                 next += temp;
628         }
629         spin_unlock_irqrestore (&ohci->lock, flags);
630         kfree (seen);
631
632         return PAGE_SIZE - size;
633 }
634 #undef DBG_SCHED_LIMIT
635
636 static ssize_t fill_registers_buffer(struct debug_buffer *buf)
637 {
638         struct usb_hcd          *hcd;
639         struct ohci_hcd         *ohci;
640         struct ohci_regs __iomem *regs;
641         unsigned long           flags;
642         unsigned                temp, size;
643         char                    *next;
644         u32                     rdata;
645
646         ohci = buf->ohci;
647         hcd = ohci_to_hcd(ohci);
648         regs = ohci->regs;
649         next = buf->page;
650         size = PAGE_SIZE;
651
652         spin_lock_irqsave (&ohci->lock, flags);
653
654         /* dump driver info, then registers in spec order */
655
656         ohci_dbg_sw (ohci, &next, &size,
657                 "bus %s, device %s\n"
658                 "%s\n"
659                 "%s\n",
660                 hcd->self.controller->bus->name,
661                 dev_name(hcd->self.controller),
662                 hcd->product_desc,
663                 hcd_name);
664
665         if (!HCD_HW_ACCESSIBLE(hcd)) {
666                 size -= scnprintf (next, size,
667                         "SUSPENDED (no register access)\n");
668                 goto done;
669         }
670
671         ohci_dump_status(ohci, &next, &size);
672
673         /* hcca */
674         if (ohci->hcca)
675                 ohci_dbg_sw (ohci, &next, &size,
676                         "hcca frame 0x%04x\n", ohci_frame_no(ohci));
677
678         /* other registers mostly affect frame timings */
679         rdata = ohci_readl (ohci, &regs->fminterval);
680         temp = scnprintf (next, size,
681                         "fmintvl 0x%08x %sFSMPS=0x%04x FI=0x%04x\n",
682                         rdata, (rdata >> 31) ? "FIT " : "",
683                         (rdata >> 16) & 0xefff, rdata & 0xffff);
684         size -= temp;
685         next += temp;
686
687         rdata = ohci_readl (ohci, &regs->fmremaining);
688         temp = scnprintf (next, size, "fmremaining 0x%08x %sFR=0x%04x\n",
689                         rdata, (rdata >> 31) ? "FRT " : "",
690                         rdata & 0x3fff);
691         size -= temp;
692         next += temp;
693
694         rdata = ohci_readl (ohci, &regs->periodicstart);
695         temp = scnprintf (next, size, "periodicstart 0x%04x\n",
696                         rdata & 0x3fff);
697         size -= temp;
698         next += temp;
699
700         rdata = ohci_readl (ohci, &regs->lsthresh);
701         temp = scnprintf (next, size, "lsthresh 0x%04x\n",
702                         rdata & 0x3fff);
703         size -= temp;
704         next += temp;
705
706         temp = scnprintf (next, size, "hub poll timer %s\n",
707                         HCD_POLL_RH(ohci_to_hcd(ohci)) ? "ON" : "off");
708         size -= temp;
709         next += temp;
710
711         /* roothub */
712         ohci_dump_roothub (ohci, 1, &next, &size);
713
714 done:
715         spin_unlock_irqrestore (&ohci->lock, flags);
716
717         return PAGE_SIZE - size;
718 }
719
720 static struct debug_buffer *alloc_buffer(struct ohci_hcd *ohci,
721                                 ssize_t (*fill_func)(struct debug_buffer *))
722 {
723         struct debug_buffer *buf;
724
725         buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
726
727         if (buf) {
728                 buf->ohci = ohci;
729                 buf->fill_func = fill_func;
730                 mutex_init(&buf->mutex);
731         }
732
733         return buf;
734 }
735
736 static int fill_buffer(struct debug_buffer *buf)
737 {
738         int ret = 0;
739
740         if (!buf->page)
741                 buf->page = (char *)get_zeroed_page(GFP_KERNEL);
742
743         if (!buf->page) {
744                 ret = -ENOMEM;
745                 goto out;
746         }
747
748         ret = buf->fill_func(buf);
749
750         if (ret >= 0) {
751                 buf->count = ret;
752                 ret = 0;
753         }
754
755 out:
756         return ret;
757 }
758
759 static ssize_t debug_output(struct file *file, char __user *user_buf,
760                         size_t len, loff_t *offset)
761 {
762         struct debug_buffer *buf = file->private_data;
763         int ret = 0;
764
765         mutex_lock(&buf->mutex);
766         if (buf->count == 0) {
767                 ret = fill_buffer(buf);
768                 if (ret != 0) {
769                         mutex_unlock(&buf->mutex);
770                         goto out;
771                 }
772         }
773         mutex_unlock(&buf->mutex);
774
775         ret = simple_read_from_buffer(user_buf, len, offset,
776                                       buf->page, buf->count);
777
778 out:
779         return ret;
780
781 }
782
783 static int debug_close(struct inode *inode, struct file *file)
784 {
785         struct debug_buffer *buf = file->private_data;
786
787         if (buf) {
788                 if (buf->page)
789                         free_page((unsigned long)buf->page);
790                 kfree(buf);
791         }
792
793         return 0;
794 }
795 static int debug_async_open(struct inode *inode, struct file *file)
796 {
797         file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
798
799         return file->private_data ? 0 : -ENOMEM;
800 }
801
802 static int debug_periodic_open(struct inode *inode, struct file *file)
803 {
804         file->private_data = alloc_buffer(inode->i_private,
805                                           fill_periodic_buffer);
806
807         return file->private_data ? 0 : -ENOMEM;
808 }
809
810 static int debug_registers_open(struct inode *inode, struct file *file)
811 {
812         file->private_data = alloc_buffer(inode->i_private,
813                                           fill_registers_buffer);
814
815         return file->private_data ? 0 : -ENOMEM;
816 }
817 static inline void create_debug_files (struct ohci_hcd *ohci)
818 {
819         struct usb_bus *bus = &ohci_to_hcd(ohci)->self;
820
821         ohci->debug_dir = debugfs_create_dir(bus->bus_name, ohci_debug_root);
822         if (!ohci->debug_dir)
823                 goto dir_error;
824
825         ohci->debug_async = debugfs_create_file("async", S_IRUGO,
826                                                 ohci->debug_dir, ohci,
827                                                 &debug_async_fops);
828         if (!ohci->debug_async)
829                 goto async_error;
830
831         ohci->debug_periodic = debugfs_create_file("periodic", S_IRUGO,
832                                                    ohci->debug_dir, ohci,
833                                                    &debug_periodic_fops);
834         if (!ohci->debug_periodic)
835                 goto periodic_error;
836
837         ohci->debug_registers = debugfs_create_file("registers", S_IRUGO,
838                                                     ohci->debug_dir, ohci,
839                                                     &debug_registers_fops);
840         if (!ohci->debug_registers)
841                 goto registers_error;
842
843         ohci_dbg (ohci, "created debug files\n");
844         return;
845
846 registers_error:
847         debugfs_remove(ohci->debug_periodic);
848 periodic_error:
849         debugfs_remove(ohci->debug_async);
850 async_error:
851         debugfs_remove(ohci->debug_dir);
852 dir_error:
853         ohci->debug_periodic = NULL;
854         ohci->debug_async = NULL;
855         ohci->debug_dir = NULL;
856 }
857
858 static inline void remove_debug_files (struct ohci_hcd *ohci)
859 {
860         debugfs_remove(ohci->debug_registers);
861         debugfs_remove(ohci->debug_periodic);
862         debugfs_remove(ohci->debug_async);
863         debugfs_remove(ohci->debug_dir);
864 }
865
866 #endif
867
868 /*-------------------------------------------------------------------------*/
869