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