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
;
164 if (PageHighMem(sg_page(sg
)))
167 /* For the text interface we copy only the first sg buffer */
168 len
= min_t(int, sg
->length
, len
);
172 memcpy(ep
->data
, src
, len
);
176 static inline unsigned int mon_get_timestamp(void)
181 do_gettimeofday(&tval
);
182 stamp
= tval
.tv_sec
& 0xFFF; /* 2^32 = 4294967296. Limit to 4096s. */
183 stamp
= stamp
* 1000000 + tval
.tv_usec
;
187 static void mon_text_event(struct mon_reader_text
*rp
, struct urb
*urb
,
188 char ev_type
, int status
)
190 struct mon_event_text
*ep
;
192 struct usb_iso_packet_descriptor
*fp
;
193 struct mon_iso_desc
*dp
;
196 stamp
= mon_get_timestamp();
198 if (rp
->nevents
>= EVENT_MAX
||
199 (ep
= kmem_cache_alloc(rp
->e_slab
, GFP_ATOMIC
)) == NULL
) {
200 rp
->r
.m_bus
->cnt_text_lost
++;
205 ep
->id
= (unsigned long) urb
;
206 ep
->busnum
= urb
->dev
->bus
->busnum
;
207 ep
->devnum
= urb
->dev
->devnum
;
208 ep
->epnum
= usb_endpoint_num(&urb
->ep
->desc
);
209 ep
->xfertype
= usb_endpoint_type(&urb
->ep
->desc
);
210 ep
->is_in
= usb_urb_dir_in(urb
);
212 ep
->length
= (ev_type
== 'S') ?
213 urb
->transfer_buffer_length
: urb
->actual_length
;
214 /* Collecting status makes debugging sense for submits, too */
217 if (ep
->xfertype
== USB_ENDPOINT_XFER_INT
) {
218 ep
->interval
= urb
->interval
;
219 } else if (ep
->xfertype
== USB_ENDPOINT_XFER_ISOC
) {
220 ep
->interval
= urb
->interval
;
221 ep
->start_frame
= urb
->start_frame
;
222 ep
->error_count
= urb
->error_count
;
224 ep
->numdesc
= urb
->number_of_packets
;
225 if (ep
->xfertype
== USB_ENDPOINT_XFER_ISOC
&&
226 urb
->number_of_packets
> 0) {
227 if ((ndesc
= urb
->number_of_packets
) > ISODESC_MAX
)
229 fp
= urb
->iso_frame_desc
;
231 for (i
= 0; i
< ndesc
; i
++) {
232 dp
->status
= fp
->status
;
233 dp
->offset
= fp
->offset
;
234 dp
->length
= (ev_type
== 'S') ?
235 fp
->length
: fp
->actual_length
;
241 ep
->setup_flag
= mon_text_get_setup(ep
, urb
, ev_type
, rp
->r
.m_bus
);
242 ep
->data_flag
= mon_text_get_data(ep
, urb
, ep
->length
, ev_type
,
246 list_add_tail(&ep
->e_link
, &rp
->e_list
);
250 static void mon_text_submit(void *data
, struct urb
*urb
)
252 struct mon_reader_text
*rp
= data
;
253 mon_text_event(rp
, urb
, 'S', -EINPROGRESS
);
256 static void mon_text_complete(void *data
, struct urb
*urb
, int status
)
258 struct mon_reader_text
*rp
= data
;
259 mon_text_event(rp
, urb
, 'C', status
);
262 static void mon_text_error(void *data
, struct urb
*urb
, int error
)
264 struct mon_reader_text
*rp
= data
;
265 struct mon_event_text
*ep
;
267 if (rp
->nevents
>= EVENT_MAX
||
268 (ep
= kmem_cache_alloc(rp
->e_slab
, GFP_ATOMIC
)) == NULL
) {
269 rp
->r
.m_bus
->cnt_text_lost
++;
274 ep
->id
= (unsigned long) urb
;
275 ep
->busnum
= urb
->dev
->bus
->busnum
;
276 ep
->devnum
= urb
->dev
->devnum
;
277 ep
->epnum
= usb_endpoint_num(&urb
->ep
->desc
);
278 ep
->xfertype
= usb_endpoint_type(&urb
->ep
->desc
);
279 ep
->is_in
= usb_urb_dir_in(urb
);
280 ep
->tstamp
= mon_get_timestamp();
284 ep
->setup_flag
= '-';
288 list_add_tail(&ep
->e_link
, &rp
->e_list
);
293 * Fetch next event from the circular buffer.
295 static struct mon_event_text
*mon_text_fetch(struct mon_reader_text
*rp
,
296 struct mon_bus
*mbus
)
301 spin_lock_irqsave(&mbus
->lock
, flags
);
302 if (list_empty(&rp
->e_list
)) {
303 spin_unlock_irqrestore(&mbus
->lock
, flags
);
309 spin_unlock_irqrestore(&mbus
->lock
, flags
);
310 return list_entry(p
, struct mon_event_text
, e_link
);
315 static int mon_text_open(struct inode
*inode
, struct file
*file
)
317 struct mon_bus
*mbus
;
318 struct mon_reader_text
*rp
;
321 mutex_lock(&mon_lock
);
322 mbus
= inode
->i_private
;
324 rp
= kzalloc(sizeof(struct mon_reader_text
), GFP_KERNEL
);
329 INIT_LIST_HEAD(&rp
->e_list
);
330 init_waitqueue_head(&rp
->wait
);
331 mutex_init(&rp
->printf_lock
);
333 rp
->printf_size
= PRINTF_DFL
;
334 rp
->printf_buf
= kmalloc(rp
->printf_size
, GFP_KERNEL
);
335 if (rp
->printf_buf
== NULL
) {
342 rp
->r
.rnf_submit
= mon_text_submit
;
343 rp
->r
.rnf_error
= mon_text_error
;
344 rp
->r
.rnf_complete
= mon_text_complete
;
346 snprintf(rp
->slab_name
, SLAB_NAME_SZ
, "mon_text_%p", rp
);
347 rp
->e_slab
= kmem_cache_create(rp
->slab_name
,
348 sizeof(struct mon_event_text
), sizeof(long), 0,
350 if (rp
->e_slab
== NULL
) {
355 mon_reader_add(mbus
, &rp
->r
);
357 file
->private_data
= rp
;
358 mutex_unlock(&mon_lock
);
362 // kmem_cache_destroy(rp->e_slab);
364 kfree(rp
->printf_buf
);
368 mutex_unlock(&mon_lock
);
373 * For simplicity, we read one record in one system call and throw out
374 * what does not fit. This means that the following does not work:
375 * dd if=/dbg/usbmon/0t bs=10
376 * Also, we do not allow seeks and do not bother advancing the offset.
378 static ssize_t
mon_text_read_t(struct file
*file
, char __user
*buf
,
379 size_t nbytes
, loff_t
*ppos
)
381 struct mon_reader_text
*rp
= file
->private_data
;
382 struct mon_event_text
*ep
;
383 struct mon_text_ptr ptr
;
385 if (IS_ERR(ep
= mon_text_read_wait(rp
, file
)))
387 mutex_lock(&rp
->printf_lock
);
389 ptr
.pbuf
= rp
->printf_buf
;
390 ptr
.limit
= rp
->printf_size
;
392 mon_text_read_head_t(rp
, &ptr
, ep
);
393 mon_text_read_statset(rp
, &ptr
, ep
);
394 ptr
.cnt
+= snprintf(ptr
.pbuf
+ ptr
.cnt
, ptr
.limit
- ptr
.cnt
,
396 mon_text_read_data(rp
, &ptr
, ep
);
398 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
400 mutex_unlock(&rp
->printf_lock
);
401 kmem_cache_free(rp
->e_slab
, ep
);
405 static ssize_t
mon_text_read_u(struct file
*file
, char __user
*buf
,
406 size_t nbytes
, loff_t
*ppos
)
408 struct mon_reader_text
*rp
= file
->private_data
;
409 struct mon_event_text
*ep
;
410 struct mon_text_ptr ptr
;
412 if (IS_ERR(ep
= mon_text_read_wait(rp
, file
)))
414 mutex_lock(&rp
->printf_lock
);
416 ptr
.pbuf
= rp
->printf_buf
;
417 ptr
.limit
= rp
->printf_size
;
419 mon_text_read_head_u(rp
, &ptr
, ep
);
420 if (ep
->type
== 'E') {
421 mon_text_read_statset(rp
, &ptr
, ep
);
422 } else if (ep
->xfertype
== USB_ENDPOINT_XFER_ISOC
) {
423 mon_text_read_isostat(rp
, &ptr
, ep
);
424 mon_text_read_isodesc(rp
, &ptr
, ep
);
425 } else if (ep
->xfertype
== USB_ENDPOINT_XFER_INT
) {
426 mon_text_read_intstat(rp
, &ptr
, ep
);
428 mon_text_read_statset(rp
, &ptr
, ep
);
430 ptr
.cnt
+= snprintf(ptr
.pbuf
+ ptr
.cnt
, ptr
.limit
- ptr
.cnt
,
432 mon_text_read_data(rp
, &ptr
, ep
);
434 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
436 mutex_unlock(&rp
->printf_lock
);
437 kmem_cache_free(rp
->e_slab
, ep
);
441 static struct mon_event_text
*mon_text_read_wait(struct mon_reader_text
*rp
,
444 struct mon_bus
*mbus
= rp
->r
.m_bus
;
445 DECLARE_WAITQUEUE(waita
, current
);
446 struct mon_event_text
*ep
;
448 add_wait_queue(&rp
->wait
, &waita
);
449 set_current_state(TASK_INTERRUPTIBLE
);
450 while ((ep
= mon_text_fetch(rp
, mbus
)) == NULL
) {
451 if (file
->f_flags
& O_NONBLOCK
) {
452 set_current_state(TASK_RUNNING
);
453 remove_wait_queue(&rp
->wait
, &waita
);
454 return ERR_PTR(-EWOULDBLOCK
);
457 * We do not count nwaiters, because ->release is supposed
458 * to be called when all openers are gone only.
461 if (signal_pending(current
)) {
462 remove_wait_queue(&rp
->wait
, &waita
);
463 return ERR_PTR(-EINTR
);
465 set_current_state(TASK_INTERRUPTIBLE
);
467 set_current_state(TASK_RUNNING
);
468 remove_wait_queue(&rp
->wait
, &waita
);
472 static void mon_text_read_head_t(struct mon_reader_text
*rp
,
473 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
477 udir
= (ep
->is_in
? 'i' : 'o');
478 switch (ep
->xfertype
) {
479 case USB_ENDPOINT_XFER_ISOC
: utype
= 'Z'; break;
480 case USB_ENDPOINT_XFER_INT
: utype
= 'I'; break;
481 case USB_ENDPOINT_XFER_CONTROL
: utype
= 'C'; break;
482 default: /* PIPE_BULK */ utype
= 'B';
484 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
485 "%lx %u %c %c%c:%03u:%02u",
486 ep
->id
, ep
->tstamp
, ep
->type
,
487 utype
, udir
, ep
->devnum
, ep
->epnum
);
490 static void mon_text_read_head_u(struct mon_reader_text
*rp
,
491 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
495 udir
= (ep
->is_in
? 'i' : 'o');
496 switch (ep
->xfertype
) {
497 case USB_ENDPOINT_XFER_ISOC
: utype
= 'Z'; break;
498 case USB_ENDPOINT_XFER_INT
: utype
= 'I'; break;
499 case USB_ENDPOINT_XFER_CONTROL
: utype
= 'C'; break;
500 default: /* PIPE_BULK */ utype
= 'B';
502 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
503 "%lx %u %c %c%c:%d:%03u:%u",
504 ep
->id
, ep
->tstamp
, ep
->type
,
505 utype
, udir
, ep
->busnum
, ep
->devnum
, ep
->epnum
);
508 static void mon_text_read_statset(struct mon_reader_text
*rp
,
509 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
512 if (ep
->setup_flag
== 0) { /* Setup packet is present and captured */
513 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
514 " s %02x %02x %04x %04x %04x",
517 (ep
->setup
[3] << 8) | ep
->setup
[2],
518 (ep
->setup
[5] << 8) | ep
->setup
[4],
519 (ep
->setup
[7] << 8) | ep
->setup
[6]);
520 } else if (ep
->setup_flag
!= '-') { /* Unable to capture setup packet */
521 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
522 " %c __ __ ____ ____ ____", ep
->setup_flag
);
523 } else { /* No setup for this kind of URB */
524 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
529 static void mon_text_read_intstat(struct mon_reader_text
*rp
,
530 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
532 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
533 " %d:%d", ep
->status
, ep
->interval
);
536 static void mon_text_read_isostat(struct mon_reader_text
*rp
,
537 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
539 if (ep
->type
== 'S') {
540 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
541 " %d:%d:%d", ep
->status
, ep
->interval
, ep
->start_frame
);
543 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
545 ep
->status
, ep
->interval
, ep
->start_frame
, ep
->error_count
);
549 static void mon_text_read_isodesc(struct mon_reader_text
*rp
,
550 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
552 int ndesc
; /* Display this many */
554 const struct mon_iso_desc
*dp
;
556 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
559 if (ndesc
> ISODESC_MAX
)
564 for (i
= 0; i
< ndesc
; i
++) {
565 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
566 " %d:%u:%u", dp
->status
, dp
->offset
, dp
->length
);
571 static void mon_text_read_data(struct mon_reader_text
*rp
,
572 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
576 if ((data_len
= ep
->length
) > 0) {
577 if (ep
->data_flag
== 0) {
578 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
580 if (data_len
>= DATA_MAX
)
582 for (i
= 0; i
< data_len
; i
++) {
584 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
588 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
590 "%02x", ep
->data
[i
]);
592 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
595 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
596 " %c\n", ep
->data_flag
);
599 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
, "\n");
603 static int mon_text_release(struct inode
*inode
, struct file
*file
)
605 struct mon_reader_text
*rp
= file
->private_data
;
606 struct mon_bus
*mbus
;
607 /* unsigned long flags; */
609 struct mon_event_text
*ep
;
611 mutex_lock(&mon_lock
);
612 mbus
= inode
->i_private
;
614 if (mbus
->nreaders
<= 0) {
615 printk(KERN_ERR TAG
": consistency error on close\n");
616 mutex_unlock(&mon_lock
);
619 mon_reader_del(mbus
, &rp
->r
);
622 * In theory, e_list is protected by mbus->lock. However,
623 * after mon_reader_del has finished, the following is the case:
624 * - we are not on reader list anymore, so new events won't be added;
625 * - whole mbus may be dropped if it was orphaned.
626 * So, we better not touch mbus.
628 /* spin_lock_irqsave(&mbus->lock, flags); */
629 while (!list_empty(&rp
->e_list
)) {
631 ep
= list_entry(p
, struct mon_event_text
, e_link
);
634 kmem_cache_free(rp
->e_slab
, ep
);
636 /* spin_unlock_irqrestore(&mbus->lock, flags); */
638 kmem_cache_destroy(rp
->e_slab
);
639 kfree(rp
->printf_buf
);
642 mutex_unlock(&mon_lock
);
646 static const struct file_operations mon_fops_text_t
= {
647 .owner
= THIS_MODULE
,
648 .open
= mon_text_open
,
650 .read
= mon_text_read_t
,
651 .release
= mon_text_release
,
654 static const struct file_operations mon_fops_text_u
= {
655 .owner
= THIS_MODULE
,
656 .open
= mon_text_open
,
658 .read
= mon_text_read_u
,
659 .release
= mon_text_release
,
662 int mon_text_add(struct mon_bus
*mbus
, const struct usb_bus
*ubus
)
665 enum { NAMESZ
= 10 };
667 int busnum
= ubus
? ubus
->busnum
: 0;
671 rc
= snprintf(name
, NAMESZ
, "%dt", busnum
);
672 if (rc
<= 0 || rc
>= NAMESZ
)
674 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
,
681 rc
= snprintf(name
, NAMESZ
, "%du", busnum
);
682 if (rc
<= 0 || rc
>= NAMESZ
)
684 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_text_u
);
689 rc
= snprintf(name
, NAMESZ
, "%ds", busnum
);
690 if (rc
<= 0 || rc
>= NAMESZ
)
692 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_stat
);
701 debugfs_remove(mbus
->dent_u
);
706 debugfs_remove(mbus
->dent_t
);
714 void mon_text_del(struct mon_bus
*mbus
)
716 debugfs_remove(mbus
->dent_u
);
717 if (mbus
->dent_t
!= NULL
)
718 debugfs_remove(mbus
->dent_t
);
719 debugfs_remove(mbus
->dent_s
);
723 * Slab interface: constructor.
725 static void mon_text_ctor(void *mem
)
728 * Nothing to initialize. No, really!
729 * So, we fill it with garbage to emulate a reused object.
731 memset(mem
, 0xe5, sizeof(struct mon_event_text
));
734 int __init
mon_text_init(void)
736 struct dentry
*mondir
;
738 mondir
= debugfs_create_dir("usbmon", usb_debug_root
);
739 if (IS_ERR(mondir
)) {
740 printk(KERN_NOTICE TAG
": debugfs is not available\n");
743 if (mondir
== NULL
) {
744 printk(KERN_NOTICE TAG
": unable to create usbmon directory\n");
751 void mon_text_exit(void)
753 debugfs_remove(mon_dir
);