USB: add scatter-gather support to usbmon
[deliverable/linux.git] / drivers / usb / mon / mon_text.c
CommitLineData
1da177e4
LT
1/*
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>
4186ecf8 11#include <linux/mutex.h>
6f23ee1f 12#include <linux/debugfs.h>
b375e116 13#include <linux/scatterlist.h>
1da177e4
LT
14#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
ae0d6cce
PZ
24/*
25 * Defined by USB 2.0 clause 9.3, table 9.2.
26 */
27#define SETUP_MAX 8
28
1da177e4
LT
29/*
30 * This limit exists to prevent OOMs when the user process stops reading.
5b1c674d
PZ
31 * 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.
1da177e4 34 */
f1c9e30b 35#define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
1da177e4 36
f1c9e30b
PZ
37/*
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};
1da177e4
LT
50
51struct mon_event_text {
52 struct list_head e_link;
53 int type; /* submit, complete, etc. */
1da177e4
LT
54 unsigned long id; /* From pointer, most of the time */
55 unsigned int tstamp;
f1c9e30b 56 int busnum;
30c7431d
PZ
57 char devnum;
58 char epnum;
59 char is_in;
60 char xfertype;
1da177e4
LT
61 int length; /* Depends on type: xfer length or act length */
62 int status;
f1c9e30b
PZ
63 int interval;
64 int start_frame;
65 int error_count;
ae0d6cce 66 char setup_flag;
1da177e4 67 char data_flag;
f1c9e30b
PZ
68 int numdesc; /* Full number */
69 struct mon_iso_desc isodesc[ISODESC_MAX];
ae0d6cce 70 unsigned char setup[SETUP_MAX];
1da177e4
LT
71 unsigned char data[DATA_MAX];
72};
73
74#define SLAB_NAME_SZ 30
75struct mon_reader_text {
e18b890b 76 struct kmem_cache *e_slab;
1da177e4
LT
77 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;
4186ecf8 84 struct mutex printf_lock;
1da177e4
LT
85
86 char slab_name[SLAB_NAME_SZ];
87};
88
6f23ee1f
PZ
89static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
90
51cc5068 91static void mon_text_ctor(void *);
1da177e4 92
f1c9e30b
PZ
93struct 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
1da177e4
LT
115/*
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
ae0d6cce 124static inline char mon_text_get_setup(struct mon_event_text *ep,
4d6cd483 125 struct urb *urb, char ev_type, struct mon_bus *mbus)
ae0d6cce
PZ
126{
127
18ea5d00 128 if (ep->xfertype != USB_ENDPOINT_XFER_CONTROL || ev_type != 'S')
ae0d6cce
PZ
129 return '-';
130
ae0d6cce
PZ
131 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
1da177e4 138static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
4d6cd483 139 int len, char ev_type, struct mon_bus *mbus)
1da177e4 140{
b375e116
AS
141 void *src;
142
1da177e4
LT
143 if (len <= 0)
144 return 'L';
02568396
PZ
145 if (len >= DATA_MAX)
146 len = DATA_MAX;
1da177e4 147
18ea5d00 148 if (ep->is_in) {
f1c9e30b 149 if (ev_type != 'C')
b9b09422
PZ
150 return '<';
151 } else {
f1c9e30b 152 if (ev_type != 'S')
b9b09422 153 return '>';
1da177e4
LT
154 }
155
b375e116
AS
156 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;
162
163 /* 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 }
02568396 172
b375e116 173 memcpy(ep->data, src, len);
1da177e4
LT
174 return 0;
175}
176
177static inline unsigned int mon_get_timestamp(void)
178{
179 struct timeval tval;
180 unsigned int stamp;
181
182 do_gettimeofday(&tval);
183 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
184 stamp = stamp * 1000000 + tval.tv_usec;
185 return stamp;
186}
187
188static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
9347d51c 189 char ev_type, int status)
1da177e4
LT
190{
191 struct mon_event_text *ep;
192 unsigned int stamp;
f1c9e30b
PZ
193 struct usb_iso_packet_descriptor *fp;
194 struct mon_iso_desc *dp;
195 int i, ndesc;
1da177e4
LT
196
197 stamp = mon_get_timestamp();
198
199 if (rp->nevents >= EVENT_MAX ||
54e6ecb2 200 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
1da177e4
LT
201 rp->r.m_bus->cnt_text_lost++;
202 return;
203 }
204
205 ep->type = ev_type;
1da177e4 206 ep->id = (unsigned long) urb;
ecb658d3 207 ep->busnum = urb->dev->bus->busnum;
18ea5d00
AS
208 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);
1da177e4
LT
212 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 */
9347d51c 216 ep->status = status;
1da177e4 217
18ea5d00 218 if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
f1c9e30b 219 ep->interval = urb->interval;
18ea5d00 220 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
f1c9e30b
PZ
221 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;
18ea5d00
AS
226 if (ep->xfertype == USB_ENDPOINT_XFER_ISOC &&
227 urb->number_of_packets > 0) {
f1c9e30b
PZ
228 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
4d6cd483
AS
242 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);
1da177e4
LT
245
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;
9347d51c 254 mon_text_event(rp, urb, 'S', -EINPROGRESS);
1da177e4
LT
255}
256
9347d51c 257static void mon_text_complete(void *data, struct urb *urb, int status)
1da177e4
LT
258{
259 struct mon_reader_text *rp = data;
9347d51c 260 mon_text_event(rp, urb, 'C', status);
1da177e4
LT
261}
262
12e72fea
PZ
263static 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 ||
54e6ecb2 269 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
12e72fea
PZ
270 rp->r.m_bus->cnt_text_lost++;
271 return;
272 }
273
274 ep->type = 'E';
12e72fea 275 ep->id = (unsigned long) urb;
f1c9e30b 276 ep->busnum = 0;
18ea5d00
AS
277 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);
12e72fea
PZ
281 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
1da177e4
LT
293/*
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;
1da177e4
LT
319 struct mon_reader_text *rp;
320 int rc;
321
4186ecf8 322 mutex_lock(&mon_lock);
8e18e294 323 mbus = inode->i_private;
1da177e4 324
80b6ca48 325 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
1da177e4
LT
326 if (rp == NULL) {
327 rc = -ENOMEM;
328 goto err_alloc;
329 }
1da177e4
LT
330 INIT_LIST_HEAD(&rp->e_list);
331 init_waitqueue_head(&rp->wait);
4186ecf8 332 mutex_init(&rp->printf_lock);
1da177e4
LT
333
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;
12e72fea 344 rp->r.rnf_error = mon_text_error;
1da177e4
LT
345 rp->r.rnf_complete = mon_text_complete;
346
ecb658d3 347 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
1da177e4
LT
348 rp->e_slab = kmem_cache_create(rp->slab_name,
349 sizeof(struct mon_event_text), sizeof(long), 0,
20c2df83 350 mon_text_ctor);
1da177e4
LT
351 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;
4186ecf8 359 mutex_unlock(&mon_lock);
1da177e4
LT
360 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:
4186ecf8 369 mutex_unlock(&mon_lock);
1da177e4
LT
370 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 */
f1c9e30b
PZ
379static ssize_t mon_text_read_t(struct file *file, char __user *buf,
380 size_t nbytes, loff_t *ppos)
381{
382 struct mon_reader_text *rp = file->private_data;
383 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,
1da177e4
LT
407 size_t nbytes, loff_t *ppos)
408{
409 struct mon_reader_text *rp = file->private_data;
f1c9e30b
PZ
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);
18ea5d00 423 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
f1c9e30b
PZ
424 mon_text_read_isostat(rp, &ptr, ep);
425 mon_text_read_isodesc(rp, &ptr, ep);
18ea5d00 426 } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
f1c9e30b
PZ
427 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{
1da177e4
LT
445 struct mon_bus *mbus = rp->r.m_bus;
446 DECLARE_WAITQUEUE(waita, current);
447 struct mon_event_text *ep;
1da177e4
LT
448
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);
f1c9e30b 455 return ERR_PTR(-EWOULDBLOCK);
1da177e4
LT
456 }
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);
f1c9e30b 464 return ERR_PTR(-EINTR);
1da177e4
LT
465 }
466 set_current_state(TASK_INTERRUPTIBLE);
467 }
468 set_current_state(TASK_RUNNING);
469 remove_wait_queue(&rp->wait, &waita);
f1c9e30b
PZ
470 return ep;
471}
1da177e4 472
f1c9e30b
PZ
473static 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;
1da177e4 477
18ea5d00
AS
478 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;
1da177e4
LT
483 default: /* PIPE_BULK */ utype = 'B';
484 }
f1c9e30b 485 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
ae0d6cce 486 "%lx %u %c %c%c:%03u:%02u",
1da177e4 487 ep->id, ep->tstamp, ep->type,
18ea5d00 488 utype, udir, ep->devnum, ep->epnum);
f1c9e30b
PZ
489}
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
18ea5d00
AS
496 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;
f1c9e30b
PZ
501 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,
18ea5d00 506 utype, udir, ep->busnum, ep->devnum, ep->epnum);
f1c9e30b
PZ
507}
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{
ae0d6cce
PZ
512
513 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
f1c9e30b 514 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
ae0d6cce
PZ
515 " 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 */
f1c9e30b 522 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
ae0d6cce
PZ
523 " %c __ __ ____ ____ ____", ep->setup_flag);
524 } else { /* No setup for this kind of URB */
f1c9e30b
PZ
525 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
526 " %d", ep->status);
527 }
528}
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);
ae0d6cce 547 }
f1c9e30b
PZ
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;
1da177e4
LT
576
577 if ((data_len = ep->length) > 0) {
578 if (ep->data_flag == 0) {
f1c9e30b
PZ
579 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
580 " =");
1da177e4
LT
581 if (data_len >= DATA_MAX)
582 data_len = DATA_MAX;
583 for (i = 0; i < data_len; i++) {
584 if (i % 4 == 0) {
f1c9e30b
PZ
585 p->cnt += snprintf(p->pbuf + p->cnt,
586 p->limit - p->cnt,
1da177e4
LT
587 " ");
588 }
f1c9e30b
PZ
589 p->cnt += snprintf(p->pbuf + p->cnt,
590 p->limit - p->cnt,
1da177e4
LT
591 "%02x", ep->data[i]);
592 }
f1c9e30b
PZ
593 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
594 "\n");
1da177e4 595 } else {
f1c9e30b 596 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
1da177e4
LT
597 " %c\n", ep->data_flag);
598 }
599 } else {
f1c9e30b 600 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
1da177e4 601 }
1da177e4
LT
602}
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
4186ecf8 612 mutex_lock(&mon_lock);
8e18e294 613 mbus = inode->i_private;
1da177e4
LT
614
615 if (mbus->nreaders <= 0) {
616 printk(KERN_ERR TAG ": consistency error on close\n");
4186ecf8 617 mutex_unlock(&mon_lock);
1da177e4
LT
618 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
4186ecf8 643 mutex_unlock(&mon_lock);
1da177e4
LT
644 return 0;
645}
646
f1c9e30b 647static const struct file_operations mon_fops_text_t = {
1da177e4
LT
648 .owner = THIS_MODULE,
649 .open = mon_text_open,
650 .llseek = no_llseek,
f1c9e30b
PZ
651 .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,
1da177e4
LT
660 .release = mon_text_release,
661};
662
ce7cd137 663int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
6f23ee1f
PZ
664{
665 struct dentry *d;
666 enum { NAMESZ = 10 };
667 char name[NAMESZ];
ce7cd137 668 int busnum = ubus? ubus->busnum: 0;
6f23ee1f
PZ
669 int rc;
670
ce7cd137
PZ
671 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 }
6f23ee1f 681
ecb658d3 682 rc = snprintf(name, NAMESZ, "%du", busnum);
f1c9e30b
PZ
683 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
ecb658d3 690 rc = snprintf(name, NAMESZ, "%ds", busnum);
6f23ee1f
PZ
691 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:
f1c9e30b
PZ
702 debugfs_remove(mbus->dent_u);
703 mbus->dent_u = NULL;
704err_create_u:
705err_print_u:
ce7cd137
PZ
706 if (ubus != NULL) {
707 debugfs_remove(mbus->dent_t);
708 mbus->dent_t = NULL;
709 }
6f23ee1f
PZ
710err_create_t:
711err_print_t:
712 return 0;
713}
714
715void mon_text_del(struct mon_bus *mbus)
716{
f1c9e30b 717 debugfs_remove(mbus->dent_u);
ce7cd137
PZ
718 if (mbus->dent_t != NULL)
719 debugfs_remove(mbus->dent_t);
6f23ee1f
PZ
720 debugfs_remove(mbus->dent_s);
721}
722
1da177e4
LT
723/*
724 * Slab interface: constructor.
725 */
51cc5068 726static void mon_text_ctor(void *mem)
1da177e4
LT
727{
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
6f23ee1f
PZ
735int __init mon_text_init(void)
736{
737 struct dentry *mondir;
738
f49ce96f 739 mondir = debugfs_create_dir("usbmon", usb_debug_root);
6f23ee1f
PZ
740 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
21641e3f 752void mon_text_exit(void)
6f23ee1f
PZ
753{
754 debugfs_remove(mon_dir);
755}
This page took 2.156777 seconds and 5 git commands to generate.