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 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>
19 #include <asm/uaccess.h>
24 * Defined by USB 2.0 clause 9.3, table 9.2.
29 #define MON_IOC_MAGIC 0x92
31 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
32 /* #2 used to be MON_IOCX_URB, removed before it got into Linus tree */
33 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
34 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
35 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
36 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
37 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
38 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
40 #define MON_IOCX_GET32 _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get32)
41 #define MON_IOCX_MFETCH32 _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch32)
45 * Some architectures have enormous basic pages (16KB for ia64, 64KB for ppc).
46 * But it's all right. Just use a simple way to make sure the chunk is never
47 * smaller than a page.
49 * N.B. An application does not know our chunk size.
51 * Woops, get_zeroed_page() returns a single page. I guess we're stuck with
52 * page-sized chunks for the time being.
54 #define CHUNK_SIZE PAGE_SIZE
55 #define CHUNK_ALIGN(x) (((x)+CHUNK_SIZE-1) & ~(CHUNK_SIZE-1))
58 * The magic limit was calculated so that it allows the monitoring
59 * application to pick data once in two ticks. This way, another application,
60 * which presumably drives the bus, gets to hog CPU, yet we collect our data.
61 * If HZ is 100, a 480 mbit/s bus drives 614 KB every jiffy. USB has an
62 * enormous overhead built into the bus protocol, so we need about 1000 KB.
64 * This is still too much for most cases, where we just snoop a few
65 * descriptor fetches for enumeration. So, the default is a "reasonable"
66 * amount for systems with HZ=250 and incomplete bus saturation.
68 * XXX What about multi-megabyte URBs which take minutes to transfer?
70 #define BUFF_MAX CHUNK_ALIGN(1200*1024)
71 #define BUFF_DFL CHUNK_ALIGN(300*1024)
72 #define BUFF_MIN CHUNK_ALIGN(8*1024)
75 * The per-event API header (2 per URB).
77 * This structure is seen in userland as defined by the documentation.
80 u64 id
; /* URB ID - from submission to callback */
81 unsigned char type
; /* Same as in text API; extensible. */
82 unsigned char xfer_type
; /* ISO, Intr, Control, Bulk */
83 unsigned char epnum
; /* Endpoint number and transfer direction */
84 unsigned char devnum
; /* Device address */
85 unsigned short busnum
; /* Bus number */
88 s64 ts_sec
; /* gettimeofday */
89 s32 ts_usec
; /* gettimeofday */
91 unsigned int len_urb
; /* Length of data (submitted or actual) */
92 unsigned int len_cap
; /* Delivered length */
93 unsigned char setup
[SETUP_LEN
]; /* Only for Control S-type */
96 /* per file statistic */
97 struct mon_bin_stats
{
103 struct mon_bin_hdr __user
*hdr
; /* Only 48 bytes, not 64. */
105 size_t alloc
; /* Length of data (can be zero) */
108 struct mon_bin_mfetch
{
109 u32 __user
*offvec
; /* Vector of events fetched */
110 u32 nfetch
; /* Number of events to fetch (out: fetched) */
111 u32 nflush
; /* Number of events to flush */
115 struct mon_bin_get32
{
121 struct mon_bin_mfetch32
{
128 /* Having these two values same prevents wrapping of the mon_bin_hdr */
132 /* max number of USB bus supported */
133 #define MON_BIN_MAX_MINOR 128
136 * The buffer: map of used pages.
140 unsigned char *ptr
; /* XXX just use page_to_virt everywhere? */
144 * This gets associated with an open file struct.
146 struct mon_reader_bin
{
147 /* The buffer: one per open. */
148 spinlock_t b_lock
; /* Protect b_cnt, b_in */
149 unsigned int b_size
; /* Current size of the buffer - bytes */
150 unsigned int b_cnt
; /* Bytes used */
151 unsigned int b_in
, b_out
; /* Offsets into buffer - bytes */
152 unsigned int b_read
; /* Amount of read data in curr. pkt. */
153 struct mon_pgmap
*b_vec
; /* The map array */
154 wait_queue_head_t b_wait
; /* Wait for data here */
156 struct mutex fetch_lock
; /* Protect b_read, b_out */
159 /* A list of these is needed for "bus 0". Some time later. */
163 unsigned int cnt_lost
;
166 static inline struct mon_bin_hdr
*MON_OFF2HDR(const struct mon_reader_bin
*rp
,
169 return (struct mon_bin_hdr
*)
170 (rp
->b_vec
[offset
/ CHUNK_SIZE
].ptr
+ offset
% CHUNK_SIZE
);
173 #define MON_RING_EMPTY(rp) ((rp)->b_cnt == 0)
175 static dev_t mon_bin_dev0
;
176 static struct cdev mon_bin_cdev
;
178 static void mon_buff_area_fill(const struct mon_reader_bin
*rp
,
179 unsigned int offset
, unsigned int size
);
180 static int mon_bin_wait_event(struct file
*file
, struct mon_reader_bin
*rp
);
181 static int mon_alloc_buff(struct mon_pgmap
*map
, int npages
);
182 static void mon_free_buff(struct mon_pgmap
*map
, int npages
);
185 * This is a "chunked memcpy". It does not manipulate any counters.
186 * But it returns the new offset for repeated application.
188 unsigned int mon_copy_to_buff(const struct mon_reader_bin
*this,
189 unsigned int off
, const unsigned char *from
, unsigned int length
)
191 unsigned int step_len
;
193 unsigned int in_page
;
197 * Determine step_len.
200 in_page
= CHUNK_SIZE
- (off
& (CHUNK_SIZE
-1));
201 if (in_page
< step_len
)
205 * Copy data and advance pointers.
207 buf
= this->b_vec
[off
/ CHUNK_SIZE
].ptr
+ off
% CHUNK_SIZE
;
208 memcpy(buf
, from
, step_len
);
209 if ((off
+= step_len
) >= this->b_size
) off
= 0;
217 * This is a little worse than the above because it's "chunked copy_to_user".
218 * The return value is an error code, not an offset.
220 static int copy_from_buf(const struct mon_reader_bin
*this, unsigned int off
,
221 char __user
*to
, int length
)
223 unsigned int step_len
;
225 unsigned int in_page
;
229 * Determine step_len.
232 in_page
= CHUNK_SIZE
- (off
& (CHUNK_SIZE
-1));
233 if (in_page
< step_len
)
237 * Copy data and advance pointers.
239 buf
= this->b_vec
[off
/ CHUNK_SIZE
].ptr
+ off
% CHUNK_SIZE
;
240 if (copy_to_user(to
, buf
, step_len
))
242 if ((off
+= step_len
) >= this->b_size
) off
= 0;
250 * Allocate an (aligned) area in the buffer.
251 * This is called under b_lock.
252 * Returns ~0 on failure.
254 static unsigned int mon_buff_area_alloc(struct mon_reader_bin
*rp
,
259 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
260 if (rp
->b_cnt
+ size
> rp
->b_size
)
264 if ((rp
->b_in
+= size
) >= rp
->b_size
)
265 rp
->b_in
-= rp
->b_size
;
270 * This is the same thing as mon_buff_area_alloc, only it does not allow
271 * buffers to wrap. This is needed by applications which pass references
272 * into mmap-ed buffers up their stacks (libpcap can do that).
274 * Currently, we always have the header stuck with the data, although
275 * it is not strictly speaking necessary.
277 * When a buffer would wrap, we place a filler packet to mark the space.
279 static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin
*rp
,
283 unsigned int fill_size
;
285 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
286 if (rp
->b_cnt
+ size
> rp
->b_size
)
288 if (rp
->b_in
+ size
> rp
->b_size
) {
290 * This would wrap. Find if we still have space after
291 * skipping to the end of the buffer. If we do, place
292 * a filler packet and allocate a new packet.
294 fill_size
= rp
->b_size
- rp
->b_in
;
295 if (rp
->b_cnt
+ size
+ fill_size
> rp
->b_size
)
297 mon_buff_area_fill(rp
, rp
->b_in
, fill_size
);
301 rp
->b_cnt
+= size
+ fill_size
;
302 } else if (rp
->b_in
+ size
== rp
->b_size
) {
315 * Return a few (kilo-)bytes to the head of the buffer.
316 * This is used if a DMA fetch fails.
318 static void mon_buff_area_shrink(struct mon_reader_bin
*rp
, unsigned int size
)
321 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
324 rp
->b_in
+= rp
->b_size
;
329 * This has to be called under both b_lock and fetch_lock, because
330 * it accesses both b_cnt and b_out.
332 static void mon_buff_area_free(struct mon_reader_bin
*rp
, unsigned int size
)
335 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
337 if ((rp
->b_out
+= size
) >= rp
->b_size
)
338 rp
->b_out
-= rp
->b_size
;
341 static void mon_buff_area_fill(const struct mon_reader_bin
*rp
,
342 unsigned int offset
, unsigned int size
)
344 struct mon_bin_hdr
*ep
;
346 ep
= MON_OFF2HDR(rp
, offset
);
347 memset(ep
, 0, PKT_SIZE
);
349 ep
->len_cap
= size
- PKT_SIZE
;
352 static inline char mon_bin_get_setup(unsigned char *setupb
,
353 const struct urb
*urb
, char ev_type
)
356 if (!usb_pipecontrol(urb
->pipe
) || ev_type
!= 'S')
359 if (urb
->transfer_flags
& URB_NO_SETUP_DMA_MAP
)
360 return mon_dmapeek(setupb
, urb
->setup_dma
, SETUP_LEN
);
361 if (urb
->setup_packet
== NULL
)
364 memcpy(setupb
, urb
->setup_packet
, SETUP_LEN
);
368 static char mon_bin_get_data(const struct mon_reader_bin
*rp
,
369 unsigned int offset
, struct urb
*urb
, unsigned int length
)
372 if (urb
->transfer_flags
& URB_NO_TRANSFER_DMA_MAP
) {
373 mon_dmapeek_vec(rp
, offset
, urb
->transfer_dma
, length
);
377 if (urb
->transfer_buffer
== NULL
)
380 mon_copy_to_buff(rp
, offset
, urb
->transfer_buffer
, length
);
384 static void mon_bin_event(struct mon_reader_bin
*rp
, struct urb
*urb
,
389 unsigned int urb_length
;
392 struct mon_bin_hdr
*ep
;
395 do_gettimeofday(&ts
);
397 spin_lock_irqsave(&rp
->b_lock
, flags
);
400 * Find the maximum allowable length, then allocate space.
402 urb_length
= (ev_type
== 'S') ?
403 urb
->transfer_buffer_length
: urb
->actual_length
;
406 if (length
>= rp
->b_size
/5)
407 length
= rp
->b_size
/5;
409 if (usb_pipein(urb
->pipe
)) {
410 if (ev_type
== 'S') {
415 if (ev_type
== 'C') {
422 offset
= mon_buff_area_alloc_contiguous(rp
, length
+ PKT_SIZE
);
424 offset
= mon_buff_area_alloc(rp
, length
+ PKT_SIZE
);
427 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
431 ep
= MON_OFF2HDR(rp
, offset
);
432 if ((offset
+= PKT_SIZE
) >= rp
->b_size
) offset
= 0;
435 * Fill the allocated area.
437 memset(ep
, 0, PKT_SIZE
);
439 ep
->xfer_type
= usb_pipetype(urb
->pipe
);
440 /* We use the fact that usb_pipein() returns 0x80 */
441 ep
->epnum
= usb_pipeendpoint(urb
->pipe
) | usb_pipein(urb
->pipe
);
442 ep
->devnum
= usb_pipedevice(urb
->pipe
);
443 ep
->busnum
= rp
->r
.m_bus
->u_bus
->busnum
;
444 ep
->id
= (unsigned long) urb
;
445 ep
->ts_sec
= ts
.tv_sec
;
446 ep
->ts_usec
= ts
.tv_usec
;
447 ep
->status
= urb
->status
;
448 ep
->len_urb
= urb_length
;
449 ep
->len_cap
= length
;
451 ep
->flag_setup
= mon_bin_get_setup(ep
->setup
, urb
, ev_type
);
453 ep
->flag_data
= mon_bin_get_data(rp
, offset
, urb
, length
);
454 if (ep
->flag_data
!= 0) { /* Yes, it's 0x00, not '0' */
456 mon_buff_area_shrink(rp
, length
);
459 ep
->flag_data
= data_tag
;
462 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
464 wake_up(&rp
->b_wait
);
467 static void mon_bin_submit(void *data
, struct urb
*urb
)
469 struct mon_reader_bin
*rp
= data
;
470 mon_bin_event(rp
, urb
, 'S');
473 static void mon_bin_complete(void *data
, struct urb
*urb
)
475 struct mon_reader_bin
*rp
= data
;
476 mon_bin_event(rp
, urb
, 'C');
479 static void mon_bin_error(void *data
, struct urb
*urb
, int error
)
481 struct mon_reader_bin
*rp
= data
;
484 struct mon_bin_hdr
*ep
;
486 spin_lock_irqsave(&rp
->b_lock
, flags
);
488 offset
= mon_buff_area_alloc(rp
, PKT_SIZE
);
490 /* Not incrementing cnt_lost. Just because. */
491 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
495 ep
= MON_OFF2HDR(rp
, offset
);
497 memset(ep
, 0, PKT_SIZE
);
499 ep
->xfer_type
= usb_pipetype(urb
->pipe
);
500 /* We use the fact that usb_pipein() returns 0x80 */
501 ep
->epnum
= usb_pipeendpoint(urb
->pipe
) | usb_pipein(urb
->pipe
);
502 ep
->devnum
= usb_pipedevice(urb
->pipe
);
503 ep
->busnum
= rp
->r
.m_bus
->u_bus
->busnum
;
504 ep
->id
= (unsigned long) urb
;
507 ep
->flag_setup
= '-';
510 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
512 wake_up(&rp
->b_wait
);
515 static int mon_bin_open(struct inode
*inode
, struct file
*file
)
517 struct mon_bus
*mbus
;
518 struct usb_bus
*ubus
;
519 struct mon_reader_bin
*rp
;
523 mutex_lock(&mon_lock
);
524 if ((mbus
= mon_bus_lookup(iminor(inode
))) == NULL
) {
525 mutex_unlock(&mon_lock
);
528 if ((ubus
= mbus
->u_bus
) == NULL
) {
529 printk(KERN_ERR TAG
": consistency error on open\n");
530 mutex_unlock(&mon_lock
);
534 rp
= kzalloc(sizeof(struct mon_reader_bin
), GFP_KERNEL
);
539 spin_lock_init(&rp
->b_lock
);
540 init_waitqueue_head(&rp
->b_wait
);
541 mutex_init(&rp
->fetch_lock
);
543 rp
->b_size
= BUFF_DFL
;
545 size
= sizeof(struct mon_pgmap
) * (rp
->b_size
/CHUNK_SIZE
);
546 if ((rp
->b_vec
= kzalloc(size
, GFP_KERNEL
)) == NULL
) {
551 if ((rc
= mon_alloc_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
)) < 0)
556 rp
->r
.rnf_submit
= mon_bin_submit
;
557 rp
->r
.rnf_error
= mon_bin_error
;
558 rp
->r
.rnf_complete
= mon_bin_complete
;
560 mon_reader_add(mbus
, &rp
->r
);
562 file
->private_data
= rp
;
563 mutex_unlock(&mon_lock
);
571 mutex_unlock(&mon_lock
);
576 * Extract an event from buffer and copy it to user space.
577 * Wait if there is no event ready.
578 * Returns zero or error.
580 static int mon_bin_get_event(struct file
*file
, struct mon_reader_bin
*rp
,
581 struct mon_bin_hdr __user
*hdr
, void __user
*data
, unsigned int nbytes
)
584 struct mon_bin_hdr
*ep
;
589 mutex_lock(&rp
->fetch_lock
);
591 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
592 mutex_unlock(&rp
->fetch_lock
);
596 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
598 if (copy_to_user(hdr
, ep
, sizeof(struct mon_bin_hdr
))) {
599 mutex_unlock(&rp
->fetch_lock
);
603 step_len
= min(ep
->len_cap
, nbytes
);
604 if ((offset
= rp
->b_out
+ PKT_SIZE
) >= rp
->b_size
) offset
= 0;
606 if (copy_from_buf(rp
, offset
, data
, step_len
)) {
607 mutex_unlock(&rp
->fetch_lock
);
611 spin_lock_irqsave(&rp
->b_lock
, flags
);
612 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
613 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
616 mutex_unlock(&rp
->fetch_lock
);
620 static int mon_bin_release(struct inode
*inode
, struct file
*file
)
622 struct mon_reader_bin
*rp
= file
->private_data
;
623 struct mon_bus
* mbus
= rp
->r
.m_bus
;
625 mutex_lock(&mon_lock
);
627 if (mbus
->nreaders
<= 0) {
628 printk(KERN_ERR TAG
": consistency error on close\n");
629 mutex_unlock(&mon_lock
);
632 mon_reader_del(mbus
, &rp
->r
);
634 mon_free_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
);
638 mutex_unlock(&mon_lock
);
642 static ssize_t
mon_bin_read(struct file
*file
, char __user
*buf
,
643 size_t nbytes
, loff_t
*ppos
)
645 struct mon_reader_bin
*rp
= file
->private_data
;
647 struct mon_bin_hdr
*ep
;
654 mutex_lock(&rp
->fetch_lock
);
656 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
657 mutex_unlock(&rp
->fetch_lock
);
661 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
663 if (rp
->b_read
< sizeof(struct mon_bin_hdr
)) {
664 step_len
= min(nbytes
, sizeof(struct mon_bin_hdr
) - rp
->b_read
);
665 ptr
= ((char *)ep
) + rp
->b_read
;
666 if (step_len
&& copy_to_user(buf
, ptr
, step_len
)) {
667 mutex_unlock(&rp
->fetch_lock
);
672 rp
->b_read
+= step_len
;
676 if (rp
->b_read
>= sizeof(struct mon_bin_hdr
)) {
677 step_len
= min(nbytes
, (size_t)ep
->len_cap
);
678 offset
= rp
->b_out
+ PKT_SIZE
;
679 offset
+= rp
->b_read
- sizeof(struct mon_bin_hdr
);
680 if (offset
>= rp
->b_size
)
681 offset
-= rp
->b_size
;
682 if (copy_from_buf(rp
, offset
, buf
, step_len
)) {
683 mutex_unlock(&rp
->fetch_lock
);
688 rp
->b_read
+= step_len
;
693 * Check if whole packet was read, and if so, jump to the next one.
695 if (rp
->b_read
>= sizeof(struct mon_bin_hdr
) + ep
->len_cap
) {
696 spin_lock_irqsave(&rp
->b_lock
, flags
);
697 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
698 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
702 mutex_unlock(&rp
->fetch_lock
);
707 * Remove at most nevents from chunked buffer.
708 * Returns the number of removed events.
710 static int mon_bin_flush(struct mon_reader_bin
*rp
, unsigned nevents
)
713 struct mon_bin_hdr
*ep
;
716 mutex_lock(&rp
->fetch_lock
);
717 spin_lock_irqsave(&rp
->b_lock
, flags
);
718 for (i
= 0; i
< nevents
; ++i
) {
719 if (MON_RING_EMPTY(rp
))
722 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
723 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
725 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
727 mutex_unlock(&rp
->fetch_lock
);
732 * Fetch at most max event offsets into the buffer and put them into vec.
733 * The events are usually freed later with mon_bin_flush.
734 * Return the effective number of events fetched.
736 static int mon_bin_fetch(struct file
*file
, struct mon_reader_bin
*rp
,
737 u32 __user
*vec
, unsigned int max
)
739 unsigned int cur_out
;
740 unsigned int bytes
, avail
;
742 unsigned int nevents
;
743 struct mon_bin_hdr
*ep
;
747 mutex_lock(&rp
->fetch_lock
);
749 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
750 mutex_unlock(&rp
->fetch_lock
);
754 spin_lock_irqsave(&rp
->b_lock
, flags
);
756 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
761 while (bytes
< avail
) {
765 ep
= MON_OFF2HDR(rp
, cur_out
);
766 if (put_user(cur_out
, &vec
[nevents
])) {
767 mutex_unlock(&rp
->fetch_lock
);
772 size
= ep
->len_cap
+ PKT_SIZE
;
773 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
774 if ((cur_out
+= size
) >= rp
->b_size
)
775 cur_out
-= rp
->b_size
;
779 mutex_unlock(&rp
->fetch_lock
);
784 * Count events. This is almost the same as the above mon_bin_fetch,
785 * only we do not store offsets into user vector, and we have no limit.
787 static int mon_bin_queued(struct mon_reader_bin
*rp
)
789 unsigned int cur_out
;
790 unsigned int bytes
, avail
;
792 unsigned int nevents
;
793 struct mon_bin_hdr
*ep
;
796 mutex_lock(&rp
->fetch_lock
);
798 spin_lock_irqsave(&rp
->b_lock
, flags
);
800 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
805 while (bytes
< avail
) {
806 ep
= MON_OFF2HDR(rp
, cur_out
);
809 size
= ep
->len_cap
+ PKT_SIZE
;
810 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
811 if ((cur_out
+= size
) >= rp
->b_size
)
812 cur_out
-= rp
->b_size
;
816 mutex_unlock(&rp
->fetch_lock
);
822 static int mon_bin_ioctl(struct inode
*inode
, struct file
*file
,
823 unsigned int cmd
, unsigned long arg
)
825 struct mon_reader_bin
*rp
= file
->private_data
;
826 // struct mon_bus* mbus = rp->r.m_bus;
828 struct mon_bin_hdr
*ep
;
833 case MON_IOCQ_URB_LEN
:
835 * N.B. This only returns the size of data, without the header.
837 spin_lock_irqsave(&rp
->b_lock
, flags
);
838 if (!MON_RING_EMPTY(rp
)) {
839 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
842 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
845 case MON_IOCQ_RING_SIZE
:
849 case MON_IOCT_RING_SIZE
:
851 * Changing the buffer size will flush it's contents; the new
852 * buffer is allocated before releasing the old one to be sure
853 * the device will stay functional also in case of memory
858 struct mon_pgmap
*vec
;
860 if (arg
< BUFF_MIN
|| arg
> BUFF_MAX
)
863 size
= CHUNK_ALIGN(arg
);
864 if ((vec
= kzalloc(sizeof(struct mon_pgmap
) * (size
/CHUNK_SIZE
),
865 GFP_KERNEL
)) == NULL
) {
870 ret
= mon_alloc_buff(vec
, size
/CHUNK_SIZE
);
876 mutex_lock(&rp
->fetch_lock
);
877 spin_lock_irqsave(&rp
->b_lock
, flags
);
878 mon_free_buff(rp
->b_vec
, size
/CHUNK_SIZE
);
882 rp
->b_read
= rp
->b_in
= rp
->b_out
= rp
->b_cnt
= 0;
884 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
885 mutex_unlock(&rp
->fetch_lock
);
889 case MON_IOCH_MFLUSH
:
890 ret
= mon_bin_flush(rp
, arg
);
895 struct mon_bin_get getb
;
897 if (copy_from_user(&getb
, (void __user
*)arg
,
898 sizeof(struct mon_bin_get
)))
901 if (getb
.alloc
> 0x10000000) /* Want to cast to u32 */
903 ret
= mon_bin_get_event(file
, rp
,
904 getb
.hdr
, getb
.data
, (unsigned int)getb
.alloc
);
909 case MON_IOCX_GET32
: {
910 struct mon_bin_get32 getb
;
912 if (copy_from_user(&getb
, (void __user
*)arg
,
913 sizeof(struct mon_bin_get32
)))
916 ret
= mon_bin_get_event(file
, rp
,
917 compat_ptr(getb
.hdr32
), compat_ptr(getb
.data32
),
923 case MON_IOCX_MFETCH
:
925 struct mon_bin_mfetch mfetch
;
926 struct mon_bin_mfetch __user
*uptr
;
928 uptr
= (struct mon_bin_mfetch __user
*)arg
;
930 if (copy_from_user(&mfetch
, uptr
, sizeof(mfetch
)))
934 ret
= mon_bin_flush(rp
, mfetch
.nflush
);
937 if (put_user(ret
, &uptr
->nflush
))
940 ret
= mon_bin_fetch(file
, rp
, mfetch
.offvec
, mfetch
.nfetch
);
943 if (put_user(ret
, &uptr
->nfetch
))
950 case MON_IOCX_MFETCH32
:
952 struct mon_bin_mfetch32 mfetch
;
953 struct mon_bin_mfetch32 __user
*uptr
;
955 uptr
= (struct mon_bin_mfetch32 __user
*) compat_ptr(arg
);
957 if (copy_from_user(&mfetch
, uptr
, sizeof(mfetch
)))
960 if (mfetch
.nflush32
) {
961 ret
= mon_bin_flush(rp
, mfetch
.nflush32
);
964 if (put_user(ret
, &uptr
->nflush32
))
967 ret
= mon_bin_fetch(file
, rp
, compat_ptr(mfetch
.offvec32
),
971 if (put_user(ret
, &uptr
->nfetch32
))
978 case MON_IOCG_STATS
: {
979 struct mon_bin_stats __user
*sp
;
980 unsigned int nevents
;
981 unsigned int ndropped
;
983 spin_lock_irqsave(&rp
->b_lock
, flags
);
984 ndropped
= rp
->cnt_lost
;
986 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
987 nevents
= mon_bin_queued(rp
);
989 sp
= (struct mon_bin_stats __user
*)arg
;
990 if (put_user(rp
->cnt_lost
, &sp
->dropped
))
992 if (put_user(nevents
, &sp
->queued
))
1006 mon_bin_poll(struct file
*file
, struct poll_table_struct
*wait
)
1008 struct mon_reader_bin
*rp
= file
->private_data
;
1009 unsigned int mask
= 0;
1010 unsigned long flags
;
1012 if (file
->f_mode
& FMODE_READ
)
1013 poll_wait(file
, &rp
->b_wait
, wait
);
1015 spin_lock_irqsave(&rp
->b_lock
, flags
);
1016 if (!MON_RING_EMPTY(rp
))
1017 mask
|= POLLIN
| POLLRDNORM
; /* readable */
1018 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1023 * open and close: just keep track of how many times the device is
1024 * mapped, to use the proper memory allocation function.
1026 static void mon_bin_vma_open(struct vm_area_struct
*vma
)
1028 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1032 static void mon_bin_vma_close(struct vm_area_struct
*vma
)
1034 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1039 * Map ring pages to user space.
1041 struct page
*mon_bin_vma_nopage(struct vm_area_struct
*vma
,
1042 unsigned long address
, int *type
)
1044 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1045 unsigned long offset
, chunk_idx
;
1046 struct page
*pageptr
;
1048 offset
= (address
- vma
->vm_start
) + (vma
->vm_pgoff
<< PAGE_SHIFT
);
1049 if (offset
>= rp
->b_size
)
1050 return NOPAGE_SIGBUS
;
1051 chunk_idx
= offset
/ CHUNK_SIZE
;
1052 pageptr
= rp
->b_vec
[chunk_idx
].pg
;
1055 *type
= VM_FAULT_MINOR
;
1059 struct vm_operations_struct mon_bin_vm_ops
= {
1060 .open
= mon_bin_vma_open
,
1061 .close
= mon_bin_vma_close
,
1062 .nopage
= mon_bin_vma_nopage
,
1065 int mon_bin_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1067 /* don't do anything here: "nopage" will set up page table entries */
1068 vma
->vm_ops
= &mon_bin_vm_ops
;
1069 vma
->vm_flags
|= VM_RESERVED
;
1070 vma
->vm_private_data
= filp
->private_data
;
1071 mon_bin_vma_open(vma
);
1075 struct file_operations mon_fops_binary
= {
1076 .owner
= THIS_MODULE
,
1077 .open
= mon_bin_open
,
1078 .llseek
= no_llseek
,
1079 .read
= mon_bin_read
,
1080 /* .write = mon_text_write, */
1081 .poll
= mon_bin_poll
,
1082 .ioctl
= mon_bin_ioctl
,
1083 .release
= mon_bin_release
,
1086 static int mon_bin_wait_event(struct file
*file
, struct mon_reader_bin
*rp
)
1088 DECLARE_WAITQUEUE(waita
, current
);
1089 unsigned long flags
;
1091 add_wait_queue(&rp
->b_wait
, &waita
);
1092 set_current_state(TASK_INTERRUPTIBLE
);
1094 spin_lock_irqsave(&rp
->b_lock
, flags
);
1095 while (MON_RING_EMPTY(rp
)) {
1096 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1098 if (file
->f_flags
& O_NONBLOCK
) {
1099 set_current_state(TASK_RUNNING
);
1100 remove_wait_queue(&rp
->b_wait
, &waita
);
1101 return -EWOULDBLOCK
; /* Same as EAGAIN in Linux */
1104 if (signal_pending(current
)) {
1105 remove_wait_queue(&rp
->b_wait
, &waita
);
1108 set_current_state(TASK_INTERRUPTIBLE
);
1110 spin_lock_irqsave(&rp
->b_lock
, flags
);
1112 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1114 set_current_state(TASK_RUNNING
);
1115 remove_wait_queue(&rp
->b_wait
, &waita
);
1119 static int mon_alloc_buff(struct mon_pgmap
*map
, int npages
)
1122 unsigned long vaddr
;
1124 for (n
= 0; n
< npages
; n
++) {
1125 vaddr
= get_zeroed_page(GFP_KERNEL
);
1128 free_page((unsigned long) map
[n
].ptr
);
1131 map
[n
].ptr
= (unsigned char *) vaddr
;
1132 map
[n
].pg
= virt_to_page(vaddr
);
1137 static void mon_free_buff(struct mon_pgmap
*map
, int npages
)
1141 for (n
= 0; n
< npages
; n
++)
1142 free_page((unsigned long) map
[n
].ptr
);
1145 int __init
mon_bin_init(void)
1149 rc
= alloc_chrdev_region(&mon_bin_dev0
, 0, MON_BIN_MAX_MINOR
, "usbmon");
1153 cdev_init(&mon_bin_cdev
, &mon_fops_binary
);
1154 mon_bin_cdev
.owner
= THIS_MODULE
;
1156 rc
= cdev_add(&mon_bin_cdev
, mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1163 unregister_chrdev_region(mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1168 void __exit
mon_bin_exit(void)
1170 cdev_del(&mon_bin_cdev
);
1171 unregister_chrdev_region(mon_bin_dev0
, MON_BIN_MAX_MINOR
);