blob: ec0cc51e39acc42b0576b15c99bced374228fc2e [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>
Pete Zaitcev6f23ee12006-12-30 22:43:10 -080012#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/uaccess.h>
14
15#include "usb_mon.h"
16
17/*
18 * No, we do not want arbitrarily long data strings.
19 * Use the binary interface if you want to capture bulk data!
20 */
21#define DATA_MAX 32
22
23/*
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070024 * Defined by USB 2.0 clause 9.3, table 9.2.
25 */
26#define SETUP_MAX 8
27
28/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * This limit exists to prevent OOMs when the user process stops reading.
Pete Zaitcev5b1c6742006-06-09 20:10:10 -070030 * If usbmon were available to unprivileged processes, it might be open
31 * to a local DoS. But we have to keep to root in order to prevent
32 * password sniffing from HID devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080034#define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080036/*
37 * Potentially unlimited number; we limit it for similar allocations.
38 * The usbfs limits this to 128, but we're not quite as generous.
39 */
40#define ISODESC_MAX 5
41
42#define PRINTF_DFL 250 /* with 5 ISOs segs */
43
44struct mon_iso_desc {
45 int status;
46 unsigned int offset;
47 unsigned int length; /* Unsigned here, signed in URB. Historic. */
48};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50struct mon_event_text {
51 struct list_head e_link;
52 int type; /* submit, complete, etc. */
53 unsigned int pipe; /* Pipe */
54 unsigned long id; /* From pointer, most of the time */
55 unsigned int tstamp;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080056 int busnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int length; /* Depends on type: xfer length or act length */
58 int status;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080059 int interval;
60 int start_frame;
61 int error_count;
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070062 char setup_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 char data_flag;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080064 int numdesc; /* Full number */
65 struct mon_iso_desc isodesc[ISODESC_MAX];
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070066 unsigned char setup[SETUP_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 unsigned char data[DATA_MAX];
68};
69
70#define SLAB_NAME_SZ 30
71struct mon_reader_text {
Christoph Lametere18b8902006-12-06 20:33:20 -080072 struct kmem_cache *e_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 int nevents;
74 struct list_head e_list;
75 struct mon_reader r; /* In C, parent class can be placed anywhere */
76
77 wait_queue_head_t wait;
78 int printf_size;
79 char *printf_buf;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010080 struct mutex printf_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 char slab_name[SLAB_NAME_SZ];
83};
84
Pete Zaitcev6f23ee12006-12-30 22:43:10 -080085static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
86
Christoph Lametere18b8902006-12-06 20:33:20 -080087static void mon_text_ctor(void *, struct kmem_cache *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080089struct mon_text_ptr {
90 int cnt, limit;
91 char *pbuf;
92};
93
94static struct mon_event_text *
95 mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
96static void mon_text_read_head_t(struct mon_reader_text *rp,
97 struct mon_text_ptr *p, const struct mon_event_text *ep);
98static void mon_text_read_head_u(struct mon_reader_text *rp,
99 struct mon_text_ptr *p, const struct mon_event_text *ep);
100static void mon_text_read_statset(struct mon_reader_text *rp,
101 struct mon_text_ptr *p, const struct mon_event_text *ep);
102static void mon_text_read_intstat(struct mon_reader_text *rp,
103 struct mon_text_ptr *p, const struct mon_event_text *ep);
104static void mon_text_read_isostat(struct mon_reader_text *rp,
105 struct mon_text_ptr *p, const struct mon_event_text *ep);
106static void mon_text_read_isodesc(struct mon_reader_text *rp,
107 struct mon_text_ptr *p, const struct mon_event_text *ep);
108static void mon_text_read_data(struct mon_reader_text *rp,
109 struct mon_text_ptr *p, const struct mon_event_text *ep);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * mon_text_submit
113 * mon_text_complete
114 *
115 * May be called from an interrupt.
116 *
117 * This is called with the whole mon_bus locked, so no additional lock.
118 */
119
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700120static inline char mon_text_get_setup(struct mon_event_text *ep,
Alan Stern4d6cd482006-08-30 11:35:21 -0400121 struct urb *urb, char ev_type, struct mon_bus *mbus)
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700122{
123
124 if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
125 return '-';
126
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700127 if (urb->dev->bus->uses_dma &&
128 (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
Pete Zaitcevbc506512005-09-01 14:35:05 -0700129 return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700130 }
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700131 if (urb->setup_packet == NULL)
132 return 'Z'; /* '0' would be not as pretty. */
133
134 memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
135 return 0;
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
Alan Stern4d6cd482006-08-30 11:35:21 -0400139 int len, char ev_type, struct mon_bus *mbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 if (len <= 0)
144 return 'L';
Pete Zaitcev02568392005-08-15 16:53:57 -0700145 if (len >= DATA_MAX)
146 len = DATA_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Pete Zaitcevb9b09422005-12-03 21:52:10 -0800148 if (usb_pipein(pipe)) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800149 if (ev_type != 'C')
Pete Zaitcevb9b09422005-12-03 21:52:10 -0800150 return '<';
151 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800152 if (ev_type != 'S')
Pete Zaitcevb9b09422005-12-03 21:52:10 -0800153 return '>';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155
Pete Zaitcev02568392005-08-15 16:53:57 -0700156 /*
157 * The check to see if it's safe to poke at data has an enormous
158 * number of corner cases, but it seems that the following is
159 * more or less safe.
160 *
Pete Zaitcev5b1c6742006-06-09 20:10:10 -0700161 * We do not even try to look at transfer_buffer, because it can
Pete Zaitcev02568392005-08-15 16:53:57 -0700162 * contain non-NULL garbage in case the upper level promised to
163 * set DMA for the HCD.
164 */
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700165 if (urb->dev->bus->uses_dma &&
166 (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
Pete Zaitcev02568392005-08-15 16:53:57 -0700167 return mon_dmapeek(ep->data, urb->transfer_dma, len);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700168 }
Pete Zaitcev02568392005-08-15 16:53:57 -0700169
170 if (urb->transfer_buffer == NULL)
171 return 'Z'; /* '0' would be not as pretty. */
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 memcpy(ep->data, urb->transfer_buffer, len);
174 return 0;
175}
176
177static inline unsigned int mon_get_timestamp(void)
178{
179 struct timeval tval;
180 unsigned int stamp;
181
182 do_gettimeofday(&tval);
183 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
184 stamp = stamp * 1000000 + tval.tv_usec;
185 return stamp;
186}
187
188static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
189 char ev_type)
190{
191 struct mon_event_text *ep;
192 unsigned int stamp;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800193 struct usb_iso_packet_descriptor *fp;
194 struct mon_iso_desc *dp;
195 int i, ndesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 stamp = mon_get_timestamp();
198
199 if (rp->nevents >= EVENT_MAX ||
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800200 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 rp->r.m_bus->cnt_text_lost++;
202 return;
203 }
204
205 ep->type = ev_type;
206 ep->pipe = urb->pipe;
207 ep->id = (unsigned long) urb;
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700208 ep->busnum = urb->dev->bus->busnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ep->tstamp = stamp;
210 ep->length = (ev_type == 'S') ?
211 urb->transfer_buffer_length : urb->actual_length;
212 /* Collecting status makes debugging sense for submits, too */
213 ep->status = urb->status;
214
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800215 if (usb_pipeint(urb->pipe)) {
216 ep->interval = urb->interval;
217 } else if (usb_pipeisoc(urb->pipe)) {
218 ep->interval = urb->interval;
219 ep->start_frame = urb->start_frame;
220 ep->error_count = urb->error_count;
221 }
222 ep->numdesc = urb->number_of_packets;
223 if (usb_pipeisoc(urb->pipe) && urb->number_of_packets > 0) {
224 if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
225 ndesc = ISODESC_MAX;
226 fp = urb->iso_frame_desc;
227 dp = ep->isodesc;
228 for (i = 0; i < ndesc; i++) {
229 dp->status = fp->status;
230 dp->offset = fp->offset;
231 dp->length = (ev_type == 'S') ?
232 fp->length : fp->actual_length;
233 fp++;
234 dp++;
235 }
236 }
237
Alan Stern4d6cd482006-08-30 11:35:21 -0400238 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
239 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
240 rp->r.m_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 rp->nevents++;
243 list_add_tail(&ep->e_link, &rp->e_list);
244 wake_up(&rp->wait);
245}
246
247static void mon_text_submit(void *data, struct urb *urb)
248{
249 struct mon_reader_text *rp = data;
250 mon_text_event(rp, urb, 'S');
251}
252
253static void mon_text_complete(void *data, struct urb *urb)
254{
255 struct mon_reader_text *rp = data;
256 mon_text_event(rp, urb, 'C');
257}
258
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700259static void mon_text_error(void *data, struct urb *urb, int error)
260{
261 struct mon_reader_text *rp = data;
262 struct mon_event_text *ep;
263
264 if (rp->nevents >= EVENT_MAX ||
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800265 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700266 rp->r.m_bus->cnt_text_lost++;
267 return;
268 }
269
270 ep->type = 'E';
271 ep->pipe = urb->pipe;
272 ep->id = (unsigned long) urb;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800273 ep->busnum = 0;
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700274 ep->tstamp = 0;
275 ep->length = 0;
276 ep->status = error;
277
278 ep->setup_flag = '-';
279 ep->data_flag = 'E';
280
281 rp->nevents++;
282 list_add_tail(&ep->e_link, &rp->e_list);
283 wake_up(&rp->wait);
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*
287 * Fetch next event from the circular buffer.
288 */
289static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
290 struct mon_bus *mbus)
291{
292 struct list_head *p;
293 unsigned long flags;
294
295 spin_lock_irqsave(&mbus->lock, flags);
296 if (list_empty(&rp->e_list)) {
297 spin_unlock_irqrestore(&mbus->lock, flags);
298 return NULL;
299 }
300 p = rp->e_list.next;
301 list_del(p);
302 --rp->nevents;
303 spin_unlock_irqrestore(&mbus->lock, flags);
304 return list_entry(p, struct mon_event_text, e_link);
305}
306
307/*
308 */
309static int mon_text_open(struct inode *inode, struct file *file)
310{
311 struct mon_bus *mbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct mon_reader_text *rp;
313 int rc;
314
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100315 mutex_lock(&mon_lock);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700316 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100318 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (rp == NULL) {
320 rc = -ENOMEM;
321 goto err_alloc;
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 INIT_LIST_HEAD(&rp->e_list);
324 init_waitqueue_head(&rp->wait);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100325 mutex_init(&rp->printf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 rp->printf_size = PRINTF_DFL;
328 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
329 if (rp->printf_buf == NULL) {
330 rc = -ENOMEM;
331 goto err_alloc_pr;
332 }
333
334 rp->r.m_bus = mbus;
335 rp->r.r_data = rp;
336 rp->r.rnf_submit = mon_text_submit;
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700337 rp->r.rnf_error = mon_text_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 rp->r.rnf_complete = mon_text_complete;
339
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700340 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 rp->e_slab = kmem_cache_create(rp->slab_name,
342 sizeof(struct mon_event_text), sizeof(long), 0,
Christoph Lameter028d2a32006-06-30 02:34:47 -0700343 mon_text_ctor, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (rp->e_slab == NULL) {
345 rc = -ENOMEM;
346 goto err_slab;
347 }
348
349 mon_reader_add(mbus, &rp->r);
350
351 file->private_data = rp;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100352 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354
355// err_busy:
356// kmem_cache_destroy(rp->e_slab);
357err_slab:
358 kfree(rp->printf_buf);
359err_alloc_pr:
360 kfree(rp);
361err_alloc:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100362 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return rc;
364}
365
366/*
367 * For simplicity, we read one record in one system call and throw out
368 * what does not fit. This means that the following does not work:
369 * dd if=/dbg/usbmon/0t bs=10
370 * Also, we do not allow seeks and do not bother advancing the offset.
371 */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800372static ssize_t mon_text_read_t(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 size_t nbytes, loff_t *ppos)
374{
375 struct mon_reader_text *rp = file->private_data;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800376 struct mon_event_text *ep;
377 struct mon_text_ptr ptr;
378
379 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
380 return PTR_ERR(ep);
381 mutex_lock(&rp->printf_lock);
382 ptr.cnt = 0;
383 ptr.pbuf = rp->printf_buf;
384 ptr.limit = rp->printf_size;
385
386 mon_text_read_head_t(rp, &ptr, ep);
387 mon_text_read_statset(rp, &ptr, ep);
388 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
389 " %d", ep->length);
390 mon_text_read_data(rp, &ptr, ep);
391
392 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
393 ptr.cnt = -EFAULT;
394 mutex_unlock(&rp->printf_lock);
395 kmem_cache_free(rp->e_slab, ep);
396 return ptr.cnt;
397}
398
399static ssize_t mon_text_read_u(struct file *file, char __user *buf,
400 size_t nbytes, loff_t *ppos)
401{
402 struct mon_reader_text *rp = file->private_data;
403 struct mon_event_text *ep;
404 struct mon_text_ptr ptr;
405
406 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
407 return PTR_ERR(ep);
408 mutex_lock(&rp->printf_lock);
409 ptr.cnt = 0;
410 ptr.pbuf = rp->printf_buf;
411 ptr.limit = rp->printf_size;
412
413 mon_text_read_head_u(rp, &ptr, ep);
414 if (ep->type == 'E') {
415 mon_text_read_statset(rp, &ptr, ep);
416 } else if (usb_pipeisoc(ep->pipe)) {
417 mon_text_read_isostat(rp, &ptr, ep);
418 mon_text_read_isodesc(rp, &ptr, ep);
419 } else if (usb_pipeint(ep->pipe)) {
420 mon_text_read_intstat(rp, &ptr, ep);
421 } else {
422 mon_text_read_statset(rp, &ptr, ep);
423 }
424 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
425 " %d", ep->length);
426 mon_text_read_data(rp, &ptr, ep);
427
428 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
429 ptr.cnt = -EFAULT;
430 mutex_unlock(&rp->printf_lock);
431 kmem_cache_free(rp->e_slab, ep);
432 return ptr.cnt;
433}
434
435static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
436 struct file *file)
437{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct mon_bus *mbus = rp->r.m_bus;
439 DECLARE_WAITQUEUE(waita, current);
440 struct mon_event_text *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 add_wait_queue(&rp->wait, &waita);
443 set_current_state(TASK_INTERRUPTIBLE);
444 while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
445 if (file->f_flags & O_NONBLOCK) {
446 set_current_state(TASK_RUNNING);
447 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800448 return ERR_PTR(-EWOULDBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450 /*
451 * We do not count nwaiters, because ->release is supposed
452 * to be called when all openers are gone only.
453 */
454 schedule();
455 if (signal_pending(current)) {
456 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800457 return ERR_PTR(-EINTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459 set_current_state(TASK_INTERRUPTIBLE);
460 }
461 set_current_state(TASK_RUNNING);
462 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800463 return ep;
464}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800466static void mon_text_read_head_t(struct mon_reader_text *rp,
467 struct mon_text_ptr *p, const struct mon_event_text *ep)
468{
469 char udir, utype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 udir = usb_pipein(ep->pipe) ? 'i' : 'o';
472 switch (usb_pipetype(ep->pipe)) {
473 case PIPE_ISOCHRONOUS: utype = 'Z'; break;
474 case PIPE_INTERRUPT: utype = 'I'; break;
475 case PIPE_CONTROL: utype = 'C'; break;
476 default: /* PIPE_BULK */ utype = 'B';
477 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800478 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700479 "%lx %u %c %c%c:%03u:%02u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 ep->id, ep->tstamp, ep->type,
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800481 utype, udir,
482 usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
483}
484
485static void mon_text_read_head_u(struct mon_reader_text *rp,
486 struct mon_text_ptr *p, const struct mon_event_text *ep)
487{
488 char udir, utype;
489
490 udir = usb_pipein(ep->pipe) ? 'i' : 'o';
491 switch (usb_pipetype(ep->pipe)) {
492 case PIPE_ISOCHRONOUS: utype = 'Z'; break;
493 case PIPE_INTERRUPT: utype = 'I'; break;
494 case PIPE_CONTROL: utype = 'C'; break;
495 default: /* PIPE_BULK */ utype = 'B';
496 }
497 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
498 "%lx %u %c %c%c:%d:%03u:%u",
499 ep->id, ep->tstamp, ep->type,
500 utype, udir,
501 ep->busnum, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
502}
503
504static void mon_text_read_statset(struct mon_reader_text *rp,
505 struct mon_text_ptr *p, const struct mon_event_text *ep)
506{
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700507
508 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800509 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700510 " s %02x %02x %04x %04x %04x",
511 ep->setup[0],
512 ep->setup[1],
513 (ep->setup[3] << 8) | ep->setup[2],
514 (ep->setup[5] << 8) | ep->setup[4],
515 (ep->setup[7] << 8) | ep->setup[6]);
516 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800517 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700518 " %c __ __ ____ ____ ____", ep->setup_flag);
519 } else { /* No setup for this kind of URB */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800520 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
521 " %d", ep->status);
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700522 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800523}
524
525static void mon_text_read_intstat(struct mon_reader_text *rp,
526 struct mon_text_ptr *p, const struct mon_event_text *ep)
527{
528 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
529 " %d:%d", ep->status, ep->interval);
530}
531
532static void mon_text_read_isostat(struct mon_reader_text *rp,
533 struct mon_text_ptr *p, const struct mon_event_text *ep)
534{
535 if (ep->type == 'S') {
536 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
537 " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
538 } else {
539 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
540 " %d:%d:%d:%d",
541 ep->status, ep->interval, ep->start_frame, ep->error_count);
542 }
543}
544
545static void mon_text_read_isodesc(struct mon_reader_text *rp,
546 struct mon_text_ptr *p, const struct mon_event_text *ep)
547{
548 int ndesc; /* Display this many */
549 int i;
550 const struct mon_iso_desc *dp;
551
552 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
553 " %d", ep->numdesc);
554 ndesc = ep->numdesc;
555 if (ndesc > ISODESC_MAX)
556 ndesc = ISODESC_MAX;
557 if (ndesc < 0)
558 ndesc = 0;
559 dp = ep->isodesc;
560 for (i = 0; i < ndesc; i++) {
561 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
562 " %d:%u:%u", dp->status, dp->offset, dp->length);
563 dp++;
564 }
565}
566
567static void mon_text_read_data(struct mon_reader_text *rp,
568 struct mon_text_ptr *p, const struct mon_event_text *ep)
569{
570 int data_len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if ((data_len = ep->length) > 0) {
573 if (ep->data_flag == 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800574 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
575 " =");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (data_len >= DATA_MAX)
577 data_len = DATA_MAX;
578 for (i = 0; i < data_len; i++) {
579 if (i % 4 == 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800580 p->cnt += snprintf(p->pbuf + p->cnt,
581 p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 " ");
583 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800584 p->cnt += snprintf(p->pbuf + p->cnt,
585 p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 "%02x", ep->data[i]);
587 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800588 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
589 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800591 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 " %c\n", ep->data_flag);
593 }
594 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800595 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599static int mon_text_release(struct inode *inode, struct file *file)
600{
601 struct mon_reader_text *rp = file->private_data;
602 struct mon_bus *mbus;
603 /* unsigned long flags; */
604 struct list_head *p;
605 struct mon_event_text *ep;
606
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100607 mutex_lock(&mon_lock);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700608 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (mbus->nreaders <= 0) {
611 printk(KERN_ERR TAG ": consistency error on close\n");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100612 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return 0;
614 }
615 mon_reader_del(mbus, &rp->r);
616
617 /*
618 * In theory, e_list is protected by mbus->lock. However,
619 * after mon_reader_del has finished, the following is the case:
620 * - we are not on reader list anymore, so new events won't be added;
621 * - whole mbus may be dropped if it was orphaned.
622 * So, we better not touch mbus.
623 */
624 /* spin_lock_irqsave(&mbus->lock, flags); */
625 while (!list_empty(&rp->e_list)) {
626 p = rp->e_list.next;
627 ep = list_entry(p, struct mon_event_text, e_link);
628 list_del(p);
629 --rp->nevents;
630 kmem_cache_free(rp->e_slab, ep);
631 }
632 /* spin_unlock_irqrestore(&mbus->lock, flags); */
633
634 kmem_cache_destroy(rp->e_slab);
635 kfree(rp->printf_buf);
636 kfree(rp);
637
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100638 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return 0;
640}
641
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800642static const struct file_operations mon_fops_text_t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 .owner = THIS_MODULE,
644 .open = mon_text_open,
645 .llseek = no_llseek,
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800646 .read = mon_text_read_t,
647 .release = mon_text_release,
648};
649
650static const struct file_operations mon_fops_text_u = {
651 .owner = THIS_MODULE,
652 .open = mon_text_open,
653 .llseek = no_llseek,
654 .read = mon_text_read_u,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 .release = mon_text_release,
656};
657
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700658int mon_text_add(struct mon_bus *mbus, int busnum)
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800659{
660 struct dentry *d;
661 enum { NAMESZ = 10 };
662 char name[NAMESZ];
663 int rc;
664
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700665 rc = snprintf(name, NAMESZ, "%dt", busnum);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800666 if (rc <= 0 || rc >= NAMESZ)
667 goto err_print_t;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800668 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_t);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800669 if (d == NULL)
670 goto err_create_t;
671 mbus->dent_t = d;
672
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700673 rc = snprintf(name, NAMESZ, "%du", busnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800674 if (rc <= 0 || rc >= NAMESZ)
675 goto err_print_u;
676 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
677 if (d == NULL)
678 goto err_create_u;
679 mbus->dent_u = d;
680
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700681 rc = snprintf(name, NAMESZ, "%ds", busnum);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800682 if (rc <= 0 || rc >= NAMESZ)
683 goto err_print_s;
684 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
685 if (d == NULL)
686 goto err_create_s;
687 mbus->dent_s = d;
688
689 return 1;
690
691err_create_s:
692err_print_s:
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800693 debugfs_remove(mbus->dent_u);
694 mbus->dent_u = NULL;
695err_create_u:
696err_print_u:
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800697 debugfs_remove(mbus->dent_t);
698 mbus->dent_t = NULL;
699err_create_t:
700err_print_t:
701 return 0;
702}
703
704void mon_text_del(struct mon_bus *mbus)
705{
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800706 debugfs_remove(mbus->dent_u);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800707 debugfs_remove(mbus->dent_t);
708 debugfs_remove(mbus->dent_s);
709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/*
712 * Slab interface: constructor.
713 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800714static void mon_text_ctor(void *mem, struct kmem_cache *slab, unsigned long sflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 /*
717 * Nothing to initialize. No, really!
718 * So, we fill it with garbage to emulate a reused object.
719 */
720 memset(mem, 0xe5, sizeof(struct mon_event_text));
721}
722
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800723int __init mon_text_init(void)
724{
725 struct dentry *mondir;
726
727 mondir = debugfs_create_dir("usbmon", NULL);
728 if (IS_ERR(mondir)) {
729 printk(KERN_NOTICE TAG ": debugfs is not available\n");
730 return -ENODEV;
731 }
732 if (mondir == NULL) {
733 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
734 return -ENODEV;
735 }
736 mon_dir = mondir;
737 return 0;
738}
739
Pete Zaitcev21641e32007-02-20 10:37:52 -0800740void mon_text_exit(void)
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800741{
742 debugfs_remove(mon_dir);
743}