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