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/sched/signal.h>
12 #include <linux/time.h>
13 #include <linux/ktime.h>
14 #include <linux/export.h>
15 #include <linux/mutex.h>
16 #include <linux/debugfs.h>
17 #include <linux/scatterlist.h>
18 #include <linux/uaccess.h>
23 * No, we do not want arbitrarily long data strings.
24 * Use the binary interface if you want to capture bulk data!
29 * Defined by USB 2.0 clause 9.3, table 9.2.
34 * This limit exists to prevent OOMs when the user process stops reading.
35 * If usbmon were available to unprivileged processes, it might be open
36 * to a local DoS. But we have to keep to root in order to prevent
37 * password sniffing from HID devices.
39 #define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
42 * Potentially unlimited number; we limit it for similar allocations.
43 * The usbfs limits this to 128, but we're not quite as generous.
47 #define PRINTF_DFL 250 /* with 5 ISOs segs */
52 unsigned int length
; /* Unsigned here, signed in URB. Historic. */
55 struct mon_event_text
{
56 struct list_head e_link
;
57 int type
; /* submit, complete, etc. */
58 unsigned long id
; /* From pointer, most of the time */
65 int length
; /* Depends on type: xfer length or act length */
72 int numdesc
; /* Full number */
73 struct mon_iso_desc isodesc
[ISODESC_MAX
];
74 unsigned char setup
[SETUP_MAX
];
75 unsigned char data
[DATA_MAX
];
78 #define SLAB_NAME_SZ 30
79 struct mon_reader_text
{
80 struct kmem_cache
*e_slab
;
82 struct list_head e_list
;
83 struct mon_reader r
; /* In C, parent class can be placed anywhere */
85 wait_queue_head_t wait
;
88 struct mutex printf_lock
;
90 char slab_name
[SLAB_NAME_SZ
];
93 static struct dentry
*mon_dir
; /* Usually /sys/kernel/debug/usbmon */
95 static void mon_text_ctor(void *);
102 static struct mon_event_text
*
103 mon_text_read_wait(struct mon_reader_text
*rp
, struct file
*file
);
104 static void mon_text_read_head_t(struct mon_reader_text
*rp
,
105 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
106 static void mon_text_read_head_u(struct mon_reader_text
*rp
,
107 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
108 static void mon_text_read_statset(struct mon_reader_text
*rp
,
109 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
110 static void mon_text_read_intstat(struct mon_reader_text
*rp
,
111 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
112 static void mon_text_read_isostat(struct mon_reader_text
*rp
,
113 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
114 static void mon_text_read_isodesc(struct mon_reader_text
*rp
,
115 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
116 static void mon_text_read_data(struct mon_reader_text
*rp
,
117 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
123 * May be called from an interrupt.
125 * This is called with the whole mon_bus locked, so no additional lock.
128 static inline char mon_text_get_setup(struct mon_event_text
*ep
,
129 struct urb
*urb
, char ev_type
, struct mon_bus
*mbus
)
132 if (ep
->xfertype
!= USB_ENDPOINT_XFER_CONTROL
|| ev_type
!= 'S')
135 if (urb
->setup_packet
== NULL
)
136 return 'Z'; /* '0' would be not as pretty. */
138 memcpy(ep
->setup
, urb
->setup_packet
, SETUP_MAX
);
142 static inline char mon_text_get_data(struct mon_event_text
*ep
, struct urb
*urb
,
143 int len
, char ev_type
, struct mon_bus
*mbus
)
160 if (urb
->num_sgs
== 0) {
161 src
= urb
->transfer_buffer
;
163 return 'Z'; /* '0' would be not as pretty. */
165 struct scatterlist
*sg
= urb
->sg
;
167 if (PageHighMem(sg_page(sg
)))
170 /* For the text interface we copy only the first sg buffer */
171 len
= min_t(int, sg
->length
, len
);
175 memcpy(ep
->data
, src
, len
);
179 static inline unsigned int mon_get_timestamp(void)
181 struct timespec64 now
;
184 ktime_get_ts64(&now
);
185 stamp
= now
.tv_sec
& 0xFFF; /* 2^32 = 4294967296. Limit to 4096s. */
186 stamp
= stamp
* USEC_PER_SEC
+ now
.tv_nsec
/ NSEC_PER_USEC
;
190 static void mon_text_event(struct mon_reader_text
*rp
, struct urb
*urb
,
191 char ev_type
, int status
)
193 struct mon_event_text
*ep
;
195 struct usb_iso_packet_descriptor
*fp
;
196 struct mon_iso_desc
*dp
;
199 stamp
= mon_get_timestamp();
201 if (rp
->nevents
>= EVENT_MAX
||
202 (ep
= kmem_cache_alloc(rp
->e_slab
, GFP_ATOMIC
)) == NULL
) {
203 rp
->r
.m_bus
->cnt_text_lost
++;
208 ep
->id
= (unsigned long) urb
;
209 ep
->busnum
= urb
->dev
->bus
->busnum
;
210 ep
->devnum
= urb
->dev
->devnum
;
211 ep
->epnum
= usb_endpoint_num(&urb
->ep
->desc
);
212 ep
->xfertype
= usb_endpoint_type(&urb
->ep
->desc
);
213 ep
->is_in
= usb_urb_dir_in(urb
);
215 ep
->length
= (ev_type
== 'S') ?
216 urb
->transfer_buffer_length
: urb
->actual_length
;
217 /* Collecting status makes debugging sense for submits, too */
220 if (ep
->xfertype
== USB_ENDPOINT_XFER_INT
) {
221 ep
->interval
= urb
->interval
;
222 } else if (ep
->xfertype
== USB_ENDPOINT_XFER_ISOC
) {
223 ep
->interval
= urb
->interval
;
224 ep
->start_frame
= urb
->start_frame
;
225 ep
->error_count
= urb
->error_count
;
227 ep
->numdesc
= urb
->number_of_packets
;
228 if (ep
->xfertype
== USB_ENDPOINT_XFER_ISOC
&&
229 urb
->number_of_packets
> 0) {
230 if ((ndesc
= urb
->number_of_packets
) > ISODESC_MAX
)
232 fp
= urb
->iso_frame_desc
;
234 for (i
= 0; i
< ndesc
; i
++) {
235 dp
->status
= fp
->status
;
236 dp
->offset
= fp
->offset
;
237 dp
->length
= (ev_type
== 'S') ?
238 fp
->length
: fp
->actual_length
;
242 /* Wasteful, but simple to understand: ISO 'C' is sparse. */
244 ep
->length
= urb
->transfer_buffer_length
;
247 ep
->setup_flag
= mon_text_get_setup(ep
, urb
, ev_type
, rp
->r
.m_bus
);
248 ep
->data_flag
= mon_text_get_data(ep
, urb
, ep
->length
, ev_type
,
252 list_add_tail(&ep
->e_link
, &rp
->e_list
);
256 static void mon_text_submit(void *data
, struct urb
*urb
)
258 struct mon_reader_text
*rp
= data
;
259 mon_text_event(rp
, urb
, 'S', -EINPROGRESS
);
262 static void mon_text_complete(void *data
, struct urb
*urb
, int status
)
264 struct mon_reader_text
*rp
= data
;
265 mon_text_event(rp
, urb
, 'C', status
);
268 static void mon_text_error(void *data
, struct urb
*urb
, int error
)
270 struct mon_reader_text
*rp
= data
;
271 struct mon_event_text
*ep
;
273 if (rp
->nevents
>= EVENT_MAX
||
274 (ep
= kmem_cache_alloc(rp
->e_slab
, GFP_ATOMIC
)) == NULL
) {
275 rp
->r
.m_bus
->cnt_text_lost
++;
280 ep
->id
= (unsigned long) urb
;
281 ep
->busnum
= urb
->dev
->bus
->busnum
;
282 ep
->devnum
= urb
->dev
->devnum
;
283 ep
->epnum
= usb_endpoint_num(&urb
->ep
->desc
);
284 ep
->xfertype
= usb_endpoint_type(&urb
->ep
->desc
);
285 ep
->is_in
= usb_urb_dir_in(urb
);
286 ep
->tstamp
= mon_get_timestamp();
290 ep
->setup_flag
= '-';
294 list_add_tail(&ep
->e_link
, &rp
->e_list
);
299 * Fetch next event from the circular buffer.
301 static struct mon_event_text
*mon_text_fetch(struct mon_reader_text
*rp
,
302 struct mon_bus
*mbus
)
307 spin_lock_irqsave(&mbus
->lock
, flags
);
308 if (list_empty(&rp
->e_list
)) {
309 spin_unlock_irqrestore(&mbus
->lock
, flags
);
315 spin_unlock_irqrestore(&mbus
->lock
, flags
);
316 return list_entry(p
, struct mon_event_text
, e_link
);
321 static int mon_text_open(struct inode
*inode
, struct file
*file
)
323 struct mon_bus
*mbus
;
324 struct mon_reader_text
*rp
;
327 mutex_lock(&mon_lock
);
328 mbus
= inode
->i_private
;
330 rp
= kzalloc(sizeof(struct mon_reader_text
), GFP_KERNEL
);
335 INIT_LIST_HEAD(&rp
->e_list
);
336 init_waitqueue_head(&rp
->wait
);
337 mutex_init(&rp
->printf_lock
);
339 rp
->printf_size
= PRINTF_DFL
;
340 rp
->printf_buf
= kmalloc(rp
->printf_size
, GFP_KERNEL
);
341 if (rp
->printf_buf
== NULL
) {
348 rp
->r
.rnf_submit
= mon_text_submit
;
349 rp
->r
.rnf_error
= mon_text_error
;
350 rp
->r
.rnf_complete
= mon_text_complete
;
352 snprintf(rp
->slab_name
, SLAB_NAME_SZ
, "mon_text_%p", rp
);
353 rp
->e_slab
= kmem_cache_create(rp
->slab_name
,
354 sizeof(struct mon_event_text
), sizeof(long), 0,
356 if (rp
->e_slab
== NULL
) {
361 mon_reader_add(mbus
, &rp
->r
);
363 file
->private_data
= rp
;
364 mutex_unlock(&mon_lock
);
368 // kmem_cache_destroy(rp->e_slab);
370 kfree(rp
->printf_buf
);
374 mutex_unlock(&mon_lock
);
379 * For simplicity, we read one record in one system call and throw out
380 * what does not fit. This means that the following does not work:
381 * dd if=/dbg/usbmon/0t bs=10
382 * Also, we do not allow seeks and do not bother advancing the offset.
384 static ssize_t
mon_text_read_t(struct file
*file
, char __user
*buf
,
385 size_t nbytes
, loff_t
*ppos
)
387 struct mon_reader_text
*rp
= file
->private_data
;
388 struct mon_event_text
*ep
;
389 struct mon_text_ptr ptr
;
391 ep
= mon_text_read_wait(rp
, file
);
394 mutex_lock(&rp
->printf_lock
);
396 ptr
.pbuf
= rp
->printf_buf
;
397 ptr
.limit
= rp
->printf_size
;
399 mon_text_read_head_t(rp
, &ptr
, ep
);
400 mon_text_read_statset(rp
, &ptr
, ep
);
401 ptr
.cnt
+= snprintf(ptr
.pbuf
+ ptr
.cnt
, ptr
.limit
- ptr
.cnt
,
403 mon_text_read_data(rp
, &ptr
, ep
);
405 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
407 mutex_unlock(&rp
->printf_lock
);
408 kmem_cache_free(rp
->e_slab
, ep
);
412 static ssize_t
mon_text_read_u(struct file
*file
, char __user
*buf
,
413 size_t nbytes
, loff_t
*ppos
)
415 struct mon_reader_text
*rp
= file
->private_data
;
416 struct mon_event_text
*ep
;
417 struct mon_text_ptr ptr
;
419 ep
= mon_text_read_wait(rp
, file
);
422 mutex_lock(&rp
->printf_lock
);
424 ptr
.pbuf
= rp
->printf_buf
;
425 ptr
.limit
= rp
->printf_size
;
427 mon_text_read_head_u(rp
, &ptr
, ep
);
428 if (ep
->type
== 'E') {
429 mon_text_read_statset(rp
, &ptr
, ep
);
430 } else if (ep
->xfertype
== USB_ENDPOINT_XFER_ISOC
) {
431 mon_text_read_isostat(rp
, &ptr
, ep
);
432 mon_text_read_isodesc(rp
, &ptr
, ep
);
433 } else if (ep
->xfertype
== USB_ENDPOINT_XFER_INT
) {
434 mon_text_read_intstat(rp
, &ptr
, ep
);
436 mon_text_read_statset(rp
, &ptr
, ep
);
438 ptr
.cnt
+= snprintf(ptr
.pbuf
+ ptr
.cnt
, ptr
.limit
- ptr
.cnt
,
440 mon_text_read_data(rp
, &ptr
, ep
);
442 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
444 mutex_unlock(&rp
->printf_lock
);
445 kmem_cache_free(rp
->e_slab
, ep
);
449 static struct mon_event_text
*mon_text_read_wait(struct mon_reader_text
*rp
,
452 struct mon_bus
*mbus
= rp
->r
.m_bus
;
453 DECLARE_WAITQUEUE(waita
, current
);
454 struct mon_event_text
*ep
;
456 add_wait_queue(&rp
->wait
, &waita
);
457 set_current_state(TASK_INTERRUPTIBLE
);
458 while ((ep
= mon_text_fetch(rp
, mbus
)) == NULL
) {
459 if (file
->f_flags
& O_NONBLOCK
) {
460 set_current_state(TASK_RUNNING
);
461 remove_wait_queue(&rp
->wait
, &waita
);
462 return ERR_PTR(-EWOULDBLOCK
);
465 * We do not count nwaiters, because ->release is supposed
466 * to be called when all openers are gone only.
469 if (signal_pending(current
)) {
470 remove_wait_queue(&rp
->wait
, &waita
);
471 return ERR_PTR(-EINTR
);
473 set_current_state(TASK_INTERRUPTIBLE
);
475 set_current_state(TASK_RUNNING
);
476 remove_wait_queue(&rp
->wait
, &waita
);
480 static void mon_text_read_head_t(struct mon_reader_text
*rp
,
481 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
485 udir
= (ep
->is_in
? 'i' : 'o');
486 switch (ep
->xfertype
) {
487 case USB_ENDPOINT_XFER_ISOC
: utype
= 'Z'; break;
488 case USB_ENDPOINT_XFER_INT
: utype
= 'I'; break;
489 case USB_ENDPOINT_XFER_CONTROL
: utype
= 'C'; break;
490 default: /* PIPE_BULK */ utype
= 'B';
492 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
493 "%lx %u %c %c%c:%03u:%02u",
494 ep
->id
, ep
->tstamp
, ep
->type
,
495 utype
, udir
, ep
->devnum
, ep
->epnum
);
498 static void mon_text_read_head_u(struct mon_reader_text
*rp
,
499 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
503 udir
= (ep
->is_in
? 'i' : 'o');
504 switch (ep
->xfertype
) {
505 case USB_ENDPOINT_XFER_ISOC
: utype
= 'Z'; break;
506 case USB_ENDPOINT_XFER_INT
: utype
= 'I'; break;
507 case USB_ENDPOINT_XFER_CONTROL
: utype
= 'C'; break;
508 default: /* PIPE_BULK */ utype
= 'B';
510 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
511 "%lx %u %c %c%c:%d:%03u:%u",
512 ep
->id
, ep
->tstamp
, ep
->type
,
513 utype
, udir
, ep
->busnum
, ep
->devnum
, ep
->epnum
);
516 static void mon_text_read_statset(struct mon_reader_text
*rp
,
517 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
520 if (ep
->setup_flag
== 0) { /* Setup packet is present and captured */
521 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
522 " s %02x %02x %04x %04x %04x",
525 (ep
->setup
[3] << 8) | ep
->setup
[2],
526 (ep
->setup
[5] << 8) | ep
->setup
[4],
527 (ep
->setup
[7] << 8) | ep
->setup
[6]);
528 } else if (ep
->setup_flag
!= '-') { /* Unable to capture setup packet */
529 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
530 " %c __ __ ____ ____ ____", ep
->setup_flag
);
531 } else { /* No setup for this kind of URB */
532 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
537 static void mon_text_read_intstat(struct mon_reader_text
*rp
,
538 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
540 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
541 " %d:%d", ep
->status
, ep
->interval
);
544 static void mon_text_read_isostat(struct mon_reader_text
*rp
,
545 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
547 if (ep
->type
== 'S') {
548 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
549 " %d:%d:%d", ep
->status
, ep
->interval
, ep
->start_frame
);
551 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
553 ep
->status
, ep
->interval
, ep
->start_frame
, ep
->error_count
);
557 static void mon_text_read_isodesc(struct mon_reader_text
*rp
,
558 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
560 int ndesc
; /* Display this many */
562 const struct mon_iso_desc
*dp
;
564 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
567 if (ndesc
> ISODESC_MAX
)
572 for (i
= 0; i
< ndesc
; i
++) {
573 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
574 " %d:%u:%u", dp
->status
, dp
->offset
, dp
->length
);
579 static void mon_text_read_data(struct mon_reader_text
*rp
,
580 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
584 if ((data_len
= ep
->length
) > 0) {
585 if (ep
->data_flag
== 0) {
586 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
588 if (data_len
>= DATA_MAX
)
590 for (i
= 0; i
< data_len
; i
++) {
592 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
596 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
598 "%02x", ep
->data
[i
]);
600 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
603 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
604 " %c\n", ep
->data_flag
);
607 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
, "\n");
611 static int mon_text_release(struct inode
*inode
, struct file
*file
)
613 struct mon_reader_text
*rp
= file
->private_data
;
614 struct mon_bus
*mbus
;
615 /* unsigned long flags; */
617 struct mon_event_text
*ep
;
619 mutex_lock(&mon_lock
);
620 mbus
= inode
->i_private
;
622 if (mbus
->nreaders
<= 0) {
623 printk(KERN_ERR TAG
": consistency error on close\n");
624 mutex_unlock(&mon_lock
);
627 mon_reader_del(mbus
, &rp
->r
);
630 * In theory, e_list is protected by mbus->lock. However,
631 * after mon_reader_del has finished, the following is the case:
632 * - we are not on reader list anymore, so new events won't be added;
633 * - whole mbus may be dropped if it was orphaned.
634 * So, we better not touch mbus.
636 /* spin_lock_irqsave(&mbus->lock, flags); */
637 while (!list_empty(&rp
->e_list
)) {
639 ep
= list_entry(p
, struct mon_event_text
, e_link
);
642 kmem_cache_free(rp
->e_slab
, ep
);
644 /* spin_unlock_irqrestore(&mbus->lock, flags); */
646 kmem_cache_destroy(rp
->e_slab
);
647 kfree(rp
->printf_buf
);
650 mutex_unlock(&mon_lock
);
654 static const struct file_operations mon_fops_text_t
= {
655 .owner
= THIS_MODULE
,
656 .open
= mon_text_open
,
658 .read
= mon_text_read_t
,
659 .release
= mon_text_release
,
662 static const struct file_operations mon_fops_text_u
= {
663 .owner
= THIS_MODULE
,
664 .open
= mon_text_open
,
666 .read
= mon_text_read_u
,
667 .release
= mon_text_release
,
670 int mon_text_add(struct mon_bus
*mbus
, const struct usb_bus
*ubus
)
673 enum { NAMESZ
= 10 };
675 int busnum
= ubus
? ubus
->busnum
: 0;
682 rc
= snprintf(name
, NAMESZ
, "%dt", busnum
);
683 if (rc
<= 0 || rc
>= NAMESZ
)
685 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
,
692 rc
= snprintf(name
, NAMESZ
, "%du", busnum
);
693 if (rc
<= 0 || rc
>= NAMESZ
)
695 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_text_u
);
700 rc
= snprintf(name
, NAMESZ
, "%ds", busnum
);
701 if (rc
<= 0 || rc
>= NAMESZ
)
703 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_stat
);
712 debugfs_remove(mbus
->dent_u
);
717 debugfs_remove(mbus
->dent_t
);
725 void mon_text_del(struct mon_bus
*mbus
)
727 debugfs_remove(mbus
->dent_u
);
728 if (mbus
->dent_t
!= NULL
)
729 debugfs_remove(mbus
->dent_t
);
730 debugfs_remove(mbus
->dent_s
);
734 * Slab interface: constructor.
736 static void mon_text_ctor(void *mem
)
739 * Nothing to initialize. No, really!
740 * So, we fill it with garbage to emulate a reused object.
742 memset(mem
, 0xe5, sizeof(struct mon_event_text
));
745 int __init
mon_text_init(void)
747 struct dentry
*mondir
;
749 mondir
= debugfs_create_dir("usbmon", usb_debug_root
);
750 if (IS_ERR(mondir
)) {
751 /* debugfs not available, but we can use usbmon without it */
754 if (mondir
== NULL
) {
755 printk(KERN_NOTICE TAG
": unable to create usbmon directory\n");
762 void mon_text_exit(void)
764 debugfs_remove(mon_dir
);