mon_main.c 9.1 KB

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