[adm5120] USB driver cleanups, no it is still not finished
[openwrt.git] / target / linux / adm5120 / files / drivers / usb / host / adm5120-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 static inline char *ed_typestring(int ed_type)
13 {
14         switch (ed_type) {
15         case PIPE_CONTROL:
16                 return "ctrl";
17         case PIPE_BULK:
18                 return "bulk";
19         case PIPE_INTERRUPT:
20                 return "intr";
21         case PIPE_ISOCHRONOUS:
22                 return "isoc";
23         }
24         return "(bad ed_type)";
25 }
26
27 static inline char *ed_statestring(int state)
28 {
29         switch (state) {
30         case ED_IDLE:
31                 return "IDLE";
32         case ED_UNLINK:
33                 return "UNLINK";
34         case ED_OPER:
35                 return "OPER";
36         case ED_NEW:
37                 return "NEW";
38         }
39         return "?STATE";
40 }
41
42 static inline char *pipestring(int pipe)
43 {
44         return ed_typestring(usb_pipetype(pipe));
45 }
46
47 static inline char *td_pidstring(u32 info)
48 {
49         switch (info & TD_DP) {
50         case TD_DP_SETUP:
51                 return "SETUP";
52         case TD_DP_IN:
53                 return "IN";
54         case TD_DP_OUT:
55                 return "OUT";
56         }
57         return "?PID";
58 }
59
60 static inline char *td_togglestring(u32 info)
61 {
62         switch (info & TD_T) {
63         case TD_T_DATA0:
64                 return "DATA0";
65         case TD_T_DATA1:
66                 return "DATA1";
67         case TD_T_CARRY:
68                 return "CARRY";
69         }
70         return "?TOGGLE";
71 }
72
73 #ifdef DEBUG
74
75 /* debug| print the main components of an URB
76  * small: 0) header + data packets 1) just header
77  */
78 static void __attribute__((unused))
79 urb_print(struct admhcd *ahcd, struct urb * urb, char * str, int small)
80 {
81         unsigned int pipe = urb->pipe;
82
83         if (!urb->dev || !urb->dev->bus) {
84                 admhc_dbg(ahcd, "%s URB: no dev", str);
85                 return;
86         }
87
88 #ifndef ADMHC_VERBOSE_DEBUG
89         if (urb->status != 0)
90 #endif
91         admhc_dbg(ahcd, "URB-%s %p dev=%d ep=%d%s-%s flags=%x len=%d/%d "
92                         "stat=%d\n",
93                         str,
94                         urb,
95                         usb_pipedevice(pipe),
96                         usb_pipeendpoint(pipe),
97                         usb_pipeout(pipe)? "out" : "in",
98                         pipestring(pipe),
99                         urb->transfer_flags,
100                         urb->actual_length,
101                         urb->transfer_buffer_length,
102                         urb->status);
103
104 #ifdef  ADMHC_VERBOSE_DEBUG
105         if (!small) {
106                 int i, len;
107
108                 if (usb_pipecontrol(pipe)) {
109                         admhc_dbg(admhc, "setup(8): ");
110                         for (i = 0; i < 8 ; i++)
111                                 printk (" %02x", ((__u8 *) urb->setup_packet) [i]);
112                         printk ("\n");
113                 }
114                 if (urb->transfer_buffer_length > 0 && urb->transfer_buffer) {
115                         admhc_dbg(admhc, "data(%d/%d): ",
116                                 urb->actual_length,
117                                 urb->transfer_buffer_length);
118                         len = usb_pipeout(pipe)?
119                                                 urb->transfer_buffer_length: urb->actual_length;
120                         for (i = 0; i < 16 && i < len; i++)
121                                 printk (" %02x", ((__u8 *) urb->transfer_buffer) [i]);
122                         printk ("%s stat:%d\n", i < len? "...": "", urb->status);
123                 }
124         }
125 #endif /* ADMHC_VERBOSE_DEBUG */
126 }
127
128 #define admhc_dbg_sw(ahcd, next, size, format, arg...) \
129         do { \
130         if (next) { \
131                 unsigned s_len; \
132                 s_len = scnprintf(*next, *size, format, ## arg ); \
133                 *size -= s_len; *next += s_len; \
134         } else \
135                 admhc_dbg(ahcd,format, ## arg ); \
136         } while (0);
137
138
139 static void admhc_dump_intr_mask(struct admhcd *ahcd, char *label, u32 mask,
140                 char **next, unsigned *size)
141 {
142         admhc_dbg_sw(ahcd, next, size, "%s 0x%08x%s%s%s%s%s%s%s%s%s%s\n",
143                 label,
144                 mask,
145                 (mask & ADMHC_INTR_INTA) ? " INTA" : "",
146                 (mask & ADMHC_INTR_FATI) ? " FATI" : "",
147                 (mask & ADMHC_INTR_SWI) ? " SWI" : "",
148                 (mask & ADMHC_INTR_TDC) ? " TDC" : "",
149                 (mask & ADMHC_INTR_FNO) ? " FNO" : "",
150                 (mask & ADMHC_INTR_SO) ? " SO" : "",
151                 (mask & ADMHC_INTR_INSM) ? " INSM" : "",
152                 (mask & ADMHC_INTR_BABI) ? " BABI" : "",
153                 (mask & ADMHC_INTR_RESI) ? " RESI" : "",
154                 (mask & ADMHC_INTR_SOFI) ? " SOFI" : ""
155                 );
156 }
157
158 static void maybe_print_eds(struct admhcd *ahcd, char *label, u32 value,
159                 char **next, unsigned *size)
160 {
161         if (value)
162                 admhc_dbg_sw(ahcd, next, size, "%s %08x\n", label, value);
163 }
164
165 static char *buss2string (int state)
166 {
167         switch (state) {
168         case ADMHC_BUSS_RESET:
169                 return "reset";
170         case ADMHC_BUSS_RESUME:
171                 return "resume";
172         case ADMHC_BUSS_OPER:
173                 return "operational";
174         case ADMHC_BUSS_SUSPEND:
175                 return "suspend";
176         }
177         return "?state";
178 }
179
180 static void
181 admhc_dump_status(struct admhcd *ahcd, char **next, unsigned *size)
182 {
183         struct admhcd_regs __iomem *regs = ahcd->regs;
184         u32                     temp;
185
186         temp = admhc_readl(ahcd, &regs->gencontrol);
187         admhc_dbg_sw(ahcd, next, size,
188                 "gencontrol 0x%08x%s%s%s%s\n",
189                 temp,
190                 (temp & ADMHC_CTRL_UHFE) ? " UHFE" : "",
191                 (temp & ADMHC_CTRL_SIR) ? " SIR" : "",
192                 (temp & ADMHC_CTRL_DMAA) ? " DMAA" : "",
193                 (temp & ADMHC_CTRL_SR) ? " SR" : ""
194                 );
195
196         temp = admhc_readl(ahcd, &regs->host_control);
197         admhc_dbg_sw(ahcd, next, size,
198                 "host_control 0x%08x BUSS=%s%s\n",
199                 temp,
200                 buss2string(temp & ADMHC_HC_BUSS),
201                 (temp & ADMHC_HC_DMAE) ? " DMAE" : ""
202                 );
203
204         admhc_dump_intr_mask(ahcd, "int_status",
205                         admhc_readl(ahcd, &regs->int_status),
206                         next, size);
207         admhc_dump_intr_mask(ahcd, "int_enable",
208                         admhc_readl(ahcd, &regs->int_enable),
209                         next, size);
210
211         maybe_print_eds(ahcd, "hosthead",
212                         admhc_readl(ahcd, &regs->hosthead), next, size);
213 }
214
215 #define dbg_port_sw(hc,num,value,next,size) \
216         admhc_dbg_sw(hc, next, size, \
217                 "portstatus [%d] " \
218                 "0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
219                 num, temp, \
220                 (temp & ADMHC_PS_PRSC) ? " PRSC" : "", \
221                 (temp & ADMHC_PS_OCIC) ? " OCIC" : "", \
222                 (temp & ADMHC_PS_PSSC) ? " PSSC" : "", \
223                 (temp & ADMHC_PS_PESC) ? " PESC" : "", \
224                 (temp & ADMHC_PS_CSC) ? " CSC" : "", \
225                 \
226                 (temp & ADMHC_PS_LSDA) ? " LSDA" : "", \
227                 (temp & ADMHC_PS_PPS) ? " PPS" : "", \
228                 (temp & ADMHC_PS_PRS) ? " PRS" : "", \
229                 (temp & ADMHC_PS_POCI) ? " POCI" : "", \
230                 (temp & ADMHC_PS_PSS) ? " PSS" : "", \
231                 \
232                 (temp & ADMHC_PS_PES) ? " PES" : "", \
233                 (temp & ADMHC_PS_CCS) ? " CCS" : "" \
234                 );
235
236
237 static void
238 admhc_dump_roothub(
239         struct admhcd *ahcd,
240         int verbose,
241         char **next,
242         unsigned *size)
243 {
244         u32                     temp, i;
245
246         temp = admhc_read_rhdesc(ahcd);
247         if (temp == ~(u32)0)
248                 return;
249
250         if (verbose) {
251                 admhc_dbg_sw(ahcd, next, size,
252                         "rhdesc %08x%s%s%s%s%s%s PPCM=%02x%s%s%s%s NUMP=%d(%d)\n",
253                         temp,
254                         (temp & ADMHC_RH_CRWE) ? " CRWE" : "",
255                         (temp & ADMHC_RH_OCIC) ? " OCIC" : "",
256                         (temp & ADMHC_RH_LPSC) ? " LPSC" : "",
257                         (temp & ADMHC_RH_LPSC) ? " DRWE" : "",
258                         (temp & ADMHC_RH_LPSC) ? " OCI" : "",
259                         (temp & ADMHC_RH_LPSC) ? " LPS" : "",
260                         ((temp & ADMHC_RH_PPCM) >> 16),
261                         (temp & ADMHC_RH_NOCP) ? " NOCP" : "",
262                         (temp & ADMHC_RH_OCPM) ? " OCPM" : "",
263                         (temp & ADMHC_RH_NPS) ? " NPS" : "",
264                         (temp & ADMHC_RH_PSM) ? " PSM" : "",
265                         (temp & ADMHC_RH_NUMP), ahcd->num_ports
266                         );
267         }
268
269         for (i = 0; i < ahcd->num_ports; i++) {
270                 temp = admhc_read_portstatus(ahcd, i);
271                 dbg_port_sw(ahcd, i, temp, next, size);
272         }
273 }
274
275 static void admhc_dump(struct admhcd *ahcd, int verbose)
276 {
277         admhc_dbg(ahcd, "ADMHC ahcd state\n");
278
279         /* dumps some of the state we know about */
280         admhc_dump_status(ahcd, NULL, NULL);
281         admhc_dbg(ahcd,"current frame #%04x\n",
282                 admhc_frame_no(ahcd));
283
284         admhc_dump_roothub(ahcd, verbose, NULL, NULL);
285 }
286
287 static const char data0[] = "DATA0";
288 static const char data1[] = "DATA1";
289
290 static void admhc_dump_td(const struct admhcd *ahcd, const char *label,
291                 const struct td *td)
292 {
293         u32     tmp;
294
295         admhc_dbg(ahcd, "%s td %p; urb %p index %d; hwNextTD %08x\n",
296                 label, td,
297                 td->urb, td->index,
298                 hc32_to_cpup(ahcd, &td->hwNextTD));
299
300         tmp = hc32_to_cpup(ahcd, &td->hwINFO);
301         admhc_dbg(ahcd, "     status %08x%s CC=%x EC=%d %s %s ISI=%x FN=%x\n",
302                 tmp,
303                 (tmp & TD_OWN) ? " OWN" : "",
304                 TD_CC_GET(tmp),
305                 TD_EC_GET(tmp),
306                 td_togglestring(tmp),
307                 td_pidstring(tmp),
308                 TD_ISI_GET(tmp),
309                 TD_FN_GET(tmp));
310
311         tmp = hc32_to_cpup(ahcd, &td->hwCBL);
312         admhc_dbg(ahcd, "     dbp %08x; cbl %08x; LEN=%d%s\n",
313                 hc32_to_cpup(ahcd, &td->hwDBP),
314                 tmp,
315                 TD_BL_GET(tmp),
316                 (tmp & TD_IE) ? " IE" : "");
317 }
318
319 static void admhc_dump_up(const struct admhcd *ahcd, const char *label,
320                 const struct urb_priv *up)
321 {
322         int i;
323
324         admhc_dbg(ahcd, "%s urb/%p:\n", label, up->urb);
325         for (i = 0; i < up->td_cnt; i++) {
326                 struct td *td = up->td[i];
327                 admhc_dump_td(ahcd, "    ->", td);
328         }
329 }
330
331 /* caller MUST own hcd spinlock if verbose is set! */
332 static void __attribute__((unused))
333 admhc_dump_ed(const struct admhcd *ahcd, const char *label,
334                 const struct ed *ed, int verbose)
335 {
336         u32 tmp = hc32_to_cpu(ahcd, ed->hwINFO);
337
338         admhc_dbg(ahcd, "%s ed %p %s type %s; next ed %08x\n",
339                 label,
340                 ed, ed_statestring(ed->state), ed_typestring(ed->type),
341                 hc32_to_cpup(ahcd, &ed->hwNextED));
342
343         admhc_dbg(ahcd, "  info %08x MAX=%d%s%s%s%s EP=%d DEV=%d\n", tmp,
344                 ED_MPS_GET(tmp),
345                 (tmp & ED_ISO) ? " ISO" : "",
346                 (tmp & ED_SKIP) ? " SKIP" : "",
347                 (tmp & ED_SPEED_FULL) ? " FULL" : " LOW",
348                 (tmp & ED_INT) ? " INT" : "",
349                 ED_EN_GET(tmp),
350                 ED_FA_GET(tmp));
351
352         tmp = hc32_to_cpup(ahcd, &ed->hwHeadP);
353         admhc_dbg(ahcd, "  tds: head %08x tail %08x %s%s\n",
354                 tmp & TD_MASK,
355                 hc32_to_cpup(ahcd, &ed->hwTailP),
356                 (tmp & ED_C) ? data1 : data0,
357                 (tmp & ED_H) ? " HALT" : "");
358
359         if (ed->urb_active)
360                 admhc_dump_up(ahcd, "  active ", ed->urb_active);
361
362         if ((verbose) && (!list_empty(&ed->urb_pending))) {
363                 struct list_head *entry;
364                 /* dump pending URBs */
365                 list_for_each(entry, &ed->urb_pending) {
366                         struct urb_priv *up;
367                         up = list_entry(entry, struct urb_priv, pending);
368                         admhc_dump_up(ahcd, "  pending ", up);
369                 }
370         }
371 }
372
373 #else /* ifdef DEBUG */
374
375 static inline void urb_print(struct urb * urb, char * str, int small) {}
376 static inline void admhc_dump_up(const struct admhcd *ahcd, const char *label,
377         const struct urb_priv *up) {}
378 static inline void admhc_dump_ed(const struct admhcd *ahcd, const char *label,
379         const struct ed *ed, int verbose) {}
380 static inline void admhc_dump_td(const struct admhcd *ahcd, const char *label,
381         const struct td *td) {}
382 static inline void admhc_dump(struct admhcd *ahcd, int verbose) {}
383
384 #undef ADMHC_VERBOSE_DEBUG
385
386 #endif /* DEBUG */
387
388 /*-------------------------------------------------------------------------*/
389
390 #ifdef STUB_DEBUG_FILES
391
392 static inline void create_debug_files(struct admhcd *bus) { }
393 static inline void remove_debug_files(struct admhcd *bus) { }
394
395 #else
396
397 static ssize_t
398 show_urb_priv(struct admhcd *ahcd, char *buf, size_t count,
399                 struct urb_priv *up)
400 {
401         unsigned temp, size = count;
402         int i;
403
404         if (!up)
405                 return 0;
406
407         temp = scnprintf(buf, size,"\n\turb %p ", up->urb);
408         size -= temp;
409         buf += temp;
410
411         for (i = 0; i< up->td_cnt; i++) {
412                 struct td *td;
413                 u32 dbp, cbl, info;
414
415                 td = up->td[i];
416                 info = hc32_to_cpup(ahcd, &td->hwINFO);
417                 dbp = hc32_to_cpup(ahcd, &td->hwDBP);
418                 cbl = hc32_to_cpup(ahcd, &td->hwCBL);
419
420                 temp = scnprintf(buf, size,
421                         "\n\t\ttd %p %s %d %s%scc=%x (%08x,%08x)",
422                         td,
423                         td_pidstring(info),
424                         TD_BL_GET(cbl),
425                         (info & TD_OWN) ? "WORK " : "DONE ",
426                         (cbl & TD_IE) ? "IE " : "",
427                         TD_CC_GET(info), info, cbl);
428                 size -= temp;
429                 buf += temp;
430         }
431
432         return count - size;
433 }
434
435 static ssize_t
436 show_list(struct admhcd *ahcd, char *buf, size_t count, struct ed *ed)
437 {
438         unsigned                temp, size = count;
439
440         if (!ed)
441                 return 0;
442
443         /* dump a snapshot of the bulk or control schedule */
444         while (ed) {
445                 u32 info = hc32_to_cpu(ahcd, ed->hwINFO);
446                 u32 headp = hc32_to_cpu(ahcd, ed->hwHeadP);
447
448                 temp = scnprintf(buf, size,
449                         "ed/%p %s %cs dev%d ep%d %s%smax %d %08x%s%s %s",
450                         ed,
451                         ed_typestring (ed->type),
452                         (info & ED_SPEED_FULL) ? 'f' : 'l',
453                         info & ED_FA_MASK,
454                         (info >> ED_EN_SHIFT) & ED_EN_MASK,
455                         (info & ED_INT) ? "INT " : "",
456                         (info & ED_ISO) ? "ISO " : "",
457                         (info >> ED_MPS_SHIFT) & ED_MPS_MASK ,
458                         info,
459                         (info & ED_SKIP) ? " S" : "",
460                         (headp & ED_H) ? " H" : "",
461                         (headp & ED_C) ? "DATA1" : "DATA0");
462                 size -= temp;
463                 buf += temp;
464
465                 if (ed->urb_active) {
466                         temp = scnprintf(buf, size, "\nactive urb:");
467                         size -= temp;
468                         buf += temp;
469
470                         temp = show_urb_priv(ahcd, buf, size, ed->urb_active);
471                         size -= temp;
472                         buf += temp;
473                 }
474
475                 if (!list_empty(&ed->urb_pending)) {
476                         struct list_head *entry;
477
478                         temp = scnprintf(buf, size, "\npending urbs:");
479                         size -= temp;
480                         buf += temp;
481
482                         list_for_each(entry, &ed->urb_pending) {
483                                 struct urb_priv *up;
484                                 up = list_entry(entry, struct urb_priv,
485                                         pending);
486
487                                 temp = show_urb_priv(ahcd, buf, size, up);
488                                 size -= temp;
489                                 buf += temp;
490                         }
491                 }
492
493                 temp = scnprintf(buf, size, "\n");
494                 size -= temp;
495                 buf += temp;
496
497                 ed = ed->ed_next;
498         }
499
500         return count - size;
501 }
502
503 static ssize_t
504 show_async(struct class_device *class_dev, char *buf)
505 {
506         struct usb_bus          *bus;
507         struct usb_hcd          *hcd;
508         struct admhcd           *ahcd;
509         size_t                  temp;
510         unsigned long           flags;
511
512         bus = class_get_devdata(class_dev);
513         hcd = bus_to_hcd(bus);
514         ahcd = hcd_to_admhcd(hcd);
515
516         /* display control and bulk lists together, for simplicity */
517         spin_lock_irqsave(&ahcd->lock, flags);
518         temp = show_list(ahcd, buf, PAGE_SIZE, ahcd->ed_head);
519         spin_unlock_irqrestore(&ahcd->lock, flags);
520
521         return temp;
522 }
523 static CLASS_DEVICE_ATTR(async, S_IRUGO, show_async, NULL);
524
525 static ssize_t
526 show_registers(struct class_device *class_dev, char *buf)
527 {
528         struct usb_bus          *bus;
529         struct usb_hcd          *hcd;
530         struct admhcd           *ahcd;
531         struct admhcd_regs __iomem *regs;
532         unsigned long           flags;
533         unsigned                temp, size;
534         char                    *next;
535         u32                     rdata;
536
537         bus = class_get_devdata(class_dev);
538         hcd = bus_to_hcd(bus);
539         ahcd = hcd_to_admhcd(hcd);
540         regs = ahcd->regs;
541         next = buf;
542         size = PAGE_SIZE;
543
544         spin_lock_irqsave(&ahcd->lock, flags);
545
546         /* dump driver info, then registers in spec order */
547
548         admhc_dbg_sw(ahcd, &next, &size,
549                 "bus %s, device %s\n"
550                 "%s\n"
551                 "%s version " DRIVER_VERSION "\n",
552                 hcd->self.controller->bus->name,
553                 hcd->self.controller->bus_id,
554                 hcd->product_desc,
555                 hcd_name);
556
557         if (bus->controller->power.power_state.event) {
558                 size -= scnprintf(next, size,
559                         "SUSPENDED (no register access)\n");
560                 goto done;
561         }
562
563         admhc_dump_status(ahcd, &next, &size);
564
565         /* other registers mostly affect frame timings */
566         rdata = admhc_readl(ahcd, &regs->fminterval);
567         temp = scnprintf(next, size,
568                         "fmintvl 0x%08x %sFSLDP=0x%04x FI=0x%04x\n",
569                         rdata, (rdata & ADMHC_SFI_FIT) ? "FIT " : "",
570                         (rdata >> ADMHC_SFI_FSLDP_SHIFT) & ADMHC_SFI_FSLDP_MASK,
571                         rdata  & ADMHC_SFI_FI_MASK);
572         size -= temp;
573         next += temp;
574
575         rdata = admhc_readl(ahcd, &regs->fmnumber);
576         temp = scnprintf(next, size, "fmnumber 0x%08x %sFR=0x%04x FN=%04x\n",
577                         rdata, (rdata & ADMHC_SFN_FRT) ? "FRT " : "",
578                         (rdata >> ADMHC_SFN_FR_SHIFT) & ADMHC_SFN_FR_MASK,
579                         rdata  & ADMHC_SFN_FN_MASK);
580         size -= temp;
581         next += temp;
582
583         /* TODO: use predefined bitmask */
584         rdata = admhc_readl(ahcd, &regs->lsthresh);
585         temp = scnprintf(next, size, "lsthresh 0x%04x\n",
586                         rdata & 0x3fff);
587         size -= temp;
588         next += temp;
589
590         temp = scnprintf(next, size, "hub poll timer: %s\n",
591                         admhcd_to_hcd(ahcd)->poll_rh ? "ON" : "OFF");
592         size -= temp;
593         next += temp;
594
595         /* roothub */
596         admhc_dump_roothub(ahcd, 1, &next, &size);
597
598 done:
599         spin_unlock_irqrestore(&ahcd->lock, flags);
600         return PAGE_SIZE - size;
601 }
602 static CLASS_DEVICE_ATTR(registers, S_IRUGO, show_registers, NULL);
603
604
605 static inline void create_debug_files (struct admhcd *ahcd)
606 {
607         struct class_device *cldev = admhcd_to_hcd(ahcd)->self.class_dev;
608         int retval;
609
610         retval = class_device_create_file(cldev, &class_device_attr_async);
611         retval = class_device_create_file(cldev, &class_device_attr_registers);
612         admhc_dbg(ahcd, "created debug files\n");
613 }
614
615 static inline void remove_debug_files (struct admhcd *ahcd)
616 {
617         struct class_device *cldev = admhcd_to_hcd(ahcd)->self.class_dev;
618
619         class_device_remove_file(cldev, &class_device_attr_async);
620         class_device_remove_file(cldev, &class_device_attr_registers);
621 }
622
623 #endif
624
625 /*-------------------------------------------------------------------------*/
626