Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * The USB Monitor, inspired by Dave Harding's USBMon.
0004  *
0005  * This is a text format reader.
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/list.h>
0010 #include <linux/usb.h>
0011 #include <linux/slab.h>
0012 #include <linux/sched/signal.h>
0013 #include <linux/time.h>
0014 #include <linux/ktime.h>
0015 #include <linux/export.h>
0016 #include <linux/mutex.h>
0017 #include <linux/debugfs.h>
0018 #include <linux/scatterlist.h>
0019 #include <linux/uaccess.h>
0020 
0021 #include "usb_mon.h"
0022 
0023 /*
0024  * No, we do not want arbitrarily long data strings.
0025  * Use the binary interface if you want to capture bulk data!
0026  */
0027 #define DATA_MAX  32
0028 
0029 /*
0030  * Defined by USB 2.0 clause 9.3, table 9.2.
0031  */
0032 #define SETUP_MAX  8
0033 
0034 /*
0035  * This limit exists to prevent OOMs when the user process stops reading.
0036  * If usbmon were available to unprivileged processes, it might be open
0037  * to a local DoS. But we have to keep to root in order to prevent
0038  * password sniffing from HID devices.
0039  */
0040 #define EVENT_MAX  (4*PAGE_SIZE / sizeof(struct mon_event_text))
0041 
0042 /*
0043  * Potentially unlimited number; we limit it for similar allocations.
0044  * The usbfs limits this to 128, but we're not quite as generous.
0045  */
0046 #define ISODESC_MAX   5
0047 
0048 #define PRINTF_DFL  250   /* with 5 ISOs segs */
0049 
0050 struct mon_iso_desc {
0051     int status;
0052     unsigned int offset;
0053     unsigned int length;    /* Unsigned here, signed in URB. Historic. */
0054 };
0055 
0056 struct mon_event_text {
0057     struct list_head e_link;
0058     int type;       /* submit, complete, etc. */
0059     unsigned long id;   /* From pointer, most of the time */
0060     unsigned int tstamp;
0061     int busnum;
0062     char devnum;
0063     char epnum;
0064     char is_in;
0065     char xfertype;
0066     int length;     /* Depends on type: xfer length or act length */
0067     int status;
0068     int interval;
0069     int start_frame;
0070     int error_count;
0071     char setup_flag;
0072     char data_flag;
0073     int numdesc;        /* Full number */
0074     struct mon_iso_desc isodesc[ISODESC_MAX];
0075     unsigned char setup[SETUP_MAX];
0076     unsigned char data[DATA_MAX];
0077 };
0078 
0079 #define SLAB_NAME_SZ  30
0080 struct mon_reader_text {
0081     struct kmem_cache *e_slab;
0082     int nevents;
0083     struct list_head e_list;
0084     struct mon_reader r;    /* In C, parent class can be placed anywhere */
0085 
0086     wait_queue_head_t wait;
0087     int printf_size;
0088     size_t printf_offset;
0089     size_t printf_togo;
0090     char *printf_buf;
0091     struct mutex printf_lock;
0092 
0093     char slab_name[SLAB_NAME_SZ];
0094 };
0095 
0096 static struct dentry *mon_dir;      /* Usually /sys/kernel/debug/usbmon */
0097 
0098 static void mon_text_ctor(void *);
0099 
0100 struct mon_text_ptr {
0101     int cnt, limit;
0102     char *pbuf;
0103 };
0104 
0105 static struct mon_event_text *
0106     mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
0107 static void mon_text_read_head_t(struct mon_reader_text *rp,
0108     struct mon_text_ptr *p, const struct mon_event_text *ep);
0109 static void mon_text_read_head_u(struct mon_reader_text *rp,
0110     struct mon_text_ptr *p, const struct mon_event_text *ep);
0111 static void mon_text_read_statset(struct mon_reader_text *rp,
0112     struct mon_text_ptr *p, const struct mon_event_text *ep);
0113 static void mon_text_read_intstat(struct mon_reader_text *rp,
0114     struct mon_text_ptr *p, const struct mon_event_text *ep);
0115 static void mon_text_read_isostat(struct mon_reader_text *rp,
0116     struct mon_text_ptr *p, const struct mon_event_text *ep);
0117 static void mon_text_read_isodesc(struct mon_reader_text *rp,
0118     struct mon_text_ptr *p, const struct mon_event_text *ep);
0119 static void mon_text_read_data(struct mon_reader_text *rp,
0120     struct mon_text_ptr *p, const struct mon_event_text *ep);
0121 
0122 /*
0123  * mon_text_submit
0124  * mon_text_complete
0125  *
0126  * May be called from an interrupt.
0127  *
0128  * This is called with the whole mon_bus locked, so no additional lock.
0129  */
0130 
0131 static inline char mon_text_get_setup(struct mon_event_text *ep,
0132     struct urb *urb, char ev_type, struct mon_bus *mbus)
0133 {
0134 
0135     if (ep->xfertype != USB_ENDPOINT_XFER_CONTROL || ev_type != 'S')
0136         return '-';
0137 
0138     if (urb->setup_packet == NULL)
0139         return 'Z'; /* '0' would be not as pretty. */
0140 
0141     memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
0142     return 0;
0143 }
0144 
0145 static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
0146     int len, char ev_type, struct mon_bus *mbus)
0147 {
0148     void *src;
0149 
0150     if (len <= 0)
0151         return 'L';
0152     if (len >= DATA_MAX)
0153         len = DATA_MAX;
0154 
0155     if (ep->is_in) {
0156         if (ev_type != 'C')
0157             return '<';
0158     } else {
0159         if (ev_type != 'S')
0160             return '>';
0161     }
0162 
0163     if (urb->num_sgs == 0) {
0164         src = urb->transfer_buffer;
0165         if (src == NULL)
0166             return 'Z'; /* '0' would be not as pretty. */
0167     } else {
0168         struct scatterlist *sg = urb->sg;
0169 
0170         if (PageHighMem(sg_page(sg)))
0171             return 'D';
0172 
0173         /* For the text interface we copy only the first sg buffer */
0174         len = min_t(int, sg->length, len);
0175         src = sg_virt(sg);
0176     }
0177 
0178     memcpy(ep->data, src, len);
0179     return 0;
0180 }
0181 
0182 static inline unsigned int mon_get_timestamp(void)
0183 {
0184     struct timespec64 now;
0185     unsigned int stamp;
0186 
0187     ktime_get_ts64(&now);
0188     stamp = now.tv_sec & 0xFFF;  /* 2^32 = 4294967296. Limit to 4096s. */
0189     stamp = stamp * USEC_PER_SEC + now.tv_nsec / NSEC_PER_USEC;
0190     return stamp;
0191 }
0192 
0193 static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
0194     char ev_type, int status)
0195 {
0196     struct mon_event_text *ep;
0197     unsigned int stamp;
0198     struct usb_iso_packet_descriptor *fp;
0199     struct mon_iso_desc *dp;
0200     int i, ndesc;
0201 
0202     stamp = mon_get_timestamp();
0203 
0204     if (rp->nevents >= EVENT_MAX ||
0205         (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
0206         rp->r.m_bus->cnt_text_lost++;
0207         return;
0208     }
0209 
0210     ep->type = ev_type;
0211     ep->id = (unsigned long) urb;
0212     ep->busnum = urb->dev->bus->busnum;
0213     ep->devnum = urb->dev->devnum;
0214     ep->epnum = usb_endpoint_num(&urb->ep->desc);
0215     ep->xfertype = usb_endpoint_type(&urb->ep->desc);
0216     ep->is_in = usb_urb_dir_in(urb);
0217     ep->tstamp = stamp;
0218     ep->length = (ev_type == 'S') ?
0219         urb->transfer_buffer_length : urb->actual_length;
0220     /* Collecting status makes debugging sense for submits, too */
0221     ep->status = status;
0222 
0223     if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
0224         ep->interval = urb->interval;
0225     } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
0226         ep->interval = urb->interval;
0227         ep->start_frame = urb->start_frame;
0228         ep->error_count = urb->error_count;
0229     }
0230     ep->numdesc = urb->number_of_packets;
0231     if (ep->xfertype == USB_ENDPOINT_XFER_ISOC &&
0232             urb->number_of_packets > 0) {
0233         if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
0234             ndesc = ISODESC_MAX;
0235         fp = urb->iso_frame_desc;
0236         dp = ep->isodesc;
0237         for (i = 0; i < ndesc; i++) {
0238             dp->status = fp->status;
0239             dp->offset = fp->offset;
0240             dp->length = (ev_type == 'S') ?
0241                 fp->length : fp->actual_length;
0242             fp++;
0243             dp++;
0244         }
0245         /* Wasteful, but simple to understand: ISO 'C' is sparse. */
0246         if (ev_type == 'C')
0247             ep->length = urb->transfer_buffer_length;
0248     }
0249 
0250     ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
0251     ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
0252             rp->r.m_bus);
0253 
0254     rp->nevents++;
0255     list_add_tail(&ep->e_link, &rp->e_list);
0256     wake_up(&rp->wait);
0257 }
0258 
0259 static void mon_text_submit(void *data, struct urb *urb)
0260 {
0261     struct mon_reader_text *rp = data;
0262     mon_text_event(rp, urb, 'S', -EINPROGRESS);
0263 }
0264 
0265 static void mon_text_complete(void *data, struct urb *urb, int status)
0266 {
0267     struct mon_reader_text *rp = data;
0268     mon_text_event(rp, urb, 'C', status);
0269 }
0270 
0271 static void mon_text_error(void *data, struct urb *urb, int error)
0272 {
0273     struct mon_reader_text *rp = data;
0274     struct mon_event_text *ep;
0275 
0276     if (rp->nevents >= EVENT_MAX ||
0277         (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
0278         rp->r.m_bus->cnt_text_lost++;
0279         return;
0280     }
0281 
0282     ep->type = 'E';
0283     ep->id = (unsigned long) urb;
0284     ep->busnum = urb->dev->bus->busnum;
0285     ep->devnum = urb->dev->devnum;
0286     ep->epnum = usb_endpoint_num(&urb->ep->desc);
0287     ep->xfertype = usb_endpoint_type(&urb->ep->desc);
0288     ep->is_in = usb_urb_dir_in(urb);
0289     ep->tstamp = mon_get_timestamp();
0290     ep->length = 0;
0291     ep->status = error;
0292 
0293     ep->setup_flag = '-';
0294     ep->data_flag = 'E';
0295 
0296     rp->nevents++;
0297     list_add_tail(&ep->e_link, &rp->e_list);
0298     wake_up(&rp->wait);
0299 }
0300 
0301 /*
0302  * Fetch next event from the circular buffer.
0303  */
0304 static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
0305     struct mon_bus *mbus)
0306 {
0307     struct list_head *p;
0308     unsigned long flags;
0309 
0310     spin_lock_irqsave(&mbus->lock, flags);
0311     if (list_empty(&rp->e_list)) {
0312         spin_unlock_irqrestore(&mbus->lock, flags);
0313         return NULL;
0314     }
0315     p = rp->e_list.next;
0316     list_del(p);
0317     --rp->nevents;
0318     spin_unlock_irqrestore(&mbus->lock, flags);
0319     return list_entry(p, struct mon_event_text, e_link);
0320 }
0321 
0322 /*
0323  */
0324 static int mon_text_open(struct inode *inode, struct file *file)
0325 {
0326     struct mon_bus *mbus;
0327     struct mon_reader_text *rp;
0328     int rc;
0329 
0330     mutex_lock(&mon_lock);
0331     mbus = inode->i_private;
0332 
0333     rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
0334     if (rp == NULL) {
0335         rc = -ENOMEM;
0336         goto err_alloc;
0337     }
0338     INIT_LIST_HEAD(&rp->e_list);
0339     init_waitqueue_head(&rp->wait);
0340     mutex_init(&rp->printf_lock);
0341 
0342     rp->printf_size = PRINTF_DFL;
0343     rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
0344     if (rp->printf_buf == NULL) {
0345         rc = -ENOMEM;
0346         goto err_alloc_pr;
0347     }
0348 
0349     rp->r.m_bus = mbus;
0350     rp->r.r_data = rp;
0351     rp->r.rnf_submit = mon_text_submit;
0352     rp->r.rnf_error = mon_text_error;
0353     rp->r.rnf_complete = mon_text_complete;
0354 
0355     snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
0356     rp->e_slab = kmem_cache_create(rp->slab_name,
0357         sizeof(struct mon_event_text), sizeof(long), 0,
0358         mon_text_ctor);
0359     if (rp->e_slab == NULL) {
0360         rc = -ENOMEM;
0361         goto err_slab;
0362     }
0363 
0364     mon_reader_add(mbus, &rp->r);
0365 
0366     file->private_data = rp;
0367     mutex_unlock(&mon_lock);
0368     return 0;
0369 
0370 // err_busy:
0371 //  kmem_cache_destroy(rp->e_slab);
0372 err_slab:
0373     kfree(rp->printf_buf);
0374 err_alloc_pr:
0375     kfree(rp);
0376 err_alloc:
0377     mutex_unlock(&mon_lock);
0378     return rc;
0379 }
0380 
0381 static ssize_t mon_text_copy_to_user(struct mon_reader_text *rp,
0382     char __user * const buf, const size_t nbytes)
0383 {
0384     const size_t togo = min(nbytes, rp->printf_togo);
0385 
0386     if (copy_to_user(buf, &rp->printf_buf[rp->printf_offset], togo))
0387         return -EFAULT;
0388     rp->printf_togo -= togo;
0389     rp->printf_offset += togo;
0390     return togo;
0391 }
0392 
0393 /* ppos is not advanced since the llseek operation is not permitted. */
0394 static ssize_t mon_text_read_t(struct file *file, char __user *buf,
0395     size_t nbytes, loff_t *ppos)
0396 {
0397     struct mon_reader_text *rp = file->private_data;
0398     struct mon_event_text *ep;
0399     struct mon_text_ptr ptr;
0400     ssize_t ret;
0401 
0402     mutex_lock(&rp->printf_lock);
0403 
0404     if (rp->printf_togo == 0) {
0405 
0406         ep = mon_text_read_wait(rp, file);
0407         if (IS_ERR(ep)) {
0408             mutex_unlock(&rp->printf_lock);
0409             return PTR_ERR(ep);
0410         }
0411         ptr.cnt = 0;
0412         ptr.pbuf = rp->printf_buf;
0413         ptr.limit = rp->printf_size;
0414 
0415         mon_text_read_head_t(rp, &ptr, ep);
0416         mon_text_read_statset(rp, &ptr, ep);
0417         ptr.cnt += scnprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
0418             " %d", ep->length);
0419         mon_text_read_data(rp, &ptr, ep);
0420 
0421         rp->printf_togo = ptr.cnt;
0422         rp->printf_offset = 0;
0423 
0424         kmem_cache_free(rp->e_slab, ep);
0425     }
0426 
0427     ret = mon_text_copy_to_user(rp, buf, nbytes);
0428     mutex_unlock(&rp->printf_lock);
0429     return ret;
0430 }
0431 
0432 /* ppos is not advanced since the llseek operation is not permitted. */
0433 static ssize_t mon_text_read_u(struct file *file, char __user *buf,
0434     size_t nbytes, loff_t *ppos)
0435 {
0436     struct mon_reader_text *rp = file->private_data;
0437     struct mon_event_text *ep;
0438     struct mon_text_ptr ptr;
0439     ssize_t ret;
0440 
0441     mutex_lock(&rp->printf_lock);
0442 
0443     if (rp->printf_togo == 0) {
0444 
0445         ep = mon_text_read_wait(rp, file);
0446         if (IS_ERR(ep)) {
0447             mutex_unlock(&rp->printf_lock);
0448             return PTR_ERR(ep);
0449         }
0450         ptr.cnt = 0;
0451         ptr.pbuf = rp->printf_buf;
0452         ptr.limit = rp->printf_size;
0453 
0454         mon_text_read_head_u(rp, &ptr, ep);
0455         if (ep->type == 'E') {
0456             mon_text_read_statset(rp, &ptr, ep);
0457         } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
0458             mon_text_read_isostat(rp, &ptr, ep);
0459             mon_text_read_isodesc(rp, &ptr, ep);
0460         } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
0461             mon_text_read_intstat(rp, &ptr, ep);
0462         } else {
0463             mon_text_read_statset(rp, &ptr, ep);
0464         }
0465         ptr.cnt += scnprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
0466             " %d", ep->length);
0467         mon_text_read_data(rp, &ptr, ep);
0468 
0469         rp->printf_togo = ptr.cnt;
0470         rp->printf_offset = 0;
0471 
0472         kmem_cache_free(rp->e_slab, ep);
0473     }
0474 
0475     ret = mon_text_copy_to_user(rp, buf, nbytes);
0476     mutex_unlock(&rp->printf_lock);
0477     return ret;
0478 }
0479 
0480 static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
0481     struct file *file)
0482 {
0483     struct mon_bus *mbus = rp->r.m_bus;
0484     DECLARE_WAITQUEUE(waita, current);
0485     struct mon_event_text *ep;
0486 
0487     add_wait_queue(&rp->wait, &waita);
0488     set_current_state(TASK_INTERRUPTIBLE);
0489     while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
0490         if (file->f_flags & O_NONBLOCK) {
0491             set_current_state(TASK_RUNNING);
0492             remove_wait_queue(&rp->wait, &waita);
0493             return ERR_PTR(-EWOULDBLOCK);
0494         }
0495         /*
0496          * We do not count nwaiters, because ->release is supposed
0497          * to be called when all openers are gone only.
0498          */
0499         schedule();
0500         if (signal_pending(current)) {
0501             remove_wait_queue(&rp->wait, &waita);
0502             return ERR_PTR(-EINTR);
0503         }
0504         set_current_state(TASK_INTERRUPTIBLE);
0505     }
0506     set_current_state(TASK_RUNNING);
0507     remove_wait_queue(&rp->wait, &waita);
0508     return ep;
0509 }
0510 
0511 static void mon_text_read_head_t(struct mon_reader_text *rp,
0512     struct mon_text_ptr *p, const struct mon_event_text *ep)
0513 {
0514     char udir, utype;
0515 
0516     udir = (ep->is_in ? 'i' : 'o');
0517     switch (ep->xfertype) {
0518     case USB_ENDPOINT_XFER_ISOC:    utype = 'Z'; break;
0519     case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
0520     case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
0521     default: /* PIPE_BULK */  utype = 'B';
0522     }
0523     p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0524         "%lx %u %c %c%c:%03u:%02u",
0525         ep->id, ep->tstamp, ep->type,
0526         utype, udir, ep->devnum, ep->epnum);
0527 }
0528 
0529 static void mon_text_read_head_u(struct mon_reader_text *rp,
0530     struct mon_text_ptr *p, const struct mon_event_text *ep)
0531 {
0532     char udir, utype;
0533 
0534     udir = (ep->is_in ? 'i' : 'o');
0535     switch (ep->xfertype) {
0536     case USB_ENDPOINT_XFER_ISOC:    utype = 'Z'; break;
0537     case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
0538     case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
0539     default: /* PIPE_BULK */  utype = 'B';
0540     }
0541     p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0542         "%lx %u %c %c%c:%d:%03u:%u",
0543         ep->id, ep->tstamp, ep->type,
0544         utype, udir, ep->busnum, ep->devnum, ep->epnum);
0545 }
0546 
0547 static void mon_text_read_statset(struct mon_reader_text *rp,
0548     struct mon_text_ptr *p, const struct mon_event_text *ep)
0549 {
0550 
0551     if (ep->setup_flag == 0) {   /* Setup packet is present and captured */
0552         p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0553             " s %02x %02x %04x %04x %04x",
0554             ep->setup[0],
0555             ep->setup[1],
0556             (ep->setup[3] << 8) | ep->setup[2],
0557             (ep->setup[5] << 8) | ep->setup[4],
0558             (ep->setup[7] << 8) | ep->setup[6]);
0559     } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
0560         p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0561             " %c __ __ ____ ____ ____", ep->setup_flag);
0562     } else {                     /* No setup for this kind of URB */
0563         p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0564             " %d", ep->status);
0565     }
0566 }
0567 
0568 static void mon_text_read_intstat(struct mon_reader_text *rp,
0569     struct mon_text_ptr *p, const struct mon_event_text *ep)
0570 {
0571     p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0572         " %d:%d", ep->status, ep->interval);
0573 }
0574 
0575 static void mon_text_read_isostat(struct mon_reader_text *rp,
0576     struct mon_text_ptr *p, const struct mon_event_text *ep)
0577 {
0578     if (ep->type == 'S') {
0579         p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0580             " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
0581     } else {
0582         p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0583             " %d:%d:%d:%d",
0584             ep->status, ep->interval, ep->start_frame, ep->error_count);
0585     }
0586 }
0587 
0588 static void mon_text_read_isodesc(struct mon_reader_text *rp,
0589     struct mon_text_ptr *p, const struct mon_event_text *ep)
0590 {
0591     int ndesc;  /* Display this many */
0592     int i;
0593     const struct mon_iso_desc *dp;
0594 
0595     p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0596         " %d", ep->numdesc);
0597     ndesc = ep->numdesc;
0598     if (ndesc > ISODESC_MAX)
0599         ndesc = ISODESC_MAX;
0600     if (ndesc < 0)
0601         ndesc = 0;
0602     dp = ep->isodesc;
0603     for (i = 0; i < ndesc; i++) {
0604         p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0605             " %d:%u:%u", dp->status, dp->offset, dp->length);
0606         dp++;
0607     }
0608 }
0609 
0610 static void mon_text_read_data(struct mon_reader_text *rp,
0611     struct mon_text_ptr *p, const struct mon_event_text *ep)
0612 {
0613     int data_len, i;
0614 
0615     if ((data_len = ep->length) > 0) {
0616         if (ep->data_flag == 0) {
0617             p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0618                 " =");
0619             if (data_len >= DATA_MAX)
0620                 data_len = DATA_MAX;
0621             for (i = 0; i < data_len; i++) {
0622                 if (i % 4 == 0) {
0623                     p->cnt += scnprintf(p->pbuf + p->cnt,
0624                         p->limit - p->cnt,
0625                         " ");
0626                 }
0627                 p->cnt += scnprintf(p->pbuf + p->cnt,
0628                     p->limit - p->cnt,
0629                     "%02x", ep->data[i]);
0630             }
0631             p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0632                 "\n");
0633         } else {
0634             p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt,
0635                 " %c\n", ep->data_flag);
0636         }
0637     } else {
0638         p->cnt += scnprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
0639     }
0640 }
0641 
0642 static int mon_text_release(struct inode *inode, struct file *file)
0643 {
0644     struct mon_reader_text *rp = file->private_data;
0645     struct mon_bus *mbus;
0646     /* unsigned long flags; */
0647     struct list_head *p;
0648     struct mon_event_text *ep;
0649 
0650     mutex_lock(&mon_lock);
0651     mbus = inode->i_private;
0652 
0653     if (mbus->nreaders <= 0) {
0654         printk(KERN_ERR TAG ": consistency error on close\n");
0655         mutex_unlock(&mon_lock);
0656         return 0;
0657     }
0658     mon_reader_del(mbus, &rp->r);
0659 
0660     /*
0661      * In theory, e_list is protected by mbus->lock. However,
0662      * after mon_reader_del has finished, the following is the case:
0663      *  - we are not on reader list anymore, so new events won't be added;
0664      *  - whole mbus may be dropped if it was orphaned.
0665      * So, we better not touch mbus.
0666      */
0667     /* spin_lock_irqsave(&mbus->lock, flags); */
0668     while (!list_empty(&rp->e_list)) {
0669         p = rp->e_list.next;
0670         ep = list_entry(p, struct mon_event_text, e_link);
0671         list_del(p);
0672         --rp->nevents;
0673         kmem_cache_free(rp->e_slab, ep);
0674     }
0675     /* spin_unlock_irqrestore(&mbus->lock, flags); */
0676 
0677     kmem_cache_destroy(rp->e_slab);
0678     kfree(rp->printf_buf);
0679     kfree(rp);
0680 
0681     mutex_unlock(&mon_lock);
0682     return 0;
0683 }
0684 
0685 static const struct file_operations mon_fops_text_t = {
0686     .owner =    THIS_MODULE,
0687     .open =     mon_text_open,
0688     .llseek =   no_llseek,
0689     .read =     mon_text_read_t,
0690     .release =  mon_text_release,
0691 };
0692 
0693 static const struct file_operations mon_fops_text_u = {
0694     .owner =    THIS_MODULE,
0695     .open =     mon_text_open,
0696     .llseek =   no_llseek,
0697     .read =     mon_text_read_u,
0698     .release =  mon_text_release,
0699 };
0700 
0701 int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
0702 {
0703     enum { NAMESZ = 10 };
0704     char name[NAMESZ];
0705     int busnum = ubus? ubus->busnum: 0;
0706     int rc;
0707 
0708     if (mon_dir == NULL)
0709         return 0;
0710 
0711     if (ubus != NULL) {
0712         rc = snprintf(name, NAMESZ, "%dt", busnum);
0713         if (rc <= 0 || rc >= NAMESZ)
0714             goto err_print_t;
0715         mbus->dent_t = debugfs_create_file(name, 0600, mon_dir, mbus,
0716                                  &mon_fops_text_t);
0717     }
0718 
0719     rc = snprintf(name, NAMESZ, "%du", busnum);
0720     if (rc <= 0 || rc >= NAMESZ)
0721         goto err_print_u;
0722     mbus->dent_u = debugfs_create_file(name, 0600, mon_dir, mbus,
0723                        &mon_fops_text_u);
0724 
0725     rc = snprintf(name, NAMESZ, "%ds", busnum);
0726     if (rc <= 0 || rc >= NAMESZ)
0727         goto err_print_s;
0728     mbus->dent_s = debugfs_create_file(name, 0600, mon_dir, mbus,
0729                        &mon_fops_stat);
0730 
0731     return 1;
0732 
0733 err_print_s:
0734     debugfs_remove(mbus->dent_u);
0735     mbus->dent_u = NULL;
0736 err_print_u:
0737     if (ubus != NULL) {
0738         debugfs_remove(mbus->dent_t);
0739         mbus->dent_t = NULL;
0740     }
0741 err_print_t:
0742     return 0;
0743 }
0744 
0745 void mon_text_del(struct mon_bus *mbus)
0746 {
0747     debugfs_remove(mbus->dent_u);
0748     debugfs_remove(mbus->dent_t);
0749     debugfs_remove(mbus->dent_s);
0750 }
0751 
0752 /*
0753  * Slab interface: constructor.
0754  */
0755 static void mon_text_ctor(void *mem)
0756 {
0757     /*
0758      * Nothing to initialize. No, really!
0759      * So, we fill it with garbage to emulate a reused object.
0760      */
0761     memset(mem, 0xe5, sizeof(struct mon_event_text));
0762 }
0763 
0764 int __init mon_text_init(void)
0765 {
0766     mon_dir = debugfs_create_dir("usbmon", usb_debug_root);
0767     return 0;
0768 }
0769 
0770 void mon_text_exit(void)
0771 {
0772     debugfs_remove(mon_dir);
0773 }