blob: 6b36897ca151998ab28f07c12f0e9baef9e372fe [file] [log] [blame]
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001/*
2 * All the USB notify logic
3 *
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
5 *
6 * notifier functions originally based on those in kernel/sys.c
7 * but fixed up to not be so broken.
8 *
9 */
10
11
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070012#include <linux/kernel.h>
13#include <linux/notifier.h>
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070014#include <linux/usb.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010015#include <linux/mutex.h>
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070016#include "usb.h"
17
Alan Sterne041c682006-03-27 01:16:30 -080018static BLOCKING_NOTIFIER_HEAD(usb_notifier_list);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070019
20/**
21 * usb_register_notify - register a notifier callback whenever a usb change happens
22 * @nb: pointer to the notifier block for the callback events.
23 *
24 * These changes are either USB devices or busses being added or removed.
25 */
26void usb_register_notify(struct notifier_block *nb)
27{
Alan Sterne041c682006-03-27 01:16:30 -080028 blocking_notifier_chain_register(&usb_notifier_list, nb);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070029}
30EXPORT_SYMBOL_GPL(usb_register_notify);
31
32/**
33 * usb_unregister_notify - unregister a notifier callback
34 * @nb: pointer to the notifier block for the callback events.
35 *
36 * usb_register_notifier() must have been previously called for this function
37 * to work properly.
38 */
39void usb_unregister_notify(struct notifier_block *nb)
40{
Alan Sterne041c682006-03-27 01:16:30 -080041 blocking_notifier_chain_unregister(&usb_notifier_list, nb);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070042}
43EXPORT_SYMBOL_GPL(usb_unregister_notify);
44
45
46void usb_notify_add_device(struct usb_device *udev)
47{
Alan Sterne041c682006-03-27 01:16:30 -080048 blocking_notifier_call_chain(&usb_notifier_list, USB_DEVICE_ADD, udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070049}
50
51void usb_notify_remove_device(struct usb_device *udev)
52{
Alan Stern4a2a8a22006-07-01 22:05:01 -040053 /* Protect against simultaneous usbfs open */
54 mutex_lock(&usbfs_mutex);
Alan Sterne041c682006-03-27 01:16:30 -080055 blocking_notifier_call_chain(&usb_notifier_list,
56 USB_DEVICE_REMOVE, udev);
Alan Stern4a2a8a22006-07-01 22:05:01 -040057 mutex_unlock(&usbfs_mutex);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070058}
59
60void usb_notify_add_bus(struct usb_bus *ubus)
61{
Alan Sterne041c682006-03-27 01:16:30 -080062 blocking_notifier_call_chain(&usb_notifier_list, USB_BUS_ADD, ubus);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070063}
64
65void usb_notify_remove_bus(struct usb_bus *ubus)
66{
Alan Sterne041c682006-03-27 01:16:30 -080067 blocking_notifier_call_chain(&usb_notifier_list, USB_BUS_REMOVE, ubus);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070068}