| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * The USB Monitor, inspired by Dave Harding's USBMon. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * mon_main.c: Main file, module initiation and exit, registrations, etc. | 
					
						
							| 
									
										
										
										
											2005-08-16 15:16:46 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <linux/kernel.h>
 | 
					
						
							|  |  |  | #include <linux/module.h>
 | 
					
						
							|  |  |  | #include <linux/usb.h>
 | 
					
						
							| 
									
										
										
										
											2005-06-20 21:15:16 -07:00
										 |  |  | #include <linux/notifier.h>
 | 
					
						
							| 
									
										
										
										
											2006-01-11 15:55:29 +01:00
										 |  |  | #include <linux/mutex.h>
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "usb_mon.h"
 | 
					
						
							|  |  |  | #include "../core/hcd.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void mon_stop(struct mon_bus *mbus); | 
					
						
							|  |  |  | static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus); | 
					
						
							|  |  |  | static void mon_bus_drop(struct kref *r); | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | static void mon_bus_init(struct usb_bus *ubus); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-11 15:55:29 +01:00
										 |  |  | DEFINE_MUTEX(mon_lock); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | struct mon_bus mon_bus0;		/* Pseudo bus meaning "all buses" */ | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | static LIST_HEAD(mon_buses);		/* All buses we know: struct mon_bus */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Link a reader into the bus. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This must be called with mon_lock taken because of mbus->ref. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned long flags; | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 	struct list_head *p; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	spin_lock_irqsave(&mbus->lock, flags); | 
					
						
							|  |  |  | 	if (mbus->nreaders == 0) { | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 		if (mbus == &mon_bus0) { | 
					
						
							|  |  |  | 			list_for_each (p, &mon_buses) { | 
					
						
							|  |  |  | 				struct mon_bus *m1; | 
					
						
							|  |  |  | 				m1 = list_entry(p, struct mon_bus, bus_link); | 
					
						
							|  |  |  | 				m1->u_bus->monitored = 1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			mbus->u_bus->monitored = 1; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	mbus->nreaders++; | 
					
						
							|  |  |  | 	list_add_tail(&r->r_link, &mbus->r_list); | 
					
						
							|  |  |  | 	spin_unlock_irqrestore(&mbus->lock, flags); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	kref_get(&mbus->ref); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Unlink reader from the bus. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This is called with mon_lock taken, so we can decrement mbus->ref. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned long flags; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spin_lock_irqsave(&mbus->lock, flags); | 
					
						
							|  |  |  | 	list_del(&r->r_link); | 
					
						
							|  |  |  | 	--mbus->nreaders; | 
					
						
							|  |  |  | 	if (mbus->nreaders == 0) | 
					
						
							|  |  |  | 		mon_stop(mbus); | 
					
						
							|  |  |  | 	spin_unlock_irqrestore(&mbus->lock, flags); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	kref_put(&mbus->ref, mon_bus_drop); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	unsigned long flags; | 
					
						
							|  |  |  | 	struct list_head *pos; | 
					
						
							|  |  |  | 	struct mon_reader *r; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spin_lock_irqsave(&mbus->lock, flags); | 
					
						
							| 
									
										
										
										
											2006-06-09 20:10:10 -07:00
										 |  |  | 	mbus->cnt_events++; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	list_for_each (pos, &mbus->r_list) { | 
					
						
							|  |  |  | 		r = list_entry(pos, struct mon_reader, r_link); | 
					
						
							|  |  |  | 		r->rnf_submit(r->r_data, urb); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	spin_unlock_irqrestore(&mbus->lock, flags); | 
					
						
							|  |  |  | 	return; | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | static void mon_submit(struct usb_bus *ubus, struct urb *urb) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct mon_bus *mbus; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((mbus = ubus->mon_bus) != NULL) | 
					
						
							|  |  |  | 		mon_bus_submit(mbus, urb); | 
					
						
							|  |  |  | 	mon_bus_submit(&mon_bus0, urb); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-06-09 22:03:32 -07:00
										 |  |  | 	unsigned long flags; | 
					
						
							|  |  |  | 	struct list_head *pos; | 
					
						
							|  |  |  | 	struct mon_reader *r; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-09 22:03:32 -07:00
										 |  |  | 	spin_lock_irqsave(&mbus->lock, flags); | 
					
						
							|  |  |  | 	mbus->cnt_events++; | 
					
						
							|  |  |  | 	list_for_each (pos, &mbus->r_list) { | 
					
						
							|  |  |  | 		r = list_entry(pos, struct mon_reader, r_link); | 
					
						
							|  |  |  | 		r->rnf_error(r->r_data, urb, error); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	spin_unlock_irqrestore(&mbus->lock, flags); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	return; | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct mon_bus *mbus; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((mbus = ubus->mon_bus) != NULL) | 
					
						
							|  |  |  | 		mon_bus_submit_error(mbus, urb, error); | 
					
						
							|  |  |  | 	mon_bus_submit_error(&mon_bus0, urb, error); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2008-03-19 22:29:51 -07:00
										 |  |  | static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	unsigned long flags; | 
					
						
							|  |  |  | 	struct list_head *pos; | 
					
						
							|  |  |  | 	struct mon_reader *r; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 	spin_lock_irqsave(&mbus->lock, flags); | 
					
						
							|  |  |  | 	mbus->cnt_events++; | 
					
						
							|  |  |  | 	list_for_each (pos, &mbus->r_list) { | 
					
						
							|  |  |  | 		r = list_entry(pos, struct mon_reader, r_link); | 
					
						
							| 
									
										
										
										
											2007-08-24 15:41:41 -04:00
										 |  |  | 		r->rnf_complete(r->r_data, urb, status); | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	spin_unlock_irqrestore(&mbus->lock, flags); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-24 15:41:41 -04:00
										 |  |  | static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status) | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct mon_bus *mbus; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-14 00:42:53 -07:00
										 |  |  | 	if ((mbus = ubus->mon_bus) != NULL) | 
					
						
							| 
									
										
										
										
											2007-08-24 15:41:41 -04:00
										 |  |  | 		mon_bus_complete(mbus, urb, status); | 
					
						
							|  |  |  | 	mon_bus_complete(&mon_bus0, urb, status); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* int (*unlink_urb) (struct urb *urb, int status); */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Stop monitoring. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void mon_stop(struct mon_bus *mbus) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-08-14 00:42:53 -07:00
										 |  |  | 	struct usb_bus *ubus; | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 	struct list_head *p; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 	if (mbus == &mon_bus0) { | 
					
						
							|  |  |  | 		list_for_each (p, &mon_buses) { | 
					
						
							|  |  |  | 			mbus = list_entry(p, struct mon_bus, bus_link); | 
					
						
							|  |  |  | 			/*
 | 
					
						
							|  |  |  | 			 * We do not change nreaders here, so rely on mon_lock. | 
					
						
							|  |  |  | 			 */ | 
					
						
							|  |  |  | 			if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL) | 
					
						
							|  |  |  | 				ubus->monitored = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * A stop can be called for a dissolved mon_bus in case of | 
					
						
							|  |  |  | 		 * a reader staying across an rmmod foo_hcd, so test ->u_bus. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) { | 
					
						
							|  |  |  | 			ubus->monitored = 0; | 
					
						
							|  |  |  | 			mb(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Add a USB bus (usually by a modprobe foo-hcd) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This does not return an error code because the core cannot care less | 
					
						
							|  |  |  |  * if monitoring is not established. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void mon_bus_add(struct usb_bus *ubus) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | 	mon_bus_init(ubus); | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 	mutex_lock(&mon_lock); | 
					
						
							|  |  |  | 	if (mon_bus0.nreaders != 0) | 
					
						
							|  |  |  | 		ubus->monitored = 1; | 
					
						
							|  |  |  | 	mutex_unlock(&mon_lock); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event). | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void mon_bus_remove(struct usb_bus *ubus) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct mon_bus *mbus = ubus->mon_bus; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-11 15:55:29 +01:00
										 |  |  | 	mutex_lock(&mon_lock); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	list_del(&mbus->bus_link); | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | 	if (mbus->text_inited) | 
					
						
							|  |  |  | 		mon_text_del(mbus); | 
					
						
							| 
									
										
										
										
											2007-05-03 16:51:16 -07:00
										 |  |  | 	if (mbus->bin_inited) | 
					
						
							|  |  |  | 		mon_bin_del(mbus); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	mon_dissolve(mbus, ubus); | 
					
						
							|  |  |  | 	kref_put(&mbus->ref, mon_bus_drop); | 
					
						
							| 
									
										
										
										
											2006-01-11 15:55:29 +01:00
										 |  |  | 	mutex_unlock(&mon_lock); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-20 21:15:16 -07:00
										 |  |  | static int mon_notify(struct notifier_block *self, unsigned long action, | 
					
						
							|  |  |  | 		      void *dev) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	switch (action) { | 
					
						
							|  |  |  | 	case USB_BUS_ADD: | 
					
						
							|  |  |  | 		mon_bus_add(dev); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case USB_BUS_REMOVE: | 
					
						
							|  |  |  | 		mon_bus_remove(dev); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return NOTIFY_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct notifier_block mon_nb = { | 
					
						
							|  |  |  | 	.notifier_call = 	mon_notify, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Ops | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static struct usb_mon_operations mon_ops_0 = { | 
					
						
							|  |  |  | 	.urb_submit =	mon_submit, | 
					
						
							|  |  |  | 	.urb_submit_error = mon_submit_error, | 
					
						
							|  |  |  | 	.urb_complete =	mon_complete, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Tear usb_bus and mon_bus apart. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ubus->monitored) { | 
					
						
							|  |  |  | 		ubus->monitored = 0; | 
					
						
							|  |  |  | 		mb(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ubus->mon_bus = NULL; | 
					
						
							|  |  |  | 	mbus->u_bus = NULL; | 
					
						
							|  |  |  | 	mb(); | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* We want synchronize_irq() here, but that needs an argument. */ | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void mon_bus_drop(struct kref *r) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct mon_bus *mbus = container_of(r, struct mon_bus, ref); | 
					
						
							|  |  |  | 	kfree(mbus); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Initialize a bus for us: | 
					
						
							|  |  |  |  *  - allocate mon_bus | 
					
						
							|  |  |  |  *  - refcount USB bus struct | 
					
						
							|  |  |  |  *  - link | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | static void mon_bus_init(struct usb_bus *ubus) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct mon_bus *mbus; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-27 21:29:43 +01:00
										 |  |  | 	if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		goto err_alloc; | 
					
						
							|  |  |  | 	kref_init(&mbus->ref); | 
					
						
							|  |  |  | 	spin_lock_init(&mbus->lock); | 
					
						
							|  |  |  | 	INIT_LIST_HEAD(&mbus->r_list); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							| 
									
										
										
										
											2006-08-30 11:32:52 -04:00
										 |  |  | 	 * We don't need to take a reference to ubus, because we receive | 
					
						
							|  |  |  | 	 * a notification if the bus is about to be removed. | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	mbus->u_bus = ubus; | 
					
						
							|  |  |  | 	ubus->mon_bus = mbus; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-03 16:51:16 -07:00
										 |  |  | 	mbus->text_inited = mon_text_add(mbus, ubus); | 
					
						
							|  |  |  | 	mbus->bin_inited = mon_bin_add(mbus, ubus); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-11 15:55:29 +01:00
										 |  |  | 	mutex_lock(&mon_lock); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	list_add_tail(&mbus->bus_link, &mon_buses); | 
					
						
							| 
									
										
										
										
											2006-01-11 15:55:29 +01:00
										 |  |  | 	mutex_unlock(&mon_lock); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | err_alloc: | 
					
						
							|  |  |  | 	return; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | static void mon_bus0_init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct mon_bus *mbus = &mon_bus0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	kref_init(&mbus->ref); | 
					
						
							|  |  |  | 	spin_lock_init(&mbus->lock); | 
					
						
							|  |  |  | 	INIT_LIST_HEAD(&mbus->r_list); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-03 16:51:16 -07:00
										 |  |  | 	mbus->text_inited = mon_text_add(mbus, NULL); | 
					
						
							|  |  |  | 	mbus->bin_inited = mon_bin_add(mbus, NULL); | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Search a USB bus by number. Notice that USB bus numbers start from one, | 
					
						
							|  |  |  |  * which we may later use to identify "all" with zero. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This function must be called with mon_lock held. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This is obviously inefficient and may be revised in the future. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | struct mon_bus *mon_bus_lookup(unsigned int num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct list_head *p; | 
					
						
							|  |  |  | 	struct mon_bus *mbus; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 	if (num == 0) { | 
					
						
							|  |  |  | 		return &mon_bus0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | 	list_for_each (p, &mon_buses) { | 
					
						
							|  |  |  | 		mbus = list_entry(p, struct mon_bus, bus_link); | 
					
						
							|  |  |  | 		if (mbus->u_bus->busnum == num) { | 
					
						
							|  |  |  | 			return mbus; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | static int __init mon_init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct usb_bus *ubus; | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | 	int rc; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | 	if ((rc = mon_text_init()) != 0) | 
					
						
							|  |  |  | 		goto err_text; | 
					
						
							|  |  |  | 	if ((rc = mon_bin_init()) != 0) | 
					
						
							|  |  |  | 		goto err_bin; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 	mon_bus0_init(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	if (usb_mon_register(&mon_ops_0) != 0) { | 
					
						
							|  |  |  | 		printk(KERN_NOTICE TAG ": unable to register with the core\n"); | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | 		rc = -ENODEV; | 
					
						
							|  |  |  | 		goto err_reg; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	// MOD_INC_USE_COUNT(which_module?);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-11 15:55:29 +01:00
										 |  |  | 	mutex_lock(&usb_bus_list_lock); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	list_for_each_entry (ubus, &usb_bus_list, bus_list) { | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | 		mon_bus_init(ubus); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-09-22 15:00:10 -07:00
										 |  |  | 	usb_register_notify(&mon_nb); | 
					
						
							| 
									
										
										
										
											2006-01-11 15:55:29 +01:00
										 |  |  | 	mutex_unlock(&usb_bus_list_lock); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | err_reg: | 
					
						
							|  |  |  | 	mon_bin_exit(); | 
					
						
							|  |  |  | err_bin: | 
					
						
							|  |  |  | 	mon_text_exit(); | 
					
						
							|  |  |  | err_text: | 
					
						
							|  |  |  | 	return rc; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void __exit mon_exit(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct mon_bus *mbus; | 
					
						
							|  |  |  | 	struct list_head *p; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-20 21:15:16 -07:00
										 |  |  | 	usb_unregister_notify(&mon_nb); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	usb_mon_deregister(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-11 15:55:29 +01:00
										 |  |  | 	mutex_lock(&mon_lock); | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	while (!list_empty(&mon_buses)) { | 
					
						
							|  |  |  | 		p = mon_buses.next; | 
					
						
							|  |  |  | 		mbus = list_entry(p, struct mon_bus, bus_link); | 
					
						
							|  |  |  | 		list_del(p); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | 		if (mbus->text_inited) | 
					
						
							|  |  |  | 			mon_text_del(mbus); | 
					
						
							| 
									
										
										
										
											2007-05-03 16:51:16 -07:00
										 |  |  | 		if (mbus->bin_inited) | 
					
						
							|  |  |  | 			mon_bin_del(mbus); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * This never happens, because the open/close paths in | 
					
						
							|  |  |  | 		 * file level maintain module use counters and so rmmod fails | 
					
						
							|  |  |  | 		 * before reaching here. However, better be safe... | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		if (mbus->nreaders) { | 
					
						
							|  |  |  | 			printk(KERN_ERR TAG | 
					
						
							|  |  |  | 			    ": Outstanding opens (%d) on usb%d, leaking...\n", | 
					
						
							|  |  |  | 			    mbus->nreaders, mbus->u_bus->busnum); | 
					
						
							|  |  |  | 			atomic_set(&mbus->ref.refcount, 2);	/* Force leak */ | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		mon_dissolve(mbus, mbus->u_bus); | 
					
						
							|  |  |  | 		kref_put(&mbus->ref, mon_bus_drop); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	mbus = &mon_bus0; | 
					
						
							|  |  |  | 	if (mbus->text_inited) | 
					
						
							|  |  |  | 		mon_text_del(mbus); | 
					
						
							| 
									
										
										
										
											2007-05-03 16:51:16 -07:00
										 |  |  | 	if (mbus->bin_inited) | 
					
						
							|  |  |  | 		mon_bin_del(mbus); | 
					
						
							| 
									
										
										
										
											2007-04-11 13:47:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-11 15:55:29 +01:00
										 |  |  | 	mutex_unlock(&mon_lock); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												USB: add binary API to usbmon
This patch adds a new, "binary" API in addition to the old, text API usbmon
had before. The new API allows for less CPU use, and it allows to capture
all data from a packet where old API only captured 32 bytes at most. There
are some limitations and conditions to this, e.g. in case someone constructs
a URB with 1GB of data, it's not likely to be captured, because even the
huge buffers of the new reader are finite. Nonetheless, I expect this new
capability to capture all data for all real life scenarios.
The downside is, a special user mode application is required where cat(1)
worked before. I have sample code at http://people.redhat.com/zaitcev/linux/
and Paolo Abeni is working on patching libpcap.
This patch was initially written by Paolo and later I tweaked it, and
we had a little back-and-forth. So this is a jointly authored patch, but
I am submitting this I am responsible for the bugs.
Signed-off-by: Paolo Abeni <paolo.abeni@email.it>
Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
											
										 
											2006-12-30 22:43:10 -08:00
										 |  |  | 	mon_text_exit(); | 
					
						
							|  |  |  | 	mon_bin_exit(); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module_init(mon_init); | 
					
						
							|  |  |  | module_exit(mon_exit); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MODULE_LICENSE("GPL"); |