1 /*
2  * Copyright (c) 2009 by Martin Fuzzey
3  *
4  * 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 imx21-hcd.c */
20 
21 #ifndef DEBUG
22 
create_debug_files(struct imx21 * imx21)23 static inline void create_debug_files(struct imx21 *imx21) { }
remove_debug_files(struct imx21 * imx21)24 static inline void remove_debug_files(struct imx21 *imx21) { }
debug_urb_submitted(struct imx21 * imx21,struct urb * urb)25 static inline void debug_urb_submitted(struct imx21 *imx21, struct urb *urb) {}
debug_urb_completed(struct imx21 * imx21,struct urb * urb,int status)26 static inline void debug_urb_completed(struct imx21 *imx21, struct urb *urb,
27 	int status) {}
debug_urb_unlinked(struct imx21 * imx21,struct urb * urb)28 static inline void debug_urb_unlinked(struct imx21 *imx21, struct urb *urb) {}
debug_urb_queued_for_etd(struct imx21 * imx21,struct urb * urb)29 static inline void debug_urb_queued_for_etd(struct imx21 *imx21,
30 	struct urb *urb) {}
debug_urb_queued_for_dmem(struct imx21 * imx21,struct urb * urb)31 static inline void debug_urb_queued_for_dmem(struct imx21 *imx21,
32 	struct urb *urb) {}
debug_etd_allocated(struct imx21 * imx21)33 static inline void debug_etd_allocated(struct imx21 *imx21) {}
debug_etd_freed(struct imx21 * imx21)34 static inline void debug_etd_freed(struct imx21 *imx21) {}
debug_dmem_allocated(struct imx21 * imx21,int size)35 static inline void debug_dmem_allocated(struct imx21 *imx21, int size) {}
debug_dmem_freed(struct imx21 * imx21,int size)36 static inline void debug_dmem_freed(struct imx21 *imx21, int size) {}
debug_isoc_submitted(struct imx21 * imx21,int frame,struct td * td)37 static inline void debug_isoc_submitted(struct imx21 *imx21,
38 	int frame, struct td *td) {}
debug_isoc_completed(struct imx21 * imx21,int frame,struct td * td,int cc,int len)39 static inline void debug_isoc_completed(struct imx21 *imx21,
40 	int frame, struct td *td, int cc, int len) {}
41 
42 #else
43 
44 #include <linux/debugfs.h>
45 #include <linux/seq_file.h>
46 
47 static const char *dir_labels[] = {
48 	"TD 0",
49 	"OUT",
50 	"IN",
51 	"TD 1"
52 };
53 
54 static const char *speed_labels[] = {
55 	"Full",
56 	"Low"
57 };
58 
59 static const char *format_labels[] = {
60 	"Control",
61 	"ISO",
62 	"Bulk",
63 	"Interrupt"
64 };
65 
stats_for_urb(struct imx21 * imx21,struct urb * urb)66 static inline struct debug_stats *stats_for_urb(struct imx21 *imx21,
67 	struct urb *urb)
68 {
69 	return usb_pipeisoc(urb->pipe) ?
70 		&imx21->isoc_stats : &imx21->nonisoc_stats;
71 }
72 
debug_urb_submitted(struct imx21 * imx21,struct urb * urb)73 static void debug_urb_submitted(struct imx21 *imx21, struct urb *urb)
74 {
75 	stats_for_urb(imx21, urb)->submitted++;
76 }
77 
debug_urb_completed(struct imx21 * imx21,struct urb * urb,int st)78 static void debug_urb_completed(struct imx21 *imx21, struct urb *urb, int st)
79 {
80 	if (st)
81 		stats_for_urb(imx21, urb)->completed_failed++;
82 	else
83 		stats_for_urb(imx21, urb)->completed_ok++;
84 }
85 
debug_urb_unlinked(struct imx21 * imx21,struct urb * urb)86 static void debug_urb_unlinked(struct imx21 *imx21, struct urb *urb)
87 {
88 	stats_for_urb(imx21, urb)->unlinked++;
89 }
90 
debug_urb_queued_for_etd(struct imx21 * imx21,struct urb * urb)91 static void debug_urb_queued_for_etd(struct imx21 *imx21, struct urb *urb)
92 {
93 	stats_for_urb(imx21, urb)->queue_etd++;
94 }
95 
debug_urb_queued_for_dmem(struct imx21 * imx21,struct urb * urb)96 static void debug_urb_queued_for_dmem(struct imx21 *imx21, struct urb *urb)
97 {
98 	stats_for_urb(imx21, urb)->queue_dmem++;
99 }
100 
debug_etd_allocated(struct imx21 * imx21)101 static inline void debug_etd_allocated(struct imx21 *imx21)
102 {
103 	imx21->etd_usage.maximum = max(
104 			++(imx21->etd_usage.value),
105 			imx21->etd_usage.maximum);
106 }
107 
debug_etd_freed(struct imx21 * imx21)108 static inline void debug_etd_freed(struct imx21 *imx21)
109 {
110 	imx21->etd_usage.value--;
111 }
112 
debug_dmem_allocated(struct imx21 * imx21,int size)113 static inline void debug_dmem_allocated(struct imx21 *imx21, int size)
114 {
115 	imx21->dmem_usage.value += size;
116 	imx21->dmem_usage.maximum = max(
117 			imx21->dmem_usage.value,
118 			imx21->dmem_usage.maximum);
119 }
120 
debug_dmem_freed(struct imx21 * imx21,int size)121 static inline void debug_dmem_freed(struct imx21 *imx21, int size)
122 {
123 	imx21->dmem_usage.value -= size;
124 }
125 
126 
debug_isoc_submitted(struct imx21 * imx21,int frame,struct td * td)127 static void debug_isoc_submitted(struct imx21 *imx21,
128 	int frame, struct td *td)
129 {
130 	struct debug_isoc_trace *trace = &imx21->isoc_trace[
131 		imx21->isoc_trace_index++];
132 
133 	imx21->isoc_trace_index %= ARRAY_SIZE(imx21->isoc_trace);
134 	trace->schedule_frame = td->frame;
135 	trace->submit_frame = frame;
136 	trace->request_len = td->len;
137 	trace->td = td;
138 }
139 
debug_isoc_completed(struct imx21 * imx21,int frame,struct td * td,int cc,int len)140 static inline void debug_isoc_completed(struct imx21 *imx21,
141 	int frame, struct td *td, int cc, int len)
142 {
143 	struct debug_isoc_trace *trace, *trace_failed;
144 	int i;
145 	int found = 0;
146 
147 	trace = imx21->isoc_trace;
148 	for (i = 0; i < ARRAY_SIZE(imx21->isoc_trace); i++, trace++) {
149 		if (trace->td == td) {
150 			trace->done_frame = frame;
151 			trace->done_len = len;
152 			trace->cc = cc;
153 			trace->td = NULL;
154 			found = 1;
155 			break;
156 		}
157 	}
158 
159 	if (found && cc) {
160 		trace_failed = &imx21->isoc_trace_failed[
161 					imx21->isoc_trace_index_failed++];
162 
163 		imx21->isoc_trace_index_failed %= ARRAY_SIZE(
164 						imx21->isoc_trace_failed);
165 		*trace_failed = *trace;
166 	}
167 }
168 
169 
format_ep(struct usb_host_endpoint * ep,char * buf,int bufsize)170 static char *format_ep(struct usb_host_endpoint *ep, char *buf, int bufsize)
171 {
172 	if (ep)
173 		snprintf(buf, bufsize, "ep_%02x (type:%02X kaddr:%p)",
174 			ep->desc.bEndpointAddress,
175 			usb_endpoint_type(&ep->desc),
176 			ep);
177 	else
178 		snprintf(buf, bufsize, "none");
179 	return buf;
180 }
181 
format_etd_dword0(u32 value,char * buf,int bufsize)182 static char *format_etd_dword0(u32 value, char *buf, int bufsize)
183 {
184 	snprintf(buf, bufsize,
185 		"addr=%d ep=%d dir=%s speed=%s format=%s halted=%d",
186 		value & 0x7F,
187 		(value >> DW0_ENDPNT) & 0x0F,
188 		dir_labels[(value >> DW0_DIRECT) & 0x03],
189 		speed_labels[(value >> DW0_SPEED) & 0x01],
190 		format_labels[(value >> DW0_FORMAT) & 0x03],
191 		(value >> DW0_HALTED) & 0x01);
192 	return buf;
193 }
194 
debug_status_show(struct seq_file * s,void * v)195 static int debug_status_show(struct seq_file *s, void *v)
196 {
197 	struct imx21 *imx21 = s->private;
198 	int etds_allocated = 0;
199 	int etds_sw_busy = 0;
200 	int etds_hw_busy = 0;
201 	int dmem_blocks = 0;
202 	int queued_for_etd = 0;
203 	int queued_for_dmem = 0;
204 	unsigned int dmem_bytes = 0;
205 	int i;
206 	struct etd_priv *etd;
207 	u32 etd_enable_mask;
208 	unsigned long flags;
209 	struct imx21_dmem_area *dmem;
210 	struct ep_priv *ep_priv;
211 
212 	spin_lock_irqsave(&imx21->lock, flags);
213 
214 	etd_enable_mask = readl(imx21->regs + USBH_ETDENSET);
215 	for (i = 0, etd = imx21->etd; i < USB_NUM_ETD; i++, etd++) {
216 		if (etd->alloc)
217 			etds_allocated++;
218 		if (etd->urb)
219 			etds_sw_busy++;
220 		if (etd_enable_mask & (1<<i))
221 			etds_hw_busy++;
222 	}
223 
224 	list_for_each_entry(dmem, &imx21->dmem_list, list) {
225 		dmem_bytes += dmem->size;
226 		dmem_blocks++;
227 	}
228 
229 	list_for_each_entry(ep_priv, &imx21->queue_for_etd, queue)
230 		queued_for_etd++;
231 
232 	list_for_each_entry(etd, &imx21->queue_for_dmem, queue)
233 		queued_for_dmem++;
234 
235 	spin_unlock_irqrestore(&imx21->lock, flags);
236 
237 	seq_printf(s,
238 		"Frame: %d\n"
239 		"ETDs allocated: %d/%d (max=%d)\n"
240 		"ETDs in use sw: %d\n"
241 		"ETDs in use hw: %d\n"
242 		"DMEM alocated: %d/%d (max=%d)\n"
243 		"DMEM blocks: %d\n"
244 		"Queued waiting for ETD: %d\n"
245 		"Queued waiting for DMEM: %d\n",
246 		readl(imx21->regs + USBH_FRMNUB) & 0xFFFF,
247 		etds_allocated, USB_NUM_ETD, imx21->etd_usage.maximum,
248 		etds_sw_busy,
249 		etds_hw_busy,
250 		dmem_bytes, DMEM_SIZE, imx21->dmem_usage.maximum,
251 		dmem_blocks,
252 		queued_for_etd,
253 		queued_for_dmem);
254 
255 	return 0;
256 }
257 
debug_dmem_show(struct seq_file * s,void * v)258 static int debug_dmem_show(struct seq_file *s, void *v)
259 {
260 	struct imx21 *imx21 = s->private;
261 	struct imx21_dmem_area *dmem;
262 	unsigned long flags;
263 	char ep_text[40];
264 
265 	spin_lock_irqsave(&imx21->lock, flags);
266 
267 	list_for_each_entry(dmem, &imx21->dmem_list, list)
268 		seq_printf(s,
269 			"%04X: size=0x%X "
270 			"ep=%s\n",
271 			dmem->offset, dmem->size,
272 			format_ep(dmem->ep, ep_text, sizeof(ep_text)));
273 
274 	spin_unlock_irqrestore(&imx21->lock, flags);
275 
276 	return 0;
277 }
278 
debug_etd_show(struct seq_file * s,void * v)279 static int debug_etd_show(struct seq_file *s, void *v)
280 {
281 	struct imx21 *imx21 = s->private;
282 	struct etd_priv *etd;
283 	char buf[60];
284 	u32 dword;
285 	int i, j;
286 	unsigned long flags;
287 
288 	spin_lock_irqsave(&imx21->lock, flags);
289 
290 	for (i = 0, etd = imx21->etd; i < USB_NUM_ETD; i++, etd++) {
291 		int state = -1;
292 		struct urb_priv *urb_priv;
293 		if (etd->urb) {
294 			urb_priv = etd->urb->hcpriv;
295 			if (urb_priv)
296 				state = urb_priv->state;
297 		}
298 
299 		seq_printf(s,
300 			"etd_num: %d\n"
301 			"ep: %s\n"
302 			"alloc: %d\n"
303 			"len: %d\n"
304 			"busy sw: %d\n"
305 			"busy hw: %d\n"
306 			"urb state: %d\n"
307 			"current urb: %p\n",
308 
309 			i,
310 			format_ep(etd->ep, buf, sizeof(buf)),
311 			etd->alloc,
312 			etd->len,
313 			etd->urb != NULL,
314 			(readl(imx21->regs + USBH_ETDENSET) & (1 << i)) > 0,
315 			state,
316 			etd->urb);
317 
318 		for (j = 0; j < 4; j++) {
319 			dword = etd_readl(imx21, i, j);
320 			switch (j) {
321 			case 0:
322 				format_etd_dword0(dword, buf, sizeof(buf));
323 				break;
324 			case 2:
325 				snprintf(buf, sizeof(buf),
326 					"cc=0X%02X", dword >> DW2_COMPCODE);
327 				break;
328 			default:
329 				*buf = 0;
330 				break;
331 			}
332 			seq_printf(s,
333 				"dword %d: submitted=%08X cur=%08X [%s]\n",
334 				j,
335 				etd->submitted_dwords[j],
336 				dword,
337 				buf);
338 		}
339 		seq_printf(s, "\n");
340 	}
341 
342 	spin_unlock_irqrestore(&imx21->lock, flags);
343 
344 	return 0;
345 }
346 
debug_statistics_show_one(struct seq_file * s,const char * name,struct debug_stats * stats)347 static void debug_statistics_show_one(struct seq_file *s,
348 	const char *name, struct debug_stats *stats)
349 {
350 	seq_printf(s, "%s:\n"
351 		"submitted URBs: %lu\n"
352 		"completed OK: %lu\n"
353 		"completed failed: %lu\n"
354 		"unlinked: %lu\n"
355 		"queued for ETD: %lu\n"
356 		"queued for DMEM: %lu\n\n",
357 		name,
358 		stats->submitted,
359 		stats->completed_ok,
360 		stats->completed_failed,
361 		stats->unlinked,
362 		stats->queue_etd,
363 		stats->queue_dmem);
364 }
365 
debug_statistics_show(struct seq_file * s,void * v)366 static int debug_statistics_show(struct seq_file *s, void *v)
367 {
368 	struct imx21 *imx21 = s->private;
369 	unsigned long flags;
370 
371 	spin_lock_irqsave(&imx21->lock, flags);
372 
373 	debug_statistics_show_one(s, "nonisoc", &imx21->nonisoc_stats);
374 	debug_statistics_show_one(s, "isoc", &imx21->isoc_stats);
375 	seq_printf(s, "unblock kludge triggers: %lu\n", imx21->debug_unblocks);
376 	spin_unlock_irqrestore(&imx21->lock, flags);
377 
378 	return 0;
379 }
380 
debug_isoc_show_one(struct seq_file * s,const char * name,int index,struct debug_isoc_trace * trace)381 static void debug_isoc_show_one(struct seq_file *s,
382 	const char *name, int index, 	struct debug_isoc_trace *trace)
383 {
384 	seq_printf(s, "%s %d:\n"
385 		"cc=0X%02X\n"
386 		"scheduled frame %d (%d)\n"
387 		"submitted frame %d (%d)\n"
388 		"completed frame %d (%d)\n"
389 		"requested length=%d\n"
390 		"completed length=%d\n\n",
391 		name, index,
392 		trace->cc,
393 		trace->schedule_frame, trace->schedule_frame & 0xFFFF,
394 		trace->submit_frame, trace->submit_frame & 0xFFFF,
395 		trace->done_frame, trace->done_frame & 0xFFFF,
396 		trace->request_len,
397 		trace->done_len);
398 }
399 
debug_isoc_show(struct seq_file * s,void * v)400 static int debug_isoc_show(struct seq_file *s, void *v)
401 {
402 	struct imx21 *imx21 = s->private;
403 	struct debug_isoc_trace *trace;
404 	unsigned long flags;
405 	int i;
406 
407 	spin_lock_irqsave(&imx21->lock, flags);
408 
409 	trace = imx21->isoc_trace_failed;
410 	for (i = 0; i < ARRAY_SIZE(imx21->isoc_trace_failed); i++, trace++)
411 		debug_isoc_show_one(s, "isoc failed", i, trace);
412 
413 	trace = imx21->isoc_trace;
414 	for (i = 0; i < ARRAY_SIZE(imx21->isoc_trace); i++, trace++)
415 		debug_isoc_show_one(s, "isoc", i, trace);
416 
417 	spin_unlock_irqrestore(&imx21->lock, flags);
418 
419 	return 0;
420 }
421 
debug_status_open(struct inode * inode,struct file * file)422 static int debug_status_open(struct inode *inode, struct file *file)
423 {
424 	return single_open(file, debug_status_show, inode->i_private);
425 }
426 
debug_dmem_open(struct inode * inode,struct file * file)427 static int debug_dmem_open(struct inode *inode, struct file *file)
428 {
429 	return single_open(file, debug_dmem_show, inode->i_private);
430 }
431 
debug_etd_open(struct inode * inode,struct file * file)432 static int debug_etd_open(struct inode *inode, struct file *file)
433 {
434 	return single_open(file, debug_etd_show, inode->i_private);
435 }
436 
debug_statistics_open(struct inode * inode,struct file * file)437 static int debug_statistics_open(struct inode *inode, struct file *file)
438 {
439 	return single_open(file, debug_statistics_show, inode->i_private);
440 }
441 
debug_isoc_open(struct inode * inode,struct file * file)442 static int debug_isoc_open(struct inode *inode, struct file *file)
443 {
444 	return single_open(file, debug_isoc_show, inode->i_private);
445 }
446 
447 static const struct file_operations debug_status_fops = {
448 	.open = debug_status_open,
449 	.read = seq_read,
450 	.llseek = seq_lseek,
451 	.release = single_release,
452 };
453 
454 static const struct file_operations debug_dmem_fops = {
455 	.open = debug_dmem_open,
456 	.read = seq_read,
457 	.llseek = seq_lseek,
458 	.release = single_release,
459 };
460 
461 static const struct file_operations debug_etd_fops = {
462 	.open = debug_etd_open,
463 	.read = seq_read,
464 	.llseek = seq_lseek,
465 	.release = single_release,
466 };
467 
468 static const struct file_operations debug_statistics_fops = {
469 	.open = debug_statistics_open,
470 	.read = seq_read,
471 	.llseek = seq_lseek,
472 	.release = single_release,
473 };
474 
475 static const struct file_operations debug_isoc_fops = {
476 	.open = debug_isoc_open,
477 	.read = seq_read,
478 	.llseek = seq_lseek,
479 	.release = single_release,
480 };
481 
create_debug_files(struct imx21 * imx21)482 static void create_debug_files(struct imx21 *imx21)
483 {
484 	imx21->debug_root = debugfs_create_dir(dev_name(imx21->dev), NULL);
485 	if (!imx21->debug_root)
486 		goto failed_create_rootdir;
487 
488 	if (!debugfs_create_file("status", S_IRUGO,
489 			imx21->debug_root, imx21, &debug_status_fops))
490 		goto failed_create;
491 
492 	if (!debugfs_create_file("dmem", S_IRUGO,
493 			imx21->debug_root, imx21, &debug_dmem_fops))
494 		goto failed_create;
495 
496 	if (!debugfs_create_file("etd", S_IRUGO,
497 			imx21->debug_root, imx21, &debug_etd_fops))
498 		goto failed_create;
499 
500 	if (!debugfs_create_file("statistics", S_IRUGO,
501 			imx21->debug_root, imx21, &debug_statistics_fops))
502 		goto failed_create;
503 
504 	if (!debugfs_create_file("isoc", S_IRUGO,
505 			imx21->debug_root, imx21, &debug_isoc_fops))
506 		goto failed_create;
507 
508 	return;
509 
510 failed_create:
511 	debugfs_remove_recursive(imx21->debug_root);
512 
513 failed_create_rootdir:
514 	imx21->debug_root = NULL;
515 }
516 
517 
remove_debug_files(struct imx21 * imx21)518 static void remove_debug_files(struct imx21 *imx21)
519 {
520 	if (imx21->debug_root) {
521 		debugfs_remove_recursive(imx21->debug_root);
522 		imx21->debug_root = NULL;
523 	}
524 }
525 
526 #endif
527 
528