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/export.h>
15 #include <linux/usb.h>
16 #include <linux/poll.h>
17 #include <linux/compat.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))
65 * The magic limit was calculated so that it allows the monitoring
66 * application to pick data once in two ticks. This way, another application,
67 * which presumably drives the bus, gets to hog CPU, yet we collect our data.
68 * If HZ is 100, a 480 mbit/s bus drives 614 KB every jiffy. USB has an
69 * enormous overhead built into the bus protocol, so we need about 1000 KB.
71 * This is still too much for most cases, where we just snoop a few
72 * descriptor fetches for enumeration. So, the default is a "reasonable"
73 * amount for systems with HZ=250 and incomplete bus saturation.
75 * XXX What about multi-megabyte URBs which take minutes to transfer?
77 #define BUFF_MAX CHUNK_ALIGN(1200*1024)
78 #define BUFF_DFL CHUNK_ALIGN(300*1024)
79 #define BUFF_MIN CHUNK_ALIGN(8*1024)
82 * The per-event API header (2 per URB).
84 * This structure is seen in userland as defined by the documentation.
87 u64 id; /* URB ID - from submission to callback */
88 unsigned char type; /* Same as in text API; extensible. */
89 unsigned char xfer_type; /* ISO, Intr, Control, Bulk */
90 unsigned char epnum; /* Endpoint number and transfer direction */
91 unsigned char devnum; /* Device address */
92 unsigned short busnum; /* Bus number */
95 s64 ts_sec; /* gettimeofday */
96 s32 ts_usec; /* gettimeofday */
98 unsigned int len_urb; /* Length of data (submitted or actual) */
99 unsigned int len_cap; /* Delivered length */
101 unsigned char setup[SETUP_LEN]; /* Only for Control S-type */
109 unsigned int xfer_flags;
110 unsigned int ndesc; /* Actual number of ISO descriptors */
114 * ISO vector, packed into the head of data stream.
115 * This has to take 16 bytes to make sure that the end of buffer
116 * wrap is not happening in the middle of a descriptor.
118 struct mon_bin_isodesc {
120 unsigned int iso_off;
121 unsigned int iso_len;
125 /* per file statistic */
126 struct mon_bin_stats {
132 struct mon_bin_hdr __user *hdr; /* Can be 48 bytes or 64. */
134 size_t alloc; /* Length of data (can be zero) */
137 struct mon_bin_mfetch {
138 u32 __user *offvec; /* Vector of events fetched */
139 u32 nfetch; /* Number of events to fetch (out: fetched) */
140 u32 nflush; /* Number of events to flush */
144 struct mon_bin_get32 {
150 struct mon_bin_mfetch32 {
157 /* Having these two values same prevents wrapping of the mon_bin_hdr */
161 #define PKT_SZ_API0 48 /* API 0 (2.6.20) size */
162 #define PKT_SZ_API1 64 /* API 1 size: extra fields */
164 #define ISODESC_MAX 128 /* Same number as usbfs allows, 2048 bytes. */
166 /* max number of USB bus supported */
167 #define MON_BIN_MAX_MINOR 128
170 * The buffer: map of used pages.
174 unsigned char *ptr; /* XXX just use page_to_virt everywhere? */
178 * This gets associated with an open file struct.
180 struct mon_reader_bin {
181 /* The buffer: one per open. */
182 spinlock_t b_lock; /* Protect b_cnt, b_in */
183 unsigned int b_size; /* Current size of the buffer - bytes */
184 unsigned int b_cnt; /* Bytes used */
185 unsigned int b_in, b_out; /* Offsets into buffer - bytes */
186 unsigned int b_read; /* Amount of read data in curr. pkt. */
187 struct mon_pgmap *b_vec; /* The map array */
188 wait_queue_head_t b_wait; /* Wait for data here */
190 struct mutex fetch_lock; /* Protect b_read, b_out */
193 /* A list of these is needed for "bus 0". Some time later. */
197 unsigned int cnt_lost;
200 static inline struct mon_bin_hdr *MON_OFF2HDR(const struct mon_reader_bin *rp,
203 return (struct mon_bin_hdr *)
204 (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
207 #define MON_RING_EMPTY(rp) ((rp)->b_cnt == 0)
209 static unsigned char xfer_to_pipe[4] = {
210 PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT
213 static struct class *mon_bin_class;
214 static dev_t mon_bin_dev0;
215 static struct cdev mon_bin_cdev;
217 static void mon_buff_area_fill(const struct mon_reader_bin *rp,
218 unsigned int offset, unsigned int size);
219 static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp);
220 static int mon_alloc_buff(struct mon_pgmap *map, int npages);
221 static void mon_free_buff(struct mon_pgmap *map, int npages);
224 * This is a "chunked memcpy". It does not manipulate any counters.
226 static unsigned int mon_copy_to_buff(const struct mon_reader_bin *this,
227 unsigned int off, const unsigned char *from, unsigned int length)
229 unsigned int step_len;
231 unsigned int in_page;
235 * Determine step_len.
238 in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
239 if (in_page < step_len)
243 * Copy data and advance pointers.
245 buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
246 memcpy(buf, from, step_len);
247 if ((off += step_len) >= this->b_size) off = 0;
255 * This is a little worse than the above because it's "chunked copy_to_user".
256 * The return value is an error code, not an offset.
258 static int copy_from_buf(const struct mon_reader_bin *this, unsigned int off,
259 char __user *to, int length)
261 unsigned int step_len;
263 unsigned int in_page;
267 * Determine step_len.
270 in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
271 if (in_page < step_len)
275 * Copy data and advance pointers.
277 buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
278 if (copy_to_user(to, buf, step_len))
280 if ((off += step_len) >= this->b_size) off = 0;
288 * Allocate an (aligned) area in the buffer.
289 * This is called under b_lock.
290 * Returns ~0 on failure.
292 static unsigned int mon_buff_area_alloc(struct mon_reader_bin *rp,
297 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
298 if (rp->b_cnt + size > rp->b_size)
302 if ((rp->b_in += size) >= rp->b_size)
303 rp->b_in -= rp->b_size;
308 * This is the same thing as mon_buff_area_alloc, only it does not allow
309 * buffers to wrap. This is needed by applications which pass references
310 * into mmap-ed buffers up their stacks (libpcap can do that).
312 * Currently, we always have the header stuck with the data, although
313 * it is not strictly speaking necessary.
315 * When a buffer would wrap, we place a filler packet to mark the space.
317 static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin *rp,
321 unsigned int fill_size;
323 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
324 if (rp->b_cnt + size > rp->b_size)
326 if (rp->b_in + size > rp->b_size) {
328 * This would wrap. Find if we still have space after
329 * skipping to the end of the buffer. If we do, place
330 * a filler packet and allocate a new packet.
332 fill_size = rp->b_size - rp->b_in;
333 if (rp->b_cnt + size + fill_size > rp->b_size)
335 mon_buff_area_fill(rp, rp->b_in, fill_size);
339 rp->b_cnt += size + fill_size;
340 } else if (rp->b_in + size == rp->b_size) {
353 * Return a few (kilo-)bytes to the head of the buffer.
354 * This is used if a data fetch fails.
356 static void mon_buff_area_shrink(struct mon_reader_bin *rp, unsigned int size)
359 /* size &= ~(PKT_ALIGN-1); -- we're called with aligned size */
362 rp->b_in += rp->b_size;
367 * This has to be called under both b_lock and fetch_lock, because
368 * it accesses both b_cnt and b_out.
370 static void mon_buff_area_free(struct mon_reader_bin *rp, unsigned int size)
373 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
375 if ((rp->b_out += size) >= rp->b_size)
376 rp->b_out -= rp->b_size;
379 static void mon_buff_area_fill(const struct mon_reader_bin *rp,
380 unsigned int offset, unsigned int size)
382 struct mon_bin_hdr *ep;
384 ep = MON_OFF2HDR(rp, offset);
385 memset(ep, 0, PKT_SIZE);
387 ep->len_cap = size - PKT_SIZE;
390 static inline char mon_bin_get_setup(unsigned char *setupb,
391 const struct urb *urb, char ev_type)
394 if (urb->setup_packet == NULL)
396 memcpy(setupb, urb->setup_packet, SETUP_LEN);
400 static unsigned int mon_bin_get_data(const struct mon_reader_bin *rp,
401 unsigned int offset, struct urb *urb, unsigned int length,
405 struct scatterlist *sg;
406 unsigned int this_len;
409 if (urb->num_sgs == 0) {
410 if (urb->transfer_buffer == NULL) {
414 mon_copy_to_buff(rp, offset, urb->transfer_buffer, length);
418 /* If IOMMU coalescing occurred, we cannot trust sg_page */
419 if (urb->transfer_flags & URB_DMA_SG_COMBINED) {
424 /* Copy up to the first non-addressable segment */
425 for_each_sg(urb->sg, sg, urb->num_sgs, i) {
426 if (length == 0 || PageHighMem(sg_page(sg)))
428 this_len = min_t(unsigned int, sg->length, length);
429 offset = mon_copy_to_buff(rp, offset, sg_virt(sg),
441 * This is the look-ahead pass in case of 'C Zi', when actual_length cannot
442 * be used to determine the length of the whole contiguous buffer.
444 static unsigned int mon_bin_collate_isodesc(const struct mon_reader_bin *rp,
445 struct urb *urb, unsigned int ndesc)
447 struct usb_iso_packet_descriptor *fp;
451 fp = urb->iso_frame_desc;
452 while (ndesc-- != 0) {
453 if (fp->actual_length != 0) {
454 if (fp->offset + fp->actual_length > length)
455 length = fp->offset + fp->actual_length;
462 static void mon_bin_get_isodesc(const struct mon_reader_bin *rp,
463 unsigned int offset, struct urb *urb, char ev_type, unsigned int ndesc)
465 struct mon_bin_isodesc *dp;
466 struct usb_iso_packet_descriptor *fp;
468 fp = urb->iso_frame_desc;
469 while (ndesc-- != 0) {
470 dp = (struct mon_bin_isodesc *)
471 (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
472 dp->iso_status = fp->status;
473 dp->iso_off = fp->offset;
474 dp->iso_len = (ev_type == 'S') ? fp->length : fp->actual_length;
476 if ((offset += sizeof(struct mon_bin_isodesc)) >= rp->b_size)
482 static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
483 char ev_type, int status)
485 const struct usb_endpoint_descriptor *epd = &urb->ep->desc;
488 unsigned int urb_length;
492 unsigned int ndesc, lendesc;
494 struct mon_bin_hdr *ep;
497 do_gettimeofday(&ts);
499 spin_lock_irqsave(&rp->b_lock, flags);
502 * Find the maximum allowable length, then allocate space.
504 urb_length = (ev_type == 'S') ?
505 urb->transfer_buffer_length : urb->actual_length;
508 if (usb_endpoint_xfer_isoc(epd)) {
509 if (urb->number_of_packets < 0) {
511 } else if (urb->number_of_packets >= ISODESC_MAX) {
514 ndesc = urb->number_of_packets;
516 if (ev_type == 'C' && usb_urb_dir_in(urb))
517 length = mon_bin_collate_isodesc(rp, urb, ndesc);
521 lendesc = ndesc*sizeof(struct mon_bin_isodesc);
523 /* not an issue unless there's a subtle bug in a HCD somewhere */
524 if (length >= urb->transfer_buffer_length)
525 length = urb->transfer_buffer_length;
527 if (length >= rp->b_size/5)
528 length = rp->b_size/5;
530 if (usb_urb_dir_in(urb)) {
531 if (ev_type == 'S') {
535 /* Cannot rely on endpoint number in case of control ep.0 */
538 if (ev_type == 'C') {
545 if (rp->mmap_active) {
546 offset = mon_buff_area_alloc_contiguous(rp,
547 length + PKT_SIZE + lendesc);
549 offset = mon_buff_area_alloc(rp, length + PKT_SIZE + lendesc);
553 spin_unlock_irqrestore(&rp->b_lock, flags);
557 ep = MON_OFF2HDR(rp, offset);
558 if ((offset += PKT_SIZE) >= rp->b_size) offset = 0;
561 * Fill the allocated area.
563 memset(ep, 0, PKT_SIZE);
565 ep->xfer_type = xfer_to_pipe[usb_endpoint_type(epd)];
566 ep->epnum = dir | usb_endpoint_num(epd);
567 ep->devnum = urb->dev->devnum;
568 ep->busnum = urb->dev->bus->busnum;
569 ep->id = (unsigned long) urb;
570 ep->ts_sec = ts.tv_sec;
571 ep->ts_usec = ts.tv_usec;
573 ep->len_urb = urb_length;
574 ep->len_cap = length + lendesc;
575 ep->xfer_flags = urb->transfer_flags;
577 if (usb_endpoint_xfer_int(epd)) {
578 ep->interval = urb->interval;
579 } else if (usb_endpoint_xfer_isoc(epd)) {
580 ep->interval = urb->interval;
581 ep->start_frame = urb->start_frame;
582 ep->s.iso.error_count = urb->error_count;
583 ep->s.iso.numdesc = urb->number_of_packets;
586 if (usb_endpoint_xfer_control(epd) && ev_type == 'S') {
587 ep->flag_setup = mon_bin_get_setup(ep->s.setup, urb, ev_type);
589 ep->flag_setup = '-';
594 mon_bin_get_isodesc(rp, offset, urb, ev_type, ndesc);
595 if ((offset += lendesc) >= rp->b_size)
596 offset -= rp->b_size;
600 length = mon_bin_get_data(rp, offset, urb, length,
603 delta = (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
604 ep->len_cap -= length;
605 delta -= (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
606 mon_buff_area_shrink(rp, delta);
609 ep->flag_data = data_tag;
612 spin_unlock_irqrestore(&rp->b_lock, flags);
614 wake_up(&rp->b_wait);
617 static void mon_bin_submit(void *data, struct urb *urb)
619 struct mon_reader_bin *rp = data;
620 mon_bin_event(rp, urb, 'S', -EINPROGRESS);
623 static void mon_bin_complete(void *data, struct urb *urb, int status)
625 struct mon_reader_bin *rp = data;
626 mon_bin_event(rp, urb, 'C', status);
629 static void mon_bin_error(void *data, struct urb *urb, int error)
631 struct mon_reader_bin *rp = data;
635 struct mon_bin_hdr *ep;
637 do_gettimeofday(&ts);
639 spin_lock_irqsave(&rp->b_lock, flags);
641 offset = mon_buff_area_alloc(rp, PKT_SIZE);
643 /* Not incrementing cnt_lost. Just because. */
644 spin_unlock_irqrestore(&rp->b_lock, flags);
648 ep = MON_OFF2HDR(rp, offset);
650 memset(ep, 0, PKT_SIZE);
652 ep->xfer_type = xfer_to_pipe[usb_endpoint_type(&urb->ep->desc)];
653 ep->epnum = usb_urb_dir_in(urb) ? USB_DIR_IN : 0;
654 ep->epnum |= usb_endpoint_num(&urb->ep->desc);
655 ep->devnum = urb->dev->devnum;
656 ep->busnum = urb->dev->bus->busnum;
657 ep->id = (unsigned long) urb;
658 ep->ts_sec = ts.tv_sec;
659 ep->ts_usec = ts.tv_usec;
662 ep->flag_setup = '-';
665 spin_unlock_irqrestore(&rp->b_lock, flags);
667 wake_up(&rp->b_wait);
670 static int mon_bin_open(struct inode *inode, struct file *file)
672 struct mon_bus *mbus;
673 struct mon_reader_bin *rp;
677 mutex_lock(&mon_lock);
678 if ((mbus = mon_bus_lookup(iminor(inode))) == NULL) {
679 mutex_unlock(&mon_lock);
682 if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
683 printk(KERN_ERR TAG ": consistency error on open\n");
684 mutex_unlock(&mon_lock);
688 rp = kzalloc(sizeof(struct mon_reader_bin), GFP_KERNEL);
693 spin_lock_init(&rp->b_lock);
694 init_waitqueue_head(&rp->b_wait);
695 mutex_init(&rp->fetch_lock);
696 rp->b_size = BUFF_DFL;
698 size = sizeof(struct mon_pgmap) * (rp->b_size/CHUNK_SIZE);
699 if ((rp->b_vec = kzalloc(size, GFP_KERNEL)) == NULL) {
704 if ((rc = mon_alloc_buff(rp->b_vec, rp->b_size/CHUNK_SIZE)) < 0)
709 rp->r.rnf_submit = mon_bin_submit;
710 rp->r.rnf_error = mon_bin_error;
711 rp->r.rnf_complete = mon_bin_complete;
713 mon_reader_add(mbus, &rp->r);
715 file->private_data = rp;
716 mutex_unlock(&mon_lock);
724 mutex_unlock(&mon_lock);
729 * Extract an event from buffer and copy it to user space.
730 * Wait if there is no event ready.
731 * Returns zero or error.
733 static int mon_bin_get_event(struct file *file, struct mon_reader_bin *rp,
734 struct mon_bin_hdr __user *hdr, unsigned int hdrbytes,
735 void __user *data, unsigned int nbytes)
738 struct mon_bin_hdr *ep;
743 mutex_lock(&rp->fetch_lock);
745 if ((rc = mon_bin_wait_event(file, rp)) < 0) {
746 mutex_unlock(&rp->fetch_lock);
750 ep = MON_OFF2HDR(rp, rp->b_out);
752 if (copy_to_user(hdr, ep, hdrbytes)) {
753 mutex_unlock(&rp->fetch_lock);
757 step_len = min(ep->len_cap, nbytes);
758 if ((offset = rp->b_out + PKT_SIZE) >= rp->b_size) offset = 0;
760 if (copy_from_buf(rp, offset, data, step_len)) {
761 mutex_unlock(&rp->fetch_lock);
765 spin_lock_irqsave(&rp->b_lock, flags);
766 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
767 spin_unlock_irqrestore(&rp->b_lock, flags);
770 mutex_unlock(&rp->fetch_lock);
774 static int mon_bin_release(struct inode *inode, struct file *file)
776 struct mon_reader_bin *rp = file->private_data;
777 struct mon_bus* mbus = rp->r.m_bus;
779 mutex_lock(&mon_lock);
781 if (mbus->nreaders <= 0) {
782 printk(KERN_ERR TAG ": consistency error on close\n");
783 mutex_unlock(&mon_lock);
786 mon_reader_del(mbus, &rp->r);
788 mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
792 mutex_unlock(&mon_lock);
796 static ssize_t mon_bin_read(struct file *file, char __user *buf,
797 size_t nbytes, loff_t *ppos)
799 struct mon_reader_bin *rp = file->private_data;
800 unsigned int hdrbytes = PKT_SZ_API0;
802 struct mon_bin_hdr *ep;
809 mutex_lock(&rp->fetch_lock);
811 if ((rc = mon_bin_wait_event(file, rp)) < 0) {
812 mutex_unlock(&rp->fetch_lock);
816 ep = MON_OFF2HDR(rp, rp->b_out);
818 if (rp->b_read < hdrbytes) {
819 step_len = min(nbytes, (size_t)(hdrbytes - rp->b_read));
820 ptr = ((char *)ep) + rp->b_read;
821 if (step_len && copy_to_user(buf, ptr, step_len)) {
822 mutex_unlock(&rp->fetch_lock);
827 rp->b_read += step_len;
831 if (rp->b_read >= hdrbytes) {
832 step_len = ep->len_cap;
833 step_len -= rp->b_read - hdrbytes;
834 if (step_len > nbytes)
836 offset = rp->b_out + PKT_SIZE;
837 offset += rp->b_read - hdrbytes;
838 if (offset >= rp->b_size)
839 offset -= rp->b_size;
840 if (copy_from_buf(rp, offset, buf, step_len)) {
841 mutex_unlock(&rp->fetch_lock);
846 rp->b_read += step_len;
851 * Check if whole packet was read, and if so, jump to the next one.
853 if (rp->b_read >= hdrbytes + ep->len_cap) {
854 spin_lock_irqsave(&rp->b_lock, flags);
855 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
856 spin_unlock_irqrestore(&rp->b_lock, flags);
860 mutex_unlock(&rp->fetch_lock);
865 * Remove at most nevents from chunked buffer.
866 * Returns the number of removed events.
868 static int mon_bin_flush(struct mon_reader_bin *rp, unsigned nevents)
871 struct mon_bin_hdr *ep;
874 mutex_lock(&rp->fetch_lock);
875 spin_lock_irqsave(&rp->b_lock, flags);
876 for (i = 0; i < nevents; ++i) {
877 if (MON_RING_EMPTY(rp))
880 ep = MON_OFF2HDR(rp, rp->b_out);
881 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
883 spin_unlock_irqrestore(&rp->b_lock, flags);
885 mutex_unlock(&rp->fetch_lock);
890 * Fetch at most max event offsets into the buffer and put them into vec.
891 * The events are usually freed later with mon_bin_flush.
892 * Return the effective number of events fetched.
894 static int mon_bin_fetch(struct file *file, struct mon_reader_bin *rp,
895 u32 __user *vec, unsigned int max)
897 unsigned int cur_out;
898 unsigned int bytes, avail;
900 unsigned int nevents;
901 struct mon_bin_hdr *ep;
905 mutex_lock(&rp->fetch_lock);
907 if ((rc = mon_bin_wait_event(file, rp)) < 0) {
908 mutex_unlock(&rp->fetch_lock);
912 spin_lock_irqsave(&rp->b_lock, flags);
914 spin_unlock_irqrestore(&rp->b_lock, flags);
919 while (bytes < avail) {
923 ep = MON_OFF2HDR(rp, cur_out);
924 if (put_user(cur_out, &vec[nevents])) {
925 mutex_unlock(&rp->fetch_lock);
930 size = ep->len_cap + PKT_SIZE;
931 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
932 if ((cur_out += size) >= rp->b_size)
933 cur_out -= rp->b_size;
937 mutex_unlock(&rp->fetch_lock);
942 * Count events. This is almost the same as the above mon_bin_fetch,
943 * only we do not store offsets into user vector, and we have no limit.
945 static int mon_bin_queued(struct mon_reader_bin *rp)
947 unsigned int cur_out;
948 unsigned int bytes, avail;
950 unsigned int nevents;
951 struct mon_bin_hdr *ep;
954 mutex_lock(&rp->fetch_lock);
956 spin_lock_irqsave(&rp->b_lock, flags);
958 spin_unlock_irqrestore(&rp->b_lock, flags);
963 while (bytes < avail) {
964 ep = MON_OFF2HDR(rp, cur_out);
967 size = ep->len_cap + PKT_SIZE;
968 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
969 if ((cur_out += size) >= rp->b_size)
970 cur_out -= rp->b_size;
974 mutex_unlock(&rp->fetch_lock);
980 static long mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
982 struct mon_reader_bin *rp = file->private_data;
983 // struct mon_bus* mbus = rp->r.m_bus;
985 struct mon_bin_hdr *ep;
990 case MON_IOCQ_URB_LEN:
992 * N.B. This only returns the size of data, without the header.
994 spin_lock_irqsave(&rp->b_lock, flags);
995 if (!MON_RING_EMPTY(rp)) {
996 ep = MON_OFF2HDR(rp, rp->b_out);
999 spin_unlock_irqrestore(&rp->b_lock, flags);
1002 case MON_IOCQ_RING_SIZE:
1006 case MON_IOCT_RING_SIZE:
1008 * Changing the buffer size will flush it's contents; the new
1009 * buffer is allocated before releasing the old one to be sure
1010 * the device will stay functional also in case of memory
1015 struct mon_pgmap *vec;
1017 if (arg < BUFF_MIN || arg > BUFF_MAX)
1020 size = CHUNK_ALIGN(arg);
1021 if ((vec = kzalloc(sizeof(struct mon_pgmap) * (size/CHUNK_SIZE),
1022 GFP_KERNEL)) == NULL) {
1027 ret = mon_alloc_buff(vec, size/CHUNK_SIZE);
1033 mutex_lock(&rp->fetch_lock);
1034 spin_lock_irqsave(&rp->b_lock, flags);
1035 mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
1039 rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0;
1041 spin_unlock_irqrestore(&rp->b_lock, flags);
1042 mutex_unlock(&rp->fetch_lock);
1046 case MON_IOCH_MFLUSH:
1047 ret = mon_bin_flush(rp, arg);
1053 struct mon_bin_get getb;
1055 if (copy_from_user(&getb, (void __user *)arg,
1056 sizeof(struct mon_bin_get)))
1059 if (getb.alloc > 0x10000000) /* Want to cast to u32 */
1061 ret = mon_bin_get_event(file, rp, getb.hdr,
1062 (cmd == MON_IOCX_GET)? PKT_SZ_API0: PKT_SZ_API1,
1063 getb.data, (unsigned int)getb.alloc);
1067 case MON_IOCX_MFETCH:
1069 struct mon_bin_mfetch mfetch;
1070 struct mon_bin_mfetch __user *uptr;
1072 uptr = (struct mon_bin_mfetch __user *)arg;
1074 if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1077 if (mfetch.nflush) {
1078 ret = mon_bin_flush(rp, mfetch.nflush);
1081 if (put_user(ret, &uptr->nflush))
1084 ret = mon_bin_fetch(file, rp, mfetch.offvec, mfetch.nfetch);
1087 if (put_user(ret, &uptr->nfetch))
1093 case MON_IOCG_STATS: {
1094 struct mon_bin_stats __user *sp;
1095 unsigned int nevents;
1096 unsigned int ndropped;
1098 spin_lock_irqsave(&rp->b_lock, flags);
1099 ndropped = rp->cnt_lost;
1101 spin_unlock_irqrestore(&rp->b_lock, flags);
1102 nevents = mon_bin_queued(rp);
1104 sp = (struct mon_bin_stats __user *)arg;
1105 if (put_user(ndropped, &sp->dropped))
1107 if (put_user(nevents, &sp->queued))
1120 #ifdef CONFIG_COMPAT
1121 static long mon_bin_compat_ioctl(struct file *file,
1122 unsigned int cmd, unsigned long arg)
1124 struct mon_reader_bin *rp = file->private_data;
1129 case MON_IOCX_GET32:
1130 case MON_IOCX_GETX32:
1132 struct mon_bin_get32 getb;
1134 if (copy_from_user(&getb, (void __user *)arg,
1135 sizeof(struct mon_bin_get32)))
1138 ret = mon_bin_get_event(file, rp, compat_ptr(getb.hdr32),
1139 (cmd == MON_IOCX_GET32)? PKT_SZ_API0: PKT_SZ_API1,
1140 compat_ptr(getb.data32), getb.alloc32);
1146 case MON_IOCX_MFETCH32:
1148 struct mon_bin_mfetch32 mfetch;
1149 struct mon_bin_mfetch32 __user *uptr;
1151 uptr = (struct mon_bin_mfetch32 __user *) compat_ptr(arg);
1153 if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1156 if (mfetch.nflush32) {
1157 ret = mon_bin_flush(rp, mfetch.nflush32);
1160 if (put_user(ret, &uptr->nflush32))
1163 ret = mon_bin_fetch(file, rp, compat_ptr(mfetch.offvec32),
1167 if (put_user(ret, &uptr->nfetch32))
1172 case MON_IOCG_STATS:
1173 return mon_bin_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1175 case MON_IOCQ_URB_LEN:
1176 case MON_IOCQ_RING_SIZE:
1177 case MON_IOCT_RING_SIZE:
1178 case MON_IOCH_MFLUSH:
1179 return mon_bin_ioctl(file, cmd, arg);
1186 #endif /* CONFIG_COMPAT */
1189 mon_bin_poll(struct file *file, struct poll_table_struct *wait)
1191 struct mon_reader_bin *rp = file->private_data;
1192 unsigned int mask = 0;
1193 unsigned long flags;
1195 if (file->f_mode & FMODE_READ)
1196 poll_wait(file, &rp->b_wait, wait);
1198 spin_lock_irqsave(&rp->b_lock, flags);
1199 if (!MON_RING_EMPTY(rp))
1200 mask |= POLLIN | POLLRDNORM; /* readable */
1201 spin_unlock_irqrestore(&rp->b_lock, flags);
1206 * open and close: just keep track of how many times the device is
1207 * mapped, to use the proper memory allocation function.
1209 static void mon_bin_vma_open(struct vm_area_struct *vma)
1211 struct mon_reader_bin *rp = vma->vm_private_data;
1215 static void mon_bin_vma_close(struct vm_area_struct *vma)
1217 struct mon_reader_bin *rp = vma->vm_private_data;
1222 * Map ring pages to user space.
1224 static int mon_bin_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1226 struct mon_reader_bin *rp = vma->vm_private_data;
1227 unsigned long offset, chunk_idx;
1228 struct page *pageptr;
1230 offset = vmf->pgoff << PAGE_SHIFT;
1231 if (offset >= rp->b_size)
1232 return VM_FAULT_SIGBUS;
1233 chunk_idx = offset / CHUNK_SIZE;
1234 pageptr = rp->b_vec[chunk_idx].pg;
1236 vmf->page = pageptr;
1240 static const struct vm_operations_struct mon_bin_vm_ops = {
1241 .open = mon_bin_vma_open,
1242 .close = mon_bin_vma_close,
1243 .fault = mon_bin_vma_fault,
1246 static int mon_bin_mmap(struct file *filp, struct vm_area_struct *vma)
1248 /* don't do anything here: "fault" will set up page table entries */
1249 vma->vm_ops = &mon_bin_vm_ops;
1250 vma->vm_flags |= VM_RESERVED;
1251 vma->vm_private_data = filp->private_data;
1252 mon_bin_vma_open(vma);
1256 static const struct file_operations mon_fops_binary = {
1257 .owner = THIS_MODULE,
1258 .open = mon_bin_open,
1259 .llseek = no_llseek,
1260 .read = mon_bin_read,
1261 /* .write = mon_text_write, */
1262 .poll = mon_bin_poll,
1263 .unlocked_ioctl = mon_bin_ioctl,
1264 #ifdef CONFIG_COMPAT
1265 .compat_ioctl = mon_bin_compat_ioctl,
1267 .release = mon_bin_release,
1268 .mmap = mon_bin_mmap,
1271 static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp)
1273 DECLARE_WAITQUEUE(waita, current);
1274 unsigned long flags;
1276 add_wait_queue(&rp->b_wait, &waita);
1277 set_current_state(TASK_INTERRUPTIBLE);
1279 spin_lock_irqsave(&rp->b_lock, flags);
1280 while (MON_RING_EMPTY(rp)) {
1281 spin_unlock_irqrestore(&rp->b_lock, flags);
1283 if (file->f_flags & O_NONBLOCK) {
1284 set_current_state(TASK_RUNNING);
1285 remove_wait_queue(&rp->b_wait, &waita);
1286 return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
1289 if (signal_pending(current)) {
1290 remove_wait_queue(&rp->b_wait, &waita);
1293 set_current_state(TASK_INTERRUPTIBLE);
1295 spin_lock_irqsave(&rp->b_lock, flags);
1297 spin_unlock_irqrestore(&rp->b_lock, flags);
1299 set_current_state(TASK_RUNNING);
1300 remove_wait_queue(&rp->b_wait, &waita);
1304 static int mon_alloc_buff(struct mon_pgmap *map, int npages)
1307 unsigned long vaddr;
1309 for (n = 0; n < npages; n++) {
1310 vaddr = get_zeroed_page(GFP_KERNEL);
1313 free_page((unsigned long) map[n].ptr);
1316 map[n].ptr = (unsigned char *) vaddr;
1317 map[n].pg = virt_to_page((void *) vaddr);
1322 static void mon_free_buff(struct mon_pgmap *map, int npages)
1326 for (n = 0; n < npages; n++)
1327 free_page((unsigned long) map[n].ptr);
1330 int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)
1333 unsigned minor = ubus? ubus->busnum: 0;
1335 if (minor >= MON_BIN_MAX_MINOR)
1338 dev = device_create(mon_bin_class, ubus ? ubus->controller : NULL,
1339 MKDEV(MAJOR(mon_bin_dev0), minor), NULL,
1344 mbus->classdev = dev;
1348 void mon_bin_del(struct mon_bus *mbus)
1350 device_destroy(mon_bin_class, mbus->classdev->devt);
1353 int __init mon_bin_init(void)
1357 mon_bin_class = class_create(THIS_MODULE, "usbmon");
1358 if (IS_ERR(mon_bin_class)) {
1359 rc = PTR_ERR(mon_bin_class);
1363 rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon");
1367 cdev_init(&mon_bin_cdev, &mon_fops_binary);
1368 mon_bin_cdev.owner = THIS_MODULE;
1370 rc = cdev_add(&mon_bin_cdev, mon_bin_dev0, MON_BIN_MAX_MINOR);
1377 unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1379 class_destroy(mon_bin_class);
1384 void mon_bin_exit(void)
1386 cdev_del(&mon_bin_cdev);
1387 unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1388 class_destroy(mon_bin_class);