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