2 * The USB Monitor, inspired by Dave Harding's USBMon.
4 * This is a text format reader.
7 #include <linux/kernel.h>
8 #include <linux/list.h>
10 #include <linux/slab.h>
11 #include <linux/time.h>
12 #include <linux/ktime.h>
13 #include <linux/export.h>
14 #include <linux/mutex.h>
15 #include <linux/debugfs.h>
16 #include <linux/scatterlist.h>
17 #include <linux/uaccess.h>
22 * No, we do not want arbitrarily long data strings.
23 * Use the binary interface if you want to capture bulk data!
28 * Defined by USB 2.0 clause 9.3, table 9.2.
33 * This limit exists to prevent OOMs when the user process stops reading.
34 * If usbmon were available to unprivileged processes, it might be open
35 * to a local DoS. But we have to keep to root in order to prevent
36 * password sniffing from HID devices.
38 #define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
41 * Potentially unlimited number; we limit it for similar allocations.
42 * The usbfs limits this to 128, but we're not quite as generous.
46 #define PRINTF_DFL 250 /* with 5 ISOs segs */
51 unsigned int length; /* Unsigned here, signed in URB. Historic. */
54 struct mon_event_text {
55 struct list_head e_link;
56 int type; /* submit, complete, etc. */
57 unsigned long id; /* From pointer, most of the time */
64 int length; /* Depends on type: xfer length or act length */
71 int numdesc; /* Full number */
72 struct mon_iso_desc isodesc[ISODESC_MAX];
73 unsigned char setup[SETUP_MAX];
74 unsigned char data[DATA_MAX];
77 #define SLAB_NAME_SZ 30
78 struct mon_reader_text {
79 struct kmem_cache *e_slab;
81 struct list_head e_list;
82 struct mon_reader r; /* In C, parent class can be placed anywhere */
84 wait_queue_head_t wait;
87 struct mutex printf_lock;
89 char slab_name[SLAB_NAME_SZ];
92 static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
94 static void mon_text_ctor(void *);
101 static struct mon_event_text *
102 mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
103 static void mon_text_read_head_t(struct mon_reader_text *rp,
104 struct mon_text_ptr *p, const struct mon_event_text *ep);
105 static void mon_text_read_head_u(struct mon_reader_text *rp,
106 struct mon_text_ptr *p, const struct mon_event_text *ep);
107 static void mon_text_read_statset(struct mon_reader_text *rp,
108 struct mon_text_ptr *p, const struct mon_event_text *ep);
109 static void mon_text_read_intstat(struct mon_reader_text *rp,
110 struct mon_text_ptr *p, const struct mon_event_text *ep);
111 static void mon_text_read_isostat(struct mon_reader_text *rp,
112 struct mon_text_ptr *p, const struct mon_event_text *ep);
113 static void mon_text_read_isodesc(struct mon_reader_text *rp,
114 struct mon_text_ptr *p, const struct mon_event_text *ep);
115 static void mon_text_read_data(struct mon_reader_text *rp,
116 struct mon_text_ptr *p, const struct mon_event_text *ep);
122 * May be called from an interrupt.
124 * This is called with the whole mon_bus locked, so no additional lock.
127 static inline char mon_text_get_setup(struct mon_event_text *ep,
128 struct urb *urb, char ev_type, struct mon_bus *mbus)
131 if (ep->xfertype != USB_ENDPOINT_XFER_CONTROL || ev_type != 'S')
134 if (urb->setup_packet == NULL)
135 return 'Z'; /* '0' would be not as pretty. */
137 memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
141 static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
142 int len, char ev_type, struct mon_bus *mbus)
159 if (urb->num_sgs == 0) {
160 src = urb->transfer_buffer;
162 return 'Z'; /* '0' would be not as pretty. */
164 struct scatterlist *sg = urb->sg;
166 if (PageHighMem(sg_page(sg)))
169 /* For the text interface we copy only the first sg buffer */
170 len = min_t(int, sg->length, len);
174 memcpy(ep->data, src, len);
178 static inline unsigned int mon_get_timestamp(void)
180 struct timespec64 now;
183 ktime_get_ts64(&now);
184 stamp = now.tv_sec & 0xFFF; /* 2^32 = 4294967296. Limit to 4096s. */
185 stamp = stamp * USEC_PER_SEC + now.tv_nsec / NSEC_PER_USEC;
189 static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
190 char ev_type, int status)
192 struct mon_event_text *ep;
194 struct usb_iso_packet_descriptor *fp;
195 struct mon_iso_desc *dp;
198 stamp = mon_get_timestamp();
200 if (rp->nevents >= EVENT_MAX ||
201 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
202 rp->r.m_bus->cnt_text_lost++;
207 ep->id = (unsigned long) urb;
208 ep->busnum = urb->dev->bus->busnum;
209 ep->devnum = urb->dev->devnum;
210 ep->epnum = usb_endpoint_num(&urb->ep->desc);
211 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
212 ep->is_in = usb_urb_dir_in(urb);
214 ep->length = (ev_type == 'S') ?
215 urb->transfer_buffer_length : urb->actual_length;
216 /* Collecting status makes debugging sense for submits, too */
219 if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
220 ep->interval = urb->interval;
221 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
222 ep->interval = urb->interval;
223 ep->start_frame = urb->start_frame;
224 ep->error_count = urb->error_count;
226 ep->numdesc = urb->number_of_packets;
227 if (ep->xfertype == USB_ENDPOINT_XFER_ISOC &&
228 urb->number_of_packets > 0) {
229 if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
231 fp = urb->iso_frame_desc;
233 for (i = 0; i < ndesc; i++) {
234 dp->status = fp->status;
235 dp->offset = fp->offset;
236 dp->length = (ev_type == 'S') ?
237 fp->length : fp->actual_length;
241 /* Wasteful, but simple to understand: ISO 'C' is sparse. */
243 ep->length = urb->transfer_buffer_length;
246 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
247 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
251 list_add_tail(&ep->e_link, &rp->e_list);
255 static void mon_text_submit(void *data, struct urb *urb)
257 struct mon_reader_text *rp = data;
258 mon_text_event(rp, urb, 'S', -EINPROGRESS);
261 static void mon_text_complete(void *data, struct urb *urb, int status)
263 struct mon_reader_text *rp = data;
264 mon_text_event(rp, urb, 'C', status);
267 static void mon_text_error(void *data, struct urb *urb, int error)
269 struct mon_reader_text *rp = data;
270 struct mon_event_text *ep;
272 if (rp->nevents >= EVENT_MAX ||
273 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
274 rp->r.m_bus->cnt_text_lost++;
279 ep->id = (unsigned long) urb;
280 ep->busnum = urb->dev->bus->busnum;
281 ep->devnum = urb->dev->devnum;
282 ep->epnum = usb_endpoint_num(&urb->ep->desc);
283 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
284 ep->is_in = usb_urb_dir_in(urb);
285 ep->tstamp = mon_get_timestamp();
289 ep->setup_flag = '-';
293 list_add_tail(&ep->e_link, &rp->e_list);
298 * Fetch next event from the circular buffer.
300 static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
301 struct mon_bus *mbus)
306 spin_lock_irqsave(&mbus->lock, flags);
307 if (list_empty(&rp->e_list)) {
308 spin_unlock_irqrestore(&mbus->lock, flags);
314 spin_unlock_irqrestore(&mbus->lock, flags);
315 return list_entry(p, struct mon_event_text, e_link);
320 static int mon_text_open(struct inode *inode, struct file *file)
322 struct mon_bus *mbus;
323 struct mon_reader_text *rp;
326 mutex_lock(&mon_lock);
327 mbus = inode->i_private;
329 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
334 INIT_LIST_HEAD(&rp->e_list);
335 init_waitqueue_head(&rp->wait);
336 mutex_init(&rp->printf_lock);
338 rp->printf_size = PRINTF_DFL;
339 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
340 if (rp->printf_buf == NULL) {
347 rp->r.rnf_submit = mon_text_submit;
348 rp->r.rnf_error = mon_text_error;
349 rp->r.rnf_complete = mon_text_complete;
351 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
352 rp->e_slab = kmem_cache_create(rp->slab_name,
353 sizeof(struct mon_event_text), sizeof(long), 0,
355 if (rp->e_slab == NULL) {
360 mon_reader_add(mbus, &rp->r);
362 file->private_data = rp;
363 mutex_unlock(&mon_lock);
367 // kmem_cache_destroy(rp->e_slab);
369 kfree(rp->printf_buf);
373 mutex_unlock(&mon_lock);
378 * For simplicity, we read one record in one system call and throw out
379 * what does not fit. This means that the following does not work:
380 * dd if=/dbg/usbmon/0t bs=10
381 * Also, we do not allow seeks and do not bother advancing the offset.
383 static ssize_t mon_text_read_t(struct file *file, char __user *buf,
384 size_t nbytes, loff_t *ppos)
386 struct mon_reader_text *rp = file->private_data;
387 struct mon_event_text *ep;
388 struct mon_text_ptr ptr;
390 ep = mon_text_read_wait(rp, file);
393 mutex_lock(&rp->printf_lock);
395 ptr.pbuf = rp->printf_buf;
396 ptr.limit = rp->printf_size;
398 mon_text_read_head_t(rp, &ptr, ep);
399 mon_text_read_statset(rp, &ptr, ep);
400 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
402 mon_text_read_data(rp, &ptr, ep);
404 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
406 mutex_unlock(&rp->printf_lock);
407 kmem_cache_free(rp->e_slab, ep);
411 static ssize_t mon_text_read_u(struct file *file, char __user *buf,
412 size_t nbytes, loff_t *ppos)
414 struct mon_reader_text *rp = file->private_data;
415 struct mon_event_text *ep;
416 struct mon_text_ptr ptr;
418 ep = mon_text_read_wait(rp, file);
421 mutex_lock(&rp->printf_lock);
423 ptr.pbuf = rp->printf_buf;
424 ptr.limit = rp->printf_size;
426 mon_text_read_head_u(rp, &ptr, ep);
427 if (ep->type == 'E') {
428 mon_text_read_statset(rp, &ptr, ep);
429 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
430 mon_text_read_isostat(rp, &ptr, ep);
431 mon_text_read_isodesc(rp, &ptr, ep);
432 } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
433 mon_text_read_intstat(rp, &ptr, ep);
435 mon_text_read_statset(rp, &ptr, ep);
437 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
439 mon_text_read_data(rp, &ptr, ep);
441 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
443 mutex_unlock(&rp->printf_lock);
444 kmem_cache_free(rp->e_slab, ep);
448 static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
451 struct mon_bus *mbus = rp->r.m_bus;
452 DECLARE_WAITQUEUE(waita, current);
453 struct mon_event_text *ep;
455 add_wait_queue(&rp->wait, &waita);
456 set_current_state(TASK_INTERRUPTIBLE);
457 while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
458 if (file->f_flags & O_NONBLOCK) {
459 set_current_state(TASK_RUNNING);
460 remove_wait_queue(&rp->wait, &waita);
461 return ERR_PTR(-EWOULDBLOCK);
464 * We do not count nwaiters, because ->release is supposed
465 * to be called when all openers are gone only.
468 if (signal_pending(current)) {
469 remove_wait_queue(&rp->wait, &waita);
470 return ERR_PTR(-EINTR);
472 set_current_state(TASK_INTERRUPTIBLE);
474 set_current_state(TASK_RUNNING);
475 remove_wait_queue(&rp->wait, &waita);
479 static void mon_text_read_head_t(struct mon_reader_text *rp,
480 struct mon_text_ptr *p, const struct mon_event_text *ep)
484 udir = (ep->is_in ? 'i' : 'o');
485 switch (ep->xfertype) {
486 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
487 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
488 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
489 default: /* PIPE_BULK */ utype = 'B';
491 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
492 "%lx %u %c %c%c:%03u:%02u",
493 ep->id, ep->tstamp, ep->type,
494 utype, udir, ep->devnum, ep->epnum);
497 static void mon_text_read_head_u(struct mon_reader_text *rp,
498 struct mon_text_ptr *p, const struct mon_event_text *ep)
502 udir = (ep->is_in ? 'i' : 'o');
503 switch (ep->xfertype) {
504 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
505 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
506 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
507 default: /* PIPE_BULK */ utype = 'B';
509 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
510 "%lx %u %c %c%c:%d:%03u:%u",
511 ep->id, ep->tstamp, ep->type,
512 utype, udir, ep->busnum, ep->devnum, ep->epnum);
515 static void mon_text_read_statset(struct mon_reader_text *rp,
516 struct mon_text_ptr *p, const struct mon_event_text *ep)
519 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
520 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
521 " s %02x %02x %04x %04x %04x",
524 (ep->setup[3] << 8) | ep->setup[2],
525 (ep->setup[5] << 8) | ep->setup[4],
526 (ep->setup[7] << 8) | ep->setup[6]);
527 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
528 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
529 " %c __ __ ____ ____ ____", ep->setup_flag);
530 } else { /* No setup for this kind of URB */
531 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
536 static void mon_text_read_intstat(struct mon_reader_text *rp,
537 struct mon_text_ptr *p, const struct mon_event_text *ep)
539 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
540 " %d:%d", ep->status, ep->interval);
543 static void mon_text_read_isostat(struct mon_reader_text *rp,
544 struct mon_text_ptr *p, const struct mon_event_text *ep)
546 if (ep->type == 'S') {
547 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
548 " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
550 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
552 ep->status, ep->interval, ep->start_frame, ep->error_count);
556 static void mon_text_read_isodesc(struct mon_reader_text *rp,
557 struct mon_text_ptr *p, const struct mon_event_text *ep)
559 int ndesc; /* Display this many */
561 const struct mon_iso_desc *dp;
563 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
566 if (ndesc > ISODESC_MAX)
571 for (i = 0; i < ndesc; i++) {
572 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
573 " %d:%u:%u", dp->status, dp->offset, dp->length);
578 static void mon_text_read_data(struct mon_reader_text *rp,
579 struct mon_text_ptr *p, const struct mon_event_text *ep)
583 if ((data_len = ep->length) > 0) {
584 if (ep->data_flag == 0) {
585 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
587 if (data_len >= DATA_MAX)
589 for (i = 0; i < data_len; i++) {
591 p->cnt += snprintf(p->pbuf + p->cnt,
595 p->cnt += snprintf(p->pbuf + p->cnt,
597 "%02x", ep->data[i]);
599 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
602 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
603 " %c\n", ep->data_flag);
606 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
610 static int mon_text_release(struct inode *inode, struct file *file)
612 struct mon_reader_text *rp = file->private_data;
613 struct mon_bus *mbus;
614 /* unsigned long flags; */
616 struct mon_event_text *ep;
618 mutex_lock(&mon_lock);
619 mbus = inode->i_private;
621 if (mbus->nreaders <= 0) {
622 printk(KERN_ERR TAG ": consistency error on close\n");
623 mutex_unlock(&mon_lock);
626 mon_reader_del(mbus, &rp->r);
629 * In theory, e_list is protected by mbus->lock. However,
630 * after mon_reader_del has finished, the following is the case:
631 * - we are not on reader list anymore, so new events won't be added;
632 * - whole mbus may be dropped if it was orphaned.
633 * So, we better not touch mbus.
635 /* spin_lock_irqsave(&mbus->lock, flags); */
636 while (!list_empty(&rp->e_list)) {
638 ep = list_entry(p, struct mon_event_text, e_link);
641 kmem_cache_free(rp->e_slab, ep);
643 /* spin_unlock_irqrestore(&mbus->lock, flags); */
645 kmem_cache_destroy(rp->e_slab);
646 kfree(rp->printf_buf);
649 mutex_unlock(&mon_lock);
653 static const struct file_operations mon_fops_text_t = {
654 .owner = THIS_MODULE,
655 .open = mon_text_open,
657 .read = mon_text_read_t,
658 .release = mon_text_release,
661 static const struct file_operations mon_fops_text_u = {
662 .owner = THIS_MODULE,
663 .open = mon_text_open,
665 .read = mon_text_read_u,
666 .release = mon_text_release,
669 int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
672 enum { NAMESZ = 10 };
674 int busnum = ubus? ubus->busnum: 0;
681 rc = snprintf(name, NAMESZ, "%dt", busnum);
682 if (rc <= 0 || rc >= NAMESZ)
684 d = debugfs_create_file(name, 0600, mon_dir, mbus,
691 rc = snprintf(name, NAMESZ, "%du", busnum);
692 if (rc <= 0 || rc >= NAMESZ)
694 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
699 rc = snprintf(name, NAMESZ, "%ds", busnum);
700 if (rc <= 0 || rc >= NAMESZ)
702 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
711 debugfs_remove(mbus->dent_u);
716 debugfs_remove(mbus->dent_t);
724 void mon_text_del(struct mon_bus *mbus)
726 debugfs_remove(mbus->dent_u);
727 if (mbus->dent_t != NULL)
728 debugfs_remove(mbus->dent_t);
729 debugfs_remove(mbus->dent_s);
733 * Slab interface: constructor.
735 static void mon_text_ctor(void *mem)
738 * Nothing to initialize. No, really!
739 * So, we fill it with garbage to emulate a reused object.
741 memset(mem, 0xe5, sizeof(struct mon_event_text));
744 int __init mon_text_init(void)
746 struct dentry *mondir;
748 mondir = debugfs_create_dir("usbmon", usb_debug_root);
749 if (IS_ERR(mondir)) {
750 /* debugfs not available, but we can use usbmon without it */
753 if (mondir == NULL) {
754 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
761 void mon_text_exit(void)
763 debugfs_remove(mon_dir);