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