1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate #include "cfga_usb.h"
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate #define MAX_FORMAT 80 /* for info table */
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate cfga_usb_ret_t usb_rcm_offline(const char *, char **, char *,
36*0Sstevel@tonic-gate cfga_flags_t);
37*0Sstevel@tonic-gate cfga_usb_ret_t usb_rcm_online(const char *, char **, char *,
38*0Sstevel@tonic-gate cfga_flags_t);
39*0Sstevel@tonic-gate cfga_usb_ret_t usb_rcm_remove(const char *, char **, char *,
40*0Sstevel@tonic-gate cfga_flags_t);
41*0Sstevel@tonic-gate static cfga_usb_ret_t usb_rcm_info_table(rcm_info_t *, char **);
42*0Sstevel@tonic-gate static cfga_usb_ret_t usb_rcm_init(const char *, cfga_flags_t, char **,
43*0Sstevel@tonic-gate uint_t *);
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate static rcm_handle_t *rcm_handle = NULL;
47*0Sstevel@tonic-gate static mutex_t rcm_handle_lock = DEFAULTMUTEX;
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate * usb_rcm_offline:
52*0Sstevel@tonic-gate * Offline USB resource consumers.
53*0Sstevel@tonic-gate */
54*0Sstevel@tonic-gate cfga_usb_ret_t
usb_rcm_offline(const char * rsrc,char ** errstring,char * rsrc_fixed,cfga_flags_t flags)55*0Sstevel@tonic-gate usb_rcm_offline(const char *rsrc, char **errstring, char *rsrc_fixed,
56*0Sstevel@tonic-gate cfga_flags_t flags)
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate int rret;
59*0Sstevel@tonic-gate uint_t rflags = 0;
60*0Sstevel@tonic-gate rcm_info_t *rinfo = NULL;
61*0Sstevel@tonic-gate cfga_usb_ret_t ret = CFGA_USB_OK;
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate DPRINTF("usb_rcm_offline:\n");
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate if ((ret = usb_rcm_init(rsrc, flags, errstring, &rflags)) !=
66*0Sstevel@tonic-gate CFGA_USB_OK) {
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate return (ret);
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate DPRINTF("usb_rcm_offline: rsrc_fixed: %s\n", rsrc_fixed);
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate if ((rret = rcm_request_offline(rcm_handle, rsrc_fixed, rflags, &rinfo))
74*0Sstevel@tonic-gate != RCM_SUCCESS) {
75*0Sstevel@tonic-gate DPRINTF("usb_rcm_offline: rcm_request_offline failed\n");
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate if (rinfo) {
78*0Sstevel@tonic-gate (void) usb_rcm_info_table(rinfo, errstring);
79*0Sstevel@tonic-gate rcm_free_info(rinfo);
80*0Sstevel@tonic-gate rinfo = NULL;
81*0Sstevel@tonic-gate }
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gate DPRINTF("usb_rcm_offline: table = %s\n", *errstring);
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate if (rret == RCM_FAILURE) {
86*0Sstevel@tonic-gate (void) usb_rcm_online(rsrc, errstring,
87*0Sstevel@tonic-gate rsrc_fixed, flags);
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate ret = CFGA_USB_RCM_OFFLINE;
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate return (ret);
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate * usb_rcm_online:
98*0Sstevel@tonic-gate * Online USB resource consumers that were previously offlined.
99*0Sstevel@tonic-gate */
100*0Sstevel@tonic-gate cfga_usb_ret_t
usb_rcm_online(const char * rsrc,char ** errstring,char * rsrc_fixed,cfga_flags_t flags)101*0Sstevel@tonic-gate usb_rcm_online(const char *rsrc, char **errstring, char *rsrc_fixed,
102*0Sstevel@tonic-gate cfga_flags_t flags)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate rcm_info_t *rinfo = NULL;
105*0Sstevel@tonic-gate cfga_usb_ret_t ret = CFGA_USB_OK;
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate DPRINTF("usb_rcm_online:\n");
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate if ((ret = usb_rcm_init(rsrc, flags, errstring, NULL)) != CFGA_USB_OK) {
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate return (ret);
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate if (rcm_notify_online(rcm_handle, rsrc_fixed, 0, &rinfo) !=
115*0Sstevel@tonic-gate RCM_SUCCESS && (rinfo != NULL)) {
116*0Sstevel@tonic-gate DPRINTF("usb_rcm_online: rcm_notify_online failed\n");
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate (void) usb_rcm_info_table(rinfo, errstring);
119*0Sstevel@tonic-gate rcm_free_info(rinfo);
120*0Sstevel@tonic-gate rinfo = NULL;
121*0Sstevel@tonic-gate ret = CFGA_USB_RCM_ONLINE;
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate return (ret);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate /*
129*0Sstevel@tonic-gate * usb_rcm_remove:
130*0Sstevel@tonic-gate * Remove USB resource consumers after their kernel removal.
131*0Sstevel@tonic-gate */
132*0Sstevel@tonic-gate cfga_usb_ret_t
usb_rcm_remove(const char * rsrc,char ** errstring,char * rsrc_fixed,cfga_flags_t flags)133*0Sstevel@tonic-gate usb_rcm_remove(const char *rsrc, char **errstring, char *rsrc_fixed,
134*0Sstevel@tonic-gate cfga_flags_t flags)
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate rcm_info_t *rinfo = NULL;
137*0Sstevel@tonic-gate cfga_usb_ret_t ret = CFGA_USB_OK;
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate DPRINTF("usb_rcm_remove:\n");
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate if ((ret = usb_rcm_init(rsrc, flags, errstring, NULL)) != CFGA_USB_OK) {
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate return (ret);
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate if (rcm_notify_remove(rcm_handle, rsrc_fixed, 0, &rinfo) !=
147*0Sstevel@tonic-gate RCM_SUCCESS && (rinfo != NULL)) {
148*0Sstevel@tonic-gate DPRINTF("usb_rcm_remove: rcm_notify_remove failed\n");
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate (void) usb_rcm_info_table(rinfo, errstring);
151*0Sstevel@tonic-gate rcm_free_info(rinfo);
152*0Sstevel@tonic-gate rinfo = NULL;
153*0Sstevel@tonic-gate ret = CFGA_USB_RCM_ONLINE;
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate return (ret);
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate /*
161*0Sstevel@tonic-gate * usb_rcm_init:
162*0Sstevel@tonic-gate * Contains common initialization code for entering a usb_rcm_xx() routine.
163*0Sstevel@tonic-gate */
164*0Sstevel@tonic-gate /* ARGSUSED */
165*0Sstevel@tonic-gate static cfga_usb_ret_t
usb_rcm_init(const char * rsrc,cfga_flags_t flags,char ** errstring,uint_t * rflags)166*0Sstevel@tonic-gate usb_rcm_init(const char *rsrc, cfga_flags_t flags, char **errstring,
167*0Sstevel@tonic-gate uint_t *rflags)
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate DPRINTF("usb_rcm_init:\n");
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate /* Validate the rsrc argument */
172*0Sstevel@tonic-gate if (rsrc == NULL) {
173*0Sstevel@tonic-gate DPRINTF("usb_rcm_init: rsrc is NULL\n");
174*0Sstevel@tonic-gate return (CFGA_USB_INTERNAL_ERROR);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate /* Translate the cfgadm flags to RCM flags */
178*0Sstevel@tonic-gate if (rflags && (flags & CFGA_FLAG_FORCE)) {
179*0Sstevel@tonic-gate *rflags |= RCM_FORCE;
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate /* Get a handle for the RCM operations */
183*0Sstevel@tonic-gate (void) mutex_lock(&rcm_handle_lock);
184*0Sstevel@tonic-gate if (rcm_handle == NULL) {
185*0Sstevel@tonic-gate if (rcm_alloc_handle(NULL, RCM_NOPID, NULL, &rcm_handle) !=
186*0Sstevel@tonic-gate RCM_SUCCESS) {
187*0Sstevel@tonic-gate DPRINTF("usb_rcm_init: alloc_handle failed\n");
188*0Sstevel@tonic-gate (void) mutex_unlock(&rcm_handle_lock);
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate return (CFGA_USB_RCM_HANDLE);
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate }
193*0Sstevel@tonic-gate (void) mutex_unlock(&rcm_handle_lock);
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gate return (CFGA_USB_OK);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gate /*
200*0Sstevel@tonic-gate * usb_rcm_info_table:
201*0Sstevel@tonic-gate * Takes an opaque rcm_info_t pointer and a character pointer,
202*0Sstevel@tonic-gate * and appends the rcm_info_t data in the form of a table to the
203*0Sstevel@tonic-gate * given character pointer.
204*0Sstevel@tonic-gate */
205*0Sstevel@tonic-gate static cfga_usb_ret_t
usb_rcm_info_table(rcm_info_t * rinfo,char ** table)206*0Sstevel@tonic-gate usb_rcm_info_table(rcm_info_t *rinfo, char **table)
207*0Sstevel@tonic-gate {
208*0Sstevel@tonic-gate int i;
209*0Sstevel@tonic-gate size_t w;
210*0Sstevel@tonic-gate size_t width = 0;
211*0Sstevel@tonic-gate size_t w_rsrc = 0;
212*0Sstevel@tonic-gate size_t w_info = 0;
213*0Sstevel@tonic-gate size_t table_size = 0;
214*0Sstevel@tonic-gate uint_t tuples = 0;
215*0Sstevel@tonic-gate rcm_info_tuple_t *tuple = NULL;
216*0Sstevel@tonic-gate char *rsrc;
217*0Sstevel@tonic-gate char *info;
218*0Sstevel@tonic-gate char *newtable;
219*0Sstevel@tonic-gate static char format[MAX_FORMAT];
220*0Sstevel@tonic-gate const char *infostr;
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate DPRINTF("usb_rcm_info_table:\n");
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gate /* Protect against invalid arguments */
225*0Sstevel@tonic-gate if (rinfo == NULL || table == NULL) {
226*0Sstevel@tonic-gate return (CFGA_USB_INTERNAL_ERROR);
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate
229*0Sstevel@tonic-gate /* Set localized table header strings */
230*0Sstevel@tonic-gate rsrc = dgettext(TEXT_DOMAIN, "Resource");
231*0Sstevel@tonic-gate info = dgettext(TEXT_DOMAIN, "Information");
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate /* A first pass, to size up the RCM information */
234*0Sstevel@tonic-gate while (tuple = rcm_info_next(rinfo, tuple)) {
235*0Sstevel@tonic-gate if ((infostr = rcm_info_info(tuple)) != NULL) {
236*0Sstevel@tonic-gate tuples++;
237*0Sstevel@tonic-gate if ((w = strlen(rcm_info_rsrc(tuple))) > w_rsrc)
238*0Sstevel@tonic-gate w_rsrc = w;
239*0Sstevel@tonic-gate if ((w = strlen(infostr)) > w_info)
240*0Sstevel@tonic-gate w_info = w;
241*0Sstevel@tonic-gate }
242*0Sstevel@tonic-gate }
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate /* If nothing was sized up above, stop early */
245*0Sstevel@tonic-gate if (tuples == 0) {
246*0Sstevel@tonic-gate DPRINTF("usb_rcm_info_table: no tuples\n");
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate return (CFGA_USB_OK);
249*0Sstevel@tonic-gate }
250*0Sstevel@tonic-gate
251*0Sstevel@tonic-gate /* Adjust column widths for column headings */
252*0Sstevel@tonic-gate if ((w = strlen(rsrc)) > w_rsrc) {
253*0Sstevel@tonic-gate w_rsrc = w;
254*0Sstevel@tonic-gate } else if ((w_rsrc - w) % 2) {
255*0Sstevel@tonic-gate w_rsrc++;
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gate if ((w = strlen(info)) > w_info) {
259*0Sstevel@tonic-gate w_info = w;
260*0Sstevel@tonic-gate } else if ((w_info - w) % 2) {
261*0Sstevel@tonic-gate w_info++;
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gate /*
265*0Sstevel@tonic-gate * Compute the total line width of each line,
266*0Sstevel@tonic-gate * accounting for intercolumn spacing.
267*0Sstevel@tonic-gate */
268*0Sstevel@tonic-gate width = w_info + w_rsrc + 4;
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate /* Allocate space for the table */
271*0Sstevel@tonic-gate table_size = (2 + tuples) * (width + 1) + 2;
272*0Sstevel@tonic-gate if (*table == NULL) {
273*0Sstevel@tonic-gate /* zero fill for the strcat() call below */
274*0Sstevel@tonic-gate *table = calloc(table_size, sizeof (char));
275*0Sstevel@tonic-gate if (*table == NULL) {
276*0Sstevel@tonic-gate DPRINTF("usb_rcm_info_table: no table\n");
277*0Sstevel@tonic-gate
278*0Sstevel@tonic-gate return (CFGA_USB_ALLOC_FAIL);
279*0Sstevel@tonic-gate }
280*0Sstevel@tonic-gate } else {
281*0Sstevel@tonic-gate newtable = realloc(*table, strlen(*table) + table_size);
282*0Sstevel@tonic-gate if (newtable == NULL) {
283*0Sstevel@tonic-gate DPRINTF("usb_rcm_info_table: no new table\n");
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate return (CFGA_USB_ALLOC_FAIL);
286*0Sstevel@tonic-gate } else {
287*0Sstevel@tonic-gate *table = newtable;
288*0Sstevel@tonic-gate }
289*0Sstevel@tonic-gate }
290*0Sstevel@tonic-gate
291*0Sstevel@tonic-gate /* Place a table header into the string */
292*0Sstevel@tonic-gate
293*0Sstevel@tonic-gate /* The resource header */
294*0Sstevel@tonic-gate (void) strcat(*table, "\n");
295*0Sstevel@tonic-gate w = strlen(rsrc);
296*0Sstevel@tonic-gate
297*0Sstevel@tonic-gate for (i = 0; i < ((w_rsrc - w) / 2); i++) {
298*0Sstevel@tonic-gate (void) strcat(*table, " ");
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate (void) strcat(*table, rsrc);
301*0Sstevel@tonic-gate
302*0Sstevel@tonic-gate for (i = 0; i < ((w_rsrc - w) / 2); i++) {
303*0Sstevel@tonic-gate (void) strcat(*table, " ");
304*0Sstevel@tonic-gate }
305*0Sstevel@tonic-gate
306*0Sstevel@tonic-gate /* The information header */
307*0Sstevel@tonic-gate (void) strcat(*table, " ");
308*0Sstevel@tonic-gate w = strlen(info);
309*0Sstevel@tonic-gate for (i = 0; i < ((w_info - w) / 2); i++) {
310*0Sstevel@tonic-gate (void) strcat(*table, " ");
311*0Sstevel@tonic-gate }
312*0Sstevel@tonic-gate (void) strcat(*table, info);
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate for (i = 0; i < ((w_info - w) / 2); i++) {
315*0Sstevel@tonic-gate (void) strcat(*table, " ");
316*0Sstevel@tonic-gate }
317*0Sstevel@tonic-gate
318*0Sstevel@tonic-gate (void) strcat(*table, "\n");
319*0Sstevel@tonic-gate
320*0Sstevel@tonic-gate /* Underline the headers */
321*0Sstevel@tonic-gate for (i = 0; i < w_rsrc; i++) {
322*0Sstevel@tonic-gate (void) strcat(*table, "-");
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate
325*0Sstevel@tonic-gate (void) strcat(*table, " ");
326*0Sstevel@tonic-gate for (i = 0; i < w_info; i++) {
327*0Sstevel@tonic-gate (void) strcat(*table, "-");
328*0Sstevel@tonic-gate }
329*0Sstevel@tonic-gate
330*0Sstevel@tonic-gate (void) strcat(*table, "\n");
331*0Sstevel@tonic-gate
332*0Sstevel@tonic-gate /* Construct the format string */
333*0Sstevel@tonic-gate (void) snprintf(format, MAX_FORMAT, "%%-%ds %%-%ds",
334*0Sstevel@tonic-gate (int)w_rsrc, (int)w_info);
335*0Sstevel@tonic-gate
336*0Sstevel@tonic-gate /* Add the tuples to the table string */
337*0Sstevel@tonic-gate tuple = NULL;
338*0Sstevel@tonic-gate while ((tuple = rcm_info_next(rinfo, tuple)) != NULL) {
339*0Sstevel@tonic-gate if ((infostr = rcm_info_info(tuple)) != NULL) {
340*0Sstevel@tonic-gate (void) sprintf(&((*table)[strlen(*table)]),
341*0Sstevel@tonic-gate format, rcm_info_rsrc(tuple), infostr);
342*0Sstevel@tonic-gate (void) strcat(*table, "\n");
343*0Sstevel@tonic-gate }
344*0Sstevel@tonic-gate }
345*0Sstevel@tonic-gate
346*0Sstevel@tonic-gate return (CFGA_USB_OK);
347*0Sstevel@tonic-gate }
348