blob: 4498efb49b95a66cc9cc9e5b08716df860892d02 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2001-2002 by David Brownell
David Brownell53bd6a62006-08-30 14:50:06 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/* this file is part of ehci-hcd.c */
20
21#define ehci_dbg(ehci, fmt, args...) \
22 dev_dbg (ehci_to_hcd(ehci)->self.controller , fmt , ## args )
23#define ehci_err(ehci, fmt, args...) \
24 dev_err (ehci_to_hcd(ehci)->self.controller , fmt , ## args )
25#define ehci_info(ehci, fmt, args...) \
26 dev_info (ehci_to_hcd(ehci)->self.controller , fmt , ## args )
27#define ehci_warn(ehci, fmt, args...) \
28 dev_warn (ehci_to_hcd(ehci)->self.controller , fmt , ## args )
29
David Brownell9776afc2008-02-01 11:42:05 -080030#ifdef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070031# define vdbg dbg
32# define ehci_vdbg ehci_dbg
33#else
34# define vdbg(fmt,args...) do { } while (0)
35# define ehci_vdbg(ehci, fmt, args...) do { } while (0)
36#endif
37
38#ifdef DEBUG
39
40/* check the values in the HCSPARAMS register
41 * (host controller _Structural_ parameters)
42 * see EHCI spec, Table 2-4 for each value
43 */
44static void dbg_hcs_params (struct ehci_hcd *ehci, char *label)
45{
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +110046 u32 params = ehci_readl(ehci, &ehci->caps->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 ehci_dbg (ehci,
49 "%s hcs_params 0x%x dbg=%d%s cc=%d pcc=%d%s%s ports=%d\n",
50 label, params,
51 HCS_DEBUG_PORT (params),
52 HCS_INDICATOR (params) ? " ind" : "",
53 HCS_N_CC (params),
54 HCS_N_PCC (params),
Stefan Roese6dbd6822007-05-01 09:29:37 -070055 HCS_PORTROUTED (params) ? "" : " ordered",
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 HCS_PPC (params) ? "" : " !ppc",
57 HCS_N_PORTS (params)
58 );
59 /* Port routing, per EHCI 0.95 Spec, Section 2.2.5 */
60 if (HCS_PORTROUTED (params)) {
61 int i;
62 char buf [46], tmp [7], byte;
63
64 buf[0] = 0;
65 for (i = 0; i < HCS_N_PORTS (params); i++) {
66 // FIXME MIPS won't readb() ...
67 byte = readb (&ehci->caps->portroute[(i>>1)]);
David Brownell53bd6a62006-08-30 14:50:06 -070068 sprintf(tmp, "%d ",
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 ((i & 0x1) ? ((byte)&0xf) : ((byte>>4)&0xf)));
70 strcat(buf, tmp);
71 }
72 ehci_dbg (ehci, "%s portroute %s\n",
73 label, buf);
74 }
75}
76#else
77
78static inline void dbg_hcs_params (struct ehci_hcd *ehci, char *label) {}
79
80#endif
81
82#ifdef DEBUG
83
84/* check the values in the HCCPARAMS register
85 * (host controller _Capability_ parameters)
86 * see EHCI Spec, Table 2-5 for each value
87 * */
88static void dbg_hcc_params (struct ehci_hcd *ehci, char *label)
89{
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +110090 u32 params = ehci_readl(ehci, &ehci->caps->hcc_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 if (HCC_ISOC_CACHE (params)) {
93 ehci_dbg (ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -070094 "%s hcc_params %04x caching frame %s%s%s\n",
95 label, params,
96 HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
97 HCC_CANPARK(params) ? " park" : "",
98 HCC_64BIT_ADDR(params) ? " 64 bit addr" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 } else {
100 ehci_dbg (ehci,
Alek Duaa4d8342010-06-04 15:47:54 +0800101 "%s hcc_params %04x thresh %d uframes %s%s%s%s%s%s%s\n",
Stefan Roese6dbd6822007-05-01 09:29:37 -0700102 label,
103 params,
104 HCC_ISOC_THRES(params),
105 HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
106 HCC_CANPARK(params) ? " park" : "",
Alek Duaa4d8342010-06-04 15:47:54 +0800107 HCC_64BIT_ADDR(params) ? " 64 bit addr" : "",
108 HCC_LPM(params) ? " LPM" : "",
109 HCC_PER_PORT_CHANGE_EVENT(params) ? " ppce" : "",
110 HCC_HW_PREFETCH(params) ? " hw prefetch" : "",
111 HCC_32FRAME_PERIODIC_LIST(params) ?
112 " 32 peridic list" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114}
115#else
116
117static inline void dbg_hcc_params (struct ehci_hcd *ehci, char *label) {}
118
119#endif
120
121#ifdef DEBUG
122
David Rientjes82345092007-05-11 14:39:44 -0700123static void __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124dbg_qtd (const char *label, struct ehci_hcd *ehci, struct ehci_qtd *qtd)
125{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700126 ehci_dbg(ehci, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
127 hc32_to_cpup(ehci, &qtd->hw_next),
128 hc32_to_cpup(ehci, &qtd->hw_alt_next),
129 hc32_to_cpup(ehci, &qtd->hw_token),
130 hc32_to_cpup(ehci, &qtd->hw_buf [0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (qtd->hw_buf [1])
Stefan Roese6dbd6822007-05-01 09:29:37 -0700132 ehci_dbg(ehci, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
133 hc32_to_cpup(ehci, &qtd->hw_buf[1]),
134 hc32_to_cpup(ehci, &qtd->hw_buf[2]),
135 hc32_to_cpup(ehci, &qtd->hw_buf[3]),
136 hc32_to_cpup(ehci, &qtd->hw_buf[4]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
David Rientjes82345092007-05-11 14:39:44 -0700139static void __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140dbg_qh (const char *label, struct ehci_hcd *ehci, struct ehci_qh *qh)
141{
Alek Du3807e262009-07-14 07:23:29 +0800142 struct ehci_qh_hw *hw = qh->hw;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 ehci_dbg (ehci, "%s qh %p n%08x info %x %x qtd %x\n", label,
Alek Du3807e262009-07-14 07:23:29 +0800145 qh, hw->hw_next, hw->hw_info1, hw->hw_info2, hw->hw_current);
146 dbg_qtd("overlay", ehci, (struct ehci_qtd *) &hw->hw_qtd_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
David Rientjes82345092007-05-11 14:39:44 -0700149static void __maybe_unused
David Brownell53bd6a62006-08-30 14:50:06 -0700150dbg_itd (const char *label, struct ehci_hcd *ehci, struct ehci_itd *itd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 ehci_dbg (ehci, "%s [%d] itd %p, next %08x, urb %p\n",
Stefan Roese6dbd6822007-05-01 09:29:37 -0700153 label, itd->frame, itd, hc32_to_cpu(ehci, itd->hw_next),
154 itd->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 ehci_dbg (ehci,
David Brownell53bd6a62006-08-30 14:50:06 -0700156 " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
Stefan Roese6dbd6822007-05-01 09:29:37 -0700157 hc32_to_cpu(ehci, itd->hw_transaction[0]),
158 hc32_to_cpu(ehci, itd->hw_transaction[1]),
159 hc32_to_cpu(ehci, itd->hw_transaction[2]),
160 hc32_to_cpu(ehci, itd->hw_transaction[3]),
161 hc32_to_cpu(ehci, itd->hw_transaction[4]),
162 hc32_to_cpu(ehci, itd->hw_transaction[5]),
163 hc32_to_cpu(ehci, itd->hw_transaction[6]),
164 hc32_to_cpu(ehci, itd->hw_transaction[7]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 ehci_dbg (ehci,
David Brownell53bd6a62006-08-30 14:50:06 -0700166 " buf: %08x %08x %08x %08x %08x %08x %08x\n",
Stefan Roese6dbd6822007-05-01 09:29:37 -0700167 hc32_to_cpu(ehci, itd->hw_bufp[0]),
168 hc32_to_cpu(ehci, itd->hw_bufp[1]),
169 hc32_to_cpu(ehci, itd->hw_bufp[2]),
170 hc32_to_cpu(ehci, itd->hw_bufp[3]),
171 hc32_to_cpu(ehci, itd->hw_bufp[4]),
172 hc32_to_cpu(ehci, itd->hw_bufp[5]),
173 hc32_to_cpu(ehci, itd->hw_bufp[6]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 ehci_dbg (ehci, " index: %d %d %d %d %d %d %d %d\n",
175 itd->index[0], itd->index[1], itd->index[2],
176 itd->index[3], itd->index[4], itd->index[5],
177 itd->index[6], itd->index[7]);
178}
179
David Rientjes82345092007-05-11 14:39:44 -0700180static void __maybe_unused
David Brownell53bd6a62006-08-30 14:50:06 -0700181dbg_sitd (const char *label, struct ehci_hcd *ehci, struct ehci_sitd *sitd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 ehci_dbg (ehci, "%s [%d] sitd %p, next %08x, urb %p\n",
Stefan Roese6dbd6822007-05-01 09:29:37 -0700184 label, sitd->frame, sitd, hc32_to_cpu(ehci, sitd->hw_next),
185 sitd->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 ehci_dbg (ehci,
David Brownell53bd6a62006-08-30 14:50:06 -0700187 " addr %08x sched %04x result %08x buf %08x %08x\n",
Stefan Roese6dbd6822007-05-01 09:29:37 -0700188 hc32_to_cpu(ehci, sitd->hw_fullspeed_ep),
189 hc32_to_cpu(ehci, sitd->hw_uframe),
190 hc32_to_cpu(ehci, sitd->hw_results),
191 hc32_to_cpu(ehci, sitd->hw_buf[0]),
192 hc32_to_cpu(ehci, sitd->hw_buf[1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
David Rientjes82345092007-05-11 14:39:44 -0700195static int __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196dbg_status_buf (char *buf, unsigned len, const char *label, u32 status)
197{
198 return scnprintf (buf, len,
Alek Duaa4d8342010-06-04 15:47:54 +0800199 "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 label, label [0] ? " " : "", status,
Alek Duaa4d8342010-06-04 15:47:54 +0800201 (status & STS_PPCE_MASK) ? " PPCE" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 (status & STS_ASS) ? " Async" : "",
203 (status & STS_PSS) ? " Periodic" : "",
204 (status & STS_RECL) ? " Recl" : "",
205 (status & STS_HALT) ? " Halt" : "",
206 (status & STS_IAA) ? " IAA" : "",
207 (status & STS_FATAL) ? " FATAL" : "",
208 (status & STS_FLR) ? " FLR" : "",
209 (status & STS_PCD) ? " PCD" : "",
210 (status & STS_ERR) ? " ERR" : "",
211 (status & STS_INT) ? " INT" : ""
212 );
213}
214
David Rientjes82345092007-05-11 14:39:44 -0700215static int __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216dbg_intr_buf (char *buf, unsigned len, const char *label, u32 enable)
217{
218 return scnprintf (buf, len,
Alek Duaa4d8342010-06-04 15:47:54 +0800219 "%s%sintrenable %02x%s%s%s%s%s%s%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 label, label [0] ? " " : "", enable,
Alek Duaa4d8342010-06-04 15:47:54 +0800221 (enable & STS_PPCE_MASK) ? " PPCE" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 (enable & STS_IAA) ? " IAA" : "",
223 (enable & STS_FATAL) ? " FATAL" : "",
224 (enable & STS_FLR) ? " FLR" : "",
225 (enable & STS_PCD) ? " PCD" : "",
226 (enable & STS_ERR) ? " ERR" : "",
227 (enable & STS_INT) ? " INT" : ""
228 );
229}
230
231static const char *const fls_strings [] =
232 { "1024", "512", "256", "??" };
233
234static int
235dbg_command_buf (char *buf, unsigned len, const char *label, u32 command)
236{
237 return scnprintf (buf, len,
Alek Duaa4d8342010-06-04 15:47:54 +0800238 "%s%scommand %07x %s%s%s%s%s%s=%d ithresh=%d%s%s%s%s "
239 "period=%s%s %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 label, label [0] ? " " : "", command,
Alek Duaa4d8342010-06-04 15:47:54 +0800241 (command & CMD_HIRD) ? " HIRD" : "",
242 (command & CMD_PPCEE) ? " PPCEE" : "",
243 (command & CMD_FSP) ? " FSP" : "",
244 (command & CMD_ASPE) ? " ASPE" : "",
245 (command & CMD_PSPE) ? " PSPE" : "",
246 (command & CMD_PARK) ? " park" : "(park)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 CMD_PARK_CNT (command),
248 (command >> 16) & 0x3f,
249 (command & CMD_LRESET) ? " LReset" : "",
250 (command & CMD_IAAD) ? " IAAD" : "",
251 (command & CMD_ASE) ? " Async" : "",
252 (command & CMD_PSE) ? " Periodic" : "",
253 fls_strings [(command >> 2) & 0x3],
254 (command & CMD_RESET) ? " Reset" : "",
255 (command & CMD_RUN) ? "RUN" : "HALT"
256 );
257}
258
259static int
260dbg_port_buf (char *buf, unsigned len, const char *label, int port, u32 status)
261{
262 char *sig;
263
264 /* signaling state */
265 switch (status & (3 << 10)) {
266 case 0 << 10: sig = "se0"; break;
267 case 1 << 10: sig = "k"; break; /* low speed */
268 case 2 << 10: sig = "j"; break;
269 default: sig = "?"; break;
270 }
271
272 return scnprintf (buf, len,
Alek Duaa4d8342010-06-04 15:47:54 +0800273 "%s%sport:%d status %06x %d %s%s%s%s%s%s "
274 "sig=%s%s%s%s%s%s%s%s%s%s%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 label, label [0] ? " " : "", port, status,
Alek Duaa4d8342010-06-04 15:47:54 +0800276 status>>25,/*device address */
277 (status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_ACK ?
278 " ACK" : "",
279 (status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_NYET ?
280 " NYET" : "",
281 (status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_STALL ?
282 " STALL" : "",
283 (status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_ERR ?
284 " ERR" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 (status & PORT_POWER) ? " POWER" : "",
286 (status & PORT_OWNER) ? " OWNER" : "",
287 sig,
Alek Duaa4d8342010-06-04 15:47:54 +0800288 (status & PORT_LPM) ? " LPM" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 (status & PORT_RESET) ? " RESET" : "",
290 (status & PORT_SUSPEND) ? " SUSPEND" : "",
291 (status & PORT_RESUME) ? " RESUME" : "",
292 (status & PORT_OCC) ? " OCC" : "",
293 (status & PORT_OC) ? " OC" : "",
294 (status & PORT_PEC) ? " PEC" : "",
295 (status & PORT_PE) ? " PE" : "",
296 (status & PORT_CSC) ? " CSC" : "",
Stefan Roese6dbd6822007-05-01 09:29:37 -0700297 (status & PORT_CONNECT) ? " CONNECT" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300#else
David Rientjes82345092007-05-11 14:39:44 -0700301static inline void __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302dbg_qh (char *label, struct ehci_hcd *ehci, struct ehci_qh *qh)
303{}
304
David Rientjes82345092007-05-11 14:39:44 -0700305static inline int __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306dbg_status_buf (char *buf, unsigned len, const char *label, u32 status)
307{ return 0; }
308
David Rientjes82345092007-05-11 14:39:44 -0700309static inline int __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310dbg_command_buf (char *buf, unsigned len, const char *label, u32 command)
311{ return 0; }
312
David Rientjes82345092007-05-11 14:39:44 -0700313static inline int __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314dbg_intr_buf (char *buf, unsigned len, const char *label, u32 enable)
315{ return 0; }
316
David Rientjes82345092007-05-11 14:39:44 -0700317static inline int __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318dbg_port_buf (char *buf, unsigned len, const char *label, int port, u32 status)
319{ return 0; }
320
321#endif /* DEBUG */
322
323/* functions have the "wrong" filename when they're output... */
324#define dbg_status(ehci, label, status) { \
325 char _buf [80]; \
326 dbg_status_buf (_buf, sizeof _buf, label, status); \
327 ehci_dbg (ehci, "%s\n", _buf); \
328}
329
330#define dbg_cmd(ehci, label, command) { \
331 char _buf [80]; \
332 dbg_command_buf (_buf, sizeof _buf, label, command); \
333 ehci_dbg (ehci, "%s\n", _buf); \
334}
335
336#define dbg_port(ehci, label, port, status) { \
337 char _buf [80]; \
338 dbg_port_buf (_buf, sizeof _buf, label, port, status); \
339 ehci_dbg (ehci, "%s\n", _buf); \
340}
341
342/*-------------------------------------------------------------------------*/
343
344#ifdef STUB_DEBUG_FILES
345
346static inline void create_debug_files (struct ehci_hcd *bus) { }
347static inline void remove_debug_files (struct ehci_hcd *bus) { }
348
349#else
350
Tony Jones694cc202007-09-11 14:07:31 -0700351/* troubleshooting help: expose state in debugfs */
352
353static int debug_async_open(struct inode *, struct file *);
354static int debug_periodic_open(struct inode *, struct file *);
355static int debug_registers_open(struct inode *, struct file *);
356static int debug_async_open(struct inode *, struct file *);
Alek Duaa4d8342010-06-04 15:47:54 +0800357static int debug_lpm_open(struct inode *, struct file *);
358static ssize_t debug_lpm_read(struct file *file, char __user *user_buf,
359 size_t count, loff_t *ppos);
360static ssize_t debug_lpm_write(struct file *file, const char __user *buffer,
361 size_t count, loff_t *ppos);
362static int debug_lpm_close(struct inode *inode, struct file *file);
363
Tony Jones694cc202007-09-11 14:07:31 -0700364static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
365static int debug_close(struct inode *, struct file *);
366
367static const struct file_operations debug_async_fops = {
368 .owner = THIS_MODULE,
369 .open = debug_async_open,
370 .read = debug_output,
371 .release = debug_close,
372};
373static const struct file_operations debug_periodic_fops = {
374 .owner = THIS_MODULE,
375 .open = debug_periodic_open,
376 .read = debug_output,
377 .release = debug_close,
378};
379static const struct file_operations debug_registers_fops = {
380 .owner = THIS_MODULE,
381 .open = debug_registers_open,
382 .read = debug_output,
383 .release = debug_close,
384};
Alek Duaa4d8342010-06-04 15:47:54 +0800385static const struct file_operations debug_lpm_fops = {
386 .owner = THIS_MODULE,
387 .open = debug_lpm_open,
388 .read = debug_lpm_read,
389 .write = debug_lpm_write,
390 .release = debug_lpm_close,
391};
Tony Jones694cc202007-09-11 14:07:31 -0700392
393static struct dentry *ehci_debug_root;
394
395struct debug_buffer {
396 ssize_t (*fill_func)(struct debug_buffer *); /* fill method */
397 struct usb_bus *bus;
398 struct mutex mutex; /* protect filling of buffer */
399 size_t count; /* number of characters filled into buffer */
Ming Lei3c04e202008-09-18 23:06:21 +0800400 char *output_buf;
401 size_t alloc_size;
Tony Jones694cc202007-09-11 14:07:31 -0700402};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404#define speed_char(info1) ({ char tmp; \
405 switch (info1 & (3 << 12)) { \
406 case 0 << 12: tmp = 'f'; break; \
407 case 1 << 12: tmp = 'l'; break; \
408 case 2 << 12: tmp = 'h'; break; \
409 default: tmp = '?'; break; \
410 }; tmp; })
411
Stefan Roese6dbd6822007-05-01 09:29:37 -0700412static inline char token_mark(struct ehci_hcd *ehci, __hc32 token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700414 __u32 v = hc32_to_cpu(ehci, token);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (v & QTD_STS_ACTIVE)
417 return '*';
418 if (v & QTD_STS_HALT)
419 return '-';
420 if (!IS_SHORT_READ (v))
421 return ' ';
422 /* tries to advance through hw_alt_next */
423 return '/';
424}
425
426static void qh_lines (
427 struct ehci_hcd *ehci,
428 struct ehci_qh *qh,
429 char **nextp,
430 unsigned *sizep
431)
432{
433 u32 scratch;
434 u32 hw_curr;
435 struct list_head *entry;
436 struct ehci_qtd *td;
437 unsigned temp;
438 unsigned size = *sizep;
439 char *next = *nextp;
440 char mark;
Al Virofd05e722008-04-28 07:00:16 +0100441 __le32 list_end = EHCI_LIST_END(ehci);
Alek Du3807e262009-07-14 07:23:29 +0800442 struct ehci_qh_hw *hw = qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Alek Du3807e262009-07-14 07:23:29 +0800444 if (hw->hw_qtd_next == list_end) /* NEC does this */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 mark = '@';
446 else
Alek Du3807e262009-07-14 07:23:29 +0800447 mark = token_mark(ehci, hw->hw_token);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (mark == '/') { /* qh_alt_next controls qh advance? */
Alek Du3807e262009-07-14 07:23:29 +0800449 if ((hw->hw_alt_next & QTD_MASK(ehci))
450 == ehci->async->hw->hw_alt_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 mark = '#'; /* blocked */
Alek Du3807e262009-07-14 07:23:29 +0800452 else if (hw->hw_alt_next == list_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 mark = '.'; /* use hw_qtd_next */
454 /* else alt_next points to some other qtd */
455 }
Alek Du3807e262009-07-14 07:23:29 +0800456 scratch = hc32_to_cpup(ehci, &hw->hw_info1);
457 hw_curr = (mark == '*') ? hc32_to_cpup(ehci, &hw->hw_current) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 temp = scnprintf (next, size,
459 "qh/%p dev%d %cs ep%d %08x %08x (%08x%c %s nak%d)",
460 qh, scratch & 0x007f,
461 speed_char (scratch),
462 (scratch >> 8) & 0x000f,
Alek Du3807e262009-07-14 07:23:29 +0800463 scratch, hc32_to_cpup(ehci, &hw->hw_info2),
464 hc32_to_cpup(ehci, &hw->hw_token), mark,
465 (cpu_to_hc32(ehci, QTD_TOGGLE) & hw->hw_token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 ? "data1" : "data0",
Alek Du3807e262009-07-14 07:23:29 +0800467 (hc32_to_cpup(ehci, &hw->hw_alt_next) >> 1) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 size -= temp;
469 next += temp;
470
471 /* hc may be modifying the list as we read it ... */
472 list_for_each (entry, &qh->qtd_list) {
473 td = list_entry (entry, struct ehci_qtd, qtd_list);
Stefan Roese6dbd6822007-05-01 09:29:37 -0700474 scratch = hc32_to_cpup(ehci, &td->hw_token);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 mark = ' ';
476 if (hw_curr == td->qtd_dma)
477 mark = '*';
Alek Du3807e262009-07-14 07:23:29 +0800478 else if (hw->hw_qtd_next == cpu_to_hc32(ehci, td->qtd_dma))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 mark = '+';
480 else if (QTD_LENGTH (scratch)) {
Alek Du3807e262009-07-14 07:23:29 +0800481 if (td->hw_alt_next == ehci->async->hw->hw_alt_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 mark = '#';
Stefan Roese6dbd6822007-05-01 09:29:37 -0700483 else if (td->hw_alt_next != list_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 mark = '/';
485 }
486 temp = snprintf (next, size,
487 "\n\t%p%c%s len=%d %08x urb %p",
488 td, mark, ({ char *tmp;
489 switch ((scratch>>8)&0x03) {
490 case 0: tmp = "out"; break;
491 case 1: tmp = "in"; break;
492 case 2: tmp = "setup"; break;
493 default: tmp = "?"; break;
494 } tmp;}),
495 (scratch >> 16) & 0x7fff,
496 scratch,
497 td->urb);
roel kluine64a5212008-10-21 00:47:23 -0400498 if (size < temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 temp = size;
500 size -= temp;
501 next += temp;
502 if (temp == size)
503 goto done;
504 }
505
506 temp = snprintf (next, size, "\n");
roel kluine64a5212008-10-21 00:47:23 -0400507 if (size < temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 temp = size;
509 size -= temp;
510 next += temp;
511
512done:
513 *sizep = size;
514 *nextp = next;
515}
516
Tony Jones694cc202007-09-11 14:07:31 -0700517static ssize_t fill_async_buffer(struct debug_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct usb_hcd *hcd;
520 struct ehci_hcd *ehci;
521 unsigned long flags;
522 unsigned temp, size;
523 char *next;
524 struct ehci_qh *qh;
525
Tony Jones694cc202007-09-11 14:07:31 -0700526 hcd = bus_to_hcd(buf->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 ehci = hcd_to_ehci (hcd);
Ming Lei3c04e202008-09-18 23:06:21 +0800528 next = buf->output_buf;
529 size = buf->alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Tony Jones694cc202007-09-11 14:07:31 -0700531 *next = 0;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* dumps a snapshot of the async schedule.
534 * usually empty except for long-term bulk reads, or head.
535 * one QH per line, and TDs we know about
536 */
537 spin_lock_irqsave (&ehci->lock, flags);
538 for (qh = ehci->async->qh_next.qh; size > 0 && qh; qh = qh->qh_next.qh)
539 qh_lines (ehci, qh, &next, &size);
540 if (ehci->reclaim && size > 0) {
541 temp = scnprintf (next, size, "\nreclaim =\n");
542 size -= temp;
543 next += temp;
544
545 for (qh = ehci->reclaim; size > 0 && qh; qh = qh->reclaim)
546 qh_lines (ehci, qh, &next, &size);
547 }
548 spin_unlock_irqrestore (&ehci->lock, flags);
549
Ming Lei3c04e202008-09-18 23:06:21 +0800550 return strlen(buf->output_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553#define DBG_SCHED_LIMIT 64
Tony Jones694cc202007-09-11 14:07:31 -0700554static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct usb_hcd *hcd;
557 struct ehci_hcd *ehci;
558 unsigned long flags;
559 union ehci_shadow p, *seen;
560 unsigned temp, size, seen_count;
561 char *next;
562 unsigned i;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700563 __hc32 tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800565 if (!(seen = kmalloc (DBG_SCHED_LIMIT * sizeof *seen, GFP_ATOMIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return 0;
567 seen_count = 0;
568
Tony Jones694cc202007-09-11 14:07:31 -0700569 hcd = bus_to_hcd(buf->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 ehci = hcd_to_ehci (hcd);
Ming Lei3c04e202008-09-18 23:06:21 +0800571 next = buf->output_buf;
572 size = buf->alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 temp = scnprintf (next, size, "size = %d\n", ehci->periodic_size);
575 size -= temp;
576 next += temp;
577
578 /* dump a snapshot of the periodic schedule.
579 * iso changes, interrupt usually doesn't.
580 */
581 spin_lock_irqsave (&ehci->lock, flags);
582 for (i = 0; i < ehci->periodic_size; i++) {
583 p = ehci->pshadow [i];
584 if (likely (!p.ptr))
585 continue;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700586 tag = Q_NEXT_TYPE(ehci, ehci->periodic [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 temp = scnprintf (next, size, "%4d: ", i);
589 size -= temp;
590 next += temp;
591
592 do {
Alek Du3807e262009-07-14 07:23:29 +0800593 struct ehci_qh_hw *hw;
594
Stefan Roese6dbd6822007-05-01 09:29:37 -0700595 switch (hc32_to_cpu(ehci, tag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 case Q_TYPE_QH:
Alek Du3807e262009-07-14 07:23:29 +0800597 hw = p.qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 temp = scnprintf (next, size, " qh%d-%04x/%p",
599 p.qh->period,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700600 hc32_to_cpup(ehci,
Alek Du3807e262009-07-14 07:23:29 +0800601 &hw->hw_info2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* uframe masks */
David Brownell7dedacf2005-08-04 18:06:41 -0700603 & (QH_CMASK | QH_SMASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 p.qh);
605 size -= temp;
606 next += temp;
607 /* don't repeat what follows this qh */
608 for (temp = 0; temp < seen_count; temp++) {
609 if (seen [temp].ptr != p.ptr)
610 continue;
Ming Lei3c4bb712008-09-18 23:06:38 +0800611 if (p.qh->qh_next.ptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 temp = scnprintf (next, size,
613 " ...");
Ming Lei3c4bb712008-09-18 23:06:38 +0800614 size -= temp;
615 next += temp;
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
618 }
619 /* show more info the first time around */
Ming Lei3c4bb712008-09-18 23:06:38 +0800620 if (temp == seen_count) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700621 u32 scratch = hc32_to_cpup(ehci,
Alek Du3807e262009-07-14 07:23:29 +0800622 &hw->hw_info1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct ehci_qtd *qtd;
624 char *type = "";
625
626 /* count tds, get ep direction */
627 temp = 0;
628 list_for_each_entry (qtd,
629 &p.qh->qtd_list,
630 qtd_list) {
631 temp++;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700632 switch (0x03 & (hc32_to_cpu(
633 ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 qtd->hw_token) >> 8)) {
635 case 0: type = "out"; continue;
636 case 1: type = "in"; continue;
637 }
638 }
639
640 temp = scnprintf (next, size,
641 " (%c%d ep%d%s "
642 "[%d/%d] q%d p%d)",
643 speed_char (scratch),
644 scratch & 0x007f,
645 (scratch >> 8) & 0x000f, type,
646 p.qh->usecs, p.qh->c_usecs,
647 temp,
648 0x7ff & (scratch >> 16));
649
650 if (seen_count < DBG_SCHED_LIMIT)
651 seen [seen_count++].qh = p.qh;
652 } else
653 temp = 0;
654 if (p.qh) {
Alek Du3807e262009-07-14 07:23:29 +0800655 tag = Q_NEXT_TYPE(ehci, hw->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 p = p.qh->qh_next;
657 }
658 break;
659 case Q_TYPE_FSTN:
660 temp = scnprintf (next, size,
661 " fstn-%8x/%p", p.fstn->hw_prev,
662 p.fstn);
Stefan Roese6dbd6822007-05-01 09:29:37 -0700663 tag = Q_NEXT_TYPE(ehci, p.fstn->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 p = p.fstn->fstn_next;
665 break;
666 case Q_TYPE_ITD:
667 temp = scnprintf (next, size,
668 " itd/%p", p.itd);
Stefan Roese6dbd6822007-05-01 09:29:37 -0700669 tag = Q_NEXT_TYPE(ehci, p.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 p = p.itd->itd_next;
671 break;
672 case Q_TYPE_SITD:
673 temp = scnprintf (next, size,
674 " sitd%d-%04x/%p",
675 p.sitd->stream->interval,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700676 hc32_to_cpup(ehci, &p.sitd->hw_uframe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 & 0x0000ffff,
678 p.sitd);
Stefan Roese6dbd6822007-05-01 09:29:37 -0700679 tag = Q_NEXT_TYPE(ehci, p.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 p = p.sitd->sitd_next;
681 break;
682 }
683 size -= temp;
684 next += temp;
685 } while (p.ptr);
686
687 temp = scnprintf (next, size, "\n");
688 size -= temp;
689 next += temp;
690 }
691 spin_unlock_irqrestore (&ehci->lock, flags);
692 kfree (seen);
693
Ming Lei3c04e202008-09-18 23:06:21 +0800694 return buf->alloc_size - size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696#undef DBG_SCHED_LIMIT
697
Tony Jones694cc202007-09-11 14:07:31 -0700698static ssize_t fill_registers_buffer(struct debug_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct usb_hcd *hcd;
701 struct ehci_hcd *ehci;
702 unsigned long flags;
703 unsigned temp, size, i;
704 char *next, scratch [80];
705 static char fmt [] = "%*s\n";
706 static char label [] = "";
707
Tony Jones694cc202007-09-11 14:07:31 -0700708 hcd = bus_to_hcd(buf->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 ehci = hcd_to_ehci (hcd);
Ming Lei3c04e202008-09-18 23:06:21 +0800710 next = buf->output_buf;
711 size = buf->alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 spin_lock_irqsave (&ehci->lock, flags);
714
Alan Stern541c7d42010-06-22 16:39:10 -0400715 if (!HCD_HW_ACCESSIBLE(hcd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 size = scnprintf (next, size,
Alan Stern2b70f072008-10-02 11:47:15 -0400717 "bus %s, device %s\n"
David Brownelld49d4312005-05-07 13:21:50 -0700718 "%s\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 "SUSPENDED (no register access)\n",
720 hcd->self.controller->bus->name,
Kay Sievers7071a3c2008-05-02 06:02:41 +0200721 dev_name(hcd->self.controller),
David Brownelld49d4312005-05-07 13:21:50 -0700722 hcd->product_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 goto done;
724 }
725
726 /* Capability Registers */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100727 i = HC_VERSION(ehci_readl(ehci, &ehci->caps->hc_capbase));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 temp = scnprintf (next, size,
Alan Stern2b70f072008-10-02 11:47:15 -0400729 "bus %s, device %s\n"
David Brownelld49d4312005-05-07 13:21:50 -0700730 "%s\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 "EHCI %x.%02x, hcd state %d\n",
732 hcd->self.controller->bus->name,
Kay Sievers7071a3c2008-05-02 06:02:41 +0200733 dev_name(hcd->self.controller),
David Brownelld49d4312005-05-07 13:21:50 -0700734 hcd->product_desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 i >> 8, i & 0x0ff, hcd->state);
736 size -= temp;
737 next += temp;
738
David Brownelld49d4312005-05-07 13:21:50 -0700739#ifdef CONFIG_PCI
740 /* EHCI 0.96 and later may have "extended capabilities" */
741 if (hcd->self.controller->bus == &pci_bus_type) {
742 struct pci_dev *pdev;
743 u32 offset, cap, cap2;
744 unsigned count = 256/4;
745
746 pdev = to_pci_dev(ehci_to_hcd(ehci)->self.controller);
Stefan Roese6dbd6822007-05-01 09:29:37 -0700747 offset = HCC_EXT_CAPS(ehci_readl(ehci,
748 &ehci->caps->hcc_params));
David Brownelld49d4312005-05-07 13:21:50 -0700749 while (offset && count--) {
750 pci_read_config_dword (pdev, offset, &cap);
751 switch (cap & 0xff) {
752 case 1:
753 temp = scnprintf (next, size,
754 "ownership %08x%s%s\n", cap,
755 (cap & (1 << 24)) ? " linux" : "",
756 (cap & (1 << 16)) ? " firmware" : "");
757 size -= temp;
758 next += temp;
759
760 offset += 4;
761 pci_read_config_dword (pdev, offset, &cap2);
762 temp = scnprintf (next, size,
763 "SMI sts/enable 0x%08x\n", cap2);
764 size -= temp;
765 next += temp;
766 break;
767 case 0: /* illegal reserved capability */
768 cap = 0;
769 /* FALLTHROUGH */
770 default: /* unknown */
771 break;
772 }
773 temp = (cap >> 8) & 0xff;
774 }
775 }
776#endif
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 // FIXME interpret both types of params
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100779 i = ehci_readl(ehci, &ehci->caps->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 temp = scnprintf (next, size, "structural params 0x%08x\n", i);
781 size -= temp;
782 next += temp;
783
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100784 i = ehci_readl(ehci, &ehci->caps->hcc_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 temp = scnprintf (next, size, "capability params 0x%08x\n", i);
786 size -= temp;
787 next += temp;
788
789 /* Operational Registers */
790 temp = dbg_status_buf (scratch, sizeof scratch, label,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100791 ehci_readl(ehci, &ehci->regs->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 temp = scnprintf (next, size, fmt, temp, scratch);
793 size -= temp;
794 next += temp;
795
796 temp = dbg_command_buf (scratch, sizeof scratch, label,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100797 ehci_readl(ehci, &ehci->regs->command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 temp = scnprintf (next, size, fmt, temp, scratch);
799 size -= temp;
800 next += temp;
801
802 temp = dbg_intr_buf (scratch, sizeof scratch, label,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100803 ehci_readl(ehci, &ehci->regs->intr_enable));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 temp = scnprintf (next, size, fmt, temp, scratch);
805 size -= temp;
806 next += temp;
807
808 temp = scnprintf (next, size, "uframe %04x\n",
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100809 ehci_readl(ehci, &ehci->regs->frame_index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 size -= temp;
811 next += temp;
812
David Brownelld49d4312005-05-07 13:21:50 -0700813 for (i = 1; i <= HCS_N_PORTS (ehci->hcs_params); i++) {
814 temp = dbg_port_buf (scratch, sizeof scratch, label, i,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700815 ehci_readl(ehci,
816 &ehci->regs->port_status[i - 1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 temp = scnprintf (next, size, fmt, temp, scratch);
818 size -= temp;
819 next += temp;
David Brownelld49d4312005-05-07 13:21:50 -0700820 if (i == HCS_DEBUG_PORT(ehci->hcs_params) && ehci->debug) {
821 temp = scnprintf (next, size,
822 " debug control %08x\n",
Stefan Roese6dbd6822007-05-01 09:29:37 -0700823 ehci_readl(ehci,
824 &ehci->debug->control));
David Brownelld49d4312005-05-07 13:21:50 -0700825 size -= temp;
826 next += temp;
827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
830 if (ehci->reclaim) {
Alan Stern07d29b62007-12-11 16:05:30 -0500831 temp = scnprintf(next, size, "reclaim qh %p\n", ehci->reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 size -= temp;
833 next += temp;
834 }
835
836#ifdef EHCI_STATS
837 temp = scnprintf (next, size,
838 "irq normal %ld err %ld reclaim %ld (lost %ld)\n",
839 ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
840 ehci->stats.lost_iaa);
841 size -= temp;
842 next += temp;
843
844 temp = scnprintf (next, size, "complete %ld unlink %ld\n",
845 ehci->stats.complete, ehci->stats.unlink);
846 size -= temp;
847 next += temp;
848#endif
849
850done:
851 spin_unlock_irqrestore (&ehci->lock, flags);
852
Ming Lei3c04e202008-09-18 23:06:21 +0800853 return buf->alloc_size - size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
Tony Jones694cc202007-09-11 14:07:31 -0700855
856static struct debug_buffer *alloc_buffer(struct usb_bus *bus,
857 ssize_t (*fill_func)(struct debug_buffer *))
858{
859 struct debug_buffer *buf;
860
861 buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
862
863 if (buf) {
864 buf->bus = bus;
865 buf->fill_func = fill_func;
866 mutex_init(&buf->mutex);
Ming Lei3c04e202008-09-18 23:06:21 +0800867 buf->alloc_size = PAGE_SIZE;
Tony Jones694cc202007-09-11 14:07:31 -0700868 }
869
870 return buf;
871}
872
873static int fill_buffer(struct debug_buffer *buf)
874{
875 int ret = 0;
876
Ming Lei3c04e202008-09-18 23:06:21 +0800877 if (!buf->output_buf)
878 buf->output_buf = (char *)vmalloc(buf->alloc_size);
Tony Jones694cc202007-09-11 14:07:31 -0700879
Ming Lei3c04e202008-09-18 23:06:21 +0800880 if (!buf->output_buf) {
Tony Jones694cc202007-09-11 14:07:31 -0700881 ret = -ENOMEM;
882 goto out;
883 }
884
885 ret = buf->fill_func(buf);
886
887 if (ret >= 0) {
888 buf->count = ret;
889 ret = 0;
890 }
891
892out:
893 return ret;
894}
895
896static ssize_t debug_output(struct file *file, char __user *user_buf,
897 size_t len, loff_t *offset)
898{
899 struct debug_buffer *buf = file->private_data;
900 int ret = 0;
901
902 mutex_lock(&buf->mutex);
903 if (buf->count == 0) {
904 ret = fill_buffer(buf);
905 if (ret != 0) {
906 mutex_unlock(&buf->mutex);
907 goto out;
908 }
909 }
910 mutex_unlock(&buf->mutex);
911
912 ret = simple_read_from_buffer(user_buf, len, offset,
Ming Lei3c04e202008-09-18 23:06:21 +0800913 buf->output_buf, buf->count);
Tony Jones694cc202007-09-11 14:07:31 -0700914
915out:
916 return ret;
917
918}
919
920static int debug_close(struct inode *inode, struct file *file)
921{
922 struct debug_buffer *buf = file->private_data;
923
924 if (buf) {
Figo.zhangf8086a02009-06-06 20:31:49 +0800925 vfree(buf->output_buf);
Tony Jones694cc202007-09-11 14:07:31 -0700926 kfree(buf);
927 }
928
929 return 0;
930}
931static int debug_async_open(struct inode *inode, struct file *file)
932{
933 file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
934
935 return file->private_data ? 0 : -ENOMEM;
936}
937
938static int debug_periodic_open(struct inode *inode, struct file *file)
939{
Ming Lei3c04e202008-09-18 23:06:21 +0800940 struct debug_buffer *buf;
941 buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
942 if (!buf)
943 return -ENOMEM;
Tony Jones694cc202007-09-11 14:07:31 -0700944
Ming Lei3c04e202008-09-18 23:06:21 +0800945 buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
946 file->private_data = buf;
947 return 0;
Tony Jones694cc202007-09-11 14:07:31 -0700948}
949
950static int debug_registers_open(struct inode *inode, struct file *file)
951{
952 file->private_data = alloc_buffer(inode->i_private,
953 fill_registers_buffer);
954
955 return file->private_data ? 0 : -ENOMEM;
956}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Alek Duaa4d8342010-06-04 15:47:54 +0800958static int debug_lpm_open(struct inode *inode, struct file *file)
959{
960 file->private_data = inode->i_private;
961 return 0;
962}
963
964static int debug_lpm_close(struct inode *inode, struct file *file)
965{
966 return 0;
967}
968
969static ssize_t debug_lpm_read(struct file *file, char __user *user_buf,
970 size_t count, loff_t *ppos)
971{
972 /* TODO: show lpm stats */
973 return 0;
974}
975
976static ssize_t debug_lpm_write(struct file *file, const char __user *user_buf,
977 size_t count, loff_t *ppos)
978{
979 struct usb_hcd *hcd;
980 struct ehci_hcd *ehci;
981 char buf[50];
982 size_t len;
983 u32 temp;
984 unsigned long port;
985 u32 __iomem *portsc ;
986 u32 params;
987
988 hcd = bus_to_hcd(file->private_data);
989 ehci = hcd_to_ehci(hcd);
990
991 len = min(count, sizeof(buf) - 1);
992 if (copy_from_user(buf, user_buf, len))
993 return -EFAULT;
994 buf[len] = '\0';
995 if (len > 0 && buf[len - 1] == '\n')
996 buf[len - 1] = '\0';
997
998 if (strncmp(buf, "enable", 5) == 0) {
999 if (strict_strtoul(buf + 7, 10, &port))
1000 return -EINVAL;
1001 params = ehci_readl(ehci, &ehci->caps->hcs_params);
1002 if (port > HCS_N_PORTS(params)) {
1003 ehci_dbg(ehci, "ERR: LPM on bad port %lu\n", port);
1004 return -ENODEV;
1005 }
1006 portsc = &ehci->regs->port_status[port-1];
1007 temp = ehci_readl(ehci, portsc);
1008 if (!(temp & PORT_DEV_ADDR)) {
1009 ehci_dbg(ehci, "LPM: no device attached\n");
1010 return -ENODEV;
1011 }
1012 temp |= PORT_LPM;
1013 ehci_writel(ehci, temp, portsc);
1014 printk(KERN_INFO "force enable LPM for port %lu\n", port);
1015 } else if (strncmp(buf, "hird=", 5) == 0) {
1016 unsigned long hird;
1017 if (strict_strtoul(buf + 5, 16, &hird))
1018 return -EINVAL;
1019 printk(KERN_INFO "setting hird %s %lu\n", buf + 6, hird);
1020 temp = ehci_readl(ehci, &ehci->regs->command);
1021 temp &= ~CMD_HIRD;
1022 temp |= hird << 24;
1023 ehci_writel(ehci, temp, &ehci->regs->command);
1024 } else if (strncmp(buf, "disable", 7) == 0) {
1025 if (strict_strtoul(buf + 8, 10, &port))
1026 return -EINVAL;
1027 params = ehci_readl(ehci, &ehci->caps->hcs_params);
1028 if (port > HCS_N_PORTS(params)) {
1029 ehci_dbg(ehci, "ERR: LPM off bad port %lu\n", port);
1030 return -ENODEV;
1031 }
1032 portsc = &ehci->regs->port_status[port-1];
1033 temp = ehci_readl(ehci, portsc);
1034 if (!(temp & PORT_DEV_ADDR)) {
1035 ehci_dbg(ehci, "ERR: no device attached\n");
1036 return -ENODEV;
1037 }
1038 temp &= ~PORT_LPM;
1039 ehci_writel(ehci, temp, portsc);
1040 printk(KERN_INFO "disabled LPM for port %lu\n", port);
1041 } else
1042 return -EOPNOTSUPP;
1043 return count;
1044}
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046static inline void create_debug_files (struct ehci_hcd *ehci)
1047{
Tony Jones694cc202007-09-11 14:07:31 -07001048 struct usb_bus *bus = &ehci_to_hcd(ehci)->self;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Tony Jones694cc202007-09-11 14:07:31 -07001050 ehci->debug_dir = debugfs_create_dir(bus->bus_name, ehci_debug_root);
1051 if (!ehci->debug_dir)
1052 goto dir_error;
1053
1054 ehci->debug_async = debugfs_create_file("async", S_IRUGO,
1055 ehci->debug_dir, bus,
1056 &debug_async_fops);
1057 if (!ehci->debug_async)
1058 goto async_error;
1059
1060 ehci->debug_periodic = debugfs_create_file("periodic", S_IRUGO,
1061 ehci->debug_dir, bus,
1062 &debug_periodic_fops);
1063 if (!ehci->debug_periodic)
1064 goto periodic_error;
1065
1066 ehci->debug_registers = debugfs_create_file("registers", S_IRUGO,
1067 ehci->debug_dir, bus,
1068 &debug_registers_fops);
Alek Duaa4d8342010-06-04 15:47:54 +08001069
1070 ehci->debug_registers = debugfs_create_file("lpm", S_IRUGO|S_IWUGO,
1071 ehci->debug_dir, bus,
1072 &debug_lpm_fops);
Tony Jones694cc202007-09-11 14:07:31 -07001073 if (!ehci->debug_registers)
1074 goto registers_error;
1075 return;
1076
1077registers_error:
1078 debugfs_remove(ehci->debug_periodic);
1079periodic_error:
1080 debugfs_remove(ehci->debug_async);
1081async_error:
1082 debugfs_remove(ehci->debug_dir);
1083dir_error:
1084 ehci->debug_periodic = NULL;
1085 ehci->debug_async = NULL;
1086 ehci->debug_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089static inline void remove_debug_files (struct ehci_hcd *ehci)
1090{
Tony Jones694cc202007-09-11 14:07:31 -07001091 debugfs_remove(ehci->debug_registers);
1092 debugfs_remove(ehci->debug_periodic);
1093 debugfs_remove(ehci->debug_async);
1094 debugfs_remove(ehci->debug_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097#endif /* STUB_DEBUG_FILES */