Linux Device Drivers (3rd Edition) [Electronic resources]

Jonathan Corbet, Greg Kroah-Hartman, Alessandro Rubini

نسخه متنی -صفحه : 202/ 168
نمايش فراداده

18.5. proc and sysfs Handling of TTY Devices

The tty core provides a very easy way for any tty driver to maintain a file in the /proc/tty/driver directory. If the driver defines the read_proc or write_proc functions, this file is created. Then, any read or write call on this file is sent to the driver. The formats of these functions are just like the standard /proc file-handling functions.

As an example, here is a simple implementation of the read_proc tty callback that merely prints out the number of the currently registered ports:

static int tiny_read_proc(char *page, char **start, off_t off, int count,
int *eof, void *data)
{
struct tiny_serial *tiny;
off_t begin = 0;
int length = 0;
int i;
length += sprintf(page, "tinyserinfo:1.0 driver:%s\n", DRIVER_VERSION);
for (i = 0; i < TINY_TTY_MINORS && length < PAGE_SIZE; ++i) {
tiny = tiny_table[i];
if (tiny =  = NULL)
continue;
length += sprintf(page+length, "%d\n", i);
if ((length + begin) > (off + count))
goto done;
if ((length + begin) < off) {
begin += length;
length = 0;
}
}
*eof = 1;
done:
if (off >= (length + begin))
return 0;
*start = page + (off-begin);
return (count < begin+length-off) ? count : begin + length-off;
}

The tty core handles all of the sysfs directory and device creation when the tty driver is registered, or when the individual tty devices are created, depending on the TTY_DRIVER_NO_DEVFS flag in the struct tty_driver. The individual directory always contains the dev file, which allows user-space tools to determine the major and minor number assigned to the device. It also contains a device and driver symlink, if a pointer to a valid struct device is passed in the call to tty_register_device. Other than these three files, it is not possible for individual tty drivers to create new sysfs files in this location. This will probably change in future kernel releases.