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