8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / cfgadm_plugins / usb / common / cfga_rcm.c
blobd7d9a2f9affc3f86656675f45bc3fd24e3dc2a9e
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include "cfga_usb.h"
33 #define MAX_FORMAT 80 /* for info table */
35 cfga_usb_ret_t usb_rcm_offline(const char *, char **, char *,
36 cfga_flags_t);
37 cfga_usb_ret_t usb_rcm_online(const char *, char **, char *,
38 cfga_flags_t);
39 cfga_usb_ret_t usb_rcm_remove(const char *, char **, char *,
40 cfga_flags_t);
41 static cfga_usb_ret_t usb_rcm_info_table(rcm_info_t *, char **);
42 static cfga_usb_ret_t usb_rcm_init(const char *, cfga_flags_t, char **,
43 uint_t *);
46 static rcm_handle_t *rcm_handle = NULL;
47 static mutex_t rcm_handle_lock = DEFAULTMUTEX;
51 * usb_rcm_offline:
52 * Offline USB resource consumers.
54 cfga_usb_ret_t
55 usb_rcm_offline(const char *rsrc, char **errstring, char *rsrc_fixed,
56 cfga_flags_t flags)
58 int rret;
59 uint_t rflags = 0;
60 rcm_info_t *rinfo = NULL;
61 cfga_usb_ret_t ret = CFGA_USB_OK;
63 DPRINTF("usb_rcm_offline:\n");
65 if ((ret = usb_rcm_init(rsrc, flags, errstring, &rflags)) !=
66 CFGA_USB_OK) {
68 return (ret);
71 DPRINTF("usb_rcm_offline: rsrc_fixed: %s\n", rsrc_fixed);
73 if ((rret = rcm_request_offline(rcm_handle, rsrc_fixed, rflags, &rinfo))
74 != RCM_SUCCESS) {
75 DPRINTF("usb_rcm_offline: rcm_request_offline failed\n");
77 if (rinfo) {
78 (void) usb_rcm_info_table(rinfo, errstring);
79 rcm_free_info(rinfo);
80 rinfo = NULL;
83 DPRINTF("usb_rcm_offline: table = %s\n", *errstring);
85 if (rret == RCM_FAILURE) {
86 (void) usb_rcm_online(rsrc, errstring,
87 rsrc_fixed, flags);
89 ret = CFGA_USB_RCM_OFFLINE;
92 return (ret);
97 * usb_rcm_online:
98 * Online USB resource consumers that were previously offlined.
100 cfga_usb_ret_t
101 usb_rcm_online(const char *rsrc, char **errstring, char *rsrc_fixed,
102 cfga_flags_t flags)
104 rcm_info_t *rinfo = NULL;
105 cfga_usb_ret_t ret = CFGA_USB_OK;
107 DPRINTF("usb_rcm_online:\n");
109 if ((ret = usb_rcm_init(rsrc, flags, errstring, NULL)) != CFGA_USB_OK) {
111 return (ret);
114 if (rcm_notify_online(rcm_handle, rsrc_fixed, 0, &rinfo) !=
115 RCM_SUCCESS && (rinfo != NULL)) {
116 DPRINTF("usb_rcm_online: rcm_notify_online failed\n");
118 (void) usb_rcm_info_table(rinfo, errstring);
119 rcm_free_info(rinfo);
120 rinfo = NULL;
121 ret = CFGA_USB_RCM_ONLINE;
124 return (ret);
129 * usb_rcm_remove:
130 * Remove USB resource consumers after their kernel removal.
132 cfga_usb_ret_t
133 usb_rcm_remove(const char *rsrc, char **errstring, char *rsrc_fixed,
134 cfga_flags_t flags)
136 rcm_info_t *rinfo = NULL;
137 cfga_usb_ret_t ret = CFGA_USB_OK;
139 DPRINTF("usb_rcm_remove:\n");
141 if ((ret = usb_rcm_init(rsrc, flags, errstring, NULL)) != CFGA_USB_OK) {
143 return (ret);
146 if (rcm_notify_remove(rcm_handle, rsrc_fixed, 0, &rinfo) !=
147 RCM_SUCCESS && (rinfo != NULL)) {
148 DPRINTF("usb_rcm_remove: rcm_notify_remove failed\n");
150 (void) usb_rcm_info_table(rinfo, errstring);
151 rcm_free_info(rinfo);
152 rinfo = NULL;
153 ret = CFGA_USB_RCM_ONLINE;
156 return (ret);
161 * usb_rcm_init:
162 * Contains common initialization code for entering a usb_rcm_xx() routine.
164 /* ARGSUSED */
165 static cfga_usb_ret_t
166 usb_rcm_init(const char *rsrc, cfga_flags_t flags, char **errstring,
167 uint_t *rflags)
169 DPRINTF("usb_rcm_init:\n");
171 /* Validate the rsrc argument */
172 if (rsrc == NULL) {
173 DPRINTF("usb_rcm_init: rsrc is NULL\n");
174 return (CFGA_USB_INTERNAL_ERROR);
177 /* Translate the cfgadm flags to RCM flags */
178 if (rflags && (flags & CFGA_FLAG_FORCE)) {
179 *rflags |= RCM_FORCE;
182 /* Get a handle for the RCM operations */
183 (void) mutex_lock(&rcm_handle_lock);
184 if (rcm_handle == NULL) {
185 if (rcm_alloc_handle(NULL, RCM_NOPID, NULL, &rcm_handle) !=
186 RCM_SUCCESS) {
187 DPRINTF("usb_rcm_init: alloc_handle failed\n");
188 (void) mutex_unlock(&rcm_handle_lock);
190 return (CFGA_USB_RCM_HANDLE);
193 (void) mutex_unlock(&rcm_handle_lock);
195 return (CFGA_USB_OK);
200 * usb_rcm_info_table:
201 * Takes an opaque rcm_info_t pointer and a character pointer,
202 * and appends the rcm_info_t data in the form of a table to the
203 * given character pointer.
205 static cfga_usb_ret_t
206 usb_rcm_info_table(rcm_info_t *rinfo, char **table)
208 int i;
209 size_t w;
210 size_t width = 0;
211 size_t w_rsrc = 0;
212 size_t w_info = 0;
213 size_t table_size = 0;
214 uint_t tuples = 0;
215 rcm_info_tuple_t *tuple = NULL;
216 char *rsrc;
217 char *info;
218 char *newtable;
219 static char format[MAX_FORMAT];
220 const char *infostr;
222 DPRINTF("usb_rcm_info_table:\n");
224 /* Protect against invalid arguments */
225 if (rinfo == NULL || table == NULL) {
226 return (CFGA_USB_INTERNAL_ERROR);
229 /* Set localized table header strings */
230 rsrc = dgettext(TEXT_DOMAIN, "Resource");
231 info = dgettext(TEXT_DOMAIN, "Information");
233 /* A first pass, to size up the RCM information */
234 while (tuple = rcm_info_next(rinfo, tuple)) {
235 if ((infostr = rcm_info_info(tuple)) != NULL) {
236 tuples++;
237 if ((w = strlen(rcm_info_rsrc(tuple))) > w_rsrc)
238 w_rsrc = w;
239 if ((w = strlen(infostr)) > w_info)
240 w_info = w;
244 /* If nothing was sized up above, stop early */
245 if (tuples == 0) {
246 DPRINTF("usb_rcm_info_table: no tuples\n");
248 return (CFGA_USB_OK);
251 /* Adjust column widths for column headings */
252 if ((w = strlen(rsrc)) > w_rsrc) {
253 w_rsrc = w;
254 } else if ((w_rsrc - w) % 2) {
255 w_rsrc++;
258 if ((w = strlen(info)) > w_info) {
259 w_info = w;
260 } else if ((w_info - w) % 2) {
261 w_info++;
265 * Compute the total line width of each line,
266 * accounting for intercolumn spacing.
268 width = w_info + w_rsrc + 4;
270 /* Allocate space for the table */
271 table_size = (2 + tuples) * (width + 1) + 2;
272 if (*table == NULL) {
273 /* zero fill for the strcat() call below */
274 *table = calloc(table_size, sizeof (char));
275 if (*table == NULL) {
276 DPRINTF("usb_rcm_info_table: no table\n");
278 return (CFGA_USB_ALLOC_FAIL);
280 } else {
281 newtable = realloc(*table, strlen(*table) + table_size);
282 if (newtable == NULL) {
283 DPRINTF("usb_rcm_info_table: no new table\n");
285 return (CFGA_USB_ALLOC_FAIL);
286 } else {
287 *table = newtable;
291 /* Place a table header into the string */
293 /* The resource header */
294 (void) strcat(*table, "\n");
295 w = strlen(rsrc);
297 for (i = 0; i < ((w_rsrc - w) / 2); i++) {
298 (void) strcat(*table, " ");
300 (void) strcat(*table, rsrc);
302 for (i = 0; i < ((w_rsrc - w) / 2); i++) {
303 (void) strcat(*table, " ");
306 /* The information header */
307 (void) strcat(*table, " ");
308 w = strlen(info);
309 for (i = 0; i < ((w_info - w) / 2); i++) {
310 (void) strcat(*table, " ");
312 (void) strcat(*table, info);
314 for (i = 0; i < ((w_info - w) / 2); i++) {
315 (void) strcat(*table, " ");
318 (void) strcat(*table, "\n");
320 /* Underline the headers */
321 for (i = 0; i < w_rsrc; i++) {
322 (void) strcat(*table, "-");
325 (void) strcat(*table, " ");
326 for (i = 0; i < w_info; i++) {
327 (void) strcat(*table, "-");
330 (void) strcat(*table, "\n");
332 /* Construct the format string */
333 (void) snprintf(format, MAX_FORMAT, "%%-%ds %%-%ds",
334 (int)w_rsrc, (int)w_info);
336 /* Add the tuples to the table string */
337 tuple = NULL;
338 while ((tuple = rcm_info_next(rinfo, tuple)) != NULL) {
339 if ((infostr = rcm_info_info(tuple)) != NULL) {
340 (void) sprintf(&((*table)[strlen(*table)]),
341 format, rcm_info_rsrc(tuple), infostr);
342 (void) strcat(*table, "\n");
346 return (CFGA_USB_OK);