2 * The USB Monitor, inspired by Dave Harding's USBMon.
4 * This is a binary format reader.
6 * Copyright (C) 2006 Paolo Abeni (paolo.abeni@email.it)
7 * Copyright (C) 2006,2007 Pete Zaitcev (zaitcev@redhat.com)
10 #include <linux/kernel.h>
11 #include <linux/types.h>
13 #include <linux/cdev.h>
14 #include <linux/usb.h>
15 #include <linux/poll.h>
16 #include <linux/compat.h>
18 #include <linux/smp_lock.h>
19 #include <linux/scatterlist.h>
20 #include <linux/slab.h>
22 #include <asm/uaccess.h>
27 * Defined by USB 2.0 clause 9.3, table 9.2.
32 #define MON_IOC_MAGIC 0x92
34 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
35 /* #2 used to be MON_IOCX_URB, removed before it got into Linus tree */
36 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
37 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
38 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
39 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
40 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
41 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
42 /* #9 was MON_IOCT_SETAPI */
43 #define MON_IOCX_GETX _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get)
46 #define MON_IOCX_GET32 _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get32)
47 #define MON_IOCX_MFETCH32 _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch32)
48 #define MON_IOCX_GETX32 _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get32)
52 * Some architectures have enormous basic pages (16KB for ia64, 64KB for ppc).
53 * But it's all right. Just use a simple way to make sure the chunk is never
54 * smaller than a page.
56 * N.B. An application does not know our chunk size.
58 * Woops, get_zeroed_page() returns a single page. I guess we're stuck with
59 * page-sized chunks for the time being.
61 #define CHUNK_SIZE PAGE_SIZE
62 #define CHUNK_ALIGN(x) (((x)+CHUNK_SIZE-1) & ~(CHUNK_SIZE-1))
64 #define BUFF_MAX CHUNK_ALIGN(1200*1024)
65 #define BUFF_DFL CHUNK_ALIGN(300*1024)
66 #define BUFF_MIN CHUNK_ALIGN(8*1024)
69 * The per-event API header (2 per URB).
71 * This structure is seen in userland as defined by the documentation.
74 u64 id
; /* URB ID - from submission to callback */
75 unsigned char type
; /* Same as in text API; extensible. */
76 unsigned char xfer_type
; /* ISO, Intr, Control, Bulk */
77 unsigned char epnum
; /* Endpoint number and transfer direction */
78 unsigned char devnum
; /* Device address */
79 unsigned short busnum
; /* Bus number */
82 s64 ts_sec
; /* gettimeofday */
83 s32 ts_usec
; /* gettimeofday */
85 unsigned int len_urb
; /* Length of data (submitted or actual) */
86 unsigned int len_cap
; /* Delivered length */
88 unsigned char setup
[SETUP_LEN
]; /* Only for Control S-type */
96 unsigned int xfer_flags
;
97 unsigned int ndesc
; /* Actual number of ISO descriptors */
101 * ISO vector, packed into the head of data stream.
102 * This has to take 16 bytes to make sure that the end of buffer
103 * wrap is not happening in the middle of a descriptor.
105 struct mon_bin_isodesc
{
107 unsigned int iso_off
;
108 unsigned int iso_len
;
112 /* per file statistic */
113 struct mon_bin_stats
{
119 struct mon_bin_hdr __user
*hdr
; /* Can be 48 bytes or 64. */
121 size_t alloc
; /* Length of data (can be zero) */
124 struct mon_bin_mfetch
{
125 u32 __user
*offvec
; /* Vector of events fetched */
126 u32 nfetch
; /* Number of events to fetch (out: fetched) */
127 u32 nflush
; /* Number of events to flush */
131 struct mon_bin_get32
{
137 struct mon_bin_mfetch32
{
144 /* Having these two values same prevents wrapping of the mon_bin_hdr */
148 #define PKT_SZ_API0 48 /* API 0 (2.6.20) size */
149 #define PKT_SZ_API1 64 /* API 1 size: extra fields */
151 #define ISODESC_MAX 128 /* Same number as usbfs allows, 2048 bytes. */
153 /* max number of USB bus supported */
154 #define MON_BIN_MAX_MINOR 128
157 * The buffer: map of used pages.
165 * This gets associated with an open file struct.
167 struct mon_reader_bin
{
168 /* The buffer: one per open. */
169 spinlock_t b_lock
; /* Protect b_cnt, b_in */
170 unsigned int b_size
; /* Current size of the buffer - bytes */
171 unsigned int b_cnt
; /* Bytes used */
172 unsigned int b_in
, b_out
; /* Offsets into buffer - bytes */
173 unsigned int b_read
; /* Amount of read data in curr. pkt. */
174 struct mon_pgmap
*b_vec
; /* The map array */
175 wait_queue_head_t b_wait
; /* Wait for data here */
177 struct mutex fetch_lock
; /* Protect b_read, b_out */
180 /* A list of these is needed for "bus 0". Some time later. */
184 unsigned int cnt_lost
;
187 static inline struct mon_bin_hdr
*MON_OFF2HDR(const struct mon_reader_bin
*rp
,
190 return (struct mon_bin_hdr
*)
191 (rp
->b_vec
[offset
/ CHUNK_SIZE
].ptr
+ offset
% CHUNK_SIZE
);
194 #define MON_RING_EMPTY(rp) ((rp)->b_cnt == 0)
196 static unsigned char xfer_to_pipe
[4] = {
197 PIPE_CONTROL
, PIPE_ISOCHRONOUS
, PIPE_BULK
, PIPE_INTERRUPT
200 static struct class *mon_bin_class
;
201 static dev_t mon_bin_dev0
;
202 static struct cdev mon_bin_cdev
;
204 static void mon_buff_area_fill(const struct mon_reader_bin
*rp
,
205 unsigned int offset
, unsigned int size
);
206 static int mon_bin_wait_event(struct file
*file
, struct mon_reader_bin
*rp
);
207 static int mon_alloc_buff(struct mon_pgmap
*map
, int npages
);
208 static void mon_free_buff(struct mon_pgmap
*map
, int npages
);
211 * This is a "chunked memcpy". It does not manipulate any counters.
213 static unsigned int mon_copy_to_buff(const struct mon_reader_bin
*this,
214 unsigned int off
, const unsigned char *from
, unsigned int length
)
216 unsigned int step_len
;
218 unsigned int in_page
;
222 * Determine step_len.
225 in_page
= CHUNK_SIZE
- (off
& (CHUNK_SIZE
-1));
226 if (in_page
< step_len
)
230 * Copy data and advance pointers.
232 buf
= this->b_vec
[off
/ CHUNK_SIZE
].ptr
+ off
% CHUNK_SIZE
;
233 memcpy(buf
, from
, step_len
);
234 if ((off
+= step_len
) >= this->b_size
) off
= 0;
242 * This is a little worse than the above because it's "chunked copy_to_user".
243 * The return value is an error code, not an offset.
245 static int copy_from_buf(const struct mon_reader_bin
*this, unsigned int off
,
246 char __user
*to
, int length
)
248 unsigned int step_len
;
250 unsigned int in_page
;
254 * Determine step_len.
257 in_page
= CHUNK_SIZE
- (off
& (CHUNK_SIZE
-1));
258 if (in_page
< step_len
)
262 * Copy data and advance pointers.
264 buf
= this->b_vec
[off
/ CHUNK_SIZE
].ptr
+ off
% CHUNK_SIZE
;
265 if (copy_to_user(to
, buf
, step_len
))
267 if ((off
+= step_len
) >= this->b_size
) off
= 0;
275 * Allocate an (aligned) area in the buffer.
276 * This is called under b_lock.
277 * Returns ~0 on failure.
279 static unsigned int mon_buff_area_alloc(struct mon_reader_bin
*rp
,
284 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
285 if (rp
->b_cnt
+ size
> rp
->b_size
)
289 if ((rp
->b_in
+= size
) >= rp
->b_size
)
290 rp
->b_in
-= rp
->b_size
;
295 * This is the same thing as mon_buff_area_alloc, only it does not allow
296 * buffers to wrap. This is needed by applications which pass references
297 * into mmap-ed buffers up their stacks (libpcap can do that).
299 * Currently, we always have the header stuck with the data, although
300 * it is not strictly speaking necessary.
302 * When a buffer would wrap, we place a filler packet to mark the space.
304 static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin
*rp
,
308 unsigned int fill_size
;
310 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
311 if (rp
->b_cnt
+ size
> rp
->b_size
)
313 if (rp
->b_in
+ size
> rp
->b_size
) {
315 * This would wrap. Find if we still have space after
316 * skipping to the end of the buffer. If we do, place
317 * a filler packet and allocate a new packet.
319 fill_size
= rp
->b_size
- rp
->b_in
;
320 if (rp
->b_cnt
+ size
+ fill_size
> rp
->b_size
)
322 mon_buff_area_fill(rp
, rp
->b_in
, fill_size
);
326 rp
->b_cnt
+= size
+ fill_size
;
327 } else if (rp
->b_in
+ size
== rp
->b_size
) {
340 * Return a few (kilo-)bytes to the head of the buffer.
341 * This is used if a data fetch fails.
343 static void mon_buff_area_shrink(struct mon_reader_bin
*rp
, unsigned int size
)
346 /* size &= ~(PKT_ALIGN-1); -- we're called with aligned size */
349 rp
->b_in
+= rp
->b_size
;
354 * This has to be called under both b_lock and fetch_lock, because
355 * it accesses both b_cnt and b_out.
357 static void mon_buff_area_free(struct mon_reader_bin
*rp
, unsigned int size
)
360 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
362 if ((rp
->b_out
+= size
) >= rp
->b_size
)
363 rp
->b_out
-= rp
->b_size
;
366 static void mon_buff_area_fill(const struct mon_reader_bin
*rp
,
367 unsigned int offset
, unsigned int size
)
369 struct mon_bin_hdr
*ep
;
371 ep
= MON_OFF2HDR(rp
, offset
);
372 memset(ep
, 0, PKT_SIZE
);
374 ep
->len_cap
= size
- PKT_SIZE
;
377 static inline char mon_bin_get_setup(unsigned char *setupb
,
378 const struct urb
*urb
, char ev_type
)
381 if (urb
->setup_packet
== NULL
)
383 memcpy(setupb
, urb
->setup_packet
, SETUP_LEN
);
387 static unsigned int mon_bin_get_data(const struct mon_reader_bin
*rp
,
388 unsigned int offset
, struct urb
*urb
, unsigned int length
,
392 struct scatterlist
*sg
;
393 unsigned int this_len
;
396 if (urb
->num_sgs
== 0) {
397 if (urb
->transfer_buffer
== NULL
) {
401 mon_copy_to_buff(rp
, offset
, urb
->transfer_buffer
, length
);
405 /* If IOMMU coalescing occurred, we cannot trust sg_page */
406 if (urb
->transfer_flags
& URB_DMA_SG_COMBINED
) {
411 /* Copy up to the first non-addressable segment */
412 for_each_sg(urb
->sg
, sg
, urb
->num_sgs
, i
) {
413 if (length
== 0 || PageHighMem(sg_page(sg
)))
415 this_len
= min_t(unsigned int, sg
->length
, length
);
416 offset
= mon_copy_to_buff(rp
, offset
, sg_virt(sg
),
427 static void mon_bin_get_isodesc(const struct mon_reader_bin
*rp
,
428 unsigned int offset
, struct urb
*urb
, char ev_type
, unsigned int ndesc
)
430 struct mon_bin_isodesc
*dp
;
431 struct usb_iso_packet_descriptor
*fp
;
433 fp
= urb
->iso_frame_desc
;
434 while (ndesc
-- != 0) {
435 dp
= (struct mon_bin_isodesc
*)
436 (rp
->b_vec
[offset
/ CHUNK_SIZE
].ptr
+ offset
% CHUNK_SIZE
);
437 dp
->iso_status
= fp
->status
;
438 dp
->iso_off
= fp
->offset
;
439 dp
->iso_len
= (ev_type
== 'S') ? fp
->length
: fp
->actual_length
;
441 if ((offset
+= sizeof(struct mon_bin_isodesc
)) >= rp
->b_size
)
447 static void mon_bin_event(struct mon_reader_bin
*rp
, struct urb
*urb
,
448 char ev_type
, int status
)
450 const struct usb_endpoint_descriptor
*epd
= &urb
->ep
->desc
;
453 unsigned int urb_length
;
457 unsigned int ndesc
, lendesc
;
459 struct mon_bin_hdr
*ep
;
462 do_gettimeofday(&ts
);
464 spin_lock_irqsave(&rp
->b_lock
, flags
);
467 * Find the maximum allowable length, then allocate space.
469 if (usb_endpoint_xfer_isoc(epd
)) {
470 if (urb
->number_of_packets
< 0) {
472 } else if (urb
->number_of_packets
>= ISODESC_MAX
) {
475 ndesc
= urb
->number_of_packets
;
480 lendesc
= ndesc
*sizeof(struct mon_bin_isodesc
);
482 urb_length
= (ev_type
== 'S') ?
483 urb
->transfer_buffer_length
: urb
->actual_length
;
486 if (length
>= rp
->b_size
/5)
487 length
= rp
->b_size
/5;
489 if (usb_urb_dir_in(urb
)) {
490 if (ev_type
== 'S') {
494 /* Cannot rely on endpoint number in case of control ep.0 */
497 if (ev_type
== 'C') {
504 if (rp
->mmap_active
) {
505 offset
= mon_buff_area_alloc_contiguous(rp
,
506 length
+ PKT_SIZE
+ lendesc
);
508 offset
= mon_buff_area_alloc(rp
, length
+ PKT_SIZE
+ lendesc
);
512 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
516 ep
= MON_OFF2HDR(rp
, offset
);
517 if ((offset
+= PKT_SIZE
) >= rp
->b_size
) offset
= 0;
520 * Fill the allocated area.
522 memset(ep
, 0, PKT_SIZE
);
524 ep
->xfer_type
= xfer_to_pipe
[usb_endpoint_type(epd
)];
525 ep
->epnum
= dir
| usb_endpoint_num(epd
);
526 ep
->devnum
= urb
->dev
->devnum
;
527 ep
->busnum
= urb
->dev
->bus
->busnum
;
528 ep
->id
= (unsigned long) urb
;
529 ep
->ts_sec
= ts
.tv_sec
;
530 ep
->ts_usec
= ts
.tv_usec
;
532 ep
->len_urb
= urb_length
;
533 ep
->len_cap
= length
+ lendesc
;
534 ep
->xfer_flags
= urb
->transfer_flags
;
536 if (usb_endpoint_xfer_int(epd
)) {
537 ep
->interval
= urb
->interval
;
538 } else if (usb_endpoint_xfer_isoc(epd
)) {
539 ep
->interval
= urb
->interval
;
540 ep
->start_frame
= urb
->start_frame
;
541 ep
->s
.iso
.error_count
= urb
->error_count
;
542 ep
->s
.iso
.numdesc
= urb
->number_of_packets
;
545 if (usb_endpoint_xfer_control(epd
) && ev_type
== 'S') {
546 ep
->flag_setup
= mon_bin_get_setup(ep
->s
.setup
, urb
, ev_type
);
548 ep
->flag_setup
= '-';
553 mon_bin_get_isodesc(rp
, offset
, urb
, ev_type
, ndesc
);
554 if ((offset
+= lendesc
) >= rp
->b_size
)
555 offset
-= rp
->b_size
;
559 length
= mon_bin_get_data(rp
, offset
, urb
, length
,
562 delta
= (ep
->len_cap
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
563 ep
->len_cap
-= length
;
564 delta
-= (ep
->len_cap
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
565 mon_buff_area_shrink(rp
, delta
);
568 ep
->flag_data
= data_tag
;
571 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
573 wake_up(&rp
->b_wait
);
576 static void mon_bin_submit(void *data
, struct urb
*urb
)
578 struct mon_reader_bin
*rp
= data
;
579 mon_bin_event(rp
, urb
, 'S', -EINPROGRESS
);
582 static void mon_bin_complete(void *data
, struct urb
*urb
, int status
)
584 struct mon_reader_bin
*rp
= data
;
585 mon_bin_event(rp
, urb
, 'C', status
);
588 static void mon_bin_error(void *data
, struct urb
*urb
, int error
)
590 struct mon_reader_bin
*rp
= data
;
594 struct mon_bin_hdr
*ep
;
596 do_gettimeofday(&ts
);
598 spin_lock_irqsave(&rp
->b_lock
, flags
);
600 offset
= mon_buff_area_alloc(rp
, PKT_SIZE
);
602 /* Not incrementing cnt_lost. Just because. */
603 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
607 ep
= MON_OFF2HDR(rp
, offset
);
609 memset(ep
, 0, PKT_SIZE
);
611 ep
->xfer_type
= xfer_to_pipe
[usb_endpoint_type(&urb
->ep
->desc
)];
612 ep
->epnum
= usb_urb_dir_in(urb
) ? USB_DIR_IN
: 0;
613 ep
->epnum
|= usb_endpoint_num(&urb
->ep
->desc
);
614 ep
->devnum
= urb
->dev
->devnum
;
615 ep
->busnum
= urb
->dev
->bus
->busnum
;
616 ep
->id
= (unsigned long) urb
;
617 ep
->ts_sec
= ts
.tv_sec
;
618 ep
->ts_usec
= ts
.tv_usec
;
621 ep
->flag_setup
= '-';
624 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
626 wake_up(&rp
->b_wait
);
629 static int mon_bin_open(struct inode
*inode
, struct file
*file
)
631 struct mon_bus
*mbus
;
632 struct mon_reader_bin
*rp
;
636 mutex_lock(&mon_lock
);
637 if ((mbus
= mon_bus_lookup(iminor(inode
))) == NULL
) {
638 mutex_unlock(&mon_lock
);
641 if (mbus
!= &mon_bus0
&& mbus
->u_bus
== NULL
) {
642 printk(KERN_ERR TAG
": consistency error on open\n");
643 mutex_unlock(&mon_lock
);
647 rp
= kzalloc(sizeof(struct mon_reader_bin
), GFP_KERNEL
);
652 spin_lock_init(&rp
->b_lock
);
653 init_waitqueue_head(&rp
->b_wait
);
654 mutex_init(&rp
->fetch_lock
);
655 rp
->b_size
= BUFF_DFL
;
657 size
= sizeof(struct mon_pgmap
) * (rp
->b_size
/CHUNK_SIZE
);
658 if ((rp
->b_vec
= kzalloc(size
, GFP_KERNEL
)) == NULL
) {
663 if ((rc
= mon_alloc_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
)) < 0)
668 rp
->r
.rnf_submit
= mon_bin_submit
;
669 rp
->r
.rnf_error
= mon_bin_error
;
670 rp
->r
.rnf_complete
= mon_bin_complete
;
672 mon_reader_add(mbus
, &rp
->r
);
674 file
->private_data
= rp
;
675 mutex_unlock(&mon_lock
);
683 mutex_unlock(&mon_lock
);
688 * Extract an event from buffer and copy it to user space.
689 * Wait if there is no event ready.
690 * Returns zero or error.
692 static int mon_bin_get_event(struct file
*file
, struct mon_reader_bin
*rp
,
693 struct mon_bin_hdr __user
*hdr
, unsigned int hdrbytes
,
694 void __user
*data
, unsigned int nbytes
)
697 struct mon_bin_hdr
*ep
;
702 mutex_lock(&rp
->fetch_lock
);
704 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
705 mutex_unlock(&rp
->fetch_lock
);
709 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
711 if (copy_to_user(hdr
, ep
, hdrbytes
)) {
712 mutex_unlock(&rp
->fetch_lock
);
716 step_len
= min(ep
->len_cap
, nbytes
);
717 if ((offset
= rp
->b_out
+ PKT_SIZE
) >= rp
->b_size
) offset
= 0;
719 if (copy_from_buf(rp
, offset
, data
, step_len
)) {
720 mutex_unlock(&rp
->fetch_lock
);
724 spin_lock_irqsave(&rp
->b_lock
, flags
);
725 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
726 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
729 mutex_unlock(&rp
->fetch_lock
);
733 static int mon_bin_release(struct inode
*inode
, struct file
*file
)
735 struct mon_reader_bin
*rp
= file
->private_data
;
736 struct mon_bus
* mbus
= rp
->r
.m_bus
;
738 mutex_lock(&mon_lock
);
740 if (mbus
->nreaders
<= 0) {
741 printk(KERN_ERR TAG
": consistency error on close\n");
742 mutex_unlock(&mon_lock
);
745 mon_reader_del(mbus
, &rp
->r
);
747 mon_free_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
);
751 mutex_unlock(&mon_lock
);
755 static ssize_t
mon_bin_read(struct file
*file
, char __user
*buf
,
756 size_t nbytes
, loff_t
*ppos
)
758 struct mon_reader_bin
*rp
= file
->private_data
;
759 unsigned int hdrbytes
= PKT_SZ_API0
;
761 struct mon_bin_hdr
*ep
;
768 mutex_lock(&rp
->fetch_lock
);
770 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
771 mutex_unlock(&rp
->fetch_lock
);
775 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
777 if (rp
->b_read
< hdrbytes
) {
778 step_len
= min(nbytes
, (size_t)(hdrbytes
- rp
->b_read
));
779 ptr
= ((char *)ep
) + rp
->b_read
;
780 if (step_len
&& copy_to_user(buf
, ptr
, step_len
)) {
781 mutex_unlock(&rp
->fetch_lock
);
786 rp
->b_read
+= step_len
;
790 if (rp
->b_read
>= hdrbytes
) {
791 step_len
= ep
->len_cap
;
792 step_len
-= rp
->b_read
- hdrbytes
;
793 if (step_len
> nbytes
)
795 offset
= rp
->b_out
+ PKT_SIZE
;
796 offset
+= rp
->b_read
- hdrbytes
;
797 if (offset
>= rp
->b_size
)
798 offset
-= rp
->b_size
;
799 if (copy_from_buf(rp
, offset
, buf
, step_len
)) {
800 mutex_unlock(&rp
->fetch_lock
);
805 rp
->b_read
+= step_len
;
810 * Check if whole packet was read, and if so, jump to the next one.
812 if (rp
->b_read
>= hdrbytes
+ ep
->len_cap
) {
813 spin_lock_irqsave(&rp
->b_lock
, flags
);
814 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
815 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
819 mutex_unlock(&rp
->fetch_lock
);
824 * Remove at most nevents from chunked buffer.
825 * Returns the number of removed events.
827 static int mon_bin_flush(struct mon_reader_bin
*rp
, unsigned nevents
)
830 struct mon_bin_hdr
*ep
;
833 mutex_lock(&rp
->fetch_lock
);
834 spin_lock_irqsave(&rp
->b_lock
, flags
);
835 for (i
= 0; i
< nevents
; ++i
) {
836 if (MON_RING_EMPTY(rp
))
839 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
840 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
842 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
844 mutex_unlock(&rp
->fetch_lock
);
849 * Fetch at most max event offsets into the buffer and put them into vec.
850 * The events are usually freed later with mon_bin_flush.
851 * Return the effective number of events fetched.
853 static int mon_bin_fetch(struct file
*file
, struct mon_reader_bin
*rp
,
854 u32 __user
*vec
, unsigned int max
)
856 unsigned int cur_out
;
857 unsigned int bytes
, avail
;
859 unsigned int nevents
;
860 struct mon_bin_hdr
*ep
;
864 mutex_lock(&rp
->fetch_lock
);
866 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
867 mutex_unlock(&rp
->fetch_lock
);
871 spin_lock_irqsave(&rp
->b_lock
, flags
);
873 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
878 while (bytes
< avail
) {
882 ep
= MON_OFF2HDR(rp
, cur_out
);
883 if (put_user(cur_out
, &vec
[nevents
])) {
884 mutex_unlock(&rp
->fetch_lock
);
889 size
= ep
->len_cap
+ PKT_SIZE
;
890 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
891 if ((cur_out
+= size
) >= rp
->b_size
)
892 cur_out
-= rp
->b_size
;
896 mutex_unlock(&rp
->fetch_lock
);
901 * Count events. This is almost the same as the above mon_bin_fetch,
902 * only we do not store offsets into user vector, and we have no limit.
904 static int mon_bin_queued(struct mon_reader_bin
*rp
)
906 unsigned int cur_out
;
907 unsigned int bytes
, avail
;
909 unsigned int nevents
;
910 struct mon_bin_hdr
*ep
;
913 mutex_lock(&rp
->fetch_lock
);
915 spin_lock_irqsave(&rp
->b_lock
, flags
);
917 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
922 while (bytes
< avail
) {
923 ep
= MON_OFF2HDR(rp
, cur_out
);
926 size
= ep
->len_cap
+ PKT_SIZE
;
927 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
928 if ((cur_out
+= size
) >= rp
->b_size
)
929 cur_out
-= rp
->b_size
;
933 mutex_unlock(&rp
->fetch_lock
);
939 static long mon_bin_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
941 struct mon_reader_bin
*rp
= file
->private_data
;
942 // struct mon_bus* mbus = rp->r.m_bus;
944 struct mon_bin_hdr
*ep
;
949 case MON_IOCQ_URB_LEN
:
951 * N.B. This only returns the size of data, without the header.
953 spin_lock_irqsave(&rp
->b_lock
, flags
);
954 if (!MON_RING_EMPTY(rp
)) {
955 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
958 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
961 case MON_IOCQ_RING_SIZE
:
965 case MON_IOCT_RING_SIZE
:
967 * Changing the buffer size will flush it's contents; the new
968 * buffer is allocated before releasing the old one to be sure
969 * the device will stay functional also in case of memory
974 struct mon_pgmap
*vec
;
976 if (arg
< BUFF_MIN
|| arg
> BUFF_MAX
)
979 size
= CHUNK_ALIGN(arg
);
980 if ((vec
= kzalloc(sizeof(struct mon_pgmap
) * (size
/CHUNK_SIZE
),
981 GFP_KERNEL
)) == NULL
) {
986 ret
= mon_alloc_buff(vec
, size
/CHUNK_SIZE
);
992 mutex_lock(&rp
->fetch_lock
);
993 spin_lock_irqsave(&rp
->b_lock
, flags
);
994 mon_free_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
);
998 rp
->b_read
= rp
->b_in
= rp
->b_out
= rp
->b_cnt
= 0;
1000 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1001 mutex_unlock(&rp
->fetch_lock
);
1005 case MON_IOCH_MFLUSH
:
1006 ret
= mon_bin_flush(rp
, arg
);
1012 struct mon_bin_get getb
;
1014 if (copy_from_user(&getb
, (void __user
*)arg
,
1015 sizeof(struct mon_bin_get
)))
1018 if (getb
.alloc
> 0x10000000) /* Want to cast to u32 */
1020 ret
= mon_bin_get_event(file
, rp
, getb
.hdr
,
1021 (cmd
== MON_IOCX_GET
)? PKT_SZ_API0
: PKT_SZ_API1
,
1022 getb
.data
, (unsigned int)getb
.alloc
);
1026 case MON_IOCX_MFETCH
:
1028 struct mon_bin_mfetch mfetch
;
1029 struct mon_bin_mfetch __user
*uptr
;
1031 uptr
= (struct mon_bin_mfetch __user
*)arg
;
1033 if (copy_from_user(&mfetch
, uptr
, sizeof(mfetch
)))
1036 if (mfetch
.nflush
) {
1037 ret
= mon_bin_flush(rp
, mfetch
.nflush
);
1040 if (put_user(ret
, &uptr
->nflush
))
1043 ret
= mon_bin_fetch(file
, rp
, mfetch
.offvec
, mfetch
.nfetch
);
1046 if (put_user(ret
, &uptr
->nfetch
))
1052 case MON_IOCG_STATS
: {
1053 struct mon_bin_stats __user
*sp
;
1054 unsigned int nevents
;
1055 unsigned int ndropped
;
1057 spin_lock_irqsave(&rp
->b_lock
, flags
);
1058 ndropped
= rp
->cnt_lost
;
1060 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1061 nevents
= mon_bin_queued(rp
);
1063 sp
= (struct mon_bin_stats __user
*)arg
;
1064 if (put_user(rp
->cnt_lost
, &sp
->dropped
))
1066 if (put_user(nevents
, &sp
->queued
))
1079 #ifdef CONFIG_COMPAT
1080 static long mon_bin_compat_ioctl(struct file
*file
,
1081 unsigned int cmd
, unsigned long arg
)
1083 struct mon_reader_bin
*rp
= file
->private_data
;
1088 case MON_IOCX_GET32
:
1089 case MON_IOCX_GETX32
:
1091 struct mon_bin_get32 getb
;
1093 if (copy_from_user(&getb
, (void __user
*)arg
,
1094 sizeof(struct mon_bin_get32
)))
1097 ret
= mon_bin_get_event(file
, rp
, compat_ptr(getb
.hdr32
),
1098 (cmd
== MON_IOCX_GET32
)? PKT_SZ_API0
: PKT_SZ_API1
,
1099 compat_ptr(getb
.data32
), getb
.alloc32
);
1105 case MON_IOCX_MFETCH32
:
1107 struct mon_bin_mfetch32 mfetch
;
1108 struct mon_bin_mfetch32 __user
*uptr
;
1110 uptr
= (struct mon_bin_mfetch32 __user
*) compat_ptr(arg
);
1112 if (copy_from_user(&mfetch
, uptr
, sizeof(mfetch
)))
1115 if (mfetch
.nflush32
) {
1116 ret
= mon_bin_flush(rp
, mfetch
.nflush32
);
1119 if (put_user(ret
, &uptr
->nflush32
))
1122 ret
= mon_bin_fetch(file
, rp
, compat_ptr(mfetch
.offvec32
),
1126 if (put_user(ret
, &uptr
->nfetch32
))
1131 case MON_IOCG_STATS
:
1132 return mon_bin_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
1134 case MON_IOCQ_URB_LEN
:
1135 case MON_IOCQ_RING_SIZE
:
1136 case MON_IOCT_RING_SIZE
:
1137 case MON_IOCH_MFLUSH
:
1138 return mon_bin_ioctl(file
, cmd
, arg
);
1145 #endif /* CONFIG_COMPAT */
1148 mon_bin_poll(struct file
*file
, struct poll_table_struct
*wait
)
1150 struct mon_reader_bin
*rp
= file
->private_data
;
1151 unsigned int mask
= 0;
1152 unsigned long flags
;
1154 if (file
->f_mode
& FMODE_READ
)
1155 poll_wait(file
, &rp
->b_wait
, wait
);
1157 spin_lock_irqsave(&rp
->b_lock
, flags
);
1158 if (!MON_RING_EMPTY(rp
))
1159 mask
|= POLLIN
| POLLRDNORM
; /* readable */
1160 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1165 * open and close: just keep track of how many times the device is
1166 * mapped, to use the proper memory allocation function.
1168 static void mon_bin_vma_open(struct vm_area_struct
*vma
)
1170 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1174 static void mon_bin_vma_close(struct vm_area_struct
*vma
)
1176 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1181 * Map ring pages to user space.
1183 static int mon_bin_vma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1185 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1186 unsigned long offset
, chunk_idx
;
1187 struct page
*pageptr
;
1189 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
1190 if (offset
>= rp
->b_size
)
1191 return VM_FAULT_SIGBUS
;
1192 chunk_idx
= offset
/ CHUNK_SIZE
;
1193 pageptr
= rp
->b_vec
[chunk_idx
].pg
;
1195 vmf
->page
= pageptr
;
1199 static const struct vm_operations_struct mon_bin_vm_ops
= {
1200 .open
= mon_bin_vma_open
,
1201 .close
= mon_bin_vma_close
,
1202 .fault
= mon_bin_vma_fault
,
1205 static int mon_bin_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1207 /* don't do anything here: "fault" will set up page table entries */
1208 vma
->vm_ops
= &mon_bin_vm_ops
;
1209 vma
->vm_flags
|= VM_RESERVED
;
1210 vma
->vm_private_data
= filp
->private_data
;
1211 mon_bin_vma_open(vma
);
1215 static const struct file_operations mon_fops_binary
= {
1216 .owner
= THIS_MODULE
,
1217 .open
= mon_bin_open
,
1218 .llseek
= no_llseek
,
1219 .read
= mon_bin_read
,
1220 /* .write = mon_text_write, */
1221 .poll
= mon_bin_poll
,
1222 .unlocked_ioctl
= mon_bin_ioctl
,
1223 #ifdef CONFIG_COMPAT
1224 .compat_ioctl
= mon_bin_compat_ioctl
,
1226 .release
= mon_bin_release
,
1227 .mmap
= mon_bin_mmap
,
1230 static int mon_bin_wait_event(struct file
*file
, struct mon_reader_bin
*rp
)
1232 DECLARE_WAITQUEUE(waita
, current
);
1233 unsigned long flags
;
1235 add_wait_queue(&rp
->b_wait
, &waita
);
1236 set_current_state(TASK_INTERRUPTIBLE
);
1238 spin_lock_irqsave(&rp
->b_lock
, flags
);
1239 while (MON_RING_EMPTY(rp
)) {
1240 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1242 if (file
->f_flags
& O_NONBLOCK
) {
1243 set_current_state(TASK_RUNNING
);
1244 remove_wait_queue(&rp
->b_wait
, &waita
);
1245 return -EWOULDBLOCK
; /* Same as EAGAIN in Linux */
1248 if (signal_pending(current
)) {
1249 remove_wait_queue(&rp
->b_wait
, &waita
);
1252 set_current_state(TASK_INTERRUPTIBLE
);
1254 spin_lock_irqsave(&rp
->b_lock
, flags
);
1256 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1258 set_current_state(TASK_RUNNING
);
1259 remove_wait_queue(&rp
->b_wait
, &waita
);
1263 static int mon_alloc_buff(struct mon_pgmap
*map
, int npages
)
1266 unsigned long vaddr
;
1268 for (n
= 0; n
< npages
; n
++) {
1269 vaddr
= get_zeroed_page(GFP_KERNEL
);
1272 free_page((unsigned long) map
[n
].ptr
);
1275 map
[n
].ptr
= (unsigned char *) vaddr
;
1276 map
[n
].pg
= virt_to_page((void *) vaddr
);
1281 static void mon_free_buff(struct mon_pgmap
*map
, int npages
)
1285 for (n
= 0; n
< npages
; n
++)
1286 free_page((unsigned long) map
[n
].ptr
);
1289 int mon_bin_add(struct mon_bus
*mbus
, const struct usb_bus
*ubus
)
1292 unsigned minor
= ubus
? ubus
->busnum
: 0;
1294 if (minor
>= MON_BIN_MAX_MINOR
)
1297 dev
= device_create(mon_bin_class
, ubus
? ubus
->controller
: NULL
,
1298 MKDEV(MAJOR(mon_bin_dev0
), minor
), NULL
,
1303 mbus
->classdev
= dev
;
1307 void mon_bin_del(struct mon_bus
*mbus
)
1309 device_destroy(mon_bin_class
, mbus
->classdev
->devt
);
1312 int __init
mon_bin_init(void)
1316 mon_bin_class
= class_create(THIS_MODULE
, "usbmon");
1317 if (IS_ERR(mon_bin_class
)) {
1318 rc
= PTR_ERR(mon_bin_class
);
1322 rc
= alloc_chrdev_region(&mon_bin_dev0
, 0, MON_BIN_MAX_MINOR
, "usbmon");
1326 cdev_init(&mon_bin_cdev
, &mon_fops_binary
);
1327 mon_bin_cdev
.owner
= THIS_MODULE
;
1329 rc
= cdev_add(&mon_bin_cdev
, mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1336 unregister_chrdev_region(mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1338 class_destroy(mon_bin_class
);
1343 void mon_bin_exit(void)
1345 cdev_del(&mon_bin_cdev
);
1346 unregister_chrdev_region(mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1347 class_destroy(mon_bin_class
);