cfga_usb.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "cfga_usb.h"
/* function prototypes */
extern cfga_usb_ret_t usb_rcm_offline(const char *, char **, char *,
extern cfga_usb_ret_t usb_rcm_online(const char *, char **, char *,
extern cfga_usb_ret_t usb_rcm_remove(const char *, char **, char *,
static int usb_confirm(struct cfga_confirm *, char *);
static char *usb_get_devicepath(const char *);
/*
* This file contains the entry points to the plugin as defined in the
* config_admin(3X) man page.
*/
/*
* Set the version number for the cfgadm library's use.
*/
int cfga_version = CFGA_HSL_V2;
#define HELP_HEADER 1
#define HELP_CONFIG 2
#define HELP_RESET_SLOT 3
#define HELP_CONFIG_SLOT 4
#define HELP_UNKNOWN 5
/* Help messages */
static char *
usb_help[] = {
NULL,
"USB specific commands:\n",
" cfgadm -c [configure|unconfigure|disconnect] ap_id [ap_id...]\n",
" cfgadm -x usb_reset ap_id [ap_id...]\n",
" cfgadm -x usb_config -o config=<index of desired configuration> ap_id\n",
"\tunknown command or option: ",
}; /* End help messages */
/* Error messages */
static msgcvt_t
usb_error_msgs[] = {
/* CFGA_USB_OK */
/* CFGA_USB_UNKNOWN */
/* CFGA_USB_INTERNAL_ERROR */
/* CFGA_USB_OPTIONS */
/* CFGA_USB_DYNAMIC_AP */
/* CFGA_USB_AP */
/* CFGA_USB_PORT */
/* CFGA_USB_DEVCTL */
/* CFGA_USB_NOT_CONNECTED */
/* CFGA_USB_NOT_CONFIGURED */
/* CFGA_USB_ALREADY_CONNECTED */
"Device already connected; cannot connect again " },
/* CFGA_USB_ALREADY_CONFIGURED */
/* CFGA_USB_OPEN */
/* CFGA_USB_IOCTL */
/* CFGA_USB_BUSY */
/* CFGA_USB_ALLOC_FAIL */
/* CFGA_USB_OPNOTSUPP */
/* CFGA_USB_DEVLINK */
/* CFGA_USB_STATE */
/* CFGA_USB_CONFIG_INVAL */
{ CVT, CFGA_ERROR,
"Specified configuration index unrecognized or exceeds "
"maximum available" },
/* CFGA_USB_PRIV */
/* CFGA_USB_NVLIST */
/* CFGA_USB_ZEROLEN */
/* CFGA_USB_CONFIG_FILE */
{ CVT, CFGA_ERROR,
/* CFGA_USB_LOCK_FILE */
/* CFGA_USB_UNLOCK_FILE */
/* CFGA_USB_ONE_CONFIG */
{ CVT, CFGA_ERROR,
"Operation not supported for devices with one configuration" },
/* CFGA_USB_RCM_HANDLE Errors */
/* CFGA_USB_RCM_ONLINE */
/* CFGA_USB_RCM_OFFLINE */
/* CFGA_USB_RCM_INFO */
}; /* End error messages */
/* ========================================================================= */
/*
* The next two funcs imported verbatim from cfgadm_scsi.
* physpath_to_devlink is the only func directly used by cfgadm_usb.
* get_link supports it.
*/
/*
* Routine to search the /dev directory or a subtree of /dev.
*/
static int
{
/*
* When path is specified, it's the node path without minor
* name. Therefore, the ../.. prefixes needs to be stripped.
*/
/* line content must have minor node */
return (DI_WALK_CONTINUE);
}
}
return (DI_WALK_TERMINATE);
}
/* ARGSUSED */
static ucfga_ret_t
const char *basedir,
const char *node_path,
char **logpp,
int *l_errnop,
int match_minor)
{
char *minor_path;
return (UCFGA_LIB_ERR);
}
if (match_minor) {
} else {
minor_path = NULL;
}
return (UCFGA_LIB_ERR);
}
return (UCFGA_OK);
}
/* ========================================================================= */
/* Utilities */
/*
* Given the index into a table (msgcvt_t) of messages, get the message
* string, converting it to the proper locale if necessary.
* NOTE: See cfga_usb.h
*/
static const char *
{
}
}
/*
* Allocates and creates a message string (in *ret_str),
* by concatenating all the (char *) args together, in order.
* Last arg MUST be NULL.
*/
static void
{
char *str;
/* We're screwed */
DPRINTF("set_msg: realloc failed.\n");
return;
}
}
}
/*
* Error message handling.
* For the rv passed in, looks up the corresponding error message string(s),
* internationalized it if necessary, and concatenates it into a new
* memory buffer, and points *errstring to it.
* Note not all rvs will result in an error message return, as not all
* error conditions warrant a USB-specific error message.
*
* Some messages may display ap_id or errno, which is why they are passed
* in.
*/
{
}
/*
* Generate the appropriate USB-specific error message(s) (if any).
*/
switch (rv) {
case CFGA_USB_OK:
/* Special case - do nothing. */
break;
case CFGA_USB_UNKNOWN:
case CFGA_USB_DYNAMIC_AP:
case CFGA_USB_INTERNAL_ERROR:
case CFGA_USB_OPTIONS:
case CFGA_USB_ALLOC_FAIL:
case CFGA_USB_STATE:
case CFGA_USB_CONFIG_INVAL:
case CFGA_USB_PRIV:
case CFGA_USB_OPNOTSUPP:
/* These messages require no additional strings passed. */
break;
case CFGA_USB_AP:
case CFGA_USB_PORT:
case CFGA_USB_NOT_CONNECTED:
case CFGA_USB_NOT_CONFIGURED:
case CFGA_USB_BUSY:
case CFGA_USB_DEVLINK:
case CFGA_USB_RCM_HANDLE:
case CFGA_USB_RCM_ONLINE:
case CFGA_USB_RCM_OFFLINE:
case CFGA_USB_RCM_INFO:
case CFGA_USB_DEVCTL:
/* These messages also print ap_id. */
break;
case CFGA_USB_IOCTL:
case CFGA_USB_NVLIST:
case CFGA_USB_CONFIG_FILE:
case CFGA_USB_ONE_CONFIG:
/* These messages also print errno. */
{
break;
}
case CFGA_USB_OPEN:
/* These messages also apid and errno. */
{
break;
}
default:
} /* end switch */
/*
* Determine the proper error code to send back to the cfgadm library.
*/
}
/*
* Ensure the ap_id passed is in the correct (physical ap_id) form:
* where xx is a one or two-digit number.
*
* Note the library always calls the plugin with a physical ap_id.
*/
static int
verify_valid_apid(const char *ap_id)
{
char *l_ap_id;
return (-1);
}
l_ap_id++;
/* Bad characters in the ap_id. */
return (-1);
}
/* ap_id has 1..2 or more than 2 dots */
return (-1);
}
return (0);
}
/*
* Verify the params passed in are valid.
*/
static cfga_usb_ret_t
const char *ap_id,
const char *options,
char **errstring)
{
}
DPRINTF("verify_params: hardware-specific options not "
"supported.\n");
return (CFGA_USB_OPTIONS);
}
/* Dynamic attachment points not supported (yet). */
DPRINTF("verify_params: dynamic ap_id passed\n");
return (CFGA_USB_DYNAMIC_AP);
}
if (verify_valid_apid(ap_id) != 0) {
DPRINTF("verify_params: not a USB ap_id.\n");
return (CFGA_USB_AP);
}
return (CFGA_USB_OK);
}
/*
* Takes a validated ap_id and extracts the port number.
*/
static cfga_usb_ret_t
{
char *port_nbr_str;
char *temp;
}
errno = 0;
if (errno) {
DPRINTF("get_port_num: conversion of port str failed\n");
return (CFGA_USB_PORT);
}
return (CFGA_USB_OK);
}
/*
*/
static void
{
if (user_nvlist != NULL) {
}
if (devctl_hdl != NULL) {
}
}
static cfga_usb_ret_t
{
/* Get a handle to the ap */
DPRINTF("setup_for_devctl_cmd: devctl_ap_acquire failed with "
"errno: %d\n", errno);
goto bailout;
}
/* Set up to pass port number down to driver */
DPRINTF("setup_for_devctl: nvlist_alloc failed, errno: %d\n",
errno);
/* cleanup_after_devctl_cmd */
goto bailout;
}
DPRINTF("setup_for_devctl_cmd: get_port_num, errno: %d\n",
errno);
goto bailout;
}
/* creates an int32_t entry */
DPRINTF("setup_for_devctl_cmd: nvlist_add_int32 failed. "
"errno: %d\n", errno);
goto bailout;
}
return (rv);
return (rv);
}
/*
* Ensure that there's a device actually connected to the ap
*/
static cfga_usb_ret_t
{
DPRINTF("device_configured:\n");
return (CFGA_USB_DEVCTL);
}
return (CFGA_USB_NOT_CONFIGURED);
}
return (rv);
}
/*
* Ensure that there's a device actually connected to the ap
*/
static cfga_usb_ret_t
{
DPRINTF("device_connected:\n");
return (CFGA_USB_DEVCTL);
}
return (CFGA_USB_NOT_CONNECTED);
}
return (rv);
}
/*
* Given a subcommand to the DEVCTL_AP_CONTROL ioctl, rquest the size of
* the data to be returned, allocate a buffer, then get the data.
* Returns *descrp (which must be freed) and size.
*
* Note USB_DESCR_TYPE_STRING returns an ASCII NULL-terminated string,
* not a string descr.
*/
{
int fd = -1;
struct hubd_ioctl_data ioctl_data;
goto bailout;
}
rv = CFGA_USB_OPEN;
rv = CFGA_USB_BUSY;
}
goto bailout;
}
/*
* Find out how large a buf we need to get the data.
*
*/
DPRINTF("do_control_ioctl: size ioctl failed: errno:%d\n",
errno);
rv = CFGA_USB_IOCTL;
goto bailout;
}
*sizep = local_size;
if (subcommand == USB_DESCR_TYPE_STRING &&
/* Zero-length data - nothing to do. */
goto bailout;
}
if (subcommand == HUBD_REFRESH_DEVDB) {
/* Already done - no data transfer; nothing left to do. */
goto bailout;
}
DPRINTF("do_control_ioctl: malloc failed\n");
goto bailout;
}
/* Get the data */
DPRINTF("do_control_ioctl: ioctl failed: errno:%d\n",
errno);
rv = CFGA_USB_IOCTL;
goto bailout;
}
return (rv);
if (fd != -1) {
}
}
}
return (rv);
}
/* ========================================================================= */
/*
* Support funcs called directly from cfga_* entry points.
*/
/*
* Invoked from cfga_private_func.
* Modify the USB persistant configuration file so that the device
* represented by ap_id will henceforth be initialized to the desired
* configuration setting (configuration index).
*/
static cfga_usb_ret_t
{
char *tmp;
/* Only one bNumConfigurations, don't allow this operation */
DPRINTF("device supports %d configurations\n",
goto bailout;
}
/* get the serial number string if it exists */
if (descrp->iSerialNumber != 0) {
CFGA_USB_OK) {
if (rv != CFGA_USB_ZEROLEN) {
DPRINTF("set_configuration: get serial "
"no string failed\n");
goto bailout;
}
}
}
DPRINTF("get device path failed\n");
goto bailout;
}
DPRINTF("calling add_entry: vid: 0x%x pid:0x%x config:0x%x,",
/*
* the devicepath should be an absolute path.
* So, if path has leading "/devices" - nuke it.
*/
} else {
}
/* Save an entry in the USBCONF_FILE */
"enable", /* Always to "enable" */
config, /* new cfgndx */
serial_no, /* serial no string */
tmp, /* device path */
driver, /* Driver (optional) */
!= CFGA_USB_OK) {
DPRINTF("set_configuration: add_entry failed\n");
goto bailout;
}
/* Notify hubd that it needs to refresh its db. */
DPRINTF("set_configuration: HUBD_REFRESH_DEVDB failed\n");
goto bailout;
}
if (dev_path) {
}
if (serial_no) {
}
return (rv);
}
/*
* Invoked from cfga_private_func() and fill_in_ap_info().
* Call into USBA and get the current configuration setting for this device,
*/
static cfga_usb_ret_t
{
DPRINTF("get_config: get current config descr failed\n");
goto bailout;
}
*config = *config_val;
return (rv);
}
/*
* Invoked from cfga_private_func.
* it does an unconfigure of the device followed by a configure,
* thus essentially resetting the device.
*/
static cfga_usb_ret_t
{
DPRINTF("reset_device: \n");
/*
* Disconnect and reconfigure the device.
* Note this forces the new default config to take effect.
*/
}
return (rv);
}
return (CFGA_USB_DEVCTL);
}
return (CFGA_USB_OK);
}
/*
* Called from cfga_list_ext.
* Fills in the 'misc_info' field in the cfga buffer (displayed with -lv).
*/
static cfga_usb_ret_t
{
DPRINTF("fill_in_ap_info:\n");
DPRINTF("fill_in_ap_info: get dev descr failed\n");
return (rv);
}
/* iManufacturer */
if (dev_descrp->iManufacturer != 0) {
if (rv == CFGA_USB_ZEROLEN) {
rv = CFGA_USB_OK;
} else {
DPRINTF("get iManufacturer failed\n");
goto bailout;
}
}
}
/* iProduct */
if (dev_descrp->iProduct != 0) {
HUBD_PRODUCT_STR, (void **)&prod_str,
&size)) != CFGA_USB_OK) {
if (rv == CFGA_USB_ZEROLEN) {
rv = CFGA_USB_OK;
} else {
DPRINTF("getting iProduct failed\n");
goto bailout;
}
}
}
/* Current conifguration */
DPRINTF("get_config failed\n");
goto bailout;
}
/* Configuration string descriptor */
if (rv == CFGA_USB_ZEROLEN) {
rv = CFGA_USB_OK;
} else {
DPRINTF("HUBD_CFG_DESCR_STR failed\n");
goto bailout;
}
}
/* add ": " to output coz PSARC case says so */
} else {
}
/* Dump local buf into passed-in buf. */
"Mfg: %s Product: %s NConfigs: %d Config: %d %s%s", mfg_str,
if (dev_descrp) {
}
}
}
}
return (rv);
}
/* ========================================================================== */
/* Entry points */
/*ARGSUSED*/
const char *ap_id,
const char *options,
struct cfga_confirm *confp,
char **errstring,
{
int ret;
int len;
char *msg;
char *devpath;
DPRINTF("cfga_change_state:\n");
goto bailout;
}
/*
* All subcommands which can change state of device require
* root privileges.
*/
if (geteuid() != 0) {
rv = CFGA_USB_PRIV;
goto bailout;
}
CFGA_USB_OK) {
goto bailout;
}
switch (state_change_cmd) {
case CFGA_CMD_CONFIGURE:
goto bailout;
}
if (rstate == AP_RSTATE_EMPTY) {
goto bailout;
}
DPRINTF("cfga_change_state: devctl_ap_configure "
"failed. errno: %d\n", errno);
}
int i;
/*
* try for some time as USB hotplug thread
* takes a while to create the path
* and then eventually give up
*/
sleep(6);
}
DPRINTF("cfga_change_state: get device "
"path failed i = %d\n", i);
break;
}
}
break;
case CFGA_CMD_UNCONFIGURE:
goto bailout;
}
/* check if it is already unconfigured */
goto bailout;
}
}
return (CFGA_NACK);
}
DPRINTF("cfga_change_state: get device path failed\n");
break;
}
CFGA_USB_OK) {
break;
}
if (ret != 0) {
DPRINTF("cfga_change_state: devctl_ap_unconfigure "
"failed with errno: %d\n", errno);
rv = CFGA_USB_BUSY;
}
} else {
}
break;
case CFGA_CMD_DISCONNECT:
/*
* special case handling for
* SLM based cfgadm disconnects
*/
if (ostate == AP_OSTATE_UNCONFIGURED)
goto bailout;
}
}
return (CFGA_NACK);
}
DPRINTF("cfga_change_state: get device path failed\n");
break;
}
/* only call rcm_offline iff the state was CONFIGURED */
if (ostate == AP_OSTATE_CONFIGURED) {
break;
}
}
if (ret != 0) {
DPRINTF("cfga_change_state: devctl_ap_disconnect "
"failed with errno: %d\n", errno);
rv = CFGA_USB_BUSY;
}
if (ostate == AP_OSTATE_CONFIGURED) {
}
} else {
if (ostate == AP_OSTATE_CONFIGURED) {
}
}
break;
case CFGA_CMD_CONNECT:
case CFGA_CMD_LOAD:
case CFGA_CMD_UNLOAD:
break;
case CFGA_CMD_NONE:
default:
}
}
/*ARGSUSED*/
const char *func,
const char *ap_id,
const char *options,
struct cfga_confirm *confp,
char **errstring,
{
int len;
char *msg;
DPRINTF("cfga_private_func:\n");
}
/*
* All subcommands which can change state of device require
* root privileges.
*/
if (geteuid() != 0) {
rv = CFGA_USB_PRIV;
goto bailout;
}
goto bailout;
}
CFGA_USB_OK) {
goto bailout;
}
goto bailout;
}
rv = CFGA_USB_OK;
} else {
return (CFGA_NACK);
}
return (CFGA_NACK);
}
goto bailout;
}
/* these are the only valid options */
char *cfg_opts[] = {
"config", /* 0 */
"drv", /* 1 */
};
/* return error if no options are specified */
DPRINTF("cfga_private_func: no options\n");
goto bailout;
}
/* parse options specified */
while (*subopts != '\0') {
case 0: /* config */
goto bailout;
} else {
errno = 0;
(char **)NULL, 10);
if (errno) {
"config conversion"
"failed\n");
rv =
goto bailout;
}
}
break;
case 1: /* drv */
goto bailout;
} else {
rv =
goto bailout;
}
}
break;
default:
goto bailout;
}
}
/* config is mandatory */
if (cfg_opt_flag != B_TRUE) {
goto bailout;
}
strlen("to USB configuration");
/* len + 8 to account for config, \n and white space */
"Setting %s%s\nto USB configuration %d\n%s",
} else {
goto bailout;
}
return (CFGA_NACK);
}
/*
* Check that the option setting selected is in range.
*/
DPRINTF("cfga_private_func: get dev descr failed\n");
goto bailout;
}
DPRINTF("cfga_private_func: config index requested "
"(%d) exceeds bNumConfigurations - 1 (%d)\n",
goto bailout;
}
/* Pass current setting to set_configuration */
goto bailout;
}
/* check if they match - yes, then nothing to do */
if (actual_config == config) {
DPRINTF("cfga_private_func: config index requested "
"(%d) matches the actual config value %d\n",
rv = CFGA_USB_OK;
goto bailout;
}
/* Save the configuration settings */
goto bailout;
}
/* Reset device to force new config to take effect */
goto bailout;
}
} else {
DPRINTF("cfga_private_func: unrecognized command.\n");
return (CFGA_INVAL);
}
}
/*ARGSUSED*/
const char *ap_id,
const char *options,
char **errstring,
{
return (CFGA_OPNOTSUPP);
}
/*ARGSUSED*/
const char *ap_id,
int *nlistp,
const char *options,
const char *listopts,
char **errstring,
{
int l_errno;
DPRINTF("cfga_list_ext:\n");
goto bailout;
}
DPRINTF("cfga_list_ext: list = NULL or nlistp = NULL\n");
goto bailout;
}
/* Get ap status */
DC_RDONLY)) != CFGA_USB_OK) {
goto bailout;
}
-1) {
DPRINTF("cfga_list_ext: devctl_ap_getstate failed. errno: %d\n",
errno);
goto bailout;
}
/*
* Create cfga_list_data_t struct.
*/
if ((*ap_id_list =
DPRINTF("cfga_list_ext: malloc for cfga_list_data_t failed. "
"errno: %d\n", errno);
goto bailout;
}
*nlistp = 1;
/*
* Rest of the code fills in the cfga_list_data_t struct.
*/
/* Remember ap_id_log must be freed */
if (rv != 0) {
goto bailout;
}
/* Get logical ap-id corresponding to the physical */
goto bailout;
}
sizeof ((*ap_id_list)->ap_log_id));
sizeof ((*ap_id_list)->ap_phys_id));
switch (devctl_ap_state.ap_rstate) {
case AP_RSTATE_EMPTY:
break;
case AP_RSTATE_DISCONNECTED:
break;
case AP_RSTATE_CONNECTED:
break;
default:
rv = CFGA_USB_STATE;
goto bailout;
}
switch (devctl_ap_state.ap_ostate) {
case AP_OSTATE_CONFIGURED:
break;
case AP_OSTATE_UNCONFIGURED:
break;
default:
rv = CFGA_USB_STATE;
goto bailout;
}
switch (devctl_ap_state.ap_condition) {
case AP_COND_OK:
break;
case AP_COND_FAILING:
break;
case AP_COND_FAILED:
break;
case AP_COND_UNUSABLE:
break;
case AP_COND_UNKNOWN:
break;
default:
rv = CFGA_USB_STATE;
goto bailout;
}
char *str_p;
/* Fill in the info for the -v option display. */
DPRINTF("cfga_list_ext: fill_in_ap_info failed\n");
goto bailout;
}
/* Fill in ap_type */
DPRINTF("cfga_list_ext: do_control_ioctl failed\n");
goto bailout;
}
/*
* NOTE: In the cfgadm display the "Type" column is only 12
* chars long. Most USB devices can be displayed here with a
* "usb-" prefix. Only USB keyboard cannot be displayed in
* its entirety as "usb-keybaord" is 13 chars in length.
* It will show up as "usb-kbd".
*/
} else {
}
} else {
}
if (*ap_id_list != NULL) {
free(*ap_id_list);
}
}
}
/*
* This routine accepts a variable number of message IDs and constructs
* a corresponding error string which is printed via the message print routine
* argument.
*/
static void
{
int len;
char *q;
DPRINTF("cfga_msg: msg\n");
return;
}
DPRINTF("cfga_msg: null str\n");
return;
}
DPRINTF("cfga_msg: null q\n");
return;
}
free(q);
}
/* ARGSUSED */
{
DPRINTF("cfga_help:\n");
if (options) {
}
return (CFGA_OK);
}
static int
{
int rval;
return (0);
}
return (rval);
}
static char *
usb_get_devicepath(const char *ap_id)
{
if (rv == CFGA_USB_OK) {
DPRINTF("usb_get_devicepath: get device path ioctl ok\n");
return (devpath);
} else {
DPRINTF("usb_get_devicepath: get device path ioctl failed\n");
return ((char *)NULL);
}
}