\documentclass{beamer}
\usetheme{linutronix}
\usepackage{german}
\usepackage[utf8]{inputenc}
\usepackage{pgf}
\usepackage{graphicx}

\title{The Userspace I/O Framework (UIO)}
\author{Hans-Jürgen Koch}
\institute{Linutronix GmbH}

\begin{document}

\maketitle

% ----- Slide "Desperate programmer" ------------------
\begin{frame}
\includegraphics[width=11cm]{images/hjk-desperate.jpg}
\end{frame}

% ----- Slide "Linux device drivers" ------------------
\begin{frame}
\frametitle{Linux device drivers}
\pause
\begin{itemize}
\item Standard devices
\pause
\item Non-standard devices
\end{itemize}
\end{frame}

% ----- Slide "Standard devices" -----------------------
\begin{frame}
\frametitle{Standard devices}
\pause
\begin{itemize}
\item Handled by a kernel subsystem (USB, networking...)
\pause
\item Hardware specific part relatively small
\pause
\item Examples of similar drivers available
\pause
\item Can probably be included in mainline kernel 
\pause
\begin{block}{Standard devices should not be handled by UIO !}
\end{block}
\end{itemize}
\end{frame}

% ----- Slide "Problematic devices" -------------------
\begin{frame}
\frametitle{Problematic devices}
\pause
\begin{itemize}
\item Doesn't fit into a standard kernel subsystem
\pause
\item Almost everything is hardware specific
\pause
\item Driver becomes large, no similar example drivers
\pause
\item Driver often invents its own ioctl based API
\pause
\item Will probably not be included in mainline kernel
\pause
\begin{block}{Drivers for such devices are often buggy and hard to maintain !}
\end{block}
\end{itemize}
\end{frame}

% ----- Slide "In-kernel driver" ------------------
\begin{frame}
\includegraphics[width=11cm]{images/konventioneller-treiber.png}
\end{frame}

% ----- Slide "UIO driver" ------------------
\begin{frame}
\includegraphics[width=11cm]{images/uio-treiber.png}
\end{frame}

% ----- Slide "How UIO works" --------------------------
\begin{frame}
\frametitle{How UIO works}
\pause
\begin{itemize}
\item Device memory can be mapped to userspace
\pause
\item All data exchange is done through this memory
\pause
\item All device control is done through this memory
\pause
\item Interrupts are acknowledged in the kernel...
\pause
\item ...and then handled in userspace
\pause
\begin{block}{A small kernel module is still needed}
\end{block}
\end{itemize}
\end{frame}

% ----- Slide "Kernel: interrupt handler" --------------------------
\begin{frame}[fragile]
\frametitle{Kernel: interrupt handler}
\begin{verbatim}
irqreturn_t my_handler(int irq, struct uio_info *dev_info)
{
    if (IRQ is not caused by my hardware)
        return IRQ_NONE;

    /* Disable interrupt */
    (Perform some register access to silence the IRQ line)

    return IRQ_HANDLED;
}
\end{verbatim}
\end{frame}

% ----- Slide "Kernel: probe() function" --------------------------
\begin{frame}[fragile]
\frametitle{Kernel: probe() function}
\begin{verbatim}
int pci_probe(struct pci_dev *dev,
              const struct pci_device_id *id)
{
    ...
    allocate and fill struct uio_info
    ...
    call uio_register_device()    
}
\end{verbatim}
\end{frame}

% ----- Slide "Kernel: struct uio_info" --------------------------
\begin{frame}[fragile]
\frametitle{Kernel: struct uio\_info}
\begin{verbatim}
struct uio_info {
    struct uio_device *uio_dev;
    char *name;
    char *version;
    struct uio_mem mem[MAX_UIO_MAPS];
    long irq;
    unsigned long irq_flags;
    void *priv;
    irqreturn_t (*handler)(int irq, struct uio_info *dev_info);
    int (*mmap)(struct uio_info *info, struct vm_area_struct *vma);
    int (*open)(struct uio_info *info, struct inode *inode);
    int (*release)(struct uio_info *info, struct inode *inode);
};
\end{verbatim}
\end{frame}

% ----- Slide "Kernel: struct uio_mem" --------------------------
\begin{frame}[fragile]
\frametitle{Kernel: struct uio\_mem}
\begin{verbatim}
struct uio_mem {
    unsigned long addr;
    unsigned long size;
    int memtype;
    void __iomem *internal_addr;
    struct uio_map *map;
};
\end{verbatim}
\end{frame}

% ----- Slide "Userspace part of driver" --------------
\begin{frame}
\frametitle{Userspace: Overview}
\pause
\begin{itemize}
\item Find device in sysfs (/sys/class/uio/...)
\pause
\item Get device information from sysfs
\pause
\item Open /dev/uioX
\pause
\item Use mmap() to get a pointer to the devices's memory
\pause
\item Initialize device, enable interrupt generation
\pause
\item Perform a blocking read() to wait for interrupts
\end{itemize}
\end{frame}

% ----- Slide "Hello UIO world" --------------
\begin{frame}[fragile]
\frametitle{Hello UIO world}
\pause
\begin{verbatim}
int32_t irq_cnt;
int fd = open("/dev/uio0", O_RDWR);
u16 *mem = mmap(..., n*getpagesize());
mem[...] = ...
while (read(fd, &irq_cnt, 4)) {
    printf("irq count = %d\n", irq_cnt);
}
\end{verbatim}
\end{frame}

% ----- Slide "Performance considerations" --------------
\begin{frame}
\frametitle{Performance considerations}
\pause
\begin{itemize}
\item Interrupt latency
\pause
\item Memory/register access
\end{itemize}
\end{frame}

% ----- Slide "ioctl vs. UIO" ------------------
\begin{frame}
\includegraphics[width=10cm]{images/ioctl-vs-uio.png}
\end{frame}

% ----- Slide "Publish your driver!" --------------
\begin{frame}
\frametitle{Publish your driver!}
\pause
\begin{itemize}
\item Get rid of maintenance work!
\pause
\item Send your patch to the UIO maintainers
\pause
\item Cc: linux-kernel@vger.kernel.org
\pause
\item Publish your userspace code, if possible
\pause
\item Make it LGPL, if possible
\pause
\item An open driver helps selling your hardware!
\end{itemize}
\end{frame}

% ----- Slide "Greg: Support all hardware on earth" ---
\begin{frame}
\includegraphics[width=11cm]{images/greg-all-hardware.jpg}
\end{frame}

% ----- Slide "Greg: All drivers in kernel.org tree" --
\begin{frame}
\includegraphics[width=11cm]{images/greg-kernel-org.jpg}
\end{frame}

% ----- Slide "Legal issues" --------------------------
\begin{frame}
\frametitle{Legal issues}
\pause
\begin{itemize}
\item Userspace code can be under any licence
\pause
\item Kernel module MUST be GPL
\pause
\item Avoid trickery and gray areas
\end{itemize}
\end{frame}

% ----- Slide "Tools" --------------------------
\begin{frame}
\frametitle{Tools}
\pause
\begin{itemize}
\item lsuio
\pause
\item TODO: libuio (nearly ready)
\end{itemize}
\end{frame}

% ----- Slide "OSADL" --------------------------
\begin{frame}
\frametitle{OSADL}
\pause
\begin{itemize}
\item Open Source Automation Development Lab
\pause
\item http://www.osadl.org
\pause
\item Hosts some UIO code
\end{itemize}
\end{frame}

% ----- Slide "Future" --------------------------
\begin{frame}
\frametitle{Future}
\pause
\begin{itemize}
\item DMA support
\pause
\item get UIO tools and libuio into Linux Distributions
\end{itemize}
\end{frame}

% ----- Slide "Thanks for listening!" ----------
\begin{frame}
\frametitle{Thanks for listening!}
\pause
\begin{itemize}
\item Questions ?
\end{itemize}
\end{frame}

\end{document}
