\section{Hotplug}

The driver model supports dynamic addition and removal of all object
types through their register() and unregister() functions. This
includes support for the dynamic addition and removal of devices on
bus types that electrically support it. This commonly referred to as
the 'hotplug' capability or feature. 

When a device's physical or logical status changes, the driver model
executes the user mode program in /proc/sys/kernel/hotplug. This
is set to '/sbin/hotplug' by default. This user mode agent may invoke
user-defined policy for each action or each device, including: 

\begin{itemize}
\begin{footnotesize}
\item Loading or unloading a driver module.
\item Configuring a device.
\item Executing a script or application.
\end{footnotesize}
\end{itemize}

The driver model calls one of two internal functions, selected by the
source of the event.  


\begin{table}
\begin{footnotesize}
\begin{tabularx}{\linewidth}{|X|X|X|} \hline
\textit{Name}	& \textit{Return Type} & \textit{Description} \\ \hline \hline


dev\_hotplug(
  struct device *dev, 
  const char *action
)
& int
& Called by driver model core when device is registered or
	unregistered with core. 
\\\hline

class\_hotplug(
  struct device *dev, 
  const char *action
)
& int
& Called by driver model core when device is registered or
	unregistered with class. 
\\\hline

\end{tabularx} 
\end{footnotesize}
\caption{\footnotesize{struct subsystem Programming Interface.}}
\end{table}


The action for both functions is one of "ADD" or "REMOVE." These
actions are set in environment variables for the the hotplug program. The
driver model sets the following parameters:

\begin{itemize}
\begin{footnotesize}
\item HOME, set to '/'
\item PATH, set to '/sbin:/bin:/usr/sbin:/usr/bin'
\item ACTION, set to 'ADD' or 'REMOVE'.
\item DEVPATH, set to the sysfs path to the device's directory.
\end{footnotesize}
\end{itemize}

Additionally, the driver model defines a 'hotplug' method for struct bus\_type and struct device\_class. 

\begin{footnotesize}
\begin{verbatim}
int(*hotplug) (struct device *dev, char **envp, 
               int num_envp, char *buffer, int buffer_size);
\end{verbatim}
\end{footnotesize}

This method is called immediately before the user mode program is
executed. If this method is defined, the bus or class driver may
supply additional environment variables that bus or class specific
policy may use in user space. 

