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/mutex.h>
13 #include <linux/debugfs.h>
14 #include <linux/scatterlist.h>
15 #include <asm/uaccess.h>
20 * No, we do not want arbitrarily long data strings.
21 * Use the binary interface if you want to capture bulk data!
26 * Defined by USB 2.0 clause 9.3, table 9.2.
31 * This limit exists to prevent OOMs when the user process stops reading.
32 * If usbmon were available to unprivileged processes, it might be open
33 * to a local DoS. But we have to keep to root in order to prevent
34 * password sniffing from HID devices.
36 #define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
39 * Potentially unlimited number; we limit it for similar allocations.
40 * The usbfs limits this to 128, but we're not quite as generous.
44 #define PRINTF_DFL 250 /* with 5 ISOs segs */
49 unsigned int length
; /* Unsigned here, signed in URB. Historic. */
52 struct mon_event_text
{
53 struct list_head e_link
;
54 int type
; /* submit, complete, etc. */
55 unsigned long id
; /* From pointer, most of the time */
62 int length
; /* Depends on type: xfer length or act length */
69 int numdesc
; /* Full number */
70 struct mon_iso_desc isodesc
[ISODESC_MAX
];
71 unsigned char setup
[SETUP_MAX
];
72 unsigned char data
[DATA_MAX
];
75 #define SLAB_NAME_SZ 30
76 struct mon_reader_text
{
77 struct kmem_cache
*e_slab
;
79 struct list_head e_list
;
80 struct mon_reader r
; /* In C, parent class can be placed anywhere */
82 wait_queue_head_t wait
;
85 struct mutex printf_lock
;
87 char slab_name
[SLAB_NAME_SZ
];
90 static struct dentry
*mon_dir
; /* Usually /sys/kernel/debug/usbmon */
92 static void mon_text_ctor(void *);
99 static struct mon_event_text
*
100 mon_text_read_wait(struct mon_reader_text
*rp
, struct file
*file
);
101 static void mon_text_read_head_t(struct mon_reader_text
*rp
,
102 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
103 static void mon_text_read_head_u(struct mon_reader_text
*rp
,
104 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
105 static void mon_text_read_statset(struct mon_reader_text
*rp
,
106 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
107 static void mon_text_read_intstat(struct mon_reader_text
*rp
,
108 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
109 static void mon_text_read_isostat(struct mon_reader_text
*rp
,
110 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
111 static void mon_text_read_isodesc(struct mon_reader_text
*rp
,
112 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
113 static void mon_text_read_data(struct mon_reader_text
*rp
,
114 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
120 * May be called from an interrupt.
122 * This is called with the whole mon_bus locked, so no additional lock.
125 static inline char mon_text_get_setup(struct mon_event_text
*ep
,
126 struct urb
*urb
, char ev_type
, struct mon_bus
*mbus
)
129 if (ep
->xfertype
!= USB_ENDPOINT_XFER_CONTROL
|| ev_type
!= 'S')
132 if (urb
->setup_packet
== NULL
)
133 return 'Z'; /* '0' would be not as pretty. */
135 memcpy(ep
->setup
, urb
->setup_packet
, SETUP_MAX
);
139 static inline char mon_text_get_data(struct mon_event_text
*ep
, struct urb
*urb
,
140 int len
, char ev_type
, struct mon_bus
*mbus
)
157 if (urb
->num_sgs
== 0) {
158 src
= urb
->transfer_buffer
;
160 return 'Z'; /* '0' would be not as pretty. */
162 struct scatterlist
*sg
= urb
->sg
->sg
;
164 /* If IOMMU coalescing occurred, we cannot trust sg_page */
165 if (urb
->sg
->nents
!= urb
->num_sgs
||
166 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)
183 do_gettimeofday(&tval
);
184 stamp
= tval
.tv_sec
& 0xFFF; /* 2^32 = 4294967296. Limit to 4096s. */
185 stamp
= stamp
* 1000000 + tval
.tv_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
;
243 ep
->setup_flag
= mon_text_get_setup(ep
, urb
, ev_type
, rp
->r
.m_bus
);
244 ep
->data_flag
= mon_text_get_data(ep
, urb
, ep
->length
, ev_type
,
248 list_add_tail(&ep
->e_link
, &rp
->e_list
);
252 static void mon_text_submit(void *data
, struct urb
*urb
)
254 struct mon_reader_text
*rp
= data
;
255 mon_text_event(rp
, urb
, 'S', -EINPROGRESS
);
258 static void mon_text_complete(void *data
, struct urb
*urb
, int status
)
260 struct mon_reader_text
*rp
= data
;
261 mon_text_event(rp
, urb
, 'C', status
);
264 static void mon_text_error(void *data
, struct urb
*urb
, int error
)
266 struct mon_reader_text
*rp
= data
;
267 struct mon_event_text
*ep
;
269 if (rp
->nevents
>= EVENT_MAX
||
270 (ep
= kmem_cache_alloc(rp
->e_slab
, GFP_ATOMIC
)) == NULL
) {
271 rp
->r
.m_bus
->cnt_text_lost
++;
276 ep
->id
= (unsigned long) urb
;
277 ep
->busnum
= urb
->dev
->bus
->busnum
;
278 ep
->devnum
= urb
->dev
->devnum
;
279 ep
->epnum
= usb_endpoint_num(&urb
->ep
->desc
);
280 ep
->xfertype
= usb_endpoint_type(&urb
->ep
->desc
);
281 ep
->is_in
= usb_urb_dir_in(urb
);
282 ep
->tstamp
= mon_get_timestamp();
286 ep
->setup_flag
= '-';
290 list_add_tail(&ep
->e_link
, &rp
->e_list
);
295 * Fetch next event from the circular buffer.
297 static struct mon_event_text
*mon_text_fetch(struct mon_reader_text
*rp
,
298 struct mon_bus
*mbus
)
303 spin_lock_irqsave(&mbus
->lock
, flags
);
304 if (list_empty(&rp
->e_list
)) {
305 spin_unlock_irqrestore(&mbus
->lock
, flags
);
311 spin_unlock_irqrestore(&mbus
->lock
, flags
);
312 return list_entry(p
, struct mon_event_text
, e_link
);
317 static int mon_text_open(struct inode
*inode
, struct file
*file
)
319 struct mon_bus
*mbus
;
320 struct mon_reader_text
*rp
;
323 mutex_lock(&mon_lock
);
324 mbus
= inode
->i_private
;
326 rp
= kzalloc(sizeof(struct mon_reader_text
), GFP_KERNEL
);
331 INIT_LIST_HEAD(&rp
->e_list
);
332 init_waitqueue_head(&rp
->wait
);
333 mutex_init(&rp
->printf_lock
);
335 rp
->printf_size
= PRINTF_DFL
;
336 rp
->printf_buf
= kmalloc(rp
->printf_size
, GFP_KERNEL
);
337 if (rp
->printf_buf
== NULL
) {
344 rp
->r
.rnf_submit
= mon_text_submit
;
345 rp
->r
.rnf_error
= mon_text_error
;
346 rp
->r
.rnf_complete
= mon_text_complete
;
348 snprintf(rp
->slab_name
, SLAB_NAME_SZ
, "mon_text_%p", rp
);
349 rp
->e_slab
= kmem_cache_create(rp
->slab_name
,
350 sizeof(struct mon_event_text
), sizeof(long), 0,
352 if (rp
->e_slab
== NULL
) {
357 mon_reader_add(mbus
, &rp
->r
);
359 file
->private_data
= rp
;
360 mutex_unlock(&mon_lock
);
364 // kmem_cache_destroy(rp->e_slab);
366 kfree(rp
->printf_buf
);
370 mutex_unlock(&mon_lock
);
375 * For simplicity, we read one record in one system call and throw out
376 * what does not fit. This means that the following does not work:
377 * dd if=/dbg/usbmon/0t bs=10
378 * Also, we do not allow seeks and do not bother advancing the offset.
380 static ssize_t
mon_text_read_t(struct file
*file
, char __user
*buf
,
381 size_t nbytes
, loff_t
*ppos
)
383 struct mon_reader_text
*rp
= file
->private_data
;
384 struct mon_event_text
*ep
;
385 struct mon_text_ptr ptr
;
387 if (IS_ERR(ep
= mon_text_read_wait(rp
, file
)))
389 mutex_lock(&rp
->printf_lock
);
391 ptr
.pbuf
= rp
->printf_buf
;
392 ptr
.limit
= rp
->printf_size
;
394 mon_text_read_head_t(rp
, &ptr
, ep
);
395 mon_text_read_statset(rp
, &ptr
, ep
);
396 ptr
.cnt
+= snprintf(ptr
.pbuf
+ ptr
.cnt
, ptr
.limit
- ptr
.cnt
,
398 mon_text_read_data(rp
, &ptr
, ep
);
400 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
402 mutex_unlock(&rp
->printf_lock
);
403 kmem_cache_free(rp
->e_slab
, ep
);
407 static ssize_t
mon_text_read_u(struct file
*file
, char __user
*buf
,
408 size_t nbytes
, loff_t
*ppos
)
410 struct mon_reader_text
*rp
= file
->private_data
;
411 struct mon_event_text
*ep
;
412 struct mon_text_ptr ptr
;
414 if (IS_ERR(ep
= mon_text_read_wait(rp
, file
)))
416 mutex_lock(&rp
->printf_lock
);
418 ptr
.pbuf
= rp
->printf_buf
;
419 ptr
.limit
= rp
->printf_size
;
421 mon_text_read_head_u(rp
, &ptr
, ep
);
422 if (ep
->type
== 'E') {
423 mon_text_read_statset(rp
, &ptr
, ep
);
424 } else if (ep
->xfertype
== USB_ENDPOINT_XFER_ISOC
) {
425 mon_text_read_isostat(rp
, &ptr
, ep
);
426 mon_text_read_isodesc(rp
, &ptr
, ep
);
427 } else if (ep
->xfertype
== USB_ENDPOINT_XFER_INT
) {
428 mon_text_read_intstat(rp
, &ptr
, ep
);
430 mon_text_read_statset(rp
, &ptr
, ep
);
432 ptr
.cnt
+= snprintf(ptr
.pbuf
+ ptr
.cnt
, ptr
.limit
- ptr
.cnt
,
434 mon_text_read_data(rp
, &ptr
, ep
);
436 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
438 mutex_unlock(&rp
->printf_lock
);
439 kmem_cache_free(rp
->e_slab
, ep
);
443 static struct mon_event_text
*mon_text_read_wait(struct mon_reader_text
*rp
,
446 struct mon_bus
*mbus
= rp
->r
.m_bus
;
447 DECLARE_WAITQUEUE(waita
, current
);
448 struct mon_event_text
*ep
;
450 add_wait_queue(&rp
->wait
, &waita
);
451 set_current_state(TASK_INTERRUPTIBLE
);
452 while ((ep
= mon_text_fetch(rp
, mbus
)) == NULL
) {
453 if (file
->f_flags
& O_NONBLOCK
) {
454 set_current_state(TASK_RUNNING
);
455 remove_wait_queue(&rp
->wait
, &waita
);
456 return ERR_PTR(-EWOULDBLOCK
);
459 * We do not count nwaiters, because ->release is supposed
460 * to be called when all openers are gone only.
463 if (signal_pending(current
)) {
464 remove_wait_queue(&rp
->wait
, &waita
);
465 return ERR_PTR(-EINTR
);
467 set_current_state(TASK_INTERRUPTIBLE
);
469 set_current_state(TASK_RUNNING
);
470 remove_wait_queue(&rp
->wait
, &waita
);
474 static void mon_text_read_head_t(struct mon_reader_text
*rp
,
475 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
479 udir
= (ep
->is_in
? 'i' : 'o');
480 switch (ep
->xfertype
) {
481 case USB_ENDPOINT_XFER_ISOC
: utype
= 'Z'; break;
482 case USB_ENDPOINT_XFER_INT
: utype
= 'I'; break;
483 case USB_ENDPOINT_XFER_CONTROL
: utype
= 'C'; break;
484 default: /* PIPE_BULK */ utype
= 'B';
486 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
487 "%lx %u %c %c%c:%03u:%02u",
488 ep
->id
, ep
->tstamp
, ep
->type
,
489 utype
, udir
, ep
->devnum
, ep
->epnum
);
492 static void mon_text_read_head_u(struct mon_reader_text
*rp
,
493 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
497 udir
= (ep
->is_in
? 'i' : 'o');
498 switch (ep
->xfertype
) {
499 case USB_ENDPOINT_XFER_ISOC
: utype
= 'Z'; break;
500 case USB_ENDPOINT_XFER_INT
: utype
= 'I'; break;
501 case USB_ENDPOINT_XFER_CONTROL
: utype
= 'C'; break;
502 default: /* PIPE_BULK */ utype
= 'B';
504 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
505 "%lx %u %c %c%c:%d:%03u:%u",
506 ep
->id
, ep
->tstamp
, ep
->type
,
507 utype
, udir
, ep
->busnum
, ep
->devnum
, ep
->epnum
);
510 static void mon_text_read_statset(struct mon_reader_text
*rp
,
511 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
514 if (ep
->setup_flag
== 0) { /* Setup packet is present and captured */
515 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
516 " s %02x %02x %04x %04x %04x",
519 (ep
->setup
[3] << 8) | ep
->setup
[2],
520 (ep
->setup
[5] << 8) | ep
->setup
[4],
521 (ep
->setup
[7] << 8) | ep
->setup
[6]);
522 } else if (ep
->setup_flag
!= '-') { /* Unable to capture setup packet */
523 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
524 " %c __ __ ____ ____ ____", ep
->setup_flag
);
525 } else { /* No setup for this kind of URB */
526 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
531 static void mon_text_read_intstat(struct mon_reader_text
*rp
,
532 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
534 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
535 " %d:%d", ep
->status
, ep
->interval
);
538 static void mon_text_read_isostat(struct mon_reader_text
*rp
,
539 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
541 if (ep
->type
== 'S') {
542 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
543 " %d:%d:%d", ep
->status
, ep
->interval
, ep
->start_frame
);
545 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
547 ep
->status
, ep
->interval
, ep
->start_frame
, ep
->error_count
);
551 static void mon_text_read_isodesc(struct mon_reader_text
*rp
,
552 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
554 int ndesc
; /* Display this many */
556 const struct mon_iso_desc
*dp
;
558 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
561 if (ndesc
> ISODESC_MAX
)
566 for (i
= 0; i
< ndesc
; i
++) {
567 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
568 " %d:%u:%u", dp
->status
, dp
->offset
, dp
->length
);
573 static void mon_text_read_data(struct mon_reader_text
*rp
,
574 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
578 if ((data_len
= ep
->length
) > 0) {
579 if (ep
->data_flag
== 0) {
580 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
582 if (data_len
>= DATA_MAX
)
584 for (i
= 0; i
< data_len
; i
++) {
586 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
590 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
592 "%02x", ep
->data
[i
]);
594 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
597 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
598 " %c\n", ep
->data_flag
);
601 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
, "\n");
605 static int mon_text_release(struct inode
*inode
, struct file
*file
)
607 struct mon_reader_text
*rp
= file
->private_data
;
608 struct mon_bus
*mbus
;
609 /* unsigned long flags; */
611 struct mon_event_text
*ep
;
613 mutex_lock(&mon_lock
);
614 mbus
= inode
->i_private
;
616 if (mbus
->nreaders
<= 0) {
617 printk(KERN_ERR TAG
": consistency error on close\n");
618 mutex_unlock(&mon_lock
);
621 mon_reader_del(mbus
, &rp
->r
);
624 * In theory, e_list is protected by mbus->lock. However,
625 * after mon_reader_del has finished, the following is the case:
626 * - we are not on reader list anymore, so new events won't be added;
627 * - whole mbus may be dropped if it was orphaned.
628 * So, we better not touch mbus.
630 /* spin_lock_irqsave(&mbus->lock, flags); */
631 while (!list_empty(&rp
->e_list
)) {
633 ep
= list_entry(p
, struct mon_event_text
, e_link
);
636 kmem_cache_free(rp
->e_slab
, ep
);
638 /* spin_unlock_irqrestore(&mbus->lock, flags); */
640 kmem_cache_destroy(rp
->e_slab
);
641 kfree(rp
->printf_buf
);
644 mutex_unlock(&mon_lock
);
648 static const struct file_operations mon_fops_text_t
= {
649 .owner
= THIS_MODULE
,
650 .open
= mon_text_open
,
652 .read
= mon_text_read_t
,
653 .release
= mon_text_release
,
656 static const struct file_operations mon_fops_text_u
= {
657 .owner
= THIS_MODULE
,
658 .open
= mon_text_open
,
660 .read
= mon_text_read_u
,
661 .release
= mon_text_release
,
664 int mon_text_add(struct mon_bus
*mbus
, const struct usb_bus
*ubus
)
667 enum { NAMESZ
= 10 };
669 int busnum
= ubus
? ubus
->busnum
: 0;
673 rc
= snprintf(name
, NAMESZ
, "%dt", busnum
);
674 if (rc
<= 0 || rc
>= NAMESZ
)
676 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
,
683 rc
= snprintf(name
, NAMESZ
, "%du", busnum
);
684 if (rc
<= 0 || rc
>= NAMESZ
)
686 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_text_u
);
691 rc
= snprintf(name
, NAMESZ
, "%ds", busnum
);
692 if (rc
<= 0 || rc
>= NAMESZ
)
694 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_stat
);
703 debugfs_remove(mbus
->dent_u
);
708 debugfs_remove(mbus
->dent_t
);
716 void mon_text_del(struct mon_bus
*mbus
)
718 debugfs_remove(mbus
->dent_u
);
719 if (mbus
->dent_t
!= NULL
)
720 debugfs_remove(mbus
->dent_t
);
721 debugfs_remove(mbus
->dent_s
);
725 * Slab interface: constructor.
727 static void mon_text_ctor(void *mem
)
730 * Nothing to initialize. No, really!
731 * So, we fill it with garbage to emulate a reused object.
733 memset(mem
, 0xe5, sizeof(struct mon_event_text
));
736 int __init
mon_text_init(void)
738 struct dentry
*mondir
;
740 mondir
= debugfs_create_dir("usbmon", usb_debug_root
);
741 if (IS_ERR(mondir
)) {
742 printk(KERN_NOTICE TAG
": debugfs is not available\n");
745 if (mondir
== NULL
) {
746 printk(KERN_NOTICE TAG
": unable to create usbmon directory\n");
753 void mon_text_exit(void)
755 debugfs_remove(mon_dir
);