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