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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <asm/uaccess.h> |
| 13 | |
| 14 | #include "usb_mon.h" |
| 15 | |
| 16 | /* |
| 17 | * No, we do not want arbitrarily long data strings. |
| 18 | * Use the binary interface if you want to capture bulk data! |
| 19 | */ |
| 20 | #define DATA_MAX 32 |
| 21 | |
| 22 | /* |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 23 | * Defined by USB 2.0 clause 9.3, table 9.2. |
| 24 | */ |
| 25 | #define SETUP_MAX 8 |
| 26 | |
| 27 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * This limit exists to prevent OOMs when the user process stops reading. |
| 29 | */ |
| 30 | #define EVENT_MAX 25 |
| 31 | |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 32 | #define PRINTF_DFL 130 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | struct mon_event_text { |
| 35 | struct list_head e_link; |
| 36 | int type; /* submit, complete, etc. */ |
| 37 | unsigned int pipe; /* Pipe */ |
| 38 | unsigned long id; /* From pointer, most of the time */ |
| 39 | unsigned int tstamp; |
| 40 | int length; /* Depends on type: xfer length or act length */ |
| 41 | int status; |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 42 | char setup_flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | char data_flag; |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 44 | unsigned char setup[SETUP_MAX]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | unsigned char data[DATA_MAX]; |
| 46 | }; |
| 47 | |
| 48 | #define SLAB_NAME_SZ 30 |
| 49 | struct mon_reader_text { |
| 50 | kmem_cache_t *e_slab; |
| 51 | int nevents; |
| 52 | struct list_head e_list; |
| 53 | struct mon_reader r; /* In C, parent class can be placed anywhere */ |
| 54 | |
| 55 | wait_queue_head_t wait; |
| 56 | int printf_size; |
| 57 | char *printf_buf; |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 58 | struct mutex printf_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | char slab_name[SLAB_NAME_SZ]; |
| 61 | }; |
| 62 | |
| 63 | static void mon_text_ctor(void *, kmem_cache_t *, unsigned long); |
| 64 | static void mon_text_dtor(void *, kmem_cache_t *, unsigned long); |
| 65 | |
| 66 | /* |
| 67 | * mon_text_submit |
| 68 | * mon_text_complete |
| 69 | * |
| 70 | * May be called from an interrupt. |
| 71 | * |
| 72 | * This is called with the whole mon_bus locked, so no additional lock. |
| 73 | */ |
| 74 | |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 75 | static inline char mon_text_get_setup(struct mon_event_text *ep, |
| 76 | struct urb *urb, char ev_type) |
| 77 | { |
| 78 | |
| 79 | if (!usb_pipecontrol(urb->pipe) || ev_type != 'S') |
| 80 | return '-'; |
| 81 | |
| 82 | if (urb->transfer_flags & URB_NO_SETUP_DMA_MAP) |
Pete Zaitcev | bc50651 | 2005-09-01 14:35:05 -0700 | [diff] [blame] | 83 | return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX); |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 84 | if (urb->setup_packet == NULL) |
| 85 | return 'Z'; /* '0' would be not as pretty. */ |
| 86 | |
| 87 | memcpy(ep->setup, urb->setup_packet, SETUP_MAX); |
| 88 | return 0; |
| 89 | } |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb, |
| 92 | int len, char ev_type) |
| 93 | { |
| 94 | int pipe = urb->pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | if (len <= 0) |
| 97 | return 'L'; |
Pete Zaitcev | 0256839 | 2005-08-15 16:53:57 -0700 | [diff] [blame] | 98 | if (len >= DATA_MAX) |
| 99 | len = DATA_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Pete Zaitcev | b9b0942 | 2005-12-03 21:52:10 -0800 | [diff] [blame] | 101 | if (usb_pipein(pipe)) { |
| 102 | if (ev_type == 'S') |
| 103 | return '<'; |
| 104 | } else { |
| 105 | if (ev_type == 'C') |
| 106 | return '>'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Pete Zaitcev | 0256839 | 2005-08-15 16:53:57 -0700 | [diff] [blame] | 109 | /* |
| 110 | * The check to see if it's safe to poke at data has an enormous |
| 111 | * number of corner cases, but it seems that the following is |
| 112 | * more or less safe. |
| 113 | * |
| 114 | * We do not even try to look transfer_buffer, because it can |
| 115 | * contain non-NULL garbage in case the upper level promised to |
| 116 | * set DMA for the HCD. |
| 117 | */ |
| 118 | if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) |
| 119 | return mon_dmapeek(ep->data, urb->transfer_dma, len); |
| 120 | |
| 121 | if (urb->transfer_buffer == NULL) |
| 122 | return 'Z'; /* '0' would be not as pretty. */ |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | memcpy(ep->data, urb->transfer_buffer, len); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static inline unsigned int mon_get_timestamp(void) |
| 129 | { |
| 130 | struct timeval tval; |
| 131 | unsigned int stamp; |
| 132 | |
| 133 | do_gettimeofday(&tval); |
| 134 | stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */ |
| 135 | stamp = stamp * 1000000 + tval.tv_usec; |
| 136 | return stamp; |
| 137 | } |
| 138 | |
| 139 | static void mon_text_event(struct mon_reader_text *rp, struct urb *urb, |
| 140 | char ev_type) |
| 141 | { |
| 142 | struct mon_event_text *ep; |
| 143 | unsigned int stamp; |
| 144 | |
| 145 | stamp = mon_get_timestamp(); |
| 146 | |
| 147 | if (rp->nevents >= EVENT_MAX || |
| 148 | (ep = kmem_cache_alloc(rp->e_slab, SLAB_ATOMIC)) == NULL) { |
| 149 | rp->r.m_bus->cnt_text_lost++; |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | ep->type = ev_type; |
| 154 | ep->pipe = urb->pipe; |
| 155 | ep->id = (unsigned long) urb; |
| 156 | ep->tstamp = stamp; |
| 157 | ep->length = (ev_type == 'S') ? |
| 158 | urb->transfer_buffer_length : urb->actual_length; |
| 159 | /* Collecting status makes debugging sense for submits, too */ |
| 160 | ep->status = urb->status; |
| 161 | |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 162 | ep->setup_flag = mon_text_get_setup(ep, urb, ev_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type); |
| 164 | |
| 165 | rp->nevents++; |
| 166 | list_add_tail(&ep->e_link, &rp->e_list); |
| 167 | wake_up(&rp->wait); |
| 168 | } |
| 169 | |
| 170 | static void mon_text_submit(void *data, struct urb *urb) |
| 171 | { |
| 172 | struct mon_reader_text *rp = data; |
| 173 | mon_text_event(rp, urb, 'S'); |
| 174 | } |
| 175 | |
| 176 | static void mon_text_complete(void *data, struct urb *urb) |
| 177 | { |
| 178 | struct mon_reader_text *rp = data; |
| 179 | mon_text_event(rp, urb, 'C'); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Fetch next event from the circular buffer. |
| 184 | */ |
| 185 | static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp, |
| 186 | struct mon_bus *mbus) |
| 187 | { |
| 188 | struct list_head *p; |
| 189 | unsigned long flags; |
| 190 | |
| 191 | spin_lock_irqsave(&mbus->lock, flags); |
| 192 | if (list_empty(&rp->e_list)) { |
| 193 | spin_unlock_irqrestore(&mbus->lock, flags); |
| 194 | return NULL; |
| 195 | } |
| 196 | p = rp->e_list.next; |
| 197 | list_del(p); |
| 198 | --rp->nevents; |
| 199 | spin_unlock_irqrestore(&mbus->lock, flags); |
| 200 | return list_entry(p, struct mon_event_text, e_link); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | */ |
| 205 | static int mon_text_open(struct inode *inode, struct file *file) |
| 206 | { |
| 207 | struct mon_bus *mbus; |
| 208 | struct usb_bus *ubus; |
| 209 | struct mon_reader_text *rp; |
| 210 | int rc; |
| 211 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 212 | mutex_lock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | mbus = inode->u.generic_ip; |
| 214 | ubus = mbus->u_bus; |
| 215 | |
| 216 | rp = kmalloc(sizeof(struct mon_reader_text), GFP_KERNEL); |
| 217 | if (rp == NULL) { |
| 218 | rc = -ENOMEM; |
| 219 | goto err_alloc; |
| 220 | } |
| 221 | memset(rp, 0, sizeof(struct mon_reader_text)); |
| 222 | INIT_LIST_HEAD(&rp->e_list); |
| 223 | init_waitqueue_head(&rp->wait); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 224 | mutex_init(&rp->printf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
| 226 | rp->printf_size = PRINTF_DFL; |
| 227 | rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL); |
| 228 | if (rp->printf_buf == NULL) { |
| 229 | rc = -ENOMEM; |
| 230 | goto err_alloc_pr; |
| 231 | } |
| 232 | |
| 233 | rp->r.m_bus = mbus; |
| 234 | rp->r.r_data = rp; |
| 235 | rp->r.rnf_submit = mon_text_submit; |
| 236 | rp->r.rnf_complete = mon_text_complete; |
| 237 | |
| 238 | snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum, |
| 239 | (long)rp); |
| 240 | rp->e_slab = kmem_cache_create(rp->slab_name, |
| 241 | sizeof(struct mon_event_text), sizeof(long), 0, |
| 242 | mon_text_ctor, mon_text_dtor); |
| 243 | if (rp->e_slab == NULL) { |
| 244 | rc = -ENOMEM; |
| 245 | goto err_slab; |
| 246 | } |
| 247 | |
| 248 | mon_reader_add(mbus, &rp->r); |
| 249 | |
| 250 | file->private_data = rp; |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 251 | mutex_unlock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return 0; |
| 253 | |
| 254 | // err_busy: |
| 255 | // kmem_cache_destroy(rp->e_slab); |
| 256 | err_slab: |
| 257 | kfree(rp->printf_buf); |
| 258 | err_alloc_pr: |
| 259 | kfree(rp); |
| 260 | err_alloc: |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 261 | mutex_unlock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return rc; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * For simplicity, we read one record in one system call and throw out |
| 267 | * what does not fit. This means that the following does not work: |
| 268 | * dd if=/dbg/usbmon/0t bs=10 |
| 269 | * Also, we do not allow seeks and do not bother advancing the offset. |
| 270 | */ |
| 271 | static ssize_t mon_text_read(struct file *file, char __user *buf, |
| 272 | size_t nbytes, loff_t *ppos) |
| 273 | { |
| 274 | struct mon_reader_text *rp = file->private_data; |
| 275 | struct mon_bus *mbus = rp->r.m_bus; |
| 276 | DECLARE_WAITQUEUE(waita, current); |
| 277 | struct mon_event_text *ep; |
| 278 | int cnt, limit; |
| 279 | char *pbuf; |
| 280 | char udir, utype; |
| 281 | int data_len, i; |
| 282 | |
| 283 | add_wait_queue(&rp->wait, &waita); |
| 284 | set_current_state(TASK_INTERRUPTIBLE); |
| 285 | while ((ep = mon_text_fetch(rp, mbus)) == NULL) { |
| 286 | if (file->f_flags & O_NONBLOCK) { |
| 287 | set_current_state(TASK_RUNNING); |
| 288 | remove_wait_queue(&rp->wait, &waita); |
| 289 | return -EWOULDBLOCK; /* Same as EAGAIN in Linux */ |
| 290 | } |
| 291 | /* |
| 292 | * We do not count nwaiters, because ->release is supposed |
| 293 | * to be called when all openers are gone only. |
| 294 | */ |
| 295 | schedule(); |
| 296 | if (signal_pending(current)) { |
| 297 | remove_wait_queue(&rp->wait, &waita); |
| 298 | return -EINTR; |
| 299 | } |
| 300 | set_current_state(TASK_INTERRUPTIBLE); |
| 301 | } |
| 302 | set_current_state(TASK_RUNNING); |
| 303 | remove_wait_queue(&rp->wait, &waita); |
| 304 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 305 | mutex_lock(&rp->printf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | cnt = 0; |
| 307 | pbuf = rp->printf_buf; |
| 308 | limit = rp->printf_size; |
| 309 | |
| 310 | udir = usb_pipein(ep->pipe) ? 'i' : 'o'; |
| 311 | switch (usb_pipetype(ep->pipe)) { |
| 312 | case PIPE_ISOCHRONOUS: utype = 'Z'; break; |
| 313 | case PIPE_INTERRUPT: utype = 'I'; break; |
| 314 | case PIPE_CONTROL: utype = 'C'; break; |
| 315 | default: /* PIPE_BULK */ utype = 'B'; |
| 316 | } |
| 317 | cnt += snprintf(pbuf + cnt, limit - cnt, |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 318 | "%lx %u %c %c%c:%03u:%02u", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | ep->id, ep->tstamp, ep->type, |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 320 | utype, udir, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe)); |
| 321 | |
| 322 | if (ep->setup_flag == 0) { /* Setup packet is present and captured */ |
| 323 | cnt += snprintf(pbuf + cnt, limit - cnt, |
| 324 | " s %02x %02x %04x %04x %04x", |
| 325 | ep->setup[0], |
| 326 | ep->setup[1], |
| 327 | (ep->setup[3] << 8) | ep->setup[2], |
| 328 | (ep->setup[5] << 8) | ep->setup[4], |
| 329 | (ep->setup[7] << 8) | ep->setup[6]); |
| 330 | } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */ |
| 331 | cnt += snprintf(pbuf + cnt, limit - cnt, |
| 332 | " %c __ __ ____ ____ ____", ep->setup_flag); |
| 333 | } else { /* No setup for this kind of URB */ |
| 334 | cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->status); |
| 335 | } |
| 336 | cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
| 338 | if ((data_len = ep->length) > 0) { |
| 339 | if (ep->data_flag == 0) { |
| 340 | cnt += snprintf(pbuf + cnt, limit - cnt, " ="); |
| 341 | if (data_len >= DATA_MAX) |
| 342 | data_len = DATA_MAX; |
| 343 | for (i = 0; i < data_len; i++) { |
| 344 | if (i % 4 == 0) { |
| 345 | cnt += snprintf(pbuf + cnt, limit - cnt, |
| 346 | " "); |
| 347 | } |
| 348 | cnt += snprintf(pbuf + cnt, limit - cnt, |
| 349 | "%02x", ep->data[i]); |
| 350 | } |
| 351 | cnt += snprintf(pbuf + cnt, limit - cnt, "\n"); |
| 352 | } else { |
| 353 | cnt += snprintf(pbuf + cnt, limit - cnt, |
| 354 | " %c\n", ep->data_flag); |
| 355 | } |
| 356 | } else { |
| 357 | cnt += snprintf(pbuf + cnt, limit - cnt, "\n"); |
| 358 | } |
| 359 | |
| 360 | if (copy_to_user(buf, rp->printf_buf, cnt)) |
| 361 | cnt = -EFAULT; |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 362 | mutex_unlock(&rp->printf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | kmem_cache_free(rp->e_slab, ep); |
| 364 | return cnt; |
| 365 | } |
| 366 | |
| 367 | static int mon_text_release(struct inode *inode, struct file *file) |
| 368 | { |
| 369 | struct mon_reader_text *rp = file->private_data; |
| 370 | struct mon_bus *mbus; |
| 371 | /* unsigned long flags; */ |
| 372 | struct list_head *p; |
| 373 | struct mon_event_text *ep; |
| 374 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 375 | mutex_lock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | mbus = inode->u.generic_ip; |
| 377 | |
| 378 | if (mbus->nreaders <= 0) { |
| 379 | printk(KERN_ERR TAG ": consistency error on close\n"); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 380 | mutex_unlock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | mon_reader_del(mbus, &rp->r); |
| 384 | |
| 385 | /* |
| 386 | * In theory, e_list is protected by mbus->lock. However, |
| 387 | * after mon_reader_del has finished, the following is the case: |
| 388 | * - we are not on reader list anymore, so new events won't be added; |
| 389 | * - whole mbus may be dropped if it was orphaned. |
| 390 | * So, we better not touch mbus. |
| 391 | */ |
| 392 | /* spin_lock_irqsave(&mbus->lock, flags); */ |
| 393 | while (!list_empty(&rp->e_list)) { |
| 394 | p = rp->e_list.next; |
| 395 | ep = list_entry(p, struct mon_event_text, e_link); |
| 396 | list_del(p); |
| 397 | --rp->nevents; |
| 398 | kmem_cache_free(rp->e_slab, ep); |
| 399 | } |
| 400 | /* spin_unlock_irqrestore(&mbus->lock, flags); */ |
| 401 | |
| 402 | kmem_cache_destroy(rp->e_slab); |
| 403 | kfree(rp->printf_buf); |
| 404 | kfree(rp); |
| 405 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 406 | mutex_unlock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | struct file_operations mon_fops_text = { |
| 411 | .owner = THIS_MODULE, |
| 412 | .open = mon_text_open, |
| 413 | .llseek = no_llseek, |
| 414 | .read = mon_text_read, |
| 415 | /* .write = mon_text_write, */ |
| 416 | /* .poll = mon_text_poll, */ |
| 417 | /* .ioctl = mon_text_ioctl, */ |
| 418 | .release = mon_text_release, |
| 419 | }; |
| 420 | |
| 421 | /* |
| 422 | * Slab interface: constructor. |
| 423 | */ |
| 424 | static void mon_text_ctor(void *mem, kmem_cache_t *slab, unsigned long sflags) |
| 425 | { |
| 426 | /* |
| 427 | * Nothing to initialize. No, really! |
| 428 | * So, we fill it with garbage to emulate a reused object. |
| 429 | */ |
| 430 | memset(mem, 0xe5, sizeof(struct mon_event_text)); |
| 431 | } |
| 432 | |
| 433 | static void mon_text_dtor(void *mem, kmem_cache_t *slab, unsigned long sflags) |
| 434 | { |
| 435 | ; |
| 436 | } |