Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * The USB Monitor, inspired by Dave Harding's USBMon. |
| 3 | * |
| 4 | * This is a text format reader. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/list.h> |
| 9 | #include <linux/usb.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 11 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/time.h> |
Tina Ruchandani | ec4dca8 | 2015-10-29 22:58:28 -0700 | [diff] [blame] | 13 | #include <linux/ktime.h> |
Paul Gortmaker | f940fcd | 2011-05-27 09:56:31 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 16 | #include <linux/debugfs.h> |
Alan Stern | b375e11 | 2009-11-06 12:32:23 -0500 | [diff] [blame] | 17 | #include <linux/scatterlist.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 18 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include "usb_mon.h" |
| 21 | |
| 22 | /* |
| 23 | * No, we do not want arbitrarily long data strings. |
| 24 | * Use the binary interface if you want to capture bulk data! |
| 25 | */ |
| 26 | #define DATA_MAX 32 |
| 27 | |
| 28 | /* |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 29 | * Defined by USB 2.0 clause 9.3, table 9.2. |
| 30 | */ |
| 31 | #define SETUP_MAX 8 |
| 32 | |
| 33 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | * This limit exists to prevent OOMs when the user process stops reading. |
Pete Zaitcev | 5b1c674 | 2006-06-09 20:10:10 -0700 | [diff] [blame] | 35 | * If usbmon were available to unprivileged processes, it might be open |
| 36 | * to a local DoS. But we have to keep to root in order to prevent |
| 37 | * password sniffing from HID devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | */ |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 39 | #define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 41 | /* |
| 42 | * Potentially unlimited number; we limit it for similar allocations. |
| 43 | * The usbfs limits this to 128, but we're not quite as generous. |
| 44 | */ |
| 45 | #define ISODESC_MAX 5 |
| 46 | |
| 47 | #define PRINTF_DFL 250 /* with 5 ISOs segs */ |
| 48 | |
| 49 | struct mon_iso_desc { |
| 50 | int status; |
| 51 | unsigned int offset; |
| 52 | unsigned int length; /* Unsigned here, signed in URB. Historic. */ |
| 53 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | struct mon_event_text { |
| 56 | struct list_head e_link; |
| 57 | int type; /* submit, complete, etc. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | unsigned long id; /* From pointer, most of the time */ |
| 59 | unsigned int tstamp; |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 60 | int busnum; |
Pete Zaitcev | 30c7431 | 2007-08-14 00:33:40 -0700 | [diff] [blame] | 61 | char devnum; |
| 62 | char epnum; |
| 63 | char is_in; |
| 64 | char xfertype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | int length; /* Depends on type: xfer length or act length */ |
| 66 | int status; |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 67 | int interval; |
| 68 | int start_frame; |
| 69 | int error_count; |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 70 | char setup_flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | char data_flag; |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 72 | int numdesc; /* Full number */ |
| 73 | struct mon_iso_desc isodesc[ISODESC_MAX]; |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 74 | unsigned char setup[SETUP_MAX]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | unsigned char data[DATA_MAX]; |
| 76 | }; |
| 77 | |
| 78 | #define SLAB_NAME_SZ 30 |
| 79 | struct mon_reader_text { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 80 | struct kmem_cache *e_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | int nevents; |
| 82 | struct list_head e_list; |
| 83 | struct mon_reader r; /* In C, parent class can be placed anywhere */ |
| 84 | |
| 85 | wait_queue_head_t wait; |
| 86 | int printf_size; |
| 87 | char *printf_buf; |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 88 | struct mutex printf_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | char slab_name[SLAB_NAME_SZ]; |
| 91 | }; |
| 92 | |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 93 | static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */ |
| 94 | |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 95 | static void mon_text_ctor(void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 97 | struct mon_text_ptr { |
| 98 | int cnt, limit; |
| 99 | char *pbuf; |
| 100 | }; |
| 101 | |
| 102 | static struct mon_event_text * |
| 103 | mon_text_read_wait(struct mon_reader_text *rp, struct file *file); |
| 104 | static void mon_text_read_head_t(struct mon_reader_text *rp, |
| 105 | struct mon_text_ptr *p, const struct mon_event_text *ep); |
| 106 | static void mon_text_read_head_u(struct mon_reader_text *rp, |
| 107 | struct mon_text_ptr *p, const struct mon_event_text *ep); |
| 108 | static void mon_text_read_statset(struct mon_reader_text *rp, |
| 109 | struct mon_text_ptr *p, const struct mon_event_text *ep); |
| 110 | static void mon_text_read_intstat(struct mon_reader_text *rp, |
| 111 | struct mon_text_ptr *p, const struct mon_event_text *ep); |
| 112 | static void mon_text_read_isostat(struct mon_reader_text *rp, |
| 113 | struct mon_text_ptr *p, const struct mon_event_text *ep); |
| 114 | static void mon_text_read_isodesc(struct mon_reader_text *rp, |
| 115 | struct mon_text_ptr *p, const struct mon_event_text *ep); |
| 116 | static void mon_text_read_data(struct mon_reader_text *rp, |
| 117 | struct mon_text_ptr *p, const struct mon_event_text *ep); |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | /* |
| 120 | * mon_text_submit |
| 121 | * mon_text_complete |
| 122 | * |
| 123 | * May be called from an interrupt. |
| 124 | * |
| 125 | * This is called with the whole mon_bus locked, so no additional lock. |
| 126 | */ |
| 127 | |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 128 | static inline char mon_text_get_setup(struct mon_event_text *ep, |
Alan Stern | 4d6cd48 | 2006-08-30 11:35:21 -0400 | [diff] [blame] | 129 | struct urb *urb, char ev_type, struct mon_bus *mbus) |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 130 | { |
| 131 | |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 132 | if (ep->xfertype != USB_ENDPOINT_XFER_CONTROL || ev_type != 'S') |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 133 | return '-'; |
| 134 | |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 135 | if (urb->setup_packet == NULL) |
| 136 | return 'Z'; /* '0' would be not as pretty. */ |
| 137 | |
| 138 | memcpy(ep->setup, urb->setup_packet, SETUP_MAX); |
| 139 | return 0; |
| 140 | } |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb, |
Alan Stern | 4d6cd48 | 2006-08-30 11:35:21 -0400 | [diff] [blame] | 143 | int len, char ev_type, struct mon_bus *mbus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
Alan Stern | b375e11 | 2009-11-06 12:32:23 -0500 | [diff] [blame] | 145 | void *src; |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | if (len <= 0) |
| 148 | return 'L'; |
Pete Zaitcev | 0256839 | 2005-08-15 16:53:57 -0700 | [diff] [blame] | 149 | if (len >= DATA_MAX) |
| 150 | len = DATA_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 152 | if (ep->is_in) { |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 153 | if (ev_type != 'C') |
Pete Zaitcev | b9b0942 | 2005-12-03 21:52:10 -0800 | [diff] [blame] | 154 | return '<'; |
| 155 | } else { |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 156 | if (ev_type != 'S') |
Pete Zaitcev | b9b0942 | 2005-12-03 21:52:10 -0800 | [diff] [blame] | 157 | return '>'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Alan Stern | b375e11 | 2009-11-06 12:32:23 -0500 | [diff] [blame] | 160 | if (urb->num_sgs == 0) { |
| 161 | src = urb->transfer_buffer; |
| 162 | if (src == NULL) |
| 163 | return 'Z'; /* '0' would be not as pretty. */ |
| 164 | } else { |
Matthew Wilcox | 910f8d0 | 2010-05-01 12:20:01 -0600 | [diff] [blame] | 165 | struct scatterlist *sg = urb->sg; |
Pete Zaitcev | 0256839 | 2005-08-15 16:53:57 -0700 | [diff] [blame] | 166 | |
Alan Stern | ff9c895 | 2010-04-02 13:27:28 -0400 | [diff] [blame] | 167 | if (PageHighMem(sg_page(sg))) |
Alan Stern | b375e11 | 2009-11-06 12:32:23 -0500 | [diff] [blame] | 168 | return 'D'; |
| 169 | |
| 170 | /* For the text interface we copy only the first sg buffer */ |
| 171 | len = min_t(int, sg->length, len); |
| 172 | src = sg_virt(sg); |
| 173 | } |
| 174 | |
| 175 | memcpy(ep->data, src, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static inline unsigned int mon_get_timestamp(void) |
| 180 | { |
Tina Ruchandani | ec4dca8 | 2015-10-29 22:58:28 -0700 | [diff] [blame] | 181 | struct timespec64 now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | unsigned int stamp; |
| 183 | |
Tina Ruchandani | ec4dca8 | 2015-10-29 22:58:28 -0700 | [diff] [blame] | 184 | ktime_get_ts64(&now); |
| 185 | stamp = now.tv_sec & 0xFFF; /* 2^32 = 4294967296. Limit to 4096s. */ |
| 186 | stamp = stamp * USEC_PER_SEC + now.tv_nsec / NSEC_PER_USEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return stamp; |
| 188 | } |
| 189 | |
| 190 | static void mon_text_event(struct mon_reader_text *rp, struct urb *urb, |
Alan Stern | 9347d51 | 2007-08-24 15:41:41 -0400 | [diff] [blame] | 191 | char ev_type, int status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | struct mon_event_text *ep; |
| 194 | unsigned int stamp; |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 195 | struct usb_iso_packet_descriptor *fp; |
| 196 | struct mon_iso_desc *dp; |
| 197 | int i, ndesc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | stamp = mon_get_timestamp(); |
| 200 | |
| 201 | if (rp->nevents >= EVENT_MAX || |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 202 | (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | rp->r.m_bus->cnt_text_lost++; |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | ep->type = ev_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | ep->id = (unsigned long) urb; |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame] | 209 | ep->busnum = urb->dev->bus->busnum; |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 210 | ep->devnum = urb->dev->devnum; |
| 211 | ep->epnum = usb_endpoint_num(&urb->ep->desc); |
| 212 | ep->xfertype = usb_endpoint_type(&urb->ep->desc); |
| 213 | ep->is_in = usb_urb_dir_in(urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | ep->tstamp = stamp; |
| 215 | ep->length = (ev_type == 'S') ? |
| 216 | urb->transfer_buffer_length : urb->actual_length; |
| 217 | /* Collecting status makes debugging sense for submits, too */ |
Alan Stern | 9347d51 | 2007-08-24 15:41:41 -0400 | [diff] [blame] | 218 | ep->status = status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 220 | if (ep->xfertype == USB_ENDPOINT_XFER_INT) { |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 221 | ep->interval = urb->interval; |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 222 | } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) { |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 223 | ep->interval = urb->interval; |
| 224 | ep->start_frame = urb->start_frame; |
| 225 | ep->error_count = urb->error_count; |
| 226 | } |
| 227 | ep->numdesc = urb->number_of_packets; |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 228 | if (ep->xfertype == USB_ENDPOINT_XFER_ISOC && |
| 229 | urb->number_of_packets > 0) { |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 230 | if ((ndesc = urb->number_of_packets) > ISODESC_MAX) |
| 231 | ndesc = ISODESC_MAX; |
| 232 | fp = urb->iso_frame_desc; |
| 233 | dp = ep->isodesc; |
| 234 | for (i = 0; i < ndesc; i++) { |
| 235 | dp->status = fp->status; |
| 236 | dp->offset = fp->offset; |
| 237 | dp->length = (ev_type == 'S') ? |
| 238 | fp->length : fp->actual_length; |
| 239 | fp++; |
| 240 | dp++; |
| 241 | } |
Pete Zaitcev | d25bc4d | 2011-02-03 22:01:36 -0700 | [diff] [blame] | 242 | /* Wasteful, but simple to understand: ISO 'C' is sparse. */ |
| 243 | if (ev_type == 'C') |
| 244 | ep->length = urb->transfer_buffer_length; |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Alan Stern | 4d6cd48 | 2006-08-30 11:35:21 -0400 | [diff] [blame] | 247 | ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus); |
| 248 | ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type, |
| 249 | rp->r.m_bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | rp->nevents++; |
| 252 | list_add_tail(&ep->e_link, &rp->e_list); |
| 253 | wake_up(&rp->wait); |
| 254 | } |
| 255 | |
| 256 | static void mon_text_submit(void *data, struct urb *urb) |
| 257 | { |
| 258 | struct mon_reader_text *rp = data; |
Alan Stern | 9347d51 | 2007-08-24 15:41:41 -0400 | [diff] [blame] | 259 | mon_text_event(rp, urb, 'S', -EINPROGRESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Alan Stern | 9347d51 | 2007-08-24 15:41:41 -0400 | [diff] [blame] | 262 | static void mon_text_complete(void *data, struct urb *urb, int status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
| 264 | struct mon_reader_text *rp = data; |
Alan Stern | 9347d51 | 2007-08-24 15:41:41 -0400 | [diff] [blame] | 265 | mon_text_event(rp, urb, 'C', status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Pete Zaitcev | 12e72fe | 2006-06-09 22:03:32 -0700 | [diff] [blame] | 268 | static void mon_text_error(void *data, struct urb *urb, int error) |
| 269 | { |
| 270 | struct mon_reader_text *rp = data; |
| 271 | struct mon_event_text *ep; |
| 272 | |
| 273 | if (rp->nevents >= EVENT_MAX || |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 274 | (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) { |
Pete Zaitcev | 12e72fe | 2006-06-09 22:03:32 -0700 | [diff] [blame] | 275 | rp->r.m_bus->cnt_text_lost++; |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | ep->type = 'E'; |
Pete Zaitcev | 12e72fe | 2006-06-09 22:03:32 -0700 | [diff] [blame] | 280 | ep->id = (unsigned long) urb; |
Pete Zaitcev | 2bc0d109 | 2010-01-05 11:50:07 -0700 | [diff] [blame] | 281 | ep->busnum = urb->dev->bus->busnum; |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 282 | ep->devnum = urb->dev->devnum; |
| 283 | ep->epnum = usb_endpoint_num(&urb->ep->desc); |
| 284 | ep->xfertype = usb_endpoint_type(&urb->ep->desc); |
| 285 | ep->is_in = usb_urb_dir_in(urb); |
Pete Zaitcev | 2bc0d109 | 2010-01-05 11:50:07 -0700 | [diff] [blame] | 286 | ep->tstamp = mon_get_timestamp(); |
Pete Zaitcev | 12e72fe | 2006-06-09 22:03:32 -0700 | [diff] [blame] | 287 | ep->length = 0; |
| 288 | ep->status = error; |
| 289 | |
| 290 | ep->setup_flag = '-'; |
| 291 | ep->data_flag = 'E'; |
| 292 | |
| 293 | rp->nevents++; |
| 294 | list_add_tail(&ep->e_link, &rp->e_list); |
| 295 | wake_up(&rp->wait); |
| 296 | } |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | /* |
| 299 | * Fetch next event from the circular buffer. |
| 300 | */ |
| 301 | static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp, |
| 302 | struct mon_bus *mbus) |
| 303 | { |
| 304 | struct list_head *p; |
| 305 | unsigned long flags; |
| 306 | |
| 307 | spin_lock_irqsave(&mbus->lock, flags); |
| 308 | if (list_empty(&rp->e_list)) { |
| 309 | spin_unlock_irqrestore(&mbus->lock, flags); |
| 310 | return NULL; |
| 311 | } |
| 312 | p = rp->e_list.next; |
| 313 | list_del(p); |
| 314 | --rp->nevents; |
| 315 | spin_unlock_irqrestore(&mbus->lock, flags); |
| 316 | return list_entry(p, struct mon_event_text, e_link); |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | */ |
| 321 | static int mon_text_open(struct inode *inode, struct file *file) |
| 322 | { |
| 323 | struct mon_bus *mbus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | struct mon_reader_text *rp; |
| 325 | int rc; |
| 326 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 327 | mutex_lock(&mon_lock); |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 328 | mbus = inode->i_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Eric Sesterhenn | 80b6ca4 | 2006-02-27 21:29:43 +0100 | [diff] [blame] | 330 | rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | if (rp == NULL) { |
| 332 | rc = -ENOMEM; |
| 333 | goto err_alloc; |
| 334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | INIT_LIST_HEAD(&rp->e_list); |
| 336 | init_waitqueue_head(&rp->wait); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 337 | mutex_init(&rp->printf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | rp->printf_size = PRINTF_DFL; |
| 340 | rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL); |
| 341 | if (rp->printf_buf == NULL) { |
| 342 | rc = -ENOMEM; |
| 343 | goto err_alloc_pr; |
| 344 | } |
| 345 | |
| 346 | rp->r.m_bus = mbus; |
| 347 | rp->r.r_data = rp; |
| 348 | rp->r.rnf_submit = mon_text_submit; |
Pete Zaitcev | 12e72fe | 2006-06-09 22:03:32 -0700 | [diff] [blame] | 349 | rp->r.rnf_error = mon_text_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | rp->r.rnf_complete = mon_text_complete; |
| 351 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame] | 352 | snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | rp->e_slab = kmem_cache_create(rp->slab_name, |
| 354 | sizeof(struct mon_event_text), sizeof(long), 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 355 | mon_text_ctor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | if (rp->e_slab == NULL) { |
| 357 | rc = -ENOMEM; |
| 358 | goto err_slab; |
| 359 | } |
| 360 | |
| 361 | mon_reader_add(mbus, &rp->r); |
| 362 | |
| 363 | file->private_data = rp; |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 364 | mutex_unlock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | return 0; |
| 366 | |
| 367 | // err_busy: |
| 368 | // kmem_cache_destroy(rp->e_slab); |
| 369 | err_slab: |
| 370 | kfree(rp->printf_buf); |
| 371 | err_alloc_pr: |
| 372 | kfree(rp); |
| 373 | err_alloc: |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 374 | mutex_unlock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | return rc; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * For simplicity, we read one record in one system call and throw out |
| 380 | * what does not fit. This means that the following does not work: |
| 381 | * dd if=/dbg/usbmon/0t bs=10 |
| 382 | * Also, we do not allow seeks and do not bother advancing the offset. |
| 383 | */ |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 384 | static ssize_t mon_text_read_t(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | size_t nbytes, loff_t *ppos) |
| 386 | { |
| 387 | struct mon_reader_text *rp = file->private_data; |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 388 | struct mon_event_text *ep; |
| 389 | struct mon_text_ptr ptr; |
| 390 | |
Julia Lawall | 46c236d | 2015-12-26 22:57:44 +0100 | [diff] [blame] | 391 | ep = mon_text_read_wait(rp, file); |
| 392 | if (IS_ERR(ep)) |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 393 | return PTR_ERR(ep); |
| 394 | mutex_lock(&rp->printf_lock); |
| 395 | ptr.cnt = 0; |
| 396 | ptr.pbuf = rp->printf_buf; |
| 397 | ptr.limit = rp->printf_size; |
| 398 | |
| 399 | mon_text_read_head_t(rp, &ptr, ep); |
| 400 | mon_text_read_statset(rp, &ptr, ep); |
| 401 | ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt, |
| 402 | " %d", ep->length); |
| 403 | mon_text_read_data(rp, &ptr, ep); |
| 404 | |
| 405 | if (copy_to_user(buf, rp->printf_buf, ptr.cnt)) |
| 406 | ptr.cnt = -EFAULT; |
| 407 | mutex_unlock(&rp->printf_lock); |
| 408 | kmem_cache_free(rp->e_slab, ep); |
| 409 | return ptr.cnt; |
| 410 | } |
| 411 | |
| 412 | static ssize_t mon_text_read_u(struct file *file, char __user *buf, |
| 413 | size_t nbytes, loff_t *ppos) |
| 414 | { |
| 415 | struct mon_reader_text *rp = file->private_data; |
| 416 | struct mon_event_text *ep; |
| 417 | struct mon_text_ptr ptr; |
| 418 | |
Julia Lawall | 46c236d | 2015-12-26 22:57:44 +0100 | [diff] [blame] | 419 | ep = mon_text_read_wait(rp, file); |
| 420 | if (IS_ERR(ep)) |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 421 | return PTR_ERR(ep); |
| 422 | mutex_lock(&rp->printf_lock); |
| 423 | ptr.cnt = 0; |
| 424 | ptr.pbuf = rp->printf_buf; |
| 425 | ptr.limit = rp->printf_size; |
| 426 | |
| 427 | mon_text_read_head_u(rp, &ptr, ep); |
| 428 | if (ep->type == 'E') { |
| 429 | mon_text_read_statset(rp, &ptr, ep); |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 430 | } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) { |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 431 | mon_text_read_isostat(rp, &ptr, ep); |
| 432 | mon_text_read_isodesc(rp, &ptr, ep); |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 433 | } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) { |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 434 | mon_text_read_intstat(rp, &ptr, ep); |
| 435 | } else { |
| 436 | mon_text_read_statset(rp, &ptr, ep); |
| 437 | } |
| 438 | ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt, |
| 439 | " %d", ep->length); |
| 440 | mon_text_read_data(rp, &ptr, ep); |
| 441 | |
| 442 | if (copy_to_user(buf, rp->printf_buf, ptr.cnt)) |
| 443 | ptr.cnt = -EFAULT; |
| 444 | mutex_unlock(&rp->printf_lock); |
| 445 | kmem_cache_free(rp->e_slab, ep); |
| 446 | return ptr.cnt; |
| 447 | } |
| 448 | |
| 449 | static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp, |
| 450 | struct file *file) |
| 451 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | struct mon_bus *mbus = rp->r.m_bus; |
| 453 | DECLARE_WAITQUEUE(waita, current); |
| 454 | struct mon_event_text *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | add_wait_queue(&rp->wait, &waita); |
| 457 | set_current_state(TASK_INTERRUPTIBLE); |
| 458 | while ((ep = mon_text_fetch(rp, mbus)) == NULL) { |
| 459 | if (file->f_flags & O_NONBLOCK) { |
| 460 | set_current_state(TASK_RUNNING); |
| 461 | remove_wait_queue(&rp->wait, &waita); |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 462 | return ERR_PTR(-EWOULDBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | /* |
| 465 | * We do not count nwaiters, because ->release is supposed |
| 466 | * to be called when all openers are gone only. |
| 467 | */ |
| 468 | schedule(); |
| 469 | if (signal_pending(current)) { |
| 470 | remove_wait_queue(&rp->wait, &waita); |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 471 | return ERR_PTR(-EINTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | set_current_state(TASK_INTERRUPTIBLE); |
| 474 | } |
| 475 | set_current_state(TASK_RUNNING); |
| 476 | remove_wait_queue(&rp->wait, &waita); |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 477 | return ep; |
| 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 480 | static void mon_text_read_head_t(struct mon_reader_text *rp, |
| 481 | struct mon_text_ptr *p, const struct mon_event_text *ep) |
| 482 | { |
| 483 | char udir, utype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 485 | udir = (ep->is_in ? 'i' : 'o'); |
| 486 | switch (ep->xfertype) { |
| 487 | case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break; |
| 488 | case USB_ENDPOINT_XFER_INT: utype = 'I'; break; |
| 489 | case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | default: /* PIPE_BULK */ utype = 'B'; |
| 491 | } |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 492 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 493 | "%lx %u %c %c%c:%03u:%02u", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | ep->id, ep->tstamp, ep->type, |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 495 | utype, udir, ep->devnum, ep->epnum); |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static void mon_text_read_head_u(struct mon_reader_text *rp, |
| 499 | struct mon_text_ptr *p, const struct mon_event_text *ep) |
| 500 | { |
| 501 | char udir, utype; |
| 502 | |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 503 | udir = (ep->is_in ? 'i' : 'o'); |
| 504 | switch (ep->xfertype) { |
| 505 | case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break; |
| 506 | case USB_ENDPOINT_XFER_INT: utype = 'I'; break; |
| 507 | case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break; |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 508 | default: /* PIPE_BULK */ utype = 'B'; |
| 509 | } |
| 510 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
| 511 | "%lx %u %c %c%c:%d:%03u:%u", |
| 512 | ep->id, ep->tstamp, ep->type, |
Alan Stern | 18ea5d0 | 2007-07-30 17:10:36 -0400 | [diff] [blame] | 513 | utype, udir, ep->busnum, ep->devnum, ep->epnum); |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static void mon_text_read_statset(struct mon_reader_text *rp, |
| 517 | struct mon_text_ptr *p, const struct mon_event_text *ep) |
| 518 | { |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 519 | |
| 520 | if (ep->setup_flag == 0) { /* Setup packet is present and captured */ |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 521 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 522 | " s %02x %02x %04x %04x %04x", |
| 523 | ep->setup[0], |
| 524 | ep->setup[1], |
| 525 | (ep->setup[3] << 8) | ep->setup[2], |
| 526 | (ep->setup[5] << 8) | ep->setup[4], |
| 527 | (ep->setup[7] << 8) | ep->setup[6]); |
| 528 | } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */ |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 529 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 530 | " %c __ __ ____ ____ ____", ep->setup_flag); |
| 531 | } else { /* No setup for this kind of URB */ |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 532 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
| 533 | " %d", ep->status); |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 534 | } |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static void mon_text_read_intstat(struct mon_reader_text *rp, |
| 538 | struct mon_text_ptr *p, const struct mon_event_text *ep) |
| 539 | { |
| 540 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
| 541 | " %d:%d", ep->status, ep->interval); |
| 542 | } |
| 543 | |
| 544 | static void mon_text_read_isostat(struct mon_reader_text *rp, |
| 545 | struct mon_text_ptr *p, const struct mon_event_text *ep) |
| 546 | { |
| 547 | if (ep->type == 'S') { |
| 548 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
| 549 | " %d:%d:%d", ep->status, ep->interval, ep->start_frame); |
| 550 | } else { |
| 551 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
| 552 | " %d:%d:%d:%d", |
| 553 | ep->status, ep->interval, ep->start_frame, ep->error_count); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | static void mon_text_read_isodesc(struct mon_reader_text *rp, |
| 558 | struct mon_text_ptr *p, const struct mon_event_text *ep) |
| 559 | { |
| 560 | int ndesc; /* Display this many */ |
| 561 | int i; |
| 562 | const struct mon_iso_desc *dp; |
| 563 | |
| 564 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
| 565 | " %d", ep->numdesc); |
| 566 | ndesc = ep->numdesc; |
| 567 | if (ndesc > ISODESC_MAX) |
| 568 | ndesc = ISODESC_MAX; |
| 569 | if (ndesc < 0) |
| 570 | ndesc = 0; |
| 571 | dp = ep->isodesc; |
| 572 | for (i = 0; i < ndesc; i++) { |
| 573 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
| 574 | " %d:%u:%u", dp->status, dp->offset, dp->length); |
| 575 | dp++; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | static void mon_text_read_data(struct mon_reader_text *rp, |
| 580 | struct mon_text_ptr *p, const struct mon_event_text *ep) |
| 581 | { |
| 582 | int data_len, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
| 584 | if ((data_len = ep->length) > 0) { |
| 585 | if (ep->data_flag == 0) { |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 586 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
| 587 | " ="); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | if (data_len >= DATA_MAX) |
| 589 | data_len = DATA_MAX; |
| 590 | for (i = 0; i < data_len; i++) { |
| 591 | if (i % 4 == 0) { |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 592 | p->cnt += snprintf(p->pbuf + p->cnt, |
| 593 | p->limit - p->cnt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | " "); |
| 595 | } |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 596 | p->cnt += snprintf(p->pbuf + p->cnt, |
| 597 | p->limit - p->cnt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | "%02x", ep->data[i]); |
| 599 | } |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 600 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
| 601 | "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } else { |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 603 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | " %c\n", ep->data_flag); |
| 605 | } |
| 606 | } else { |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 607 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | static int mon_text_release(struct inode *inode, struct file *file) |
| 612 | { |
| 613 | struct mon_reader_text *rp = file->private_data; |
| 614 | struct mon_bus *mbus; |
| 615 | /* unsigned long flags; */ |
| 616 | struct list_head *p; |
| 617 | struct mon_event_text *ep; |
| 618 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 619 | mutex_lock(&mon_lock); |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 620 | mbus = inode->i_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | if (mbus->nreaders <= 0) { |
| 623 | printk(KERN_ERR TAG ": consistency error on close\n"); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 624 | mutex_unlock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | return 0; |
| 626 | } |
| 627 | mon_reader_del(mbus, &rp->r); |
| 628 | |
| 629 | /* |
| 630 | * In theory, e_list is protected by mbus->lock. However, |
| 631 | * after mon_reader_del has finished, the following is the case: |
| 632 | * - we are not on reader list anymore, so new events won't be added; |
| 633 | * - whole mbus may be dropped if it was orphaned. |
| 634 | * So, we better not touch mbus. |
| 635 | */ |
| 636 | /* spin_lock_irqsave(&mbus->lock, flags); */ |
| 637 | while (!list_empty(&rp->e_list)) { |
| 638 | p = rp->e_list.next; |
| 639 | ep = list_entry(p, struct mon_event_text, e_link); |
| 640 | list_del(p); |
| 641 | --rp->nevents; |
| 642 | kmem_cache_free(rp->e_slab, ep); |
| 643 | } |
| 644 | /* spin_unlock_irqrestore(&mbus->lock, flags); */ |
| 645 | |
| 646 | kmem_cache_destroy(rp->e_slab); |
| 647 | kfree(rp->printf_buf); |
| 648 | kfree(rp); |
| 649 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 650 | mutex_unlock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 654 | static const struct file_operations mon_fops_text_t = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | .owner = THIS_MODULE, |
| 656 | .open = mon_text_open, |
| 657 | .llseek = no_llseek, |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 658 | .read = mon_text_read_t, |
| 659 | .release = mon_text_release, |
| 660 | }; |
| 661 | |
| 662 | static const struct file_operations mon_fops_text_u = { |
| 663 | .owner = THIS_MODULE, |
| 664 | .open = mon_text_open, |
| 665 | .llseek = no_llseek, |
| 666 | .read = mon_text_read_u, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | .release = mon_text_release, |
| 668 | }; |
| 669 | |
Pete Zaitcev | ce7cd137f | 2007-05-03 16:51:16 -0700 | [diff] [blame] | 670 | int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus) |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 671 | { |
| 672 | struct dentry *d; |
| 673 | enum { NAMESZ = 10 }; |
| 674 | char name[NAMESZ]; |
Pete Zaitcev | ce7cd137f | 2007-05-03 16:51:16 -0700 | [diff] [blame] | 675 | int busnum = ubus? ubus->busnum: 0; |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 676 | int rc; |
| 677 | |
Tobias Klauser | 8dec92b | 2011-07-07 09:28:21 +0200 | [diff] [blame] | 678 | if (mon_dir == NULL) |
| 679 | return 0; |
| 680 | |
Pete Zaitcev | ce7cd137f | 2007-05-03 16:51:16 -0700 | [diff] [blame] | 681 | if (ubus != NULL) { |
| 682 | rc = snprintf(name, NAMESZ, "%dt", busnum); |
| 683 | if (rc <= 0 || rc >= NAMESZ) |
| 684 | goto err_print_t; |
| 685 | d = debugfs_create_file(name, 0600, mon_dir, mbus, |
| 686 | &mon_fops_text_t); |
| 687 | if (d == NULL) |
| 688 | goto err_create_t; |
| 689 | mbus->dent_t = d; |
| 690 | } |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 691 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame] | 692 | rc = snprintf(name, NAMESZ, "%du", busnum); |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 693 | if (rc <= 0 || rc >= NAMESZ) |
| 694 | goto err_print_u; |
| 695 | d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u); |
| 696 | if (d == NULL) |
| 697 | goto err_create_u; |
| 698 | mbus->dent_u = d; |
| 699 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame] | 700 | rc = snprintf(name, NAMESZ, "%ds", busnum); |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 701 | if (rc <= 0 || rc >= NAMESZ) |
| 702 | goto err_print_s; |
| 703 | d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat); |
| 704 | if (d == NULL) |
| 705 | goto err_create_s; |
| 706 | mbus->dent_s = d; |
| 707 | |
| 708 | return 1; |
| 709 | |
| 710 | err_create_s: |
| 711 | err_print_s: |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 712 | debugfs_remove(mbus->dent_u); |
| 713 | mbus->dent_u = NULL; |
| 714 | err_create_u: |
| 715 | err_print_u: |
Pete Zaitcev | ce7cd137f | 2007-05-03 16:51:16 -0700 | [diff] [blame] | 716 | if (ubus != NULL) { |
| 717 | debugfs_remove(mbus->dent_t); |
| 718 | mbus->dent_t = NULL; |
| 719 | } |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 720 | err_create_t: |
| 721 | err_print_t: |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | void mon_text_del(struct mon_bus *mbus) |
| 726 | { |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 727 | debugfs_remove(mbus->dent_u); |
Pete Zaitcev | ce7cd137f | 2007-05-03 16:51:16 -0700 | [diff] [blame] | 728 | if (mbus->dent_t != NULL) |
| 729 | debugfs_remove(mbus->dent_t); |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 730 | debugfs_remove(mbus->dent_s); |
| 731 | } |
| 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | /* |
| 734 | * Slab interface: constructor. |
| 735 | */ |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 736 | static void mon_text_ctor(void *mem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | { |
| 738 | /* |
| 739 | * Nothing to initialize. No, really! |
| 740 | * So, we fill it with garbage to emulate a reused object. |
| 741 | */ |
| 742 | memset(mem, 0xe5, sizeof(struct mon_event_text)); |
| 743 | } |
| 744 | |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 745 | int __init mon_text_init(void) |
| 746 | { |
| 747 | struct dentry *mondir; |
| 748 | |
Greg Kroah-Hartman | f49ce96 | 2009-04-24 15:15:49 -0700 | [diff] [blame] | 749 | mondir = debugfs_create_dir("usbmon", usb_debug_root); |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 750 | if (IS_ERR(mondir)) { |
Tobias Klauser | 8dec92b | 2011-07-07 09:28:21 +0200 | [diff] [blame] | 751 | /* debugfs not available, but we can use usbmon without it */ |
| 752 | return 0; |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 753 | } |
| 754 | if (mondir == NULL) { |
| 755 | printk(KERN_NOTICE TAG ": unable to create usbmon directory\n"); |
Tobias Klauser | 8dec92b | 2011-07-07 09:28:21 +0200 | [diff] [blame] | 756 | return -ENOMEM; |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 757 | } |
| 758 | mon_dir = mondir; |
| 759 | return 0; |
| 760 | } |
| 761 | |
Pete Zaitcev | 21641e3 | 2007-02-20 10:37:52 -0800 | [diff] [blame] | 762 | void mon_text_exit(void) |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 763 | { |
| 764 | debugfs_remove(mon_dir); |
| 765 | } |