2 * The USB Monitor, inspired by Dave Harding's USBMon.
4 * This is a text format reader.
7 #include <linux/kernel.h>
8 #include <linux/list.h>
10 #include <linux/time.h>
11 #include <linux/mutex.h>
12 #include <asm/uaccess.h>
17 * No, we do not want arbitrarily long data strings.
18 * Use the binary interface if you want to capture bulk data!
23 * Defined by USB 2.0 clause 9.3, table 9.2.
28 * This limit exists to prevent OOMs when the user process stops reading.
29 * If usbmon were available to unprivileged processes, it might be open
30 * to a local DoS. But we have to keep to root in order to prevent
31 * password sniffing from HID devices.
33 #define EVENT_MAX (2*PAGE_SIZE / sizeof(struct mon_event_text))
35 #define PRINTF_DFL 160
37 struct mon_event_text {
38 struct list_head e_link;
39 int type; /* submit, complete, etc. */
40 unsigned int pipe; /* Pipe */
41 unsigned long id; /* From pointer, most of the time */
43 int length; /* Depends on type: xfer length or act length */
47 unsigned char setup[SETUP_MAX];
48 unsigned char data[DATA_MAX];
51 #define SLAB_NAME_SZ 30
52 struct mon_reader_text {
55 struct list_head e_list;
56 struct mon_reader r; /* In C, parent class can be placed anywhere */
58 wait_queue_head_t wait;
61 struct mutex printf_lock;
63 char slab_name[SLAB_NAME_SZ];
66 static void mon_text_ctor(void *, kmem_cache_t *, unsigned long);
72 * May be called from an interrupt.
74 * This is called with the whole mon_bus locked, so no additional lock.
77 static inline char mon_text_get_setup(struct mon_event_text *ep,
78 struct urb *urb, char ev_type, struct mon_bus *mbus)
81 if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
84 if (mbus->uses_dma && (urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
85 return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
86 if (urb->setup_packet == NULL)
87 return 'Z'; /* '0' would be not as pretty. */
89 memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
93 static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
94 int len, char ev_type, struct mon_bus *mbus)
103 if (usb_pipein(pipe)) {
112 * The check to see if it's safe to poke at data has an enormous
113 * number of corner cases, but it seems that the following is
116 * We do not even try to look at transfer_buffer, because it can
117 * contain non-NULL garbage in case the upper level promised to
118 * set DMA for the HCD.
120 if (mbus->uses_dma && (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
121 return mon_dmapeek(ep->data, urb->transfer_dma, len);
123 if (urb->transfer_buffer == NULL)
124 return 'Z'; /* '0' would be not as pretty. */
126 memcpy(ep->data, urb->transfer_buffer, len);
130 static inline unsigned int mon_get_timestamp(void)
135 do_gettimeofday(&tval);
136 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
137 stamp = stamp * 1000000 + tval.tv_usec;
141 static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
144 struct mon_event_text *ep;
147 stamp = mon_get_timestamp();
149 if (rp->nevents >= EVENT_MAX ||
150 (ep = kmem_cache_alloc(rp->e_slab, SLAB_ATOMIC)) == NULL) {
151 rp->r.m_bus->cnt_text_lost++;
156 ep->pipe = urb->pipe;
157 ep->id = (unsigned long) urb;
159 ep->length = (ev_type == 'S') ?
160 urb->transfer_buffer_length : urb->actual_length;
161 /* Collecting status makes debugging sense for submits, too */
162 ep->status = urb->status;
164 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
165 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
169 list_add_tail(&ep->e_link, &rp->e_list);
173 static void mon_text_submit(void *data, struct urb *urb)
175 struct mon_reader_text *rp = data;
176 mon_text_event(rp, urb, 'S');
179 static void mon_text_complete(void *data, struct urb *urb)
181 struct mon_reader_text *rp = data;
182 mon_text_event(rp, urb, 'C');
185 static void mon_text_error(void *data, struct urb *urb, int error)
187 struct mon_reader_text *rp = data;
188 struct mon_event_text *ep;
190 if (rp->nevents >= EVENT_MAX ||
191 (ep = kmem_cache_alloc(rp->e_slab, SLAB_ATOMIC)) == NULL) {
192 rp->r.m_bus->cnt_text_lost++;
197 ep->pipe = urb->pipe;
198 ep->id = (unsigned long) urb;
203 ep->setup_flag = '-';
207 list_add_tail(&ep->e_link, &rp->e_list);
212 * Fetch next event from the circular buffer.
214 static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
215 struct mon_bus *mbus)
220 spin_lock_irqsave(&mbus->lock, flags);
221 if (list_empty(&rp->e_list)) {
222 spin_unlock_irqrestore(&mbus->lock, flags);
228 spin_unlock_irqrestore(&mbus->lock, flags);
229 return list_entry(p, struct mon_event_text, e_link);
234 static int mon_text_open(struct inode *inode, struct file *file)
236 struct mon_bus *mbus;
237 struct usb_bus *ubus;
238 struct mon_reader_text *rp;
241 mutex_lock(&mon_lock);
242 mbus = inode->i_private;
245 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
250 INIT_LIST_HEAD(&rp->e_list);
251 init_waitqueue_head(&rp->wait);
252 mutex_init(&rp->printf_lock);
254 rp->printf_size = PRINTF_DFL;
255 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
256 if (rp->printf_buf == NULL) {
263 rp->r.rnf_submit = mon_text_submit;
264 rp->r.rnf_error = mon_text_error;
265 rp->r.rnf_complete = mon_text_complete;
267 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
269 rp->e_slab = kmem_cache_create(rp->slab_name,
270 sizeof(struct mon_event_text), sizeof(long), 0,
271 mon_text_ctor, NULL);
272 if (rp->e_slab == NULL) {
277 mon_reader_add(mbus, &rp->r);
279 file->private_data = rp;
280 mutex_unlock(&mon_lock);
284 // kmem_cache_destroy(rp->e_slab);
286 kfree(rp->printf_buf);
290 mutex_unlock(&mon_lock);
295 * For simplicity, we read one record in one system call and throw out
296 * what does not fit. This means that the following does not work:
297 * dd if=/dbg/usbmon/0t bs=10
298 * Also, we do not allow seeks and do not bother advancing the offset.
300 static ssize_t mon_text_read(struct file *file, char __user *buf,
301 size_t nbytes, loff_t *ppos)
303 struct mon_reader_text *rp = file->private_data;
304 struct mon_bus *mbus = rp->r.m_bus;
305 DECLARE_WAITQUEUE(waita, current);
306 struct mon_event_text *ep;
312 add_wait_queue(&rp->wait, &waita);
313 set_current_state(TASK_INTERRUPTIBLE);
314 while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
315 if (file->f_flags & O_NONBLOCK) {
316 set_current_state(TASK_RUNNING);
317 remove_wait_queue(&rp->wait, &waita);
318 return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
321 * We do not count nwaiters, because ->release is supposed
322 * to be called when all openers are gone only.
325 if (signal_pending(current)) {
326 remove_wait_queue(&rp->wait, &waita);
329 set_current_state(TASK_INTERRUPTIBLE);
331 set_current_state(TASK_RUNNING);
332 remove_wait_queue(&rp->wait, &waita);
334 mutex_lock(&rp->printf_lock);
336 pbuf = rp->printf_buf;
337 limit = rp->printf_size;
339 udir = usb_pipein(ep->pipe) ? 'i' : 'o';
340 switch (usb_pipetype(ep->pipe)) {
341 case PIPE_ISOCHRONOUS: utype = 'Z'; break;
342 case PIPE_INTERRUPT: utype = 'I'; break;
343 case PIPE_CONTROL: utype = 'C'; break;
344 default: /* PIPE_BULK */ utype = 'B';
346 cnt += snprintf(pbuf + cnt, limit - cnt,
347 "%lx %u %c %c%c:%03u:%02u",
348 ep->id, ep->tstamp, ep->type,
349 utype, udir, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
351 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
352 cnt += snprintf(pbuf + cnt, limit - cnt,
353 " s %02x %02x %04x %04x %04x",
356 (ep->setup[3] << 8) | ep->setup[2],
357 (ep->setup[5] << 8) | ep->setup[4],
358 (ep->setup[7] << 8) | ep->setup[6]);
359 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
360 cnt += snprintf(pbuf + cnt, limit - cnt,
361 " %c __ __ ____ ____ ____", ep->setup_flag);
362 } else { /* No setup for this kind of URB */
363 cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->status);
365 cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->length);
367 if ((data_len = ep->length) > 0) {
368 if (ep->data_flag == 0) {
369 cnt += snprintf(pbuf + cnt, limit - cnt, " =");
370 if (data_len >= DATA_MAX)
372 for (i = 0; i < data_len; i++) {
374 cnt += snprintf(pbuf + cnt, limit - cnt,
377 cnt += snprintf(pbuf + cnt, limit - cnt,
378 "%02x", ep->data[i]);
380 cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
382 cnt += snprintf(pbuf + cnt, limit - cnt,
383 " %c\n", ep->data_flag);
386 cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
389 if (copy_to_user(buf, rp->printf_buf, cnt))
391 mutex_unlock(&rp->printf_lock);
392 kmem_cache_free(rp->e_slab, ep);
396 static int mon_text_release(struct inode *inode, struct file *file)
398 struct mon_reader_text *rp = file->private_data;
399 struct mon_bus *mbus;
400 /* unsigned long flags; */
402 struct mon_event_text *ep;
404 mutex_lock(&mon_lock);
405 mbus = inode->i_private;
407 if (mbus->nreaders <= 0) {
408 printk(KERN_ERR TAG ": consistency error on close\n");
409 mutex_unlock(&mon_lock);
412 mon_reader_del(mbus, &rp->r);
415 * In theory, e_list is protected by mbus->lock. However,
416 * after mon_reader_del has finished, the following is the case:
417 * - we are not on reader list anymore, so new events won't be added;
418 * - whole mbus may be dropped if it was orphaned.
419 * So, we better not touch mbus.
421 /* spin_lock_irqsave(&mbus->lock, flags); */
422 while (!list_empty(&rp->e_list)) {
424 ep = list_entry(p, struct mon_event_text, e_link);
427 kmem_cache_free(rp->e_slab, ep);
429 /* spin_unlock_irqrestore(&mbus->lock, flags); */
431 kmem_cache_destroy(rp->e_slab);
432 kfree(rp->printf_buf);
435 mutex_unlock(&mon_lock);
439 const struct file_operations mon_fops_text = {
440 .owner = THIS_MODULE,
441 .open = mon_text_open,
443 .read = mon_text_read,
444 /* .write = mon_text_write, */
445 /* .poll = mon_text_poll, */
446 /* .ioctl = mon_text_ioctl, */
447 .release = mon_text_release,
451 * Slab interface: constructor.
453 static void mon_text_ctor(void *mem, kmem_cache_t *slab, unsigned long sflags)
456 * Nothing to initialize. No, really!
457 * So, we fill it with garbage to emulate a reused object.
459 memset(mem, 0xe5, sizeof(struct mon_event_text));