mon_main.c 8.0 KB

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