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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <regex.h>
29 #include <devfsadm.h>
30 #include <stdio.h>
31 #include <strings.h>
32 #include <stdlib.h>
33 #include <limits.h>
34 #include <ctype.h>
35 #include <sys/mc_amd.h>
36 #include <bsm/devalloc.h>
37 
38 extern int system_labeled;
39 
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 bmc(di_minor_t minor, di_node_t node);
47 static int smbios(di_minor_t minor, di_node_t node);
48 static int agp_process(di_minor_t minor, di_node_t node);
49 static int drm_node(di_minor_t minor, di_node_t node);
50 static int mc_node(di_minor_t minor, di_node_t node);
51 static int xsvc(di_minor_t minor, di_node_t node);
52 static int srn(di_minor_t minor, di_node_t node);
53 static int ucode(di_minor_t minor, di_node_t node);
54 
55 static devfsadm_create_t misc_cbt[] = {
56 	{ "vt00", "ddi_display", NULL,
57 	    TYPE_EXACT, ILEVEL_0,	vt00
58 	},
59 	{ "drm", "ddi_display:drm", NULL,
60 	    TYPE_EXACT, ILEVEL_0,	drm_node
61 	},
62 	{ "mouse", "ddi_mouse", "mouse8042",
63 	    TYPE_EXACT | DRV_EXACT, ILEVEL_0, kdmouse
64 	},
65 	{ "pseudo", "ddi_pseudo", "bmc",
66 	    TYPE_EXACT | DRV_EXACT, ILEVEL_0, bmc,
67 	},
68 	{ "pseudo", "ddi_pseudo", "smbios",
69 	    TYPE_EXACT | DRV_EXACT, ILEVEL_1, smbios,
70 	},
71 	{ "disk",  "ddi_block:diskette", NULL,
72 	    TYPE_EXACT, ILEVEL_1, diskette
73 	},
74 	{ "parallel",  "ddi_printer", NULL,
75 	    TYPE_EXACT, ILEVEL_1, lp
76 	},
77 	{ "serial", "ddi_serial:mb", NULL,
78 	    TYPE_EXACT, ILEVEL_1, serial
79 	},
80 	{ "serial",  "ddi_serial:dialout,mb", NULL,
81 	    TYPE_EXACT, ILEVEL_1, serial_dialout
82 	},
83 	{ "agp", "ddi_agp:pseudo", NULL,
84 	    TYPE_EXACT, ILEVEL_0, agp_process
85 	},
86 	{ "agp", "ddi_agp:target", NULL,
87 	    TYPE_EXACT, ILEVEL_0, agp_process
88 	},
89 	{ "agp", "ddi_agp:cpugart", NULL,
90 	    TYPE_EXACT, ILEVEL_0, agp_process
91 	},
92 	{ "agp", "ddi_agp:master", NULL,
93 	    TYPE_EXACT, ILEVEL_0, agp_process
94 	},
95 	{ "pseudo", "ddi_pseudo", NULL,
96 	    TYPE_EXACT, ILEVEL_0, xsvc
97 	},
98 	{ "pseudo", "ddi_pseudo", NULL,
99 	    TYPE_EXACT, ILEVEL_0, srn
100 	},
101 	{ "memory-controller", "ddi_mem_ctrl", NULL,
102 	    TYPE_EXACT, ILEVEL_0, mc_node
103 	},
104 	{ "pseudo", "ddi_pseudo", "ucode",
105 	    TYPE_EXACT | DRV_EXACT, ILEVEL_0, ucode,
106 	}
107 };
108 
109 DEVFSADM_CREATE_INIT_V0(misc_cbt);
110 
111 static char *debug_mid = "misc_mid";
112 
113 typedef enum {
114 	DRIVER_AGPPSEUDO = 0,
115 	DRIVER_AGPTARGET,
116 	DRIVER_CPUGART,
117 	DRIVER_AGPMASTER_DRM,
118 	DRIVER_AGPMASTER_VGATEXT,
119 	DRIVER_UNKNOWN
120 } driver_defs_t;
121 
122 typedef struct {
123 	char	*driver_name;
124 	int	index;
125 } driver_name_table_entry_t;
126 
127 static driver_name_table_entry_t driver_name_table[] = {
128 	{ "agpgart",		DRIVER_AGPPSEUDO },
129 	{ "agptarget",		DRIVER_AGPTARGET },
130 	{ "amd64_gart",		DRIVER_CPUGART },
131 	/* AGP master device managed by drm driver */
132 	{ "i915",		DRIVER_AGPMASTER_DRM },
133 	{ "vgatext",		DRIVER_AGPMASTER_VGATEXT },
134 	{ NULL,			DRIVER_UNKNOWN }
135 };
136 
137 static devfsadm_enumerate_t agptarget_rules[1] =
138 	{ "^agp$/^agptarget([0-9]+)$", 1, MATCH_ALL };
139 static devfsadm_enumerate_t cpugart_rules[1] =
140 	{ "^agp$/^cpugart([0-9]+)$", 1, MATCH_ALL };
141 static devfsadm_enumerate_t agpmaster_rules[1] =
142 	{  "^agp$/^agpmaster([0-9]+)$", 1, MATCH_ALL };
143 
144 static devfsadm_remove_t misc_remove_cbt[] = {
145 	{ "vt", "vt[0-9][0-9]", RM_PRE|RM_ALWAYS,
146 		ILEVEL_0, devfsadm_rm_all
147 	},
148 	{ "pseudo", "^ucode$", RM_ALWAYS | RM_PRE | RM_HOT,
149 		ILEVEL_0, devfsadm_rm_all
150 	}
151 };
152 
153 DEVFSADM_REMOVE_INIT_V0(misc_remove_cbt);
154 
155 /*
156  * Handles minor node type "ddi_display", in addition to generic processing
157  * done by display().
158  *
159  * This creates a /dev/vt00 link to /dev/fb, for backwards compatibility.
160  */
161 /* ARGSUSED */
162 int
163 vt00(di_minor_t minor, di_node_t node)
164 {
165 	(void) devfsadm_secondary_link("vt00", "fb", 0);
166 	return (DEVFSADM_CONTINUE);
167 }
168 
169 /*
170  * type=ddi_block:diskette;addr=0,0;minor=c        diskette
171  * type=ddi_block:diskette;addr=0,0;minor=c,raw    rdiskette
172  * type=ddi_block:diskette;addr1=0;minor=c diskette\A2
173  * type=ddi_block:diskette;addr1=0;minor=c,raw     rdiskette\A2
174  */
175 static int
176 diskette(di_minor_t minor, di_node_t node)
177 {
178 	int flags = 0;
179 	char *a2;
180 	char link[PATH_MAX];
181 	char *addr = di_bus_addr(node);
182 	char *mn = di_minor_name(minor);
183 
184 	if (system_labeled)
185 		flags = DA_ADD|DA_FLOPPY;
186 
187 	if (strcmp(addr, "0,0") == 0) {
188 		if (strcmp(mn, "c") == 0) {
189 			(void) devfsadm_mklink("diskette", node, minor, flags);
190 		} else if (strcmp(mn, "c,raw") == 0) {
191 			(void) devfsadm_mklink("rdiskette", node, minor, flags);
192 		}
193 
194 	}
195 
196 	if (addr[0] == '0') {
197 		if ((a2 = strchr(addr, ',')) != NULL) {
198 			a2++;
199 			if (strcmp(mn, "c") == 0) {
200 				(void) strcpy(link, "diskette");
201 				(void) strcat(link, a2);
202 				(void) devfsadm_mklink(link, node, minor,
203 				    flags);
204 			} else if (strcmp(mn, "c,raw") == 0) {
205 				(void) strcpy(link, "rdiskette");
206 				(void) strcat(link, a2);
207 				(void) devfsadm_mklink(link, node, minor,
208 				    flags);
209 			}
210 		}
211 	}
212 
213 	return (DEVFSADM_CONTINUE);
214 }
215 
216 /*
217  * type=ddi_printer;name=lp;addr=1,3bc      lp0
218  * type=ddi_printer;name=lp;addr=1,378      lp1
219  * type=ddi_printer;name=lp;addr=1,278      lp2
220  */
221 static int
222 lp(di_minor_t minor, di_node_t node)
223 {
224 	char *addr = di_bus_addr(node);
225 	char *buf;
226 	char path[PATH_MAX + 1];
227 	devfsadm_enumerate_t rules[1] = {"^ecpp([0-9]+)$", 1, MATCH_ALL};
228 
229 	if (strcmp(addr, "1,3bc") == 0) {
230 		(void) devfsadm_mklink("lp0", node, minor, 0);
231 
232 	} else if (strcmp(addr, "1,378") == 0) {
233 		(void) devfsadm_mklink("lp1", node, minor, 0);
234 
235 	} else if (strcmp(addr, "1,278") == 0) {
236 		(void) devfsadm_mklink("lp2", node, minor, 0);
237 	}
238 
239 	if (strcmp(di_driver_name(node), "ecpp") != 0) {
240 		return (DEVFSADM_CONTINUE);
241 	}
242 
243 	if ((buf = di_devfs_path(node)) == NULL) {
244 		return (DEVFSADM_CONTINUE);
245 	}
246 
247 	(void) snprintf(path, sizeof (path), "%s:%s",
248 	    buf, di_minor_name(minor));
249 
250 	di_devfs_path_free(buf);
251 
252 	if (devfsadm_enumerate_int(path, 0, &buf, rules, 1)) {
253 		return (DEVFSADM_CONTINUE);
254 	}
255 
256 	(void) snprintf(path, sizeof (path), "ecpp%s", buf);
257 	free(buf);
258 	(void) devfsadm_mklink(path, node, minor, 0);
259 	return (DEVFSADM_CONTINUE);
260 }
261 
262 /*
263  * type=ddi_serial:mb;minor=a      tty00
264  * type=ddi_serial:mb;minor=b      tty01
265  * type=ddi_serial:mb;minor=c      tty02
266  * type=ddi_serial:mb;minor=d      tty03
267  */
268 static int
269 serial(di_minor_t minor, di_node_t node)
270 {
271 
272 	char *mn = di_minor_name(minor);
273 	char link[PATH_MAX];
274 
275 	(void) strcpy(link, "tty");
276 	(void) strcat(link, mn);
277 	(void) devfsadm_mklink(link, node, minor, 0);
278 
279 	if (strcmp(mn, "a") == 0) {
280 		(void) devfsadm_mklink("tty00", node, minor, 0);
281 
282 	} else if (strcmp(mn, "b") == 0) {
283 		(void) devfsadm_mklink("tty01", node, minor, 0);
284 
285 	} else if (strcmp(mn, "c") == 0) {
286 		(void) devfsadm_mklink("tty02", node, minor, 0);
287 
288 	} else if (strcmp(mn, "d") == 0) {
289 		(void) devfsadm_mklink("tty03", node, minor, 0);
290 	}
291 	return (DEVFSADM_CONTINUE);
292 }
293 
294 /*
295  * type=ddi_serial:dialout,mb;minor=a,cu   ttyd0
296  * type=ddi_serial:dialout,mb;minor=b,cu   ttyd1
297  * type=ddi_serial:dialout,mb;minor=c,cu   ttyd2
298  * type=ddi_serial:dialout,mb;minor=d,cu   ttyd3
299  */
300 static int
301 serial_dialout(di_minor_t minor, di_node_t node)
302 {
303 	char *mn = di_minor_name(minor);
304 
305 	if (strcmp(mn, "a,cu") == 0) {
306 		(void) devfsadm_mklink("ttyd0", node, minor, 0);
307 		(void) devfsadm_mklink("cua0", node, minor, 0);
308 
309 	} else if (strcmp(mn, "b,cu") == 0) {
310 		(void) devfsadm_mklink("ttyd1", node, minor, 0);
311 		(void) devfsadm_mklink("cua1", node, minor, 0);
312 
313 	} else if (strcmp(mn, "c,cu") == 0) {
314 		(void) devfsadm_mklink("ttyd2", node, minor, 0);
315 		(void) devfsadm_mklink("cua2", node, minor, 0);
316 
317 	} else if (strcmp(mn, "d,cu") == 0) {
318 		(void) devfsadm_mklink("ttyd3", node, minor, 0);
319 		(void) devfsadm_mklink("cua3", node, minor, 0);
320 	}
321 	return (DEVFSADM_CONTINUE);
322 }
323 
324 static int
325 kdmouse(di_minor_t minor, di_node_t node)
326 {
327 	(void) devfsadm_mklink("kdmouse", node, minor, 0);
328 	return (DEVFSADM_CONTINUE);
329 }
330 
331 static int
332 bmc(di_minor_t minor, di_node_t node)
333 {
334 	(void) devfsadm_mklink("bmc", node, minor, 0);
335 	return (DEVFSADM_CONTINUE);
336 }
337 
338 static int
339 smbios(di_minor_t minor, di_node_t node)
340 {
341 	(void) devfsadm_mklink("smbios", node, minor, 0);
342 	return (DEVFSADM_CONTINUE);
343 }
344 
345 static int
346 agp_process(di_minor_t minor, di_node_t node)
347 {
348 	char *minor_nm, *drv_nm;
349 	char *devfspath;
350 	char *I_path, *p_path, *buf;
351 	char *name = (char *)NULL;
352 	int i, index;
353 	devfsadm_enumerate_t rules[1];
354 
355 	minor_nm = di_minor_name(minor);
356 	drv_nm = di_driver_name(node);
357 
358 	if ((minor_nm == NULL) || (drv_nm == NULL)) {
359 		return (DEVFSADM_CONTINUE);
360 	}
361 
362 	devfsadm_print(debug_mid, "agp_process: minor=%s node=%s\n",
363 	    minor_nm, di_node_name(node));
364 
365 	devfspath = di_devfs_path(node);
366 	if (devfspath == NULL) {
367 		devfsadm_print(debug_mid, "agp_process: devfspath is NULL\n");
368 		return (DEVFSADM_CONTINUE);
369 	}
370 
371 	I_path = (char *)malloc(PATH_MAX);
372 
373 	if (I_path == NULL) {
374 		di_devfs_path_free(devfspath);
375 		devfsadm_print(debug_mid,  "agp_process: malloc failed\n");
376 		return (DEVFSADM_CONTINUE);
377 	}
378 
379 	p_path = (char *)malloc(PATH_MAX);
380 
381 	if (p_path == NULL) {
382 		devfsadm_print(debug_mid,  "agp_process: malloc failed\n");
383 		di_devfs_path_free(devfspath);
384 		free(I_path);
385 		return (DEVFSADM_CONTINUE);
386 	}
387 
388 	(void) strlcpy(p_path, devfspath, PATH_MAX);
389 	(void) strlcat(p_path, ":", PATH_MAX);
390 	(void) strlcat(p_path, minor_nm, PATH_MAX);
391 	di_devfs_path_free(devfspath);
392 
393 	devfsadm_print(debug_mid, "agp_process: path %s\n", p_path);
394 
395 	for (i = 0; ; i++) {
396 		if ((driver_name_table[i].driver_name == NULL) ||
397 		    (strcmp(drv_nm, driver_name_table[i].driver_name) == 0)) {
398 			index = driver_name_table[i].index;
399 			break;
400 		}
401 	}
402 	switch (index) {
403 	case DRIVER_AGPPSEUDO:
404 		devfsadm_print(debug_mid,
405 		    "agp_process: psdeudo driver name\n");
406 		name = "agpgart";
407 		(void) snprintf(I_path, PATH_MAX, "%s", name);
408 		devfsadm_print(debug_mid,
409 		    "mklink %s -> %s\n", I_path, p_path);
410 
411 		(void) devfsadm_mklink(I_path, node, minor, 0);
412 
413 		free(I_path);
414 		free(p_path);
415 		return (DEVFSADM_CONTINUE);
416 	case DRIVER_AGPTARGET:
417 		devfsadm_print(debug_mid,
418 		    "agp_process: target driver name\n");
419 		rules[0] = agptarget_rules[0];
420 		name = "agptarget";
421 		break;
422 	case DRIVER_CPUGART:
423 		devfsadm_print(debug_mid,
424 		    "agp_process: cpugart driver name\n");
425 		rules[0] = cpugart_rules[0];
426 		name = "cpugart";
427 		break;
428 	case DRIVER_AGPMASTER_DRM:
429 	case DRIVER_AGPMASTER_VGATEXT:
430 		devfsadm_print(debug_mid,
431 		    "agp_process: agpmaster driver name\n");
432 		rules[0] = agpmaster_rules[0];
433 		name = "agpmaster";
434 		break;
435 	case DRIVER_UNKNOWN:
436 		devfsadm_print(debug_mid,
437 		    "agp_process: unknown driver name=%s\n", drv_nm);
438 		free(I_path);
439 		free(p_path);
440 		return (DEVFSADM_CONTINUE);
441 	}
442 
443 	if (devfsadm_enumerate_int(p_path, 0, &buf, rules, 1)) {
444 		devfsadm_print(debug_mid, "agp_process: exit/coninue\n");
445 		free(I_path);
446 		free(p_path);
447 		return (DEVFSADM_CONTINUE);
448 	}
449 
450 
451 	(void) snprintf(I_path, PATH_MAX, "agp/%s%s", name, buf);
452 
453 	devfsadm_print(debug_mid, "agp_process: p_path=%s buf=%s\n",
454 	    p_path, buf);
455 
456 	free(buf);
457 
458 	devfsadm_print(debug_mid, "mklink %s -> %s\n", I_path, p_path);
459 
460 	(void) devfsadm_mklink(I_path, node, minor, 0);
461 
462 	free(p_path);
463 	free(I_path);
464 
465 	return (DEVFSADM_CONTINUE);
466 }
467 
468 static int
469 drm_node(di_minor_t minor, di_node_t node)
470 {
471 	char *minor_nm, *drv_nm;
472 	char *devfspath;
473 	char *I_path, *p_path, *buf;
474 	char *name = "card";
475 
476 	devfsadm_enumerate_t drm_rules[1] = {"^dri$/^card([0-9]+)$", 1,
477 		MATCH_ALL };
478 
479 
480 	minor_nm = di_minor_name(minor);
481 	drv_nm = di_driver_name(node);
482 	if ((minor_nm == NULL) || (drv_nm == NULL)) {
483 		return (DEVFSADM_CONTINUE);
484 	}
485 
486 	devfsadm_print(debug_mid, "drm_node: minor=%s node=%s type=%s\n",
487 	    minor_nm, di_node_name(node), di_minor_nodetype(minor));
488 
489 	devfspath = di_devfs_path(node);
490 	if (devfspath == NULL) {
491 		devfsadm_print(debug_mid, "drm_node: devfspath is NULL\n");
492 		return (DEVFSADM_CONTINUE);
493 	}
494 
495 	I_path = (char *)malloc(PATH_MAX);
496 
497 	if (I_path == NULL) {
498 		di_devfs_path_free(devfspath);
499 		devfsadm_print(debug_mid,  "drm_node: malloc failed\n");
500 		return (DEVFSADM_CONTINUE);
501 	}
502 
503 	p_path = (char *)malloc(PATH_MAX);
504 
505 	if (p_path == NULL) {
506 		devfsadm_print(debug_mid,  "drm_node: malloc failed\n");
507 		di_devfs_path_free(devfspath);
508 		free(I_path);
509 		return (DEVFSADM_CONTINUE);
510 	}
511 
512 	(void) strlcpy(p_path, devfspath, PATH_MAX);
513 	(void) strlcat(p_path, ":", PATH_MAX);
514 	(void) strlcat(p_path, minor_nm, PATH_MAX);
515 	di_devfs_path_free(devfspath);
516 
517 	devfsadm_print(debug_mid, "drm_node: p_path %s\n", p_path);
518 
519 	if (devfsadm_enumerate_int(p_path, 0, &buf, drm_rules, 1)) {
520 		free(p_path);
521 		devfsadm_print(debug_mid, "drm_node: exit/coninue\n");
522 		return (DEVFSADM_CONTINUE);
523 	}
524 	(void) snprintf(I_path, PATH_MAX, "dri/%s%s", name, buf);
525 
526 	devfsadm_print(debug_mid, "drm_node: p_path=%s buf=%s\n",
527 	    p_path, buf);
528 
529 	free(buf);
530 
531 	devfsadm_print(debug_mid, "mklink %s -> %s\n", I_path, p_path);
532 	(void) devfsadm_mklink(I_path, node, minor, 0);
533 
534 	free(p_path);
535 	free(I_path);
536 
537 	return (0);
538 }
539 
540 /*
541  * /dev/mc/mc<chipid> -> /devices/.../pci1022,1102@<chipid+24>,2:mc-amd
542  */
543 static int
544 mc_node(di_minor_t minor, di_node_t node)
545 {
546 	const char *minorname = di_minor_name(minor);
547 	const char *busaddr = di_bus_addr(node);
548 	char linkpath[PATH_MAX];
549 	int unitaddr;
550 	char *c;
551 
552 	if (minorname == NULL || busaddr == NULL)
553 		return (DEVFSADM_CONTINUE);
554 
555 	errno = 0;
556 	unitaddr = strtol(busaddr, &c, 16);
557 
558 	if (errno != 0)
559 		return (DEVFSADM_CONTINUE);
560 
561 	if (unitaddr == 0) {
562 		(void) snprintf(linkpath, sizeof (linkpath), "mc/mc");
563 	} else if (unitaddr >= MC_AMD_DEV_OFFSET) {
564 		(void) snprintf(linkpath, sizeof (linkpath), "mc/mc%u",
565 		    unitaddr - MC_AMD_DEV_OFFSET);
566 	} else {
567 		return (DEVFSADM_CONTINUE);
568 	}
569 	(void) devfsadm_mklink(linkpath, node, minor, 0);
570 	return (DEVFSADM_CONTINUE);
571 }
572 
573 /*
574  * Creates \M0 devlink for xsvc node
575  */
576 static int
577 xsvc(di_minor_t minor, di_node_t node)
578 {
579 	char *mn;
580 
581 	if (strcmp(di_node_name(node), "xsvc") != 0)
582 		return (DEVFSADM_CONTINUE);
583 
584 	mn = di_minor_name(minor);
585 	if (mn == NULL)
586 		return (DEVFSADM_CONTINUE);
587 
588 	(void) devfsadm_mklink(mn, node, minor, 0);
589 	return (DEVFSADM_CONTINUE);
590 }
591 
592 /*
593  * Creates \M0 devlink for srn device
594  */
595 static int
596 srn(di_minor_t minor, di_node_t node)
597 {
598 	char *mn;
599 
600 	if (strcmp(di_node_name(node), "srn") != 0)
601 		return (DEVFSADM_CONTINUE);
602 
603 	mn = di_minor_name(minor);
604 	if (mn == NULL)
605 		return (DEVFSADM_CONTINUE);
606 
607 	(void) devfsadm_mklink(mn, node, minor, 0);
608 	return (DEVFSADM_CONTINUE);
609 }
610 
611 /*
612  *	/dev/ucode	->	/devices/pseudo/ucode@0:ucode
613  */
614 static int
615 ucode(di_minor_t minor, di_node_t node)
616 {
617 	(void) devfsadm_mklink("ucode", node, minor, 0);
618 	return (DEVFSADM_CONTINUE);
619 }
620