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 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 * Copyright 2018 Joyent, Inc. All rights reserved. 25 */ 26 27 #include <regex.h> 28 #include <devfsadm.h> 29 #include <stdio.h> 30 #include <strings.h> 31 #include <stdlib.h> 32 #include <limits.h> 33 #include <ctype.h> 34 #include <sys/mc_amd.h> 35 #include <bsm/devalloc.h> 36 37 extern int system_labeled; 38 39 static int ln_minor_name(di_minor_t minor, di_node_t node); 40 static int lp(di_minor_t minor, di_node_t node); 41 static int serial_dialout(di_minor_t minor, di_node_t node); 42 static int serial(di_minor_t minor, di_node_t node); 43 static int diskette(di_minor_t minor, di_node_t node); 44 static int vt00(di_minor_t minor, di_node_t node); 45 static int kdmouse(di_minor_t minor, di_node_t node); 46 static int ipmi(di_minor_t minor, di_node_t node); 47 static int mc_node(di_minor_t minor, di_node_t node); 48 static int vmmctl(di_minor_t minor, di_node_t node); 49 50 static devfsadm_create_t misc_cbt[] = { 51 { "vt00", "ddi_display", NULL, 52 TYPE_EXACT, ILEVEL_0, vt00 53 }, 54 { "mouse", "ddi_mouse", "mouse8042", 55 TYPE_EXACT | DRV_EXACT, ILEVEL_0, kdmouse 56 }, 57 { "pseudo", "ddi_pseudo", "ipmi", 58 TYPE_EXACT | DRV_EXACT, ILEVEL_0, ipmi, 59 }, 60 { "pseudo", "ddi_pseudo", "smbios", 61 TYPE_EXACT | DRV_EXACT, ILEVEL_1, ln_minor_name, 62 }, 63 /* floppies share the same class, but not link regex, as hard disks */ 64 { "disk", "ddi_block:diskette", NULL, 65 TYPE_EXACT, ILEVEL_1, diskette 66 }, 67 { "parallel", "ddi_printer", NULL, 68 TYPE_EXACT, ILEVEL_1, lp 69 }, 70 { "serial", "ddi_serial:mb", NULL, 71 TYPE_EXACT, ILEVEL_1, serial 72 }, 73 { "serial", "ddi_serial:dialout,mb", NULL, 74 TYPE_EXACT, ILEVEL_1, serial_dialout 75 }, 76 { "pseudo", "ddi_pseudo", "xsvc", 77 TYPE_EXACT | DRV_EXACT, ILEVEL_0, ln_minor_name, 78 }, 79 { "pseudo", "ddi_pseudo", "srn", 80 TYPE_EXACT | DRV_EXACT, ILEVEL_0, ln_minor_name, 81 }, 82 { "memory-controller", "ddi_mem_ctrl", NULL, 83 TYPE_EXACT, ILEVEL_0, mc_node 84 }, 85 { "pseudo", "ddi_pseudo", "ucode", 86 TYPE_EXACT | DRV_EXACT, ILEVEL_0, ln_minor_name, 87 }, 88 { "pseudo", "ddi_pseudo", "vmm", 89 TYPE_EXACT | DRV_EXACT, ILEVEL_0, vmmctl, 90 } 91 }; 92 93 DEVFSADM_CREATE_INIT_V0(misc_cbt); 94 95 static devfsadm_remove_t misc_remove_cbt[] = { 96 { "vt", "vt[0-9][0-9]", RM_PRE|RM_ALWAYS, 97 ILEVEL_0, devfsadm_rm_all 98 }, 99 { "pseudo", "^ucode$", RM_ALWAYS | RM_PRE | RM_HOT, 100 ILEVEL_0, devfsadm_rm_all 101 }, 102 { "mouse", "^kdmouse$", RM_ALWAYS | RM_PRE, 103 ILEVEL_0, devfsadm_rm_all 104 }, 105 { "disk", "^(diskette|rdiskette)([0-9]*)$", 106 RM_ALWAYS | RM_PRE, ILEVEL_1, devfsadm_rm_all 107 }, 108 { "parallel", "^(lp|ecpp)([0-9]+)$", RM_ALWAYS | RM_PRE, 109 ILEVEL_1, devfsadm_rm_all 110 }, 111 { "serial", "^(tty|ttyd)([0-9]+)$", RM_ALWAYS | RM_PRE, 112 ILEVEL_1, devfsadm_rm_all 113 }, 114 { "serial", "^tty[a-z]$", RM_ALWAYS | RM_PRE, 115 ILEVEL_1, devfsadm_rm_all 116 }, 117 { "pseudo", "^vmmctl$", RM_ALWAYS | RM_PRE | RM_HOT, 118 ILEVEL_0, devfsadm_rm_all 119 } 120 }; 121 122 DEVFSADM_REMOVE_INIT_V0(misc_remove_cbt); 123 124 /* 125 * Any /dev/foo entry named after the minor name such as 126 * /devices/.../driver@0:foo 127 */ 128 static int 129 ln_minor_name(di_minor_t minor, di_node_t node) 130 { 131 (void) devfsadm_mklink(di_minor_name(minor), node, minor, 0); 132 return (DEVFSADM_CONTINUE); 133 } 134 135 /* 136 * Handles minor node type "ddi_display", in addition to generic processing 137 * done by display(). 138 * 139 * This creates a /dev/vt00 link to /dev/fb, for backwards compatibility. 140 */ 141 /* ARGSUSED */ 142 int 143 vt00(di_minor_t minor, di_node_t node) 144 { 145 (void) devfsadm_secondary_link("vt00", "fb", 0); 146 return (DEVFSADM_CONTINUE); 147 } 148 149 /* 150 * type=ddi_block:diskette;addr=0,0;minor=c diskette 151 * type=ddi_block:diskette;addr=0,0;minor=c,raw rdiskette 152 * type=ddi_block:diskette;addr1=0;minor=c diskette\A2 153 * type=ddi_block:diskette;addr1=0;minor=c,raw rdiskette\A2 154 */ 155 static int 156 diskette(di_minor_t minor, di_node_t node) 157 { 158 int flags = 0; 159 char *a2; 160 char link[PATH_MAX]; 161 char *addr = di_bus_addr(node); 162 char *mn = di_minor_name(minor); 163 164 if (system_labeled) 165 flags = DA_ADD|DA_FLOPPY; 166 167 if (strcmp(addr, "0,0") == 0) { 168 if (strcmp(mn, "c") == 0) { 169 (void) devfsadm_mklink("diskette", node, minor, flags); 170 } else if (strcmp(mn, "c,raw") == 0) { 171 (void) devfsadm_mklink("rdiskette", node, minor, flags); 172 } 173 174 } 175 176 if (addr[0] == '0') { 177 if ((a2 = strchr(addr, ',')) != NULL) { 178 a2++; 179 if (strcmp(mn, "c") == 0) { 180 (void) strcpy(link, "diskette"); 181 (void) strcat(link, a2); 182 (void) devfsadm_mklink(link, node, minor, 183 flags); 184 } else if (strcmp(mn, "c,raw") == 0) { 185 (void) strcpy(link, "rdiskette"); 186 (void) strcat(link, a2); 187 (void) devfsadm_mklink(link, node, minor, 188 flags); 189 } 190 } 191 } 192 193 return (DEVFSADM_CONTINUE); 194 } 195 196 /* 197 * type=ddi_printer;name=lp;addr=1,3bc lp0 198 * type=ddi_printer;name=lp;addr=1,378 lp1 199 * type=ddi_printer;name=lp;addr=1,278 lp2 200 */ 201 static int 202 lp(di_minor_t minor, di_node_t node) 203 { 204 char *addr = di_bus_addr(node); 205 char *buf; 206 char path[PATH_MAX + 1]; 207 devfsadm_enumerate_t rules[1] = {"^ecpp([0-9]+)$", 1, MATCH_ALL}; 208 209 if (strcmp(addr, "1,3bc") == 0) { 210 (void) devfsadm_mklink("lp0", node, minor, 0); 211 212 } else if (strcmp(addr, "1,378") == 0) { 213 (void) devfsadm_mklink("lp1", node, minor, 0); 214 215 } else if (strcmp(addr, "1,278") == 0) { 216 (void) devfsadm_mklink("lp2", node, minor, 0); 217 } 218 219 if (strcmp(di_driver_name(node), "ecpp") != 0) { 220 return (DEVFSADM_CONTINUE); 221 } 222 223 if ((buf = di_devfs_path(node)) == NULL) { 224 return (DEVFSADM_CONTINUE); 225 } 226 227 (void) snprintf(path, sizeof (path), "%s:%s", 228 buf, di_minor_name(minor)); 229 230 di_devfs_path_free(buf); 231 232 if (devfsadm_enumerate_int(path, 0, &buf, rules, 1)) { 233 return (DEVFSADM_CONTINUE); 234 } 235 236 (void) snprintf(path, sizeof (path), "ecpp%s", buf); 237 free(buf); 238 (void) devfsadm_mklink(path, node, minor, 0); 239 return (DEVFSADM_CONTINUE); 240 } 241 242 /* 243 * type=ddi_serial:mb;minor=a tty00 244 * type=ddi_serial:mb;minor=b tty01 245 * type=ddi_serial:mb;minor=c tty02 246 * type=ddi_serial:mb;minor=d tty03 247 */ 248 static int 249 serial(di_minor_t minor, di_node_t node) 250 { 251 252 char *mn = di_minor_name(minor); 253 char link[PATH_MAX]; 254 255 (void) strcpy(link, "tty"); 256 (void) strcat(link, mn); 257 (void) devfsadm_mklink(link, node, minor, 0); 258 259 if (strcmp(mn, "a") == 0) { 260 (void) devfsadm_mklink("tty00", node, minor, 0); 261 262 } else if (strcmp(mn, "b") == 0) { 263 (void) devfsadm_mklink("tty01", node, minor, 0); 264 265 } else if (strcmp(mn, "c") == 0) { 266 (void) devfsadm_mklink("tty02", node, minor, 0); 267 268 } else if (strcmp(mn, "d") == 0) { 269 (void) devfsadm_mklink("tty03", node, minor, 0); 270 } 271 return (DEVFSADM_CONTINUE); 272 } 273 274 /* 275 * type=ddi_serial:dialout,mb;minor=a,cu ttyd0 276 * type=ddi_serial:dialout,mb;minor=b,cu ttyd1 277 * type=ddi_serial:dialout,mb;minor=c,cu ttyd2 278 * type=ddi_serial:dialout,mb;minor=d,cu ttyd3 279 */ 280 static int 281 serial_dialout(di_minor_t minor, di_node_t node) 282 { 283 char *mn = di_minor_name(minor); 284 285 if (strcmp(mn, "a,cu") == 0) { 286 (void) devfsadm_mklink("ttyd0", node, minor, 0); 287 (void) devfsadm_mklink("cua0", node, minor, 0); 288 289 } else if (strcmp(mn, "b,cu") == 0) { 290 (void) devfsadm_mklink("ttyd1", node, minor, 0); 291 (void) devfsadm_mklink("cua1", node, minor, 0); 292 293 } else if (strcmp(mn, "c,cu") == 0) { 294 (void) devfsadm_mklink("ttyd2", node, minor, 0); 295 (void) devfsadm_mklink("cua2", node, minor, 0); 296 297 } else if (strcmp(mn, "d,cu") == 0) { 298 (void) devfsadm_mklink("ttyd3", node, minor, 0); 299 (void) devfsadm_mklink("cua3", node, minor, 0); 300 } 301 return (DEVFSADM_CONTINUE); 302 } 303 304 static int 305 kdmouse(di_minor_t minor, di_node_t node) 306 { 307 (void) devfsadm_mklink("kdmouse", node, minor, 0); 308 return (DEVFSADM_CONTINUE); 309 } 310 311 static int 312 ipmi(di_minor_t minor, di_node_t node) 313 { 314 /* 315 * Follow convention from other systems, and include an instance#, 316 * even though there will only be one. 317 */ 318 (void) devfsadm_mklink("ipmi0", node, minor, 0); 319 return (DEVFSADM_CONTINUE); 320 } 321 322 /* 323 * /dev/mc/mc<chipid> -> /devices/.../pci1022,1102@<chipid+24>,2:mc-amd 324 */ 325 static int 326 mc_node(di_minor_t minor, di_node_t node) 327 { 328 const char *minorname = di_minor_name(minor); 329 const char *busaddr = di_bus_addr(node); 330 char linkpath[PATH_MAX]; 331 int unitaddr; 332 char *c; 333 334 if (minorname == NULL || busaddr == NULL) 335 return (DEVFSADM_CONTINUE); 336 337 errno = 0; 338 unitaddr = strtol(busaddr, &c, 16); 339 340 if (errno != 0) 341 return (DEVFSADM_CONTINUE); 342 343 if (unitaddr == 0) { 344 (void) snprintf(linkpath, sizeof (linkpath), "mc/mc"); 345 } else if (unitaddr >= MC_AMD_DEV_OFFSET) { 346 (void) snprintf(linkpath, sizeof (linkpath), "mc/mc%u", 347 unitaddr - MC_AMD_DEV_OFFSET); 348 } else { 349 (void) snprintf(linkpath, sizeof (linkpath), "mc/mc%u", 350 minor->dev_minor); 351 } 352 (void) devfsadm_mklink(linkpath, node, minor, 0); 353 return (DEVFSADM_CONTINUE); 354 } 355 356 /* 357 * /dev/vmmctl -> /devices/pseudo/vmm@0:ctl 358 */ 359 static int 360 vmmctl(di_minor_t minor, di_node_t node) 361 { 362 if (strcmp(di_minor_name(minor), "ctl") == 0) 363 (void) devfsadm_mklink("vmmctl", node, minor, 0); 364 return (DEVFSADM_CONTINUE); 365 } 366