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