/*
       * proc_tty.c -- handles /proc/tty
       *
       * Copyright 1997, Theodore Ts'o
       */
      
      #include <asm/uaccess.h>
      
      #include <linux/init.h>
      #include <linux/errno.h>
      #include <linux/sched.h>
      #include <linux/proc_fs.h>
      #include <linux/stat.h>
      #include <linux/tty.h>
      #include <asm/bitops.h>
      
      extern struct tty_driver *tty_drivers;	/* linked list of tty drivers */
      extern struct tty_ldisc ldiscs[];
      
      
      static int tty_drivers_read_proc(char *page, char **start, off_t off,
      				 int count, int *eof, void *data);
      static int tty_ldiscs_read_proc(char *page, char **start, off_t off,
      				int count, int *eof, void *data);
      
      /*
       * The /proc/tty directory inodes...
       */
      static struct proc_dir_entry *proc_tty_ldisc, *proc_tty_driver;
      
      /*
       * This is the handler for /proc/tty/drivers
       */
  34  static int tty_drivers_read_proc(char *page, char **start, off_t off,
      				 int count, int *eof, void *data)
      {
      	int	len = 0;
      	off_t	begin = 0;
      	struct tty_driver *p;
      	char	range[20], deftype[20];
      	char	*type;
      
  43  	for (p = tty_drivers; p; p = p->next) {
  44  		if (p->num > 1)
      			sprintf(range, "%d-%d", p->minor_start,
      				p->minor_start + p->num - 1);
  47  		else
      			sprintf(range, "%d", p->minor_start);
  49  		switch (p->type) {
  50  		case TTY_DRIVER_TYPE_SYSTEM:
  51  			if (p->subtype == SYSTEM_TYPE_TTY)
      				type = "system:/dev/tty";
  53  			else if (p->subtype == SYSTEM_TYPE_SYSCONS)
      				type = "system:console";
  55  			else if (p->subtype == SYSTEM_TYPE_CONSOLE)
      				type = "system:vtmaster";
  57  			else
      				type = "system";
  59  			break;
  60  		case TTY_DRIVER_TYPE_CONSOLE:
      			type = "console";
  62  			break;
  63  		case TTY_DRIVER_TYPE_SERIAL:
  64  			if (p->subtype == 2)
      				type = "serial:callout";
  66  			else
      				type = "serial";
  68  			break;
  69  		case TTY_DRIVER_TYPE_PTY:
  70  			if (p->subtype == PTY_TYPE_MASTER)
      				type = "pty:master";
  72  			else if (p->subtype == PTY_TYPE_SLAVE)
      				type = "pty:slave";
  74  			else
      				type = "pty";
  76  			break;
  77  		default:
      			sprintf(deftype, "type:%d.%d", p->type, p->subtype);
      			type = deftype;
  80  			break;
      		}
      		len += sprintf(page+len, "%-20s /dev/%-8s %3d %7s %s\n",
      			       p->driver_name ? p->driver_name : "unknown",
      			       p->name, p->major, range, type);
  85  		if (len+begin > off+count)
  86  			break;
  87  		if (len+begin < off) {
      			begin += len;
      			len = 0;
      		}
      	}
  92  	if (!p)
      		*eof = 1;
  94  	if (off >= len+begin)
  95  		return 0;
      	*start = page + (off-begin);
  97  	return ((count < begin+len-off) ? count : begin+len-off);
      }
      
      /*
       * This is the handler for /proc/tty/ldiscs
       */
 103  static int tty_ldiscs_read_proc(char *page, char **start, off_t off,
      				int count, int *eof, void *data)
      {
      	int	i;
      	int	len = 0;
      	off_t	begin = 0;
      
 110  	for (i=0; i < NR_LDISCS; i++) {
 111  		if (!(ldiscs[i].flags & LDISC_FLAG_DEFINED))
 112  			continue;
      		len += sprintf(page+len, "%-10s %2d\n",
      			       ldiscs[i].name ? ldiscs[i].name : "???", i);
 115  		if (len+begin > off+count)
 116  			break;
 117  		if (len+begin < off) {
      			begin += len;
      			len = 0;
      		}
      	}
 122  	if (i >= NR_LDISCS)
      		*eof = 1;
 124  	if (off >= len+begin)
 125  		return 0;
      	*start = page + (off-begin);
 127  	return ((count < begin+len-off) ? count : begin+len-off);
      }
      
      /*
       * Thsi function is called by register_tty_driver() to handle
       * registering the driver's /proc handler into /proc/tty/driver/<foo>
       */
 134  void proc_tty_register_driver(struct tty_driver *driver)
      {
      	struct proc_dir_entry *ent;
      		
      	if ((!driver->read_proc && !driver->write_proc) ||
      	    !driver->driver_name ||
 140  	    driver->proc_entry)
 141  		return;
      
      	ent = create_proc_entry(driver->driver_name, 0, proc_tty_driver);
 144  	if (!ent)
 145  		return;
      	ent->read_proc = driver->read_proc;
      	ent->write_proc = driver->write_proc;
      	ent->data = driver;
      
      	driver->proc_entry = ent;
      }
      
      /*
       * This function is called by unregister_tty_driver()
       */
 156  void proc_tty_unregister_driver(struct tty_driver *driver)
      {
      	struct proc_dir_entry *ent;
      
      	ent = driver->proc_entry;
 161  	if (!ent)
 162  		return;
      		
      	remove_proc_entry(driver->driver_name, proc_tty_driver);
      	
      	driver->proc_entry = 0;
      }
      
      /*
       * Called by proc_root_init() to initialize the /proc/tty subtree
       */
 172  void __init proc_tty_init(void)
      {
 174  	if (!proc_mkdir("tty", 0))
 175  		return;
      	proc_tty_ldisc = proc_mkdir("tty/ldisc", 0);
      	proc_tty_driver = proc_mkdir("tty/driver", 0);
      
      	create_proc_read_entry("tty/ldiscs", 0, 0, tty_ldiscs_read_proc,NULL);
      	create_proc_read_entry("tty/drivers", 0, 0, tty_drivers_read_proc,NULL);
      }