1*3941Svenki /*
2*3941Svenki  * CDDL HEADER START
3*3941Svenki  *
4*3941Svenki  * The contents of this file are subject to the terms of the
5*3941Svenki  * Common Development and Distribution License (the "License").
6*3941Svenki  * You may not use this file except in compliance with the License.
7*3941Svenki  *
8*3941Svenki  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*3941Svenki  * or http://www.opensolaris.org/os/licensing.
10*3941Svenki  * See the License for the specific language governing permissions
11*3941Svenki  * and limitations under the License.
12*3941Svenki  *
13*3941Svenki  * When distributing Covered Code, include this CDDL HEADER in each
14*3941Svenki  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*3941Svenki  * If applicable, add the following below this CDDL HEADER, with the
16*3941Svenki  * fields enclosed by brackets "[]" replaced with your own identifying
17*3941Svenki  * information: Portions Copyright [yyyy] [name of copyright owner]
18*3941Svenki  *
19*3941Svenki  * CDDL HEADER END
20*3941Svenki  */
21*3941Svenki 
22*3941Svenki /*
23*3941Svenki  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*3941Svenki  * Use is subject to license terms.
25*3941Svenki  */
26*3941Svenki 
27*3941Svenki #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*3941Svenki 
29*3941Svenki #include <sys/types.h>
30*3941Svenki #include <errno.h>
31*3941Svenki #include <malloc.h>
32*3941Svenki #include <mdesc.h>
33*3941Svenki #include <pri.h>
34*3941Svenki #include "priplugin.h"
35*3941Svenki 
36*3941Svenki static void pri_free(void *bufp, size_t size);
37*3941Svenki uint64_t *md_bufp;
38*3941Svenki 
39*3941Svenki md_t *
40*3941Svenki pri_devinit(void)
41*3941Svenki {
42*3941Svenki 	md_t *mdp;
43*3941Svenki 	uint64_t tok;
44*3941Svenki 
45*3941Svenki 	md_bufp = NULL;
46*3941Svenki 	tok = 0;
47*3941Svenki 
48*3941Svenki 	if (pri_init() != -1) {
49*3941Svenki 		if (pri_get(PRI_GET, &tok, &md_bufp, malloc, pri_free) ==
50*3941Svenki 		    (ssize_t)-1) {
51*3941Svenki 			pri_debug(LOG_NOTICE, "pri_devinit: can'r read from "
52*3941Svenki 			    "the PRI: %d\n", errno);
53*3941Svenki 		}
54*3941Svenki 		if (md_bufp == NULL) {
55*3941Svenki 			pri_debug(LOG_NOTICE, "pri_devinit: pri_get returned"
56*3941Svenki 			    "NULL buffer!\n");
57*3941Svenki 		}
58*3941Svenki 	} else {
59*3941Svenki 		pri_debug(LOG_NOTICE, "pri_devinit: pri_init failed!\n");
60*3941Svenki 	}
61*3941Svenki 	pri_fini();
62*3941Svenki 
63*3941Svenki 	pri_debug(LOG_NOTICE, "pri_devinit: done reading PRI\n");
64*3941Svenki 
65*3941Svenki 	/*
66*3941Svenki 	 * The PRI and the MD use the same data format so they can be
67*3941Svenki 	 * parsed by the same functions.
68*3941Svenki 	 */
69*3941Svenki 	if (md_bufp) {
70*3941Svenki 		mdp = md_init_intern(md_bufp, malloc, pri_free);
71*3941Svenki 		if (mdp == NULL) {
72*3941Svenki 			pri_debug(LOG_NOTICE, "pri_devinit: md_init_intern "
73*3941Svenki 			"failed\n");
74*3941Svenki 			free(md_bufp);
75*3941Svenki 			md_bufp = NULL;
76*3941Svenki 		} else {
77*3941Svenki 			pri_debug(LOG_NOTICE, "pri_devinit: mdi_init_intern "
78*3941Svenki 			    "completed successfully\n");
79*3941Svenki 		}
80*3941Svenki 	} else
81*3941Svenki 		mdp = NULL;
82*3941Svenki 
83*3941Svenki 	pri_debug(LOG_NOTICE, "pri_devinit: returning\n");
84*3941Svenki 
85*3941Svenki 	return (mdp);
86*3941Svenki }
87*3941Svenki 
88*3941Svenki /*ARGSUSED*/
89*3941Svenki static void
90*3941Svenki pri_free(void *bufp, size_t size)
91*3941Svenki {
92*3941Svenki 	if (bufp)
93*3941Svenki 		free(bufp);
94*3941Svenki }
95*3941Svenki 
96*3941Svenki void
97*3941Svenki pri_devfini(md_t *mdp)
98*3941Svenki {
99*3941Svenki 	if (mdp)
100*3941Svenki 		(void) md_fini(mdp);
101*3941Svenki 
102*3941Svenki 	if (md_bufp)
103*3941Svenki 		free(md_bufp);
104*3941Svenki 	md_bufp = NULL;
105*3941Svenki }
106