mon_main.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/usb/hcd.h>
  12. #include <linux/slab.h>
  13. #include <linux/notifier.h>
  14. #include <linux/mutex.h>
  15. #include "usb_mon.h"
  16. static void mon_stop(struct mon_bus *mbus);
  17. static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
  18. static void mon_bus_drop(struct kref *r);
  19. static void mon_bus_init(struct usb_bus *ubus);
  20. DEFINE_MUTEX(mon_lock);
  21. struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
  22. static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
  23. /*
  24. * Link a reader into the bus.
  25. *
  26. * This must be called with mon_lock taken because of mbus->ref.
  27. */
  28. void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
  29. {
  30. unsigned long flags;
  31. struct list_head *p;
  32. spin_lock_irqsave(&mbus->lock, flags);
  33. if (mbus->nreaders == 0) {
  34. if (mbus == &mon_bus0) {
  35. list_for_each (p, &mon_buses) {
  36. struct mon_bus *m1;
  37. m1 = list_entry(p, struct mon_bus, bus_link);
  38. m1->u_bus->monitored = 1;
  39. }
  40. } else {
  41. mbus->u_bus->monitored = 1;
  42. }
  43. }
  44. mbus->nreaders++;
  45. list_add_tail(&r->r_link, &mbus->r_list);
  46. spin_unlock_irqrestore(&mbus->lock, flags);
  47. kref_get(&mbus->ref);
  48. }
  49. /*
  50. * Unlink reader from the bus.
  51. *
  52. * This is called with mon_lock taken, so we can decrement mbus->ref.
  53. */
  54. void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
  55. {
  56. unsigned long flags;
  57. spin_lock_irqsave(&mbus->lock, flags);
  58. list_del(&r->r_link);
  59. --mbus->nreaders;
  60. if (mbus->nreaders == 0)
  61. mon_stop(mbus);
  62. spin_unlock_irqrestore(&mbus->lock, flags);
  63. kref_put(&mbus->ref, mon_bus_drop);
  64. }
  65. /*
  66. */
  67. static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
  68. {
  69. unsigned long flags;
  70. struct list_head *pos;
  71. struct mon_reader *r;
  72. spin_lock_irqsave(&mbus->lock, flags);
  73. mbus->cnt_events++;
  74. list_for_each (pos, &mbus->r_list) {
  75. r = list_entry(pos, struct mon_reader, r_link);
  76. r->rnf_submit(r->r_data, urb);
  77. }
  78. spin_unlock_irqrestore(&mbus->lock, flags);
  79. }
  80. static void mon_submit(struct usb_bus *ubus, struct urb *urb)
  81. {
  82. struct mon_bus *mbus;
  83. if ((mbus = ubus->mon_bus) != NULL)
  84. mon_bus_submit(mbus, urb);
  85. mon_bus_submit(&mon_bus0, urb);
  86. }
  87. /*
  88. */
  89. static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
  90. {
  91. unsigned long flags;
  92. struct list_head *pos;
  93. struct mon_reader *r;
  94. spin_lock_irqsave(&mbus->lock, flags);
  95. mbus->cnt_events++;
  96. list_for_each (pos, &mbus->r_list) {
  97. r = list_entry(pos, struct mon_reader, r_link);
  98. r->rnf_error(r->r_data, urb, error);
  99. }
  100. spin_unlock_irqrestore(&mbus->lock, flags);
  101. }
  102. static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
  103. {
  104. struct mon_bus *mbus;
  105. if ((mbus = ubus->mon_bus) != NULL)
  106. mon_bus_submit_error(mbus, urb, error);
  107. mon_bus_submit_error(&mon_bus0, urb, error);
  108. }
  109. /*
  110. */
  111. static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status)
  112. {
  113. unsigned long flags;
  114. struct list_head *pos;
  115. struct mon_reader *r;
  116. spin_lock_irqsave(&mbus->lock, flags);
  117. mbus->cnt_events++;
  118. list_for_each (pos, &mbus->r_list) {
  119. r = list_entry(pos, struct mon_reader, r_link);
  120. r->rnf_complete(r->r_data, urb, status);
  121. }
  122. spin_unlock_irqrestore(&mbus->lock, flags);
  123. }
  124. static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
  125. {
  126. struct mon_bus *mbus;
  127. if ((mbus = ubus->mon_bus) != NULL)
  128. mon_bus_complete(mbus, urb, status);
  129. mon_bus_complete(&mon_bus0, urb, status);
  130. }
  131. /* int (*unlink_urb) (struct urb *urb, int status); */
  132. /*
  133. * Stop monitoring.
  134. */
  135. static void mon_stop(struct mon_bus *mbus)
  136. {
  137. struct usb_bus *ubus;
  138. struct list_head *p;
  139. if (mbus == &mon_bus0) {
  140. list_for_each (p, &mon_buses) {
  141. mbus = list_entry(p, struct mon_bus, bus_link);
  142. /*
  143. * We do not change nreaders here, so rely on mon_lock.
  144. */
  145. if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
  146. ubus->monitored = 0;
  147. }
  148. } else {
  149. /*
  150. * A stop can be called for a dissolved mon_bus in case of
  151. * a reader staying across an rmmod foo_hcd, so test ->u_bus.
  152. */
  153. if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
  154. ubus->monitored = 0;
  155. mb();
  156. }
  157. }
  158. }
  159. /*
  160. * Add a USB bus (usually by a modprobe foo-hcd)
  161. *
  162. * This does not return an error code because the core cannot care less
  163. * if monitoring is not established.
  164. */
  165. static void mon_bus_add(struct usb_bus *ubus)
  166. {
  167. mon_bus_init(ubus);
  168. mutex_lock(&mon_lock);
  169. if (mon_bus0.nreaders != 0)
  170. ubus->monitored = 1;
  171. mutex_unlock(&mon_lock);
  172. }
  173. /*
  174. * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
  175. */
  176. static void mon_bus_remove(struct usb_bus *ubus)
  177. {
  178. struct mon_bus *mbus = ubus->mon_bus;
  179. mutex_lock(&mon_lock);
  180. list_del(&mbus->bus_link);
  181. if (mbus->text_inited)
  182. mon_text_del(mbus);
  183. if (mbus->bin_inited)
  184. mon_bin_del(mbus);
  185. mon_dissolve(mbus, ubus);
  186. kref_put(&mbus->ref, mon_bus_drop);
  187. mutex_unlock(&mon_lock);
  188. }
  189. static int mon_notify(struct notifier_block *self, unsigned long action,
  190. void *dev)
  191. {
  192. switch (action) {
  193. case USB_BUS_ADD:
  194. mon_bus_add(dev);
  195. break;
  196. case USB_BUS_REMOVE:
  197. mon_bus_remove(dev);
  198. }
  199. return NOTIFY_OK;
  200. }
  201. static struct notifier_block mon_nb = {
  202. .notifier_call = mon_notify,
  203. };
  204. /*
  205. * Ops
  206. */
  207. static struct usb_mon_operations mon_ops_0 = {
  208. .urb_submit = mon_submit,
  209. .urb_submit_error = mon_submit_error,
  210. .urb_complete = mon_complete,
  211. };
  212. /*
  213. * Tear usb_bus and mon_bus apart.
  214. */
  215. static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
  216. {
  217. if (ubus->monitored) {
  218. ubus->monitored = 0;
  219. mb();
  220. }
  221. ubus->mon_bus = NULL;
  222. mbus->u_bus = NULL;
  223. mb();
  224. /* We want synchronize_irq() here, but that needs an argument. */
  225. }
  226. /*
  227. */
  228. static void mon_bus_drop(struct kref *r)
  229. {
  230. struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
  231. kfree(mbus);
  232. }
  233. /*
  234. * Initialize a bus for us:
  235. * - allocate mon_bus
  236. * - refcount USB bus struct
  237. * - link
  238. */
  239. static void mon_bus_init(struct usb_bus *ubus)
  240. {
  241. struct mon_bus *mbus;
  242. if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
  243. goto err_alloc;
  244. kref_init(&mbus->ref);
  245. spin_lock_init(&mbus->lock);
  246. INIT_LIST_HEAD(&mbus->r_list);
  247. /*
  248. * We don't need to take a reference to ubus, because we receive
  249. * a notification if the bus is about to be removed.
  250. */
  251. mbus->u_bus = ubus;
  252. ubus->mon_bus = mbus;
  253. mbus->text_inited = mon_text_add(mbus, ubus);
  254. mbus->bin_inited = mon_bin_add(mbus, ubus);
  255. mutex_lock(&mon_lock);
  256. list_add_tail(&mbus->bus_link, &mon_buses);
  257. mutex_unlock(&mon_lock);
  258. return;
  259. err_alloc:
  260. return;
  261. }
  262. static void mon_bus0_init(void)
  263. {
  264. struct mon_bus *mbus = &mon_bus0;
  265. kref_init(&mbus->ref);
  266. spin_lock_init(&mbus->lock);
  267. INIT_LIST_HEAD(&mbus->r_list);
  268. mbus->text_inited = mon_text_add(mbus, NULL);
  269. mbus->bin_inited = mon_bin_add(mbus, NULL);
  270. }
  271. /*
  272. * Search a USB bus by number. Notice that USB bus numbers start from one,
  273. * which we may later use to identify "all" with zero.
  274. *
  275. * This function must be called with mon_lock held.
  276. *
  277. * This is obviously inefficient and may be revised in the future.
  278. */
  279. struct mon_bus *mon_bus_lookup(unsigned int num)
  280. {
  281. struct list_head *p;
  282. struct mon_bus *mbus;
  283. if (num == 0) {
  284. return &mon_bus0;
  285. }
  286. list_for_each (p, &mon_buses) {
  287. mbus = list_entry(p, struct mon_bus, bus_link);
  288. if (mbus->u_bus->busnum == num) {
  289. return mbus;
  290. }
  291. }
  292. return NULL;
  293. }
  294. static int __init mon_init(void)
  295. {
  296. struct usb_bus *ubus;
  297. int rc;
  298. if ((rc = mon_text_init()) != 0)
  299. goto err_text;
  300. if ((rc = mon_bin_init()) != 0)
  301. goto err_bin;
  302. mon_bus0_init();
  303. if (usb_mon_register(&mon_ops_0) != 0) {
  304. printk(KERN_NOTICE TAG ": unable to register with the core\n");
  305. rc = -ENODEV;
  306. goto err_reg;
  307. }
  308. // MOD_INC_USE_COUNT(which_module?);
  309. mutex_lock(&usb_bus_list_lock);
  310. list_for_each_entry (ubus, &usb_bus_list, bus_list) {
  311. mon_bus_init(ubus);
  312. }
  313. usb_register_notify(&mon_nb);
  314. mutex_unlock(&usb_bus_list_lock);
  315. return 0;
  316. err_reg:
  317. mon_bin_exit();
  318. err_bin:
  319. mon_text_exit();
  320. err_text:
  321. return rc;
  322. }
  323. static void __exit mon_exit(void)
  324. {
  325. struct mon_bus *mbus;
  326. struct list_head *p;
  327. usb_unregister_notify(&mon_nb);
  328. usb_mon_deregister();
  329. mutex_lock(&mon_lock);
  330. while (!list_empty(&mon_buses)) {
  331. p = mon_buses.next;
  332. mbus = list_entry(p, struct mon_bus, bus_link);
  333. list_del(p);
  334. if (mbus->text_inited)
  335. mon_text_del(mbus);
  336. if (mbus->bin_inited)
  337. mon_bin_del(mbus);
  338. /*
  339. * This never happens, because the open/close paths in
  340. * file level maintain module use counters and so rmmod fails
  341. * before reaching here. However, better be safe...
  342. */
  343. if (mbus->nreaders) {
  344. printk(KERN_ERR TAG
  345. ": Outstanding opens (%d) on usb%d, leaking...\n",
  346. mbus->nreaders, mbus->u_bus->busnum);
  347. atomic_set(&mbus->ref.refcount, 2); /* Force leak */
  348. }
  349. mon_dissolve(mbus, mbus->u_bus);
  350. kref_put(&mbus->ref, mon_bus_drop);
  351. }
  352. mbus = &mon_bus0;
  353. if (mbus->text_inited)
  354. mon_text_del(mbus);
  355. if (mbus->bin_inited)
  356. mon_bin_del(mbus);
  357. mutex_unlock(&mon_lock);
  358. mon_text_exit();
  359. mon_bin_exit();
  360. }
  361. module_init(mon_init);
  362. module_exit(mon_exit);
  363. MODULE_LICENSE("GPL");