Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * The USB Monitor, inspired by Dave Harding's USBMon.
0004  *
0005  * mon_main.c: Main file, module initiation and exit, registrations, etc.
0006  *
0007  * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com)
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/usb.h>
0013 #include <linux/usb/hcd.h>
0014 #include <linux/slab.h>
0015 #include <linux/notifier.h>
0016 #include <linux/mutex.h>
0017 
0018 #include "usb_mon.h"
0019 
0020 
0021 static void mon_stop(struct mon_bus *mbus);
0022 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
0023 static void mon_bus_drop(struct kref *r);
0024 static void mon_bus_init(struct usb_bus *ubus);
0025 
0026 DEFINE_MUTEX(mon_lock);
0027 
0028 struct mon_bus mon_bus0;        /* Pseudo bus meaning "all buses" */
0029 static LIST_HEAD(mon_buses);        /* All buses we know: struct mon_bus */
0030 
0031 /*
0032  * Link a reader into the bus.
0033  *
0034  * This must be called with mon_lock taken because of mbus->ref.
0035  */
0036 void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
0037 {
0038     unsigned long flags;
0039     struct list_head *p;
0040 
0041     spin_lock_irqsave(&mbus->lock, flags);
0042     if (mbus->nreaders == 0) {
0043         if (mbus == &mon_bus0) {
0044             list_for_each (p, &mon_buses) {
0045                 struct mon_bus *m1;
0046                 m1 = list_entry(p, struct mon_bus, bus_link);
0047                 m1->u_bus->monitored = 1;
0048             }
0049         } else {
0050             mbus->u_bus->monitored = 1;
0051         }
0052     }
0053     mbus->nreaders++;
0054     list_add_tail(&r->r_link, &mbus->r_list);
0055     spin_unlock_irqrestore(&mbus->lock, flags);
0056 
0057     kref_get(&mbus->ref);
0058 }
0059 
0060 /*
0061  * Unlink reader from the bus.
0062  *
0063  * This is called with mon_lock taken, so we can decrement mbus->ref.
0064  */
0065 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
0066 {
0067     unsigned long flags;
0068 
0069     spin_lock_irqsave(&mbus->lock, flags);
0070     list_del(&r->r_link);
0071     --mbus->nreaders;
0072     if (mbus->nreaders == 0)
0073         mon_stop(mbus);
0074     spin_unlock_irqrestore(&mbus->lock, flags);
0075 
0076     kref_put(&mbus->ref, mon_bus_drop);
0077 }
0078 
0079 /*
0080  */
0081 static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
0082 {
0083     unsigned long flags;
0084     struct list_head *pos;
0085     struct mon_reader *r;
0086 
0087     spin_lock_irqsave(&mbus->lock, flags);
0088     mbus->cnt_events++;
0089     list_for_each (pos, &mbus->r_list) {
0090         r = list_entry(pos, struct mon_reader, r_link);
0091         r->rnf_submit(r->r_data, urb);
0092     }
0093     spin_unlock_irqrestore(&mbus->lock, flags);
0094 }
0095 
0096 static void mon_submit(struct usb_bus *ubus, struct urb *urb)
0097 {
0098     struct mon_bus *mbus;
0099 
0100     mbus = ubus->mon_bus;
0101     if (mbus != NULL)
0102         mon_bus_submit(mbus, urb);
0103     mon_bus_submit(&mon_bus0, urb);
0104 }
0105 
0106 /*
0107  */
0108 static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
0109 {
0110     unsigned long flags;
0111     struct list_head *pos;
0112     struct mon_reader *r;
0113 
0114     spin_lock_irqsave(&mbus->lock, flags);
0115     mbus->cnt_events++;
0116     list_for_each (pos, &mbus->r_list) {
0117         r = list_entry(pos, struct mon_reader, r_link);
0118         r->rnf_error(r->r_data, urb, error);
0119     }
0120     spin_unlock_irqrestore(&mbus->lock, flags);
0121 }
0122 
0123 static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
0124 {
0125     struct mon_bus *mbus;
0126 
0127     mbus = ubus->mon_bus;
0128     if (mbus != NULL)
0129         mon_bus_submit_error(mbus, urb, error);
0130     mon_bus_submit_error(&mon_bus0, urb, error);
0131 }
0132 
0133 /*
0134  */
0135 static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status)
0136 {
0137     unsigned long flags;
0138     struct list_head *pos;
0139     struct mon_reader *r;
0140 
0141     spin_lock_irqsave(&mbus->lock, flags);
0142     mbus->cnt_events++;
0143     list_for_each (pos, &mbus->r_list) {
0144         r = list_entry(pos, struct mon_reader, r_link);
0145         r->rnf_complete(r->r_data, urb, status);
0146     }
0147     spin_unlock_irqrestore(&mbus->lock, flags);
0148 }
0149 
0150 static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
0151 {
0152     struct mon_bus *mbus;
0153 
0154     mbus = ubus->mon_bus;
0155     if (mbus != NULL)
0156         mon_bus_complete(mbus, urb, status);
0157     mon_bus_complete(&mon_bus0, urb, status);
0158 }
0159 
0160 /* int (*unlink_urb) (struct urb *urb, int status); */
0161 
0162 /*
0163  * Stop monitoring.
0164  */
0165 static void mon_stop(struct mon_bus *mbus)
0166 {
0167     struct usb_bus *ubus;
0168     struct list_head *p;
0169 
0170     if (mbus == &mon_bus0) {
0171         list_for_each (p, &mon_buses) {
0172             mbus = list_entry(p, struct mon_bus, bus_link);
0173             /*
0174              * We do not change nreaders here, so rely on mon_lock.
0175              */
0176             if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
0177                 ubus->monitored = 0;
0178         }
0179     } else {
0180         /*
0181          * A stop can be called for a dissolved mon_bus in case of
0182          * a reader staying across an rmmod foo_hcd, so test ->u_bus.
0183          */
0184         if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
0185             ubus->monitored = 0;
0186             mb();
0187         }
0188     }
0189 }
0190 
0191 /*
0192  * Add a USB bus (usually by a modprobe foo-hcd)
0193  *
0194  * This does not return an error code because the core cannot care less
0195  * if monitoring is not established.
0196  */
0197 static void mon_bus_add(struct usb_bus *ubus)
0198 {
0199     mon_bus_init(ubus);
0200     mutex_lock(&mon_lock);
0201     if (mon_bus0.nreaders != 0)
0202         ubus->monitored = 1;
0203     mutex_unlock(&mon_lock);
0204 }
0205 
0206 /*
0207  * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
0208  */
0209 static void mon_bus_remove(struct usb_bus *ubus)
0210 {
0211     struct mon_bus *mbus = ubus->mon_bus;
0212 
0213     mutex_lock(&mon_lock);
0214     list_del(&mbus->bus_link);
0215     if (mbus->text_inited)
0216         mon_text_del(mbus);
0217     if (mbus->bin_inited)
0218         mon_bin_del(mbus);
0219 
0220     mon_dissolve(mbus, ubus);
0221     kref_put(&mbus->ref, mon_bus_drop);
0222     mutex_unlock(&mon_lock);
0223 }
0224 
0225 static int mon_notify(struct notifier_block *self, unsigned long action,
0226               void *dev)
0227 {
0228     switch (action) {
0229     case USB_BUS_ADD:
0230         mon_bus_add(dev);
0231         break;
0232     case USB_BUS_REMOVE:
0233         mon_bus_remove(dev);
0234     }
0235     return NOTIFY_OK;
0236 }
0237 
0238 static struct notifier_block mon_nb = {
0239     .notifier_call =    mon_notify,
0240 };
0241 
0242 /*
0243  * Ops
0244  */
0245 static const struct usb_mon_operations mon_ops_0 = {
0246     .urb_submit =   mon_submit,
0247     .urb_submit_error = mon_submit_error,
0248     .urb_complete = mon_complete,
0249 };
0250 
0251 /*
0252  * Tear usb_bus and mon_bus apart.
0253  */
0254 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
0255 {
0256 
0257     if (ubus->monitored) {
0258         ubus->monitored = 0;
0259         mb();
0260     }
0261 
0262     ubus->mon_bus = NULL;
0263     mbus->u_bus = NULL;
0264     mb();
0265 
0266     /* We want synchronize_irq() here, but that needs an argument. */
0267 }
0268 
0269 /*
0270  */
0271 static void mon_bus_drop(struct kref *r)
0272 {
0273     struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
0274     kfree(mbus);
0275 }
0276 
0277 /*
0278  * Initialize a bus for us:
0279  *  - allocate mon_bus
0280  *  - refcount USB bus struct
0281  *  - link
0282  */
0283 static void mon_bus_init(struct usb_bus *ubus)
0284 {
0285     struct mon_bus *mbus;
0286 
0287     mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL);
0288     if (mbus == NULL)
0289         goto err_alloc;
0290     kref_init(&mbus->ref);
0291     spin_lock_init(&mbus->lock);
0292     INIT_LIST_HEAD(&mbus->r_list);
0293 
0294     /*
0295      * We don't need to take a reference to ubus, because we receive
0296      * a notification if the bus is about to be removed.
0297      */
0298     mbus->u_bus = ubus;
0299     ubus->mon_bus = mbus;
0300 
0301     mbus->text_inited = mon_text_add(mbus, ubus);
0302     mbus->bin_inited = mon_bin_add(mbus, ubus);
0303 
0304     mutex_lock(&mon_lock);
0305     list_add_tail(&mbus->bus_link, &mon_buses);
0306     mutex_unlock(&mon_lock);
0307     return;
0308 
0309 err_alloc:
0310     return;
0311 }
0312 
0313 static void mon_bus0_init(void)
0314 {
0315     struct mon_bus *mbus = &mon_bus0;
0316 
0317     kref_init(&mbus->ref);
0318     spin_lock_init(&mbus->lock);
0319     INIT_LIST_HEAD(&mbus->r_list);
0320 
0321     mbus->text_inited = mon_text_add(mbus, NULL);
0322     mbus->bin_inited = mon_bin_add(mbus, NULL);
0323 }
0324 
0325 /*
0326  * Search a USB bus by number. Notice that USB bus numbers start from one,
0327  * which we may later use to identify "all" with zero.
0328  *
0329  * This function must be called with mon_lock held.
0330  *
0331  * This is obviously inefficient and may be revised in the future.
0332  */
0333 struct mon_bus *mon_bus_lookup(unsigned int num)
0334 {
0335     struct list_head *p;
0336     struct mon_bus *mbus;
0337 
0338     if (num == 0) {
0339         return &mon_bus0;
0340     }
0341     list_for_each (p, &mon_buses) {
0342         mbus = list_entry(p, struct mon_bus, bus_link);
0343         if (mbus->u_bus->busnum == num) {
0344             return mbus;
0345         }
0346     }
0347     return NULL;
0348 }
0349 
0350 static int __init mon_init(void)
0351 {
0352     struct usb_bus *ubus;
0353     int rc, id;
0354 
0355     if ((rc = mon_text_init()) != 0)
0356         goto err_text;
0357     if ((rc = mon_bin_init()) != 0)
0358         goto err_bin;
0359 
0360     mon_bus0_init();
0361 
0362     if (usb_mon_register(&mon_ops_0) != 0) {
0363         printk(KERN_NOTICE TAG ": unable to register with the core\n");
0364         rc = -ENODEV;
0365         goto err_reg;
0366     }
0367     // MOD_INC_USE_COUNT(which_module?);
0368 
0369     mutex_lock(&usb_bus_idr_lock);
0370     idr_for_each_entry(&usb_bus_idr, ubus, id)
0371         mon_bus_init(ubus);
0372     usb_register_notify(&mon_nb);
0373     mutex_unlock(&usb_bus_idr_lock);
0374     return 0;
0375 
0376 err_reg:
0377     mon_bin_exit();
0378 err_bin:
0379     mon_text_exit();
0380 err_text:
0381     return rc;
0382 }
0383 
0384 static void __exit mon_exit(void)
0385 {
0386     struct mon_bus *mbus;
0387     struct list_head *p;
0388 
0389     usb_unregister_notify(&mon_nb);
0390     usb_mon_deregister();
0391 
0392     mutex_lock(&mon_lock);
0393 
0394     while (!list_empty(&mon_buses)) {
0395         p = mon_buses.next;
0396         mbus = list_entry(p, struct mon_bus, bus_link);
0397         list_del(p);
0398 
0399         if (mbus->text_inited)
0400             mon_text_del(mbus);
0401         if (mbus->bin_inited)
0402             mon_bin_del(mbus);
0403 
0404         /*
0405          * This never happens, because the open/close paths in
0406          * file level maintain module use counters and so rmmod fails
0407          * before reaching here. However, better be safe...
0408          */
0409         if (mbus->nreaders) {
0410             printk(KERN_ERR TAG
0411                 ": Outstanding opens (%d) on usb%d, leaking...\n",
0412                 mbus->nreaders, mbus->u_bus->busnum);
0413             kref_get(&mbus->ref); /* Force leak */
0414         }
0415 
0416         mon_dissolve(mbus, mbus->u_bus);
0417         kref_put(&mbus->ref, mon_bus_drop);
0418     }
0419 
0420     mbus = &mon_bus0;
0421     if (mbus->text_inited)
0422         mon_text_del(mbus);
0423     if (mbus->bin_inited)
0424         mon_bin_del(mbus);
0425 
0426     mutex_unlock(&mon_lock);
0427 
0428     mon_text_exit();
0429     mon_bin_exit();
0430 }
0431 
0432 module_init(mon_init);
0433 module_exit(mon_exit);
0434 
0435 MODULE_LICENSE("GPL");