1/*
2 * The USB Monitor, inspired by Dave Harding's USBMon.
3 *
4 * This is a text format reader.
5 */
6
7#include <linux/kernel.h>
8#include <linux/list.h>
9#include <linux/usb.h>
10#include <linux/time.h>
11#include <linux/mutex.h>
12#include <linux/debugfs.h>
13#include <asm/uaccess.h>
14
15#include "usb_mon.h"
16
17/*
18 * No, we do not want arbitrarily long data strings.
19 * Use the binary interface if you want to capture bulk data!
20 */
21#define DATA_MAX  32
22
23/*
24 * Defined by USB 2.0 clause 9.3, table 9.2.
25 */
26#define SETUP_MAX  8
27
28/*
29 * This limit exists to prevent OOMs when the user process stops reading.
30 * If usbmon were available to unprivileged processes, it might be open
31 * to a local DoS. But we have to keep to root in order to prevent
32 * password sniffing from HID devices.
33 */
34#define EVENT_MAX  (4*PAGE_SIZE / sizeof(struct mon_event_text))
35
36/*
37 * Potentially unlimited number; we limit it for similar allocations.
38 * The usbfs limits this to 128, but we're not quite as generous.
39 */
40#define ISODESC_MAX   5
41
42#define PRINTF_DFL  250   /* with 5 ISOs segs */
43
44struct mon_iso_desc {
45	int status;
46	unsigned int offset;
47	unsigned int length;	/* Unsigned here, signed in URB. Historic. */
48};
49
50struct mon_event_text {
51	struct list_head e_link;
52	int type;		/* submit, complete, etc. */
53	unsigned int pipe;	/* Pipe */
54	unsigned long id;	/* From pointer, most of the time */
55	unsigned int tstamp;
56	int busnum;
57	int length;		/* Depends on type: xfer length or act length */
58	int status;
59	int interval;
60	int start_frame;
61	int error_count;
62	char setup_flag;
63	char data_flag;
64	int numdesc;		/* Full number */
65	struct mon_iso_desc isodesc[ISODESC_MAX];
66	unsigned char setup[SETUP_MAX];
67	unsigned char data[DATA_MAX];
68};
69
70#define SLAB_NAME_SZ  30
71struct mon_reader_text {
72	struct kmem_cache *e_slab;
73	int nevents;
74	struct list_head e_list;
75	struct mon_reader r;	/* In C, parent class can be placed anywhere */
76
77	wait_queue_head_t wait;
78	int printf_size;
79	char *printf_buf;
80	struct mutex printf_lock;
81
82	char slab_name[SLAB_NAME_SZ];
83};
84
85static struct dentry *mon_dir;		/* Usually /sys/kernel/debug/usbmon */
86
87static void mon_text_ctor(void *, struct kmem_cache *, unsigned long);
88
89struct mon_text_ptr {
90	int cnt, limit;
91	char *pbuf;
92};
93
94static struct mon_event_text *
95    mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
96static void mon_text_read_head_t(struct mon_reader_text *rp,
97	struct mon_text_ptr *p, const struct mon_event_text *ep);
98static void mon_text_read_head_u(struct mon_reader_text *rp,
99	struct mon_text_ptr *p, const struct mon_event_text *ep);
100static void mon_text_read_statset(struct mon_reader_text *rp,
101	struct mon_text_ptr *p, const struct mon_event_text *ep);
102static void mon_text_read_intstat(struct mon_reader_text *rp,
103	struct mon_text_ptr *p, const struct mon_event_text *ep);
104static void mon_text_read_isostat(struct mon_reader_text *rp,
105	struct mon_text_ptr *p, const struct mon_event_text *ep);
106static void mon_text_read_isodesc(struct mon_reader_text *rp,
107	struct mon_text_ptr *p, const struct mon_event_text *ep);
108static void mon_text_read_data(struct mon_reader_text *rp,
109    struct mon_text_ptr *p, const struct mon_event_text *ep);
110
111/*
112 * mon_text_submit
113 * mon_text_complete
114 *
115 * May be called from an interrupt.
116 *
117 * This is called with the whole mon_bus locked, so no additional lock.
118 */
119
120static inline char mon_text_get_setup(struct mon_event_text *ep,
121    struct urb *urb, char ev_type, struct mon_bus *mbus)
122{
123
124	if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
125		return '-';
126
127	if (urb->dev->bus->uses_dma &&
128	    (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
129		return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
130	}
131	if (urb->setup_packet == NULL)
132		return 'Z';	/* '0' would be not as pretty. */
133
134	memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
135	return 0;
136}
137
138static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
139    int len, char ev_type, struct mon_bus *mbus)
140{
141	int pipe = urb->pipe;
142
143	if (len <= 0)
144		return 'L';
145	if (len >= DATA_MAX)
146		len = DATA_MAX;
147
148	if (usb_pipein(pipe)) {
149		if (ev_type != 'C')
150			return '<';
151	} else {
152		if (ev_type != 'S')
153			return '>';
154	}
155
156	/*
157	 * The check to see if it's safe to poke at data has an enormous
158	 * number of corner cases, but it seems that the following is
159	 * more or less safe.
160	 *
161	 * We do not even try to look at transfer_buffer, because it can
162	 * contain non-NULL garbage in case the upper level promised to
163	 * set DMA for the HCD.
164	 */
165	if (urb->dev->bus->uses_dma &&
166	    (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
167		return mon_dmapeek(ep->data, urb->transfer_dma, len);
168	}
169
170	if (urb->transfer_buffer == NULL)
171		return 'Z';	/* '0' would be not as pretty. */
172
173	memcpy(ep->data, urb->transfer_buffer, len);
174	return 0;
175}
176
177static inline unsigned int mon_get_timestamp(void)
178{
179	struct timeval tval;
180	unsigned int stamp;
181
182	do_gettimeofday(&tval);
183	stamp = tval.tv_sec & 0xFFFF;	/* 2^32 = 4294967296. Limit to 4096s. */
184	stamp = stamp * 1000000 + tval.tv_usec;
185	return stamp;
186}
187
188static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
189    char ev_type)
190{
191	struct mon_event_text *ep;
192	unsigned int stamp;
193	struct usb_iso_packet_descriptor *fp;
194	struct mon_iso_desc *dp;
195	int i, ndesc;
196
197	stamp = mon_get_timestamp();
198
199	if (rp->nevents >= EVENT_MAX ||
200	    (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
201		rp->r.m_bus->cnt_text_lost++;
202		return;
203	}
204
205	ep->type = ev_type;
206	ep->pipe = urb->pipe;
207	ep->id = (unsigned long) urb;
208	ep->busnum = urb->dev->bus->busnum;
209	ep->tstamp = stamp;
210	ep->length = (ev_type == 'S') ?
211	    urb->transfer_buffer_length : urb->actual_length;
212	/* Collecting status makes debugging sense for submits, too */
213	ep->status = urb->status;
214
215	if (usb_pipeint(urb->pipe)) {
216		ep->interval = urb->interval;
217	} else if (usb_pipeisoc(urb->pipe)) {
218		ep->interval = urb->interval;
219		ep->start_frame = urb->start_frame;
220		ep->error_count = urb->error_count;
221	}
222	ep->numdesc = urb->number_of_packets;
223	if (usb_pipeisoc(urb->pipe) && urb->number_of_packets > 0) {
224		if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
225			ndesc = ISODESC_MAX;
226		fp = urb->iso_frame_desc;
227		dp = ep->isodesc;
228		for (i = 0; i < ndesc; i++) {
229			dp->status = fp->status;
230			dp->offset = fp->offset;
231			dp->length = (ev_type == 'S') ?
232			    fp->length : fp->actual_length;
233			fp++;
234			dp++;
235		}
236	}
237
238	ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
239	ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
240			rp->r.m_bus);
241
242	rp->nevents++;
243	list_add_tail(&ep->e_link, &rp->e_list);
244	wake_up(&rp->wait);
245}
246
247static void mon_text_submit(void *data, struct urb *urb)
248{
249	struct mon_reader_text *rp = data;
250	mon_text_event(rp, urb, 'S');
251}
252
253static void mon_text_complete(void *data, struct urb *urb)
254{
255	struct mon_reader_text *rp = data;
256	mon_text_event(rp, urb, 'C');
257}
258
259static void mon_text_error(void *data, struct urb *urb, int error)
260{
261	struct mon_reader_text *rp = data;
262	struct mon_event_text *ep;
263
264	if (rp->nevents >= EVENT_MAX ||
265	    (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
266		rp->r.m_bus->cnt_text_lost++;
267		return;
268	}
269
270	ep->type = 'E';
271	ep->pipe = urb->pipe;
272	ep->id = (unsigned long) urb;
273	ep->busnum = 0;
274	ep->tstamp = 0;
275	ep->length = 0;
276	ep->status = error;
277
278	ep->setup_flag = '-';
279	ep->data_flag = 'E';
280
281	rp->nevents++;
282	list_add_tail(&ep->e_link, &rp->e_list);
283	wake_up(&rp->wait);
284}
285
286/*
287 * Fetch next event from the circular buffer.
288 */
289static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
290    struct mon_bus *mbus)
291{
292	struct list_head *p;
293	unsigned long flags;
294
295	spin_lock_irqsave(&mbus->lock, flags);
296	if (list_empty(&rp->e_list)) {
297		spin_unlock_irqrestore(&mbus->lock, flags);
298		return NULL;
299	}
300	p = rp->e_list.next;
301	list_del(p);
302	--rp->nevents;
303	spin_unlock_irqrestore(&mbus->lock, flags);
304	return list_entry(p, struct mon_event_text, e_link);
305}
306
307/*
308 */
309static int mon_text_open(struct inode *inode, struct file *file)
310{
311	struct mon_bus *mbus;
312	struct mon_reader_text *rp;
313	int rc;
314
315	mutex_lock(&mon_lock);
316	mbus = inode->i_private;
317
318	rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
319	if (rp == NULL) {
320		rc = -ENOMEM;
321		goto err_alloc;
322	}
323	INIT_LIST_HEAD(&rp->e_list);
324	init_waitqueue_head(&rp->wait);
325	mutex_init(&rp->printf_lock);
326
327	rp->printf_size = PRINTF_DFL;
328	rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
329	if (rp->printf_buf == NULL) {
330		rc = -ENOMEM;
331		goto err_alloc_pr;
332	}
333
334	rp->r.m_bus = mbus;
335	rp->r.r_data = rp;
336	rp->r.rnf_submit = mon_text_submit;
337	rp->r.rnf_error = mon_text_error;
338	rp->r.rnf_complete = mon_text_complete;
339
340	snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
341	rp->e_slab = kmem_cache_create(rp->slab_name,
342	    sizeof(struct mon_event_text), sizeof(long), 0,
343	    mon_text_ctor, NULL);
344	if (rp->e_slab == NULL) {
345		rc = -ENOMEM;
346		goto err_slab;
347	}
348
349	mon_reader_add(mbus, &rp->r);
350
351	file->private_data = rp;
352	mutex_unlock(&mon_lock);
353	return 0;
354
355// err_busy:
356//	kmem_cache_destroy(rp->e_slab);
357err_slab:
358	kfree(rp->printf_buf);
359err_alloc_pr:
360	kfree(rp);
361err_alloc:
362	mutex_unlock(&mon_lock);
363	return rc;
364}
365
366/*
367 * For simplicity, we read one record in one system call and throw out
368 * what does not fit. This means that the following does not work:
369 *   dd if=/dbg/usbmon/0t bs=10
370 * Also, we do not allow seeks and do not bother advancing the offset.
371 */
372static ssize_t mon_text_read_t(struct file *file, char __user *buf,
373				size_t nbytes, loff_t *ppos)
374{
375	struct mon_reader_text *rp = file->private_data;
376	struct mon_event_text *ep;
377	struct mon_text_ptr ptr;
378
379	if (IS_ERR(ep = mon_text_read_wait(rp, file)))
380		return PTR_ERR(ep);
381	mutex_lock(&rp->printf_lock);
382	ptr.cnt = 0;
383	ptr.pbuf = rp->printf_buf;
384	ptr.limit = rp->printf_size;
385
386	mon_text_read_head_t(rp, &ptr, ep);
387	mon_text_read_statset(rp, &ptr, ep);
388	ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
389	    " %d", ep->length);
390	mon_text_read_data(rp, &ptr, ep);
391
392	if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
393		ptr.cnt = -EFAULT;
394	mutex_unlock(&rp->printf_lock);
395	kmem_cache_free(rp->e_slab, ep);
396	return ptr.cnt;
397}
398
399static ssize_t mon_text_read_u(struct file *file, char __user *buf,
400				size_t nbytes, loff_t *ppos)
401{
402	struct mon_reader_text *rp = file->private_data;
403	struct mon_event_text *ep;
404	struct mon_text_ptr ptr;
405
406	if (IS_ERR(ep = mon_text_read_wait(rp, file)))
407		return PTR_ERR(ep);
408	mutex_lock(&rp->printf_lock);
409	ptr.cnt = 0;
410	ptr.pbuf = rp->printf_buf;
411	ptr.limit = rp->printf_size;
412
413	mon_text_read_head_u(rp, &ptr, ep);
414	if (ep->type == 'E') {
415		mon_text_read_statset(rp, &ptr, ep);
416	} else if (usb_pipeisoc(ep->pipe)) {
417		mon_text_read_isostat(rp, &ptr, ep);
418		mon_text_read_isodesc(rp, &ptr, ep);
419	} else if (usb_pipeint(ep->pipe)) {
420		mon_text_read_intstat(rp, &ptr, ep);
421	} else {
422		mon_text_read_statset(rp, &ptr, ep);
423	}
424	ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
425	    " %d", ep->length);
426	mon_text_read_data(rp, &ptr, ep);
427
428	if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
429		ptr.cnt = -EFAULT;
430	mutex_unlock(&rp->printf_lock);
431	kmem_cache_free(rp->e_slab, ep);
432	return ptr.cnt;
433}
434
435static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
436    struct file *file)
437{
438	struct mon_bus *mbus = rp->r.m_bus;
439	DECLARE_WAITQUEUE(waita, current);
440	struct mon_event_text *ep;
441
442	add_wait_queue(&rp->wait, &waita);
443	set_current_state(TASK_INTERRUPTIBLE);
444	while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
445		if (file->f_flags & O_NONBLOCK) {
446			set_current_state(TASK_RUNNING);
447			remove_wait_queue(&rp->wait, &waita);
448			return ERR_PTR(-EWOULDBLOCK);
449		}
450		/*
451		 * We do not count nwaiters, because ->release is supposed
452		 * to be called when all openers are gone only.
453		 */
454		schedule();
455		if (signal_pending(current)) {
456			remove_wait_queue(&rp->wait, &waita);
457			return ERR_PTR(-EINTR);
458		}
459		set_current_state(TASK_INTERRUPTIBLE);
460	}
461	set_current_state(TASK_RUNNING);
462	remove_wait_queue(&rp->wait, &waita);
463	return ep;
464}
465
466static void mon_text_read_head_t(struct mon_reader_text *rp,
467	struct mon_text_ptr *p, const struct mon_event_text *ep)
468{
469	char udir, utype;
470
471	udir = usb_pipein(ep->pipe) ? 'i' : 'o';
472	switch (usb_pipetype(ep->pipe)) {
473	case PIPE_ISOCHRONOUS:	utype = 'Z'; break;
474	case PIPE_INTERRUPT:	utype = 'I'; break;
475	case PIPE_CONTROL:	utype = 'C'; break;
476	default: /* PIPE_BULK */  utype = 'B';
477	}
478	p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
479	    "%lx %u %c %c%c:%03u:%02u",
480	    ep->id, ep->tstamp, ep->type,
481	    utype, udir,
482	    usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
483}
484
485static void mon_text_read_head_u(struct mon_reader_text *rp,
486	struct mon_text_ptr *p, const struct mon_event_text *ep)
487{
488	char udir, utype;
489
490	udir = usb_pipein(ep->pipe) ? 'i' : 'o';
491	switch (usb_pipetype(ep->pipe)) {
492	case PIPE_ISOCHRONOUS:	utype = 'Z'; break;
493	case PIPE_INTERRUPT:	utype = 'I'; break;
494	case PIPE_CONTROL:	utype = 'C'; break;
495	default: /* PIPE_BULK */  utype = 'B';
496	}
497	p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
498	    "%lx %u %c %c%c:%d:%03u:%u",
499	    ep->id, ep->tstamp, ep->type,
500	    utype, udir,
501	    ep->busnum, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
502}
503
504static void mon_text_read_statset(struct mon_reader_text *rp,
505	struct mon_text_ptr *p, const struct mon_event_text *ep)
506{
507
508	if (ep->setup_flag == 0) {   /* Setup packet is present and captured */
509		p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
510		    " s %02x %02x %04x %04x %04x",
511		    ep->setup[0],
512		    ep->setup[1],
513		    (ep->setup[3] << 8) | ep->setup[2],
514		    (ep->setup[5] << 8) | ep->setup[4],
515		    (ep->setup[7] << 8) | ep->setup[6]);
516	} else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
517		p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
518		    " %c __ __ ____ ____ ____", ep->setup_flag);
519	} else {                     /* No setup for this kind of URB */
520		p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
521		    " %d", ep->status);
522	}
523}
524
525static void mon_text_read_intstat(struct mon_reader_text *rp,
526	struct mon_text_ptr *p, const struct mon_event_text *ep)
527{
528	p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
529	    " %d:%d", ep->status, ep->interval);
530}
531
532static void mon_text_read_isostat(struct mon_reader_text *rp,
533	struct mon_text_ptr *p, const struct mon_event_text *ep)
534{
535	if (ep->type == 'S') {
536		p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
537		    " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
538	} else {
539		p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
540		    " %d:%d:%d:%d",
541		    ep->status, ep->interval, ep->start_frame, ep->error_count);
542	}
543}
544
545static void mon_text_read_isodesc(struct mon_reader_text *rp,
546	struct mon_text_ptr *p, const struct mon_event_text *ep)
547{
548	int ndesc;	/* Display this many */
549	int i;
550	const struct mon_iso_desc *dp;
551
552	p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
553	    " %d", ep->numdesc);
554	ndesc = ep->numdesc;
555	if (ndesc > ISODESC_MAX)
556		ndesc = ISODESC_MAX;
557	if (ndesc < 0)
558		ndesc = 0;
559	dp = ep->isodesc;
560	for (i = 0; i < ndesc; i++) {
561		p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
562		    " %d:%u:%u", dp->status, dp->offset, dp->length);
563		dp++;
564	}
565}
566
567static void mon_text_read_data(struct mon_reader_text *rp,
568    struct mon_text_ptr *p, const struct mon_event_text *ep)
569{
570	int data_len, i;
571
572	if ((data_len = ep->length) > 0) {
573		if (ep->data_flag == 0) {
574			p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
575			    " =");
576			if (data_len >= DATA_MAX)
577				data_len = DATA_MAX;
578			for (i = 0; i < data_len; i++) {
579				if (i % 4 == 0) {
580					p->cnt += snprintf(p->pbuf + p->cnt,
581					    p->limit - p->cnt,
582					    " ");
583				}
584				p->cnt += snprintf(p->pbuf + p->cnt,
585				    p->limit - p->cnt,
586				    "%02x", ep->data[i]);
587			}
588			p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
589			    "\n");
590		} else {
591			p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
592			    " %c\n", ep->data_flag);
593		}
594	} else {
595		p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
596	}
597}
598
599static int mon_text_release(struct inode *inode, struct file *file)
600{
601	struct mon_reader_text *rp = file->private_data;
602	struct mon_bus *mbus;
603	/* unsigned long flags; */
604	struct list_head *p;
605	struct mon_event_text *ep;
606
607	mutex_lock(&mon_lock);
608	mbus = inode->i_private;
609
610	if (mbus->nreaders <= 0) {
611		printk(KERN_ERR TAG ": consistency error on close\n");
612		mutex_unlock(&mon_lock);
613		return 0;
614	}
615	mon_reader_del(mbus, &rp->r);
616
617	/*
618	 * In theory, e_list is protected by mbus->lock. However,
619	 * after mon_reader_del has finished, the following is the case:
620	 *  - we are not on reader list anymore, so new events won't be added;
621	 *  - whole mbus may be dropped if it was orphaned.
622	 * So, we better not touch mbus.
623	 */
624	/* spin_lock_irqsave(&mbus->lock, flags); */
625	while (!list_empty(&rp->e_list)) {
626		p = rp->e_list.next;
627		ep = list_entry(p, struct mon_event_text, e_link);
628		list_del(p);
629		--rp->nevents;
630		kmem_cache_free(rp->e_slab, ep);
631	}
632	/* spin_unlock_irqrestore(&mbus->lock, flags); */
633
634	kmem_cache_destroy(rp->e_slab);
635	kfree(rp->printf_buf);
636	kfree(rp);
637
638	mutex_unlock(&mon_lock);
639	return 0;
640}
641
642static const struct file_operations mon_fops_text_t = {
643	.owner =	THIS_MODULE,
644	.open =		mon_text_open,
645	.llseek =	no_llseek,
646	.read =		mon_text_read_t,
647	.release =	mon_text_release,
648};
649
650static const struct file_operations mon_fops_text_u = {
651	.owner =	THIS_MODULE,
652	.open =		mon_text_open,
653	.llseek =	no_llseek,
654	.read =		mon_text_read_u,
655	.release =	mon_text_release,
656};
657
658int mon_text_add(struct mon_bus *mbus, int busnum)
659{
660	struct dentry *d;
661	enum { NAMESZ = 10 };
662	char name[NAMESZ];
663	int rc;
664
665	rc = snprintf(name, NAMESZ, "%dt", busnum);
666	if (rc <= 0 || rc >= NAMESZ)
667		goto err_print_t;
668	d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_t);
669	if (d == NULL)
670		goto err_create_t;
671	mbus->dent_t = d;
672
673	rc = snprintf(name, NAMESZ, "%du", busnum);
674	if (rc <= 0 || rc >= NAMESZ)
675		goto err_print_u;
676	d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
677	if (d == NULL)
678		goto err_create_u;
679	mbus->dent_u = d;
680
681	rc = snprintf(name, NAMESZ, "%ds", busnum);
682	if (rc <= 0 || rc >= NAMESZ)
683		goto err_print_s;
684	d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
685	if (d == NULL)
686		goto err_create_s;
687	mbus->dent_s = d;
688
689	return 1;
690
691err_create_s:
692err_print_s:
693	debugfs_remove(mbus->dent_u);
694	mbus->dent_u = NULL;
695err_create_u:
696err_print_u:
697	debugfs_remove(mbus->dent_t);
698	mbus->dent_t = NULL;
699err_create_t:
700err_print_t:
701	return 0;
702}
703
704void mon_text_del(struct mon_bus *mbus)
705{
706	debugfs_remove(mbus->dent_u);
707	debugfs_remove(mbus->dent_t);
708	debugfs_remove(mbus->dent_s);
709}
710
711/*
712 * Slab interface: constructor.
713 */
714static void mon_text_ctor(void *mem, struct kmem_cache *slab, unsigned long sflags)
715{
716	/*
717	 * Nothing to initialize. No, really!
718	 * So, we fill it with garbage to emulate a reused object.
719	 */
720	memset(mem, 0xe5, sizeof(struct mon_event_text));
721}
722
723int __init mon_text_init(void)
724{
725	struct dentry *mondir;
726
727	mondir = debugfs_create_dir("usbmon", NULL);
728	if (IS_ERR(mondir)) {
729		printk(KERN_NOTICE TAG ": debugfs is not available\n");
730		return -ENODEV;
731	}
732	if (mondir == NULL) {
733		printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
734		return -ENODEV;
735	}
736	mon_dir = mondir;
737	return 0;
738}
739
740void mon_text_exit(void)
741{
742	debugfs_remove(mon_dir);
743}
744