This chapter is the first of several case studies that’ll guide you through a real-world device driver. The purpose of these case studies is to expose you to genuine driver code—warts and all—and to consolidate the information presented in earlier chapters.
In this chapter, we’ll go through nmdm(4)
, the virtual null modem terminal driver. This driver creates two tty(4)
devices that are connected by a virtual null modem cable. In other words, the output of one tty(4)
device is the input for the other tty(4)
device, and vice versa. I chose to profile nmdm(4)
because it uses event handlers, callouts, and taskqueues, all of which were described, but not demonstrated, in Chapter 5.
Before I can walk you through nmdm(4)
, you’ll need to grok the following functions:
#include <sys/tty.h> struct tty * tty_alloc_mutex(struct ttydevsw *tsw, void *softc, struct mtx *mtx); void tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...); void * tty_softc(struct tty *tp);
The tty_alloc_mutex
function creates a TTY device. The tsw
argument expects a pointer to a TTY device switch table, which is like a character device switch table, but for TTY devices. The softc
argument is the software context (or instance variables) for the TTY device. The mtx
argument specifies the mutex that’ll protect the TTY device.
At some point in the near future, the tty_alloc_mutex
function is supposed to be deprecated and removed.
The tty_makedev
function creates a TTY device node under /dev. The tp
argument expects a pointer to a TTY device (for example, the return value from tty_alloc_mutex
). The cred argument is the credentials for the device node. If cred
is NULL
, UID_ROOT
and GID_WHEEL
are used. The fmt
argument specifies the name for the device node.
The tty_softc
function returns the software context of the TTY device tp
.
3.145.69.255