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