PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/usb/mon/mon_text.c

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