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 * This limit exists to prevent OOMs when the user process stops reading.
26 #define PRINTF_DFL 120
28 struct mon_event_text {
29 struct list_head e_link;
30 int type; /* submit, complete, etc. */
31 unsigned int pipe; /* Pipe */
32 unsigned long id; /* From pointer, most of the time */
34 int length; /* Depends on type: xfer length or act length */
37 unsigned char data[DATA_MAX];
40 #define SLAB_NAME_SZ 30
41 struct mon_reader_text {
44 struct list_head e_list;
45 struct mon_reader r; /* In C, parent class can be placed anywhere */
47 wait_queue_head_t wait;
50 struct semaphore printf_lock;
52 char slab_name[SLAB_NAME_SZ];
55 static void mon_text_ctor(void *, kmem_cache_t *, unsigned long);
56 static void mon_text_dtor(void *, kmem_cache_t *, unsigned long);
62 * May be called from an interrupt.
64 * This is called with the whole mon_bus locked, so no additional lock.
67 static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
68 int len, char ev_type)
74 * The check to see if it's safe to poke at data has an enormous
75 * number of corner cases, but it seems that the following is
78 * We do not even try to look transfer_buffer, because it can
79 * contain non-NULL garbage in case the upper level promised to
80 * set DMA for the HCD.
82 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
88 if ((data = urb->transfer_buffer) == NULL)
89 return 'Z'; /* '0' would be not as pretty. */
92 * Bulk is easy to shortcut reliably.
93 * XXX Control needs setup packet taken.
94 * XXX Other pipe types need consideration. Currently, we overdo it
95 * and collect garbage for them: better more than less.
97 if (usb_pipebulk(pipe) || usb_pipecontrol(pipe)) {
98 if (usb_pipein(pipe)) {
109 memcpy(ep->data, urb->transfer_buffer, len);
113 static inline unsigned int mon_get_timestamp(void)
118 do_gettimeofday(&tval);
119 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
120 stamp = stamp * 1000000 + tval.tv_usec;
124 static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
127 struct mon_event_text *ep;
130 stamp = mon_get_timestamp();
132 if (rp->nevents >= EVENT_MAX ||
133 (ep = kmem_cache_alloc(rp->e_slab, SLAB_ATOMIC)) == NULL) {
134 rp->r.m_bus->cnt_text_lost++;
139 ep->pipe = urb->pipe;
140 ep->id = (unsigned long) urb;
142 ep->length = (ev_type == 'S') ?
143 urb->transfer_buffer_length : urb->actual_length;
144 /* Collecting status makes debugging sense for submits, too */
145 ep->status = urb->status;
147 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type);
150 list_add_tail(&ep->e_link, &rp->e_list);
154 static void mon_text_submit(void *data, struct urb *urb)
156 struct mon_reader_text *rp = data;
157 mon_text_event(rp, urb, 'S');
160 static void mon_text_complete(void *data, struct urb *urb)
162 struct mon_reader_text *rp = data;
163 mon_text_event(rp, urb, 'C');
167 * Fetch next event from the circular buffer.
169 static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
170 struct mon_bus *mbus)
175 spin_lock_irqsave(&mbus->lock, flags);
176 if (list_empty(&rp->e_list)) {
177 spin_unlock_irqrestore(&mbus->lock, flags);
183 spin_unlock_irqrestore(&mbus->lock, flags);
184 return list_entry(p, struct mon_event_text, e_link);
189 static int mon_text_open(struct inode *inode, struct file *file)
191 struct mon_bus *mbus;
192 struct usb_bus *ubus;
193 struct mon_reader_text *rp;
197 mbus = inode->u.generic_ip;
200 rp = kmalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
205 memset(rp, 0, sizeof(struct mon_reader_text));
206 INIT_LIST_HEAD(&rp->e_list);
207 init_waitqueue_head(&rp->wait);
208 init_MUTEX(&rp->printf_lock);
210 rp->printf_size = PRINTF_DFL;
211 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
212 if (rp->printf_buf == NULL) {
219 rp->r.rnf_submit = mon_text_submit;
220 rp->r.rnf_complete = mon_text_complete;
222 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
224 rp->e_slab = kmem_cache_create(rp->slab_name,
225 sizeof(struct mon_event_text), sizeof(long), 0,
226 mon_text_ctor, mon_text_dtor);
227 if (rp->e_slab == NULL) {
232 mon_reader_add(mbus, &rp->r);
234 file->private_data = rp;
239 // kmem_cache_destroy(rp->e_slab);
241 kfree(rp->printf_buf);
250 * For simplicity, we read one record in one system call and throw out
251 * what does not fit. This means that the following does not work:
252 * dd if=/dbg/usbmon/0t bs=10
253 * Also, we do not allow seeks and do not bother advancing the offset.
255 static ssize_t mon_text_read(struct file *file, char __user *buf,
256 size_t nbytes, loff_t *ppos)
258 struct mon_reader_text *rp = file->private_data;
259 struct mon_bus *mbus = rp->r.m_bus;
260 DECLARE_WAITQUEUE(waita, current);
261 struct mon_event_text *ep;
267 add_wait_queue(&rp->wait, &waita);
268 set_current_state(TASK_INTERRUPTIBLE);
269 while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
270 if (file->f_flags & O_NONBLOCK) {
271 set_current_state(TASK_RUNNING);
272 remove_wait_queue(&rp->wait, &waita);
273 return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
276 * We do not count nwaiters, because ->release is supposed
277 * to be called when all openers are gone only.
280 if (signal_pending(current)) {
281 remove_wait_queue(&rp->wait, &waita);
284 set_current_state(TASK_INTERRUPTIBLE);
286 set_current_state(TASK_RUNNING);
287 remove_wait_queue(&rp->wait, &waita);
289 down(&rp->printf_lock);
291 pbuf = rp->printf_buf;
292 limit = rp->printf_size;
294 udir = usb_pipein(ep->pipe) ? 'i' : 'o';
295 switch (usb_pipetype(ep->pipe)) {
296 case PIPE_ISOCHRONOUS: utype = 'Z'; break;
297 case PIPE_INTERRUPT: utype = 'I'; break;
298 case PIPE_CONTROL: utype = 'C'; break;
299 default: /* PIPE_BULK */ utype = 'B';
301 cnt += snprintf(pbuf + cnt, limit - cnt,
302 "%lx %u %c %c%c:%03u:%02u %d %d",
303 ep->id, ep->tstamp, ep->type,
304 utype, udir, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe),
305 ep->status, ep->length);
307 if ((data_len = ep->length) > 0) {
308 if (ep->data_flag == 0) {
309 cnt += snprintf(pbuf + cnt, limit - cnt, " =");
310 if (data_len >= DATA_MAX)
312 for (i = 0; i < data_len; i++) {
314 cnt += snprintf(pbuf + cnt, limit - cnt,
317 cnt += snprintf(pbuf + cnt, limit - cnt,
318 "%02x", ep->data[i]);
320 cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
322 cnt += snprintf(pbuf + cnt, limit - cnt,
323 " %c\n", ep->data_flag);
326 cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
329 if (copy_to_user(buf, rp->printf_buf, cnt))
331 up(&rp->printf_lock);
332 kmem_cache_free(rp->e_slab, ep);
336 static int mon_text_release(struct inode *inode, struct file *file)
338 struct mon_reader_text *rp = file->private_data;
339 struct mon_bus *mbus;
340 /* unsigned long flags; */
342 struct mon_event_text *ep;
345 mbus = inode->u.generic_ip;
347 if (mbus->nreaders <= 0) {
348 printk(KERN_ERR TAG ": consistency error on close\n");
352 mon_reader_del(mbus, &rp->r);
355 * In theory, e_list is protected by mbus->lock. However,
356 * after mon_reader_del has finished, the following is the case:
357 * - we are not on reader list anymore, so new events won't be added;
358 * - whole mbus may be dropped if it was orphaned.
359 * So, we better not touch mbus.
361 /* spin_lock_irqsave(&mbus->lock, flags); */
362 while (!list_empty(&rp->e_list)) {
364 ep = list_entry(p, struct mon_event_text, e_link);
367 kmem_cache_free(rp->e_slab, ep);
369 /* spin_unlock_irqrestore(&mbus->lock, flags); */
371 kmem_cache_destroy(rp->e_slab);
372 kfree(rp->printf_buf);
379 struct file_operations mon_fops_text = {
380 .owner = THIS_MODULE,
381 .open = mon_text_open,
383 .read = mon_text_read,
384 /* .write = mon_text_write, */
385 /* .poll = mon_text_poll, */
386 /* .ioctl = mon_text_ioctl, */
387 .release = mon_text_release,
391 * Slab interface: constructor.
393 static void mon_text_ctor(void *mem, kmem_cache_t *slab, unsigned long sflags)
396 * Nothing to initialize. No, really!
397 * So, we fill it with garbage to emulate a reused object.
399 memset(mem, 0xe5, sizeof(struct mon_event_text));
402 static void mon_text_dtor(void *mem, kmem_cache_t *slab, unsigned long sflags)