mon_text.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * The USB Monitor, inspired by Dave Harding's USBMon.
  3. *
  4. * This is a text format reader.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/list.h>
  8. #include <linux/usb.h>
  9. #include <linux/time.h>
  10. #include <asm/uaccess.h>
  11. #include "usb_mon.h"
  12. /*
  13. * No, we do not want arbitrarily long data strings.
  14. * Use the binary interface if you want to capture bulk data!
  15. */
  16. #define DATA_MAX 32
  17. /*
  18. * Defined by USB 2.0 clause 9.3, table 9.2.
  19. */
  20. #define SETUP_MAX 8
  21. /*
  22. * This limit exists to prevent OOMs when the user process stops reading.
  23. */
  24. #define EVENT_MAX 25
  25. #define PRINTF_DFL 130
  26. struct mon_event_text {
  27. struct list_head e_link;
  28. int type; /* submit, complete, etc. */
  29. unsigned int pipe; /* Pipe */
  30. unsigned long id; /* From pointer, most of the time */
  31. unsigned int tstamp;
  32. int length; /* Depends on type: xfer length or act length */
  33. int status;
  34. char setup_flag;
  35. char data_flag;
  36. unsigned char setup[SETUP_MAX];
  37. unsigned char data[DATA_MAX];
  38. };
  39. #define SLAB_NAME_SZ 30
  40. struct mon_reader_text {
  41. kmem_cache_t *e_slab;
  42. int nevents;
  43. struct list_head e_list;
  44. struct mon_reader r; /* In C, parent class can be placed anywhere */
  45. wait_queue_head_t wait;
  46. int printf_size;
  47. char *printf_buf;
  48. struct semaphore printf_lock;
  49. char slab_name[SLAB_NAME_SZ];
  50. };
  51. static void mon_text_ctor(void *, kmem_cache_t *, unsigned long);
  52. static void mon_text_dtor(void *, kmem_cache_t *, unsigned long);
  53. /*
  54. * mon_text_submit
  55. * mon_text_complete
  56. *
  57. * May be called from an interrupt.
  58. *
  59. * This is called with the whole mon_bus locked, so no additional lock.
  60. */
  61. static inline char mon_text_get_setup(struct mon_event_text *ep,
  62. struct urb *urb, char ev_type)
  63. {
  64. if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
  65. return '-';
  66. if (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)
  67. return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
  68. if (urb->setup_packet == NULL)
  69. return 'Z'; /* '0' would be not as pretty. */
  70. memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
  71. return 0;
  72. }
  73. static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
  74. int len, char ev_type)
  75. {
  76. int pipe = urb->pipe;
  77. if (len <= 0)
  78. return 'L';
  79. if (len >= DATA_MAX)
  80. len = DATA_MAX;
  81. if (usb_pipein(pipe)) {
  82. if (ev_type == 'S')
  83. return '<';
  84. } else {
  85. if (ev_type == 'C')
  86. return '>';
  87. }
  88. /*
  89. * The check to see if it's safe to poke at data has an enormous
  90. * number of corner cases, but it seems that the following is
  91. * more or less safe.
  92. *
  93. * We do not even try to look transfer_buffer, because it can
  94. * contain non-NULL garbage in case the upper level promised to
  95. * set DMA for the HCD.
  96. */
  97. if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  98. return mon_dmapeek(ep->data, urb->transfer_dma, len);
  99. if (urb->transfer_buffer == NULL)
  100. return 'Z'; /* '0' would be not as pretty. */
  101. memcpy(ep->data, urb->transfer_buffer, len);
  102. return 0;
  103. }
  104. static inline unsigned int mon_get_timestamp(void)
  105. {
  106. struct timeval tval;
  107. unsigned int stamp;
  108. do_gettimeofday(&tval);
  109. stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
  110. stamp = stamp * 1000000 + tval.tv_usec;
  111. return stamp;
  112. }
  113. static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
  114. char ev_type)
  115. {
  116. struct mon_event_text *ep;
  117. unsigned int stamp;
  118. stamp = mon_get_timestamp();
  119. if (rp->nevents >= EVENT_MAX ||
  120. (ep = kmem_cache_alloc(rp->e_slab, SLAB_ATOMIC)) == NULL) {
  121. rp->r.m_bus->cnt_text_lost++;
  122. return;
  123. }
  124. ep->type = ev_type;
  125. ep->pipe = urb->pipe;
  126. ep->id = (unsigned long) urb;
  127. ep->tstamp = stamp;
  128. ep->length = (ev_type == 'S') ?
  129. urb->transfer_buffer_length : urb->actual_length;
  130. /* Collecting status makes debugging sense for submits, too */
  131. ep->status = urb->status;
  132. ep->setup_flag = mon_text_get_setup(ep, urb, ev_type);
  133. ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type);
  134. rp->nevents++;
  135. list_add_tail(&ep->e_link, &rp->e_list);
  136. wake_up(&rp->wait);
  137. }
  138. static void mon_text_submit(void *data, struct urb *urb)
  139. {
  140. struct mon_reader_text *rp = data;
  141. mon_text_event(rp, urb, 'S');
  142. }
  143. static void mon_text_complete(void *data, struct urb *urb)
  144. {
  145. struct mon_reader_text *rp = data;
  146. mon_text_event(rp, urb, 'C');
  147. }
  148. /*
  149. * Fetch next event from the circular buffer.
  150. */
  151. static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
  152. struct mon_bus *mbus)
  153. {
  154. struct list_head *p;
  155. unsigned long flags;
  156. spin_lock_irqsave(&mbus->lock, flags);
  157. if (list_empty(&rp->e_list)) {
  158. spin_unlock_irqrestore(&mbus->lock, flags);
  159. return NULL;
  160. }
  161. p = rp->e_list.next;
  162. list_del(p);
  163. --rp->nevents;
  164. spin_unlock_irqrestore(&mbus->lock, flags);
  165. return list_entry(p, struct mon_event_text, e_link);
  166. }
  167. /*
  168. */
  169. static int mon_text_open(struct inode *inode, struct file *file)
  170. {
  171. struct mon_bus *mbus;
  172. struct usb_bus *ubus;
  173. struct mon_reader_text *rp;
  174. int rc;
  175. down(&mon_lock);
  176. mbus = inode->u.generic_ip;
  177. ubus = mbus->u_bus;
  178. rp = kmalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
  179. if (rp == NULL) {
  180. rc = -ENOMEM;
  181. goto err_alloc;
  182. }
  183. memset(rp, 0, sizeof(struct mon_reader_text));
  184. INIT_LIST_HEAD(&rp->e_list);
  185. init_waitqueue_head(&rp->wait);
  186. init_MUTEX(&rp->printf_lock);
  187. rp->printf_size = PRINTF_DFL;
  188. rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
  189. if (rp->printf_buf == NULL) {
  190. rc = -ENOMEM;
  191. goto err_alloc_pr;
  192. }
  193. rp->r.m_bus = mbus;
  194. rp->r.r_data = rp;
  195. rp->r.rnf_submit = mon_text_submit;
  196. rp->r.rnf_complete = mon_text_complete;
  197. snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
  198. (long)rp);
  199. rp->e_slab = kmem_cache_create(rp->slab_name,
  200. sizeof(struct mon_event_text), sizeof(long), 0,
  201. mon_text_ctor, mon_text_dtor);
  202. if (rp->e_slab == NULL) {
  203. rc = -ENOMEM;
  204. goto err_slab;
  205. }
  206. mon_reader_add(mbus, &rp->r);
  207. file->private_data = rp;
  208. up(&mon_lock);
  209. return 0;
  210. // err_busy:
  211. // kmem_cache_destroy(rp->e_slab);
  212. err_slab:
  213. kfree(rp->printf_buf);
  214. err_alloc_pr:
  215. kfree(rp);
  216. err_alloc:
  217. up(&mon_lock);
  218. return rc;
  219. }
  220. /*
  221. * For simplicity, we read one record in one system call and throw out
  222. * what does not fit. This means that the following does not work:
  223. * dd if=/dbg/usbmon/0t bs=10
  224. * Also, we do not allow seeks and do not bother advancing the offset.
  225. */
  226. static ssize_t mon_text_read(struct file *file, char __user *buf,
  227. size_t nbytes, loff_t *ppos)
  228. {
  229. struct mon_reader_text *rp = file->private_data;
  230. struct mon_bus *mbus = rp->r.m_bus;
  231. DECLARE_WAITQUEUE(waita, current);
  232. struct mon_event_text *ep;
  233. int cnt, limit;
  234. char *pbuf;
  235. char udir, utype;
  236. int data_len, i;
  237. add_wait_queue(&rp->wait, &waita);
  238. set_current_state(TASK_INTERRUPTIBLE);
  239. while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
  240. if (file->f_flags & O_NONBLOCK) {
  241. set_current_state(TASK_RUNNING);
  242. remove_wait_queue(&rp->wait, &waita);
  243. return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
  244. }
  245. /*
  246. * We do not count nwaiters, because ->release is supposed
  247. * to be called when all openers are gone only.
  248. */
  249. schedule();
  250. if (signal_pending(current)) {
  251. remove_wait_queue(&rp->wait, &waita);
  252. return -EINTR;
  253. }
  254. set_current_state(TASK_INTERRUPTIBLE);
  255. }
  256. set_current_state(TASK_RUNNING);
  257. remove_wait_queue(&rp->wait, &waita);
  258. down(&rp->printf_lock);
  259. cnt = 0;
  260. pbuf = rp->printf_buf;
  261. limit = rp->printf_size;
  262. udir = usb_pipein(ep->pipe) ? 'i' : 'o';
  263. switch (usb_pipetype(ep->pipe)) {
  264. case PIPE_ISOCHRONOUS: utype = 'Z'; break;
  265. case PIPE_INTERRUPT: utype = 'I'; break;
  266. case PIPE_CONTROL: utype = 'C'; break;
  267. default: /* PIPE_BULK */ utype = 'B';
  268. }
  269. cnt += snprintf(pbuf + cnt, limit - cnt,
  270. "%lx %u %c %c%c:%03u:%02u",
  271. ep->id, ep->tstamp, ep->type,
  272. utype, udir, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
  273. if (ep->setup_flag == 0) { /* Setup packet is present and captured */
  274. cnt += snprintf(pbuf + cnt, limit - cnt,
  275. " s %02x %02x %04x %04x %04x",
  276. ep->setup[0],
  277. ep->setup[1],
  278. (ep->setup[3] << 8) | ep->setup[2],
  279. (ep->setup[5] << 8) | ep->setup[4],
  280. (ep->setup[7] << 8) | ep->setup[6]);
  281. } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
  282. cnt += snprintf(pbuf + cnt, limit - cnt,
  283. " %c __ __ ____ ____ ____", ep->setup_flag);
  284. } else { /* No setup for this kind of URB */
  285. cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->status);
  286. }
  287. cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->length);
  288. if ((data_len = ep->length) > 0) {
  289. if (ep->data_flag == 0) {
  290. cnt += snprintf(pbuf + cnt, limit - cnt, " =");
  291. if (data_len >= DATA_MAX)
  292. data_len = DATA_MAX;
  293. for (i = 0; i < data_len; i++) {
  294. if (i % 4 == 0) {
  295. cnt += snprintf(pbuf + cnt, limit - cnt,
  296. " ");
  297. }
  298. cnt += snprintf(pbuf + cnt, limit - cnt,
  299. "%02x", ep->data[i]);
  300. }
  301. cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
  302. } else {
  303. cnt += snprintf(pbuf + cnt, limit - cnt,
  304. " %c\n", ep->data_flag);
  305. }
  306. } else {
  307. cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
  308. }
  309. if (copy_to_user(buf, rp->printf_buf, cnt))
  310. cnt = -EFAULT;
  311. up(&rp->printf_lock);
  312. kmem_cache_free(rp->e_slab, ep);
  313. return cnt;
  314. }
  315. static int mon_text_release(struct inode *inode, struct file *file)
  316. {
  317. struct mon_reader_text *rp = file->private_data;
  318. struct mon_bus *mbus;
  319. /* unsigned long flags; */
  320. struct list_head *p;
  321. struct mon_event_text *ep;
  322. down(&mon_lock);
  323. mbus = inode->u.generic_ip;
  324. if (mbus->nreaders <= 0) {
  325. printk(KERN_ERR TAG ": consistency error on close\n");
  326. up(&mon_lock);
  327. return 0;
  328. }
  329. mon_reader_del(mbus, &rp->r);
  330. /*
  331. * In theory, e_list is protected by mbus->lock. However,
  332. * after mon_reader_del has finished, the following is the case:
  333. * - we are not on reader list anymore, so new events won't be added;
  334. * - whole mbus may be dropped if it was orphaned.
  335. * So, we better not touch mbus.
  336. */
  337. /* spin_lock_irqsave(&mbus->lock, flags); */
  338. while (!list_empty(&rp->e_list)) {
  339. p = rp->e_list.next;
  340. ep = list_entry(p, struct mon_event_text, e_link);
  341. list_del(p);
  342. --rp->nevents;
  343. kmem_cache_free(rp->e_slab, ep);
  344. }
  345. /* spin_unlock_irqrestore(&mbus->lock, flags); */
  346. kmem_cache_destroy(rp->e_slab);
  347. kfree(rp->printf_buf);
  348. kfree(rp);
  349. up(&mon_lock);
  350. return 0;
  351. }
  352. struct file_operations mon_fops_text = {
  353. .owner = THIS_MODULE,
  354. .open = mon_text_open,
  355. .llseek = no_llseek,
  356. .read = mon_text_read,
  357. /* .write = mon_text_write, */
  358. /* .poll = mon_text_poll, */
  359. /* .ioctl = mon_text_ioctl, */
  360. .release = mon_text_release,
  361. };
  362. /*
  363. * Slab interface: constructor.
  364. */
  365. static void mon_text_ctor(void *mem, kmem_cache_t *slab, unsigned long sflags)
  366. {
  367. /*
  368. * Nothing to initialize. No, really!
  369. * So, we fill it with garbage to emulate a reused object.
  370. */
  371. memset(mem, 0xe5, sizeof(struct mon_event_text));
  372. }
  373. static void mon_text_dtor(void *mem, kmem_cache_t *slab, unsigned long sflags)
  374. {
  375. ;
  376. }