mon_main.c 7.9 KB

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