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