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