2009-10-21 21:25:23 +08:00
|
|
|
#include "net.h"
|
2009-07-15 19:43:31 +08:00
|
|
|
#include "qdev.h"
|
2010-03-22 17:29:00 +08:00
|
|
|
#include "qerror.h"
|
2010-08-24 23:22:24 +08:00
|
|
|
#include "blockdev.h"
|
2009-07-15 19:43:31 +08:00
|
|
|
|
|
|
|
void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
|
|
|
|
{
|
|
|
|
void *ptr = dev;
|
|
|
|
ptr += prop->offset;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2010-01-10 19:52:41 +08:00
|
|
|
static uint32_t qdev_get_prop_mask(Property *prop)
|
|
|
|
{
|
2012-02-03 05:51:09 +08:00
|
|
|
assert(prop->info == &qdev_prop_bit);
|
2010-01-10 19:52:41 +08:00
|
|
|
return 0x1 << prop->bitnr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bit_prop_set(DeviceState *dev, Property *props, bool val)
|
|
|
|
{
|
|
|
|
uint32_t *p = qdev_get_prop_ptr(dev, props);
|
|
|
|
uint32_t mask = qdev_get_prop_mask(props);
|
|
|
|
if (val)
|
2010-01-13 03:16:59 +08:00
|
|
|
*p |= mask;
|
2010-01-10 19:52:41 +08:00
|
|
|
else
|
|
|
|
*p &= ~mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bit */
|
|
|
|
|
|
|
|
static int print_bit(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
2011-01-11 06:11:40 +08:00
|
|
|
uint32_t *p = qdev_get_prop_ptr(dev, prop);
|
2010-01-10 19:52:41 +08:00
|
|
|
return snprintf(dest, len, (*p & qdev_get_prop_mask(prop)) ? "on" : "off");
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void get_bit(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
uint32_t *p = qdev_get_prop_ptr(dev, prop);
|
|
|
|
bool value = (*p & qdev_get_prop_mask(prop)) != 0;
|
|
|
|
|
|
|
|
visit_type_bool(v, &value, name, errp);
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void set_bit(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
bool value;
|
|
|
|
|
|
|
|
if (dev->state != DEV_STATE_CREATED) {
|
|
|
|
error_set(errp, QERR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_bool(v, &value, name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bit_prop_set(dev, prop, value);
|
|
|
|
}
|
|
|
|
|
2010-01-10 19:52:41 +08:00
|
|
|
PropertyInfo qdev_prop_bit = {
|
2011-12-19 00:05:10 +08:00
|
|
|
.name = "boolean",
|
|
|
|
.legacy_name = "on/off",
|
2010-01-10 19:52:41 +08:00
|
|
|
.print = print_bit,
|
2011-12-19 00:05:09 +08:00
|
|
|
.get = get_bit,
|
|
|
|
.set = set_bit,
|
2010-01-10 19:52:41 +08:00
|
|
|
};
|
|
|
|
|
2009-09-30 04:48:25 +08:00
|
|
|
/* --- 8bit integer --- */
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void get_int8(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
int8_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
int64_t value;
|
|
|
|
|
|
|
|
value = *ptr;
|
|
|
|
visit_type_int(v, &value, name, errp);
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void set_int8(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
2011-12-19 00:05:09 +08:00
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
int8_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
Error *local_err = NULL;
|
|
|
|
int64_t value;
|
|
|
|
|
|
|
|
if (dev->state != DEV_STATE_CREATED) {
|
|
|
|
error_set(errp, QERR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_int(v, &value, name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
2012-02-02 23:19:21 +08:00
|
|
|
if (value >= prop->info->min && value <= prop->info->max) {
|
2011-12-19 00:05:09 +08:00
|
|
|
*ptr = value;
|
|
|
|
} else {
|
|
|
|
error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
|
|
|
|
dev->id?:"", name, value, prop->info->min,
|
|
|
|
prop->info->max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-30 04:48:25 +08:00
|
|
|
PropertyInfo qdev_prop_uint8 = {
|
|
|
|
.name = "uint8",
|
2011-12-19 00:05:09 +08:00
|
|
|
.get = get_int8,
|
|
|
|
.set = set_int8,
|
|
|
|
.min = 0,
|
|
|
|
.max = 255,
|
2009-09-30 04:48:25 +08:00
|
|
|
};
|
|
|
|
|
2011-10-07 15:19:52 +08:00
|
|
|
/* --- 8bit hex value --- */
|
|
|
|
|
|
|
|
static int parse_hex8(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
char *end;
|
|
|
|
|
2012-02-09 17:07:38 +08:00
|
|
|
if (str[0] != '0' || str[1] != 'x') {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-10-07 15:19:52 +08:00
|
|
|
*ptr = strtoul(str, &end, 16);
|
|
|
|
if ((*end != '\0') || (end == str)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_hex8(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
return snprintf(dest, len, "0x%" PRIx8, *ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_hex8 = {
|
2011-12-19 00:05:10 +08:00
|
|
|
.name = "uint8",
|
|
|
|
.legacy_name = "hex8",
|
2011-10-07 15:19:52 +08:00
|
|
|
.parse = parse_hex8,
|
|
|
|
.print = print_hex8,
|
2011-12-19 00:05:09 +08:00
|
|
|
.get = get_int8,
|
|
|
|
.set = set_int8,
|
|
|
|
.min = 0,
|
|
|
|
.max = 255,
|
2011-10-07 15:19:52 +08:00
|
|
|
};
|
|
|
|
|
2009-07-15 19:43:31 +08:00
|
|
|
/* --- 16bit integer --- */
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void get_int16(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
int16_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
int64_t value;
|
|
|
|
|
|
|
|
value = *ptr;
|
|
|
|
visit_type_int(v, &value, name, errp);
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void set_int16(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
int16_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
Error *local_err = NULL;
|
|
|
|
int64_t value;
|
|
|
|
|
|
|
|
if (dev->state != DEV_STATE_CREATED) {
|
|
|
|
error_set(errp, QERR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_int(v, &value, name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
2012-02-02 23:19:21 +08:00
|
|
|
if (value >= prop->info->min && value <= prop->info->max) {
|
2011-12-19 00:05:09 +08:00
|
|
|
*ptr = value;
|
|
|
|
} else {
|
|
|
|
error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
|
|
|
|
dev->id?:"", name, value, prop->info->min,
|
|
|
|
prop->info->max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-15 19:43:31 +08:00
|
|
|
PropertyInfo qdev_prop_uint16 = {
|
|
|
|
.name = "uint16",
|
2011-12-19 00:05:09 +08:00
|
|
|
.get = get_int16,
|
|
|
|
.set = set_int16,
|
|
|
|
.min = 0,
|
|
|
|
.max = 65535,
|
2009-07-15 19:43:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* --- 32bit integer --- */
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void get_int32(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
int32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
int64_t value;
|
|
|
|
|
|
|
|
value = *ptr;
|
|
|
|
visit_type_int(v, &value, name, errp);
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void set_int32(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
int32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
Error *local_err = NULL;
|
|
|
|
int64_t value;
|
|
|
|
|
|
|
|
if (dev->state != DEV_STATE_CREATED) {
|
|
|
|
error_set(errp, QERR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_int(v, &value, name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
2012-02-02 23:19:21 +08:00
|
|
|
if (value >= prop->info->min && value <= prop->info->max) {
|
2011-12-19 00:05:09 +08:00
|
|
|
*ptr = value;
|
|
|
|
} else {
|
|
|
|
error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
|
|
|
|
dev->id?:"", name, value, prop->info->min,
|
|
|
|
prop->info->max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-15 19:43:31 +08:00
|
|
|
PropertyInfo qdev_prop_uint32 = {
|
|
|
|
.name = "uint32",
|
2011-12-19 00:05:09 +08:00
|
|
|
.get = get_int32,
|
|
|
|
.set = set_int32,
|
|
|
|
.min = 0,
|
|
|
|
.max = 0xFFFFFFFFULL,
|
2009-07-15 19:43:31 +08:00
|
|
|
};
|
|
|
|
|
2009-09-10 17:43:25 +08:00
|
|
|
PropertyInfo qdev_prop_int32 = {
|
|
|
|
.name = "int32",
|
2011-12-19 00:05:09 +08:00
|
|
|
.get = get_int32,
|
|
|
|
.set = set_int32,
|
|
|
|
.min = -0x80000000LL,
|
|
|
|
.max = 0x7FFFFFFFLL,
|
2009-09-10 17:43:25 +08:00
|
|
|
};
|
|
|
|
|
2009-07-15 19:43:31 +08:00
|
|
|
/* --- 32bit hex value --- */
|
|
|
|
|
|
|
|
static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
2010-06-19 00:27:03 +08:00
|
|
|
char *end;
|
2009-07-15 19:43:31 +08:00
|
|
|
|
2012-02-09 17:07:38 +08:00
|
|
|
if (str[0] != '0' || str[1] != 'x') {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-06-19 00:27:03 +08:00
|
|
|
*ptr = strtoul(str, &end, 16);
|
|
|
|
if ((*end != '\0') || (end == str)) {
|
2010-02-26 22:50:50 +08:00
|
|
|
return -EINVAL;
|
2010-06-19 00:27:03 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 19:43:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_hex32(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
return snprintf(dest, len, "0x%" PRIx32, *ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_hex32 = {
|
2011-12-19 00:05:10 +08:00
|
|
|
.name = "uint32",
|
|
|
|
.legacy_name = "hex32",
|
2009-07-15 19:43:31 +08:00
|
|
|
.parse = parse_hex32,
|
|
|
|
.print = print_hex32,
|
2011-12-19 00:05:09 +08:00
|
|
|
.get = get_int32,
|
|
|
|
.set = set_int32,
|
|
|
|
.min = 0,
|
|
|
|
.max = 0xFFFFFFFFULL,
|
2009-07-15 19:43:31 +08:00
|
|
|
};
|
|
|
|
|
2009-07-21 19:10:41 +08:00
|
|
|
/* --- 64bit integer --- */
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void get_int64(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
int64_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
visit_type_int(v, ptr, name, errp);
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void set_int64(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
int64_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
if (dev->state != DEV_STATE_CREATED) {
|
|
|
|
error_set(errp, QERR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_int(v, ptr, name, errp);
|
|
|
|
}
|
|
|
|
|
2009-07-21 19:10:41 +08:00
|
|
|
PropertyInfo qdev_prop_uint64 = {
|
|
|
|
.name = "uint64",
|
2011-12-19 00:05:09 +08:00
|
|
|
.get = get_int64,
|
|
|
|
.set = set_int64,
|
2009-07-21 19:10:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* --- 64bit hex value --- */
|
|
|
|
|
|
|
|
static int parse_hex64(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
|
2010-06-19 00:27:03 +08:00
|
|
|
char *end;
|
2009-07-21 19:10:41 +08:00
|
|
|
|
2012-02-09 17:07:38 +08:00
|
|
|
if (str[0] != '0' || str[1] != 'x') {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-06-19 00:27:03 +08:00
|
|
|
*ptr = strtoull(str, &end, 16);
|
|
|
|
if ((*end != '\0') || (end == str)) {
|
2010-02-26 22:50:50 +08:00
|
|
|
return -EINVAL;
|
2010-06-19 00:27:03 +08:00
|
|
|
}
|
|
|
|
|
2009-07-21 19:10:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_hex64(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
return snprintf(dest, len, "0x%" PRIx64, *ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_hex64 = {
|
2011-12-19 00:05:10 +08:00
|
|
|
.name = "uint64",
|
|
|
|
.legacy_name = "hex64",
|
2009-07-21 19:10:41 +08:00
|
|
|
.parse = parse_hex64,
|
|
|
|
.print = print_hex64,
|
2011-12-19 00:05:09 +08:00
|
|
|
.get = get_int64,
|
|
|
|
.set = set_int64,
|
2009-07-21 19:10:41 +08:00
|
|
|
};
|
|
|
|
|
2009-10-12 19:45:47 +08:00
|
|
|
/* --- string --- */
|
|
|
|
|
2012-02-02 20:08:48 +08:00
|
|
|
static void release_string(Object *obj, const char *name, void *opaque)
|
2010-06-02 02:32:31 +08:00
|
|
|
{
|
2012-02-02 20:08:48 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
g_free(*(char **)qdev_get_prop_ptr(DEVICE(obj), prop));
|
2010-06-02 02:32:31 +08:00
|
|
|
}
|
|
|
|
|
2009-10-12 19:45:47 +08:00
|
|
|
static int print_string(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
char **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
if (!*ptr)
|
|
|
|
return snprintf(dest, len, "<null>");
|
|
|
|
return snprintf(dest, len, "\"%s\"", *ptr);
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void get_string(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
char **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
if (!*ptr) {
|
|
|
|
char *str = (char *)"";
|
|
|
|
visit_type_str(v, &str, name, errp);
|
|
|
|
} else {
|
|
|
|
visit_type_str(v, ptr, name, errp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void set_string(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
char **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
Error *local_err = NULL;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
if (dev->state != DEV_STATE_CREATED) {
|
|
|
|
error_set(errp, QERR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_str(v, &str, name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (*ptr) {
|
|
|
|
g_free(*ptr);
|
|
|
|
}
|
|
|
|
*ptr = str;
|
|
|
|
}
|
|
|
|
|
2009-10-12 19:45:47 +08:00
|
|
|
PropertyInfo qdev_prop_string = {
|
|
|
|
.name = "string",
|
|
|
|
.print = print_string,
|
2012-02-02 20:08:48 +08:00
|
|
|
.release = release_string,
|
2011-12-19 00:05:09 +08:00
|
|
|
.get = get_string,
|
|
|
|
.set = set_string,
|
2009-10-12 19:45:47 +08:00
|
|
|
};
|
|
|
|
|
2009-07-31 18:25:40 +08:00
|
|
|
/* --- drive --- */
|
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
static int parse_drive(DeviceState *dev, const char *str, void **ptr)
|
2009-07-31 18:25:40 +08:00
|
|
|
{
|
2010-05-05 22:36:52 +08:00
|
|
|
BlockDriverState *bs;
|
2009-07-31 18:25:40 +08:00
|
|
|
|
2010-05-05 22:36:52 +08:00
|
|
|
bs = bdrv_find(str);
|
|
|
|
if (bs == NULL)
|
2010-02-26 22:50:50 +08:00
|
|
|
return -ENOENT;
|
2011-08-03 21:07:40 +08:00
|
|
|
if (bdrv_attach_dev(bs, dev) < 0)
|
2010-06-29 22:58:30 +08:00
|
|
|
return -EEXIST;
|
2010-05-05 22:36:52 +08:00
|
|
|
*ptr = bs;
|
2009-07-31 18:25:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-02 20:08:48 +08:00
|
|
|
static void release_drive(Object *obj, const char *name, void *opaque)
|
2010-06-25 14:09:10 +08:00
|
|
|
{
|
2012-02-02 20:08:48 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
2010-05-05 22:36:52 +08:00
|
|
|
BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
|
2010-06-25 14:09:10 +08:00
|
|
|
|
|
|
|
if (*ptr) {
|
2011-08-03 21:07:40 +08:00
|
|
|
bdrv_detach_dev(*ptr, dev);
|
2010-05-05 22:36:52 +08:00
|
|
|
blockdev_auto_del(*ptr);
|
2010-06-25 14:09:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
static const char *print_drive(void *ptr)
|
2009-07-31 18:25:40 +08:00
|
|
|
{
|
2012-02-02 20:01:40 +08:00
|
|
|
return bdrv_get_device_name(ptr);
|
2009-07-31 18:25:40 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
static void get_pointer(Object *obj, Visitor *v, Property *prop,
|
|
|
|
const char *(*print)(void *ptr),
|
|
|
|
const char *name, Error **errp)
|
2011-12-19 00:05:09 +08:00
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
void **ptr = qdev_get_prop_ptr(dev, prop);
|
2012-02-02 20:01:40 +08:00
|
|
|
char *p;
|
2011-12-19 00:05:09 +08:00
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
p = (char *) (*ptr ? print(*ptr) : "");
|
2011-12-19 00:05:09 +08:00
|
|
|
visit_type_str(v, &p, name, errp);
|
|
|
|
}
|
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
static void set_pointer(Object *obj, Visitor *v, Property *prop,
|
|
|
|
int (*parse)(DeviceState *dev, const char *str, void **ptr),
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Error *local_err = NULL;
|
2012-02-02 20:01:40 +08:00
|
|
|
void **ptr = qdev_get_prop_ptr(dev, prop);
|
2011-12-19 00:05:09 +08:00
|
|
|
char *str;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (dev->state != DEV_STATE_CREATED) {
|
|
|
|
error_set(errp, QERR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_str(v, &str, name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!*str) {
|
|
|
|
g_free(str);
|
2012-02-14 17:19:52 +08:00
|
|
|
*ptr = NULL;
|
2011-12-19 00:05:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-02-02 20:01:40 +08:00
|
|
|
ret = parse(dev, str, ptr);
|
2011-12-19 00:05:09 +08:00
|
|
|
error_set_from_qdev_prop_error(errp, ret, dev, prop, str);
|
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
static void get_drive(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
get_pointer(obj, v, opaque, print_drive, name, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_drive(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
set_pointer(obj, v, opaque, parse_drive, name, errp);
|
|
|
|
}
|
|
|
|
|
2009-07-31 18:25:40 +08:00
|
|
|
PropertyInfo qdev_prop_drive = {
|
|
|
|
.name = "drive",
|
2012-02-02 20:01:40 +08:00
|
|
|
.get = get_drive,
|
|
|
|
.set = set_drive,
|
2012-02-02 20:08:48 +08:00
|
|
|
.release = release_drive,
|
2009-07-31 18:25:40 +08:00
|
|
|
};
|
|
|
|
|
2009-08-03 23:35:18 +08:00
|
|
|
/* --- character device --- */
|
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
static int parse_chr(DeviceState *dev, const char *str, void **ptr)
|
2009-09-10 16:58:53 +08:00
|
|
|
{
|
2012-02-02 20:01:40 +08:00
|
|
|
CharDriverState *chr = qemu_chr_find(str);
|
|
|
|
if (chr == NULL) {
|
2010-02-26 22:50:50 +08:00
|
|
|
return -ENOENT;
|
2011-02-10 15:25:20 +08:00
|
|
|
}
|
2012-02-02 20:01:40 +08:00
|
|
|
if (chr->avail_connections < 1) {
|
2011-02-10 15:25:20 +08:00
|
|
|
return -EEXIST;
|
|
|
|
}
|
2012-02-02 20:01:40 +08:00
|
|
|
*ptr = chr;
|
|
|
|
--chr->avail_connections;
|
2009-09-10 16:58:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-02 20:08:48 +08:00
|
|
|
static void release_chr(Object *obj, const char *name, void *opaque)
|
2012-01-13 17:59:47 +08:00
|
|
|
{
|
2012-02-02 20:08:48 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
2012-01-13 17:59:47 +08:00
|
|
|
CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
if (*ptr) {
|
|
|
|
qemu_chr_add_handlers(*ptr, NULL, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
static const char *print_chr(void *ptr)
|
2009-08-03 23:35:18 +08:00
|
|
|
{
|
2012-02-02 20:01:40 +08:00
|
|
|
CharDriverState *chr = ptr;
|
2009-08-14 00:26:52 +08:00
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
return chr->label ? chr->label : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_chr(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
get_pointer(obj, v, opaque, print_chr, name, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_chr(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
set_pointer(obj, v, opaque, parse_chr, name, errp);
|
2009-08-03 23:35:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_chr = {
|
|
|
|
.name = "chr",
|
2012-02-02 20:01:40 +08:00
|
|
|
.get = get_chr,
|
|
|
|
.set = set_chr,
|
2012-02-02 20:08:48 +08:00
|
|
|
.release = release_chr,
|
2009-08-03 23:35:18 +08:00
|
|
|
};
|
|
|
|
|
2009-10-21 21:25:24 +08:00
|
|
|
/* --- netdev device --- */
|
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
static int parse_netdev(DeviceState *dev, const char *str, void **ptr)
|
2009-10-21 21:25:24 +08:00
|
|
|
{
|
2012-02-02 20:01:40 +08:00
|
|
|
VLANClientState *netdev = qemu_find_netdev(str);
|
2009-10-21 21:25:24 +08:00
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
if (netdev == NULL) {
|
2010-02-26 22:50:50 +08:00
|
|
|
return -ENOENT;
|
2012-02-02 20:01:40 +08:00
|
|
|
}
|
|
|
|
if (netdev->peer) {
|
2010-02-26 22:50:51 +08:00
|
|
|
return -EEXIST;
|
|
|
|
}
|
2012-02-02 20:01:40 +08:00
|
|
|
*ptr = netdev;
|
2009-10-21 21:25:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
static const char *print_netdev(void *ptr)
|
2009-10-21 21:25:24 +08:00
|
|
|
{
|
2012-02-02 20:01:40 +08:00
|
|
|
VLANClientState *netdev = ptr;
|
2009-10-21 21:25:24 +08:00
|
|
|
|
2012-02-02 20:01:40 +08:00
|
|
|
return netdev->name ? netdev->name : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_netdev(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
get_pointer(obj, v, opaque, print_netdev, name, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_netdev(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
set_pointer(obj, v, opaque, parse_netdev, name, errp);
|
2009-10-21 21:25:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_netdev = {
|
|
|
|
.name = "netdev",
|
2012-02-02 20:01:40 +08:00
|
|
|
.get = get_netdev,
|
|
|
|
.set = set_netdev,
|
2009-10-21 21:25:24 +08:00
|
|
|
};
|
|
|
|
|
2009-10-21 21:25:25 +08:00
|
|
|
/* --- vlan --- */
|
|
|
|
|
|
|
|
static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
VLANState **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
if (*ptr) {
|
|
|
|
return snprintf(dest, len, "%d", (*ptr)->id);
|
|
|
|
} else {
|
|
|
|
return snprintf(dest, len, "<null>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void get_vlan(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
VLANState **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
int64_t id;
|
|
|
|
|
|
|
|
id = *ptr ? (*ptr)->id : -1;
|
|
|
|
visit_type_int(v, &id, name, errp);
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:55:55 +08:00
|
|
|
static void set_vlan(Object *obj, Visitor *v, void *opaque,
|
2011-12-19 00:05:09 +08:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
2012-01-30 22:55:55 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2011-12-19 00:05:09 +08:00
|
|
|
Property *prop = opaque;
|
|
|
|
VLANState **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
Error *local_err = NULL;
|
|
|
|
int64_t id;
|
|
|
|
VLANState *vlan;
|
|
|
|
|
|
|
|
if (dev->state != DEV_STATE_CREATED) {
|
|
|
|
error_set(errp, QERR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_int(v, &id, name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (id == -1) {
|
|
|
|
*ptr = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vlan = qemu_find_vlan(id, 1);
|
|
|
|
if (!vlan) {
|
|
|
|
error_set(errp, QERR_INVALID_PARAMETER_VALUE,
|
|
|
|
name, prop->info->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*ptr = vlan;
|
|
|
|
}
|
|
|
|
|
2009-10-21 21:25:25 +08:00
|
|
|
PropertyInfo qdev_prop_vlan = {
|
|
|
|
.name = "vlan",
|
|
|
|
.print = print_vlan,
|
2011-12-19 00:05:09 +08:00
|
|
|
.get = get_vlan,
|
|
|
|
.set = set_vlan,
|
2009-10-21 21:25:25 +08:00
|
|
|
};
|
|
|
|
|
2009-07-15 19:43:31 +08:00
|
|
|
/* --- pointer --- */
|
|
|
|
|
2010-02-19 18:47:06 +08:00
|
|
|
/* Not a proper property, just for dirty hacks. TODO Remove it! */
|
2009-07-15 19:43:31 +08:00
|
|
|
PropertyInfo qdev_prop_ptr = {
|
|
|
|
.name = "ptr",
|
|
|
|
};
|
|
|
|
|
|
|
|
/* --- mac address --- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* accepted syntax versions:
|
|
|
|
* 01:02:03:04:05:06
|
|
|
|
* 01-02-03-04-05-06
|
|
|
|
*/
|
2012-02-03 00:08:47 +08:00
|
|
|
static void get_mac(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
2009-07-15 19:43:31 +08:00
|
|
|
{
|
2012-02-03 00:08:47 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
2009-10-21 21:25:23 +08:00
|
|
|
MACAddr *mac = qdev_get_prop_ptr(dev, prop);
|
2012-02-03 00:08:47 +08:00
|
|
|
char buffer[2 * 6 + 5 + 1];
|
|
|
|
char *p = buffer;
|
|
|
|
|
|
|
|
snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x",
|
|
|
|
mac->a[0], mac->a[1], mac->a[2],
|
|
|
|
mac->a[3], mac->a[4], mac->a[5]);
|
|
|
|
|
|
|
|
visit_type_str(v, &p, name, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_mac(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
|
|
|
MACAddr *mac = qdev_get_prop_ptr(dev, prop);
|
|
|
|
Error *local_err = NULL;
|
2009-07-15 19:43:31 +08:00
|
|
|
int i, pos;
|
2012-02-03 00:08:47 +08:00
|
|
|
char *str, *p;
|
|
|
|
|
|
|
|
if (dev->state != DEV_STATE_CREATED) {
|
|
|
|
error_set(errp, QERR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_str(v, &str, name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
2009-07-15 19:43:31 +08:00
|
|
|
|
|
|
|
for (i = 0, pos = 0; i < 6; i++, pos += 3) {
|
2009-07-18 01:48:02 +08:00
|
|
|
if (!qemu_isxdigit(str[pos]))
|
2012-02-03 00:08:47 +08:00
|
|
|
goto inval;
|
2009-07-18 01:48:02 +08:00
|
|
|
if (!qemu_isxdigit(str[pos+1]))
|
2012-02-03 00:08:47 +08:00
|
|
|
goto inval;
|
2009-10-21 21:25:23 +08:00
|
|
|
if (i == 5) {
|
|
|
|
if (str[pos+2] != '\0')
|
2012-02-03 00:08:47 +08:00
|
|
|
goto inval;
|
2009-10-21 21:25:23 +08:00
|
|
|
} else {
|
|
|
|
if (str[pos+2] != ':' && str[pos+2] != '-')
|
2012-02-03 00:08:47 +08:00
|
|
|
goto inval;
|
2009-10-21 21:25:23 +08:00
|
|
|
}
|
|
|
|
mac->a[i] = strtol(str+pos, &p, 16);
|
2009-07-15 19:43:31 +08:00
|
|
|
}
|
2012-05-18 19:14:13 +08:00
|
|
|
g_free(str);
|
2012-02-03 00:08:47 +08:00
|
|
|
return;
|
2009-10-21 21:25:23 +08:00
|
|
|
|
2012-02-03 00:08:47 +08:00
|
|
|
inval:
|
|
|
|
error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
|
2012-05-18 19:14:13 +08:00
|
|
|
g_free(str);
|
2009-07-15 19:43:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_macaddr = {
|
2009-10-21 21:25:23 +08:00
|
|
|
.name = "macaddr",
|
2012-02-03 00:08:47 +08:00
|
|
|
.get = get_mac,
|
|
|
|
.set = set_mac,
|
2009-07-15 19:43:31 +08:00
|
|
|
};
|
|
|
|
|
2012-01-24 03:15:11 +08:00
|
|
|
|
|
|
|
/* --- lost tick policy --- */
|
|
|
|
|
2012-02-03 05:09:44 +08:00
|
|
|
static const char *lost_tick_policy_table[LOST_TICK_MAX+1] = {
|
|
|
|
[LOST_TICK_DISCARD] = "discard",
|
|
|
|
[LOST_TICK_DELAY] = "delay",
|
|
|
|
[LOST_TICK_MERGE] = "merge",
|
|
|
|
[LOST_TICK_SLEW] = "slew",
|
|
|
|
[LOST_TICK_MAX] = NULL,
|
2012-01-24 03:15:11 +08:00
|
|
|
};
|
|
|
|
|
2012-02-03 05:09:44 +08:00
|
|
|
QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
|
|
|
|
|
|
|
|
static void get_enum(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
2012-01-24 03:15:11 +08:00
|
|
|
{
|
2012-02-03 05:09:44 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
|
|
|
int *ptr = qdev_get_prop_ptr(dev, prop);
|
2012-01-24 03:15:11 +08:00
|
|
|
|
2012-02-03 05:09:44 +08:00
|
|
|
visit_type_enum(v, ptr, prop->info->enum_table,
|
|
|
|
prop->info->name, prop->name, errp);
|
2012-01-24 03:15:11 +08:00
|
|
|
}
|
|
|
|
|
2012-02-03 05:09:44 +08:00
|
|
|
static void set_enum(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
2012-01-24 03:15:11 +08:00
|
|
|
{
|
2012-02-03 05:09:44 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
|
|
|
int *ptr = qdev_get_prop_ptr(dev, prop);
|
2012-01-24 03:15:11 +08:00
|
|
|
|
2012-02-03 05:09:44 +08:00
|
|
|
if (dev->state != DEV_STATE_CREATED) {
|
|
|
|
error_set(errp, QERR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_enum(v, ptr, prop->info->enum_table,
|
|
|
|
prop->info->name, prop->name, errp);
|
2012-01-24 03:15:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_losttickpolicy = {
|
2012-02-03 05:09:44 +08:00
|
|
|
.name = "LostTickPolicy",
|
|
|
|
.enum_table = lost_tick_policy_table,
|
|
|
|
.get = get_enum,
|
|
|
|
.set = set_enum,
|
2012-01-24 03:15:11 +08:00
|
|
|
};
|
|
|
|
|
2009-07-15 19:59:22 +08:00
|
|
|
/* --- pci address --- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bus-local address, i.e. "$slot" or "$slot.$fn"
|
|
|
|
*/
|
2012-02-09 16:53:32 +08:00
|
|
|
static void set_pci_devfn(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
2009-07-15 19:59:22 +08:00
|
|
|
{
|
2012-02-09 16:53:32 +08:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
2009-07-15 19:59:22 +08:00
|
|
|
uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
unsigned int slot, fn, n;
|
2012-02-09 16:53:32 +08:00
|
|
|
Error *local_err = NULL;
|
2012-05-18 19:14:13 +08:00
|
|
|
char *str;
|
2012-02-09 16:53:32 +08:00
|
|
|
|
|
|
|
if (dev->state != DEV_STATE_CREATED) {
|
|
|
|
error_set(errp, QERR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_str(v, &str, name, &local_err);
|
|
|
|
if (local_err) {
|
2012-04-28 10:32:42 +08:00
|
|
|
error_free(local_err);
|
2012-02-09 16:53:32 +08:00
|
|
|
return set_int32(obj, v, opaque, name, errp);
|
|
|
|
}
|
2009-07-15 19:59:22 +08:00
|
|
|
|
|
|
|
if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
|
|
|
|
fn = 0;
|
|
|
|
if (sscanf(str, "%x%n", &slot, &n) != 1) {
|
2012-02-09 16:53:32 +08:00
|
|
|
goto invalid;
|
2009-07-15 19:59:22 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-09 16:53:32 +08:00
|
|
|
if (str[n] != '\0' || fn > 7 || slot > 31) {
|
|
|
|
goto invalid;
|
|
|
|
}
|
2009-07-15 19:59:22 +08:00
|
|
|
*ptr = slot << 3 | fn;
|
2012-05-18 19:14:13 +08:00
|
|
|
g_free(str);
|
2012-02-09 16:53:32 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
|
2012-05-18 19:14:13 +08:00
|
|
|
g_free(str);
|
2009-07-15 19:59:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
2009-08-29 03:12:16 +08:00
|
|
|
if (*ptr == -1) {
|
2009-07-15 19:59:22 +08:00
|
|
|
return snprintf(dest, len, "<unset>");
|
|
|
|
} else {
|
|
|
|
return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_pci_devfn = {
|
2012-02-03 00:12:19 +08:00
|
|
|
.name = "int32",
|
|
|
|
.legacy_name = "pci-devfn",
|
2009-07-15 19:59:22 +08:00
|
|
|
.print = print_pci_devfn,
|
2012-02-03 00:12:19 +08:00
|
|
|
.get = get_int32,
|
2012-02-09 16:53:32 +08:00
|
|
|
.set = set_pci_devfn,
|
2012-02-03 00:12:19 +08:00
|
|
|
/* FIXME: this should be -1...255, but the address is stored
|
|
|
|
* into an uint32_t rather than int32_t.
|
|
|
|
*/
|
|
|
|
.min = 0,
|
|
|
|
.max = 0xFFFFFFFFULL,
|
2009-07-15 19:59:22 +08:00
|
|
|
};
|
|
|
|
|
2012-03-14 23:57:05 +08:00
|
|
|
/* --- blocksize --- */
|
|
|
|
|
|
|
|
static void set_blocksize(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
|
|
|
int16_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
Error *local_err = NULL;
|
|
|
|
int64_t value;
|
|
|
|
|
|
|
|
if (dev->state != DEV_STATE_CREATED) {
|
|
|
|
error_set(errp, QERR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_int(v, &value, name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (value < prop->info->min || value > prop->info->max) {
|
|
|
|
error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
|
|
|
|
dev->id?:"", name, value, prop->info->min,
|
|
|
|
prop->info->max);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We rely on power-of-2 blocksizes for bitmasks */
|
|
|
|
if ((value & (value - 1)) != 0) {
|
|
|
|
error_set(errp, QERR_PROPERTY_VALUE_NOT_POWER_OF_2,
|
|
|
|
dev->id?:"", name, value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_blocksize = {
|
|
|
|
.name = "blocksize",
|
|
|
|
.get = get_int16,
|
|
|
|
.set = set_blocksize,
|
|
|
|
.min = 512,
|
|
|
|
.max = 65024,
|
|
|
|
};
|
|
|
|
|
2009-07-15 19:43:31 +08:00
|
|
|
/* --- public helpers --- */
|
|
|
|
|
|
|
|
static Property *qdev_prop_walk(Property *props, const char *name)
|
|
|
|
{
|
|
|
|
if (!props)
|
|
|
|
return NULL;
|
|
|
|
while (props->name) {
|
|
|
|
if (strcmp(props->name, name) == 0)
|
|
|
|
return props;
|
|
|
|
props++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Property *qdev_prop_find(DeviceState *dev, const char *name)
|
|
|
|
{
|
|
|
|
Property *prop;
|
|
|
|
|
|
|
|
/* device properties */
|
2011-12-10 00:51:49 +08:00
|
|
|
prop = qdev_prop_walk(qdev_get_props(dev), name);
|
2009-07-15 19:43:31 +08:00
|
|
|
if (prop)
|
|
|
|
return prop;
|
|
|
|
|
|
|
|
/* bus properties */
|
|
|
|
prop = qdev_prop_walk(dev->parent_bus->info->props, name);
|
|
|
|
if (prop)
|
|
|
|
return prop;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-21 21:25:28 +08:00
|
|
|
int qdev_prop_exists(DeviceState *dev, const char *name)
|
|
|
|
{
|
|
|
|
return qdev_prop_find(dev, name) ? true : false;
|
|
|
|
}
|
|
|
|
|
2011-12-19 00:05:07 +08:00
|
|
|
void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
|
|
|
|
Property *prop, const char *value)
|
|
|
|
{
|
|
|
|
switch (ret) {
|
|
|
|
case -EEXIST:
|
|
|
|
error_set(errp, QERR_PROPERTY_VALUE_IN_USE,
|
2011-12-05 01:17:51 +08:00
|
|
|
object_get_typename(OBJECT(dev)), prop->name, value);
|
2011-12-19 00:05:07 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case -EINVAL:
|
|
|
|
error_set(errp, QERR_PROPERTY_VALUE_BAD,
|
2011-12-05 01:17:51 +08:00
|
|
|
object_get_typename(OBJECT(dev)), prop->name, value);
|
2011-12-19 00:05:07 +08:00
|
|
|
break;
|
|
|
|
case -ENOENT:
|
|
|
|
error_set(errp, QERR_PROPERTY_VALUE_NOT_FOUND,
|
2011-12-05 01:17:51 +08:00
|
|
|
object_get_typename(OBJECT(dev)), prop->name, value);
|
2011-12-19 00:05:07 +08:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-15 19:43:31 +08:00
|
|
|
int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
|
|
|
|
{
|
2012-02-02 16:47:13 +08:00
|
|
|
char *legacy_name;
|
|
|
|
Error *err = NULL;
|
2009-07-15 19:43:31 +08:00
|
|
|
|
2012-02-02 16:47:13 +08:00
|
|
|
legacy_name = g_strdup_printf("legacy-%s", name);
|
|
|
|
if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
|
2012-02-09 17:12:08 +08:00
|
|
|
object_property_parse(OBJECT(dev), value, legacy_name, &err);
|
2012-02-02 16:47:13 +08:00
|
|
|
} else {
|
2012-02-09 17:12:08 +08:00
|
|
|
object_property_parse(OBJECT(dev), value, name, &err);
|
2009-07-15 19:43:31 +08:00
|
|
|
}
|
2012-02-02 16:47:13 +08:00
|
|
|
g_free(legacy_name);
|
|
|
|
|
|
|
|
if (err) {
|
2011-12-19 00:05:07 +08:00
|
|
|
qerror_report_err(err);
|
|
|
|
error_free(err);
|
2009-12-16 21:22:11 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2009-07-15 19:43:31 +08:00
|
|
|
}
|
|
|
|
|
2010-06-23 15:15:29 +08:00
|
|
|
void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value)
|
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Error *errp = NULL;
|
|
|
|
object_property_set_bool(OBJECT(dev), value, name, &errp);
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2010-06-23 15:15:29 +08:00
|
|
|
}
|
|
|
|
|
2009-09-30 04:48:25 +08:00
|
|
|
void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
|
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Error *errp = NULL;
|
|
|
|
object_property_set_int(OBJECT(dev), value, name, &errp);
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2009-09-30 04:48:25 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 19:43:31 +08:00
|
|
|
void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
|
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Error *errp = NULL;
|
|
|
|
object_property_set_int(OBJECT(dev), value, name, &errp);
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2009-07-15 19:43:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
|
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Error *errp = NULL;
|
|
|
|
object_property_set_int(OBJECT(dev), value, name, &errp);
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2009-07-15 19:43:31 +08:00
|
|
|
}
|
|
|
|
|
2009-09-10 17:43:25 +08:00
|
|
|
void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
|
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Error *errp = NULL;
|
|
|
|
object_property_set_int(OBJECT(dev), value, name, &errp);
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2009-09-10 17:43:25 +08:00
|
|
|
}
|
|
|
|
|
2009-07-21 19:10:41 +08:00
|
|
|
void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
|
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Error *errp = NULL;
|
|
|
|
object_property_set_int(OBJECT(dev), value, name, &errp);
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2009-07-21 19:10:41 +08:00
|
|
|
}
|
|
|
|
|
2010-06-02 02:32:30 +08:00
|
|
|
void qdev_prop_set_string(DeviceState *dev, const char *name, char *value)
|
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Error *errp = NULL;
|
|
|
|
object_property_set_str(OBJECT(dev), value, name, &errp);
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2010-06-02 02:32:30 +08:00
|
|
|
}
|
|
|
|
|
2010-06-29 22:58:30 +08:00
|
|
|
int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value)
|
2009-07-31 18:25:40 +08:00
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Error *errp = NULL;
|
2012-02-14 17:19:52 +08:00
|
|
|
const char *bdrv_name = value ? bdrv_get_device_name(value) : "";
|
|
|
|
object_property_set_str(OBJECT(dev), bdrv_name,
|
2012-02-02 19:51:44 +08:00
|
|
|
name, &errp);
|
|
|
|
if (errp) {
|
|
|
|
qerror_report_err(errp);
|
|
|
|
error_free(errp);
|
2010-06-29 22:58:30 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2009-07-31 18:25:40 +08:00
|
|
|
}
|
|
|
|
|
2010-06-29 22:58:30 +08:00
|
|
|
void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value)
|
|
|
|
{
|
|
|
|
if (qdev_prop_set_drive(dev, name, value) < 0) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2009-08-03 23:35:18 +08:00
|
|
|
void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value)
|
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Error *errp = NULL;
|
2012-02-14 17:19:52 +08:00
|
|
|
assert(!value || value->label);
|
|
|
|
object_property_set_str(OBJECT(dev),
|
|
|
|
value ? value->label : "", name, &errp);
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2009-08-03 23:35:18 +08:00
|
|
|
}
|
|
|
|
|
2009-10-21 21:25:24 +08:00
|
|
|
void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value)
|
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Error *errp = NULL;
|
2012-02-14 17:19:52 +08:00
|
|
|
assert(!value || value->name);
|
|
|
|
object_property_set_str(OBJECT(dev),
|
|
|
|
value ? value->name : "", name, &errp);
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2009-10-21 21:25:24 +08:00
|
|
|
}
|
|
|
|
|
2009-10-21 21:25:25 +08:00
|
|
|
void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value)
|
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Error *errp = NULL;
|
|
|
|
object_property_set_int(OBJECT(dev), value ? value->id : -1, name, &errp);
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2009-10-21 21:25:25 +08:00
|
|
|
}
|
|
|
|
|
2009-10-21 21:25:23 +08:00
|
|
|
void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
|
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Error *errp = NULL;
|
|
|
|
char str[2 * 6 + 5 + 1];
|
|
|
|
snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
|
|
|
|
value[0], value[1], value[2], value[3], value[4], value[5]);
|
|
|
|
|
|
|
|
object_property_set_str(OBJECT(dev), str, name, &errp);
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2009-10-21 21:25:23 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 19:51:44 +08:00
|
|
|
void qdev_prop_set_enum(DeviceState *dev, const char *name, int value)
|
2012-01-24 03:15:11 +08:00
|
|
|
{
|
2012-02-02 19:51:44 +08:00
|
|
|
Property *prop;
|
|
|
|
Error *errp = NULL;
|
|
|
|
|
|
|
|
prop = qdev_prop_find(dev, name);
|
|
|
|
object_property_set_str(OBJECT(dev), prop->info->enum_table[value],
|
|
|
|
name, &errp);
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2012-01-24 03:15:11 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 19:43:31 +08:00
|
|
|
void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value)
|
|
|
|
{
|
2012-02-02 23:58:31 +08:00
|
|
|
Property *prop;
|
|
|
|
void **ptr;
|
|
|
|
|
|
|
|
prop = qdev_prop_find(dev, name);
|
|
|
|
assert(prop && prop->info == &qdev_prop_ptr);
|
|
|
|
ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
*ptr = value;
|
2009-07-15 19:43:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void qdev_prop_set_defaults(DeviceState *dev, Property *props)
|
|
|
|
{
|
2012-02-02 16:43:02 +08:00
|
|
|
Object *obj = OBJECT(dev);
|
2009-07-15 19:43:31 +08:00
|
|
|
if (!props)
|
|
|
|
return;
|
2012-02-02 16:43:02 +08:00
|
|
|
for (; props->name; props++) {
|
|
|
|
Error *errp = NULL;
|
|
|
|
if (props->qtype == QTYPE_NONE) {
|
|
|
|
continue;
|
2009-07-15 19:43:31 +08:00
|
|
|
}
|
2012-02-02 16:43:02 +08:00
|
|
|
if (props->qtype == QTYPE_QBOOL) {
|
|
|
|
object_property_set_bool(obj, props->defval, props->name, &errp);
|
|
|
|
} else if (props->info->enum_table) {
|
|
|
|
object_property_set_str(obj, props->info->enum_table[props->defval],
|
|
|
|
props->name, &errp);
|
|
|
|
} else if (props->qtype == QTYPE_QINT) {
|
|
|
|
object_property_set_int(obj, props->defval, props->name, &errp);
|
|
|
|
}
|
2012-02-14 17:19:53 +08:00
|
|
|
assert_no_error(errp);
|
2009-07-15 19:43:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-08 20:11:33 +08:00
|
|
|
static QTAILQ_HEAD(, GlobalProperty) global_props = QTAILQ_HEAD_INITIALIZER(global_props);
|
2009-07-15 19:48:21 +08:00
|
|
|
|
2010-03-16 00:01:24 +08:00
|
|
|
static void qdev_prop_register_global(GlobalProperty *prop)
|
2009-07-15 19:48:21 +08:00
|
|
|
{
|
2009-12-08 20:11:33 +08:00
|
|
|
QTAILQ_INSERT_TAIL(&global_props, prop, next);
|
2009-07-15 19:48:21 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 20:11:33 +08:00
|
|
|
void qdev_prop_register_global_list(GlobalProperty *props)
|
2009-07-15 19:48:21 +08:00
|
|
|
{
|
2009-12-08 20:11:33 +08:00
|
|
|
int i;
|
2009-07-15 19:48:21 +08:00
|
|
|
|
2009-12-08 20:11:33 +08:00
|
|
|
for (i = 0; props[i].driver != NULL; i++) {
|
|
|
|
qdev_prop_register_global(props+i);
|
2009-07-15 19:48:21 +08:00
|
|
|
}
|
2009-12-08 20:11:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void qdev_prop_set_globals(DeviceState *dev)
|
|
|
|
{
|
|
|
|
GlobalProperty *prop;
|
|
|
|
|
|
|
|
QTAILQ_FOREACH(prop, &global_props, next) {
|
2011-12-05 01:17:51 +08:00
|
|
|
if (strcmp(object_get_typename(OBJECT(dev)), prop->driver) != 0 &&
|
2011-12-10 00:51:49 +08:00
|
|
|
strcmp(qdev_get_bus_info(dev)->name, prop->driver) != 0) {
|
2009-07-15 19:48:21 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (qdev_prop_parse(dev, prop->property, prop->value) != 0) {
|
2009-12-16 21:22:11 +08:00
|
|
|
exit(1);
|
2009-07-15 19:48:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-16 00:01:24 +08:00
|
|
|
|
|
|
|
static int qdev_add_one_global(QemuOpts *opts, void *opaque)
|
|
|
|
{
|
|
|
|
GlobalProperty *g;
|
|
|
|
|
2011-08-21 11:09:37 +08:00
|
|
|
g = g_malloc0(sizeof(*g));
|
2010-03-16 00:01:24 +08:00
|
|
|
g->driver = qemu_opt_get(opts, "driver");
|
|
|
|
g->property = qemu_opt_get(opts, "property");
|
|
|
|
g->value = qemu_opt_get(opts, "value");
|
|
|
|
qdev_prop_register_global(g);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_add_globals(void)
|
|
|
|
{
|
2010-08-20 19:52:01 +08:00
|
|
|
qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global, NULL, 0);
|
2010-03-16 00:01:24 +08:00
|
|
|
}
|