Home | History | Annotate | Line # | Download | only in common
      1  1.11     skrll /*	$NetBSD: tty_60.c,v 1.11 2021/07/21 06:35:44 skrll Exp $	*/
      2   1.1       apb 
      3   1.1       apb /*-
      4   1.1       apb  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5   1.1       apb  * All rights reserved.
      6   1.1       apb  *
      7   1.1       apb  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1       apb  * by Alan Barrett
      9   1.1       apb  *
     10   1.1       apb  * Redistribution and use in source and binary forms, with or without
     11   1.1       apb  * modification, are permitted provided that the following conditions
     12   1.1       apb  * are met:
     13   1.1       apb  * 1. Redistributions of source code must retain the above copyright
     14   1.1       apb  *    notice, this list of conditions and the following disclaimer.
     15   1.1       apb  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       apb  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       apb  *    documentation and/or other materials provided with the distribution.
     18   1.1       apb  *
     19   1.1       apb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1       apb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1       apb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1       apb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1       apb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1       apb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1       apb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1       apb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1       apb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1       apb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1       apb  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1       apb  */
     31   1.1       apb 
     32   1.1       apb #include <sys/cdefs.h>
     33  1.11     skrll __KERNEL_RCSID(0, "$NetBSD: tty_60.c,v 1.11 2021/07/21 06:35:44 skrll Exp $");
     34   1.1       apb 
     35   1.1       apb #if defined(_KERNEL_OPT)
     36   1.1       apb #include "opt_compat_netbsd.h"
     37   1.1       apb #endif
     38   1.1       apb 
     39  1.11     skrll #include <sys/param.h>
     40   1.1       apb #include <sys/types.h>
     41   1.1       apb 
     42   1.1       apb #include <sys/conf.h>
     43   1.1       apb #include <sys/errno.h>
     44   1.1       apb #include <sys/systm.h>
     45   1.5  pgoyette #include <sys/compat_stub.h>
     46  1.10  jdolecek #include <sys/kmem.h>
     47   1.1       apb 
     48   1.1       apb #include <sys/tty.h>
     49   1.5  pgoyette 
     50   1.5  pgoyette #include <compat/common/compat_mod.h>
     51   1.1       apb #include <compat/sys/ttycom.h>
     52   1.1       apb 
     53   1.1       apb /* convert struct ptmget to struct compat_60_ptmget */
     54   1.1       apb static int
     55   1.1       apb ptmget_to_ptmget60(struct ptmget *pg, struct compat_60_ptmget *pg60)
     56   1.1       apb {
     57   1.1       apb 	memset(pg60, 0, sizeof(*pg60));
     58   1.1       apb 	pg60->cfd = pg->cfd;
     59   1.3       apb 	pg60->sfd = pg->sfd;
     60   1.1       apb 	strlcpy(pg60->cn, pg->cn, sizeof(pg60->cn));
     61   1.1       apb 	strlcpy(pg60->sn, pg->sn, sizeof(pg60->sn));
     62   1.1       apb 	if (strlen(pg->cn) >= sizeof(pg60->cn)
     63   1.1       apb 	    || strlen(pg->sn) >= sizeof(pg60->sn))
     64   1.1       apb 		return E2BIG;
     65   1.1       apb 	return 0;
     66   1.1       apb }
     67   1.1       apb 
     68   1.1       apb /* Helper for compat ioctls that use struct compat_60_ptmget. */
     69   1.1       apb static int
     70   1.1       apb compat_60_ptmget_ioctl(dev_t dev, u_long cmd, void *data, int flag,
     71   1.1       apb 	struct lwp *l)
     72   1.1       apb {
     73   1.1       apb 	int ret;
     74   1.1       apb 	u_long newcmd;
     75  1.10  jdolecek 	struct ptmget *pg;
     76   1.1       apb 	const struct cdevsw *cd = cdevsw_lookup(dev);
     77   1.1       apb 
     78   1.1       apb 	if (cd == NULL || cd->d_ioctl == NULL)
     79   1.1       apb 		return ENXIO;
     80   1.1       apb 
     81   1.1       apb 	switch (cmd) {
     82   1.1       apb 	case COMPAT_60_TIOCPTMGET:  newcmd = TIOCPTMGET; break;
     83   1.1       apb 	case COMPAT_60_TIOCPTSNAME: newcmd = TIOCPTSNAME; break;
     84   1.1       apb 	default: return ENOTTY;
     85   1.1       apb 	}
     86   1.1       apb 
     87  1.10  jdolecek 	pg = kmem_alloc(sizeof(*pg), KM_SLEEP);
     88  1.10  jdolecek 
     89  1.10  jdolecek 	ret = (cd->d_ioctl)(dev, newcmd, pg, flag, l);
     90   1.1       apb 	if (ret != 0)
     91  1.10  jdolecek 		goto out;
     92  1.10  jdolecek 
     93  1.10  jdolecek 	ret = ptmget_to_ptmget60(pg, data);
     94  1.10  jdolecek 
     95  1.10  jdolecek out:
     96  1.10  jdolecek 	kmem_free(pg, sizeof(*pg));
     97   1.1       apb 	return ret;
     98   1.1       apb }
     99   1.1       apb 
    100   1.1       apb /*
    101   1.1       apb  * COMPAT_60 versions of ttioctl and ptmioctl.
    102   1.1       apb  */
    103   1.1       apb int
    104   1.6  christos compat_60_ttioctl(struct tty *tp, u_long cmd, void *data, int flag,
    105   1.1       apb 	struct lwp *l)
    106   1.1       apb {
    107   1.1       apb 
    108   1.1       apb 	switch (cmd) {
    109   1.4  christos 	case COMPAT_60_TIOCPTMGET:
    110   1.1       apb 	case COMPAT_60_TIOCPTSNAME:
    111   1.6  christos 		return compat_60_ptmget_ioctl(tp->t_dev, cmd, data, flag, l);
    112   1.1       apb 	default:
    113   1.1       apb 		return EPASSTHROUGH;
    114   1.1       apb 	}
    115   1.1       apb }
    116   1.1       apb 
    117   1.1       apb int
    118   1.6  christos compat_60_ptmioctl(dev_t dev, u_long cmd, void *data, int flag,
    119   1.6  christos     struct lwp *l)
    120   1.1       apb {
    121   1.1       apb 
    122   1.1       apb 	switch (cmd) {
    123   1.1       apb 	case COMPAT_60_TIOCPTMGET:
    124   1.1       apb 		return compat_60_ptmget_ioctl(dev, cmd, data, flag, l);
    125   1.1       apb 	default:
    126   1.1       apb 		return EPASSTHROUGH;
    127   1.1       apb 	}
    128   1.1       apb }
    129   1.2       apb 
    130   1.5  pgoyette void
    131   1.5  pgoyette kern_tty_60_init(void)
    132   1.5  pgoyette {
    133   1.5  pgoyette 
    134   1.9  pgoyette 	MODULE_HOOK_SET(tty_ttioctl_60_hook, compat_60_ttioctl);
    135   1.9  pgoyette 	MODULE_HOOK_SET(tty_ptmioctl_60_hook, compat_60_ptmioctl);
    136   1.5  pgoyette }
    137   1.5  pgoyette 
    138   1.5  pgoyette void
    139   1.5  pgoyette kern_tty_60_fini(void)
    140   1.5  pgoyette {
    141   1.8  pgoyette 	MODULE_HOOK_UNSET(tty_ttioctl_60_hook);
    142   1.8  pgoyette 	MODULE_HOOK_UNSET(tty_ptmioctl_60_hook);
    143   1.5  pgoyette }
    144