blob: c302e1983c70dfa6fd911614b09664450b298b95 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/time.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010012#include <linux/mutex.h>
Pete Zaitcev6f23ee12006-12-30 22:43:10 -080013#include <linux/debugfs.h>
Alan Sternb375e112009-11-06 12:32:23 -050014#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/uaccess.h>
16
17#include "usb_mon.h"
18
19/*
20 * No, we do not want arbitrarily long data strings.
21 * Use the binary interface if you want to capture bulk data!
22 */
23#define DATA_MAX 32
24
25/*
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070026 * Defined by USB 2.0 clause 9.3, table 9.2.
27 */
28#define SETUP_MAX 8
29
30/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * This limit exists to prevent OOMs when the user process stops reading.
Pete Zaitcev5b1c6742006-06-09 20:10:10 -070032 * If usbmon were available to unprivileged processes, it might be open
33 * to a local DoS. But we have to keep to root in order to prevent
34 * password sniffing from HID devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080036#define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080038/*
39 * Potentially unlimited number; we limit it for similar allocations.
40 * The usbfs limits this to 128, but we're not quite as generous.
41 */
42#define ISODESC_MAX 5
43
44#define PRINTF_DFL 250 /* with 5 ISOs segs */
45
46struct mon_iso_desc {
47 int status;
48 unsigned int offset;
49 unsigned int length; /* Unsigned here, signed in URB. Historic. */
50};
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52struct mon_event_text {
53 struct list_head e_link;
54 int type; /* submit, complete, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 unsigned long id; /* From pointer, most of the time */
56 unsigned int tstamp;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080057 int busnum;
Pete Zaitcev30c74312007-08-14 00:33:40 -070058 char devnum;
59 char epnum;
60 char is_in;
61 char xfertype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 int length; /* Depends on type: xfer length or act length */
63 int status;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080064 int interval;
65 int start_frame;
66 int error_count;
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070067 char setup_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 char data_flag;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080069 int numdesc; /* Full number */
70 struct mon_iso_desc isodesc[ISODESC_MAX];
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070071 unsigned char setup[SETUP_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned char data[DATA_MAX];
73};
74
75#define SLAB_NAME_SZ 30
76struct mon_reader_text {
Christoph Lametere18b8902006-12-06 20:33:20 -080077 struct kmem_cache *e_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int nevents;
79 struct list_head e_list;
80 struct mon_reader r; /* In C, parent class can be placed anywhere */
81
82 wait_queue_head_t wait;
83 int printf_size;
84 char *printf_buf;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010085 struct mutex printf_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 char slab_name[SLAB_NAME_SZ];
88};
89
Pete Zaitcev6f23ee12006-12-30 22:43:10 -080090static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
91
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070092static void mon_text_ctor(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080094struct mon_text_ptr {
95 int cnt, limit;
96 char *pbuf;
97};
98
99static struct mon_event_text *
100 mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
101static void mon_text_read_head_t(struct mon_reader_text *rp,
102 struct mon_text_ptr *p, const struct mon_event_text *ep);
103static void mon_text_read_head_u(struct mon_reader_text *rp,
104 struct mon_text_ptr *p, const struct mon_event_text *ep);
105static void mon_text_read_statset(struct mon_reader_text *rp,
106 struct mon_text_ptr *p, const struct mon_event_text *ep);
107static void mon_text_read_intstat(struct mon_reader_text *rp,
108 struct mon_text_ptr *p, const struct mon_event_text *ep);
109static void mon_text_read_isostat(struct mon_reader_text *rp,
110 struct mon_text_ptr *p, const struct mon_event_text *ep);
111static void mon_text_read_isodesc(struct mon_reader_text *rp,
112 struct mon_text_ptr *p, const struct mon_event_text *ep);
113static void mon_text_read_data(struct mon_reader_text *rp,
114 struct mon_text_ptr *p, const struct mon_event_text *ep);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
117 * mon_text_submit
118 * mon_text_complete
119 *
120 * May be called from an interrupt.
121 *
122 * This is called with the whole mon_bus locked, so no additional lock.
123 */
124
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700125static inline char mon_text_get_setup(struct mon_event_text *ep,
Alan Stern4d6cd482006-08-30 11:35:21 -0400126 struct urb *urb, char ev_type, struct mon_bus *mbus)
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700127{
128
Alan Stern18ea5d02007-07-30 17:10:36 -0400129 if (ep->xfertype != USB_ENDPOINT_XFER_CONTROL || ev_type != 'S')
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700130 return '-';
131
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700132 if (urb->setup_packet == NULL)
133 return 'Z'; /* '0' would be not as pretty. */
134
135 memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
136 return 0;
137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
Alan Stern4d6cd482006-08-30 11:35:21 -0400140 int len, char ev_type, struct mon_bus *mbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Alan Sternb375e112009-11-06 12:32:23 -0500142 void *src;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (len <= 0)
145 return 'L';
Pete Zaitcev02568392005-08-15 16:53:57 -0700146 if (len >= DATA_MAX)
147 len = DATA_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Alan Stern18ea5d02007-07-30 17:10:36 -0400149 if (ep->is_in) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800150 if (ev_type != 'C')
Pete Zaitcevb9b09422005-12-03 21:52:10 -0800151 return '<';
152 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800153 if (ev_type != 'S')
Pete Zaitcevb9b09422005-12-03 21:52:10 -0800154 return '>';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156
Alan Sternb375e112009-11-06 12:32:23 -0500157 if (urb->num_sgs == 0) {
158 src = urb->transfer_buffer;
159 if (src == NULL)
160 return 'Z'; /* '0' would be not as pretty. */
161 } else {
Matthew Wilcox910f8d02010-05-01 12:20:01 -0600162 struct scatterlist *sg = urb->sg;
Pete Zaitcev02568392005-08-15 16:53:57 -0700163
Alan Sternff9c8952010-04-02 13:27:28 -0400164 if (PageHighMem(sg_page(sg)))
Alan Sternb375e112009-11-06 12:32:23 -0500165 return 'D';
166
167 /* For the text interface we copy only the first sg buffer */
168 len = min_t(int, sg->length, len);
169 src = sg_virt(sg);
170 }
171
172 memcpy(ep->data, src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 0;
174}
175
176static inline unsigned int mon_get_timestamp(void)
177{
178 struct timeval tval;
179 unsigned int stamp;
180
181 do_gettimeofday(&tval);
Pete Zaitcev47cb17082010-02-19 23:55:57 -0700182 stamp = tval.tv_sec & 0xFFF; /* 2^32 = 4294967296. Limit to 4096s. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 stamp = stamp * 1000000 + tval.tv_usec;
184 return stamp;
185}
186
187static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
Alan Stern9347d512007-08-24 15:41:41 -0400188 char ev_type, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct mon_event_text *ep;
191 unsigned int stamp;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800192 struct usb_iso_packet_descriptor *fp;
193 struct mon_iso_desc *dp;
194 int i, ndesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 stamp = mon_get_timestamp();
197
198 if (rp->nevents >= EVENT_MAX ||
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800199 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 rp->r.m_bus->cnt_text_lost++;
201 return;
202 }
203
204 ep->type = ev_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 ep->id = (unsigned long) urb;
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700206 ep->busnum = urb->dev->bus->busnum;
Alan Stern18ea5d02007-07-30 17:10:36 -0400207 ep->devnum = urb->dev->devnum;
208 ep->epnum = usb_endpoint_num(&urb->ep->desc);
209 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
210 ep->is_in = usb_urb_dir_in(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 ep->tstamp = stamp;
212 ep->length = (ev_type == 'S') ?
213 urb->transfer_buffer_length : urb->actual_length;
214 /* Collecting status makes debugging sense for submits, too */
Alan Stern9347d512007-08-24 15:41:41 -0400215 ep->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Alan Stern18ea5d02007-07-30 17:10:36 -0400217 if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800218 ep->interval = urb->interval;
Alan Stern18ea5d02007-07-30 17:10:36 -0400219 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800220 ep->interval = urb->interval;
221 ep->start_frame = urb->start_frame;
222 ep->error_count = urb->error_count;
223 }
224 ep->numdesc = urb->number_of_packets;
Alan Stern18ea5d02007-07-30 17:10:36 -0400225 if (ep->xfertype == USB_ENDPOINT_XFER_ISOC &&
226 urb->number_of_packets > 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800227 if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
228 ndesc = ISODESC_MAX;
229 fp = urb->iso_frame_desc;
230 dp = ep->isodesc;
231 for (i = 0; i < ndesc; i++) {
232 dp->status = fp->status;
233 dp->offset = fp->offset;
234 dp->length = (ev_type == 'S') ?
235 fp->length : fp->actual_length;
236 fp++;
237 dp++;
238 }
Pete Zaitcevd25bc4d2011-02-03 22:01:36 -0700239 /* Wasteful, but simple to understand: ISO 'C' is sparse. */
240 if (ev_type == 'C')
241 ep->length = urb->transfer_buffer_length;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800242 }
243
Alan Stern4d6cd482006-08-30 11:35:21 -0400244 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
245 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
246 rp->r.m_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 rp->nevents++;
249 list_add_tail(&ep->e_link, &rp->e_list);
250 wake_up(&rp->wait);
251}
252
253static void mon_text_submit(void *data, struct urb *urb)
254{
255 struct mon_reader_text *rp = data;
Alan Stern9347d512007-08-24 15:41:41 -0400256 mon_text_event(rp, urb, 'S', -EINPROGRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Alan Stern9347d512007-08-24 15:41:41 -0400259static void mon_text_complete(void *data, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct mon_reader_text *rp = data;
Alan Stern9347d512007-08-24 15:41:41 -0400262 mon_text_event(rp, urb, 'C', status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700265static void mon_text_error(void *data, struct urb *urb, int error)
266{
267 struct mon_reader_text *rp = data;
268 struct mon_event_text *ep;
269
270 if (rp->nevents >= EVENT_MAX ||
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800271 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700272 rp->r.m_bus->cnt_text_lost++;
273 return;
274 }
275
276 ep->type = 'E';
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700277 ep->id = (unsigned long) urb;
Pete Zaitcev2bc0d1092010-01-05 11:50:07 -0700278 ep->busnum = urb->dev->bus->busnum;
Alan Stern18ea5d02007-07-30 17:10:36 -0400279 ep->devnum = urb->dev->devnum;
280 ep->epnum = usb_endpoint_num(&urb->ep->desc);
281 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
282 ep->is_in = usb_urb_dir_in(urb);
Pete Zaitcev2bc0d1092010-01-05 11:50:07 -0700283 ep->tstamp = mon_get_timestamp();
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700284 ep->length = 0;
285 ep->status = error;
286
287 ep->setup_flag = '-';
288 ep->data_flag = 'E';
289
290 rp->nevents++;
291 list_add_tail(&ep->e_link, &rp->e_list);
292 wake_up(&rp->wait);
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*
296 * Fetch next event from the circular buffer.
297 */
298static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
299 struct mon_bus *mbus)
300{
301 struct list_head *p;
302 unsigned long flags;
303
304 spin_lock_irqsave(&mbus->lock, flags);
305 if (list_empty(&rp->e_list)) {
306 spin_unlock_irqrestore(&mbus->lock, flags);
307 return NULL;
308 }
309 p = rp->e_list.next;
310 list_del(p);
311 --rp->nevents;
312 spin_unlock_irqrestore(&mbus->lock, flags);
313 return list_entry(p, struct mon_event_text, e_link);
314}
315
316/*
317 */
318static int mon_text_open(struct inode *inode, struct file *file)
319{
320 struct mon_bus *mbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct mon_reader_text *rp;
322 int rc;
323
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100324 mutex_lock(&mon_lock);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700325 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100327 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (rp == NULL) {
329 rc = -ENOMEM;
330 goto err_alloc;
331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 INIT_LIST_HEAD(&rp->e_list);
333 init_waitqueue_head(&rp->wait);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100334 mutex_init(&rp->printf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 rp->printf_size = PRINTF_DFL;
337 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
338 if (rp->printf_buf == NULL) {
339 rc = -ENOMEM;
340 goto err_alloc_pr;
341 }
342
343 rp->r.m_bus = mbus;
344 rp->r.r_data = rp;
345 rp->r.rnf_submit = mon_text_submit;
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700346 rp->r.rnf_error = mon_text_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 rp->r.rnf_complete = mon_text_complete;
348
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700349 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 rp->e_slab = kmem_cache_create(rp->slab_name,
351 sizeof(struct mon_event_text), sizeof(long), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900352 mon_text_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (rp->e_slab == NULL) {
354 rc = -ENOMEM;
355 goto err_slab;
356 }
357
358 mon_reader_add(mbus, &rp->r);
359
360 file->private_data = rp;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100361 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0;
363
364// err_busy:
365// kmem_cache_destroy(rp->e_slab);
366err_slab:
367 kfree(rp->printf_buf);
368err_alloc_pr:
369 kfree(rp);
370err_alloc:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100371 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return rc;
373}
374
375/*
376 * For simplicity, we read one record in one system call and throw out
377 * what does not fit. This means that the following does not work:
378 * dd if=/dbg/usbmon/0t bs=10
379 * Also, we do not allow seeks and do not bother advancing the offset.
380 */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800381static ssize_t mon_text_read_t(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 size_t nbytes, loff_t *ppos)
383{
384 struct mon_reader_text *rp = file->private_data;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800385 struct mon_event_text *ep;
386 struct mon_text_ptr ptr;
387
388 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
389 return PTR_ERR(ep);
390 mutex_lock(&rp->printf_lock);
391 ptr.cnt = 0;
392 ptr.pbuf = rp->printf_buf;
393 ptr.limit = rp->printf_size;
394
395 mon_text_read_head_t(rp, &ptr, ep);
396 mon_text_read_statset(rp, &ptr, ep);
397 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
398 " %d", ep->length);
399 mon_text_read_data(rp, &ptr, ep);
400
401 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
402 ptr.cnt = -EFAULT;
403 mutex_unlock(&rp->printf_lock);
404 kmem_cache_free(rp->e_slab, ep);
405 return ptr.cnt;
406}
407
408static ssize_t mon_text_read_u(struct file *file, char __user *buf,
409 size_t nbytes, loff_t *ppos)
410{
411 struct mon_reader_text *rp = file->private_data;
412 struct mon_event_text *ep;
413 struct mon_text_ptr ptr;
414
415 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
416 return PTR_ERR(ep);
417 mutex_lock(&rp->printf_lock);
418 ptr.cnt = 0;
419 ptr.pbuf = rp->printf_buf;
420 ptr.limit = rp->printf_size;
421
422 mon_text_read_head_u(rp, &ptr, ep);
423 if (ep->type == 'E') {
424 mon_text_read_statset(rp, &ptr, ep);
Alan Stern18ea5d02007-07-30 17:10:36 -0400425 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800426 mon_text_read_isostat(rp, &ptr, ep);
427 mon_text_read_isodesc(rp, &ptr, ep);
Alan Stern18ea5d02007-07-30 17:10:36 -0400428 } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800429 mon_text_read_intstat(rp, &ptr, ep);
430 } else {
431 mon_text_read_statset(rp, &ptr, ep);
432 }
433 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
434 " %d", ep->length);
435 mon_text_read_data(rp, &ptr, ep);
436
437 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
438 ptr.cnt = -EFAULT;
439 mutex_unlock(&rp->printf_lock);
440 kmem_cache_free(rp->e_slab, ep);
441 return ptr.cnt;
442}
443
444static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
445 struct file *file)
446{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct mon_bus *mbus = rp->r.m_bus;
448 DECLARE_WAITQUEUE(waita, current);
449 struct mon_event_text *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 add_wait_queue(&rp->wait, &waita);
452 set_current_state(TASK_INTERRUPTIBLE);
453 while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
454 if (file->f_flags & O_NONBLOCK) {
455 set_current_state(TASK_RUNNING);
456 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800457 return ERR_PTR(-EWOULDBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459 /*
460 * We do not count nwaiters, because ->release is supposed
461 * to be called when all openers are gone only.
462 */
463 schedule();
464 if (signal_pending(current)) {
465 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800466 return ERR_PTR(-EINTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468 set_current_state(TASK_INTERRUPTIBLE);
469 }
470 set_current_state(TASK_RUNNING);
471 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800472 return ep;
473}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800475static void mon_text_read_head_t(struct mon_reader_text *rp,
476 struct mon_text_ptr *p, const struct mon_event_text *ep)
477{
478 char udir, utype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Alan Stern18ea5d02007-07-30 17:10:36 -0400480 udir = (ep->is_in ? 'i' : 'o');
481 switch (ep->xfertype) {
482 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
483 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
484 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 default: /* PIPE_BULK */ utype = 'B';
486 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800487 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700488 "%lx %u %c %c%c:%03u:%02u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 ep->id, ep->tstamp, ep->type,
Alan Stern18ea5d02007-07-30 17:10:36 -0400490 utype, udir, ep->devnum, ep->epnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800491}
492
493static void mon_text_read_head_u(struct mon_reader_text *rp,
494 struct mon_text_ptr *p, const struct mon_event_text *ep)
495{
496 char udir, utype;
497
Alan Stern18ea5d02007-07-30 17:10:36 -0400498 udir = (ep->is_in ? 'i' : 'o');
499 switch (ep->xfertype) {
500 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
501 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
502 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800503 default: /* PIPE_BULK */ utype = 'B';
504 }
505 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
506 "%lx %u %c %c%c:%d:%03u:%u",
507 ep->id, ep->tstamp, ep->type,
Alan Stern18ea5d02007-07-30 17:10:36 -0400508 utype, udir, ep->busnum, ep->devnum, ep->epnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800509}
510
511static void mon_text_read_statset(struct mon_reader_text *rp,
512 struct mon_text_ptr *p, const struct mon_event_text *ep)
513{
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700514
515 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800516 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700517 " s %02x %02x %04x %04x %04x",
518 ep->setup[0],
519 ep->setup[1],
520 (ep->setup[3] << 8) | ep->setup[2],
521 (ep->setup[5] << 8) | ep->setup[4],
522 (ep->setup[7] << 8) | ep->setup[6]);
523 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800524 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700525 " %c __ __ ____ ____ ____", ep->setup_flag);
526 } else { /* No setup for this kind of URB */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800527 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
528 " %d", ep->status);
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700529 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800530}
531
532static void mon_text_read_intstat(struct mon_reader_text *rp,
533 struct mon_text_ptr *p, const struct mon_event_text *ep)
534{
535 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
536 " %d:%d", ep->status, ep->interval);
537}
538
539static void mon_text_read_isostat(struct mon_reader_text *rp,
540 struct mon_text_ptr *p, const struct mon_event_text *ep)
541{
542 if (ep->type == 'S') {
543 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
544 " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
545 } else {
546 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
547 " %d:%d:%d:%d",
548 ep->status, ep->interval, ep->start_frame, ep->error_count);
549 }
550}
551
552static void mon_text_read_isodesc(struct mon_reader_text *rp,
553 struct mon_text_ptr *p, const struct mon_event_text *ep)
554{
555 int ndesc; /* Display this many */
556 int i;
557 const struct mon_iso_desc *dp;
558
559 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
560 " %d", ep->numdesc);
561 ndesc = ep->numdesc;
562 if (ndesc > ISODESC_MAX)
563 ndesc = ISODESC_MAX;
564 if (ndesc < 0)
565 ndesc = 0;
566 dp = ep->isodesc;
567 for (i = 0; i < ndesc; i++) {
568 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
569 " %d:%u:%u", dp->status, dp->offset, dp->length);
570 dp++;
571 }
572}
573
574static void mon_text_read_data(struct mon_reader_text *rp,
575 struct mon_text_ptr *p, const struct mon_event_text *ep)
576{
577 int data_len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if ((data_len = ep->length) > 0) {
580 if (ep->data_flag == 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800581 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
582 " =");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (data_len >= DATA_MAX)
584 data_len = DATA_MAX;
585 for (i = 0; i < data_len; i++) {
586 if (i % 4 == 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800587 p->cnt += snprintf(p->pbuf + p->cnt,
588 p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 " ");
590 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800591 p->cnt += snprintf(p->pbuf + p->cnt,
592 p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 "%02x", ep->data[i]);
594 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800595 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
596 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800598 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 " %c\n", ep->data_flag);
600 }
601 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800602 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606static int mon_text_release(struct inode *inode, struct file *file)
607{
608 struct mon_reader_text *rp = file->private_data;
609 struct mon_bus *mbus;
610 /* unsigned long flags; */
611 struct list_head *p;
612 struct mon_event_text *ep;
613
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100614 mutex_lock(&mon_lock);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700615 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (mbus->nreaders <= 0) {
618 printk(KERN_ERR TAG ": consistency error on close\n");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100619 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return 0;
621 }
622 mon_reader_del(mbus, &rp->r);
623
624 /*
625 * In theory, e_list is protected by mbus->lock. However,
626 * after mon_reader_del has finished, the following is the case:
627 * - we are not on reader list anymore, so new events won't be added;
628 * - whole mbus may be dropped if it was orphaned.
629 * So, we better not touch mbus.
630 */
631 /* spin_lock_irqsave(&mbus->lock, flags); */
632 while (!list_empty(&rp->e_list)) {
633 p = rp->e_list.next;
634 ep = list_entry(p, struct mon_event_text, e_link);
635 list_del(p);
636 --rp->nevents;
637 kmem_cache_free(rp->e_slab, ep);
638 }
639 /* spin_unlock_irqrestore(&mbus->lock, flags); */
640
641 kmem_cache_destroy(rp->e_slab);
642 kfree(rp->printf_buf);
643 kfree(rp);
644
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100645 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return 0;
647}
648
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800649static const struct file_operations mon_fops_text_t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 .owner = THIS_MODULE,
651 .open = mon_text_open,
652 .llseek = no_llseek,
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800653 .read = mon_text_read_t,
654 .release = mon_text_release,
655};
656
657static const struct file_operations mon_fops_text_u = {
658 .owner = THIS_MODULE,
659 .open = mon_text_open,
660 .llseek = no_llseek,
661 .read = mon_text_read_u,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 .release = mon_text_release,
663};
664
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700665int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800666{
667 struct dentry *d;
668 enum { NAMESZ = 10 };
669 char name[NAMESZ];
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700670 int busnum = ubus? ubus->busnum: 0;
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800671 int rc;
672
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700673 if (ubus != NULL) {
674 rc = snprintf(name, NAMESZ, "%dt", busnum);
675 if (rc <= 0 || rc >= NAMESZ)
676 goto err_print_t;
677 d = debugfs_create_file(name, 0600, mon_dir, mbus,
678 &mon_fops_text_t);
679 if (d == NULL)
680 goto err_create_t;
681 mbus->dent_t = d;
682 }
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800683
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700684 rc = snprintf(name, NAMESZ, "%du", busnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800685 if (rc <= 0 || rc >= NAMESZ)
686 goto err_print_u;
687 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
688 if (d == NULL)
689 goto err_create_u;
690 mbus->dent_u = d;
691
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700692 rc = snprintf(name, NAMESZ, "%ds", busnum);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800693 if (rc <= 0 || rc >= NAMESZ)
694 goto err_print_s;
695 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
696 if (d == NULL)
697 goto err_create_s;
698 mbus->dent_s = d;
699
700 return 1;
701
702err_create_s:
703err_print_s:
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800704 debugfs_remove(mbus->dent_u);
705 mbus->dent_u = NULL;
706err_create_u:
707err_print_u:
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700708 if (ubus != NULL) {
709 debugfs_remove(mbus->dent_t);
710 mbus->dent_t = NULL;
711 }
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800712err_create_t:
713err_print_t:
714 return 0;
715}
716
717void mon_text_del(struct mon_bus *mbus)
718{
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800719 debugfs_remove(mbus->dent_u);
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700720 if (mbus->dent_t != NULL)
721 debugfs_remove(mbus->dent_t);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800722 debugfs_remove(mbus->dent_s);
723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/*
726 * Slab interface: constructor.
727 */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700728static void mon_text_ctor(void *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 /*
731 * Nothing to initialize. No, really!
732 * So, we fill it with garbage to emulate a reused object.
733 */
734 memset(mem, 0xe5, sizeof(struct mon_event_text));
735}
736
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800737int __init mon_text_init(void)
738{
739 struct dentry *mondir;
740
Greg Kroah-Hartmanf49ce962009-04-24 15:15:49 -0700741 mondir = debugfs_create_dir("usbmon", usb_debug_root);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800742 if (IS_ERR(mondir)) {
743 printk(KERN_NOTICE TAG ": debugfs is not available\n");
744 return -ENODEV;
745 }
746 if (mondir == NULL) {
747 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
748 return -ENODEV;
749 }
750 mon_dir = mondir;
751 return 0;
752}
753
Pete Zaitcev21641e32007-02-20 10:37:52 -0800754void mon_text_exit(void)
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800755{
756 debugfs_remove(mon_dir);
757}