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.
32 #define PRINTF_DFL 130
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 */
40 int length; /* Depends on type: xfer length or act length */
44 unsigned char setup[SETUP_MAX];
45 unsigned char data[DATA_MAX];
48 #define SLAB_NAME_SZ 30
49 struct mon_reader_text {
52 struct list_head e_list;
53 struct mon_reader r; /* In C, parent class can be placed anywhere */
55 wait_queue_head_t wait;
58 struct mutex printf_lock;
60 char slab_name[SLAB_NAME_SZ];
63 static void mon_text_ctor(void *, kmem_cache_t *, unsigned long);
64 static void mon_text_dtor(void *, kmem_cache_t *, unsigned long);
70 * May be called from an interrupt.
72 * This is called with the whole mon_bus locked, so no additional lock.
75 static inline char mon_text_get_setup(struct mon_event_text *ep,
76 struct urb *urb, char ev_type)
79 if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
82 if (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)
83 return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
84 if (urb->setup_packet == NULL)
85 return 'Z'; /* '0' would be not as pretty. */
87 memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
91 static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
92 int len, char ev_type)
101 if (usb_pipein(pipe)) {
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
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.
118 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
119 return mon_dmapeek(ep->data, urb->transfer_dma, len);
121 if (urb->transfer_buffer == NULL)
122 return 'Z'; /* '0' would be not as pretty. */
124 memcpy(ep->data, urb->transfer_buffer, len);
128 static inline unsigned int mon_get_timestamp(void)
133 do_gettimeofday(&tval);
134 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
135 stamp = stamp * 1000000 + tval.tv_usec;
139 static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
142 struct mon_event_text *ep;
145 stamp = mon_get_timestamp();
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++;
154 ep->pipe = urb->pipe;
155 ep->id = (unsigned long) urb;
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;
162 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type);
163 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type);
166 list_add_tail(&ep->e_link, &rp->e_list);
170 static void mon_text_submit(void *data, struct urb *urb)
172 struct mon_reader_text *rp = data;
173 mon_text_event(rp, urb, 'S');
176 static void mon_text_complete(void *data, struct urb *urb)
178 struct mon_reader_text *rp = data;
179 mon_text_event(rp, urb, 'C');
183 * Fetch next event from the circular buffer.
185 static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
186 struct mon_bus *mbus)
191 spin_lock_irqsave(&mbus->lock, flags);
192 if (list_empty(&rp->e_list)) {
193 spin_unlock_irqrestore(&mbus->lock, flags);
199 spin_unlock_irqrestore(&mbus->lock, flags);
200 return list_entry(p, struct mon_event_text, e_link);
205 static int mon_text_open(struct inode *inode, struct file *file)
207 struct mon_bus *mbus;
208 struct usb_bus *ubus;
209 struct mon_reader_text *rp;
212 mutex_lock(&mon_lock);
213 mbus = inode->u.generic_ip;
216 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
221 INIT_LIST_HEAD(&rp->e_list);
222 init_waitqueue_head(&rp->wait);
223 mutex_init(&rp->printf_lock);
225 rp->printf_size = PRINTF_DFL;
226 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
227 if (rp->printf_buf == NULL) {
234 rp->r.rnf_submit = mon_text_submit;
235 rp->r.rnf_complete = mon_text_complete;
237 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
239 rp->e_slab = kmem_cache_create(rp->slab_name,
240 sizeof(struct mon_event_text), sizeof(long), 0,
241 mon_text_ctor, mon_text_dtor);
242 if (rp->e_slab == NULL) {
247 mon_reader_add(mbus, &rp->r);
249 file->private_data = rp;
250 mutex_unlock(&mon_lock);
254 // kmem_cache_destroy(rp->e_slab);
256 kfree(rp->printf_buf);
260 mutex_unlock(&mon_lock);
265 * For simplicity, we read one record in one system call and throw out
266 * what does not fit. This means that the following does not work:
267 * dd if=/dbg/usbmon/0t bs=10
268 * Also, we do not allow seeks and do not bother advancing the offset.
270 static ssize_t mon_text_read(struct file *file, char __user *buf,
271 size_t nbytes, loff_t *ppos)
273 struct mon_reader_text *rp = file->private_data;
274 struct mon_bus *mbus = rp->r.m_bus;
275 DECLARE_WAITQUEUE(waita, current);
276 struct mon_event_text *ep;
282 add_wait_queue(&rp->wait, &waita);
283 set_current_state(TASK_INTERRUPTIBLE);
284 while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
285 if (file->f_flags & O_NONBLOCK) {
286 set_current_state(TASK_RUNNING);
287 remove_wait_queue(&rp->wait, &waita);
288 return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
291 * We do not count nwaiters, because ->release is supposed
292 * to be called when all openers are gone only.
295 if (signal_pending(current)) {
296 remove_wait_queue(&rp->wait, &waita);
299 set_current_state(TASK_INTERRUPTIBLE);
301 set_current_state(TASK_RUNNING);
302 remove_wait_queue(&rp->wait, &waita);
304 mutex_lock(&rp->printf_lock);
306 pbuf = rp->printf_buf;
307 limit = rp->printf_size;
309 udir = usb_pipein(ep->pipe) ? 'i' : 'o';
310 switch (usb_pipetype(ep->pipe)) {
311 case PIPE_ISOCHRONOUS: utype = 'Z'; break;
312 case PIPE_INTERRUPT: utype = 'I'; break;
313 case PIPE_CONTROL: utype = 'C'; break;
314 default: /* PIPE_BULK */ utype = 'B';
316 cnt += snprintf(pbuf + cnt, limit - cnt,
317 "%lx %u %c %c%c:%03u:%02u",
318 ep->id, ep->tstamp, ep->type,
319 utype, udir, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
321 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
322 cnt += snprintf(pbuf + cnt, limit - cnt,
323 " s %02x %02x %04x %04x %04x",
326 (ep->setup[3] << 8) | ep->setup[2],
327 (ep->setup[5] << 8) | ep->setup[4],
328 (ep->setup[7] << 8) | ep->setup[6]);
329 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
330 cnt += snprintf(pbuf + cnt, limit - cnt,
331 " %c __ __ ____ ____ ____", ep->setup_flag);
332 } else { /* No setup for this kind of URB */
333 cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->status);
335 cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->length);
337 if ((data_len = ep->length) > 0) {
338 if (ep->data_flag == 0) {
339 cnt += snprintf(pbuf + cnt, limit - cnt, " =");
340 if (data_len >= DATA_MAX)
342 for (i = 0; i < data_len; i++) {
344 cnt += snprintf(pbuf + cnt, limit - cnt,
347 cnt += snprintf(pbuf + cnt, limit - cnt,
348 "%02x", ep->data[i]);
350 cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
352 cnt += snprintf(pbuf + cnt, limit - cnt,
353 " %c\n", ep->data_flag);
356 cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
359 if (copy_to_user(buf, rp->printf_buf, cnt))
361 mutex_unlock(&rp->printf_lock);
362 kmem_cache_free(rp->e_slab, ep);
366 static int mon_text_release(struct inode *inode, struct file *file)
368 struct mon_reader_text *rp = file->private_data;
369 struct mon_bus *mbus;
370 /* unsigned long flags; */
372 struct mon_event_text *ep;
374 mutex_lock(&mon_lock);
375 mbus = inode->u.generic_ip;
377 if (mbus->nreaders <= 0) {
378 printk(KERN_ERR TAG ": consistency error on close\n");
379 mutex_unlock(&mon_lock);
382 mon_reader_del(mbus, &rp->r);
385 * In theory, e_list is protected by mbus->lock. However,
386 * after mon_reader_del has finished, the following is the case:
387 * - we are not on reader list anymore, so new events won't be added;
388 * - whole mbus may be dropped if it was orphaned.
389 * So, we better not touch mbus.
391 /* spin_lock_irqsave(&mbus->lock, flags); */
392 while (!list_empty(&rp->e_list)) {
394 ep = list_entry(p, struct mon_event_text, e_link);
397 kmem_cache_free(rp->e_slab, ep);
399 /* spin_unlock_irqrestore(&mbus->lock, flags); */
401 kmem_cache_destroy(rp->e_slab);
402 kfree(rp->printf_buf);
405 mutex_unlock(&mon_lock);
409 struct file_operations mon_fops_text = {
410 .owner = THIS_MODULE,
411 .open = mon_text_open,
413 .read = mon_text_read,
414 /* .write = mon_text_write, */
415 /* .poll = mon_text_poll, */
416 /* .ioctl = mon_text_ioctl, */
417 .release = mon_text_release,
421 * Slab interface: constructor.
423 static void mon_text_ctor(void *mem, kmem_cache_t *slab, unsigned long sflags)
426 * Nothing to initialize. No, really!
427 * So, we fill it with garbage to emulate a reused object.
429 memset(mem, 0xe5, sizeof(struct mon_event_text));
432 static void mon_text_dtor(void *mem, kmem_cache_t *slab, unsigned long sflags)