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