Commit | Line | Data |
---|---|---|
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> |
1da177e4 LT |
13 | #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 | ||
ae0d6cce PZ |
23 | /* |
24 | * Defined by USB 2.0 clause 9.3, table 9.2. | |
25 | */ | |
26 | #define SETUP_MAX 8 | |
27 | ||
1da177e4 LT |
28 | /* |
29 | * This limit exists to prevent OOMs when the user process stops reading. | |
5b1c674d PZ |
30 | * 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. | |
1da177e4 | 33 | */ |
f1c9e30b | 34 | #define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text)) |
1da177e4 | 35 | |
f1c9e30b PZ |
36 | /* |
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 | ||
44 | struct mon_iso_desc { | |
45 | int status; | |
46 | unsigned int offset; | |
47 | unsigned int length; /* Unsigned here, signed in URB. Historic. */ | |
48 | }; | |
1da177e4 LT |
49 | |
50 | struct mon_event_text { | |
51 | struct list_head e_link; | |
52 | int type; /* submit, complete, etc. */ | |
1da177e4 LT |
53 | unsigned long id; /* From pointer, most of the time */ |
54 | unsigned int tstamp; | |
f1c9e30b | 55 | int busnum; |
30c7431d PZ |
56 | char devnum; |
57 | char epnum; | |
58 | char is_in; | |
59 | char xfertype; | |
1da177e4 LT |
60 | int length; /* Depends on type: xfer length or act length */ |
61 | int status; | |
f1c9e30b PZ |
62 | int interval; |
63 | int start_frame; | |
64 | int error_count; | |
ae0d6cce | 65 | char setup_flag; |
1da177e4 | 66 | char data_flag; |
f1c9e30b PZ |
67 | int numdesc; /* Full number */ |
68 | struct mon_iso_desc isodesc[ISODESC_MAX]; | |
ae0d6cce | 69 | unsigned char setup[SETUP_MAX]; |
1da177e4 LT |
70 | unsigned char data[DATA_MAX]; |
71 | }; | |
72 | ||
73 | #define SLAB_NAME_SZ 30 | |
74 | struct mon_reader_text { | |
e18b890b | 75 | struct kmem_cache *e_slab; |
1da177e4 LT |
76 | 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; | |
4186ecf8 | 83 | struct mutex printf_lock; |
1da177e4 LT |
84 | |
85 | char slab_name[SLAB_NAME_SZ]; | |
86 | }; | |
87 | ||
6f23ee1f PZ |
88 | static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */ |
89 | ||
51cc5068 | 90 | static void mon_text_ctor(void *); |
1da177e4 | 91 | |
f1c9e30b PZ |
92 | struct mon_text_ptr { |
93 | int cnt, limit; | |
94 | char *pbuf; | |
95 | }; | |
96 | ||
97 | static struct mon_event_text * | |
98 | mon_text_read_wait(struct mon_reader_text *rp, struct file *file); | |
99 | static void mon_text_read_head_t(struct mon_reader_text *rp, | |
100 | struct mon_text_ptr *p, const struct mon_event_text *ep); | |
101 | static void mon_text_read_head_u(struct mon_reader_text *rp, | |
102 | struct mon_text_ptr *p, const struct mon_event_text *ep); | |
103 | static void mon_text_read_statset(struct mon_reader_text *rp, | |
104 | struct mon_text_ptr *p, const struct mon_event_text *ep); | |
105 | static void mon_text_read_intstat(struct mon_reader_text *rp, | |
106 | struct mon_text_ptr *p, const struct mon_event_text *ep); | |
107 | static void mon_text_read_isostat(struct mon_reader_text *rp, | |
108 | struct mon_text_ptr *p, const struct mon_event_text *ep); | |
109 | static void mon_text_read_isodesc(struct mon_reader_text *rp, | |
110 | struct mon_text_ptr *p, const struct mon_event_text *ep); | |
111 | static void mon_text_read_data(struct mon_reader_text *rp, | |
112 | struct mon_text_ptr *p, const struct mon_event_text *ep); | |
113 | ||
1da177e4 LT |
114 | /* |
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 | ||
ae0d6cce | 123 | static inline char mon_text_get_setup(struct mon_event_text *ep, |
4d6cd483 | 124 | struct urb *urb, char ev_type, struct mon_bus *mbus) |
ae0d6cce PZ |
125 | { |
126 | ||
18ea5d00 | 127 | if (ep->xfertype != USB_ENDPOINT_XFER_CONTROL || ev_type != 'S') |
ae0d6cce PZ |
128 | return '-'; |
129 | ||
ae0d6cce PZ |
130 | 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 | ||
1da177e4 | 137 | static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb, |
4d6cd483 | 138 | int len, char ev_type, struct mon_bus *mbus) |
1da177e4 | 139 | { |
1da177e4 LT |
140 | if (len <= 0) |
141 | return 'L'; | |
02568396 PZ |
142 | if (len >= DATA_MAX) |
143 | len = DATA_MAX; | |
1da177e4 | 144 | |
18ea5d00 | 145 | if (ep->is_in) { |
f1c9e30b | 146 | if (ev_type != 'C') |
b9b09422 PZ |
147 | return '<'; |
148 | } else { | |
f1c9e30b | 149 | if (ev_type != 'S') |
b9b09422 | 150 | return '>'; |
1da177e4 LT |
151 | } |
152 | ||
02568396 PZ |
153 | if (urb->transfer_buffer == NULL) |
154 | return 'Z'; /* '0' would be not as pretty. */ | |
155 | ||
1da177e4 LT |
156 | memcpy(ep->data, urb->transfer_buffer, len); |
157 | return 0; | |
158 | } | |
159 | ||
160 | static inline unsigned int mon_get_timestamp(void) | |
161 | { | |
162 | struct timeval tval; | |
163 | unsigned int stamp; | |
164 | ||
165 | do_gettimeofday(&tval); | |
166 | stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */ | |
167 | stamp = stamp * 1000000 + tval.tv_usec; | |
168 | return stamp; | |
169 | } | |
170 | ||
171 | static void mon_text_event(struct mon_reader_text *rp, struct urb *urb, | |
9347d51c | 172 | char ev_type, int status) |
1da177e4 LT |
173 | { |
174 | struct mon_event_text *ep; | |
175 | unsigned int stamp; | |
f1c9e30b PZ |
176 | struct usb_iso_packet_descriptor *fp; |
177 | struct mon_iso_desc *dp; | |
178 | int i, ndesc; | |
1da177e4 LT |
179 | |
180 | stamp = mon_get_timestamp(); | |
181 | ||
182 | if (rp->nevents >= EVENT_MAX || | |
54e6ecb2 | 183 | (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) { |
1da177e4 LT |
184 | rp->r.m_bus->cnt_text_lost++; |
185 | return; | |
186 | } | |
187 | ||
188 | ep->type = ev_type; | |
1da177e4 | 189 | ep->id = (unsigned long) urb; |
ecb658d3 | 190 | ep->busnum = urb->dev->bus->busnum; |
18ea5d00 AS |
191 | ep->devnum = urb->dev->devnum; |
192 | ep->epnum = usb_endpoint_num(&urb->ep->desc); | |
193 | ep->xfertype = usb_endpoint_type(&urb->ep->desc); | |
194 | ep->is_in = usb_urb_dir_in(urb); | |
1da177e4 LT |
195 | ep->tstamp = stamp; |
196 | ep->length = (ev_type == 'S') ? | |
197 | urb->transfer_buffer_length : urb->actual_length; | |
198 | /* Collecting status makes debugging sense for submits, too */ | |
9347d51c | 199 | ep->status = status; |
1da177e4 | 200 | |
18ea5d00 | 201 | if (ep->xfertype == USB_ENDPOINT_XFER_INT) { |
f1c9e30b | 202 | ep->interval = urb->interval; |
18ea5d00 | 203 | } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) { |
f1c9e30b PZ |
204 | ep->interval = urb->interval; |
205 | ep->start_frame = urb->start_frame; | |
206 | ep->error_count = urb->error_count; | |
207 | } | |
208 | ep->numdesc = urb->number_of_packets; | |
18ea5d00 AS |
209 | if (ep->xfertype == USB_ENDPOINT_XFER_ISOC && |
210 | urb->number_of_packets > 0) { | |
f1c9e30b PZ |
211 | if ((ndesc = urb->number_of_packets) > ISODESC_MAX) |
212 | ndesc = ISODESC_MAX; | |
213 | fp = urb->iso_frame_desc; | |
214 | dp = ep->isodesc; | |
215 | for (i = 0; i < ndesc; i++) { | |
216 | dp->status = fp->status; | |
217 | dp->offset = fp->offset; | |
218 | dp->length = (ev_type == 'S') ? | |
219 | fp->length : fp->actual_length; | |
220 | fp++; | |
221 | dp++; | |
222 | } | |
223 | } | |
224 | ||
4d6cd483 AS |
225 | ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus); |
226 | ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type, | |
227 | rp->r.m_bus); | |
1da177e4 LT |
228 | |
229 | rp->nevents++; | |
230 | list_add_tail(&ep->e_link, &rp->e_list); | |
231 | wake_up(&rp->wait); | |
232 | } | |
233 | ||
234 | static void mon_text_submit(void *data, struct urb *urb) | |
235 | { | |
236 | struct mon_reader_text *rp = data; | |
9347d51c | 237 | mon_text_event(rp, urb, 'S', -EINPROGRESS); |
1da177e4 LT |
238 | } |
239 | ||
9347d51c | 240 | static void mon_text_complete(void *data, struct urb *urb, int status) |
1da177e4 LT |
241 | { |
242 | struct mon_reader_text *rp = data; | |
9347d51c | 243 | mon_text_event(rp, urb, 'C', status); |
1da177e4 LT |
244 | } |
245 | ||
12e72fea PZ |
246 | static void mon_text_error(void *data, struct urb *urb, int error) |
247 | { | |
248 | struct mon_reader_text *rp = data; | |
249 | struct mon_event_text *ep; | |
250 | ||
251 | if (rp->nevents >= EVENT_MAX || | |
54e6ecb2 | 252 | (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) { |
12e72fea PZ |
253 | rp->r.m_bus->cnt_text_lost++; |
254 | return; | |
255 | } | |
256 | ||
257 | ep->type = 'E'; | |
12e72fea | 258 | ep->id = (unsigned long) urb; |
f1c9e30b | 259 | ep->busnum = 0; |
18ea5d00 AS |
260 | ep->devnum = urb->dev->devnum; |
261 | ep->epnum = usb_endpoint_num(&urb->ep->desc); | |
262 | ep->xfertype = usb_endpoint_type(&urb->ep->desc); | |
263 | ep->is_in = usb_urb_dir_in(urb); | |
12e72fea PZ |
264 | ep->tstamp = 0; |
265 | ep->length = 0; | |
266 | ep->status = error; | |
267 | ||
268 | ep->setup_flag = '-'; | |
269 | ep->data_flag = 'E'; | |
270 | ||
271 | rp->nevents++; | |
272 | list_add_tail(&ep->e_link, &rp->e_list); | |
273 | wake_up(&rp->wait); | |
274 | } | |
275 | ||
1da177e4 LT |
276 | /* |
277 | * Fetch next event from the circular buffer. | |
278 | */ | |
279 | static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp, | |
280 | struct mon_bus *mbus) | |
281 | { | |
282 | struct list_head *p; | |
283 | unsigned long flags; | |
284 | ||
285 | spin_lock_irqsave(&mbus->lock, flags); | |
286 | if (list_empty(&rp->e_list)) { | |
287 | spin_unlock_irqrestore(&mbus->lock, flags); | |
288 | return NULL; | |
289 | } | |
290 | p = rp->e_list.next; | |
291 | list_del(p); | |
292 | --rp->nevents; | |
293 | spin_unlock_irqrestore(&mbus->lock, flags); | |
294 | return list_entry(p, struct mon_event_text, e_link); | |
295 | } | |
296 | ||
297 | /* | |
298 | */ | |
299 | static int mon_text_open(struct inode *inode, struct file *file) | |
300 | { | |
301 | struct mon_bus *mbus; | |
1da177e4 LT |
302 | struct mon_reader_text *rp; |
303 | int rc; | |
304 | ||
4186ecf8 | 305 | mutex_lock(&mon_lock); |
8e18e294 | 306 | mbus = inode->i_private; |
1da177e4 | 307 | |
80b6ca48 | 308 | rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL); |
1da177e4 LT |
309 | if (rp == NULL) { |
310 | rc = -ENOMEM; | |
311 | goto err_alloc; | |
312 | } | |
1da177e4 LT |
313 | INIT_LIST_HEAD(&rp->e_list); |
314 | init_waitqueue_head(&rp->wait); | |
4186ecf8 | 315 | mutex_init(&rp->printf_lock); |
1da177e4 LT |
316 | |
317 | rp->printf_size = PRINTF_DFL; | |
318 | rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL); | |
319 | if (rp->printf_buf == NULL) { | |
320 | rc = -ENOMEM; | |
321 | goto err_alloc_pr; | |
322 | } | |
323 | ||
324 | rp->r.m_bus = mbus; | |
325 | rp->r.r_data = rp; | |
326 | rp->r.rnf_submit = mon_text_submit; | |
12e72fea | 327 | rp->r.rnf_error = mon_text_error; |
1da177e4 LT |
328 | rp->r.rnf_complete = mon_text_complete; |
329 | ||
ecb658d3 | 330 | snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp); |
1da177e4 LT |
331 | rp->e_slab = kmem_cache_create(rp->slab_name, |
332 | sizeof(struct mon_event_text), sizeof(long), 0, | |
20c2df83 | 333 | mon_text_ctor); |
1da177e4 LT |
334 | if (rp->e_slab == NULL) { |
335 | rc = -ENOMEM; | |
336 | goto err_slab; | |
337 | } | |
338 | ||
339 | mon_reader_add(mbus, &rp->r); | |
340 | ||
341 | file->private_data = rp; | |
4186ecf8 | 342 | mutex_unlock(&mon_lock); |
1da177e4 LT |
343 | return 0; |
344 | ||
345 | // err_busy: | |
346 | // kmem_cache_destroy(rp->e_slab); | |
347 | err_slab: | |
348 | kfree(rp->printf_buf); | |
349 | err_alloc_pr: | |
350 | kfree(rp); | |
351 | err_alloc: | |
4186ecf8 | 352 | mutex_unlock(&mon_lock); |
1da177e4 LT |
353 | return rc; |
354 | } | |
355 | ||
356 | /* | |
357 | * For simplicity, we read one record in one system call and throw out | |
358 | * what does not fit. This means that the following does not work: | |
359 | * dd if=/dbg/usbmon/0t bs=10 | |
360 | * Also, we do not allow seeks and do not bother advancing the offset. | |
361 | */ | |
f1c9e30b PZ |
362 | static ssize_t mon_text_read_t(struct file *file, char __user *buf, |
363 | size_t nbytes, loff_t *ppos) | |
364 | { | |
365 | struct mon_reader_text *rp = file->private_data; | |
366 | struct mon_event_text *ep; | |
367 | struct mon_text_ptr ptr; | |
368 | ||
369 | if (IS_ERR(ep = mon_text_read_wait(rp, file))) | |
370 | return PTR_ERR(ep); | |
371 | mutex_lock(&rp->printf_lock); | |
372 | ptr.cnt = 0; | |
373 | ptr.pbuf = rp->printf_buf; | |
374 | ptr.limit = rp->printf_size; | |
375 | ||
376 | mon_text_read_head_t(rp, &ptr, ep); | |
377 | mon_text_read_statset(rp, &ptr, ep); | |
378 | ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt, | |
379 | " %d", ep->length); | |
380 | mon_text_read_data(rp, &ptr, ep); | |
381 | ||
382 | if (copy_to_user(buf, rp->printf_buf, ptr.cnt)) | |
383 | ptr.cnt = -EFAULT; | |
384 | mutex_unlock(&rp->printf_lock); | |
385 | kmem_cache_free(rp->e_slab, ep); | |
386 | return ptr.cnt; | |
387 | } | |
388 | ||
389 | static ssize_t mon_text_read_u(struct file *file, char __user *buf, | |
1da177e4 LT |
390 | size_t nbytes, loff_t *ppos) |
391 | { | |
392 | struct mon_reader_text *rp = file->private_data; | |
f1c9e30b PZ |
393 | struct mon_event_text *ep; |
394 | struct mon_text_ptr ptr; | |
395 | ||
396 | if (IS_ERR(ep = mon_text_read_wait(rp, file))) | |
397 | return PTR_ERR(ep); | |
398 | mutex_lock(&rp->printf_lock); | |
399 | ptr.cnt = 0; | |
400 | ptr.pbuf = rp->printf_buf; | |
401 | ptr.limit = rp->printf_size; | |
402 | ||
403 | mon_text_read_head_u(rp, &ptr, ep); | |
404 | if (ep->type == 'E') { | |
405 | mon_text_read_statset(rp, &ptr, ep); | |
18ea5d00 | 406 | } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) { |
f1c9e30b PZ |
407 | mon_text_read_isostat(rp, &ptr, ep); |
408 | mon_text_read_isodesc(rp, &ptr, ep); | |
18ea5d00 | 409 | } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) { |
f1c9e30b PZ |
410 | mon_text_read_intstat(rp, &ptr, ep); |
411 | } else { | |
412 | mon_text_read_statset(rp, &ptr, ep); | |
413 | } | |
414 | ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt, | |
415 | " %d", ep->length); | |
416 | mon_text_read_data(rp, &ptr, ep); | |
417 | ||
418 | if (copy_to_user(buf, rp->printf_buf, ptr.cnt)) | |
419 | ptr.cnt = -EFAULT; | |
420 | mutex_unlock(&rp->printf_lock); | |
421 | kmem_cache_free(rp->e_slab, ep); | |
422 | return ptr.cnt; | |
423 | } | |
424 | ||
425 | static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp, | |
426 | struct file *file) | |
427 | { | |
1da177e4 LT |
428 | struct mon_bus *mbus = rp->r.m_bus; |
429 | DECLARE_WAITQUEUE(waita, current); | |
430 | struct mon_event_text *ep; | |
1da177e4 LT |
431 | |
432 | add_wait_queue(&rp->wait, &waita); | |
433 | set_current_state(TASK_INTERRUPTIBLE); | |
434 | while ((ep = mon_text_fetch(rp, mbus)) == NULL) { | |
435 | if (file->f_flags & O_NONBLOCK) { | |
436 | set_current_state(TASK_RUNNING); | |
437 | remove_wait_queue(&rp->wait, &waita); | |
f1c9e30b | 438 | return ERR_PTR(-EWOULDBLOCK); |
1da177e4 LT |
439 | } |
440 | /* | |
441 | * We do not count nwaiters, because ->release is supposed | |
442 | * to be called when all openers are gone only. | |
443 | */ | |
444 | schedule(); | |
445 | if (signal_pending(current)) { | |
446 | remove_wait_queue(&rp->wait, &waita); | |
f1c9e30b | 447 | return ERR_PTR(-EINTR); |
1da177e4 LT |
448 | } |
449 | set_current_state(TASK_INTERRUPTIBLE); | |
450 | } | |
451 | set_current_state(TASK_RUNNING); | |
452 | remove_wait_queue(&rp->wait, &waita); | |
f1c9e30b PZ |
453 | return ep; |
454 | } | |
1da177e4 | 455 | |
f1c9e30b PZ |
456 | static void mon_text_read_head_t(struct mon_reader_text *rp, |
457 | struct mon_text_ptr *p, const struct mon_event_text *ep) | |
458 | { | |
459 | char udir, utype; | |
1da177e4 | 460 | |
18ea5d00 AS |
461 | udir = (ep->is_in ? 'i' : 'o'); |
462 | switch (ep->xfertype) { | |
463 | case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break; | |
464 | case USB_ENDPOINT_XFER_INT: utype = 'I'; break; | |
465 | case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break; | |
1da177e4 LT |
466 | default: /* PIPE_BULK */ utype = 'B'; |
467 | } | |
f1c9e30b | 468 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
ae0d6cce | 469 | "%lx %u %c %c%c:%03u:%02u", |
1da177e4 | 470 | ep->id, ep->tstamp, ep->type, |
18ea5d00 | 471 | utype, udir, ep->devnum, ep->epnum); |
f1c9e30b PZ |
472 | } |
473 | ||
474 | static void mon_text_read_head_u(struct mon_reader_text *rp, | |
475 | struct mon_text_ptr *p, const struct mon_event_text *ep) | |
476 | { | |
477 | char udir, utype; | |
478 | ||
18ea5d00 AS |
479 | udir = (ep->is_in ? 'i' : 'o'); |
480 | switch (ep->xfertype) { | |
481 | case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break; | |
482 | case USB_ENDPOINT_XFER_INT: utype = 'I'; break; | |
483 | case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break; | |
f1c9e30b PZ |
484 | default: /* PIPE_BULK */ utype = 'B'; |
485 | } | |
486 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, | |
487 | "%lx %u %c %c%c:%d:%03u:%u", | |
488 | ep->id, ep->tstamp, ep->type, | |
18ea5d00 | 489 | utype, udir, ep->busnum, ep->devnum, ep->epnum); |
f1c9e30b PZ |
490 | } |
491 | ||
492 | static void mon_text_read_statset(struct mon_reader_text *rp, | |
493 | struct mon_text_ptr *p, const struct mon_event_text *ep) | |
494 | { | |
ae0d6cce PZ |
495 | |
496 | if (ep->setup_flag == 0) { /* Setup packet is present and captured */ | |
f1c9e30b | 497 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
ae0d6cce PZ |
498 | " s %02x %02x %04x %04x %04x", |
499 | ep->setup[0], | |
500 | ep->setup[1], | |
501 | (ep->setup[3] << 8) | ep->setup[2], | |
502 | (ep->setup[5] << 8) | ep->setup[4], | |
503 | (ep->setup[7] << 8) | ep->setup[6]); | |
504 | } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */ | |
f1c9e30b | 505 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
ae0d6cce PZ |
506 | " %c __ __ ____ ____ ____", ep->setup_flag); |
507 | } else { /* No setup for this kind of URB */ | |
f1c9e30b PZ |
508 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
509 | " %d", ep->status); | |
510 | } | |
511 | } | |
512 | ||
513 | static void mon_text_read_intstat(struct mon_reader_text *rp, | |
514 | struct mon_text_ptr *p, const struct mon_event_text *ep) | |
515 | { | |
516 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, | |
517 | " %d:%d", ep->status, ep->interval); | |
518 | } | |
519 | ||
520 | static void mon_text_read_isostat(struct mon_reader_text *rp, | |
521 | struct mon_text_ptr *p, const struct mon_event_text *ep) | |
522 | { | |
523 | if (ep->type == 'S') { | |
524 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, | |
525 | " %d:%d:%d", ep->status, ep->interval, ep->start_frame); | |
526 | } else { | |
527 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, | |
528 | " %d:%d:%d:%d", | |
529 | ep->status, ep->interval, ep->start_frame, ep->error_count); | |
ae0d6cce | 530 | } |
f1c9e30b PZ |
531 | } |
532 | ||
533 | static void mon_text_read_isodesc(struct mon_reader_text *rp, | |
534 | struct mon_text_ptr *p, const struct mon_event_text *ep) | |
535 | { | |
536 | int ndesc; /* Display this many */ | |
537 | int i; | |
538 | const struct mon_iso_desc *dp; | |
539 | ||
540 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, | |
541 | " %d", ep->numdesc); | |
542 | ndesc = ep->numdesc; | |
543 | if (ndesc > ISODESC_MAX) | |
544 | ndesc = ISODESC_MAX; | |
545 | if (ndesc < 0) | |
546 | ndesc = 0; | |
547 | dp = ep->isodesc; | |
548 | for (i = 0; i < ndesc; i++) { | |
549 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, | |
550 | " %d:%u:%u", dp->status, dp->offset, dp->length); | |
551 | dp++; | |
552 | } | |
553 | } | |
554 | ||
555 | static void mon_text_read_data(struct mon_reader_text *rp, | |
556 | struct mon_text_ptr *p, const struct mon_event_text *ep) | |
557 | { | |
558 | int data_len, i; | |
1da177e4 LT |
559 | |
560 | if ((data_len = ep->length) > 0) { | |
561 | if (ep->data_flag == 0) { | |
f1c9e30b PZ |
562 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
563 | " ="); | |
1da177e4 LT |
564 | if (data_len >= DATA_MAX) |
565 | data_len = DATA_MAX; | |
566 | for (i = 0; i < data_len; i++) { | |
567 | if (i % 4 == 0) { | |
f1c9e30b PZ |
568 | p->cnt += snprintf(p->pbuf + p->cnt, |
569 | p->limit - p->cnt, | |
1da177e4 LT |
570 | " "); |
571 | } | |
f1c9e30b PZ |
572 | p->cnt += snprintf(p->pbuf + p->cnt, |
573 | p->limit - p->cnt, | |
1da177e4 LT |
574 | "%02x", ep->data[i]); |
575 | } | |
f1c9e30b PZ |
576 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
577 | "\n"); | |
1da177e4 | 578 | } else { |
f1c9e30b | 579 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, |
1da177e4 LT |
580 | " %c\n", ep->data_flag); |
581 | } | |
582 | } else { | |
f1c9e30b | 583 | p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n"); |
1da177e4 | 584 | } |
1da177e4 LT |
585 | } |
586 | ||
587 | static int mon_text_release(struct inode *inode, struct file *file) | |
588 | { | |
589 | struct mon_reader_text *rp = file->private_data; | |
590 | struct mon_bus *mbus; | |
591 | /* unsigned long flags; */ | |
592 | struct list_head *p; | |
593 | struct mon_event_text *ep; | |
594 | ||
4186ecf8 | 595 | mutex_lock(&mon_lock); |
8e18e294 | 596 | mbus = inode->i_private; |
1da177e4 LT |
597 | |
598 | if (mbus->nreaders <= 0) { | |
599 | printk(KERN_ERR TAG ": consistency error on close\n"); | |
4186ecf8 | 600 | mutex_unlock(&mon_lock); |
1da177e4 LT |
601 | return 0; |
602 | } | |
603 | mon_reader_del(mbus, &rp->r); | |
604 | ||
605 | /* | |
606 | * In theory, e_list is protected by mbus->lock. However, | |
607 | * after mon_reader_del has finished, the following is the case: | |
608 | * - we are not on reader list anymore, so new events won't be added; | |
609 | * - whole mbus may be dropped if it was orphaned. | |
610 | * So, we better not touch mbus. | |
611 | */ | |
612 | /* spin_lock_irqsave(&mbus->lock, flags); */ | |
613 | while (!list_empty(&rp->e_list)) { | |
614 | p = rp->e_list.next; | |
615 | ep = list_entry(p, struct mon_event_text, e_link); | |
616 | list_del(p); | |
617 | --rp->nevents; | |
618 | kmem_cache_free(rp->e_slab, ep); | |
619 | } | |
620 | /* spin_unlock_irqrestore(&mbus->lock, flags); */ | |
621 | ||
622 | kmem_cache_destroy(rp->e_slab); | |
623 | kfree(rp->printf_buf); | |
624 | kfree(rp); | |
625 | ||
4186ecf8 | 626 | mutex_unlock(&mon_lock); |
1da177e4 LT |
627 | return 0; |
628 | } | |
629 | ||
f1c9e30b | 630 | static const struct file_operations mon_fops_text_t = { |
1da177e4 LT |
631 | .owner = THIS_MODULE, |
632 | .open = mon_text_open, | |
633 | .llseek = no_llseek, | |
f1c9e30b PZ |
634 | .read = mon_text_read_t, |
635 | .release = mon_text_release, | |
636 | }; | |
637 | ||
638 | static const struct file_operations mon_fops_text_u = { | |
639 | .owner = THIS_MODULE, | |
640 | .open = mon_text_open, | |
641 | .llseek = no_llseek, | |
642 | .read = mon_text_read_u, | |
1da177e4 LT |
643 | .release = mon_text_release, |
644 | }; | |
645 | ||
ce7cd137 | 646 | int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus) |
6f23ee1f PZ |
647 | { |
648 | struct dentry *d; | |
649 | enum { NAMESZ = 10 }; | |
650 | char name[NAMESZ]; | |
ce7cd137 | 651 | int busnum = ubus? ubus->busnum: 0; |
6f23ee1f PZ |
652 | int rc; |
653 | ||
ce7cd137 PZ |
654 | if (ubus != NULL) { |
655 | rc = snprintf(name, NAMESZ, "%dt", busnum); | |
656 | if (rc <= 0 || rc >= NAMESZ) | |
657 | goto err_print_t; | |
658 | d = debugfs_create_file(name, 0600, mon_dir, mbus, | |
659 | &mon_fops_text_t); | |
660 | if (d == NULL) | |
661 | goto err_create_t; | |
662 | mbus->dent_t = d; | |
663 | } | |
6f23ee1f | 664 | |
ecb658d3 | 665 | rc = snprintf(name, NAMESZ, "%du", busnum); |
f1c9e30b PZ |
666 | if (rc <= 0 || rc >= NAMESZ) |
667 | goto err_print_u; | |
668 | d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u); | |
669 | if (d == NULL) | |
670 | goto err_create_u; | |
671 | mbus->dent_u = d; | |
672 | ||
ecb658d3 | 673 | rc = snprintf(name, NAMESZ, "%ds", busnum); |
6f23ee1f PZ |
674 | if (rc <= 0 || rc >= NAMESZ) |
675 | goto err_print_s; | |
676 | d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat); | |
677 | if (d == NULL) | |
678 | goto err_create_s; | |
679 | mbus->dent_s = d; | |
680 | ||
681 | return 1; | |
682 | ||
683 | err_create_s: | |
684 | err_print_s: | |
f1c9e30b PZ |
685 | debugfs_remove(mbus->dent_u); |
686 | mbus->dent_u = NULL; | |
687 | err_create_u: | |
688 | err_print_u: | |
ce7cd137 PZ |
689 | if (ubus != NULL) { |
690 | debugfs_remove(mbus->dent_t); | |
691 | mbus->dent_t = NULL; | |
692 | } | |
6f23ee1f PZ |
693 | err_create_t: |
694 | err_print_t: | |
695 | return 0; | |
696 | } | |
697 | ||
698 | void mon_text_del(struct mon_bus *mbus) | |
699 | { | |
f1c9e30b | 700 | debugfs_remove(mbus->dent_u); |
ce7cd137 PZ |
701 | if (mbus->dent_t != NULL) |
702 | debugfs_remove(mbus->dent_t); | |
6f23ee1f PZ |
703 | debugfs_remove(mbus->dent_s); |
704 | } | |
705 | ||
1da177e4 LT |
706 | /* |
707 | * Slab interface: constructor. | |
708 | */ | |
51cc5068 | 709 | static void mon_text_ctor(void *mem) |
1da177e4 LT |
710 | { |
711 | /* | |
712 | * Nothing to initialize. No, really! | |
713 | * So, we fill it with garbage to emulate a reused object. | |
714 | */ | |
715 | memset(mem, 0xe5, sizeof(struct mon_event_text)); | |
716 | } | |
717 | ||
6f23ee1f PZ |
718 | int __init mon_text_init(void) |
719 | { | |
720 | struct dentry *mondir; | |
721 | ||
f49ce96f | 722 | mondir = debugfs_create_dir("usbmon", usb_debug_root); |
6f23ee1f PZ |
723 | if (IS_ERR(mondir)) { |
724 | printk(KERN_NOTICE TAG ": debugfs is not available\n"); | |
725 | return -ENODEV; | |
726 | } | |
727 | if (mondir == NULL) { | |
728 | printk(KERN_NOTICE TAG ": unable to create usbmon directory\n"); | |
729 | return -ENODEV; | |
730 | } | |
731 | mon_dir = mondir; | |
732 | return 0; | |
733 | } | |
734 | ||
21641e3f | 735 | void mon_text_exit(void) |
6f23ee1f PZ |
736 | { |
737 | debugfs_remove(mon_dir); | |
738 | } |