blob: 047568ff223db60743435effeaaf522b995d3e20 [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>
Alan Sternb375e112009-11-06 12:32:23 -050013#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/uaccess.h>
15
16#include "usb_mon.h"
17
18/*
19 * No, we do not want arbitrarily long data strings.
20 * Use the binary interface if you want to capture bulk data!
21 */
22#define DATA_MAX 32
23
24/*
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070025 * Defined by USB 2.0 clause 9.3, table 9.2.
26 */
27#define SETUP_MAX 8
28
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * This limit exists to prevent OOMs when the user process stops reading.
Pete Zaitcev5b1c6742006-06-09 20:10:10 -070031 * If usbmon were available to unprivileged processes, it might be open
32 * to a local DoS. But we have to keep to root in order to prevent
33 * password sniffing from HID devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080035#define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080037/*
38 * Potentially unlimited number; we limit it for similar allocations.
39 * The usbfs limits this to 128, but we're not quite as generous.
40 */
41#define ISODESC_MAX 5
42
43#define PRINTF_DFL 250 /* with 5 ISOs segs */
44
45struct mon_iso_desc {
46 int status;
47 unsigned int offset;
48 unsigned int length; /* Unsigned here, signed in URB. Historic. */
49};
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51struct mon_event_text {
52 struct list_head e_link;
53 int type; /* submit, complete, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 unsigned long id; /* From pointer, most of the time */
55 unsigned int tstamp;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080056 int busnum;
Pete Zaitcev30c74312007-08-14 00:33:40 -070057 char devnum;
58 char epnum;
59 char is_in;
60 char xfertype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int length; /* Depends on type: xfer length or act length */
62 int status;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080063 int interval;
64 int start_frame;
65 int error_count;
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070066 char setup_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 char data_flag;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080068 int numdesc; /* Full number */
69 struct mon_iso_desc isodesc[ISODESC_MAX];
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070070 unsigned char setup[SETUP_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 unsigned char data[DATA_MAX];
72};
73
74#define SLAB_NAME_SZ 30
75struct mon_reader_text {
Christoph Lametere18b8902006-12-06 20:33:20 -080076 struct kmem_cache *e_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 int nevents;
78 struct list_head e_list;
79 struct mon_reader r; /* In C, parent class can be placed anywhere */
80
81 wait_queue_head_t wait;
82 int printf_size;
83 char *printf_buf;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010084 struct mutex printf_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 char slab_name[SLAB_NAME_SZ];
87};
88
Pete Zaitcev6f23ee12006-12-30 22:43:10 -080089static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
90
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070091static void mon_text_ctor(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080093struct mon_text_ptr {
94 int cnt, limit;
95 char *pbuf;
96};
97
98static struct mon_event_text *
99 mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
100static void mon_text_read_head_t(struct mon_reader_text *rp,
101 struct mon_text_ptr *p, const struct mon_event_text *ep);
102static void mon_text_read_head_u(struct mon_reader_text *rp,
103 struct mon_text_ptr *p, const struct mon_event_text *ep);
104static void mon_text_read_statset(struct mon_reader_text *rp,
105 struct mon_text_ptr *p, const struct mon_event_text *ep);
106static void mon_text_read_intstat(struct mon_reader_text *rp,
107 struct mon_text_ptr *p, const struct mon_event_text *ep);
108static void mon_text_read_isostat(struct mon_reader_text *rp,
109 struct mon_text_ptr *p, const struct mon_event_text *ep);
110static void mon_text_read_isodesc(struct mon_reader_text *rp,
111 struct mon_text_ptr *p, const struct mon_event_text *ep);
112static void mon_text_read_data(struct mon_reader_text *rp,
113 struct mon_text_ptr *p, const struct mon_event_text *ep);
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/*
116 * mon_text_submit
117 * mon_text_complete
118 *
119 * May be called from an interrupt.
120 *
121 * This is called with the whole mon_bus locked, so no additional lock.
122 */
123
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700124static inline char mon_text_get_setup(struct mon_event_text *ep,
Alan Stern4d6cd482006-08-30 11:35:21 -0400125 struct urb *urb, char ev_type, struct mon_bus *mbus)
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700126{
127
Alan Stern18ea5d02007-07-30 17:10:36 -0400128 if (ep->xfertype != USB_ENDPOINT_XFER_CONTROL || ev_type != 'S')
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700129 return '-';
130
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{
Alan Sternb375e112009-11-06 12:32:23 -0500141 void *src;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 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
Alan Stern18ea5d02007-07-30 17:10:36 -0400148 if (ep->is_in) {
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
Alan Sternb375e112009-11-06 12:32:23 -0500156 if (urb->num_sgs == 0) {
157 src = urb->transfer_buffer;
158 if (src == NULL)
159 return 'Z'; /* '0' would be not as pretty. */
160 } else {
161 struct scatterlist *sg = urb->sg->sg;
Pete Zaitcev02568392005-08-15 16:53:57 -0700162
Alan Sternb375e112009-11-06 12:32:23 -0500163 /* If IOMMU coalescing occurred, we cannot trust sg_page */
164 if (urb->sg->nents != urb->num_sgs ||
165 PageHighMem(sg_page(sg)))
166 return 'D';
167
168 /* For the text interface we copy only the first sg buffer */
169 len = min_t(int, sg->length, len);
170 src = sg_virt(sg);
171 }
172
173 memcpy(ep->data, src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 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,
Alan Stern9347d512007-08-24 15:41:41 -0400189 char ev_type, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 ep->id = (unsigned long) urb;
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700207 ep->busnum = urb->dev->bus->busnum;
Alan Stern18ea5d02007-07-30 17:10:36 -0400208 ep->devnum = urb->dev->devnum;
209 ep->epnum = usb_endpoint_num(&urb->ep->desc);
210 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
211 ep->is_in = usb_urb_dir_in(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 ep->tstamp = stamp;
213 ep->length = (ev_type == 'S') ?
214 urb->transfer_buffer_length : urb->actual_length;
215 /* Collecting status makes debugging sense for submits, too */
Alan Stern9347d512007-08-24 15:41:41 -0400216 ep->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Alan Stern18ea5d02007-07-30 17:10:36 -0400218 if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800219 ep->interval = urb->interval;
Alan Stern18ea5d02007-07-30 17:10:36 -0400220 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800221 ep->interval = urb->interval;
222 ep->start_frame = urb->start_frame;
223 ep->error_count = urb->error_count;
224 }
225 ep->numdesc = urb->number_of_packets;
Alan Stern18ea5d02007-07-30 17:10:36 -0400226 if (ep->xfertype == USB_ENDPOINT_XFER_ISOC &&
227 urb->number_of_packets > 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800228 if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
229 ndesc = ISODESC_MAX;
230 fp = urb->iso_frame_desc;
231 dp = ep->isodesc;
232 for (i = 0; i < ndesc; i++) {
233 dp->status = fp->status;
234 dp->offset = fp->offset;
235 dp->length = (ev_type == 'S') ?
236 fp->length : fp->actual_length;
237 fp++;
238 dp++;
239 }
240 }
241
Alan Stern4d6cd482006-08-30 11:35:21 -0400242 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
243 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
244 rp->r.m_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 rp->nevents++;
247 list_add_tail(&ep->e_link, &rp->e_list);
248 wake_up(&rp->wait);
249}
250
251static void mon_text_submit(void *data, struct urb *urb)
252{
253 struct mon_reader_text *rp = data;
Alan Stern9347d512007-08-24 15:41:41 -0400254 mon_text_event(rp, urb, 'S', -EINPROGRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Alan Stern9347d512007-08-24 15:41:41 -0400257static void mon_text_complete(void *data, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct mon_reader_text *rp = data;
Alan Stern9347d512007-08-24 15:41:41 -0400260 mon_text_event(rp, urb, 'C', status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700263static void mon_text_error(void *data, struct urb *urb, int error)
264{
265 struct mon_reader_text *rp = data;
266 struct mon_event_text *ep;
267
268 if (rp->nevents >= EVENT_MAX ||
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800269 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700270 rp->r.m_bus->cnt_text_lost++;
271 return;
272 }
273
274 ep->type = 'E';
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700275 ep->id = (unsigned long) urb;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800276 ep->busnum = 0;
Alan Stern18ea5d02007-07-30 17:10:36 -0400277 ep->devnum = urb->dev->devnum;
278 ep->epnum = usb_endpoint_num(&urb->ep->desc);
279 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
280 ep->is_in = usb_urb_dir_in(urb);
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700281 ep->tstamp = 0;
282 ep->length = 0;
283 ep->status = error;
284
285 ep->setup_flag = '-';
286 ep->data_flag = 'E';
287
288 rp->nevents++;
289 list_add_tail(&ep->e_link, &rp->e_list);
290 wake_up(&rp->wait);
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/*
294 * Fetch next event from the circular buffer.
295 */
296static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
297 struct mon_bus *mbus)
298{
299 struct list_head *p;
300 unsigned long flags;
301
302 spin_lock_irqsave(&mbus->lock, flags);
303 if (list_empty(&rp->e_list)) {
304 spin_unlock_irqrestore(&mbus->lock, flags);
305 return NULL;
306 }
307 p = rp->e_list.next;
308 list_del(p);
309 --rp->nevents;
310 spin_unlock_irqrestore(&mbus->lock, flags);
311 return list_entry(p, struct mon_event_text, e_link);
312}
313
314/*
315 */
316static int mon_text_open(struct inode *inode, struct file *file)
317{
318 struct mon_bus *mbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct mon_reader_text *rp;
320 int rc;
321
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100322 mutex_lock(&mon_lock);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700323 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100325 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (rp == NULL) {
327 rc = -ENOMEM;
328 goto err_alloc;
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 INIT_LIST_HEAD(&rp->e_list);
331 init_waitqueue_head(&rp->wait);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100332 mutex_init(&rp->printf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 rp->printf_size = PRINTF_DFL;
335 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
336 if (rp->printf_buf == NULL) {
337 rc = -ENOMEM;
338 goto err_alloc_pr;
339 }
340
341 rp->r.m_bus = mbus;
342 rp->r.r_data = rp;
343 rp->r.rnf_submit = mon_text_submit;
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700344 rp->r.rnf_error = mon_text_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 rp->r.rnf_complete = mon_text_complete;
346
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700347 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 rp->e_slab = kmem_cache_create(rp->slab_name,
349 sizeof(struct mon_event_text), sizeof(long), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900350 mon_text_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (rp->e_slab == NULL) {
352 rc = -ENOMEM;
353 goto err_slab;
354 }
355
356 mon_reader_add(mbus, &rp->r);
357
358 file->private_data = rp;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100359 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361
362// err_busy:
363// kmem_cache_destroy(rp->e_slab);
364err_slab:
365 kfree(rp->printf_buf);
366err_alloc_pr:
367 kfree(rp);
368err_alloc:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100369 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return rc;
371}
372
373/*
374 * For simplicity, we read one record in one system call and throw out
375 * what does not fit. This means that the following does not work:
376 * dd if=/dbg/usbmon/0t bs=10
377 * Also, we do not allow seeks and do not bother advancing the offset.
378 */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800379static ssize_t mon_text_read_t(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 size_t nbytes, loff_t *ppos)
381{
382 struct mon_reader_text *rp = file->private_data;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800383 struct mon_event_text *ep;
384 struct mon_text_ptr ptr;
385
386 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
387 return PTR_ERR(ep);
388 mutex_lock(&rp->printf_lock);
389 ptr.cnt = 0;
390 ptr.pbuf = rp->printf_buf;
391 ptr.limit = rp->printf_size;
392
393 mon_text_read_head_t(rp, &ptr, ep);
394 mon_text_read_statset(rp, &ptr, ep);
395 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
396 " %d", ep->length);
397 mon_text_read_data(rp, &ptr, ep);
398
399 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
400 ptr.cnt = -EFAULT;
401 mutex_unlock(&rp->printf_lock);
402 kmem_cache_free(rp->e_slab, ep);
403 return ptr.cnt;
404}
405
406static ssize_t mon_text_read_u(struct file *file, char __user *buf,
407 size_t nbytes, loff_t *ppos)
408{
409 struct mon_reader_text *rp = file->private_data;
410 struct mon_event_text *ep;
411 struct mon_text_ptr ptr;
412
413 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
414 return PTR_ERR(ep);
415 mutex_lock(&rp->printf_lock);
416 ptr.cnt = 0;
417 ptr.pbuf = rp->printf_buf;
418 ptr.limit = rp->printf_size;
419
420 mon_text_read_head_u(rp, &ptr, ep);
421 if (ep->type == 'E') {
422 mon_text_read_statset(rp, &ptr, ep);
Alan Stern18ea5d02007-07-30 17:10:36 -0400423 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800424 mon_text_read_isostat(rp, &ptr, ep);
425 mon_text_read_isodesc(rp, &ptr, ep);
Alan Stern18ea5d02007-07-30 17:10:36 -0400426 } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800427 mon_text_read_intstat(rp, &ptr, ep);
428 } else {
429 mon_text_read_statset(rp, &ptr, ep);
430 }
431 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
432 " %d", ep->length);
433 mon_text_read_data(rp, &ptr, ep);
434
435 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
436 ptr.cnt = -EFAULT;
437 mutex_unlock(&rp->printf_lock);
438 kmem_cache_free(rp->e_slab, ep);
439 return ptr.cnt;
440}
441
442static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
443 struct file *file)
444{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct mon_bus *mbus = rp->r.m_bus;
446 DECLARE_WAITQUEUE(waita, current);
447 struct mon_event_text *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 add_wait_queue(&rp->wait, &waita);
450 set_current_state(TASK_INTERRUPTIBLE);
451 while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
452 if (file->f_flags & O_NONBLOCK) {
453 set_current_state(TASK_RUNNING);
454 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800455 return ERR_PTR(-EWOULDBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 /*
458 * We do not count nwaiters, because ->release is supposed
459 * to be called when all openers are gone only.
460 */
461 schedule();
462 if (signal_pending(current)) {
463 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800464 return ERR_PTR(-EINTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466 set_current_state(TASK_INTERRUPTIBLE);
467 }
468 set_current_state(TASK_RUNNING);
469 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800470 return ep;
471}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800473static void mon_text_read_head_t(struct mon_reader_text *rp,
474 struct mon_text_ptr *p, const struct mon_event_text *ep)
475{
476 char udir, utype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Alan Stern18ea5d02007-07-30 17:10:36 -0400478 udir = (ep->is_in ? 'i' : 'o');
479 switch (ep->xfertype) {
480 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
481 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
482 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 default: /* PIPE_BULK */ utype = 'B';
484 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800485 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700486 "%lx %u %c %c%c:%03u:%02u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 ep->id, ep->tstamp, ep->type,
Alan Stern18ea5d02007-07-30 17:10:36 -0400488 utype, udir, ep->devnum, ep->epnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800489}
490
491static void mon_text_read_head_u(struct mon_reader_text *rp,
492 struct mon_text_ptr *p, const struct mon_event_text *ep)
493{
494 char udir, utype;
495
Alan Stern18ea5d02007-07-30 17:10:36 -0400496 udir = (ep->is_in ? 'i' : 'o');
497 switch (ep->xfertype) {
498 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
499 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
500 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800501 default: /* PIPE_BULK */ utype = 'B';
502 }
503 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
504 "%lx %u %c %c%c:%d:%03u:%u",
505 ep->id, ep->tstamp, ep->type,
Alan Stern18ea5d02007-07-30 17:10:36 -0400506 utype, udir, ep->busnum, ep->devnum, ep->epnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800507}
508
509static void mon_text_read_statset(struct mon_reader_text *rp,
510 struct mon_text_ptr *p, const struct mon_event_text *ep)
511{
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700512
513 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800514 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700515 " s %02x %02x %04x %04x %04x",
516 ep->setup[0],
517 ep->setup[1],
518 (ep->setup[3] << 8) | ep->setup[2],
519 (ep->setup[5] << 8) | ep->setup[4],
520 (ep->setup[7] << 8) | ep->setup[6]);
521 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800522 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700523 " %c __ __ ____ ____ ____", ep->setup_flag);
524 } else { /* No setup for this kind of URB */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800525 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
526 " %d", ep->status);
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700527 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800528}
529
530static void mon_text_read_intstat(struct mon_reader_text *rp,
531 struct mon_text_ptr *p, const struct mon_event_text *ep)
532{
533 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
534 " %d:%d", ep->status, ep->interval);
535}
536
537static void mon_text_read_isostat(struct mon_reader_text *rp,
538 struct mon_text_ptr *p, const struct mon_event_text *ep)
539{
540 if (ep->type == 'S') {
541 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
542 " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
543 } else {
544 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
545 " %d:%d:%d:%d",
546 ep->status, ep->interval, ep->start_frame, ep->error_count);
547 }
548}
549
550static void mon_text_read_isodesc(struct mon_reader_text *rp,
551 struct mon_text_ptr *p, const struct mon_event_text *ep)
552{
553 int ndesc; /* Display this many */
554 int i;
555 const struct mon_iso_desc *dp;
556
557 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
558 " %d", ep->numdesc);
559 ndesc = ep->numdesc;
560 if (ndesc > ISODESC_MAX)
561 ndesc = ISODESC_MAX;
562 if (ndesc < 0)
563 ndesc = 0;
564 dp = ep->isodesc;
565 for (i = 0; i < ndesc; i++) {
566 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
567 " %d:%u:%u", dp->status, dp->offset, dp->length);
568 dp++;
569 }
570}
571
572static void mon_text_read_data(struct mon_reader_text *rp,
573 struct mon_text_ptr *p, const struct mon_event_text *ep)
574{
575 int data_len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 if ((data_len = ep->length) > 0) {
578 if (ep->data_flag == 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800579 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
580 " =");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (data_len >= DATA_MAX)
582 data_len = DATA_MAX;
583 for (i = 0; i < data_len; i++) {
584 if (i % 4 == 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800585 p->cnt += snprintf(p->pbuf + p->cnt,
586 p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 " ");
588 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800589 p->cnt += snprintf(p->pbuf + p->cnt,
590 p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 "%02x", ep->data[i]);
592 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800593 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
594 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800596 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 " %c\n", ep->data_flag);
598 }
599 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800600 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604static int mon_text_release(struct inode *inode, struct file *file)
605{
606 struct mon_reader_text *rp = file->private_data;
607 struct mon_bus *mbus;
608 /* unsigned long flags; */
609 struct list_head *p;
610 struct mon_event_text *ep;
611
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100612 mutex_lock(&mon_lock);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700613 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (mbus->nreaders <= 0) {
616 printk(KERN_ERR TAG ": consistency error on close\n");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100617 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 0;
619 }
620 mon_reader_del(mbus, &rp->r);
621
622 /*
623 * In theory, e_list is protected by mbus->lock. However,
624 * after mon_reader_del has finished, the following is the case:
625 * - we are not on reader list anymore, so new events won't be added;
626 * - whole mbus may be dropped if it was orphaned.
627 * So, we better not touch mbus.
628 */
629 /* spin_lock_irqsave(&mbus->lock, flags); */
630 while (!list_empty(&rp->e_list)) {
631 p = rp->e_list.next;
632 ep = list_entry(p, struct mon_event_text, e_link);
633 list_del(p);
634 --rp->nevents;
635 kmem_cache_free(rp->e_slab, ep);
636 }
637 /* spin_unlock_irqrestore(&mbus->lock, flags); */
638
639 kmem_cache_destroy(rp->e_slab);
640 kfree(rp->printf_buf);
641 kfree(rp);
642
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100643 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return 0;
645}
646
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800647static const struct file_operations mon_fops_text_t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 .owner = THIS_MODULE,
649 .open = mon_text_open,
650 .llseek = no_llseek,
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800651 .read = mon_text_read_t,
652 .release = mon_text_release,
653};
654
655static const struct file_operations mon_fops_text_u = {
656 .owner = THIS_MODULE,
657 .open = mon_text_open,
658 .llseek = no_llseek,
659 .read = mon_text_read_u,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 .release = mon_text_release,
661};
662
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700663int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800664{
665 struct dentry *d;
666 enum { NAMESZ = 10 };
667 char name[NAMESZ];
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700668 int busnum = ubus? ubus->busnum: 0;
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800669 int rc;
670
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700671 if (ubus != NULL) {
672 rc = snprintf(name, NAMESZ, "%dt", busnum);
673 if (rc <= 0 || rc >= NAMESZ)
674 goto err_print_t;
675 d = debugfs_create_file(name, 0600, mon_dir, mbus,
676 &mon_fops_text_t);
677 if (d == NULL)
678 goto err_create_t;
679 mbus->dent_t = d;
680 }
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800681
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700682 rc = snprintf(name, NAMESZ, "%du", busnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800683 if (rc <= 0 || rc >= NAMESZ)
684 goto err_print_u;
685 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
686 if (d == NULL)
687 goto err_create_u;
688 mbus->dent_u = d;
689
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700690 rc = snprintf(name, NAMESZ, "%ds", busnum);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800691 if (rc <= 0 || rc >= NAMESZ)
692 goto err_print_s;
693 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
694 if (d == NULL)
695 goto err_create_s;
696 mbus->dent_s = d;
697
698 return 1;
699
700err_create_s:
701err_print_s:
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800702 debugfs_remove(mbus->dent_u);
703 mbus->dent_u = NULL;
704err_create_u:
705err_print_u:
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700706 if (ubus != NULL) {
707 debugfs_remove(mbus->dent_t);
708 mbus->dent_t = NULL;
709 }
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800710err_create_t:
711err_print_t:
712 return 0;
713}
714
715void mon_text_del(struct mon_bus *mbus)
716{
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800717 debugfs_remove(mbus->dent_u);
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700718 if (mbus->dent_t != NULL)
719 debugfs_remove(mbus->dent_t);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800720 debugfs_remove(mbus->dent_s);
721}
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723/*
724 * Slab interface: constructor.
725 */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700726static void mon_text_ctor(void *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 /*
729 * Nothing to initialize. No, really!
730 * So, we fill it with garbage to emulate a reused object.
731 */
732 memset(mem, 0xe5, sizeof(struct mon_event_text));
733}
734
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800735int __init mon_text_init(void)
736{
737 struct dentry *mondir;
738
Greg Kroah-Hartmanf49ce962009-04-24 15:15:49 -0700739 mondir = debugfs_create_dir("usbmon", usb_debug_root);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800740 if (IS_ERR(mondir)) {
741 printk(KERN_NOTICE TAG ": debugfs is not available\n");
742 return -ENODEV;
743 }
744 if (mondir == NULL) {
745 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
746 return -ENODEV;
747 }
748 mon_dir = mondir;
749 return 0;
750}
751
Pete Zaitcev21641e32007-02-20 10:37:52 -0800752void mon_text_exit(void)
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800753{
754 debugfs_remove(mon_dir);
755}