1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  22 /*        All Rights Reserved   */
  23 
  24 
  25 /*
  26  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  27  * Use is subject to license terms.
  28  *
  29  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
  30  */
  31 
  32 /*
  33  * UNIX Device Driver Interface functions
  34  * (excerpts)
  35  */
  36 
  37 #include <sys/types.h>
  38 #include <sys/sysmacros.h>
  39 #include <sys/param.h>
  40 #include <sys/ddi.h>
  41 #include <sys/mkdev.h>
  42 #include <sys/debug.h>
  43 
  44 #ifndef NODEV32
  45 #define NODEV32 (dev32_t)(-1)
  46 #endif  /* NODEV32 */
  47 
  48 /*
  49  * return internal major number corresponding to device
  50  * number (new format) argument
  51  */
  52 major_t
  53 getmajor(dev_t dev)
  54 {
  55 #ifdef _LP64
  56         return ((major_t)((dev >> NBITSMINOR64) & MAXMAJ64));
  57 #else
  58         return ((major_t)((dev >> NBITSMINOR) & MAXMAJ));
  59 #endif
  60 }
  61 
  62 /*
  63  * return internal minor number corresponding to device
  64  * number (new format) argument
  65  */
  66 minor_t
  67 getminor(dev_t dev)
  68 {
  69 #ifdef _LP64
  70         return ((minor_t)(dev & MAXMIN64));
  71 #else
  72         return ((minor_t)(dev & MAXMIN));
  73 #endif
  74 }
  75 
  76 /*
  77  * encode external major and minor number arguments into a
  78  * new format device number
  79  */
  80 dev_t
  81 makedevice(major_t maj, minor_t minor)
  82 {
  83 #ifdef _LP64
  84         return (((dev_t)maj << NBITSMINOR64) | (minor & MAXMIN64));
  85 #else
  86         return (((dev_t)maj << NBITSMINOR) | (minor & MAXMIN));
  87 #endif
  88 }
  89 
  90 /*
  91  * Compress 'long' device number encoding to 32-bit device number
  92  * encoding.  If it won't fit, we return failure, but set the
  93  * device number to 32-bit NODEV for the sake of our callers.
  94  */
  95 int
  96 cmpldev(dev32_t *dst, dev_t dev)
  97 {
  98 #if defined(_LP64)
  99         if (dev == NODEV) {
 100                 *dst = NODEV32;
 101         } else {
 102                 major_t major = dev >> L_BITSMINOR;
 103                 minor_t minor = dev & L_MAXMIN;
 104 
 105                 if (major > L_MAXMAJ32 || minor > L_MAXMIN32) {
 106                         *dst = NODEV32;
 107                         return (0);
 108                 }
 109 
 110                 *dst = (dev32_t)((major << L_BITSMINOR32) | minor);
 111         }
 112 #else
 113         *dst = (dev32_t)dev;
 114 #endif
 115         return (1);
 116 }
 117 
 118 /*
 119  * Expand 32-bit dev_t's to long dev_t's.  Expansion always "fits"
 120  * into the return type, but we're careful to expand NODEV explicitly.
 121  */
 122 dev_t
 123 expldev(dev32_t dev32)
 124 {
 125 #ifdef _LP64
 126         if (dev32 == NODEV32)
 127                 return (NODEV);
 128         return (makedevice((dev32 >> L_BITSMINOR32) & L_MAXMAJ32,
 129             dev32 & L_MAXMIN32));
 130 #else
 131         return ((dev_t)dev32);
 132 #endif
 133 }