xref: /illumos-gate/usr/src/lib/libsmedia/plugins/blkdev/common/b_generic.c (revision 0245b61fd282e95735b173b8d95be0d6688163b4)
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 (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  * b_generic.c :
27  *      This file contains the functions for generic block devices
28  *	for libsmedia.
29  */
30 
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <locale.h>
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/dkio.h>
37 #include <string.h>
38 #include "../../../library/inc/smedia.h"
39 #include "../../../library/inc/rmedia.h"
40 #include "../../../library/common/l_defines.h"
41 
42 #define	PERROR(string)	my_perror(gettext(string))
43 
44 static void
45 my_perror(char *err_string)
46 {
47 
48 	int error_no;
49 	if (errno == 0)
50 		return;
51 
52 	error_no = errno;
53 	(void) fprintf(stderr, gettext(err_string));
54 	(void) fprintf(stderr, gettext(" : "));
55 	errno = error_no;
56 	perror("");
57 }
58 
59 int32_t
60 _m_version_no(void)
61 {
62 	return (SM_BLKDEV_VERSION_1);
63 }
64 
65 int32_t
66 _m_device_type(ushort_t ctype, ushort_t mtype)
67 {
68 	if (ctype == DKC_BLKDEV) {
69 		if (mtype == 0)
70 			return (0);
71 	}
72 	return (-1);
73 }
74 
75 
76 int32_t
77 _m_get_media_info(rmedia_handle_t *handle, void *ip)
78 {
79 	smmedium_prop_t *mp = (smmedium_prop_t *)ip;
80 	struct dk_geom		dkg;
81 	struct dk_minfo		minfo;
82 	enum dkio_state		state = DKIO_NONE;
83 	int			ret_val;
84 
85 	if (handle == NULL) {
86 		DPRINTF("Null Handle\n");
87 		errno = EINVAL;
88 		return (-1);
89 	}
90 	if (handle->sm_signature != (int32_t)LIBSMEDIA_SIGNATURE) {
91 		DPRINTF2("Signature expected=0x%x, found=0x%x\n",
92 		    LIBSMEDIA_SIGNATURE, handle->sm_signature);
93 		errno = EINVAL;
94 		return (-1);
95 	}
96 
97 	if (ioctl(handle->sm_fd, DKIOCSTATE, &state) < 0) {
98 		PERROR("DKIOCSTATE failed");
99 		return (-1);
100 	}
101 
102 	if (state != DKIO_INSERTED) {
103 		DPRINTF("No media.\n");
104 		mp->sm_media_type = SM_NOT_PRESENT;
105 		mp->sm_version = SMMEDIA_PROP_V_1;
106 		return (0);
107 
108 	}
109 
110 	ret_val = ioctl(handle->sm_fd, DKIOCGMEDIAINFO, &minfo);
111 	if (ret_val < 0) {
112 		DPRINTF("DKIOCGMEDIAINFO ioctl failed");
113 		return (ret_val);
114 	}
115 	ret_val = ioctl(handle->sm_fd, DKIOCGGEOM, &dkg);
116 	if (ret_val < 0) {
117 		DPRINTF("DKIOCGGEOM ioctl failed");
118 		return (ret_val);
119 	}
120 
121 	mp->sm_media_type = SM_BLOCK;
122 	mp->sm_blocksize = minfo.dki_lbsize;
123 	mp->sm_capacity = minfo.dki_capacity;
124 	mp->sm_pcyl = dkg.dkg_pcyl;
125 	mp->sm_nhead = dkg.dkg_nhead;
126 	mp->sm_nsect = dkg.dkg_nsect;
127 	return (0);
128 }
129 
130 
131 
132 /* ARGSUSED0 */
133 
134 int32_t
135 _m_get_device_info(rmedia_handle_t *handle, void *ip)
136 {
137 	smdevice_info_t *mp = (smdevice_info_t *)ip;
138 	char *vendor_name, *product_name, *fw_version;
139 
140 	if (handle == NULL) {
141 		DPRINTF("Null Handle\n");
142 		errno = EINVAL;
143 		return (-1);
144 	}
145 	if (handle->sm_signature != (int32_t)LIBSMEDIA_SIGNATURE) {
146 		DPRINTF2("Signature expected=0x%x, found=0x%x\n",
147 		    LIBSMEDIA_SIGNATURE, handle->sm_signature);
148 		errno = EINVAL;
149 		return (-1);
150 	}
151 	vendor_name = (char *)malloc(1);
152 	if (vendor_name == NULL) {
153 		if (!errno)
154 			errno = ENOMEM;
155 		return (-1);
156 	}
157 	product_name = (char *)malloc(1);
158 	if (product_name == NULL) {
159 		free(vendor_name);
160 		if (!errno)
161 			errno = ENOMEM;
162 		return (-1);
163 	}
164 
165 	fw_version = (char *)malloc(1);
166 	if (fw_version == NULL) {
167 		free(vendor_name);
168 		free(product_name);
169 			if (!errno)
170 		errno = ENOMEM;
171 		return (-1);
172 	}
173 
174 	/* Note: we could potentially offer more here */
175 	vendor_name[0] = 0;
176 	product_name[0] = 0;
177 	fw_version[0] = 0;
178 	mp->sm_interface_type = IF_BLOCK;
179 	mp->sm_vendor_name = vendor_name;
180 	mp->sm_product_name = product_name;
181 	mp->sm_firmware_version = fw_version;
182 	return (0);
183 }
184 
185 int32_t
186 _m_free_device_info(rmedia_handle_t *handle, void *ip)
187 {
188 	struct smdevice_info *dev_info = ip;
189 
190 	/* Check for valid handle */
191 	if (handle == NULL) {
192 		DPRINTF("Null Handle\n");
193 		errno = EINVAL;
194 		return (-1);
195 	}
196 	if (handle->sm_signature != LIBSMEDIA_SIGNATURE) {
197 		DPRINTF("Invalid signature in handle.\n");
198 		errno = EINVAL;
199 		return (-1);
200 	}
201 
202 	free(dev_info->sm_vendor_name);
203 	free(dev_info->sm_product_name);
204 	free(dev_info->sm_firmware_version);
205 	return (0);
206 }
207