blob: 9f1a9227ebe6571454ee3081d4df80ec5490f4c7 [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. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 unsigned long id; /* From pointer, most of the time */
54 unsigned int tstamp;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080055 int busnum;
Pete Zaitcev30c74312007-08-14 00:33:40 -070056 char devnum;
57 char epnum;
58 char is_in;
59 char xfertype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 int length; /* Depends on type: xfer length or act length */
61 int status;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080062 int interval;
63 int start_frame;
64 int error_count;
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070065 char setup_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 char data_flag;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080067 int numdesc; /* Full number */
68 struct mon_iso_desc isodesc[ISODESC_MAX];
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070069 unsigned char setup[SETUP_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 unsigned char data[DATA_MAX];
71};
72
73#define SLAB_NAME_SZ 30
74struct mon_reader_text {
Christoph Lametere18b8902006-12-06 20:33:20 -080075 struct kmem_cache *e_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int nevents;
77 struct list_head e_list;
78 struct mon_reader r; /* In C, parent class can be placed anywhere */
79
80 wait_queue_head_t wait;
81 int printf_size;
82 char *printf_buf;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010083 struct mutex printf_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 char slab_name[SLAB_NAME_SZ];
86};
87
Pete Zaitcev6f23ee12006-12-30 22:43:10 -080088static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
89
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070090static void mon_text_ctor(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080092struct mon_text_ptr {
93 int cnt, limit;
94 char *pbuf;
95};
96
97static struct mon_event_text *
98 mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
99static void mon_text_read_head_t(struct mon_reader_text *rp,
100 struct mon_text_ptr *p, const struct mon_event_text *ep);
101static void mon_text_read_head_u(struct mon_reader_text *rp,
102 struct mon_text_ptr *p, const struct mon_event_text *ep);
103static void mon_text_read_statset(struct mon_reader_text *rp,
104 struct mon_text_ptr *p, const struct mon_event_text *ep);
105static void mon_text_read_intstat(struct mon_reader_text *rp,
106 struct mon_text_ptr *p, const struct mon_event_text *ep);
107static void mon_text_read_isostat(struct mon_reader_text *rp,
108 struct mon_text_ptr *p, const struct mon_event_text *ep);
109static void mon_text_read_isodesc(struct mon_reader_text *rp,
110 struct mon_text_ptr *p, const struct mon_event_text *ep);
111static void mon_text_read_data(struct mon_reader_text *rp,
112 struct mon_text_ptr *p, const struct mon_event_text *ep);
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*
115 * mon_text_submit
116 * mon_text_complete
117 *
118 * May be called from an interrupt.
119 *
120 * This is called with the whole mon_bus locked, so no additional lock.
121 */
122
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700123static inline char mon_text_get_setup(struct mon_event_text *ep,
Alan Stern4d6cd482006-08-30 11:35:21 -0400124 struct urb *urb, char ev_type, struct mon_bus *mbus)
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700125{
126
Alan Stern18ea5d02007-07-30 17:10:36 -0400127 if (ep->xfertype != USB_ENDPOINT_XFER_CONTROL || ev_type != 'S')
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700128 return '-';
129
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700130 if (urb->setup_packet == NULL)
131 return 'Z'; /* '0' would be not as pretty. */
132
133 memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
134 return 0;
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
Alan Stern4d6cd482006-08-30 11:35:21 -0400138 int len, char ev_type, struct mon_bus *mbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (len <= 0)
141 return 'L';
Pete Zaitcev02568392005-08-15 16:53:57 -0700142 if (len >= DATA_MAX)
143 len = DATA_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Alan Stern18ea5d02007-07-30 17:10:36 -0400145 if (ep->is_in) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800146 if (ev_type != 'C')
Pete Zaitcevb9b09422005-12-03 21:52:10 -0800147 return '<';
148 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800149 if (ev_type != 'S')
Pete Zaitcevb9b09422005-12-03 21:52:10 -0800150 return '>';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
Pete Zaitcev02568392005-08-15 16:53:57 -0700153 if (urb->transfer_buffer == NULL)
154 return 'Z'; /* '0' would be not as pretty. */
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 memcpy(ep->data, urb->transfer_buffer, len);
157 return 0;
158}
159
160static inline unsigned int mon_get_timestamp(void)
161{
162 struct timeval tval;
163 unsigned int stamp;
164
165 do_gettimeofday(&tval);
166 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
167 stamp = stamp * 1000000 + tval.tv_usec;
168 return stamp;
169}
170
171static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
Alan Stern9347d512007-08-24 15:41:41 -0400172 char ev_type, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct mon_event_text *ep;
175 unsigned int stamp;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800176 struct usb_iso_packet_descriptor *fp;
177 struct mon_iso_desc *dp;
178 int i, ndesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 stamp = mon_get_timestamp();
181
182 if (rp->nevents >= EVENT_MAX ||
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800183 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 rp->r.m_bus->cnt_text_lost++;
185 return;
186 }
187
188 ep->type = ev_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 ep->id = (unsigned long) urb;
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700190 ep->busnum = urb->dev->bus->busnum;
Alan Stern18ea5d02007-07-30 17:10:36 -0400191 ep->devnum = urb->dev->devnum;
192 ep->epnum = usb_endpoint_num(&urb->ep->desc);
193 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
194 ep->is_in = usb_urb_dir_in(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 ep->tstamp = stamp;
196 ep->length = (ev_type == 'S') ?
197 urb->transfer_buffer_length : urb->actual_length;
198 /* Collecting status makes debugging sense for submits, too */
Alan Stern9347d512007-08-24 15:41:41 -0400199 ep->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Alan Stern18ea5d02007-07-30 17:10:36 -0400201 if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800202 ep->interval = urb->interval;
Alan Stern18ea5d02007-07-30 17:10:36 -0400203 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800204 ep->interval = urb->interval;
205 ep->start_frame = urb->start_frame;
206 ep->error_count = urb->error_count;
207 }
208 ep->numdesc = urb->number_of_packets;
Alan Stern18ea5d02007-07-30 17:10:36 -0400209 if (ep->xfertype == USB_ENDPOINT_XFER_ISOC &&
210 urb->number_of_packets > 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800211 if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
212 ndesc = ISODESC_MAX;
213 fp = urb->iso_frame_desc;
214 dp = ep->isodesc;
215 for (i = 0; i < ndesc; i++) {
216 dp->status = fp->status;
217 dp->offset = fp->offset;
218 dp->length = (ev_type == 'S') ?
219 fp->length : fp->actual_length;
220 fp++;
221 dp++;
222 }
223 }
224
Alan Stern4d6cd482006-08-30 11:35:21 -0400225 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
226 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
227 rp->r.m_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 rp->nevents++;
230 list_add_tail(&ep->e_link, &rp->e_list);
231 wake_up(&rp->wait);
232}
233
234static void mon_text_submit(void *data, struct urb *urb)
235{
236 struct mon_reader_text *rp = data;
Alan Stern9347d512007-08-24 15:41:41 -0400237 mon_text_event(rp, urb, 'S', -EINPROGRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Alan Stern9347d512007-08-24 15:41:41 -0400240static void mon_text_complete(void *data, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct mon_reader_text *rp = data;
Alan Stern9347d512007-08-24 15:41:41 -0400243 mon_text_event(rp, urb, 'C', status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700246static void mon_text_error(void *data, struct urb *urb, int error)
247{
248 struct mon_reader_text *rp = data;
249 struct mon_event_text *ep;
250
251 if (rp->nevents >= EVENT_MAX ||
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800252 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700253 rp->r.m_bus->cnt_text_lost++;
254 return;
255 }
256
257 ep->type = 'E';
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700258 ep->id = (unsigned long) urb;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800259 ep->busnum = 0;
Alan Stern18ea5d02007-07-30 17:10:36 -0400260 ep->devnum = urb->dev->devnum;
261 ep->epnum = usb_endpoint_num(&urb->ep->desc);
262 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
263 ep->is_in = usb_urb_dir_in(urb);
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700264 ep->tstamp = 0;
265 ep->length = 0;
266 ep->status = error;
267
268 ep->setup_flag = '-';
269 ep->data_flag = 'E';
270
271 rp->nevents++;
272 list_add_tail(&ep->e_link, &rp->e_list);
273 wake_up(&rp->wait);
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*
277 * Fetch next event from the circular buffer.
278 */
279static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
280 struct mon_bus *mbus)
281{
282 struct list_head *p;
283 unsigned long flags;
284
285 spin_lock_irqsave(&mbus->lock, flags);
286 if (list_empty(&rp->e_list)) {
287 spin_unlock_irqrestore(&mbus->lock, flags);
288 return NULL;
289 }
290 p = rp->e_list.next;
291 list_del(p);
292 --rp->nevents;
293 spin_unlock_irqrestore(&mbus->lock, flags);
294 return list_entry(p, struct mon_event_text, e_link);
295}
296
297/*
298 */
299static int mon_text_open(struct inode *inode, struct file *file)
300{
301 struct mon_bus *mbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct mon_reader_text *rp;
303 int rc;
304
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100305 mutex_lock(&mon_lock);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700306 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100308 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (rp == NULL) {
310 rc = -ENOMEM;
311 goto err_alloc;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 INIT_LIST_HEAD(&rp->e_list);
314 init_waitqueue_head(&rp->wait);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100315 mutex_init(&rp->printf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 rp->printf_size = PRINTF_DFL;
318 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
319 if (rp->printf_buf == NULL) {
320 rc = -ENOMEM;
321 goto err_alloc_pr;
322 }
323
324 rp->r.m_bus = mbus;
325 rp->r.r_data = rp;
326 rp->r.rnf_submit = mon_text_submit;
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700327 rp->r.rnf_error = mon_text_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 rp->r.rnf_complete = mon_text_complete;
329
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700330 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 rp->e_slab = kmem_cache_create(rp->slab_name,
332 sizeof(struct mon_event_text), sizeof(long), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900333 mon_text_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (rp->e_slab == NULL) {
335 rc = -ENOMEM;
336 goto err_slab;
337 }
338
339 mon_reader_add(mbus, &rp->r);
340
341 file->private_data = rp;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100342 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344
345// err_busy:
346// kmem_cache_destroy(rp->e_slab);
347err_slab:
348 kfree(rp->printf_buf);
349err_alloc_pr:
350 kfree(rp);
351err_alloc:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100352 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return rc;
354}
355
356/*
357 * For simplicity, we read one record in one system call and throw out
358 * what does not fit. This means that the following does not work:
359 * dd if=/dbg/usbmon/0t bs=10
360 * Also, we do not allow seeks and do not bother advancing the offset.
361 */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800362static ssize_t mon_text_read_t(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 size_t nbytes, loff_t *ppos)
364{
365 struct mon_reader_text *rp = file->private_data;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800366 struct mon_event_text *ep;
367 struct mon_text_ptr ptr;
368
369 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
370 return PTR_ERR(ep);
371 mutex_lock(&rp->printf_lock);
372 ptr.cnt = 0;
373 ptr.pbuf = rp->printf_buf;
374 ptr.limit = rp->printf_size;
375
376 mon_text_read_head_t(rp, &ptr, ep);
377 mon_text_read_statset(rp, &ptr, ep);
378 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
379 " %d", ep->length);
380 mon_text_read_data(rp, &ptr, ep);
381
382 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
383 ptr.cnt = -EFAULT;
384 mutex_unlock(&rp->printf_lock);
385 kmem_cache_free(rp->e_slab, ep);
386 return ptr.cnt;
387}
388
389static ssize_t mon_text_read_u(struct file *file, char __user *buf,
390 size_t nbytes, loff_t *ppos)
391{
392 struct mon_reader_text *rp = file->private_data;
393 struct mon_event_text *ep;
394 struct mon_text_ptr ptr;
395
396 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
397 return PTR_ERR(ep);
398 mutex_lock(&rp->printf_lock);
399 ptr.cnt = 0;
400 ptr.pbuf = rp->printf_buf;
401 ptr.limit = rp->printf_size;
402
403 mon_text_read_head_u(rp, &ptr, ep);
404 if (ep->type == 'E') {
405 mon_text_read_statset(rp, &ptr, ep);
Alan Stern18ea5d02007-07-30 17:10:36 -0400406 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800407 mon_text_read_isostat(rp, &ptr, ep);
408 mon_text_read_isodesc(rp, &ptr, ep);
Alan Stern18ea5d02007-07-30 17:10:36 -0400409 } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800410 mon_text_read_intstat(rp, &ptr, ep);
411 } else {
412 mon_text_read_statset(rp, &ptr, ep);
413 }
414 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
415 " %d", ep->length);
416 mon_text_read_data(rp, &ptr, ep);
417
418 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
419 ptr.cnt = -EFAULT;
420 mutex_unlock(&rp->printf_lock);
421 kmem_cache_free(rp->e_slab, ep);
422 return ptr.cnt;
423}
424
425static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
426 struct file *file)
427{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct mon_bus *mbus = rp->r.m_bus;
429 DECLARE_WAITQUEUE(waita, current);
430 struct mon_event_text *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 add_wait_queue(&rp->wait, &waita);
433 set_current_state(TASK_INTERRUPTIBLE);
434 while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
435 if (file->f_flags & O_NONBLOCK) {
436 set_current_state(TASK_RUNNING);
437 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800438 return ERR_PTR(-EWOULDBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 /*
441 * We do not count nwaiters, because ->release is supposed
442 * to be called when all openers are gone only.
443 */
444 schedule();
445 if (signal_pending(current)) {
446 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800447 return ERR_PTR(-EINTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 set_current_state(TASK_INTERRUPTIBLE);
450 }
451 set_current_state(TASK_RUNNING);
452 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800453 return ep;
454}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800456static void mon_text_read_head_t(struct mon_reader_text *rp,
457 struct mon_text_ptr *p, const struct mon_event_text *ep)
458{
459 char udir, utype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Alan Stern18ea5d02007-07-30 17:10:36 -0400461 udir = (ep->is_in ? 'i' : 'o');
462 switch (ep->xfertype) {
463 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
464 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
465 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 default: /* PIPE_BULK */ utype = 'B';
467 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800468 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700469 "%lx %u %c %c%c:%03u:%02u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 ep->id, ep->tstamp, ep->type,
Alan Stern18ea5d02007-07-30 17:10:36 -0400471 utype, udir, ep->devnum, ep->epnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800472}
473
474static void mon_text_read_head_u(struct mon_reader_text *rp,
475 struct mon_text_ptr *p, const struct mon_event_text *ep)
476{
477 char udir, utype;
478
Alan Stern18ea5d02007-07-30 17:10:36 -0400479 udir = (ep->is_in ? 'i' : 'o');
480 switch (ep->xfertype) {
481 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
482 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
483 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800484 default: /* PIPE_BULK */ utype = 'B';
485 }
486 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
487 "%lx %u %c %c%c:%d:%03u:%u",
488 ep->id, ep->tstamp, ep->type,
Alan Stern18ea5d02007-07-30 17:10:36 -0400489 utype, udir, ep->busnum, ep->devnum, ep->epnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800490}
491
492static void mon_text_read_statset(struct mon_reader_text *rp,
493 struct mon_text_ptr *p, const struct mon_event_text *ep)
494{
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700495
496 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800497 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700498 " s %02x %02x %04x %04x %04x",
499 ep->setup[0],
500 ep->setup[1],
501 (ep->setup[3] << 8) | ep->setup[2],
502 (ep->setup[5] << 8) | ep->setup[4],
503 (ep->setup[7] << 8) | ep->setup[6]);
504 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800505 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700506 " %c __ __ ____ ____ ____", ep->setup_flag);
507 } else { /* No setup for this kind of URB */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800508 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
509 " %d", ep->status);
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700510 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800511}
512
513static void mon_text_read_intstat(struct mon_reader_text *rp,
514 struct mon_text_ptr *p, const struct mon_event_text *ep)
515{
516 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
517 " %d:%d", ep->status, ep->interval);
518}
519
520static void mon_text_read_isostat(struct mon_reader_text *rp,
521 struct mon_text_ptr *p, const struct mon_event_text *ep)
522{
523 if (ep->type == 'S') {
524 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
525 " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
526 } else {
527 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
528 " %d:%d:%d:%d",
529 ep->status, ep->interval, ep->start_frame, ep->error_count);
530 }
531}
532
533static void mon_text_read_isodesc(struct mon_reader_text *rp,
534 struct mon_text_ptr *p, const struct mon_event_text *ep)
535{
536 int ndesc; /* Display this many */
537 int i;
538 const struct mon_iso_desc *dp;
539
540 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
541 " %d", ep->numdesc);
542 ndesc = ep->numdesc;
543 if (ndesc > ISODESC_MAX)
544 ndesc = ISODESC_MAX;
545 if (ndesc < 0)
546 ndesc = 0;
547 dp = ep->isodesc;
548 for (i = 0; i < ndesc; i++) {
549 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
550 " %d:%u:%u", dp->status, dp->offset, dp->length);
551 dp++;
552 }
553}
554
555static void mon_text_read_data(struct mon_reader_text *rp,
556 struct mon_text_ptr *p, const struct mon_event_text *ep)
557{
558 int data_len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if ((data_len = ep->length) > 0) {
561 if (ep->data_flag == 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800562 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
563 " =");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (data_len >= DATA_MAX)
565 data_len = DATA_MAX;
566 for (i = 0; i < data_len; i++) {
567 if (i % 4 == 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800568 p->cnt += snprintf(p->pbuf + p->cnt,
569 p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 " ");
571 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800572 p->cnt += snprintf(p->pbuf + p->cnt,
573 p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 "%02x", ep->data[i]);
575 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800576 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
577 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800579 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 " %c\n", ep->data_flag);
581 }
582 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800583 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587static int mon_text_release(struct inode *inode, struct file *file)
588{
589 struct mon_reader_text *rp = file->private_data;
590 struct mon_bus *mbus;
591 /* unsigned long flags; */
592 struct list_head *p;
593 struct mon_event_text *ep;
594
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100595 mutex_lock(&mon_lock);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700596 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (mbus->nreaders <= 0) {
599 printk(KERN_ERR TAG ": consistency error on close\n");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100600 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602 }
603 mon_reader_del(mbus, &rp->r);
604
605 /*
606 * In theory, e_list is protected by mbus->lock. However,
607 * after mon_reader_del has finished, the following is the case:
608 * - we are not on reader list anymore, so new events won't be added;
609 * - whole mbus may be dropped if it was orphaned.
610 * So, we better not touch mbus.
611 */
612 /* spin_lock_irqsave(&mbus->lock, flags); */
613 while (!list_empty(&rp->e_list)) {
614 p = rp->e_list.next;
615 ep = list_entry(p, struct mon_event_text, e_link);
616 list_del(p);
617 --rp->nevents;
618 kmem_cache_free(rp->e_slab, ep);
619 }
620 /* spin_unlock_irqrestore(&mbus->lock, flags); */
621
622 kmem_cache_destroy(rp->e_slab);
623 kfree(rp->printf_buf);
624 kfree(rp);
625
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100626 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return 0;
628}
629
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800630static const struct file_operations mon_fops_text_t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 .owner = THIS_MODULE,
632 .open = mon_text_open,
633 .llseek = no_llseek,
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800634 .read = mon_text_read_t,
635 .release = mon_text_release,
636};
637
638static const struct file_operations mon_fops_text_u = {
639 .owner = THIS_MODULE,
640 .open = mon_text_open,
641 .llseek = no_llseek,
642 .read = mon_text_read_u,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 .release = mon_text_release,
644};
645
Pete Zaitcevce7cd132007-05-03 16:51:16 -0700646int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800647{
648 struct dentry *d;
649 enum { NAMESZ = 10 };
650 char name[NAMESZ];
Pete Zaitcevce7cd132007-05-03 16:51:16 -0700651 int busnum = ubus? ubus->busnum: 0;
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800652 int rc;
653
Pete Zaitcevce7cd132007-05-03 16:51:16 -0700654 if (ubus != NULL) {
655 rc = snprintf(name, NAMESZ, "%dt", busnum);
656 if (rc <= 0 || rc >= NAMESZ)
657 goto err_print_t;
658 d = debugfs_create_file(name, 0600, mon_dir, mbus,
659 &mon_fops_text_t);
660 if (d == NULL)
661 goto err_create_t;
662 mbus->dent_t = d;
663 }
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800664
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700665 rc = snprintf(name, NAMESZ, "%du", busnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800666 if (rc <= 0 || rc >= NAMESZ)
667 goto err_print_u;
668 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
669 if (d == NULL)
670 goto err_create_u;
671 mbus->dent_u = d;
672
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700673 rc = snprintf(name, NAMESZ, "%ds", busnum);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800674 if (rc <= 0 || rc >= NAMESZ)
675 goto err_print_s;
676 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
677 if (d == NULL)
678 goto err_create_s;
679 mbus->dent_s = d;
680
681 return 1;
682
683err_create_s:
684err_print_s:
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800685 debugfs_remove(mbus->dent_u);
686 mbus->dent_u = NULL;
687err_create_u:
688err_print_u:
Pete Zaitcevce7cd132007-05-03 16:51:16 -0700689 if (ubus != NULL) {
690 debugfs_remove(mbus->dent_t);
691 mbus->dent_t = NULL;
692 }
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800693err_create_t:
694err_print_t:
695 return 0;
696}
697
698void mon_text_del(struct mon_bus *mbus)
699{
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800700 debugfs_remove(mbus->dent_u);
Pete Zaitcevce7cd132007-05-03 16:51:16 -0700701 if (mbus->dent_t != NULL)
702 debugfs_remove(mbus->dent_t);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800703 debugfs_remove(mbus->dent_s);
704}
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706/*
707 * Slab interface: constructor.
708 */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700709static void mon_text_ctor(void *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 /*
712 * Nothing to initialize. No, really!
713 * So, we fill it with garbage to emulate a reused object.
714 */
715 memset(mem, 0xe5, sizeof(struct mon_event_text));
716}
717
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800718int __init mon_text_init(void)
719{
720 struct dentry *mondir;
721
Greg Kroah-Hartmanf49ce962009-04-24 15:15:49 -0700722 mondir = debugfs_create_dir("usbmon", usb_debug_root);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800723 if (IS_ERR(mondir)) {
724 printk(KERN_NOTICE TAG ": debugfs is not available\n");
725 return -ENODEV;
726 }
727 if (mondir == NULL) {
728 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
729 return -ENODEV;
730 }
731 mon_dir = mondir;
732 return 0;
733}
734
Pete Zaitcev21641e32007-02-20 10:37:52 -0800735void mon_text_exit(void)
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800736{
737 debugfs_remove(mon_dir);
738}