blob: a7eb4c99342c91d0fabdb8a6d9beab5e09b093b9 [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 /*
154 * The check to see if it's safe to poke at data has an enormous
155 * number of corner cases, but it seems that the following is
156 * more or less safe.
157 *
Pete Zaitcev5b1c6742006-06-09 20:10:10 -0700158 * We do not even try to look at transfer_buffer, because it can
Pete Zaitcev02568392005-08-15 16:53:57 -0700159 * contain non-NULL garbage in case the upper level promised to
160 * set DMA for the HCD.
161 */
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700162 if (urb->dev->bus->uses_dma &&
163 (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
Pete Zaitcev02568392005-08-15 16:53:57 -0700164 return mon_dmapeek(ep->data, urb->transfer_dma, len);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700165 }
Pete Zaitcev02568392005-08-15 16:53:57 -0700166
167 if (urb->transfer_buffer == NULL)
168 return 'Z'; /* '0' would be not as pretty. */
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 memcpy(ep->data, urb->transfer_buffer, len);
171 return 0;
172}
173
174static inline unsigned int mon_get_timestamp(void)
175{
176 struct timeval tval;
177 unsigned int stamp;
178
179 do_gettimeofday(&tval);
180 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
181 stamp = stamp * 1000000 + tval.tv_usec;
182 return stamp;
183}
184
185static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
Alan Stern9347d512007-08-24 15:41:41 -0400186 char ev_type, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct mon_event_text *ep;
189 unsigned int stamp;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800190 struct usb_iso_packet_descriptor *fp;
191 struct mon_iso_desc *dp;
192 int i, ndesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 stamp = mon_get_timestamp();
195
196 if (rp->nevents >= EVENT_MAX ||
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800197 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 rp->r.m_bus->cnt_text_lost++;
199 return;
200 }
201
202 ep->type = ev_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 ep->id = (unsigned long) urb;
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700204 ep->busnum = urb->dev->bus->busnum;
Alan Stern18ea5d02007-07-30 17:10:36 -0400205 ep->devnum = urb->dev->devnum;
206 ep->epnum = usb_endpoint_num(&urb->ep->desc);
207 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
208 ep->is_in = usb_urb_dir_in(urb);
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 */
Alan Stern9347d512007-08-24 15:41:41 -0400213 ep->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Alan Stern18ea5d02007-07-30 17:10:36 -0400215 if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800216 ep->interval = urb->interval;
Alan Stern18ea5d02007-07-30 17:10:36 -0400217 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800218 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;
Alan Stern18ea5d02007-07-30 17:10:36 -0400223 if (ep->xfertype == USB_ENDPOINT_XFER_ISOC &&
224 urb->number_of_packets > 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800225 if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
226 ndesc = ISODESC_MAX;
227 fp = urb->iso_frame_desc;
228 dp = ep->isodesc;
229 for (i = 0; i < ndesc; i++) {
230 dp->status = fp->status;
231 dp->offset = fp->offset;
232 dp->length = (ev_type == 'S') ?
233 fp->length : fp->actual_length;
234 fp++;
235 dp++;
236 }
237 }
238
Alan Stern4d6cd482006-08-30 11:35:21 -0400239 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
240 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
241 rp->r.m_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 rp->nevents++;
244 list_add_tail(&ep->e_link, &rp->e_list);
245 wake_up(&rp->wait);
246}
247
248static void mon_text_submit(void *data, struct urb *urb)
249{
250 struct mon_reader_text *rp = data;
Alan Stern9347d512007-08-24 15:41:41 -0400251 mon_text_event(rp, urb, 'S', -EINPROGRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Alan Stern9347d512007-08-24 15:41:41 -0400254static void mon_text_complete(void *data, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct mon_reader_text *rp = data;
Alan Stern9347d512007-08-24 15:41:41 -0400257 mon_text_event(rp, urb, 'C', status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700260static void mon_text_error(void *data, struct urb *urb, int error)
261{
262 struct mon_reader_text *rp = data;
263 struct mon_event_text *ep;
264
265 if (rp->nevents >= EVENT_MAX ||
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800266 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700267 rp->r.m_bus->cnt_text_lost++;
268 return;
269 }
270
271 ep->type = 'E';
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700272 ep->id = (unsigned long) urb;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800273 ep->busnum = 0;
Alan Stern18ea5d02007-07-30 17:10:36 -0400274 ep->devnum = urb->dev->devnum;
275 ep->epnum = usb_endpoint_num(&urb->ep->desc);
276 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
277 ep->is_in = usb_urb_dir_in(urb);
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700278 ep->tstamp = 0;
279 ep->length = 0;
280 ep->status = error;
281
282 ep->setup_flag = '-';
283 ep->data_flag = 'E';
284
285 rp->nevents++;
286 list_add_tail(&ep->e_link, &rp->e_list);
287 wake_up(&rp->wait);
288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/*
291 * Fetch next event from the circular buffer.
292 */
293static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
294 struct mon_bus *mbus)
295{
296 struct list_head *p;
297 unsigned long flags;
298
299 spin_lock_irqsave(&mbus->lock, flags);
300 if (list_empty(&rp->e_list)) {
301 spin_unlock_irqrestore(&mbus->lock, flags);
302 return NULL;
303 }
304 p = rp->e_list.next;
305 list_del(p);
306 --rp->nevents;
307 spin_unlock_irqrestore(&mbus->lock, flags);
308 return list_entry(p, struct mon_event_text, e_link);
309}
310
311/*
312 */
313static int mon_text_open(struct inode *inode, struct file *file)
314{
315 struct mon_bus *mbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct mon_reader_text *rp;
317 int rc;
318
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100319 mutex_lock(&mon_lock);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700320 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100322 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (rp == NULL) {
324 rc = -ENOMEM;
325 goto err_alloc;
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 INIT_LIST_HEAD(&rp->e_list);
328 init_waitqueue_head(&rp->wait);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100329 mutex_init(&rp->printf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 rp->printf_size = PRINTF_DFL;
332 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
333 if (rp->printf_buf == NULL) {
334 rc = -ENOMEM;
335 goto err_alloc_pr;
336 }
337
338 rp->r.m_bus = mbus;
339 rp->r.r_data = rp;
340 rp->r.rnf_submit = mon_text_submit;
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700341 rp->r.rnf_error = mon_text_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 rp->r.rnf_complete = mon_text_complete;
343
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700344 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 rp->e_slab = kmem_cache_create(rp->slab_name,
346 sizeof(struct mon_event_text), sizeof(long), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900347 mon_text_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (rp->e_slab == NULL) {
349 rc = -ENOMEM;
350 goto err_slab;
351 }
352
353 mon_reader_add(mbus, &rp->r);
354
355 file->private_data = rp;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100356 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358
359// err_busy:
360// kmem_cache_destroy(rp->e_slab);
361err_slab:
362 kfree(rp->printf_buf);
363err_alloc_pr:
364 kfree(rp);
365err_alloc:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100366 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return rc;
368}
369
370/*
371 * For simplicity, we read one record in one system call and throw out
372 * what does not fit. This means that the following does not work:
373 * dd if=/dbg/usbmon/0t bs=10
374 * Also, we do not allow seeks and do not bother advancing the offset.
375 */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800376static ssize_t mon_text_read_t(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 size_t nbytes, loff_t *ppos)
378{
379 struct mon_reader_text *rp = file->private_data;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800380 struct mon_event_text *ep;
381 struct mon_text_ptr ptr;
382
383 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
384 return PTR_ERR(ep);
385 mutex_lock(&rp->printf_lock);
386 ptr.cnt = 0;
387 ptr.pbuf = rp->printf_buf;
388 ptr.limit = rp->printf_size;
389
390 mon_text_read_head_t(rp, &ptr, ep);
391 mon_text_read_statset(rp, &ptr, ep);
392 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
393 " %d", ep->length);
394 mon_text_read_data(rp, &ptr, ep);
395
396 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
397 ptr.cnt = -EFAULT;
398 mutex_unlock(&rp->printf_lock);
399 kmem_cache_free(rp->e_slab, ep);
400 return ptr.cnt;
401}
402
403static ssize_t mon_text_read_u(struct file *file, char __user *buf,
404 size_t nbytes, loff_t *ppos)
405{
406 struct mon_reader_text *rp = file->private_data;
407 struct mon_event_text *ep;
408 struct mon_text_ptr ptr;
409
410 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
411 return PTR_ERR(ep);
412 mutex_lock(&rp->printf_lock);
413 ptr.cnt = 0;
414 ptr.pbuf = rp->printf_buf;
415 ptr.limit = rp->printf_size;
416
417 mon_text_read_head_u(rp, &ptr, ep);
418 if (ep->type == 'E') {
419 mon_text_read_statset(rp, &ptr, ep);
Alan Stern18ea5d02007-07-30 17:10:36 -0400420 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800421 mon_text_read_isostat(rp, &ptr, ep);
422 mon_text_read_isodesc(rp, &ptr, ep);
Alan Stern18ea5d02007-07-30 17:10:36 -0400423 } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800424 mon_text_read_intstat(rp, &ptr, ep);
425 } else {
426 mon_text_read_statset(rp, &ptr, ep);
427 }
428 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
429 " %d", ep->length);
430 mon_text_read_data(rp, &ptr, ep);
431
432 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
433 ptr.cnt = -EFAULT;
434 mutex_unlock(&rp->printf_lock);
435 kmem_cache_free(rp->e_slab, ep);
436 return ptr.cnt;
437}
438
439static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
440 struct file *file)
441{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct mon_bus *mbus = rp->r.m_bus;
443 DECLARE_WAITQUEUE(waita, current);
444 struct mon_event_text *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 add_wait_queue(&rp->wait, &waita);
447 set_current_state(TASK_INTERRUPTIBLE);
448 while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
449 if (file->f_flags & O_NONBLOCK) {
450 set_current_state(TASK_RUNNING);
451 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800452 return ERR_PTR(-EWOULDBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 /*
455 * We do not count nwaiters, because ->release is supposed
456 * to be called when all openers are gone only.
457 */
458 schedule();
459 if (signal_pending(current)) {
460 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800461 return ERR_PTR(-EINTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 set_current_state(TASK_INTERRUPTIBLE);
464 }
465 set_current_state(TASK_RUNNING);
466 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800467 return ep;
468}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800470static void mon_text_read_head_t(struct mon_reader_text *rp,
471 struct mon_text_ptr *p, const struct mon_event_text *ep)
472{
473 char udir, utype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Alan Stern18ea5d02007-07-30 17:10:36 -0400475 udir = (ep->is_in ? 'i' : 'o');
476 switch (ep->xfertype) {
477 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
478 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
479 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 default: /* PIPE_BULK */ utype = 'B';
481 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800482 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700483 "%lx %u %c %c%c:%03u:%02u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 ep->id, ep->tstamp, ep->type,
Alan Stern18ea5d02007-07-30 17:10:36 -0400485 utype, udir, ep->devnum, ep->epnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800486}
487
488static void mon_text_read_head_u(struct mon_reader_text *rp,
489 struct mon_text_ptr *p, const struct mon_event_text *ep)
490{
491 char udir, utype;
492
Alan Stern18ea5d02007-07-30 17:10:36 -0400493 udir = (ep->is_in ? 'i' : 'o');
494 switch (ep->xfertype) {
495 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
496 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
497 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800498 default: /* PIPE_BULK */ utype = 'B';
499 }
500 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
501 "%lx %u %c %c%c:%d:%03u:%u",
502 ep->id, ep->tstamp, ep->type,
Alan Stern18ea5d02007-07-30 17:10:36 -0400503 utype, udir, ep->busnum, ep->devnum, ep->epnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800504}
505
506static void mon_text_read_statset(struct mon_reader_text *rp,
507 struct mon_text_ptr *p, const struct mon_event_text *ep)
508{
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700509
510 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800511 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700512 " s %02x %02x %04x %04x %04x",
513 ep->setup[0],
514 ep->setup[1],
515 (ep->setup[3] << 8) | ep->setup[2],
516 (ep->setup[5] << 8) | ep->setup[4],
517 (ep->setup[7] << 8) | ep->setup[6]);
518 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800519 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700520 " %c __ __ ____ ____ ____", ep->setup_flag);
521 } else { /* No setup for this kind of URB */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800522 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
523 " %d", ep->status);
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700524 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800525}
526
527static void mon_text_read_intstat(struct mon_reader_text *rp,
528 struct mon_text_ptr *p, const struct mon_event_text *ep)
529{
530 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
531 " %d:%d", ep->status, ep->interval);
532}
533
534static void mon_text_read_isostat(struct mon_reader_text *rp,
535 struct mon_text_ptr *p, const struct mon_event_text *ep)
536{
537 if (ep->type == 'S') {
538 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
539 " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
540 } else {
541 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
542 " %d:%d:%d:%d",
543 ep->status, ep->interval, ep->start_frame, ep->error_count);
544 }
545}
546
547static void mon_text_read_isodesc(struct mon_reader_text *rp,
548 struct mon_text_ptr *p, const struct mon_event_text *ep)
549{
550 int ndesc; /* Display this many */
551 int i;
552 const struct mon_iso_desc *dp;
553
554 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
555 " %d", ep->numdesc);
556 ndesc = ep->numdesc;
557 if (ndesc > ISODESC_MAX)
558 ndesc = ISODESC_MAX;
559 if (ndesc < 0)
560 ndesc = 0;
561 dp = ep->isodesc;
562 for (i = 0; i < ndesc; i++) {
563 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
564 " %d:%u:%u", dp->status, dp->offset, dp->length);
565 dp++;
566 }
567}
568
569static void mon_text_read_data(struct mon_reader_text *rp,
570 struct mon_text_ptr *p, const struct mon_event_text *ep)
571{
572 int data_len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if ((data_len = ep->length) > 0) {
575 if (ep->data_flag == 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800576 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
577 " =");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (data_len >= DATA_MAX)
579 data_len = DATA_MAX;
580 for (i = 0; i < data_len; i++) {
581 if (i % 4 == 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800582 p->cnt += snprintf(p->pbuf + p->cnt,
583 p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 " ");
585 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800586 p->cnt += snprintf(p->pbuf + p->cnt,
587 p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 "%02x", ep->data[i]);
589 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800590 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
591 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800593 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 " %c\n", ep->data_flag);
595 }
596 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800597 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601static int mon_text_release(struct inode *inode, struct file *file)
602{
603 struct mon_reader_text *rp = file->private_data;
604 struct mon_bus *mbus;
605 /* unsigned long flags; */
606 struct list_head *p;
607 struct mon_event_text *ep;
608
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100609 mutex_lock(&mon_lock);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700610 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 if (mbus->nreaders <= 0) {
613 printk(KERN_ERR TAG ": consistency error on close\n");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100614 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return 0;
616 }
617 mon_reader_del(mbus, &rp->r);
618
619 /*
620 * In theory, e_list is protected by mbus->lock. However,
621 * after mon_reader_del has finished, the following is the case:
622 * - we are not on reader list anymore, so new events won't be added;
623 * - whole mbus may be dropped if it was orphaned.
624 * So, we better not touch mbus.
625 */
626 /* spin_lock_irqsave(&mbus->lock, flags); */
627 while (!list_empty(&rp->e_list)) {
628 p = rp->e_list.next;
629 ep = list_entry(p, struct mon_event_text, e_link);
630 list_del(p);
631 --rp->nevents;
632 kmem_cache_free(rp->e_slab, ep);
633 }
634 /* spin_unlock_irqrestore(&mbus->lock, flags); */
635
636 kmem_cache_destroy(rp->e_slab);
637 kfree(rp->printf_buf);
638 kfree(rp);
639
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100640 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 0;
642}
643
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800644static const struct file_operations mon_fops_text_t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 .owner = THIS_MODULE,
646 .open = mon_text_open,
647 .llseek = no_llseek,
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800648 .read = mon_text_read_t,
649 .release = mon_text_release,
650};
651
652static const struct file_operations mon_fops_text_u = {
653 .owner = THIS_MODULE,
654 .open = mon_text_open,
655 .llseek = no_llseek,
656 .read = mon_text_read_u,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 .release = mon_text_release,
658};
659
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700660int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800661{
662 struct dentry *d;
663 enum { NAMESZ = 10 };
664 char name[NAMESZ];
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700665 int busnum = ubus? ubus->busnum: 0;
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800666 int rc;
667
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700668 if (ubus != NULL) {
669 rc = snprintf(name, NAMESZ, "%dt", busnum);
670 if (rc <= 0 || rc >= NAMESZ)
671 goto err_print_t;
672 d = debugfs_create_file(name, 0600, mon_dir, mbus,
673 &mon_fops_text_t);
674 if (d == NULL)
675 goto err_create_t;
676 mbus->dent_t = d;
677 }
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800678
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700679 rc = snprintf(name, NAMESZ, "%du", busnum);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800680 if (rc <= 0 || rc >= NAMESZ)
681 goto err_print_u;
682 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
683 if (d == NULL)
684 goto err_create_u;
685 mbus->dent_u = d;
686
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700687 rc = snprintf(name, NAMESZ, "%ds", busnum);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800688 if (rc <= 0 || rc >= NAMESZ)
689 goto err_print_s;
690 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
691 if (d == NULL)
692 goto err_create_s;
693 mbus->dent_s = d;
694
695 return 1;
696
697err_create_s:
698err_print_s:
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800699 debugfs_remove(mbus->dent_u);
700 mbus->dent_u = NULL;
701err_create_u:
702err_print_u:
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700703 if (ubus != NULL) {
704 debugfs_remove(mbus->dent_t);
705 mbus->dent_t = NULL;
706 }
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800707err_create_t:
708err_print_t:
709 return 0;
710}
711
712void mon_text_del(struct mon_bus *mbus)
713{
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800714 debugfs_remove(mbus->dent_u);
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700715 if (mbus->dent_t != NULL)
716 debugfs_remove(mbus->dent_t);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800717 debugfs_remove(mbus->dent_s);
718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*
721 * Slab interface: constructor.
722 */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700723static void mon_text_ctor(void *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 /*
726 * Nothing to initialize. No, really!
727 * So, we fill it with garbage to emulate a reused object.
728 */
729 memset(mem, 0xe5, sizeof(struct mon_event_text));
730}
731
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800732int __init mon_text_init(void)
733{
734 struct dentry *mondir;
735
Greg Kroah-Hartmanf49ce962009-04-24 15:15:49 -0700736 mondir = debugfs_create_dir("usbmon", usb_debug_root);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800737 if (IS_ERR(mondir)) {
738 printk(KERN_NOTICE TAG ": debugfs is not available\n");
739 return -ENODEV;
740 }
741 if (mondir == NULL) {
742 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
743 return -ENODEV;
744 }
745 mon_dir = mondir;
746 return 0;
747}
748
Pete Zaitcev21641e32007-02-20 10:37:52 -0800749void mon_text_exit(void)
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800750{
751 debugfs_remove(mon_dir);
752}