mon_main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * The USB Monitor, inspired by Dave Harding's USBMon.
  3. *
  4. * mon_main.c: Main file, module initiation and exit, registrations, etc.
  5. *
  6. * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com)
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/usb.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/notifier.h>
  14. #include "usb_mon.h"
  15. #include "../core/hcd.h"
  16. static void mon_submit(struct usb_bus *ubus, struct urb *urb);
  17. static void mon_complete(struct usb_bus *ubus, struct urb *urb);
  18. static void mon_stop(struct mon_bus *mbus);
  19. static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
  20. static void mon_bus_drop(struct kref *r);
  21. static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus);
  22. DECLARE_MUTEX(mon_lock);
  23. static struct dentry *mon_dir; /* /dbg/usbmon */
  24. static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
  25. /*
  26. * Link a reader into the bus.
  27. *
  28. * This must be called with mon_lock taken because of mbus->ref.
  29. */
  30. void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
  31. {
  32. unsigned long flags;
  33. struct usb_bus *ubus;
  34. spin_lock_irqsave(&mbus->lock, flags);
  35. if (mbus->nreaders == 0) {
  36. ubus = mbus->u_bus;
  37. if (ubus->monitored) {
  38. /*
  39. * Something is really broken, refuse to go on and
  40. * possibly corrupt ops pointers or worse.
  41. */
  42. printk(KERN_ERR TAG ": bus %d is already monitored\n",
  43. ubus->busnum);
  44. spin_unlock_irqrestore(&mbus->lock, flags);
  45. return;
  46. }
  47. ubus->monitored = 1;
  48. }
  49. mbus->nreaders++;
  50. list_add_tail(&r->r_link, &mbus->r_list);
  51. spin_unlock_irqrestore(&mbus->lock, flags);
  52. kref_get(&mbus->ref);
  53. }
  54. /*
  55. * Unlink reader from the bus.
  56. *
  57. * This is called with mon_lock taken, so we can decrement mbus->ref.
  58. */
  59. void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
  60. {
  61. unsigned long flags;
  62. spin_lock_irqsave(&mbus->lock, flags);
  63. list_del(&r->r_link);
  64. --mbus->nreaders;
  65. if (mbus->nreaders == 0)
  66. mon_stop(mbus);
  67. spin_unlock_irqrestore(&mbus->lock, flags);
  68. kref_put(&mbus->ref, mon_bus_drop);
  69. }
  70. /*
  71. */
  72. static void mon_submit(struct usb_bus *ubus, struct urb *urb)
  73. {
  74. struct mon_bus *mbus;
  75. unsigned long flags;
  76. struct list_head *pos;
  77. struct mon_reader *r;
  78. mbus = ubus->mon_bus;
  79. if (mbus == NULL)
  80. goto out_unlocked;
  81. spin_lock_irqsave(&mbus->lock, flags);
  82. if (mbus->nreaders == 0)
  83. goto out_locked;
  84. list_for_each (pos, &mbus->r_list) {
  85. r = list_entry(pos, struct mon_reader, r_link);
  86. r->rnf_submit(r->r_data, urb);
  87. }
  88. spin_unlock_irqrestore(&mbus->lock, flags);
  89. return;
  90. out_locked:
  91. spin_unlock_irqrestore(&mbus->lock, flags);
  92. out_unlocked:
  93. return;
  94. }
  95. /*
  96. */
  97. static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int err)
  98. {
  99. struct mon_bus *mbus;
  100. mbus = ubus->mon_bus;
  101. if (mbus == NULL)
  102. goto out_unlocked;
  103. /*
  104. * XXX Capture the error code and the 'E' event.
  105. */
  106. return;
  107. out_unlocked:
  108. return;
  109. }
  110. /*
  111. */
  112. static void mon_complete(struct usb_bus *ubus, struct urb *urb)
  113. {
  114. struct mon_bus *mbus;
  115. unsigned long flags;
  116. struct list_head *pos;
  117. struct mon_reader *r;
  118. mbus = ubus->mon_bus;
  119. if (mbus == NULL) {
  120. /*
  121. * This should not happen.
  122. * At this point we do not even know the bus number...
  123. */
  124. printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
  125. urb->pipe);
  126. return;
  127. }
  128. spin_lock_irqsave(&mbus->lock, flags);
  129. list_for_each (pos, &mbus->r_list) {
  130. r = list_entry(pos, struct mon_reader, r_link);
  131. r->rnf_complete(r->r_data, urb);
  132. }
  133. spin_unlock_irqrestore(&mbus->lock, flags);
  134. }
  135. /* int (*unlink_urb) (struct urb *urb, int status); */
  136. /*
  137. * Stop monitoring.
  138. * Obviously this must be well locked, so no need to play with mb's.
  139. */
  140. static void mon_stop(struct mon_bus *mbus)
  141. {
  142. struct usb_bus *ubus = mbus->u_bus;
  143. /*
  144. * A stop can be called for a dissolved mon_bus in case of
  145. * a reader staying across an rmmod foo_hcd.
  146. */
  147. if (ubus != NULL) {
  148. ubus->monitored = 0;
  149. mb();
  150. }
  151. }
  152. /*
  153. * Add a USB bus (usually by a modprobe foo-hcd)
  154. *
  155. * This does not return an error code because the core cannot care less
  156. * if monitoring is not established.
  157. */
  158. static void mon_bus_add(struct usb_bus *ubus)
  159. {
  160. mon_bus_init(mon_dir, ubus);
  161. }
  162. /*
  163. * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
  164. */
  165. static void mon_bus_remove(struct usb_bus *ubus)
  166. {
  167. struct mon_bus *mbus = ubus->mon_bus;
  168. down(&mon_lock);
  169. list_del(&mbus->bus_link);
  170. debugfs_remove(mbus->dent_t);
  171. debugfs_remove(mbus->dent_s);
  172. mon_dissolve(mbus, ubus);
  173. kref_put(&mbus->ref, mon_bus_drop);
  174. up(&mon_lock);
  175. }
  176. static int mon_notify(struct notifier_block *self, unsigned long action,
  177. void *dev)
  178. {
  179. switch (action) {
  180. case USB_BUS_ADD:
  181. mon_bus_add(dev);
  182. break;
  183. case USB_BUS_REMOVE:
  184. mon_bus_remove(dev);
  185. }
  186. return NOTIFY_OK;
  187. }
  188. static struct notifier_block mon_nb = {
  189. .notifier_call = mon_notify,
  190. };
  191. /*
  192. * Ops
  193. */
  194. static struct usb_mon_operations mon_ops_0 = {
  195. .urb_submit = mon_submit,
  196. .urb_submit_error = mon_submit_error,
  197. .urb_complete = mon_complete,
  198. };
  199. /*
  200. * Tear usb_bus and mon_bus apart.
  201. */
  202. static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
  203. {
  204. /*
  205. * Never happens, but...
  206. */
  207. if (ubus->monitored) {
  208. printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
  209. ubus->busnum);
  210. ubus->monitored = 0;
  211. mb();
  212. }
  213. ubus->mon_bus = NULL;
  214. mbus->u_bus = NULL;
  215. mb();
  216. // usb_bus_put(ubus);
  217. }
  218. /*
  219. */
  220. static void mon_bus_drop(struct kref *r)
  221. {
  222. struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
  223. kfree(mbus);
  224. }
  225. /*
  226. * Initialize a bus for us:
  227. * - allocate mon_bus
  228. * - refcount USB bus struct
  229. * - link
  230. */
  231. static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
  232. {
  233. struct dentry *d;
  234. struct mon_bus *mbus;
  235. enum { NAMESZ = 10 };
  236. char name[NAMESZ];
  237. int rc;
  238. if ((mbus = kmalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
  239. goto err_alloc;
  240. memset(mbus, 0, sizeof(struct mon_bus));
  241. kref_init(&mbus->ref);
  242. spin_lock_init(&mbus->lock);
  243. INIT_LIST_HEAD(&mbus->r_list);
  244. /*
  245. * This usb_bus_get here is superfluous, because we receive
  246. * a notification if usb_bus is about to be removed.
  247. */
  248. // usb_bus_get(ubus);
  249. mbus->u_bus = ubus;
  250. ubus->mon_bus = mbus;
  251. rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
  252. if (rc <= 0 || rc >= NAMESZ)
  253. goto err_print_t;
  254. d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_text);
  255. if (d == NULL)
  256. goto err_create_t;
  257. mbus->dent_t = d;
  258. rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
  259. if (rc <= 0 || rc >= NAMESZ)
  260. goto err_print_s;
  261. d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_stat);
  262. if (d == NULL)
  263. goto err_create_s;
  264. mbus->dent_s = d;
  265. down(&mon_lock);
  266. list_add_tail(&mbus->bus_link, &mon_buses);
  267. up(&mon_lock);
  268. return;
  269. err_create_s:
  270. err_print_s:
  271. debugfs_remove(mbus->dent_t);
  272. err_create_t:
  273. err_print_t:
  274. kfree(mbus);
  275. err_alloc:
  276. return;
  277. }
  278. static int __init mon_init(void)
  279. {
  280. struct usb_bus *ubus;
  281. struct dentry *mondir;
  282. mondir = debugfs_create_dir("usbmon", NULL);
  283. if (IS_ERR(mondir)) {
  284. printk(KERN_NOTICE TAG ": debugfs is not available\n");
  285. return -ENODEV;
  286. }
  287. if (mondir == NULL) {
  288. printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
  289. return -ENODEV;
  290. }
  291. mon_dir = mondir;
  292. if (usb_mon_register(&mon_ops_0) != 0) {
  293. printk(KERN_NOTICE TAG ": unable to register with the core\n");
  294. debugfs_remove(mondir);
  295. return -ENODEV;
  296. }
  297. // MOD_INC_USE_COUNT(which_module?);
  298. usb_register_notify(&mon_nb);
  299. down(&usb_bus_list_lock);
  300. list_for_each_entry (ubus, &usb_bus_list, bus_list) {
  301. mon_bus_init(mondir, ubus);
  302. }
  303. up(&usb_bus_list_lock);
  304. return 0;
  305. }
  306. static void __exit mon_exit(void)
  307. {
  308. struct mon_bus *mbus;
  309. struct list_head *p;
  310. usb_unregister_notify(&mon_nb);
  311. usb_mon_deregister();
  312. down(&mon_lock);
  313. while (!list_empty(&mon_buses)) {
  314. p = mon_buses.next;
  315. mbus = list_entry(p, struct mon_bus, bus_link);
  316. list_del(p);
  317. debugfs_remove(mbus->dent_t);
  318. debugfs_remove(mbus->dent_s);
  319. /*
  320. * This never happens, because the open/close paths in
  321. * file level maintain module use counters and so rmmod fails
  322. * before reaching here. However, better be safe...
  323. */
  324. if (mbus->nreaders) {
  325. printk(KERN_ERR TAG
  326. ": Outstanding opens (%d) on usb%d, leaking...\n",
  327. mbus->nreaders, mbus->u_bus->busnum);
  328. atomic_set(&mbus->ref.refcount, 2); /* Force leak */
  329. }
  330. mon_dissolve(mbus, mbus->u_bus);
  331. kref_put(&mbus->ref, mon_bus_drop);
  332. }
  333. up(&mon_lock);
  334. debugfs_remove(mon_dir);
  335. }
  336. module_init(mon_init);
  337. module_exit(mon_exit);
  338. MODULE_LICENSE("GPL");