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