Home | History | Annotate | Line # | Download | only in audiocfg
drvctl.c revision 1.1
      1 /* $NetBSD: drvctl.c,v 1.1 2010/08/30 02:19:47 mrg Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <stdbool.h>
     30 #include <stdlib.h>
     31 #include <string.h>
     32 #include <unistd.h>
     33 
     34 #include "drvctl.h"
     35 
     36 static int
     37 drvctl_list(int fd, const char *name, struct devlistargs *laa)
     38 {
     39 	size_t children;
     40 
     41 	memset(laa, 0, sizeof(*laa));
     42 	strlcpy(laa->l_devname, name, sizeof(laa->l_devname));
     43 	if (ioctl(fd, DRVLISTDEV, laa) == -1)
     44 		return -1;
     45 	children = laa->l_children;
     46 	laa->l_childname = malloc(children * sizeof(laa->l_childname[0]));
     47 	if (laa->l_childname == NULL)
     48 		return -1;
     49 	if (ioctl(fd, DRVLISTDEV, laa) == -1)
     50 		return -1;
     51 	return 0;
     52 }
     53 
     54 static bool
     55 drvctl_get_properties(int fd, const char *devnode, prop_dictionary_t *props)
     56 {
     57 	prop_dictionary_t command_dict;
     58 	prop_dictionary_t args_dict;
     59 	prop_dictionary_t results_dict;
     60 	int8_t perr;
     61 	int error;
     62 	bool rv;
     63 
     64 	command_dict = prop_dictionary_create();
     65 	args_dict = prop_dictionary_create();
     66 
     67 	prop_dictionary_set_cstring_nocopy(command_dict, "drvctl-command",
     68 	    "get-properties");
     69 	prop_dictionary_set_cstring_nocopy(args_dict, "device-name", devnode);
     70 	prop_dictionary_set(command_dict, "drvctl-arguments", args_dict);
     71 	prop_object_release(args_dict);
     72 
     73 	error = prop_dictionary_sendrecv_ioctl(command_dict, fd,
     74 	    DRVCTLCOMMAND, &results_dict);
     75 	prop_object_release(command_dict);
     76 	if (error)
     77 		return false;
     78 
     79 	rv = prop_dictionary_get_int8(results_dict, "drvctl-error", &perr);
     80 	if (rv == false || perr != 0) {
     81 		prop_object_release(results_dict);
     82 		return false;
     83 	}
     84 
     85 	if (props) {
     86 		prop_dictionary_t result_data;
     87 		result_data = prop_dictionary_get(results_dict,
     88 		    "drvctl-result-data");
     89 		if (result_data)
     90 			*props = prop_dictionary_copy(result_data);
     91 	}
     92 
     93 	prop_object_release(results_dict);
     94 
     95 	return true;
     96 }
     97 
     98 static int
     99 drvctl_search(int fd, const char *curnode, const char *dvname,
    100     void (*callback)(void *, const char *, unsigned int), void *args)
    101 {
    102 	struct devlistargs laa;
    103 	unsigned int i;
    104 	uint32_t unit;
    105 	bool rv;
    106 
    107 	if (drvctl_list(fd, curnode, &laa) == -1)
    108 		return -1;
    109 
    110 	for (i = 0; i < laa.l_children; i++) {
    111 		prop_dictionary_t props = NULL;
    112 		const char *curdvname;
    113 
    114 		rv = drvctl_get_properties(fd, laa.l_childname[i], &props);
    115 		if (rv == false || props == NULL)
    116 			continue;
    117 		rv = prop_dictionary_get_cstring_nocopy(props,
    118 		    "device-driver", &curdvname);
    119 		if (rv == true && strcmp(curdvname, dvname) == 0) {
    120 			rv = prop_dictionary_get_uint32(props,
    121 			    "device-unit", &unit);
    122 			if (rv == true)
    123 				callback(args, laa.l_childname[i], unit);
    124 		}
    125 		prop_object_release(props);
    126 
    127 		if (drvctl_search(fd, laa.l_childname[i], dvname,
    128 		    callback, args) == -1)
    129 			return -1;
    130 	}
    131 
    132 	return 0;
    133 }
    134 
    135 int
    136 drvctl_foreach(int fd, const char *dvname,
    137     void (*callback)(void *, const char *, unsigned int), void *args)
    138 {
    139 	return drvctl_search(fd, "", dvname, callback, args);
    140 }
    141