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