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