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 <asm/uaccess.h>
16 * No, we do not want arbitrarily long data strings.
17 * Use the binary interface if you want to capture bulk data!
22 * Defined by USB 2.0 clause 9.3, table 9.2.
27 * This limit exists to prevent OOMs when the user process stops reading.
31 #define PRINTF_DFL 130
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 */
39 int length; /* Depends on type: xfer length or act length */
43 unsigned char setup[SETUP_MAX];
44 unsigned char data[DATA_MAX];
47 #define SLAB_NAME_SZ 30
48 struct mon_reader_text {
51 struct list_head e_list;
52 struct mon_reader r; /* In C, parent class can be placed anywhere */
54 wait_queue_head_t wait;
57 struct semaphore printf_lock;
59 char slab_name[SLAB_NAME_SZ];
62 static void mon_text_ctor(void *, kmem_cache_t *, unsigned long);
63 static void mon_text_dtor(void *, kmem_cache_t *, unsigned long);
69 * May be called from an interrupt.
71 * This is called with the whole mon_bus locked, so no additional lock.
74 static inline char mon_text_get_setup(struct mon_event_text *ep,
75 struct urb *urb, char ev_type)
78 if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
81 if (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)
83 if (urb->setup_packet == NULL)
84 return 'Z'; /* '0' would be not as pretty. */
86 memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
90 static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
91 int len, char ev_type)
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.
105 if (usb_pipebulk(pipe) || usb_pipecontrol(pipe)) {
106 if (usb_pipein(pipe)) {
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
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.
124 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
125 return mon_dmapeek(ep->data, urb->transfer_dma, len);
127 if (urb->transfer_buffer == NULL)
128 return 'Z'; /* '0' would be not as pretty. */
130 memcpy(ep->data, urb->transfer_buffer, len);
134 static inline unsigned int mon_get_timestamp(void)
139 do_gettimeofday(&tval);
140 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
141 stamp = stamp * 1000000 + tval.tv_usec;
145 static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
148 struct mon_event_text *ep;
151 stamp = mon_get_timestamp();
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++;
160 ep->pipe = urb->pipe;
161 ep->id = (unsigned long) urb;
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;
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);
172 list_add_tail(&ep->e_link, &rp->e_list);
176 static void mon_text_submit(void *data, struct urb *urb)
178 struct mon_reader_text *rp = data;
179 mon_text_event(rp, urb, 'S');
182 static void mon_text_complete(void *data, struct urb *urb)
184 struct mon_reader_text *rp = data;
185 mon_text_event(rp, urb, 'C');
189 * Fetch next event from the circular buffer.
191 static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
192 struct mon_bus *mbus)
197 spin_lock_irqsave(&mbus->lock, flags);
198 if (list_empty(&rp->e_list)) {
199 spin_unlock_irqrestore(&mbus->lock, flags);
205 spin_unlock_irqrestore(&mbus->lock, flags);
206 return list_entry(p, struct mon_event_text, e_link);
211 static int mon_text_open(struct inode *inode, struct file *file)
213 struct mon_bus *mbus;
214 struct usb_bus *ubus;
215 struct mon_reader_text *rp;
219 mbus = inode->u.generic_ip;
222 rp = kmalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
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);
232 rp->printf_size = PRINTF_DFL;
233 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
234 if (rp->printf_buf == NULL) {
241 rp->r.rnf_submit = mon_text_submit;
242 rp->r.rnf_complete = mon_text_complete;
244 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
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) {
254 mon_reader_add(mbus, &rp->r);
256 file->private_data = rp;
261 // kmem_cache_destroy(rp->e_slab);
263 kfree(rp->printf_buf);
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.
277 static ssize_t mon_text_read(struct file *file, char __user *buf,
278 size_t nbytes, loff_t *ppos)
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;
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 */
298 * We do not count nwaiters, because ->release is supposed
299 * to be called when all openers are gone only.
302 if (signal_pending(current)) {
303 remove_wait_queue(&rp->wait, &waita);
306 set_current_state(TASK_INTERRUPTIBLE);
308 set_current_state(TASK_RUNNING);
309 remove_wait_queue(&rp->wait, &waita);
311 down(&rp->printf_lock);
313 pbuf = rp->printf_buf;
314 limit = rp->printf_size;
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';
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));
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",
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);
342 cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->length);
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)
349 for (i = 0; i < data_len; i++) {
351 cnt += snprintf(pbuf + cnt, limit - cnt,
354 cnt += snprintf(pbuf + cnt, limit - cnt,
355 "%02x", ep->data[i]);
357 cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
359 cnt += snprintf(pbuf + cnt, limit - cnt,
360 " %c\n", ep->data_flag);
363 cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
366 if (copy_to_user(buf, rp->printf_buf, cnt))
368 up(&rp->printf_lock);
369 kmem_cache_free(rp->e_slab, ep);
373 static int mon_text_release(struct inode *inode, struct file *file)
375 struct mon_reader_text *rp = file->private_data;
376 struct mon_bus *mbus;
377 /* unsigned long flags; */
379 struct mon_event_text *ep;
382 mbus = inode->u.generic_ip;
384 if (mbus->nreaders <= 0) {
385 printk(KERN_ERR TAG ": consistency error on close\n");
389 mon_reader_del(mbus, &rp->r);
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.
398 /* spin_lock_irqsave(&mbus->lock, flags); */
399 while (!list_empty(&rp->e_list)) {
401 ep = list_entry(p, struct mon_event_text, e_link);
404 kmem_cache_free(rp->e_slab, ep);
406 /* spin_unlock_irqrestore(&mbus->lock, flags); */
408 kmem_cache_destroy(rp->e_slab);
409 kfree(rp->printf_buf);
416 struct file_operations mon_fops_text = {
417 .owner = THIS_MODULE,
418 .open = mon_text_open,
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,
428 * Slab interface: constructor.
430 static void mon_text_ctor(void *mem, kmem_cache_t *slab, unsigned long sflags)
433 * Nothing to initialize. No, really!
434 * So, we fill it with garbage to emulate a reused object.
436 memset(mem, 0xe5, sizeof(struct mon_event_text));
439 static void mon_text_dtor(void *mem, kmem_cache_t *slab, unsigned long sflags)